stop_machine: introduce stop_machine_create/destroy.
[deliverable/linux.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{
995f054f
IM
617 /* Retrigger the CPU local events: */
618 retrigger_next_event(NULL);
619}
620
54cdfdb4
TG
621/*
622 * Initialize the high resolution related parts of cpu_base
623 */
624static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
625{
626 base->expires_next.tv64 = KTIME_MAX;
627 base->hres_active = 0;
54cdfdb4
TG
628}
629
630/*
631 * Initialize the high resolution related parts of a hrtimer
632 */
633static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
634{
54cdfdb4
TG
635}
636
ca109491
PZ
637static void __run_hrtimer(struct hrtimer *timer);
638
54cdfdb4
TG
639/*
640 * When High resolution timers are active, try to reprogram. Note, that in case
641 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
642 * check happens. The timer gets enqueued into the rbtree. The reprogramming
643 * and expiry check is done in the hrtimer_interrupt or in the softirq.
644 */
645static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
646 struct hrtimer_clock_base *base)
647{
648 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
ca109491
PZ
649 /*
650 * XXX: recursion check?
651 * hrtimer_forward() should round up with timer granularity
652 * so that we never get into inf recursion here,
653 * it doesn't do that though
654 */
655 __run_hrtimer(timer);
656 return 1;
54cdfdb4
TG
657 }
658 return 0;
659}
660
661/*
662 * Switch to high resolution mode
663 */
f8953856 664static int hrtimer_switch_to_hres(void)
54cdfdb4 665{
820de5c3
IM
666 int cpu = smp_processor_id();
667 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
54cdfdb4
TG
668 unsigned long flags;
669
670 if (base->hres_active)
f8953856 671 return 1;
54cdfdb4
TG
672
673 local_irq_save(flags);
674
675 if (tick_init_highres()) {
676 local_irq_restore(flags);
820de5c3
IM
677 printk(KERN_WARNING "Could not switch to high resolution "
678 "mode on CPU %d\n", cpu);
f8953856 679 return 0;
54cdfdb4
TG
680 }
681 base->hres_active = 1;
682 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
683 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
684
685 tick_setup_sched_timer();
686
687 /* "Retrigger" the interrupt to get things going */
688 retrigger_next_event(NULL);
689 local_irq_restore(flags);
edfed66e 690 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
54cdfdb4 691 smp_processor_id());
f8953856 692 return 1;
54cdfdb4
TG
693}
694
695#else
696
697static inline int hrtimer_hres_active(void) { return 0; }
698static inline int hrtimer_is_hres_enabled(void) { return 0; }
f8953856 699static inline int hrtimer_switch_to_hres(void) { return 0; }
54cdfdb4
TG
700static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
701static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
702 struct hrtimer_clock_base *base)
703{
704 return 0;
705}
54cdfdb4
TG
706static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
707static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
d3d74453
PZ
708static inline int hrtimer_reprogram(struct hrtimer *timer,
709 struct hrtimer_clock_base *base)
710{
711 return 0;
712}
54cdfdb4
TG
713
714#endif /* CONFIG_HIGH_RES_TIMERS */
715
82f67cd9
IM
716#ifdef CONFIG_TIMER_STATS
717void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
718{
719 if (timer->start_site)
720 return;
721
722 timer->start_site = addr;
723 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
724 timer->start_pid = current->pid;
725}
726#endif
727
c0a31329 728/*
6506f2aa 729 * Counterpart to lock_hrtimer_base above:
c0a31329
TG
730 */
731static inline
732void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
733{
3c8aa39d 734 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
c0a31329
TG
735}
736
737/**
738 * hrtimer_forward - forward the timer expiry
c0a31329 739 * @timer: hrtimer to forward
44f21475 740 * @now: forward past this time
c0a31329
TG
741 * @interval: the interval to forward
742 *
743 * Forward the timer expiry so it will expire in the future.
8dca6f33 744 * Returns the number of overruns.
c0a31329 745 */
4d672e7a 746u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
c0a31329 747{
4d672e7a 748 u64 orun = 1;
44f21475 749 ktime_t delta;
c0a31329 750
cc584b21 751 delta = ktime_sub(now, hrtimer_get_expires(timer));
c0a31329
TG
752
753 if (delta.tv64 < 0)
754 return 0;
755
c9db4fa1
TG
756 if (interval.tv64 < timer->base->resolution.tv64)
757 interval.tv64 = timer->base->resolution.tv64;
758
c0a31329 759 if (unlikely(delta.tv64 >= interval.tv64)) {
df869b63 760 s64 incr = ktime_to_ns(interval);
c0a31329
TG
761
762 orun = ktime_divns(delta, incr);
cc584b21
AV
763 hrtimer_add_expires_ns(timer, incr * orun);
764 if (hrtimer_get_expires_tv64(timer) > now.tv64)
c0a31329
TG
765 return orun;
766 /*
767 * This (and the ktime_add() below) is the
768 * correction for exact:
769 */
770 orun++;
771 }
cc584b21 772 hrtimer_add_expires(timer, interval);
c0a31329
TG
773
774 return orun;
775}
6bdb6b62 776EXPORT_SYMBOL_GPL(hrtimer_forward);
c0a31329
TG
777
778/*
779 * enqueue_hrtimer - internal function to (re)start a timer
780 *
781 * The timer is inserted in expiry order. Insertion into the
782 * red black tree is O(log(n)). Must hold the base lock.
783 */
3c8aa39d 784static void enqueue_hrtimer(struct hrtimer *timer,
54cdfdb4 785 struct hrtimer_clock_base *base, int reprogram)
c0a31329
TG
786{
787 struct rb_node **link = &base->active.rb_node;
c0a31329
TG
788 struct rb_node *parent = NULL;
789 struct hrtimer *entry;
99bc2fcb 790 int leftmost = 1;
c0a31329 791
237fc6e7
TG
792 debug_hrtimer_activate(timer);
793
c0a31329
TG
794 /*
795 * Find the right place in the rbtree:
796 */
797 while (*link) {
798 parent = *link;
799 entry = rb_entry(parent, struct hrtimer, node);
800 /*
801 * We dont care about collisions. Nodes with
802 * the same expiry time stay together.
803 */
cc584b21
AV
804 if (hrtimer_get_expires_tv64(timer) <
805 hrtimer_get_expires_tv64(entry)) {
c0a31329 806 link = &(*link)->rb_left;
99bc2fcb 807 } else {
c0a31329 808 link = &(*link)->rb_right;
99bc2fcb
IM
809 leftmost = 0;
810 }
c0a31329
TG
811 }
812
813 /*
288867ec
TG
814 * Insert the timer to the rbtree and check whether it
815 * replaces the first pending timer
c0a31329 816 */
99bc2fcb 817 if (leftmost) {
54cdfdb4
TG
818 /*
819 * Reprogram the clock event device. When the timer is already
820 * expired hrtimer_enqueue_reprogram has either called the
821 * callback or added it to the pending list and raised the
822 * softirq.
823 *
824 * This is a NOP for !HIGHRES
825 */
826 if (reprogram && hrtimer_enqueue_reprogram(timer, base))
827 return;
828
829 base->first = &timer->node;
830 }
831
c0a31329
TG
832 rb_link_node(&timer->node, parent, link);
833 rb_insert_color(&timer->node, &base->active);
303e967f
TG
834 /*
835 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
836 * state of a possibly running callback.
837 */
838 timer->state |= HRTIMER_STATE_ENQUEUED;
288867ec 839}
c0a31329
TG
840
841/*
842 * __remove_hrtimer - internal function to remove a timer
843 *
844 * Caller must hold the base lock.
54cdfdb4
TG
845 *
846 * High resolution timer mode reprograms the clock event device when the
847 * timer is the one which expires next. The caller can disable this by setting
848 * reprogram to zero. This is useful, when the context does a reprogramming
849 * anyway (e.g. timer interrupt)
c0a31329 850 */
3c8aa39d 851static void __remove_hrtimer(struct hrtimer *timer,
303e967f 852 struct hrtimer_clock_base *base,
54cdfdb4 853 unsigned long newstate, int reprogram)
c0a31329 854{
ca109491 855 if (timer->state & HRTIMER_STATE_ENQUEUED) {
54cdfdb4
TG
856 /*
857 * Remove the timer from the rbtree and replace the
858 * first entry pointer if necessary.
859 */
860 if (base->first == &timer->node) {
861 base->first = rb_next(&timer->node);
862 /* Reprogram the clock event device. if enabled */
863 if (reprogram && hrtimer_hres_active())
864 hrtimer_force_reprogram(base->cpu_base);
865 }
866 rb_erase(&timer->node, &base->active);
867 }
303e967f 868 timer->state = newstate;
c0a31329
TG
869}
870
871/*
872 * remove hrtimer, called with base lock held
873 */
874static inline int
3c8aa39d 875remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
c0a31329 876{
303e967f 877 if (hrtimer_is_queued(timer)) {
54cdfdb4
TG
878 int reprogram;
879
880 /*
881 * Remove the timer and force reprogramming when high
882 * resolution mode is active and the timer is on the current
883 * CPU. If we remove a timer on another CPU, reprogramming is
884 * skipped. The interrupt event on this CPU is fired and
885 * reprogramming happens in the interrupt handler. This is a
886 * rare case and less expensive than a smp call.
887 */
237fc6e7 888 debug_hrtimer_deactivate(timer);
82f67cd9 889 timer_stats_hrtimer_clear_start_info(timer);
54cdfdb4
TG
890 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
891 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
892 reprogram);
c0a31329
TG
893 return 1;
894 }
895 return 0;
896}
897
898/**
e1dd7bc5 899 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
c0a31329
TG
900 * @timer: the timer to be added
901 * @tim: expiry time
da8f2e17 902 * @delta_ns: "slack" range for the timer
c0a31329
TG
903 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
904 *
905 * Returns:
906 * 0 on success
907 * 1 when the timer was active
908 */
909int
da8f2e17
AV
910hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns,
911 const enum hrtimer_mode mode)
c0a31329 912{
3c8aa39d 913 struct hrtimer_clock_base *base, *new_base;
c0a31329 914 unsigned long flags;
ca109491 915 int ret;
c0a31329
TG
916
917 base = lock_hrtimer_base(timer, &flags);
918
919 /* Remove an active timer from the queue: */
920 ret = remove_hrtimer(timer, base);
921
922 /* Switch the timer base, if necessary: */
923 new_base = switch_hrtimer_base(timer, base);
924
c9cb2e3d 925 if (mode == HRTIMER_MODE_REL) {
5a7780e7 926 tim = ktime_add_safe(tim, new_base->get_time());
06027bdd
IM
927 /*
928 * CONFIG_TIME_LOW_RES is a temporary way for architectures
929 * to signal that they simply return xtime in
930 * do_gettimeoffset(). In this case we want to round up by
931 * resolution when starting a relative timer, to avoid short
932 * timeouts. This will go away with the GTOD framework.
933 */
934#ifdef CONFIG_TIME_LOW_RES
5a7780e7 935 tim = ktime_add_safe(tim, base->resolution);
06027bdd
IM
936#endif
937 }
237fc6e7 938
da8f2e17 939 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
c0a31329 940
82f67cd9
IM
941 timer_stats_hrtimer_set_start_info(timer);
942
935c631d
IM
943 /*
944 * Only allow reprogramming if the new base is on this CPU.
945 * (it might still be on another CPU if the timer was pending)
946 */
947 enqueue_hrtimer(timer, new_base,
948 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
c0a31329
TG
949
950 unlock_hrtimer_base(timer, &flags);
951
952 return ret;
953}
da8f2e17
AV
954EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
955
956/**
e1dd7bc5 957 * hrtimer_start - (re)start an hrtimer on the current CPU
da8f2e17
AV
958 * @timer: the timer to be added
959 * @tim: expiry time
960 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
961 *
962 * Returns:
963 * 0 on success
964 * 1 when the timer was active
965 */
966int
967hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
968{
969 return hrtimer_start_range_ns(timer, tim, 0, mode);
970}
8d16b764 971EXPORT_SYMBOL_GPL(hrtimer_start);
c0a31329 972
da8f2e17 973
c0a31329
TG
974/**
975 * hrtimer_try_to_cancel - try to deactivate a timer
c0a31329
TG
976 * @timer: hrtimer to stop
977 *
978 * Returns:
979 * 0 when the timer was not active
980 * 1 when the timer was active
981 * -1 when the timer is currently excuting the callback function and
fa9799e3 982 * cannot be stopped
c0a31329
TG
983 */
984int hrtimer_try_to_cancel(struct hrtimer *timer)
985{
3c8aa39d 986 struct hrtimer_clock_base *base;
c0a31329
TG
987 unsigned long flags;
988 int ret = -1;
989
990 base = lock_hrtimer_base(timer, &flags);
991
303e967f 992 if (!hrtimer_callback_running(timer))
c0a31329
TG
993 ret = remove_hrtimer(timer, base);
994
995 unlock_hrtimer_base(timer, &flags);
996
997 return ret;
998
999}
8d16b764 1000EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
c0a31329
TG
1001
1002/**
1003 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
c0a31329
TG
1004 * @timer: the timer to be cancelled
1005 *
1006 * Returns:
1007 * 0 when the timer was not active
1008 * 1 when the timer was active
1009 */
1010int hrtimer_cancel(struct hrtimer *timer)
1011{
1012 for (;;) {
1013 int ret = hrtimer_try_to_cancel(timer);
1014
1015 if (ret >= 0)
1016 return ret;
5ef37b19 1017 cpu_relax();
c0a31329
TG
1018 }
1019}
8d16b764 1020EXPORT_SYMBOL_GPL(hrtimer_cancel);
c0a31329
TG
1021
1022/**
1023 * hrtimer_get_remaining - get remaining time for the timer
c0a31329
TG
1024 * @timer: the timer to read
1025 */
1026ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1027{
3c8aa39d 1028 struct hrtimer_clock_base *base;
c0a31329
TG
1029 unsigned long flags;
1030 ktime_t rem;
1031
1032 base = lock_hrtimer_base(timer, &flags);
cc584b21 1033 rem = hrtimer_expires_remaining(timer);
c0a31329
TG
1034 unlock_hrtimer_base(timer, &flags);
1035
1036 return rem;
1037}
8d16b764 1038EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
c0a31329 1039
ee9c5785 1040#ifdef CONFIG_NO_HZ
69239749
TL
1041/**
1042 * hrtimer_get_next_event - get the time until next expiry event
1043 *
1044 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1045 * is pending.
1046 */
1047ktime_t hrtimer_get_next_event(void)
1048{
3c8aa39d
TG
1049 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1050 struct hrtimer_clock_base *base = cpu_base->clock_base;
69239749
TL
1051 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1052 unsigned long flags;
1053 int i;
1054
3c8aa39d
TG
1055 spin_lock_irqsave(&cpu_base->lock, flags);
1056
54cdfdb4
TG
1057 if (!hrtimer_hres_active()) {
1058 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1059 struct hrtimer *timer;
69239749 1060
54cdfdb4
TG
1061 if (!base->first)
1062 continue;
3c8aa39d 1063
54cdfdb4 1064 timer = rb_entry(base->first, struct hrtimer, node);
cc584b21 1065 delta.tv64 = hrtimer_get_expires_tv64(timer);
54cdfdb4
TG
1066 delta = ktime_sub(delta, base->get_time());
1067 if (delta.tv64 < mindelta.tv64)
1068 mindelta.tv64 = delta.tv64;
1069 }
69239749 1070 }
3c8aa39d
TG
1071
1072 spin_unlock_irqrestore(&cpu_base->lock, flags);
1073
69239749
TL
1074 if (mindelta.tv64 < 0)
1075 mindelta.tv64 = 0;
1076 return mindelta;
1077}
1078#endif
1079
237fc6e7
TG
1080static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1081 enum hrtimer_mode mode)
c0a31329 1082{
3c8aa39d 1083 struct hrtimer_cpu_base *cpu_base;
c0a31329 1084
7978672c
GA
1085 memset(timer, 0, sizeof(struct hrtimer));
1086
3c8aa39d 1087 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
c0a31329 1088
c9cb2e3d 1089 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
7978672c
GA
1090 clock_id = CLOCK_MONOTONIC;
1091
3c8aa39d 1092 timer->base = &cpu_base->clock_base[clock_id];
d3d74453 1093 INIT_LIST_HEAD(&timer->cb_entry);
54cdfdb4 1094 hrtimer_init_timer_hres(timer);
82f67cd9
IM
1095
1096#ifdef CONFIG_TIMER_STATS
1097 timer->start_site = NULL;
1098 timer->start_pid = -1;
1099 memset(timer->start_comm, 0, TASK_COMM_LEN);
1100#endif
c0a31329 1101}
237fc6e7
TG
1102
1103/**
1104 * hrtimer_init - initialize a timer to the given clock
1105 * @timer: the timer to be initialized
1106 * @clock_id: the clock to be used
1107 * @mode: timer mode abs/rel
1108 */
1109void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1110 enum hrtimer_mode mode)
1111{
1112 debug_hrtimer_init(timer);
1113 __hrtimer_init(timer, clock_id, mode);
1114}
8d16b764 1115EXPORT_SYMBOL_GPL(hrtimer_init);
c0a31329
TG
1116
1117/**
1118 * hrtimer_get_res - get the timer resolution for a clock
c0a31329
TG
1119 * @which_clock: which clock to query
1120 * @tp: pointer to timespec variable to store the resolution
1121 *
72fd4a35
RD
1122 * Store the resolution of the clock selected by @which_clock in the
1123 * variable pointed to by @tp.
c0a31329
TG
1124 */
1125int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1126{
3c8aa39d 1127 struct hrtimer_cpu_base *cpu_base;
c0a31329 1128
3c8aa39d
TG
1129 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1130 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
c0a31329
TG
1131
1132 return 0;
1133}
8d16b764 1134EXPORT_SYMBOL_GPL(hrtimer_get_res);
c0a31329 1135
d3d74453
PZ
1136static void __run_hrtimer(struct hrtimer *timer)
1137{
1138 struct hrtimer_clock_base *base = timer->base;
1139 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1140 enum hrtimer_restart (*fn)(struct hrtimer *);
1141 int restart;
1142
ca109491
PZ
1143 WARN_ON(!irqs_disabled());
1144
237fc6e7 1145 debug_hrtimer_deactivate(timer);
d3d74453
PZ
1146 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1147 timer_stats_account_hrtimer(timer);
d3d74453 1148 fn = timer->function;
ca109491
PZ
1149
1150 /*
1151 * Because we run timers from hardirq context, there is no chance
1152 * they get migrated to another cpu, therefore its safe to unlock
1153 * the timer base.
1154 */
1155 spin_unlock(&cpu_base->lock);
1156 restart = fn(timer);
1157 spin_lock(&cpu_base->lock);
d3d74453
PZ
1158
1159 /*
1160 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1161 * reprogramming of the event hardware. This happens at the end of this
1162 * function anyway.
1163 */
1164 if (restart != HRTIMER_NORESTART) {
1165 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1166 enqueue_hrtimer(timer, base, 0);
1167 }
1168 timer->state &= ~HRTIMER_STATE_CALLBACK;
1169}
1170
54cdfdb4
TG
1171#ifdef CONFIG_HIGH_RES_TIMERS
1172
1173/*
1174 * High resolution timer interrupt
1175 * Called with interrupts disabled
1176 */
1177void hrtimer_interrupt(struct clock_event_device *dev)
1178{
1179 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1180 struct hrtimer_clock_base *base;
1181 ktime_t expires_next, now;
ca109491 1182 int i;
54cdfdb4
TG
1183
1184 BUG_ON(!cpu_base->hres_active);
1185 cpu_base->nr_events++;
1186 dev->next_event.tv64 = KTIME_MAX;
1187
1188 retry:
1189 now = ktime_get();
1190
1191 expires_next.tv64 = KTIME_MAX;
1192
1193 base = cpu_base->clock_base;
1194
1195 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1196 ktime_t basenow;
1197 struct rb_node *node;
1198
1199 spin_lock(&cpu_base->lock);
1200
1201 basenow = ktime_add(now, base->offset);
1202
1203 while ((node = base->first)) {
1204 struct hrtimer *timer;
1205
1206 timer = rb_entry(node, struct hrtimer, node);
1207
654c8e0b
AV
1208 /*
1209 * The immediate goal for using the softexpires is
1210 * minimizing wakeups, not running timers at the
1211 * earliest interrupt after their soft expiration.
1212 * This allows us to avoid using a Priority Search
1213 * Tree, which can answer a stabbing querry for
1214 * overlapping intervals and instead use the simple
1215 * BST we already have.
1216 * We don't add extra wakeups by delaying timers that
1217 * are right-of a not yet expired timer, because that
1218 * timer will have to trigger a wakeup anyway.
1219 */
1220
1221 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
54cdfdb4
TG
1222 ktime_t expires;
1223
cc584b21 1224 expires = ktime_sub(hrtimer_get_expires(timer),
54cdfdb4
TG
1225 base->offset);
1226 if (expires.tv64 < expires_next.tv64)
1227 expires_next = expires;
1228 break;
1229 }
1230
d3d74453 1231 __run_hrtimer(timer);
54cdfdb4
TG
1232 }
1233 spin_unlock(&cpu_base->lock);
1234 base++;
1235 }
1236
1237 cpu_base->expires_next = expires_next;
1238
1239 /* Reprogramming necessary ? */
1240 if (expires_next.tv64 != KTIME_MAX) {
1241 if (tick_program_event(expires_next, 0))
1242 goto retry;
1243 }
54cdfdb4
TG
1244}
1245
2e94d1f7
AV
1246/**
1247 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1248 *
1249 * hrtimer_peek_ahead_timers will peek at the timer queue of
1250 * the current cpu and check if there are any timers for which
1251 * the soft expires time has passed. If any such timers exist,
1252 * they are run immediately and then removed from the timer queue.
1253 *
1254 */
1255void hrtimer_peek_ahead_timers(void)
1256{
2e94d1f7 1257 struct tick_device *td;
643bdf68 1258 unsigned long flags;
dc4304f7
AV
1259
1260 if (!hrtimer_hres_active())
2e94d1f7
AV
1261 return;
1262
1263 local_irq_save(flags);
1264 td = &__get_cpu_var(tick_cpu_device);
643bdf68
TG
1265 if (td && td->evtdev)
1266 hrtimer_interrupt(td->evtdev);
2e94d1f7
AV
1267 local_irq_restore(flags);
1268}
1269
d3d74453 1270#endif /* CONFIG_HIGH_RES_TIMERS */
82f67cd9 1271
d3d74453
PZ
1272/*
1273 * Called from timer softirq every jiffy, expire hrtimers:
1274 *
1275 * For HRT its the fall back code to run the softirq in the timer
1276 * softirq context in case the hrtimer initialization failed or has
1277 * not been done yet.
1278 */
1279void hrtimer_run_pending(void)
1280{
d3d74453
PZ
1281 if (hrtimer_hres_active())
1282 return;
54cdfdb4 1283
d3d74453
PZ
1284 /*
1285 * This _is_ ugly: We have to check in the softirq context,
1286 * whether we can switch to highres and / or nohz mode. The
1287 * clocksource switch happens in the timer interrupt with
1288 * xtime_lock held. Notification from there only sets the
1289 * check bit in the tick_oneshot code, otherwise we might
1290 * deadlock vs. xtime_lock.
1291 */
1292 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1293 hrtimer_switch_to_hres();
54cdfdb4
TG
1294}
1295
c0a31329 1296/*
d3d74453 1297 * Called from hardirq context every jiffy
c0a31329 1298 */
833883d9 1299void hrtimer_run_queues(void)
c0a31329 1300{
288867ec 1301 struct rb_node *node;
833883d9
DS
1302 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1303 struct hrtimer_clock_base *base;
1304 int index, gettime = 1;
c0a31329 1305
833883d9 1306 if (hrtimer_hres_active())
3055adda
DS
1307 return;
1308
833883d9
DS
1309 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1310 base = &cpu_base->clock_base[index];
c0a31329 1311
833883d9 1312 if (!base->first)
d3d74453 1313 continue;
833883d9 1314
d7cfb60c 1315 if (gettime) {
833883d9
DS
1316 hrtimer_get_softirq_time(cpu_base);
1317 gettime = 0;
b75f7a51 1318 }
d3d74453 1319
833883d9 1320 spin_lock(&cpu_base->lock);
c0a31329 1321
833883d9
DS
1322 while ((node = base->first)) {
1323 struct hrtimer *timer;
54cdfdb4 1324
833883d9 1325 timer = rb_entry(node, struct hrtimer, node);
cc584b21
AV
1326 if (base->softirq_time.tv64 <=
1327 hrtimer_get_expires_tv64(timer))
833883d9
DS
1328 break;
1329
833883d9
DS
1330 __run_hrtimer(timer);
1331 }
1332 spin_unlock(&cpu_base->lock);
1333 }
c0a31329
TG
1334}
1335
10c94ec1
TG
1336/*
1337 * Sleep related functions:
1338 */
c9cb2e3d 1339static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
00362e33
TG
1340{
1341 struct hrtimer_sleeper *t =
1342 container_of(timer, struct hrtimer_sleeper, timer);
1343 struct task_struct *task = t->task;
1344
1345 t->task = NULL;
1346 if (task)
1347 wake_up_process(task);
1348
1349 return HRTIMER_NORESTART;
1350}
1351
36c8b586 1352void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
00362e33
TG
1353{
1354 sl->timer.function = hrtimer_wakeup;
1355 sl->task = task;
1356}
1357
669d7868 1358static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
432569bb 1359{
669d7868 1360 hrtimer_init_sleeper(t, current);
10c94ec1 1361
432569bb
RZ
1362 do {
1363 set_current_state(TASK_INTERRUPTIBLE);
cc584b21 1364 hrtimer_start_expires(&t->timer, mode);
37bb6cb4
PZ
1365 if (!hrtimer_active(&t->timer))
1366 t->task = NULL;
432569bb 1367
54cdfdb4
TG
1368 if (likely(t->task))
1369 schedule();
432569bb 1370
669d7868 1371 hrtimer_cancel(&t->timer);
c9cb2e3d 1372 mode = HRTIMER_MODE_ABS;
669d7868
TG
1373
1374 } while (t->task && !signal_pending(current));
432569bb 1375
3588a085
PZ
1376 __set_current_state(TASK_RUNNING);
1377
669d7868 1378 return t->task == NULL;
10c94ec1
TG
1379}
1380
080344b9
ON
1381static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1382{
1383 struct timespec rmt;
1384 ktime_t rem;
1385
cc584b21 1386 rem = hrtimer_expires_remaining(timer);
080344b9
ON
1387 if (rem.tv64 <= 0)
1388 return 0;
1389 rmt = ktime_to_timespec(rem);
1390
1391 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1392 return -EFAULT;
1393
1394 return 1;
1395}
1396
1711ef38 1397long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
10c94ec1 1398{
669d7868 1399 struct hrtimer_sleeper t;
080344b9 1400 struct timespec __user *rmtp;
237fc6e7 1401 int ret = 0;
10c94ec1 1402
237fc6e7
TG
1403 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1404 HRTIMER_MODE_ABS);
cc584b21 1405 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
10c94ec1 1406
c9cb2e3d 1407 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
237fc6e7 1408 goto out;
10c94ec1 1409
029a07e0 1410 rmtp = restart->nanosleep.rmtp;
432569bb 1411 if (rmtp) {
237fc6e7 1412 ret = update_rmtp(&t.timer, rmtp);
080344b9 1413 if (ret <= 0)
237fc6e7 1414 goto out;
432569bb 1415 }
10c94ec1 1416
10c94ec1 1417 /* The other values in restart are already filled in */
237fc6e7
TG
1418 ret = -ERESTART_RESTARTBLOCK;
1419out:
1420 destroy_hrtimer_on_stack(&t.timer);
1421 return ret;
10c94ec1
TG
1422}
1423
080344b9 1424long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
10c94ec1
TG
1425 const enum hrtimer_mode mode, const clockid_t clockid)
1426{
1427 struct restart_block *restart;
669d7868 1428 struct hrtimer_sleeper t;
237fc6e7 1429 int ret = 0;
3bd01206
AV
1430 unsigned long slack;
1431
1432 slack = current->timer_slack_ns;
1433 if (rt_task(current))
1434 slack = 0;
10c94ec1 1435
237fc6e7 1436 hrtimer_init_on_stack(&t.timer, clockid, mode);
3bd01206 1437 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
432569bb 1438 if (do_nanosleep(&t, mode))
237fc6e7 1439 goto out;
10c94ec1 1440
7978672c 1441 /* Absolute timers do not update the rmtp value and restart: */
237fc6e7
TG
1442 if (mode == HRTIMER_MODE_ABS) {
1443 ret = -ERESTARTNOHAND;
1444 goto out;
1445 }
10c94ec1 1446
432569bb 1447 if (rmtp) {
237fc6e7 1448 ret = update_rmtp(&t.timer, rmtp);
080344b9 1449 if (ret <= 0)
237fc6e7 1450 goto out;
432569bb 1451 }
10c94ec1
TG
1452
1453 restart = &current_thread_info()->restart_block;
1711ef38 1454 restart->fn = hrtimer_nanosleep_restart;
029a07e0
TG
1455 restart->nanosleep.index = t.timer.base->index;
1456 restart->nanosleep.rmtp = rmtp;
cc584b21 1457 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
10c94ec1 1458
237fc6e7
TG
1459 ret = -ERESTART_RESTARTBLOCK;
1460out:
1461 destroy_hrtimer_on_stack(&t.timer);
1462 return ret;
10c94ec1
TG
1463}
1464
6ba1b912
TG
1465asmlinkage long
1466sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1467{
080344b9 1468 struct timespec tu;
6ba1b912
TG
1469
1470 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1471 return -EFAULT;
1472
1473 if (!timespec_valid(&tu))
1474 return -EINVAL;
1475
080344b9 1476 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
6ba1b912
TG
1477}
1478
c0a31329
TG
1479/*
1480 * Functions related to boot-time initialization:
1481 */
0ec160dd 1482static void __cpuinit init_hrtimers_cpu(int cpu)
c0a31329 1483{
3c8aa39d 1484 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
c0a31329
TG
1485 int i;
1486
3c8aa39d 1487 spin_lock_init(&cpu_base->lock);
3c8aa39d
TG
1488
1489 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1490 cpu_base->clock_base[i].cpu_base = cpu_base;
1491
54cdfdb4 1492 hrtimer_init_hres(cpu_base);
c0a31329
TG
1493}
1494
1495#ifdef CONFIG_HOTPLUG_CPU
1496
ca109491 1497static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
37810659 1498 struct hrtimer_clock_base *new_base)
c0a31329
TG
1499{
1500 struct hrtimer *timer;
1501 struct rb_node *node;
1502
1503 while ((node = rb_first(&old_base->active))) {
1504 timer = rb_entry(node, struct hrtimer, node);
54cdfdb4 1505 BUG_ON(hrtimer_callback_running(timer));
237fc6e7 1506 debug_hrtimer_deactivate(timer);
b00c1a99
TG
1507
1508 /*
1509 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1510 * timer could be seen as !active and just vanish away
1511 * under us on another CPU
1512 */
1513 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
c0a31329 1514 timer->base = new_base;
54cdfdb4 1515 /*
37810659
PZ
1516 * Enqueue the timers on the new cpu, but do not reprogram
1517 * the timer as that would enable a deadlock between
1518 * hrtimer_enqueue_reprogramm() running the timer and us still
1519 * holding a nested base lock.
1520 *
1521 * Instead we tickle the hrtimer interrupt after the migration
1522 * is done, which will run all expired timers and re-programm
1523 * the timer device.
54cdfdb4 1524 */
37810659 1525 enqueue_hrtimer(timer, new_base, 0);
41e1022e 1526
b00c1a99
TG
1527 /* Clear the migration state bit */
1528 timer->state &= ~HRTIMER_STATE_MIGRATE;
c0a31329
TG
1529 }
1530}
1531
37810659 1532static int migrate_hrtimers(int scpu)
c0a31329 1533{
3c8aa39d 1534 struct hrtimer_cpu_base *old_base, *new_base;
37810659 1535 int dcpu, i;
c0a31329 1536
37810659
PZ
1537 BUG_ON(cpu_online(scpu));
1538 old_base = &per_cpu(hrtimer_bases, scpu);
3c8aa39d 1539 new_base = &get_cpu_var(hrtimer_bases);
c0a31329 1540
37810659
PZ
1541 dcpu = smp_processor_id();
1542
1543 tick_cancel_sched_timer(scpu);
d82f0b0f
ON
1544 /*
1545 * The caller is globally serialized and nobody else
1546 * takes two locks at once, deadlock is not possible.
1547 */
1548 spin_lock_irq(&new_base->lock);
8e60e05f 1549 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
c0a31329 1550
3c8aa39d 1551 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
ca109491 1552 migrate_hrtimer_list(&old_base->clock_base[i],
37810659 1553 &new_base->clock_base[i]);
c0a31329
TG
1554 }
1555
8e60e05f 1556 spin_unlock(&old_base->lock);
d82f0b0f 1557 spin_unlock_irq(&new_base->lock);
c0a31329 1558 put_cpu_var(hrtimer_bases);
37810659
PZ
1559
1560 return dcpu;
1561}
1562
1563static void tickle_timers(void *arg)
1564{
1565 hrtimer_peek_ahead_timers();
c0a31329 1566}
37810659 1567
c0a31329
TG
1568#endif /* CONFIG_HOTPLUG_CPU */
1569
8c78f307 1570static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
c0a31329
TG
1571 unsigned long action, void *hcpu)
1572{
b2e3c0ad 1573 int scpu = (long)hcpu;
c0a31329
TG
1574
1575 switch (action) {
1576
1577 case CPU_UP_PREPARE:
8bb78442 1578 case CPU_UP_PREPARE_FROZEN:
37810659 1579 init_hrtimers_cpu(scpu);
c0a31329
TG
1580 break;
1581
1582#ifdef CONFIG_HOTPLUG_CPU
1583 case CPU_DEAD:
8bb78442 1584 case CPU_DEAD_FROZEN:
b2e3c0ad
IM
1585 {
1586 int dcpu;
1587
37810659
PZ
1588 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1589 dcpu = migrate_hrtimers(scpu);
37810659 1590 smp_call_function_single(dcpu, tickle_timers, NULL, 0);
c0a31329 1591 break;
b2e3c0ad 1592 }
c0a31329
TG
1593#endif
1594
1595 default:
1596 break;
1597 }
1598
1599 return NOTIFY_OK;
1600}
1601
8c78f307 1602static struct notifier_block __cpuinitdata hrtimers_nb = {
c0a31329
TG
1603 .notifier_call = hrtimer_cpu_notify,
1604};
1605
1606void __init hrtimers_init(void)
1607{
1608 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1609 (void *)(long)smp_processor_id());
1610 register_cpu_notifier(&hrtimers_nb);
1611}
1612
7bb67439 1613/**
654c8e0b 1614 * schedule_hrtimeout_range - sleep until timeout
7bb67439 1615 * @expires: timeout value (ktime_t)
654c8e0b 1616 * @delta: slack in expires timeout (ktime_t)
7bb67439
AV
1617 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1618 *
1619 * Make the current task sleep until the given expiry time has
1620 * elapsed. The routine will return immediately unless
1621 * the current task state has been set (see set_current_state()).
1622 *
654c8e0b
AV
1623 * The @delta argument gives the kernel the freedom to schedule the
1624 * actual wakeup to a time that is both power and performance friendly.
1625 * The kernel give the normal best effort behavior for "@expires+@delta",
1626 * but may decide to fire the timer earlier, but no earlier than @expires.
1627 *
7bb67439
AV
1628 * You can set the task state as follows -
1629 *
1630 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1631 * pass before the routine returns.
1632 *
1633 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1634 * delivered to the current task.
1635 *
1636 * The current task state is guaranteed to be TASK_RUNNING when this
1637 * routine returns.
1638 *
1639 * Returns 0 when the timer has expired otherwise -EINTR
1640 */
654c8e0b 1641int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
7bb67439
AV
1642 const enum hrtimer_mode mode)
1643{
1644 struct hrtimer_sleeper t;
1645
1646 /*
1647 * Optimize when a zero timeout value is given. It does not
1648 * matter whether this is an absolute or a relative time.
1649 */
1650 if (expires && !expires->tv64) {
1651 __set_current_state(TASK_RUNNING);
1652 return 0;
1653 }
1654
1655 /*
1656 * A NULL parameter means "inifinte"
1657 */
1658 if (!expires) {
1659 schedule();
1660 __set_current_state(TASK_RUNNING);
1661 return -EINTR;
1662 }
1663
1664 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
654c8e0b 1665 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
7bb67439
AV
1666
1667 hrtimer_init_sleeper(&t, current);
1668
cc584b21 1669 hrtimer_start_expires(&t.timer, mode);
7bb67439
AV
1670 if (!hrtimer_active(&t.timer))
1671 t.task = NULL;
1672
1673 if (likely(t.task))
1674 schedule();
1675
1676 hrtimer_cancel(&t.timer);
1677 destroy_hrtimer_on_stack(&t.timer);
1678
1679 __set_current_state(TASK_RUNNING);
1680
1681 return !t.task ? 0 : -EINTR;
1682}
654c8e0b
AV
1683EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1684
1685/**
1686 * schedule_hrtimeout - sleep until timeout
1687 * @expires: timeout value (ktime_t)
1688 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1689 *
1690 * Make the current task sleep until the given expiry time has
1691 * elapsed. The routine will return immediately unless
1692 * the current task state has been set (see set_current_state()).
1693 *
1694 * You can set the task state as follows -
1695 *
1696 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1697 * pass before the routine returns.
1698 *
1699 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1700 * delivered to the current task.
1701 *
1702 * The current task state is guaranteed to be TASK_RUNNING when this
1703 * routine returns.
1704 *
1705 * Returns 0 when the timer has expired otherwise -EINTR
1706 */
1707int __sched schedule_hrtimeout(ktime_t *expires,
1708 const enum hrtimer_mode mode)
1709{
1710 return schedule_hrtimeout_range(expires, 0, mode);
1711}
7bb67439 1712EXPORT_SYMBOL_GPL(schedule_hrtimeout);
This page took 0.4667 seconds and 5 git commands to generate.