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