blob: 18f6906169dab70136d424689d8da2c5a55bb7e9 [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 Anzinger7978672c2006-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 *
196switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800197{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800198 struct hrtimer_clock_base *new_base;
199 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800200
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800201 new_cpu_base = &__get_cpu_var(hrtimer_bases);
202 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800203
204 if (base != new_base) {
205 /*
206 * We are trying to schedule the timer on the local CPU.
207 * However we can't change timer's base while it is running,
208 * so we keep it on the same CPU. No hassle vs. reprogramming
209 * the event source in the high resolution case. The softirq
210 * code will take care of this when the timer function has
211 * completed. There is no conflict as we hold the lock until
212 * the timer is enqueued.
213 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800214 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800215 return base;
216
217 /* See the comment in lock_timer_base() */
218 timer->base = NULL;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800219 spin_unlock(&base->cpu_base->lock);
220 spin_lock(&new_base->cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800221 timer->base = new_base;
222 }
223 return new_base;
224}
225
226#else /* CONFIG_SMP */
227
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800228static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800229lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
230{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800231 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800232
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800233 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800234
235 return base;
236}
237
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800238# define switch_hrtimer_base(t, b) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800239
240#endif /* !CONFIG_SMP */
241
242/*
243 * Functions for the union type storage format of ktime_t which are
244 * too large for inlining:
245 */
246#if BITS_PER_LONG < 64
247# ifndef CONFIG_KTIME_SCALAR
248/**
249 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800250 * @kt: addend
251 * @nsec: the scalar nsec value to add
252 *
253 * Returns the sum of kt and nsec in ktime_t format
254 */
255ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
256{
257 ktime_t tmp;
258
259 if (likely(nsec < NSEC_PER_SEC)) {
260 tmp.tv64 = nsec;
261 } else {
262 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
263
264 tmp = ktime_set((long)nsec, rem);
265 }
266
267 return ktime_add(kt, tmp);
268}
David Howellsb8b8fd22007-04-27 15:31:24 -0700269
270EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700271
272/**
273 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
274 * @kt: minuend
275 * @nsec: the scalar nsec value to subtract
276 *
277 * Returns the subtraction of @nsec from @kt in ktime_t format
278 */
279ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
280{
281 ktime_t tmp;
282
283 if (likely(nsec < NSEC_PER_SEC)) {
284 tmp.tv64 = nsec;
285 } else {
286 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
287
288 tmp = ktime_set((long)nsec, rem);
289 }
290
291 return ktime_sub(kt, tmp);
292}
293
294EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800295# endif /* !CONFIG_KTIME_SCALAR */
296
297/*
298 * Divide a ktime value by a nanosecond value
299 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800300u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800301{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300302 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800303 int sft = 0;
304
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300305 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800306 /* Make sure the divisor is less than 2^32: */
307 while (div >> 32) {
308 sft++;
309 div >>= 1;
310 }
311 dclc >>= sft;
312 do_div(dclc, (unsigned long) div);
313
Davide Libenzi4d672e72008-02-04 22:27:26 -0800314 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800315}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800316#endif /* BITS_PER_LONG >= 64 */
317
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100318/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100319 * Add two ktime values and do a safety check for overflow:
320 */
321ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
322{
323 ktime_t res = ktime_add(lhs, rhs);
324
325 /*
326 * We use KTIME_SEC_MAX here, the maximum timeout which we can
327 * return to user space in a timespec:
328 */
329 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
330 res = ktime_set(KTIME_SEC_MAX, 0);
331
332 return res;
333}
334
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300335EXPORT_SYMBOL_GPL(ktime_add_safe);
336
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700337#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
338
339static struct debug_obj_descr hrtimer_debug_descr;
340
341/*
342 * fixup_init is called when:
343 * - an active object is initialized
344 */
345static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
346{
347 struct hrtimer *timer = addr;
348
349 switch (state) {
350 case ODEBUG_STATE_ACTIVE:
351 hrtimer_cancel(timer);
352 debug_object_init(timer, &hrtimer_debug_descr);
353 return 1;
354 default:
355 return 0;
356 }
357}
358
359/*
360 * fixup_activate is called when:
361 * - an active object is activated
362 * - an unknown object is activated (might be a statically initialized object)
363 */
364static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
365{
366 switch (state) {
367
368 case ODEBUG_STATE_NOTAVAILABLE:
369 WARN_ON_ONCE(1);
370 return 0;
371
372 case ODEBUG_STATE_ACTIVE:
373 WARN_ON(1);
374
375 default:
376 return 0;
377 }
378}
379
380/*
381 * fixup_free is called when:
382 * - an active object is freed
383 */
384static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
385{
386 struct hrtimer *timer = addr;
387
388 switch (state) {
389 case ODEBUG_STATE_ACTIVE:
390 hrtimer_cancel(timer);
391 debug_object_free(timer, &hrtimer_debug_descr);
392 return 1;
393 default:
394 return 0;
395 }
396}
397
398static struct debug_obj_descr hrtimer_debug_descr = {
399 .name = "hrtimer",
400 .fixup_init = hrtimer_fixup_init,
401 .fixup_activate = hrtimer_fixup_activate,
402 .fixup_free = hrtimer_fixup_free,
403};
404
405static inline void debug_hrtimer_init(struct hrtimer *timer)
406{
407 debug_object_init(timer, &hrtimer_debug_descr);
408}
409
410static inline void debug_hrtimer_activate(struct hrtimer *timer)
411{
412 debug_object_activate(timer, &hrtimer_debug_descr);
413}
414
415static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
416{
417 debug_object_deactivate(timer, &hrtimer_debug_descr);
418}
419
420static inline void debug_hrtimer_free(struct hrtimer *timer)
421{
422 debug_object_free(timer, &hrtimer_debug_descr);
423}
424
425static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
426 enum hrtimer_mode mode);
427
428void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
429 enum hrtimer_mode mode)
430{
431 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
432 __hrtimer_init(timer, clock_id, mode);
433}
434
435void destroy_hrtimer_on_stack(struct hrtimer *timer)
436{
437 debug_object_free(timer, &hrtimer_debug_descr);
438}
439
440#else
441static inline void debug_hrtimer_init(struct hrtimer *timer) { }
442static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
443static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
444#endif
445
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800446/* High resolution timer related functions */
447#ifdef CONFIG_HIGH_RES_TIMERS
448
449/*
450 * High resolution timer enabled ?
451 */
452static int hrtimer_hres_enabled __read_mostly = 1;
453
454/*
455 * Enable / Disable high resolution mode
456 */
457static int __init setup_hrtimer_hres(char *str)
458{
459 if (!strcmp(str, "off"))
460 hrtimer_hres_enabled = 0;
461 else if (!strcmp(str, "on"))
462 hrtimer_hres_enabled = 1;
463 else
464 return 0;
465 return 1;
466}
467
468__setup("highres=", setup_hrtimer_hres);
469
470/*
471 * hrtimer_high_res_enabled - query, if the highres mode is enabled
472 */
473static inline int hrtimer_is_hres_enabled(void)
474{
475 return hrtimer_hres_enabled;
476}
477
478/*
479 * Is the high resolution mode active ?
480 */
481static inline int hrtimer_hres_active(void)
482{
483 return __get_cpu_var(hrtimer_bases).hres_active;
484}
485
486/*
487 * Reprogram the event source with checking both queues for the
488 * next event
489 * Called with interrupts disabled and base->lock held
490 */
491static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
492{
493 int i;
494 struct hrtimer_clock_base *base = cpu_base->clock_base;
495 ktime_t expires;
496
497 cpu_base->expires_next.tv64 = KTIME_MAX;
498
499 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
500 struct hrtimer *timer;
501
502 if (!base->first)
503 continue;
504 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700505 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100506 /*
507 * clock_was_set() has changed base->offset so the
508 * result might be negative. Fix it up to prevent a
509 * false positive in clockevents_program_event()
510 */
511 if (expires.tv64 < 0)
512 expires.tv64 = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800513 if (expires.tv64 < cpu_base->expires_next.tv64)
514 cpu_base->expires_next = expires;
515 }
516
517 if (cpu_base->expires_next.tv64 != KTIME_MAX)
518 tick_program_event(cpu_base->expires_next, 1);
519}
520
521/*
522 * Shared reprogramming for clock_realtime and clock_monotonic
523 *
524 * When a timer is enqueued and expires earlier than the already enqueued
525 * timers, we have to check, whether it expires earlier than the timer for
526 * which the clock event device was armed.
527 *
528 * Called with interrupts disabled and base->cpu_base.lock held
529 */
530static int hrtimer_reprogram(struct hrtimer *timer,
531 struct hrtimer_clock_base *base)
532{
533 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700534 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800535 int res;
536
Arjan van de Vencc584b22008-09-01 15:02:30 -0700537 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100538
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800539 /*
540 * When the callback is running, we do not reprogram the clock event
541 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200542 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800543 * reprogramming is handled either by the softirq, which called the
544 * callback or at the end of the hrtimer_interrupt.
545 */
546 if (hrtimer_callback_running(timer))
547 return 0;
548
Thomas Gleixner63070a72008-02-14 00:58:36 +0100549 /*
550 * CLOCK_REALTIME timer might be requested with an absolute
551 * expiry time which is less than base->offset. Nothing wrong
552 * about that, just avoid to call into the tick code, which
553 * has now objections against negative expiry values.
554 */
555 if (expires.tv64 < 0)
556 return -ETIME;
557
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800558 if (expires.tv64 >= expires_next->tv64)
559 return 0;
560
561 /*
562 * Clockevents returns -ETIME, when the event was in the past.
563 */
564 res = tick_program_event(expires, 0);
565 if (!IS_ERR_VALUE(res))
566 *expires_next = expires;
567 return res;
568}
569
570
571/*
572 * Retrigger next event is called after clock was set
573 *
574 * Called with interrupts disabled via on_each_cpu()
575 */
576static void retrigger_next_event(void *arg)
577{
578 struct hrtimer_cpu_base *base;
579 struct timespec realtime_offset;
580 unsigned long seq;
581
582 if (!hrtimer_hres_active())
583 return;
584
585 do {
586 seq = read_seqbegin(&xtime_lock);
587 set_normalized_timespec(&realtime_offset,
588 -wall_to_monotonic.tv_sec,
589 -wall_to_monotonic.tv_nsec);
590 } while (read_seqretry(&xtime_lock, seq));
591
592 base = &__get_cpu_var(hrtimer_bases);
593
594 /* Adjust CLOCK_REALTIME offset */
595 spin_lock(&base->lock);
596 base->clock_base[CLOCK_REALTIME].offset =
597 timespec_to_ktime(realtime_offset);
598
599 hrtimer_force_reprogram(base);
600 spin_unlock(&base->lock);
601}
602
603/*
604 * Clock realtime was set
605 *
606 * Change the offset of the realtime clock vs. the monotonic
607 * clock.
608 *
609 * We might have to reprogram the high resolution timer interrupt. On
610 * SMP we call the architecture specific code to retrigger _all_ high
611 * resolution timer interrupts. On UP we just disable interrupts and
612 * call the high resolution interrupt code.
613 */
614void clock_was_set(void)
615{
616 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200617 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800618}
619
620/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200621 * During resume we might have to reprogram the high resolution timer
622 * interrupt (on the local CPU):
623 */
624void hres_timers_resume(void)
625{
Peter Zijlstra1d4a7f12009-01-18 16:39:29 +0100626 WARN_ONCE(!irqs_disabled(),
627 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
628
Ingo Molnar995f0542007-04-07 12:05:00 +0200629 retrigger_next_event(NULL);
630}
631
632/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800633 * Initialize the high resolution related parts of cpu_base
634 */
635static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
636{
637 base->expires_next.tv64 = KTIME_MAX;
638 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800639}
640
641/*
642 * Initialize the high resolution related parts of a hrtimer
643 */
644static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
645{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800646}
647
Peter Zijlstraca109492008-11-25 12:43:51 +0100648
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800649/*
650 * When High resolution timers are active, try to reprogram. Note, that in case
651 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
652 * check happens. The timer gets enqueued into the rbtree. The reprogramming
653 * and expiry check is done in the hrtimer_interrupt or in the softirq.
654 */
655static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100656 struct hrtimer_clock_base *base,
657 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800658{
659 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100660 if (wakeup) {
661 spin_unlock(&base->cpu_base->lock);
662 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
663 spin_lock(&base->cpu_base->lock);
664 } else
665 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
666
Peter Zijlstraca109492008-11-25 12:43:51 +0100667 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800668 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100669
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800670 return 0;
671}
672
673/*
674 * Switch to high resolution mode
675 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800676static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800677{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700678 int cpu = smp_processor_id();
679 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800680 unsigned long flags;
681
682 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800683 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800684
685 local_irq_save(flags);
686
687 if (tick_init_highres()) {
688 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700689 printk(KERN_WARNING "Could not switch to high resolution "
690 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800691 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800692 }
693 base->hres_active = 1;
694 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
695 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
696
697 tick_setup_sched_timer();
698
699 /* "Retrigger" the interrupt to get things going */
700 retrigger_next_event(NULL);
701 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100702 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800703 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800704 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800705}
706
707#else
708
709static inline int hrtimer_hres_active(void) { return 0; }
710static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800711static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800712static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
713static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100714 struct hrtimer_clock_base *base,
715 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800716{
717 return 0;
718}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800719static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
720static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
721
722#endif /* CONFIG_HIGH_RES_TIMERS */
723
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800724#ifdef CONFIG_TIMER_STATS
725void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
726{
727 if (timer->start_site)
728 return;
729
730 timer->start_site = addr;
731 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
732 timer->start_pid = current->pid;
733}
734#endif
735
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800736/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200737 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800738 */
739static inline
740void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
741{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800742 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800743}
744
745/**
746 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800747 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800748 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800749 * @interval: the interval to forward
750 *
751 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700752 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800753 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800754u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800755{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800756 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800757 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800758
Arjan van de Vencc584b22008-09-01 15:02:30 -0700759 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800760
761 if (delta.tv64 < 0)
762 return 0;
763
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100764 if (interval.tv64 < timer->base->resolution.tv64)
765 interval.tv64 = timer->base->resolution.tv64;
766
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800767 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800768 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800769
770 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700771 hrtimer_add_expires_ns(timer, incr * orun);
772 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800773 return orun;
774 /*
775 * This (and the ktime_add() below) is the
776 * correction for exact:
777 */
778 orun++;
779 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700780 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800781
782 return orun;
783}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700784EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800785
786/*
787 * enqueue_hrtimer - internal function to (re)start a timer
788 *
789 * The timer is inserted in expiry order. Insertion into the
790 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100791 *
792 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800793 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100794static int enqueue_hrtimer(struct hrtimer *timer,
795 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800796{
797 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800798 struct rb_node *parent = NULL;
799 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700800 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800801
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700802 debug_hrtimer_activate(timer);
803
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800804 /*
805 * Find the right place in the rbtree:
806 */
807 while (*link) {
808 parent = *link;
809 entry = rb_entry(parent, struct hrtimer, node);
810 /*
811 * We dont care about collisions. Nodes with
812 * the same expiry time stay together.
813 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700814 if (hrtimer_get_expires_tv64(timer) <
815 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800816 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700817 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800818 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700819 leftmost = 0;
820 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821 }
822
823 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100824 * Insert the timer to the rbtree and check whether it
825 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800826 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100827 if (leftmost)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800828 base->first = &timer->node;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800829
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800830 rb_link_node(&timer->node, parent, link);
831 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800832 /*
833 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
834 * state of a possibly running callback.
835 */
836 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100837
838 return leftmost;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100839}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800840
841/*
842 * __remove_hrtimer - internal function to remove a timer
843 *
844 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800845 *
846 * High resolution timer mode reprograms the clock event device when the
847 * timer is the one which expires next. The caller can disable this by setting
848 * reprogram to zero. This is useful, when the context does a reprogramming
849 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800850 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800851static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800852 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800853 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800854{
Peter Zijlstraca109492008-11-25 12:43:51 +0100855 if (timer->state & HRTIMER_STATE_ENQUEUED) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800856 /*
857 * Remove the timer from the rbtree and replace the
858 * first entry pointer if necessary.
859 */
860 if (base->first == &timer->node) {
861 base->first = rb_next(&timer->node);
862 /* Reprogram the clock event device. if enabled */
863 if (reprogram && hrtimer_hres_active())
864 hrtimer_force_reprogram(base->cpu_base);
865 }
866 rb_erase(&timer->node, &base->active);
867 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800868 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800869}
870
871/*
872 * remove hrtimer, called with base lock held
873 */
874static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800875remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800876{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800877 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800878 int reprogram;
879
880 /*
881 * Remove the timer and force reprogramming when high
882 * resolution mode is active and the timer is on the current
883 * CPU. If we remove a timer on another CPU, reprogramming is
884 * skipped. The interrupt event on this CPU is fired and
885 * reprogramming happens in the interrupt handler. This is a
886 * rare case and less expensive than a smp call.
887 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700888 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800889 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800890 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
891 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
892 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800893 return 1;
894 }
895 return 0;
896}
897
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100898int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
899 unsigned long delta_ns, const enum hrtimer_mode mode,
900 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800901{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800902 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800903 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100904 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800905
906 base = lock_hrtimer_base(timer, &flags);
907
908 /* Remove an active timer from the queue: */
909 ret = remove_hrtimer(timer, base);
910
911 /* Switch the timer base, if necessary: */
912 new_base = switch_hrtimer_base(timer, base);
913
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800914 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100915 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800916 /*
917 * CONFIG_TIME_LOW_RES is a temporary way for architectures
918 * to signal that they simply return xtime in
919 * do_gettimeoffset(). In this case we want to round up by
920 * resolution when starting a relative timer, to avoid short
921 * timeouts. This will go away with the GTOD framework.
922 */
923#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100924 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800925#endif
926 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700927
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700928 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800929
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800930 timer_stats_hrtimer_set_start_info(timer);
931
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100932 leftmost = enqueue_hrtimer(timer, new_base);
933
Ingo Molnar935c6312007-03-28 13:17:18 +0200934 /*
935 * Only allow reprogramming if the new base is on this CPU.
936 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100937 *
938 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200939 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100940 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100941 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800942
943 unlock_hrtimer_base(timer, &flags);
944
945 return ret;
946}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100947
948/**
949 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
950 * @timer: the timer to be added
951 * @tim: expiry time
952 * @delta_ns: "slack" range for the timer
953 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
954 *
955 * Returns:
956 * 0 on success
957 * 1 when the timer was active
958 */
959int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
960 unsigned long delta_ns, const enum hrtimer_mode mode)
961{
962 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
963}
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700964EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
965
966/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200967 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700968 * @timer: the timer to be added
969 * @tim: expiry time
970 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
971 *
972 * Returns:
973 * 0 on success
974 * 1 when the timer was active
975 */
976int
977hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
978{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100979 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700980}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700981EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800982
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700983
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800984/**
985 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800986 * @timer: hrtimer to stop
987 *
988 * Returns:
989 * 0 when the timer was not active
990 * 1 when the timer was active
991 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700992 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800993 */
994int hrtimer_try_to_cancel(struct hrtimer *timer)
995{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800996 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800997 unsigned long flags;
998 int ret = -1;
999
1000 base = lock_hrtimer_base(timer, &flags);
1001
Thomas Gleixner303e9672007-02-16 01:27:51 -08001002 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001003 ret = remove_hrtimer(timer, base);
1004
1005 unlock_hrtimer_base(timer, &flags);
1006
1007 return ret;
1008
1009}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001010EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001011
1012/**
1013 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001014 * @timer: the timer to be cancelled
1015 *
1016 * Returns:
1017 * 0 when the timer was not active
1018 * 1 when the timer was active
1019 */
1020int hrtimer_cancel(struct hrtimer *timer)
1021{
1022 for (;;) {
1023 int ret = hrtimer_try_to_cancel(timer);
1024
1025 if (ret >= 0)
1026 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001027 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001028 }
1029}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001030EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001031
1032/**
1033 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001034 * @timer: the timer to read
1035 */
1036ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1037{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001038 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001039 unsigned long flags;
1040 ktime_t rem;
1041
1042 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001043 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001044 unlock_hrtimer_base(timer, &flags);
1045
1046 return rem;
1047}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001048EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001049
Russell Kingee9c5782008-04-20 13:59:33 +01001050#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001051/**
1052 * hrtimer_get_next_event - get the time until next expiry event
1053 *
1054 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1055 * is pending.
1056 */
1057ktime_t hrtimer_get_next_event(void)
1058{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001059 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1060 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001061 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1062 unsigned long flags;
1063 int i;
1064
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001065 spin_lock_irqsave(&cpu_base->lock, flags);
1066
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001067 if (!hrtimer_hres_active()) {
1068 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1069 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001070
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001071 if (!base->first)
1072 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001073
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001074 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001075 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001076 delta = ktime_sub(delta, base->get_time());
1077 if (delta.tv64 < mindelta.tv64)
1078 mindelta.tv64 = delta.tv64;
1079 }
Tony Lindgren69239742006-03-06 15:42:45 -08001080 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001081
1082 spin_unlock_irqrestore(&cpu_base->lock, flags);
1083
Tony Lindgren69239742006-03-06 15:42:45 -08001084 if (mindelta.tv64 < 0)
1085 mindelta.tv64 = 0;
1086 return mindelta;
1087}
1088#endif
1089
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001090static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1091 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001092{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001093 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001094
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001095 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001096
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001097 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001098
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001099 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001100 clock_id = CLOCK_MONOTONIC;
1101
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001102 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001103 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001104 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001105
1106#ifdef CONFIG_TIMER_STATS
1107 timer->start_site = NULL;
1108 timer->start_pid = -1;
1109 memset(timer->start_comm, 0, TASK_COMM_LEN);
1110#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001111}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001112
1113/**
1114 * hrtimer_init - initialize a timer to the given clock
1115 * @timer: the timer to be initialized
1116 * @clock_id: the clock to be used
1117 * @mode: timer mode abs/rel
1118 */
1119void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1120 enum hrtimer_mode mode)
1121{
1122 debug_hrtimer_init(timer);
1123 __hrtimer_init(timer, clock_id, mode);
1124}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001125EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001126
1127/**
1128 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001129 * @which_clock: which clock to query
1130 * @tp: pointer to timespec variable to store the resolution
1131 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001132 * Store the resolution of the clock selected by @which_clock in the
1133 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001134 */
1135int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1136{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001137 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001138
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001139 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1140 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001141
1142 return 0;
1143}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001144EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001145
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001146static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001147{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001148 struct hrtimer_clock_base *base = timer->base;
1149 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1150 enum hrtimer_restart (*fn)(struct hrtimer *);
1151 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001152
Peter Zijlstraca109492008-11-25 12:43:51 +01001153 WARN_ON(!irqs_disabled());
1154
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001155 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001156 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1157 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001158 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001159
1160 /*
1161 * Because we run timers from hardirq context, there is no chance
1162 * they get migrated to another cpu, therefore its safe to unlock
1163 * the timer base.
1164 */
1165 spin_unlock(&cpu_base->lock);
1166 restart = fn(timer);
1167 spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001168
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001169 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001170 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1171 * we do not reprogramm the event hardware. Happens either in
1172 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001173 */
1174 if (restart != HRTIMER_NORESTART) {
1175 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001176 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001177 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001178 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001179}
1180
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001181#ifdef CONFIG_HIGH_RES_TIMERS
1182
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001183static int force_clock_reprogram;
1184
1185/*
1186 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1187 * is hanging, which could happen with something that slows the interrupt
1188 * such as the tracing. Then we force the clock reprogramming for each future
1189 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1190 * threshold that we will overwrite.
1191 * The next tick event will be scheduled to 3 times we currently spend on
1192 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1193 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1194 * let it running without serious starvation.
1195 */
1196
1197static inline void
1198hrtimer_interrupt_hanging(struct clock_event_device *dev,
1199 ktime_t try_time)
1200{
1201 force_clock_reprogram = 1;
1202 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1203 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1204 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1205}
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001206/*
1207 * High resolution timer interrupt
1208 * Called with interrupts disabled
1209 */
1210void hrtimer_interrupt(struct clock_event_device *dev)
1211{
1212 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1213 struct hrtimer_clock_base *base;
1214 ktime_t expires_next, now;
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001215 int nr_retries = 0;
Peter Zijlstraca109492008-11-25 12:43:51 +01001216 int i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001217
1218 BUG_ON(!cpu_base->hres_active);
1219 cpu_base->nr_events++;
1220 dev->next_event.tv64 = KTIME_MAX;
1221
1222 retry:
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001223 /* 5 retries is enough to notice a hang */
1224 if (!(++nr_retries % 5))
1225 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1226
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001227 now = ktime_get();
1228
1229 expires_next.tv64 = KTIME_MAX;
1230
1231 base = cpu_base->clock_base;
1232
1233 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1234 ktime_t basenow;
1235 struct rb_node *node;
1236
1237 spin_lock(&cpu_base->lock);
1238
1239 basenow = ktime_add(now, base->offset);
1240
1241 while ((node = base->first)) {
1242 struct hrtimer *timer;
1243
1244 timer = rb_entry(node, struct hrtimer, node);
1245
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001246 /*
1247 * The immediate goal for using the softexpires is
1248 * minimizing wakeups, not running timers at the
1249 * earliest interrupt after their soft expiration.
1250 * This allows us to avoid using a Priority Search
1251 * Tree, which can answer a stabbing querry for
1252 * overlapping intervals and instead use the simple
1253 * BST we already have.
1254 * We don't add extra wakeups by delaying timers that
1255 * are right-of a not yet expired timer, because that
1256 * timer will have to trigger a wakeup anyway.
1257 */
1258
1259 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001260 ktime_t expires;
1261
Arjan van de Vencc584b22008-09-01 15:02:30 -07001262 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001263 base->offset);
1264 if (expires.tv64 < expires_next.tv64)
1265 expires_next = expires;
1266 break;
1267 }
1268
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001269 __run_hrtimer(timer);
1270 }
1271 spin_unlock(&cpu_base->lock);
1272 base++;
1273 }
1274
1275 cpu_base->expires_next = expires_next;
1276
1277 /* Reprogramming necessary ? */
1278 if (expires_next.tv64 != KTIME_MAX) {
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001279 if (tick_program_event(expires_next, force_clock_reprogram))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001280 goto retry;
1281 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001282}
1283
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001284/*
1285 * local version of hrtimer_peek_ahead_timers() called with interrupts
1286 * disabled.
1287 */
1288static void __hrtimer_peek_ahead_timers(void)
1289{
1290 struct tick_device *td;
1291
1292 if (!hrtimer_hres_active())
1293 return;
1294
1295 td = &__get_cpu_var(tick_cpu_device);
1296 if (td && td->evtdev)
1297 hrtimer_interrupt(td->evtdev);
1298}
1299
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001300/**
1301 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1302 *
1303 * hrtimer_peek_ahead_timers will peek at the timer queue of
1304 * the current cpu and check if there are any timers for which
1305 * the soft expires time has passed. If any such timers exist,
1306 * they are run immediately and then removed from the timer queue.
1307 *
1308 */
1309void hrtimer_peek_ahead_timers(void)
1310{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001311 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001312
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001313 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001314 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001315 local_irq_restore(flags);
1316}
1317
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001318static void run_hrtimer_softirq(struct softirq_action *h)
1319{
1320 hrtimer_peek_ahead_timers();
1321}
1322
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001323#else /* CONFIG_HIGH_RES_TIMERS */
1324
1325static inline void __hrtimer_peek_ahead_timers(void) { }
1326
1327#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001328
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001329/*
1330 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001331 *
1332 * For HRT its the fall back code to run the softirq in the timer
1333 * softirq context in case the hrtimer initialization failed or has
1334 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001335 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001336void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001337{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001338 if (hrtimer_hres_active())
1339 return;
1340
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001341 /*
1342 * This _is_ ugly: We have to check in the softirq context,
1343 * whether we can switch to highres and / or nohz mode. The
1344 * clocksource switch happens in the timer interrupt with
1345 * xtime_lock held. Notification from there only sets the
1346 * check bit in the tick_oneshot code, otherwise we might
1347 * deadlock vs. xtime_lock.
1348 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001349 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001350 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001351}
1352
1353/*
1354 * Called from hardirq context every jiffy
1355 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001356void hrtimer_run_queues(void)
1357{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001358 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001359 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001360 struct hrtimer_clock_base *base;
1361 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001362
1363 if (hrtimer_hres_active())
1364 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001365
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001366 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1367 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001368
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001369 if (!base->first)
1370 continue;
1371
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001372 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001373 hrtimer_get_softirq_time(cpu_base);
1374 gettime = 0;
1375 }
1376
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001377 spin_lock(&cpu_base->lock);
1378
1379 while ((node = base->first)) {
1380 struct hrtimer *timer;
1381
1382 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001383 if (base->softirq_time.tv64 <=
1384 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001385 break;
1386
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001387 __run_hrtimer(timer);
1388 }
1389 spin_unlock(&cpu_base->lock);
1390 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001391}
1392
1393/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001394 * Sleep related functions:
1395 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001396static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001397{
1398 struct hrtimer_sleeper *t =
1399 container_of(timer, struct hrtimer_sleeper, timer);
1400 struct task_struct *task = t->task;
1401
1402 t->task = NULL;
1403 if (task)
1404 wake_up_process(task);
1405
1406 return HRTIMER_NORESTART;
1407}
1408
Ingo Molnar36c8b582006-07-03 00:25:41 -07001409void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001410{
1411 sl->timer.function = hrtimer_wakeup;
1412 sl->task = task;
1413}
1414
Thomas Gleixner669d7862006-03-31 02:31:19 -08001415static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001416{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001417 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001418
Roman Zippel432569b2006-03-26 01:38:08 -08001419 do {
1420 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001421 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001422 if (!hrtimer_active(&t->timer))
1423 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001424
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001425 if (likely(t->task))
1426 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001427
Thomas Gleixner669d7862006-03-31 02:31:19 -08001428 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001429 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001430
Thomas Gleixner669d7862006-03-31 02:31:19 -08001431 } while (t->task && !signal_pending(current));
1432
Peter Zijlstra3588a082008-02-01 17:45:13 +01001433 __set_current_state(TASK_RUNNING);
1434
Thomas Gleixner669d7862006-03-31 02:31:19 -08001435 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001436}
1437
Oleg Nesterov080344b2008-02-01 17:29:05 +03001438static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1439{
1440 struct timespec rmt;
1441 ktime_t rem;
1442
Arjan van de Vencc584b22008-09-01 15:02:30 -07001443 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001444 if (rem.tv64 <= 0)
1445 return 0;
1446 rmt = ktime_to_timespec(rem);
1447
1448 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1449 return -EFAULT;
1450
1451 return 1;
1452}
1453
Toyo Abe1711ef32006-09-29 02:00:28 -07001454long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001455{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001456 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001457 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001458 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001459
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001460 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1461 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001462 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001463
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001464 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001465 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001466
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001467 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001468 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001469 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001470 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001471 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001472 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001473
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001474 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001475 ret = -ERESTART_RESTARTBLOCK;
1476out:
1477 destroy_hrtimer_on_stack(&t.timer);
1478 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001479}
1480
Oleg Nesterov080344b2008-02-01 17:29:05 +03001481long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001482 const enum hrtimer_mode mode, const clockid_t clockid)
1483{
1484 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001485 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001486 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001487 unsigned long slack;
1488
1489 slack = current->timer_slack_ns;
1490 if (rt_task(current))
1491 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001492
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001493 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001494 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001495 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001496 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001497
George Anzinger7978672c2006-02-01 03:05:11 -08001498 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001499 if (mode == HRTIMER_MODE_ABS) {
1500 ret = -ERESTARTNOHAND;
1501 goto out;
1502 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001503
Roman Zippel432569b2006-03-26 01:38:08 -08001504 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001505 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001506 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001507 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001508 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001509
1510 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001511 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001512 restart->nanosleep.index = t.timer.base->index;
1513 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001514 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001515
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001516 ret = -ERESTART_RESTARTBLOCK;
1517out:
1518 destroy_hrtimer_on_stack(&t.timer);
1519 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001520}
1521
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001522SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1523 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001524{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001525 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001526
1527 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1528 return -EFAULT;
1529
1530 if (!timespec_valid(&tu))
1531 return -EINVAL;
1532
Oleg Nesterov080344b2008-02-01 17:29:05 +03001533 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001534}
1535
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001536/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001537 * Functions related to boot-time initialization:
1538 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001539static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001540{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001541 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001542 int i;
1543
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001544 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001545
1546 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1547 cpu_base->clock_base[i].cpu_base = cpu_base;
1548
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001549 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001550}
1551
1552#ifdef CONFIG_HOTPLUG_CPU
1553
Peter Zijlstraca109492008-11-25 12:43:51 +01001554static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001555 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001556{
1557 struct hrtimer *timer;
1558 struct rb_node *node;
1559
1560 while ((node = rb_first(&old_base->active))) {
1561 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001562 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001563 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001564
1565 /*
1566 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1567 * timer could be seen as !active and just vanish away
1568 * under us on another CPU
1569 */
1570 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001571 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001572 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001573 * Enqueue the timers on the new cpu. This does not
1574 * reprogram the event device in case the timer
1575 * expires before the earliest on this CPU, but we run
1576 * hrtimer_interrupt after we migrated everything to
1577 * sort out already expired timers and reprogram the
1578 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001579 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001580 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001581
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001582 /* Clear the migration state bit */
1583 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001584 }
1585}
1586
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001587static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001588{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001589 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001590 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001591
Peter Zijlstra37810652008-12-04 11:17:10 +01001592 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001593 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001594
1595 local_irq_disable();
1596 old_base = &per_cpu(hrtimer_bases, scpu);
1597 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001598 /*
1599 * The caller is globally serialized and nobody else
1600 * takes two locks at once, deadlock is not possible.
1601 */
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001602 spin_lock(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001603 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001604
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001605 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001606 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001607 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001608 }
1609
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001610 spin_unlock(&old_base->lock);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001611 spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001612
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001613 /* Check, if we got expired work to do */
1614 __hrtimer_peek_ahead_timers();
1615 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001616}
1617
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001618#endif /* CONFIG_HOTPLUG_CPU */
1619
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001620static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001621 unsigned long action, void *hcpu)
1622{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001623 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001624
1625 switch (action) {
1626
1627 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001628 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001629 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001630 break;
1631
1632#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001633 case CPU_DYING:
1634 case CPU_DYING_FROZEN:
1635 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1636 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001637 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001638 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001639 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001640 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001641 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001642 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001643 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001644#endif
1645
1646 default:
1647 break;
1648 }
1649
1650 return NOTIFY_OK;
1651}
1652
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001653static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001654 .notifier_call = hrtimer_cpu_notify,
1655};
1656
1657void __init hrtimers_init(void)
1658{
1659 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1660 (void *)(long)smp_processor_id());
1661 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001662#ifdef CONFIG_HIGH_RES_TIMERS
1663 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1664#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001665}
1666
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001667/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001668 * schedule_hrtimeout_range - sleep until timeout
1669 * @expires: timeout value (ktime_t)
1670 * @delta: slack in expires timeout (ktime_t)
1671 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1672 *
1673 * Make the current task sleep until the given expiry time has
1674 * elapsed. The routine will return immediately unless
1675 * the current task state has been set (see set_current_state()).
1676 *
1677 * The @delta argument gives the kernel the freedom to schedule the
1678 * actual wakeup to a time that is both power and performance friendly.
1679 * The kernel give the normal best effort behavior for "@expires+@delta",
1680 * but may decide to fire the timer earlier, but no earlier than @expires.
1681 *
1682 * You can set the task state as follows -
1683 *
1684 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1685 * pass before the routine returns.
1686 *
1687 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1688 * delivered to the current task.
1689 *
1690 * The current task state is guaranteed to be TASK_RUNNING when this
1691 * routine returns.
1692 *
1693 * Returns 0 when the timer has expired otherwise -EINTR
1694 */
1695int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1696 const enum hrtimer_mode mode)
1697{
1698 struct hrtimer_sleeper t;
1699
1700 /*
1701 * Optimize when a zero timeout value is given. It does not
1702 * matter whether this is an absolute or a relative time.
1703 */
1704 if (expires && !expires->tv64) {
1705 __set_current_state(TASK_RUNNING);
1706 return 0;
1707 }
1708
1709 /*
1710 * A NULL parameter means "inifinte"
1711 */
1712 if (!expires) {
1713 schedule();
1714 __set_current_state(TASK_RUNNING);
1715 return -EINTR;
1716 }
1717
1718 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1719 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1720
1721 hrtimer_init_sleeper(&t, current);
1722
1723 hrtimer_start_expires(&t.timer, mode);
1724 if (!hrtimer_active(&t.timer))
1725 t.task = NULL;
1726
1727 if (likely(t.task))
1728 schedule();
1729
1730 hrtimer_cancel(&t.timer);
1731 destroy_hrtimer_on_stack(&t.timer);
1732
1733 __set_current_state(TASK_RUNNING);
1734
1735 return !t.task ? 0 : -EINTR;
1736}
1737EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1738
1739/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001740 * schedule_hrtimeout - sleep until timeout
1741 * @expires: timeout value (ktime_t)
1742 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1743 *
1744 * Make the current task sleep until the given expiry time has
1745 * elapsed. The routine will return immediately unless
1746 * the current task state has been set (see set_current_state()).
1747 *
1748 * You can set the task state as follows -
1749 *
1750 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1751 * pass before the routine returns.
1752 *
1753 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1754 * delivered to the current task.
1755 *
1756 * The current task state is guaranteed to be TASK_RUNNING when this
1757 * routine returns.
1758 *
1759 * Returns 0 when the timer has expired otherwise -EINTR
1760 */
1761int __sched schedule_hrtimeout(ktime_t *expires,
1762 const enum hrtimer_mode mode)
1763{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001764 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001765}
1766EXPORT_SYMBOL_GPL(schedule_hrtimeout);