blob: 2bd230be1cb5ae3a778546d2ed0e651e153ba46e [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 Anzinger7978672c2006-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;
675 case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:
676 /*
677 * This is solely for the sched tick emulation with
678 * dynamic tick support to ensure that we do not
679 * restart the tick right on the edge and end up with
680 * the tick timer in the softirq ! The calling site
681 * takes care of this.
682 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700683 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800684 return 1;
685 case HRTIMER_CB_IRQSAFE:
686 case HRTIMER_CB_SOFTIRQ:
687 /*
688 * Move everything else into the softirq pending list !
689 */
690 list_add_tail(&timer->cb_entry,
691 &base->cpu_base->cb_pending);
692 timer->state = HRTIMER_STATE_PENDING;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800693 return 1;
694 default:
695 BUG();
696 }
697 }
698 return 0;
699}
700
701/*
702 * Switch to high resolution mode
703 */
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800704static int hrtimer_switch_to_hres(void)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800705{
Ingo Molnar820de5c2007-07-21 04:37:36 -0700706 int cpu = smp_processor_id();
707 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800708 unsigned long flags;
709
710 if (base->hres_active)
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800711 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800712
713 local_irq_save(flags);
714
715 if (tick_init_highres()) {
716 local_irq_restore(flags);
Ingo Molnar820de5c2007-07-21 04:37:36 -0700717 printk(KERN_WARNING "Could not switch to high resolution "
718 "mode on CPU %d\n", cpu);
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800719 return 0;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800720 }
721 base->hres_active = 1;
722 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
723 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
724
725 tick_setup_sched_timer();
726
727 /* "Retrigger" the interrupt to get things going */
728 retrigger_next_event(NULL);
729 local_irq_restore(flags);
Michael Ellermanedfed662007-10-29 16:35:29 +1100730 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800731 smp_processor_id());
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800732 return 1;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800733}
734
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200735static inline void hrtimer_raise_softirq(void)
736{
737 raise_softirq(HRTIMER_SOFTIRQ);
738}
739
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800740#else
741
742static inline int hrtimer_hres_active(void) { return 0; }
743static inline int hrtimer_is_hres_enabled(void) { return 0; }
Thomas Gleixnerf8953852007-03-06 01:42:08 -0800744static inline int hrtimer_switch_to_hres(void) { return 0; }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800745static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
746static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
747 struct hrtimer_clock_base *base)
748{
749 return 0;
750}
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800751static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
752static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
Peter Zijlstrad3d74452008-01-25 21:08:31 +0100753static inline int hrtimer_reprogram(struct hrtimer *timer,
754 struct hrtimer_clock_base *base)
755{
756 return 0;
757}
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200758static inline void hrtimer_raise_softirq(void) { }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800759
760#endif /* CONFIG_HIGH_RES_TIMERS */
761
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800762#ifdef CONFIG_TIMER_STATS
763void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
764{
765 if (timer->start_site)
766 return;
767
768 timer->start_site = addr;
769 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
770 timer->start_pid = current->pid;
771}
772#endif
773
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800774/*
Uwe Kleine-König6506f2a2007-10-20 01:56:53 +0200775 * Counterpart to lock_hrtimer_base above:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800776 */
777static inline
778void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
779{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800780 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800781}
782
783/**
784 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800785 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800786 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800787 * @interval: the interval to forward
788 *
789 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700790 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800791 */
Davide Libenzi4d672e72008-02-04 22:27:26 -0800792u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800793{
Davide Libenzi4d672e72008-02-04 22:27:26 -0800794 u64 orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800795 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800796
Arjan van de Vencc584b22008-09-01 15:02:30 -0700797 delta = ktime_sub(now, hrtimer_get_expires(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800798
799 if (delta.tv64 < 0)
800 return 0;
801
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100802 if (interval.tv64 < timer->base->resolution.tv64)
803 interval.tv64 = timer->base->resolution.tv64;
804
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800805 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800806 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800807
808 orun = ktime_divns(delta, incr);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700809 hrtimer_add_expires_ns(timer, incr * orun);
810 if (hrtimer_get_expires_tv64(timer) > now.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800811 return orun;
812 /*
813 * This (and the ktime_add() below) is the
814 * correction for exact:
815 */
816 orun++;
817 }
Arjan van de Vencc584b22008-09-01 15:02:30 -0700818 hrtimer_add_expires(timer, interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800819
820 return orun;
821}
Stas Sergeev6bdb6b62007-05-08 00:31:58 -0700822EXPORT_SYMBOL_GPL(hrtimer_forward);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823
824/*
825 * enqueue_hrtimer - internal function to (re)start a timer
826 *
827 * The timer is inserted in expiry order. Insertion into the
828 * red black tree is O(log(n)). Must hold the base lock.
829 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800830static void enqueue_hrtimer(struct hrtimer *timer,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800831 struct hrtimer_clock_base *base, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832{
833 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800834 struct rb_node *parent = NULL;
835 struct hrtimer *entry;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700836 int leftmost = 1;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800837
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700838 debug_hrtimer_activate(timer);
839
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800840 /*
841 * Find the right place in the rbtree:
842 */
843 while (*link) {
844 parent = *link;
845 entry = rb_entry(parent, struct hrtimer, node);
846 /*
847 * We dont care about collisions. Nodes with
848 * the same expiry time stay together.
849 */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700850 if (hrtimer_get_expires_tv64(timer) <
851 hrtimer_get_expires_tv64(entry)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800852 link = &(*link)->rb_left;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700853 } else {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800854 link = &(*link)->rb_right;
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700855 leftmost = 0;
856 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800857 }
858
859 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100860 * Insert the timer to the rbtree and check whether it
861 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800862 */
Ingo Molnar99bc2fc2007-07-21 04:37:36 -0700863 if (leftmost) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800864 /*
865 * Reprogram the clock event device. When the timer is already
866 * expired hrtimer_enqueue_reprogram has either called the
867 * callback or added it to the pending list and raised the
868 * softirq.
869 *
870 * This is a NOP for !HIGHRES
871 */
872 if (reprogram && hrtimer_enqueue_reprogram(timer, base))
873 return;
874
875 base->first = &timer->node;
876 }
877
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800878 rb_link_node(&timer->node, parent, link);
879 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800880 /*
881 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
882 * state of a possibly running callback.
883 */
884 timer->state |= HRTIMER_STATE_ENQUEUED;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100885}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800886
887/*
888 * __remove_hrtimer - internal function to remove a timer
889 *
890 * Caller must hold the base lock.
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800891 *
892 * High resolution timer mode reprograms the clock event device when the
893 * timer is the one which expires next. The caller can disable this by setting
894 * reprogram to zero. This is useful, when the context does a reprogramming
895 * anyway (e.g. timer interrupt)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800896 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800897static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800898 struct hrtimer_clock_base *base,
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800899 unsigned long newstate, int reprogram)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800900{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800901 /* High res. callback list. NOP for !HIGHRES */
902 if (hrtimer_cb_pending(timer))
903 hrtimer_remove_cb_pending(timer);
904 else {
905 /*
906 * Remove the timer from the rbtree and replace the
907 * first entry pointer if necessary.
908 */
909 if (base->first == &timer->node) {
910 base->first = rb_next(&timer->node);
911 /* Reprogram the clock event device. if enabled */
912 if (reprogram && hrtimer_hres_active())
913 hrtimer_force_reprogram(base->cpu_base);
914 }
915 rb_erase(&timer->node, &base->active);
916 }
Thomas Gleixner303e9672007-02-16 01:27:51 -0800917 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800918}
919
920/*
921 * remove hrtimer, called with base lock held
922 */
923static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800924remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800925{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800926 if (hrtimer_is_queued(timer)) {
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800927 int reprogram;
928
929 /*
930 * Remove the timer and force reprogramming when high
931 * resolution mode is active and the timer is on the current
932 * CPU. If we remove a timer on another CPU, reprogramming is
933 * skipped. The interrupt event on this CPU is fired and
934 * reprogramming happens in the interrupt handler. This is a
935 * rare case and less expensive than a smp call.
936 */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700937 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800938 timer_stats_hrtimer_clear_start_info(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -0800939 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
940 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
941 reprogram);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800942 return 1;
943 }
944 return 0;
945}
946
947/**
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700948 * hrtimer_start_range_ns - (re)start an relative timer on the current CPU
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800949 * @timer: the timer to be added
950 * @tim: expiry time
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700951 * @delta_ns: "slack" range for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800952 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
953 *
954 * Returns:
955 * 0 on success
956 * 1 when the timer was active
957 */
958int
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700959hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
960 const enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800961{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800962 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800963 unsigned long flags;
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200964 int ret, raise;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800965
966 base = lock_hrtimer_base(timer, &flags);
967
968 /* Remove an active timer from the queue: */
969 ret = remove_hrtimer(timer, base);
970
971 /* Switch the timer base, if necessary: */
972 new_base = switch_hrtimer_base(timer, base);
973
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800974 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100975 tim = ktime_add_safe(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800976 /*
977 * CONFIG_TIME_LOW_RES is a temporary way for architectures
978 * to signal that they simply return xtime in
979 * do_gettimeoffset(). In this case we want to round up by
980 * resolution when starting a relative timer, to avoid short
981 * timeouts. This will go away with the GTOD framework.
982 */
983#ifdef CONFIG_TIME_LOW_RES
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100984 tim = ktime_add_safe(tim, base->resolution);
Ingo Molnar06027bd2006-02-14 13:53:15 -0800985#endif
986 }
Thomas Gleixner237fc6e2008-04-30 00:55:04 -0700987
Arjan van de Venda8f2e12008-09-07 10:47:46 -0700988 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800989
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800990 timer_stats_hrtimer_set_start_info(timer);
991
Ingo Molnar935c6312007-03-28 13:17:18 +0200992 /*
993 * Only allow reprogramming if the new base is on this CPU.
994 * (it might still be on another CPU if the timer was pending)
995 */
996 enqueue_hrtimer(timer, new_base,
997 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800998
Thomas Gleixner0c96c592008-04-28 09:23:24 +0200999 /*
1000 * The timer may be expired and moved to the cb_pending
1001 * list. We can not raise the softirq with base lock held due
1002 * to a possible deadlock with runqueue lock.
1003 */
1004 raise = timer->state == HRTIMER_STATE_PENDING;
1005
Steven Rostedtee3ece82008-07-03 14:31:26 -04001006 /*
1007 * We use preempt_disable to prevent this task from migrating after
1008 * setting up the softirq and raising it. Otherwise, if me migrate
1009 * we will raise the softirq on the wrong CPU.
1010 */
1011 preempt_disable();
1012
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001013 unlock_hrtimer_base(timer, &flags);
1014
Thomas Gleixner0c96c592008-04-28 09:23:24 +02001015 if (raise)
1016 hrtimer_raise_softirq();
Steven Rostedtee3ece82008-07-03 14:31:26 -04001017 preempt_enable();
Thomas Gleixner0c96c592008-04-28 09:23:24 +02001018
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001019 return ret;
1020}
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001021EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1022
1023/**
1024 * hrtimer_start - (re)start an relative timer on the current CPU
1025 * @timer: the timer to be added
1026 * @tim: expiry time
1027 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1028 *
1029 * Returns:
1030 * 0 on success
1031 * 1 when the timer was active
1032 */
1033int
1034hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1035{
1036 return hrtimer_start_range_ns(timer, tim, 0, mode);
1037}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001038EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001039
Arjan van de Venda8f2e12008-09-07 10:47:46 -07001040
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001041/**
1042 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001043 * @timer: hrtimer to stop
1044 *
1045 * Returns:
1046 * 0 when the timer was not active
1047 * 1 when the timer was active
1048 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -07001049 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001050 */
1051int hrtimer_try_to_cancel(struct hrtimer *timer)
1052{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001053 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001054 unsigned long flags;
1055 int ret = -1;
1056
1057 base = lock_hrtimer_base(timer, &flags);
1058
Thomas Gleixner303e9672007-02-16 01:27:51 -08001059 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001060 ret = remove_hrtimer(timer, base);
1061
1062 unlock_hrtimer_base(timer, &flags);
1063
1064 return ret;
1065
1066}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001067EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001068
1069/**
1070 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001071 * @timer: the timer to be cancelled
1072 *
1073 * Returns:
1074 * 0 when the timer was not active
1075 * 1 when the timer was active
1076 */
1077int hrtimer_cancel(struct hrtimer *timer)
1078{
1079 for (;;) {
1080 int ret = hrtimer_try_to_cancel(timer);
1081
1082 if (ret >= 0)
1083 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -07001084 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001085 }
1086}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001087EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001088
1089/**
1090 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001091 * @timer: the timer to read
1092 */
1093ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1094{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001095 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001096 unsigned long flags;
1097 ktime_t rem;
1098
1099 base = lock_hrtimer_base(timer, &flags);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001100 rem = hrtimer_expires_remaining(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001101 unlock_hrtimer_base(timer, &flags);
1102
1103 return rem;
1104}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001105EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001106
Russell Kingee9c5782008-04-20 13:59:33 +01001107#ifdef CONFIG_NO_HZ
Tony Lindgren69239742006-03-06 15:42:45 -08001108/**
1109 * hrtimer_get_next_event - get the time until next expiry event
1110 *
1111 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1112 * is pending.
1113 */
1114ktime_t hrtimer_get_next_event(void)
1115{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001116 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1117 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -08001118 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1119 unsigned long flags;
1120 int i;
1121
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001122 spin_lock_irqsave(&cpu_base->lock, flags);
1123
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001124 if (!hrtimer_hres_active()) {
1125 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1126 struct hrtimer *timer;
Tony Lindgren69239742006-03-06 15:42:45 -08001127
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001128 if (!base->first)
1129 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001130
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001131 timer = rb_entry(base->first, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001132 delta.tv64 = hrtimer_get_expires_tv64(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001133 delta = ktime_sub(delta, base->get_time());
1134 if (delta.tv64 < mindelta.tv64)
1135 mindelta.tv64 = delta.tv64;
1136 }
Tony Lindgren69239742006-03-06 15:42:45 -08001137 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001138
1139 spin_unlock_irqrestore(&cpu_base->lock, flags);
1140
Tony Lindgren69239742006-03-06 15:42:45 -08001141 if (mindelta.tv64 < 0)
1142 mindelta.tv64 = 0;
1143 return mindelta;
1144}
1145#endif
1146
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001147static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1148 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001149{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001150 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -08001151
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001152 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -08001153
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001154 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -08001155
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001156 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -08001157 clock_id = CLOCK_MONOTONIC;
1158
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001159 timer->base = &cpu_base->clock_base[clock_id];
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001160 INIT_LIST_HEAD(&timer->cb_entry);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001161 hrtimer_init_timer_hres(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001162
1163#ifdef CONFIG_TIMER_STATS
1164 timer->start_site = NULL;
1165 timer->start_pid = -1;
1166 memset(timer->start_comm, 0, TASK_COMM_LEN);
1167#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001168}
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001169
1170/**
1171 * hrtimer_init - initialize a timer to the given clock
1172 * @timer: the timer to be initialized
1173 * @clock_id: the clock to be used
1174 * @mode: timer mode abs/rel
1175 */
1176void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1177 enum hrtimer_mode mode)
1178{
1179 debug_hrtimer_init(timer);
1180 __hrtimer_init(timer, clock_id, mode);
1181}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001182EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001183
1184/**
1185 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001186 * @which_clock: which clock to query
1187 * @tp: pointer to timespec variable to store the resolution
1188 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001189 * Store the resolution of the clock selected by @which_clock in the
1190 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001191 */
1192int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1193{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001194 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001195
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001196 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1197 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001198
1199 return 0;
1200}
Stephen Hemminger8d16b762006-05-30 21:26:09 -07001201EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001202
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001203static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001204{
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001205 spin_lock_irq(&cpu_base->lock);
1206
1207 while (!list_empty(&cpu_base->cb_pending)) {
1208 enum hrtimer_restart (*fn)(struct hrtimer *);
1209 struct hrtimer *timer;
1210 int restart;
1211
1212 timer = list_entry(cpu_base->cb_pending.next,
1213 struct hrtimer, cb_entry);
1214
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001215 debug_hrtimer_deactivate(timer);
Ingo Molnar82f67cd2007-02-16 01:28:13 -08001216 timer_stats_account_hrtimer(timer);
1217
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001218 fn = timer->function;
1219 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
1220 spin_unlock_irq(&cpu_base->lock);
1221
1222 restart = fn(timer);
1223
1224 spin_lock_irq(&cpu_base->lock);
1225
1226 timer->state &= ~HRTIMER_STATE_CALLBACK;
1227 if (restart == HRTIMER_RESTART) {
1228 BUG_ON(hrtimer_active(timer));
1229 /*
1230 * Enqueue the timer, allow reprogramming of the event
1231 * device
1232 */
1233 enqueue_hrtimer(timer, timer->base, 1);
1234 } else if (hrtimer_active(timer)) {
1235 /*
1236 * If the timer was rearmed on another CPU, reprogram
1237 * the event device.
1238 */
Bodo Stroesserd7b41a22008-04-26 14:10:16 -07001239 struct hrtimer_clock_base *base = timer->base;
1240
1241 if (base->first == &timer->node &&
1242 hrtimer_reprogram(timer, base)) {
1243 /*
1244 * Timer is expired. Thus move it from tree to
1245 * pending list again.
1246 */
1247 __remove_hrtimer(timer, base,
1248 HRTIMER_STATE_PENDING, 0);
1249 list_add_tail(&timer->cb_entry,
1250 &base->cpu_base->cb_pending);
1251 }
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001252 }
1253 }
1254 spin_unlock_irq(&cpu_base->lock);
1255}
1256
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001257static void __run_hrtimer(struct hrtimer *timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001258{
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001259 struct hrtimer_clock_base *base = timer->base;
1260 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1261 enum hrtimer_restart (*fn)(struct hrtimer *);
1262 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001263
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001264 debug_hrtimer_deactivate(timer);
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001265 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1266 timer_stats_account_hrtimer(timer);
Dimitri Sivanich3055add2006-03-31 02:31:20 -08001267
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001268 fn = timer->function;
1269 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
1270 /*
1271 * Used for scheduler timers, avoid lock inversion with
1272 * rq->lock and tasklist_lock.
1273 *
1274 * These timers are required to deal with enqueue expiry
1275 * themselves and are not allowed to migrate.
1276 */
1277 spin_unlock(&cpu_base->lock);
1278 restart = fn(timer);
1279 spin_lock(&cpu_base->lock);
1280 } else
Roman Zippel05cfb612006-03-26 01:38:12 -08001281 restart = fn(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001282
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001283 /*
1284 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1285 * reprogramming of the event hardware. This happens at the end of this
1286 * function anyway.
1287 */
1288 if (restart != HRTIMER_NORESTART) {
1289 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1290 enqueue_hrtimer(timer, base, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001291 }
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001292 timer->state &= ~HRTIMER_STATE_CALLBACK;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001293}
1294
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001295#ifdef CONFIG_HIGH_RES_TIMERS
1296
1297/*
1298 * High resolution timer interrupt
1299 * Called with interrupts disabled
1300 */
1301void hrtimer_interrupt(struct clock_event_device *dev)
1302{
1303 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1304 struct hrtimer_clock_base *base;
1305 ktime_t expires_next, now;
1306 int i, raise = 0;
1307
1308 BUG_ON(!cpu_base->hres_active);
1309 cpu_base->nr_events++;
1310 dev->next_event.tv64 = KTIME_MAX;
1311
1312 retry:
1313 now = ktime_get();
1314
1315 expires_next.tv64 = KTIME_MAX;
1316
1317 base = cpu_base->clock_base;
1318
1319 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1320 ktime_t basenow;
1321 struct rb_node *node;
1322
1323 spin_lock(&cpu_base->lock);
1324
1325 basenow = ktime_add(now, base->offset);
1326
1327 while ((node = base->first)) {
1328 struct hrtimer *timer;
1329
1330 timer = rb_entry(node, struct hrtimer, node);
1331
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001332 /*
1333 * The immediate goal for using the softexpires is
1334 * minimizing wakeups, not running timers at the
1335 * earliest interrupt after their soft expiration.
1336 * This allows us to avoid using a Priority Search
1337 * Tree, which can answer a stabbing querry for
1338 * overlapping intervals and instead use the simple
1339 * BST we already have.
1340 * We don't add extra wakeups by delaying timers that
1341 * are right-of a not yet expired timer, because that
1342 * timer will have to trigger a wakeup anyway.
1343 */
1344
1345 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001346 ktime_t expires;
1347
Arjan van de Vencc584b22008-09-01 15:02:30 -07001348 expires = ktime_sub(hrtimer_get_expires(timer),
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001349 base->offset);
1350 if (expires.tv64 < expires_next.tv64)
1351 expires_next = expires;
1352 break;
1353 }
1354
1355 /* Move softirq callbacks to the pending list */
1356 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1357 __remove_hrtimer(timer, base,
1358 HRTIMER_STATE_PENDING, 0);
1359 list_add_tail(&timer->cb_entry,
1360 &base->cpu_base->cb_pending);
1361 raise = 1;
1362 continue;
1363 }
1364
1365 __run_hrtimer(timer);
1366 }
1367 spin_unlock(&cpu_base->lock);
1368 base++;
1369 }
1370
1371 cpu_base->expires_next = expires_next;
1372
1373 /* Reprogramming necessary ? */
1374 if (expires_next.tv64 != KTIME_MAX) {
1375 if (tick_program_event(expires_next, 0))
1376 goto retry;
1377 }
1378
1379 /* Raise softirq ? */
1380 if (raise)
1381 raise_softirq(HRTIMER_SOFTIRQ);
1382}
1383
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001384/**
1385 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1386 *
1387 * hrtimer_peek_ahead_timers will peek at the timer queue of
1388 * the current cpu and check if there are any timers for which
1389 * the soft expires time has passed. If any such timers exist,
1390 * they are run immediately and then removed from the timer queue.
1391 *
1392 */
1393void hrtimer_peek_ahead_timers(void)
1394{
1395 unsigned long flags;
1396 struct tick_device *td;
1397 struct clock_event_device *dev;
Arjan van de Vendc4304f2008-10-13 10:32:15 -04001398
1399 if (!hrtimer_hres_active())
Arjan van de Ven2e94d1f2008-09-10 16:06:00 -07001400 return;
1401
1402 local_irq_save(flags);
1403 td = &__get_cpu_var(tick_cpu_device);
1404 if (!td)
1405 goto out;
1406 dev = td->evtdev;
1407 if (!dev)
1408 goto out;
1409 hrtimer_interrupt(dev);
1410out:
1411 local_irq_restore(flags);
1412}
1413
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001414static void run_hrtimer_softirq(struct softirq_action *h)
1415{
1416 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
1417}
1418
1419#endif /* CONFIG_HIGH_RES_TIMERS */
1420
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001421/*
1422 * Called from timer softirq every jiffy, expire hrtimers:
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001423 *
1424 * For HRT its the fall back code to run the softirq in the timer
1425 * softirq context in case the hrtimer initialization failed or has
1426 * not been done yet.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001427 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001428void hrtimer_run_pending(void)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001429{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001430 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001431
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001432 if (hrtimer_hres_active())
1433 return;
1434
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001435 /*
1436 * This _is_ ugly: We have to check in the softirq context,
1437 * whether we can switch to highres and / or nohz mode. The
1438 * clocksource switch happens in the timer interrupt with
1439 * xtime_lock held. Notification from there only sets the
1440 * check bit in the tick_oneshot code, otherwise we might
1441 * deadlock vs. xtime_lock.
1442 */
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001443 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001444 hrtimer_switch_to_hres();
1445
1446 run_hrtimer_pending(cpu_base);
1447}
1448
1449/*
1450 * Called from hardirq context every jiffy
1451 */
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001452void hrtimer_run_queues(void)
1453{
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001454 struct rb_node *node;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001455 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001456 struct hrtimer_clock_base *base;
1457 int index, gettime = 1;
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001458
1459 if (hrtimer_hres_active())
1460 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001461
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001462 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1463 base = &cpu_base->clock_base[index];
Thomas Gleixner92127c72006-03-26 01:38:05 -08001464
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001465 if (!base->first)
1466 continue;
1467
Thomas Gleixner259aae82008-04-19 21:31:26 +02001468 if (base->get_softirq_time)
1469 base->softirq_time = base->get_softirq_time();
1470 else if (gettime) {
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001471 hrtimer_get_softirq_time(cpu_base);
1472 gettime = 0;
1473 }
1474
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001475 spin_lock(&cpu_base->lock);
1476
1477 while ((node = base->first)) {
1478 struct hrtimer *timer;
1479
1480 timer = rb_entry(node, struct hrtimer, node);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001481 if (base->softirq_time.tv64 <=
1482 hrtimer_get_expires_tv64(timer))
Dimitri Sivanich833883d2008-04-18 13:39:00 -07001483 break;
1484
1485 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1486 __remove_hrtimer(timer, base,
1487 HRTIMER_STATE_PENDING, 0);
1488 list_add_tail(&timer->cb_entry,
1489 &base->cpu_base->cb_pending);
1490 continue;
1491 }
1492
1493 __run_hrtimer(timer);
1494 }
1495 spin_unlock(&cpu_base->lock);
1496 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001497}
1498
1499/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001500 * Sleep related functions:
1501 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001502static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001503{
1504 struct hrtimer_sleeper *t =
1505 container_of(timer, struct hrtimer_sleeper, timer);
1506 struct task_struct *task = t->task;
1507
1508 t->task = NULL;
1509 if (task)
1510 wake_up_process(task);
1511
1512 return HRTIMER_NORESTART;
1513}
1514
Ingo Molnar36c8b582006-07-03 00:25:41 -07001515void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -08001516{
1517 sl->timer.function = hrtimer_wakeup;
1518 sl->task = task;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001519#ifdef CONFIG_HIGH_RES_TIMERS
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001520 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001521#endif
Thomas Gleixner00362e32006-03-31 02:31:17 -08001522}
1523
Thomas Gleixner669d7862006-03-31 02:31:19 -08001524static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001525{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001526 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001527
Roman Zippel432569b2006-03-26 01:38:08 -08001528 do {
1529 set_current_state(TASK_INTERRUPTIBLE);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001530 hrtimer_start_expires(&t->timer, mode);
Peter Zijlstra37bb6cb2008-01-25 21:08:32 +01001531 if (!hrtimer_active(&t->timer))
1532 t->task = NULL;
Roman Zippel432569b2006-03-26 01:38:08 -08001533
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001534 if (likely(t->task))
1535 schedule();
Roman Zippel432569b2006-03-26 01:38:08 -08001536
Thomas Gleixner669d7862006-03-31 02:31:19 -08001537 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001538 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -08001539
Thomas Gleixner669d7862006-03-31 02:31:19 -08001540 } while (t->task && !signal_pending(current));
1541
Peter Zijlstra3588a082008-02-01 17:45:13 +01001542 __set_current_state(TASK_RUNNING);
1543
Thomas Gleixner669d7862006-03-31 02:31:19 -08001544 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001545}
1546
Oleg Nesterov080344b2008-02-01 17:29:05 +03001547static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1548{
1549 struct timespec rmt;
1550 ktime_t rem;
1551
Arjan van de Vencc584b22008-09-01 15:02:30 -07001552 rem = hrtimer_expires_remaining(timer);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001553 if (rem.tv64 <= 0)
1554 return 0;
1555 rmt = ktime_to_timespec(rem);
1556
1557 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1558 return -EFAULT;
1559
1560 return 1;
1561}
1562
Toyo Abe1711ef32006-09-29 02:00:28 -07001563long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001564{
Thomas Gleixner669d7862006-03-31 02:31:19 -08001565 struct hrtimer_sleeper t;
Oleg Nesterov080344b2008-02-01 17:29:05 +03001566 struct timespec __user *rmtp;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001567 int ret = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001568
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001569 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1570 HRTIMER_MODE_ABS);
Arjan van de Vencc584b22008-09-01 15:02:30 -07001571 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001572
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -08001573 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001574 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001575
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001576 rmtp = restart->nanosleep.rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -08001577 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001578 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001579 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001580 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001581 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001582
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001583 /* The other values in restart are already filled in */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001584 ret = -ERESTART_RESTARTBLOCK;
1585out:
1586 destroy_hrtimer_on_stack(&t.timer);
1587 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001588}
1589
Oleg Nesterov080344b2008-02-01 17:29:05 +03001590long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001591 const enum hrtimer_mode mode, const clockid_t clockid)
1592{
1593 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -08001594 struct hrtimer_sleeper t;
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001595 int ret = 0;
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001596 unsigned long slack;
1597
1598 slack = current->timer_slack_ns;
1599 if (rt_task(current))
1600 slack = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001601
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001602 hrtimer_init_on_stack(&t.timer, clockid, mode);
Arjan van de Ven3bd01202008-09-08 08:58:59 -07001603 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
Roman Zippel432569b2006-03-26 01:38:08 -08001604 if (do_nanosleep(&t, mode))
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001605 goto out;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001606
George Anzinger7978672c2006-02-01 03:05:11 -08001607 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001608 if (mode == HRTIMER_MODE_ABS) {
1609 ret = -ERESTARTNOHAND;
1610 goto out;
1611 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001612
Roman Zippel432569b2006-03-26 01:38:08 -08001613 if (rmtp) {
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001614 ret = update_rmtp(&t.timer, rmtp);
Oleg Nesterov080344b2008-02-01 17:29:05 +03001615 if (ret <= 0)
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001616 goto out;
Roman Zippel432569b2006-03-26 01:38:08 -08001617 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001618
1619 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -07001620 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner029a07e2008-02-10 09:17:43 +01001621 restart->nanosleep.index = t.timer.base->index;
1622 restart->nanosleep.rmtp = rmtp;
Arjan van de Vencc584b22008-09-01 15:02:30 -07001623 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001624
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001625 ret = -ERESTART_RESTARTBLOCK;
1626out:
1627 destroy_hrtimer_on_stack(&t.timer);
1628 return ret;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001629}
1630
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001631asmlinkage long
1632sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1633{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001634 struct timespec tu;
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001635
1636 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1637 return -EFAULT;
1638
1639 if (!timespec_valid(&tu))
1640 return -EINVAL;
1641
Oleg Nesterov080344b2008-02-01 17:29:05 +03001642 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -08001643}
1644
Thomas Gleixner10c94ec2006-01-09 20:52:35 -08001645/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001646 * Functions related to boot-time initialization:
1647 */
Randy Dunlap0ec160d2008-01-21 17:18:24 -08001648static void __cpuinit init_hrtimers_cpu(int cpu)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001649{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001650 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001651 int i;
1652
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001653 spin_lock_init(&cpu_base->lock);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001654
1655 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1656 cpu_base->clock_base[i].cpu_base = cpu_base;
1657
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001658 INIT_LIST_HEAD(&cpu_base->cb_pending);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001659 hrtimer_init_hres(cpu_base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001660}
1661
1662#ifdef CONFIG_HOTPLUG_CPU
1663
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001664static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1665 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001666{
1667 struct hrtimer *timer;
1668 struct rb_node *node;
1669
1670 while ((node = rb_first(&old_base->active))) {
1671 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001672 BUG_ON(hrtimer_callback_running(timer));
Thomas Gleixner237fc6e2008-04-30 00:55:04 -07001673 debug_hrtimer_deactivate(timer);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001674 __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001675 timer->base = new_base;
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001676 /*
1677 * Enqueue the timer. Allow reprogramming of the event device
1678 */
1679 enqueue_hrtimer(timer, new_base, 1);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001680 }
1681}
1682
1683static void migrate_hrtimers(int cpu)
1684{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001685 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001686 int i;
1687
1688 BUG_ON(cpu_online(cpu));
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001689 old_base = &per_cpu(hrtimer_bases, cpu);
1690 new_base = &get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001691
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001692 tick_cancel_sched_timer(cpu);
1693
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001694 local_irq_disable();
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001695 spin_lock(&new_base->lock);
1696 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001697
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001698 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08001699 migrate_hrtimer_list(&old_base->clock_base[i],
1700 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001701 }
1702
Oleg Nesterov8e60e052008-04-04 20:54:10 +02001703 spin_unlock(&old_base->lock);
1704 spin_unlock(&new_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001705 local_irq_enable();
1706 put_cpu_var(hrtimer_bases);
1707}
1708#endif /* CONFIG_HOTPLUG_CPU */
1709
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001710static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001711 unsigned long action, void *hcpu)
1712{
David Miller7713a7d2007-07-16 17:17:44 -07001713 unsigned int cpu = (long)hcpu;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001714
1715 switch (action) {
1716
1717 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001718 case CPU_UP_PREPARE_FROZEN:
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001719 init_hrtimers_cpu(cpu);
1720 break;
1721
1722#ifdef CONFIG_HOTPLUG_CPU
1723 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001724 case CPU_DEAD_FROZEN:
Thomas Gleixnerd316c572007-02-16 01:28:00 -08001725 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001726 migrate_hrtimers(cpu);
1727 break;
1728#endif
1729
1730 default:
1731 break;
1732 }
1733
1734 return NOTIFY_OK;
1735}
1736
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001737static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001738 .notifier_call = hrtimer_cpu_notify,
1739};
1740
1741void __init hrtimers_init(void)
1742{
1743 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1744 (void *)(long)smp_processor_id());
1745 register_cpu_notifier(&hrtimers_nb);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001746#ifdef CONFIG_HIGH_RES_TIMERS
Carlos R. Mafra962cf362008-05-15 11:15:37 -03001747 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
Thomas Gleixner54cdfdb2007-02-16 01:28:11 -08001748#endif
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001749}
1750
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001751/**
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001752 * schedule_hrtimeout_range - sleep until timeout
1753 * @expires: timeout value (ktime_t)
1754 * @delta: slack in expires timeout (ktime_t)
1755 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1756 *
1757 * Make the current task sleep until the given expiry time has
1758 * elapsed. The routine will return immediately unless
1759 * the current task state has been set (see set_current_state()).
1760 *
1761 * The @delta argument gives the kernel the freedom to schedule the
1762 * actual wakeup to a time that is both power and performance friendly.
1763 * The kernel give the normal best effort behavior for "@expires+@delta",
1764 * but may decide to fire the timer earlier, but no earlier than @expires.
1765 *
1766 * You can set the task state as follows -
1767 *
1768 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1769 * pass before the routine returns.
1770 *
1771 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1772 * delivered to the current task.
1773 *
1774 * The current task state is guaranteed to be TASK_RUNNING when this
1775 * routine returns.
1776 *
1777 * Returns 0 when the timer has expired otherwise -EINTR
1778 */
1779int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1780 const enum hrtimer_mode mode)
1781{
1782 struct hrtimer_sleeper t;
1783
1784 /*
1785 * Optimize when a zero timeout value is given. It does not
1786 * matter whether this is an absolute or a relative time.
1787 */
1788 if (expires && !expires->tv64) {
1789 __set_current_state(TASK_RUNNING);
1790 return 0;
1791 }
1792
1793 /*
1794 * A NULL parameter means "inifinte"
1795 */
1796 if (!expires) {
1797 schedule();
1798 __set_current_state(TASK_RUNNING);
1799 return -EINTR;
1800 }
1801
1802 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1803 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1804
1805 hrtimer_init_sleeper(&t, current);
1806
1807 hrtimer_start_expires(&t.timer, mode);
1808 if (!hrtimer_active(&t.timer))
1809 t.task = NULL;
1810
1811 if (likely(t.task))
1812 schedule();
1813
1814 hrtimer_cancel(&t.timer);
1815 destroy_hrtimer_on_stack(&t.timer);
1816
1817 __set_current_state(TASK_RUNNING);
1818
1819 return !t.task ? 0 : -EINTR;
1820}
1821EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1822
1823/**
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001824 * schedule_hrtimeout - sleep until timeout
1825 * @expires: timeout value (ktime_t)
1826 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1827 *
1828 * Make the current task sleep until the given expiry time has
1829 * elapsed. The routine will return immediately unless
1830 * the current task state has been set (see set_current_state()).
1831 *
1832 * You can set the task state as follows -
1833 *
1834 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1835 * pass before the routine returns.
1836 *
1837 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1838 * delivered to the current task.
1839 *
1840 * The current task state is guaranteed to be TASK_RUNNING when this
1841 * routine returns.
1842 *
1843 * Returns 0 when the timer has expired otherwise -EINTR
1844 */
1845int __sched schedule_hrtimeout(ktime_t *expires,
1846 const enum hrtimer_mode mode)
1847{
Arjan van de Ven654c8e02008-09-01 15:47:08 -07001848 return schedule_hrtimeout_range(expires, 0, mode);
Arjan van de Ven7bb67432008-08-31 08:05:58 -07001849}
1850EXPORT_SYMBOL_GPL(schedule_hrtimeout);