2 * linux/kernel/hrtimer.c
4 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
7 * High-resolution kernel timers
9 * In contrast to the low-resolution timeout API implemented in
10 * kernel/timer.c, hrtimers provide finer resolution and accuracy
11 * depending on system configuration and capabilities.
13 * These timers are currently used for:
17 * - precise in-kernel timing
19 * Started by: Thomas Gleixner and Ingo Molnar
22 * based on kernel/timer.c
24 * Help, testing, suggestions, bugfixes, improvements were
27 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
30 * For licencing details see kernel-base/COPYING
33 #include <linux/cpu.h>
34 #include <linux/module.h>
35 #include <linux/percpu.h>
36 #include <linux/hrtimer.h>
37 #include <linux/notifier.h>
38 #include <linux/syscalls.h>
39 #include <linux/interrupt.h>
41 #include <asm/uaccess.h>
44 * ktime_get - get the monotonic time in ktime_t format
46 * returns the time in ktime_t format
48 static ktime_t
ktime_get(void)
54 return timespec_to_ktime(now
);
58 * ktime_get_real - get the real (wall-) time in ktime_t format
60 * returns the time in ktime_t format
62 static ktime_t
ktime_get_real(void)
68 return timespec_to_ktime(now
);
71 EXPORT_SYMBOL_GPL(ktime_get_real
);
76 * Note: If we want to add new timer bases, we have to skip the two
77 * clock ids captured by the cpu-timers. We do this by holding empty
78 * entries rather than doing math adjustment of the clock ids.
79 * This ensures that we capture erroneous accesses to these clock ids
80 * rather than moving them into the range of valid clock id's.
83 #define MAX_HRTIMER_BASES 2
85 static DEFINE_PER_CPU(struct hrtimer_base
, hrtimer_bases
[MAX_HRTIMER_BASES
]) =
88 .index
= CLOCK_REALTIME
,
89 .get_time
= &ktime_get_real
,
90 .resolution
= KTIME_REALTIME_RES
,
93 .index
= CLOCK_MONOTONIC
,
94 .get_time
= &ktime_get
,
95 .resolution
= KTIME_MONOTONIC_RES
,
100 * ktime_get_ts - get the monotonic clock in timespec format
102 * @ts: pointer to timespec variable
104 * The function calculates the monotonic clock from the realtime
105 * clock and the wall_to_monotonic offset and stores the result
106 * in normalized timespec format in the variable pointed to by ts.
108 void ktime_get_ts(struct timespec
*ts
)
110 struct timespec tomono
;
114 seq
= read_seqbegin(&xtime_lock
);
116 tomono
= wall_to_monotonic
;
118 } while (read_seqretry(&xtime_lock
, seq
));
120 set_normalized_timespec(ts
, ts
->tv_sec
+ tomono
.tv_sec
,
121 ts
->tv_nsec
+ tomono
.tv_nsec
);
123 EXPORT_SYMBOL_GPL(ktime_get_ts
);
126 * Get the coarse grained time at the softirq based on xtime and
129 static void hrtimer_get_softirq_time(struct hrtimer_base
*base
)
131 ktime_t xtim
, tomono
;
135 seq
= read_seqbegin(&xtime_lock
);
136 xtim
= timespec_to_ktime(xtime
);
137 tomono
= timespec_to_ktime(wall_to_monotonic
);
139 } while (read_seqretry(&xtime_lock
, seq
));
141 base
[CLOCK_REALTIME
].softirq_time
= xtim
;
142 base
[CLOCK_MONOTONIC
].softirq_time
= ktime_add(xtim
, tomono
);
146 * Functions and macros which are different for UP/SMP systems are kept in a
151 #define set_curr_timer(b, t) do { (b)->curr_timer = (t); } while (0)
154 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
155 * means that all timers which are tied to this base via timer->base are
156 * locked, and the base itself is locked too.
158 * So __run_timers/migrate_timers can safely modify all timers which could
159 * be found on the lists/queues.
161 * When the timer's base is locked, and the timer removed from list, it is
162 * possible to set timer->base = NULL and drop the lock: the timer remains
165 static struct hrtimer_base
*lock_hrtimer_base(const struct hrtimer
*timer
,
166 unsigned long *flags
)
168 struct hrtimer_base
*base
;
172 if (likely(base
!= NULL
)) {
173 spin_lock_irqsave(&base
->lock
, *flags
);
174 if (likely(base
== timer
->base
))
176 /* The timer has migrated to another CPU: */
177 spin_unlock_irqrestore(&base
->lock
, *flags
);
184 * Switch the timer base to the current CPU when possible.
186 static inline struct hrtimer_base
*
187 switch_hrtimer_base(struct hrtimer
*timer
, struct hrtimer_base
*base
)
189 struct hrtimer_base
*new_base
;
191 new_base
= &__get_cpu_var(hrtimer_bases
[base
->index
]);
193 if (base
!= new_base
) {
195 * We are trying to schedule the timer on the local CPU.
196 * However we can't change timer's base while it is running,
197 * so we keep it on the same CPU. No hassle vs. reprogramming
198 * the event source in the high resolution case. The softirq
199 * code will take care of this when the timer function has
200 * completed. There is no conflict as we hold the lock until
201 * the timer is enqueued.
203 if (unlikely(base
->curr_timer
== timer
))
206 /* See the comment in lock_timer_base() */
208 spin_unlock(&base
->lock
);
209 spin_lock(&new_base
->lock
);
210 timer
->base
= new_base
;
215 #else /* CONFIG_SMP */
217 #define set_curr_timer(b, t) do { } while (0)
219 static inline struct hrtimer_base
*
220 lock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
222 struct hrtimer_base
*base
= timer
->base
;
224 spin_lock_irqsave(&base
->lock
, *flags
);
229 #define switch_hrtimer_base(t, b) (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
243 * @nsec: the scalar nsec value to add
245 * Returns the sum of kt and nsec in ktime_t format
247 ktime_t
ktime_add_ns(const ktime_t kt
, u64 nsec
)
251 if (likely(nsec
< NSEC_PER_SEC
)) {
254 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
256 tmp
= ktime_set((long)nsec
, rem
);
259 return ktime_add(kt
, tmp
);
262 #else /* CONFIG_KTIME_SCALAR */
264 # endif /* !CONFIG_KTIME_SCALAR */
267 * Divide a ktime value by a nanosecond value
269 static unsigned long ktime_divns(const ktime_t kt
, s64 div
)
274 dclc
= dns
= ktime_to_ns(kt
);
276 /* Make sure the divisor is less than 2^32: */
282 do_div(dclc
, (unsigned long) div
);
284 return (unsigned long) dclc
;
287 #else /* BITS_PER_LONG < 64 */
288 # define ktime_divns(kt, div) (unsigned long)((kt).tv64 / (div))
289 #endif /* BITS_PER_LONG >= 64 */
292 * Counterpart to lock_timer_base above:
295 void unlock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
297 spin_unlock_irqrestore(&timer
->base
->lock
, *flags
);
301 * hrtimer_forward - forward the timer expiry
303 * @timer: hrtimer to forward
304 * @now: forward past this time
305 * @interval: the interval to forward
307 * Forward the timer expiry so it will expire in the future.
308 * Returns the number of overruns.
311 hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
)
313 unsigned long orun
= 1;
316 delta
= ktime_sub(now
, timer
->expires
);
321 if (interval
.tv64
< timer
->base
->resolution
.tv64
)
322 interval
.tv64
= timer
->base
->resolution
.tv64
;
324 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
325 s64 incr
= ktime_to_ns(interval
);
327 orun
= ktime_divns(delta
, incr
);
328 timer
->expires
= ktime_add_ns(timer
->expires
, incr
* orun
);
329 if (timer
->expires
.tv64
> now
.tv64
)
332 * This (and the ktime_add() below) is the
333 * correction for exact:
337 timer
->expires
= ktime_add(timer
->expires
, interval
);
343 * enqueue_hrtimer - internal function to (re)start a timer
345 * The timer is inserted in expiry order. Insertion into the
346 * red black tree is O(log(n)). Must hold the base lock.
348 static void enqueue_hrtimer(struct hrtimer
*timer
, struct hrtimer_base
*base
)
350 struct rb_node
**link
= &base
->active
.rb_node
;
351 struct rb_node
*parent
= NULL
;
352 struct hrtimer
*entry
;
355 * Find the right place in the rbtree:
359 entry
= rb_entry(parent
, struct hrtimer
, node
);
361 * We dont care about collisions. Nodes with
362 * the same expiry time stay together.
364 if (timer
->expires
.tv64
< entry
->expires
.tv64
)
365 link
= &(*link
)->rb_left
;
367 link
= &(*link
)->rb_right
;
371 * Insert the timer to the rbtree and check whether it
372 * replaces the first pending timer
374 rb_link_node(&timer
->node
, parent
, link
);
375 rb_insert_color(&timer
->node
, &base
->active
);
377 if (!base
->first
|| timer
->expires
.tv64
<
378 rb_entry(base
->first
, struct hrtimer
, node
)->expires
.tv64
)
379 base
->first
= &timer
->node
;
383 * __remove_hrtimer - internal function to remove a timer
385 * Caller must hold the base lock.
387 static void __remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_base
*base
)
390 * Remove the timer from the rbtree and replace the
391 * first entry pointer if necessary.
393 if (base
->first
== &timer
->node
)
394 base
->first
= rb_next(&timer
->node
);
395 rb_erase(&timer
->node
, &base
->active
);
396 timer
->node
.rb_parent
= HRTIMER_INACTIVE
;
400 * remove hrtimer, called with base lock held
403 remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_base
*base
)
405 if (hrtimer_active(timer
)) {
406 __remove_hrtimer(timer
, base
);
413 * hrtimer_start - (re)start an relative timer on the current CPU
415 * @timer: the timer to be added
417 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
421 * 1 when the timer was active
424 hrtimer_start(struct hrtimer
*timer
, ktime_t tim
, const enum hrtimer_mode mode
)
426 struct hrtimer_base
*base
, *new_base
;
430 base
= lock_hrtimer_base(timer
, &flags
);
432 /* Remove an active timer from the queue: */
433 ret
= remove_hrtimer(timer
, base
);
435 /* Switch the timer base, if necessary: */
436 new_base
= switch_hrtimer_base(timer
, base
);
438 if (mode
== HRTIMER_REL
) {
439 tim
= ktime_add(tim
, new_base
->get_time());
441 * CONFIG_TIME_LOW_RES is a temporary way for architectures
442 * to signal that they simply return xtime in
443 * do_gettimeoffset(). In this case we want to round up by
444 * resolution when starting a relative timer, to avoid short
445 * timeouts. This will go away with the GTOD framework.
447 #ifdef CONFIG_TIME_LOW_RES
448 tim
= ktime_add(tim
, base
->resolution
);
451 timer
->expires
= tim
;
453 enqueue_hrtimer(timer
, new_base
);
455 unlock_hrtimer_base(timer
, &flags
);
461 * hrtimer_try_to_cancel - try to deactivate a timer
463 * @timer: hrtimer to stop
466 * 0 when the timer was not active
467 * 1 when the timer was active
468 * -1 when the timer is currently excuting the callback function and
471 int hrtimer_try_to_cancel(struct hrtimer
*timer
)
473 struct hrtimer_base
*base
;
477 base
= lock_hrtimer_base(timer
, &flags
);
479 if (base
->curr_timer
!= timer
)
480 ret
= remove_hrtimer(timer
, base
);
482 unlock_hrtimer_base(timer
, &flags
);
489 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
491 * @timer: the timer to be cancelled
494 * 0 when the timer was not active
495 * 1 when the timer was active
497 int hrtimer_cancel(struct hrtimer
*timer
)
500 int ret
= hrtimer_try_to_cancel(timer
);
509 * hrtimer_get_remaining - get remaining time for the timer
511 * @timer: the timer to read
513 ktime_t
hrtimer_get_remaining(const struct hrtimer
*timer
)
515 struct hrtimer_base
*base
;
519 base
= lock_hrtimer_base(timer
, &flags
);
520 rem
= ktime_sub(timer
->expires
, timer
->base
->get_time());
521 unlock_hrtimer_base(timer
, &flags
);
526 #ifdef CONFIG_NO_IDLE_HZ
528 * hrtimer_get_next_event - get the time until next expiry event
530 * Returns the delta to the next expiry event or KTIME_MAX if no timer
533 ktime_t
hrtimer_get_next_event(void)
535 struct hrtimer_base
*base
= __get_cpu_var(hrtimer_bases
);
536 ktime_t delta
, mindelta
= { .tv64
= KTIME_MAX
};
540 for (i
= 0; i
< MAX_HRTIMER_BASES
; i
++, base
++) {
541 struct hrtimer
*timer
;
543 spin_lock_irqsave(&base
->lock
, flags
);
545 spin_unlock_irqrestore(&base
->lock
, flags
);
548 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
549 delta
.tv64
= timer
->expires
.tv64
;
550 spin_unlock_irqrestore(&base
->lock
, flags
);
551 delta
= ktime_sub(delta
, base
->get_time());
552 if (delta
.tv64
< mindelta
.tv64
)
553 mindelta
.tv64
= delta
.tv64
;
555 if (mindelta
.tv64
< 0)
562 * hrtimer_init - initialize a timer to the given clock
564 * @timer: the timer to be initialized
565 * @clock_id: the clock to be used
566 * @mode: timer mode abs/rel
568 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
569 enum hrtimer_mode mode
)
571 struct hrtimer_base
*bases
;
573 memset(timer
, 0, sizeof(struct hrtimer
));
575 bases
= per_cpu(hrtimer_bases
, raw_smp_processor_id());
577 if (clock_id
== CLOCK_REALTIME
&& mode
!= HRTIMER_ABS
)
578 clock_id
= CLOCK_MONOTONIC
;
580 timer
->base
= &bases
[clock_id
];
581 timer
->node
.rb_parent
= HRTIMER_INACTIVE
;
585 * hrtimer_get_res - get the timer resolution for a clock
587 * @which_clock: which clock to query
588 * @tp: pointer to timespec variable to store the resolution
590 * Store the resolution of the clock selected by which_clock in the
591 * variable pointed to by tp.
593 int hrtimer_get_res(const clockid_t which_clock
, struct timespec
*tp
)
595 struct hrtimer_base
*bases
;
597 bases
= per_cpu(hrtimer_bases
, raw_smp_processor_id());
598 *tp
= ktime_to_timespec(bases
[which_clock
].resolution
);
604 * Expire the per base hrtimer-queue:
606 static inline void run_hrtimer_queue(struct hrtimer_base
*base
)
608 struct rb_node
*node
;
613 if (base
->get_softirq_time
)
614 base
->softirq_time
= base
->get_softirq_time();
616 spin_lock_irq(&base
->lock
);
618 while ((node
= base
->first
)) {
619 struct hrtimer
*timer
;
620 int (*fn
)(struct hrtimer
*);
623 timer
= rb_entry(node
, struct hrtimer
, node
);
624 if (base
->softirq_time
.tv64
<= timer
->expires
.tv64
)
627 fn
= timer
->function
;
628 set_curr_timer(base
, timer
);
629 __remove_hrtimer(timer
, base
);
630 spin_unlock_irq(&base
->lock
);
634 spin_lock_irq(&base
->lock
);
636 if (restart
!= HRTIMER_NORESTART
) {
637 BUG_ON(hrtimer_active(timer
));
638 enqueue_hrtimer(timer
, base
);
641 set_curr_timer(base
, NULL
);
642 spin_unlock_irq(&base
->lock
);
646 * Called from timer softirq every jiffy, expire hrtimers:
648 void hrtimer_run_queues(void)
650 struct hrtimer_base
*base
= __get_cpu_var(hrtimer_bases
);
653 hrtimer_get_softirq_time(base
);
655 for (i
= 0; i
< MAX_HRTIMER_BASES
; i
++)
656 run_hrtimer_queue(&base
[i
]);
660 * Sleep related functions:
662 static int hrtimer_wakeup(struct hrtimer
*timer
)
664 struct hrtimer_sleeper
*t
=
665 container_of(timer
, struct hrtimer_sleeper
, timer
);
666 struct task_struct
*task
= t
->task
;
670 wake_up_process(task
);
672 return HRTIMER_NORESTART
;
675 void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
, task_t
*task
)
677 sl
->timer
.function
= hrtimer_wakeup
;
681 static int __sched
do_nanosleep(struct hrtimer_sleeper
*t
, enum hrtimer_mode mode
)
683 hrtimer_init_sleeper(t
, current
);
686 set_current_state(TASK_INTERRUPTIBLE
);
687 hrtimer_start(&t
->timer
, t
->timer
.expires
, mode
);
691 hrtimer_cancel(&t
->timer
);
694 } while (t
->task
&& !signal_pending(current
));
696 return t
->task
== NULL
;
699 static long __sched
nanosleep_restart(struct restart_block
*restart
)
701 struct hrtimer_sleeper t
;
702 struct timespec __user
*rmtp
;
706 restart
->fn
= do_no_restart_syscall
;
708 hrtimer_init(&t
.timer
, restart
->arg3
, HRTIMER_ABS
);
709 t
.timer
.expires
.tv64
= ((u64
)restart
->arg1
<< 32) | (u64
) restart
->arg0
;
711 if (do_nanosleep(&t
, HRTIMER_ABS
))
714 rmtp
= (struct timespec __user
*) restart
->arg2
;
716 time
= ktime_sub(t
.timer
.expires
, t
.timer
.base
->get_time());
719 tu
= ktime_to_timespec(time
);
720 if (copy_to_user(rmtp
, &tu
, sizeof(tu
)))
724 restart
->fn
= nanosleep_restart
;
726 /* The other values in restart are already filled in */
727 return -ERESTART_RESTARTBLOCK
;
730 long hrtimer_nanosleep(struct timespec
*rqtp
, struct timespec __user
*rmtp
,
731 const enum hrtimer_mode mode
, const clockid_t clockid
)
733 struct restart_block
*restart
;
734 struct hrtimer_sleeper t
;
738 hrtimer_init(&t
.timer
, clockid
, mode
);
739 t
.timer
.expires
= timespec_to_ktime(*rqtp
);
740 if (do_nanosleep(&t
, mode
))
743 /* Absolute timers do not update the rmtp value and restart: */
744 if (mode
== HRTIMER_ABS
)
745 return -ERESTARTNOHAND
;
748 rem
= ktime_sub(t
.timer
.expires
, t
.timer
.base
->get_time());
751 tu
= ktime_to_timespec(rem
);
752 if (copy_to_user(rmtp
, &tu
, sizeof(tu
)))
756 restart
= ¤t_thread_info()->restart_block
;
757 restart
->fn
= nanosleep_restart
;
758 restart
->arg0
= t
.timer
.expires
.tv64
& 0xFFFFFFFF;
759 restart
->arg1
= t
.timer
.expires
.tv64
>> 32;
760 restart
->arg2
= (unsigned long) rmtp
;
761 restart
->arg3
= (unsigned long) t
.timer
.base
->index
;
763 return -ERESTART_RESTARTBLOCK
;
767 sys_nanosleep(struct timespec __user
*rqtp
, struct timespec __user
*rmtp
)
771 if (copy_from_user(&tu
, rqtp
, sizeof(tu
)))
774 if (!timespec_valid(&tu
))
777 return hrtimer_nanosleep(&tu
, rmtp
, HRTIMER_REL
, CLOCK_MONOTONIC
);
781 * Functions related to boot-time initialization:
783 static void __devinit
init_hrtimers_cpu(int cpu
)
785 struct hrtimer_base
*base
= per_cpu(hrtimer_bases
, cpu
);
788 for (i
= 0; i
< MAX_HRTIMER_BASES
; i
++, base
++)
789 spin_lock_init(&base
->lock
);
792 #ifdef CONFIG_HOTPLUG_CPU
794 static void migrate_hrtimer_list(struct hrtimer_base
*old_base
,
795 struct hrtimer_base
*new_base
)
797 struct hrtimer
*timer
;
798 struct rb_node
*node
;
800 while ((node
= rb_first(&old_base
->active
))) {
801 timer
= rb_entry(node
, struct hrtimer
, node
);
802 __remove_hrtimer(timer
, old_base
);
803 timer
->base
= new_base
;
804 enqueue_hrtimer(timer
, new_base
);
808 static void migrate_hrtimers(int cpu
)
810 struct hrtimer_base
*old_base
, *new_base
;
813 BUG_ON(cpu_online(cpu
));
814 old_base
= per_cpu(hrtimer_bases
, cpu
);
815 new_base
= get_cpu_var(hrtimer_bases
);
819 for (i
= 0; i
< MAX_HRTIMER_BASES
; i
++) {
821 spin_lock(&new_base
->lock
);
822 spin_lock(&old_base
->lock
);
824 BUG_ON(old_base
->curr_timer
);
826 migrate_hrtimer_list(old_base
, new_base
);
828 spin_unlock(&old_base
->lock
);
829 spin_unlock(&new_base
->lock
);
835 put_cpu_var(hrtimer_bases
);
837 #endif /* CONFIG_HOTPLUG_CPU */
839 static int __devinit
hrtimer_cpu_notify(struct notifier_block
*self
,
840 unsigned long action
, void *hcpu
)
842 long cpu
= (long)hcpu
;
847 init_hrtimers_cpu(cpu
);
850 #ifdef CONFIG_HOTPLUG_CPU
852 migrate_hrtimers(cpu
);
863 static struct notifier_block __devinitdata hrtimers_nb
= {
864 .notifier_call
= hrtimer_cpu_notify
,
867 void __init
hrtimers_init(void)
869 hrtimer_cpu_notify(&hrtimers_nb
, (unsigned long)CPU_UP_PREPARE
,
870 (void *)(long)smp_processor_id());
871 register_cpu_notifier(&hrtimers_nb
);