2 * linux/kernel/hrtimer.c
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
8 * High-resolution kernel timers
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.
14 * These timers are currently used for:
18 * - precise in-kernel timing
20 * Started by: Thomas Gleixner and Ingo Molnar
23 * based on kernel/timer.c
25 * Help, testing, suggestions, bugfixes, improvements were
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
31 * For licencing details see kernel-base/COPYING
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/timer.h>
50 #include <linux/freezer.h>
52 #include <asm/uaccess.h>
54 #include <trace/events/timer.h>
56 #include <linux/mt_sched_mon.h>
58 //#define MTK_HRTIME_DEBUG /*MTK debug func*/
62 * There are more clockids then hrtimer bases. Thus, we index
63 * into the timer bases by the hrtimer_base_type enum. When trying
64 * to reach a base using a clockid, hrtimer_clockid_to_base()
65 * is used to convert from clockid to the proper hrtimer_base_type.
67 DEFINE_PER_CPU(struct hrtimer_cpu_base
, hrtimer_bases
) =
70 .lock
= __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases
.lock
),
74 .index
= HRTIMER_BASE_MONOTONIC
,
75 .clockid
= CLOCK_MONOTONIC
,
76 .get_time
= &ktime_get
,
77 .resolution
= KTIME_LOW_RES
,
80 .index
= HRTIMER_BASE_REALTIME
,
81 .clockid
= CLOCK_REALTIME
,
82 .get_time
= &ktime_get_real
,
83 .resolution
= KTIME_LOW_RES
,
86 .index
= HRTIMER_BASE_BOOTTIME
,
87 .clockid
= CLOCK_BOOTTIME
,
88 .get_time
= &ktime_get_boottime
,
89 .resolution
= KTIME_LOW_RES
,
92 .index
= HRTIMER_BASE_TAI
,
94 .get_time
= &ktime_get_clocktai
,
95 .resolution
= KTIME_LOW_RES
,
100 static const int hrtimer_clock_to_base_table
[MAX_CLOCKS
] = {
101 [CLOCK_REALTIME
] = HRTIMER_BASE_REALTIME
,
102 [CLOCK_MONOTONIC
] = HRTIMER_BASE_MONOTONIC
,
103 [CLOCK_BOOTTIME
] = HRTIMER_BASE_BOOTTIME
,
104 [CLOCK_TAI
] = HRTIMER_BASE_TAI
,
107 static inline int hrtimer_clockid_to_base(clockid_t clock_id
)
109 return hrtimer_clock_to_base_table
[clock_id
];
114 * Get the coarse grained time at the softirq based on xtime and
117 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base
*base
)
119 ktime_t xtim
, mono
, boot
;
120 struct timespec xts
, tom
, slp
;
123 get_xtime_and_monotonic_and_sleep_offset(&xts
, &tom
, &slp
);
124 tai_offset
= timekeeping_get_tai_offset();
126 xtim
= timespec_to_ktime(xts
);
127 mono
= ktime_add(xtim
, timespec_to_ktime(tom
));
128 boot
= ktime_add(mono
, timespec_to_ktime(slp
));
129 base
->clock_base
[HRTIMER_BASE_REALTIME
].softirq_time
= xtim
;
130 base
->clock_base
[HRTIMER_BASE_MONOTONIC
].softirq_time
= mono
;
131 base
->clock_base
[HRTIMER_BASE_BOOTTIME
].softirq_time
= boot
;
132 base
->clock_base
[HRTIMER_BASE_TAI
].softirq_time
=
133 ktime_add(xtim
, ktime_set(tai_offset
, 0));
137 * Functions and macros which are different for UP/SMP systems are kept in a
143 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
144 * means that all timers which are tied to this base via timer->base are
145 * locked, and the base itself is locked too.
147 * So __run_timers/migrate_timers can safely modify all timers which could
148 * be found on the lists/queues.
150 * When the timer's base is locked, and the timer removed from list, it is
151 * possible to set timer->base = NULL and drop the lock: the timer remains
155 struct hrtimer_clock_base
*lock_hrtimer_base(const struct hrtimer
*timer
,
156 unsigned long *flags
)
158 struct hrtimer_clock_base
*base
;
162 if (likely(base
!= NULL
)) {
163 raw_spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
164 if (likely(base
== timer
->base
))
166 /* The timer has migrated to another CPU: */
167 raw_spin_unlock_irqrestore(&base
->cpu_base
->lock
, *flags
);
175 * Get the preferred target CPU for NOHZ
177 static int hrtimer_get_target(int this_cpu
, int pinned
)
179 #ifdef CONFIG_NO_HZ_COMMON
180 if (!pinned
&& get_sysctl_timer_migration() && idle_cpu(this_cpu
))
181 return get_nohz_timer_target();
187 * With HIGHRES=y we do not migrate the timer when it is expiring
188 * before the next event on the target cpu because we cannot reprogram
189 * the target cpu hardware and we would cause it to fire late.
191 * Called with cpu_base->lock of target cpu held.
194 hrtimer_check_target(struct hrtimer
*timer
, struct hrtimer_clock_base
*new_base
)
196 #ifdef CONFIG_HIGH_RES_TIMERS
199 if (!new_base
->cpu_base
->hres_active
)
202 expires
= ktime_sub(hrtimer_get_expires(timer
), new_base
->offset
);
203 return expires
.tv64
<= new_base
->cpu_base
->expires_next
.tv64
;
210 * Switch the timer base to the current CPU when possible.
212 static inline struct hrtimer_clock_base
*
213 switch_hrtimer_base(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
,
216 struct hrtimer_clock_base
*new_base
;
217 struct hrtimer_cpu_base
*new_cpu_base
;
218 int this_cpu
= smp_processor_id();
219 int cpu
= hrtimer_get_target(this_cpu
, pinned
);
220 int basenum
= base
->index
;
223 new_cpu_base
= &per_cpu(hrtimer_bases
, cpu
);
224 new_base
= &new_cpu_base
->clock_base
[basenum
];
226 if (base
!= new_base
) {
228 * We are trying to move timer to new_base.
229 * However we can't change timer's base while it is running,
230 * so we keep it on the same CPU. No hassle vs. reprogramming
231 * the event source in the high resolution case. The softirq
232 * code will take care of this when the timer function has
233 * completed. There is no conflict as we hold the lock until
234 * the timer is enqueued.
236 if (unlikely(hrtimer_callback_running(timer
)))
239 /* See the comment in lock_timer_base() */
241 raw_spin_unlock(&base
->cpu_base
->lock
);
242 raw_spin_lock(&new_base
->cpu_base
->lock
);
244 if (cpu
!= this_cpu
&& hrtimer_check_target(timer
, new_base
)) {
246 raw_spin_unlock(&new_base
->cpu_base
->lock
);
247 raw_spin_lock(&base
->cpu_base
->lock
);
251 timer
->base
= new_base
;
253 if (cpu
!= this_cpu
&& hrtimer_check_target(timer
, new_base
)) {
261 #else /* CONFIG_SMP */
263 static inline struct hrtimer_clock_base
*
264 lock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
266 struct hrtimer_clock_base
*base
= timer
->base
;
268 raw_spin_lock_irqsave(&base
->cpu_base
->lock
, *flags
);
273 # define switch_hrtimer_base(t, b, p) (b)
275 #endif /* !CONFIG_SMP */
278 * Functions for the union type storage format of ktime_t which are
279 * too large for inlining:
281 #if BITS_PER_LONG < 64
282 # ifndef CONFIG_KTIME_SCALAR
284 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
286 * @nsec: the scalar nsec value to add
288 * Returns the sum of kt and nsec in ktime_t format
290 ktime_t
ktime_add_ns(const ktime_t kt
, u64 nsec
)
294 if (likely(nsec
< NSEC_PER_SEC
)) {
297 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
299 /* Make sure nsec fits into long */
300 if (unlikely(nsec
> KTIME_SEC_MAX
))
301 return (ktime_t
){ .tv64
= KTIME_MAX
};
303 tmp
= ktime_set((long)nsec
, rem
);
306 return ktime_add(kt
, tmp
);
309 EXPORT_SYMBOL_GPL(ktime_add_ns
);
312 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
314 * @nsec: the scalar nsec value to subtract
316 * Returns the subtraction of @nsec from @kt in ktime_t format
318 ktime_t
ktime_sub_ns(const ktime_t kt
, u64 nsec
)
322 if (likely(nsec
< NSEC_PER_SEC
)) {
325 unsigned long rem
= do_div(nsec
, NSEC_PER_SEC
);
327 tmp
= ktime_set((long)nsec
, rem
);
330 return ktime_sub(kt
, tmp
);
333 EXPORT_SYMBOL_GPL(ktime_sub_ns
);
334 # endif /* !CONFIG_KTIME_SCALAR */
337 * Divide a ktime value by a nanosecond value
339 u64
ktime_divns(const ktime_t kt
, s64 div
)
344 dclc
= ktime_to_ns(kt
);
345 /* Make sure the divisor is less than 2^32: */
351 do_div(dclc
, (unsigned long) div
);
355 #endif /* BITS_PER_LONG >= 64 */
358 * Add two ktime values and do a safety check for overflow:
360 ktime_t
ktime_add_safe(const ktime_t lhs
, const ktime_t rhs
)
362 ktime_t res
= ktime_add(lhs
, rhs
);
365 * We use KTIME_SEC_MAX here, the maximum timeout which we can
366 * return to user space in a timespec:
368 if (res
.tv64
< 0 || res
.tv64
< lhs
.tv64
|| res
.tv64
< rhs
.tv64
)
369 res
= ktime_set(KTIME_SEC_MAX
, 0);
374 EXPORT_SYMBOL_GPL(ktime_add_safe
);
376 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
378 static struct debug_obj_descr hrtimer_debug_descr
;
380 static void *hrtimer_debug_hint(void *addr
)
382 return ((struct hrtimer
*) addr
)->function
;
386 * fixup_init is called when:
387 * - an active object is initialized
389 static int hrtimer_fixup_init(void *addr
, enum debug_obj_state state
)
391 struct hrtimer
*timer
= addr
;
394 case ODEBUG_STATE_ACTIVE
:
395 hrtimer_cancel(timer
);
396 debug_object_init(timer
, &hrtimer_debug_descr
);
404 * fixup_activate is called when:
405 * - an active object is activated
406 * - an unknown object is activated (might be a statically initialized object)
408 static int hrtimer_fixup_activate(void *addr
, enum debug_obj_state state
)
412 case ODEBUG_STATE_NOTAVAILABLE
:
416 case ODEBUG_STATE_ACTIVE
:
425 * fixup_free is called when:
426 * - an active object is freed
428 static int hrtimer_fixup_free(void *addr
, enum debug_obj_state state
)
430 struct hrtimer
*timer
= addr
;
433 case ODEBUG_STATE_ACTIVE
:
434 hrtimer_cancel(timer
);
435 debug_object_free(timer
, &hrtimer_debug_descr
);
442 static struct debug_obj_descr hrtimer_debug_descr
= {
444 .debug_hint
= hrtimer_debug_hint
,
445 .fixup_init
= hrtimer_fixup_init
,
446 .fixup_activate
= hrtimer_fixup_activate
,
447 .fixup_free
= hrtimer_fixup_free
,
450 static inline void debug_hrtimer_init(struct hrtimer
*timer
)
452 debug_object_init(timer
, &hrtimer_debug_descr
);
455 static inline void debug_hrtimer_activate(struct hrtimer
*timer
)
457 debug_object_activate(timer
, &hrtimer_debug_descr
);
460 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
)
462 debug_object_deactivate(timer
, &hrtimer_debug_descr
);
465 static inline void debug_hrtimer_free(struct hrtimer
*timer
)
467 debug_object_free(timer
, &hrtimer_debug_descr
);
470 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
471 enum hrtimer_mode mode
);
473 void hrtimer_init_on_stack(struct hrtimer
*timer
, clockid_t clock_id
,
474 enum hrtimer_mode mode
)
476 debug_object_init_on_stack(timer
, &hrtimer_debug_descr
);
477 __hrtimer_init(timer
, clock_id
, mode
);
479 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack
);
481 void destroy_hrtimer_on_stack(struct hrtimer
*timer
)
483 debug_object_free(timer
, &hrtimer_debug_descr
);
487 static inline void debug_hrtimer_init(struct hrtimer
*timer
) { }
488 static inline void debug_hrtimer_activate(struct hrtimer
*timer
) { }
489 static inline void debug_hrtimer_deactivate(struct hrtimer
*timer
) { }
493 debug_init(struct hrtimer
*timer
, clockid_t clockid
,
494 enum hrtimer_mode mode
)
496 debug_hrtimer_init(timer
);
497 trace_hrtimer_init(timer
, clockid
, mode
);
500 static inline void debug_activate(struct hrtimer
*timer
)
502 debug_hrtimer_activate(timer
);
503 trace_hrtimer_start(timer
);
506 static inline void debug_deactivate(struct hrtimer
*timer
)
508 debug_hrtimer_deactivate(timer
);
509 trace_hrtimer_cancel(timer
);
512 /* High resolution timer related functions */
513 #ifdef CONFIG_HIGH_RES_TIMERS
516 * High resolution timer enabled ?
518 static int hrtimer_hres_enabled __read_mostly
= 1;
521 * Enable / Disable high resolution mode
523 static int __init
setup_hrtimer_hres(char *str
)
525 if (!strcmp(str
, "off"))
526 hrtimer_hres_enabled
= 0;
527 else if (!strcmp(str
, "on"))
528 hrtimer_hres_enabled
= 1;
534 __setup("highres=", setup_hrtimer_hres
);
537 * hrtimer_high_res_enabled - query, if the highres mode is enabled
539 static inline int hrtimer_is_hres_enabled(void)
541 return hrtimer_hres_enabled
;
545 * Is the high resolution mode active ?
547 static inline int hrtimer_hres_active(void)
549 return __this_cpu_read(hrtimer_bases
.hres_active
);
553 * Reprogram the event source with checking both queues for the
555 * Called with interrupts disabled and base->lock held
558 hrtimer_force_reprogram(struct hrtimer_cpu_base
*cpu_base
, int skip_equal
)
561 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
562 ktime_t expires
, expires_next
;
564 expires_next
.tv64
= KTIME_MAX
;
566 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
567 struct hrtimer
*timer
;
568 struct timerqueue_node
*next
;
570 next
= timerqueue_getnext(&base
->active
);
573 timer
= container_of(next
, struct hrtimer
, node
);
575 expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
577 * clock_was_set() has changed base->offset so the
578 * result might be negative. Fix it up to prevent a
579 * false positive in clockevents_program_event()
581 if (expires
.tv64
< 0)
583 if (expires
.tv64
< expires_next
.tv64
)
584 expires_next
= expires
;
587 if (skip_equal
&& expires_next
.tv64
== cpu_base
->expires_next
.tv64
)
590 cpu_base
->expires_next
.tv64
= expires_next
.tv64
;
593 * If a hang was detected in the last timer interrupt then we
594 * leave the hang delay active in the hardware. We want the
595 * system to make progress. That also prevents the following
597 * T1 expires 50ms from now
598 * T2 expires 5s from now
600 * T1 is removed, so this code is called and would reprogram
601 * the hardware to 5s from now. Any hrtimer_start after that
602 * will not reprogram the hardware due to hang_detected being
603 * set. So we'd effectivly block all timers until the T2 event
606 if (cpu_base
->hang_detected
)
609 if (cpu_base
->expires_next
.tv64
!= KTIME_MAX
)
610 tick_program_event(cpu_base
->expires_next
, 1);
614 * Shared reprogramming for clock_realtime and clock_monotonic
616 * When a timer is enqueued and expires earlier than the already enqueued
617 * timers, we have to check, whether it expires earlier than the timer for
618 * which the clock event device was armed.
620 * Called with interrupts disabled and base->cpu_base.lock held
622 static int hrtimer_reprogram(struct hrtimer
*timer
,
623 struct hrtimer_clock_base
*base
)
625 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
626 ktime_t expires
= ktime_sub(hrtimer_get_expires(timer
), base
->offset
);
629 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer
) < 0);
632 * When the callback is running, we do not reprogram the clock event
633 * device. The timer callback is either running on a different CPU or
634 * the callback is executed in the hrtimer_interrupt context. The
635 * reprogramming is handled either by the softirq, which called the
636 * callback or at the end of the hrtimer_interrupt.
638 if (hrtimer_callback_running(timer
))
642 * CLOCK_REALTIME timer might be requested with an absolute
643 * expiry time which is less than base->offset. Nothing wrong
644 * about that, just avoid to call into the tick code, which
645 * has now objections against negative expiry values.
647 if (expires
.tv64
< 0)
650 if (expires
.tv64
>= cpu_base
->expires_next
.tv64
)
654 * If a hang was detected in the last timer interrupt then we
655 * do not schedule a timer which is earlier than the expiry
656 * which we enforced in the hang detection. We want the system
659 if (cpu_base
->hang_detected
)
663 * Clockevents returns -ETIME, when the event was in the past.
665 res
= tick_program_event(expires
, 0);
666 if (!IS_ERR_VALUE(res
))
667 cpu_base
->expires_next
= expires
;
672 * Initialize the high resolution related parts of cpu_base
674 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
)
676 base
->expires_next
.tv64
= KTIME_MAX
;
677 base
->hres_active
= 0;
681 * When High resolution timers are active, try to reprogram. Note, that in case
682 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
683 * check happens. The timer gets enqueued into the rbtree. The reprogramming
684 * and expiry check is done in the hrtimer_interrupt or in the softirq.
686 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
687 struct hrtimer_clock_base
*base
)
689 return base
->cpu_base
->hres_active
&& hrtimer_reprogram(timer
, base
);
692 static inline ktime_t
hrtimer_update_base(struct hrtimer_cpu_base
*base
)
694 ktime_t
*offs_real
= &base
->clock_base
[HRTIMER_BASE_REALTIME
].offset
;
695 ktime_t
*offs_boot
= &base
->clock_base
[HRTIMER_BASE_BOOTTIME
].offset
;
696 ktime_t
*offs_tai
= &base
->clock_base
[HRTIMER_BASE_TAI
].offset
;
698 return ktime_get_update_offsets(offs_real
, offs_boot
, offs_tai
);
702 * Retrigger next event is called after clock was set
704 * Called with interrupts disabled via on_each_cpu()
706 static void retrigger_next_event(void *arg
)
708 struct hrtimer_cpu_base
*base
= &__get_cpu_var(hrtimer_bases
);
710 if (!hrtimer_hres_active())
713 raw_spin_lock(&base
->lock
);
714 hrtimer_update_base(base
);
715 hrtimer_force_reprogram(base
, 0);
716 raw_spin_unlock(&base
->lock
);
720 * Switch to high resolution mode
722 static int hrtimer_switch_to_hres(void)
724 int i
, cpu
= smp_processor_id();
725 struct hrtimer_cpu_base
*base
= &per_cpu(hrtimer_bases
, cpu
);
728 if (base
->hres_active
)
731 local_irq_save(flags
);
733 if (tick_init_highres()) {
734 local_irq_restore(flags
);
735 printk(KERN_WARNING
"Could not switch to high resolution "
736 "mode on CPU %d\n", cpu
);
739 base
->hres_active
= 1;
740 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++)
741 base
->clock_base
[i
].resolution
= KTIME_HIGH_RES
;
743 tick_setup_sched_timer();
744 /* "Retrigger" the interrupt to get things going */
745 retrigger_next_event(NULL
);
746 local_irq_restore(flags
);
750 static void clock_was_set_work(struct work_struct
*work
)
755 static DECLARE_WORK(hrtimer_work
, clock_was_set_work
);
758 * Called from timekeeping and resume code to reprogramm the hrtimer
759 * interrupt device on all cpus.
761 void clock_was_set_delayed(void)
763 schedule_work(&hrtimer_work
);
768 static inline int hrtimer_hres_active(void) { return 0; }
769 static inline int hrtimer_is_hres_enabled(void) { return 0; }
770 static inline int hrtimer_switch_to_hres(void) { return 0; }
772 hrtimer_force_reprogram(struct hrtimer_cpu_base
*base
, int skip_equal
) { }
773 static inline int hrtimer_enqueue_reprogram(struct hrtimer
*timer
,
774 struct hrtimer_clock_base
*base
)
778 static inline void hrtimer_init_hres(struct hrtimer_cpu_base
*base
) { }
779 static inline void retrigger_next_event(void *arg
) { }
781 #endif /* CONFIG_HIGH_RES_TIMERS */
784 * Clock realtime was set
786 * Change the offset of the realtime clock vs. the monotonic
789 * We might have to reprogram the high resolution timer interrupt. On
790 * SMP we call the architecture specific code to retrigger _all_ high
791 * resolution timer interrupts. On UP we just disable interrupts and
792 * call the high resolution interrupt code.
794 void clock_was_set(void)
796 #ifdef CONFIG_HIGH_RES_TIMERS
797 /* Retrigger the CPU local events everywhere */
798 on_each_cpu(retrigger_next_event
, NULL
, 1);
800 timerfd_clock_was_set();
804 * During resume we might have to reprogram the high resolution timer
805 * interrupt (on the local CPU):
807 void hrtimers_resume(void)
809 WARN_ONCE(!irqs_disabled(),
810 KERN_INFO
"hrtimers_resume() called with IRQs enabled!");
812 /* Retrigger on the local CPU */
813 retrigger_next_event(NULL
);
814 /* And schedule a retrigger for all others */
815 clock_was_set_delayed();
818 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
)
820 #ifdef CONFIG_TIMER_STATS
821 if (timer
->start_site
)
823 timer
->start_site
= __builtin_return_address(0);
824 memcpy(timer
->start_comm
, current
->comm
, TASK_COMM_LEN
);
825 timer
->start_pid
= current
->pid
;
829 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer
*timer
)
831 #ifdef CONFIG_TIMER_STATS
832 timer
->start_site
= NULL
;
836 static inline void timer_stats_account_hrtimer(struct hrtimer
*timer
)
838 #ifdef CONFIG_TIMER_STATS
839 if (likely(!timer_stats_active
))
841 timer_stats_update_stats(timer
, timer
->start_pid
, timer
->start_site
,
842 timer
->function
, timer
->start_comm
, 0);
847 * Counterpart to lock_hrtimer_base above:
850 void unlock_hrtimer_base(const struct hrtimer
*timer
, unsigned long *flags
)
852 raw_spin_unlock_irqrestore(&timer
->base
->cpu_base
->lock
, *flags
);
856 * hrtimer_forward - forward the timer expiry
857 * @timer: hrtimer to forward
858 * @now: forward past this time
859 * @interval: the interval to forward
861 * Forward the timer expiry so it will expire in the future.
862 * Returns the number of overruns.
864 u64
hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
)
869 delta
= ktime_sub(now
, hrtimer_get_expires(timer
));
874 if (interval
.tv64
< timer
->base
->resolution
.tv64
)
875 interval
.tv64
= timer
->base
->resolution
.tv64
;
877 if (unlikely(delta
.tv64
>= interval
.tv64
)) {
878 s64 incr
= ktime_to_ns(interval
);
880 orun
= ktime_divns(delta
, incr
);
881 hrtimer_add_expires_ns(timer
, incr
* orun
);
882 if (hrtimer_get_expires_tv64(timer
) > now
.tv64
)
885 * This (and the ktime_add() below) is the
886 * correction for exact:
890 hrtimer_add_expires(timer
, interval
);
894 EXPORT_SYMBOL_GPL(hrtimer_forward
);
897 * enqueue_hrtimer - internal function to (re)start a timer
899 * The timer is inserted in expiry order. Insertion into the
900 * red black tree is O(log(n)). Must hold the base lock.
902 * Returns 1 when the new timer is the leftmost timer in the tree.
904 static int enqueue_hrtimer(struct hrtimer
*timer
,
905 struct hrtimer_clock_base
*base
)
907 debug_activate(timer
);
909 timerqueue_add(&base
->active
, &timer
->node
);
910 base
->cpu_base
->active_bases
|= 1 << base
->index
;
913 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
914 * state of a possibly running callback.
916 timer
->state
|= HRTIMER_STATE_ENQUEUED
;
918 return (&timer
->node
== base
->active
.next
);
922 * __remove_hrtimer - internal function to remove a timer
924 * Caller must hold the base lock.
926 * High resolution timer mode reprograms the clock event device when the
927 * timer is the one which expires next. The caller can disable this by setting
928 * reprogram to zero. This is useful, when the context does a reprogramming
929 * anyway (e.g. timer interrupt)
931 static void __remove_hrtimer(struct hrtimer
*timer
,
932 struct hrtimer_clock_base
*base
,
933 unsigned long newstate
, int reprogram
)
935 struct timerqueue_node
*next_timer
;
936 if (!(timer
->state
& HRTIMER_STATE_ENQUEUED
))
939 next_timer
= timerqueue_getnext(&base
->active
);
940 timerqueue_del(&base
->active
, &timer
->node
);
941 if (&timer
->node
== next_timer
) {
942 #ifdef CONFIG_HIGH_RES_TIMERS
943 /* Reprogram the clock event device. if enabled */
944 if (reprogram
&& hrtimer_hres_active()) {
947 expires
= ktime_sub(hrtimer_get_expires(timer
),
949 if (base
->cpu_base
->expires_next
.tv64
== expires
.tv64
)
950 hrtimer_force_reprogram(base
->cpu_base
, 1);
954 if (!timerqueue_getnext(&base
->active
))
955 base
->cpu_base
->active_bases
&= ~(1 << base
->index
);
957 timer
->state
= newstate
;
961 * remove hrtimer, called with base lock held
964 remove_hrtimer(struct hrtimer
*timer
, struct hrtimer_clock_base
*base
)
966 if (hrtimer_is_queued(timer
)) {
971 * Remove the timer and force reprogramming when high
972 * resolution mode is active and the timer is on the current
973 * CPU. If we remove a timer on another CPU, reprogramming is
974 * skipped. The interrupt event on this CPU is fired and
975 * reprogramming happens in the interrupt handler. This is a
976 * rare case and less expensive than a smp call.
978 debug_deactivate(timer
);
979 timer_stats_hrtimer_clear_start_info(timer
);
980 reprogram
= base
->cpu_base
== &__get_cpu_var(hrtimer_bases
);
982 * We must preserve the CALLBACK state flag here,
983 * otherwise we could move the timer base in
984 * switch_hrtimer_base.
986 state
= timer
->state
& HRTIMER_STATE_CALLBACK
;
987 __remove_hrtimer(timer
, base
, state
, reprogram
);
993 int __hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
994 unsigned long delta_ns
, const enum hrtimer_mode mode
,
997 struct hrtimer_clock_base
*base
, *new_base
;
1000 /*add MTK debug log for ALPS01804694*/
1001 if(timer
->function
== NULL
) {
1002 pr_alert("add hrtimer but do nothing");
1006 base
= lock_hrtimer_base(timer
, &flags
);
1008 /* Remove an active timer from the queue: */
1009 ret
= remove_hrtimer(timer
, base
);
1011 if (mode
& HRTIMER_MODE_REL
) {
1012 tim
= ktime_add_safe(tim
, base
->get_time());
1014 * CONFIG_TIME_LOW_RES is a temporary way for architectures
1015 * to signal that they simply return xtime in
1016 * do_gettimeoffset(). In this case we want to round up by
1017 * resolution when starting a relative timer, to avoid short
1018 * timeouts. This will go away with the GTOD framework.
1020 #ifdef CONFIG_TIME_LOW_RES
1021 tim
= ktime_add_safe(tim
, base
->resolution
);
1025 hrtimer_set_expires_range_ns(timer
, tim
, delta_ns
);
1027 /* Switch the timer base, if necessary: */
1028 new_base
= switch_hrtimer_base(timer
, base
, mode
& HRTIMER_MODE_PINNED
);
1030 timer_stats_hrtimer_set_start_info(timer
);
1032 leftmost
= enqueue_hrtimer(timer
, new_base
);
1035 * Only allow reprogramming if the new base is on this CPU.
1036 * (it might still be on another CPU if the timer was pending)
1038 * XXX send_remote_softirq() ?
1040 if (leftmost
&& new_base
->cpu_base
== &__get_cpu_var(hrtimer_bases
)
1041 && hrtimer_enqueue_reprogram(timer
, new_base
)) {
1044 * We need to drop cpu_base->lock to avoid a
1045 * lock ordering issue vs. rq->lock.
1047 raw_spin_unlock(&new_base
->cpu_base
->lock
);
1048 raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
1049 local_irq_restore(flags
);
1052 __raise_softirq_irqoff(HRTIMER_SOFTIRQ
);
1056 unlock_hrtimer_base(timer
, &flags
);
1062 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1063 * @timer: the timer to be added
1065 * @delta_ns: "slack" range for the timer
1066 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1067 * relative (HRTIMER_MODE_REL)
1071 * 1 when the timer was active
1073 int hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
1074 unsigned long delta_ns
, const enum hrtimer_mode mode
)
1076 return __hrtimer_start_range_ns(timer
, tim
, delta_ns
, mode
, 1);
1078 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns
);
1081 * hrtimer_start - (re)start an hrtimer on the current CPU
1082 * @timer: the timer to be added
1084 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1085 * relative (HRTIMER_MODE_REL)
1089 * 1 when the timer was active
1092 hrtimer_start(struct hrtimer
*timer
, ktime_t tim
, const enum hrtimer_mode mode
)
1094 return __hrtimer_start_range_ns(timer
, tim
, 0, mode
, 1);
1096 EXPORT_SYMBOL_GPL(hrtimer_start
);
1100 * hrtimer_try_to_cancel - try to deactivate a timer
1101 * @timer: hrtimer to stop
1104 * 0 when the timer was not active
1105 * 1 when the timer was active
1106 * -1 when the timer is currently excuting the callback function and
1109 int hrtimer_try_to_cancel(struct hrtimer
*timer
)
1111 struct hrtimer_clock_base
*base
;
1112 unsigned long flags
;
1115 base
= lock_hrtimer_base(timer
, &flags
);
1117 if (!hrtimer_callback_running(timer
))
1118 ret
= remove_hrtimer(timer
, base
);
1120 unlock_hrtimer_base(timer
, &flags
);
1125 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel
);
1128 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1129 * @timer: the timer to be cancelled
1132 * 0 when the timer was not active
1133 * 1 when the timer was active
1135 int hrtimer_cancel(struct hrtimer
*timer
)
1138 int ret
= hrtimer_try_to_cancel(timer
);
1145 EXPORT_SYMBOL_GPL(hrtimer_cancel
);
1148 * hrtimer_get_remaining - get remaining time for the timer
1149 * @timer: the timer to read
1151 ktime_t
hrtimer_get_remaining(const struct hrtimer
*timer
)
1153 unsigned long flags
;
1156 lock_hrtimer_base(timer
, &flags
);
1157 rem
= hrtimer_expires_remaining(timer
);
1158 unlock_hrtimer_base(timer
, &flags
);
1162 EXPORT_SYMBOL_GPL(hrtimer_get_remaining
);
1164 #ifdef CONFIG_NO_HZ_COMMON
1166 * hrtimer_get_next_event - get the time until next expiry event
1168 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1171 ktime_t
hrtimer_get_next_event(void)
1173 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1174 struct hrtimer_clock_base
*base
= cpu_base
->clock_base
;
1175 ktime_t delta
, mindelta
= { .tv64
= KTIME_MAX
};
1176 unsigned long flags
;
1179 raw_spin_lock_irqsave(&cpu_base
->lock
, flags
);
1181 if (!hrtimer_hres_active()) {
1182 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++, base
++) {
1183 struct hrtimer
*timer
;
1184 struct timerqueue_node
*next
;
1186 next
= timerqueue_getnext(&base
->active
);
1190 timer
= container_of(next
, struct hrtimer
, node
);
1191 delta
.tv64
= hrtimer_get_expires_tv64(timer
);
1192 delta
= ktime_sub(delta
, base
->get_time());
1193 if (delta
.tv64
< mindelta
.tv64
)
1194 mindelta
.tv64
= delta
.tv64
;
1198 raw_spin_unlock_irqrestore(&cpu_base
->lock
, flags
);
1200 if (mindelta
.tv64
< 0)
1206 static void __hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1207 enum hrtimer_mode mode
)
1209 struct hrtimer_cpu_base
*cpu_base
;
1212 memset(timer
, 0, sizeof(struct hrtimer
));
1214 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1216 if (clock_id
== CLOCK_REALTIME
&& mode
!= HRTIMER_MODE_ABS
)
1217 clock_id
= CLOCK_MONOTONIC
;
1219 base
= hrtimer_clockid_to_base(clock_id
);
1220 timer
->base
= &cpu_base
->clock_base
[base
];
1221 timerqueue_init(&timer
->node
);
1223 #ifdef CONFIG_TIMER_STATS
1224 timer
->start_site
= NULL
;
1225 timer
->start_pid
= -1;
1226 memset(timer
->start_comm
, 0, TASK_COMM_LEN
);
1231 * hrtimer_init - initialize a timer to the given clock
1232 * @timer: the timer to be initialized
1233 * @clock_id: the clock to be used
1234 * @mode: timer mode abs/rel
1236 void hrtimer_init(struct hrtimer
*timer
, clockid_t clock_id
,
1237 enum hrtimer_mode mode
)
1239 debug_init(timer
, clock_id
, mode
);
1240 __hrtimer_init(timer
, clock_id
, mode
);
1242 EXPORT_SYMBOL_GPL(hrtimer_init
);
1245 * hrtimer_get_res - get the timer resolution for a clock
1246 * @which_clock: which clock to query
1247 * @tp: pointer to timespec variable to store the resolution
1249 * Store the resolution of the clock selected by @which_clock in the
1250 * variable pointed to by @tp.
1252 int hrtimer_get_res(const clockid_t which_clock
, struct timespec
*tp
)
1254 struct hrtimer_cpu_base
*cpu_base
;
1255 int base
= hrtimer_clockid_to_base(which_clock
);
1257 cpu_base
= &__raw_get_cpu_var(hrtimer_bases
);
1258 *tp
= ktime_to_timespec(cpu_base
->clock_base
[base
].resolution
);
1262 EXPORT_SYMBOL_GPL(hrtimer_get_res
);
1264 #ifdef MTK_HRTIME_DEBUG
1265 static void dump_hrtimer_callinfo(struct hrtimer
*timer
)
1268 char symname
[KSYM_NAME_LEN
];
1269 if (lookup_symbol_name((unsigned long)(timer
->function
), symname
) < 0) {
1270 pr_err("timer info1: state/%lx, func/%pK\n",
1271 timer
->state
, timer
->function
);
1273 pr_err("timer info2: state/%lx, func/%s\n",
1274 timer
->state
, symname
);
1277 #ifdef CONFIG_TIMER_STATS
1278 if (lookup_symbol_name((unsigned long)(timer
->start_site
),
1280 pr_err("timer stats1: pid/%d(%s), site/%pK\n",
1281 timer
->start_pid
, timer
->start_comm
, timer
->start_site
);
1283 pr_err("timer stats2: pid/%d(%s), site/%s\n",
1284 timer
->start_pid
, timer
->start_comm
, symname
);
1289 static void __run_hrtimer(struct hrtimer
*timer
, ktime_t
*now
)
1291 struct hrtimer_clock_base
*base
= timer
->base
;
1292 struct hrtimer_cpu_base
*cpu_base
= base
->cpu_base
;
1293 enum hrtimer_restart (*fn
)(struct hrtimer
*);
1296 WARN_ON(!irqs_disabled());
1298 debug_deactivate(timer
);
1299 __remove_hrtimer(timer
, base
, HRTIMER_STATE_CALLBACK
, 0);
1300 timer_stats_account_hrtimer(timer
);
1301 fn
= timer
->function
;
1304 * Because we run timers from hardirq context, there is no chance
1305 * they get migrated to another cpu, therefore its safe to unlock
1308 raw_spin_unlock(&cpu_base
->lock
);
1309 trace_hrtimer_expire_entry(timer
, now
);
1311 mt_trace_hrt_start(fn
);
1312 restart
= fn(timer
);
1313 mt_trace_hrt_end(fn
);
1314 trace_hrtimer_expire_exit(timer
);
1315 raw_spin_lock(&cpu_base
->lock
);
1318 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1319 * we do not reprogramm the event hardware. Happens either in
1320 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1322 if (restart
!= HRTIMER_NORESTART
) {
1323 BUG_ON(timer
->state
!= HRTIMER_STATE_CALLBACK
);
1324 enqueue_hrtimer(timer
, base
);
1327 WARN_ON_ONCE(!(timer
->state
& HRTIMER_STATE_CALLBACK
));
1329 timer
->state
&= ~HRTIMER_STATE_CALLBACK
;
1332 #ifdef CONFIG_HIGH_RES_TIMERS
1335 * High resolution timer interrupt
1336 * Called with interrupts disabled
1338 void hrtimer_interrupt(struct clock_event_device
*dev
)
1340 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1341 ktime_t expires_next
, now
, entry_time
, delta
;
1344 BUG_ON(!cpu_base
->hres_active
);
1345 cpu_base
->nr_events
++;
1346 dev
->next_event
.tv64
= KTIME_MAX
;
1348 raw_spin_lock(&cpu_base
->lock
);
1349 entry_time
= now
= hrtimer_update_base(cpu_base
);
1351 expires_next
.tv64
= KTIME_MAX
;
1353 * We set expires_next to KTIME_MAX here with cpu_base->lock
1354 * held to prevent that a timer is enqueued in our queue via
1355 * the migration code. This does not affect enqueueing of
1356 * timers which run their callback and need to be requeued on
1359 cpu_base
->expires_next
.tv64
= KTIME_MAX
;
1361 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1362 struct hrtimer_clock_base
*base
;
1363 struct timerqueue_node
*node
;
1366 if (!(cpu_base
->active_bases
& (1 << i
)))
1369 base
= cpu_base
->clock_base
+ i
;
1370 basenow
= ktime_add(now
, base
->offset
);
1372 while ((node
= timerqueue_getnext(&base
->active
))) {
1373 struct hrtimer
*timer
;
1375 timer
= container_of(node
, struct hrtimer
, node
);
1378 * The immediate goal for using the softexpires is
1379 * minimizing wakeups, not running timers at the
1380 * earliest interrupt after their soft expiration.
1381 * This allows us to avoid using a Priority Search
1382 * Tree, which can answer a stabbing querry for
1383 * overlapping intervals and instead use the simple
1384 * BST we already have.
1385 * We don't add extra wakeups by delaying timers that
1386 * are right-of a not yet expired timer, because that
1387 * timer will have to trigger a wakeup anyway.
1390 if (basenow
.tv64
< hrtimer_get_softexpires_tv64(timer
)) {
1393 expires
= ktime_sub(hrtimer_get_expires(timer
),
1395 if (expires
.tv64
< 0)
1396 expires
.tv64
= KTIME_MAX
;
1397 if (expires
.tv64
< expires_next
.tv64
)
1398 expires_next
= expires
;
1402 __run_hrtimer(timer
, &basenow
);
1407 * Store the new expiry value so the migration code can verify
1410 cpu_base
->expires_next
= expires_next
;
1411 raw_spin_unlock(&cpu_base
->lock
);
1413 /* Reprogramming necessary ? */
1414 if (expires_next
.tv64
== KTIME_MAX
||
1415 !tick_program_event(expires_next
, 0)) {
1416 cpu_base
->hang_detected
= 0;
1421 * The next timer was already expired due to:
1423 * - long lasting callbacks
1424 * - being scheduled away when running in a VM
1426 * We need to prevent that we loop forever in the hrtimer
1427 * interrupt routine. We give it 3 attempts to avoid
1428 * overreacting on some spurious event.
1430 * Acquire base lock for updating the offsets and retrieving
1433 raw_spin_lock(&cpu_base
->lock
);
1434 now
= hrtimer_update_base(cpu_base
);
1435 cpu_base
->nr_retries
++;
1439 * Give the system a chance to do something else than looping
1440 * here. We stored the entry time, so we know exactly how long
1441 * we spent here. We schedule the next event this amount of
1444 cpu_base
->nr_hangs
++;
1445 cpu_base
->hang_detected
= 1;
1446 raw_spin_unlock(&cpu_base
->lock
);
1447 delta
= ktime_sub(now
, entry_time
);
1448 if (delta
.tv64
> cpu_base
->max_hang_time
.tv64
)
1449 cpu_base
->max_hang_time
= delta
;
1451 * Limit it to a sensible value as we enforce a longer
1452 * delay. Give the CPU at least 100ms to catch up.
1454 if (delta
.tv64
> 100 * NSEC_PER_MSEC
)
1455 expires_next
= ktime_add_ns(now
, 100 * NSEC_PER_MSEC
);
1457 expires_next
= ktime_add(now
, delta
);
1458 tick_program_event(expires_next
, 1);
1459 printk_once(KERN_WARNING
"hrtimer: interrupt took %llu ns\n",
1460 ktime_to_ns(delta
));
1464 * local version of hrtimer_peek_ahead_timers() called with interrupts
1467 static void __hrtimer_peek_ahead_timers(void)
1469 struct tick_device
*td
;
1471 if (!hrtimer_hres_active())
1474 td
= &__get_cpu_var(tick_cpu_device
);
1475 if (td
&& td
->evtdev
)
1476 hrtimer_interrupt(td
->evtdev
);
1480 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1482 * hrtimer_peek_ahead_timers will peek at the timer queue of
1483 * the current cpu and check if there are any timers for which
1484 * the soft expires time has passed. If any such timers exist,
1485 * they are run immediately and then removed from the timer queue.
1488 void hrtimer_peek_ahead_timers(void)
1490 unsigned long flags
;
1492 local_irq_save(flags
);
1493 __hrtimer_peek_ahead_timers();
1494 local_irq_restore(flags
);
1497 static void run_hrtimer_softirq(struct softirq_action
*h
)
1499 hrtimer_peek_ahead_timers();
1502 #else /* CONFIG_HIGH_RES_TIMERS */
1504 static inline void __hrtimer_peek_ahead_timers(void) { }
1506 #endif /* !CONFIG_HIGH_RES_TIMERS */
1509 * Called from timer softirq every jiffy, expire hrtimers:
1511 * For HRT its the fall back code to run the softirq in the timer
1512 * softirq context in case the hrtimer initialization failed or has
1513 * not been done yet.
1515 void hrtimer_run_pending(void)
1517 if (hrtimer_hres_active())
1521 * This _is_ ugly: We have to check in the softirq context,
1522 * whether we can switch to highres and / or nohz mode. The
1523 * clocksource switch happens in the timer interrupt with
1524 * xtime_lock held. Notification from there only sets the
1525 * check bit in the tick_oneshot code, otherwise we might
1526 * deadlock vs. xtime_lock.
1528 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1529 hrtimer_switch_to_hres();
1533 * Called from hardirq context every jiffy
1535 void hrtimer_run_queues(void)
1537 struct timerqueue_node
*node
;
1538 struct hrtimer_cpu_base
*cpu_base
= &__get_cpu_var(hrtimer_bases
);
1539 struct hrtimer_clock_base
*base
;
1540 int index
, gettime
= 1;
1542 if (hrtimer_hres_active())
1545 for (index
= 0; index
< HRTIMER_MAX_CLOCK_BASES
; index
++) {
1546 base
= &cpu_base
->clock_base
[index
];
1547 if (!timerqueue_getnext(&base
->active
))
1551 hrtimer_get_softirq_time(cpu_base
);
1555 raw_spin_lock(&cpu_base
->lock
);
1557 while ((node
= timerqueue_getnext(&base
->active
))) {
1558 struct hrtimer
*timer
;
1560 timer
= container_of(node
, struct hrtimer
, node
);
1561 if (base
->softirq_time
.tv64
<=
1562 hrtimer_get_expires_tv64(timer
))
1565 __run_hrtimer(timer
, &base
->softirq_time
);
1567 raw_spin_unlock(&cpu_base
->lock
);
1572 * Sleep related functions:
1574 static enum hrtimer_restart
hrtimer_wakeup(struct hrtimer
*timer
)
1576 struct hrtimer_sleeper
*t
=
1577 container_of(timer
, struct hrtimer_sleeper
, timer
);
1578 struct task_struct
*task
= t
->task
;
1582 wake_up_process(task
);
1584 return HRTIMER_NORESTART
;
1587 void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
, struct task_struct
*task
)
1589 sl
->timer
.function
= hrtimer_wakeup
;
1592 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper
);
1594 static int __sched
do_nanosleep(struct hrtimer_sleeper
*t
, enum hrtimer_mode mode
)
1596 hrtimer_init_sleeper(t
, current
);
1599 set_current_state(TASK_INTERRUPTIBLE
);
1600 hrtimer_start_expires(&t
->timer
, mode
);
1601 if (!hrtimer_active(&t
->timer
))
1604 if (likely(t
->task
))
1605 freezable_schedule();
1607 hrtimer_cancel(&t
->timer
);
1608 mode
= HRTIMER_MODE_ABS
;
1610 } while (t
->task
&& !signal_pending(current
));
1612 __set_current_state(TASK_RUNNING
);
1614 return t
->task
== NULL
;
1617 static int update_rmtp(struct hrtimer
*timer
, struct timespec __user
*rmtp
)
1619 struct timespec rmt
;
1622 rem
= hrtimer_expires_remaining(timer
);
1625 rmt
= ktime_to_timespec(rem
);
1627 if (copy_to_user(rmtp
, &rmt
, sizeof(*rmtp
)))
1633 long __sched
hrtimer_nanosleep_restart(struct restart_block
*restart
)
1635 struct hrtimer_sleeper t
;
1636 struct timespec __user
*rmtp
;
1639 hrtimer_init_on_stack(&t
.timer
, restart
->nanosleep
.clockid
,
1641 hrtimer_set_expires_tv64(&t
.timer
, restart
->nanosleep
.expires
);
1643 if (do_nanosleep(&t
, HRTIMER_MODE_ABS
))
1646 rmtp
= restart
->nanosleep
.rmtp
;
1648 ret
= update_rmtp(&t
.timer
, rmtp
);
1653 /* The other values in restart are already filled in */
1654 ret
= -ERESTART_RESTARTBLOCK
;
1656 destroy_hrtimer_on_stack(&t
.timer
);
1660 long hrtimer_nanosleep(struct timespec
*rqtp
, struct timespec __user
*rmtp
,
1661 const enum hrtimer_mode mode
, const clockid_t clockid
)
1663 struct restart_block
*restart
;
1664 struct hrtimer_sleeper t
;
1666 unsigned long slack
;
1668 slack
= current
->timer_slack_ns
;
1669 if (rt_task(current
))
1672 hrtimer_init_on_stack(&t
.timer
, clockid
, mode
);
1673 hrtimer_set_expires_range_ns(&t
.timer
, timespec_to_ktime(*rqtp
), slack
);
1674 if (do_nanosleep(&t
, mode
))
1677 /* Absolute timers do not update the rmtp value and restart: */
1678 if (mode
== HRTIMER_MODE_ABS
) {
1679 ret
= -ERESTARTNOHAND
;
1684 ret
= update_rmtp(&t
.timer
, rmtp
);
1689 restart
= ¤t_thread_info()->restart_block
;
1690 restart
->fn
= hrtimer_nanosleep_restart
;
1691 restart
->nanosleep
.clockid
= t
.timer
.base
->clockid
;
1692 restart
->nanosleep
.rmtp
= rmtp
;
1693 restart
->nanosleep
.expires
= hrtimer_get_expires_tv64(&t
.timer
);
1695 ret
= -ERESTART_RESTARTBLOCK
;
1697 destroy_hrtimer_on_stack(&t
.timer
);
1701 SYSCALL_DEFINE2(nanosleep
, struct timespec __user
*, rqtp
,
1702 struct timespec __user
*, rmtp
)
1706 if (copy_from_user(&tu
, rqtp
, sizeof(tu
)))
1709 if (!timespec_valid(&tu
))
1712 return hrtimer_nanosleep(&tu
, rmtp
, HRTIMER_MODE_REL
, CLOCK_MONOTONIC
);
1716 * Functions related to boot-time initialization:
1718 static void __cpuinit
init_hrtimers_cpu(int cpu
)
1720 struct hrtimer_cpu_base
*cpu_base
= &per_cpu(hrtimer_bases
, cpu
);
1723 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1724 cpu_base
->clock_base
[i
].cpu_base
= cpu_base
;
1725 timerqueue_init_head(&cpu_base
->clock_base
[i
].active
);
1728 hrtimer_init_hres(cpu_base
);
1731 #ifdef CONFIG_HOTPLUG_CPU
1733 static void migrate_hrtimer_list(struct hrtimer_clock_base
*old_base
,
1734 struct hrtimer_clock_base
*new_base
)
1736 struct hrtimer
*timer
;
1737 struct timerqueue_node
*node
;
1739 while ((node
= timerqueue_getnext(&old_base
->active
))) {
1740 timer
= container_of(node
, struct hrtimer
, node
);
1741 BUG_ON(hrtimer_callback_running(timer
));
1742 debug_deactivate(timer
);
1745 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1746 * timer could be seen as !active and just vanish away
1747 * under us on another CPU
1749 __remove_hrtimer(timer
, old_base
, HRTIMER_STATE_MIGRATE
, 0);
1750 timer
->base
= new_base
;
1752 * Enqueue the timers on the new cpu. This does not
1753 * reprogram the event device in case the timer
1754 * expires before the earliest on this CPU, but we run
1755 * hrtimer_interrupt after we migrated everything to
1756 * sort out already expired timers and reprogram the
1759 enqueue_hrtimer(timer
, new_base
);
1761 /* Clear the migration state bit */
1762 timer
->state
&= ~HRTIMER_STATE_MIGRATE
;
1766 static void migrate_hrtimers(int scpu
)
1768 struct hrtimer_cpu_base
*old_base
, *new_base
;
1771 BUG_ON(cpu_online(scpu
));
1772 tick_cancel_sched_timer(scpu
);
1774 local_irq_disable();
1775 old_base
= &per_cpu(hrtimer_bases
, scpu
);
1776 new_base
= &__get_cpu_var(hrtimer_bases
);
1778 * The caller is globally serialized and nobody else
1779 * takes two locks at once, deadlock is not possible.
1781 raw_spin_lock(&new_base
->lock
);
1782 raw_spin_lock_nested(&old_base
->lock
, SINGLE_DEPTH_NESTING
);
1784 for (i
= 0; i
< HRTIMER_MAX_CLOCK_BASES
; i
++) {
1785 migrate_hrtimer_list(&old_base
->clock_base
[i
],
1786 &new_base
->clock_base
[i
]);
1789 raw_spin_unlock(&old_base
->lock
);
1790 raw_spin_unlock(&new_base
->lock
);
1792 /* Check, if we got expired work to do */
1793 __hrtimer_peek_ahead_timers();
1797 #endif /* CONFIG_HOTPLUG_CPU */
1799 static int __cpuinit
hrtimer_cpu_notify(struct notifier_block
*self
,
1800 unsigned long action
, void *hcpu
)
1802 int scpu
= (long)hcpu
;
1806 case CPU_UP_PREPARE
:
1807 case CPU_UP_PREPARE_FROZEN
:
1808 init_hrtimers_cpu(scpu
);
1811 #ifdef CONFIG_HOTPLUG_CPU
1813 case CPU_DYING_FROZEN
:
1814 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING
, &scpu
);
1817 case CPU_DEAD_FROZEN
:
1819 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD
, &scpu
);
1820 migrate_hrtimers(scpu
);
1832 static struct notifier_block __cpuinitdata hrtimers_nb
= {
1833 .notifier_call
= hrtimer_cpu_notify
,
1836 void __init
hrtimers_init(void)
1838 hrtimer_cpu_notify(&hrtimers_nb
, (unsigned long)CPU_UP_PREPARE
,
1839 (void *)(long)smp_processor_id());
1840 register_cpu_notifier(&hrtimers_nb
);
1841 #ifdef CONFIG_HIGH_RES_TIMERS
1842 open_softirq(HRTIMER_SOFTIRQ
, run_hrtimer_softirq
);
1847 * schedule_hrtimeout_range_clock - sleep until timeout
1848 * @expires: timeout value (ktime_t)
1849 * @delta: slack in expires timeout (ktime_t)
1850 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1851 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1854 schedule_hrtimeout_range_clock(ktime_t
*expires
, unsigned long delta
,
1855 const enum hrtimer_mode mode
, int clock
)
1857 struct hrtimer_sleeper t
;
1860 * Optimize when a zero timeout value is given. It does not
1861 * matter whether this is an absolute or a relative time.
1863 if (expires
&& !expires
->tv64
) {
1864 __set_current_state(TASK_RUNNING
);
1869 * A NULL parameter means "infinite"
1873 __set_current_state(TASK_RUNNING
);
1877 hrtimer_init_on_stack(&t
.timer
, clock
, mode
);
1878 hrtimer_set_expires_range_ns(&t
.timer
, *expires
, delta
);
1880 hrtimer_init_sleeper(&t
, current
);
1882 hrtimer_start_expires(&t
.timer
, mode
);
1883 if (!hrtimer_active(&t
.timer
))
1889 hrtimer_cancel(&t
.timer
);
1890 destroy_hrtimer_on_stack(&t
.timer
);
1892 __set_current_state(TASK_RUNNING
);
1894 return !t
.task
? 0 : -EINTR
;
1898 * schedule_hrtimeout_range - sleep until timeout
1899 * @expires: timeout value (ktime_t)
1900 * @delta: slack in expires timeout (ktime_t)
1901 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1903 * Make the current task sleep until the given expiry time has
1904 * elapsed. The routine will return immediately unless
1905 * the current task state has been set (see set_current_state()).
1907 * The @delta argument gives the kernel the freedom to schedule the
1908 * actual wakeup to a time that is both power and performance friendly.
1909 * The kernel give the normal best effort behavior for "@expires+@delta",
1910 * but may decide to fire the timer earlier, but no earlier than @expires.
1912 * You can set the task state as follows -
1914 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1915 * pass before the routine returns.
1917 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1918 * delivered to the current task.
1920 * The current task state is guaranteed to be TASK_RUNNING when this
1923 * Returns 0 when the timer has expired otherwise -EINTR
1925 int __sched
schedule_hrtimeout_range(ktime_t
*expires
, unsigned long delta
,
1926 const enum hrtimer_mode mode
)
1928 return schedule_hrtimeout_range_clock(expires
, delta
, mode
,
1931 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range
);
1934 * schedule_hrtimeout - sleep until timeout
1935 * @expires: timeout value (ktime_t)
1936 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1938 * Make the current task sleep until the given expiry time has
1939 * elapsed. The routine will return immediately unless
1940 * the current task state has been set (see set_current_state()).
1942 * You can set the task state as follows -
1944 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1945 * pass before the routine returns.
1947 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1948 * delivered to the current task.
1950 * The current task state is guaranteed to be TASK_RUNNING when this
1953 * Returns 0 when the timer has expired otherwise -EINTR
1955 int __sched
schedule_hrtimeout(ktime_t
*expires
,
1956 const enum hrtimer_mode mode
)
1958 return schedule_hrtimeout_range(expires
, 0, mode
);
1960 EXPORT_SYMBOL_GPL(schedule_hrtimeout
);