blob: 8a9aa59d0d61ba379564ece409088ff4a1de36a4 [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>
Arun R Bharadwajeea08f32009-04-16 12:16:41 +053048#include <linux/timer.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080049
50#include <asm/uaccess.h>
51
Xiao Guangrongc6a2a172009-08-10 10:51:23 +080052#include <trace/events/timer.h>
53
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080054/*
55 * The timer bases:
George Anzinger7978672c2006-02-01 03:05:11 -080056 *
John Stultze06383d2010-12-14 19:37:07 -080057 * There are more clockids then hrtimer bases. Thus, we index
58 * into the timer bases by the hrtimer_base_type enum. When trying
59 * to reach a base using a clockid, hrtimer_clockid_to_base()
60 * is used to convert from clockid to the proper hrtimer_base_type.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080061 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080062DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080063{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080064
65 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080066 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080067 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020068 .index = HRTIMER_BASE_MONOTONIC,
69 .clockid = CLOCK_MONOTONIC,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080070 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080071 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080072 },
John Stultz70a08cc2011-02-15 10:45:16 -080073 {
Thomas Gleixner68fa61c2011-05-20 23:14:04 +020074 .index = HRTIMER_BASE_REALTIME,
75 .clockid = CLOCK_REALTIME,
76 .get_time = &ktime_get_real,
77 .resolution = KTIME_LOW_RES,
78 },
79 {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +020080 .index = HRTIMER_BASE_BOOTTIME,
81 .clockid = CLOCK_BOOTTIME,
John Stultz70a08cc2011-02-15 10:45:16 -080082 .get_time = &ktime_get_boottime,
83 .resolution = KTIME_LOW_RES,
84 },
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080085 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080086};
87
Mike Frysinger942c3c52011-05-02 15:24:27 -040088static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
Thomas Gleixnerce313322011-04-29 00:02:00 +020089 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
90 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
91 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
92};
John Stultze06383d2010-12-14 19:37:07 -080093
94static inline int hrtimer_clockid_to_base(clockid_t clock_id)
95{
96 return hrtimer_clock_to_base_table[clock_id];
97}
98
99
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800100/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800101 * Get the coarse grained time at the softirq based on xtime and
102 * wall_to_monotonic.
103 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800104static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800105{
John Stultz70a08cc2011-02-15 10:45:16 -0800106 ktime_t xtim, mono, boot;
John Stultz314ac372011-02-14 18:43:08 -0800107 struct timespec xts, tom, slp;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800108
John Stultz314ac372011-02-14 18:43:08 -0800109 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800110
john stultzf4304ab2007-02-16 01:27:26 -0800111 xtim = timespec_to_ktime(xts);
John Stultz70a08cc2011-02-15 10:45:16 -0800112 mono = ktime_add(xtim, timespec_to_ktime(tom));
113 boot = ktime_add(mono, timespec_to_ktime(slp));
John Stultze06383d2010-12-14 19:37:07 -0800114 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
John Stultz70a08cc2011-02-15 10:45:16 -0800115 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
116 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800117}
118
119/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800120 * Functions and macros which are different for UP/SMP systems are kept in a
121 * single place
122 */
123#ifdef CONFIG_SMP
124
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800125/*
126 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
127 * means that all timers which are tied to this base via timer->base are
128 * locked, and the base itself is locked too.
129 *
130 * So __run_timers/migrate_timers can safely modify all timers which could
131 * be found on the lists/queues.
132 *
133 * When the timer's base is locked, and the timer removed from list, it is
134 * possible to set timer->base = NULL and drop the lock: the timer remains
135 * locked.
136 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800137static
138struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
139 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800140{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800141 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800142
143 for (;;) {
144 base = timer->base;
145 if (likely(base != NULL)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100146 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800147 if (likely(base == timer->base))
148 return base;
149 /* The timer has migrated to another CPU: */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100150 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800151 }
152 cpu_relax();
153 }
154}
155
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200156
157/*
158 * Get the preferred target CPU for NOHZ
159 */
160static int hrtimer_get_target(int this_cpu, int pinned)
161{
162#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700163 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
164 return get_nohz_timer_target();
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200165#endif
166 return this_cpu;
167}
168
169/*
170 * With HIGHRES=y we do not migrate the timer when it is expiring
171 * before the next event on the target cpu because we cannot reprogram
172 * the target cpu hardware and we would cause it to fire late.
173 *
174 * Called with cpu_base->lock of target cpu held.
175 */
176static int
177hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
178{
179#ifdef CONFIG_HIGH_RES_TIMERS
180 ktime_t expires;
181
182 if (!new_base->cpu_base->hres_active)
183 return 0;
184
185 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
186 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
187#else
188 return 0;
189#endif
190}
191
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800192/*
193 * Switch the timer base to the current CPU when possible.
194 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800195static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530196switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
197 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800198{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800199 struct hrtimer_clock_base *new_base;
200 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200201 int this_cpu = smp_processor_id();
202 int cpu = hrtimer_get_target(this_cpu, pinned);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200203 int basenum = base->index;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530204
205again:
206 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
John Stultze06383d2010-12-14 19:37:07 -0800207 new_base = &new_cpu_base->clock_base[basenum];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800208
209 if (base != new_base) {
210 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200211 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800212 * However we can't change timer's base while it is running,
213 * so we keep it on the same CPU. No hassle vs. reprogramming
214 * the event source in the high resolution case. The softirq
215 * code will take care of this when the timer function has
216 * completed. There is no conflict as we hold the lock until
217 * the timer is enqueued.
218 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800219 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800220 return base;
221
222 /* See the comment in lock_timer_base() */
223 timer->base = NULL;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100224 raw_spin_unlock(&base->cpu_base->lock);
225 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530226
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200227 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
228 cpu = this_cpu;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100229 raw_spin_unlock(&new_base->cpu_base->lock);
230 raw_spin_lock(&base->cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200231 timer->base = base;
232 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530233 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800234 timer->base = new_base;
235 }
236 return new_base;
237}
238
239#else /* CONFIG_SMP */
240
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800241static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800242lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
243{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800244 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800245
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100246 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800247
248 return base;
249}
250
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530251# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800252
253#endif /* !CONFIG_SMP */
254
255/*
256 * Functions for the union type storage format of ktime_t which are
257 * too large for inlining:
258 */
259#if BITS_PER_LONG < 64
260# ifndef CONFIG_KTIME_SCALAR
261/**
262 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800263 * @kt: addend
264 * @nsec: the scalar nsec value to add
265 *
266 * Returns the sum of kt and nsec in ktime_t format
267 */
268ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
269{
270 ktime_t tmp;
271
272 if (likely(nsec < NSEC_PER_SEC)) {
273 tmp.tv64 = nsec;
274 } else {
275 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
276
277 tmp = ktime_set((long)nsec, rem);
278 }
279
280 return ktime_add(kt, tmp);
281}
David Howellsb8b8fd22007-04-27 15:31:24 -0700282
283EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700284
285/**
286 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
287 * @kt: minuend
288 * @nsec: the scalar nsec value to subtract
289 *
290 * Returns the subtraction of @nsec from @kt in ktime_t format
291 */
292ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
293{
294 ktime_t tmp;
295
296 if (likely(nsec < NSEC_PER_SEC)) {
297 tmp.tv64 = nsec;
298 } else {
299 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
300
301 tmp = ktime_set((long)nsec, rem);
302 }
303
304 return ktime_sub(kt, tmp);
305}
306
307EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800308# endif /* !CONFIG_KTIME_SCALAR */
309
310/*
311 * Divide a ktime value by a nanosecond value
312 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800313u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800314{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300315 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800316 int sft = 0;
317
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300318 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800319 /* Make sure the divisor is less than 2^32: */
320 while (div >> 32) {
321 sft++;
322 div >>= 1;
323 }
324 dclc >>= sft;
325 do_div(dclc, (unsigned long) div);
326
Davide Libenzi4d672e72008-02-04 22:27:26 -0800327 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800328}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800329#endif /* BITS_PER_LONG >= 64 */
330
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100331/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100332 * Add two ktime values and do a safety check for overflow:
333 */
334ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
335{
336 ktime_t res = ktime_add(lhs, rhs);
337
338 /*
339 * We use KTIME_SEC_MAX here, the maximum timeout which we can
340 * return to user space in a timespec:
341 */
342 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
343 res = ktime_set(KTIME_SEC_MAX, 0);
344
345 return res;
346}
347
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300348EXPORT_SYMBOL_GPL(ktime_add_safe);
349
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700350#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
351
352static struct debug_obj_descr hrtimer_debug_descr;
353
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100354static void *hrtimer_debug_hint(void *addr)
355{
356 return ((struct hrtimer *) addr)->function;
357}
358
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700359/*
360 * fixup_init is called when:
361 * - an active object is initialized
362 */
363static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
364{
365 struct hrtimer *timer = addr;
366
367 switch (state) {
368 case ODEBUG_STATE_ACTIVE:
369 hrtimer_cancel(timer);
370 debug_object_init(timer, &hrtimer_debug_descr);
371 return 1;
372 default:
373 return 0;
374 }
375}
376
377/*
378 * fixup_activate is called when:
379 * - an active object is activated
380 * - an unknown object is activated (might be a statically initialized object)
381 */
382static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
383{
384 switch (state) {
385
386 case ODEBUG_STATE_NOTAVAILABLE:
387 WARN_ON_ONCE(1);
388 return 0;
389
390 case ODEBUG_STATE_ACTIVE:
391 WARN_ON(1);
392
393 default:
394 return 0;
395 }
396}
397
398/*
399 * fixup_free is called when:
400 * - an active object is freed
401 */
402static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
403{
404 struct hrtimer *timer = addr;
405
406 switch (state) {
407 case ODEBUG_STATE_ACTIVE:
408 hrtimer_cancel(timer);
409 debug_object_free(timer, &hrtimer_debug_descr);
410 return 1;
411 default:
412 return 0;
413 }
414}
415
416static struct debug_obj_descr hrtimer_debug_descr = {
417 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100418 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700419 .fixup_init = hrtimer_fixup_init,
420 .fixup_activate = hrtimer_fixup_activate,
421 .fixup_free = hrtimer_fixup_free,
422};
423
424static inline void debug_hrtimer_init(struct hrtimer *timer)
425{
426 debug_object_init(timer, &hrtimer_debug_descr);
427}
428
429static inline void debug_hrtimer_activate(struct hrtimer *timer)
430{
431 debug_object_activate(timer, &hrtimer_debug_descr);
432}
433
434static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
435{
436 debug_object_deactivate(timer, &hrtimer_debug_descr);
437}
438
439static inline void debug_hrtimer_free(struct hrtimer *timer)
440{
441 debug_object_free(timer, &hrtimer_debug_descr);
442}
443
444static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
445 enum hrtimer_mode mode);
446
447void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
448 enum hrtimer_mode mode)
449{
450 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
451 __hrtimer_init(timer, clock_id, mode);
452}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700453EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700454
455void destroy_hrtimer_on_stack(struct hrtimer *timer)
456{
457 debug_object_free(timer, &hrtimer_debug_descr);
458}
459
460#else
461static inline void debug_hrtimer_init(struct hrtimer *timer) { }
462static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
463static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
464#endif
465
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800466static inline void
467debug_init(struct hrtimer *timer, clockid_t clockid,
468 enum hrtimer_mode mode)
469{
470 debug_hrtimer_init(timer);
471 trace_hrtimer_init(timer, clockid, mode);
472}
473
474static inline void debug_activate(struct hrtimer *timer)
475{
476 debug_hrtimer_activate(timer);
477 trace_hrtimer_start(timer);
478}
479
480static inline void debug_deactivate(struct hrtimer *timer)
481{
482 debug_hrtimer_deactivate(timer);
483 trace_hrtimer_cancel(timer);
484}
485
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800486/* High resolution timer related functions */
487#ifdef CONFIG_HIGH_RES_TIMERS
488
489/*
490 * High resolution timer enabled ?
491 */
492static int hrtimer_hres_enabled __read_mostly = 1;
493
494/*
495 * Enable / Disable high resolution mode
496 */
497static int __init setup_hrtimer_hres(char *str)
498{
499 if (!strcmp(str, "off"))
500 hrtimer_hres_enabled = 0;
501 else if (!strcmp(str, "on"))
502 hrtimer_hres_enabled = 1;
503 else
504 return 0;
505 return 1;
506}
507
508__setup("highres=", setup_hrtimer_hres);
509
510/*
511 * hrtimer_high_res_enabled - query, if the highres mode is enabled
512 */
513static inline int hrtimer_is_hres_enabled(void)
514{
515 return hrtimer_hres_enabled;
516}
517
518/*
519 * Is the high resolution mode active ?
520 */
521static inline int hrtimer_hres_active(void)
522{
Christoph Lameter909ea962010-12-08 16:22:55 +0100523 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800524}
525
526/*
527 * Reprogram the event source with checking both queues for the
528 * next event
529 * Called with interrupts disabled and base->lock held
530 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400531static void
532hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800533{
534 int i;
535 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400536 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800537
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400538 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800539
540 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
541 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700542 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800543
John Stultz998adc32010-09-20 19:19:17 -0700544 next = timerqueue_getnext(&base->active);
545 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800546 continue;
John Stultz998adc32010-09-20 19:19:17 -0700547 timer = container_of(next, struct hrtimer, node);
548
Arjan van de Vencc584b22008-09-01 15:02:30 -0700549 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100550 /*
551 * clock_was_set() has changed base->offset so the
552 * result might be negative. Fix it up to prevent a
553 * false positive in clockevents_program_event()
554 */
555 if (expires.tv64 < 0)
556 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400557 if (expires.tv64 < expires_next.tv64)
558 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800559 }
560
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400561 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
562 return;
563
564 cpu_base->expires_next.tv64 = expires_next.tv64;
565
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800566 if (cpu_base->expires_next.tv64 != KTIME_MAX)
567 tick_program_event(cpu_base->expires_next, 1);
568}
569
570/*
571 * Shared reprogramming for clock_realtime and clock_monotonic
572 *
573 * When a timer is enqueued and expires earlier than the already enqueued
574 * timers, we have to check, whether it expires earlier than the timer for
575 * which the clock event device was armed.
576 *
577 * Called with interrupts disabled and base->cpu_base.lock held
578 */
579static int hrtimer_reprogram(struct hrtimer *timer,
580 struct hrtimer_clock_base *base)
581{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100582 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700583 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800584 int res;
585
Arjan van de Vencc584b22008-09-01 15:02:30 -0700586 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100587
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800588 /*
589 * When the callback is running, we do not reprogram the clock event
590 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200591 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800592 * reprogramming is handled either by the softirq, which called the
593 * callback or at the end of the hrtimer_interrupt.
594 */
595 if (hrtimer_callback_running(timer))
596 return 0;
597
Thomas Gleixner63070a72008-02-14 00:58:36 +0100598 /*
599 * CLOCK_REALTIME timer might be requested with an absolute
600 * expiry time which is less than base->offset. Nothing wrong
601 * about that, just avoid to call into the tick code, which
602 * has now objections against negative expiry values.
603 */
604 if (expires.tv64 < 0)
605 return -ETIME;
606
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100607 if (expires.tv64 >= cpu_base->expires_next.tv64)
608 return 0;
609
610 /*
611 * If a hang was detected in the last timer interrupt then we
612 * do not schedule a timer which is earlier than the expiry
613 * which we enforced in the hang detection. We want the system
614 * to make progress.
615 */
616 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800617 return 0;
618
619 /*
620 * Clockevents returns -ETIME, when the event was in the past.
621 */
622 res = tick_program_event(expires, 0);
623 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100624 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800625 return res;
626}
627
Ingo Molnar995f0542007-04-07 12:05:00 +0200628/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800629 * Initialize the high resolution related parts of cpu_base
630 */
631static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
632{
633 base->expires_next.tv64 = KTIME_MAX;
634 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800635}
636
637/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800638 * When High resolution timers are active, try to reprogram. Note, that in case
639 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
640 * check happens. The timer gets enqueued into the rbtree. The reprogramming
641 * and expiry check is done in the hrtimer_interrupt or in the softirq.
642 */
643static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100644 struct hrtimer_clock_base *base,
645 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800646{
647 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100648 if (wakeup) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100649 raw_spin_unlock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100650 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100651 raw_spin_lock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100652 } else
653 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
654
Peter Zijlstraca109492008-11-25 12:43:51 +0100655 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800656 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100657
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800658 return 0;
659}
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;
665
666 return ktime_get_update_offsets(offs_real, offs_boot);
667}
668
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200669/*
670 * Retrigger next event is called after clock was set
671 *
672 * Called with interrupts disabled via on_each_cpu()
673 */
674static void retrigger_next_event(void *arg)
675{
676 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200677
678 if (!hrtimer_hres_active())
679 return;
680
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200681 raw_spin_lock(&base->lock);
John Stultz5baefd62012-07-10 18:43:25 -0400682 hrtimer_update_base(base);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200683 hrtimer_force_reprogram(base, 0);
684 raw_spin_unlock(&base->lock);
685}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200686
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800687/*
688 * Switch to high resolution mode
689 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800690static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800691{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200692 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700693 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800694 unsigned long flags;
695
696 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800697 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800698
699 local_irq_save(flags);
700
701 if (tick_init_highres()) {
702 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700703 printk(KERN_WARNING "Could not switch to high resolution "
704 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800705 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800706 }
707 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200708 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
709 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800710
711 tick_setup_sched_timer();
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800712 /* "Retrigger" the interrupt to get things going */
713 retrigger_next_event(NULL);
714 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800715 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800716}
717
John Stultzf55a6fa2012-07-10 18:43:19 -0400718/*
719 * Called from timekeeping code to reprogramm the hrtimer interrupt
720 * device. If called from the timer interrupt context we defer it to
721 * softirq context.
722 */
723void clock_was_set_delayed(void)
724{
725 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
726
727 cpu_base->clock_was_set = 1;
728 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
729}
730
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800731#else
732
733static inline int hrtimer_hres_active(void) { return 0; }
734static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800735static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400736static inline void
737hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800738static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100739 struct hrtimer_clock_base *base,
740 int wakeup)
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 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100999 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001000 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001001
1002 unlock_hrtimer_base(timer, &flags);
1003
1004 return ret;
1005}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001006
1007/**
1008 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1009 * @timer: the timer to be added
1010 * @tim: expiry time
1011 * @delta_ns: "slack" range for the timer
1012 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1013 *
1014 * Returns:
1015 * 0 on success
1016 * 1 when the timer was active
1017 */
1018int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1019 unsigned long delta_ns, const enum hrtimer_mode mode)
1020{
1021 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1022}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001023EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1024
1025/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001026 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001027 * @timer: the timer to be added
1028 * @tim: expiry time
1029 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1030 *
1031 * Returns:
1032 * 0 on success
1033 * 1 when the timer was active
1034 */
1035int
1036hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1037{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001038 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001039}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001040EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001041
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001042
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001043/**
1044 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001045 * @timer: hrtimer to stop
1046 *
1047 * Returns:
1048 * 0 when the timer was not active
1049 * 1 when the timer was active
1050 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001051 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001052 */
1053int hrtimer_try_to_cancel(struct hrtimer *timer)
1054{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001055 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001056 unsigned long flags;
1057 int ret = -1;
1058
1059 base = lock_hrtimer_base(timer, &flags);
1060
Thomas Gleixner303e9672007-02-16 01:27:51 -08001061 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001062 ret = remove_hrtimer(timer, base);
1063
1064 unlock_hrtimer_base(timer, &flags);
1065
1066 return ret;
1067
1068}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001069EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001070
1071/**
1072 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001073 * @timer: the timer to be cancelled
1074 *
1075 * Returns:
1076 * 0 when the timer was not active
1077 * 1 when the timer was active
1078 */
1079int hrtimer_cancel(struct hrtimer *timer)
1080{
1081 for (;;) {
1082 int ret = hrtimer_try_to_cancel(timer);
1083
1084 if (ret >= 0)
1085 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001086 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001087 }
1088}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001089EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001090
1091/**
1092 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001093 * @timer: the timer to read
1094 */
1095ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1096{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001097 unsigned long flags;
1098 ktime_t rem;
1099
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001100 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001101 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001102 unlock_hrtimer_base(timer, &flags);
1103
1104 return rem;
1105}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001106EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001107
Russell Kingee9c5782008-04-20 13:59:33 +01001108#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001109/**
1110 * hrtimer_get_next_event - get the time until next expiry event
1111 *
1112 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1113 * is pending.
1114 */
1115ktime_t hrtimer_get_next_event(void)
1116{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001117 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1118 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001119 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1120 unsigned long flags;
1121 int i;
1122
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001123 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001124
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001125 if (!hrtimer_hres_active()) {
1126 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1127 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001128 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001129
John Stultz998adc32010-09-20 19:19:17 -07001130 next = timerqueue_getnext(&base->active);
1131 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001132 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001133
John Stultz998adc32010-09-20 19:19:17 -07001134 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001135 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001136 delta = ktime_sub(delta, base->get_time());
1137 if (delta.tv64 < mindelta.tv64)
1138 mindelta.tv64 = delta.tv64;
1139 }
Tony Lindgren69239742006-03-06 15:42:45 -08001140 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001141
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001142 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001143
Tony Lindgren69239742006-03-06 15:42:45 -08001144 if (mindelta.tv64 < 0)
1145 mindelta.tv64 = 0;
1146 return mindelta;
1147}
1148#endif
1149
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001150static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1151 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001152{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001153 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001154 int base;
George Anzinger7978672c2006-02-01 03:05:11 -08001155
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001156 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001157
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001158 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001159
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001160 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001161 clock_id = CLOCK_MONOTONIC;
1162
John Stultze06383d2010-12-14 19:37:07 -08001163 base = hrtimer_clockid_to_base(clock_id);
1164 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001165 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001166
1167#ifdef CONFIG_TIMER_STATS
1168 timer->start_site = NULL;
1169 timer->start_pid = -1;
1170 memset(timer->start_comm, 0, TASK_COMM_LEN);
1171#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001172}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001173
1174/**
1175 * hrtimer_init - initialize a timer to the given clock
1176 * @timer: the timer to be initialized
1177 * @clock_id: the clock to be used
1178 * @mode: timer mode abs/rel
1179 */
1180void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1181 enum hrtimer_mode mode)
1182{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001183 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001184 __hrtimer_init(timer, clock_id, mode);
1185}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001186EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001187
1188/**
1189 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001190 * @which_clock: which clock to query
1191 * @tp: pointer to timespec variable to store the resolution
1192 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001193 * Store the resolution of the clock selected by @which_clock in the
1194 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001195 */
1196int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1197{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001198 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001199 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001200
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001201 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001202 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001203
1204 return 0;
1205}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001206EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001207
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001208static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001209{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001210 struct hrtimer_clock_base *base = timer->base;
1211 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1212 enum hrtimer_restart (*fn)(struct hrtimer *);
1213 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001214
Peter Zijlstraca109492008-11-25 12:43:51 +01001215 WARN_ON(!irqs_disabled());
1216
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001217 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001218 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1219 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001220 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001221
1222 /*
1223 * Because we run timers from hardirq context, there is no chance
1224 * they get migrated to another cpu, therefore its safe to unlock
1225 * the timer base.
1226 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001227 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001228 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001229 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001230 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001231 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001232
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001233 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001234 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1235 * we do not reprogramm the event hardware. Happens either in
1236 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001237 */
1238 if (restart != HRTIMER_NORESTART) {
1239 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001240 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001241 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001242
1243 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1244
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001245 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001246}
1247
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001248#ifdef CONFIG_HIGH_RES_TIMERS
1249
1250/*
1251 * High resolution timer interrupt
1252 * Called with interrupts disabled
1253 */
1254void hrtimer_interrupt(struct clock_event_device *dev)
1255{
1256 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001257 ktime_t expires_next, now, entry_time, delta;
1258 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001259
1260 BUG_ON(!cpu_base->hres_active);
1261 cpu_base->nr_events++;
1262 dev->next_event.tv64 = KTIME_MAX;
1263
Thomas Gleixner196951e2012-07-10 18:43:23 -04001264 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001265 entry_time = now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001266retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001267 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001268 /*
1269 * We set expires_next to KTIME_MAX here with cpu_base->lock
1270 * held to prevent that a timer is enqueued in our queue via
1271 * the migration code. This does not affect enqueueing of
1272 * timers which run their callback and need to be requeued on
1273 * this CPU.
1274 */
1275 cpu_base->expires_next.tv64 = KTIME_MAX;
1276
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001277 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001278 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001279 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001280 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001281
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001282 if (!(cpu_base->active_bases & (1 << i)))
1283 continue;
1284
1285 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001286 basenow = ktime_add(now, base->offset);
1287
John Stultz998adc32010-09-20 19:19:17 -07001288 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001289 struct hrtimer *timer;
1290
John Stultz998adc32010-09-20 19:19:17 -07001291 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001292
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001293 /*
1294 * The immediate goal for using the softexpires is
1295 * minimizing wakeups, not running timers at the
1296 * earliest interrupt after their soft expiration.
1297 * This allows us to avoid using a Priority Search
1298 * Tree, which can answer a stabbing querry for
1299 * overlapping intervals and instead use the simple
1300 * BST we already have.
1301 * We don't add extra wakeups by delaying timers that
1302 * are right-of a not yet expired timer, because that
1303 * timer will have to trigger a wakeup anyway.
1304 */
1305
1306 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001307 ktime_t expires;
1308
Arjan van de Vencc584b22008-09-01 15:02:30 -07001309 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001310 base->offset);
1311 if (expires.tv64 < expires_next.tv64)
1312 expires_next = expires;
1313 break;
1314 }
1315
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001316 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001317 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001318 }
1319
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001320 /*
1321 * Store the new expiry value so the migration code can verify
1322 * against it.
1323 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001324 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001325 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001326
1327 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001328 if (expires_next.tv64 == KTIME_MAX ||
1329 !tick_program_event(expires_next, 0)) {
1330 cpu_base->hang_detected = 0;
1331 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001332 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001333
1334 /*
1335 * The next timer was already expired due to:
1336 * - tracing
1337 * - long lasting callbacks
1338 * - being scheduled away when running in a VM
1339 *
1340 * We need to prevent that we loop forever in the hrtimer
1341 * interrupt routine. We give it 3 attempts to avoid
1342 * overreacting on some spurious event.
John Stultz5baefd62012-07-10 18:43:25 -04001343 *
1344 * Acquire base lock for updating the offsets and retrieving
1345 * the current time.
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001346 */
Thomas Gleixner196951e2012-07-10 18:43:23 -04001347 raw_spin_lock(&cpu_base->lock);
John Stultz5baefd62012-07-10 18:43:25 -04001348 now = hrtimer_update_base(cpu_base);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001349 cpu_base->nr_retries++;
1350 if (++retries < 3)
1351 goto retry;
1352 /*
1353 * Give the system a chance to do something else than looping
1354 * here. We stored the entry time, so we know exactly how long
1355 * we spent here. We schedule the next event this amount of
1356 * time away.
1357 */
1358 cpu_base->nr_hangs++;
1359 cpu_base->hang_detected = 1;
Thomas Gleixner196951e2012-07-10 18:43:23 -04001360 raw_spin_unlock(&cpu_base->lock);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001361 delta = ktime_sub(now, entry_time);
1362 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1363 cpu_base->max_hang_time = delta;
1364 /*
1365 * Limit it to a sensible value as we enforce a longer
1366 * delay. Give the CPU at least 100ms to catch up.
1367 */
1368 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1369 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1370 else
1371 expires_next = ktime_add(now, delta);
1372 tick_program_event(expires_next, 1);
1373 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1374 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001375}
1376
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001377/*
1378 * local version of hrtimer_peek_ahead_timers() called with interrupts
1379 * disabled.
1380 */
1381static void __hrtimer_peek_ahead_timers(void)
1382{
1383 struct tick_device *td;
1384
1385 if (!hrtimer_hres_active())
1386 return;
1387
1388 td = &__get_cpu_var(tick_cpu_device);
1389 if (td && td->evtdev)
1390 hrtimer_interrupt(td->evtdev);
1391}
1392
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001393/**
1394 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1395 *
1396 * hrtimer_peek_ahead_timers will peek at the timer queue of
1397 * the current cpu and check if there are any timers for which
1398 * the soft expires time has passed. If any such timers exist,
1399 * they are run immediately and then removed from the timer queue.
1400 *
1401 */
1402void hrtimer_peek_ahead_timers(void)
1403{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001404 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001405
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001406 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001407 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001408 local_irq_restore(flags);
1409}
1410
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001411static void run_hrtimer_softirq(struct softirq_action *h)
1412{
John Stultzf55a6fa2012-07-10 18:43:19 -04001413 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1414
1415 if (cpu_base->clock_was_set) {
1416 cpu_base->clock_was_set = 0;
1417 clock_was_set();
1418 }
1419
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001420 hrtimer_peek_ahead_timers();
1421}
1422
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001423#else /* CONFIG_HIGH_RES_TIMERS */
1424
1425static inline void __hrtimer_peek_ahead_timers(void) { }
1426
1427#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001428
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001429/*
1430 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001431 *
1432 * For HRT its the fall back code to run the softirq in the timer
1433 * softirq context in case the hrtimer initialization failed or has
1434 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001435 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001436void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001437{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001438 if (hrtimer_hres_active())
1439 return;
1440
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001441 /*
1442 * This _is_ ugly: We have to check in the softirq context,
1443 * whether we can switch to highres and / or nohz mode. The
1444 * clocksource switch happens in the timer interrupt with
1445 * xtime_lock held. Notification from there only sets the
1446 * check bit in the tick_oneshot code, otherwise we might
1447 * deadlock vs. xtime_lock.
1448 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001449 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001450 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001451}
1452
1453/*
1454 * Called from hardirq context every jiffy
1455 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001456void hrtimer_run_queues(void)
1457{
John Stultz998adc32010-09-20 19:19:17 -07001458 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001459 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001460 struct hrtimer_clock_base *base;
1461 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001462
1463 if (hrtimer_hres_active())
1464 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001465
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001466 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1467 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001468 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001469 continue;
1470
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001471 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001472 hrtimer_get_softirq_time(cpu_base);
1473 gettime = 0;
1474 }
1475
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001476 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001477
John Stultzb007c382010-12-10 22:19:53 -08001478 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001479 struct hrtimer *timer;
1480
John Stultz998adc32010-09-20 19:19:17 -07001481 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001482 if (base->softirq_time.tv64 <=
1483 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001484 break;
1485
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001486 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001487 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001488 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001489 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001490}
1491
1492/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001493 * Sleep related functions:
1494 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001495static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001496{
1497 struct hrtimer_sleeper *t =
1498 container_of(timer, struct hrtimer_sleeper, timer);
1499 struct task_struct *task = t->task;
1500
1501 t->task = NULL;
1502 if (task)
1503 wake_up_process(task);
1504
1505 return HRTIMER_NORESTART;
1506}
1507
Ingo Molnar36c8b582006-07-03 00:25:41 -07001508void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001509{
1510 sl->timer.function = hrtimer_wakeup;
1511 sl->task = task;
1512}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001513EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001514
Thomas Gleixner669d7862006-03-31 02:31:19 -08001515static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001516{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001517 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001518
Roman Zippel432569b2006-03-26 01:38:08 -08001519 do {
1520 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001521 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001522 if (!hrtimer_active(&t->timer))
1523 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001524
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001525 if (likely(t->task))
1526 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001527
Thomas Gleixner669d7862006-03-31 02:31:19 -08001528 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001529 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001530
Thomas Gleixner669d7862006-03-31 02:31:19 -08001531 } while (t->task && !signal_pending(current));
1532
Peter Zijlstra3588a082008-02-01 17:45:13 +01001533 __set_current_state(TASK_RUNNING);
1534
Thomas Gleixner669d7862006-03-31 02:31:19 -08001535 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001536}
1537
Oleg Nesterov080344b2008-02-01 17:29:05 +03001538static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1539{
1540 struct timespec rmt;
1541 ktime_t rem;
1542
Arjan van de Vencc584b22008-09-01 15:02:30 -07001543 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001544 if (rem.tv64 <= 0)
1545 return 0;
1546 rmt = ktime_to_timespec(rem);
1547
1548 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1549 return -EFAULT;
1550
1551 return 1;
1552}
1553
Toyo Abe1711ef32006-09-29 02:00:28 -07001554long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001555{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001556 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001557 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001558 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001559
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001560 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001561 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001562 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001563
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001564 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001565 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001566
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001567 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001568 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001569 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001570 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001571 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001572 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001573
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001574 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001575 ret = -ERESTART_RESTARTBLOCK;
1576out:
1577 destroy_hrtimer_on_stack(&t.timer);
1578 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001579}
1580
Oleg Nesterov080344b2008-02-01 17:29:05 +03001581long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001582 const enum hrtimer_mode mode, const clockid_t clockid)
1583{
1584 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001585 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001586 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001587 unsigned long slack;
1588
1589 slack = current->timer_slack_ns;
1590 if (rt_task(current))
1591 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001592
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001593 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001594 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001595 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001596 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001597
George Anzinger7978672c2006-02-01 03:05:11 -08001598 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001599 if (mode == HRTIMER_MODE_ABS) {
1600 ret = -ERESTARTNOHAND;
1601 goto out;
1602 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001603
Roman Zippel432569b2006-03-26 01:38:08 -08001604 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001605 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001606 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001607 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001608 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001609
1610 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001611 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001612 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001613 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001614 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001615
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001616 ret = -ERESTART_RESTARTBLOCK;
1617out:
1618 destroy_hrtimer_on_stack(&t.timer);
1619 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001620}
1621
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001622SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1623 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001624{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001625 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001626
1627 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1628 return -EFAULT;
1629
1630 if (!timespec_valid(&tu))
1631 return -EINVAL;
1632
Oleg Nesterov080344b2008-02-01 17:29:05 +03001633 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001634}
1635
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001636/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001637 * Functions related to boot-time initialization:
1638 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001639static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001640{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001641 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001642 int i;
1643
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001644 raw_spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001645
John Stultz998adc32010-09-20 19:19:17 -07001646 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001647 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001648 timerqueue_init_head(&cpu_base->clock_base[i].active);
1649 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001650
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001651 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001652}
1653
1654#ifdef CONFIG_HOTPLUG_CPU
1655
Peter Zijlstraca109492008-11-25 12:43:51 +01001656static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001657 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001658{
1659 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001660 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001661
John Stultz998adc32010-09-20 19:19:17 -07001662 while ((node = timerqueue_getnext(&old_base->active))) {
1663 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001664 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001665 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001666
1667 /*
1668 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1669 * timer could be seen as !active and just vanish away
1670 * under us on another CPU
1671 */
1672 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001673 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001674 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001675 * Enqueue the timers on the new cpu. This does not
1676 * reprogram the event device in case the timer
1677 * expires before the earliest on this CPU, but we run
1678 * hrtimer_interrupt after we migrated everything to
1679 * sort out already expired timers and reprogram the
1680 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001681 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001682 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001683
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001684 /* Clear the migration state bit */
1685 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001686 }
1687}
1688
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001689static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001690{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001691 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001692 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001693
Peter Zijlstra37810652008-12-04 11:17:10 +01001694 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001695 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001696
1697 local_irq_disable();
1698 old_base = &per_cpu(hrtimer_bases, scpu);
1699 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001700 /*
1701 * The caller is globally serialized and nobody else
1702 * takes two locks at once, deadlock is not possible.
1703 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001704 raw_spin_lock(&new_base->lock);
1705 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001706
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001707 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001708 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001709 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001710 }
1711
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001712 raw_spin_unlock(&old_base->lock);
1713 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001714
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001715 /* Check, if we got expired work to do */
1716 __hrtimer_peek_ahead_timers();
1717 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001718}
1719
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001720#endif /* CONFIG_HOTPLUG_CPU */
1721
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001722static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001723 unsigned long action, void *hcpu)
1724{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001725 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001726
1727 switch (action) {
1728
1729 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001730 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001731 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001732 break;
1733
1734#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001735 case CPU_DYING:
1736 case CPU_DYING_FROZEN:
1737 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1738 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001739 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001740 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001741 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001742 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001743 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001744 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001745 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001746#endif
1747
1748 default:
1749 break;
1750 }
1751
1752 return NOTIFY_OK;
1753}
1754
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001755static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001756 .notifier_call = hrtimer_cpu_notify,
1757};
1758
1759void __init hrtimers_init(void)
1760{
1761 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1762 (void *)(long)smp_processor_id());
1763 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001764#ifdef CONFIG_HIGH_RES_TIMERS
1765 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1766#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001767}
1768
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001769/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001770 * schedule_hrtimeout_range_clock - sleep until timeout
1771 * @expires: timeout value (ktime_t)
1772 * @delta: slack in expires timeout (ktime_t)
1773 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1774 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1775 */
1776int __sched
1777schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1778 const enum hrtimer_mode mode, int clock)
1779{
1780 struct hrtimer_sleeper t;
1781
1782 /*
1783 * Optimize when a zero timeout value is given. It does not
1784 * matter whether this is an absolute or a relative time.
1785 */
1786 if (expires && !expires->tv64) {
1787 __set_current_state(TASK_RUNNING);
1788 return 0;
1789 }
1790
1791 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001792 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001793 */
1794 if (!expires) {
1795 schedule();
1796 __set_current_state(TASK_RUNNING);
1797 return -EINTR;
1798 }
1799
1800 hrtimer_init_on_stack(&t.timer, clock, mode);
1801 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1802
1803 hrtimer_init_sleeper(&t, current);
1804
1805 hrtimer_start_expires(&t.timer, mode);
1806 if (!hrtimer_active(&t.timer))
1807 t.task = NULL;
1808
1809 if (likely(t.task))
1810 schedule();
1811
1812 hrtimer_cancel(&t.timer);
1813 destroy_hrtimer_on_stack(&t.timer);
1814
1815 __set_current_state(TASK_RUNNING);
1816
1817 return !t.task ? 0 : -EINTR;
1818}
1819
1820/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001821 * schedule_hrtimeout_range - sleep until timeout
1822 * @expires: timeout value (ktime_t)
1823 * @delta: slack in expires timeout (ktime_t)
1824 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1825 *
1826 * Make the current task sleep until the given expiry time has
1827 * elapsed. The routine will return immediately unless
1828 * the current task state has been set (see set_current_state()).
1829 *
1830 * The @delta argument gives the kernel the freedom to schedule the
1831 * actual wakeup to a time that is both power and performance friendly.
1832 * The kernel give the normal best effort behavior for "@expires+@delta",
1833 * but may decide to fire the timer earlier, but no earlier than @expires.
1834 *
1835 * You can set the task state as follows -
1836 *
1837 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1838 * pass before the routine returns.
1839 *
1840 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1841 * delivered to the current task.
1842 *
1843 * The current task state is guaranteed to be TASK_RUNNING when this
1844 * routine returns.
1845 *
1846 * Returns 0 when the timer has expired otherwise -EINTR
1847 */
1848int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001849 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001850{
Carsten Emde351b3f72010-04-02 22:40:19 +02001851 return schedule_hrtimeout_range_clock(expires, delta, mode,
1852 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001853}
1854EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1855
1856/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001857 * schedule_hrtimeout - sleep until timeout
1858 * @expires: timeout value (ktime_t)
1859 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1860 *
1861 * Make the current task sleep until the given expiry time has
1862 * elapsed. The routine will return immediately unless
1863 * the current task state has been set (see set_current_state()).
1864 *
1865 * You can set the task state as follows -
1866 *
1867 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1868 * pass before the routine returns.
1869 *
1870 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1871 * delivered to the current task.
1872 *
1873 * The current task state is guaranteed to be TASK_RUNNING when this
1874 * routine returns.
1875 *
1876 * Returns 0 when the timer has expired otherwise -EINTR
1877 */
1878int __sched schedule_hrtimeout(ktime_t *expires,
1879 const enum hrtimer_mode mode)
1880{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001881 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001882}
1883EXPORT_SYMBOL_GPL(schedule_hrtimeout);