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