blob: cb8a15c1958318c52c116d08e33003ad2ea0cc98 [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
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700335#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
336
337static struct debug_obj_descr hrtimer_debug_descr;
338
339/*
340 * fixup_init is called when:
341 * - an active object is initialized
342 */
343static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
344{
345 struct hrtimer *timer = addr;
346
347 switch (state) {
348 case ODEBUG_STATE_ACTIVE:
349 hrtimer_cancel(timer);
350 debug_object_init(timer, &hrtimer_debug_descr);
351 return 1;
352 default:
353 return 0;
354 }
355}
356
357/*
358 * fixup_activate is called when:
359 * - an active object is activated
360 * - an unknown object is activated (might be a statically initialized object)
361 */
362static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
363{
364 switch (state) {
365
366 case ODEBUG_STATE_NOTAVAILABLE:
367 WARN_ON_ONCE(1);
368 return 0;
369
370 case ODEBUG_STATE_ACTIVE:
371 WARN_ON(1);
372
373 default:
374 return 0;
375 }
376}
377
378/*
379 * fixup_free is called when:
380 * - an active object is freed
381 */
382static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
383{
384 struct hrtimer *timer = addr;
385
386 switch (state) {
387 case ODEBUG_STATE_ACTIVE:
388 hrtimer_cancel(timer);
389 debug_object_free(timer, &hrtimer_debug_descr);
390 return 1;
391 default:
392 return 0;
393 }
394}
395
396static struct debug_obj_descr hrtimer_debug_descr = {
397 .name = "hrtimer",
398 .fixup_init = hrtimer_fixup_init,
399 .fixup_activate = hrtimer_fixup_activate,
400 .fixup_free = hrtimer_fixup_free,
401};
402
403static inline void debug_hrtimer_init(struct hrtimer *timer)
404{
405 debug_object_init(timer, &hrtimer_debug_descr);
406}
407
408static inline void debug_hrtimer_activate(struct hrtimer *timer)
409{
410 debug_object_activate(timer, &hrtimer_debug_descr);
411}
412
413static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
414{
415 debug_object_deactivate(timer, &hrtimer_debug_descr);
416}
417
418static inline void debug_hrtimer_free(struct hrtimer *timer)
419{
420 debug_object_free(timer, &hrtimer_debug_descr);
421}
422
423static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
424 enum hrtimer_mode mode);
425
426void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
427 enum hrtimer_mode mode)
428{
429 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
430 __hrtimer_init(timer, clock_id, mode);
431}
432
433void destroy_hrtimer_on_stack(struct hrtimer *timer)
434{
435 debug_object_free(timer, &hrtimer_debug_descr);
436}
437
438#else
439static inline void debug_hrtimer_init(struct hrtimer *timer) { }
440static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
441static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
442#endif
443
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800444/* High resolution timer related functions */
445#ifdef CONFIG_HIGH_RES_TIMERS
446
447/*
448 * High resolution timer enabled ?
449 */
450static int hrtimer_hres_enabled __read_mostly = 1;
451
452/*
453 * Enable / Disable high resolution mode
454 */
455static int __init setup_hrtimer_hres(char *str)
456{
457 if (!strcmp(str, "off"))
458 hrtimer_hres_enabled = 0;
459 else if (!strcmp(str, "on"))
460 hrtimer_hres_enabled = 1;
461 else
462 return 0;
463 return 1;
464}
465
466__setup("highres=", setup_hrtimer_hres);
467
468/*
469 * hrtimer_high_res_enabled - query, if the highres mode is enabled
470 */
471static inline int hrtimer_is_hres_enabled(void)
472{
473 return hrtimer_hres_enabled;
474}
475
476/*
477 * Is the high resolution mode active ?
478 */
479static inline int hrtimer_hres_active(void)
480{
481 return __get_cpu_var(hrtimer_bases).hres_active;
482}
483
484/*
485 * Reprogram the event source with checking both queues for the
486 * next event
487 * Called with interrupts disabled and base->lock held
488 */
489static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
490{
491 int i;
492 struct hrtimer_clock_base *base = cpu_base->clock_base;
493 ktime_t expires;
494
495 cpu_base->expires_next.tv64 = KTIME_MAX;
496
497 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
498 struct hrtimer *timer;
499
500 if (!base->first)
501 continue;
502 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700503 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100504 /*
505 * clock_was_set() has changed base->offset so the
506 * result might be negative. Fix it up to prevent a
507 * false positive in clockevents_program_event()
508 */
509 if (expires.tv64 < 0)
510 expires.tv64 = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800511 if (expires.tv64 < cpu_base->expires_next.tv64)
512 cpu_base->expires_next = expires;
513 }
514
515 if (cpu_base->expires_next.tv64 != KTIME_MAX)
516 tick_program_event(cpu_base->expires_next, 1);
517}
518
519/*
520 * Shared reprogramming for clock_realtime and clock_monotonic
521 *
522 * When a timer is enqueued and expires earlier than the already enqueued
523 * timers, we have to check, whether it expires earlier than the timer for
524 * which the clock event device was armed.
525 *
526 * Called with interrupts disabled and base->cpu_base.lock held
527 */
528static int hrtimer_reprogram(struct hrtimer *timer,
529 struct hrtimer_clock_base *base)
530{
531 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700532 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800533 int res;
534
Arjan van de Vencc584b22008-09-01 15:02:30 -0700535 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100536
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800537 /*
538 * When the callback is running, we do not reprogram the clock event
539 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200540 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800541 * reprogramming is handled either by the softirq, which called the
542 * callback or at the end of the hrtimer_interrupt.
543 */
544 if (hrtimer_callback_running(timer))
545 return 0;
546
Thomas Gleixner63070a72008-02-14 00:58:36 +0100547 /*
548 * CLOCK_REALTIME timer might be requested with an absolute
549 * expiry time which is less than base->offset. Nothing wrong
550 * about that, just avoid to call into the tick code, which
551 * has now objections against negative expiry values.
552 */
553 if (expires.tv64 < 0)
554 return -ETIME;
555
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800556 if (expires.tv64 >= expires_next->tv64)
557 return 0;
558
559 /*
560 * Clockevents returns -ETIME, when the event was in the past.
561 */
562 res = tick_program_event(expires, 0);
563 if (!IS_ERR_VALUE(res))
564 *expires_next = expires;
565 return res;
566}
567
568
569/*
570 * Retrigger next event is called after clock was set
571 *
572 * Called with interrupts disabled via on_each_cpu()
573 */
574static void retrigger_next_event(void *arg)
575{
576 struct hrtimer_cpu_base *base;
577 struct timespec realtime_offset;
578 unsigned long seq;
579
580 if (!hrtimer_hres_active())
581 return;
582
583 do {
584 seq = read_seqbegin(&xtime_lock);
585 set_normalized_timespec(&realtime_offset,
586 -wall_to_monotonic.tv_sec,
587 -wall_to_monotonic.tv_nsec);
588 } while (read_seqretry(&xtime_lock, seq));
589
590 base = &__get_cpu_var(hrtimer_bases);
591
592 /* Adjust CLOCK_REALTIME offset */
593 spin_lock(&base->lock);
594 base->clock_base[CLOCK_REALTIME].offset =
595 timespec_to_ktime(realtime_offset);
596
597 hrtimer_force_reprogram(base);
598 spin_unlock(&base->lock);
599}
600
601/*
602 * Clock realtime was set
603 *
604 * Change the offset of the realtime clock vs. the monotonic
605 * clock.
606 *
607 * We might have to reprogram the high resolution timer interrupt. On
608 * SMP we call the architecture specific code to retrigger _all_ high
609 * resolution timer interrupts. On UP we just disable interrupts and
610 * call the high resolution interrupt code.
611 */
612void clock_was_set(void)
613{
614 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200615 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800616}
617
618/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200619 * During resume we might have to reprogram the high resolution timer
620 * interrupt (on the local CPU):
621 */
622void hres_timers_resume(void)
623{
Peter Zijlstra1d4a7f12009-01-18 16:39:29 +0100624 WARN_ONCE(!irqs_disabled(),
625 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
626
Ingo Molnar995f0542007-04-07 12:05:00 +0200627 retrigger_next_event(NULL);
628}
629
630/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800631 * Initialize the high resolution related parts of cpu_base
632 */
633static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
634{
635 base->expires_next.tv64 = KTIME_MAX;
636 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800637}
638
639/*
640 * Initialize the high resolution related parts of a hrtimer
641 */
642static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
643{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800644}
645
Peter Zijlstraca109492008-11-25 12:43:51 +0100646
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800647/*
648 * When High resolution timers are active, try to reprogram. Note, that in case
649 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
650 * check happens. The timer gets enqueued into the rbtree. The reprogramming
651 * and expiry check is done in the hrtimer_interrupt or in the softirq.
652 */
653static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100654 struct hrtimer_clock_base *base,
655 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800656{
657 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100658 if (wakeup) {
659 spin_unlock(&base->cpu_base->lock);
660 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
661 spin_lock(&base->cpu_base->lock);
662 } else
663 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
664
Peter Zijlstraca109492008-11-25 12:43:51 +0100665 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800666 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100667
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800668 return 0;
669}
670
671/*
672 * Switch to high resolution mode
673 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800674static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800675{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700676 int cpu = smp_processor_id();
677 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800678 unsigned long flags;
679
680 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800681 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800682
683 local_irq_save(flags);
684
685 if (tick_init_highres()) {
686 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700687 printk(KERN_WARNING "Could not switch to high resolution "
688 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800689 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800690 }
691 base->hres_active = 1;
692 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
693 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
694
695 tick_setup_sched_timer();
696
697 /* "Retrigger" the interrupt to get things going */
698 retrigger_next_event(NULL);
699 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100700 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800701 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800702 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800703}
704
705#else
706
707static inline int hrtimer_hres_active(void) { return 0; }
708static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800709static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800710static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
711static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100712 struct hrtimer_clock_base *base,
713 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800714{
715 return 0;
716}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800717static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
718static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
719
720#endif /* CONFIG_HIGH_RES_TIMERS */
721
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800722#ifdef CONFIG_TIMER_STATS
723void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
724{
725 if (timer->start_site)
726 return;
727
728 timer->start_site = addr;
729 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
730 timer->start_pid = current->pid;
731}
732#endif
733
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800734/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200735 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800736 */
737static inline
738void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
739{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800740 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800741}
742
743/**
744 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800745 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800746 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800747 * @interval: the interval to forward
748 *
749 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700750 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800751 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800752u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800753{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800754 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800755 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800756
Arjan van de Vencc584b22008-09-01 15:02:30 -0700757 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800758
759 if (delta.tv64 < 0)
760 return 0;
761
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100762 if (interval.tv64 < timer->base->resolution.tv64)
763 interval.tv64 = timer->base->resolution.tv64;
764
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800765 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800766 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800767
768 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700769 hrtimer_add_expires_ns(timer, incr * orun);
770 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800771 return orun;
772 /*
773 * This (and the ktime_add() below) is the
774 * correction for exact:
775 */
776 orun++;
777 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700778 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800779
780 return orun;
781}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700782EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800783
784/*
785 * enqueue_hrtimer - internal function to (re)start a timer
786 *
787 * The timer is inserted in expiry order. Insertion into the
788 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100789 *
790 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800791 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100792static int enqueue_hrtimer(struct hrtimer *timer,
793 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800794{
795 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800796 struct rb_node *parent = NULL;
797 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700798 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800799
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700800 debug_hrtimer_activate(timer);
801
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800802 /*
803 * Find the right place in the rbtree:
804 */
805 while (*link) {
806 parent = *link;
807 entry = rb_entry(parent, struct hrtimer, node);
808 /*
809 * We dont care about collisions. Nodes with
810 * the same expiry time stay together.
811 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700812 if (hrtimer_get_expires_tv64(timer) <
813 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800814 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700815 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800816 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700817 leftmost = 0;
818 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800819 }
820
821 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100822 * Insert the timer to the rbtree and check whether it
823 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800824 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100825 if (leftmost)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800826 base->first = &timer->node;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800827
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800828 rb_link_node(&timer->node, parent, link);
829 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800830 /*
831 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
832 * state of a possibly running callback.
833 */
834 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100835
836 return leftmost;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100837}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800838
839/*
840 * __remove_hrtimer - internal function to remove a timer
841 *
842 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800843 *
844 * High resolution timer mode reprograms the clock event device when the
845 * timer is the one which expires next. The caller can disable this by setting
846 * reprogram to zero. This is useful, when the context does a reprogramming
847 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800848 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800849static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800850 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800851 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852{
Peter Zijlstraca109492008-11-25 12:43:51 +0100853 if (timer->state & HRTIMER_STATE_ENQUEUED) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800854 /*
855 * Remove the timer from the rbtree and replace the
856 * first entry pointer if necessary.
857 */
858 if (base->first == &timer->node) {
859 base->first = rb_next(&timer->node);
860 /* Reprogram the clock event device. if enabled */
861 if (reprogram && hrtimer_hres_active())
862 hrtimer_force_reprogram(base->cpu_base);
863 }
864 rb_erase(&timer->node, &base->active);
865 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800866 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800867}
868
869/*
870 * remove hrtimer, called with base lock held
871 */
872static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800873remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800874{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800875 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800876 int reprogram;
877
878 /*
879 * Remove the timer and force reprogramming when high
880 * resolution mode is active and the timer is on the current
881 * CPU. If we remove a timer on another CPU, reprogramming is
882 * skipped. The interrupt event on this CPU is fired and
883 * reprogramming happens in the interrupt handler. This is a
884 * rare case and less expensive than a smp call.
885 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700886 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800887 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800888 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
889 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
890 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800891 return 1;
892 }
893 return 0;
894}
895
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100896int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
897 unsigned long delta_ns, const enum hrtimer_mode mode,
898 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800899{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800900 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800901 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100902 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800903
904 base = lock_hrtimer_base(timer, &flags);
905
906 /* Remove an active timer from the queue: */
907 ret = remove_hrtimer(timer, base);
908
909 /* Switch the timer base, if necessary: */
910 new_base = switch_hrtimer_base(timer, base);
911
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800912 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100913 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800914 /*
915 * CONFIG_TIME_LOW_RES is a temporary way for architectures
916 * to signal that they simply return xtime in
917 * do_gettimeoffset(). In this case we want to round up by
918 * resolution when starting a relative timer, to avoid short
919 * timeouts. This will go away with the GTOD framework.
920 */
921#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100922 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800923#endif
924 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700925
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700926 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800927
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800928 timer_stats_hrtimer_set_start_info(timer);
929
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100930 leftmost = enqueue_hrtimer(timer, new_base);
931
Ingo Molnar935c6312007-03-28 13:17:18 +0200932 /*
933 * Only allow reprogramming if the new base is on this CPU.
934 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100935 *
936 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200937 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100938 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100939 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800940
941 unlock_hrtimer_base(timer, &flags);
942
943 return ret;
944}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100945
946/**
947 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
948 * @timer: the timer to be added
949 * @tim: expiry time
950 * @delta_ns: "slack" range for the timer
951 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
952 *
953 * Returns:
954 * 0 on success
955 * 1 when the timer was active
956 */
957int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
958 unsigned long delta_ns, const enum hrtimer_mode mode)
959{
960 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
961}
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700962EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
963
964/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200965 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700966 * @timer: the timer to be added
967 * @tim: expiry time
968 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
969 *
970 * Returns:
971 * 0 on success
972 * 1 when the timer was active
973 */
974int
975hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
976{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100977 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700978}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700979EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800980
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700981
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800982/**
983 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800984 * @timer: hrtimer to stop
985 *
986 * Returns:
987 * 0 when the timer was not active
988 * 1 when the timer was active
989 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700990 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800991 */
992int hrtimer_try_to_cancel(struct hrtimer *timer)
993{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800994 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800995 unsigned long flags;
996 int ret = -1;
997
998 base = lock_hrtimer_base(timer, &flags);
999
Thomas Gleixner303e9672007-02-16 01:27:51 -08001000 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001001 ret = remove_hrtimer(timer, base);
1002
1003 unlock_hrtimer_base(timer, &flags);
1004
1005 return ret;
1006
1007}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001008EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001009
1010/**
1011 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001012 * @timer: the timer to be cancelled
1013 *
1014 * Returns:
1015 * 0 when the timer was not active
1016 * 1 when the timer was active
1017 */
1018int hrtimer_cancel(struct hrtimer *timer)
1019{
1020 for (;;) {
1021 int ret = hrtimer_try_to_cancel(timer);
1022
1023 if (ret >= 0)
1024 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001025 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001026 }
1027}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001028EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001029
1030/**
1031 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001032 * @timer: the timer to read
1033 */
1034ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1035{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001036 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001037 unsigned long flags;
1038 ktime_t rem;
1039
1040 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001041 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001042 unlock_hrtimer_base(timer, &flags);
1043
1044 return rem;
1045}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001046EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001047
Russell Kingee9c5782008-04-20 13:59:33 +01001048#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001049/**
1050 * hrtimer_get_next_event - get the time until next expiry event
1051 *
1052 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1053 * is pending.
1054 */
1055ktime_t hrtimer_get_next_event(void)
1056{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001057 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1058 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001059 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1060 unsigned long flags;
1061 int i;
1062
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001063 spin_lock_irqsave(&cpu_base->lock, flags);
1064
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001065 if (!hrtimer_hres_active()) {
1066 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1067 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001068
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001069 if (!base->first)
1070 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001071
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001072 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001073 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001074 delta = ktime_sub(delta, base->get_time());
1075 if (delta.tv64 < mindelta.tv64)
1076 mindelta.tv64 = delta.tv64;
1077 }
Tony Lindgren69239742006-03-06 15:42:45 -08001078 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001079
1080 spin_unlock_irqrestore(&cpu_base->lock, flags);
1081
Tony Lindgren69239742006-03-06 15:42:45 -08001082 if (mindelta.tv64 < 0)
1083 mindelta.tv64 = 0;
1084 return mindelta;
1085}
1086#endif
1087
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001088static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1089 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001090{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001091 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001092
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001093 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001094
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001095 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001096
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001097 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001098 clock_id = CLOCK_MONOTONIC;
1099
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001100 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001101 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001102 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001103
1104#ifdef CONFIG_TIMER_STATS
1105 timer->start_site = NULL;
1106 timer->start_pid = -1;
1107 memset(timer->start_comm, 0, TASK_COMM_LEN);
1108#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001109}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001110
1111/**
1112 * hrtimer_init - initialize a timer to the given clock
1113 * @timer: the timer to be initialized
1114 * @clock_id: the clock to be used
1115 * @mode: timer mode abs/rel
1116 */
1117void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1118 enum hrtimer_mode mode)
1119{
1120 debug_hrtimer_init(timer);
1121 __hrtimer_init(timer, clock_id, mode);
1122}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001123EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001124
1125/**
1126 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001127 * @which_clock: which clock to query
1128 * @tp: pointer to timespec variable to store the resolution
1129 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001130 * Store the resolution of the clock selected by @which_clock in the
1131 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001132 */
1133int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1134{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001135 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001136
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001137 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1138 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001139
1140 return 0;
1141}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001142EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001143
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001144static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001145{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001146 struct hrtimer_clock_base *base = timer->base;
1147 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1148 enum hrtimer_restart (*fn)(struct hrtimer *);
1149 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001150
Peter Zijlstraca109492008-11-25 12:43:51 +01001151 WARN_ON(!irqs_disabled());
1152
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001153 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001154 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1155 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001156 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001157
1158 /*
1159 * Because we run timers from hardirq context, there is no chance
1160 * they get migrated to another cpu, therefore its safe to unlock
1161 * the timer base.
1162 */
1163 spin_unlock(&cpu_base->lock);
1164 restart = fn(timer);
1165 spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001166
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001167 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001168 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1169 * we do not reprogramm the event hardware. Happens either in
1170 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001171 */
1172 if (restart != HRTIMER_NORESTART) {
1173 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001174 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001175 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001176 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001177}
1178
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001179#ifdef CONFIG_HIGH_RES_TIMERS
1180
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001181static int force_clock_reprogram;
1182
1183/*
1184 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1185 * is hanging, which could happen with something that slows the interrupt
1186 * such as the tracing. Then we force the clock reprogramming for each future
1187 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1188 * threshold that we will overwrite.
1189 * The next tick event will be scheduled to 3 times we currently spend on
1190 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1191 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1192 * let it running without serious starvation.
1193 */
1194
1195static inline void
1196hrtimer_interrupt_hanging(struct clock_event_device *dev,
1197 ktime_t try_time)
1198{
1199 force_clock_reprogram = 1;
1200 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1201 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1202 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1203}
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001204/*
1205 * High resolution timer interrupt
1206 * Called with interrupts disabled
1207 */
1208void hrtimer_interrupt(struct clock_event_device *dev)
1209{
1210 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1211 struct hrtimer_clock_base *base;
1212 ktime_t expires_next, now;
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001213 int nr_retries = 0;
Peter Zijlstraca109492008-11-25 12:43:51 +01001214 int i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001215
1216 BUG_ON(!cpu_base->hres_active);
1217 cpu_base->nr_events++;
1218 dev->next_event.tv64 = KTIME_MAX;
1219
1220 retry:
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001221 /* 5 retries is enough to notice a hang */
1222 if (!(++nr_retries % 5))
1223 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1224
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001225 now = ktime_get();
1226
1227 expires_next.tv64 = KTIME_MAX;
1228
1229 base = cpu_base->clock_base;
1230
1231 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1232 ktime_t basenow;
1233 struct rb_node *node;
1234
1235 spin_lock(&cpu_base->lock);
1236
1237 basenow = ktime_add(now, base->offset);
1238
1239 while ((node = base->first)) {
1240 struct hrtimer *timer;
1241
1242 timer = rb_entry(node, struct hrtimer, node);
1243
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001244 /*
1245 * The immediate goal for using the softexpires is
1246 * minimizing wakeups, not running timers at the
1247 * earliest interrupt after their soft expiration.
1248 * This allows us to avoid using a Priority Search
1249 * Tree, which can answer a stabbing querry for
1250 * overlapping intervals and instead use the simple
1251 * BST we already have.
1252 * We don't add extra wakeups by delaying timers that
1253 * are right-of a not yet expired timer, because that
1254 * timer will have to trigger a wakeup anyway.
1255 */
1256
1257 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001258 ktime_t expires;
1259
Arjan van de Vencc584b22008-09-01 15:02:30 -07001260 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001261 base->offset);
1262 if (expires.tv64 < expires_next.tv64)
1263 expires_next = expires;
1264 break;
1265 }
1266
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001267 __run_hrtimer(timer);
1268 }
1269 spin_unlock(&cpu_base->lock);
1270 base++;
1271 }
1272
1273 cpu_base->expires_next = expires_next;
1274
1275 /* Reprogramming necessary ? */
1276 if (expires_next.tv64 != KTIME_MAX) {
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001277 if (tick_program_event(expires_next, force_clock_reprogram))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001278 goto retry;
1279 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001280}
1281
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001282/*
1283 * local version of hrtimer_peek_ahead_timers() called with interrupts
1284 * disabled.
1285 */
1286static void __hrtimer_peek_ahead_timers(void)
1287{
1288 struct tick_device *td;
1289
1290 if (!hrtimer_hres_active())
1291 return;
1292
1293 td = &__get_cpu_var(tick_cpu_device);
1294 if (td && td->evtdev)
1295 hrtimer_interrupt(td->evtdev);
1296}
1297
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001298/**
1299 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1300 *
1301 * hrtimer_peek_ahead_timers will peek at the timer queue of
1302 * the current cpu and check if there are any timers for which
1303 * the soft expires time has passed. If any such timers exist,
1304 * they are run immediately and then removed from the timer queue.
1305 *
1306 */
1307void hrtimer_peek_ahead_timers(void)
1308{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001309 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001310
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001311 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001312 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001313 local_irq_restore(flags);
1314}
1315
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001316static void run_hrtimer_softirq(struct softirq_action *h)
1317{
1318 hrtimer_peek_ahead_timers();
1319}
1320
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001321#else /* CONFIG_HIGH_RES_TIMERS */
1322
1323static inline void __hrtimer_peek_ahead_timers(void) { }
1324
1325#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001326
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001327/*
1328 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001329 *
1330 * For HRT its the fall back code to run the softirq in the timer
1331 * softirq context in case the hrtimer initialization failed or has
1332 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001333 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001334void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001335{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001336 if (hrtimer_hres_active())
1337 return;
1338
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001339 /*
1340 * This _is_ ugly: We have to check in the softirq context,
1341 * whether we can switch to highres and / or nohz mode. The
1342 * clocksource switch happens in the timer interrupt with
1343 * xtime_lock held. Notification from there only sets the
1344 * check bit in the tick_oneshot code, otherwise we might
1345 * deadlock vs. xtime_lock.
1346 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001347 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001348 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001349}
1350
1351/*
1352 * Called from hardirq context every jiffy
1353 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001354void hrtimer_run_queues(void)
1355{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001356 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001357 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001358 struct hrtimer_clock_base *base;
1359 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001360
1361 if (hrtimer_hres_active())
1362 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001363
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001364 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1365 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001366
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001367 if (!base->first)
1368 continue;
1369
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001370 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001371 hrtimer_get_softirq_time(cpu_base);
1372 gettime = 0;
1373 }
1374
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001375 spin_lock(&cpu_base->lock);
1376
1377 while ((node = base->first)) {
1378 struct hrtimer *timer;
1379
1380 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001381 if (base->softirq_time.tv64 <=
1382 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001383 break;
1384
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001385 __run_hrtimer(timer);
1386 }
1387 spin_unlock(&cpu_base->lock);
1388 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001389}
1390
1391/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001392 * Sleep related functions:
1393 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001394static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001395{
1396 struct hrtimer_sleeper *t =
1397 container_of(timer, struct hrtimer_sleeper, timer);
1398 struct task_struct *task = t->task;
1399
1400 t->task = NULL;
1401 if (task)
1402 wake_up_process(task);
1403
1404 return HRTIMER_NORESTART;
1405}
1406
Ingo Molnar36c8b582006-07-03 00:25:41 -07001407void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001408{
1409 sl->timer.function = hrtimer_wakeup;
1410 sl->task = task;
1411}
1412
Thomas Gleixner669d7862006-03-31 02:31:19 -08001413static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001414{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001415 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001416
Roman Zippel432569b2006-03-26 01:38:08 -08001417 do {
1418 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001419 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001420 if (!hrtimer_active(&t->timer))
1421 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001422
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001423 if (likely(t->task))
1424 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001425
Thomas Gleixner669d7862006-03-31 02:31:19 -08001426 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001427 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001428
Thomas Gleixner669d7862006-03-31 02:31:19 -08001429 } while (t->task && !signal_pending(current));
1430
Peter Zijlstra3588a082008-02-01 17:45:13 +01001431 __set_current_state(TASK_RUNNING);
1432
Thomas Gleixner669d7862006-03-31 02:31:19 -08001433 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001434}
1435
Oleg Nesterov080344b2008-02-01 17:29:05 +03001436static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1437{
1438 struct timespec rmt;
1439 ktime_t rem;
1440
Arjan van de Vencc584b22008-09-01 15:02:30 -07001441 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001442 if (rem.tv64 <= 0)
1443 return 0;
1444 rmt = ktime_to_timespec(rem);
1445
1446 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1447 return -EFAULT;
1448
1449 return 1;
1450}
1451
Toyo Abe1711ef32006-09-29 02:00:28 -07001452long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001453{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001454 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001455 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001456 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001457
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001458 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1459 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001460 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001461
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001462 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001463 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001464
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001465 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001466 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001467 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001468 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001469 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001470 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001471
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001472 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001473 ret = -ERESTART_RESTARTBLOCK;
1474out:
1475 destroy_hrtimer_on_stack(&t.timer);
1476 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001477}
1478
Oleg Nesterov080344b2008-02-01 17:29:05 +03001479long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001480 const enum hrtimer_mode mode, const clockid_t clockid)
1481{
1482 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001483 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001484 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001485 unsigned long slack;
1486
1487 slack = current->timer_slack_ns;
1488 if (rt_task(current))
1489 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001490
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001491 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001492 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001493 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001494 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001495
George Anzinger7978672c2006-02-01 03:05:11 -08001496 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001497 if (mode == HRTIMER_MODE_ABS) {
1498 ret = -ERESTARTNOHAND;
1499 goto out;
1500 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001501
Roman Zippel432569b2006-03-26 01:38:08 -08001502 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001503 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001504 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001505 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001506 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001507
1508 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001509 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001510 restart->nanosleep.index = t.timer.base->index;
1511 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001512 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001513
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001514 ret = -ERESTART_RESTARTBLOCK;
1515out:
1516 destroy_hrtimer_on_stack(&t.timer);
1517 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001518}
1519
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001520SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1521 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001522{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001523 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001524
1525 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1526 return -EFAULT;
1527
1528 if (!timespec_valid(&tu))
1529 return -EINVAL;
1530
Oleg Nesterov080344b2008-02-01 17:29:05 +03001531 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001532}
1533
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001534/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001535 * Functions related to boot-time initialization:
1536 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001537static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001538{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001539 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001540 int i;
1541
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001542 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001543
1544 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1545 cpu_base->clock_base[i].cpu_base = cpu_base;
1546
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001547 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001548}
1549
1550#ifdef CONFIG_HOTPLUG_CPU
1551
Peter Zijlstraca109492008-11-25 12:43:51 +01001552static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001553 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001554{
1555 struct hrtimer *timer;
1556 struct rb_node *node;
1557
1558 while ((node = rb_first(&old_base->active))) {
1559 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001560 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001561 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001562
1563 /*
1564 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1565 * timer could be seen as !active and just vanish away
1566 * under us on another CPU
1567 */
1568 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001569 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001570 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001571 * Enqueue the timers on the new cpu. This does not
1572 * reprogram the event device in case the timer
1573 * expires before the earliest on this CPU, but we run
1574 * hrtimer_interrupt after we migrated everything to
1575 * sort out already expired timers and reprogram the
1576 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001577 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001578 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001579
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001580 /* Clear the migration state bit */
1581 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001582 }
1583}
1584
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001585static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001586{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001587 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001588 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001589
Peter Zijlstra37810652008-12-04 11:17:10 +01001590 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001591 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001592
1593 local_irq_disable();
1594 old_base = &per_cpu(hrtimer_bases, scpu);
1595 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001596 /*
1597 * The caller is globally serialized and nobody else
1598 * takes two locks at once, deadlock is not possible.
1599 */
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001600 spin_lock(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001601 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001602
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001603 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001604 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001605 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001606 }
1607
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001608 spin_unlock(&old_base->lock);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001609 spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001610
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001611 /* Check, if we got expired work to do */
1612 __hrtimer_peek_ahead_timers();
1613 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001614}
1615
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001616#endif /* CONFIG_HOTPLUG_CPU */
1617
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001618static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001619 unsigned long action, void *hcpu)
1620{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001621 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001622
1623 switch (action) {
1624
1625 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001626 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001627 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001628 break;
1629
1630#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001631 case CPU_DYING:
1632 case CPU_DYING_FROZEN:
1633 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1634 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001635 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001636 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001637 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001638 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001639 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001640 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001641 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001642#endif
1643
1644 default:
1645 break;
1646 }
1647
1648 return NOTIFY_OK;
1649}
1650
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001651static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001652 .notifier_call = hrtimer_cpu_notify,
1653};
1654
1655void __init hrtimers_init(void)
1656{
1657 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1658 (void *)(long)smp_processor_id());
1659 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001660#ifdef CONFIG_HIGH_RES_TIMERS
1661 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1662#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001663}
1664
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001665/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001666 * schedule_hrtimeout_range - sleep until timeout
1667 * @expires: timeout value (ktime_t)
1668 * @delta: slack in expires timeout (ktime_t)
1669 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1670 *
1671 * Make the current task sleep until the given expiry time has
1672 * elapsed. The routine will return immediately unless
1673 * the current task state has been set (see set_current_state()).
1674 *
1675 * The @delta argument gives the kernel the freedom to schedule the
1676 * actual wakeup to a time that is both power and performance friendly.
1677 * The kernel give the normal best effort behavior for "@expires+@delta",
1678 * but may decide to fire the timer earlier, but no earlier than @expires.
1679 *
1680 * You can set the task state as follows -
1681 *
1682 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1683 * pass before the routine returns.
1684 *
1685 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1686 * delivered to the current task.
1687 *
1688 * The current task state is guaranteed to be TASK_RUNNING when this
1689 * routine returns.
1690 *
1691 * Returns 0 when the timer has expired otherwise -EINTR
1692 */
1693int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1694 const enum hrtimer_mode mode)
1695{
1696 struct hrtimer_sleeper t;
1697
1698 /*
1699 * Optimize when a zero timeout value is given. It does not
1700 * matter whether this is an absolute or a relative time.
1701 */
1702 if (expires && !expires->tv64) {
1703 __set_current_state(TASK_RUNNING);
1704 return 0;
1705 }
1706
1707 /*
1708 * A NULL parameter means "inifinte"
1709 */
1710 if (!expires) {
1711 schedule();
1712 __set_current_state(TASK_RUNNING);
1713 return -EINTR;
1714 }
1715
1716 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1717 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1718
1719 hrtimer_init_sleeper(&t, current);
1720
1721 hrtimer_start_expires(&t.timer, mode);
1722 if (!hrtimer_active(&t.timer))
1723 t.task = NULL;
1724
1725 if (likely(t.task))
1726 schedule();
1727
1728 hrtimer_cancel(&t.timer);
1729 destroy_hrtimer_on_stack(&t.timer);
1730
1731 __set_current_state(TASK_RUNNING);
1732
1733 return !t.task ? 0 : -EINTR;
1734}
1735EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1736
1737/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001738 * schedule_hrtimeout - sleep until timeout
1739 * @expires: timeout value (ktime_t)
1740 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1741 *
1742 * Make the current task sleep until the given expiry time has
1743 * elapsed. The routine will return immediately unless
1744 * the current task state has been set (see set_current_state()).
1745 *
1746 * You can set the task state as follows -
1747 *
1748 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1749 * pass before the routine returns.
1750 *
1751 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1752 * delivered to the current task.
1753 *
1754 * The current task state is guaranteed to be TASK_RUNNING when this
1755 * routine returns.
1756 *
1757 * Returns 0 when the timer has expired otherwise -EINTR
1758 */
1759int __sched schedule_hrtimeout(ktime_t *expires,
1760 const enum hrtimer_mode mode)
1761{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001762 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001763}
1764EXPORT_SYMBOL_GPL(schedule_hrtimeout);