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>
54 * Note: If we want to add new timer bases, we have to skip the two
55 * clock ids captured by the cpu-timers. We do this by holding empty
56 * entries rather than doing math adjustment of the clock ids.
57 * This ensures that we capture erroneous accesses to these clock ids
58 * rather than moving them into the range of valid clock id's.
60 DEFINE_PER_CPU(struct hrtimer_cpu_base
, hrtimer_bases
) =
66 .index
= CLOCK_REALTIME
,
67 .get_time
= &ktime_get_real
,
68 .resolution
= KTIME_LOW_RES
,
71 .index
= CLOCK_MONOTONIC
,
72 .get_time
= &ktime_get
,
73 .resolution
= KTIME_LOW_RES
,
79 * Get the coarse grained time at the softirq based on xtime and
82 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base
*base
)
85 struct timespec xts
, tom
;
89 seq
= read_seqbegin(&xtime_lock
);
90 xts
= current_kernel_time();
91 tom
= wall_to_monotonic
;
92 } while (read_seqretry(&xtime_lock
, seq
));
94 xtim
= timespec_to_ktime(xts
);
95 tomono
= timespec_to_ktime(tom
);
96 base
->clock_base
[CLOCK_REALTIME
].softirq_time
= xtim
;
97 base
->clock_base
[CLOCK_MONOTONIC
].softirq_time
=
98 ktime_add(xtim
, tomono
);
102 * Functions and macros which are different for UP/SMP systems are kept in a
108 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
109 * means that all timers which are tied to this base via timer->base are
110 * locked, and the base itself is locked too.
112 * So __run_timers/migrate_timers can safely modify all timers which could
113 * be found on the lists/queues.
115 * When the timer's base is locked, and the timer removed from list, it is
116 * possible to set timer->base = NULL and drop the lock: the timer remains
120 struct hrtimer_clock_base
*lock_hrtimer_base(const struct hrtimer
*timer
,
121 unsigned long *flags
)
123 struct hrtimer_clock_base
*base
;
127 if (likely(base
!= NULL
)) {
128 spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
129 if (likely(base
== timer
->base
))
131 /* The timer has migrated to another CPU: */
132 spin_unlock_irqrestore(&base
->cpu_base
->lock
, *flags
);
140 * Get the preferred target CPU for NOHZ
142 static int hrtimer_get_target(int this_cpu
, int pinned
)
145 if (!pinned
&& get_sysctl_timer_migration() && idle_cpu(this_cpu
)) {
146 int preferred_cpu
= get_nohz_load_balancer();
148 if (preferred_cpu
>= 0)
149 return preferred_cpu
;
156 * With HIGHRES=y we do not migrate the timer when it is expiring
157 * before the next event on the target cpu because we cannot reprogram
158 * the target cpu hardware and we would cause it to fire late.
160 * Called with cpu_base->lock of target cpu held.
163 hrtimer_check_target(struct hrtimer
*timer
, struct hrtimer_clock_base
*new_base
)
165 #ifdef CONFIG_HIGH_RES_TIMERS
168 if (!new_base
->cpu_base
->hres_active
)
171 expires
= ktime_sub(hrtimer_get_expires(timer
), new_base
->offset
);
172 return expires
.tv64
<= new_base
->cpu_base
->expires_next
.tv64
;
179 * Switch the timer base to the current CPU when possible.
181 static inline struct hrtimer_clock_base
*
182 switch_hrtimer_base(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
,
185 struct hrtimer_clock_base
*new_base
;
186 struct hrtimer_cpu_base
*new_cpu_base
;
187 int this_cpu
= smp_processor_id();
188 int cpu
= hrtimer_get_target(this_cpu
, pinned
);
191 new_cpu_base
= &per_cpu(hrtimer_bases
, cpu
);
192 new_base
= &new_cpu_base
->clock_base
[base
->index
];
194 if (base
!= new_base
) {
196 * We are trying to move timer to new_base.
197 * However we can't change timer's base while it is running,
198 * so we keep it on the same CPU. No hassle vs. reprogramming
199 * the event source in the high resolution case. The softirq
200 * code will take care of this when the timer function has
201 * completed. There is no conflict as we hold the lock until
202 * the timer is enqueued.
204 if (unlikely(hrtimer_callback_running(timer
)))
207 /* See the comment in lock_timer_base() */
209 spin_unlock(&base
->cpu_base
->lock
);
210 spin_lock(&new_base
->cpu_base
->lock
);
212 if (cpu
!= this_cpu
&& hrtimer_check_target(timer
, new_base
)) {
214 spin_unlock(&new_base
->cpu_base
->lock
);
215 spin_lock(&base
->cpu_base
->lock
);
219 timer
->base
= new_base
;
224 #else /* CONFIG_SMP */
226 static inline struct hrtimer_clock_base
*
227 lock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
229 struct hrtimer_clock_base
*base
= timer
->base
;
231 spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
236 # define switch_hrtimer_base(t, b, p) (b)
238 #endif /* !CONFIG_SMP */
241 * Functions for the union type storage format of ktime_t which are
242 * too large for inlining:
244 #if BITS_PER_LONG < 64
245 # ifndef CONFIG_KTIME_SCALAR
247 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
249 * @nsec: the scalar nsec value to add
251 * Returns the sum of kt and nsec in ktime_t format
253 ktime_t
ktime_add_ns(const ktime_t kt
, u64 nsec
)
257 if (likely(nsec
< NSEC_PER_SEC
)) {
260 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
262 tmp
= ktime_set((long)nsec
, rem
);
265 return ktime_add(kt
, tmp
);
268 EXPORT_SYMBOL_GPL(ktime_add_ns
);
271 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
273 * @nsec: the scalar nsec value to subtract
275 * Returns the subtraction of @nsec from @kt in ktime_t format
277 ktime_t
ktime_sub_ns(const ktime_t kt
, u64 nsec
)
281 if (likely(nsec
< NSEC_PER_SEC
)) {
284 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
286 tmp
= ktime_set((long)nsec
, rem
);
289 return ktime_sub(kt
, tmp
);
292 EXPORT_SYMBOL_GPL(ktime_sub_ns
);
293 # endif /* !CONFIG_KTIME_SCALAR */
296 * Divide a ktime value by a nanosecond value
298 u64
ktime_divns(const ktime_t kt
, s64 div
)
303 dclc
= ktime_to_ns(kt
);
304 /* Make sure the divisor is less than 2^32: */
310 do_div(dclc
, (unsigned long) div
);
314 #endif /* BITS_PER_LONG >= 64 */
317 * Add two ktime values and do a safety check for overflow:
319 ktime_t
ktime_add_safe(const ktime_t lhs
, const ktime_t rhs
)
321 ktime_t res
= ktime_add(lhs
, rhs
);
324 * We use KTIME_SEC_MAX here, the maximum timeout which we can
325 * return to user space in a timespec:
327 if (res
.tv64
< 0 || res
.tv64
< lhs
.tv64
|| res
.tv64
< rhs
.tv64
)
328 res
= ktime_set(KTIME_SEC_MAX
, 0);
333 EXPORT_SYMBOL_GPL(ktime_add_safe
);
335 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
337 static struct debug_obj_descr hrtimer_debug_descr
;
340 * fixup_init is called when:
341 * - an active object is initialized
343 static int hrtimer_fixup_init(void *addr
, enum debug_obj_state state
)
345 struct hrtimer
*timer
= addr
;
348 case ODEBUG_STATE_ACTIVE
:
349 hrtimer_cancel(timer
);
350 debug_object_init(timer
, &hrtimer_debug_descr
);
358 * fixup_activate is called when:
359 * - an active object is activated
360 * - an unknown object is activated (might be a statically initialized object)
362 static int hrtimer_fixup_activate(void *addr
, enum debug_obj_state state
)
366 case ODEBUG_STATE_NOTAVAILABLE
:
370 case ODEBUG_STATE_ACTIVE
:
379 * fixup_free is called when:
380 * - an active object is freed
382 static int hrtimer_fixup_free(void *addr
, enum debug_obj_state state
)
384 struct hrtimer
*timer
= addr
;
387 case ODEBUG_STATE_ACTIVE
:
388 hrtimer_cancel(timer
);
389 debug_object_free(timer
, &hrtimer_debug_descr
);
396 static struct debug_obj_descr hrtimer_debug_descr
= {
398 .fixup_init
= hrtimer_fixup_init
,
399 .fixup_activate
= hrtimer_fixup_activate
,
400 .fixup_free
= hrtimer_fixup_free
,
403 static inline void debug_hrtimer_init(struct hrtimer
*timer
)
405 debug_object_init(timer
, &hrtimer_debug_descr
);
408 static inline void debug_hrtimer_activate(struct hrtimer
*timer
)
410 debug_object_activate(timer
, &hrtimer_debug_descr
);
413 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
)
415 debug_object_deactivate(timer
, &hrtimer_debug_descr
);
418 static inline void debug_hrtimer_free(struct hrtimer
*timer
)
420 debug_object_free(timer
, &hrtimer_debug_descr
);
423 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
424 enum hrtimer_mode mode
);
426 void hrtimer_init_on_stack(struct hrtimer
*timer
, clockid_t clock_id
,
427 enum hrtimer_mode mode
)
429 debug_object_init_on_stack(timer
, &hrtimer_debug_descr
);
430 __hrtimer_init(timer
, clock_id
, mode
);
433 void destroy_hrtimer_on_stack(struct hrtimer
*timer
)
435 debug_object_free(timer
, &hrtimer_debug_descr
);
439 static inline void debug_hrtimer_init(struct hrtimer
*timer
) { }
440 static inline void debug_hrtimer_activate(struct hrtimer
*timer
) { }
441 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
) { }
444 /* High resolution timer related functions */
445 #ifdef CONFIG_HIGH_RES_TIMERS
448 * High resolution timer enabled ?
450 static int hrtimer_hres_enabled __read_mostly
= 1;
453 * Enable / Disable high resolution mode
455 static int __init
setup_hrtimer_hres(char *str
)
457 if (!strcmp(str
, "off"))
458 hrtimer_hres_enabled
= 0;
459 else if (!strcmp(str
, "on"))
460 hrtimer_hres_enabled
= 1;
466 __setup("highres=", setup_hrtimer_hres
);
469 * hrtimer_high_res_enabled - query, if the highres mode is enabled
471 static inline int hrtimer_is_hres_enabled(void)
473 return hrtimer_hres_enabled
;
477 * Is the high resolution mode active ?
479 static inline int hrtimer_hres_active(void)
481 return __get_cpu_var(hrtimer_bases
).hres_active
;
485 * Reprogram the event source with checking both queues for the
487 * Called with interrupts disabled and base->lock held
489 static void hrtimer_force_reprogram(struct hrtimer_cpu_base
*cpu_base
)
492 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
495 cpu_base
->expires_next
.tv64
= KTIME_MAX
;
497 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
498 struct hrtimer
*timer
;
502 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
503 expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
505 * clock_was_set() has changed base->offset so the
506 * result might be negative. Fix it up to prevent a
507 * false positive in clockevents_program_event()
509 if (expires
.tv64
< 0)
511 if (expires
.tv64
< cpu_base
->expires_next
.tv64
)
512 cpu_base
->expires_next
= expires
;
515 if (cpu_base
->expires_next
.tv64
!= KTIME_MAX
)
516 tick_program_event(cpu_base
->expires_next
, 1);
520 * Shared reprogramming for clock_realtime and clock_monotonic
522 * When a timer is enqueued and expires earlier than the already enqueued
523 * timers, we have to check, whether it expires earlier than the timer for
524 * which the clock event device was armed.
526 * Called with interrupts disabled and base->cpu_base.lock held
528 static int hrtimer_reprogram(struct hrtimer
*timer
,
529 struct hrtimer_clock_base
*base
)
531 ktime_t
*expires_next
= &__get_cpu_var(hrtimer_bases
).expires_next
;
532 ktime_t expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
535 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer
) < 0);
538 * When the callback is running, we do not reprogram the clock event
539 * device. The timer callback is either running on a different CPU or
540 * the callback is executed in the hrtimer_interrupt context. The
541 * reprogramming is handled either by the softirq, which called the
542 * callback or at the end of the hrtimer_interrupt.
544 if (hrtimer_callback_running(timer
))
548 * CLOCK_REALTIME timer might be requested with an absolute
549 * expiry time which is less than base->offset. Nothing wrong
550 * about that, just avoid to call into the tick code, which
551 * has now objections against negative expiry values.
553 if (expires
.tv64
< 0)
556 if (expires
.tv64
>= expires_next
->tv64
)
560 * Clockevents returns -ETIME, when the event was in the past.
562 res
= tick_program_event(expires
, 0);
563 if (!IS_ERR_VALUE(res
))
564 *expires_next
= expires
;
570 * Retrigger next event is called after clock was set
572 * Called with interrupts disabled via on_each_cpu()
574 static void retrigger_next_event(void *arg
)
576 struct hrtimer_cpu_base
*base
;
577 struct timespec realtime_offset
;
580 if (!hrtimer_hres_active())
584 seq
= read_seqbegin(&xtime_lock
);
585 set_normalized_timespec(&realtime_offset
,
586 -wall_to_monotonic
.tv_sec
,
587 -wall_to_monotonic
.tv_nsec
);
588 } while (read_seqretry(&xtime_lock
, seq
));
590 base
= &__get_cpu_var(hrtimer_bases
);
592 /* Adjust CLOCK_REALTIME offset */
593 spin_lock(&base
->lock
);
594 base
->clock_base
[CLOCK_REALTIME
].offset
=
595 timespec_to_ktime(realtime_offset
);
597 hrtimer_force_reprogram(base
);
598 spin_unlock(&base
->lock
);
602 * Clock realtime was set
604 * Change the offset of the realtime clock vs. the monotonic
607 * We might have to reprogram the high resolution timer interrupt. On
608 * SMP we call the architecture specific code to retrigger _all_ high
609 * resolution timer interrupts. On UP we just disable interrupts and
610 * call the high resolution interrupt code.
612 void clock_was_set(void)
614 /* Retrigger the CPU local events everywhere */
615 on_each_cpu(retrigger_next_event
, NULL
, 1);
619 * During resume we might have to reprogram the high resolution timer
620 * interrupt (on the local CPU):
622 void hres_timers_resume(void)
624 WARN_ONCE(!irqs_disabled(),
625 KERN_INFO
"hres_timers_resume() called with IRQs enabled!");
627 retrigger_next_event(NULL
);
631 * Initialize the high resolution related parts of cpu_base
633 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
)
635 base
->expires_next
.tv64
= KTIME_MAX
;
636 base
->hres_active
= 0;
640 * Initialize the high resolution related parts of a hrtimer
642 static inline void hrtimer_init_timer_hres(struct hrtimer
*timer
)
648 * When High resolution timers are active, try to reprogram. Note, that in case
649 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
650 * check happens. The timer gets enqueued into the rbtree. The reprogramming
651 * and expiry check is done in the hrtimer_interrupt or in the softirq.
653 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
654 struct hrtimer_clock_base
*base
,
657 if (base
->cpu_base
->hres_active
&& hrtimer_reprogram(timer
, base
)) {
659 spin_unlock(&base
->cpu_base
->lock
);
660 raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
661 spin_lock(&base
->cpu_base
->lock
);
663 __raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
672 * Switch to high resolution mode
674 static int hrtimer_switch_to_hres(void)
676 int cpu
= smp_processor_id();
677 struct hrtimer_cpu_base
*base
= &per_cpu(hrtimer_bases
, cpu
);
680 if (base
->hres_active
)
683 local_irq_save(flags
);
685 if (tick_init_highres()) {
686 local_irq_restore(flags
);
687 printk(KERN_WARNING
"Could not switch to high resolution "
688 "mode on CPU %d\n", cpu
);
691 base
->hres_active
= 1;
692 base
->clock_base
[CLOCK_REALTIME
].resolution
= KTIME_HIGH_RES
;
693 base
->clock_base
[CLOCK_MONOTONIC
].resolution
= KTIME_HIGH_RES
;
695 tick_setup_sched_timer();
697 /* "Retrigger" the interrupt to get things going */
698 retrigger_next_event(NULL
);
699 local_irq_restore(flags
);
700 printk(KERN_DEBUG
"Switched to high resolution mode on CPU %d\n",
707 static inline int hrtimer_hres_active(void) { return 0; }
708 static inline int hrtimer_is_hres_enabled(void) { return 0; }
709 static inline int hrtimer_switch_to_hres(void) { return 0; }
710 static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base
*base
) { }
711 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
712 struct hrtimer_clock_base
*base
,
717 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
) { }
718 static inline void hrtimer_init_timer_hres(struct hrtimer
*timer
) { }
720 #endif /* CONFIG_HIGH_RES_TIMERS */
722 #ifdef CONFIG_TIMER_STATS
723 void __timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
, void *addr
)
725 if (timer
->start_site
)
728 timer
->start_site
= addr
;
729 memcpy(timer
->start_comm
, current
->comm
, TASK_COMM_LEN
);
730 timer
->start_pid
= current
->pid
;
735 * Counterpart to lock_hrtimer_base above:
738 void unlock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
740 spin_unlock_irqrestore(&timer
->base
->cpu_base
->lock
, *flags
);
744 * hrtimer_forward - forward the timer expiry
745 * @timer: hrtimer to forward
746 * @now: forward past this time
747 * @interval: the interval to forward
749 * Forward the timer expiry so it will expire in the future.
750 * Returns the number of overruns.
752 u64
hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
)
757 delta
= ktime_sub(now
, hrtimer_get_expires(timer
));
762 if (interval
.tv64
< timer
->base
->resolution
.tv64
)
763 interval
.tv64
= timer
->base
->resolution
.tv64
;
765 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
766 s64 incr
= ktime_to_ns(interval
);
768 orun
= ktime_divns(delta
, incr
);
769 hrtimer_add_expires_ns(timer
, incr
* orun
);
770 if (hrtimer_get_expires_tv64(timer
) > now
.tv64
)
773 * This (and the ktime_add() below) is the
774 * correction for exact:
778 hrtimer_add_expires(timer
, interval
);
782 EXPORT_SYMBOL_GPL(hrtimer_forward
);
785 * enqueue_hrtimer - internal function to (re)start a timer
787 * The timer is inserted in expiry order. Insertion into the
788 * red black tree is O(log(n)). Must hold the base lock.
790 * Returns 1 when the new timer is the leftmost timer in the tree.
792 static int enqueue_hrtimer(struct hrtimer
*timer
,
793 struct hrtimer_clock_base
*base
)
795 struct rb_node
**link
= &base
->active
.rb_node
;
796 struct rb_node
*parent
= NULL
;
797 struct hrtimer
*entry
;
800 debug_hrtimer_activate(timer
);
803 * Find the right place in the rbtree:
807 entry
= rb_entry(parent
, struct hrtimer
, node
);
809 * We dont care about collisions. Nodes with
810 * the same expiry time stay together.
812 if (hrtimer_get_expires_tv64(timer
) <
813 hrtimer_get_expires_tv64(entry
)) {
814 link
= &(*link
)->rb_left
;
816 link
= &(*link
)->rb_right
;
822 * Insert the timer to the rbtree and check whether it
823 * replaces the first pending timer
826 base
->first
= &timer
->node
;
828 rb_link_node(&timer
->node
, parent
, link
);
829 rb_insert_color(&timer
->node
, &base
->active
);
831 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
832 * state of a possibly running callback.
834 timer
->state
|= HRTIMER_STATE_ENQUEUED
;
840 * __remove_hrtimer - internal function to remove a timer
842 * Caller must hold the base lock.
844 * High resolution timer mode reprograms the clock event device when the
845 * timer is the one which expires next. The caller can disable this by setting
846 * reprogram to zero. This is useful, when the context does a reprogramming
847 * anyway (e.g. timer interrupt)
849 static void __remove_hrtimer(struct hrtimer
*timer
,
850 struct hrtimer_clock_base
*base
,
851 unsigned long newstate
, int reprogram
)
853 if (timer
->state
& HRTIMER_STATE_ENQUEUED
) {
855 * Remove the timer from the rbtree and replace the
856 * first entry pointer if necessary.
858 if (base
->first
== &timer
->node
) {
859 base
->first
= rb_next(&timer
->node
);
860 /* Reprogram the clock event device. if enabled */
861 if (reprogram
&& hrtimer_hres_active())
862 hrtimer_force_reprogram(base
->cpu_base
);
864 rb_erase(&timer
->node
, &base
->active
);
866 timer
->state
= newstate
;
870 * remove hrtimer, called with base lock held
873 remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
)
875 if (hrtimer_is_queued(timer
)) {
879 * Remove the timer and force reprogramming when high
880 * resolution mode is active and the timer is on the current
881 * CPU. If we remove a timer on another CPU, reprogramming is
882 * skipped. The interrupt event on this CPU is fired and
883 * reprogramming happens in the interrupt handler. This is a
884 * rare case and less expensive than a smp call.
886 debug_hrtimer_deactivate(timer
);
887 timer_stats_hrtimer_clear_start_info(timer
);
888 reprogram
= base
->cpu_base
== &__get_cpu_var(hrtimer_bases
);
889 __remove_hrtimer(timer
, base
, HRTIMER_STATE_INACTIVE
,
896 int __hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
897 unsigned long delta_ns
, const enum hrtimer_mode mode
,
900 struct hrtimer_clock_base
*base
, *new_base
;
904 base
= lock_hrtimer_base(timer
, &flags
);
906 /* Remove an active timer from the queue: */
907 ret
= remove_hrtimer(timer
, base
);
909 /* Switch the timer base, if necessary: */
910 new_base
= switch_hrtimer_base(timer
, base
, mode
& HRTIMER_MODE_PINNED
);
912 if (mode
& HRTIMER_MODE_REL
) {
913 tim
= ktime_add_safe(tim
, new_base
->get_time());
915 * CONFIG_TIME_LOW_RES is a temporary way for architectures
916 * to signal that they simply return xtime in
917 * do_gettimeoffset(). In this case we want to round up by
918 * resolution when starting a relative timer, to avoid short
919 * timeouts. This will go away with the GTOD framework.
921 #ifdef CONFIG_TIME_LOW_RES
922 tim
= ktime_add_safe(tim
, base
->resolution
);
926 hrtimer_set_expires_range_ns(timer
, tim
, delta_ns
);
928 timer_stats_hrtimer_set_start_info(timer
);
930 leftmost
= enqueue_hrtimer(timer
, new_base
);
933 * Only allow reprogramming if the new base is on this CPU.
934 * (it might still be on another CPU if the timer was pending)
936 * XXX send_remote_softirq() ?
938 if (leftmost
&& new_base
->cpu_base
== &__get_cpu_var(hrtimer_bases
))
939 hrtimer_enqueue_reprogram(timer
, new_base
, wakeup
);
941 unlock_hrtimer_base(timer
, &flags
);
947 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
948 * @timer: the timer to be added
950 * @delta_ns: "slack" range for the timer
951 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
955 * 1 when the timer was active
957 int hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
958 unsigned long delta_ns
, const enum hrtimer_mode mode
)
960 return __hrtimer_start_range_ns(timer
, tim
, delta_ns
, mode
, 1);
962 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns
);
965 * hrtimer_start - (re)start an hrtimer on the current CPU
966 * @timer: the timer to be added
968 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
972 * 1 when the timer was active
975 hrtimer_start(struct hrtimer
*timer
, ktime_t tim
, const enum hrtimer_mode mode
)
977 return __hrtimer_start_range_ns(timer
, tim
, 0, mode
, 1);
979 EXPORT_SYMBOL_GPL(hrtimer_start
);
983 * hrtimer_try_to_cancel - try to deactivate a timer
984 * @timer: hrtimer to stop
987 * 0 when the timer was not active
988 * 1 when the timer was active
989 * -1 when the timer is currently excuting the callback function and
992 int hrtimer_try_to_cancel(struct hrtimer
*timer
)
994 struct hrtimer_clock_base
*base
;
998 base
= lock_hrtimer_base(timer
, &flags
);
1000 if (!hrtimer_callback_running(timer
))
1001 ret
= remove_hrtimer(timer
, base
);
1003 unlock_hrtimer_base(timer
, &flags
);
1008 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel
);
1011 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1012 * @timer: the timer to be cancelled
1015 * 0 when the timer was not active
1016 * 1 when the timer was active
1018 int hrtimer_cancel(struct hrtimer
*timer
)
1021 int ret
= hrtimer_try_to_cancel(timer
);
1028 EXPORT_SYMBOL_GPL(hrtimer_cancel
);
1031 * hrtimer_get_remaining - get remaining time for the timer
1032 * @timer: the timer to read
1034 ktime_t
hrtimer_get_remaining(const struct hrtimer
*timer
)
1036 struct hrtimer_clock_base
*base
;
1037 unsigned long flags
;
1040 base
= lock_hrtimer_base(timer
, &flags
);
1041 rem
= hrtimer_expires_remaining(timer
);
1042 unlock_hrtimer_base(timer
, &flags
);
1046 EXPORT_SYMBOL_GPL(hrtimer_get_remaining
);
1050 * hrtimer_get_next_event - get the time until next expiry event
1052 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1055 ktime_t
hrtimer_get_next_event(void)
1057 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1058 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
1059 ktime_t delta
, mindelta
= { .tv64
= KTIME_MAX
};
1060 unsigned long flags
;
1063 spin_lock_irqsave(&cpu_base
->lock
, flags
);
1065 if (!hrtimer_hres_active()) {
1066 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
1067 struct hrtimer
*timer
;
1072 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
1073 delta
.tv64
= hrtimer_get_expires_tv64(timer
);
1074 delta
= ktime_sub(delta
, base
->get_time());
1075 if (delta
.tv64
< mindelta
.tv64
)
1076 mindelta
.tv64
= delta
.tv64
;
1080 spin_unlock_irqrestore(&cpu_base
->lock
, flags
);
1082 if (mindelta
.tv64
< 0)
1088 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1089 enum hrtimer_mode mode
)
1091 struct hrtimer_cpu_base
*cpu_base
;
1093 memset(timer
, 0, sizeof(struct hrtimer
));
1095 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1097 if (clock_id
== CLOCK_REALTIME
&& mode
!= HRTIMER_MODE_ABS
)
1098 clock_id
= CLOCK_MONOTONIC
;
1100 timer
->base
= &cpu_base
->clock_base
[clock_id
];
1101 hrtimer_init_timer_hres(timer
);
1103 #ifdef CONFIG_TIMER_STATS
1104 timer
->start_site
= NULL
;
1105 timer
->start_pid
= -1;
1106 memset(timer
->start_comm
, 0, TASK_COMM_LEN
);
1111 * hrtimer_init - initialize a timer to the given clock
1112 * @timer: the timer to be initialized
1113 * @clock_id: the clock to be used
1114 * @mode: timer mode abs/rel
1116 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1117 enum hrtimer_mode mode
)
1119 debug_hrtimer_init(timer
);
1120 __hrtimer_init(timer
, clock_id
, mode
);
1122 EXPORT_SYMBOL_GPL(hrtimer_init
);
1125 * hrtimer_get_res - get the timer resolution for a clock
1126 * @which_clock: which clock to query
1127 * @tp: pointer to timespec variable to store the resolution
1129 * Store the resolution of the clock selected by @which_clock in the
1130 * variable pointed to by @tp.
1132 int hrtimer_get_res(const clockid_t which_clock
, struct timespec
*tp
)
1134 struct hrtimer_cpu_base
*cpu_base
;
1136 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1137 *tp
= ktime_to_timespec(cpu_base
->clock_base
[which_clock
].resolution
);
1141 EXPORT_SYMBOL_GPL(hrtimer_get_res
);
1143 static void __run_hrtimer(struct hrtimer
*timer
)
1145 struct hrtimer_clock_base
*base
= timer
->base
;
1146 struct hrtimer_cpu_base
*cpu_base
= base
->cpu_base
;
1147 enum hrtimer_restart (*fn
)(struct hrtimer
*);
1150 WARN_ON(!irqs_disabled());
1152 debug_hrtimer_deactivate(timer
);
1153 __remove_hrtimer(timer
, base
, HRTIMER_STATE_CALLBACK
, 0);
1154 timer_stats_account_hrtimer(timer
);
1155 fn
= timer
->function
;
1158 * Because we run timers from hardirq context, there is no chance
1159 * they get migrated to another cpu, therefore its safe to unlock
1162 spin_unlock(&cpu_base
->lock
);
1163 restart
= fn(timer
);
1164 spin_lock(&cpu_base
->lock
);
1167 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1168 * we do not reprogramm the event hardware. Happens either in
1169 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1171 if (restart
!= HRTIMER_NORESTART
) {
1172 BUG_ON(timer
->state
!= HRTIMER_STATE_CALLBACK
);
1173 enqueue_hrtimer(timer
, base
);
1175 timer
->state
&= ~HRTIMER_STATE_CALLBACK
;
1178 #ifdef CONFIG_HIGH_RES_TIMERS
1180 static int force_clock_reprogram
;
1183 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1184 * is hanging, which could happen with something that slows the interrupt
1185 * such as the tracing. Then we force the clock reprogramming for each future
1186 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1187 * threshold that we will overwrite.
1188 * The next tick event will be scheduled to 3 times we currently spend on
1189 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1190 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1191 * let it running without serious starvation.
1195 hrtimer_interrupt_hanging(struct clock_event_device
*dev
,
1198 force_clock_reprogram
= 1;
1199 dev
->min_delta_ns
= (unsigned long)try_time
.tv64
* 3;
1200 printk(KERN_WARNING
"hrtimer: interrupt too slow, "
1201 "forcing clock min delta to %lu ns\n", dev
->min_delta_ns
);
1204 * High resolution timer interrupt
1205 * Called with interrupts disabled
1207 void hrtimer_interrupt(struct clock_event_device
*dev
)
1209 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1210 struct hrtimer_clock_base
*base
;
1211 ktime_t expires_next
, now
;
1215 BUG_ON(!cpu_base
->hres_active
);
1216 cpu_base
->nr_events
++;
1217 dev
->next_event
.tv64
= KTIME_MAX
;
1220 /* 5 retries is enough to notice a hang */
1221 if (!(++nr_retries
% 5))
1222 hrtimer_interrupt_hanging(dev
, ktime_sub(ktime_get(), now
));
1226 expires_next
.tv64
= KTIME_MAX
;
1228 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 rb_node
*node
;
1244 basenow
= ktime_add(now
, base
->offset
);
1246 while ((node
= base
->first
)) {
1247 struct hrtimer
*timer
;
1249 timer
= rb_entry(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
);
1280 * Store the new expiry value so the migration code can verify
1283 cpu_base
->expires_next
= expires_next
;
1284 spin_unlock(&cpu_base
->lock
);
1286 /* Reprogramming necessary ? */
1287 if (expires_next
.tv64
!= KTIME_MAX
) {
1288 if (tick_program_event(expires_next
, force_clock_reprogram
))
1294 * local version of hrtimer_peek_ahead_timers() called with interrupts
1297 static void __hrtimer_peek_ahead_timers(void)
1299 struct tick_device
*td
;
1301 if (!hrtimer_hres_active())
1304 td
= &__get_cpu_var(tick_cpu_device
);
1305 if (td
&& td
->evtdev
)
1306 hrtimer_interrupt(td
->evtdev
);
1310 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1312 * hrtimer_peek_ahead_timers will peek at the timer queue of
1313 * the current cpu and check if there are any timers for which
1314 * the soft expires time has passed. If any such timers exist,
1315 * they are run immediately and then removed from the timer queue.
1318 void hrtimer_peek_ahead_timers(void)
1320 unsigned long flags
;
1322 local_irq_save(flags
);
1323 __hrtimer_peek_ahead_timers();
1324 local_irq_restore(flags
);
1327 static void run_hrtimer_softirq(struct softirq_action
*h
)
1329 hrtimer_peek_ahead_timers();
1332 #else /* CONFIG_HIGH_RES_TIMERS */
1334 static inline void __hrtimer_peek_ahead_timers(void) { }
1336 #endif /* !CONFIG_HIGH_RES_TIMERS */
1339 * Called from timer softirq every jiffy, expire hrtimers:
1341 * For HRT its the fall back code to run the softirq in the timer
1342 * softirq context in case the hrtimer initialization failed or has
1343 * not been done yet.
1345 void hrtimer_run_pending(void)
1347 if (hrtimer_hres_active())
1351 * This _is_ ugly: We have to check in the softirq context,
1352 * whether we can switch to highres and / or nohz mode. The
1353 * clocksource switch happens in the timer interrupt with
1354 * xtime_lock held. Notification from there only sets the
1355 * check bit in the tick_oneshot code, otherwise we might
1356 * deadlock vs. xtime_lock.
1358 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1359 hrtimer_switch_to_hres();
1363 * Called from hardirq context every jiffy
1365 void hrtimer_run_queues(void)
1367 struct rb_node
*node
;
1368 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1369 struct hrtimer_clock_base
*base
;
1370 int index
, gettime
= 1;
1372 if (hrtimer_hres_active())
1375 for (index
= 0; index
< HRTIMER_MAX_CLOCK_BASES
; index
++) {
1376 base
= &cpu_base
->clock_base
[index
];
1382 hrtimer_get_softirq_time(cpu_base
);
1386 spin_lock(&cpu_base
->lock
);
1388 while ((node
= base
->first
)) {
1389 struct hrtimer
*timer
;
1391 timer
= rb_entry(node
, struct hrtimer
, node
);
1392 if (base
->softirq_time
.tv64
<=
1393 hrtimer_get_expires_tv64(timer
))
1396 __run_hrtimer(timer
);
1398 spin_unlock(&cpu_base
->lock
);
1403 * Sleep related functions:
1405 static enum hrtimer_restart
hrtimer_wakeup(struct hrtimer
*timer
)
1407 struct hrtimer_sleeper
*t
=
1408 container_of(timer
, struct hrtimer_sleeper
, timer
);
1409 struct task_struct
*task
= t
->task
;
1413 wake_up_process(task
);
1415 return HRTIMER_NORESTART
;
1418 void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
, struct task_struct
*task
)
1420 sl
->timer
.function
= hrtimer_wakeup
;
1424 static int __sched
do_nanosleep(struct hrtimer_sleeper
*t
, enum hrtimer_mode mode
)
1426 hrtimer_init_sleeper(t
, current
);
1429 set_current_state(TASK_INTERRUPTIBLE
);
1430 hrtimer_start_expires(&t
->timer
, mode
);
1431 if (!hrtimer_active(&t
->timer
))
1434 if (likely(t
->task
))
1437 hrtimer_cancel(&t
->timer
);
1438 mode
= HRTIMER_MODE_ABS
;
1440 } while (t
->task
&& !signal_pending(current
));
1442 __set_current_state(TASK_RUNNING
);
1444 return t
->task
== NULL
;
1447 static int update_rmtp(struct hrtimer
*timer
, struct timespec __user
*rmtp
)
1449 struct timespec rmt
;
1452 rem
= hrtimer_expires_remaining(timer
);
1455 rmt
= ktime_to_timespec(rem
);
1457 if (copy_to_user(rmtp
, &rmt
, sizeof(*rmtp
)))
1463 long __sched
hrtimer_nanosleep_restart(struct restart_block
*restart
)
1465 struct hrtimer_sleeper t
;
1466 struct timespec __user
*rmtp
;
1469 hrtimer_init_on_stack(&t
.timer
, restart
->nanosleep
.index
,
1471 hrtimer_set_expires_tv64(&t
.timer
, restart
->nanosleep
.expires
);
1473 if (do_nanosleep(&t
, HRTIMER_MODE_ABS
))
1476 rmtp
= restart
->nanosleep
.rmtp
;
1478 ret
= update_rmtp(&t
.timer
, rmtp
);
1483 /* The other values in restart are already filled in */
1484 ret
= -ERESTART_RESTARTBLOCK
;
1486 destroy_hrtimer_on_stack(&t
.timer
);
1490 long hrtimer_nanosleep(struct timespec
*rqtp
, struct timespec __user
*rmtp
,
1491 const enum hrtimer_mode mode
, const clockid_t clockid
)
1493 struct restart_block
*restart
;
1494 struct hrtimer_sleeper t
;
1496 unsigned long slack
;
1498 slack
= current
->timer_slack_ns
;
1499 if (rt_task(current
))
1502 hrtimer_init_on_stack(&t
.timer
, clockid
, mode
);
1503 hrtimer_set_expires_range_ns(&t
.timer
, timespec_to_ktime(*rqtp
), slack
);
1504 if (do_nanosleep(&t
, mode
))
1507 /* Absolute timers do not update the rmtp value and restart: */
1508 if (mode
== HRTIMER_MODE_ABS
) {
1509 ret
= -ERESTARTNOHAND
;
1514 ret
= update_rmtp(&t
.timer
, rmtp
);
1519 restart
= ¤t_thread_info()->restart_block
;
1520 restart
->fn
= hrtimer_nanosleep_restart
;
1521 restart
->nanosleep
.index
= t
.timer
.base
->index
;
1522 restart
->nanosleep
.rmtp
= rmtp
;
1523 restart
->nanosleep
.expires
= hrtimer_get_expires_tv64(&t
.timer
);
1525 ret
= -ERESTART_RESTARTBLOCK
;
1527 destroy_hrtimer_on_stack(&t
.timer
);
1531 SYSCALL_DEFINE2(nanosleep
, struct timespec __user
*, rqtp
,
1532 struct timespec __user
*, rmtp
)
1536 if (copy_from_user(&tu
, rqtp
, sizeof(tu
)))
1539 if (!timespec_valid(&tu
))
1542 return hrtimer_nanosleep(&tu
, rmtp
, HRTIMER_MODE_REL
, CLOCK_MONOTONIC
);
1546 * Functions related to boot-time initialization:
1548 static void __cpuinit
init_hrtimers_cpu(int cpu
)
1550 struct hrtimer_cpu_base
*cpu_base
= &per_cpu(hrtimer_bases
, cpu
);
1553 spin_lock_init(&cpu_base
->lock
);
1555 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++)
1556 cpu_base
->clock_base
[i
].cpu_base
= cpu_base
;
1558 hrtimer_init_hres(cpu_base
);
1561 #ifdef CONFIG_HOTPLUG_CPU
1563 static void migrate_hrtimer_list(struct hrtimer_clock_base
*old_base
,
1564 struct hrtimer_clock_base
*new_base
)
1566 struct hrtimer
*timer
;
1567 struct rb_node
*node
;
1569 while ((node
= rb_first(&old_base
->active
))) {
1570 timer
= rb_entry(node
, struct hrtimer
, node
);
1571 BUG_ON(hrtimer_callback_running(timer
));
1572 debug_hrtimer_deactivate(timer
);
1575 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1576 * timer could be seen as !active and just vanish away
1577 * under us on another CPU
1579 __remove_hrtimer(timer
, old_base
, HRTIMER_STATE_MIGRATE
, 0);
1580 timer
->base
= new_base
;
1582 * Enqueue the timers on the new cpu. This does not
1583 * reprogram the event device in case the timer
1584 * expires before the earliest on this CPU, but we run
1585 * hrtimer_interrupt after we migrated everything to
1586 * sort out already expired timers and reprogram the
1589 enqueue_hrtimer(timer
, new_base
);
1591 /* Clear the migration state bit */
1592 timer
->state
&= ~HRTIMER_STATE_MIGRATE
;
1596 static void migrate_hrtimers(int scpu
)
1598 struct hrtimer_cpu_base
*old_base
, *new_base
;
1601 BUG_ON(cpu_online(scpu
));
1602 tick_cancel_sched_timer(scpu
);
1604 local_irq_disable();
1605 old_base
= &per_cpu(hrtimer_bases
, scpu
);
1606 new_base
= &__get_cpu_var(hrtimer_bases
);
1608 * The caller is globally serialized and nobody else
1609 * takes two locks at once, deadlock is not possible.
1611 spin_lock(&new_base
->lock
);
1612 spin_lock_nested(&old_base
->lock
, SINGLE_DEPTH_NESTING
);
1614 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1615 migrate_hrtimer_list(&old_base
->clock_base
[i
],
1616 &new_base
->clock_base
[i
]);
1619 spin_unlock(&old_base
->lock
);
1620 spin_unlock(&new_base
->lock
);
1622 /* Check, if we got expired work to do */
1623 __hrtimer_peek_ahead_timers();
1627 #endif /* CONFIG_HOTPLUG_CPU */
1629 static int __cpuinit
hrtimer_cpu_notify(struct notifier_block
*self
,
1630 unsigned long action
, void *hcpu
)
1632 int scpu
= (long)hcpu
;
1636 case CPU_UP_PREPARE
:
1637 case CPU_UP_PREPARE_FROZEN
:
1638 init_hrtimers_cpu(scpu
);
1641 #ifdef CONFIG_HOTPLUG_CPU
1643 case CPU_DYING_FROZEN
:
1644 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING
, &scpu
);
1647 case CPU_DEAD_FROZEN
:
1649 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD
, &scpu
);
1650 migrate_hrtimers(scpu
);
1662 static struct notifier_block __cpuinitdata hrtimers_nb
= {
1663 .notifier_call
= hrtimer_cpu_notify
,
1666 void __init
hrtimers_init(void)
1668 hrtimer_cpu_notify(&hrtimers_nb
, (unsigned long)CPU_UP_PREPARE
,
1669 (void *)(long)smp_processor_id());
1670 register_cpu_notifier(&hrtimers_nb
);
1671 #ifdef CONFIG_HIGH_RES_TIMERS
1672 open_softirq(HRTIMER_SOFTIRQ
, run_hrtimer_softirq
);
1677 * schedule_hrtimeout_range - sleep until timeout
1678 * @expires: timeout value (ktime_t)
1679 * @delta: slack in expires timeout (ktime_t)
1680 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1682 * Make the current task sleep until the given expiry time has
1683 * elapsed. The routine will return immediately unless
1684 * the current task state has been set (see set_current_state()).
1686 * The @delta argument gives the kernel the freedom to schedule the
1687 * actual wakeup to a time that is both power and performance friendly.
1688 * The kernel give the normal best effort behavior for "@expires+@delta",
1689 * but may decide to fire the timer earlier, but no earlier than @expires.
1691 * You can set the task state as follows -
1693 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1694 * pass before the routine returns.
1696 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1697 * delivered to the current task.
1699 * The current task state is guaranteed to be TASK_RUNNING when this
1702 * Returns 0 when the timer has expired otherwise -EINTR
1704 int __sched
schedule_hrtimeout_range(ktime_t
*expires
, unsigned long delta
,
1705 const enum hrtimer_mode mode
)
1707 struct hrtimer_sleeper t
;
1710 * Optimize when a zero timeout value is given. It does not
1711 * matter whether this is an absolute or a relative time.
1713 if (expires
&& !expires
->tv64
) {
1714 __set_current_state(TASK_RUNNING
);
1719 * A NULL parameter means "inifinte"
1723 __set_current_state(TASK_RUNNING
);
1727 hrtimer_init_on_stack(&t
.timer
, CLOCK_MONOTONIC
, mode
);
1728 hrtimer_set_expires_range_ns(&t
.timer
, *expires
, delta
);
1730 hrtimer_init_sleeper(&t
, current
);
1732 hrtimer_start_expires(&t
.timer
, mode
);
1733 if (!hrtimer_active(&t
.timer
))
1739 hrtimer_cancel(&t
.timer
);
1740 destroy_hrtimer_on_stack(&t
.timer
);
1742 __set_current_state(TASK_RUNNING
);
1744 return !t
.task
? 0 : -EINTR
;
1746 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range
);
1749 * schedule_hrtimeout - sleep until timeout
1750 * @expires: timeout value (ktime_t)
1751 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1753 * Make the current task sleep until the given expiry time has
1754 * elapsed. The routine will return immediately unless
1755 * the current task state has been set (see set_current_state()).
1757 * You can set the task state as follows -
1759 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1760 * pass before the routine returns.
1762 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1763 * delivered to the current task.
1765 * The current task state is guaranteed to be TASK_RUNNING when this
1768 * Returns 0 when the timer has expired otherwise -EINTR
1770 int __sched
schedule_hrtimeout(ktime_t
*expires
,
1771 const enum hrtimer_mode mode
)
1773 return schedule_hrtimeout_range(expires
, 0, mode
);
1775 EXPORT_SYMBOL_GPL(schedule_hrtimeout
);