blob: 43d151f185b6420b1f6706808a5f4e24cd82daad [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>
Arun R Bharadwajeea08f32009-04-16 12:16:41 +053046#include <linux/sched.h>
47#include <linux/timer.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080048
49#include <asm/uaccess.h>
50
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080051/*
52 * The timer bases:
George Anzinger79786722006-02-01 03:05:11 -080053 *
54 * Note: If we want to add new timer bases, we have to skip the two
55 * clock ids captured by the cpu-timers. We do this by holding empty
56 * entries rather than doing math adjustment of the clock ids.
57 * This ensures that we capture erroneous accesses to these clock ids
58 * rather than moving them into the range of valid clock id's.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080059 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080060DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080061{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080062
63 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080064 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080065 {
66 .index = CLOCK_REALTIME,
67 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080068 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080069 },
70 {
71 .index = CLOCK_MONOTONIC,
72 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080073 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080074 },
75 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080076};
77
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080078/*
Thomas Gleixner92127c72006-03-26 01:38:05 -080079 * Get the coarse grained time at the softirq based on xtime and
80 * wall_to_monotonic.
81 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080082static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -080083{
84 ktime_t xtim, tomono;
Thomas Gleixnerad28d942007-03-16 13:38:21 -080085 struct timespec xts, tom;
Thomas Gleixner92127c72006-03-26 01:38:05 -080086 unsigned long seq;
87
88 do {
89 seq = read_seqbegin(&xtime_lock);
john stultz2c6b47d2007-07-24 17:47:43 -070090 xts = current_kernel_time();
Thomas Gleixnerad28d942007-03-16 13:38:21 -080091 tom = wall_to_monotonic;
Thomas Gleixner92127c72006-03-26 01:38:05 -080092 } while (read_seqretry(&xtime_lock, seq));
93
john stultzf4304ab2007-02-16 01:27:26 -080094 xtim = timespec_to_ktime(xts);
Thomas Gleixnerad28d942007-03-16 13:38:21 -080095 tomono = timespec_to_ktime(tom);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080096 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
97 base->clock_base[CLOCK_MONOTONIC].softirq_time =
98 ktime_add(xtim, tomono);
Thomas Gleixner92127c72006-03-26 01:38:05 -080099}
100
101/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800102 * Functions and macros which are different for UP/SMP systems are kept in a
103 * single place
104 */
105#ifdef CONFIG_SMP
106
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800107/*
108 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
109 * means that all timers which are tied to this base via timer->base are
110 * locked, and the base itself is locked too.
111 *
112 * So __run_timers/migrate_timers can safely modify all timers which could
113 * be found on the lists/queues.
114 *
115 * When the timer's base is locked, and the timer removed from list, it is
116 * possible to set timer->base = NULL and drop the lock: the timer remains
117 * locked.
118 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800119static
120struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
121 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800122{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800123 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800124
125 for (;;) {
126 base = timer->base;
127 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800128 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800129 if (likely(base == timer->base))
130 return base;
131 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800132 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800133 }
134 cpu_relax();
135 }
136}
137
138/*
139 * Switch the timer base to the current CPU when possible.
140 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800141static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530142switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
143 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800144{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800145 struct hrtimer_clock_base *new_base;
146 struct hrtimer_cpu_base *new_cpu_base;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530147 int cpu, preferred_cpu = -1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800148
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530149 cpu = smp_processor_id();
150#if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP)
151 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu)) {
152 preferred_cpu = get_nohz_load_balancer();
153 if (preferred_cpu >= 0)
154 cpu = preferred_cpu;
155 }
156#endif
157
158again:
159 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800160 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800161
162 if (base != new_base) {
163 /*
164 * We are trying to schedule the timer on the local CPU.
165 * However we can't change timer's base while it is running,
166 * so we keep it on the same CPU. No hassle vs. reprogramming
167 * the event source in the high resolution case. The softirq
168 * code will take care of this when the timer function has
169 * completed. There is no conflict as we hold the lock until
170 * the timer is enqueued.
171 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800172 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800173 return base;
174
175 /* See the comment in lock_timer_base() */
176 timer->base = NULL;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800177 spin_unlock(&base->cpu_base->lock);
178 spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530179
180 /* Optimized away for NOHZ=n SMP=n */
181 if (cpu == preferred_cpu) {
182 /* Calculate clock monotonic expiry time */
183#ifdef CONFIG_HIGH_RES_TIMERS
184 ktime_t expires = ktime_sub(hrtimer_get_expires(timer),
185 new_base->offset);
186#else
187 ktime_t expires = hrtimer_get_expires(timer);
188#endif
189
190 /*
191 * Get the next event on target cpu from the
192 * clock events layer.
193 * This covers the highres=off nohz=on case as well.
194 */
195 ktime_t next = clockevents_get_next_event(cpu);
196
197 ktime_t delta = ktime_sub(expires, next);
198
199 /*
200 * We do not migrate the timer when it is expiring
201 * before the next event on the target cpu because
202 * we cannot reprogram the target cpu hardware and
203 * we would cause it to fire late.
204 */
205 if (delta.tv64 < 0) {
206 cpu = smp_processor_id();
207 spin_unlock(&new_base->cpu_base->lock);
208 spin_lock(&base->cpu_base->lock);
209 timer->base = base;
210 goto again;
211 }
212 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800213 timer->base = new_base;
214 }
215 return new_base;
216}
217
218#else /* CONFIG_SMP */
219
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800220static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800221lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
222{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800223 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800224
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800225 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800226
227 return base;
228}
229
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530230# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800231
232#endif /* !CONFIG_SMP */
233
234/*
235 * Functions for the union type storage format of ktime_t which are
236 * too large for inlining:
237 */
238#if BITS_PER_LONG < 64
239# ifndef CONFIG_KTIME_SCALAR
240/**
241 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800242 * @kt: addend
243 * @nsec: the scalar nsec value to add
244 *
245 * Returns the sum of kt and nsec in ktime_t format
246 */
247ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
248{
249 ktime_t tmp;
250
251 if (likely(nsec < NSEC_PER_SEC)) {
252 tmp.tv64 = nsec;
253 } else {
254 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
255
256 tmp = ktime_set((long)nsec, rem);
257 }
258
259 return ktime_add(kt, tmp);
260}
David Howellsb8b8fd22007-04-27 15:31:24 -0700261
262EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700263
264/**
265 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
266 * @kt: minuend
267 * @nsec: the scalar nsec value to subtract
268 *
269 * Returns the subtraction of @nsec from @kt in ktime_t format
270 */
271ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
272{
273 ktime_t tmp;
274
275 if (likely(nsec < NSEC_PER_SEC)) {
276 tmp.tv64 = nsec;
277 } else {
278 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
279
280 tmp = ktime_set((long)nsec, rem);
281 }
282
283 return ktime_sub(kt, tmp);
284}
285
286EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800287# endif /* !CONFIG_KTIME_SCALAR */
288
289/*
290 * Divide a ktime value by a nanosecond value
291 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800292u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800293{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300294 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800295 int sft = 0;
296
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300297 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800298 /* Make sure the divisor is less than 2^32: */
299 while (div >> 32) {
300 sft++;
301 div >>= 1;
302 }
303 dclc >>= sft;
304 do_div(dclc, (unsigned long) div);
305
Davide Libenzi4d672e72008-02-04 22:27:26 -0800306 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800307}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800308#endif /* BITS_PER_LONG >= 64 */
309
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100310/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100311 * Add two ktime values and do a safety check for overflow:
312 */
313ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
314{
315 ktime_t res = ktime_add(lhs, rhs);
316
317 /*
318 * We use KTIME_SEC_MAX here, the maximum timeout which we can
319 * return to user space in a timespec:
320 */
321 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
322 res = ktime_set(KTIME_SEC_MAX, 0);
323
324 return res;
325}
326
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300327EXPORT_SYMBOL_GPL(ktime_add_safe);
328
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700329#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
330
331static struct debug_obj_descr hrtimer_debug_descr;
332
333/*
334 * fixup_init is called when:
335 * - an active object is initialized
336 */
337static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
338{
339 struct hrtimer *timer = addr;
340
341 switch (state) {
342 case ODEBUG_STATE_ACTIVE:
343 hrtimer_cancel(timer);
344 debug_object_init(timer, &hrtimer_debug_descr);
345 return 1;
346 default:
347 return 0;
348 }
349}
350
351/*
352 * fixup_activate is called when:
353 * - an active object is activated
354 * - an unknown object is activated (might be a statically initialized object)
355 */
356static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
357{
358 switch (state) {
359
360 case ODEBUG_STATE_NOTAVAILABLE:
361 WARN_ON_ONCE(1);
362 return 0;
363
364 case ODEBUG_STATE_ACTIVE:
365 WARN_ON(1);
366
367 default:
368 return 0;
369 }
370}
371
372/*
373 * fixup_free is called when:
374 * - an active object is freed
375 */
376static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
377{
378 struct hrtimer *timer = addr;
379
380 switch (state) {
381 case ODEBUG_STATE_ACTIVE:
382 hrtimer_cancel(timer);
383 debug_object_free(timer, &hrtimer_debug_descr);
384 return 1;
385 default:
386 return 0;
387 }
388}
389
390static struct debug_obj_descr hrtimer_debug_descr = {
391 .name = "hrtimer",
392 .fixup_init = hrtimer_fixup_init,
393 .fixup_activate = hrtimer_fixup_activate,
394 .fixup_free = hrtimer_fixup_free,
395};
396
397static inline void debug_hrtimer_init(struct hrtimer *timer)
398{
399 debug_object_init(timer, &hrtimer_debug_descr);
400}
401
402static inline void debug_hrtimer_activate(struct hrtimer *timer)
403{
404 debug_object_activate(timer, &hrtimer_debug_descr);
405}
406
407static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
408{
409 debug_object_deactivate(timer, &hrtimer_debug_descr);
410}
411
412static inline void debug_hrtimer_free(struct hrtimer *timer)
413{
414 debug_object_free(timer, &hrtimer_debug_descr);
415}
416
417static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
418 enum hrtimer_mode mode);
419
420void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
421 enum hrtimer_mode mode)
422{
423 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
424 __hrtimer_init(timer, clock_id, mode);
425}
426
427void destroy_hrtimer_on_stack(struct hrtimer *timer)
428{
429 debug_object_free(timer, &hrtimer_debug_descr);
430}
431
432#else
433static inline void debug_hrtimer_init(struct hrtimer *timer) { }
434static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
435static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
436#endif
437
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800438/* High resolution timer related functions */
439#ifdef CONFIG_HIGH_RES_TIMERS
440
441/*
442 * High resolution timer enabled ?
443 */
444static int hrtimer_hres_enabled __read_mostly = 1;
445
446/*
447 * Enable / Disable high resolution mode
448 */
449static int __init setup_hrtimer_hres(char *str)
450{
451 if (!strcmp(str, "off"))
452 hrtimer_hres_enabled = 0;
453 else if (!strcmp(str, "on"))
454 hrtimer_hres_enabled = 1;
455 else
456 return 0;
457 return 1;
458}
459
460__setup("highres=", setup_hrtimer_hres);
461
462/*
463 * hrtimer_high_res_enabled - query, if the highres mode is enabled
464 */
465static inline int hrtimer_is_hres_enabled(void)
466{
467 return hrtimer_hres_enabled;
468}
469
470/*
471 * Is the high resolution mode active ?
472 */
473static inline int hrtimer_hres_active(void)
474{
475 return __get_cpu_var(hrtimer_bases).hres_active;
476}
477
478/*
479 * Reprogram the event source with checking both queues for the
480 * next event
481 * Called with interrupts disabled and base->lock held
482 */
483static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
484{
485 int i;
486 struct hrtimer_clock_base *base = cpu_base->clock_base;
487 ktime_t expires;
488
489 cpu_base->expires_next.tv64 = KTIME_MAX;
490
491 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
492 struct hrtimer *timer;
493
494 if (!base->first)
495 continue;
496 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700497 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100498 /*
499 * clock_was_set() has changed base->offset so the
500 * result might be negative. Fix it up to prevent a
501 * false positive in clockevents_program_event()
502 */
503 if (expires.tv64 < 0)
504 expires.tv64 = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800505 if (expires.tv64 < cpu_base->expires_next.tv64)
506 cpu_base->expires_next = expires;
507 }
508
509 if (cpu_base->expires_next.tv64 != KTIME_MAX)
510 tick_program_event(cpu_base->expires_next, 1);
511}
512
513/*
514 * Shared reprogramming for clock_realtime and clock_monotonic
515 *
516 * When a timer is enqueued and expires earlier than the already enqueued
517 * timers, we have to check, whether it expires earlier than the timer for
518 * which the clock event device was armed.
519 *
520 * Called with interrupts disabled and base->cpu_base.lock held
521 */
522static int hrtimer_reprogram(struct hrtimer *timer,
523 struct hrtimer_clock_base *base)
524{
525 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700526 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800527 int res;
528
Arjan van de Vencc584b22008-09-01 15:02:30 -0700529 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100530
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800531 /*
532 * When the callback is running, we do not reprogram the clock event
533 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200534 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800535 * reprogramming is handled either by the softirq, which called the
536 * callback or at the end of the hrtimer_interrupt.
537 */
538 if (hrtimer_callback_running(timer))
539 return 0;
540
Thomas Gleixner63070a72008-02-14 00:58:36 +0100541 /*
542 * CLOCK_REALTIME timer might be requested with an absolute
543 * expiry time which is less than base->offset. Nothing wrong
544 * about that, just avoid to call into the tick code, which
545 * has now objections against negative expiry values.
546 */
547 if (expires.tv64 < 0)
548 return -ETIME;
549
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800550 if (expires.tv64 >= expires_next->tv64)
551 return 0;
552
553 /*
554 * Clockevents returns -ETIME, when the event was in the past.
555 */
556 res = tick_program_event(expires, 0);
557 if (!IS_ERR_VALUE(res))
558 *expires_next = expires;
559 return res;
560}
561
562
563/*
564 * Retrigger next event is called after clock was set
565 *
566 * Called with interrupts disabled via on_each_cpu()
567 */
568static void retrigger_next_event(void *arg)
569{
570 struct hrtimer_cpu_base *base;
571 struct timespec realtime_offset;
572 unsigned long seq;
573
574 if (!hrtimer_hres_active())
575 return;
576
577 do {
578 seq = read_seqbegin(&xtime_lock);
579 set_normalized_timespec(&realtime_offset,
580 -wall_to_monotonic.tv_sec,
581 -wall_to_monotonic.tv_nsec);
582 } while (read_seqretry(&xtime_lock, seq));
583
584 base = &__get_cpu_var(hrtimer_bases);
585
586 /* Adjust CLOCK_REALTIME offset */
587 spin_lock(&base->lock);
588 base->clock_base[CLOCK_REALTIME].offset =
589 timespec_to_ktime(realtime_offset);
590
591 hrtimer_force_reprogram(base);
592 spin_unlock(&base->lock);
593}
594
595/*
596 * Clock realtime was set
597 *
598 * Change the offset of the realtime clock vs. the monotonic
599 * clock.
600 *
601 * We might have to reprogram the high resolution timer interrupt. On
602 * SMP we call the architecture specific code to retrigger _all_ high
603 * resolution timer interrupts. On UP we just disable interrupts and
604 * call the high resolution interrupt code.
605 */
606void clock_was_set(void)
607{
608 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200609 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800610}
611
612/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200613 * During resume we might have to reprogram the high resolution timer
614 * interrupt (on the local CPU):
615 */
616void hres_timers_resume(void)
617{
Peter Zijlstra1d4a7f12009-01-18 16:39:29 +0100618 WARN_ONCE(!irqs_disabled(),
619 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
620
Ingo Molnar995f0542007-04-07 12:05:00 +0200621 retrigger_next_event(NULL);
622}
623
624/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800625 * Initialize the high resolution related parts of cpu_base
626 */
627static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
628{
629 base->expires_next.tv64 = KTIME_MAX;
630 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800631}
632
633/*
634 * Initialize the high resolution related parts of a hrtimer
635 */
636static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
637{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800638}
639
Peter Zijlstraca109492008-11-25 12:43:51 +0100640
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800641/*
642 * When High resolution timers are active, try to reprogram. Note, that in case
643 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
644 * check happens. The timer gets enqueued into the rbtree. The reprogramming
645 * and expiry check is done in the hrtimer_interrupt or in the softirq.
646 */
647static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100648 struct hrtimer_clock_base *base,
649 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800650{
651 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100652 if (wakeup) {
653 spin_unlock(&base->cpu_base->lock);
654 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
655 spin_lock(&base->cpu_base->lock);
656 } else
657 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
658
Peter Zijlstraca109492008-11-25 12:43:51 +0100659 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800660 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100661
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800662 return 0;
663}
664
665/*
666 * Switch to high resolution mode
667 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800668static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800669{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700670 int cpu = smp_processor_id();
671 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800672 unsigned long flags;
673
674 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800675 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800676
677 local_irq_save(flags);
678
679 if (tick_init_highres()) {
680 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700681 printk(KERN_WARNING "Could not switch to high resolution "
682 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800683 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800684 }
685 base->hres_active = 1;
686 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
687 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
688
689 tick_setup_sched_timer();
690
691 /* "Retrigger" the interrupt to get things going */
692 retrigger_next_event(NULL);
693 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100694 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800695 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800696 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800697}
698
699#else
700
701static inline int hrtimer_hres_active(void) { return 0; }
702static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800703static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800704static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
705static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100706 struct hrtimer_clock_base *base,
707 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800708{
709 return 0;
710}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800711static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
712static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
713
714#endif /* CONFIG_HIGH_RES_TIMERS */
715
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800716#ifdef CONFIG_TIMER_STATS
717void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
718{
719 if (timer->start_site)
720 return;
721
722 timer->start_site = addr;
723 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
724 timer->start_pid = current->pid;
725}
726#endif
727
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800728/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200729 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800730 */
731static inline
732void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
733{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800734 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800735}
736
737/**
738 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800739 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800740 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800741 * @interval: the interval to forward
742 *
743 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700744 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800745 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800746u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800747{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800748 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800749 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800750
Arjan van de Vencc584b22008-09-01 15:02:30 -0700751 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800752
753 if (delta.tv64 < 0)
754 return 0;
755
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100756 if (interval.tv64 < timer->base->resolution.tv64)
757 interval.tv64 = timer->base->resolution.tv64;
758
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800759 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800760 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800761
762 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700763 hrtimer_add_expires_ns(timer, incr * orun);
764 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800765 return orun;
766 /*
767 * This (and the ktime_add() below) is the
768 * correction for exact:
769 */
770 orun++;
771 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700772 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800773
774 return orun;
775}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700776EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800777
778/*
779 * enqueue_hrtimer - internal function to (re)start a timer
780 *
781 * The timer is inserted in expiry order. Insertion into the
782 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100783 *
784 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800785 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100786static int enqueue_hrtimer(struct hrtimer *timer,
787 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800788{
789 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800790 struct rb_node *parent = NULL;
791 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700792 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800793
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700794 debug_hrtimer_activate(timer);
795
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800796 /*
797 * Find the right place in the rbtree:
798 */
799 while (*link) {
800 parent = *link;
801 entry = rb_entry(parent, struct hrtimer, node);
802 /*
803 * We dont care about collisions. Nodes with
804 * the same expiry time stay together.
805 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700806 if (hrtimer_get_expires_tv64(timer) <
807 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800808 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700809 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800810 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700811 leftmost = 0;
812 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800813 }
814
815 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100816 * Insert the timer to the rbtree and check whether it
817 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800818 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100819 if (leftmost)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800820 base->first = &timer->node;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800821
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800822 rb_link_node(&timer->node, parent, link);
823 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800824 /*
825 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
826 * state of a possibly running callback.
827 */
828 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100829
830 return leftmost;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100831}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832
833/*
834 * __remove_hrtimer - internal function to remove a timer
835 *
836 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800837 *
838 * High resolution timer mode reprograms the clock event device when the
839 * timer is the one which expires next. The caller can disable this by setting
840 * reprogram to zero. This is useful, when the context does a reprogramming
841 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800842 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800843static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800844 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800845 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800846{
Peter Zijlstraca109492008-11-25 12:43:51 +0100847 if (timer->state & HRTIMER_STATE_ENQUEUED) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800848 /*
849 * Remove the timer from the rbtree and replace the
850 * first entry pointer if necessary.
851 */
852 if (base->first == &timer->node) {
853 base->first = rb_next(&timer->node);
854 /* Reprogram the clock event device. if enabled */
855 if (reprogram && hrtimer_hres_active())
856 hrtimer_force_reprogram(base->cpu_base);
857 }
858 rb_erase(&timer->node, &base->active);
859 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800860 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800861}
862
863/*
864 * remove hrtimer, called with base lock held
865 */
866static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800867remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800868{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800869 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800870 int reprogram;
871
872 /*
873 * Remove the timer and force reprogramming when high
874 * resolution mode is active and the timer is on the current
875 * CPU. If we remove a timer on another CPU, reprogramming is
876 * skipped. The interrupt event on this CPU is fired and
877 * reprogramming happens in the interrupt handler. This is a
878 * rare case and less expensive than a smp call.
879 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700880 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800881 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800882 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
883 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
884 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800885 return 1;
886 }
887 return 0;
888}
889
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100890int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
891 unsigned long delta_ns, const enum hrtimer_mode mode,
892 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800893{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800894 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800895 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100896 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800897
898 base = lock_hrtimer_base(timer, &flags);
899
900 /* Remove an active timer from the queue: */
901 ret = remove_hrtimer(timer, base);
902
903 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530904 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800905
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530906 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100907 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800908 /*
909 * CONFIG_TIME_LOW_RES is a temporary way for architectures
910 * to signal that they simply return xtime in
911 * do_gettimeoffset(). In this case we want to round up by
912 * resolution when starting a relative timer, to avoid short
913 * timeouts. This will go away with the GTOD framework.
914 */
915#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100916 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800917#endif
918 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700919
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700920 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800921
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800922 timer_stats_hrtimer_set_start_info(timer);
923
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100924 leftmost = enqueue_hrtimer(timer, new_base);
925
Ingo Molnar935c6312007-03-28 13:17:18 +0200926 /*
927 * Only allow reprogramming if the new base is on this CPU.
928 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100929 *
930 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200931 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100932 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100933 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800934
935 unlock_hrtimer_base(timer, &flags);
936
937 return ret;
938}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100939
940/**
941 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
942 * @timer: the timer to be added
943 * @tim: expiry time
944 * @delta_ns: "slack" range for the timer
945 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
946 *
947 * Returns:
948 * 0 on success
949 * 1 when the timer was active
950 */
951int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
952 unsigned long delta_ns, const enum hrtimer_mode mode)
953{
954 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
955}
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700956EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
957
958/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200959 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700960 * @timer: the timer to be added
961 * @tim: expiry time
962 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
963 *
964 * Returns:
965 * 0 on success
966 * 1 when the timer was active
967 */
968int
969hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
970{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100971 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700972}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700973EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800974
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700975
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800976/**
977 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800978 * @timer: hrtimer to stop
979 *
980 * Returns:
981 * 0 when the timer was not active
982 * 1 when the timer was active
983 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700984 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800985 */
986int hrtimer_try_to_cancel(struct hrtimer *timer)
987{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800988 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800989 unsigned long flags;
990 int ret = -1;
991
992 base = lock_hrtimer_base(timer, &flags);
993
Thomas Gleixner303e9672007-02-16 01:27:51 -0800994 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800995 ret = remove_hrtimer(timer, base);
996
997 unlock_hrtimer_base(timer, &flags);
998
999 return ret;
1000
1001}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001002EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001003
1004/**
1005 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001006 * @timer: the timer to be cancelled
1007 *
1008 * Returns:
1009 * 0 when the timer was not active
1010 * 1 when the timer was active
1011 */
1012int hrtimer_cancel(struct hrtimer *timer)
1013{
1014 for (;;) {
1015 int ret = hrtimer_try_to_cancel(timer);
1016
1017 if (ret >= 0)
1018 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001019 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001020 }
1021}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001022EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001023
1024/**
1025 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001026 * @timer: the timer to read
1027 */
1028ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1029{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001030 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001031 unsigned long flags;
1032 ktime_t rem;
1033
1034 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001035 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001036 unlock_hrtimer_base(timer, &flags);
1037
1038 return rem;
1039}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001040EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001041
Russell Kingee9c5782008-04-20 13:59:33 +01001042#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001043/**
1044 * hrtimer_get_next_event - get the time until next expiry event
1045 *
1046 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1047 * is pending.
1048 */
1049ktime_t hrtimer_get_next_event(void)
1050{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001051 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1052 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001053 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1054 unsigned long flags;
1055 int i;
1056
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001057 spin_lock_irqsave(&cpu_base->lock, flags);
1058
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001059 if (!hrtimer_hres_active()) {
1060 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1061 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001062
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001063 if (!base->first)
1064 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001065
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001066 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001067 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001068 delta = ktime_sub(delta, base->get_time());
1069 if (delta.tv64 < mindelta.tv64)
1070 mindelta.tv64 = delta.tv64;
1071 }
Tony Lindgren69239742006-03-06 15:42:45 -08001072 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001073
1074 spin_unlock_irqrestore(&cpu_base->lock, flags);
1075
Tony Lindgren69239742006-03-06 15:42:45 -08001076 if (mindelta.tv64 < 0)
1077 mindelta.tv64 = 0;
1078 return mindelta;
1079}
1080#endif
1081
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001082static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1083 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001084{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001085 struct hrtimer_cpu_base *cpu_base;
George Anzinger79786722006-02-01 03:05:11 -08001086
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001087 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001088
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001089 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001090
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001091 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001092 clock_id = CLOCK_MONOTONIC;
1093
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001094 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001095 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001096 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001097
1098#ifdef CONFIG_TIMER_STATS
1099 timer->start_site = NULL;
1100 timer->start_pid = -1;
1101 memset(timer->start_comm, 0, TASK_COMM_LEN);
1102#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001103}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001104
1105/**
1106 * hrtimer_init - initialize a timer to the given clock
1107 * @timer: the timer to be initialized
1108 * @clock_id: the clock to be used
1109 * @mode: timer mode abs/rel
1110 */
1111void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1112 enum hrtimer_mode mode)
1113{
1114 debug_hrtimer_init(timer);
1115 __hrtimer_init(timer, clock_id, mode);
1116}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001117EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001118
1119/**
1120 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001121 * @which_clock: which clock to query
1122 * @tp: pointer to timespec variable to store the resolution
1123 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001124 * Store the resolution of the clock selected by @which_clock in the
1125 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001126 */
1127int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1128{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001129 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001130
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001131 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1132 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001133
1134 return 0;
1135}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001136EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001137
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001138static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001139{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001140 struct hrtimer_clock_base *base = timer->base;
1141 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1142 enum hrtimer_restart (*fn)(struct hrtimer *);
1143 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001144
Peter Zijlstraca109492008-11-25 12:43:51 +01001145 WARN_ON(!irqs_disabled());
1146
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001147 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001148 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1149 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001150 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001151
1152 /*
1153 * Because we run timers from hardirq context, there is no chance
1154 * they get migrated to another cpu, therefore its safe to unlock
1155 * the timer base.
1156 */
1157 spin_unlock(&cpu_base->lock);
1158 restart = fn(timer);
1159 spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001160
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001161 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001162 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1163 * we do not reprogramm the event hardware. Happens either in
1164 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001165 */
1166 if (restart != HRTIMER_NORESTART) {
1167 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001168 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001169 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001170 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001171}
1172
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001173#ifdef CONFIG_HIGH_RES_TIMERS
1174
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001175static int force_clock_reprogram;
1176
1177/*
1178 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1179 * is hanging, which could happen with something that slows the interrupt
1180 * such as the tracing. Then we force the clock reprogramming for each future
1181 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1182 * threshold that we will overwrite.
1183 * The next tick event will be scheduled to 3 times we currently spend on
1184 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1185 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1186 * let it running without serious starvation.
1187 */
1188
1189static inline void
1190hrtimer_interrupt_hanging(struct clock_event_device *dev,
1191 ktime_t try_time)
1192{
1193 force_clock_reprogram = 1;
1194 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1195 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1196 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1197}
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001198/*
1199 * High resolution timer interrupt
1200 * Called with interrupts disabled
1201 */
1202void hrtimer_interrupt(struct clock_event_device *dev)
1203{
1204 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1205 struct hrtimer_clock_base *base;
1206 ktime_t expires_next, now;
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001207 int nr_retries = 0;
Peter Zijlstraca109492008-11-25 12:43:51 +01001208 int i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001209
1210 BUG_ON(!cpu_base->hres_active);
1211 cpu_base->nr_events++;
1212 dev->next_event.tv64 = KTIME_MAX;
1213
1214 retry:
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001215 /* 5 retries is enough to notice a hang */
1216 if (!(++nr_retries % 5))
1217 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1218
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001219 now = ktime_get();
1220
1221 expires_next.tv64 = KTIME_MAX;
1222
1223 base = cpu_base->clock_base;
1224
1225 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1226 ktime_t basenow;
1227 struct rb_node *node;
1228
1229 spin_lock(&cpu_base->lock);
1230
1231 basenow = ktime_add(now, base->offset);
1232
1233 while ((node = base->first)) {
1234 struct hrtimer *timer;
1235
1236 timer = rb_entry(node, struct hrtimer, node);
1237
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001238 /*
1239 * The immediate goal for using the softexpires is
1240 * minimizing wakeups, not running timers at the
1241 * earliest interrupt after their soft expiration.
1242 * This allows us to avoid using a Priority Search
1243 * Tree, which can answer a stabbing querry for
1244 * overlapping intervals and instead use the simple
1245 * BST we already have.
1246 * We don't add extra wakeups by delaying timers that
1247 * are right-of a not yet expired timer, because that
1248 * timer will have to trigger a wakeup anyway.
1249 */
1250
1251 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001252 ktime_t expires;
1253
Arjan van de Vencc584b22008-09-01 15:02:30 -07001254 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001255 base->offset);
1256 if (expires.tv64 < expires_next.tv64)
1257 expires_next = expires;
1258 break;
1259 }
1260
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001261 __run_hrtimer(timer);
1262 }
1263 spin_unlock(&cpu_base->lock);
1264 base++;
1265 }
1266
1267 cpu_base->expires_next = expires_next;
1268
1269 /* Reprogramming necessary ? */
1270 if (expires_next.tv64 != KTIME_MAX) {
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001271 if (tick_program_event(expires_next, force_clock_reprogram))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001272 goto retry;
1273 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001274}
1275
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001276/*
1277 * local version of hrtimer_peek_ahead_timers() called with interrupts
1278 * disabled.
1279 */
1280static void __hrtimer_peek_ahead_timers(void)
1281{
1282 struct tick_device *td;
1283
1284 if (!hrtimer_hres_active())
1285 return;
1286
1287 td = &__get_cpu_var(tick_cpu_device);
1288 if (td && td->evtdev)
1289 hrtimer_interrupt(td->evtdev);
1290}
1291
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001292/**
1293 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1294 *
1295 * hrtimer_peek_ahead_timers will peek at the timer queue of
1296 * the current cpu and check if there are any timers for which
1297 * the soft expires time has passed. If any such timers exist,
1298 * they are run immediately and then removed from the timer queue.
1299 *
1300 */
1301void hrtimer_peek_ahead_timers(void)
1302{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001303 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001304
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001305 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001306 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001307 local_irq_restore(flags);
1308}
1309
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001310static void run_hrtimer_softirq(struct softirq_action *h)
1311{
1312 hrtimer_peek_ahead_timers();
1313}
1314
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001315#else /* CONFIG_HIGH_RES_TIMERS */
1316
1317static inline void __hrtimer_peek_ahead_timers(void) { }
1318
1319#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001320
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001321/*
1322 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001323 *
1324 * For HRT its the fall back code to run the softirq in the timer
1325 * softirq context in case the hrtimer initialization failed or has
1326 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001327 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001328void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001329{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001330 if (hrtimer_hres_active())
1331 return;
1332
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001333 /*
1334 * This _is_ ugly: We have to check in the softirq context,
1335 * whether we can switch to highres and / or nohz mode. The
1336 * clocksource switch happens in the timer interrupt with
1337 * xtime_lock held. Notification from there only sets the
1338 * check bit in the tick_oneshot code, otherwise we might
1339 * deadlock vs. xtime_lock.
1340 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001341 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001342 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001343}
1344
1345/*
1346 * Called from hardirq context every jiffy
1347 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001348void hrtimer_run_queues(void)
1349{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001350 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001351 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001352 struct hrtimer_clock_base *base;
1353 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001354
1355 if (hrtimer_hres_active())
1356 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001357
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001358 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1359 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001360
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001361 if (!base->first)
1362 continue;
1363
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001364 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001365 hrtimer_get_softirq_time(cpu_base);
1366 gettime = 0;
1367 }
1368
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001369 spin_lock(&cpu_base->lock);
1370
1371 while ((node = base->first)) {
1372 struct hrtimer *timer;
1373
1374 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001375 if (base->softirq_time.tv64 <=
1376 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001377 break;
1378
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001379 __run_hrtimer(timer);
1380 }
1381 spin_unlock(&cpu_base->lock);
1382 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001383}
1384
1385/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001386 * Sleep related functions:
1387 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001388static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001389{
1390 struct hrtimer_sleeper *t =
1391 container_of(timer, struct hrtimer_sleeper, timer);
1392 struct task_struct *task = t->task;
1393
1394 t->task = NULL;
1395 if (task)
1396 wake_up_process(task);
1397
1398 return HRTIMER_NORESTART;
1399}
1400
Ingo Molnar36c8b582006-07-03 00:25:41 -07001401void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001402{
1403 sl->timer.function = hrtimer_wakeup;
1404 sl->task = task;
1405}
1406
Thomas Gleixner669d7862006-03-31 02:31:19 -08001407static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001408{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001409 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001410
Roman Zippel432569b2006-03-26 01:38:08 -08001411 do {
1412 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001413 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001414 if (!hrtimer_active(&t->timer))
1415 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001416
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001417 if (likely(t->task))
1418 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001419
Thomas Gleixner669d7862006-03-31 02:31:19 -08001420 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001421 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001422
Thomas Gleixner669d7862006-03-31 02:31:19 -08001423 } while (t->task && !signal_pending(current));
1424
Peter Zijlstra3588a082008-02-01 17:45:13 +01001425 __set_current_state(TASK_RUNNING);
1426
Thomas Gleixner669d7862006-03-31 02:31:19 -08001427 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001428}
1429
Oleg Nesterov080344b2008-02-01 17:29:05 +03001430static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1431{
1432 struct timespec rmt;
1433 ktime_t rem;
1434
Arjan van de Vencc584b22008-09-01 15:02:30 -07001435 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001436 if (rem.tv64 <= 0)
1437 return 0;
1438 rmt = ktime_to_timespec(rem);
1439
1440 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1441 return -EFAULT;
1442
1443 return 1;
1444}
1445
Toyo Abe1711ef32006-09-29 02:00:28 -07001446long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001447{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001448 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001449 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001450 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001451
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001452 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1453 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001454 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001455
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001456 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001457 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001458
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001459 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001460 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001461 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001462 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001463 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001464 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001465
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001466 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001467 ret = -ERESTART_RESTARTBLOCK;
1468out:
1469 destroy_hrtimer_on_stack(&t.timer);
1470 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001471}
1472
Oleg Nesterov080344b2008-02-01 17:29:05 +03001473long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001474 const enum hrtimer_mode mode, const clockid_t clockid)
1475{
1476 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001477 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001478 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001479 unsigned long slack;
1480
1481 slack = current->timer_slack_ns;
1482 if (rt_task(current))
1483 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001484
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001485 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001486 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001487 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001488 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001489
George Anzinger79786722006-02-01 03:05:11 -08001490 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001491 if (mode == HRTIMER_MODE_ABS) {
1492 ret = -ERESTARTNOHAND;
1493 goto out;
1494 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001495
Roman Zippel432569b2006-03-26 01:38:08 -08001496 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001497 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001498 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001499 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001500 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001501
1502 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001503 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001504 restart->nanosleep.index = t.timer.base->index;
1505 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001506 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001507
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001508 ret = -ERESTART_RESTARTBLOCK;
1509out:
1510 destroy_hrtimer_on_stack(&t.timer);
1511 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001512}
1513
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001514SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1515 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001516{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001517 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001518
1519 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1520 return -EFAULT;
1521
1522 if (!timespec_valid(&tu))
1523 return -EINVAL;
1524
Oleg Nesterov080344b2008-02-01 17:29:05 +03001525 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001526}
1527
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001528/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001529 * Functions related to boot-time initialization:
1530 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001531static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001532{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001533 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001534 int i;
1535
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001536 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001537
1538 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1539 cpu_base->clock_base[i].cpu_base = cpu_base;
1540
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001541 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001542}
1543
1544#ifdef CONFIG_HOTPLUG_CPU
1545
Peter Zijlstraca109492008-11-25 12:43:51 +01001546static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001547 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001548{
1549 struct hrtimer *timer;
1550 struct rb_node *node;
1551
1552 while ((node = rb_first(&old_base->active))) {
1553 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001554 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001555 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001556
1557 /*
1558 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1559 * timer could be seen as !active and just vanish away
1560 * under us on another CPU
1561 */
1562 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001563 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001564 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001565 * Enqueue the timers on the new cpu. This does not
1566 * reprogram the event device in case the timer
1567 * expires before the earliest on this CPU, but we run
1568 * hrtimer_interrupt after we migrated everything to
1569 * sort out already expired timers and reprogram the
1570 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001571 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001572 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001573
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001574 /* Clear the migration state bit */
1575 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001576 }
1577}
1578
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001579static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001580{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001581 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001582 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001583
Peter Zijlstra37810652008-12-04 11:17:10 +01001584 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001585 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001586
1587 local_irq_disable();
1588 old_base = &per_cpu(hrtimer_bases, scpu);
1589 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001590 /*
1591 * The caller is globally serialized and nobody else
1592 * takes two locks at once, deadlock is not possible.
1593 */
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001594 spin_lock(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001595 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001596
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001597 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001598 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001599 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001600 }
1601
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001602 spin_unlock(&old_base->lock);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001603 spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001604
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001605 /* Check, if we got expired work to do */
1606 __hrtimer_peek_ahead_timers();
1607 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001608}
1609
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001610#endif /* CONFIG_HOTPLUG_CPU */
1611
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001612static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001613 unsigned long action, void *hcpu)
1614{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001615 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001616
1617 switch (action) {
1618
1619 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001620 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001621 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001622 break;
1623
1624#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001625 case CPU_DYING:
1626 case CPU_DYING_FROZEN:
1627 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1628 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001629 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001630 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001631 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001632 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001633 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001634 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001635 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001636#endif
1637
1638 default:
1639 break;
1640 }
1641
1642 return NOTIFY_OK;
1643}
1644
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001645static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001646 .notifier_call = hrtimer_cpu_notify,
1647};
1648
1649void __init hrtimers_init(void)
1650{
1651 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1652 (void *)(long)smp_processor_id());
1653 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001654#ifdef CONFIG_HIGH_RES_TIMERS
1655 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1656#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001657}
1658
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001659/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001660 * schedule_hrtimeout_range - sleep until timeout
1661 * @expires: timeout value (ktime_t)
1662 * @delta: slack in expires timeout (ktime_t)
1663 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1664 *
1665 * Make the current task sleep until the given expiry time has
1666 * elapsed. The routine will return immediately unless
1667 * the current task state has been set (see set_current_state()).
1668 *
1669 * The @delta argument gives the kernel the freedom to schedule the
1670 * actual wakeup to a time that is both power and performance friendly.
1671 * The kernel give the normal best effort behavior for "@expires+@delta",
1672 * but may decide to fire the timer earlier, but no earlier than @expires.
1673 *
1674 * You can set the task state as follows -
1675 *
1676 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1677 * pass before the routine returns.
1678 *
1679 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1680 * delivered to the current task.
1681 *
1682 * The current task state is guaranteed to be TASK_RUNNING when this
1683 * routine returns.
1684 *
1685 * Returns 0 when the timer has expired otherwise -EINTR
1686 */
1687int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1688 const enum hrtimer_mode mode)
1689{
1690 struct hrtimer_sleeper t;
1691
1692 /*
1693 * Optimize when a zero timeout value is given. It does not
1694 * matter whether this is an absolute or a relative time.
1695 */
1696 if (expires && !expires->tv64) {
1697 __set_current_state(TASK_RUNNING);
1698 return 0;
1699 }
1700
1701 /*
1702 * A NULL parameter means "inifinte"
1703 */
1704 if (!expires) {
1705 schedule();
1706 __set_current_state(TASK_RUNNING);
1707 return -EINTR;
1708 }
1709
1710 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1711 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1712
1713 hrtimer_init_sleeper(&t, current);
1714
1715 hrtimer_start_expires(&t.timer, mode);
1716 if (!hrtimer_active(&t.timer))
1717 t.task = NULL;
1718
1719 if (likely(t.task))
1720 schedule();
1721
1722 hrtimer_cancel(&t.timer);
1723 destroy_hrtimer_on_stack(&t.timer);
1724
1725 __set_current_state(TASK_RUNNING);
1726
1727 return !t.task ? 0 : -EINTR;
1728}
1729EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1730
1731/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001732 * schedule_hrtimeout - sleep until timeout
1733 * @expires: timeout value (ktime_t)
1734 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1735 *
1736 * Make the current task sleep until the given expiry time has
1737 * elapsed. The routine will return immediately unless
1738 * the current task state has been set (see set_current_state()).
1739 *
1740 * You can set the task state as follows -
1741 *
1742 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1743 * pass before the routine returns.
1744 *
1745 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1746 * delivered to the current task.
1747 *
1748 * The current task state is guaranteed to be TASK_RUNNING when this
1749 * routine returns.
1750 *
1751 * Returns 0 when the timer has expired otherwise -EINTR
1752 */
1753int __sched schedule_hrtimeout(ktime_t *expires,
1754 const enum hrtimer_mode mode)
1755{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001756 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001757}
1758EXPORT_SYMBOL_GPL(schedule_hrtimeout);