2 * linux/kernel/hrtimer.c
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/timer.h>
49 #include <asm/uaccess.h>
51 #include <trace/events/timer.h>
56 * There are more clockids then hrtimer bases. Thus, we index
57 * into the timer bases by the hrtimer_base_type enum. When trying
58 * to reach a base using a clockid, hrtimer_clockid_to_base()
59 * is used to convert from clockid to the proper hrtimer_base_type.
61 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
67 .index = HRTIMER_BASE_MONOTONIC,
68 .clockid = CLOCK_MONOTONIC,
69 .get_time = &ktime_get,
70 .resolution = KTIME_LOW_RES,
73 .index = HRTIMER_BASE_REALTIME,
74 .clockid = CLOCK_REALTIME,
75 .get_time = &ktime_get_real,
76 .resolution = KTIME_LOW_RES,
79 .index = HRTIMER_BASE_BOOTTIME,
80 .clockid = CLOCK_BOOTTIME,
81 .get_time = &ktime_get_boottime,
82 .resolution = KTIME_LOW_RES,
87 static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
88 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
89 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
90 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
93 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
95 return hrtimer_clock_to_base_table[clock_id];
100 * Get the coarse grained time at the softirq based on xtime and
103 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
105 ktime_t xtim, mono, boot;
106 struct timespec xts, tom, slp;
108 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
110 xtim = timespec_to_ktime(xts);
111 mono = ktime_add(xtim, timespec_to_ktime(tom));
112 boot = ktime_add(mono, timespec_to_ktime(slp));
113 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
114 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
115 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
119 * Functions and macros which are different for UP/SMP systems are kept in a
125 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
126 * means that all timers which are tied to this base via timer->base are
127 * locked, and the base itself is locked too.
129 * So __run_timers/migrate_timers can safely modify all timers which could
130 * be found on the lists/queues.
132 * When the timer's base is locked, and the timer removed from list, it is
133 * possible to set timer->base = NULL and drop the lock: the timer remains
137 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
138 unsigned long *flags)
140 struct hrtimer_clock_base *base;
144 if (likely(base != NULL)) {
145 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
146 if (likely(base == timer->base))
148 /* The timer has migrated to another CPU: */
149 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
157 * Get the preferred target CPU for NOHZ
159 static int hrtimer_get_target(int this_cpu, int pinned)
162 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
163 return get_nohz_timer_target();
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 = hrtimer_get_target(this_cpu, 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;
238 #else /* CONFIG_SMP */
240 static inline struct hrtimer_clock_base *
241 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
243 struct hrtimer_clock_base *base = timer->base;
245 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
250 # define switch_hrtimer_base(t, b, p) (b)
252 #endif /* !CONFIG_SMP */
255 * Functions for the union type storage format of ktime_t which are
256 * too large for inlining:
258 #if BITS_PER_LONG < 64
259 # ifndef CONFIG_KTIME_SCALAR
261 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
263 * @nsec: the scalar nsec value to add
265 * Returns the sum of kt and nsec in ktime_t format
267 ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
271 if (likely(nsec < NSEC_PER_SEC)) {
274 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
276 tmp = ktime_set((long)nsec, rem);
279 return ktime_add(kt, tmp);
282 EXPORT_SYMBOL_GPL(ktime_add_ns);
285 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
287 * @nsec: the scalar nsec value to subtract
289 * Returns the subtraction of @nsec from @kt in ktime_t format
291 ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
295 if (likely(nsec < NSEC_PER_SEC)) {
298 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
300 tmp = ktime_set((long)nsec, rem);
303 return ktime_sub(kt, tmp);
306 EXPORT_SYMBOL_GPL(ktime_sub_ns);
307 # endif /* !CONFIG_KTIME_SCALAR */
310 * Divide a ktime value by a nanosecond value
312 u64 ktime_divns(const ktime_t kt, s64 div)
317 dclc = ktime_to_ns(kt);
318 /* Make sure the divisor is less than 2^32: */
324 do_div(dclc, (unsigned long) div);
328 #endif /* BITS_PER_LONG >= 64 */
331 * Add two ktime values and do a safety check for overflow:
333 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
335 ktime_t res = ktime_add(lhs, rhs);
338 * We use KTIME_SEC_MAX here, the maximum timeout which we can
339 * return to user space in a timespec:
341 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
342 res = ktime_set(KTIME_SEC_MAX, 0);
347 EXPORT_SYMBOL_GPL(ktime_add_safe);
349 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
351 static struct debug_obj_descr hrtimer_debug_descr;
353 static void *hrtimer_debug_hint(void *addr)
355 return ((struct hrtimer *) addr)->function;
359 * fixup_init is called when:
360 * - an active object is initialized
362 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
364 struct hrtimer *timer = addr;
367 case ODEBUG_STATE_ACTIVE:
368 hrtimer_cancel(timer);
369 debug_object_init(timer, &hrtimer_debug_descr);
377 * fixup_activate is called when:
378 * - an active object is activated
379 * - an unknown object is activated (might be a statically initialized object)
381 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
385 case ODEBUG_STATE_NOTAVAILABLE:
389 case ODEBUG_STATE_ACTIVE:
398 * fixup_free is called when:
399 * - an active object is freed
401 static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
403 struct hrtimer *timer = addr;
406 case ODEBUG_STATE_ACTIVE:
407 hrtimer_cancel(timer);
408 debug_object_free(timer, &hrtimer_debug_descr);
415 static struct debug_obj_descr hrtimer_debug_descr = {
417 .debug_hint = hrtimer_debug_hint,
418 .fixup_init = hrtimer_fixup_init,
419 .fixup_activate = hrtimer_fixup_activate,
420 .fixup_free = hrtimer_fixup_free,
423 static inline void debug_hrtimer_init(struct hrtimer *timer)
425 debug_object_init(timer, &hrtimer_debug_descr);
428 static inline void debug_hrtimer_activate(struct hrtimer *timer)
430 debug_object_activate(timer, &hrtimer_debug_descr);
433 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
435 debug_object_deactivate(timer, &hrtimer_debug_descr);
438 static inline void debug_hrtimer_free(struct hrtimer *timer)
440 debug_object_free(timer, &hrtimer_debug_descr);
443 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
444 enum hrtimer_mode mode);
446 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
447 enum hrtimer_mode mode)
449 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
450 __hrtimer_init(timer, clock_id, mode);
452 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
454 void destroy_hrtimer_on_stack(struct hrtimer *timer)
456 debug_object_free(timer, &hrtimer_debug_descr);
460 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
461 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
462 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
466 debug_init(struct hrtimer *timer, clockid_t clockid,
467 enum hrtimer_mode mode)
469 debug_hrtimer_init(timer);
470 trace_hrtimer_init(timer, clockid, mode);
473 static inline void debug_activate(struct hrtimer *timer)
475 debug_hrtimer_activate(timer);
476 trace_hrtimer_start(timer);
479 static inline void debug_deactivate(struct hrtimer *timer)
481 debug_hrtimer_deactivate(timer);
482 trace_hrtimer_cancel(timer);
485 /* High resolution timer related functions */
486 #ifdef CONFIG_HIGH_RES_TIMERS
489 * High resolution timer enabled ?
491 static int hrtimer_hres_enabled __read_mostly = 1;
494 * Enable / Disable high resolution mode
496 static int __init setup_hrtimer_hres(char *str)
498 if (!strcmp(str, "off"))
499 hrtimer_hres_enabled = 0;
500 else if (!strcmp(str, "on"))
501 hrtimer_hres_enabled = 1;
507 __setup("highres=", setup_hrtimer_hres);
510 * hrtimer_high_res_enabled - query, if the highres mode is enabled
512 static inline int hrtimer_is_hres_enabled(void)
514 return hrtimer_hres_enabled;
518 * Is the high resolution mode active ?
520 static inline int hrtimer_hres_active(void)
522 return __this_cpu_read(hrtimer_bases.hres_active);
526 * Reprogram the event source with checking both queues for the
528 * Called with interrupts disabled and base->lock held
531 hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
534 struct hrtimer_clock_base *base = cpu_base->clock_base;
535 ktime_t expires, expires_next;
537 expires_next.tv64 = KTIME_MAX;
539 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
540 struct hrtimer *timer;
541 struct timerqueue_node *next;
543 next = timerqueue_getnext(&base->active);
546 timer = container_of(next, struct hrtimer, node);
548 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
550 * clock_was_set() has changed base->offset so the
551 * result might be negative. Fix it up to prevent a
552 * false positive in clockevents_program_event()
554 if (expires.tv64 < 0)
556 if (expires.tv64 < expires_next.tv64)
557 expires_next = expires;
560 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
563 cpu_base->expires_next.tv64 = expires_next.tv64;
565 if (cpu_base->expires_next.tv64 != KTIME_MAX)
566 tick_program_event(cpu_base->expires_next, 1);
570 * Shared reprogramming for clock_realtime and clock_monotonic
572 * When a timer is enqueued and expires earlier than the already enqueued
573 * timers, we have to check, whether it expires earlier than the timer for
574 * which the clock event device was armed.
576 * Called with interrupts disabled and base->cpu_base.lock held
578 static int hrtimer_reprogram(struct hrtimer *timer,
579 struct hrtimer_clock_base *base)
581 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
582 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
585 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
588 * When the callback is running, we do not reprogram the clock event
589 * device. The timer callback is either running on a different CPU or
590 * the callback is executed in the hrtimer_interrupt context. The
591 * reprogramming is handled either by the softirq, which called the
592 * callback or at the end of the hrtimer_interrupt.
594 if (hrtimer_callback_running(timer))
598 * CLOCK_REALTIME timer might be requested with an absolute
599 * expiry time which is less than base->offset. Nothing wrong
600 * about that, just avoid to call into the tick code, which
601 * has now objections against negative expiry values.
603 if (expires.tv64 < 0)
606 if (expires.tv64 >= cpu_base->expires_next.tv64)
610 * If a hang was detected in the last timer interrupt then we
611 * do not schedule a timer which is earlier than the expiry
612 * which we enforced in the hang detection. We want the system
615 if (cpu_base->hang_detected)
619 * Clockevents returns -ETIME, when the event was in the past.
621 res = tick_program_event(expires, 0);
622 if (!IS_ERR_VALUE(res))
623 cpu_base->expires_next = expires;
628 * Initialize the high resolution related parts of cpu_base
630 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
632 base->expires_next.tv64 = KTIME_MAX;
633 base->hres_active = 0;
637 * When High resolution timers are active, try to reprogram. Note, that in case
638 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
639 * check happens. The timer gets enqueued into the rbtree. The reprogramming
640 * and expiry check is done in the hrtimer_interrupt or in the softirq.
642 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
643 struct hrtimer_clock_base *base,
646 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
648 raw_spin_unlock(&base->cpu_base->lock);
649 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
650 raw_spin_lock(&base->cpu_base->lock);
652 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
661 * Retrigger next event is called after clock was set
663 * Called with interrupts disabled via on_each_cpu()
665 static void retrigger_next_event(void *arg)
667 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
668 struct timespec realtime_offset, xtim, wtm, sleep;
670 if (!hrtimer_hres_active())
673 /* Optimized out for !HIGH_RES */
674 get_xtime_and_monotonic_and_sleep_offset(&xtim, &wtm, &sleep);
675 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
677 /* Adjust CLOCK_REALTIME offset */
678 raw_spin_lock(&base->lock);
679 base->clock_base[HRTIMER_BASE_REALTIME].offset =
680 timespec_to_ktime(realtime_offset);
681 base->clock_base[HRTIMER_BASE_BOOTTIME].offset =
682 timespec_to_ktime(sleep);
684 hrtimer_force_reprogram(base, 0);
685 raw_spin_unlock(&base->lock);
689 * Switch to high resolution mode
691 static int hrtimer_switch_to_hres(void)
693 int i, cpu = smp_processor_id();
694 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
697 if (base->hres_active)
700 local_irq_save(flags);
702 if (tick_init_highres()) {
703 local_irq_restore(flags);
704 printk(KERN_WARNING "Could not switch to high resolution "
705 "mode on CPU %d\n", cpu);
708 base->hres_active = 1;
709 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
710 base->clock_base[i].resolution = KTIME_HIGH_RES;
712 tick_setup_sched_timer();
714 /* "Retrigger" the interrupt to get things going */
715 retrigger_next_event(NULL);
716 local_irq_restore(flags);
721 * Called from timekeeping code to reprogramm the hrtimer interrupt
722 * device. If called from the timer interrupt context we defer it to
725 void clock_was_set_delayed(void)
727 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
729 cpu_base->clock_was_set = 1;
730 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
735 static inline int hrtimer_hres_active(void) { return 0; }
736 static inline int hrtimer_is_hres_enabled(void) { return 0; }
737 static inline int hrtimer_switch_to_hres(void) { return 0; }
739 hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
740 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
741 struct hrtimer_clock_base *base,
746 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
747 static inline void retrigger_next_event(void *arg) { }
749 #endif /* CONFIG_HIGH_RES_TIMERS */
752 * Clock realtime was set
754 * Change the offset of the realtime clock vs. the monotonic
757 * We might have to reprogram the high resolution timer interrupt. On
758 * SMP we call the architecture specific code to retrigger _all_ high
759 * resolution timer interrupts. On UP we just disable interrupts and
760 * call the high resolution interrupt code.
762 void clock_was_set(void)
764 #ifdef CONFIG_HIGH_RES_TIMERS
765 /* Retrigger the CPU local events everywhere */
766 on_each_cpu(retrigger_next_event, NULL, 1);
768 timerfd_clock_was_set();
772 * During resume we might have to reprogram the high resolution timer
773 * interrupt (on the local CPU):
775 void hrtimers_resume(void)
777 WARN_ONCE(!irqs_disabled(),
778 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
780 retrigger_next_event(NULL);
781 timerfd_clock_was_set();
784 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
786 #ifdef CONFIG_TIMER_STATS
787 if (timer->start_site)
789 timer->start_site = __builtin_return_address(0);
790 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
791 timer->start_pid = current->pid;
795 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
797 #ifdef CONFIG_TIMER_STATS
798 timer->start_site = NULL;
802 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
804 #ifdef CONFIG_TIMER_STATS
805 if (likely(!timer_stats_active))
807 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
808 timer->function, timer->start_comm, 0);
813 * Counterpart to lock_hrtimer_base above:
816 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
818 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
822 * hrtimer_forward - forward the timer expiry
823 * @timer: hrtimer to forward
824 * @now: forward past this time
825 * @interval: the interval to forward
827 * Forward the timer expiry so it will expire in the future.
828 * Returns the number of overruns.
830 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
835 delta = ktime_sub(now, hrtimer_get_expires(timer));
840 if (interval.tv64 < timer->base->resolution.tv64)
841 interval.tv64 = timer->base->resolution.tv64;
843 if (unlikely(delta.tv64 >= interval.tv64)) {
844 s64 incr = ktime_to_ns(interval);
846 orun = ktime_divns(delta, incr);
847 hrtimer_add_expires_ns(timer, incr * orun);
848 if (hrtimer_get_expires_tv64(timer) > now.tv64)
851 * This (and the ktime_add() below) is the
852 * correction for exact:
856 hrtimer_add_expires(timer, interval);
860 EXPORT_SYMBOL_GPL(hrtimer_forward);
863 * enqueue_hrtimer - internal function to (re)start a timer
865 * The timer is inserted in expiry order. Insertion into the
866 * red black tree is O(log(n)). Must hold the base lock.
868 * Returns 1 when the new timer is the leftmost timer in the tree.
870 static int enqueue_hrtimer(struct hrtimer *timer,
871 struct hrtimer_clock_base *base)
873 debug_activate(timer);
875 timerqueue_add(&base->active, &timer->node);
876 base->cpu_base->active_bases |= 1 << base->index;
879 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
880 * state of a possibly running callback.
882 timer->state |= HRTIMER_STATE_ENQUEUED;
884 return (&timer->node == base->active.next);
888 * __remove_hrtimer - internal function to remove a timer
890 * Caller must hold the base lock.
892 * High resolution timer mode reprograms the clock event device when the
893 * timer is the one which expires next. The caller can disable this by setting
894 * reprogram to zero. This is useful, when the context does a reprogramming
895 * anyway (e.g. timer interrupt)
897 static void __remove_hrtimer(struct hrtimer *timer,
898 struct hrtimer_clock_base *base,
899 unsigned long newstate, int reprogram)
901 struct timerqueue_node *next_timer;
902 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
905 next_timer = timerqueue_getnext(&base->active);
906 timerqueue_del(&base->active, &timer->node);
907 if (&timer->node == next_timer) {
908 #ifdef CONFIG_HIGH_RES_TIMERS
909 /* Reprogram the clock event device. if enabled */
910 if (reprogram && hrtimer_hres_active()) {
913 expires = ktime_sub(hrtimer_get_expires(timer),
915 if (base->cpu_base->expires_next.tv64 == expires.tv64)
916 hrtimer_force_reprogram(base->cpu_base, 1);
920 if (!timerqueue_getnext(&base->active))
921 base->cpu_base->active_bases &= ~(1 << base->index);
923 timer->state = newstate;
927 * remove hrtimer, called with base lock held
930 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
932 if (hrtimer_is_queued(timer)) {
937 * Remove the timer and force reprogramming when high
938 * resolution mode is active and the timer is on the current
939 * CPU. If we remove a timer on another CPU, reprogramming is
940 * skipped. The interrupt event on this CPU is fired and
941 * reprogramming happens in the interrupt handler. This is a
942 * rare case and less expensive than a smp call.
944 debug_deactivate(timer);
945 timer_stats_hrtimer_clear_start_info(timer);
946 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
948 * We must preserve the CALLBACK state flag here,
949 * otherwise we could move the timer base in
950 * switch_hrtimer_base.
952 state = timer->state & HRTIMER_STATE_CALLBACK;
953 __remove_hrtimer(timer, base, state, reprogram);
959 int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
960 unsigned long delta_ns, const enum hrtimer_mode mode,
963 struct hrtimer_clock_base *base, *new_base;
967 base = lock_hrtimer_base(timer, &flags);
969 /* Remove an active timer from the queue: */
970 ret = remove_hrtimer(timer, base);
972 /* Switch the timer base, if necessary: */
973 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
975 if (mode & HRTIMER_MODE_REL) {
976 tim = ktime_add_safe(tim, new_base->get_time());
978 * CONFIG_TIME_LOW_RES is a temporary way for architectures
979 * to signal that they simply return xtime in
980 * do_gettimeoffset(). In this case we want to round up by
981 * resolution when starting a relative timer, to avoid short
982 * timeouts. This will go away with the GTOD framework.
984 #ifdef CONFIG_TIME_LOW_RES
985 tim = ktime_add_safe(tim, base->resolution);
989 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
991 timer_stats_hrtimer_set_start_info(timer);
993 leftmost = enqueue_hrtimer(timer, new_base);
996 * Only allow reprogramming if the new base is on this CPU.
997 * (it might still be on another CPU if the timer was pending)
999 * XXX send_remote_softirq() ?
1001 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
1002 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
1004 unlock_hrtimer_base(timer, &flags);
1010 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1011 * @timer: the timer to be added
1013 * @delta_ns: "slack" range for the timer
1014 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1018 * 1 when the timer was active
1020 int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1021 unsigned long delta_ns, const enum hrtimer_mode mode)
1023 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1025 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1028 * hrtimer_start - (re)start an hrtimer on the current CPU
1029 * @timer: the timer to be added
1031 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1035 * 1 when the timer was active
1038 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1040 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
1042 EXPORT_SYMBOL_GPL(hrtimer_start);
1046 * hrtimer_try_to_cancel - try to deactivate a timer
1047 * @timer: hrtimer to stop
1050 * 0 when the timer was not active
1051 * 1 when the timer was active
1052 * -1 when the timer is currently excuting the callback function and
1055 int hrtimer_try_to_cancel(struct hrtimer *timer)
1057 struct hrtimer_clock_base *base;
1058 unsigned long flags;
1061 base = lock_hrtimer_base(timer, &flags);
1063 if (!hrtimer_callback_running(timer))
1064 ret = remove_hrtimer(timer, base);
1066 unlock_hrtimer_base(timer, &flags);
1071 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1074 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1075 * @timer: the timer to be cancelled
1078 * 0 when the timer was not active
1079 * 1 when the timer was active
1081 int hrtimer_cancel(struct hrtimer *timer)
1084 int ret = hrtimer_try_to_cancel(timer);
1091 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1094 * hrtimer_get_remaining - get remaining time for the timer
1095 * @timer: the timer to read
1097 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1099 unsigned long flags;
1102 lock_hrtimer_base(timer, &flags);
1103 rem = hrtimer_expires_remaining(timer);
1104 unlock_hrtimer_base(timer, &flags);
1108 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1112 * hrtimer_get_next_event - get the time until next expiry event
1114 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1117 ktime_t hrtimer_get_next_event(void)
1119 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1120 struct hrtimer_clock_base *base = cpu_base->clock_base;
1121 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1122 unsigned long flags;
1125 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1127 if (!hrtimer_hres_active()) {
1128 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1129 struct hrtimer *timer;
1130 struct timerqueue_node *next;
1132 next = timerqueue_getnext(&base->active);
1136 timer = container_of(next, struct hrtimer, node);
1137 delta.tv64 = hrtimer_get_expires_tv64(timer);
1138 delta = ktime_sub(delta, base->get_time());
1139 if (delta.tv64 < mindelta.tv64)
1140 mindelta.tv64 = delta.tv64;
1144 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1146 if (mindelta.tv64 < 0)
1152 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1153 enum hrtimer_mode mode)
1155 struct hrtimer_cpu_base *cpu_base;
1158 memset(timer, 0, sizeof(struct hrtimer));
1160 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1162 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1163 clock_id = CLOCK_MONOTONIC;
1165 base = hrtimer_clockid_to_base(clock_id);
1166 timer->base = &cpu_base->clock_base[base];
1167 timerqueue_init(&timer->node);
1169 #ifdef CONFIG_TIMER_STATS
1170 timer->start_site = NULL;
1171 timer->start_pid = -1;
1172 memset(timer->start_comm, 0, TASK_COMM_LEN);
1177 * hrtimer_init - initialize a timer to the given clock
1178 * @timer: the timer to be initialized
1179 * @clock_id: the clock to be used
1180 * @mode: timer mode abs/rel
1182 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1183 enum hrtimer_mode mode)
1185 debug_init(timer, clock_id, mode);
1186 __hrtimer_init(timer, clock_id, mode);
1188 EXPORT_SYMBOL_GPL(hrtimer_init);
1191 * hrtimer_get_res - get the timer resolution for a clock
1192 * @which_clock: which clock to query
1193 * @tp: pointer to timespec variable to store the resolution
1195 * Store the resolution of the clock selected by @which_clock in the
1196 * variable pointed to by @tp.
1198 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1200 struct hrtimer_cpu_base *cpu_base;
1201 int base = hrtimer_clockid_to_base(which_clock);
1203 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1204 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
1208 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1210 static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
1212 struct hrtimer_clock_base *base = timer->base;
1213 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1214 enum hrtimer_restart (*fn)(struct hrtimer *);
1217 WARN_ON(!irqs_disabled());
1219 debug_deactivate(timer);
1220 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1221 timer_stats_account_hrtimer(timer);
1222 fn = timer->function;
1225 * Because we run timers from hardirq context, there is no chance
1226 * they get migrated to another cpu, therefore its safe to unlock
1229 raw_spin_unlock(&cpu_base->lock);
1230 trace_hrtimer_expire_entry(timer, now);
1231 restart = fn(timer);
1232 trace_hrtimer_expire_exit(timer);
1233 raw_spin_lock(&cpu_base->lock);
1236 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1237 * we do not reprogramm the event hardware. Happens either in
1238 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1240 if (restart != HRTIMER_NORESTART) {
1241 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1242 enqueue_hrtimer(timer, base);
1245 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1247 timer->state &= ~HRTIMER_STATE_CALLBACK;
1250 #ifdef CONFIG_HIGH_RES_TIMERS
1253 * High resolution timer interrupt
1254 * Called with interrupts disabled
1256 void hrtimer_interrupt(struct clock_event_device *dev)
1258 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1259 ktime_t expires_next, now, entry_time, delta;
1262 BUG_ON(!cpu_base->hres_active);
1263 cpu_base->nr_events++;
1264 dev->next_event.tv64 = KTIME_MAX;
1266 entry_time = now = ktime_get();
1268 expires_next.tv64 = KTIME_MAX;
1270 raw_spin_lock(&cpu_base->lock);
1272 * We set expires_next to KTIME_MAX here with cpu_base->lock
1273 * held to prevent that a timer is enqueued in our queue via
1274 * the migration code. This does not affect enqueueing of
1275 * timers which run their callback and need to be requeued on
1278 cpu_base->expires_next.tv64 = KTIME_MAX;
1280 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1281 struct hrtimer_clock_base *base;
1282 struct timerqueue_node *node;
1285 if (!(cpu_base->active_bases & (1 << i)))
1288 base = cpu_base->clock_base + i;
1289 basenow = ktime_add(now, base->offset);
1291 while ((node = timerqueue_getnext(&base->active))) {
1292 struct hrtimer *timer;
1294 timer = container_of(node, struct hrtimer, node);
1297 * The immediate goal for using the softexpires is
1298 * minimizing wakeups, not running timers at the
1299 * earliest interrupt after their soft expiration.
1300 * This allows us to avoid using a Priority Search
1301 * Tree, which can answer a stabbing querry for
1302 * overlapping intervals and instead use the simple
1303 * BST we already have.
1304 * We don't add extra wakeups by delaying timers that
1305 * are right-of a not yet expired timer, because that
1306 * timer will have to trigger a wakeup anyway.
1309 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
1312 expires = ktime_sub(hrtimer_get_expires(timer),
1314 if (expires.tv64 < expires_next.tv64)
1315 expires_next = expires;
1319 __run_hrtimer(timer, &basenow);
1324 * Store the new expiry value so the migration code can verify
1327 cpu_base->expires_next = expires_next;
1328 raw_spin_unlock(&cpu_base->lock);
1330 /* Reprogramming necessary ? */
1331 if (expires_next.tv64 == KTIME_MAX ||
1332 !tick_program_event(expires_next, 0)) {
1333 cpu_base->hang_detected = 0;
1338 * The next timer was already expired due to:
1340 * - long lasting callbacks
1341 * - being scheduled away when running in a VM
1343 * We need to prevent that we loop forever in the hrtimer
1344 * interrupt routine. We give it 3 attempts to avoid
1345 * overreacting on some spurious event.
1348 cpu_base->nr_retries++;
1352 * Give the system a chance to do something else than looping
1353 * here. We stored the entry time, so we know exactly how long
1354 * we spent here. We schedule the next event this amount of
1357 cpu_base->nr_hangs++;
1358 cpu_base->hang_detected = 1;
1359 delta = ktime_sub(now, entry_time);
1360 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1361 cpu_base->max_hang_time = delta;
1363 * Limit it to a sensible value as we enforce a longer
1364 * delay. Give the CPU at least 100ms to catch up.
1366 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1367 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1369 expires_next = ktime_add(now, delta);
1370 tick_program_event(expires_next, 1);
1371 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1372 ktime_to_ns(delta));
1376 * local version of hrtimer_peek_ahead_timers() called with interrupts
1379 static void __hrtimer_peek_ahead_timers(void)
1381 struct tick_device *td;
1383 if (!hrtimer_hres_active())
1386 td = &__get_cpu_var(tick_cpu_device);
1387 if (td && td->evtdev)
1388 hrtimer_interrupt(td->evtdev);
1392 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1394 * hrtimer_peek_ahead_timers will peek at the timer queue of
1395 * the current cpu and check if there are any timers for which
1396 * the soft expires time has passed. If any such timers exist,
1397 * they are run immediately and then removed from the timer queue.
1400 void hrtimer_peek_ahead_timers(void)
1402 unsigned long flags;
1404 local_irq_save(flags);
1405 __hrtimer_peek_ahead_timers();
1406 local_irq_restore(flags);
1409 static void run_hrtimer_softirq(struct softirq_action *h)
1411 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1413 if (cpu_base->clock_was_set) {
1414 cpu_base->clock_was_set = 0;
1418 hrtimer_peek_ahead_timers();
1421 #else /* CONFIG_HIGH_RES_TIMERS */
1423 static inline void __hrtimer_peek_ahead_timers(void) { }
1425 #endif /* !CONFIG_HIGH_RES_TIMERS */
1428 * Called from timer softirq every jiffy, expire hrtimers:
1430 * For HRT its the fall back code to run the softirq in the timer
1431 * softirq context in case the hrtimer initialization failed or has
1432 * not been done yet.
1434 void hrtimer_run_pending(void)
1436 if (hrtimer_hres_active())
1440 * This _is_ ugly: We have to check in the softirq context,
1441 * whether we can switch to highres and / or nohz mode. The
1442 * clocksource switch happens in the timer interrupt with
1443 * xtime_lock held. Notification from there only sets the
1444 * check bit in the tick_oneshot code, otherwise we might
1445 * deadlock vs. xtime_lock.
1447 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1448 hrtimer_switch_to_hres();
1452 * Called from hardirq context every jiffy
1454 void hrtimer_run_queues(void)
1456 struct timerqueue_node *node;
1457 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1458 struct hrtimer_clock_base *base;
1459 int index, gettime = 1;
1461 if (hrtimer_hres_active())
1464 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1465 base = &cpu_base->clock_base[index];
1466 if (!timerqueue_getnext(&base->active))
1470 hrtimer_get_softirq_time(cpu_base);
1474 raw_spin_lock(&cpu_base->lock);
1476 while ((node = timerqueue_getnext(&base->active))) {
1477 struct hrtimer *timer;
1479 timer = container_of(node, struct hrtimer, node);
1480 if (base->softirq_time.tv64 <=
1481 hrtimer_get_expires_tv64(timer))
1484 __run_hrtimer(timer, &base->softirq_time);
1486 raw_spin_unlock(&cpu_base->lock);
1491 * Sleep related functions:
1493 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1495 struct hrtimer_sleeper *t =
1496 container_of(timer, struct hrtimer_sleeper, timer);
1497 struct task_struct *task = t->task;
1501 wake_up_process(task);
1503 return HRTIMER_NORESTART;
1506 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1508 sl->timer.function = hrtimer_wakeup;
1511 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
1513 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1515 hrtimer_init_sleeper(t, current);
1518 set_current_state(TASK_INTERRUPTIBLE);
1519 hrtimer_start_expires(&t->timer, mode);
1520 if (!hrtimer_active(&t->timer))
1523 if (likely(t->task))
1526 hrtimer_cancel(&t->timer);
1527 mode = HRTIMER_MODE_ABS;
1529 } while (t->task && !signal_pending(current));
1531 __set_current_state(TASK_RUNNING);
1533 return t->task == NULL;
1536 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1538 struct timespec rmt;
1541 rem = hrtimer_expires_remaining(timer);
1544 rmt = ktime_to_timespec(rem);
1546 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1552 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1554 struct hrtimer_sleeper t;
1555 struct timespec __user *rmtp;
1558 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
1560 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1562 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1565 rmtp = restart->nanosleep.rmtp;
1567 ret = update_rmtp(&t.timer, rmtp);
1572 /* The other values in restart are already filled in */
1573 ret = -ERESTART_RESTARTBLOCK;
1575 destroy_hrtimer_on_stack(&t.timer);
1579 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1580 const enum hrtimer_mode mode, const clockid_t clockid)
1582 struct restart_block *restart;
1583 struct hrtimer_sleeper t;
1585 unsigned long slack;
1587 slack = current->timer_slack_ns;
1588 if (rt_task(current))
1591 hrtimer_init_on_stack(&t.timer, clockid, mode);
1592 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1593 if (do_nanosleep(&t, mode))
1596 /* Absolute timers do not update the rmtp value and restart: */
1597 if (mode == HRTIMER_MODE_ABS) {
1598 ret = -ERESTARTNOHAND;
1603 ret = update_rmtp(&t.timer, rmtp);
1608 restart = ¤t_thread_info()->restart_block;
1609 restart->fn = hrtimer_nanosleep_restart;
1610 restart->nanosleep.clockid = t.timer.base->clockid;
1611 restart->nanosleep.rmtp = rmtp;
1612 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1614 ret = -ERESTART_RESTARTBLOCK;
1616 destroy_hrtimer_on_stack(&t.timer);
1620 SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1621 struct timespec __user *, rmtp)
1625 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1628 if (!timespec_valid(&tu))
1631 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1635 * Functions related to boot-time initialization:
1637 static void __cpuinit init_hrtimers_cpu(int cpu)
1639 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1642 raw_spin_lock_init(&cpu_base->lock);
1644 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1645 cpu_base->clock_base[i].cpu_base = cpu_base;
1646 timerqueue_init_head(&cpu_base->clock_base[i].active);
1649 hrtimer_init_hres(cpu_base);
1652 #ifdef CONFIG_HOTPLUG_CPU
1654 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1655 struct hrtimer_clock_base *new_base)
1657 struct hrtimer *timer;
1658 struct timerqueue_node *node;
1660 while ((node = timerqueue_getnext(&old_base->active))) {
1661 timer = container_of(node, struct hrtimer, node);
1662 BUG_ON(hrtimer_callback_running(timer));
1663 debug_deactivate(timer);
1666 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1667 * timer could be seen as !active and just vanish away
1668 * under us on another CPU
1670 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
1671 timer->base = new_base;
1673 * Enqueue the timers on the new cpu. This does not
1674 * reprogram the event device in case the timer
1675 * expires before the earliest on this CPU, but we run
1676 * hrtimer_interrupt after we migrated everything to
1677 * sort out already expired timers and reprogram the
1680 enqueue_hrtimer(timer, new_base);
1682 /* Clear the migration state bit */
1683 timer->state &= ~HRTIMER_STATE_MIGRATE;
1687 static void migrate_hrtimers(int scpu)
1689 struct hrtimer_cpu_base *old_base, *new_base;
1692 BUG_ON(cpu_online(scpu));
1693 tick_cancel_sched_timer(scpu);
1695 local_irq_disable();
1696 old_base = &per_cpu(hrtimer_bases, scpu);
1697 new_base = &__get_cpu_var(hrtimer_bases);
1699 * The caller is globally serialized and nobody else
1700 * takes two locks at once, deadlock is not possible.
1702 raw_spin_lock(&new_base->lock);
1703 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1705 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1706 migrate_hrtimer_list(&old_base->clock_base[i],
1707 &new_base->clock_base[i]);
1710 raw_spin_unlock(&old_base->lock);
1711 raw_spin_unlock(&new_base->lock);
1713 /* Check, if we got expired work to do */
1714 __hrtimer_peek_ahead_timers();
1718 #endif /* CONFIG_HOTPLUG_CPU */
1720 static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
1721 unsigned long action, void *hcpu)
1723 int scpu = (long)hcpu;
1727 case CPU_UP_PREPARE:
1728 case CPU_UP_PREPARE_FROZEN:
1729 init_hrtimers_cpu(scpu);
1732 #ifdef CONFIG_HOTPLUG_CPU
1734 case CPU_DYING_FROZEN:
1735 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1738 case CPU_DEAD_FROZEN:
1740 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1741 migrate_hrtimers(scpu);
1753 static struct notifier_block __cpuinitdata hrtimers_nb = {
1754 .notifier_call = hrtimer_cpu_notify,
1757 void __init hrtimers_init(void)
1759 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1760 (void *)(long)smp_processor_id());
1761 register_cpu_notifier(&hrtimers_nb);
1762 #ifdef CONFIG_HIGH_RES_TIMERS
1763 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1768 * schedule_hrtimeout_range_clock - sleep until timeout
1769 * @expires: timeout value (ktime_t)
1770 * @delta: slack in expires timeout (ktime_t)
1771 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1772 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1775 schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1776 const enum hrtimer_mode mode, int clock)
1778 struct hrtimer_sleeper t;
1781 * Optimize when a zero timeout value is given. It does not
1782 * matter whether this is an absolute or a relative time.
1784 if (expires && !expires->tv64) {
1785 __set_current_state(TASK_RUNNING);
1790 * A NULL parameter means "infinite"
1794 __set_current_state(TASK_RUNNING);
1798 hrtimer_init_on_stack(&t.timer, clock, mode);
1799 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1801 hrtimer_init_sleeper(&t, current);
1803 hrtimer_start_expires(&t.timer, mode);
1804 if (!hrtimer_active(&t.timer))
1810 hrtimer_cancel(&t.timer);
1811 destroy_hrtimer_on_stack(&t.timer);
1813 __set_current_state(TASK_RUNNING);
1815 return !t.task ? 0 : -EINTR;
1819 * schedule_hrtimeout_range - sleep until timeout
1820 * @expires: timeout value (ktime_t)
1821 * @delta: slack in expires timeout (ktime_t)
1822 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1824 * Make the current task sleep until the given expiry time has
1825 * elapsed. The routine will return immediately unless
1826 * the current task state has been set (see set_current_state()).
1828 * The @delta argument gives the kernel the freedom to schedule the
1829 * actual wakeup to a time that is both power and performance friendly.
1830 * The kernel give the normal best effort behavior for "@expires+@delta",
1831 * but may decide to fire the timer earlier, but no earlier than @expires.
1833 * You can set the task state as follows -
1835 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1836 * pass before the routine returns.
1838 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1839 * delivered to the current task.
1841 * The current task state is guaranteed to be TASK_RUNNING when this
1844 * Returns 0 when the timer has expired otherwise -EINTR
1846 int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1847 const enum hrtimer_mode mode)
1849 return schedule_hrtimeout_range_clock(expires, delta, mode,
1852 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1855 * schedule_hrtimeout - sleep until timeout
1856 * @expires: timeout value (ktime_t)
1857 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1859 * Make the current task sleep until the given expiry time has
1860 * elapsed. The routine will return immediately unless
1861 * the current task state has been set (see set_current_state()).
1863 * You can set the task state as follows -
1865 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1866 * pass before the routine returns.
1868 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1869 * delivered to the current task.
1871 * The current task state is guaranteed to be TASK_RUNNING when this
1874 * Returns 0 when the timer has expired otherwise -EINTR
1876 int __sched schedule_hrtimeout(ktime_t *expires,
1877 const enum hrtimer_mode mode)
1879 return schedule_hrtimeout_range(expires, 0, mode);
1881 EXPORT_SYMBOL_GPL(schedule_hrtimeout);