blob: c145ed643bcaee73db1a97f7d3c5f189088320da [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>
35#include <linux/module.h>
36#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 Anzinger7978672c2006-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
64 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080065 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080066 {
67 .index = CLOCK_REALTIME,
68 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080069 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080070 },
71 {
72 .index = CLOCK_MONOTONIC,
73 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080074 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080075 },
John Stultz70a08cc2011-02-15 10:45:16 -080076 {
77 .index = CLOCK_BOOTTIME,
78 .get_time = &ktime_get_boottime,
79 .resolution = KTIME_LOW_RES,
80 },
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080081 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080082};
83
Mike Frysinger942c3c52011-05-02 15:24:27 -040084static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
Thomas Gleixnerce313322011-04-29 00:02:00 +020085 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
86 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
87 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
88};
John Stultze06383d2010-12-14 19:37:07 -080089
90static inline int hrtimer_clockid_to_base(clockid_t clock_id)
91{
92 return hrtimer_clock_to_base_table[clock_id];
93}
94
95
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080096/*
Thomas Gleixner92127c72006-03-26 01:38:05 -080097 * Get the coarse grained time at the softirq based on xtime and
98 * wall_to_monotonic.
99 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800100static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800101{
John Stultz70a08cc2011-02-15 10:45:16 -0800102 ktime_t xtim, mono, boot;
John Stultz314ac372011-02-14 18:43:08 -0800103 struct timespec xts, tom, slp;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800104
John Stultz314ac372011-02-14 18:43:08 -0800105 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800106
john stultzf4304ab2007-02-16 01:27:26 -0800107 xtim = timespec_to_ktime(xts);
John Stultz70a08cc2011-02-15 10:45:16 -0800108 mono = ktime_add(xtim, timespec_to_ktime(tom));
109 boot = ktime_add(mono, timespec_to_ktime(slp));
John Stultze06383d2010-12-14 19:37:07 -0800110 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
John Stultz70a08cc2011-02-15 10:45:16 -0800111 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
112 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800113}
114
115/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800116 * Functions and macros which are different for UP/SMP systems are kept in a
117 * single place
118 */
119#ifdef CONFIG_SMP
120
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800121/*
122 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
123 * means that all timers which are tied to this base via timer->base are
124 * locked, and the base itself is locked too.
125 *
126 * So __run_timers/migrate_timers can safely modify all timers which could
127 * be found on the lists/queues.
128 *
129 * When the timer's base is locked, and the timer removed from list, it is
130 * possible to set timer->base = NULL and drop the lock: the timer remains
131 * locked.
132 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800133static
134struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
135 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800136{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800137 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800138
139 for (;;) {
140 base = timer->base;
141 if (likely(base != NULL)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100142 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800143 if (likely(base == timer->base))
144 return base;
145 /* The timer has migrated to another CPU: */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100146 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800147 }
148 cpu_relax();
149 }
150}
151
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200152
153/*
154 * Get the preferred target CPU for NOHZ
155 */
156static int hrtimer_get_target(int this_cpu, int pinned)
157{
158#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700159 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
160 return get_nohz_timer_target();
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200161#endif
162 return this_cpu;
163}
164
165/*
166 * With HIGHRES=y we do not migrate the timer when it is expiring
167 * before the next event on the target cpu because we cannot reprogram
168 * the target cpu hardware and we would cause it to fire late.
169 *
170 * Called with cpu_base->lock of target cpu held.
171 */
172static int
173hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
174{
175#ifdef CONFIG_HIGH_RES_TIMERS
176 ktime_t expires;
177
178 if (!new_base->cpu_base->hres_active)
179 return 0;
180
181 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
182 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
183#else
184 return 0;
185#endif
186}
187
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800188/*
189 * Switch the timer base to the current CPU when possible.
190 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800191static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530192switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
193 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800194{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800195 struct hrtimer_clock_base *new_base;
196 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200197 int this_cpu = smp_processor_id();
198 int cpu = hrtimer_get_target(this_cpu, pinned);
John Stultze06383d2010-12-14 19:37:07 -0800199 int basenum = hrtimer_clockid_to_base(base->index);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530200
201again:
202 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
John Stultze06383d2010-12-14 19:37:07 -0800203 new_base = &new_cpu_base->clock_base[basenum];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800204
205 if (base != new_base) {
206 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200207 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800208 * However we can't change timer's base while it is running,
209 * so we keep it on the same CPU. No hassle vs. reprogramming
210 * the event source in the high resolution case. The softirq
211 * code will take care of this when the timer function has
212 * completed. There is no conflict as we hold the lock until
213 * the timer is enqueued.
214 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800215 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800216 return base;
217
218 /* See the comment in lock_timer_base() */
219 timer->base = NULL;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100220 raw_spin_unlock(&base->cpu_base->lock);
221 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530222
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200223 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
224 cpu = this_cpu;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100225 raw_spin_unlock(&new_base->cpu_base->lock);
226 raw_spin_lock(&base->cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200227 timer->base = base;
228 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530229 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800230 timer->base = new_base;
231 }
232 return new_base;
233}
234
235#else /* CONFIG_SMP */
236
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800237static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800238lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
239{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800240 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800241
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100242 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800243
244 return base;
245}
246
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530247# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800248
249#endif /* !CONFIG_SMP */
250
251/*
252 * Functions for the union type storage format of ktime_t which are
253 * too large for inlining:
254 */
255#if BITS_PER_LONG < 64
256# ifndef CONFIG_KTIME_SCALAR
257/**
258 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800259 * @kt: addend
260 * @nsec: the scalar nsec value to add
261 *
262 * Returns the sum of kt and nsec in ktime_t format
263 */
264ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
265{
266 ktime_t tmp;
267
268 if (likely(nsec < NSEC_PER_SEC)) {
269 tmp.tv64 = nsec;
270 } else {
271 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
272
273 tmp = ktime_set((long)nsec, rem);
274 }
275
276 return ktime_add(kt, tmp);
277}
David Howellsb8b8fd22007-04-27 15:31:24 -0700278
279EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700280
281/**
282 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
283 * @kt: minuend
284 * @nsec: the scalar nsec value to subtract
285 *
286 * Returns the subtraction of @nsec from @kt in ktime_t format
287 */
288ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
289{
290 ktime_t tmp;
291
292 if (likely(nsec < NSEC_PER_SEC)) {
293 tmp.tv64 = nsec;
294 } else {
295 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
296
297 tmp = ktime_set((long)nsec, rem);
298 }
299
300 return ktime_sub(kt, tmp);
301}
302
303EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800304# endif /* !CONFIG_KTIME_SCALAR */
305
306/*
307 * Divide a ktime value by a nanosecond value
308 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800309u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800310{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300311 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800312 int sft = 0;
313
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300314 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800315 /* Make sure the divisor is less than 2^32: */
316 while (div >> 32) {
317 sft++;
318 div >>= 1;
319 }
320 dclc >>= sft;
321 do_div(dclc, (unsigned long) div);
322
Davide Libenzi4d672e72008-02-04 22:27:26 -0800323 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800324}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800325#endif /* BITS_PER_LONG >= 64 */
326
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100327/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100328 * Add two ktime values and do a safety check for overflow:
329 */
330ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
331{
332 ktime_t res = ktime_add(lhs, rhs);
333
334 /*
335 * We use KTIME_SEC_MAX here, the maximum timeout which we can
336 * return to user space in a timespec:
337 */
338 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
339 res = ktime_set(KTIME_SEC_MAX, 0);
340
341 return res;
342}
343
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300344EXPORT_SYMBOL_GPL(ktime_add_safe);
345
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700346#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
347
348static struct debug_obj_descr hrtimer_debug_descr;
349
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100350static void *hrtimer_debug_hint(void *addr)
351{
352 return ((struct hrtimer *) addr)->function;
353}
354
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700355/*
356 * fixup_init is called when:
357 * - an active object is initialized
358 */
359static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
360{
361 struct hrtimer *timer = addr;
362
363 switch (state) {
364 case ODEBUG_STATE_ACTIVE:
365 hrtimer_cancel(timer);
366 debug_object_init(timer, &hrtimer_debug_descr);
367 return 1;
368 default:
369 return 0;
370 }
371}
372
373/*
374 * fixup_activate is called when:
375 * - an active object is activated
376 * - an unknown object is activated (might be a statically initialized object)
377 */
378static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
379{
380 switch (state) {
381
382 case ODEBUG_STATE_NOTAVAILABLE:
383 WARN_ON_ONCE(1);
384 return 0;
385
386 case ODEBUG_STATE_ACTIVE:
387 WARN_ON(1);
388
389 default:
390 return 0;
391 }
392}
393
394/*
395 * fixup_free is called when:
396 * - an active object is freed
397 */
398static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
399{
400 struct hrtimer *timer = addr;
401
402 switch (state) {
403 case ODEBUG_STATE_ACTIVE:
404 hrtimer_cancel(timer);
405 debug_object_free(timer, &hrtimer_debug_descr);
406 return 1;
407 default:
408 return 0;
409 }
410}
411
412static struct debug_obj_descr hrtimer_debug_descr = {
413 .name = "hrtimer",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100414 .debug_hint = hrtimer_debug_hint,
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700415 .fixup_init = hrtimer_fixup_init,
416 .fixup_activate = hrtimer_fixup_activate,
417 .fixup_free = hrtimer_fixup_free,
418};
419
420static inline void debug_hrtimer_init(struct hrtimer *timer)
421{
422 debug_object_init(timer, &hrtimer_debug_descr);
423}
424
425static inline void debug_hrtimer_activate(struct hrtimer *timer)
426{
427 debug_object_activate(timer, &hrtimer_debug_descr);
428}
429
430static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
431{
432 debug_object_deactivate(timer, &hrtimer_debug_descr);
433}
434
435static inline void debug_hrtimer_free(struct hrtimer *timer)
436{
437 debug_object_free(timer, &hrtimer_debug_descr);
438}
439
440static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
441 enum hrtimer_mode mode);
442
443void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
444 enum hrtimer_mode mode)
445{
446 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
447 __hrtimer_init(timer, clock_id, mode);
448}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700449EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700450
451void destroy_hrtimer_on_stack(struct hrtimer *timer)
452{
453 debug_object_free(timer, &hrtimer_debug_descr);
454}
455
456#else
457static inline void debug_hrtimer_init(struct hrtimer *timer) { }
458static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
459static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
460#endif
461
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800462static inline void
463debug_init(struct hrtimer *timer, clockid_t clockid,
464 enum hrtimer_mode mode)
465{
466 debug_hrtimer_init(timer);
467 trace_hrtimer_init(timer, clockid, mode);
468}
469
470static inline void debug_activate(struct hrtimer *timer)
471{
472 debug_hrtimer_activate(timer);
473 trace_hrtimer_start(timer);
474}
475
476static inline void debug_deactivate(struct hrtimer *timer)
477{
478 debug_hrtimer_deactivate(timer);
479 trace_hrtimer_cancel(timer);
480}
481
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800482/* High resolution timer related functions */
483#ifdef CONFIG_HIGH_RES_TIMERS
484
485/*
486 * High resolution timer enabled ?
487 */
488static int hrtimer_hres_enabled __read_mostly = 1;
489
490/*
491 * Enable / Disable high resolution mode
492 */
493static int __init setup_hrtimer_hres(char *str)
494{
495 if (!strcmp(str, "off"))
496 hrtimer_hres_enabled = 0;
497 else if (!strcmp(str, "on"))
498 hrtimer_hres_enabled = 1;
499 else
500 return 0;
501 return 1;
502}
503
504__setup("highres=", setup_hrtimer_hres);
505
506/*
507 * hrtimer_high_res_enabled - query, if the highres mode is enabled
508 */
509static inline int hrtimer_is_hres_enabled(void)
510{
511 return hrtimer_hres_enabled;
512}
513
514/*
515 * Is the high resolution mode active ?
516 */
517static inline int hrtimer_hres_active(void)
518{
Christoph Lameter909ea962010-12-08 16:22:55 +0100519 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800520}
521
522/*
523 * Reprogram the event source with checking both queues for the
524 * next event
525 * Called with interrupts disabled and base->lock held
526 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400527static void
528hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800529{
530 int i;
531 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400532 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800533
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400534 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800535
536 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
537 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700538 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800539
John Stultz998adc32010-09-20 19:19:17 -0700540 next = timerqueue_getnext(&base->active);
541 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800542 continue;
John Stultz998adc32010-09-20 19:19:17 -0700543 timer = container_of(next, struct hrtimer, node);
544
Arjan van de Vencc584b22008-09-01 15:02:30 -0700545 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100546 /*
547 * clock_was_set() has changed base->offset so the
548 * result might be negative. Fix it up to prevent a
549 * false positive in clockevents_program_event()
550 */
551 if (expires.tv64 < 0)
552 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400553 if (expires.tv64 < expires_next.tv64)
554 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800555 }
556
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400557 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
558 return;
559
560 cpu_base->expires_next.tv64 = expires_next.tv64;
561
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800562 if (cpu_base->expires_next.tv64 != KTIME_MAX)
563 tick_program_event(cpu_base->expires_next, 1);
564}
565
566/*
567 * Shared reprogramming for clock_realtime and clock_monotonic
568 *
569 * When a timer is enqueued and expires earlier than the already enqueued
570 * timers, we have to check, whether it expires earlier than the timer for
571 * which the clock event device was armed.
572 *
573 * Called with interrupts disabled and base->cpu_base.lock held
574 */
575static int hrtimer_reprogram(struct hrtimer *timer,
576 struct hrtimer_clock_base *base)
577{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100578 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700579 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800580 int res;
581
Arjan van de Vencc584b22008-09-01 15:02:30 -0700582 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100583
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800584 /*
585 * When the callback is running, we do not reprogram the clock event
586 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200587 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800588 * reprogramming is handled either by the softirq, which called the
589 * callback or at the end of the hrtimer_interrupt.
590 */
591 if (hrtimer_callback_running(timer))
592 return 0;
593
Thomas Gleixner63070a72008-02-14 00:58:36 +0100594 /*
595 * CLOCK_REALTIME timer might be requested with an absolute
596 * expiry time which is less than base->offset. Nothing wrong
597 * about that, just avoid to call into the tick code, which
598 * has now objections against negative expiry values.
599 */
600 if (expires.tv64 < 0)
601 return -ETIME;
602
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100603 if (expires.tv64 >= cpu_base->expires_next.tv64)
604 return 0;
605
606 /*
607 * If a hang was detected in the last timer interrupt then we
608 * do not schedule a timer which is earlier than the expiry
609 * which we enforced in the hang detection. We want the system
610 * to make progress.
611 */
612 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800613 return 0;
614
615 /*
616 * Clockevents returns -ETIME, when the event was in the past.
617 */
618 res = tick_program_event(expires, 0);
619 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100620 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800621 return res;
622}
623
Ingo Molnar995f0542007-04-07 12:05:00 +0200624/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800625 * Initialize the high resolution related parts of cpu_base
626 */
627static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
628{
629 base->expires_next.tv64 = KTIME_MAX;
630 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800631}
632
633/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800634 * When High resolution timers are active, try to reprogram. Note, that in case
635 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
636 * check happens. The timer gets enqueued into the rbtree. The reprogramming
637 * and expiry check is done in the hrtimer_interrupt or in the softirq.
638 */
639static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100640 struct hrtimer_clock_base *base,
641 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800642{
643 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100644 if (wakeup) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100645 raw_spin_unlock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100646 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100647 raw_spin_lock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100648 } else
649 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
650
Peter Zijlstraca109492008-11-25 12:43:51 +0100651 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800652 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100653
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800654 return 0;
655}
656
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200657static void retrigger_next_event(void *arg);
658
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800659/*
660 * Switch to high resolution mode
661 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800662static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800663{
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200664 int i, cpu = smp_processor_id();
Ingo Molnar820de5c2007-07-21 04:37:36 -0700665 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800666 unsigned long flags;
667
668 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800669 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800670
671 local_irq_save(flags);
672
673 if (tick_init_highres()) {
674 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700675 printk(KERN_WARNING "Could not switch to high resolution "
676 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800677 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800678 }
679 base->hres_active = 1;
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200680 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
681 base->clock_base[i].resolution = KTIME_HIGH_RES;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800682
683 tick_setup_sched_timer();
684
685 /* "Retrigger" the interrupt to get things going */
686 retrigger_next_event(NULL);
687 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800688 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800689}
690
691#else
692
693static inline int hrtimer_hres_active(void) { return 0; }
694static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800695static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400696static inline void
697hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800698static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100699 struct hrtimer_clock_base *base,
700 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800701{
702 return 0;
703}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800704static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800705
706#endif /* CONFIG_HIGH_RES_TIMERS */
707
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200708/*
709 * Retrigger next event is called after clock was set
710 *
711 * Called with interrupts disabled via on_each_cpu()
712 */
713static void retrigger_next_event(void *arg)
714{
715 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
716 struct timespec realtime_offset, xtim, wtm, sleep;
717
718 if (!hrtimer_hres_active())
719 return;
720
721 get_xtime_and_monotonic_and_sleep_offset(&xtim, &wtm, &sleep);
722 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
723
724 /* Adjust CLOCK_REALTIME offset */
725 raw_spin_lock(&base->lock);
726 base->clock_base[HRTIMER_BASE_REALTIME].offset =
727 timespec_to_ktime(realtime_offset);
728 base->clock_base[HRTIMER_BASE_BOOTTIME].offset =
729 timespec_to_ktime(sleep);
730
731 hrtimer_force_reprogram(base, 0);
732 raw_spin_unlock(&base->lock);
733}
734
735/*
736 * Clock realtime was set
737 *
738 * Change the offset of the realtime clock vs. the monotonic
739 * clock.
740 *
741 * We might have to reprogram the high resolution timer interrupt. On
742 * SMP we call the architecture specific code to retrigger _all_ high
743 * resolution timer interrupts. On UP we just disable interrupts and
744 * call the high resolution interrupt code.
745 */
746void clock_was_set(void)
747{
748 /* Retrigger the CPU local events everywhere */
749 on_each_cpu(retrigger_next_event, NULL, 1);
750}
751
752/*
753 * During resume we might have to reprogram the high resolution timer
754 * interrupt (on the local CPU):
755 */
756void hrtimers_resume(void)
757{
758 WARN_ONCE(!irqs_disabled(),
759 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
760
761 retrigger_next_event(NULL);
762}
763
Heiko Carstens5f201902009-12-10 10:56:29 +0100764static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800765{
Heiko Carstens5f201902009-12-10 10:56:29 +0100766#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800767 if (timer->start_site)
768 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100769 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800770 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
771 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800772#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100773}
774
775static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
776{
777#ifdef CONFIG_TIMER_STATS
778 timer->start_site = NULL;
779#endif
780}
781
782static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
783{
784#ifdef CONFIG_TIMER_STATS
785 if (likely(!timer_stats_active))
786 return;
787 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
788 timer->function, timer->start_comm, 0);
789#endif
790}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800791
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800792/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200793 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800794 */
795static inline
796void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
797{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100798 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800799}
800
801/**
802 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800803 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800804 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800805 * @interval: the interval to forward
806 *
807 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700808 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800809 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800810u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800811{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800812 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800813 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800814
Arjan van de Vencc584b22008-09-01 15:02:30 -0700815 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800816
817 if (delta.tv64 < 0)
818 return 0;
819
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100820 if (interval.tv64 < timer->base->resolution.tv64)
821 interval.tv64 = timer->base->resolution.tv64;
822
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800824 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800825
826 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700827 hrtimer_add_expires_ns(timer, incr * orun);
828 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800829 return orun;
830 /*
831 * This (and the ktime_add() below) is the
832 * correction for exact:
833 */
834 orun++;
835 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700836 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800837
838 return orun;
839}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700840EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800841
842/*
843 * enqueue_hrtimer - internal function to (re)start a timer
844 *
845 * The timer is inserted in expiry order. Insertion into the
846 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100847 *
848 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800849 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100850static int enqueue_hrtimer(struct hrtimer *timer,
851 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800853 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700854
John Stultz998adc32010-09-20 19:19:17 -0700855 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800856
857 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800858 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
859 * state of a possibly running callback.
860 */
861 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100862
John Stultz998adc32010-09-20 19:19:17 -0700863 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100864}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800865
866/*
867 * __remove_hrtimer - internal function to remove a timer
868 *
869 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800870 *
871 * High resolution timer mode reprograms the clock event device when the
872 * timer is the one which expires next. The caller can disable this by setting
873 * reprogram to zero. This is useful, when the context does a reprogramming
874 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800875 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800876static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800877 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800878 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800879{
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400880 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
881 goto out;
882
John Stultz998adc32010-09-20 19:19:17 -0700883 if (&timer->node == timerqueue_getnext(&base->active)) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400884#ifdef CONFIG_HIGH_RES_TIMERS
885 /* Reprogram the clock event device. if enabled */
886 if (reprogram && hrtimer_hres_active()) {
887 ktime_t expires;
888
889 expires = ktime_sub(hrtimer_get_expires(timer),
890 base->offset);
891 if (base->cpu_base->expires_next.tv64 == expires.tv64)
892 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800893 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400894#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800895 }
John Stultz998adc32010-09-20 19:19:17 -0700896 timerqueue_del(&base->active, &timer->node);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400897out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800898 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800899}
900
901/*
902 * remove hrtimer, called with base lock held
903 */
904static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800905remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800906{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800907 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700908 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800909 int reprogram;
910
911 /*
912 * Remove the timer and force reprogramming when high
913 * resolution mode is active and the timer is on the current
914 * CPU. If we remove a timer on another CPU, reprogramming is
915 * skipped. The interrupt event on this CPU is fired and
916 * reprogramming happens in the interrupt handler. This is a
917 * rare case and less expensive than a smp call.
918 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800919 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800920 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800921 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700922 /*
923 * We must preserve the CALLBACK state flag here,
924 * otherwise we could move the timer base in
925 * switch_hrtimer_base.
926 */
927 state = timer->state & HRTIMER_STATE_CALLBACK;
928 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800929 return 1;
930 }
931 return 0;
932}
933
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100934int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
935 unsigned long delta_ns, const enum hrtimer_mode mode,
936 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800937{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800938 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800939 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100940 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800941
942 base = lock_hrtimer_base(timer, &flags);
943
944 /* Remove an active timer from the queue: */
945 ret = remove_hrtimer(timer, base);
946
947 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530948 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800949
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530950 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100951 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800952 /*
953 * CONFIG_TIME_LOW_RES is a temporary way for architectures
954 * to signal that they simply return xtime in
955 * do_gettimeoffset(). In this case we want to round up by
956 * resolution when starting a relative timer, to avoid short
957 * timeouts. This will go away with the GTOD framework.
958 */
959#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100960 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800961#endif
962 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700963
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700964 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800965
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800966 timer_stats_hrtimer_set_start_info(timer);
967
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100968 leftmost = enqueue_hrtimer(timer, new_base);
969
Ingo Molnar935c6312007-03-28 13:17:18 +0200970 /*
971 * Only allow reprogramming if the new base is on this CPU.
972 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100973 *
974 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200975 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100976 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100977 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800978
979 unlock_hrtimer_base(timer, &flags);
980
981 return ret;
982}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100983
984/**
985 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
986 * @timer: the timer to be added
987 * @tim: expiry time
988 * @delta_ns: "slack" range for the timer
989 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
990 *
991 * Returns:
992 * 0 on success
993 * 1 when the timer was active
994 */
995int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
996 unsigned long delta_ns, const enum hrtimer_mode mode)
997{
998 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
999}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001000EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1001
1002/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001003 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001004 * @timer: the timer to be added
1005 * @tim: expiry time
1006 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1007 *
1008 * Returns:
1009 * 0 on success
1010 * 1 when the timer was active
1011 */
1012int
1013hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1014{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001015 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001016}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001017EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001018
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001019
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001020/**
1021 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001022 * @timer: hrtimer to stop
1023 *
1024 * Returns:
1025 * 0 when the timer was not active
1026 * 1 when the timer was active
1027 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001028 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001029 */
1030int hrtimer_try_to_cancel(struct hrtimer *timer)
1031{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001032 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001033 unsigned long flags;
1034 int ret = -1;
1035
1036 base = lock_hrtimer_base(timer, &flags);
1037
Thomas Gleixner303e9672007-02-16 01:27:51 -08001038 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001039 ret = remove_hrtimer(timer, base);
1040
1041 unlock_hrtimer_base(timer, &flags);
1042
1043 return ret;
1044
1045}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001046EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001047
1048/**
1049 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001050 * @timer: the timer to be cancelled
1051 *
1052 * Returns:
1053 * 0 when the timer was not active
1054 * 1 when the timer was active
1055 */
1056int hrtimer_cancel(struct hrtimer *timer)
1057{
1058 for (;;) {
1059 int ret = hrtimer_try_to_cancel(timer);
1060
1061 if (ret >= 0)
1062 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001063 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001064 }
1065}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001066EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001067
1068/**
1069 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001070 * @timer: the timer to read
1071 */
1072ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1073{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001074 unsigned long flags;
1075 ktime_t rem;
1076
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001077 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001078 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001079 unlock_hrtimer_base(timer, &flags);
1080
1081 return rem;
1082}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001083EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001084
Russell Kingee9c5782008-04-20 13:59:33 +01001085#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001086/**
1087 * hrtimer_get_next_event - get the time until next expiry event
1088 *
1089 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1090 * is pending.
1091 */
1092ktime_t hrtimer_get_next_event(void)
1093{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001094 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1095 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001096 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1097 unsigned long flags;
1098 int i;
1099
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001100 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001101
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001102 if (!hrtimer_hres_active()) {
1103 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1104 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001105 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001106
John Stultz998adc32010-09-20 19:19:17 -07001107 next = timerqueue_getnext(&base->active);
1108 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001109 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001110
John Stultz998adc32010-09-20 19:19:17 -07001111 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001112 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001113 delta = ktime_sub(delta, base->get_time());
1114 if (delta.tv64 < mindelta.tv64)
1115 mindelta.tv64 = delta.tv64;
1116 }
Tony Lindgren69239742006-03-06 15:42:45 -08001117 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001118
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001119 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001120
Tony Lindgren69239742006-03-06 15:42:45 -08001121 if (mindelta.tv64 < 0)
1122 mindelta.tv64 = 0;
1123 return mindelta;
1124}
1125#endif
1126
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001127static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1128 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001129{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001130 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001131 int base;
George Anzinger7978672c2006-02-01 03:05:11 -08001132
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001133 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001134
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001135 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001136
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001137 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001138 clock_id = CLOCK_MONOTONIC;
1139
John Stultze06383d2010-12-14 19:37:07 -08001140 base = hrtimer_clockid_to_base(clock_id);
1141 timer->base = &cpu_base->clock_base[base];
John Stultz998adc32010-09-20 19:19:17 -07001142 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001143
1144#ifdef CONFIG_TIMER_STATS
1145 timer->start_site = NULL;
1146 timer->start_pid = -1;
1147 memset(timer->start_comm, 0, TASK_COMM_LEN);
1148#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001149}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001150
1151/**
1152 * hrtimer_init - initialize a timer to the given clock
1153 * @timer: the timer to be initialized
1154 * @clock_id: the clock to be used
1155 * @mode: timer mode abs/rel
1156 */
1157void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1158 enum hrtimer_mode mode)
1159{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001160 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001161 __hrtimer_init(timer, clock_id, mode);
1162}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001163EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001164
1165/**
1166 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001167 * @which_clock: which clock to query
1168 * @tp: pointer to timespec variable to store the resolution
1169 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001170 * Store the resolution of the clock selected by @which_clock in the
1171 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001172 */
1173int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1174{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001175 struct hrtimer_cpu_base *cpu_base;
John Stultze06383d2010-12-14 19:37:07 -08001176 int base = hrtimer_clockid_to_base(which_clock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001177
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001178 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
John Stultze06383d2010-12-14 19:37:07 -08001179 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001180
1181 return 0;
1182}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001183EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001184
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001185static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001186{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001187 struct hrtimer_clock_base *base = timer->base;
1188 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1189 enum hrtimer_restart (*fn)(struct hrtimer *);
1190 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001191
Peter Zijlstraca109492008-11-25 12:43:51 +01001192 WARN_ON(!irqs_disabled());
1193
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001194 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001195 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1196 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001197 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001198
1199 /*
1200 * Because we run timers from hardirq context, there is no chance
1201 * they get migrated to another cpu, therefore its safe to unlock
1202 * the timer base.
1203 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001204 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001205 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001206 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001207 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001208 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001209
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001210 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001211 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1212 * we do not reprogramm the event hardware. Happens either in
1213 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001214 */
1215 if (restart != HRTIMER_NORESTART) {
1216 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001217 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001218 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001219
1220 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1221
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001222 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001223}
1224
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001225#ifdef CONFIG_HIGH_RES_TIMERS
1226
1227/*
1228 * High resolution timer interrupt
1229 * Called with interrupts disabled
1230 */
1231void hrtimer_interrupt(struct clock_event_device *dev)
1232{
1233 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1234 struct hrtimer_clock_base *base;
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001235 ktime_t expires_next, now, entry_time, delta;
1236 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001237
1238 BUG_ON(!cpu_base->hres_active);
1239 cpu_base->nr_events++;
1240 dev->next_event.tv64 = KTIME_MAX;
1241
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001242 entry_time = now = ktime_get();
1243retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001244 expires_next.tv64 = KTIME_MAX;
1245
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001246 raw_spin_lock(&cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001247 /*
1248 * We set expires_next to KTIME_MAX here with cpu_base->lock
1249 * held to prevent that a timer is enqueued in our queue via
1250 * the migration code. This does not affect enqueueing of
1251 * timers which run their callback and need to be requeued on
1252 * this CPU.
1253 */
1254 cpu_base->expires_next.tv64 = KTIME_MAX;
1255
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001256 base = cpu_base->clock_base;
1257
1258 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1259 ktime_t basenow;
John Stultz998adc32010-09-20 19:19:17 -07001260 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001261
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001262 basenow = ktime_add(now, base->offset);
1263
John Stultz998adc32010-09-20 19:19:17 -07001264 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001265 struct hrtimer *timer;
1266
John Stultz998adc32010-09-20 19:19:17 -07001267 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001268
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001269 /*
1270 * The immediate goal for using the softexpires is
1271 * minimizing wakeups, not running timers at the
1272 * earliest interrupt after their soft expiration.
1273 * This allows us to avoid using a Priority Search
1274 * Tree, which can answer a stabbing querry for
1275 * overlapping intervals and instead use the simple
1276 * BST we already have.
1277 * We don't add extra wakeups by delaying timers that
1278 * are right-of a not yet expired timer, because that
1279 * timer will have to trigger a wakeup anyway.
1280 */
1281
1282 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001283 ktime_t expires;
1284
Arjan van de Vencc584b22008-09-01 15:02:30 -07001285 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001286 base->offset);
1287 if (expires.tv64 < expires_next.tv64)
1288 expires_next = expires;
1289 break;
1290 }
1291
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001292 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001293 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001294 base++;
1295 }
1296
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001297 /*
1298 * Store the new expiry value so the migration code can verify
1299 * against it.
1300 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001301 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001302 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001303
1304 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001305 if (expires_next.tv64 == KTIME_MAX ||
1306 !tick_program_event(expires_next, 0)) {
1307 cpu_base->hang_detected = 0;
1308 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001309 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001310
1311 /*
1312 * The next timer was already expired due to:
1313 * - tracing
1314 * - long lasting callbacks
1315 * - being scheduled away when running in a VM
1316 *
1317 * We need to prevent that we loop forever in the hrtimer
1318 * interrupt routine. We give it 3 attempts to avoid
1319 * overreacting on some spurious event.
1320 */
1321 now = ktime_get();
1322 cpu_base->nr_retries++;
1323 if (++retries < 3)
1324 goto retry;
1325 /*
1326 * Give the system a chance to do something else than looping
1327 * here. We stored the entry time, so we know exactly how long
1328 * we spent here. We schedule the next event this amount of
1329 * time away.
1330 */
1331 cpu_base->nr_hangs++;
1332 cpu_base->hang_detected = 1;
1333 delta = ktime_sub(now, entry_time);
1334 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1335 cpu_base->max_hang_time = delta;
1336 /*
1337 * Limit it to a sensible value as we enforce a longer
1338 * delay. Give the CPU at least 100ms to catch up.
1339 */
1340 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1341 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1342 else
1343 expires_next = ktime_add(now, delta);
1344 tick_program_event(expires_next, 1);
1345 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1346 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001347}
1348
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001349/*
1350 * local version of hrtimer_peek_ahead_timers() called with interrupts
1351 * disabled.
1352 */
1353static void __hrtimer_peek_ahead_timers(void)
1354{
1355 struct tick_device *td;
1356
1357 if (!hrtimer_hres_active())
1358 return;
1359
1360 td = &__get_cpu_var(tick_cpu_device);
1361 if (td && td->evtdev)
1362 hrtimer_interrupt(td->evtdev);
1363}
1364
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001365/**
1366 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1367 *
1368 * hrtimer_peek_ahead_timers will peek at the timer queue of
1369 * the current cpu and check if there are any timers for which
1370 * the soft expires time has passed. If any such timers exist,
1371 * they are run immediately and then removed from the timer queue.
1372 *
1373 */
1374void hrtimer_peek_ahead_timers(void)
1375{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001376 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001377
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001378 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001379 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001380 local_irq_restore(flags);
1381}
1382
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001383static void run_hrtimer_softirq(struct softirq_action *h)
1384{
1385 hrtimer_peek_ahead_timers();
1386}
1387
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001388#else /* CONFIG_HIGH_RES_TIMERS */
1389
1390static inline void __hrtimer_peek_ahead_timers(void) { }
1391
1392#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001393
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001394/*
1395 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001396 *
1397 * For HRT its the fall back code to run the softirq in the timer
1398 * softirq context in case the hrtimer initialization failed or has
1399 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001400 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001401void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001402{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001403 if (hrtimer_hres_active())
1404 return;
1405
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001406 /*
1407 * This _is_ ugly: We have to check in the softirq context,
1408 * whether we can switch to highres and / or nohz mode. The
1409 * clocksource switch happens in the timer interrupt with
1410 * xtime_lock held. Notification from there only sets the
1411 * check bit in the tick_oneshot code, otherwise we might
1412 * deadlock vs. xtime_lock.
1413 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001414 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001415 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001416}
1417
1418/*
1419 * Called from hardirq context every jiffy
1420 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001421void hrtimer_run_queues(void)
1422{
John Stultz998adc32010-09-20 19:19:17 -07001423 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001424 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001425 struct hrtimer_clock_base *base;
1426 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001427
1428 if (hrtimer_hres_active())
1429 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001430
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001431 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1432 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001433 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001434 continue;
1435
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001436 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001437 hrtimer_get_softirq_time(cpu_base);
1438 gettime = 0;
1439 }
1440
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001441 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001442
John Stultzb007c382010-12-10 22:19:53 -08001443 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001444 struct hrtimer *timer;
1445
John Stultz998adc32010-09-20 19:19:17 -07001446 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001447 if (base->softirq_time.tv64 <=
1448 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001449 break;
1450
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001451 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001452 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001453 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001454 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001455}
1456
1457/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001458 * Sleep related functions:
1459 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001460static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001461{
1462 struct hrtimer_sleeper *t =
1463 container_of(timer, struct hrtimer_sleeper, timer);
1464 struct task_struct *task = t->task;
1465
1466 t->task = NULL;
1467 if (task)
1468 wake_up_process(task);
1469
1470 return HRTIMER_NORESTART;
1471}
1472
Ingo Molnar36c8b582006-07-03 00:25:41 -07001473void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001474{
1475 sl->timer.function = hrtimer_wakeup;
1476 sl->task = task;
1477}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001478EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001479
Thomas Gleixner669d7862006-03-31 02:31:19 -08001480static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001481{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001482 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001483
Roman Zippel432569b2006-03-26 01:38:08 -08001484 do {
1485 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001486 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001487 if (!hrtimer_active(&t->timer))
1488 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001489
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001490 if (likely(t->task))
1491 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001492
Thomas Gleixner669d7862006-03-31 02:31:19 -08001493 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001494 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001495
Thomas Gleixner669d7862006-03-31 02:31:19 -08001496 } while (t->task && !signal_pending(current));
1497
Peter Zijlstra3588a082008-02-01 17:45:13 +01001498 __set_current_state(TASK_RUNNING);
1499
Thomas Gleixner669d7862006-03-31 02:31:19 -08001500 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001501}
1502
Oleg Nesterov080344b2008-02-01 17:29:05 +03001503static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1504{
1505 struct timespec rmt;
1506 ktime_t rem;
1507
Arjan van de Vencc584b22008-09-01 15:02:30 -07001508 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001509 if (rem.tv64 <= 0)
1510 return 0;
1511 rmt = ktime_to_timespec(rem);
1512
1513 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1514 return -EFAULT;
1515
1516 return 1;
1517}
1518
Toyo Abe1711ef32006-09-29 02:00:28 -07001519long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001520{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001521 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001522 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001523 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001524
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001525 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1526 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001527 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001528
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001529 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001530 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001531
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001532 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001533 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001534 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001535 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001536 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001537 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001538
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001539 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001540 ret = -ERESTART_RESTARTBLOCK;
1541out:
1542 destroy_hrtimer_on_stack(&t.timer);
1543 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001544}
1545
Oleg Nesterov080344b2008-02-01 17:29:05 +03001546long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001547 const enum hrtimer_mode mode, const clockid_t clockid)
1548{
1549 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001550 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001551 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001552 unsigned long slack;
1553
1554 slack = current->timer_slack_ns;
1555 if (rt_task(current))
1556 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001557
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001558 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001559 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001560 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001561 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001562
George Anzinger7978672c2006-02-01 03:05:11 -08001563 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001564 if (mode == HRTIMER_MODE_ABS) {
1565 ret = -ERESTARTNOHAND;
1566 goto out;
1567 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001568
Roman Zippel432569b2006-03-26 01:38:08 -08001569 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001570 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001571 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001572 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001573 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001574
1575 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001576 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001577 restart->nanosleep.index = t.timer.base->index;
1578 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001579 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001580
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001581 ret = -ERESTART_RESTARTBLOCK;
1582out:
1583 destroy_hrtimer_on_stack(&t.timer);
1584 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001585}
1586
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001587SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1588 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001589{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001590 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001591
1592 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1593 return -EFAULT;
1594
1595 if (!timespec_valid(&tu))
1596 return -EINVAL;
1597
Oleg Nesterov080344b2008-02-01 17:29:05 +03001598 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001599}
1600
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001601/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001602 * Functions related to boot-time initialization:
1603 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001604static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001605{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001606 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001607 int i;
1608
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001609 raw_spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001610
John Stultz998adc32010-09-20 19:19:17 -07001611 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001612 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001613 timerqueue_init_head(&cpu_base->clock_base[i].active);
1614 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001615
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001616 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001617}
1618
1619#ifdef CONFIG_HOTPLUG_CPU
1620
Peter Zijlstraca109492008-11-25 12:43:51 +01001621static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001622 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001623{
1624 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001625 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001626
John Stultz998adc32010-09-20 19:19:17 -07001627 while ((node = timerqueue_getnext(&old_base->active))) {
1628 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001629 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001630 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001631
1632 /*
1633 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1634 * timer could be seen as !active and just vanish away
1635 * under us on another CPU
1636 */
1637 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001638 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001639 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001640 * Enqueue the timers on the new cpu. This does not
1641 * reprogram the event device in case the timer
1642 * expires before the earliest on this CPU, but we run
1643 * hrtimer_interrupt after we migrated everything to
1644 * sort out already expired timers and reprogram the
1645 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001646 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001647 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001648
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001649 /* Clear the migration state bit */
1650 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001651 }
1652}
1653
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001654static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001655{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001656 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001657 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001658
Peter Zijlstra37810652008-12-04 11:17:10 +01001659 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001660 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001661
1662 local_irq_disable();
1663 old_base = &per_cpu(hrtimer_bases, scpu);
1664 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001665 /*
1666 * The caller is globally serialized and nobody else
1667 * takes two locks at once, deadlock is not possible.
1668 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001669 raw_spin_lock(&new_base->lock);
1670 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001671
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001672 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001673 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001674 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001675 }
1676
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001677 raw_spin_unlock(&old_base->lock);
1678 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001679
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001680 /* Check, if we got expired work to do */
1681 __hrtimer_peek_ahead_timers();
1682 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001683}
1684
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001685#endif /* CONFIG_HOTPLUG_CPU */
1686
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001687static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001688 unsigned long action, void *hcpu)
1689{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001690 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001691
1692 switch (action) {
1693
1694 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001695 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001696 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001697 break;
1698
1699#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001700 case CPU_DYING:
1701 case CPU_DYING_FROZEN:
1702 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1703 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001704 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001705 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001706 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001707 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001708 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001709 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001710 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001711#endif
1712
1713 default:
1714 break;
1715 }
1716
1717 return NOTIFY_OK;
1718}
1719
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001720static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001721 .notifier_call = hrtimer_cpu_notify,
1722};
1723
1724void __init hrtimers_init(void)
1725{
1726 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1727 (void *)(long)smp_processor_id());
1728 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001729#ifdef CONFIG_HIGH_RES_TIMERS
1730 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1731#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001732}
1733
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001734/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001735 * schedule_hrtimeout_range_clock - sleep until timeout
1736 * @expires: timeout value (ktime_t)
1737 * @delta: slack in expires timeout (ktime_t)
1738 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1739 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1740 */
1741int __sched
1742schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1743 const enum hrtimer_mode mode, int clock)
1744{
1745 struct hrtimer_sleeper t;
1746
1747 /*
1748 * Optimize when a zero timeout value is given. It does not
1749 * matter whether this is an absolute or a relative time.
1750 */
1751 if (expires && !expires->tv64) {
1752 __set_current_state(TASK_RUNNING);
1753 return 0;
1754 }
1755
1756 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001757 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001758 */
1759 if (!expires) {
1760 schedule();
1761 __set_current_state(TASK_RUNNING);
1762 return -EINTR;
1763 }
1764
1765 hrtimer_init_on_stack(&t.timer, clock, mode);
1766 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1767
1768 hrtimer_init_sleeper(&t, current);
1769
1770 hrtimer_start_expires(&t.timer, mode);
1771 if (!hrtimer_active(&t.timer))
1772 t.task = NULL;
1773
1774 if (likely(t.task))
1775 schedule();
1776
1777 hrtimer_cancel(&t.timer);
1778 destroy_hrtimer_on_stack(&t.timer);
1779
1780 __set_current_state(TASK_RUNNING);
1781
1782 return !t.task ? 0 : -EINTR;
1783}
1784
1785/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001786 * schedule_hrtimeout_range - sleep until timeout
1787 * @expires: timeout value (ktime_t)
1788 * @delta: slack in expires timeout (ktime_t)
1789 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1790 *
1791 * Make the current task sleep until the given expiry time has
1792 * elapsed. The routine will return immediately unless
1793 * the current task state has been set (see set_current_state()).
1794 *
1795 * The @delta argument gives the kernel the freedom to schedule the
1796 * actual wakeup to a time that is both power and performance friendly.
1797 * The kernel give the normal best effort behavior for "@expires+@delta",
1798 * but may decide to fire the timer earlier, but no earlier than @expires.
1799 *
1800 * You can set the task state as follows -
1801 *
1802 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1803 * pass before the routine returns.
1804 *
1805 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1806 * delivered to the current task.
1807 *
1808 * The current task state is guaranteed to be TASK_RUNNING when this
1809 * routine returns.
1810 *
1811 * Returns 0 when the timer has expired otherwise -EINTR
1812 */
1813int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001814 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001815{
Carsten Emde351b3f72010-04-02 22:40:19 +02001816 return schedule_hrtimeout_range_clock(expires, delta, mode,
1817 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001818}
1819EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1820
1821/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001822 * schedule_hrtimeout - sleep until timeout
1823 * @expires: timeout value (ktime_t)
1824 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1825 *
1826 * Make the current task sleep until the given expiry time has
1827 * elapsed. The routine will return immediately unless
1828 * the current task state has been set (see set_current_state()).
1829 *
1830 * You can set the task state as follows -
1831 *
1832 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1833 * pass before the routine returns.
1834 *
1835 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1836 * delivered to the current task.
1837 *
1838 * The current task state is guaranteed to be TASK_RUNNING when this
1839 * routine returns.
1840 *
1841 * Returns 0 when the timer has expired otherwise -EINTR
1842 */
1843int __sched schedule_hrtimeout(ktime_t *expires,
1844 const enum hrtimer_mode mode)
1845{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001846 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001847}
1848EXPORT_SYMBOL_GPL(schedule_hrtimeout);