blob: 57c4d33c9a9d6197e65fb1148a7186794e78d231 [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 *
56 * Note: If we want to add new timer bases, we have to skip the two
57 * clock ids captured by the cpu-timers. We do this by holding empty
58 * entries rather than doing math adjustment of the clock ids.
59 * This ensures that we capture erroneous accesses to these clock ids
60 * rather than moving them into the range of valid clock id's.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080061 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080062DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080063{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080064
65 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080066 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080067 {
68 .index = CLOCK_REALTIME,
69 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080070 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080071 },
72 {
73 .index = CLOCK_MONOTONIC,
74 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080075 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080076 },
77 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080078};
79
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080080/*
Thomas Gleixner92127c72006-03-26 01:38:05 -080081 * Get the coarse grained time at the softirq based on xtime and
82 * wall_to_monotonic.
83 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080084static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -080085{
86 ktime_t xtim, tomono;
Thomas Gleixnerad28d942007-03-16 13:38:21 -080087 struct timespec xts, tom;
Thomas Gleixner92127c72006-03-26 01:38:05 -080088
Torben Hohn48cf76f72011-01-27 15:59:05 +010089 get_xtime_and_monotonic_offset(&xts, &tom);
Thomas Gleixner92127c72006-03-26 01:38:05 -080090
john stultzf4304ab2007-02-16 01:27:26 -080091 xtim = timespec_to_ktime(xts);
Thomas Gleixnerad28d942007-03-16 13:38:21 -080092 tomono = timespec_to_ktime(tom);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080093 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
94 base->clock_base[CLOCK_MONOTONIC].softirq_time =
95 ktime_add(xtim, tomono);
Thomas Gleixner92127c72006-03-26 01:38:05 -080096}
97
98/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080099 * Functions and macros which are different for UP/SMP systems are kept in a
100 * single place
101 */
102#ifdef CONFIG_SMP
103
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800104/*
105 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
106 * means that all timers which are tied to this base via timer->base are
107 * locked, and the base itself is locked too.
108 *
109 * So __run_timers/migrate_timers can safely modify all timers which could
110 * be found on the lists/queues.
111 *
112 * When the timer's base is locked, and the timer removed from list, it is
113 * possible to set timer->base = NULL and drop the lock: the timer remains
114 * locked.
115 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800116static
117struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
118 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800119{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800120 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800121
122 for (;;) {
123 base = timer->base;
124 if (likely(base != NULL)) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100125 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800126 if (likely(base == timer->base))
127 return base;
128 /* The timer has migrated to another CPU: */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100129 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800130 }
131 cpu_relax();
132 }
133}
134
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200135
136/*
137 * Get the preferred target CPU for NOHZ
138 */
139static int hrtimer_get_target(int this_cpu, int pinned)
140{
141#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700142 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
143 return get_nohz_timer_target();
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200144#endif
145 return this_cpu;
146}
147
148/*
149 * With HIGHRES=y we do not migrate the timer when it is expiring
150 * before the next event on the target cpu because we cannot reprogram
151 * the target cpu hardware and we would cause it to fire late.
152 *
153 * Called with cpu_base->lock of target cpu held.
154 */
155static int
156hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
157{
158#ifdef CONFIG_HIGH_RES_TIMERS
159 ktime_t expires;
160
161 if (!new_base->cpu_base->hres_active)
162 return 0;
163
164 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
165 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
166#else
167 return 0;
168#endif
169}
170
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800171/*
172 * Switch the timer base to the current CPU when possible.
173 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800174static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530175switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
176 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800177{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800178 struct hrtimer_clock_base *new_base;
179 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200180 int this_cpu = smp_processor_id();
181 int cpu = hrtimer_get_target(this_cpu, pinned);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530182
183again:
184 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800185 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800186
187 if (base != new_base) {
188 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200189 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800190 * However we can't change timer's base while it is running,
191 * so we keep it on the same CPU. No hassle vs. reprogramming
192 * the event source in the high resolution case. The softirq
193 * code will take care of this when the timer function has
194 * completed. There is no conflict as we hold the lock until
195 * the timer is enqueued.
196 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800197 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800198 return base;
199
200 /* See the comment in lock_timer_base() */
201 timer->base = NULL;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100202 raw_spin_unlock(&base->cpu_base->lock);
203 raw_spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530204
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200205 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
206 cpu = this_cpu;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100207 raw_spin_unlock(&new_base->cpu_base->lock);
208 raw_spin_lock(&base->cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200209 timer->base = base;
210 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530211 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800212 timer->base = new_base;
213 }
214 return new_base;
215}
216
217#else /* CONFIG_SMP */
218
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800219static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800220lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
221{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800222 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800223
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100224 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800225
226 return base;
227}
228
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530229# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800230
231#endif /* !CONFIG_SMP */
232
233/*
234 * Functions for the union type storage format of ktime_t which are
235 * too large for inlining:
236 */
237#if BITS_PER_LONG < 64
238# ifndef CONFIG_KTIME_SCALAR
239/**
240 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800241 * @kt: addend
242 * @nsec: the scalar nsec value to add
243 *
244 * Returns the sum of kt and nsec in ktime_t format
245 */
246ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
247{
248 ktime_t tmp;
249
250 if (likely(nsec < NSEC_PER_SEC)) {
251 tmp.tv64 = nsec;
252 } else {
253 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
254
255 tmp = ktime_set((long)nsec, rem);
256 }
257
258 return ktime_add(kt, tmp);
259}
David Howellsb8b8fd22007-04-27 15:31:24 -0700260
261EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700262
263/**
264 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
265 * @kt: minuend
266 * @nsec: the scalar nsec value to subtract
267 *
268 * Returns the subtraction of @nsec from @kt in ktime_t format
269 */
270ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
271{
272 ktime_t tmp;
273
274 if (likely(nsec < NSEC_PER_SEC)) {
275 tmp.tv64 = nsec;
276 } else {
277 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
278
279 tmp = ktime_set((long)nsec, rem);
280 }
281
282 return ktime_sub(kt, tmp);
283}
284
285EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800286# endif /* !CONFIG_KTIME_SCALAR */
287
288/*
289 * Divide a ktime value by a nanosecond value
290 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800291u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800292{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300293 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800294 int sft = 0;
295
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300296 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800297 /* Make sure the divisor is less than 2^32: */
298 while (div >> 32) {
299 sft++;
300 div >>= 1;
301 }
302 dclc >>= sft;
303 do_div(dclc, (unsigned long) div);
304
Davide Libenzi4d672e72008-02-04 22:27:26 -0800305 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800306}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800307#endif /* BITS_PER_LONG >= 64 */
308
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100309/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100310 * Add two ktime values and do a safety check for overflow:
311 */
312ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
313{
314 ktime_t res = ktime_add(lhs, rhs);
315
316 /*
317 * We use KTIME_SEC_MAX here, the maximum timeout which we can
318 * return to user space in a timespec:
319 */
320 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
321 res = ktime_set(KTIME_SEC_MAX, 0);
322
323 return res;
324}
325
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300326EXPORT_SYMBOL_GPL(ktime_add_safe);
327
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700328#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
329
330static struct debug_obj_descr hrtimer_debug_descr;
331
332/*
333 * fixup_init is called when:
334 * - an active object is initialized
335 */
336static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
337{
338 struct hrtimer *timer = addr;
339
340 switch (state) {
341 case ODEBUG_STATE_ACTIVE:
342 hrtimer_cancel(timer);
343 debug_object_init(timer, &hrtimer_debug_descr);
344 return 1;
345 default:
346 return 0;
347 }
348}
349
350/*
351 * fixup_activate is called when:
352 * - an active object is activated
353 * - an unknown object is activated (might be a statically initialized object)
354 */
355static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
356{
357 switch (state) {
358
359 case ODEBUG_STATE_NOTAVAILABLE:
360 WARN_ON_ONCE(1);
361 return 0;
362
363 case ODEBUG_STATE_ACTIVE:
364 WARN_ON(1);
365
366 default:
367 return 0;
368 }
369}
370
371/*
372 * fixup_free is called when:
373 * - an active object is freed
374 */
375static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
376{
377 struct hrtimer *timer = addr;
378
379 switch (state) {
380 case ODEBUG_STATE_ACTIVE:
381 hrtimer_cancel(timer);
382 debug_object_free(timer, &hrtimer_debug_descr);
383 return 1;
384 default:
385 return 0;
386 }
387}
388
389static struct debug_obj_descr hrtimer_debug_descr = {
390 .name = "hrtimer",
391 .fixup_init = hrtimer_fixup_init,
392 .fixup_activate = hrtimer_fixup_activate,
393 .fixup_free = hrtimer_fixup_free,
394};
395
396static inline void debug_hrtimer_init(struct hrtimer *timer)
397{
398 debug_object_init(timer, &hrtimer_debug_descr);
399}
400
401static inline void debug_hrtimer_activate(struct hrtimer *timer)
402{
403 debug_object_activate(timer, &hrtimer_debug_descr);
404}
405
406static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
407{
408 debug_object_deactivate(timer, &hrtimer_debug_descr);
409}
410
411static inline void debug_hrtimer_free(struct hrtimer *timer)
412{
413 debug_object_free(timer, &hrtimer_debug_descr);
414}
415
416static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
417 enum hrtimer_mode mode);
418
419void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
420 enum hrtimer_mode mode)
421{
422 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
423 __hrtimer_init(timer, clock_id, mode);
424}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -0700425EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700426
427void destroy_hrtimer_on_stack(struct hrtimer *timer)
428{
429 debug_object_free(timer, &hrtimer_debug_descr);
430}
431
432#else
433static inline void debug_hrtimer_init(struct hrtimer *timer) { }
434static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
435static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
436#endif
437
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800438static inline void
439debug_init(struct hrtimer *timer, clockid_t clockid,
440 enum hrtimer_mode mode)
441{
442 debug_hrtimer_init(timer);
443 trace_hrtimer_init(timer, clockid, mode);
444}
445
446static inline void debug_activate(struct hrtimer *timer)
447{
448 debug_hrtimer_activate(timer);
449 trace_hrtimer_start(timer);
450}
451
452static inline void debug_deactivate(struct hrtimer *timer)
453{
454 debug_hrtimer_deactivate(timer);
455 trace_hrtimer_cancel(timer);
456}
457
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800458/* High resolution timer related functions */
459#ifdef CONFIG_HIGH_RES_TIMERS
460
461/*
462 * High resolution timer enabled ?
463 */
464static int hrtimer_hres_enabled __read_mostly = 1;
465
466/*
467 * Enable / Disable high resolution mode
468 */
469static int __init setup_hrtimer_hres(char *str)
470{
471 if (!strcmp(str, "off"))
472 hrtimer_hres_enabled = 0;
473 else if (!strcmp(str, "on"))
474 hrtimer_hres_enabled = 1;
475 else
476 return 0;
477 return 1;
478}
479
480__setup("highres=", setup_hrtimer_hres);
481
482/*
483 * hrtimer_high_res_enabled - query, if the highres mode is enabled
484 */
485static inline int hrtimer_is_hres_enabled(void)
486{
487 return hrtimer_hres_enabled;
488}
489
490/*
491 * Is the high resolution mode active ?
492 */
493static inline int hrtimer_hres_active(void)
494{
Christoph Lameter909ea962010-12-08 16:22:55 +0100495 return __this_cpu_read(hrtimer_bases.hres_active);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800496}
497
498/*
499 * Reprogram the event source with checking both queues for the
500 * next event
501 * Called with interrupts disabled and base->lock held
502 */
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400503static void
504hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800505{
506 int i;
507 struct hrtimer_clock_base *base = cpu_base->clock_base;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400508 ktime_t expires, expires_next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800509
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400510 expires_next.tv64 = KTIME_MAX;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800511
512 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
513 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -0700514 struct timerqueue_node *next;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800515
John Stultz998adc32010-09-20 19:19:17 -0700516 next = timerqueue_getnext(&base->active);
517 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800518 continue;
John Stultz998adc32010-09-20 19:19:17 -0700519 timer = container_of(next, struct hrtimer, node);
520
Arjan van de Vencc584b22008-09-01 15:02:30 -0700521 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100522 /*
523 * clock_was_set() has changed base->offset so the
524 * result might be negative. Fix it up to prevent a
525 * false positive in clockevents_program_event()
526 */
527 if (expires.tv64 < 0)
528 expires.tv64 = 0;
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400529 if (expires.tv64 < expires_next.tv64)
530 expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800531 }
532
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400533 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
534 return;
535
536 cpu_base->expires_next.tv64 = expires_next.tv64;
537
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800538 if (cpu_base->expires_next.tv64 != KTIME_MAX)
539 tick_program_event(cpu_base->expires_next, 1);
540}
541
542/*
543 * Shared reprogramming for clock_realtime and clock_monotonic
544 *
545 * When a timer is enqueued and expires earlier than the already enqueued
546 * timers, we have to check, whether it expires earlier than the timer for
547 * which the clock event device was armed.
548 *
549 * Called with interrupts disabled and base->cpu_base.lock held
550 */
551static int hrtimer_reprogram(struct hrtimer *timer,
552 struct hrtimer_clock_base *base)
553{
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100554 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700555 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800556 int res;
557
Arjan van de Vencc584b22008-09-01 15:02:30 -0700558 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100559
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800560 /*
561 * When the callback is running, we do not reprogram the clock event
562 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200563 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800564 * reprogramming is handled either by the softirq, which called the
565 * callback or at the end of the hrtimer_interrupt.
566 */
567 if (hrtimer_callback_running(timer))
568 return 0;
569
Thomas Gleixner63070a72008-02-14 00:58:36 +0100570 /*
571 * CLOCK_REALTIME timer might be requested with an absolute
572 * expiry time which is less than base->offset. Nothing wrong
573 * about that, just avoid to call into the tick code, which
574 * has now objections against negative expiry values.
575 */
576 if (expires.tv64 < 0)
577 return -ETIME;
578
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100579 if (expires.tv64 >= cpu_base->expires_next.tv64)
580 return 0;
581
582 /*
583 * If a hang was detected in the last timer interrupt then we
584 * do not schedule a timer which is earlier than the expiry
585 * which we enforced in the hang detection. We want the system
586 * to make progress.
587 */
588 if (cpu_base->hang_detected)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800589 return 0;
590
591 /*
592 * Clockevents returns -ETIME, when the event was in the past.
593 */
594 res = tick_program_event(expires, 0);
595 if (!IS_ERR_VALUE(res))
Thomas Gleixner41d2e492009-11-13 17:05:44 +0100596 cpu_base->expires_next = expires;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800597 return res;
598}
599
600
601/*
602 * Retrigger next event is called after clock was set
603 *
604 * Called with interrupts disabled via on_each_cpu()
605 */
606static void retrigger_next_event(void *arg)
607{
608 struct hrtimer_cpu_base *base;
John Stultz8ab43512010-07-13 17:56:25 -0700609 struct timespec realtime_offset, wtm;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800610
611 if (!hrtimer_hres_active())
612 return;
613
Torben Hohn48cf76f72011-01-27 15:59:05 +0100614 get_xtime_and_monotonic_offset(&realtime_offset, &wtm);
John Stultz8ab43512010-07-13 17:56:25 -0700615 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800616
617 base = &__get_cpu_var(hrtimer_bases);
618
619 /* Adjust CLOCK_REALTIME offset */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100620 raw_spin_lock(&base->lock);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800621 base->clock_base[CLOCK_REALTIME].offset =
622 timespec_to_ktime(realtime_offset);
623
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400624 hrtimer_force_reprogram(base, 0);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100625 raw_spin_unlock(&base->lock);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800626}
627
628/*
629 * Clock realtime was set
630 *
631 * Change the offset of the realtime clock vs. the monotonic
632 * clock.
633 *
634 * We might have to reprogram the high resolution timer interrupt. On
635 * SMP we call the architecture specific code to retrigger _all_ high
636 * resolution timer interrupts. On UP we just disable interrupts and
637 * call the high resolution interrupt code.
638 */
639void clock_was_set(void)
640{
641 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200642 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800643}
644
645/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200646 * During resume we might have to reprogram the high resolution timer
647 * interrupt (on the local CPU):
648 */
649void hres_timers_resume(void)
650{
Peter Zijlstra1d4a7f12009-01-18 16:39:29 +0100651 WARN_ONCE(!irqs_disabled(),
652 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
653
Ingo Molnar995f0542007-04-07 12:05:00 +0200654 retrigger_next_event(NULL);
655}
656
657/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800658 * Initialize the high resolution related parts of cpu_base
659 */
660static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
661{
662 base->expires_next.tv64 = KTIME_MAX;
663 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800664}
665
666/*
667 * Initialize the high resolution related parts of a hrtimer
668 */
669static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
670{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800671}
672
Peter Zijlstraca109492008-11-25 12:43:51 +0100673
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800674/*
675 * When High resolution timers are active, try to reprogram. Note, that in case
676 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
677 * check happens. The timer gets enqueued into the rbtree. The reprogramming
678 * and expiry check is done in the hrtimer_interrupt or in the softirq.
679 */
680static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100681 struct hrtimer_clock_base *base,
682 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800683{
684 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100685 if (wakeup) {
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100686 raw_spin_unlock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100687 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100688 raw_spin_lock(&base->cpu_base->lock);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100689 } else
690 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
691
Peter Zijlstraca109492008-11-25 12:43:51 +0100692 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800693 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100694
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800695 return 0;
696}
697
698/*
699 * Switch to high resolution mode
700 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800701static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800702{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700703 int cpu = smp_processor_id();
704 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800705 unsigned long flags;
706
707 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800708 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800709
710 local_irq_save(flags);
711
712 if (tick_init_highres()) {
713 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700714 printk(KERN_WARNING "Could not switch to high resolution "
715 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800716 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800717 }
718 base->hres_active = 1;
719 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
720 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
721
722 tick_setup_sched_timer();
723
724 /* "Retrigger" the interrupt to get things going */
725 retrigger_next_event(NULL);
726 local_irq_restore(flags);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800727 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800728}
729
730#else
731
732static inline int hrtimer_hres_active(void) { return 0; }
733static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800734static inline int hrtimer_switch_to_hres(void) { return 0; }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400735static inline void
736hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800737static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100738 struct hrtimer_clock_base *base,
739 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800740{
741 return 0;
742}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800743static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
744static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
745
746#endif /* CONFIG_HIGH_RES_TIMERS */
747
Heiko Carstens5f201902009-12-10 10:56:29 +0100748static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800749{
Heiko Carstens5f201902009-12-10 10:56:29 +0100750#ifdef CONFIG_TIMER_STATS
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800751 if (timer->start_site)
752 return;
Heiko Carstens5f201902009-12-10 10:56:29 +0100753 timer->start_site = __builtin_return_address(0);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800754 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
755 timer->start_pid = current->pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800756#endif
Heiko Carstens5f201902009-12-10 10:56:29 +0100757}
758
759static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
760{
761#ifdef CONFIG_TIMER_STATS
762 timer->start_site = NULL;
763#endif
764}
765
766static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
767{
768#ifdef CONFIG_TIMER_STATS
769 if (likely(!timer_stats_active))
770 return;
771 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
772 timer->function, timer->start_comm, 0);
773#endif
774}
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800775
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800776/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200777 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800778 */
779static inline
780void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
781{
Thomas Gleixnerecb49d12009-11-17 16:36:54 +0100782 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800783}
784
785/**
786 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800787 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800788 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800789 * @interval: the interval to forward
790 *
791 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700792 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800793 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800794u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800795{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800796 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800797 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800798
Arjan van de Vencc584b22008-09-01 15:02:30 -0700799 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800800
801 if (delta.tv64 < 0)
802 return 0;
803
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100804 if (interval.tv64 < timer->base->resolution.tv64)
805 interval.tv64 = timer->base->resolution.tv64;
806
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800807 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800808 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800809
810 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700811 hrtimer_add_expires_ns(timer, incr * orun);
812 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800813 return orun;
814 /*
815 * This (and the ktime_add() below) is the
816 * correction for exact:
817 */
818 orun++;
819 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700820 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821
822 return orun;
823}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700824EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800825
826/*
827 * enqueue_hrtimer - internal function to (re)start a timer
828 *
829 * The timer is inserted in expiry order. Insertion into the
830 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100831 *
832 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800833 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100834static int enqueue_hrtimer(struct hrtimer *timer,
835 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800836{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800837 debug_activate(timer);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700838
John Stultz998adc32010-09-20 19:19:17 -0700839 timerqueue_add(&base->active, &timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800840
841 /*
Thomas Gleixner303e9672007-02-16 01:27:51 -0800842 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
843 * state of a possibly running callback.
844 */
845 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100846
John Stultz998adc32010-09-20 19:19:17 -0700847 return (&timer->node == base->active.next);
Thomas Gleixner288867e2006-01-12 11:25:54 +0100848}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800849
850/*
851 * __remove_hrtimer - internal function to remove a timer
852 *
853 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800854 *
855 * High resolution timer mode reprograms the clock event device when the
856 * timer is the one which expires next. The caller can disable this by setting
857 * reprogram to zero. This is useful, when the context does a reprogramming
858 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800859 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800860static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800861 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800862 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800863{
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400864 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
865 goto out;
866
John Stultz998adc32010-09-20 19:19:17 -0700867 if (&timer->node == timerqueue_getnext(&base->active)) {
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400868#ifdef CONFIG_HIGH_RES_TIMERS
869 /* Reprogram the clock event device. if enabled */
870 if (reprogram && hrtimer_hres_active()) {
871 ktime_t expires;
872
873 expires = ktime_sub(hrtimer_get_expires(timer),
874 base->offset);
875 if (base->cpu_base->expires_next.tv64 == expires.tv64)
876 hrtimer_force_reprogram(base->cpu_base, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800877 }
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400878#endif
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800879 }
John Stultz998adc32010-09-20 19:19:17 -0700880 timerqueue_del(&base->active, &timer->node);
Ashwin Chaugule7403f412009-09-01 23:03:33 -0400881out:
Thomas Gleixner303e9672007-02-16 01:27:51 -0800882 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800883}
884
885/*
886 * remove hrtimer, called with base lock held
887 */
888static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800889remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800890{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800891 if (hrtimer_is_queued(timer)) {
Salman Qazif13d4f92010-10-12 07:25:19 -0700892 unsigned long state;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800893 int reprogram;
894
895 /*
896 * Remove the timer and force reprogramming when high
897 * resolution mode is active and the timer is on the current
898 * CPU. If we remove a timer on another CPU, reprogramming is
899 * skipped. The interrupt event on this CPU is fired and
900 * reprogramming happens in the interrupt handler. This is a
901 * rare case and less expensive than a smp call.
902 */
Xiao Guangrongc6a2a172009-08-10 10:51:23 +0800903 debug_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800904 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800905 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
Salman Qazif13d4f92010-10-12 07:25:19 -0700906 /*
907 * We must preserve the CALLBACK state flag here,
908 * otherwise we could move the timer base in
909 * switch_hrtimer_base.
910 */
911 state = timer->state & HRTIMER_STATE_CALLBACK;
912 __remove_hrtimer(timer, base, state, reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800913 return 1;
914 }
915 return 0;
916}
917
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100918int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
919 unsigned long delta_ns, const enum hrtimer_mode mode,
920 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800921{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800922 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800923 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100924 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800925
926 base = lock_hrtimer_base(timer, &flags);
927
928 /* Remove an active timer from the queue: */
929 ret = remove_hrtimer(timer, base);
930
931 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530932 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800933
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530934 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100935 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800936 /*
937 * CONFIG_TIME_LOW_RES is a temporary way for architectures
938 * to signal that they simply return xtime in
939 * do_gettimeoffset(). In this case we want to round up by
940 * resolution when starting a relative timer, to avoid short
941 * timeouts. This will go away with the GTOD framework.
942 */
943#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100944 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800945#endif
946 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700947
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700948 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800949
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800950 timer_stats_hrtimer_set_start_info(timer);
951
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100952 leftmost = enqueue_hrtimer(timer, new_base);
953
Ingo Molnar935c6312007-03-28 13:17:18 +0200954 /*
955 * Only allow reprogramming if the new base is on this CPU.
956 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100957 *
958 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200959 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100960 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100961 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800962
963 unlock_hrtimer_base(timer, &flags);
964
965 return ret;
966}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100967
968/**
969 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
970 * @timer: the timer to be added
971 * @tim: expiry time
972 * @delta_ns: "slack" range for the timer
973 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
974 *
975 * Returns:
976 * 0 on success
977 * 1 when the timer was active
978 */
979int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
980 unsigned long delta_ns, const enum hrtimer_mode mode)
981{
982 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
983}
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700984EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
985
986/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200987 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700988 * @timer: the timer to be added
989 * @tim: expiry time
990 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
991 *
992 * Returns:
993 * 0 on success
994 * 1 when the timer was active
995 */
996int
997hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
998{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100999 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001000}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001001EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001002
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001003
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001004/**
1005 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001006 * @timer: hrtimer to stop
1007 *
1008 * Returns:
1009 * 0 when the timer was not active
1010 * 1 when the timer was active
1011 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001012 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001013 */
1014int hrtimer_try_to_cancel(struct hrtimer *timer)
1015{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001016 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001017 unsigned long flags;
1018 int ret = -1;
1019
1020 base = lock_hrtimer_base(timer, &flags);
1021
Thomas Gleixner303e9672007-02-16 01:27:51 -08001022 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001023 ret = remove_hrtimer(timer, base);
1024
1025 unlock_hrtimer_base(timer, &flags);
1026
1027 return ret;
1028
1029}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001030EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001031
1032/**
1033 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001034 * @timer: the timer to be cancelled
1035 *
1036 * Returns:
1037 * 0 when the timer was not active
1038 * 1 when the timer was active
1039 */
1040int hrtimer_cancel(struct hrtimer *timer)
1041{
1042 for (;;) {
1043 int ret = hrtimer_try_to_cancel(timer);
1044
1045 if (ret >= 0)
1046 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001047 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001048 }
1049}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001050EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001051
1052/**
1053 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001054 * @timer: the timer to read
1055 */
1056ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1057{
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001058 unsigned long flags;
1059 ktime_t rem;
1060
Andi Kleenb3bd3de2010-08-10 14:17:51 -07001061 lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001062 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001063 unlock_hrtimer_base(timer, &flags);
1064
1065 return rem;
1066}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001067EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001068
Russell Kingee9c5782008-04-20 13:59:33 +01001069#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001070/**
1071 * hrtimer_get_next_event - get the time until next expiry event
1072 *
1073 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1074 * is pending.
1075 */
1076ktime_t hrtimer_get_next_event(void)
1077{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001078 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1079 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001080 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1081 unsigned long flags;
1082 int i;
1083
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001084 raw_spin_lock_irqsave(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001085
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001086 if (!hrtimer_hres_active()) {
1087 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1088 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001089 struct timerqueue_node *next;
Tony Lindgren69239742006-03-06 15:42:45 -08001090
John Stultz998adc32010-09-20 19:19:17 -07001091 next = timerqueue_getnext(&base->active);
1092 if (!next)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001093 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001094
John Stultz998adc32010-09-20 19:19:17 -07001095 timer = container_of(next, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001096 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001097 delta = ktime_sub(delta, base->get_time());
1098 if (delta.tv64 < mindelta.tv64)
1099 mindelta.tv64 = delta.tv64;
1100 }
Tony Lindgren69239742006-03-06 15:42:45 -08001101 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001102
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001103 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001104
Tony Lindgren69239742006-03-06 15:42:45 -08001105 if (mindelta.tv64 < 0)
1106 mindelta.tv64 = 0;
1107 return mindelta;
1108}
1109#endif
1110
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001111static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1112 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001113{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001114 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001115
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001116 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001117
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001118 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001119
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001120 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001121 clock_id = CLOCK_MONOTONIC;
1122
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001123 timer->base = &cpu_base->clock_base[clock_id];
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001124 hrtimer_init_timer_hres(timer);
John Stultz998adc32010-09-20 19:19:17 -07001125 timerqueue_init(&timer->node);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001126
1127#ifdef CONFIG_TIMER_STATS
1128 timer->start_site = NULL;
1129 timer->start_pid = -1;
1130 memset(timer->start_comm, 0, TASK_COMM_LEN);
1131#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001132}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001133
1134/**
1135 * hrtimer_init - initialize a timer to the given clock
1136 * @timer: the timer to be initialized
1137 * @clock_id: the clock to be used
1138 * @mode: timer mode abs/rel
1139 */
1140void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1141 enum hrtimer_mode mode)
1142{
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001143 debug_init(timer, clock_id, mode);
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001144 __hrtimer_init(timer, clock_id, mode);
1145}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001146EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001147
1148/**
1149 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001150 * @which_clock: which clock to query
1151 * @tp: pointer to timespec variable to store the resolution
1152 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001153 * Store the resolution of the clock selected by @which_clock in the
1154 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001155 */
1156int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1157{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001158 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001159
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001160 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1161 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001162
1163 return 0;
1164}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001165EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001166
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001167static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001168{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001169 struct hrtimer_clock_base *base = timer->base;
1170 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1171 enum hrtimer_restart (*fn)(struct hrtimer *);
1172 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001173
Peter Zijlstraca109492008-11-25 12:43:51 +01001174 WARN_ON(!irqs_disabled());
1175
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001176 debug_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001177 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1178 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001179 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001180
1181 /*
1182 * Because we run timers from hardirq context, there is no chance
1183 * they get migrated to another cpu, therefore its safe to unlock
1184 * the timer base.
1185 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001186 raw_spin_unlock(&cpu_base->lock);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001187 trace_hrtimer_expire_entry(timer, now);
Peter Zijlstraca109492008-11-25 12:43:51 +01001188 restart = fn(timer);
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001189 trace_hrtimer_expire_exit(timer);
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001190 raw_spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001191
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001192 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001193 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1194 * we do not reprogramm the event hardware. Happens either in
1195 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001196 */
1197 if (restart != HRTIMER_NORESTART) {
1198 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001199 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001200 }
Salman Qazif13d4f92010-10-12 07:25:19 -07001201
1202 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1203
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001204 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001205}
1206
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001207#ifdef CONFIG_HIGH_RES_TIMERS
1208
1209/*
1210 * High resolution timer interrupt
1211 * Called with interrupts disabled
1212 */
1213void hrtimer_interrupt(struct clock_event_device *dev)
1214{
1215 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1216 struct hrtimer_clock_base *base;
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001217 ktime_t expires_next, now, entry_time, delta;
1218 int i, retries = 0;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001219
1220 BUG_ON(!cpu_base->hres_active);
1221 cpu_base->nr_events++;
1222 dev->next_event.tv64 = KTIME_MAX;
1223
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001224 entry_time = now = ktime_get();
1225retry:
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001226 expires_next.tv64 = KTIME_MAX;
1227
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001228 raw_spin_lock(&cpu_base->lock);
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001229 /*
1230 * We set expires_next to KTIME_MAX here with cpu_base->lock
1231 * held to prevent that a timer is enqueued in our queue via
1232 * the migration code. This does not affect enqueueing of
1233 * timers which run their callback and need to be requeued on
1234 * this CPU.
1235 */
1236 cpu_base->expires_next.tv64 = KTIME_MAX;
1237
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001238 base = cpu_base->clock_base;
1239
1240 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1241 ktime_t basenow;
John Stultz998adc32010-09-20 19:19:17 -07001242 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001243
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001244 basenow = ktime_add(now, base->offset);
1245
John Stultz998adc32010-09-20 19:19:17 -07001246 while ((node = timerqueue_getnext(&base->active))) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001247 struct hrtimer *timer;
1248
John Stultz998adc32010-09-20 19:19:17 -07001249 timer = container_of(node, struct hrtimer, node);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001250
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001251 /*
1252 * The immediate goal for using the softexpires is
1253 * minimizing wakeups, not running timers at the
1254 * earliest interrupt after their soft expiration.
1255 * This allows us to avoid using a Priority Search
1256 * Tree, which can answer a stabbing querry for
1257 * overlapping intervals and instead use the simple
1258 * BST we already have.
1259 * We don't add extra wakeups by delaying timers that
1260 * are right-of a not yet expired timer, because that
1261 * timer will have to trigger a wakeup anyway.
1262 */
1263
1264 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001265 ktime_t expires;
1266
Arjan van de Vencc584b22008-09-01 15:02:30 -07001267 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001268 base->offset);
1269 if (expires.tv64 < expires_next.tv64)
1270 expires_next = expires;
1271 break;
1272 }
1273
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001274 __run_hrtimer(timer, &basenow);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001275 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001276 base++;
1277 }
1278
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001279 /*
1280 * Store the new expiry value so the migration code can verify
1281 * against it.
1282 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001283 cpu_base->expires_next = expires_next;
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001284 raw_spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001285
1286 /* Reprogramming necessary ? */
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001287 if (expires_next.tv64 == KTIME_MAX ||
1288 !tick_program_event(expires_next, 0)) {
1289 cpu_base->hang_detected = 0;
1290 return;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001291 }
Thomas Gleixner41d2e492009-11-13 17:05:44 +01001292
1293 /*
1294 * The next timer was already expired due to:
1295 * - tracing
1296 * - long lasting callbacks
1297 * - being scheduled away when running in a VM
1298 *
1299 * We need to prevent that we loop forever in the hrtimer
1300 * interrupt routine. We give it 3 attempts to avoid
1301 * overreacting on some spurious event.
1302 */
1303 now = ktime_get();
1304 cpu_base->nr_retries++;
1305 if (++retries < 3)
1306 goto retry;
1307 /*
1308 * Give the system a chance to do something else than looping
1309 * here. We stored the entry time, so we know exactly how long
1310 * we spent here. We schedule the next event this amount of
1311 * time away.
1312 */
1313 cpu_base->nr_hangs++;
1314 cpu_base->hang_detected = 1;
1315 delta = ktime_sub(now, entry_time);
1316 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1317 cpu_base->max_hang_time = delta;
1318 /*
1319 * Limit it to a sensible value as we enforce a longer
1320 * delay. Give the CPU at least 100ms to catch up.
1321 */
1322 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1323 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1324 else
1325 expires_next = ktime_add(now, delta);
1326 tick_program_event(expires_next, 1);
1327 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1328 ktime_to_ns(delta));
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001329}
1330
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001331/*
1332 * local version of hrtimer_peek_ahead_timers() called with interrupts
1333 * disabled.
1334 */
1335static void __hrtimer_peek_ahead_timers(void)
1336{
1337 struct tick_device *td;
1338
1339 if (!hrtimer_hres_active())
1340 return;
1341
1342 td = &__get_cpu_var(tick_cpu_device);
1343 if (td && td->evtdev)
1344 hrtimer_interrupt(td->evtdev);
1345}
1346
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001347/**
1348 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1349 *
1350 * hrtimer_peek_ahead_timers will peek at the timer queue of
1351 * the current cpu and check if there are any timers for which
1352 * the soft expires time has passed. If any such timers exist,
1353 * they are run immediately and then removed from the timer queue.
1354 *
1355 */
1356void hrtimer_peek_ahead_timers(void)
1357{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001358 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001359
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001360 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001361 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001362 local_irq_restore(flags);
1363}
1364
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001365static void run_hrtimer_softirq(struct softirq_action *h)
1366{
1367 hrtimer_peek_ahead_timers();
1368}
1369
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001370#else /* CONFIG_HIGH_RES_TIMERS */
1371
1372static inline void __hrtimer_peek_ahead_timers(void) { }
1373
1374#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001375
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001376/*
1377 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001378 *
1379 * For HRT its the fall back code to run the softirq in the timer
1380 * softirq context in case the hrtimer initialization failed or has
1381 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001382 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001383void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001384{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001385 if (hrtimer_hres_active())
1386 return;
1387
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001388 /*
1389 * This _is_ ugly: We have to check in the softirq context,
1390 * whether we can switch to highres and / or nohz mode. The
1391 * clocksource switch happens in the timer interrupt with
1392 * xtime_lock held. Notification from there only sets the
1393 * check bit in the tick_oneshot code, otherwise we might
1394 * deadlock vs. xtime_lock.
1395 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001396 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001397 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001398}
1399
1400/*
1401 * Called from hardirq context every jiffy
1402 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001403void hrtimer_run_queues(void)
1404{
John Stultz998adc32010-09-20 19:19:17 -07001405 struct timerqueue_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001406 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001407 struct hrtimer_clock_base *base;
1408 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001409
1410 if (hrtimer_hres_active())
1411 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001412
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001413 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1414 base = &cpu_base->clock_base[index];
John Stultzb007c382010-12-10 22:19:53 -08001415 if (!timerqueue_getnext(&base->active))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001416 continue;
1417
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001418 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001419 hrtimer_get_softirq_time(cpu_base);
1420 gettime = 0;
1421 }
1422
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001423 raw_spin_lock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001424
John Stultzb007c382010-12-10 22:19:53 -08001425 while ((node = timerqueue_getnext(&base->active))) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001426 struct hrtimer *timer;
1427
John Stultz998adc32010-09-20 19:19:17 -07001428 timer = container_of(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001429 if (base->softirq_time.tv64 <=
1430 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001431 break;
1432
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001433 __run_hrtimer(timer, &base->softirq_time);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001434 }
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001435 raw_spin_unlock(&cpu_base->lock);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001436 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001437}
1438
1439/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001440 * Sleep related functions:
1441 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001442static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001443{
1444 struct hrtimer_sleeper *t =
1445 container_of(timer, struct hrtimer_sleeper, timer);
1446 struct task_struct *task = t->task;
1447
1448 t->task = NULL;
1449 if (task)
1450 wake_up_process(task);
1451
1452 return HRTIMER_NORESTART;
1453}
1454
Ingo Molnar36c8b582006-07-03 00:25:41 -07001455void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001456{
1457 sl->timer.function = hrtimer_wakeup;
1458 sl->task = task;
1459}
Stephen Hemminger2bc481c2009-08-28 23:41:29 -07001460EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
Thomas Gleixner00362e32006-03-31 02:31:17 -08001461
Thomas Gleixner669d7862006-03-31 02:31:19 -08001462static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001463{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001464 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001465
Roman Zippel432569b2006-03-26 01:38:08 -08001466 do {
1467 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001468 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001469 if (!hrtimer_active(&t->timer))
1470 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001471
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001472 if (likely(t->task))
1473 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001474
Thomas Gleixner669d7862006-03-31 02:31:19 -08001475 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001476 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001477
Thomas Gleixner669d7862006-03-31 02:31:19 -08001478 } while (t->task && !signal_pending(current));
1479
Peter Zijlstra3588a082008-02-01 17:45:13 +01001480 __set_current_state(TASK_RUNNING);
1481
Thomas Gleixner669d7862006-03-31 02:31:19 -08001482 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001483}
1484
Oleg Nesterov080344b2008-02-01 17:29:05 +03001485static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1486{
1487 struct timespec rmt;
1488 ktime_t rem;
1489
Arjan van de Vencc584b22008-09-01 15:02:30 -07001490 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001491 if (rem.tv64 <= 0)
1492 return 0;
1493 rmt = ktime_to_timespec(rem);
1494
1495 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1496 return -EFAULT;
1497
1498 return 1;
1499}
1500
Toyo Abe1711ef32006-09-29 02:00:28 -07001501long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001502{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001503 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001504 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001505 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001506
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001507 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1508 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001509 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001510
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001511 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001512 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001513
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001514 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001515 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001516 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001517 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001518 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001519 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001520
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001521 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001522 ret = -ERESTART_RESTARTBLOCK;
1523out:
1524 destroy_hrtimer_on_stack(&t.timer);
1525 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001526}
1527
Oleg Nesterov080344b2008-02-01 17:29:05 +03001528long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001529 const enum hrtimer_mode mode, const clockid_t clockid)
1530{
1531 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001532 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001533 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001534 unsigned long slack;
1535
1536 slack = current->timer_slack_ns;
1537 if (rt_task(current))
1538 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001539
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001540 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001541 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001542 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001543 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001544
George Anzinger7978672c2006-02-01 03:05:11 -08001545 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001546 if (mode == HRTIMER_MODE_ABS) {
1547 ret = -ERESTARTNOHAND;
1548 goto out;
1549 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001550
Roman Zippel432569b2006-03-26 01:38:08 -08001551 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001552 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001553 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001554 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001555 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001556
1557 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001558 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001559 restart->nanosleep.index = t.timer.base->index;
1560 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001561 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001562
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001563 ret = -ERESTART_RESTARTBLOCK;
1564out:
1565 destroy_hrtimer_on_stack(&t.timer);
1566 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001567}
1568
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001569SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1570 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001571{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001572 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001573
1574 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1575 return -EFAULT;
1576
1577 if (!timespec_valid(&tu))
1578 return -EINVAL;
1579
Oleg Nesterov080344b2008-02-01 17:29:05 +03001580 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001581}
1582
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001583/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001584 * Functions related to boot-time initialization:
1585 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001586static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001587{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001588 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001589 int i;
1590
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001591 raw_spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001592
John Stultz998adc32010-09-20 19:19:17 -07001593 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001594 cpu_base->clock_base[i].cpu_base = cpu_base;
John Stultz998adc32010-09-20 19:19:17 -07001595 timerqueue_init_head(&cpu_base->clock_base[i].active);
1596 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001597
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001598 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001599}
1600
1601#ifdef CONFIG_HOTPLUG_CPU
1602
Peter Zijlstraca109492008-11-25 12:43:51 +01001603static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001604 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001605{
1606 struct hrtimer *timer;
John Stultz998adc32010-09-20 19:19:17 -07001607 struct timerqueue_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001608
John Stultz998adc32010-09-20 19:19:17 -07001609 while ((node = timerqueue_getnext(&old_base->active))) {
1610 timer = container_of(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001611 BUG_ON(hrtimer_callback_running(timer));
Xiao Guangrongc6a2a172009-08-10 10:51:23 +08001612 debug_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001613
1614 /*
1615 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1616 * timer could be seen as !active and just vanish away
1617 * under us on another CPU
1618 */
1619 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001620 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001621 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001622 * Enqueue the timers on the new cpu. This does not
1623 * reprogram the event device in case the timer
1624 * expires before the earliest on this CPU, but we run
1625 * hrtimer_interrupt after we migrated everything to
1626 * sort out already expired timers and reprogram the
1627 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001628 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001629 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001630
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001631 /* Clear the migration state bit */
1632 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001633 }
1634}
1635
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001636static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001637{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001638 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001639 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001640
Peter Zijlstra37810652008-12-04 11:17:10 +01001641 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001642 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001643
1644 local_irq_disable();
1645 old_base = &per_cpu(hrtimer_bases, scpu);
1646 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001647 /*
1648 * The caller is globally serialized and nobody else
1649 * takes two locks at once, deadlock is not possible.
1650 */
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001651 raw_spin_lock(&new_base->lock);
1652 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001653
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001654 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001655 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001656 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001657 }
1658
Thomas Gleixnerecb49d12009-11-17 16:36:54 +01001659 raw_spin_unlock(&old_base->lock);
1660 raw_spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001661
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001662 /* Check, if we got expired work to do */
1663 __hrtimer_peek_ahead_timers();
1664 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001665}
1666
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001667#endif /* CONFIG_HOTPLUG_CPU */
1668
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001669static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001670 unsigned long action, void *hcpu)
1671{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001672 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001673
1674 switch (action) {
1675
1676 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001677 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001678 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001679 break;
1680
1681#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001682 case CPU_DYING:
1683 case CPU_DYING_FROZEN:
1684 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1685 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001686 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001687 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001688 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001689 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001690 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001691 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001692 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001693#endif
1694
1695 default:
1696 break;
1697 }
1698
1699 return NOTIFY_OK;
1700}
1701
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001702static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001703 .notifier_call = hrtimer_cpu_notify,
1704};
1705
1706void __init hrtimers_init(void)
1707{
1708 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1709 (void *)(long)smp_processor_id());
1710 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001711#ifdef CONFIG_HIGH_RES_TIMERS
1712 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1713#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001714}
1715
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001716/**
Carsten Emde351b3f72010-04-02 22:40:19 +02001717 * schedule_hrtimeout_range_clock - sleep until timeout
1718 * @expires: timeout value (ktime_t)
1719 * @delta: slack in expires timeout (ktime_t)
1720 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1721 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1722 */
1723int __sched
1724schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1725 const enum hrtimer_mode mode, int clock)
1726{
1727 struct hrtimer_sleeper t;
1728
1729 /*
1730 * Optimize when a zero timeout value is given. It does not
1731 * matter whether this is an absolute or a relative time.
1732 */
1733 if (expires && !expires->tv64) {
1734 __set_current_state(TASK_RUNNING);
1735 return 0;
1736 }
1737
1738 /*
Namhyung Kim43b21012010-12-22 19:01:47 +01001739 * A NULL parameter means "infinite"
Carsten Emde351b3f72010-04-02 22:40:19 +02001740 */
1741 if (!expires) {
1742 schedule();
1743 __set_current_state(TASK_RUNNING);
1744 return -EINTR;
1745 }
1746
1747 hrtimer_init_on_stack(&t.timer, clock, mode);
1748 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1749
1750 hrtimer_init_sleeper(&t, current);
1751
1752 hrtimer_start_expires(&t.timer, mode);
1753 if (!hrtimer_active(&t.timer))
1754 t.task = NULL;
1755
1756 if (likely(t.task))
1757 schedule();
1758
1759 hrtimer_cancel(&t.timer);
1760 destroy_hrtimer_on_stack(&t.timer);
1761
1762 __set_current_state(TASK_RUNNING);
1763
1764 return !t.task ? 0 : -EINTR;
1765}
1766
1767/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001768 * schedule_hrtimeout_range - sleep until timeout
1769 * @expires: timeout value (ktime_t)
1770 * @delta: slack in expires timeout (ktime_t)
1771 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1772 *
1773 * Make the current task sleep until the given expiry time has
1774 * elapsed. The routine will return immediately unless
1775 * the current task state has been set (see set_current_state()).
1776 *
1777 * The @delta argument gives the kernel the freedom to schedule the
1778 * actual wakeup to a time that is both power and performance friendly.
1779 * The kernel give the normal best effort behavior for "@expires+@delta",
1780 * but may decide to fire the timer earlier, but no earlier than @expires.
1781 *
1782 * You can set the task state as follows -
1783 *
1784 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1785 * pass before the routine returns.
1786 *
1787 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1788 * delivered to the current task.
1789 *
1790 * The current task state is guaranteed to be TASK_RUNNING when this
1791 * routine returns.
1792 *
1793 * Returns 0 when the timer has expired otherwise -EINTR
1794 */
1795int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
Carsten Emde351b3f72010-04-02 22:40:19 +02001796 const enum hrtimer_mode mode)
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001797{
Carsten Emde351b3f72010-04-02 22:40:19 +02001798 return schedule_hrtimeout_range_clock(expires, delta, mode,
1799 CLOCK_MONOTONIC);
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001800}
1801EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1802
1803/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001804 * schedule_hrtimeout - sleep until timeout
1805 * @expires: timeout value (ktime_t)
1806 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1807 *
1808 * Make the current task sleep until the given expiry time has
1809 * elapsed. The routine will return immediately unless
1810 * the current task state has been set (see set_current_state()).
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(ktime_t *expires,
1826 const enum hrtimer_mode mode)
1827{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001828 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001829}
1830EXPORT_SYMBOL_GPL(schedule_hrtimeout);