blob: e268e269a695d90985271910cff9a81ec7c9452e [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>
Colin Cross98009ee2013-05-06 23:50:19 +000050#include <linux/freezer.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080051
52#include <asm/uaccess.h>
53
Xiao Guangrongc6a2a172009-08-10 10:51:23 +080054#include <trace/events/timer.h>
55
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080056/*
57 * The timer bases:
George Anzinger79786722006-02-01 03:05:11 -080058 *
John Stultze06383d2010-12-14 19:37:07 -080059 * There are more clockids then hrtimer bases. Thus, we index
60 * into the timer bases by the hrtimer_base_type enum. When trying
61 * to reach a base using a clockid, hrtimer_clockid_to_base()
62 * is used to convert from clockid to the proper hrtimer_base_type.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080063 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080064DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080065{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080066
Michael Bohan84cc8fd2013-03-19 19:19:25 -070067 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080068 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080069 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080070 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020071 .index = HRTIMER_BASE_MONOTONIC,
72 .clockid = CLOCK_MONOTONIC,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080073 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080074 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080075 },
John Stultz70a08cc2011-02-15 10:45:16 -080076 {
Thomas Gleixner68fa61c2011-05-20 23:14:04 +020077 .index = HRTIMER_BASE_REALTIME,
78 .clockid = CLOCK_REALTIME,
79 .get_time = &ktime_get_real,
80 .resolution = KTIME_LOW_RES,
81 },
82 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020083 .index = HRTIMER_BASE_BOOTTIME,
84 .clockid = CLOCK_BOOTTIME,
John Stultz70a08cc2011-02-15 10:45:16 -080085 .get_time = &ktime_get_boottime,
86 .resolution = KTIME_LOW_RES,
87 },
John Stultz90adda92013-01-21 17:00:11 -080088 {
89 .index = HRTIMER_BASE_TAI,
90 .clockid = CLOCK_TAI,
91 .get_time = &ktime_get_clocktai,
92 .resolution = KTIME_LOW_RES,
93 },
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080094 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080095};
96
Mike Frysinger942c3c52011-05-02 15:24:27 -040097static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
Thomas Gleixnerce313322011-04-29 00:02:00 +020098 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
99 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
100 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
John Stultz90adda92013-01-21 17:00:11 -0800101 [CLOCK_TAI] = HRTIMER_BASE_TAI,
Thomas Gleixnerce313322011-04-29 00:02:00 +0200102};
John Stultze06383d2010-12-14 19:37:07 -0800103
104static inline int hrtimer_clockid_to_base(clockid_t clock_id)
105{
106 return hrtimer_clock_to_base_table[clock_id];
107}
108
109
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800110/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800111 * Get the coarse grained time at the softirq based on xtime and
112 * wall_to_monotonic.
113 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800114static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800115{
John Stultz70a08cc2011-02-15 10:45:16 -0800116 ktime_t xtim, mono, boot;
John Stultz314ac372011-02-14 18:43:08 -0800117 struct timespec xts, tom, slp;
John Stultz90adda92013-01-21 17:00:11 -0800118 s32 tai_offset;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800119
John Stultz314ac372011-02-14 18:43:08 -0800120 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
John Stultz90adda92013-01-21 17:00:11 -0800121 tai_offset = timekeeping_get_tai_offset();
Thomas Gleixner92127c72006-03-26 01:38:05 -0800122
john stultzf4304ab2007-02-16 01:27:26 -0800123 xtim = timespec_to_ktime(xts);
John Stultz70a08cc2011-02-15 10:45:16 -0800124 mono = ktime_add(xtim, timespec_to_ktime(tom));
125 boot = ktime_add(mono, timespec_to_ktime(slp));
John Stultze06383d2010-12-14 19:37:07 -0800126 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
John Stultz70a08cc2011-02-15 10:45:16 -0800127 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
128 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
John Stultz90adda92013-01-21 17:00:11 -0800129 base->clock_base[HRTIMER_BASE_TAI].softirq_time =
130 ktime_add(xtim, ktime_set(tai_offset, 0));
Thomas Gleixner92127c72006-03-26 01:38:05 -0800131}
132
133/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800134 * Functions and macros which are different for UP/SMP systems are kept in a
135 * single place
136 */
137#ifdef CONFIG_SMP
138
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800139/*
140 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
141 * means that all timers which are tied to this base via timer->base are
142 * locked, and the base itself is locked too.
143 *
144 * So __run_timers/migrate_timers can safely modify all timers which could
145 * be found on the lists/queues.
146 *
147 * When the timer's base is locked, and the timer removed from list, it is
148 * possible to set timer->base = NULL and drop the lock: the timer remains
149 * locked.
150 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800151static
152struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
153 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800154{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800155 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800156
157 for (;;) {
158 base = timer->base;
159 if (likely(base != NULL)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100160 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800161 if (likely(base == timer->base))
162 return base;
163 /* The timer has migrated to another CPU: */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100164 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800165 }
166 cpu_relax();
167 }
168}
169
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200170
171/*
172 * Get the preferred target CPU for NOHZ
173 */
174static int hrtimer_get_target(int this_cpu, int pinned)
175{
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200176#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700177 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
178 return get_nohz_timer_target();
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200179#endif
180 return this_cpu;
181}
182
183/*
184 * With HIGHRES=y we do not migrate the timer when it is expiring
185 * before the next event on the target cpu because we cannot reprogram
186 * the target cpu hardware and we would cause it to fire late.
187 *
188 * Called with cpu_base->lock of target cpu held.
189 */
190static int
191hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
192{
193#ifdef CONFIG_HIGH_RES_TIMERS
194 ktime_t expires;
195
196 if (!new_base->cpu_base->hres_active)
197 return 0;
198
199 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
200 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
201#else
202 return 0;
203#endif
204}
205
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800206/*
207 * Switch the timer base to the current CPU when possible.
208 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800209static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530210switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
211 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800212{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800213 struct hrtimer_clock_base *new_base;
214 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200215 int this_cpu = smp_processor_id();
216 int cpu = hrtimer_get_target(this_cpu, pinned);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200217 int basenum = base->index;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530218
219again:
220 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
John Stultze06383d2010-12-14 19:37:07 -0800221 new_base = &new_cpu_base->clock_base[basenum];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800222
223 if (base != new_base) {
224 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200225 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800226 * However we can't change timer's base while it is running,
227 * so we keep it on the same CPU. No hassle vs. reprogramming
228 * the event source in the high resolution case. The softirq
229 * code will take care of this when the timer function has
230 * completed. There is no conflict as we hold the lock until
231 * the timer is enqueued.
232 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800233 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800234 return base;
235
236 /* See the comment in lock_timer_base() */
237 timer->base = NULL;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100238 raw_spin_unlock(&base->cpu_base->lock);
239 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530240
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200241 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
242 cpu = this_cpu;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100243 raw_spin_unlock(&new_base->cpu_base->lock);
244 raw_spin_lock(&base->cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200245 timer->base = base;
246 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530247 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800248 timer->base = new_base;
249 }
250 return new_base;
251}
252
253#else /* CONFIG_SMP */
254
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800255static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800256lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
257{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800258 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800259
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100260 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800261
262 return base;
263}
264
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530265# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800266
267#endif /* !CONFIG_SMP */
268
269/*
270 * Functions for the union type storage format of ktime_t which are
271 * too large for inlining:
272 */
273#if BITS_PER_LONG < 64
274# ifndef CONFIG_KTIME_SCALAR
275/**
276 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800277 * @kt: addend
278 * @nsec: the scalar nsec value to add
279 *
280 * Returns the sum of kt and nsec in ktime_t format
281 */
282ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
283{
284 ktime_t tmp;
285
286 if (likely(nsec < NSEC_PER_SEC)) {
287 tmp.tv64 = nsec;
288 } else {
289 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
290
David Engraf51fd36f2013-03-19 13:29:55 +0100291 /* Make sure nsec fits into long */
292 if (unlikely(nsec > KTIME_SEC_MAX))
293 return (ktime_t){ .tv64 = KTIME_MAX };
294
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800295 tmp = ktime_set((long)nsec, rem);
296 }
297
298 return ktime_add(kt, tmp);
299}
David Howellsb8b8fd22007-04-27 15:31:24 -0700300
301EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700302
303/**
304 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
305 * @kt: minuend
306 * @nsec: the scalar nsec value to subtract
307 *
308 * Returns the subtraction of @nsec from @kt in ktime_t format
309 */
310ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
311{
312 ktime_t tmp;
313
314 if (likely(nsec < NSEC_PER_SEC)) {
315 tmp.tv64 = nsec;
316 } else {
317 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
318
319 tmp = ktime_set((long)nsec, rem);
320 }
321
322 return ktime_sub(kt, tmp);
323}
324
325EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800326# endif /* !CONFIG_KTIME_SCALAR */
327
328/*
329 * Divide a ktime value by a nanosecond value
330 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800331u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800332{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300333 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800334 int sft = 0;
335
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300336 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800337 /* Make sure the divisor is less than 2^32: */
338 while (div >> 32) {
339 sft++;
340 div >>= 1;
341 }
342 dclc >>= sft;
343 do_div(dclc, (unsigned long) div);
344
Davide Libenzi4d672e72008-02-04 22:27:26 -0800345 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800346}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800347#endif /* BITS_PER_LONG >= 64 */
348
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100349/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100350 * Add two ktime values and do a safety check for overflow:
351 */
352ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
353{
354 ktime_t res = ktime_add(lhs, rhs);
355
356 /*
357 * We use KTIME_SEC_MAX here, the maximum timeout which we can
358 * return to user space in a timespec:
359 */
360 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
361 res = ktime_set(KTIME_SEC_MAX, 0);
362
363 return res;
364}
365
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300366EXPORT_SYMBOL_GPL(ktime_add_safe);
367
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700368#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
369
370static struct debug_obj_descr hrtimer_debug_descr;
371
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100372static void *hrtimer_debug_hint(void *addr)
373{
374 return ((struct hrtimer *) addr)->function;
375}
376
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700377/*
378 * fixup_init is called when:
379 * - an active object is initialized
380 */
381static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
382{
383 struct hrtimer *timer = addr;
384
385 switch (state) {
386 case ODEBUG_STATE_ACTIVE:
387 hrtimer_cancel(timer);
388 debug_object_init(timer, &hrtimer_debug_descr);
389 return 1;
390 default:
391 return 0;
392 }
393}
394
395/*
396 * fixup_activate is called when:
397 * - an active object is activated
398 * - an unknown object is activated (might be a statically initialized object)
399 */
400static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
401{
402 switch (state) {
403
404 case ODEBUG_STATE_NOTAVAILABLE:
405 WARN_ON_ONCE(1);
406 return 0;
407
408 case ODEBUG_STATE_ACTIVE:
409 WARN_ON(1);
410
411 default:
412 return 0;
413 }
414}
415
416/*
417 * fixup_free is called when:
418 * - an active object is freed
419 */
420static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
421{
422 struct hrtimer *timer = addr;
423
424 switch (state) {
425 case ODEBUG_STATE_ACTIVE:
426 hrtimer_cancel(timer);
427 debug_object_free(timer, &hrtimer_debug_descr);
428 return 1;
429 default:
430 return 0;
431 }
432}
433
434static struct debug_obj_descr hrtimer_debug_descr = {
435 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100436 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700437 .fixup_init = hrtimer_fixup_init,
438 .fixup_activate = hrtimer_fixup_activate,
439 .fixup_free = hrtimer_fixup_free,
440};
441
442static inline void debug_hrtimer_init(struct hrtimer *timer)
443{
444 debug_object_init(timer, &hrtimer_debug_descr);
445}
446
447static inline void debug_hrtimer_activate(struct hrtimer *timer)
448{
449 debug_object_activate(timer, &hrtimer_debug_descr);
450}
451
452static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
453{
454 debug_object_deactivate(timer, &hrtimer_debug_descr);
455}
456
457static inline void debug_hrtimer_free(struct hrtimer *timer)
458{
459 debug_object_free(timer, &hrtimer_debug_descr);
460}
461
462static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
463 enum hrtimer_mode mode);
464
465void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
466 enum hrtimer_mode mode)
467{
468 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
469 __hrtimer_init(timer, clock_id, mode);
470}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700471EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700472
473void destroy_hrtimer_on_stack(struct hrtimer *timer)
474{
475 debug_object_free(timer, &hrtimer_debug_descr);
476}
477
478#else
479static inline void debug_hrtimer_init(struct hrtimer *timer) { }
480static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
481static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
482#endif
483
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800484static inline void
485debug_init(struct hrtimer *timer, clockid_t clockid,
486 enum hrtimer_mode mode)
487{
488 debug_hrtimer_init(timer);
489 trace_hrtimer_init(timer, clockid, mode);
490}
491
492static inline void debug_activate(struct hrtimer *timer)
493{
494 debug_hrtimer_activate(timer);
495 trace_hrtimer_start(timer);
496}
497
498static inline void debug_deactivate(struct hrtimer *timer)
499{
500 debug_hrtimer_deactivate(timer);
501 trace_hrtimer_cancel(timer);
502}
503
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800504/* High resolution timer related functions */
505#ifdef CONFIG_HIGH_RES_TIMERS
506
507/*
508 * High resolution timer enabled ?
509 */
510static int hrtimer_hres_enabled __read_mostly = 1;
511
512/*
513 * Enable / Disable high resolution mode
514 */
515static int __init setup_hrtimer_hres(char *str)
516{
517 if (!strcmp(str, "off"))
518 hrtimer_hres_enabled = 0;
519 else if (!strcmp(str, "on"))
520 hrtimer_hres_enabled = 1;
521 else
522 return 0;
523 return 1;
524}
525
526__setup("highres=", setup_hrtimer_hres);
527
528/*
529 * hrtimer_high_res_enabled - query, if the highres mode is enabled
530 */
531static inline int hrtimer_is_hres_enabled(void)
532{
533 return hrtimer_hres_enabled;
534}
535
536/*
537 * Is the high resolution mode active ?
538 */
539static inline int hrtimer_hres_active(void)
540{
Christoph Lameter909ea962010-12-08 16:22:55 +0100541 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800542}
543
544/*
545 * Reprogram the event source with checking both queues for the
546 * next event
547 * Called with interrupts disabled and base->lock held
548 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400549static void
550hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800551{
552 int i;
553 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400554 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800555
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400556 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800557
558 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
559 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700560 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800561
John Stultz998adc32010-09-20 19:19:17 -0700562 next = timerqueue_getnext(&base->active);
563 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800564 continue;
John Stultz998adc32010-09-20 19:19:17 -0700565 timer = container_of(next, struct hrtimer, node);
566
Arjan van de Vencc584b22008-09-01 15:02:30 -0700567 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100568 /*
569 * clock_was_set() has changed base->offset so the
570 * result might be negative. Fix it up to prevent a
571 * false positive in clockevents_program_event()
572 */
573 if (expires.tv64 < 0)
574 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400575 if (expires.tv64 < expires_next.tv64)
576 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800577 }
578
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400579 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
580 return;
581
582 cpu_base->expires_next.tv64 = expires_next.tv64;
583
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800584 if (cpu_base->expires_next.tv64 != KTIME_MAX)
585 tick_program_event(cpu_base->expires_next, 1);
586}
587
588/*
589 * Shared reprogramming for clock_realtime and clock_monotonic
590 *
591 * When a timer is enqueued and expires earlier than the already enqueued
592 * timers, we have to check, whether it expires earlier than the timer for
593 * which the clock event device was armed.
594 *
595 * Called with interrupts disabled and base->cpu_base.lock held
596 */
597static int hrtimer_reprogram(struct hrtimer *timer,
598 struct hrtimer_clock_base *base)
599{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100600 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700601 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800602 int res;
603
Arjan van de Vencc584b22008-09-01 15:02:30 -0700604 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100605
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800606 /*
607 * When the callback is running, we do not reprogram the clock event
608 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200609 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800610 * reprogramming is handled either by the softirq, which called the
611 * callback or at the end of the hrtimer_interrupt.
612 */
613 if (hrtimer_callback_running(timer))
614 return 0;
615
Thomas Gleixner63070a72008-02-14 00:58:36 +0100616 /*
617 * CLOCK_REALTIME timer might be requested with an absolute
618 * expiry time which is less than base->offset. Nothing wrong
619 * about that, just avoid to call into the tick code, which
620 * has now objections against negative expiry values.
621 */
622 if (expires.tv64 < 0)
623 return -ETIME;
624
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100625 if (expires.tv64 >= cpu_base->expires_next.tv64)
626 return 0;
627
628 /*
629 * If a hang was detected in the last timer interrupt then we
630 * do not schedule a timer which is earlier than the expiry
631 * which we enforced in the hang detection. We want the system
632 * to make progress.
633 */
634 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800635 return 0;
636
637 /*
638 * Clockevents returns -ETIME, when the event was in the past.
639 */
640 res = tick_program_event(expires, 0);
641 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100642 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800643 return res;
644}
645
Ingo Molnar995f0542007-04-07 12:05:00 +0200646/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800647 * Initialize the high resolution related parts of cpu_base
648 */
649static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
650{
651 base->expires_next.tv64 = KTIME_MAX;
652 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800653}
654
655/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800656 * When High resolution timers are active, try to reprogram. Note, that in case
657 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
658 * check happens. The timer gets enqueued into the rbtree. The reprogramming
659 * and expiry check is done in the hrtimer_interrupt or in the softirq.
660 */
661static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzb22affe2013-02-04 14:33:37 +0200662 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800663{
Leonid Shatzb22affe2013-02-04 14:33:37 +0200664 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800665}
666
John Stultz5baefd62012-07-10 18:43:25 -0400667static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
668{
669 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
670 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
John Stultz90adda92013-01-21 17:00:11 -0800671 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
John Stultz5baefd62012-07-10 18:43:25 -0400672
John Stultz90adda92013-01-21 17:00:11 -0800673 return ktime_get_update_offsets(offs_real, offs_boot, offs_tai);
John Stultz5baefd62012-07-10 18:43:25 -0400674}
675
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200676/*
677 * Retrigger next event is called after clock was set
678 *
679 * Called with interrupts disabled via on_each_cpu()
680 */
681static void retrigger_next_event(void *arg)
682{
683 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200684
685 if (!hrtimer_hres_active())
686 return;
687
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200688 raw_spin_lock(&base->lock);
John Stultz5baefd62012-07-10 18:43:25 -0400689 hrtimer_update_base(base);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200690 hrtimer_force_reprogram(base, 0);
691 raw_spin_unlock(&base->lock);
692}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200693
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800694/*
695 * Switch to high resolution mode
696 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800697static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800698{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200699 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700700 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800701 unsigned long flags;
702
703 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800704 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800705
706 local_irq_save(flags);
707
708 if (tick_init_highres()) {
709 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700710 printk(KERN_WARNING "Could not switch to high resolution "
711 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800712 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800713 }
714 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200715 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
716 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800717
718 tick_setup_sched_timer();
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800719 /* "Retrigger" the interrupt to get things going */
720 retrigger_next_event(NULL);
721 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800722 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800723}
724
Thomas Gleixner2d06fa02013-07-05 12:09:18 +0200725static void clock_was_set_work(struct work_struct *work)
726{
727 clock_was_set();
728}
729
730static DECLARE_WORK(hrtimer_work, clock_was_set_work);
731
John Stultzf55a6fa2012-07-10 18:43:19 -0400732/*
Thomas Gleixner2d06fa02013-07-05 12:09:18 +0200733 * Called from timekeeping and resume code to reprogramm the hrtimer
734 * interrupt device on all cpus.
John Stultzf55a6fa2012-07-10 18:43:19 -0400735 */
736void clock_was_set_delayed(void)
737{
Thomas Gleixner2d06fa02013-07-05 12:09:18 +0200738 schedule_work(&hrtimer_work);
John Stultzf55a6fa2012-07-10 18:43:19 -0400739}
740
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800741#else
742
743static inline int hrtimer_hres_active(void) { return 0; }
744static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800745static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400746static inline void
747hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800748static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzb22affe2013-02-04 14:33:37 +0200749 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800750{
751 return 0;
752}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800753static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200754static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800755
756#endif /* CONFIG_HIGH_RES_TIMERS */
757
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200758/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200759 * Clock realtime was set
760 *
761 * Change the offset of the realtime clock vs. the monotonic
762 * clock.
763 *
764 * We might have to reprogram the high resolution timer interrupt. On
765 * SMP we call the architecture specific code to retrigger _all_ high
766 * resolution timer interrupts. On UP we just disable interrupts and
767 * call the high resolution interrupt code.
768 */
769void clock_was_set(void)
770{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200771#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200772 /* Retrigger the CPU local events everywhere */
773 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200774#endif
775 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200776}
777
778/*
779 * During resume we might have to reprogram the high resolution timer
780 * interrupt (on the local CPU):
781 */
782void hrtimers_resume(void)
783{
784 WARN_ONCE(!irqs_disabled(),
785 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
786
Thomas Gleixner2d06fa02013-07-05 12:09:18 +0200787 /* Retrigger on the local CPU */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200788 retrigger_next_event(NULL);
Thomas Gleixner2d06fa02013-07-05 12:09:18 +0200789 /* And schedule a retrigger for all others */
790 clock_was_set_delayed();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200791}
792
Heiko Carstens5f201902009-12-10 10:56:29 +0100793static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800794{
Heiko Carstens5f201902009-12-10 10:56:29 +0100795#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800796 if (timer->start_site)
797 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100798 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800799 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
800 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800801#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100802}
803
804static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
805{
806#ifdef CONFIG_TIMER_STATS
807 timer->start_site = NULL;
808#endif
809}
810
811static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
812{
813#ifdef CONFIG_TIMER_STATS
814 if (likely(!timer_stats_active))
815 return;
816 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
817 timer->function, timer->start_comm, 0);
818#endif
819}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800820
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200822 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823 */
824static inline
825void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
826{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100827 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800828}
829
830/**
831 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800833 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800834 * @interval: the interval to forward
835 *
836 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700837 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800838 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800839u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800840{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800841 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800842 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800843
Arjan van de Vencc584b22008-09-01 15:02:30 -0700844 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800845
846 if (delta.tv64 < 0)
847 return 0;
848
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100849 if (interval.tv64 < timer->base->resolution.tv64)
850 interval.tv64 = timer->base->resolution.tv64;
851
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800853 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800854
855 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700856 hrtimer_add_expires_ns(timer, incr * orun);
857 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800858 return orun;
859 /*
860 * This (and the ktime_add() below) is the
861 * correction for exact:
862 */
863 orun++;
864 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700865 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800866
867 return orun;
868}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700869EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800870
871/*
872 * enqueue_hrtimer - internal function to (re)start a timer
873 *
874 * The timer is inserted in expiry order. Insertion into the
875 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100876 *
877 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800878 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100879static int enqueue_hrtimer(struct hrtimer *timer,
880 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800881{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800882 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700883
John Stultz998adc32010-09-20 19:19:17 -0700884 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200885 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800886
887 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800888 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
889 * state of a possibly running callback.
890 */
891 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100892
John Stultz998adc32010-09-20 19:19:17 -0700893 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100894}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800895
896/*
897 * __remove_hrtimer - internal function to remove a timer
898 *
899 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800900 *
901 * High resolution timer mode reprograms the clock event device when the
902 * timer is the one which expires next. The caller can disable this by setting
903 * reprogram to zero. This is useful, when the context does a reprogramming
904 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800905 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800906static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800907 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800908 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800909{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800910 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400911 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
912 goto out;
913
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800914 next_timer = timerqueue_getnext(&base->active);
915 timerqueue_del(&base->active, &timer->node);
916 if (&timer->node == next_timer) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400917#ifdef CONFIG_HIGH_RES_TIMERS
918 /* Reprogram the clock event device. if enabled */
919 if (reprogram && hrtimer_hres_active()) {
920 ktime_t expires;
921
922 expires = ktime_sub(hrtimer_get_expires(timer),
923 base->offset);
924 if (base->cpu_base->expires_next.tv64 == expires.tv64)
925 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800926 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400927#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800928 }
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200929 if (!timerqueue_getnext(&base->active))
930 base->cpu_base->active_bases &= ~(1 << base->index);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400931out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800932 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800933}
934
935/*
936 * remove hrtimer, called with base lock held
937 */
938static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800939remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800940{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800941 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700942 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800943 int reprogram;
944
945 /*
946 * Remove the timer and force reprogramming when high
947 * resolution mode is active and the timer is on the current
948 * CPU. If we remove a timer on another CPU, reprogramming is
949 * skipped. The interrupt event on this CPU is fired and
950 * reprogramming happens in the interrupt handler. This is a
951 * rare case and less expensive than a smp call.
952 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800953 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800954 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800955 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700956 /*
957 * We must preserve the CALLBACK state flag here,
958 * otherwise we could move the timer base in
959 * switch_hrtimer_base.
960 */
961 state = timer->state & HRTIMER_STATE_CALLBACK;
962 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800963 return 1;
964 }
965 return 0;
966}
967
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100968int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
969 unsigned long delta_ns, const enum hrtimer_mode mode,
970 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800971{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800972 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800973 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100974 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800975
976 base = lock_hrtimer_base(timer, &flags);
977
978 /* Remove an active timer from the queue: */
979 ret = remove_hrtimer(timer, base);
980
981 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530982 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800983
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530984 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100985 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800986 /*
987 * CONFIG_TIME_LOW_RES is a temporary way for architectures
988 * to signal that they simply return xtime in
989 * do_gettimeoffset(). In this case we want to round up by
990 * resolution when starting a relative timer, to avoid short
991 * timeouts. This will go away with the GTOD framework.
992 */
993#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100994 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800995#endif
996 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700997
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700998 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800999
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001000 timer_stats_hrtimer_set_start_info(timer);
1001
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001002 leftmost = enqueue_hrtimer(timer, new_base);
1003
Ingo Molnar935c6312007-03-28 13:17:18 +02001004 /*
1005 * Only allow reprogramming if the new base is on this CPU.
1006 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001007 *
1008 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +02001009 */
Leonid Shatzb22affe2013-02-04 14:33:37 +02001010 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
1011 && hrtimer_enqueue_reprogram(timer, new_base)) {
1012 if (wakeup) {
1013 /*
1014 * We need to drop cpu_base->lock to avoid a
1015 * lock ordering issue vs. rq->lock.
1016 */
1017 raw_spin_unlock(&new_base->cpu_base->lock);
1018 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1019 local_irq_restore(flags);
1020 return ret;
1021 } else {
1022 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1023 }
1024 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001025
1026 unlock_hrtimer_base(timer, &flags);
1027
1028 return ret;
1029}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001030
1031/**
1032 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1033 * @timer: the timer to be added
1034 * @tim: expiry time
1035 * @delta_ns: "slack" range for the timer
David Daney8ffbc7d2013-03-13 12:20:38 -07001036 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1037 * relative (HRTIMER_MODE_REL)
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001038 *
1039 * Returns:
1040 * 0 on success
1041 * 1 when the timer was active
1042 */
1043int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1044 unsigned long delta_ns, const enum hrtimer_mode mode)
1045{
1046 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1047}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001048EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1049
1050/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001051 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001052 * @timer: the timer to be added
1053 * @tim: expiry time
David Daney8ffbc7d2013-03-13 12:20:38 -07001054 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1055 * relative (HRTIMER_MODE_REL)
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001056 *
1057 * Returns:
1058 * 0 on success
1059 * 1 when the timer was active
1060 */
1061int
1062hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1063{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001064 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001065}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001066EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001067
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001068
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001069/**
1070 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001071 * @timer: hrtimer to stop
1072 *
1073 * Returns:
1074 * 0 when the timer was not active
1075 * 1 when the timer was active
1076 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001077 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001078 */
1079int hrtimer_try_to_cancel(struct hrtimer *timer)
1080{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001081 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001082 unsigned long flags;
1083 int ret = -1;
1084
1085 base = lock_hrtimer_base(timer, &flags);
1086
Thomas Gleixner303e9672007-02-16 01:27:51 -08001087 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001088 ret = remove_hrtimer(timer, base);
1089
1090 unlock_hrtimer_base(timer, &flags);
1091
1092 return ret;
1093
1094}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001095EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001096
1097/**
1098 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001099 * @timer: the timer to be cancelled
1100 *
1101 * Returns:
1102 * 0 when the timer was not active
1103 * 1 when the timer was active
1104 */
1105int hrtimer_cancel(struct hrtimer *timer)
1106{
1107 for (;;) {
1108 int ret = hrtimer_try_to_cancel(timer);
1109
1110 if (ret >= 0)
1111 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001112 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001113 }
1114}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001115EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001116
1117/**
1118 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001119 * @timer: the timer to read
1120 */
1121ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1122{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001123 unsigned long flags;
1124 ktime_t rem;
1125
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001126 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001127 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001128 unlock_hrtimer_base(timer, &flags);
1129
1130 return rem;
1131}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001132EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001133
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001134#ifdef CONFIG_NO_HZ_COMMON
Tony Lindgren69239742006-03-06 15:42:45 -08001135/**
1136 * hrtimer_get_next_event - get the time until next expiry event
1137 *
1138 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1139 * is pending.
1140 */
1141ktime_t hrtimer_get_next_event(void)
1142{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001143 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1144 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001145 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1146 unsigned long flags;
1147 int i;
1148
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001149 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001150
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001151 if (!hrtimer_hres_active()) {
1152 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1153 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001154 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001155
John Stultz998adc32010-09-20 19:19:17 -07001156 next = timerqueue_getnext(&base->active);
1157 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001158 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001159
John Stultz998adc32010-09-20 19:19:17 -07001160 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001161 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001162 delta = ktime_sub(delta, base->get_time());
1163 if (delta.tv64 < mindelta.tv64)
1164 mindelta.tv64 = delta.tv64;
1165 }
Tony Lindgren69239742006-03-06 15:42:45 -08001166 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001167
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001168 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001169
Tony Lindgren69239742006-03-06 15:42:45 -08001170 if (mindelta.tv64 < 0)
1171 mindelta.tv64 = 0;
1172 return mindelta;
1173}
1174#endif
1175
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001176static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1177 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001178{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001179 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001180 int base;
George Anzinger79786722006-02-01 03:05:11 -08001181
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001182 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001183
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001184 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001185
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001186 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001187 clock_id = CLOCK_MONOTONIC;
1188
John Stultze06383d2010-12-14 19:37:07 -08001189 base = hrtimer_clockid_to_base(clock_id);
1190 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001191 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001192
1193#ifdef CONFIG_TIMER_STATS
1194 timer->start_site = NULL;
1195 timer->start_pid = -1;
1196 memset(timer->start_comm, 0, TASK_COMM_LEN);
1197#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001198}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001199
1200/**
1201 * hrtimer_init - initialize a timer to the given clock
1202 * @timer: the timer to be initialized
1203 * @clock_id: the clock to be used
1204 * @mode: timer mode abs/rel
1205 */
1206void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1207 enum hrtimer_mode mode)
1208{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001209 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001210 __hrtimer_init(timer, clock_id, mode);
1211}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001212EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001213
1214/**
1215 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001216 * @which_clock: which clock to query
1217 * @tp: pointer to timespec variable to store the resolution
1218 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001219 * Store the resolution of the clock selected by @which_clock in the
1220 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001221 */
1222int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1223{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001224 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001225 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001226
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001227 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001228 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001229
1230 return 0;
1231}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001232EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001233
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001234static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001235{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001236 struct hrtimer_clock_base *base = timer->base;
1237 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1238 enum hrtimer_restart (*fn)(struct hrtimer *);
1239 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001240
Peter Zijlstraca109492008-11-25 12:43:51 +01001241 WARN_ON(!irqs_disabled());
1242
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001243 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001244 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1245 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001246 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001247
1248 /*
1249 * Because we run timers from hardirq context, there is no chance
1250 * they get migrated to another cpu, therefore its safe to unlock
1251 * the timer base.
1252 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001253 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001254 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001255 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001256 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001257 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001258
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001259 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001260 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1261 * we do not reprogramm the event hardware. Happens either in
1262 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001263 */
1264 if (restart != HRTIMER_NORESTART) {
1265 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001266 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001267 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001268
1269 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1270
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001271 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001272}
1273
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001274#ifdef CONFIG_HIGH_RES_TIMERS
1275
1276/*
1277 * High resolution timer interrupt
1278 * Called with interrupts disabled
1279 */
1280void hrtimer_interrupt(struct clock_event_device *dev)
1281{
1282 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001283 ktime_t expires_next, now, entry_time, delta;
1284 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001285
1286 BUG_ON(!cpu_base->hres_active);
1287 cpu_base->nr_events++;
1288 dev->next_event.tv64 = KTIME_MAX;
1289
Thomas Gleixner196951e2012-07-10 18:43:23 -04001290 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001291 entry_time = now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001292retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001293 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001294 /*
1295 * We set expires_next to KTIME_MAX here with cpu_base->lock
1296 * held to prevent that a timer is enqueued in our queue via
1297 * the migration code. This does not affect enqueueing of
1298 * timers which run their callback and need to be requeued on
1299 * this CPU.
1300 */
1301 cpu_base->expires_next.tv64 = KTIME_MAX;
1302
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001303 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001304 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001305 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001306 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001307
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001308 if (!(cpu_base->active_bases & (1 << i)))
1309 continue;
1310
1311 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001312 basenow = ktime_add(now, base->offset);
1313
John Stultz998adc32010-09-20 19:19:17 -07001314 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001315 struct hrtimer *timer;
1316
John Stultz998adc32010-09-20 19:19:17 -07001317 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001318
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001319 /*
1320 * The immediate goal for using the softexpires is
1321 * minimizing wakeups, not running timers at the
1322 * earliest interrupt after their soft expiration.
1323 * This allows us to avoid using a Priority Search
1324 * Tree, which can answer a stabbing querry for
1325 * overlapping intervals and instead use the simple
1326 * BST we already have.
1327 * We don't add extra wakeups by delaying timers that
1328 * are right-of a not yet expired timer, because that
1329 * timer will have to trigger a wakeup anyway.
1330 */
1331
1332 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001333 ktime_t expires;
1334
Arjan van de Vencc584b22008-09-01 15:02:30 -07001335 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001336 base->offset);
Prarit Bhargava8f294b52013-04-08 08:47:15 -04001337 if (expires.tv64 < 0)
1338 expires.tv64 = KTIME_MAX;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001339 if (expires.tv64 < expires_next.tv64)
1340 expires_next = expires;
1341 break;
1342 }
1343
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001344 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001345 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001346 }
1347
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001348 /*
1349 * Store the new expiry value so the migration code can verify
1350 * against it.
1351 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001352 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001353 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001354
1355 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001356 if (expires_next.tv64 == KTIME_MAX ||
1357 !tick_program_event(expires_next, 0)) {
1358 cpu_base->hang_detected = 0;
1359 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001360 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001361
1362 /*
1363 * The next timer was already expired due to:
1364 * - tracing
1365 * - long lasting callbacks
1366 * - being scheduled away when running in a VM
1367 *
1368 * We need to prevent that we loop forever in the hrtimer
1369 * interrupt routine. We give it 3 attempts to avoid
1370 * overreacting on some spurious event.
John Stultz5baefd62012-07-10 18:43:25 -04001371 *
1372 * Acquire base lock for updating the offsets and retrieving
1373 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001374 */
Thomas Gleixner196951e2012-07-10 18:43:23 -04001375 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001376 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001377 cpu_base->nr_retries++;
1378 if (++retries < 3)
1379 goto retry;
1380 /*
1381 * Give the system a chance to do something else than looping
1382 * here. We stored the entry time, so we know exactly how long
1383 * we spent here. We schedule the next event this amount of
1384 * time away.
1385 */
1386 cpu_base->nr_hangs++;
1387 cpu_base->hang_detected = 1;
Thomas Gleixner196951e2012-07-10 18:43:23 -04001388 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001389 delta = ktime_sub(now, entry_time);
1390 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1391 cpu_base->max_hang_time = delta;
1392 /*
1393 * Limit it to a sensible value as we enforce a longer
1394 * delay. Give the CPU at least 100ms to catch up.
1395 */
1396 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1397 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1398 else
1399 expires_next = ktime_add(now, delta);
1400 tick_program_event(expires_next, 1);
1401 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1402 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001403}
1404
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001405/*
1406 * local version of hrtimer_peek_ahead_timers() called with interrupts
1407 * disabled.
1408 */
1409static void __hrtimer_peek_ahead_timers(void)
1410{
1411 struct tick_device *td;
1412
1413 if (!hrtimer_hres_active())
1414 return;
1415
1416 td = &__get_cpu_var(tick_cpu_device);
1417 if (td && td->evtdev)
1418 hrtimer_interrupt(td->evtdev);
1419}
1420
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001421/**
1422 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1423 *
1424 * hrtimer_peek_ahead_timers will peek at the timer queue of
1425 * the current cpu and check if there are any timers for which
1426 * the soft expires time has passed. If any such timers exist,
1427 * they are run immediately and then removed from the timer queue.
1428 *
1429 */
1430void hrtimer_peek_ahead_timers(void)
1431{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001432 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001433
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001434 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001435 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001436 local_irq_restore(flags);
1437}
1438
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001439static void run_hrtimer_softirq(struct softirq_action *h)
1440{
1441 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))
Colin Cross98009ee2013-05-06 23:50:19 +00001547 freezable_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 Anzinger79786722006-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
John Stultz998adc32010-09-20 19:19:17 -07001665 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001666 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001667 timerqueue_init_head(&cpu_base->clock_base[i].active);
1668 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001669
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001670 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001671}
1672
1673#ifdef CONFIG_HOTPLUG_CPU
1674
Peter Zijlstraca109492008-11-25 12:43:51 +01001675static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001676 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001677{
1678 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001679 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001680
John Stultz998adc32010-09-20 19:19:17 -07001681 while ((node = timerqueue_getnext(&old_base->active))) {
1682 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001683 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001684 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001685
1686 /*
1687 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1688 * timer could be seen as !active and just vanish away
1689 * under us on another CPU
1690 */
1691 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001692 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001693 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001694 * Enqueue the timers on the new cpu. This does not
1695 * reprogram the event device in case the timer
1696 * expires before the earliest on this CPU, but we run
1697 * hrtimer_interrupt after we migrated everything to
1698 * sort out already expired timers and reprogram the
1699 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001700 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001701 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001702
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001703 /* Clear the migration state bit */
1704 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001705 }
1706}
1707
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001708static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001709{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001710 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001711 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001712
Peter Zijlstra37810652008-12-04 11:17:10 +01001713 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001714 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001715
1716 local_irq_disable();
1717 old_base = &per_cpu(hrtimer_bases, scpu);
1718 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001719 /*
1720 * The caller is globally serialized and nobody else
1721 * takes two locks at once, deadlock is not possible.
1722 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001723 raw_spin_lock(&new_base->lock);
1724 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001725
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001726 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001727 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001728 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001729 }
1730
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001731 raw_spin_unlock(&old_base->lock);
1732 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001733
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001734 /* Check, if we got expired work to do */
1735 __hrtimer_peek_ahead_timers();
1736 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001737}
1738
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001739#endif /* CONFIG_HOTPLUG_CPU */
1740
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001741static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001742 unsigned long action, void *hcpu)
1743{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001744 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001745
1746 switch (action) {
1747
1748 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001749 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001750 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001751 break;
1752
1753#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001754 case CPU_DYING:
1755 case CPU_DYING_FROZEN:
1756 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1757 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001758 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001759 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001760 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001761 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001762 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001763 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001764 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001765#endif
1766
1767 default:
1768 break;
1769 }
1770
1771 return NOTIFY_OK;
1772}
1773
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001774static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001775 .notifier_call = hrtimer_cpu_notify,
1776};
1777
1778void __init hrtimers_init(void)
1779{
1780 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1781 (void *)(long)smp_processor_id());
1782 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001783#ifdef CONFIG_HIGH_RES_TIMERS
1784 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1785#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001786}
1787
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001788/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001789 * schedule_hrtimeout_range_clock - sleep until timeout
1790 * @expires: timeout value (ktime_t)
1791 * @delta: slack in expires timeout (ktime_t)
1792 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1793 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1794 */
1795int __sched
1796schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1797 const enum hrtimer_mode mode, int clock)
1798{
1799 struct hrtimer_sleeper t;
1800
1801 /*
1802 * Optimize when a zero timeout value is given. It does not
1803 * matter whether this is an absolute or a relative time.
1804 */
1805 if (expires && !expires->tv64) {
1806 __set_current_state(TASK_RUNNING);
1807 return 0;
1808 }
1809
1810 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001811 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001812 */
1813 if (!expires) {
1814 schedule();
1815 __set_current_state(TASK_RUNNING);
1816 return -EINTR;
1817 }
1818
1819 hrtimer_init_on_stack(&t.timer, clock, mode);
1820 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1821
1822 hrtimer_init_sleeper(&t, current);
1823
1824 hrtimer_start_expires(&t.timer, mode);
1825 if (!hrtimer_active(&t.timer))
1826 t.task = NULL;
1827
1828 if (likely(t.task))
1829 schedule();
1830
1831 hrtimer_cancel(&t.timer);
1832 destroy_hrtimer_on_stack(&t.timer);
1833
1834 __set_current_state(TASK_RUNNING);
1835
1836 return !t.task ? 0 : -EINTR;
1837}
1838
1839/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001840 * schedule_hrtimeout_range - sleep until timeout
1841 * @expires: timeout value (ktime_t)
1842 * @delta: slack in expires timeout (ktime_t)
1843 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1844 *
1845 * Make the current task sleep until the given expiry time has
1846 * elapsed. The routine will return immediately unless
1847 * the current task state has been set (see set_current_state()).
1848 *
1849 * The @delta argument gives the kernel the freedom to schedule the
1850 * actual wakeup to a time that is both power and performance friendly.
1851 * The kernel give the normal best effort behavior for "@expires+@delta",
1852 * but may decide to fire the timer earlier, but no earlier than @expires.
1853 *
1854 * You can set the task state as follows -
1855 *
1856 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1857 * pass before the routine returns.
1858 *
1859 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1860 * delivered to the current task.
1861 *
1862 * The current task state is guaranteed to be TASK_RUNNING when this
1863 * routine returns.
1864 *
1865 * Returns 0 when the timer has expired otherwise -EINTR
1866 */
1867int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001868 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001869{
Carsten Emde351b3f72010-04-02 22:40:19 +02001870 return schedule_hrtimeout_range_clock(expires, delta, mode,
1871 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001872}
1873EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1874
1875/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001876 * schedule_hrtimeout - sleep until timeout
1877 * @expires: timeout value (ktime_t)
1878 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1879 *
1880 * Make the current task sleep until the given expiry time has
1881 * elapsed. The routine will return immediately unless
1882 * the current task state has been set (see set_current_state()).
1883 *
1884 * You can set the task state as follows -
1885 *
1886 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1887 * pass before the routine returns.
1888 *
1889 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1890 * delivered to the current task.
1891 *
1892 * The current task state is guaranteed to be TASK_RUNNING when this
1893 * routine returns.
1894 *
1895 * Returns 0 when the timer has expired otherwise -EINTR
1896 */
1897int __sched schedule_hrtimeout(ktime_t *expires,
1898 const enum hrtimer_mode mode)
1899{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001900 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001901}
1902EXPORT_SYMBOL_GPL(schedule_hrtimeout);