These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / kernel / time / hrtimer.c
1 /*
2  *  linux/kernel/hrtimer.c
3  *
4  *  Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
5  *  Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
6  *  Copyright(C) 2006-2007  Timesys Corp., Thomas Gleixner
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  *
25  *      Help, testing, suggestions, bugfixes, improvements were
26  *      provided by:
27  *
28  *      George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29  *      et. al.
30  *
31  *  For licencing details see kernel-base/COPYING
32  */
33
34 #include <linux/cpu.h>
35 #include <linux/export.h>
36 #include <linux/percpu.h>
37 #include <linux/hrtimer.h>
38 #include <linux/notifier.h>
39 #include <linux/syscalls.h>
40 #include <linux/kallsyms.h>
41 #include <linux/interrupt.h>
42 #include <linux/tick.h>
43 #include <linux/seq_file.h>
44 #include <linux/err.h>
45 #include <linux/debugobjects.h>
46 #include <linux/sched.h>
47 #include <linux/sched/sysctl.h>
48 #include <linux/sched/rt.h>
49 #include <linux/sched/deadline.h>
50 #include <linux/timer.h>
51 #include <linux/kthread.h>
52 #include <linux/freezer.h>
53
54 #include <asm/uaccess.h>
55
56 #include <trace/events/timer.h>
57 #include <trace/events/hist.h>
58
59 #include "tick-internal.h"
60
61 /*
62  * The timer bases:
63  *
64  * There are more clockids than hrtimer bases. Thus, we index
65  * into the timer bases by the hrtimer_base_type enum. When trying
66  * to reach a base using a clockid, hrtimer_clockid_to_base()
67  * is used to convert from clockid to the proper hrtimer_base_type.
68  */
69 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
70 {
71         .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
72         .seq = SEQCNT_ZERO(hrtimer_bases.seq),
73         .clock_base =
74         {
75                 {
76                         .index = HRTIMER_BASE_MONOTONIC,
77                         .clockid = CLOCK_MONOTONIC,
78                         .get_time = &ktime_get,
79                 },
80                 {
81                         .index = HRTIMER_BASE_REALTIME,
82                         .clockid = CLOCK_REALTIME,
83                         .get_time = &ktime_get_real,
84                 },
85                 {
86                         .index = HRTIMER_BASE_BOOTTIME,
87                         .clockid = CLOCK_BOOTTIME,
88                         .get_time = &ktime_get_boottime,
89                 },
90                 {
91                         .index = HRTIMER_BASE_TAI,
92                         .clockid = CLOCK_TAI,
93                         .get_time = &ktime_get_clocktai,
94                 },
95         }
96 };
97
98 static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
99         [CLOCK_REALTIME]        = HRTIMER_BASE_REALTIME,
100         [CLOCK_MONOTONIC]       = HRTIMER_BASE_MONOTONIC,
101         [CLOCK_BOOTTIME]        = HRTIMER_BASE_BOOTTIME,
102         [CLOCK_TAI]             = HRTIMER_BASE_TAI,
103 };
104
105 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
106 {
107         return hrtimer_clock_to_base_table[clock_id];
108 }
109
110 /*
111  * Functions and macros which are different for UP/SMP systems are kept in a
112  * single place
113  */
114 #ifdef CONFIG_SMP
115
116 /*
117  * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
118  * such that hrtimer_callback_running() can unconditionally dereference
119  * timer->base->cpu_base
120  */
121 static struct hrtimer_cpu_base migration_cpu_base = {
122         .seq = SEQCNT_ZERO(migration_cpu_base),
123         .clock_base = { { .cpu_base = &migration_cpu_base, }, },
124 };
125
126 #define migration_base  migration_cpu_base.clock_base[0]
127
128 /*
129  * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
130  * means that all timers which are tied to this base via timer->base are
131  * locked, and the base itself is locked too.
132  *
133  * So __run_timers/migrate_timers can safely modify all timers which could
134  * be found on the lists/queues.
135  *
136  * When the timer's base is locked, and the timer removed from list, it is
137  * possible to set timer->base = &migration_base and drop the lock: the timer
138  * remains locked.
139  */
140 static
141 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
142                                              unsigned long *flags)
143 {
144         struct hrtimer_clock_base *base;
145
146         for (;;) {
147                 base = timer->base;
148                 if (likely(base != &migration_base)) {
149                         raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
150                         if (likely(base == timer->base))
151                                 return base;
152                         /* The timer has migrated to another CPU: */
153                         raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
154                 }
155                 cpu_relax();
156         }
157 }
158
159 /*
160  * With HIGHRES=y we do not migrate the timer when it is expiring
161  * before the next event on the target cpu because we cannot reprogram
162  * the target cpu hardware and we would cause it to fire late.
163  *
164  * Called with cpu_base->lock of target cpu held.
165  */
166 static int
167 hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
168 {
169 #ifdef CONFIG_HIGH_RES_TIMERS
170         ktime_t expires;
171
172         if (!new_base->cpu_base->hres_active)
173                 return 0;
174
175         expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
176         return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
177 #else
178         return 0;
179 #endif
180 }
181
182 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
183 static inline
184 struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
185                                          int pinned)
186 {
187         if (pinned || !base->migration_enabled)
188                 return base;
189         return &per_cpu(hrtimer_bases, get_nohz_timer_target());
190 }
191 #else
192 static inline
193 struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
194                                          int pinned)
195 {
196         return base;
197 }
198 #endif
199
200 /*
201  * We switch the timer base to a power-optimized selected CPU target,
202  * if:
203  *      - NO_HZ_COMMON is enabled
204  *      - timer migration is enabled
205  *      - the timer callback is not running
206  *      - the timer is not the first expiring timer on the new target
207  *
208  * If one of the above requirements is not fulfilled we move the timer
209  * to the current CPU or leave it on the previously assigned CPU if
210  * the timer callback is currently running.
211  */
212 static inline struct hrtimer_clock_base *
213 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
214                     int pinned)
215 {
216         struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
217         struct hrtimer_clock_base *new_base;
218         int basenum = base->index;
219
220         this_cpu_base = this_cpu_ptr(&hrtimer_bases);
221         new_cpu_base = get_target_base(this_cpu_base, pinned);
222 again:
223         new_base = &new_cpu_base->clock_base[basenum];
224
225         if (base != new_base) {
226                 /*
227                  * We are trying to move timer to new_base.
228                  * However we can't change timer's base while it is running,
229                  * so we keep it on the same CPU. No hassle vs. reprogramming
230                  * the event source in the high resolution case. The softirq
231                  * code will take care of this when the timer function has
232                  * completed. There is no conflict as we hold the lock until
233                  * the timer is enqueued.
234                  */
235                 if (unlikely(hrtimer_callback_running(timer)))
236                         return base;
237
238                 /* See the comment in lock_hrtimer_base() */
239                 timer->base = &migration_base;
240                 raw_spin_unlock(&base->cpu_base->lock);
241                 raw_spin_lock(&new_base->cpu_base->lock);
242
243                 if (new_cpu_base != this_cpu_base &&
244                     hrtimer_check_target(timer, new_base)) {
245                         raw_spin_unlock(&new_base->cpu_base->lock);
246                         raw_spin_lock(&base->cpu_base->lock);
247                         new_cpu_base = this_cpu_base;
248                         timer->base = base;
249                         goto again;
250                 }
251                 timer->base = new_base;
252         } else {
253                 if (new_cpu_base != this_cpu_base &&
254                     hrtimer_check_target(timer, new_base)) {
255                         new_cpu_base = this_cpu_base;
256                         goto again;
257                 }
258         }
259         return new_base;
260 }
261
262 #else /* CONFIG_SMP */
263
264 static inline struct hrtimer_clock_base *
265 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
266 {
267         struct hrtimer_clock_base *base = timer->base;
268
269         raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
270
271         return base;
272 }
273
274 # define switch_hrtimer_base(t, b, p)   (b)
275
276 #endif  /* !CONFIG_SMP */
277
278 /*
279  * Functions for the union type storage format of ktime_t which are
280  * too large for inlining:
281  */
282 #if BITS_PER_LONG < 64
283 /*
284  * Divide a ktime value by a nanosecond value
285  */
286 s64 __ktime_divns(const ktime_t kt, s64 div)
287 {
288         int sft = 0;
289         s64 dclc;
290         u64 tmp;
291
292         dclc = ktime_to_ns(kt);
293         tmp = dclc < 0 ? -dclc : dclc;
294
295         /* Make sure the divisor is less than 2^32: */
296         while (div >> 32) {
297                 sft++;
298                 div >>= 1;
299         }
300         tmp >>= sft;
301         do_div(tmp, (unsigned long) div);
302         return dclc < 0 ? -tmp : tmp;
303 }
304 EXPORT_SYMBOL_GPL(__ktime_divns);
305 #endif /* BITS_PER_LONG >= 64 */
306
307 /*
308  * Add two ktime values and do a safety check for overflow:
309  */
310 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
311 {
312         ktime_t res = ktime_add(lhs, rhs);
313
314         /*
315          * We use KTIME_SEC_MAX here, the maximum timeout which we can
316          * return to user space in a timespec:
317          */
318         if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
319                 res = ktime_set(KTIME_SEC_MAX, 0);
320
321         return res;
322 }
323
324 EXPORT_SYMBOL_GPL(ktime_add_safe);
325
326 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
327
328 static struct debug_obj_descr hrtimer_debug_descr;
329
330 static void *hrtimer_debug_hint(void *addr)
331 {
332         return ((struct hrtimer *) addr)->function;
333 }
334
335 /*
336  * fixup_init is called when:
337  * - an active object is initialized
338  */
339 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
340 {
341         struct hrtimer *timer = addr;
342
343         switch (state) {
344         case ODEBUG_STATE_ACTIVE:
345                 hrtimer_cancel(timer);
346                 debug_object_init(timer, &hrtimer_debug_descr);
347                 return 1;
348         default:
349                 return 0;
350         }
351 }
352
353 /*
354  * fixup_activate is called when:
355  * - an active object is activated
356  * - an unknown object is activated (might be a statically initialized object)
357  */
358 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
359 {
360         switch (state) {
361
362         case ODEBUG_STATE_NOTAVAILABLE:
363                 WARN_ON_ONCE(1);
364                 return 0;
365
366         case ODEBUG_STATE_ACTIVE:
367                 WARN_ON(1);
368
369         default:
370                 return 0;
371         }
372 }
373
374 /*
375  * fixup_free is called when:
376  * - an active object is freed
377  */
378 static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
379 {
380         struct hrtimer *timer = addr;
381
382         switch (state) {
383         case ODEBUG_STATE_ACTIVE:
384                 hrtimer_cancel(timer);
385                 debug_object_free(timer, &hrtimer_debug_descr);
386                 return 1;
387         default:
388                 return 0;
389         }
390 }
391
392 static struct debug_obj_descr hrtimer_debug_descr = {
393         .name           = "hrtimer",
394         .debug_hint     = hrtimer_debug_hint,
395         .fixup_init     = hrtimer_fixup_init,
396         .fixup_activate = hrtimer_fixup_activate,
397         .fixup_free     = hrtimer_fixup_free,
398 };
399
400 static inline void debug_hrtimer_init(struct hrtimer *timer)
401 {
402         debug_object_init(timer, &hrtimer_debug_descr);
403 }
404
405 static inline void debug_hrtimer_activate(struct hrtimer *timer)
406 {
407         debug_object_activate(timer, &hrtimer_debug_descr);
408 }
409
410 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
411 {
412         debug_object_deactivate(timer, &hrtimer_debug_descr);
413 }
414
415 static inline void debug_hrtimer_free(struct hrtimer *timer)
416 {
417         debug_object_free(timer, &hrtimer_debug_descr);
418 }
419
420 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
421                            enum hrtimer_mode mode);
422
423 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
424                            enum hrtimer_mode mode)
425 {
426         debug_object_init_on_stack(timer, &hrtimer_debug_descr);
427         __hrtimer_init(timer, clock_id, mode);
428 }
429 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
430
431 void destroy_hrtimer_on_stack(struct hrtimer *timer)
432 {
433         debug_object_free(timer, &hrtimer_debug_descr);
434 }
435
436 #else
437 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
438 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
439 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
440 #endif
441
442 static inline void
443 debug_init(struct hrtimer *timer, clockid_t clockid,
444            enum hrtimer_mode mode)
445 {
446         debug_hrtimer_init(timer);
447         trace_hrtimer_init(timer, clockid, mode);
448 }
449
450 static inline void debug_activate(struct hrtimer *timer)
451 {
452         debug_hrtimer_activate(timer);
453         trace_hrtimer_start(timer);
454 }
455
456 static inline void debug_deactivate(struct hrtimer *timer)
457 {
458         debug_hrtimer_deactivate(timer);
459         trace_hrtimer_cancel(timer);
460 }
461
462 #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
463 static inline void hrtimer_update_next_timer(struct hrtimer_cpu_base *cpu_base,
464                                              struct hrtimer *timer)
465 {
466 #ifdef CONFIG_HIGH_RES_TIMERS
467         cpu_base->next_timer = timer;
468 #endif
469 }
470
471 static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
472 {
473         struct hrtimer_clock_base *base = cpu_base->clock_base;
474         ktime_t expires, expires_next = { .tv64 = KTIME_MAX };
475         unsigned int active = cpu_base->active_bases;
476
477         hrtimer_update_next_timer(cpu_base, NULL);
478         for (; active; base++, active >>= 1) {
479                 struct timerqueue_node *next;
480                 struct hrtimer *timer;
481
482                 if (!(active & 0x01))
483                         continue;
484
485                 next = timerqueue_getnext(&base->active);
486                 timer = container_of(next, struct hrtimer, node);
487                 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
488                 if (expires.tv64 < expires_next.tv64) {
489                         expires_next = expires;
490                         hrtimer_update_next_timer(cpu_base, timer);
491                 }
492         }
493         /*
494          * clock_was_set() might have changed base->offset of any of
495          * the clock bases so the result might be negative. Fix it up
496          * to prevent a false positive in clockevents_program_event().
497          */
498         if (expires_next.tv64 < 0)
499                 expires_next.tv64 = 0;
500         return expires_next;
501 }
502 #endif
503
504 static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
505 {
506         ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
507         ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
508         ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
509
510         return ktime_get_update_offsets_now(&base->clock_was_set_seq,
511                                             offs_real, offs_boot, offs_tai);
512 }
513
514 /* High resolution timer related functions */
515 #ifdef CONFIG_HIGH_RES_TIMERS
516
517 /*
518  * High resolution timer enabled ?
519  */
520 static int hrtimer_hres_enabled __read_mostly  = 1;
521 unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
522 EXPORT_SYMBOL_GPL(hrtimer_resolution);
523
524 /*
525  * Enable / Disable high resolution mode
526  */
527 static int __init setup_hrtimer_hres(char *str)
528 {
529         if (!strcmp(str, "off"))
530                 hrtimer_hres_enabled = 0;
531         else if (!strcmp(str, "on"))
532                 hrtimer_hres_enabled = 1;
533         else
534                 return 0;
535         return 1;
536 }
537
538 __setup("highres=", setup_hrtimer_hres);
539
540 /*
541  * hrtimer_high_res_enabled - query, if the highres mode is enabled
542  */
543 static inline int hrtimer_is_hres_enabled(void)
544 {
545         return hrtimer_hres_enabled;
546 }
547
548 /*
549  * Is the high resolution mode active ?
550  */
551 static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
552 {
553         return cpu_base->hres_active;
554 }
555
556 static inline int hrtimer_hres_active(void)
557 {
558         return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
559 }
560
561 /*
562  * Reprogram the event source with checking both queues for the
563  * next event
564  * Called with interrupts disabled and base->lock held
565  */
566 static void
567 hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
568 {
569         ktime_t expires_next;
570
571         if (!cpu_base->hres_active)
572                 return;
573
574         expires_next = __hrtimer_get_next_event(cpu_base);
575
576         if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
577                 return;
578
579         cpu_base->expires_next.tv64 = expires_next.tv64;
580
581         /*
582          * If a hang was detected in the last timer interrupt then we
583          * leave the hang delay active in the hardware. We want the
584          * system to make progress. That also prevents the following
585          * scenario:
586          * T1 expires 50ms from now
587          * T2 expires 5s from now
588          *
589          * T1 is removed, so this code is called and would reprogram
590          * the hardware to 5s from now. Any hrtimer_start after that
591          * will not reprogram the hardware due to hang_detected being
592          * set. So we'd effectivly block all timers until the T2 event
593          * fires.
594          */
595         if (cpu_base->hang_detected)
596                 return;
597
598         tick_program_event(cpu_base->expires_next, 1);
599 }
600
601 /*
602  * When a timer is enqueued and expires earlier than the already enqueued
603  * timers, we have to check, whether it expires earlier than the timer for
604  * which the clock event device was armed.
605  *
606  * Called with interrupts disabled and base->cpu_base.lock held
607  */
608 static void hrtimer_reprogram(struct hrtimer *timer,
609                               struct hrtimer_clock_base *base)
610 {
611         struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
612         ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
613
614         WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
615
616         /*
617          * If the timer is not on the current cpu, we cannot reprogram
618          * the other cpus clock event device.
619          */
620         if (base->cpu_base != cpu_base)
621                 return;
622
623         /*
624          * If the hrtimer interrupt is running, then it will
625          * reevaluate the clock bases and reprogram the clock event
626          * device. The callbacks are always executed in hard interrupt
627          * context so we don't need an extra check for a running
628          * callback.
629          */
630         if (cpu_base->in_hrtirq)
631                 return;
632
633         if (base->cpu_base != cpu_base)
634                 return 0;
635
636        if (cpu_base->in_hrtirq)
637                return 0;
638
639         /*
640          * CLOCK_REALTIME timer might be requested with an absolute
641          * expiry time which is less than base->offset. Set it to 0.
642          */
643         if (expires.tv64 < 0)
644                 expires.tv64 = 0;
645
646         if (expires.tv64 >= cpu_base->expires_next.tv64)
647                 return;
648
649         /* Update the pointer to the next expiring timer */
650         cpu_base->next_timer = timer;
651
652         /*
653          * If a hang was detected in the last timer interrupt then we
654          * do not schedule a timer which is earlier than the expiry
655          * which we enforced in the hang detection. We want the system
656          * to make progress.
657          */
658         if (cpu_base->hang_detected)
659                 return;
660
661         cpu_base->expires_next = expires;
662         /*
663          * Program the timer hardware. We enforce the expiry for
664          * events which are already in the past.
665          */
666         tick_program_event(expires, 1);
667 }
668
669 /*
670  * Initialize the high resolution related parts of cpu_base
671  */
672 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
673 {
674         base->expires_next.tv64 = KTIME_MAX;
675         base->hres_active = 0;
676 }
677
678 /*
679  * Retrigger next event is called after clock was set
680  *
681  * Called with interrupts disabled via on_each_cpu()
682  */
683 static void retrigger_next_event(void *arg)
684 {
685         struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
686
687         if (!base->hres_active)
688                 return;
689
690         raw_spin_lock(&base->lock);
691         hrtimer_update_base(base);
692         hrtimer_force_reprogram(base, 0);
693         raw_spin_unlock(&base->lock);
694 }
695
696 /*
697  * Switch to high resolution mode
698  */
699 static void hrtimer_switch_to_hres(void)
700 {
701         struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
702
703         if (tick_init_highres()) {
704                 printk(KERN_WARNING "Could not switch to high resolution "
705                                     "mode on CPU %d\n", base->cpu);
706                 return;
707         }
708         base->hres_active = 1;
709         hrtimer_resolution = HIGH_RES_NSEC;
710
711         tick_setup_sched_timer();
712         /* "Retrigger" the interrupt to get things going */
713         retrigger_next_event(NULL);
714 }
715
716 static void clock_was_set_work(struct work_struct *work)
717 {
718         clock_was_set();
719 }
720
721 static DECLARE_WORK(hrtimer_work, clock_was_set_work);
722
723 #ifdef CONFIG_PREEMPT_RT_FULL
724 /*
725  * RT can not call schedule_work from real interrupt context.
726  * Need to make a thread to do the real work.
727  */
728 static struct task_struct *clock_set_delay_thread;
729 static bool do_clock_set_delay;
730
731 static int run_clock_set_delay(void *ignore)
732 {
733         while (!kthread_should_stop()) {
734                 set_current_state(TASK_INTERRUPTIBLE);
735                 if (do_clock_set_delay) {
736                         do_clock_set_delay = false;
737                         schedule_work(&hrtimer_work);
738                 }
739                 schedule();
740         }
741         __set_current_state(TASK_RUNNING);
742         return 0;
743 }
744
745 void clock_was_set_delayed(void)
746 {
747         do_clock_set_delay = true;
748         /* Make visible before waking up process */
749         smp_wmb();
750         wake_up_process(clock_set_delay_thread);
751 }
752
753 static __init int create_clock_set_delay_thread(void)
754 {
755         clock_set_delay_thread = kthread_run(run_clock_set_delay, NULL, "kclksetdelayd");
756         BUG_ON(!clock_set_delay_thread);
757         return 0;
758 }
759 early_initcall(create_clock_set_delay_thread);
760 #else /* PREEMPT_RT_FULL */
761 /*
762  * Called from timekeeping and resume code to reprogramm the hrtimer
763  * interrupt device on all cpus.
764  */
765 void clock_was_set_delayed(void)
766 {
767         schedule_work(&hrtimer_work);
768 }
769 #endif
770
771 #else
772
773 static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *b) { return 0; }
774 static inline int hrtimer_hres_active(void) { return 0; }
775 static inline int hrtimer_is_hres_enabled(void) { return 0; }
776 static inline void hrtimer_switch_to_hres(void) { }
777 static inline void
778 hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
779 static inline void hrtimer_reprogram(struct hrtimer *timer,
780                                      struct hrtimer_clock_base *base) { }
781 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
782 static inline void retrigger_next_event(void *arg) { }
783
784 #endif /* CONFIG_HIGH_RES_TIMERS */
785
786 /*
787  * Clock realtime was set
788  *
789  * Change the offset of the realtime clock vs. the monotonic
790  * clock.
791  *
792  * We might have to reprogram the high resolution timer interrupt. On
793  * SMP we call the architecture specific code to retrigger _all_ high
794  * resolution timer interrupts. On UP we just disable interrupts and
795  * call the high resolution interrupt code.
796  */
797 void clock_was_set(void)
798 {
799 #ifdef CONFIG_HIGH_RES_TIMERS
800         /* Retrigger the CPU local events everywhere */
801         on_each_cpu(retrigger_next_event, NULL, 1);
802 #endif
803         timerfd_clock_was_set();
804 }
805
806 /*
807  * During resume we might have to reprogram the high resolution timer
808  * interrupt on all online CPUs.  However, all other CPUs will be
809  * stopped with IRQs interrupts disabled so the clock_was_set() call
810  * must be deferred.
811  */
812 void hrtimers_resume(void)
813 {
814         WARN_ONCE(!irqs_disabled(),
815                   KERN_INFO "hrtimers_resume() called with IRQs enabled!");
816
817         /* Retrigger on the local CPU */
818         retrigger_next_event(NULL);
819         /* And schedule a retrigger for all others */
820         clock_was_set_delayed();
821 }
822
823 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
824 {
825 #ifdef CONFIG_TIMER_STATS
826         if (timer->start_site)
827                 return;
828         timer->start_site = __builtin_return_address(0);
829         memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
830         timer->start_pid = current->pid;
831 #endif
832 }
833
834 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
835 {
836 #ifdef CONFIG_TIMER_STATS
837         timer->start_site = NULL;
838 #endif
839 }
840
841 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
842 {
843 #ifdef CONFIG_TIMER_STATS
844         if (likely(!timer_stats_active))
845                 return;
846         timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
847                                  timer->function, timer->start_comm, 0);
848 #endif
849 }
850
851 /*
852  * Counterpart to lock_hrtimer_base above:
853  */
854 static inline
855 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
856 {
857         raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
858 }
859
860 /**
861  * hrtimer_forward - forward the timer expiry
862  * @timer:      hrtimer to forward
863  * @now:        forward past this time
864  * @interval:   the interval to forward
865  *
866  * Forward the timer expiry so it will expire in the future.
867  * Returns the number of overruns.
868  *
869  * Can be safely called from the callback function of @timer. If
870  * called from other contexts @timer must neither be enqueued nor
871  * running the callback and the caller needs to take care of
872  * serialization.
873  *
874  * Note: This only updates the timer expiry value and does not requeue
875  * the timer.
876  */
877 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
878 {
879         u64 orun = 1;
880         ktime_t delta;
881
882         delta = ktime_sub(now, hrtimer_get_expires(timer));
883
884         if (delta.tv64 < 0)
885                 return 0;
886
887         if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
888                 return 0;
889
890         if (interval.tv64 < hrtimer_resolution)
891                 interval.tv64 = hrtimer_resolution;
892
893         if (unlikely(delta.tv64 >= interval.tv64)) {
894                 s64 incr = ktime_to_ns(interval);
895
896                 orun = ktime_divns(delta, incr);
897                 hrtimer_add_expires_ns(timer, incr * orun);
898                 if (hrtimer_get_expires_tv64(timer) > now.tv64)
899                         return orun;
900                 /*
901                  * This (and the ktime_add() below) is the
902                  * correction for exact:
903                  */
904                 orun++;
905         }
906         hrtimer_add_expires(timer, interval);
907
908         return orun;
909 }
910 EXPORT_SYMBOL_GPL(hrtimer_forward);
911
912 #ifdef CONFIG_PREEMPT_RT_BASE
913 # define wake_up_timer_waiters(b)       wake_up(&(b)->wait)
914
915 /**
916  * hrtimer_wait_for_timer - Wait for a running timer
917  *
918  * @timer:      timer to wait for
919  *
920  * The function waits in case the timers callback function is
921  * currently executed on the waitqueue of the timer base. The
922  * waitqueue is woken up after the timer callback function has
923  * finished execution.
924  */
925 void hrtimer_wait_for_timer(const struct hrtimer *timer)
926 {
927         struct hrtimer_clock_base *base = timer->base;
928
929         if (base && base->cpu_base && !timer->irqsafe)
930                 wait_event(base->cpu_base->wait,
931                                 !(hrtimer_callback_running(timer)));
932 }
933
934 #else
935 # define wake_up_timer_waiters(b)       do { } while (0)
936 #endif
937
938 /*
939  * enqueue_hrtimer - internal function to (re)start a timer
940  *
941  * The timer is inserted in expiry order. Insertion into the
942  * red black tree is O(log(n)). Must hold the base lock.
943  *
944  * Returns 1 when the new timer is the leftmost timer in the tree.
945  */
946 static int enqueue_hrtimer(struct hrtimer *timer,
947                            struct hrtimer_clock_base *base)
948 {
949         debug_activate(timer);
950
951         base->cpu_base->active_bases |= 1 << base->index;
952
953         timer->state = HRTIMER_STATE_ENQUEUED;
954
955         return timerqueue_add(&base->active, &timer->node);
956 }
957
958 /*
959  * __remove_hrtimer - internal function to remove a timer
960  *
961  * Caller must hold the base lock.
962  *
963  * High resolution timer mode reprograms the clock event device when the
964  * timer is the one which expires next. The caller can disable this by setting
965  * reprogram to zero. This is useful, when the context does a reprogramming
966  * anyway (e.g. timer interrupt)
967  */
968 static void __remove_hrtimer(struct hrtimer *timer,
969                              struct hrtimer_clock_base *base,
970                              u8 newstate, int reprogram)
971 {
972         struct hrtimer_cpu_base *cpu_base = base->cpu_base;
973         u8 state = timer->state;
974
975         timer->state = newstate;
976         if (!(state & HRTIMER_STATE_ENQUEUED))
977                 return;
978
979         if (unlikely(!list_empty(&timer->cb_entry))) {
980                 list_del_init(&timer->cb_entry);
981                 return;
982         }
983
984         if (!timerqueue_del(&base->active, &timer->node))
985                 cpu_base->active_bases &= ~(1 << base->index);
986
987 #ifdef CONFIG_HIGH_RES_TIMERS
988         /*
989          * Note: If reprogram is false we do not update
990          * cpu_base->next_timer. This happens when we remove the first
991          * timer on a remote cpu. No harm as we never dereference
992          * cpu_base->next_timer. So the worst thing what can happen is
993          * an superflous call to hrtimer_force_reprogram() on the
994          * remote cpu later on if the same timer gets enqueued again.
995          */
996         if (reprogram && timer == cpu_base->next_timer)
997                 hrtimer_force_reprogram(cpu_base, 1);
998 #endif
999 }
1000
1001 /*
1002  * remove hrtimer, called with base lock held
1003  */
1004 static inline int
1005 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart)
1006 {
1007         if (hrtimer_is_queued(timer)) {
1008                 u8 state = timer->state;
1009                 int reprogram;
1010
1011                 /*
1012                  * Remove the timer and force reprogramming when high
1013                  * resolution mode is active and the timer is on the current
1014                  * CPU. If we remove a timer on another CPU, reprogramming is
1015                  * skipped. The interrupt event on this CPU is fired and
1016                  * reprogramming happens in the interrupt handler. This is a
1017                  * rare case and less expensive than a smp call.
1018                  */
1019                 debug_deactivate(timer);
1020                 timer_stats_hrtimer_clear_start_info(timer);
1021                 reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
1022
1023                 if (!restart)
1024                         state = HRTIMER_STATE_INACTIVE;
1025
1026                 __remove_hrtimer(timer, base, state, reprogram);
1027                 return 1;
1028         }
1029         return 0;
1030 }
1031
1032 static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
1033                                             const enum hrtimer_mode mode)
1034 {
1035 #ifdef CONFIG_TIME_LOW_RES
1036         /*
1037          * CONFIG_TIME_LOW_RES indicates that the system has no way to return
1038          * granular time values. For relative timers we add hrtimer_resolution
1039          * (i.e. one jiffie) to prevent short timeouts.
1040          */
1041         timer->is_rel = mode & HRTIMER_MODE_REL;
1042         if (timer->is_rel)
1043                 tim = ktime_add_safe(tim, ktime_set(0, hrtimer_resolution));
1044 #endif
1045         return tim;
1046 }
1047
1048 /**
1049  * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1050  * @timer:      the timer to be added
1051  * @tim:        expiry time
1052  * @delta_ns:   "slack" range for the timer
1053  * @mode:       expiry mode: absolute (HRTIMER_MODE_ABS) or
1054  *              relative (HRTIMER_MODE_REL)
1055  */
1056 void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1057                             unsigned long delta_ns, const enum hrtimer_mode mode)
1058 {
1059         struct hrtimer_clock_base *base, *new_base;
1060         unsigned long flags;
1061         int leftmost;
1062
1063         base = lock_hrtimer_base(timer, &flags);
1064
1065         /* Remove an active timer from the queue: */
1066         remove_hrtimer(timer, base, true);
1067
1068         if (mode & HRTIMER_MODE_REL)
1069                 tim = ktime_add_safe(tim, base->get_time());
1070
1071         tim = hrtimer_update_lowres(timer, tim, mode);
1072
1073         hrtimer_set_expires_range_ns(timer, tim, delta_ns);
1074
1075         /* Switch the timer base, if necessary: */
1076         new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
1077
1078         timer_stats_hrtimer_set_start_info(timer);
1079 #ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
1080         {
1081                 ktime_t now = new_base->get_time();
1082
1083                 if (ktime_to_ns(tim) < ktime_to_ns(now))
1084                         timer->praecox = now;
1085                 else
1086                         timer->praecox = ktime_set(0, 0);
1087         }
1088 #endif
1089         leftmost = enqueue_hrtimer(timer, new_base);
1090         if (!leftmost)
1091                 goto unlock;
1092
1093         if (!hrtimer_is_hres_active(timer)) {
1094                 /*
1095                  * Kick to reschedule the next tick to handle the new timer
1096                  * on dynticks target.
1097                  */
1098                 if (new_base->cpu_base->nohz_active)
1099                         wake_up_nohz_cpu(new_base->cpu_base->cpu);
1100         } else {
1101                 hrtimer_reprogram(timer, new_base);
1102         }
1103 unlock:
1104         unlock_hrtimer_base(timer, &flags);
1105 }
1106 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1107
1108 /**
1109  * hrtimer_try_to_cancel - try to deactivate a timer
1110  * @timer:      hrtimer to stop
1111  *
1112  * Returns:
1113  *  0 when the timer was not active
1114  *  1 when the timer was active
1115  * -1 when the timer is currently excuting the callback function and
1116  *    cannot be stopped
1117  */
1118 int hrtimer_try_to_cancel(struct hrtimer *timer)
1119 {
1120         struct hrtimer_clock_base *base;
1121         unsigned long flags;
1122         int ret = -1;
1123
1124         /*
1125          * Check lockless first. If the timer is not active (neither
1126          * enqueued nor running the callback, nothing to do here.  The
1127          * base lock does not serialize against a concurrent enqueue,
1128          * so we can avoid taking it.
1129          */
1130         if (!hrtimer_active(timer))
1131                 return 0;
1132
1133         base = lock_hrtimer_base(timer, &flags);
1134
1135         if (!hrtimer_callback_running(timer))
1136                 ret = remove_hrtimer(timer, base, false);
1137
1138         unlock_hrtimer_base(timer, &flags);
1139
1140         return ret;
1141
1142 }
1143 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1144
1145 /**
1146  * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1147  * @timer:      the timer to be cancelled
1148  *
1149  * Returns:
1150  *  0 when the timer was not active
1151  *  1 when the timer was active
1152  */
1153 int hrtimer_cancel(struct hrtimer *timer)
1154 {
1155         for (;;) {
1156                 int ret = hrtimer_try_to_cancel(timer);
1157
1158                 if (ret >= 0)
1159                         return ret;
1160                 hrtimer_wait_for_timer(timer);
1161         }
1162 }
1163 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1164
1165 /**
1166  * hrtimer_get_remaining - get remaining time for the timer
1167  * @timer:      the timer to read
1168  * @adjust:     adjust relative timers when CONFIG_TIME_LOW_RES=y
1169  */
1170 ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
1171 {
1172         unsigned long flags;
1173         ktime_t rem;
1174
1175         lock_hrtimer_base(timer, &flags);
1176         if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1177                 rem = hrtimer_expires_remaining_adjusted(timer);
1178         else
1179                 rem = hrtimer_expires_remaining(timer);
1180         unlock_hrtimer_base(timer, &flags);
1181
1182         return rem;
1183 }
1184 EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
1185
1186 #ifdef CONFIG_NO_HZ_COMMON
1187 /**
1188  * hrtimer_get_next_event - get the time until next expiry event
1189  *
1190  * Returns the next expiry time or KTIME_MAX if no timer is pending.
1191  */
1192 u64 hrtimer_get_next_event(void)
1193 {
1194         struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1195         u64 expires = KTIME_MAX;
1196         unsigned long flags;
1197
1198         raw_spin_lock_irqsave(&cpu_base->lock, flags);
1199
1200         if (!__hrtimer_hres_active(cpu_base))
1201                 expires = __hrtimer_get_next_event(cpu_base).tv64;
1202
1203         raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1204
1205         return expires;
1206 }
1207 #endif
1208
1209 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1210                            enum hrtimer_mode mode)
1211 {
1212         struct hrtimer_cpu_base *cpu_base;
1213         int base;
1214
1215         memset(timer, 0, sizeof(struct hrtimer));
1216
1217         cpu_base = raw_cpu_ptr(&hrtimer_bases);
1218
1219         if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1220                 clock_id = CLOCK_MONOTONIC;
1221
1222         base = hrtimer_clockid_to_base(clock_id);
1223         timer->base = &cpu_base->clock_base[base];
1224         INIT_LIST_HEAD(&timer->cb_entry);
1225         timerqueue_init(&timer->node);
1226
1227 #ifdef CONFIG_TIMER_STATS
1228         timer->start_site = NULL;
1229         timer->start_pid = -1;
1230         memset(timer->start_comm, 0, TASK_COMM_LEN);
1231 #endif
1232 }
1233
1234 /**
1235  * hrtimer_init - initialize a timer to the given clock
1236  * @timer:      the timer to be initialized
1237  * @clock_id:   the clock to be used
1238  * @mode:       timer mode abs/rel
1239  */
1240 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1241                   enum hrtimer_mode mode)
1242 {
1243         debug_init(timer, clock_id, mode);
1244         __hrtimer_init(timer, clock_id, mode);
1245 }
1246 EXPORT_SYMBOL_GPL(hrtimer_init);
1247
1248 /*
1249  * A timer is active, when it is enqueued into the rbtree or the
1250  * callback function is running or it's in the state of being migrated
1251  * to another cpu.
1252  *
1253  * It is important for this function to not return a false negative.
1254  */
1255 bool hrtimer_active(const struct hrtimer *timer)
1256 {
1257         struct hrtimer_cpu_base *cpu_base;
1258         unsigned int seq;
1259
1260         do {
1261                 cpu_base = READ_ONCE(timer->base->cpu_base);
1262                 seq = raw_read_seqcount_begin(&cpu_base->seq);
1263
1264                 if (timer->state != HRTIMER_STATE_INACTIVE ||
1265                     cpu_base->running_soft == timer ||
1266                     cpu_base->running == timer)
1267                         return true;
1268
1269         } while (read_seqcount_retry(&cpu_base->seq, seq) ||
1270                  cpu_base != READ_ONCE(timer->base->cpu_base));
1271
1272         return false;
1273 }
1274 EXPORT_SYMBOL_GPL(hrtimer_active);
1275
1276 /*
1277  * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1278  * distinct sections:
1279  *
1280  *  - queued:   the timer is queued
1281  *  - callback: the timer is being ran
1282  *  - post:     the timer is inactive or (re)queued
1283  *
1284  * On the read side we ensure we observe timer->state and cpu_base->running
1285  * from the same section, if anything changed while we looked at it, we retry.
1286  * This includes timer->base changing because sequence numbers alone are
1287  * insufficient for that.
1288  *
1289  * The sequence numbers are required because otherwise we could still observe
1290  * a false negative if the read side got smeared over multiple consequtive
1291  * __run_hrtimer() invocations.
1292  */
1293
1294 static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
1295                           struct hrtimer_clock_base *base,
1296                           struct hrtimer *timer, ktime_t *now)
1297 {
1298         enum hrtimer_restart (*fn)(struct hrtimer *);
1299         int restart;
1300
1301         lockdep_assert_held(&cpu_base->lock);
1302
1303         debug_deactivate(timer);
1304         cpu_base->running = timer;
1305
1306         /*
1307          * Separate the ->running assignment from the ->state assignment.
1308          *
1309          * As with a regular write barrier, this ensures the read side in
1310          * hrtimer_active() cannot observe cpu_base->running == NULL &&
1311          * timer->state == INACTIVE.
1312          */
1313         raw_write_seqcount_barrier(&cpu_base->seq);
1314
1315         __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
1316         timer_stats_account_hrtimer(timer);
1317         fn = timer->function;
1318
1319         /*
1320          * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
1321          * timer is restarted with a period then it becomes an absolute
1322          * timer. If its not restarted it does not matter.
1323          */
1324         if (IS_ENABLED(CONFIG_TIME_LOW_RES))
1325                 timer->is_rel = false;
1326
1327         /*
1328          * Because we run timers from hardirq context, there is no chance
1329          * they get migrated to another cpu, therefore its safe to unlock
1330          * the timer base.
1331          */
1332         raw_spin_unlock(&cpu_base->lock);
1333         trace_hrtimer_expire_entry(timer, now);
1334         restart = fn(timer);
1335         trace_hrtimer_expire_exit(timer);
1336         raw_spin_lock(&cpu_base->lock);
1337
1338         /*
1339          * Note: We clear the running state after enqueue_hrtimer and
1340          * we do not reprogramm the event hardware. Happens either in
1341          * hrtimer_start_range_ns() or in hrtimer_interrupt()
1342          *
1343          * Note: Because we dropped the cpu_base->lock above,
1344          * hrtimer_start_range_ns() can have popped in and enqueued the timer
1345          * for us already.
1346          */
1347         if (restart != HRTIMER_NORESTART &&
1348             !(timer->state & HRTIMER_STATE_ENQUEUED))
1349                 enqueue_hrtimer(timer, base);
1350
1351         /*
1352          * Separate the ->running assignment from the ->state assignment.
1353          *
1354          * As with a regular write barrier, this ensures the read side in
1355          * hrtimer_active() cannot observe cpu_base->running == NULL &&
1356          * timer->state == INACTIVE.
1357          */
1358         raw_write_seqcount_barrier(&cpu_base->seq);
1359
1360         WARN_ON_ONCE(cpu_base->running != timer);
1361         cpu_base->running = NULL;
1362 }
1363
1364 #ifdef CONFIG_PREEMPT_RT_BASE
1365 static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
1366                                  struct hrtimer_clock_base *base)
1367 {
1368         int leftmost;
1369
1370         if (restart != HRTIMER_NORESTART &&
1371             !(timer->state & HRTIMER_STATE_ENQUEUED)) {
1372
1373                 leftmost = enqueue_hrtimer(timer, base);
1374                 if (!leftmost)
1375                         return;
1376 #ifdef CONFIG_HIGH_RES_TIMERS
1377                 if (!hrtimer_is_hres_active(timer)) {
1378                         /*
1379                          * Kick to reschedule the next tick to handle the new timer
1380                          * on dynticks target.
1381                          */
1382                         if (base->cpu_base->nohz_active)
1383                                 wake_up_nohz_cpu(base->cpu_base->cpu);
1384                 } else {
1385
1386                         hrtimer_reprogram(timer, base);
1387                 }
1388 #endif
1389         }
1390 }
1391
1392 /*
1393  * The changes in mainline which removed the callback modes from
1394  * hrtimer are not yet working with -rt. The non wakeup_process()
1395  * based callbacks which involve sleeping locks need to be treated
1396  * seperately.
1397  */
1398 static void hrtimer_rt_run_pending(void)
1399 {
1400         enum hrtimer_restart (*fn)(struct hrtimer *);
1401         struct hrtimer_cpu_base *cpu_base;
1402         struct hrtimer_clock_base *base;
1403         struct hrtimer *timer;
1404         int index, restart;
1405
1406         local_irq_disable();
1407         cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
1408
1409         raw_spin_lock(&cpu_base->lock);
1410
1411         for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1412                 base = &cpu_base->clock_base[index];
1413
1414                 while (!list_empty(&base->expired)) {
1415                         timer = list_first_entry(&base->expired,
1416                                                  struct hrtimer, cb_entry);
1417
1418                         /*
1419                          * Same as the above __run_hrtimer function
1420                          * just we run with interrupts enabled.
1421                          */
1422                         debug_deactivate(timer);
1423                         cpu_base->running_soft = timer;
1424                         raw_write_seqcount_barrier(&cpu_base->seq);
1425
1426                         __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
1427                         timer_stats_account_hrtimer(timer);
1428                         fn = timer->function;
1429
1430                         raw_spin_unlock_irq(&cpu_base->lock);
1431                         restart = fn(timer);
1432                         raw_spin_lock_irq(&cpu_base->lock);
1433
1434                         hrtimer_rt_reprogram(restart, timer, base);
1435                         raw_write_seqcount_barrier(&cpu_base->seq);
1436
1437                         WARN_ON_ONCE(cpu_base->running_soft != timer);
1438                         cpu_base->running_soft = NULL;
1439                 }
1440         }
1441
1442         raw_spin_unlock_irq(&cpu_base->lock);
1443
1444         wake_up_timer_waiters(cpu_base);
1445 }
1446
1447 static int hrtimer_rt_defer(struct hrtimer *timer)
1448 {
1449         if (timer->irqsafe)
1450                 return 0;
1451
1452         __remove_hrtimer(timer, timer->base, timer->state, 0);
1453         list_add_tail(&timer->cb_entry, &timer->base->expired);
1454         return 1;
1455 }
1456
1457 #else
1458
1459 static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
1460
1461 #endif
1462
1463 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
1464
1465 static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
1466 {
1467         struct hrtimer_clock_base *base = cpu_base->clock_base;
1468         unsigned int active = cpu_base->active_bases;
1469         int raise = 0;
1470
1471         for (; active; base++, active >>= 1) {
1472                 struct timerqueue_node *node;
1473                 ktime_t basenow;
1474
1475                 if (!(active & 0x01))
1476                         continue;
1477
1478                 basenow = ktime_add(now, base->offset);
1479
1480                 while ((node = timerqueue_getnext(&base->active))) {
1481                         struct hrtimer *timer;
1482
1483                         timer = container_of(node, struct hrtimer, node);
1484
1485                         trace_hrtimer_interrupt(raw_smp_processor_id(),
1486                             ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
1487                                 timer->praecox : hrtimer_get_expires(timer),
1488                                 basenow)),
1489                             current,
1490                             timer->function == hrtimer_wakeup ?
1491                             container_of(timer, struct hrtimer_sleeper,
1492                                 timer)->task : NULL);
1493
1494                         /*
1495                          * The immediate goal for using the softexpires is
1496                          * minimizing wakeups, not running timers at the
1497                          * earliest interrupt after their soft expiration.
1498                          * This allows us to avoid using a Priority Search
1499                          * Tree, which can answer a stabbing querry for
1500                          * overlapping intervals and instead use the simple
1501                          * BST we already have.
1502                          * We don't add extra wakeups by delaying timers that
1503                          * are right-of a not yet expired timer, because that
1504                          * timer will have to trigger a wakeup anyway.
1505                          */
1506                         if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
1507                                 break;
1508
1509                         if (!hrtimer_rt_defer(timer))
1510                                 __run_hrtimer(cpu_base, base, timer, &basenow);
1511                         else
1512                                 raise = 1;
1513                 }
1514         }
1515         if (raise)
1516                 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1517 }
1518
1519 #ifdef CONFIG_HIGH_RES_TIMERS
1520
1521 /*
1522  * High resolution timer interrupt
1523  * Called with interrupts disabled
1524  */
1525 void hrtimer_interrupt(struct clock_event_device *dev)
1526 {
1527         struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1528         ktime_t expires_next, now, entry_time, delta;
1529         int retries = 0;
1530
1531         BUG_ON(!cpu_base->hres_active);
1532         cpu_base->nr_events++;
1533         dev->next_event.tv64 = KTIME_MAX;
1534
1535         raw_spin_lock(&cpu_base->lock);
1536         entry_time = now = hrtimer_update_base(cpu_base);
1537 retry:
1538         cpu_base->in_hrtirq = 1;
1539         /*
1540          * We set expires_next to KTIME_MAX here with cpu_base->lock
1541          * held to prevent that a timer is enqueued in our queue via
1542          * the migration code. This does not affect enqueueing of
1543          * timers which run their callback and need to be requeued on
1544          * this CPU.
1545          */
1546         cpu_base->expires_next.tv64 = KTIME_MAX;
1547
1548         __hrtimer_run_queues(cpu_base, now);
1549
1550         /* Reevaluate the clock bases for the next expiry */
1551         expires_next = __hrtimer_get_next_event(cpu_base);
1552         /*
1553          * Store the new expiry value so the migration code can verify
1554          * against it.
1555          */
1556         cpu_base->expires_next = expires_next;
1557         cpu_base->in_hrtirq = 0;
1558         raw_spin_unlock(&cpu_base->lock);
1559
1560         /* Reprogramming necessary ? */
1561         if (!tick_program_event(expires_next, 0)) {
1562                 cpu_base->hang_detected = 0;
1563                 return;
1564         }
1565
1566         /*
1567          * The next timer was already expired due to:
1568          * - tracing
1569          * - long lasting callbacks
1570          * - being scheduled away when running in a VM
1571          *
1572          * We need to prevent that we loop forever in the hrtimer
1573          * interrupt routine. We give it 3 attempts to avoid
1574          * overreacting on some spurious event.
1575          *
1576          * Acquire base lock for updating the offsets and retrieving
1577          * the current time.
1578          */
1579         raw_spin_lock(&cpu_base->lock);
1580         now = hrtimer_update_base(cpu_base);
1581         cpu_base->nr_retries++;
1582         if (++retries < 3)
1583                 goto retry;
1584         /*
1585          * Give the system a chance to do something else than looping
1586          * here. We stored the entry time, so we know exactly how long
1587          * we spent here. We schedule the next event this amount of
1588          * time away.
1589          */
1590         cpu_base->nr_hangs++;
1591         cpu_base->hang_detected = 1;
1592         raw_spin_unlock(&cpu_base->lock);
1593         delta = ktime_sub(now, entry_time);
1594         if ((unsigned int)delta.tv64 > cpu_base->max_hang_time)
1595                 cpu_base->max_hang_time = (unsigned int) delta.tv64;
1596         /*
1597          * Limit it to a sensible value as we enforce a longer
1598          * delay. Give the CPU at least 100ms to catch up.
1599          */
1600         if (delta.tv64 > 100 * NSEC_PER_MSEC)
1601                 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1602         else
1603                 expires_next = ktime_add(now, delta);
1604         tick_program_event(expires_next, 1);
1605         printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1606                     ktime_to_ns(delta));
1607 }
1608
1609 /*
1610  * local version of hrtimer_peek_ahead_timers() called with interrupts
1611  * disabled.
1612  */
1613 static inline void __hrtimer_peek_ahead_timers(void)
1614 {
1615         struct tick_device *td;
1616
1617         if (!hrtimer_hres_active())
1618                 return;
1619
1620         td = this_cpu_ptr(&tick_cpu_device);
1621         if (td && td->evtdev)
1622                 hrtimer_interrupt(td->evtdev);
1623 }
1624
1625 #else /* CONFIG_HIGH_RES_TIMERS */
1626
1627 static inline void __hrtimer_peek_ahead_timers(void) { }
1628
1629 #endif  /* !CONFIG_HIGH_RES_TIMERS */
1630
1631 /*
1632  * Called from run_local_timers in hardirq context every jiffy
1633  */
1634 void hrtimer_run_queues(void)
1635 {
1636         struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1637         ktime_t now;
1638
1639         if (__hrtimer_hres_active(cpu_base))
1640                 return;
1641
1642         /*
1643          * This _is_ ugly: We have to check periodically, whether we
1644          * can switch to highres and / or nohz mode. The clocksource
1645          * switch happens with xtime_lock held. Notification from
1646          * there only sets the check bit in the tick_oneshot code,
1647          * otherwise we might deadlock vs. xtime_lock.
1648          */
1649         if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
1650                 hrtimer_switch_to_hres();
1651                 return;
1652         }
1653
1654         raw_spin_lock(&cpu_base->lock);
1655         now = hrtimer_update_base(cpu_base);
1656         __hrtimer_run_queues(cpu_base, now);
1657         raw_spin_unlock(&cpu_base->lock);
1658 }
1659
1660 /*
1661  * Sleep related functions:
1662  */
1663 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1664 {
1665         struct hrtimer_sleeper *t =
1666                 container_of(timer, struct hrtimer_sleeper, timer);
1667         struct task_struct *task = t->task;
1668
1669         t->task = NULL;
1670         if (task)
1671                 wake_up_process(task);
1672
1673         return HRTIMER_NORESTART;
1674 }
1675
1676 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1677 {
1678         sl->timer.function = hrtimer_wakeup;
1679         sl->timer.irqsafe = 1;
1680         sl->task = task;
1681 }
1682 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
1683
1684 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
1685                                 unsigned long state)
1686 {
1687         hrtimer_init_sleeper(t, current);
1688
1689         do {
1690                 set_current_state(state);
1691                 hrtimer_start_expires(&t->timer, mode);
1692
1693                 if (likely(t->task))
1694                         freezable_schedule();
1695
1696                 hrtimer_cancel(&t->timer);
1697                 mode = HRTIMER_MODE_ABS;
1698
1699         } while (t->task && !signal_pending(current));
1700
1701         __set_current_state(TASK_RUNNING);
1702
1703         return t->task == NULL;
1704 }
1705
1706 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1707 {
1708         struct timespec rmt;
1709         ktime_t rem;
1710
1711         rem = hrtimer_expires_remaining(timer);
1712         if (rem.tv64 <= 0)
1713                 return 0;
1714         rmt = ktime_to_timespec(rem);
1715
1716         if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1717                 return -EFAULT;
1718
1719         return 1;
1720 }
1721
1722 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1723 {
1724         struct hrtimer_sleeper t;
1725         struct timespec __user  *rmtp;
1726         int ret = 0;
1727
1728         hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
1729                                 HRTIMER_MODE_ABS);
1730         hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1731
1732         /* cpu_chill() does not care about restart state. */
1733         if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
1734                 goto out;
1735
1736         rmtp = restart->nanosleep.rmtp;
1737         if (rmtp) {
1738                 ret = update_rmtp(&t.timer, rmtp);
1739                 if (ret <= 0)
1740                         goto out;
1741         }
1742
1743         /* The other values in restart are already filled in */
1744         ret = -ERESTART_RESTARTBLOCK;
1745 out:
1746         destroy_hrtimer_on_stack(&t.timer);
1747         return ret;
1748 }
1749
1750 static long
1751 __hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1752                     const enum hrtimer_mode mode, const clockid_t clockid,
1753                     unsigned long state)
1754 {
1755         struct restart_block *restart;
1756         struct hrtimer_sleeper t;
1757         int ret = 0;
1758         unsigned long slack;
1759
1760         slack = current->timer_slack_ns;
1761         if (dl_task(current) || rt_task(current))
1762                 slack = 0;
1763
1764         hrtimer_init_on_stack(&t.timer, clockid, mode);
1765         hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1766         if (do_nanosleep(&t, mode, state))
1767                 goto out;
1768
1769         /* Absolute timers do not update the rmtp value and restart: */
1770         if (mode == HRTIMER_MODE_ABS) {
1771                 ret = -ERESTARTNOHAND;
1772                 goto out;
1773         }
1774
1775         if (rmtp) {
1776                 ret = update_rmtp(&t.timer, rmtp);
1777                 if (ret <= 0)
1778                         goto out;
1779         }
1780
1781         restart = &current->restart_block;
1782         restart->fn = hrtimer_nanosleep_restart;
1783         restart->nanosleep.clockid = t.timer.base->clockid;
1784         restart->nanosleep.rmtp = rmtp;
1785         restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1786
1787         ret = -ERESTART_RESTARTBLOCK;
1788 out:
1789         destroy_hrtimer_on_stack(&t.timer);
1790         return ret;
1791 }
1792
1793 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1794                        const enum hrtimer_mode mode, const clockid_t clockid)
1795 {
1796         return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
1797 }
1798
1799 SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1800                 struct timespec __user *, rmtp)
1801 {
1802         struct timespec tu;
1803
1804         if (copy_from_user(&tu, rqtp, sizeof(tu)))
1805                 return -EFAULT;
1806
1807         if (!timespec_valid(&tu))
1808                 return -EINVAL;
1809
1810         return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1811 }
1812
1813 #ifdef CONFIG_PREEMPT_RT_FULL
1814 /*
1815  * Sleep for 1 ms in hope whoever holds what we want will let it go.
1816  */
1817 void cpu_chill(void)
1818 {
1819         struct timespec tu = {
1820                 .tv_nsec = NSEC_PER_MSEC,
1821         };
1822         unsigned int freeze_flag = current->flags & PF_NOFREEZE;
1823
1824         current->flags |= PF_NOFREEZE;
1825         __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
1826                             TASK_UNINTERRUPTIBLE);
1827         if (!freeze_flag)
1828                 current->flags &= ~PF_NOFREEZE;
1829 }
1830 EXPORT_SYMBOL(cpu_chill);
1831 #endif
1832
1833 /*
1834  * Functions related to boot-time initialization:
1835  */
1836 static void init_hrtimers_cpu(int cpu)
1837 {
1838         struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1839         int i;
1840
1841         for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1842                 cpu_base->clock_base[i].cpu_base = cpu_base;
1843                 timerqueue_init_head(&cpu_base->clock_base[i].active);
1844                 INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
1845         }
1846
1847         cpu_base->cpu = cpu;
1848         hrtimer_init_hres(cpu_base);
1849 #ifdef CONFIG_PREEMPT_RT_BASE
1850         init_waitqueue_head(&cpu_base->wait);
1851 #endif
1852 }
1853
1854 #ifdef CONFIG_HOTPLUG_CPU
1855
1856 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1857                                 struct hrtimer_clock_base *new_base)
1858 {
1859         struct hrtimer *timer;
1860         struct timerqueue_node *node;
1861
1862         while ((node = timerqueue_getnext(&old_base->active))) {
1863                 timer = container_of(node, struct hrtimer, node);
1864                 BUG_ON(hrtimer_callback_running(timer));
1865                 debug_deactivate(timer);
1866
1867                 /*
1868                  * Mark it as ENQUEUED not INACTIVE otherwise the
1869                  * timer could be seen as !active and just vanish away
1870                  * under us on another CPU
1871                  */
1872                 __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
1873                 timer->base = new_base;
1874                 /*
1875                  * Enqueue the timers on the new cpu. This does not
1876                  * reprogram the event device in case the timer
1877                  * expires before the earliest on this CPU, but we run
1878                  * hrtimer_interrupt after we migrated everything to
1879                  * sort out already expired timers and reprogram the
1880                  * event device.
1881                  */
1882                 enqueue_hrtimer(timer, new_base);
1883         }
1884 }
1885
1886 static void migrate_hrtimers(int scpu)
1887 {
1888         struct hrtimer_cpu_base *old_base, *new_base;
1889         int i;
1890
1891         BUG_ON(cpu_online(scpu));
1892         tick_cancel_sched_timer(scpu);
1893
1894         local_irq_disable();
1895         old_base = &per_cpu(hrtimer_bases, scpu);
1896         new_base = this_cpu_ptr(&hrtimer_bases);
1897         /*
1898          * The caller is globally serialized and nobody else
1899          * takes two locks at once, deadlock is not possible.
1900          */
1901         raw_spin_lock(&new_base->lock);
1902         raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1903
1904         for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1905                 migrate_hrtimer_list(&old_base->clock_base[i],
1906                                      &new_base->clock_base[i]);
1907         }
1908
1909         raw_spin_unlock(&old_base->lock);
1910         raw_spin_unlock(&new_base->lock);
1911
1912         /* Check, if we got expired work to do */
1913         __hrtimer_peek_ahead_timers();
1914         local_irq_enable();
1915 }
1916
1917 #endif /* CONFIG_HOTPLUG_CPU */
1918
1919 static int hrtimer_cpu_notify(struct notifier_block *self,
1920                                         unsigned long action, void *hcpu)
1921 {
1922         int scpu = (long)hcpu;
1923
1924         switch (action) {
1925
1926         case CPU_UP_PREPARE:
1927         case CPU_UP_PREPARE_FROZEN:
1928                 init_hrtimers_cpu(scpu);
1929                 break;
1930
1931 #ifdef CONFIG_HOTPLUG_CPU
1932         case CPU_DEAD:
1933         case CPU_DEAD_FROZEN:
1934                 migrate_hrtimers(scpu);
1935                 break;
1936 #endif
1937
1938         default:
1939                 break;
1940         }
1941
1942         return NOTIFY_OK;
1943 }
1944
1945 static struct notifier_block hrtimers_nb = {
1946         .notifier_call = hrtimer_cpu_notify,
1947 };
1948
1949 #ifdef CONFIG_PREEMPT_RT_BASE
1950 static void run_hrtimer_softirq(struct softirq_action *h)
1951 {
1952         hrtimer_rt_run_pending();
1953 }
1954 #endif
1955
1956 void __init hrtimers_init(void)
1957 {
1958         hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1959                           (void *)(long)smp_processor_id());
1960         register_cpu_notifier(&hrtimers_nb);
1961 #ifdef CONFIG_PREEMPT_RT_BASE
1962         open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1963 #endif
1964 }
1965
1966 /**
1967  * schedule_hrtimeout_range_clock - sleep until timeout
1968  * @expires:    timeout value (ktime_t)
1969  * @delta:      slack in expires timeout (ktime_t)
1970  * @mode:       timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1971  * @clock:      timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1972  */
1973 int __sched
1974 schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1975                                const enum hrtimer_mode mode, int clock)
1976 {
1977         struct hrtimer_sleeper t;
1978
1979         /*
1980          * Optimize when a zero timeout value is given. It does not
1981          * matter whether this is an absolute or a relative time.
1982          */
1983         if (expires && !expires->tv64) {
1984                 __set_current_state(TASK_RUNNING);
1985                 return 0;
1986         }
1987
1988         /*
1989          * A NULL parameter means "infinite"
1990          */
1991         if (!expires) {
1992                 schedule();
1993                 return -EINTR;
1994         }
1995
1996         hrtimer_init_on_stack(&t.timer, clock, mode);
1997         hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1998
1999         hrtimer_init_sleeper(&t, current);
2000
2001         hrtimer_start_expires(&t.timer, mode);
2002
2003         if (likely(t.task))
2004                 schedule();
2005
2006         hrtimer_cancel(&t.timer);
2007         destroy_hrtimer_on_stack(&t.timer);
2008
2009         __set_current_state(TASK_RUNNING);
2010
2011         return !t.task ? 0 : -EINTR;
2012 }
2013
2014 /**
2015  * schedule_hrtimeout_range - sleep until timeout
2016  * @expires:    timeout value (ktime_t)
2017  * @delta:      slack in expires timeout (ktime_t)
2018  * @mode:       timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
2019  *
2020  * Make the current task sleep until the given expiry time has
2021  * elapsed. The routine will return immediately unless
2022  * the current task state has been set (see set_current_state()).
2023  *
2024  * The @delta argument gives the kernel the freedom to schedule the
2025  * actual wakeup to a time that is both power and performance friendly.
2026  * The kernel give the normal best effort behavior for "@expires+@delta",
2027  * but may decide to fire the timer earlier, but no earlier than @expires.
2028  *
2029  * You can set the task state as follows -
2030  *
2031  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
2032  * pass before the routine returns.
2033  *
2034  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
2035  * delivered to the current task.
2036  *
2037  * The current task state is guaranteed to be TASK_RUNNING when this
2038  * routine returns.
2039  *
2040  * Returns 0 when the timer has expired otherwise -EINTR
2041  */
2042 int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
2043                                      const enum hrtimer_mode mode)
2044 {
2045         return schedule_hrtimeout_range_clock(expires, delta, mode,
2046                                               CLOCK_MONOTONIC);
2047 }
2048 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
2049
2050 /**
2051  * schedule_hrtimeout - sleep until timeout
2052  * @expires:    timeout value (ktime_t)
2053  * @mode:       timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
2054  *
2055  * Make the current task sleep until the given expiry time has
2056  * elapsed. The routine will return immediately unless
2057  * the current task state has been set (see set_current_state()).
2058  *
2059  * You can set the task state as follows -
2060  *
2061  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
2062  * pass before the routine returns.
2063  *
2064  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
2065  * delivered to the current task.
2066  *
2067  * The current task state is guaranteed to be TASK_RUNNING when this
2068  * routine returns.
2069  *
2070  * Returns 0 when the timer has expired otherwise -EINTR
2071  */
2072 int __sched schedule_hrtimeout(ktime_t *expires,
2073                                const enum hrtimer_mode mode)
2074 {
2075         return schedule_hrtimeout_range(expires, 0, mode);
2076 }
2077 EXPORT_SYMBOL_GPL(schedule_hrtimeout);