blob: 60f7e326b6c8affc04d6e18c398db9a3abfdb26f [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
John Stultz5e5006e2012-07-17 02:39:50 -0400710/*
711 * Called from timekeeping code to reprogramm the hrtimer interrupt
712 * device. If called from the timer interrupt context we defer it to
713 * softirq context.
714 */
715void clock_was_set_delayed(void)
716{
717 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
718
719 cpu_base->clock_was_set = 1;
720 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
721}
722
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800723#else
724
725static inline int hrtimer_hres_active(void) { return 0; }
726static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800727static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400728static inline void
729hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800730static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzdd9c58a2013-02-04 14:33:37 +0200731 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800732{
733 return 0;
734}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800735static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200736static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800737
738#endif /* CONFIG_HIGH_RES_TIMERS */
739
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200740/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200741 * Clock realtime was set
742 *
743 * Change the offset of the realtime clock vs. the monotonic
744 * clock.
745 *
746 * We might have to reprogram the high resolution timer interrupt. On
747 * SMP we call the architecture specific code to retrigger _all_ high
748 * resolution timer interrupts. On UP we just disable interrupts and
749 * call the high resolution interrupt code.
750 */
751void clock_was_set(void)
752{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200753#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200754 /* Retrigger the CPU local events everywhere */
755 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200756#endif
757 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200758}
759
760/*
761 * During resume we might have to reprogram the high resolution timer
762 * interrupt (on the local CPU):
763 */
764void hrtimers_resume(void)
765{
766 WARN_ONCE(!irqs_disabled(),
767 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
768
769 retrigger_next_event(NULL);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200770 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200771}
772
Heiko Carstens5f201902009-12-10 10:56:29 +0100773static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800774{
Heiko Carstens5f201902009-12-10 10:56:29 +0100775#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800776 if (timer->start_site)
777 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100778 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800779 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
780 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800781#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100782}
783
784static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
785{
786#ifdef CONFIG_TIMER_STATS
787 timer->start_site = NULL;
788#endif
789}
790
791static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
792{
793#ifdef CONFIG_TIMER_STATS
794 if (likely(!timer_stats_active))
795 return;
796 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
797 timer->function, timer->start_comm, 0);
798#endif
799}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800800
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800801/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200802 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800803 */
804static inline
805void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
806{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100807 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800808}
809
810/**
811 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800813 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800814 * @interval: the interval to forward
815 *
816 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700817 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800818 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800819u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800820{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800821 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800822 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823
Arjan van de Vencc584b22008-09-01 15:02:30 -0700824 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800825
826 if (delta.tv64 < 0)
827 return 0;
828
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100829 if (interval.tv64 < timer->base->resolution.tv64)
830 interval.tv64 = timer->base->resolution.tv64;
831
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800833 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800834
835 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700836 hrtimer_add_expires_ns(timer, incr * orun);
837 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800838 return orun;
839 /*
840 * This (and the ktime_add() below) is the
841 * correction for exact:
842 */
843 orun++;
844 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700845 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800846
847 return orun;
848}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700849EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800850
851/*
852 * enqueue_hrtimer - internal function to (re)start a timer
853 *
854 * The timer is inserted in expiry order. Insertion into the
855 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100856 *
857 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800858 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100859static int enqueue_hrtimer(struct hrtimer *timer,
860 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800861{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800862 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700863
John Stultz998adc32010-09-20 19:19:17 -0700864 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200865 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800866
867 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800868 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
869 * state of a possibly running callback.
870 */
871 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100872
John Stultz998adc32010-09-20 19:19:17 -0700873 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100874}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800875
876/*
877 * __remove_hrtimer - internal function to remove a timer
878 *
879 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800880 *
881 * High resolution timer mode reprograms the clock event device when the
882 * timer is the one which expires next. The caller can disable this by setting
883 * reprogram to zero. This is useful, when the context does a reprogramming
884 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800885 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800886static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800887 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800888 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800889{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800890 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400891 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
892 goto out;
893
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800894 next_timer = timerqueue_getnext(&base->active);
895 timerqueue_del(&base->active, &timer->node);
896 if (&timer->node == next_timer) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400897#ifdef CONFIG_HIGH_RES_TIMERS
898 /* Reprogram the clock event device. if enabled */
899 if (reprogram && hrtimer_hres_active()) {
900 ktime_t expires;
901
902 expires = ktime_sub(hrtimer_get_expires(timer),
903 base->offset);
904 if (base->cpu_base->expires_next.tv64 == expires.tv64)
905 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800906 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400907#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800908 }
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200909 if (!timerqueue_getnext(&base->active))
910 base->cpu_base->active_bases &= ~(1 << base->index);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400911out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800912 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800913}
914
915/*
916 * remove hrtimer, called with base lock held
917 */
918static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800919remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800920{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800921 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700922 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800923 int reprogram;
924
925 /*
926 * Remove the timer and force reprogramming when high
927 * resolution mode is active and the timer is on the current
928 * CPU. If we remove a timer on another CPU, reprogramming is
929 * skipped. The interrupt event on this CPU is fired and
930 * reprogramming happens in the interrupt handler. This is a
931 * rare case and less expensive than a smp call.
932 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800933 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800934 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800935 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700936 /*
937 * We must preserve the CALLBACK state flag here,
938 * otherwise we could move the timer base in
939 * switch_hrtimer_base.
940 */
941 state = timer->state & HRTIMER_STATE_CALLBACK;
942 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800943 return 1;
944 }
945 return 0;
946}
947
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100948int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
949 unsigned long delta_ns, const enum hrtimer_mode mode,
950 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800951{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800952 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800953 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100954 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800955
956 base = lock_hrtimer_base(timer, &flags);
957
958 /* Remove an active timer from the queue: */
959 ret = remove_hrtimer(timer, base);
960
961 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530962 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800963
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530964 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100965 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800966 /*
967 * CONFIG_TIME_LOW_RES is a temporary way for architectures
968 * to signal that they simply return xtime in
969 * do_gettimeoffset(). In this case we want to round up by
970 * resolution when starting a relative timer, to avoid short
971 * timeouts. This will go away with the GTOD framework.
972 */
973#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100974 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800975#endif
976 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700977
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700978 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800979
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800980 timer_stats_hrtimer_set_start_info(timer);
981
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100982 leftmost = enqueue_hrtimer(timer, new_base);
983
Ingo Molnar935c6312007-03-28 13:17:18 +0200984 /*
985 * Only allow reprogramming if the new base is on this CPU.
986 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100987 *
988 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200989 */
Leonid Shatzdd9c58a2013-02-04 14:33:37 +0200990 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
991 && hrtimer_enqueue_reprogram(timer, new_base)) {
992 if (wakeup) {
993 /*
994 * We need to drop cpu_base->lock to avoid a
995 * lock ordering issue vs. rq->lock.
996 */
997 raw_spin_unlock(&new_base->cpu_base->lock);
998 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
999 local_irq_restore(flags);
1000 return ret;
1001 } else {
1002 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1003 }
1004 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001005
1006 unlock_hrtimer_base(timer, &flags);
1007
1008 return ret;
1009}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001010
1011/**
1012 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1013 * @timer: the timer to be added
1014 * @tim: expiry time
1015 * @delta_ns: "slack" range for the timer
1016 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1017 *
1018 * Returns:
1019 * 0 on success
1020 * 1 when the timer was active
1021 */
1022int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1023 unsigned long delta_ns, const enum hrtimer_mode mode)
1024{
1025 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1026}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001027EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1028
1029/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001030 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001031 * @timer: the timer to be added
1032 * @tim: expiry time
1033 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1034 *
1035 * Returns:
1036 * 0 on success
1037 * 1 when the timer was active
1038 */
1039int
1040hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1041{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001042 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001043}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001044EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001045
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001046
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001047/**
1048 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001049 * @timer: hrtimer to stop
1050 *
1051 * Returns:
1052 * 0 when the timer was not active
1053 * 1 when the timer was active
1054 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001055 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001056 */
1057int hrtimer_try_to_cancel(struct hrtimer *timer)
1058{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001059 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001060 unsigned long flags;
1061 int ret = -1;
1062
1063 base = lock_hrtimer_base(timer, &flags);
1064
Thomas Gleixner303e9672007-02-16 01:27:51 -08001065 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001066 ret = remove_hrtimer(timer, base);
1067
1068 unlock_hrtimer_base(timer, &flags);
1069
1070 return ret;
1071
1072}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001073EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001074
1075/**
1076 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001077 * @timer: the timer to be cancelled
1078 *
1079 * Returns:
1080 * 0 when the timer was not active
1081 * 1 when the timer was active
1082 */
1083int hrtimer_cancel(struct hrtimer *timer)
1084{
1085 for (;;) {
1086 int ret = hrtimer_try_to_cancel(timer);
1087
1088 if (ret >= 0)
1089 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001090 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001091 }
1092}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001093EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001094
1095/**
1096 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001097 * @timer: the timer to read
1098 */
1099ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1100{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001101 unsigned long flags;
1102 ktime_t rem;
1103
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001104 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001105 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001106 unlock_hrtimer_base(timer, &flags);
1107
1108 return rem;
1109}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001110EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001111
Russell Kingee9c5782008-04-20 13:59:33 +01001112#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001113/**
1114 * hrtimer_get_next_event - get the time until next expiry event
1115 *
1116 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1117 * is pending.
1118 */
1119ktime_t hrtimer_get_next_event(void)
1120{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001121 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1122 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001123 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1124 unsigned long flags;
1125 int i;
1126
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001127 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001128
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001129 if (!hrtimer_hres_active()) {
1130 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1131 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001132 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001133
John Stultz998adc32010-09-20 19:19:17 -07001134 next = timerqueue_getnext(&base->active);
1135 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001136 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001137
John Stultz998adc32010-09-20 19:19:17 -07001138 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001139 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001140 delta = ktime_sub(delta, base->get_time());
1141 if (delta.tv64 < mindelta.tv64)
1142 mindelta.tv64 = delta.tv64;
1143 }
Tony Lindgren69239742006-03-06 15:42:45 -08001144 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001145
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001146 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001147
Tony Lindgren69239742006-03-06 15:42:45 -08001148 if (mindelta.tv64 < 0)
1149 mindelta.tv64 = 0;
1150 return mindelta;
1151}
1152#endif
1153
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001154static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1155 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001156{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001157 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001158 int base;
George Anzinger79786722006-02-01 03:05:11 -08001159
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001160 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001161
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001162 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001163
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001164 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001165 clock_id = CLOCK_MONOTONIC;
1166
John Stultze06383d2010-12-14 19:37:07 -08001167 base = hrtimer_clockid_to_base(clock_id);
1168 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001169 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001170
1171#ifdef CONFIG_TIMER_STATS
1172 timer->start_site = NULL;
1173 timer->start_pid = -1;
1174 memset(timer->start_comm, 0, TASK_COMM_LEN);
1175#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001176}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001177
1178/**
1179 * hrtimer_init - initialize a timer to the given clock
1180 * @timer: the timer to be initialized
1181 * @clock_id: the clock to be used
1182 * @mode: timer mode abs/rel
1183 */
1184void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1185 enum hrtimer_mode mode)
1186{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001187 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001188 __hrtimer_init(timer, clock_id, mode);
1189}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001190EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001191
1192/**
1193 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001194 * @which_clock: which clock to query
1195 * @tp: pointer to timespec variable to store the resolution
1196 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001197 * Store the resolution of the clock selected by @which_clock in the
1198 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001199 */
1200int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1201{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001202 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001203 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001204
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001205 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001206 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001207
1208 return 0;
1209}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001210EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001211
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001212static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001213{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001214 struct hrtimer_clock_base *base = timer->base;
1215 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1216 enum hrtimer_restart (*fn)(struct hrtimer *);
1217 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001218
Peter Zijlstraca109492008-11-25 12:43:51 +01001219 WARN_ON(!irqs_disabled());
1220
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001221 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001222 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1223 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001224 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001225
1226 /*
1227 * Because we run timers from hardirq context, there is no chance
1228 * they get migrated to another cpu, therefore its safe to unlock
1229 * the timer base.
1230 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001231 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001232 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001233 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001234 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001235 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001236
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001237 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001238 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1239 * we do not reprogramm the event hardware. Happens either in
1240 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001241 */
1242 if (restart != HRTIMER_NORESTART) {
1243 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001244 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001245 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001246
1247 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1248
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001249 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001250}
1251
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001252#ifdef CONFIG_HIGH_RES_TIMERS
1253
1254/*
1255 * High resolution timer interrupt
1256 * Called with interrupts disabled
1257 */
1258void hrtimer_interrupt(struct clock_event_device *dev)
1259{
1260 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001261 ktime_t expires_next, now, entry_time, delta;
1262 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001263
1264 BUG_ON(!cpu_base->hres_active);
1265 cpu_base->nr_events++;
1266 dev->next_event.tv64 = KTIME_MAX;
1267
Thomas Gleixnerdd3cded2012-07-17 02:39:53 -04001268 raw_spin_lock(&cpu_base->lock);
John Stultz6321a0a2012-07-17 02:39:55 -04001269 entry_time = now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001270retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001271 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001272 /*
1273 * We set expires_next to KTIME_MAX here with cpu_base->lock
1274 * held to prevent that a timer is enqueued in our queue via
1275 * the migration code. This does not affect enqueueing of
1276 * timers which run their callback and need to be requeued on
1277 * this CPU.
1278 */
1279 cpu_base->expires_next.tv64 = KTIME_MAX;
1280
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001281 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001282 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001283 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001284 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001285
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001286 if (!(cpu_base->active_bases & (1 << i)))
1287 continue;
1288
1289 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001290 basenow = ktime_add(now, base->offset);
1291
John Stultz998adc32010-09-20 19:19:17 -07001292 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001293 struct hrtimer *timer;
1294
John Stultz998adc32010-09-20 19:19:17 -07001295 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001296
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001297 /*
1298 * The immediate goal for using the softexpires is
1299 * minimizing wakeups, not running timers at the
1300 * earliest interrupt after their soft expiration.
1301 * This allows us to avoid using a Priority Search
1302 * Tree, which can answer a stabbing querry for
1303 * overlapping intervals and instead use the simple
1304 * BST we already have.
1305 * We don't add extra wakeups by delaying timers that
1306 * are right-of a not yet expired timer, because that
1307 * timer will have to trigger a wakeup anyway.
1308 */
1309
1310 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001311 ktime_t expires;
1312
Arjan van de Vencc584b22008-09-01 15:02:30 -07001313 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001314 base->offset);
Prarit Bhargava194d30b2013-04-08 08:47:15 -04001315 if (expires.tv64 < 0)
1316 expires.tv64 = KTIME_MAX;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001317 if (expires.tv64 < expires_next.tv64)
1318 expires_next = expires;
1319 break;
1320 }
1321
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001322 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001323 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001324 }
1325
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001326 /*
1327 * Store the new expiry value so the migration code can verify
1328 * against it.
1329 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001330 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001331 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001332
1333 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001334 if (expires_next.tv64 == KTIME_MAX ||
1335 !tick_program_event(expires_next, 0)) {
1336 cpu_base->hang_detected = 0;
1337 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001338 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001339
1340 /*
1341 * The next timer was already expired due to:
1342 * - tracing
1343 * - long lasting callbacks
1344 * - being scheduled away when running in a VM
1345 *
1346 * We need to prevent that we loop forever in the hrtimer
1347 * interrupt routine. We give it 3 attempts to avoid
1348 * overreacting on some spurious event.
John Stultz6321a0a2012-07-17 02:39:55 -04001349 *
1350 * Acquire base lock for updating the offsets and retrieving
1351 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001352 */
Thomas Gleixnerdd3cded2012-07-17 02:39:53 -04001353 raw_spin_lock(&cpu_base->lock);
John Stultz6321a0a2012-07-17 02:39:55 -04001354 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001355 cpu_base->nr_retries++;
1356 if (++retries < 3)
1357 goto retry;
1358 /*
1359 * Give the system a chance to do something else than looping
1360 * here. We stored the entry time, so we know exactly how long
1361 * we spent here. We schedule the next event this amount of
1362 * time away.
1363 */
1364 cpu_base->nr_hangs++;
1365 cpu_base->hang_detected = 1;
Thomas Gleixnerdd3cded2012-07-17 02:39:53 -04001366 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001367 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{
John Stultz5e5006e2012-07-17 02:39:50 -04001419 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1420
1421 if (cpu_base->clock_was_set) {
1422 cpu_base->clock_was_set = 0;
1423 clock_was_set();
1424 }
1425
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001426 hrtimer_peek_ahead_timers();
1427}
1428
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001429#else /* CONFIG_HIGH_RES_TIMERS */
1430
1431static inline void __hrtimer_peek_ahead_timers(void) { }
1432
1433#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001434
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001435/*
1436 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001437 *
1438 * For HRT its the fall back code to run the softirq in the timer
1439 * softirq context in case the hrtimer initialization failed or has
1440 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001441 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001442void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001443{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001444 if (hrtimer_hres_active())
1445 return;
1446
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001447 /*
1448 * This _is_ ugly: We have to check in the softirq context,
1449 * whether we can switch to highres and / or nohz mode. The
1450 * clocksource switch happens in the timer interrupt with
1451 * xtime_lock held. Notification from there only sets the
1452 * check bit in the tick_oneshot code, otherwise we might
1453 * deadlock vs. xtime_lock.
1454 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001455 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001456 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001457}
1458
1459/*
1460 * Called from hardirq context every jiffy
1461 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001462void hrtimer_run_queues(void)
1463{
John Stultz998adc32010-09-20 19:19:17 -07001464 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001465 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001466 struct hrtimer_clock_base *base;
1467 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001468
1469 if (hrtimer_hres_active())
1470 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001471
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001472 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1473 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001474 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001475 continue;
1476
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001477 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001478 hrtimer_get_softirq_time(cpu_base);
1479 gettime = 0;
1480 }
1481
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001482 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001483
John Stultzb007c382010-12-10 22:19:53 -08001484 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001485 struct hrtimer *timer;
1486
John Stultz998adc32010-09-20 19:19:17 -07001487 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001488 if (base->softirq_time.tv64 <=
1489 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001490 break;
1491
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001492 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001493 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001494 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001495 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001496}
1497
1498/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001499 * Sleep related functions:
1500 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001501static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001502{
1503 struct hrtimer_sleeper *t =
1504 container_of(timer, struct hrtimer_sleeper, timer);
1505 struct task_struct *task = t->task;
1506
1507 t->task = NULL;
1508 if (task)
1509 wake_up_process(task);
1510
1511 return HRTIMER_NORESTART;
1512}
1513
Ingo Molnar36c8b582006-07-03 00:25:41 -07001514void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001515{
1516 sl->timer.function = hrtimer_wakeup;
1517 sl->task = task;
1518}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001519EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001520
Thomas Gleixner669d7862006-03-31 02:31:19 -08001521static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001522{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001523 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001524
Roman Zippel432569b2006-03-26 01:38:08 -08001525 do {
1526 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001527 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001528 if (!hrtimer_active(&t->timer))
1529 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001530
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001531 if (likely(t->task))
1532 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001533
Thomas Gleixner669d7862006-03-31 02:31:19 -08001534 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001535 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001536
Thomas Gleixner669d7862006-03-31 02:31:19 -08001537 } while (t->task && !signal_pending(current));
1538
Peter Zijlstra3588a082008-02-01 17:45:13 +01001539 __set_current_state(TASK_RUNNING);
1540
Thomas Gleixner669d7862006-03-31 02:31:19 -08001541 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001542}
1543
Oleg Nesterov080344b2008-02-01 17:29:05 +03001544static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1545{
1546 struct timespec rmt;
1547 ktime_t rem;
1548
Arjan van de Vencc584b22008-09-01 15:02:30 -07001549 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001550 if (rem.tv64 <= 0)
1551 return 0;
1552 rmt = ktime_to_timespec(rem);
1553
1554 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1555 return -EFAULT;
1556
1557 return 1;
1558}
1559
Toyo Abe1711ef32006-09-29 02:00:28 -07001560long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001561{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001562 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001563 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001564 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001565
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001566 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001567 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001568 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001569
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001570 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001571 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001572
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001573 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001574 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001575 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001576 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001577 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001578 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001579
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001580 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001581 ret = -ERESTART_RESTARTBLOCK;
1582out:
1583 destroy_hrtimer_on_stack(&t.timer);
1584 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001585}
1586
Oleg Nesterov080344b2008-02-01 17:29:05 +03001587long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001588 const enum hrtimer_mode mode, const clockid_t clockid)
1589{
1590 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001591 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001592 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001593 unsigned long slack;
1594
1595 slack = current->timer_slack_ns;
1596 if (rt_task(current))
1597 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001598
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001599 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001600 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001601 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001602 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001603
George Anzinger79786722006-02-01 03:05:11 -08001604 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001605 if (mode == HRTIMER_MODE_ABS) {
1606 ret = -ERESTARTNOHAND;
1607 goto out;
1608 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001609
Roman Zippel432569b2006-03-26 01:38:08 -08001610 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001611 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001612 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001613 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001614 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001615
1616 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001617 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001618 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001619 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001620 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001621
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001622 ret = -ERESTART_RESTARTBLOCK;
1623out:
1624 destroy_hrtimer_on_stack(&t.timer);
1625 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001626}
1627
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001628SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1629 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001630{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001631 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001632
1633 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1634 return -EFAULT;
1635
1636 if (!timespec_valid(&tu))
1637 return -EINVAL;
1638
Oleg Nesterov080344b2008-02-01 17:29:05 +03001639 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001640}
1641
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001642/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001643 * Functions related to boot-time initialization:
1644 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001645static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001646{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001647 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001648 int i;
1649
John Stultz998adc32010-09-20 19:19:17 -07001650 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001651 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001652 timerqueue_init_head(&cpu_base->clock_base[i].active);
1653 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001654
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001655 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001656}
1657
1658#ifdef CONFIG_HOTPLUG_CPU
1659
Peter Zijlstraca109492008-11-25 12:43:51 +01001660static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001661 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001662{
1663 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001664 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001665
John Stultz998adc32010-09-20 19:19:17 -07001666 while ((node = timerqueue_getnext(&old_base->active))) {
1667 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001668 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001669 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001670
1671 /*
1672 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1673 * timer could be seen as !active and just vanish away
1674 * under us on another CPU
1675 */
1676 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001677 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001678 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001679 * Enqueue the timers on the new cpu. This does not
1680 * reprogram the event device in case the timer
1681 * expires before the earliest on this CPU, but we run
1682 * hrtimer_interrupt after we migrated everything to
1683 * sort out already expired timers and reprogram the
1684 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001685 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001686 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001687
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001688 /* Clear the migration state bit */
1689 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001690 }
1691}
1692
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001693static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001694{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001695 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001696 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001697
Peter Zijlstra37810652008-12-04 11:17:10 +01001698 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001699 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001700
1701 local_irq_disable();
1702 old_base = &per_cpu(hrtimer_bases, scpu);
1703 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001704 /*
1705 * The caller is globally serialized and nobody else
1706 * takes two locks at once, deadlock is not possible.
1707 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001708 raw_spin_lock(&new_base->lock);
1709 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001710
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001711 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001712 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001713 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001714 }
1715
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001716 raw_spin_unlock(&old_base->lock);
1717 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001718
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001719 /* Check, if we got expired work to do */
1720 __hrtimer_peek_ahead_timers();
1721 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001722}
1723
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001724#endif /* CONFIG_HOTPLUG_CPU */
1725
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001726static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001727 unsigned long action, void *hcpu)
1728{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001729 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001730
1731 switch (action) {
1732
1733 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001734 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001735 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001736 break;
1737
1738#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001739 case CPU_DYING:
1740 case CPU_DYING_FROZEN:
1741 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1742 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001743 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001744 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001745 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001746 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001747 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001748 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001749 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001750#endif
1751
1752 default:
1753 break;
1754 }
1755
1756 return NOTIFY_OK;
1757}
1758
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001759static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001760 .notifier_call = hrtimer_cpu_notify,
1761};
1762
1763void __init hrtimers_init(void)
1764{
1765 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1766 (void *)(long)smp_processor_id());
1767 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001768#ifdef CONFIG_HIGH_RES_TIMERS
1769 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1770#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001771}
1772
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001773/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001774 * schedule_hrtimeout_range_clock - sleep until timeout
1775 * @expires: timeout value (ktime_t)
1776 * @delta: slack in expires timeout (ktime_t)
1777 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1778 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1779 */
1780int __sched
1781schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1782 const enum hrtimer_mode mode, int clock)
1783{
1784 struct hrtimer_sleeper t;
1785
1786 /*
1787 * Optimize when a zero timeout value is given. It does not
1788 * matter whether this is an absolute or a relative time.
1789 */
1790 if (expires && !expires->tv64) {
1791 __set_current_state(TASK_RUNNING);
1792 return 0;
1793 }
1794
1795 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001796 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001797 */
1798 if (!expires) {
1799 schedule();
1800 __set_current_state(TASK_RUNNING);
1801 return -EINTR;
1802 }
1803
1804 hrtimer_init_on_stack(&t.timer, clock, mode);
1805 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1806
1807 hrtimer_init_sleeper(&t, current);
1808
1809 hrtimer_start_expires(&t.timer, mode);
1810 if (!hrtimer_active(&t.timer))
1811 t.task = NULL;
1812
1813 if (likely(t.task))
1814 schedule();
1815
1816 hrtimer_cancel(&t.timer);
1817 destroy_hrtimer_on_stack(&t.timer);
1818
1819 __set_current_state(TASK_RUNNING);
1820
1821 return !t.task ? 0 : -EINTR;
1822}
1823
1824/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001825 * schedule_hrtimeout_range - sleep until timeout
1826 * @expires: timeout value (ktime_t)
1827 * @delta: slack in expires timeout (ktime_t)
1828 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1829 *
1830 * Make the current task sleep until the given expiry time has
1831 * elapsed. The routine will return immediately unless
1832 * the current task state has been set (see set_current_state()).
1833 *
1834 * The @delta argument gives the kernel the freedom to schedule the
1835 * actual wakeup to a time that is both power and performance friendly.
1836 * The kernel give the normal best effort behavior for "@expires+@delta",
1837 * but may decide to fire the timer earlier, but no earlier than @expires.
1838 *
1839 * You can set the task state as follows -
1840 *
1841 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1842 * pass before the routine returns.
1843 *
1844 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1845 * delivered to the current task.
1846 *
1847 * The current task state is guaranteed to be TASK_RUNNING when this
1848 * routine returns.
1849 *
1850 * Returns 0 when the timer has expired otherwise -EINTR
1851 */
1852int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001853 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001854{
Carsten Emde351b3f72010-04-02 22:40:19 +02001855 return schedule_hrtimeout_range_clock(expires, delta, mode,
1856 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001857}
1858EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1859
1860/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001861 * schedule_hrtimeout - sleep until timeout
1862 * @expires: timeout value (ktime_t)
1863 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1864 *
1865 * Make the current task sleep until the given expiry time has
1866 * elapsed. The routine will return immediately unless
1867 * the current task state has been set (see set_current_state()).
1868 *
1869 * You can set the task state as follows -
1870 *
1871 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1872 * pass before the routine returns.
1873 *
1874 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1875 * delivered to the current task.
1876 *
1877 * The current task state is guaranteed to be TASK_RUNNING when this
1878 * routine returns.
1879 *
1880 * Returns 0 when the timer has expired otherwise -EINTR
1881 */
1882int __sched schedule_hrtimeout(ktime_t *expires,
1883 const enum hrtimer_mode mode)
1884{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001885 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001886}
1887EXPORT_SYMBOL_GPL(schedule_hrtimeout);