blob: 9af1d6a8095e40f80e64cdade6c0c3b505184466 [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 Gleixner303e9672007-02-16 01:27:51 -0800157 * Helper function to check, whether the timer is running the callback
158 * function
159 */
160static inline int hrtimer_callback_running(struct hrtimer *timer)
161{
162 return timer->state & HRTIMER_STATE_CALLBACK;
163}
164
165/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800166 * Functions and macros which are different for UP/SMP systems are kept in a
167 * single place
168 */
169#ifdef CONFIG_SMP
170
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800171/*
172 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
173 * means that all timers which are tied to this base via timer->base are
174 * locked, and the base itself is locked too.
175 *
176 * So __run_timers/migrate_timers can safely modify all timers which could
177 * be found on the lists/queues.
178 *
179 * When the timer's base is locked, and the timer removed from list, it is
180 * possible to set timer->base = NULL and drop the lock: the timer remains
181 * locked.
182 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800183static
184struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
185 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800186{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800187 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800188
189 for (;;) {
190 base = timer->base;
191 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800192 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800193 if (likely(base == timer->base))
194 return base;
195 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800196 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800197 }
198 cpu_relax();
199 }
200}
201
202/*
203 * Switch the timer base to the current CPU when possible.
204 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800205static inline struct hrtimer_clock_base *
206switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800207{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800208 struct hrtimer_clock_base *new_base;
209 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800210
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800211 new_cpu_base = &__get_cpu_var(hrtimer_bases);
212 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800213
214 if (base != new_base) {
215 /*
216 * We are trying to schedule the timer on the local CPU.
217 * However we can't change timer's base while it is running,
218 * so we keep it on the same CPU. No hassle vs. reprogramming
219 * the event source in the high resolution case. The softirq
220 * code will take care of this when the timer function has
221 * completed. There is no conflict as we hold the lock until
222 * the timer is enqueued.
223 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800224 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800225 return base;
226
227 /* See the comment in lock_timer_base() */
228 timer->base = NULL;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800229 spin_unlock(&base->cpu_base->lock);
230 spin_lock(&new_base->cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800231 timer->base = new_base;
232 }
233 return new_base;
234}
235
236#else /* CONFIG_SMP */
237
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800238static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800239lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
240{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800241 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800242
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800243 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800244
245 return base;
246}
247
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800248# define switch_hrtimer_base(t, b) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800249
250#endif /* !CONFIG_SMP */
251
252/*
253 * Functions for the union type storage format of ktime_t which are
254 * too large for inlining:
255 */
256#if BITS_PER_LONG < 64
257# ifndef CONFIG_KTIME_SCALAR
258/**
259 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800260 * @kt: addend
261 * @nsec: the scalar nsec value to add
262 *
263 * Returns the sum of kt and nsec in ktime_t format
264 */
265ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
266{
267 ktime_t tmp;
268
269 if (likely(nsec < NSEC_PER_SEC)) {
270 tmp.tv64 = nsec;
271 } else {
272 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
273
274 tmp = ktime_set((long)nsec, rem);
275 }
276
277 return ktime_add(kt, tmp);
278}
David Howellsb8b8fd22007-04-27 15:31:24 -0700279
280EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700281
282/**
283 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
284 * @kt: minuend
285 * @nsec: the scalar nsec value to subtract
286 *
287 * Returns the subtraction of @nsec from @kt in ktime_t format
288 */
289ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
290{
291 ktime_t tmp;
292
293 if (likely(nsec < NSEC_PER_SEC)) {
294 tmp.tv64 = nsec;
295 } else {
296 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
297
298 tmp = ktime_set((long)nsec, rem);
299 }
300
301 return ktime_sub(kt, tmp);
302}
303
304EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800305# endif /* !CONFIG_KTIME_SCALAR */
306
307/*
308 * Divide a ktime value by a nanosecond value
309 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800310u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800311{
312 u64 dclc, inc, dns;
313 int sft = 0;
314
315 dclc = dns = ktime_to_ns(kt);
316 inc = div;
317 /* Make sure the divisor is less than 2^32: */
318 while (div >> 32) {
319 sft++;
320 div >>= 1;
321 }
322 dclc >>= sft;
323 do_div(dclc, (unsigned long) div);
324
Davide Libenzi4d672e72008-02-04 22:27:26 -0800325 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800326}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800327#endif /* BITS_PER_LONG >= 64 */
328
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100329/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100330 * Add two ktime values and do a safety check for overflow:
331 */
332ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
333{
334 ktime_t res = ktime_add(lhs, rhs);
335
336 /*
337 * We use KTIME_SEC_MAX here, the maximum timeout which we can
338 * return to user space in a timespec:
339 */
340 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
341 res = ktime_set(KTIME_SEC_MAX, 0);
342
343 return res;
344}
345
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700346#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
347
348static struct debug_obj_descr hrtimer_debug_descr;
349
350/*
351 * fixup_init is called when:
352 * - an active object is initialized
353 */
354static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
355{
356 struct hrtimer *timer = addr;
357
358 switch (state) {
359 case ODEBUG_STATE_ACTIVE:
360 hrtimer_cancel(timer);
361 debug_object_init(timer, &hrtimer_debug_descr);
362 return 1;
363 default:
364 return 0;
365 }
366}
367
368/*
369 * fixup_activate is called when:
370 * - an active object is activated
371 * - an unknown object is activated (might be a statically initialized object)
372 */
373static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
374{
375 switch (state) {
376
377 case ODEBUG_STATE_NOTAVAILABLE:
378 WARN_ON_ONCE(1);
379 return 0;
380
381 case ODEBUG_STATE_ACTIVE:
382 WARN_ON(1);
383
384 default:
385 return 0;
386 }
387}
388
389/*
390 * fixup_free is called when:
391 * - an active object is freed
392 */
393static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
394{
395 struct hrtimer *timer = addr;
396
397 switch (state) {
398 case ODEBUG_STATE_ACTIVE:
399 hrtimer_cancel(timer);
400 debug_object_free(timer, &hrtimer_debug_descr);
401 return 1;
402 default:
403 return 0;
404 }
405}
406
407static struct debug_obj_descr hrtimer_debug_descr = {
408 .name = "hrtimer",
409 .fixup_init = hrtimer_fixup_init,
410 .fixup_activate = hrtimer_fixup_activate,
411 .fixup_free = hrtimer_fixup_free,
412};
413
414static inline void debug_hrtimer_init(struct hrtimer *timer)
415{
416 debug_object_init(timer, &hrtimer_debug_descr);
417}
418
419static inline void debug_hrtimer_activate(struct hrtimer *timer)
420{
421 debug_object_activate(timer, &hrtimer_debug_descr);
422}
423
424static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
425{
426 debug_object_deactivate(timer, &hrtimer_debug_descr);
427}
428
429static inline void debug_hrtimer_free(struct hrtimer *timer)
430{
431 debug_object_free(timer, &hrtimer_debug_descr);
432}
433
434static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
435 enum hrtimer_mode mode);
436
437void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
438 enum hrtimer_mode mode)
439{
440 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
441 __hrtimer_init(timer, clock_id, mode);
442}
443
444void destroy_hrtimer_on_stack(struct hrtimer *timer)
445{
446 debug_object_free(timer, &hrtimer_debug_descr);
447}
448
449#else
450static inline void debug_hrtimer_init(struct hrtimer *timer) { }
451static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
452static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
453#endif
454
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100455/*
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100456 * Check, whether the timer is on the callback pending list
457 */
458static inline int hrtimer_cb_pending(const struct hrtimer *timer)
459{
460 return timer->state & HRTIMER_STATE_PENDING;
461}
462
463/*
464 * Remove a timer from the callback pending list
465 */
466static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
467{
468 list_del_init(&timer->cb_entry);
469}
470
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800471/* High resolution timer related functions */
472#ifdef CONFIG_HIGH_RES_TIMERS
473
474/*
475 * High resolution timer enabled ?
476 */
477static int hrtimer_hres_enabled __read_mostly = 1;
478
479/*
480 * Enable / Disable high resolution mode
481 */
482static int __init setup_hrtimer_hres(char *str)
483{
484 if (!strcmp(str, "off"))
485 hrtimer_hres_enabled = 0;
486 else if (!strcmp(str, "on"))
487 hrtimer_hres_enabled = 1;
488 else
489 return 0;
490 return 1;
491}
492
493__setup("highres=", setup_hrtimer_hres);
494
495/*
496 * hrtimer_high_res_enabled - query, if the highres mode is enabled
497 */
498static inline int hrtimer_is_hres_enabled(void)
499{
500 return hrtimer_hres_enabled;
501}
502
503/*
504 * Is the high resolution mode active ?
505 */
506static inline int hrtimer_hres_active(void)
507{
508 return __get_cpu_var(hrtimer_bases).hres_active;
509}
510
511/*
512 * Reprogram the event source with checking both queues for the
513 * next event
514 * Called with interrupts disabled and base->lock held
515 */
516static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
517{
518 int i;
519 struct hrtimer_clock_base *base = cpu_base->clock_base;
520 ktime_t expires;
521
522 cpu_base->expires_next.tv64 = KTIME_MAX;
523
524 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
525 struct hrtimer *timer;
526
527 if (!base->first)
528 continue;
529 timer = rb_entry(base->first, struct hrtimer, node);
530 expires = ktime_sub(timer->expires, base->offset);
531 if (expires.tv64 < cpu_base->expires_next.tv64)
532 cpu_base->expires_next = expires;
533 }
534
535 if (cpu_base->expires_next.tv64 != KTIME_MAX)
536 tick_program_event(cpu_base->expires_next, 1);
537}
538
539/*
540 * Shared reprogramming for clock_realtime and clock_monotonic
541 *
542 * When a timer is enqueued and expires earlier than the already enqueued
543 * timers, we have to check, whether it expires earlier than the timer for
544 * which the clock event device was armed.
545 *
546 * Called with interrupts disabled and base->cpu_base.lock held
547 */
548static int hrtimer_reprogram(struct hrtimer *timer,
549 struct hrtimer_clock_base *base)
550{
551 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
552 ktime_t expires = ktime_sub(timer->expires, base->offset);
553 int res;
554
Thomas Gleixner63070a72008-02-14 00:58:36 +0100555 WARN_ON_ONCE(timer->expires.tv64 < 0);
556
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800557 /*
558 * When the callback is running, we do not reprogram the clock event
559 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200560 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800561 * reprogramming is handled either by the softirq, which called the
562 * callback or at the end of the hrtimer_interrupt.
563 */
564 if (hrtimer_callback_running(timer))
565 return 0;
566
Thomas Gleixner63070a72008-02-14 00:58:36 +0100567 /*
568 * CLOCK_REALTIME timer might be requested with an absolute
569 * expiry time which is less than base->offset. Nothing wrong
570 * about that, just avoid to call into the tick code, which
571 * has now objections against negative expiry values.
572 */
573 if (expires.tv64 < 0)
574 return -ETIME;
575
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800576 if (expires.tv64 >= expires_next->tv64)
577 return 0;
578
579 /*
580 * Clockevents returns -ETIME, when the event was in the past.
581 */
582 res = tick_program_event(expires, 0);
583 if (!IS_ERR_VALUE(res))
584 *expires_next = expires;
585 return res;
586}
587
588
589/*
590 * Retrigger next event is called after clock was set
591 *
592 * Called with interrupts disabled via on_each_cpu()
593 */
594static void retrigger_next_event(void *arg)
595{
596 struct hrtimer_cpu_base *base;
597 struct timespec realtime_offset;
598 unsigned long seq;
599
600 if (!hrtimer_hres_active())
601 return;
602
603 do {
604 seq = read_seqbegin(&xtime_lock);
605 set_normalized_timespec(&realtime_offset,
606 -wall_to_monotonic.tv_sec,
607 -wall_to_monotonic.tv_nsec);
608 } while (read_seqretry(&xtime_lock, seq));
609
610 base = &__get_cpu_var(hrtimer_bases);
611
612 /* Adjust CLOCK_REALTIME offset */
613 spin_lock(&base->lock);
614 base->clock_base[CLOCK_REALTIME].offset =
615 timespec_to_ktime(realtime_offset);
616
617 hrtimer_force_reprogram(base);
618 spin_unlock(&base->lock);
619}
620
621/*
622 * Clock realtime was set
623 *
624 * Change the offset of the realtime clock vs. the monotonic
625 * clock.
626 *
627 * We might have to reprogram the high resolution timer interrupt. On
628 * SMP we call the architecture specific code to retrigger _all_ high
629 * resolution timer interrupts. On UP we just disable interrupts and
630 * call the high resolution interrupt code.
631 */
632void clock_was_set(void)
633{
634 /* Retrigger the CPU local events everywhere */
635 on_each_cpu(retrigger_next_event, NULL, 0, 1);
636}
637
638/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200639 * During resume we might have to reprogram the high resolution timer
640 * interrupt (on the local CPU):
641 */
642void hres_timers_resume(void)
643{
644 WARN_ON_ONCE(num_online_cpus() > 1);
645
646 /* Retrigger the CPU local events: */
647 retrigger_next_event(NULL);
648}
649
650/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800651 * Initialize the high resolution related parts of cpu_base
652 */
653static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
654{
655 base->expires_next.tv64 = KTIME_MAX;
656 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800657}
658
659/*
660 * Initialize the high resolution related parts of a hrtimer
661 */
662static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
663{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800664}
665
666/*
667 * When High resolution timers are active, try to reprogram. Note, that in case
668 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
669 * check happens. The timer gets enqueued into the rbtree. The reprogramming
670 * and expiry check is done in the hrtimer_interrupt or in the softirq.
671 */
672static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
673 struct hrtimer_clock_base *base)
674{
675 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
676
677 /* Timer is expired, act upon the callback mode */
678 switch(timer->cb_mode) {
679 case HRTIMER_CB_IRQSAFE_NO_RESTART:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700680 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800681 /*
682 * We can call the callback from here. No restart
683 * happens, so no danger of recursion
684 */
685 BUG_ON(timer->function(timer) != HRTIMER_NORESTART);
686 return 1;
687 case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:
688 /*
689 * This is solely for the sched tick emulation with
690 * dynamic tick support to ensure that we do not
691 * restart the tick right on the edge and end up with
692 * the tick timer in the softirq ! The calling site
693 * takes care of this.
694 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700695 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800696 return 1;
697 case HRTIMER_CB_IRQSAFE:
698 case HRTIMER_CB_SOFTIRQ:
699 /*
700 * Move everything else into the softirq pending list !
701 */
702 list_add_tail(&timer->cb_entry,
703 &base->cpu_base->cb_pending);
704 timer->state = HRTIMER_STATE_PENDING;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800705 return 1;
706 default:
707 BUG();
708 }
709 }
710 return 0;
711}
712
713/*
714 * Switch to high resolution mode
715 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800716static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800717{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700718 int cpu = smp_processor_id();
719 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800720 unsigned long flags;
721
722 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800723 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800724
725 local_irq_save(flags);
726
727 if (tick_init_highres()) {
728 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700729 printk(KERN_WARNING "Could not switch to high resolution "
730 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800731 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800732 }
733 base->hres_active = 1;
734 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
735 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
736
737 tick_setup_sched_timer();
738
739 /* "Retrigger" the interrupt to get things going */
740 retrigger_next_event(NULL);
741 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100742 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800743 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800744 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800745}
746
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200747static inline void hrtimer_raise_softirq(void)
748{
749 raise_softirq(HRTIMER_SOFTIRQ);
750}
751
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800752#else
753
754static inline int hrtimer_hres_active(void) { return 0; }
755static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800756static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800757static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
758static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
759 struct hrtimer_clock_base *base)
760{
761 return 0;
762}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800763static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
764static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100765static inline int hrtimer_reprogram(struct hrtimer *timer,
766 struct hrtimer_clock_base *base)
767{
768 return 0;
769}
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200770static inline void hrtimer_raise_softirq(void) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800771
772#endif /* CONFIG_HIGH_RES_TIMERS */
773
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800774#ifdef CONFIG_TIMER_STATS
775void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
776{
777 if (timer->start_site)
778 return;
779
780 timer->start_site = addr;
781 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
782 timer->start_pid = current->pid;
783}
784#endif
785
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800786/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200787 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800788 */
789static inline
790void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
791{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800792 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800793}
794
795/**
796 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800797 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800798 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800799 * @interval: the interval to forward
800 *
801 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700802 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800803 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800804u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800805{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800806 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800807 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800808
809 delta = ktime_sub(now, timer->expires);
810
811 if (delta.tv64 < 0)
812 return 0;
813
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100814 if (interval.tv64 < timer->base->resolution.tv64)
815 interval.tv64 = timer->base->resolution.tv64;
816
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800818 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800819
820 orun = ktime_divns(delta, incr);
821 timer->expires = ktime_add_ns(timer->expires, incr * orun);
822 if (timer->expires.tv64 > now.tv64)
823 return orun;
824 /*
825 * This (and the ktime_add() below) is the
826 * correction for exact:
827 */
828 orun++;
829 }
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100830 timer->expires = ktime_add_safe(timer->expires, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800831
832 return orun;
833}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700834EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800835
836/*
837 * enqueue_hrtimer - internal function to (re)start a timer
838 *
839 * The timer is inserted in expiry order. Insertion into the
840 * red black tree is O(log(n)). Must hold the base lock.
841 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800842static void enqueue_hrtimer(struct hrtimer *timer,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800843 struct hrtimer_clock_base *base, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800844{
845 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800846 struct rb_node *parent = NULL;
847 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700848 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800849
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700850 debug_hrtimer_activate(timer);
851
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852 /*
853 * Find the right place in the rbtree:
854 */
855 while (*link) {
856 parent = *link;
857 entry = rb_entry(parent, struct hrtimer, node);
858 /*
859 * We dont care about collisions. Nodes with
860 * the same expiry time stay together.
861 */
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700862 if (timer->expires.tv64 < entry->expires.tv64) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800863 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700864 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800865 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700866 leftmost = 0;
867 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800868 }
869
870 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100871 * Insert the timer to the rbtree and check whether it
872 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800873 */
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700874 if (leftmost) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800875 /*
876 * Reprogram the clock event device. When the timer is already
877 * expired hrtimer_enqueue_reprogram has either called the
878 * callback or added it to the pending list and raised the
879 * softirq.
880 *
881 * This is a NOP for !HIGHRES
882 */
883 if (reprogram && hrtimer_enqueue_reprogram(timer, base))
884 return;
885
886 base->first = &timer->node;
887 }
888
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800889 rb_link_node(&timer->node, parent, link);
890 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800891 /*
892 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
893 * state of a possibly running callback.
894 */
895 timer->state |= HRTIMER_STATE_ENQUEUED;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100896}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800897
898/*
899 * __remove_hrtimer - internal function to remove a timer
900 *
901 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800902 *
903 * High resolution timer mode reprograms the clock event device when the
904 * timer is the one which expires next. The caller can disable this by setting
905 * reprogram to zero. This is useful, when the context does a reprogramming
906 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800907 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800908static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800909 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800910 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800911{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800912 /* High res. callback list. NOP for !HIGHRES */
913 if (hrtimer_cb_pending(timer))
914 hrtimer_remove_cb_pending(timer);
915 else {
916 /*
917 * Remove the timer from the rbtree and replace the
918 * first entry pointer if necessary.
919 */
920 if (base->first == &timer->node) {
921 base->first = rb_next(&timer->node);
922 /* Reprogram the clock event device. if enabled */
923 if (reprogram && hrtimer_hres_active())
924 hrtimer_force_reprogram(base->cpu_base);
925 }
926 rb_erase(&timer->node, &base->active);
927 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800928 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800929}
930
931/*
932 * remove hrtimer, called with base lock held
933 */
934static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800935remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800936{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800937 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800938 int reprogram;
939
940 /*
941 * Remove the timer and force reprogramming when high
942 * resolution mode is active and the timer is on the current
943 * CPU. If we remove a timer on another CPU, reprogramming is
944 * skipped. The interrupt event on this CPU is fired and
945 * reprogramming happens in the interrupt handler. This is a
946 * rare case and less expensive than a smp call.
947 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700948 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800949 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800950 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
951 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
952 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800953 return 1;
954 }
955 return 0;
956}
957
958/**
959 * hrtimer_start - (re)start an relative timer on the current CPU
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800960 * @timer: the timer to be added
961 * @tim: expiry time
962 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
963 *
964 * Returns:
965 * 0 on success
966 * 1 when the timer was active
967 */
968int
969hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
970{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800971 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800972 unsigned long flags;
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200973 int ret, raise;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800974
975 base = lock_hrtimer_base(timer, &flags);
976
977 /* Remove an active timer from the queue: */
978 ret = remove_hrtimer(timer, base);
979
980 /* Switch the timer base, if necessary: */
981 new_base = switch_hrtimer_base(timer, base);
982
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800983 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100984 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800985 /*
986 * CONFIG_TIME_LOW_RES is a temporary way for architectures
987 * to signal that they simply return xtime in
988 * do_gettimeoffset(). In this case we want to round up by
989 * resolution when starting a relative timer, to avoid short
990 * timeouts. This will go away with the GTOD framework.
991 */
992#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100993 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800994#endif
995 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700996
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800997 timer->expires = tim;
998
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800999 timer_stats_hrtimer_set_start_info(timer);
1000
Ingo Molnar935c6312007-03-28 13:17:18 +02001001 /*
1002 * Only allow reprogramming if the new base is on this CPU.
1003 * (it might still be on another CPU if the timer was pending)
1004 */
1005 enqueue_hrtimer(timer, new_base,
1006 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001007
Thomas Gleixner0c96c592008-04-28 09:23:24 +02001008 /*
1009 * The timer may be expired and moved to the cb_pending
1010 * list. We can not raise the softirq with base lock held due
1011 * to a possible deadlock with runqueue lock.
1012 */
1013 raise = timer->state == HRTIMER_STATE_PENDING;
1014
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001015 unlock_hrtimer_base(timer, &flags);
1016
Thomas Gleixner0c96c592008-04-28 09:23:24 +02001017 if (raise)
1018 hrtimer_raise_softirq();
1019
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001020 return ret;
1021}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001022EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001023
1024/**
1025 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001026 * @timer: hrtimer to stop
1027 *
1028 * Returns:
1029 * 0 when the timer was not active
1030 * 1 when the timer was active
1031 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001032 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001033 */
1034int hrtimer_try_to_cancel(struct hrtimer *timer)
1035{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001036 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001037 unsigned long flags;
1038 int ret = -1;
1039
1040 base = lock_hrtimer_base(timer, &flags);
1041
Thomas Gleixner303e9672007-02-16 01:27:51 -08001042 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001043 ret = remove_hrtimer(timer, base);
1044
1045 unlock_hrtimer_base(timer, &flags);
1046
1047 return ret;
1048
1049}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001050EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001051
1052/**
1053 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001054 * @timer: the timer to be cancelled
1055 *
1056 * Returns:
1057 * 0 when the timer was not active
1058 * 1 when the timer was active
1059 */
1060int hrtimer_cancel(struct hrtimer *timer)
1061{
1062 for (;;) {
1063 int ret = hrtimer_try_to_cancel(timer);
1064
1065 if (ret >= 0)
1066 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001067 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001068 }
1069}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001070EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001071
1072/**
1073 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001074 * @timer: the timer to read
1075 */
1076ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1077{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001078 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001079 unsigned long flags;
1080 ktime_t rem;
1081
1082 base = lock_hrtimer_base(timer, &flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001083 rem = ktime_sub(timer->expires, base->get_time());
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001084 unlock_hrtimer_base(timer, &flags);
1085
1086 return rem;
1087}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001088EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001089
Thomas Gleixnerfd064b92007-02-16 01:27:47 -08001090#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
Tony Lindgren69239742006-03-06 15:42:45 -08001091/**
1092 * hrtimer_get_next_event - get the time until next expiry event
1093 *
1094 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1095 * is pending.
1096 */
1097ktime_t hrtimer_get_next_event(void)
1098{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001099 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1100 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001101 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1102 unsigned long flags;
1103 int i;
1104
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001105 spin_lock_irqsave(&cpu_base->lock, flags);
1106
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001107 if (!hrtimer_hres_active()) {
1108 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1109 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001110
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001111 if (!base->first)
1112 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001113
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001114 timer = rb_entry(base->first, struct hrtimer, node);
1115 delta.tv64 = timer->expires.tv64;
1116 delta = ktime_sub(delta, base->get_time());
1117 if (delta.tv64 < mindelta.tv64)
1118 mindelta.tv64 = delta.tv64;
1119 }
Tony Lindgren69239742006-03-06 15:42:45 -08001120 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001121
1122 spin_unlock_irqrestore(&cpu_base->lock, flags);
1123
Tony Lindgren69239742006-03-06 15:42:45 -08001124 if (mindelta.tv64 < 0)
1125 mindelta.tv64 = 0;
1126 return mindelta;
1127}
1128#endif
1129
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001130static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1131 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001132{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001133 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001134
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001135 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001136
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001137 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001138
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001139 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001140 clock_id = CLOCK_MONOTONIC;
1141
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001142 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001143 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001144 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001145
1146#ifdef CONFIG_TIMER_STATS
1147 timer->start_site = NULL;
1148 timer->start_pid = -1;
1149 memset(timer->start_comm, 0, TASK_COMM_LEN);
1150#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001151}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001152
1153/**
1154 * hrtimer_init - initialize a timer to the given clock
1155 * @timer: the timer to be initialized
1156 * @clock_id: the clock to be used
1157 * @mode: timer mode abs/rel
1158 */
1159void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1160 enum hrtimer_mode mode)
1161{
1162 debug_hrtimer_init(timer);
1163 __hrtimer_init(timer, clock_id, mode);
1164}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001165EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001166
1167/**
1168 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001169 * @which_clock: which clock to query
1170 * @tp: pointer to timespec variable to store the resolution
1171 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001172 * Store the resolution of the clock selected by @which_clock in the
1173 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001174 */
1175int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1176{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001177 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001178
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001179 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1180 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001181
1182 return 0;
1183}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001184EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001185
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001186static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001187{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001188 spin_lock_irq(&cpu_base->lock);
1189
1190 while (!list_empty(&cpu_base->cb_pending)) {
1191 enum hrtimer_restart (*fn)(struct hrtimer *);
1192 struct hrtimer *timer;
1193 int restart;
1194
1195 timer = list_entry(cpu_base->cb_pending.next,
1196 struct hrtimer, cb_entry);
1197
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001198 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001199 timer_stats_account_hrtimer(timer);
1200
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001201 fn = timer->function;
1202 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
1203 spin_unlock_irq(&cpu_base->lock);
1204
1205 restart = fn(timer);
1206
1207 spin_lock_irq(&cpu_base->lock);
1208
1209 timer->state &= ~HRTIMER_STATE_CALLBACK;
1210 if (restart == HRTIMER_RESTART) {
1211 BUG_ON(hrtimer_active(timer));
1212 /*
1213 * Enqueue the timer, allow reprogramming of the event
1214 * device
1215 */
1216 enqueue_hrtimer(timer, timer->base, 1);
1217 } else if (hrtimer_active(timer)) {
1218 /*
1219 * If the timer was rearmed on another CPU, reprogram
1220 * the event device.
1221 */
Bodo Stroesserd7b41a22008-04-26 14:10:16 -07001222 struct hrtimer_clock_base *base = timer->base;
1223
1224 if (base->first == &timer->node &&
1225 hrtimer_reprogram(timer, base)) {
1226 /*
1227 * Timer is expired. Thus move it from tree to
1228 * pending list again.
1229 */
1230 __remove_hrtimer(timer, base,
1231 HRTIMER_STATE_PENDING, 0);
1232 list_add_tail(&timer->cb_entry,
1233 &base->cpu_base->cb_pending);
1234 }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001235 }
1236 }
1237 spin_unlock_irq(&cpu_base->lock);
1238}
1239
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001240static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001241{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001242 struct hrtimer_clock_base *base = timer->base;
1243 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1244 enum hrtimer_restart (*fn)(struct hrtimer *);
1245 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001246
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001247 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001248 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1249 timer_stats_account_hrtimer(timer);
Dimitri Sivanich3055add2006-03-31 02:31:20 -08001250
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001251 fn = timer->function;
1252 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
1253 /*
1254 * Used for scheduler timers, avoid lock inversion with
1255 * rq->lock and tasklist_lock.
1256 *
1257 * These timers are required to deal with enqueue expiry
1258 * themselves and are not allowed to migrate.
1259 */
1260 spin_unlock(&cpu_base->lock);
1261 restart = fn(timer);
1262 spin_lock(&cpu_base->lock);
1263 } else
Roman Zippel05cfb612006-03-26 01:38:12 -08001264 restart = fn(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001265
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001266 /*
1267 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1268 * reprogramming of the event hardware. This happens at the end of this
1269 * function anyway.
1270 */
1271 if (restart != HRTIMER_NORESTART) {
1272 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1273 enqueue_hrtimer(timer, base, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001274 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001275 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001276}
1277
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001278#ifdef CONFIG_HIGH_RES_TIMERS
1279
1280/*
1281 * High resolution timer interrupt
1282 * Called with interrupts disabled
1283 */
1284void hrtimer_interrupt(struct clock_event_device *dev)
1285{
1286 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1287 struct hrtimer_clock_base *base;
1288 ktime_t expires_next, now;
1289 int i, raise = 0;
1290
1291 BUG_ON(!cpu_base->hres_active);
1292 cpu_base->nr_events++;
1293 dev->next_event.tv64 = KTIME_MAX;
1294
1295 retry:
1296 now = ktime_get();
1297
1298 expires_next.tv64 = KTIME_MAX;
1299
1300 base = cpu_base->clock_base;
1301
1302 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1303 ktime_t basenow;
1304 struct rb_node *node;
1305
1306 spin_lock(&cpu_base->lock);
1307
1308 basenow = ktime_add(now, base->offset);
1309
1310 while ((node = base->first)) {
1311 struct hrtimer *timer;
1312
1313 timer = rb_entry(node, struct hrtimer, node);
1314
1315 if (basenow.tv64 < timer->expires.tv64) {
1316 ktime_t expires;
1317
1318 expires = ktime_sub(timer->expires,
1319 base->offset);
1320 if (expires.tv64 < expires_next.tv64)
1321 expires_next = expires;
1322 break;
1323 }
1324
1325 /* Move softirq callbacks to the pending list */
1326 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1327 __remove_hrtimer(timer, base,
1328 HRTIMER_STATE_PENDING, 0);
1329 list_add_tail(&timer->cb_entry,
1330 &base->cpu_base->cb_pending);
1331 raise = 1;
1332 continue;
1333 }
1334
1335 __run_hrtimer(timer);
1336 }
1337 spin_unlock(&cpu_base->lock);
1338 base++;
1339 }
1340
1341 cpu_base->expires_next = expires_next;
1342
1343 /* Reprogramming necessary ? */
1344 if (expires_next.tv64 != KTIME_MAX) {
1345 if (tick_program_event(expires_next, 0))
1346 goto retry;
1347 }
1348
1349 /* Raise softirq ? */
1350 if (raise)
1351 raise_softirq(HRTIMER_SOFTIRQ);
1352}
1353
1354static void run_hrtimer_softirq(struct softirq_action *h)
1355{
1356 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
1357}
1358
1359#endif /* CONFIG_HIGH_RES_TIMERS */
1360
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001361/*
1362 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001363 *
1364 * For HRT its the fall back code to run the softirq in the timer
1365 * softirq context in case the hrtimer initialization failed or has
1366 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001367 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001368void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001369{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001370 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001371
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001372 if (hrtimer_hres_active())
1373 return;
1374
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001375 /*
1376 * This _is_ ugly: We have to check in the softirq context,
1377 * whether we can switch to highres and / or nohz mode. The
1378 * clocksource switch happens in the timer interrupt with
1379 * xtime_lock held. Notification from there only sets the
1380 * check bit in the tick_oneshot code, otherwise we might
1381 * deadlock vs. xtime_lock.
1382 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001383 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001384 hrtimer_switch_to_hres();
1385
1386 run_hrtimer_pending(cpu_base);
1387}
1388
1389/*
1390 * Called from hardirq context every jiffy
1391 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001392void hrtimer_run_queues(void)
1393{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001394 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001395 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001396 struct hrtimer_clock_base *base;
1397 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001398
1399 if (hrtimer_hres_active())
1400 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001401
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001402 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1403 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001404
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001405 if (!base->first)
1406 continue;
1407
Thomas Gleixner259aae82008-04-19 21:31:26 +02001408 if (base->get_softirq_time)
1409 base->softirq_time = base->get_softirq_time();
1410 else if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001411 hrtimer_get_softirq_time(cpu_base);
1412 gettime = 0;
1413 }
1414
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001415 spin_lock(&cpu_base->lock);
1416
1417 while ((node = base->first)) {
1418 struct hrtimer *timer;
1419
1420 timer = rb_entry(node, struct hrtimer, node);
1421 if (base->softirq_time.tv64 <= timer->expires.tv64)
1422 break;
1423
1424 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1425 __remove_hrtimer(timer, base,
1426 HRTIMER_STATE_PENDING, 0);
1427 list_add_tail(&timer->cb_entry,
1428 &base->cpu_base->cb_pending);
1429 continue;
1430 }
1431
1432 __run_hrtimer(timer);
1433 }
1434 spin_unlock(&cpu_base->lock);
1435 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001436}
1437
1438/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001439 * Sleep related functions:
1440 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001441static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001442{
1443 struct hrtimer_sleeper *t =
1444 container_of(timer, struct hrtimer_sleeper, timer);
1445 struct task_struct *task = t->task;
1446
1447 t->task = NULL;
1448 if (task)
1449 wake_up_process(task);
1450
1451 return HRTIMER_NORESTART;
1452}
1453
Ingo Molnar36c8b582006-07-03 00:25:41 -07001454void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001455{
1456 sl->timer.function = hrtimer_wakeup;
1457 sl->task = task;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001458#ifdef CONFIG_HIGH_RES_TIMERS
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001459 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001460#endif
Thomas Gleixner00362e32006-03-31 02:31:17 -08001461}
1462
Thomas Gleixner669d7862006-03-31 02:31:19 -08001463static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001464{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001465 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001466
Roman Zippel432569b2006-03-26 01:38:08 -08001467 do {
1468 set_current_state(TASK_INTERRUPTIBLE);
1469 hrtimer_start(&t->timer, t->timer.expires, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001470 if (!hrtimer_active(&t->timer))
1471 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001472
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001473 if (likely(t->task))
1474 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001475
Thomas Gleixner669d7862006-03-31 02:31:19 -08001476 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001477 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001478
Thomas Gleixner669d7862006-03-31 02:31:19 -08001479 } while (t->task && !signal_pending(current));
1480
Peter Zijlstra3588a082008-02-01 17:45:13 +01001481 __set_current_state(TASK_RUNNING);
1482
Thomas Gleixner669d7862006-03-31 02:31:19 -08001483 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001484}
1485
Oleg Nesterov080344b2008-02-01 17:29:05 +03001486static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1487{
1488 struct timespec rmt;
1489 ktime_t rem;
1490
1491 rem = ktime_sub(timer->expires, timer->base->get_time());
1492 if (rem.tv64 <= 0)
1493 return 0;
1494 rmt = ktime_to_timespec(rem);
1495
1496 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1497 return -EFAULT;
1498
1499 return 1;
1500}
1501
Toyo Abe1711ef32006-09-29 02:00:28 -07001502long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001503{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001504 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001505 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001506 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001507
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001508 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1509 HRTIMER_MODE_ABS);
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001510 t.timer.expires.tv64 = restart->nanosleep.expires;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001511
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001512 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001513 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001514
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001515 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001516 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001517 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001518 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001519 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001520 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001521
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001522 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001523 ret = -ERESTART_RESTARTBLOCK;
1524out:
1525 destroy_hrtimer_on_stack(&t.timer);
1526 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001527}
1528
Oleg Nesterov080344b2008-02-01 17:29:05 +03001529long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001530 const enum hrtimer_mode mode, const clockid_t clockid)
1531{
1532 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001533 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001534 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001535
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001536 hrtimer_init_on_stack(&t.timer, clockid, mode);
Roman Zippel432569b2006-03-26 01:38:08 -08001537 t.timer.expires = timespec_to_ktime(*rqtp);
1538 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001539 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001540
George Anzinger7978672c2006-02-01 03:05:11 -08001541 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001542 if (mode == HRTIMER_MODE_ABS) {
1543 ret = -ERESTARTNOHAND;
1544 goto out;
1545 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001546
Roman Zippel432569b2006-03-26 01:38:08 -08001547 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001548 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001549 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001550 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001551 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001552
1553 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001554 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001555 restart->nanosleep.index = t.timer.base->index;
1556 restart->nanosleep.rmtp = rmtp;
1557 restart->nanosleep.expires = t.timer.expires.tv64;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001558
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001559 ret = -ERESTART_RESTARTBLOCK;
1560out:
1561 destroy_hrtimer_on_stack(&t.timer);
1562 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001563}
1564
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001565asmlinkage long
1566sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1567{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001568 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001569
1570 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1571 return -EFAULT;
1572
1573 if (!timespec_valid(&tu))
1574 return -EINVAL;
1575
Oleg Nesterov080344b2008-02-01 17:29:05 +03001576 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001577}
1578
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001579/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001580 * Functions related to boot-time initialization:
1581 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001582static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001583{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001584 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001585 int i;
1586
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001587 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001588
1589 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1590 cpu_base->clock_base[i].cpu_base = cpu_base;
1591
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001592 INIT_LIST_HEAD(&cpu_base->cb_pending);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001593 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001594}
1595
1596#ifdef CONFIG_HOTPLUG_CPU
1597
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001598static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1599 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001600{
1601 struct hrtimer *timer;
1602 struct rb_node *node;
1603
1604 while ((node = rb_first(&old_base->active))) {
1605 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001606 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001607 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001608 __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001609 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001610 /*
1611 * Enqueue the timer. Allow reprogramming of the event device
1612 */
1613 enqueue_hrtimer(timer, new_base, 1);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001614 }
1615}
1616
1617static void migrate_hrtimers(int cpu)
1618{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001619 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001620 int i;
1621
1622 BUG_ON(cpu_online(cpu));
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001623 old_base = &per_cpu(hrtimer_bases, cpu);
1624 new_base = &get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001625
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001626 tick_cancel_sched_timer(cpu);
1627
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001628 local_irq_disable();
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001629 spin_lock(&new_base->lock);
1630 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001631
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001632 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001633 migrate_hrtimer_list(&old_base->clock_base[i],
1634 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001635 }
1636
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001637 spin_unlock(&old_base->lock);
1638 spin_unlock(&new_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001639 local_irq_enable();
1640 put_cpu_var(hrtimer_bases);
1641}
1642#endif /* CONFIG_HOTPLUG_CPU */
1643
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001644static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001645 unsigned long action, void *hcpu)
1646{
David Miller7713a7d2007-07-16 17:17:44 -07001647 unsigned int cpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001648
1649 switch (action) {
1650
1651 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001652 case CPU_UP_PREPARE_FROZEN:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001653 init_hrtimers_cpu(cpu);
1654 break;
1655
1656#ifdef CONFIG_HOTPLUG_CPU
1657 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001658 case CPU_DEAD_FROZEN:
Thomas Gleixnerd316c572007-02-16 01:28:00 -08001659 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001660 migrate_hrtimers(cpu);
1661 break;
1662#endif
1663
1664 default:
1665 break;
1666 }
1667
1668 return NOTIFY_OK;
1669}
1670
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001671static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001672 .notifier_call = hrtimer_cpu_notify,
1673};
1674
1675void __init hrtimers_init(void)
1676{
1677 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1678 (void *)(long)smp_processor_id());
1679 register_cpu_notifier(&hrtimers_nb);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001680#ifdef CONFIG_HIGH_RES_TIMERS
1681 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq, NULL);
1682#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001683}
1684