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