blob: 829e0664b72ea9b300d00b6a02c164cd27321932 [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
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +020051#ifndef CONFIG_GENERIC_TIME
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080052/**
53 * ktime_get - get the monotonic time in ktime_t format
54 *
55 * returns the time in ktime_t format
56 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080057ktime_t ktime_get(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080058{
59 struct timespec now;
60
61 ktime_get_ts(&now);
62
63 return timespec_to_ktime(now);
64}
Patrick McHardy641b9e02007-03-16 01:18:42 -070065EXPORT_SYMBOL_GPL(ktime_get);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +020066#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080067
68/**
69 * ktime_get_real - get the real (wall-) time in ktime_t format
70 *
71 * returns the time in ktime_t format
72 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080073ktime_t ktime_get_real(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080074{
75 struct timespec now;
76
77 getnstimeofday(&now);
78
79 return timespec_to_ktime(now);
80}
81
82EXPORT_SYMBOL_GPL(ktime_get_real);
83
84/*
85 * The timer bases:
George Anzinger7978672c2006-02-01 03:05:11 -080086 *
87 * Note: If we want to add new timer bases, we have to skip the two
88 * clock ids captured by the cpu-timers. We do this by holding empty
89 * entries rather than doing math adjustment of the clock ids.
90 * This ensures that we capture erroneous accesses to these clock ids
91 * rather than moving them into the range of valid clock id's.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080092 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080093DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080094{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080095
96 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080097 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080098 {
99 .index = CLOCK_REALTIME,
100 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800101 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800102 },
103 {
104 .index = CLOCK_MONOTONIC,
105 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800106 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800107 },
108 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800109};
110
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200111#ifndef CONFIG_GENERIC_TIME
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800112/**
113 * ktime_get_ts - get the monotonic clock in timespec format
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800114 * @ts: pointer to timespec variable
115 *
116 * The function calculates the monotonic clock from the realtime
117 * clock and the wall_to_monotonic offset and stores the result
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800118 * in normalized timespec format in the variable pointed to by @ts.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800119 */
120void ktime_get_ts(struct timespec *ts)
121{
122 struct timespec tomono;
123 unsigned long seq;
124
125 do {
126 seq = read_seqbegin(&xtime_lock);
127 getnstimeofday(ts);
128 tomono = wall_to_monotonic;
129
130 } while (read_seqretry(&xtime_lock, seq));
131
132 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
133 ts->tv_nsec + tomono.tv_nsec);
134}
Matt Helsley69778e32006-01-09 20:52:39 -0800135EXPORT_SYMBOL_GPL(ktime_get_ts);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200136#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800137
138/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800139 * Get the coarse grained time at the softirq based on xtime and
140 * wall_to_monotonic.
141 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800142static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800143{
144 ktime_t xtim, tomono;
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800145 struct timespec xts, tom;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800146 unsigned long seq;
147
148 do {
149 seq = read_seqbegin(&xtime_lock);
john stultz2c6b47d2007-07-24 17:47:43 -0700150 xts = current_kernel_time();
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800151 tom = wall_to_monotonic;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800152 } while (read_seqretry(&xtime_lock, seq));
153
john stultzf4304ab2007-02-16 01:27:26 -0800154 xtim = timespec_to_ktime(xts);
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800155 tomono = timespec_to_ktime(tom);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800156 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
157 base->clock_base[CLOCK_MONOTONIC].softirq_time =
158 ktime_add(xtim, tomono);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800159}
160
161/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800162 * Functions and macros which are different for UP/SMP systems are kept in a
163 * single place
164 */
165#ifdef CONFIG_SMP
166
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800167/*
168 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
169 * means that all timers which are tied to this base via timer->base are
170 * locked, and the base itself is locked too.
171 *
172 * So __run_timers/migrate_timers can safely modify all timers which could
173 * be found on the lists/queues.
174 *
175 * When the timer's base is locked, and the timer removed from list, it is
176 * possible to set timer->base = NULL and drop the lock: the timer remains
177 * locked.
178 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800179static
180struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
181 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800182{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800183 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800184
185 for (;;) {
186 base = timer->base;
187 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800188 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800189 if (likely(base == timer->base))
190 return base;
191 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800192 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800193 }
194 cpu_relax();
195 }
196}
197
198/*
199 * Switch the timer base to the current CPU when possible.
200 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800201static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530202switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
203 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800204{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800205 struct hrtimer_clock_base *new_base;
206 struct hrtimer_cpu_base *new_cpu_base;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530207 int cpu, preferred_cpu = -1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800208
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530209 cpu = smp_processor_id();
210#if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP)
211 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu)) {
212 preferred_cpu = get_nohz_load_balancer();
213 if (preferred_cpu >= 0)
214 cpu = preferred_cpu;
215 }
216#endif
217
218again:
219 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800220 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800221
222 if (base != new_base) {
223 /*
224 * We are trying to schedule the timer on the local CPU.
225 * However we can't change timer's base while it is running,
226 * so we keep it on the same CPU. No hassle vs. reprogramming
227 * the event source in the high resolution case. The softirq
228 * code will take care of this when the timer function has
229 * completed. There is no conflict as we hold the lock until
230 * the timer is enqueued.
231 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800232 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800233 return base;
234
235 /* See the comment in lock_timer_base() */
236 timer->base = NULL;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800237 spin_unlock(&base->cpu_base->lock);
238 spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530239
240 /* Optimized away for NOHZ=n SMP=n */
241 if (cpu == preferred_cpu) {
242 /* Calculate clock monotonic expiry time */
243#ifdef CONFIG_HIGH_RES_TIMERS
244 ktime_t expires = ktime_sub(hrtimer_get_expires(timer),
245 new_base->offset);
246#else
247 ktime_t expires = hrtimer_get_expires(timer);
248#endif
249
250 /*
251 * Get the next event on target cpu from the
252 * clock events layer.
253 * This covers the highres=off nohz=on case as well.
254 */
255 ktime_t next = clockevents_get_next_event(cpu);
256
257 ktime_t delta = ktime_sub(expires, next);
258
259 /*
260 * We do not migrate the timer when it is expiring
261 * before the next event on the target cpu because
262 * we cannot reprogram the target cpu hardware and
263 * we would cause it to fire late.
264 */
265 if (delta.tv64 < 0) {
266 cpu = smp_processor_id();
267 spin_unlock(&new_base->cpu_base->lock);
268 spin_lock(&base->cpu_base->lock);
269 timer->base = base;
270 goto again;
271 }
272 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800273 timer->base = new_base;
274 }
275 return new_base;
276}
277
278#else /* CONFIG_SMP */
279
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800280static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800281lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
282{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800283 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800284
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800285 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800286
287 return base;
288}
289
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530290# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800291
292#endif /* !CONFIG_SMP */
293
294/*
295 * Functions for the union type storage format of ktime_t which are
296 * too large for inlining:
297 */
298#if BITS_PER_LONG < 64
299# ifndef CONFIG_KTIME_SCALAR
300/**
301 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800302 * @kt: addend
303 * @nsec: the scalar nsec value to add
304 *
305 * Returns the sum of kt and nsec in ktime_t format
306 */
307ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
308{
309 ktime_t tmp;
310
311 if (likely(nsec < NSEC_PER_SEC)) {
312 tmp.tv64 = nsec;
313 } else {
314 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
315
316 tmp = ktime_set((long)nsec, rem);
317 }
318
319 return ktime_add(kt, tmp);
320}
David Howellsb8b8fd22007-04-27 15:31:24 -0700321
322EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700323
324/**
325 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
326 * @kt: minuend
327 * @nsec: the scalar nsec value to subtract
328 *
329 * Returns the subtraction of @nsec from @kt in ktime_t format
330 */
331ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
332{
333 ktime_t tmp;
334
335 if (likely(nsec < NSEC_PER_SEC)) {
336 tmp.tv64 = nsec;
337 } else {
338 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
339
340 tmp = ktime_set((long)nsec, rem);
341 }
342
343 return ktime_sub(kt, tmp);
344}
345
346EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800347# endif /* !CONFIG_KTIME_SCALAR */
348
349/*
350 * Divide a ktime value by a nanosecond value
351 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800352u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800353{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300354 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800355 int sft = 0;
356
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300357 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800358 /* Make sure the divisor is less than 2^32: */
359 while (div >> 32) {
360 sft++;
361 div >>= 1;
362 }
363 dclc >>= sft;
364 do_div(dclc, (unsigned long) div);
365
Davide Libenzi4d672e72008-02-04 22:27:26 -0800366 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800367}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800368#endif /* BITS_PER_LONG >= 64 */
369
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100370/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100371 * Add two ktime values and do a safety check for overflow:
372 */
373ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
374{
375 ktime_t res = ktime_add(lhs, rhs);
376
377 /*
378 * We use KTIME_SEC_MAX here, the maximum timeout which we can
379 * return to user space in a timespec:
380 */
381 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
382 res = ktime_set(KTIME_SEC_MAX, 0);
383
384 return res;
385}
386
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300387EXPORT_SYMBOL_GPL(ktime_add_safe);
388
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700389#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
390
391static struct debug_obj_descr hrtimer_debug_descr;
392
393/*
394 * fixup_init is called when:
395 * - an active object is initialized
396 */
397static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
398{
399 struct hrtimer *timer = addr;
400
401 switch (state) {
402 case ODEBUG_STATE_ACTIVE:
403 hrtimer_cancel(timer);
404 debug_object_init(timer, &hrtimer_debug_descr);
405 return 1;
406 default:
407 return 0;
408 }
409}
410
411/*
412 * fixup_activate is called when:
413 * - an active object is activated
414 * - an unknown object is activated (might be a statically initialized object)
415 */
416static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
417{
418 switch (state) {
419
420 case ODEBUG_STATE_NOTAVAILABLE:
421 WARN_ON_ONCE(1);
422 return 0;
423
424 case ODEBUG_STATE_ACTIVE:
425 WARN_ON(1);
426
427 default:
428 return 0;
429 }
430}
431
432/*
433 * fixup_free is called when:
434 * - an active object is freed
435 */
436static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
437{
438 struct hrtimer *timer = addr;
439
440 switch (state) {
441 case ODEBUG_STATE_ACTIVE:
442 hrtimer_cancel(timer);
443 debug_object_free(timer, &hrtimer_debug_descr);
444 return 1;
445 default:
446 return 0;
447 }
448}
449
450static struct debug_obj_descr hrtimer_debug_descr = {
451 .name = "hrtimer",
452 .fixup_init = hrtimer_fixup_init,
453 .fixup_activate = hrtimer_fixup_activate,
454 .fixup_free = hrtimer_fixup_free,
455};
456
457static inline void debug_hrtimer_init(struct hrtimer *timer)
458{
459 debug_object_init(timer, &hrtimer_debug_descr);
460}
461
462static inline void debug_hrtimer_activate(struct hrtimer *timer)
463{
464 debug_object_activate(timer, &hrtimer_debug_descr);
465}
466
467static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
468{
469 debug_object_deactivate(timer, &hrtimer_debug_descr);
470}
471
472static inline void debug_hrtimer_free(struct hrtimer *timer)
473{
474 debug_object_free(timer, &hrtimer_debug_descr);
475}
476
477static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
478 enum hrtimer_mode mode);
479
480void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
481 enum hrtimer_mode mode)
482{
483 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
484 __hrtimer_init(timer, clock_id, mode);
485}
486
487void destroy_hrtimer_on_stack(struct hrtimer *timer)
488{
489 debug_object_free(timer, &hrtimer_debug_descr);
490}
491
492#else
493static inline void debug_hrtimer_init(struct hrtimer *timer) { }
494static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
495static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
496#endif
497
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800498/* High resolution timer related functions */
499#ifdef CONFIG_HIGH_RES_TIMERS
500
501/*
502 * High resolution timer enabled ?
503 */
504static int hrtimer_hres_enabled __read_mostly = 1;
505
506/*
507 * Enable / Disable high resolution mode
508 */
509static int __init setup_hrtimer_hres(char *str)
510{
511 if (!strcmp(str, "off"))
512 hrtimer_hres_enabled = 0;
513 else if (!strcmp(str, "on"))
514 hrtimer_hres_enabled = 1;
515 else
516 return 0;
517 return 1;
518}
519
520__setup("highres=", setup_hrtimer_hres);
521
522/*
523 * hrtimer_high_res_enabled - query, if the highres mode is enabled
524 */
525static inline int hrtimer_is_hres_enabled(void)
526{
527 return hrtimer_hres_enabled;
528}
529
530/*
531 * Is the high resolution mode active ?
532 */
533static inline int hrtimer_hres_active(void)
534{
535 return __get_cpu_var(hrtimer_bases).hres_active;
536}
537
538/*
539 * Reprogram the event source with checking both queues for the
540 * next event
541 * Called with interrupts disabled and base->lock held
542 */
543static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
544{
545 int i;
546 struct hrtimer_clock_base *base = cpu_base->clock_base;
547 ktime_t expires;
548
549 cpu_base->expires_next.tv64 = KTIME_MAX;
550
551 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
552 struct hrtimer *timer;
553
554 if (!base->first)
555 continue;
556 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700557 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100558 /*
559 * clock_was_set() has changed base->offset so the
560 * result might be negative. Fix it up to prevent a
561 * false positive in clockevents_program_event()
562 */
563 if (expires.tv64 < 0)
564 expires.tv64 = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800565 if (expires.tv64 < cpu_base->expires_next.tv64)
566 cpu_base->expires_next = expires;
567 }
568
569 if (cpu_base->expires_next.tv64 != KTIME_MAX)
570 tick_program_event(cpu_base->expires_next, 1);
571}
572
573/*
574 * Shared reprogramming for clock_realtime and clock_monotonic
575 *
576 * When a timer is enqueued and expires earlier than the already enqueued
577 * timers, we have to check, whether it expires earlier than the timer for
578 * which the clock event device was armed.
579 *
580 * Called with interrupts disabled and base->cpu_base.lock held
581 */
582static int hrtimer_reprogram(struct hrtimer *timer,
583 struct hrtimer_clock_base *base)
584{
585 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700586 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800587 int res;
588
Arjan van de Vencc584b22008-09-01 15:02:30 -0700589 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100590
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800591 /*
592 * When the callback is running, we do not reprogram the clock event
593 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200594 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800595 * reprogramming is handled either by the softirq, which called the
596 * callback or at the end of the hrtimer_interrupt.
597 */
598 if (hrtimer_callback_running(timer))
599 return 0;
600
Thomas Gleixner63070a72008-02-14 00:58:36 +0100601 /*
602 * CLOCK_REALTIME timer might be requested with an absolute
603 * expiry time which is less than base->offset. Nothing wrong
604 * about that, just avoid to call into the tick code, which
605 * has now objections against negative expiry values.
606 */
607 if (expires.tv64 < 0)
608 return -ETIME;
609
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800610 if (expires.tv64 >= expires_next->tv64)
611 return 0;
612
613 /*
614 * Clockevents returns -ETIME, when the event was in the past.
615 */
616 res = tick_program_event(expires, 0);
617 if (!IS_ERR_VALUE(res))
618 *expires_next = expires;
619 return res;
620}
621
622
623/*
624 * Retrigger next event is called after clock was set
625 *
626 * Called with interrupts disabled via on_each_cpu()
627 */
628static void retrigger_next_event(void *arg)
629{
630 struct hrtimer_cpu_base *base;
631 struct timespec realtime_offset;
632 unsigned long seq;
633
634 if (!hrtimer_hres_active())
635 return;
636
637 do {
638 seq = read_seqbegin(&xtime_lock);
639 set_normalized_timespec(&realtime_offset,
640 -wall_to_monotonic.tv_sec,
641 -wall_to_monotonic.tv_nsec);
642 } while (read_seqretry(&xtime_lock, seq));
643
644 base = &__get_cpu_var(hrtimer_bases);
645
646 /* Adjust CLOCK_REALTIME offset */
647 spin_lock(&base->lock);
648 base->clock_base[CLOCK_REALTIME].offset =
649 timespec_to_ktime(realtime_offset);
650
651 hrtimer_force_reprogram(base);
652 spin_unlock(&base->lock);
653}
654
655/*
656 * Clock realtime was set
657 *
658 * Change the offset of the realtime clock vs. the monotonic
659 * clock.
660 *
661 * We might have to reprogram the high resolution timer interrupt. On
662 * SMP we call the architecture specific code to retrigger _all_ high
663 * resolution timer interrupts. On UP we just disable interrupts and
664 * call the high resolution interrupt code.
665 */
666void clock_was_set(void)
667{
668 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200669 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800670}
671
672/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200673 * During resume we might have to reprogram the high resolution timer
674 * interrupt (on the local CPU):
675 */
676void hres_timers_resume(void)
677{
Peter Zijlstra1d4a7f12009-01-18 16:39:29 +0100678 WARN_ONCE(!irqs_disabled(),
679 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
680
Ingo Molnar995f0542007-04-07 12:05:00 +0200681 retrigger_next_event(NULL);
682}
683
684/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800685 * Initialize the high resolution related parts of cpu_base
686 */
687static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
688{
689 base->expires_next.tv64 = KTIME_MAX;
690 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800691}
692
693/*
694 * Initialize the high resolution related parts of a hrtimer
695 */
696static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
697{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800698}
699
Peter Zijlstraca109492008-11-25 12:43:51 +0100700
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800701/*
702 * When High resolution timers are active, try to reprogram. Note, that in case
703 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
704 * check happens. The timer gets enqueued into the rbtree. The reprogramming
705 * and expiry check is done in the hrtimer_interrupt or in the softirq.
706 */
707static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100708 struct hrtimer_clock_base *base,
709 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800710{
711 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100712 if (wakeup) {
713 spin_unlock(&base->cpu_base->lock);
714 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
715 spin_lock(&base->cpu_base->lock);
716 } else
717 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
718
Peter Zijlstraca109492008-11-25 12:43:51 +0100719 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800720 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100721
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800722 return 0;
723}
724
725/*
726 * Switch to high resolution mode
727 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800728static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800729{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700730 int cpu = smp_processor_id();
731 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800732 unsigned long flags;
733
734 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800735 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800736
737 local_irq_save(flags);
738
739 if (tick_init_highres()) {
740 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700741 printk(KERN_WARNING "Could not switch to high resolution "
742 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800743 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800744 }
745 base->hres_active = 1;
746 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
747 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
748
749 tick_setup_sched_timer();
750
751 /* "Retrigger" the interrupt to get things going */
752 retrigger_next_event(NULL);
753 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100754 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800755 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800756 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800757}
758
759#else
760
761static inline int hrtimer_hres_active(void) { return 0; }
762static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800763static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800764static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
765static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100766 struct hrtimer_clock_base *base,
767 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800768{
769 return 0;
770}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800771static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
772static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
773
774#endif /* CONFIG_HIGH_RES_TIMERS */
775
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800776#ifdef CONFIG_TIMER_STATS
777void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
778{
779 if (timer->start_site)
780 return;
781
782 timer->start_site = addr;
783 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
784 timer->start_pid = current->pid;
785}
786#endif
787
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800788/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200789 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800790 */
791static inline
792void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
793{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800794 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800795}
796
797/**
798 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800799 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800800 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800801 * @interval: the interval to forward
802 *
803 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700804 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800805 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800806u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800807{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800808 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800809 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800810
Arjan van de Vencc584b22008-09-01 15:02:30 -0700811 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812
813 if (delta.tv64 < 0)
814 return 0;
815
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100816 if (interval.tv64 < timer->base->resolution.tv64)
817 interval.tv64 = timer->base->resolution.tv64;
818
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800819 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800820 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821
822 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700823 hrtimer_add_expires_ns(timer, incr * orun);
824 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800825 return orun;
826 /*
827 * This (and the ktime_add() below) is the
828 * correction for exact:
829 */
830 orun++;
831 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700832 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800833
834 return orun;
835}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700836EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800837
838/*
839 * enqueue_hrtimer - internal function to (re)start a timer
840 *
841 * The timer is inserted in expiry order. Insertion into the
842 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100843 *
844 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800845 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100846static int enqueue_hrtimer(struct hrtimer *timer,
847 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800848{
849 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800850 struct rb_node *parent = NULL;
851 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700852 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800853
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700854 debug_hrtimer_activate(timer);
855
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800856 /*
857 * Find the right place in the rbtree:
858 */
859 while (*link) {
860 parent = *link;
861 entry = rb_entry(parent, struct hrtimer, node);
862 /*
863 * We dont care about collisions. Nodes with
864 * the same expiry time stay together.
865 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700866 if (hrtimer_get_expires_tv64(timer) <
867 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800868 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700869 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800870 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700871 leftmost = 0;
872 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800873 }
874
875 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100876 * Insert the timer to the rbtree and check whether it
877 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800878 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100879 if (leftmost)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800880 base->first = &timer->node;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800881
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800882 rb_link_node(&timer->node, parent, link);
883 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800884 /*
885 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
886 * state of a possibly running callback.
887 */
888 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100889
890 return leftmost;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100891}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800892
893/*
894 * __remove_hrtimer - internal function to remove a timer
895 *
896 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800897 *
898 * High resolution timer mode reprograms the clock event device when the
899 * timer is the one which expires next. The caller can disable this by setting
900 * reprogram to zero. This is useful, when the context does a reprogramming
901 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800902 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800903static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800904 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800905 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800906{
Peter Zijlstraca109492008-11-25 12:43:51 +0100907 if (timer->state & HRTIMER_STATE_ENQUEUED) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800908 /*
909 * Remove the timer from the rbtree and replace the
910 * first entry pointer if necessary.
911 */
912 if (base->first == &timer->node) {
913 base->first = rb_next(&timer->node);
914 /* Reprogram the clock event device. if enabled */
915 if (reprogram && hrtimer_hres_active())
916 hrtimer_force_reprogram(base->cpu_base);
917 }
918 rb_erase(&timer->node, &base->active);
919 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800920 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800921}
922
923/*
924 * remove hrtimer, called with base lock held
925 */
926static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800927remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800928{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800929 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800930 int reprogram;
931
932 /*
933 * Remove the timer and force reprogramming when high
934 * resolution mode is active and the timer is on the current
935 * CPU. If we remove a timer on another CPU, reprogramming is
936 * skipped. The interrupt event on this CPU is fired and
937 * reprogramming happens in the interrupt handler. This is a
938 * rare case and less expensive than a smp call.
939 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700940 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800941 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800942 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
943 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
944 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800945 return 1;
946 }
947 return 0;
948}
949
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100950int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
951 unsigned long delta_ns, const enum hrtimer_mode mode,
952 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800953{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800954 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800955 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100956 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800957
958 base = lock_hrtimer_base(timer, &flags);
959
960 /* Remove an active timer from the queue: */
961 ret = remove_hrtimer(timer, base);
962
963 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530964 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800965
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530966 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100967 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800968 /*
969 * CONFIG_TIME_LOW_RES is a temporary way for architectures
970 * to signal that they simply return xtime in
971 * do_gettimeoffset(). In this case we want to round up by
972 * resolution when starting a relative timer, to avoid short
973 * timeouts. This will go away with the GTOD framework.
974 */
975#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100976 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800977#endif
978 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700979
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700980 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800981
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800982 timer_stats_hrtimer_set_start_info(timer);
983
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100984 leftmost = enqueue_hrtimer(timer, new_base);
985
Ingo Molnar935c6312007-03-28 13:17:18 +0200986 /*
987 * Only allow reprogramming if the new base is on this CPU.
988 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100989 *
990 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200991 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100992 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100993 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800994
995 unlock_hrtimer_base(timer, &flags);
996
997 return ret;
998}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100999
1000/**
1001 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1002 * @timer: the timer to be added
1003 * @tim: expiry time
1004 * @delta_ns: "slack" range for the timer
1005 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1006 *
1007 * Returns:
1008 * 0 on success
1009 * 1 when the timer was active
1010 */
1011int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1012 unsigned long delta_ns, const enum hrtimer_mode mode)
1013{
1014 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1015}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001016EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1017
1018/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001019 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001020 * @timer: the timer to be added
1021 * @tim: expiry time
1022 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1023 *
1024 * Returns:
1025 * 0 on success
1026 * 1 when the timer was active
1027 */
1028int
1029hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1030{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001031 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001032}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001033EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001034
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001035
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001036/**
1037 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001038 * @timer: hrtimer to stop
1039 *
1040 * Returns:
1041 * 0 when the timer was not active
1042 * 1 when the timer was active
1043 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001044 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001045 */
1046int hrtimer_try_to_cancel(struct hrtimer *timer)
1047{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001048 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001049 unsigned long flags;
1050 int ret = -1;
1051
1052 base = lock_hrtimer_base(timer, &flags);
1053
Thomas Gleixner303e9672007-02-16 01:27:51 -08001054 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001055 ret = remove_hrtimer(timer, base);
1056
1057 unlock_hrtimer_base(timer, &flags);
1058
1059 return ret;
1060
1061}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001062EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001063
1064/**
1065 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001066 * @timer: the timer to be cancelled
1067 *
1068 * Returns:
1069 * 0 when the timer was not active
1070 * 1 when the timer was active
1071 */
1072int hrtimer_cancel(struct hrtimer *timer)
1073{
1074 for (;;) {
1075 int ret = hrtimer_try_to_cancel(timer);
1076
1077 if (ret >= 0)
1078 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001079 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001080 }
1081}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001082EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001083
1084/**
1085 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001086 * @timer: the timer to read
1087 */
1088ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1089{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001090 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001091 unsigned long flags;
1092 ktime_t rem;
1093
1094 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001095 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001096 unlock_hrtimer_base(timer, &flags);
1097
1098 return rem;
1099}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001100EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001101
Russell Kingee9c5782008-04-20 13:59:33 +01001102#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001103/**
1104 * hrtimer_get_next_event - get the time until next expiry event
1105 *
1106 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1107 * is pending.
1108 */
1109ktime_t hrtimer_get_next_event(void)
1110{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001111 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1112 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001113 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1114 unsigned long flags;
1115 int i;
1116
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001117 spin_lock_irqsave(&cpu_base->lock, flags);
1118
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001119 if (!hrtimer_hres_active()) {
1120 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1121 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001122
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001123 if (!base->first)
1124 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001125
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001126 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001127 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001128 delta = ktime_sub(delta, base->get_time());
1129 if (delta.tv64 < mindelta.tv64)
1130 mindelta.tv64 = delta.tv64;
1131 }
Tony Lindgren69239742006-03-06 15:42:45 -08001132 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001133
1134 spin_unlock_irqrestore(&cpu_base->lock, flags);
1135
Tony Lindgren69239742006-03-06 15:42:45 -08001136 if (mindelta.tv64 < 0)
1137 mindelta.tv64 = 0;
1138 return mindelta;
1139}
1140#endif
1141
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001142static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1143 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001144{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001145 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001146
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001147 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001148
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001149 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001150
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001151 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001152 clock_id = CLOCK_MONOTONIC;
1153
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001154 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001155 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001156 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001157
1158#ifdef CONFIG_TIMER_STATS
1159 timer->start_site = NULL;
1160 timer->start_pid = -1;
1161 memset(timer->start_comm, 0, TASK_COMM_LEN);
1162#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001163}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001164
1165/**
1166 * hrtimer_init - initialize a timer to the given clock
1167 * @timer: the timer to be initialized
1168 * @clock_id: the clock to be used
1169 * @mode: timer mode abs/rel
1170 */
1171void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1172 enum hrtimer_mode mode)
1173{
1174 debug_hrtimer_init(timer);
1175 __hrtimer_init(timer, clock_id, mode);
1176}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001177EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001178
1179/**
1180 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001181 * @which_clock: which clock to query
1182 * @tp: pointer to timespec variable to store the resolution
1183 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001184 * Store the resolution of the clock selected by @which_clock in the
1185 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001186 */
1187int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1188{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001189 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001190
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001191 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1192 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001193
1194 return 0;
1195}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001196EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001197
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001198static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001199{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001200 struct hrtimer_clock_base *base = timer->base;
1201 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1202 enum hrtimer_restart (*fn)(struct hrtimer *);
1203 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001204
Peter Zijlstraca109492008-11-25 12:43:51 +01001205 WARN_ON(!irqs_disabled());
1206
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001207 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001208 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1209 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001210 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001211
1212 /*
1213 * Because we run timers from hardirq context, there is no chance
1214 * they get migrated to another cpu, therefore its safe to unlock
1215 * the timer base.
1216 */
1217 spin_unlock(&cpu_base->lock);
1218 restart = fn(timer);
1219 spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001220
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001221 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001222 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1223 * we do not reprogramm the event hardware. Happens either in
1224 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001225 */
1226 if (restart != HRTIMER_NORESTART) {
1227 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001228 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001229 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001230 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001231}
1232
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001233#ifdef CONFIG_HIGH_RES_TIMERS
1234
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001235static int force_clock_reprogram;
1236
1237/*
1238 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1239 * is hanging, which could happen with something that slows the interrupt
1240 * such as the tracing. Then we force the clock reprogramming for each future
1241 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1242 * threshold that we will overwrite.
1243 * The next tick event will be scheduled to 3 times we currently spend on
1244 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1245 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1246 * let it running without serious starvation.
1247 */
1248
1249static inline void
1250hrtimer_interrupt_hanging(struct clock_event_device *dev,
1251 ktime_t try_time)
1252{
1253 force_clock_reprogram = 1;
1254 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1255 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1256 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1257}
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001258/*
1259 * High resolution timer interrupt
1260 * Called with interrupts disabled
1261 */
1262void hrtimer_interrupt(struct clock_event_device *dev)
1263{
1264 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1265 struct hrtimer_clock_base *base;
1266 ktime_t expires_next, now;
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001267 int nr_retries = 0;
Peter Zijlstraca109492008-11-25 12:43:51 +01001268 int i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001269
1270 BUG_ON(!cpu_base->hres_active);
1271 cpu_base->nr_events++;
1272 dev->next_event.tv64 = KTIME_MAX;
1273
1274 retry:
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001275 /* 5 retries is enough to notice a hang */
1276 if (!(++nr_retries % 5))
1277 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1278
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001279 now = ktime_get();
1280
1281 expires_next.tv64 = KTIME_MAX;
1282
1283 base = cpu_base->clock_base;
1284
1285 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1286 ktime_t basenow;
1287 struct rb_node *node;
1288
1289 spin_lock(&cpu_base->lock);
1290
1291 basenow = ktime_add(now, base->offset);
1292
1293 while ((node = base->first)) {
1294 struct hrtimer *timer;
1295
1296 timer = rb_entry(node, struct hrtimer, node);
1297
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001298 /*
1299 * The immediate goal for using the softexpires is
1300 * minimizing wakeups, not running timers at the
1301 * earliest interrupt after their soft expiration.
1302 * This allows us to avoid using a Priority Search
1303 * Tree, which can answer a stabbing querry for
1304 * overlapping intervals and instead use the simple
1305 * BST we already have.
1306 * We don't add extra wakeups by delaying timers that
1307 * are right-of a not yet expired timer, because that
1308 * timer will have to trigger a wakeup anyway.
1309 */
1310
1311 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001312 ktime_t expires;
1313
Arjan van de Vencc584b22008-09-01 15:02:30 -07001314 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001315 base->offset);
1316 if (expires.tv64 < expires_next.tv64)
1317 expires_next = expires;
1318 break;
1319 }
1320
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001321 __run_hrtimer(timer);
1322 }
1323 spin_unlock(&cpu_base->lock);
1324 base++;
1325 }
1326
1327 cpu_base->expires_next = expires_next;
1328
1329 /* Reprogramming necessary ? */
1330 if (expires_next.tv64 != KTIME_MAX) {
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001331 if (tick_program_event(expires_next, force_clock_reprogram))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001332 goto retry;
1333 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001334}
1335
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001336/*
1337 * local version of hrtimer_peek_ahead_timers() called with interrupts
1338 * disabled.
1339 */
1340static void __hrtimer_peek_ahead_timers(void)
1341{
1342 struct tick_device *td;
1343
1344 if (!hrtimer_hres_active())
1345 return;
1346
1347 td = &__get_cpu_var(tick_cpu_device);
1348 if (td && td->evtdev)
1349 hrtimer_interrupt(td->evtdev);
1350}
1351
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001352/**
1353 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1354 *
1355 * hrtimer_peek_ahead_timers will peek at the timer queue of
1356 * the current cpu and check if there are any timers for which
1357 * the soft expires time has passed. If any such timers exist,
1358 * they are run immediately and then removed from the timer queue.
1359 *
1360 */
1361void hrtimer_peek_ahead_timers(void)
1362{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001363 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001364
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001365 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001366 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001367 local_irq_restore(flags);
1368}
1369
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001370static void run_hrtimer_softirq(struct softirq_action *h)
1371{
1372 hrtimer_peek_ahead_timers();
1373}
1374
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001375#else /* CONFIG_HIGH_RES_TIMERS */
1376
1377static inline void __hrtimer_peek_ahead_timers(void) { }
1378
1379#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001380
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001381/*
1382 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001383 *
1384 * For HRT its the fall back code to run the softirq in the timer
1385 * softirq context in case the hrtimer initialization failed or has
1386 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001387 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001388void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001389{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001390 if (hrtimer_hres_active())
1391 return;
1392
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001393 /*
1394 * This _is_ ugly: We have to check in the softirq context,
1395 * whether we can switch to highres and / or nohz mode. The
1396 * clocksource switch happens in the timer interrupt with
1397 * xtime_lock held. Notification from there only sets the
1398 * check bit in the tick_oneshot code, otherwise we might
1399 * deadlock vs. xtime_lock.
1400 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001401 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001402 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001403}
1404
1405/*
1406 * Called from hardirq context every jiffy
1407 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001408void hrtimer_run_queues(void)
1409{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001410 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001411 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001412 struct hrtimer_clock_base *base;
1413 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001414
1415 if (hrtimer_hres_active())
1416 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001417
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001418 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1419 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001420
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001421 if (!base->first)
1422 continue;
1423
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001424 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001425 hrtimer_get_softirq_time(cpu_base);
1426 gettime = 0;
1427 }
1428
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001429 spin_lock(&cpu_base->lock);
1430
1431 while ((node = base->first)) {
1432 struct hrtimer *timer;
1433
1434 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001435 if (base->softirq_time.tv64 <=
1436 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001437 break;
1438
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001439 __run_hrtimer(timer);
1440 }
1441 spin_unlock(&cpu_base->lock);
1442 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001443}
1444
1445/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001446 * Sleep related functions:
1447 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001448static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001449{
1450 struct hrtimer_sleeper *t =
1451 container_of(timer, struct hrtimer_sleeper, timer);
1452 struct task_struct *task = t->task;
1453
1454 t->task = NULL;
1455 if (task)
1456 wake_up_process(task);
1457
1458 return HRTIMER_NORESTART;
1459}
1460
Ingo Molnar36c8b582006-07-03 00:25:41 -07001461void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001462{
1463 sl->timer.function = hrtimer_wakeup;
1464 sl->task = task;
1465}
1466
Thomas Gleixner669d7862006-03-31 02:31:19 -08001467static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001468{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001469 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001470
Roman Zippel432569b2006-03-26 01:38:08 -08001471 do {
1472 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001473 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001474 if (!hrtimer_active(&t->timer))
1475 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001476
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001477 if (likely(t->task))
1478 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001479
Thomas Gleixner669d7862006-03-31 02:31:19 -08001480 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001481 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001482
Thomas Gleixner669d7862006-03-31 02:31:19 -08001483 } while (t->task && !signal_pending(current));
1484
Peter Zijlstra3588a082008-02-01 17:45:13 +01001485 __set_current_state(TASK_RUNNING);
1486
Thomas Gleixner669d7862006-03-31 02:31:19 -08001487 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001488}
1489
Oleg Nesterov080344b2008-02-01 17:29:05 +03001490static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1491{
1492 struct timespec rmt;
1493 ktime_t rem;
1494
Arjan van de Vencc584b22008-09-01 15:02:30 -07001495 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001496 if (rem.tv64 <= 0)
1497 return 0;
1498 rmt = ktime_to_timespec(rem);
1499
1500 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1501 return -EFAULT;
1502
1503 return 1;
1504}
1505
Toyo Abe1711ef32006-09-29 02:00:28 -07001506long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001507{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001508 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001509 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001510 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001511
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001512 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1513 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001514 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001515
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001516 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001517 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001518
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001519 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001520 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001521 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001522 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001523 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001524 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001525
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001526 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001527 ret = -ERESTART_RESTARTBLOCK;
1528out:
1529 destroy_hrtimer_on_stack(&t.timer);
1530 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001531}
1532
Oleg Nesterov080344b2008-02-01 17:29:05 +03001533long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001534 const enum hrtimer_mode mode, const clockid_t clockid)
1535{
1536 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001537 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001538 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001539 unsigned long slack;
1540
1541 slack = current->timer_slack_ns;
1542 if (rt_task(current))
1543 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001544
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001545 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001546 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001547 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001548 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001549
George Anzinger7978672c2006-02-01 03:05:11 -08001550 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001551 if (mode == HRTIMER_MODE_ABS) {
1552 ret = -ERESTARTNOHAND;
1553 goto out;
1554 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001555
Roman Zippel432569b2006-03-26 01:38:08 -08001556 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001557 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001558 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001559 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001560 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001561
1562 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001563 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001564 restart->nanosleep.index = t.timer.base->index;
1565 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001566 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001567
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001568 ret = -ERESTART_RESTARTBLOCK;
1569out:
1570 destroy_hrtimer_on_stack(&t.timer);
1571 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001572}
1573
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001574SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1575 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001576{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001577 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001578
1579 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1580 return -EFAULT;
1581
1582 if (!timespec_valid(&tu))
1583 return -EINVAL;
1584
Oleg Nesterov080344b2008-02-01 17:29:05 +03001585 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001586}
1587
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001588/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001589 * Functions related to boot-time initialization:
1590 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001591static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001592{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001593 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001594 int i;
1595
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001596 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001597
1598 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1599 cpu_base->clock_base[i].cpu_base = cpu_base;
1600
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001601 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001602}
1603
1604#ifdef CONFIG_HOTPLUG_CPU
1605
Peter Zijlstraca109492008-11-25 12:43:51 +01001606static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001607 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001608{
1609 struct hrtimer *timer;
1610 struct rb_node *node;
1611
1612 while ((node = rb_first(&old_base->active))) {
1613 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001614 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001615 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001616
1617 /*
1618 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1619 * timer could be seen as !active and just vanish away
1620 * under us on another CPU
1621 */
1622 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001623 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001624 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001625 * Enqueue the timers on the new cpu. This does not
1626 * reprogram the event device in case the timer
1627 * expires before the earliest on this CPU, but we run
1628 * hrtimer_interrupt after we migrated everything to
1629 * sort out already expired timers and reprogram the
1630 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001631 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001632 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001633
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001634 /* Clear the migration state bit */
1635 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001636 }
1637}
1638
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001639static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001640{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001641 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001642 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001643
Peter Zijlstra37810652008-12-04 11:17:10 +01001644 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001645 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001646
1647 local_irq_disable();
1648 old_base = &per_cpu(hrtimer_bases, scpu);
1649 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001650 /*
1651 * The caller is globally serialized and nobody else
1652 * takes two locks at once, deadlock is not possible.
1653 */
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001654 spin_lock(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001655 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001656
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001657 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001658 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001659 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001660 }
1661
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001662 spin_unlock(&old_base->lock);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001663 spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001664
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001665 /* Check, if we got expired work to do */
1666 __hrtimer_peek_ahead_timers();
1667 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001668}
1669
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001670#endif /* CONFIG_HOTPLUG_CPU */
1671
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001672static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001673 unsigned long action, void *hcpu)
1674{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001675 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001676
1677 switch (action) {
1678
1679 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001680 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001681 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001682 break;
1683
1684#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001685 case CPU_DYING:
1686 case CPU_DYING_FROZEN:
1687 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1688 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001689 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001690 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001691 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001692 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001693 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001694 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001695 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001696#endif
1697
1698 default:
1699 break;
1700 }
1701
1702 return NOTIFY_OK;
1703}
1704
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001705static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001706 .notifier_call = hrtimer_cpu_notify,
1707};
1708
1709void __init hrtimers_init(void)
1710{
1711 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1712 (void *)(long)smp_processor_id());
1713 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001714#ifdef CONFIG_HIGH_RES_TIMERS
1715 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1716#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001717}
1718
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001719/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001720 * schedule_hrtimeout_range - sleep until timeout
1721 * @expires: timeout value (ktime_t)
1722 * @delta: slack in expires timeout (ktime_t)
1723 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1724 *
1725 * Make the current task sleep until the given expiry time has
1726 * elapsed. The routine will return immediately unless
1727 * the current task state has been set (see set_current_state()).
1728 *
1729 * The @delta argument gives the kernel the freedom to schedule the
1730 * actual wakeup to a time that is both power and performance friendly.
1731 * The kernel give the normal best effort behavior for "@expires+@delta",
1732 * but may decide to fire the timer earlier, but no earlier than @expires.
1733 *
1734 * You can set the task state as follows -
1735 *
1736 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1737 * pass before the routine returns.
1738 *
1739 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1740 * delivered to the current task.
1741 *
1742 * The current task state is guaranteed to be TASK_RUNNING when this
1743 * routine returns.
1744 *
1745 * Returns 0 when the timer has expired otherwise -EINTR
1746 */
1747int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1748 const enum hrtimer_mode mode)
1749{
1750 struct hrtimer_sleeper t;
1751
1752 /*
1753 * Optimize when a zero timeout value is given. It does not
1754 * matter whether this is an absolute or a relative time.
1755 */
1756 if (expires && !expires->tv64) {
1757 __set_current_state(TASK_RUNNING);
1758 return 0;
1759 }
1760
1761 /*
1762 * A NULL parameter means "inifinte"
1763 */
1764 if (!expires) {
1765 schedule();
1766 __set_current_state(TASK_RUNNING);
1767 return -EINTR;
1768 }
1769
1770 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1771 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1772
1773 hrtimer_init_sleeper(&t, current);
1774
1775 hrtimer_start_expires(&t.timer, mode);
1776 if (!hrtimer_active(&t.timer))
1777 t.task = NULL;
1778
1779 if (likely(t.task))
1780 schedule();
1781
1782 hrtimer_cancel(&t.timer);
1783 destroy_hrtimer_on_stack(&t.timer);
1784
1785 __set_current_state(TASK_RUNNING);
1786
1787 return !t.task ? 0 : -EINTR;
1788}
1789EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1790
1791/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001792 * schedule_hrtimeout - sleep until timeout
1793 * @expires: timeout value (ktime_t)
1794 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1795 *
1796 * Make the current task sleep until the given expiry time has
1797 * elapsed. The routine will return immediately unless
1798 * the current task state has been set (see set_current_state()).
1799 *
1800 * You can set the task state as follows -
1801 *
1802 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1803 * pass before the routine returns.
1804 *
1805 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1806 * delivered to the current task.
1807 *
1808 * The current task state is guaranteed to be TASK_RUNNING when this
1809 * routine returns.
1810 *
1811 * Returns 0 when the timer has expired otherwise -EINTR
1812 */
1813int __sched schedule_hrtimeout(ktime_t *expires,
1814 const enum hrtimer_mode mode)
1815{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001816 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001817}
1818EXPORT_SYMBOL_GPL(schedule_hrtimeout);