2 * linux/kernel/hrtimer.c
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
8 * High-resolution kernel timers
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.
14 * These timers are currently used for:
18 * - precise in-kernel timing
20 * Started by: Thomas Gleixner and Ingo Molnar
23 * based on kernel/timer.c
25 * Help, testing, suggestions, bugfixes, improvements were
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
31 * For licencing details see kernel-base/COPYING
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/freezer.h>
53 #include <asm/uaccess.h>
55 #include <trace/events/timer.h>
60 * There are more clockids then hrtimer bases. Thus, we index
61 * into the timer bases by the hrtimer_base_type enum. When trying
62 * to reach a base using a clockid, hrtimer_clockid_to_base()
63 * is used to convert from clockid to the proper hrtimer_base_type.
65 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
68 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
72 .index = HRTIMER_BASE_MONOTONIC,
73 .clockid = CLOCK_MONOTONIC,
74 .get_time = &ktime_get,
75 .resolution = KTIME_LOW_RES,
78 .index = HRTIMER_BASE_REALTIME,
79 .clockid = CLOCK_REALTIME,
80 .get_time = &ktime_get_real,
81 .resolution = KTIME_LOW_RES,
84 .index = HRTIMER_BASE_BOOTTIME,
85 .clockid = CLOCK_BOOTTIME,
86 .get_time = &ktime_get_boottime,
87 .resolution = KTIME_LOW_RES,
90 .index = HRTIMER_BASE_TAI,
92 .get_time = &ktime_get_clocktai,
93 .resolution = KTIME_LOW_RES,
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,
105 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
107 return hrtimer_clock_to_base_table[clock_id];
112 * Get the coarse grained time at the softirq based on xtime and
115 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
117 ktime_t xtim, mono, boot, tai;
118 ktime_t off_real, off_boot, off_tai;
120 mono = ktime_get_update_offsets_tick(&off_real, &off_boot, &off_tai);
121 boot = ktime_add(mono, off_boot);
122 xtim = ktime_add(mono, off_real);
123 tai = ktime_add(xtim, off_tai);
125 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
126 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
127 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
128 base->clock_base[HRTIMER_BASE_TAI].softirq_time = tai;
132 * Functions and macros which are different for UP/SMP systems are kept in a
138 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
139 * means that all timers which are tied to this base via timer->base are
140 * locked, and the base itself is locked too.
142 * So __run_timers/migrate_timers can safely modify all timers which could
143 * be found on the lists/queues.
145 * When the timer's base is locked, and the timer removed from list, it is
146 * possible to set timer->base = NULL and drop the lock: the timer remains
150 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
151 unsigned long *flags)
153 struct hrtimer_clock_base *base;
157 if (likely(base != NULL)) {
158 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
159 if (likely(base == timer->base))
161 /* The timer has migrated to another CPU: */
162 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
169 * With HIGHRES=y we do not migrate the timer when it is expiring
170 * before the next event on the target cpu because we cannot reprogram
171 * the target cpu hardware and we would cause it to fire late.
173 * Called with cpu_base->lock of target cpu held.
176 hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
178 #ifdef CONFIG_HIGH_RES_TIMERS
181 if (!new_base->cpu_base->hres_active)
184 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
185 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
192 * Switch the timer base to the current CPU when possible.
194 static inline struct hrtimer_clock_base *
195 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
198 struct hrtimer_clock_base *new_base;
199 struct hrtimer_cpu_base *new_cpu_base;
200 int this_cpu = smp_processor_id();
201 int cpu = get_nohz_timer_target(pinned);
202 int basenum = base->index;
205 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
206 new_base = &new_cpu_base->clock_base[basenum];
208 if (base != new_base) {
210 * We are trying to move timer to new_base.
211 * However we can't change timer's base while it is running,
212 * so we keep it on the same CPU. No hassle vs. reprogramming
213 * the event source in the high resolution case. The softirq
214 * code will take care of this when the timer function has
215 * completed. There is no conflict as we hold the lock until
216 * the timer is enqueued.
218 if (unlikely(hrtimer_callback_running(timer)))
221 /* See the comment in lock_timer_base() */
223 raw_spin_unlock(&base->cpu_base->lock);
224 raw_spin_lock(&new_base->cpu_base->lock);
226 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
228 raw_spin_unlock(&new_base->cpu_base->lock);
229 raw_spin_lock(&base->cpu_base->lock);
233 timer->base = new_base;
235 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
243 #else /* CONFIG_SMP */
245 static inline struct hrtimer_clock_base *
246 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
248 struct hrtimer_clock_base *base = timer->base;
250 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
255 # define switch_hrtimer_base(t, b, p) (b)
257 #endif /* !CONFIG_SMP */
260 * Functions for the union type storage format of ktime_t which are
261 * too large for inlining:
263 #if BITS_PER_LONG < 64
265 * Divide a ktime value by a nanosecond value
267 u64 ktime_divns(const ktime_t kt, s64 div)
272 dclc = ktime_to_ns(kt);
273 /* Make sure the divisor is less than 2^32: */
279 do_div(dclc, (unsigned long) div);
283 #endif /* BITS_PER_LONG >= 64 */
286 * Add two ktime values and do a safety check for overflow:
288 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
290 ktime_t res = ktime_add(lhs, rhs);
293 * We use KTIME_SEC_MAX here, the maximum timeout which we can
294 * return to user space in a timespec:
296 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
297 res = ktime_set(KTIME_SEC_MAX, 0);
302 EXPORT_SYMBOL_GPL(ktime_add_safe);
304 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
306 static struct debug_obj_descr hrtimer_debug_descr;
308 static void *hrtimer_debug_hint(void *addr)
310 return ((struct hrtimer *) addr)->function;
314 * fixup_init is called when:
315 * - an active object is initialized
317 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
319 struct hrtimer *timer = addr;
322 case ODEBUG_STATE_ACTIVE:
323 hrtimer_cancel(timer);
324 debug_object_init(timer, &hrtimer_debug_descr);
332 * fixup_activate is called when:
333 * - an active object is activated
334 * - an unknown object is activated (might be a statically initialized object)
336 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
340 case ODEBUG_STATE_NOTAVAILABLE:
344 case ODEBUG_STATE_ACTIVE:
353 * fixup_free is called when:
354 * - an active object is freed
356 static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
358 struct hrtimer *timer = addr;
361 case ODEBUG_STATE_ACTIVE:
362 hrtimer_cancel(timer);
363 debug_object_free(timer, &hrtimer_debug_descr);
370 static struct debug_obj_descr hrtimer_debug_descr = {
372 .debug_hint = hrtimer_debug_hint,
373 .fixup_init = hrtimer_fixup_init,
374 .fixup_activate = hrtimer_fixup_activate,
375 .fixup_free = hrtimer_fixup_free,
378 static inline void debug_hrtimer_init(struct hrtimer *timer)
380 debug_object_init(timer, &hrtimer_debug_descr);
383 static inline void debug_hrtimer_activate(struct hrtimer *timer)
385 debug_object_activate(timer, &hrtimer_debug_descr);
388 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
390 debug_object_deactivate(timer, &hrtimer_debug_descr);
393 static inline void debug_hrtimer_free(struct hrtimer *timer)
395 debug_object_free(timer, &hrtimer_debug_descr);
398 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
399 enum hrtimer_mode mode);
401 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
402 enum hrtimer_mode mode)
404 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
405 __hrtimer_init(timer, clock_id, mode);
407 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
409 void destroy_hrtimer_on_stack(struct hrtimer *timer)
411 debug_object_free(timer, &hrtimer_debug_descr);
415 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
416 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
417 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
421 debug_init(struct hrtimer *timer, clockid_t clockid,
422 enum hrtimer_mode mode)
424 debug_hrtimer_init(timer);
425 trace_hrtimer_init(timer, clockid, mode);
428 static inline void debug_activate(struct hrtimer *timer)
430 debug_hrtimer_activate(timer);
431 trace_hrtimer_start(timer);
434 static inline void debug_deactivate(struct hrtimer *timer)
436 debug_hrtimer_deactivate(timer);
437 trace_hrtimer_cancel(timer);
440 /* High resolution timer related functions */
441 #ifdef CONFIG_HIGH_RES_TIMERS
444 * High resolution timer enabled ?
446 static int hrtimer_hres_enabled __read_mostly = 1;
449 * Enable / Disable high resolution mode
451 static int __init setup_hrtimer_hres(char *str)
453 if (!strcmp(str, "off"))
454 hrtimer_hres_enabled = 0;
455 else if (!strcmp(str, "on"))
456 hrtimer_hres_enabled = 1;
462 __setup("highres=", setup_hrtimer_hres);
465 * hrtimer_high_res_enabled - query, if the highres mode is enabled
467 static inline int hrtimer_is_hres_enabled(void)
469 return hrtimer_hres_enabled;
473 * Is the high resolution mode active ?
475 static inline int hrtimer_hres_active(void)
477 return __this_cpu_read(hrtimer_bases.hres_active);
481 * Reprogram the event source with checking both queues for the
483 * Called with interrupts disabled and base->lock held
486 hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
489 struct hrtimer_clock_base *base = cpu_base->clock_base;
490 ktime_t expires, expires_next;
492 expires_next.tv64 = KTIME_MAX;
494 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
495 struct hrtimer *timer;
496 struct timerqueue_node *next;
498 next = timerqueue_getnext(&base->active);
501 timer = container_of(next, struct hrtimer, node);
503 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
505 * clock_was_set() has changed base->offset so the
506 * result might be negative. Fix it up to prevent a
507 * false positive in clockevents_program_event()
509 if (expires.tv64 < 0)
511 if (expires.tv64 < expires_next.tv64)
512 expires_next = expires;
515 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
518 cpu_base->expires_next.tv64 = expires_next.tv64;
521 * If a hang was detected in the last timer interrupt then we
522 * leave the hang delay active in the hardware. We want the
523 * system to make progress. That also prevents the following
525 * T1 expires 50ms from now
526 * T2 expires 5s from now
528 * T1 is removed, so this code is called and would reprogram
529 * the hardware to 5s from now. Any hrtimer_start after that
530 * will not reprogram the hardware due to hang_detected being
531 * set. So we'd effectivly block all timers until the T2 event
534 if (cpu_base->hang_detected)
537 if (cpu_base->expires_next.tv64 != KTIME_MAX)
538 tick_program_event(cpu_base->expires_next, 1);
542 * Shared reprogramming for clock_realtime and clock_monotonic
544 * When a timer is enqueued and expires earlier than the already enqueued
545 * timers, we have to check, whether it expires earlier than the timer for
546 * which the clock event device was armed.
548 * Note, that in case the state has HRTIMER_STATE_CALLBACK set, no reprogramming
549 * and no expiry check happens. The timer gets enqueued into the rbtree. The
550 * reprogramming and expiry check is done in the hrtimer_interrupt or in the
553 * Called with interrupts disabled and base->cpu_base.lock held
555 static int hrtimer_reprogram(struct hrtimer *timer,
556 struct hrtimer_clock_base *base)
558 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
559 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
562 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
565 * When the callback is running, we do not reprogram the clock event
566 * device. The timer callback is either running on a different CPU or
567 * the callback is executed in the hrtimer_interrupt context. The
568 * reprogramming is handled either by the softirq, which called the
569 * callback or at the end of the hrtimer_interrupt.
571 if (hrtimer_callback_running(timer))
575 * CLOCK_REALTIME timer might be requested with an absolute
576 * expiry time which is less than base->offset. Nothing wrong
577 * about that, just avoid to call into the tick code, which
578 * has now objections against negative expiry values.
580 if (expires.tv64 < 0)
583 if (expires.tv64 >= cpu_base->expires_next.tv64)
587 * If a hang was detected in the last timer interrupt then we
588 * do not schedule a timer which is earlier than the expiry
589 * which we enforced in the hang detection. We want the system
592 if (cpu_base->hang_detected)
596 * Clockevents returns -ETIME, when the event was in the past.
598 res = tick_program_event(expires, 0);
599 if (!IS_ERR_VALUE(res))
600 cpu_base->expires_next = expires;
605 * Initialize the high resolution related parts of cpu_base
607 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
609 base->expires_next.tv64 = KTIME_MAX;
610 base->hres_active = 0;
613 static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
615 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
616 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
617 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
619 return ktime_get_update_offsets_now(offs_real, offs_boot, offs_tai);
623 * Retrigger next event is called after clock was set
625 * Called with interrupts disabled via on_each_cpu()
627 static void retrigger_next_event(void *arg)
629 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
631 if (!hrtimer_hres_active())
634 raw_spin_lock(&base->lock);
635 hrtimer_update_base(base);
636 hrtimer_force_reprogram(base, 0);
637 raw_spin_unlock(&base->lock);
641 * Switch to high resolution mode
643 static int hrtimer_switch_to_hres(void)
645 int i, cpu = smp_processor_id();
646 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
649 if (base->hres_active)
652 local_irq_save(flags);
654 if (tick_init_highres()) {
655 local_irq_restore(flags);
656 printk(KERN_WARNING "Could not switch to high resolution "
657 "mode on CPU %d\n", cpu);
660 base->hres_active = 1;
661 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
662 base->clock_base[i].resolution = KTIME_HIGH_RES;
664 tick_setup_sched_timer();
665 /* "Retrigger" the interrupt to get things going */
666 retrigger_next_event(NULL);
667 local_irq_restore(flags);
671 static void clock_was_set_work(struct work_struct *work)
676 static DECLARE_WORK(hrtimer_work, clock_was_set_work);
679 * Called from timekeeping and resume code to reprogramm the hrtimer
680 * interrupt device on all cpus.
682 void clock_was_set_delayed(void)
684 schedule_work(&hrtimer_work);
689 static inline int hrtimer_hres_active(void) { return 0; }
690 static inline int hrtimer_is_hres_enabled(void) { return 0; }
691 static inline int hrtimer_switch_to_hres(void) { return 0; }
693 hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
694 static inline int hrtimer_reprogram(struct hrtimer *timer,
695 struct hrtimer_clock_base *base)
699 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
700 static inline void retrigger_next_event(void *arg) { }
702 #endif /* CONFIG_HIGH_RES_TIMERS */
705 * Clock realtime was set
707 * Change the offset of the realtime clock vs. the monotonic
710 * We might have to reprogram the high resolution timer interrupt. On
711 * SMP we call the architecture specific code to retrigger _all_ high
712 * resolution timer interrupts. On UP we just disable interrupts and
713 * call the high resolution interrupt code.
715 void clock_was_set(void)
717 #ifdef CONFIG_HIGH_RES_TIMERS
718 /* Retrigger the CPU local events everywhere */
719 on_each_cpu(retrigger_next_event, NULL, 1);
721 timerfd_clock_was_set();
725 * During resume we might have to reprogram the high resolution timer
726 * interrupt on all online CPUs. However, all other CPUs will be
727 * stopped with IRQs interrupts disabled so the clock_was_set() call
730 void hrtimers_resume(void)
732 WARN_ONCE(!irqs_disabled(),
733 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
735 /* Retrigger on the local CPU */
736 retrigger_next_event(NULL);
737 /* And schedule a retrigger for all others */
738 clock_was_set_delayed();
741 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
743 #ifdef CONFIG_TIMER_STATS
744 if (timer->start_site)
746 timer->start_site = __builtin_return_address(0);
747 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
748 timer->start_pid = current->pid;
752 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
754 #ifdef CONFIG_TIMER_STATS
755 timer->start_site = NULL;
759 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
761 #ifdef CONFIG_TIMER_STATS
762 if (likely(!timer_stats_active))
764 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
765 timer->function, timer->start_comm, 0);
770 * Counterpart to lock_hrtimer_base above:
773 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
775 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
779 * hrtimer_forward - forward the timer expiry
780 * @timer: hrtimer to forward
781 * @now: forward past this time
782 * @interval: the interval to forward
784 * Forward the timer expiry so it will expire in the future.
785 * Returns the number of overruns.
787 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
792 delta = ktime_sub(now, hrtimer_get_expires(timer));
797 if (interval.tv64 < timer->base->resolution.tv64)
798 interval.tv64 = timer->base->resolution.tv64;
800 if (unlikely(delta.tv64 >= interval.tv64)) {
801 s64 incr = ktime_to_ns(interval);
803 orun = ktime_divns(delta, incr);
804 hrtimer_add_expires_ns(timer, incr * orun);
805 if (hrtimer_get_expires_tv64(timer) > now.tv64)
808 * This (and the ktime_add() below) is the
809 * correction for exact:
813 hrtimer_add_expires(timer, interval);
817 EXPORT_SYMBOL_GPL(hrtimer_forward);
820 * enqueue_hrtimer - internal function to (re)start a timer
822 * The timer is inserted in expiry order. Insertion into the
823 * red black tree is O(log(n)). Must hold the base lock.
825 * Returns 1 when the new timer is the leftmost timer in the tree.
827 static int enqueue_hrtimer(struct hrtimer *timer,
828 struct hrtimer_clock_base *base)
830 debug_activate(timer);
832 timerqueue_add(&base->active, &timer->node);
833 base->cpu_base->active_bases |= 1 << base->index;
836 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
837 * state of a possibly running callback.
839 timer->state |= HRTIMER_STATE_ENQUEUED;
841 return (&timer->node == base->active.next);
845 * __remove_hrtimer - internal function to remove a timer
847 * Caller must hold the base lock.
849 * High resolution timer mode reprograms the clock event device when the
850 * timer is the one which expires next. The caller can disable this by setting
851 * reprogram to zero. This is useful, when the context does a reprogramming
852 * anyway (e.g. timer interrupt)
854 static void __remove_hrtimer(struct hrtimer *timer,
855 struct hrtimer_clock_base *base,
856 unsigned long newstate, int reprogram)
858 struct timerqueue_node *next_timer;
859 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
862 next_timer = timerqueue_getnext(&base->active);
863 timerqueue_del(&base->active, &timer->node);
864 if (&timer->node == next_timer) {
865 #ifdef CONFIG_HIGH_RES_TIMERS
866 /* Reprogram the clock event device. if enabled */
867 if (reprogram && hrtimer_hres_active()) {
870 expires = ktime_sub(hrtimer_get_expires(timer),
872 if (base->cpu_base->expires_next.tv64 == expires.tv64)
873 hrtimer_force_reprogram(base->cpu_base, 1);
877 if (!timerqueue_getnext(&base->active))
878 base->cpu_base->active_bases &= ~(1 << base->index);
880 timer->state = newstate;
884 * remove hrtimer, called with base lock held
887 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
889 if (hrtimer_is_queued(timer)) {
894 * Remove the timer and force reprogramming when high
895 * resolution mode is active and the timer is on the current
896 * CPU. If we remove a timer on another CPU, reprogramming is
897 * skipped. The interrupt event on this CPU is fired and
898 * reprogramming happens in the interrupt handler. This is a
899 * rare case and less expensive than a smp call.
901 debug_deactivate(timer);
902 timer_stats_hrtimer_clear_start_info(timer);
903 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
905 * We must preserve the CALLBACK state flag here,
906 * otherwise we could move the timer base in
907 * switch_hrtimer_base.
909 state = timer->state & HRTIMER_STATE_CALLBACK;
910 __remove_hrtimer(timer, base, state, reprogram);
916 int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
917 unsigned long delta_ns, const enum hrtimer_mode mode,
920 struct hrtimer_clock_base *base, *new_base;
924 base = lock_hrtimer_base(timer, &flags);
926 /* Remove an active timer from the queue: */
927 ret = remove_hrtimer(timer, base);
929 if (mode & HRTIMER_MODE_REL) {
930 tim = ktime_add_safe(tim, base->get_time());
932 * CONFIG_TIME_LOW_RES is a temporary way for architectures
933 * to signal that they simply return xtime in
934 * do_gettimeoffset(). In this case we want to round up by
935 * resolution when starting a relative timer, to avoid short
936 * timeouts. This will go away with the GTOD framework.
938 #ifdef CONFIG_TIME_LOW_RES
939 tim = ktime_add_safe(tim, base->resolution);
943 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
945 /* Switch the timer base, if necessary: */
946 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
948 timer_stats_hrtimer_set_start_info(timer);
950 leftmost = enqueue_hrtimer(timer, new_base);
953 unlock_hrtimer_base(timer, &flags);
957 if (!hrtimer_is_hres_active(timer)) {
959 * Kick to reschedule the next tick to handle the new timer
960 * on dynticks target.
962 wake_up_nohz_cpu(new_base->cpu_base->cpu);
963 } else if (new_base->cpu_base == &__get_cpu_var(hrtimer_bases) &&
964 hrtimer_reprogram(timer, new_base)) {
966 * Only allow reprogramming if the new base is on this CPU.
967 * (it might still be on another CPU if the timer was pending)
969 * XXX send_remote_softirq() ?
973 * We need to drop cpu_base->lock to avoid a
974 * lock ordering issue vs. rq->lock.
976 raw_spin_unlock(&new_base->cpu_base->lock);
977 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
978 local_irq_restore(flags);
981 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
985 unlock_hrtimer_base(timer, &flags);
989 EXPORT_SYMBOL_GPL(__hrtimer_start_range_ns);
992 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
993 * @timer: the timer to be added
995 * @delta_ns: "slack" range for the timer
996 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
997 * relative (HRTIMER_MODE_REL)
1001 * 1 when the timer was active
1003 int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1004 unsigned long delta_ns, const enum hrtimer_mode mode)
1006 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1008 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1011 * hrtimer_start - (re)start an hrtimer on the current CPU
1012 * @timer: the timer to be added
1014 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1015 * relative (HRTIMER_MODE_REL)
1019 * 1 when the timer was active
1022 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1024 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
1026 EXPORT_SYMBOL_GPL(hrtimer_start);
1030 * hrtimer_try_to_cancel - try to deactivate a timer
1031 * @timer: hrtimer to stop
1034 * 0 when the timer was not active
1035 * 1 when the timer was active
1036 * -1 when the timer is currently excuting the callback function and
1039 int hrtimer_try_to_cancel(struct hrtimer *timer)
1041 struct hrtimer_clock_base *base;
1042 unsigned long flags;
1045 base = lock_hrtimer_base(timer, &flags);
1047 if (!hrtimer_callback_running(timer))
1048 ret = remove_hrtimer(timer, base);
1050 unlock_hrtimer_base(timer, &flags);
1055 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1058 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1059 * @timer: the timer to be cancelled
1062 * 0 when the timer was not active
1063 * 1 when the timer was active
1065 int hrtimer_cancel(struct hrtimer *timer)
1068 int ret = hrtimer_try_to_cancel(timer);
1075 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1078 * hrtimer_get_remaining - get remaining time for the timer
1079 * @timer: the timer to read
1081 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1083 unsigned long flags;
1086 lock_hrtimer_base(timer, &flags);
1087 rem = hrtimer_expires_remaining(timer);
1088 unlock_hrtimer_base(timer, &flags);
1092 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1094 #ifdef CONFIG_NO_HZ_COMMON
1096 * hrtimer_get_next_event - get the time until next expiry event
1098 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1101 ktime_t hrtimer_get_next_event(void)
1103 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1104 struct hrtimer_clock_base *base = cpu_base->clock_base;
1105 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1106 unsigned long flags;
1109 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1111 if (!hrtimer_hres_active()) {
1112 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1113 struct hrtimer *timer;
1114 struct timerqueue_node *next;
1116 next = timerqueue_getnext(&base->active);
1120 timer = container_of(next, struct hrtimer, node);
1121 delta.tv64 = hrtimer_get_expires_tv64(timer);
1122 delta = ktime_sub(delta, base->get_time());
1123 if (delta.tv64 < mindelta.tv64)
1124 mindelta.tv64 = delta.tv64;
1128 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1130 if (mindelta.tv64 < 0)
1136 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1137 enum hrtimer_mode mode)
1139 struct hrtimer_cpu_base *cpu_base;
1142 memset(timer, 0, sizeof(struct hrtimer));
1144 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1146 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1147 clock_id = CLOCK_MONOTONIC;
1149 base = hrtimer_clockid_to_base(clock_id);
1150 timer->base = &cpu_base->clock_base[base];
1151 timerqueue_init(&timer->node);
1153 #ifdef CONFIG_TIMER_STATS
1154 timer->start_site = NULL;
1155 timer->start_pid = -1;
1156 memset(timer->start_comm, 0, TASK_COMM_LEN);
1161 * hrtimer_init - initialize a timer to the given clock
1162 * @timer: the timer to be initialized
1163 * @clock_id: the clock to be used
1164 * @mode: timer mode abs/rel
1166 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1167 enum hrtimer_mode mode)
1169 debug_init(timer, clock_id, mode);
1170 __hrtimer_init(timer, clock_id, mode);
1172 EXPORT_SYMBOL_GPL(hrtimer_init);
1175 * hrtimer_get_res - get the timer resolution for a clock
1176 * @which_clock: which clock to query
1177 * @tp: pointer to timespec variable to store the resolution
1179 * Store the resolution of the clock selected by @which_clock in the
1180 * variable pointed to by @tp.
1182 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1184 struct hrtimer_cpu_base *cpu_base;
1185 int base = hrtimer_clockid_to_base(which_clock);
1187 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1188 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
1192 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1194 static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
1196 struct hrtimer_clock_base *base = timer->base;
1197 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1198 enum hrtimer_restart (*fn)(struct hrtimer *);
1201 WARN_ON(!irqs_disabled());
1203 debug_deactivate(timer);
1204 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1205 timer_stats_account_hrtimer(timer);
1206 fn = timer->function;
1209 * Because we run timers from hardirq context, there is no chance
1210 * they get migrated to another cpu, therefore its safe to unlock
1213 raw_spin_unlock(&cpu_base->lock);
1214 trace_hrtimer_expire_entry(timer, now);
1215 restart = fn(timer);
1216 trace_hrtimer_expire_exit(timer);
1217 raw_spin_lock(&cpu_base->lock);
1220 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1221 * we do not reprogramm the event hardware. Happens either in
1222 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1224 if (restart != HRTIMER_NORESTART) {
1225 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1226 enqueue_hrtimer(timer, base);
1229 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1231 timer->state &= ~HRTIMER_STATE_CALLBACK;
1234 #ifdef CONFIG_HIGH_RES_TIMERS
1237 * High resolution timer interrupt
1238 * Called with interrupts disabled
1240 void hrtimer_interrupt(struct clock_event_device *dev)
1242 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1243 ktime_t expires_next, now, entry_time, delta;
1246 BUG_ON(!cpu_base->hres_active);
1247 cpu_base->nr_events++;
1248 dev->next_event.tv64 = KTIME_MAX;
1250 raw_spin_lock(&cpu_base->lock);
1251 entry_time = now = hrtimer_update_base(cpu_base);
1253 expires_next.tv64 = KTIME_MAX;
1255 * We set expires_next to KTIME_MAX here with cpu_base->lock
1256 * held to prevent that a timer is enqueued in our queue via
1257 * the migration code. This does not affect enqueueing of
1258 * timers which run their callback and need to be requeued on
1261 cpu_base->expires_next.tv64 = KTIME_MAX;
1263 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1264 struct hrtimer_clock_base *base;
1265 struct timerqueue_node *node;
1268 if (!(cpu_base->active_bases & (1 << i)))
1271 base = cpu_base->clock_base + i;
1272 basenow = ktime_add(now, base->offset);
1274 while ((node = timerqueue_getnext(&base->active))) {
1275 struct hrtimer *timer;
1277 timer = container_of(node, struct hrtimer, node);
1280 * The immediate goal for using the softexpires is
1281 * minimizing wakeups, not running timers at the
1282 * earliest interrupt after their soft expiration.
1283 * This allows us to avoid using a Priority Search
1284 * Tree, which can answer a stabbing querry for
1285 * overlapping intervals and instead use the simple
1286 * BST we already have.
1287 * We don't add extra wakeups by delaying timers that
1288 * are right-of a not yet expired timer, because that
1289 * timer will have to trigger a wakeup anyway.
1292 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
1295 expires = ktime_sub(hrtimer_get_expires(timer),
1297 if (expires.tv64 < 0)
1298 expires.tv64 = KTIME_MAX;
1299 if (expires.tv64 < expires_next.tv64)
1300 expires_next = expires;
1304 __run_hrtimer(timer, &basenow);
1309 * Store the new expiry value so the migration code can verify
1312 cpu_base->expires_next = expires_next;
1313 raw_spin_unlock(&cpu_base->lock);
1315 /* Reprogramming necessary ? */
1316 if (expires_next.tv64 == KTIME_MAX ||
1317 !tick_program_event(expires_next, 0)) {
1318 cpu_base->hang_detected = 0;
1323 * The next timer was already expired due to:
1325 * - long lasting callbacks
1326 * - being scheduled away when running in a VM
1328 * We need to prevent that we loop forever in the hrtimer
1329 * interrupt routine. We give it 3 attempts to avoid
1330 * overreacting on some spurious event.
1332 * Acquire base lock for updating the offsets and retrieving
1335 raw_spin_lock(&cpu_base->lock);
1336 now = hrtimer_update_base(cpu_base);
1337 cpu_base->nr_retries++;
1341 * Give the system a chance to do something else than looping
1342 * here. We stored the entry time, so we know exactly how long
1343 * we spent here. We schedule the next event this amount of
1346 cpu_base->nr_hangs++;
1347 cpu_base->hang_detected = 1;
1348 raw_spin_unlock(&cpu_base->lock);
1349 delta = ktime_sub(now, entry_time);
1350 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1351 cpu_base->max_hang_time = delta;
1353 * Limit it to a sensible value as we enforce a longer
1354 * delay. Give the CPU at least 100ms to catch up.
1356 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1357 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1359 expires_next = ktime_add(now, delta);
1360 tick_program_event(expires_next, 1);
1361 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1362 ktime_to_ns(delta));
1366 * local version of hrtimer_peek_ahead_timers() called with interrupts
1369 static void __hrtimer_peek_ahead_timers(void)
1371 struct tick_device *td;
1373 if (!hrtimer_hres_active())
1376 td = &__get_cpu_var(tick_cpu_device);
1377 if (td && td->evtdev)
1378 hrtimer_interrupt(td->evtdev);
1382 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1384 * hrtimer_peek_ahead_timers will peek at the timer queue of
1385 * the current cpu and check if there are any timers for which
1386 * the soft expires time has passed. If any such timers exist,
1387 * they are run immediately and then removed from the timer queue.
1390 void hrtimer_peek_ahead_timers(void)
1392 unsigned long flags;
1394 local_irq_save(flags);
1395 __hrtimer_peek_ahead_timers();
1396 local_irq_restore(flags);
1399 static void run_hrtimer_softirq(struct softirq_action *h)
1401 hrtimer_peek_ahead_timers();
1404 #else /* CONFIG_HIGH_RES_TIMERS */
1406 static inline void __hrtimer_peek_ahead_timers(void) { }
1408 #endif /* !CONFIG_HIGH_RES_TIMERS */
1411 * Called from timer softirq every jiffy, expire hrtimers:
1413 * For HRT its the fall back code to run the softirq in the timer
1414 * softirq context in case the hrtimer initialization failed or has
1415 * not been done yet.
1417 void hrtimer_run_pending(void)
1419 if (hrtimer_hres_active())
1423 * This _is_ ugly: We have to check in the softirq context,
1424 * whether we can switch to highres and / or nohz mode. The
1425 * clocksource switch happens in the timer interrupt with
1426 * xtime_lock held. Notification from there only sets the
1427 * check bit in the tick_oneshot code, otherwise we might
1428 * deadlock vs. xtime_lock.
1430 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1431 hrtimer_switch_to_hres();
1435 * Called from hardirq context every jiffy
1437 void hrtimer_run_queues(void)
1439 struct timerqueue_node *node;
1440 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1441 struct hrtimer_clock_base *base;
1442 int index, gettime = 1;
1444 if (hrtimer_hres_active())
1447 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1448 base = &cpu_base->clock_base[index];
1449 if (!timerqueue_getnext(&base->active))
1453 hrtimer_get_softirq_time(cpu_base);
1457 raw_spin_lock(&cpu_base->lock);
1459 while ((node = timerqueue_getnext(&base->active))) {
1460 struct hrtimer *timer;
1462 timer = container_of(node, struct hrtimer, node);
1463 if (base->softirq_time.tv64 <=
1464 hrtimer_get_expires_tv64(timer))
1467 __run_hrtimer(timer, &base->softirq_time);
1469 raw_spin_unlock(&cpu_base->lock);
1474 * Sleep related functions:
1476 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1478 struct hrtimer_sleeper *t =
1479 container_of(timer, struct hrtimer_sleeper, timer);
1480 struct task_struct *task = t->task;
1484 wake_up_process(task);
1486 return HRTIMER_NORESTART;
1489 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1491 sl->timer.function = hrtimer_wakeup;
1494 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
1496 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1498 hrtimer_init_sleeper(t, current);
1501 set_current_state(TASK_INTERRUPTIBLE);
1502 hrtimer_start_expires(&t->timer, mode);
1503 if (!hrtimer_active(&t->timer))
1506 if (likely(t->task))
1507 freezable_schedule();
1509 hrtimer_cancel(&t->timer);
1510 mode = HRTIMER_MODE_ABS;
1512 } while (t->task && !signal_pending(current));
1514 __set_current_state(TASK_RUNNING);
1516 return t->task == NULL;
1519 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1521 struct timespec rmt;
1524 rem = hrtimer_expires_remaining(timer);
1527 rmt = ktime_to_timespec(rem);
1529 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1535 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1537 struct hrtimer_sleeper t;
1538 struct timespec __user *rmtp;
1541 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
1543 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1545 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1548 rmtp = restart->nanosleep.rmtp;
1550 ret = update_rmtp(&t.timer, rmtp);
1555 /* The other values in restart are already filled in */
1556 ret = -ERESTART_RESTARTBLOCK;
1558 destroy_hrtimer_on_stack(&t.timer);
1562 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1563 const enum hrtimer_mode mode, const clockid_t clockid)
1565 struct restart_block *restart;
1566 struct hrtimer_sleeper t;
1568 unsigned long slack;
1570 slack = current->timer_slack_ns;
1571 if (dl_task(current) || rt_task(current))
1574 hrtimer_init_on_stack(&t.timer, clockid, mode);
1575 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1576 if (do_nanosleep(&t, mode))
1579 /* Absolute timers do not update the rmtp value and restart: */
1580 if (mode == HRTIMER_MODE_ABS) {
1581 ret = -ERESTARTNOHAND;
1586 ret = update_rmtp(&t.timer, rmtp);
1591 restart = ¤t_thread_info()->restart_block;
1592 restart->fn = hrtimer_nanosleep_restart;
1593 restart->nanosleep.clockid = t.timer.base->clockid;
1594 restart->nanosleep.rmtp = rmtp;
1595 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1597 ret = -ERESTART_RESTARTBLOCK;
1599 destroy_hrtimer_on_stack(&t.timer);
1603 SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1604 struct timespec __user *, rmtp)
1608 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1611 if (!timespec_valid(&tu))
1614 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1618 * Functions related to boot-time initialization:
1620 static void init_hrtimers_cpu(int cpu)
1622 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1625 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1626 cpu_base->clock_base[i].cpu_base = cpu_base;
1627 timerqueue_init_head(&cpu_base->clock_base[i].active);
1630 cpu_base->cpu = cpu;
1631 hrtimer_init_hres(cpu_base);
1634 #ifdef CONFIG_HOTPLUG_CPU
1636 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1637 struct hrtimer_clock_base *new_base)
1639 struct hrtimer *timer;
1640 struct timerqueue_node *node;
1642 while ((node = timerqueue_getnext(&old_base->active))) {
1643 timer = container_of(node, struct hrtimer, node);
1644 BUG_ON(hrtimer_callback_running(timer));
1645 debug_deactivate(timer);
1648 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1649 * timer could be seen as !active and just vanish away
1650 * under us on another CPU
1652 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
1653 timer->base = new_base;
1655 * Enqueue the timers on the new cpu. This does not
1656 * reprogram the event device in case the timer
1657 * expires before the earliest on this CPU, but we run
1658 * hrtimer_interrupt after we migrated everything to
1659 * sort out already expired timers and reprogram the
1662 enqueue_hrtimer(timer, new_base);
1664 /* Clear the migration state bit */
1665 timer->state &= ~HRTIMER_STATE_MIGRATE;
1669 static void migrate_hrtimers(int scpu)
1671 struct hrtimer_cpu_base *old_base, *new_base;
1674 BUG_ON(cpu_online(scpu));
1675 tick_cancel_sched_timer(scpu);
1677 local_irq_disable();
1678 old_base = &per_cpu(hrtimer_bases, scpu);
1679 new_base = &__get_cpu_var(hrtimer_bases);
1681 * The caller is globally serialized and nobody else
1682 * takes two locks at once, deadlock is not possible.
1684 raw_spin_lock(&new_base->lock);
1685 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1687 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1688 migrate_hrtimer_list(&old_base->clock_base[i],
1689 &new_base->clock_base[i]);
1692 raw_spin_unlock(&old_base->lock);
1693 raw_spin_unlock(&new_base->lock);
1695 /* Check, if we got expired work to do */
1696 __hrtimer_peek_ahead_timers();
1700 #endif /* CONFIG_HOTPLUG_CPU */
1702 static int hrtimer_cpu_notify(struct notifier_block *self,
1703 unsigned long action, void *hcpu)
1705 int scpu = (long)hcpu;
1709 case CPU_UP_PREPARE:
1710 case CPU_UP_PREPARE_FROZEN:
1711 init_hrtimers_cpu(scpu);
1714 #ifdef CONFIG_HOTPLUG_CPU
1716 case CPU_DYING_FROZEN:
1717 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1720 case CPU_DEAD_FROZEN:
1722 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1723 migrate_hrtimers(scpu);
1735 static struct notifier_block hrtimers_nb = {
1736 .notifier_call = hrtimer_cpu_notify,
1739 void __init hrtimers_init(void)
1741 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1742 (void *)(long)smp_processor_id());
1743 register_cpu_notifier(&hrtimers_nb);
1744 #ifdef CONFIG_HIGH_RES_TIMERS
1745 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1750 * schedule_hrtimeout_range_clock - sleep until timeout
1751 * @expires: timeout value (ktime_t)
1752 * @delta: slack in expires timeout (ktime_t)
1753 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1754 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1757 schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1758 const enum hrtimer_mode mode, int clock)
1760 struct hrtimer_sleeper t;
1763 * Optimize when a zero timeout value is given. It does not
1764 * matter whether this is an absolute or a relative time.
1766 if (expires && !expires->tv64) {
1767 __set_current_state(TASK_RUNNING);
1772 * A NULL parameter means "infinite"
1776 __set_current_state(TASK_RUNNING);
1780 hrtimer_init_on_stack(&t.timer, clock, mode);
1781 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1783 hrtimer_init_sleeper(&t, current);
1785 hrtimer_start_expires(&t.timer, mode);
1786 if (!hrtimer_active(&t.timer))
1792 hrtimer_cancel(&t.timer);
1793 destroy_hrtimer_on_stack(&t.timer);
1795 __set_current_state(TASK_RUNNING);
1797 return !t.task ? 0 : -EINTR;
1801 * schedule_hrtimeout_range - sleep until timeout
1802 * @expires: timeout value (ktime_t)
1803 * @delta: slack in expires timeout (ktime_t)
1804 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1806 * Make the current task sleep until the given expiry time has
1807 * elapsed. The routine will return immediately unless
1808 * the current task state has been set (see set_current_state()).
1810 * The @delta argument gives the kernel the freedom to schedule the
1811 * actual wakeup to a time that is both power and performance friendly.
1812 * The kernel give the normal best effort behavior for "@expires+@delta",
1813 * but may decide to fire the timer earlier, but no earlier than @expires.
1815 * You can set the task state as follows -
1817 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1818 * pass before the routine returns.
1820 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1821 * delivered to the current task.
1823 * The current task state is guaranteed to be TASK_RUNNING when this
1826 * Returns 0 when the timer has expired otherwise -EINTR
1828 int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1829 const enum hrtimer_mode mode)
1831 return schedule_hrtimeout_range_clock(expires, delta, mode,
1834 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1837 * schedule_hrtimeout - sleep until timeout
1838 * @expires: timeout value (ktime_t)
1839 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1841 * Make the current task sleep until the given expiry time has
1842 * elapsed. The routine will return immediately unless
1843 * the current task state has been set (see set_current_state()).
1845 * You can set the task state as follows -
1847 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1848 * pass before the routine returns.
1850 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1851 * delivered to the current task.
1853 * The current task state is guaranteed to be TASK_RUNNING when this
1856 * Returns 0 when the timer has expired otherwise -EINTR
1858 int __sched schedule_hrtimeout(ktime_t *expires,
1859 const enum hrtimer_mode mode)
1861 return schedule_hrtimeout_range(expires, 0, mode);
1863 EXPORT_SYMBOL_GPL(schedule_hrtimeout);