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