blob: d1e73a4eed982267eb87f53a26732c405c8cd6e3 [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 Bohanb7824c62013-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
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200227 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
228 cpu = this_cpu;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100229 raw_spin_unlock(&new_base->cpu_base->lock);
230 raw_spin_lock(&base->cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200231 timer->base = base;
232 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530233 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800234 timer->base = new_base;
235 }
236 return new_base;
237}
238
239#else /* CONFIG_SMP */
240
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800241static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800242lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
243{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800244 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800245
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100246 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800247
248 return base;
249}
250
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530251# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800252
253#endif /* !CONFIG_SMP */
254
255/*
256 * Functions for the union type storage format of ktime_t which are
257 * too large for inlining:
258 */
259#if BITS_PER_LONG < 64
260# ifndef CONFIG_KTIME_SCALAR
261/**
262 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800263 * @kt: addend
264 * @nsec: the scalar nsec value to add
265 *
266 * Returns the sum of kt and nsec in ktime_t format
267 */
268ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
269{
270 ktime_t tmp;
271
272 if (likely(nsec < NSEC_PER_SEC)) {
273 tmp.tv64 = nsec;
274 } else {
275 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
276
277 tmp = ktime_set((long)nsec, rem);
278 }
279
280 return ktime_add(kt, tmp);
281}
David Howellsb8b8fd22007-04-27 15:31:24 -0700282
283EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700284
285/**
286 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
287 * @kt: minuend
288 * @nsec: the scalar nsec value to subtract
289 *
290 * Returns the subtraction of @nsec from @kt in ktime_t format
291 */
292ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
293{
294 ktime_t tmp;
295
296 if (likely(nsec < NSEC_PER_SEC)) {
297 tmp.tv64 = nsec;
298 } else {
299 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
300
301 tmp = ktime_set((long)nsec, rem);
302 }
303
304 return ktime_sub(kt, tmp);
305}
306
307EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800308# endif /* !CONFIG_KTIME_SCALAR */
309
310/*
311 * Divide a ktime value by a nanosecond value
312 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800313u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800314{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300315 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800316 int sft = 0;
317
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300318 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800319 /* Make sure the divisor is less than 2^32: */
320 while (div >> 32) {
321 sft++;
322 div >>= 1;
323 }
324 dclc >>= sft;
325 do_div(dclc, (unsigned long) div);
326
Davide Libenzi4d672e72008-02-04 22:27:26 -0800327 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800328}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800329#endif /* BITS_PER_LONG >= 64 */
330
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100331/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100332 * Add two ktime values and do a safety check for overflow:
333 */
334ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
335{
336 ktime_t res = ktime_add(lhs, rhs);
337
338 /*
339 * We use KTIME_SEC_MAX here, the maximum timeout which we can
340 * return to user space in a timespec:
341 */
342 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
343 res = ktime_set(KTIME_SEC_MAX, 0);
344
345 return res;
346}
347
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300348EXPORT_SYMBOL_GPL(ktime_add_safe);
349
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700350#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
351
352static struct debug_obj_descr hrtimer_debug_descr;
353
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100354static void *hrtimer_debug_hint(void *addr)
355{
356 return ((struct hrtimer *) addr)->function;
357}
358
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700359/*
360 * fixup_init is called when:
361 * - an active object is initialized
362 */
363static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
364{
365 struct hrtimer *timer = addr;
366
367 switch (state) {
368 case ODEBUG_STATE_ACTIVE:
369 hrtimer_cancel(timer);
370 debug_object_init(timer, &hrtimer_debug_descr);
371 return 1;
372 default:
373 return 0;
374 }
375}
376
377/*
378 * fixup_activate is called when:
379 * - an active object is activated
380 * - an unknown object is activated (might be a statically initialized object)
381 */
382static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
383{
384 switch (state) {
385
386 case ODEBUG_STATE_NOTAVAILABLE:
387 WARN_ON_ONCE(1);
388 return 0;
389
390 case ODEBUG_STATE_ACTIVE:
391 WARN_ON(1);
392
393 default:
394 return 0;
395 }
396}
397
398/*
399 * fixup_free is called when:
400 * - an active object is freed
401 */
402static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
403{
404 struct hrtimer *timer = addr;
405
406 switch (state) {
407 case ODEBUG_STATE_ACTIVE:
408 hrtimer_cancel(timer);
409 debug_object_free(timer, &hrtimer_debug_descr);
410 return 1;
411 default:
412 return 0;
413 }
414}
415
416static struct debug_obj_descr hrtimer_debug_descr = {
417 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100418 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700419 .fixup_init = hrtimer_fixup_init,
420 .fixup_activate = hrtimer_fixup_activate,
421 .fixup_free = hrtimer_fixup_free,
422};
423
424static inline void debug_hrtimer_init(struct hrtimer *timer)
425{
426 debug_object_init(timer, &hrtimer_debug_descr);
427}
428
429static inline void debug_hrtimer_activate(struct hrtimer *timer)
430{
431 debug_object_activate(timer, &hrtimer_debug_descr);
432}
433
434static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
435{
436 debug_object_deactivate(timer, &hrtimer_debug_descr);
437}
438
439static inline void debug_hrtimer_free(struct hrtimer *timer)
440{
441 debug_object_free(timer, &hrtimer_debug_descr);
442}
443
444static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
445 enum hrtimer_mode mode);
446
447void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
448 enum hrtimer_mode mode)
449{
450 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
451 __hrtimer_init(timer, clock_id, mode);
452}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700453EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700454
455void destroy_hrtimer_on_stack(struct hrtimer *timer)
456{
457 debug_object_free(timer, &hrtimer_debug_descr);
458}
459
460#else
461static inline void debug_hrtimer_init(struct hrtimer *timer) { }
462static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
463static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
464#endif
465
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800466static inline void
467debug_init(struct hrtimer *timer, clockid_t clockid,
468 enum hrtimer_mode mode)
469{
470 debug_hrtimer_init(timer);
471 trace_hrtimer_init(timer, clockid, mode);
472}
473
474static inline void debug_activate(struct hrtimer *timer)
475{
476 debug_hrtimer_activate(timer);
477 trace_hrtimer_start(timer);
478}
479
480static inline void debug_deactivate(struct hrtimer *timer)
481{
482 debug_hrtimer_deactivate(timer);
483 trace_hrtimer_cancel(timer);
484}
485
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800486/* High resolution timer related functions */
487#ifdef CONFIG_HIGH_RES_TIMERS
488
489/*
490 * High resolution timer enabled ?
491 */
492static int hrtimer_hres_enabled __read_mostly = 1;
493
494/*
495 * Enable / Disable high resolution mode
496 */
497static int __init setup_hrtimer_hres(char *str)
498{
499 if (!strcmp(str, "off"))
500 hrtimer_hres_enabled = 0;
501 else if (!strcmp(str, "on"))
502 hrtimer_hres_enabled = 1;
503 else
504 return 0;
505 return 1;
506}
507
508__setup("highres=", setup_hrtimer_hres);
509
510/*
511 * hrtimer_high_res_enabled - query, if the highres mode is enabled
512 */
513static inline int hrtimer_is_hres_enabled(void)
514{
515 return hrtimer_hres_enabled;
516}
517
518/*
519 * Is the high resolution mode active ?
520 */
521static inline int hrtimer_hres_active(void)
522{
Christoph Lameter909ea962010-12-08 16:22:55 +0100523 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800524}
525
526/*
527 * Reprogram the event source with checking both queues for the
528 * next event
529 * Called with interrupts disabled and base->lock held
530 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400531static void
532hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800533{
534 int i;
535 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400536 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800537
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400538 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800539
540 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
541 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700542 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800543
John Stultz998adc32010-09-20 19:19:17 -0700544 next = timerqueue_getnext(&base->active);
545 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800546 continue;
John Stultz998adc32010-09-20 19:19:17 -0700547 timer = container_of(next, struct hrtimer, node);
548
Arjan van de Vencc584b22008-09-01 15:02:30 -0700549 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100550 /*
551 * clock_was_set() has changed base->offset so the
552 * result might be negative. Fix it up to prevent a
553 * false positive in clockevents_program_event()
554 */
555 if (expires.tv64 < 0)
556 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400557 if (expires.tv64 < expires_next.tv64)
558 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800559 }
560
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400561 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
562 return;
563
564 cpu_base->expires_next.tv64 = expires_next.tv64;
565
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800566 if (cpu_base->expires_next.tv64 != KTIME_MAX)
567 tick_program_event(cpu_base->expires_next, 1);
568}
569
570/*
571 * Shared reprogramming for clock_realtime and clock_monotonic
572 *
573 * When a timer is enqueued and expires earlier than the already enqueued
574 * timers, we have to check, whether it expires earlier than the timer for
575 * which the clock event device was armed.
576 *
577 * Called with interrupts disabled and base->cpu_base.lock held
578 */
579static int hrtimer_reprogram(struct hrtimer *timer,
580 struct hrtimer_clock_base *base)
581{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100582 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700583 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800584 int res;
585
Arjan van de Vencc584b22008-09-01 15:02:30 -0700586 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100587
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800588 /*
589 * When the callback is running, we do not reprogram the clock event
590 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200591 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800592 * reprogramming is handled either by the softirq, which called the
593 * callback or at the end of the hrtimer_interrupt.
594 */
595 if (hrtimer_callback_running(timer))
596 return 0;
597
Thomas Gleixner63070a72008-02-14 00:58:36 +0100598 /*
599 * CLOCK_REALTIME timer might be requested with an absolute
600 * expiry time which is less than base->offset. Nothing wrong
601 * about that, just avoid to call into the tick code, which
602 * has now objections against negative expiry values.
603 */
604 if (expires.tv64 < 0)
605 return -ETIME;
606
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100607 if (expires.tv64 >= cpu_base->expires_next.tv64)
608 return 0;
609
610 /*
611 * If a hang was detected in the last timer interrupt then we
612 * do not schedule a timer which is earlier than the expiry
613 * which we enforced in the hang detection. We want the system
614 * to make progress.
615 */
616 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800617 return 0;
618
619 /*
620 * Clockevents returns -ETIME, when the event was in the past.
621 */
622 res = tick_program_event(expires, 0);
623 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100624 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800625 return res;
626}
627
Ingo Molnar995f0542007-04-07 12:05:00 +0200628/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800629 * Initialize the high resolution related parts of cpu_base
630 */
631static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
632{
633 base->expires_next.tv64 = KTIME_MAX;
634 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800635}
636
637/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800638 * When High resolution timers are active, try to reprogram. Note, that in case
639 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
640 * check happens. The timer gets enqueued into the rbtree. The reprogramming
641 * and expiry check is done in the hrtimer_interrupt or in the softirq.
642 */
643static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100644 struct hrtimer_clock_base *base,
645 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800646{
647 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100648 if (wakeup) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100649 raw_spin_unlock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100650 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100651 raw_spin_lock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100652 } else
653 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
654
Peter Zijlstraca109492008-11-25 12:43:51 +0100655 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800656 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100657
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800658 return 0;
659}
660
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200661/*
662 * Retrigger next event is called after clock was set
663 *
664 * Called with interrupts disabled via on_each_cpu()
665 */
666static void retrigger_next_event(void *arg)
667{
668 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
669 struct timespec realtime_offset, xtim, wtm, sleep;
670
671 if (!hrtimer_hres_active())
672 return;
673
674 /* Optimized out for !HIGH_RES */
675 get_xtime_and_monotonic_and_sleep_offset(&xtim, &wtm, &sleep);
676 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
677
678 /* Adjust CLOCK_REALTIME offset */
679 raw_spin_lock(&base->lock);
680 base->clock_base[HRTIMER_BASE_REALTIME].offset =
681 timespec_to_ktime(realtime_offset);
682 base->clock_base[HRTIMER_BASE_BOOTTIME].offset =
683 timespec_to_ktime(sleep);
684
685 hrtimer_force_reprogram(base, 0);
686 raw_spin_unlock(&base->lock);
687}
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200688
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800689/*
690 * Switch to high resolution mode
691 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800692static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800693{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200694 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700695 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800696 unsigned long flags;
697
698 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800699 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800700
701 local_irq_save(flags);
702
703 if (tick_init_highres()) {
704 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700705 printk(KERN_WARNING "Could not switch to high resolution "
706 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800707 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800708 }
709 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200710 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
711 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800712
713 tick_setup_sched_timer();
714
715 /* "Retrigger" the interrupt to get things going */
716 retrigger_next_event(NULL);
717 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800718 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800719}
720
721#else
722
723static inline int hrtimer_hres_active(void) { return 0; }
724static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800725static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400726static inline void
727hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800728static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100729 struct hrtimer_clock_base *base,
730 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800731{
732 return 0;
733}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800734static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200735static inline void retrigger_next_event(void *arg) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800736
737#endif /* CONFIG_HIGH_RES_TIMERS */
738
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200739/*
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200740 * Clock realtime was set
741 *
742 * Change the offset of the realtime clock vs. the monotonic
743 * clock.
744 *
745 * We might have to reprogram the high resolution timer interrupt. On
746 * SMP we call the architecture specific code to retrigger _all_ high
747 * resolution timer interrupts. On UP we just disable interrupts and
748 * call the high resolution interrupt code.
749 */
750void clock_was_set(void)
751{
Thomas Gleixner90ff1f32011-05-25 23:08:17 +0200752#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200753 /* Retrigger the CPU local events everywhere */
754 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200755#endif
756 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200757}
758
759/*
760 * During resume we might have to reprogram the high resolution timer
761 * interrupt (on the local CPU):
762 */
763void hrtimers_resume(void)
764{
765 WARN_ONCE(!irqs_disabled(),
766 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
767
768 retrigger_next_event(NULL);
Thomas Gleixner9ec26902011-05-20 16:18:50 +0200769 timerfd_clock_was_set();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200770}
771
Heiko Carstens5f201902009-12-10 10:56:29 +0100772static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800773{
Heiko Carstens5f201902009-12-10 10:56:29 +0100774#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800775 if (timer->start_site)
776 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100777 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800778 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
779 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800780#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100781}
782
783static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
784{
785#ifdef CONFIG_TIMER_STATS
786 timer->start_site = NULL;
787#endif
788}
789
790static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
791{
792#ifdef CONFIG_TIMER_STATS
793 if (likely(!timer_stats_active))
794 return;
795 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
796 timer->function, timer->start_comm, 0);
797#endif
798}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800799
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800800/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200801 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800802 */
803static inline
804void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
805{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100806 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800807}
808
809/**
810 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800811 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800812 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800813 * @interval: the interval to forward
814 *
815 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700816 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800818u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800819{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800820 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800821 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800822
Arjan van de Vencc584b22008-09-01 15:02:30 -0700823 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800824
825 if (delta.tv64 < 0)
826 return 0;
827
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100828 if (interval.tv64 < timer->base->resolution.tv64)
829 interval.tv64 = timer->base->resolution.tv64;
830
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800831 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800832 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800833
834 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700835 hrtimer_add_expires_ns(timer, incr * orun);
836 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800837 return orun;
838 /*
839 * This (and the ktime_add() below) is the
840 * correction for exact:
841 */
842 orun++;
843 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700844 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800845
846 return orun;
847}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700848EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800849
850/*
851 * enqueue_hrtimer - internal function to (re)start a timer
852 *
853 * The timer is inserted in expiry order. Insertion into the
854 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100855 *
856 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800857 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100858static int enqueue_hrtimer(struct hrtimer *timer,
859 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800860{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800861 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700862
John Stultz998adc32010-09-20 19:19:17 -0700863 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200864 base->cpu_base->active_bases |= 1 << base->index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800865
866 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800867 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
868 * state of a possibly running callback.
869 */
870 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100871
John Stultz998adc32010-09-20 19:19:17 -0700872 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100873}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800874
875/*
876 * __remove_hrtimer - internal function to remove a timer
877 *
878 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800879 *
880 * High resolution timer mode reprograms the clock event device when the
881 * timer is the one which expires next. The caller can disable this by setting
882 * reprogram to zero. This is useful, when the context does a reprogramming
883 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800884 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800885static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800886 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800887 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800888{
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800889 struct timerqueue_node *next_timer;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400890 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
891 goto out;
892
Jeff Ohlstein27c9cd72011-11-18 15:47:10 -0800893 next_timer = timerqueue_getnext(&base->active);
894 timerqueue_del(&base->active, &timer->node);
895 if (&timer->node == next_timer) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400896#ifdef CONFIG_HIGH_RES_TIMERS
897 /* Reprogram the clock event device. if enabled */
898 if (reprogram && hrtimer_hres_active()) {
899 ktime_t expires;
900
901 expires = ktime_sub(hrtimer_get_expires(timer),
902 base->offset);
903 if (base->cpu_base->expires_next.tv64 == expires.tv64)
904 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800905 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400906#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800907 }
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200908 if (!timerqueue_getnext(&base->active))
909 base->cpu_base->active_bases &= ~(1 << base->index);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400910out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800911 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800912}
913
914/*
915 * remove hrtimer, called with base lock held
916 */
917static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800918remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800919{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800920 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700921 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800922 int reprogram;
923
924 /*
925 * Remove the timer and force reprogramming when high
926 * resolution mode is active and the timer is on the current
927 * CPU. If we remove a timer on another CPU, reprogramming is
928 * skipped. The interrupt event on this CPU is fired and
929 * reprogramming happens in the interrupt handler. This is a
930 * rare case and less expensive than a smp call.
931 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800932 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800933 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800934 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700935 /*
936 * We must preserve the CALLBACK state flag here,
937 * otherwise we could move the timer base in
938 * switch_hrtimer_base.
939 */
940 state = timer->state & HRTIMER_STATE_CALLBACK;
941 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800942 return 1;
943 }
944 return 0;
945}
946
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100947int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
948 unsigned long delta_ns, const enum hrtimer_mode mode,
949 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800950{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800951 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800952 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100953 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800954
955 base = lock_hrtimer_base(timer, &flags);
956
957 /* Remove an active timer from the queue: */
958 ret = remove_hrtimer(timer, base);
959
960 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530961 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800962
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530963 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100964 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800965 /*
966 * CONFIG_TIME_LOW_RES is a temporary way for architectures
967 * to signal that they simply return xtime in
968 * do_gettimeoffset(). In this case we want to round up by
969 * resolution when starting a relative timer, to avoid short
970 * timeouts. This will go away with the GTOD framework.
971 */
972#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100973 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800974#endif
975 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700976
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700977 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800978
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800979 timer_stats_hrtimer_set_start_info(timer);
980
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100981 leftmost = enqueue_hrtimer(timer, new_base);
982
Ingo Molnar935c6312007-03-28 13:17:18 +0200983 /*
984 * Only allow reprogramming if the new base is on this CPU.
985 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100986 *
987 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200988 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100989 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100990 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800991
992 unlock_hrtimer_base(timer, &flags);
993
994 return ret;
995}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100996
997/**
998 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
999 * @timer: the timer to be added
1000 * @tim: expiry time
1001 * @delta_ns: "slack" range for the timer
1002 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1003 *
1004 * Returns:
1005 * 0 on success
1006 * 1 when the timer was active
1007 */
1008int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1009 unsigned long delta_ns, const enum hrtimer_mode mode)
1010{
1011 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1012}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001013EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1014
1015/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001016 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001017 * @timer: the timer to be added
1018 * @tim: expiry time
1019 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1020 *
1021 * Returns:
1022 * 0 on success
1023 * 1 when the timer was active
1024 */
1025int
1026hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1027{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001028 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001029}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001030EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001031
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001032
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001033/**
1034 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001035 * @timer: hrtimer to stop
1036 *
1037 * Returns:
1038 * 0 when the timer was not active
1039 * 1 when the timer was active
1040 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001041 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001042 */
1043int hrtimer_try_to_cancel(struct hrtimer *timer)
1044{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001045 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001046 unsigned long flags;
1047 int ret = -1;
1048
1049 base = lock_hrtimer_base(timer, &flags);
1050
Thomas Gleixner303e9672007-02-16 01:27:51 -08001051 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001052 ret = remove_hrtimer(timer, base);
1053
1054 unlock_hrtimer_base(timer, &flags);
1055
1056 return ret;
1057
1058}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001059EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001060
1061/**
1062 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001063 * @timer: the timer to be cancelled
1064 *
1065 * Returns:
1066 * 0 when the timer was not active
1067 * 1 when the timer was active
1068 */
1069int hrtimer_cancel(struct hrtimer *timer)
1070{
1071 for (;;) {
1072 int ret = hrtimer_try_to_cancel(timer);
1073
1074 if (ret >= 0)
1075 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001076 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001077 }
1078}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001079EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001080
1081/**
1082 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001083 * @timer: the timer to read
1084 */
1085ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1086{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001087 unsigned long flags;
1088 ktime_t rem;
1089
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001090 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001091 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001092 unlock_hrtimer_base(timer, &flags);
1093
1094 return rem;
1095}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001096EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001097
Russell Kingee9c5782008-04-20 13:59:33 +01001098#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001099/**
1100 * hrtimer_get_next_event - get the time until next expiry event
1101 *
1102 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1103 * is pending.
1104 */
1105ktime_t hrtimer_get_next_event(void)
1106{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001107 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1108 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001109 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1110 unsigned long flags;
1111 int i;
1112
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001113 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001114
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001115 if (!hrtimer_hres_active()) {
1116 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1117 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001118 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001119
John Stultz998adc32010-09-20 19:19:17 -07001120 next = timerqueue_getnext(&base->active);
1121 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001122 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001123
John Stultz998adc32010-09-20 19:19:17 -07001124 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001125 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001126 delta = ktime_sub(delta, base->get_time());
1127 if (delta.tv64 < mindelta.tv64)
1128 mindelta.tv64 = delta.tv64;
1129 }
Tony Lindgren69239742006-03-06 15:42:45 -08001130 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001131
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001132 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001133
Tony Lindgren69239742006-03-06 15:42:45 -08001134 if (mindelta.tv64 < 0)
1135 mindelta.tv64 = 0;
1136 return mindelta;
1137}
1138#endif
1139
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001140static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1141 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001142{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001143 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001144 int base;
George Anzinger79786722006-02-01 03:05:11 -08001145
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001146 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001147
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001148 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001149
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001150 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001151 clock_id = CLOCK_MONOTONIC;
1152
John Stultze06383d2010-12-14 19:37:07 -08001153 base = hrtimer_clockid_to_base(clock_id);
1154 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001155 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001156
1157#ifdef CONFIG_TIMER_STATS
1158 timer->start_site = NULL;
1159 timer->start_pid = -1;
1160 memset(timer->start_comm, 0, TASK_COMM_LEN);
1161#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001162}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001163
1164/**
1165 * hrtimer_init - initialize a timer to the given clock
1166 * @timer: the timer to be initialized
1167 * @clock_id: the clock to be used
1168 * @mode: timer mode abs/rel
1169 */
1170void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1171 enum hrtimer_mode mode)
1172{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001173 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001174 __hrtimer_init(timer, clock_id, mode);
1175}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001176EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001177
1178/**
1179 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001180 * @which_clock: which clock to query
1181 * @tp: pointer to timespec variable to store the resolution
1182 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001183 * Store the resolution of the clock selected by @which_clock in the
1184 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001185 */
1186int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1187{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001188 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001189 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001190
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001191 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001192 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001193
1194 return 0;
1195}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001196EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001197
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001198static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001199{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001200 struct hrtimer_clock_base *base = timer->base;
1201 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1202 enum hrtimer_restart (*fn)(struct hrtimer *);
1203 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001204
Peter Zijlstraca109492008-11-25 12:43:51 +01001205 WARN_ON(!irqs_disabled());
1206
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001207 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001208 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1209 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001210 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001211
1212 /*
1213 * Because we run timers from hardirq context, there is no chance
1214 * they get migrated to another cpu, therefore its safe to unlock
1215 * the timer base.
1216 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001217 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001218 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001219 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001220 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001221 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001222
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001223 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001224 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1225 * we do not reprogramm the event hardware. Happens either in
1226 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001227 */
1228 if (restart != HRTIMER_NORESTART) {
1229 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001230 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001231 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001232
1233 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1234
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001235 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001236}
1237
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001238#ifdef CONFIG_HIGH_RES_TIMERS
1239
1240/*
1241 * High resolution timer interrupt
1242 * Called with interrupts disabled
1243 */
1244void hrtimer_interrupt(struct clock_event_device *dev)
1245{
1246 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001247 ktime_t expires_next, now, entry_time, delta;
1248 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001249
1250 BUG_ON(!cpu_base->hres_active);
1251 cpu_base->nr_events++;
1252 dev->next_event.tv64 = KTIME_MAX;
1253
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001254 entry_time = now = ktime_get();
1255retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001256 expires_next.tv64 = KTIME_MAX;
1257
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001258 raw_spin_lock(&cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001259 /*
1260 * We set expires_next to KTIME_MAX here with cpu_base->lock
1261 * held to prevent that a timer is enqueued in our queue via
1262 * the migration code. This does not affect enqueueing of
1263 * timers which run their callback and need to be requeued on
1264 * this CPU.
1265 */
1266 cpu_base->expires_next.tv64 = KTIME_MAX;
1267
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001268 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001269 struct hrtimer_clock_base *base;
John Stultz998adc32010-09-20 19:19:17 -07001270 struct timerqueue_node *node;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001271 ktime_t basenow;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001272
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001273 if (!(cpu_base->active_bases & (1 << i)))
1274 continue;
1275
1276 base = cpu_base->clock_base + i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001277 basenow = ktime_add(now, base->offset);
1278
John Stultz998adc32010-09-20 19:19:17 -07001279 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001280 struct hrtimer *timer;
1281
John Stultz998adc32010-09-20 19:19:17 -07001282 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001283
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001284 /*
1285 * The immediate goal for using the softexpires is
1286 * minimizing wakeups, not running timers at the
1287 * earliest interrupt after their soft expiration.
1288 * This allows us to avoid using a Priority Search
1289 * Tree, which can answer a stabbing querry for
1290 * overlapping intervals and instead use the simple
1291 * BST we already have.
1292 * We don't add extra wakeups by delaying timers that
1293 * are right-of a not yet expired timer, because that
1294 * timer will have to trigger a wakeup anyway.
1295 */
1296
1297 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001298 ktime_t expires;
1299
Arjan van de Vencc584b22008-09-01 15:02:30 -07001300 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001301 base->offset);
1302 if (expires.tv64 < expires_next.tv64)
1303 expires_next = expires;
1304 break;
1305 }
1306
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001307 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001308 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001309 }
1310
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001311 /*
1312 * Store the new expiry value so the migration code can verify
1313 * against it.
1314 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001315 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001316 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001317
1318 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001319 if (expires_next.tv64 == KTIME_MAX ||
1320 !tick_program_event(expires_next, 0)) {
1321 cpu_base->hang_detected = 0;
1322 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001323 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001324
1325 /*
1326 * The next timer was already expired due to:
1327 * - tracing
1328 * - long lasting callbacks
1329 * - being scheduled away when running in a VM
1330 *
1331 * We need to prevent that we loop forever in the hrtimer
1332 * interrupt routine. We give it 3 attempts to avoid
1333 * overreacting on some spurious event.
1334 */
1335 now = ktime_get();
1336 cpu_base->nr_retries++;
1337 if (++retries < 3)
1338 goto retry;
1339 /*
1340 * Give the system a chance to do something else than looping
1341 * here. We stored the entry time, so we know exactly how long
1342 * we spent here. We schedule the next event this amount of
1343 * time away.
1344 */
1345 cpu_base->nr_hangs++;
1346 cpu_base->hang_detected = 1;
1347 delta = ktime_sub(now, entry_time);
1348 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1349 cpu_base->max_hang_time = delta;
1350 /*
1351 * Limit it to a sensible value as we enforce a longer
1352 * delay. Give the CPU at least 100ms to catch up.
1353 */
1354 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1355 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1356 else
1357 expires_next = ktime_add(now, delta);
1358 tick_program_event(expires_next, 1);
1359 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1360 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001361}
1362
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001363/*
1364 * local version of hrtimer_peek_ahead_timers() called with interrupts
1365 * disabled.
1366 */
1367static void __hrtimer_peek_ahead_timers(void)
1368{
1369 struct tick_device *td;
1370
1371 if (!hrtimer_hres_active())
1372 return;
1373
1374 td = &__get_cpu_var(tick_cpu_device);
1375 if (td && td->evtdev)
1376 hrtimer_interrupt(td->evtdev);
1377}
1378
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001379/**
1380 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1381 *
1382 * hrtimer_peek_ahead_timers will peek at the timer queue of
1383 * the current cpu and check if there are any timers for which
1384 * the soft expires time has passed. If any such timers exist,
1385 * they are run immediately and then removed from the timer queue.
1386 *
1387 */
1388void hrtimer_peek_ahead_timers(void)
1389{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001390 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001391
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001392 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001393 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001394 local_irq_restore(flags);
1395}
1396
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001397static void run_hrtimer_softirq(struct softirq_action *h)
1398{
1399 hrtimer_peek_ahead_timers();
1400}
1401
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001402#else /* CONFIG_HIGH_RES_TIMERS */
1403
1404static inline void __hrtimer_peek_ahead_timers(void) { }
1405
1406#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001407
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001408/*
1409 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001410 *
1411 * For HRT its the fall back code to run the softirq in the timer
1412 * softirq context in case the hrtimer initialization failed or has
1413 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001414 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001415void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001416{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001417 if (hrtimer_hres_active())
1418 return;
1419
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001420 /*
1421 * This _is_ ugly: We have to check in the softirq context,
1422 * whether we can switch to highres and / or nohz mode. The
1423 * clocksource switch happens in the timer interrupt with
1424 * xtime_lock held. Notification from there only sets the
1425 * check bit in the tick_oneshot code, otherwise we might
1426 * deadlock vs. xtime_lock.
1427 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001428 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001429 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001430}
1431
1432/*
1433 * Called from hardirq context every jiffy
1434 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001435void hrtimer_run_queues(void)
1436{
John Stultz998adc32010-09-20 19:19:17 -07001437 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001438 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001439 struct hrtimer_clock_base *base;
1440 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001441
1442 if (hrtimer_hres_active())
1443 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001444
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001445 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1446 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001447 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001448 continue;
1449
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001450 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001451 hrtimer_get_softirq_time(cpu_base);
1452 gettime = 0;
1453 }
1454
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001455 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001456
John Stultzb007c382010-12-10 22:19:53 -08001457 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001458 struct hrtimer *timer;
1459
John Stultz998adc32010-09-20 19:19:17 -07001460 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001461 if (base->softirq_time.tv64 <=
1462 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001463 break;
1464
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001465 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001466 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001467 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001468 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001469}
1470
1471/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001472 * Sleep related functions:
1473 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001474static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001475{
1476 struct hrtimer_sleeper *t =
1477 container_of(timer, struct hrtimer_sleeper, timer);
1478 struct task_struct *task = t->task;
1479
1480 t->task = NULL;
1481 if (task)
1482 wake_up_process(task);
1483
1484 return HRTIMER_NORESTART;
1485}
1486
Ingo Molnar36c8b582006-07-03 00:25:41 -07001487void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001488{
1489 sl->timer.function = hrtimer_wakeup;
1490 sl->task = task;
1491}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001492EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001493
Thomas Gleixner669d7862006-03-31 02:31:19 -08001494static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001495{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001496 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001497
Roman Zippel432569b2006-03-26 01:38:08 -08001498 do {
1499 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001500 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001501 if (!hrtimer_active(&t->timer))
1502 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001503
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001504 if (likely(t->task))
1505 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001506
Thomas Gleixner669d7862006-03-31 02:31:19 -08001507 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001508 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001509
Thomas Gleixner669d7862006-03-31 02:31:19 -08001510 } while (t->task && !signal_pending(current));
1511
Peter Zijlstra3588a082008-02-01 17:45:13 +01001512 __set_current_state(TASK_RUNNING);
1513
Thomas Gleixner669d7862006-03-31 02:31:19 -08001514 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001515}
1516
Oleg Nesterov080344b2008-02-01 17:29:05 +03001517static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1518{
1519 struct timespec rmt;
1520 ktime_t rem;
1521
Arjan van de Vencc584b22008-09-01 15:02:30 -07001522 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001523 if (rem.tv64 <= 0)
1524 return 0;
1525 rmt = ktime_to_timespec(rem);
1526
1527 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1528 return -EFAULT;
1529
1530 return 1;
1531}
1532
Toyo Abe1711ef32006-09-29 02:00:28 -07001533long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001534{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001535 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001536 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001537 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001538
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001539 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001540 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001541 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001542
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001543 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001544 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001545
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001546 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001547 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001548 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001549 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001550 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001551 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001552
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001553 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001554 ret = -ERESTART_RESTARTBLOCK;
1555out:
1556 destroy_hrtimer_on_stack(&t.timer);
1557 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001558}
1559
Oleg Nesterov080344b2008-02-01 17:29:05 +03001560long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001561 const enum hrtimer_mode mode, const clockid_t clockid)
1562{
1563 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001564 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001565 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001566 unsigned long slack;
1567
1568 slack = current->timer_slack_ns;
1569 if (rt_task(current))
1570 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001571
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001572 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001573 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001574 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001575 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001576
George Anzinger79786722006-02-01 03:05:11 -08001577 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001578 if (mode == HRTIMER_MODE_ABS) {
1579 ret = -ERESTARTNOHAND;
1580 goto out;
1581 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001582
Roman Zippel432569b2006-03-26 01:38:08 -08001583 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001584 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001585 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001586 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001587 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001588
1589 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001590 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001591 restart->nanosleep.clockid = t.timer.base->clockid;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001592 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001593 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001594
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001595 ret = -ERESTART_RESTARTBLOCK;
1596out:
1597 destroy_hrtimer_on_stack(&t.timer);
1598 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001599}
1600
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001601SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1602 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001603{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001604 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001605
1606 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1607 return -EFAULT;
1608
1609 if (!timespec_valid(&tu))
1610 return -EINVAL;
1611
Oleg Nesterov080344b2008-02-01 17:29:05 +03001612 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001613}
1614
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001615/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001616 * Functions related to boot-time initialization:
1617 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001618static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001619{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001620 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001621 int i;
1622
John Stultz998adc32010-09-20 19:19:17 -07001623 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001624 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001625 timerqueue_init_head(&cpu_base->clock_base[i].active);
1626 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001627
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001628 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001629}
1630
1631#ifdef CONFIG_HOTPLUG_CPU
1632
Peter Zijlstraca109492008-11-25 12:43:51 +01001633static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001634 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001635{
1636 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001637 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001638
John Stultz998adc32010-09-20 19:19:17 -07001639 while ((node = timerqueue_getnext(&old_base->active))) {
1640 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001641 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001642 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001643
1644 /*
1645 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1646 * timer could be seen as !active and just vanish away
1647 * under us on another CPU
1648 */
1649 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001650 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001651 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001652 * Enqueue the timers on the new cpu. This does not
1653 * reprogram the event device in case the timer
1654 * expires before the earliest on this CPU, but we run
1655 * hrtimer_interrupt after we migrated everything to
1656 * sort out already expired timers and reprogram the
1657 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001658 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001659 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001660
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001661 /* Clear the migration state bit */
1662 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001663 }
1664}
1665
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001666static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001667{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001668 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001669 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001670
Peter Zijlstra37810652008-12-04 11:17:10 +01001671 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001672 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001673
1674 local_irq_disable();
1675 old_base = &per_cpu(hrtimer_bases, scpu);
1676 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001677 /*
1678 * The caller is globally serialized and nobody else
1679 * takes two locks at once, deadlock is not possible.
1680 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001681 raw_spin_lock(&new_base->lock);
1682 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001683
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001684 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001685 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001686 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001687 }
1688
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001689 raw_spin_unlock(&old_base->lock);
1690 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001691
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001692 /* Check, if we got expired work to do */
1693 __hrtimer_peek_ahead_timers();
1694 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001695}
1696
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001697#endif /* CONFIG_HOTPLUG_CPU */
1698
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001699static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001700 unsigned long action, void *hcpu)
1701{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001702 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001703
1704 switch (action) {
1705
1706 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001707 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001708 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001709 break;
1710
1711#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001712 case CPU_DYING:
1713 case CPU_DYING_FROZEN:
1714 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1715 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001716 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001717 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001718 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001719 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001720 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001721 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001722 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001723#endif
1724
1725 default:
1726 break;
1727 }
1728
1729 return NOTIFY_OK;
1730}
1731
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001732static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001733 .notifier_call = hrtimer_cpu_notify,
1734};
1735
1736void __init hrtimers_init(void)
1737{
1738 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1739 (void *)(long)smp_processor_id());
1740 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001741#ifdef CONFIG_HIGH_RES_TIMERS
1742 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1743#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001744}
1745
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001746/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001747 * schedule_hrtimeout_range_clock - sleep until timeout
1748 * @expires: timeout value (ktime_t)
1749 * @delta: slack in expires timeout (ktime_t)
1750 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1751 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1752 */
1753int __sched
1754schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1755 const enum hrtimer_mode mode, int clock)
1756{
1757 struct hrtimer_sleeper t;
1758
1759 /*
1760 * Optimize when a zero timeout value is given. It does not
1761 * matter whether this is an absolute or a relative time.
1762 */
1763 if (expires && !expires->tv64) {
1764 __set_current_state(TASK_RUNNING);
1765 return 0;
1766 }
1767
1768 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001769 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001770 */
1771 if (!expires) {
1772 schedule();
1773 __set_current_state(TASK_RUNNING);
1774 return -EINTR;
1775 }
1776
1777 hrtimer_init_on_stack(&t.timer, clock, mode);
1778 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1779
1780 hrtimer_init_sleeper(&t, current);
1781
1782 hrtimer_start_expires(&t.timer, mode);
1783 if (!hrtimer_active(&t.timer))
1784 t.task = NULL;
1785
1786 if (likely(t.task))
1787 schedule();
1788
1789 hrtimer_cancel(&t.timer);
1790 destroy_hrtimer_on_stack(&t.timer);
1791
1792 __set_current_state(TASK_RUNNING);
1793
1794 return !t.task ? 0 : -EINTR;
1795}
1796
1797/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001798 * schedule_hrtimeout_range - sleep until timeout
1799 * @expires: timeout value (ktime_t)
1800 * @delta: slack in expires timeout (ktime_t)
1801 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1802 *
1803 * Make the current task sleep until the given expiry time has
1804 * elapsed. The routine will return immediately unless
1805 * the current task state has been set (see set_current_state()).
1806 *
1807 * The @delta argument gives the kernel the freedom to schedule the
1808 * actual wakeup to a time that is both power and performance friendly.
1809 * The kernel give the normal best effort behavior for "@expires+@delta",
1810 * but may decide to fire the timer earlier, but no earlier than @expires.
1811 *
1812 * You can set the task state as follows -
1813 *
1814 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1815 * pass before the routine returns.
1816 *
1817 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1818 * delivered to the current task.
1819 *
1820 * The current task state is guaranteed to be TASK_RUNNING when this
1821 * routine returns.
1822 *
1823 * Returns 0 when the timer has expired otherwise -EINTR
1824 */
1825int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001826 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001827{
Carsten Emde351b3f72010-04-02 22:40:19 +02001828 return schedule_hrtimeout_range_clock(expires, delta, mode,
1829 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001830}
1831EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1832
1833/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001834 * schedule_hrtimeout - sleep until timeout
1835 * @expires: timeout value (ktime_t)
1836 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1837 *
1838 * Make the current task sleep until the given expiry time has
1839 * elapsed. The routine will return immediately unless
1840 * the current task state has been set (see set_current_state()).
1841 *
1842 * You can set the task state as follows -
1843 *
1844 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1845 * pass before the routine returns.
1846 *
1847 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1848 * delivered to the current task.
1849 *
1850 * The current task state is guaranteed to be TASK_RUNNING when this
1851 * routine returns.
1852 *
1853 * Returns 0 when the timer has expired otherwise -EINTR
1854 */
1855int __sched schedule_hrtimeout(ktime_t *expires,
1856 const enum hrtimer_mode mode)
1857{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001858 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001859}
1860EXPORT_SYMBOL_GPL(schedule_hrtimeout);