blob: 49da79ab8486df682bc7f252674b4a01b810c108 [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
51/**
52 * ktime_get - get the monotonic time in ktime_t format
53 *
54 * returns the time in ktime_t format
55 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080056ktime_t ktime_get(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080057{
58 struct timespec now;
59
60 ktime_get_ts(&now);
61
62 return timespec_to_ktime(now);
63}
Patrick McHardy641b9e02007-03-16 01:18:42 -070064EXPORT_SYMBOL_GPL(ktime_get);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080065
66/**
67 * ktime_get_real - get the real (wall-) time in ktime_t format
68 *
69 * returns the time in ktime_t format
70 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080071ktime_t ktime_get_real(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080072{
73 struct timespec now;
74
75 getnstimeofday(&now);
76
77 return timespec_to_ktime(now);
78}
79
80EXPORT_SYMBOL_GPL(ktime_get_real);
81
82/*
83 * The timer bases:
George Anzinger7978672c2006-02-01 03:05:11 -080084 *
85 * Note: If we want to add new timer bases, we have to skip the two
86 * clock ids captured by the cpu-timers. We do this by holding empty
87 * entries rather than doing math adjustment of the clock ids.
88 * This ensures that we capture erroneous accesses to these clock ids
89 * rather than moving them into the range of valid clock id's.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080090 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080091DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080092{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080093
94 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080095 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080096 {
97 .index = CLOCK_REALTIME,
98 .get_time = &ktime_get_real,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -080099 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800100 },
101 {
102 .index = CLOCK_MONOTONIC,
103 .get_time = &ktime_get,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800104 .resolution = KTIME_LOW_RES,
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800105 },
106 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800107};
108
109/**
110 * ktime_get_ts - get the monotonic clock in timespec format
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800111 * @ts: pointer to timespec variable
112 *
113 * The function calculates the monotonic clock from the realtime
114 * clock and the wall_to_monotonic offset and stores the result
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800115 * in normalized timespec format in the variable pointed to by @ts.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800116 */
117void ktime_get_ts(struct timespec *ts)
118{
119 struct timespec tomono;
120 unsigned long seq;
121
122 do {
123 seq = read_seqbegin(&xtime_lock);
124 getnstimeofday(ts);
125 tomono = wall_to_monotonic;
126
127 } while (read_seqretry(&xtime_lock, seq));
128
129 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
130 ts->tv_nsec + tomono.tv_nsec);
131}
Matt Helsley69778e32006-01-09 20:52:39 -0800132EXPORT_SYMBOL_GPL(ktime_get_ts);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800133
134/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800135 * Get the coarse grained time at the softirq based on xtime and
136 * wall_to_monotonic.
137 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800138static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800139{
140 ktime_t xtim, tomono;
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800141 struct timespec xts, tom;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800142 unsigned long seq;
143
144 do {
145 seq = read_seqbegin(&xtime_lock);
john stultz2c6b47d2007-07-24 17:47:43 -0700146 xts = current_kernel_time();
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800147 tom = wall_to_monotonic;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800148 } while (read_seqretry(&xtime_lock, seq));
149
john stultzf4304ab2007-02-16 01:27:26 -0800150 xtim = timespec_to_ktime(xts);
Thomas Gleixnerad28d942007-03-16 13:38:21 -0800151 tomono = timespec_to_ktime(tom);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800152 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
153 base->clock_base[CLOCK_MONOTONIC].softirq_time =
154 ktime_add(xtim, tomono);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800155}
156
157/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800158 * Functions and macros which are different for UP/SMP systems are kept in a
159 * single place
160 */
161#ifdef CONFIG_SMP
162
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800163/*
164 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
165 * means that all timers which are tied to this base via timer->base are
166 * locked, and the base itself is locked too.
167 *
168 * So __run_timers/migrate_timers can safely modify all timers which could
169 * be found on the lists/queues.
170 *
171 * When the timer's base is locked, and the timer removed from list, it is
172 * possible to set timer->base = NULL and drop the lock: the timer remains
173 * locked.
174 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800175static
176struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
177 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800178{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800179 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800180
181 for (;;) {
182 base = timer->base;
183 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800184 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800185 if (likely(base == timer->base))
186 return base;
187 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800188 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800189 }
190 cpu_relax();
191 }
192}
193
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200194
195/*
196 * Get the preferred target CPU for NOHZ
197 */
198static int hrtimer_get_target(int this_cpu, int pinned)
199{
200#ifdef CONFIG_NO_HZ
201 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu)) {
202 int preferred_cpu = get_nohz_load_balancer();
203
204 if (preferred_cpu >= 0)
205 return preferred_cpu;
206 }
207#endif
208 return this_cpu;
209}
210
211/*
212 * With HIGHRES=y we do not migrate the timer when it is expiring
213 * before the next event on the target cpu because we cannot reprogram
214 * the target cpu hardware and we would cause it to fire late.
215 *
216 * Called with cpu_base->lock of target cpu held.
217 */
218static int
219hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
220{
221#ifdef CONFIG_HIGH_RES_TIMERS
222 ktime_t expires;
223
224 if (!new_base->cpu_base->hres_active)
225 return 0;
226
227 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
228 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
229#else
230 return 0;
231#endif
232}
233
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800234/*
235 * Switch the timer base to the current CPU when possible.
236 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800237static inline struct hrtimer_clock_base *
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530238switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
239 int pinned)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800240{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800241 struct hrtimer_clock_base *new_base;
242 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200243 int this_cpu = smp_processor_id();
244 int cpu = hrtimer_get_target(this_cpu, pinned);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530245
246again:
247 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800248 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800249
250 if (base != new_base) {
251 /*
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200252 * We are trying to move timer to new_base.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800253 * However we can't change timer's base while it is running,
254 * so we keep it on the same CPU. No hassle vs. reprogramming
255 * the event source in the high resolution case. The softirq
256 * code will take care of this when the timer function has
257 * completed. There is no conflict as we hold the lock until
258 * the timer is enqueued.
259 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800260 if (unlikely(hrtimer_callback_running(timer)))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800261 return base;
262
263 /* See the comment in lock_timer_base() */
264 timer->base = NULL;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800265 spin_unlock(&base->cpu_base->lock);
266 spin_lock(&new_base->cpu_base->lock);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530267
Thomas Gleixner6ff70412009-07-10 14:57:05 +0200268 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
269 cpu = this_cpu;
270 spin_unlock(&new_base->cpu_base->lock);
271 spin_lock(&base->cpu_base->lock);
272 timer->base = base;
273 goto again;
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530274 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800275 timer->base = new_base;
276 }
277 return new_base;
278}
279
280#else /* CONFIG_SMP */
281
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800282static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800283lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
284{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800285 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800286
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800287 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800288
289 return base;
290}
291
Arun R Bharadwajeea08f32009-04-16 12:16:41 +0530292# define switch_hrtimer_base(t, b, p) (b)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800293
294#endif /* !CONFIG_SMP */
295
296/*
297 * Functions for the union type storage format of ktime_t which are
298 * too large for inlining:
299 */
300#if BITS_PER_LONG < 64
301# ifndef CONFIG_KTIME_SCALAR
302/**
303 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800304 * @kt: addend
305 * @nsec: the scalar nsec value to add
306 *
307 * Returns the sum of kt and nsec in ktime_t format
308 */
309ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
310{
311 ktime_t tmp;
312
313 if (likely(nsec < NSEC_PER_SEC)) {
314 tmp.tv64 = nsec;
315 } else {
316 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
317
318 tmp = ktime_set((long)nsec, rem);
319 }
320
321 return ktime_add(kt, tmp);
322}
David Howellsb8b8fd22007-04-27 15:31:24 -0700323
324EXPORT_SYMBOL_GPL(ktime_add_ns);
Arnaldo Carvalho de Meloa2723782007-08-19 17:16:05 -0700325
326/**
327 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
328 * @kt: minuend
329 * @nsec: the scalar nsec value to subtract
330 *
331 * Returns the subtraction of @nsec from @kt in ktime_t format
332 */
333ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
334{
335 ktime_t tmp;
336
337 if (likely(nsec < NSEC_PER_SEC)) {
338 tmp.tv64 = nsec;
339 } else {
340 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
341
342 tmp = ktime_set((long)nsec, rem);
343 }
344
345 return ktime_sub(kt, tmp);
346}
347
348EXPORT_SYMBOL_GPL(ktime_sub_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800349# endif /* !CONFIG_KTIME_SCALAR */
350
351/*
352 * Divide a ktime value by a nanosecond value
353 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800354u64 ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800355{
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300356 u64 dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800357 int sft = 0;
358
Carlos R. Mafra900cfa42008-05-22 19:25:11 -0300359 dclc = ktime_to_ns(kt);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800360 /* Make sure the divisor is less than 2^32: */
361 while (div >> 32) {
362 sft++;
363 div >>= 1;
364 }
365 dclc >>= sft;
366 do_div(dclc, (unsigned long) div);
367
Davide Libenzi4d672e72008-02-04 22:27:26 -0800368 return dclc;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800369}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800370#endif /* BITS_PER_LONG >= 64 */
371
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100372/*
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100373 * Add two ktime values and do a safety check for overflow:
374 */
375ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
376{
377 ktime_t res = ktime_add(lhs, rhs);
378
379 /*
380 * We use KTIME_SEC_MAX here, the maximum timeout which we can
381 * return to user space in a timespec:
382 */
383 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
384 res = ktime_set(KTIME_SEC_MAX, 0);
385
386 return res;
387}
388
Artem Bityutskiy8daa21e2009-05-28 16:21:24 +0300389EXPORT_SYMBOL_GPL(ktime_add_safe);
390
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700391#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
392
393static struct debug_obj_descr hrtimer_debug_descr;
394
395/*
396 * fixup_init is called when:
397 * - an active object is initialized
398 */
399static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
400{
401 struct hrtimer *timer = addr;
402
403 switch (state) {
404 case ODEBUG_STATE_ACTIVE:
405 hrtimer_cancel(timer);
406 debug_object_init(timer, &hrtimer_debug_descr);
407 return 1;
408 default:
409 return 0;
410 }
411}
412
413/*
414 * fixup_activate is called when:
415 * - an active object is activated
416 * - an unknown object is activated (might be a statically initialized object)
417 */
418static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
419{
420 switch (state) {
421
422 case ODEBUG_STATE_NOTAVAILABLE:
423 WARN_ON_ONCE(1);
424 return 0;
425
426 case ODEBUG_STATE_ACTIVE:
427 WARN_ON(1);
428
429 default:
430 return 0;
431 }
432}
433
434/*
435 * fixup_free is called when:
436 * - an active object is freed
437 */
438static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
439{
440 struct hrtimer *timer = addr;
441
442 switch (state) {
443 case ODEBUG_STATE_ACTIVE:
444 hrtimer_cancel(timer);
445 debug_object_free(timer, &hrtimer_debug_descr);
446 return 1;
447 default:
448 return 0;
449 }
450}
451
452static struct debug_obj_descr hrtimer_debug_descr = {
453 .name = "hrtimer",
454 .fixup_init = hrtimer_fixup_init,
455 .fixup_activate = hrtimer_fixup_activate,
456 .fixup_free = hrtimer_fixup_free,
457};
458
459static inline void debug_hrtimer_init(struct hrtimer *timer)
460{
461 debug_object_init(timer, &hrtimer_debug_descr);
462}
463
464static inline void debug_hrtimer_activate(struct hrtimer *timer)
465{
466 debug_object_activate(timer, &hrtimer_debug_descr);
467}
468
469static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
470{
471 debug_object_deactivate(timer, &hrtimer_debug_descr);
472}
473
474static inline void debug_hrtimer_free(struct hrtimer *timer)
475{
476 debug_object_free(timer, &hrtimer_debug_descr);
477}
478
479static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
480 enum hrtimer_mode mode);
481
482void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
483 enum hrtimer_mode mode)
484{
485 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
486 __hrtimer_init(timer, clock_id, mode);
487}
488
489void destroy_hrtimer_on_stack(struct hrtimer *timer)
490{
491 debug_object_free(timer, &hrtimer_debug_descr);
492}
493
494#else
495static inline void debug_hrtimer_init(struct hrtimer *timer) { }
496static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
497static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
498#endif
499
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800500/* High resolution timer related functions */
501#ifdef CONFIG_HIGH_RES_TIMERS
502
503/*
504 * High resolution timer enabled ?
505 */
506static int hrtimer_hres_enabled __read_mostly = 1;
507
508/*
509 * Enable / Disable high resolution mode
510 */
511static int __init setup_hrtimer_hres(char *str)
512{
513 if (!strcmp(str, "off"))
514 hrtimer_hres_enabled = 0;
515 else if (!strcmp(str, "on"))
516 hrtimer_hres_enabled = 1;
517 else
518 return 0;
519 return 1;
520}
521
522__setup("highres=", setup_hrtimer_hres);
523
524/*
525 * hrtimer_high_res_enabled - query, if the highres mode is enabled
526 */
527static inline int hrtimer_is_hres_enabled(void)
528{
529 return hrtimer_hres_enabled;
530}
531
532/*
533 * Is the high resolution mode active ?
534 */
535static inline int hrtimer_hres_active(void)
536{
537 return __get_cpu_var(hrtimer_bases).hres_active;
538}
539
540/*
541 * Reprogram the event source with checking both queues for the
542 * next event
543 * Called with interrupts disabled and base->lock held
544 */
545static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
546{
547 int i;
548 struct hrtimer_clock_base *base = cpu_base->clock_base;
549 ktime_t expires;
550
551 cpu_base->expires_next.tv64 = KTIME_MAX;
552
553 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
554 struct hrtimer *timer;
555
556 if (!base->first)
557 continue;
558 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700559 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixnerb0a9b512009-01-25 11:31:36 +0100560 /*
561 * clock_was_set() has changed base->offset so the
562 * result might be negative. Fix it up to prevent a
563 * false positive in clockevents_program_event()
564 */
565 if (expires.tv64 < 0)
566 expires.tv64 = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800567 if (expires.tv64 < cpu_base->expires_next.tv64)
568 cpu_base->expires_next = expires;
569 }
570
571 if (cpu_base->expires_next.tv64 != KTIME_MAX)
572 tick_program_event(cpu_base->expires_next, 1);
573}
574
575/*
576 * Shared reprogramming for clock_realtime and clock_monotonic
577 *
578 * When a timer is enqueued and expires earlier than the already enqueued
579 * timers, we have to check, whether it expires earlier than the timer for
580 * which the clock event device was armed.
581 *
582 * Called with interrupts disabled and base->cpu_base.lock held
583 */
584static int hrtimer_reprogram(struct hrtimer *timer,
585 struct hrtimer_clock_base *base)
586{
587 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
Arjan van de Vencc584b22008-09-01 15:02:30 -0700588 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800589 int res;
590
Arjan van de Vencc584b22008-09-01 15:02:30 -0700591 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
Thomas Gleixner63070a72008-02-14 00:58:36 +0100592
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800593 /*
594 * When the callback is running, we do not reprogram the clock event
595 * device. The timer callback is either running on a different CPU or
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200596 * the callback is executed in the hrtimer_interrupt context. The
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800597 * reprogramming is handled either by the softirq, which called the
598 * callback or at the end of the hrtimer_interrupt.
599 */
600 if (hrtimer_callback_running(timer))
601 return 0;
602
Thomas Gleixner63070a72008-02-14 00:58:36 +0100603 /*
604 * CLOCK_REALTIME timer might be requested with an absolute
605 * expiry time which is less than base->offset. Nothing wrong
606 * about that, just avoid to call into the tick code, which
607 * has now objections against negative expiry values.
608 */
609 if (expires.tv64 < 0)
610 return -ETIME;
611
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800612 if (expires.tv64 >= expires_next->tv64)
613 return 0;
614
615 /*
616 * Clockevents returns -ETIME, when the event was in the past.
617 */
618 res = tick_program_event(expires, 0);
619 if (!IS_ERR_VALUE(res))
620 *expires_next = expires;
621 return res;
622}
623
624
625/*
626 * Retrigger next event is called after clock was set
627 *
628 * Called with interrupts disabled via on_each_cpu()
629 */
630static void retrigger_next_event(void *arg)
631{
632 struct hrtimer_cpu_base *base;
633 struct timespec realtime_offset;
634 unsigned long seq;
635
636 if (!hrtimer_hres_active())
637 return;
638
639 do {
640 seq = read_seqbegin(&xtime_lock);
641 set_normalized_timespec(&realtime_offset,
642 -wall_to_monotonic.tv_sec,
643 -wall_to_monotonic.tv_nsec);
644 } while (read_seqretry(&xtime_lock, seq));
645
646 base = &__get_cpu_var(hrtimer_bases);
647
648 /* Adjust CLOCK_REALTIME offset */
649 spin_lock(&base->lock);
650 base->clock_base[CLOCK_REALTIME].offset =
651 timespec_to_ktime(realtime_offset);
652
653 hrtimer_force_reprogram(base);
654 spin_unlock(&base->lock);
655}
656
657/*
658 * Clock realtime was set
659 *
660 * Change the offset of the realtime clock vs. the monotonic
661 * clock.
662 *
663 * We might have to reprogram the high resolution timer interrupt. On
664 * SMP we call the architecture specific code to retrigger _all_ high
665 * resolution timer interrupts. On UP we just disable interrupts and
666 * call the high resolution interrupt code.
667 */
668void clock_was_set(void)
669{
670 /* Retrigger the CPU local events everywhere */
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200671 on_each_cpu(retrigger_next_event, NULL, 1);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800672}
673
674/*
Ingo Molnar995f0542007-04-07 12:05:00 +0200675 * During resume we might have to reprogram the high resolution timer
676 * interrupt (on the local CPU):
677 */
678void hres_timers_resume(void)
679{
Peter Zijlstra1d4a7f12009-01-18 16:39:29 +0100680 WARN_ONCE(!irqs_disabled(),
681 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
682
Ingo Molnar995f0542007-04-07 12:05:00 +0200683 retrigger_next_event(NULL);
684}
685
686/*
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800687 * Initialize the high resolution related parts of cpu_base
688 */
689static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
690{
691 base->expires_next.tv64 = KTIME_MAX;
692 base->hres_active = 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800693}
694
695/*
696 * Initialize the high resolution related parts of a hrtimer
697 */
698static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
699{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800700}
701
Peter Zijlstraca109492008-11-25 12:43:51 +0100702
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800703/*
704 * When High resolution timers are active, try to reprogram. Note, that in case
705 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
706 * check happens. The timer gets enqueued into the rbtree. The reprogramming
707 * and expiry check is done in the hrtimer_interrupt or in the softirq.
708 */
709static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100710 struct hrtimer_clock_base *base,
711 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800712{
713 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100714 if (wakeup) {
715 spin_unlock(&base->cpu_base->lock);
716 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
717 spin_lock(&base->cpu_base->lock);
718 } else
719 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
720
Peter Zijlstraca109492008-11-25 12:43:51 +0100721 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800722 }
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100723
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800724 return 0;
725}
726
727/*
728 * Switch to high resolution mode
729 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800730static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800731{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700732 int cpu = smp_processor_id();
733 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800734 unsigned long flags;
735
736 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800737 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800738
739 local_irq_save(flags);
740
741 if (tick_init_highres()) {
742 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700743 printk(KERN_WARNING "Could not switch to high resolution "
744 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800745 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800746 }
747 base->hres_active = 1;
748 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
749 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
750
751 tick_setup_sched_timer();
752
753 /* "Retrigger" the interrupt to get things going */
754 retrigger_next_event(NULL);
755 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100756 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800757 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800758 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800759}
760
761#else
762
763static inline int hrtimer_hres_active(void) { return 0; }
764static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800765static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800766static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
767static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100768 struct hrtimer_clock_base *base,
769 int wakeup)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800770{
771 return 0;
772}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800773static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
774static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
775
776#endif /* CONFIG_HIGH_RES_TIMERS */
777
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800778#ifdef CONFIG_TIMER_STATS
779void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
780{
781 if (timer->start_site)
782 return;
783
784 timer->start_site = addr;
785 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
786 timer->start_pid = current->pid;
787}
788#endif
789
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800790/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200791 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800792 */
793static inline
794void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
795{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800796 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800797}
798
799/**
800 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800801 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800802 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800803 * @interval: the interval to forward
804 *
805 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700806 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800807 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800808u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800809{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800810 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800811 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800812
Arjan van de Vencc584b22008-09-01 15:02:30 -0700813 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800814
815 if (delta.tv64 < 0)
816 return 0;
817
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100818 if (interval.tv64 < timer->base->resolution.tv64)
819 interval.tv64 = timer->base->resolution.tv64;
820
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800821 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800822 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823
824 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700825 hrtimer_add_expires_ns(timer, incr * orun);
826 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800827 return orun;
828 /*
829 * This (and the ktime_add() below) is the
830 * correction for exact:
831 */
832 orun++;
833 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700834 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800835
836 return orun;
837}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700838EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800839
840/*
841 * enqueue_hrtimer - internal function to (re)start a timer
842 *
843 * The timer is inserted in expiry order. Insertion into the
844 * red black tree is O(log(n)). Must hold the base lock.
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100845 *
846 * Returns 1 when the new timer is the leftmost timer in the tree.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800847 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100848static int enqueue_hrtimer(struct hrtimer *timer,
849 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800850{
851 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852 struct rb_node *parent = NULL;
853 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700854 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800855
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700856 debug_hrtimer_activate(timer);
857
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800858 /*
859 * Find the right place in the rbtree:
860 */
861 while (*link) {
862 parent = *link;
863 entry = rb_entry(parent, struct hrtimer, node);
864 /*
865 * We dont care about collisions. Nodes with
866 * the same expiry time stay together.
867 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700868 if (hrtimer_get_expires_tv64(timer) <
869 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800870 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700871 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800872 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700873 leftmost = 0;
874 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800875 }
876
877 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100878 * Insert the timer to the rbtree and check whether it
879 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800880 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100881 if (leftmost)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800882 base->first = &timer->node;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800883
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800884 rb_link_node(&timer->node, parent, link);
885 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800886 /*
887 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
888 * state of a possibly running callback.
889 */
890 timer->state |= HRTIMER_STATE_ENQUEUED;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100891
892 return leftmost;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100893}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800894
895/*
896 * __remove_hrtimer - internal function to remove a timer
897 *
898 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800899 *
900 * High resolution timer mode reprograms the clock event device when the
901 * timer is the one which expires next. The caller can disable this by setting
902 * reprogram to zero. This is useful, when the context does a reprogramming
903 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800904 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800905static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800906 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800907 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800908{
Peter Zijlstraca109492008-11-25 12:43:51 +0100909 if (timer->state & HRTIMER_STATE_ENQUEUED) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800910 /*
911 * Remove the timer from the rbtree and replace the
912 * first entry pointer if necessary.
913 */
914 if (base->first == &timer->node) {
915 base->first = rb_next(&timer->node);
916 /* Reprogram the clock event device. if enabled */
917 if (reprogram && hrtimer_hres_active())
918 hrtimer_force_reprogram(base->cpu_base);
919 }
920 rb_erase(&timer->node, &base->active);
921 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800922 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800923}
924
925/*
926 * remove hrtimer, called with base lock held
927 */
928static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800929remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800930{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800931 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800932 int reprogram;
933
934 /*
935 * Remove the timer and force reprogramming when high
936 * resolution mode is active and the timer is on the current
937 * CPU. If we remove a timer on another CPU, reprogramming is
938 * skipped. The interrupt event on this CPU is fired and
939 * reprogramming happens in the interrupt handler. This is a
940 * rare case and less expensive than a smp call.
941 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700942 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800943 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800944 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
945 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
946 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800947 return 1;
948 }
949 return 0;
950}
951
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100952int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
953 unsigned long delta_ns, const enum hrtimer_mode mode,
954 int wakeup)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800955{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800956 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800957 unsigned long flags;
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100958 int ret, leftmost;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800959
960 base = lock_hrtimer_base(timer, &flags);
961
962 /* Remove an active timer from the queue: */
963 ret = remove_hrtimer(timer, base);
964
965 /* Switch the timer base, if necessary: */
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530966 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800967
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530968 if (mode & HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100969 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800970 /*
971 * CONFIG_TIME_LOW_RES is a temporary way for architectures
972 * to signal that they simply return xtime in
973 * do_gettimeoffset(). In this case we want to round up by
974 * resolution when starting a relative timer, to avoid short
975 * timeouts. This will go away with the GTOD framework.
976 */
977#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100978 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800979#endif
980 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700981
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700982 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800983
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800984 timer_stats_hrtimer_set_start_info(timer);
985
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100986 leftmost = enqueue_hrtimer(timer, new_base);
987
Ingo Molnar935c6312007-03-28 13:17:18 +0200988 /*
989 * Only allow reprogramming if the new base is on this CPU.
990 * (it might still be on another CPU if the timer was pending)
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100991 *
992 * XXX send_remote_softirq() ?
Ingo Molnar935c6312007-03-28 13:17:18 +0200993 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +0100994 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100995 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800996
997 unlock_hrtimer_base(timer, &flags);
998
999 return ret;
1000}
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001001
1002/**
1003 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1004 * @timer: the timer to be added
1005 * @tim: expiry time
1006 * @delta_ns: "slack" range for the timer
1007 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1008 *
1009 * Returns:
1010 * 0 on success
1011 * 1 when the timer was active
1012 */
1013int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1014 unsigned long delta_ns, const enum hrtimer_mode mode)
1015{
1016 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1017}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001018EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1019
1020/**
Thomas Gleixnere1dd7bc2008-10-20 13:33:36 +02001021 * hrtimer_start - (re)start an hrtimer on the current CPU
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001022 * @timer: the timer to be added
1023 * @tim: expiry time
1024 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1025 *
1026 * Returns:
1027 * 0 on success
1028 * 1 when the timer was active
1029 */
1030int
1031hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1032{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001033 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001034}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001035EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001036
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001037
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001038/**
1039 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001040 * @timer: hrtimer to stop
1041 *
1042 * Returns:
1043 * 0 when the timer was not active
1044 * 1 when the timer was active
1045 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001046 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001047 */
1048int hrtimer_try_to_cancel(struct hrtimer *timer)
1049{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001050 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001051 unsigned long flags;
1052 int ret = -1;
1053
1054 base = lock_hrtimer_base(timer, &flags);
1055
Thomas Gleixner303e9672007-02-16 01:27:51 -08001056 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001057 ret = remove_hrtimer(timer, base);
1058
1059 unlock_hrtimer_base(timer, &flags);
1060
1061 return ret;
1062
1063}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001064EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001065
1066/**
1067 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001068 * @timer: the timer to be cancelled
1069 *
1070 * Returns:
1071 * 0 when the timer was not active
1072 * 1 when the timer was active
1073 */
1074int hrtimer_cancel(struct hrtimer *timer)
1075{
1076 for (;;) {
1077 int ret = hrtimer_try_to_cancel(timer);
1078
1079 if (ret >= 0)
1080 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001081 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001082 }
1083}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001084EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001085
1086/**
1087 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001088 * @timer: the timer to read
1089 */
1090ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1091{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001092 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001093 unsigned long flags;
1094 ktime_t rem;
1095
1096 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001097 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001098 unlock_hrtimer_base(timer, &flags);
1099
1100 return rem;
1101}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001102EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001103
Russell Kingee9c5782008-04-20 13:59:33 +01001104#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001105/**
1106 * hrtimer_get_next_event - get the time until next expiry event
1107 *
1108 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1109 * is pending.
1110 */
1111ktime_t hrtimer_get_next_event(void)
1112{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001113 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1114 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001115 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1116 unsigned long flags;
1117 int i;
1118
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001119 spin_lock_irqsave(&cpu_base->lock, flags);
1120
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001121 if (!hrtimer_hres_active()) {
1122 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1123 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001124
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001125 if (!base->first)
1126 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001127
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001128 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001129 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001130 delta = ktime_sub(delta, base->get_time());
1131 if (delta.tv64 < mindelta.tv64)
1132 mindelta.tv64 = delta.tv64;
1133 }
Tony Lindgren69239742006-03-06 15:42:45 -08001134 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001135
1136 spin_unlock_irqrestore(&cpu_base->lock, flags);
1137
Tony Lindgren69239742006-03-06 15:42:45 -08001138 if (mindelta.tv64 < 0)
1139 mindelta.tv64 = 0;
1140 return mindelta;
1141}
1142#endif
1143
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001144static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1145 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001146{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001147 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001148
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001149 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001150
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001151 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001152
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001153 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001154 clock_id = CLOCK_MONOTONIC;
1155
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001156 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001157 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001158 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001159
1160#ifdef CONFIG_TIMER_STATS
1161 timer->start_site = NULL;
1162 timer->start_pid = -1;
1163 memset(timer->start_comm, 0, TASK_COMM_LEN);
1164#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001165}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001166
1167/**
1168 * hrtimer_init - initialize a timer to the given clock
1169 * @timer: the timer to be initialized
1170 * @clock_id: the clock to be used
1171 * @mode: timer mode abs/rel
1172 */
1173void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1174 enum hrtimer_mode mode)
1175{
1176 debug_hrtimer_init(timer);
1177 __hrtimer_init(timer, clock_id, mode);
1178}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001179EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001180
1181/**
1182 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001183 * @which_clock: which clock to query
1184 * @tp: pointer to timespec variable to store the resolution
1185 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001186 * Store the resolution of the clock selected by @which_clock in the
1187 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001188 */
1189int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1190{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001191 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001192
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001193 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1194 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001195
1196 return 0;
1197}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001198EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001199
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001200static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001201{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001202 struct hrtimer_clock_base *base = timer->base;
1203 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1204 enum hrtimer_restart (*fn)(struct hrtimer *);
1205 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001206
Peter Zijlstraca109492008-11-25 12:43:51 +01001207 WARN_ON(!irqs_disabled());
1208
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001209 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001210 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1211 timer_stats_account_hrtimer(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001212 fn = timer->function;
Peter Zijlstraca109492008-11-25 12:43:51 +01001213
1214 /*
1215 * Because we run timers from hardirq context, there is no chance
1216 * they get migrated to another cpu, therefore its safe to unlock
1217 * the timer base.
1218 */
1219 spin_unlock(&cpu_base->lock);
1220 restart = fn(timer);
1221 spin_lock(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001222
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001223 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001224 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1225 * we do not reprogramm the event hardware. Happens either in
1226 * hrtimer_start_range_ns() or in hrtimer_interrupt()
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001227 */
1228 if (restart != HRTIMER_NORESTART) {
1229 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001230 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001231 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001232 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001233}
1234
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001235#ifdef CONFIG_HIGH_RES_TIMERS
1236
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001237static int force_clock_reprogram;
1238
1239/*
1240 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1241 * is hanging, which could happen with something that slows the interrupt
1242 * such as the tracing. Then we force the clock reprogramming for each future
1243 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1244 * threshold that we will overwrite.
1245 * The next tick event will be scheduled to 3 times we currently spend on
1246 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1247 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1248 * let it running without serious starvation.
1249 */
1250
1251static inline void
1252hrtimer_interrupt_hanging(struct clock_event_device *dev,
1253 ktime_t try_time)
1254{
1255 force_clock_reprogram = 1;
1256 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1257 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1258 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1259}
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001260/*
1261 * High resolution timer interrupt
1262 * Called with interrupts disabled
1263 */
1264void hrtimer_interrupt(struct clock_event_device *dev)
1265{
1266 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1267 struct hrtimer_clock_base *base;
1268 ktime_t expires_next, now;
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001269 int nr_retries = 0;
Peter Zijlstraca109492008-11-25 12:43:51 +01001270 int i;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001271
1272 BUG_ON(!cpu_base->hres_active);
1273 cpu_base->nr_events++;
1274 dev->next_event.tv64 = KTIME_MAX;
1275
1276 retry:
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001277 /* 5 retries is enough to notice a hang */
1278 if (!(++nr_retries % 5))
1279 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1280
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001281 now = ktime_get();
1282
1283 expires_next.tv64 = KTIME_MAX;
1284
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001285 spin_lock(&cpu_base->lock);
1286 /*
1287 * We set expires_next to KTIME_MAX here with cpu_base->lock
1288 * held to prevent that a timer is enqueued in our queue via
1289 * the migration code. This does not affect enqueueing of
1290 * timers which run their callback and need to be requeued on
1291 * this CPU.
1292 */
1293 cpu_base->expires_next.tv64 = KTIME_MAX;
1294
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001295 base = cpu_base->clock_base;
1296
1297 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1298 ktime_t basenow;
1299 struct rb_node *node;
1300
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001301 basenow = ktime_add(now, base->offset);
1302
1303 while ((node = base->first)) {
1304 struct hrtimer *timer;
1305
1306 timer = rb_entry(node, struct hrtimer, node);
1307
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001308 /*
1309 * The immediate goal for using the softexpires is
1310 * minimizing wakeups, not running timers at the
1311 * earliest interrupt after their soft expiration.
1312 * This allows us to avoid using a Priority Search
1313 * Tree, which can answer a stabbing querry for
1314 * overlapping intervals and instead use the simple
1315 * BST we already have.
1316 * We don't add extra wakeups by delaying timers that
1317 * are right-of a not yet expired timer, because that
1318 * timer will have to trigger a wakeup anyway.
1319 */
1320
1321 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001322 ktime_t expires;
1323
Arjan van de Vencc584b22008-09-01 15:02:30 -07001324 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001325 base->offset);
1326 if (expires.tv64 < expires_next.tv64)
1327 expires_next = expires;
1328 break;
1329 }
1330
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001331 __run_hrtimer(timer);
1332 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001333 base++;
1334 }
1335
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001336 /*
1337 * Store the new expiry value so the migration code can verify
1338 * against it.
1339 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001340 cpu_base->expires_next = expires_next;
Thomas Gleixner6ff70412009-07-10 14:57:05 +02001341 spin_unlock(&cpu_base->lock);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001342
1343 /* Reprogramming necessary ? */
1344 if (expires_next.tv64 != KTIME_MAX) {
Frederic Weisbecker7f223912008-12-22 02:24:48 +01001345 if (tick_program_event(expires_next, force_clock_reprogram))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001346 goto retry;
1347 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001348}
1349
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001350/*
1351 * local version of hrtimer_peek_ahead_timers() called with interrupts
1352 * disabled.
1353 */
1354static void __hrtimer_peek_ahead_timers(void)
1355{
1356 struct tick_device *td;
1357
1358 if (!hrtimer_hres_active())
1359 return;
1360
1361 td = &__get_cpu_var(tick_cpu_device);
1362 if (td && td->evtdev)
1363 hrtimer_interrupt(td->evtdev);
1364}
1365
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001366/**
1367 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1368 *
1369 * hrtimer_peek_ahead_timers will peek at the timer queue of
1370 * the current cpu and check if there are any timers for which
1371 * the soft expires time has passed. If any such timers exist,
1372 * they are run immediately and then removed from the timer queue.
1373 *
1374 */
1375void hrtimer_peek_ahead_timers(void)
1376{
Thomas Gleixner643bdf62008-10-20 13:38:11 +02001377 unsigned long flags;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001378
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001379 local_irq_save(flags);
Thomas Gleixner8bdec952009-01-05 11:28:19 +01001380 __hrtimer_peek_ahead_timers();
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001381 local_irq_restore(flags);
1382}
1383
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001384static void run_hrtimer_softirq(struct softirq_action *h)
1385{
1386 hrtimer_peek_ahead_timers();
1387}
1388
Ingo Molnar82c5b7b2009-01-05 14:11:10 +01001389#else /* CONFIG_HIGH_RES_TIMERS */
1390
1391static inline void __hrtimer_peek_ahead_timers(void) { }
1392
1393#endif /* !CONFIG_HIGH_RES_TIMERS */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001394
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001395/*
1396 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001397 *
1398 * For HRT its the fall back code to run the softirq in the timer
1399 * softirq context in case the hrtimer initialization failed or has
1400 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001401 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001402void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001403{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001404 if (hrtimer_hres_active())
1405 return;
1406
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001407 /*
1408 * This _is_ ugly: We have to check in the softirq context,
1409 * whether we can switch to highres and / or nohz mode. The
1410 * clocksource switch happens in the timer interrupt with
1411 * xtime_lock held. Notification from there only sets the
1412 * check bit in the tick_oneshot code, otherwise we might
1413 * deadlock vs. xtime_lock.
1414 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001415 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001416 hrtimer_switch_to_hres();
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001417}
1418
1419/*
1420 * Called from hardirq context every jiffy
1421 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001422void hrtimer_run_queues(void)
1423{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001424 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001425 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001426 struct hrtimer_clock_base *base;
1427 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001428
1429 if (hrtimer_hres_active())
1430 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001431
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001432 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1433 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001434
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001435 if (!base->first)
1436 continue;
1437
Mark McLoughlind7cfb602008-09-19 13:13:44 +01001438 if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001439 hrtimer_get_softirq_time(cpu_base);
1440 gettime = 0;
1441 }
1442
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001443 spin_lock(&cpu_base->lock);
1444
1445 while ((node = base->first)) {
1446 struct hrtimer *timer;
1447
1448 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001449 if (base->softirq_time.tv64 <=
1450 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001451 break;
1452
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001453 __run_hrtimer(timer);
1454 }
1455 spin_unlock(&cpu_base->lock);
1456 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001457}
1458
1459/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001460 * Sleep related functions:
1461 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001462static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001463{
1464 struct hrtimer_sleeper *t =
1465 container_of(timer, struct hrtimer_sleeper, timer);
1466 struct task_struct *task = t->task;
1467
1468 t->task = NULL;
1469 if (task)
1470 wake_up_process(task);
1471
1472 return HRTIMER_NORESTART;
1473}
1474
Ingo Molnar36c8b582006-07-03 00:25:41 -07001475void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001476{
1477 sl->timer.function = hrtimer_wakeup;
1478 sl->task = task;
1479}
1480
Thomas Gleixner669d7862006-03-31 02:31:19 -08001481static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001482{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001483 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001484
Roman Zippel432569b2006-03-26 01:38:08 -08001485 do {
1486 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001487 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001488 if (!hrtimer_active(&t->timer))
1489 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001490
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001491 if (likely(t->task))
1492 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001493
Thomas Gleixner669d7862006-03-31 02:31:19 -08001494 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001495 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001496
Thomas Gleixner669d7862006-03-31 02:31:19 -08001497 } while (t->task && !signal_pending(current));
1498
Peter Zijlstra3588a082008-02-01 17:45:13 +01001499 __set_current_state(TASK_RUNNING);
1500
Thomas Gleixner669d7862006-03-31 02:31:19 -08001501 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001502}
1503
Oleg Nesterov080344b2008-02-01 17:29:05 +03001504static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1505{
1506 struct timespec rmt;
1507 ktime_t rem;
1508
Arjan van de Vencc584b22008-09-01 15:02:30 -07001509 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001510 if (rem.tv64 <= 0)
1511 return 0;
1512 rmt = ktime_to_timespec(rem);
1513
1514 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1515 return -EFAULT;
1516
1517 return 1;
1518}
1519
Toyo Abe1711ef32006-09-29 02:00:28 -07001520long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001521{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001522 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001523 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001524 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001525
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001526 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1527 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001528 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001529
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001530 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001531 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001532
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001533 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001534 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001535 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001536 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001537 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001538 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001539
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001540 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001541 ret = -ERESTART_RESTARTBLOCK;
1542out:
1543 destroy_hrtimer_on_stack(&t.timer);
1544 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001545}
1546
Oleg Nesterov080344b2008-02-01 17:29:05 +03001547long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001548 const enum hrtimer_mode mode, const clockid_t clockid)
1549{
1550 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001551 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001552 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001553 unsigned long slack;
1554
1555 slack = current->timer_slack_ns;
1556 if (rt_task(current))
1557 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001558
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001559 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001560 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001561 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001562 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001563
George Anzinger7978672c2006-02-01 03:05:11 -08001564 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001565 if (mode == HRTIMER_MODE_ABS) {
1566 ret = -ERESTARTNOHAND;
1567 goto out;
1568 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001569
Roman Zippel432569b2006-03-26 01:38:08 -08001570 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001571 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001572 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001573 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001574 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001575
1576 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001577 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001578 restart->nanosleep.index = t.timer.base->index;
1579 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001580 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001581
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001582 ret = -ERESTART_RESTARTBLOCK;
1583out:
1584 destroy_hrtimer_on_stack(&t.timer);
1585 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001586}
1587
Heiko Carstens58fd3aa2009-01-14 14:14:03 +01001588SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1589 struct timespec __user *, rmtp)
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001590{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001591 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001592
1593 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1594 return -EFAULT;
1595
1596 if (!timespec_valid(&tu))
1597 return -EINVAL;
1598
Oleg Nesterov080344b2008-02-01 17:29:05 +03001599 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001600}
1601
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001602/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001603 * Functions related to boot-time initialization:
1604 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001605static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001606{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001607 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001608 int i;
1609
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001610 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001611
1612 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1613 cpu_base->clock_base[i].cpu_base = cpu_base;
1614
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001615 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001616}
1617
1618#ifdef CONFIG_HOTPLUG_CPU
1619
Peter Zijlstraca109492008-11-25 12:43:51 +01001620static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
Peter Zijlstra37810652008-12-04 11:17:10 +01001621 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001622{
1623 struct hrtimer *timer;
1624 struct rb_node *node;
1625
1626 while ((node = rb_first(&old_base->active))) {
1627 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001628 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001629 debug_hrtimer_deactivate(timer);
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001630
1631 /*
1632 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1633 * timer could be seen as !active and just vanish away
1634 * under us on another CPU
1635 */
1636 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001637 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001638 /*
Thomas Gleixnere3f1d882009-01-05 11:28:23 +01001639 * Enqueue the timers on the new cpu. This does not
1640 * reprogram the event device in case the timer
1641 * expires before the earliest on this CPU, but we run
1642 * hrtimer_interrupt after we migrated everything to
1643 * sort out already expired timers and reprogram the
1644 * event device.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001645 */
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001646 enqueue_hrtimer(timer, new_base);
Thomas Gleixner41e10222008-09-29 14:09:39 +02001647
Thomas Gleixnerb00c1a92008-09-29 15:44:46 +02001648 /* Clear the migration state bit */
1649 timer->state &= ~HRTIMER_STATE_MIGRATE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001650 }
1651}
1652
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001653static void migrate_hrtimers(int scpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001654{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001655 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001656 int i;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001657
Peter Zijlstra37810652008-12-04 11:17:10 +01001658 BUG_ON(cpu_online(scpu));
Peter Zijlstra37810652008-12-04 11:17:10 +01001659 tick_cancel_sched_timer(scpu);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001660
1661 local_irq_disable();
1662 old_base = &per_cpu(hrtimer_bases, scpu);
1663 new_base = &__get_cpu_var(hrtimer_bases);
Oleg Nesterovd82f0b02008-08-20 16:46:04 -07001664 /*
1665 * The caller is globally serialized and nobody else
1666 * takes two locks at once, deadlock is not possible.
1667 */
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001668 spin_lock(&new_base->lock);
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001669 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001670
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001671 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Peter Zijlstraca109492008-11-25 12:43:51 +01001672 migrate_hrtimer_list(&old_base->clock_base[i],
Peter Zijlstra37810652008-12-04 11:17:10 +01001673 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001674 }
1675
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001676 spin_unlock(&old_base->lock);
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001677 spin_unlock(&new_base->lock);
Peter Zijlstra37810652008-12-04 11:17:10 +01001678
Thomas Gleixner731a55b2009-01-05 11:28:21 +01001679 /* Check, if we got expired work to do */
1680 __hrtimer_peek_ahead_timers();
1681 local_irq_enable();
Peter Zijlstra37810652008-12-04 11:17:10 +01001682}
1683
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001684#endif /* CONFIG_HOTPLUG_CPU */
1685
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001686static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001687 unsigned long action, void *hcpu)
1688{
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001689 int scpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001690
1691 switch (action) {
1692
1693 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001694 case CPU_UP_PREPARE_FROZEN:
Peter Zijlstra37810652008-12-04 11:17:10 +01001695 init_hrtimers_cpu(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001696 break;
1697
1698#ifdef CONFIG_HOTPLUG_CPU
Sebastien Dugue94df7de2008-12-01 14:09:07 +01001699 case CPU_DYING:
1700 case CPU_DYING_FROZEN:
1701 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1702 break;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001703 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001704 case CPU_DEAD_FROZEN:
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001705 {
Peter Zijlstra37810652008-12-04 11:17:10 +01001706 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
Thomas Gleixnerd5fd43c2009-01-05 11:28:20 +01001707 migrate_hrtimers(scpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001708 break;
Ingo Molnarb2e3c0a2008-12-19 00:48:27 +01001709 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001710#endif
1711
1712 default:
1713 break;
1714 }
1715
1716 return NOTIFY_OK;
1717}
1718
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001719static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001720 .notifier_call = hrtimer_cpu_notify,
1721};
1722
1723void __init hrtimers_init(void)
1724{
1725 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1726 (void *)(long)smp_processor_id());
1727 register_cpu_notifier(&hrtimers_nb);
Peter Zijlstraa6037b62009-01-05 11:28:22 +01001728#ifdef CONFIG_HIGH_RES_TIMERS
1729 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1730#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001731}
1732
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001733/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001734 * schedule_hrtimeout_range - sleep until timeout
1735 * @expires: timeout value (ktime_t)
1736 * @delta: slack in expires timeout (ktime_t)
1737 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1738 *
1739 * Make the current task sleep until the given expiry time has
1740 * elapsed. The routine will return immediately unless
1741 * the current task state has been set (see set_current_state()).
1742 *
1743 * The @delta argument gives the kernel the freedom to schedule the
1744 * actual wakeup to a time that is both power and performance friendly.
1745 * The kernel give the normal best effort behavior for "@expires+@delta",
1746 * but may decide to fire the timer earlier, but no earlier than @expires.
1747 *
1748 * You can set the task state as follows -
1749 *
1750 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1751 * pass before the routine returns.
1752 *
1753 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1754 * delivered to the current task.
1755 *
1756 * The current task state is guaranteed to be TASK_RUNNING when this
1757 * routine returns.
1758 *
1759 * Returns 0 when the timer has expired otherwise -EINTR
1760 */
1761int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1762 const enum hrtimer_mode mode)
1763{
1764 struct hrtimer_sleeper t;
1765
1766 /*
1767 * Optimize when a zero timeout value is given. It does not
1768 * matter whether this is an absolute or a relative time.
1769 */
1770 if (expires && !expires->tv64) {
1771 __set_current_state(TASK_RUNNING);
1772 return 0;
1773 }
1774
1775 /*
1776 * A NULL parameter means "inifinte"
1777 */
1778 if (!expires) {
1779 schedule();
1780 __set_current_state(TASK_RUNNING);
1781 return -EINTR;
1782 }
1783
1784 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1785 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1786
1787 hrtimer_init_sleeper(&t, current);
1788
1789 hrtimer_start_expires(&t.timer, mode);
1790 if (!hrtimer_active(&t.timer))
1791 t.task = NULL;
1792
1793 if (likely(t.task))
1794 schedule();
1795
1796 hrtimer_cancel(&t.timer);
1797 destroy_hrtimer_on_stack(&t.timer);
1798
1799 __set_current_state(TASK_RUNNING);
1800
1801 return !t.task ? 0 : -EINTR;
1802}
1803EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1804
1805/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001806 * schedule_hrtimeout - sleep until timeout
1807 * @expires: timeout value (ktime_t)
1808 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1809 *
1810 * Make the current task sleep until the given expiry time has
1811 * elapsed. The routine will return immediately unless
1812 * the current task state has been set (see set_current_state()).
1813 *
1814 * You can set the task state as follows -
1815 *
1816 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1817 * pass before the routine returns.
1818 *
1819 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1820 * delivered to the current task.
1821 *
1822 * The current task state is guaranteed to be TASK_RUNNING when this
1823 * routine returns.
1824 *
1825 * Returns 0 when the timer has expired otherwise -EINTR
1826 */
1827int __sched schedule_hrtimeout(ktime_t *expires,
1828 const enum hrtimer_mode mode)
1829{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001830 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001831}
1832EXPORT_SYMBOL_GPL(schedule_hrtimeout);