blob: e6a2e358dd78e7dd8603ffeefab58f9900e632e5 [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>
47#include <linux/timer.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080048
49#include <asm/uaccess.h>
50
Xiao Guangrongc6a2a172009-08-10 10:51:23 +080051#include <trace/events/timer.h>
52
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080053/*
54 * The timer bases:
George Anzinger79786722006-02-01 03:05:11 -080055 *
John Stultze06383d2010-12-14 19:37:07 -080056 * There are more clockids then hrtimer bases. Thus, we index
57 * into the timer bases by the hrtimer_base_type enum. When trying
58 * to reach a base using a clockid, hrtimer_clockid_to_base()
59 * is used to convert from clockid to the proper hrtimer_base_type.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080060 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080061DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080062{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080063
Michael Bohand2f23b32013-03-19 11:07:23 -070064 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080065 .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
Michael Bohan05600292013-04-04 11:53:20 -0700227 this_cpu = smp_processor_id();
228
Michael Bohanf62b8ec2013-04-03 13:48:45 -0700229 if (cpu != this_cpu && (hrtimer_check_target(timer, new_base)
230 || !cpu_online(cpu))) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100231 raw_spin_unlock(&new_base->cpu_base->lock);
232 raw_spin_lock(&base->cpu_base->lock);
Michael Bohan05600292013-04-04 11:53:20 -0700233 cpu = smp_processor_id();
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200234 timer->base = base;
235 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530236 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800237 timer->base = new_base;
238 }
239 return new_base;
240}
241
242#else /* CONFIG_SMP */
243
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800244static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800245lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
246{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800247 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800248
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100249 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800250
251 return base;
252}
253
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530254# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800255
256#endif /* !CONFIG_SMP */
257
258/*
259 * Functions for the union type storage format of ktime_t which are
260 * too large for inlining:
261 */
262#if BITS_PER_LONG < 64
263# ifndef CONFIG_KTIME_SCALAR
264/**
265 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800266 * @kt: addend
267 * @nsec: the scalar nsec value to add
268 *
269 * Returns the sum of kt and nsec in ktime_t format
270 */
271ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
272{
273 ktime_t tmp;
274
275 if (likely(nsec < NSEC_PER_SEC)) {
276 tmp.tv64 = nsec;
277 } else {
278 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
279
280 tmp = ktime_set((long)nsec, rem);
281 }
282
283 return ktime_add(kt, tmp);
284}
David Howellsb8b8fd22007-04-27 15:31:24 -0700285
286EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700287
288/**
289 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
290 * @kt: minuend
291 * @nsec: the scalar nsec value to subtract
292 *
293 * Returns the subtraction of @nsec from @kt in ktime_t format
294 */
295ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
296{
297 ktime_t tmp;
298
299 if (likely(nsec < NSEC_PER_SEC)) {
300 tmp.tv64 = nsec;
301 } else {
302 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
303
304 tmp = ktime_set((long)nsec, rem);
305 }
306
307 return ktime_sub(kt, tmp);
308}
309
310EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800311# endif /* !CONFIG_KTIME_SCALAR */
312
313/*
314 * Divide a ktime value by a nanosecond value
315 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800316u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800317{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300318 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800319 int sft = 0;
320
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300321 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800322 /* Make sure the divisor is less than 2^32: */
323 while (div >> 32) {
324 sft++;
325 div >>= 1;
326 }
327 dclc >>= sft;
328 do_div(dclc, (unsigned long) div);
329
Davide Libenzi4d672e72008-02-04 22:27:26 -0800330 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800331}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800332#endif /* BITS_PER_LONG >= 64 */
333
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100334/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100335 * Add two ktime values and do a safety check for overflow:
336 */
337ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
338{
339 ktime_t res = ktime_add(lhs, rhs);
340
341 /*
342 * We use KTIME_SEC_MAX here, the maximum timeout which we can
343 * return to user space in a timespec:
344 */
345 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
346 res = ktime_set(KTIME_SEC_MAX, 0);
347
348 return res;
349}
350
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300351EXPORT_SYMBOL_GPL(ktime_add_safe);
352
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700353#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
354
355static struct debug_obj_descr hrtimer_debug_descr;
356
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100357static void *hrtimer_debug_hint(void *addr)
358{
359 return ((struct hrtimer *) addr)->function;
360}
361
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700362/*
363 * fixup_init is called when:
364 * - an active object is initialized
365 */
366static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
367{
368 struct hrtimer *timer = addr;
369
370 switch (state) {
371 case ODEBUG_STATE_ACTIVE:
372 hrtimer_cancel(timer);
373 debug_object_init(timer, &hrtimer_debug_descr);
374 return 1;
375 default:
376 return 0;
377 }
378}
379
380/*
381 * fixup_activate is called when:
382 * - an active object is activated
383 * - an unknown object is activated (might be a statically initialized object)
384 */
385static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
386{
387 switch (state) {
388
389 case ODEBUG_STATE_NOTAVAILABLE:
390 WARN_ON_ONCE(1);
391 return 0;
392
393 case ODEBUG_STATE_ACTIVE:
394 WARN_ON(1);
395
396 default:
397 return 0;
398 }
399}
400
401/*
402 * fixup_free is called when:
403 * - an active object is freed
404 */
405static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
406{
407 struct hrtimer *timer = addr;
408
409 switch (state) {
410 case ODEBUG_STATE_ACTIVE:
411 hrtimer_cancel(timer);
412 debug_object_free(timer, &hrtimer_debug_descr);
413 return 1;
414 default:
415 return 0;
416 }
417}
418
419static struct debug_obj_descr hrtimer_debug_descr = {
420 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100421 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700422 .fixup_init = hrtimer_fixup_init,
423 .fixup_activate = hrtimer_fixup_activate,
424 .fixup_free = hrtimer_fixup_free,
425};
426
427static inline void debug_hrtimer_init(struct hrtimer *timer)
428{
429 debug_object_init(timer, &hrtimer_debug_descr);
430}
431
432static inline void debug_hrtimer_activate(struct hrtimer *timer)
433{
434 debug_object_activate(timer, &hrtimer_debug_descr);
435}
436
437static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
438{
439 debug_object_deactivate(timer, &hrtimer_debug_descr);
440}
441
442static inline void debug_hrtimer_free(struct hrtimer *timer)
443{
444 debug_object_free(timer, &hrtimer_debug_descr);
445}
446
447static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
448 enum hrtimer_mode mode);
449
450void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
451 enum hrtimer_mode mode)
452{
453 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
454 __hrtimer_init(timer, clock_id, mode);
455}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700456EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700457
458void destroy_hrtimer_on_stack(struct hrtimer *timer)
459{
460 debug_object_free(timer, &hrtimer_debug_descr);
461}
462
463#else
464static inline void debug_hrtimer_init(struct hrtimer *timer) { }
465static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
466static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
467#endif
468
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800469static inline void
470debug_init(struct hrtimer *timer, clockid_t clockid,
471 enum hrtimer_mode mode)
472{
473 debug_hrtimer_init(timer);
474 trace_hrtimer_init(timer, clockid, mode);
475}
476
477static inline void debug_activate(struct hrtimer *timer)
478{
479 debug_hrtimer_activate(timer);
480 trace_hrtimer_start(timer);
481}
482
483static inline void debug_deactivate(struct hrtimer *timer)
484{
485 debug_hrtimer_deactivate(timer);
486 trace_hrtimer_cancel(timer);
487}
488
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800489/* High resolution timer related functions */
490#ifdef CONFIG_HIGH_RES_TIMERS
491
492/*
493 * High resolution timer enabled ?
494 */
495static int hrtimer_hres_enabled __read_mostly = 1;
496
497/*
498 * Enable / Disable high resolution mode
499 */
500static int __init setup_hrtimer_hres(char *str)
501{
502 if (!strcmp(str, "off"))
503 hrtimer_hres_enabled = 0;
504 else if (!strcmp(str, "on"))
505 hrtimer_hres_enabled = 1;
506 else
507 return 0;
508 return 1;
509}
510
511__setup("highres=", setup_hrtimer_hres);
512
513/*
514 * hrtimer_high_res_enabled - query, if the highres mode is enabled
515 */
516static inline int hrtimer_is_hres_enabled(void)
517{
518 return hrtimer_hres_enabled;
519}
520
521/*
522 * Is the high resolution mode active ?
523 */
524static inline int hrtimer_hres_active(void)
525{
Christoph Lameter909ea962010-12-08 16:22:55 +0100526 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800527}
528
529/*
530 * Reprogram the event source with checking both queues for the
531 * next event
532 * Called with interrupts disabled and base->lock held
533 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400534static void
535hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800536{
537 int i;
538 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400539 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800540
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400541 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800542
543 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
544 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700545 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800546
John Stultz998adc32010-09-20 19:19:17 -0700547 next = timerqueue_getnext(&base->active);
548 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800549 continue;
John Stultz998adc32010-09-20 19:19:17 -0700550 timer = container_of(next, struct hrtimer, node);
551
Arjan van de Vencc584b22008-09-01 15:02:30 -0700552 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100553 /*
554 * clock_was_set() has changed base->offset so the
555 * result might be negative. Fix it up to prevent a
556 * false positive in clockevents_program_event()
557 */
558 if (expires.tv64 < 0)
559 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400560 if (expires.tv64 < expires_next.tv64)
561 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800562 }
563
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400564 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
565 return;
566
567 cpu_base->expires_next.tv64 = expires_next.tv64;
568
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800569 if (cpu_base->expires_next.tv64 != KTIME_MAX)
570 tick_program_event(cpu_base->expires_next, 1);
571}
572
573/*
574 * Shared reprogramming for clock_realtime and clock_monotonic
575 *
576 * When a timer is enqueued and expires earlier than the already enqueued
577 * timers, we have to check, whether it expires earlier than the timer for
578 * which the clock event device was armed.
579 *
580 * Called with interrupts disabled and base->cpu_base.lock held
581 */
582static int hrtimer_reprogram(struct hrtimer *timer,
583 struct hrtimer_clock_base *base)
584{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100585 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700586 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800587 int res;
588
Arjan van de Vencc584b22008-09-01 15:02:30 -0700589 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100590
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800591 /*
592 * When the callback is running, we do not reprogram the clock event
593 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200594 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800595 * reprogramming is handled either by the softirq, which called the
596 * callback or at the end of the hrtimer_interrupt.
597 */
598 if (hrtimer_callback_running(timer))
599 return 0;
600
Thomas Gleixner63070a72008-02-14 00:58:36 +0100601 /*
602 * CLOCK_REALTIME timer might be requested with an absolute
603 * expiry time which is less than base->offset. Nothing wrong
604 * about that, just avoid to call into the tick code, which
605 * has now objections against negative expiry values.
606 */
607 if (expires.tv64 < 0)
608 return -ETIME;
609
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100610 if (expires.tv64 >= cpu_base->expires_next.tv64)
611 return 0;
612
613 /*
614 * If a hang was detected in the last timer interrupt then we
615 * do not schedule a timer which is earlier than the expiry
616 * which we enforced in the hang detection. We want the system
617 * to make progress.
618 */
619 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800620 return 0;
621
622 /*
623 * Clockevents returns -ETIME, when the event was in the past.
624 */
625 res = tick_program_event(expires, 0);
626 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100627 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800628 return res;
629}
630
Ingo Molnar995f0542007-04-07 12:05:00 +0200631/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800632 * Initialize the high resolution related parts of cpu_base
633 */
634static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
635{
636 base->expires_next.tv64 = KTIME_MAX;
637 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800638}
639
640/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800641 * When High resolution timers are active, try to reprogram. Note, that in case
642 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
643 * check happens. The timer gets enqueued into the rbtree. The reprogramming
644 * and expiry check is done in the hrtimer_interrupt or in the softirq.
645 */
646static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzabf04782013-02-04 14:33:37 +0200647 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800648{
Leonid Shatzabf04782013-02-04 14:33:37 +0200649 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800650}
651
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200652/*
653 * Retrigger next event is called after clock was set
654 *
655 * Called with interrupts disabled via on_each_cpu()
656 */
657static void retrigger_next_event(void *arg)
658{
659 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
660 struct timespec realtime_offset, xtim, wtm, sleep;
661
662 if (!hrtimer_hres_active())
663 return;
664
665 /* Optimized out for !HIGH_RES */
666 get_xtime_and_monotonic_and_sleep_offset(&xtim, &wtm, &sleep);
667 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
668
669 /* Adjust CLOCK_REALTIME offset */
670 raw_spin_lock(&base->lock);
671 base->clock_base[HRTIMER_BASE_REALTIME].offset =
672 timespec_to_ktime(realtime_offset);
673 base->clock_base[HRTIMER_BASE_BOOTTIME].offset =
674 timespec_to_ktime(sleep);
675
676 hrtimer_force_reprogram(base, 0);
677 raw_spin_unlock(&base->lock);
678}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200679
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800680/*
681 * Switch to high resolution mode
682 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800683static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800684{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200685 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700686 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800687 unsigned long flags;
688
689 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800690 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800691
692 local_irq_save(flags);
693
694 if (tick_init_highres()) {
695 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700696 printk(KERN_WARNING "Could not switch to high resolution "
697 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800698 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800699 }
700 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200701 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
702 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800703
704 tick_setup_sched_timer();
705
706 /* "Retrigger" the interrupt to get things going */
707 retrigger_next_event(NULL);
708 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800709 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800710}
711
712#else
713
714static inline int hrtimer_hres_active(void) { return 0; }
715static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800716static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400717static inline void
718hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800719static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzabf04782013-02-04 14:33:37 +0200720 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800721{
722 return 0;
723}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800724static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200725static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800726
727#endif /* CONFIG_HIGH_RES_TIMERS */
728
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200729/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200730 * Clock realtime was set
731 *
732 * Change the offset of the realtime clock vs. the monotonic
733 * clock.
734 *
735 * We might have to reprogram the high resolution timer interrupt. On
736 * SMP we call the architecture specific code to retrigger _all_ high
737 * resolution timer interrupts. On UP we just disable interrupts and
738 * call the high resolution interrupt code.
739 */
740void clock_was_set(void)
741{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200742#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200743 /* Retrigger the CPU local events everywhere */
744 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200745#endif
746 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200747}
748
749/*
750 * During resume we might have to reprogram the high resolution timer
751 * interrupt (on the local CPU):
752 */
753void hrtimers_resume(void)
754{
755 WARN_ONCE(!irqs_disabled(),
756 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
757
758 retrigger_next_event(NULL);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200759 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200760}
761
Heiko Carstens5f201902009-12-10 10:56:29 +0100762static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800763{
Heiko Carstens5f201902009-12-10 10:56:29 +0100764#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800765 if (timer->start_site)
766 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100767 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800768 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
769 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800770#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100771}
772
773static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
774{
775#ifdef CONFIG_TIMER_STATS
776 timer->start_site = NULL;
777#endif
778}
779
780static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
781{
782#ifdef CONFIG_TIMER_STATS
783 if (likely(!timer_stats_active))
784 return;
785 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
786 timer->function, timer->start_comm, 0);
787#endif
788}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800789
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800790/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200791 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800792 */
793static inline
794void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
795{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100796 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800797}
798
799/**
800 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800801 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800802 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800803 * @interval: the interval to forward
804 *
805 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700806 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800807 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800808u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800809{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800810 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800811 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812
Arjan van de Vencc584b22008-09-01 15:02:30 -0700813 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800814
815 if (delta.tv64 < 0)
816 return 0;
817
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100818 if (interval.tv64 < timer->base->resolution.tv64)
819 interval.tv64 = timer->base->resolution.tv64;
820
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800822 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823
824 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700825 hrtimer_add_expires_ns(timer, incr * orun);
826 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800827 return orun;
828 /*
829 * This (and the ktime_add() below) is the
830 * correction for exact:
831 */
832 orun++;
833 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700834 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800835
836 return orun;
837}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700838EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800839
840/*
841 * enqueue_hrtimer - internal function to (re)start a timer
842 *
843 * The timer is inserted in expiry order. Insertion into the
844 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100845 *
846 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800847 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100848static int enqueue_hrtimer(struct hrtimer *timer,
849 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800850{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800851 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700852
John Stultz998adc32010-09-20 19:19:17 -0700853 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200854 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800855
856 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800857 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
858 * state of a possibly running callback.
859 */
860 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100861
John Stultz998adc32010-09-20 19:19:17 -0700862 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100863}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800864
865/*
866 * __remove_hrtimer - internal function to remove a timer
867 *
868 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800869 *
870 * High resolution timer mode reprograms the clock event device when the
871 * timer is the one which expires next. The caller can disable this by setting
872 * reprogram to zero. This is useful, when the context does a reprogramming
873 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800874 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800875static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800876 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800877 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800878{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800879 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400880 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
881 goto out;
882
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800883 next_timer = timerqueue_getnext(&base->active);
884 timerqueue_del(&base->active, &timer->node);
885 if (&timer->node == next_timer) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400886#ifdef CONFIG_HIGH_RES_TIMERS
887 /* Reprogram the clock event device. if enabled */
888 if (reprogram && hrtimer_hres_active()) {
889 ktime_t expires;
890
891 expires = ktime_sub(hrtimer_get_expires(timer),
892 base->offset);
893 if (base->cpu_base->expires_next.tv64 == expires.tv64)
894 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800895 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400896#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800897 }
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200898 if (!timerqueue_getnext(&base->active))
899 base->cpu_base->active_bases &= ~(1 << base->index);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400900out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800901 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800902}
903
904/*
905 * remove hrtimer, called with base lock held
906 */
907static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800908remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800909{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800910 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700911 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800912 int reprogram;
913
914 /*
915 * Remove the timer and force reprogramming when high
916 * resolution mode is active and the timer is on the current
917 * CPU. If we remove a timer on another CPU, reprogramming is
918 * skipped. The interrupt event on this CPU is fired and
919 * reprogramming happens in the interrupt handler. This is a
920 * rare case and less expensive than a smp call.
921 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800922 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800923 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800924 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700925 /*
926 * We must preserve the CALLBACK state flag here,
927 * otherwise we could move the timer base in
928 * switch_hrtimer_base.
929 */
930 state = timer->state & HRTIMER_STATE_CALLBACK;
931 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800932 return 1;
933 }
934 return 0;
935}
936
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100937int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
938 unsigned long delta_ns, const enum hrtimer_mode mode,
939 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800940{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800941 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800942 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100943 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800944
945 base = lock_hrtimer_base(timer, &flags);
946
947 /* Remove an active timer from the queue: */
948 ret = remove_hrtimer(timer, base);
949
950 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530951 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800952
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530953 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100954 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800955 /*
956 * CONFIG_TIME_LOW_RES is a temporary way for architectures
957 * to signal that they simply return xtime in
958 * do_gettimeoffset(). In this case we want to round up by
959 * resolution when starting a relative timer, to avoid short
960 * timeouts. This will go away with the GTOD framework.
961 */
962#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100963 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800964#endif
965 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700966
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700967 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800968
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800969 timer_stats_hrtimer_set_start_info(timer);
970
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100971 leftmost = enqueue_hrtimer(timer, new_base);
972
Ingo Molnar935c6312007-03-28 13:17:18 +0200973 /*
974 * Only allow reprogramming if the new base is on this CPU.
975 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100976 *
977 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200978 */
Leonid Shatzabf04782013-02-04 14:33:37 +0200979 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
980 && hrtimer_enqueue_reprogram(timer, new_base)) {
981 if (wakeup) {
982 /*
983 * We need to drop cpu_base->lock to avoid a
984 * lock ordering issue vs. rq->lock.
985 */
986 raw_spin_unlock(&new_base->cpu_base->lock);
987 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
988 local_irq_restore(flags);
989 return ret;
990 } else {
991 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
992 }
993 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800994
995 unlock_hrtimer_base(timer, &flags);
996
997 return ret;
998}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100999
1000/**
1001 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1002 * @timer: the timer to be added
1003 * @tim: expiry time
1004 * @delta_ns: "slack" range for the timer
1005 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1006 *
1007 * Returns:
1008 * 0 on success
1009 * 1 when the timer was active
1010 */
1011int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1012 unsigned long delta_ns, const enum hrtimer_mode mode)
1013{
1014 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1015}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001016EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1017
1018/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001019 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001020 * @timer: the timer to be added
1021 * @tim: expiry time
1022 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1023 *
1024 * Returns:
1025 * 0 on success
1026 * 1 when the timer was active
1027 */
1028int
1029hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1030{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001031 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001032}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001033EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001034
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001035
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001036/**
1037 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001038 * @timer: hrtimer to stop
1039 *
1040 * Returns:
1041 * 0 when the timer was not active
1042 * 1 when the timer was active
1043 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001044 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001045 */
1046int hrtimer_try_to_cancel(struct hrtimer *timer)
1047{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001048 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001049 unsigned long flags;
1050 int ret = -1;
1051
1052 base = lock_hrtimer_base(timer, &flags);
1053
Thomas Gleixner303e9672007-02-16 01:27:51 -08001054 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001055 ret = remove_hrtimer(timer, base);
1056
1057 unlock_hrtimer_base(timer, &flags);
1058
1059 return ret;
1060
1061}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001062EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001063
1064/**
1065 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001066 * @timer: the timer to be cancelled
1067 *
1068 * Returns:
1069 * 0 when the timer was not active
1070 * 1 when the timer was active
1071 */
1072int hrtimer_cancel(struct hrtimer *timer)
1073{
1074 for (;;) {
1075 int ret = hrtimer_try_to_cancel(timer);
1076
1077 if (ret >= 0)
1078 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001079 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001080 }
1081}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001082EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001083
1084/**
1085 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001086 * @timer: the timer to read
1087 */
1088ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1089{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001090 unsigned long flags;
1091 ktime_t rem;
1092
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001093 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001094 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001095 unlock_hrtimer_base(timer, &flags);
1096
1097 return rem;
1098}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001099EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001100
Russell Kingee9c5782008-04-20 13:59:33 +01001101#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001102/**
1103 * hrtimer_get_next_event - get the time until next expiry event
1104 *
1105 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1106 * is pending.
1107 */
1108ktime_t hrtimer_get_next_event(void)
1109{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001110 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1111 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001112 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1113 unsigned long flags;
1114 int i;
1115
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001116 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001117
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001118 if (!hrtimer_hres_active()) {
1119 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1120 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001121 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001122
John Stultz998adc32010-09-20 19:19:17 -07001123 next = timerqueue_getnext(&base->active);
1124 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001125 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001126
John Stultz998adc32010-09-20 19:19:17 -07001127 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001128 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001129 delta = ktime_sub(delta, base->get_time());
1130 if (delta.tv64 < mindelta.tv64)
1131 mindelta.tv64 = delta.tv64;
1132 }
Tony Lindgren69239742006-03-06 15:42:45 -08001133 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001134
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001135 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001136
Tony Lindgren69239742006-03-06 15:42:45 -08001137 if (mindelta.tv64 < 0)
1138 mindelta.tv64 = 0;
1139 return mindelta;
1140}
1141#endif
1142
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001143static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1144 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001145{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001146 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001147 int base;
George Anzinger79786722006-02-01 03:05:11 -08001148
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001149 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001150
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001151 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001152
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001153 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001154 clock_id = CLOCK_MONOTONIC;
1155
John Stultze06383d2010-12-14 19:37:07 -08001156 base = hrtimer_clockid_to_base(clock_id);
1157 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001158 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001159
1160#ifdef CONFIG_TIMER_STATS
1161 timer->start_site = NULL;
1162 timer->start_pid = -1;
1163 memset(timer->start_comm, 0, TASK_COMM_LEN);
1164#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001165}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001166
1167/**
1168 * hrtimer_init - initialize a timer to the given clock
1169 * @timer: the timer to be initialized
1170 * @clock_id: the clock to be used
1171 * @mode: timer mode abs/rel
1172 */
1173void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1174 enum hrtimer_mode mode)
1175{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001176 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001177 __hrtimer_init(timer, clock_id, mode);
1178}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001179EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001180
1181/**
1182 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001183 * @which_clock: which clock to query
1184 * @tp: pointer to timespec variable to store the resolution
1185 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001186 * Store the resolution of the clock selected by @which_clock in the
1187 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001188 */
1189int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1190{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001191 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001192 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001193
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001194 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001195 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001196
1197 return 0;
1198}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001199EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001200
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001201static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001202{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001203 struct hrtimer_clock_base *base = timer->base;
1204 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1205 enum hrtimer_restart (*fn)(struct hrtimer *);
1206 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001207
Peter Zijlstraca109492008-11-25 12:43:51 +01001208 WARN_ON(!irqs_disabled());
1209
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001210 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001211 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1212 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001213 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001214
1215 /*
1216 * Because we run timers from hardirq context, there is no chance
1217 * they get migrated to another cpu, therefore its safe to unlock
1218 * the timer base.
1219 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001220 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001221 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001222 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001223 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001224 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001225
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001226 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001227 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1228 * we do not reprogramm the event hardware. Happens either in
1229 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001230 */
1231 if (restart != HRTIMER_NORESTART) {
1232 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001233 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001234 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001235
1236 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1237
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001238 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001239}
1240
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001241#ifdef CONFIG_HIGH_RES_TIMERS
1242
1243/*
1244 * High resolution timer interrupt
1245 * Called with interrupts disabled
1246 */
1247void hrtimer_interrupt(struct clock_event_device *dev)
1248{
1249 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001250 ktime_t expires_next, now, entry_time, delta;
1251 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001252
1253 BUG_ON(!cpu_base->hres_active);
1254 cpu_base->nr_events++;
1255 dev->next_event.tv64 = KTIME_MAX;
1256
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001257 entry_time = now = ktime_get();
1258retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001259 expires_next.tv64 = KTIME_MAX;
1260
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001261 raw_spin_lock(&cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001262 /*
1263 * We set expires_next to KTIME_MAX here with cpu_base->lock
1264 * held to prevent that a timer is enqueued in our queue via
1265 * the migration code. This does not affect enqueueing of
1266 * timers which run their callback and need to be requeued on
1267 * this CPU.
1268 */
1269 cpu_base->expires_next.tv64 = KTIME_MAX;
1270
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001271 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001272 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001273 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001274 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001275
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001276 if (!(cpu_base->active_bases & (1 << i)))
1277 continue;
1278
1279 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001280 basenow = ktime_add(now, base->offset);
1281
John Stultz998adc32010-09-20 19:19:17 -07001282 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001283 struct hrtimer *timer;
1284
John Stultz998adc32010-09-20 19:19:17 -07001285 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001286
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001287 /*
1288 * The immediate goal for using the softexpires is
1289 * minimizing wakeups, not running timers at the
1290 * earliest interrupt after their soft expiration.
1291 * This allows us to avoid using a Priority Search
1292 * Tree, which can answer a stabbing querry for
1293 * overlapping intervals and instead use the simple
1294 * BST we already have.
1295 * We don't add extra wakeups by delaying timers that
1296 * are right-of a not yet expired timer, because that
1297 * timer will have to trigger a wakeup anyway.
1298 */
1299
1300 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001301 ktime_t expires;
1302
Arjan van de Vencc584b22008-09-01 15:02:30 -07001303 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001304 base->offset);
1305 if (expires.tv64 < expires_next.tv64)
1306 expires_next = expires;
1307 break;
1308 }
1309
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001310 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001311 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001312 }
1313
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001314 /*
1315 * Store the new expiry value so the migration code can verify
1316 * against it.
1317 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001318 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001319 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001320
1321 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001322 if (expires_next.tv64 == KTIME_MAX ||
1323 !tick_program_event(expires_next, 0)) {
1324 cpu_base->hang_detected = 0;
1325 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001326 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001327
1328 /*
1329 * The next timer was already expired due to:
1330 * - tracing
1331 * - long lasting callbacks
1332 * - being scheduled away when running in a VM
1333 *
1334 * We need to prevent that we loop forever in the hrtimer
1335 * interrupt routine. We give it 3 attempts to avoid
1336 * overreacting on some spurious event.
1337 */
1338 now = ktime_get();
1339 cpu_base->nr_retries++;
1340 if (++retries < 3)
1341 goto retry;
1342 /*
1343 * Give the system a chance to do something else than looping
1344 * here. We stored the entry time, so we know exactly how long
1345 * we spent here. We schedule the next event this amount of
1346 * time away.
1347 */
1348 cpu_base->nr_hangs++;
1349 cpu_base->hang_detected = 1;
1350 delta = ktime_sub(now, entry_time);
1351 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1352 cpu_base->max_hang_time = delta;
1353 /*
1354 * Limit it to a sensible value as we enforce a longer
1355 * delay. Give the CPU at least 100ms to catch up.
1356 */
1357 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1358 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1359 else
1360 expires_next = ktime_add(now, delta);
1361 tick_program_event(expires_next, 1);
1362 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1363 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001364}
1365
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001366/*
1367 * local version of hrtimer_peek_ahead_timers() called with interrupts
1368 * disabled.
1369 */
1370static void __hrtimer_peek_ahead_timers(void)
1371{
1372 struct tick_device *td;
1373
1374 if (!hrtimer_hres_active())
1375 return;
1376
1377 td = &__get_cpu_var(tick_cpu_device);
1378 if (td && td->evtdev)
1379 hrtimer_interrupt(td->evtdev);
1380}
1381
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001382/**
1383 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1384 *
1385 * hrtimer_peek_ahead_timers will peek at the timer queue of
1386 * the current cpu and check if there are any timers for which
1387 * the soft expires time has passed. If any such timers exist,
1388 * they are run immediately and then removed from the timer queue.
1389 *
1390 */
1391void hrtimer_peek_ahead_timers(void)
1392{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001393 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001394
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001395 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001396 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001397 local_irq_restore(flags);
1398}
1399
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001400static void run_hrtimer_softirq(struct softirq_action *h)
1401{
1402 hrtimer_peek_ahead_timers();
1403}
1404
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001405#else /* CONFIG_HIGH_RES_TIMERS */
1406
1407static inline void __hrtimer_peek_ahead_timers(void) { }
1408
1409#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001410
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001411/*
1412 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001413 *
1414 * For HRT its the fall back code to run the softirq in the timer
1415 * softirq context in case the hrtimer initialization failed or has
1416 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001417 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001418void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001419{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001420 if (hrtimer_hres_active())
1421 return;
1422
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001423 /*
1424 * This _is_ ugly: We have to check in the softirq context,
1425 * whether we can switch to highres and / or nohz mode. The
1426 * clocksource switch happens in the timer interrupt with
1427 * xtime_lock held. Notification from there only sets the
1428 * check bit in the tick_oneshot code, otherwise we might
1429 * deadlock vs. xtime_lock.
1430 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001431 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001432 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001433}
1434
1435/*
1436 * Called from hardirq context every jiffy
1437 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001438void hrtimer_run_queues(void)
1439{
John Stultz998adc32010-09-20 19:19:17 -07001440 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001441 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001442 struct hrtimer_clock_base *base;
1443 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001444
1445 if (hrtimer_hres_active())
1446 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001447
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001448 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1449 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001450 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001451 continue;
1452
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001453 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001454 hrtimer_get_softirq_time(cpu_base);
1455 gettime = 0;
1456 }
1457
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001458 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001459
John Stultzb007c382010-12-10 22:19:53 -08001460 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001461 struct hrtimer *timer;
1462
John Stultz998adc32010-09-20 19:19:17 -07001463 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001464 if (base->softirq_time.tv64 <=
1465 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001466 break;
1467
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001468 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001469 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001470 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001471 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001472}
1473
1474/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001475 * Sleep related functions:
1476 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001477static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001478{
1479 struct hrtimer_sleeper *t =
1480 container_of(timer, struct hrtimer_sleeper, timer);
1481 struct task_struct *task = t->task;
1482
1483 t->task = NULL;
1484 if (task)
1485 wake_up_process(task);
1486
1487 return HRTIMER_NORESTART;
1488}
1489
Ingo Molnar36c8b582006-07-03 00:25:41 -07001490void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001491{
1492 sl->timer.function = hrtimer_wakeup;
1493 sl->task = task;
1494}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001495EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001496
Thomas Gleixner669d7862006-03-31 02:31:19 -08001497static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001498{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001499 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001500
Roman Zippel432569b2006-03-26 01:38:08 -08001501 do {
1502 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001503 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001504 if (!hrtimer_active(&t->timer))
1505 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001506
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001507 if (likely(t->task))
1508 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001509
Thomas Gleixner669d7862006-03-31 02:31:19 -08001510 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001511 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001512
Thomas Gleixner669d7862006-03-31 02:31:19 -08001513 } while (t->task && !signal_pending(current));
1514
Peter Zijlstra3588a082008-02-01 17:45:13 +01001515 __set_current_state(TASK_RUNNING);
1516
Thomas Gleixner669d7862006-03-31 02:31:19 -08001517 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001518}
1519
Oleg Nesterov080344b2008-02-01 17:29:05 +03001520static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1521{
1522 struct timespec rmt;
1523 ktime_t rem;
1524
Arjan van de Vencc584b22008-09-01 15:02:30 -07001525 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001526 if (rem.tv64 <= 0)
1527 return 0;
1528 rmt = ktime_to_timespec(rem);
1529
1530 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1531 return -EFAULT;
1532
1533 return 1;
1534}
1535
Toyo Abe1711ef32006-09-29 02:00:28 -07001536long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001537{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001538 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001539 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001540 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001541
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001542 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001543 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001544 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001545
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001546 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001547 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001548
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001549 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001550 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001551 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001552 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001553 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001554 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001555
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001556 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001557 ret = -ERESTART_RESTARTBLOCK;
1558out:
1559 destroy_hrtimer_on_stack(&t.timer);
1560 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001561}
1562
Oleg Nesterov080344b2008-02-01 17:29:05 +03001563long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001564 const enum hrtimer_mode mode, const clockid_t clockid)
1565{
1566 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001567 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001568 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001569 unsigned long slack;
1570
1571 slack = current->timer_slack_ns;
1572 if (rt_task(current))
1573 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001574
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001575 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001576 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001577 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001578 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001579
George Anzinger79786722006-02-01 03:05:11 -08001580 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001581 if (mode == HRTIMER_MODE_ABS) {
1582 ret = -ERESTARTNOHAND;
1583 goto out;
1584 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001585
Roman Zippel432569b2006-03-26 01:38:08 -08001586 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001587 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001588 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001589 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001590 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001591
1592 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001593 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001594 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001595 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001596 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001597
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001598 ret = -ERESTART_RESTARTBLOCK;
1599out:
1600 destroy_hrtimer_on_stack(&t.timer);
1601 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001602}
1603
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001604SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1605 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001606{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001607 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001608
1609 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1610 return -EFAULT;
1611
1612 if (!timespec_valid(&tu))
1613 return -EINVAL;
1614
Oleg Nesterov080344b2008-02-01 17:29:05 +03001615 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001616}
1617
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001618/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001619 * Functions related to boot-time initialization:
1620 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001621static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001622{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001623 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001624 int i;
1625
John Stultz998adc32010-09-20 19:19:17 -07001626 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001627 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001628 timerqueue_init_head(&cpu_base->clock_base[i].active);
1629 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001630
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001631 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001632}
1633
1634#ifdef CONFIG_HOTPLUG_CPU
1635
Peter Zijlstraca109492008-11-25 12:43:51 +01001636static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001637 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001638{
1639 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001640 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001641
John Stultz998adc32010-09-20 19:19:17 -07001642 while ((node = timerqueue_getnext(&old_base->active))) {
1643 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001644 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001645 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001646
1647 /*
1648 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1649 * timer could be seen as !active and just vanish away
1650 * under us on another CPU
1651 */
1652 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001653 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001654 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001655 * Enqueue the timers on the new cpu. This does not
1656 * reprogram the event device in case the timer
1657 * expires before the earliest on this CPU, but we run
1658 * hrtimer_interrupt after we migrated everything to
1659 * sort out already expired timers and reprogram the
1660 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001661 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001662 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001663
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001664 /* Clear the migration state bit */
1665 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001666 }
1667}
1668
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001669static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001670{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001671 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001672 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001673
Peter Zijlstra37810652008-12-04 11:17:10 +01001674 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001675 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001676
1677 local_irq_disable();
1678 old_base = &per_cpu(hrtimer_bases, scpu);
1679 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001680 /*
1681 * The caller is globally serialized and nobody else
1682 * takes two locks at once, deadlock is not possible.
1683 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001684 raw_spin_lock(&new_base->lock);
1685 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001686
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001687 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001688 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001689 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001690 }
1691
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001692 raw_spin_unlock(&old_base->lock);
1693 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001694
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001695 /* Check, if we got expired work to do */
1696 __hrtimer_peek_ahead_timers();
1697 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001698}
1699
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001700#endif /* CONFIG_HOTPLUG_CPU */
1701
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001702static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001703 unsigned long action, void *hcpu)
1704{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001705 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001706
1707 switch (action) {
1708
1709 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001710 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001711 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001712 break;
1713
1714#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001715 case CPU_DYING:
1716 case CPU_DYING_FROZEN:
1717 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1718 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001719 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001720 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001721 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001722 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001723 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001724 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001725 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001726#endif
1727
1728 default:
1729 break;
1730 }
1731
1732 return NOTIFY_OK;
1733}
1734
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001735static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001736 .notifier_call = hrtimer_cpu_notify,
1737};
1738
1739void __init hrtimers_init(void)
1740{
1741 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1742 (void *)(long)smp_processor_id());
1743 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001744#ifdef CONFIG_HIGH_RES_TIMERS
1745 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1746#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001747}
1748
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001749/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001750 * schedule_hrtimeout_range_clock - sleep until timeout
1751 * @expires: timeout value (ktime_t)
1752 * @delta: slack in expires timeout (ktime_t)
1753 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1754 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1755 */
1756int __sched
1757schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1758 const enum hrtimer_mode mode, int clock)
1759{
1760 struct hrtimer_sleeper t;
1761
1762 /*
1763 * Optimize when a zero timeout value is given. It does not
1764 * matter whether this is an absolute or a relative time.
1765 */
1766 if (expires && !expires->tv64) {
1767 __set_current_state(TASK_RUNNING);
1768 return 0;
1769 }
1770
1771 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001772 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001773 */
1774 if (!expires) {
1775 schedule();
1776 __set_current_state(TASK_RUNNING);
1777 return -EINTR;
1778 }
1779
1780 hrtimer_init_on_stack(&t.timer, clock, mode);
1781 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1782
1783 hrtimer_init_sleeper(&t, current);
1784
1785 hrtimer_start_expires(&t.timer, mode);
1786 if (!hrtimer_active(&t.timer))
1787 t.task = NULL;
1788
1789 if (likely(t.task))
1790 schedule();
1791
1792 hrtimer_cancel(&t.timer);
1793 destroy_hrtimer_on_stack(&t.timer);
1794
1795 __set_current_state(TASK_RUNNING);
1796
1797 return !t.task ? 0 : -EINTR;
1798}
1799
1800/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001801 * schedule_hrtimeout_range - sleep until timeout
1802 * @expires: timeout value (ktime_t)
1803 * @delta: slack in expires timeout (ktime_t)
1804 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1805 *
1806 * Make the current task sleep until the given expiry time has
1807 * elapsed. The routine will return immediately unless
1808 * the current task state has been set (see set_current_state()).
1809 *
1810 * The @delta argument gives the kernel the freedom to schedule the
1811 * actual wakeup to a time that is both power and performance friendly.
1812 * The kernel give the normal best effort behavior for "@expires+@delta",
1813 * but may decide to fire the timer earlier, but no earlier than @expires.
1814 *
1815 * You can set the task state as follows -
1816 *
1817 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1818 * pass before the routine returns.
1819 *
1820 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1821 * delivered to the current task.
1822 *
1823 * The current task state is guaranteed to be TASK_RUNNING when this
1824 * routine returns.
1825 *
1826 * Returns 0 when the timer has expired otherwise -EINTR
1827 */
1828int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001829 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001830{
Carsten Emde351b3f72010-04-02 22:40:19 +02001831 return schedule_hrtimeout_range_clock(expires, delta, mode,
1832 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001833}
1834EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1835
1836/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001837 * schedule_hrtimeout - sleep until timeout
1838 * @expires: timeout value (ktime_t)
1839 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1840 *
1841 * Make the current task sleep until the given expiry time has
1842 * elapsed. The routine will return immediately unless
1843 * the current task state has been set (see set_current_state()).
1844 *
1845 * You can set the task state as follows -
1846 *
1847 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1848 * pass before the routine returns.
1849 *
1850 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1851 * delivered to the current task.
1852 *
1853 * The current task state is guaranteed to be TASK_RUNNING when this
1854 * routine returns.
1855 *
1856 * Returns 0 when the timer has expired otherwise -EINTR
1857 */
1858int __sched schedule_hrtimeout(ktime_t *expires,
1859 const enum hrtimer_mode mode)
1860{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001861 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001862}
1863EXPORT_SYMBOL_GPL(schedule_hrtimeout);