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