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>
50 * ktime_get - get the monotonic time in ktime_t format
52 * returns the time in ktime_t format
54 ktime_t
ktime_get(void)
60 return timespec_to_ktime(now
);
62 EXPORT_SYMBOL_GPL(ktime_get
);
65 * ktime_get_real - get the real (wall-) time in ktime_t format
67 * returns the time in ktime_t format
69 ktime_t
ktime_get_real(void)
75 return timespec_to_ktime(now
);
78 EXPORT_SYMBOL_GPL(ktime_get_real
);
83 * Note: If we want to add new timer bases, we have to skip the two
84 * clock ids captured by the cpu-timers. We do this by holding empty
85 * entries rather than doing math adjustment of the clock ids.
86 * This ensures that we capture erroneous accesses to these clock ids
87 * rather than moving them into the range of valid clock id's.
89 DEFINE_PER_CPU(struct hrtimer_cpu_base
, hrtimer_bases
) =
95 .index
= CLOCK_REALTIME
,
96 .get_time
= &ktime_get_real
,
97 .resolution
= KTIME_LOW_RES
,
100 .index
= CLOCK_MONOTONIC
,
101 .get_time
= &ktime_get
,
102 .resolution
= KTIME_LOW_RES
,
108 * ktime_get_ts - get the monotonic clock in timespec format
109 * @ts: pointer to timespec variable
111 * The function calculates the monotonic clock from the realtime
112 * clock and the wall_to_monotonic offset and stores the result
113 * in normalized timespec format in the variable pointed to by @ts.
115 void ktime_get_ts(struct timespec
*ts
)
117 struct timespec tomono
;
121 seq
= read_seqbegin(&xtime_lock
);
123 tomono
= wall_to_monotonic
;
125 } while (read_seqretry(&xtime_lock
, seq
));
127 set_normalized_timespec(ts
, ts
->tv_sec
+ tomono
.tv_sec
,
128 ts
->tv_nsec
+ tomono
.tv_nsec
);
130 EXPORT_SYMBOL_GPL(ktime_get_ts
);
133 * Get the coarse grained time at the softirq based on xtime and
136 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base
*base
)
138 ktime_t xtim
, tomono
;
139 struct timespec xts
, tom
;
143 seq
= read_seqbegin(&xtime_lock
);
144 xts
= current_kernel_time();
145 tom
= wall_to_monotonic
;
146 } while (read_seqretry(&xtime_lock
, seq
));
148 xtim
= timespec_to_ktime(xts
);
149 tomono
= timespec_to_ktime(tom
);
150 base
->clock_base
[CLOCK_REALTIME
].softirq_time
= xtim
;
151 base
->clock_base
[CLOCK_MONOTONIC
].softirq_time
=
152 ktime_add(xtim
, tomono
);
156 * Functions and macros which are different for UP/SMP systems are kept in a
162 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
163 * means that all timers which are tied to this base via timer->base are
164 * locked, and the base itself is locked too.
166 * So __run_timers/migrate_timers can safely modify all timers which could
167 * be found on the lists/queues.
169 * When the timer's base is locked, and the timer removed from list, it is
170 * possible to set timer->base = NULL and drop the lock: the timer remains
174 struct hrtimer_clock_base
*lock_hrtimer_base(const struct hrtimer
*timer
,
175 unsigned long *flags
)
177 struct hrtimer_clock_base
*base
;
181 if (likely(base
!= NULL
)) {
182 spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
183 if (likely(base
== timer
->base
))
185 /* The timer has migrated to another CPU: */
186 spin_unlock_irqrestore(&base
->cpu_base
->lock
, *flags
);
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
)
198 struct hrtimer_clock_base
*new_base
;
199 struct hrtimer_cpu_base
*new_cpu_base
;
201 new_cpu_base
= &__get_cpu_var(hrtimer_bases
);
202 new_base
= &new_cpu_base
->clock_base
[base
->index
];
204 if (base
!= new_base
) {
206 * We are trying to schedule the timer on the local CPU.
207 * However we can't change timer's base while it is running,
208 * so we keep it on the same CPU. No hassle vs. reprogramming
209 * the event source in the high resolution case. The softirq
210 * code will take care of this when the timer function has
211 * completed. There is no conflict as we hold the lock until
212 * the timer is enqueued.
214 if (unlikely(hrtimer_callback_running(timer
)))
217 /* See the comment in lock_timer_base() */
219 spin_unlock(&base
->cpu_base
->lock
);
220 spin_lock(&new_base
->cpu_base
->lock
);
221 timer
->base
= new_base
;
226 #else /* CONFIG_SMP */
228 static inline struct hrtimer_clock_base
*
229 lock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
231 struct hrtimer_clock_base
*base
= timer
->base
;
233 spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
238 # define switch_hrtimer_base(t, b) (b)
240 #endif /* !CONFIG_SMP */
243 * Functions for the union type storage format of ktime_t which are
244 * too large for inlining:
246 #if BITS_PER_LONG < 64
247 # ifndef CONFIG_KTIME_SCALAR
249 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
251 * @nsec: the scalar nsec value to add
253 * Returns the sum of kt and nsec in ktime_t format
255 ktime_t
ktime_add_ns(const ktime_t kt
, u64 nsec
)
259 if (likely(nsec
< NSEC_PER_SEC
)) {
262 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
264 tmp
= ktime_set((long)nsec
, rem
);
267 return ktime_add(kt
, tmp
);
270 EXPORT_SYMBOL_GPL(ktime_add_ns
);
273 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
275 * @nsec: the scalar nsec value to subtract
277 * Returns the subtraction of @nsec from @kt in ktime_t format
279 ktime_t
ktime_sub_ns(const ktime_t kt
, u64 nsec
)
283 if (likely(nsec
< NSEC_PER_SEC
)) {
286 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
288 tmp
= ktime_set((long)nsec
, rem
);
291 return ktime_sub(kt
, tmp
);
294 EXPORT_SYMBOL_GPL(ktime_sub_ns
);
295 # endif /* !CONFIG_KTIME_SCALAR */
298 * Divide a ktime value by a nanosecond value
300 u64
ktime_divns(const ktime_t kt
, s64 div
)
305 dclc
= ktime_to_ns(kt
);
306 /* Make sure the divisor is less than 2^32: */
312 do_div(dclc
, (unsigned long) div
);
316 #endif /* BITS_PER_LONG >= 64 */
319 * Add two ktime values and do a safety check for overflow:
321 ktime_t
ktime_add_safe(const ktime_t lhs
, const ktime_t rhs
)
323 ktime_t res
= ktime_add(lhs
, rhs
);
326 * We use KTIME_SEC_MAX here, the maximum timeout which we can
327 * return to user space in a timespec:
329 if (res
.tv64
< 0 || res
.tv64
< lhs
.tv64
|| res
.tv64
< rhs
.tv64
)
330 res
= ktime_set(KTIME_SEC_MAX
, 0);
335 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
337 static struct debug_obj_descr hrtimer_debug_descr
;
340 * fixup_init is called when:
341 * - an active object is initialized
343 static int hrtimer_fixup_init(void *addr
, enum debug_obj_state state
)
345 struct hrtimer
*timer
= addr
;
348 case ODEBUG_STATE_ACTIVE
:
349 hrtimer_cancel(timer
);
350 debug_object_init(timer
, &hrtimer_debug_descr
);
358 * fixup_activate is called when:
359 * - an active object is activated
360 * - an unknown object is activated (might be a statically initialized object)
362 static int hrtimer_fixup_activate(void *addr
, enum debug_obj_state state
)
366 case ODEBUG_STATE_NOTAVAILABLE
:
370 case ODEBUG_STATE_ACTIVE
:
379 * fixup_free is called when:
380 * - an active object is freed
382 static int hrtimer_fixup_free(void *addr
, enum debug_obj_state state
)
384 struct hrtimer
*timer
= addr
;
387 case ODEBUG_STATE_ACTIVE
:
388 hrtimer_cancel(timer
);
389 debug_object_free(timer
, &hrtimer_debug_descr
);
396 static struct debug_obj_descr hrtimer_debug_descr
= {
398 .fixup_init
= hrtimer_fixup_init
,
399 .fixup_activate
= hrtimer_fixup_activate
,
400 .fixup_free
= hrtimer_fixup_free
,
403 static inline void debug_hrtimer_init(struct hrtimer
*timer
)
405 debug_object_init(timer
, &hrtimer_debug_descr
);
408 static inline void debug_hrtimer_activate(struct hrtimer
*timer
)
410 debug_object_activate(timer
, &hrtimer_debug_descr
);
413 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
)
415 debug_object_deactivate(timer
, &hrtimer_debug_descr
);
418 static inline void debug_hrtimer_free(struct hrtimer
*timer
)
420 debug_object_free(timer
, &hrtimer_debug_descr
);
423 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
424 enum hrtimer_mode mode
);
426 void hrtimer_init_on_stack(struct hrtimer
*timer
, clockid_t clock_id
,
427 enum hrtimer_mode mode
)
429 debug_object_init_on_stack(timer
, &hrtimer_debug_descr
);
430 __hrtimer_init(timer
, clock_id
, mode
);
433 void destroy_hrtimer_on_stack(struct hrtimer
*timer
)
435 debug_object_free(timer
, &hrtimer_debug_descr
);
439 static inline void debug_hrtimer_init(struct hrtimer
*timer
) { }
440 static inline void debug_hrtimer_activate(struct hrtimer
*timer
) { }
441 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
) { }
445 * Check, whether the timer is on the callback pending list
447 static inline int hrtimer_cb_pending(const struct hrtimer
*timer
)
449 return timer
->state
& HRTIMER_STATE_PENDING
;
453 * Remove a timer from the callback pending list
455 static inline void hrtimer_remove_cb_pending(struct hrtimer
*timer
)
457 list_del_init(&timer
->cb_entry
);
460 /* High resolution timer related functions */
461 #ifdef CONFIG_HIGH_RES_TIMERS
464 * High resolution timer enabled ?
466 static int hrtimer_hres_enabled __read_mostly
= 1;
469 * Enable / Disable high resolution mode
471 static int __init
setup_hrtimer_hres(char *str
)
473 if (!strcmp(str
, "off"))
474 hrtimer_hres_enabled
= 0;
475 else if (!strcmp(str
, "on"))
476 hrtimer_hres_enabled
= 1;
482 __setup("highres=", setup_hrtimer_hres
);
485 * hrtimer_high_res_enabled - query, if the highres mode is enabled
487 static inline int hrtimer_is_hres_enabled(void)
489 return hrtimer_hres_enabled
;
493 * Is the high resolution mode active ?
495 static inline int hrtimer_hres_active(void)
497 return __get_cpu_var(hrtimer_bases
).hres_active
;
501 * Reprogram the event source with checking both queues for the
503 * Called with interrupts disabled and base->lock held
505 static void hrtimer_force_reprogram(struct hrtimer_cpu_base
*cpu_base
)
508 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
511 cpu_base
->expires_next
.tv64
= KTIME_MAX
;
513 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
514 struct hrtimer
*timer
;
518 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
519 expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
520 if (expires
.tv64
< cpu_base
->expires_next
.tv64
)
521 cpu_base
->expires_next
= expires
;
524 if (cpu_base
->expires_next
.tv64
!= KTIME_MAX
)
525 tick_program_event(cpu_base
->expires_next
, 1);
529 * Shared reprogramming for clock_realtime and clock_monotonic
531 * When a timer is enqueued and expires earlier than the already enqueued
532 * timers, we have to check, whether it expires earlier than the timer for
533 * which the clock event device was armed.
535 * Called with interrupts disabled and base->cpu_base.lock held
537 static int hrtimer_reprogram(struct hrtimer
*timer
,
538 struct hrtimer_clock_base
*base
)
540 ktime_t
*expires_next
= &__get_cpu_var(hrtimer_bases
).expires_next
;
541 ktime_t expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
544 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer
) < 0);
547 * When the callback is running, we do not reprogram the clock event
548 * device. The timer callback is either running on a different CPU or
549 * the callback is executed in the hrtimer_interrupt context. The
550 * reprogramming is handled either by the softirq, which called the
551 * callback or at the end of the hrtimer_interrupt.
553 if (hrtimer_callback_running(timer
))
557 * CLOCK_REALTIME timer might be requested with an absolute
558 * expiry time which is less than base->offset. Nothing wrong
559 * about that, just avoid to call into the tick code, which
560 * has now objections against negative expiry values.
562 if (expires
.tv64
< 0)
565 if (expires
.tv64
>= expires_next
->tv64
)
569 * Clockevents returns -ETIME, when the event was in the past.
571 res
= tick_program_event(expires
, 0);
572 if (!IS_ERR_VALUE(res
))
573 *expires_next
= expires
;
579 * Retrigger next event is called after clock was set
581 * Called with interrupts disabled via on_each_cpu()
583 static void retrigger_next_event(void *arg
)
585 struct hrtimer_cpu_base
*base
;
586 struct timespec realtime_offset
;
589 if (!hrtimer_hres_active())
593 seq
= read_seqbegin(&xtime_lock
);
594 set_normalized_timespec(&realtime_offset
,
595 -wall_to_monotonic
.tv_sec
,
596 -wall_to_monotonic
.tv_nsec
);
597 } while (read_seqretry(&xtime_lock
, seq
));
599 base
= &__get_cpu_var(hrtimer_bases
);
601 /* Adjust CLOCK_REALTIME offset */
602 spin_lock(&base
->lock
);
603 base
->clock_base
[CLOCK_REALTIME
].offset
=
604 timespec_to_ktime(realtime_offset
);
606 hrtimer_force_reprogram(base
);
607 spin_unlock(&base
->lock
);
611 * Clock realtime was set
613 * Change the offset of the realtime clock vs. the monotonic
616 * We might have to reprogram the high resolution timer interrupt. On
617 * SMP we call the architecture specific code to retrigger _all_ high
618 * resolution timer interrupts. On UP we just disable interrupts and
619 * call the high resolution interrupt code.
621 void clock_was_set(void)
623 /* Retrigger the CPU local events everywhere */
624 on_each_cpu(retrigger_next_event
, NULL
, 1);
628 * During resume we might have to reprogram the high resolution timer
629 * interrupt (on the local CPU):
631 void hres_timers_resume(void)
633 /* Retrigger the CPU local events: */
634 retrigger_next_event(NULL
);
638 * Initialize the high resolution related parts of cpu_base
640 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
)
642 base
->expires_next
.tv64
= KTIME_MAX
;
643 base
->hres_active
= 0;
647 * Initialize the high resolution related parts of a hrtimer
649 static inline void hrtimer_init_timer_hres(struct hrtimer
*timer
)
654 * When High resolution timers are active, try to reprogram. Note, that in case
655 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
656 * check happens. The timer gets enqueued into the rbtree. The reprogramming
657 * and expiry check is done in the hrtimer_interrupt or in the softirq.
659 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
660 struct hrtimer_clock_base
*base
)
662 if (base
->cpu_base
->hres_active
&& hrtimer_reprogram(timer
, base
)) {
664 /* Timer is expired, act upon the callback mode */
665 switch(timer
->cb_mode
) {
666 case HRTIMER_CB_IRQSAFE_PERCPU
:
667 case HRTIMER_CB_IRQSAFE_UNLOCKED
:
669 * This is solely for the sched tick emulation with
670 * dynamic tick support to ensure that we do not
671 * restart the tick right on the edge and end up with
672 * the tick timer in the softirq ! The calling site
673 * takes care of this. Also used for hrtimer sleeper !
675 debug_hrtimer_deactivate(timer
);
677 case HRTIMER_CB_SOFTIRQ
:
679 * Move everything else into the softirq pending list !
681 list_add_tail(&timer
->cb_entry
,
682 &base
->cpu_base
->cb_pending
);
683 timer
->state
= HRTIMER_STATE_PENDING
;
693 * Switch to high resolution mode
695 static int hrtimer_switch_to_hres(void)
697 int cpu
= smp_processor_id();
698 struct hrtimer_cpu_base
*base
= &per_cpu(hrtimer_bases
, cpu
);
701 if (base
->hres_active
)
704 local_irq_save(flags
);
706 if (tick_init_highres()) {
707 local_irq_restore(flags
);
708 printk(KERN_WARNING
"Could not switch to high resolution "
709 "mode on CPU %d\n", cpu
);
712 base
->hres_active
= 1;
713 base
->clock_base
[CLOCK_REALTIME
].resolution
= KTIME_HIGH_RES
;
714 base
->clock_base
[CLOCK_MONOTONIC
].resolution
= KTIME_HIGH_RES
;
716 tick_setup_sched_timer();
718 /* "Retrigger" the interrupt to get things going */
719 retrigger_next_event(NULL
);
720 local_irq_restore(flags
);
721 printk(KERN_DEBUG
"Switched to high resolution mode on CPU %d\n",
726 static inline void hrtimer_raise_softirq(void)
728 raise_softirq(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; }
736 static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base
*base
) { }
737 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
738 struct hrtimer_clock_base
*base
)
742 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
) { }
743 static inline void hrtimer_init_timer_hres(struct hrtimer
*timer
) { }
744 static inline int hrtimer_reprogram(struct hrtimer
*timer
,
745 struct hrtimer_clock_base
*base
)
749 static inline void hrtimer_raise_softirq(void) { }
751 #endif /* CONFIG_HIGH_RES_TIMERS */
753 #ifdef CONFIG_TIMER_STATS
754 void __timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
, void *addr
)
756 if (timer
->start_site
)
759 timer
->start_site
= addr
;
760 memcpy(timer
->start_comm
, current
->comm
, TASK_COMM_LEN
);
761 timer
->start_pid
= current
->pid
;
766 * Counterpart to lock_hrtimer_base above:
769 void unlock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
771 spin_unlock_irqrestore(&timer
->base
->cpu_base
->lock
, *flags
);
775 * hrtimer_forward - forward the timer expiry
776 * @timer: hrtimer to forward
777 * @now: forward past this time
778 * @interval: the interval to forward
780 * Forward the timer expiry so it will expire in the future.
781 * Returns the number of overruns.
783 u64
hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
)
788 delta
= ktime_sub(now
, hrtimer_get_expires(timer
));
793 if (interval
.tv64
< timer
->base
->resolution
.tv64
)
794 interval
.tv64
= timer
->base
->resolution
.tv64
;
796 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
797 s64 incr
= ktime_to_ns(interval
);
799 orun
= ktime_divns(delta
, incr
);
800 hrtimer_add_expires_ns(timer
, incr
* orun
);
801 if (hrtimer_get_expires_tv64(timer
) > now
.tv64
)
804 * This (and the ktime_add() below) is the
805 * correction for exact:
809 hrtimer_add_expires(timer
, interval
);
813 EXPORT_SYMBOL_GPL(hrtimer_forward
);
816 * enqueue_hrtimer - internal function to (re)start a timer
818 * The timer is inserted in expiry order. Insertion into the
819 * red black tree is O(log(n)). Must hold the base lock.
821 static void enqueue_hrtimer(struct hrtimer
*timer
,
822 struct hrtimer_clock_base
*base
, int reprogram
)
824 struct rb_node
**link
= &base
->active
.rb_node
;
825 struct rb_node
*parent
= NULL
;
826 struct hrtimer
*entry
;
829 debug_hrtimer_activate(timer
);
832 * Find the right place in the rbtree:
836 entry
= rb_entry(parent
, struct hrtimer
, node
);
838 * We dont care about collisions. Nodes with
839 * the same expiry time stay together.
841 if (hrtimer_get_expires_tv64(timer
) <
842 hrtimer_get_expires_tv64(entry
)) {
843 link
= &(*link
)->rb_left
;
845 link
= &(*link
)->rb_right
;
851 * Insert the timer to the rbtree and check whether it
852 * replaces the first pending timer
856 * Reprogram the clock event device. When the timer is already
857 * expired hrtimer_enqueue_reprogram has either called the
858 * callback or added it to the pending list and raised the
861 * This is a NOP for !HIGHRES
863 if (reprogram
&& hrtimer_enqueue_reprogram(timer
, base
))
866 base
->first
= &timer
->node
;
869 rb_link_node(&timer
->node
, parent
, link
);
870 rb_insert_color(&timer
->node
, &base
->active
);
872 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
873 * state of a possibly running callback.
875 timer
->state
|= HRTIMER_STATE_ENQUEUED
;
879 * __remove_hrtimer - internal function to remove a timer
881 * Caller must hold the base lock.
883 * High resolution timer mode reprograms the clock event device when the
884 * timer is the one which expires next. The caller can disable this by setting
885 * reprogram to zero. This is useful, when the context does a reprogramming
886 * anyway (e.g. timer interrupt)
888 static void __remove_hrtimer(struct hrtimer
*timer
,
889 struct hrtimer_clock_base
*base
,
890 unsigned long newstate
, int reprogram
)
892 /* High res. callback list. NOP for !HIGHRES */
893 if (hrtimer_cb_pending(timer
))
894 hrtimer_remove_cb_pending(timer
);
897 * Remove the timer from the rbtree and replace the
898 * first entry pointer if necessary.
900 if (base
->first
== &timer
->node
) {
901 base
->first
= rb_next(&timer
->node
);
902 /* Reprogram the clock event device. if enabled */
903 if (reprogram
&& hrtimer_hres_active())
904 hrtimer_force_reprogram(base
->cpu_base
);
906 rb_erase(&timer
->node
, &base
->active
);
908 timer
->state
= newstate
;
912 * remove hrtimer, called with base lock held
915 remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
)
917 if (hrtimer_is_queued(timer
)) {
921 * Remove the timer and force reprogramming when high
922 * resolution mode is active and the timer is on the current
923 * CPU. If we remove a timer on another CPU, reprogramming is
924 * skipped. The interrupt event on this CPU is fired and
925 * reprogramming happens in the interrupt handler. This is a
926 * rare case and less expensive than a smp call.
928 debug_hrtimer_deactivate(timer
);
929 timer_stats_hrtimer_clear_start_info(timer
);
930 reprogram
= base
->cpu_base
== &__get_cpu_var(hrtimer_bases
);
931 __remove_hrtimer(timer
, base
, HRTIMER_STATE_INACTIVE
,
939 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
940 * @timer: the timer to be added
942 * @delta_ns: "slack" range for the timer
943 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
947 * 1 when the timer was active
950 hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
, unsigned long delta_ns
,
951 const enum hrtimer_mode mode
)
953 struct hrtimer_clock_base
*base
, *new_base
;
957 base
= lock_hrtimer_base(timer
, &flags
);
959 /* Remove an active timer from the queue: */
960 ret
= remove_hrtimer(timer
, base
);
962 /* Switch the timer base, if necessary: */
963 new_base
= switch_hrtimer_base(timer
, base
);
965 if (mode
== HRTIMER_MODE_REL
) {
966 tim
= ktime_add_safe(tim
, new_base
->get_time());
968 * CONFIG_TIME_LOW_RES is a temporary way for architectures
969 * to signal that they simply return xtime in
970 * do_gettimeoffset(). In this case we want to round up by
971 * resolution when starting a relative timer, to avoid short
972 * timeouts. This will go away with the GTOD framework.
974 #ifdef CONFIG_TIME_LOW_RES
975 tim
= ktime_add_safe(tim
, base
->resolution
);
979 hrtimer_set_expires_range_ns(timer
, tim
, delta_ns
);
981 timer_stats_hrtimer_set_start_info(timer
);
984 * Only allow reprogramming if the new base is on this CPU.
985 * (it might still be on another CPU if the timer was pending)
987 enqueue_hrtimer(timer
, new_base
,
988 new_base
->cpu_base
== &__get_cpu_var(hrtimer_bases
));
991 * The timer may be expired and moved to the cb_pending
992 * list. We can not raise the softirq with base lock held due
993 * to a possible deadlock with runqueue lock.
995 raise
= timer
->state
== HRTIMER_STATE_PENDING
;
998 * We use preempt_disable to prevent this task from migrating after
999 * setting up the softirq and raising it. Otherwise, if me migrate
1000 * we will raise the softirq on the wrong CPU.
1004 unlock_hrtimer_base(timer
, &flags
);
1007 hrtimer_raise_softirq();
1012 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns
);
1015 * hrtimer_start - (re)start an hrtimer on the current CPU
1016 * @timer: the timer to be added
1018 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1022 * 1 when the timer was active
1025 hrtimer_start(struct hrtimer
*timer
, ktime_t tim
, const enum hrtimer_mode mode
)
1027 return hrtimer_start_range_ns(timer
, tim
, 0, mode
);
1029 EXPORT_SYMBOL_GPL(hrtimer_start
);
1033 * hrtimer_try_to_cancel - try to deactivate a timer
1034 * @timer: hrtimer to stop
1037 * 0 when the timer was not active
1038 * 1 when the timer was active
1039 * -1 when the timer is currently excuting the callback function and
1042 int hrtimer_try_to_cancel(struct hrtimer
*timer
)
1044 struct hrtimer_clock_base
*base
;
1045 unsigned long flags
;
1048 base
= lock_hrtimer_base(timer
, &flags
);
1050 if (!hrtimer_callback_running(timer
))
1051 ret
= remove_hrtimer(timer
, base
);
1053 unlock_hrtimer_base(timer
, &flags
);
1058 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel
);
1061 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1062 * @timer: the timer to be cancelled
1065 * 0 when the timer was not active
1066 * 1 when the timer was active
1068 int hrtimer_cancel(struct hrtimer
*timer
)
1071 int ret
= hrtimer_try_to_cancel(timer
);
1078 EXPORT_SYMBOL_GPL(hrtimer_cancel
);
1081 * hrtimer_get_remaining - get remaining time for the timer
1082 * @timer: the timer to read
1084 ktime_t
hrtimer_get_remaining(const struct hrtimer
*timer
)
1086 struct hrtimer_clock_base
*base
;
1087 unsigned long flags
;
1090 base
= lock_hrtimer_base(timer
, &flags
);
1091 rem
= hrtimer_expires_remaining(timer
);
1092 unlock_hrtimer_base(timer
, &flags
);
1096 EXPORT_SYMBOL_GPL(hrtimer_get_remaining
);
1100 * hrtimer_get_next_event - get the time until next expiry event
1102 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1105 ktime_t
hrtimer_get_next_event(void)
1107 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1108 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
1109 ktime_t delta
, mindelta
= { .tv64
= KTIME_MAX
};
1110 unsigned long flags
;
1113 spin_lock_irqsave(&cpu_base
->lock
, flags
);
1115 if (!hrtimer_hres_active()) {
1116 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
1117 struct hrtimer
*timer
;
1122 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
1123 delta
.tv64
= hrtimer_get_expires_tv64(timer
);
1124 delta
= ktime_sub(delta
, base
->get_time());
1125 if (delta
.tv64
< mindelta
.tv64
)
1126 mindelta
.tv64
= delta
.tv64
;
1130 spin_unlock_irqrestore(&cpu_base
->lock
, flags
);
1132 if (mindelta
.tv64
< 0)
1138 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1139 enum hrtimer_mode mode
)
1141 struct hrtimer_cpu_base
*cpu_base
;
1143 memset(timer
, 0, sizeof(struct hrtimer
));
1145 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1147 if (clock_id
== CLOCK_REALTIME
&& mode
!= HRTIMER_MODE_ABS
)
1148 clock_id
= CLOCK_MONOTONIC
;
1150 timer
->base
= &cpu_base
->clock_base
[clock_id
];
1151 INIT_LIST_HEAD(&timer
->cb_entry
);
1152 hrtimer_init_timer_hres(timer
);
1154 #ifdef CONFIG_TIMER_STATS
1155 timer
->start_site
= NULL
;
1156 timer
->start_pid
= -1;
1157 memset(timer
->start_comm
, 0, TASK_COMM_LEN
);
1162 * hrtimer_init - initialize a timer to the given clock
1163 * @timer: the timer to be initialized
1164 * @clock_id: the clock to be used
1165 * @mode: timer mode abs/rel
1167 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1168 enum hrtimer_mode mode
)
1170 debug_hrtimer_init(timer
);
1171 __hrtimer_init(timer
, clock_id
, mode
);
1173 EXPORT_SYMBOL_GPL(hrtimer_init
);
1176 * hrtimer_get_res - get the timer resolution for a clock
1177 * @which_clock: which clock to query
1178 * @tp: pointer to timespec variable to store the resolution
1180 * Store the resolution of the clock selected by @which_clock in the
1181 * variable pointed to by @tp.
1183 int hrtimer_get_res(const clockid_t which_clock
, struct timespec
*tp
)
1185 struct hrtimer_cpu_base
*cpu_base
;
1187 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1188 *tp
= ktime_to_timespec(cpu_base
->clock_base
[which_clock
].resolution
);
1192 EXPORT_SYMBOL_GPL(hrtimer_get_res
);
1194 static void run_hrtimer_pending(struct hrtimer_cpu_base
*cpu_base
)
1196 spin_lock_irq(&cpu_base
->lock
);
1198 while (!list_empty(&cpu_base
->cb_pending
)) {
1199 enum hrtimer_restart (*fn
)(struct hrtimer
*);
1200 struct hrtimer
*timer
;
1202 int emulate_hardirq_ctx
= 0;
1204 timer
= list_entry(cpu_base
->cb_pending
.next
,
1205 struct hrtimer
, cb_entry
);
1207 debug_hrtimer_deactivate(timer
);
1208 timer_stats_account_hrtimer(timer
);
1210 fn
= timer
->function
;
1212 * A timer might have been added to the cb_pending list
1213 * when it was migrated during a cpu-offline operation.
1214 * Emulate hardirq context for such timers.
1216 if (timer
->cb_mode
== HRTIMER_CB_IRQSAFE_PERCPU
||
1217 timer
->cb_mode
== HRTIMER_CB_IRQSAFE_UNLOCKED
)
1218 emulate_hardirq_ctx
= 1;
1220 __remove_hrtimer(timer
, timer
->base
, HRTIMER_STATE_CALLBACK
, 0);
1221 spin_unlock_irq(&cpu_base
->lock
);
1223 if (unlikely(emulate_hardirq_ctx
)) {
1224 local_irq_disable();
1225 restart
= fn(timer
);
1228 restart
= fn(timer
);
1230 spin_lock_irq(&cpu_base
->lock
);
1232 timer
->state
&= ~HRTIMER_STATE_CALLBACK
;
1233 if (restart
== HRTIMER_RESTART
) {
1234 BUG_ON(hrtimer_active(timer
));
1236 * Enqueue the timer, allow reprogramming of the event
1239 enqueue_hrtimer(timer
, timer
->base
, 1);
1240 } else if (hrtimer_active(timer
)) {
1242 * If the timer was rearmed on another CPU, reprogram
1245 struct hrtimer_clock_base
*base
= timer
->base
;
1247 if (base
->first
== &timer
->node
&&
1248 hrtimer_reprogram(timer
, base
)) {
1250 * Timer is expired. Thus move it from tree to
1251 * pending list again.
1253 __remove_hrtimer(timer
, base
,
1254 HRTIMER_STATE_PENDING
, 0);
1255 list_add_tail(&timer
->cb_entry
,
1256 &base
->cpu_base
->cb_pending
);
1260 spin_unlock_irq(&cpu_base
->lock
);
1263 static void __run_hrtimer(struct hrtimer
*timer
)
1265 struct hrtimer_clock_base
*base
= timer
->base
;
1266 struct hrtimer_cpu_base
*cpu_base
= base
->cpu_base
;
1267 enum hrtimer_restart (*fn
)(struct hrtimer
*);
1270 debug_hrtimer_deactivate(timer
);
1271 __remove_hrtimer(timer
, base
, HRTIMER_STATE_CALLBACK
, 0);
1272 timer_stats_account_hrtimer(timer
);
1274 fn
= timer
->function
;
1275 if (timer
->cb_mode
== HRTIMER_CB_IRQSAFE_PERCPU
||
1276 timer
->cb_mode
== HRTIMER_CB_IRQSAFE_UNLOCKED
) {
1278 * Used for scheduler timers, avoid lock inversion with
1279 * rq->lock and tasklist_lock.
1281 * These timers are required to deal with enqueue expiry
1282 * themselves and are not allowed to migrate.
1284 spin_unlock(&cpu_base
->lock
);
1285 restart
= fn(timer
);
1286 spin_lock(&cpu_base
->lock
);
1288 restart
= fn(timer
);
1291 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1292 * reprogramming of the event hardware. This happens at the end of this
1295 if (restart
!= HRTIMER_NORESTART
) {
1296 BUG_ON(timer
->state
!= HRTIMER_STATE_CALLBACK
);
1297 enqueue_hrtimer(timer
, base
, 0);
1299 timer
->state
&= ~HRTIMER_STATE_CALLBACK
;
1302 #ifdef CONFIG_HIGH_RES_TIMERS
1305 * High resolution timer interrupt
1306 * Called with interrupts disabled
1308 void hrtimer_interrupt(struct clock_event_device
*dev
)
1310 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1311 struct hrtimer_clock_base
*base
;
1312 ktime_t expires_next
, now
;
1315 BUG_ON(!cpu_base
->hres_active
);
1316 cpu_base
->nr_events
++;
1317 dev
->next_event
.tv64
= KTIME_MAX
;
1322 expires_next
.tv64
= KTIME_MAX
;
1324 base
= cpu_base
->clock_base
;
1326 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1328 struct rb_node
*node
;
1330 spin_lock(&cpu_base
->lock
);
1332 basenow
= ktime_add(now
, base
->offset
);
1334 while ((node
= base
->first
)) {
1335 struct hrtimer
*timer
;
1337 timer
= rb_entry(node
, struct hrtimer
, node
);
1340 * The immediate goal for using the softexpires is
1341 * minimizing wakeups, not running timers at the
1342 * earliest interrupt after their soft expiration.
1343 * This allows us to avoid using a Priority Search
1344 * Tree, which can answer a stabbing querry for
1345 * overlapping intervals and instead use the simple
1346 * BST we already have.
1347 * We don't add extra wakeups by delaying timers that
1348 * are right-of a not yet expired timer, because that
1349 * timer will have to trigger a wakeup anyway.
1352 if (basenow
.tv64
< hrtimer_get_softexpires_tv64(timer
)) {
1355 expires
= ktime_sub(hrtimer_get_expires(timer
),
1357 if (expires
.tv64
< expires_next
.tv64
)
1358 expires_next
= expires
;
1362 /* Move softirq callbacks to the pending list */
1363 if (timer
->cb_mode
== HRTIMER_CB_SOFTIRQ
) {
1364 __remove_hrtimer(timer
, base
,
1365 HRTIMER_STATE_PENDING
, 0);
1366 list_add_tail(&timer
->cb_entry
,
1367 &base
->cpu_base
->cb_pending
);
1372 __run_hrtimer(timer
);
1374 spin_unlock(&cpu_base
->lock
);
1378 cpu_base
->expires_next
= expires_next
;
1380 /* Reprogramming necessary ? */
1381 if (expires_next
.tv64
!= KTIME_MAX
) {
1382 if (tick_program_event(expires_next
, 0))
1386 /* Raise softirq ? */
1388 raise_softirq(HRTIMER_SOFTIRQ
);
1392 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1394 * hrtimer_peek_ahead_timers will peek at the timer queue of
1395 * the current cpu and check if there are any timers for which
1396 * the soft expires time has passed. If any such timers exist,
1397 * they are run immediately and then removed from the timer queue.
1400 void hrtimer_peek_ahead_timers(void)
1402 struct tick_device
*td
;
1403 unsigned long flags
;
1405 if (!hrtimer_hres_active())
1408 local_irq_save(flags
);
1409 td
= &__get_cpu_var(tick_cpu_device
);
1410 if (td
&& td
->evtdev
)
1411 hrtimer_interrupt(td
->evtdev
);
1412 local_irq_restore(flags
);
1415 static void run_hrtimer_softirq(struct softirq_action
*h
)
1417 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases
));
1420 #endif /* CONFIG_HIGH_RES_TIMERS */
1423 * Called from timer softirq every jiffy, expire hrtimers:
1425 * For HRT its the fall back code to run the softirq in the timer
1426 * softirq context in case the hrtimer initialization failed or has
1427 * not been done yet.
1429 void hrtimer_run_pending(void)
1431 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1433 if (hrtimer_hres_active())
1437 * This _is_ ugly: We have to check in the softirq context,
1438 * whether we can switch to highres and / or nohz mode. The
1439 * clocksource switch happens in the timer interrupt with
1440 * xtime_lock held. Notification from there only sets the
1441 * check bit in the tick_oneshot code, otherwise we might
1442 * deadlock vs. xtime_lock.
1444 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1445 hrtimer_switch_to_hres();
1447 run_hrtimer_pending(cpu_base
);
1451 * Called from hardirq context every jiffy
1453 void hrtimer_run_queues(void)
1455 struct rb_node
*node
;
1456 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1457 struct hrtimer_clock_base
*base
;
1458 int index
, gettime
= 1;
1460 if (hrtimer_hres_active())
1463 for (index
= 0; index
< HRTIMER_MAX_CLOCK_BASES
; index
++) {
1464 base
= &cpu_base
->clock_base
[index
];
1470 hrtimer_get_softirq_time(cpu_base
);
1474 spin_lock(&cpu_base
->lock
);
1476 while ((node
= base
->first
)) {
1477 struct hrtimer
*timer
;
1479 timer
= rb_entry(node
, struct hrtimer
, node
);
1480 if (base
->softirq_time
.tv64
<=
1481 hrtimer_get_expires_tv64(timer
))
1484 if (timer
->cb_mode
== HRTIMER_CB_SOFTIRQ
) {
1485 __remove_hrtimer(timer
, base
,
1486 HRTIMER_STATE_PENDING
, 0);
1487 list_add_tail(&timer
->cb_entry
,
1488 &base
->cpu_base
->cb_pending
);
1492 __run_hrtimer(timer
);
1494 spin_unlock(&cpu_base
->lock
);
1499 * Sleep related functions:
1501 static enum hrtimer_restart
hrtimer_wakeup(struct hrtimer
*timer
)
1503 struct hrtimer_sleeper
*t
=
1504 container_of(timer
, struct hrtimer_sleeper
, timer
);
1505 struct task_struct
*task
= t
->task
;
1509 wake_up_process(task
);
1511 return HRTIMER_NORESTART
;
1514 void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
, struct task_struct
*task
)
1516 sl
->timer
.function
= hrtimer_wakeup
;
1518 #ifdef CONFIG_HIGH_RES_TIMERS
1519 sl
->timer
.cb_mode
= HRTIMER_CB_IRQSAFE_UNLOCKED
;
1523 static int __sched
do_nanosleep(struct hrtimer_sleeper
*t
, enum hrtimer_mode mode
)
1525 hrtimer_init_sleeper(t
, current
);
1528 set_current_state(TASK_INTERRUPTIBLE
);
1529 hrtimer_start_expires(&t
->timer
, mode
);
1530 if (!hrtimer_active(&t
->timer
))
1533 if (likely(t
->task
))
1536 hrtimer_cancel(&t
->timer
);
1537 mode
= HRTIMER_MODE_ABS
;
1539 } while (t
->task
&& !signal_pending(current
));
1541 __set_current_state(TASK_RUNNING
);
1543 return t
->task
== NULL
;
1546 static int update_rmtp(struct hrtimer
*timer
, struct timespec __user
*rmtp
)
1548 struct timespec rmt
;
1551 rem
= hrtimer_expires_remaining(timer
);
1554 rmt
= ktime_to_timespec(rem
);
1556 if (copy_to_user(rmtp
, &rmt
, sizeof(*rmtp
)))
1562 long __sched
hrtimer_nanosleep_restart(struct restart_block
*restart
)
1564 struct hrtimer_sleeper t
;
1565 struct timespec __user
*rmtp
;
1568 hrtimer_init_on_stack(&t
.timer
, restart
->nanosleep
.index
,
1570 hrtimer_set_expires_tv64(&t
.timer
, restart
->nanosleep
.expires
);
1572 if (do_nanosleep(&t
, HRTIMER_MODE_ABS
))
1575 rmtp
= restart
->nanosleep
.rmtp
;
1577 ret
= update_rmtp(&t
.timer
, rmtp
);
1582 /* The other values in restart are already filled in */
1583 ret
= -ERESTART_RESTARTBLOCK
;
1585 destroy_hrtimer_on_stack(&t
.timer
);
1589 long hrtimer_nanosleep(struct timespec
*rqtp
, struct timespec __user
*rmtp
,
1590 const enum hrtimer_mode mode
, const clockid_t clockid
)
1592 struct restart_block
*restart
;
1593 struct hrtimer_sleeper t
;
1595 unsigned long slack
;
1597 slack
= current
->timer_slack_ns
;
1598 if (rt_task(current
))
1601 hrtimer_init_on_stack(&t
.timer
, clockid
, mode
);
1602 hrtimer_set_expires_range_ns(&t
.timer
, timespec_to_ktime(*rqtp
), slack
);
1603 if (do_nanosleep(&t
, mode
))
1606 /* Absolute timers do not update the rmtp value and restart: */
1607 if (mode
== HRTIMER_MODE_ABS
) {
1608 ret
= -ERESTARTNOHAND
;
1613 ret
= update_rmtp(&t
.timer
, rmtp
);
1618 restart
= ¤t_thread_info()->restart_block
;
1619 restart
->fn
= hrtimer_nanosleep_restart
;
1620 restart
->nanosleep
.index
= t
.timer
.base
->index
;
1621 restart
->nanosleep
.rmtp
= rmtp
;
1622 restart
->nanosleep
.expires
= hrtimer_get_expires_tv64(&t
.timer
);
1624 ret
= -ERESTART_RESTARTBLOCK
;
1626 destroy_hrtimer_on_stack(&t
.timer
);
1631 sys_nanosleep(struct timespec __user
*rqtp
, struct timespec __user
*rmtp
)
1635 if (copy_from_user(&tu
, rqtp
, sizeof(tu
)))
1638 if (!timespec_valid(&tu
))
1641 return hrtimer_nanosleep(&tu
, rmtp
, HRTIMER_MODE_REL
, CLOCK_MONOTONIC
);
1645 * Functions related to boot-time initialization:
1647 static void __cpuinit
init_hrtimers_cpu(int cpu
)
1649 struct hrtimer_cpu_base
*cpu_base
= &per_cpu(hrtimer_bases
, cpu
);
1652 spin_lock_init(&cpu_base
->lock
);
1654 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++)
1655 cpu_base
->clock_base
[i
].cpu_base
= cpu_base
;
1657 INIT_LIST_HEAD(&cpu_base
->cb_pending
);
1658 hrtimer_init_hres(cpu_base
);
1661 #ifdef CONFIG_HOTPLUG_CPU
1663 static int migrate_hrtimer_list(struct hrtimer_clock_base
*old_base
,
1664 struct hrtimer_clock_base
*new_base
, int dcpu
)
1666 struct hrtimer
*timer
;
1667 struct rb_node
*node
;
1670 while ((node
= rb_first(&old_base
->active
))) {
1671 timer
= rb_entry(node
, struct hrtimer
, node
);
1672 BUG_ON(hrtimer_callback_running(timer
));
1673 debug_hrtimer_deactivate(timer
);
1676 * Should not happen. Per CPU timers should be
1677 * canceled _before_ the migration code is called
1679 if (timer
->cb_mode
== HRTIMER_CB_IRQSAFE_PERCPU
) {
1680 __remove_hrtimer(timer
, old_base
,
1681 HRTIMER_STATE_INACTIVE
, 0);
1682 WARN(1, "hrtimer (%p %p)active but cpu %d dead\n",
1683 timer
, timer
->function
, dcpu
);
1688 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1689 * timer could be seen as !active and just vanish away
1690 * under us on another CPU
1692 __remove_hrtimer(timer
, old_base
, HRTIMER_STATE_MIGRATE
, 0);
1693 timer
->base
= new_base
;
1695 * Enqueue the timer. Allow reprogramming of the event device
1697 enqueue_hrtimer(timer
, new_base
, 1);
1699 #ifdef CONFIG_HIGH_RES_TIMERS
1701 * Happens with high res enabled when the timer was
1702 * already expired and the callback mode is
1703 * HRTIMER_CB_IRQSAFE_UNLOCKED (hrtimer_sleeper). The
1704 * enqueue code does not move them to the soft irq
1705 * pending list for performance/latency reasons, but
1706 * in the migration state, we need to do that
1707 * otherwise we end up with a stale timer.
1709 if (timer
->state
== HRTIMER_STATE_MIGRATE
) {
1710 timer
->state
= HRTIMER_STATE_PENDING
;
1711 list_add_tail(&timer
->cb_entry
,
1712 &new_base
->cpu_base
->cb_pending
);
1716 /* Clear the migration state bit */
1717 timer
->state
&= ~HRTIMER_STATE_MIGRATE
;
1722 #ifdef CONFIG_HIGH_RES_TIMERS
1723 static int migrate_hrtimer_pending(struct hrtimer_cpu_base
*old_base
,
1724 struct hrtimer_cpu_base
*new_base
)
1726 struct hrtimer
*timer
;
1729 while (!list_empty(&old_base
->cb_pending
)) {
1730 timer
= list_entry(old_base
->cb_pending
.next
,
1731 struct hrtimer
, cb_entry
);
1733 __remove_hrtimer(timer
, timer
->base
, HRTIMER_STATE_PENDING
, 0);
1734 timer
->base
= &new_base
->clock_base
[timer
->base
->index
];
1735 list_add_tail(&timer
->cb_entry
, &new_base
->cb_pending
);
1741 static int migrate_hrtimer_pending(struct hrtimer_cpu_base
*old_base
,
1742 struct hrtimer_cpu_base
*new_base
)
1748 static void migrate_hrtimers(int cpu
)
1750 struct hrtimer_cpu_base
*old_base
, *new_base
;
1753 BUG_ON(cpu_online(cpu
));
1754 old_base
= &per_cpu(hrtimer_bases
, cpu
);
1755 new_base
= &get_cpu_var(hrtimer_bases
);
1757 tick_cancel_sched_timer(cpu
);
1759 * The caller is globally serialized and nobody else
1760 * takes two locks at once, deadlock is not possible.
1762 spin_lock_irq(&new_base
->lock
);
1763 spin_lock_nested(&old_base
->lock
, SINGLE_DEPTH_NESTING
);
1765 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1766 if (migrate_hrtimer_list(&old_base
->clock_base
[i
],
1767 &new_base
->clock_base
[i
], cpu
))
1771 if (migrate_hrtimer_pending(old_base
, new_base
))
1774 spin_unlock(&old_base
->lock
);
1775 spin_unlock_irq(&new_base
->lock
);
1776 put_cpu_var(hrtimer_bases
);
1779 hrtimer_raise_softirq();
1781 #endif /* CONFIG_HOTPLUG_CPU */
1783 static int __cpuinit
hrtimer_cpu_notify(struct notifier_block
*self
,
1784 unsigned long action
, void *hcpu
)
1786 unsigned int cpu
= (long)hcpu
;
1790 case CPU_UP_PREPARE
:
1791 case CPU_UP_PREPARE_FROZEN
:
1792 init_hrtimers_cpu(cpu
);
1795 #ifdef CONFIG_HOTPLUG_CPU
1797 case CPU_DEAD_FROZEN
:
1798 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD
, &cpu
);
1799 migrate_hrtimers(cpu
);
1810 static struct notifier_block __cpuinitdata hrtimers_nb
= {
1811 .notifier_call
= hrtimer_cpu_notify
,
1814 void __init
hrtimers_init(void)
1816 hrtimer_cpu_notify(&hrtimers_nb
, (unsigned long)CPU_UP_PREPARE
,
1817 (void *)(long)smp_processor_id());
1818 register_cpu_notifier(&hrtimers_nb
);
1819 #ifdef CONFIG_HIGH_RES_TIMERS
1820 open_softirq(HRTIMER_SOFTIRQ
, run_hrtimer_softirq
);
1825 * schedule_hrtimeout_range - sleep until timeout
1826 * @expires: timeout value (ktime_t)
1827 * @delta: slack in expires timeout (ktime_t)
1828 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1830 * Make the current task sleep until the given expiry time has
1831 * elapsed. The routine will return immediately unless
1832 * the current task state has been set (see set_current_state()).
1834 * The @delta argument gives the kernel the freedom to schedule the
1835 * actual wakeup to a time that is both power and performance friendly.
1836 * The kernel give the normal best effort behavior for "@expires+@delta",
1837 * but may decide to fire the timer earlier, but no earlier than @expires.
1839 * You can set the task state as follows -
1841 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1842 * pass before the routine returns.
1844 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1845 * delivered to the current task.
1847 * The current task state is guaranteed to be TASK_RUNNING when this
1850 * Returns 0 when the timer has expired otherwise -EINTR
1852 int __sched
schedule_hrtimeout_range(ktime_t
*expires
, unsigned long delta
,
1853 const enum hrtimer_mode mode
)
1855 struct hrtimer_sleeper t
;
1858 * Optimize when a zero timeout value is given. It does not
1859 * matter whether this is an absolute or a relative time.
1861 if (expires
&& !expires
->tv64
) {
1862 __set_current_state(TASK_RUNNING
);
1867 * A NULL parameter means "inifinte"
1871 __set_current_state(TASK_RUNNING
);
1875 hrtimer_init_on_stack(&t
.timer
, CLOCK_MONOTONIC
, mode
);
1876 hrtimer_set_expires_range_ns(&t
.timer
, *expires
, delta
);
1878 hrtimer_init_sleeper(&t
, current
);
1880 hrtimer_start_expires(&t
.timer
, mode
);
1881 if (!hrtimer_active(&t
.timer
))
1887 hrtimer_cancel(&t
.timer
);
1888 destroy_hrtimer_on_stack(&t
.timer
);
1890 __set_current_state(TASK_RUNNING
);
1892 return !t
.task
? 0 : -EINTR
;
1894 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range
);
1897 * schedule_hrtimeout - sleep until timeout
1898 * @expires: timeout value (ktime_t)
1899 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1901 * Make the current task sleep until the given expiry time has
1902 * elapsed. The routine will return immediately unless
1903 * the current task state has been set (see set_current_state()).
1905 * You can set the task state as follows -
1907 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1908 * pass before the routine returns.
1910 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1911 * delivered to the current task.
1913 * The current task state is guaranteed to be TASK_RUNNING when this
1916 * Returns 0 when the timer has expired otherwise -EINTR
1918 int __sched
schedule_hrtimeout(ktime_t
*expires
,
1919 const enum hrtimer_mode mode
)
1921 return schedule_hrtimeout_range(expires
, 0, mode
);
1923 EXPORT_SYMBOL_GPL(schedule_hrtimeout
);