blob: eabcbd781433abdef455cf70adb3265095cde03d [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
Xiao Guangrongc6a2a172009-08-10 10:51:23 +080051#include <trace/events/timer.h>
52
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080053/*
54 * The timer bases:
George Anzinger79786722006-02-01 03:05:11 -080055 *
John Stultze06383d2010-12-14 19:37:07 -080056 * There are more clockids then hrtimer bases. Thus, we index
57 * into the timer bases by the hrtimer_base_type enum. When trying
58 * to reach a base using a clockid, hrtimer_clockid_to_base()
59 * is used to convert from clockid to the proper hrtimer_base_type.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080060 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080061DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080062{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080063
64 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080065 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080066 {
67 .index = CLOCK_REALTIME,
68 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080069 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080070 },
71 {
72 .index = CLOCK_MONOTONIC,
73 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080074 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080075 },
John Stultz70a08cc2011-02-15 10:45:16 -080076 {
77 .index = CLOCK_BOOTTIME,
78 .get_time = &ktime_get_boottime,
79 .resolution = KTIME_LOW_RES,
80 },
Thomas Gleixner99ee5312011-04-27 14:16:42 +020081 {
82 .index = CLOCK_REALTIME_COS,
83 .get_time = &ktime_get_real,
84 .resolution = KTIME_LOW_RES,
85 },
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080086 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080087};
88
Mike Frysinger942c3c52011-05-02 15:24:27 -040089static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
Thomas Gleixnerce313322011-04-29 00:02:00 +020090 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
91 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
92 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
Thomas Gleixner99ee5312011-04-27 14:16:42 +020093 [CLOCK_REALTIME_COS] = HRTIMER_BASE_REALTIME_COS,
Thomas Gleixnerce313322011-04-29 00:02:00 +020094};
John Stultze06383d2010-12-14 19:37:07 -080095
96static inline int hrtimer_clockid_to_base(clockid_t clock_id)
97{
98 return hrtimer_clock_to_base_table[clock_id];
99}
100
101
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800102/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800103 * Get the coarse grained time at the softirq based on xtime and
104 * wall_to_monotonic.
105 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800106static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800107{
John Stultz70a08cc2011-02-15 10:45:16 -0800108 ktime_t xtim, mono, boot;
John Stultz314ac372011-02-14 18:43:08 -0800109 struct timespec xts, tom, slp;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800110
John Stultz314ac372011-02-14 18:43:08 -0800111 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800112
john stultzf4304ab2007-02-16 01:27:26 -0800113 xtim = timespec_to_ktime(xts);
John Stultz70a08cc2011-02-15 10:45:16 -0800114 mono = ktime_add(xtim, timespec_to_ktime(tom));
115 boot = ktime_add(mono, timespec_to_ktime(slp));
John Stultze06383d2010-12-14 19:37:07 -0800116 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
John Stultz70a08cc2011-02-15 10:45:16 -0800117 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
118 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200119 base->clock_base[HRTIMER_BASE_REALTIME_COS].softirq_time = xtim;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800120}
121
122/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800123 * Functions and macros which are different for UP/SMP systems are kept in a
124 * single place
125 */
126#ifdef CONFIG_SMP
127
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800128/*
129 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
130 * means that all timers which are tied to this base via timer->base are
131 * locked, and the base itself is locked too.
132 *
133 * So __run_timers/migrate_timers can safely modify all timers which could
134 * be found on the lists/queues.
135 *
136 * When the timer's base is locked, and the timer removed from list, it is
137 * possible to set timer->base = NULL and drop the lock: the timer remains
138 * locked.
139 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800140static
141struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
142 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800143{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800144 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800145
146 for (;;) {
147 base = timer->base;
148 if (likely(base != NULL)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100149 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800150 if (likely(base == timer->base))
151 return base;
152 /* The timer has migrated to another CPU: */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100153 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800154 }
155 cpu_relax();
156 }
157}
158
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200159
160/*
161 * Get the preferred target CPU for NOHZ
162 */
163static int hrtimer_get_target(int this_cpu, int pinned)
164{
165#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700166 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
167 return get_nohz_timer_target();
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200168#endif
169 return this_cpu;
170}
171
172/*
173 * With HIGHRES=y we do not migrate the timer when it is expiring
174 * before the next event on the target cpu because we cannot reprogram
175 * the target cpu hardware and we would cause it to fire late.
176 *
177 * Called with cpu_base->lock of target cpu held.
178 */
179static int
180hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
181{
182#ifdef CONFIG_HIGH_RES_TIMERS
183 ktime_t expires;
184
185 if (!new_base->cpu_base->hres_active)
186 return 0;
187
188 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
189 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
190#else
191 return 0;
192#endif
193}
194
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800195/*
196 * Switch the timer base to the current CPU when possible.
197 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800198static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530199switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
200 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800201{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800202 struct hrtimer_clock_base *new_base;
203 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200204 int this_cpu = smp_processor_id();
205 int cpu = hrtimer_get_target(this_cpu, pinned);
John Stultze06383d2010-12-14 19:37:07 -0800206 int basenum = hrtimer_clockid_to_base(base->index);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530207
208again:
209 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
John Stultze06383d2010-12-14 19:37:07 -0800210 new_base = &new_cpu_base->clock_base[basenum];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800211
212 if (base != new_base) {
213 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200214 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800215 * However we can't change timer's base while it is running,
216 * so we keep it on the same CPU. No hassle vs. reprogramming
217 * the event source in the high resolution case. The softirq
218 * code will take care of this when the timer function has
219 * completed. There is no conflict as we hold the lock until
220 * the timer is enqueued.
221 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800222 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800223 return base;
224
225 /* See the comment in lock_timer_base() */
226 timer->base = NULL;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100227 raw_spin_unlock(&base->cpu_base->lock);
228 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530229
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200230 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
231 cpu = this_cpu;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100232 raw_spin_unlock(&new_base->cpu_base->lock);
233 raw_spin_lock(&base->cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200234 timer->base = base;
235 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530236 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800237 timer->base = new_base;
238 }
239 return new_base;
240}
241
242#else /* CONFIG_SMP */
243
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800244static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800245lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
246{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800247 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800248
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100249 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800250
251 return base;
252}
253
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530254# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800255
256#endif /* !CONFIG_SMP */
257
258/*
259 * Functions for the union type storage format of ktime_t which are
260 * too large for inlining:
261 */
262#if BITS_PER_LONG < 64
263# ifndef CONFIG_KTIME_SCALAR
264/**
265 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800266 * @kt: addend
267 * @nsec: the scalar nsec value to add
268 *
269 * Returns the sum of kt and nsec in ktime_t format
270 */
271ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
272{
273 ktime_t tmp;
274
275 if (likely(nsec < NSEC_PER_SEC)) {
276 tmp.tv64 = nsec;
277 } else {
278 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
279
280 tmp = ktime_set((long)nsec, rem);
281 }
282
283 return ktime_add(kt, tmp);
284}
David Howellsb8b8fd22007-04-27 15:31:24 -0700285
286EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700287
288/**
289 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
290 * @kt: minuend
291 * @nsec: the scalar nsec value to subtract
292 *
293 * Returns the subtraction of @nsec from @kt in ktime_t format
294 */
295ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
296{
297 ktime_t tmp;
298
299 if (likely(nsec < NSEC_PER_SEC)) {
300 tmp.tv64 = nsec;
301 } else {
302 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
303
304 tmp = ktime_set((long)nsec, rem);
305 }
306
307 return ktime_sub(kt, tmp);
308}
309
310EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800311# endif /* !CONFIG_KTIME_SCALAR */
312
313/*
314 * Divide a ktime value by a nanosecond value
315 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800316u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800317{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300318 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800319 int sft = 0;
320
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300321 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800322 /* Make sure the divisor is less than 2^32: */
323 while (div >> 32) {
324 sft++;
325 div >>= 1;
326 }
327 dclc >>= sft;
328 do_div(dclc, (unsigned long) div);
329
Davide Libenzi4d672e72008-02-04 22:27:26 -0800330 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800331}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800332#endif /* BITS_PER_LONG >= 64 */
333
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100334/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100335 * Add two ktime values and do a safety check for overflow:
336 */
337ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
338{
339 ktime_t res = ktime_add(lhs, rhs);
340
341 /*
342 * We use KTIME_SEC_MAX here, the maximum timeout which we can
343 * return to user space in a timespec:
344 */
345 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
346 res = ktime_set(KTIME_SEC_MAX, 0);
347
348 return res;
349}
350
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300351EXPORT_SYMBOL_GPL(ktime_add_safe);
352
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700353#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
354
355static struct debug_obj_descr hrtimer_debug_descr;
356
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100357static void *hrtimer_debug_hint(void *addr)
358{
359 return ((struct hrtimer *) addr)->function;
360}
361
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700362/*
363 * fixup_init is called when:
364 * - an active object is initialized
365 */
366static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
367{
368 struct hrtimer *timer = addr;
369
370 switch (state) {
371 case ODEBUG_STATE_ACTIVE:
372 hrtimer_cancel(timer);
373 debug_object_init(timer, &hrtimer_debug_descr);
374 return 1;
375 default:
376 return 0;
377 }
378}
379
380/*
381 * fixup_activate is called when:
382 * - an active object is activated
383 * - an unknown object is activated (might be a statically initialized object)
384 */
385static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
386{
387 switch (state) {
388
389 case ODEBUG_STATE_NOTAVAILABLE:
390 WARN_ON_ONCE(1);
391 return 0;
392
393 case ODEBUG_STATE_ACTIVE:
394 WARN_ON(1);
395
396 default:
397 return 0;
398 }
399}
400
401/*
402 * fixup_free is called when:
403 * - an active object is freed
404 */
405static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
406{
407 struct hrtimer *timer = addr;
408
409 switch (state) {
410 case ODEBUG_STATE_ACTIVE:
411 hrtimer_cancel(timer);
412 debug_object_free(timer, &hrtimer_debug_descr);
413 return 1;
414 default:
415 return 0;
416 }
417}
418
419static struct debug_obj_descr hrtimer_debug_descr = {
420 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100421 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700422 .fixup_init = hrtimer_fixup_init,
423 .fixup_activate = hrtimer_fixup_activate,
424 .fixup_free = hrtimer_fixup_free,
425};
426
427static inline void debug_hrtimer_init(struct hrtimer *timer)
428{
429 debug_object_init(timer, &hrtimer_debug_descr);
430}
431
432static inline void debug_hrtimer_activate(struct hrtimer *timer)
433{
434 debug_object_activate(timer, &hrtimer_debug_descr);
435}
436
437static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
438{
439 debug_object_deactivate(timer, &hrtimer_debug_descr);
440}
441
442static inline void debug_hrtimer_free(struct hrtimer *timer)
443{
444 debug_object_free(timer, &hrtimer_debug_descr);
445}
446
447static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
448 enum hrtimer_mode mode);
449
450void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
451 enum hrtimer_mode mode)
452{
453 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
454 __hrtimer_init(timer, clock_id, mode);
455}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700456EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700457
458void destroy_hrtimer_on_stack(struct hrtimer *timer)
459{
460 debug_object_free(timer, &hrtimer_debug_descr);
461}
462
463#else
464static inline void debug_hrtimer_init(struct hrtimer *timer) { }
465static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
466static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
467#endif
468
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800469static inline void
470debug_init(struct hrtimer *timer, clockid_t clockid,
471 enum hrtimer_mode mode)
472{
473 debug_hrtimer_init(timer);
474 trace_hrtimer_init(timer, clockid, mode);
475}
476
477static inline void debug_activate(struct hrtimer *timer)
478{
479 debug_hrtimer_activate(timer);
480 trace_hrtimer_start(timer);
481}
482
483static inline void debug_deactivate(struct hrtimer *timer)
484{
485 debug_hrtimer_deactivate(timer);
486 trace_hrtimer_cancel(timer);
487}
488
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200489static void hrtimer_expire_cancelable(struct hrtimer_cpu_base *cpu_base);
490
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800491/* High resolution timer related functions */
492#ifdef CONFIG_HIGH_RES_TIMERS
493
494/*
495 * High resolution timer enabled ?
496 */
497static int hrtimer_hres_enabled __read_mostly = 1;
498
499/*
500 * Enable / Disable high resolution mode
501 */
502static int __init setup_hrtimer_hres(char *str)
503{
504 if (!strcmp(str, "off"))
505 hrtimer_hres_enabled = 0;
506 else if (!strcmp(str, "on"))
507 hrtimer_hres_enabled = 1;
508 else
509 return 0;
510 return 1;
511}
512
513__setup("highres=", setup_hrtimer_hres);
514
515/*
516 * hrtimer_high_res_enabled - query, if the highres mode is enabled
517 */
518static inline int hrtimer_is_hres_enabled(void)
519{
520 return hrtimer_hres_enabled;
521}
522
523/*
524 * Is the high resolution mode active ?
525 */
526static inline int hrtimer_hres_active(void)
527{
Christoph Lameter909ea962010-12-08 16:22:55 +0100528 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800529}
530
531/*
532 * Reprogram the event source with checking both queues for the
533 * next event
534 * Called with interrupts disabled and base->lock held
535 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400536static void
537hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800538{
539 int i;
540 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400541 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800542
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400543 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800544
545 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
546 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700547 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800548
John Stultz998adc32010-09-20 19:19:17 -0700549 next = timerqueue_getnext(&base->active);
550 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800551 continue;
John Stultz998adc32010-09-20 19:19:17 -0700552 timer = container_of(next, struct hrtimer, node);
553
Arjan van de Vencc584b22008-09-01 15:02:30 -0700554 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100555 /*
556 * clock_was_set() has changed base->offset so the
557 * result might be negative. Fix it up to prevent a
558 * false positive in clockevents_program_event()
559 */
560 if (expires.tv64 < 0)
561 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400562 if (expires.tv64 < expires_next.tv64)
563 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800564 }
565
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400566 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
567 return;
568
569 cpu_base->expires_next.tv64 = expires_next.tv64;
570
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800571 if (cpu_base->expires_next.tv64 != KTIME_MAX)
572 tick_program_event(cpu_base->expires_next, 1);
573}
574
575/*
576 * Shared reprogramming for clock_realtime and clock_monotonic
577 *
578 * When a timer is enqueued and expires earlier than the already enqueued
579 * timers, we have to check, whether it expires earlier than the timer for
580 * which the clock event device was armed.
581 *
582 * Called with interrupts disabled and base->cpu_base.lock held
583 */
584static int hrtimer_reprogram(struct hrtimer *timer,
585 struct hrtimer_clock_base *base)
586{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100587 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700588 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800589 int res;
590
Arjan van de Vencc584b22008-09-01 15:02:30 -0700591 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100592
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800593 /*
594 * When the callback is running, we do not reprogram the clock event
595 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200596 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800597 * reprogramming is handled either by the softirq, which called the
598 * callback or at the end of the hrtimer_interrupt.
599 */
600 if (hrtimer_callback_running(timer))
601 return 0;
602
Thomas Gleixner63070a72008-02-14 00:58:36 +0100603 /*
604 * CLOCK_REALTIME timer might be requested with an absolute
605 * expiry time which is less than base->offset. Nothing wrong
606 * about that, just avoid to call into the tick code, which
607 * has now objections against negative expiry values.
608 */
609 if (expires.tv64 < 0)
610 return -ETIME;
611
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100612 if (expires.tv64 >= cpu_base->expires_next.tv64)
613 return 0;
614
615 /*
616 * If a hang was detected in the last timer interrupt then we
617 * do not schedule a timer which is earlier than the expiry
618 * which we enforced in the hang detection. We want the system
619 * to make progress.
620 */
621 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800622 return 0;
623
624 /*
625 * Clockevents returns -ETIME, when the event was in the past.
626 */
627 res = tick_program_event(expires, 0);
628 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100629 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800630 return res;
631}
632
Ingo Molnar995f0542007-04-07 12:05:00 +0200633/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800634 * Initialize the high resolution related parts of cpu_base
635 */
636static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
637{
638 base->expires_next.tv64 = KTIME_MAX;
639 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800640}
641
642/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800643 * When High resolution timers are active, try to reprogram. Note, that in case
644 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
645 * check happens. The timer gets enqueued into the rbtree. The reprogramming
646 * and expiry check is done in the hrtimer_interrupt or in the softirq.
647 */
648static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100649 struct hrtimer_clock_base *base,
650 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800651{
652 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100653 if (wakeup) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100654 raw_spin_unlock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100655 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100656 raw_spin_lock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100657 } else
658 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
659
Peter Zijlstraca109492008-11-25 12:43:51 +0100660 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800661 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100662
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800663 return 0;
664}
665
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200666static void retrigger_next_event(void *arg);
667
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800668/*
669 * Switch to high resolution mode
670 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800671static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800672{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200673 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700674 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800675 unsigned long flags;
676
677 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800678 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800679
680 local_irq_save(flags);
681
682 if (tick_init_highres()) {
683 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700684 printk(KERN_WARNING "Could not switch to high resolution "
685 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800686 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800687 }
688 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200689 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
690 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800691
692 tick_setup_sched_timer();
693
694 /* "Retrigger" the interrupt to get things going */
695 retrigger_next_event(NULL);
696 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800697 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800698}
699
700#else
701
702static inline int hrtimer_hres_active(void) { return 0; }
703static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800704static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400705static inline void
706hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800707static 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 return 0;
712}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800713static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800714
715#endif /* CONFIG_HIGH_RES_TIMERS */
716
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200717/*
718 * Retrigger next event is called after clock was set
719 *
720 * Called with interrupts disabled via on_each_cpu()
721 */
722static void retrigger_next_event(void *arg)
723{
724 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
725 struct timespec realtime_offset, xtim, wtm, sleep;
726
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200727 if (!hrtimer_hres_active()) {
728 raw_spin_lock(&base->lock);
729 hrtimer_expire_cancelable(base);
730 raw_spin_unlock(&base->lock);
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200731 return;
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200732 }
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200733
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200734 /* Optimized out for !HIGH_RES */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200735 get_xtime_and_monotonic_and_sleep_offset(&xtim, &wtm, &sleep);
736 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
737
738 /* Adjust CLOCK_REALTIME offset */
739 raw_spin_lock(&base->lock);
740 base->clock_base[HRTIMER_BASE_REALTIME].offset =
741 timespec_to_ktime(realtime_offset);
742 base->clock_base[HRTIMER_BASE_BOOTTIME].offset =
743 timespec_to_ktime(sleep);
Thomas Gleixner99ee5312011-04-27 14:16:42 +0200744 base->clock_base[HRTIMER_BASE_REALTIME_COS].offset =
745 timespec_to_ktime(realtime_offset);
746
747 hrtimer_expire_cancelable(base);
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200748
749 hrtimer_force_reprogram(base, 0);
750 raw_spin_unlock(&base->lock);
751}
752
753/*
754 * Clock realtime was set
755 *
756 * Change the offset of the realtime clock vs. the monotonic
757 * clock.
758 *
759 * We might have to reprogram the high resolution timer interrupt. On
760 * SMP we call the architecture specific code to retrigger _all_ high
761 * resolution timer interrupts. On UP we just disable interrupts and
762 * call the high resolution interrupt code.
763 */
764void clock_was_set(void)
765{
766 /* Retrigger the CPU local events everywhere */
767 on_each_cpu(retrigger_next_event, NULL, 1);
768}
769
770/*
771 * During resume we might have to reprogram the high resolution timer
772 * interrupt (on the local CPU):
773 */
774void hrtimers_resume(void)
775{
776 WARN_ONCE(!irqs_disabled(),
777 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
778
779 retrigger_next_event(NULL);
780}
781
Heiko Carstens5f201902009-12-10 10:56:29 +0100782static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800783{
Heiko Carstens5f201902009-12-10 10:56:29 +0100784#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800785 if (timer->start_site)
786 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100787 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800788 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
789 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800790#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100791}
792
793static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
794{
795#ifdef CONFIG_TIMER_STATS
796 timer->start_site = NULL;
797#endif
798}
799
800static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
801{
802#ifdef CONFIG_TIMER_STATS
803 if (likely(!timer_stats_active))
804 return;
805 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
806 timer->function, timer->start_comm, 0);
807#endif
808}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800809
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800810/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200811 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812 */
813static inline
814void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
815{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100816 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817}
818
819/**
820 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800822 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823 * @interval: the interval to forward
824 *
825 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700826 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800827 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800828u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800829{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800830 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800831 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832
Arjan van de Vencc584b22008-09-01 15:02:30 -0700833 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800834
835 if (delta.tv64 < 0)
836 return 0;
837
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100838 if (interval.tv64 < timer->base->resolution.tv64)
839 interval.tv64 = timer->base->resolution.tv64;
840
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800841 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800842 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800843
844 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700845 hrtimer_add_expires_ns(timer, incr * orun);
846 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800847 return orun;
848 /*
849 * This (and the ktime_add() below) is the
850 * correction for exact:
851 */
852 orun++;
853 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700854 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800855
856 return orun;
857}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700858EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800859
860/*
861 * enqueue_hrtimer - internal function to (re)start a timer
862 *
863 * The timer is inserted in expiry order. Insertion into the
864 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100865 *
866 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800867 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100868static int enqueue_hrtimer(struct hrtimer *timer,
869 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800870{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800871 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700872
John Stultz998adc32010-09-20 19:19:17 -0700873 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800874
875 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800876 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
877 * state of a possibly running callback.
878 */
879 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100880
John Stultz998adc32010-09-20 19:19:17 -0700881 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100882}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800883
884/*
885 * __remove_hrtimer - internal function to remove a timer
886 *
887 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800888 *
889 * High resolution timer mode reprograms the clock event device when the
890 * timer is the one which expires next. The caller can disable this by setting
891 * reprogram to zero. This is useful, when the context does a reprogramming
892 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800893 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800894static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800895 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800896 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800897{
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400898 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
899 goto out;
900
John Stultz998adc32010-09-20 19:19:17 -0700901 if (&timer->node == timerqueue_getnext(&base->active)) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400902#ifdef CONFIG_HIGH_RES_TIMERS
903 /* Reprogram the clock event device. if enabled */
904 if (reprogram && hrtimer_hres_active()) {
905 ktime_t expires;
906
907 expires = ktime_sub(hrtimer_get_expires(timer),
908 base->offset);
909 if (base->cpu_base->expires_next.tv64 == expires.tv64)
910 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800911 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400912#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800913 }
John Stultz998adc32010-09-20 19:19:17 -0700914 timerqueue_del(&base->active, &timer->node);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400915out:
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)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700926 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800927 int reprogram;
928
929 /*
930 * Remove the timer and force reprogramming when high
931 * resolution mode is active and the timer is on the current
932 * CPU. If we remove a timer on another CPU, reprogramming is
933 * skipped. The interrupt event on this CPU is fired and
934 * reprogramming happens in the interrupt handler. This is a
935 * rare case and less expensive than a smp call.
936 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800937 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800938 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800939 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700940 /*
941 * We must preserve the CALLBACK state flag here,
942 * otherwise we could move the timer base in
943 * switch_hrtimer_base.
944 */
945 state = timer->state & HRTIMER_STATE_CALLBACK;
946 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800947 return 1;
948 }
949 return 0;
950}
951
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100952int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
953 unsigned long delta_ns, const enum hrtimer_mode mode,
954 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800955{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800956 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800957 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100958 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800959
960 base = lock_hrtimer_base(timer, &flags);
961
962 /* Remove an active timer from the queue: */
963 ret = remove_hrtimer(timer, base);
964
965 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530966 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800967
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530968 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100969 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800970 /*
971 * CONFIG_TIME_LOW_RES is a temporary way for architectures
972 * to signal that they simply return xtime in
973 * do_gettimeoffset(). In this case we want to round up by
974 * resolution when starting a relative timer, to avoid short
975 * timeouts. This will go away with the GTOD framework.
976 */
977#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100978 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800979#endif
980 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700981
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700982 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800983
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800984 timer_stats_hrtimer_set_start_info(timer);
985
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100986 leftmost = enqueue_hrtimer(timer, new_base);
987
Ingo Molnar935c6312007-03-28 13:17:18 +0200988 /*
989 * Only allow reprogramming if the new base is on this CPU.
990 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100991 *
992 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200993 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100994 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100995 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800996
997 unlock_hrtimer_base(timer, &flags);
998
999 return ret;
1000}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001001
1002/**
1003 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1004 * @timer: the timer to be added
1005 * @tim: expiry time
1006 * @delta_ns: "slack" range for the timer
1007 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1008 *
1009 * Returns:
1010 * 0 on success
1011 * 1 when the timer was active
1012 */
1013int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1014 unsigned long delta_ns, const enum hrtimer_mode mode)
1015{
1016 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1017}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001018EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1019
1020/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001021 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001022 * @timer: the timer to be added
1023 * @tim: expiry time
1024 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1025 *
1026 * Returns:
1027 * 0 on success
1028 * 1 when the timer was active
1029 */
1030int
1031hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1032{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001033 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001034}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001035EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001036
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001037
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001038/**
1039 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001040 * @timer: hrtimer to stop
1041 *
1042 * Returns:
1043 * 0 when the timer was not active
1044 * 1 when the timer was active
1045 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001046 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001047 */
1048int hrtimer_try_to_cancel(struct hrtimer *timer)
1049{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001050 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001051 unsigned long flags;
1052 int ret = -1;
1053
1054 base = lock_hrtimer_base(timer, &flags);
1055
Thomas Gleixner303e9672007-02-16 01:27:51 -08001056 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001057 ret = remove_hrtimer(timer, base);
1058
1059 unlock_hrtimer_base(timer, &flags);
1060
1061 return ret;
1062
1063}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001064EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001065
1066/**
1067 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001068 * @timer: the timer to be cancelled
1069 *
1070 * Returns:
1071 * 0 when the timer was not active
1072 * 1 when the timer was active
1073 */
1074int hrtimer_cancel(struct hrtimer *timer)
1075{
1076 for (;;) {
1077 int ret = hrtimer_try_to_cancel(timer);
1078
1079 if (ret >= 0)
1080 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001081 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001082 }
1083}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001084EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001085
1086/**
1087 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001088 * @timer: the timer to read
1089 */
1090ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1091{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001092 unsigned long flags;
1093 ktime_t rem;
1094
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001095 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001096 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001097 unlock_hrtimer_base(timer, &flags);
1098
1099 return rem;
1100}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001101EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001102
Russell Kingee9c5782008-04-20 13:59:33 +01001103#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001104/**
1105 * hrtimer_get_next_event - get the time until next expiry event
1106 *
1107 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1108 * is pending.
1109 */
1110ktime_t hrtimer_get_next_event(void)
1111{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001112 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1113 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001114 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1115 unsigned long flags;
1116 int i;
1117
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001118 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001119
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001120 if (!hrtimer_hres_active()) {
1121 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1122 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001123 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001124
John Stultz998adc32010-09-20 19:19:17 -07001125 next = timerqueue_getnext(&base->active);
1126 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001127 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001128
John Stultz998adc32010-09-20 19:19:17 -07001129 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001130 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001131 delta = ktime_sub(delta, base->get_time());
1132 if (delta.tv64 < mindelta.tv64)
1133 mindelta.tv64 = delta.tv64;
1134 }
Tony Lindgren69239742006-03-06 15:42:45 -08001135 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001136
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001137 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001138
Tony Lindgren69239742006-03-06 15:42:45 -08001139 if (mindelta.tv64 < 0)
1140 mindelta.tv64 = 0;
1141 return mindelta;
1142}
1143#endif
1144
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001145static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1146 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001147{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001148 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001149 int base;
George Anzinger79786722006-02-01 03:05:11 -08001150
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001151 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001152
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001153 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001154
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001155 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001156 clock_id = CLOCK_MONOTONIC;
1157
John Stultze06383d2010-12-14 19:37:07 -08001158 base = hrtimer_clockid_to_base(clock_id);
1159 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001160 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001161
1162#ifdef CONFIG_TIMER_STATS
1163 timer->start_site = NULL;
1164 timer->start_pid = -1;
1165 memset(timer->start_comm, 0, TASK_COMM_LEN);
1166#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001167}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001168
1169/**
1170 * hrtimer_init - initialize a timer to the given clock
1171 * @timer: the timer to be initialized
1172 * @clock_id: the clock to be used
1173 * @mode: timer mode abs/rel
1174 */
1175void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1176 enum hrtimer_mode mode)
1177{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001178 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001179 __hrtimer_init(timer, clock_id, mode);
1180}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001181EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001182
1183/**
1184 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001185 * @which_clock: which clock to query
1186 * @tp: pointer to timespec variable to store the resolution
1187 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001188 * Store the resolution of the clock selected by @which_clock in the
1189 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001190 */
1191int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1192{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001193 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001194 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001195
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001196 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001197 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001198
1199 return 0;
1200}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001201EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001202
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001203static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001204{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001205 struct hrtimer_clock_base *base = timer->base;
1206 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1207 enum hrtimer_restart (*fn)(struct hrtimer *);
1208 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001209
Peter Zijlstraca109492008-11-25 12:43:51 +01001210 WARN_ON(!irqs_disabled());
1211
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001212 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001213 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1214 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001215 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001216
1217 /*
1218 * Because we run timers from hardirq context, there is no chance
1219 * they get migrated to another cpu, therefore its safe to unlock
1220 * the timer base.
1221 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001222 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001223 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001224 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001225 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001226 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001227
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001228 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001229 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1230 * we do not reprogramm the event hardware. Happens either in
1231 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001232 */
1233 if (restart != HRTIMER_NORESTART) {
1234 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001235 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001236 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001237
1238 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1239
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001240 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001241}
1242
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001243static void hrtimer_expire_cancelable(struct hrtimer_cpu_base *cpu_base)
1244{
1245 struct timerqueue_node *node;
1246 struct hrtimer_clock_base *base;
1247 ktime_t now = ktime_get_real();
1248
1249 base = &cpu_base->clock_base[HRTIMER_BASE_REALTIME_COS];
1250
1251 while ((node = timerqueue_getnext(&base->active))) {
1252 struct hrtimer *timer;
1253
1254 timer = container_of(node, struct hrtimer, node);
1255 __run_hrtimer(timer, &now);
1256 }
1257}
1258
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001259#ifdef CONFIG_HIGH_RES_TIMERS
1260
1261/*
1262 * High resolution timer interrupt
1263 * Called with interrupts disabled
1264 */
1265void hrtimer_interrupt(struct clock_event_device *dev)
1266{
1267 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1268 struct hrtimer_clock_base *base;
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001269 ktime_t expires_next, now, entry_time, delta;
1270 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001271
1272 BUG_ON(!cpu_base->hres_active);
1273 cpu_base->nr_events++;
1274 dev->next_event.tv64 = KTIME_MAX;
1275
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001276 entry_time = now = ktime_get();
1277retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001278 expires_next.tv64 = KTIME_MAX;
1279
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001280 raw_spin_lock(&cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001281 /*
1282 * We set expires_next to KTIME_MAX here with cpu_base->lock
1283 * held to prevent that a timer is enqueued in our queue via
1284 * the migration code. This does not affect enqueueing of
1285 * timers which run their callback and need to be requeued on
1286 * this CPU.
1287 */
1288 cpu_base->expires_next.tv64 = KTIME_MAX;
1289
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001290 base = cpu_base->clock_base;
1291
1292 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1293 ktime_t basenow;
John Stultz998adc32010-09-20 19:19:17 -07001294 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001295
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001296 basenow = ktime_add(now, base->offset);
1297
John Stultz998adc32010-09-20 19:19:17 -07001298 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001299 struct hrtimer *timer;
1300
John Stultz998adc32010-09-20 19:19:17 -07001301 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001302
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001303 /*
1304 * The immediate goal for using the softexpires is
1305 * minimizing wakeups, not running timers at the
1306 * earliest interrupt after their soft expiration.
1307 * This allows us to avoid using a Priority Search
1308 * Tree, which can answer a stabbing querry for
1309 * overlapping intervals and instead use the simple
1310 * BST we already have.
1311 * We don't add extra wakeups by delaying timers that
1312 * are right-of a not yet expired timer, because that
1313 * timer will have to trigger a wakeup anyway.
1314 */
1315
1316 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001317 ktime_t expires;
1318
Arjan van de Vencc584b22008-09-01 15:02:30 -07001319 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001320 base->offset);
1321 if (expires.tv64 < expires_next.tv64)
1322 expires_next = expires;
1323 break;
1324 }
1325
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001326 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001327 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001328 base++;
1329 }
1330
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001331 /*
1332 * Store the new expiry value so the migration code can verify
1333 * against it.
1334 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001335 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001336 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001337
1338 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001339 if (expires_next.tv64 == KTIME_MAX ||
1340 !tick_program_event(expires_next, 0)) {
1341 cpu_base->hang_detected = 0;
1342 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001343 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001344
1345 /*
1346 * The next timer was already expired due to:
1347 * - tracing
1348 * - long lasting callbacks
1349 * - being scheduled away when running in a VM
1350 *
1351 * We need to prevent that we loop forever in the hrtimer
1352 * interrupt routine. We give it 3 attempts to avoid
1353 * overreacting on some spurious event.
1354 */
1355 now = ktime_get();
1356 cpu_base->nr_retries++;
1357 if (++retries < 3)
1358 goto retry;
1359 /*
1360 * Give the system a chance to do something else than looping
1361 * here. We stored the entry time, so we know exactly how long
1362 * we spent here. We schedule the next event this amount of
1363 * time away.
1364 */
1365 cpu_base->nr_hangs++;
1366 cpu_base->hang_detected = 1;
1367 delta = ktime_sub(now, entry_time);
1368 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1369 cpu_base->max_hang_time = delta;
1370 /*
1371 * Limit it to a sensible value as we enforce a longer
1372 * delay. Give the CPU at least 100ms to catch up.
1373 */
1374 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1375 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1376 else
1377 expires_next = ktime_add(now, delta);
1378 tick_program_event(expires_next, 1);
1379 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1380 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001381}
1382
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001383/*
1384 * local version of hrtimer_peek_ahead_timers() called with interrupts
1385 * disabled.
1386 */
1387static void __hrtimer_peek_ahead_timers(void)
1388{
1389 struct tick_device *td;
1390
1391 if (!hrtimer_hres_active())
1392 return;
1393
1394 td = &__get_cpu_var(tick_cpu_device);
1395 if (td && td->evtdev)
1396 hrtimer_interrupt(td->evtdev);
1397}
1398
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001399/**
1400 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1401 *
1402 * hrtimer_peek_ahead_timers will peek at the timer queue of
1403 * the current cpu and check if there are any timers for which
1404 * the soft expires time has passed. If any such timers exist,
1405 * they are run immediately and then removed from the timer queue.
1406 *
1407 */
1408void hrtimer_peek_ahead_timers(void)
1409{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001410 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001411
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001412 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001413 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001414 local_irq_restore(flags);
1415}
1416
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001417static void run_hrtimer_softirq(struct softirq_action *h)
1418{
1419 hrtimer_peek_ahead_timers();
1420}
1421
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001422#else /* CONFIG_HIGH_RES_TIMERS */
1423
1424static inline void __hrtimer_peek_ahead_timers(void) { }
1425
1426#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001427
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001428/*
1429 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001430 *
1431 * For HRT its the fall back code to run the softirq in the timer
1432 * softirq context in case the hrtimer initialization failed or has
1433 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001434 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001435void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001436{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001437 if (hrtimer_hres_active())
1438 return;
1439
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001440 /*
1441 * This _is_ ugly: We have to check in the softirq context,
1442 * whether we can switch to highres and / or nohz mode. The
1443 * clocksource switch happens in the timer interrupt with
1444 * xtime_lock held. Notification from there only sets the
1445 * check bit in the tick_oneshot code, otherwise we might
1446 * deadlock vs. xtime_lock.
1447 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001448 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001449 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001450}
1451
1452/*
1453 * Called from hardirq context every jiffy
1454 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001455void hrtimer_run_queues(void)
1456{
John Stultz998adc32010-09-20 19:19:17 -07001457 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001458 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001459 struct hrtimer_clock_base *base;
1460 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001461
1462 if (hrtimer_hres_active())
1463 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001464
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001465 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1466 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001467 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001468 continue;
1469
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001470 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001471 hrtimer_get_softirq_time(cpu_base);
1472 gettime = 0;
1473 }
1474
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001475 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001476
John Stultzb007c382010-12-10 22:19:53 -08001477 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001478 struct hrtimer *timer;
1479
John Stultz998adc32010-09-20 19:19:17 -07001480 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001481 if (base->softirq_time.tv64 <=
1482 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001483 break;
1484
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001485 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001486 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001487 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001488 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001489}
1490
1491/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001492 * Sleep related functions:
1493 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001494static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001495{
1496 struct hrtimer_sleeper *t =
1497 container_of(timer, struct hrtimer_sleeper, timer);
1498 struct task_struct *task = t->task;
1499
1500 t->task = NULL;
1501 if (task)
1502 wake_up_process(task);
1503
1504 return HRTIMER_NORESTART;
1505}
1506
Ingo Molnar36c8b582006-07-03 00:25:41 -07001507void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001508{
1509 sl->timer.function = hrtimer_wakeup;
1510 sl->task = task;
1511}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001512EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001513
Thomas Gleixner669d7862006-03-31 02:31:19 -08001514static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001515{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001516 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001517
Roman Zippel432569b2006-03-26 01:38:08 -08001518 do {
1519 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001520 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001521 if (!hrtimer_active(&t->timer))
1522 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001523
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001524 if (likely(t->task))
1525 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001526
Thomas Gleixner669d7862006-03-31 02:31:19 -08001527 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001528 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001529
Thomas Gleixner669d7862006-03-31 02:31:19 -08001530 } while (t->task && !signal_pending(current));
1531
Peter Zijlstra3588a082008-02-01 17:45:13 +01001532 __set_current_state(TASK_RUNNING);
1533
Thomas Gleixner669d7862006-03-31 02:31:19 -08001534 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001535}
1536
Oleg Nesterov080344b2008-02-01 17:29:05 +03001537static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1538{
1539 struct timespec rmt;
1540 ktime_t rem;
1541
Arjan van de Vencc584b22008-09-01 15:02:30 -07001542 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001543 if (rem.tv64 <= 0)
1544 return 0;
1545 rmt = ktime_to_timespec(rem);
1546
1547 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1548 return -EFAULT;
1549
1550 return 1;
1551}
1552
Toyo Abe1711ef32006-09-29 02:00:28 -07001553long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001554{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001555 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001556 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001557 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001558
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001559 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1560 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001561 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001562
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001563 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001564 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001565
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001566 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001567 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001568 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001569 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001570 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001571 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001572
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001573 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001574 ret = -ERESTART_RESTARTBLOCK;
1575out:
1576 destroy_hrtimer_on_stack(&t.timer);
1577 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001578}
1579
Oleg Nesterov080344b2008-02-01 17:29:05 +03001580long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001581 const enum hrtimer_mode mode, const clockid_t clockid)
1582{
1583 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001584 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001585 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001586 unsigned long slack;
1587
1588 slack = current->timer_slack_ns;
1589 if (rt_task(current))
1590 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001591
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001592 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001593 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001594 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001595 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001596
George Anzinger79786722006-02-01 03:05:11 -08001597 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001598 if (mode == HRTIMER_MODE_ABS) {
1599 ret = -ERESTARTNOHAND;
1600 goto out;
1601 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001602
Roman Zippel432569b2006-03-26 01:38:08 -08001603 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001604 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001605 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001606 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001607 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001608
1609 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001610 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001611 restart->nanosleep.index = t.timer.base->index;
1612 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001613 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001614
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001615 ret = -ERESTART_RESTARTBLOCK;
1616out:
1617 destroy_hrtimer_on_stack(&t.timer);
1618 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001619}
1620
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001621SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1622 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001623{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001624 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001625
1626 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1627 return -EFAULT;
1628
1629 if (!timespec_valid(&tu))
1630 return -EINVAL;
1631
Oleg Nesterov080344b2008-02-01 17:29:05 +03001632 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001633}
1634
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001635/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001636 * Functions related to boot-time initialization:
1637 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001638static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001639{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001640 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001641 int i;
1642
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001643 raw_spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001644
John Stultz998adc32010-09-20 19:19:17 -07001645 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001646 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001647 timerqueue_init_head(&cpu_base->clock_base[i].active);
1648 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001649
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001650 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001651}
1652
1653#ifdef CONFIG_HOTPLUG_CPU
1654
Peter Zijlstraca109492008-11-25 12:43:51 +01001655static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001656 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001657{
1658 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001659 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001660
John Stultz998adc32010-09-20 19:19:17 -07001661 while ((node = timerqueue_getnext(&old_base->active))) {
1662 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001663 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001664 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001665
1666 /*
1667 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1668 * timer could be seen as !active and just vanish away
1669 * under us on another CPU
1670 */
1671 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001672 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001673 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001674 * Enqueue the timers on the new cpu. This does not
1675 * reprogram the event device in case the timer
1676 * expires before the earliest on this CPU, but we run
1677 * hrtimer_interrupt after we migrated everything to
1678 * sort out already expired timers and reprogram the
1679 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001680 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001681 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001682
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001683 /* Clear the migration state bit */
1684 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001685 }
1686}
1687
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001688static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001689{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001690 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001691 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001692
Peter Zijlstra37810652008-12-04 11:17:10 +01001693 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001694 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001695
1696 local_irq_disable();
1697 old_base = &per_cpu(hrtimer_bases, scpu);
1698 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001699 /*
1700 * The caller is globally serialized and nobody else
1701 * takes two locks at once, deadlock is not possible.
1702 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001703 raw_spin_lock(&new_base->lock);
1704 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001705
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001706 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001707 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001708 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001709 }
1710
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001711 raw_spin_unlock(&old_base->lock);
1712 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001713
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001714 /* Check, if we got expired work to do */
1715 __hrtimer_peek_ahead_timers();
1716 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001717}
1718
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001719#endif /* CONFIG_HOTPLUG_CPU */
1720
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001721static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001722 unsigned long action, void *hcpu)
1723{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001724 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001725
1726 switch (action) {
1727
1728 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001729 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001730 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001731 break;
1732
1733#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001734 case CPU_DYING:
1735 case CPU_DYING_FROZEN:
1736 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1737 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001738 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001739 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001740 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001741 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001742 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001743 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001744 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001745#endif
1746
1747 default:
1748 break;
1749 }
1750
1751 return NOTIFY_OK;
1752}
1753
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001754static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001755 .notifier_call = hrtimer_cpu_notify,
1756};
1757
1758void __init hrtimers_init(void)
1759{
1760 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1761 (void *)(long)smp_processor_id());
1762 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001763#ifdef CONFIG_HIGH_RES_TIMERS
1764 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1765#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001766}
1767
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001768/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001769 * schedule_hrtimeout_range_clock - sleep until timeout
1770 * @expires: timeout value (ktime_t)
1771 * @delta: slack in expires timeout (ktime_t)
1772 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1773 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1774 */
1775int __sched
1776schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1777 const enum hrtimer_mode mode, int clock)
1778{
1779 struct hrtimer_sleeper t;
1780
1781 /*
1782 * Optimize when a zero timeout value is given. It does not
1783 * matter whether this is an absolute or a relative time.
1784 */
1785 if (expires && !expires->tv64) {
1786 __set_current_state(TASK_RUNNING);
1787 return 0;
1788 }
1789
1790 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001791 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001792 */
1793 if (!expires) {
1794 schedule();
1795 __set_current_state(TASK_RUNNING);
1796 return -EINTR;
1797 }
1798
1799 hrtimer_init_on_stack(&t.timer, clock, mode);
1800 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1801
1802 hrtimer_init_sleeper(&t, current);
1803
1804 hrtimer_start_expires(&t.timer, mode);
1805 if (!hrtimer_active(&t.timer))
1806 t.task = NULL;
1807
1808 if (likely(t.task))
1809 schedule();
1810
1811 hrtimer_cancel(&t.timer);
1812 destroy_hrtimer_on_stack(&t.timer);
1813
1814 __set_current_state(TASK_RUNNING);
1815
1816 return !t.task ? 0 : -EINTR;
1817}
1818
1819/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001820 * schedule_hrtimeout_range - sleep until timeout
1821 * @expires: timeout value (ktime_t)
1822 * @delta: slack in expires timeout (ktime_t)
1823 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1824 *
1825 * Make the current task sleep until the given expiry time has
1826 * elapsed. The routine will return immediately unless
1827 * the current task state has been set (see set_current_state()).
1828 *
1829 * The @delta argument gives the kernel the freedom to schedule the
1830 * actual wakeup to a time that is both power and performance friendly.
1831 * The kernel give the normal best effort behavior for "@expires+@delta",
1832 * but may decide to fire the timer earlier, but no earlier than @expires.
1833 *
1834 * You can set the task state as follows -
1835 *
1836 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1837 * pass before the routine returns.
1838 *
1839 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1840 * delivered to the current task.
1841 *
1842 * The current task state is guaranteed to be TASK_RUNNING when this
1843 * routine returns.
1844 *
1845 * Returns 0 when the timer has expired otherwise -EINTR
1846 */
1847int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001848 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001849{
Carsten Emde351b3f72010-04-02 22:40:19 +02001850 return schedule_hrtimeout_range_clock(expires, delta, mode,
1851 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001852}
1853EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1854
1855/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001856 * schedule_hrtimeout - sleep until timeout
1857 * @expires: timeout value (ktime_t)
1858 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1859 *
1860 * Make the current task sleep until the given expiry time has
1861 * elapsed. The routine will return immediately unless
1862 * the current task state has been set (see set_current_state()).
1863 *
1864 * You can set the task state as follows -
1865 *
1866 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1867 * pass before the routine returns.
1868 *
1869 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1870 * delivered to the current task.
1871 *
1872 * The current task state is guaranteed to be TASK_RUNNING when this
1873 * routine returns.
1874 *
1875 * Returns 0 when the timer has expired otherwise -EINTR
1876 */
1877int __sched schedule_hrtimeout(ktime_t *expires,
1878 const enum hrtimer_mode mode)
1879{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001880 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001881}
1882EXPORT_SYMBOL_GPL(schedule_hrtimeout);