blob: aa3739c1cd31c3675b6c4fb63c25b34b4c60e019 [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
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200229 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100230 raw_spin_unlock(&new_base->cpu_base->lock);
231 raw_spin_lock(&base->cpu_base->lock);
Michael Bohan05600292013-04-04 11:53:20 -0700232 cpu = smp_processor_id();
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200233 timer->base = base;
234 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530235 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800236 timer->base = new_base;
237 }
238 return new_base;
239}
240
241#else /* CONFIG_SMP */
242
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800243static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800244lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
245{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800246 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800247
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100248 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800249
250 return base;
251}
252
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530253# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800254
255#endif /* !CONFIG_SMP */
256
257/*
258 * Functions for the union type storage format of ktime_t which are
259 * too large for inlining:
260 */
261#if BITS_PER_LONG < 64
262# ifndef CONFIG_KTIME_SCALAR
263/**
264 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800265 * @kt: addend
266 * @nsec: the scalar nsec value to add
267 *
268 * Returns the sum of kt and nsec in ktime_t format
269 */
270ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
271{
272 ktime_t tmp;
273
274 if (likely(nsec < NSEC_PER_SEC)) {
275 tmp.tv64 = nsec;
276 } else {
277 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
278
279 tmp = ktime_set((long)nsec, rem);
280 }
281
282 return ktime_add(kt, tmp);
283}
David Howellsb8b8fd22007-04-27 15:31:24 -0700284
285EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700286
287/**
288 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
289 * @kt: minuend
290 * @nsec: the scalar nsec value to subtract
291 *
292 * Returns the subtraction of @nsec from @kt in ktime_t format
293 */
294ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
295{
296 ktime_t tmp;
297
298 if (likely(nsec < NSEC_PER_SEC)) {
299 tmp.tv64 = nsec;
300 } else {
301 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
302
303 tmp = ktime_set((long)nsec, rem);
304 }
305
306 return ktime_sub(kt, tmp);
307}
308
309EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800310# endif /* !CONFIG_KTIME_SCALAR */
311
312/*
313 * Divide a ktime value by a nanosecond value
314 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800315u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800316{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300317 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800318 int sft = 0;
319
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300320 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800321 /* Make sure the divisor is less than 2^32: */
322 while (div >> 32) {
323 sft++;
324 div >>= 1;
325 }
326 dclc >>= sft;
327 do_div(dclc, (unsigned long) div);
328
Davide Libenzi4d672e72008-02-04 22:27:26 -0800329 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800330}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800331#endif /* BITS_PER_LONG >= 64 */
332
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100333/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100334 * Add two ktime values and do a safety check for overflow:
335 */
336ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
337{
338 ktime_t res = ktime_add(lhs, rhs);
339
340 /*
341 * We use KTIME_SEC_MAX here, the maximum timeout which we can
342 * return to user space in a timespec:
343 */
344 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
345 res = ktime_set(KTIME_SEC_MAX, 0);
346
347 return res;
348}
349
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300350EXPORT_SYMBOL_GPL(ktime_add_safe);
351
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700352#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
353
354static struct debug_obj_descr hrtimer_debug_descr;
355
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100356static void *hrtimer_debug_hint(void *addr)
357{
358 return ((struct hrtimer *) addr)->function;
359}
360
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700361/*
362 * fixup_init is called when:
363 * - an active object is initialized
364 */
365static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
366{
367 struct hrtimer *timer = addr;
368
369 switch (state) {
370 case ODEBUG_STATE_ACTIVE:
371 hrtimer_cancel(timer);
372 debug_object_init(timer, &hrtimer_debug_descr);
373 return 1;
374 default:
375 return 0;
376 }
377}
378
379/*
380 * fixup_activate is called when:
381 * - an active object is activated
382 * - an unknown object is activated (might be a statically initialized object)
383 */
384static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
385{
386 switch (state) {
387
388 case ODEBUG_STATE_NOTAVAILABLE:
389 WARN_ON_ONCE(1);
390 return 0;
391
392 case ODEBUG_STATE_ACTIVE:
393 WARN_ON(1);
394
395 default:
396 return 0;
397 }
398}
399
400/*
401 * fixup_free is called when:
402 * - an active object is freed
403 */
404static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
405{
406 struct hrtimer *timer = addr;
407
408 switch (state) {
409 case ODEBUG_STATE_ACTIVE:
410 hrtimer_cancel(timer);
411 debug_object_free(timer, &hrtimer_debug_descr);
412 return 1;
413 default:
414 return 0;
415 }
416}
417
418static struct debug_obj_descr hrtimer_debug_descr = {
419 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100420 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700421 .fixup_init = hrtimer_fixup_init,
422 .fixup_activate = hrtimer_fixup_activate,
423 .fixup_free = hrtimer_fixup_free,
424};
425
426static inline void debug_hrtimer_init(struct hrtimer *timer)
427{
428 debug_object_init(timer, &hrtimer_debug_descr);
429}
430
431static inline void debug_hrtimer_activate(struct hrtimer *timer)
432{
433 debug_object_activate(timer, &hrtimer_debug_descr);
434}
435
436static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
437{
438 debug_object_deactivate(timer, &hrtimer_debug_descr);
439}
440
441static inline void debug_hrtimer_free(struct hrtimer *timer)
442{
443 debug_object_free(timer, &hrtimer_debug_descr);
444}
445
446static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
447 enum hrtimer_mode mode);
448
449void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
450 enum hrtimer_mode mode)
451{
452 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
453 __hrtimer_init(timer, clock_id, mode);
454}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700455EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700456
457void destroy_hrtimer_on_stack(struct hrtimer *timer)
458{
459 debug_object_free(timer, &hrtimer_debug_descr);
460}
461
462#else
463static inline void debug_hrtimer_init(struct hrtimer *timer) { }
464static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
465static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
466#endif
467
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800468static inline void
469debug_init(struct hrtimer *timer, clockid_t clockid,
470 enum hrtimer_mode mode)
471{
472 debug_hrtimer_init(timer);
473 trace_hrtimer_init(timer, clockid, mode);
474}
475
476static inline void debug_activate(struct hrtimer *timer)
477{
478 debug_hrtimer_activate(timer);
479 trace_hrtimer_start(timer);
480}
481
482static inline void debug_deactivate(struct hrtimer *timer)
483{
484 debug_hrtimer_deactivate(timer);
485 trace_hrtimer_cancel(timer);
486}
487
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800488/* High resolution timer related functions */
489#ifdef CONFIG_HIGH_RES_TIMERS
490
491/*
492 * High resolution timer enabled ?
493 */
494static int hrtimer_hres_enabled __read_mostly = 1;
495
496/*
497 * Enable / Disable high resolution mode
498 */
499static int __init setup_hrtimer_hres(char *str)
500{
501 if (!strcmp(str, "off"))
502 hrtimer_hres_enabled = 0;
503 else if (!strcmp(str, "on"))
504 hrtimer_hres_enabled = 1;
505 else
506 return 0;
507 return 1;
508}
509
510__setup("highres=", setup_hrtimer_hres);
511
512/*
513 * hrtimer_high_res_enabled - query, if the highres mode is enabled
514 */
515static inline int hrtimer_is_hres_enabled(void)
516{
517 return hrtimer_hres_enabled;
518}
519
520/*
521 * Is the high resolution mode active ?
522 */
523static inline int hrtimer_hres_active(void)
524{
Christoph Lameter909ea962010-12-08 16:22:55 +0100525 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800526}
527
528/*
529 * Reprogram the event source with checking both queues for the
530 * next event
531 * Called with interrupts disabled and base->lock held
532 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400533static void
534hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800535{
536 int i;
537 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400538 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800539
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400540 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800541
542 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
543 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700544 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800545
John Stultz998adc32010-09-20 19:19:17 -0700546 next = timerqueue_getnext(&base->active);
547 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800548 continue;
John Stultz998adc32010-09-20 19:19:17 -0700549 timer = container_of(next, struct hrtimer, node);
550
Arjan van de Vencc584b22008-09-01 15:02:30 -0700551 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100552 /*
553 * clock_was_set() has changed base->offset so the
554 * result might be negative. Fix it up to prevent a
555 * false positive in clockevents_program_event()
556 */
557 if (expires.tv64 < 0)
558 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400559 if (expires.tv64 < expires_next.tv64)
560 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800561 }
562
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400563 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
564 return;
565
566 cpu_base->expires_next.tv64 = expires_next.tv64;
567
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800568 if (cpu_base->expires_next.tv64 != KTIME_MAX)
569 tick_program_event(cpu_base->expires_next, 1);
570}
571
572/*
573 * Shared reprogramming for clock_realtime and clock_monotonic
574 *
575 * When a timer is enqueued and expires earlier than the already enqueued
576 * timers, we have to check, whether it expires earlier than the timer for
577 * which the clock event device was armed.
578 *
579 * Called with interrupts disabled and base->cpu_base.lock held
580 */
581static int hrtimer_reprogram(struct hrtimer *timer,
582 struct hrtimer_clock_base *base)
583{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100584 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700585 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800586 int res;
587
Arjan van de Vencc584b22008-09-01 15:02:30 -0700588 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100589
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800590 /*
591 * When the callback is running, we do not reprogram the clock event
592 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200593 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800594 * reprogramming is handled either by the softirq, which called the
595 * callback or at the end of the hrtimer_interrupt.
596 */
597 if (hrtimer_callback_running(timer))
598 return 0;
599
Thomas Gleixner63070a72008-02-14 00:58:36 +0100600 /*
601 * CLOCK_REALTIME timer might be requested with an absolute
602 * expiry time which is less than base->offset. Nothing wrong
603 * about that, just avoid to call into the tick code, which
604 * has now objections against negative expiry values.
605 */
606 if (expires.tv64 < 0)
607 return -ETIME;
608
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100609 if (expires.tv64 >= cpu_base->expires_next.tv64)
610 return 0;
611
612 /*
613 * If a hang was detected in the last timer interrupt then we
614 * do not schedule a timer which is earlier than the expiry
615 * which we enforced in the hang detection. We want the system
616 * to make progress.
617 */
618 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800619 return 0;
620
621 /*
622 * Clockevents returns -ETIME, when the event was in the past.
623 */
624 res = tick_program_event(expires, 0);
625 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100626 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800627 return res;
628}
629
Ingo Molnar995f0542007-04-07 12:05:00 +0200630/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800631 * Initialize the high resolution related parts of cpu_base
632 */
633static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
634{
635 base->expires_next.tv64 = KTIME_MAX;
636 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800637}
638
639/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800640 * When High resolution timers are active, try to reprogram. Note, that in case
641 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
642 * check happens. The timer gets enqueued into the rbtree. The reprogramming
643 * and expiry check is done in the hrtimer_interrupt or in the softirq.
644 */
645static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzabf04782013-02-04 14:33:37 +0200646 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800647{
Leonid Shatzabf04782013-02-04 14:33:37 +0200648 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800649}
650
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200651/*
652 * Retrigger next event is called after clock was set
653 *
654 * Called with interrupts disabled via on_each_cpu()
655 */
656static void retrigger_next_event(void *arg)
657{
658 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
659 struct timespec realtime_offset, xtim, wtm, sleep;
660
661 if (!hrtimer_hres_active())
662 return;
663
664 /* Optimized out for !HIGH_RES */
665 get_xtime_and_monotonic_and_sleep_offset(&xtim, &wtm, &sleep);
666 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
667
668 /* Adjust CLOCK_REALTIME offset */
669 raw_spin_lock(&base->lock);
670 base->clock_base[HRTIMER_BASE_REALTIME].offset =
671 timespec_to_ktime(realtime_offset);
672 base->clock_base[HRTIMER_BASE_BOOTTIME].offset =
673 timespec_to_ktime(sleep);
674
675 hrtimer_force_reprogram(base, 0);
676 raw_spin_unlock(&base->lock);
677}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200678
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800679/*
680 * Switch to high resolution mode
681 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800682static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800683{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200684 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700685 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800686 unsigned long flags;
687
688 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800689 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800690
691 local_irq_save(flags);
692
693 if (tick_init_highres()) {
694 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700695 printk(KERN_WARNING "Could not switch to high resolution "
696 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800697 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800698 }
699 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200700 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
701 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800702
703 tick_setup_sched_timer();
704
705 /* "Retrigger" the interrupt to get things going */
706 retrigger_next_event(NULL);
707 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800708 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800709}
710
711#else
712
713static inline int hrtimer_hres_active(void) { return 0; }
714static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800715static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400716static inline void
717hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800718static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Leonid Shatzabf04782013-02-04 14:33:37 +0200719 struct hrtimer_clock_base *base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800720{
721 return 0;
722}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800723static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200724static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800725
726#endif /* CONFIG_HIGH_RES_TIMERS */
727
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200728/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200729 * Clock realtime was set
730 *
731 * Change the offset of the realtime clock vs. the monotonic
732 * clock.
733 *
734 * We might have to reprogram the high resolution timer interrupt. On
735 * SMP we call the architecture specific code to retrigger _all_ high
736 * resolution timer interrupts. On UP we just disable interrupts and
737 * call the high resolution interrupt code.
738 */
739void clock_was_set(void)
740{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200741#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200742 /* Retrigger the CPU local events everywhere */
743 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200744#endif
745 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200746}
747
748/*
749 * During resume we might have to reprogram the high resolution timer
750 * interrupt (on the local CPU):
751 */
752void hrtimers_resume(void)
753{
754 WARN_ONCE(!irqs_disabled(),
755 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
756
757 retrigger_next_event(NULL);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200758 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200759}
760
Heiko Carstens5f201902009-12-10 10:56:29 +0100761static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800762{
Heiko Carstens5f201902009-12-10 10:56:29 +0100763#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800764 if (timer->start_site)
765 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100766 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800767 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
768 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800769#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100770}
771
772static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
773{
774#ifdef CONFIG_TIMER_STATS
775 timer->start_site = NULL;
776#endif
777}
778
779static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
780{
781#ifdef CONFIG_TIMER_STATS
782 if (likely(!timer_stats_active))
783 return;
784 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
785 timer->function, timer->start_comm, 0);
786#endif
787}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800788
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800789/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200790 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800791 */
792static inline
793void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
794{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100795 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800796}
797
798/**
799 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800800 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800801 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800802 * @interval: the interval to forward
803 *
804 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700805 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800806 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800807u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800808{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800809 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800810 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800811
Arjan van de Vencc584b22008-09-01 15:02:30 -0700812 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800813
814 if (delta.tv64 < 0)
815 return 0;
816
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100817 if (interval.tv64 < timer->base->resolution.tv64)
818 interval.tv64 = timer->base->resolution.tv64;
819
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800820 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800821 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800822
823 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700824 hrtimer_add_expires_ns(timer, incr * orun);
825 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800826 return orun;
827 /*
828 * This (and the ktime_add() below) is the
829 * correction for exact:
830 */
831 orun++;
832 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700833 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800834
835 return orun;
836}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700837EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800838
839/*
840 * enqueue_hrtimer - internal function to (re)start a timer
841 *
842 * The timer is inserted in expiry order. Insertion into the
843 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100844 *
845 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800846 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100847static int enqueue_hrtimer(struct hrtimer *timer,
848 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800849{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800850 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700851
John Stultz998adc32010-09-20 19:19:17 -0700852 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200853 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800854
855 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800856 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
857 * state of a possibly running callback.
858 */
859 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100860
John Stultz998adc32010-09-20 19:19:17 -0700861 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100862}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800863
864/*
865 * __remove_hrtimer - internal function to remove a timer
866 *
867 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800868 *
869 * High resolution timer mode reprograms the clock event device when the
870 * timer is the one which expires next. The caller can disable this by setting
871 * reprogram to zero. This is useful, when the context does a reprogramming
872 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800873 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800874static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800875 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800876 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800877{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800878 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400879 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
880 goto out;
881
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800882 next_timer = timerqueue_getnext(&base->active);
883 timerqueue_del(&base->active, &timer->node);
884 if (&timer->node == next_timer) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400885#ifdef CONFIG_HIGH_RES_TIMERS
886 /* Reprogram the clock event device. if enabled */
887 if (reprogram && hrtimer_hres_active()) {
888 ktime_t expires;
889
890 expires = ktime_sub(hrtimer_get_expires(timer),
891 base->offset);
892 if (base->cpu_base->expires_next.tv64 == expires.tv64)
893 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800894 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400895#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800896 }
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200897 if (!timerqueue_getnext(&base->active))
898 base->cpu_base->active_bases &= ~(1 << base->index);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400899out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800900 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800901}
902
903/*
904 * remove hrtimer, called with base lock held
905 */
906static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800907remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800908{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800909 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700910 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800911 int reprogram;
912
913 /*
914 * Remove the timer and force reprogramming when high
915 * resolution mode is active and the timer is on the current
916 * CPU. If we remove a timer on another CPU, reprogramming is
917 * skipped. The interrupt event on this CPU is fired and
918 * reprogramming happens in the interrupt handler. This is a
919 * rare case and less expensive than a smp call.
920 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800921 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800922 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800923 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700924 /*
925 * We must preserve the CALLBACK state flag here,
926 * otherwise we could move the timer base in
927 * switch_hrtimer_base.
928 */
929 state = timer->state & HRTIMER_STATE_CALLBACK;
930 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800931 return 1;
932 }
933 return 0;
934}
935
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100936int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
937 unsigned long delta_ns, const enum hrtimer_mode mode,
938 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800939{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800940 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800941 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100942 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800943
944 base = lock_hrtimer_base(timer, &flags);
945
946 /* Remove an active timer from the queue: */
947 ret = remove_hrtimer(timer, base);
948
949 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530950 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800951
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530952 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100953 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800954 /*
955 * CONFIG_TIME_LOW_RES is a temporary way for architectures
956 * to signal that they simply return xtime in
957 * do_gettimeoffset(). In this case we want to round up by
958 * resolution when starting a relative timer, to avoid short
959 * timeouts. This will go away with the GTOD framework.
960 */
961#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100962 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800963#endif
964 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700965
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700966 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800967
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800968 timer_stats_hrtimer_set_start_info(timer);
969
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100970 leftmost = enqueue_hrtimer(timer, new_base);
971
Ingo Molnar935c6312007-03-28 13:17:18 +0200972 /*
973 * Only allow reprogramming if the new base is on this CPU.
974 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100975 *
976 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200977 */
Leonid Shatzabf04782013-02-04 14:33:37 +0200978 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
979 && hrtimer_enqueue_reprogram(timer, new_base)) {
980 if (wakeup) {
981 /*
982 * We need to drop cpu_base->lock to avoid a
983 * lock ordering issue vs. rq->lock.
984 */
985 raw_spin_unlock(&new_base->cpu_base->lock);
986 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
987 local_irq_restore(flags);
988 return ret;
989 } else {
990 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
991 }
992 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800993
994 unlock_hrtimer_base(timer, &flags);
995
996 return ret;
997}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100998
999/**
1000 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1001 * @timer: the timer to be added
1002 * @tim: expiry time
1003 * @delta_ns: "slack" range for the timer
1004 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1005 *
1006 * Returns:
1007 * 0 on success
1008 * 1 when the timer was active
1009 */
1010int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1011 unsigned long delta_ns, const enum hrtimer_mode mode)
1012{
1013 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1014}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001015EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1016
1017/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001018 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001019 * @timer: the timer to be added
1020 * @tim: expiry time
1021 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1022 *
1023 * Returns:
1024 * 0 on success
1025 * 1 when the timer was active
1026 */
1027int
1028hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1029{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001030 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001031}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001032EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001033
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001034
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001035/**
1036 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001037 * @timer: hrtimer to stop
1038 *
1039 * Returns:
1040 * 0 when the timer was not active
1041 * 1 when the timer was active
1042 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001043 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001044 */
1045int hrtimer_try_to_cancel(struct hrtimer *timer)
1046{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001047 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001048 unsigned long flags;
1049 int ret = -1;
1050
1051 base = lock_hrtimer_base(timer, &flags);
1052
Thomas Gleixner303e9672007-02-16 01:27:51 -08001053 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001054 ret = remove_hrtimer(timer, base);
1055
1056 unlock_hrtimer_base(timer, &flags);
1057
1058 return ret;
1059
1060}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001061EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001062
1063/**
1064 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001065 * @timer: the timer to be cancelled
1066 *
1067 * Returns:
1068 * 0 when the timer was not active
1069 * 1 when the timer was active
1070 */
1071int hrtimer_cancel(struct hrtimer *timer)
1072{
1073 for (;;) {
1074 int ret = hrtimer_try_to_cancel(timer);
1075
1076 if (ret >= 0)
1077 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001078 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001079 }
1080}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001081EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001082
1083/**
1084 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001085 * @timer: the timer to read
1086 */
1087ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1088{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001089 unsigned long flags;
1090 ktime_t rem;
1091
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001092 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001093 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001094 unlock_hrtimer_base(timer, &flags);
1095
1096 return rem;
1097}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001098EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001099
Russell Kingee9c5782008-04-20 13:59:33 +01001100#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001101/**
1102 * hrtimer_get_next_event - get the time until next expiry event
1103 *
1104 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1105 * is pending.
1106 */
1107ktime_t hrtimer_get_next_event(void)
1108{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001109 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1110 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001111 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1112 unsigned long flags;
1113 int i;
1114
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001115 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001116
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001117 if (!hrtimer_hres_active()) {
1118 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1119 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001120 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001121
John Stultz998adc32010-09-20 19:19:17 -07001122 next = timerqueue_getnext(&base->active);
1123 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001124 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001125
John Stultz998adc32010-09-20 19:19:17 -07001126 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001127 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001128 delta = ktime_sub(delta, base->get_time());
1129 if (delta.tv64 < mindelta.tv64)
1130 mindelta.tv64 = delta.tv64;
1131 }
Tony Lindgren69239742006-03-06 15:42:45 -08001132 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001133
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001134 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001135
Tony Lindgren69239742006-03-06 15:42:45 -08001136 if (mindelta.tv64 < 0)
1137 mindelta.tv64 = 0;
1138 return mindelta;
1139}
1140#endif
1141
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001142static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1143 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001144{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001145 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001146 int base;
George Anzinger79786722006-02-01 03:05:11 -08001147
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001148 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001149
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001150 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001151
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001152 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001153 clock_id = CLOCK_MONOTONIC;
1154
John Stultze06383d2010-12-14 19:37:07 -08001155 base = hrtimer_clockid_to_base(clock_id);
1156 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001157 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001158
1159#ifdef CONFIG_TIMER_STATS
1160 timer->start_site = NULL;
1161 timer->start_pid = -1;
1162 memset(timer->start_comm, 0, TASK_COMM_LEN);
1163#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001164}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001165
1166/**
1167 * hrtimer_init - initialize a timer to the given clock
1168 * @timer: the timer to be initialized
1169 * @clock_id: the clock to be used
1170 * @mode: timer mode abs/rel
1171 */
1172void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1173 enum hrtimer_mode mode)
1174{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001175 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001176 __hrtimer_init(timer, clock_id, mode);
1177}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001178EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001179
1180/**
1181 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001182 * @which_clock: which clock to query
1183 * @tp: pointer to timespec variable to store the resolution
1184 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001185 * Store the resolution of the clock selected by @which_clock in the
1186 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001187 */
1188int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1189{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001190 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001191 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001192
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001193 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001194 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001195
1196 return 0;
1197}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001198EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001199
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001200static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001201{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001202 struct hrtimer_clock_base *base = timer->base;
1203 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1204 enum hrtimer_restart (*fn)(struct hrtimer *);
1205 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001206
Peter Zijlstraca109492008-11-25 12:43:51 +01001207 WARN_ON(!irqs_disabled());
1208
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001209 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001210 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1211 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001212 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001213
1214 /*
1215 * Because we run timers from hardirq context, there is no chance
1216 * they get migrated to another cpu, therefore its safe to unlock
1217 * the timer base.
1218 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001219 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001220 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001221 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001222 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001223 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001224
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001225 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001226 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1227 * we do not reprogramm the event hardware. Happens either in
1228 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001229 */
1230 if (restart != HRTIMER_NORESTART) {
1231 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001232 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001233 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001234
1235 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1236
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001237 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001238}
1239
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001240#ifdef CONFIG_HIGH_RES_TIMERS
1241
1242/*
1243 * High resolution timer interrupt
1244 * Called with interrupts disabled
1245 */
1246void hrtimer_interrupt(struct clock_event_device *dev)
1247{
1248 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001249 ktime_t expires_next, now, entry_time, delta;
1250 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001251
1252 BUG_ON(!cpu_base->hres_active);
1253 cpu_base->nr_events++;
1254 dev->next_event.tv64 = KTIME_MAX;
1255
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001256 entry_time = now = ktime_get();
1257retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001258 expires_next.tv64 = KTIME_MAX;
1259
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001260 raw_spin_lock(&cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001261 /*
1262 * We set expires_next to KTIME_MAX here with cpu_base->lock
1263 * held to prevent that a timer is enqueued in our queue via
1264 * the migration code. This does not affect enqueueing of
1265 * timers which run their callback and need to be requeued on
1266 * this CPU.
1267 */
1268 cpu_base->expires_next.tv64 = KTIME_MAX;
1269
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001270 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001271 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001272 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001273 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001274
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001275 if (!(cpu_base->active_bases & (1 << i)))
1276 continue;
1277
1278 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001279 basenow = ktime_add(now, base->offset);
1280
John Stultz998adc32010-09-20 19:19:17 -07001281 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001282 struct hrtimer *timer;
1283
John Stultz998adc32010-09-20 19:19:17 -07001284 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001285
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001286 /*
1287 * The immediate goal for using the softexpires is
1288 * minimizing wakeups, not running timers at the
1289 * earliest interrupt after their soft expiration.
1290 * This allows us to avoid using a Priority Search
1291 * Tree, which can answer a stabbing querry for
1292 * overlapping intervals and instead use the simple
1293 * BST we already have.
1294 * We don't add extra wakeups by delaying timers that
1295 * are right-of a not yet expired timer, because that
1296 * timer will have to trigger a wakeup anyway.
1297 */
1298
1299 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001300 ktime_t expires;
1301
Arjan van de Vencc584b22008-09-01 15:02:30 -07001302 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001303 base->offset);
1304 if (expires.tv64 < expires_next.tv64)
1305 expires_next = expires;
1306 break;
1307 }
1308
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001309 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001310 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001311 }
1312
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001313 /*
1314 * Store the new expiry value so the migration code can verify
1315 * against it.
1316 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001317 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001318 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001319
1320 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001321 if (expires_next.tv64 == KTIME_MAX ||
1322 !tick_program_event(expires_next, 0)) {
1323 cpu_base->hang_detected = 0;
1324 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001325 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001326
1327 /*
1328 * The next timer was already expired due to:
1329 * - tracing
1330 * - long lasting callbacks
1331 * - being scheduled away when running in a VM
1332 *
1333 * We need to prevent that we loop forever in the hrtimer
1334 * interrupt routine. We give it 3 attempts to avoid
1335 * overreacting on some spurious event.
1336 */
1337 now = ktime_get();
1338 cpu_base->nr_retries++;
1339 if (++retries < 3)
1340 goto retry;
1341 /*
1342 * Give the system a chance to do something else than looping
1343 * here. We stored the entry time, so we know exactly how long
1344 * we spent here. We schedule the next event this amount of
1345 * time away.
1346 */
1347 cpu_base->nr_hangs++;
1348 cpu_base->hang_detected = 1;
1349 delta = ktime_sub(now, entry_time);
1350 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1351 cpu_base->max_hang_time = delta;
1352 /*
1353 * Limit it to a sensible value as we enforce a longer
1354 * delay. Give the CPU at least 100ms to catch up.
1355 */
1356 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1357 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1358 else
1359 expires_next = ktime_add(now, delta);
1360 tick_program_event(expires_next, 1);
1361 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1362 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001363}
1364
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001365/*
1366 * local version of hrtimer_peek_ahead_timers() called with interrupts
1367 * disabled.
1368 */
1369static void __hrtimer_peek_ahead_timers(void)
1370{
1371 struct tick_device *td;
1372
1373 if (!hrtimer_hres_active())
1374 return;
1375
1376 td = &__get_cpu_var(tick_cpu_device);
1377 if (td && td->evtdev)
1378 hrtimer_interrupt(td->evtdev);
1379}
1380
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001381/**
1382 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1383 *
1384 * hrtimer_peek_ahead_timers will peek at the timer queue of
1385 * the current cpu and check if there are any timers for which
1386 * the soft expires time has passed. If any such timers exist,
1387 * they are run immediately and then removed from the timer queue.
1388 *
1389 */
1390void hrtimer_peek_ahead_timers(void)
1391{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001392 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001393
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001394 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001395 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001396 local_irq_restore(flags);
1397}
1398
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001399static void run_hrtimer_softirq(struct softirq_action *h)
1400{
1401 hrtimer_peek_ahead_timers();
1402}
1403
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001404#else /* CONFIG_HIGH_RES_TIMERS */
1405
1406static inline void __hrtimer_peek_ahead_timers(void) { }
1407
1408#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001409
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001410/*
1411 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001412 *
1413 * For HRT its the fall back code to run the softirq in the timer
1414 * softirq context in case the hrtimer initialization failed or has
1415 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001416 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001417void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001418{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001419 if (hrtimer_hres_active())
1420 return;
1421
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001422 /*
1423 * This _is_ ugly: We have to check in the softirq context,
1424 * whether we can switch to highres and / or nohz mode. The
1425 * clocksource switch happens in the timer interrupt with
1426 * xtime_lock held. Notification from there only sets the
1427 * check bit in the tick_oneshot code, otherwise we might
1428 * deadlock vs. xtime_lock.
1429 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001430 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001431 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001432}
1433
1434/*
1435 * Called from hardirq context every jiffy
1436 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001437void hrtimer_run_queues(void)
1438{
John Stultz998adc32010-09-20 19:19:17 -07001439 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001440 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001441 struct hrtimer_clock_base *base;
1442 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001443
1444 if (hrtimer_hres_active())
1445 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001446
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001447 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1448 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001449 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001450 continue;
1451
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001452 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001453 hrtimer_get_softirq_time(cpu_base);
1454 gettime = 0;
1455 }
1456
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001457 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001458
John Stultzb007c382010-12-10 22:19:53 -08001459 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001460 struct hrtimer *timer;
1461
John Stultz998adc32010-09-20 19:19:17 -07001462 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001463 if (base->softirq_time.tv64 <=
1464 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001465 break;
1466
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001467 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001468 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001469 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001470 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001471}
1472
1473/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001474 * Sleep related functions:
1475 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001476static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001477{
1478 struct hrtimer_sleeper *t =
1479 container_of(timer, struct hrtimer_sleeper, timer);
1480 struct task_struct *task = t->task;
1481
1482 t->task = NULL;
1483 if (task)
1484 wake_up_process(task);
1485
1486 return HRTIMER_NORESTART;
1487}
1488
Ingo Molnar36c8b582006-07-03 00:25:41 -07001489void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001490{
1491 sl->timer.function = hrtimer_wakeup;
1492 sl->task = task;
1493}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001494EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001495
Thomas Gleixner669d7862006-03-31 02:31:19 -08001496static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001497{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001498 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001499
Roman Zippel432569b2006-03-26 01:38:08 -08001500 do {
1501 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001502 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001503 if (!hrtimer_active(&t->timer))
1504 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001505
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001506 if (likely(t->task))
1507 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001508
Thomas Gleixner669d7862006-03-31 02:31:19 -08001509 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001510 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001511
Thomas Gleixner669d7862006-03-31 02:31:19 -08001512 } while (t->task && !signal_pending(current));
1513
Peter Zijlstra3588a082008-02-01 17:45:13 +01001514 __set_current_state(TASK_RUNNING);
1515
Thomas Gleixner669d7862006-03-31 02:31:19 -08001516 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001517}
1518
Oleg Nesterov080344b2008-02-01 17:29:05 +03001519static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1520{
1521 struct timespec rmt;
1522 ktime_t rem;
1523
Arjan van de Vencc584b22008-09-01 15:02:30 -07001524 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001525 if (rem.tv64 <= 0)
1526 return 0;
1527 rmt = ktime_to_timespec(rem);
1528
1529 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1530 return -EFAULT;
1531
1532 return 1;
1533}
1534
Toyo Abe1711ef32006-09-29 02:00:28 -07001535long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001536{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001537 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001538 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001539 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001540
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001541 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001542 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001543 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001544
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001545 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001546 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001547
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001548 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001549 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001550 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001551 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001552 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001553 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001554
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001555 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001556 ret = -ERESTART_RESTARTBLOCK;
1557out:
1558 destroy_hrtimer_on_stack(&t.timer);
1559 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001560}
1561
Oleg Nesterov080344b2008-02-01 17:29:05 +03001562long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001563 const enum hrtimer_mode mode, const clockid_t clockid)
1564{
1565 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001566 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001567 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001568 unsigned long slack;
1569
1570 slack = current->timer_slack_ns;
1571 if (rt_task(current))
1572 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001573
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001574 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001575 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001576 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001577 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001578
George Anzinger79786722006-02-01 03:05:11 -08001579 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001580 if (mode == HRTIMER_MODE_ABS) {
1581 ret = -ERESTARTNOHAND;
1582 goto out;
1583 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001584
Roman Zippel432569b2006-03-26 01:38:08 -08001585 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001586 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001587 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001588 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001589 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001590
1591 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001592 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001593 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001594 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001595 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001596
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001597 ret = -ERESTART_RESTARTBLOCK;
1598out:
1599 destroy_hrtimer_on_stack(&t.timer);
1600 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001601}
1602
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001603SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1604 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001605{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001606 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001607
1608 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1609 return -EFAULT;
1610
1611 if (!timespec_valid(&tu))
1612 return -EINVAL;
1613
Oleg Nesterov080344b2008-02-01 17:29:05 +03001614 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001615}
1616
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001617/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001618 * Functions related to boot-time initialization:
1619 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001620static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001621{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001622 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001623 int i;
1624
John Stultz998adc32010-09-20 19:19:17 -07001625 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001626 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001627 timerqueue_init_head(&cpu_base->clock_base[i].active);
1628 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001629
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001630 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001631}
1632
1633#ifdef CONFIG_HOTPLUG_CPU
1634
Peter Zijlstraca109492008-11-25 12:43:51 +01001635static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001636 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001637{
1638 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001639 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001640
John Stultz998adc32010-09-20 19:19:17 -07001641 while ((node = timerqueue_getnext(&old_base->active))) {
1642 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001643 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001644 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001645
1646 /*
1647 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1648 * timer could be seen as !active and just vanish away
1649 * under us on another CPU
1650 */
1651 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001652 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001653 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001654 * Enqueue the timers on the new cpu. This does not
1655 * reprogram the event device in case the timer
1656 * expires before the earliest on this CPU, but we run
1657 * hrtimer_interrupt after we migrated everything to
1658 * sort out already expired timers and reprogram the
1659 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001660 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001661 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001662
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001663 /* Clear the migration state bit */
1664 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001665 }
1666}
1667
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001668static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001669{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001670 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001671 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001672
Peter Zijlstra37810652008-12-04 11:17:10 +01001673 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001674 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001675
1676 local_irq_disable();
1677 old_base = &per_cpu(hrtimer_bases, scpu);
1678 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001679 /*
1680 * The caller is globally serialized and nobody else
1681 * takes two locks at once, deadlock is not possible.
1682 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001683 raw_spin_lock(&new_base->lock);
1684 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001685
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001686 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001687 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001688 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001689 }
1690
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001691 raw_spin_unlock(&old_base->lock);
1692 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001693
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001694 /* Check, if we got expired work to do */
1695 __hrtimer_peek_ahead_timers();
1696 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001697}
1698
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001699#endif /* CONFIG_HOTPLUG_CPU */
1700
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001701static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001702 unsigned long action, void *hcpu)
1703{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001704 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001705
1706 switch (action) {
1707
1708 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001709 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001710 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001711 break;
1712
1713#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001714 case CPU_DYING:
1715 case CPU_DYING_FROZEN:
1716 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1717 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001718 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001719 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001720 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001721 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001722 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001723 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001724 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001725#endif
1726
1727 default:
1728 break;
1729 }
1730
1731 return NOTIFY_OK;
1732}
1733
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001734static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001735 .notifier_call = hrtimer_cpu_notify,
1736};
1737
1738void __init hrtimers_init(void)
1739{
1740 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1741 (void *)(long)smp_processor_id());
1742 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001743#ifdef CONFIG_HIGH_RES_TIMERS
1744 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1745#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001746}
1747
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001748/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001749 * schedule_hrtimeout_range_clock - sleep until timeout
1750 * @expires: timeout value (ktime_t)
1751 * @delta: slack in expires timeout (ktime_t)
1752 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1753 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1754 */
1755int __sched
1756schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1757 const enum hrtimer_mode mode, int clock)
1758{
1759 struct hrtimer_sleeper t;
1760
1761 /*
1762 * Optimize when a zero timeout value is given. It does not
1763 * matter whether this is an absolute or a relative time.
1764 */
1765 if (expires && !expires->tv64) {
1766 __set_current_state(TASK_RUNNING);
1767 return 0;
1768 }
1769
1770 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001771 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001772 */
1773 if (!expires) {
1774 schedule();
1775 __set_current_state(TASK_RUNNING);
1776 return -EINTR;
1777 }
1778
1779 hrtimer_init_on_stack(&t.timer, clock, mode);
1780 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1781
1782 hrtimer_init_sleeper(&t, current);
1783
1784 hrtimer_start_expires(&t.timer, mode);
1785 if (!hrtimer_active(&t.timer))
1786 t.task = NULL;
1787
1788 if (likely(t.task))
1789 schedule();
1790
1791 hrtimer_cancel(&t.timer);
1792 destroy_hrtimer_on_stack(&t.timer);
1793
1794 __set_current_state(TASK_RUNNING);
1795
1796 return !t.task ? 0 : -EINTR;
1797}
1798
1799/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001800 * schedule_hrtimeout_range - sleep until timeout
1801 * @expires: timeout value (ktime_t)
1802 * @delta: slack in expires timeout (ktime_t)
1803 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1804 *
1805 * Make the current task sleep until the given expiry time has
1806 * elapsed. The routine will return immediately unless
1807 * the current task state has been set (see set_current_state()).
1808 *
1809 * The @delta argument gives the kernel the freedom to schedule the
1810 * actual wakeup to a time that is both power and performance friendly.
1811 * The kernel give the normal best effort behavior for "@expires+@delta",
1812 * but may decide to fire the timer earlier, but no earlier than @expires.
1813 *
1814 * You can set the task state as follows -
1815 *
1816 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1817 * pass before the routine returns.
1818 *
1819 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1820 * delivered to the current task.
1821 *
1822 * The current task state is guaranteed to be TASK_RUNNING when this
1823 * routine returns.
1824 *
1825 * Returns 0 when the timer has expired otherwise -EINTR
1826 */
1827int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001828 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001829{
Carsten Emde351b3f72010-04-02 22:40:19 +02001830 return schedule_hrtimeout_range_clock(expires, delta, mode,
1831 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001832}
1833EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1834
1835/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001836 * schedule_hrtimeout - sleep until timeout
1837 * @expires: timeout value (ktime_t)
1838 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1839 *
1840 * Make the current task sleep until the given expiry time has
1841 * elapsed. The routine will return immediately unless
1842 * the current task state has been set (see set_current_state()).
1843 *
1844 * You can set the task state as follows -
1845 *
1846 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1847 * pass before the routine returns.
1848 *
1849 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1850 * delivered to the current task.
1851 *
1852 * The current task state is guaranteed to be TASK_RUNNING when this
1853 * routine returns.
1854 *
1855 * Returns 0 when the timer has expired otherwise -EINTR
1856 */
1857int __sched schedule_hrtimeout(ktime_t *expires,
1858 const enum hrtimer_mode mode)
1859{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001860 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001861}
1862EXPORT_SYMBOL_GPL(schedule_hrtimeout);