blob: a57ef25867e3a7cbd24e488aa64253a5550ae42b [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>
Paul Gortmaker9984de12011-05-23 14:51:41 -040035#include <linux/export.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080036#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
Michael Bohan11036072013-03-19 19:19:25 -070064 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080065 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080066 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080067 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020068 .index = HRTIMER_BASE_MONOTONIC,
69 .clockid = CLOCK_MONOTONIC,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080070 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080071 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080072 },
John Stultz70a08cc2011-02-15 10:45:16 -080073 {
Thomas Gleixner68fa61c2011-05-20 23:14:04 +020074 .index = HRTIMER_BASE_REALTIME,
75 .clockid = CLOCK_REALTIME,
76 .get_time = &ktime_get_real,
77 .resolution = KTIME_LOW_RES,
78 },
79 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020080 .index = HRTIMER_BASE_BOOTTIME,
81 .clockid = CLOCK_BOOTTIME,
John Stultz70a08cc2011-02-15 10:45:16 -080082 .get_time = &ktime_get_boottime,
83 .resolution = KTIME_LOW_RES,
84 },
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080085 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080086};
87
Mike Frysinger942c3c52011-05-02 15:24:27 -040088static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
Thomas Gleixnerce313322011-04-29 00:02:00 +020089 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
90 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
91 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
92};
John Stultze06383d2010-12-14 19:37:07 -080093
94static inline int hrtimer_clockid_to_base(clockid_t clock_id)
95{
96 return hrtimer_clock_to_base_table[clock_id];
97}
98
99
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800100/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800101 * Get the coarse grained time at the softirq based on xtime and
102 * wall_to_monotonic.
103 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800104static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800105{
John Stultz70a08cc2011-02-15 10:45:16 -0800106 ktime_t xtim, mono, boot;
John Stultz314ac372011-02-14 18:43:08 -0800107 struct timespec xts, tom, slp;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800108
John Stultz314ac372011-02-14 18:43:08 -0800109 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800110
john stultzf4304ab2007-02-16 01:27:26 -0800111 xtim = timespec_to_ktime(xts);
John Stultz70a08cc2011-02-15 10:45:16 -0800112 mono = ktime_add(xtim, timespec_to_ktime(tom));
113 boot = ktime_add(mono, timespec_to_ktime(slp));
John Stultze06383d2010-12-14 19:37:07 -0800114 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
John Stultz70a08cc2011-02-15 10:45:16 -0800115 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
116 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800117}
118
119/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800120 * Functions and macros which are different for UP/SMP systems are kept in a
121 * single place
122 */
123#ifdef CONFIG_SMP
124
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800125/*
126 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
127 * means that all timers which are tied to this base via timer->base are
128 * locked, and the base itself is locked too.
129 *
130 * So __run_timers/migrate_timers can safely modify all timers which could
131 * be found on the lists/queues.
132 *
133 * When the timer's base is locked, and the timer removed from list, it is
134 * possible to set timer->base = NULL and drop the lock: the timer remains
135 * locked.
136 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800137static
138struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
139 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800140{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800141 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800142
143 for (;;) {
144 base = timer->base;
145 if (likely(base != NULL)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100146 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800147 if (likely(base == timer->base))
148 return base;
149 /* The timer has migrated to another CPU: */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100150 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800151 }
152 cpu_relax();
153 }
154}
155
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200156
157/*
158 * Get the preferred target CPU for NOHZ
159 */
160static int hrtimer_get_target(int this_cpu, int pinned)
161{
162#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700163 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
164 return get_nohz_timer_target();
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200165#endif
166 return this_cpu;
167}
168
169/*
170 * With HIGHRES=y we do not migrate the timer when it is expiring
171 * before the next event on the target cpu because we cannot reprogram
172 * the target cpu hardware and we would cause it to fire late.
173 *
174 * Called with cpu_base->lock of target cpu held.
175 */
176static int
177hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
178{
179#ifdef CONFIG_HIGH_RES_TIMERS
180 ktime_t expires;
181
182 if (!new_base->cpu_base->hres_active)
183 return 0;
184
185 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
186 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
187#else
188 return 0;
189#endif
190}
191
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800192/*
193 * Switch the timer base to the current CPU when possible.
194 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800195static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530196switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
197 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800198{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800199 struct hrtimer_clock_base *new_base;
200 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200201 int this_cpu = smp_processor_id();
202 int cpu = hrtimer_get_target(this_cpu, pinned);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200203 int basenum = base->index;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530204
205again:
206 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
John Stultze06383d2010-12-14 19:37:07 -0800207 new_base = &new_cpu_base->clock_base[basenum];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800208
209 if (base != new_base) {
210 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200211 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800212 * However we can't change timer's base while it is running,
213 * so we keep it on the same CPU. No hassle vs. reprogramming
214 * the event source in the high resolution case. The softirq
215 * code will take care of this when the timer function has
216 * completed. There is no conflict as we hold the lock until
217 * the timer is enqueued.
218 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800219 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800220 return base;
221
222 /* See the comment in lock_timer_base() */
223 timer->base = NULL;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100224 raw_spin_unlock(&base->cpu_base->lock);
225 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530226
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200227 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
228 cpu = this_cpu;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100229 raw_spin_unlock(&new_base->cpu_base->lock);
230 raw_spin_lock(&base->cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200231 timer->base = base;
232 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530233 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800234 timer->base = new_base;
235 }
236 return new_base;
237}
238
239#else /* CONFIG_SMP */
240
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800241static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800242lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
243{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800244 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800245
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100246 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800247
248 return base;
249}
250
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530251# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800252
253#endif /* !CONFIG_SMP */
254
255/*
256 * Functions for the union type storage format of ktime_t which are
257 * too large for inlining:
258 */
259#if BITS_PER_LONG < 64
260# ifndef CONFIG_KTIME_SCALAR
261/**
262 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800263 * @kt: addend
264 * @nsec: the scalar nsec value to add
265 *
266 * Returns the sum of kt and nsec in ktime_t format
267 */
268ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
269{
270 ktime_t tmp;
271
272 if (likely(nsec < NSEC_PER_SEC)) {
273 tmp.tv64 = nsec;
274 } else {
275 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
276
277 tmp = ktime_set((long)nsec, rem);
278 }
279
280 return ktime_add(kt, tmp);
281}
David Howellsb8b8fd22007-04-27 15:31:24 -0700282
283EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700284
285/**
286 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
287 * @kt: minuend
288 * @nsec: the scalar nsec value to subtract
289 *
290 * Returns the subtraction of @nsec from @kt in ktime_t format
291 */
292ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
293{
294 ktime_t tmp;
295
296 if (likely(nsec < NSEC_PER_SEC)) {
297 tmp.tv64 = nsec;
298 } else {
299 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
300
David Engraf86a640f2013-03-19 13:29:55 +0100301 /* Make sure nsec fits into long */
302 if (unlikely(nsec > KTIME_SEC_MAX))
303 return (ktime_t){ .tv64 = KTIME_MAX };
304
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700305 tmp = ktime_set((long)nsec, rem);
306 }
307
308 return ktime_sub(kt, tmp);
309}
310
311EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800312# endif /* !CONFIG_KTIME_SCALAR */
313
314/*
315 * Divide a ktime value by a nanosecond value
316 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800317u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800318{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300319 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800320 int sft = 0;
321
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300322 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800323 /* Make sure the divisor is less than 2^32: */
324 while (div >> 32) {
325 sft++;
326 div >>= 1;
327 }
328 dclc >>= sft;
329 do_div(dclc, (unsigned long) div);
330
Davide Libenzi4d672e72008-02-04 22:27:26 -0800331 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800332}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800333#endif /* BITS_PER_LONG >= 64 */
334
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100335/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100336 * Add two ktime values and do a safety check for overflow:
337 */
338ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
339{
340 ktime_t res = ktime_add(lhs, rhs);
341
342 /*
343 * We use KTIME_SEC_MAX here, the maximum timeout which we can
344 * return to user space in a timespec:
345 */
346 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
347 res = ktime_set(KTIME_SEC_MAX, 0);
348
349 return res;
350}
351
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300352EXPORT_SYMBOL_GPL(ktime_add_safe);
353
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700354#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
355
356static struct debug_obj_descr hrtimer_debug_descr;
357
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100358static void *hrtimer_debug_hint(void *addr)
359{
360 return ((struct hrtimer *) addr)->function;
361}
362
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700363/*
364 * fixup_init is called when:
365 * - an active object is initialized
366 */
367static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
368{
369 struct hrtimer *timer = addr;
370
371 switch (state) {
372 case ODEBUG_STATE_ACTIVE:
373 hrtimer_cancel(timer);
374 debug_object_init(timer, &hrtimer_debug_descr);
375 return 1;
376 default:
377 return 0;
378 }
379}
380
381/*
382 * fixup_activate is called when:
383 * - an active object is activated
384 * - an unknown object is activated (might be a statically initialized object)
385 */
386static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
387{
388 switch (state) {
389
390 case ODEBUG_STATE_NOTAVAILABLE:
391 WARN_ON_ONCE(1);
392 return 0;
393
394 case ODEBUG_STATE_ACTIVE:
395 WARN_ON(1);
396
397 default:
398 return 0;
399 }
400}
401
402/*
403 * fixup_free is called when:
404 * - an active object is freed
405 */
406static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
407{
408 struct hrtimer *timer = addr;
409
410 switch (state) {
411 case ODEBUG_STATE_ACTIVE:
412 hrtimer_cancel(timer);
413 debug_object_free(timer, &hrtimer_debug_descr);
414 return 1;
415 default:
416 return 0;
417 }
418}
419
420static struct debug_obj_descr hrtimer_debug_descr = {
421 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100422 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700423 .fixup_init = hrtimer_fixup_init,
424 .fixup_activate = hrtimer_fixup_activate,
425 .fixup_free = hrtimer_fixup_free,
426};
427
428static inline void debug_hrtimer_init(struct hrtimer *timer)
429{
430 debug_object_init(timer, &hrtimer_debug_descr);
431}
432
433static inline void debug_hrtimer_activate(struct hrtimer *timer)
434{
435 debug_object_activate(timer, &hrtimer_debug_descr);
436}
437
438static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
439{
440 debug_object_deactivate(timer, &hrtimer_debug_descr);
441}
442
443static inline void debug_hrtimer_free(struct hrtimer *timer)
444{
445 debug_object_free(timer, &hrtimer_debug_descr);
446}
447
448static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
449 enum hrtimer_mode mode);
450
451void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
452 enum hrtimer_mode mode)
453{
454 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
455 __hrtimer_init(timer, clock_id, mode);
456}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700457EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700458
459void destroy_hrtimer_on_stack(struct hrtimer *timer)
460{
461 debug_object_free(timer, &hrtimer_debug_descr);
462}
463
464#else
465static inline void debug_hrtimer_init(struct hrtimer *timer) { }
466static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
467static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
468#endif
469
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800470static inline void
471debug_init(struct hrtimer *timer, clockid_t clockid,
472 enum hrtimer_mode mode)
473{
474 debug_hrtimer_init(timer);
475 trace_hrtimer_init(timer, clockid, mode);
476}
477
478static inline void debug_activate(struct hrtimer *timer)
479{
480 debug_hrtimer_activate(timer);
481 trace_hrtimer_start(timer);
482}
483
484static inline void debug_deactivate(struct hrtimer *timer)
485{
486 debug_hrtimer_deactivate(timer);
487 trace_hrtimer_cancel(timer);
488}
489
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800490/* High resolution timer related functions */
491#ifdef CONFIG_HIGH_RES_TIMERS
492
493/*
494 * High resolution timer enabled ?
495 */
496static int hrtimer_hres_enabled __read_mostly = 1;
497
498/*
499 * Enable / Disable high resolution mode
500 */
501static int __init setup_hrtimer_hres(char *str)
502{
503 if (!strcmp(str, "off"))
504 hrtimer_hres_enabled = 0;
505 else if (!strcmp(str, "on"))
506 hrtimer_hres_enabled = 1;
507 else
508 return 0;
509 return 1;
510}
511
512__setup("highres=", setup_hrtimer_hres);
513
514/*
515 * hrtimer_high_res_enabled - query, if the highres mode is enabled
516 */
517static inline int hrtimer_is_hres_enabled(void)
518{
519 return hrtimer_hres_enabled;
520}
521
522/*
523 * Is the high resolution mode active ?
524 */
525static inline int hrtimer_hres_active(void)
526{
Christoph Lameter909ea962010-12-08 16:22:55 +0100527 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800528}
529
530/*
531 * Reprogram the event source with checking both queues for the
532 * next event
533 * Called with interrupts disabled and base->lock held
534 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400535static void
536hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800537{
538 int i;
539 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400540 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800541
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400542 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800543
544 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
545 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700546 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800547
John Stultz998adc32010-09-20 19:19:17 -0700548 next = timerqueue_getnext(&base->active);
549 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800550 continue;
John Stultz998adc32010-09-20 19:19:17 -0700551 timer = container_of(next, struct hrtimer, node);
552
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;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400561 if (expires.tv64 < expires_next.tv64)
562 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800563 }
564
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400565 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
566 return;
567
568 cpu_base->expires_next.tv64 = expires_next.tv64;
569
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800570 if (cpu_base->expires_next.tv64 != KTIME_MAX)
571 tick_program_event(cpu_base->expires_next, 1);
572}
573
574/*
575 * Shared reprogramming for clock_realtime and clock_monotonic
576 *
577 * When a timer is enqueued and expires earlier than the already enqueued
578 * timers, we have to check, whether it expires earlier than the timer for
579 * which the clock event device was armed.
580 *
581 * Called with interrupts disabled and base->cpu_base.lock held
582 */
583static int hrtimer_reprogram(struct hrtimer *timer,
584 struct hrtimer_clock_base *base)
585{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100586 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700587 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800588 int res;
589
Arjan van de Vencc584b22008-09-01 15:02:30 -0700590 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100591
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800592 /*
593 * When the callback is running, we do not reprogram the clock event
594 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200595 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800596 * reprogramming is handled either by the softirq, which called the
597 * callback or at the end of the hrtimer_interrupt.
598 */
599 if (hrtimer_callback_running(timer))
600 return 0;
601
Thomas Gleixner63070a72008-02-14 00:58:36 +0100602 /*
603 * CLOCK_REALTIME timer might be requested with an absolute
604 * expiry time which is less than base->offset. Nothing wrong
605 * about that, just avoid to call into the tick code, which
606 * has now objections against negative expiry values.
607 */
608 if (expires.tv64 < 0)
609 return -ETIME;
610
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100611 if (expires.tv64 >= cpu_base->expires_next.tv64)
612 return 0;
613
614 /*
615 * If a hang was detected in the last timer interrupt then we
616 * do not schedule a timer which is earlier than the expiry
617 * which we enforced in the hang detection. We want the system
618 * to make progress.
619 */
620 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800621 return 0;
622
623 /*
624 * Clockevents returns -ETIME, when the event was in the past.
625 */
626 res = tick_program_event(expires, 0);
627 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100628 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800629 return res;
630}
631
Ingo Molnar995f0542007-04-07 12:05:00 +0200632/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800633 * Initialize the high resolution related parts of cpu_base
634 */
635static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
636{
637 base->expires_next.tv64 = KTIME_MAX;
638 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800639}
640
641/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800642 * When High resolution timers are active, try to reprogram. Note, that in case
643 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
644 * check happens. The timer gets enqueued into the rbtree. The reprogramming
645 * and expiry check is done in the hrtimer_interrupt or in the softirq.
646 */
647static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzdd9c58a2013-02-04 14:33:37 +0200648 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800649{
Leonid Shatzdd9c58a2013-02-04 14:33:37 +0200650 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800651}
652
John Stultz6321a0a2012-07-17 02:39:55 -0400653static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
654{
655 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
656 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
657
658 return ktime_get_update_offsets(offs_real, offs_boot);
659}
660
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200661/*
662 * Retrigger next event is called after clock was set
663 *
664 * Called with interrupts disabled via on_each_cpu()
665 */
666static void retrigger_next_event(void *arg)
667{
668 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200669
670 if (!hrtimer_hres_active())
671 return;
672
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200673 raw_spin_lock(&base->lock);
John Stultz6321a0a2012-07-17 02:39:55 -0400674 hrtimer_update_base(base);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200675 hrtimer_force_reprogram(base, 0);
676 raw_spin_unlock(&base->lock);
677}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200678
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800679/*
680 * Switch to high resolution mode
681 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800682static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800683{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200684 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700685 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800686 unsigned long flags;
687
688 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800689 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800690
691 local_irq_save(flags);
692
693 if (tick_init_highres()) {
694 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700695 printk(KERN_WARNING "Could not switch to high resolution "
696 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800697 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800698 }
699 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200700 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
701 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800702
703 tick_setup_sched_timer();
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800704 /* "Retrigger" the interrupt to get things going */
705 retrigger_next_event(NULL);
706 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800707 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800708}
709
Thomas Gleixner39367f42013-07-05 12:09:18 +0200710static void clock_was_set_work(struct work_struct *work)
711{
712 clock_was_set();
713}
714
715static DECLARE_WORK(hrtimer_work, clock_was_set_work);
716
John Stultz5e5006e2012-07-17 02:39:50 -0400717/*
Thomas Gleixner39367f42013-07-05 12:09:18 +0200718 * Called from timekeeping and resume code to reprogramm the hrtimer
719 * interrupt device on all cpus.
John Stultz5e5006e2012-07-17 02:39:50 -0400720 */
721void clock_was_set_delayed(void)
722{
Thomas Gleixner39367f42013-07-05 12:09:18 +0200723 schedule_work(&hrtimer_work);
John Stultz5e5006e2012-07-17 02:39:50 -0400724}
725
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800726#else
727
728static inline int hrtimer_hres_active(void) { return 0; }
729static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800730static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400731static inline void
732hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800733static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzdd9c58a2013-02-04 14:33:37 +0200734 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800735{
736 return 0;
737}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800738static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200739static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800740
741#endif /* CONFIG_HIGH_RES_TIMERS */
742
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200743/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200744 * Clock realtime was set
745 *
746 * Change the offset of the realtime clock vs. the monotonic
747 * clock.
748 *
749 * We might have to reprogram the high resolution timer interrupt. On
750 * SMP we call the architecture specific code to retrigger _all_ high
751 * resolution timer interrupts. On UP we just disable interrupts and
752 * call the high resolution interrupt code.
753 */
754void clock_was_set(void)
755{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200756#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200757 /* Retrigger the CPU local events everywhere */
758 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200759#endif
760 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200761}
762
763/*
764 * During resume we might have to reprogram the high resolution timer
765 * interrupt (on the local CPU):
766 */
767void hrtimers_resume(void)
768{
769 WARN_ONCE(!irqs_disabled(),
770 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
771
Thomas Gleixner39367f42013-07-05 12:09:18 +0200772 /* Retrigger on the local CPU */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200773 retrigger_next_event(NULL);
Thomas Gleixner39367f42013-07-05 12:09:18 +0200774 /* And schedule a retrigger for all others */
775 clock_was_set_delayed();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200776}
777
Heiko Carstens5f201902009-12-10 10:56:29 +0100778static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800779{
Heiko Carstens5f201902009-12-10 10:56:29 +0100780#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800781 if (timer->start_site)
782 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100783 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800784 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
785 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800786#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100787}
788
789static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
790{
791#ifdef CONFIG_TIMER_STATS
792 timer->start_site = NULL;
793#endif
794}
795
796static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
797{
798#ifdef CONFIG_TIMER_STATS
799 if (likely(!timer_stats_active))
800 return;
801 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
802 timer->function, timer->start_comm, 0);
803#endif
804}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800805
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800806/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200807 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800808 */
809static inline
810void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
811{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100812 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800813}
814
815/**
816 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800818 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800819 * @interval: the interval to forward
820 *
821 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700822 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800824u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800825{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800826 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800827 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800828
Arjan van de Vencc584b22008-09-01 15:02:30 -0700829 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800830
831 if (delta.tv64 < 0)
832 return 0;
833
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100834 if (interval.tv64 < timer->base->resolution.tv64)
835 interval.tv64 = timer->base->resolution.tv64;
836
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800837 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800838 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800839
840 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700841 hrtimer_add_expires_ns(timer, incr * orun);
842 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800843 return orun;
844 /*
845 * This (and the ktime_add() below) is the
846 * correction for exact:
847 */
848 orun++;
849 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700850 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800851
852 return orun;
853}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700854EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800855
856/*
857 * enqueue_hrtimer - internal function to (re)start a timer
858 *
859 * The timer is inserted in expiry order. Insertion into the
860 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100861 *
862 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800863 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100864static int enqueue_hrtimer(struct hrtimer *timer,
865 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800866{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800867 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700868
John Stultz998adc32010-09-20 19:19:17 -0700869 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200870 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800871
872 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800873 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
874 * state of a possibly running callback.
875 */
876 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100877
John Stultz998adc32010-09-20 19:19:17 -0700878 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100879}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800880
881/*
882 * __remove_hrtimer - internal function to remove a timer
883 *
884 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800885 *
886 * High resolution timer mode reprograms the clock event device when the
887 * timer is the one which expires next. The caller can disable this by setting
888 * reprogram to zero. This is useful, when the context does a reprogramming
889 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800890 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800891static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800892 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800893 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800894{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800895 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400896 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
897 goto out;
898
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800899 next_timer = timerqueue_getnext(&base->active);
900 timerqueue_del(&base->active, &timer->node);
901 if (&timer->node == next_timer) {
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 }
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200914 if (!timerqueue_getnext(&base->active))
915 base->cpu_base->active_bases &= ~(1 << base->index);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400916out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800917 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800918}
919
920/*
921 * remove hrtimer, called with base lock held
922 */
923static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800924remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800925{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800926 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700927 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800928 int reprogram;
929
930 /*
931 * Remove the timer and force reprogramming when high
932 * resolution mode is active and the timer is on the current
933 * CPU. If we remove a timer on another CPU, reprogramming is
934 * skipped. The interrupt event on this CPU is fired and
935 * reprogramming happens in the interrupt handler. This is a
936 * rare case and less expensive than a smp call.
937 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800938 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800939 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800940 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700941 /*
942 * We must preserve the CALLBACK state flag here,
943 * otherwise we could move the timer base in
944 * switch_hrtimer_base.
945 */
946 state = timer->state & HRTIMER_STATE_CALLBACK;
947 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800948 return 1;
949 }
950 return 0;
951}
952
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100953int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
954 unsigned long delta_ns, const enum hrtimer_mode mode,
955 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800956{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800957 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800958 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100959 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800960
961 base = lock_hrtimer_base(timer, &flags);
962
963 /* Remove an active timer from the queue: */
964 ret = remove_hrtimer(timer, base);
965
966 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530967 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800968
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530969 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100970 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800971 /*
972 * CONFIG_TIME_LOW_RES is a temporary way for architectures
973 * to signal that they simply return xtime in
974 * do_gettimeoffset(). In this case we want to round up by
975 * resolution when starting a relative timer, to avoid short
976 * timeouts. This will go away with the GTOD framework.
977 */
978#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100979 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800980#endif
981 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700982
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700983 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800984
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800985 timer_stats_hrtimer_set_start_info(timer);
986
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100987 leftmost = enqueue_hrtimer(timer, new_base);
988
Ingo Molnar935c6312007-03-28 13:17:18 +0200989 /*
990 * Only allow reprogramming if the new base is on this CPU.
991 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100992 *
993 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200994 */
Leonid Shatzdd9c58a2013-02-04 14:33:37 +0200995 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
996 && hrtimer_enqueue_reprogram(timer, new_base)) {
997 if (wakeup) {
998 /*
999 * We need to drop cpu_base->lock to avoid a
1000 * lock ordering issue vs. rq->lock.
1001 */
1002 raw_spin_unlock(&new_base->cpu_base->lock);
1003 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1004 local_irq_restore(flags);
1005 return ret;
1006 } else {
1007 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1008 }
1009 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001010
1011 unlock_hrtimer_base(timer, &flags);
1012
1013 return ret;
1014}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001015
1016/**
1017 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1018 * @timer: the timer to be added
1019 * @tim: expiry time
1020 * @delta_ns: "slack" range for the timer
1021 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1022 *
1023 * Returns:
1024 * 0 on success
1025 * 1 when the timer was active
1026 */
1027int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1028 unsigned long delta_ns, const enum hrtimer_mode mode)
1029{
1030 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1031}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001032EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1033
1034/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001035 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001036 * @timer: the timer to be added
1037 * @tim: expiry time
1038 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1039 *
1040 * Returns:
1041 * 0 on success
1042 * 1 when the timer was active
1043 */
1044int
1045hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1046{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001047 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001048}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001049EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001050
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001051
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001052/**
1053 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001054 * @timer: hrtimer to stop
1055 *
1056 * Returns:
1057 * 0 when the timer was not active
1058 * 1 when the timer was active
1059 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001060 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001061 */
1062int hrtimer_try_to_cancel(struct hrtimer *timer)
1063{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001064 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001065 unsigned long flags;
1066 int ret = -1;
1067
1068 base = lock_hrtimer_base(timer, &flags);
1069
Thomas Gleixner303e9672007-02-16 01:27:51 -08001070 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001071 ret = remove_hrtimer(timer, base);
1072
1073 unlock_hrtimer_base(timer, &flags);
1074
1075 return ret;
1076
1077}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001078EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001079
1080/**
1081 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001082 * @timer: the timer to be cancelled
1083 *
1084 * Returns:
1085 * 0 when the timer was not active
1086 * 1 when the timer was active
1087 */
1088int hrtimer_cancel(struct hrtimer *timer)
1089{
1090 for (;;) {
1091 int ret = hrtimer_try_to_cancel(timer);
1092
1093 if (ret >= 0)
1094 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001095 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001096 }
1097}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001098EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001099
1100/**
1101 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001102 * @timer: the timer to read
1103 */
1104ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1105{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001106 unsigned long flags;
1107 ktime_t rem;
1108
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001109 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001110 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001111 unlock_hrtimer_base(timer, &flags);
1112
1113 return rem;
1114}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001115EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001116
Russell Kingee9c5782008-04-20 13:59:33 +01001117#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001118/**
1119 * hrtimer_get_next_event - get the time until next expiry event
1120 *
1121 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1122 * is pending.
1123 */
1124ktime_t hrtimer_get_next_event(void)
1125{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001126 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1127 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001128 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1129 unsigned long flags;
1130 int i;
1131
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001132 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001133
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001134 if (!hrtimer_hres_active()) {
1135 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1136 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001137 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001138
John Stultz998adc32010-09-20 19:19:17 -07001139 next = timerqueue_getnext(&base->active);
1140 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001141 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001142
John Stultz998adc32010-09-20 19:19:17 -07001143 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001144 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001145 delta = ktime_sub(delta, base->get_time());
1146 if (delta.tv64 < mindelta.tv64)
1147 mindelta.tv64 = delta.tv64;
1148 }
Tony Lindgren69239742006-03-06 15:42:45 -08001149 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001150
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001151 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001152
Tony Lindgren69239742006-03-06 15:42:45 -08001153 if (mindelta.tv64 < 0)
1154 mindelta.tv64 = 0;
1155 return mindelta;
1156}
1157#endif
1158
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001159static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1160 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001161{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001162 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001163 int base;
George Anzinger79786722006-02-01 03:05:11 -08001164
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001165 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001166
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001167 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001168
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001169 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001170 clock_id = CLOCK_MONOTONIC;
1171
John Stultze06383d2010-12-14 19:37:07 -08001172 base = hrtimer_clockid_to_base(clock_id);
1173 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001174 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001175
1176#ifdef CONFIG_TIMER_STATS
1177 timer->start_site = NULL;
1178 timer->start_pid = -1;
1179 memset(timer->start_comm, 0, TASK_COMM_LEN);
1180#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001181}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001182
1183/**
1184 * hrtimer_init - initialize a timer to the given clock
1185 * @timer: the timer to be initialized
1186 * @clock_id: the clock to be used
1187 * @mode: timer mode abs/rel
1188 */
1189void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1190 enum hrtimer_mode mode)
1191{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001192 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001193 __hrtimer_init(timer, clock_id, mode);
1194}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001195EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001196
1197/**
1198 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001199 * @which_clock: which clock to query
1200 * @tp: pointer to timespec variable to store the resolution
1201 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001202 * Store the resolution of the clock selected by @which_clock in the
1203 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001204 */
1205int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1206{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001207 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001208 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001209
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001210 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001211 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001212
1213 return 0;
1214}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001215EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001216
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001217static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001218{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001219 struct hrtimer_clock_base *base = timer->base;
1220 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1221 enum hrtimer_restart (*fn)(struct hrtimer *);
1222 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001223
Peter Zijlstraca109492008-11-25 12:43:51 +01001224 WARN_ON(!irqs_disabled());
1225
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001226 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001227 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1228 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001229 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001230
1231 /*
1232 * Because we run timers from hardirq context, there is no chance
1233 * they get migrated to another cpu, therefore its safe to unlock
1234 * the timer base.
1235 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001236 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001237 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001238 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001239 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001240 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001241
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001242 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001243 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1244 * we do not reprogramm the event hardware. Happens either in
1245 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001246 */
1247 if (restart != HRTIMER_NORESTART) {
1248 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001249 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001250 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001251
1252 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1253
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001254 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001255}
1256
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001257#ifdef CONFIG_HIGH_RES_TIMERS
1258
1259/*
1260 * High resolution timer interrupt
1261 * Called with interrupts disabled
1262 */
1263void hrtimer_interrupt(struct clock_event_device *dev)
1264{
1265 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001266 ktime_t expires_next, now, entry_time, delta;
1267 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001268
1269 BUG_ON(!cpu_base->hres_active);
1270 cpu_base->nr_events++;
1271 dev->next_event.tv64 = KTIME_MAX;
1272
Thomas Gleixnerdd3cded2012-07-17 02:39:53 -04001273 raw_spin_lock(&cpu_base->lock);
John Stultz6321a0a2012-07-17 02:39:55 -04001274 entry_time = now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001275retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001276 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001277 /*
1278 * We set expires_next to KTIME_MAX here with cpu_base->lock
1279 * held to prevent that a timer is enqueued in our queue via
1280 * the migration code. This does not affect enqueueing of
1281 * timers which run their callback and need to be requeued on
1282 * this CPU.
1283 */
1284 cpu_base->expires_next.tv64 = KTIME_MAX;
1285
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001286 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001287 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001288 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001289 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001290
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001291 if (!(cpu_base->active_bases & (1 << i)))
1292 continue;
1293
1294 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001295 basenow = ktime_add(now, base->offset);
1296
John Stultz998adc32010-09-20 19:19:17 -07001297 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001298 struct hrtimer *timer;
1299
John Stultz998adc32010-09-20 19:19:17 -07001300 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001301
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001302 /*
1303 * The immediate goal for using the softexpires is
1304 * minimizing wakeups, not running timers at the
1305 * earliest interrupt after their soft expiration.
1306 * This allows us to avoid using a Priority Search
1307 * Tree, which can answer a stabbing querry for
1308 * overlapping intervals and instead use the simple
1309 * BST we already have.
1310 * We don't add extra wakeups by delaying timers that
1311 * are right-of a not yet expired timer, because that
1312 * timer will have to trigger a wakeup anyway.
1313 */
1314
1315 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001316 ktime_t expires;
1317
Arjan van de Vencc584b22008-09-01 15:02:30 -07001318 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001319 base->offset);
Prarit Bhargava194d30b2013-04-08 08:47:15 -04001320 if (expires.tv64 < 0)
1321 expires.tv64 = KTIME_MAX;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001322 if (expires.tv64 < expires_next.tv64)
1323 expires_next = expires;
1324 break;
1325 }
1326
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001327 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001328 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001329 }
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.
John Stultz6321a0a2012-07-17 02:39:55 -04001354 *
1355 * Acquire base lock for updating the offsets and retrieving
1356 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001357 */
Thomas Gleixnerdd3cded2012-07-17 02:39:53 -04001358 raw_spin_lock(&cpu_base->lock);
John Stultz6321a0a2012-07-17 02:39:55 -04001359 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001360 cpu_base->nr_retries++;
1361 if (++retries < 3)
1362 goto retry;
1363 /*
1364 * Give the system a chance to do something else than looping
1365 * here. We stored the entry time, so we know exactly how long
1366 * we spent here. We schedule the next event this amount of
1367 * time away.
1368 */
1369 cpu_base->nr_hangs++;
1370 cpu_base->hang_detected = 1;
Thomas Gleixnerdd3cded2012-07-17 02:39:53 -04001371 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001372 delta = ktime_sub(now, entry_time);
1373 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1374 cpu_base->max_hang_time = delta;
1375 /*
1376 * Limit it to a sensible value as we enforce a longer
1377 * delay. Give the CPU at least 100ms to catch up.
1378 */
1379 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1380 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1381 else
1382 expires_next = ktime_add(now, delta);
1383 tick_program_event(expires_next, 1);
1384 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1385 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001386}
1387
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001388/*
1389 * local version of hrtimer_peek_ahead_timers() called with interrupts
1390 * disabled.
1391 */
1392static void __hrtimer_peek_ahead_timers(void)
1393{
1394 struct tick_device *td;
1395
1396 if (!hrtimer_hres_active())
1397 return;
1398
1399 td = &__get_cpu_var(tick_cpu_device);
1400 if (td && td->evtdev)
1401 hrtimer_interrupt(td->evtdev);
1402}
1403
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001404/**
1405 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1406 *
1407 * hrtimer_peek_ahead_timers will peek at the timer queue of
1408 * the current cpu and check if there are any timers for which
1409 * the soft expires time has passed. If any such timers exist,
1410 * they are run immediately and then removed from the timer queue.
1411 *
1412 */
1413void hrtimer_peek_ahead_timers(void)
1414{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001415 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001416
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001417 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001418 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001419 local_irq_restore(flags);
1420}
1421
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001422static void run_hrtimer_softirq(struct softirq_action *h)
1423{
1424 hrtimer_peek_ahead_timers();
1425}
1426
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001427#else /* CONFIG_HIGH_RES_TIMERS */
1428
1429static inline void __hrtimer_peek_ahead_timers(void) { }
1430
1431#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001432
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001433/*
1434 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001435 *
1436 * For HRT its the fall back code to run the softirq in the timer
1437 * softirq context in case the hrtimer initialization failed or has
1438 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001439 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001440void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001441{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001442 if (hrtimer_hres_active())
1443 return;
1444
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001445 /*
1446 * This _is_ ugly: We have to check in the softirq context,
1447 * whether we can switch to highres and / or nohz mode. The
1448 * clocksource switch happens in the timer interrupt with
1449 * xtime_lock held. Notification from there only sets the
1450 * check bit in the tick_oneshot code, otherwise we might
1451 * deadlock vs. xtime_lock.
1452 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001453 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001454 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001455}
1456
1457/*
1458 * Called from hardirq context every jiffy
1459 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001460void hrtimer_run_queues(void)
1461{
John Stultz998adc32010-09-20 19:19:17 -07001462 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001463 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001464 struct hrtimer_clock_base *base;
1465 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001466
1467 if (hrtimer_hres_active())
1468 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001469
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001470 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1471 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001472 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001473 continue;
1474
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001475 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001476 hrtimer_get_softirq_time(cpu_base);
1477 gettime = 0;
1478 }
1479
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001480 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001481
John Stultzb007c382010-12-10 22:19:53 -08001482 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001483 struct hrtimer *timer;
1484
John Stultz998adc32010-09-20 19:19:17 -07001485 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001486 if (base->softirq_time.tv64 <=
1487 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001488 break;
1489
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001490 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001491 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001492 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001493 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001494}
1495
1496/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001497 * Sleep related functions:
1498 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001499static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001500{
1501 struct hrtimer_sleeper *t =
1502 container_of(timer, struct hrtimer_sleeper, timer);
1503 struct task_struct *task = t->task;
1504
1505 t->task = NULL;
1506 if (task)
1507 wake_up_process(task);
1508
1509 return HRTIMER_NORESTART;
1510}
1511
Ingo Molnar36c8b582006-07-03 00:25:41 -07001512void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001513{
1514 sl->timer.function = hrtimer_wakeup;
1515 sl->task = task;
1516}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001517EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001518
Thomas Gleixner669d7862006-03-31 02:31:19 -08001519static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001520{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001521 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001522
Roman Zippel432569b2006-03-26 01:38:08 -08001523 do {
1524 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001525 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001526 if (!hrtimer_active(&t->timer))
1527 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001528
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001529 if (likely(t->task))
1530 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001531
Thomas Gleixner669d7862006-03-31 02:31:19 -08001532 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001533 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001534
Thomas Gleixner669d7862006-03-31 02:31:19 -08001535 } while (t->task && !signal_pending(current));
1536
Peter Zijlstra3588a082008-02-01 17:45:13 +01001537 __set_current_state(TASK_RUNNING);
1538
Thomas Gleixner669d7862006-03-31 02:31:19 -08001539 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001540}
1541
Oleg Nesterov080344b2008-02-01 17:29:05 +03001542static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1543{
1544 struct timespec rmt;
1545 ktime_t rem;
1546
Arjan van de Vencc584b22008-09-01 15:02:30 -07001547 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001548 if (rem.tv64 <= 0)
1549 return 0;
1550 rmt = ktime_to_timespec(rem);
1551
1552 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1553 return -EFAULT;
1554
1555 return 1;
1556}
1557
Toyo Abe1711ef32006-09-29 02:00:28 -07001558long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001559{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001560 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001561 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001562 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001563
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001564 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001565 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001566 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001567
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001568 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001569 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001570
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001571 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001572 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001573 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001574 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001575 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001576 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001577
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001578 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001579 ret = -ERESTART_RESTARTBLOCK;
1580out:
1581 destroy_hrtimer_on_stack(&t.timer);
1582 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001583}
1584
Oleg Nesterov080344b2008-02-01 17:29:05 +03001585long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001586 const enum hrtimer_mode mode, const clockid_t clockid)
1587{
1588 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001589 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001590 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001591 unsigned long slack;
1592
1593 slack = current->timer_slack_ns;
1594 if (rt_task(current))
1595 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001596
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001597 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001598 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001599 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001600 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001601
George Anzinger79786722006-02-01 03:05:11 -08001602 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001603 if (mode == HRTIMER_MODE_ABS) {
1604 ret = -ERESTARTNOHAND;
1605 goto out;
1606 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001607
Roman Zippel432569b2006-03-26 01:38:08 -08001608 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001609 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001610 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001611 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001612 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001613
1614 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001615 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001616 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001617 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001618 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001619
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001620 ret = -ERESTART_RESTARTBLOCK;
1621out:
1622 destroy_hrtimer_on_stack(&t.timer);
1623 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001624}
1625
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001626SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1627 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001628{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001629 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001630
1631 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1632 return -EFAULT;
1633
1634 if (!timespec_valid(&tu))
1635 return -EINVAL;
1636
Oleg Nesterov080344b2008-02-01 17:29:05 +03001637 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001638}
1639
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001640/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001641 * Functions related to boot-time initialization:
1642 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001643static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001644{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001645 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001646 int i;
1647
John Stultz998adc32010-09-20 19:19:17 -07001648 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001649 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001650 timerqueue_init_head(&cpu_base->clock_base[i].active);
1651 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001652
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001653 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001654}
1655
1656#ifdef CONFIG_HOTPLUG_CPU
1657
Peter Zijlstraca109492008-11-25 12:43:51 +01001658static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001659 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001660{
1661 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001662 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001663
John Stultz998adc32010-09-20 19:19:17 -07001664 while ((node = timerqueue_getnext(&old_base->active))) {
1665 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001666 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001667 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001668
1669 /*
1670 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1671 * timer could be seen as !active and just vanish away
1672 * under us on another CPU
1673 */
1674 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001675 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001676 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001677 * Enqueue the timers on the new cpu. This does not
1678 * reprogram the event device in case the timer
1679 * expires before the earliest on this CPU, but we run
1680 * hrtimer_interrupt after we migrated everything to
1681 * sort out already expired timers and reprogram the
1682 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001683 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001684 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001685
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001686 /* Clear the migration state bit */
1687 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001688 }
1689}
1690
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001691static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001692{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001693 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001694 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001695
Peter Zijlstra37810652008-12-04 11:17:10 +01001696 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001697 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001698
1699 local_irq_disable();
1700 old_base = &per_cpu(hrtimer_bases, scpu);
1701 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001702 /*
1703 * The caller is globally serialized and nobody else
1704 * takes two locks at once, deadlock is not possible.
1705 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001706 raw_spin_lock(&new_base->lock);
1707 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001708
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001709 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001710 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001711 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001712 }
1713
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001714 raw_spin_unlock(&old_base->lock);
1715 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001716
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001717 /* Check, if we got expired work to do */
1718 __hrtimer_peek_ahead_timers();
1719 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001720}
1721
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001722#endif /* CONFIG_HOTPLUG_CPU */
1723
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001724static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001725 unsigned long action, void *hcpu)
1726{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001727 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001728
1729 switch (action) {
1730
1731 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001732 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001733 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001734 break;
1735
1736#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001737 case CPU_DYING:
1738 case CPU_DYING_FROZEN:
1739 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1740 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001741 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001742 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001743 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001744 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001745 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001746 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001747 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001748#endif
1749
1750 default:
1751 break;
1752 }
1753
1754 return NOTIFY_OK;
1755}
1756
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001757static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001758 .notifier_call = hrtimer_cpu_notify,
1759};
1760
1761void __init hrtimers_init(void)
1762{
1763 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1764 (void *)(long)smp_processor_id());
1765 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001766#ifdef CONFIG_HIGH_RES_TIMERS
1767 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1768#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001769}
1770
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001771/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001772 * schedule_hrtimeout_range_clock - sleep until timeout
1773 * @expires: timeout value (ktime_t)
1774 * @delta: slack in expires timeout (ktime_t)
1775 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1776 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1777 */
1778int __sched
1779schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1780 const enum hrtimer_mode mode, int clock)
1781{
1782 struct hrtimer_sleeper t;
1783
1784 /*
1785 * Optimize when a zero timeout value is given. It does not
1786 * matter whether this is an absolute or a relative time.
1787 */
1788 if (expires && !expires->tv64) {
1789 __set_current_state(TASK_RUNNING);
1790 return 0;
1791 }
1792
1793 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001794 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001795 */
1796 if (!expires) {
1797 schedule();
1798 __set_current_state(TASK_RUNNING);
1799 return -EINTR;
1800 }
1801
1802 hrtimer_init_on_stack(&t.timer, clock, mode);
1803 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1804
1805 hrtimer_init_sleeper(&t, current);
1806
1807 hrtimer_start_expires(&t.timer, mode);
1808 if (!hrtimer_active(&t.timer))
1809 t.task = NULL;
1810
1811 if (likely(t.task))
1812 schedule();
1813
1814 hrtimer_cancel(&t.timer);
1815 destroy_hrtimer_on_stack(&t.timer);
1816
1817 __set_current_state(TASK_RUNNING);
1818
1819 return !t.task ? 0 : -EINTR;
1820}
1821
1822/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001823 * schedule_hrtimeout_range - sleep until timeout
1824 * @expires: timeout value (ktime_t)
1825 * @delta: slack in expires timeout (ktime_t)
1826 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1827 *
1828 * Make the current task sleep until the given expiry time has
1829 * elapsed. The routine will return immediately unless
1830 * the current task state has been set (see set_current_state()).
1831 *
1832 * The @delta argument gives the kernel the freedom to schedule the
1833 * actual wakeup to a time that is both power and performance friendly.
1834 * The kernel give the normal best effort behavior for "@expires+@delta",
1835 * but may decide to fire the timer earlier, but no earlier than @expires.
1836 *
1837 * You can set the task state as follows -
1838 *
1839 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1840 * pass before the routine returns.
1841 *
1842 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1843 * delivered to the current task.
1844 *
1845 * The current task state is guaranteed to be TASK_RUNNING when this
1846 * routine returns.
1847 *
1848 * Returns 0 when the timer has expired otherwise -EINTR
1849 */
1850int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001851 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001852{
Carsten Emde351b3f72010-04-02 22:40:19 +02001853 return schedule_hrtimeout_range_clock(expires, delta, mode,
1854 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001855}
1856EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1857
1858/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001859 * schedule_hrtimeout - sleep until timeout
1860 * @expires: timeout value (ktime_t)
1861 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1862 *
1863 * Make the current task sleep until the given expiry time has
1864 * elapsed. The routine will return immediately unless
1865 * the current task state has been set (see set_current_state()).
1866 *
1867 * You can set the task state as follows -
1868 *
1869 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1870 * pass before the routine returns.
1871 *
1872 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1873 * delivered to the current task.
1874 *
1875 * The current task state is guaranteed to be TASK_RUNNING when this
1876 * routine returns.
1877 *
1878 * Returns 0 when the timer has expired otherwise -EINTR
1879 */
1880int __sched schedule_hrtimeout(ktime_t *expires,
1881 const enum hrtimer_mode mode)
1882{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001883 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001884}
1885EXPORT_SYMBOL_GPL(schedule_hrtimeout);