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/timer.h>
50 #include <asm/uaccess.h>
52 #include <trace/events/timer.h>
57 * There are more clockids then hrtimer bases. Thus, we index
58 * into the timer bases by the hrtimer_base_type enum. When trying
59 * to reach a base using a clockid, hrtimer_clockid_to_base()
60 * is used to convert from clockid to the proper hrtimer_base_type.
62 DEFINE_PER_CPU(struct hrtimer_cpu_base
, hrtimer_bases
) =
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 tmp
= ktime_set((long)nsec
, rem
);
304 return ktime_sub(kt
, tmp
);
307 EXPORT_SYMBOL_GPL(ktime_sub_ns
);
308 # endif /* !CONFIG_KTIME_SCALAR */
311 * Divide a ktime value by a nanosecond value
313 u64
ktime_divns(const ktime_t kt
, s64 div
)
318 dclc
= ktime_to_ns(kt
);
319 /* Make sure the divisor is less than 2^32: */
325 do_div(dclc
, (unsigned long) div
);
329 #endif /* BITS_PER_LONG >= 64 */
332 * Add two ktime values and do a safety check for overflow:
334 ktime_t
ktime_add_safe(const ktime_t lhs
, const ktime_t rhs
)
336 ktime_t res
= ktime_add(lhs
, rhs
);
339 * We use KTIME_SEC_MAX here, the maximum timeout which we can
340 * return to user space in a timespec:
342 if (res
.tv64
< 0 || res
.tv64
< lhs
.tv64
|| res
.tv64
< rhs
.tv64
)
343 res
= ktime_set(KTIME_SEC_MAX
, 0);
348 EXPORT_SYMBOL_GPL(ktime_add_safe
);
350 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
352 static struct debug_obj_descr hrtimer_debug_descr
;
354 static void *hrtimer_debug_hint(void *addr
)
356 return ((struct hrtimer
*) addr
)->function
;
360 * fixup_init is called when:
361 * - an active object is initialized
363 static int hrtimer_fixup_init(void *addr
, enum debug_obj_state state
)
365 struct hrtimer
*timer
= addr
;
368 case ODEBUG_STATE_ACTIVE
:
369 hrtimer_cancel(timer
);
370 debug_object_init(timer
, &hrtimer_debug_descr
);
378 * fixup_activate is called when:
379 * - an active object is activated
380 * - an unknown object is activated (might be a statically initialized object)
382 static int hrtimer_fixup_activate(void *addr
, enum debug_obj_state state
)
386 case ODEBUG_STATE_NOTAVAILABLE
:
390 case ODEBUG_STATE_ACTIVE
:
399 * fixup_free is called when:
400 * - an active object is freed
402 static int hrtimer_fixup_free(void *addr
, enum debug_obj_state state
)
404 struct hrtimer
*timer
= addr
;
407 case ODEBUG_STATE_ACTIVE
:
408 hrtimer_cancel(timer
);
409 debug_object_free(timer
, &hrtimer_debug_descr
);
416 static struct debug_obj_descr hrtimer_debug_descr
= {
418 .debug_hint
= hrtimer_debug_hint
,
419 .fixup_init
= hrtimer_fixup_init
,
420 .fixup_activate
= hrtimer_fixup_activate
,
421 .fixup_free
= hrtimer_fixup_free
,
424 static inline void debug_hrtimer_init(struct hrtimer
*timer
)
426 debug_object_init(timer
, &hrtimer_debug_descr
);
429 static inline void debug_hrtimer_activate(struct hrtimer
*timer
)
431 debug_object_activate(timer
, &hrtimer_debug_descr
);
434 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
)
436 debug_object_deactivate(timer
, &hrtimer_debug_descr
);
439 static inline void debug_hrtimer_free(struct hrtimer
*timer
)
441 debug_object_free(timer
, &hrtimer_debug_descr
);
444 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
445 enum hrtimer_mode mode
);
447 void hrtimer_init_on_stack(struct hrtimer
*timer
, clockid_t clock_id
,
448 enum hrtimer_mode mode
)
450 debug_object_init_on_stack(timer
, &hrtimer_debug_descr
);
451 __hrtimer_init(timer
, clock_id
, mode
);
453 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack
);
455 void destroy_hrtimer_on_stack(struct hrtimer
*timer
)
457 debug_object_free(timer
, &hrtimer_debug_descr
);
461 static inline void debug_hrtimer_init(struct hrtimer
*timer
) { }
462 static inline void debug_hrtimer_activate(struct hrtimer
*timer
) { }
463 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
) { }
467 debug_init(struct hrtimer
*timer
, clockid_t clockid
,
468 enum hrtimer_mode mode
)
470 debug_hrtimer_init(timer
);
471 trace_hrtimer_init(timer
, clockid
, mode
);
474 static inline void debug_activate(struct hrtimer
*timer
)
476 debug_hrtimer_activate(timer
);
477 trace_hrtimer_start(timer
);
480 static inline void debug_deactivate(struct hrtimer
*timer
)
482 debug_hrtimer_deactivate(timer
);
483 trace_hrtimer_cancel(timer
);
486 /* High resolution timer related functions */
487 #ifdef CONFIG_HIGH_RES_TIMERS
490 * High resolution timer enabled ?
492 static int hrtimer_hres_enabled __read_mostly
= 1;
495 * Enable / Disable high resolution mode
497 static int __init
setup_hrtimer_hres(char *str
)
499 if (!strcmp(str
, "off"))
500 hrtimer_hres_enabled
= 0;
501 else if (!strcmp(str
, "on"))
502 hrtimer_hres_enabled
= 1;
508 __setup("highres=", setup_hrtimer_hres
);
511 * hrtimer_high_res_enabled - query, if the highres mode is enabled
513 static inline int hrtimer_is_hres_enabled(void)
515 return hrtimer_hres_enabled
;
519 * Is the high resolution mode active ?
521 static inline int hrtimer_hres_active(void)
523 return __this_cpu_read(hrtimer_bases
.hres_active
);
527 * Reprogram the event source with checking both queues for the
529 * Called with interrupts disabled and base->lock held
532 hrtimer_force_reprogram(struct hrtimer_cpu_base
*cpu_base
, int skip_equal
)
535 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
536 ktime_t expires
, expires_next
;
538 expires_next
.tv64
= KTIME_MAX
;
540 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
541 struct hrtimer
*timer
;
542 struct timerqueue_node
*next
;
544 next
= timerqueue_getnext(&base
->active
);
547 timer
= container_of(next
, struct hrtimer
, node
);
549 expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
551 * clock_was_set() has changed base->offset so the
552 * result might be negative. Fix it up to prevent a
553 * false positive in clockevents_program_event()
555 if (expires
.tv64
< 0)
557 if (expires
.tv64
< expires_next
.tv64
)
558 expires_next
= expires
;
561 if (skip_equal
&& expires_next
.tv64
== cpu_base
->expires_next
.tv64
)
564 cpu_base
->expires_next
.tv64
= expires_next
.tv64
;
566 if (cpu_base
->expires_next
.tv64
!= KTIME_MAX
)
567 tick_program_event(cpu_base
->expires_next
, 1);
571 * Shared reprogramming for clock_realtime and clock_monotonic
573 * When a timer is enqueued and expires earlier than the already enqueued
574 * timers, we have to check, whether it expires earlier than the timer for
575 * which the clock event device was armed.
577 * Called with interrupts disabled and base->cpu_base.lock held
579 static int hrtimer_reprogram(struct hrtimer
*timer
,
580 struct hrtimer_clock_base
*base
)
582 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
583 ktime_t expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
586 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer
) < 0);
589 * When the callback is running, we do not reprogram the clock event
590 * device. The timer callback is either running on a different CPU or
591 * the callback is executed in the hrtimer_interrupt context. The
592 * reprogramming is handled either by the softirq, which called the
593 * callback or at the end of the hrtimer_interrupt.
595 if (hrtimer_callback_running(timer
))
599 * CLOCK_REALTIME timer might be requested with an absolute
600 * expiry time which is less than base->offset. Nothing wrong
601 * about that, just avoid to call into the tick code, which
602 * has now objections against negative expiry values.
604 if (expires
.tv64
< 0)
607 if (expires
.tv64
>= cpu_base
->expires_next
.tv64
)
611 * If a hang was detected in the last timer interrupt then we
612 * do not schedule a timer which is earlier than the expiry
613 * which we enforced in the hang detection. We want the system
616 if (cpu_base
->hang_detected
)
620 * Clockevents returns -ETIME, when the event was in the past.
622 res
= tick_program_event(expires
, 0);
623 if (!IS_ERR_VALUE(res
))
624 cpu_base
->expires_next
= expires
;
629 * Initialize the high resolution related parts of cpu_base
631 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
)
633 base
->expires_next
.tv64
= KTIME_MAX
;
634 base
->hres_active
= 0;
638 * When High resolution timers are active, try to reprogram. Note, that in case
639 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
640 * check happens. The timer gets enqueued into the rbtree. The reprogramming
641 * and expiry check is done in the hrtimer_interrupt or in the softirq.
643 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
644 struct hrtimer_clock_base
*base
,
647 if (base
->cpu_base
->hres_active
&& hrtimer_reprogram(timer
, base
)) {
649 raw_spin_unlock(&base
->cpu_base
->lock
);
650 raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
651 raw_spin_lock(&base
->cpu_base
->lock
);
653 __raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
661 static inline ktime_t
hrtimer_update_base(struct hrtimer_cpu_base
*base
)
663 ktime_t
*offs_real
= &base
->clock_base
[HRTIMER_BASE_REALTIME
].offset
;
664 ktime_t
*offs_boot
= &base
->clock_base
[HRTIMER_BASE_BOOTTIME
].offset
;
666 return ktime_get_update_offsets(offs_real
, offs_boot
);
670 * Retrigger next event is called after clock was set
672 * Called with interrupts disabled via on_each_cpu()
674 static void retrigger_next_event(void *arg
)
676 struct hrtimer_cpu_base
*base
= &__get_cpu_var(hrtimer_bases
);
678 if (!hrtimer_hres_active())
681 raw_spin_lock(&base
->lock
);
682 hrtimer_update_base(base
);
683 hrtimer_force_reprogram(base
, 0);
684 raw_spin_unlock(&base
->lock
);
688 * Switch to high resolution mode
690 static int hrtimer_switch_to_hres(void)
692 int i
, cpu
= smp_processor_id();
693 struct hrtimer_cpu_base
*base
= &per_cpu(hrtimer_bases
, cpu
);
696 if (base
->hres_active
)
699 local_irq_save(flags
);
701 if (tick_init_highres()) {
702 local_irq_restore(flags
);
703 printk(KERN_WARNING
"Could not switch to high resolution "
704 "mode on CPU %d\n", cpu
);
707 base
->hres_active
= 1;
708 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++)
709 base
->clock_base
[i
].resolution
= KTIME_HIGH_RES
;
711 tick_setup_sched_timer();
712 /* "Retrigger" the interrupt to get things going */
713 retrigger_next_event(NULL
);
714 local_irq_restore(flags
);
719 * Called from timekeeping code to reprogramm the hrtimer interrupt
720 * device. If called from the timer interrupt context we defer it to
723 void clock_was_set_delayed(void)
725 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
727 cpu_base
->clock_was_set
= 1;
728 __raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
733 static inline int hrtimer_hres_active(void) { return 0; }
734 static inline int hrtimer_is_hres_enabled(void) { return 0; }
735 static inline int hrtimer_switch_to_hres(void) { return 0; }
737 hrtimer_force_reprogram(struct hrtimer_cpu_base
*base
, int skip_equal
) { }
738 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
739 struct hrtimer_clock_base
*base
,
744 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
) { }
745 static inline void retrigger_next_event(void *arg
) { }
747 #endif /* CONFIG_HIGH_RES_TIMERS */
750 * Clock realtime was set
752 * Change the offset of the realtime clock vs. the monotonic
755 * We might have to reprogram the high resolution timer interrupt. On
756 * SMP we call the architecture specific code to retrigger _all_ high
757 * resolution timer interrupts. On UP we just disable interrupts and
758 * call the high resolution interrupt code.
760 void clock_was_set(void)
762 #ifdef CONFIG_HIGH_RES_TIMERS
763 /* Retrigger the CPU local events everywhere */
764 on_each_cpu(retrigger_next_event
, NULL
, 1);
766 timerfd_clock_was_set();
770 * During resume we might have to reprogram the high resolution timer
771 * interrupt (on the local CPU):
773 void hrtimers_resume(void)
775 WARN_ONCE(!irqs_disabled(),
776 KERN_INFO
"hrtimers_resume() called with IRQs enabled!");
778 retrigger_next_event(NULL
);
779 timerfd_clock_was_set();
782 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
)
784 #ifdef CONFIG_TIMER_STATS
785 if (timer
->start_site
)
787 timer
->start_site
= __builtin_return_address(0);
788 memcpy(timer
->start_comm
, current
->comm
, TASK_COMM_LEN
);
789 timer
->start_pid
= current
->pid
;
793 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer
*timer
)
795 #ifdef CONFIG_TIMER_STATS
796 timer
->start_site
= NULL
;
800 static inline void timer_stats_account_hrtimer(struct hrtimer
*timer
)
802 #ifdef CONFIG_TIMER_STATS
803 if (likely(!timer_stats_active
))
805 timer_stats_update_stats(timer
, timer
->start_pid
, timer
->start_site
,
806 timer
->function
, timer
->start_comm
, 0);
811 * Counterpart to lock_hrtimer_base above:
814 void unlock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
816 raw_spin_unlock_irqrestore(&timer
->base
->cpu_base
->lock
, *flags
);
820 * hrtimer_forward - forward the timer expiry
821 * @timer: hrtimer to forward
822 * @now: forward past this time
823 * @interval: the interval to forward
825 * Forward the timer expiry so it will expire in the future.
826 * Returns the number of overruns.
828 u64
hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
)
833 delta
= ktime_sub(now
, hrtimer_get_expires(timer
));
838 if (interval
.tv64
< timer
->base
->resolution
.tv64
)
839 interval
.tv64
= timer
->base
->resolution
.tv64
;
841 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
842 s64 incr
= ktime_to_ns(interval
);
844 orun
= ktime_divns(delta
, incr
);
845 hrtimer_add_expires_ns(timer
, incr
* orun
);
846 if (hrtimer_get_expires_tv64(timer
) > now
.tv64
)
849 * This (and the ktime_add() below) is the
850 * correction for exact:
854 hrtimer_add_expires(timer
, interval
);
858 EXPORT_SYMBOL_GPL(hrtimer_forward
);
861 * enqueue_hrtimer - internal function to (re)start a timer
863 * The timer is inserted in expiry order. Insertion into the
864 * red black tree is O(log(n)). Must hold the base lock.
866 * Returns 1 when the new timer is the leftmost timer in the tree.
868 static int enqueue_hrtimer(struct hrtimer
*timer
,
869 struct hrtimer_clock_base
*base
)
871 debug_activate(timer
);
873 timerqueue_add(&base
->active
, &timer
->node
);
874 base
->cpu_base
->active_bases
|= 1 << base
->index
;
877 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
878 * state of a possibly running callback.
880 timer
->state
|= HRTIMER_STATE_ENQUEUED
;
882 return (&timer
->node
== base
->active
.next
);
886 * __remove_hrtimer - internal function to remove a timer
888 * Caller must hold the base lock.
890 * High resolution timer mode reprograms the clock event device when the
891 * timer is the one which expires next. The caller can disable this by setting
892 * reprogram to zero. This is useful, when the context does a reprogramming
893 * anyway (e.g. timer interrupt)
895 static void __remove_hrtimer(struct hrtimer
*timer
,
896 struct hrtimer_clock_base
*base
,
897 unsigned long newstate
, int reprogram
)
899 struct timerqueue_node
*next_timer
;
900 if (!(timer
->state
& HRTIMER_STATE_ENQUEUED
))
903 next_timer
= timerqueue_getnext(&base
->active
);
904 timerqueue_del(&base
->active
, &timer
->node
);
905 if (&timer
->node
== next_timer
) {
906 #ifdef CONFIG_HIGH_RES_TIMERS
907 /* Reprogram the clock event device. if enabled */
908 if (reprogram
&& hrtimer_hres_active()) {
911 expires
= ktime_sub(hrtimer_get_expires(timer
),
913 if (base
->cpu_base
->expires_next
.tv64
== expires
.tv64
)
914 hrtimer_force_reprogram(base
->cpu_base
, 1);
918 if (!timerqueue_getnext(&base
->active
))
919 base
->cpu_base
->active_bases
&= ~(1 << base
->index
);
921 timer
->state
= newstate
;
925 * remove hrtimer, called with base lock held
928 remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
)
930 if (hrtimer_is_queued(timer
)) {
935 * Remove the timer and force reprogramming when high
936 * resolution mode is active and the timer is on the current
937 * CPU. If we remove a timer on another CPU, reprogramming is
938 * skipped. The interrupt event on this CPU is fired and
939 * reprogramming happens in the interrupt handler. This is a
940 * rare case and less expensive than a smp call.
942 debug_deactivate(timer
);
943 timer_stats_hrtimer_clear_start_info(timer
);
944 reprogram
= base
->cpu_base
== &__get_cpu_var(hrtimer_bases
);
946 * We must preserve the CALLBACK state flag here,
947 * otherwise we could move the timer base in
948 * switch_hrtimer_base.
950 state
= timer
->state
& HRTIMER_STATE_CALLBACK
;
951 __remove_hrtimer(timer
, base
, state
, reprogram
);
957 int __hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
958 unsigned long delta_ns
, const enum hrtimer_mode mode
,
961 struct hrtimer_clock_base
*base
, *new_base
;
965 base
= lock_hrtimer_base(timer
, &flags
);
967 /* Remove an active timer from the queue: */
968 ret
= remove_hrtimer(timer
, base
);
970 /* Switch the timer base, if necessary: */
971 new_base
= switch_hrtimer_base(timer
, base
, mode
& HRTIMER_MODE_PINNED
);
973 if (mode
& HRTIMER_MODE_REL
) {
974 tim
= ktime_add_safe(tim
, new_base
->get_time());
976 * CONFIG_TIME_LOW_RES is a temporary way for architectures
977 * to signal that they simply return xtime in
978 * do_gettimeoffset(). In this case we want to round up by
979 * resolution when starting a relative timer, to avoid short
980 * timeouts. This will go away with the GTOD framework.
982 #ifdef CONFIG_TIME_LOW_RES
983 tim
= ktime_add_safe(tim
, base
->resolution
);
987 hrtimer_set_expires_range_ns(timer
, tim
, delta_ns
);
989 timer_stats_hrtimer_set_start_info(timer
);
991 leftmost
= enqueue_hrtimer(timer
, new_base
);
994 * Only allow reprogramming if the new base is on this CPU.
995 * (it might still be on another CPU if the timer was pending)
997 * XXX send_remote_softirq() ?
999 if (leftmost
&& new_base
->cpu_base
== &__get_cpu_var(hrtimer_bases
))
1000 hrtimer_enqueue_reprogram(timer
, new_base
, wakeup
);
1002 unlock_hrtimer_base(timer
, &flags
);
1008 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1009 * @timer: the timer to be added
1011 * @delta_ns: "slack" range for the timer
1012 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1016 * 1 when the timer was active
1018 int hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
1019 unsigned long delta_ns
, const enum hrtimer_mode mode
)
1021 return __hrtimer_start_range_ns(timer
, tim
, delta_ns
, mode
, 1);
1023 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns
);
1026 * hrtimer_start - (re)start an hrtimer on the current CPU
1027 * @timer: the timer to be added
1029 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1033 * 1 when the timer was active
1036 hrtimer_start(struct hrtimer
*timer
, ktime_t tim
, const enum hrtimer_mode mode
)
1038 return __hrtimer_start_range_ns(timer
, tim
, 0, mode
, 1);
1040 EXPORT_SYMBOL_GPL(hrtimer_start
);
1044 * hrtimer_try_to_cancel - try to deactivate a timer
1045 * @timer: hrtimer to stop
1048 * 0 when the timer was not active
1049 * 1 when the timer was active
1050 * -1 when the timer is currently excuting the callback function and
1053 int hrtimer_try_to_cancel(struct hrtimer
*timer
)
1055 struct hrtimer_clock_base
*base
;
1056 unsigned long flags
;
1059 base
= lock_hrtimer_base(timer
, &flags
);
1061 if (!hrtimer_callback_running(timer
))
1062 ret
= remove_hrtimer(timer
, base
);
1064 unlock_hrtimer_base(timer
, &flags
);
1069 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel
);
1072 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1073 * @timer: the timer to be cancelled
1076 * 0 when the timer was not active
1077 * 1 when the timer was active
1079 int hrtimer_cancel(struct hrtimer
*timer
)
1082 int ret
= hrtimer_try_to_cancel(timer
);
1089 EXPORT_SYMBOL_GPL(hrtimer_cancel
);
1092 * hrtimer_get_remaining - get remaining time for the timer
1093 * @timer: the timer to read
1095 ktime_t
hrtimer_get_remaining(const struct hrtimer
*timer
)
1097 unsigned long flags
;
1100 lock_hrtimer_base(timer
, &flags
);
1101 rem
= hrtimer_expires_remaining(timer
);
1102 unlock_hrtimer_base(timer
, &flags
);
1106 EXPORT_SYMBOL_GPL(hrtimer_get_remaining
);
1110 * hrtimer_get_next_event - get the time until next expiry event
1112 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1115 ktime_t
hrtimer_get_next_event(void)
1117 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1118 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
1119 ktime_t delta
, mindelta
= { .tv64
= KTIME_MAX
};
1120 unsigned long flags
;
1123 raw_spin_lock_irqsave(&cpu_base
->lock
, flags
);
1125 if (!hrtimer_hres_active()) {
1126 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
1127 struct hrtimer
*timer
;
1128 struct timerqueue_node
*next
;
1130 next
= timerqueue_getnext(&base
->active
);
1134 timer
= container_of(next
, struct hrtimer
, node
);
1135 delta
.tv64
= hrtimer_get_expires_tv64(timer
);
1136 delta
= ktime_sub(delta
, base
->get_time());
1137 if (delta
.tv64
< mindelta
.tv64
)
1138 mindelta
.tv64
= delta
.tv64
;
1142 raw_spin_unlock_irqrestore(&cpu_base
->lock
, flags
);
1144 if (mindelta
.tv64
< 0)
1150 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1151 enum hrtimer_mode mode
)
1153 struct hrtimer_cpu_base
*cpu_base
;
1156 memset(timer
, 0, sizeof(struct hrtimer
));
1158 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1160 if (clock_id
== CLOCK_REALTIME
&& mode
!= HRTIMER_MODE_ABS
)
1161 clock_id
= CLOCK_MONOTONIC
;
1163 base
= hrtimer_clockid_to_base(clock_id
);
1164 timer
->base
= &cpu_base
->clock_base
[base
];
1165 timerqueue_init(&timer
->node
);
1167 #ifdef CONFIG_TIMER_STATS
1168 timer
->start_site
= NULL
;
1169 timer
->start_pid
= -1;
1170 memset(timer
->start_comm
, 0, TASK_COMM_LEN
);
1175 * hrtimer_init - initialize a timer to the given clock
1176 * @timer: the timer to be initialized
1177 * @clock_id: the clock to be used
1178 * @mode: timer mode abs/rel
1180 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1181 enum hrtimer_mode mode
)
1183 debug_init(timer
, clock_id
, mode
);
1184 __hrtimer_init(timer
, clock_id
, mode
);
1186 EXPORT_SYMBOL_GPL(hrtimer_init
);
1189 * hrtimer_get_res - get the timer resolution for a clock
1190 * @which_clock: which clock to query
1191 * @tp: pointer to timespec variable to store the resolution
1193 * Store the resolution of the clock selected by @which_clock in the
1194 * variable pointed to by @tp.
1196 int hrtimer_get_res(const clockid_t which_clock
, struct timespec
*tp
)
1198 struct hrtimer_cpu_base
*cpu_base
;
1199 int base
= hrtimer_clockid_to_base(which_clock
);
1201 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1202 *tp
= ktime_to_timespec(cpu_base
->clock_base
[base
].resolution
);
1206 EXPORT_SYMBOL_GPL(hrtimer_get_res
);
1208 static void __run_hrtimer(struct hrtimer
*timer
, ktime_t
*now
)
1210 struct hrtimer_clock_base
*base
= timer
->base
;
1211 struct hrtimer_cpu_base
*cpu_base
= base
->cpu_base
;
1212 enum hrtimer_restart (*fn
)(struct hrtimer
*);
1215 WARN_ON(!irqs_disabled());
1217 debug_deactivate(timer
);
1218 __remove_hrtimer(timer
, base
, HRTIMER_STATE_CALLBACK
, 0);
1219 timer_stats_account_hrtimer(timer
);
1220 fn
= timer
->function
;
1223 * Because we run timers from hardirq context, there is no chance
1224 * they get migrated to another cpu, therefore its safe to unlock
1227 raw_spin_unlock(&cpu_base
->lock
);
1228 trace_hrtimer_expire_entry(timer
, now
);
1229 restart
= fn(timer
);
1230 trace_hrtimer_expire_exit(timer
);
1231 raw_spin_lock(&cpu_base
->lock
);
1234 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1235 * we do not reprogramm the event hardware. Happens either in
1236 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1238 if (restart
!= HRTIMER_NORESTART
) {
1239 BUG_ON(timer
->state
!= HRTIMER_STATE_CALLBACK
);
1240 enqueue_hrtimer(timer
, base
);
1243 WARN_ON_ONCE(!(timer
->state
& HRTIMER_STATE_CALLBACK
));
1245 timer
->state
&= ~HRTIMER_STATE_CALLBACK
;
1248 #ifdef CONFIG_HIGH_RES_TIMERS
1251 * High resolution timer interrupt
1252 * Called with interrupts disabled
1254 void hrtimer_interrupt(struct clock_event_device
*dev
)
1256 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1257 ktime_t expires_next
, now
, entry_time
, delta
;
1260 BUG_ON(!cpu_base
->hres_active
);
1261 cpu_base
->nr_events
++;
1262 dev
->next_event
.tv64
= KTIME_MAX
;
1264 raw_spin_lock(&cpu_base
->lock
);
1265 entry_time
= now
= hrtimer_update_base(cpu_base
);
1267 expires_next
.tv64
= KTIME_MAX
;
1269 * We set expires_next to KTIME_MAX here with cpu_base->lock
1270 * held to prevent that a timer is enqueued in our queue via
1271 * the migration code. This does not affect enqueueing of
1272 * timers which run their callback and need to be requeued on
1275 cpu_base
->expires_next
.tv64
= KTIME_MAX
;
1277 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1278 struct hrtimer_clock_base
*base
;
1279 struct timerqueue_node
*node
;
1282 if (!(cpu_base
->active_bases
& (1 << i
)))
1285 base
= cpu_base
->clock_base
+ i
;
1286 basenow
= ktime_add(now
, base
->offset
);
1288 while ((node
= timerqueue_getnext(&base
->active
))) {
1289 struct hrtimer
*timer
;
1291 timer
= container_of(node
, struct hrtimer
, node
);
1294 * The immediate goal for using the softexpires is
1295 * minimizing wakeups, not running timers at the
1296 * earliest interrupt after their soft expiration.
1297 * This allows us to avoid using a Priority Search
1298 * Tree, which can answer a stabbing querry for
1299 * overlapping intervals and instead use the simple
1300 * BST we already have.
1301 * We don't add extra wakeups by delaying timers that
1302 * are right-of a not yet expired timer, because that
1303 * timer will have to trigger a wakeup anyway.
1306 if (basenow
.tv64
< hrtimer_get_softexpires_tv64(timer
)) {
1309 expires
= ktime_sub(hrtimer_get_expires(timer
),
1311 if (expires
.tv64
< expires_next
.tv64
)
1312 expires_next
= expires
;
1316 __run_hrtimer(timer
, &basenow
);
1321 * Store the new expiry value so the migration code can verify
1324 cpu_base
->expires_next
= expires_next
;
1325 raw_spin_unlock(&cpu_base
->lock
);
1327 /* Reprogramming necessary ? */
1328 if (expires_next
.tv64
== KTIME_MAX
||
1329 !tick_program_event(expires_next
, 0)) {
1330 cpu_base
->hang_detected
= 0;
1335 * The next timer was already expired due to:
1337 * - long lasting callbacks
1338 * - being scheduled away when running in a VM
1340 * We need to prevent that we loop forever in the hrtimer
1341 * interrupt routine. We give it 3 attempts to avoid
1342 * overreacting on some spurious event.
1344 * Acquire base lock for updating the offsets and retrieving
1347 raw_spin_lock(&cpu_base
->lock
);
1348 now
= hrtimer_update_base(cpu_base
);
1349 cpu_base
->nr_retries
++;
1353 * Give the system a chance to do something else than looping
1354 * here. We stored the entry time, so we know exactly how long
1355 * we spent here. We schedule the next event this amount of
1358 cpu_base
->nr_hangs
++;
1359 cpu_base
->hang_detected
= 1;
1360 raw_spin_unlock(&cpu_base
->lock
);
1361 delta
= ktime_sub(now
, entry_time
);
1362 if (delta
.tv64
> cpu_base
->max_hang_time
.tv64
)
1363 cpu_base
->max_hang_time
= delta
;
1365 * Limit it to a sensible value as we enforce a longer
1366 * delay. Give the CPU at least 100ms to catch up.
1368 if (delta
.tv64
> 100 * NSEC_PER_MSEC
)
1369 expires_next
= ktime_add_ns(now
, 100 * NSEC_PER_MSEC
);
1371 expires_next
= ktime_add(now
, delta
);
1372 tick_program_event(expires_next
, 1);
1373 printk_once(KERN_WARNING
"hrtimer: interrupt took %llu ns\n",
1374 ktime_to_ns(delta
));
1378 * local version of hrtimer_peek_ahead_timers() called with interrupts
1381 static void __hrtimer_peek_ahead_timers(void)
1383 struct tick_device
*td
;
1385 if (!hrtimer_hres_active())
1388 td
= &__get_cpu_var(tick_cpu_device
);
1389 if (td
&& td
->evtdev
)
1390 hrtimer_interrupt(td
->evtdev
);
1394 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1396 * hrtimer_peek_ahead_timers will peek at the timer queue of
1397 * the current cpu and check if there are any timers for which
1398 * the soft expires time has passed. If any such timers exist,
1399 * they are run immediately and then removed from the timer queue.
1402 void hrtimer_peek_ahead_timers(void)
1404 unsigned long flags
;
1406 local_irq_save(flags
);
1407 __hrtimer_peek_ahead_timers();
1408 local_irq_restore(flags
);
1411 static void run_hrtimer_softirq(struct softirq_action
*h
)
1413 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1415 if (cpu_base
->clock_was_set
) {
1416 cpu_base
->clock_was_set
= 0;
1420 hrtimer_peek_ahead_timers();
1423 #else /* CONFIG_HIGH_RES_TIMERS */
1425 static inline void __hrtimer_peek_ahead_timers(void) { }
1427 #endif /* !CONFIG_HIGH_RES_TIMERS */
1430 * Called from timer softirq every jiffy, expire hrtimers:
1432 * For HRT its the fall back code to run the softirq in the timer
1433 * softirq context in case the hrtimer initialization failed or has
1434 * not been done yet.
1436 void hrtimer_run_pending(void)
1438 if (hrtimer_hres_active())
1442 * This _is_ ugly: We have to check in the softirq context,
1443 * whether we can switch to highres and / or nohz mode. The
1444 * clocksource switch happens in the timer interrupt with
1445 * xtime_lock held. Notification from there only sets the
1446 * check bit in the tick_oneshot code, otherwise we might
1447 * deadlock vs. xtime_lock.
1449 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1450 hrtimer_switch_to_hres();
1454 * Called from hardirq context every jiffy
1456 void hrtimer_run_queues(void)
1458 struct timerqueue_node
*node
;
1459 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1460 struct hrtimer_clock_base
*base
;
1461 int index
, gettime
= 1;
1463 if (hrtimer_hres_active())
1466 for (index
= 0; index
< HRTIMER_MAX_CLOCK_BASES
; index
++) {
1467 base
= &cpu_base
->clock_base
[index
];
1468 if (!timerqueue_getnext(&base
->active
))
1472 hrtimer_get_softirq_time(cpu_base
);
1476 raw_spin_lock(&cpu_base
->lock
);
1478 while ((node
= timerqueue_getnext(&base
->active
))) {
1479 struct hrtimer
*timer
;
1481 timer
= container_of(node
, struct hrtimer
, node
);
1482 if (base
->softirq_time
.tv64
<=
1483 hrtimer_get_expires_tv64(timer
))
1486 __run_hrtimer(timer
, &base
->softirq_time
);
1488 raw_spin_unlock(&cpu_base
->lock
);
1493 * Sleep related functions:
1495 static enum hrtimer_restart
hrtimer_wakeup(struct hrtimer
*timer
)
1497 struct hrtimer_sleeper
*t
=
1498 container_of(timer
, struct hrtimer_sleeper
, timer
);
1499 struct task_struct
*task
= t
->task
;
1503 wake_up_process(task
);
1505 return HRTIMER_NORESTART
;
1508 void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
, struct task_struct
*task
)
1510 sl
->timer
.function
= hrtimer_wakeup
;
1513 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper
);
1515 static int __sched
do_nanosleep(struct hrtimer_sleeper
*t
, enum hrtimer_mode mode
)
1517 hrtimer_init_sleeper(t
, current
);
1520 set_current_state(TASK_INTERRUPTIBLE
);
1521 hrtimer_start_expires(&t
->timer
, mode
);
1522 if (!hrtimer_active(&t
->timer
))
1525 if (likely(t
->task
))
1528 hrtimer_cancel(&t
->timer
);
1529 mode
= HRTIMER_MODE_ABS
;
1531 } while (t
->task
&& !signal_pending(current
));
1533 __set_current_state(TASK_RUNNING
);
1535 return t
->task
== NULL
;
1538 static int update_rmtp(struct hrtimer
*timer
, struct timespec __user
*rmtp
)
1540 struct timespec rmt
;
1543 rem
= hrtimer_expires_remaining(timer
);
1546 rmt
= ktime_to_timespec(rem
);
1548 if (copy_to_user(rmtp
, &rmt
, sizeof(*rmtp
)))
1554 long __sched
hrtimer_nanosleep_restart(struct restart_block
*restart
)
1556 struct hrtimer_sleeper t
;
1557 struct timespec __user
*rmtp
;
1560 hrtimer_init_on_stack(&t
.timer
, restart
->nanosleep
.clockid
,
1562 hrtimer_set_expires_tv64(&t
.timer
, restart
->nanosleep
.expires
);
1564 if (do_nanosleep(&t
, HRTIMER_MODE_ABS
))
1567 rmtp
= restart
->nanosleep
.rmtp
;
1569 ret
= update_rmtp(&t
.timer
, rmtp
);
1574 /* The other values in restart are already filled in */
1575 ret
= -ERESTART_RESTARTBLOCK
;
1577 destroy_hrtimer_on_stack(&t
.timer
);
1581 long hrtimer_nanosleep(struct timespec
*rqtp
, struct timespec __user
*rmtp
,
1582 const enum hrtimer_mode mode
, const clockid_t clockid
)
1584 struct restart_block
*restart
;
1585 struct hrtimer_sleeper t
;
1587 unsigned long slack
;
1589 slack
= current
->timer_slack_ns
;
1590 if (rt_task(current
))
1593 hrtimer_init_on_stack(&t
.timer
, clockid
, mode
);
1594 hrtimer_set_expires_range_ns(&t
.timer
, timespec_to_ktime(*rqtp
), slack
);
1595 if (do_nanosleep(&t
, mode
))
1598 /* Absolute timers do not update the rmtp value and restart: */
1599 if (mode
== HRTIMER_MODE_ABS
) {
1600 ret
= -ERESTARTNOHAND
;
1605 ret
= update_rmtp(&t
.timer
, rmtp
);
1610 restart
= ¤t_thread_info()->restart_block
;
1611 restart
->fn
= hrtimer_nanosleep_restart
;
1612 restart
->nanosleep
.clockid
= t
.timer
.base
->clockid
;
1613 restart
->nanosleep
.rmtp
= rmtp
;
1614 restart
->nanosleep
.expires
= hrtimer_get_expires_tv64(&t
.timer
);
1616 ret
= -ERESTART_RESTARTBLOCK
;
1618 destroy_hrtimer_on_stack(&t
.timer
);
1622 SYSCALL_DEFINE2(nanosleep
, struct timespec __user
*, rqtp
,
1623 struct timespec __user
*, rmtp
)
1627 if (copy_from_user(&tu
, rqtp
, sizeof(tu
)))
1630 if (!timespec_valid(&tu
))
1633 return hrtimer_nanosleep(&tu
, rmtp
, HRTIMER_MODE_REL
, CLOCK_MONOTONIC
);
1637 * Functions related to boot-time initialization:
1639 static void __cpuinit
init_hrtimers_cpu(int cpu
)
1641 struct hrtimer_cpu_base
*cpu_base
= &per_cpu(hrtimer_bases
, cpu
);
1644 raw_spin_lock_init(&cpu_base
->lock
);
1646 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1647 cpu_base
->clock_base
[i
].cpu_base
= cpu_base
;
1648 timerqueue_init_head(&cpu_base
->clock_base
[i
].active
);
1651 hrtimer_init_hres(cpu_base
);
1654 #ifdef CONFIG_HOTPLUG_CPU
1656 static void migrate_hrtimer_list(struct hrtimer_clock_base
*old_base
,
1657 struct hrtimer_clock_base
*new_base
)
1659 struct hrtimer
*timer
;
1660 struct timerqueue_node
*node
;
1662 while ((node
= timerqueue_getnext(&old_base
->active
))) {
1663 timer
= container_of(node
, struct hrtimer
, node
);
1664 BUG_ON(hrtimer_callback_running(timer
));
1665 debug_deactivate(timer
);
1668 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1669 * timer could be seen as !active and just vanish away
1670 * under us on another CPU
1672 __remove_hrtimer(timer
, old_base
, HRTIMER_STATE_MIGRATE
, 0);
1673 timer
->base
= new_base
;
1675 * Enqueue the timers on the new cpu. This does not
1676 * reprogram the event device in case the timer
1677 * expires before the earliest on this CPU, but we run
1678 * hrtimer_interrupt after we migrated everything to
1679 * sort out already expired timers and reprogram the
1682 enqueue_hrtimer(timer
, new_base
);
1684 /* Clear the migration state bit */
1685 timer
->state
&= ~HRTIMER_STATE_MIGRATE
;
1689 static void migrate_hrtimers(int scpu
)
1691 struct hrtimer_cpu_base
*old_base
, *new_base
;
1694 BUG_ON(cpu_online(scpu
));
1695 tick_cancel_sched_timer(scpu
);
1697 local_irq_disable();
1698 old_base
= &per_cpu(hrtimer_bases
, scpu
);
1699 new_base
= &__get_cpu_var(hrtimer_bases
);
1701 * The caller is globally serialized and nobody else
1702 * takes two locks at once, deadlock is not possible.
1704 raw_spin_lock(&new_base
->lock
);
1705 raw_spin_lock_nested(&old_base
->lock
, SINGLE_DEPTH_NESTING
);
1707 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1708 migrate_hrtimer_list(&old_base
->clock_base
[i
],
1709 &new_base
->clock_base
[i
]);
1712 raw_spin_unlock(&old_base
->lock
);
1713 raw_spin_unlock(&new_base
->lock
);
1715 /* Check, if we got expired work to do */
1716 __hrtimer_peek_ahead_timers();
1720 #endif /* CONFIG_HOTPLUG_CPU */
1722 static int __cpuinit
hrtimer_cpu_notify(struct notifier_block
*self
,
1723 unsigned long action
, void *hcpu
)
1725 int scpu
= (long)hcpu
;
1729 case CPU_UP_PREPARE
:
1730 case CPU_UP_PREPARE_FROZEN
:
1731 init_hrtimers_cpu(scpu
);
1734 #ifdef CONFIG_HOTPLUG_CPU
1736 case CPU_DYING_FROZEN
:
1737 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING
, &scpu
);
1740 case CPU_DEAD_FROZEN
:
1742 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD
, &scpu
);
1743 migrate_hrtimers(scpu
);
1755 static struct notifier_block __cpuinitdata hrtimers_nb
= {
1756 .notifier_call
= hrtimer_cpu_notify
,
1759 void __init
hrtimers_init(void)
1761 hrtimer_cpu_notify(&hrtimers_nb
, (unsigned long)CPU_UP_PREPARE
,
1762 (void *)(long)smp_processor_id());
1763 register_cpu_notifier(&hrtimers_nb
);
1764 #ifdef CONFIG_HIGH_RES_TIMERS
1765 open_softirq(HRTIMER_SOFTIRQ
, run_hrtimer_softirq
);
1770 * schedule_hrtimeout_range_clock - sleep until timeout
1771 * @expires: timeout value (ktime_t)
1772 * @delta: slack in expires timeout (ktime_t)
1773 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1774 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1777 schedule_hrtimeout_range_clock(ktime_t
*expires
, unsigned long delta
,
1778 const enum hrtimer_mode mode
, int clock
)
1780 struct hrtimer_sleeper t
;
1783 * Optimize when a zero timeout value is given. It does not
1784 * matter whether this is an absolute or a relative time.
1786 if (expires
&& !expires
->tv64
) {
1787 __set_current_state(TASK_RUNNING
);
1792 * A NULL parameter means "infinite"
1796 __set_current_state(TASK_RUNNING
);
1800 hrtimer_init_on_stack(&t
.timer
, clock
, mode
);
1801 hrtimer_set_expires_range_ns(&t
.timer
, *expires
, delta
);
1803 hrtimer_init_sleeper(&t
, current
);
1805 hrtimer_start_expires(&t
.timer
, mode
);
1806 if (!hrtimer_active(&t
.timer
))
1812 hrtimer_cancel(&t
.timer
);
1813 destroy_hrtimer_on_stack(&t
.timer
);
1815 __set_current_state(TASK_RUNNING
);
1817 return !t
.task
? 0 : -EINTR
;
1821 * schedule_hrtimeout_range - sleep until timeout
1822 * @expires: timeout value (ktime_t)
1823 * @delta: slack in expires timeout (ktime_t)
1824 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1826 * Make the current task sleep until the given expiry time has
1827 * elapsed. The routine will return immediately unless
1828 * the current task state has been set (see set_current_state()).
1830 * The @delta argument gives the kernel the freedom to schedule the
1831 * actual wakeup to a time that is both power and performance friendly.
1832 * The kernel give the normal best effort behavior for "@expires+@delta",
1833 * but may decide to fire the timer earlier, but no earlier than @expires.
1835 * You can set the task state as follows -
1837 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1838 * pass before the routine returns.
1840 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1841 * delivered to the current task.
1843 * The current task state is guaranteed to be TASK_RUNNING when this
1846 * Returns 0 when the timer has expired otherwise -EINTR
1848 int __sched
schedule_hrtimeout_range(ktime_t
*expires
, unsigned long delta
,
1849 const enum hrtimer_mode mode
)
1851 return schedule_hrtimeout_range_clock(expires
, delta
, mode
,
1854 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range
);
1857 * schedule_hrtimeout - sleep until timeout
1858 * @expires: timeout value (ktime_t)
1859 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1861 * Make the current task sleep until the given expiry time has
1862 * elapsed. The routine will return immediately unless
1863 * the current task state has been set (see set_current_state()).
1865 * You can set the task state as follows -
1867 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1868 * pass before the routine returns.
1870 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1871 * delivered to the current task.
1873 * The current task state is guaranteed to be TASK_RUNNING when this
1876 * Returns 0 when the timer has expired otherwise -EINTR
1878 int __sched
schedule_hrtimeout(ktime_t
*expires
,
1879 const enum hrtimer_mode mode
)
1881 return schedule_hrtimeout_range(expires
, 0, mode
);
1883 EXPORT_SYMBOL_GPL(schedule_hrtimeout
);