blob: 95d3949f2ae51135b181906f632b8de7f5e3f8e7 [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>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080035#include <linux/irq.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080036#include <linux/module.h>
37#include <linux/percpu.h>
38#include <linux/hrtimer.h>
39#include <linux/notifier.h>
40#include <linux/syscalls.h>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080041#include <linux/kallsyms.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080042#include <linux/interrupt.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080043#include <linux/tick.h>
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080044#include <linux/seq_file.h>
45#include <linux/err.h>
Thomas Gleixner237fc6e2008-04-30 00:55:04 -070046#include <linux/debugobjects.h>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080047
48#include <asm/uaccess.h>
49
50/**
51 * ktime_get - get the monotonic time in ktime_t format
52 *
53 * returns the time in ktime_t format
54 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080055ktime_t ktime_get(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080056{
57 struct timespec now;
58
59 ktime_get_ts(&now);
60
61 return timespec_to_ktime(now);
62}
Patrick McHardy641b9e02007-03-16 01:18:42 -070063EXPORT_SYMBOL_GPL(ktime_get);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080064
65/**
66 * ktime_get_real - get the real (wall-) time in ktime_t format
67 *
68 * returns the time in ktime_t format
69 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080070ktime_t ktime_get_real(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080071{
72 struct timespec now;
73
74 getnstimeofday(&now);
75
76 return timespec_to_ktime(now);
77}
78
79EXPORT_SYMBOL_GPL(ktime_get_real);
80
81/*
82 * The timer bases:
George Anzinger79786722006-02-01 03:05:11 -080083 *
84 * Note: If we want to add new timer bases, we have to skip the two
85 * clock ids captured by the cpu-timers. We do this by holding empty
86 * entries rather than doing math adjustment of the clock ids.
87 * This ensures that we capture erroneous accesses to these clock ids
88 * rather than moving them into the range of valid clock id's.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080089 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080090DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080091{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080092
93 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080094 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080095 {
96 .index = CLOCK_REALTIME,
97 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080098 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080099 },
100 {
101 .index = CLOCK_MONOTONIC,
102 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800103 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800104 },
105 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800106};
107
108/**
109 * ktime_get_ts - get the monotonic clock in timespec format
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800110 * @ts: pointer to timespec variable
111 *
112 * The function calculates the monotonic clock from the realtime
113 * clock and the wall_to_monotonic offset and stores the result
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800114 * in normalized timespec format in the variable pointed to by @ts.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800115 */
116void ktime_get_ts(struct timespec *ts)
117{
118 struct timespec tomono;
119 unsigned long seq;
120
121 do {
122 seq = read_seqbegin(&xtime_lock);
123 getnstimeofday(ts);
124 tomono = wall_to_monotonic;
125
126 } while (read_seqretry(&xtime_lock, seq));
127
128 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
129 ts->tv_nsec + tomono.tv_nsec);
130}
Matt Helsley69778e32006-01-09 20:52:39 -0800131EXPORT_SYMBOL_GPL(ktime_get_ts);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800132
133/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800134 * Get the coarse grained time at the softirq based on xtime and
135 * wall_to_monotonic.
136 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800137static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800138{
139 ktime_t xtim, tomono;
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800140 struct timespec xts, tom;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800141 unsigned long seq;
142
143 do {
144 seq = read_seqbegin(&xtime_lock);
john stultz2c6b47d2007-07-24 17:47:43 -0700145 xts = current_kernel_time();
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800146 tom = wall_to_monotonic;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800147 } while (read_seqretry(&xtime_lock, seq));
148
john stultzf4304ab2007-02-16 01:27:26 -0800149 xtim = timespec_to_ktime(xts);
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800150 tomono = timespec_to_ktime(tom);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800151 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
152 base->clock_base[CLOCK_MONOTONIC].softirq_time =
153 ktime_add(xtim, tomono);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800154}
155
156/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800157 * Functions and macros which are different for UP/SMP systems are kept in a
158 * single place
159 */
160#ifdef CONFIG_SMP
161
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800162/*
163 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
164 * means that all timers which are tied to this base via timer->base are
165 * locked, and the base itself is locked too.
166 *
167 * So __run_timers/migrate_timers can safely modify all timers which could
168 * be found on the lists/queues.
169 *
170 * When the timer's base is locked, and the timer removed from list, it is
171 * possible to set timer->base = NULL and drop the lock: the timer remains
172 * locked.
173 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800174static
175struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
176 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800177{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800178 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800179
180 for (;;) {
181 base = timer->base;
182 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800183 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800184 if (likely(base == timer->base))
185 return base;
186 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800187 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800188 }
189 cpu_relax();
190 }
191}
192
193/*
194 * Switch the timer base to the current CPU when possible.
195 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800196static inline struct hrtimer_clock_base *
197switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800198{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800199 struct hrtimer_clock_base *new_base;
200 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800201
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800202 new_cpu_base = &__get_cpu_var(hrtimer_bases);
203 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800204
205 if (base != new_base) {
206 /*
207 * We are trying to schedule the timer on the local CPU.
208 * 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 Gleixner3c8aa392007-02-16 01:27:50 -0800220 spin_unlock(&base->cpu_base->lock);
221 spin_lock(&new_base->cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800222 timer->base = new_base;
223 }
224 return new_base;
225}
226
227#else /* CONFIG_SMP */
228
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800229static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800230lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
231{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800232 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800233
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800234 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800235
236 return base;
237}
238
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800239# define switch_hrtimer_base(t, b) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800240
241#endif /* !CONFIG_SMP */
242
243/*
244 * Functions for the union type storage format of ktime_t which are
245 * too large for inlining:
246 */
247#if BITS_PER_LONG < 64
248# ifndef CONFIG_KTIME_SCALAR
249/**
250 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800251 * @kt: addend
252 * @nsec: the scalar nsec value to add
253 *
254 * Returns the sum of kt and nsec in ktime_t format
255 */
256ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
257{
258 ktime_t tmp;
259
260 if (likely(nsec < NSEC_PER_SEC)) {
261 tmp.tv64 = nsec;
262 } else {
263 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
264
265 tmp = ktime_set((long)nsec, rem);
266 }
267
268 return ktime_add(kt, tmp);
269}
David Howellsb8b8fd22007-04-27 15:31:24 -0700270
271EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700272
273/**
274 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
275 * @kt: minuend
276 * @nsec: the scalar nsec value to subtract
277 *
278 * Returns the subtraction of @nsec from @kt in ktime_t format
279 */
280ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
281{
282 ktime_t tmp;
283
284 if (likely(nsec < NSEC_PER_SEC)) {
285 tmp.tv64 = nsec;
286 } else {
287 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
288
289 tmp = ktime_set((long)nsec, rem);
290 }
291
292 return ktime_sub(kt, tmp);
293}
294
295EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800296# endif /* !CONFIG_KTIME_SCALAR */
297
298/*
299 * Divide a ktime value by a nanosecond value
300 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800301u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800302{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300303 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800304 int sft = 0;
305
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300306 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800307 /* Make sure the divisor is less than 2^32: */
308 while (div >> 32) {
309 sft++;
310 div >>= 1;
311 }
312 dclc >>= sft;
313 do_div(dclc, (unsigned long) div);
314
Davide Libenzi4d672e72008-02-04 22:27:26 -0800315 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800316}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800317#endif /* BITS_PER_LONG >= 64 */
318
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100319/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100320 * Add two ktime values and do a safety check for overflow:
321 */
322ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
323{
324 ktime_t res = ktime_add(lhs, rhs);
325
326 /*
327 * We use KTIME_SEC_MAX here, the maximum timeout which we can
328 * return to user space in a timespec:
329 */
330 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
331 res = ktime_set(KTIME_SEC_MAX, 0);
332
333 return res;
334}
335
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700336#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
337
338static struct debug_obj_descr hrtimer_debug_descr;
339
340/*
341 * fixup_init is called when:
342 * - an active object is initialized
343 */
344static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
345{
346 struct hrtimer *timer = addr;
347
348 switch (state) {
349 case ODEBUG_STATE_ACTIVE:
350 hrtimer_cancel(timer);
351 debug_object_init(timer, &hrtimer_debug_descr);
352 return 1;
353 default:
354 return 0;
355 }
356}
357
358/*
359 * fixup_activate is called when:
360 * - an active object is activated
361 * - an unknown object is activated (might be a statically initialized object)
362 */
363static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
364{
365 switch (state) {
366
367 case ODEBUG_STATE_NOTAVAILABLE:
368 WARN_ON_ONCE(1);
369 return 0;
370
371 case ODEBUG_STATE_ACTIVE:
372 WARN_ON(1);
373
374 default:
375 return 0;
376 }
377}
378
379/*
380 * fixup_free is called when:
381 * - an active object is freed
382 */
383static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
384{
385 struct hrtimer *timer = addr;
386
387 switch (state) {
388 case ODEBUG_STATE_ACTIVE:
389 hrtimer_cancel(timer);
390 debug_object_free(timer, &hrtimer_debug_descr);
391 return 1;
392 default:
393 return 0;
394 }
395}
396
397static struct debug_obj_descr hrtimer_debug_descr = {
398 .name = "hrtimer",
399 .fixup_init = hrtimer_fixup_init,
400 .fixup_activate = hrtimer_fixup_activate,
401 .fixup_free = hrtimer_fixup_free,
402};
403
404static inline void debug_hrtimer_init(struct hrtimer *timer)
405{
406 debug_object_init(timer, &hrtimer_debug_descr);
407}
408
409static inline void debug_hrtimer_activate(struct hrtimer *timer)
410{
411 debug_object_activate(timer, &hrtimer_debug_descr);
412}
413
414static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
415{
416 debug_object_deactivate(timer, &hrtimer_debug_descr);
417}
418
419static inline void debug_hrtimer_free(struct hrtimer *timer)
420{
421 debug_object_free(timer, &hrtimer_debug_descr);
422}
423
424static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
425 enum hrtimer_mode mode);
426
427void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
428 enum hrtimer_mode mode)
429{
430 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
431 __hrtimer_init(timer, clock_id, mode);
432}
433
434void destroy_hrtimer_on_stack(struct hrtimer *timer)
435{
436 debug_object_free(timer, &hrtimer_debug_descr);
437}
438
439#else
440static inline void debug_hrtimer_init(struct hrtimer *timer) { }
441static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
442static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
443#endif
444
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100445/*
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100446 * Check, whether the timer is on the callback pending list
447 */
448static inline int hrtimer_cb_pending(const struct hrtimer *timer)
449{
450 return timer->state & HRTIMER_STATE_PENDING;
451}
452
453/*
454 * Remove a timer from the callback pending list
455 */
456static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
457{
458 list_del_init(&timer->cb_entry);
459}
460
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800461/* High resolution timer related functions */
462#ifdef CONFIG_HIGH_RES_TIMERS
463
464/*
465 * High resolution timer enabled ?
466 */
467static int hrtimer_hres_enabled __read_mostly = 1;
468
469/*
470 * Enable / Disable high resolution mode
471 */
472static int __init setup_hrtimer_hres(char *str)
473{
474 if (!strcmp(str, "off"))
475 hrtimer_hres_enabled = 0;
476 else if (!strcmp(str, "on"))
477 hrtimer_hres_enabled = 1;
478 else
479 return 0;
480 return 1;
481}
482
483__setup("highres=", setup_hrtimer_hres);
484
485/*
486 * hrtimer_high_res_enabled - query, if the highres mode is enabled
487 */
488static inline int hrtimer_is_hres_enabled(void)
489{
490 return hrtimer_hres_enabled;
491}
492
493/*
494 * Is the high resolution mode active ?
495 */
496static inline int hrtimer_hres_active(void)
497{
498 return __get_cpu_var(hrtimer_bases).hres_active;
499}
500
501/*
502 * Reprogram the event source with checking both queues for the
503 * next event
504 * Called with interrupts disabled and base->lock held
505 */
506static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
507{
508 int i;
509 struct hrtimer_clock_base *base = cpu_base->clock_base;
510 ktime_t expires;
511
512 cpu_base->expires_next.tv64 = KTIME_MAX;
513
514 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
515 struct hrtimer *timer;
516
517 if (!base->first)
518 continue;
519 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700520 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800521 if (expires.tv64 < cpu_base->expires_next.tv64)
522 cpu_base->expires_next = expires;
523 }
524
525 if (cpu_base->expires_next.tv64 != KTIME_MAX)
526 tick_program_event(cpu_base->expires_next, 1);
527}
528
529/*
530 * Shared reprogramming for clock_realtime and clock_monotonic
531 *
532 * When a timer is enqueued and expires earlier than the already enqueued
533 * timers, we have to check, whether it expires earlier than the timer for
534 * which the clock event device was armed.
535 *
536 * Called with interrupts disabled and base->cpu_base.lock held
537 */
538static int hrtimer_reprogram(struct hrtimer *timer,
539 struct hrtimer_clock_base *base)
540{
541 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700542 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800543 int res;
544
Arjan van de Vencc584b22008-09-01 15:02:30 -0700545 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100546
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800547 /*
548 * When the callback is running, we do not reprogram the clock event
549 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200550 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800551 * reprogramming is handled either by the softirq, which called the
552 * callback or at the end of the hrtimer_interrupt.
553 */
554 if (hrtimer_callback_running(timer))
555 return 0;
556
Thomas Gleixner63070a72008-02-14 00:58:36 +0100557 /*
558 * CLOCK_REALTIME timer might be requested with an absolute
559 * expiry time which is less than base->offset. Nothing wrong
560 * about that, just avoid to call into the tick code, which
561 * has now objections against negative expiry values.
562 */
563 if (expires.tv64 < 0)
564 return -ETIME;
565
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800566 if (expires.tv64 >= expires_next->tv64)
567 return 0;
568
569 /*
570 * Clockevents returns -ETIME, when the event was in the past.
571 */
572 res = tick_program_event(expires, 0);
573 if (!IS_ERR_VALUE(res))
574 *expires_next = expires;
575 return res;
576}
577
578
579/*
580 * Retrigger next event is called after clock was set
581 *
582 * Called with interrupts disabled via on_each_cpu()
583 */
584static void retrigger_next_event(void *arg)
585{
586 struct hrtimer_cpu_base *base;
587 struct timespec realtime_offset;
588 unsigned long seq;
589
590 if (!hrtimer_hres_active())
591 return;
592
593 do {
594 seq = read_seqbegin(&xtime_lock);
595 set_normalized_timespec(&realtime_offset,
596 -wall_to_monotonic.tv_sec,
597 -wall_to_monotonic.tv_nsec);
598 } while (read_seqretry(&xtime_lock, seq));
599
600 base = &__get_cpu_var(hrtimer_bases);
601
602 /* Adjust CLOCK_REALTIME offset */
603 spin_lock(&base->lock);
604 base->clock_base[CLOCK_REALTIME].offset =
605 timespec_to_ktime(realtime_offset);
606
607 hrtimer_force_reprogram(base);
608 spin_unlock(&base->lock);
609}
610
611/*
612 * Clock realtime was set
613 *
614 * Change the offset of the realtime clock vs. the monotonic
615 * clock.
616 *
617 * We might have to reprogram the high resolution timer interrupt. On
618 * SMP we call the architecture specific code to retrigger _all_ high
619 * resolution timer interrupts. On UP we just disable interrupts and
620 * call the high resolution interrupt code.
621 */
622void clock_was_set(void)
623{
624 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200625 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800626}
627
628/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200629 * During resume we might have to reprogram the high resolution timer
630 * interrupt (on the local CPU):
631 */
632void hres_timers_resume(void)
633{
Ingo Molnar995f0542007-04-07 12:05:00 +0200634 /* Retrigger the CPU local events: */
635 retrigger_next_event(NULL);
636}
637
638/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800639 * Initialize the high resolution related parts of cpu_base
640 */
641static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
642{
643 base->expires_next.tv64 = KTIME_MAX;
644 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800645}
646
647/*
648 * Initialize the high resolution related parts of a hrtimer
649 */
650static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
651{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800652}
653
654/*
655 * When High resolution timers are active, try to reprogram. Note, that in case
656 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
657 * check happens. The timer gets enqueued into the rbtree. The reprogramming
658 * and expiry check is done in the hrtimer_interrupt or in the softirq.
659 */
660static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
661 struct hrtimer_clock_base *base)
662{
663 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
664
665 /* Timer is expired, act upon the callback mode */
666 switch(timer->cb_mode) {
667 case HRTIMER_CB_IRQSAFE_NO_RESTART:
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700668 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800669 /*
670 * We can call the callback from here. No restart
671 * happens, so no danger of recursion
672 */
673 BUG_ON(timer->function(timer) != HRTIMER_NORESTART);
674 return 1;
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +0200675 case HRTIMER_CB_IRQSAFE_PERCPU:
676 case HRTIMER_CB_IRQSAFE_UNLOCKED:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800677 /*
678 * This is solely for the sched tick emulation with
679 * dynamic tick support to ensure that we do not
680 * restart the tick right on the edge and end up with
681 * the tick timer in the softirq ! The calling site
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +0200682 * takes care of this. Also used for hrtimer sleeper !
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800683 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700684 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800685 return 1;
686 case HRTIMER_CB_IRQSAFE:
687 case HRTIMER_CB_SOFTIRQ:
688 /*
689 * Move everything else into the softirq pending list !
690 */
691 list_add_tail(&timer->cb_entry,
692 &base->cpu_base->cb_pending);
693 timer->state = HRTIMER_STATE_PENDING;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800694 return 1;
695 default:
696 BUG();
697 }
698 }
699 return 0;
700}
701
702/*
703 * Switch to high resolution mode
704 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800705static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800706{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700707 int cpu = smp_processor_id();
708 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800709 unsigned long flags;
710
711 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800712 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800713
714 local_irq_save(flags);
715
716 if (tick_init_highres()) {
717 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700718 printk(KERN_WARNING "Could not switch to high resolution "
719 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800720 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800721 }
722 base->hres_active = 1;
723 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
724 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
725
726 tick_setup_sched_timer();
727
728 /* "Retrigger" the interrupt to get things going */
729 retrigger_next_event(NULL);
730 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100731 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800732 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800733 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800734}
735
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200736static inline void hrtimer_raise_softirq(void)
737{
738 raise_softirq(HRTIMER_SOFTIRQ);
739}
740
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800741#else
742
743static inline int hrtimer_hres_active(void) { return 0; }
744static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800745static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800746static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
747static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
748 struct hrtimer_clock_base *base)
749{
750 return 0;
751}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800752static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
753static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100754static inline int hrtimer_reprogram(struct hrtimer *timer,
755 struct hrtimer_clock_base *base)
756{
757 return 0;
758}
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200759static inline void hrtimer_raise_softirq(void) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800760
761#endif /* CONFIG_HIGH_RES_TIMERS */
762
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800763#ifdef CONFIG_TIMER_STATS
764void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
765{
766 if (timer->start_site)
767 return;
768
769 timer->start_site = addr;
770 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
771 timer->start_pid = current->pid;
772}
773#endif
774
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800775/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200776 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800777 */
778static inline
779void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
780{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800781 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800782}
783
784/**
785 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800786 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800787 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800788 * @interval: the interval to forward
789 *
790 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700791 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800792 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800793u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800794{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800795 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800796 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800797
Arjan van de Vencc584b22008-09-01 15:02:30 -0700798 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800799
800 if (delta.tv64 < 0)
801 return 0;
802
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100803 if (interval.tv64 < timer->base->resolution.tv64)
804 interval.tv64 = timer->base->resolution.tv64;
805
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800806 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800807 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800808
809 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700810 hrtimer_add_expires_ns(timer, incr * orun);
811 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812 return orun;
813 /*
814 * This (and the ktime_add() below) is the
815 * correction for exact:
816 */
817 orun++;
818 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700819 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800820
821 return orun;
822}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700823EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800824
825/*
826 * enqueue_hrtimer - internal function to (re)start a timer
827 *
828 * The timer is inserted in expiry order. Insertion into the
829 * red black tree is O(log(n)). Must hold the base lock.
830 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800831static void enqueue_hrtimer(struct hrtimer *timer,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800832 struct hrtimer_clock_base *base, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800833{
834 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800835 struct rb_node *parent = NULL;
836 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700837 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800838
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700839 debug_hrtimer_activate(timer);
840
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800841 /*
842 * Find the right place in the rbtree:
843 */
844 while (*link) {
845 parent = *link;
846 entry = rb_entry(parent, struct hrtimer, node);
847 /*
848 * We dont care about collisions. Nodes with
849 * the same expiry time stay together.
850 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700851 if (hrtimer_get_expires_tv64(timer) <
852 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800853 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700854 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800855 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700856 leftmost = 0;
857 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800858 }
859
860 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100861 * Insert the timer to the rbtree and check whether it
862 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800863 */
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700864 if (leftmost) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800865 /*
866 * Reprogram the clock event device. When the timer is already
867 * expired hrtimer_enqueue_reprogram has either called the
868 * callback or added it to the pending list and raised the
869 * softirq.
870 *
871 * This is a NOP for !HIGHRES
872 */
873 if (reprogram && hrtimer_enqueue_reprogram(timer, base))
874 return;
875
876 base->first = &timer->node;
877 }
878
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800879 rb_link_node(&timer->node, parent, link);
880 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800881 /*
882 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
883 * state of a possibly running callback.
884 */
885 timer->state |= HRTIMER_STATE_ENQUEUED;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100886}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800887
888/*
889 * __remove_hrtimer - internal function to remove a timer
890 *
891 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800892 *
893 * High resolution timer mode reprograms the clock event device when the
894 * timer is the one which expires next. The caller can disable this by setting
895 * reprogram to zero. This is useful, when the context does a reprogramming
896 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800897 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800898static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800899 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800900 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800901{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800902 /* High res. callback list. NOP for !HIGHRES */
903 if (hrtimer_cb_pending(timer))
904 hrtimer_remove_cb_pending(timer);
905 else {
906 /*
907 * Remove the timer from the rbtree and replace the
908 * first entry pointer if necessary.
909 */
910 if (base->first == &timer->node) {
911 base->first = rb_next(&timer->node);
912 /* Reprogram the clock event device. if enabled */
913 if (reprogram && hrtimer_hres_active())
914 hrtimer_force_reprogram(base->cpu_base);
915 }
916 rb_erase(&timer->node, &base->active);
917 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800918 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800919}
920
921/*
922 * remove hrtimer, called with base lock held
923 */
924static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800925remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800926{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800927 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800928 int reprogram;
929
930 /*
931 * Remove the timer and force reprogramming when high
932 * resolution mode is active and the timer is on the current
933 * CPU. If we remove a timer on another CPU, reprogramming is
934 * skipped. The interrupt event on this CPU is fired and
935 * reprogramming happens in the interrupt handler. This is a
936 * rare case and less expensive than a smp call.
937 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700938 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800939 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800940 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
941 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
942 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800943 return 1;
944 }
945 return 0;
946}
947
948/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +0200949 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800950 * @timer: the timer to be added
951 * @tim: expiry time
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700952 * @delta_ns: "slack" range for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800953 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
954 *
955 * Returns:
956 * 0 on success
957 * 1 when the timer was active
958 */
959int
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700960hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
961 const enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800962{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800963 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800964 unsigned long flags;
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200965 int ret, raise;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800966
967 base = lock_hrtimer_base(timer, &flags);
968
969 /* Remove an active timer from the queue: */
970 ret = remove_hrtimer(timer, base);
971
972 /* Switch the timer base, if necessary: */
973 new_base = switch_hrtimer_base(timer, base);
974
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800975 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100976 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800977 /*
978 * CONFIG_TIME_LOW_RES is a temporary way for architectures
979 * to signal that they simply return xtime in
980 * do_gettimeoffset(). In this case we want to round up by
981 * resolution when starting a relative timer, to avoid short
982 * timeouts. This will go away with the GTOD framework.
983 */
984#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100985 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800986#endif
987 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700988
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700989 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800990
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800991 timer_stats_hrtimer_set_start_info(timer);
992
Ingo Molnar935c6312007-03-28 13:17:18 +0200993 /*
994 * Only allow reprogramming if the new base is on this CPU.
995 * (it might still be on another CPU if the timer was pending)
996 */
997 enqueue_hrtimer(timer, new_base,
998 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800999
Thomas Gleixner0c96c592008-04-28 09:23:24 +02001000 /*
1001 * The timer may be expired and moved to the cb_pending
1002 * list. We can not raise the softirq with base lock held due
1003 * to a possible deadlock with runqueue lock.
1004 */
1005 raise = timer->state == HRTIMER_STATE_PENDING;
1006
Steven Rostedtee3ece82008-07-03 14:31:26 -04001007 /*
1008 * We use preempt_disable to prevent this task from migrating after
1009 * setting up the softirq and raising it. Otherwise, if me migrate
1010 * we will raise the softirq on the wrong CPU.
1011 */
1012 preempt_disable();
1013
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001014 unlock_hrtimer_base(timer, &flags);
1015
Thomas Gleixner0c96c592008-04-28 09:23:24 +02001016 if (raise)
1017 hrtimer_raise_softirq();
Steven Rostedtee3ece82008-07-03 14:31:26 -04001018 preempt_enable();
Thomas Gleixner0c96c592008-04-28 09:23:24 +02001019
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001020 return ret;
1021}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001022EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1023
1024/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001025 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001026 * @timer: the timer to be added
1027 * @tim: expiry time
1028 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1029 *
1030 * Returns:
1031 * 0 on success
1032 * 1 when the timer was active
1033 */
1034int
1035hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1036{
1037 return hrtimer_start_range_ns(timer, tim, 0, mode);
1038}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001039EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001040
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001041
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001042/**
1043 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001044 * @timer: hrtimer to stop
1045 *
1046 * Returns:
1047 * 0 when the timer was not active
1048 * 1 when the timer was active
1049 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001050 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001051 */
1052int hrtimer_try_to_cancel(struct hrtimer *timer)
1053{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001054 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001055 unsigned long flags;
1056 int ret = -1;
1057
1058 base = lock_hrtimer_base(timer, &flags);
1059
Thomas Gleixner303e9672007-02-16 01:27:51 -08001060 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001061 ret = remove_hrtimer(timer, base);
1062
1063 unlock_hrtimer_base(timer, &flags);
1064
1065 return ret;
1066
1067}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001068EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001069
1070/**
1071 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001072 * @timer: the timer to be cancelled
1073 *
1074 * Returns:
1075 * 0 when the timer was not active
1076 * 1 when the timer was active
1077 */
1078int hrtimer_cancel(struct hrtimer *timer)
1079{
1080 for (;;) {
1081 int ret = hrtimer_try_to_cancel(timer);
1082
1083 if (ret >= 0)
1084 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001085 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001086 }
1087}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001088EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001089
1090/**
1091 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001092 * @timer: the timer to read
1093 */
1094ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1095{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001096 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001097 unsigned long flags;
1098 ktime_t rem;
1099
1100 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001101 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001102 unlock_hrtimer_base(timer, &flags);
1103
1104 return rem;
1105}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001106EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001107
Russell Kingee9c5782008-04-20 13:59:33 +01001108#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001109/**
1110 * hrtimer_get_next_event - get the time until next expiry event
1111 *
1112 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1113 * is pending.
1114 */
1115ktime_t hrtimer_get_next_event(void)
1116{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001117 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1118 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001119 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1120 unsigned long flags;
1121 int i;
1122
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001123 spin_lock_irqsave(&cpu_base->lock, flags);
1124
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001125 if (!hrtimer_hres_active()) {
1126 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1127 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001128
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001129 if (!base->first)
1130 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001131
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001132 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001133 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001134 delta = ktime_sub(delta, base->get_time());
1135 if (delta.tv64 < mindelta.tv64)
1136 mindelta.tv64 = delta.tv64;
1137 }
Tony Lindgren69239742006-03-06 15:42:45 -08001138 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001139
1140 spin_unlock_irqrestore(&cpu_base->lock, flags);
1141
Tony Lindgren69239742006-03-06 15:42:45 -08001142 if (mindelta.tv64 < 0)
1143 mindelta.tv64 = 0;
1144 return mindelta;
1145}
1146#endif
1147
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001148static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1149 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001150{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001151 struct hrtimer_cpu_base *cpu_base;
George Anzinger79786722006-02-01 03:05:11 -08001152
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001153 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger79786722006-02-01 03:05:11 -08001154
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001155 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger79786722006-02-01 03:05:11 -08001156
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001157 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger79786722006-02-01 03:05:11 -08001158 clock_id = CLOCK_MONOTONIC;
1159
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001160 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001161 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001162 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001163
1164#ifdef CONFIG_TIMER_STATS
1165 timer->start_site = NULL;
1166 timer->start_pid = -1;
1167 memset(timer->start_comm, 0, TASK_COMM_LEN);
1168#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001169}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001170
1171/**
1172 * hrtimer_init - initialize a timer to the given clock
1173 * @timer: the timer to be initialized
1174 * @clock_id: the clock to be used
1175 * @mode: timer mode abs/rel
1176 */
1177void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1178 enum hrtimer_mode mode)
1179{
1180 debug_hrtimer_init(timer);
1181 __hrtimer_init(timer, clock_id, mode);
1182}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001183EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001184
1185/**
1186 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001187 * @which_clock: which clock to query
1188 * @tp: pointer to timespec variable to store the resolution
1189 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001190 * Store the resolution of the clock selected by @which_clock in the
1191 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001192 */
1193int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1194{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001195 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001196
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001197 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1198 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001199
1200 return 0;
1201}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001202EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001203
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001204static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001205{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001206 spin_lock_irq(&cpu_base->lock);
1207
1208 while (!list_empty(&cpu_base->cb_pending)) {
1209 enum hrtimer_restart (*fn)(struct hrtimer *);
1210 struct hrtimer *timer;
1211 int restart;
Gautham R Shenoy5d5254f2008-10-25 10:22:38 +05301212 int emulate_hardirq_ctx = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001213
1214 timer = list_entry(cpu_base->cb_pending.next,
1215 struct hrtimer, cb_entry);
1216
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001217 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001218 timer_stats_account_hrtimer(timer);
1219
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001220 fn = timer->function;
Gautham R Shenoy5d5254f2008-10-25 10:22:38 +05301221 /*
1222 * A timer might have been added to the cb_pending list
1223 * when it was migrated during a cpu-offline operation.
1224 * Emulate hardirq context for such timers.
1225 */
1226 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU ||
1227 timer->cb_mode == HRTIMER_CB_IRQSAFE_UNLOCKED)
1228 emulate_hardirq_ctx = 1;
1229
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001230 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
1231 spin_unlock_irq(&cpu_base->lock);
1232
Gautham R Shenoy5d5254f2008-10-25 10:22:38 +05301233 if (unlikely(emulate_hardirq_ctx)) {
1234 local_irq_disable();
1235 restart = fn(timer);
1236 local_irq_enable();
1237 } else
1238 restart = fn(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001239
1240 spin_lock_irq(&cpu_base->lock);
1241
1242 timer->state &= ~HRTIMER_STATE_CALLBACK;
1243 if (restart == HRTIMER_RESTART) {
1244 BUG_ON(hrtimer_active(timer));
1245 /*
1246 * Enqueue the timer, allow reprogramming of the event
1247 * device
1248 */
1249 enqueue_hrtimer(timer, timer->base, 1);
1250 } else if (hrtimer_active(timer)) {
1251 /*
1252 * If the timer was rearmed on another CPU, reprogram
1253 * the event device.
1254 */
Bodo Stroesserd7b41a22008-04-26 14:10:16 -07001255 struct hrtimer_clock_base *base = timer->base;
1256
1257 if (base->first == &timer->node &&
1258 hrtimer_reprogram(timer, base)) {
1259 /*
1260 * Timer is expired. Thus move it from tree to
1261 * pending list again.
1262 */
1263 __remove_hrtimer(timer, base,
1264 HRTIMER_STATE_PENDING, 0);
1265 list_add_tail(&timer->cb_entry,
1266 &base->cpu_base->cb_pending);
1267 }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001268 }
1269 }
1270 spin_unlock_irq(&cpu_base->lock);
1271}
1272
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001273static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001274{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001275 struct hrtimer_clock_base *base = timer->base;
1276 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1277 enum hrtimer_restart (*fn)(struct hrtimer *);
1278 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001279
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001280 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001281 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1282 timer_stats_account_hrtimer(timer);
Dimitri Sivanich3055add2006-03-31 02:31:20 -08001283
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001284 fn = timer->function;
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +02001285 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU ||
1286 timer->cb_mode == HRTIMER_CB_IRQSAFE_UNLOCKED) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001287 /*
1288 * Used for scheduler timers, avoid lock inversion with
1289 * rq->lock and tasklist_lock.
1290 *
1291 * These timers are required to deal with enqueue expiry
1292 * themselves and are not allowed to migrate.
1293 */
1294 spin_unlock(&cpu_base->lock);
1295 restart = fn(timer);
1296 spin_lock(&cpu_base->lock);
1297 } else
Roman Zippel05cfb612006-03-26 01:38:12 -08001298 restart = fn(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001299
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001300 /*
1301 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1302 * reprogramming of the event hardware. This happens at the end of this
1303 * function anyway.
1304 */
1305 if (restart != HRTIMER_NORESTART) {
1306 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1307 enqueue_hrtimer(timer, base, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001308 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001309 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001310}
1311
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001312#ifdef CONFIG_HIGH_RES_TIMERS
1313
1314/*
1315 * High resolution timer interrupt
1316 * Called with interrupts disabled
1317 */
1318void hrtimer_interrupt(struct clock_event_device *dev)
1319{
1320 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1321 struct hrtimer_clock_base *base;
1322 ktime_t expires_next, now;
1323 int i, raise = 0;
1324
1325 BUG_ON(!cpu_base->hres_active);
1326 cpu_base->nr_events++;
1327 dev->next_event.tv64 = KTIME_MAX;
1328
1329 retry:
1330 now = ktime_get();
1331
1332 expires_next.tv64 = KTIME_MAX;
1333
1334 base = cpu_base->clock_base;
1335
1336 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1337 ktime_t basenow;
1338 struct rb_node *node;
1339
1340 spin_lock(&cpu_base->lock);
1341
1342 basenow = ktime_add(now, base->offset);
1343
1344 while ((node = base->first)) {
1345 struct hrtimer *timer;
1346
1347 timer = rb_entry(node, struct hrtimer, node);
1348
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001349 /*
1350 * The immediate goal for using the softexpires is
1351 * minimizing wakeups, not running timers at the
1352 * earliest interrupt after their soft expiration.
1353 * This allows us to avoid using a Priority Search
1354 * Tree, which can answer a stabbing querry for
1355 * overlapping intervals and instead use the simple
1356 * BST we already have.
1357 * We don't add extra wakeups by delaying timers that
1358 * are right-of a not yet expired timer, because that
1359 * timer will have to trigger a wakeup anyway.
1360 */
1361
1362 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001363 ktime_t expires;
1364
Arjan van de Vencc584b22008-09-01 15:02:30 -07001365 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001366 base->offset);
1367 if (expires.tv64 < expires_next.tv64)
1368 expires_next = expires;
1369 break;
1370 }
1371
1372 /* Move softirq callbacks to the pending list */
1373 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1374 __remove_hrtimer(timer, base,
1375 HRTIMER_STATE_PENDING, 0);
1376 list_add_tail(&timer->cb_entry,
1377 &base->cpu_base->cb_pending);
1378 raise = 1;
1379 continue;
1380 }
1381
1382 __run_hrtimer(timer);
1383 }
1384 spin_unlock(&cpu_base->lock);
1385 base++;
1386 }
1387
1388 cpu_base->expires_next = expires_next;
1389
1390 /* Reprogramming necessary ? */
1391 if (expires_next.tv64 != KTIME_MAX) {
1392 if (tick_program_event(expires_next, 0))
1393 goto retry;
1394 }
1395
1396 /* Raise softirq ? */
1397 if (raise)
1398 raise_softirq(HRTIMER_SOFTIRQ);
1399}
1400
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001401/**
1402 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1403 *
1404 * hrtimer_peek_ahead_timers will peek at the timer queue of
1405 * the current cpu and check if there are any timers for which
1406 * the soft expires time has passed. If any such timers exist,
1407 * they are run immediately and then removed from the timer queue.
1408 *
1409 */
1410void hrtimer_peek_ahead_timers(void)
1411{
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001412 struct tick_device *td;
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001413 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001414
1415 if (!hrtimer_hres_active())
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001416 return;
1417
1418 local_irq_save(flags);
1419 td = &__get_cpu_var(tick_cpu_device);
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001420 if (td && td->evtdev)
1421 hrtimer_interrupt(td->evtdev);
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001422 local_irq_restore(flags);
1423}
1424
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001425static void run_hrtimer_softirq(struct softirq_action *h)
1426{
1427 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
1428}
1429
1430#endif /* CONFIG_HIGH_RES_TIMERS */
1431
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001432/*
1433 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001434 *
1435 * For HRT its the fall back code to run the softirq in the timer
1436 * softirq context in case the hrtimer initialization failed or has
1437 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001438 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001439void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001440{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001441 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001442
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001443 if (hrtimer_hres_active())
1444 return;
1445
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001446 /*
1447 * This _is_ ugly: We have to check in the softirq context,
1448 * whether we can switch to highres and / or nohz mode. The
1449 * clocksource switch happens in the timer interrupt with
1450 * xtime_lock held. Notification from there only sets the
1451 * check bit in the tick_oneshot code, otherwise we might
1452 * deadlock vs. xtime_lock.
1453 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001454 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001455 hrtimer_switch_to_hres();
1456
1457 run_hrtimer_pending(cpu_base);
1458}
1459
1460/*
1461 * Called from hardirq context every jiffy
1462 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001463void hrtimer_run_queues(void)
1464{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001465 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001466 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001467 struct hrtimer_clock_base *base;
1468 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001469
1470 if (hrtimer_hres_active())
1471 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001472
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001473 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1474 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001475
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001476 if (!base->first)
1477 continue;
1478
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001479 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001480 hrtimer_get_softirq_time(cpu_base);
1481 gettime = 0;
1482 }
1483
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001484 spin_lock(&cpu_base->lock);
1485
1486 while ((node = base->first)) {
1487 struct hrtimer *timer;
1488
1489 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001490 if (base->softirq_time.tv64 <=
1491 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001492 break;
1493
1494 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1495 __remove_hrtimer(timer, base,
1496 HRTIMER_STATE_PENDING, 0);
1497 list_add_tail(&timer->cb_entry,
1498 &base->cpu_base->cb_pending);
1499 continue;
1500 }
1501
1502 __run_hrtimer(timer);
1503 }
1504 spin_unlock(&cpu_base->lock);
1505 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001506}
1507
1508/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001509 * Sleep related functions:
1510 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001511static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001512{
1513 struct hrtimer_sleeper *t =
1514 container_of(timer, struct hrtimer_sleeper, timer);
1515 struct task_struct *task = t->task;
1516
1517 t->task = NULL;
1518 if (task)
1519 wake_up_process(task);
1520
1521 return HRTIMER_NORESTART;
1522}
1523
Ingo Molnar36c8b582006-07-03 00:25:41 -07001524void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001525{
1526 sl->timer.function = hrtimer_wakeup;
1527 sl->task = task;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001528#ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +02001529 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_UNLOCKED;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001530#endif
Thomas Gleixner00362e32006-03-31 02:31:17 -08001531}
1532
Thomas Gleixner669d7862006-03-31 02:31:19 -08001533static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001534{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001535 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001536
Roman Zippel432569b2006-03-26 01:38:08 -08001537 do {
1538 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001539 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001540 if (!hrtimer_active(&t->timer))
1541 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001542
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001543 if (likely(t->task))
1544 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001545
Thomas Gleixner669d7862006-03-31 02:31:19 -08001546 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001547 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001548
Thomas Gleixner669d7862006-03-31 02:31:19 -08001549 } while (t->task && !signal_pending(current));
1550
Peter Zijlstra3588a082008-02-01 17:45:13 +01001551 __set_current_state(TASK_RUNNING);
1552
Thomas Gleixner669d7862006-03-31 02:31:19 -08001553 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001554}
1555
Oleg Nesterov080344b2008-02-01 17:29:05 +03001556static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1557{
1558 struct timespec rmt;
1559 ktime_t rem;
1560
Arjan van de Vencc584b22008-09-01 15:02:30 -07001561 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001562 if (rem.tv64 <= 0)
1563 return 0;
1564 rmt = ktime_to_timespec(rem);
1565
1566 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1567 return -EFAULT;
1568
1569 return 1;
1570}
1571
Toyo Abe1711ef32006-09-29 02:00:28 -07001572long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001573{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001574 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001575 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001576 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001577
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001578 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1579 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001580 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001581
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001582 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001583 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001584
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001585 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001586 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001587 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001588 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001589 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001590 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001591
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001592 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001593 ret = -ERESTART_RESTARTBLOCK;
1594out:
1595 destroy_hrtimer_on_stack(&t.timer);
1596 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001597}
1598
Oleg Nesterov080344b2008-02-01 17:29:05 +03001599long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001600 const enum hrtimer_mode mode, const clockid_t clockid)
1601{
1602 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001603 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001604 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001605 unsigned long slack;
1606
1607 slack = current->timer_slack_ns;
1608 if (rt_task(current))
1609 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001610
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001611 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001612 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001613 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001614 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001615
George Anzinger79786722006-02-01 03:05:11 -08001616 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001617 if (mode == HRTIMER_MODE_ABS) {
1618 ret = -ERESTARTNOHAND;
1619 goto out;
1620 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001621
Roman Zippel432569b2006-03-26 01:38:08 -08001622 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001623 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001624 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001625 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001626 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001627
1628 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001629 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001630 restart->nanosleep.index = t.timer.base->index;
1631 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001632 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001633
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001634 ret = -ERESTART_RESTARTBLOCK;
1635out:
1636 destroy_hrtimer_on_stack(&t.timer);
1637 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001638}
1639
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001640asmlinkage long
1641sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1642{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001643 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001644
1645 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1646 return -EFAULT;
1647
1648 if (!timespec_valid(&tu))
1649 return -EINVAL;
1650
Oleg Nesterov080344b2008-02-01 17:29:05 +03001651 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001652}
1653
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001654/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001655 * Functions related to boot-time initialization:
1656 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001657static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001658{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001659 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001660 int i;
1661
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001662 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001663
1664 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1665 cpu_base->clock_base[i].cpu_base = cpu_base;
1666
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001667 INIT_LIST_HEAD(&cpu_base->cb_pending);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001668 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001669}
1670
1671#ifdef CONFIG_HOTPLUG_CPU
1672
Thomas Gleixner41e10222008-09-29 14:09:39 +02001673static int migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +02001674 struct hrtimer_clock_base *new_base, int dcpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001675{
1676 struct hrtimer *timer;
1677 struct rb_node *node;
Thomas Gleixner41e10222008-09-29 14:09:39 +02001678 int raise = 0;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001679
1680 while ((node = rb_first(&old_base->active))) {
1681 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001682 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001683 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001684
1685 /*
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +02001686 * Should not happen. Per CPU timers should be
1687 * canceled _before_ the migration code is called
1688 */
1689 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU) {
1690 __remove_hrtimer(timer, old_base,
1691 HRTIMER_STATE_INACTIVE, 0);
1692 WARN(1, "hrtimer (%p %p)active but cpu %d dead\n",
1693 timer, timer->function, dcpu);
1694 continue;
1695 }
1696
1697 /*
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001698 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1699 * timer could be seen as !active and just vanish away
1700 * under us on another CPU
1701 */
1702 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001703 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001704 /*
1705 * Enqueue the timer. Allow reprogramming of the event device
1706 */
1707 enqueue_hrtimer(timer, new_base, 1);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001708
1709#ifdef CONFIG_HIGH_RES_TIMERS
1710 /*
1711 * Happens with high res enabled when the timer was
1712 * already expired and the callback mode is
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +02001713 * HRTIMER_CB_IRQSAFE_UNLOCKED (hrtimer_sleeper). The
1714 * enqueue code does not move them to the soft irq
1715 * pending list for performance/latency reasons, but
1716 * in the migration state, we need to do that
1717 * otherwise we end up with a stale timer.
Thomas Gleixner41e10222008-09-29 14:09:39 +02001718 */
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001719 if (timer->state == HRTIMER_STATE_MIGRATE) {
Thomas Gleixner41e10222008-09-29 14:09:39 +02001720 timer->state = HRTIMER_STATE_PENDING;
1721 list_add_tail(&timer->cb_entry,
1722 &new_base->cpu_base->cb_pending);
1723 raise = 1;
1724 }
1725#endif
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001726 /* Clear the migration state bit */
1727 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001728 }
Thomas Gleixner41e10222008-09-29 14:09:39 +02001729 return raise;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001730}
1731
Thomas Gleixner7659e342008-09-29 14:06:45 +02001732#ifdef CONFIG_HIGH_RES_TIMERS
1733static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base,
1734 struct hrtimer_cpu_base *new_base)
1735{
1736 struct hrtimer *timer;
1737 int raise = 0;
1738
1739 while (!list_empty(&old_base->cb_pending)) {
1740 timer = list_entry(old_base->cb_pending.next,
1741 struct hrtimer, cb_entry);
1742
1743 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_PENDING, 0);
1744 timer->base = &new_base->clock_base[timer->base->index];
1745 list_add_tail(&timer->cb_entry, &new_base->cb_pending);
1746 raise = 1;
1747 }
1748 return raise;
1749}
1750#else
1751static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base,
1752 struct hrtimer_cpu_base *new_base)
1753{
1754 return 0;
1755}
1756#endif
1757
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001758static void migrate_hrtimers(int cpu)
1759{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001760 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner7659e342008-09-29 14:06:45 +02001761 int i, raise = 0;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001762
1763 BUG_ON(cpu_online(cpu));
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001764 old_base = &per_cpu(hrtimer_bases, cpu);
1765 new_base = &get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001766
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001767 tick_cancel_sched_timer(cpu);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001768 /*
1769 * The caller is globally serialized and nobody else
1770 * takes two locks at once, deadlock is not possible.
1771 */
1772 spin_lock_irq(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001773 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001774
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001775 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner41e10222008-09-29 14:09:39 +02001776 if (migrate_hrtimer_list(&old_base->clock_base[i],
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +02001777 &new_base->clock_base[i], cpu))
Thomas Gleixner41e10222008-09-29 14:09:39 +02001778 raise = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001779 }
1780
Thomas Gleixner7659e342008-09-29 14:06:45 +02001781 if (migrate_hrtimer_pending(old_base, new_base))
1782 raise = 1;
1783
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001784 spin_unlock(&old_base->lock);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001785 spin_unlock_irq(&new_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001786 put_cpu_var(hrtimer_bases);
Thomas Gleixner7659e342008-09-29 14:06:45 +02001787
1788 if (raise)
1789 hrtimer_raise_softirq();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001790}
1791#endif /* CONFIG_HOTPLUG_CPU */
1792
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001793static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001794 unsigned long action, void *hcpu)
1795{
David Miller7713a7d2007-07-16 17:17:44 -07001796 unsigned int cpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001797
1798 switch (action) {
1799
1800 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001801 case CPU_UP_PREPARE_FROZEN:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001802 init_hrtimers_cpu(cpu);
1803 break;
1804
1805#ifdef CONFIG_HOTPLUG_CPU
1806 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001807 case CPU_DEAD_FROZEN:
Thomas Gleixnerd316c572007-02-16 01:28:00 -08001808 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001809 migrate_hrtimers(cpu);
1810 break;
1811#endif
1812
1813 default:
1814 break;
1815 }
1816
1817 return NOTIFY_OK;
1818}
1819
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001820static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001821 .notifier_call = hrtimer_cpu_notify,
1822};
1823
1824void __init hrtimers_init(void)
1825{
1826 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1827 (void *)(long)smp_processor_id());
1828 register_cpu_notifier(&hrtimers_nb);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001829#ifdef CONFIG_HIGH_RES_TIMERS
Carlos R. Mafra962cf362008-05-15 11:15:37 -03001830 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001831#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001832}
1833
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001834/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001835 * schedule_hrtimeout_range - sleep until timeout
1836 * @expires: timeout value (ktime_t)
1837 * @delta: slack in expires timeout (ktime_t)
1838 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1839 *
1840 * Make the current task sleep until the given expiry time has
1841 * elapsed. The routine will return immediately unless
1842 * the current task state has been set (see set_current_state()).
1843 *
1844 * The @delta argument gives the kernel the freedom to schedule the
1845 * actual wakeup to a time that is both power and performance friendly.
1846 * The kernel give the normal best effort behavior for "@expires+@delta",
1847 * but may decide to fire the timer earlier, but no earlier than @expires.
1848 *
1849 * You can set the task state as follows -
1850 *
1851 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1852 * pass before the routine returns.
1853 *
1854 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1855 * delivered to the current task.
1856 *
1857 * The current task state is guaranteed to be TASK_RUNNING when this
1858 * routine returns.
1859 *
1860 * Returns 0 when the timer has expired otherwise -EINTR
1861 */
1862int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1863 const enum hrtimer_mode mode)
1864{
1865 struct hrtimer_sleeper t;
1866
1867 /*
1868 * Optimize when a zero timeout value is given. It does not
1869 * matter whether this is an absolute or a relative time.
1870 */
1871 if (expires && !expires->tv64) {
1872 __set_current_state(TASK_RUNNING);
1873 return 0;
1874 }
1875
1876 /*
1877 * A NULL parameter means "inifinte"
1878 */
1879 if (!expires) {
1880 schedule();
1881 __set_current_state(TASK_RUNNING);
1882 return -EINTR;
1883 }
1884
1885 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1886 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1887
1888 hrtimer_init_sleeper(&t, current);
1889
1890 hrtimer_start_expires(&t.timer, mode);
1891 if (!hrtimer_active(&t.timer))
1892 t.task = NULL;
1893
1894 if (likely(t.task))
1895 schedule();
1896
1897 hrtimer_cancel(&t.timer);
1898 destroy_hrtimer_on_stack(&t.timer);
1899
1900 __set_current_state(TASK_RUNNING);
1901
1902 return !t.task ? 0 : -EINTR;
1903}
1904EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1905
1906/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001907 * schedule_hrtimeout - sleep until timeout
1908 * @expires: timeout value (ktime_t)
1909 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1910 *
1911 * Make the current task sleep until the given expiry time has
1912 * elapsed. The routine will return immediately unless
1913 * the current task state has been set (see set_current_state()).
1914 *
1915 * You can set the task state as follows -
1916 *
1917 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1918 * pass before the routine returns.
1919 *
1920 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1921 * delivered to the current task.
1922 *
1923 * The current task state is guaranteed to be TASK_RUNNING when this
1924 * routine returns.
1925 *
1926 * Returns 0 when the timer has expired otherwise -EINTR
1927 */
1928int __sched schedule_hrtimeout(ktime_t *expires,
1929 const enum hrtimer_mode mode)
1930{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001931 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001932}
1933EXPORT_SYMBOL_GPL(schedule_hrtimeout);