2 * linux/kernel/hrtimer.c
4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
8 * High-resolution kernel timers
10 * In contrast to the low-resolution timeout API implemented in
11 * kernel/timer.c, hrtimers provide finer resolution and accuracy
12 * depending on system configuration and capabilities.
14 * These timers are currently used for:
18 * - precise in-kernel timing
20 * Started by: Thomas Gleixner and Ingo Molnar
23 * based on kernel/timer.c
25 * Help, testing, suggestions, bugfixes, improvements were
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
31 * For licencing details see kernel-base/COPYING
34 #include <linux/cpu.h>
35 #include <linux/module.h>
36 #include <linux/percpu.h>
37 #include <linux/hrtimer.h>
38 #include <linux/notifier.h>
39 #include <linux/syscalls.h>
40 #include <linux/kallsyms.h>
41 #include <linux/interrupt.h>
42 #include <linux/tick.h>
43 #include <linux/seq_file.h>
44 #include <linux/err.h>
45 #include <linux/debugobjects.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
49 #include <asm/uaccess.h>
51 #include <trace/events/timer.h>
56 * Note: If we want to add new timer bases, we have to skip the two
57 * clock ids captured by the cpu-timers. We do this by holding empty
58 * entries rather than doing math adjustment of the clock ids.
59 * This ensures that we capture erroneous accesses to these clock ids
60 * rather than moving them into the range of valid clock id's.
62 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
68 .index = CLOCK_REALTIME,
69 .get_time = &ktime_get_real,
70 .resolution = KTIME_LOW_RES,
73 .index = CLOCK_MONOTONIC,
74 .get_time = &ktime_get,
75 .resolution = KTIME_LOW_RES,
81 * Get the coarse grained time at the softirq based on xtime and
84 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
87 struct timespec xts, tom;
89 get_xtime_and_monotonic_offset(&xts, &tom);
91 xtim = timespec_to_ktime(xts);
92 tomono = timespec_to_ktime(tom);
93 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
94 base->clock_base[CLOCK_MONOTONIC].softirq_time =
95 ktime_add(xtim, tomono);
99 * Functions and macros which are different for UP/SMP systems are kept in a
105 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
106 * means that all timers which are tied to this base via timer->base are
107 * locked, and the base itself is locked too.
109 * So __run_timers/migrate_timers can safely modify all timers which could
110 * be found on the lists/queues.
112 * When the timer's base is locked, and the timer removed from list, it is
113 * possible to set timer->base = NULL and drop the lock: the timer remains
117 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
118 unsigned long *flags)
120 struct hrtimer_clock_base *base;
124 if (likely(base != NULL)) {
125 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
126 if (likely(base == timer->base))
128 /* The timer has migrated to another CPU: */
129 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
137 * Get the preferred target CPU for NOHZ
139 static int hrtimer_get_target(int this_cpu, int pinned)
142 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
143 return get_nohz_timer_target();
149 * With HIGHRES=y we do not migrate the timer when it is expiring
150 * before the next event on the target cpu because we cannot reprogram
151 * the target cpu hardware and we would cause it to fire late.
153 * Called with cpu_base->lock of target cpu held.
156 hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
158 #ifdef CONFIG_HIGH_RES_TIMERS
161 if (!new_base->cpu_base->hres_active)
164 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
165 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
172 * Switch the timer base to the current CPU when possible.
174 static inline struct hrtimer_clock_base *
175 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
178 struct hrtimer_clock_base *new_base;
179 struct hrtimer_cpu_base *new_cpu_base;
180 int this_cpu = smp_processor_id();
181 int cpu = hrtimer_get_target(this_cpu, pinned);
184 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
185 new_base = &new_cpu_base->clock_base[base->index];
187 if (base != new_base) {
189 * We are trying to move timer to new_base.
190 * However we can't change timer's base while it is running,
191 * so we keep it on the same CPU. No hassle vs. reprogramming
192 * the event source in the high resolution case. The softirq
193 * code will take care of this when the timer function has
194 * completed. There is no conflict as we hold the lock until
195 * the timer is enqueued.
197 if (unlikely(hrtimer_callback_running(timer)))
200 /* See the comment in lock_timer_base() */
202 raw_spin_unlock(&base->cpu_base->lock);
203 raw_spin_lock(&new_base->cpu_base->lock);
205 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
207 raw_spin_unlock(&new_base->cpu_base->lock);
208 raw_spin_lock(&base->cpu_base->lock);
212 timer->base = new_base;
217 #else /* CONFIG_SMP */
219 static inline struct hrtimer_clock_base *
220 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
222 struct hrtimer_clock_base *base = timer->base;
224 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
229 # define switch_hrtimer_base(t, b, p) (b)
231 #endif /* !CONFIG_SMP */
234 * Functions for the union type storage format of ktime_t which are
235 * too large for inlining:
237 #if BITS_PER_LONG < 64
238 # ifndef CONFIG_KTIME_SCALAR
240 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
242 * @nsec: the scalar nsec value to add
244 * Returns the sum of kt and nsec in ktime_t format
246 ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
250 if (likely(nsec < NSEC_PER_SEC)) {
253 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
255 tmp = ktime_set((long)nsec, rem);
258 return ktime_add(kt, tmp);
261 EXPORT_SYMBOL_GPL(ktime_add_ns);
264 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
266 * @nsec: the scalar nsec value to subtract
268 * Returns the subtraction of @nsec from @kt in ktime_t format
270 ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
274 if (likely(nsec < NSEC_PER_SEC)) {
277 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
279 tmp = ktime_set((long)nsec, rem);
282 return ktime_sub(kt, tmp);
285 EXPORT_SYMBOL_GPL(ktime_sub_ns);
286 # endif /* !CONFIG_KTIME_SCALAR */
289 * Divide a ktime value by a nanosecond value
291 u64 ktime_divns(const ktime_t kt, s64 div)
296 dclc = ktime_to_ns(kt);
297 /* Make sure the divisor is less than 2^32: */
303 do_div(dclc, (unsigned long) div);
307 #endif /* BITS_PER_LONG >= 64 */
310 * Add two ktime values and do a safety check for overflow:
312 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
314 ktime_t res = ktime_add(lhs, rhs);
317 * We use KTIME_SEC_MAX here, the maximum timeout which we can
318 * return to user space in a timespec:
320 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
321 res = ktime_set(KTIME_SEC_MAX, 0);
326 EXPORT_SYMBOL_GPL(ktime_add_safe);
328 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
330 static struct debug_obj_descr hrtimer_debug_descr;
333 * fixup_init is called when:
334 * - an active object is initialized
336 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
338 struct hrtimer *timer = addr;
341 case ODEBUG_STATE_ACTIVE:
342 hrtimer_cancel(timer);
343 debug_object_init(timer, &hrtimer_debug_descr);
351 * fixup_activate is called when:
352 * - an active object is activated
353 * - an unknown object is activated (might be a statically initialized object)
355 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
359 case ODEBUG_STATE_NOTAVAILABLE:
363 case ODEBUG_STATE_ACTIVE:
372 * fixup_free is called when:
373 * - an active object is freed
375 static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
377 struct hrtimer *timer = addr;
380 case ODEBUG_STATE_ACTIVE:
381 hrtimer_cancel(timer);
382 debug_object_free(timer, &hrtimer_debug_descr);
389 static struct debug_obj_descr hrtimer_debug_descr = {
391 .fixup_init = hrtimer_fixup_init,
392 .fixup_activate = hrtimer_fixup_activate,
393 .fixup_free = hrtimer_fixup_free,
396 static inline void debug_hrtimer_init(struct hrtimer *timer)
398 debug_object_init(timer, &hrtimer_debug_descr);
401 static inline void debug_hrtimer_activate(struct hrtimer *timer)
403 debug_object_activate(timer, &hrtimer_debug_descr);
406 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
408 debug_object_deactivate(timer, &hrtimer_debug_descr);
411 static inline void debug_hrtimer_free(struct hrtimer *timer)
413 debug_object_free(timer, &hrtimer_debug_descr);
416 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
417 enum hrtimer_mode mode);
419 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
420 enum hrtimer_mode mode)
422 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
423 __hrtimer_init(timer, clock_id, mode);
425 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
427 void destroy_hrtimer_on_stack(struct hrtimer *timer)
429 debug_object_free(timer, &hrtimer_debug_descr);
433 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
434 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
435 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
439 debug_init(struct hrtimer *timer, clockid_t clockid,
440 enum hrtimer_mode mode)
442 debug_hrtimer_init(timer);
443 trace_hrtimer_init(timer, clockid, mode);
446 static inline void debug_activate(struct hrtimer *timer)
448 debug_hrtimer_activate(timer);
449 trace_hrtimer_start(timer);
452 static inline void debug_deactivate(struct hrtimer *timer)
454 debug_hrtimer_deactivate(timer);
455 trace_hrtimer_cancel(timer);
458 /* High resolution timer related functions */
459 #ifdef CONFIG_HIGH_RES_TIMERS
462 * High resolution timer enabled ?
464 static int hrtimer_hres_enabled __read_mostly = 1;
467 * Enable / Disable high resolution mode
469 static int __init setup_hrtimer_hres(char *str)
471 if (!strcmp(str, "off"))
472 hrtimer_hres_enabled = 0;
473 else if (!strcmp(str, "on"))
474 hrtimer_hres_enabled = 1;
480 __setup("highres=", setup_hrtimer_hres);
483 * hrtimer_high_res_enabled - query, if the highres mode is enabled
485 static inline int hrtimer_is_hres_enabled(void)
487 return hrtimer_hres_enabled;
491 * Is the high resolution mode active ?
493 static inline int hrtimer_hres_active(void)
495 return __this_cpu_read(hrtimer_bases.hres_active);
499 * Reprogram the event source with checking both queues for the
501 * Called with interrupts disabled and base->lock held
504 hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
507 struct hrtimer_clock_base *base = cpu_base->clock_base;
508 ktime_t expires, expires_next;
510 expires_next.tv64 = KTIME_MAX;
512 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
513 struct hrtimer *timer;
514 struct timerqueue_node *next;
516 next = timerqueue_getnext(&base->active);
519 timer = container_of(next, struct hrtimer, node);
521 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
523 * clock_was_set() has changed base->offset so the
524 * result might be negative. Fix it up to prevent a
525 * false positive in clockevents_program_event()
527 if (expires.tv64 < 0)
529 if (expires.tv64 < expires_next.tv64)
530 expires_next = expires;
533 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
536 cpu_base->expires_next.tv64 = expires_next.tv64;
538 if (cpu_base->expires_next.tv64 != KTIME_MAX)
539 tick_program_event(cpu_base->expires_next, 1);
543 * Shared reprogramming for clock_realtime and clock_monotonic
545 * When a timer is enqueued and expires earlier than the already enqueued
546 * timers, we have to check, whether it expires earlier than the timer for
547 * which the clock event device was armed.
549 * Called with interrupts disabled and base->cpu_base.lock held
551 static int hrtimer_reprogram(struct hrtimer *timer,
552 struct hrtimer_clock_base *base)
554 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
555 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
558 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
561 * When the callback is running, we do not reprogram the clock event
562 * device. The timer callback is either running on a different CPU or
563 * the callback is executed in the hrtimer_interrupt context. The
564 * reprogramming is handled either by the softirq, which called the
565 * callback or at the end of the hrtimer_interrupt.
567 if (hrtimer_callback_running(timer))
571 * CLOCK_REALTIME timer might be requested with an absolute
572 * expiry time which is less than base->offset. Nothing wrong
573 * about that, just avoid to call into the tick code, which
574 * has now objections against negative expiry values.
576 if (expires.tv64 < 0)
579 if (expires.tv64 >= cpu_base->expires_next.tv64)
583 * If a hang was detected in the last timer interrupt then we
584 * do not schedule a timer which is earlier than the expiry
585 * which we enforced in the hang detection. We want the system
588 if (cpu_base->hang_detected)
592 * Clockevents returns -ETIME, when the event was in the past.
594 res = tick_program_event(expires, 0);
595 if (!IS_ERR_VALUE(res))
596 cpu_base->expires_next = expires;
602 * Retrigger next event is called after clock was set
604 * Called with interrupts disabled via on_each_cpu()
606 static void retrigger_next_event(void *arg)
608 struct hrtimer_cpu_base *base;
609 struct timespec realtime_offset, wtm;
611 if (!hrtimer_hres_active())
614 get_xtime_and_monotonic_offset(&realtime_offset, &wtm);
615 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
617 base = &__get_cpu_var(hrtimer_bases);
619 /* Adjust CLOCK_REALTIME offset */
620 raw_spin_lock(&base->lock);
621 base->clock_base[CLOCK_REALTIME].offset =
622 timespec_to_ktime(realtime_offset);
624 hrtimer_force_reprogram(base, 0);
625 raw_spin_unlock(&base->lock);
629 * Clock realtime was set
631 * Change the offset of the realtime clock vs. the monotonic
634 * We might have to reprogram the high resolution timer interrupt. On
635 * SMP we call the architecture specific code to retrigger _all_ high
636 * resolution timer interrupts. On UP we just disable interrupts and
637 * call the high resolution interrupt code.
639 void clock_was_set(void)
641 /* Retrigger the CPU local events everywhere */
642 on_each_cpu(retrigger_next_event, NULL, 1);
646 * During resume we might have to reprogram the high resolution timer
647 * interrupt (on the local CPU):
649 void hres_timers_resume(void)
651 WARN_ONCE(!irqs_disabled(),
652 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
654 retrigger_next_event(NULL);
658 * Initialize the high resolution related parts of cpu_base
660 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
662 base->expires_next.tv64 = KTIME_MAX;
663 base->hres_active = 0;
667 * Initialize the high resolution related parts of a hrtimer
669 static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
675 * When High resolution timers are active, try to reprogram. Note, that in case
676 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
677 * check happens. The timer gets enqueued into the rbtree. The reprogramming
678 * and expiry check is done in the hrtimer_interrupt or in the softirq.
680 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
681 struct hrtimer_clock_base *base,
684 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
686 raw_spin_unlock(&base->cpu_base->lock);
687 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
688 raw_spin_lock(&base->cpu_base->lock);
690 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
699 * Switch to high resolution mode
701 static int hrtimer_switch_to_hres(void)
703 int cpu = smp_processor_id();
704 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
707 if (base->hres_active)
710 local_irq_save(flags);
712 if (tick_init_highres()) {
713 local_irq_restore(flags);
714 printk(KERN_WARNING "Could not switch to high resolution "
715 "mode on CPU %d\n", cpu);
718 base->hres_active = 1;
719 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
720 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
722 tick_setup_sched_timer();
724 /* "Retrigger" the interrupt to get things going */
725 retrigger_next_event(NULL);
726 local_irq_restore(flags);
732 static inline int hrtimer_hres_active(void) { return 0; }
733 static inline int hrtimer_is_hres_enabled(void) { return 0; }
734 static inline int hrtimer_switch_to_hres(void) { return 0; }
736 hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
737 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
738 struct hrtimer_clock_base *base,
743 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
744 static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
746 #endif /* CONFIG_HIGH_RES_TIMERS */
748 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
750 #ifdef CONFIG_TIMER_STATS
751 if (timer->start_site)
753 timer->start_site = __builtin_return_address(0);
754 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
755 timer->start_pid = current->pid;
759 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
761 #ifdef CONFIG_TIMER_STATS
762 timer->start_site = NULL;
766 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
768 #ifdef CONFIG_TIMER_STATS
769 if (likely(!timer_stats_active))
771 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
772 timer->function, timer->start_comm, 0);
777 * Counterpart to lock_hrtimer_base above:
780 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
782 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
786 * hrtimer_forward - forward the timer expiry
787 * @timer: hrtimer to forward
788 * @now: forward past this time
789 * @interval: the interval to forward
791 * Forward the timer expiry so it will expire in the future.
792 * Returns the number of overruns.
794 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
799 delta = ktime_sub(now, hrtimer_get_expires(timer));
804 if (interval.tv64 < timer->base->resolution.tv64)
805 interval.tv64 = timer->base->resolution.tv64;
807 if (unlikely(delta.tv64 >= interval.tv64)) {
808 s64 incr = ktime_to_ns(interval);
810 orun = ktime_divns(delta, incr);
811 hrtimer_add_expires_ns(timer, incr * orun);
812 if (hrtimer_get_expires_tv64(timer) > now.tv64)
815 * This (and the ktime_add() below) is the
816 * correction for exact:
820 hrtimer_add_expires(timer, interval);
824 EXPORT_SYMBOL_GPL(hrtimer_forward);
827 * enqueue_hrtimer - internal function to (re)start a timer
829 * The timer is inserted in expiry order. Insertion into the
830 * red black tree is O(log(n)). Must hold the base lock.
832 * Returns 1 when the new timer is the leftmost timer in the tree.
834 static int enqueue_hrtimer(struct hrtimer *timer,
835 struct hrtimer_clock_base *base)
837 debug_activate(timer);
839 timerqueue_add(&base->active, &timer->node);
842 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
843 * state of a possibly running callback.
845 timer->state |= HRTIMER_STATE_ENQUEUED;
847 return (&timer->node == base->active.next);
851 * __remove_hrtimer - internal function to remove a timer
853 * Caller must hold the base lock.
855 * High resolution timer mode reprograms the clock event device when the
856 * timer is the one which expires next. The caller can disable this by setting
857 * reprogram to zero. This is useful, when the context does a reprogramming
858 * anyway (e.g. timer interrupt)
860 static void __remove_hrtimer(struct hrtimer *timer,
861 struct hrtimer_clock_base *base,
862 unsigned long newstate, int reprogram)
864 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
867 if (&timer->node == timerqueue_getnext(&base->active)) {
868 #ifdef CONFIG_HIGH_RES_TIMERS
869 /* Reprogram the clock event device. if enabled */
870 if (reprogram && hrtimer_hres_active()) {
873 expires = ktime_sub(hrtimer_get_expires(timer),
875 if (base->cpu_base->expires_next.tv64 == expires.tv64)
876 hrtimer_force_reprogram(base->cpu_base, 1);
880 timerqueue_del(&base->active, &timer->node);
882 timer->state = newstate;
886 * remove hrtimer, called with base lock held
889 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
891 if (hrtimer_is_queued(timer)) {
896 * Remove the timer and force reprogramming when high
897 * resolution mode is active and the timer is on the current
898 * CPU. If we remove a timer on another CPU, reprogramming is
899 * skipped. The interrupt event on this CPU is fired and
900 * reprogramming happens in the interrupt handler. This is a
901 * rare case and less expensive than a smp call.
903 debug_deactivate(timer);
904 timer_stats_hrtimer_clear_start_info(timer);
905 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
907 * We must preserve the CALLBACK state flag here,
908 * otherwise we could move the timer base in
909 * switch_hrtimer_base.
911 state = timer->state & HRTIMER_STATE_CALLBACK;
912 __remove_hrtimer(timer, base, state, reprogram);
918 int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
919 unsigned long delta_ns, const enum hrtimer_mode mode,
922 struct hrtimer_clock_base *base, *new_base;
926 base = lock_hrtimer_base(timer, &flags);
928 /* Remove an active timer from the queue: */
929 ret = remove_hrtimer(timer, base);
931 /* Switch the timer base, if necessary: */
932 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
934 if (mode & HRTIMER_MODE_REL) {
935 tim = ktime_add_safe(tim, new_base->get_time());
937 * CONFIG_TIME_LOW_RES is a temporary way for architectures
938 * to signal that they simply return xtime in
939 * do_gettimeoffset(). In this case we want to round up by
940 * resolution when starting a relative timer, to avoid short
941 * timeouts. This will go away with the GTOD framework.
943 #ifdef CONFIG_TIME_LOW_RES
944 tim = ktime_add_safe(tim, base->resolution);
948 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
950 timer_stats_hrtimer_set_start_info(timer);
952 leftmost = enqueue_hrtimer(timer, new_base);
955 * Only allow reprogramming if the new base is on this CPU.
956 * (it might still be on another CPU if the timer was pending)
958 * XXX send_remote_softirq() ?
960 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
961 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
963 unlock_hrtimer_base(timer, &flags);
969 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
970 * @timer: the timer to be added
972 * @delta_ns: "slack" range for the timer
973 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
977 * 1 when the timer was active
979 int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
980 unsigned long delta_ns, const enum hrtimer_mode mode)
982 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
984 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
987 * hrtimer_start - (re)start an hrtimer on the current CPU
988 * @timer: the timer to be added
990 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
994 * 1 when the timer was active
997 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
999 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
1001 EXPORT_SYMBOL_GPL(hrtimer_start);
1005 * hrtimer_try_to_cancel - try to deactivate a timer
1006 * @timer: hrtimer to stop
1009 * 0 when the timer was not active
1010 * 1 when the timer was active
1011 * -1 when the timer is currently excuting the callback function and
1014 int hrtimer_try_to_cancel(struct hrtimer *timer)
1016 struct hrtimer_clock_base *base;
1017 unsigned long flags;
1020 base = lock_hrtimer_base(timer, &flags);
1022 if (!hrtimer_callback_running(timer))
1023 ret = remove_hrtimer(timer, base);
1025 unlock_hrtimer_base(timer, &flags);
1030 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1033 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1034 * @timer: the timer to be cancelled
1037 * 0 when the timer was not active
1038 * 1 when the timer was active
1040 int hrtimer_cancel(struct hrtimer *timer)
1043 int ret = hrtimer_try_to_cancel(timer);
1050 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1053 * hrtimer_get_remaining - get remaining time for the timer
1054 * @timer: the timer to read
1056 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1058 unsigned long flags;
1061 lock_hrtimer_base(timer, &flags);
1062 rem = hrtimer_expires_remaining(timer);
1063 unlock_hrtimer_base(timer, &flags);
1067 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1071 * hrtimer_get_next_event - get the time until next expiry event
1073 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1076 ktime_t hrtimer_get_next_event(void)
1078 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1079 struct hrtimer_clock_base *base = cpu_base->clock_base;
1080 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1081 unsigned long flags;
1084 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1086 if (!hrtimer_hres_active()) {
1087 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1088 struct hrtimer *timer;
1089 struct timerqueue_node *next;
1091 next = timerqueue_getnext(&base->active);
1095 timer = container_of(next, struct hrtimer, node);
1096 delta.tv64 = hrtimer_get_expires_tv64(timer);
1097 delta = ktime_sub(delta, base->get_time());
1098 if (delta.tv64 < mindelta.tv64)
1099 mindelta.tv64 = delta.tv64;
1103 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1105 if (mindelta.tv64 < 0)
1111 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1112 enum hrtimer_mode mode)
1114 struct hrtimer_cpu_base *cpu_base;
1116 memset(timer, 0, sizeof(struct hrtimer));
1118 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1120 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1121 clock_id = CLOCK_MONOTONIC;
1123 timer->base = &cpu_base->clock_base[clock_id];
1124 hrtimer_init_timer_hres(timer);
1125 timerqueue_init(&timer->node);
1127 #ifdef CONFIG_TIMER_STATS
1128 timer->start_site = NULL;
1129 timer->start_pid = -1;
1130 memset(timer->start_comm, 0, TASK_COMM_LEN);
1135 * hrtimer_init - initialize a timer to the given clock
1136 * @timer: the timer to be initialized
1137 * @clock_id: the clock to be used
1138 * @mode: timer mode abs/rel
1140 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1141 enum hrtimer_mode mode)
1143 debug_init(timer, clock_id, mode);
1144 __hrtimer_init(timer, clock_id, mode);
1146 EXPORT_SYMBOL_GPL(hrtimer_init);
1149 * hrtimer_get_res - get the timer resolution for a clock
1150 * @which_clock: which clock to query
1151 * @tp: pointer to timespec variable to store the resolution
1153 * Store the resolution of the clock selected by @which_clock in the
1154 * variable pointed to by @tp.
1156 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1158 struct hrtimer_cpu_base *cpu_base;
1160 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1161 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
1165 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1167 static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
1169 struct hrtimer_clock_base *base = timer->base;
1170 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1171 enum hrtimer_restart (*fn)(struct hrtimer *);
1174 WARN_ON(!irqs_disabled());
1176 debug_deactivate(timer);
1177 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1178 timer_stats_account_hrtimer(timer);
1179 fn = timer->function;
1182 * Because we run timers from hardirq context, there is no chance
1183 * they get migrated to another cpu, therefore its safe to unlock
1186 raw_spin_unlock(&cpu_base->lock);
1187 trace_hrtimer_expire_entry(timer, now);
1188 restart = fn(timer);
1189 trace_hrtimer_expire_exit(timer);
1190 raw_spin_lock(&cpu_base->lock);
1193 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1194 * we do not reprogramm the event hardware. Happens either in
1195 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1197 if (restart != HRTIMER_NORESTART) {
1198 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1199 enqueue_hrtimer(timer, base);
1202 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1204 timer->state &= ~HRTIMER_STATE_CALLBACK;
1207 #ifdef CONFIG_HIGH_RES_TIMERS
1210 * High resolution timer interrupt
1211 * Called with interrupts disabled
1213 void hrtimer_interrupt(struct clock_event_device *dev)
1215 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1216 struct hrtimer_clock_base *base;
1217 ktime_t expires_next, now, entry_time, delta;
1220 BUG_ON(!cpu_base->hres_active);
1221 cpu_base->nr_events++;
1222 dev->next_event.tv64 = KTIME_MAX;
1224 entry_time = now = ktime_get();
1226 expires_next.tv64 = KTIME_MAX;
1228 raw_spin_lock(&cpu_base->lock);
1230 * We set expires_next to KTIME_MAX here with cpu_base->lock
1231 * held to prevent that a timer is enqueued in our queue via
1232 * the migration code. This does not affect enqueueing of
1233 * timers which run their callback and need to be requeued on
1236 cpu_base->expires_next.tv64 = KTIME_MAX;
1238 base = cpu_base->clock_base;
1240 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1242 struct timerqueue_node *node;
1244 basenow = ktime_add(now, base->offset);
1246 while ((node = timerqueue_getnext(&base->active))) {
1247 struct hrtimer *timer;
1249 timer = container_of(node, struct hrtimer, node);
1252 * The immediate goal for using the softexpires is
1253 * minimizing wakeups, not running timers at the
1254 * earliest interrupt after their soft expiration.
1255 * This allows us to avoid using a Priority Search
1256 * Tree, which can answer a stabbing querry for
1257 * overlapping intervals and instead use the simple
1258 * BST we already have.
1259 * We don't add extra wakeups by delaying timers that
1260 * are right-of a not yet expired timer, because that
1261 * timer will have to trigger a wakeup anyway.
1264 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
1267 expires = ktime_sub(hrtimer_get_expires(timer),
1269 if (expires.tv64 < expires_next.tv64)
1270 expires_next = expires;
1274 __run_hrtimer(timer, &basenow);
1280 * Store the new expiry value so the migration code can verify
1283 cpu_base->expires_next = expires_next;
1284 raw_spin_unlock(&cpu_base->lock);
1286 /* Reprogramming necessary ? */
1287 if (expires_next.tv64 == KTIME_MAX ||
1288 !tick_program_event(expires_next, 0)) {
1289 cpu_base->hang_detected = 0;
1294 * The next timer was already expired due to:
1296 * - long lasting callbacks
1297 * - being scheduled away when running in a VM
1299 * We need to prevent that we loop forever in the hrtimer
1300 * interrupt routine. We give it 3 attempts to avoid
1301 * overreacting on some spurious event.
1304 cpu_base->nr_retries++;
1308 * Give the system a chance to do something else than looping
1309 * here. We stored the entry time, so we know exactly how long
1310 * we spent here. We schedule the next event this amount of
1313 cpu_base->nr_hangs++;
1314 cpu_base->hang_detected = 1;
1315 delta = ktime_sub(now, entry_time);
1316 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1317 cpu_base->max_hang_time = delta;
1319 * Limit it to a sensible value as we enforce a longer
1320 * delay. Give the CPU at least 100ms to catch up.
1322 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1323 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1325 expires_next = ktime_add(now, delta);
1326 tick_program_event(expires_next, 1);
1327 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1328 ktime_to_ns(delta));
1332 * local version of hrtimer_peek_ahead_timers() called with interrupts
1335 static void __hrtimer_peek_ahead_timers(void)
1337 struct tick_device *td;
1339 if (!hrtimer_hres_active())
1342 td = &__get_cpu_var(tick_cpu_device);
1343 if (td && td->evtdev)
1344 hrtimer_interrupt(td->evtdev);
1348 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1350 * hrtimer_peek_ahead_timers will peek at the timer queue of
1351 * the current cpu and check if there are any timers for which
1352 * the soft expires time has passed. If any such timers exist,
1353 * they are run immediately and then removed from the timer queue.
1356 void hrtimer_peek_ahead_timers(void)
1358 unsigned long flags;
1360 local_irq_save(flags);
1361 __hrtimer_peek_ahead_timers();
1362 local_irq_restore(flags);
1365 static void run_hrtimer_softirq(struct softirq_action *h)
1367 hrtimer_peek_ahead_timers();
1370 #else /* CONFIG_HIGH_RES_TIMERS */
1372 static inline void __hrtimer_peek_ahead_timers(void) { }
1374 #endif /* !CONFIG_HIGH_RES_TIMERS */
1377 * Called from timer softirq every jiffy, expire hrtimers:
1379 * For HRT its the fall back code to run the softirq in the timer
1380 * softirq context in case the hrtimer initialization failed or has
1381 * not been done yet.
1383 void hrtimer_run_pending(void)
1385 if (hrtimer_hres_active())
1389 * This _is_ ugly: We have to check in the softirq context,
1390 * whether we can switch to highres and / or nohz mode. The
1391 * clocksource switch happens in the timer interrupt with
1392 * xtime_lock held. Notification from there only sets the
1393 * check bit in the tick_oneshot code, otherwise we might
1394 * deadlock vs. xtime_lock.
1396 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1397 hrtimer_switch_to_hres();
1401 * Called from hardirq context every jiffy
1403 void hrtimer_run_queues(void)
1405 struct timerqueue_node *node;
1406 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1407 struct hrtimer_clock_base *base;
1408 int index, gettime = 1;
1410 if (hrtimer_hres_active())
1413 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1414 base = &cpu_base->clock_base[index];
1415 if (!timerqueue_getnext(&base->active))
1419 hrtimer_get_softirq_time(cpu_base);
1423 raw_spin_lock(&cpu_base->lock);
1425 while ((node = timerqueue_getnext(&base->active))) {
1426 struct hrtimer *timer;
1428 timer = container_of(node, struct hrtimer, node);
1429 if (base->softirq_time.tv64 <=
1430 hrtimer_get_expires_tv64(timer))
1433 __run_hrtimer(timer, &base->softirq_time);
1435 raw_spin_unlock(&cpu_base->lock);
1440 * Sleep related functions:
1442 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1444 struct hrtimer_sleeper *t =
1445 container_of(timer, struct hrtimer_sleeper, timer);
1446 struct task_struct *task = t->task;
1450 wake_up_process(task);
1452 return HRTIMER_NORESTART;
1455 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1457 sl->timer.function = hrtimer_wakeup;
1460 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
1462 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1464 hrtimer_init_sleeper(t, current);
1467 set_current_state(TASK_INTERRUPTIBLE);
1468 hrtimer_start_expires(&t->timer, mode);
1469 if (!hrtimer_active(&t->timer))
1472 if (likely(t->task))
1475 hrtimer_cancel(&t->timer);
1476 mode = HRTIMER_MODE_ABS;
1478 } while (t->task && !signal_pending(current));
1480 __set_current_state(TASK_RUNNING);
1482 return t->task == NULL;
1485 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1487 struct timespec rmt;
1490 rem = hrtimer_expires_remaining(timer);
1493 rmt = ktime_to_timespec(rem);
1495 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1501 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1503 struct hrtimer_sleeper t;
1504 struct timespec __user *rmtp;
1507 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1509 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1511 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1514 rmtp = restart->nanosleep.rmtp;
1516 ret = update_rmtp(&t.timer, rmtp);
1521 /* The other values in restart are already filled in */
1522 ret = -ERESTART_RESTARTBLOCK;
1524 destroy_hrtimer_on_stack(&t.timer);
1528 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1529 const enum hrtimer_mode mode, const clockid_t clockid)
1531 struct restart_block *restart;
1532 struct hrtimer_sleeper t;
1534 unsigned long slack;
1536 slack = current->timer_slack_ns;
1537 if (rt_task(current))
1540 hrtimer_init_on_stack(&t.timer, clockid, mode);
1541 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1542 if (do_nanosleep(&t, mode))
1545 /* Absolute timers do not update the rmtp value and restart: */
1546 if (mode == HRTIMER_MODE_ABS) {
1547 ret = -ERESTARTNOHAND;
1552 ret = update_rmtp(&t.timer, rmtp);
1557 restart = ¤t_thread_info()->restart_block;
1558 restart->fn = hrtimer_nanosleep_restart;
1559 restart->nanosleep.index = t.timer.base->index;
1560 restart->nanosleep.rmtp = rmtp;
1561 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1563 ret = -ERESTART_RESTARTBLOCK;
1565 destroy_hrtimer_on_stack(&t.timer);
1569 SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1570 struct timespec __user *, rmtp)
1574 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1577 if (!timespec_valid(&tu))
1580 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1584 * Functions related to boot-time initialization:
1586 static void __cpuinit init_hrtimers_cpu(int cpu)
1588 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1591 raw_spin_lock_init(&cpu_base->lock);
1593 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1594 cpu_base->clock_base[i].cpu_base = cpu_base;
1595 timerqueue_init_head(&cpu_base->clock_base[i].active);
1598 hrtimer_init_hres(cpu_base);
1601 #ifdef CONFIG_HOTPLUG_CPU
1603 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1604 struct hrtimer_clock_base *new_base)
1606 struct hrtimer *timer;
1607 struct timerqueue_node *node;
1609 while ((node = timerqueue_getnext(&old_base->active))) {
1610 timer = container_of(node, struct hrtimer, node);
1611 BUG_ON(hrtimer_callback_running(timer));
1612 debug_deactivate(timer);
1615 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1616 * timer could be seen as !active and just vanish away
1617 * under us on another CPU
1619 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
1620 timer->base = new_base;
1622 * Enqueue the timers on the new cpu. This does not
1623 * reprogram the event device in case the timer
1624 * expires before the earliest on this CPU, but we run
1625 * hrtimer_interrupt after we migrated everything to
1626 * sort out already expired timers and reprogram the
1629 enqueue_hrtimer(timer, new_base);
1631 /* Clear the migration state bit */
1632 timer->state &= ~HRTIMER_STATE_MIGRATE;
1636 static void migrate_hrtimers(int scpu)
1638 struct hrtimer_cpu_base *old_base, *new_base;
1641 BUG_ON(cpu_online(scpu));
1642 tick_cancel_sched_timer(scpu);
1644 local_irq_disable();
1645 old_base = &per_cpu(hrtimer_bases, scpu);
1646 new_base = &__get_cpu_var(hrtimer_bases);
1648 * The caller is globally serialized and nobody else
1649 * takes two locks at once, deadlock is not possible.
1651 raw_spin_lock(&new_base->lock);
1652 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1654 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1655 migrate_hrtimer_list(&old_base->clock_base[i],
1656 &new_base->clock_base[i]);
1659 raw_spin_unlock(&old_base->lock);
1660 raw_spin_unlock(&new_base->lock);
1662 /* Check, if we got expired work to do */
1663 __hrtimer_peek_ahead_timers();
1667 #endif /* CONFIG_HOTPLUG_CPU */
1669 static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
1670 unsigned long action, void *hcpu)
1672 int scpu = (long)hcpu;
1676 case CPU_UP_PREPARE:
1677 case CPU_UP_PREPARE_FROZEN:
1678 init_hrtimers_cpu(scpu);
1681 #ifdef CONFIG_HOTPLUG_CPU
1683 case CPU_DYING_FROZEN:
1684 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1687 case CPU_DEAD_FROZEN:
1689 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1690 migrate_hrtimers(scpu);
1702 static struct notifier_block __cpuinitdata hrtimers_nb = {
1703 .notifier_call = hrtimer_cpu_notify,
1706 void __init hrtimers_init(void)
1708 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1709 (void *)(long)smp_processor_id());
1710 register_cpu_notifier(&hrtimers_nb);
1711 #ifdef CONFIG_HIGH_RES_TIMERS
1712 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1717 * schedule_hrtimeout_range_clock - sleep until timeout
1718 * @expires: timeout value (ktime_t)
1719 * @delta: slack in expires timeout (ktime_t)
1720 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1721 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1724 schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1725 const enum hrtimer_mode mode, int clock)
1727 struct hrtimer_sleeper t;
1730 * Optimize when a zero timeout value is given. It does not
1731 * matter whether this is an absolute or a relative time.
1733 if (expires && !expires->tv64) {
1734 __set_current_state(TASK_RUNNING);
1739 * A NULL parameter means "infinite"
1743 __set_current_state(TASK_RUNNING);
1747 hrtimer_init_on_stack(&t.timer, clock, mode);
1748 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1750 hrtimer_init_sleeper(&t, current);
1752 hrtimer_start_expires(&t.timer, mode);
1753 if (!hrtimer_active(&t.timer))
1759 hrtimer_cancel(&t.timer);
1760 destroy_hrtimer_on_stack(&t.timer);
1762 __set_current_state(TASK_RUNNING);
1764 return !t.task ? 0 : -EINTR;
1768 * schedule_hrtimeout_range - sleep until timeout
1769 * @expires: timeout value (ktime_t)
1770 * @delta: slack in expires timeout (ktime_t)
1771 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1773 * Make the current task sleep until the given expiry time has
1774 * elapsed. The routine will return immediately unless
1775 * the current task state has been set (see set_current_state()).
1777 * The @delta argument gives the kernel the freedom to schedule the
1778 * actual wakeup to a time that is both power and performance friendly.
1779 * The kernel give the normal best effort behavior for "@expires+@delta",
1780 * but may decide to fire the timer earlier, but no earlier than @expires.
1782 * You can set the task state as follows -
1784 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1785 * pass before the routine returns.
1787 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1788 * delivered to the current task.
1790 * The current task state is guaranteed to be TASK_RUNNING when this
1793 * Returns 0 when the timer has expired otherwise -EINTR
1795 int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1796 const enum hrtimer_mode mode)
1798 return schedule_hrtimeout_range_clock(expires, delta, mode,
1801 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1804 * schedule_hrtimeout - sleep until timeout
1805 * @expires: timeout value (ktime_t)
1806 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1808 * Make the current task sleep until the given expiry time has
1809 * elapsed. The routine will return immediately unless
1810 * the current task state has been set (see set_current_state()).
1812 * You can set the task state as follows -
1814 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1815 * pass before the routine returns.
1817 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1818 * delivered to the current task.
1820 * The current task state is guaranteed to be TASK_RUNNING when this
1823 * Returns 0 when the timer has expired otherwise -EINTR
1825 int __sched schedule_hrtimeout(ktime_t *expires,
1826 const enum hrtimer_mode mode)
1828 return schedule_hrtimeout_range(expires, 0, mode);
1830 EXPORT_SYMBOL_GPL(schedule_hrtimeout);