blob: 258720741d3e6deeb4e493eda81e01f9a164430e [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>
Clark Williamscf4aebc22013-02-07 09:46:59 -060047#include <linux/sched/sysctl.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060048#include <linux/sched/rt.h>
Arun R Bharadwajeea08f32009-04-16 12:16:41 +053049#include <linux/timer.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080050
51#include <asm/uaccess.h>
52
Xiao Guangrongc6a2a172009-08-10 10:51:23 +080053#include <trace/events/timer.h>
54
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080055/*
56 * The timer bases:
George Anzinger79786722006-02-01 03:05:11 -080057 *
John Stultze06383d2010-12-14 19:37:07 -080058 * There are more clockids then hrtimer bases. Thus, we index
59 * into the timer bases by the hrtimer_base_type enum. When trying
60 * to reach a base using a clockid, hrtimer_clockid_to_base()
61 * is used to convert from clockid to the proper hrtimer_base_type.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080062 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080063DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080064{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080065
66 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080067 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080068 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020069 .index = HRTIMER_BASE_MONOTONIC,
70 .clockid = CLOCK_MONOTONIC,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080071 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080072 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080073 },
John Stultz70a08cc2011-02-15 10:45:16 -080074 {
Thomas Gleixner68fa61c2011-05-20 23:14:04 +020075 .index = HRTIMER_BASE_REALTIME,
76 .clockid = CLOCK_REALTIME,
77 .get_time = &ktime_get_real,
78 .resolution = KTIME_LOW_RES,
79 },
80 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020081 .index = HRTIMER_BASE_BOOTTIME,
82 .clockid = CLOCK_BOOTTIME,
John Stultz70a08cc2011-02-15 10:45:16 -080083 .get_time = &ktime_get_boottime,
84 .resolution = KTIME_LOW_RES,
85 },
John Stultz90adda92013-01-21 17:00:11 -080086 {
87 .index = HRTIMER_BASE_TAI,
88 .clockid = CLOCK_TAI,
89 .get_time = &ktime_get_clocktai,
90 .resolution = KTIME_LOW_RES,
91 },
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080092 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080093};
94
Mike Frysinger942c3c52011-05-02 15:24:27 -040095static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
Thomas Gleixnerce313322011-04-29 00:02:00 +020096 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
97 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
98 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
John Stultz90adda92013-01-21 17:00:11 -080099 [CLOCK_TAI] = HRTIMER_BASE_TAI,
Thomas Gleixnerce313322011-04-29 00:02:00 +0200100};
John Stultze06383d2010-12-14 19:37:07 -0800101
102static inline int hrtimer_clockid_to_base(clockid_t clock_id)
103{
104 return hrtimer_clock_to_base_table[clock_id];
105}
106
107
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800108/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800109 * Get the coarse grained time at the softirq based on xtime and
110 * wall_to_monotonic.
111 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800112static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800113{
John Stultz70a08cc2011-02-15 10:45:16 -0800114 ktime_t xtim, mono, boot;
John Stultz314ac372011-02-14 18:43:08 -0800115 struct timespec xts, tom, slp;
John Stultz90adda92013-01-21 17:00:11 -0800116 s32 tai_offset;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800117
John Stultz314ac372011-02-14 18:43:08 -0800118 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
John Stultz90adda92013-01-21 17:00:11 -0800119 tai_offset = timekeeping_get_tai_offset();
Thomas Gleixner92127c72006-03-26 01:38:05 -0800120
john stultzf4304ab2007-02-16 01:27:26 -0800121 xtim = timespec_to_ktime(xts);
John Stultz70a08cc2011-02-15 10:45:16 -0800122 mono = ktime_add(xtim, timespec_to_ktime(tom));
123 boot = ktime_add(mono, timespec_to_ktime(slp));
John Stultze06383d2010-12-14 19:37:07 -0800124 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
John Stultz70a08cc2011-02-15 10:45:16 -0800125 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
126 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
John Stultz90adda92013-01-21 17:00:11 -0800127 base->clock_base[HRTIMER_BASE_TAI].softirq_time =
128 ktime_add(xtim, ktime_set(tai_offset, 0));
Thomas Gleixner92127c72006-03-26 01:38:05 -0800129}
130
131/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800132 * Functions and macros which are different for UP/SMP systems are kept in a
133 * single place
134 */
135#ifdef CONFIG_SMP
136
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800137/*
138 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
139 * means that all timers which are tied to this base via timer->base are
140 * locked, and the base itself is locked too.
141 *
142 * So __run_timers/migrate_timers can safely modify all timers which could
143 * be found on the lists/queues.
144 *
145 * When the timer's base is locked, and the timer removed from list, it is
146 * possible to set timer->base = NULL and drop the lock: the timer remains
147 * locked.
148 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800149static
150struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
151 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800152{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800153 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800154
155 for (;;) {
156 base = timer->base;
157 if (likely(base != NULL)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100158 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800159 if (likely(base == timer->base))
160 return base;
161 /* The timer has migrated to another CPU: */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100162 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800163 }
164 cpu_relax();
165 }
166}
167
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200168
169/*
170 * Get the preferred target CPU for NOHZ
171 */
172static int hrtimer_get_target(int this_cpu, int pinned)
173{
174#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700175 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
176 return get_nohz_timer_target();
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200177#endif
178 return this_cpu;
179}
180
181/*
182 * With HIGHRES=y we do not migrate the timer when it is expiring
183 * before the next event on the target cpu because we cannot reprogram
184 * the target cpu hardware and we would cause it to fire late.
185 *
186 * Called with cpu_base->lock of target cpu held.
187 */
188static int
189hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
190{
191#ifdef CONFIG_HIGH_RES_TIMERS
192 ktime_t expires;
193
194 if (!new_base->cpu_base->hres_active)
195 return 0;
196
197 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
198 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
199#else
200 return 0;
201#endif
202}
203
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800204/*
205 * Switch the timer base to the current CPU when possible.
206 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800207static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530208switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
209 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800210{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800211 struct hrtimer_clock_base *new_base;
212 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200213 int this_cpu = smp_processor_id();
214 int cpu = hrtimer_get_target(this_cpu, pinned);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200215 int basenum = base->index;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530216
217again:
218 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
John Stultze06383d2010-12-14 19:37:07 -0800219 new_base = &new_cpu_base->clock_base[basenum];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800220
221 if (base != new_base) {
222 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200223 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800224 * However we can't change timer's base while it is running,
225 * so we keep it on the same CPU. No hassle vs. reprogramming
226 * the event source in the high resolution case. The softirq
227 * code will take care of this when the timer function has
228 * completed. There is no conflict as we hold the lock until
229 * the timer is enqueued.
230 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800231 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800232 return base;
233
234 /* See the comment in lock_timer_base() */
235 timer->base = NULL;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100236 raw_spin_unlock(&base->cpu_base->lock);
237 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530238
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200239 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
240 cpu = this_cpu;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100241 raw_spin_unlock(&new_base->cpu_base->lock);
242 raw_spin_lock(&base->cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200243 timer->base = base;
244 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530245 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800246 timer->base = new_base;
247 }
248 return new_base;
249}
250
251#else /* CONFIG_SMP */
252
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800253static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800254lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
255{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800256 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800257
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100258 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800259
260 return base;
261}
262
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530263# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800264
265#endif /* !CONFIG_SMP */
266
267/*
268 * Functions for the union type storage format of ktime_t which are
269 * too large for inlining:
270 */
271#if BITS_PER_LONG < 64
272# ifndef CONFIG_KTIME_SCALAR
273/**
274 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800275 * @kt: addend
276 * @nsec: the scalar nsec value to add
277 *
278 * Returns the sum of kt and nsec in ktime_t format
279 */
280ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
281{
282 ktime_t tmp;
283
284 if (likely(nsec < NSEC_PER_SEC)) {
285 tmp.tv64 = nsec;
286 } else {
287 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
288
289 tmp = ktime_set((long)nsec, rem);
290 }
291
292 return ktime_add(kt, tmp);
293}
David Howellsb8b8fd22007-04-27 15:31:24 -0700294
295EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700296
297/**
298 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
299 * @kt: minuend
300 * @nsec: the scalar nsec value to subtract
301 *
302 * Returns the subtraction of @nsec from @kt in ktime_t format
303 */
304ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
305{
306 ktime_t tmp;
307
308 if (likely(nsec < NSEC_PER_SEC)) {
309 tmp.tv64 = nsec;
310 } else {
311 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
312
313 tmp = ktime_set((long)nsec, rem);
314 }
315
316 return ktime_sub(kt, tmp);
317}
318
319EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800320# endif /* !CONFIG_KTIME_SCALAR */
321
322/*
323 * Divide a ktime value by a nanosecond value
324 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800325u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800326{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300327 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800328 int sft = 0;
329
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300330 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800331 /* Make sure the divisor is less than 2^32: */
332 while (div >> 32) {
333 sft++;
334 div >>= 1;
335 }
336 dclc >>= sft;
337 do_div(dclc, (unsigned long) div);
338
Davide Libenzi4d672e72008-02-04 22:27:26 -0800339 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800340}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800341#endif /* BITS_PER_LONG >= 64 */
342
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100343/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100344 * Add two ktime values and do a safety check for overflow:
345 */
346ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
347{
348 ktime_t res = ktime_add(lhs, rhs);
349
350 /*
351 * We use KTIME_SEC_MAX here, the maximum timeout which we can
352 * return to user space in a timespec:
353 */
354 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
355 res = ktime_set(KTIME_SEC_MAX, 0);
356
357 return res;
358}
359
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300360EXPORT_SYMBOL_GPL(ktime_add_safe);
361
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700362#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
363
364static struct debug_obj_descr hrtimer_debug_descr;
365
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100366static void *hrtimer_debug_hint(void *addr)
367{
368 return ((struct hrtimer *) addr)->function;
369}
370
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700371/*
372 * fixup_init is called when:
373 * - an active object is initialized
374 */
375static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
376{
377 struct hrtimer *timer = addr;
378
379 switch (state) {
380 case ODEBUG_STATE_ACTIVE:
381 hrtimer_cancel(timer);
382 debug_object_init(timer, &hrtimer_debug_descr);
383 return 1;
384 default:
385 return 0;
386 }
387}
388
389/*
390 * fixup_activate is called when:
391 * - an active object is activated
392 * - an unknown object is activated (might be a statically initialized object)
393 */
394static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
395{
396 switch (state) {
397
398 case ODEBUG_STATE_NOTAVAILABLE:
399 WARN_ON_ONCE(1);
400 return 0;
401
402 case ODEBUG_STATE_ACTIVE:
403 WARN_ON(1);
404
405 default:
406 return 0;
407 }
408}
409
410/*
411 * fixup_free is called when:
412 * - an active object is freed
413 */
414static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
415{
416 struct hrtimer *timer = addr;
417
418 switch (state) {
419 case ODEBUG_STATE_ACTIVE:
420 hrtimer_cancel(timer);
421 debug_object_free(timer, &hrtimer_debug_descr);
422 return 1;
423 default:
424 return 0;
425 }
426}
427
428static struct debug_obj_descr hrtimer_debug_descr = {
429 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100430 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700431 .fixup_init = hrtimer_fixup_init,
432 .fixup_activate = hrtimer_fixup_activate,
433 .fixup_free = hrtimer_fixup_free,
434};
435
436static inline void debug_hrtimer_init(struct hrtimer *timer)
437{
438 debug_object_init(timer, &hrtimer_debug_descr);
439}
440
441static inline void debug_hrtimer_activate(struct hrtimer *timer)
442{
443 debug_object_activate(timer, &hrtimer_debug_descr);
444}
445
446static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
447{
448 debug_object_deactivate(timer, &hrtimer_debug_descr);
449}
450
451static inline void debug_hrtimer_free(struct hrtimer *timer)
452{
453 debug_object_free(timer, &hrtimer_debug_descr);
454}
455
456static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
457 enum hrtimer_mode mode);
458
459void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
460 enum hrtimer_mode mode)
461{
462 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
463 __hrtimer_init(timer, clock_id, mode);
464}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700465EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700466
467void destroy_hrtimer_on_stack(struct hrtimer *timer)
468{
469 debug_object_free(timer, &hrtimer_debug_descr);
470}
471
472#else
473static inline void debug_hrtimer_init(struct hrtimer *timer) { }
474static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
475static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
476#endif
477
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800478static inline void
479debug_init(struct hrtimer *timer, clockid_t clockid,
480 enum hrtimer_mode mode)
481{
482 debug_hrtimer_init(timer);
483 trace_hrtimer_init(timer, clockid, mode);
484}
485
486static inline void debug_activate(struct hrtimer *timer)
487{
488 debug_hrtimer_activate(timer);
489 trace_hrtimer_start(timer);
490}
491
492static inline void debug_deactivate(struct hrtimer *timer)
493{
494 debug_hrtimer_deactivate(timer);
495 trace_hrtimer_cancel(timer);
496}
497
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800498/* High resolution timer related functions */
499#ifdef CONFIG_HIGH_RES_TIMERS
500
501/*
502 * High resolution timer enabled ?
503 */
504static int hrtimer_hres_enabled __read_mostly = 1;
505
506/*
507 * Enable / Disable high resolution mode
508 */
509static int __init setup_hrtimer_hres(char *str)
510{
511 if (!strcmp(str, "off"))
512 hrtimer_hres_enabled = 0;
513 else if (!strcmp(str, "on"))
514 hrtimer_hres_enabled = 1;
515 else
516 return 0;
517 return 1;
518}
519
520__setup("highres=", setup_hrtimer_hres);
521
522/*
523 * hrtimer_high_res_enabled - query, if the highres mode is enabled
524 */
525static inline int hrtimer_is_hres_enabled(void)
526{
527 return hrtimer_hres_enabled;
528}
529
530/*
531 * Is the high resolution mode active ?
532 */
533static inline int hrtimer_hres_active(void)
534{
Christoph Lameter909ea962010-12-08 16:22:55 +0100535 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800536}
537
538/*
539 * Reprogram the event source with checking both queues for the
540 * next event
541 * Called with interrupts disabled and base->lock held
542 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400543static void
544hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800545{
546 int i;
547 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400548 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800549
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400550 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800551
552 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
553 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700554 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800555
John Stultz998adc32010-09-20 19:19:17 -0700556 next = timerqueue_getnext(&base->active);
557 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800558 continue;
John Stultz998adc32010-09-20 19:19:17 -0700559 timer = container_of(next, struct hrtimer, node);
560
Arjan van de Vencc584b22008-09-01 15:02:30 -0700561 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100562 /*
563 * clock_was_set() has changed base->offset so the
564 * result might be negative. Fix it up to prevent a
565 * false positive in clockevents_program_event()
566 */
567 if (expires.tv64 < 0)
568 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400569 if (expires.tv64 < expires_next.tv64)
570 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800571 }
572
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400573 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
574 return;
575
576 cpu_base->expires_next.tv64 = expires_next.tv64;
577
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800578 if (cpu_base->expires_next.tv64 != KTIME_MAX)
579 tick_program_event(cpu_base->expires_next, 1);
580}
581
582/*
583 * Shared reprogramming for clock_realtime and clock_monotonic
584 *
585 * When a timer is enqueued and expires earlier than the already enqueued
586 * timers, we have to check, whether it expires earlier than the timer for
587 * which the clock event device was armed.
588 *
589 * Called with interrupts disabled and base->cpu_base.lock held
590 */
591static int hrtimer_reprogram(struct hrtimer *timer,
592 struct hrtimer_clock_base *base)
593{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100594 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700595 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800596 int res;
597
Arjan van de Vencc584b22008-09-01 15:02:30 -0700598 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100599
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800600 /*
601 * When the callback is running, we do not reprogram the clock event
602 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200603 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800604 * reprogramming is handled either by the softirq, which called the
605 * callback or at the end of the hrtimer_interrupt.
606 */
607 if (hrtimer_callback_running(timer))
608 return 0;
609
Thomas Gleixner63070a72008-02-14 00:58:36 +0100610 /*
611 * CLOCK_REALTIME timer might be requested with an absolute
612 * expiry time which is less than base->offset. Nothing wrong
613 * about that, just avoid to call into the tick code, which
614 * has now objections against negative expiry values.
615 */
616 if (expires.tv64 < 0)
617 return -ETIME;
618
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100619 if (expires.tv64 >= cpu_base->expires_next.tv64)
620 return 0;
621
622 /*
623 * If a hang was detected in the last timer interrupt then we
624 * do not schedule a timer which is earlier than the expiry
625 * which we enforced in the hang detection. We want the system
626 * to make progress.
627 */
628 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800629 return 0;
630
631 /*
632 * Clockevents returns -ETIME, when the event was in the past.
633 */
634 res = tick_program_event(expires, 0);
635 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100636 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800637 return res;
638}
639
Ingo Molnar995f0542007-04-07 12:05:00 +0200640/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800641 * Initialize the high resolution related parts of cpu_base
642 */
643static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
644{
645 base->expires_next.tv64 = KTIME_MAX;
646 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800647}
648
649/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800650 * When High resolution timers are active, try to reprogram. Note, that in case
651 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
652 * check happens. The timer gets enqueued into the rbtree. The reprogramming
653 * and expiry check is done in the hrtimer_interrupt or in the softirq.
654 */
655static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzb22affe2013-02-04 14:33:37 +0200656 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800657{
Leonid Shatzb22affe2013-02-04 14:33:37 +0200658 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800659}
660
John Stultz5baefd62012-07-10 18:43:25 -0400661static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
662{
663 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
664 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
John Stultz90adda92013-01-21 17:00:11 -0800665 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
John Stultz5baefd62012-07-10 18:43:25 -0400666
John Stultz90adda92013-01-21 17:00:11 -0800667 return ktime_get_update_offsets(offs_real, offs_boot, offs_tai);
John Stultz5baefd62012-07-10 18:43:25 -0400668}
669
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200670/*
671 * Retrigger next event is called after clock was set
672 *
673 * Called with interrupts disabled via on_each_cpu()
674 */
675static void retrigger_next_event(void *arg)
676{
677 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200678
679 if (!hrtimer_hres_active())
680 return;
681
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200682 raw_spin_lock(&base->lock);
John Stultz5baefd62012-07-10 18:43:25 -0400683 hrtimer_update_base(base);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200684 hrtimer_force_reprogram(base, 0);
685 raw_spin_unlock(&base->lock);
686}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200687
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800688/*
689 * Switch to high resolution mode
690 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800691static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800692{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200693 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700694 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800695 unsigned long flags;
696
697 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800698 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800699
700 local_irq_save(flags);
701
702 if (tick_init_highres()) {
703 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700704 printk(KERN_WARNING "Could not switch to high resolution "
705 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800706 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800707 }
708 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200709 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
710 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800711
712 tick_setup_sched_timer();
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800713 /* "Retrigger" the interrupt to get things going */
714 retrigger_next_event(NULL);
715 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800716 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800717}
718
John Stultzf55a6fa2012-07-10 18:43:19 -0400719/*
720 * Called from timekeeping code to reprogramm the hrtimer interrupt
721 * device. If called from the timer interrupt context we defer it to
722 * softirq context.
723 */
724void clock_was_set_delayed(void)
725{
726 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
727
728 cpu_base->clock_was_set = 1;
729 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
730}
731
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800732#else
733
734static inline int hrtimer_hres_active(void) { return 0; }
735static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800736static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400737static inline void
738hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800739static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzb22affe2013-02-04 14:33:37 +0200740 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800741{
742 return 0;
743}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800744static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200745static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800746
747#endif /* CONFIG_HIGH_RES_TIMERS */
748
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200749/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200750 * Clock realtime was set
751 *
752 * Change the offset of the realtime clock vs. the monotonic
753 * clock.
754 *
755 * We might have to reprogram the high resolution timer interrupt. On
756 * SMP we call the architecture specific code to retrigger _all_ high
757 * resolution timer interrupts. On UP we just disable interrupts and
758 * call the high resolution interrupt code.
759 */
760void clock_was_set(void)
761{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200762#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200763 /* Retrigger the CPU local events everywhere */
764 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200765#endif
766 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200767}
768
769/*
770 * During resume we might have to reprogram the high resolution timer
771 * interrupt (on the local CPU):
772 */
773void hrtimers_resume(void)
774{
775 WARN_ONCE(!irqs_disabled(),
776 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
777
778 retrigger_next_event(NULL);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200779 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200780}
781
Heiko Carstens5f201902009-12-10 10:56:29 +0100782static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800783{
Heiko Carstens5f201902009-12-10 10:56:29 +0100784#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800785 if (timer->start_site)
786 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100787 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800788 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
789 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800790#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100791}
792
793static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
794{
795#ifdef CONFIG_TIMER_STATS
796 timer->start_site = NULL;
797#endif
798}
799
800static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
801{
802#ifdef CONFIG_TIMER_STATS
803 if (likely(!timer_stats_active))
804 return;
805 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
806 timer->function, timer->start_comm, 0);
807#endif
808}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800809
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800810/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200811 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812 */
813static inline
814void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
815{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100816 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817}
818
819/**
820 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800822 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823 * @interval: the interval to forward
824 *
825 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700826 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800827 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800828u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800829{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800830 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800831 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832
Arjan van de Vencc584b22008-09-01 15:02:30 -0700833 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800834
835 if (delta.tv64 < 0)
836 return 0;
837
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100838 if (interval.tv64 < timer->base->resolution.tv64)
839 interval.tv64 = timer->base->resolution.tv64;
840
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800841 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800842 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800843
844 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700845 hrtimer_add_expires_ns(timer, incr * orun);
846 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800847 return orun;
848 /*
849 * This (and the ktime_add() below) is the
850 * correction for exact:
851 */
852 orun++;
853 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700854 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800855
856 return orun;
857}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700858EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800859
860/*
861 * enqueue_hrtimer - internal function to (re)start a timer
862 *
863 * The timer is inserted in expiry order. Insertion into the
864 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100865 *
866 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800867 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100868static int enqueue_hrtimer(struct hrtimer *timer,
869 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800870{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800871 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700872
John Stultz998adc32010-09-20 19:19:17 -0700873 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200874 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800875
876 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800877 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
878 * state of a possibly running callback.
879 */
880 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100881
John Stultz998adc32010-09-20 19:19:17 -0700882 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100883}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800884
885/*
886 * __remove_hrtimer - internal function to remove a timer
887 *
888 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800889 *
890 * High resolution timer mode reprograms the clock event device when the
891 * timer is the one which expires next. The caller can disable this by setting
892 * reprogram to zero. This is useful, when the context does a reprogramming
893 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800894 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800895static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800896 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800897 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800898{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800899 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400900 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
901 goto out;
902
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800903 next_timer = timerqueue_getnext(&base->active);
904 timerqueue_del(&base->active, &timer->node);
905 if (&timer->node == next_timer) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400906#ifdef CONFIG_HIGH_RES_TIMERS
907 /* Reprogram the clock event device. if enabled */
908 if (reprogram && hrtimer_hres_active()) {
909 ktime_t expires;
910
911 expires = ktime_sub(hrtimer_get_expires(timer),
912 base->offset);
913 if (base->cpu_base->expires_next.tv64 == expires.tv64)
914 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800915 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400916#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800917 }
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200918 if (!timerqueue_getnext(&base->active))
919 base->cpu_base->active_bases &= ~(1 << base->index);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400920out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800921 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800922}
923
924/*
925 * remove hrtimer, called with base lock held
926 */
927static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800928remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800929{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800930 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700931 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800932 int reprogram;
933
934 /*
935 * Remove the timer and force reprogramming when high
936 * resolution mode is active and the timer is on the current
937 * CPU. If we remove a timer on another CPU, reprogramming is
938 * skipped. The interrupt event on this CPU is fired and
939 * reprogramming happens in the interrupt handler. This is a
940 * rare case and less expensive than a smp call.
941 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800942 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800943 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800944 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700945 /*
946 * We must preserve the CALLBACK state flag here,
947 * otherwise we could move the timer base in
948 * switch_hrtimer_base.
949 */
950 state = timer->state & HRTIMER_STATE_CALLBACK;
951 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800952 return 1;
953 }
954 return 0;
955}
956
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100957int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
958 unsigned long delta_ns, const enum hrtimer_mode mode,
959 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800960{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800961 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800962 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100963 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800964
965 base = lock_hrtimer_base(timer, &flags);
966
967 /* Remove an active timer from the queue: */
968 ret = remove_hrtimer(timer, base);
969
970 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530971 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800972
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530973 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100974 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800975 /*
976 * CONFIG_TIME_LOW_RES is a temporary way for architectures
977 * to signal that they simply return xtime in
978 * do_gettimeoffset(). In this case we want to round up by
979 * resolution when starting a relative timer, to avoid short
980 * timeouts. This will go away with the GTOD framework.
981 */
982#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100983 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800984#endif
985 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700986
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700987 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800988
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800989 timer_stats_hrtimer_set_start_info(timer);
990
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100991 leftmost = enqueue_hrtimer(timer, new_base);
992
Ingo Molnar935c6312007-03-28 13:17:18 +0200993 /*
994 * Only allow reprogramming if the new base is on this CPU.
995 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100996 *
997 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200998 */
Leonid Shatzb22affe2013-02-04 14:33:37 +0200999 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
1000 && hrtimer_enqueue_reprogram(timer, new_base)) {
1001 if (wakeup) {
1002 /*
1003 * We need to drop cpu_base->lock to avoid a
1004 * lock ordering issue vs. rq->lock.
1005 */
1006 raw_spin_unlock(&new_base->cpu_base->lock);
1007 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1008 local_irq_restore(flags);
1009 return ret;
1010 } else {
1011 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1012 }
1013 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001014
1015 unlock_hrtimer_base(timer, &flags);
1016
1017 return ret;
1018}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001019
1020/**
1021 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1022 * @timer: the timer to be added
1023 * @tim: expiry time
1024 * @delta_ns: "slack" range for the timer
1025 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1026 *
1027 * Returns:
1028 * 0 on success
1029 * 1 when the timer was active
1030 */
1031int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1032 unsigned long delta_ns, const enum hrtimer_mode mode)
1033{
1034 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1035}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001036EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1037
1038/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001039 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001040 * @timer: the timer to be added
1041 * @tim: expiry time
1042 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1043 *
1044 * Returns:
1045 * 0 on success
1046 * 1 when the timer was active
1047 */
1048int
1049hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1050{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001051 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001052}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001053EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001054
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001055
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001056/**
1057 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001058 * @timer: hrtimer to stop
1059 *
1060 * Returns:
1061 * 0 when the timer was not active
1062 * 1 when the timer was active
1063 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001064 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001065 */
1066int hrtimer_try_to_cancel(struct hrtimer *timer)
1067{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001068 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001069 unsigned long flags;
1070 int ret = -1;
1071
1072 base = lock_hrtimer_base(timer, &flags);
1073
Thomas Gleixner303e9672007-02-16 01:27:51 -08001074 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001075 ret = remove_hrtimer(timer, base);
1076
1077 unlock_hrtimer_base(timer, &flags);
1078
1079 return ret;
1080
1081}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001082EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001083
1084/**
1085 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001086 * @timer: the timer to be cancelled
1087 *
1088 * Returns:
1089 * 0 when the timer was not active
1090 * 1 when the timer was active
1091 */
1092int hrtimer_cancel(struct hrtimer *timer)
1093{
1094 for (;;) {
1095 int ret = hrtimer_try_to_cancel(timer);
1096
1097 if (ret >= 0)
1098 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001099 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001100 }
1101}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001102EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001103
1104/**
1105 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001106 * @timer: the timer to read
1107 */
1108ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1109{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001110 unsigned long flags;
1111 ktime_t rem;
1112
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001113 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001114 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001115 unlock_hrtimer_base(timer, &flags);
1116
1117 return rem;
1118}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001119EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001120
Russell Kingee9c5782008-04-20 13:59:33 +01001121#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001122/**
1123 * hrtimer_get_next_event - get the time until next expiry event
1124 *
1125 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1126 * is pending.
1127 */
1128ktime_t hrtimer_get_next_event(void)
1129{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001130 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1131 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001132 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1133 unsigned long flags;
1134 int i;
1135
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001136 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001137
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001138 if (!hrtimer_hres_active()) {
1139 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1140 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001141 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001142
John Stultz998adc32010-09-20 19:19:17 -07001143 next = timerqueue_getnext(&base->active);
1144 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001145 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001146
John Stultz998adc32010-09-20 19:19:17 -07001147 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001148 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001149 delta = ktime_sub(delta, base->get_time());
1150 if (delta.tv64 < mindelta.tv64)
1151 mindelta.tv64 = delta.tv64;
1152 }
Tony Lindgren69239742006-03-06 15:42:45 -08001153 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001154
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001155 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001156
Tony Lindgren69239742006-03-06 15:42:45 -08001157 if (mindelta.tv64 < 0)
1158 mindelta.tv64 = 0;
1159 return mindelta;
1160}
1161#endif
1162
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001163static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1164 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001165{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001166 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001167 int base;
George Anzinger79786722006-02-01 03:05:11 -08001168
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001169 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001170
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001171 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001172
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001173 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001174 clock_id = CLOCK_MONOTONIC;
1175
John Stultze06383d2010-12-14 19:37:07 -08001176 base = hrtimer_clockid_to_base(clock_id);
1177 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001178 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001179
1180#ifdef CONFIG_TIMER_STATS
1181 timer->start_site = NULL;
1182 timer->start_pid = -1;
1183 memset(timer->start_comm, 0, TASK_COMM_LEN);
1184#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001185}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001186
1187/**
1188 * hrtimer_init - initialize a timer to the given clock
1189 * @timer: the timer to be initialized
1190 * @clock_id: the clock to be used
1191 * @mode: timer mode abs/rel
1192 */
1193void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1194 enum hrtimer_mode mode)
1195{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001196 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001197 __hrtimer_init(timer, clock_id, mode);
1198}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001199EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001200
1201/**
1202 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001203 * @which_clock: which clock to query
1204 * @tp: pointer to timespec variable to store the resolution
1205 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001206 * Store the resolution of the clock selected by @which_clock in the
1207 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001208 */
1209int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1210{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001211 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001212 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001213
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001214 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001215 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001216
1217 return 0;
1218}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001219EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001220
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001221static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001222{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001223 struct hrtimer_clock_base *base = timer->base;
1224 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1225 enum hrtimer_restart (*fn)(struct hrtimer *);
1226 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001227
Peter Zijlstraca109492008-11-25 12:43:51 +01001228 WARN_ON(!irqs_disabled());
1229
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001230 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001231 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1232 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001233 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001234
1235 /*
1236 * Because we run timers from hardirq context, there is no chance
1237 * they get migrated to another cpu, therefore its safe to unlock
1238 * the timer base.
1239 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001240 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001241 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001242 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001243 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001244 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001245
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001246 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001247 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1248 * we do not reprogramm the event hardware. Happens either in
1249 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001250 */
1251 if (restart != HRTIMER_NORESTART) {
1252 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001253 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001254 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001255
1256 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1257
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001258 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001259}
1260
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001261#ifdef CONFIG_HIGH_RES_TIMERS
1262
1263/*
1264 * High resolution timer interrupt
1265 * Called with interrupts disabled
1266 */
1267void hrtimer_interrupt(struct clock_event_device *dev)
1268{
1269 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001270 ktime_t expires_next, now, entry_time, delta;
1271 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001272
1273 BUG_ON(!cpu_base->hres_active);
1274 cpu_base->nr_events++;
1275 dev->next_event.tv64 = KTIME_MAX;
1276
Thomas Gleixner196951e2012-07-10 18:43:23 -04001277 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001278 entry_time = now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001279retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001280 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001281 /*
1282 * We set expires_next to KTIME_MAX here with cpu_base->lock
1283 * held to prevent that a timer is enqueued in our queue via
1284 * the migration code. This does not affect enqueueing of
1285 * timers which run their callback and need to be requeued on
1286 * this CPU.
1287 */
1288 cpu_base->expires_next.tv64 = KTIME_MAX;
1289
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001290 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001291 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001292 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001293 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001294
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001295 if (!(cpu_base->active_bases & (1 << i)))
1296 continue;
1297
1298 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001299 basenow = ktime_add(now, base->offset);
1300
John Stultz998adc32010-09-20 19:19:17 -07001301 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001302 struct hrtimer *timer;
1303
John Stultz998adc32010-09-20 19:19:17 -07001304 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001305
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001306 /*
1307 * The immediate goal for using the softexpires is
1308 * minimizing wakeups, not running timers at the
1309 * earliest interrupt after their soft expiration.
1310 * This allows us to avoid using a Priority Search
1311 * Tree, which can answer a stabbing querry for
1312 * overlapping intervals and instead use the simple
1313 * BST we already have.
1314 * We don't add extra wakeups by delaying timers that
1315 * are right-of a not yet expired timer, because that
1316 * timer will have to trigger a wakeup anyway.
1317 */
1318
1319 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001320 ktime_t expires;
1321
Arjan van de Vencc584b22008-09-01 15:02:30 -07001322 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001323 base->offset);
1324 if (expires.tv64 < expires_next.tv64)
1325 expires_next = expires;
1326 break;
1327 }
1328
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001329 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001330 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001331 }
1332
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001333 /*
1334 * Store the new expiry value so the migration code can verify
1335 * against it.
1336 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001337 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001338 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001339
1340 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001341 if (expires_next.tv64 == KTIME_MAX ||
1342 !tick_program_event(expires_next, 0)) {
1343 cpu_base->hang_detected = 0;
1344 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001345 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001346
1347 /*
1348 * The next timer was already expired due to:
1349 * - tracing
1350 * - long lasting callbacks
1351 * - being scheduled away when running in a VM
1352 *
1353 * We need to prevent that we loop forever in the hrtimer
1354 * interrupt routine. We give it 3 attempts to avoid
1355 * overreacting on some spurious event.
John Stultz5baefd62012-07-10 18:43:25 -04001356 *
1357 * Acquire base lock for updating the offsets and retrieving
1358 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001359 */
Thomas Gleixner196951e2012-07-10 18:43:23 -04001360 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001361 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001362 cpu_base->nr_retries++;
1363 if (++retries < 3)
1364 goto retry;
1365 /*
1366 * Give the system a chance to do something else than looping
1367 * here. We stored the entry time, so we know exactly how long
1368 * we spent here. We schedule the next event this amount of
1369 * time away.
1370 */
1371 cpu_base->nr_hangs++;
1372 cpu_base->hang_detected = 1;
Thomas Gleixner196951e2012-07-10 18:43:23 -04001373 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001374 delta = ktime_sub(now, entry_time);
1375 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1376 cpu_base->max_hang_time = delta;
1377 /*
1378 * Limit it to a sensible value as we enforce a longer
1379 * delay. Give the CPU at least 100ms to catch up.
1380 */
1381 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1382 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1383 else
1384 expires_next = ktime_add(now, delta);
1385 tick_program_event(expires_next, 1);
1386 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1387 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001388}
1389
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001390/*
1391 * local version of hrtimer_peek_ahead_timers() called with interrupts
1392 * disabled.
1393 */
1394static void __hrtimer_peek_ahead_timers(void)
1395{
1396 struct tick_device *td;
1397
1398 if (!hrtimer_hres_active())
1399 return;
1400
1401 td = &__get_cpu_var(tick_cpu_device);
1402 if (td && td->evtdev)
1403 hrtimer_interrupt(td->evtdev);
1404}
1405
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001406/**
1407 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1408 *
1409 * hrtimer_peek_ahead_timers will peek at the timer queue of
1410 * the current cpu and check if there are any timers for which
1411 * the soft expires time has passed. If any such timers exist,
1412 * they are run immediately and then removed from the timer queue.
1413 *
1414 */
1415void hrtimer_peek_ahead_timers(void)
1416{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001417 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001418
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001419 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001420 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001421 local_irq_restore(flags);
1422}
1423
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001424static void run_hrtimer_softirq(struct softirq_action *h)
1425{
John Stultzf55a6fa2012-07-10 18:43:19 -04001426 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1427
1428 if (cpu_base->clock_was_set) {
1429 cpu_base->clock_was_set = 0;
1430 clock_was_set();
1431 }
1432
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001433 hrtimer_peek_ahead_timers();
1434}
1435
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001436#else /* CONFIG_HIGH_RES_TIMERS */
1437
1438static inline void __hrtimer_peek_ahead_timers(void) { }
1439
1440#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001441
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001442/*
1443 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001444 *
1445 * For HRT its the fall back code to run the softirq in the timer
1446 * softirq context in case the hrtimer initialization failed or has
1447 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001448 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001449void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001450{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001451 if (hrtimer_hres_active())
1452 return;
1453
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001454 /*
1455 * This _is_ ugly: We have to check in the softirq context,
1456 * whether we can switch to highres and / or nohz mode. The
1457 * clocksource switch happens in the timer interrupt with
1458 * xtime_lock held. Notification from there only sets the
1459 * check bit in the tick_oneshot code, otherwise we might
1460 * deadlock vs. xtime_lock.
1461 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001462 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001463 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001464}
1465
1466/*
1467 * Called from hardirq context every jiffy
1468 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001469void hrtimer_run_queues(void)
1470{
John Stultz998adc32010-09-20 19:19:17 -07001471 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001472 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001473 struct hrtimer_clock_base *base;
1474 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001475
1476 if (hrtimer_hres_active())
1477 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001478
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001479 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1480 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001481 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001482 continue;
1483
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001484 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001485 hrtimer_get_softirq_time(cpu_base);
1486 gettime = 0;
1487 }
1488
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001489 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001490
John Stultzb007c382010-12-10 22:19:53 -08001491 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001492 struct hrtimer *timer;
1493
John Stultz998adc32010-09-20 19:19:17 -07001494 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001495 if (base->softirq_time.tv64 <=
1496 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001497 break;
1498
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001499 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001500 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001501 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001502 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001503}
1504
1505/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001506 * Sleep related functions:
1507 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001508static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001509{
1510 struct hrtimer_sleeper *t =
1511 container_of(timer, struct hrtimer_sleeper, timer);
1512 struct task_struct *task = t->task;
1513
1514 t->task = NULL;
1515 if (task)
1516 wake_up_process(task);
1517
1518 return HRTIMER_NORESTART;
1519}
1520
Ingo Molnar36c8b582006-07-03 00:25:41 -07001521void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001522{
1523 sl->timer.function = hrtimer_wakeup;
1524 sl->task = task;
1525}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001526EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001527
Thomas Gleixner669d7862006-03-31 02:31:19 -08001528static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001529{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001530 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001531
Roman Zippel432569b2006-03-26 01:38:08 -08001532 do {
1533 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001534 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001535 if (!hrtimer_active(&t->timer))
1536 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001537
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001538 if (likely(t->task))
1539 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001540
Thomas Gleixner669d7862006-03-31 02:31:19 -08001541 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001542 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001543
Thomas Gleixner669d7862006-03-31 02:31:19 -08001544 } while (t->task && !signal_pending(current));
1545
Peter Zijlstra3588a082008-02-01 17:45:13 +01001546 __set_current_state(TASK_RUNNING);
1547
Thomas Gleixner669d7862006-03-31 02:31:19 -08001548 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001549}
1550
Oleg Nesterov080344b2008-02-01 17:29:05 +03001551static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1552{
1553 struct timespec rmt;
1554 ktime_t rem;
1555
Arjan van de Vencc584b22008-09-01 15:02:30 -07001556 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001557 if (rem.tv64 <= 0)
1558 return 0;
1559 rmt = ktime_to_timespec(rem);
1560
1561 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1562 return -EFAULT;
1563
1564 return 1;
1565}
1566
Toyo Abe1711ef32006-09-29 02:00:28 -07001567long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001568{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001569 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001570 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001571 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001572
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001573 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001574 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001575 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001576
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001577 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001578 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001579
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001580 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001581 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001582 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001583 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001584 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001585 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001586
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001587 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001588 ret = -ERESTART_RESTARTBLOCK;
1589out:
1590 destroy_hrtimer_on_stack(&t.timer);
1591 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001592}
1593
Oleg Nesterov080344b2008-02-01 17:29:05 +03001594long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001595 const enum hrtimer_mode mode, const clockid_t clockid)
1596{
1597 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001598 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001599 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001600 unsigned long slack;
1601
1602 slack = current->timer_slack_ns;
1603 if (rt_task(current))
1604 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001605
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001606 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001607 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001608 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001609 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001610
George Anzinger79786722006-02-01 03:05:11 -08001611 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001612 if (mode == HRTIMER_MODE_ABS) {
1613 ret = -ERESTARTNOHAND;
1614 goto out;
1615 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001616
Roman Zippel432569b2006-03-26 01:38:08 -08001617 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001618 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001619 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001620 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001621 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001622
1623 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001624 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001625 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001626 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001627 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001628
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001629 ret = -ERESTART_RESTARTBLOCK;
1630out:
1631 destroy_hrtimer_on_stack(&t.timer);
1632 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001633}
1634
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001635SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1636 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001637{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001638 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001639
1640 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1641 return -EFAULT;
1642
1643 if (!timespec_valid(&tu))
1644 return -EINVAL;
1645
Oleg Nesterov080344b2008-02-01 17:29:05 +03001646 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001647}
1648
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001649/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001650 * Functions related to boot-time initialization:
1651 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001652static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001653{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001654 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001655 int i;
1656
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001657 raw_spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001658
John Stultz998adc32010-09-20 19:19:17 -07001659 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001660 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001661 timerqueue_init_head(&cpu_base->clock_base[i].active);
1662 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001663
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001664 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001665}
1666
1667#ifdef CONFIG_HOTPLUG_CPU
1668
Peter Zijlstraca109492008-11-25 12:43:51 +01001669static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001670 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001671{
1672 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001673 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001674
John Stultz998adc32010-09-20 19:19:17 -07001675 while ((node = timerqueue_getnext(&old_base->active))) {
1676 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001677 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001678 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001679
1680 /*
1681 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1682 * timer could be seen as !active and just vanish away
1683 * under us on another CPU
1684 */
1685 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001686 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001687 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001688 * Enqueue the timers on the new cpu. This does not
1689 * reprogram the event device in case the timer
1690 * expires before the earliest on this CPU, but we run
1691 * hrtimer_interrupt after we migrated everything to
1692 * sort out already expired timers and reprogram the
1693 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001694 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001695 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001696
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001697 /* Clear the migration state bit */
1698 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001699 }
1700}
1701
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001702static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001703{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001704 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001705 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001706
Peter Zijlstra37810652008-12-04 11:17:10 +01001707 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001708 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001709
1710 local_irq_disable();
1711 old_base = &per_cpu(hrtimer_bases, scpu);
1712 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001713 /*
1714 * The caller is globally serialized and nobody else
1715 * takes two locks at once, deadlock is not possible.
1716 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001717 raw_spin_lock(&new_base->lock);
1718 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001719
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001720 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001721 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001722 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001723 }
1724
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001725 raw_spin_unlock(&old_base->lock);
1726 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001727
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001728 /* Check, if we got expired work to do */
1729 __hrtimer_peek_ahead_timers();
1730 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001731}
1732
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001733#endif /* CONFIG_HOTPLUG_CPU */
1734
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001735static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001736 unsigned long action, void *hcpu)
1737{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001738 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001739
1740 switch (action) {
1741
1742 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001743 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001744 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001745 break;
1746
1747#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001748 case CPU_DYING:
1749 case CPU_DYING_FROZEN:
1750 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1751 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001752 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001753 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001754 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001755 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001756 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001757 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001758 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001759#endif
1760
1761 default:
1762 break;
1763 }
1764
1765 return NOTIFY_OK;
1766}
1767
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001768static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001769 .notifier_call = hrtimer_cpu_notify,
1770};
1771
1772void __init hrtimers_init(void)
1773{
1774 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1775 (void *)(long)smp_processor_id());
1776 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001777#ifdef CONFIG_HIGH_RES_TIMERS
1778 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1779#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001780}
1781
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001782/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001783 * schedule_hrtimeout_range_clock - sleep until timeout
1784 * @expires: timeout value (ktime_t)
1785 * @delta: slack in expires timeout (ktime_t)
1786 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1787 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1788 */
1789int __sched
1790schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1791 const enum hrtimer_mode mode, int clock)
1792{
1793 struct hrtimer_sleeper t;
1794
1795 /*
1796 * Optimize when a zero timeout value is given. It does not
1797 * matter whether this is an absolute or a relative time.
1798 */
1799 if (expires && !expires->tv64) {
1800 __set_current_state(TASK_RUNNING);
1801 return 0;
1802 }
1803
1804 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001805 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001806 */
1807 if (!expires) {
1808 schedule();
1809 __set_current_state(TASK_RUNNING);
1810 return -EINTR;
1811 }
1812
1813 hrtimer_init_on_stack(&t.timer, clock, mode);
1814 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1815
1816 hrtimer_init_sleeper(&t, current);
1817
1818 hrtimer_start_expires(&t.timer, mode);
1819 if (!hrtimer_active(&t.timer))
1820 t.task = NULL;
1821
1822 if (likely(t.task))
1823 schedule();
1824
1825 hrtimer_cancel(&t.timer);
1826 destroy_hrtimer_on_stack(&t.timer);
1827
1828 __set_current_state(TASK_RUNNING);
1829
1830 return !t.task ? 0 : -EINTR;
1831}
1832
1833/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001834 * schedule_hrtimeout_range - sleep until timeout
1835 * @expires: timeout value (ktime_t)
1836 * @delta: slack in expires timeout (ktime_t)
1837 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1838 *
1839 * Make the current task sleep until the given expiry time has
1840 * elapsed. The routine will return immediately unless
1841 * the current task state has been set (see set_current_state()).
1842 *
1843 * The @delta argument gives the kernel the freedom to schedule the
1844 * actual wakeup to a time that is both power and performance friendly.
1845 * The kernel give the normal best effort behavior for "@expires+@delta",
1846 * but may decide to fire the timer earlier, but no earlier than @expires.
1847 *
1848 * You can set the task state as follows -
1849 *
1850 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1851 * pass before the routine returns.
1852 *
1853 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1854 * delivered to the current task.
1855 *
1856 * The current task state is guaranteed to be TASK_RUNNING when this
1857 * routine returns.
1858 *
1859 * Returns 0 when the timer has expired otherwise -EINTR
1860 */
1861int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001862 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001863{
Carsten Emde351b3f72010-04-02 22:40:19 +02001864 return schedule_hrtimeout_range_clock(expires, delta, mode,
1865 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001866}
1867EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1868
1869/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001870 * schedule_hrtimeout - sleep until timeout
1871 * @expires: timeout value (ktime_t)
1872 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1873 *
1874 * Make the current task sleep until the given expiry time has
1875 * elapsed. The routine will return immediately unless
1876 * the current task state has been set (see set_current_state()).
1877 *
1878 * You can set the task state as follows -
1879 *
1880 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1881 * pass before the routine returns.
1882 *
1883 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1884 * delivered to the current task.
1885 *
1886 * The current task state is guaranteed to be TASK_RUNNING when this
1887 * routine returns.
1888 *
1889 * Returns 0 when the timer has expired otherwise -EINTR
1890 */
1891int __sched schedule_hrtimeout(ktime_t *expires,
1892 const enum hrtimer_mode mode)
1893{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001894 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001895}
1896EXPORT_SYMBOL_GPL(schedule_hrtimeout);