2 * linux/kernel/hrtimer.c
4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
8 * High-resolution kernel timers
10 * In contrast to the low-resolution timeout API implemented in
11 * kernel/timer.c, hrtimers provide finer resolution and accuracy
12 * depending on system configuration and capabilities.
14 * These timers are currently used for:
18 * - precise in-kernel timing
20 * Started by: Thomas Gleixner and Ingo Molnar
23 * based on kernel/timer.c
25 * Help, testing, suggestions, bugfixes, improvements were
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
31 * For licencing details see kernel-base/COPYING
34 #include <linux/cpu.h>
35 #include <linux/module.h>
36 #include <linux/percpu.h>
37 #include <linux/hrtimer.h>
38 #include <linux/notifier.h>
39 #include <linux/syscalls.h>
40 #include <linux/kallsyms.h>
41 #include <linux/interrupt.h>
42 #include <linux/tick.h>
43 #include <linux/seq_file.h>
44 #include <linux/err.h>
45 #include <linux/debugobjects.h>
47 #include <asm/uaccess.h>
50 * ktime_get - get the monotonic time in ktime_t format
52 * returns the time in ktime_t format
54 ktime_t
ktime_get(void)
60 return timespec_to_ktime(now
);
62 EXPORT_SYMBOL_GPL(ktime_get
);
65 * ktime_get_real - get the real (wall-) time in ktime_t format
67 * returns the time in ktime_t format
69 ktime_t
ktime_get_real(void)
75 return timespec_to_ktime(now
);
78 EXPORT_SYMBOL_GPL(ktime_get_real
);
83 * Note: If we want to add new timer bases, we have to skip the two
84 * clock ids captured by the cpu-timers. We do this by holding empty
85 * entries rather than doing math adjustment of the clock ids.
86 * This ensures that we capture erroneous accesses to these clock ids
87 * rather than moving them into the range of valid clock id's.
89 DEFINE_PER_CPU(struct hrtimer_cpu_base
, hrtimer_bases
) =
95 .index
= CLOCK_REALTIME
,
96 .get_time
= &ktime_get_real
,
97 .resolution
= KTIME_LOW_RES
,
100 .index
= CLOCK_MONOTONIC
,
101 .get_time
= &ktime_get
,
102 .resolution
= KTIME_LOW_RES
,
108 * ktime_get_ts - get the monotonic clock in timespec format
109 * @ts: pointer to timespec variable
111 * The function calculates the monotonic clock from the realtime
112 * clock and the wall_to_monotonic offset and stores the result
113 * in normalized timespec format in the variable pointed to by @ts.
115 void ktime_get_ts(struct timespec
*ts
)
117 struct timespec tomono
;
121 seq
= read_seqbegin(&xtime_lock
);
123 tomono
= wall_to_monotonic
;
125 } while (read_seqretry(&xtime_lock
, seq
));
127 set_normalized_timespec(ts
, ts
->tv_sec
+ tomono
.tv_sec
,
128 ts
->tv_nsec
+ tomono
.tv_nsec
);
130 EXPORT_SYMBOL_GPL(ktime_get_ts
);
133 * Get the coarse grained time at the softirq based on xtime and
136 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base
*base
)
138 ktime_t xtim
, tomono
;
139 struct timespec xts
, tom
;
143 seq
= read_seqbegin(&xtime_lock
);
144 xts
= current_kernel_time();
145 tom
= wall_to_monotonic
;
146 } while (read_seqretry(&xtime_lock
, seq
));
148 xtim
= timespec_to_ktime(xts
);
149 tomono
= timespec_to_ktime(tom
);
150 base
->clock_base
[CLOCK_REALTIME
].softirq_time
= xtim
;
151 base
->clock_base
[CLOCK_MONOTONIC
].softirq_time
=
152 ktime_add(xtim
, tomono
);
156 * Functions and macros which are different for UP/SMP systems are kept in a
162 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
163 * means that all timers which are tied to this base via timer->base are
164 * locked, and the base itself is locked too.
166 * So __run_timers/migrate_timers can safely modify all timers which could
167 * be found on the lists/queues.
169 * When the timer's base is locked, and the timer removed from list, it is
170 * possible to set timer->base = NULL and drop the lock: the timer remains
174 struct hrtimer_clock_base
*lock_hrtimer_base(const struct hrtimer
*timer
,
175 unsigned long *flags
)
177 struct hrtimer_clock_base
*base
;
181 if (likely(base
!= NULL
)) {
182 spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
183 if (likely(base
== timer
->base
))
185 /* The timer has migrated to another CPU: */
186 spin_unlock_irqrestore(&base
->cpu_base
->lock
, *flags
);
193 * Switch the timer base to the current CPU when possible.
195 static inline struct hrtimer_clock_base
*
196 switch_hrtimer_base(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
)
198 struct hrtimer_clock_base
*new_base
;
199 struct hrtimer_cpu_base
*new_cpu_base
;
201 new_cpu_base
= &__get_cpu_var(hrtimer_bases
);
202 new_base
= &new_cpu_base
->clock_base
[base
->index
];
204 if (base
!= new_base
) {
206 * We are trying to schedule the timer on the local CPU.
207 * However we can't change timer's base while it is running,
208 * so we keep it on the same CPU. No hassle vs. reprogramming
209 * the event source in the high resolution case. The softirq
210 * code will take care of this when the timer function has
211 * completed. There is no conflict as we hold the lock until
212 * the timer is enqueued.
214 if (unlikely(hrtimer_callback_running(timer
)))
217 /* See the comment in lock_timer_base() */
219 spin_unlock(&base
->cpu_base
->lock
);
220 spin_lock(&new_base
->cpu_base
->lock
);
221 timer
->base
= new_base
;
226 #else /* CONFIG_SMP */
228 static inline struct hrtimer_clock_base
*
229 lock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
231 struct hrtimer_clock_base
*base
= timer
->base
;
233 spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
238 # define switch_hrtimer_base(t, b) (b)
240 #endif /* !CONFIG_SMP */
243 * Functions for the union type storage format of ktime_t which are
244 * too large for inlining:
246 #if BITS_PER_LONG < 64
247 # ifndef CONFIG_KTIME_SCALAR
249 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
251 * @nsec: the scalar nsec value to add
253 * Returns the sum of kt and nsec in ktime_t format
255 ktime_t
ktime_add_ns(const ktime_t kt
, u64 nsec
)
259 if (likely(nsec
< NSEC_PER_SEC
)) {
262 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
264 tmp
= ktime_set((long)nsec
, rem
);
267 return ktime_add(kt
, tmp
);
270 EXPORT_SYMBOL_GPL(ktime_add_ns
);
273 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
275 * @nsec: the scalar nsec value to subtract
277 * Returns the subtraction of @nsec from @kt in ktime_t format
279 ktime_t
ktime_sub_ns(const ktime_t kt
, u64 nsec
)
283 if (likely(nsec
< NSEC_PER_SEC
)) {
286 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
288 tmp
= ktime_set((long)nsec
, rem
);
291 return ktime_sub(kt
, tmp
);
294 EXPORT_SYMBOL_GPL(ktime_sub_ns
);
295 # endif /* !CONFIG_KTIME_SCALAR */
298 * Divide a ktime value by a nanosecond value
300 u64
ktime_divns(const ktime_t kt
, s64 div
)
305 dclc
= ktime_to_ns(kt
);
306 /* Make sure the divisor is less than 2^32: */
312 do_div(dclc
, (unsigned long) div
);
316 #endif /* BITS_PER_LONG >= 64 */
319 * Add two ktime values and do a safety check for overflow:
321 ktime_t
ktime_add_safe(const ktime_t lhs
, const ktime_t rhs
)
323 ktime_t res
= ktime_add(lhs
, rhs
);
326 * We use KTIME_SEC_MAX here, the maximum timeout which we can
327 * return to user space in a timespec:
329 if (res
.tv64
< 0 || res
.tv64
< lhs
.tv64
|| res
.tv64
< rhs
.tv64
)
330 res
= ktime_set(KTIME_SEC_MAX
, 0);
335 EXPORT_SYMBOL_GPL(ktime_add_safe
);
337 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
339 static struct debug_obj_descr hrtimer_debug_descr
;
342 * fixup_init is called when:
343 * - an active object is initialized
345 static int hrtimer_fixup_init(void *addr
, enum debug_obj_state state
)
347 struct hrtimer
*timer
= addr
;
350 case ODEBUG_STATE_ACTIVE
:
351 hrtimer_cancel(timer
);
352 debug_object_init(timer
, &hrtimer_debug_descr
);
360 * fixup_activate is called when:
361 * - an active object is activated
362 * - an unknown object is activated (might be a statically initialized object)
364 static int hrtimer_fixup_activate(void *addr
, enum debug_obj_state state
)
368 case ODEBUG_STATE_NOTAVAILABLE
:
372 case ODEBUG_STATE_ACTIVE
:
381 * fixup_free is called when:
382 * - an active object is freed
384 static int hrtimer_fixup_free(void *addr
, enum debug_obj_state state
)
386 struct hrtimer
*timer
= addr
;
389 case ODEBUG_STATE_ACTIVE
:
390 hrtimer_cancel(timer
);
391 debug_object_free(timer
, &hrtimer_debug_descr
);
398 static struct debug_obj_descr hrtimer_debug_descr
= {
400 .fixup_init
= hrtimer_fixup_init
,
401 .fixup_activate
= hrtimer_fixup_activate
,
402 .fixup_free
= hrtimer_fixup_free
,
405 static inline void debug_hrtimer_init(struct hrtimer
*timer
)
407 debug_object_init(timer
, &hrtimer_debug_descr
);
410 static inline void debug_hrtimer_activate(struct hrtimer
*timer
)
412 debug_object_activate(timer
, &hrtimer_debug_descr
);
415 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
)
417 debug_object_deactivate(timer
, &hrtimer_debug_descr
);
420 static inline void debug_hrtimer_free(struct hrtimer
*timer
)
422 debug_object_free(timer
, &hrtimer_debug_descr
);
425 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
426 enum hrtimer_mode mode
);
428 void hrtimer_init_on_stack(struct hrtimer
*timer
, clockid_t clock_id
,
429 enum hrtimer_mode mode
)
431 debug_object_init_on_stack(timer
, &hrtimer_debug_descr
);
432 __hrtimer_init(timer
, clock_id
, mode
);
435 void destroy_hrtimer_on_stack(struct hrtimer
*timer
)
437 debug_object_free(timer
, &hrtimer_debug_descr
);
441 static inline void debug_hrtimer_init(struct hrtimer
*timer
) { }
442 static inline void debug_hrtimer_activate(struct hrtimer
*timer
) { }
443 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
) { }
446 /* High resolution timer related functions */
447 #ifdef CONFIG_HIGH_RES_TIMERS
450 * High resolution timer enabled ?
452 static int hrtimer_hres_enabled __read_mostly
= 1;
455 * Enable / Disable high resolution mode
457 static int __init
setup_hrtimer_hres(char *str
)
459 if (!strcmp(str
, "off"))
460 hrtimer_hres_enabled
= 0;
461 else if (!strcmp(str
, "on"))
462 hrtimer_hres_enabled
= 1;
468 __setup("highres=", setup_hrtimer_hres
);
471 * hrtimer_high_res_enabled - query, if the highres mode is enabled
473 static inline int hrtimer_is_hres_enabled(void)
475 return hrtimer_hres_enabled
;
479 * Is the high resolution mode active ?
481 static inline int hrtimer_hres_active(void)
483 return __get_cpu_var(hrtimer_bases
).hres_active
;
487 * Reprogram the event source with checking both queues for the
489 * Called with interrupts disabled and base->lock held
491 static void hrtimer_force_reprogram(struct hrtimer_cpu_base
*cpu_base
)
494 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
497 cpu_base
->expires_next
.tv64
= KTIME_MAX
;
499 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
500 struct hrtimer
*timer
;
504 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
505 expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
507 * clock_was_set() has changed base->offset so the
508 * result might be negative. Fix it up to prevent a
509 * false positive in clockevents_program_event()
511 if (expires
.tv64
< 0)
513 if (expires
.tv64
< cpu_base
->expires_next
.tv64
)
514 cpu_base
->expires_next
= expires
;
517 if (cpu_base
->expires_next
.tv64
!= KTIME_MAX
)
518 tick_program_event(cpu_base
->expires_next
, 1);
522 * Shared reprogramming for clock_realtime and clock_monotonic
524 * When a timer is enqueued and expires earlier than the already enqueued
525 * timers, we have to check, whether it expires earlier than the timer for
526 * which the clock event device was armed.
528 * Called with interrupts disabled and base->cpu_base.lock held
530 static int hrtimer_reprogram(struct hrtimer
*timer
,
531 struct hrtimer_clock_base
*base
)
533 ktime_t
*expires_next
= &__get_cpu_var(hrtimer_bases
).expires_next
;
534 ktime_t expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
537 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer
) < 0);
540 * When the callback is running, we do not reprogram the clock event
541 * device. The timer callback is either running on a different CPU or
542 * the callback is executed in the hrtimer_interrupt context. The
543 * reprogramming is handled either by the softirq, which called the
544 * callback or at the end of the hrtimer_interrupt.
546 if (hrtimer_callback_running(timer
))
550 * CLOCK_REALTIME timer might be requested with an absolute
551 * expiry time which is less than base->offset. Nothing wrong
552 * about that, just avoid to call into the tick code, which
553 * has now objections against negative expiry values.
555 if (expires
.tv64
< 0)
558 if (expires
.tv64
>= expires_next
->tv64
)
562 * Clockevents returns -ETIME, when the event was in the past.
564 res
= tick_program_event(expires
, 0);
565 if (!IS_ERR_VALUE(res
))
566 *expires_next
= expires
;
572 * Retrigger next event is called after clock was set
574 * Called with interrupts disabled via on_each_cpu()
576 static void retrigger_next_event(void *arg
)
578 struct hrtimer_cpu_base
*base
;
579 struct timespec realtime_offset
;
582 if (!hrtimer_hres_active())
586 seq
= read_seqbegin(&xtime_lock
);
587 set_normalized_timespec(&realtime_offset
,
588 -wall_to_monotonic
.tv_sec
,
589 -wall_to_monotonic
.tv_nsec
);
590 } while (read_seqretry(&xtime_lock
, seq
));
592 base
= &__get_cpu_var(hrtimer_bases
);
594 /* Adjust CLOCK_REALTIME offset */
595 spin_lock(&base
->lock
);
596 base
->clock_base
[CLOCK_REALTIME
].offset
=
597 timespec_to_ktime(realtime_offset
);
599 hrtimer_force_reprogram(base
);
600 spin_unlock(&base
->lock
);
604 * Clock realtime was set
606 * Change the offset of the realtime clock vs. the monotonic
609 * We might have to reprogram the high resolution timer interrupt. On
610 * SMP we call the architecture specific code to retrigger _all_ high
611 * resolution timer interrupts. On UP we just disable interrupts and
612 * call the high resolution interrupt code.
614 void clock_was_set(void)
616 /* Retrigger the CPU local events everywhere */
617 on_each_cpu(retrigger_next_event
, NULL
, 1);
621 * During resume we might have to reprogram the high resolution timer
622 * interrupt (on the local CPU):
624 void hres_timers_resume(void)
626 WARN_ONCE(!irqs_disabled(),
627 KERN_INFO
"hres_timers_resume() called with IRQs enabled!");
629 retrigger_next_event(NULL
);
633 * Initialize the high resolution related parts of cpu_base
635 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
)
637 base
->expires_next
.tv64
= KTIME_MAX
;
638 base
->hres_active
= 0;
642 * Initialize the high resolution related parts of a hrtimer
644 static inline void hrtimer_init_timer_hres(struct hrtimer
*timer
)
650 * When High resolution timers are active, try to reprogram. Note, that in case
651 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
652 * check happens. The timer gets enqueued into the rbtree. The reprogramming
653 * and expiry check is done in the hrtimer_interrupt or in the softirq.
655 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
656 struct hrtimer_clock_base
*base
,
659 if (base
->cpu_base
->hres_active
&& hrtimer_reprogram(timer
, base
)) {
661 spin_unlock(&base
->cpu_base
->lock
);
662 raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
663 spin_lock(&base
->cpu_base
->lock
);
665 __raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
674 * Switch to high resolution mode
676 static int hrtimer_switch_to_hres(void)
678 int cpu
= smp_processor_id();
679 struct hrtimer_cpu_base
*base
= &per_cpu(hrtimer_bases
, cpu
);
682 if (base
->hres_active
)
685 local_irq_save(flags
);
687 if (tick_init_highres()) {
688 local_irq_restore(flags
);
689 printk(KERN_WARNING
"Could not switch to high resolution "
690 "mode on CPU %d\n", cpu
);
693 base
->hres_active
= 1;
694 base
->clock_base
[CLOCK_REALTIME
].resolution
= KTIME_HIGH_RES
;
695 base
->clock_base
[CLOCK_MONOTONIC
].resolution
= KTIME_HIGH_RES
;
697 tick_setup_sched_timer();
699 /* "Retrigger" the interrupt to get things going */
700 retrigger_next_event(NULL
);
701 local_irq_restore(flags
);
702 printk(KERN_DEBUG
"Switched to high resolution mode on CPU %d\n",
709 static inline int hrtimer_hres_active(void) { return 0; }
710 static inline int hrtimer_is_hres_enabled(void) { return 0; }
711 static inline int hrtimer_switch_to_hres(void) { return 0; }
712 static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base
*base
) { }
713 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
714 struct hrtimer_clock_base
*base
,
719 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
) { }
720 static inline void hrtimer_init_timer_hres(struct hrtimer
*timer
) { }
722 #endif /* CONFIG_HIGH_RES_TIMERS */
724 #ifdef CONFIG_TIMER_STATS
725 void __timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
, void *addr
)
727 if (timer
->start_site
)
730 timer
->start_site
= addr
;
731 memcpy(timer
->start_comm
, current
->comm
, TASK_COMM_LEN
);
732 timer
->start_pid
= current
->pid
;
737 * Counterpart to lock_hrtimer_base above:
740 void unlock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
742 spin_unlock_irqrestore(&timer
->base
->cpu_base
->lock
, *flags
);
746 * hrtimer_forward - forward the timer expiry
747 * @timer: hrtimer to forward
748 * @now: forward past this time
749 * @interval: the interval to forward
751 * Forward the timer expiry so it will expire in the future.
752 * Returns the number of overruns.
754 u64
hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
)
759 delta
= ktime_sub(now
, hrtimer_get_expires(timer
));
764 if (interval
.tv64
< timer
->base
->resolution
.tv64
)
765 interval
.tv64
= timer
->base
->resolution
.tv64
;
767 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
768 s64 incr
= ktime_to_ns(interval
);
770 orun
= ktime_divns(delta
, incr
);
771 hrtimer_add_expires_ns(timer
, incr
* orun
);
772 if (hrtimer_get_expires_tv64(timer
) > now
.tv64
)
775 * This (and the ktime_add() below) is the
776 * correction for exact:
780 hrtimer_add_expires(timer
, interval
);
784 EXPORT_SYMBOL_GPL(hrtimer_forward
);
787 * enqueue_hrtimer - internal function to (re)start a timer
789 * The timer is inserted in expiry order. Insertion into the
790 * red black tree is O(log(n)). Must hold the base lock.
792 * Returns 1 when the new timer is the leftmost timer in the tree.
794 static int enqueue_hrtimer(struct hrtimer
*timer
,
795 struct hrtimer_clock_base
*base
)
797 struct rb_node
**link
= &base
->active
.rb_node
;
798 struct rb_node
*parent
= NULL
;
799 struct hrtimer
*entry
;
802 debug_hrtimer_activate(timer
);
805 * Find the right place in the rbtree:
809 entry
= rb_entry(parent
, struct hrtimer
, node
);
811 * We dont care about collisions. Nodes with
812 * the same expiry time stay together.
814 if (hrtimer_get_expires_tv64(timer
) <
815 hrtimer_get_expires_tv64(entry
)) {
816 link
= &(*link
)->rb_left
;
818 link
= &(*link
)->rb_right
;
824 * Insert the timer to the rbtree and check whether it
825 * replaces the first pending timer
828 base
->first
= &timer
->node
;
830 rb_link_node(&timer
->node
, parent
, link
);
831 rb_insert_color(&timer
->node
, &base
->active
);
833 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
834 * state of a possibly running callback.
836 timer
->state
|= HRTIMER_STATE_ENQUEUED
;
842 * __remove_hrtimer - internal function to remove a timer
844 * Caller must hold the base lock.
846 * High resolution timer mode reprograms the clock event device when the
847 * timer is the one which expires next. The caller can disable this by setting
848 * reprogram to zero. This is useful, when the context does a reprogramming
849 * anyway (e.g. timer interrupt)
851 static void __remove_hrtimer(struct hrtimer
*timer
,
852 struct hrtimer_clock_base
*base
,
853 unsigned long newstate
, int reprogram
)
855 if (timer
->state
& HRTIMER_STATE_ENQUEUED
) {
857 * Remove the timer from the rbtree and replace the
858 * first entry pointer if necessary.
860 if (base
->first
== &timer
->node
) {
861 base
->first
= rb_next(&timer
->node
);
862 /* Reprogram the clock event device. if enabled */
863 if (reprogram
&& hrtimer_hres_active())
864 hrtimer_force_reprogram(base
->cpu_base
);
866 rb_erase(&timer
->node
, &base
->active
);
868 timer
->state
= newstate
;
872 * remove hrtimer, called with base lock held
875 remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
)
877 if (hrtimer_is_queued(timer
)) {
881 * Remove the timer and force reprogramming when high
882 * resolution mode is active and the timer is on the current
883 * CPU. If we remove a timer on another CPU, reprogramming is
884 * skipped. The interrupt event on this CPU is fired and
885 * reprogramming happens in the interrupt handler. This is a
886 * rare case and less expensive than a smp call.
888 debug_hrtimer_deactivate(timer
);
889 timer_stats_hrtimer_clear_start_info(timer
);
890 reprogram
= base
->cpu_base
== &__get_cpu_var(hrtimer_bases
);
891 __remove_hrtimer(timer
, base
, HRTIMER_STATE_INACTIVE
,
898 int __hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
899 unsigned long delta_ns
, const enum hrtimer_mode mode
,
902 struct hrtimer_clock_base
*base
, *new_base
;
906 base
= lock_hrtimer_base(timer
, &flags
);
908 /* Remove an active timer from the queue: */
909 ret
= remove_hrtimer(timer
, base
);
911 /* Switch the timer base, if necessary: */
912 new_base
= switch_hrtimer_base(timer
, base
);
914 if (mode
== HRTIMER_MODE_REL
) {
915 tim
= ktime_add_safe(tim
, new_base
->get_time());
917 * CONFIG_TIME_LOW_RES is a temporary way for architectures
918 * to signal that they simply return xtime in
919 * do_gettimeoffset(). In this case we want to round up by
920 * resolution when starting a relative timer, to avoid short
921 * timeouts. This will go away with the GTOD framework.
923 #ifdef CONFIG_TIME_LOW_RES
924 tim
= ktime_add_safe(tim
, base
->resolution
);
928 hrtimer_set_expires_range_ns(timer
, tim
, delta_ns
);
930 timer_stats_hrtimer_set_start_info(timer
);
932 leftmost
= enqueue_hrtimer(timer
, new_base
);
935 * Only allow reprogramming if the new base is on this CPU.
936 * (it might still be on another CPU if the timer was pending)
938 * XXX send_remote_softirq() ?
940 if (leftmost
&& new_base
->cpu_base
== &__get_cpu_var(hrtimer_bases
))
941 hrtimer_enqueue_reprogram(timer
, new_base
, wakeup
);
943 unlock_hrtimer_base(timer
, &flags
);
949 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
950 * @timer: the timer to be added
952 * @delta_ns: "slack" range for the timer
953 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
957 * 1 when the timer was active
959 int hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
960 unsigned long delta_ns
, const enum hrtimer_mode mode
)
962 return __hrtimer_start_range_ns(timer
, tim
, delta_ns
, mode
, 1);
964 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns
);
967 * hrtimer_start - (re)start an hrtimer on the current CPU
968 * @timer: the timer to be added
970 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
974 * 1 when the timer was active
977 hrtimer_start(struct hrtimer
*timer
, ktime_t tim
, const enum hrtimer_mode mode
)
979 return __hrtimer_start_range_ns(timer
, tim
, 0, mode
, 1);
981 EXPORT_SYMBOL_GPL(hrtimer_start
);
985 * hrtimer_try_to_cancel - try to deactivate a timer
986 * @timer: hrtimer to stop
989 * 0 when the timer was not active
990 * 1 when the timer was active
991 * -1 when the timer is currently excuting the callback function and
994 int hrtimer_try_to_cancel(struct hrtimer
*timer
)
996 struct hrtimer_clock_base
*base
;
1000 base
= lock_hrtimer_base(timer
, &flags
);
1002 if (!hrtimer_callback_running(timer
))
1003 ret
= remove_hrtimer(timer
, base
);
1005 unlock_hrtimer_base(timer
, &flags
);
1010 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel
);
1013 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1014 * @timer: the timer to be cancelled
1017 * 0 when the timer was not active
1018 * 1 when the timer was active
1020 int hrtimer_cancel(struct hrtimer
*timer
)
1023 int ret
= hrtimer_try_to_cancel(timer
);
1030 EXPORT_SYMBOL_GPL(hrtimer_cancel
);
1033 * hrtimer_get_remaining - get remaining time for the timer
1034 * @timer: the timer to read
1036 ktime_t
hrtimer_get_remaining(const struct hrtimer
*timer
)
1038 struct hrtimer_clock_base
*base
;
1039 unsigned long flags
;
1042 base
= lock_hrtimer_base(timer
, &flags
);
1043 rem
= hrtimer_expires_remaining(timer
);
1044 unlock_hrtimer_base(timer
, &flags
);
1048 EXPORT_SYMBOL_GPL(hrtimer_get_remaining
);
1052 * hrtimer_get_next_event - get the time until next expiry event
1054 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1057 ktime_t
hrtimer_get_next_event(void)
1059 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1060 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
1061 ktime_t delta
, mindelta
= { .tv64
= KTIME_MAX
};
1062 unsigned long flags
;
1065 spin_lock_irqsave(&cpu_base
->lock
, flags
);
1067 if (!hrtimer_hres_active()) {
1068 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
1069 struct hrtimer
*timer
;
1074 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
1075 delta
.tv64
= hrtimer_get_expires_tv64(timer
);
1076 delta
= ktime_sub(delta
, base
->get_time());
1077 if (delta
.tv64
< mindelta
.tv64
)
1078 mindelta
.tv64
= delta
.tv64
;
1082 spin_unlock_irqrestore(&cpu_base
->lock
, flags
);
1084 if (mindelta
.tv64
< 0)
1090 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1091 enum hrtimer_mode mode
)
1093 struct hrtimer_cpu_base
*cpu_base
;
1095 memset(timer
, 0, sizeof(struct hrtimer
));
1097 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1099 if (clock_id
== CLOCK_REALTIME
&& mode
!= HRTIMER_MODE_ABS
)
1100 clock_id
= CLOCK_MONOTONIC
;
1102 timer
->base
= &cpu_base
->clock_base
[clock_id
];
1103 INIT_LIST_HEAD(&timer
->cb_entry
);
1104 hrtimer_init_timer_hres(timer
);
1106 #ifdef CONFIG_TIMER_STATS
1107 timer
->start_site
= NULL
;
1108 timer
->start_pid
= -1;
1109 memset(timer
->start_comm
, 0, TASK_COMM_LEN
);
1114 * hrtimer_init - initialize a timer to the given clock
1115 * @timer: the timer to be initialized
1116 * @clock_id: the clock to be used
1117 * @mode: timer mode abs/rel
1119 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1120 enum hrtimer_mode mode
)
1122 debug_hrtimer_init(timer
);
1123 __hrtimer_init(timer
, clock_id
, mode
);
1125 EXPORT_SYMBOL_GPL(hrtimer_init
);
1128 * hrtimer_get_res - get the timer resolution for a clock
1129 * @which_clock: which clock to query
1130 * @tp: pointer to timespec variable to store the resolution
1132 * Store the resolution of the clock selected by @which_clock in the
1133 * variable pointed to by @tp.
1135 int hrtimer_get_res(const clockid_t which_clock
, struct timespec
*tp
)
1137 struct hrtimer_cpu_base
*cpu_base
;
1139 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1140 *tp
= ktime_to_timespec(cpu_base
->clock_base
[which_clock
].resolution
);
1144 EXPORT_SYMBOL_GPL(hrtimer_get_res
);
1146 static void __run_hrtimer(struct hrtimer
*timer
)
1148 struct hrtimer_clock_base
*base
= timer
->base
;
1149 struct hrtimer_cpu_base
*cpu_base
= base
->cpu_base
;
1150 enum hrtimer_restart (*fn
)(struct hrtimer
*);
1153 WARN_ON(!irqs_disabled());
1155 debug_hrtimer_deactivate(timer
);
1156 __remove_hrtimer(timer
, base
, HRTIMER_STATE_CALLBACK
, 0);
1157 timer_stats_account_hrtimer(timer
);
1158 fn
= timer
->function
;
1161 * Because we run timers from hardirq context, there is no chance
1162 * they get migrated to another cpu, therefore its safe to unlock
1165 spin_unlock(&cpu_base
->lock
);
1166 restart
= fn(timer
);
1167 spin_lock(&cpu_base
->lock
);
1170 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1171 * we do not reprogramm the event hardware. Happens either in
1172 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1174 if (restart
!= HRTIMER_NORESTART
) {
1175 BUG_ON(timer
->state
!= HRTIMER_STATE_CALLBACK
);
1176 enqueue_hrtimer(timer
, base
);
1178 timer
->state
&= ~HRTIMER_STATE_CALLBACK
;
1181 #ifdef CONFIG_HIGH_RES_TIMERS
1183 static int force_clock_reprogram
;
1186 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1187 * is hanging, which could happen with something that slows the interrupt
1188 * such as the tracing. Then we force the clock reprogramming for each future
1189 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1190 * threshold that we will overwrite.
1191 * The next tick event will be scheduled to 3 times we currently spend on
1192 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1193 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1194 * let it running without serious starvation.
1198 hrtimer_interrupt_hanging(struct clock_event_device
*dev
,
1201 force_clock_reprogram
= 1;
1202 dev
->min_delta_ns
= (unsigned long)try_time
.tv64
* 3;
1203 printk(KERN_WARNING
"hrtimer: interrupt too slow, "
1204 "forcing clock min delta to %lu ns\n", dev
->min_delta_ns
);
1207 * High resolution timer interrupt
1208 * Called with interrupts disabled
1210 void hrtimer_interrupt(struct clock_event_device
*dev
)
1212 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1213 struct hrtimer_clock_base
*base
;
1214 ktime_t expires_next
, now
;
1218 BUG_ON(!cpu_base
->hres_active
);
1219 cpu_base
->nr_events
++;
1220 dev
->next_event
.tv64
= KTIME_MAX
;
1223 /* 5 retries is enough to notice a hang */
1224 if (!(++nr_retries
% 5))
1225 hrtimer_interrupt_hanging(dev
, ktime_sub(ktime_get(), now
));
1229 expires_next
.tv64
= KTIME_MAX
;
1231 base
= cpu_base
->clock_base
;
1233 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1235 struct rb_node
*node
;
1237 spin_lock(&cpu_base
->lock
);
1239 basenow
= ktime_add(now
, base
->offset
);
1241 while ((node
= base
->first
)) {
1242 struct hrtimer
*timer
;
1244 timer
= rb_entry(node
, struct hrtimer
, node
);
1247 * The immediate goal for using the softexpires is
1248 * minimizing wakeups, not running timers at the
1249 * earliest interrupt after their soft expiration.
1250 * This allows us to avoid using a Priority Search
1251 * Tree, which can answer a stabbing querry for
1252 * overlapping intervals and instead use the simple
1253 * BST we already have.
1254 * We don't add extra wakeups by delaying timers that
1255 * are right-of a not yet expired timer, because that
1256 * timer will have to trigger a wakeup anyway.
1259 if (basenow
.tv64
< hrtimer_get_softexpires_tv64(timer
)) {
1262 expires
= ktime_sub(hrtimer_get_expires(timer
),
1264 if (expires
.tv64
< expires_next
.tv64
)
1265 expires_next
= expires
;
1269 __run_hrtimer(timer
);
1271 spin_unlock(&cpu_base
->lock
);
1275 cpu_base
->expires_next
= expires_next
;
1277 /* Reprogramming necessary ? */
1278 if (expires_next
.tv64
!= KTIME_MAX
) {
1279 if (tick_program_event(expires_next
, force_clock_reprogram
))
1285 * local version of hrtimer_peek_ahead_timers() called with interrupts
1288 static void __hrtimer_peek_ahead_timers(void)
1290 struct tick_device
*td
;
1292 if (!hrtimer_hres_active())
1295 td
= &__get_cpu_var(tick_cpu_device
);
1296 if (td
&& td
->evtdev
)
1297 hrtimer_interrupt(td
->evtdev
);
1301 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1303 * hrtimer_peek_ahead_timers will peek at the timer queue of
1304 * the current cpu and check if there are any timers for which
1305 * the soft expires time has passed. If any such timers exist,
1306 * they are run immediately and then removed from the timer queue.
1309 void hrtimer_peek_ahead_timers(void)
1311 unsigned long flags
;
1313 local_irq_save(flags
);
1314 __hrtimer_peek_ahead_timers();
1315 local_irq_restore(flags
);
1318 static void run_hrtimer_softirq(struct softirq_action
*h
)
1320 hrtimer_peek_ahead_timers();
1323 #else /* CONFIG_HIGH_RES_TIMERS */
1325 static inline void __hrtimer_peek_ahead_timers(void) { }
1327 #endif /* !CONFIG_HIGH_RES_TIMERS */
1330 * Called from timer softirq every jiffy, expire hrtimers:
1332 * For HRT its the fall back code to run the softirq in the timer
1333 * softirq context in case the hrtimer initialization failed or has
1334 * not been done yet.
1336 void hrtimer_run_pending(void)
1338 if (hrtimer_hres_active())
1342 * This _is_ ugly: We have to check in the softirq context,
1343 * whether we can switch to highres and / or nohz mode. The
1344 * clocksource switch happens in the timer interrupt with
1345 * xtime_lock held. Notification from there only sets the
1346 * check bit in the tick_oneshot code, otherwise we might
1347 * deadlock vs. xtime_lock.
1349 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1350 hrtimer_switch_to_hres();
1354 * Called from hardirq context every jiffy
1356 void hrtimer_run_queues(void)
1358 struct rb_node
*node
;
1359 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1360 struct hrtimer_clock_base
*base
;
1361 int index
, gettime
= 1;
1363 if (hrtimer_hres_active())
1366 for (index
= 0; index
< HRTIMER_MAX_CLOCK_BASES
; index
++) {
1367 base
= &cpu_base
->clock_base
[index
];
1373 hrtimer_get_softirq_time(cpu_base
);
1377 spin_lock(&cpu_base
->lock
);
1379 while ((node
= base
->first
)) {
1380 struct hrtimer
*timer
;
1382 timer
= rb_entry(node
, struct hrtimer
, node
);
1383 if (base
->softirq_time
.tv64
<=
1384 hrtimer_get_expires_tv64(timer
))
1387 __run_hrtimer(timer
);
1389 spin_unlock(&cpu_base
->lock
);
1394 * Sleep related functions:
1396 static enum hrtimer_restart
hrtimer_wakeup(struct hrtimer
*timer
)
1398 struct hrtimer_sleeper
*t
=
1399 container_of(timer
, struct hrtimer_sleeper
, timer
);
1400 struct task_struct
*task
= t
->task
;
1404 wake_up_process(task
);
1406 return HRTIMER_NORESTART
;
1409 void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
, struct task_struct
*task
)
1411 sl
->timer
.function
= hrtimer_wakeup
;
1415 static int __sched
do_nanosleep(struct hrtimer_sleeper
*t
, enum hrtimer_mode mode
)
1417 hrtimer_init_sleeper(t
, current
);
1420 set_current_state(TASK_INTERRUPTIBLE
);
1421 hrtimer_start_expires(&t
->timer
, mode
);
1422 if (!hrtimer_active(&t
->timer
))
1425 if (likely(t
->task
))
1428 hrtimer_cancel(&t
->timer
);
1429 mode
= HRTIMER_MODE_ABS
;
1431 } while (t
->task
&& !signal_pending(current
));
1433 __set_current_state(TASK_RUNNING
);
1435 return t
->task
== NULL
;
1438 static int update_rmtp(struct hrtimer
*timer
, struct timespec __user
*rmtp
)
1440 struct timespec rmt
;
1443 rem
= hrtimer_expires_remaining(timer
);
1446 rmt
= ktime_to_timespec(rem
);
1448 if (copy_to_user(rmtp
, &rmt
, sizeof(*rmtp
)))
1454 long __sched
hrtimer_nanosleep_restart(struct restart_block
*restart
)
1456 struct hrtimer_sleeper t
;
1457 struct timespec __user
*rmtp
;
1460 hrtimer_init_on_stack(&t
.timer
, restart
->nanosleep
.index
,
1462 hrtimer_set_expires_tv64(&t
.timer
, restart
->nanosleep
.expires
);
1464 if (do_nanosleep(&t
, HRTIMER_MODE_ABS
))
1467 rmtp
= restart
->nanosleep
.rmtp
;
1469 ret
= update_rmtp(&t
.timer
, rmtp
);
1474 /* The other values in restart are already filled in */
1475 ret
= -ERESTART_RESTARTBLOCK
;
1477 destroy_hrtimer_on_stack(&t
.timer
);
1481 long hrtimer_nanosleep(struct timespec
*rqtp
, struct timespec __user
*rmtp
,
1482 const enum hrtimer_mode mode
, const clockid_t clockid
)
1484 struct restart_block
*restart
;
1485 struct hrtimer_sleeper t
;
1487 unsigned long slack
;
1489 slack
= current
->timer_slack_ns
;
1490 if (rt_task(current
))
1493 hrtimer_init_on_stack(&t
.timer
, clockid
, mode
);
1494 hrtimer_set_expires_range_ns(&t
.timer
, timespec_to_ktime(*rqtp
), slack
);
1495 if (do_nanosleep(&t
, mode
))
1498 /* Absolute timers do not update the rmtp value and restart: */
1499 if (mode
== HRTIMER_MODE_ABS
) {
1500 ret
= -ERESTARTNOHAND
;
1505 ret
= update_rmtp(&t
.timer
, rmtp
);
1510 restart
= ¤t_thread_info()->restart_block
;
1511 restart
->fn
= hrtimer_nanosleep_restart
;
1512 restart
->nanosleep
.index
= t
.timer
.base
->index
;
1513 restart
->nanosleep
.rmtp
= rmtp
;
1514 restart
->nanosleep
.expires
= hrtimer_get_expires_tv64(&t
.timer
);
1516 ret
= -ERESTART_RESTARTBLOCK
;
1518 destroy_hrtimer_on_stack(&t
.timer
);
1522 SYSCALL_DEFINE2(nanosleep
, struct timespec __user
*, rqtp
,
1523 struct timespec __user
*, rmtp
)
1527 if (copy_from_user(&tu
, rqtp
, sizeof(tu
)))
1530 if (!timespec_valid(&tu
))
1533 return hrtimer_nanosleep(&tu
, rmtp
, HRTIMER_MODE_REL
, CLOCK_MONOTONIC
);
1537 * Functions related to boot-time initialization:
1539 static void __cpuinit
init_hrtimers_cpu(int cpu
)
1541 struct hrtimer_cpu_base
*cpu_base
= &per_cpu(hrtimer_bases
, cpu
);
1544 spin_lock_init(&cpu_base
->lock
);
1546 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++)
1547 cpu_base
->clock_base
[i
].cpu_base
= cpu_base
;
1549 hrtimer_init_hres(cpu_base
);
1552 #ifdef CONFIG_HOTPLUG_CPU
1554 static void migrate_hrtimer_list(struct hrtimer_clock_base
*old_base
,
1555 struct hrtimer_clock_base
*new_base
)
1557 struct hrtimer
*timer
;
1558 struct rb_node
*node
;
1560 while ((node
= rb_first(&old_base
->active
))) {
1561 timer
= rb_entry(node
, struct hrtimer
, node
);
1562 BUG_ON(hrtimer_callback_running(timer
));
1563 debug_hrtimer_deactivate(timer
);
1566 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1567 * timer could be seen as !active and just vanish away
1568 * under us on another CPU
1570 __remove_hrtimer(timer
, old_base
, HRTIMER_STATE_MIGRATE
, 0);
1571 timer
->base
= new_base
;
1573 * Enqueue the timers on the new cpu. This does not
1574 * reprogram the event device in case the timer
1575 * expires before the earliest on this CPU, but we run
1576 * hrtimer_interrupt after we migrated everything to
1577 * sort out already expired timers and reprogram the
1580 enqueue_hrtimer(timer
, new_base
);
1582 /* Clear the migration state bit */
1583 timer
->state
&= ~HRTIMER_STATE_MIGRATE
;
1587 static void migrate_hrtimers(int scpu
)
1589 struct hrtimer_cpu_base
*old_base
, *new_base
;
1592 BUG_ON(cpu_online(scpu
));
1593 tick_cancel_sched_timer(scpu
);
1595 local_irq_disable();
1596 old_base
= &per_cpu(hrtimer_bases
, scpu
);
1597 new_base
= &__get_cpu_var(hrtimer_bases
);
1599 * The caller is globally serialized and nobody else
1600 * takes two locks at once, deadlock is not possible.
1602 spin_lock(&new_base
->lock
);
1603 spin_lock_nested(&old_base
->lock
, SINGLE_DEPTH_NESTING
);
1605 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1606 migrate_hrtimer_list(&old_base
->clock_base
[i
],
1607 &new_base
->clock_base
[i
]);
1610 spin_unlock(&old_base
->lock
);
1611 spin_unlock(&new_base
->lock
);
1613 /* Check, if we got expired work to do */
1614 __hrtimer_peek_ahead_timers();
1618 #endif /* CONFIG_HOTPLUG_CPU */
1620 static int __cpuinit
hrtimer_cpu_notify(struct notifier_block
*self
,
1621 unsigned long action
, void *hcpu
)
1623 int scpu
= (long)hcpu
;
1627 case CPU_UP_PREPARE
:
1628 case CPU_UP_PREPARE_FROZEN
:
1629 init_hrtimers_cpu(scpu
);
1632 #ifdef CONFIG_HOTPLUG_CPU
1634 case CPU_DYING_FROZEN
:
1635 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING
, &scpu
);
1638 case CPU_DEAD_FROZEN
:
1640 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD
, &scpu
);
1641 migrate_hrtimers(scpu
);
1653 static struct notifier_block __cpuinitdata hrtimers_nb
= {
1654 .notifier_call
= hrtimer_cpu_notify
,
1657 void __init
hrtimers_init(void)
1659 hrtimer_cpu_notify(&hrtimers_nb
, (unsigned long)CPU_UP_PREPARE
,
1660 (void *)(long)smp_processor_id());
1661 register_cpu_notifier(&hrtimers_nb
);
1662 #ifdef CONFIG_HIGH_RES_TIMERS
1663 open_softirq(HRTIMER_SOFTIRQ
, run_hrtimer_softirq
);
1668 * schedule_hrtimeout_range - sleep until timeout
1669 * @expires: timeout value (ktime_t)
1670 * @delta: slack in expires timeout (ktime_t)
1671 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1673 * Make the current task sleep until the given expiry time has
1674 * elapsed. The routine will return immediately unless
1675 * the current task state has been set (see set_current_state()).
1677 * The @delta argument gives the kernel the freedom to schedule the
1678 * actual wakeup to a time that is both power and performance friendly.
1679 * The kernel give the normal best effort behavior for "@expires+@delta",
1680 * but may decide to fire the timer earlier, but no earlier than @expires.
1682 * You can set the task state as follows -
1684 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1685 * pass before the routine returns.
1687 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1688 * delivered to the current task.
1690 * The current task state is guaranteed to be TASK_RUNNING when this
1693 * Returns 0 when the timer has expired otherwise -EINTR
1695 int __sched
schedule_hrtimeout_range(ktime_t
*expires
, unsigned long delta
,
1696 const enum hrtimer_mode mode
)
1698 struct hrtimer_sleeper t
;
1701 * Optimize when a zero timeout value is given. It does not
1702 * matter whether this is an absolute or a relative time.
1704 if (expires
&& !expires
->tv64
) {
1705 __set_current_state(TASK_RUNNING
);
1710 * A NULL parameter means "inifinte"
1714 __set_current_state(TASK_RUNNING
);
1718 hrtimer_init_on_stack(&t
.timer
, CLOCK_MONOTONIC
, mode
);
1719 hrtimer_set_expires_range_ns(&t
.timer
, *expires
, delta
);
1721 hrtimer_init_sleeper(&t
, current
);
1723 hrtimer_start_expires(&t
.timer
, mode
);
1724 if (!hrtimer_active(&t
.timer
))
1730 hrtimer_cancel(&t
.timer
);
1731 destroy_hrtimer_on_stack(&t
.timer
);
1733 __set_current_state(TASK_RUNNING
);
1735 return !t
.task
? 0 : -EINTR
;
1737 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range
);
1740 * schedule_hrtimeout - sleep until timeout
1741 * @expires: timeout value (ktime_t)
1742 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1744 * Make the current task sleep until the given expiry time has
1745 * elapsed. The routine will return immediately unless
1746 * the current task state has been set (see set_current_state()).
1748 * You can set the task state as follows -
1750 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1751 * pass before the routine returns.
1753 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1754 * delivered to the current task.
1756 * The current task state is guaranteed to be TASK_RUNNING when this
1759 * Returns 0 when the timer has expired otherwise -EINTR
1761 int __sched
schedule_hrtimeout(ktime_t
*expires
,
1762 const enum hrtimer_mode mode
)
1764 return schedule_hrtimeout_range(expires
, 0, mode
);
1766 EXPORT_SYMBOL_GPL(schedule_hrtimeout
);