blob: b68e98f4e4c12bb86b8a30ed0a37866b810defed [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 Gleixner54cdfdb2007-02-16 01:28:11 -0800504 if (expires.tv64 < cpu_base->expires_next.tv64)
505 cpu_base->expires_next = expires;
506 }
507
508 if (cpu_base->expires_next.tv64 != KTIME_MAX)
509 tick_program_event(cpu_base->expires_next, 1);
510}
511
512/*
513 * Shared reprogramming for clock_realtime and clock_monotonic
514 *
515 * When a timer is enqueued and expires earlier than the already enqueued
516 * timers, we have to check, whether it expires earlier than the timer for
517 * which the clock event device was armed.
518 *
519 * Called with interrupts disabled and base->cpu_base.lock held
520 */
521static int hrtimer_reprogram(struct hrtimer *timer,
522 struct hrtimer_clock_base *base)
523{
524 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700525 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800526 int res;
527
Arjan van de Vencc584b22008-09-01 15:02:30 -0700528 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100529
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800530 /*
531 * When the callback is running, we do not reprogram the clock event
532 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200533 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800534 * reprogramming is handled either by the softirq, which called the
535 * callback or at the end of the hrtimer_interrupt.
536 */
537 if (hrtimer_callback_running(timer))
538 return 0;
539
Thomas Gleixner63070a72008-02-14 00:58:36 +0100540 /*
541 * CLOCK_REALTIME timer might be requested with an absolute
542 * expiry time which is less than base->offset. Nothing wrong
543 * about that, just avoid to call into the tick code, which
544 * has now objections against negative expiry values.
545 */
546 if (expires.tv64 < 0)
547 return -ETIME;
548
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800549 if (expires.tv64 >= expires_next->tv64)
550 return 0;
551
552 /*
553 * Clockevents returns -ETIME, when the event was in the past.
554 */
555 res = tick_program_event(expires, 0);
556 if (!IS_ERR_VALUE(res))
557 *expires_next = expires;
558 return res;
559}
560
561
562/*
563 * Retrigger next event is called after clock was set
564 *
565 * Called with interrupts disabled via on_each_cpu()
566 */
567static void retrigger_next_event(void *arg)
568{
569 struct hrtimer_cpu_base *base;
570 struct timespec realtime_offset;
571 unsigned long seq;
572
573 if (!hrtimer_hres_active())
574 return;
575
576 do {
577 seq = read_seqbegin(&xtime_lock);
578 set_normalized_timespec(&realtime_offset,
579 -wall_to_monotonic.tv_sec,
580 -wall_to_monotonic.tv_nsec);
581 } while (read_seqretry(&xtime_lock, seq));
582
583 base = &__get_cpu_var(hrtimer_bases);
584
585 /* Adjust CLOCK_REALTIME offset */
586 spin_lock(&base->lock);
587 base->clock_base[CLOCK_REALTIME].offset =
588 timespec_to_ktime(realtime_offset);
589
590 hrtimer_force_reprogram(base);
591 spin_unlock(&base->lock);
592}
593
594/*
595 * Clock realtime was set
596 *
597 * Change the offset of the realtime clock vs. the monotonic
598 * clock.
599 *
600 * We might have to reprogram the high resolution timer interrupt. On
601 * SMP we call the architecture specific code to retrigger _all_ high
602 * resolution timer interrupts. On UP we just disable interrupts and
603 * call the high resolution interrupt code.
604 */
605void clock_was_set(void)
606{
607 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200608 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800609}
610
611/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200612 * During resume we might have to reprogram the high resolution timer
613 * interrupt (on the local CPU):
614 */
615void hres_timers_resume(void)
616{
Ingo Molnar995f0542007-04-07 12:05:00 +0200617 /* Retrigger the CPU local events: */
618 retrigger_next_event(NULL);
619}
620
621/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800622 * Initialize the high resolution related parts of cpu_base
623 */
624static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
625{
626 base->expires_next.tv64 = KTIME_MAX;
627 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800628}
629
630/*
631 * Initialize the high resolution related parts of a hrtimer
632 */
633static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
634{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800635}
636
Peter Zijlstraca109492008-11-25 12:43:51 +0100637
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800638/*
639 * When High resolution timers are active, try to reprogram. Note, that in case
640 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
641 * check happens. The timer gets enqueued into the rbtree. The reprogramming
642 * and expiry check is done in the hrtimer_interrupt or in the softirq.
643 */
644static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
645 struct hrtimer_clock_base *base)
646{
647 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100648 spin_unlock(&base->cpu_base->lock);
649 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
650 spin_lock(&base->cpu_base->lock);
Peter Zijlstraca109492008-11-25 12:43:51 +0100651 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800652 }
653 return 0;
654}
655
656/*
657 * Switch to high resolution mode
658 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800659static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800660{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700661 int cpu = smp_processor_id();
662 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800663 unsigned long flags;
664
665 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800666 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800667
668 local_irq_save(flags);
669
670 if (tick_init_highres()) {
671 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700672 printk(KERN_WARNING "Could not switch to high resolution "
673 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800674 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800675 }
676 base->hres_active = 1;
677 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
678 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
679
680 tick_setup_sched_timer();
681
682 /* "Retrigger" the interrupt to get things going */
683 retrigger_next_event(NULL);
684 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100685 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800686 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800687 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800688}
689
690#else
691
692static inline int hrtimer_hres_active(void) { return 0; }
693static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800694static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800695static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
696static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
697 struct hrtimer_clock_base *base)
698{
699 return 0;
700}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800701static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
702static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
703
704#endif /* CONFIG_HIGH_RES_TIMERS */
705
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800706#ifdef CONFIG_TIMER_STATS
707void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
708{
709 if (timer->start_site)
710 return;
711
712 timer->start_site = addr;
713 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
714 timer->start_pid = current->pid;
715}
716#endif
717
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800718/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200719 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800720 */
721static inline
722void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
723{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800724 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800725}
726
727/**
728 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800729 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800730 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800731 * @interval: the interval to forward
732 *
733 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700734 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800735 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800736u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800737{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800738 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800739 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800740
Arjan van de Vencc584b22008-09-01 15:02:30 -0700741 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800742
743 if (delta.tv64 < 0)
744 return 0;
745
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100746 if (interval.tv64 < timer->base->resolution.tv64)
747 interval.tv64 = timer->base->resolution.tv64;
748
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800749 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800750 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800751
752 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700753 hrtimer_add_expires_ns(timer, incr * orun);
754 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800755 return orun;
756 /*
757 * This (and the ktime_add() below) is the
758 * correction for exact:
759 */
760 orun++;
761 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700762 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800763
764 return orun;
765}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700766EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800767
768/*
769 * enqueue_hrtimer - internal function to (re)start a timer
770 *
771 * The timer is inserted in expiry order. Insertion into the
772 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100773 *
774 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800775 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100776static int enqueue_hrtimer(struct hrtimer *timer,
777 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800778{
779 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800780 struct rb_node *parent = NULL;
781 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700782 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800783
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700784 debug_hrtimer_activate(timer);
785
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800786 /*
787 * Find the right place in the rbtree:
788 */
789 while (*link) {
790 parent = *link;
791 entry = rb_entry(parent, struct hrtimer, node);
792 /*
793 * We dont care about collisions. Nodes with
794 * the same expiry time stay together.
795 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700796 if (hrtimer_get_expires_tv64(timer) <
797 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800798 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700799 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800800 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700801 leftmost = 0;
802 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800803 }
804
805 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100806 * Insert the timer to the rbtree and check whether it
807 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800808 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100809 if (leftmost)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800810 base->first = &timer->node;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800811
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812 rb_link_node(&timer->node, parent, link);
813 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800814 /*
815 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
816 * state of a possibly running callback.
817 */
818 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100819
820 return leftmost;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100821}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800822
823/*
824 * __remove_hrtimer - internal function to remove a timer
825 *
826 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800827 *
828 * High resolution timer mode reprograms the clock event device when the
829 * timer is the one which expires next. The caller can disable this by setting
830 * reprogram to zero. This is useful, when the context does a reprogramming
831 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800833static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800834 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800835 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800836{
Peter Zijlstraca109492008-11-25 12:43:51 +0100837 if (timer->state & HRTIMER_STATE_ENQUEUED) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800838 /*
839 * Remove the timer from the rbtree and replace the
840 * first entry pointer if necessary.
841 */
842 if (base->first == &timer->node) {
843 base->first = rb_next(&timer->node);
844 /* Reprogram the clock event device. if enabled */
845 if (reprogram && hrtimer_hres_active())
846 hrtimer_force_reprogram(base->cpu_base);
847 }
848 rb_erase(&timer->node, &base->active);
849 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800850 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800851}
852
853/*
854 * remove hrtimer, called with base lock held
855 */
856static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800857remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800858{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800859 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800860 int reprogram;
861
862 /*
863 * Remove the timer and force reprogramming when high
864 * resolution mode is active and the timer is on the current
865 * CPU. If we remove a timer on another CPU, reprogramming is
866 * skipped. The interrupt event on this CPU is fired and
867 * reprogramming happens in the interrupt handler. This is a
868 * rare case and less expensive than a smp call.
869 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700870 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800871 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800872 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
873 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
874 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800875 return 1;
876 }
877 return 0;
878}
879
880/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200881 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800882 * @timer: the timer to be added
883 * @tim: expiry time
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700884 * @delta_ns: "slack" range for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800885 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
886 *
887 * Returns:
888 * 0 on success
889 * 1 when the timer was active
890 */
891int
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700892hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
893 const enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800894{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800895 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800896 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100897 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800898
899 base = lock_hrtimer_base(timer, &flags);
900
901 /* Remove an active timer from the queue: */
902 ret = remove_hrtimer(timer, base);
903
904 /* Switch the timer base, if necessary: */
905 new_base = switch_hrtimer_base(timer, base);
906
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800907 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100908 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800909 /*
910 * CONFIG_TIME_LOW_RES is a temporary way for architectures
911 * to signal that they simply return xtime in
912 * do_gettimeoffset(). In this case we want to round up by
913 * resolution when starting a relative timer, to avoid short
914 * timeouts. This will go away with the GTOD framework.
915 */
916#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100917 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800918#endif
919 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700920
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700921 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800922
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800923 timer_stats_hrtimer_set_start_info(timer);
924
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100925 leftmost = enqueue_hrtimer(timer, new_base);
926
Ingo Molnar935c6312007-03-28 13:17:18 +0200927 /*
928 * Only allow reprogramming if the new base is on this CPU.
929 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100930 *
931 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200932 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100933 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
934 hrtimer_enqueue_reprogram(timer, new_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800935
936 unlock_hrtimer_base(timer, &flags);
937
938 return ret;
939}
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700940EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
941
942/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200943 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700944 * @timer: the timer to be added
945 * @tim: expiry time
946 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
947 *
948 * Returns:
949 * 0 on success
950 * 1 when the timer was active
951 */
952int
953hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
954{
955 return hrtimer_start_range_ns(timer, tim, 0, mode);
956}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700957EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800958
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700959
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800960/**
961 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800962 * @timer: hrtimer to stop
963 *
964 * Returns:
965 * 0 when the timer was not active
966 * 1 when the timer was active
967 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700968 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800969 */
970int hrtimer_try_to_cancel(struct hrtimer *timer)
971{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800972 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800973 unsigned long flags;
974 int ret = -1;
975
976 base = lock_hrtimer_base(timer, &flags);
977
Thomas Gleixner303e9672007-02-16 01:27:51 -0800978 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800979 ret = remove_hrtimer(timer, base);
980
981 unlock_hrtimer_base(timer, &flags);
982
983 return ret;
984
985}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700986EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800987
988/**
989 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800990 * @timer: the timer to be cancelled
991 *
992 * Returns:
993 * 0 when the timer was not active
994 * 1 when the timer was active
995 */
996int hrtimer_cancel(struct hrtimer *timer)
997{
998 for (;;) {
999 int ret = hrtimer_try_to_cancel(timer);
1000
1001 if (ret >= 0)
1002 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001003 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001004 }
1005}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001006EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001007
1008/**
1009 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001010 * @timer: the timer to read
1011 */
1012ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1013{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001014 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001015 unsigned long flags;
1016 ktime_t rem;
1017
1018 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001019 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001020 unlock_hrtimer_base(timer, &flags);
1021
1022 return rem;
1023}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001024EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001025
Russell Kingee9c5782008-04-20 13:59:33 +01001026#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001027/**
1028 * hrtimer_get_next_event - get the time until next expiry event
1029 *
1030 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1031 * is pending.
1032 */
1033ktime_t hrtimer_get_next_event(void)
1034{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001035 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1036 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001037 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1038 unsigned long flags;
1039 int i;
1040
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001041 spin_lock_irqsave(&cpu_base->lock, flags);
1042
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001043 if (!hrtimer_hres_active()) {
1044 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1045 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001046
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001047 if (!base->first)
1048 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001049
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001050 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001051 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001052 delta = ktime_sub(delta, base->get_time());
1053 if (delta.tv64 < mindelta.tv64)
1054 mindelta.tv64 = delta.tv64;
1055 }
Tony Lindgren69239742006-03-06 15:42:45 -08001056 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001057
1058 spin_unlock_irqrestore(&cpu_base->lock, flags);
1059
Tony Lindgren69239742006-03-06 15:42:45 -08001060 if (mindelta.tv64 < 0)
1061 mindelta.tv64 = 0;
1062 return mindelta;
1063}
1064#endif
1065
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001066static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1067 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001068{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001069 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001070
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001071 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001072
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001073 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001074
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001075 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001076 clock_id = CLOCK_MONOTONIC;
1077
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001078 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001079 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001080 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001081
1082#ifdef CONFIG_TIMER_STATS
1083 timer->start_site = NULL;
1084 timer->start_pid = -1;
1085 memset(timer->start_comm, 0, TASK_COMM_LEN);
1086#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001087}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001088
1089/**
1090 * hrtimer_init - initialize a timer to the given clock
1091 * @timer: the timer to be initialized
1092 * @clock_id: the clock to be used
1093 * @mode: timer mode abs/rel
1094 */
1095void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1096 enum hrtimer_mode mode)
1097{
1098 debug_hrtimer_init(timer);
1099 __hrtimer_init(timer, clock_id, mode);
1100}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001101EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001102
1103/**
1104 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001105 * @which_clock: which clock to query
1106 * @tp: pointer to timespec variable to store the resolution
1107 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001108 * Store the resolution of the clock selected by @which_clock in the
1109 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001110 */
1111int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1112{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001113 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001114
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001115 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1116 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001117
1118 return 0;
1119}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001120EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001121
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001122static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001123{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001124 struct hrtimer_clock_base *base = timer->base;
1125 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1126 enum hrtimer_restart (*fn)(struct hrtimer *);
1127 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001128
Peter Zijlstraca109492008-11-25 12:43:51 +01001129 WARN_ON(!irqs_disabled());
1130
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001131 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001132 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1133 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001134 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001135
1136 /*
1137 * Because we run timers from hardirq context, there is no chance
1138 * they get migrated to another cpu, therefore its safe to unlock
1139 * the timer base.
1140 */
1141 spin_unlock(&cpu_base->lock);
1142 restart = fn(timer);
1143 spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001144
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001145 /*
1146 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1147 * reprogramming of the event hardware. This happens at the end of this
1148 * function anyway.
1149 */
1150 if (restart != HRTIMER_NORESTART) {
1151 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001152 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001153 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001154 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001155}
1156
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001157#ifdef CONFIG_HIGH_RES_TIMERS
1158
1159/*
1160 * High resolution timer interrupt
1161 * Called with interrupts disabled
1162 */
1163void hrtimer_interrupt(struct clock_event_device *dev)
1164{
1165 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1166 struct hrtimer_clock_base *base;
1167 ktime_t expires_next, now;
Peter Zijlstraca109492008-11-25 12:43:51 +01001168 int i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001169
1170 BUG_ON(!cpu_base->hres_active);
1171 cpu_base->nr_events++;
1172 dev->next_event.tv64 = KTIME_MAX;
1173
1174 retry:
1175 now = ktime_get();
1176
1177 expires_next.tv64 = KTIME_MAX;
1178
1179 base = cpu_base->clock_base;
1180
1181 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1182 ktime_t basenow;
1183 struct rb_node *node;
1184
1185 spin_lock(&cpu_base->lock);
1186
1187 basenow = ktime_add(now, base->offset);
1188
1189 while ((node = base->first)) {
1190 struct hrtimer *timer;
1191
1192 timer = rb_entry(node, struct hrtimer, node);
1193
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001194 /*
1195 * The immediate goal for using the softexpires is
1196 * minimizing wakeups, not running timers at the
1197 * earliest interrupt after their soft expiration.
1198 * This allows us to avoid using a Priority Search
1199 * Tree, which can answer a stabbing querry for
1200 * overlapping intervals and instead use the simple
1201 * BST we already have.
1202 * We don't add extra wakeups by delaying timers that
1203 * are right-of a not yet expired timer, because that
1204 * timer will have to trigger a wakeup anyway.
1205 */
1206
1207 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001208 ktime_t expires;
1209
Arjan van de Vencc584b22008-09-01 15:02:30 -07001210 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001211 base->offset);
1212 if (expires.tv64 < expires_next.tv64)
1213 expires_next = expires;
1214 break;
1215 }
1216
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001217 __run_hrtimer(timer);
1218 }
1219 spin_unlock(&cpu_base->lock);
1220 base++;
1221 }
1222
1223 cpu_base->expires_next = expires_next;
1224
1225 /* Reprogramming necessary ? */
1226 if (expires_next.tv64 != KTIME_MAX) {
1227 if (tick_program_event(expires_next, 0))
1228 goto retry;
1229 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001230}
1231
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001232/*
1233 * local version of hrtimer_peek_ahead_timers() called with interrupts
1234 * disabled.
1235 */
1236static void __hrtimer_peek_ahead_timers(void)
1237{
1238 struct tick_device *td;
1239
1240 if (!hrtimer_hres_active())
1241 return;
1242
1243 td = &__get_cpu_var(tick_cpu_device);
1244 if (td && td->evtdev)
1245 hrtimer_interrupt(td->evtdev);
1246}
1247
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001248/**
1249 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1250 *
1251 * hrtimer_peek_ahead_timers will peek at the timer queue of
1252 * the current cpu and check if there are any timers for which
1253 * the soft expires time has passed. If any such timers exist,
1254 * they are run immediately and then removed from the timer queue.
1255 *
1256 */
1257void hrtimer_peek_ahead_timers(void)
1258{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001259 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001260
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001261 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001262 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001263 local_irq_restore(flags);
1264}
1265
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001266static void run_hrtimer_softirq(struct softirq_action *h)
1267{
1268 hrtimer_peek_ahead_timers();
1269}
1270
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001271#endif /* CONFIG_HIGH_RES_TIMERS */
1272
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001273/*
1274 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001275 *
1276 * For HRT its the fall back code to run the softirq in the timer
1277 * softirq context in case the hrtimer initialization failed or has
1278 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001279 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001280void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001281{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001282 if (hrtimer_hres_active())
1283 return;
1284
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001285 /*
1286 * This _is_ ugly: We have to check in the softirq context,
1287 * whether we can switch to highres and / or nohz mode. The
1288 * clocksource switch happens in the timer interrupt with
1289 * xtime_lock held. Notification from there only sets the
1290 * check bit in the tick_oneshot code, otherwise we might
1291 * deadlock vs. xtime_lock.
1292 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001293 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001294 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001295}
1296
1297/*
1298 * Called from hardirq context every jiffy
1299 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001300void hrtimer_run_queues(void)
1301{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001302 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001303 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001304 struct hrtimer_clock_base *base;
1305 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001306
1307 if (hrtimer_hres_active())
1308 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001309
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001310 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1311 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001312
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001313 if (!base->first)
1314 continue;
1315
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001316 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001317 hrtimer_get_softirq_time(cpu_base);
1318 gettime = 0;
1319 }
1320
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001321 spin_lock(&cpu_base->lock);
1322
1323 while ((node = base->first)) {
1324 struct hrtimer *timer;
1325
1326 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001327 if (base->softirq_time.tv64 <=
1328 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001329 break;
1330
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001331 __run_hrtimer(timer);
1332 }
1333 spin_unlock(&cpu_base->lock);
1334 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001335}
1336
1337/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001338 * Sleep related functions:
1339 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001340static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001341{
1342 struct hrtimer_sleeper *t =
1343 container_of(timer, struct hrtimer_sleeper, timer);
1344 struct task_struct *task = t->task;
1345
1346 t->task = NULL;
1347 if (task)
1348 wake_up_process(task);
1349
1350 return HRTIMER_NORESTART;
1351}
1352
Ingo Molnar36c8b582006-07-03 00:25:41 -07001353void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001354{
1355 sl->timer.function = hrtimer_wakeup;
1356 sl->task = task;
1357}
1358
Thomas Gleixner669d7862006-03-31 02:31:19 -08001359static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001360{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001361 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001362
Roman Zippel432569b2006-03-26 01:38:08 -08001363 do {
1364 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001365 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001366 if (!hrtimer_active(&t->timer))
1367 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001368
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001369 if (likely(t->task))
1370 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001371
Thomas Gleixner669d7862006-03-31 02:31:19 -08001372 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001373 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001374
Thomas Gleixner669d7862006-03-31 02:31:19 -08001375 } while (t->task && !signal_pending(current));
1376
Peter Zijlstra3588a082008-02-01 17:45:13 +01001377 __set_current_state(TASK_RUNNING);
1378
Thomas Gleixner669d7862006-03-31 02:31:19 -08001379 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001380}
1381
Oleg Nesterov080344b2008-02-01 17:29:05 +03001382static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1383{
1384 struct timespec rmt;
1385 ktime_t rem;
1386
Arjan van de Vencc584b22008-09-01 15:02:30 -07001387 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001388 if (rem.tv64 <= 0)
1389 return 0;
1390 rmt = ktime_to_timespec(rem);
1391
1392 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1393 return -EFAULT;
1394
1395 return 1;
1396}
1397
Toyo Abe1711ef32006-09-29 02:00:28 -07001398long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001399{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001400 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001401 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001402 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001403
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001404 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1405 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001406 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001407
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001408 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001409 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001410
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001411 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001412 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001413 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001414 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001415 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001416 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001417
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001418 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001419 ret = -ERESTART_RESTARTBLOCK;
1420out:
1421 destroy_hrtimer_on_stack(&t.timer);
1422 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001423}
1424
Oleg Nesterov080344b2008-02-01 17:29:05 +03001425long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001426 const enum hrtimer_mode mode, const clockid_t clockid)
1427{
1428 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001429 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001430 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001431 unsigned long slack;
1432
1433 slack = current->timer_slack_ns;
1434 if (rt_task(current))
1435 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001436
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001437 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001438 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001439 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001440 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001441
George Anzinger7978672c2006-02-01 03:05:11 -08001442 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001443 if (mode == HRTIMER_MODE_ABS) {
1444 ret = -ERESTARTNOHAND;
1445 goto out;
1446 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001447
Roman Zippel432569b2006-03-26 01:38:08 -08001448 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001449 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001450 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001451 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001452 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001453
1454 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001455 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001456 restart->nanosleep.index = t.timer.base->index;
1457 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001458 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001459
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001460 ret = -ERESTART_RESTARTBLOCK;
1461out:
1462 destroy_hrtimer_on_stack(&t.timer);
1463 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001464}
1465
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001466asmlinkage long
1467sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1468{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001469 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001470
1471 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1472 return -EFAULT;
1473
1474 if (!timespec_valid(&tu))
1475 return -EINVAL;
1476
Oleg Nesterov080344b2008-02-01 17:29:05 +03001477 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001478}
1479
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001480/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001481 * Functions related to boot-time initialization:
1482 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001483static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001484{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001485 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001486 int i;
1487
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001488 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001489
1490 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1491 cpu_base->clock_base[i].cpu_base = cpu_base;
1492
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001493 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001494}
1495
1496#ifdef CONFIG_HOTPLUG_CPU
1497
Peter Zijlstraca109492008-11-25 12:43:51 +01001498static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001499 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001500{
1501 struct hrtimer *timer;
1502 struct rb_node *node;
1503
1504 while ((node = rb_first(&old_base->active))) {
1505 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001506 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001507 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001508
1509 /*
1510 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1511 * timer could be seen as !active and just vanish away
1512 * under us on another CPU
1513 */
1514 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001515 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001516 /*
Peter Zijlstra37810652008-12-04 11:17:10 +01001517 * Enqueue the timers on the new cpu, but do not reprogram
1518 * the timer as that would enable a deadlock between
1519 * hrtimer_enqueue_reprogramm() running the timer and us still
1520 * holding a nested base lock.
1521 *
1522 * Instead we tickle the hrtimer interrupt after the migration
1523 * is done, which will run all expired timers and re-programm
1524 * the timer device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001525 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001526 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001527
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001528 /* Clear the migration state bit */
1529 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001530 }
1531}
1532
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001533static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001534{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001535 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001536 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001537
Peter Zijlstra37810652008-12-04 11:17:10 +01001538 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001539 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001540
1541 local_irq_disable();
1542 old_base = &per_cpu(hrtimer_bases, scpu);
1543 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001544 /*
1545 * The caller is globally serialized and nobody else
1546 * takes two locks at once, deadlock is not possible.
1547 */
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001548 spin_lock(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001549 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001550
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001551 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001552 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001553 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001554 }
1555
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001556 spin_unlock(&old_base->lock);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001557 spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001558
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001559 /* Check, if we got expired work to do */
1560 __hrtimer_peek_ahead_timers();
1561 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001562}
1563
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001564#endif /* CONFIG_HOTPLUG_CPU */
1565
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001566static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001567 unsigned long action, void *hcpu)
1568{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001569 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001570
1571 switch (action) {
1572
1573 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001574 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001575 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001576 break;
1577
1578#ifdef CONFIG_HOTPLUG_CPU
1579 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001580 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001581 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001582 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001583 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001584 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001585 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001586#endif
1587
1588 default:
1589 break;
1590 }
1591
1592 return NOTIFY_OK;
1593}
1594
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001595static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001596 .notifier_call = hrtimer_cpu_notify,
1597};
1598
1599void __init hrtimers_init(void)
1600{
1601 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1602 (void *)(long)smp_processor_id());
1603 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001604#ifdef CONFIG_HIGH_RES_TIMERS
1605 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1606#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001607}
1608
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001609/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001610 * schedule_hrtimeout_range - sleep until timeout
1611 * @expires: timeout value (ktime_t)
1612 * @delta: slack in expires timeout (ktime_t)
1613 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1614 *
1615 * Make the current task sleep until the given expiry time has
1616 * elapsed. The routine will return immediately unless
1617 * the current task state has been set (see set_current_state()).
1618 *
1619 * The @delta argument gives the kernel the freedom to schedule the
1620 * actual wakeup to a time that is both power and performance friendly.
1621 * The kernel give the normal best effort behavior for "@expires+@delta",
1622 * but may decide to fire the timer earlier, but no earlier than @expires.
1623 *
1624 * You can set the task state as follows -
1625 *
1626 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1627 * pass before the routine returns.
1628 *
1629 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1630 * delivered to the current task.
1631 *
1632 * The current task state is guaranteed to be TASK_RUNNING when this
1633 * routine returns.
1634 *
1635 * Returns 0 when the timer has expired otherwise -EINTR
1636 */
1637int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1638 const enum hrtimer_mode mode)
1639{
1640 struct hrtimer_sleeper t;
1641
1642 /*
1643 * Optimize when a zero timeout value is given. It does not
1644 * matter whether this is an absolute or a relative time.
1645 */
1646 if (expires && !expires->tv64) {
1647 __set_current_state(TASK_RUNNING);
1648 return 0;
1649 }
1650
1651 /*
1652 * A NULL parameter means "inifinte"
1653 */
1654 if (!expires) {
1655 schedule();
1656 __set_current_state(TASK_RUNNING);
1657 return -EINTR;
1658 }
1659
1660 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1661 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1662
1663 hrtimer_init_sleeper(&t, current);
1664
1665 hrtimer_start_expires(&t.timer, mode);
1666 if (!hrtimer_active(&t.timer))
1667 t.task = NULL;
1668
1669 if (likely(t.task))
1670 schedule();
1671
1672 hrtimer_cancel(&t.timer);
1673 destroy_hrtimer_on_stack(&t.timer);
1674
1675 __set_current_state(TASK_RUNNING);
1676
1677 return !t.task ? 0 : -EINTR;
1678}
1679EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1680
1681/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001682 * schedule_hrtimeout - sleep until timeout
1683 * @expires: timeout value (ktime_t)
1684 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1685 *
1686 * Make the current task sleep until the given expiry time has
1687 * elapsed. The routine will return immediately unless
1688 * the current task state has been set (see set_current_state()).
1689 *
1690 * You can set the task state as follows -
1691 *
1692 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1693 * pass before the routine returns.
1694 *
1695 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1696 * delivered to the current task.
1697 *
1698 * The current task state is guaranteed to be TASK_RUNNING when this
1699 * routine returns.
1700 *
1701 * Returns 0 when the timer has expired otherwise -EINTR
1702 */
1703int __sched schedule_hrtimeout(ktime_t *expires,
1704 const enum hrtimer_mode mode)
1705{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001706 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001707}
1708EXPORT_SYMBOL_GPL(schedule_hrtimeout);