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
264 # ifndef CONFIG_KTIME_SCALAR
266 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
268 * @nsec: the scalar nsec value to add
270 * Returns the sum of kt and nsec in ktime_t format
272 ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
276 if (likely(nsec < NSEC_PER_SEC)) {
279 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
281 /* Make sure nsec fits into long */
282 if (unlikely(nsec > KTIME_SEC_MAX))
283 return (ktime_t){ .tv64 = KTIME_MAX };
285 tmp = ktime_set((long)nsec, rem);
288 return ktime_add(kt, tmp);
291 EXPORT_SYMBOL_GPL(ktime_add_ns);
294 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
296 * @nsec: the scalar nsec value to subtract
298 * Returns the subtraction of @nsec from @kt in ktime_t format
300 ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
304 if (likely(nsec < NSEC_PER_SEC)) {
307 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
309 tmp = ktime_set((long)nsec, rem);
312 return ktime_sub(kt, tmp);
315 EXPORT_SYMBOL_GPL(ktime_sub_ns);
316 # endif /* !CONFIG_KTIME_SCALAR */
319 * Divide a ktime value by a nanosecond value
321 u64 ktime_divns(const ktime_t kt, s64 div)
326 dclc = ktime_to_ns(kt);
327 /* Make sure the divisor is less than 2^32: */
333 do_div(dclc, (unsigned long) div);
337 #endif /* BITS_PER_LONG >= 64 */
340 * Add two ktime values and do a safety check for overflow:
342 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
344 ktime_t res = ktime_add(lhs, rhs);
347 * We use KTIME_SEC_MAX here, the maximum timeout which we can
348 * return to user space in a timespec:
350 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
351 res = ktime_set(KTIME_SEC_MAX, 0);
356 EXPORT_SYMBOL_GPL(ktime_add_safe);
358 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
360 static struct debug_obj_descr hrtimer_debug_descr;
362 static void *hrtimer_debug_hint(void *addr)
364 return ((struct hrtimer *) addr)->function;
368 * fixup_init is called when:
369 * - an active object is initialized
371 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
373 struct hrtimer *timer = addr;
376 case ODEBUG_STATE_ACTIVE:
377 hrtimer_cancel(timer);
378 debug_object_init(timer, &hrtimer_debug_descr);
386 * fixup_activate is called when:
387 * - an active object is activated
388 * - an unknown object is activated (might be a statically initialized object)
390 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
394 case ODEBUG_STATE_NOTAVAILABLE:
398 case ODEBUG_STATE_ACTIVE:
407 * fixup_free is called when:
408 * - an active object is freed
410 static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
412 struct hrtimer *timer = addr;
415 case ODEBUG_STATE_ACTIVE:
416 hrtimer_cancel(timer);
417 debug_object_free(timer, &hrtimer_debug_descr);
424 static struct debug_obj_descr hrtimer_debug_descr = {
426 .debug_hint = hrtimer_debug_hint,
427 .fixup_init = hrtimer_fixup_init,
428 .fixup_activate = hrtimer_fixup_activate,
429 .fixup_free = hrtimer_fixup_free,
432 static inline void debug_hrtimer_init(struct hrtimer *timer)
434 debug_object_init(timer, &hrtimer_debug_descr);
437 static inline void debug_hrtimer_activate(struct hrtimer *timer)
439 debug_object_activate(timer, &hrtimer_debug_descr);
442 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
444 debug_object_deactivate(timer, &hrtimer_debug_descr);
447 static inline void debug_hrtimer_free(struct hrtimer *timer)
449 debug_object_free(timer, &hrtimer_debug_descr);
452 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
453 enum hrtimer_mode mode);
455 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
456 enum hrtimer_mode mode)
458 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
459 __hrtimer_init(timer, clock_id, mode);
461 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
463 void destroy_hrtimer_on_stack(struct hrtimer *timer)
465 debug_object_free(timer, &hrtimer_debug_descr);
469 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
470 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
471 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
475 debug_init(struct hrtimer *timer, clockid_t clockid,
476 enum hrtimer_mode mode)
478 debug_hrtimer_init(timer);
479 trace_hrtimer_init(timer, clockid, mode);
482 static inline void debug_activate(struct hrtimer *timer)
484 debug_hrtimer_activate(timer);
485 trace_hrtimer_start(timer);
488 static inline void debug_deactivate(struct hrtimer *timer)
490 debug_hrtimer_deactivate(timer);
491 trace_hrtimer_cancel(timer);
494 /* High resolution timer related functions */
495 #ifdef CONFIG_HIGH_RES_TIMERS
498 * High resolution timer enabled ?
500 static int hrtimer_hres_enabled __read_mostly = 1;
503 * Enable / Disable high resolution mode
505 static int __init setup_hrtimer_hres(char *str)
507 if (!strcmp(str, "off"))
508 hrtimer_hres_enabled = 0;
509 else if (!strcmp(str, "on"))
510 hrtimer_hres_enabled = 1;
516 __setup("highres=", setup_hrtimer_hres);
519 * hrtimer_high_res_enabled - query, if the highres mode is enabled
521 static inline int hrtimer_is_hres_enabled(void)
523 return hrtimer_hres_enabled;
527 * Is the high resolution mode active ?
529 static inline int hrtimer_hres_active(void)
531 return __this_cpu_read(hrtimer_bases.hres_active);
535 * Reprogram the event source with checking both queues for the
537 * Called with interrupts disabled and base->lock held
540 hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
543 struct hrtimer_clock_base *base = cpu_base->clock_base;
544 ktime_t expires, expires_next;
546 expires_next.tv64 = KTIME_MAX;
548 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
549 struct hrtimer *timer;
550 struct timerqueue_node *next;
552 next = timerqueue_getnext(&base->active);
555 timer = container_of(next, struct hrtimer, node);
557 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
559 * clock_was_set() has changed base->offset so the
560 * result might be negative. Fix it up to prevent a
561 * false positive in clockevents_program_event()
563 if (expires.tv64 < 0)
565 if (expires.tv64 < expires_next.tv64)
566 expires_next = expires;
569 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
572 cpu_base->expires_next.tv64 = expires_next.tv64;
575 * If a hang was detected in the last timer interrupt then we
576 * leave the hang delay active in the hardware. We want the
577 * system to make progress. That also prevents the following
579 * T1 expires 50ms from now
580 * T2 expires 5s from now
582 * T1 is removed, so this code is called and would reprogram
583 * the hardware to 5s from now. Any hrtimer_start after that
584 * will not reprogram the hardware due to hang_detected being
585 * set. So we'd effectivly block all timers until the T2 event
588 if (cpu_base->hang_detected)
591 if (cpu_base->expires_next.tv64 != KTIME_MAX)
592 tick_program_event(cpu_base->expires_next, 1);
596 * Shared reprogramming for clock_realtime and clock_monotonic
598 * When a timer is enqueued and expires earlier than the already enqueued
599 * timers, we have to check, whether it expires earlier than the timer for
600 * which the clock event device was armed.
602 * Note, that in case the state has HRTIMER_STATE_CALLBACK set, no reprogramming
603 * and no expiry check happens. The timer gets enqueued into the rbtree. The
604 * reprogramming and expiry check is done in the hrtimer_interrupt or in the
607 * Called with interrupts disabled and base->cpu_base.lock held
609 static int hrtimer_reprogram(struct hrtimer *timer,
610 struct hrtimer_clock_base *base)
612 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
613 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
616 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
619 * When the callback is running, we do not reprogram the clock event
620 * device. The timer callback is either running on a different CPU or
621 * the callback is executed in the hrtimer_interrupt context. The
622 * reprogramming is handled either by the softirq, which called the
623 * callback or at the end of the hrtimer_interrupt.
625 if (hrtimer_callback_running(timer))
629 * CLOCK_REALTIME timer might be requested with an absolute
630 * expiry time which is less than base->offset. Nothing wrong
631 * about that, just avoid to call into the tick code, which
632 * has now objections against negative expiry values.
634 if (expires.tv64 < 0)
637 if (expires.tv64 >= cpu_base->expires_next.tv64)
641 * If a hang was detected in the last timer interrupt then we
642 * do not schedule a timer which is earlier than the expiry
643 * which we enforced in the hang detection. We want the system
646 if (cpu_base->hang_detected)
650 * Clockevents returns -ETIME, when the event was in the past.
652 res = tick_program_event(expires, 0);
653 if (!IS_ERR_VALUE(res))
654 cpu_base->expires_next = expires;
659 * Initialize the high resolution related parts of cpu_base
661 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
663 base->expires_next.tv64 = KTIME_MAX;
664 base->hres_active = 0;
667 static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
669 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
670 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
671 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
673 return ktime_get_update_offsets_now(offs_real, offs_boot, offs_tai);
677 * Retrigger next event is called after clock was set
679 * Called with interrupts disabled via on_each_cpu()
681 static void retrigger_next_event(void *arg)
683 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
685 if (!hrtimer_hres_active())
688 raw_spin_lock(&base->lock);
689 hrtimer_update_base(base);
690 hrtimer_force_reprogram(base, 0);
691 raw_spin_unlock(&base->lock);
695 * Switch to high resolution mode
697 static int hrtimer_switch_to_hres(void)
699 int i, cpu = smp_processor_id();
700 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
703 if (base->hres_active)
706 local_irq_save(flags);
708 if (tick_init_highres()) {
709 local_irq_restore(flags);
710 printk(KERN_WARNING "Could not switch to high resolution "
711 "mode on CPU %d\n", cpu);
714 base->hres_active = 1;
715 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
716 base->clock_base[i].resolution = KTIME_HIGH_RES;
718 tick_setup_sched_timer();
719 /* "Retrigger" the interrupt to get things going */
720 retrigger_next_event(NULL);
721 local_irq_restore(flags);
725 static void clock_was_set_work(struct work_struct *work)
730 static DECLARE_WORK(hrtimer_work, clock_was_set_work);
733 * Called from timekeeping and resume code to reprogramm the hrtimer
734 * interrupt device on all cpus.
736 void clock_was_set_delayed(void)
738 schedule_work(&hrtimer_work);
743 static inline int hrtimer_hres_active(void) { return 0; }
744 static inline int hrtimer_is_hres_enabled(void) { return 0; }
745 static inline int hrtimer_switch_to_hres(void) { return 0; }
747 hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
748 static inline int hrtimer_reprogram(struct hrtimer *timer,
749 struct hrtimer_clock_base *base)
753 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
754 static inline void retrigger_next_event(void *arg) { }
756 #endif /* CONFIG_HIGH_RES_TIMERS */
759 * Clock realtime was set
761 * Change the offset of the realtime clock vs. the monotonic
764 * We might have to reprogram the high resolution timer interrupt. On
765 * SMP we call the architecture specific code to retrigger _all_ high
766 * resolution timer interrupts. On UP we just disable interrupts and
767 * call the high resolution interrupt code.
769 void clock_was_set(void)
771 #ifdef CONFIG_HIGH_RES_TIMERS
772 /* Retrigger the CPU local events everywhere */
773 on_each_cpu(retrigger_next_event, NULL, 1);
775 timerfd_clock_was_set();
779 * During resume we might have to reprogram the high resolution timer
780 * interrupt on all online CPUs. However, all other CPUs will be
781 * stopped with IRQs interrupts disabled so the clock_was_set() call
784 void hrtimers_resume(void)
786 WARN_ONCE(!irqs_disabled(),
787 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
789 /* Retrigger on the local CPU */
790 retrigger_next_event(NULL);
791 /* And schedule a retrigger for all others */
792 clock_was_set_delayed();
795 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
797 #ifdef CONFIG_TIMER_STATS
798 if (timer->start_site)
800 timer->start_site = __builtin_return_address(0);
801 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
802 timer->start_pid = current->pid;
806 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
808 #ifdef CONFIG_TIMER_STATS
809 timer->start_site = NULL;
813 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
815 #ifdef CONFIG_TIMER_STATS
816 if (likely(!timer_stats_active))
818 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
819 timer->function, timer->start_comm, 0);
824 * Counterpart to lock_hrtimer_base above:
827 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
829 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
833 * hrtimer_forward - forward the timer expiry
834 * @timer: hrtimer to forward
835 * @now: forward past this time
836 * @interval: the interval to forward
838 * Forward the timer expiry so it will expire in the future.
839 * Returns the number of overruns.
841 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
846 delta = ktime_sub(now, hrtimer_get_expires(timer));
851 if (interval.tv64 < timer->base->resolution.tv64)
852 interval.tv64 = timer->base->resolution.tv64;
854 if (unlikely(delta.tv64 >= interval.tv64)) {
855 s64 incr = ktime_to_ns(interval);
857 orun = ktime_divns(delta, incr);
858 hrtimer_add_expires_ns(timer, incr * orun);
859 if (hrtimer_get_expires_tv64(timer) > now.tv64)
862 * This (and the ktime_add() below) is the
863 * correction for exact:
867 hrtimer_add_expires(timer, interval);
871 EXPORT_SYMBOL_GPL(hrtimer_forward);
874 * enqueue_hrtimer - internal function to (re)start a timer
876 * The timer is inserted in expiry order. Insertion into the
877 * red black tree is O(log(n)). Must hold the base lock.
879 * Returns 1 when the new timer is the leftmost timer in the tree.
881 static int enqueue_hrtimer(struct hrtimer *timer,
882 struct hrtimer_clock_base *base)
884 debug_activate(timer);
886 timerqueue_add(&base->active, &timer->node);
887 base->cpu_base->active_bases |= 1 << base->index;
890 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
891 * state of a possibly running callback.
893 timer->state |= HRTIMER_STATE_ENQUEUED;
895 return (&timer->node == base->active.next);
899 * __remove_hrtimer - internal function to remove a timer
901 * Caller must hold the base lock.
903 * High resolution timer mode reprograms the clock event device when the
904 * timer is the one which expires next. The caller can disable this by setting
905 * reprogram to zero. This is useful, when the context does a reprogramming
906 * anyway (e.g. timer interrupt)
908 static void __remove_hrtimer(struct hrtimer *timer,
909 struct hrtimer_clock_base *base,
910 unsigned long newstate, int reprogram)
912 struct timerqueue_node *next_timer;
913 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
916 next_timer = timerqueue_getnext(&base->active);
917 timerqueue_del(&base->active, &timer->node);
918 if (&timer->node == next_timer) {
919 #ifdef CONFIG_HIGH_RES_TIMERS
920 /* Reprogram the clock event device. if enabled */
921 if (reprogram && hrtimer_hres_active()) {
924 expires = ktime_sub(hrtimer_get_expires(timer),
926 if (base->cpu_base->expires_next.tv64 == expires.tv64)
927 hrtimer_force_reprogram(base->cpu_base, 1);
931 if (!timerqueue_getnext(&base->active))
932 base->cpu_base->active_bases &= ~(1 << base->index);
934 timer->state = newstate;
938 * remove hrtimer, called with base lock held
941 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
943 if (hrtimer_is_queued(timer)) {
948 * Remove the timer and force reprogramming when high
949 * resolution mode is active and the timer is on the current
950 * CPU. If we remove a timer on another CPU, reprogramming is
951 * skipped. The interrupt event on this CPU is fired and
952 * reprogramming happens in the interrupt handler. This is a
953 * rare case and less expensive than a smp call.
955 debug_deactivate(timer);
956 timer_stats_hrtimer_clear_start_info(timer);
957 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
959 * We must preserve the CALLBACK state flag here,
960 * otherwise we could move the timer base in
961 * switch_hrtimer_base.
963 state = timer->state & HRTIMER_STATE_CALLBACK;
964 __remove_hrtimer(timer, base, state, reprogram);
970 int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
971 unsigned long delta_ns, const enum hrtimer_mode mode,
974 struct hrtimer_clock_base *base, *new_base;
978 base = lock_hrtimer_base(timer, &flags);
980 /* Remove an active timer from the queue: */
981 ret = remove_hrtimer(timer, base);
983 if (mode & HRTIMER_MODE_REL) {
984 tim = ktime_add_safe(tim, base->get_time());
986 * CONFIG_TIME_LOW_RES is a temporary way for architectures
987 * to signal that they simply return xtime in
988 * do_gettimeoffset(). In this case we want to round up by
989 * resolution when starting a relative timer, to avoid short
990 * timeouts. This will go away with the GTOD framework.
992 #ifdef CONFIG_TIME_LOW_RES
993 tim = ktime_add_safe(tim, base->resolution);
997 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
999 /* Switch the timer base, if necessary: */
1000 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
1002 timer_stats_hrtimer_set_start_info(timer);
1004 leftmost = enqueue_hrtimer(timer, new_base);
1007 unlock_hrtimer_base(timer, &flags);
1011 if (!hrtimer_is_hres_active(timer)) {
1013 * Kick to reschedule the next tick to handle the new timer
1014 * on dynticks target.
1016 wake_up_nohz_cpu(new_base->cpu_base->cpu);
1017 } else if (new_base->cpu_base == &__get_cpu_var(hrtimer_bases) &&
1018 hrtimer_reprogram(timer, new_base)) {
1020 * Only allow reprogramming if the new base is on this CPU.
1021 * (it might still be on another CPU if the timer was pending)
1023 * XXX send_remote_softirq() ?
1027 * We need to drop cpu_base->lock to avoid a
1028 * lock ordering issue vs. rq->lock.
1030 raw_spin_unlock(&new_base->cpu_base->lock);
1031 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1032 local_irq_restore(flags);
1035 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1039 unlock_hrtimer_base(timer, &flags);
1043 EXPORT_SYMBOL_GPL(__hrtimer_start_range_ns);
1046 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1047 * @timer: the timer to be added
1049 * @delta_ns: "slack" range for the timer
1050 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1051 * relative (HRTIMER_MODE_REL)
1055 * 1 when the timer was active
1057 int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1058 unsigned long delta_ns, const enum hrtimer_mode mode)
1060 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1062 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1065 * hrtimer_start - (re)start an hrtimer on the current CPU
1066 * @timer: the timer to be added
1068 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1069 * relative (HRTIMER_MODE_REL)
1073 * 1 when the timer was active
1076 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1078 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
1080 EXPORT_SYMBOL_GPL(hrtimer_start);
1084 * hrtimer_try_to_cancel - try to deactivate a timer
1085 * @timer: hrtimer to stop
1088 * 0 when the timer was not active
1089 * 1 when the timer was active
1090 * -1 when the timer is currently excuting the callback function and
1093 int hrtimer_try_to_cancel(struct hrtimer *timer)
1095 struct hrtimer_clock_base *base;
1096 unsigned long flags;
1099 base = lock_hrtimer_base(timer, &flags);
1101 if (!hrtimer_callback_running(timer))
1102 ret = remove_hrtimer(timer, base);
1104 unlock_hrtimer_base(timer, &flags);
1109 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1112 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1113 * @timer: the timer to be cancelled
1116 * 0 when the timer was not active
1117 * 1 when the timer was active
1119 int hrtimer_cancel(struct hrtimer *timer)
1122 int ret = hrtimer_try_to_cancel(timer);
1129 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1132 * hrtimer_get_remaining - get remaining time for the timer
1133 * @timer: the timer to read
1135 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1137 unsigned long flags;
1140 lock_hrtimer_base(timer, &flags);
1141 rem = hrtimer_expires_remaining(timer);
1142 unlock_hrtimer_base(timer, &flags);
1146 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1148 #ifdef CONFIG_NO_HZ_COMMON
1150 * hrtimer_get_next_event - get the time until next expiry event
1152 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1155 ktime_t hrtimer_get_next_event(void)
1157 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1158 struct hrtimer_clock_base *base = cpu_base->clock_base;
1159 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1160 unsigned long flags;
1163 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1165 if (!hrtimer_hres_active()) {
1166 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1167 struct hrtimer *timer;
1168 struct timerqueue_node *next;
1170 next = timerqueue_getnext(&base->active);
1174 timer = container_of(next, struct hrtimer, node);
1175 delta.tv64 = hrtimer_get_expires_tv64(timer);
1176 delta = ktime_sub(delta, base->get_time());
1177 if (delta.tv64 < mindelta.tv64)
1178 mindelta.tv64 = delta.tv64;
1182 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1184 if (mindelta.tv64 < 0)
1190 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1191 enum hrtimer_mode mode)
1193 struct hrtimer_cpu_base *cpu_base;
1196 memset(timer, 0, sizeof(struct hrtimer));
1198 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1200 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1201 clock_id = CLOCK_MONOTONIC;
1203 base = hrtimer_clockid_to_base(clock_id);
1204 timer->base = &cpu_base->clock_base[base];
1205 timerqueue_init(&timer->node);
1207 #ifdef CONFIG_TIMER_STATS
1208 timer->start_site = NULL;
1209 timer->start_pid = -1;
1210 memset(timer->start_comm, 0, TASK_COMM_LEN);
1215 * hrtimer_init - initialize a timer to the given clock
1216 * @timer: the timer to be initialized
1217 * @clock_id: the clock to be used
1218 * @mode: timer mode abs/rel
1220 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1221 enum hrtimer_mode mode)
1223 debug_init(timer, clock_id, mode);
1224 __hrtimer_init(timer, clock_id, mode);
1226 EXPORT_SYMBOL_GPL(hrtimer_init);
1229 * hrtimer_get_res - get the timer resolution for a clock
1230 * @which_clock: which clock to query
1231 * @tp: pointer to timespec variable to store the resolution
1233 * Store the resolution of the clock selected by @which_clock in the
1234 * variable pointed to by @tp.
1236 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1238 struct hrtimer_cpu_base *cpu_base;
1239 int base = hrtimer_clockid_to_base(which_clock);
1241 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1242 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
1246 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1248 static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
1250 struct hrtimer_clock_base *base = timer->base;
1251 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1252 enum hrtimer_restart (*fn)(struct hrtimer *);
1255 WARN_ON(!irqs_disabled());
1257 debug_deactivate(timer);
1258 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1259 timer_stats_account_hrtimer(timer);
1260 fn = timer->function;
1263 * Because we run timers from hardirq context, there is no chance
1264 * they get migrated to another cpu, therefore its safe to unlock
1267 raw_spin_unlock(&cpu_base->lock);
1268 trace_hrtimer_expire_entry(timer, now);
1269 restart = fn(timer);
1270 trace_hrtimer_expire_exit(timer);
1271 raw_spin_lock(&cpu_base->lock);
1274 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1275 * we do not reprogramm the event hardware. Happens either in
1276 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1278 if (restart != HRTIMER_NORESTART) {
1279 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1280 enqueue_hrtimer(timer, base);
1283 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1285 timer->state &= ~HRTIMER_STATE_CALLBACK;
1288 #ifdef CONFIG_HIGH_RES_TIMERS
1291 * High resolution timer interrupt
1292 * Called with interrupts disabled
1294 void hrtimer_interrupt(struct clock_event_device *dev)
1296 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1297 ktime_t expires_next, now, entry_time, delta;
1300 BUG_ON(!cpu_base->hres_active);
1301 cpu_base->nr_events++;
1302 dev->next_event.tv64 = KTIME_MAX;
1304 raw_spin_lock(&cpu_base->lock);
1305 entry_time = now = hrtimer_update_base(cpu_base);
1307 expires_next.tv64 = KTIME_MAX;
1309 * We set expires_next to KTIME_MAX here with cpu_base->lock
1310 * held to prevent that a timer is enqueued in our queue via
1311 * the migration code. This does not affect enqueueing of
1312 * timers which run their callback and need to be requeued on
1315 cpu_base->expires_next.tv64 = KTIME_MAX;
1317 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1318 struct hrtimer_clock_base *base;
1319 struct timerqueue_node *node;
1322 if (!(cpu_base->active_bases & (1 << i)))
1325 base = cpu_base->clock_base + i;
1326 basenow = ktime_add(now, base->offset);
1328 while ((node = timerqueue_getnext(&base->active))) {
1329 struct hrtimer *timer;
1331 timer = container_of(node, struct hrtimer, node);
1334 * The immediate goal for using the softexpires is
1335 * minimizing wakeups, not running timers at the
1336 * earliest interrupt after their soft expiration.
1337 * This allows us to avoid using a Priority Search
1338 * Tree, which can answer a stabbing querry for
1339 * overlapping intervals and instead use the simple
1340 * BST we already have.
1341 * We don't add extra wakeups by delaying timers that
1342 * are right-of a not yet expired timer, because that
1343 * timer will have to trigger a wakeup anyway.
1346 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
1349 expires = ktime_sub(hrtimer_get_expires(timer),
1351 if (expires.tv64 < 0)
1352 expires.tv64 = KTIME_MAX;
1353 if (expires.tv64 < expires_next.tv64)
1354 expires_next = expires;
1358 __run_hrtimer(timer, &basenow);
1363 * Store the new expiry value so the migration code can verify
1366 cpu_base->expires_next = expires_next;
1367 raw_spin_unlock(&cpu_base->lock);
1369 /* Reprogramming necessary ? */
1370 if (expires_next.tv64 == KTIME_MAX ||
1371 !tick_program_event(expires_next, 0)) {
1372 cpu_base->hang_detected = 0;
1377 * The next timer was already expired due to:
1379 * - long lasting callbacks
1380 * - being scheduled away when running in a VM
1382 * We need to prevent that we loop forever in the hrtimer
1383 * interrupt routine. We give it 3 attempts to avoid
1384 * overreacting on some spurious event.
1386 * Acquire base lock for updating the offsets and retrieving
1389 raw_spin_lock(&cpu_base->lock);
1390 now = hrtimer_update_base(cpu_base);
1391 cpu_base->nr_retries++;
1395 * Give the system a chance to do something else than looping
1396 * here. We stored the entry time, so we know exactly how long
1397 * we spent here. We schedule the next event this amount of
1400 cpu_base->nr_hangs++;
1401 cpu_base->hang_detected = 1;
1402 raw_spin_unlock(&cpu_base->lock);
1403 delta = ktime_sub(now, entry_time);
1404 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1405 cpu_base->max_hang_time = delta;
1407 * Limit it to a sensible value as we enforce a longer
1408 * delay. Give the CPU at least 100ms to catch up.
1410 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1411 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1413 expires_next = ktime_add(now, delta);
1414 tick_program_event(expires_next, 1);
1415 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1416 ktime_to_ns(delta));
1420 * local version of hrtimer_peek_ahead_timers() called with interrupts
1423 static void __hrtimer_peek_ahead_timers(void)
1425 struct tick_device *td;
1427 if (!hrtimer_hres_active())
1430 td = &__get_cpu_var(tick_cpu_device);
1431 if (td && td->evtdev)
1432 hrtimer_interrupt(td->evtdev);
1436 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1438 * hrtimer_peek_ahead_timers will peek at the timer queue of
1439 * the current cpu and check if there are any timers for which
1440 * the soft expires time has passed. If any such timers exist,
1441 * they are run immediately and then removed from the timer queue.
1444 void hrtimer_peek_ahead_timers(void)
1446 unsigned long flags;
1448 local_irq_save(flags);
1449 __hrtimer_peek_ahead_timers();
1450 local_irq_restore(flags);
1453 static void run_hrtimer_softirq(struct softirq_action *h)
1455 hrtimer_peek_ahead_timers();
1458 #else /* CONFIG_HIGH_RES_TIMERS */
1460 static inline void __hrtimer_peek_ahead_timers(void) { }
1462 #endif /* !CONFIG_HIGH_RES_TIMERS */
1465 * Called from timer softirq every jiffy, expire hrtimers:
1467 * For HRT its the fall back code to run the softirq in the timer
1468 * softirq context in case the hrtimer initialization failed or has
1469 * not been done yet.
1471 void hrtimer_run_pending(void)
1473 if (hrtimer_hres_active())
1477 * This _is_ ugly: We have to check in the softirq context,
1478 * whether we can switch to highres and / or nohz mode. The
1479 * clocksource switch happens in the timer interrupt with
1480 * xtime_lock held. Notification from there only sets the
1481 * check bit in the tick_oneshot code, otherwise we might
1482 * deadlock vs. xtime_lock.
1484 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1485 hrtimer_switch_to_hres();
1489 * Called from hardirq context every jiffy
1491 void hrtimer_run_queues(void)
1493 struct timerqueue_node *node;
1494 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1495 struct hrtimer_clock_base *base;
1496 int index, gettime = 1;
1498 if (hrtimer_hres_active())
1501 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1502 base = &cpu_base->clock_base[index];
1503 if (!timerqueue_getnext(&base->active))
1507 hrtimer_get_softirq_time(cpu_base);
1511 raw_spin_lock(&cpu_base->lock);
1513 while ((node = timerqueue_getnext(&base->active))) {
1514 struct hrtimer *timer;
1516 timer = container_of(node, struct hrtimer, node);
1517 if (base->softirq_time.tv64 <=
1518 hrtimer_get_expires_tv64(timer))
1521 __run_hrtimer(timer, &base->softirq_time);
1523 raw_spin_unlock(&cpu_base->lock);
1528 * Sleep related functions:
1530 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1532 struct hrtimer_sleeper *t =
1533 container_of(timer, struct hrtimer_sleeper, timer);
1534 struct task_struct *task = t->task;
1538 wake_up_process(task);
1540 return HRTIMER_NORESTART;
1543 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1545 sl->timer.function = hrtimer_wakeup;
1548 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
1550 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1552 hrtimer_init_sleeper(t, current);
1555 set_current_state(TASK_INTERRUPTIBLE);
1556 hrtimer_start_expires(&t->timer, mode);
1557 if (!hrtimer_active(&t->timer))
1560 if (likely(t->task))
1561 freezable_schedule();
1563 hrtimer_cancel(&t->timer);
1564 mode = HRTIMER_MODE_ABS;
1566 } while (t->task && !signal_pending(current));
1568 __set_current_state(TASK_RUNNING);
1570 return t->task == NULL;
1573 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1575 struct timespec rmt;
1578 rem = hrtimer_expires_remaining(timer);
1581 rmt = ktime_to_timespec(rem);
1583 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1589 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1591 struct hrtimer_sleeper t;
1592 struct timespec __user *rmtp;
1595 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
1597 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1599 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1602 rmtp = restart->nanosleep.rmtp;
1604 ret = update_rmtp(&t.timer, rmtp);
1609 /* The other values in restart are already filled in */
1610 ret = -ERESTART_RESTARTBLOCK;
1612 destroy_hrtimer_on_stack(&t.timer);
1616 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1617 const enum hrtimer_mode mode, const clockid_t clockid)
1619 struct restart_block *restart;
1620 struct hrtimer_sleeper t;
1622 unsigned long slack;
1624 slack = current->timer_slack_ns;
1625 if (dl_task(current) || rt_task(current))
1628 hrtimer_init_on_stack(&t.timer, clockid, mode);
1629 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1630 if (do_nanosleep(&t, mode))
1633 /* Absolute timers do not update the rmtp value and restart: */
1634 if (mode == HRTIMER_MODE_ABS) {
1635 ret = -ERESTARTNOHAND;
1640 ret = update_rmtp(&t.timer, rmtp);
1645 restart = ¤t_thread_info()->restart_block;
1646 restart->fn = hrtimer_nanosleep_restart;
1647 restart->nanosleep.clockid = t.timer.base->clockid;
1648 restart->nanosleep.rmtp = rmtp;
1649 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1651 ret = -ERESTART_RESTARTBLOCK;
1653 destroy_hrtimer_on_stack(&t.timer);
1657 SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1658 struct timespec __user *, rmtp)
1662 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1665 if (!timespec_valid(&tu))
1668 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1672 * Functions related to boot-time initialization:
1674 static void init_hrtimers_cpu(int cpu)
1676 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1679 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1680 cpu_base->clock_base[i].cpu_base = cpu_base;
1681 timerqueue_init_head(&cpu_base->clock_base[i].active);
1684 cpu_base->cpu = cpu;
1685 hrtimer_init_hres(cpu_base);
1688 #ifdef CONFIG_HOTPLUG_CPU
1690 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1691 struct hrtimer_clock_base *new_base)
1693 struct hrtimer *timer;
1694 struct timerqueue_node *node;
1696 while ((node = timerqueue_getnext(&old_base->active))) {
1697 timer = container_of(node, struct hrtimer, node);
1698 BUG_ON(hrtimer_callback_running(timer));
1699 debug_deactivate(timer);
1702 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1703 * timer could be seen as !active and just vanish away
1704 * under us on another CPU
1706 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
1707 timer->base = new_base;
1709 * Enqueue the timers on the new cpu. This does not
1710 * reprogram the event device in case the timer
1711 * expires before the earliest on this CPU, but we run
1712 * hrtimer_interrupt after we migrated everything to
1713 * sort out already expired timers and reprogram the
1716 enqueue_hrtimer(timer, new_base);
1718 /* Clear the migration state bit */
1719 timer->state &= ~HRTIMER_STATE_MIGRATE;
1723 static void migrate_hrtimers(int scpu)
1725 struct hrtimer_cpu_base *old_base, *new_base;
1728 BUG_ON(cpu_online(scpu));
1729 tick_cancel_sched_timer(scpu);
1731 local_irq_disable();
1732 old_base = &per_cpu(hrtimer_bases, scpu);
1733 new_base = &__get_cpu_var(hrtimer_bases);
1735 * The caller is globally serialized and nobody else
1736 * takes two locks at once, deadlock is not possible.
1738 raw_spin_lock(&new_base->lock);
1739 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1741 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1742 migrate_hrtimer_list(&old_base->clock_base[i],
1743 &new_base->clock_base[i]);
1746 raw_spin_unlock(&old_base->lock);
1747 raw_spin_unlock(&new_base->lock);
1749 /* Check, if we got expired work to do */
1750 __hrtimer_peek_ahead_timers();
1754 #endif /* CONFIG_HOTPLUG_CPU */
1756 static int hrtimer_cpu_notify(struct notifier_block *self,
1757 unsigned long action, void *hcpu)
1759 int scpu = (long)hcpu;
1763 case CPU_UP_PREPARE:
1764 case CPU_UP_PREPARE_FROZEN:
1765 init_hrtimers_cpu(scpu);
1768 #ifdef CONFIG_HOTPLUG_CPU
1770 case CPU_DYING_FROZEN:
1771 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1774 case CPU_DEAD_FROZEN:
1776 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1777 migrate_hrtimers(scpu);
1789 static struct notifier_block hrtimers_nb = {
1790 .notifier_call = hrtimer_cpu_notify,
1793 void __init hrtimers_init(void)
1795 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1796 (void *)(long)smp_processor_id());
1797 register_cpu_notifier(&hrtimers_nb);
1798 #ifdef CONFIG_HIGH_RES_TIMERS
1799 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1804 * schedule_hrtimeout_range_clock - sleep until timeout
1805 * @expires: timeout value (ktime_t)
1806 * @delta: slack in expires timeout (ktime_t)
1807 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1808 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1811 schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1812 const enum hrtimer_mode mode, int clock)
1814 struct hrtimer_sleeper t;
1817 * Optimize when a zero timeout value is given. It does not
1818 * matter whether this is an absolute or a relative time.
1820 if (expires && !expires->tv64) {
1821 __set_current_state(TASK_RUNNING);
1826 * A NULL parameter means "infinite"
1830 __set_current_state(TASK_RUNNING);
1834 hrtimer_init_on_stack(&t.timer, clock, mode);
1835 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1837 hrtimer_init_sleeper(&t, current);
1839 hrtimer_start_expires(&t.timer, mode);
1840 if (!hrtimer_active(&t.timer))
1846 hrtimer_cancel(&t.timer);
1847 destroy_hrtimer_on_stack(&t.timer);
1849 __set_current_state(TASK_RUNNING);
1851 return !t.task ? 0 : -EINTR;
1855 * schedule_hrtimeout_range - sleep until timeout
1856 * @expires: timeout value (ktime_t)
1857 * @delta: slack in expires timeout (ktime_t)
1858 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1860 * Make the current task sleep until the given expiry time has
1861 * elapsed. The routine will return immediately unless
1862 * the current task state has been set (see set_current_state()).
1864 * The @delta argument gives the kernel the freedom to schedule the
1865 * actual wakeup to a time that is both power and performance friendly.
1866 * The kernel give the normal best effort behavior for "@expires+@delta",
1867 * but may decide to fire the timer earlier, but no earlier than @expires.
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_range(ktime_t *expires, unsigned long delta,
1883 const enum hrtimer_mode mode)
1885 return schedule_hrtimeout_range_clock(expires, delta, mode,
1888 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1891 * schedule_hrtimeout - sleep until timeout
1892 * @expires: timeout value (ktime_t)
1893 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1895 * Make the current task sleep until the given expiry time has
1896 * elapsed. The routine will return immediately unless
1897 * the current task state has been set (see set_current_state()).
1899 * You can set the task state as follows -
1901 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1902 * pass before the routine returns.
1904 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1905 * delivered to the current task.
1907 * The current task state is guaranteed to be TASK_RUNNING when this
1910 * Returns 0 when the timer has expired otherwise -EINTR
1912 int __sched schedule_hrtimeout(ktime_t *expires,
1913 const enum hrtimer_mode mode)
1915 return schedule_hrtimeout_range(expires, 0, mode);
1917 EXPORT_SYMBOL_GPL(schedule_hrtimeout);