blob: c0875ae0de1791239fdc6fadcc1a7f6c8ab09729 [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 Anzinger7978672c2006-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
David Engraf51fd36f2013-03-19 13:29:55 +0100289 /* Make sure nsec fits into long */
290 if (unlikely(nsec > KTIME_SEC_MAX))
291 return (ktime_t){ .tv64 = KTIME_MAX };
292
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800293 tmp = ktime_set((long)nsec, rem);
294 }
295
296 return ktime_add(kt, tmp);
297}
David Howellsb8b8fd22007-04-27 15:31:24 -0700298
299EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700300
301/**
302 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
303 * @kt: minuend
304 * @nsec: the scalar nsec value to subtract
305 *
306 * Returns the subtraction of @nsec from @kt in ktime_t format
307 */
308ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
309{
310 ktime_t tmp;
311
312 if (likely(nsec < NSEC_PER_SEC)) {
313 tmp.tv64 = nsec;
314 } else {
315 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
316
317 tmp = ktime_set((long)nsec, rem);
318 }
319
320 return ktime_sub(kt, tmp);
321}
322
323EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800324# endif /* !CONFIG_KTIME_SCALAR */
325
326/*
327 * Divide a ktime value by a nanosecond value
328 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800329u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800330{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300331 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800332 int sft = 0;
333
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300334 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800335 /* Make sure the divisor is less than 2^32: */
336 while (div >> 32) {
337 sft++;
338 div >>= 1;
339 }
340 dclc >>= sft;
341 do_div(dclc, (unsigned long) div);
342
Davide Libenzi4d672e72008-02-04 22:27:26 -0800343 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800344}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800345#endif /* BITS_PER_LONG >= 64 */
346
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100347/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100348 * Add two ktime values and do a safety check for overflow:
349 */
350ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
351{
352 ktime_t res = ktime_add(lhs, rhs);
353
354 /*
355 * We use KTIME_SEC_MAX here, the maximum timeout which we can
356 * return to user space in a timespec:
357 */
358 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
359 res = ktime_set(KTIME_SEC_MAX, 0);
360
361 return res;
362}
363
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300364EXPORT_SYMBOL_GPL(ktime_add_safe);
365
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700366#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
367
368static struct debug_obj_descr hrtimer_debug_descr;
369
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100370static void *hrtimer_debug_hint(void *addr)
371{
372 return ((struct hrtimer *) addr)->function;
373}
374
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700375/*
376 * fixup_init is called when:
377 * - an active object is initialized
378 */
379static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
380{
381 struct hrtimer *timer = addr;
382
383 switch (state) {
384 case ODEBUG_STATE_ACTIVE:
385 hrtimer_cancel(timer);
386 debug_object_init(timer, &hrtimer_debug_descr);
387 return 1;
388 default:
389 return 0;
390 }
391}
392
393/*
394 * fixup_activate is called when:
395 * - an active object is activated
396 * - an unknown object is activated (might be a statically initialized object)
397 */
398static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
399{
400 switch (state) {
401
402 case ODEBUG_STATE_NOTAVAILABLE:
403 WARN_ON_ONCE(1);
404 return 0;
405
406 case ODEBUG_STATE_ACTIVE:
407 WARN_ON(1);
408
409 default:
410 return 0;
411 }
412}
413
414/*
415 * fixup_free is called when:
416 * - an active object is freed
417 */
418static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
419{
420 struct hrtimer *timer = addr;
421
422 switch (state) {
423 case ODEBUG_STATE_ACTIVE:
424 hrtimer_cancel(timer);
425 debug_object_free(timer, &hrtimer_debug_descr);
426 return 1;
427 default:
428 return 0;
429 }
430}
431
432static struct debug_obj_descr hrtimer_debug_descr = {
433 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100434 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700435 .fixup_init = hrtimer_fixup_init,
436 .fixup_activate = hrtimer_fixup_activate,
437 .fixup_free = hrtimer_fixup_free,
438};
439
440static inline void debug_hrtimer_init(struct hrtimer *timer)
441{
442 debug_object_init(timer, &hrtimer_debug_descr);
443}
444
445static inline void debug_hrtimer_activate(struct hrtimer *timer)
446{
447 debug_object_activate(timer, &hrtimer_debug_descr);
448}
449
450static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
451{
452 debug_object_deactivate(timer, &hrtimer_debug_descr);
453}
454
455static inline void debug_hrtimer_free(struct hrtimer *timer)
456{
457 debug_object_free(timer, &hrtimer_debug_descr);
458}
459
460static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
461 enum hrtimer_mode mode);
462
463void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
464 enum hrtimer_mode mode)
465{
466 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
467 __hrtimer_init(timer, clock_id, mode);
468}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700469EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700470
471void destroy_hrtimer_on_stack(struct hrtimer *timer)
472{
473 debug_object_free(timer, &hrtimer_debug_descr);
474}
475
476#else
477static inline void debug_hrtimer_init(struct hrtimer *timer) { }
478static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
479static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
480#endif
481
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800482static inline void
483debug_init(struct hrtimer *timer, clockid_t clockid,
484 enum hrtimer_mode mode)
485{
486 debug_hrtimer_init(timer);
487 trace_hrtimer_init(timer, clockid, mode);
488}
489
490static inline void debug_activate(struct hrtimer *timer)
491{
492 debug_hrtimer_activate(timer);
493 trace_hrtimer_start(timer);
494}
495
496static inline void debug_deactivate(struct hrtimer *timer)
497{
498 debug_hrtimer_deactivate(timer);
499 trace_hrtimer_cancel(timer);
500}
501
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800502/* High resolution timer related functions */
503#ifdef CONFIG_HIGH_RES_TIMERS
504
505/*
506 * High resolution timer enabled ?
507 */
508static int hrtimer_hres_enabled __read_mostly = 1;
509
510/*
511 * Enable / Disable high resolution mode
512 */
513static int __init setup_hrtimer_hres(char *str)
514{
515 if (!strcmp(str, "off"))
516 hrtimer_hres_enabled = 0;
517 else if (!strcmp(str, "on"))
518 hrtimer_hres_enabled = 1;
519 else
520 return 0;
521 return 1;
522}
523
524__setup("highres=", setup_hrtimer_hres);
525
526/*
527 * hrtimer_high_res_enabled - query, if the highres mode is enabled
528 */
529static inline int hrtimer_is_hres_enabled(void)
530{
531 return hrtimer_hres_enabled;
532}
533
534/*
535 * Is the high resolution mode active ?
536 */
537static inline int hrtimer_hres_active(void)
538{
Christoph Lameter909ea962010-12-08 16:22:55 +0100539 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800540}
541
542/*
543 * Reprogram the event source with checking both queues for the
544 * next event
545 * Called with interrupts disabled and base->lock held
546 */
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400547static void
548hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800549{
550 int i;
551 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400552 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800553
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400554 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800555
556 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
557 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700558 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800559
John Stultz998adc32010-09-20 19:19:17 -0700560 next = timerqueue_getnext(&base->active);
561 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800562 continue;
John Stultz998adc32010-09-20 19:19:17 -0700563 timer = container_of(next, struct hrtimer, node);
564
Arjan van de Vencc584b22008-09-01 15:02:30 -0700565 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100566 /*
567 * clock_was_set() has changed base->offset so the
568 * result might be negative. Fix it up to prevent a
569 * false positive in clockevents_program_event()
570 */
571 if (expires.tv64 < 0)
572 expires.tv64 = 0;
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400573 if (expires.tv64 < expires_next.tv64)
574 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800575 }
576
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400577 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
578 return;
579
580 cpu_base->expires_next.tv64 = expires_next.tv64;
581
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800582 if (cpu_base->expires_next.tv64 != KTIME_MAX)
583 tick_program_event(cpu_base->expires_next, 1);
584}
585
586/*
587 * Shared reprogramming for clock_realtime and clock_monotonic
588 *
589 * When a timer is enqueued and expires earlier than the already enqueued
590 * timers, we have to check, whether it expires earlier than the timer for
591 * which the clock event device was armed.
592 *
593 * Called with interrupts disabled and base->cpu_base.lock held
594 */
595static int hrtimer_reprogram(struct hrtimer *timer,
596 struct hrtimer_clock_base *base)
597{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100598 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700599 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800600 int res;
601
Arjan van de Vencc584b22008-09-01 15:02:30 -0700602 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100603
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800604 /*
605 * When the callback is running, we do not reprogram the clock event
606 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200607 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800608 * reprogramming is handled either by the softirq, which called the
609 * callback or at the end of the hrtimer_interrupt.
610 */
611 if (hrtimer_callback_running(timer))
612 return 0;
613
Thomas Gleixner63070a72008-02-14 00:58:36 +0100614 /*
615 * CLOCK_REALTIME timer might be requested with an absolute
616 * expiry time which is less than base->offset. Nothing wrong
617 * about that, just avoid to call into the tick code, which
618 * has now objections against negative expiry values.
619 */
620 if (expires.tv64 < 0)
621 return -ETIME;
622
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100623 if (expires.tv64 >= cpu_base->expires_next.tv64)
624 return 0;
625
626 /*
627 * If a hang was detected in the last timer interrupt then we
628 * do not schedule a timer which is earlier than the expiry
629 * which we enforced in the hang detection. We want the system
630 * to make progress.
631 */
632 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800633 return 0;
634
635 /*
636 * Clockevents returns -ETIME, when the event was in the past.
637 */
638 res = tick_program_event(expires, 0);
639 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100640 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800641 return res;
642}
643
Ingo Molnar995f0542007-04-07 12:05:00 +0200644/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800645 * Initialize the high resolution related parts of cpu_base
646 */
647static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
648{
649 base->expires_next.tv64 = KTIME_MAX;
650 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800651}
652
653/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800654 * When High resolution timers are active, try to reprogram. Note, that in case
655 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
656 * check happens. The timer gets enqueued into the rbtree. The reprogramming
657 * and expiry check is done in the hrtimer_interrupt or in the softirq.
658 */
659static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzb22affe2013-02-04 14:33:37 +0200660 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800661{
Leonid Shatzb22affe2013-02-04 14:33:37 +0200662 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800663}
664
John Stultz5baefd62012-07-10 18:43:25 -0400665static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
666{
667 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
668 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
John Stultz90adda92013-01-21 17:00:11 -0800669 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
John Stultz5baefd62012-07-10 18:43:25 -0400670
John Stultz90adda92013-01-21 17:00:11 -0800671 return ktime_get_update_offsets(offs_real, offs_boot, offs_tai);
John Stultz5baefd62012-07-10 18:43:25 -0400672}
673
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200674/*
675 * Retrigger next event is called after clock was set
676 *
677 * Called with interrupts disabled via on_each_cpu()
678 */
679static void retrigger_next_event(void *arg)
680{
681 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200682
683 if (!hrtimer_hres_active())
684 return;
685
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200686 raw_spin_lock(&base->lock);
John Stultz5baefd62012-07-10 18:43:25 -0400687 hrtimer_update_base(base);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200688 hrtimer_force_reprogram(base, 0);
689 raw_spin_unlock(&base->lock);
690}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200691
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800692/*
693 * Switch to high resolution mode
694 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800695static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800696{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200697 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700698 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800699 unsigned long flags;
700
701 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800702 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800703
704 local_irq_save(flags);
705
706 if (tick_init_highres()) {
707 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700708 printk(KERN_WARNING "Could not switch to high resolution "
709 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800710 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800711 }
712 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200713 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
714 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800715
716 tick_setup_sched_timer();
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800717 /* "Retrigger" the interrupt to get things going */
718 retrigger_next_event(NULL);
719 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800720 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800721}
722
John Stultzf55a6fa2012-07-10 18:43:19 -0400723/*
724 * Called from timekeeping code to reprogramm the hrtimer interrupt
725 * device. If called from the timer interrupt context we defer it to
726 * softirq context.
727 */
728void clock_was_set_delayed(void)
729{
730 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
731
732 cpu_base->clock_was_set = 1;
733 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
734}
735
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800736#else
737
738static inline int hrtimer_hres_active(void) { return 0; }
739static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800740static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400741static inline void
742hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800743static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzb22affe2013-02-04 14:33:37 +0200744 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800745{
746 return 0;
747}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800748static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200749static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800750
751#endif /* CONFIG_HIGH_RES_TIMERS */
752
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200753/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200754 * Clock realtime was set
755 *
756 * Change the offset of the realtime clock vs. the monotonic
757 * clock.
758 *
759 * We might have to reprogram the high resolution timer interrupt. On
760 * SMP we call the architecture specific code to retrigger _all_ high
761 * resolution timer interrupts. On UP we just disable interrupts and
762 * call the high resolution interrupt code.
763 */
764void clock_was_set(void)
765{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200766#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200767 /* Retrigger the CPU local events everywhere */
768 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200769#endif
770 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200771}
772
773/*
774 * During resume we might have to reprogram the high resolution timer
775 * interrupt (on the local CPU):
776 */
777void hrtimers_resume(void)
778{
779 WARN_ONCE(!irqs_disabled(),
780 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
781
782 retrigger_next_event(NULL);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200783 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200784}
785
Heiko Carstens5f201902009-12-10 10:56:29 +0100786static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800787{
Heiko Carstens5f201902009-12-10 10:56:29 +0100788#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800789 if (timer->start_site)
790 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100791 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800792 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
793 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800794#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100795}
796
797static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
798{
799#ifdef CONFIG_TIMER_STATS
800 timer->start_site = NULL;
801#endif
802}
803
804static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
805{
806#ifdef CONFIG_TIMER_STATS
807 if (likely(!timer_stats_active))
808 return;
809 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
810 timer->function, timer->start_comm, 0);
811#endif
812}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800813
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800814/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200815 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800816 */
817static inline
818void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
819{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100820 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821}
822
823/**
824 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800825 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800826 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800827 * @interval: the interval to forward
828 *
829 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700830 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800831 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800832u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800833{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800834 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800835 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800836
Arjan van de Vencc584b22008-09-01 15:02:30 -0700837 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800838
839 if (delta.tv64 < 0)
840 return 0;
841
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100842 if (interval.tv64 < timer->base->resolution.tv64)
843 interval.tv64 = timer->base->resolution.tv64;
844
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800845 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800846 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800847
848 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700849 hrtimer_add_expires_ns(timer, incr * orun);
850 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800851 return orun;
852 /*
853 * This (and the ktime_add() below) is the
854 * correction for exact:
855 */
856 orun++;
857 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700858 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800859
860 return orun;
861}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700862EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800863
864/*
865 * enqueue_hrtimer - internal function to (re)start a timer
866 *
867 * The timer is inserted in expiry order. Insertion into the
868 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100869 *
870 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800871 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100872static int enqueue_hrtimer(struct hrtimer *timer,
873 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800874{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800875 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700876
John Stultz998adc32010-09-20 19:19:17 -0700877 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200878 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800879
880 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800881 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
882 * state of a possibly running callback.
883 */
884 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100885
John Stultz998adc32010-09-20 19:19:17 -0700886 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100887}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800888
889/*
890 * __remove_hrtimer - internal function to remove a timer
891 *
892 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800893 *
894 * High resolution timer mode reprograms the clock event device when the
895 * timer is the one which expires next. The caller can disable this by setting
896 * reprogram to zero. This is useful, when the context does a reprogramming
897 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800898 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800899static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800900 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800901 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800902{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800903 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400904 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
905 goto out;
906
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800907 next_timer = timerqueue_getnext(&base->active);
908 timerqueue_del(&base->active, &timer->node);
909 if (&timer->node == next_timer) {
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400910#ifdef CONFIG_HIGH_RES_TIMERS
911 /* Reprogram the clock event device. if enabled */
912 if (reprogram && hrtimer_hres_active()) {
913 ktime_t expires;
914
915 expires = ktime_sub(hrtimer_get_expires(timer),
916 base->offset);
917 if (base->cpu_base->expires_next.tv64 == expires.tv64)
918 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800919 }
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400920#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800921 }
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200922 if (!timerqueue_getnext(&base->active))
923 base->cpu_base->active_bases &= ~(1 << base->index);
Ashwin Chaugule7403f41f2009-09-01 23:03:33 -0400924out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800925 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800926}
927
928/*
929 * remove hrtimer, called with base lock held
930 */
931static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800932remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800933{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800934 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700935 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800936 int reprogram;
937
938 /*
939 * Remove the timer and force reprogramming when high
940 * resolution mode is active and the timer is on the current
941 * CPU. If we remove a timer on another CPU, reprogramming is
942 * skipped. The interrupt event on this CPU is fired and
943 * reprogramming happens in the interrupt handler. This is a
944 * rare case and less expensive than a smp call.
945 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800946 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800947 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800948 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700949 /*
950 * We must preserve the CALLBACK state flag here,
951 * otherwise we could move the timer base in
952 * switch_hrtimer_base.
953 */
954 state = timer->state & HRTIMER_STATE_CALLBACK;
955 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800956 return 1;
957 }
958 return 0;
959}
960
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100961int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
962 unsigned long delta_ns, const enum hrtimer_mode mode,
963 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800964{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800965 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800966 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100967 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800968
969 base = lock_hrtimer_base(timer, &flags);
970
971 /* Remove an active timer from the queue: */
972 ret = remove_hrtimer(timer, base);
973
974 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530975 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800976
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530977 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100978 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800979 /*
980 * CONFIG_TIME_LOW_RES is a temporary way for architectures
981 * to signal that they simply return xtime in
982 * do_gettimeoffset(). In this case we want to round up by
983 * resolution when starting a relative timer, to avoid short
984 * timeouts. This will go away with the GTOD framework.
985 */
986#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100987 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800988#endif
989 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700990
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700991 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800992
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800993 timer_stats_hrtimer_set_start_info(timer);
994
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100995 leftmost = enqueue_hrtimer(timer, new_base);
996
Ingo Molnar935c6312007-03-28 13:17:18 +0200997 /*
998 * Only allow reprogramming if the new base is on this CPU.
999 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001000 *
1001 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +02001002 */
Leonid Shatzb22affe2013-02-04 14:33:37 +02001003 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
1004 && hrtimer_enqueue_reprogram(timer, new_base)) {
1005 if (wakeup) {
1006 /*
1007 * We need to drop cpu_base->lock to avoid a
1008 * lock ordering issue vs. rq->lock.
1009 */
1010 raw_spin_unlock(&new_base->cpu_base->lock);
1011 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1012 local_irq_restore(flags);
1013 return ret;
1014 } else {
1015 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1016 }
1017 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001018
1019 unlock_hrtimer_base(timer, &flags);
1020
1021 return ret;
1022}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001023
1024/**
1025 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1026 * @timer: the timer to be added
1027 * @tim: expiry time
1028 * @delta_ns: "slack" range for the timer
David Daney8ffbc7d2013-03-13 12:20:38 -07001029 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1030 * relative (HRTIMER_MODE_REL)
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001031 *
1032 * Returns:
1033 * 0 on success
1034 * 1 when the timer was active
1035 */
1036int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1037 unsigned long delta_ns, const enum hrtimer_mode mode)
1038{
1039 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1040}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001041EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1042
1043/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001044 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001045 * @timer: the timer to be added
1046 * @tim: expiry time
David Daney8ffbc7d2013-03-13 12:20:38 -07001047 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1048 * relative (HRTIMER_MODE_REL)
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001049 *
1050 * Returns:
1051 * 0 on success
1052 * 1 when the timer was active
1053 */
1054int
1055hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1056{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001057 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001058}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001059EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001060
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001061
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001062/**
1063 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001064 * @timer: hrtimer to stop
1065 *
1066 * Returns:
1067 * 0 when the timer was not active
1068 * 1 when the timer was active
1069 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001070 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001071 */
1072int hrtimer_try_to_cancel(struct hrtimer *timer)
1073{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001074 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001075 unsigned long flags;
1076 int ret = -1;
1077
1078 base = lock_hrtimer_base(timer, &flags);
1079
Thomas Gleixner303e9672007-02-16 01:27:51 -08001080 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001081 ret = remove_hrtimer(timer, base);
1082
1083 unlock_hrtimer_base(timer, &flags);
1084
1085 return ret;
1086
1087}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001088EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001089
1090/**
1091 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001092 * @timer: the timer to be cancelled
1093 *
1094 * Returns:
1095 * 0 when the timer was not active
1096 * 1 when the timer was active
1097 */
1098int hrtimer_cancel(struct hrtimer *timer)
1099{
1100 for (;;) {
1101 int ret = hrtimer_try_to_cancel(timer);
1102
1103 if (ret >= 0)
1104 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001105 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001106 }
1107}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001108EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001109
1110/**
1111 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001112 * @timer: the timer to read
1113 */
1114ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1115{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001116 unsigned long flags;
1117 ktime_t rem;
1118
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001119 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001120 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001121 unlock_hrtimer_base(timer, &flags);
1122
1123 return rem;
1124}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001125EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001126
Russell Kingee9c5782008-04-20 13:59:33 +01001127#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001128/**
1129 * hrtimer_get_next_event - get the time until next expiry event
1130 *
1131 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1132 * is pending.
1133 */
1134ktime_t hrtimer_get_next_event(void)
1135{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001136 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1137 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001138 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1139 unsigned long flags;
1140 int i;
1141
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001142 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001143
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001144 if (!hrtimer_hres_active()) {
1145 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1146 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001147 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001148
John Stultz998adc32010-09-20 19:19:17 -07001149 next = timerqueue_getnext(&base->active);
1150 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001151 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001152
John Stultz998adc32010-09-20 19:19:17 -07001153 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001154 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001155 delta = ktime_sub(delta, base->get_time());
1156 if (delta.tv64 < mindelta.tv64)
1157 mindelta.tv64 = delta.tv64;
1158 }
Tony Lindgren69239742006-03-06 15:42:45 -08001159 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001160
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001161 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001162
Tony Lindgren69239742006-03-06 15:42:45 -08001163 if (mindelta.tv64 < 0)
1164 mindelta.tv64 = 0;
1165 return mindelta;
1166}
1167#endif
1168
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001169static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1170 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001171{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001172 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001173 int base;
George Anzinger7978672c2006-02-01 03:05:11 -08001174
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001175 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001176
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001177 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001178
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001179 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001180 clock_id = CLOCK_MONOTONIC;
1181
John Stultze06383d2010-12-14 19:37:07 -08001182 base = hrtimer_clockid_to_base(clock_id);
1183 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001184 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001185
1186#ifdef CONFIG_TIMER_STATS
1187 timer->start_site = NULL;
1188 timer->start_pid = -1;
1189 memset(timer->start_comm, 0, TASK_COMM_LEN);
1190#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001191}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001192
1193/**
1194 * hrtimer_init - initialize a timer to the given clock
1195 * @timer: the timer to be initialized
1196 * @clock_id: the clock to be used
1197 * @mode: timer mode abs/rel
1198 */
1199void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1200 enum hrtimer_mode mode)
1201{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001202 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001203 __hrtimer_init(timer, clock_id, mode);
1204}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001205EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001206
1207/**
1208 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001209 * @which_clock: which clock to query
1210 * @tp: pointer to timespec variable to store the resolution
1211 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001212 * Store the resolution of the clock selected by @which_clock in the
1213 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001214 */
1215int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1216{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001217 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001218 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001219
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001220 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001221 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001222
1223 return 0;
1224}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001225EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001226
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001227static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001228{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001229 struct hrtimer_clock_base *base = timer->base;
1230 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1231 enum hrtimer_restart (*fn)(struct hrtimer *);
1232 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001233
Peter Zijlstraca109492008-11-25 12:43:51 +01001234 WARN_ON(!irqs_disabled());
1235
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001236 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001237 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1238 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001239 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001240
1241 /*
1242 * Because we run timers from hardirq context, there is no chance
1243 * they get migrated to another cpu, therefore its safe to unlock
1244 * the timer base.
1245 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001246 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001247 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001248 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001249 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001250 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001251
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001252 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001253 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1254 * we do not reprogramm the event hardware. Happens either in
1255 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001256 */
1257 if (restart != HRTIMER_NORESTART) {
1258 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001259 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001260 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001261
1262 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1263
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001264 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001265}
1266
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001267#ifdef CONFIG_HIGH_RES_TIMERS
1268
1269/*
1270 * High resolution timer interrupt
1271 * Called with interrupts disabled
1272 */
1273void hrtimer_interrupt(struct clock_event_device *dev)
1274{
1275 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001276 ktime_t expires_next, now, entry_time, delta;
1277 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001278
1279 BUG_ON(!cpu_base->hres_active);
1280 cpu_base->nr_events++;
1281 dev->next_event.tv64 = KTIME_MAX;
1282
Thomas Gleixner196951e2012-07-10 18:43:23 -04001283 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001284 entry_time = now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001285retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001286 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001287 /*
1288 * We set expires_next to KTIME_MAX here with cpu_base->lock
1289 * held to prevent that a timer is enqueued in our queue via
1290 * the migration code. This does not affect enqueueing of
1291 * timers which run their callback and need to be requeued on
1292 * this CPU.
1293 */
1294 cpu_base->expires_next.tv64 = KTIME_MAX;
1295
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001296 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001297 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001298 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001299 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001300
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001301 if (!(cpu_base->active_bases & (1 << i)))
1302 continue;
1303
1304 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001305 basenow = ktime_add(now, base->offset);
1306
John Stultz998adc32010-09-20 19:19:17 -07001307 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001308 struct hrtimer *timer;
1309
John Stultz998adc32010-09-20 19:19:17 -07001310 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001311
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001312 /*
1313 * The immediate goal for using the softexpires is
1314 * minimizing wakeups, not running timers at the
1315 * earliest interrupt after their soft expiration.
1316 * This allows us to avoid using a Priority Search
1317 * Tree, which can answer a stabbing querry for
1318 * overlapping intervals and instead use the simple
1319 * BST we already have.
1320 * We don't add extra wakeups by delaying timers that
1321 * are right-of a not yet expired timer, because that
1322 * timer will have to trigger a wakeup anyway.
1323 */
1324
1325 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001326 ktime_t expires;
1327
Arjan van de Vencc584b22008-09-01 15:02:30 -07001328 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001329 base->offset);
Prarit Bhargava8f294b52013-04-08 08:47:15 -04001330 if (expires.tv64 < 0)
1331 expires.tv64 = KTIME_MAX;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001332 if (expires.tv64 < expires_next.tv64)
1333 expires_next = expires;
1334 break;
1335 }
1336
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001337 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001338 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001339 }
1340
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001341 /*
1342 * Store the new expiry value so the migration code can verify
1343 * against it.
1344 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001345 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001346 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001347
1348 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001349 if (expires_next.tv64 == KTIME_MAX ||
1350 !tick_program_event(expires_next, 0)) {
1351 cpu_base->hang_detected = 0;
1352 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001353 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001354
1355 /*
1356 * The next timer was already expired due to:
1357 * - tracing
1358 * - long lasting callbacks
1359 * - being scheduled away when running in a VM
1360 *
1361 * We need to prevent that we loop forever in the hrtimer
1362 * interrupt routine. We give it 3 attempts to avoid
1363 * overreacting on some spurious event.
John Stultz5baefd62012-07-10 18:43:25 -04001364 *
1365 * Acquire base lock for updating the offsets and retrieving
1366 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001367 */
Thomas Gleixner196951e2012-07-10 18:43:23 -04001368 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001369 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001370 cpu_base->nr_retries++;
1371 if (++retries < 3)
1372 goto retry;
1373 /*
1374 * Give the system a chance to do something else than looping
1375 * here. We stored the entry time, so we know exactly how long
1376 * we spent here. We schedule the next event this amount of
1377 * time away.
1378 */
1379 cpu_base->nr_hangs++;
1380 cpu_base->hang_detected = 1;
Thomas Gleixner196951e2012-07-10 18:43:23 -04001381 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001382 delta = ktime_sub(now, entry_time);
1383 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1384 cpu_base->max_hang_time = delta;
1385 /*
1386 * Limit it to a sensible value as we enforce a longer
1387 * delay. Give the CPU at least 100ms to catch up.
1388 */
1389 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1390 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1391 else
1392 expires_next = ktime_add(now, delta);
1393 tick_program_event(expires_next, 1);
1394 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1395 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001396}
1397
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001398/*
1399 * local version of hrtimer_peek_ahead_timers() called with interrupts
1400 * disabled.
1401 */
1402static void __hrtimer_peek_ahead_timers(void)
1403{
1404 struct tick_device *td;
1405
1406 if (!hrtimer_hres_active())
1407 return;
1408
1409 td = &__get_cpu_var(tick_cpu_device);
1410 if (td && td->evtdev)
1411 hrtimer_interrupt(td->evtdev);
1412}
1413
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001414/**
1415 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1416 *
1417 * hrtimer_peek_ahead_timers will peek at the timer queue of
1418 * the current cpu and check if there are any timers for which
1419 * the soft expires time has passed. If any such timers exist,
1420 * they are run immediately and then removed from the timer queue.
1421 *
1422 */
1423void hrtimer_peek_ahead_timers(void)
1424{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001425 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001426
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001427 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001428 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001429 local_irq_restore(flags);
1430}
1431
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001432static void run_hrtimer_softirq(struct softirq_action *h)
1433{
John Stultzf55a6fa2012-07-10 18:43:19 -04001434 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1435
1436 if (cpu_base->clock_was_set) {
1437 cpu_base->clock_was_set = 0;
1438 clock_was_set();
1439 }
1440
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001441 hrtimer_peek_ahead_timers();
1442}
1443
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001444#else /* CONFIG_HIGH_RES_TIMERS */
1445
1446static inline void __hrtimer_peek_ahead_timers(void) { }
1447
1448#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001449
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001450/*
1451 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001452 *
1453 * For HRT its the fall back code to run the softirq in the timer
1454 * softirq context in case the hrtimer initialization failed or has
1455 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001456 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001457void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001458{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001459 if (hrtimer_hres_active())
1460 return;
1461
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001462 /*
1463 * This _is_ ugly: We have to check in the softirq context,
1464 * whether we can switch to highres and / or nohz mode. The
1465 * clocksource switch happens in the timer interrupt with
1466 * xtime_lock held. Notification from there only sets the
1467 * check bit in the tick_oneshot code, otherwise we might
1468 * deadlock vs. xtime_lock.
1469 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001470 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001471 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001472}
1473
1474/*
1475 * Called from hardirq context every jiffy
1476 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001477void hrtimer_run_queues(void)
1478{
John Stultz998adc32010-09-20 19:19:17 -07001479 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001480 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001481 struct hrtimer_clock_base *base;
1482 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001483
1484 if (hrtimer_hres_active())
1485 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001486
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001487 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1488 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001489 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001490 continue;
1491
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001492 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001493 hrtimer_get_softirq_time(cpu_base);
1494 gettime = 0;
1495 }
1496
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001497 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001498
John Stultzb007c382010-12-10 22:19:53 -08001499 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001500 struct hrtimer *timer;
1501
John Stultz998adc32010-09-20 19:19:17 -07001502 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001503 if (base->softirq_time.tv64 <=
1504 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001505 break;
1506
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001507 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001508 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001509 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001510 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001511}
1512
1513/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001514 * Sleep related functions:
1515 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001516static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001517{
1518 struct hrtimer_sleeper *t =
1519 container_of(timer, struct hrtimer_sleeper, timer);
1520 struct task_struct *task = t->task;
1521
1522 t->task = NULL;
1523 if (task)
1524 wake_up_process(task);
1525
1526 return HRTIMER_NORESTART;
1527}
1528
Ingo Molnar36c8b582006-07-03 00:25:41 -07001529void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001530{
1531 sl->timer.function = hrtimer_wakeup;
1532 sl->task = task;
1533}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001534EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001535
Thomas Gleixner669d7862006-03-31 02:31:19 -08001536static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001537{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001538 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001539
Roman Zippel432569b2006-03-26 01:38:08 -08001540 do {
1541 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001542 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001543 if (!hrtimer_active(&t->timer))
1544 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001545
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001546 if (likely(t->task))
1547 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001548
Thomas Gleixner669d7862006-03-31 02:31:19 -08001549 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001550 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001551
Thomas Gleixner669d7862006-03-31 02:31:19 -08001552 } while (t->task && !signal_pending(current));
1553
Peter Zijlstra3588a082008-02-01 17:45:13 +01001554 __set_current_state(TASK_RUNNING);
1555
Thomas Gleixner669d7862006-03-31 02:31:19 -08001556 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001557}
1558
Oleg Nesterov080344b2008-02-01 17:29:05 +03001559static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1560{
1561 struct timespec rmt;
1562 ktime_t rem;
1563
Arjan van de Vencc584b22008-09-01 15:02:30 -07001564 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001565 if (rem.tv64 <= 0)
1566 return 0;
1567 rmt = ktime_to_timespec(rem);
1568
1569 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1570 return -EFAULT;
1571
1572 return 1;
1573}
1574
Toyo Abe1711ef32006-09-29 02:00:28 -07001575long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001576{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001577 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001578 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001579 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001580
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001581 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001582 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001583 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001584
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001585 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001586 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001587
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001588 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001589 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001590 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001591 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001592 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001593 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001594
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001595 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001596 ret = -ERESTART_RESTARTBLOCK;
1597out:
1598 destroy_hrtimer_on_stack(&t.timer);
1599 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001600}
1601
Oleg Nesterov080344b2008-02-01 17:29:05 +03001602long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001603 const enum hrtimer_mode mode, const clockid_t clockid)
1604{
1605 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001606 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001607 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001608 unsigned long slack;
1609
1610 slack = current->timer_slack_ns;
1611 if (rt_task(current))
1612 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001613
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001614 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001615 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001616 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001617 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001618
George Anzinger7978672c2006-02-01 03:05:11 -08001619 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001620 if (mode == HRTIMER_MODE_ABS) {
1621 ret = -ERESTARTNOHAND;
1622 goto out;
1623 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001624
Roman Zippel432569b2006-03-26 01:38:08 -08001625 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001626 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001627 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001628 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001629 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001630
1631 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001632 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001633 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001634 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001635 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001636
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001637 ret = -ERESTART_RESTARTBLOCK;
1638out:
1639 destroy_hrtimer_on_stack(&t.timer);
1640 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001641}
1642
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001643SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1644 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001645{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001646 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001647
1648 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1649 return -EFAULT;
1650
1651 if (!timespec_valid(&tu))
1652 return -EINVAL;
1653
Oleg Nesterov080344b2008-02-01 17:29:05 +03001654 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001655}
1656
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001657/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001658 * Functions related to boot-time initialization:
1659 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001660static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001661{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001662 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001663 int i;
1664
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001665 raw_spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001666
John Stultz998adc32010-09-20 19:19:17 -07001667 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001668 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001669 timerqueue_init_head(&cpu_base->clock_base[i].active);
1670 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001671
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001672 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001673}
1674
1675#ifdef CONFIG_HOTPLUG_CPU
1676
Peter Zijlstraca109492008-11-25 12:43:51 +01001677static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001678 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001679{
1680 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001681 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001682
John Stultz998adc32010-09-20 19:19:17 -07001683 while ((node = timerqueue_getnext(&old_base->active))) {
1684 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001685 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001686 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001687
1688 /*
1689 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1690 * timer could be seen as !active and just vanish away
1691 * under us on another CPU
1692 */
1693 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001694 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001695 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001696 * Enqueue the timers on the new cpu. This does not
1697 * reprogram the event device in case the timer
1698 * expires before the earliest on this CPU, but we run
1699 * hrtimer_interrupt after we migrated everything to
1700 * sort out already expired timers and reprogram the
1701 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001702 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001703 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001704
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001705 /* Clear the migration state bit */
1706 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001707 }
1708}
1709
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001710static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001711{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001712 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001713 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001714
Peter Zijlstra37810652008-12-04 11:17:10 +01001715 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001716 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001717
1718 local_irq_disable();
1719 old_base = &per_cpu(hrtimer_bases, scpu);
1720 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001721 /*
1722 * The caller is globally serialized and nobody else
1723 * takes two locks at once, deadlock is not possible.
1724 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001725 raw_spin_lock(&new_base->lock);
1726 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001727
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001728 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001729 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001730 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001731 }
1732
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001733 raw_spin_unlock(&old_base->lock);
1734 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001735
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001736 /* Check, if we got expired work to do */
1737 __hrtimer_peek_ahead_timers();
1738 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001739}
1740
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001741#endif /* CONFIG_HOTPLUG_CPU */
1742
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001743static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001744 unsigned long action, void *hcpu)
1745{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001746 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001747
1748 switch (action) {
1749
1750 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001751 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001752 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001753 break;
1754
1755#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001756 case CPU_DYING:
1757 case CPU_DYING_FROZEN:
1758 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1759 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001760 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001761 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001762 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001763 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001764 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001765 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001766 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001767#endif
1768
1769 default:
1770 break;
1771 }
1772
1773 return NOTIFY_OK;
1774}
1775
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001776static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001777 .notifier_call = hrtimer_cpu_notify,
1778};
1779
1780void __init hrtimers_init(void)
1781{
1782 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1783 (void *)(long)smp_processor_id());
1784 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001785#ifdef CONFIG_HIGH_RES_TIMERS
1786 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1787#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001788}
1789
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001790/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001791 * schedule_hrtimeout_range_clock - sleep until timeout
1792 * @expires: timeout value (ktime_t)
1793 * @delta: slack in expires timeout (ktime_t)
1794 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1795 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1796 */
1797int __sched
1798schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1799 const enum hrtimer_mode mode, int clock)
1800{
1801 struct hrtimer_sleeper t;
1802
1803 /*
1804 * Optimize when a zero timeout value is given. It does not
1805 * matter whether this is an absolute or a relative time.
1806 */
1807 if (expires && !expires->tv64) {
1808 __set_current_state(TASK_RUNNING);
1809 return 0;
1810 }
1811
1812 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001813 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001814 */
1815 if (!expires) {
1816 schedule();
1817 __set_current_state(TASK_RUNNING);
1818 return -EINTR;
1819 }
1820
1821 hrtimer_init_on_stack(&t.timer, clock, mode);
1822 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1823
1824 hrtimer_init_sleeper(&t, current);
1825
1826 hrtimer_start_expires(&t.timer, mode);
1827 if (!hrtimer_active(&t.timer))
1828 t.task = NULL;
1829
1830 if (likely(t.task))
1831 schedule();
1832
1833 hrtimer_cancel(&t.timer);
1834 destroy_hrtimer_on_stack(&t.timer);
1835
1836 __set_current_state(TASK_RUNNING);
1837
1838 return !t.task ? 0 : -EINTR;
1839}
1840
1841/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001842 * schedule_hrtimeout_range - sleep until timeout
1843 * @expires: timeout value (ktime_t)
1844 * @delta: slack in expires timeout (ktime_t)
1845 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1846 *
1847 * Make the current task sleep until the given expiry time has
1848 * elapsed. The routine will return immediately unless
1849 * the current task state has been set (see set_current_state()).
1850 *
1851 * The @delta argument gives the kernel the freedom to schedule the
1852 * actual wakeup to a time that is both power and performance friendly.
1853 * The kernel give the normal best effort behavior for "@expires+@delta",
1854 * but may decide to fire the timer earlier, but no earlier than @expires.
1855 *
1856 * You can set the task state as follows -
1857 *
1858 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1859 * pass before the routine returns.
1860 *
1861 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1862 * delivered to the current task.
1863 *
1864 * The current task state is guaranteed to be TASK_RUNNING when this
1865 * routine returns.
1866 *
1867 * Returns 0 when the timer has expired otherwise -EINTR
1868 */
1869int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001870 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001871{
Carsten Emde351b3f72010-04-02 22:40:19 +02001872 return schedule_hrtimeout_range_clock(expires, delta, mode,
1873 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001874}
1875EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1876
1877/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001878 * schedule_hrtimeout - sleep until timeout
1879 * @expires: timeout value (ktime_t)
1880 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1881 *
1882 * Make the current task sleep until the given expiry time has
1883 * elapsed. The routine will return immediately unless
1884 * the current task state has been set (see set_current_state()).
1885 *
1886 * You can set the task state as follows -
1887 *
1888 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1889 * pass before the routine returns.
1890 *
1891 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1892 * delivered to the current task.
1893 *
1894 * The current task state is guaranteed to be TASK_RUNNING when this
1895 * routine returns.
1896 *
1897 * Returns 0 when the timer has expired otherwise -EINTR
1898 */
1899int __sched schedule_hrtimeout(ktime_t *expires,
1900 const enum hrtimer_mode mode)
1901{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001902 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001903}
1904EXPORT_SYMBOL_GPL(schedule_hrtimeout);