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/module.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>
47 #include <asm/uaccess.h>
52 * Note: If we want to add new timer bases, we have to skip the two
53 * clock ids captured by the cpu-timers. We do this by holding empty
54 * entries rather than doing math adjustment of the clock ids.
55 * This ensures that we capture erroneous accesses to these clock ids
56 * rather than moving them into the range of valid clock id's.
58 DEFINE_PER_CPU(struct hrtimer_cpu_base
, hrtimer_bases
) =
64 .index
= CLOCK_REALTIME
,
65 .get_time
= &ktime_get_real
,
66 .resolution
= KTIME_LOW_RES
,
69 .index
= CLOCK_MONOTONIC
,
70 .get_time
= &ktime_get
,
71 .resolution
= KTIME_LOW_RES
,
77 * Get the coarse grained time at the softirq based on xtime and
80 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base
*base
)
83 struct timespec xts
, tom
;
87 seq
= read_seqbegin(&xtime_lock
);
88 xts
= current_kernel_time();
89 tom
= wall_to_monotonic
;
90 } while (read_seqretry(&xtime_lock
, seq
));
92 xtim
= timespec_to_ktime(xts
);
93 tomono
= timespec_to_ktime(tom
);
94 base
->clock_base
[CLOCK_REALTIME
].softirq_time
= xtim
;
95 base
->clock_base
[CLOCK_MONOTONIC
].softirq_time
=
96 ktime_add(xtim
, tomono
);
100 * Functions and macros which are different for UP/SMP systems are kept in a
106 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
107 * means that all timers which are tied to this base via timer->base are
108 * locked, and the base itself is locked too.
110 * So __run_timers/migrate_timers can safely modify all timers which could
111 * be found on the lists/queues.
113 * When the timer's base is locked, and the timer removed from list, it is
114 * possible to set timer->base = NULL and drop the lock: the timer remains
118 struct hrtimer_clock_base
*lock_hrtimer_base(const struct hrtimer
*timer
,
119 unsigned long *flags
)
121 struct hrtimer_clock_base
*base
;
125 if (likely(base
!= NULL
)) {
126 spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
127 if (likely(base
== timer
->base
))
129 /* The timer has migrated to another CPU: */
130 spin_unlock_irqrestore(&base
->cpu_base
->lock
, *flags
);
137 * Switch the timer base to the current CPU when possible.
139 static inline struct hrtimer_clock_base
*
140 switch_hrtimer_base(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
)
142 struct hrtimer_clock_base
*new_base
;
143 struct hrtimer_cpu_base
*new_cpu_base
;
145 new_cpu_base
= &__get_cpu_var(hrtimer_bases
);
146 new_base
= &new_cpu_base
->clock_base
[base
->index
];
148 if (base
!= new_base
) {
150 * We are trying to schedule the timer on the local CPU.
151 * However we can't change timer's base while it is running,
152 * so we keep it on the same CPU. No hassle vs. reprogramming
153 * the event source in the high resolution case. The softirq
154 * code will take care of this when the timer function has
155 * completed. There is no conflict as we hold the lock until
156 * the timer is enqueued.
158 if (unlikely(hrtimer_callback_running(timer
)))
161 /* See the comment in lock_timer_base() */
163 spin_unlock(&base
->cpu_base
->lock
);
164 spin_lock(&new_base
->cpu_base
->lock
);
165 timer
->base
= new_base
;
170 #else /* CONFIG_SMP */
172 static inline struct hrtimer_clock_base
*
173 lock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
175 struct hrtimer_clock_base
*base
= timer
->base
;
177 spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
182 # define switch_hrtimer_base(t, b) (b)
184 #endif /* !CONFIG_SMP */
187 * Functions for the union type storage format of ktime_t which are
188 * too large for inlining:
190 #if BITS_PER_LONG < 64
191 # ifndef CONFIG_KTIME_SCALAR
193 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
195 * @nsec: the scalar nsec value to add
197 * Returns the sum of kt and nsec in ktime_t format
199 ktime_t
ktime_add_ns(const ktime_t kt
, u64 nsec
)
203 if (likely(nsec
< NSEC_PER_SEC
)) {
206 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
208 tmp
= ktime_set((long)nsec
, rem
);
211 return ktime_add(kt
, tmp
);
214 EXPORT_SYMBOL_GPL(ktime_add_ns
);
217 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
219 * @nsec: the scalar nsec value to subtract
221 * Returns the subtraction of @nsec from @kt in ktime_t format
223 ktime_t
ktime_sub_ns(const ktime_t kt
, u64 nsec
)
227 if (likely(nsec
< NSEC_PER_SEC
)) {
230 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
232 tmp
= ktime_set((long)nsec
, rem
);
235 return ktime_sub(kt
, tmp
);
238 EXPORT_SYMBOL_GPL(ktime_sub_ns
);
239 # endif /* !CONFIG_KTIME_SCALAR */
242 * Divide a ktime value by a nanosecond value
244 u64
ktime_divns(const ktime_t kt
, s64 div
)
249 dclc
= ktime_to_ns(kt
);
250 /* Make sure the divisor is less than 2^32: */
256 do_div(dclc
, (unsigned long) div
);
260 #endif /* BITS_PER_LONG >= 64 */
263 * Add two ktime values and do a safety check for overflow:
265 ktime_t
ktime_add_safe(const ktime_t lhs
, const ktime_t rhs
)
267 ktime_t res
= ktime_add(lhs
, rhs
);
270 * We use KTIME_SEC_MAX here, the maximum timeout which we can
271 * return to user space in a timespec:
273 if (res
.tv64
< 0 || res
.tv64
< lhs
.tv64
|| res
.tv64
< rhs
.tv64
)
274 res
= ktime_set(KTIME_SEC_MAX
, 0);
279 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
281 static struct debug_obj_descr hrtimer_debug_descr
;
284 * fixup_init is called when:
285 * - an active object is initialized
287 static int hrtimer_fixup_init(void *addr
, enum debug_obj_state state
)
289 struct hrtimer
*timer
= addr
;
292 case ODEBUG_STATE_ACTIVE
:
293 hrtimer_cancel(timer
);
294 debug_object_init(timer
, &hrtimer_debug_descr
);
302 * fixup_activate is called when:
303 * - an active object is activated
304 * - an unknown object is activated (might be a statically initialized object)
306 static int hrtimer_fixup_activate(void *addr
, enum debug_obj_state state
)
310 case ODEBUG_STATE_NOTAVAILABLE
:
314 case ODEBUG_STATE_ACTIVE
:
323 * fixup_free is called when:
324 * - an active object is freed
326 static int hrtimer_fixup_free(void *addr
, enum debug_obj_state state
)
328 struct hrtimer
*timer
= addr
;
331 case ODEBUG_STATE_ACTIVE
:
332 hrtimer_cancel(timer
);
333 debug_object_free(timer
, &hrtimer_debug_descr
);
340 static struct debug_obj_descr hrtimer_debug_descr
= {
342 .fixup_init
= hrtimer_fixup_init
,
343 .fixup_activate
= hrtimer_fixup_activate
,
344 .fixup_free
= hrtimer_fixup_free
,
347 static inline void debug_hrtimer_init(struct hrtimer
*timer
)
349 debug_object_init(timer
, &hrtimer_debug_descr
);
352 static inline void debug_hrtimer_activate(struct hrtimer
*timer
)
354 debug_object_activate(timer
, &hrtimer_debug_descr
);
357 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
)
359 debug_object_deactivate(timer
, &hrtimer_debug_descr
);
362 static inline void debug_hrtimer_free(struct hrtimer
*timer
)
364 debug_object_free(timer
, &hrtimer_debug_descr
);
367 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
368 enum hrtimer_mode mode
);
370 void hrtimer_init_on_stack(struct hrtimer
*timer
, clockid_t clock_id
,
371 enum hrtimer_mode mode
)
373 debug_object_init_on_stack(timer
, &hrtimer_debug_descr
);
374 __hrtimer_init(timer
, clock_id
, mode
);
377 void destroy_hrtimer_on_stack(struct hrtimer
*timer
)
379 debug_object_free(timer
, &hrtimer_debug_descr
);
383 static inline void debug_hrtimer_init(struct hrtimer
*timer
) { }
384 static inline void debug_hrtimer_activate(struct hrtimer
*timer
) { }
385 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
) { }
388 /* High resolution timer related functions */
389 #ifdef CONFIG_HIGH_RES_TIMERS
392 * High resolution timer enabled ?
394 static int hrtimer_hres_enabled __read_mostly
= 1;
397 * Enable / Disable high resolution mode
399 static int __init
setup_hrtimer_hres(char *str
)
401 if (!strcmp(str
, "off"))
402 hrtimer_hres_enabled
= 0;
403 else if (!strcmp(str
, "on"))
404 hrtimer_hres_enabled
= 1;
410 __setup("highres=", setup_hrtimer_hres
);
413 * hrtimer_high_res_enabled - query, if the highres mode is enabled
415 static inline int hrtimer_is_hres_enabled(void)
417 return hrtimer_hres_enabled
;
421 * Is the high resolution mode active ?
423 static inline int hrtimer_hres_active(struct hrtimer_cpu_base
*cpu_base
)
425 return cpu_base
->hres_active
;
429 * Reprogram the event source with checking both queues for the
431 * Called with interrupts disabled and base->lock held
433 static void hrtimer_force_reprogram(struct hrtimer_cpu_base
*cpu_base
)
436 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
439 cpu_base
->expires_next
.tv64
= KTIME_MAX
;
441 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
442 struct hrtimer
*timer
;
446 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
447 expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
449 * clock_was_set() has changed base->offset so the
450 * result might be negative. Fix it up to prevent a
451 * false positive in clockevents_program_event()
453 if (expires
.tv64
< 0)
455 if (expires
.tv64
< cpu_base
->expires_next
.tv64
)
456 cpu_base
->expires_next
= expires
;
459 if (cpu_base
->expires_next
.tv64
!= KTIME_MAX
)
460 tick_program_event(cpu_base
->expires_next
, 1);
464 * Shared reprogramming for clock_realtime and clock_monotonic
466 * When a timer is enqueued and expires earlier than the already enqueued
467 * timers, we have to check, whether it expires earlier than the timer for
468 * which the clock event device was armed.
470 * Called with interrupts disabled and base->cpu_base.lock held
472 static int hrtimer_reprogram(struct hrtimer
*timer
,
473 struct hrtimer_clock_base
*base
)
475 ktime_t
*expires_next
= &__get_cpu_var(hrtimer_bases
).expires_next
;
476 ktime_t expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
479 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer
) < 0);
482 * When the callback is running, we do not reprogram the clock event
483 * device. The timer callback is either running on a different CPU or
484 * the callback is executed in the hrtimer_interrupt context. The
485 * reprogramming is handled at the end of the hrtimer_interrupt.
487 if (hrtimer_callback_running(timer
))
491 * CLOCK_REALTIME timer might be requested with an absolute
492 * expiry time which is less than base->offset. Nothing wrong
493 * about that, just avoid to call into the tick code, which
494 * has now objections against negative expiry values.
496 if (expires
.tv64
< 0)
499 if (expires
.tv64
>= expires_next
->tv64
)
503 * Clockevents returns -ETIME, when the event was in the past.
505 res
= tick_program_event(expires
, 0);
506 if (!IS_ERR_VALUE(res
))
507 *expires_next
= expires
;
513 * Retrigger next event is called after clock was set
515 * Called with interrupts disabled via on_each_cpu()
517 static void retrigger_next_event(void *arg
)
519 struct hrtimer_cpu_base
*base
= &__get_cpu_var(hrtimer_bases
);
521 struct timespec realtime_offset
;
524 if (!hrtimer_hres_active(base
))
528 seq
= read_seqbegin(&xtime_lock
);
529 set_normalized_timespec(&realtime_offset
,
530 -wall_to_monotonic
.tv_sec
,
531 -wall_to_monotonic
.tv_nsec
);
532 } while (read_seqretry(&xtime_lock
, seq
));
534 /* Adjust CLOCK_REALTIME offset */
535 spin_lock(&base
->lock
);
536 base
->clock_base
[CLOCK_REALTIME
].offset
=
537 timespec_to_ktime(realtime_offset
);
539 hrtimer_force_reprogram(base
);
540 spin_unlock(&base
->lock
);
544 * Clock realtime was set
546 * Change the offset of the realtime clock vs. the monotonic
549 * We might have to reprogram the high resolution timer interrupt. On
550 * SMP we call the architecture specific code to retrigger _all_ high
551 * resolution timer interrupts. On UP we just disable interrupts and
552 * call the high resolution interrupt code.
554 void clock_was_set(void)
556 /* Retrigger the CPU local events everywhere */
557 on_each_cpu(retrigger_next_event
, NULL
, 1);
561 * During resume we might have to reprogram the high resolution timer
562 * interrupt (on the local CPU):
564 void hres_timers_resume(void)
566 WARN_ONCE(!irqs_disabled(),
567 KERN_INFO
"hres_timers_resume() called with IRQs enabled!");
569 retrigger_next_event(NULL
);
573 * Initialize the high resolution related parts of cpu_base
575 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
)
577 base
->expires_next
.tv64
= KTIME_MAX
;
578 base
->hres_active
= 0;
582 * Initialize the high resolution related parts of a hrtimer
584 static inline void hrtimer_init_timer_hres(struct hrtimer
*timer
)
588 static void __run_hrtimer(struct hrtimer
*timer
);
589 static int hrtimer_rt_defer(struct hrtimer
*timer
);
592 * When High resolution timers are active, try to reprogram. Note, that in case
593 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
594 * check happens. The timer gets enqueued into the rbtree. The reprogramming
595 * and expiry check is done in the hrtimer_interrupt or in the softirq.
597 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
598 struct hrtimer_clock_base
*base
,
601 #ifdef CONFIG_PREEMPT_RT
604 if (base
->cpu_base
->hres_active
&& hrtimer_reprogram(timer
, base
)) {
605 #ifdef CONFIG_PREEMPT_RT
607 * Move softirq based timers away from the rbtree in
608 * case it expired already. Otherwise we would have a
609 * stale base->first entry until the softirq runs.
611 if (!hrtimer_rt_defer(timer
)) {
612 __run_hrtimer(timer
);
614 * __run_hrtimer might have requeued timer and
615 * it could be base->first again.
617 if (base
->first
== &timer
->node
)
623 spin_unlock(&base
->cpu_base
->lock
);
624 raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
625 spin_lock(&base
->cpu_base
->lock
);
627 __raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
636 * Switch to high resolution mode
638 static int hrtimer_switch_to_hres(struct hrtimer_cpu_base
*base
)
642 if (base
->hres_active
)
645 local_irq_save(flags
);
647 if (tick_init_highres()) {
648 local_irq_restore(flags
);
649 printk(KERN_WARNING
"Could not switch to high resolution "
650 "mode on CPU %d\n", raw_smp_processor_id());
653 base
->hres_active
= 1;
654 base
->clock_base
[CLOCK_REALTIME
].resolution
= KTIME_HIGH_RES
;
655 base
->clock_base
[CLOCK_MONOTONIC
].resolution
= KTIME_HIGH_RES
;
657 tick_setup_sched_timer();
659 /* "Retrigger" the interrupt to get things going */
660 retrigger_next_event(NULL
);
661 local_irq_restore(flags
);
667 static inline int hrtimer_hres_active(struct hrtimer_cpu_base
*base
)
671 static inline int hrtimer_is_hres_enabled(void) { return 0; }
672 static inline int hrtimer_switch_to_hres(struct hrtimer_cpu_base
*base
)
676 static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base
*base
) { }
677 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
678 struct hrtimer_clock_base
*base
,
684 static inline int hrtimer_reprogram(struct hrtimer
*timer
,
685 struct hrtimer_clock_base
*base
)
690 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
) { }
691 static inline void hrtimer_init_timer_hres(struct hrtimer
*timer
) { }
693 #endif /* CONFIG_HIGH_RES_TIMERS */
695 #ifdef CONFIG_TIMER_STATS
696 void __timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
, void *addr
)
698 if (timer
->start_site
)
701 timer
->start_site
= addr
;
702 memcpy(timer
->start_comm
, current
->comm
, TASK_COMM_LEN
);
703 timer
->start_pid
= current
->pid
;
708 * Counterpart to lock_hrtimer_base above:
711 void unlock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
713 spin_unlock_irqrestore(&timer
->base
->cpu_base
->lock
, *flags
);
717 * hrtimer_forward - forward the timer expiry
718 * @timer: hrtimer to forward
719 * @now: forward past this time
720 * @interval: the interval to forward
722 * Forward the timer expiry so it will expire in the future.
723 * Returns the number of overruns.
725 u64
hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
)
730 delta
= ktime_sub(now
, hrtimer_get_expires(timer
));
735 if (interval
.tv64
< timer
->base
->resolution
.tv64
)
736 interval
.tv64
= timer
->base
->resolution
.tv64
;
738 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
739 s64 incr
= ktime_to_ns(interval
);
741 orun
= ktime_divns(delta
, incr
);
742 hrtimer_add_expires_ns(timer
, incr
* orun
);
743 if (hrtimer_get_expires_tv64(timer
) > now
.tv64
)
746 * This (and the ktime_add() below) is the
747 * correction for exact:
751 hrtimer_add_expires(timer
, interval
);
755 EXPORT_SYMBOL_GPL(hrtimer_forward
);
758 * enqueue_hrtimer - internal function to (re)start a timer
760 * The timer is inserted in expiry order. Insertion into the
761 * red black tree is O(log(n)). Must hold the base lock.
763 * Returns 1 when the new timer is the leftmost timer in the tree.
765 static int enqueue_hrtimer(struct hrtimer
*timer
,
766 struct hrtimer_clock_base
*base
)
768 struct rb_node
**link
= &base
->active
.rb_node
;
769 struct rb_node
*parent
= NULL
;
770 struct hrtimer
*entry
;
773 debug_hrtimer_activate(timer
);
776 * Find the right place in the rbtree:
780 entry
= rb_entry(parent
, struct hrtimer
, node
);
782 * We dont care about collisions. Nodes with
783 * the same expiry time stay together.
785 if (hrtimer_get_expires_tv64(timer
) <
786 hrtimer_get_expires_tv64(entry
)) {
787 link
= &(*link
)->rb_left
;
789 link
= &(*link
)->rb_right
;
795 * Insert the timer to the rbtree and check whether it
796 * replaces the first pending timer
799 base
->first
= &timer
->node
;
801 rb_link_node(&timer
->node
, parent
, link
);
802 rb_insert_color(&timer
->node
, &base
->active
);
804 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
805 * state of a possibly running callback.
807 timer
->state
|= HRTIMER_STATE_ENQUEUED
;
812 #ifdef CONFIG_PREEMPT_SOFTIRQS
813 # define wake_up_timer_waiters(b) wake_up(&(b)->wait)
816 * hrtimer_wait_for_timer - Wait for a running timer
818 * @timer: timer to wait for
820 * The function waits in case the timers callback function is
821 * currently executed on the waitqueue of the timer base. The
822 * waitqueue is woken up after the timer callback function has
823 * finished execution.
825 void hrtimer_wait_for_timer(const struct hrtimer
*timer
)
827 struct hrtimer_clock_base
*base
= timer
->base
;
829 if (base
&& base
->cpu_base
&& !timer
->irqsafe
)
830 wait_event(base
->cpu_base
->wait
,
831 !(timer
->state
& HRTIMER_STATE_CALLBACK
));
835 # define wake_up_timer_waiters(b) do { } while (0)
839 * __remove_hrtimer - internal function to remove a timer
841 * Caller must hold the base lock.
843 * High resolution timer mode reprograms the clock event device when the
844 * timer is the one which expires next. The caller can disable this by setting
845 * reprogram to zero. This is useful, when the context does a reprogramming
846 * anyway (e.g. timer interrupt)
848 static void __remove_hrtimer(struct hrtimer
*timer
,
849 struct hrtimer_clock_base
*base
,
850 unsigned long newstate
, int reprogram
)
852 if (timer
->state
& HRTIMER_STATE_ENQUEUED
) {
854 if (unlikely(!list_empty(&timer
->cb_entry
))) {
855 list_del_init(&timer
->cb_entry
);
859 * Remove the timer from the rbtree and replace the
860 * first entry pointer if necessary.
862 if (base
->first
== &timer
->node
) {
863 base
->first
= rb_next(&timer
->node
);
864 /* Reprogram the clock event device. if enabled */
865 if (reprogram
&& hrtimer_hres_active(base
->cpu_base
))
866 hrtimer_force_reprogram(base
->cpu_base
);
868 rb_erase(&timer
->node
, &base
->active
);
871 timer
->state
= newstate
;
875 * remove hrtimer, called with base lock held
878 remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
)
880 if (hrtimer_is_queued(timer
)) {
884 * Remove the timer and force reprogramming when high
885 * resolution mode is active and the timer is on the current
886 * CPU. If we remove a timer on another CPU, reprogramming is
887 * skipped. The interrupt event on this CPU is fired and
888 * reprogramming happens in the interrupt handler. This is a
889 * rare case and less expensive than a smp call.
891 debug_hrtimer_deactivate(timer
);
892 timer_stats_hrtimer_clear_start_info(timer
);
893 reprogram
= base
->cpu_base
== &__get_cpu_var(hrtimer_bases
);
894 __remove_hrtimer(timer
, base
, HRTIMER_STATE_INACTIVE
,
901 int __hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
902 unsigned long delta_ns
, const enum hrtimer_mode mode
,
905 struct hrtimer_clock_base
*base
, *new_base
;
909 base
= lock_hrtimer_base(timer
, &flags
);
911 /* Remove an active timer from the queue: */
912 ret
= remove_hrtimer(timer
, base
);
914 /* Switch the timer base, if necessary: */
915 new_base
= switch_hrtimer_base(timer
, base
);
917 if (mode
== HRTIMER_MODE_REL
) {
918 tim
= ktime_add_safe(tim
, new_base
->get_time());
920 * CONFIG_TIME_LOW_RES is a temporary way for architectures
921 * to signal that they simply return xtime in
922 * do_gettimeoffset(). In this case we want to round up by
923 * resolution when starting a relative timer, to avoid short
924 * timeouts. This will go away with the GTOD framework.
926 #ifdef CONFIG_TIME_LOW_RES
927 tim
= ktime_add_safe(tim
, base
->resolution
);
931 hrtimer_set_expires_range_ns(timer
, tim
, delta_ns
);
933 timer_stats_hrtimer_set_start_info(timer
);
935 leftmost
= enqueue_hrtimer(timer
, new_base
);
938 * Only allow reprogramming if the new base is on this CPU.
939 * (it might still be on another CPU if the timer was pending)
941 * XXX send_remote_softirq() ?
943 if (leftmost
&& new_base
->cpu_base
== &__get_cpu_var(hrtimer_bases
))
944 hrtimer_enqueue_reprogram(timer
, new_base
, wakeup
);
946 unlock_hrtimer_base(timer
, &flags
);
952 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
953 * @timer: the timer to be added
955 * @delta_ns: "slack" range for the timer
956 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
960 * 1 when the timer was active
962 int hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
963 unsigned long delta_ns
, const enum hrtimer_mode mode
)
965 return __hrtimer_start_range_ns(timer
, tim
, delta_ns
, mode
, 1);
967 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns
);
970 * hrtimer_start - (re)start an hrtimer on the current CPU
971 * @timer: the timer to be added
973 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
977 * 1 when the timer was active
980 hrtimer_start(struct hrtimer
*timer
, ktime_t tim
, const enum hrtimer_mode mode
)
982 return __hrtimer_start_range_ns(timer
, tim
, 0, mode
, 1);
984 EXPORT_SYMBOL_GPL(hrtimer_start
);
988 * hrtimer_try_to_cancel - try to deactivate a timer
989 * @timer: hrtimer to stop
992 * 0 when the timer was not active
993 * 1 when the timer was active
994 * -1 when the timer is currently excuting the callback function and
997 int hrtimer_try_to_cancel(struct hrtimer
*timer
)
999 struct hrtimer_clock_base
*base
;
1000 unsigned long flags
;
1003 base
= lock_hrtimer_base(timer
, &flags
);
1005 if (!hrtimer_callback_running(timer
))
1006 ret
= remove_hrtimer(timer
, base
);
1008 unlock_hrtimer_base(timer
, &flags
);
1013 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel
);
1016 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1017 * @timer: the timer to be cancelled
1020 * 0 when the timer was not active
1021 * 1 when the timer was active
1023 int hrtimer_cancel(struct hrtimer
*timer
)
1026 int ret
= hrtimer_try_to_cancel(timer
);
1030 hrtimer_wait_for_timer(timer
);
1033 EXPORT_SYMBOL_GPL(hrtimer_cancel
);
1036 * hrtimer_get_remaining - get remaining time for the timer
1037 * @timer: the timer to read
1039 ktime_t
hrtimer_get_remaining(const struct hrtimer
*timer
)
1041 struct hrtimer_clock_base
*base
;
1042 unsigned long flags
;
1045 base
= lock_hrtimer_base(timer
, &flags
);
1046 rem
= hrtimer_expires_remaining(timer
);
1047 unlock_hrtimer_base(timer
, &flags
);
1051 EXPORT_SYMBOL_GPL(hrtimer_get_remaining
);
1055 * hrtimer_get_next_event - get the time until next expiry event
1057 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1060 ktime_t
hrtimer_get_next_event(void)
1062 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1063 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
1064 ktime_t delta
, mindelta
= { .tv64
= KTIME_MAX
};
1065 unsigned long flags
;
1068 spin_lock_irqsave(&cpu_base
->lock
, flags
);
1070 if (!hrtimer_hres_active(cpu_base
)) {
1071 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
1072 struct hrtimer
*timer
;
1077 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
1078 delta
.tv64
= hrtimer_get_expires_tv64(timer
);
1079 delta
= ktime_sub(delta
, base
->get_time());
1080 if (delta
.tv64
< mindelta
.tv64
)
1081 mindelta
.tv64
= delta
.tv64
;
1085 spin_unlock_irqrestore(&cpu_base
->lock
, flags
);
1087 if (mindelta
.tv64
< 0)
1093 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1094 enum hrtimer_mode mode
)
1096 struct hrtimer_cpu_base
*cpu_base
;
1098 memset(timer
, 0, sizeof(struct hrtimer
));
1100 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1102 if (clock_id
== CLOCK_REALTIME
&& mode
!= HRTIMER_MODE_ABS
)
1103 clock_id
= CLOCK_MONOTONIC
;
1105 timer
->base
= &cpu_base
->clock_base
[clock_id
];
1106 INIT_LIST_HEAD(&timer
->cb_entry
);
1107 hrtimer_init_timer_hres(timer
);
1109 #ifdef CONFIG_TIMER_STATS
1110 timer
->start_site
= NULL
;
1111 timer
->start_pid
= -1;
1112 memset(timer
->start_comm
, 0, TASK_COMM_LEN
);
1117 * hrtimer_init - initialize a timer to the given clock
1118 * @timer: the timer to be initialized
1119 * @clock_id: the clock to be used
1120 * @mode: timer mode abs/rel
1122 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1123 enum hrtimer_mode mode
)
1125 debug_hrtimer_init(timer
);
1126 __hrtimer_init(timer
, clock_id
, mode
);
1128 EXPORT_SYMBOL_GPL(hrtimer_init
);
1131 * hrtimer_get_res - get the timer resolution for a clock
1132 * @which_clock: which clock to query
1133 * @tp: pointer to timespec variable to store the resolution
1135 * Store the resolution of the clock selected by @which_clock in the
1136 * variable pointed to by @tp.
1138 int hrtimer_get_res(const clockid_t which_clock
, struct timespec
*tp
)
1140 struct hrtimer_cpu_base
*cpu_base
;
1142 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1143 *tp
= ktime_to_timespec(cpu_base
->clock_base
[which_clock
].resolution
);
1147 EXPORT_SYMBOL_GPL(hrtimer_get_res
);
1149 static void __run_hrtimer(struct hrtimer
*timer
)
1151 struct hrtimer_clock_base
*base
= timer
->base
;
1152 struct hrtimer_cpu_base
*cpu_base
= base
->cpu_base
;
1153 enum hrtimer_restart (*fn
)(struct hrtimer
*);
1156 WARN_ON(!irqs_disabled());
1158 debug_hrtimer_deactivate(timer
);
1159 __remove_hrtimer(timer
, base
, HRTIMER_STATE_CALLBACK
, 0);
1160 timer_stats_account_hrtimer(timer
);
1161 fn
= timer
->function
;
1164 * Because we run timers from hardirq context, there is no chance
1165 * they get migrated to another cpu, therefore its safe to unlock
1168 spin_unlock(&cpu_base
->lock
);
1169 restart
= fn(timer
);
1170 spin_lock(&cpu_base
->lock
);
1173 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1174 * we do not reprogramm the event hardware. Happens either in
1175 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1177 if (restart
!= HRTIMER_NORESTART
) {
1178 BUG_ON(timer
->state
!= HRTIMER_STATE_CALLBACK
);
1179 enqueue_hrtimer(timer
, base
);
1181 timer
->state
&= ~HRTIMER_STATE_CALLBACK
;
1184 #ifdef CONFIG_PREEMPT_RT
1186 static void hrtimer_rt_reprogram(int restart
, struct hrtimer
*timer
,
1187 struct hrtimer_clock_base
*base
)
1190 * Note, we clear the callback flag before we requeue the
1191 * timer otherwise we trigger the callback_running() check
1192 * in hrtimer_reprogram().
1194 timer
->state
&= ~HRTIMER_STATE_CALLBACK
;
1196 if (restart
!= HRTIMER_NORESTART
) {
1197 BUG_ON(hrtimer_active(timer
));
1199 * Enqueue the timer, if it's the leftmost timer then
1200 * we need to reprogram it.
1202 if (!enqueue_hrtimer(timer
, base
))
1205 if (hrtimer_reprogram(timer
, base
))
1208 } else if (hrtimer_active(timer
)) {
1210 * If the timer was rearmed on another CPU, reprogram
1213 if (base
->first
== &timer
->node
&&
1214 hrtimer_reprogram(timer
, base
))
1221 * Timer is expired. Thus move it from tree to pending list
1224 __remove_hrtimer(timer
, base
, timer
->state
, 0);
1225 list_add_tail(&timer
->cb_entry
, &base
->expired
);
1229 * The changes in mainline which removed the callback modes from
1230 * hrtimer are not yet working with -rt. The non wakeup_process()
1231 * based callbacks which involve sleeping locks need to be treated
1234 static void hrtimer_rt_run_pending(void)
1236 enum hrtimer_restart (*fn
)(struct hrtimer
*);
1237 struct hrtimer_cpu_base
*cpu_base
;
1238 struct hrtimer_clock_base
*base
;
1239 struct hrtimer
*timer
;
1242 local_irq_disable();
1243 cpu_base
= &per_cpu(hrtimer_bases
, smp_processor_id());
1245 spin_lock(&cpu_base
->lock
);
1247 for (index
= 0; index
< HRTIMER_MAX_CLOCK_BASES
; index
++) {
1248 base
= &cpu_base
->clock_base
[index
];
1250 while (!list_empty(&base
->expired
)) {
1251 timer
= list_first_entry(&base
->expired
,
1252 struct hrtimer
, cb_entry
);
1255 * Same as the above __run_hrtimer function
1256 * just we run with interrupts enabled.
1258 debug_hrtimer_deactivate(timer
);
1259 __remove_hrtimer(timer
, base
, HRTIMER_STATE_CALLBACK
, 0);
1260 timer_stats_account_hrtimer(timer
);
1261 fn
= timer
->function
;
1263 spin_unlock_irq(&cpu_base
->lock
);
1264 restart
= fn(timer
);
1265 spin_lock_irq(&cpu_base
->lock
);
1267 hrtimer_rt_reprogram(restart
, timer
, base
);
1270 spin_unlock_irq(&cpu_base
->lock
);
1272 wake_up_timer_waiters(cpu_base
);
1275 static int hrtimer_rt_defer(struct hrtimer
*timer
)
1280 __remove_hrtimer(timer
, timer
->base
, timer
->state
, 0);
1281 list_add_tail(&timer
->cb_entry
, &timer
->base
->expired
);
1287 static inline void hrtimer_rt_run_pending(void) { }
1288 static inline int hrtimer_rt_defer(struct hrtimer
*timer
) { return 0; }
1292 #ifdef CONFIG_HIGH_RES_TIMERS
1294 static int force_clock_reprogram
;
1297 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1298 * is hanging, which could happen with something that slows the interrupt
1299 * such as the tracing. Then we force the clock reprogramming for each future
1300 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1301 * threshold that we will overwrite.
1302 * The next tick event will be scheduled to 3 times we currently spend on
1303 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1304 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1305 * let it running without serious starvation.
1309 hrtimer_interrupt_hanging(struct clock_event_device
*dev
,
1312 force_clock_reprogram
= 1;
1313 dev
->min_delta_ns
= (unsigned long)try_time
.tv64
* 3;
1314 printk(KERN_WARNING
"hrtimer: interrupt too slow, "
1315 "forcing clock min delta to %lu ns\n", dev
->min_delta_ns
);
1318 * High resolution timer interrupt
1319 * Called with interrupts disabled
1321 void hrtimer_interrupt(struct clock_event_device
*dev
)
1323 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1324 struct hrtimer_clock_base
*base
;
1325 ktime_t expires_next
, now
;
1329 BUG_ON(!cpu_base
->hres_active
);
1330 cpu_base
->nr_events
++;
1331 dev
->next_event
.tv64
= KTIME_MAX
;
1334 /* 5 retries is enough to notice a hang */
1335 if (!(++nr_retries
% 5))
1336 hrtimer_interrupt_hanging(dev
, ktime_sub(ktime_get(), now
));
1340 expires_next
.tv64
= KTIME_MAX
;
1342 base
= cpu_base
->clock_base
;
1344 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1346 struct rb_node
*node
;
1348 spin_lock(&cpu_base
->lock
);
1350 basenow
= ktime_add(now
, base
->offset
);
1352 while ((node
= base
->first
)) {
1353 struct hrtimer
*timer
;
1355 timer
= rb_entry(node
, struct hrtimer
, node
);
1358 * The immediate goal for using the softexpires is
1359 * minimizing wakeups, not running timers at the
1360 * earliest interrupt after their soft expiration.
1361 * This allows us to avoid using a Priority Search
1362 * Tree, which can answer a stabbing querry for
1363 * overlapping intervals and instead use the simple
1364 * BST we already have.
1365 * We don't add extra wakeups by delaying timers that
1366 * are right-of a not yet expired timer, because that
1367 * timer will have to trigger a wakeup anyway.
1370 if (basenow
.tv64
< hrtimer_get_softexpires_tv64(timer
)) {
1373 expires
= ktime_sub(hrtimer_get_expires(timer
),
1375 if (expires
.tv64
< expires_next
.tv64
)
1376 expires_next
= expires
;
1380 if (!hrtimer_rt_defer(timer
))
1381 __run_hrtimer(timer
);
1385 spin_unlock(&cpu_base
->lock
);
1389 cpu_base
->expires_next
= expires_next
;
1391 /* Reprogramming necessary ? */
1392 if (expires_next
.tv64
!= KTIME_MAX
) {
1393 if (tick_program_event(expires_next
, force_clock_reprogram
))
1398 raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
1402 * local version of hrtimer_peek_ahead_timers() called with interrupts
1405 static void __hrtimer_peek_ahead_timers(void)
1407 struct hrtimer_cpu_base
*cpu_base
;
1408 struct tick_device
*td
;
1410 cpu_base
= &__get_cpu_var(hrtimer_bases
);
1411 if (!hrtimer_hres_active(cpu_base
))
1414 td
= &__get_cpu_var(tick_cpu_device
);
1415 if (td
&& td
->evtdev
)
1416 hrtimer_interrupt(td
->evtdev
);
1420 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1422 * hrtimer_peek_ahead_timers will peek at the timer queue of
1423 * the current cpu and check if there are any timers for which
1424 * the soft expires time has passed. If any such timers exist,
1425 * they are run immediately and then removed from the timer queue.
1428 void hrtimer_peek_ahead_timers(void)
1430 unsigned long flags
;
1432 local_irq_save(flags
);
1433 __hrtimer_peek_ahead_timers();
1434 local_irq_restore(flags
);
1437 #else /* CONFIG_HIGH_RES_TIMERS */
1439 static inline void __hrtimer_peek_ahead_timers(void) { }
1441 #endif /* !CONFIG_HIGH_RES_TIMERS */
1443 static void run_hrtimer_softirq(struct softirq_action
*h
)
1445 hrtimer_rt_run_pending();
1449 * Called from timer softirq every jiffy, expire hrtimers:
1451 * For HRT its the fall back code to run the softirq in the timer
1452 * softirq context in case the hrtimer initialization failed or has
1453 * not been done yet.
1455 void hrtimer_run_pending(void)
1457 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1459 if (hrtimer_hres_active(cpu_base
))
1463 * This _is_ ugly: We have to check in the softirq context,
1464 * whether we can switch to highres and / or nohz mode. The
1465 * clocksource switch happens in the timer interrupt with
1466 * xtime_lock held. Notification from there only sets the
1467 * check bit in the tick_oneshot code, otherwise we might
1468 * deadlock vs. xtime_lock.
1470 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1471 hrtimer_switch_to_hres(cpu_base
);
1475 * Called from hardirq context every jiffy
1477 void hrtimer_run_queues(void)
1479 struct rb_node
*node
;
1480 struct hrtimer_cpu_base
*cpu_base
;
1481 struct hrtimer_clock_base
*base
;
1482 int index
, gettime
= 1, raise
= 0;
1484 cpu_base
= &per_cpu(hrtimer_bases
, raw_smp_processor_id());
1485 if (hrtimer_hres_active(cpu_base
))
1488 for (index
= 0; index
< HRTIMER_MAX_CLOCK_BASES
; index
++) {
1489 base
= &cpu_base
->clock_base
[index
];
1495 hrtimer_get_softirq_time(cpu_base
);
1499 spin_lock(&cpu_base
->lock
);
1501 while ((node
= base
->first
)) {
1502 struct hrtimer
*timer
;
1504 timer
= rb_entry(node
, struct hrtimer
, node
);
1505 if (base
->softirq_time
.tv64
<=
1506 hrtimer_get_expires_tv64(timer
))
1509 if (!hrtimer_rt_defer(timer
))
1510 __run_hrtimer(timer
);
1514 spin_unlock(&cpu_base
->lock
);
1518 raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
1522 * Sleep related functions:
1524 static enum hrtimer_restart
hrtimer_wakeup(struct hrtimer
*timer
)
1526 struct hrtimer_sleeper
*t
=
1527 container_of(timer
, struct hrtimer_sleeper
, timer
);
1528 struct task_struct
*task
= t
->task
;
1532 wake_up_process(task
);
1534 return HRTIMER_NORESTART
;
1537 void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
, struct task_struct
*task
)
1539 sl
->timer
.function
= hrtimer_wakeup
;
1540 sl
->timer
.irqsafe
= 1;
1544 static int __sched
do_nanosleep(struct hrtimer_sleeper
*t
, enum hrtimer_mode mode
)
1546 hrtimer_init_sleeper(t
, current
);
1549 set_current_state(TASK_INTERRUPTIBLE
);
1550 hrtimer_start_expires(&t
->timer
, mode
);
1551 if (!hrtimer_active(&t
->timer
))
1554 if (likely(t
->task
))
1557 hrtimer_cancel(&t
->timer
);
1558 mode
= HRTIMER_MODE_ABS
;
1560 } while (t
->task
&& !signal_pending(current
));
1562 __set_current_state(TASK_RUNNING
);
1564 return t
->task
== NULL
;
1567 static int update_rmtp(struct hrtimer
*timer
, struct timespec __user
*rmtp
)
1569 struct timespec rmt
;
1572 rem
= hrtimer_expires_remaining(timer
);
1575 rmt
= ktime_to_timespec(rem
);
1577 if (copy_to_user(rmtp
, &rmt
, sizeof(*rmtp
)))
1583 long __sched
hrtimer_nanosleep_restart(struct restart_block
*restart
)
1585 struct hrtimer_sleeper t
;
1586 struct timespec __user
*rmtp
;
1589 hrtimer_init_on_stack(&t
.timer
, restart
->nanosleep
.index
,
1591 hrtimer_set_expires_tv64(&t
.timer
, restart
->nanosleep
.expires
);
1593 if (do_nanosleep(&t
, HRTIMER_MODE_ABS
))
1596 rmtp
= restart
->nanosleep
.rmtp
;
1598 ret
= update_rmtp(&t
.timer
, rmtp
);
1603 /* The other values in restart are already filled in */
1604 ret
= -ERESTART_RESTARTBLOCK
;
1606 destroy_hrtimer_on_stack(&t
.timer
);
1610 long hrtimer_nanosleep(struct timespec
*rqtp
, struct timespec __user
*rmtp
,
1611 const enum hrtimer_mode mode
, const clockid_t clockid
)
1613 struct restart_block
*restart
;
1614 struct hrtimer_sleeper t
;
1616 unsigned long slack
;
1618 slack
= current
->timer_slack_ns
;
1619 if (rt_task(current
))
1622 hrtimer_init_on_stack(&t
.timer
, clockid
, mode
);
1623 hrtimer_set_expires_range_ns(&t
.timer
, timespec_to_ktime(*rqtp
), slack
);
1624 if (do_nanosleep(&t
, mode
))
1627 /* Absolute timers do not update the rmtp value and restart: */
1628 if (mode
== HRTIMER_MODE_ABS
) {
1629 ret
= -ERESTARTNOHAND
;
1634 ret
= update_rmtp(&t
.timer
, rmtp
);
1639 restart
= ¤t_thread_info()->restart_block
;
1640 restart
->fn
= hrtimer_nanosleep_restart
;
1641 restart
->nanosleep
.index
= t
.timer
.base
->index
;
1642 restart
->nanosleep
.rmtp
= rmtp
;
1643 restart
->nanosleep
.expires
= hrtimer_get_expires_tv64(&t
.timer
);
1645 ret
= -ERESTART_RESTARTBLOCK
;
1647 destroy_hrtimer_on_stack(&t
.timer
);
1651 SYSCALL_DEFINE2(nanosleep
, struct timespec __user
*, rqtp
,
1652 struct timespec __user
*, rmtp
)
1656 if (copy_from_user(&tu
, rqtp
, sizeof(tu
)))
1659 if (!timespec_valid(&tu
))
1662 return hrtimer_nanosleep(&tu
, rmtp
, HRTIMER_MODE_REL
, CLOCK_MONOTONIC
);
1666 * Functions related to boot-time initialization:
1668 static void __cpuinit
init_hrtimers_cpu(int cpu
)
1670 struct hrtimer_cpu_base
*cpu_base
= &per_cpu(hrtimer_bases
, cpu
);
1673 spin_lock_init(&cpu_base
->lock
);
1675 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1676 cpu_base
->clock_base
[i
].cpu_base
= cpu_base
;
1677 INIT_LIST_HEAD(&cpu_base
->clock_base
[i
].expired
);
1680 hrtimer_init_hres(cpu_base
);
1681 #ifdef CONFIG_PREEMPT_RT
1682 init_waitqueue_head(&cpu_base
->wait
);
1686 #ifdef CONFIG_HOTPLUG_CPU
1688 static void migrate_hrtimer_list(struct hrtimer_clock_base
*old_base
,
1689 struct hrtimer_clock_base
*new_base
)
1691 struct hrtimer
*timer
;
1692 struct rb_node
*node
;
1694 while ((node
= rb_first(&old_base
->active
))) {
1695 timer
= rb_entry(node
, struct hrtimer
, node
);
1696 BUG_ON(hrtimer_callback_running(timer
));
1697 debug_hrtimer_deactivate(timer
);
1700 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1701 * timer could be seen as !active and just vanish away
1702 * under us on another CPU
1704 __remove_hrtimer(timer
, old_base
, HRTIMER_STATE_MIGRATE
, 0);
1705 timer
->base
= new_base
;
1707 * Enqueue the timers on the new cpu. This does not
1708 * reprogram the event device in case the timer
1709 * expires before the earliest on this CPU, but we run
1710 * hrtimer_interrupt after we migrated everything to
1711 * sort out already expired timers and reprogram the
1714 enqueue_hrtimer(timer
, new_base
);
1716 /* Clear the migration state bit */
1717 timer
->state
&= ~HRTIMER_STATE_MIGRATE
;
1721 static void migrate_hrtimers(int scpu
)
1723 struct hrtimer_cpu_base
*old_base
, *new_base
;
1726 BUG_ON(cpu_online(scpu
));
1727 tick_cancel_sched_timer(scpu
);
1729 local_irq_disable();
1730 old_base
= &per_cpu(hrtimer_bases
, scpu
);
1731 new_base
= &__get_cpu_var(hrtimer_bases
);
1733 * The caller is globally serialized and nobody else
1734 * takes two locks at once, deadlock is not possible.
1736 spin_lock(&new_base
->lock
);
1737 spin_lock_nested(&old_base
->lock
, SINGLE_DEPTH_NESTING
);
1739 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1740 migrate_hrtimer_list(&old_base
->clock_base
[i
],
1741 &new_base
->clock_base
[i
]);
1744 spin_unlock(&old_base
->lock
);
1745 spin_unlock(&new_base
->lock
);
1747 /* Check, if we got expired work to do */
1748 __hrtimer_peek_ahead_timers();
1752 #endif /* CONFIG_HOTPLUG_CPU */
1754 static int __cpuinit
hrtimer_cpu_notify(struct notifier_block
*self
,
1755 unsigned long action
, void *hcpu
)
1757 int scpu
= (long)hcpu
;
1761 case CPU_UP_PREPARE
:
1762 case CPU_UP_PREPARE_FROZEN
:
1763 init_hrtimers_cpu(scpu
);
1766 #ifdef CONFIG_HOTPLUG_CPU
1768 case CPU_DYING_FROZEN
:
1769 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING
, &scpu
);
1772 case CPU_DEAD_FROZEN
:
1774 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD
, &scpu
);
1775 migrate_hrtimers(scpu
);
1787 static struct notifier_block __cpuinitdata hrtimers_nb
= {
1788 .notifier_call
= hrtimer_cpu_notify
,
1791 void __init
hrtimers_init(void)
1793 hrtimer_cpu_notify(&hrtimers_nb
, (unsigned long)CPU_UP_PREPARE
,
1794 (void *)(long)smp_processor_id());
1795 register_cpu_notifier(&hrtimers_nb
);
1796 open_softirq(HRTIMER_SOFTIRQ
, run_hrtimer_softirq
);
1800 * schedule_hrtimeout_range - sleep until timeout
1801 * @expires: timeout value (ktime_t)
1802 * @delta: slack in expires timeout (ktime_t)
1803 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1805 * Make the current task sleep until the given expiry time has
1806 * elapsed. The routine will return immediately unless
1807 * the current task state has been set (see set_current_state()).
1809 * The @delta argument gives the kernel the freedom to schedule the
1810 * actual wakeup to a time that is both power and performance friendly.
1811 * The kernel give the normal best effort behavior for "@expires+@delta",
1812 * but may decide to fire the timer earlier, but no earlier than @expires.
1814 * You can set the task state as follows -
1816 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1817 * pass before the routine returns.
1819 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1820 * delivered to the current task.
1822 * The current task state is guaranteed to be TASK_RUNNING when this
1825 * Returns 0 when the timer has expired otherwise -EINTR
1827 int __sched
schedule_hrtimeout_range(ktime_t
*expires
, unsigned long delta
,
1828 const enum hrtimer_mode mode
)
1830 struct hrtimer_sleeper t
;
1833 * Optimize when a zero timeout value is given. It does not
1834 * matter whether this is an absolute or a relative time.
1836 if (expires
&& !expires
->tv64
) {
1837 __set_current_state(TASK_RUNNING
);
1842 * A NULL parameter means "inifinte"
1846 __set_current_state(TASK_RUNNING
);
1850 hrtimer_init_on_stack(&t
.timer
, CLOCK_MONOTONIC
, mode
);
1851 hrtimer_set_expires_range_ns(&t
.timer
, *expires
, delta
);
1853 hrtimer_init_sleeper(&t
, current
);
1855 hrtimer_start_expires(&t
.timer
, mode
);
1856 if (!hrtimer_active(&t
.timer
))
1862 hrtimer_cancel(&t
.timer
);
1863 destroy_hrtimer_on_stack(&t
.timer
);
1865 __set_current_state(TASK_RUNNING
);
1867 return !t
.task
? 0 : -EINTR
;
1869 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range
);
1872 * schedule_hrtimeout - sleep until timeout
1873 * @expires: timeout value (ktime_t)
1874 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1876 * Make the current task sleep until the given expiry time has
1877 * elapsed. The routine will return immediately unless
1878 * the current task state has been set (see set_current_state()).
1880 * You can set the task state as follows -
1882 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1883 * pass before the routine returns.
1885 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1886 * delivered to the current task.
1888 * The current task state is guaranteed to be TASK_RUNNING when this
1891 * Returns 0 when the timer has expired otherwise -EINTR
1893 int __sched
schedule_hrtimeout(ktime_t
*expires
,
1894 const enum hrtimer_mode mode
)
1896 return schedule_hrtimeout_range(expires
, 0, mode
);
1898 EXPORT_SYMBOL_GPL(schedule_hrtimeout
);