blob: b09c7a27631de64956d51bb213e6283e5ee36631 [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{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300303 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800304 int sft = 0;
305
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300306 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800307 /* Make sure the divisor is less than 2^32: */
308 while (div >> 32) {
309 sft++;
310 div >>= 1;
311 }
312 dclc >>= sft;
313 do_div(dclc, (unsigned long) div);
314
Davide Libenzi4d672e72008-02-04 22:27:26 -0800315 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800316}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800317#endif /* BITS_PER_LONG >= 64 */
318
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100319/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100320 * Add two ktime values and do a safety check for overflow:
321 */
322ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
323{
324 ktime_t res = ktime_add(lhs, rhs);
325
326 /*
327 * We use KTIME_SEC_MAX here, the maximum timeout which we can
328 * return to user space in a timespec:
329 */
330 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
331 res = ktime_set(KTIME_SEC_MAX, 0);
332
333 return res;
334}
335
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700336#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
337
338static struct debug_obj_descr hrtimer_debug_descr;
339
340/*
341 * fixup_init is called when:
342 * - an active object is initialized
343 */
344static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
345{
346 struct hrtimer *timer = addr;
347
348 switch (state) {
349 case ODEBUG_STATE_ACTIVE:
350 hrtimer_cancel(timer);
351 debug_object_init(timer, &hrtimer_debug_descr);
352 return 1;
353 default:
354 return 0;
355 }
356}
357
358/*
359 * fixup_activate is called when:
360 * - an active object is activated
361 * - an unknown object is activated (might be a statically initialized object)
362 */
363static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
364{
365 switch (state) {
366
367 case ODEBUG_STATE_NOTAVAILABLE:
368 WARN_ON_ONCE(1);
369 return 0;
370
371 case ODEBUG_STATE_ACTIVE:
372 WARN_ON(1);
373
374 default:
375 return 0;
376 }
377}
378
379/*
380 * fixup_free is called when:
381 * - an active object is freed
382 */
383static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
384{
385 struct hrtimer *timer = addr;
386
387 switch (state) {
388 case ODEBUG_STATE_ACTIVE:
389 hrtimer_cancel(timer);
390 debug_object_free(timer, &hrtimer_debug_descr);
391 return 1;
392 default:
393 return 0;
394 }
395}
396
397static struct debug_obj_descr hrtimer_debug_descr = {
398 .name = "hrtimer",
399 .fixup_init = hrtimer_fixup_init,
400 .fixup_activate = hrtimer_fixup_activate,
401 .fixup_free = hrtimer_fixup_free,
402};
403
404static inline void debug_hrtimer_init(struct hrtimer *timer)
405{
406 debug_object_init(timer, &hrtimer_debug_descr);
407}
408
409static inline void debug_hrtimer_activate(struct hrtimer *timer)
410{
411 debug_object_activate(timer, &hrtimer_debug_descr);
412}
413
414static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
415{
416 debug_object_deactivate(timer, &hrtimer_debug_descr);
417}
418
419static inline void debug_hrtimer_free(struct hrtimer *timer)
420{
421 debug_object_free(timer, &hrtimer_debug_descr);
422}
423
424static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
425 enum hrtimer_mode mode);
426
427void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
428 enum hrtimer_mode mode)
429{
430 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
431 __hrtimer_init(timer, clock_id, mode);
432}
433
434void destroy_hrtimer_on_stack(struct hrtimer *timer)
435{
436 debug_object_free(timer, &hrtimer_debug_descr);
437}
438
439#else
440static inline void debug_hrtimer_init(struct hrtimer *timer) { }
441static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
442static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
443#endif
444
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800445/* High resolution timer related functions */
446#ifdef CONFIG_HIGH_RES_TIMERS
447
448/*
449 * High resolution timer enabled ?
450 */
451static int hrtimer_hres_enabled __read_mostly = 1;
452
453/*
454 * Enable / Disable high resolution mode
455 */
456static int __init setup_hrtimer_hres(char *str)
457{
458 if (!strcmp(str, "off"))
459 hrtimer_hres_enabled = 0;
460 else if (!strcmp(str, "on"))
461 hrtimer_hres_enabled = 1;
462 else
463 return 0;
464 return 1;
465}
466
467__setup("highres=", setup_hrtimer_hres);
468
469/*
470 * hrtimer_high_res_enabled - query, if the highres mode is enabled
471 */
472static inline int hrtimer_is_hres_enabled(void)
473{
474 return hrtimer_hres_enabled;
475}
476
477/*
478 * Is the high resolution mode active ?
479 */
480static inline int hrtimer_hres_active(void)
481{
482 return __get_cpu_var(hrtimer_bases).hres_active;
483}
484
485/*
486 * Reprogram the event source with checking both queues for the
487 * next event
488 * Called with interrupts disabled and base->lock held
489 */
490static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
491{
492 int i;
493 struct hrtimer_clock_base *base = cpu_base->clock_base;
494 ktime_t expires;
495
496 cpu_base->expires_next.tv64 = KTIME_MAX;
497
498 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
499 struct hrtimer *timer;
500
501 if (!base->first)
502 continue;
503 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700504 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800505 if (expires.tv64 < cpu_base->expires_next.tv64)
506 cpu_base->expires_next = expires;
507 }
508
509 if (cpu_base->expires_next.tv64 != KTIME_MAX)
510 tick_program_event(cpu_base->expires_next, 1);
511}
512
513/*
514 * Shared reprogramming for clock_realtime and clock_monotonic
515 *
516 * When a timer is enqueued and expires earlier than the already enqueued
517 * timers, we have to check, whether it expires earlier than the timer for
518 * which the clock event device was armed.
519 *
520 * Called with interrupts disabled and base->cpu_base.lock held
521 */
522static int hrtimer_reprogram(struct hrtimer *timer,
523 struct hrtimer_clock_base *base)
524{
525 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700526 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800527 int res;
528
Arjan van de Vencc584b22008-09-01 15:02:30 -0700529 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100530
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800531 /*
532 * When the callback is running, we do not reprogram the clock event
533 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200534 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800535 * reprogramming is handled either by the softirq, which called the
536 * callback or at the end of the hrtimer_interrupt.
537 */
538 if (hrtimer_callback_running(timer))
539 return 0;
540
Thomas Gleixner63070a72008-02-14 00:58:36 +0100541 /*
542 * CLOCK_REALTIME timer might be requested with an absolute
543 * expiry time which is less than base->offset. Nothing wrong
544 * about that, just avoid to call into the tick code, which
545 * has now objections against negative expiry values.
546 */
547 if (expires.tv64 < 0)
548 return -ETIME;
549
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800550 if (expires.tv64 >= expires_next->tv64)
551 return 0;
552
553 /*
554 * Clockevents returns -ETIME, when the event was in the past.
555 */
556 res = tick_program_event(expires, 0);
557 if (!IS_ERR_VALUE(res))
558 *expires_next = expires;
559 return res;
560}
561
562
563/*
564 * Retrigger next event is called after clock was set
565 *
566 * Called with interrupts disabled via on_each_cpu()
567 */
568static void retrigger_next_event(void *arg)
569{
570 struct hrtimer_cpu_base *base;
571 struct timespec realtime_offset;
572 unsigned long seq;
573
574 if (!hrtimer_hres_active())
575 return;
576
577 do {
578 seq = read_seqbegin(&xtime_lock);
579 set_normalized_timespec(&realtime_offset,
580 -wall_to_monotonic.tv_sec,
581 -wall_to_monotonic.tv_nsec);
582 } while (read_seqretry(&xtime_lock, seq));
583
584 base = &__get_cpu_var(hrtimer_bases);
585
586 /* Adjust CLOCK_REALTIME offset */
587 spin_lock(&base->lock);
588 base->clock_base[CLOCK_REALTIME].offset =
589 timespec_to_ktime(realtime_offset);
590
591 hrtimer_force_reprogram(base);
592 spin_unlock(&base->lock);
593}
594
595/*
596 * Clock realtime was set
597 *
598 * Change the offset of the realtime clock vs. the monotonic
599 * clock.
600 *
601 * We might have to reprogram the high resolution timer interrupt. On
602 * SMP we call the architecture specific code to retrigger _all_ high
603 * resolution timer interrupts. On UP we just disable interrupts and
604 * call the high resolution interrupt code.
605 */
606void clock_was_set(void)
607{
608 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200609 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800610}
611
612/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200613 * During resume we might have to reprogram the high resolution timer
614 * interrupt (on the local CPU):
615 */
616void hres_timers_resume(void)
617{
Ingo Molnar995f0542007-04-07 12:05:00 +0200618 /* Retrigger the CPU local events: */
619 retrigger_next_event(NULL);
620}
621
622/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800623 * Initialize the high resolution related parts of cpu_base
624 */
625static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
626{
627 base->expires_next.tv64 = KTIME_MAX;
628 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800629}
630
631/*
632 * Initialize the high resolution related parts of a hrtimer
633 */
634static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
635{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800636}
637
Peter Zijlstraca109492008-11-25 12:43:51 +0100638static void __run_hrtimer(struct hrtimer *timer);
639
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800640/*
641 * When High resolution timers are active, try to reprogram. Note, that in case
642 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
643 * check happens. The timer gets enqueued into the rbtree. The reprogramming
644 * and expiry check is done in the hrtimer_interrupt or in the softirq.
645 */
646static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
647 struct hrtimer_clock_base *base)
648{
649 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstraca109492008-11-25 12:43:51 +0100650 /*
651 * XXX: recursion check?
652 * hrtimer_forward() should round up with timer granularity
653 * so that we never get into inf recursion here,
654 * it doesn't do that though
655 */
656 __run_hrtimer(timer);
657 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800658 }
659 return 0;
660}
661
662/*
663 * Switch to high resolution mode
664 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800665static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800666{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700667 int cpu = smp_processor_id();
668 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800669 unsigned long flags;
670
671 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800672 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800673
674 local_irq_save(flags);
675
676 if (tick_init_highres()) {
677 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700678 printk(KERN_WARNING "Could not switch to high resolution "
679 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800680 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800681 }
682 base->hres_active = 1;
683 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
684 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
685
686 tick_setup_sched_timer();
687
688 /* "Retrigger" the interrupt to get things going */
689 retrigger_next_event(NULL);
690 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100691 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800692 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800693 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800694}
695
696#else
697
698static inline int hrtimer_hres_active(void) { return 0; }
699static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800700static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800701static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
702static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
703 struct hrtimer_clock_base *base)
704{
705 return 0;
706}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800707static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
708static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100709static inline int hrtimer_reprogram(struct hrtimer *timer,
710 struct hrtimer_clock_base *base)
711{
712 return 0;
713}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800714
715#endif /* CONFIG_HIGH_RES_TIMERS */
716
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800717#ifdef CONFIG_TIMER_STATS
718void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
719{
720 if (timer->start_site)
721 return;
722
723 timer->start_site = addr;
724 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
725 timer->start_pid = current->pid;
726}
727#endif
728
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800729/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200730 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800731 */
732static inline
733void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
734{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800735 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800736}
737
738/**
739 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800740 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800741 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800742 * @interval: the interval to forward
743 *
744 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700745 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800746 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800747u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800748{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800749 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800750 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800751
Arjan van de Vencc584b22008-09-01 15:02:30 -0700752 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800753
754 if (delta.tv64 < 0)
755 return 0;
756
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100757 if (interval.tv64 < timer->base->resolution.tv64)
758 interval.tv64 = timer->base->resolution.tv64;
759
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800760 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800761 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800762
763 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700764 hrtimer_add_expires_ns(timer, incr * orun);
765 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800766 return orun;
767 /*
768 * This (and the ktime_add() below) is the
769 * correction for exact:
770 */
771 orun++;
772 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700773 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800774
775 return orun;
776}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700777EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800778
779/*
780 * enqueue_hrtimer - internal function to (re)start a timer
781 *
782 * The timer is inserted in expiry order. Insertion into the
783 * red black tree is O(log(n)). Must hold the base lock.
784 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800785static void enqueue_hrtimer(struct hrtimer *timer,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800786 struct hrtimer_clock_base *base, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800787{
788 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800789 struct rb_node *parent = NULL;
790 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700791 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800792
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700793 debug_hrtimer_activate(timer);
794
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800795 /*
796 * Find the right place in the rbtree:
797 */
798 while (*link) {
799 parent = *link;
800 entry = rb_entry(parent, struct hrtimer, node);
801 /*
802 * We dont care about collisions. Nodes with
803 * the same expiry time stay together.
804 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700805 if (hrtimer_get_expires_tv64(timer) <
806 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800807 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700808 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800809 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700810 leftmost = 0;
811 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812 }
813
814 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100815 * Insert the timer to the rbtree and check whether it
816 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817 */
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700818 if (leftmost) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800819 /*
820 * Reprogram the clock event device. When the timer is already
821 * expired hrtimer_enqueue_reprogram has either called the
822 * callback or added it to the pending list and raised the
823 * softirq.
824 *
825 * This is a NOP for !HIGHRES
826 */
827 if (reprogram && hrtimer_enqueue_reprogram(timer, base))
828 return;
829
830 base->first = &timer->node;
831 }
832
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800833 rb_link_node(&timer->node, parent, link);
834 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800835 /*
836 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
837 * state of a possibly running callback.
838 */
839 timer->state |= HRTIMER_STATE_ENQUEUED;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100840}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800841
842/*
843 * __remove_hrtimer - internal function to remove a timer
844 *
845 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800846 *
847 * High resolution timer mode reprograms the clock event device when the
848 * timer is the one which expires next. The caller can disable this by setting
849 * reprogram to zero. This is useful, when the context does a reprogramming
850 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800851 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800852static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800853 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800854 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800855{
Peter Zijlstraca109492008-11-25 12:43:51 +0100856 if (timer->state & HRTIMER_STATE_ENQUEUED) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800857 /*
858 * Remove the timer from the rbtree and replace the
859 * first entry pointer if necessary.
860 */
861 if (base->first == &timer->node) {
862 base->first = rb_next(&timer->node);
863 /* Reprogram the clock event device. if enabled */
864 if (reprogram && hrtimer_hres_active())
865 hrtimer_force_reprogram(base->cpu_base);
866 }
867 rb_erase(&timer->node, &base->active);
868 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800869 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800870}
871
872/*
873 * remove hrtimer, called with base lock held
874 */
875static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800876remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800877{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800878 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800879 int reprogram;
880
881 /*
882 * Remove the timer and force reprogramming when high
883 * resolution mode is active and the timer is on the current
884 * CPU. If we remove a timer on another CPU, reprogramming is
885 * skipped. The interrupt event on this CPU is fired and
886 * reprogramming happens in the interrupt handler. This is a
887 * rare case and less expensive than a smp call.
888 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700889 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800890 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800891 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
892 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
893 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800894 return 1;
895 }
896 return 0;
897}
898
899/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200900 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800901 * @timer: the timer to be added
902 * @tim: expiry time
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700903 * @delta_ns: "slack" range for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800904 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
905 *
906 * Returns:
907 * 0 on success
908 * 1 when the timer was active
909 */
910int
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700911hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
912 const enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800913{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800914 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800915 unsigned long flags;
Peter Zijlstraca109492008-11-25 12:43:51 +0100916 int ret;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800917
918 base = lock_hrtimer_base(timer, &flags);
919
920 /* Remove an active timer from the queue: */
921 ret = remove_hrtimer(timer, base);
922
923 /* Switch the timer base, if necessary: */
924 new_base = switch_hrtimer_base(timer, base);
925
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800926 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100927 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800928 /*
929 * CONFIG_TIME_LOW_RES is a temporary way for architectures
930 * to signal that they simply return xtime in
931 * do_gettimeoffset(). In this case we want to round up by
932 * resolution when starting a relative timer, to avoid short
933 * timeouts. This will go away with the GTOD framework.
934 */
935#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100936 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800937#endif
938 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700939
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700940 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800941
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800942 timer_stats_hrtimer_set_start_info(timer);
943
Ingo Molnar935c6312007-03-28 13:17:18 +0200944 /*
945 * Only allow reprogramming if the new base is on this CPU.
946 * (it might still be on another CPU if the timer was pending)
947 */
948 enqueue_hrtimer(timer, new_base,
949 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800950
951 unlock_hrtimer_base(timer, &flags);
952
953 return ret;
954}
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700955EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
956
957/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200958 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700959 * @timer: the timer to be added
960 * @tim: expiry time
961 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
962 *
963 * Returns:
964 * 0 on success
965 * 1 when the timer was active
966 */
967int
968hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
969{
970 return hrtimer_start_range_ns(timer, tim, 0, mode);
971}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700972EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800973
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700974
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800975/**
976 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800977 * @timer: hrtimer to stop
978 *
979 * Returns:
980 * 0 when the timer was not active
981 * 1 when the timer was active
982 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700983 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800984 */
985int hrtimer_try_to_cancel(struct hrtimer *timer)
986{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800987 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800988 unsigned long flags;
989 int ret = -1;
990
991 base = lock_hrtimer_base(timer, &flags);
992
Thomas Gleixner303e9672007-02-16 01:27:51 -0800993 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800994 ret = remove_hrtimer(timer, base);
995
996 unlock_hrtimer_base(timer, &flags);
997
998 return ret;
999
1000}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001001EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001002
1003/**
1004 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001005 * @timer: the timer to be cancelled
1006 *
1007 * Returns:
1008 * 0 when the timer was not active
1009 * 1 when the timer was active
1010 */
1011int hrtimer_cancel(struct hrtimer *timer)
1012{
1013 for (;;) {
1014 int ret = hrtimer_try_to_cancel(timer);
1015
1016 if (ret >= 0)
1017 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001018 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001019 }
1020}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001021EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001022
1023/**
1024 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001025 * @timer: the timer to read
1026 */
1027ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1028{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001029 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001030 unsigned long flags;
1031 ktime_t rem;
1032
1033 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001034 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001035 unlock_hrtimer_base(timer, &flags);
1036
1037 return rem;
1038}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001039EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001040
Russell Kingee9c5782008-04-20 13:59:33 +01001041#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001042/**
1043 * hrtimer_get_next_event - get the time until next expiry event
1044 *
1045 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1046 * is pending.
1047 */
1048ktime_t hrtimer_get_next_event(void)
1049{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001050 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1051 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001052 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1053 unsigned long flags;
1054 int i;
1055
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001056 spin_lock_irqsave(&cpu_base->lock, flags);
1057
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001058 if (!hrtimer_hres_active()) {
1059 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1060 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001061
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001062 if (!base->first)
1063 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001064
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001065 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001066 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001067 delta = ktime_sub(delta, base->get_time());
1068 if (delta.tv64 < mindelta.tv64)
1069 mindelta.tv64 = delta.tv64;
1070 }
Tony Lindgren69239742006-03-06 15:42:45 -08001071 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001072
1073 spin_unlock_irqrestore(&cpu_base->lock, flags);
1074
Tony Lindgren69239742006-03-06 15:42:45 -08001075 if (mindelta.tv64 < 0)
1076 mindelta.tv64 = 0;
1077 return mindelta;
1078}
1079#endif
1080
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001081static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1082 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001083{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001084 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001085
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001086 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001087
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001088 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001089
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001090 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001091 clock_id = CLOCK_MONOTONIC;
1092
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001093 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001094 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001095 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001096
1097#ifdef CONFIG_TIMER_STATS
1098 timer->start_site = NULL;
1099 timer->start_pid = -1;
1100 memset(timer->start_comm, 0, TASK_COMM_LEN);
1101#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001102}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001103
1104/**
1105 * hrtimer_init - initialize a timer to the given clock
1106 * @timer: the timer to be initialized
1107 * @clock_id: the clock to be used
1108 * @mode: timer mode abs/rel
1109 */
1110void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1111 enum hrtimer_mode mode)
1112{
1113 debug_hrtimer_init(timer);
1114 __hrtimer_init(timer, clock_id, mode);
1115}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001116EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001117
1118/**
1119 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001120 * @which_clock: which clock to query
1121 * @tp: pointer to timespec variable to store the resolution
1122 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001123 * Store the resolution of the clock selected by @which_clock in the
1124 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001125 */
1126int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1127{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001128 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001129
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001130 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1131 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001132
1133 return 0;
1134}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001135EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001136
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001137static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001138{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001139 struct hrtimer_clock_base *base = timer->base;
1140 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1141 enum hrtimer_restart (*fn)(struct hrtimer *);
1142 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001143
Peter Zijlstraca109492008-11-25 12:43:51 +01001144 WARN_ON(!irqs_disabled());
1145
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001146 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001147 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1148 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001149 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001150
1151 /*
1152 * Because we run timers from hardirq context, there is no chance
1153 * they get migrated to another cpu, therefore its safe to unlock
1154 * the timer base.
1155 */
1156 spin_unlock(&cpu_base->lock);
1157 restart = fn(timer);
1158 spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001159
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001160 /*
1161 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1162 * reprogramming of the event hardware. This happens at the end of this
1163 * function anyway.
1164 */
1165 if (restart != HRTIMER_NORESTART) {
1166 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1167 enqueue_hrtimer(timer, base, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001168 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001169 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001170}
1171
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001172#ifdef CONFIG_HIGH_RES_TIMERS
1173
1174/*
1175 * High resolution timer interrupt
1176 * Called with interrupts disabled
1177 */
1178void hrtimer_interrupt(struct clock_event_device *dev)
1179{
1180 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1181 struct hrtimer_clock_base *base;
1182 ktime_t expires_next, now;
Peter Zijlstraca109492008-11-25 12:43:51 +01001183 int i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001184
1185 BUG_ON(!cpu_base->hres_active);
1186 cpu_base->nr_events++;
1187 dev->next_event.tv64 = KTIME_MAX;
1188
1189 retry:
1190 now = ktime_get();
1191
1192 expires_next.tv64 = KTIME_MAX;
1193
1194 base = cpu_base->clock_base;
1195
1196 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1197 ktime_t basenow;
1198 struct rb_node *node;
1199
1200 spin_lock(&cpu_base->lock);
1201
1202 basenow = ktime_add(now, base->offset);
1203
1204 while ((node = base->first)) {
1205 struct hrtimer *timer;
1206
1207 timer = rb_entry(node, struct hrtimer, node);
1208
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001209 /*
1210 * The immediate goal for using the softexpires is
1211 * minimizing wakeups, not running timers at the
1212 * earliest interrupt after their soft expiration.
1213 * This allows us to avoid using a Priority Search
1214 * Tree, which can answer a stabbing querry for
1215 * overlapping intervals and instead use the simple
1216 * BST we already have.
1217 * We don't add extra wakeups by delaying timers that
1218 * are right-of a not yet expired timer, because that
1219 * timer will have to trigger a wakeup anyway.
1220 */
1221
1222 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001223 ktime_t expires;
1224
Arjan van de Vencc584b22008-09-01 15:02:30 -07001225 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001226 base->offset);
1227 if (expires.tv64 < expires_next.tv64)
1228 expires_next = expires;
1229 break;
1230 }
1231
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001232 __run_hrtimer(timer);
1233 }
1234 spin_unlock(&cpu_base->lock);
1235 base++;
1236 }
1237
1238 cpu_base->expires_next = expires_next;
1239
1240 /* Reprogramming necessary ? */
1241 if (expires_next.tv64 != KTIME_MAX) {
1242 if (tick_program_event(expires_next, 0))
1243 goto retry;
1244 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001245}
1246
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001247/**
1248 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1249 *
1250 * hrtimer_peek_ahead_timers will peek at the timer queue of
1251 * the current cpu and check if there are any timers for which
1252 * the soft expires time has passed. If any such timers exist,
1253 * they are run immediately and then removed from the timer queue.
1254 *
1255 */
1256void hrtimer_peek_ahead_timers(void)
1257{
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001258 struct tick_device *td;
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001259 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001260
1261 if (!hrtimer_hres_active())
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001262 return;
1263
1264 local_irq_save(flags);
1265 td = &__get_cpu_var(tick_cpu_device);
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001266 if (td && td->evtdev)
1267 hrtimer_interrupt(td->evtdev);
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001268 local_irq_restore(flags);
1269}
1270
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001271#endif /* CONFIG_HIGH_RES_TIMERS */
1272
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001273/*
1274 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001275 *
1276 * For HRT its the fall back code to run the softirq in the timer
1277 * softirq context in case the hrtimer initialization failed or has
1278 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001279 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001280void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001281{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001282 if (hrtimer_hres_active())
1283 return;
1284
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001285 /*
1286 * This _is_ ugly: We have to check in the softirq context,
1287 * whether we can switch to highres and / or nohz mode. The
1288 * clocksource switch happens in the timer interrupt with
1289 * xtime_lock held. Notification from there only sets the
1290 * check bit in the tick_oneshot code, otherwise we might
1291 * deadlock vs. xtime_lock.
1292 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001293 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001294 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001295}
1296
1297/*
1298 * Called from hardirq context every jiffy
1299 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001300void hrtimer_run_queues(void)
1301{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001302 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001303 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001304 struct hrtimer_clock_base *base;
1305 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001306
1307 if (hrtimer_hres_active())
1308 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001309
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001310 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1311 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001312
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001313 if (!base->first)
1314 continue;
1315
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001316 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001317 hrtimer_get_softirq_time(cpu_base);
1318 gettime = 0;
1319 }
1320
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001321 spin_lock(&cpu_base->lock);
1322
1323 while ((node = base->first)) {
1324 struct hrtimer *timer;
1325
1326 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001327 if (base->softirq_time.tv64 <=
1328 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001329 break;
1330
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001331 __run_hrtimer(timer);
1332 }
1333 spin_unlock(&cpu_base->lock);
1334 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001335}
1336
1337/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001338 * Sleep related functions:
1339 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001340static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001341{
1342 struct hrtimer_sleeper *t =
1343 container_of(timer, struct hrtimer_sleeper, timer);
1344 struct task_struct *task = t->task;
1345
1346 t->task = NULL;
1347 if (task)
1348 wake_up_process(task);
1349
1350 return HRTIMER_NORESTART;
1351}
1352
Ingo Molnar36c8b582006-07-03 00:25:41 -07001353void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001354{
1355 sl->timer.function = hrtimer_wakeup;
1356 sl->task = task;
1357}
1358
Thomas Gleixner669d7862006-03-31 02:31:19 -08001359static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001360{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001361 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001362
Roman Zippel432569b2006-03-26 01:38:08 -08001363 do {
1364 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001365 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001366 if (!hrtimer_active(&t->timer))
1367 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001368
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001369 if (likely(t->task))
1370 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001371
Thomas Gleixner669d7862006-03-31 02:31:19 -08001372 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001373 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001374
Thomas Gleixner669d7862006-03-31 02:31:19 -08001375 } while (t->task && !signal_pending(current));
1376
Peter Zijlstra3588a082008-02-01 17:45:13 +01001377 __set_current_state(TASK_RUNNING);
1378
Thomas Gleixner669d7862006-03-31 02:31:19 -08001379 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001380}
1381
Oleg Nesterov080344b2008-02-01 17:29:05 +03001382static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1383{
1384 struct timespec rmt;
1385 ktime_t rem;
1386
Arjan van de Vencc584b22008-09-01 15:02:30 -07001387 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001388 if (rem.tv64 <= 0)
1389 return 0;
1390 rmt = ktime_to_timespec(rem);
1391
1392 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1393 return -EFAULT;
1394
1395 return 1;
1396}
1397
Toyo Abe1711ef32006-09-29 02:00:28 -07001398long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001399{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001400 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001401 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001402 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001403
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001404 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1405 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001406 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001407
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001408 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001409 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001410
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001411 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001412 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001413 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001414 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001415 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001416 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001417
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001418 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001419 ret = -ERESTART_RESTARTBLOCK;
1420out:
1421 destroy_hrtimer_on_stack(&t.timer);
1422 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001423}
1424
Oleg Nesterov080344b2008-02-01 17:29:05 +03001425long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001426 const enum hrtimer_mode mode, const clockid_t clockid)
1427{
1428 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001429 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001430 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001431 unsigned long slack;
1432
1433 slack = current->timer_slack_ns;
1434 if (rt_task(current))
1435 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001436
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001437 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001438 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001439 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001440 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001441
George Anzinger7978672c2006-02-01 03:05:11 -08001442 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001443 if (mode == HRTIMER_MODE_ABS) {
1444 ret = -ERESTARTNOHAND;
1445 goto out;
1446 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001447
Roman Zippel432569b2006-03-26 01:38:08 -08001448 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001449 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001450 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001451 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001452 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001453
1454 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001455 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001456 restart->nanosleep.index = t.timer.base->index;
1457 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001458 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001459
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001460 ret = -ERESTART_RESTARTBLOCK;
1461out:
1462 destroy_hrtimer_on_stack(&t.timer);
1463 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001464}
1465
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001466asmlinkage long
1467sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1468{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001469 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001470
1471 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1472 return -EFAULT;
1473
1474 if (!timespec_valid(&tu))
1475 return -EINVAL;
1476
Oleg Nesterov080344b2008-02-01 17:29:05 +03001477 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001478}
1479
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001480/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001481 * Functions related to boot-time initialization:
1482 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001483static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001484{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001485 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001486 int i;
1487
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001488 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001489
1490 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1491 cpu_base->clock_base[i].cpu_base = cpu_base;
1492
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001493 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001494}
1495
1496#ifdef CONFIG_HOTPLUG_CPU
1497
Peter Zijlstraca109492008-11-25 12:43:51 +01001498static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001499 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001500{
1501 struct hrtimer *timer;
1502 struct rb_node *node;
1503
1504 while ((node = rb_first(&old_base->active))) {
1505 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001506 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001507 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001508
1509 /*
1510 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1511 * timer could be seen as !active and just vanish away
1512 * under us on another CPU
1513 */
1514 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001515 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001516 /*
Peter Zijlstra37810652008-12-04 11:17:10 +01001517 * Enqueue the timers on the new cpu, but do not reprogram
1518 * the timer as that would enable a deadlock between
1519 * hrtimer_enqueue_reprogramm() running the timer and us still
1520 * holding a nested base lock.
1521 *
1522 * Instead we tickle the hrtimer interrupt after the migration
1523 * is done, which will run all expired timers and re-programm
1524 * the timer device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001525 */
Peter Zijlstra37810652008-12-04 11:17:10 +01001526 enqueue_hrtimer(timer, new_base, 0);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001527
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001528 /* Clear the migration state bit */
1529 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001530 }
1531}
1532
Peter Zijlstra37810652008-12-04 11:17:10 +01001533static int migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001534{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001535 struct hrtimer_cpu_base *old_base, *new_base;
Peter Zijlstra37810652008-12-04 11:17:10 +01001536 int dcpu, i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001537
Peter Zijlstra37810652008-12-04 11:17:10 +01001538 BUG_ON(cpu_online(scpu));
1539 old_base = &per_cpu(hrtimer_bases, scpu);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001540 new_base = &get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001541
Peter Zijlstra37810652008-12-04 11:17:10 +01001542 dcpu = smp_processor_id();
1543
1544 tick_cancel_sched_timer(scpu);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001545 /*
1546 * The caller is globally serialized and nobody else
1547 * takes two locks at once, deadlock is not possible.
1548 */
1549 spin_lock_irq(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001550 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001551
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001552 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001553 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001554 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001555 }
1556
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001557 spin_unlock(&old_base->lock);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001558 spin_unlock_irq(&new_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001559 put_cpu_var(hrtimer_bases);
Peter Zijlstra37810652008-12-04 11:17:10 +01001560
1561 return dcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001562}
Peter Zijlstra37810652008-12-04 11:17:10 +01001563
1564static void tickle_timers(void *arg)
1565{
1566 hrtimer_peek_ahead_timers();
1567}
1568
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001569#endif /* CONFIG_HOTPLUG_CPU */
1570
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001571static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001572 unsigned long action, void *hcpu)
1573{
Peter Zijlstra37810652008-12-04 11:17:10 +01001574 int dcpu = -1, scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001575
1576 switch (action) {
1577
1578 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001579 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001580 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001581 break;
1582
1583#ifdef CONFIG_HOTPLUG_CPU
1584 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001585 case CPU_DEAD_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001586 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1587 dcpu = migrate_hrtimers(scpu);
1588 break;
1589
1590 case CPU_POST_DEAD:
1591 if (dcpu == -1)
1592 break;
1593
1594 smp_call_function_single(dcpu, tickle_timers, NULL, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001595 break;
1596#endif
1597
1598 default:
1599 break;
1600 }
1601
1602 return NOTIFY_OK;
1603}
1604
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001605static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001606 .notifier_call = hrtimer_cpu_notify,
1607};
1608
1609void __init hrtimers_init(void)
1610{
1611 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1612 (void *)(long)smp_processor_id());
1613 register_cpu_notifier(&hrtimers_nb);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001614}
1615
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001616/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001617 * schedule_hrtimeout_range - sleep until timeout
1618 * @expires: timeout value (ktime_t)
1619 * @delta: slack in expires timeout (ktime_t)
1620 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1621 *
1622 * Make the current task sleep until the given expiry time has
1623 * elapsed. The routine will return immediately unless
1624 * the current task state has been set (see set_current_state()).
1625 *
1626 * The @delta argument gives the kernel the freedom to schedule the
1627 * actual wakeup to a time that is both power and performance friendly.
1628 * The kernel give the normal best effort behavior for "@expires+@delta",
1629 * but may decide to fire the timer earlier, but no earlier than @expires.
1630 *
1631 * You can set the task state as follows -
1632 *
1633 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1634 * pass before the routine returns.
1635 *
1636 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1637 * delivered to the current task.
1638 *
1639 * The current task state is guaranteed to be TASK_RUNNING when this
1640 * routine returns.
1641 *
1642 * Returns 0 when the timer has expired otherwise -EINTR
1643 */
1644int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1645 const enum hrtimer_mode mode)
1646{
1647 struct hrtimer_sleeper t;
1648
1649 /*
1650 * Optimize when a zero timeout value is given. It does not
1651 * matter whether this is an absolute or a relative time.
1652 */
1653 if (expires && !expires->tv64) {
1654 __set_current_state(TASK_RUNNING);
1655 return 0;
1656 }
1657
1658 /*
1659 * A NULL parameter means "inifinte"
1660 */
1661 if (!expires) {
1662 schedule();
1663 __set_current_state(TASK_RUNNING);
1664 return -EINTR;
1665 }
1666
1667 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1668 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1669
1670 hrtimer_init_sleeper(&t, current);
1671
1672 hrtimer_start_expires(&t.timer, mode);
1673 if (!hrtimer_active(&t.timer))
1674 t.task = NULL;
1675
1676 if (likely(t.task))
1677 schedule();
1678
1679 hrtimer_cancel(&t.timer);
1680 destroy_hrtimer_on_stack(&t.timer);
1681
1682 __set_current_state(TASK_RUNNING);
1683
1684 return !t.task ? 0 : -EINTR;
1685}
1686EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1687
1688/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001689 * schedule_hrtimeout - sleep until timeout
1690 * @expires: timeout value (ktime_t)
1691 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1692 *
1693 * Make the current task sleep until the given expiry time has
1694 * elapsed. The routine will return immediately unless
1695 * the current task state has been set (see set_current_state()).
1696 *
1697 * You can set the task state as follows -
1698 *
1699 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1700 * pass before the routine returns.
1701 *
1702 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1703 * delivered to the current task.
1704 *
1705 * The current task state is guaranteed to be TASK_RUNNING when this
1706 * routine returns.
1707 *
1708 * Returns 0 when the timer has expired otherwise -EINTR
1709 */
1710int __sched schedule_hrtimeout(ktime_t *expires,
1711 const enum hrtimer_mode mode)
1712{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001713 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001714}
1715EXPORT_SYMBOL_GPL(schedule_hrtimeout);