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/irq.h>
36 #include <linux/module.h>
37 #include <linux/percpu.h>
38 #include <linux/hrtimer.h>
39 #include <linux/notifier.h>
40 #include <linux/syscalls.h>
41 #include <linux/kallsyms.h>
42 #include <linux/interrupt.h>
43 #include <linux/tick.h>
44 #include <linux/seq_file.h>
45 #include <linux/err.h>
46 #include <linux/debugobjects.h>
48 #include <asm/uaccess.h>
51 * ktime_get - get the monotonic time in ktime_t format
53 * returns the time in ktime_t format
55 ktime_t ktime_get(void)
61 return timespec_to_ktime(now);
63 EXPORT_SYMBOL_GPL(ktime_get);
66 * ktime_get_real - get the real (wall-) time in ktime_t format
68 * returns the time in ktime_t format
70 ktime_t ktime_get_real(void)
76 return timespec_to_ktime(now);
79 EXPORT_SYMBOL_GPL(ktime_get_real);
84 * Note: If we want to add new timer bases, we have to skip the two
85 * clock ids captured by the cpu-timers. We do this by holding empty
86 * entries rather than doing math adjustment of the clock ids.
87 * This ensures that we capture erroneous accesses to these clock ids
88 * rather than moving them into the range of valid clock id's.
90 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
96 .index = CLOCK_REALTIME,
97 .get_time = &ktime_get_real,
98 .resolution = KTIME_LOW_RES,
101 .index = CLOCK_MONOTONIC,
102 .get_time = &ktime_get,
103 .resolution = KTIME_LOW_RES,
109 * ktime_get_ts - get the monotonic clock in timespec format
110 * @ts: pointer to timespec variable
112 * The function calculates the monotonic clock from the realtime
113 * clock and the wall_to_monotonic offset and stores the result
114 * in normalized timespec format in the variable pointed to by @ts.
116 void ktime_get_ts(struct timespec *ts)
118 struct timespec tomono;
122 seq = read_seqbegin(&xtime_lock);
124 tomono = wall_to_monotonic;
126 } while (read_seqretry(&xtime_lock, seq));
128 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
129 ts->tv_nsec + tomono.tv_nsec);
131 EXPORT_SYMBOL_GPL(ktime_get_ts);
134 * Get the coarse grained time at the softirq based on xtime and
137 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
139 ktime_t xtim, tomono;
140 struct timespec xts, tom;
144 seq = read_seqbegin(&xtime_lock);
145 xts = current_kernel_time();
146 tom = wall_to_monotonic;
147 } while (read_seqretry(&xtime_lock, seq));
149 xtim = timespec_to_ktime(xts);
150 tomono = timespec_to_ktime(tom);
151 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
152 base->clock_base[CLOCK_MONOTONIC].softirq_time =
153 ktime_add(xtim, tomono);
157 * Functions and macros which are different for UP/SMP systems are kept in a
163 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
164 * means that all timers which are tied to this base via timer->base are
165 * locked, and the base itself is locked too.
167 * So __run_timers/migrate_timers can safely modify all timers which could
168 * be found on the lists/queues.
170 * When the timer's base is locked, and the timer removed from list, it is
171 * possible to set timer->base = NULL and drop the lock: the timer remains
175 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
176 unsigned long *flags)
178 struct hrtimer_clock_base *base;
182 if (likely(base != NULL)) {
183 spin_lock_irqsave(&base->cpu_base->lock, *flags);
184 if (likely(base == timer->base))
186 /* The timer has migrated to another CPU: */
187 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
194 * Switch the timer base to the current CPU when possible.
196 static inline struct hrtimer_clock_base *
197 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
199 struct hrtimer_clock_base *new_base;
200 struct hrtimer_cpu_base *new_cpu_base;
202 new_cpu_base = &__get_cpu_var(hrtimer_bases);
203 new_base = &new_cpu_base->clock_base[base->index];
205 if (base != new_base) {
207 * We are trying to schedule the timer on the local CPU.
208 * However we can't change timer's base while it is running,
209 * so we keep it on the same CPU. No hassle vs. reprogramming
210 * the event source in the high resolution case. The softirq
211 * code will take care of this when the timer function has
212 * completed. There is no conflict as we hold the lock until
213 * the timer is enqueued.
215 if (unlikely(hrtimer_callback_running(timer)))
218 /* See the comment in lock_timer_base() */
220 spin_unlock(&base->cpu_base->lock);
221 spin_lock(&new_base->cpu_base->lock);
222 timer->base = new_base;
227 #else /* CONFIG_SMP */
229 static inline struct hrtimer_clock_base *
230 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
232 struct hrtimer_clock_base *base = timer->base;
234 spin_lock_irqsave(&base->cpu_base->lock, *flags);
239 # define switch_hrtimer_base(t, b) (b)
241 #endif /* !CONFIG_SMP */
244 * Functions for the union type storage format of ktime_t which are
245 * too large for inlining:
247 #if BITS_PER_LONG < 64
248 # ifndef CONFIG_KTIME_SCALAR
250 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
252 * @nsec: the scalar nsec value to add
254 * Returns the sum of kt and nsec in ktime_t format
256 ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
260 if (likely(nsec < NSEC_PER_SEC)) {
263 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
265 tmp = ktime_set((long)nsec, rem);
268 return ktime_add(kt, tmp);
271 EXPORT_SYMBOL_GPL(ktime_add_ns);
274 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
276 * @nsec: the scalar nsec value to subtract
278 * Returns the subtraction of @nsec from @kt in ktime_t format
280 ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
284 if (likely(nsec < NSEC_PER_SEC)) {
287 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
289 tmp = ktime_set((long)nsec, rem);
292 return ktime_sub(kt, tmp);
295 EXPORT_SYMBOL_GPL(ktime_sub_ns);
296 # endif /* !CONFIG_KTIME_SCALAR */
299 * Divide a ktime value by a nanosecond value
301 u64 ktime_divns(const ktime_t kt, s64 div)
306 dclc = ktime_to_ns(kt);
307 /* Make sure the divisor is less than 2^32: */
313 do_div(dclc, (unsigned long) div);
317 #endif /* BITS_PER_LONG >= 64 */
320 * Add two ktime values and do a safety check for overflow:
322 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
324 ktime_t res = ktime_add(lhs, rhs);
327 * We use KTIME_SEC_MAX here, the maximum timeout which we can
328 * return to user space in a timespec:
330 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
331 res = ktime_set(KTIME_SEC_MAX, 0);
336 EXPORT_SYMBOL_GPL(ktime_add_safe);
338 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
340 static struct debug_obj_descr hrtimer_debug_descr;
343 * fixup_init is called when:
344 * - an active object is initialized
346 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
348 struct hrtimer *timer = addr;
351 case ODEBUG_STATE_ACTIVE:
352 hrtimer_cancel(timer);
353 debug_object_init(timer, &hrtimer_debug_descr);
361 * fixup_activate is called when:
362 * - an active object is activated
363 * - an unknown object is activated (might be a statically initialized object)
365 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
369 case ODEBUG_STATE_NOTAVAILABLE:
373 case ODEBUG_STATE_ACTIVE:
382 * fixup_free is called when:
383 * - an active object is freed
385 static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
387 struct hrtimer *timer = addr;
390 case ODEBUG_STATE_ACTIVE:
391 hrtimer_cancel(timer);
392 debug_object_free(timer, &hrtimer_debug_descr);
399 static struct debug_obj_descr hrtimer_debug_descr = {
401 .fixup_init = hrtimer_fixup_init,
402 .fixup_activate = hrtimer_fixup_activate,
403 .fixup_free = hrtimer_fixup_free,
406 static inline void debug_hrtimer_init(struct hrtimer *timer)
408 debug_object_init(timer, &hrtimer_debug_descr);
411 static inline void debug_hrtimer_activate(struct hrtimer *timer)
413 debug_object_activate(timer, &hrtimer_debug_descr);
416 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
418 debug_object_deactivate(timer, &hrtimer_debug_descr);
421 static inline void debug_hrtimer_free(struct hrtimer *timer)
423 debug_object_free(timer, &hrtimer_debug_descr);
426 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
427 enum hrtimer_mode mode);
429 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
430 enum hrtimer_mode mode)
432 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
433 __hrtimer_init(timer, clock_id, mode);
436 void destroy_hrtimer_on_stack(struct hrtimer *timer)
438 debug_object_free(timer, &hrtimer_debug_descr);
442 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
443 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
444 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
448 * Check, whether the timer is on the callback pending list
450 static inline int hrtimer_cb_pending(const struct hrtimer *timer)
452 return timer->state & HRTIMER_STATE_PENDING;
456 * Remove a timer from the callback pending list
458 static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
460 list_del_init(&timer->cb_entry);
463 /* High resolution timer related functions */
464 #ifdef CONFIG_HIGH_RES_TIMERS
467 * High resolution timer enabled ?
469 static int hrtimer_hres_enabled __read_mostly = 1;
472 * Enable / Disable high resolution mode
474 static int __init setup_hrtimer_hres(char *str)
476 if (!strcmp(str, "off"))
477 hrtimer_hres_enabled = 0;
478 else if (!strcmp(str, "on"))
479 hrtimer_hres_enabled = 1;
485 __setup("highres=", setup_hrtimer_hres);
488 * hrtimer_high_res_enabled - query, if the highres mode is enabled
490 static inline int hrtimer_is_hres_enabled(void)
492 return hrtimer_hres_enabled;
496 * Is the high resolution mode active ?
498 static inline int hrtimer_hres_active(void)
500 return __get_cpu_var(hrtimer_bases).hres_active;
504 * Reprogram the event source with checking both queues for the
506 * Called with interrupts disabled and base->lock held
508 static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
511 struct hrtimer_clock_base *base = cpu_base->clock_base;
514 cpu_base->expires_next.tv64 = KTIME_MAX;
516 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
517 struct hrtimer *timer;
521 timer = rb_entry(base->first, struct hrtimer, node);
522 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
523 if (expires.tv64 < cpu_base->expires_next.tv64)
524 cpu_base->expires_next = expires;
527 if (cpu_base->expires_next.tv64 != KTIME_MAX)
528 tick_program_event(cpu_base->expires_next, 1);
532 * Shared reprogramming for clock_realtime and clock_monotonic
534 * When a timer is enqueued and expires earlier than the already enqueued
535 * timers, we have to check, whether it expires earlier than the timer for
536 * which the clock event device was armed.
538 * Called with interrupts disabled and base->cpu_base.lock held
540 static int hrtimer_reprogram(struct hrtimer *timer,
541 struct hrtimer_clock_base *base)
543 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
544 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
547 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
550 * When the callback is running, we do not reprogram the clock event
551 * device. The timer callback is either running on a different CPU or
552 * the callback is executed in the hrtimer_interrupt context. The
553 * reprogramming is handled either by the softirq, which called the
554 * callback or at the end of the hrtimer_interrupt.
556 if (hrtimer_callback_running(timer))
560 * CLOCK_REALTIME timer might be requested with an absolute
561 * expiry time which is less than base->offset. Nothing wrong
562 * about that, just avoid to call into the tick code, which
563 * has now objections against negative expiry values.
565 if (expires.tv64 < 0)
568 if (expires.tv64 >= expires_next->tv64)
572 * Clockevents returns -ETIME, when the event was in the past.
574 res = tick_program_event(expires, 0);
575 if (!IS_ERR_VALUE(res))
576 *expires_next = expires;
582 * Retrigger next event is called after clock was set
584 * Called with interrupts disabled via on_each_cpu()
586 static void retrigger_next_event(void *arg)
588 struct hrtimer_cpu_base *base;
589 struct timespec realtime_offset;
592 if (!hrtimer_hres_active())
596 seq = read_seqbegin(&xtime_lock);
597 set_normalized_timespec(&realtime_offset,
598 -wall_to_monotonic.tv_sec,
599 -wall_to_monotonic.tv_nsec);
600 } while (read_seqretry(&xtime_lock, seq));
602 base = &__get_cpu_var(hrtimer_bases);
604 /* Adjust CLOCK_REALTIME offset */
605 spin_lock(&base->lock);
606 base->clock_base[CLOCK_REALTIME].offset =
607 timespec_to_ktime(realtime_offset);
609 hrtimer_force_reprogram(base);
610 spin_unlock(&base->lock);
614 * Clock realtime was set
616 * Change the offset of the realtime clock vs. the monotonic
619 * We might have to reprogram the high resolution timer interrupt. On
620 * SMP we call the architecture specific code to retrigger _all_ high
621 * resolution timer interrupts. On UP we just disable interrupts and
622 * call the high resolution interrupt code.
624 void clock_was_set(void)
626 /* Retrigger the CPU local events everywhere */
627 on_each_cpu(retrigger_next_event, NULL, 1);
631 * During resume we might have to reprogram the high resolution timer
632 * interrupt (on the local CPU):
634 void hres_timers_resume(void)
636 /* Retrigger the CPU local events: */
637 retrigger_next_event(NULL);
641 * Initialize the high resolution related parts of cpu_base
643 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
645 base->expires_next.tv64 = KTIME_MAX;
646 base->hres_active = 0;
650 * Initialize the high resolution related parts of a hrtimer
652 static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
657 * When High resolution timers are active, try to reprogram. Note, that in case
658 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
659 * check happens. The timer gets enqueued into the rbtree. The reprogramming
660 * and expiry check is done in the hrtimer_interrupt or in the softirq.
662 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
663 struct hrtimer_clock_base *base)
665 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
667 /* Timer is expired, act upon the callback mode */
668 switch(timer->cb_mode) {
669 case HRTIMER_CB_IRQSAFE_PERCPU:
670 case HRTIMER_CB_IRQSAFE_UNLOCKED:
672 * This is solely for the sched tick emulation with
673 * dynamic tick support to ensure that we do not
674 * restart the tick right on the edge and end up with
675 * the tick timer in the softirq ! The calling site
676 * takes care of this. Also used for hrtimer sleeper !
678 debug_hrtimer_deactivate(timer);
680 case HRTIMER_CB_SOFTIRQ:
682 * Move everything else into the softirq pending list !
684 list_add_tail(&timer->cb_entry,
685 &base->cpu_base->cb_pending);
686 timer->state = HRTIMER_STATE_PENDING;
696 * Switch to high resolution mode
698 static int hrtimer_switch_to_hres(void)
700 int cpu = smp_processor_id();
701 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
704 if (base->hres_active)
707 local_irq_save(flags);
709 if (tick_init_highres()) {
710 local_irq_restore(flags);
711 printk(KERN_WARNING "Could not switch to high resolution "
712 "mode on CPU %d\n", cpu);
715 base->hres_active = 1;
716 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
717 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
719 tick_setup_sched_timer();
721 /* "Retrigger" the interrupt to get things going */
722 retrigger_next_event(NULL);
723 local_irq_restore(flags);
724 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
729 static inline void hrtimer_raise_softirq(void)
731 raise_softirq(HRTIMER_SOFTIRQ);
736 static inline int hrtimer_hres_active(void) { return 0; }
737 static inline int hrtimer_is_hres_enabled(void) { return 0; }
738 static inline int hrtimer_switch_to_hres(void) { return 0; }
739 static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
740 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
741 struct hrtimer_clock_base *base)
745 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
746 static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
747 static inline int hrtimer_reprogram(struct hrtimer *timer,
748 struct hrtimer_clock_base *base)
752 static inline void hrtimer_raise_softirq(void) { }
754 #endif /* CONFIG_HIGH_RES_TIMERS */
756 #ifdef CONFIG_TIMER_STATS
757 void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
759 if (timer->start_site)
762 timer->start_site = addr;
763 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
764 timer->start_pid = current->pid;
769 * Counterpart to lock_hrtimer_base above:
772 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
774 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
778 * hrtimer_forward - forward the timer expiry
779 * @timer: hrtimer to forward
780 * @now: forward past this time
781 * @interval: the interval to forward
783 * Forward the timer expiry so it will expire in the future.
784 * Returns the number of overruns.
786 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
791 delta = ktime_sub(now, hrtimer_get_expires(timer));
796 if (interval.tv64 < timer->base->resolution.tv64)
797 interval.tv64 = timer->base->resolution.tv64;
799 if (unlikely(delta.tv64 >= interval.tv64)) {
800 s64 incr = ktime_to_ns(interval);
802 orun = ktime_divns(delta, incr);
803 hrtimer_add_expires_ns(timer, incr * orun);
804 if (hrtimer_get_expires_tv64(timer) > now.tv64)
807 * This (and the ktime_add() below) is the
808 * correction for exact:
812 hrtimer_add_expires(timer, interval);
816 EXPORT_SYMBOL_GPL(hrtimer_forward);
819 * enqueue_hrtimer - internal function to (re)start a timer
821 * The timer is inserted in expiry order. Insertion into the
822 * red black tree is O(log(n)). Must hold the base lock.
824 static void enqueue_hrtimer(struct hrtimer *timer,
825 struct hrtimer_clock_base *base, int reprogram)
827 struct rb_node **link = &base->active.rb_node;
828 struct rb_node *parent = NULL;
829 struct hrtimer *entry;
832 debug_hrtimer_activate(timer);
835 * Find the right place in the rbtree:
839 entry = rb_entry(parent, struct hrtimer, node);
841 * We dont care about collisions. Nodes with
842 * the same expiry time stay together.
844 if (hrtimer_get_expires_tv64(timer) <
845 hrtimer_get_expires_tv64(entry)) {
846 link = &(*link)->rb_left;
848 link = &(*link)->rb_right;
854 * Insert the timer to the rbtree and check whether it
855 * replaces the first pending timer
859 * Reprogram the clock event device. When the timer is already
860 * expired hrtimer_enqueue_reprogram has either called the
861 * callback or added it to the pending list and raised the
864 * This is a NOP for !HIGHRES
866 if (reprogram && hrtimer_enqueue_reprogram(timer, base))
869 base->first = &timer->node;
872 rb_link_node(&timer->node, parent, link);
873 rb_insert_color(&timer->node, &base->active);
875 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
876 * state of a possibly running callback.
878 timer->state |= HRTIMER_STATE_ENQUEUED;
882 * __remove_hrtimer - internal function to remove a timer
884 * Caller must hold the base lock.
886 * High resolution timer mode reprograms the clock event device when the
887 * timer is the one which expires next. The caller can disable this by setting
888 * reprogram to zero. This is useful, when the context does a reprogramming
889 * anyway (e.g. timer interrupt)
891 static void __remove_hrtimer(struct hrtimer *timer,
892 struct hrtimer_clock_base *base,
893 unsigned long newstate, int reprogram)
895 /* High res. callback list. NOP for !HIGHRES */
896 if (hrtimer_cb_pending(timer))
897 hrtimer_remove_cb_pending(timer);
900 * Remove the timer from the rbtree and replace the
901 * first entry pointer if necessary.
903 if (base->first == &timer->node) {
904 base->first = rb_next(&timer->node);
905 /* Reprogram the clock event device. if enabled */
906 if (reprogram && hrtimer_hres_active())
907 hrtimer_force_reprogram(base->cpu_base);
909 rb_erase(&timer->node, &base->active);
911 timer->state = newstate;
915 * remove hrtimer, called with base lock held
918 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
920 if (hrtimer_is_queued(timer)) {
924 * Remove the timer and force reprogramming when high
925 * resolution mode is active and the timer is on the current
926 * CPU. If we remove a timer on another CPU, reprogramming is
927 * skipped. The interrupt event on this CPU is fired and
928 * reprogramming happens in the interrupt handler. This is a
929 * rare case and less expensive than a smp call.
931 debug_hrtimer_deactivate(timer);
932 timer_stats_hrtimer_clear_start_info(timer);
933 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
934 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
942 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
943 * @timer: the timer to be added
945 * @delta_ns: "slack" range for the timer
946 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
950 * 1 when the timer was active
953 hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
954 const enum hrtimer_mode mode)
956 struct hrtimer_clock_base *base, *new_base;
960 base = lock_hrtimer_base(timer, &flags);
962 /* Remove an active timer from the queue: */
963 ret = remove_hrtimer(timer, base);
965 /* Switch the timer base, if necessary: */
966 new_base = switch_hrtimer_base(timer, base);
968 if (mode == HRTIMER_MODE_REL) {
969 tim = ktime_add_safe(tim, new_base->get_time());
971 * CONFIG_TIME_LOW_RES is a temporary way for architectures
972 * to signal that they simply return xtime in
973 * do_gettimeoffset(). In this case we want to round up by
974 * resolution when starting a relative timer, to avoid short
975 * timeouts. This will go away with the GTOD framework.
977 #ifdef CONFIG_TIME_LOW_RES
978 tim = ktime_add_safe(tim, base->resolution);
982 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
984 timer_stats_hrtimer_set_start_info(timer);
987 * Only allow reprogramming if the new base is on this CPU.
988 * (it might still be on another CPU if the timer was pending)
990 enqueue_hrtimer(timer, new_base,
991 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
994 * The timer may be expired and moved to the cb_pending
995 * list. We can not raise the softirq with base lock held due
996 * to a possible deadlock with runqueue lock.
998 raise = timer->state == HRTIMER_STATE_PENDING;
1001 * We use preempt_disable to prevent this task from migrating after
1002 * setting up the softirq and raising it. Otherwise, if me migrate
1003 * we will raise the softirq on the wrong CPU.
1007 unlock_hrtimer_base(timer, &flags);
1010 hrtimer_raise_softirq();
1015 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1018 * hrtimer_start - (re)start an hrtimer on the current CPU
1019 * @timer: the timer to be added
1021 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1025 * 1 when the timer was active
1028 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1030 return hrtimer_start_range_ns(timer, tim, 0, mode);
1032 EXPORT_SYMBOL_GPL(hrtimer_start);
1036 * hrtimer_try_to_cancel - try to deactivate a timer
1037 * @timer: hrtimer to stop
1040 * 0 when the timer was not active
1041 * 1 when the timer was active
1042 * -1 when the timer is currently excuting the callback function and
1045 int hrtimer_try_to_cancel(struct hrtimer *timer)
1047 struct hrtimer_clock_base *base;
1048 unsigned long flags;
1051 base = lock_hrtimer_base(timer, &flags);
1053 if (!hrtimer_callback_running(timer))
1054 ret = remove_hrtimer(timer, base);
1056 unlock_hrtimer_base(timer, &flags);
1061 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1064 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1065 * @timer: the timer to be cancelled
1068 * 0 when the timer was not active
1069 * 1 when the timer was active
1071 int hrtimer_cancel(struct hrtimer *timer)
1074 int ret = hrtimer_try_to_cancel(timer);
1081 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1084 * hrtimer_get_remaining - get remaining time for the timer
1085 * @timer: the timer to read
1087 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1089 struct hrtimer_clock_base *base;
1090 unsigned long flags;
1093 base = lock_hrtimer_base(timer, &flags);
1094 rem = hrtimer_expires_remaining(timer);
1095 unlock_hrtimer_base(timer, &flags);
1099 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1103 * hrtimer_get_next_event - get the time until next expiry event
1105 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1108 ktime_t hrtimer_get_next_event(void)
1110 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1111 struct hrtimer_clock_base *base = cpu_base->clock_base;
1112 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1113 unsigned long flags;
1116 spin_lock_irqsave(&cpu_base->lock, flags);
1118 if (!hrtimer_hres_active()) {
1119 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1120 struct hrtimer *timer;
1125 timer = rb_entry(base->first, struct hrtimer, node);
1126 delta.tv64 = hrtimer_get_expires_tv64(timer);
1127 delta = ktime_sub(delta, base->get_time());
1128 if (delta.tv64 < mindelta.tv64)
1129 mindelta.tv64 = delta.tv64;
1133 spin_unlock_irqrestore(&cpu_base->lock, flags);
1135 if (mindelta.tv64 < 0)
1141 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1142 enum hrtimer_mode mode)
1144 struct hrtimer_cpu_base *cpu_base;
1146 memset(timer, 0, sizeof(struct hrtimer));
1148 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1150 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1151 clock_id = CLOCK_MONOTONIC;
1153 timer->base = &cpu_base->clock_base[clock_id];
1154 INIT_LIST_HEAD(&timer->cb_entry);
1155 hrtimer_init_timer_hres(timer);
1157 #ifdef CONFIG_TIMER_STATS
1158 timer->start_site = NULL;
1159 timer->start_pid = -1;
1160 memset(timer->start_comm, 0, TASK_COMM_LEN);
1165 * hrtimer_init - initialize a timer to the given clock
1166 * @timer: the timer to be initialized
1167 * @clock_id: the clock to be used
1168 * @mode: timer mode abs/rel
1170 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1171 enum hrtimer_mode mode)
1173 debug_hrtimer_init(timer);
1174 __hrtimer_init(timer, clock_id, mode);
1176 EXPORT_SYMBOL_GPL(hrtimer_init);
1179 * hrtimer_get_res - get the timer resolution for a clock
1180 * @which_clock: which clock to query
1181 * @tp: pointer to timespec variable to store the resolution
1183 * Store the resolution of the clock selected by @which_clock in the
1184 * variable pointed to by @tp.
1186 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1188 struct hrtimer_cpu_base *cpu_base;
1190 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1191 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
1195 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1197 static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
1199 spin_lock_irq(&cpu_base->lock);
1201 while (!list_empty(&cpu_base->cb_pending)) {
1202 enum hrtimer_restart (*fn)(struct hrtimer *);
1203 struct hrtimer *timer;
1205 int emulate_hardirq_ctx = 0;
1207 timer = list_entry(cpu_base->cb_pending.next,
1208 struct hrtimer, cb_entry);
1210 debug_hrtimer_deactivate(timer);
1211 timer_stats_account_hrtimer(timer);
1213 fn = timer->function;
1215 * A timer might have been added to the cb_pending list
1216 * when it was migrated during a cpu-offline operation.
1217 * Emulate hardirq context for such timers.
1219 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU ||
1220 timer->cb_mode == HRTIMER_CB_IRQSAFE_UNLOCKED)
1221 emulate_hardirq_ctx = 1;
1223 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
1224 spin_unlock_irq(&cpu_base->lock);
1226 if (unlikely(emulate_hardirq_ctx)) {
1227 local_irq_disable();
1228 restart = fn(timer);
1231 restart = fn(timer);
1233 spin_lock_irq(&cpu_base->lock);
1235 timer->state &= ~HRTIMER_STATE_CALLBACK;
1236 if (restart == HRTIMER_RESTART) {
1237 BUG_ON(hrtimer_active(timer));
1239 * Enqueue the timer, allow reprogramming of the event
1242 enqueue_hrtimer(timer, timer->base, 1);
1243 } else if (hrtimer_active(timer)) {
1245 * If the timer was rearmed on another CPU, reprogram
1248 struct hrtimer_clock_base *base = timer->base;
1250 if (base->first == &timer->node &&
1251 hrtimer_reprogram(timer, base)) {
1253 * Timer is expired. Thus move it from tree to
1254 * pending list again.
1256 __remove_hrtimer(timer, base,
1257 HRTIMER_STATE_PENDING, 0);
1258 list_add_tail(&timer->cb_entry,
1259 &base->cpu_base->cb_pending);
1263 spin_unlock_irq(&cpu_base->lock);
1266 static void __run_hrtimer(struct hrtimer *timer)
1268 struct hrtimer_clock_base *base = timer->base;
1269 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1270 enum hrtimer_restart (*fn)(struct hrtimer *);
1273 debug_hrtimer_deactivate(timer);
1274 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1275 timer_stats_account_hrtimer(timer);
1277 fn = timer->function;
1278 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU ||
1279 timer->cb_mode == HRTIMER_CB_IRQSAFE_UNLOCKED) {
1281 * Used for scheduler timers, avoid lock inversion with
1282 * rq->lock and tasklist_lock.
1284 * These timers are required to deal with enqueue expiry
1285 * themselves and are not allowed to migrate.
1287 spin_unlock(&cpu_base->lock);
1288 restart = fn(timer);
1289 spin_lock(&cpu_base->lock);
1291 restart = fn(timer);
1294 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1295 * reprogramming of the event hardware. This happens at the end of this
1298 if (restart != HRTIMER_NORESTART) {
1299 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1300 enqueue_hrtimer(timer, base, 0);
1302 timer->state &= ~HRTIMER_STATE_CALLBACK;
1305 #ifdef CONFIG_HIGH_RES_TIMERS
1308 * High resolution timer interrupt
1309 * Called with interrupts disabled
1311 void hrtimer_interrupt(struct clock_event_device *dev)
1313 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1314 struct hrtimer_clock_base *base;
1315 ktime_t expires_next, now;
1318 BUG_ON(!cpu_base->hres_active);
1319 cpu_base->nr_events++;
1320 dev->next_event.tv64 = KTIME_MAX;
1325 expires_next.tv64 = KTIME_MAX;
1327 base = cpu_base->clock_base;
1329 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1331 struct rb_node *node;
1333 spin_lock(&cpu_base->lock);
1335 basenow = ktime_add(now, base->offset);
1337 while ((node = base->first)) {
1338 struct hrtimer *timer;
1340 timer = rb_entry(node, struct hrtimer, node);
1343 * The immediate goal for using the softexpires is
1344 * minimizing wakeups, not running timers at the
1345 * earliest interrupt after their soft expiration.
1346 * This allows us to avoid using a Priority Search
1347 * Tree, which can answer a stabbing querry for
1348 * overlapping intervals and instead use the simple
1349 * BST we already have.
1350 * We don't add extra wakeups by delaying timers that
1351 * are right-of a not yet expired timer, because that
1352 * timer will have to trigger a wakeup anyway.
1355 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
1358 expires = ktime_sub(hrtimer_get_expires(timer),
1360 if (expires.tv64 < expires_next.tv64)
1361 expires_next = expires;
1365 /* Move softirq callbacks to the pending list */
1366 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1367 __remove_hrtimer(timer, base,
1368 HRTIMER_STATE_PENDING, 0);
1369 list_add_tail(&timer->cb_entry,
1370 &base->cpu_base->cb_pending);
1375 __run_hrtimer(timer);
1377 spin_unlock(&cpu_base->lock);
1381 cpu_base->expires_next = expires_next;
1383 /* Reprogramming necessary ? */
1384 if (expires_next.tv64 != KTIME_MAX) {
1385 if (tick_program_event(expires_next, 0))
1389 /* Raise softirq ? */
1391 raise_softirq(HRTIMER_SOFTIRQ);
1395 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1397 * hrtimer_peek_ahead_timers will peek at the timer queue of
1398 * the current cpu and check if there are any timers for which
1399 * the soft expires time has passed. If any such timers exist,
1400 * they are run immediately and then removed from the timer queue.
1403 void hrtimer_peek_ahead_timers(void)
1405 struct tick_device *td;
1406 unsigned long flags;
1408 if (!hrtimer_hres_active())
1411 local_irq_save(flags);
1412 td = &__get_cpu_var(tick_cpu_device);
1413 if (td && td->evtdev)
1414 hrtimer_interrupt(td->evtdev);
1415 local_irq_restore(flags);
1418 static void run_hrtimer_softirq(struct softirq_action *h)
1420 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
1423 #endif /* CONFIG_HIGH_RES_TIMERS */
1426 * Called from timer softirq every jiffy, expire hrtimers:
1428 * For HRT its the fall back code to run the softirq in the timer
1429 * softirq context in case the hrtimer initialization failed or has
1430 * not been done yet.
1432 void hrtimer_run_pending(void)
1434 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1436 if (hrtimer_hres_active())
1440 * This _is_ ugly: We have to check in the softirq context,
1441 * whether we can switch to highres and / or nohz mode. The
1442 * clocksource switch happens in the timer interrupt with
1443 * xtime_lock held. Notification from there only sets the
1444 * check bit in the tick_oneshot code, otherwise we might
1445 * deadlock vs. xtime_lock.
1447 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1448 hrtimer_switch_to_hres();
1450 run_hrtimer_pending(cpu_base);
1454 * Called from hardirq context every jiffy
1456 void hrtimer_run_queues(void)
1458 struct rb_node *node;
1459 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1460 struct hrtimer_clock_base *base;
1461 int index, gettime = 1;
1463 if (hrtimer_hres_active())
1466 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1467 base = &cpu_base->clock_base[index];
1473 hrtimer_get_softirq_time(cpu_base);
1477 spin_lock(&cpu_base->lock);
1479 while ((node = base->first)) {
1480 struct hrtimer *timer;
1482 timer = rb_entry(node, struct hrtimer, node);
1483 if (base->softirq_time.tv64 <=
1484 hrtimer_get_expires_tv64(timer))
1487 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1488 __remove_hrtimer(timer, base,
1489 HRTIMER_STATE_PENDING, 0);
1490 list_add_tail(&timer->cb_entry,
1491 &base->cpu_base->cb_pending);
1495 __run_hrtimer(timer);
1497 spin_unlock(&cpu_base->lock);
1502 * Sleep related functions:
1504 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1506 struct hrtimer_sleeper *t =
1507 container_of(timer, struct hrtimer_sleeper, timer);
1508 struct task_struct *task = t->task;
1512 wake_up_process(task);
1514 return HRTIMER_NORESTART;
1517 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1519 sl->timer.function = hrtimer_wakeup;
1521 #ifdef CONFIG_HIGH_RES_TIMERS
1522 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_UNLOCKED;
1526 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1528 hrtimer_init_sleeper(t, current);
1531 set_current_state(TASK_INTERRUPTIBLE);
1532 hrtimer_start_expires(&t->timer, mode);
1533 if (!hrtimer_active(&t->timer))
1536 if (likely(t->task))
1539 hrtimer_cancel(&t->timer);
1540 mode = HRTIMER_MODE_ABS;
1542 } while (t->task && !signal_pending(current));
1544 __set_current_state(TASK_RUNNING);
1546 return t->task == NULL;
1549 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1551 struct timespec rmt;
1554 rem = hrtimer_expires_remaining(timer);
1557 rmt = ktime_to_timespec(rem);
1559 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1565 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1567 struct hrtimer_sleeper t;
1568 struct timespec __user *rmtp;
1571 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1573 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1575 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1578 rmtp = restart->nanosleep.rmtp;
1580 ret = update_rmtp(&t.timer, rmtp);
1585 /* The other values in restart are already filled in */
1586 ret = -ERESTART_RESTARTBLOCK;
1588 destroy_hrtimer_on_stack(&t.timer);
1592 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1593 const enum hrtimer_mode mode, const clockid_t clockid)
1595 struct restart_block *restart;
1596 struct hrtimer_sleeper t;
1598 unsigned long slack;
1600 slack = current->timer_slack_ns;
1601 if (rt_task(current))
1604 hrtimer_init_on_stack(&t.timer, clockid, mode);
1605 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1606 if (do_nanosleep(&t, mode))
1609 /* Absolute timers do not update the rmtp value and restart: */
1610 if (mode == HRTIMER_MODE_ABS) {
1611 ret = -ERESTARTNOHAND;
1616 ret = update_rmtp(&t.timer, rmtp);
1621 restart = ¤t_thread_info()->restart_block;
1622 restart->fn = hrtimer_nanosleep_restart;
1623 restart->nanosleep.index = t.timer.base->index;
1624 restart->nanosleep.rmtp = rmtp;
1625 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1627 ret = -ERESTART_RESTARTBLOCK;
1629 destroy_hrtimer_on_stack(&t.timer);
1634 sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1638 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1641 if (!timespec_valid(&tu))
1644 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1648 * Functions related to boot-time initialization:
1650 static void __cpuinit init_hrtimers_cpu(int cpu)
1652 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1655 spin_lock_init(&cpu_base->lock);
1657 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1658 cpu_base->clock_base[i].cpu_base = cpu_base;
1660 INIT_LIST_HEAD(&cpu_base->cb_pending);
1661 hrtimer_init_hres(cpu_base);
1664 #ifdef CONFIG_HOTPLUG_CPU
1666 static int migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1667 struct hrtimer_clock_base *new_base, int dcpu)
1669 struct hrtimer *timer;
1670 struct rb_node *node;
1673 while ((node = rb_first(&old_base->active))) {
1674 timer = rb_entry(node, struct hrtimer, node);
1675 BUG_ON(hrtimer_callback_running(timer));
1676 debug_hrtimer_deactivate(timer);
1679 * Should not happen. Per CPU timers should be
1680 * canceled _before_ the migration code is called
1682 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU) {
1683 __remove_hrtimer(timer, old_base,
1684 HRTIMER_STATE_INACTIVE, 0);
1685 WARN(1, "hrtimer (%p %p)active but cpu %d dead\n",
1686 timer, timer->function, dcpu);
1691 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1692 * timer could be seen as !active and just vanish away
1693 * under us on another CPU
1695 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
1696 timer->base = new_base;
1698 * Enqueue the timer. Allow reprogramming of the event device
1700 enqueue_hrtimer(timer, new_base, 1);
1702 #ifdef CONFIG_HIGH_RES_TIMERS
1704 * Happens with high res enabled when the timer was
1705 * already expired and the callback mode is
1706 * HRTIMER_CB_IRQSAFE_UNLOCKED (hrtimer_sleeper). The
1707 * enqueue code does not move them to the soft irq
1708 * pending list for performance/latency reasons, but
1709 * in the migration state, we need to do that
1710 * otherwise we end up with a stale timer.
1712 if (timer->state == HRTIMER_STATE_MIGRATE) {
1713 timer->state = HRTIMER_STATE_PENDING;
1714 list_add_tail(&timer->cb_entry,
1715 &new_base->cpu_base->cb_pending);
1719 /* Clear the migration state bit */
1720 timer->state &= ~HRTIMER_STATE_MIGRATE;
1725 #ifdef CONFIG_HIGH_RES_TIMERS
1726 static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base,
1727 struct hrtimer_cpu_base *new_base)
1729 struct hrtimer *timer;
1732 while (!list_empty(&old_base->cb_pending)) {
1733 timer = list_entry(old_base->cb_pending.next,
1734 struct hrtimer, cb_entry);
1736 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_PENDING, 0);
1737 timer->base = &new_base->clock_base[timer->base->index];
1738 list_add_tail(&timer->cb_entry, &new_base->cb_pending);
1744 static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base,
1745 struct hrtimer_cpu_base *new_base)
1751 static void migrate_hrtimers(int cpu)
1753 struct hrtimer_cpu_base *old_base, *new_base;
1756 BUG_ON(cpu_online(cpu));
1757 old_base = &per_cpu(hrtimer_bases, cpu);
1758 new_base = &get_cpu_var(hrtimer_bases);
1760 tick_cancel_sched_timer(cpu);
1762 * The caller is globally serialized and nobody else
1763 * takes two locks at once, deadlock is not possible.
1765 spin_lock_irq(&new_base->lock);
1766 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1768 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1769 if (migrate_hrtimer_list(&old_base->clock_base[i],
1770 &new_base->clock_base[i], cpu))
1774 if (migrate_hrtimer_pending(old_base, new_base))
1777 spin_unlock(&old_base->lock);
1778 spin_unlock_irq(&new_base->lock);
1779 put_cpu_var(hrtimer_bases);
1782 hrtimer_raise_softirq();
1784 #endif /* CONFIG_HOTPLUG_CPU */
1786 static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
1787 unsigned long action, void *hcpu)
1789 unsigned int cpu = (long)hcpu;
1793 case CPU_UP_PREPARE:
1794 case CPU_UP_PREPARE_FROZEN:
1795 init_hrtimers_cpu(cpu);
1798 #ifdef CONFIG_HOTPLUG_CPU
1800 case CPU_DEAD_FROZEN:
1801 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
1802 migrate_hrtimers(cpu);
1813 static struct notifier_block __cpuinitdata hrtimers_nb = {
1814 .notifier_call = hrtimer_cpu_notify,
1817 void __init hrtimers_init(void)
1819 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1820 (void *)(long)smp_processor_id());
1821 register_cpu_notifier(&hrtimers_nb);
1822 #ifdef CONFIG_HIGH_RES_TIMERS
1823 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1828 * schedule_hrtimeout_range - sleep until timeout
1829 * @expires: timeout value (ktime_t)
1830 * @delta: slack in expires timeout (ktime_t)
1831 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1833 * Make the current task sleep until the given expiry time has
1834 * elapsed. The routine will return immediately unless
1835 * the current task state has been set (see set_current_state()).
1837 * The @delta argument gives the kernel the freedom to schedule the
1838 * actual wakeup to a time that is both power and performance friendly.
1839 * The kernel give the normal best effort behavior for "@expires+@delta",
1840 * but may decide to fire the timer earlier, but no earlier than @expires.
1842 * You can set the task state as follows -
1844 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1845 * pass before the routine returns.
1847 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1848 * delivered to the current task.
1850 * The current task state is guaranteed to be TASK_RUNNING when this
1853 * Returns 0 when the timer has expired otherwise -EINTR
1855 int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1856 const enum hrtimer_mode mode)
1858 struct hrtimer_sleeper t;
1861 * Optimize when a zero timeout value is given. It does not
1862 * matter whether this is an absolute or a relative time.
1864 if (expires && !expires->tv64) {
1865 __set_current_state(TASK_RUNNING);
1870 * A NULL parameter means "inifinte"
1874 __set_current_state(TASK_RUNNING);
1878 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1879 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1881 hrtimer_init_sleeper(&t, current);
1883 hrtimer_start_expires(&t.timer, mode);
1884 if (!hrtimer_active(&t.timer))
1890 hrtimer_cancel(&t.timer);
1891 destroy_hrtimer_on_stack(&t.timer);
1893 __set_current_state(TASK_RUNNING);
1895 return !t.task ? 0 : -EINTR;
1897 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1900 * schedule_hrtimeout - sleep until timeout
1901 * @expires: timeout value (ktime_t)
1902 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1904 * Make the current task sleep until the given expiry time has
1905 * elapsed. The routine will return immediately unless
1906 * the current task state has been set (see set_current_state()).
1908 * You can set the task state as follows -
1910 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1911 * pass before the routine returns.
1913 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1914 * delivered to the current task.
1916 * The current task state is guaranteed to be TASK_RUNNING when this
1919 * Returns 0 when the timer has expired otherwise -EINTR
1921 int __sched schedule_hrtimeout(ktime_t *expires,
1922 const enum hrtimer_mode mode)
1924 return schedule_hrtimeout_range(expires, 0, mode);
1926 EXPORT_SYMBOL_GPL(schedule_hrtimeout);