blob: c71bcd54924133cfb38102385e990828313eb208 [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>
35#include <linux/module.h>
36#include <linux/percpu.h>
37#include <linux/hrtimer.h>
38#include <linux/notifier.h>
39#include <linux/syscalls.h>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080040#include <linux/kallsyms.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080041#include <linux/interrupt.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080042#include <linux/tick.h>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080043#include <linux/seq_file.h>
44#include <linux/err.h>
Thomas Gleixner237fc6e2008-04-30 00:55:04 -070045#include <linux/debugobjects.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080046
47#include <asm/uaccess.h>
48
49/**
50 * ktime_get - get the monotonic time in ktime_t format
51 *
52 * returns the time in ktime_t format
53 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080054ktime_t ktime_get(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080055{
56 struct timespec now;
57
58 ktime_get_ts(&now);
59
60 return timespec_to_ktime(now);
61}
Patrick McHardy641b9e02007-03-16 01:18:42 -070062EXPORT_SYMBOL_GPL(ktime_get);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080063
64/**
65 * ktime_get_real - get the real (wall-) time in ktime_t format
66 *
67 * returns the time in ktime_t format
68 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080069ktime_t ktime_get_real(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080070{
71 struct timespec now;
72
73 getnstimeofday(&now);
74
75 return timespec_to_ktime(now);
76}
77
78EXPORT_SYMBOL_GPL(ktime_get_real);
79
80/*
81 * The timer bases:
George Anzinger79786722006-02-01 03:05:11 -080082 *
83 * Note: If we want to add new timer bases, we have to skip the two
84 * clock ids captured by the cpu-timers. We do this by holding empty
85 * entries rather than doing math adjustment of the clock ids.
86 * This ensures that we capture erroneous accesses to these clock ids
87 * rather than moving them into the range of valid clock id's.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080088 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080089DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080090{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080091
92 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080093 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080094 {
95 .index = CLOCK_REALTIME,
96 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080097 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080098 },
99 {
100 .index = CLOCK_MONOTONIC,
101 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800102 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800103 },
104 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800105};
106
107/**
108 * ktime_get_ts - get the monotonic clock in timespec format
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800109 * @ts: pointer to timespec variable
110 *
111 * The function calculates the monotonic clock from the realtime
112 * clock and the wall_to_monotonic offset and stores the result
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800113 * in normalized timespec format in the variable pointed to by @ts.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800114 */
115void ktime_get_ts(struct timespec *ts)
116{
117 struct timespec tomono;
118 unsigned long seq;
119
120 do {
121 seq = read_seqbegin(&xtime_lock);
122 getnstimeofday(ts);
123 tomono = wall_to_monotonic;
124
125 } while (read_seqretry(&xtime_lock, seq));
126
127 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
128 ts->tv_nsec + tomono.tv_nsec);
129}
Matt Helsley69778e32006-01-09 20:52:39 -0800130EXPORT_SYMBOL_GPL(ktime_get_ts);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800131
132/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800133 * Get the coarse grained time at the softirq based on xtime and
134 * wall_to_monotonic.
135 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800136static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800137{
138 ktime_t xtim, tomono;
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800139 struct timespec xts, tom;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800140 unsigned long seq;
141
142 do {
143 seq = read_seqbegin(&xtime_lock);
john stultz2c6b47d2007-07-24 17:47:43 -0700144 xts = current_kernel_time();
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800145 tom = wall_to_monotonic;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800146 } while (read_seqretry(&xtime_lock, seq));
147
john stultzf4304ab2007-02-16 01:27:26 -0800148 xtim = timespec_to_ktime(xts);
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800149 tomono = timespec_to_ktime(tom);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800150 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
151 base->clock_base[CLOCK_MONOTONIC].softirq_time =
152 ktime_add(xtim, tomono);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800153}
154
155/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800156 * Functions and macros which are different for UP/SMP systems are kept in a
157 * single place
158 */
159#ifdef CONFIG_SMP
160
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800161/*
162 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
163 * means that all timers which are tied to this base via timer->base are
164 * locked, and the base itself is locked too.
165 *
166 * So __run_timers/migrate_timers can safely modify all timers which could
167 * be found on the lists/queues.
168 *
169 * When the timer's base is locked, and the timer removed from list, it is
170 * possible to set timer->base = NULL and drop the lock: the timer remains
171 * locked.
172 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800173static
174struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
175 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800176{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800177 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800178
179 for (;;) {
180 base = timer->base;
181 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800182 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800183 if (likely(base == timer->base))
184 return base;
185 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800186 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800187 }
188 cpu_relax();
189 }
190}
191
192/*
193 * Switch the timer base to the current CPU when possible.
194 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800195static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530196switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
197 int pinned)
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 Gleixnerb0a9b512009-01-25 11:31:36 +0100505 /*
506 * clock_was_set() has changed base->offset so the
507 * result might be negative. Fix it up to prevent a
508 * false positive in clockevents_program_event()
509 */
510 if (expires.tv64 < 0)
511 expires.tv64 = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800512 if (expires.tv64 < cpu_base->expires_next.tv64)
513 cpu_base->expires_next = expires;
514 }
515
516 if (cpu_base->expires_next.tv64 != KTIME_MAX)
517 tick_program_event(cpu_base->expires_next, 1);
518}
519
520/*
521 * Shared reprogramming for clock_realtime and clock_monotonic
522 *
523 * When a timer is enqueued and expires earlier than the already enqueued
524 * timers, we have to check, whether it expires earlier than the timer for
525 * which the clock event device was armed.
526 *
527 * Called with interrupts disabled and base->cpu_base.lock held
528 */
529static int hrtimer_reprogram(struct hrtimer *timer,
530 struct hrtimer_clock_base *base)
531{
532 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700533 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800534 int res;
535
Arjan van de Vencc584b22008-09-01 15:02:30 -0700536 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100537
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800538 /*
539 * When the callback is running, we do not reprogram the clock event
540 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200541 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800542 * reprogramming is handled either by the softirq, which called the
543 * callback or at the end of the hrtimer_interrupt.
544 */
545 if (hrtimer_callback_running(timer))
546 return 0;
547
Thomas Gleixner63070a72008-02-14 00:58:36 +0100548 /*
549 * CLOCK_REALTIME timer might be requested with an absolute
550 * expiry time which is less than base->offset. Nothing wrong
551 * about that, just avoid to call into the tick code, which
552 * has now objections against negative expiry values.
553 */
554 if (expires.tv64 < 0)
555 return -ETIME;
556
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800557 if (expires.tv64 >= expires_next->tv64)
558 return 0;
559
560 /*
561 * Clockevents returns -ETIME, when the event was in the past.
562 */
563 res = tick_program_event(expires, 0);
564 if (!IS_ERR_VALUE(res))
565 *expires_next = expires;
566 return res;
567}
568
569
570/*
571 * Retrigger next event is called after clock was set
572 *
573 * Called with interrupts disabled via on_each_cpu()
574 */
575static void retrigger_next_event(void *arg)
576{
577 struct hrtimer_cpu_base *base;
578 struct timespec realtime_offset;
579 unsigned long seq;
580
581 if (!hrtimer_hres_active())
582 return;
583
584 do {
585 seq = read_seqbegin(&xtime_lock);
586 set_normalized_timespec(&realtime_offset,
587 -wall_to_monotonic.tv_sec,
588 -wall_to_monotonic.tv_nsec);
589 } while (read_seqretry(&xtime_lock, seq));
590
591 base = &__get_cpu_var(hrtimer_bases);
592
593 /* Adjust CLOCK_REALTIME offset */
594 spin_lock(&base->lock);
595 base->clock_base[CLOCK_REALTIME].offset =
596 timespec_to_ktime(realtime_offset);
597
598 hrtimer_force_reprogram(base);
599 spin_unlock(&base->lock);
600}
601
602/*
603 * Clock realtime was set
604 *
605 * Change the offset of the realtime clock vs. the monotonic
606 * clock.
607 *
608 * We might have to reprogram the high resolution timer interrupt. On
609 * SMP we call the architecture specific code to retrigger _all_ high
610 * resolution timer interrupts. On UP we just disable interrupts and
611 * call the high resolution interrupt code.
612 */
613void clock_was_set(void)
614{
615 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200616 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800617}
618
619/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200620 * During resume we might have to reprogram the high resolution timer
621 * interrupt (on the local CPU):
622 */
623void hres_timers_resume(void)
624{
Peter Zijlstra1d4a7f12009-01-18 16:39:29 +0100625 WARN_ONCE(!irqs_disabled(),
626 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
627
Ingo Molnar995f0542007-04-07 12:05:00 +0200628 retrigger_next_event(NULL);
629}
630
631/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800632 * Initialize the high resolution related parts of cpu_base
633 */
634static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
635{
636 base->expires_next.tv64 = KTIME_MAX;
637 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800638}
639
640/*
641 * Initialize the high resolution related parts of a hrtimer
642 */
643static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
644{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800645}
646
Peter Zijlstraca109492008-11-25 12:43:51 +0100647
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800648/*
649 * When High resolution timers are active, try to reprogram. Note, that in case
650 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
651 * check happens. The timer gets enqueued into the rbtree. The reprogramming
652 * and expiry check is done in the hrtimer_interrupt or in the softirq.
653 */
654static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100655 struct hrtimer_clock_base *base,
656 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800657{
658 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100659 if (wakeup) {
660 spin_unlock(&base->cpu_base->lock);
661 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
662 spin_lock(&base->cpu_base->lock);
663 } else
664 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
665
Peter Zijlstraca109492008-11-25 12:43:51 +0100666 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800667 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100668
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800669 return 0;
670}
671
672/*
673 * Switch to high resolution mode
674 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800675static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800676{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700677 int cpu = smp_processor_id();
678 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800679 unsigned long flags;
680
681 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800682 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800683
684 local_irq_save(flags);
685
686 if (tick_init_highres()) {
687 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700688 printk(KERN_WARNING "Could not switch to high resolution "
689 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800690 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800691 }
692 base->hres_active = 1;
693 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
694 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
695
696 tick_setup_sched_timer();
697
698 /* "Retrigger" the interrupt to get things going */
699 retrigger_next_event(NULL);
700 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100701 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800702 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800703 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800704}
705
706#else
707
708static inline int hrtimer_hres_active(void) { return 0; }
709static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800710static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800711static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
712static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100713 struct hrtimer_clock_base *base,
714 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800715{
716 return 0;
717}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800718static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
719static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
720
721#endif /* CONFIG_HIGH_RES_TIMERS */
722
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800723#ifdef CONFIG_TIMER_STATS
724void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
725{
726 if (timer->start_site)
727 return;
728
729 timer->start_site = addr;
730 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
731 timer->start_pid = current->pid;
732}
733#endif
734
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800735/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200736 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800737 */
738static inline
739void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
740{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800741 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800742}
743
744/**
745 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800746 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800747 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800748 * @interval: the interval to forward
749 *
750 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700751 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800752 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800753u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800754{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800755 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800756 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800757
Arjan van de Vencc584b22008-09-01 15:02:30 -0700758 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800759
760 if (delta.tv64 < 0)
761 return 0;
762
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100763 if (interval.tv64 < timer->base->resolution.tv64)
764 interval.tv64 = timer->base->resolution.tv64;
765
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800766 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800767 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800768
769 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700770 hrtimer_add_expires_ns(timer, incr * orun);
771 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800772 return orun;
773 /*
774 * This (and the ktime_add() below) is the
775 * correction for exact:
776 */
777 orun++;
778 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700779 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800780
781 return orun;
782}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700783EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800784
785/*
786 * enqueue_hrtimer - internal function to (re)start a timer
787 *
788 * The timer is inserted in expiry order. Insertion into the
789 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100790 *
791 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800792 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100793static int enqueue_hrtimer(struct hrtimer *timer,
794 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800795{
796 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800797 struct rb_node *parent = NULL;
798 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700799 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800800
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700801 debug_hrtimer_activate(timer);
802
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800803 /*
804 * Find the right place in the rbtree:
805 */
806 while (*link) {
807 parent = *link;
808 entry = rb_entry(parent, struct hrtimer, node);
809 /*
810 * We dont care about collisions. Nodes with
811 * the same expiry time stay together.
812 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700813 if (hrtimer_get_expires_tv64(timer) <
814 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800815 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700816 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700818 leftmost = 0;
819 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800820 }
821
822 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100823 * Insert the timer to the rbtree and check whether it
824 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800825 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100826 if (leftmost)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800827 base->first = &timer->node;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800828
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800829 rb_link_node(&timer->node, parent, link);
830 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800831 /*
832 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
833 * state of a possibly running callback.
834 */
835 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100836
837 return leftmost;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100838}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800839
840/*
841 * __remove_hrtimer - internal function to remove a timer
842 *
843 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800844 *
845 * High resolution timer mode reprograms the clock event device when the
846 * timer is the one which expires next. The caller can disable this by setting
847 * reprogram to zero. This is useful, when the context does a reprogramming
848 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800849 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800850static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800851 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800852 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800853{
Peter Zijlstraca109492008-11-25 12:43:51 +0100854 if (timer->state & HRTIMER_STATE_ENQUEUED) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800855 /*
856 * Remove the timer from the rbtree and replace the
857 * first entry pointer if necessary.
858 */
859 if (base->first == &timer->node) {
860 base->first = rb_next(&timer->node);
861 /* Reprogram the clock event device. if enabled */
862 if (reprogram && hrtimer_hres_active())
863 hrtimer_force_reprogram(base->cpu_base);
864 }
865 rb_erase(&timer->node, &base->active);
866 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800867 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800868}
869
870/*
871 * remove hrtimer, called with base lock held
872 */
873static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800874remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800875{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800876 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800877 int reprogram;
878
879 /*
880 * Remove the timer and force reprogramming when high
881 * resolution mode is active and the timer is on the current
882 * CPU. If we remove a timer on another CPU, reprogramming is
883 * skipped. The interrupt event on this CPU is fired and
884 * reprogramming happens in the interrupt handler. This is a
885 * rare case and less expensive than a smp call.
886 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700887 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800888 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800889 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
890 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
891 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800892 return 1;
893 }
894 return 0;
895}
896
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100897int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
898 unsigned long delta_ns, const enum hrtimer_mode mode,
899 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800900{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800901 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800902 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100903 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800904
905 base = lock_hrtimer_base(timer, &flags);
906
907 /* Remove an active timer from the queue: */
908 ret = remove_hrtimer(timer, base);
909
910 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530911 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800912
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530913 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100914 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800915 /*
916 * CONFIG_TIME_LOW_RES is a temporary way for architectures
917 * to signal that they simply return xtime in
918 * do_gettimeoffset(). In this case we want to round up by
919 * resolution when starting a relative timer, to avoid short
920 * timeouts. This will go away with the GTOD framework.
921 */
922#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100923 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800924#endif
925 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700926
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700927 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800928
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800929 timer_stats_hrtimer_set_start_info(timer);
930
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100931 leftmost = enqueue_hrtimer(timer, new_base);
932
Ingo Molnar935c6312007-03-28 13:17:18 +0200933 /*
934 * Only allow reprogramming if the new base is on this CPU.
935 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100936 *
937 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200938 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100939 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100940 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800941
942 unlock_hrtimer_base(timer, &flags);
943
944 return ret;
945}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100946
947/**
948 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
949 * @timer: the timer to be added
950 * @tim: expiry time
951 * @delta_ns: "slack" range for the timer
952 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
953 *
954 * Returns:
955 * 0 on success
956 * 1 when the timer was active
957 */
958int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
959 unsigned long delta_ns, const enum hrtimer_mode mode)
960{
961 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
962}
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700963EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
964
965/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200966 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700967 * @timer: the timer to be added
968 * @tim: expiry time
969 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
970 *
971 * Returns:
972 * 0 on success
973 * 1 when the timer was active
974 */
975int
976hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
977{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100978 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700979}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700980EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800981
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700982
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800983/**
984 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800985 * @timer: hrtimer to stop
986 *
987 * Returns:
988 * 0 when the timer was not active
989 * 1 when the timer was active
990 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700991 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800992 */
993int hrtimer_try_to_cancel(struct hrtimer *timer)
994{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800995 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800996 unsigned long flags;
997 int ret = -1;
998
999 base = lock_hrtimer_base(timer, &flags);
1000
Thomas Gleixner303e9672007-02-16 01:27:51 -08001001 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001002 ret = remove_hrtimer(timer, base);
1003
1004 unlock_hrtimer_base(timer, &flags);
1005
1006 return ret;
1007
1008}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001009EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001010
1011/**
1012 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001013 * @timer: the timer to be cancelled
1014 *
1015 * Returns:
1016 * 0 when the timer was not active
1017 * 1 when the timer was active
1018 */
1019int hrtimer_cancel(struct hrtimer *timer)
1020{
1021 for (;;) {
1022 int ret = hrtimer_try_to_cancel(timer);
1023
1024 if (ret >= 0)
1025 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001026 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001027 }
1028}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001029EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001030
1031/**
1032 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001033 * @timer: the timer to read
1034 */
1035ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1036{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001037 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001038 unsigned long flags;
1039 ktime_t rem;
1040
1041 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001042 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001043 unlock_hrtimer_base(timer, &flags);
1044
1045 return rem;
1046}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001047EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001048
Russell Kingee9c5782008-04-20 13:59:33 +01001049#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001050/**
1051 * hrtimer_get_next_event - get the time until next expiry event
1052 *
1053 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1054 * is pending.
1055 */
1056ktime_t hrtimer_get_next_event(void)
1057{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001058 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1059 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001060 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1061 unsigned long flags;
1062 int i;
1063
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001064 spin_lock_irqsave(&cpu_base->lock, flags);
1065
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001066 if (!hrtimer_hres_active()) {
1067 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1068 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001069
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001070 if (!base->first)
1071 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001072
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001073 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001074 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001075 delta = ktime_sub(delta, base->get_time());
1076 if (delta.tv64 < mindelta.tv64)
1077 mindelta.tv64 = delta.tv64;
1078 }
Tony Lindgren69239742006-03-06 15:42:45 -08001079 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001080
1081 spin_unlock_irqrestore(&cpu_base->lock, flags);
1082
Tony Lindgren69239742006-03-06 15:42:45 -08001083 if (mindelta.tv64 < 0)
1084 mindelta.tv64 = 0;
1085 return mindelta;
1086}
1087#endif
1088
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001089static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1090 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001091{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001092 struct hrtimer_cpu_base *cpu_base;
George Anzinger79786722006-02-01 03:05:11 -08001093
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001094 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001095
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001096 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001097
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001098 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001099 clock_id = CLOCK_MONOTONIC;
1100
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001101 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001102 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001103 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001104
1105#ifdef CONFIG_TIMER_STATS
1106 timer->start_site = NULL;
1107 timer->start_pid = -1;
1108 memset(timer->start_comm, 0, TASK_COMM_LEN);
1109#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001110}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001111
1112/**
1113 * hrtimer_init - initialize a timer to the given clock
1114 * @timer: the timer to be initialized
1115 * @clock_id: the clock to be used
1116 * @mode: timer mode abs/rel
1117 */
1118void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1119 enum hrtimer_mode mode)
1120{
1121 debug_hrtimer_init(timer);
1122 __hrtimer_init(timer, clock_id, mode);
1123}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001124EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001125
1126/**
1127 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001128 * @which_clock: which clock to query
1129 * @tp: pointer to timespec variable to store the resolution
1130 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001131 * Store the resolution of the clock selected by @which_clock in the
1132 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001133 */
1134int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1135{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001136 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001137
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001138 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1139 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001140
1141 return 0;
1142}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001143EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001144
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001145static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001146{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001147 struct hrtimer_clock_base *base = timer->base;
1148 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1149 enum hrtimer_restart (*fn)(struct hrtimer *);
1150 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001151
Peter Zijlstraca109492008-11-25 12:43:51 +01001152 WARN_ON(!irqs_disabled());
1153
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001154 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001155 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1156 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001157 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001158
1159 /*
1160 * Because we run timers from hardirq context, there is no chance
1161 * they get migrated to another cpu, therefore its safe to unlock
1162 * the timer base.
1163 */
1164 spin_unlock(&cpu_base->lock);
1165 restart = fn(timer);
1166 spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001167
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001168 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001169 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1170 * we do not reprogramm the event hardware. Happens either in
1171 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001172 */
1173 if (restart != HRTIMER_NORESTART) {
1174 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001175 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001176 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001177 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001178}
1179
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001180#ifdef CONFIG_HIGH_RES_TIMERS
1181
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001182static int force_clock_reprogram;
1183
1184/*
1185 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1186 * is hanging, which could happen with something that slows the interrupt
1187 * such as the tracing. Then we force the clock reprogramming for each future
1188 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1189 * threshold that we will overwrite.
1190 * The next tick event will be scheduled to 3 times we currently spend on
1191 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1192 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1193 * let it running without serious starvation.
1194 */
1195
1196static inline void
1197hrtimer_interrupt_hanging(struct clock_event_device *dev,
1198 ktime_t try_time)
1199{
1200 force_clock_reprogram = 1;
1201 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1202 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1203 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1204}
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001205/*
1206 * High resolution timer interrupt
1207 * Called with interrupts disabled
1208 */
1209void hrtimer_interrupt(struct clock_event_device *dev)
1210{
1211 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1212 struct hrtimer_clock_base *base;
1213 ktime_t expires_next, now;
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001214 int nr_retries = 0;
Peter Zijlstraca109492008-11-25 12:43:51 +01001215 int i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001216
1217 BUG_ON(!cpu_base->hres_active);
1218 cpu_base->nr_events++;
1219 dev->next_event.tv64 = KTIME_MAX;
1220
1221 retry:
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001222 /* 5 retries is enough to notice a hang */
1223 if (!(++nr_retries % 5))
1224 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1225
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001226 now = ktime_get();
1227
1228 expires_next.tv64 = KTIME_MAX;
1229
1230 base = cpu_base->clock_base;
1231
1232 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1233 ktime_t basenow;
1234 struct rb_node *node;
1235
1236 spin_lock(&cpu_base->lock);
1237
1238 basenow = ktime_add(now, base->offset);
1239
1240 while ((node = base->first)) {
1241 struct hrtimer *timer;
1242
1243 timer = rb_entry(node, struct hrtimer, node);
1244
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001245 /*
1246 * The immediate goal for using the softexpires is
1247 * minimizing wakeups, not running timers at the
1248 * earliest interrupt after their soft expiration.
1249 * This allows us to avoid using a Priority Search
1250 * Tree, which can answer a stabbing querry for
1251 * overlapping intervals and instead use the simple
1252 * BST we already have.
1253 * We don't add extra wakeups by delaying timers that
1254 * are right-of a not yet expired timer, because that
1255 * timer will have to trigger a wakeup anyway.
1256 */
1257
1258 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001259 ktime_t expires;
1260
Arjan van de Vencc584b22008-09-01 15:02:30 -07001261 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001262 base->offset);
1263 if (expires.tv64 < expires_next.tv64)
1264 expires_next = expires;
1265 break;
1266 }
1267
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001268 __run_hrtimer(timer);
1269 }
1270 spin_unlock(&cpu_base->lock);
1271 base++;
1272 }
1273
1274 cpu_base->expires_next = expires_next;
1275
1276 /* Reprogramming necessary ? */
1277 if (expires_next.tv64 != KTIME_MAX) {
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001278 if (tick_program_event(expires_next, force_clock_reprogram))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001279 goto retry;
1280 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001281}
1282
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001283/*
1284 * local version of hrtimer_peek_ahead_timers() called with interrupts
1285 * disabled.
1286 */
1287static void __hrtimer_peek_ahead_timers(void)
1288{
1289 struct tick_device *td;
1290
1291 if (!hrtimer_hres_active())
1292 return;
1293
1294 td = &__get_cpu_var(tick_cpu_device);
1295 if (td && td->evtdev)
1296 hrtimer_interrupt(td->evtdev);
1297}
1298
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001299/**
1300 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1301 *
1302 * hrtimer_peek_ahead_timers will peek at the timer queue of
1303 * the current cpu and check if there are any timers for which
1304 * the soft expires time has passed. If any such timers exist,
1305 * they are run immediately and then removed from the timer queue.
1306 *
1307 */
1308void hrtimer_peek_ahead_timers(void)
1309{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001310 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001311
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001312 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001313 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001314 local_irq_restore(flags);
1315}
1316
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001317static void run_hrtimer_softirq(struct softirq_action *h)
1318{
1319 hrtimer_peek_ahead_timers();
1320}
1321
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001322#else /* CONFIG_HIGH_RES_TIMERS */
1323
1324static inline void __hrtimer_peek_ahead_timers(void) { }
1325
1326#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001327
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001328/*
1329 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001330 *
1331 * For HRT its the fall back code to run the softirq in the timer
1332 * softirq context in case the hrtimer initialization failed or has
1333 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001334 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001335void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001336{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001337 if (hrtimer_hres_active())
1338 return;
1339
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001340 /*
1341 * This _is_ ugly: We have to check in the softirq context,
1342 * whether we can switch to highres and / or nohz mode. The
1343 * clocksource switch happens in the timer interrupt with
1344 * xtime_lock held. Notification from there only sets the
1345 * check bit in the tick_oneshot code, otherwise we might
1346 * deadlock vs. xtime_lock.
1347 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001348 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001349 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001350}
1351
1352/*
1353 * Called from hardirq context every jiffy
1354 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001355void hrtimer_run_queues(void)
1356{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001357 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001358 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001359 struct hrtimer_clock_base *base;
1360 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001361
1362 if (hrtimer_hres_active())
1363 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001364
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001365 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1366 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001367
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001368 if (!base->first)
1369 continue;
1370
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001371 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001372 hrtimer_get_softirq_time(cpu_base);
1373 gettime = 0;
1374 }
1375
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001376 spin_lock(&cpu_base->lock);
1377
1378 while ((node = base->first)) {
1379 struct hrtimer *timer;
1380
1381 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001382 if (base->softirq_time.tv64 <=
1383 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001384 break;
1385
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001386 __run_hrtimer(timer);
1387 }
1388 spin_unlock(&cpu_base->lock);
1389 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001390}
1391
1392/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001393 * Sleep related functions:
1394 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001395static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001396{
1397 struct hrtimer_sleeper *t =
1398 container_of(timer, struct hrtimer_sleeper, timer);
1399 struct task_struct *task = t->task;
1400
1401 t->task = NULL;
1402 if (task)
1403 wake_up_process(task);
1404
1405 return HRTIMER_NORESTART;
1406}
1407
Ingo Molnar36c8b582006-07-03 00:25:41 -07001408void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001409{
1410 sl->timer.function = hrtimer_wakeup;
1411 sl->task = task;
1412}
1413
Thomas Gleixner669d7862006-03-31 02:31:19 -08001414static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001415{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001416 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001417
Roman Zippel432569b2006-03-26 01:38:08 -08001418 do {
1419 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001420 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001421 if (!hrtimer_active(&t->timer))
1422 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001423
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001424 if (likely(t->task))
1425 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001426
Thomas Gleixner669d7862006-03-31 02:31:19 -08001427 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001428 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001429
Thomas Gleixner669d7862006-03-31 02:31:19 -08001430 } while (t->task && !signal_pending(current));
1431
Peter Zijlstra3588a082008-02-01 17:45:13 +01001432 __set_current_state(TASK_RUNNING);
1433
Thomas Gleixner669d7862006-03-31 02:31:19 -08001434 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001435}
1436
Oleg Nesterov080344b2008-02-01 17:29:05 +03001437static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1438{
1439 struct timespec rmt;
1440 ktime_t rem;
1441
Arjan van de Vencc584b22008-09-01 15:02:30 -07001442 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001443 if (rem.tv64 <= 0)
1444 return 0;
1445 rmt = ktime_to_timespec(rem);
1446
1447 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1448 return -EFAULT;
1449
1450 return 1;
1451}
1452
Toyo Abe1711ef32006-09-29 02:00:28 -07001453long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001454{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001455 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001456 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001457 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001458
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001459 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1460 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001461 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001462
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001463 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001464 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001465
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001466 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001467 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001468 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001469 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001470 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001471 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001472
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001473 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001474 ret = -ERESTART_RESTARTBLOCK;
1475out:
1476 destroy_hrtimer_on_stack(&t.timer);
1477 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001478}
1479
Oleg Nesterov080344b2008-02-01 17:29:05 +03001480long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001481 const enum hrtimer_mode mode, const clockid_t clockid)
1482{
1483 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001484 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001485 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001486 unsigned long slack;
1487
1488 slack = current->timer_slack_ns;
1489 if (rt_task(current))
1490 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001491
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001492 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001493 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001494 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001495 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001496
George Anzinger79786722006-02-01 03:05:11 -08001497 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001498 if (mode == HRTIMER_MODE_ABS) {
1499 ret = -ERESTARTNOHAND;
1500 goto out;
1501 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001502
Roman Zippel432569b2006-03-26 01:38:08 -08001503 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001504 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001505 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001506 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001507 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001508
1509 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001510 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001511 restart->nanosleep.index = t.timer.base->index;
1512 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001513 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001514
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001515 ret = -ERESTART_RESTARTBLOCK;
1516out:
1517 destroy_hrtimer_on_stack(&t.timer);
1518 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001519}
1520
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001521SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1522 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001523{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001524 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001525
1526 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1527 return -EFAULT;
1528
1529 if (!timespec_valid(&tu))
1530 return -EINVAL;
1531
Oleg Nesterov080344b2008-02-01 17:29:05 +03001532 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001533}
1534
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001535/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001536 * Functions related to boot-time initialization:
1537 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001538static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001539{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001540 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001541 int i;
1542
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001543 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001544
1545 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1546 cpu_base->clock_base[i].cpu_base = cpu_base;
1547
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001548 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001549}
1550
1551#ifdef CONFIG_HOTPLUG_CPU
1552
Peter Zijlstraca109492008-11-25 12:43:51 +01001553static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001554 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001555{
1556 struct hrtimer *timer;
1557 struct rb_node *node;
1558
1559 while ((node = rb_first(&old_base->active))) {
1560 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001561 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001562 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001563
1564 /*
1565 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1566 * timer could be seen as !active and just vanish away
1567 * under us on another CPU
1568 */
1569 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001570 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001571 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001572 * Enqueue the timers on the new cpu. This does not
1573 * reprogram the event device in case the timer
1574 * expires before the earliest on this CPU, but we run
1575 * hrtimer_interrupt after we migrated everything to
1576 * sort out already expired timers and reprogram the
1577 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001578 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001579 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001580
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001581 /* Clear the migration state bit */
1582 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001583 }
1584}
1585
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001586static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001587{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001588 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001589 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001590
Peter Zijlstra37810652008-12-04 11:17:10 +01001591 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001592 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001593
1594 local_irq_disable();
1595 old_base = &per_cpu(hrtimer_bases, scpu);
1596 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001597 /*
1598 * The caller is globally serialized and nobody else
1599 * takes two locks at once, deadlock is not possible.
1600 */
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001601 spin_lock(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001602 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001603
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001604 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001605 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001606 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001607 }
1608
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001609 spin_unlock(&old_base->lock);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001610 spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001611
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001612 /* Check, if we got expired work to do */
1613 __hrtimer_peek_ahead_timers();
1614 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001615}
1616
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001617#endif /* CONFIG_HOTPLUG_CPU */
1618
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001619static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001620 unsigned long action, void *hcpu)
1621{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001622 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001623
1624 switch (action) {
1625
1626 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001627 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001628 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001629 break;
1630
1631#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001632 case CPU_DYING:
1633 case CPU_DYING_FROZEN:
1634 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1635 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001636 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001637 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001638 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001639 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001640 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001641 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001642 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001643#endif
1644
1645 default:
1646 break;
1647 }
1648
1649 return NOTIFY_OK;
1650}
1651
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001652static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001653 .notifier_call = hrtimer_cpu_notify,
1654};
1655
1656void __init hrtimers_init(void)
1657{
1658 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1659 (void *)(long)smp_processor_id());
1660 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001661#ifdef CONFIG_HIGH_RES_TIMERS
1662 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1663#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001664}
1665
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001666/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001667 * schedule_hrtimeout_range - sleep until timeout
1668 * @expires: timeout value (ktime_t)
1669 * @delta: slack in expires timeout (ktime_t)
1670 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1671 *
1672 * Make the current task sleep until the given expiry time has
1673 * elapsed. The routine will return immediately unless
1674 * the current task state has been set (see set_current_state()).
1675 *
1676 * The @delta argument gives the kernel the freedom to schedule the
1677 * actual wakeup to a time that is both power and performance friendly.
1678 * The kernel give the normal best effort behavior for "@expires+@delta",
1679 * but may decide to fire the timer earlier, but no earlier than @expires.
1680 *
1681 * You can set the task state as follows -
1682 *
1683 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1684 * pass before the routine returns.
1685 *
1686 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1687 * delivered to the current task.
1688 *
1689 * The current task state is guaranteed to be TASK_RUNNING when this
1690 * routine returns.
1691 *
1692 * Returns 0 when the timer has expired otherwise -EINTR
1693 */
1694int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1695 const enum hrtimer_mode mode)
1696{
1697 struct hrtimer_sleeper t;
1698
1699 /*
1700 * Optimize when a zero timeout value is given. It does not
1701 * matter whether this is an absolute or a relative time.
1702 */
1703 if (expires && !expires->tv64) {
1704 __set_current_state(TASK_RUNNING);
1705 return 0;
1706 }
1707
1708 /*
1709 * A NULL parameter means "inifinte"
1710 */
1711 if (!expires) {
1712 schedule();
1713 __set_current_state(TASK_RUNNING);
1714 return -EINTR;
1715 }
1716
1717 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1718 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1719
1720 hrtimer_init_sleeper(&t, current);
1721
1722 hrtimer_start_expires(&t.timer, mode);
1723 if (!hrtimer_active(&t.timer))
1724 t.task = NULL;
1725
1726 if (likely(t.task))
1727 schedule();
1728
1729 hrtimer_cancel(&t.timer);
1730 destroy_hrtimer_on_stack(&t.timer);
1731
1732 __set_current_state(TASK_RUNNING);
1733
1734 return !t.task ? 0 : -EINTR;
1735}
1736EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1737
1738/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001739 * schedule_hrtimeout - sleep until timeout
1740 * @expires: timeout value (ktime_t)
1741 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1742 *
1743 * Make the current task sleep until the given expiry time has
1744 * elapsed. The routine will return immediately unless
1745 * the current task state has been set (see set_current_state()).
1746 *
1747 * You can set the task state as follows -
1748 *
1749 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1750 * pass before the routine returns.
1751 *
1752 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1753 * delivered to the current task.
1754 *
1755 * The current task state is guaranteed to be TASK_RUNNING when this
1756 * routine returns.
1757 *
1758 * Returns 0 when the timer has expired otherwise -EINTR
1759 */
1760int __sched schedule_hrtimeout(ktime_t *expires,
1761 const enum hrtimer_mode mode)
1762{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001763 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001764}
1765EXPORT_SYMBOL_GPL(schedule_hrtimeout);