hrtimer: another build fix
[linux-block.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>
54cdfdb4 35#include <linux/irq.h>
c0a31329
TG
36#include <linux/module.h>
37#include <linux/percpu.h>
38#include <linux/hrtimer.h>
39#include <linux/notifier.h>
40#include <linux/syscalls.h>
54cdfdb4 41#include <linux/kallsyms.h>
c0a31329 42#include <linux/interrupt.h>
79bf2bb3 43#include <linux/tick.h>
54cdfdb4
TG
44#include <linux/seq_file.h>
45#include <linux/err.h>
237fc6e7 46#include <linux/debugobjects.h>
c0a31329
TG
47
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 */
d316c57f 55ktime_t ktime_get(void)
c0a31329
TG
56{
57 struct timespec now;
58
59 ktime_get_ts(&now);
60
61 return timespec_to_ktime(now);
62}
641b9e0e 63EXPORT_SYMBOL_GPL(ktime_get);
c0a31329
TG
64
65/**
66 * ktime_get_real - get the real (wall-) time in ktime_t format
67 *
68 * returns the time in ktime_t format
69 */
d316c57f 70ktime_t ktime_get_real(void)
c0a31329
TG
71{
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:
7978672c
GA
83 *
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.
c0a31329 89 */
54cdfdb4 90DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
c0a31329 91{
3c8aa39d
TG
92
93 .clock_base =
c0a31329 94 {
3c8aa39d
TG
95 {
96 .index = CLOCK_REALTIME,
97 .get_time = &ktime_get_real,
54cdfdb4 98 .resolution = KTIME_LOW_RES,
3c8aa39d
TG
99 },
100 {
101 .index = CLOCK_MONOTONIC,
102 .get_time = &ktime_get,
54cdfdb4 103 .resolution = KTIME_LOW_RES,
3c8aa39d
TG
104 },
105 }
c0a31329
TG
106};
107
108/**
109 * ktime_get_ts - get the monotonic clock in timespec format
c0a31329
TG
110 * @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
72fd4a35 114 * in normalized timespec format in the variable pointed to by @ts.
c0a31329
TG
115 */
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}
69778e32 131EXPORT_SYMBOL_GPL(ktime_get_ts);
c0a31329 132
92127c7a
TG
133/*
134 * Get the coarse grained time at the softirq based on xtime and
135 * wall_to_monotonic.
136 */
3c8aa39d 137static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
92127c7a
TG
138{
139 ktime_t xtim, tomono;
ad28d94a 140 struct timespec xts, tom;
92127c7a
TG
141 unsigned long seq;
142
143 do {
144 seq = read_seqbegin(&xtime_lock);
2c6b47de 145 xts = current_kernel_time();
ad28d94a 146 tom = wall_to_monotonic;
92127c7a
TG
147 } while (read_seqretry(&xtime_lock, seq));
148
f4304ab2 149 xtim = timespec_to_ktime(xts);
ad28d94a 150 tomono = timespec_to_ktime(tom);
3c8aa39d
TG
151 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
152 base->clock_base[CLOCK_MONOTONIC].softirq_time =
153 ktime_add(xtim, tomono);
92127c7a
TG
154}
155
c0a31329
TG
156/*
157 * Functions and macros which are different for UP/SMP systems are kept in a
158 * single place
159 */
160#ifdef CONFIG_SMP
161
c0a31329
TG
162/*
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 */
3c8aa39d
TG
174static
175struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
176 unsigned long *flags)
c0a31329 177{
3c8aa39d 178 struct hrtimer_clock_base *base;
c0a31329
TG
179
180 for (;;) {
181 base = timer->base;
182 if (likely(base != NULL)) {
3c8aa39d 183 spin_lock_irqsave(&base->cpu_base->lock, *flags);
c0a31329
TG
184 if (likely(base == timer->base))
185 return base;
186 /* The timer has migrated to another CPU: */
3c8aa39d 187 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
c0a31329
TG
188 }
189 cpu_relax();
190 }
191}
192
193/*
194 * Switch the timer base to the current CPU when possible.
195 */
3c8aa39d
TG
196static inline struct hrtimer_clock_base *
197switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
c0a31329 198{
3c8aa39d
TG
199 struct hrtimer_clock_base *new_base;
200 struct hrtimer_cpu_base *new_cpu_base;
c0a31329 201
3c8aa39d
TG
202 new_cpu_base = &__get_cpu_var(hrtimer_bases);
203 new_base = &new_cpu_base->clock_base[base->index];
c0a31329
TG
204
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 */
54cdfdb4 215 if (unlikely(hrtimer_callback_running(timer)))
c0a31329
TG
216 return base;
217
218 /* See the comment in lock_timer_base() */
219 timer->base = NULL;
3c8aa39d
TG
220 spin_unlock(&base->cpu_base->lock);
221 spin_lock(&new_base->cpu_base->lock);
c0a31329
TG
222 timer->base = new_base;
223 }
224 return new_base;
225}
226
227#else /* CONFIG_SMP */
228
3c8aa39d 229static inline struct hrtimer_clock_base *
c0a31329
TG
230lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
231{
3c8aa39d 232 struct hrtimer_clock_base *base = timer->base;
c0a31329 233
3c8aa39d 234 spin_lock_irqsave(&base->cpu_base->lock, *flags);
c0a31329
TG
235
236 return base;
237}
238
54cdfdb4 239# define switch_hrtimer_base(t, b) (b)
c0a31329
TG
240
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
c0a31329
TG
251 * @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}
b8b8fd2d
DH
270
271EXPORT_SYMBOL_GPL(ktime_add_ns);
a272378d
ACM
272
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);
c0a31329
TG
296# endif /* !CONFIG_KTIME_SCALAR */
297
298/*
299 * Divide a ktime value by a nanosecond value
300 */
4d672e7a 301u64 ktime_divns(const ktime_t kt, s64 div)
c0a31329 302{
900cfa46 303 u64 dclc;
c0a31329
TG
304 int sft = 0;
305
900cfa46 306 dclc = ktime_to_ns(kt);
c0a31329
TG
307 /* 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
4d672e7a 315 return dclc;
c0a31329 316}
c0a31329
TG
317#endif /* BITS_PER_LONG >= 64 */
318
5a7780e7
TG
319/*
320 * 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
237fc6e7
TG
336#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
d3d74453
PZ
445/*
446 * 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
54cdfdb4
TG
461/* 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);
cc584b21 520 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
54cdfdb4
TG
521 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;
cc584b21 542 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
54cdfdb4
TG
543 int res;
544
cc584b21 545 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
63070a79 546
54cdfdb4
TG
547 /*
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
3a4fa0a2 550 * the callback is executed in the hrtimer_interrupt context. The
54cdfdb4
TG
551 * 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
63070a79
TG
557 /*
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
54cdfdb4
TG
566 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 */
15c8b6c1 625 on_each_cpu(retrigger_next_event, NULL, 1);
54cdfdb4
TG
626}
627
995f054f
IM
628/*
629 * During resume we might have to reprogram the high resolution timer
630 * interrupt (on the local CPU):
631 */
632void hres_timers_resume(void)
633{
995f054f
IM
634 /* Retrigger the CPU local events: */
635 retrigger_next_event(NULL);
636}
637
54cdfdb4
TG
638/*
639 * 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;
54cdfdb4
TG
645}
646
647/*
648 * Initialize the high resolution related parts of a hrtimer
649 */
650static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
651{
54cdfdb4
TG
652}
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:
237fc6e7 668 debug_hrtimer_deactivate(timer);
54cdfdb4
TG
669 /*
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 */
237fc6e7 683 debug_hrtimer_deactivate(timer);
54cdfdb4
TG
684 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;
54cdfdb4
TG
693 return 1;
694 default:
695 BUG();
696 }
697 }
698 return 0;
699}
700
701/*
702 * Switch to high resolution mode
703 */
f8953856 704static int hrtimer_switch_to_hres(void)
54cdfdb4 705{
820de5c3
IM
706 int cpu = smp_processor_id();
707 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
54cdfdb4
TG
708 unsigned long flags;
709
710 if (base->hres_active)
f8953856 711 return 1;
54cdfdb4
TG
712
713 local_irq_save(flags);
714
715 if (tick_init_highres()) {
716 local_irq_restore(flags);
820de5c3
IM
717 printk(KERN_WARNING "Could not switch to high resolution "
718 "mode on CPU %d\n", cpu);
f8953856 719 return 0;
54cdfdb4
TG
720 }
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);
edfed66e 730 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
54cdfdb4 731 smp_processor_id());
f8953856 732 return 1;
54cdfdb4
TG
733}
734
0c96c597
TG
735static inline void hrtimer_raise_softirq(void)
736{
737 raise_softirq(HRTIMER_SOFTIRQ);
738}
739
54cdfdb4
TG
740#else
741
742static inline int hrtimer_hres_active(void) { return 0; }
743static inline int hrtimer_is_hres_enabled(void) { return 0; }
f8953856 744static inline int hrtimer_switch_to_hres(void) { return 0; }
54cdfdb4
TG
745static 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}
54cdfdb4
TG
751static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
752static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
d3d74453
PZ
753static inline int hrtimer_reprogram(struct hrtimer *timer,
754 struct hrtimer_clock_base *base)
755{
756 return 0;
757}
0c96c597 758static inline void hrtimer_raise_softirq(void) { }
54cdfdb4
TG
759
760#endif /* CONFIG_HIGH_RES_TIMERS */
761
82f67cd9
IM
762#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
c0a31329 774/*
6506f2aa 775 * Counterpart to lock_hrtimer_base above:
c0a31329
TG
776 */
777static inline
778void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
779{
3c8aa39d 780 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
c0a31329
TG
781}
782
783/**
784 * hrtimer_forward - forward the timer expiry
c0a31329 785 * @timer: hrtimer to forward
44f21475 786 * @now: forward past this time
c0a31329
TG
787 * @interval: the interval to forward
788 *
789 * Forward the timer expiry so it will expire in the future.
8dca6f33 790 * Returns the number of overruns.
c0a31329 791 */
4d672e7a 792u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
c0a31329 793{
4d672e7a 794 u64 orun = 1;
44f21475 795 ktime_t delta;
c0a31329 796
cc584b21 797 delta = ktime_sub(now, hrtimer_get_expires(timer));
c0a31329
TG
798
799 if (delta.tv64 < 0)
800 return 0;
801
c9db4fa1
TG
802 if (interval.tv64 < timer->base->resolution.tv64)
803 interval.tv64 = timer->base->resolution.tv64;
804
c0a31329 805 if (unlikely(delta.tv64 >= interval.tv64)) {
df869b63 806 s64 incr = ktime_to_ns(interval);
c0a31329
TG
807
808 orun = ktime_divns(delta, incr);
cc584b21
AV
809 hrtimer_add_expires_ns(timer, incr * orun);
810 if (hrtimer_get_expires_tv64(timer) > now.tv64)
c0a31329
TG
811 return orun;
812 /*
813 * This (and the ktime_add() below) is the
814 * correction for exact:
815 */
816 orun++;
817 }
cc584b21 818 hrtimer_add_expires(timer, interval);
c0a31329
TG
819
820 return orun;
821}
6bdb6b62 822EXPORT_SYMBOL_GPL(hrtimer_forward);
c0a31329
TG
823
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 */
3c8aa39d 830static void enqueue_hrtimer(struct hrtimer *timer,
54cdfdb4 831 struct hrtimer_clock_base *base, int reprogram)
c0a31329
TG
832{
833 struct rb_node **link = &base->active.rb_node;
c0a31329
TG
834 struct rb_node *parent = NULL;
835 struct hrtimer *entry;
99bc2fcb 836 int leftmost = 1;
c0a31329 837
237fc6e7
TG
838 debug_hrtimer_activate(timer);
839
c0a31329
TG
840 /*
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 */
cc584b21
AV
850 if (hrtimer_get_expires_tv64(timer) <
851 hrtimer_get_expires_tv64(entry)) {
c0a31329 852 link = &(*link)->rb_left;
99bc2fcb 853 } else {
c0a31329 854 link = &(*link)->rb_right;
99bc2fcb
IM
855 leftmost = 0;
856 }
c0a31329
TG
857 }
858
859 /*
288867ec
TG
860 * Insert the timer to the rbtree and check whether it
861 * replaces the first pending timer
c0a31329 862 */
99bc2fcb 863 if (leftmost) {
54cdfdb4
TG
864 /*
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
c0a31329
TG
878 rb_link_node(&timer->node, parent, link);
879 rb_insert_color(&timer->node, &base->active);
303e967f
TG
880 /*
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;
288867ec 885}
c0a31329
TG
886
887/*
888 * __remove_hrtimer - internal function to remove a timer
889 *
890 * Caller must hold the base lock.
54cdfdb4
TG
891 *
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)
c0a31329 896 */
3c8aa39d 897static void __remove_hrtimer(struct hrtimer *timer,
303e967f 898 struct hrtimer_clock_base *base,
54cdfdb4 899 unsigned long newstate, int reprogram)
c0a31329 900{
54cdfdb4
TG
901 /* 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 }
303e967f 917 timer->state = newstate;
c0a31329
TG
918}
919
920/*
921 * remove hrtimer, called with base lock held
922 */
923static inline int
3c8aa39d 924remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
c0a31329 925{
303e967f 926 if (hrtimer_is_queued(timer)) {
54cdfdb4
TG
927 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 */
237fc6e7 937 debug_hrtimer_deactivate(timer);
82f67cd9 938 timer_stats_hrtimer_clear_start_info(timer);
54cdfdb4
TG
939 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
940 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
941 reprogram);
c0a31329
TG
942 return 1;
943 }
944 return 0;
945}
946
947/**
948 * hrtimer_start - (re)start an relative timer on the current CPU
c0a31329
TG
949 * @timer: the timer to be added
950 * @tim: expiry time
951 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
952 *
953 * Returns:
954 * 0 on success
955 * 1 when the timer was active
956 */
957int
958hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
959{
3c8aa39d 960 struct hrtimer_clock_base *base, *new_base;
c0a31329 961 unsigned long flags;
0c96c597 962 int ret, raise;
c0a31329
TG
963
964 base = lock_hrtimer_base(timer, &flags);
965
966 /* Remove an active timer from the queue: */
967 ret = remove_hrtimer(timer, base);
968
969 /* Switch the timer base, if necessary: */
970 new_base = switch_hrtimer_base(timer, base);
971
c9cb2e3d 972 if (mode == HRTIMER_MODE_REL) {
5a7780e7 973 tim = ktime_add_safe(tim, new_base->get_time());
06027bdd
IM
974 /*
975 * CONFIG_TIME_LOW_RES is a temporary way for architectures
976 * to signal that they simply return xtime in
977 * do_gettimeoffset(). In this case we want to round up by
978 * resolution when starting a relative timer, to avoid short
979 * timeouts. This will go away with the GTOD framework.
980 */
981#ifdef CONFIG_TIME_LOW_RES
5a7780e7 982 tim = ktime_add_safe(tim, base->resolution);
06027bdd
IM
983#endif
984 }
237fc6e7 985
cc584b21 986 hrtimer_set_expires(timer, tim);
c0a31329 987
82f67cd9
IM
988 timer_stats_hrtimer_set_start_info(timer);
989
935c631d
IM
990 /*
991 * Only allow reprogramming if the new base is on this CPU.
992 * (it might still be on another CPU if the timer was pending)
993 */
994 enqueue_hrtimer(timer, new_base,
995 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
c0a31329 996
0c96c597
TG
997 /*
998 * The timer may be expired and moved to the cb_pending
999 * list. We can not raise the softirq with base lock held due
1000 * to a possible deadlock with runqueue lock.
1001 */
1002 raise = timer->state == HRTIMER_STATE_PENDING;
1003
ee3ece83
SR
1004 /*
1005 * We use preempt_disable to prevent this task from migrating after
1006 * setting up the softirq and raising it. Otherwise, if me migrate
1007 * we will raise the softirq on the wrong CPU.
1008 */
1009 preempt_disable();
1010
c0a31329
TG
1011 unlock_hrtimer_base(timer, &flags);
1012
0c96c597
TG
1013 if (raise)
1014 hrtimer_raise_softirq();
ee3ece83 1015 preempt_enable();
0c96c597 1016
c0a31329
TG
1017 return ret;
1018}
8d16b764 1019EXPORT_SYMBOL_GPL(hrtimer_start);
c0a31329
TG
1020
1021/**
1022 * hrtimer_try_to_cancel - try to deactivate a timer
c0a31329
TG
1023 * @timer: hrtimer to stop
1024 *
1025 * Returns:
1026 * 0 when the timer was not active
1027 * 1 when the timer was active
1028 * -1 when the timer is currently excuting the callback function and
fa9799e3 1029 * cannot be stopped
c0a31329
TG
1030 */
1031int hrtimer_try_to_cancel(struct hrtimer *timer)
1032{
3c8aa39d 1033 struct hrtimer_clock_base *base;
c0a31329
TG
1034 unsigned long flags;
1035 int ret = -1;
1036
1037 base = lock_hrtimer_base(timer, &flags);
1038
303e967f 1039 if (!hrtimer_callback_running(timer))
c0a31329
TG
1040 ret = remove_hrtimer(timer, base);
1041
1042 unlock_hrtimer_base(timer, &flags);
1043
1044 return ret;
1045
1046}
8d16b764 1047EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
c0a31329
TG
1048
1049/**
1050 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
c0a31329
TG
1051 * @timer: the timer to be cancelled
1052 *
1053 * Returns:
1054 * 0 when the timer was not active
1055 * 1 when the timer was active
1056 */
1057int hrtimer_cancel(struct hrtimer *timer)
1058{
1059 for (;;) {
1060 int ret = hrtimer_try_to_cancel(timer);
1061
1062 if (ret >= 0)
1063 return ret;
5ef37b19 1064 cpu_relax();
c0a31329
TG
1065 }
1066}
8d16b764 1067EXPORT_SYMBOL_GPL(hrtimer_cancel);
c0a31329
TG
1068
1069/**
1070 * hrtimer_get_remaining - get remaining time for the timer
c0a31329
TG
1071 * @timer: the timer to read
1072 */
1073ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1074{
3c8aa39d 1075 struct hrtimer_clock_base *base;
c0a31329
TG
1076 unsigned long flags;
1077 ktime_t rem;
1078
1079 base = lock_hrtimer_base(timer, &flags);
cc584b21 1080 rem = hrtimer_expires_remaining(timer);
c0a31329
TG
1081 unlock_hrtimer_base(timer, &flags);
1082
1083 return rem;
1084}
8d16b764 1085EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
c0a31329 1086
ee9c5785 1087#ifdef CONFIG_NO_HZ
69239749
TL
1088/**
1089 * hrtimer_get_next_event - get the time until next expiry event
1090 *
1091 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1092 * is pending.
1093 */
1094ktime_t hrtimer_get_next_event(void)
1095{
3c8aa39d
TG
1096 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1097 struct hrtimer_clock_base *base = cpu_base->clock_base;
69239749
TL
1098 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1099 unsigned long flags;
1100 int i;
1101
3c8aa39d
TG
1102 spin_lock_irqsave(&cpu_base->lock, flags);
1103
54cdfdb4
TG
1104 if (!hrtimer_hres_active()) {
1105 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1106 struct hrtimer *timer;
69239749 1107
54cdfdb4
TG
1108 if (!base->first)
1109 continue;
3c8aa39d 1110
54cdfdb4 1111 timer = rb_entry(base->first, struct hrtimer, node);
cc584b21 1112 delta.tv64 = hrtimer_get_expires_tv64(timer);
54cdfdb4
TG
1113 delta = ktime_sub(delta, base->get_time());
1114 if (delta.tv64 < mindelta.tv64)
1115 mindelta.tv64 = delta.tv64;
1116 }
69239749 1117 }
3c8aa39d
TG
1118
1119 spin_unlock_irqrestore(&cpu_base->lock, flags);
1120
69239749
TL
1121 if (mindelta.tv64 < 0)
1122 mindelta.tv64 = 0;
1123 return mindelta;
1124}
1125#endif
1126
237fc6e7
TG
1127static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1128 enum hrtimer_mode mode)
c0a31329 1129{
3c8aa39d 1130 struct hrtimer_cpu_base *cpu_base;
c0a31329 1131
7978672c
GA
1132 memset(timer, 0, sizeof(struct hrtimer));
1133
3c8aa39d 1134 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
c0a31329 1135
c9cb2e3d 1136 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
7978672c
GA
1137 clock_id = CLOCK_MONOTONIC;
1138
3c8aa39d 1139 timer->base = &cpu_base->clock_base[clock_id];
d3d74453 1140 INIT_LIST_HEAD(&timer->cb_entry);
54cdfdb4 1141 hrtimer_init_timer_hres(timer);
82f67cd9
IM
1142
1143#ifdef CONFIG_TIMER_STATS
1144 timer->start_site = NULL;
1145 timer->start_pid = -1;
1146 memset(timer->start_comm, 0, TASK_COMM_LEN);
1147#endif
c0a31329 1148}
237fc6e7
TG
1149
1150/**
1151 * hrtimer_init - initialize a timer to the given clock
1152 * @timer: the timer to be initialized
1153 * @clock_id: the clock to be used
1154 * @mode: timer mode abs/rel
1155 */
1156void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1157 enum hrtimer_mode mode)
1158{
1159 debug_hrtimer_init(timer);
1160 __hrtimer_init(timer, clock_id, mode);
1161}
8d16b764 1162EXPORT_SYMBOL_GPL(hrtimer_init);
c0a31329
TG
1163
1164/**
1165 * hrtimer_get_res - get the timer resolution for a clock
c0a31329
TG
1166 * @which_clock: which clock to query
1167 * @tp: pointer to timespec variable to store the resolution
1168 *
72fd4a35
RD
1169 * Store the resolution of the clock selected by @which_clock in the
1170 * variable pointed to by @tp.
c0a31329
TG
1171 */
1172int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1173{
3c8aa39d 1174 struct hrtimer_cpu_base *cpu_base;
c0a31329 1175
3c8aa39d
TG
1176 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1177 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
c0a31329
TG
1178
1179 return 0;
1180}
8d16b764 1181EXPORT_SYMBOL_GPL(hrtimer_get_res);
c0a31329 1182
d3d74453
PZ
1183static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
1184{
1185 spin_lock_irq(&cpu_base->lock);
1186
1187 while (!list_empty(&cpu_base->cb_pending)) {
1188 enum hrtimer_restart (*fn)(struct hrtimer *);
1189 struct hrtimer *timer;
1190 int restart;
1191
1192 timer = list_entry(cpu_base->cb_pending.next,
1193 struct hrtimer, cb_entry);
1194
237fc6e7 1195 debug_hrtimer_deactivate(timer);
d3d74453
PZ
1196 timer_stats_account_hrtimer(timer);
1197
1198 fn = timer->function;
1199 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
1200 spin_unlock_irq(&cpu_base->lock);
1201
1202 restart = fn(timer);
1203
1204 spin_lock_irq(&cpu_base->lock);
1205
1206 timer->state &= ~HRTIMER_STATE_CALLBACK;
1207 if (restart == HRTIMER_RESTART) {
1208 BUG_ON(hrtimer_active(timer));
1209 /*
1210 * Enqueue the timer, allow reprogramming of the event
1211 * device
1212 */
1213 enqueue_hrtimer(timer, timer->base, 1);
1214 } else if (hrtimer_active(timer)) {
1215 /*
1216 * If the timer was rearmed on another CPU, reprogram
1217 * the event device.
1218 */
d7b41a24
BS
1219 struct hrtimer_clock_base *base = timer->base;
1220
1221 if (base->first == &timer->node &&
1222 hrtimer_reprogram(timer, base)) {
1223 /*
1224 * Timer is expired. Thus move it from tree to
1225 * pending list again.
1226 */
1227 __remove_hrtimer(timer, base,
1228 HRTIMER_STATE_PENDING, 0);
1229 list_add_tail(&timer->cb_entry,
1230 &base->cpu_base->cb_pending);
1231 }
d3d74453
PZ
1232 }
1233 }
1234 spin_unlock_irq(&cpu_base->lock);
1235}
1236
1237static void __run_hrtimer(struct hrtimer *timer)
1238{
1239 struct hrtimer_clock_base *base = timer->base;
1240 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1241 enum hrtimer_restart (*fn)(struct hrtimer *);
1242 int restart;
1243
237fc6e7 1244 debug_hrtimer_deactivate(timer);
d3d74453
PZ
1245 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1246 timer_stats_account_hrtimer(timer);
1247
1248 fn = timer->function;
1249 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
1250 /*
1251 * Used for scheduler timers, avoid lock inversion with
1252 * rq->lock and tasklist_lock.
1253 *
1254 * These timers are required to deal with enqueue expiry
1255 * themselves and are not allowed to migrate.
1256 */
1257 spin_unlock(&cpu_base->lock);
1258 restart = fn(timer);
1259 spin_lock(&cpu_base->lock);
1260 } else
1261 restart = fn(timer);
1262
1263 /*
1264 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1265 * reprogramming of the event hardware. This happens at the end of this
1266 * function anyway.
1267 */
1268 if (restart != HRTIMER_NORESTART) {
1269 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1270 enqueue_hrtimer(timer, base, 0);
1271 }
1272 timer->state &= ~HRTIMER_STATE_CALLBACK;
1273}
1274
54cdfdb4
TG
1275#ifdef CONFIG_HIGH_RES_TIMERS
1276
1277/*
1278 * High resolution timer interrupt
1279 * Called with interrupts disabled
1280 */
1281void hrtimer_interrupt(struct clock_event_device *dev)
1282{
1283 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1284 struct hrtimer_clock_base *base;
1285 ktime_t expires_next, now;
1286 int i, raise = 0;
1287
1288 BUG_ON(!cpu_base->hres_active);
1289 cpu_base->nr_events++;
1290 dev->next_event.tv64 = KTIME_MAX;
1291
1292 retry:
1293 now = ktime_get();
1294
1295 expires_next.tv64 = KTIME_MAX;
1296
1297 base = cpu_base->clock_base;
1298
1299 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1300 ktime_t basenow;
1301 struct rb_node *node;
1302
1303 spin_lock(&cpu_base->lock);
1304
1305 basenow = ktime_add(now, base->offset);
1306
1307 while ((node = base->first)) {
1308 struct hrtimer *timer;
1309
1310 timer = rb_entry(node, struct hrtimer, node);
1311
654c8e0b
AV
1312 /*
1313 * The immediate goal for using the softexpires is
1314 * minimizing wakeups, not running timers at the
1315 * earliest interrupt after their soft expiration.
1316 * This allows us to avoid using a Priority Search
1317 * Tree, which can answer a stabbing querry for
1318 * overlapping intervals and instead use the simple
1319 * BST we already have.
1320 * We don't add extra wakeups by delaying timers that
1321 * are right-of a not yet expired timer, because that
1322 * timer will have to trigger a wakeup anyway.
1323 */
1324
1325 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
54cdfdb4
TG
1326 ktime_t expires;
1327
cc584b21 1328 expires = ktime_sub(hrtimer_get_expires(timer),
54cdfdb4
TG
1329 base->offset);
1330 if (expires.tv64 < expires_next.tv64)
1331 expires_next = expires;
1332 break;
1333 }
1334
1335 /* Move softirq callbacks to the pending list */
1336 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1337 __remove_hrtimer(timer, base,
1338 HRTIMER_STATE_PENDING, 0);
1339 list_add_tail(&timer->cb_entry,
1340 &base->cpu_base->cb_pending);
1341 raise = 1;
1342 continue;
1343 }
1344
d3d74453 1345 __run_hrtimer(timer);
54cdfdb4
TG
1346 }
1347 spin_unlock(&cpu_base->lock);
1348 base++;
1349 }
1350
1351 cpu_base->expires_next = expires_next;
1352
1353 /* Reprogramming necessary ? */
1354 if (expires_next.tv64 != KTIME_MAX) {
1355 if (tick_program_event(expires_next, 0))
1356 goto retry;
1357 }
1358
1359 /* Raise softirq ? */
1360 if (raise)
1361 raise_softirq(HRTIMER_SOFTIRQ);
1362}
1363
1364static void run_hrtimer_softirq(struct softirq_action *h)
1365{
d3d74453
PZ
1366 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
1367}
54cdfdb4 1368
d3d74453 1369#endif /* CONFIG_HIGH_RES_TIMERS */
82f67cd9 1370
d3d74453
PZ
1371/*
1372 * Called from timer softirq every jiffy, expire hrtimers:
1373 *
1374 * For HRT its the fall back code to run the softirq in the timer
1375 * softirq context in case the hrtimer initialization failed or has
1376 * not been done yet.
1377 */
1378void hrtimer_run_pending(void)
1379{
1380 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
54cdfdb4 1381
d3d74453
PZ
1382 if (hrtimer_hres_active())
1383 return;
54cdfdb4 1384
d3d74453
PZ
1385 /*
1386 * This _is_ ugly: We have to check in the softirq context,
1387 * whether we can switch to highres and / or nohz mode. The
1388 * clocksource switch happens in the timer interrupt with
1389 * xtime_lock held. Notification from there only sets the
1390 * check bit in the tick_oneshot code, otherwise we might
1391 * deadlock vs. xtime_lock.
1392 */
1393 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1394 hrtimer_switch_to_hres();
54cdfdb4 1395
d3d74453 1396 run_hrtimer_pending(cpu_base);
54cdfdb4
TG
1397}
1398
c0a31329 1399/*
d3d74453 1400 * Called from hardirq context every jiffy
c0a31329 1401 */
833883d9 1402void hrtimer_run_queues(void)
c0a31329 1403{
288867ec 1404 struct rb_node *node;
833883d9
DS
1405 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1406 struct hrtimer_clock_base *base;
1407 int index, gettime = 1;
c0a31329 1408
833883d9 1409 if (hrtimer_hres_active())
3055adda
DS
1410 return;
1411
833883d9
DS
1412 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1413 base = &cpu_base->clock_base[index];
c0a31329 1414
833883d9 1415 if (!base->first)
d3d74453 1416 continue;
833883d9 1417
259aae86
TG
1418 if (base->get_softirq_time)
1419 base->softirq_time = base->get_softirq_time();
1420 else if (gettime) {
833883d9
DS
1421 hrtimer_get_softirq_time(cpu_base);
1422 gettime = 0;
b75f7a51 1423 }
d3d74453 1424
833883d9 1425 spin_lock(&cpu_base->lock);
c0a31329 1426
833883d9
DS
1427 while ((node = base->first)) {
1428 struct hrtimer *timer;
54cdfdb4 1429
833883d9 1430 timer = rb_entry(node, struct hrtimer, node);
cc584b21
AV
1431 if (base->softirq_time.tv64 <=
1432 hrtimer_get_expires_tv64(timer))
833883d9
DS
1433 break;
1434
1435 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1436 __remove_hrtimer(timer, base,
1437 HRTIMER_STATE_PENDING, 0);
1438 list_add_tail(&timer->cb_entry,
1439 &base->cpu_base->cb_pending);
1440 continue;
1441 }
92127c7a 1442
833883d9
DS
1443 __run_hrtimer(timer);
1444 }
1445 spin_unlock(&cpu_base->lock);
1446 }
c0a31329
TG
1447}
1448
10c94ec1
TG
1449/*
1450 * Sleep related functions:
1451 */
c9cb2e3d 1452static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
00362e33
TG
1453{
1454 struct hrtimer_sleeper *t =
1455 container_of(timer, struct hrtimer_sleeper, timer);
1456 struct task_struct *task = t->task;
1457
1458 t->task = NULL;
1459 if (task)
1460 wake_up_process(task);
1461
1462 return HRTIMER_NORESTART;
1463}
1464
36c8b586 1465void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
00362e33
TG
1466{
1467 sl->timer.function = hrtimer_wakeup;
1468 sl->task = task;
54cdfdb4 1469#ifdef CONFIG_HIGH_RES_TIMERS
37bb6cb4 1470 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
54cdfdb4 1471#endif
00362e33
TG
1472}
1473
669d7868 1474static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
432569bb 1475{
669d7868 1476 hrtimer_init_sleeper(t, current);
10c94ec1 1477
432569bb
RZ
1478 do {
1479 set_current_state(TASK_INTERRUPTIBLE);
cc584b21 1480 hrtimer_start_expires(&t->timer, mode);
37bb6cb4
PZ
1481 if (!hrtimer_active(&t->timer))
1482 t->task = NULL;
432569bb 1483
54cdfdb4
TG
1484 if (likely(t->task))
1485 schedule();
432569bb 1486
669d7868 1487 hrtimer_cancel(&t->timer);
c9cb2e3d 1488 mode = HRTIMER_MODE_ABS;
669d7868
TG
1489
1490 } while (t->task && !signal_pending(current));
432569bb 1491
3588a085
PZ
1492 __set_current_state(TASK_RUNNING);
1493
669d7868 1494 return t->task == NULL;
10c94ec1
TG
1495}
1496
080344b9
ON
1497static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1498{
1499 struct timespec rmt;
1500 ktime_t rem;
1501
cc584b21 1502 rem = hrtimer_expires_remaining(timer);
080344b9
ON
1503 if (rem.tv64 <= 0)
1504 return 0;
1505 rmt = ktime_to_timespec(rem);
1506
1507 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1508 return -EFAULT;
1509
1510 return 1;
1511}
1512
1711ef38 1513long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
10c94ec1 1514{
669d7868 1515 struct hrtimer_sleeper t;
080344b9 1516 struct timespec __user *rmtp;
237fc6e7 1517 int ret = 0;
10c94ec1 1518
237fc6e7
TG
1519 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1520 HRTIMER_MODE_ABS);
cc584b21 1521 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
10c94ec1 1522
c9cb2e3d 1523 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
237fc6e7 1524 goto out;
10c94ec1 1525
029a07e0 1526 rmtp = restart->nanosleep.rmtp;
432569bb 1527 if (rmtp) {
237fc6e7 1528 ret = update_rmtp(&t.timer, rmtp);
080344b9 1529 if (ret <= 0)
237fc6e7 1530 goto out;
432569bb 1531 }
10c94ec1 1532
10c94ec1 1533 /* The other values in restart are already filled in */
237fc6e7
TG
1534 ret = -ERESTART_RESTARTBLOCK;
1535out:
1536 destroy_hrtimer_on_stack(&t.timer);
1537 return ret;
10c94ec1
TG
1538}
1539
080344b9 1540long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
10c94ec1
TG
1541 const enum hrtimer_mode mode, const clockid_t clockid)
1542{
1543 struct restart_block *restart;
669d7868 1544 struct hrtimer_sleeper t;
237fc6e7 1545 int ret = 0;
10c94ec1 1546
237fc6e7 1547 hrtimer_init_on_stack(&t.timer, clockid, mode);
cc584b21 1548 hrtimer_set_expires(&t.timer, timespec_to_ktime(*rqtp));
432569bb 1549 if (do_nanosleep(&t, mode))
237fc6e7 1550 goto out;
10c94ec1 1551
7978672c 1552 /* Absolute timers do not update the rmtp value and restart: */
237fc6e7
TG
1553 if (mode == HRTIMER_MODE_ABS) {
1554 ret = -ERESTARTNOHAND;
1555 goto out;
1556 }
10c94ec1 1557
432569bb 1558 if (rmtp) {
237fc6e7 1559 ret = update_rmtp(&t.timer, rmtp);
080344b9 1560 if (ret <= 0)
237fc6e7 1561 goto out;
432569bb 1562 }
10c94ec1
TG
1563
1564 restart = &current_thread_info()->restart_block;
1711ef38 1565 restart->fn = hrtimer_nanosleep_restart;
029a07e0
TG
1566 restart->nanosleep.index = t.timer.base->index;
1567 restart->nanosleep.rmtp = rmtp;
cc584b21 1568 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
10c94ec1 1569
237fc6e7
TG
1570 ret = -ERESTART_RESTARTBLOCK;
1571out:
1572 destroy_hrtimer_on_stack(&t.timer);
1573 return ret;
10c94ec1
TG
1574}
1575
6ba1b912
TG
1576asmlinkage long
1577sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1578{
080344b9 1579 struct timespec tu;
6ba1b912
TG
1580
1581 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1582 return -EFAULT;
1583
1584 if (!timespec_valid(&tu))
1585 return -EINVAL;
1586
080344b9 1587 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
6ba1b912
TG
1588}
1589
c0a31329
TG
1590/*
1591 * Functions related to boot-time initialization:
1592 */
0ec160dd 1593static void __cpuinit init_hrtimers_cpu(int cpu)
c0a31329 1594{
3c8aa39d 1595 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
c0a31329
TG
1596 int i;
1597
3c8aa39d 1598 spin_lock_init(&cpu_base->lock);
3c8aa39d
TG
1599
1600 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1601 cpu_base->clock_base[i].cpu_base = cpu_base;
1602
d3d74453 1603 INIT_LIST_HEAD(&cpu_base->cb_pending);
54cdfdb4 1604 hrtimer_init_hres(cpu_base);
c0a31329
TG
1605}
1606
1607#ifdef CONFIG_HOTPLUG_CPU
1608
3c8aa39d
TG
1609static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1610 struct hrtimer_clock_base *new_base)
c0a31329
TG
1611{
1612 struct hrtimer *timer;
1613 struct rb_node *node;
1614
1615 while ((node = rb_first(&old_base->active))) {
1616 timer = rb_entry(node, struct hrtimer, node);
54cdfdb4 1617 BUG_ON(hrtimer_callback_running(timer));
237fc6e7 1618 debug_hrtimer_deactivate(timer);
54cdfdb4 1619 __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0);
c0a31329 1620 timer->base = new_base;
54cdfdb4
TG
1621 /*
1622 * Enqueue the timer. Allow reprogramming of the event device
1623 */
1624 enqueue_hrtimer(timer, new_base, 1);
c0a31329
TG
1625 }
1626}
1627
1628static void migrate_hrtimers(int cpu)
1629{
3c8aa39d 1630 struct hrtimer_cpu_base *old_base, *new_base;
c0a31329
TG
1631 int i;
1632
1633 BUG_ON(cpu_online(cpu));
3c8aa39d
TG
1634 old_base = &per_cpu(hrtimer_bases, cpu);
1635 new_base = &get_cpu_var(hrtimer_bases);
c0a31329 1636
54cdfdb4
TG
1637 tick_cancel_sched_timer(cpu);
1638
c0a31329 1639 local_irq_disable();
8e60e05f
ON
1640 spin_lock(&new_base->lock);
1641 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
c0a31329 1642
3c8aa39d 1643 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
3c8aa39d
TG
1644 migrate_hrtimer_list(&old_base->clock_base[i],
1645 &new_base->clock_base[i]);
c0a31329
TG
1646 }
1647
8e60e05f
ON
1648 spin_unlock(&old_base->lock);
1649 spin_unlock(&new_base->lock);
c0a31329
TG
1650 local_irq_enable();
1651 put_cpu_var(hrtimer_bases);
1652}
1653#endif /* CONFIG_HOTPLUG_CPU */
1654
8c78f307 1655static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
c0a31329
TG
1656 unsigned long action, void *hcpu)
1657{
7713a7d1 1658 unsigned int cpu = (long)hcpu;
c0a31329
TG
1659
1660 switch (action) {
1661
1662 case CPU_UP_PREPARE:
8bb78442 1663 case CPU_UP_PREPARE_FROZEN:
c0a31329
TG
1664 init_hrtimers_cpu(cpu);
1665 break;
1666
1667#ifdef CONFIG_HOTPLUG_CPU
1668 case CPU_DEAD:
8bb78442 1669 case CPU_DEAD_FROZEN:
d316c57f 1670 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
c0a31329
TG
1671 migrate_hrtimers(cpu);
1672 break;
1673#endif
1674
1675 default:
1676 break;
1677 }
1678
1679 return NOTIFY_OK;
1680}
1681
8c78f307 1682static struct notifier_block __cpuinitdata hrtimers_nb = {
c0a31329
TG
1683 .notifier_call = hrtimer_cpu_notify,
1684};
1685
1686void __init hrtimers_init(void)
1687{
1688 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1689 (void *)(long)smp_processor_id());
1690 register_cpu_notifier(&hrtimers_nb);
54cdfdb4 1691#ifdef CONFIG_HIGH_RES_TIMERS
962cf36c 1692 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
54cdfdb4 1693#endif
c0a31329
TG
1694}
1695
7bb67439 1696/**
654c8e0b 1697 * schedule_hrtimeout_range - sleep until timeout
7bb67439 1698 * @expires: timeout value (ktime_t)
654c8e0b 1699 * @delta: slack in expires timeout (ktime_t)
7bb67439
AV
1700 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1701 *
1702 * Make the current task sleep until the given expiry time has
1703 * elapsed. The routine will return immediately unless
1704 * the current task state has been set (see set_current_state()).
1705 *
654c8e0b
AV
1706 * The @delta argument gives the kernel the freedom to schedule the
1707 * actual wakeup to a time that is both power and performance friendly.
1708 * The kernel give the normal best effort behavior for "@expires+@delta",
1709 * but may decide to fire the timer earlier, but no earlier than @expires.
1710 *
7bb67439
AV
1711 * You can set the task state as follows -
1712 *
1713 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1714 * pass before the routine returns.
1715 *
1716 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1717 * delivered to the current task.
1718 *
1719 * The current task state is guaranteed to be TASK_RUNNING when this
1720 * routine returns.
1721 *
1722 * Returns 0 when the timer has expired otherwise -EINTR
1723 */
654c8e0b 1724int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
7bb67439
AV
1725 const enum hrtimer_mode mode)
1726{
1727 struct hrtimer_sleeper t;
1728
1729 /*
1730 * Optimize when a zero timeout value is given. It does not
1731 * matter whether this is an absolute or a relative time.
1732 */
1733 if (expires && !expires->tv64) {
1734 __set_current_state(TASK_RUNNING);
1735 return 0;
1736 }
1737
1738 /*
1739 * A NULL parameter means "inifinte"
1740 */
1741 if (!expires) {
1742 schedule();
1743 __set_current_state(TASK_RUNNING);
1744 return -EINTR;
1745 }
1746
1747 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
654c8e0b 1748 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
7bb67439
AV
1749
1750 hrtimer_init_sleeper(&t, current);
1751
cc584b21 1752 hrtimer_start_expires(&t.timer, mode);
7bb67439
AV
1753 if (!hrtimer_active(&t.timer))
1754 t.task = NULL;
1755
1756 if (likely(t.task))
1757 schedule();
1758
1759 hrtimer_cancel(&t.timer);
1760 destroy_hrtimer_on_stack(&t.timer);
1761
1762 __set_current_state(TASK_RUNNING);
1763
1764 return !t.task ? 0 : -EINTR;
1765}
654c8e0b
AV
1766EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1767
1768/**
1769 * schedule_hrtimeout - sleep until timeout
1770 * @expires: timeout value (ktime_t)
1771 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1772 *
1773 * Make the current task sleep until the given expiry time has
1774 * elapsed. The routine will return immediately unless
1775 * the current task state has been set (see set_current_state()).
1776 *
1777 * You can set the task state as follows -
1778 *
1779 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1780 * pass before the routine returns.
1781 *
1782 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1783 * delivered to the current task.
1784 *
1785 * The current task state is guaranteed to be TASK_RUNNING when this
1786 * routine returns.
1787 *
1788 * Returns 0 when the timer has expired otherwise -EINTR
1789 */
1790int __sched schedule_hrtimeout(ktime_t *expires,
1791 const enum hrtimer_mode mode)
1792{
1793 return schedule_hrtimeout_range(expires, 0, mode);
1794}
7bb67439 1795EXPORT_SYMBOL_GPL(schedule_hrtimeout);