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/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) =
64 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
68 .index = HRTIMER_BASE_MONOTONIC,
69 .clockid = CLOCK_MONOTONIC,
70 .get_time = &ktime_get,
71 .resolution = KTIME_LOW_RES,
74 .index = HRTIMER_BASE_REALTIME,
75 .clockid = CLOCK_REALTIME,
76 .get_time = &ktime_get_real,
77 .resolution = KTIME_LOW_RES,
80 .index = HRTIMER_BASE_BOOTTIME,
81 .clockid = CLOCK_BOOTTIME,
82 .get_time = &ktime_get_boottime,
83 .resolution = KTIME_LOW_RES,
88 static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
89 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
90 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
91 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
94 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
96 return hrtimer_clock_to_base_table[clock_id];
101 * Get the coarse grained time at the softirq based on xtime and
104 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
106 ktime_t xtim, mono, boot;
107 struct timespec xts, tom, slp;
109 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
111 xtim = timespec_to_ktime(xts);
112 mono = ktime_add(xtim, timespec_to_ktime(tom));
113 boot = ktime_add(mono, timespec_to_ktime(slp));
114 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
115 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
116 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
120 * Functions and macros which are different for UP/SMP systems are kept in a
126 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
127 * means that all timers which are tied to this base via timer->base are
128 * locked, and the base itself is locked too.
130 * So __run_timers/migrate_timers can safely modify all timers which could
131 * be found on the lists/queues.
133 * When the timer's base is locked, and the timer removed from list, it is
134 * possible to set timer->base = NULL and drop the lock: the timer remains
138 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
139 unsigned long *flags)
141 struct hrtimer_clock_base *base;
145 if (likely(base != NULL)) {
146 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
147 if (likely(base == timer->base))
149 /* The timer has migrated to another CPU: */
150 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
158 * Get the preferred target CPU for NOHZ
160 static int hrtimer_get_target(int this_cpu, int pinned)
163 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
164 return get_nohz_timer_target();
170 * With HIGHRES=y we do not migrate the timer when it is expiring
171 * before the next event on the target cpu because we cannot reprogram
172 * the target cpu hardware and we would cause it to fire late.
174 * Called with cpu_base->lock of target cpu held.
177 hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
179 #ifdef CONFIG_HIGH_RES_TIMERS
182 if (!new_base->cpu_base->hres_active)
185 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
186 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
193 * Switch the timer base to the current CPU when possible.
195 static inline struct hrtimer_clock_base *
196 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
199 struct hrtimer_clock_base *new_base;
200 struct hrtimer_cpu_base *new_cpu_base;
201 int this_cpu = smp_processor_id();
202 int cpu = hrtimer_get_target(this_cpu, pinned);
203 int basenum = base->index;
206 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
207 new_base = &new_cpu_base->clock_base[basenum];
209 if (base != new_base) {
211 * We are trying to move timer to new_base.
212 * However we can't change timer's base while it is running,
213 * so we keep it on the same CPU. No hassle vs. reprogramming
214 * the event source in the high resolution case. The softirq
215 * code will take care of this when the timer function has
216 * completed. There is no conflict as we hold the lock until
217 * the timer is enqueued.
219 if (unlikely(hrtimer_callback_running(timer)))
222 /* See the comment in lock_timer_base() */
224 raw_spin_unlock(&base->cpu_base->lock);
225 raw_spin_lock(&new_base->cpu_base->lock);
227 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
229 raw_spin_unlock(&new_base->cpu_base->lock);
230 raw_spin_lock(&base->cpu_base->lock);
234 timer->base = new_base;
239 #else /* CONFIG_SMP */
241 static inline struct hrtimer_clock_base *
242 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
244 struct hrtimer_clock_base *base = timer->base;
246 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
251 # define switch_hrtimer_base(t, b, p) (b)
253 #endif /* !CONFIG_SMP */
256 * Functions for the union type storage format of ktime_t which are
257 * too large for inlining:
259 #if BITS_PER_LONG < 64
260 # ifndef CONFIG_KTIME_SCALAR
262 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
264 * @nsec: the scalar nsec value to add
266 * Returns the sum of kt and nsec in ktime_t format
268 ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
272 if (likely(nsec < NSEC_PER_SEC)) {
275 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
277 tmp = ktime_set((long)nsec, rem);
280 return ktime_add(kt, tmp);
283 EXPORT_SYMBOL_GPL(ktime_add_ns);
286 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
288 * @nsec: the scalar nsec value to subtract
290 * Returns the subtraction of @nsec from @kt in ktime_t format
292 ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
296 if (likely(nsec < NSEC_PER_SEC)) {
299 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
301 /* Make sure nsec fits into long */
302 if (unlikely(nsec > KTIME_SEC_MAX))
303 return (ktime_t){ .tv64 = KTIME_MAX };
305 tmp = ktime_set((long)nsec, rem);
308 return ktime_sub(kt, tmp);
311 EXPORT_SYMBOL_GPL(ktime_sub_ns);
312 # endif /* !CONFIG_KTIME_SCALAR */
315 * Divide a ktime value by a nanosecond value
317 u64 ktime_divns(const ktime_t kt, s64 div)
322 dclc = ktime_to_ns(kt);
323 /* Make sure the divisor is less than 2^32: */
329 do_div(dclc, (unsigned long) div);
333 #endif /* BITS_PER_LONG >= 64 */
336 * Add two ktime values and do a safety check for overflow:
338 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
340 ktime_t res = ktime_add(lhs, rhs);
343 * We use KTIME_SEC_MAX here, the maximum timeout which we can
344 * return to user space in a timespec:
346 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
347 res = ktime_set(KTIME_SEC_MAX, 0);
352 EXPORT_SYMBOL_GPL(ktime_add_safe);
354 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
356 static struct debug_obj_descr hrtimer_debug_descr;
358 static void *hrtimer_debug_hint(void *addr)
360 return ((struct hrtimer *) addr)->function;
364 * fixup_init is called when:
365 * - an active object is initialized
367 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
369 struct hrtimer *timer = addr;
372 case ODEBUG_STATE_ACTIVE:
373 hrtimer_cancel(timer);
374 debug_object_init(timer, &hrtimer_debug_descr);
382 * fixup_activate is called when:
383 * - an active object is activated
384 * - an unknown object is activated (might be a statically initialized object)
386 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
390 case ODEBUG_STATE_NOTAVAILABLE:
394 case ODEBUG_STATE_ACTIVE:
403 * fixup_free is called when:
404 * - an active object is freed
406 static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
408 struct hrtimer *timer = addr;
411 case ODEBUG_STATE_ACTIVE:
412 hrtimer_cancel(timer);
413 debug_object_free(timer, &hrtimer_debug_descr);
420 static struct debug_obj_descr hrtimer_debug_descr = {
422 .debug_hint = hrtimer_debug_hint,
423 .fixup_init = hrtimer_fixup_init,
424 .fixup_activate = hrtimer_fixup_activate,
425 .fixup_free = hrtimer_fixup_free,
428 static inline void debug_hrtimer_init(struct hrtimer *timer)
430 debug_object_init(timer, &hrtimer_debug_descr);
433 static inline void debug_hrtimer_activate(struct hrtimer *timer)
435 debug_object_activate(timer, &hrtimer_debug_descr);
438 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
440 debug_object_deactivate(timer, &hrtimer_debug_descr);
443 static inline void debug_hrtimer_free(struct hrtimer *timer)
445 debug_object_free(timer, &hrtimer_debug_descr);
448 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
449 enum hrtimer_mode mode);
451 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
452 enum hrtimer_mode mode)
454 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
455 __hrtimer_init(timer, clock_id, mode);
457 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
459 void destroy_hrtimer_on_stack(struct hrtimer *timer)
461 debug_object_free(timer, &hrtimer_debug_descr);
465 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
466 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
467 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
471 debug_init(struct hrtimer *timer, clockid_t clockid,
472 enum hrtimer_mode mode)
474 debug_hrtimer_init(timer);
475 trace_hrtimer_init(timer, clockid, mode);
478 static inline void debug_activate(struct hrtimer *timer)
480 debug_hrtimer_activate(timer);
481 trace_hrtimer_start(timer);
484 static inline void debug_deactivate(struct hrtimer *timer)
486 debug_hrtimer_deactivate(timer);
487 trace_hrtimer_cancel(timer);
490 /* High resolution timer related functions */
491 #ifdef CONFIG_HIGH_RES_TIMERS
494 * High resolution timer enabled ?
496 static int hrtimer_hres_enabled __read_mostly = 1;
499 * Enable / Disable high resolution mode
501 static int __init setup_hrtimer_hres(char *str)
503 if (!strcmp(str, "off"))
504 hrtimer_hres_enabled = 0;
505 else if (!strcmp(str, "on"))
506 hrtimer_hres_enabled = 1;
512 __setup("highres=", setup_hrtimer_hres);
515 * hrtimer_high_res_enabled - query, if the highres mode is enabled
517 static inline int hrtimer_is_hres_enabled(void)
519 return hrtimer_hres_enabled;
523 * Is the high resolution mode active ?
525 static inline int hrtimer_hres_active(void)
527 return __this_cpu_read(hrtimer_bases.hres_active);
531 * Reprogram the event source with checking both queues for the
533 * Called with interrupts disabled and base->lock held
536 hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
539 struct hrtimer_clock_base *base = cpu_base->clock_base;
540 ktime_t expires, expires_next;
542 expires_next.tv64 = KTIME_MAX;
544 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
545 struct hrtimer *timer;
546 struct timerqueue_node *next;
548 next = timerqueue_getnext(&base->active);
551 timer = container_of(next, struct hrtimer, node);
553 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
555 * clock_was_set() has changed base->offset so the
556 * result might be negative. Fix it up to prevent a
557 * false positive in clockevents_program_event()
559 if (expires.tv64 < 0)
561 if (expires.tv64 < expires_next.tv64)
562 expires_next = expires;
565 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
568 cpu_base->expires_next.tv64 = expires_next.tv64;
570 if (cpu_base->expires_next.tv64 != KTIME_MAX)
571 tick_program_event(cpu_base->expires_next, 1);
575 * Shared reprogramming for clock_realtime and clock_monotonic
577 * When a timer is enqueued and expires earlier than the already enqueued
578 * timers, we have to check, whether it expires earlier than the timer for
579 * which the clock event device was armed.
581 * Called with interrupts disabled and base->cpu_base.lock held
583 static int hrtimer_reprogram(struct hrtimer *timer,
584 struct hrtimer_clock_base *base)
586 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
587 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
590 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
593 * When the callback is running, we do not reprogram the clock event
594 * device. The timer callback is either running on a different CPU or
595 * the callback is executed in the hrtimer_interrupt context. The
596 * reprogramming is handled either by the softirq, which called the
597 * callback or at the end of the hrtimer_interrupt.
599 if (hrtimer_callback_running(timer))
603 * CLOCK_REALTIME timer might be requested with an absolute
604 * expiry time which is less than base->offset. Nothing wrong
605 * about that, just avoid to call into the tick code, which
606 * has now objections against negative expiry values.
608 if (expires.tv64 < 0)
611 if (expires.tv64 >= cpu_base->expires_next.tv64)
615 * If a hang was detected in the last timer interrupt then we
616 * do not schedule a timer which is earlier than the expiry
617 * which we enforced in the hang detection. We want the system
620 if (cpu_base->hang_detected)
624 * Clockevents returns -ETIME, when the event was in the past.
626 res = tick_program_event(expires, 0);
627 if (!IS_ERR_VALUE(res))
628 cpu_base->expires_next = expires;
633 * Initialize the high resolution related parts of cpu_base
635 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
637 base->expires_next.tv64 = KTIME_MAX;
638 base->hres_active = 0;
642 * When High resolution timers are active, try to reprogram. Note, that in case
643 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
644 * check happens. The timer gets enqueued into the rbtree. The reprogramming
645 * and expiry check is done in the hrtimer_interrupt or in the softirq.
647 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
648 struct hrtimer_clock_base *base)
650 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
653 static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
655 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
656 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
658 return ktime_get_update_offsets(offs_real, offs_boot);
662 * Retrigger next event is called after clock was set
664 * Called with interrupts disabled via on_each_cpu()
666 static void retrigger_next_event(void *arg)
668 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
670 if (!hrtimer_hres_active())
673 raw_spin_lock(&base->lock);
674 hrtimer_update_base(base);
675 hrtimer_force_reprogram(base, 0);
676 raw_spin_unlock(&base->lock);
680 * Switch to high resolution mode
682 static int hrtimer_switch_to_hres(void)
684 int i, cpu = smp_processor_id();
685 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
688 if (base->hres_active)
691 local_irq_save(flags);
693 if (tick_init_highres()) {
694 local_irq_restore(flags);
695 printk(KERN_WARNING "Could not switch to high resolution "
696 "mode on CPU %d\n", cpu);
699 base->hres_active = 1;
700 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
701 base->clock_base[i].resolution = KTIME_HIGH_RES;
703 tick_setup_sched_timer();
704 /* "Retrigger" the interrupt to get things going */
705 retrigger_next_event(NULL);
706 local_irq_restore(flags);
711 * Called from timekeeping code to reprogramm the hrtimer interrupt
712 * device. If called from the timer interrupt context we defer it to
715 void clock_was_set_delayed(void)
717 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
719 cpu_base->clock_was_set = 1;
720 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
725 static inline int hrtimer_hres_active(void) { return 0; }
726 static inline int hrtimer_is_hres_enabled(void) { return 0; }
727 static inline int hrtimer_switch_to_hres(void) { return 0; }
729 hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
730 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
731 struct hrtimer_clock_base *base)
735 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
736 static inline void retrigger_next_event(void *arg) { }
738 #endif /* CONFIG_HIGH_RES_TIMERS */
741 * Clock realtime was set
743 * Change the offset of the realtime clock vs. the monotonic
746 * We might have to reprogram the high resolution timer interrupt. On
747 * SMP we call the architecture specific code to retrigger _all_ high
748 * resolution timer interrupts. On UP we just disable interrupts and
749 * call the high resolution interrupt code.
751 void clock_was_set(void)
753 #ifdef CONFIG_HIGH_RES_TIMERS
754 /* Retrigger the CPU local events everywhere */
755 on_each_cpu(retrigger_next_event, NULL, 1);
757 timerfd_clock_was_set();
761 * During resume we might have to reprogram the high resolution timer
762 * interrupt (on the local CPU):
764 void hrtimers_resume(void)
766 WARN_ONCE(!irqs_disabled(),
767 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
769 retrigger_next_event(NULL);
770 timerfd_clock_was_set();
773 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
775 #ifdef CONFIG_TIMER_STATS
776 if (timer->start_site)
778 timer->start_site = __builtin_return_address(0);
779 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
780 timer->start_pid = current->pid;
784 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
786 #ifdef CONFIG_TIMER_STATS
787 timer->start_site = NULL;
791 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
793 #ifdef CONFIG_TIMER_STATS
794 if (likely(!timer_stats_active))
796 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
797 timer->function, timer->start_comm, 0);
802 * Counterpart to lock_hrtimer_base above:
805 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
807 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
811 * hrtimer_forward - forward the timer expiry
812 * @timer: hrtimer to forward
813 * @now: forward past this time
814 * @interval: the interval to forward
816 * Forward the timer expiry so it will expire in the future.
817 * Returns the number of overruns.
819 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
824 delta = ktime_sub(now, hrtimer_get_expires(timer));
829 if (interval.tv64 < timer->base->resolution.tv64)
830 interval.tv64 = timer->base->resolution.tv64;
832 if (unlikely(delta.tv64 >= interval.tv64)) {
833 s64 incr = ktime_to_ns(interval);
835 orun = ktime_divns(delta, incr);
836 hrtimer_add_expires_ns(timer, incr * orun);
837 if (hrtimer_get_expires_tv64(timer) > now.tv64)
840 * This (and the ktime_add() below) is the
841 * correction for exact:
845 hrtimer_add_expires(timer, interval);
849 EXPORT_SYMBOL_GPL(hrtimer_forward);
852 * enqueue_hrtimer - internal function to (re)start a timer
854 * The timer is inserted in expiry order. Insertion into the
855 * red black tree is O(log(n)). Must hold the base lock.
857 * Returns 1 when the new timer is the leftmost timer in the tree.
859 static int enqueue_hrtimer(struct hrtimer *timer,
860 struct hrtimer_clock_base *base)
862 debug_activate(timer);
864 timerqueue_add(&base->active, &timer->node);
865 base->cpu_base->active_bases |= 1 << base->index;
868 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
869 * state of a possibly running callback.
871 timer->state |= HRTIMER_STATE_ENQUEUED;
873 return (&timer->node == base->active.next);
877 * __remove_hrtimer - internal function to remove a timer
879 * Caller must hold the base lock.
881 * High resolution timer mode reprograms the clock event device when the
882 * timer is the one which expires next. The caller can disable this by setting
883 * reprogram to zero. This is useful, when the context does a reprogramming
884 * anyway (e.g. timer interrupt)
886 static void __remove_hrtimer(struct hrtimer *timer,
887 struct hrtimer_clock_base *base,
888 unsigned long newstate, int reprogram)
890 struct timerqueue_node *next_timer;
891 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
894 next_timer = timerqueue_getnext(&base->active);
895 timerqueue_del(&base->active, &timer->node);
896 if (&timer->node == next_timer) {
897 #ifdef CONFIG_HIGH_RES_TIMERS
898 /* Reprogram the clock event device. if enabled */
899 if (reprogram && hrtimer_hres_active()) {
902 expires = ktime_sub(hrtimer_get_expires(timer),
904 if (base->cpu_base->expires_next.tv64 == expires.tv64)
905 hrtimer_force_reprogram(base->cpu_base, 1);
909 if (!timerqueue_getnext(&base->active))
910 base->cpu_base->active_bases &= ~(1 << base->index);
912 timer->state = newstate;
916 * remove hrtimer, called with base lock held
919 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
921 if (hrtimer_is_queued(timer)) {
926 * Remove the timer and force reprogramming when high
927 * resolution mode is active and the timer is on the current
928 * CPU. If we remove a timer on another CPU, reprogramming is
929 * skipped. The interrupt event on this CPU is fired and
930 * reprogramming happens in the interrupt handler. This is a
931 * rare case and less expensive than a smp call.
933 debug_deactivate(timer);
934 timer_stats_hrtimer_clear_start_info(timer);
935 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
937 * We must preserve the CALLBACK state flag here,
938 * otherwise we could move the timer base in
939 * switch_hrtimer_base.
941 state = timer->state & HRTIMER_STATE_CALLBACK;
942 __remove_hrtimer(timer, base, state, reprogram);
948 int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
949 unsigned long delta_ns, const enum hrtimer_mode mode,
952 struct hrtimer_clock_base *base, *new_base;
956 base = lock_hrtimer_base(timer, &flags);
958 /* Remove an active timer from the queue: */
959 ret = remove_hrtimer(timer, base);
961 /* Switch the timer base, if necessary: */
962 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
964 if (mode & HRTIMER_MODE_REL) {
965 tim = ktime_add_safe(tim, new_base->get_time());
967 * CONFIG_TIME_LOW_RES is a temporary way for architectures
968 * to signal that they simply return xtime in
969 * do_gettimeoffset(). In this case we want to round up by
970 * resolution when starting a relative timer, to avoid short
971 * timeouts. This will go away with the GTOD framework.
973 #ifdef CONFIG_TIME_LOW_RES
974 tim = ktime_add_safe(tim, base->resolution);
978 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
980 timer_stats_hrtimer_set_start_info(timer);
982 leftmost = enqueue_hrtimer(timer, new_base);
985 * Only allow reprogramming if the new base is on this CPU.
986 * (it might still be on another CPU if the timer was pending)
988 * XXX send_remote_softirq() ?
990 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
991 && hrtimer_enqueue_reprogram(timer, new_base)) {
994 * We need to drop cpu_base->lock to avoid a
995 * lock ordering issue vs. rq->lock.
997 raw_spin_unlock(&new_base->cpu_base->lock);
998 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
999 local_irq_restore(flags);
1002 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1006 unlock_hrtimer_base(timer, &flags);
1012 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1013 * @timer: the timer to be added
1015 * @delta_ns: "slack" range for the timer
1016 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1020 * 1 when the timer was active
1022 int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1023 unsigned long delta_ns, const enum hrtimer_mode mode)
1025 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1027 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1030 * hrtimer_start - (re)start an hrtimer on the current CPU
1031 * @timer: the timer to be added
1033 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1037 * 1 when the timer was active
1040 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1042 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
1044 EXPORT_SYMBOL_GPL(hrtimer_start);
1048 * hrtimer_try_to_cancel - try to deactivate a timer
1049 * @timer: hrtimer to stop
1052 * 0 when the timer was not active
1053 * 1 when the timer was active
1054 * -1 when the timer is currently excuting the callback function and
1057 int hrtimer_try_to_cancel(struct hrtimer *timer)
1059 struct hrtimer_clock_base *base;
1060 unsigned long flags;
1063 base = lock_hrtimer_base(timer, &flags);
1065 if (!hrtimer_callback_running(timer))
1066 ret = remove_hrtimer(timer, base);
1068 unlock_hrtimer_base(timer, &flags);
1073 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1076 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1077 * @timer: the timer to be cancelled
1080 * 0 when the timer was not active
1081 * 1 when the timer was active
1083 int hrtimer_cancel(struct hrtimer *timer)
1086 int ret = hrtimer_try_to_cancel(timer);
1093 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1096 * hrtimer_get_remaining - get remaining time for the timer
1097 * @timer: the timer to read
1099 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1101 unsigned long flags;
1104 lock_hrtimer_base(timer, &flags);
1105 rem = hrtimer_expires_remaining(timer);
1106 unlock_hrtimer_base(timer, &flags);
1110 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1114 * hrtimer_get_next_event - get the time until next expiry event
1116 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1119 ktime_t hrtimer_get_next_event(void)
1121 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1122 struct hrtimer_clock_base *base = cpu_base->clock_base;
1123 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1124 unsigned long flags;
1127 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1129 if (!hrtimer_hres_active()) {
1130 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1131 struct hrtimer *timer;
1132 struct timerqueue_node *next;
1134 next = timerqueue_getnext(&base->active);
1138 timer = container_of(next, struct hrtimer, node);
1139 delta.tv64 = hrtimer_get_expires_tv64(timer);
1140 delta = ktime_sub(delta, base->get_time());
1141 if (delta.tv64 < mindelta.tv64)
1142 mindelta.tv64 = delta.tv64;
1146 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1148 if (mindelta.tv64 < 0)
1154 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1155 enum hrtimer_mode mode)
1157 struct hrtimer_cpu_base *cpu_base;
1160 memset(timer, 0, sizeof(struct hrtimer));
1162 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1164 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1165 clock_id = CLOCK_MONOTONIC;
1167 base = hrtimer_clockid_to_base(clock_id);
1168 timer->base = &cpu_base->clock_base[base];
1169 timerqueue_init(&timer->node);
1171 #ifdef CONFIG_TIMER_STATS
1172 timer->start_site = NULL;
1173 timer->start_pid = -1;
1174 memset(timer->start_comm, 0, TASK_COMM_LEN);
1179 * hrtimer_init - initialize a timer to the given clock
1180 * @timer: the timer to be initialized
1181 * @clock_id: the clock to be used
1182 * @mode: timer mode abs/rel
1184 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1185 enum hrtimer_mode mode)
1187 debug_init(timer, clock_id, mode);
1188 __hrtimer_init(timer, clock_id, mode);
1190 EXPORT_SYMBOL_GPL(hrtimer_init);
1193 * hrtimer_get_res - get the timer resolution for a clock
1194 * @which_clock: which clock to query
1195 * @tp: pointer to timespec variable to store the resolution
1197 * Store the resolution of the clock selected by @which_clock in the
1198 * variable pointed to by @tp.
1200 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1202 struct hrtimer_cpu_base *cpu_base;
1203 int base = hrtimer_clockid_to_base(which_clock);
1205 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1206 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
1210 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1212 static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
1214 struct hrtimer_clock_base *base = timer->base;
1215 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1216 enum hrtimer_restart (*fn)(struct hrtimer *);
1219 WARN_ON(!irqs_disabled());
1221 debug_deactivate(timer);
1222 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1223 timer_stats_account_hrtimer(timer);
1224 fn = timer->function;
1227 * Because we run timers from hardirq context, there is no chance
1228 * they get migrated to another cpu, therefore its safe to unlock
1231 raw_spin_unlock(&cpu_base->lock);
1232 trace_hrtimer_expire_entry(timer, now);
1233 restart = fn(timer);
1234 trace_hrtimer_expire_exit(timer);
1235 raw_spin_lock(&cpu_base->lock);
1238 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1239 * we do not reprogramm the event hardware. Happens either in
1240 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1242 if (restart != HRTIMER_NORESTART) {
1243 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1244 enqueue_hrtimer(timer, base);
1247 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1249 timer->state &= ~HRTIMER_STATE_CALLBACK;
1252 #ifdef CONFIG_HIGH_RES_TIMERS
1255 * High resolution timer interrupt
1256 * Called with interrupts disabled
1258 void hrtimer_interrupt(struct clock_event_device *dev)
1260 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1261 ktime_t expires_next, now, entry_time, delta;
1264 BUG_ON(!cpu_base->hres_active);
1265 cpu_base->nr_events++;
1266 dev->next_event.tv64 = KTIME_MAX;
1268 raw_spin_lock(&cpu_base->lock);
1269 entry_time = now = hrtimer_update_base(cpu_base);
1271 expires_next.tv64 = KTIME_MAX;
1273 * We set expires_next to KTIME_MAX here with cpu_base->lock
1274 * held to prevent that a timer is enqueued in our queue via
1275 * the migration code. This does not affect enqueueing of
1276 * timers which run their callback and need to be requeued on
1279 cpu_base->expires_next.tv64 = KTIME_MAX;
1281 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1282 struct hrtimer_clock_base *base;
1283 struct timerqueue_node *node;
1286 if (!(cpu_base->active_bases & (1 << i)))
1289 base = cpu_base->clock_base + i;
1290 basenow = ktime_add(now, base->offset);
1292 while ((node = timerqueue_getnext(&base->active))) {
1293 struct hrtimer *timer;
1295 timer = container_of(node, struct hrtimer, node);
1298 * The immediate goal for using the softexpires is
1299 * minimizing wakeups, not running timers at the
1300 * earliest interrupt after their soft expiration.
1301 * This allows us to avoid using a Priority Search
1302 * Tree, which can answer a stabbing querry for
1303 * overlapping intervals and instead use the simple
1304 * BST we already have.
1305 * We don't add extra wakeups by delaying timers that
1306 * are right-of a not yet expired timer, because that
1307 * timer will have to trigger a wakeup anyway.
1310 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
1313 expires = ktime_sub(hrtimer_get_expires(timer),
1315 if (expires.tv64 < 0)
1316 expires.tv64 = KTIME_MAX;
1317 if (expires.tv64 < expires_next.tv64)
1318 expires_next = expires;
1322 __run_hrtimer(timer, &basenow);
1327 * Store the new expiry value so the migration code can verify
1330 cpu_base->expires_next = expires_next;
1331 raw_spin_unlock(&cpu_base->lock);
1333 /* Reprogramming necessary ? */
1334 if (expires_next.tv64 == KTIME_MAX ||
1335 !tick_program_event(expires_next, 0)) {
1336 cpu_base->hang_detected = 0;
1341 * The next timer was already expired due to:
1343 * - long lasting callbacks
1344 * - being scheduled away when running in a VM
1346 * We need to prevent that we loop forever in the hrtimer
1347 * interrupt routine. We give it 3 attempts to avoid
1348 * overreacting on some spurious event.
1350 * Acquire base lock for updating the offsets and retrieving
1353 raw_spin_lock(&cpu_base->lock);
1354 now = hrtimer_update_base(cpu_base);
1355 cpu_base->nr_retries++;
1359 * Give the system a chance to do something else than looping
1360 * here. We stored the entry time, so we know exactly how long
1361 * we spent here. We schedule the next event this amount of
1364 cpu_base->nr_hangs++;
1365 cpu_base->hang_detected = 1;
1366 raw_spin_unlock(&cpu_base->lock);
1367 delta = ktime_sub(now, entry_time);
1368 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1369 cpu_base->max_hang_time = delta;
1371 * Limit it to a sensible value as we enforce a longer
1372 * delay. Give the CPU at least 100ms to catch up.
1374 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1375 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1377 expires_next = ktime_add(now, delta);
1378 tick_program_event(expires_next, 1);
1379 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1380 ktime_to_ns(delta));
1384 * local version of hrtimer_peek_ahead_timers() called with interrupts
1387 static void __hrtimer_peek_ahead_timers(void)
1389 struct tick_device *td;
1391 if (!hrtimer_hres_active())
1394 td = &__get_cpu_var(tick_cpu_device);
1395 if (td && td->evtdev)
1396 hrtimer_interrupt(td->evtdev);
1400 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1402 * hrtimer_peek_ahead_timers will peek at the timer queue of
1403 * the current cpu and check if there are any timers for which
1404 * the soft expires time has passed. If any such timers exist,
1405 * they are run immediately and then removed from the timer queue.
1408 void hrtimer_peek_ahead_timers(void)
1410 unsigned long flags;
1412 local_irq_save(flags);
1413 __hrtimer_peek_ahead_timers();
1414 local_irq_restore(flags);
1417 static void run_hrtimer_softirq(struct softirq_action *h)
1419 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1421 if (cpu_base->clock_was_set) {
1422 cpu_base->clock_was_set = 0;
1426 hrtimer_peek_ahead_timers();
1429 #else /* CONFIG_HIGH_RES_TIMERS */
1431 static inline void __hrtimer_peek_ahead_timers(void) { }
1433 #endif /* !CONFIG_HIGH_RES_TIMERS */
1436 * Called from timer softirq every jiffy, expire hrtimers:
1438 * For HRT its the fall back code to run the softirq in the timer
1439 * softirq context in case the hrtimer initialization failed or has
1440 * not been done yet.
1442 void hrtimer_run_pending(void)
1444 if (hrtimer_hres_active())
1448 * This _is_ ugly: We have to check in the softirq context,
1449 * whether we can switch to highres and / or nohz mode. The
1450 * clocksource switch happens in the timer interrupt with
1451 * xtime_lock held. Notification from there only sets the
1452 * check bit in the tick_oneshot code, otherwise we might
1453 * deadlock vs. xtime_lock.
1455 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1456 hrtimer_switch_to_hres();
1460 * Called from hardirq context every jiffy
1462 void hrtimer_run_queues(void)
1464 struct timerqueue_node *node;
1465 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1466 struct hrtimer_clock_base *base;
1467 int index, gettime = 1;
1469 if (hrtimer_hres_active())
1472 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1473 base = &cpu_base->clock_base[index];
1474 if (!timerqueue_getnext(&base->active))
1478 hrtimer_get_softirq_time(cpu_base);
1482 raw_spin_lock(&cpu_base->lock);
1484 while ((node = timerqueue_getnext(&base->active))) {
1485 struct hrtimer *timer;
1487 timer = container_of(node, struct hrtimer, node);
1488 if (base->softirq_time.tv64 <=
1489 hrtimer_get_expires_tv64(timer))
1492 __run_hrtimer(timer, &base->softirq_time);
1494 raw_spin_unlock(&cpu_base->lock);
1499 * Sleep related functions:
1501 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1503 struct hrtimer_sleeper *t =
1504 container_of(timer, struct hrtimer_sleeper, timer);
1505 struct task_struct *task = t->task;
1509 wake_up_process(task);
1511 return HRTIMER_NORESTART;
1514 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1516 sl->timer.function = hrtimer_wakeup;
1519 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
1521 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1523 hrtimer_init_sleeper(t, current);
1526 set_current_state(TASK_INTERRUPTIBLE);
1527 hrtimer_start_expires(&t->timer, mode);
1528 if (!hrtimer_active(&t->timer))
1531 if (likely(t->task))
1534 hrtimer_cancel(&t->timer);
1535 mode = HRTIMER_MODE_ABS;
1537 } while (t->task && !signal_pending(current));
1539 __set_current_state(TASK_RUNNING);
1541 return t->task == NULL;
1544 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1546 struct timespec rmt;
1549 rem = hrtimer_expires_remaining(timer);
1552 rmt = ktime_to_timespec(rem);
1554 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1560 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1562 struct hrtimer_sleeper t;
1563 struct timespec __user *rmtp;
1566 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
1568 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1570 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1573 rmtp = restart->nanosleep.rmtp;
1575 ret = update_rmtp(&t.timer, rmtp);
1580 /* The other values in restart are already filled in */
1581 ret = -ERESTART_RESTARTBLOCK;
1583 destroy_hrtimer_on_stack(&t.timer);
1587 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1588 const enum hrtimer_mode mode, const clockid_t clockid)
1590 struct restart_block *restart;
1591 struct hrtimer_sleeper t;
1593 unsigned long slack;
1595 slack = current->timer_slack_ns;
1596 if (rt_task(current))
1599 hrtimer_init_on_stack(&t.timer, clockid, mode);
1600 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1601 if (do_nanosleep(&t, mode))
1604 /* Absolute timers do not update the rmtp value and restart: */
1605 if (mode == HRTIMER_MODE_ABS) {
1606 ret = -ERESTARTNOHAND;
1611 ret = update_rmtp(&t.timer, rmtp);
1616 restart = ¤t_thread_info()->restart_block;
1617 restart->fn = hrtimer_nanosleep_restart;
1618 restart->nanosleep.clockid = t.timer.base->clockid;
1619 restart->nanosleep.rmtp = rmtp;
1620 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1622 ret = -ERESTART_RESTARTBLOCK;
1624 destroy_hrtimer_on_stack(&t.timer);
1628 SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1629 struct timespec __user *, rmtp)
1633 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1636 if (!timespec_valid(&tu))
1639 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1643 * Functions related to boot-time initialization:
1645 static void __cpuinit init_hrtimers_cpu(int cpu)
1647 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1650 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1651 cpu_base->clock_base[i].cpu_base = cpu_base;
1652 timerqueue_init_head(&cpu_base->clock_base[i].active);
1655 hrtimer_init_hres(cpu_base);
1658 #ifdef CONFIG_HOTPLUG_CPU
1660 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1661 struct hrtimer_clock_base *new_base)
1663 struct hrtimer *timer;
1664 struct timerqueue_node *node;
1666 while ((node = timerqueue_getnext(&old_base->active))) {
1667 timer = container_of(node, struct hrtimer, node);
1668 BUG_ON(hrtimer_callback_running(timer));
1669 debug_deactivate(timer);
1672 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1673 * timer could be seen as !active and just vanish away
1674 * under us on another CPU
1676 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
1677 timer->base = new_base;
1679 * Enqueue the timers on the new cpu. This does not
1680 * reprogram the event device in case the timer
1681 * expires before the earliest on this CPU, but we run
1682 * hrtimer_interrupt after we migrated everything to
1683 * sort out already expired timers and reprogram the
1686 enqueue_hrtimer(timer, new_base);
1688 /* Clear the migration state bit */
1689 timer->state &= ~HRTIMER_STATE_MIGRATE;
1693 static void migrate_hrtimers(int scpu)
1695 struct hrtimer_cpu_base *old_base, *new_base;
1698 BUG_ON(cpu_online(scpu));
1699 tick_cancel_sched_timer(scpu);
1701 local_irq_disable();
1702 old_base = &per_cpu(hrtimer_bases, scpu);
1703 new_base = &__get_cpu_var(hrtimer_bases);
1705 * The caller is globally serialized and nobody else
1706 * takes two locks at once, deadlock is not possible.
1708 raw_spin_lock(&new_base->lock);
1709 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1711 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1712 migrate_hrtimer_list(&old_base->clock_base[i],
1713 &new_base->clock_base[i]);
1716 raw_spin_unlock(&old_base->lock);
1717 raw_spin_unlock(&new_base->lock);
1719 /* Check, if we got expired work to do */
1720 __hrtimer_peek_ahead_timers();
1724 #endif /* CONFIG_HOTPLUG_CPU */
1726 static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
1727 unsigned long action, void *hcpu)
1729 int scpu = (long)hcpu;
1733 case CPU_UP_PREPARE:
1734 case CPU_UP_PREPARE_FROZEN:
1735 init_hrtimers_cpu(scpu);
1738 #ifdef CONFIG_HOTPLUG_CPU
1740 case CPU_DYING_FROZEN:
1741 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1744 case CPU_DEAD_FROZEN:
1746 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1747 migrate_hrtimers(scpu);
1759 static struct notifier_block __cpuinitdata hrtimers_nb = {
1760 .notifier_call = hrtimer_cpu_notify,
1763 void __init hrtimers_init(void)
1765 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1766 (void *)(long)smp_processor_id());
1767 register_cpu_notifier(&hrtimers_nb);
1768 #ifdef CONFIG_HIGH_RES_TIMERS
1769 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1774 * schedule_hrtimeout_range_clock - sleep until timeout
1775 * @expires: timeout value (ktime_t)
1776 * @delta: slack in expires timeout (ktime_t)
1777 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1778 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1781 schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1782 const enum hrtimer_mode mode, int clock)
1784 struct hrtimer_sleeper t;
1787 * Optimize when a zero timeout value is given. It does not
1788 * matter whether this is an absolute or a relative time.
1790 if (expires && !expires->tv64) {
1791 __set_current_state(TASK_RUNNING);
1796 * A NULL parameter means "infinite"
1800 __set_current_state(TASK_RUNNING);
1804 hrtimer_init_on_stack(&t.timer, clock, mode);
1805 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1807 hrtimer_init_sleeper(&t, current);
1809 hrtimer_start_expires(&t.timer, mode);
1810 if (!hrtimer_active(&t.timer))
1816 hrtimer_cancel(&t.timer);
1817 destroy_hrtimer_on_stack(&t.timer);
1819 __set_current_state(TASK_RUNNING);
1821 return !t.task ? 0 : -EINTR;
1825 * schedule_hrtimeout_range - sleep until timeout
1826 * @expires: timeout value (ktime_t)
1827 * @delta: slack in expires timeout (ktime_t)
1828 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1830 * Make the current task sleep until the given expiry time has
1831 * elapsed. The routine will return immediately unless
1832 * the current task state has been set (see set_current_state()).
1834 * The @delta argument gives the kernel the freedom to schedule the
1835 * actual wakeup to a time that is both power and performance friendly.
1836 * The kernel give the normal best effort behavior for "@expires+@delta",
1837 * but may decide to fire the timer earlier, but no earlier than @expires.
1839 * You can set the task state as follows -
1841 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1842 * pass before the routine returns.
1844 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1845 * delivered to the current task.
1847 * The current task state is guaranteed to be TASK_RUNNING when this
1850 * Returns 0 when the timer has expired otherwise -EINTR
1852 int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1853 const enum hrtimer_mode mode)
1855 return schedule_hrtimeout_range_clock(expires, delta, mode,
1858 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1861 * schedule_hrtimeout - sleep until timeout
1862 * @expires: timeout value (ktime_t)
1863 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1865 * Make the current task sleep until the given expiry time has
1866 * elapsed. The routine will return immediately unless
1867 * the current task state has been set (see set_current_state()).
1869 * You can set the task state as follows -
1871 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1872 * pass before the routine returns.
1874 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1875 * delivered to the current task.
1877 * The current task state is guaranteed to be TASK_RUNNING when this
1880 * Returns 0 when the timer has expired otherwise -EINTR
1882 int __sched schedule_hrtimeout(ktime_t *expires,
1883 const enum hrtimer_mode mode)
1885 return schedule_hrtimeout_range(expires, 0, mode);
1887 EXPORT_SYMBOL_GPL(schedule_hrtimeout);