]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/hrtimer.c
hrtimers: increase clock min delta threshold while interrupt hanging
[net-next-2.6.git] / kernel / hrtimer.c
CommitLineData
c0a31329
TG
1/*
2 * linux/kernel/hrtimer.c
3 *
3c8aa39d 4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
79bf2bb3 5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
54cdfdb4 6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
c0a31329
TG
7 *
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 *
66188fae
TG
25 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
27 *
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
30 *
c0a31329
TG
31 * 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>
54cdfdb4 40#include <linux/kallsyms.h>
c0a31329 41#include <linux/interrupt.h>
79bf2bb3 42#include <linux/tick.h>
54cdfdb4
TG
43#include <linux/seq_file.h>
44#include <linux/err.h>
237fc6e7 45#include <linux/debugobjects.h>
c0a31329
TG
46
47#include <asm/uaccess.h>
48
49/**
50 * ktime_get - get the monotonic time in ktime_t format
51 *
52 * returns the time in ktime_t format
53 */
d316c57f 54ktime_t ktime_get(void)
c0a31329
TG
55{
56 struct timespec now;
57
58 ktime_get_ts(&now);
59
60 return timespec_to_ktime(now);
61}
641b9e0e 62EXPORT_SYMBOL_GPL(ktime_get);
c0a31329
TG
63
64/**
65 * ktime_get_real - get the real (wall-) time in ktime_t format
66 *
67 * returns the time in ktime_t format
68 */
d316c57f 69ktime_t ktime_get_real(void)
c0a31329
TG
70{
71 struct timespec now;
72
73 getnstimeofday(&now);
74
75 return timespec_to_ktime(now);
76}
77
78EXPORT_SYMBOL_GPL(ktime_get_real);
79
80/*
81 * The timer bases:
7978672c
GA
82 *
83 * Note: If we want to add new timer bases, we have to skip the two
84 * clock ids captured by the cpu-timers. We do this by holding empty
85 * entries rather than doing math adjustment of the clock ids.
86 * This ensures that we capture erroneous accesses to these clock ids
87 * rather than moving them into the range of valid clock id's.
c0a31329 88 */
54cdfdb4 89DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
c0a31329 90{
3c8aa39d
TG
91
92 .clock_base =
c0a31329 93 {
3c8aa39d
TG
94 {
95 .index = CLOCK_REALTIME,
96 .get_time = &ktime_get_real,
54cdfdb4 97 .resolution = KTIME_LOW_RES,
3c8aa39d
TG
98 },
99 {
100 .index = CLOCK_MONOTONIC,
101 .get_time = &ktime_get,
54cdfdb4 102 .resolution = KTIME_LOW_RES,
3c8aa39d
TG
103 },
104 }
c0a31329
TG
105};
106
107/**
108 * ktime_get_ts - get the monotonic clock in timespec format
c0a31329
TG
109 * @ts: pointer to timespec variable
110 *
111 * The function calculates the monotonic clock from the realtime
112 * clock and the wall_to_monotonic offset and stores the result
72fd4a35 113 * in normalized timespec format in the variable pointed to by @ts.
c0a31329
TG
114 */
115void ktime_get_ts(struct timespec *ts)
116{
117 struct timespec tomono;
118 unsigned long seq;
119
120 do {
121 seq = read_seqbegin(&xtime_lock);
122 getnstimeofday(ts);
123 tomono = wall_to_monotonic;
124
125 } while (read_seqretry(&xtime_lock, seq));
126
127 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
128 ts->tv_nsec + tomono.tv_nsec);
129}
69778e32 130EXPORT_SYMBOL_GPL(ktime_get_ts);
c0a31329 131
92127c7a
TG
132/*
133 * Get the coarse grained time at the softirq based on xtime and
134 * wall_to_monotonic.
135 */
3c8aa39d 136static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
92127c7a
TG
137{
138 ktime_t xtim, tomono;
ad28d94a 139 struct timespec xts, tom;
92127c7a
TG
140 unsigned long seq;
141
142 do {
143 seq = read_seqbegin(&xtime_lock);
2c6b47de 144 xts = current_kernel_time();
ad28d94a 145 tom = wall_to_monotonic;
92127c7a
TG
146 } while (read_seqretry(&xtime_lock, seq));
147
f4304ab2 148 xtim = timespec_to_ktime(xts);
ad28d94a 149 tomono = timespec_to_ktime(tom);
3c8aa39d
TG
150 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
151 base->clock_base[CLOCK_MONOTONIC].softirq_time =
152 ktime_add(xtim, tomono);
92127c7a
TG
153}
154
c0a31329
TG
155/*
156 * Functions and macros which are different for UP/SMP systems are kept in a
157 * single place
158 */
159#ifdef CONFIG_SMP
160
c0a31329
TG
161/*
162 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
163 * means that all timers which are tied to this base via timer->base are
164 * locked, and the base itself is locked too.
165 *
166 * So __run_timers/migrate_timers can safely modify all timers which could
167 * be found on the lists/queues.
168 *
169 * When the timer's base is locked, and the timer removed from list, it is
170 * possible to set timer->base = NULL and drop the lock: the timer remains
171 * locked.
172 */
3c8aa39d
TG
173static
174struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
175 unsigned long *flags)
c0a31329 176{
3c8aa39d 177 struct hrtimer_clock_base *base;
c0a31329
TG
178
179 for (;;) {
180 base = timer->base;
181 if (likely(base != NULL)) {
3c8aa39d 182 spin_lock_irqsave(&base->cpu_base->lock, *flags);
c0a31329
TG
183 if (likely(base == timer->base))
184 return base;
185 /* The timer has migrated to another CPU: */
3c8aa39d 186 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
c0a31329
TG
187 }
188 cpu_relax();
189 }
190}
191
192/*
193 * Switch the timer base to the current CPU when possible.
194 */
3c8aa39d
TG
195static inline struct hrtimer_clock_base *
196switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
c0a31329 197{
3c8aa39d
TG
198 struct hrtimer_clock_base *new_base;
199 struct hrtimer_cpu_base *new_cpu_base;
c0a31329 200
3c8aa39d
TG
201 new_cpu_base = &__get_cpu_var(hrtimer_bases);
202 new_base = &new_cpu_base->clock_base[base->index];
c0a31329
TG
203
204 if (base != new_base) {
205 /*
206 * We are trying to schedule the timer on the local CPU.
207 * However we can't change timer's base while it is running,
208 * so we keep it on the same CPU. No hassle vs. reprogramming
209 * the event source in the high resolution case. The softirq
210 * code will take care of this when the timer function has
211 * completed. There is no conflict as we hold the lock until
212 * the timer is enqueued.
213 */
54cdfdb4 214 if (unlikely(hrtimer_callback_running(timer)))
c0a31329
TG
215 return base;
216
217 /* See the comment in lock_timer_base() */
218 timer->base = NULL;
3c8aa39d
TG
219 spin_unlock(&base->cpu_base->lock);
220 spin_lock(&new_base->cpu_base->lock);
c0a31329
TG
221 timer->base = new_base;
222 }
223 return new_base;
224}
225
226#else /* CONFIG_SMP */
227
3c8aa39d 228static inline struct hrtimer_clock_base *
c0a31329
TG
229lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
230{
3c8aa39d 231 struct hrtimer_clock_base *base = timer->base;
c0a31329 232
3c8aa39d 233 spin_lock_irqsave(&base->cpu_base->lock, *flags);
c0a31329
TG
234
235 return base;
236}
237
54cdfdb4 238# define switch_hrtimer_base(t, b) (b)
c0a31329
TG
239
240#endif /* !CONFIG_SMP */
241
242/*
243 * Functions for the union type storage format of ktime_t which are
244 * too large for inlining:
245 */
246#if BITS_PER_LONG < 64
247# ifndef CONFIG_KTIME_SCALAR
248/**
249 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
c0a31329
TG
250 * @kt: addend
251 * @nsec: the scalar nsec value to add
252 *
253 * Returns the sum of kt and nsec in ktime_t format
254 */
255ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
256{
257 ktime_t tmp;
258
259 if (likely(nsec < NSEC_PER_SEC)) {
260 tmp.tv64 = nsec;
261 } else {
262 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
263
264 tmp = ktime_set((long)nsec, rem);
265 }
266
267 return ktime_add(kt, tmp);
268}
b8b8fd2d
DH
269
270EXPORT_SYMBOL_GPL(ktime_add_ns);
a272378d
ACM
271
272/**
273 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
274 * @kt: minuend
275 * @nsec: the scalar nsec value to subtract
276 *
277 * Returns the subtraction of @nsec from @kt in ktime_t format
278 */
279ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
280{
281 ktime_t tmp;
282
283 if (likely(nsec < NSEC_PER_SEC)) {
284 tmp.tv64 = nsec;
285 } else {
286 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
287
288 tmp = ktime_set((long)nsec, rem);
289 }
290
291 return ktime_sub(kt, tmp);
292}
293
294EXPORT_SYMBOL_GPL(ktime_sub_ns);
c0a31329
TG
295# endif /* !CONFIG_KTIME_SCALAR */
296
297/*
298 * Divide a ktime value by a nanosecond value
299 */
4d672e7a 300u64 ktime_divns(const ktime_t kt, s64 div)
c0a31329 301{
900cfa46 302 u64 dclc;
c0a31329
TG
303 int sft = 0;
304
900cfa46 305 dclc = ktime_to_ns(kt);
c0a31329
TG
306 /* Make sure the divisor is less than 2^32: */
307 while (div >> 32) {
308 sft++;
309 div >>= 1;
310 }
311 dclc >>= sft;
312 do_div(dclc, (unsigned long) div);
313
4d672e7a 314 return dclc;
c0a31329 315}
c0a31329
TG
316#endif /* BITS_PER_LONG >= 64 */
317
5a7780e7
TG
318/*
319 * Add two ktime values and do a safety check for overflow:
320 */
321ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
322{
323 ktime_t res = ktime_add(lhs, rhs);
324
325 /*
326 * We use KTIME_SEC_MAX here, the maximum timeout which we can
327 * return to user space in a timespec:
328 */
329 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
330 res = ktime_set(KTIME_SEC_MAX, 0);
331
332 return res;
333}
334
237fc6e7
TG
335#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
336
337static struct debug_obj_descr hrtimer_debug_descr;
338
339/*
340 * fixup_init is called when:
341 * - an active object is initialized
342 */
343static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
344{
345 struct hrtimer *timer = addr;
346
347 switch (state) {
348 case ODEBUG_STATE_ACTIVE:
349 hrtimer_cancel(timer);
350 debug_object_init(timer, &hrtimer_debug_descr);
351 return 1;
352 default:
353 return 0;
354 }
355}
356
357/*
358 * fixup_activate is called when:
359 * - an active object is activated
360 * - an unknown object is activated (might be a statically initialized object)
361 */
362static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
363{
364 switch (state) {
365
366 case ODEBUG_STATE_NOTAVAILABLE:
367 WARN_ON_ONCE(1);
368 return 0;
369
370 case ODEBUG_STATE_ACTIVE:
371 WARN_ON(1);
372
373 default:
374 return 0;
375 }
376}
377
378/*
379 * fixup_free is called when:
380 * - an active object is freed
381 */
382static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
383{
384 struct hrtimer *timer = addr;
385
386 switch (state) {
387 case ODEBUG_STATE_ACTIVE:
388 hrtimer_cancel(timer);
389 debug_object_free(timer, &hrtimer_debug_descr);
390 return 1;
391 default:
392 return 0;
393 }
394}
395
396static struct debug_obj_descr hrtimer_debug_descr = {
397 .name = "hrtimer",
398 .fixup_init = hrtimer_fixup_init,
399 .fixup_activate = hrtimer_fixup_activate,
400 .fixup_free = hrtimer_fixup_free,
401};
402
403static inline void debug_hrtimer_init(struct hrtimer *timer)
404{
405 debug_object_init(timer, &hrtimer_debug_descr);
406}
407
408static inline void debug_hrtimer_activate(struct hrtimer *timer)
409{
410 debug_object_activate(timer, &hrtimer_debug_descr);
411}
412
413static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
414{
415 debug_object_deactivate(timer, &hrtimer_debug_descr);
416}
417
418static inline void debug_hrtimer_free(struct hrtimer *timer)
419{
420 debug_object_free(timer, &hrtimer_debug_descr);
421}
422
423static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
424 enum hrtimer_mode mode);
425
426void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
427 enum hrtimer_mode mode)
428{
429 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
430 __hrtimer_init(timer, clock_id, mode);
431}
432
433void destroy_hrtimer_on_stack(struct hrtimer *timer)
434{
435 debug_object_free(timer, &hrtimer_debug_descr);
436}
437
438#else
439static inline void debug_hrtimer_init(struct hrtimer *timer) { }
440static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
441static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
442#endif
443
54cdfdb4
TG
444/* High resolution timer related functions */
445#ifdef CONFIG_HIGH_RES_TIMERS
446
447/*
448 * High resolution timer enabled ?
449 */
450static int hrtimer_hres_enabled __read_mostly = 1;
451
452/*
453 * Enable / Disable high resolution mode
454 */
455static int __init setup_hrtimer_hres(char *str)
456{
457 if (!strcmp(str, "off"))
458 hrtimer_hres_enabled = 0;
459 else if (!strcmp(str, "on"))
460 hrtimer_hres_enabled = 1;
461 else
462 return 0;
463 return 1;
464}
465
466__setup("highres=", setup_hrtimer_hres);
467
468/*
469 * hrtimer_high_res_enabled - query, if the highres mode is enabled
470 */
471static inline int hrtimer_is_hres_enabled(void)
472{
473 return hrtimer_hres_enabled;
474}
475
476/*
477 * Is the high resolution mode active ?
478 */
479static inline int hrtimer_hres_active(void)
480{
481 return __get_cpu_var(hrtimer_bases).hres_active;
482}
483
484/*
485 * Reprogram the event source with checking both queues for the
486 * next event
487 * Called with interrupts disabled and base->lock held
488 */
489static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
490{
491 int i;
492 struct hrtimer_clock_base *base = cpu_base->clock_base;
493 ktime_t expires;
494
495 cpu_base->expires_next.tv64 = KTIME_MAX;
496
497 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
498 struct hrtimer *timer;
499
500 if (!base->first)
501 continue;
502 timer = rb_entry(base->first, struct hrtimer, node);
cc584b21 503 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
54cdfdb4
TG
504 if (expires.tv64 < cpu_base->expires_next.tv64)
505 cpu_base->expires_next = expires;
506 }
507
508 if (cpu_base->expires_next.tv64 != KTIME_MAX)
509 tick_program_event(cpu_base->expires_next, 1);
510}
511
512/*
513 * Shared reprogramming for clock_realtime and clock_monotonic
514 *
515 * When a timer is enqueued and expires earlier than the already enqueued
516 * timers, we have to check, whether it expires earlier than the timer for
517 * which the clock event device was armed.
518 *
519 * Called with interrupts disabled and base->cpu_base.lock held
520 */
521static int hrtimer_reprogram(struct hrtimer *timer,
522 struct hrtimer_clock_base *base)
523{
524 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
cc584b21 525 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
54cdfdb4
TG
526 int res;
527
cc584b21 528 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
63070a79 529
54cdfdb4
TG
530 /*
531 * When the callback is running, we do not reprogram the clock event
532 * device. The timer callback is either running on a different CPU or
3a4fa0a2 533 * the callback is executed in the hrtimer_interrupt context. The
54cdfdb4
TG
534 * reprogramming is handled either by the softirq, which called the
535 * callback or at the end of the hrtimer_interrupt.
536 */
537 if (hrtimer_callback_running(timer))
538 return 0;
539
63070a79
TG
540 /*
541 * CLOCK_REALTIME timer might be requested with an absolute
542 * expiry time which is less than base->offset. Nothing wrong
543 * about that, just avoid to call into the tick code, which
544 * has now objections against negative expiry values.
545 */
546 if (expires.tv64 < 0)
547 return -ETIME;
548
54cdfdb4
TG
549 if (expires.tv64 >= expires_next->tv64)
550 return 0;
551
552 /*
553 * Clockevents returns -ETIME, when the event was in the past.
554 */
555 res = tick_program_event(expires, 0);
556 if (!IS_ERR_VALUE(res))
557 *expires_next = expires;
558 return res;
559}
560
561
562/*
563 * Retrigger next event is called after clock was set
564 *
565 * Called with interrupts disabled via on_each_cpu()
566 */
567static void retrigger_next_event(void *arg)
568{
569 struct hrtimer_cpu_base *base;
570 struct timespec realtime_offset;
571 unsigned long seq;
572
573 if (!hrtimer_hres_active())
574 return;
575
576 do {
577 seq = read_seqbegin(&xtime_lock);
578 set_normalized_timespec(&realtime_offset,
579 -wall_to_monotonic.tv_sec,
580 -wall_to_monotonic.tv_nsec);
581 } while (read_seqretry(&xtime_lock, seq));
582
583 base = &__get_cpu_var(hrtimer_bases);
584
585 /* Adjust CLOCK_REALTIME offset */
586 spin_lock(&base->lock);
587 base->clock_base[CLOCK_REALTIME].offset =
588 timespec_to_ktime(realtime_offset);
589
590 hrtimer_force_reprogram(base);
591 spin_unlock(&base->lock);
592}
593
594/*
595 * Clock realtime was set
596 *
597 * Change the offset of the realtime clock vs. the monotonic
598 * clock.
599 *
600 * We might have to reprogram the high resolution timer interrupt. On
601 * SMP we call the architecture specific code to retrigger _all_ high
602 * resolution timer interrupts. On UP we just disable interrupts and
603 * call the high resolution interrupt code.
604 */
605void clock_was_set(void)
606{
607 /* Retrigger the CPU local events everywhere */
15c8b6c1 608 on_each_cpu(retrigger_next_event, NULL, 1);
54cdfdb4
TG
609}
610
995f054f
IM
611/*
612 * During resume we might have to reprogram the high resolution timer
613 * interrupt (on the local CPU):
614 */
615void hres_timers_resume(void)
616{
1d4a7f1c
PZ
617 WARN_ONCE(!irqs_disabled(),
618 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
619
995f054f
IM
620 retrigger_next_event(NULL);
621}
622
54cdfdb4
TG
623/*
624 * Initialize the high resolution related parts of cpu_base
625 */
626static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
627{
628 base->expires_next.tv64 = KTIME_MAX;
629 base->hres_active = 0;
54cdfdb4
TG
630}
631
632/*
633 * Initialize the high resolution related parts of a hrtimer
634 */
635static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
636{
54cdfdb4
TG
637}
638
ca109491 639
54cdfdb4
TG
640/*
641 * When High resolution timers are active, try to reprogram. Note, that in case
642 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
643 * check happens. The timer gets enqueued into the rbtree. The reprogramming
644 * and expiry check is done in the hrtimer_interrupt or in the softirq.
645 */
646static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
647 struct hrtimer_clock_base *base)
648{
649 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
a6037b61
PZ
650 spin_unlock(&base->cpu_base->lock);
651 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
652 spin_lock(&base->cpu_base->lock);
ca109491 653 return 1;
54cdfdb4
TG
654 }
655 return 0;
656}
657
658/*
659 * Switch to high resolution mode
660 */
f8953856 661static int hrtimer_switch_to_hres(void)
54cdfdb4 662{
820de5c3
IM
663 int cpu = smp_processor_id();
664 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
54cdfdb4
TG
665 unsigned long flags;
666
667 if (base->hres_active)
f8953856 668 return 1;
54cdfdb4
TG
669
670 local_irq_save(flags);
671
672 if (tick_init_highres()) {
673 local_irq_restore(flags);
820de5c3
IM
674 printk(KERN_WARNING "Could not switch to high resolution "
675 "mode on CPU %d\n", cpu);
f8953856 676 return 0;
54cdfdb4
TG
677 }
678 base->hres_active = 1;
679 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
680 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
681
682 tick_setup_sched_timer();
683
684 /* "Retrigger" the interrupt to get things going */
685 retrigger_next_event(NULL);
686 local_irq_restore(flags);
edfed66e 687 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
54cdfdb4 688 smp_processor_id());
f8953856 689 return 1;
54cdfdb4
TG
690}
691
692#else
693
694static inline int hrtimer_hres_active(void) { return 0; }
695static inline int hrtimer_is_hres_enabled(void) { return 0; }
f8953856 696static inline int hrtimer_switch_to_hres(void) { return 0; }
54cdfdb4
TG
697static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
698static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
699 struct hrtimer_clock_base *base)
700{
701 return 0;
702}
54cdfdb4
TG
703static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
704static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
705
706#endif /* CONFIG_HIGH_RES_TIMERS */
707
82f67cd9
IM
708#ifdef CONFIG_TIMER_STATS
709void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
710{
711 if (timer->start_site)
712 return;
713
714 timer->start_site = addr;
715 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
716 timer->start_pid = current->pid;
717}
718#endif
719
c0a31329 720/*
6506f2aa 721 * Counterpart to lock_hrtimer_base above:
c0a31329
TG
722 */
723static inline
724void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
725{
3c8aa39d 726 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
c0a31329
TG
727}
728
729/**
730 * hrtimer_forward - forward the timer expiry
c0a31329 731 * @timer: hrtimer to forward
44f21475 732 * @now: forward past this time
c0a31329
TG
733 * @interval: the interval to forward
734 *
735 * Forward the timer expiry so it will expire in the future.
8dca6f33 736 * Returns the number of overruns.
c0a31329 737 */
4d672e7a 738u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
c0a31329 739{
4d672e7a 740 u64 orun = 1;
44f21475 741 ktime_t delta;
c0a31329 742
cc584b21 743 delta = ktime_sub(now, hrtimer_get_expires(timer));
c0a31329
TG
744
745 if (delta.tv64 < 0)
746 return 0;
747
c9db4fa1
TG
748 if (interval.tv64 < timer->base->resolution.tv64)
749 interval.tv64 = timer->base->resolution.tv64;
750
c0a31329 751 if (unlikely(delta.tv64 >= interval.tv64)) {
df869b63 752 s64 incr = ktime_to_ns(interval);
c0a31329
TG
753
754 orun = ktime_divns(delta, incr);
cc584b21
AV
755 hrtimer_add_expires_ns(timer, incr * orun);
756 if (hrtimer_get_expires_tv64(timer) > now.tv64)
c0a31329
TG
757 return orun;
758 /*
759 * This (and the ktime_add() below) is the
760 * correction for exact:
761 */
762 orun++;
763 }
cc584b21 764 hrtimer_add_expires(timer, interval);
c0a31329
TG
765
766 return orun;
767}
6bdb6b62 768EXPORT_SYMBOL_GPL(hrtimer_forward);
c0a31329
TG
769
770/*
771 * enqueue_hrtimer - internal function to (re)start a timer
772 *
773 * The timer is inserted in expiry order. Insertion into the
774 * red black tree is O(log(n)). Must hold the base lock.
a6037b61
PZ
775 *
776 * Returns 1 when the new timer is the leftmost timer in the tree.
c0a31329 777 */
a6037b61
PZ
778static int enqueue_hrtimer(struct hrtimer *timer,
779 struct hrtimer_clock_base *base)
c0a31329
TG
780{
781 struct rb_node **link = &base->active.rb_node;
c0a31329
TG
782 struct rb_node *parent = NULL;
783 struct hrtimer *entry;
99bc2fcb 784 int leftmost = 1;
c0a31329 785
237fc6e7
TG
786 debug_hrtimer_activate(timer);
787
c0a31329
TG
788 /*
789 * Find the right place in the rbtree:
790 */
791 while (*link) {
792 parent = *link;
793 entry = rb_entry(parent, struct hrtimer, node);
794 /*
795 * We dont care about collisions. Nodes with
796 * the same expiry time stay together.
797 */
cc584b21
AV
798 if (hrtimer_get_expires_tv64(timer) <
799 hrtimer_get_expires_tv64(entry)) {
c0a31329 800 link = &(*link)->rb_left;
99bc2fcb 801 } else {
c0a31329 802 link = &(*link)->rb_right;
99bc2fcb
IM
803 leftmost = 0;
804 }
c0a31329
TG
805 }
806
807 /*
288867ec
TG
808 * Insert the timer to the rbtree and check whether it
809 * replaces the first pending timer
c0a31329 810 */
a6037b61 811 if (leftmost)
54cdfdb4 812 base->first = &timer->node;
54cdfdb4 813
c0a31329
TG
814 rb_link_node(&timer->node, parent, link);
815 rb_insert_color(&timer->node, &base->active);
303e967f
TG
816 /*
817 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
818 * state of a possibly running callback.
819 */
820 timer->state |= HRTIMER_STATE_ENQUEUED;
a6037b61
PZ
821
822 return leftmost;
288867ec 823}
c0a31329
TG
824
825/*
826 * __remove_hrtimer - internal function to remove a timer
827 *
828 * Caller must hold the base lock.
54cdfdb4
TG
829 *
830 * High resolution timer mode reprograms the clock event device when the
831 * timer is the one which expires next. The caller can disable this by setting
832 * reprogram to zero. This is useful, when the context does a reprogramming
833 * anyway (e.g. timer interrupt)
c0a31329 834 */
3c8aa39d 835static void __remove_hrtimer(struct hrtimer *timer,
303e967f 836 struct hrtimer_clock_base *base,
54cdfdb4 837 unsigned long newstate, int reprogram)
c0a31329 838{
ca109491 839 if (timer->state & HRTIMER_STATE_ENQUEUED) {
54cdfdb4
TG
840 /*
841 * Remove the timer from the rbtree and replace the
842 * first entry pointer if necessary.
843 */
844 if (base->first == &timer->node) {
845 base->first = rb_next(&timer->node);
846 /* Reprogram the clock event device. if enabled */
847 if (reprogram && hrtimer_hres_active())
848 hrtimer_force_reprogram(base->cpu_base);
849 }
850 rb_erase(&timer->node, &base->active);
851 }
303e967f 852 timer->state = newstate;
c0a31329
TG
853}
854
855/*
856 * remove hrtimer, called with base lock held
857 */
858static inline int
3c8aa39d 859remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
c0a31329 860{
303e967f 861 if (hrtimer_is_queued(timer)) {
54cdfdb4
TG
862 int reprogram;
863
864 /*
865 * Remove the timer and force reprogramming when high
866 * resolution mode is active and the timer is on the current
867 * CPU. If we remove a timer on another CPU, reprogramming is
868 * skipped. The interrupt event on this CPU is fired and
869 * reprogramming happens in the interrupt handler. This is a
870 * rare case and less expensive than a smp call.
871 */
237fc6e7 872 debug_hrtimer_deactivate(timer);
82f67cd9 873 timer_stats_hrtimer_clear_start_info(timer);
54cdfdb4
TG
874 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
875 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
876 reprogram);
c0a31329
TG
877 return 1;
878 }
879 return 0;
880}
881
882/**
e1dd7bc5 883 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
c0a31329
TG
884 * @timer: the timer to be added
885 * @tim: expiry time
da8f2e17 886 * @delta_ns: "slack" range for the timer
c0a31329
TG
887 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
888 *
889 * Returns:
890 * 0 on success
891 * 1 when the timer was active
892 */
893int
da8f2e17
AV
894hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
895 const enum hrtimer_mode mode)
c0a31329 896{
3c8aa39d 897 struct hrtimer_clock_base *base, *new_base;
c0a31329 898 unsigned long flags;
a6037b61 899 int ret, leftmost;
c0a31329
TG
900
901 base = lock_hrtimer_base(timer, &flags);
902
903 /* Remove an active timer from the queue: */
904 ret = remove_hrtimer(timer, base);
905
906 /* Switch the timer base, if necessary: */
907 new_base = switch_hrtimer_base(timer, base);
908
c9cb2e3d 909 if (mode == HRTIMER_MODE_REL) {
5a7780e7 910 tim = ktime_add_safe(tim, new_base->get_time());
06027bdd
IM
911 /*
912 * CONFIG_TIME_LOW_RES is a temporary way for architectures
913 * to signal that they simply return xtime in
914 * do_gettimeoffset(). In this case we want to round up by
915 * resolution when starting a relative timer, to avoid short
916 * timeouts. This will go away with the GTOD framework.
917 */
918#ifdef CONFIG_TIME_LOW_RES
5a7780e7 919 tim = ktime_add_safe(tim, base->resolution);
06027bdd
IM
920#endif
921 }
237fc6e7 922
da8f2e17 923 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
c0a31329 924
82f67cd9
IM
925 timer_stats_hrtimer_set_start_info(timer);
926
a6037b61
PZ
927 leftmost = enqueue_hrtimer(timer, new_base);
928
935c631d
IM
929 /*
930 * Only allow reprogramming if the new base is on this CPU.
931 * (it might still be on another CPU if the timer was pending)
a6037b61
PZ
932 *
933 * XXX send_remote_softirq() ?
935c631d 934 */
a6037b61
PZ
935 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
936 hrtimer_enqueue_reprogram(timer, new_base);
c0a31329
TG
937
938 unlock_hrtimer_base(timer, &flags);
939
940 return ret;
941}
da8f2e17
AV
942EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
943
944/**
e1dd7bc5 945 * hrtimer_start - (re)start an hrtimer on the current CPU
da8f2e17
AV
946 * @timer: the timer to be added
947 * @tim: expiry time
948 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
949 *
950 * Returns:
951 * 0 on success
952 * 1 when the timer was active
953 */
954int
955hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
956{
957 return hrtimer_start_range_ns(timer, tim, 0, mode);
958}
8d16b764 959EXPORT_SYMBOL_GPL(hrtimer_start);
c0a31329 960
da8f2e17 961
c0a31329
TG
962/**
963 * hrtimer_try_to_cancel - try to deactivate a timer
c0a31329
TG
964 * @timer: hrtimer to stop
965 *
966 * Returns:
967 * 0 when the timer was not active
968 * 1 when the timer was active
969 * -1 when the timer is currently excuting the callback function and
fa9799e3 970 * cannot be stopped
c0a31329
TG
971 */
972int hrtimer_try_to_cancel(struct hrtimer *timer)
973{
3c8aa39d 974 struct hrtimer_clock_base *base;
c0a31329
TG
975 unsigned long flags;
976 int ret = -1;
977
978 base = lock_hrtimer_base(timer, &flags);
979
303e967f 980 if (!hrtimer_callback_running(timer))
c0a31329
TG
981 ret = remove_hrtimer(timer, base);
982
983 unlock_hrtimer_base(timer, &flags);
984
985 return ret;
986
987}
8d16b764 988EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
c0a31329
TG
989
990/**
991 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
c0a31329
TG
992 * @timer: the timer to be cancelled
993 *
994 * Returns:
995 * 0 when the timer was not active
996 * 1 when the timer was active
997 */
998int hrtimer_cancel(struct hrtimer *timer)
999{
1000 for (;;) {
1001 int ret = hrtimer_try_to_cancel(timer);
1002
1003 if (ret >= 0)
1004 return ret;
5ef37b19 1005 cpu_relax();
c0a31329
TG
1006 }
1007}
8d16b764 1008EXPORT_SYMBOL_GPL(hrtimer_cancel);
c0a31329
TG
1009
1010/**
1011 * hrtimer_get_remaining - get remaining time for the timer
c0a31329
TG
1012 * @timer: the timer to read
1013 */
1014ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1015{
3c8aa39d 1016 struct hrtimer_clock_base *base;
c0a31329
TG
1017 unsigned long flags;
1018 ktime_t rem;
1019
1020 base = lock_hrtimer_base(timer, &flags);
cc584b21 1021 rem = hrtimer_expires_remaining(timer);
c0a31329
TG
1022 unlock_hrtimer_base(timer, &flags);
1023
1024 return rem;
1025}
8d16b764 1026EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
c0a31329 1027
ee9c5785 1028#ifdef CONFIG_NO_HZ
69239749
TL
1029/**
1030 * hrtimer_get_next_event - get the time until next expiry event
1031 *
1032 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1033 * is pending.
1034 */
1035ktime_t hrtimer_get_next_event(void)
1036{
3c8aa39d
TG
1037 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1038 struct hrtimer_clock_base *base = cpu_base->clock_base;
69239749
TL
1039 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1040 unsigned long flags;
1041 int i;
1042
3c8aa39d
TG
1043 spin_lock_irqsave(&cpu_base->lock, flags);
1044
54cdfdb4
TG
1045 if (!hrtimer_hres_active()) {
1046 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1047 struct hrtimer *timer;
69239749 1048
54cdfdb4
TG
1049 if (!base->first)
1050 continue;
3c8aa39d 1051
54cdfdb4 1052 timer = rb_entry(base->first, struct hrtimer, node);
cc584b21 1053 delta.tv64 = hrtimer_get_expires_tv64(timer);
54cdfdb4
TG
1054 delta = ktime_sub(delta, base->get_time());
1055 if (delta.tv64 < mindelta.tv64)
1056 mindelta.tv64 = delta.tv64;
1057 }
69239749 1058 }
3c8aa39d
TG
1059
1060 spin_unlock_irqrestore(&cpu_base->lock, flags);
1061
69239749
TL
1062 if (mindelta.tv64 < 0)
1063 mindelta.tv64 = 0;
1064 return mindelta;
1065}
1066#endif
1067
237fc6e7
TG
1068static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1069 enum hrtimer_mode mode)
c0a31329 1070{
3c8aa39d 1071 struct hrtimer_cpu_base *cpu_base;
c0a31329 1072
7978672c
GA
1073 memset(timer, 0, sizeof(struct hrtimer));
1074
3c8aa39d 1075 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
c0a31329 1076
c9cb2e3d 1077 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
7978672c
GA
1078 clock_id = CLOCK_MONOTONIC;
1079
3c8aa39d 1080 timer->base = &cpu_base->clock_base[clock_id];
d3d74453 1081 INIT_LIST_HEAD(&timer->cb_entry);
54cdfdb4 1082 hrtimer_init_timer_hres(timer);
82f67cd9
IM
1083
1084#ifdef CONFIG_TIMER_STATS
1085 timer->start_site = NULL;
1086 timer->start_pid = -1;
1087 memset(timer->start_comm, 0, TASK_COMM_LEN);
1088#endif
c0a31329 1089}
237fc6e7
TG
1090
1091/**
1092 * hrtimer_init - initialize a timer to the given clock
1093 * @timer: the timer to be initialized
1094 * @clock_id: the clock to be used
1095 * @mode: timer mode abs/rel
1096 */
1097void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1098 enum hrtimer_mode mode)
1099{
1100 debug_hrtimer_init(timer);
1101 __hrtimer_init(timer, clock_id, mode);
1102}
8d16b764 1103EXPORT_SYMBOL_GPL(hrtimer_init);
c0a31329
TG
1104
1105/**
1106 * hrtimer_get_res - get the timer resolution for a clock
c0a31329
TG
1107 * @which_clock: which clock to query
1108 * @tp: pointer to timespec variable to store the resolution
1109 *
72fd4a35
RD
1110 * Store the resolution of the clock selected by @which_clock in the
1111 * variable pointed to by @tp.
c0a31329
TG
1112 */
1113int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1114{
3c8aa39d 1115 struct hrtimer_cpu_base *cpu_base;
c0a31329 1116
3c8aa39d
TG
1117 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1118 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
c0a31329
TG
1119
1120 return 0;
1121}
8d16b764 1122EXPORT_SYMBOL_GPL(hrtimer_get_res);
c0a31329 1123
d3d74453
PZ
1124static void __run_hrtimer(struct hrtimer *timer)
1125{
1126 struct hrtimer_clock_base *base = timer->base;
1127 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1128 enum hrtimer_restart (*fn)(struct hrtimer *);
1129 int restart;
1130
ca109491
PZ
1131 WARN_ON(!irqs_disabled());
1132
237fc6e7 1133 debug_hrtimer_deactivate(timer);
d3d74453
PZ
1134 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1135 timer_stats_account_hrtimer(timer);
d3d74453 1136 fn = timer->function;
ca109491
PZ
1137
1138 /*
1139 * Because we run timers from hardirq context, there is no chance
1140 * they get migrated to another cpu, therefore its safe to unlock
1141 * the timer base.
1142 */
1143 spin_unlock(&cpu_base->lock);
1144 restart = fn(timer);
1145 spin_lock(&cpu_base->lock);
d3d74453
PZ
1146
1147 /*
e3f1d883
TG
1148 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1149 * we do not reprogramm the event hardware. Happens either in
1150 * hrtimer_start_range_ns() or in hrtimer_interrupt()
d3d74453
PZ
1151 */
1152 if (restart != HRTIMER_NORESTART) {
1153 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
a6037b61 1154 enqueue_hrtimer(timer, base);
d3d74453
PZ
1155 }
1156 timer->state &= ~HRTIMER_STATE_CALLBACK;
1157}
1158
54cdfdb4
TG
1159#ifdef CONFIG_HIGH_RES_TIMERS
1160
7f22391c
FW
1161static int force_clock_reprogram;
1162
1163/*
1164 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1165 * is hanging, which could happen with something that slows the interrupt
1166 * such as the tracing. Then we force the clock reprogramming for each future
1167 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1168 * threshold that we will overwrite.
1169 * The next tick event will be scheduled to 3 times we currently spend on
1170 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1171 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1172 * let it running without serious starvation.
1173 */
1174
1175static inline void
1176hrtimer_interrupt_hanging(struct clock_event_device *dev,
1177 ktime_t try_time)
1178{
1179 force_clock_reprogram = 1;
1180 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1181 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1182 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1183}
54cdfdb4
TG
1184/*
1185 * High resolution timer interrupt
1186 * Called with interrupts disabled
1187 */
1188void hrtimer_interrupt(struct clock_event_device *dev)
1189{
1190 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1191 struct hrtimer_clock_base *base;
1192 ktime_t expires_next, now;
7f22391c 1193 int nr_retries = 0;
ca109491 1194 int i;
54cdfdb4
TG
1195
1196 BUG_ON(!cpu_base->hres_active);
1197 cpu_base->nr_events++;
1198 dev->next_event.tv64 = KTIME_MAX;
1199
1200 retry:
7f22391c
FW
1201 /* 5 retries is enough to notice a hang */
1202 if (!(++nr_retries % 5))
1203 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1204
54cdfdb4
TG
1205 now = ktime_get();
1206
1207 expires_next.tv64 = KTIME_MAX;
1208
1209 base = cpu_base->clock_base;
1210
1211 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1212 ktime_t basenow;
1213 struct rb_node *node;
1214
1215 spin_lock(&cpu_base->lock);
1216
1217 basenow = ktime_add(now, base->offset);
1218
1219 while ((node = base->first)) {
1220 struct hrtimer *timer;
1221
1222 timer = rb_entry(node, struct hrtimer, node);
1223
654c8e0b
AV
1224 /*
1225 * The immediate goal for using the softexpires is
1226 * minimizing wakeups, not running timers at the
1227 * earliest interrupt after their soft expiration.
1228 * This allows us to avoid using a Priority Search
1229 * Tree, which can answer a stabbing querry for
1230 * overlapping intervals and instead use the simple
1231 * BST we already have.
1232 * We don't add extra wakeups by delaying timers that
1233 * are right-of a not yet expired timer, because that
1234 * timer will have to trigger a wakeup anyway.
1235 */
1236
1237 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
54cdfdb4
TG
1238 ktime_t expires;
1239
cc584b21 1240 expires = ktime_sub(hrtimer_get_expires(timer),
54cdfdb4
TG
1241 base->offset);
1242 if (expires.tv64 < expires_next.tv64)
1243 expires_next = expires;
1244 break;
1245 }
1246
d3d74453 1247 __run_hrtimer(timer);
54cdfdb4
TG
1248 }
1249 spin_unlock(&cpu_base->lock);
1250 base++;
1251 }
1252
1253 cpu_base->expires_next = expires_next;
1254
1255 /* Reprogramming necessary ? */
1256 if (expires_next.tv64 != KTIME_MAX) {
7f22391c 1257 if (tick_program_event(expires_next, force_clock_reprogram))
54cdfdb4
TG
1258 goto retry;
1259 }
54cdfdb4
TG
1260}
1261
8bdec955
TG
1262/*
1263 * local version of hrtimer_peek_ahead_timers() called with interrupts
1264 * disabled.
1265 */
1266static void __hrtimer_peek_ahead_timers(void)
1267{
1268 struct tick_device *td;
1269
1270 if (!hrtimer_hres_active())
1271 return;
1272
1273 td = &__get_cpu_var(tick_cpu_device);
1274 if (td && td->evtdev)
1275 hrtimer_interrupt(td->evtdev);
1276}
1277
2e94d1f7
AV
1278/**
1279 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1280 *
1281 * hrtimer_peek_ahead_timers will peek at the timer queue of
1282 * the current cpu and check if there are any timers for which
1283 * the soft expires time has passed. If any such timers exist,
1284 * they are run immediately and then removed from the timer queue.
1285 *
1286 */
1287void hrtimer_peek_ahead_timers(void)
1288{
643bdf68 1289 unsigned long flags;
dc4304f7 1290
2e94d1f7 1291 local_irq_save(flags);
8bdec955 1292 __hrtimer_peek_ahead_timers();
2e94d1f7
AV
1293 local_irq_restore(flags);
1294}
1295
a6037b61
PZ
1296static void run_hrtimer_softirq(struct softirq_action *h)
1297{
1298 hrtimer_peek_ahead_timers();
1299}
1300
82c5b7b5
IM
1301#else /* CONFIG_HIGH_RES_TIMERS */
1302
1303static inline void __hrtimer_peek_ahead_timers(void) { }
1304
1305#endif /* !CONFIG_HIGH_RES_TIMERS */
82f67cd9 1306
d3d74453
PZ
1307/*
1308 * Called from timer softirq every jiffy, expire hrtimers:
1309 *
1310 * For HRT its the fall back code to run the softirq in the timer
1311 * softirq context in case the hrtimer initialization failed or has
1312 * not been done yet.
1313 */
1314void hrtimer_run_pending(void)
1315{
d3d74453
PZ
1316 if (hrtimer_hres_active())
1317 return;
54cdfdb4 1318
d3d74453
PZ
1319 /*
1320 * This _is_ ugly: We have to check in the softirq context,
1321 * whether we can switch to highres and / or nohz mode. The
1322 * clocksource switch happens in the timer interrupt with
1323 * xtime_lock held. Notification from there only sets the
1324 * check bit in the tick_oneshot code, otherwise we might
1325 * deadlock vs. xtime_lock.
1326 */
1327 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1328 hrtimer_switch_to_hres();
54cdfdb4
TG
1329}
1330
c0a31329 1331/*
d3d74453 1332 * Called from hardirq context every jiffy
c0a31329 1333 */
833883d9 1334void hrtimer_run_queues(void)
c0a31329 1335{
288867ec 1336 struct rb_node *node;
833883d9
DS
1337 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1338 struct hrtimer_clock_base *base;
1339 int index, gettime = 1;
c0a31329 1340
833883d9 1341 if (hrtimer_hres_active())
3055adda
DS
1342 return;
1343
833883d9
DS
1344 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1345 base = &cpu_base->clock_base[index];
c0a31329 1346
833883d9 1347 if (!base->first)
d3d74453 1348 continue;
833883d9 1349
d7cfb60c 1350 if (gettime) {
833883d9
DS
1351 hrtimer_get_softirq_time(cpu_base);
1352 gettime = 0;
b75f7a51 1353 }
d3d74453 1354
833883d9 1355 spin_lock(&cpu_base->lock);
c0a31329 1356
833883d9
DS
1357 while ((node = base->first)) {
1358 struct hrtimer *timer;
54cdfdb4 1359
833883d9 1360 timer = rb_entry(node, struct hrtimer, node);
cc584b21
AV
1361 if (base->softirq_time.tv64 <=
1362 hrtimer_get_expires_tv64(timer))
833883d9
DS
1363 break;
1364
833883d9
DS
1365 __run_hrtimer(timer);
1366 }
1367 spin_unlock(&cpu_base->lock);
1368 }
c0a31329
TG
1369}
1370
10c94ec1
TG
1371/*
1372 * Sleep related functions:
1373 */
c9cb2e3d 1374static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
00362e33
TG
1375{
1376 struct hrtimer_sleeper *t =
1377 container_of(timer, struct hrtimer_sleeper, timer);
1378 struct task_struct *task = t->task;
1379
1380 t->task = NULL;
1381 if (task)
1382 wake_up_process(task);
1383
1384 return HRTIMER_NORESTART;
1385}
1386
36c8b586 1387void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
00362e33
TG
1388{
1389 sl->timer.function = hrtimer_wakeup;
1390 sl->task = task;
1391}
1392
669d7868 1393static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
432569bb 1394{
669d7868 1395 hrtimer_init_sleeper(t, current);
10c94ec1 1396
432569bb
RZ
1397 do {
1398 set_current_state(TASK_INTERRUPTIBLE);
cc584b21 1399 hrtimer_start_expires(&t->timer, mode);
37bb6cb4
PZ
1400 if (!hrtimer_active(&t->timer))
1401 t->task = NULL;
432569bb 1402
54cdfdb4
TG
1403 if (likely(t->task))
1404 schedule();
432569bb 1405
669d7868 1406 hrtimer_cancel(&t->timer);
c9cb2e3d 1407 mode = HRTIMER_MODE_ABS;
669d7868
TG
1408
1409 } while (t->task && !signal_pending(current));
432569bb 1410
3588a085
PZ
1411 __set_current_state(TASK_RUNNING);
1412
669d7868 1413 return t->task == NULL;
10c94ec1
TG
1414}
1415
080344b9
ON
1416static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1417{
1418 struct timespec rmt;
1419 ktime_t rem;
1420
cc584b21 1421 rem = hrtimer_expires_remaining(timer);
080344b9
ON
1422 if (rem.tv64 <= 0)
1423 return 0;
1424 rmt = ktime_to_timespec(rem);
1425
1426 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1427 return -EFAULT;
1428
1429 return 1;
1430}
1431
1711ef38 1432long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
10c94ec1 1433{
669d7868 1434 struct hrtimer_sleeper t;
080344b9 1435 struct timespec __user *rmtp;
237fc6e7 1436 int ret = 0;
10c94ec1 1437
237fc6e7
TG
1438 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1439 HRTIMER_MODE_ABS);
cc584b21 1440 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
10c94ec1 1441
c9cb2e3d 1442 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
237fc6e7 1443 goto out;
10c94ec1 1444
029a07e0 1445 rmtp = restart->nanosleep.rmtp;
432569bb 1446 if (rmtp) {
237fc6e7 1447 ret = update_rmtp(&t.timer, rmtp);
080344b9 1448 if (ret <= 0)
237fc6e7 1449 goto out;
432569bb 1450 }
10c94ec1 1451
10c94ec1 1452 /* The other values in restart are already filled in */
237fc6e7
TG
1453 ret = -ERESTART_RESTARTBLOCK;
1454out:
1455 destroy_hrtimer_on_stack(&t.timer);
1456 return ret;
10c94ec1
TG
1457}
1458
080344b9 1459long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
10c94ec1
TG
1460 const enum hrtimer_mode mode, const clockid_t clockid)
1461{
1462 struct restart_block *restart;
669d7868 1463 struct hrtimer_sleeper t;
237fc6e7 1464 int ret = 0;
3bd01206
AV
1465 unsigned long slack;
1466
1467 slack = current->timer_slack_ns;
1468 if (rt_task(current))
1469 slack = 0;
10c94ec1 1470
237fc6e7 1471 hrtimer_init_on_stack(&t.timer, clockid, mode);
3bd01206 1472 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
432569bb 1473 if (do_nanosleep(&t, mode))
237fc6e7 1474 goto out;
10c94ec1 1475
7978672c 1476 /* Absolute timers do not update the rmtp value and restart: */
237fc6e7
TG
1477 if (mode == HRTIMER_MODE_ABS) {
1478 ret = -ERESTARTNOHAND;
1479 goto out;
1480 }
10c94ec1 1481
432569bb 1482 if (rmtp) {
237fc6e7 1483 ret = update_rmtp(&t.timer, rmtp);
080344b9 1484 if (ret <= 0)
237fc6e7 1485 goto out;
432569bb 1486 }
10c94ec1
TG
1487
1488 restart = &current_thread_info()->restart_block;
1711ef38 1489 restart->fn = hrtimer_nanosleep_restart;
029a07e0
TG
1490 restart->nanosleep.index = t.timer.base->index;
1491 restart->nanosleep.rmtp = rmtp;
cc584b21 1492 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
10c94ec1 1493
237fc6e7
TG
1494 ret = -ERESTART_RESTARTBLOCK;
1495out:
1496 destroy_hrtimer_on_stack(&t.timer);
1497 return ret;
10c94ec1
TG
1498}
1499
58fd3aa2
HC
1500SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1501 struct timespec __user *, rmtp)
6ba1b912 1502{
080344b9 1503 struct timespec tu;
6ba1b912
TG
1504
1505 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1506 return -EFAULT;
1507
1508 if (!timespec_valid(&tu))
1509 return -EINVAL;
1510
080344b9 1511 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
6ba1b912
TG
1512}
1513
c0a31329
TG
1514/*
1515 * Functions related to boot-time initialization:
1516 */
0ec160dd 1517static void __cpuinit init_hrtimers_cpu(int cpu)
c0a31329 1518{
3c8aa39d 1519 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
c0a31329
TG
1520 int i;
1521
3c8aa39d 1522 spin_lock_init(&cpu_base->lock);
3c8aa39d
TG
1523
1524 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1525 cpu_base->clock_base[i].cpu_base = cpu_base;
1526
54cdfdb4 1527 hrtimer_init_hres(cpu_base);
c0a31329
TG
1528}
1529
1530#ifdef CONFIG_HOTPLUG_CPU
1531
ca109491 1532static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
37810659 1533 struct hrtimer_clock_base *new_base)
c0a31329
TG
1534{
1535 struct hrtimer *timer;
1536 struct rb_node *node;
1537
1538 while ((node = rb_first(&old_base->active))) {
1539 timer = rb_entry(node, struct hrtimer, node);
54cdfdb4 1540 BUG_ON(hrtimer_callback_running(timer));
237fc6e7 1541 debug_hrtimer_deactivate(timer);
b00c1a99
TG
1542
1543 /*
1544 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1545 * timer could be seen as !active and just vanish away
1546 * under us on another CPU
1547 */
1548 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
c0a31329 1549 timer->base = new_base;
54cdfdb4 1550 /*
e3f1d883
TG
1551 * Enqueue the timers on the new cpu. This does not
1552 * reprogram the event device in case the timer
1553 * expires before the earliest on this CPU, but we run
1554 * hrtimer_interrupt after we migrated everything to
1555 * sort out already expired timers and reprogram the
1556 * event device.
54cdfdb4 1557 */
a6037b61 1558 enqueue_hrtimer(timer, new_base);
41e1022e 1559
b00c1a99
TG
1560 /* Clear the migration state bit */
1561 timer->state &= ~HRTIMER_STATE_MIGRATE;
c0a31329
TG
1562 }
1563}
1564
d5fd43c4 1565static void migrate_hrtimers(int scpu)
c0a31329 1566{
3c8aa39d 1567 struct hrtimer_cpu_base *old_base, *new_base;
731a55ba 1568 int i;
c0a31329 1569
37810659 1570 BUG_ON(cpu_online(scpu));
37810659 1571 tick_cancel_sched_timer(scpu);
731a55ba
TG
1572
1573 local_irq_disable();
1574 old_base = &per_cpu(hrtimer_bases, scpu);
1575 new_base = &__get_cpu_var(hrtimer_bases);
d82f0b0f
ON
1576 /*
1577 * The caller is globally serialized and nobody else
1578 * takes two locks at once, deadlock is not possible.
1579 */
731a55ba 1580 spin_lock(&new_base->lock);
8e60e05f 1581 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
c0a31329 1582
3c8aa39d 1583 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
ca109491 1584 migrate_hrtimer_list(&old_base->clock_base[i],
37810659 1585 &new_base->clock_base[i]);
c0a31329
TG
1586 }
1587
8e60e05f 1588 spin_unlock(&old_base->lock);
731a55ba 1589 spin_unlock(&new_base->lock);
37810659 1590
731a55ba
TG
1591 /* Check, if we got expired work to do */
1592 __hrtimer_peek_ahead_timers();
1593 local_irq_enable();
c0a31329 1594}
37810659 1595
c0a31329
TG
1596#endif /* CONFIG_HOTPLUG_CPU */
1597
8c78f307 1598static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
c0a31329
TG
1599 unsigned long action, void *hcpu)
1600{
b2e3c0ad 1601 int scpu = (long)hcpu;
c0a31329
TG
1602
1603 switch (action) {
1604
1605 case CPU_UP_PREPARE:
8bb78442 1606 case CPU_UP_PREPARE_FROZEN:
37810659 1607 init_hrtimers_cpu(scpu);
c0a31329
TG
1608 break;
1609
1610#ifdef CONFIG_HOTPLUG_CPU
1611 case CPU_DEAD:
8bb78442 1612 case CPU_DEAD_FROZEN:
b2e3c0ad 1613 {
37810659 1614 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
d5fd43c4 1615 migrate_hrtimers(scpu);
c0a31329 1616 break;
b2e3c0ad 1617 }
c0a31329
TG
1618#endif
1619
1620 default:
1621 break;
1622 }
1623
1624 return NOTIFY_OK;
1625}
1626
8c78f307 1627static struct notifier_block __cpuinitdata hrtimers_nb = {
c0a31329
TG
1628 .notifier_call = hrtimer_cpu_notify,
1629};
1630
1631void __init hrtimers_init(void)
1632{
1633 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1634 (void *)(long)smp_processor_id());
1635 register_cpu_notifier(&hrtimers_nb);
a6037b61
PZ
1636#ifdef CONFIG_HIGH_RES_TIMERS
1637 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1638#endif
c0a31329
TG
1639}
1640
7bb67439 1641/**
654c8e0b 1642 * schedule_hrtimeout_range - sleep until timeout
7bb67439 1643 * @expires: timeout value (ktime_t)
654c8e0b 1644 * @delta: slack in expires timeout (ktime_t)
7bb67439
AV
1645 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1646 *
1647 * Make the current task sleep until the given expiry time has
1648 * elapsed. The routine will return immediately unless
1649 * the current task state has been set (see set_current_state()).
1650 *
654c8e0b
AV
1651 * The @delta argument gives the kernel the freedom to schedule the
1652 * actual wakeup to a time that is both power and performance friendly.
1653 * The kernel give the normal best effort behavior for "@expires+@delta",
1654 * but may decide to fire the timer earlier, but no earlier than @expires.
1655 *
7bb67439
AV
1656 * You can set the task state as follows -
1657 *
1658 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1659 * pass before the routine returns.
1660 *
1661 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1662 * delivered to the current task.
1663 *
1664 * The current task state is guaranteed to be TASK_RUNNING when this
1665 * routine returns.
1666 *
1667 * Returns 0 when the timer has expired otherwise -EINTR
1668 */
654c8e0b 1669int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
7bb67439
AV
1670 const enum hrtimer_mode mode)
1671{
1672 struct hrtimer_sleeper t;
1673
1674 /*
1675 * Optimize when a zero timeout value is given. It does not
1676 * matter whether this is an absolute or a relative time.
1677 */
1678 if (expires && !expires->tv64) {
1679 __set_current_state(TASK_RUNNING);
1680 return 0;
1681 }
1682
1683 /*
1684 * A NULL parameter means "inifinte"
1685 */
1686 if (!expires) {
1687 schedule();
1688 __set_current_state(TASK_RUNNING);
1689 return -EINTR;
1690 }
1691
1692 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
654c8e0b 1693 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
7bb67439
AV
1694
1695 hrtimer_init_sleeper(&t, current);
1696
cc584b21 1697 hrtimer_start_expires(&t.timer, mode);
7bb67439
AV
1698 if (!hrtimer_active(&t.timer))
1699 t.task = NULL;
1700
1701 if (likely(t.task))
1702 schedule();
1703
1704 hrtimer_cancel(&t.timer);
1705 destroy_hrtimer_on_stack(&t.timer);
1706
1707 __set_current_state(TASK_RUNNING);
1708
1709 return !t.task ? 0 : -EINTR;
1710}
654c8e0b
AV
1711EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1712
1713/**
1714 * schedule_hrtimeout - sleep until timeout
1715 * @expires: timeout value (ktime_t)
1716 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1717 *
1718 * Make the current task sleep until the given expiry time has
1719 * elapsed. The routine will return immediately unless
1720 * the current task state has been set (see set_current_state()).
1721 *
1722 * You can set the task state as follows -
1723 *
1724 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1725 * pass before the routine returns.
1726 *
1727 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1728 * delivered to the current task.
1729 *
1730 * The current task state is guaranteed to be TASK_RUNNING when this
1731 * routine returns.
1732 *
1733 * Returns 0 when the timer has expired otherwise -EINTR
1734 */
1735int __sched schedule_hrtimeout(ktime_t *expires,
1736 const enum hrtimer_mode mode)
1737{
1738 return schedule_hrtimeout_range(expires, 0, mode);
1739}
7bb67439 1740EXPORT_SYMBOL_GPL(schedule_hrtimeout);