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