Merge branch 'linus' into timers/hrtimers
[linux-2.6.git] / kernel / hrtimer.c
blob2c40ee8f44bda2a6406de4f15daca70806ea0990
1 /*
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:
15 * - itimers
16 * - POSIX timers
17 * - nanosleep
18 * - precise in-kernel timing
20 * Started by: Thomas Gleixner and Ingo Molnar
22 * Credits:
23 * based on kernel/timer.c
25 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
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>
49 /**
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)
56 struct timespec now;
58 ktime_get_ts(&now);
60 return timespec_to_ktime(now);
62 EXPORT_SYMBOL_GPL(ktime_get);
64 /**
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)
71 struct timespec now;
73 getnstimeofday(&now);
75 return timespec_to_ktime(now);
78 EXPORT_SYMBOL_GPL(ktime_get_real);
81 * The timer bases:
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) =
92 .clock_base =
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;
118 unsigned long seq;
120 do {
121 seq = read_seqbegin(&xtime_lock);
122 getnstimeofday(ts);
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
134 * wall_to_monotonic.
136 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
138 ktime_t xtim, tomono;
139 struct timespec xts, tom;
140 unsigned long seq;
142 do {
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
157 * single place
159 #ifdef CONFIG_SMP
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
171 * locked.
173 static
174 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
175 unsigned long *flags)
177 struct hrtimer_clock_base *base;
179 for (;;) {
180 base = timer->base;
181 if (likely(base != NULL)) {
182 spin_lock_irqsave(&base->cpu_base->lock, *flags);
183 if (likely(base == timer->base))
184 return base;
185 /* The timer has migrated to another CPU: */
186 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
188 cpu_relax();
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)))
215 return base;
217 /* See the comment in lock_timer_base() */
218 timer->base = NULL;
219 spin_unlock(&base->cpu_base->lock);
220 spin_lock(&new_base->cpu_base->lock);
221 timer->base = new_base;
223 return 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);
235 return base;
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
250 * @kt: addend
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)
257 ktime_t tmp;
259 if (likely(nsec < NSEC_PER_SEC)) {
260 tmp.tv64 = nsec;
261 } else {
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
274 * @kt: minuend
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)
281 ktime_t tmp;
283 if (likely(nsec < NSEC_PER_SEC)) {
284 tmp.tv64 = nsec;
285 } else {
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)
302 u64 dclc;
303 int sft = 0;
305 dclc = ktime_to_ns(kt);
306 /* Make sure the divisor is less than 2^32: */
307 while (div >> 32) {
308 sft++;
309 div >>= 1;
311 dclc >>= sft;
312 do_div(dclc, (unsigned long) div);
314 return dclc;
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);
332 return res;
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;
347 switch (state) {
348 case ODEBUG_STATE_ACTIVE:
349 hrtimer_cancel(timer);
350 debug_object_init(timer, &hrtimer_debug_descr);
351 return 1;
352 default:
353 return 0;
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)
364 switch (state) {
366 case ODEBUG_STATE_NOTAVAILABLE:
367 WARN_ON_ONCE(1);
368 return 0;
370 case ODEBUG_STATE_ACTIVE:
371 WARN_ON(1);
373 default:
374 return 0;
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;
386 switch (state) {
387 case ODEBUG_STATE_ACTIVE:
388 hrtimer_cancel(timer);
389 debug_object_free(timer, &hrtimer_debug_descr);
390 return 1;
391 default:
392 return 0;
396 static struct debug_obj_descr hrtimer_debug_descr = {
397 .name = "hrtimer",
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);
438 #else
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) { }
442 #endif
444 /* High resolution timer related functions */
445 #ifdef CONFIG_HIGH_RES_TIMERS
448 * High resolution timer enabled ?
450 static int hrtimer_hres_enabled __read_mostly = 1;
453 * Enable / Disable high resolution mode
455 static int __init setup_hrtimer_hres(char *str)
457 if (!strcmp(str, "off"))
458 hrtimer_hres_enabled = 0;
459 else if (!strcmp(str, "on"))
460 hrtimer_hres_enabled = 1;
461 else
462 return 0;
463 return 1;
466 __setup("highres=", setup_hrtimer_hres);
469 * hrtimer_high_res_enabled - query, if the highres mode is enabled
471 static inline int hrtimer_is_hres_enabled(void)
473 return hrtimer_hres_enabled;
477 * Is the high resolution mode active ?
479 static inline int hrtimer_hres_active(void)
481 return __get_cpu_var(hrtimer_bases).hres_active;
485 * Reprogram the event source with checking both queues for the
486 * next event
487 * Called with interrupts disabled and base->lock held
489 static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
491 int i;
492 struct hrtimer_clock_base *base = cpu_base->clock_base;
493 ktime_t expires;
495 cpu_base->expires_next.tv64 = KTIME_MAX;
497 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
498 struct hrtimer *timer;
500 if (!base->first)
501 continue;
502 timer = rb_entry(base->first, struct hrtimer, node);
503 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
504 if (expires.tv64 < cpu_base->expires_next.tv64)
505 cpu_base->expires_next = expires;
508 if (cpu_base->expires_next.tv64 != KTIME_MAX)
509 tick_program_event(cpu_base->expires_next, 1);
513 * Shared reprogramming for clock_realtime and clock_monotonic
515 * When a timer is enqueued and expires earlier than the already enqueued
516 * timers, we have to check, whether it expires earlier than the timer for
517 * which the clock event device was armed.
519 * Called with interrupts disabled and base->cpu_base.lock held
521 static int hrtimer_reprogram(struct hrtimer *timer,
522 struct hrtimer_clock_base *base)
524 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
525 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
526 int res;
528 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
531 * When the callback is running, we do not reprogram the clock event
532 * device. The timer callback is either running on a different CPU or
533 * the callback is executed in the hrtimer_interrupt context. The
534 * reprogramming is handled either by the softirq, which called the
535 * callback or at the end of the hrtimer_interrupt.
537 if (hrtimer_callback_running(timer))
538 return 0;
541 * CLOCK_REALTIME timer might be requested with an absolute
542 * expiry time which is less than base->offset. Nothing wrong
543 * about that, just avoid to call into the tick code, which
544 * has now objections against negative expiry values.
546 if (expires.tv64 < 0)
547 return -ETIME;
549 if (expires.tv64 >= expires_next->tv64)
550 return 0;
553 * Clockevents returns -ETIME, when the event was in the past.
555 res = tick_program_event(expires, 0);
556 if (!IS_ERR_VALUE(res))
557 *expires_next = expires;
558 return res;
563 * Retrigger next event is called after clock was set
565 * Called with interrupts disabled via on_each_cpu()
567 static void retrigger_next_event(void *arg)
569 struct hrtimer_cpu_base *base;
570 struct timespec realtime_offset;
571 unsigned long seq;
573 if (!hrtimer_hres_active())
574 return;
576 do {
577 seq = read_seqbegin(&xtime_lock);
578 set_normalized_timespec(&realtime_offset,
579 -wall_to_monotonic.tv_sec,
580 -wall_to_monotonic.tv_nsec);
581 } while (read_seqretry(&xtime_lock, seq));
583 base = &__get_cpu_var(hrtimer_bases);
585 /* Adjust CLOCK_REALTIME offset */
586 spin_lock(&base->lock);
587 base->clock_base[CLOCK_REALTIME].offset =
588 timespec_to_ktime(realtime_offset);
590 hrtimer_force_reprogram(base);
591 spin_unlock(&base->lock);
595 * Clock realtime was set
597 * Change the offset of the realtime clock vs. the monotonic
598 * clock.
600 * We might have to reprogram the high resolution timer interrupt. On
601 * SMP we call the architecture specific code to retrigger _all_ high
602 * resolution timer interrupts. On UP we just disable interrupts and
603 * call the high resolution interrupt code.
605 void clock_was_set(void)
607 /* Retrigger the CPU local events everywhere */
608 on_each_cpu(retrigger_next_event, NULL, 1);
612 * During resume we might have to reprogram the high resolution timer
613 * interrupt (on the local CPU):
615 void hres_timers_resume(void)
617 /* Retrigger the CPU local events: */
618 retrigger_next_event(NULL);
622 * Initialize the high resolution related parts of cpu_base
624 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
626 base->expires_next.tv64 = KTIME_MAX;
627 base->hres_active = 0;
631 * Initialize the high resolution related parts of a hrtimer
633 static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
639 * When High resolution timers are active, try to reprogram. Note, that in case
640 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
641 * check happens. The timer gets enqueued into the rbtree. The reprogramming
642 * and expiry check is done in the hrtimer_interrupt or in the softirq.
644 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
645 struct hrtimer_clock_base *base)
647 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
648 spin_unlock(&base->cpu_base->lock);
649 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
650 spin_lock(&base->cpu_base->lock);
651 return 1;
653 return 0;
657 * Switch to high resolution mode
659 static int hrtimer_switch_to_hres(void)
661 int cpu = smp_processor_id();
662 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
663 unsigned long flags;
665 if (base->hres_active)
666 return 1;
668 local_irq_save(flags);
670 if (tick_init_highres()) {
671 local_irq_restore(flags);
672 printk(KERN_WARNING "Could not switch to high resolution "
673 "mode on CPU %d\n", cpu);
674 return 0;
676 base->hres_active = 1;
677 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
678 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
680 tick_setup_sched_timer();
682 /* "Retrigger" the interrupt to get things going */
683 retrigger_next_event(NULL);
684 local_irq_restore(flags);
685 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
686 smp_processor_id());
687 return 1;
690 #else
692 static inline int hrtimer_hres_active(void) { return 0; }
693 static inline int hrtimer_is_hres_enabled(void) { return 0; }
694 static inline int hrtimer_switch_to_hres(void) { return 0; }
695 static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
696 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
697 struct hrtimer_clock_base *base)
699 return 0;
701 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
702 static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
704 #endif /* CONFIG_HIGH_RES_TIMERS */
706 #ifdef CONFIG_TIMER_STATS
707 void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
709 if (timer->start_site)
710 return;
712 timer->start_site = addr;
713 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
714 timer->start_pid = current->pid;
716 #endif
719 * Counterpart to lock_hrtimer_base above:
721 static inline
722 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
724 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
728 * hrtimer_forward - forward the timer expiry
729 * @timer: hrtimer to forward
730 * @now: forward past this time
731 * @interval: the interval to forward
733 * Forward the timer expiry so it will expire in the future.
734 * Returns the number of overruns.
736 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
738 u64 orun = 1;
739 ktime_t delta;
741 delta = ktime_sub(now, hrtimer_get_expires(timer));
743 if (delta.tv64 < 0)
744 return 0;
746 if (interval.tv64 < timer->base->resolution.tv64)
747 interval.tv64 = timer->base->resolution.tv64;
749 if (unlikely(delta.tv64 >= interval.tv64)) {
750 s64 incr = ktime_to_ns(interval);
752 orun = ktime_divns(delta, incr);
753 hrtimer_add_expires_ns(timer, incr * orun);
754 if (hrtimer_get_expires_tv64(timer) > now.tv64)
755 return orun;
757 * This (and the ktime_add() below) is the
758 * correction for exact:
760 orun++;
762 hrtimer_add_expires(timer, interval);
764 return orun;
766 EXPORT_SYMBOL_GPL(hrtimer_forward);
769 * enqueue_hrtimer - internal function to (re)start a timer
771 * The timer is inserted in expiry order. Insertion into the
772 * red black tree is O(log(n)). Must hold the base lock.
774 * Returns 1 when the new timer is the leftmost timer in the tree.
776 static int enqueue_hrtimer(struct hrtimer *timer,
777 struct hrtimer_clock_base *base)
779 struct rb_node **link = &base->active.rb_node;
780 struct rb_node *parent = NULL;
781 struct hrtimer *entry;
782 int leftmost = 1;
784 debug_hrtimer_activate(timer);
787 * Find the right place in the rbtree:
789 while (*link) {
790 parent = *link;
791 entry = rb_entry(parent, struct hrtimer, node);
793 * We dont care about collisions. Nodes with
794 * the same expiry time stay together.
796 if (hrtimer_get_expires_tv64(timer) <
797 hrtimer_get_expires_tv64(entry)) {
798 link = &(*link)->rb_left;
799 } else {
800 link = &(*link)->rb_right;
801 leftmost = 0;
806 * Insert the timer to the rbtree and check whether it
807 * replaces the first pending timer
809 if (leftmost)
810 base->first = &timer->node;
812 rb_link_node(&timer->node, parent, link);
813 rb_insert_color(&timer->node, &base->active);
815 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
816 * state of a possibly running callback.
818 timer->state |= HRTIMER_STATE_ENQUEUED;
820 return leftmost;
824 * __remove_hrtimer - internal function to remove a timer
826 * Caller must hold the base lock.
828 * High resolution timer mode reprograms the clock event device when the
829 * timer is the one which expires next. The caller can disable this by setting
830 * reprogram to zero. This is useful, when the context does a reprogramming
831 * anyway (e.g. timer interrupt)
833 static void __remove_hrtimer(struct hrtimer *timer,
834 struct hrtimer_clock_base *base,
835 unsigned long newstate, int reprogram)
837 if (timer->state & HRTIMER_STATE_ENQUEUED) {
839 * Remove the timer from the rbtree and replace the
840 * first entry pointer if necessary.
842 if (base->first == &timer->node) {
843 base->first = rb_next(&timer->node);
844 /* Reprogram the clock event device. if enabled */
845 if (reprogram && hrtimer_hres_active())
846 hrtimer_force_reprogram(base->cpu_base);
848 rb_erase(&timer->node, &base->active);
850 timer->state = newstate;
854 * remove hrtimer, called with base lock held
856 static inline int
857 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
859 if (hrtimer_is_queued(timer)) {
860 int reprogram;
863 * Remove the timer and force reprogramming when high
864 * resolution mode is active and the timer is on the current
865 * CPU. If we remove a timer on another CPU, reprogramming is
866 * skipped. The interrupt event on this CPU is fired and
867 * reprogramming happens in the interrupt handler. This is a
868 * rare case and less expensive than a smp call.
870 debug_hrtimer_deactivate(timer);
871 timer_stats_hrtimer_clear_start_info(timer);
872 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
873 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
874 reprogram);
875 return 1;
877 return 0;
881 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
882 * @timer: the timer to be added
883 * @tim: expiry time
884 * @delta_ns: "slack" range for the timer
885 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
887 * Returns:
888 * 0 on success
889 * 1 when the timer was active
892 hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
893 const enum hrtimer_mode mode)
895 struct hrtimer_clock_base *base, *new_base;
896 unsigned long flags;
897 int ret, leftmost;
899 base = lock_hrtimer_base(timer, &flags);
901 /* Remove an active timer from the queue: */
902 ret = remove_hrtimer(timer, base);
904 /* Switch the timer base, if necessary: */
905 new_base = switch_hrtimer_base(timer, base);
907 if (mode == HRTIMER_MODE_REL) {
908 tim = ktime_add_safe(tim, new_base->get_time());
910 * CONFIG_TIME_LOW_RES is a temporary way for architectures
911 * to signal that they simply return xtime in
912 * do_gettimeoffset(). In this case we want to round up by
913 * resolution when starting a relative timer, to avoid short
914 * timeouts. This will go away with the GTOD framework.
916 #ifdef CONFIG_TIME_LOW_RES
917 tim = ktime_add_safe(tim, base->resolution);
918 #endif
921 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
923 timer_stats_hrtimer_set_start_info(timer);
925 leftmost = enqueue_hrtimer(timer, new_base);
928 * Only allow reprogramming if the new base is on this CPU.
929 * (it might still be on another CPU if the timer was pending)
931 * XXX send_remote_softirq() ?
933 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
934 hrtimer_enqueue_reprogram(timer, new_base);
936 unlock_hrtimer_base(timer, &flags);
938 return ret;
940 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
943 * hrtimer_start - (re)start an hrtimer on the current CPU
944 * @timer: the timer to be added
945 * @tim: expiry time
946 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
948 * Returns:
949 * 0 on success
950 * 1 when the timer was active
953 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
955 return hrtimer_start_range_ns(timer, tim, 0, mode);
957 EXPORT_SYMBOL_GPL(hrtimer_start);
961 * hrtimer_try_to_cancel - try to deactivate a timer
962 * @timer: hrtimer to stop
964 * Returns:
965 * 0 when the timer was not active
966 * 1 when the timer was active
967 * -1 when the timer is currently excuting the callback function and
968 * cannot be stopped
970 int hrtimer_try_to_cancel(struct hrtimer *timer)
972 struct hrtimer_clock_base *base;
973 unsigned long flags;
974 int ret = -1;
976 base = lock_hrtimer_base(timer, &flags);
978 if (!hrtimer_callback_running(timer))
979 ret = remove_hrtimer(timer, base);
981 unlock_hrtimer_base(timer, &flags);
983 return ret;
986 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
989 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
990 * @timer: the timer to be cancelled
992 * Returns:
993 * 0 when the timer was not active
994 * 1 when the timer was active
996 int hrtimer_cancel(struct hrtimer *timer)
998 for (;;) {
999 int ret = hrtimer_try_to_cancel(timer);
1001 if (ret >= 0)
1002 return ret;
1003 cpu_relax();
1006 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1009 * hrtimer_get_remaining - get remaining time for the timer
1010 * @timer: the timer to read
1012 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1014 struct hrtimer_clock_base *base;
1015 unsigned long flags;
1016 ktime_t rem;
1018 base = lock_hrtimer_base(timer, &flags);
1019 rem = hrtimer_expires_remaining(timer);
1020 unlock_hrtimer_base(timer, &flags);
1022 return rem;
1024 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1026 #ifdef CONFIG_NO_HZ
1028 * hrtimer_get_next_event - get the time until next expiry event
1030 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1031 * is pending.
1033 ktime_t hrtimer_get_next_event(void)
1035 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1036 struct hrtimer_clock_base *base = cpu_base->clock_base;
1037 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1038 unsigned long flags;
1039 int i;
1041 spin_lock_irqsave(&cpu_base->lock, flags);
1043 if (!hrtimer_hres_active()) {
1044 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1045 struct hrtimer *timer;
1047 if (!base->first)
1048 continue;
1050 timer = rb_entry(base->first, struct hrtimer, node);
1051 delta.tv64 = hrtimer_get_expires_tv64(timer);
1052 delta = ktime_sub(delta, base->get_time());
1053 if (delta.tv64 < mindelta.tv64)
1054 mindelta.tv64 = delta.tv64;
1058 spin_unlock_irqrestore(&cpu_base->lock, flags);
1060 if (mindelta.tv64 < 0)
1061 mindelta.tv64 = 0;
1062 return mindelta;
1064 #endif
1066 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1067 enum hrtimer_mode mode)
1069 struct hrtimer_cpu_base *cpu_base;
1071 memset(timer, 0, sizeof(struct hrtimer));
1073 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1075 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1076 clock_id = CLOCK_MONOTONIC;
1078 timer->base = &cpu_base->clock_base[clock_id];
1079 INIT_LIST_HEAD(&timer->cb_entry);
1080 hrtimer_init_timer_hres(timer);
1082 #ifdef CONFIG_TIMER_STATS
1083 timer->start_site = NULL;
1084 timer->start_pid = -1;
1085 memset(timer->start_comm, 0, TASK_COMM_LEN);
1086 #endif
1090 * hrtimer_init - initialize a timer to the given clock
1091 * @timer: the timer to be initialized
1092 * @clock_id: the clock to be used
1093 * @mode: timer mode abs/rel
1095 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1096 enum hrtimer_mode mode)
1098 debug_hrtimer_init(timer);
1099 __hrtimer_init(timer, clock_id, mode);
1101 EXPORT_SYMBOL_GPL(hrtimer_init);
1104 * hrtimer_get_res - get the timer resolution for a clock
1105 * @which_clock: which clock to query
1106 * @tp: pointer to timespec variable to store the resolution
1108 * Store the resolution of the clock selected by @which_clock in the
1109 * variable pointed to by @tp.
1111 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1113 struct hrtimer_cpu_base *cpu_base;
1115 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1116 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
1118 return 0;
1120 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1122 static void __run_hrtimer(struct hrtimer *timer)
1124 struct hrtimer_clock_base *base = timer->base;
1125 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1126 enum hrtimer_restart (*fn)(struct hrtimer *);
1127 int restart;
1129 WARN_ON(!irqs_disabled());
1131 debug_hrtimer_deactivate(timer);
1132 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1133 timer_stats_account_hrtimer(timer);
1134 fn = timer->function;
1137 * Because we run timers from hardirq context, there is no chance
1138 * they get migrated to another cpu, therefore its safe to unlock
1139 * the timer base.
1141 spin_unlock(&cpu_base->lock);
1142 restart = fn(timer);
1143 spin_lock(&cpu_base->lock);
1146 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1147 * we do not reprogramm the event hardware. Happens either in
1148 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1150 if (restart != HRTIMER_NORESTART) {
1151 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1152 enqueue_hrtimer(timer, base);
1154 timer->state &= ~HRTIMER_STATE_CALLBACK;
1157 #ifdef CONFIG_HIGH_RES_TIMERS
1159 static int force_clock_reprogram;
1162 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1163 * is hanging, which could happen with something that slows the interrupt
1164 * such as the tracing. Then we force the clock reprogramming for each future
1165 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1166 * threshold that we will overwrite.
1167 * The next tick event will be scheduled to 3 times we currently spend on
1168 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1169 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1170 * let it running without serious starvation.
1173 static inline void
1174 hrtimer_interrupt_hanging(struct clock_event_device *dev,
1175 ktime_t try_time)
1177 force_clock_reprogram = 1;
1178 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1179 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1180 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1183 * High resolution timer interrupt
1184 * Called with interrupts disabled
1186 void hrtimer_interrupt(struct clock_event_device *dev)
1188 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1189 struct hrtimer_clock_base *base;
1190 ktime_t expires_next, now;
1191 int nr_retries = 0;
1192 int i;
1194 BUG_ON(!cpu_base->hres_active);
1195 cpu_base->nr_events++;
1196 dev->next_event.tv64 = KTIME_MAX;
1198 retry:
1199 /* 5 retries is enough to notice a hang */
1200 if (!(++nr_retries % 5))
1201 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1203 now = ktime_get();
1205 expires_next.tv64 = KTIME_MAX;
1207 base = cpu_base->clock_base;
1209 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1210 ktime_t basenow;
1211 struct rb_node *node;
1213 spin_lock(&cpu_base->lock);
1215 basenow = ktime_add(now, base->offset);
1217 while ((node = base->first)) {
1218 struct hrtimer *timer;
1220 timer = rb_entry(node, struct hrtimer, node);
1223 * The immediate goal for using the softexpires is
1224 * minimizing wakeups, not running timers at the
1225 * earliest interrupt after their soft expiration.
1226 * This allows us to avoid using a Priority Search
1227 * Tree, which can answer a stabbing querry for
1228 * overlapping intervals and instead use the simple
1229 * BST we already have.
1230 * We don't add extra wakeups by delaying timers that
1231 * are right-of a not yet expired timer, because that
1232 * timer will have to trigger a wakeup anyway.
1235 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
1236 ktime_t expires;
1238 expires = ktime_sub(hrtimer_get_expires(timer),
1239 base->offset);
1240 if (expires.tv64 < expires_next.tv64)
1241 expires_next = expires;
1242 break;
1245 __run_hrtimer(timer);
1247 spin_unlock(&cpu_base->lock);
1248 base++;
1251 cpu_base->expires_next = expires_next;
1253 /* Reprogramming necessary ? */
1254 if (expires_next.tv64 != KTIME_MAX) {
1255 if (tick_program_event(expires_next, force_clock_reprogram))
1256 goto retry;
1261 * local version of hrtimer_peek_ahead_timers() called with interrupts
1262 * disabled.
1264 static void __hrtimer_peek_ahead_timers(void)
1266 struct tick_device *td;
1268 if (!hrtimer_hres_active())
1269 return;
1271 td = &__get_cpu_var(tick_cpu_device);
1272 if (td && td->evtdev)
1273 hrtimer_interrupt(td->evtdev);
1277 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1279 * hrtimer_peek_ahead_timers will peek at the timer queue of
1280 * the current cpu and check if there are any timers for which
1281 * the soft expires time has passed. If any such timers exist,
1282 * they are run immediately and then removed from the timer queue.
1285 void hrtimer_peek_ahead_timers(void)
1287 unsigned long flags;
1289 local_irq_save(flags);
1290 __hrtimer_peek_ahead_timers();
1291 local_irq_restore(flags);
1294 static void run_hrtimer_softirq(struct softirq_action *h)
1296 hrtimer_peek_ahead_timers();
1299 #else /* CONFIG_HIGH_RES_TIMERS */
1301 static inline void __hrtimer_peek_ahead_timers(void) { }
1303 #endif /* !CONFIG_HIGH_RES_TIMERS */
1306 * Called from timer softirq every jiffy, expire hrtimers:
1308 * For HRT its the fall back code to run the softirq in the timer
1309 * softirq context in case the hrtimer initialization failed or has
1310 * not been done yet.
1312 void hrtimer_run_pending(void)
1314 if (hrtimer_hres_active())
1315 return;
1318 * This _is_ ugly: We have to check in the softirq context,
1319 * whether we can switch to highres and / or nohz mode. The
1320 * clocksource switch happens in the timer interrupt with
1321 * xtime_lock held. Notification from there only sets the
1322 * check bit in the tick_oneshot code, otherwise we might
1323 * deadlock vs. xtime_lock.
1325 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1326 hrtimer_switch_to_hres();
1330 * Called from hardirq context every jiffy
1332 void hrtimer_run_queues(void)
1334 struct rb_node *node;
1335 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1336 struct hrtimer_clock_base *base;
1337 int index, gettime = 1;
1339 if (hrtimer_hres_active())
1340 return;
1342 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1343 base = &cpu_base->clock_base[index];
1345 if (!base->first)
1346 continue;
1348 if (gettime) {
1349 hrtimer_get_softirq_time(cpu_base);
1350 gettime = 0;
1353 spin_lock(&cpu_base->lock);
1355 while ((node = base->first)) {
1356 struct hrtimer *timer;
1358 timer = rb_entry(node, struct hrtimer, node);
1359 if (base->softirq_time.tv64 <=
1360 hrtimer_get_expires_tv64(timer))
1361 break;
1363 __run_hrtimer(timer);
1365 spin_unlock(&cpu_base->lock);
1370 * Sleep related functions:
1372 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1374 struct hrtimer_sleeper *t =
1375 container_of(timer, struct hrtimer_sleeper, timer);
1376 struct task_struct *task = t->task;
1378 t->task = NULL;
1379 if (task)
1380 wake_up_process(task);
1382 return HRTIMER_NORESTART;
1385 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1387 sl->timer.function = hrtimer_wakeup;
1388 sl->task = task;
1391 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1393 hrtimer_init_sleeper(t, current);
1395 do {
1396 set_current_state(TASK_INTERRUPTIBLE);
1397 hrtimer_start_expires(&t->timer, mode);
1398 if (!hrtimer_active(&t->timer))
1399 t->task = NULL;
1401 if (likely(t->task))
1402 schedule();
1404 hrtimer_cancel(&t->timer);
1405 mode = HRTIMER_MODE_ABS;
1407 } while (t->task && !signal_pending(current));
1409 __set_current_state(TASK_RUNNING);
1411 return t->task == NULL;
1414 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1416 struct timespec rmt;
1417 ktime_t rem;
1419 rem = hrtimer_expires_remaining(timer);
1420 if (rem.tv64 <= 0)
1421 return 0;
1422 rmt = ktime_to_timespec(rem);
1424 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1425 return -EFAULT;
1427 return 1;
1430 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1432 struct hrtimer_sleeper t;
1433 struct timespec __user *rmtp;
1434 int ret = 0;
1436 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1437 HRTIMER_MODE_ABS);
1438 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1440 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1441 goto out;
1443 rmtp = restart->nanosleep.rmtp;
1444 if (rmtp) {
1445 ret = update_rmtp(&t.timer, rmtp);
1446 if (ret <= 0)
1447 goto out;
1450 /* The other values in restart are already filled in */
1451 ret = -ERESTART_RESTARTBLOCK;
1452 out:
1453 destroy_hrtimer_on_stack(&t.timer);
1454 return ret;
1457 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1458 const enum hrtimer_mode mode, const clockid_t clockid)
1460 struct restart_block *restart;
1461 struct hrtimer_sleeper t;
1462 int ret = 0;
1463 unsigned long slack;
1465 slack = current->timer_slack_ns;
1466 if (rt_task(current))
1467 slack = 0;
1469 hrtimer_init_on_stack(&t.timer, clockid, mode);
1470 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1471 if (do_nanosleep(&t, mode))
1472 goto out;
1474 /* Absolute timers do not update the rmtp value and restart: */
1475 if (mode == HRTIMER_MODE_ABS) {
1476 ret = -ERESTARTNOHAND;
1477 goto out;
1480 if (rmtp) {
1481 ret = update_rmtp(&t.timer, rmtp);
1482 if (ret <= 0)
1483 goto out;
1486 restart = &current_thread_info()->restart_block;
1487 restart->fn = hrtimer_nanosleep_restart;
1488 restart->nanosleep.index = t.timer.base->index;
1489 restart->nanosleep.rmtp = rmtp;
1490 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1492 ret = -ERESTART_RESTARTBLOCK;
1493 out:
1494 destroy_hrtimer_on_stack(&t.timer);
1495 return ret;
1498 SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1499 struct timespec __user *, rmtp)
1501 struct timespec tu;
1503 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1504 return -EFAULT;
1506 if (!timespec_valid(&tu))
1507 return -EINVAL;
1509 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1513 * Functions related to boot-time initialization:
1515 static void __cpuinit init_hrtimers_cpu(int cpu)
1517 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1518 int i;
1520 spin_lock_init(&cpu_base->lock);
1522 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1523 cpu_base->clock_base[i].cpu_base = cpu_base;
1525 hrtimer_init_hres(cpu_base);
1528 #ifdef CONFIG_HOTPLUG_CPU
1530 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1531 struct hrtimer_clock_base *new_base)
1533 struct hrtimer *timer;
1534 struct rb_node *node;
1536 while ((node = rb_first(&old_base->active))) {
1537 timer = rb_entry(node, struct hrtimer, node);
1538 BUG_ON(hrtimer_callback_running(timer));
1539 debug_hrtimer_deactivate(timer);
1542 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1543 * timer could be seen as !active and just vanish away
1544 * under us on another CPU
1546 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
1547 timer->base = new_base;
1549 * Enqueue the timers on the new cpu. This does not
1550 * reprogram the event device in case the timer
1551 * expires before the earliest on this CPU, but we run
1552 * hrtimer_interrupt after we migrated everything to
1553 * sort out already expired timers and reprogram the
1554 * event device.
1556 enqueue_hrtimer(timer, new_base);
1558 /* Clear the migration state bit */
1559 timer->state &= ~HRTIMER_STATE_MIGRATE;
1563 static void migrate_hrtimers(int scpu)
1565 struct hrtimer_cpu_base *old_base, *new_base;
1566 int i;
1568 BUG_ON(cpu_online(scpu));
1569 tick_cancel_sched_timer(scpu);
1571 local_irq_disable();
1572 old_base = &per_cpu(hrtimer_bases, scpu);
1573 new_base = &__get_cpu_var(hrtimer_bases);
1575 * The caller is globally serialized and nobody else
1576 * takes two locks at once, deadlock is not possible.
1578 spin_lock(&new_base->lock);
1579 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1581 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1582 migrate_hrtimer_list(&old_base->clock_base[i],
1583 &new_base->clock_base[i]);
1586 spin_unlock(&old_base->lock);
1587 spin_unlock(&new_base->lock);
1589 /* Check, if we got expired work to do */
1590 __hrtimer_peek_ahead_timers();
1591 local_irq_enable();
1594 #endif /* CONFIG_HOTPLUG_CPU */
1596 static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
1597 unsigned long action, void *hcpu)
1599 int scpu = (long)hcpu;
1601 switch (action) {
1603 case CPU_UP_PREPARE:
1604 case CPU_UP_PREPARE_FROZEN:
1605 init_hrtimers_cpu(scpu);
1606 break;
1608 #ifdef CONFIG_HOTPLUG_CPU
1609 case CPU_DYING:
1610 case CPU_DYING_FROZEN:
1611 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1612 break;
1613 case CPU_DEAD:
1614 case CPU_DEAD_FROZEN:
1616 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1617 migrate_hrtimers(scpu);
1618 break;
1620 #endif
1622 default:
1623 break;
1626 return NOTIFY_OK;
1629 static struct notifier_block __cpuinitdata hrtimers_nb = {
1630 .notifier_call = hrtimer_cpu_notify,
1633 void __init hrtimers_init(void)
1635 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1636 (void *)(long)smp_processor_id());
1637 register_cpu_notifier(&hrtimers_nb);
1638 #ifdef CONFIG_HIGH_RES_TIMERS
1639 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1640 #endif
1644 * schedule_hrtimeout_range - sleep until timeout
1645 * @expires: timeout value (ktime_t)
1646 * @delta: slack in expires timeout (ktime_t)
1647 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1649 * Make the current task sleep until the given expiry time has
1650 * elapsed. The routine will return immediately unless
1651 * the current task state has been set (see set_current_state()).
1653 * The @delta argument gives the kernel the freedom to schedule the
1654 * actual wakeup to a time that is both power and performance friendly.
1655 * The kernel give the normal best effort behavior for "@expires+@delta",
1656 * but may decide to fire the timer earlier, but no earlier than @expires.
1658 * You can set the task state as follows -
1660 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1661 * pass before the routine returns.
1663 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1664 * delivered to the current task.
1666 * The current task state is guaranteed to be TASK_RUNNING when this
1667 * routine returns.
1669 * Returns 0 when the timer has expired otherwise -EINTR
1671 int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1672 const enum hrtimer_mode mode)
1674 struct hrtimer_sleeper t;
1677 * Optimize when a zero timeout value is given. It does not
1678 * matter whether this is an absolute or a relative time.
1680 if (expires && !expires->tv64) {
1681 __set_current_state(TASK_RUNNING);
1682 return 0;
1686 * A NULL parameter means "inifinte"
1688 if (!expires) {
1689 schedule();
1690 __set_current_state(TASK_RUNNING);
1691 return -EINTR;
1694 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1695 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1697 hrtimer_init_sleeper(&t, current);
1699 hrtimer_start_expires(&t.timer, mode);
1700 if (!hrtimer_active(&t.timer))
1701 t.task = NULL;
1703 if (likely(t.task))
1704 schedule();
1706 hrtimer_cancel(&t.timer);
1707 destroy_hrtimer_on_stack(&t.timer);
1709 __set_current_state(TASK_RUNNING);
1711 return !t.task ? 0 : -EINTR;
1713 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1716 * schedule_hrtimeout - sleep until timeout
1717 * @expires: timeout value (ktime_t)
1718 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1720 * Make the current task sleep until the given expiry time has
1721 * elapsed. The routine will return immediately unless
1722 * the current task state has been set (see set_current_state()).
1724 * You can set the task state as follows -
1726 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1727 * pass before the routine returns.
1729 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1730 * delivered to the current task.
1732 * The current task state is guaranteed to be TASK_RUNNING when this
1733 * routine returns.
1735 * Returns 0 when the timer has expired otherwise -EINTR
1737 int __sched schedule_hrtimeout(ktime_t *expires,
1738 const enum hrtimer_mode mode)
1740 return schedule_hrtimeout_range(expires, 0, mode);
1742 EXPORT_SYMBOL_GPL(schedule_hrtimeout);