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
101 * @ts: pointer to timespec variable
103 * The function calculates the monotonic clock from the realtime
104 * clock and the wall_to_monotonic offset and stores the result
105 * in normalized timespec format in the variable pointed to by @ts.
107 void ktime_get_ts(struct timespec
*ts
)
109 struct timespec tomono
;
113 seq
= read_seqbegin(&xtime_lock
);
115 tomono
= wall_to_monotonic
;
117 } while (read_seqretry(&xtime_lock
, seq
));
119 set_normalized_timespec(ts
, ts
->tv_sec
+ tomono
.tv_sec
,
120 ts
->tv_nsec
+ tomono
.tv_nsec
);
122 EXPORT_SYMBOL_GPL(ktime_get_ts
);
125 * Get the coarse grained time at the softirq based on xtime and
128 static void hrtimer_get_softirq_time(struct hrtimer_base
*base
)
130 ktime_t xtim
, tomono
;
135 seq
= read_seqbegin(&xtime_lock
);
137 getnstimeofday(&xts
);
141 } while (read_seqretry(&xtime_lock
, seq
));
143 xtim
= timespec_to_ktime(xts
);
144 tomono
= timespec_to_ktime(wall_to_monotonic
);
145 base
[CLOCK_REALTIME
].softirq_time
= xtim
;
146 base
[CLOCK_MONOTONIC
].softirq_time
= ktime_add(xtim
, tomono
);
150 * Functions and macros which are different for UP/SMP systems are kept in a
155 #define set_curr_timer(b, t) do { (b)->curr_timer = (t); } while (0)
158 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
159 * means that all timers which are tied to this base via timer->base are
160 * locked, and the base itself is locked too.
162 * So __run_timers/migrate_timers can safely modify all timers which could
163 * be found on the lists/queues.
165 * When the timer's base is locked, and the timer removed from list, it is
166 * possible to set timer->base = NULL and drop the lock: the timer remains
169 static struct hrtimer_base
*lock_hrtimer_base(const struct hrtimer
*timer
,
170 unsigned long *flags
)
172 struct hrtimer_base
*base
;
176 if (likely(base
!= NULL
)) {
177 spin_lock_irqsave(&base
->lock
, *flags
);
178 if (likely(base
== timer
->base
))
180 /* The timer has migrated to another CPU: */
181 spin_unlock_irqrestore(&base
->lock
, *flags
);
188 * Switch the timer base to the current CPU when possible.
190 static inline struct hrtimer_base
*
191 switch_hrtimer_base(struct hrtimer
*timer
, struct hrtimer_base
*base
)
193 struct hrtimer_base
*new_base
;
195 new_base
= &__get_cpu_var(hrtimer_bases
)[base
->index
];
197 if (base
!= new_base
) {
199 * We are trying to schedule the timer on the local CPU.
200 * However we can't change timer's base while it is running,
201 * so we keep it on the same CPU. No hassle vs. reprogramming
202 * the event source in the high resolution case. The softirq
203 * code will take care of this when the timer function has
204 * completed. There is no conflict as we hold the lock until
205 * the timer is enqueued.
207 if (unlikely(base
->curr_timer
== timer
))
210 /* See the comment in lock_timer_base() */
212 spin_unlock(&base
->lock
);
213 spin_lock(&new_base
->lock
);
214 timer
->base
= new_base
;
219 #else /* CONFIG_SMP */
221 #define set_curr_timer(b, t) do { } while (0)
223 static inline struct hrtimer_base
*
224 lock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
226 struct hrtimer_base
*base
= timer
->base
;
228 spin_lock_irqsave(&base
->lock
, *flags
);
233 #define switch_hrtimer_base(t, b) (b)
235 #endif /* !CONFIG_SMP */
238 * Functions for the union type storage format of ktime_t which are
239 * too large for inlining:
241 #if BITS_PER_LONG < 64
242 # ifndef CONFIG_KTIME_SCALAR
244 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
246 * @nsec: the scalar nsec value to add
248 * Returns the sum of kt and nsec in ktime_t format
250 ktime_t
ktime_add_ns(const ktime_t kt
, u64 nsec
)
254 if (likely(nsec
< NSEC_PER_SEC
)) {
257 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
259 tmp
= ktime_set((long)nsec
, rem
);
262 return ktime_add(kt
, tmp
);
265 #else /* CONFIG_KTIME_SCALAR */
267 # endif /* !CONFIG_KTIME_SCALAR */
270 * Divide a ktime value by a nanosecond value
272 static unsigned long ktime_divns(const ktime_t kt
, s64 div
)
277 dclc
= dns
= ktime_to_ns(kt
);
279 /* Make sure the divisor is less than 2^32: */
285 do_div(dclc
, (unsigned long) div
);
287 return (unsigned long) dclc
;
290 #else /* BITS_PER_LONG < 64 */
291 # define ktime_divns(kt, div) (unsigned long)((kt).tv64 / (div))
292 #endif /* BITS_PER_LONG >= 64 */
295 * Timekeeping resumed notification
297 void hrtimer_notify_resume(void)
303 * Counterpart to lock_timer_base above:
306 void unlock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
308 spin_unlock_irqrestore(&timer
->base
->lock
, *flags
);
312 * hrtimer_forward - forward the timer expiry
313 * @timer: hrtimer to forward
314 * @now: forward past this time
315 * @interval: the interval to forward
317 * Forward the timer expiry so it will expire in the future.
318 * Returns the number of overruns.
321 hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
)
323 unsigned long orun
= 1;
326 delta
= ktime_sub(now
, timer
->expires
);
331 if (interval
.tv64
< timer
->base
->resolution
.tv64
)
332 interval
.tv64
= timer
->base
->resolution
.tv64
;
334 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
335 s64 incr
= ktime_to_ns(interval
);
337 orun
= ktime_divns(delta
, incr
);
338 timer
->expires
= ktime_add_ns(timer
->expires
, incr
* orun
);
339 if (timer
->expires
.tv64
> now
.tv64
)
342 * This (and the ktime_add() below) is the
343 * correction for exact:
347 timer
->expires
= ktime_add(timer
->expires
, interval
);
353 * enqueue_hrtimer - internal function to (re)start a timer
355 * The timer is inserted in expiry order. Insertion into the
356 * red black tree is O(log(n)). Must hold the base lock.
358 static void enqueue_hrtimer(struct hrtimer
*timer
, struct hrtimer_base
*base
)
360 struct rb_node
**link
= &base
->active
.rb_node
;
361 struct rb_node
*parent
= NULL
;
362 struct hrtimer
*entry
;
365 * Find the right place in the rbtree:
369 entry
= rb_entry(parent
, struct hrtimer
, node
);
371 * We dont care about collisions. Nodes with
372 * the same expiry time stay together.
374 if (timer
->expires
.tv64
< entry
->expires
.tv64
)
375 link
= &(*link
)->rb_left
;
377 link
= &(*link
)->rb_right
;
381 * Insert the timer to the rbtree and check whether it
382 * replaces the first pending timer
384 rb_link_node(&timer
->node
, parent
, link
);
385 rb_insert_color(&timer
->node
, &base
->active
);
387 if (!base
->first
|| timer
->expires
.tv64
<
388 rb_entry(base
->first
, struct hrtimer
, node
)->expires
.tv64
)
389 base
->first
= &timer
->node
;
393 * __remove_hrtimer - internal function to remove a timer
395 * Caller must hold the base lock.
397 static void __remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_base
*base
)
400 * Remove the timer from the rbtree and replace the
401 * first entry pointer if necessary.
403 if (base
->first
== &timer
->node
)
404 base
->first
= rb_next(&timer
->node
);
405 rb_erase(&timer
->node
, &base
->active
);
406 rb_set_parent(&timer
->node
, &timer
->node
);
410 * remove hrtimer, called with base lock held
413 remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_base
*base
)
415 if (hrtimer_active(timer
)) {
416 __remove_hrtimer(timer
, base
);
423 * hrtimer_start - (re)start an relative timer on the current CPU
424 * @timer: the timer to be added
426 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
430 * 1 when the timer was active
433 hrtimer_start(struct hrtimer
*timer
, ktime_t tim
, const enum hrtimer_mode mode
)
435 struct hrtimer_base
*base
, *new_base
;
439 base
= lock_hrtimer_base(timer
, &flags
);
441 /* Remove an active timer from the queue: */
442 ret
= remove_hrtimer(timer
, base
);
444 /* Switch the timer base, if necessary: */
445 new_base
= switch_hrtimer_base(timer
, base
);
447 if (mode
== HRTIMER_REL
) {
448 tim
= ktime_add(tim
, new_base
->get_time());
450 * CONFIG_TIME_LOW_RES is a temporary way for architectures
451 * to signal that they simply return xtime in
452 * do_gettimeoffset(). In this case we want to round up by
453 * resolution when starting a relative timer, to avoid short
454 * timeouts. This will go away with the GTOD framework.
456 #ifdef CONFIG_TIME_LOW_RES
457 tim
= ktime_add(tim
, base
->resolution
);
460 timer
->expires
= tim
;
462 enqueue_hrtimer(timer
, new_base
);
464 unlock_hrtimer_base(timer
, &flags
);
468 EXPORT_SYMBOL_GPL(hrtimer_start
);
471 * hrtimer_try_to_cancel - try to deactivate a timer
472 * @timer: hrtimer to stop
475 * 0 when the timer was not active
476 * 1 when the timer was active
477 * -1 when the timer is currently excuting the callback function and
480 int hrtimer_try_to_cancel(struct hrtimer
*timer
)
482 struct hrtimer_base
*base
;
486 base
= lock_hrtimer_base(timer
, &flags
);
488 if (base
->curr_timer
!= timer
)
489 ret
= remove_hrtimer(timer
, base
);
491 unlock_hrtimer_base(timer
, &flags
);
496 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel
);
499 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
500 * @timer: the timer to be cancelled
503 * 0 when the timer was not active
504 * 1 when the timer was active
506 int hrtimer_cancel(struct hrtimer
*timer
)
509 int ret
= hrtimer_try_to_cancel(timer
);
516 EXPORT_SYMBOL_GPL(hrtimer_cancel
);
519 * hrtimer_get_remaining - get remaining time for the timer
520 * @timer: the timer to read
522 ktime_t
hrtimer_get_remaining(const struct hrtimer
*timer
)
524 struct hrtimer_base
*base
;
528 base
= lock_hrtimer_base(timer
, &flags
);
529 rem
= ktime_sub(timer
->expires
, timer
->base
->get_time());
530 unlock_hrtimer_base(timer
, &flags
);
534 EXPORT_SYMBOL_GPL(hrtimer_get_remaining
);
536 #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
538 * hrtimer_get_next_event - get the time until next expiry event
540 * Returns the delta to the next expiry event or KTIME_MAX if no timer
543 ktime_t
hrtimer_get_next_event(void)
545 struct hrtimer_base
*base
= __get_cpu_var(hrtimer_bases
);
546 ktime_t delta
, mindelta
= { .tv64
= KTIME_MAX
};
550 for (i
= 0; i
< MAX_HRTIMER_BASES
; i
++, base
++) {
551 struct hrtimer
*timer
;
553 spin_lock_irqsave(&base
->lock
, flags
);
555 spin_unlock_irqrestore(&base
->lock
, flags
);
558 timer
= rb_entry(base
->first
, struct hrtimer
, node
);
559 delta
.tv64
= timer
->expires
.tv64
;
560 spin_unlock_irqrestore(&base
->lock
, flags
);
561 delta
= ktime_sub(delta
, base
->get_time());
562 if (delta
.tv64
< mindelta
.tv64
)
563 mindelta
.tv64
= delta
.tv64
;
565 if (mindelta
.tv64
< 0)
572 * hrtimer_init - initialize a timer to the given clock
573 * @timer: the timer to be initialized
574 * @clock_id: the clock to be used
575 * @mode: timer mode abs/rel
577 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
578 enum hrtimer_mode mode
)
580 struct hrtimer_base
*bases
;
582 memset(timer
, 0, sizeof(struct hrtimer
));
584 bases
= __raw_get_cpu_var(hrtimer_bases
);
586 if (clock_id
== CLOCK_REALTIME
&& mode
!= HRTIMER_ABS
)
587 clock_id
= CLOCK_MONOTONIC
;
589 timer
->base
= &bases
[clock_id
];
590 rb_set_parent(&timer
->node
, &timer
->node
);
592 EXPORT_SYMBOL_GPL(hrtimer_init
);
595 * hrtimer_get_res - get the timer resolution for a clock
596 * @which_clock: which clock to query
597 * @tp: pointer to timespec variable to store the resolution
599 * Store the resolution of the clock selected by @which_clock in the
600 * variable pointed to by @tp.
602 int hrtimer_get_res(const clockid_t which_clock
, struct timespec
*tp
)
604 struct hrtimer_base
*bases
;
606 bases
= __raw_get_cpu_var(hrtimer_bases
);
607 *tp
= ktime_to_timespec(bases
[which_clock
].resolution
);
611 EXPORT_SYMBOL_GPL(hrtimer_get_res
);
614 * Expire the per base hrtimer-queue:
616 static inline void run_hrtimer_queue(struct hrtimer_base
*base
)
618 struct rb_node
*node
;
623 if (base
->get_softirq_time
)
624 base
->softirq_time
= base
->get_softirq_time();
626 spin_lock_irq(&base
->lock
);
628 while ((node
= base
->first
)) {
629 struct hrtimer
*timer
;
630 int (*fn
)(struct hrtimer
*);
633 timer
= rb_entry(node
, struct hrtimer
, node
);
634 if (base
->softirq_time
.tv64
<= timer
->expires
.tv64
)
637 fn
= timer
->function
;
638 set_curr_timer(base
, timer
);
639 __remove_hrtimer(timer
, base
);
640 spin_unlock_irq(&base
->lock
);
644 spin_lock_irq(&base
->lock
);
646 if (restart
!= HRTIMER_NORESTART
) {
647 BUG_ON(hrtimer_active(timer
));
648 enqueue_hrtimer(timer
, base
);
651 set_curr_timer(base
, NULL
);
652 spin_unlock_irq(&base
->lock
);
656 * Called from timer softirq every jiffy, expire hrtimers:
658 void hrtimer_run_queues(void)
660 struct hrtimer_base
*base
= __get_cpu_var(hrtimer_bases
);
663 hrtimer_get_softirq_time(base
);
665 for (i
= 0; i
< MAX_HRTIMER_BASES
; i
++)
666 run_hrtimer_queue(&base
[i
]);
670 * Sleep related functions:
672 static int hrtimer_wakeup(struct hrtimer
*timer
)
674 struct hrtimer_sleeper
*t
=
675 container_of(timer
, struct hrtimer_sleeper
, timer
);
676 struct task_struct
*task
= t
->task
;
680 wake_up_process(task
);
682 return HRTIMER_NORESTART
;
685 void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
, struct task_struct
*task
)
687 sl
->timer
.function
= hrtimer_wakeup
;
691 static int __sched
do_nanosleep(struct hrtimer_sleeper
*t
, enum hrtimer_mode mode
)
693 hrtimer_init_sleeper(t
, current
);
696 set_current_state(TASK_INTERRUPTIBLE
);
697 hrtimer_start(&t
->timer
, t
->timer
.expires
, mode
);
701 hrtimer_cancel(&t
->timer
);
704 } while (t
->task
&& !signal_pending(current
));
706 return t
->task
== NULL
;
709 long __sched
hrtimer_nanosleep_restart(struct restart_block
*restart
)
711 struct hrtimer_sleeper t
;
712 struct timespec __user
*rmtp
;
716 restart
->fn
= do_no_restart_syscall
;
718 hrtimer_init(&t
.timer
, restart
->arg0
, HRTIMER_ABS
);
719 t
.timer
.expires
.tv64
= ((u64
)restart
->arg3
<< 32) | (u64
) restart
->arg2
;
721 if (do_nanosleep(&t
, HRTIMER_ABS
))
724 rmtp
= (struct timespec __user
*) restart
->arg1
;
726 time
= ktime_sub(t
.timer
.expires
, t
.timer
.base
->get_time());
729 tu
= ktime_to_timespec(time
);
730 if (copy_to_user(rmtp
, &tu
, sizeof(tu
)))
734 restart
->fn
= hrtimer_nanosleep_restart
;
736 /* The other values in restart are already filled in */
737 return -ERESTART_RESTARTBLOCK
;
740 long hrtimer_nanosleep(struct timespec
*rqtp
, struct timespec __user
*rmtp
,
741 const enum hrtimer_mode mode
, const clockid_t clockid
)
743 struct restart_block
*restart
;
744 struct hrtimer_sleeper t
;
748 hrtimer_init(&t
.timer
, clockid
, mode
);
749 t
.timer
.expires
= timespec_to_ktime(*rqtp
);
750 if (do_nanosleep(&t
, mode
))
753 /* Absolute timers do not update the rmtp value and restart: */
754 if (mode
== HRTIMER_ABS
)
755 return -ERESTARTNOHAND
;
758 rem
= ktime_sub(t
.timer
.expires
, t
.timer
.base
->get_time());
761 tu
= ktime_to_timespec(rem
);
762 if (copy_to_user(rmtp
, &tu
, sizeof(tu
)))
766 restart
= ¤t_thread_info()->restart_block
;
767 restart
->fn
= hrtimer_nanosleep_restart
;
768 restart
->arg0
= (unsigned long) t
.timer
.base
->index
;
769 restart
->arg1
= (unsigned long) rmtp
;
770 restart
->arg2
= t
.timer
.expires
.tv64
& 0xFFFFFFFF;
771 restart
->arg3
= t
.timer
.expires
.tv64
>> 32;
773 return -ERESTART_RESTARTBLOCK
;
777 sys_nanosleep(struct timespec __user
*rqtp
, struct timespec __user
*rmtp
)
781 if (copy_from_user(&tu
, rqtp
, sizeof(tu
)))
784 if (!timespec_valid(&tu
))
787 return hrtimer_nanosleep(&tu
, rmtp
, HRTIMER_REL
, CLOCK_MONOTONIC
);
791 * Functions related to boot-time initialization:
793 static void __devinit
init_hrtimers_cpu(int cpu
)
795 struct hrtimer_base
*base
= per_cpu(hrtimer_bases
, cpu
);
798 for (i
= 0; i
< MAX_HRTIMER_BASES
; i
++, base
++) {
799 spin_lock_init(&base
->lock
);
800 lockdep_set_class(&base
->lock
, &base
->lock_key
);
804 #ifdef CONFIG_HOTPLUG_CPU
806 static void migrate_hrtimer_list(struct hrtimer_base
*old_base
,
807 struct hrtimer_base
*new_base
)
809 struct hrtimer
*timer
;
810 struct rb_node
*node
;
812 while ((node
= rb_first(&old_base
->active
))) {
813 timer
= rb_entry(node
, struct hrtimer
, node
);
814 __remove_hrtimer(timer
, old_base
);
815 timer
->base
= new_base
;
816 enqueue_hrtimer(timer
, new_base
);
820 static void migrate_hrtimers(int cpu
)
822 struct hrtimer_base
*old_base
, *new_base
;
825 BUG_ON(cpu_online(cpu
));
826 old_base
= per_cpu(hrtimer_bases
, cpu
);
827 new_base
= get_cpu_var(hrtimer_bases
);
831 for (i
= 0; i
< MAX_HRTIMER_BASES
; i
++) {
833 spin_lock(&new_base
->lock
);
834 spin_lock(&old_base
->lock
);
836 BUG_ON(old_base
->curr_timer
);
838 migrate_hrtimer_list(old_base
, new_base
);
840 spin_unlock(&old_base
->lock
);
841 spin_unlock(&new_base
->lock
);
847 put_cpu_var(hrtimer_bases
);
849 #endif /* CONFIG_HOTPLUG_CPU */
851 static int __cpuinit
hrtimer_cpu_notify(struct notifier_block
*self
,
852 unsigned long action
, void *hcpu
)
854 long cpu
= (long)hcpu
;
859 init_hrtimers_cpu(cpu
);
862 #ifdef CONFIG_HOTPLUG_CPU
864 migrate_hrtimers(cpu
);
875 static struct notifier_block __cpuinitdata hrtimers_nb
= {
876 .notifier_call
= hrtimer_cpu_notify
,
879 void __init
hrtimers_init(void)
881 hrtimer_cpu_notify(&hrtimers_nb
, (unsigned long)CPU_UP_PREPARE
,
882 (void *)(long)smp_processor_id());
883 register_cpu_notifier(&hrtimers_nb
);