]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - kernel/time/hrtimer.c
hrtimer: Fix hrtimer_start[_range_ns]() function descriptions
[mirror_ubuntu-eoan-kernel.git] / kernel / time / hrtimer.c
CommitLineData
c0a31329
TG
1/*
2 * linux/kernel/hrtimer.c
3 *
3c8aa39d 4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
79bf2bb3 5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
54cdfdb4 6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
c0a31329
TG
7 *
8 * High-resolution kernel timers
9 *
10 * In contrast to the low-resolution timeout API implemented in
11 * kernel/timer.c, hrtimers provide finer resolution and accuracy
12 * depending on system configuration and capabilities.
13 *
14 * These timers are currently used for:
15 * - itimers
16 * - POSIX timers
17 * - nanosleep
18 * - precise in-kernel timing
19 *
20 * Started by: Thomas Gleixner and Ingo Molnar
21 *
22 * Credits:
23 * based on kernel/timer.c
24 *
66188fae
TG
25 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
27 *
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
30 *
c0a31329
TG
31 * For licencing details see kernel-base/COPYING
32 */
33
34#include <linux/cpu.h>
9984de1a 35#include <linux/export.h>
c0a31329
TG
36#include <linux/percpu.h>
37#include <linux/hrtimer.h>
38#include <linux/notifier.h>
39#include <linux/syscalls.h>
54cdfdb4 40#include <linux/kallsyms.h>
c0a31329 41#include <linux/interrupt.h>
79bf2bb3 42#include <linux/tick.h>
54cdfdb4
TG
43#include <linux/seq_file.h>
44#include <linux/err.h>
237fc6e7 45#include <linux/debugobjects.h>
174cd4b1 46#include <linux/sched/signal.h>
cf4aebc2 47#include <linux/sched/sysctl.h>
8bd75c77 48#include <linux/sched/rt.h>
aab03e05 49#include <linux/sched/deadline.h>
370c9135 50#include <linux/sched/nohz.h>
b17b0153 51#include <linux/sched/debug.h>
eea08f32 52#include <linux/timer.h>
b0f8c44f 53#include <linux/freezer.h>
edbeda46 54#include <linux/compat.h>
c0a31329 55
7c0f6ba6 56#include <linux/uaccess.h>
c0a31329 57
c6a2a177
XG
58#include <trace/events/timer.h>
59
c1797baf 60#include "tick-internal.h"
8b094cd0 61
c0a31329
TG
62/*
63 * The timer bases:
7978672c 64 *
571af55a 65 * There are more clockids than hrtimer bases. Thus, we index
e06383db
JS
66 * into the timer bases by the hrtimer_base_type enum. When trying
67 * to reach a base using a clockid, hrtimer_clockid_to_base()
68 * is used to convert from clockid to the proper hrtimer_base_type.
c0a31329 69 */
54cdfdb4 70DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
c0a31329 71{
84cc8fd2 72 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
887d9dc9 73 .seq = SEQCNT_ZERO(hrtimer_bases.seq),
3c8aa39d 74 .clock_base =
c0a31329 75 {
3c8aa39d 76 {
ab8177bc
TG
77 .index = HRTIMER_BASE_MONOTONIC,
78 .clockid = CLOCK_MONOTONIC,
3c8aa39d 79 .get_time = &ktime_get,
3c8aa39d 80 },
68fa61c0
TG
81 {
82 .index = HRTIMER_BASE_REALTIME,
83 .clockid = CLOCK_REALTIME,
84 .get_time = &ktime_get_real,
68fa61c0 85 },
70a08cca 86 {
ab8177bc
TG
87 .index = HRTIMER_BASE_BOOTTIME,
88 .clockid = CLOCK_BOOTTIME,
70a08cca 89 .get_time = &ktime_get_boottime,
70a08cca 90 },
90adda98
JS
91 {
92 .index = HRTIMER_BASE_TAI,
93 .clockid = CLOCK_TAI,
94 .get_time = &ktime_get_clocktai,
90adda98 95 },
3c8aa39d 96 }
c0a31329
TG
97};
98
942c3c5c 99static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
336a9cde
MZ
100 /* Make sure we catch unsupported clockids */
101 [0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES,
102
ce31332d
TG
103 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
104 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
105 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
90adda98 106 [CLOCK_TAI] = HRTIMER_BASE_TAI,
ce31332d 107};
e06383db 108
c0a31329
TG
109/*
110 * Functions and macros which are different for UP/SMP systems are kept in a
111 * single place
112 */
113#ifdef CONFIG_SMP
114
887d9dc9
PZ
115/*
116 * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
117 * such that hrtimer_callback_running() can unconditionally dereference
118 * timer->base->cpu_base
119 */
120static struct hrtimer_cpu_base migration_cpu_base = {
121 .seq = SEQCNT_ZERO(migration_cpu_base),
122 .clock_base = { { .cpu_base = &migration_cpu_base, }, },
123};
124
125#define migration_base migration_cpu_base.clock_base[0]
126
c0a31329
TG
127/*
128 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
129 * means that all timers which are tied to this base via timer->base are
130 * locked, and the base itself is locked too.
131 *
132 * So __run_timers/migrate_timers can safely modify all timers which could
133 * be found on the lists/queues.
134 *
135 * When the timer's base is locked, and the timer removed from list, it is
887d9dc9
PZ
136 * possible to set timer->base = &migration_base and drop the lock: the timer
137 * remains locked.
c0a31329 138 */
3c8aa39d
TG
139static
140struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
141 unsigned long *flags)
c0a31329 142{
3c8aa39d 143 struct hrtimer_clock_base *base;
c0a31329
TG
144
145 for (;;) {
146 base = timer->base;
887d9dc9 147 if (likely(base != &migration_base)) {
ecb49d1a 148 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
c0a31329
TG
149 if (likely(base == timer->base))
150 return base;
151 /* The timer has migrated to another CPU: */
ecb49d1a 152 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
c0a31329
TG
153 }
154 cpu_relax();
155 }
156}
157
6ff7041d
TG
158/*
159 * With HIGHRES=y we do not migrate the timer when it is expiring
160 * before the next event on the target cpu because we cannot reprogram
161 * the target cpu hardware and we would cause it to fire late.
162 *
163 * Called with cpu_base->lock of target cpu held.
164 */
165static int
166hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
167{
168#ifdef CONFIG_HIGH_RES_TIMERS
169 ktime_t expires;
170
171 if (!new_base->cpu_base->hres_active)
172 return 0;
173
174 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
2456e855 175 return expires <= new_base->cpu_base->expires_next;
6ff7041d
TG
176#else
177 return 0;
178#endif
179}
180
bc7a34b8
TG
181static inline
182struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
183 int pinned)
184{
ae67bada
TG
185#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
186 if (static_branch_likely(&timers_migration_enabled) && !pinned)
187 return &per_cpu(hrtimer_bases, get_nohz_timer_target());
188#endif
662b3e19 189 return base;
bc7a34b8 190}
bc7a34b8 191
c0a31329 192/*
b48362d8
FW
193 * We switch the timer base to a power-optimized selected CPU target,
194 * if:
195 * - NO_HZ_COMMON is enabled
196 * - timer migration is enabled
197 * - the timer callback is not running
198 * - the timer is not the first expiring timer on the new target
199 *
200 * If one of the above requirements is not fulfilled we move the timer
201 * to the current CPU or leave it on the previously assigned CPU if
202 * the timer callback is currently running.
c0a31329 203 */
3c8aa39d 204static inline struct hrtimer_clock_base *
597d0275
AB
205switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
206 int pinned)
c0a31329 207{
b48362d8 208 struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
3c8aa39d 209 struct hrtimer_clock_base *new_base;
ab8177bc 210 int basenum = base->index;
c0a31329 211
b48362d8
FW
212 this_cpu_base = this_cpu_ptr(&hrtimer_bases);
213 new_cpu_base = get_target_base(this_cpu_base, pinned);
eea08f32 214again:
e06383db 215 new_base = &new_cpu_base->clock_base[basenum];
c0a31329
TG
216
217 if (base != new_base) {
218 /*
6ff7041d 219 * We are trying to move timer to new_base.
c0a31329
TG
220 * However we can't change timer's base while it is running,
221 * so we keep it on the same CPU. No hassle vs. reprogramming
222 * the event source in the high resolution case. The softirq
223 * code will take care of this when the timer function has
224 * completed. There is no conflict as we hold the lock until
225 * the timer is enqueued.
226 */
54cdfdb4 227 if (unlikely(hrtimer_callback_running(timer)))
c0a31329
TG
228 return base;
229
887d9dc9
PZ
230 /* See the comment in lock_hrtimer_base() */
231 timer->base = &migration_base;
ecb49d1a
TG
232 raw_spin_unlock(&base->cpu_base->lock);
233 raw_spin_lock(&new_base->cpu_base->lock);
eea08f32 234
b48362d8 235 if (new_cpu_base != this_cpu_base &&
bc7a34b8 236 hrtimer_check_target(timer, new_base)) {
ecb49d1a
TG
237 raw_spin_unlock(&new_base->cpu_base->lock);
238 raw_spin_lock(&base->cpu_base->lock);
b48362d8 239 new_cpu_base = this_cpu_base;
6ff7041d
TG
240 timer->base = base;
241 goto again;
eea08f32 242 }
c0a31329 243 timer->base = new_base;
012a45e3 244 } else {
b48362d8 245 if (new_cpu_base != this_cpu_base &&
bc7a34b8 246 hrtimer_check_target(timer, new_base)) {
b48362d8 247 new_cpu_base = this_cpu_base;
012a45e3
LM
248 goto again;
249 }
c0a31329
TG
250 }
251 return new_base;
252}
253
254#else /* CONFIG_SMP */
255
3c8aa39d 256static inline struct hrtimer_clock_base *
c0a31329
TG
257lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
258{
3c8aa39d 259 struct hrtimer_clock_base *base = timer->base;
c0a31329 260
ecb49d1a 261 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
c0a31329
TG
262
263 return base;
264}
265
eea08f32 266# define switch_hrtimer_base(t, b, p) (b)
c0a31329
TG
267
268#endif /* !CONFIG_SMP */
269
270/*
271 * Functions for the union type storage format of ktime_t which are
272 * too large for inlining:
273 */
274#if BITS_PER_LONG < 64
c0a31329
TG
275/*
276 * Divide a ktime value by a nanosecond value
277 */
f7bcb70e 278s64 __ktime_divns(const ktime_t kt, s64 div)
c0a31329 279{
c0a31329 280 int sft = 0;
f7bcb70e
JS
281 s64 dclc;
282 u64 tmp;
c0a31329 283
900cfa46 284 dclc = ktime_to_ns(kt);
f7bcb70e
JS
285 tmp = dclc < 0 ? -dclc : dclc;
286
c0a31329
TG
287 /* Make sure the divisor is less than 2^32: */
288 while (div >> 32) {
289 sft++;
290 div >>= 1;
291 }
f7bcb70e
JS
292 tmp >>= sft;
293 do_div(tmp, (unsigned long) div);
294 return dclc < 0 ? -tmp : tmp;
c0a31329 295}
8b618628 296EXPORT_SYMBOL_GPL(__ktime_divns);
c0a31329
TG
297#endif /* BITS_PER_LONG >= 64 */
298
5a7780e7
TG
299/*
300 * Add two ktime values and do a safety check for overflow:
301 */
302ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
303{
979515c5 304 ktime_t res = ktime_add_unsafe(lhs, rhs);
5a7780e7
TG
305
306 /*
307 * We use KTIME_SEC_MAX here, the maximum timeout which we can
308 * return to user space in a timespec:
309 */
2456e855 310 if (res < 0 || res < lhs || res < rhs)
5a7780e7
TG
311 res = ktime_set(KTIME_SEC_MAX, 0);
312
313 return res;
314}
315
8daa21e6
AB
316EXPORT_SYMBOL_GPL(ktime_add_safe);
317
237fc6e7
TG
318#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
319
320static struct debug_obj_descr hrtimer_debug_descr;
321
99777288
SG
322static void *hrtimer_debug_hint(void *addr)
323{
324 return ((struct hrtimer *) addr)->function;
325}
326
237fc6e7
TG
327/*
328 * fixup_init is called when:
329 * - an active object is initialized
330 */
e3252464 331static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
237fc6e7
TG
332{
333 struct hrtimer *timer = addr;
334
335 switch (state) {
336 case ODEBUG_STATE_ACTIVE:
337 hrtimer_cancel(timer);
338 debug_object_init(timer, &hrtimer_debug_descr);
e3252464 339 return true;
237fc6e7 340 default:
e3252464 341 return false;
237fc6e7
TG
342 }
343}
344
345/*
346 * fixup_activate is called when:
347 * - an active object is activated
b9fdac7f 348 * - an unknown non-static object is activated
237fc6e7 349 */
e3252464 350static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
237fc6e7
TG
351{
352 switch (state) {
237fc6e7
TG
353 case ODEBUG_STATE_ACTIVE:
354 WARN_ON(1);
355
356 default:
e3252464 357 return false;
237fc6e7
TG
358 }
359}
360
361/*
362 * fixup_free is called when:
363 * - an active object is freed
364 */
e3252464 365static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
237fc6e7
TG
366{
367 struct hrtimer *timer = addr;
368
369 switch (state) {
370 case ODEBUG_STATE_ACTIVE:
371 hrtimer_cancel(timer);
372 debug_object_free(timer, &hrtimer_debug_descr);
e3252464 373 return true;
237fc6e7 374 default:
e3252464 375 return false;
237fc6e7
TG
376 }
377}
378
379static struct debug_obj_descr hrtimer_debug_descr = {
380 .name = "hrtimer",
99777288 381 .debug_hint = hrtimer_debug_hint,
237fc6e7
TG
382 .fixup_init = hrtimer_fixup_init,
383 .fixup_activate = hrtimer_fixup_activate,
384 .fixup_free = hrtimer_fixup_free,
385};
386
387static inline void debug_hrtimer_init(struct hrtimer *timer)
388{
389 debug_object_init(timer, &hrtimer_debug_descr);
390}
391
392static inline void debug_hrtimer_activate(struct hrtimer *timer)
393{
394 debug_object_activate(timer, &hrtimer_debug_descr);
395}
396
397static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
398{
399 debug_object_deactivate(timer, &hrtimer_debug_descr);
400}
401
402static inline void debug_hrtimer_free(struct hrtimer *timer)
403{
404 debug_object_free(timer, &hrtimer_debug_descr);
405}
406
407static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
408 enum hrtimer_mode mode);
409
410void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
411 enum hrtimer_mode mode)
412{
413 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
414 __hrtimer_init(timer, clock_id, mode);
415}
2bc481cf 416EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
237fc6e7
TG
417
418void destroy_hrtimer_on_stack(struct hrtimer *timer)
419{
420 debug_object_free(timer, &hrtimer_debug_descr);
421}
c08376ac 422EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
237fc6e7
TG
423
424#else
425static inline void debug_hrtimer_init(struct hrtimer *timer) { }
426static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
427static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
428#endif
429
c6a2a177
XG
430static inline void
431debug_init(struct hrtimer *timer, clockid_t clockid,
432 enum hrtimer_mode mode)
433{
434 debug_hrtimer_init(timer);
435 trace_hrtimer_init(timer, clockid, mode);
436}
437
438static inline void debug_activate(struct hrtimer *timer)
439{
440 debug_hrtimer_activate(timer);
441 trace_hrtimer_start(timer);
442}
443
444static inline void debug_deactivate(struct hrtimer *timer)
445{
446 debug_hrtimer_deactivate(timer);
447 trace_hrtimer_cancel(timer);
448}
449
9bc74919 450#if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
895bdfa7
TG
451static inline void hrtimer_update_next_timer(struct hrtimer_cpu_base *cpu_base,
452 struct hrtimer *timer)
453{
454#ifdef CONFIG_HIGH_RES_TIMERS
455 cpu_base->next_timer = timer;
456#endif
457}
458
4ebbda52 459static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
9bc74919
TG
460{
461 struct hrtimer_clock_base *base = cpu_base->clock_base;
34aee88a 462 unsigned int active = cpu_base->active_bases;
2456e855 463 ktime_t expires, expires_next = KTIME_MAX;
9bc74919 464
895bdfa7 465 hrtimer_update_next_timer(cpu_base, NULL);
34aee88a 466 for (; active; base++, active >>= 1) {
9bc74919
TG
467 struct timerqueue_node *next;
468 struct hrtimer *timer;
469
34aee88a 470 if (!(active & 0x01))
9bc74919
TG
471 continue;
472
34aee88a 473 next = timerqueue_getnext(&base->active);
9bc74919
TG
474 timer = container_of(next, struct hrtimer, node);
475 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
2456e855 476 if (expires < expires_next) {
9bc74919 477 expires_next = expires;
895bdfa7
TG
478 hrtimer_update_next_timer(cpu_base, timer);
479 }
9bc74919
TG
480 }
481 /*
482 * clock_was_set() might have changed base->offset of any of
483 * the clock bases so the result might be negative. Fix it up
484 * to prevent a false positive in clockevents_program_event().
485 */
2456e855
TG
486 if (expires_next < 0)
487 expires_next = 0;
9bc74919
TG
488 return expires_next;
489}
490#endif
491
21d6d52a
TG
492static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
493{
494 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
495 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
496 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
497
868a3e91
TG
498 return ktime_get_update_offsets_now(&base->clock_was_set_seq,
499 offs_real, offs_boot, offs_tai);
21d6d52a
TG
500}
501
54cdfdb4
TG
502/* High resolution timer related functions */
503#ifdef CONFIG_HIGH_RES_TIMERS
504
505/*
506 * High resolution timer enabled ?
507 */
4cc7ecb7 508static bool hrtimer_hres_enabled __read_mostly = true;
398ca17f
TG
509unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
510EXPORT_SYMBOL_GPL(hrtimer_resolution);
54cdfdb4
TG
511
512/*
513 * Enable / Disable high resolution mode
514 */
515static int __init setup_hrtimer_hres(char *str)
516{
4cc7ecb7 517 return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
54cdfdb4
TG
518}
519
520__setup("highres=", setup_hrtimer_hres);
521
522/*
523 * hrtimer_high_res_enabled - query, if the highres mode is enabled
524 */
525static inline int hrtimer_is_hres_enabled(void)
526{
527 return hrtimer_hres_enabled;
528}
529
530/*
531 * Is the high resolution mode active ?
532 */
e19ffe8b
TG
533static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
534{
535 return cpu_base->hres_active;
536}
537
54cdfdb4
TG
538static inline int hrtimer_hres_active(void)
539{
e19ffe8b 540 return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
54cdfdb4
TG
541}
542
543/*
544 * Reprogram the event source with checking both queues for the
545 * next event
546 * Called with interrupts disabled and base->lock held
547 */
7403f41f
AC
548static void
549hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
54cdfdb4 550{
21d6d52a
TG
551 ktime_t expires_next;
552
553 if (!cpu_base->hres_active)
554 return;
555
556 expires_next = __hrtimer_get_next_event(cpu_base);
54cdfdb4 557
2456e855 558 if (skip_equal && expires_next == cpu_base->expires_next)
7403f41f
AC
559 return;
560
2456e855 561 cpu_base->expires_next = expires_next;
7403f41f 562
6c6c0d5a
SH
563 /*
564 * If a hang was detected in the last timer interrupt then we
565 * leave the hang delay active in the hardware. We want the
566 * system to make progress. That also prevents the following
567 * scenario:
568 * T1 expires 50ms from now
569 * T2 expires 5s from now
570 *
571 * T1 is removed, so this code is called and would reprogram
572 * the hardware to 5s from now. Any hrtimer_start after that
573 * will not reprogram the hardware due to hang_detected being
574 * set. So we'd effectivly block all timers until the T2 event
575 * fires.
576 */
577 if (cpu_base->hang_detected)
578 return;
579
d2540875 580 tick_program_event(cpu_base->expires_next, 1);
54cdfdb4
TG
581}
582
583/*
54cdfdb4
TG
584 * When a timer is enqueued and expires earlier than the already enqueued
585 * timers, we have to check, whether it expires earlier than the timer for
586 * which the clock event device was armed.
587 *
588 * Called with interrupts disabled and base->cpu_base.lock held
589 */
c6eb3f70
TG
590static void hrtimer_reprogram(struct hrtimer *timer,
591 struct hrtimer_clock_base *base)
54cdfdb4 592{
dc5df73b 593 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
cc584b21 594 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
54cdfdb4 595
cc584b21 596 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
63070a79 597
54cdfdb4 598 /*
c6eb3f70
TG
599 * If the timer is not on the current cpu, we cannot reprogram
600 * the other cpus clock event device.
54cdfdb4 601 */
c6eb3f70
TG
602 if (base->cpu_base != cpu_base)
603 return;
604
605 /*
606 * If the hrtimer interrupt is running, then it will
607 * reevaluate the clock bases and reprogram the clock event
608 * device. The callbacks are always executed in hard interrupt
609 * context so we don't need an extra check for a running
610 * callback.
611 */
612 if (cpu_base->in_hrtirq)
613 return;
54cdfdb4 614
63070a79
TG
615 /*
616 * CLOCK_REALTIME timer might be requested with an absolute
c6eb3f70 617 * expiry time which is less than base->offset. Set it to 0.
63070a79 618 */
2456e855
TG
619 if (expires < 0)
620 expires = 0;
63070a79 621
2456e855 622 if (expires >= cpu_base->expires_next)
c6eb3f70 623 return;
41d2e494 624
c6eb3f70 625 /* Update the pointer to the next expiring timer */
895bdfa7 626 cpu_base->next_timer = timer;
9bc74919 627
41d2e494
TG
628 /*
629 * If a hang was detected in the last timer interrupt then we
630 * do not schedule a timer which is earlier than the expiry
631 * which we enforced in the hang detection. We want the system
632 * to make progress.
633 */
634 if (cpu_base->hang_detected)
c6eb3f70 635 return;
54cdfdb4
TG
636
637 /*
c6eb3f70
TG
638 * Program the timer hardware. We enforce the expiry for
639 * events which are already in the past.
54cdfdb4 640 */
c6eb3f70
TG
641 cpu_base->expires_next = expires;
642 tick_program_event(expires, 1);
54cdfdb4
TG
643}
644
54cdfdb4
TG
645/*
646 * Initialize the high resolution related parts of cpu_base
647 */
648static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
649{
2456e855 650 base->expires_next = KTIME_MAX;
54cdfdb4 651 base->hres_active = 0;
54cdfdb4
TG
652}
653
9ec26907
TG
654/*
655 * Retrigger next event is called after clock was set
656 *
657 * Called with interrupts disabled via on_each_cpu()
658 */
659static void retrigger_next_event(void *arg)
660{
dc5df73b 661 struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
9ec26907 662
e19ffe8b 663 if (!base->hres_active)
9ec26907
TG
664 return;
665
9ec26907 666 raw_spin_lock(&base->lock);
5baefd6d 667 hrtimer_update_base(base);
9ec26907
TG
668 hrtimer_force_reprogram(base, 0);
669 raw_spin_unlock(&base->lock);
670}
b12a03ce 671
54cdfdb4
TG
672/*
673 * Switch to high resolution mode
674 */
75e3b37d 675static void hrtimer_switch_to_hres(void)
54cdfdb4 676{
c6eb3f70 677 struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
54cdfdb4
TG
678
679 if (tick_init_highres()) {
820de5c3 680 printk(KERN_WARNING "Could not switch to high resolution "
c6eb3f70 681 "mode on CPU %d\n", base->cpu);
85e1cd6e 682 return;
54cdfdb4
TG
683 }
684 base->hres_active = 1;
398ca17f 685 hrtimer_resolution = HIGH_RES_NSEC;
54cdfdb4
TG
686
687 tick_setup_sched_timer();
54cdfdb4
TG
688 /* "Retrigger" the interrupt to get things going */
689 retrigger_next_event(NULL);
54cdfdb4
TG
690}
691
5ec2481b
TG
692static void clock_was_set_work(struct work_struct *work)
693{
694 clock_was_set();
695}
696
697static DECLARE_WORK(hrtimer_work, clock_was_set_work);
698
f55a6faa 699/*
b4d90e9f 700 * Called from timekeeping and resume code to reprogram the hrtimer
5ec2481b 701 * interrupt device on all cpus.
f55a6faa
JS
702 */
703void clock_was_set_delayed(void)
704{
5ec2481b 705 schedule_work(&hrtimer_work);
f55a6faa
JS
706}
707
54cdfdb4
TG
708#else
709
e19ffe8b 710static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *b) { return 0; }
54cdfdb4
TG
711static inline int hrtimer_hres_active(void) { return 0; }
712static inline int hrtimer_is_hres_enabled(void) { return 0; }
75e3b37d 713static inline void hrtimer_switch_to_hres(void) { }
7403f41f
AC
714static inline void
715hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
9e1e01dd
VK
716static inline int hrtimer_reprogram(struct hrtimer *timer,
717 struct hrtimer_clock_base *base)
54cdfdb4
TG
718{
719 return 0;
720}
54cdfdb4 721static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
9ec26907 722static inline void retrigger_next_event(void *arg) { }
54cdfdb4
TG
723
724#endif /* CONFIG_HIGH_RES_TIMERS */
725
b12a03ce
TG
726/*
727 * Clock realtime was set
728 *
729 * Change the offset of the realtime clock vs. the monotonic
730 * clock.
731 *
732 * We might have to reprogram the high resolution timer interrupt. On
733 * SMP we call the architecture specific code to retrigger _all_ high
734 * resolution timer interrupts. On UP we just disable interrupts and
735 * call the high resolution interrupt code.
736 */
737void clock_was_set(void)
738{
90ff1f30 739#ifdef CONFIG_HIGH_RES_TIMERS
b12a03ce
TG
740 /* Retrigger the CPU local events everywhere */
741 on_each_cpu(retrigger_next_event, NULL, 1);
9ec26907
TG
742#endif
743 timerfd_clock_was_set();
b12a03ce
TG
744}
745
746/*
747 * During resume we might have to reprogram the high resolution timer
7c4c3a0f
DV
748 * interrupt on all online CPUs. However, all other CPUs will be
749 * stopped with IRQs interrupts disabled so the clock_was_set() call
5ec2481b 750 * must be deferred.
b12a03ce
TG
751 */
752void hrtimers_resume(void)
753{
53bef3fd 754 lockdep_assert_irqs_disabled();
5ec2481b 755 /* Retrigger on the local CPU */
b12a03ce 756 retrigger_next_event(NULL);
5ec2481b
TG
757 /* And schedule a retrigger for all others */
758 clock_was_set_delayed();
b12a03ce
TG
759}
760
c0a31329 761/*
6506f2aa 762 * Counterpart to lock_hrtimer_base above:
c0a31329
TG
763 */
764static inline
765void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
766{
ecb49d1a 767 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
c0a31329
TG
768}
769
770/**
771 * hrtimer_forward - forward the timer expiry
c0a31329 772 * @timer: hrtimer to forward
44f21475 773 * @now: forward past this time
c0a31329
TG
774 * @interval: the interval to forward
775 *
776 * Forward the timer expiry so it will expire in the future.
8dca6f33 777 * Returns the number of overruns.
91e5a217
TG
778 *
779 * Can be safely called from the callback function of @timer. If
780 * called from other contexts @timer must neither be enqueued nor
781 * running the callback and the caller needs to take care of
782 * serialization.
783 *
784 * Note: This only updates the timer expiry value and does not requeue
785 * the timer.
c0a31329 786 */
4d672e7a 787u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
c0a31329 788{
4d672e7a 789 u64 orun = 1;
44f21475 790 ktime_t delta;
c0a31329 791
cc584b21 792 delta = ktime_sub(now, hrtimer_get_expires(timer));
c0a31329 793
2456e855 794 if (delta < 0)
c0a31329
TG
795 return 0;
796
5de2755c
PZ
797 if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
798 return 0;
799
2456e855
TG
800 if (interval < hrtimer_resolution)
801 interval = hrtimer_resolution;
c9db4fa1 802
2456e855 803 if (unlikely(delta >= interval)) {
df869b63 804 s64 incr = ktime_to_ns(interval);
c0a31329
TG
805
806 orun = ktime_divns(delta, incr);
cc584b21 807 hrtimer_add_expires_ns(timer, incr * orun);
2456e855 808 if (hrtimer_get_expires_tv64(timer) > now)
c0a31329
TG
809 return orun;
810 /*
811 * This (and the ktime_add() below) is the
812 * correction for exact:
813 */
814 orun++;
815 }
cc584b21 816 hrtimer_add_expires(timer, interval);
c0a31329
TG
817
818 return orun;
819}
6bdb6b62 820EXPORT_SYMBOL_GPL(hrtimer_forward);
c0a31329
TG
821
822/*
823 * enqueue_hrtimer - internal function to (re)start a timer
824 *
825 * The timer is inserted in expiry order. Insertion into the
826 * red black tree is O(log(n)). Must hold the base lock.
a6037b61
PZ
827 *
828 * Returns 1 when the new timer is the leftmost timer in the tree.
c0a31329 829 */
a6037b61
PZ
830static int enqueue_hrtimer(struct hrtimer *timer,
831 struct hrtimer_clock_base *base)
c0a31329 832{
c6a2a177 833 debug_activate(timer);
237fc6e7 834
ab8177bc 835 base->cpu_base->active_bases |= 1 << base->index;
54cdfdb4 836
887d9dc9 837 timer->state = HRTIMER_STATE_ENQUEUED;
a6037b61 838
b97f44c9 839 return timerqueue_add(&base->active, &timer->node);
288867ec 840}
c0a31329
TG
841
842/*
843 * __remove_hrtimer - internal function to remove a timer
844 *
845 * Caller must hold the base lock.
54cdfdb4
TG
846 *
847 * High resolution timer mode reprograms the clock event device when the
848 * timer is the one which expires next. The caller can disable this by setting
849 * reprogram to zero. This is useful, when the context does a reprogramming
850 * anyway (e.g. timer interrupt)
c0a31329 851 */
3c8aa39d 852static void __remove_hrtimer(struct hrtimer *timer,
303e967f 853 struct hrtimer_clock_base *base,
203cbf77 854 u8 newstate, int reprogram)
c0a31329 855{
e19ffe8b 856 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
203cbf77 857 u8 state = timer->state;
e19ffe8b 858
895bdfa7
TG
859 timer->state = newstate;
860 if (!(state & HRTIMER_STATE_ENQUEUED))
861 return;
7403f41f 862
b97f44c9 863 if (!timerqueue_del(&base->active, &timer->node))
e19ffe8b 864 cpu_base->active_bases &= ~(1 << base->index);
7403f41f 865
7403f41f 866#ifdef CONFIG_HIGH_RES_TIMERS
895bdfa7
TG
867 /*
868 * Note: If reprogram is false we do not update
869 * cpu_base->next_timer. This happens when we remove the first
870 * timer on a remote cpu. No harm as we never dereference
871 * cpu_base->next_timer. So the worst thing what can happen is
872 * an superflous call to hrtimer_force_reprogram() on the
873 * remote cpu later on if the same timer gets enqueued again.
874 */
875 if (reprogram && timer == cpu_base->next_timer)
876 hrtimer_force_reprogram(cpu_base, 1);
7403f41f 877#endif
c0a31329
TG
878}
879
880/*
881 * remove hrtimer, called with base lock held
882 */
883static inline int
8edfb036 884remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart)
c0a31329 885{
303e967f 886 if (hrtimer_is_queued(timer)) {
203cbf77 887 u8 state = timer->state;
54cdfdb4
TG
888 int reprogram;
889
890 /*
891 * Remove the timer and force reprogramming when high
892 * resolution mode is active and the timer is on the current
893 * CPU. If we remove a timer on another CPU, reprogramming is
894 * skipped. The interrupt event on this CPU is fired and
895 * reprogramming happens in the interrupt handler. This is a
896 * rare case and less expensive than a smp call.
897 */
c6a2a177 898 debug_deactivate(timer);
dc5df73b 899 reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
8edfb036 900
887d9dc9
PZ
901 if (!restart)
902 state = HRTIMER_STATE_INACTIVE;
903
f13d4f97 904 __remove_hrtimer(timer, base, state, reprogram);
c0a31329
TG
905 return 1;
906 }
907 return 0;
908}
909
203cbf77
TG
910static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
911 const enum hrtimer_mode mode)
912{
913#ifdef CONFIG_TIME_LOW_RES
914 /*
915 * CONFIG_TIME_LOW_RES indicates that the system has no way to return
916 * granular time values. For relative timers we add hrtimer_resolution
917 * (i.e. one jiffie) to prevent short timeouts.
918 */
919 timer->is_rel = mode & HRTIMER_MODE_REL;
920 if (timer->is_rel)
8b0e1953 921 tim = ktime_add_safe(tim, hrtimer_resolution);
203cbf77
TG
922#endif
923 return tim;
924}
925
58f1f803 926/**
6de6250c 927 * hrtimer_start_range_ns - (re)start an hrtimer
58f1f803
TG
928 * @timer: the timer to be added
929 * @tim: expiry time
930 * @delta_ns: "slack" range for the timer
6de6250c
AMG
931 * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
932 * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED)
58f1f803 933 */
61699e13 934void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
da8b44d5 935 u64 delta_ns, const enum hrtimer_mode mode)
c0a31329 936{
3c8aa39d 937 struct hrtimer_clock_base *base, *new_base;
c0a31329 938 unsigned long flags;
61699e13 939 int leftmost;
c0a31329
TG
940
941 base = lock_hrtimer_base(timer, &flags);
942
943 /* Remove an active timer from the queue: */
8edfb036 944 remove_hrtimer(timer, base, true);
c0a31329 945
203cbf77 946 if (mode & HRTIMER_MODE_REL)
84ea7fe3 947 tim = ktime_add_safe(tim, base->get_time());
203cbf77
TG
948
949 tim = hrtimer_update_lowres(timer, tim, mode);
237fc6e7 950
da8f2e17 951 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
c0a31329 952
84ea7fe3
VK
953 /* Switch the timer base, if necessary: */
954 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
955
a6037b61 956 leftmost = enqueue_hrtimer(timer, new_base);
61699e13
TG
957 if (!leftmost)
958 goto unlock;
49a2a075
VK
959
960 if (!hrtimer_is_hres_active(timer)) {
961 /*
962 * Kick to reschedule the next tick to handle the new timer
963 * on dynticks target.
964 */
ae67bada 965 if (is_timers_nohz_active())
683be13a 966 wake_up_nohz_cpu(new_base->cpu_base->cpu);
c6eb3f70
TG
967 } else {
968 hrtimer_reprogram(timer, new_base);
b22affe0 969 }
61699e13 970unlock:
c0a31329 971 unlock_hrtimer_base(timer, &flags);
7f1e2ca9 972}
da8f2e17
AV
973EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
974
c0a31329
TG
975/**
976 * hrtimer_try_to_cancel - try to deactivate a timer
c0a31329
TG
977 * @timer: hrtimer to stop
978 *
979 * Returns:
980 * 0 when the timer was not active
981 * 1 when the timer was active
0ba42a59 982 * -1 when the timer is currently executing the callback function and
fa9799e3 983 * cannot be stopped
c0a31329
TG
984 */
985int hrtimer_try_to_cancel(struct hrtimer *timer)
986{
3c8aa39d 987 struct hrtimer_clock_base *base;
c0a31329
TG
988 unsigned long flags;
989 int ret = -1;
990
19d9f422
TG
991 /*
992 * Check lockless first. If the timer is not active (neither
993 * enqueued nor running the callback, nothing to do here. The
994 * base lock does not serialize against a concurrent enqueue,
995 * so we can avoid taking it.
996 */
997 if (!hrtimer_active(timer))
998 return 0;
999
c0a31329
TG
1000 base = lock_hrtimer_base(timer, &flags);
1001
303e967f 1002 if (!hrtimer_callback_running(timer))
8edfb036 1003 ret = remove_hrtimer(timer, base, false);
c0a31329
TG
1004
1005 unlock_hrtimer_base(timer, &flags);
1006
1007 return ret;
1008
1009}
8d16b764 1010EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
c0a31329
TG
1011
1012/**
1013 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
c0a31329
TG
1014 * @timer: the timer to be cancelled
1015 *
1016 * Returns:
1017 * 0 when the timer was not active
1018 * 1 when the timer was active
1019 */
1020int hrtimer_cancel(struct hrtimer *timer)
1021{
1022 for (;;) {
1023 int ret = hrtimer_try_to_cancel(timer);
1024
1025 if (ret >= 0)
1026 return ret;
5ef37b19 1027 cpu_relax();
c0a31329
TG
1028 }
1029}
8d16b764 1030EXPORT_SYMBOL_GPL(hrtimer_cancel);
c0a31329
TG
1031
1032/**
1033 * hrtimer_get_remaining - get remaining time for the timer
c0a31329 1034 * @timer: the timer to read
203cbf77 1035 * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y
c0a31329 1036 */
203cbf77 1037ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
c0a31329 1038{
c0a31329
TG
1039 unsigned long flags;
1040 ktime_t rem;
1041
b3bd3de6 1042 lock_hrtimer_base(timer, &flags);
203cbf77
TG
1043 if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1044 rem = hrtimer_expires_remaining_adjusted(timer);
1045 else
1046 rem = hrtimer_expires_remaining(timer);
c0a31329
TG
1047 unlock_hrtimer_base(timer, &flags);
1048
1049 return rem;
1050}
203cbf77 1051EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
c0a31329 1052
3451d024 1053#ifdef CONFIG_NO_HZ_COMMON
69239749
TL
1054/**
1055 * hrtimer_get_next_event - get the time until next expiry event
1056 *
c1ad348b 1057 * Returns the next expiry time or KTIME_MAX if no timer is pending.
69239749 1058 */
c1ad348b 1059u64 hrtimer_get_next_event(void)
69239749 1060{
dc5df73b 1061 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
c1ad348b 1062 u64 expires = KTIME_MAX;
69239749 1063 unsigned long flags;
69239749 1064
ecb49d1a 1065 raw_spin_lock_irqsave(&cpu_base->lock, flags);
3c8aa39d 1066
e19ffe8b 1067 if (!__hrtimer_hres_active(cpu_base))
2456e855 1068 expires = __hrtimer_get_next_event(cpu_base);
3c8aa39d 1069
ecb49d1a 1070 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
3c8aa39d 1071
c1ad348b 1072 return expires;
69239749
TL
1073}
1074#endif
1075
336a9cde
MZ
1076static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1077{
1078 if (likely(clock_id < MAX_CLOCKS)) {
1079 int base = hrtimer_clock_to_base_table[clock_id];
1080
1081 if (likely(base != HRTIMER_MAX_CLOCK_BASES))
1082 return base;
1083 }
1084 WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1085 return HRTIMER_BASE_MONOTONIC;
1086}
1087
237fc6e7
TG
1088static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1089 enum hrtimer_mode mode)
c0a31329 1090{
3c8aa39d 1091 struct hrtimer_cpu_base *cpu_base;
e06383db 1092 int base;
c0a31329 1093
7978672c
GA
1094 memset(timer, 0, sizeof(struct hrtimer));
1095
22127e93 1096 cpu_base = raw_cpu_ptr(&hrtimer_bases);
c0a31329 1097
c9cb2e3d 1098 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
7978672c
GA
1099 clock_id = CLOCK_MONOTONIC;
1100
e06383db
JS
1101 base = hrtimer_clockid_to_base(clock_id);
1102 timer->base = &cpu_base->clock_base[base];
998adc3d 1103 timerqueue_init(&timer->node);
c0a31329 1104}
237fc6e7
TG
1105
1106/**
1107 * hrtimer_init - initialize a timer to the given clock
1108 * @timer: the timer to be initialized
1109 * @clock_id: the clock to be used
6de6250c
AMG
1110 * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
1111 * relative (HRTIMER_MODE_REL); pinned is not considered here!
237fc6e7
TG
1112 */
1113void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1114 enum hrtimer_mode mode)
1115{
c6a2a177 1116 debug_init(timer, clock_id, mode);
237fc6e7
TG
1117 __hrtimer_init(timer, clock_id, mode);
1118}
8d16b764 1119EXPORT_SYMBOL_GPL(hrtimer_init);
c0a31329 1120
887d9dc9
PZ
1121/*
1122 * A timer is active, when it is enqueued into the rbtree or the
1123 * callback function is running or it's in the state of being migrated
1124 * to another cpu.
c0a31329 1125 *
887d9dc9 1126 * It is important for this function to not return a false negative.
c0a31329 1127 */
887d9dc9 1128bool hrtimer_active(const struct hrtimer *timer)
c0a31329 1129{
3c8aa39d 1130 struct hrtimer_cpu_base *cpu_base;
887d9dc9 1131 unsigned int seq;
c0a31329 1132
887d9dc9
PZ
1133 do {
1134 cpu_base = READ_ONCE(timer->base->cpu_base);
1135 seq = raw_read_seqcount_begin(&cpu_base->seq);
c0a31329 1136
887d9dc9
PZ
1137 if (timer->state != HRTIMER_STATE_INACTIVE ||
1138 cpu_base->running == timer)
1139 return true;
1140
1141 } while (read_seqcount_retry(&cpu_base->seq, seq) ||
1142 cpu_base != READ_ONCE(timer->base->cpu_base));
1143
1144 return false;
c0a31329 1145}
887d9dc9 1146EXPORT_SYMBOL_GPL(hrtimer_active);
c0a31329 1147
887d9dc9
PZ
1148/*
1149 * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1150 * distinct sections:
1151 *
1152 * - queued: the timer is queued
1153 * - callback: the timer is being ran
1154 * - post: the timer is inactive or (re)queued
1155 *
1156 * On the read side we ensure we observe timer->state and cpu_base->running
1157 * from the same section, if anything changed while we looked at it, we retry.
1158 * This includes timer->base changing because sequence numbers alone are
1159 * insufficient for that.
1160 *
1161 * The sequence numbers are required because otherwise we could still observe
1162 * a false negative if the read side got smeared over multiple consequtive
1163 * __run_hrtimer() invocations.
1164 */
1165
21d6d52a
TG
1166static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
1167 struct hrtimer_clock_base *base,
1168 struct hrtimer *timer, ktime_t *now)
d3d74453 1169{
d3d74453
PZ
1170 enum hrtimer_restart (*fn)(struct hrtimer *);
1171 int restart;
1172
887d9dc9 1173 lockdep_assert_held(&cpu_base->lock);
ca109491 1174
c6a2a177 1175 debug_deactivate(timer);
887d9dc9
PZ
1176 cpu_base->running = timer;
1177
1178 /*
1179 * Separate the ->running assignment from the ->state assignment.
1180 *
1181 * As with a regular write barrier, this ensures the read side in
1182 * hrtimer_active() cannot observe cpu_base->running == NULL &&
1183 * timer->state == INACTIVE.
1184 */
1185 raw_write_seqcount_barrier(&cpu_base->seq);
1186
1187 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
d3d74453 1188 fn = timer->function;
ca109491 1189
203cbf77
TG
1190 /*
1191 * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
1192 * timer is restarted with a period then it becomes an absolute
1193 * timer. If its not restarted it does not matter.
1194 */
1195 if (IS_ENABLED(CONFIG_TIME_LOW_RES))
1196 timer->is_rel = false;
1197
ca109491 1198 /*
d05ca13b
TG
1199 * The timer is marked as running in the CPU base, so it is
1200 * protected against migration to a different CPU even if the lock
1201 * is dropped.
ca109491 1202 */
ecb49d1a 1203 raw_spin_unlock(&cpu_base->lock);
c6a2a177 1204 trace_hrtimer_expire_entry(timer, now);
ca109491 1205 restart = fn(timer);
c6a2a177 1206 trace_hrtimer_expire_exit(timer);
ecb49d1a 1207 raw_spin_lock(&cpu_base->lock);
d3d74453
PZ
1208
1209 /*
887d9dc9 1210 * Note: We clear the running state after enqueue_hrtimer and
b4d90e9f 1211 * we do not reprogram the event hardware. Happens either in
e3f1d883 1212 * hrtimer_start_range_ns() or in hrtimer_interrupt()
5de2755c
PZ
1213 *
1214 * Note: Because we dropped the cpu_base->lock above,
1215 * hrtimer_start_range_ns() can have popped in and enqueued the timer
1216 * for us already.
d3d74453 1217 */
5de2755c
PZ
1218 if (restart != HRTIMER_NORESTART &&
1219 !(timer->state & HRTIMER_STATE_ENQUEUED))
a6037b61 1220 enqueue_hrtimer(timer, base);
f13d4f97 1221
887d9dc9
PZ
1222 /*
1223 * Separate the ->running assignment from the ->state assignment.
1224 *
1225 * As with a regular write barrier, this ensures the read side in
1226 * hrtimer_active() cannot observe cpu_base->running == NULL &&
1227 * timer->state == INACTIVE.
1228 */
1229 raw_write_seqcount_barrier(&cpu_base->seq);
f13d4f97 1230
887d9dc9
PZ
1231 WARN_ON_ONCE(cpu_base->running != timer);
1232 cpu_base->running = NULL;
d3d74453
PZ
1233}
1234
21d6d52a 1235static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
54cdfdb4 1236{
34aee88a
TG
1237 struct hrtimer_clock_base *base = cpu_base->clock_base;
1238 unsigned int active = cpu_base->active_bases;
6ff7041d 1239
34aee88a 1240 for (; active; base++, active >>= 1) {
998adc3d 1241 struct timerqueue_node *node;
ab8177bc
TG
1242 ktime_t basenow;
1243
34aee88a 1244 if (!(active & 0x01))
ab8177bc 1245 continue;
54cdfdb4 1246
54cdfdb4
TG
1247 basenow = ktime_add(now, base->offset);
1248
998adc3d 1249 while ((node = timerqueue_getnext(&base->active))) {
54cdfdb4
TG
1250 struct hrtimer *timer;
1251
998adc3d 1252 timer = container_of(node, struct hrtimer, node);
54cdfdb4 1253
654c8e0b
AV
1254 /*
1255 * The immediate goal for using the softexpires is
1256 * minimizing wakeups, not running timers at the
1257 * earliest interrupt after their soft expiration.
1258 * This allows us to avoid using a Priority Search
1259 * Tree, which can answer a stabbing querry for
1260 * overlapping intervals and instead use the simple
1261 * BST we already have.
1262 * We don't add extra wakeups by delaying timers that
1263 * are right-of a not yet expired timer, because that
1264 * timer will have to trigger a wakeup anyway.
1265 */
2456e855 1266 if (basenow < hrtimer_get_softexpires_tv64(timer))
54cdfdb4 1267 break;
54cdfdb4 1268
21d6d52a 1269 __run_hrtimer(cpu_base, base, timer, &basenow);
54cdfdb4 1270 }
54cdfdb4 1271 }
21d6d52a
TG
1272}
1273
1274#ifdef CONFIG_HIGH_RES_TIMERS
1275
1276/*
1277 * High resolution timer interrupt
1278 * Called with interrupts disabled
1279 */
1280void hrtimer_interrupt(struct clock_event_device *dev)
1281{
1282 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1283 ktime_t expires_next, now, entry_time, delta;
1284 int retries = 0;
1285
1286 BUG_ON(!cpu_base->hres_active);
1287 cpu_base->nr_events++;
2456e855 1288 dev->next_event = KTIME_MAX;
21d6d52a
TG
1289
1290 raw_spin_lock(&cpu_base->lock);
1291 entry_time = now = hrtimer_update_base(cpu_base);
1292retry:
1293 cpu_base->in_hrtirq = 1;
1294 /*
1295 * We set expires_next to KTIME_MAX here with cpu_base->lock
1296 * held to prevent that a timer is enqueued in our queue via
1297 * the migration code. This does not affect enqueueing of
1298 * timers which run their callback and need to be requeued on
1299 * this CPU.
1300 */
2456e855 1301 cpu_base->expires_next = KTIME_MAX;
21d6d52a
TG
1302
1303 __hrtimer_run_queues(cpu_base, now);
1304
9bc74919
TG
1305 /* Reevaluate the clock bases for the next expiry */
1306 expires_next = __hrtimer_get_next_event(cpu_base);
6ff7041d
TG
1307 /*
1308 * Store the new expiry value so the migration code can verify
1309 * against it.
1310 */
54cdfdb4 1311 cpu_base->expires_next = expires_next;
9bc74919 1312 cpu_base->in_hrtirq = 0;
ecb49d1a 1313 raw_spin_unlock(&cpu_base->lock);
54cdfdb4
TG
1314
1315 /* Reprogramming necessary ? */
d2540875 1316 if (!tick_program_event(expires_next, 0)) {
41d2e494
TG
1317 cpu_base->hang_detected = 0;
1318 return;
54cdfdb4 1319 }
41d2e494
TG
1320
1321 /*
1322 * The next timer was already expired due to:
1323 * - tracing
1324 * - long lasting callbacks
1325 * - being scheduled away when running in a VM
1326 *
1327 * We need to prevent that we loop forever in the hrtimer
1328 * interrupt routine. We give it 3 attempts to avoid
1329 * overreacting on some spurious event.
5baefd6d
JS
1330 *
1331 * Acquire base lock for updating the offsets and retrieving
1332 * the current time.
41d2e494 1333 */
196951e9 1334 raw_spin_lock(&cpu_base->lock);
5baefd6d 1335 now = hrtimer_update_base(cpu_base);
41d2e494
TG
1336 cpu_base->nr_retries++;
1337 if (++retries < 3)
1338 goto retry;
1339 /*
1340 * Give the system a chance to do something else than looping
1341 * here. We stored the entry time, so we know exactly how long
1342 * we spent here. We schedule the next event this amount of
1343 * time away.
1344 */
1345 cpu_base->nr_hangs++;
1346 cpu_base->hang_detected = 1;
196951e9 1347 raw_spin_unlock(&cpu_base->lock);
41d2e494 1348 delta = ktime_sub(now, entry_time);
2456e855
TG
1349 if ((unsigned int)delta > cpu_base->max_hang_time)
1350 cpu_base->max_hang_time = (unsigned int) delta;
41d2e494
TG
1351 /*
1352 * Limit it to a sensible value as we enforce a longer
1353 * delay. Give the CPU at least 100ms to catch up.
1354 */
2456e855 1355 if (delta > 100 * NSEC_PER_MSEC)
41d2e494
TG
1356 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1357 else
1358 expires_next = ktime_add(now, delta);
1359 tick_program_event(expires_next, 1);
1360 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1361 ktime_to_ns(delta));
54cdfdb4
TG
1362}
1363
016da201 1364/* called with interrupts disabled */
c6eb3f70 1365static inline void __hrtimer_peek_ahead_timers(void)
8bdec955
TG
1366{
1367 struct tick_device *td;
1368
1369 if (!hrtimer_hres_active())
1370 return;
1371
22127e93 1372 td = this_cpu_ptr(&tick_cpu_device);
8bdec955
TG
1373 if (td && td->evtdev)
1374 hrtimer_interrupt(td->evtdev);
1375}
1376
82c5b7b5
IM
1377#else /* CONFIG_HIGH_RES_TIMERS */
1378
1379static inline void __hrtimer_peek_ahead_timers(void) { }
1380
1381#endif /* !CONFIG_HIGH_RES_TIMERS */
82f67cd9 1382
d3d74453 1383/*
c6eb3f70 1384 * Called from run_local_timers in hardirq context every jiffy
d3d74453 1385 */
833883d9 1386void hrtimer_run_queues(void)
d3d74453 1387{
dc5df73b 1388 struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
21d6d52a 1389 ktime_t now;
c0a31329 1390
e19ffe8b 1391 if (__hrtimer_hres_active(cpu_base))
d3d74453 1392 return;
54cdfdb4 1393
d3d74453 1394 /*
c6eb3f70
TG
1395 * This _is_ ugly: We have to check periodically, whether we
1396 * can switch to highres and / or nohz mode. The clocksource
1397 * switch happens with xtime_lock held. Notification from
1398 * there only sets the check bit in the tick_oneshot code,
1399 * otherwise we might deadlock vs. xtime_lock.
d3d74453 1400 */
c6eb3f70 1401 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
d3d74453 1402 hrtimer_switch_to_hres();
3055adda 1403 return;
833883d9 1404 }
c6eb3f70 1405
21d6d52a
TG
1406 raw_spin_lock(&cpu_base->lock);
1407 now = hrtimer_update_base(cpu_base);
1408 __hrtimer_run_queues(cpu_base, now);
1409 raw_spin_unlock(&cpu_base->lock);
c0a31329
TG
1410}
1411
10c94ec1
TG
1412/*
1413 * Sleep related functions:
1414 */
c9cb2e3d 1415static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
00362e33
TG
1416{
1417 struct hrtimer_sleeper *t =
1418 container_of(timer, struct hrtimer_sleeper, timer);
1419 struct task_struct *task = t->task;
1420
1421 t->task = NULL;
1422 if (task)
1423 wake_up_process(task);
1424
1425 return HRTIMER_NORESTART;
1426}
1427
36c8b586 1428void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
00362e33
TG
1429{
1430 sl->timer.function = hrtimer_wakeup;
1431 sl->task = task;
1432}
2bc481cf 1433EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
00362e33 1434
c0edd7c9 1435int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
ce41aaf4
AV
1436{
1437 switch(restart->nanosleep.type) {
1438#ifdef CONFIG_COMPAT
1439 case TT_COMPAT:
c0edd7c9 1440 if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp))
ce41aaf4
AV
1441 return -EFAULT;
1442 break;
1443#endif
1444 case TT_NATIVE:
c0edd7c9 1445 if (put_timespec64(ts, restart->nanosleep.rmtp))
ce41aaf4
AV
1446 return -EFAULT;
1447 break;
1448 default:
1449 BUG();
1450 }
1451 return -ERESTART_RESTARTBLOCK;
1452}
1453
669d7868 1454static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
432569bb 1455{
edbeda46
AV
1456 struct restart_block *restart;
1457
669d7868 1458 hrtimer_init_sleeper(t, current);
10c94ec1 1459
432569bb
RZ
1460 do {
1461 set_current_state(TASK_INTERRUPTIBLE);
cc584b21 1462 hrtimer_start_expires(&t->timer, mode);
432569bb 1463
54cdfdb4 1464 if (likely(t->task))
b0f8c44f 1465 freezable_schedule();
432569bb 1466
669d7868 1467 hrtimer_cancel(&t->timer);
c9cb2e3d 1468 mode = HRTIMER_MODE_ABS;
669d7868
TG
1469
1470 } while (t->task && !signal_pending(current));
432569bb 1471
3588a085
PZ
1472 __set_current_state(TASK_RUNNING);
1473
a7602681 1474 if (!t->task)
080344b9 1475 return 0;
080344b9 1476
edbeda46
AV
1477 restart = &current->restart_block;
1478 if (restart->nanosleep.type != TT_NONE) {
a7602681 1479 ktime_t rem = hrtimer_expires_remaining(&t->timer);
c0edd7c9 1480 struct timespec64 rmt;
edbeda46 1481
a7602681
AV
1482 if (rem <= 0)
1483 return 0;
c0edd7c9 1484 rmt = ktime_to_timespec64(rem);
a7602681 1485
ce41aaf4 1486 return nanosleep_copyout(restart, &rmt);
a7602681
AV
1487 }
1488 return -ERESTART_RESTARTBLOCK;
080344b9
ON
1489}
1490
fb923c4a 1491static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
10c94ec1 1492{
669d7868 1493 struct hrtimer_sleeper t;
a7602681 1494 int ret;
10c94ec1 1495
ab8177bc 1496 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
237fc6e7 1497 HRTIMER_MODE_ABS);
cc584b21 1498 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
10c94ec1 1499
a7602681 1500 ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
237fc6e7
TG
1501 destroy_hrtimer_on_stack(&t.timer);
1502 return ret;
10c94ec1
TG
1503}
1504
938e7cf2 1505long hrtimer_nanosleep(const struct timespec64 *rqtp,
10c94ec1
TG
1506 const enum hrtimer_mode mode, const clockid_t clockid)
1507{
a7602681 1508 struct restart_block *restart;
669d7868 1509 struct hrtimer_sleeper t;
237fc6e7 1510 int ret = 0;
da8b44d5 1511 u64 slack;
3bd01206
AV
1512
1513 slack = current->timer_slack_ns;
aab03e05 1514 if (dl_task(current) || rt_task(current))
3bd01206 1515 slack = 0;
10c94ec1 1516
237fc6e7 1517 hrtimer_init_on_stack(&t.timer, clockid, mode);
ad196384 1518 hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
a7602681
AV
1519 ret = do_nanosleep(&t, mode);
1520 if (ret != -ERESTART_RESTARTBLOCK)
237fc6e7 1521 goto out;
10c94ec1 1522
7978672c 1523 /* Absolute timers do not update the rmtp value and restart: */
237fc6e7
TG
1524 if (mode == HRTIMER_MODE_ABS) {
1525 ret = -ERESTARTNOHAND;
1526 goto out;
1527 }
10c94ec1 1528
a7602681 1529 restart = &current->restart_block;
1711ef38 1530 restart->fn = hrtimer_nanosleep_restart;
ab8177bc 1531 restart->nanosleep.clockid = t.timer.base->clockid;
cc584b21 1532 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
237fc6e7
TG
1533out:
1534 destroy_hrtimer_on_stack(&t.timer);
1535 return ret;
10c94ec1
TG
1536}
1537
58fd3aa2
HC
1538SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1539 struct timespec __user *, rmtp)
6ba1b912 1540{
c0edd7c9 1541 struct timespec64 tu;
6ba1b912 1542
c0edd7c9 1543 if (get_timespec64(&tu, rqtp))
6ba1b912
TG
1544 return -EFAULT;
1545
c0edd7c9 1546 if (!timespec64_valid(&tu))
6ba1b912
TG
1547 return -EINVAL;
1548
edbeda46 1549 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
192a82f9 1550 current->restart_block.nanosleep.rmtp = rmtp;
c0edd7c9 1551 return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
6ba1b912
TG
1552}
1553
edbeda46
AV
1554#ifdef CONFIG_COMPAT
1555
1556COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
1557 struct compat_timespec __user *, rmtp)
1558{
c0edd7c9 1559 struct timespec64 tu;
edbeda46 1560
c0edd7c9 1561 if (compat_get_timespec64(&tu, rqtp))
edbeda46
AV
1562 return -EFAULT;
1563
c0edd7c9 1564 if (!timespec64_valid(&tu))
edbeda46
AV
1565 return -EINVAL;
1566
1567 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1568 current->restart_block.nanosleep.compat_rmtp = rmtp;
c0edd7c9 1569 return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
edbeda46
AV
1570}
1571#endif
1572
c0a31329
TG
1573/*
1574 * Functions related to boot-time initialization:
1575 */
27590dc1 1576int hrtimers_prepare_cpu(unsigned int cpu)
c0a31329 1577{
3c8aa39d 1578 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
c0a31329
TG
1579 int i;
1580
998adc3d 1581 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
3c8aa39d 1582 cpu_base->clock_base[i].cpu_base = cpu_base;
998adc3d
JS
1583 timerqueue_init_head(&cpu_base->clock_base[i].active);
1584 }
3c8aa39d 1585
cddd0248 1586 cpu_base->cpu = cpu;
54cdfdb4 1587 hrtimer_init_hres(cpu_base);
27590dc1 1588 return 0;
c0a31329
TG
1589}
1590
1591#ifdef CONFIG_HOTPLUG_CPU
1592
ca109491 1593static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
37810659 1594 struct hrtimer_clock_base *new_base)
c0a31329
TG
1595{
1596 struct hrtimer *timer;
998adc3d 1597 struct timerqueue_node *node;
c0a31329 1598
998adc3d
JS
1599 while ((node = timerqueue_getnext(&old_base->active))) {
1600 timer = container_of(node, struct hrtimer, node);
54cdfdb4 1601 BUG_ON(hrtimer_callback_running(timer));
c6a2a177 1602 debug_deactivate(timer);
b00c1a99
TG
1603
1604 /*
c04dca02 1605 * Mark it as ENQUEUED not INACTIVE otherwise the
b00c1a99
TG
1606 * timer could be seen as !active and just vanish away
1607 * under us on another CPU
1608 */
c04dca02 1609 __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
c0a31329 1610 timer->base = new_base;
54cdfdb4 1611 /*
e3f1d883
TG
1612 * Enqueue the timers on the new cpu. This does not
1613 * reprogram the event device in case the timer
1614 * expires before the earliest on this CPU, but we run
1615 * hrtimer_interrupt after we migrated everything to
1616 * sort out already expired timers and reprogram the
1617 * event device.
54cdfdb4 1618 */
a6037b61 1619 enqueue_hrtimer(timer, new_base);
c0a31329
TG
1620 }
1621}
1622
27590dc1 1623int hrtimers_dead_cpu(unsigned int scpu)
c0a31329 1624{
3c8aa39d 1625 struct hrtimer_cpu_base *old_base, *new_base;
731a55ba 1626 int i;
c0a31329 1627
37810659 1628 BUG_ON(cpu_online(scpu));
37810659 1629 tick_cancel_sched_timer(scpu);
731a55ba
TG
1630
1631 local_irq_disable();
1632 old_base = &per_cpu(hrtimer_bases, scpu);
dc5df73b 1633 new_base = this_cpu_ptr(&hrtimer_bases);
d82f0b0f
ON
1634 /*
1635 * The caller is globally serialized and nobody else
1636 * takes two locks at once, deadlock is not possible.
1637 */
ecb49d1a
TG
1638 raw_spin_lock(&new_base->lock);
1639 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
c0a31329 1640
3c8aa39d 1641 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
ca109491 1642 migrate_hrtimer_list(&old_base->clock_base[i],
37810659 1643 &new_base->clock_base[i]);
c0a31329
TG
1644 }
1645
ecb49d1a
TG
1646 raw_spin_unlock(&old_base->lock);
1647 raw_spin_unlock(&new_base->lock);
37810659 1648
731a55ba
TG
1649 /* Check, if we got expired work to do */
1650 __hrtimer_peek_ahead_timers();
1651 local_irq_enable();
27590dc1 1652 return 0;
c0a31329 1653}
37810659 1654
c0a31329
TG
1655#endif /* CONFIG_HOTPLUG_CPU */
1656
c0a31329
TG
1657void __init hrtimers_init(void)
1658{
27590dc1 1659 hrtimers_prepare_cpu(smp_processor_id());
c0a31329
TG
1660}
1661
7bb67439 1662/**
351b3f7a 1663 * schedule_hrtimeout_range_clock - sleep until timeout
7bb67439 1664 * @expires: timeout value (ktime_t)
654c8e0b 1665 * @delta: slack in expires timeout (ktime_t)
90777713
AMG
1666 * @mode: timer mode
1667 * @clock_id: timer clock to be used
7bb67439 1668 */
351b3f7a 1669int __sched
da8b44d5 1670schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
90777713 1671 const enum hrtimer_mode mode, clockid_t clock_id)
7bb67439
AV
1672{
1673 struct hrtimer_sleeper t;
1674
1675 /*
1676 * Optimize when a zero timeout value is given. It does not
1677 * matter whether this is an absolute or a relative time.
1678 */
2456e855 1679 if (expires && *expires == 0) {
7bb67439
AV
1680 __set_current_state(TASK_RUNNING);
1681 return 0;
1682 }
1683
1684 /*
43b21013 1685 * A NULL parameter means "infinite"
7bb67439
AV
1686 */
1687 if (!expires) {
1688 schedule();
7bb67439
AV
1689 return -EINTR;
1690 }
1691
90777713 1692 hrtimer_init_on_stack(&t.timer, clock_id, mode);
654c8e0b 1693 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
7bb67439
AV
1694
1695 hrtimer_init_sleeper(&t, current);
1696
cc584b21 1697 hrtimer_start_expires(&t.timer, mode);
7bb67439
AV
1698
1699 if (likely(t.task))
1700 schedule();
1701
1702 hrtimer_cancel(&t.timer);
1703 destroy_hrtimer_on_stack(&t.timer);
1704
1705 __set_current_state(TASK_RUNNING);
1706
1707 return !t.task ? 0 : -EINTR;
1708}
351b3f7a
CE
1709
1710/**
1711 * schedule_hrtimeout_range - sleep until timeout
1712 * @expires: timeout value (ktime_t)
1713 * @delta: slack in expires timeout (ktime_t)
90777713 1714 * @mode: timer mode
351b3f7a
CE
1715 *
1716 * Make the current task sleep until the given expiry time has
1717 * elapsed. The routine will return immediately unless
1718 * the current task state has been set (see set_current_state()).
1719 *
1720 * The @delta argument gives the kernel the freedom to schedule the
1721 * actual wakeup to a time that is both power and performance friendly.
1722 * The kernel give the normal best effort behavior for "@expires+@delta",
1723 * but may decide to fire the timer earlier, but no earlier than @expires.
1724 *
1725 * You can set the task state as follows -
1726 *
1727 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
4b7e9cf9
DA
1728 * pass before the routine returns unless the current task is explicitly
1729 * woken up, (e.g. by wake_up_process()).
351b3f7a
CE
1730 *
1731 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
4b7e9cf9
DA
1732 * delivered to the current task or the current task is explicitly woken
1733 * up.
351b3f7a
CE
1734 *
1735 * The current task state is guaranteed to be TASK_RUNNING when this
1736 * routine returns.
1737 *
4b7e9cf9
DA
1738 * Returns 0 when the timer has expired. If the task was woken before the
1739 * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
1740 * by an explicit wakeup, it returns -EINTR.
351b3f7a 1741 */
da8b44d5 1742int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
351b3f7a
CE
1743 const enum hrtimer_mode mode)
1744{
1745 return schedule_hrtimeout_range_clock(expires, delta, mode,
1746 CLOCK_MONOTONIC);
1747}
654c8e0b
AV
1748EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1749
1750/**
1751 * schedule_hrtimeout - sleep until timeout
1752 * @expires: timeout value (ktime_t)
90777713 1753 * @mode: timer mode
654c8e0b
AV
1754 *
1755 * Make the current task sleep until the given expiry time has
1756 * elapsed. The routine will return immediately unless
1757 * the current task state has been set (see set_current_state()).
1758 *
1759 * You can set the task state as follows -
1760 *
1761 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
4b7e9cf9
DA
1762 * pass before the routine returns unless the current task is explicitly
1763 * woken up, (e.g. by wake_up_process()).
654c8e0b
AV
1764 *
1765 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
4b7e9cf9
DA
1766 * delivered to the current task or the current task is explicitly woken
1767 * up.
654c8e0b
AV
1768 *
1769 * The current task state is guaranteed to be TASK_RUNNING when this
1770 * routine returns.
1771 *
4b7e9cf9
DA
1772 * Returns 0 when the timer has expired. If the task was woken before the
1773 * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
1774 * by an explicit wakeup, it returns -EINTR.
654c8e0b
AV
1775 */
1776int __sched schedule_hrtimeout(ktime_t *expires,
1777 const enum hrtimer_mode mode)
1778{
1779 return schedule_hrtimeout_range(expires, 0, mode);
1780}
7bb67439 1781EXPORT_SYMBOL_GPL(schedule_hrtimeout);