blob: 493c4b8b8cbabaecff8262592bb7207979c74a2d [file] [log] [blame]
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001/*
2 * linux/kernel/hrtimer.c
3 *
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08004 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08005 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08006 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08007 *
8 * High-resolution kernel timers
9 *
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.
13 *
14 * These timers are currently used for:
15 * - itimers
16 * - POSIX timers
17 * - nanosleep
18 * - precise in-kernel timing
19 *
20 * Started by: Thomas Gleixner and Ingo Molnar
21 *
22 * Credits:
23 * based on kernel/timer.c
24 *
Thomas Gleixner66188fa2006-02-01 03:05:13 -080025 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
27 *
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
30 *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080031 * For licencing details see kernel-base/COPYING
32 */
33
34#include <linux/cpu.h>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080035#include <linux/irq.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080036#include <linux/module.h>
37#include <linux/percpu.h>
38#include <linux/hrtimer.h>
39#include <linux/notifier.h>
40#include <linux/syscalls.h>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080041#include <linux/kallsyms.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080042#include <linux/interrupt.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080043#include <linux/tick.h>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080044#include <linux/seq_file.h>
45#include <linux/err.h>
Thomas Gleixner237fc6e2008-04-30 00:55:04 -070046#include <linux/debugobjects.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080047
48#include <asm/uaccess.h>
49
50/**
51 * ktime_get - get the monotonic time in ktime_t format
52 *
53 * returns the time in ktime_t format
54 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080055ktime_t ktime_get(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080056{
57 struct timespec now;
58
59 ktime_get_ts(&now);
60
61 return timespec_to_ktime(now);
62}
Patrick McHardy641b9e02007-03-16 01:18:42 -070063EXPORT_SYMBOL_GPL(ktime_get);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080064
65/**
66 * ktime_get_real - get the real (wall-) time in ktime_t format
67 *
68 * returns the time in ktime_t format
69 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080070ktime_t ktime_get_real(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080071{
72 struct timespec now;
73
74 getnstimeofday(&now);
75
76 return timespec_to_ktime(now);
77}
78
79EXPORT_SYMBOL_GPL(ktime_get_real);
80
81/*
82 * The timer bases:
George Anzinger7978672c2006-02-01 03:05:11 -080083 *
84 * Note: If we want to add new timer bases, we have to skip the two
85 * clock ids captured by the cpu-timers. We do this by holding empty
86 * entries rather than doing math adjustment of the clock ids.
87 * This ensures that we capture erroneous accesses to these clock ids
88 * rather than moving them into the range of valid clock id's.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080089 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080090DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080091{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080092
93 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080094 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080095 {
96 .index = CLOCK_REALTIME,
97 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080098 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080099 },
100 {
101 .index = CLOCK_MONOTONIC,
102 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800103 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800104 },
105 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800106};
107
108/**
109 * ktime_get_ts - get the monotonic clock in timespec format
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800110 * @ts: pointer to timespec variable
111 *
112 * The function calculates the monotonic clock from the realtime
113 * clock and the wall_to_monotonic offset and stores the result
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800114 * in normalized timespec format in the variable pointed to by @ts.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800115 */
116void ktime_get_ts(struct timespec *ts)
117{
118 struct timespec tomono;
119 unsigned long seq;
120
121 do {
122 seq = read_seqbegin(&xtime_lock);
123 getnstimeofday(ts);
124 tomono = wall_to_monotonic;
125
126 } while (read_seqretry(&xtime_lock, seq));
127
128 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
129 ts->tv_nsec + tomono.tv_nsec);
130}
Matt Helsley69778e32006-01-09 20:52:39 -0800131EXPORT_SYMBOL_GPL(ktime_get_ts);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800132
133/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800134 * Get the coarse grained time at the softirq based on xtime and
135 * wall_to_monotonic.
136 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800137static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800138{
139 ktime_t xtim, tomono;
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800140 struct timespec xts, tom;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800141 unsigned long seq;
142
143 do {
144 seq = read_seqbegin(&xtime_lock);
john stultz2c6b47d2007-07-24 17:47:43 -0700145 xts = current_kernel_time();
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800146 tom = wall_to_monotonic;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800147 } while (read_seqretry(&xtime_lock, seq));
148
john stultzf4304ab2007-02-16 01:27:26 -0800149 xtim = timespec_to_ktime(xts);
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800150 tomono = timespec_to_ktime(tom);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800151 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
152 base->clock_base[CLOCK_MONOTONIC].softirq_time =
153 ktime_add(xtim, tomono);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800154}
155
156/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800157 * Functions and macros which are different for UP/SMP systems are kept in a
158 * single place
159 */
160#ifdef CONFIG_SMP
161
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800162/*
163 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
164 * means that all timers which are tied to this base via timer->base are
165 * locked, and the base itself is locked too.
166 *
167 * So __run_timers/migrate_timers can safely modify all timers which could
168 * be found on the lists/queues.
169 *
170 * When the timer's base is locked, and the timer removed from list, it is
171 * possible to set timer->base = NULL and drop the lock: the timer remains
172 * locked.
173 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800174static
175struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
176 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800177{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800178 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800179
180 for (;;) {
181 base = timer->base;
182 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800183 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800184 if (likely(base == timer->base))
185 return base;
186 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800187 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800188 }
189 cpu_relax();
190 }
191}
192
193/*
194 * Switch the timer base to the current CPU when possible.
195 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800196static inline struct hrtimer_clock_base *
197switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800198{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800199 struct hrtimer_clock_base *new_base;
200 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800201
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800202 new_cpu_base = &__get_cpu_var(hrtimer_bases);
203 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800204
205 if (base != new_base) {
206 /*
207 * We are trying to schedule the timer on the local CPU.
208 * However we can't change timer's base while it is running,
209 * so we keep it on the same CPU. No hassle vs. reprogramming
210 * the event source in the high resolution case. The softirq
211 * code will take care of this when the timer function has
212 * completed. There is no conflict as we hold the lock until
213 * the timer is enqueued.
214 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800215 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800216 return base;
217
218 /* See the comment in lock_timer_base() */
219 timer->base = NULL;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800220 spin_unlock(&base->cpu_base->lock);
221 spin_lock(&new_base->cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800222 timer->base = new_base;
223 }
224 return new_base;
225}
226
227#else /* CONFIG_SMP */
228
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800229static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800230lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
231{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800232 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800233
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800234 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800235
236 return base;
237}
238
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800239# define switch_hrtimer_base(t, b) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800240
241#endif /* !CONFIG_SMP */
242
243/*
244 * Functions for the union type storage format of ktime_t which are
245 * too large for inlining:
246 */
247#if BITS_PER_LONG < 64
248# ifndef CONFIG_KTIME_SCALAR
249/**
250 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800251 * @kt: addend
252 * @nsec: the scalar nsec value to add
253 *
254 * Returns the sum of kt and nsec in ktime_t format
255 */
256ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
257{
258 ktime_t tmp;
259
260 if (likely(nsec < NSEC_PER_SEC)) {
261 tmp.tv64 = nsec;
262 } else {
263 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
264
265 tmp = ktime_set((long)nsec, rem);
266 }
267
268 return ktime_add(kt, tmp);
269}
David Howellsb8b8fd22007-04-27 15:31:24 -0700270
271EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700272
273/**
274 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
275 * @kt: minuend
276 * @nsec: the scalar nsec value to subtract
277 *
278 * Returns the subtraction of @nsec from @kt in ktime_t format
279 */
280ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
281{
282 ktime_t tmp;
283
284 if (likely(nsec < NSEC_PER_SEC)) {
285 tmp.tv64 = nsec;
286 } else {
287 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
288
289 tmp = ktime_set((long)nsec, rem);
290 }
291
292 return ktime_sub(kt, tmp);
293}
294
295EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800296# endif /* !CONFIG_KTIME_SCALAR */
297
298/*
299 * Divide a ktime value by a nanosecond value
300 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800301u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800302{
303 u64 dclc, inc, dns;
304 int sft = 0;
305
306 dclc = dns = ktime_to_ns(kt);
307 inc = div;
308 /* Make sure the divisor is less than 2^32: */
309 while (div >> 32) {
310 sft++;
311 div >>= 1;
312 }
313 dclc >>= sft;
314 do_div(dclc, (unsigned long) div);
315
Davide Libenzi4d672e72008-02-04 22:27:26 -0800316 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800317}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800318#endif /* BITS_PER_LONG >= 64 */
319
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100320/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100321 * Add two ktime values and do a safety check for overflow:
322 */
323ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
324{
325 ktime_t res = ktime_add(lhs, rhs);
326
327 /*
328 * We use KTIME_SEC_MAX here, the maximum timeout which we can
329 * return to user space in a timespec:
330 */
331 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
332 res = ktime_set(KTIME_SEC_MAX, 0);
333
334 return res;
335}
336
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700337#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
338
339static struct debug_obj_descr hrtimer_debug_descr;
340
341/*
342 * fixup_init is called when:
343 * - an active object is initialized
344 */
345static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
346{
347 struct hrtimer *timer = addr;
348
349 switch (state) {
350 case ODEBUG_STATE_ACTIVE:
351 hrtimer_cancel(timer);
352 debug_object_init(timer, &hrtimer_debug_descr);
353 return 1;
354 default:
355 return 0;
356 }
357}
358
359/*
360 * fixup_activate is called when:
361 * - an active object is activated
362 * - an unknown object is activated (might be a statically initialized object)
363 */
364static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
365{
366 switch (state) {
367
368 case ODEBUG_STATE_NOTAVAILABLE:
369 WARN_ON_ONCE(1);
370 return 0;
371
372 case ODEBUG_STATE_ACTIVE:
373 WARN_ON(1);
374
375 default:
376 return 0;
377 }
378}
379
380/*
381 * fixup_free is called when:
382 * - an active object is freed
383 */
384static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
385{
386 struct hrtimer *timer = addr;
387
388 switch (state) {
389 case ODEBUG_STATE_ACTIVE:
390 hrtimer_cancel(timer);
391 debug_object_free(timer, &hrtimer_debug_descr);
392 return 1;
393 default:
394 return 0;
395 }
396}
397
398static struct debug_obj_descr hrtimer_debug_descr = {
399 .name = "hrtimer",
400 .fixup_init = hrtimer_fixup_init,
401 .fixup_activate = hrtimer_fixup_activate,
402 .fixup_free = hrtimer_fixup_free,
403};
404
405static inline void debug_hrtimer_init(struct hrtimer *timer)
406{
407 debug_object_init(timer, &hrtimer_debug_descr);
408}
409
410static inline void debug_hrtimer_activate(struct hrtimer *timer)
411{
412 debug_object_activate(timer, &hrtimer_debug_descr);
413}
414
415static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
416{
417 debug_object_deactivate(timer, &hrtimer_debug_descr);
418}
419
420static inline void debug_hrtimer_free(struct hrtimer *timer)
421{
422 debug_object_free(timer, &hrtimer_debug_descr);
423}
424
425static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
426 enum hrtimer_mode mode);
427
428void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
429 enum hrtimer_mode mode)
430{
431 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
432 __hrtimer_init(timer, clock_id, mode);
433}
434
435void destroy_hrtimer_on_stack(struct hrtimer *timer)
436{
437 debug_object_free(timer, &hrtimer_debug_descr);
438}
439
440#else
441static inline void debug_hrtimer_init(struct hrtimer *timer) { }
442static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
443static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
444#endif
445
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100446/*
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100447 * Check, whether the timer is on the callback pending list
448 */
449static inline int hrtimer_cb_pending(const struct hrtimer *timer)
450{
451 return timer->state & HRTIMER_STATE_PENDING;
452}
453
454/*
455 * Remove a timer from the callback pending list
456 */
457static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
458{
459 list_del_init(&timer->cb_entry);
460}
461
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800462/* High resolution timer related functions */
463#ifdef CONFIG_HIGH_RES_TIMERS
464
465/*
466 * High resolution timer enabled ?
467 */
468static int hrtimer_hres_enabled __read_mostly = 1;
469
470/*
471 * Enable / Disable high resolution mode
472 */
473static int __init setup_hrtimer_hres(char *str)
474{
475 if (!strcmp(str, "off"))
476 hrtimer_hres_enabled = 0;
477 else if (!strcmp(str, "on"))
478 hrtimer_hres_enabled = 1;
479 else
480 return 0;
481 return 1;
482}
483
484__setup("highres=", setup_hrtimer_hres);
485
486/*
487 * hrtimer_high_res_enabled - query, if the highres mode is enabled
488 */
489static inline int hrtimer_is_hres_enabled(void)
490{
491 return hrtimer_hres_enabled;
492}
493
494/*
495 * Is the high resolution mode active ?
496 */
497static inline int hrtimer_hres_active(void)
498{
499 return __get_cpu_var(hrtimer_bases).hres_active;
500}
501
502/*
503 * Reprogram the event source with checking both queues for the
504 * next event
505 * Called with interrupts disabled and base->lock held
506 */
507static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
508{
509 int i;
510 struct hrtimer_clock_base *base = cpu_base->clock_base;
511 ktime_t expires;
512
513 cpu_base->expires_next.tv64 = KTIME_MAX;
514
515 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
516 struct hrtimer *timer;
517
518 if (!base->first)
519 continue;
520 timer = rb_entry(base->first, struct hrtimer, node);
521 expires = ktime_sub(timer->expires, base->offset);
522 if (expires.tv64 < cpu_base->expires_next.tv64)
523 cpu_base->expires_next = expires;
524 }
525
526 if (cpu_base->expires_next.tv64 != KTIME_MAX)
527 tick_program_event(cpu_base->expires_next, 1);
528}
529
530/*
531 * Shared reprogramming for clock_realtime and clock_monotonic
532 *
533 * When a timer is enqueued and expires earlier than the already enqueued
534 * timers, we have to check, whether it expires earlier than the timer for
535 * which the clock event device was armed.
536 *
537 * Called with interrupts disabled and base->cpu_base.lock held
538 */
539static int hrtimer_reprogram(struct hrtimer *timer,
540 struct hrtimer_clock_base *base)
541{
542 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
543 ktime_t expires = ktime_sub(timer->expires, base->offset);
544 int res;
545
Thomas Gleixner63070a72008-02-14 00:58:36 +0100546 WARN_ON_ONCE(timer->expires.tv64 < 0);
547
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800548 /*
549 * When the callback is running, we do not reprogram the clock event
550 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200551 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800552 * reprogramming is handled either by the softirq, which called the
553 * callback or at the end of the hrtimer_interrupt.
554 */
555 if (hrtimer_callback_running(timer))
556 return 0;
557
Thomas Gleixner63070a72008-02-14 00:58:36 +0100558 /*
559 * CLOCK_REALTIME timer might be requested with an absolute
560 * expiry time which is less than base->offset. Nothing wrong
561 * about that, just avoid to call into the tick code, which
562 * has now objections against negative expiry values.
563 */
564 if (expires.tv64 < 0)
565 return -ETIME;
566
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800567 if (expires.tv64 >= expires_next->tv64)
568 return 0;
569
570 /*
571 * Clockevents returns -ETIME, when the event was in the past.
572 */
573 res = tick_program_event(expires, 0);
574 if (!IS_ERR_VALUE(res))
575 *expires_next = expires;
576 return res;
577}
578
579
580/*
581 * Retrigger next event is called after clock was set
582 *
583 * Called with interrupts disabled via on_each_cpu()
584 */
585static void retrigger_next_event(void *arg)
586{
587 struct hrtimer_cpu_base *base;
588 struct timespec realtime_offset;
589 unsigned long seq;
590
591 if (!hrtimer_hres_active())
592 return;
593
594 do {
595 seq = read_seqbegin(&xtime_lock);
596 set_normalized_timespec(&realtime_offset,
597 -wall_to_monotonic.tv_sec,
598 -wall_to_monotonic.tv_nsec);
599 } while (read_seqretry(&xtime_lock, seq));
600
601 base = &__get_cpu_var(hrtimer_bases);
602
603 /* Adjust CLOCK_REALTIME offset */
604 spin_lock(&base->lock);
605 base->clock_base[CLOCK_REALTIME].offset =
606 timespec_to_ktime(realtime_offset);
607
608 hrtimer_force_reprogram(base);
609 spin_unlock(&base->lock);
610}
611
612/*
613 * Clock realtime was set
614 *
615 * Change the offset of the realtime clock vs. the monotonic
616 * clock.
617 *
618 * We might have to reprogram the high resolution timer interrupt. On
619 * SMP we call the architecture specific code to retrigger _all_ high
620 * resolution timer interrupts. On UP we just disable interrupts and
621 * call the high resolution interrupt code.
622 */
623void clock_was_set(void)
624{
625 /* Retrigger the CPU local events everywhere */
626 on_each_cpu(retrigger_next_event, NULL, 0, 1);
627}
628
629/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200630 * During resume we might have to reprogram the high resolution timer
631 * interrupt (on the local CPU):
632 */
633void hres_timers_resume(void)
634{
Ingo Molnar995f0542007-04-07 12:05:00 +0200635 /* Retrigger the CPU local events: */
636 retrigger_next_event(NULL);
637}
638
639/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800640 * Initialize the high resolution related parts of cpu_base
641 */
642static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
643{
644 base->expires_next.tv64 = KTIME_MAX;
645 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800646}
647
648/*
649 * Initialize the high resolution related parts of a hrtimer
650 */
651static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
652{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800653}
654
655/*
656 * When High resolution timers are active, try to reprogram. Note, that in case
657 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
658 * check happens. The timer gets enqueued into the rbtree. The reprogramming
659 * and expiry check is done in the hrtimer_interrupt or in the softirq.
660 */
661static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
662 struct hrtimer_clock_base *base)
663{
664 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
665
666 /* Timer is expired, act upon the callback mode */
667 switch(timer->cb_mode) {
668 case HRTIMER_CB_IRQSAFE_NO_RESTART:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700669 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800670 /*
671 * We can call the callback from here. No restart
672 * happens, so no danger of recursion
673 */
674 BUG_ON(timer->function(timer) != HRTIMER_NORESTART);
675 return 1;
676 case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:
677 /*
678 * This is solely for the sched tick emulation with
679 * dynamic tick support to ensure that we do not
680 * restart the tick right on the edge and end up with
681 * the tick timer in the softirq ! The calling site
682 * takes care of this.
683 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700684 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800685 return 1;
686 case HRTIMER_CB_IRQSAFE:
687 case HRTIMER_CB_SOFTIRQ:
688 /*
689 * Move everything else into the softirq pending list !
690 */
691 list_add_tail(&timer->cb_entry,
692 &base->cpu_base->cb_pending);
693 timer->state = HRTIMER_STATE_PENDING;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800694 return 1;
695 default:
696 BUG();
697 }
698 }
699 return 0;
700}
701
702/*
703 * Switch to high resolution mode
704 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800705static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800706{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700707 int cpu = smp_processor_id();
708 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800709 unsigned long flags;
710
711 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800712 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800713
714 local_irq_save(flags);
715
716 if (tick_init_highres()) {
717 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700718 printk(KERN_WARNING "Could not switch to high resolution "
719 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800720 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800721 }
722 base->hres_active = 1;
723 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
724 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
725
726 tick_setup_sched_timer();
727
728 /* "Retrigger" the interrupt to get things going */
729 retrigger_next_event(NULL);
730 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100731 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800732 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800733 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800734}
735
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200736static inline void hrtimer_raise_softirq(void)
737{
738 raise_softirq(HRTIMER_SOFTIRQ);
739}
740
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800741#else
742
743static inline int hrtimer_hres_active(void) { return 0; }
744static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800745static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800746static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
747static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
748 struct hrtimer_clock_base *base)
749{
750 return 0;
751}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800752static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
753static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100754static inline int hrtimer_reprogram(struct hrtimer *timer,
755 struct hrtimer_clock_base *base)
756{
757 return 0;
758}
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200759static inline void hrtimer_raise_softirq(void) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800760
761#endif /* CONFIG_HIGH_RES_TIMERS */
762
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800763#ifdef CONFIG_TIMER_STATS
764void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
765{
766 if (timer->start_site)
767 return;
768
769 timer->start_site = addr;
770 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
771 timer->start_pid = current->pid;
772}
773#endif
774
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800775/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200776 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800777 */
778static inline
779void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
780{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800781 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800782}
783
784/**
785 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800786 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800787 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800788 * @interval: the interval to forward
789 *
790 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700791 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800792 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800793u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800794{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800795 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800796 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800797
798 delta = ktime_sub(now, timer->expires);
799
800 if (delta.tv64 < 0)
801 return 0;
802
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100803 if (interval.tv64 < timer->base->resolution.tv64)
804 interval.tv64 = timer->base->resolution.tv64;
805
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800806 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800807 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800808
809 orun = ktime_divns(delta, incr);
810 timer->expires = ktime_add_ns(timer->expires, incr * orun);
811 if (timer->expires.tv64 > now.tv64)
812 return orun;
813 /*
814 * This (and the ktime_add() below) is the
815 * correction for exact:
816 */
817 orun++;
818 }
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100819 timer->expires = ktime_add_safe(timer->expires, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800820
821 return orun;
822}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700823EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800824
825/*
826 * enqueue_hrtimer - internal function to (re)start a timer
827 *
828 * The timer is inserted in expiry order. Insertion into the
829 * red black tree is O(log(n)). Must hold the base lock.
830 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800831static void enqueue_hrtimer(struct hrtimer *timer,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800832 struct hrtimer_clock_base *base, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800833{
834 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800835 struct rb_node *parent = NULL;
836 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700837 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800838
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700839 debug_hrtimer_activate(timer);
840
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800841 /*
842 * Find the right place in the rbtree:
843 */
844 while (*link) {
845 parent = *link;
846 entry = rb_entry(parent, struct hrtimer, node);
847 /*
848 * We dont care about collisions. Nodes with
849 * the same expiry time stay together.
850 */
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700851 if (timer->expires.tv64 < entry->expires.tv64) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700853 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800854 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700855 leftmost = 0;
856 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800857 }
858
859 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100860 * Insert the timer to the rbtree and check whether it
861 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800862 */
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700863 if (leftmost) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800864 /*
865 * Reprogram the clock event device. When the timer is already
866 * expired hrtimer_enqueue_reprogram has either called the
867 * callback or added it to the pending list and raised the
868 * softirq.
869 *
870 * This is a NOP for !HIGHRES
871 */
872 if (reprogram && hrtimer_enqueue_reprogram(timer, base))
873 return;
874
875 base->first = &timer->node;
876 }
877
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800878 rb_link_node(&timer->node, parent, link);
879 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800880 /*
881 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
882 * state of a possibly running callback.
883 */
884 timer->state |= HRTIMER_STATE_ENQUEUED;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100885}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800886
887/*
888 * __remove_hrtimer - internal function to remove a timer
889 *
890 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800891 *
892 * High resolution timer mode reprograms the clock event device when the
893 * timer is the one which expires next. The caller can disable this by setting
894 * reprogram to zero. This is useful, when the context does a reprogramming
895 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800896 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800897static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800898 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800899 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800900{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800901 /* High res. callback list. NOP for !HIGHRES */
902 if (hrtimer_cb_pending(timer))
903 hrtimer_remove_cb_pending(timer);
904 else {
905 /*
906 * Remove the timer from the rbtree and replace the
907 * first entry pointer if necessary.
908 */
909 if (base->first == &timer->node) {
910 base->first = rb_next(&timer->node);
911 /* Reprogram the clock event device. if enabled */
912 if (reprogram && hrtimer_hres_active())
913 hrtimer_force_reprogram(base->cpu_base);
914 }
915 rb_erase(&timer->node, &base->active);
916 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800917 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800918}
919
920/*
921 * remove hrtimer, called with base lock held
922 */
923static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800924remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800925{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800926 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800927 int reprogram;
928
929 /*
930 * Remove the timer and force reprogramming when high
931 * resolution mode is active and the timer is on the current
932 * CPU. If we remove a timer on another CPU, reprogramming is
933 * skipped. The interrupt event on this CPU is fired and
934 * reprogramming happens in the interrupt handler. This is a
935 * rare case and less expensive than a smp call.
936 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700937 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800938 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800939 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
940 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
941 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800942 return 1;
943 }
944 return 0;
945}
946
947/**
948 * hrtimer_start - (re)start an relative timer on the current CPU
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800949 * @timer: the timer to be added
950 * @tim: expiry time
951 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
952 *
953 * Returns:
954 * 0 on success
955 * 1 when the timer was active
956 */
957int
958hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
959{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800960 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800961 unsigned long flags;
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200962 int ret, raise;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800963
964 base = lock_hrtimer_base(timer, &flags);
965
966 /* Remove an active timer from the queue: */
967 ret = remove_hrtimer(timer, base);
968
969 /* Switch the timer base, if necessary: */
970 new_base = switch_hrtimer_base(timer, base);
971
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800972 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100973 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800974 /*
975 * CONFIG_TIME_LOW_RES is a temporary way for architectures
976 * to signal that they simply return xtime in
977 * do_gettimeoffset(). In this case we want to round up by
978 * resolution when starting a relative timer, to avoid short
979 * timeouts. This will go away with the GTOD framework.
980 */
981#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100982 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800983#endif
984 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700985
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800986 timer->expires = tim;
987
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800988 timer_stats_hrtimer_set_start_info(timer);
989
Ingo Molnar935c6312007-03-28 13:17:18 +0200990 /*
991 * Only allow reprogramming if the new base is on this CPU.
992 * (it might still be on another CPU if the timer was pending)
993 */
994 enqueue_hrtimer(timer, new_base,
995 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800996
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200997 /*
998 * The timer may be expired and moved to the cb_pending
999 * list. We can not raise the softirq with base lock held due
1000 * to a possible deadlock with runqueue lock.
1001 */
1002 raise = timer->state == HRTIMER_STATE_PENDING;
1003
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001004 unlock_hrtimer_base(timer, &flags);
1005
Thomas Gleixner0c96c592008-04-28 09:23:24 +02001006 if (raise)
1007 hrtimer_raise_softirq();
1008
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001009 return ret;
1010}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001011EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001012
1013/**
1014 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001015 * @timer: hrtimer to stop
1016 *
1017 * Returns:
1018 * 0 when the timer was not active
1019 * 1 when the timer was active
1020 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001021 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001022 */
1023int hrtimer_try_to_cancel(struct hrtimer *timer)
1024{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001025 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001026 unsigned long flags;
1027 int ret = -1;
1028
1029 base = lock_hrtimer_base(timer, &flags);
1030
Thomas Gleixner303e9672007-02-16 01:27:51 -08001031 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001032 ret = remove_hrtimer(timer, base);
1033
1034 unlock_hrtimer_base(timer, &flags);
1035
1036 return ret;
1037
1038}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001039EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001040
1041/**
1042 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001043 * @timer: the timer to be cancelled
1044 *
1045 * Returns:
1046 * 0 when the timer was not active
1047 * 1 when the timer was active
1048 */
1049int hrtimer_cancel(struct hrtimer *timer)
1050{
1051 for (;;) {
1052 int ret = hrtimer_try_to_cancel(timer);
1053
1054 if (ret >= 0)
1055 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001056 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001057 }
1058}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001059EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001060
1061/**
1062 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001063 * @timer: the timer to read
1064 */
1065ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1066{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001067 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001068 unsigned long flags;
1069 ktime_t rem;
1070
1071 base = lock_hrtimer_base(timer, &flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001072 rem = ktime_sub(timer->expires, base->get_time());
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001073 unlock_hrtimer_base(timer, &flags);
1074
1075 return rem;
1076}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001077EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001078
Thomas Gleixnerfd064b92007-02-16 01:27:47 -08001079#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
Tony Lindgren69239742006-03-06 15:42:45 -08001080/**
1081 * hrtimer_get_next_event - get the time until next expiry event
1082 *
1083 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1084 * is pending.
1085 */
1086ktime_t hrtimer_get_next_event(void)
1087{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001088 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1089 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001090 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1091 unsigned long flags;
1092 int i;
1093
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001094 spin_lock_irqsave(&cpu_base->lock, flags);
1095
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001096 if (!hrtimer_hres_active()) {
1097 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1098 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001099
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001100 if (!base->first)
1101 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001102
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001103 timer = rb_entry(base->first, struct hrtimer, node);
1104 delta.tv64 = timer->expires.tv64;
1105 delta = ktime_sub(delta, base->get_time());
1106 if (delta.tv64 < mindelta.tv64)
1107 mindelta.tv64 = delta.tv64;
1108 }
Tony Lindgren69239742006-03-06 15:42:45 -08001109 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001110
1111 spin_unlock_irqrestore(&cpu_base->lock, flags);
1112
Tony Lindgren69239742006-03-06 15:42:45 -08001113 if (mindelta.tv64 < 0)
1114 mindelta.tv64 = 0;
1115 return mindelta;
1116}
1117#endif
1118
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001119static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1120 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001121{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001122 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001123
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001124 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001125
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001126 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001127
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001128 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001129 clock_id = CLOCK_MONOTONIC;
1130
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001131 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001132 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001133 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001134
1135#ifdef CONFIG_TIMER_STATS
1136 timer->start_site = NULL;
1137 timer->start_pid = -1;
1138 memset(timer->start_comm, 0, TASK_COMM_LEN);
1139#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001140}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001141
1142/**
1143 * hrtimer_init - initialize a timer to the given clock
1144 * @timer: the timer to be initialized
1145 * @clock_id: the clock to be used
1146 * @mode: timer mode abs/rel
1147 */
1148void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1149 enum hrtimer_mode mode)
1150{
1151 debug_hrtimer_init(timer);
1152 __hrtimer_init(timer, clock_id, mode);
1153}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001154EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001155
1156/**
1157 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001158 * @which_clock: which clock to query
1159 * @tp: pointer to timespec variable to store the resolution
1160 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001161 * Store the resolution of the clock selected by @which_clock in the
1162 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001163 */
1164int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1165{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001166 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001167
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001168 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1169 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001170
1171 return 0;
1172}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001173EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001174
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001175static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001176{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001177 spin_lock_irq(&cpu_base->lock);
1178
1179 while (!list_empty(&cpu_base->cb_pending)) {
1180 enum hrtimer_restart (*fn)(struct hrtimer *);
1181 struct hrtimer *timer;
1182 int restart;
1183
1184 timer = list_entry(cpu_base->cb_pending.next,
1185 struct hrtimer, cb_entry);
1186
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001187 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001188 timer_stats_account_hrtimer(timer);
1189
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001190 fn = timer->function;
1191 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
1192 spin_unlock_irq(&cpu_base->lock);
1193
1194 restart = fn(timer);
1195
1196 spin_lock_irq(&cpu_base->lock);
1197
1198 timer->state &= ~HRTIMER_STATE_CALLBACK;
1199 if (restart == HRTIMER_RESTART) {
1200 BUG_ON(hrtimer_active(timer));
1201 /*
1202 * Enqueue the timer, allow reprogramming of the event
1203 * device
1204 */
1205 enqueue_hrtimer(timer, timer->base, 1);
1206 } else if (hrtimer_active(timer)) {
1207 /*
1208 * If the timer was rearmed on another CPU, reprogram
1209 * the event device.
1210 */
Bodo Stroesserd7b41a22008-04-26 14:10:16 -07001211 struct hrtimer_clock_base *base = timer->base;
1212
1213 if (base->first == &timer->node &&
1214 hrtimer_reprogram(timer, base)) {
1215 /*
1216 * Timer is expired. Thus move it from tree to
1217 * pending list again.
1218 */
1219 __remove_hrtimer(timer, base,
1220 HRTIMER_STATE_PENDING, 0);
1221 list_add_tail(&timer->cb_entry,
1222 &base->cpu_base->cb_pending);
1223 }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001224 }
1225 }
1226 spin_unlock_irq(&cpu_base->lock);
1227}
1228
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001229static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001230{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001231 struct hrtimer_clock_base *base = timer->base;
1232 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1233 enum hrtimer_restart (*fn)(struct hrtimer *);
1234 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001235
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001236 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001237 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1238 timer_stats_account_hrtimer(timer);
Dimitri Sivanich3055add2006-03-31 02:31:20 -08001239
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001240 fn = timer->function;
1241 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
1242 /*
1243 * Used for scheduler timers, avoid lock inversion with
1244 * rq->lock and tasklist_lock.
1245 *
1246 * These timers are required to deal with enqueue expiry
1247 * themselves and are not allowed to migrate.
1248 */
1249 spin_unlock(&cpu_base->lock);
1250 restart = fn(timer);
1251 spin_lock(&cpu_base->lock);
1252 } else
Roman Zippel05cfb612006-03-26 01:38:12 -08001253 restart = fn(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001254
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001255 /*
1256 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1257 * reprogramming of the event hardware. This happens at the end of this
1258 * function anyway.
1259 */
1260 if (restart != HRTIMER_NORESTART) {
1261 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1262 enqueue_hrtimer(timer, base, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001263 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001264 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001265}
1266
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001267#ifdef CONFIG_HIGH_RES_TIMERS
1268
1269/*
1270 * High resolution timer interrupt
1271 * Called with interrupts disabled
1272 */
1273void hrtimer_interrupt(struct clock_event_device *dev)
1274{
1275 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1276 struct hrtimer_clock_base *base;
1277 ktime_t expires_next, now;
1278 int i, raise = 0;
1279
1280 BUG_ON(!cpu_base->hres_active);
1281 cpu_base->nr_events++;
1282 dev->next_event.tv64 = KTIME_MAX;
1283
1284 retry:
1285 now = ktime_get();
1286
1287 expires_next.tv64 = KTIME_MAX;
1288
1289 base = cpu_base->clock_base;
1290
1291 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1292 ktime_t basenow;
1293 struct rb_node *node;
1294
1295 spin_lock(&cpu_base->lock);
1296
1297 basenow = ktime_add(now, base->offset);
1298
1299 while ((node = base->first)) {
1300 struct hrtimer *timer;
1301
1302 timer = rb_entry(node, struct hrtimer, node);
1303
1304 if (basenow.tv64 < timer->expires.tv64) {
1305 ktime_t expires;
1306
1307 expires = ktime_sub(timer->expires,
1308 base->offset);
1309 if (expires.tv64 < expires_next.tv64)
1310 expires_next = expires;
1311 break;
1312 }
1313
1314 /* Move softirq callbacks to the pending list */
1315 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1316 __remove_hrtimer(timer, base,
1317 HRTIMER_STATE_PENDING, 0);
1318 list_add_tail(&timer->cb_entry,
1319 &base->cpu_base->cb_pending);
1320 raise = 1;
1321 continue;
1322 }
1323
1324 __run_hrtimer(timer);
1325 }
1326 spin_unlock(&cpu_base->lock);
1327 base++;
1328 }
1329
1330 cpu_base->expires_next = expires_next;
1331
1332 /* Reprogramming necessary ? */
1333 if (expires_next.tv64 != KTIME_MAX) {
1334 if (tick_program_event(expires_next, 0))
1335 goto retry;
1336 }
1337
1338 /* Raise softirq ? */
1339 if (raise)
1340 raise_softirq(HRTIMER_SOFTIRQ);
1341}
1342
1343static void run_hrtimer_softirq(struct softirq_action *h)
1344{
1345 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
1346}
1347
1348#endif /* CONFIG_HIGH_RES_TIMERS */
1349
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001350/*
1351 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001352 *
1353 * For HRT its the fall back code to run the softirq in the timer
1354 * softirq context in case the hrtimer initialization failed or has
1355 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001356 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001357void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001358{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001359 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001360
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001361 if (hrtimer_hres_active())
1362 return;
1363
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001364 /*
1365 * This _is_ ugly: We have to check in the softirq context,
1366 * whether we can switch to highres and / or nohz mode. The
1367 * clocksource switch happens in the timer interrupt with
1368 * xtime_lock held. Notification from there only sets the
1369 * check bit in the tick_oneshot code, otherwise we might
1370 * deadlock vs. xtime_lock.
1371 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001372 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001373 hrtimer_switch_to_hres();
1374
1375 run_hrtimer_pending(cpu_base);
1376}
1377
1378/*
1379 * Called from hardirq context every jiffy
1380 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001381void hrtimer_run_queues(void)
1382{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001383 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001384 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001385 struct hrtimer_clock_base *base;
1386 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001387
1388 if (hrtimer_hres_active())
1389 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001390
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001391 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1392 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001393
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001394 if (!base->first)
1395 continue;
1396
Thomas Gleixner259aae82008-04-19 21:31:26 +02001397 if (base->get_softirq_time)
1398 base->softirq_time = base->get_softirq_time();
1399 else if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001400 hrtimer_get_softirq_time(cpu_base);
1401 gettime = 0;
1402 }
1403
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001404 spin_lock(&cpu_base->lock);
1405
1406 while ((node = base->first)) {
1407 struct hrtimer *timer;
1408
1409 timer = rb_entry(node, struct hrtimer, node);
1410 if (base->softirq_time.tv64 <= timer->expires.tv64)
1411 break;
1412
1413 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1414 __remove_hrtimer(timer, base,
1415 HRTIMER_STATE_PENDING, 0);
1416 list_add_tail(&timer->cb_entry,
1417 &base->cpu_base->cb_pending);
1418 continue;
1419 }
1420
1421 __run_hrtimer(timer);
1422 }
1423 spin_unlock(&cpu_base->lock);
1424 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001425}
1426
1427/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001428 * Sleep related functions:
1429 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001430static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001431{
1432 struct hrtimer_sleeper *t =
1433 container_of(timer, struct hrtimer_sleeper, timer);
1434 struct task_struct *task = t->task;
1435
1436 t->task = NULL;
1437 if (task)
1438 wake_up_process(task);
1439
1440 return HRTIMER_NORESTART;
1441}
1442
Ingo Molnar36c8b582006-07-03 00:25:41 -07001443void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001444{
1445 sl->timer.function = hrtimer_wakeup;
1446 sl->task = task;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001447#ifdef CONFIG_HIGH_RES_TIMERS
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001448 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001449#endif
Thomas Gleixner00362e32006-03-31 02:31:17 -08001450}
1451
Thomas Gleixner669d7862006-03-31 02:31:19 -08001452static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001453{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001454 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001455
Roman Zippel432569b2006-03-26 01:38:08 -08001456 do {
1457 set_current_state(TASK_INTERRUPTIBLE);
1458 hrtimer_start(&t->timer, t->timer.expires, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001459 if (!hrtimer_active(&t->timer))
1460 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001461
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001462 if (likely(t->task))
1463 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001464
Thomas Gleixner669d7862006-03-31 02:31:19 -08001465 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001466 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001467
Thomas Gleixner669d7862006-03-31 02:31:19 -08001468 } while (t->task && !signal_pending(current));
1469
Peter Zijlstra3588a082008-02-01 17:45:13 +01001470 __set_current_state(TASK_RUNNING);
1471
Thomas Gleixner669d7862006-03-31 02:31:19 -08001472 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001473}
1474
Oleg Nesterov080344b2008-02-01 17:29:05 +03001475static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1476{
1477 struct timespec rmt;
1478 ktime_t rem;
1479
1480 rem = ktime_sub(timer->expires, timer->base->get_time());
1481 if (rem.tv64 <= 0)
1482 return 0;
1483 rmt = ktime_to_timespec(rem);
1484
1485 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1486 return -EFAULT;
1487
1488 return 1;
1489}
1490
Toyo Abe1711ef32006-09-29 02:00:28 -07001491long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001492{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001493 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001494 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001495 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001496
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001497 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1498 HRTIMER_MODE_ABS);
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001499 t.timer.expires.tv64 = restart->nanosleep.expires;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001500
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001501 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001502 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001503
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001504 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001505 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001506 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001507 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001508 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001509 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001510
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001511 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001512 ret = -ERESTART_RESTARTBLOCK;
1513out:
1514 destroy_hrtimer_on_stack(&t.timer);
1515 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001516}
1517
Oleg Nesterov080344b2008-02-01 17:29:05 +03001518long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001519 const enum hrtimer_mode mode, const clockid_t clockid)
1520{
1521 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001522 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001523 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001524
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001525 hrtimer_init_on_stack(&t.timer, clockid, mode);
Roman Zippel432569b2006-03-26 01:38:08 -08001526 t.timer.expires = timespec_to_ktime(*rqtp);
1527 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001528 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001529
George Anzinger7978672c2006-02-01 03:05:11 -08001530 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001531 if (mode == HRTIMER_MODE_ABS) {
1532 ret = -ERESTARTNOHAND;
1533 goto out;
1534 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001535
Roman Zippel432569b2006-03-26 01:38:08 -08001536 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001537 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001538 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001539 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001540 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001541
1542 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001543 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001544 restart->nanosleep.index = t.timer.base->index;
1545 restart->nanosleep.rmtp = rmtp;
1546 restart->nanosleep.expires = t.timer.expires.tv64;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001547
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001548 ret = -ERESTART_RESTARTBLOCK;
1549out:
1550 destroy_hrtimer_on_stack(&t.timer);
1551 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001552}
1553
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001554asmlinkage long
1555sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1556{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001557 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001558
1559 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1560 return -EFAULT;
1561
1562 if (!timespec_valid(&tu))
1563 return -EINVAL;
1564
Oleg Nesterov080344b2008-02-01 17:29:05 +03001565 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001566}
1567
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001568/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001569 * Functions related to boot-time initialization:
1570 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001571static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001572{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001573 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001574 int i;
1575
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001576 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001577
1578 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1579 cpu_base->clock_base[i].cpu_base = cpu_base;
1580
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001581 INIT_LIST_HEAD(&cpu_base->cb_pending);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001582 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001583}
1584
1585#ifdef CONFIG_HOTPLUG_CPU
1586
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001587static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1588 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001589{
1590 struct hrtimer *timer;
1591 struct rb_node *node;
1592
1593 while ((node = rb_first(&old_base->active))) {
1594 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001595 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001596 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001597 __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001598 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001599 /*
1600 * Enqueue the timer. Allow reprogramming of the event device
1601 */
1602 enqueue_hrtimer(timer, new_base, 1);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001603 }
1604}
1605
1606static void migrate_hrtimers(int cpu)
1607{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001608 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001609 int i;
1610
1611 BUG_ON(cpu_online(cpu));
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001612 old_base = &per_cpu(hrtimer_bases, cpu);
1613 new_base = &get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001614
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001615 tick_cancel_sched_timer(cpu);
1616
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001617 local_irq_disable();
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001618 spin_lock(&new_base->lock);
1619 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001620
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001621 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001622 migrate_hrtimer_list(&old_base->clock_base[i],
1623 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001624 }
1625
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001626 spin_unlock(&old_base->lock);
1627 spin_unlock(&new_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001628 local_irq_enable();
1629 put_cpu_var(hrtimer_bases);
1630}
1631#endif /* CONFIG_HOTPLUG_CPU */
1632
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001633static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001634 unsigned long action, void *hcpu)
1635{
David Miller7713a7d2007-07-16 17:17:44 -07001636 unsigned int cpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001637
1638 switch (action) {
1639
1640 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001641 case CPU_UP_PREPARE_FROZEN:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001642 init_hrtimers_cpu(cpu);
1643 break;
1644
1645#ifdef CONFIG_HOTPLUG_CPU
1646 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001647 case CPU_DEAD_FROZEN:
Thomas Gleixnerd316c572007-02-16 01:28:00 -08001648 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001649 migrate_hrtimers(cpu);
1650 break;
1651#endif
1652
1653 default:
1654 break;
1655 }
1656
1657 return NOTIFY_OK;
1658}
1659
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001660static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001661 .notifier_call = hrtimer_cpu_notify,
1662};
1663
1664void __init hrtimers_init(void)
1665{
1666 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1667 (void *)(long)smp_processor_id());
1668 register_cpu_notifier(&hrtimers_nb);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001669#ifdef CONFIG_HIGH_RES_TIMERS
1670 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq, NULL);
1671#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001672}
1673