1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
4 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
5 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
6 *
7 * High-resolution kernel timers
8 *
9 * In contrast to the low-resolution timeout API, aka timer wheel,
10 * hrtimers provide finer resolution and accuracy depending on system
11 * configuration and capabilities.
12 *
13 * Started by: Thomas Gleixner and Ingo Molnar
14 *
15 * Credits:
16 * Based on the original timer wheel code
17 *
18 * Help, testing, suggestions, bugfixes, improvements were
19 * provided by:
20 *
21 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
22 * et. al.
23 */
24
25 #include <linux/cpu.h>
26 #include <linux/export.h>
27 #include <linux/percpu.h>
28 #include <linux/hrtimer.h>
29 #include <linux/notifier.h>
30 #include <linux/syscalls.h>
31 #include <linux/interrupt.h>
32 #include <linux/tick.h>
33 #include <linux/err.h>
34 #include <linux/debugobjects.h>
35 #include <linux/sched/signal.h>
36 #include <linux/sched/sysctl.h>
37 #include <linux/sched/rt.h>
38 #include <linux/sched/deadline.h>
39 #include <linux/sched/nohz.h>
40 #include <linux/sched/debug.h>
41 #include <linux/sched/isolation.h>
42 #include <linux/timer.h>
43 #include <linux/freezer.h>
44 #include <linux/compat.h>
45
46 #include <linux/uaccess.h>
47
48 #include <trace/events/timer.h>
49
50 #include "tick-internal.h"
51
52 /*
53 * Masks for selecting the soft and hard context timers from
54 * cpu_base->active
55 */
56 #define MASK_SHIFT (HRTIMER_BASE_MONOTONIC_SOFT)
57 #define HRTIMER_ACTIVE_HARD ((1U << MASK_SHIFT) - 1)
58 #define HRTIMER_ACTIVE_SOFT (HRTIMER_ACTIVE_HARD << MASK_SHIFT)
59 #define HRTIMER_ACTIVE_ALL (HRTIMER_ACTIVE_SOFT | HRTIMER_ACTIVE_HARD)
60
61 static void retrigger_next_event(void *arg);
62
63 /*
64 * The timer bases:
65 *
66 * There are more clockids than hrtimer bases. Thus, we index
67 * into the timer bases by the hrtimer_base_type enum. When trying
68 * to reach a base using a clockid, hrtimer_clockid_to_base()
69 * is used to convert from clockid to the proper hrtimer_base_type.
70 */
71 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
72 {
73 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
74 .clock_base =
75 {
76 {
77 .index = HRTIMER_BASE_MONOTONIC,
78 .clockid = CLOCK_MONOTONIC,
79 .get_time = &ktime_get,
80 },
81 {
82 .index = HRTIMER_BASE_REALTIME,
83 .clockid = CLOCK_REALTIME,
84 .get_time = &ktime_get_real,
85 },
86 {
87 .index = HRTIMER_BASE_BOOTTIME,
88 .clockid = CLOCK_BOOTTIME,
89 .get_time = &ktime_get_boottime,
90 },
91 {
92 .index = HRTIMER_BASE_TAI,
93 .clockid = CLOCK_TAI,
94 .get_time = &ktime_get_clocktai,
95 },
96 {
97 .index = HRTIMER_BASE_MONOTONIC_SOFT,
98 .clockid = CLOCK_MONOTONIC,
99 .get_time = &ktime_get,
100 },
101 {
102 .index = HRTIMER_BASE_REALTIME_SOFT,
103 .clockid = CLOCK_REALTIME,
104 .get_time = &ktime_get_real,
105 },
106 {
107 .index = HRTIMER_BASE_BOOTTIME_SOFT,
108 .clockid = CLOCK_BOOTTIME,
109 .get_time = &ktime_get_boottime,
110 },
111 {
112 .index = HRTIMER_BASE_TAI_SOFT,
113 .clockid = CLOCK_TAI,
114 .get_time = &ktime_get_clocktai,
115 },
116 },
117 .csd = CSD_INIT(retrigger_next_event, NULL)
118 };
119
120 static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
121 /* Make sure we catch unsupported clockids */
122 [0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES,
123
124 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
125 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
126 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
127 [CLOCK_TAI] = HRTIMER_BASE_TAI,
128 };
129
hrtimer_base_is_online(struct hrtimer_cpu_base * base)130 static inline bool hrtimer_base_is_online(struct hrtimer_cpu_base *base)
131 {
132 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
133 return true;
134 else
135 return likely(base->online);
136 }
137
138 /*
139 * Functions and macros which are different for UP/SMP systems are kept in a
140 * single place
141 */
142 #ifdef CONFIG_SMP
143
144 /*
145 * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
146 * such that hrtimer_callback_running() can unconditionally dereference
147 * timer->base->cpu_base
148 */
149 static struct hrtimer_cpu_base migration_cpu_base = {
150 .clock_base = { {
151 .cpu_base = &migration_cpu_base,
152 .seq = SEQCNT_RAW_SPINLOCK_ZERO(migration_cpu_base.seq,
153 &migration_cpu_base.lock),
154 }, },
155 };
156
157 #define migration_base migration_cpu_base.clock_base[0]
158
159 /*
160 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
161 * means that all timers which are tied to this base via timer->base are
162 * locked, and the base itself is locked too.
163 *
164 * So __run_timers/migrate_timers can safely modify all timers which could
165 * be found on the lists/queues.
166 *
167 * When the timer's base is locked, and the timer removed from list, it is
168 * possible to set timer->base = &migration_base and drop the lock: the timer
169 * remains locked.
170 */
171 static
lock_hrtimer_base(const struct hrtimer * timer,unsigned long * flags)172 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
173 unsigned long *flags)
174 __acquires(&timer->base->lock)
175 {
176 struct hrtimer_clock_base *base;
177
178 for (;;) {
179 base = READ_ONCE(timer->base);
180 if (likely(base != &migration_base)) {
181 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
182 if (likely(base == timer->base))
183 return base;
184 /* The timer has migrated to another CPU: */
185 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
186 }
187 cpu_relax();
188 }
189 }
190
191 /*
192 * Check if the elected target is suitable considering its next
193 * event and the hotplug state of the current CPU.
194 *
195 * If the elected target is remote and its next event is after the timer
196 * to queue, then a remote reprogram is necessary. However there is no
197 * guarantee the IPI handling the operation would arrive in time to meet
198 * the high resolution deadline. In this case the local CPU becomes a
199 * preferred target, unless it is offline.
200 *
201 * High and low resolution modes are handled the same way for simplicity.
202 *
203 * Called with cpu_base->lock of target cpu held.
204 */
hrtimer_suitable_target(struct hrtimer * timer,struct hrtimer_clock_base * new_base,struct hrtimer_cpu_base * new_cpu_base,struct hrtimer_cpu_base * this_cpu_base)205 static bool hrtimer_suitable_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base,
206 struct hrtimer_cpu_base *new_cpu_base,
207 struct hrtimer_cpu_base *this_cpu_base)
208 {
209 ktime_t expires;
210
211 /*
212 * The local CPU clockevent can be reprogrammed. Also get_target_base()
213 * guarantees it is online.
214 */
215 if (new_cpu_base == this_cpu_base)
216 return true;
217
218 /*
219 * The offline local CPU can't be the default target if the
220 * next remote target event is after this timer. Keep the
221 * elected new base. An IPI will we issued to reprogram
222 * it as a last resort.
223 */
224 if (!hrtimer_base_is_online(this_cpu_base))
225 return true;
226
227 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
228
229 return expires >= new_base->cpu_base->expires_next;
230 }
231
get_target_base(struct hrtimer_cpu_base * base,int pinned)232 static inline struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base, int pinned)
233 {
234 if (!hrtimer_base_is_online(base)) {
235 int cpu = cpumask_any_and(cpu_online_mask, housekeeping_cpumask(HK_TYPE_TIMER));
236
237 return &per_cpu(hrtimer_bases, cpu);
238 }
239
240 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
241 if (static_branch_likely(&timers_migration_enabled) && !pinned)
242 return &per_cpu(hrtimer_bases, get_nohz_timer_target());
243 #endif
244 return base;
245 }
246
247 /*
248 * We switch the timer base to a power-optimized selected CPU target,
249 * if:
250 * - NO_HZ_COMMON is enabled
251 * - timer migration is enabled
252 * - the timer callback is not running
253 * - the timer is not the first expiring timer on the new target
254 *
255 * If one of the above requirements is not fulfilled we move the timer
256 * to the current CPU or leave it on the previously assigned CPU if
257 * the timer callback is currently running.
258 */
259 static inline struct hrtimer_clock_base *
switch_hrtimer_base(struct hrtimer * timer,struct hrtimer_clock_base * base,int pinned)260 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
261 int pinned)
262 {
263 struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
264 struct hrtimer_clock_base *new_base;
265 int basenum = base->index;
266
267 this_cpu_base = this_cpu_ptr(&hrtimer_bases);
268 new_cpu_base = get_target_base(this_cpu_base, pinned);
269 again:
270 new_base = &new_cpu_base->clock_base[basenum];
271
272 if (base != new_base) {
273 /*
274 * We are trying to move timer to new_base.
275 * However we can't change timer's base while it is running,
276 * so we keep it on the same CPU. No hassle vs. reprogramming
277 * the event source in the high resolution case. The softirq
278 * code will take care of this when the timer function has
279 * completed. There is no conflict as we hold the lock until
280 * the timer is enqueued.
281 */
282 if (unlikely(hrtimer_callback_running(timer)))
283 return base;
284
285 /* See the comment in lock_hrtimer_base() */
286 WRITE_ONCE(timer->base, &migration_base);
287 raw_spin_unlock(&base->cpu_base->lock);
288 raw_spin_lock(&new_base->cpu_base->lock);
289
290 if (!hrtimer_suitable_target(timer, new_base, new_cpu_base,
291 this_cpu_base)) {
292 raw_spin_unlock(&new_base->cpu_base->lock);
293 raw_spin_lock(&base->cpu_base->lock);
294 new_cpu_base = this_cpu_base;
295 WRITE_ONCE(timer->base, base);
296 goto again;
297 }
298 WRITE_ONCE(timer->base, new_base);
299 } else {
300 if (!hrtimer_suitable_target(timer, new_base, new_cpu_base, this_cpu_base)) {
301 new_cpu_base = this_cpu_base;
302 goto again;
303 }
304 }
305 return new_base;
306 }
307
308 #else /* CONFIG_SMP */
309
310 static inline struct hrtimer_clock_base *
lock_hrtimer_base(const struct hrtimer * timer,unsigned long * flags)311 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
312 __acquires(&timer->base->cpu_base->lock)
313 {
314 struct hrtimer_clock_base *base = timer->base;
315
316 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
317
318 return base;
319 }
320
321 # define switch_hrtimer_base(t, b, p) (b)
322
323 #endif /* !CONFIG_SMP */
324
325 /*
326 * Functions for the union type storage format of ktime_t which are
327 * too large for inlining:
328 */
329 #if BITS_PER_LONG < 64
330 /*
331 * Divide a ktime value by a nanosecond value
332 */
__ktime_divns(const ktime_t kt,s64 div)333 s64 __ktime_divns(const ktime_t kt, s64 div)
334 {
335 int sft = 0;
336 s64 dclc;
337 u64 tmp;
338
339 dclc = ktime_to_ns(kt);
340 tmp = dclc < 0 ? -dclc : dclc;
341
342 /* Make sure the divisor is less than 2^32: */
343 while (div >> 32) {
344 sft++;
345 div >>= 1;
346 }
347 tmp >>= sft;
348 do_div(tmp, (u32) div);
349 return dclc < 0 ? -tmp : tmp;
350 }
351 EXPORT_SYMBOL_GPL(__ktime_divns);
352 #endif /* BITS_PER_LONG >= 64 */
353
354 /*
355 * Add two ktime values and do a safety check for overflow:
356 */
ktime_add_safe(const ktime_t lhs,const ktime_t rhs)357 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
358 {
359 ktime_t res = ktime_add_unsafe(lhs, rhs);
360
361 /*
362 * We use KTIME_SEC_MAX here, the maximum timeout which we can
363 * return to user space in a timespec:
364 */
365 if (res < 0 || res < lhs || res < rhs)
366 res = ktime_set(KTIME_SEC_MAX, 0);
367
368 return res;
369 }
370
371 EXPORT_SYMBOL_GPL(ktime_add_safe);
372
373 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
374
375 static const struct debug_obj_descr hrtimer_debug_descr;
376
hrtimer_debug_hint(void * addr)377 static void *hrtimer_debug_hint(void *addr)
378 {
379 return ((struct hrtimer *) addr)->function;
380 }
381
382 /*
383 * fixup_init is called when:
384 * - an active object is initialized
385 */
hrtimer_fixup_init(void * addr,enum debug_obj_state state)386 static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
387 {
388 struct hrtimer *timer = addr;
389
390 switch (state) {
391 case ODEBUG_STATE_ACTIVE:
392 hrtimer_cancel(timer);
393 debug_object_init(timer, &hrtimer_debug_descr);
394 return true;
395 default:
396 return false;
397 }
398 }
399
400 /*
401 * fixup_activate is called when:
402 * - an active object is activated
403 * - an unknown non-static object is activated
404 */
hrtimer_fixup_activate(void * addr,enum debug_obj_state state)405 static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
406 {
407 switch (state) {
408 case ODEBUG_STATE_ACTIVE:
409 WARN_ON(1);
410 fallthrough;
411 default:
412 return false;
413 }
414 }
415
416 /*
417 * fixup_free is called when:
418 * - an active object is freed
419 */
hrtimer_fixup_free(void * addr,enum debug_obj_state state)420 static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
421 {
422 struct hrtimer *timer = addr;
423
424 switch (state) {
425 case ODEBUG_STATE_ACTIVE:
426 hrtimer_cancel(timer);
427 debug_object_free(timer, &hrtimer_debug_descr);
428 return true;
429 default:
430 return false;
431 }
432 }
433
434 static const struct debug_obj_descr hrtimer_debug_descr = {
435 .name = "hrtimer",
436 .debug_hint = hrtimer_debug_hint,
437 .fixup_init = hrtimer_fixup_init,
438 .fixup_activate = hrtimer_fixup_activate,
439 .fixup_free = hrtimer_fixup_free,
440 };
441
debug_hrtimer_init(struct hrtimer * timer)442 static inline void debug_hrtimer_init(struct hrtimer *timer)
443 {
444 debug_object_init(timer, &hrtimer_debug_descr);
445 }
446
debug_hrtimer_activate(struct hrtimer * timer,enum hrtimer_mode mode)447 static inline void debug_hrtimer_activate(struct hrtimer *timer,
448 enum hrtimer_mode mode)
449 {
450 debug_object_activate(timer, &hrtimer_debug_descr);
451 }
452
debug_hrtimer_deactivate(struct hrtimer * timer)453 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
454 {
455 debug_object_deactivate(timer, &hrtimer_debug_descr);
456 }
457
458 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
459 enum hrtimer_mode mode);
460
hrtimer_init_on_stack(struct hrtimer * timer,clockid_t clock_id,enum hrtimer_mode mode)461 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
462 enum hrtimer_mode mode)
463 {
464 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
465 __hrtimer_init(timer, clock_id, mode);
466 }
467 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
468
469 static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
470 clockid_t clock_id, enum hrtimer_mode mode);
471
hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)472 void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
473 clockid_t clock_id, enum hrtimer_mode mode)
474 {
475 debug_object_init_on_stack(&sl->timer, &hrtimer_debug_descr);
476 __hrtimer_init_sleeper(sl, clock_id, mode);
477 }
478 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper_on_stack);
479
destroy_hrtimer_on_stack(struct hrtimer * timer)480 void destroy_hrtimer_on_stack(struct hrtimer *timer)
481 {
482 debug_object_free(timer, &hrtimer_debug_descr);
483 }
484 EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
485
486 #else
487
debug_hrtimer_init(struct hrtimer * timer)488 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
debug_hrtimer_activate(struct hrtimer * timer,enum hrtimer_mode mode)489 static inline void debug_hrtimer_activate(struct hrtimer *timer,
490 enum hrtimer_mode mode) { }
debug_hrtimer_deactivate(struct hrtimer * timer)491 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
492 #endif
493
494 static inline void
debug_init(struct hrtimer * timer,clockid_t clockid,enum hrtimer_mode mode)495 debug_init(struct hrtimer *timer, clockid_t clockid,
496 enum hrtimer_mode mode)
497 {
498 debug_hrtimer_init(timer);
499 trace_hrtimer_init(timer, clockid, mode);
500 }
501
debug_activate(struct hrtimer * timer,enum hrtimer_mode mode)502 static inline void debug_activate(struct hrtimer *timer,
503 enum hrtimer_mode mode)
504 {
505 debug_hrtimer_activate(timer, mode);
506 trace_hrtimer_start(timer, mode);
507 }
508
debug_deactivate(struct hrtimer * timer)509 static inline void debug_deactivate(struct hrtimer *timer)
510 {
511 debug_hrtimer_deactivate(timer);
512 trace_hrtimer_cancel(timer);
513 }
514
515 static struct hrtimer_clock_base *
__next_base(struct hrtimer_cpu_base * cpu_base,unsigned int * active)516 __next_base(struct hrtimer_cpu_base *cpu_base, unsigned int *active)
517 {
518 unsigned int idx;
519
520 if (!*active)
521 return NULL;
522
523 idx = __ffs(*active);
524 *active &= ~(1U << idx);
525
526 return &cpu_base->clock_base[idx];
527 }
528
529 #define for_each_active_base(base, cpu_base, active) \
530 while ((base = __next_base((cpu_base), &(active))))
531
__hrtimer_next_event_base(struct hrtimer_cpu_base * cpu_base,const struct hrtimer * exclude,unsigned int active,ktime_t expires_next)532 static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base,
533 const struct hrtimer *exclude,
534 unsigned int active,
535 ktime_t expires_next)
536 {
537 struct hrtimer_clock_base *base;
538 ktime_t expires;
539
540 for_each_active_base(base, cpu_base, active) {
541 struct timerqueue_node *next;
542 struct hrtimer *timer;
543
544 next = timerqueue_getnext(&base->active);
545 timer = container_of(next, struct hrtimer, node);
546 if (timer == exclude) {
547 /* Get to the next timer in the queue. */
548 next = timerqueue_iterate_next(next);
549 if (!next)
550 continue;
551
552 timer = container_of(next, struct hrtimer, node);
553 }
554 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
555 if (expires < expires_next) {
556 expires_next = expires;
557
558 /* Skip cpu_base update if a timer is being excluded. */
559 if (exclude)
560 continue;
561
562 if (timer->is_soft)
563 cpu_base->softirq_next_timer = timer;
564 else
565 cpu_base->next_timer = timer;
566 }
567 }
568 /*
569 * clock_was_set() might have changed base->offset of any of
570 * the clock bases so the result might be negative. Fix it up
571 * to prevent a false positive in clockevents_program_event().
572 */
573 if (expires_next < 0)
574 expires_next = 0;
575 return expires_next;
576 }
577
578 /*
579 * Recomputes cpu_base::*next_timer and returns the earliest expires_next
580 * but does not set cpu_base::*expires_next, that is done by
581 * hrtimer[_force]_reprogram and hrtimer_interrupt only. When updating
582 * cpu_base::*expires_next right away, reprogramming logic would no longer
583 * work.
584 *
585 * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
586 * those timers will get run whenever the softirq gets handled, at the end of
587 * hrtimer_run_softirq(), hrtimer_update_softirq_timer() will re-add these bases.
588 *
589 * Therefore softirq values are those from the HRTIMER_ACTIVE_SOFT clock bases.
590 * The !softirq values are the minima across HRTIMER_ACTIVE_ALL, unless an actual
591 * softirq is pending, in which case they're the minima of HRTIMER_ACTIVE_HARD.
592 *
593 * @active_mask must be one of:
594 * - HRTIMER_ACTIVE_ALL,
595 * - HRTIMER_ACTIVE_SOFT, or
596 * - HRTIMER_ACTIVE_HARD.
597 */
598 static ktime_t
__hrtimer_get_next_event(struct hrtimer_cpu_base * cpu_base,unsigned int active_mask)599 __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_mask)
600 {
601 unsigned int active;
602 struct hrtimer *next_timer = NULL;
603 ktime_t expires_next = KTIME_MAX;
604
605 if (!cpu_base->softirq_activated && (active_mask & HRTIMER_ACTIVE_SOFT)) {
606 active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
607 cpu_base->softirq_next_timer = NULL;
608 expires_next = __hrtimer_next_event_base(cpu_base, NULL,
609 active, KTIME_MAX);
610
611 next_timer = cpu_base->softirq_next_timer;
612 }
613
614 if (active_mask & HRTIMER_ACTIVE_HARD) {
615 active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
616 cpu_base->next_timer = next_timer;
617 expires_next = __hrtimer_next_event_base(cpu_base, NULL, active,
618 expires_next);
619 }
620
621 return expires_next;
622 }
623
hrtimer_update_next_event(struct hrtimer_cpu_base * cpu_base)624 static ktime_t hrtimer_update_next_event(struct hrtimer_cpu_base *cpu_base)
625 {
626 ktime_t expires_next, soft = KTIME_MAX;
627
628 /*
629 * If the soft interrupt has already been activated, ignore the
630 * soft bases. They will be handled in the already raised soft
631 * interrupt.
632 */
633 if (!cpu_base->softirq_activated) {
634 soft = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
635 /*
636 * Update the soft expiry time. clock_settime() might have
637 * affected it.
638 */
639 cpu_base->softirq_expires_next = soft;
640 }
641
642 expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD);
643 /*
644 * If a softirq timer is expiring first, update cpu_base->next_timer
645 * and program the hardware with the soft expiry time.
646 */
647 if (expires_next > soft) {
648 cpu_base->next_timer = cpu_base->softirq_next_timer;
649 expires_next = soft;
650 }
651
652 return expires_next;
653 }
654
hrtimer_update_base(struct hrtimer_cpu_base * base)655 static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
656 {
657 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
658 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
659 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
660
661 ktime_t now = ktime_get_update_offsets_now(&base->clock_was_set_seq,
662 offs_real, offs_boot, offs_tai);
663
664 base->clock_base[HRTIMER_BASE_REALTIME_SOFT].offset = *offs_real;
665 base->clock_base[HRTIMER_BASE_BOOTTIME_SOFT].offset = *offs_boot;
666 base->clock_base[HRTIMER_BASE_TAI_SOFT].offset = *offs_tai;
667
668 return now;
669 }
670
671 /*
672 * Is the high resolution mode active ?
673 */
__hrtimer_hres_active(struct hrtimer_cpu_base * cpu_base)674 static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
675 {
676 return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
677 cpu_base->hres_active : 0;
678 }
679
hrtimer_hres_active(void)680 static inline int hrtimer_hres_active(void)
681 {
682 return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
683 }
684
__hrtimer_reprogram(struct hrtimer_cpu_base * cpu_base,struct hrtimer * next_timer,ktime_t expires_next)685 static void __hrtimer_reprogram(struct hrtimer_cpu_base *cpu_base,
686 struct hrtimer *next_timer,
687 ktime_t expires_next)
688 {
689 cpu_base->expires_next = expires_next;
690
691 /*
692 * If hres is not active, hardware does not have to be
693 * reprogrammed yet.
694 *
695 * If a hang was detected in the last timer interrupt then we
696 * leave the hang delay active in the hardware. We want the
697 * system to make progress. That also prevents the following
698 * scenario:
699 * T1 expires 50ms from now
700 * T2 expires 5s from now
701 *
702 * T1 is removed, so this code is called and would reprogram
703 * the hardware to 5s from now. Any hrtimer_start after that
704 * will not reprogram the hardware due to hang_detected being
705 * set. So we'd effectively block all timers until the T2 event
706 * fires.
707 */
708 if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
709 return;
710
711 tick_program_event(expires_next, 1);
712 }
713
714 /*
715 * Reprogram the event source with checking both queues for the
716 * next event
717 * Called with interrupts disabled and base->lock held
718 */
719 static void
hrtimer_force_reprogram(struct hrtimer_cpu_base * cpu_base,int skip_equal)720 hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
721 {
722 ktime_t expires_next;
723
724 expires_next = hrtimer_update_next_event(cpu_base);
725
726 if (skip_equal && expires_next == cpu_base->expires_next)
727 return;
728
729 __hrtimer_reprogram(cpu_base, cpu_base->next_timer, expires_next);
730 }
731
732 /* High resolution timer related functions */
733 #ifdef CONFIG_HIGH_RES_TIMERS
734
735 /*
736 * High resolution timer enabled ?
737 */
738 static bool hrtimer_hres_enabled __read_mostly = true;
739 unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
740 EXPORT_SYMBOL_GPL(hrtimer_resolution);
741
742 /*
743 * Enable / Disable high resolution mode
744 */
setup_hrtimer_hres(char * str)745 static int __init setup_hrtimer_hres(char *str)
746 {
747 return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
748 }
749
750 __setup("highres=", setup_hrtimer_hres);
751
752 /*
753 * hrtimer_high_res_enabled - query, if the highres mode is enabled
754 */
hrtimer_is_hres_enabled(void)755 static inline int hrtimer_is_hres_enabled(void)
756 {
757 return hrtimer_hres_enabled;
758 }
759
760 /*
761 * Switch to high resolution mode
762 */
hrtimer_switch_to_hres(void)763 static void hrtimer_switch_to_hres(void)
764 {
765 struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
766
767 if (tick_init_highres()) {
768 pr_warn("Could not switch to high resolution mode on CPU %u\n",
769 base->cpu);
770 return;
771 }
772 base->hres_active = 1;
773 hrtimer_resolution = HIGH_RES_NSEC;
774
775 tick_setup_sched_timer();
776 /* "Retrigger" the interrupt to get things going */
777 retrigger_next_event(NULL);
778 }
779
780 #else
781
hrtimer_is_hres_enabled(void)782 static inline int hrtimer_is_hres_enabled(void) { return 0; }
hrtimer_switch_to_hres(void)783 static inline void hrtimer_switch_to_hres(void) { }
784
785 #endif /* CONFIG_HIGH_RES_TIMERS */
786 /*
787 * Retrigger next event is called after clock was set with interrupts
788 * disabled through an SMP function call or directly from low level
789 * resume code.
790 *
791 * This is only invoked when:
792 * - CONFIG_HIGH_RES_TIMERS is enabled.
793 * - CONFIG_NOHZ_COMMON is enabled
794 *
795 * For the other cases this function is empty and because the call sites
796 * are optimized out it vanishes as well, i.e. no need for lots of
797 * #ifdeffery.
798 */
retrigger_next_event(void * arg)799 static void retrigger_next_event(void *arg)
800 {
801 struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
802
803 /*
804 * When high resolution mode or nohz is active, then the offsets of
805 * CLOCK_REALTIME/TAI/BOOTTIME have to be updated. Otherwise the
806 * next tick will take care of that.
807 *
808 * If high resolution mode is active then the next expiring timer
809 * must be reevaluated and the clock event device reprogrammed if
810 * necessary.
811 *
812 * In the NOHZ case the update of the offset and the reevaluation
813 * of the next expiring timer is enough. The return from the SMP
814 * function call will take care of the reprogramming in case the
815 * CPU was in a NOHZ idle sleep.
816 */
817 if (!__hrtimer_hres_active(base) && !tick_nohz_active)
818 return;
819
820 raw_spin_lock(&base->lock);
821 hrtimer_update_base(base);
822 if (__hrtimer_hres_active(base))
823 hrtimer_force_reprogram(base, 0);
824 else
825 hrtimer_update_next_event(base);
826 raw_spin_unlock(&base->lock);
827 }
828
829 /*
830 * When a timer is enqueued and expires earlier than the already enqueued
831 * timers, we have to check, whether it expires earlier than the timer for
832 * which the clock event device was armed.
833 *
834 * Called with interrupts disabled and base->cpu_base.lock held
835 */
hrtimer_reprogram(struct hrtimer * timer,bool reprogram)836 static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
837 {
838 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
839 struct hrtimer_clock_base *base = timer->base;
840 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
841
842 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
843
844 /*
845 * CLOCK_REALTIME timer might be requested with an absolute
846 * expiry time which is less than base->offset. Set it to 0.
847 */
848 if (expires < 0)
849 expires = 0;
850
851 if (timer->is_soft) {
852 /*
853 * soft hrtimer could be started on a remote CPU. In this
854 * case softirq_expires_next needs to be updated on the
855 * remote CPU. The soft hrtimer will not expire before the
856 * first hard hrtimer on the remote CPU -
857 * hrtimer_check_target() prevents this case.
858 */
859 struct hrtimer_cpu_base *timer_cpu_base = base->cpu_base;
860
861 if (timer_cpu_base->softirq_activated)
862 return;
863
864 if (!ktime_before(expires, timer_cpu_base->softirq_expires_next))
865 return;
866
867 timer_cpu_base->softirq_next_timer = timer;
868 timer_cpu_base->softirq_expires_next = expires;
869
870 if (!ktime_before(expires, timer_cpu_base->expires_next) ||
871 !reprogram)
872 return;
873 }
874
875 /*
876 * If the timer is not on the current cpu, we cannot reprogram
877 * the other cpus clock event device.
878 */
879 if (base->cpu_base != cpu_base)
880 return;
881
882 if (expires >= cpu_base->expires_next)
883 return;
884
885 /*
886 * If the hrtimer interrupt is running, then it will reevaluate the
887 * clock bases and reprogram the clock event device.
888 */
889 if (cpu_base->in_hrtirq)
890 return;
891
892 cpu_base->next_timer = timer;
893
894 __hrtimer_reprogram(cpu_base, timer, expires);
895 }
896
update_needs_ipi(struct hrtimer_cpu_base * cpu_base,unsigned int active)897 static bool update_needs_ipi(struct hrtimer_cpu_base *cpu_base,
898 unsigned int active)
899 {
900 struct hrtimer_clock_base *base;
901 unsigned int seq;
902 ktime_t expires;
903
904 /*
905 * Update the base offsets unconditionally so the following
906 * checks whether the SMP function call is required works.
907 *
908 * The update is safe even when the remote CPU is in the hrtimer
909 * interrupt or the hrtimer soft interrupt and expiring affected
910 * bases. Either it will see the update before handling a base or
911 * it will see it when it finishes the processing and reevaluates
912 * the next expiring timer.
913 */
914 seq = cpu_base->clock_was_set_seq;
915 hrtimer_update_base(cpu_base);
916
917 /*
918 * If the sequence did not change over the update then the
919 * remote CPU already handled it.
920 */
921 if (seq == cpu_base->clock_was_set_seq)
922 return false;
923
924 /*
925 * If the remote CPU is currently handling an hrtimer interrupt, it
926 * will reevaluate the first expiring timer of all clock bases
927 * before reprogramming. Nothing to do here.
928 */
929 if (cpu_base->in_hrtirq)
930 return false;
931
932 /*
933 * Walk the affected clock bases and check whether the first expiring
934 * timer in a clock base is moving ahead of the first expiring timer of
935 * @cpu_base. If so, the IPI must be invoked because per CPU clock
936 * event devices cannot be remotely reprogrammed.
937 */
938 active &= cpu_base->active_bases;
939
940 for_each_active_base(base, cpu_base, active) {
941 struct timerqueue_node *next;
942
943 next = timerqueue_getnext(&base->active);
944 expires = ktime_sub(next->expires, base->offset);
945 if (expires < cpu_base->expires_next)
946 return true;
947
948 /* Extra check for softirq clock bases */
949 if (base->clockid < HRTIMER_BASE_MONOTONIC_SOFT)
950 continue;
951 if (cpu_base->softirq_activated)
952 continue;
953 if (expires < cpu_base->softirq_expires_next)
954 return true;
955 }
956 return false;
957 }
958
959 /*
960 * Clock was set. This might affect CLOCK_REALTIME, CLOCK_TAI and
961 * CLOCK_BOOTTIME (for late sleep time injection).
962 *
963 * This requires to update the offsets for these clocks
964 * vs. CLOCK_MONOTONIC. When high resolution timers are enabled, then this
965 * also requires to eventually reprogram the per CPU clock event devices
966 * when the change moves an affected timer ahead of the first expiring
967 * timer on that CPU. Obviously remote per CPU clock event devices cannot
968 * be reprogrammed. The other reason why an IPI has to be sent is when the
969 * system is in !HIGH_RES and NOHZ mode. The NOHZ mode updates the offsets
970 * in the tick, which obviously might be stopped, so this has to bring out
971 * the remote CPU which might sleep in idle to get this sorted.
972 */
clock_was_set(unsigned int bases)973 void clock_was_set(unsigned int bases)
974 {
975 struct hrtimer_cpu_base *cpu_base = raw_cpu_ptr(&hrtimer_bases);
976 cpumask_var_t mask;
977 int cpu;
978
979 if (!__hrtimer_hres_active(cpu_base) && !tick_nohz_active)
980 goto out_timerfd;
981
982 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
983 on_each_cpu(retrigger_next_event, NULL, 1);
984 goto out_timerfd;
985 }
986
987 /* Avoid interrupting CPUs if possible */
988 cpus_read_lock();
989 for_each_online_cpu(cpu) {
990 unsigned long flags;
991
992 cpu_base = &per_cpu(hrtimer_bases, cpu);
993 raw_spin_lock_irqsave(&cpu_base->lock, flags);
994
995 if (update_needs_ipi(cpu_base, bases))
996 cpumask_set_cpu(cpu, mask);
997
998 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
999 }
1000
1001 preempt_disable();
1002 smp_call_function_many(mask, retrigger_next_event, NULL, 1);
1003 preempt_enable();
1004 cpus_read_unlock();
1005 free_cpumask_var(mask);
1006
1007 out_timerfd:
1008 timerfd_clock_was_set();
1009 }
1010
clock_was_set_work(struct work_struct * work)1011 static void clock_was_set_work(struct work_struct *work)
1012 {
1013 clock_was_set(CLOCK_SET_WALL);
1014 }
1015
1016 static DECLARE_WORK(hrtimer_work, clock_was_set_work);
1017
1018 /*
1019 * Called from timekeeping code to reprogram the hrtimer interrupt device
1020 * on all cpus and to notify timerfd.
1021 */
clock_was_set_delayed(void)1022 void clock_was_set_delayed(void)
1023 {
1024 schedule_work(&hrtimer_work);
1025 }
1026
1027 /*
1028 * Called during resume either directly from via timekeeping_resume()
1029 * or in the case of s2idle from tick_unfreeze() to ensure that the
1030 * hrtimers are up to date.
1031 */
hrtimers_resume_local(void)1032 void hrtimers_resume_local(void)
1033 {
1034 lockdep_assert_irqs_disabled();
1035 /* Retrigger on the local CPU */
1036 retrigger_next_event(NULL);
1037 }
1038
1039 /*
1040 * Counterpart to lock_hrtimer_base above:
1041 */
1042 static inline
unlock_hrtimer_base(const struct hrtimer * timer,unsigned long * flags)1043 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
1044 __releases(&timer->base->cpu_base->lock)
1045 {
1046 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
1047 }
1048
1049 /**
1050 * hrtimer_forward - forward the timer expiry
1051 * @timer: hrtimer to forward
1052 * @now: forward past this time
1053 * @interval: the interval to forward
1054 *
1055 * Forward the timer expiry so it will expire in the future.
1056 * Returns the number of overruns.
1057 *
1058 * Can be safely called from the callback function of @timer. If
1059 * called from other contexts @timer must neither be enqueued nor
1060 * running the callback and the caller needs to take care of
1061 * serialization.
1062 *
1063 * Note: This only updates the timer expiry value and does not requeue
1064 * the timer.
1065 */
hrtimer_forward(struct hrtimer * timer,ktime_t now,ktime_t interval)1066 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
1067 {
1068 u64 orun = 1;
1069 ktime_t delta;
1070
1071 delta = ktime_sub(now, hrtimer_get_expires(timer));
1072
1073 if (delta < 0)
1074 return 0;
1075
1076 if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
1077 return 0;
1078
1079 if (interval < hrtimer_resolution)
1080 interval = hrtimer_resolution;
1081
1082 if (unlikely(delta >= interval)) {
1083 s64 incr = ktime_to_ns(interval);
1084
1085 orun = ktime_divns(delta, incr);
1086 hrtimer_add_expires_ns(timer, incr * orun);
1087 if (hrtimer_get_expires_tv64(timer) > now)
1088 return orun;
1089 /*
1090 * This (and the ktime_add() below) is the
1091 * correction for exact:
1092 */
1093 orun++;
1094 }
1095 hrtimer_add_expires(timer, interval);
1096
1097 return orun;
1098 }
1099 EXPORT_SYMBOL_GPL(hrtimer_forward);
1100
1101 /*
1102 * enqueue_hrtimer - internal function to (re)start a timer
1103 *
1104 * The timer is inserted in expiry order. Insertion into the
1105 * red black tree is O(log(n)). Must hold the base lock.
1106 *
1107 * Returns 1 when the new timer is the leftmost timer in the tree.
1108 */
enqueue_hrtimer(struct hrtimer * timer,struct hrtimer_clock_base * base,enum hrtimer_mode mode)1109 static int enqueue_hrtimer(struct hrtimer *timer,
1110 struct hrtimer_clock_base *base,
1111 enum hrtimer_mode mode)
1112 {
1113 debug_activate(timer, mode);
1114 WARN_ON_ONCE(!base->cpu_base->online);
1115
1116 base->cpu_base->active_bases |= 1 << base->index;
1117
1118 /* Pairs with the lockless read in hrtimer_is_queued() */
1119 WRITE_ONCE(timer->state, HRTIMER_STATE_ENQUEUED);
1120
1121 return timerqueue_add(&base->active, &timer->node);
1122 }
1123
1124 /*
1125 * __remove_hrtimer - internal function to remove a timer
1126 *
1127 * Caller must hold the base lock.
1128 *
1129 * High resolution timer mode reprograms the clock event device when the
1130 * timer is the one which expires next. The caller can disable this by setting
1131 * reprogram to zero. This is useful, when the context does a reprogramming
1132 * anyway (e.g. timer interrupt)
1133 */
__remove_hrtimer(struct hrtimer * timer,struct hrtimer_clock_base * base,u8 newstate,int reprogram)1134 static void __remove_hrtimer(struct hrtimer *timer,
1135 struct hrtimer_clock_base *base,
1136 u8 newstate, int reprogram)
1137 {
1138 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1139 u8 state = timer->state;
1140
1141 /* Pairs with the lockless read in hrtimer_is_queued() */
1142 WRITE_ONCE(timer->state, newstate);
1143 if (!(state & HRTIMER_STATE_ENQUEUED))
1144 return;
1145
1146 if (!timerqueue_del(&base->active, &timer->node))
1147 cpu_base->active_bases &= ~(1 << base->index);
1148
1149 /*
1150 * Note: If reprogram is false we do not update
1151 * cpu_base->next_timer. This happens when we remove the first
1152 * timer on a remote cpu. No harm as we never dereference
1153 * cpu_base->next_timer. So the worst thing what can happen is
1154 * an superfluous call to hrtimer_force_reprogram() on the
1155 * remote cpu later on if the same timer gets enqueued again.
1156 */
1157 if (reprogram && timer == cpu_base->next_timer)
1158 hrtimer_force_reprogram(cpu_base, 1);
1159 }
1160
1161 /*
1162 * remove hrtimer, called with base lock held
1163 */
1164 static inline int
remove_hrtimer(struct hrtimer * timer,struct hrtimer_clock_base * base,bool restart,bool keep_local)1165 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base,
1166 bool restart, bool keep_local)
1167 {
1168 u8 state = timer->state;
1169
1170 if (state & HRTIMER_STATE_ENQUEUED) {
1171 bool reprogram;
1172
1173 /*
1174 * Remove the timer and force reprogramming when high
1175 * resolution mode is active and the timer is on the current
1176 * CPU. If we remove a timer on another CPU, reprogramming is
1177 * skipped. The interrupt event on this CPU is fired and
1178 * reprogramming happens in the interrupt handler. This is a
1179 * rare case and less expensive than a smp call.
1180 */
1181 debug_deactivate(timer);
1182 reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
1183
1184 /*
1185 * If the timer is not restarted then reprogramming is
1186 * required if the timer is local. If it is local and about
1187 * to be restarted, avoid programming it twice (on removal
1188 * and a moment later when it's requeued).
1189 */
1190 if (!restart)
1191 state = HRTIMER_STATE_INACTIVE;
1192 else
1193 reprogram &= !keep_local;
1194
1195 __remove_hrtimer(timer, base, state, reprogram);
1196 return 1;
1197 }
1198 return 0;
1199 }
1200
hrtimer_update_lowres(struct hrtimer * timer,ktime_t tim,const enum hrtimer_mode mode)1201 static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
1202 const enum hrtimer_mode mode)
1203 {
1204 #ifdef CONFIG_TIME_LOW_RES
1205 /*
1206 * CONFIG_TIME_LOW_RES indicates that the system has no way to return
1207 * granular time values. For relative timers we add hrtimer_resolution
1208 * (i.e. one jiffie) to prevent short timeouts.
1209 */
1210 timer->is_rel = mode & HRTIMER_MODE_REL;
1211 if (timer->is_rel)
1212 tim = ktime_add_safe(tim, hrtimer_resolution);
1213 #endif
1214 return tim;
1215 }
1216
1217 static void
hrtimer_update_softirq_timer(struct hrtimer_cpu_base * cpu_base,bool reprogram)1218 hrtimer_update_softirq_timer(struct hrtimer_cpu_base *cpu_base, bool reprogram)
1219 {
1220 ktime_t expires;
1221
1222 /*
1223 * Find the next SOFT expiration.
1224 */
1225 expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
1226
1227 /*
1228 * reprogramming needs to be triggered, even if the next soft
1229 * hrtimer expires at the same time than the next hard
1230 * hrtimer. cpu_base->softirq_expires_next needs to be updated!
1231 */
1232 if (expires == KTIME_MAX)
1233 return;
1234
1235 /*
1236 * cpu_base->*next_timer is recomputed by __hrtimer_get_next_event()
1237 * cpu_base->*expires_next is only set by hrtimer_reprogram()
1238 */
1239 hrtimer_reprogram(cpu_base->softirq_next_timer, reprogram);
1240 }
1241
__hrtimer_start_range_ns(struct hrtimer * timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode,struct hrtimer_clock_base * base)1242 static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1243 u64 delta_ns, const enum hrtimer_mode mode,
1244 struct hrtimer_clock_base *base)
1245 {
1246 struct hrtimer_cpu_base *this_cpu_base = this_cpu_ptr(&hrtimer_bases);
1247 struct hrtimer_clock_base *new_base;
1248 bool force_local, first;
1249
1250 /*
1251 * If the timer is on the local cpu base and is the first expiring
1252 * timer then this might end up reprogramming the hardware twice
1253 * (on removal and on enqueue). To avoid that by prevent the
1254 * reprogram on removal, keep the timer local to the current CPU
1255 * and enforce reprogramming after it is queued no matter whether
1256 * it is the new first expiring timer again or not.
1257 */
1258 force_local = base->cpu_base == this_cpu_base;
1259 force_local &= base->cpu_base->next_timer == timer;
1260
1261 /*
1262 * Don't force local queuing if this enqueue happens on a unplugged
1263 * CPU after hrtimer_cpu_dying() has been invoked.
1264 */
1265 force_local &= this_cpu_base->online;
1266
1267 /*
1268 * Remove an active timer from the queue. In case it is not queued
1269 * on the current CPU, make sure that remove_hrtimer() updates the
1270 * remote data correctly.
1271 *
1272 * If it's on the current CPU and the first expiring timer, then
1273 * skip reprogramming, keep the timer local and enforce
1274 * reprogramming later if it was the first expiring timer. This
1275 * avoids programming the underlying clock event twice (once at
1276 * removal and once after enqueue).
1277 */
1278 remove_hrtimer(timer, base, true, force_local);
1279
1280 if (mode & HRTIMER_MODE_REL)
1281 tim = ktime_add_safe(tim, base->get_time());
1282
1283 tim = hrtimer_update_lowres(timer, tim, mode);
1284
1285 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
1286
1287 /* Switch the timer base, if necessary: */
1288 if (!force_local) {
1289 new_base = switch_hrtimer_base(timer, base,
1290 mode & HRTIMER_MODE_PINNED);
1291 } else {
1292 new_base = base;
1293 }
1294
1295 first = enqueue_hrtimer(timer, new_base, mode);
1296 if (!force_local) {
1297 /*
1298 * If the current CPU base is online, then the timer is
1299 * never queued on a remote CPU if it would be the first
1300 * expiring timer there.
1301 */
1302 if (hrtimer_base_is_online(this_cpu_base))
1303 return first;
1304
1305 /*
1306 * Timer was enqueued remote because the current base is
1307 * already offline. If the timer is the first to expire,
1308 * kick the remote CPU to reprogram the clock event.
1309 */
1310 if (first) {
1311 struct hrtimer_cpu_base *new_cpu_base = new_base->cpu_base;
1312
1313 smp_call_function_single_async(new_cpu_base->cpu, &new_cpu_base->csd);
1314 }
1315 return 0;
1316 }
1317
1318 /*
1319 * Timer was forced to stay on the current CPU to avoid
1320 * reprogramming on removal and enqueue. Force reprogram the
1321 * hardware by evaluating the new first expiring timer.
1322 */
1323 hrtimer_force_reprogram(new_base->cpu_base, 1);
1324 return 0;
1325 }
1326
1327 /**
1328 * hrtimer_start_range_ns - (re)start an hrtimer
1329 * @timer: the timer to be added
1330 * @tim: expiry time
1331 * @delta_ns: "slack" range for the timer
1332 * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
1333 * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
1334 * softirq based mode is considered for debug purpose only!
1335 */
hrtimer_start_range_ns(struct hrtimer * timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode)1336 void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1337 u64 delta_ns, const enum hrtimer_mode mode)
1338 {
1339 struct hrtimer_clock_base *base;
1340 unsigned long flags;
1341
1342 if (WARN_ON_ONCE(!timer->function))
1343 return;
1344 /*
1345 * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
1346 * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard
1347 * expiry mode because unmarked timers are moved to softirq expiry.
1348 */
1349 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
1350 WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft);
1351 else
1352 WARN_ON_ONCE(!(mode & HRTIMER_MODE_HARD) ^ !timer->is_hard);
1353
1354 base = lock_hrtimer_base(timer, &flags);
1355
1356 if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base))
1357 hrtimer_reprogram(timer, true);
1358
1359 unlock_hrtimer_base(timer, &flags);
1360 }
1361 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1362
1363 /**
1364 * hrtimer_try_to_cancel - try to deactivate a timer
1365 * @timer: hrtimer to stop
1366 *
1367 * Returns:
1368 *
1369 * * 0 when the timer was not active
1370 * * 1 when the timer was active
1371 * * -1 when the timer is currently executing the callback function and
1372 * cannot be stopped
1373 */
hrtimer_try_to_cancel(struct hrtimer * timer)1374 int hrtimer_try_to_cancel(struct hrtimer *timer)
1375 {
1376 struct hrtimer_clock_base *base;
1377 unsigned long flags;
1378 int ret = -1;
1379
1380 /*
1381 * Check lockless first. If the timer is not active (neither
1382 * enqueued nor running the callback, nothing to do here. The
1383 * base lock does not serialize against a concurrent enqueue,
1384 * so we can avoid taking it.
1385 */
1386 if (!hrtimer_active(timer))
1387 return 0;
1388
1389 base = lock_hrtimer_base(timer, &flags);
1390
1391 if (!hrtimer_callback_running(timer))
1392 ret = remove_hrtimer(timer, base, false, false);
1393
1394 unlock_hrtimer_base(timer, &flags);
1395
1396 return ret;
1397
1398 }
1399 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1400
1401 #ifdef CONFIG_PREEMPT_RT
hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base * base)1402 static void hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base)
1403 {
1404 spin_lock_init(&base->softirq_expiry_lock);
1405 }
1406
hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base * base)1407 static void hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base)
1408 {
1409 spin_lock(&base->softirq_expiry_lock);
1410 }
1411
hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base * base)1412 static void hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base)
1413 {
1414 spin_unlock(&base->softirq_expiry_lock);
1415 }
1416
1417 /*
1418 * The counterpart to hrtimer_cancel_wait_running().
1419 *
1420 * If there is a waiter for cpu_base->expiry_lock, then it was waiting for
1421 * the timer callback to finish. Drop expiry_lock and reacquire it. That
1422 * allows the waiter to acquire the lock and make progress.
1423 */
hrtimer_sync_wait_running(struct hrtimer_cpu_base * cpu_base,unsigned long flags)1424 static void hrtimer_sync_wait_running(struct hrtimer_cpu_base *cpu_base,
1425 unsigned long flags)
1426 {
1427 if (atomic_read(&cpu_base->timer_waiters)) {
1428 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1429 spin_unlock(&cpu_base->softirq_expiry_lock);
1430 spin_lock(&cpu_base->softirq_expiry_lock);
1431 raw_spin_lock_irq(&cpu_base->lock);
1432 }
1433 }
1434
1435 #ifdef CONFIG_SMP
is_migration_base(struct hrtimer_clock_base * base)1436 static __always_inline bool is_migration_base(struct hrtimer_clock_base *base)
1437 {
1438 return base == &migration_base;
1439 }
1440 #else
is_migration_base(struct hrtimer_clock_base * base)1441 static __always_inline bool is_migration_base(struct hrtimer_clock_base *base)
1442 {
1443 return false;
1444 }
1445 #endif
1446
1447 /*
1448 * This function is called on PREEMPT_RT kernels when the fast path
1449 * deletion of a timer failed because the timer callback function was
1450 * running.
1451 *
1452 * This prevents priority inversion: if the soft irq thread is preempted
1453 * in the middle of a timer callback, then calling del_timer_sync() can
1454 * lead to two issues:
1455 *
1456 * - If the caller is on a remote CPU then it has to spin wait for the timer
1457 * handler to complete. This can result in unbound priority inversion.
1458 *
1459 * - If the caller originates from the task which preempted the timer
1460 * handler on the same CPU, then spin waiting for the timer handler to
1461 * complete is never going to end.
1462 */
hrtimer_cancel_wait_running(const struct hrtimer * timer)1463 void hrtimer_cancel_wait_running(const struct hrtimer *timer)
1464 {
1465 /* Lockless read. Prevent the compiler from reloading it below */
1466 struct hrtimer_clock_base *base = READ_ONCE(timer->base);
1467
1468 /*
1469 * Just relax if the timer expires in hard interrupt context or if
1470 * it is currently on the migration base.
1471 */
1472 if (!timer->is_soft || is_migration_base(base)) {
1473 cpu_relax();
1474 return;
1475 }
1476
1477 /*
1478 * Mark the base as contended and grab the expiry lock, which is
1479 * held by the softirq across the timer callback. Drop the lock
1480 * immediately so the softirq can expire the next timer. In theory
1481 * the timer could already be running again, but that's more than
1482 * unlikely and just causes another wait loop.
1483 */
1484 atomic_inc(&base->cpu_base->timer_waiters);
1485 spin_lock_bh(&base->cpu_base->softirq_expiry_lock);
1486 atomic_dec(&base->cpu_base->timer_waiters);
1487 spin_unlock_bh(&base->cpu_base->softirq_expiry_lock);
1488 }
1489 #else
1490 static inline void
hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base * base)1491 hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) { }
1492 static inline void
hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base * base)1493 hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) { }
1494 static inline void
hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base * base)1495 hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) { }
hrtimer_sync_wait_running(struct hrtimer_cpu_base * base,unsigned long flags)1496 static inline void hrtimer_sync_wait_running(struct hrtimer_cpu_base *base,
1497 unsigned long flags) { }
1498 #endif
1499
1500 /**
1501 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1502 * @timer: the timer to be cancelled
1503 *
1504 * Returns:
1505 * 0 when the timer was not active
1506 * 1 when the timer was active
1507 */
hrtimer_cancel(struct hrtimer * timer)1508 int hrtimer_cancel(struct hrtimer *timer)
1509 {
1510 int ret;
1511
1512 do {
1513 ret = hrtimer_try_to_cancel(timer);
1514
1515 if (ret < 0)
1516 hrtimer_cancel_wait_running(timer);
1517 } while (ret < 0);
1518 return ret;
1519 }
1520 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1521
1522 /**
1523 * __hrtimer_get_remaining - get remaining time for the timer
1524 * @timer: the timer to read
1525 * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y
1526 */
__hrtimer_get_remaining(const struct hrtimer * timer,bool adjust)1527 ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
1528 {
1529 unsigned long flags;
1530 ktime_t rem;
1531
1532 lock_hrtimer_base(timer, &flags);
1533 if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1534 rem = hrtimer_expires_remaining_adjusted(timer);
1535 else
1536 rem = hrtimer_expires_remaining(timer);
1537 unlock_hrtimer_base(timer, &flags);
1538
1539 return rem;
1540 }
1541 EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
1542
1543 #ifdef CONFIG_NO_HZ_COMMON
1544 /**
1545 * hrtimer_get_next_event - get the time until next expiry event
1546 *
1547 * Returns the next expiry time or KTIME_MAX if no timer is pending.
1548 */
hrtimer_get_next_event(void)1549 u64 hrtimer_get_next_event(void)
1550 {
1551 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1552 u64 expires = KTIME_MAX;
1553 unsigned long flags;
1554
1555 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1556
1557 if (!__hrtimer_hres_active(cpu_base))
1558 expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
1559
1560 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1561
1562 return expires;
1563 }
1564
1565 /**
1566 * hrtimer_next_event_without - time until next expiry event w/o one timer
1567 * @exclude: timer to exclude
1568 *
1569 * Returns the next expiry time over all timers except for the @exclude one or
1570 * KTIME_MAX if none of them is pending.
1571 */
hrtimer_next_event_without(const struct hrtimer * exclude)1572 u64 hrtimer_next_event_without(const struct hrtimer *exclude)
1573 {
1574 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1575 u64 expires = KTIME_MAX;
1576 unsigned long flags;
1577
1578 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1579
1580 if (__hrtimer_hres_active(cpu_base)) {
1581 unsigned int active;
1582
1583 if (!cpu_base->softirq_activated) {
1584 active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
1585 expires = __hrtimer_next_event_base(cpu_base, exclude,
1586 active, KTIME_MAX);
1587 }
1588 active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
1589 expires = __hrtimer_next_event_base(cpu_base, exclude, active,
1590 expires);
1591 }
1592
1593 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1594
1595 return expires;
1596 }
1597 #endif
1598
hrtimer_clockid_to_base(clockid_t clock_id)1599 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1600 {
1601 if (likely(clock_id < MAX_CLOCKS)) {
1602 int base = hrtimer_clock_to_base_table[clock_id];
1603
1604 if (likely(base != HRTIMER_MAX_CLOCK_BASES))
1605 return base;
1606 }
1607 WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1608 return HRTIMER_BASE_MONOTONIC;
1609 }
1610
__hrtimer_init(struct hrtimer * timer,clockid_t clock_id,enum hrtimer_mode mode)1611 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1612 enum hrtimer_mode mode)
1613 {
1614 bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
1615 struct hrtimer_cpu_base *cpu_base;
1616 int base;
1617
1618 /*
1619 * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
1620 * marked for hard interrupt expiry mode are moved into soft
1621 * interrupt context for latency reasons and because the callbacks
1622 * can invoke functions which might sleep on RT, e.g. spin_lock().
1623 */
1624 if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD))
1625 softtimer = true;
1626
1627 memset(timer, 0, sizeof(struct hrtimer));
1628
1629 cpu_base = raw_cpu_ptr(&hrtimer_bases);
1630
1631 /*
1632 * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
1633 * clock modifications, so they needs to become CLOCK_MONOTONIC to
1634 * ensure POSIX compliance.
1635 */
1636 if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
1637 clock_id = CLOCK_MONOTONIC;
1638
1639 base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
1640 base += hrtimer_clockid_to_base(clock_id);
1641 timer->is_soft = softtimer;
1642 timer->is_hard = !!(mode & HRTIMER_MODE_HARD);
1643 timer->base = &cpu_base->clock_base[base];
1644 timerqueue_init(&timer->node);
1645 }
1646
1647 /**
1648 * hrtimer_init - initialize a timer to the given clock
1649 * @timer: the timer to be initialized
1650 * @clock_id: the clock to be used
1651 * @mode: The modes which are relevant for initialization:
1652 * HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
1653 * HRTIMER_MODE_REL_SOFT
1654 *
1655 * The PINNED variants of the above can be handed in,
1656 * but the PINNED bit is ignored as pinning happens
1657 * when the hrtimer is started
1658 */
hrtimer_init(struct hrtimer * timer,clockid_t clock_id,enum hrtimer_mode mode)1659 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1660 enum hrtimer_mode mode)
1661 {
1662 debug_init(timer, clock_id, mode);
1663 __hrtimer_init(timer, clock_id, mode);
1664 }
1665 EXPORT_SYMBOL_GPL(hrtimer_init);
1666
1667 /*
1668 * A timer is active, when it is enqueued into the rbtree or the
1669 * callback function is running or it's in the state of being migrated
1670 * to another cpu.
1671 *
1672 * It is important for this function to not return a false negative.
1673 */
hrtimer_active(const struct hrtimer * timer)1674 bool hrtimer_active(const struct hrtimer *timer)
1675 {
1676 struct hrtimer_clock_base *base;
1677 unsigned int seq;
1678
1679 do {
1680 base = READ_ONCE(timer->base);
1681 seq = raw_read_seqcount_begin(&base->seq);
1682
1683 if (timer->state != HRTIMER_STATE_INACTIVE ||
1684 base->running == timer)
1685 return true;
1686
1687 } while (read_seqcount_retry(&base->seq, seq) ||
1688 base != READ_ONCE(timer->base));
1689
1690 return false;
1691 }
1692 EXPORT_SYMBOL_GPL(hrtimer_active);
1693
1694 /*
1695 * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1696 * distinct sections:
1697 *
1698 * - queued: the timer is queued
1699 * - callback: the timer is being ran
1700 * - post: the timer is inactive or (re)queued
1701 *
1702 * On the read side we ensure we observe timer->state and cpu_base->running
1703 * from the same section, if anything changed while we looked at it, we retry.
1704 * This includes timer->base changing because sequence numbers alone are
1705 * insufficient for that.
1706 *
1707 * The sequence numbers are required because otherwise we could still observe
1708 * a false negative if the read side got smeared over multiple consecutive
1709 * __run_hrtimer() invocations.
1710 */
1711
__run_hrtimer(struct hrtimer_cpu_base * cpu_base,struct hrtimer_clock_base * base,struct hrtimer * timer,ktime_t * now,unsigned long flags)1712 static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
1713 struct hrtimer_clock_base *base,
1714 struct hrtimer *timer, ktime_t *now,
1715 unsigned long flags) __must_hold(&cpu_base->lock)
1716 {
1717 enum hrtimer_restart (*fn)(struct hrtimer *);
1718 bool expires_in_hardirq;
1719 int restart;
1720
1721 lockdep_assert_held(&cpu_base->lock);
1722
1723 debug_deactivate(timer);
1724 base->running = timer;
1725
1726 /*
1727 * Separate the ->running assignment from the ->state assignment.
1728 *
1729 * As with a regular write barrier, this ensures the read side in
1730 * hrtimer_active() cannot observe base->running == NULL &&
1731 * timer->state == INACTIVE.
1732 */
1733 raw_write_seqcount_barrier(&base->seq);
1734
1735 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
1736 fn = timer->function;
1737
1738 /*
1739 * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
1740 * timer is restarted with a period then it becomes an absolute
1741 * timer. If its not restarted it does not matter.
1742 */
1743 if (IS_ENABLED(CONFIG_TIME_LOW_RES))
1744 timer->is_rel = false;
1745
1746 /*
1747 * The timer is marked as running in the CPU base, so it is
1748 * protected against migration to a different CPU even if the lock
1749 * is dropped.
1750 */
1751 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1752 trace_hrtimer_expire_entry(timer, now);
1753 expires_in_hardirq = lockdep_hrtimer_enter(timer);
1754
1755 restart = fn(timer);
1756
1757 lockdep_hrtimer_exit(expires_in_hardirq);
1758 trace_hrtimer_expire_exit(timer);
1759 raw_spin_lock_irq(&cpu_base->lock);
1760
1761 /*
1762 * Note: We clear the running state after enqueue_hrtimer and
1763 * we do not reprogram the event hardware. Happens either in
1764 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1765 *
1766 * Note: Because we dropped the cpu_base->lock above,
1767 * hrtimer_start_range_ns() can have popped in and enqueued the timer
1768 * for us already.
1769 */
1770 if (restart != HRTIMER_NORESTART &&
1771 !(timer->state & HRTIMER_STATE_ENQUEUED))
1772 enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS);
1773
1774 /*
1775 * Separate the ->running assignment from the ->state assignment.
1776 *
1777 * As with a regular write barrier, this ensures the read side in
1778 * hrtimer_active() cannot observe base->running.timer == NULL &&
1779 * timer->state == INACTIVE.
1780 */
1781 raw_write_seqcount_barrier(&base->seq);
1782
1783 WARN_ON_ONCE(base->running != timer);
1784 base->running = NULL;
1785 }
1786
__hrtimer_run_queues(struct hrtimer_cpu_base * cpu_base,ktime_t now,unsigned long flags,unsigned int active_mask)1787 static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
1788 unsigned long flags, unsigned int active_mask)
1789 {
1790 struct hrtimer_clock_base *base;
1791 unsigned int active = cpu_base->active_bases & active_mask;
1792
1793 for_each_active_base(base, cpu_base, active) {
1794 struct timerqueue_node *node;
1795 ktime_t basenow;
1796
1797 basenow = ktime_add(now, base->offset);
1798
1799 while ((node = timerqueue_getnext(&base->active))) {
1800 struct hrtimer *timer;
1801
1802 timer = container_of(node, struct hrtimer, node);
1803
1804 /*
1805 * The immediate goal for using the softexpires is
1806 * minimizing wakeups, not running timers at the
1807 * earliest interrupt after their soft expiration.
1808 * This allows us to avoid using a Priority Search
1809 * Tree, which can answer a stabbing query for
1810 * overlapping intervals and instead use the simple
1811 * BST we already have.
1812 * We don't add extra wakeups by delaying timers that
1813 * are right-of a not yet expired timer, because that
1814 * timer will have to trigger a wakeup anyway.
1815 */
1816 if (basenow < hrtimer_get_softexpires_tv64(timer))
1817 break;
1818
1819 __run_hrtimer(cpu_base, base, timer, &basenow, flags);
1820 if (active_mask == HRTIMER_ACTIVE_SOFT)
1821 hrtimer_sync_wait_running(cpu_base, flags);
1822 }
1823 }
1824 }
1825
hrtimer_run_softirq(struct softirq_action * h)1826 static __latent_entropy void hrtimer_run_softirq(struct softirq_action *h)
1827 {
1828 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1829 unsigned long flags;
1830 ktime_t now;
1831
1832 hrtimer_cpu_base_lock_expiry(cpu_base);
1833 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1834
1835 now = hrtimer_update_base(cpu_base);
1836 __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_SOFT);
1837
1838 cpu_base->softirq_activated = 0;
1839 hrtimer_update_softirq_timer(cpu_base, true);
1840
1841 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1842 hrtimer_cpu_base_unlock_expiry(cpu_base);
1843 }
1844
1845 #ifdef CONFIG_HIGH_RES_TIMERS
1846
1847 /*
1848 * High resolution timer interrupt
1849 * Called with interrupts disabled
1850 */
hrtimer_interrupt(struct clock_event_device * dev)1851 void hrtimer_interrupt(struct clock_event_device *dev)
1852 {
1853 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1854 ktime_t expires_next, now, entry_time, delta;
1855 unsigned long flags;
1856 int retries = 0;
1857
1858 BUG_ON(!cpu_base->hres_active);
1859 cpu_base->nr_events++;
1860 dev->next_event = KTIME_MAX;
1861
1862 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1863 entry_time = now = hrtimer_update_base(cpu_base);
1864 retry:
1865 cpu_base->in_hrtirq = 1;
1866 /*
1867 * We set expires_next to KTIME_MAX here with cpu_base->lock
1868 * held to prevent that a timer is enqueued in our queue via
1869 * the migration code. This does not affect enqueueing of
1870 * timers which run their callback and need to be requeued on
1871 * this CPU.
1872 */
1873 cpu_base->expires_next = KTIME_MAX;
1874
1875 if (!ktime_before(now, cpu_base->softirq_expires_next)) {
1876 cpu_base->softirq_expires_next = KTIME_MAX;
1877 cpu_base->softirq_activated = 1;
1878 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1879 }
1880
1881 __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
1882
1883 /* Reevaluate the clock bases for the [soft] next expiry */
1884 expires_next = hrtimer_update_next_event(cpu_base);
1885 /*
1886 * Store the new expiry value so the migration code can verify
1887 * against it.
1888 */
1889 cpu_base->expires_next = expires_next;
1890 cpu_base->in_hrtirq = 0;
1891 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1892
1893 /* Reprogramming necessary ? */
1894 if (!tick_program_event(expires_next, 0)) {
1895 cpu_base->hang_detected = 0;
1896 return;
1897 }
1898
1899 /*
1900 * The next timer was already expired due to:
1901 * - tracing
1902 * - long lasting callbacks
1903 * - being scheduled away when running in a VM
1904 *
1905 * We need to prevent that we loop forever in the hrtimer
1906 * interrupt routine. We give it 3 attempts to avoid
1907 * overreacting on some spurious event.
1908 *
1909 * Acquire base lock for updating the offsets and retrieving
1910 * the current time.
1911 */
1912 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1913 now = hrtimer_update_base(cpu_base);
1914 cpu_base->nr_retries++;
1915 if (++retries < 3)
1916 goto retry;
1917 /*
1918 * Give the system a chance to do something else than looping
1919 * here. We stored the entry time, so we know exactly how long
1920 * we spent here. We schedule the next event this amount of
1921 * time away.
1922 */
1923 cpu_base->nr_hangs++;
1924 cpu_base->hang_detected = 1;
1925 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1926
1927 delta = ktime_sub(now, entry_time);
1928 if ((unsigned int)delta > cpu_base->max_hang_time)
1929 cpu_base->max_hang_time = (unsigned int) delta;
1930 /*
1931 * Limit it to a sensible value as we enforce a longer
1932 * delay. Give the CPU at least 100ms to catch up.
1933 */
1934 if (delta > 100 * NSEC_PER_MSEC)
1935 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1936 else
1937 expires_next = ktime_add(now, delta);
1938 tick_program_event(expires_next, 1);
1939 pr_warn_once("hrtimer: interrupt took %llu ns\n", ktime_to_ns(delta));
1940 }
1941
1942 /* called with interrupts disabled */
__hrtimer_peek_ahead_timers(void)1943 static inline void __hrtimer_peek_ahead_timers(void)
1944 {
1945 struct tick_device *td;
1946
1947 if (!hrtimer_hres_active())
1948 return;
1949
1950 td = this_cpu_ptr(&tick_cpu_device);
1951 if (td && td->evtdev)
1952 hrtimer_interrupt(td->evtdev);
1953 }
1954
1955 #else /* CONFIG_HIGH_RES_TIMERS */
1956
__hrtimer_peek_ahead_timers(void)1957 static inline void __hrtimer_peek_ahead_timers(void) { }
1958
1959 #endif /* !CONFIG_HIGH_RES_TIMERS */
1960
1961 /*
1962 * Called from run_local_timers in hardirq context every jiffy
1963 */
hrtimer_run_queues(void)1964 void hrtimer_run_queues(void)
1965 {
1966 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1967 unsigned long flags;
1968 ktime_t now;
1969
1970 if (__hrtimer_hres_active(cpu_base))
1971 return;
1972
1973 /*
1974 * This _is_ ugly: We have to check periodically, whether we
1975 * can switch to highres and / or nohz mode. The clocksource
1976 * switch happens with xtime_lock held. Notification from
1977 * there only sets the check bit in the tick_oneshot code,
1978 * otherwise we might deadlock vs. xtime_lock.
1979 */
1980 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
1981 hrtimer_switch_to_hres();
1982 return;
1983 }
1984
1985 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1986 now = hrtimer_update_base(cpu_base);
1987
1988 if (!ktime_before(now, cpu_base->softirq_expires_next)) {
1989 cpu_base->softirq_expires_next = KTIME_MAX;
1990 cpu_base->softirq_activated = 1;
1991 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1992 }
1993
1994 __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
1995 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1996 }
1997
1998 /*
1999 * Sleep related functions:
2000 */
hrtimer_wakeup(struct hrtimer * timer)2001 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
2002 {
2003 struct hrtimer_sleeper *t =
2004 container_of(timer, struct hrtimer_sleeper, timer);
2005 struct task_struct *task = t->task;
2006
2007 t->task = NULL;
2008 if (task)
2009 wake_up_process(task);
2010
2011 return HRTIMER_NORESTART;
2012 }
2013
2014 /**
2015 * hrtimer_sleeper_start_expires - Start a hrtimer sleeper timer
2016 * @sl: sleeper to be started
2017 * @mode: timer mode abs/rel
2018 *
2019 * Wrapper around hrtimer_start_expires() for hrtimer_sleeper based timers
2020 * to allow PREEMPT_RT to tweak the delivery mode (soft/hardirq context)
2021 */
hrtimer_sleeper_start_expires(struct hrtimer_sleeper * sl,enum hrtimer_mode mode)2022 void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
2023 enum hrtimer_mode mode)
2024 {
2025 /*
2026 * Make the enqueue delivery mode check work on RT. If the sleeper
2027 * was initialized for hard interrupt delivery, force the mode bit.
2028 * This is a special case for hrtimer_sleepers because
2029 * hrtimer_init_sleeper() determines the delivery mode on RT so the
2030 * fiddling with this decision is avoided at the call sites.
2031 */
2032 if (IS_ENABLED(CONFIG_PREEMPT_RT) && sl->timer.is_hard)
2033 mode |= HRTIMER_MODE_HARD;
2034
2035 hrtimer_start_expires(&sl->timer, mode);
2036 }
2037 EXPORT_SYMBOL_GPL(hrtimer_sleeper_start_expires);
2038
__hrtimer_init_sleeper(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)2039 static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
2040 clockid_t clock_id, enum hrtimer_mode mode)
2041 {
2042 /*
2043 * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
2044 * marked for hard interrupt expiry mode are moved into soft
2045 * interrupt context either for latency reasons or because the
2046 * hrtimer callback takes regular spinlocks or invokes other
2047 * functions which are not suitable for hard interrupt context on
2048 * PREEMPT_RT.
2049 *
2050 * The hrtimer_sleeper callback is RT compatible in hard interrupt
2051 * context, but there is a latency concern: Untrusted userspace can
2052 * spawn many threads which arm timers for the same expiry time on
2053 * the same CPU. That causes a latency spike due to the wakeup of
2054 * a gazillion threads.
2055 *
2056 * OTOH, privileged real-time user space applications rely on the
2057 * low latency of hard interrupt wakeups. If the current task is in
2058 * a real-time scheduling class, mark the mode for hard interrupt
2059 * expiry.
2060 */
2061 if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
2062 if (task_is_realtime(current) && !(mode & HRTIMER_MODE_SOFT))
2063 mode |= HRTIMER_MODE_HARD;
2064 }
2065
2066 __hrtimer_init(&sl->timer, clock_id, mode);
2067 sl->timer.function = hrtimer_wakeup;
2068 sl->task = current;
2069 }
2070
2071 /**
2072 * hrtimer_init_sleeper - initialize sleeper to the given clock
2073 * @sl: sleeper to be initialized
2074 * @clock_id: the clock to be used
2075 * @mode: timer mode abs/rel
2076 */
hrtimer_init_sleeper(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)2077 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
2078 enum hrtimer_mode mode)
2079 {
2080 debug_init(&sl->timer, clock_id, mode);
2081 __hrtimer_init_sleeper(sl, clock_id, mode);
2082
2083 }
2084 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
2085
nanosleep_copyout(struct restart_block * restart,struct timespec64 * ts)2086 int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
2087 {
2088 switch(restart->nanosleep.type) {
2089 #ifdef CONFIG_COMPAT_32BIT_TIME
2090 case TT_COMPAT:
2091 if (put_old_timespec32(ts, restart->nanosleep.compat_rmtp))
2092 return -EFAULT;
2093 break;
2094 #endif
2095 case TT_NATIVE:
2096 if (put_timespec64(ts, restart->nanosleep.rmtp))
2097 return -EFAULT;
2098 break;
2099 default:
2100 BUG();
2101 }
2102 return -ERESTART_RESTARTBLOCK;
2103 }
2104
do_nanosleep(struct hrtimer_sleeper * t,enum hrtimer_mode mode)2105 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
2106 {
2107 struct restart_block *restart;
2108
2109 do {
2110 set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
2111 hrtimer_sleeper_start_expires(t, mode);
2112
2113 if (likely(t->task))
2114 schedule();
2115
2116 hrtimer_cancel(&t->timer);
2117 mode = HRTIMER_MODE_ABS;
2118
2119 } while (t->task && !signal_pending(current));
2120
2121 __set_current_state(TASK_RUNNING);
2122
2123 if (!t->task)
2124 return 0;
2125
2126 restart = ¤t->restart_block;
2127 if (restart->nanosleep.type != TT_NONE) {
2128 ktime_t rem = hrtimer_expires_remaining(&t->timer);
2129 struct timespec64 rmt;
2130
2131 if (rem <= 0)
2132 return 0;
2133 rmt = ktime_to_timespec64(rem);
2134
2135 return nanosleep_copyout(restart, &rmt);
2136 }
2137 return -ERESTART_RESTARTBLOCK;
2138 }
2139
hrtimer_nanosleep_restart(struct restart_block * restart)2140 static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
2141 {
2142 struct hrtimer_sleeper t;
2143 int ret;
2144
2145 hrtimer_init_sleeper_on_stack(&t, restart->nanosleep.clockid,
2146 HRTIMER_MODE_ABS);
2147 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
2148 ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
2149 destroy_hrtimer_on_stack(&t.timer);
2150 return ret;
2151 }
2152
hrtimer_nanosleep(ktime_t rqtp,const enum hrtimer_mode mode,const clockid_t clockid)2153 long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
2154 const clockid_t clockid)
2155 {
2156 struct restart_block *restart;
2157 struct hrtimer_sleeper t;
2158 int ret = 0;
2159
2160 hrtimer_init_sleeper_on_stack(&t, clockid, mode);
2161 hrtimer_set_expires_range_ns(&t.timer, rqtp, current->timer_slack_ns);
2162 ret = do_nanosleep(&t, mode);
2163 if (ret != -ERESTART_RESTARTBLOCK)
2164 goto out;
2165
2166 /* Absolute timers do not update the rmtp value and restart: */
2167 if (mode == HRTIMER_MODE_ABS) {
2168 ret = -ERESTARTNOHAND;
2169 goto out;
2170 }
2171
2172 restart = ¤t->restart_block;
2173 restart->nanosleep.clockid = t.timer.base->clockid;
2174 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
2175 set_restart_fn(restart, hrtimer_nanosleep_restart);
2176 out:
2177 destroy_hrtimer_on_stack(&t.timer);
2178 return ret;
2179 }
2180
2181 #ifdef CONFIG_64BIT
2182
SYSCALL_DEFINE2(nanosleep,struct __kernel_timespec __user *,rqtp,struct __kernel_timespec __user *,rmtp)2183 SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp,
2184 struct __kernel_timespec __user *, rmtp)
2185 {
2186 struct timespec64 tu;
2187
2188 if (get_timespec64(&tu, rqtp))
2189 return -EFAULT;
2190
2191 if (!timespec64_valid(&tu))
2192 return -EINVAL;
2193
2194 current->restart_block.fn = do_no_restart_syscall;
2195 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
2196 current->restart_block.nanosleep.rmtp = rmtp;
2197 return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL,
2198 CLOCK_MONOTONIC);
2199 }
2200
2201 #endif
2202
2203 #ifdef CONFIG_COMPAT_32BIT_TIME
2204
SYSCALL_DEFINE2(nanosleep_time32,struct old_timespec32 __user *,rqtp,struct old_timespec32 __user *,rmtp)2205 SYSCALL_DEFINE2(nanosleep_time32, struct old_timespec32 __user *, rqtp,
2206 struct old_timespec32 __user *, rmtp)
2207 {
2208 struct timespec64 tu;
2209
2210 if (get_old_timespec32(&tu, rqtp))
2211 return -EFAULT;
2212
2213 if (!timespec64_valid(&tu))
2214 return -EINVAL;
2215
2216 current->restart_block.fn = do_no_restart_syscall;
2217 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
2218 current->restart_block.nanosleep.compat_rmtp = rmtp;
2219 return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL,
2220 CLOCK_MONOTONIC);
2221 }
2222 #endif
2223
2224 /*
2225 * Functions related to boot-time initialization:
2226 */
hrtimers_prepare_cpu(unsigned int cpu)2227 int hrtimers_prepare_cpu(unsigned int cpu)
2228 {
2229 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
2230 int i;
2231
2232 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
2233 struct hrtimer_clock_base *clock_b = &cpu_base->clock_base[i];
2234
2235 clock_b->cpu_base = cpu_base;
2236 seqcount_raw_spinlock_init(&clock_b->seq, &cpu_base->lock);
2237 timerqueue_init_head(&clock_b->active);
2238 }
2239
2240 cpu_base->cpu = cpu;
2241 hrtimer_cpu_base_init_expiry_lock(cpu_base);
2242 return 0;
2243 }
2244
hrtimers_cpu_starting(unsigned int cpu)2245 int hrtimers_cpu_starting(unsigned int cpu)
2246 {
2247 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
2248
2249 /* Clear out any left over state from a CPU down operation */
2250 cpu_base->active_bases = 0;
2251 cpu_base->hres_active = 0;
2252 cpu_base->hang_detected = 0;
2253 cpu_base->next_timer = NULL;
2254 cpu_base->softirq_next_timer = NULL;
2255 cpu_base->expires_next = KTIME_MAX;
2256 cpu_base->softirq_expires_next = KTIME_MAX;
2257 cpu_base->online = 1;
2258 return 0;
2259 }
2260
2261 #ifdef CONFIG_HOTPLUG_CPU
2262
migrate_hrtimer_list(struct hrtimer_clock_base * old_base,struct hrtimer_clock_base * new_base)2263 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
2264 struct hrtimer_clock_base *new_base)
2265 {
2266 struct hrtimer *timer;
2267 struct timerqueue_node *node;
2268
2269 while ((node = timerqueue_getnext(&old_base->active))) {
2270 timer = container_of(node, struct hrtimer, node);
2271 BUG_ON(hrtimer_callback_running(timer));
2272 debug_deactivate(timer);
2273
2274 /*
2275 * Mark it as ENQUEUED not INACTIVE otherwise the
2276 * timer could be seen as !active and just vanish away
2277 * under us on another CPU
2278 */
2279 __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
2280 timer->base = new_base;
2281 /*
2282 * Enqueue the timers on the new cpu. This does not
2283 * reprogram the event device in case the timer
2284 * expires before the earliest on this CPU, but we run
2285 * hrtimer_interrupt after we migrated everything to
2286 * sort out already expired timers and reprogram the
2287 * event device.
2288 */
2289 enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS);
2290 }
2291 }
2292
hrtimers_cpu_dying(unsigned int dying_cpu)2293 int hrtimers_cpu_dying(unsigned int dying_cpu)
2294 {
2295 int i, ncpu = cpumask_any_and(cpu_active_mask, housekeeping_cpumask(HK_TYPE_TIMER));
2296 struct hrtimer_cpu_base *old_base, *new_base;
2297
2298 tick_cancel_sched_timer(dying_cpu);
2299
2300 old_base = this_cpu_ptr(&hrtimer_bases);
2301 new_base = &per_cpu(hrtimer_bases, ncpu);
2302
2303 /*
2304 * The caller is globally serialized and nobody else
2305 * takes two locks at once, deadlock is not possible.
2306 */
2307 raw_spin_lock(&old_base->lock);
2308 raw_spin_lock_nested(&new_base->lock, SINGLE_DEPTH_NESTING);
2309
2310 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
2311 migrate_hrtimer_list(&old_base->clock_base[i],
2312 &new_base->clock_base[i]);
2313 }
2314
2315 /*
2316 * The migration might have changed the first expiring softirq
2317 * timer on this CPU. Update it.
2318 */
2319 __hrtimer_get_next_event(new_base, HRTIMER_ACTIVE_SOFT);
2320 /* Tell the other CPU to retrigger the next event */
2321 smp_call_function_single(ncpu, retrigger_next_event, NULL, 0);
2322
2323 raw_spin_unlock(&new_base->lock);
2324 old_base->online = 0;
2325 raw_spin_unlock(&old_base->lock);
2326
2327 return 0;
2328 }
2329
2330 #endif /* CONFIG_HOTPLUG_CPU */
2331
hrtimers_init(void)2332 void __init hrtimers_init(void)
2333 {
2334 hrtimers_prepare_cpu(smp_processor_id());
2335 hrtimers_cpu_starting(smp_processor_id());
2336 open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
2337 }
2338
2339 /**
2340 * schedule_hrtimeout_range_clock - sleep until timeout
2341 * @expires: timeout value (ktime_t)
2342 * @delta: slack in expires timeout (ktime_t)
2343 * @mode: timer mode
2344 * @clock_id: timer clock to be used
2345 */
2346 int __sched
schedule_hrtimeout_range_clock(ktime_t * expires,u64 delta,const enum hrtimer_mode mode,clockid_t clock_id)2347 schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
2348 const enum hrtimer_mode mode, clockid_t clock_id)
2349 {
2350 struct hrtimer_sleeper t;
2351
2352 /*
2353 * Optimize when a zero timeout value is given. It does not
2354 * matter whether this is an absolute or a relative time.
2355 */
2356 if (expires && *expires == 0) {
2357 __set_current_state(TASK_RUNNING);
2358 return 0;
2359 }
2360
2361 /*
2362 * A NULL parameter means "infinite"
2363 */
2364 if (!expires) {
2365 schedule();
2366 return -EINTR;
2367 }
2368
2369 hrtimer_init_sleeper_on_stack(&t, clock_id, mode);
2370 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
2371 hrtimer_sleeper_start_expires(&t, mode);
2372
2373 if (likely(t.task))
2374 schedule();
2375
2376 hrtimer_cancel(&t.timer);
2377 destroy_hrtimer_on_stack(&t.timer);
2378
2379 __set_current_state(TASK_RUNNING);
2380
2381 return !t.task ? 0 : -EINTR;
2382 }
2383 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range_clock);
2384
2385 /**
2386 * schedule_hrtimeout_range - sleep until timeout
2387 * @expires: timeout value (ktime_t)
2388 * @delta: slack in expires timeout (ktime_t)
2389 * @mode: timer mode
2390 *
2391 * Make the current task sleep until the given expiry time has
2392 * elapsed. The routine will return immediately unless
2393 * the current task state has been set (see set_current_state()).
2394 *
2395 * The @delta argument gives the kernel the freedom to schedule the
2396 * actual wakeup to a time that is both power and performance friendly
2397 * for regular (non RT/DL) tasks.
2398 * The kernel give the normal best effort behavior for "@expires+@delta",
2399 * but may decide to fire the timer earlier, but no earlier than @expires.
2400 *
2401 * You can set the task state as follows -
2402 *
2403 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
2404 * pass before the routine returns unless the current task is explicitly
2405 * woken up, (e.g. by wake_up_process()).
2406 *
2407 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
2408 * delivered to the current task or the current task is explicitly woken
2409 * up.
2410 *
2411 * The current task state is guaranteed to be TASK_RUNNING when this
2412 * routine returns.
2413 *
2414 * Returns 0 when the timer has expired. If the task was woken before the
2415 * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
2416 * by an explicit wakeup, it returns -EINTR.
2417 */
schedule_hrtimeout_range(ktime_t * expires,u64 delta,const enum hrtimer_mode mode)2418 int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
2419 const enum hrtimer_mode mode)
2420 {
2421 return schedule_hrtimeout_range_clock(expires, delta, mode,
2422 CLOCK_MONOTONIC);
2423 }
2424 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
2425
2426 /**
2427 * schedule_hrtimeout - sleep until timeout
2428 * @expires: timeout value (ktime_t)
2429 * @mode: timer mode
2430 *
2431 * Make the current task sleep until the given expiry time has
2432 * elapsed. The routine will return immediately unless
2433 * the current task state has been set (see set_current_state()).
2434 *
2435 * You can set the task state as follows -
2436 *
2437 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
2438 * pass before the routine returns unless the current task is explicitly
2439 * woken up, (e.g. by wake_up_process()).
2440 *
2441 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
2442 * delivered to the current task or the current task is explicitly woken
2443 * up.
2444 *
2445 * The current task state is guaranteed to be TASK_RUNNING when this
2446 * routine returns.
2447 *
2448 * Returns 0 when the timer has expired. If the task was woken before the
2449 * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
2450 * by an explicit wakeup, it returns -EINTR.
2451 */
schedule_hrtimeout(ktime_t * expires,const enum hrtimer_mode mode)2452 int __sched schedule_hrtimeout(ktime_t *expires,
2453 const enum hrtimer_mode mode)
2454 {
2455 return schedule_hrtimeout_range(expires, 0, mode);
2456 }
2457 EXPORT_SYMBOL_GPL(schedule_hrtimeout);
2458