blob: 071e093c448670024d3804a7b8e03c4eff2eb68f [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
David Daney8ffbc7d2013-03-13 12:20:38 -07001025 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1026 * relative (HRTIMER_MODE_REL)
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001027 *
1028 * Returns:
1029 * 0 on success
1030 * 1 when the timer was active
1031 */
1032int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1033 unsigned long delta_ns, const enum hrtimer_mode mode)
1034{
1035 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1036}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001037EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1038
1039/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001040 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001041 * @timer: the timer to be added
1042 * @tim: expiry time
David Daney8ffbc7d2013-03-13 12:20:38 -07001043 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1044 * relative (HRTIMER_MODE_REL)
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001045 *
1046 * Returns:
1047 * 0 on success
1048 * 1 when the timer was active
1049 */
1050int
1051hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1052{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001053 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001054}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001055EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001056
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001057
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001058/**
1059 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001060 * @timer: hrtimer to stop
1061 *
1062 * Returns:
1063 * 0 when the timer was not active
1064 * 1 when the timer was active
1065 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001066 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001067 */
1068int hrtimer_try_to_cancel(struct hrtimer *timer)
1069{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001070 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001071 unsigned long flags;
1072 int ret = -1;
1073
1074 base = lock_hrtimer_base(timer, &flags);
1075
Thomas Gleixner303e9672007-02-16 01:27:51 -08001076 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001077 ret = remove_hrtimer(timer, base);
1078
1079 unlock_hrtimer_base(timer, &flags);
1080
1081 return ret;
1082
1083}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001084EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001085
1086/**
1087 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001088 * @timer: the timer to be cancelled
1089 *
1090 * Returns:
1091 * 0 when the timer was not active
1092 * 1 when the timer was active
1093 */
1094int hrtimer_cancel(struct hrtimer *timer)
1095{
1096 for (;;) {
1097 int ret = hrtimer_try_to_cancel(timer);
1098
1099 if (ret >= 0)
1100 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001101 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001102 }
1103}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001104EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001105
1106/**
1107 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001108 * @timer: the timer to read
1109 */
1110ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1111{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001112 unsigned long flags;
1113 ktime_t rem;
1114
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001115 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001116 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001117 unlock_hrtimer_base(timer, &flags);
1118
1119 return rem;
1120}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001121EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001122
Russell Kingee9c5782008-04-20 13:59:33 +01001123#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001124/**
1125 * hrtimer_get_next_event - get the time until next expiry event
1126 *
1127 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1128 * is pending.
1129 */
1130ktime_t hrtimer_get_next_event(void)
1131{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001132 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1133 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001134 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1135 unsigned long flags;
1136 int i;
1137
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001138 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001139
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001140 if (!hrtimer_hres_active()) {
1141 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1142 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001143 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001144
John Stultz998adc32010-09-20 19:19:17 -07001145 next = timerqueue_getnext(&base->active);
1146 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001147 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001148
John Stultz998adc32010-09-20 19:19:17 -07001149 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001150 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001151 delta = ktime_sub(delta, base->get_time());
1152 if (delta.tv64 < mindelta.tv64)
1153 mindelta.tv64 = delta.tv64;
1154 }
Tony Lindgren69239742006-03-06 15:42:45 -08001155 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001156
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001157 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001158
Tony Lindgren69239742006-03-06 15:42:45 -08001159 if (mindelta.tv64 < 0)
1160 mindelta.tv64 = 0;
1161 return mindelta;
1162}
1163#endif
1164
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001165static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1166 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001167{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001168 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001169 int base;
George Anzinger79786722006-02-01 03:05:11 -08001170
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001171 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001172
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001173 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001174
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001175 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001176 clock_id = CLOCK_MONOTONIC;
1177
John Stultze06383d2010-12-14 19:37:07 -08001178 base = hrtimer_clockid_to_base(clock_id);
1179 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001180 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001181
1182#ifdef CONFIG_TIMER_STATS
1183 timer->start_site = NULL;
1184 timer->start_pid = -1;
1185 memset(timer->start_comm, 0, TASK_COMM_LEN);
1186#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001187}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001188
1189/**
1190 * hrtimer_init - initialize a timer to the given clock
1191 * @timer: the timer to be initialized
1192 * @clock_id: the clock to be used
1193 * @mode: timer mode abs/rel
1194 */
1195void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1196 enum hrtimer_mode mode)
1197{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001198 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001199 __hrtimer_init(timer, clock_id, mode);
1200}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001201EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001202
1203/**
1204 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001205 * @which_clock: which clock to query
1206 * @tp: pointer to timespec variable to store the resolution
1207 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001208 * Store the resolution of the clock selected by @which_clock in the
1209 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001210 */
1211int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1212{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001213 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001214 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001215
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001216 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001217 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001218
1219 return 0;
1220}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001221EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001222
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001223static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001224{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001225 struct hrtimer_clock_base *base = timer->base;
1226 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1227 enum hrtimer_restart (*fn)(struct hrtimer *);
1228 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001229
Peter Zijlstraca109492008-11-25 12:43:51 +01001230 WARN_ON(!irqs_disabled());
1231
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001232 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001233 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1234 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001235 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001236
1237 /*
1238 * Because we run timers from hardirq context, there is no chance
1239 * they get migrated to another cpu, therefore its safe to unlock
1240 * the timer base.
1241 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001242 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001243 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001244 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001245 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001246 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001247
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001248 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001249 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1250 * we do not reprogramm the event hardware. Happens either in
1251 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001252 */
1253 if (restart != HRTIMER_NORESTART) {
1254 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001255 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001256 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001257
1258 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1259
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001260 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001261}
1262
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001263#ifdef CONFIG_HIGH_RES_TIMERS
1264
1265/*
1266 * High resolution timer interrupt
1267 * Called with interrupts disabled
1268 */
1269void hrtimer_interrupt(struct clock_event_device *dev)
1270{
1271 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001272 ktime_t expires_next, now, entry_time, delta;
1273 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001274
1275 BUG_ON(!cpu_base->hres_active);
1276 cpu_base->nr_events++;
1277 dev->next_event.tv64 = KTIME_MAX;
1278
Thomas Gleixner196951e2012-07-10 18:43:23 -04001279 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001280 entry_time = now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001281retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001282 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001283 /*
1284 * We set expires_next to KTIME_MAX here with cpu_base->lock
1285 * held to prevent that a timer is enqueued in our queue via
1286 * the migration code. This does not affect enqueueing of
1287 * timers which run their callback and need to be requeued on
1288 * this CPU.
1289 */
1290 cpu_base->expires_next.tv64 = KTIME_MAX;
1291
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001292 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001293 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001294 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001295 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001296
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001297 if (!(cpu_base->active_bases & (1 << i)))
1298 continue;
1299
1300 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001301 basenow = ktime_add(now, base->offset);
1302
John Stultz998adc32010-09-20 19:19:17 -07001303 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001304 struct hrtimer *timer;
1305
John Stultz998adc32010-09-20 19:19:17 -07001306 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001307
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001308 /*
1309 * The immediate goal for using the softexpires is
1310 * minimizing wakeups, not running timers at the
1311 * earliest interrupt after their soft expiration.
1312 * This allows us to avoid using a Priority Search
1313 * Tree, which can answer a stabbing querry for
1314 * overlapping intervals and instead use the simple
1315 * BST we already have.
1316 * We don't add extra wakeups by delaying timers that
1317 * are right-of a not yet expired timer, because that
1318 * timer will have to trigger a wakeup anyway.
1319 */
1320
1321 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001322 ktime_t expires;
1323
Arjan van de Vencc584b22008-09-01 15:02:30 -07001324 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001325 base->offset);
Prarit Bhargava8f294b52013-04-08 08:47:15 -04001326 if (expires.tv64 < 0)
1327 expires.tv64 = KTIME_MAX;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001328 if (expires.tv64 < expires_next.tv64)
1329 expires_next = expires;
1330 break;
1331 }
1332
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001333 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001334 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001335 }
1336
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001337 /*
1338 * Store the new expiry value so the migration code can verify
1339 * against it.
1340 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001341 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001342 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001343
1344 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001345 if (expires_next.tv64 == KTIME_MAX ||
1346 !tick_program_event(expires_next, 0)) {
1347 cpu_base->hang_detected = 0;
1348 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001349 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001350
1351 /*
1352 * The next timer was already expired due to:
1353 * - tracing
1354 * - long lasting callbacks
1355 * - being scheduled away when running in a VM
1356 *
1357 * We need to prevent that we loop forever in the hrtimer
1358 * interrupt routine. We give it 3 attempts to avoid
1359 * overreacting on some spurious event.
John Stultz5baefd62012-07-10 18:43:25 -04001360 *
1361 * Acquire base lock for updating the offsets and retrieving
1362 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001363 */
Thomas Gleixner196951e2012-07-10 18:43:23 -04001364 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001365 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001366 cpu_base->nr_retries++;
1367 if (++retries < 3)
1368 goto retry;
1369 /*
1370 * Give the system a chance to do something else than looping
1371 * here. We stored the entry time, so we know exactly how long
1372 * we spent here. We schedule the next event this amount of
1373 * time away.
1374 */
1375 cpu_base->nr_hangs++;
1376 cpu_base->hang_detected = 1;
Thomas Gleixner196951e2012-07-10 18:43:23 -04001377 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001378 delta = ktime_sub(now, entry_time);
1379 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1380 cpu_base->max_hang_time = delta;
1381 /*
1382 * Limit it to a sensible value as we enforce a longer
1383 * delay. Give the CPU at least 100ms to catch up.
1384 */
1385 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1386 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1387 else
1388 expires_next = ktime_add(now, delta);
1389 tick_program_event(expires_next, 1);
1390 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1391 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001392}
1393
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001394/*
1395 * local version of hrtimer_peek_ahead_timers() called with interrupts
1396 * disabled.
1397 */
1398static void __hrtimer_peek_ahead_timers(void)
1399{
1400 struct tick_device *td;
1401
1402 if (!hrtimer_hres_active())
1403 return;
1404
1405 td = &__get_cpu_var(tick_cpu_device);
1406 if (td && td->evtdev)
1407 hrtimer_interrupt(td->evtdev);
1408}
1409
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001410/**
1411 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1412 *
1413 * hrtimer_peek_ahead_timers will peek at the timer queue of
1414 * the current cpu and check if there are any timers for which
1415 * the soft expires time has passed. If any such timers exist,
1416 * they are run immediately and then removed from the timer queue.
1417 *
1418 */
1419void hrtimer_peek_ahead_timers(void)
1420{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001421 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001422
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001423 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001424 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001425 local_irq_restore(flags);
1426}
1427
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001428static void run_hrtimer_softirq(struct softirq_action *h)
1429{
John Stultzf55a6fa2012-07-10 18:43:19 -04001430 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1431
1432 if (cpu_base->clock_was_set) {
1433 cpu_base->clock_was_set = 0;
1434 clock_was_set();
1435 }
1436
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001437 hrtimer_peek_ahead_timers();
1438}
1439
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001440#else /* CONFIG_HIGH_RES_TIMERS */
1441
1442static inline void __hrtimer_peek_ahead_timers(void) { }
1443
1444#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001445
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001446/*
1447 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001448 *
1449 * For HRT its the fall back code to run the softirq in the timer
1450 * softirq context in case the hrtimer initialization failed or has
1451 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001452 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001453void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001454{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001455 if (hrtimer_hres_active())
1456 return;
1457
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001458 /*
1459 * This _is_ ugly: We have to check in the softirq context,
1460 * whether we can switch to highres and / or nohz mode. The
1461 * clocksource switch happens in the timer interrupt with
1462 * xtime_lock held. Notification from there only sets the
1463 * check bit in the tick_oneshot code, otherwise we might
1464 * deadlock vs. xtime_lock.
1465 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001466 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001467 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001468}
1469
1470/*
1471 * Called from hardirq context every jiffy
1472 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001473void hrtimer_run_queues(void)
1474{
John Stultz998adc32010-09-20 19:19:17 -07001475 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001476 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001477 struct hrtimer_clock_base *base;
1478 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001479
1480 if (hrtimer_hres_active())
1481 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001482
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001483 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1484 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001485 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001486 continue;
1487
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001488 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001489 hrtimer_get_softirq_time(cpu_base);
1490 gettime = 0;
1491 }
1492
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001493 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001494
John Stultzb007c382010-12-10 22:19:53 -08001495 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001496 struct hrtimer *timer;
1497
John Stultz998adc32010-09-20 19:19:17 -07001498 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001499 if (base->softirq_time.tv64 <=
1500 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001501 break;
1502
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001503 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001504 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001505 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001506 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001507}
1508
1509/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001510 * Sleep related functions:
1511 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001512static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001513{
1514 struct hrtimer_sleeper *t =
1515 container_of(timer, struct hrtimer_sleeper, timer);
1516 struct task_struct *task = t->task;
1517
1518 t->task = NULL;
1519 if (task)
1520 wake_up_process(task);
1521
1522 return HRTIMER_NORESTART;
1523}
1524
Ingo Molnar36c8b582006-07-03 00:25:41 -07001525void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001526{
1527 sl->timer.function = hrtimer_wakeup;
1528 sl->task = task;
1529}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001530EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001531
Thomas Gleixner669d7862006-03-31 02:31:19 -08001532static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001533{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001534 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001535
Roman Zippel432569b2006-03-26 01:38:08 -08001536 do {
1537 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001538 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001539 if (!hrtimer_active(&t->timer))
1540 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001541
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001542 if (likely(t->task))
1543 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001544
Thomas Gleixner669d7862006-03-31 02:31:19 -08001545 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001546 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001547
Thomas Gleixner669d7862006-03-31 02:31:19 -08001548 } while (t->task && !signal_pending(current));
1549
Peter Zijlstra3588a082008-02-01 17:45:13 +01001550 __set_current_state(TASK_RUNNING);
1551
Thomas Gleixner669d7862006-03-31 02:31:19 -08001552 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001553}
1554
Oleg Nesterov080344b2008-02-01 17:29:05 +03001555static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1556{
1557 struct timespec rmt;
1558 ktime_t rem;
1559
Arjan van de Vencc584b22008-09-01 15:02:30 -07001560 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001561 if (rem.tv64 <= 0)
1562 return 0;
1563 rmt = ktime_to_timespec(rem);
1564
1565 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1566 return -EFAULT;
1567
1568 return 1;
1569}
1570
Toyo Abe1711ef32006-09-29 02:00:28 -07001571long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001572{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001573 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001574 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001575 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001576
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001577 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001578 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001579 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001580
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001581 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001582 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001583
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001584 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001585 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001586 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001587 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001588 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001589 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001590
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001591 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001592 ret = -ERESTART_RESTARTBLOCK;
1593out:
1594 destroy_hrtimer_on_stack(&t.timer);
1595 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001596}
1597
Oleg Nesterov080344b2008-02-01 17:29:05 +03001598long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001599 const enum hrtimer_mode mode, const clockid_t clockid)
1600{
1601 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001602 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001603 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001604 unsigned long slack;
1605
1606 slack = current->timer_slack_ns;
1607 if (rt_task(current))
1608 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001609
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001610 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001611 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001612 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001613 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001614
George Anzinger79786722006-02-01 03:05:11 -08001615 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001616 if (mode == HRTIMER_MODE_ABS) {
1617 ret = -ERESTARTNOHAND;
1618 goto out;
1619 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001620
Roman Zippel432569b2006-03-26 01:38:08 -08001621 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001622 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001623 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001624 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001625 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001626
1627 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001628 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001629 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001630 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001631 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001632
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001633 ret = -ERESTART_RESTARTBLOCK;
1634out:
1635 destroy_hrtimer_on_stack(&t.timer);
1636 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001637}
1638
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001639SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1640 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001641{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001642 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001643
1644 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1645 return -EFAULT;
1646
1647 if (!timespec_valid(&tu))
1648 return -EINVAL;
1649
Oleg Nesterov080344b2008-02-01 17:29:05 +03001650 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001651}
1652
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001653/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001654 * Functions related to boot-time initialization:
1655 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001656static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001657{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001658 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001659 int i;
1660
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001661 raw_spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001662
John Stultz998adc32010-09-20 19:19:17 -07001663 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001664 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001665 timerqueue_init_head(&cpu_base->clock_base[i].active);
1666 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001667
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001668 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001669}
1670
1671#ifdef CONFIG_HOTPLUG_CPU
1672
Peter Zijlstraca109492008-11-25 12:43:51 +01001673static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001674 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001675{
1676 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001677 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001678
John Stultz998adc32010-09-20 19:19:17 -07001679 while ((node = timerqueue_getnext(&old_base->active))) {
1680 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001681 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001682 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001683
1684 /*
1685 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1686 * timer could be seen as !active and just vanish away
1687 * under us on another CPU
1688 */
1689 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001690 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001691 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001692 * Enqueue the timers on the new cpu. This does not
1693 * reprogram the event device in case the timer
1694 * expires before the earliest on this CPU, but we run
1695 * hrtimer_interrupt after we migrated everything to
1696 * sort out already expired timers and reprogram the
1697 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001698 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001699 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001700
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001701 /* Clear the migration state bit */
1702 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001703 }
1704}
1705
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001706static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001707{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001708 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001709 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001710
Peter Zijlstra37810652008-12-04 11:17:10 +01001711 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001712 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001713
1714 local_irq_disable();
1715 old_base = &per_cpu(hrtimer_bases, scpu);
1716 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001717 /*
1718 * The caller is globally serialized and nobody else
1719 * takes two locks at once, deadlock is not possible.
1720 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001721 raw_spin_lock(&new_base->lock);
1722 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001723
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001724 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001725 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001726 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001727 }
1728
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001729 raw_spin_unlock(&old_base->lock);
1730 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001731
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001732 /* Check, if we got expired work to do */
1733 __hrtimer_peek_ahead_timers();
1734 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001735}
1736
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001737#endif /* CONFIG_HOTPLUG_CPU */
1738
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001739static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001740 unsigned long action, void *hcpu)
1741{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001742 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001743
1744 switch (action) {
1745
1746 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001747 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001748 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001749 break;
1750
1751#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001752 case CPU_DYING:
1753 case CPU_DYING_FROZEN:
1754 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1755 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001756 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001757 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001758 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001759 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001760 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001761 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001762 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001763#endif
1764
1765 default:
1766 break;
1767 }
1768
1769 return NOTIFY_OK;
1770}
1771
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001772static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001773 .notifier_call = hrtimer_cpu_notify,
1774};
1775
1776void __init hrtimers_init(void)
1777{
1778 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1779 (void *)(long)smp_processor_id());
1780 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001781#ifdef CONFIG_HIGH_RES_TIMERS
1782 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1783#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001784}
1785
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001786/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001787 * schedule_hrtimeout_range_clock - sleep until timeout
1788 * @expires: timeout value (ktime_t)
1789 * @delta: slack in expires timeout (ktime_t)
1790 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1791 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1792 */
1793int __sched
1794schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1795 const enum hrtimer_mode mode, int clock)
1796{
1797 struct hrtimer_sleeper t;
1798
1799 /*
1800 * Optimize when a zero timeout value is given. It does not
1801 * matter whether this is an absolute or a relative time.
1802 */
1803 if (expires && !expires->tv64) {
1804 __set_current_state(TASK_RUNNING);
1805 return 0;
1806 }
1807
1808 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001809 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001810 */
1811 if (!expires) {
1812 schedule();
1813 __set_current_state(TASK_RUNNING);
1814 return -EINTR;
1815 }
1816
1817 hrtimer_init_on_stack(&t.timer, clock, mode);
1818 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1819
1820 hrtimer_init_sleeper(&t, current);
1821
1822 hrtimer_start_expires(&t.timer, mode);
1823 if (!hrtimer_active(&t.timer))
1824 t.task = NULL;
1825
1826 if (likely(t.task))
1827 schedule();
1828
1829 hrtimer_cancel(&t.timer);
1830 destroy_hrtimer_on_stack(&t.timer);
1831
1832 __set_current_state(TASK_RUNNING);
1833
1834 return !t.task ? 0 : -EINTR;
1835}
1836
1837/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001838 * schedule_hrtimeout_range - sleep until timeout
1839 * @expires: timeout value (ktime_t)
1840 * @delta: slack in expires timeout (ktime_t)
1841 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1842 *
1843 * Make the current task sleep until the given expiry time has
1844 * elapsed. The routine will return immediately unless
1845 * the current task state has been set (see set_current_state()).
1846 *
1847 * The @delta argument gives the kernel the freedom to schedule the
1848 * actual wakeup to a time that is both power and performance friendly.
1849 * The kernel give the normal best effort behavior for "@expires+@delta",
1850 * but may decide to fire the timer earlier, but no earlier than @expires.
1851 *
1852 * You can set the task state as follows -
1853 *
1854 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1855 * pass before the routine returns.
1856 *
1857 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1858 * delivered to the current task.
1859 *
1860 * The current task state is guaranteed to be TASK_RUNNING when this
1861 * routine returns.
1862 *
1863 * Returns 0 when the timer has expired otherwise -EINTR
1864 */
1865int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001866 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001867{
Carsten Emde351b3f72010-04-02 22:40:19 +02001868 return schedule_hrtimeout_range_clock(expires, delta, mode,
1869 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001870}
1871EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1872
1873/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001874 * schedule_hrtimeout - sleep until timeout
1875 * @expires: timeout value (ktime_t)
1876 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1877 *
1878 * Make the current task sleep until the given expiry time has
1879 * elapsed. The routine will return immediately unless
1880 * the current task state has been set (see set_current_state()).
1881 *
1882 * You can set the task state as follows -
1883 *
1884 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1885 * pass before the routine returns.
1886 *
1887 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1888 * delivered to the current task.
1889 *
1890 * The current task state is guaranteed to be TASK_RUNNING when this
1891 * routine returns.
1892 *
1893 * Returns 0 when the timer has expired otherwise -EINTR
1894 */
1895int __sched schedule_hrtimeout(ktime_t *expires,
1896 const enum hrtimer_mode mode)
1897{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001898 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001899}
1900EXPORT_SYMBOL_GPL(schedule_hrtimeout);