blob: 9002958a96e70ef0f8acc70ceeccac5327efae9e [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
51/**
52 * ktime_get - get the monotonic time in ktime_t format
53 *
54 * returns the time in ktime_t format
55 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080056ktime_t ktime_get(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080057{
58 struct timespec now;
59
60 ktime_get_ts(&now);
61
62 return timespec_to_ktime(now);
63}
Patrick McHardy641b9e02007-03-16 01:18:42 -070064EXPORT_SYMBOL_GPL(ktime_get);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080065
66/**
67 * ktime_get_real - get the real (wall-) time in ktime_t format
68 *
69 * returns the time in ktime_t format
70 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080071ktime_t ktime_get_real(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080072{
73 struct timespec now;
74
75 getnstimeofday(&now);
76
77 return timespec_to_ktime(now);
78}
79
80EXPORT_SYMBOL_GPL(ktime_get_real);
81
82/*
83 * The timer bases:
George Anzinger7978672c2006-02-01 03:05:11 -080084 *
85 * Note: If we want to add new timer bases, we have to skip the two
86 * clock ids captured by the cpu-timers. We do this by holding empty
87 * entries rather than doing math adjustment of the clock ids.
88 * This ensures that we capture erroneous accesses to these clock ids
89 * rather than moving them into the range of valid clock id's.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080090 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080091DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080092{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080093
94 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080095 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080096 {
97 .index = CLOCK_REALTIME,
98 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080099 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800100 },
101 {
102 .index = CLOCK_MONOTONIC,
103 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800104 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800105 },
106 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800107};
108
109/**
110 * ktime_get_ts - get the monotonic clock in timespec format
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800111 * @ts: pointer to timespec variable
112 *
113 * The function calculates the monotonic clock from the realtime
114 * clock and the wall_to_monotonic offset and stores the result
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800115 * in normalized timespec format in the variable pointed to by @ts.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800116 */
117void ktime_get_ts(struct timespec *ts)
118{
119 struct timespec tomono;
120 unsigned long seq;
121
122 do {
123 seq = read_seqbegin(&xtime_lock);
124 getnstimeofday(ts);
125 tomono = wall_to_monotonic;
126
127 } while (read_seqretry(&xtime_lock, seq));
128
129 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
130 ts->tv_nsec + tomono.tv_nsec);
131}
Matt Helsley69778e32006-01-09 20:52:39 -0800132EXPORT_SYMBOL_GPL(ktime_get_ts);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800133
134/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800135 * Get the coarse grained time at the softirq based on xtime and
136 * wall_to_monotonic.
137 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800138static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800139{
140 ktime_t xtim, tomono;
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800141 struct timespec xts, tom;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800142 unsigned long seq;
143
144 do {
145 seq = read_seqbegin(&xtime_lock);
john stultz2c6b47d2007-07-24 17:47:43 -0700146 xts = current_kernel_time();
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800147 tom = wall_to_monotonic;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800148 } while (read_seqretry(&xtime_lock, seq));
149
john stultzf4304ab2007-02-16 01:27:26 -0800150 xtim = timespec_to_ktime(xts);
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800151 tomono = timespec_to_ktime(tom);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800152 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
153 base->clock_base[CLOCK_MONOTONIC].softirq_time =
154 ktime_add(xtim, tomono);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800155}
156
157/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800158 * Functions and macros which are different for UP/SMP systems are kept in a
159 * single place
160 */
161#ifdef CONFIG_SMP
162
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800163/*
164 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
165 * means that all timers which are tied to this base via timer->base are
166 * locked, and the base itself is locked too.
167 *
168 * So __run_timers/migrate_timers can safely modify all timers which could
169 * be found on the lists/queues.
170 *
171 * When the timer's base is locked, and the timer removed from list, it is
172 * possible to set timer->base = NULL and drop the lock: the timer remains
173 * locked.
174 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800175static
176struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
177 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800178{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800179 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800180
181 for (;;) {
182 base = timer->base;
183 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800184 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800185 if (likely(base == timer->base))
186 return base;
187 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800188 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800189 }
190 cpu_relax();
191 }
192}
193
194/*
195 * Switch the timer base to the current CPU when possible.
196 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800197static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530198switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
199 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800200{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800201 struct hrtimer_clock_base *new_base;
202 struct hrtimer_cpu_base *new_cpu_base;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530203 int cpu, preferred_cpu = -1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800204
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530205 cpu = smp_processor_id();
206#if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP)
207 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu)) {
208 preferred_cpu = get_nohz_load_balancer();
209 if (preferred_cpu >= 0)
210 cpu = preferred_cpu;
211 }
212#endif
213
214again:
215 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800216 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800217
218 if (base != new_base) {
219 /*
220 * We are trying to schedule the timer on the local CPU.
221 * However we can't change timer's base while it is running,
222 * so we keep it on the same CPU. No hassle vs. reprogramming
223 * the event source in the high resolution case. The softirq
224 * code will take care of this when the timer function has
225 * completed. There is no conflict as we hold the lock until
226 * the timer is enqueued.
227 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800228 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800229 return base;
230
231 /* See the comment in lock_timer_base() */
232 timer->base = NULL;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800233 spin_unlock(&base->cpu_base->lock);
234 spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530235
236 /* Optimized away for NOHZ=n SMP=n */
237 if (cpu == preferred_cpu) {
238 /* Calculate clock monotonic expiry time */
239#ifdef CONFIG_HIGH_RES_TIMERS
240 ktime_t expires = ktime_sub(hrtimer_get_expires(timer),
241 new_base->offset);
242#else
243 ktime_t expires = hrtimer_get_expires(timer);
244#endif
245
246 /*
247 * Get the next event on target cpu from the
248 * clock events layer.
249 * This covers the highres=off nohz=on case as well.
250 */
251 ktime_t next = clockevents_get_next_event(cpu);
252
253 ktime_t delta = ktime_sub(expires, next);
254
255 /*
256 * We do not migrate the timer when it is expiring
257 * before the next event on the target cpu because
258 * we cannot reprogram the target cpu hardware and
259 * we would cause it to fire late.
260 */
261 if (delta.tv64 < 0) {
262 cpu = smp_processor_id();
263 spin_unlock(&new_base->cpu_base->lock);
264 spin_lock(&base->cpu_base->lock);
265 timer->base = base;
266 goto again;
267 }
268 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800269 timer->base = new_base;
270 }
271 return new_base;
272}
273
274#else /* CONFIG_SMP */
275
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800276static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800277lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
278{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800279 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800280
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800281 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800282
283 return base;
284}
285
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530286# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800287
288#endif /* !CONFIG_SMP */
289
290/*
291 * Functions for the union type storage format of ktime_t which are
292 * too large for inlining:
293 */
294#if BITS_PER_LONG < 64
295# ifndef CONFIG_KTIME_SCALAR
296/**
297 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800298 * @kt: addend
299 * @nsec: the scalar nsec value to add
300 *
301 * Returns the sum of kt and nsec in ktime_t format
302 */
303ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
304{
305 ktime_t tmp;
306
307 if (likely(nsec < NSEC_PER_SEC)) {
308 tmp.tv64 = nsec;
309 } else {
310 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
311
312 tmp = ktime_set((long)nsec, rem);
313 }
314
315 return ktime_add(kt, tmp);
316}
David Howellsb8b8fd22007-04-27 15:31:24 -0700317
318EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700319
320/**
321 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
322 * @kt: minuend
323 * @nsec: the scalar nsec value to subtract
324 *
325 * Returns the subtraction of @nsec from @kt in ktime_t format
326 */
327ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
328{
329 ktime_t tmp;
330
331 if (likely(nsec < NSEC_PER_SEC)) {
332 tmp.tv64 = nsec;
333 } else {
334 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
335
336 tmp = ktime_set((long)nsec, rem);
337 }
338
339 return ktime_sub(kt, tmp);
340}
341
342EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800343# endif /* !CONFIG_KTIME_SCALAR */
344
345/*
346 * Divide a ktime value by a nanosecond value
347 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800348u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800349{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300350 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800351 int sft = 0;
352
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300353 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800354 /* Make sure the divisor is less than 2^32: */
355 while (div >> 32) {
356 sft++;
357 div >>= 1;
358 }
359 dclc >>= sft;
360 do_div(dclc, (unsigned long) div);
361
Davide Libenzi4d672e72008-02-04 22:27:26 -0800362 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800363}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800364#endif /* BITS_PER_LONG >= 64 */
365
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100366/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100367 * Add two ktime values and do a safety check for overflow:
368 */
369ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
370{
371 ktime_t res = ktime_add(lhs, rhs);
372
373 /*
374 * We use KTIME_SEC_MAX here, the maximum timeout which we can
375 * return to user space in a timespec:
376 */
377 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
378 res = ktime_set(KTIME_SEC_MAX, 0);
379
380 return res;
381}
382
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300383EXPORT_SYMBOL_GPL(ktime_add_safe);
384
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700385#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
386
387static struct debug_obj_descr hrtimer_debug_descr;
388
389/*
390 * fixup_init is called when:
391 * - an active object is initialized
392 */
393static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
394{
395 struct hrtimer *timer = addr;
396
397 switch (state) {
398 case ODEBUG_STATE_ACTIVE:
399 hrtimer_cancel(timer);
400 debug_object_init(timer, &hrtimer_debug_descr);
401 return 1;
402 default:
403 return 0;
404 }
405}
406
407/*
408 * fixup_activate is called when:
409 * - an active object is activated
410 * - an unknown object is activated (might be a statically initialized object)
411 */
412static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
413{
414 switch (state) {
415
416 case ODEBUG_STATE_NOTAVAILABLE:
417 WARN_ON_ONCE(1);
418 return 0;
419
420 case ODEBUG_STATE_ACTIVE:
421 WARN_ON(1);
422
423 default:
424 return 0;
425 }
426}
427
428/*
429 * fixup_free is called when:
430 * - an active object is freed
431 */
432static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
433{
434 struct hrtimer *timer = addr;
435
436 switch (state) {
437 case ODEBUG_STATE_ACTIVE:
438 hrtimer_cancel(timer);
439 debug_object_free(timer, &hrtimer_debug_descr);
440 return 1;
441 default:
442 return 0;
443 }
444}
445
446static struct debug_obj_descr hrtimer_debug_descr = {
447 .name = "hrtimer",
448 .fixup_init = hrtimer_fixup_init,
449 .fixup_activate = hrtimer_fixup_activate,
450 .fixup_free = hrtimer_fixup_free,
451};
452
453static inline void debug_hrtimer_init(struct hrtimer *timer)
454{
455 debug_object_init(timer, &hrtimer_debug_descr);
456}
457
458static inline void debug_hrtimer_activate(struct hrtimer *timer)
459{
460 debug_object_activate(timer, &hrtimer_debug_descr);
461}
462
463static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
464{
465 debug_object_deactivate(timer, &hrtimer_debug_descr);
466}
467
468static inline void debug_hrtimer_free(struct hrtimer *timer)
469{
470 debug_object_free(timer, &hrtimer_debug_descr);
471}
472
473static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
474 enum hrtimer_mode mode);
475
476void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
477 enum hrtimer_mode mode)
478{
479 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
480 __hrtimer_init(timer, clock_id, mode);
481}
482
483void destroy_hrtimer_on_stack(struct hrtimer *timer)
484{
485 debug_object_free(timer, &hrtimer_debug_descr);
486}
487
488#else
489static inline void debug_hrtimer_init(struct hrtimer *timer) { }
490static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
491static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
492#endif
493
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800494/* High resolution timer related functions */
495#ifdef CONFIG_HIGH_RES_TIMERS
496
497/*
498 * High resolution timer enabled ?
499 */
500static int hrtimer_hres_enabled __read_mostly = 1;
501
502/*
503 * Enable / Disable high resolution mode
504 */
505static int __init setup_hrtimer_hres(char *str)
506{
507 if (!strcmp(str, "off"))
508 hrtimer_hres_enabled = 0;
509 else if (!strcmp(str, "on"))
510 hrtimer_hres_enabled = 1;
511 else
512 return 0;
513 return 1;
514}
515
516__setup("highres=", setup_hrtimer_hres);
517
518/*
519 * hrtimer_high_res_enabled - query, if the highres mode is enabled
520 */
521static inline int hrtimer_is_hres_enabled(void)
522{
523 return hrtimer_hres_enabled;
524}
525
526/*
527 * Is the high resolution mode active ?
528 */
529static inline int hrtimer_hres_active(void)
530{
531 return __get_cpu_var(hrtimer_bases).hres_active;
532}
533
534/*
535 * Reprogram the event source with checking both queues for the
536 * next event
537 * Called with interrupts disabled and base->lock held
538 */
539static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
540{
541 int i;
542 struct hrtimer_clock_base *base = cpu_base->clock_base;
543 ktime_t expires;
544
545 cpu_base->expires_next.tv64 = KTIME_MAX;
546
547 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
548 struct hrtimer *timer;
549
550 if (!base->first)
551 continue;
552 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700553 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100554 /*
555 * clock_was_set() has changed base->offset so the
556 * result might be negative. Fix it up to prevent a
557 * false positive in clockevents_program_event()
558 */
559 if (expires.tv64 < 0)
560 expires.tv64 = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800561 if (expires.tv64 < cpu_base->expires_next.tv64)
562 cpu_base->expires_next = expires;
563 }
564
565 if (cpu_base->expires_next.tv64 != KTIME_MAX)
566 tick_program_event(cpu_base->expires_next, 1);
567}
568
569/*
570 * Shared reprogramming for clock_realtime and clock_monotonic
571 *
572 * When a timer is enqueued and expires earlier than the already enqueued
573 * timers, we have to check, whether it expires earlier than the timer for
574 * which the clock event device was armed.
575 *
576 * Called with interrupts disabled and base->cpu_base.lock held
577 */
578static int hrtimer_reprogram(struct hrtimer *timer,
579 struct hrtimer_clock_base *base)
580{
581 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700582 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800583 int res;
584
Arjan van de Vencc584b22008-09-01 15:02:30 -0700585 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100586
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800587 /*
588 * When the callback is running, we do not reprogram the clock event
589 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200590 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800591 * reprogramming is handled either by the softirq, which called the
592 * callback or at the end of the hrtimer_interrupt.
593 */
594 if (hrtimer_callback_running(timer))
595 return 0;
596
Thomas Gleixner63070a72008-02-14 00:58:36 +0100597 /*
598 * CLOCK_REALTIME timer might be requested with an absolute
599 * expiry time which is less than base->offset. Nothing wrong
600 * about that, just avoid to call into the tick code, which
601 * has now objections against negative expiry values.
602 */
603 if (expires.tv64 < 0)
604 return -ETIME;
605
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800606 if (expires.tv64 >= expires_next->tv64)
607 return 0;
608
609 /*
610 * Clockevents returns -ETIME, when the event was in the past.
611 */
612 res = tick_program_event(expires, 0);
613 if (!IS_ERR_VALUE(res))
614 *expires_next = expires;
615 return res;
616}
617
618
619/*
620 * Retrigger next event is called after clock was set
621 *
622 * Called with interrupts disabled via on_each_cpu()
623 */
624static void retrigger_next_event(void *arg)
625{
626 struct hrtimer_cpu_base *base;
627 struct timespec realtime_offset;
628 unsigned long seq;
629
630 if (!hrtimer_hres_active())
631 return;
632
633 do {
634 seq = read_seqbegin(&xtime_lock);
635 set_normalized_timespec(&realtime_offset,
636 -wall_to_monotonic.tv_sec,
637 -wall_to_monotonic.tv_nsec);
638 } while (read_seqretry(&xtime_lock, seq));
639
640 base = &__get_cpu_var(hrtimer_bases);
641
642 /* Adjust CLOCK_REALTIME offset */
643 spin_lock(&base->lock);
644 base->clock_base[CLOCK_REALTIME].offset =
645 timespec_to_ktime(realtime_offset);
646
647 hrtimer_force_reprogram(base);
648 spin_unlock(&base->lock);
649}
650
651/*
652 * Clock realtime was set
653 *
654 * Change the offset of the realtime clock vs. the monotonic
655 * clock.
656 *
657 * We might have to reprogram the high resolution timer interrupt. On
658 * SMP we call the architecture specific code to retrigger _all_ high
659 * resolution timer interrupts. On UP we just disable interrupts and
660 * call the high resolution interrupt code.
661 */
662void clock_was_set(void)
663{
664 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200665 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800666}
667
668/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200669 * During resume we might have to reprogram the high resolution timer
670 * interrupt (on the local CPU):
671 */
672void hres_timers_resume(void)
673{
Peter Zijlstra1d4a7f12009-01-18 16:39:29 +0100674 WARN_ONCE(!irqs_disabled(),
675 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
676
Ingo Molnar995f0542007-04-07 12:05:00 +0200677 retrigger_next_event(NULL);
678}
679
680/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800681 * Initialize the high resolution related parts of cpu_base
682 */
683static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
684{
685 base->expires_next.tv64 = KTIME_MAX;
686 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800687}
688
689/*
690 * Initialize the high resolution related parts of a hrtimer
691 */
692static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
693{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800694}
695
Peter Zijlstraca109492008-11-25 12:43:51 +0100696
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800697/*
698 * When High resolution timers are active, try to reprogram. Note, that in case
699 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
700 * check happens. The timer gets enqueued into the rbtree. The reprogramming
701 * and expiry check is done in the hrtimer_interrupt or in the softirq.
702 */
703static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100704 struct hrtimer_clock_base *base,
705 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800706{
707 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100708 if (wakeup) {
709 spin_unlock(&base->cpu_base->lock);
710 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
711 spin_lock(&base->cpu_base->lock);
712 } else
713 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
714
Peter Zijlstraca109492008-11-25 12:43:51 +0100715 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800716 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100717
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800718 return 0;
719}
720
721/*
722 * Switch to high resolution mode
723 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800724static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800725{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700726 int cpu = smp_processor_id();
727 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800728 unsigned long flags;
729
730 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800731 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800732
733 local_irq_save(flags);
734
735 if (tick_init_highres()) {
736 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700737 printk(KERN_WARNING "Could not switch to high resolution "
738 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800739 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800740 }
741 base->hres_active = 1;
742 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
743 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
744
745 tick_setup_sched_timer();
746
747 /* "Retrigger" the interrupt to get things going */
748 retrigger_next_event(NULL);
749 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100750 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800751 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800752 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800753}
754
755#else
756
757static inline int hrtimer_hres_active(void) { return 0; }
758static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800759static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800760static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
761static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100762 struct hrtimer_clock_base *base,
763 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800764{
765 return 0;
766}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800767static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
768static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
769
770#endif /* CONFIG_HIGH_RES_TIMERS */
771
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800772#ifdef CONFIG_TIMER_STATS
773void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
774{
775 if (timer->start_site)
776 return;
777
778 timer->start_site = addr;
779 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
780 timer->start_pid = current->pid;
781}
782#endif
783
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800784/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200785 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800786 */
787static inline
788void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
789{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800790 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800791}
792
793/**
794 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800795 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800796 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800797 * @interval: the interval to forward
798 *
799 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700800 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800801 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800802u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800803{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800804 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800805 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800806
Arjan van de Vencc584b22008-09-01 15:02:30 -0700807 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800808
809 if (delta.tv64 < 0)
810 return 0;
811
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100812 if (interval.tv64 < timer->base->resolution.tv64)
813 interval.tv64 = timer->base->resolution.tv64;
814
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800815 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800816 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817
818 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700819 hrtimer_add_expires_ns(timer, incr * orun);
820 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821 return orun;
822 /*
823 * This (and the ktime_add() below) is the
824 * correction for exact:
825 */
826 orun++;
827 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700828 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800829
830 return orun;
831}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700832EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800833
834/*
835 * enqueue_hrtimer - internal function to (re)start a timer
836 *
837 * The timer is inserted in expiry order. Insertion into the
838 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100839 *
840 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800841 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100842static int enqueue_hrtimer(struct hrtimer *timer,
843 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800844{
845 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800846 struct rb_node *parent = NULL;
847 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700848 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800849
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700850 debug_hrtimer_activate(timer);
851
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852 /*
853 * Find the right place in the rbtree:
854 */
855 while (*link) {
856 parent = *link;
857 entry = rb_entry(parent, struct hrtimer, node);
858 /*
859 * We dont care about collisions. Nodes with
860 * the same expiry time stay together.
861 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700862 if (hrtimer_get_expires_tv64(timer) <
863 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800864 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700865 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800866 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700867 leftmost = 0;
868 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800869 }
870
871 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100872 * Insert the timer to the rbtree and check whether it
873 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800874 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100875 if (leftmost)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800876 base->first = &timer->node;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800877
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800878 rb_link_node(&timer->node, parent, link);
879 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800880 /*
881 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
882 * state of a possibly running callback.
883 */
884 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100885
886 return leftmost;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100887}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800888
889/*
890 * __remove_hrtimer - internal function to remove a timer
891 *
892 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800893 *
894 * High resolution timer mode reprograms the clock event device when the
895 * timer is the one which expires next. The caller can disable this by setting
896 * reprogram to zero. This is useful, when the context does a reprogramming
897 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800898 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800899static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800900 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800901 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800902{
Peter Zijlstraca109492008-11-25 12:43:51 +0100903 if (timer->state & HRTIMER_STATE_ENQUEUED) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800904 /*
905 * Remove the timer from the rbtree and replace the
906 * first entry pointer if necessary.
907 */
908 if (base->first == &timer->node) {
909 base->first = rb_next(&timer->node);
910 /* Reprogram the clock event device. if enabled */
911 if (reprogram && hrtimer_hres_active())
912 hrtimer_force_reprogram(base->cpu_base);
913 }
914 rb_erase(&timer->node, &base->active);
915 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800916 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800917}
918
919/*
920 * remove hrtimer, called with base lock held
921 */
922static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800923remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800924{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800925 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800926 int reprogram;
927
928 /*
929 * Remove the timer and force reprogramming when high
930 * resolution mode is active and the timer is on the current
931 * CPU. If we remove a timer on another CPU, reprogramming is
932 * skipped. The interrupt event on this CPU is fired and
933 * reprogramming happens in the interrupt handler. This is a
934 * rare case and less expensive than a smp call.
935 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700936 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800937 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800938 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
939 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
940 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800941 return 1;
942 }
943 return 0;
944}
945
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100946int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
947 unsigned long delta_ns, const enum hrtimer_mode mode,
948 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800949{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800950 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800951 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100952 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800953
954 base = lock_hrtimer_base(timer, &flags);
955
956 /* Remove an active timer from the queue: */
957 ret = remove_hrtimer(timer, base);
958
959 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530960 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800961
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530962 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100963 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800964 /*
965 * CONFIG_TIME_LOW_RES is a temporary way for architectures
966 * to signal that they simply return xtime in
967 * do_gettimeoffset(). In this case we want to round up by
968 * resolution when starting a relative timer, to avoid short
969 * timeouts. This will go away with the GTOD framework.
970 */
971#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100972 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800973#endif
974 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700975
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700976 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800977
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800978 timer_stats_hrtimer_set_start_info(timer);
979
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100980 leftmost = enqueue_hrtimer(timer, new_base);
981
Ingo Molnar935c6312007-03-28 13:17:18 +0200982 /*
983 * Only allow reprogramming if the new base is on this CPU.
984 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100985 *
986 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200987 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100988 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100989 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800990
991 unlock_hrtimer_base(timer, &flags);
992
993 return ret;
994}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100995
996/**
997 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
998 * @timer: the timer to be added
999 * @tim: expiry time
1000 * @delta_ns: "slack" range for the timer
1001 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1002 *
1003 * Returns:
1004 * 0 on success
1005 * 1 when the timer was active
1006 */
1007int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1008 unsigned long delta_ns, const enum hrtimer_mode mode)
1009{
1010 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1011}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001012EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1013
1014/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001015 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001016 * @timer: the timer to be added
1017 * @tim: expiry time
1018 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1019 *
1020 * Returns:
1021 * 0 on success
1022 * 1 when the timer was active
1023 */
1024int
1025hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1026{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001027 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001028}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001029EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001030
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001031
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001032/**
1033 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001034 * @timer: hrtimer to stop
1035 *
1036 * Returns:
1037 * 0 when the timer was not active
1038 * 1 when the timer was active
1039 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001040 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001041 */
1042int hrtimer_try_to_cancel(struct hrtimer *timer)
1043{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001044 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001045 unsigned long flags;
1046 int ret = -1;
1047
1048 base = lock_hrtimer_base(timer, &flags);
1049
Thomas Gleixner303e9672007-02-16 01:27:51 -08001050 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001051 ret = remove_hrtimer(timer, base);
1052
1053 unlock_hrtimer_base(timer, &flags);
1054
1055 return ret;
1056
1057}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001058EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001059
1060/**
1061 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001062 * @timer: the timer to be cancelled
1063 *
1064 * Returns:
1065 * 0 when the timer was not active
1066 * 1 when the timer was active
1067 */
1068int hrtimer_cancel(struct hrtimer *timer)
1069{
1070 for (;;) {
1071 int ret = hrtimer_try_to_cancel(timer);
1072
1073 if (ret >= 0)
1074 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001075 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001076 }
1077}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001078EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001079
1080/**
1081 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001082 * @timer: the timer to read
1083 */
1084ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1085{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001086 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001087 unsigned long flags;
1088 ktime_t rem;
1089
1090 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001091 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001092 unlock_hrtimer_base(timer, &flags);
1093
1094 return rem;
1095}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001096EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001097
Russell Kingee9c5782008-04-20 13:59:33 +01001098#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001099/**
1100 * hrtimer_get_next_event - get the time until next expiry event
1101 *
1102 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1103 * is pending.
1104 */
1105ktime_t hrtimer_get_next_event(void)
1106{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001107 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1108 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001109 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1110 unsigned long flags;
1111 int i;
1112
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001113 spin_lock_irqsave(&cpu_base->lock, flags);
1114
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001115 if (!hrtimer_hres_active()) {
1116 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1117 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001118
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001119 if (!base->first)
1120 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001121
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001122 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001123 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001124 delta = ktime_sub(delta, base->get_time());
1125 if (delta.tv64 < mindelta.tv64)
1126 mindelta.tv64 = delta.tv64;
1127 }
Tony Lindgren69239742006-03-06 15:42:45 -08001128 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001129
1130 spin_unlock_irqrestore(&cpu_base->lock, flags);
1131
Tony Lindgren69239742006-03-06 15:42:45 -08001132 if (mindelta.tv64 < 0)
1133 mindelta.tv64 = 0;
1134 return mindelta;
1135}
1136#endif
1137
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001138static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1139 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001140{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001141 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001142
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001143 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001144
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001145 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001146
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001147 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001148 clock_id = CLOCK_MONOTONIC;
1149
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001150 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001151 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001152 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001153
1154#ifdef CONFIG_TIMER_STATS
1155 timer->start_site = NULL;
1156 timer->start_pid = -1;
1157 memset(timer->start_comm, 0, TASK_COMM_LEN);
1158#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001159}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001160
1161/**
1162 * hrtimer_init - initialize a timer to the given clock
1163 * @timer: the timer to be initialized
1164 * @clock_id: the clock to be used
1165 * @mode: timer mode abs/rel
1166 */
1167void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1168 enum hrtimer_mode mode)
1169{
1170 debug_hrtimer_init(timer);
1171 __hrtimer_init(timer, clock_id, mode);
1172}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001173EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001174
1175/**
1176 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001177 * @which_clock: which clock to query
1178 * @tp: pointer to timespec variable to store the resolution
1179 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001180 * Store the resolution of the clock selected by @which_clock in the
1181 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001182 */
1183int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1184{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001185 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001186
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001187 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1188 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001189
1190 return 0;
1191}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001192EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001193
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001194static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001195{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001196 struct hrtimer_clock_base *base = timer->base;
1197 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1198 enum hrtimer_restart (*fn)(struct hrtimer *);
1199 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001200
Peter Zijlstraca109492008-11-25 12:43:51 +01001201 WARN_ON(!irqs_disabled());
1202
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001203 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001204 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1205 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001206 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001207
1208 /*
1209 * Because we run timers from hardirq context, there is no chance
1210 * they get migrated to another cpu, therefore its safe to unlock
1211 * the timer base.
1212 */
1213 spin_unlock(&cpu_base->lock);
1214 restart = fn(timer);
1215 spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001216
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001217 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001218 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1219 * we do not reprogramm the event hardware. Happens either in
1220 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001221 */
1222 if (restart != HRTIMER_NORESTART) {
1223 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001224 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001225 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001226 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001227}
1228
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001229#ifdef CONFIG_HIGH_RES_TIMERS
1230
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001231static int force_clock_reprogram;
1232
1233/*
1234 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1235 * is hanging, which could happen with something that slows the interrupt
1236 * such as the tracing. Then we force the clock reprogramming for each future
1237 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1238 * threshold that we will overwrite.
1239 * The next tick event will be scheduled to 3 times we currently spend on
1240 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1241 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1242 * let it running without serious starvation.
1243 */
1244
1245static inline void
1246hrtimer_interrupt_hanging(struct clock_event_device *dev,
1247 ktime_t try_time)
1248{
1249 force_clock_reprogram = 1;
1250 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1251 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1252 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1253}
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001254/*
1255 * High resolution timer interrupt
1256 * Called with interrupts disabled
1257 */
1258void hrtimer_interrupt(struct clock_event_device *dev)
1259{
1260 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1261 struct hrtimer_clock_base *base;
1262 ktime_t expires_next, now;
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001263 int nr_retries = 0;
Peter Zijlstraca109492008-11-25 12:43:51 +01001264 int i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001265
1266 BUG_ON(!cpu_base->hres_active);
1267 cpu_base->nr_events++;
1268 dev->next_event.tv64 = KTIME_MAX;
1269
1270 retry:
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001271 /* 5 retries is enough to notice a hang */
1272 if (!(++nr_retries % 5))
1273 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1274
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001275 now = ktime_get();
1276
1277 expires_next.tv64 = KTIME_MAX;
1278
1279 base = cpu_base->clock_base;
1280
1281 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1282 ktime_t basenow;
1283 struct rb_node *node;
1284
1285 spin_lock(&cpu_base->lock);
1286
1287 basenow = ktime_add(now, base->offset);
1288
1289 while ((node = base->first)) {
1290 struct hrtimer *timer;
1291
1292 timer = rb_entry(node, struct hrtimer, node);
1293
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001294 /*
1295 * The immediate goal for using the softexpires is
1296 * minimizing wakeups, not running timers at the
1297 * earliest interrupt after their soft expiration.
1298 * This allows us to avoid using a Priority Search
1299 * Tree, which can answer a stabbing querry for
1300 * overlapping intervals and instead use the simple
1301 * BST we already have.
1302 * We don't add extra wakeups by delaying timers that
1303 * are right-of a not yet expired timer, because that
1304 * timer will have to trigger a wakeup anyway.
1305 */
1306
1307 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001308 ktime_t expires;
1309
Arjan van de Vencc584b22008-09-01 15:02:30 -07001310 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001311 base->offset);
1312 if (expires.tv64 < expires_next.tv64)
1313 expires_next = expires;
1314 break;
1315 }
1316
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001317 __run_hrtimer(timer);
1318 }
1319 spin_unlock(&cpu_base->lock);
1320 base++;
1321 }
1322
1323 cpu_base->expires_next = expires_next;
1324
1325 /* Reprogramming necessary ? */
1326 if (expires_next.tv64 != KTIME_MAX) {
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001327 if (tick_program_event(expires_next, force_clock_reprogram))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001328 goto retry;
1329 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001330}
1331
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001332/*
1333 * local version of hrtimer_peek_ahead_timers() called with interrupts
1334 * disabled.
1335 */
1336static void __hrtimer_peek_ahead_timers(void)
1337{
1338 struct tick_device *td;
1339
1340 if (!hrtimer_hres_active())
1341 return;
1342
1343 td = &__get_cpu_var(tick_cpu_device);
1344 if (td && td->evtdev)
1345 hrtimer_interrupt(td->evtdev);
1346}
1347
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001348/**
1349 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1350 *
1351 * hrtimer_peek_ahead_timers will peek at the timer queue of
1352 * the current cpu and check if there are any timers for which
1353 * the soft expires time has passed. If any such timers exist,
1354 * they are run immediately and then removed from the timer queue.
1355 *
1356 */
1357void hrtimer_peek_ahead_timers(void)
1358{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001359 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001360
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001361 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001362 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001363 local_irq_restore(flags);
1364}
1365
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001366static void run_hrtimer_softirq(struct softirq_action *h)
1367{
1368 hrtimer_peek_ahead_timers();
1369}
1370
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001371#else /* CONFIG_HIGH_RES_TIMERS */
1372
1373static inline void __hrtimer_peek_ahead_timers(void) { }
1374
1375#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001376
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001377/*
1378 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001379 *
1380 * For HRT its the fall back code to run the softirq in the timer
1381 * softirq context in case the hrtimer initialization failed or has
1382 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001383 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001384void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001385{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001386 if (hrtimer_hres_active())
1387 return;
1388
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001389 /*
1390 * This _is_ ugly: We have to check in the softirq context,
1391 * whether we can switch to highres and / or nohz mode. The
1392 * clocksource switch happens in the timer interrupt with
1393 * xtime_lock held. Notification from there only sets the
1394 * check bit in the tick_oneshot code, otherwise we might
1395 * deadlock vs. xtime_lock.
1396 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001397 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001398 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001399}
1400
1401/*
1402 * Called from hardirq context every jiffy
1403 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001404void hrtimer_run_queues(void)
1405{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001406 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001407 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001408 struct hrtimer_clock_base *base;
1409 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001410
1411 if (hrtimer_hres_active())
1412 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001413
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001414 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1415 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001416
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001417 if (!base->first)
1418 continue;
1419
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001420 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001421 hrtimer_get_softirq_time(cpu_base);
1422 gettime = 0;
1423 }
1424
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001425 spin_lock(&cpu_base->lock);
1426
1427 while ((node = base->first)) {
1428 struct hrtimer *timer;
1429
1430 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001431 if (base->softirq_time.tv64 <=
1432 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001433 break;
1434
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001435 __run_hrtimer(timer);
1436 }
1437 spin_unlock(&cpu_base->lock);
1438 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001439}
1440
1441/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001442 * Sleep related functions:
1443 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001444static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001445{
1446 struct hrtimer_sleeper *t =
1447 container_of(timer, struct hrtimer_sleeper, timer);
1448 struct task_struct *task = t->task;
1449
1450 t->task = NULL;
1451 if (task)
1452 wake_up_process(task);
1453
1454 return HRTIMER_NORESTART;
1455}
1456
Ingo Molnar36c8b582006-07-03 00:25:41 -07001457void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001458{
1459 sl->timer.function = hrtimer_wakeup;
1460 sl->task = task;
1461}
1462
Thomas Gleixner669d7862006-03-31 02:31:19 -08001463static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001464{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001465 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001466
Roman Zippel432569b2006-03-26 01:38:08 -08001467 do {
1468 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001469 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001470 if (!hrtimer_active(&t->timer))
1471 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001472
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001473 if (likely(t->task))
1474 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001475
Thomas Gleixner669d7862006-03-31 02:31:19 -08001476 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001477 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001478
Thomas Gleixner669d7862006-03-31 02:31:19 -08001479 } while (t->task && !signal_pending(current));
1480
Peter Zijlstra3588a082008-02-01 17:45:13 +01001481 __set_current_state(TASK_RUNNING);
1482
Thomas Gleixner669d7862006-03-31 02:31:19 -08001483 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001484}
1485
Oleg Nesterov080344b2008-02-01 17:29:05 +03001486static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1487{
1488 struct timespec rmt;
1489 ktime_t rem;
1490
Arjan van de Vencc584b22008-09-01 15:02:30 -07001491 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001492 if (rem.tv64 <= 0)
1493 return 0;
1494 rmt = ktime_to_timespec(rem);
1495
1496 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1497 return -EFAULT;
1498
1499 return 1;
1500}
1501
Toyo Abe1711ef32006-09-29 02:00:28 -07001502long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001503{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001504 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001505 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001506 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001507
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001508 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1509 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001510 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001511
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001512 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001513 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001514
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001515 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001516 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001517 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001518 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001519 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001520 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001521
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001522 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001523 ret = -ERESTART_RESTARTBLOCK;
1524out:
1525 destroy_hrtimer_on_stack(&t.timer);
1526 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001527}
1528
Oleg Nesterov080344b2008-02-01 17:29:05 +03001529long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001530 const enum hrtimer_mode mode, const clockid_t clockid)
1531{
1532 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001533 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001534 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001535 unsigned long slack;
1536
1537 slack = current->timer_slack_ns;
1538 if (rt_task(current))
1539 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001540
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001541 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001542 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001543 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001544 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001545
George Anzinger7978672c2006-02-01 03:05:11 -08001546 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001547 if (mode == HRTIMER_MODE_ABS) {
1548 ret = -ERESTARTNOHAND;
1549 goto out;
1550 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001551
Roman Zippel432569b2006-03-26 01:38:08 -08001552 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001553 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001554 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001555 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001556 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001557
1558 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001559 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001560 restart->nanosleep.index = t.timer.base->index;
1561 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001562 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001563
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001564 ret = -ERESTART_RESTARTBLOCK;
1565out:
1566 destroy_hrtimer_on_stack(&t.timer);
1567 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001568}
1569
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001570SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1571 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001572{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001573 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001574
1575 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1576 return -EFAULT;
1577
1578 if (!timespec_valid(&tu))
1579 return -EINVAL;
1580
Oleg Nesterov080344b2008-02-01 17:29:05 +03001581 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001582}
1583
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001584/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001585 * Functions related to boot-time initialization:
1586 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001587static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001588{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001589 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001590 int i;
1591
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001592 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001593
1594 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1595 cpu_base->clock_base[i].cpu_base = cpu_base;
1596
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001597 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001598}
1599
1600#ifdef CONFIG_HOTPLUG_CPU
1601
Peter Zijlstraca109492008-11-25 12:43:51 +01001602static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001603 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001604{
1605 struct hrtimer *timer;
1606 struct rb_node *node;
1607
1608 while ((node = rb_first(&old_base->active))) {
1609 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001610 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001611 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001612
1613 /*
1614 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1615 * timer could be seen as !active and just vanish away
1616 * under us on another CPU
1617 */
1618 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001619 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001620 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001621 * Enqueue the timers on the new cpu. This does not
1622 * reprogram the event device in case the timer
1623 * expires before the earliest on this CPU, but we run
1624 * hrtimer_interrupt after we migrated everything to
1625 * sort out already expired timers and reprogram the
1626 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001627 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001628 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001629
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001630 /* Clear the migration state bit */
1631 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001632 }
1633}
1634
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001635static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001636{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001637 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001638 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001639
Peter Zijlstra37810652008-12-04 11:17:10 +01001640 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001641 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001642
1643 local_irq_disable();
1644 old_base = &per_cpu(hrtimer_bases, scpu);
1645 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001646 /*
1647 * The caller is globally serialized and nobody else
1648 * takes two locks at once, deadlock is not possible.
1649 */
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001650 spin_lock(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001651 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001652
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001653 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001654 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001655 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001656 }
1657
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001658 spin_unlock(&old_base->lock);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001659 spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001660
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001661 /* Check, if we got expired work to do */
1662 __hrtimer_peek_ahead_timers();
1663 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001664}
1665
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001666#endif /* CONFIG_HOTPLUG_CPU */
1667
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001668static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001669 unsigned long action, void *hcpu)
1670{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001671 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001672
1673 switch (action) {
1674
1675 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001676 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001677 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001678 break;
1679
1680#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001681 case CPU_DYING:
1682 case CPU_DYING_FROZEN:
1683 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1684 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001685 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001686 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001687 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001688 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001689 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001690 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001691 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001692#endif
1693
1694 default:
1695 break;
1696 }
1697
1698 return NOTIFY_OK;
1699}
1700
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001701static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001702 .notifier_call = hrtimer_cpu_notify,
1703};
1704
1705void __init hrtimers_init(void)
1706{
1707 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1708 (void *)(long)smp_processor_id());
1709 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001710#ifdef CONFIG_HIGH_RES_TIMERS
1711 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1712#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001713}
1714
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001715/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001716 * schedule_hrtimeout_range - sleep until timeout
1717 * @expires: timeout value (ktime_t)
1718 * @delta: slack in expires timeout (ktime_t)
1719 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1720 *
1721 * Make the current task sleep until the given expiry time has
1722 * elapsed. The routine will return immediately unless
1723 * the current task state has been set (see set_current_state()).
1724 *
1725 * The @delta argument gives the kernel the freedom to schedule the
1726 * actual wakeup to a time that is both power and performance friendly.
1727 * The kernel give the normal best effort behavior for "@expires+@delta",
1728 * but may decide to fire the timer earlier, but no earlier than @expires.
1729 *
1730 * You can set the task state as follows -
1731 *
1732 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1733 * pass before the routine returns.
1734 *
1735 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1736 * delivered to the current task.
1737 *
1738 * The current task state is guaranteed to be TASK_RUNNING when this
1739 * routine returns.
1740 *
1741 * Returns 0 when the timer has expired otherwise -EINTR
1742 */
1743int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1744 const enum hrtimer_mode mode)
1745{
1746 struct hrtimer_sleeper t;
1747
1748 /*
1749 * Optimize when a zero timeout value is given. It does not
1750 * matter whether this is an absolute or a relative time.
1751 */
1752 if (expires && !expires->tv64) {
1753 __set_current_state(TASK_RUNNING);
1754 return 0;
1755 }
1756
1757 /*
1758 * A NULL parameter means "inifinte"
1759 */
1760 if (!expires) {
1761 schedule();
1762 __set_current_state(TASK_RUNNING);
1763 return -EINTR;
1764 }
1765
1766 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1767 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1768
1769 hrtimer_init_sleeper(&t, current);
1770
1771 hrtimer_start_expires(&t.timer, mode);
1772 if (!hrtimer_active(&t.timer))
1773 t.task = NULL;
1774
1775 if (likely(t.task))
1776 schedule();
1777
1778 hrtimer_cancel(&t.timer);
1779 destroy_hrtimer_on_stack(&t.timer);
1780
1781 __set_current_state(TASK_RUNNING);
1782
1783 return !t.task ? 0 : -EINTR;
1784}
1785EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1786
1787/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001788 * schedule_hrtimeout - sleep until timeout
1789 * @expires: timeout value (ktime_t)
1790 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1791 *
1792 * Make the current task sleep until the given expiry time has
1793 * elapsed. The routine will return immediately unless
1794 * the current task state has been set (see set_current_state()).
1795 *
1796 * You can set the task state as follows -
1797 *
1798 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1799 * pass before the routine returns.
1800 *
1801 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1802 * delivered to the current task.
1803 *
1804 * The current task state is guaranteed to be TASK_RUNNING when this
1805 * routine returns.
1806 *
1807 * Returns 0 when the timer has expired otherwise -EINTR
1808 */
1809int __sched schedule_hrtimeout(ktime_t *expires,
1810 const enum hrtimer_mode mode)
1811{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001812 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001813}
1814EXPORT_SYMBOL_GPL(schedule_hrtimeout);