blob: 434f2b673d5b7a24814933a84a43dcf932a437f7 [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;
Leon Maa91ccfc2014-04-30 16:43:10 +0800235 } else {
236 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
237 cpu = this_cpu;
238 goto again;
239 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800240 }
241 return new_base;
242}
243
244#else /* CONFIG_SMP */
245
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800246static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800247lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
248{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800249 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800250
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100251 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800252
253 return base;
254}
255
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530256# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800257
258#endif /* !CONFIG_SMP */
259
260/*
261 * Functions for the union type storage format of ktime_t which are
262 * too large for inlining:
263 */
264#if BITS_PER_LONG < 64
265# ifndef CONFIG_KTIME_SCALAR
266/**
267 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800268 * @kt: addend
269 * @nsec: the scalar nsec value to add
270 *
271 * Returns the sum of kt and nsec in ktime_t format
272 */
273ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
274{
275 ktime_t tmp;
276
277 if (likely(nsec < NSEC_PER_SEC)) {
278 tmp.tv64 = nsec;
279 } else {
280 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
281
282 tmp = ktime_set((long)nsec, rem);
283 }
284
285 return ktime_add(kt, tmp);
286}
David Howellsb8b8fd22007-04-27 15:31:24 -0700287
288EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700289
290/**
291 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
292 * @kt: minuend
293 * @nsec: the scalar nsec value to subtract
294 *
295 * Returns the subtraction of @nsec from @kt in ktime_t format
296 */
297ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
298{
299 ktime_t tmp;
300
301 if (likely(nsec < NSEC_PER_SEC)) {
302 tmp.tv64 = nsec;
303 } else {
304 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
305
David Engraf86a640f2013-03-19 13:29:55 +0100306 /* Make sure nsec fits into long */
307 if (unlikely(nsec > KTIME_SEC_MAX))
308 return (ktime_t){ .tv64 = KTIME_MAX };
309
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700310 tmp = ktime_set((long)nsec, rem);
311 }
312
313 return ktime_sub(kt, tmp);
314}
315
316EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800317# endif /* !CONFIG_KTIME_SCALAR */
318
319/*
320 * Divide a ktime value by a nanosecond value
321 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800322u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800323{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300324 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800325 int sft = 0;
326
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300327 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800328 /* Make sure the divisor is less than 2^32: */
329 while (div >> 32) {
330 sft++;
331 div >>= 1;
332 }
333 dclc >>= sft;
334 do_div(dclc, (unsigned long) div);
335
Davide Libenzi4d672e72008-02-04 22:27:26 -0800336 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800337}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800338#endif /* BITS_PER_LONG >= 64 */
339
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100340/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100341 * Add two ktime values and do a safety check for overflow:
342 */
343ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
344{
345 ktime_t res = ktime_add(lhs, rhs);
346
347 /*
348 * We use KTIME_SEC_MAX here, the maximum timeout which we can
349 * return to user space in a timespec:
350 */
351 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
352 res = ktime_set(KTIME_SEC_MAX, 0);
353
354 return res;
355}
356
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300357EXPORT_SYMBOL_GPL(ktime_add_safe);
358
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700359#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
360
361static struct debug_obj_descr hrtimer_debug_descr;
362
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100363static void *hrtimer_debug_hint(void *addr)
364{
365 return ((struct hrtimer *) addr)->function;
366}
367
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700368/*
369 * fixup_init is called when:
370 * - an active object is initialized
371 */
372static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
373{
374 struct hrtimer *timer = addr;
375
376 switch (state) {
377 case ODEBUG_STATE_ACTIVE:
378 hrtimer_cancel(timer);
379 debug_object_init(timer, &hrtimer_debug_descr);
380 return 1;
381 default:
382 return 0;
383 }
384}
385
386/*
387 * fixup_activate is called when:
388 * - an active object is activated
389 * - an unknown object is activated (might be a statically initialized object)
390 */
391static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
392{
393 switch (state) {
394
395 case ODEBUG_STATE_NOTAVAILABLE:
396 WARN_ON_ONCE(1);
397 return 0;
398
399 case ODEBUG_STATE_ACTIVE:
400 WARN_ON(1);
401
402 default:
403 return 0;
404 }
405}
406
407/*
408 * fixup_free is called when:
409 * - an active object is freed
410 */
411static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
412{
413 struct hrtimer *timer = addr;
414
415 switch (state) {
416 case ODEBUG_STATE_ACTIVE:
417 hrtimer_cancel(timer);
418 debug_object_free(timer, &hrtimer_debug_descr);
419 return 1;
420 default:
421 return 0;
422 }
423}
424
425static struct debug_obj_descr hrtimer_debug_descr = {
426 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100427 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700428 .fixup_init = hrtimer_fixup_init,
429 .fixup_activate = hrtimer_fixup_activate,
430 .fixup_free = hrtimer_fixup_free,
431};
432
433static inline void debug_hrtimer_init(struct hrtimer *timer)
434{
435 debug_object_init(timer, &hrtimer_debug_descr);
436}
437
438static inline void debug_hrtimer_activate(struct hrtimer *timer)
439{
440 debug_object_activate(timer, &hrtimer_debug_descr);
441}
442
443static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
444{
445 debug_object_deactivate(timer, &hrtimer_debug_descr);
446}
447
448static inline void debug_hrtimer_free(struct hrtimer *timer)
449{
450 debug_object_free(timer, &hrtimer_debug_descr);
451}
452
453static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
454 enum hrtimer_mode mode);
455
456void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
457 enum hrtimer_mode mode)
458{
459 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
460 __hrtimer_init(timer, clock_id, mode);
461}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700462EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700463
464void destroy_hrtimer_on_stack(struct hrtimer *timer)
465{
466 debug_object_free(timer, &hrtimer_debug_descr);
467}
468
469#else
470static inline void debug_hrtimer_init(struct hrtimer *timer) { }
471static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
472static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
473#endif
474
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800475static inline void
476debug_init(struct hrtimer *timer, clockid_t clockid,
477 enum hrtimer_mode mode)
478{
479 debug_hrtimer_init(timer);
480 trace_hrtimer_init(timer, clockid, mode);
481}
482
483static inline void debug_activate(struct hrtimer *timer)
484{
485 debug_hrtimer_activate(timer);
486 trace_hrtimer_start(timer);
487}
488
489static inline void debug_deactivate(struct hrtimer *timer)
490{
491 debug_hrtimer_deactivate(timer);
492 trace_hrtimer_cancel(timer);
493}
494
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800495/* High resolution timer related functions */
496#ifdef CONFIG_HIGH_RES_TIMERS
497
498/*
499 * High resolution timer enabled ?
500 */
501static int hrtimer_hres_enabled __read_mostly = 1;
502
503/*
504 * Enable / Disable high resolution mode
505 */
506static int __init setup_hrtimer_hres(char *str)
507{
508 if (!strcmp(str, "off"))
509 hrtimer_hres_enabled = 0;
510 else if (!strcmp(str, "on"))
511 hrtimer_hres_enabled = 1;
512 else
513 return 0;
514 return 1;
515}
516
517__setup("highres=", setup_hrtimer_hres);
518
519/*
520 * hrtimer_high_res_enabled - query, if the highres mode is enabled
521 */
522static inline int hrtimer_is_hres_enabled(void)
523{
524 return hrtimer_hres_enabled;
525}
526
527/*
528 * Is the high resolution mode active ?
529 */
530static inline int hrtimer_hres_active(void)
531{
Christoph Lameter909ea962010-12-08 16:22:55 +0100532 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800533}
534
535/*
536 * Reprogram the event source with checking both queues for the
537 * next event
538 * Called with interrupts disabled and base->lock held
539 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400540static void
541hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800542{
543 int i;
544 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400545 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800546
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400547 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800548
549 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
550 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700551 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800552
John Stultz998adc32010-09-20 19:19:17 -0700553 next = timerqueue_getnext(&base->active);
554 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800555 continue;
John Stultz998adc32010-09-20 19:19:17 -0700556 timer = container_of(next, struct hrtimer, node);
557
Arjan van de Vencc584b22008-09-01 15:02:30 -0700558 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100559 /*
560 * clock_was_set() has changed base->offset so the
561 * result might be negative. Fix it up to prevent a
562 * false positive in clockevents_program_event()
563 */
564 if (expires.tv64 < 0)
565 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400566 if (expires.tv64 < expires_next.tv64)
567 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800568 }
569
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400570 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
571 return;
572
573 cpu_base->expires_next.tv64 = expires_next.tv64;
574
Stuart Hayes3d8b2f52014-04-29 17:55:02 -0500575 /*
576 * If a hang was detected in the last timer interrupt then we
577 * leave the hang delay active in the hardware. We want the
578 * system to make progress. That also prevents the following
579 * scenario:
580 * T1 expires 50ms from now
581 * T2 expires 5s from now
582 *
583 * T1 is removed, so this code is called and would reprogram
584 * the hardware to 5s from now. Any hrtimer_start after that
585 * will not reprogram the hardware due to hang_detected being
586 * set. So we'd effectivly block all timers until the T2 event
587 * fires.
588 */
589 if (cpu_base->hang_detected)
590 return;
591
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800592 if (cpu_base->expires_next.tv64 != KTIME_MAX)
593 tick_program_event(cpu_base->expires_next, 1);
594}
595
596/*
597 * Shared reprogramming for clock_realtime and clock_monotonic
598 *
599 * When a timer is enqueued and expires earlier than the already enqueued
600 * timers, we have to check, whether it expires earlier than the timer for
601 * which the clock event device was armed.
602 *
603 * Called with interrupts disabled and base->cpu_base.lock held
604 */
605static int hrtimer_reprogram(struct hrtimer *timer,
606 struct hrtimer_clock_base *base)
607{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100608 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700609 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800610 int res;
611
Arjan van de Vencc584b22008-09-01 15:02:30 -0700612 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100613
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800614 /*
615 * When the callback is running, we do not reprogram the clock event
616 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200617 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800618 * reprogramming is handled either by the softirq, which called the
619 * callback or at the end of the hrtimer_interrupt.
620 */
621 if (hrtimer_callback_running(timer))
622 return 0;
623
Thomas Gleixner63070a72008-02-14 00:58:36 +0100624 /*
625 * CLOCK_REALTIME timer might be requested with an absolute
626 * expiry time which is less than base->offset. Nothing wrong
627 * about that, just avoid to call into the tick code, which
628 * has now objections against negative expiry values.
629 */
630 if (expires.tv64 < 0)
631 return -ETIME;
632
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100633 if (expires.tv64 >= cpu_base->expires_next.tv64)
634 return 0;
635
636 /*
637 * If a hang was detected in the last timer interrupt then we
638 * do not schedule a timer which is earlier than the expiry
639 * which we enforced in the hang detection. We want the system
640 * to make progress.
641 */
642 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800643 return 0;
644
645 /*
646 * Clockevents returns -ETIME, when the event was in the past.
647 */
648 res = tick_program_event(expires, 0);
649 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100650 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800651 return res;
652}
653
Ingo Molnar995f0542007-04-07 12:05:00 +0200654/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800655 * Initialize the high resolution related parts of cpu_base
656 */
657static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
658{
659 base->expires_next.tv64 = KTIME_MAX;
660 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800661}
662
663/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800664 * When High resolution timers are active, try to reprogram. Note, that in case
665 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
666 * check happens. The timer gets enqueued into the rbtree. The reprogramming
667 * and expiry check is done in the hrtimer_interrupt or in the softirq.
668 */
669static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzdd9c58a2013-02-04 14:33:37 +0200670 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800671{
Leonid Shatzdd9c58a2013-02-04 14:33:37 +0200672 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800673}
674
John Stultz6321a0a2012-07-17 02:39:55 -0400675static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
676{
677 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
678 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
679
680 return ktime_get_update_offsets(offs_real, offs_boot);
681}
682
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200683/*
684 * Retrigger next event is called after clock was set
685 *
686 * Called with interrupts disabled via on_each_cpu()
687 */
688static void retrigger_next_event(void *arg)
689{
690 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200691
692 if (!hrtimer_hres_active())
693 return;
694
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200695 raw_spin_lock(&base->lock);
John Stultz6321a0a2012-07-17 02:39:55 -0400696 hrtimer_update_base(base);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200697 hrtimer_force_reprogram(base, 0);
698 raw_spin_unlock(&base->lock);
699}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200700
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800701/*
702 * Switch to high resolution mode
703 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800704static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800705{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200706 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700707 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800708 unsigned long flags;
709
710 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800711 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800712
713 local_irq_save(flags);
714
715 if (tick_init_highres()) {
716 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700717 printk(KERN_WARNING "Could not switch to high resolution "
718 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800719 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800720 }
721 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200722 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
723 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800724
725 tick_setup_sched_timer();
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800726 /* "Retrigger" the interrupt to get things going */
727 retrigger_next_event(NULL);
728 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800729 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800730}
731
Thomas Gleixner39367f42013-07-05 12:09:18 +0200732static void clock_was_set_work(struct work_struct *work)
733{
734 clock_was_set();
735}
736
737static DECLARE_WORK(hrtimer_work, clock_was_set_work);
738
John Stultz5e5006e2012-07-17 02:39:50 -0400739/*
Thomas Gleixner39367f42013-07-05 12:09:18 +0200740 * Called from timekeeping and resume code to reprogramm the hrtimer
741 * interrupt device on all cpus.
John Stultz5e5006e2012-07-17 02:39:50 -0400742 */
743void clock_was_set_delayed(void)
744{
Thomas Gleixner39367f42013-07-05 12:09:18 +0200745 schedule_work(&hrtimer_work);
John Stultz5e5006e2012-07-17 02:39:50 -0400746}
747
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800748#else
749
750static inline int hrtimer_hres_active(void) { return 0; }
751static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800752static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400753static inline void
754hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800755static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzdd9c58a2013-02-04 14:33:37 +0200756 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800757{
758 return 0;
759}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800760static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200761static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800762
763#endif /* CONFIG_HIGH_RES_TIMERS */
764
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200765/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200766 * Clock realtime was set
767 *
768 * Change the offset of the realtime clock vs. the monotonic
769 * clock.
770 *
771 * We might have to reprogram the high resolution timer interrupt. On
772 * SMP we call the architecture specific code to retrigger _all_ high
773 * resolution timer interrupts. On UP we just disable interrupts and
774 * call the high resolution interrupt code.
775 */
776void clock_was_set(void)
777{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200778#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200779 /* Retrigger the CPU local events everywhere */
780 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200781#endif
782 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200783}
784
785/*
786 * During resume we might have to reprogram the high resolution timer
787 * interrupt (on the local CPU):
788 */
789void hrtimers_resume(void)
790{
791 WARN_ONCE(!irqs_disabled(),
792 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
793
Thomas Gleixner39367f42013-07-05 12:09:18 +0200794 /* Retrigger on the local CPU */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200795 retrigger_next_event(NULL);
Thomas Gleixner39367f42013-07-05 12:09:18 +0200796 /* And schedule a retrigger for all others */
797 clock_was_set_delayed();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200798}
799
Heiko Carstens5f201902009-12-10 10:56:29 +0100800static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800801{
Heiko Carstens5f201902009-12-10 10:56:29 +0100802#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800803 if (timer->start_site)
804 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100805 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800806 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
807 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800808#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100809}
810
811static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
812{
813#ifdef CONFIG_TIMER_STATS
814 timer->start_site = NULL;
815#endif
816}
817
818static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
819{
820#ifdef CONFIG_TIMER_STATS
821 if (likely(!timer_stats_active))
822 return;
823 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
824 timer->function, timer->start_comm, 0);
825#endif
826}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800827
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800828/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200829 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800830 */
831static inline
832void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
833{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100834 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800835}
836
837/**
838 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800839 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800840 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800841 * @interval: the interval to forward
842 *
843 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700844 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800845 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800846u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800847{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800848 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800849 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800850
Arjan van de Vencc584b22008-09-01 15:02:30 -0700851 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852
853 if (delta.tv64 < 0)
854 return 0;
855
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100856 if (interval.tv64 < timer->base->resolution.tv64)
857 interval.tv64 = timer->base->resolution.tv64;
858
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800859 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800860 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800861
862 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700863 hrtimer_add_expires_ns(timer, incr * orun);
864 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800865 return orun;
866 /*
867 * This (and the ktime_add() below) is the
868 * correction for exact:
869 */
870 orun++;
871 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700872 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800873
874 return orun;
875}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700876EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800877
878/*
879 * enqueue_hrtimer - internal function to (re)start a timer
880 *
881 * The timer is inserted in expiry order. Insertion into the
882 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100883 *
884 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800885 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100886static int enqueue_hrtimer(struct hrtimer *timer,
887 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800888{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800889 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700890
John Stultz998adc32010-09-20 19:19:17 -0700891 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200892 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800893
894 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800895 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
896 * state of a possibly running callback.
897 */
898 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100899
John Stultz998adc32010-09-20 19:19:17 -0700900 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100901}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800902
903/*
904 * __remove_hrtimer - internal function to remove a timer
905 *
906 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800907 *
908 * High resolution timer mode reprograms the clock event device when the
909 * timer is the one which expires next. The caller can disable this by setting
910 * reprogram to zero. This is useful, when the context does a reprogramming
911 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800912 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800913static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800914 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800915 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800916{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800917 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400918 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
919 goto out;
920
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800921 next_timer = timerqueue_getnext(&base->active);
922 timerqueue_del(&base->active, &timer->node);
923 if (&timer->node == next_timer) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400924#ifdef CONFIG_HIGH_RES_TIMERS
925 /* Reprogram the clock event device. if enabled */
926 if (reprogram && hrtimer_hres_active()) {
927 ktime_t expires;
928
929 expires = ktime_sub(hrtimer_get_expires(timer),
930 base->offset);
931 if (base->cpu_base->expires_next.tv64 == expires.tv64)
932 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800933 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400934#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800935 }
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200936 if (!timerqueue_getnext(&base->active))
937 base->cpu_base->active_bases &= ~(1 << base->index);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400938out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800939 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800940}
941
942/*
943 * remove hrtimer, called with base lock held
944 */
945static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800946remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800947{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800948 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700949 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800950 int reprogram;
951
952 /*
953 * Remove the timer and force reprogramming when high
954 * resolution mode is active and the timer is on the current
955 * CPU. If we remove a timer on another CPU, reprogramming is
956 * skipped. The interrupt event on this CPU is fired and
957 * reprogramming happens in the interrupt handler. This is a
958 * rare case and less expensive than a smp call.
959 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800960 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800961 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800962 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700963 /*
964 * We must preserve the CALLBACK state flag here,
965 * otherwise we could move the timer base in
966 * switch_hrtimer_base.
967 */
968 state = timer->state & HRTIMER_STATE_CALLBACK;
969 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800970 return 1;
971 }
972 return 0;
973}
974
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100975int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
976 unsigned long delta_ns, const enum hrtimer_mode mode,
977 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800978{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800979 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800980 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100981 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800982
983 base = lock_hrtimer_base(timer, &flags);
984
985 /* Remove an active timer from the queue: */
986 ret = remove_hrtimer(timer, base);
987
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530988 if (mode & HRTIMER_MODE_REL) {
Viresh Kumar11f87a62014-05-12 13:42:29 +0530989 tim = ktime_add_safe(tim, base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800990 /*
991 * CONFIG_TIME_LOW_RES is a temporary way for architectures
992 * to signal that they simply return xtime in
993 * do_gettimeoffset(). In this case we want to round up by
994 * resolution when starting a relative timer, to avoid short
995 * timeouts. This will go away with the GTOD framework.
996 */
997#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100998 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800999#endif
1000 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001001
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001002 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001003
Viresh Kumar11f87a62014-05-12 13:42:29 +05301004 /* Switch the timer base, if necessary: */
1005 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
1006
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001007 timer_stats_hrtimer_set_start_info(timer);
1008
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001009 leftmost = enqueue_hrtimer(timer, new_base);
1010
Ingo Molnar935c6312007-03-28 13:17:18 +02001011 /*
1012 * Only allow reprogramming if the new base is on this CPU.
1013 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001014 *
1015 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +02001016 */
Leonid Shatzdd9c58a2013-02-04 14:33:37 +02001017 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
1018 && hrtimer_enqueue_reprogram(timer, new_base)) {
1019 if (wakeup) {
1020 /*
1021 * We need to drop cpu_base->lock to avoid a
1022 * lock ordering issue vs. rq->lock.
1023 */
1024 raw_spin_unlock(&new_base->cpu_base->lock);
1025 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1026 local_irq_restore(flags);
1027 return ret;
1028 } else {
1029 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1030 }
1031 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001032
1033 unlock_hrtimer_base(timer, &flags);
1034
1035 return ret;
1036}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001037
1038/**
1039 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1040 * @timer: the timer to be added
1041 * @tim: expiry time
1042 * @delta_ns: "slack" range for the timer
1043 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1044 *
1045 * Returns:
1046 * 0 on success
1047 * 1 when the timer was active
1048 */
1049int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1050 unsigned long delta_ns, const enum hrtimer_mode mode)
1051{
1052 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1053}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001054EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1055
1056/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001057 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001058 * @timer: the timer to be added
1059 * @tim: expiry time
1060 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1061 *
1062 * Returns:
1063 * 0 on success
1064 * 1 when the timer was active
1065 */
1066int
1067hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1068{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001069 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001070}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001071EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001072
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001073
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001074/**
1075 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001076 * @timer: hrtimer to stop
1077 *
1078 * Returns:
1079 * 0 when the timer was not active
1080 * 1 when the timer was active
1081 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001082 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001083 */
1084int hrtimer_try_to_cancel(struct hrtimer *timer)
1085{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001086 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001087 unsigned long flags;
1088 int ret = -1;
1089
1090 base = lock_hrtimer_base(timer, &flags);
1091
Thomas Gleixner303e9672007-02-16 01:27:51 -08001092 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001093 ret = remove_hrtimer(timer, base);
1094
1095 unlock_hrtimer_base(timer, &flags);
1096
1097 return ret;
1098
1099}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001100EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001101
1102/**
1103 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001104 * @timer: the timer to be cancelled
1105 *
1106 * Returns:
1107 * 0 when the timer was not active
1108 * 1 when the timer was active
1109 */
1110int hrtimer_cancel(struct hrtimer *timer)
1111{
1112 for (;;) {
1113 int ret = hrtimer_try_to_cancel(timer);
1114
1115 if (ret >= 0)
1116 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001117 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001118 }
1119}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001120EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001121
1122/**
1123 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001124 * @timer: the timer to read
1125 */
1126ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1127{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001128 unsigned long flags;
1129 ktime_t rem;
1130
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001131 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001132 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001133 unlock_hrtimer_base(timer, &flags);
1134
1135 return rem;
1136}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001137EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001138
Russell Kingee9c5782008-04-20 13:59:33 +01001139#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001140/**
1141 * hrtimer_get_next_event - get the time until next expiry event
1142 *
1143 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1144 * is pending.
1145 */
1146ktime_t hrtimer_get_next_event(void)
1147{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001148 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1149 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001150 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1151 unsigned long flags;
1152 int i;
1153
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001154 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001155
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001156 if (!hrtimer_hres_active()) {
1157 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1158 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001159 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001160
John Stultz998adc32010-09-20 19:19:17 -07001161 next = timerqueue_getnext(&base->active);
1162 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001163 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001164
John Stultz998adc32010-09-20 19:19:17 -07001165 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001166 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001167 delta = ktime_sub(delta, base->get_time());
1168 if (delta.tv64 < mindelta.tv64)
1169 mindelta.tv64 = delta.tv64;
1170 }
Tony Lindgren69239742006-03-06 15:42:45 -08001171 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001172
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001173 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001174
Tony Lindgren69239742006-03-06 15:42:45 -08001175 if (mindelta.tv64 < 0)
1176 mindelta.tv64 = 0;
1177 return mindelta;
1178}
1179#endif
1180
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001181static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1182 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001183{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001184 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001185 int base;
George Anzinger79786722006-02-01 03:05:11 -08001186
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001187 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001188
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001189 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001190
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001191 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001192 clock_id = CLOCK_MONOTONIC;
1193
John Stultze06383d2010-12-14 19:37:07 -08001194 base = hrtimer_clockid_to_base(clock_id);
1195 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001196 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001197
1198#ifdef CONFIG_TIMER_STATS
1199 timer->start_site = NULL;
1200 timer->start_pid = -1;
1201 memset(timer->start_comm, 0, TASK_COMM_LEN);
1202#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001203}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001204
1205/**
1206 * hrtimer_init - initialize a timer to the given clock
1207 * @timer: the timer to be initialized
1208 * @clock_id: the clock to be used
1209 * @mode: timer mode abs/rel
1210 */
1211void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1212 enum hrtimer_mode mode)
1213{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001214 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001215 __hrtimer_init(timer, clock_id, mode);
1216}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001217EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001218
1219/**
1220 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001221 * @which_clock: which clock to query
1222 * @tp: pointer to timespec variable to store the resolution
1223 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001224 * Store the resolution of the clock selected by @which_clock in the
1225 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001226 */
1227int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1228{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001229 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001230 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001231
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001232 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001233 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001234
1235 return 0;
1236}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001237EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001238
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001239static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001240{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001241 struct hrtimer_clock_base *base = timer->base;
1242 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1243 enum hrtimer_restart (*fn)(struct hrtimer *);
1244 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001245
Peter Zijlstraca109492008-11-25 12:43:51 +01001246 WARN_ON(!irqs_disabled());
1247
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001248 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001249 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1250 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001251 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001252
1253 /*
1254 * Because we run timers from hardirq context, there is no chance
1255 * they get migrated to another cpu, therefore its safe to unlock
1256 * the timer base.
1257 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001258 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001259 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001260 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001261 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001262 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001263
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001264 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001265 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1266 * we do not reprogramm the event hardware. Happens either in
1267 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001268 */
1269 if (restart != HRTIMER_NORESTART) {
1270 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001271 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001272 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001273
1274 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1275
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001276 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001277}
1278
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001279#ifdef CONFIG_HIGH_RES_TIMERS
1280
1281/*
1282 * High resolution timer interrupt
1283 * Called with interrupts disabled
1284 */
1285void hrtimer_interrupt(struct clock_event_device *dev)
1286{
1287 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001288 ktime_t expires_next, now, entry_time, delta;
1289 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001290
1291 BUG_ON(!cpu_base->hres_active);
1292 cpu_base->nr_events++;
1293 dev->next_event.tv64 = KTIME_MAX;
1294
Thomas Gleixnerdd3cded2012-07-17 02:39:53 -04001295 raw_spin_lock(&cpu_base->lock);
John Stultz6321a0a2012-07-17 02:39:55 -04001296 entry_time = now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001297retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001298 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001299 /*
1300 * We set expires_next to KTIME_MAX here with cpu_base->lock
1301 * held to prevent that a timer is enqueued in our queue via
1302 * the migration code. This does not affect enqueueing of
1303 * timers which run their callback and need to be requeued on
1304 * this CPU.
1305 */
1306 cpu_base->expires_next.tv64 = KTIME_MAX;
1307
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001308 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001309 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001310 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001311 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001312
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001313 if (!(cpu_base->active_bases & (1 << i)))
1314 continue;
1315
1316 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001317 basenow = ktime_add(now, base->offset);
1318
John Stultz998adc32010-09-20 19:19:17 -07001319 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001320 struct hrtimer *timer;
1321
John Stultz998adc32010-09-20 19:19:17 -07001322 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001323
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001324 /*
1325 * The immediate goal for using the softexpires is
1326 * minimizing wakeups, not running timers at the
1327 * earliest interrupt after their soft expiration.
1328 * This allows us to avoid using a Priority Search
1329 * Tree, which can answer a stabbing querry for
1330 * overlapping intervals and instead use the simple
1331 * BST we already have.
1332 * We don't add extra wakeups by delaying timers that
1333 * are right-of a not yet expired timer, because that
1334 * timer will have to trigger a wakeup anyway.
1335 */
1336
1337 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001338 ktime_t expires;
1339
Arjan van de Vencc584b22008-09-01 15:02:30 -07001340 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001341 base->offset);
Prarit Bhargava194d30b2013-04-08 08:47:15 -04001342 if (expires.tv64 < 0)
1343 expires.tv64 = KTIME_MAX;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001344 if (expires.tv64 < expires_next.tv64)
1345 expires_next = expires;
1346 break;
1347 }
1348
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001349 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001350 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001351 }
1352
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001353 /*
1354 * Store the new expiry value so the migration code can verify
1355 * against it.
1356 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001357 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001358 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001359
1360 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001361 if (expires_next.tv64 == KTIME_MAX ||
1362 !tick_program_event(expires_next, 0)) {
1363 cpu_base->hang_detected = 0;
1364 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001365 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001366
1367 /*
1368 * The next timer was already expired due to:
1369 * - tracing
1370 * - long lasting callbacks
1371 * - being scheduled away when running in a VM
1372 *
1373 * We need to prevent that we loop forever in the hrtimer
1374 * interrupt routine. We give it 3 attempts to avoid
1375 * overreacting on some spurious event.
John Stultz6321a0a2012-07-17 02:39:55 -04001376 *
1377 * Acquire base lock for updating the offsets and retrieving
1378 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001379 */
Thomas Gleixnerdd3cded2012-07-17 02:39:53 -04001380 raw_spin_lock(&cpu_base->lock);
John Stultz6321a0a2012-07-17 02:39:55 -04001381 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001382 cpu_base->nr_retries++;
1383 if (++retries < 3)
1384 goto retry;
1385 /*
1386 * Give the system a chance to do something else than looping
1387 * here. We stored the entry time, so we know exactly how long
1388 * we spent here. We schedule the next event this amount of
1389 * time away.
1390 */
1391 cpu_base->nr_hangs++;
1392 cpu_base->hang_detected = 1;
Thomas Gleixnerdd3cded2012-07-17 02:39:53 -04001393 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001394 delta = ktime_sub(now, entry_time);
1395 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1396 cpu_base->max_hang_time = delta;
1397 /*
1398 * Limit it to a sensible value as we enforce a longer
1399 * delay. Give the CPU at least 100ms to catch up.
1400 */
1401 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1402 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1403 else
1404 expires_next = ktime_add(now, delta);
1405 tick_program_event(expires_next, 1);
1406 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1407 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001408}
1409
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001410/*
1411 * local version of hrtimer_peek_ahead_timers() called with interrupts
1412 * disabled.
1413 */
1414static void __hrtimer_peek_ahead_timers(void)
1415{
1416 struct tick_device *td;
1417
1418 if (!hrtimer_hres_active())
1419 return;
1420
1421 td = &__get_cpu_var(tick_cpu_device);
1422 if (td && td->evtdev)
1423 hrtimer_interrupt(td->evtdev);
1424}
1425
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001426/**
1427 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1428 *
1429 * hrtimer_peek_ahead_timers will peek at the timer queue of
1430 * the current cpu and check if there are any timers for which
1431 * the soft expires time has passed. If any such timers exist,
1432 * they are run immediately and then removed from the timer queue.
1433 *
1434 */
1435void hrtimer_peek_ahead_timers(void)
1436{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001437 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001438
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001439 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001440 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001441 local_irq_restore(flags);
1442}
1443
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001444static void run_hrtimer_softirq(struct softirq_action *h)
1445{
1446 hrtimer_peek_ahead_timers();
1447}
1448
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001449#else /* CONFIG_HIGH_RES_TIMERS */
1450
1451static inline void __hrtimer_peek_ahead_timers(void) { }
1452
1453#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001454
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001455/*
1456 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001457 *
1458 * For HRT its the fall back code to run the softirq in the timer
1459 * softirq context in case the hrtimer initialization failed or has
1460 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001461 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001462void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001463{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001464 if (hrtimer_hres_active())
1465 return;
1466
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001467 /*
1468 * This _is_ ugly: We have to check in the softirq context,
1469 * whether we can switch to highres and / or nohz mode. The
1470 * clocksource switch happens in the timer interrupt with
1471 * xtime_lock held. Notification from there only sets the
1472 * check bit in the tick_oneshot code, otherwise we might
1473 * deadlock vs. xtime_lock.
1474 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001475 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001476 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001477}
1478
1479/*
1480 * Called from hardirq context every jiffy
1481 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001482void hrtimer_run_queues(void)
1483{
John Stultz998adc32010-09-20 19:19:17 -07001484 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001485 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001486 struct hrtimer_clock_base *base;
1487 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001488
1489 if (hrtimer_hres_active())
1490 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001491
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001492 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1493 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001494 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001495 continue;
1496
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001497 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001498 hrtimer_get_softirq_time(cpu_base);
1499 gettime = 0;
1500 }
1501
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001502 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001503
John Stultzb007c382010-12-10 22:19:53 -08001504 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001505 struct hrtimer *timer;
1506
John Stultz998adc32010-09-20 19:19:17 -07001507 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001508 if (base->softirq_time.tv64 <=
1509 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001510 break;
1511
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001512 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001513 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001514 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001515 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001516}
1517
1518/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001519 * Sleep related functions:
1520 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001521static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001522{
1523 struct hrtimer_sleeper *t =
1524 container_of(timer, struct hrtimer_sleeper, timer);
1525 struct task_struct *task = t->task;
1526
1527 t->task = NULL;
1528 if (task)
1529 wake_up_process(task);
1530
1531 return HRTIMER_NORESTART;
1532}
1533
Ingo Molnar36c8b582006-07-03 00:25:41 -07001534void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001535{
1536 sl->timer.function = hrtimer_wakeup;
1537 sl->task = task;
1538}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001539EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001540
Thomas Gleixner669d7862006-03-31 02:31:19 -08001541static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001542{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001543 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001544
Roman Zippel432569b2006-03-26 01:38:08 -08001545 do {
1546 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001547 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001548 if (!hrtimer_active(&t->timer))
1549 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001550
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001551 if (likely(t->task))
1552 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001553
Thomas Gleixner669d7862006-03-31 02:31:19 -08001554 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001555 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001556
Thomas Gleixner669d7862006-03-31 02:31:19 -08001557 } while (t->task && !signal_pending(current));
1558
Peter Zijlstra3588a082008-02-01 17:45:13 +01001559 __set_current_state(TASK_RUNNING);
1560
Thomas Gleixner669d7862006-03-31 02:31:19 -08001561 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001562}
1563
Oleg Nesterov080344b2008-02-01 17:29:05 +03001564static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1565{
1566 struct timespec rmt;
1567 ktime_t rem;
1568
Arjan van de Vencc584b22008-09-01 15:02:30 -07001569 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001570 if (rem.tv64 <= 0)
1571 return 0;
1572 rmt = ktime_to_timespec(rem);
1573
1574 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1575 return -EFAULT;
1576
1577 return 1;
1578}
1579
Toyo Abe1711ef32006-09-29 02:00:28 -07001580long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001581{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001582 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001583 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001584 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001585
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001586 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001587 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001588 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001589
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001590 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001591 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001592
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001593 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001594 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001595 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001596 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001597 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001598 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001599
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001600 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001601 ret = -ERESTART_RESTARTBLOCK;
1602out:
1603 destroy_hrtimer_on_stack(&t.timer);
1604 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001605}
1606
Oleg Nesterov080344b2008-02-01 17:29:05 +03001607long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001608 const enum hrtimer_mode mode, const clockid_t clockid)
1609{
1610 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001611 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001612 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001613 unsigned long slack;
1614
1615 slack = current->timer_slack_ns;
1616 if (rt_task(current))
1617 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001618
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001619 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001620 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001621 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001622 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001623
George Anzinger79786722006-02-01 03:05:11 -08001624 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001625 if (mode == HRTIMER_MODE_ABS) {
1626 ret = -ERESTARTNOHAND;
1627 goto out;
1628 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001629
Roman Zippel432569b2006-03-26 01:38:08 -08001630 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001631 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001632 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001633 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001634 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001635
1636 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001637 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001638 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001639 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001640 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001641
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001642 ret = -ERESTART_RESTARTBLOCK;
1643out:
1644 destroy_hrtimer_on_stack(&t.timer);
1645 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001646}
1647
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001648SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1649 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001650{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001651 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001652
1653 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1654 return -EFAULT;
1655
1656 if (!timespec_valid(&tu))
1657 return -EINVAL;
1658
Oleg Nesterov080344b2008-02-01 17:29:05 +03001659 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001660}
1661
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001662/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001663 * Functions related to boot-time initialization:
1664 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001665static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001666{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001667 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001668 int i;
1669
John Stultz998adc32010-09-20 19:19:17 -07001670 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001671 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001672 timerqueue_init_head(&cpu_base->clock_base[i].active);
1673 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001674
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001675 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001676}
1677
1678#ifdef CONFIG_HOTPLUG_CPU
1679
Peter Zijlstraca109492008-11-25 12:43:51 +01001680static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001681 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001682{
1683 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001684 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001685
John Stultz998adc32010-09-20 19:19:17 -07001686 while ((node = timerqueue_getnext(&old_base->active))) {
1687 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001688 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001689 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001690
1691 /*
1692 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1693 * timer could be seen as !active and just vanish away
1694 * under us on another CPU
1695 */
1696 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001697 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001698 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001699 * Enqueue the timers on the new cpu. This does not
1700 * reprogram the event device in case the timer
1701 * expires before the earliest on this CPU, but we run
1702 * hrtimer_interrupt after we migrated everything to
1703 * sort out already expired timers and reprogram the
1704 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001705 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001706 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001707
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001708 /* Clear the migration state bit */
1709 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001710 }
1711}
1712
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001713static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001714{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001715 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001716 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001717
Peter Zijlstra37810652008-12-04 11:17:10 +01001718 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001719 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001720
1721 local_irq_disable();
1722 old_base = &per_cpu(hrtimer_bases, scpu);
1723 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001724 /*
1725 * The caller is globally serialized and nobody else
1726 * takes two locks at once, deadlock is not possible.
1727 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001728 raw_spin_lock(&new_base->lock);
1729 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001730
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001731 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001732 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001733 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001734 }
1735
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001736 raw_spin_unlock(&old_base->lock);
1737 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001738
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001739 /* Check, if we got expired work to do */
1740 __hrtimer_peek_ahead_timers();
1741 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001742}
1743
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001744#endif /* CONFIG_HOTPLUG_CPU */
1745
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001746static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001747 unsigned long action, void *hcpu)
1748{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001749 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001750
1751 switch (action) {
1752
1753 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001754 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001755 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001756 break;
1757
1758#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001759 case CPU_DYING:
1760 case CPU_DYING_FROZEN:
1761 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1762 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001763 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001764 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001765 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001766 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001767 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001768 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001769 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001770#endif
1771
1772 default:
1773 break;
1774 }
1775
1776 return NOTIFY_OK;
1777}
1778
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001779static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001780 .notifier_call = hrtimer_cpu_notify,
1781};
1782
1783void __init hrtimers_init(void)
1784{
1785 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1786 (void *)(long)smp_processor_id());
1787 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001788#ifdef CONFIG_HIGH_RES_TIMERS
1789 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1790#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001791}
1792
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001793/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001794 * schedule_hrtimeout_range_clock - sleep until timeout
1795 * @expires: timeout value (ktime_t)
1796 * @delta: slack in expires timeout (ktime_t)
1797 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1798 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1799 */
1800int __sched
1801schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1802 const enum hrtimer_mode mode, int clock)
1803{
1804 struct hrtimer_sleeper t;
1805
1806 /*
1807 * Optimize when a zero timeout value is given. It does not
1808 * matter whether this is an absolute or a relative time.
1809 */
1810 if (expires && !expires->tv64) {
1811 __set_current_state(TASK_RUNNING);
1812 return 0;
1813 }
1814
1815 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001816 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001817 */
1818 if (!expires) {
1819 schedule();
1820 __set_current_state(TASK_RUNNING);
1821 return -EINTR;
1822 }
1823
1824 hrtimer_init_on_stack(&t.timer, clock, mode);
1825 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1826
1827 hrtimer_init_sleeper(&t, current);
1828
1829 hrtimer_start_expires(&t.timer, mode);
1830 if (!hrtimer_active(&t.timer))
1831 t.task = NULL;
1832
1833 if (likely(t.task))
1834 schedule();
1835
1836 hrtimer_cancel(&t.timer);
1837 destroy_hrtimer_on_stack(&t.timer);
1838
1839 __set_current_state(TASK_RUNNING);
1840
1841 return !t.task ? 0 : -EINTR;
1842}
1843
1844/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001845 * schedule_hrtimeout_range - sleep until timeout
1846 * @expires: timeout value (ktime_t)
1847 * @delta: slack in expires timeout (ktime_t)
1848 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1849 *
1850 * Make the current task sleep until the given expiry time has
1851 * elapsed. The routine will return immediately unless
1852 * the current task state has been set (see set_current_state()).
1853 *
1854 * The @delta argument gives the kernel the freedom to schedule the
1855 * actual wakeup to a time that is both power and performance friendly.
1856 * The kernel give the normal best effort behavior for "@expires+@delta",
1857 * but may decide to fire the timer earlier, but no earlier than @expires.
1858 *
1859 * You can set the task state as follows -
1860 *
1861 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1862 * pass before the routine returns.
1863 *
1864 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1865 * delivered to the current task.
1866 *
1867 * The current task state is guaranteed to be TASK_RUNNING when this
1868 * routine returns.
1869 *
1870 * Returns 0 when the timer has expired otherwise -EINTR
1871 */
1872int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001873 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001874{
Carsten Emde351b3f72010-04-02 22:40:19 +02001875 return schedule_hrtimeout_range_clock(expires, delta, mode,
1876 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001877}
1878EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1879
1880/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001881 * schedule_hrtimeout - sleep until timeout
1882 * @expires: timeout value (ktime_t)
1883 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1884 *
1885 * Make the current task sleep until the given expiry time has
1886 * elapsed. The routine will return immediately unless
1887 * the current task state has been set (see set_current_state()).
1888 *
1889 * You can set the task state as follows -
1890 *
1891 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1892 * pass before the routine returns.
1893 *
1894 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1895 * delivered to the current task.
1896 *
1897 * The current task state is guaranteed to be TASK_RUNNING when this
1898 * routine returns.
1899 *
1900 * Returns 0 when the timer has expired otherwise -EINTR
1901 */
1902int __sched schedule_hrtimeout(ktime_t *expires,
1903 const enum hrtimer_mode mode)
1904{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001905 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001906}
1907EXPORT_SYMBOL_GPL(schedule_hrtimeout);