xref: /openbmc/linux/kernel/time/hrtimer.c (revision 571af55a31d3652ac1f758f116835a76d0335661)
15cee9645SThomas Gleixner /*
25cee9645SThomas Gleixner  *  linux/kernel/hrtimer.c
35cee9645SThomas Gleixner  *
45cee9645SThomas Gleixner  *  Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
55cee9645SThomas Gleixner  *  Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
65cee9645SThomas Gleixner  *  Copyright(C) 2006-2007  Timesys Corp., Thomas Gleixner
75cee9645SThomas Gleixner  *
85cee9645SThomas Gleixner  *  High-resolution kernel timers
95cee9645SThomas Gleixner  *
105cee9645SThomas Gleixner  *  In contrast to the low-resolution timeout API implemented in
115cee9645SThomas Gleixner  *  kernel/timer.c, hrtimers provide finer resolution and accuracy
125cee9645SThomas Gleixner  *  depending on system configuration and capabilities.
135cee9645SThomas Gleixner  *
145cee9645SThomas Gleixner  *  These timers are currently used for:
155cee9645SThomas Gleixner  *   - itimers
165cee9645SThomas Gleixner  *   - POSIX timers
175cee9645SThomas Gleixner  *   - nanosleep
185cee9645SThomas Gleixner  *   - precise in-kernel timing
195cee9645SThomas Gleixner  *
205cee9645SThomas Gleixner  *  Started by: Thomas Gleixner and Ingo Molnar
215cee9645SThomas Gleixner  *
225cee9645SThomas Gleixner  *  Credits:
235cee9645SThomas Gleixner  *	based on kernel/timer.c
245cee9645SThomas Gleixner  *
255cee9645SThomas Gleixner  *	Help, testing, suggestions, bugfixes, improvements were
265cee9645SThomas Gleixner  *	provided by:
275cee9645SThomas Gleixner  *
285cee9645SThomas Gleixner  *	George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
295cee9645SThomas Gleixner  *	et. al.
305cee9645SThomas Gleixner  *
315cee9645SThomas Gleixner  *  For licencing details see kernel-base/COPYING
325cee9645SThomas Gleixner  */
335cee9645SThomas Gleixner 
345cee9645SThomas Gleixner #include <linux/cpu.h>
355cee9645SThomas Gleixner #include <linux/export.h>
365cee9645SThomas Gleixner #include <linux/percpu.h>
375cee9645SThomas Gleixner #include <linux/hrtimer.h>
385cee9645SThomas Gleixner #include <linux/notifier.h>
395cee9645SThomas Gleixner #include <linux/syscalls.h>
405cee9645SThomas Gleixner #include <linux/kallsyms.h>
415cee9645SThomas Gleixner #include <linux/interrupt.h>
425cee9645SThomas Gleixner #include <linux/tick.h>
435cee9645SThomas Gleixner #include <linux/seq_file.h>
445cee9645SThomas Gleixner #include <linux/err.h>
455cee9645SThomas Gleixner #include <linux/debugobjects.h>
465cee9645SThomas Gleixner #include <linux/sched.h>
475cee9645SThomas Gleixner #include <linux/sched/sysctl.h>
485cee9645SThomas Gleixner #include <linux/sched/rt.h>
495cee9645SThomas Gleixner #include <linux/sched/deadline.h>
505cee9645SThomas Gleixner #include <linux/timer.h>
515cee9645SThomas Gleixner #include <linux/freezer.h>
525cee9645SThomas Gleixner 
535cee9645SThomas Gleixner #include <asm/uaccess.h>
545cee9645SThomas Gleixner 
555cee9645SThomas Gleixner #include <trace/events/timer.h>
565cee9645SThomas Gleixner 
57c1797bafSThomas Gleixner #include "tick-internal.h"
588b094cd0SThomas Gleixner 
595cee9645SThomas Gleixner /*
605cee9645SThomas Gleixner  * The timer bases:
615cee9645SThomas Gleixner  *
62*571af55aSZhen Lei  * There are more clockids than hrtimer bases. Thus, we index
635cee9645SThomas Gleixner  * into the timer bases by the hrtimer_base_type enum. When trying
645cee9645SThomas Gleixner  * to reach a base using a clockid, hrtimer_clockid_to_base()
655cee9645SThomas Gleixner  * is used to convert from clockid to the proper hrtimer_base_type.
665cee9645SThomas Gleixner  */
675cee9645SThomas Gleixner DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
685cee9645SThomas Gleixner {
695cee9645SThomas Gleixner 	.lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
70887d9dc9SPeter Zijlstra 	.seq = SEQCNT_ZERO(hrtimer_bases.seq),
715cee9645SThomas Gleixner 	.clock_base =
725cee9645SThomas Gleixner 	{
735cee9645SThomas Gleixner 		{
745cee9645SThomas Gleixner 			.index = HRTIMER_BASE_MONOTONIC,
755cee9645SThomas Gleixner 			.clockid = CLOCK_MONOTONIC,
765cee9645SThomas Gleixner 			.get_time = &ktime_get,
775cee9645SThomas Gleixner 		},
785cee9645SThomas Gleixner 		{
795cee9645SThomas Gleixner 			.index = HRTIMER_BASE_REALTIME,
805cee9645SThomas Gleixner 			.clockid = CLOCK_REALTIME,
815cee9645SThomas Gleixner 			.get_time = &ktime_get_real,
825cee9645SThomas Gleixner 		},
835cee9645SThomas Gleixner 		{
845cee9645SThomas Gleixner 			.index = HRTIMER_BASE_BOOTTIME,
855cee9645SThomas Gleixner 			.clockid = CLOCK_BOOTTIME,
865cee9645SThomas Gleixner 			.get_time = &ktime_get_boottime,
875cee9645SThomas Gleixner 		},
885cee9645SThomas Gleixner 		{
895cee9645SThomas Gleixner 			.index = HRTIMER_BASE_TAI,
905cee9645SThomas Gleixner 			.clockid = CLOCK_TAI,
915cee9645SThomas Gleixner 			.get_time = &ktime_get_clocktai,
925cee9645SThomas Gleixner 		},
935cee9645SThomas Gleixner 	}
945cee9645SThomas Gleixner };
955cee9645SThomas Gleixner 
965cee9645SThomas Gleixner static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
975cee9645SThomas Gleixner 	[CLOCK_REALTIME]	= HRTIMER_BASE_REALTIME,
985cee9645SThomas Gleixner 	[CLOCK_MONOTONIC]	= HRTIMER_BASE_MONOTONIC,
995cee9645SThomas Gleixner 	[CLOCK_BOOTTIME]	= HRTIMER_BASE_BOOTTIME,
1005cee9645SThomas Gleixner 	[CLOCK_TAI]		= HRTIMER_BASE_TAI,
1015cee9645SThomas Gleixner };
1025cee9645SThomas Gleixner 
1035cee9645SThomas Gleixner static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1045cee9645SThomas Gleixner {
1055cee9645SThomas Gleixner 	return hrtimer_clock_to_base_table[clock_id];
1065cee9645SThomas Gleixner }
1075cee9645SThomas Gleixner 
1085cee9645SThomas Gleixner /*
1095cee9645SThomas Gleixner  * Functions and macros which are different for UP/SMP systems are kept in a
1105cee9645SThomas Gleixner  * single place
1115cee9645SThomas Gleixner  */
1125cee9645SThomas Gleixner #ifdef CONFIG_SMP
1135cee9645SThomas Gleixner 
1145cee9645SThomas Gleixner /*
115887d9dc9SPeter Zijlstra  * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
116887d9dc9SPeter Zijlstra  * such that hrtimer_callback_running() can unconditionally dereference
117887d9dc9SPeter Zijlstra  * timer->base->cpu_base
118887d9dc9SPeter Zijlstra  */
119887d9dc9SPeter Zijlstra static struct hrtimer_cpu_base migration_cpu_base = {
120887d9dc9SPeter Zijlstra 	.seq = SEQCNT_ZERO(migration_cpu_base),
121887d9dc9SPeter Zijlstra 	.clock_base = { { .cpu_base = &migration_cpu_base, }, },
122887d9dc9SPeter Zijlstra };
123887d9dc9SPeter Zijlstra 
124887d9dc9SPeter Zijlstra #define migration_base	migration_cpu_base.clock_base[0]
125887d9dc9SPeter Zijlstra 
126887d9dc9SPeter Zijlstra /*
1275cee9645SThomas Gleixner  * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
1285cee9645SThomas Gleixner  * means that all timers which are tied to this base via timer->base are
1295cee9645SThomas Gleixner  * locked, and the base itself is locked too.
1305cee9645SThomas Gleixner  *
1315cee9645SThomas Gleixner  * So __run_timers/migrate_timers can safely modify all timers which could
1325cee9645SThomas Gleixner  * be found on the lists/queues.
1335cee9645SThomas Gleixner  *
1345cee9645SThomas Gleixner  * When the timer's base is locked, and the timer removed from list, it is
135887d9dc9SPeter Zijlstra  * possible to set timer->base = &migration_base and drop the lock: the timer
136887d9dc9SPeter Zijlstra  * remains locked.
1375cee9645SThomas Gleixner  */
1385cee9645SThomas Gleixner static
1395cee9645SThomas Gleixner struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
1405cee9645SThomas Gleixner 					     unsigned long *flags)
1415cee9645SThomas Gleixner {
1425cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
1435cee9645SThomas Gleixner 
1445cee9645SThomas Gleixner 	for (;;) {
1455cee9645SThomas Gleixner 		base = timer->base;
146887d9dc9SPeter Zijlstra 		if (likely(base != &migration_base)) {
1475cee9645SThomas Gleixner 			raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
1485cee9645SThomas Gleixner 			if (likely(base == timer->base))
1495cee9645SThomas Gleixner 				return base;
1505cee9645SThomas Gleixner 			/* The timer has migrated to another CPU: */
1515cee9645SThomas Gleixner 			raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
1525cee9645SThomas Gleixner 		}
1535cee9645SThomas Gleixner 		cpu_relax();
1545cee9645SThomas Gleixner 	}
1555cee9645SThomas Gleixner }
1565cee9645SThomas Gleixner 
1575cee9645SThomas Gleixner /*
1585cee9645SThomas Gleixner  * With HIGHRES=y we do not migrate the timer when it is expiring
1595cee9645SThomas Gleixner  * before the next event on the target cpu because we cannot reprogram
1605cee9645SThomas Gleixner  * the target cpu hardware and we would cause it to fire late.
1615cee9645SThomas Gleixner  *
1625cee9645SThomas Gleixner  * Called with cpu_base->lock of target cpu held.
1635cee9645SThomas Gleixner  */
1645cee9645SThomas Gleixner static int
1655cee9645SThomas Gleixner hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
1665cee9645SThomas Gleixner {
1675cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
1685cee9645SThomas Gleixner 	ktime_t expires;
1695cee9645SThomas Gleixner 
1705cee9645SThomas Gleixner 	if (!new_base->cpu_base->hres_active)
1715cee9645SThomas Gleixner 		return 0;
1725cee9645SThomas Gleixner 
1735cee9645SThomas Gleixner 	expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
1745cee9645SThomas Gleixner 	return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
1755cee9645SThomas Gleixner #else
1765cee9645SThomas Gleixner 	return 0;
1775cee9645SThomas Gleixner #endif
1785cee9645SThomas Gleixner }
1795cee9645SThomas Gleixner 
180bc7a34b8SThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
181bc7a34b8SThomas Gleixner static inline
182bc7a34b8SThomas Gleixner struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
183bc7a34b8SThomas Gleixner 					 int pinned)
184bc7a34b8SThomas Gleixner {
185bc7a34b8SThomas Gleixner 	if (pinned || !base->migration_enabled)
186662b3e19SFrederic Weisbecker 		return base;
187bc7a34b8SThomas Gleixner 	return &per_cpu(hrtimer_bases, get_nohz_timer_target());
188bc7a34b8SThomas Gleixner }
189bc7a34b8SThomas Gleixner #else
190bc7a34b8SThomas Gleixner static inline
191bc7a34b8SThomas Gleixner struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
192bc7a34b8SThomas Gleixner 					 int pinned)
193bc7a34b8SThomas Gleixner {
194662b3e19SFrederic Weisbecker 	return base;
195bc7a34b8SThomas Gleixner }
196bc7a34b8SThomas Gleixner #endif
197bc7a34b8SThomas Gleixner 
1985cee9645SThomas Gleixner /*
199b48362d8SFrederic Weisbecker  * We switch the timer base to a power-optimized selected CPU target,
200b48362d8SFrederic Weisbecker  * if:
201b48362d8SFrederic Weisbecker  *	- NO_HZ_COMMON is enabled
202b48362d8SFrederic Weisbecker  *	- timer migration is enabled
203b48362d8SFrederic Weisbecker  *	- the timer callback is not running
204b48362d8SFrederic Weisbecker  *	- the timer is not the first expiring timer on the new target
205b48362d8SFrederic Weisbecker  *
206b48362d8SFrederic Weisbecker  * If one of the above requirements is not fulfilled we move the timer
207b48362d8SFrederic Weisbecker  * to the current CPU or leave it on the previously assigned CPU if
208b48362d8SFrederic Weisbecker  * the timer callback is currently running.
2095cee9645SThomas Gleixner  */
2105cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
2115cee9645SThomas Gleixner switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
2125cee9645SThomas Gleixner 		    int pinned)
2135cee9645SThomas Gleixner {
214b48362d8SFrederic Weisbecker 	struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
2155cee9645SThomas Gleixner 	struct hrtimer_clock_base *new_base;
2165cee9645SThomas Gleixner 	int basenum = base->index;
2175cee9645SThomas Gleixner 
218b48362d8SFrederic Weisbecker 	this_cpu_base = this_cpu_ptr(&hrtimer_bases);
219b48362d8SFrederic Weisbecker 	new_cpu_base = get_target_base(this_cpu_base, pinned);
2205cee9645SThomas Gleixner again:
2215cee9645SThomas Gleixner 	new_base = &new_cpu_base->clock_base[basenum];
2225cee9645SThomas Gleixner 
2235cee9645SThomas Gleixner 	if (base != new_base) {
2245cee9645SThomas Gleixner 		/*
2255cee9645SThomas Gleixner 		 * We are trying to move timer to new_base.
2265cee9645SThomas Gleixner 		 * However we can't change timer's base while it is running,
2275cee9645SThomas Gleixner 		 * so we keep it on the same CPU. No hassle vs. reprogramming
2285cee9645SThomas Gleixner 		 * the event source in the high resolution case. The softirq
2295cee9645SThomas Gleixner 		 * code will take care of this when the timer function has
2305cee9645SThomas Gleixner 		 * completed. There is no conflict as we hold the lock until
2315cee9645SThomas Gleixner 		 * the timer is enqueued.
2325cee9645SThomas Gleixner 		 */
2335cee9645SThomas Gleixner 		if (unlikely(hrtimer_callback_running(timer)))
2345cee9645SThomas Gleixner 			return base;
2355cee9645SThomas Gleixner 
236887d9dc9SPeter Zijlstra 		/* See the comment in lock_hrtimer_base() */
237887d9dc9SPeter Zijlstra 		timer->base = &migration_base;
2385cee9645SThomas Gleixner 		raw_spin_unlock(&base->cpu_base->lock);
2395cee9645SThomas Gleixner 		raw_spin_lock(&new_base->cpu_base->lock);
2405cee9645SThomas Gleixner 
241b48362d8SFrederic Weisbecker 		if (new_cpu_base != this_cpu_base &&
242bc7a34b8SThomas Gleixner 		    hrtimer_check_target(timer, new_base)) {
2435cee9645SThomas Gleixner 			raw_spin_unlock(&new_base->cpu_base->lock);
2445cee9645SThomas Gleixner 			raw_spin_lock(&base->cpu_base->lock);
245b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
2465cee9645SThomas Gleixner 			timer->base = base;
2475cee9645SThomas Gleixner 			goto again;
2485cee9645SThomas Gleixner 		}
2495cee9645SThomas Gleixner 		timer->base = new_base;
2505cee9645SThomas Gleixner 	} else {
251b48362d8SFrederic Weisbecker 		if (new_cpu_base != this_cpu_base &&
252bc7a34b8SThomas Gleixner 		    hrtimer_check_target(timer, new_base)) {
253b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
2545cee9645SThomas Gleixner 			goto again;
2555cee9645SThomas Gleixner 		}
2565cee9645SThomas Gleixner 	}
2575cee9645SThomas Gleixner 	return new_base;
2585cee9645SThomas Gleixner }
2595cee9645SThomas Gleixner 
2605cee9645SThomas Gleixner #else /* CONFIG_SMP */
2615cee9645SThomas Gleixner 
2625cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
2635cee9645SThomas Gleixner lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
2645cee9645SThomas Gleixner {
2655cee9645SThomas Gleixner 	struct hrtimer_clock_base *base = timer->base;
2665cee9645SThomas Gleixner 
2675cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
2685cee9645SThomas Gleixner 
2695cee9645SThomas Gleixner 	return base;
2705cee9645SThomas Gleixner }
2715cee9645SThomas Gleixner 
2725cee9645SThomas Gleixner # define switch_hrtimer_base(t, b, p)	(b)
2735cee9645SThomas Gleixner 
2745cee9645SThomas Gleixner #endif	/* !CONFIG_SMP */
2755cee9645SThomas Gleixner 
2765cee9645SThomas Gleixner /*
2775cee9645SThomas Gleixner  * Functions for the union type storage format of ktime_t which are
2785cee9645SThomas Gleixner  * too large for inlining:
2795cee9645SThomas Gleixner  */
2805cee9645SThomas Gleixner #if BITS_PER_LONG < 64
2815cee9645SThomas Gleixner /*
2825cee9645SThomas Gleixner  * Divide a ktime value by a nanosecond value
2835cee9645SThomas Gleixner  */
284f7bcb70eSJohn Stultz s64 __ktime_divns(const ktime_t kt, s64 div)
2855cee9645SThomas Gleixner {
2865cee9645SThomas Gleixner 	int sft = 0;
287f7bcb70eSJohn Stultz 	s64 dclc;
288f7bcb70eSJohn Stultz 	u64 tmp;
2895cee9645SThomas Gleixner 
2905cee9645SThomas Gleixner 	dclc = ktime_to_ns(kt);
291f7bcb70eSJohn Stultz 	tmp = dclc < 0 ? -dclc : dclc;
292f7bcb70eSJohn Stultz 
2935cee9645SThomas Gleixner 	/* Make sure the divisor is less than 2^32: */
2945cee9645SThomas Gleixner 	while (div >> 32) {
2955cee9645SThomas Gleixner 		sft++;
2965cee9645SThomas Gleixner 		div >>= 1;
2975cee9645SThomas Gleixner 	}
298f7bcb70eSJohn Stultz 	tmp >>= sft;
299f7bcb70eSJohn Stultz 	do_div(tmp, (unsigned long) div);
300f7bcb70eSJohn Stultz 	return dclc < 0 ? -tmp : tmp;
3015cee9645SThomas Gleixner }
3028b618628SNicolas Pitre EXPORT_SYMBOL_GPL(__ktime_divns);
3035cee9645SThomas Gleixner #endif /* BITS_PER_LONG >= 64 */
3045cee9645SThomas Gleixner 
3055cee9645SThomas Gleixner /*
3065cee9645SThomas Gleixner  * Add two ktime values and do a safety check for overflow:
3075cee9645SThomas Gleixner  */
3085cee9645SThomas Gleixner ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
3095cee9645SThomas Gleixner {
3105cee9645SThomas Gleixner 	ktime_t res = ktime_add(lhs, rhs);
3115cee9645SThomas Gleixner 
3125cee9645SThomas Gleixner 	/*
3135cee9645SThomas Gleixner 	 * We use KTIME_SEC_MAX here, the maximum timeout which we can
3145cee9645SThomas Gleixner 	 * return to user space in a timespec:
3155cee9645SThomas Gleixner 	 */
3165cee9645SThomas Gleixner 	if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
3175cee9645SThomas Gleixner 		res = ktime_set(KTIME_SEC_MAX, 0);
3185cee9645SThomas Gleixner 
3195cee9645SThomas Gleixner 	return res;
3205cee9645SThomas Gleixner }
3215cee9645SThomas Gleixner 
3225cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_add_safe);
3235cee9645SThomas Gleixner 
3245cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
3255cee9645SThomas Gleixner 
3265cee9645SThomas Gleixner static struct debug_obj_descr hrtimer_debug_descr;
3275cee9645SThomas Gleixner 
3285cee9645SThomas Gleixner static void *hrtimer_debug_hint(void *addr)
3295cee9645SThomas Gleixner {
3305cee9645SThomas Gleixner 	return ((struct hrtimer *) addr)->function;
3315cee9645SThomas Gleixner }
3325cee9645SThomas Gleixner 
3335cee9645SThomas Gleixner /*
3345cee9645SThomas Gleixner  * fixup_init is called when:
3355cee9645SThomas Gleixner  * - an active object is initialized
3365cee9645SThomas Gleixner  */
3375cee9645SThomas Gleixner static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
3385cee9645SThomas Gleixner {
3395cee9645SThomas Gleixner 	struct hrtimer *timer = addr;
3405cee9645SThomas Gleixner 
3415cee9645SThomas Gleixner 	switch (state) {
3425cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3435cee9645SThomas Gleixner 		hrtimer_cancel(timer);
3445cee9645SThomas Gleixner 		debug_object_init(timer, &hrtimer_debug_descr);
3455cee9645SThomas Gleixner 		return 1;
3465cee9645SThomas Gleixner 	default:
3475cee9645SThomas Gleixner 		return 0;
3485cee9645SThomas Gleixner 	}
3495cee9645SThomas Gleixner }
3505cee9645SThomas Gleixner 
3515cee9645SThomas Gleixner /*
3525cee9645SThomas Gleixner  * fixup_activate is called when:
3535cee9645SThomas Gleixner  * - an active object is activated
3545cee9645SThomas Gleixner  * - an unknown object is activated (might be a statically initialized object)
3555cee9645SThomas Gleixner  */
3565cee9645SThomas Gleixner static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
3575cee9645SThomas Gleixner {
3585cee9645SThomas Gleixner 	switch (state) {
3595cee9645SThomas Gleixner 
3605cee9645SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
3615cee9645SThomas Gleixner 		WARN_ON_ONCE(1);
3625cee9645SThomas Gleixner 		return 0;
3635cee9645SThomas Gleixner 
3645cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3655cee9645SThomas Gleixner 		WARN_ON(1);
3665cee9645SThomas Gleixner 
3675cee9645SThomas Gleixner 	default:
3685cee9645SThomas Gleixner 		return 0;
3695cee9645SThomas Gleixner 	}
3705cee9645SThomas Gleixner }
3715cee9645SThomas Gleixner 
3725cee9645SThomas Gleixner /*
3735cee9645SThomas Gleixner  * fixup_free is called when:
3745cee9645SThomas Gleixner  * - an active object is freed
3755cee9645SThomas Gleixner  */
3765cee9645SThomas Gleixner static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
3775cee9645SThomas Gleixner {
3785cee9645SThomas Gleixner 	struct hrtimer *timer = addr;
3795cee9645SThomas Gleixner 
3805cee9645SThomas Gleixner 	switch (state) {
3815cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3825cee9645SThomas Gleixner 		hrtimer_cancel(timer);
3835cee9645SThomas Gleixner 		debug_object_free(timer, &hrtimer_debug_descr);
3845cee9645SThomas Gleixner 		return 1;
3855cee9645SThomas Gleixner 	default:
3865cee9645SThomas Gleixner 		return 0;
3875cee9645SThomas Gleixner 	}
3885cee9645SThomas Gleixner }
3895cee9645SThomas Gleixner 
3905cee9645SThomas Gleixner static struct debug_obj_descr hrtimer_debug_descr = {
3915cee9645SThomas Gleixner 	.name		= "hrtimer",
3925cee9645SThomas Gleixner 	.debug_hint	= hrtimer_debug_hint,
3935cee9645SThomas Gleixner 	.fixup_init	= hrtimer_fixup_init,
3945cee9645SThomas Gleixner 	.fixup_activate	= hrtimer_fixup_activate,
3955cee9645SThomas Gleixner 	.fixup_free	= hrtimer_fixup_free,
3965cee9645SThomas Gleixner };
3975cee9645SThomas Gleixner 
3985cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer)
3995cee9645SThomas Gleixner {
4005cee9645SThomas Gleixner 	debug_object_init(timer, &hrtimer_debug_descr);
4015cee9645SThomas Gleixner }
4025cee9645SThomas Gleixner 
4035cee9645SThomas Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer)
4045cee9645SThomas Gleixner {
4055cee9645SThomas Gleixner 	debug_object_activate(timer, &hrtimer_debug_descr);
4065cee9645SThomas Gleixner }
4075cee9645SThomas Gleixner 
4085cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
4095cee9645SThomas Gleixner {
4105cee9645SThomas Gleixner 	debug_object_deactivate(timer, &hrtimer_debug_descr);
4115cee9645SThomas Gleixner }
4125cee9645SThomas Gleixner 
4135cee9645SThomas Gleixner static inline void debug_hrtimer_free(struct hrtimer *timer)
4145cee9645SThomas Gleixner {
4155cee9645SThomas Gleixner 	debug_object_free(timer, &hrtimer_debug_descr);
4165cee9645SThomas Gleixner }
4175cee9645SThomas Gleixner 
4185cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
4195cee9645SThomas Gleixner 			   enum hrtimer_mode mode);
4205cee9645SThomas Gleixner 
4215cee9645SThomas Gleixner void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
4225cee9645SThomas Gleixner 			   enum hrtimer_mode mode)
4235cee9645SThomas Gleixner {
4245cee9645SThomas Gleixner 	debug_object_init_on_stack(timer, &hrtimer_debug_descr);
4255cee9645SThomas Gleixner 	__hrtimer_init(timer, clock_id, mode);
4265cee9645SThomas Gleixner }
4275cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
4285cee9645SThomas Gleixner 
4295cee9645SThomas Gleixner void destroy_hrtimer_on_stack(struct hrtimer *timer)
4305cee9645SThomas Gleixner {
4315cee9645SThomas Gleixner 	debug_object_free(timer, &hrtimer_debug_descr);
4325cee9645SThomas Gleixner }
4335cee9645SThomas Gleixner 
4345cee9645SThomas Gleixner #else
4355cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer) { }
4365cee9645SThomas Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
4375cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
4385cee9645SThomas Gleixner #endif
4395cee9645SThomas Gleixner 
4405cee9645SThomas Gleixner static inline void
4415cee9645SThomas Gleixner debug_init(struct hrtimer *timer, clockid_t clockid,
4425cee9645SThomas Gleixner 	   enum hrtimer_mode mode)
4435cee9645SThomas Gleixner {
4445cee9645SThomas Gleixner 	debug_hrtimer_init(timer);
4455cee9645SThomas Gleixner 	trace_hrtimer_init(timer, clockid, mode);
4465cee9645SThomas Gleixner }
4475cee9645SThomas Gleixner 
4485cee9645SThomas Gleixner static inline void debug_activate(struct hrtimer *timer)
4495cee9645SThomas Gleixner {
4505cee9645SThomas Gleixner 	debug_hrtimer_activate(timer);
4515cee9645SThomas Gleixner 	trace_hrtimer_start(timer);
4525cee9645SThomas Gleixner }
4535cee9645SThomas Gleixner 
4545cee9645SThomas Gleixner static inline void debug_deactivate(struct hrtimer *timer)
4555cee9645SThomas Gleixner {
4565cee9645SThomas Gleixner 	debug_hrtimer_deactivate(timer);
4575cee9645SThomas Gleixner 	trace_hrtimer_cancel(timer);
4585cee9645SThomas Gleixner }
4595cee9645SThomas Gleixner 
4609bc74919SThomas Gleixner #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
461895bdfa7SThomas Gleixner static inline void hrtimer_update_next_timer(struct hrtimer_cpu_base *cpu_base,
462895bdfa7SThomas Gleixner 					     struct hrtimer *timer)
463895bdfa7SThomas Gleixner {
464895bdfa7SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
465895bdfa7SThomas Gleixner 	cpu_base->next_timer = timer;
466895bdfa7SThomas Gleixner #endif
467895bdfa7SThomas Gleixner }
468895bdfa7SThomas Gleixner 
4694ebbda52Skbuild test robot static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base)
4709bc74919SThomas Gleixner {
4719bc74919SThomas Gleixner 	struct hrtimer_clock_base *base = cpu_base->clock_base;
4729bc74919SThomas Gleixner 	ktime_t expires, expires_next = { .tv64 = KTIME_MAX };
47334aee88aSThomas Gleixner 	unsigned int active = cpu_base->active_bases;
4749bc74919SThomas Gleixner 
475895bdfa7SThomas Gleixner 	hrtimer_update_next_timer(cpu_base, NULL);
47634aee88aSThomas Gleixner 	for (; active; base++, active >>= 1) {
4779bc74919SThomas Gleixner 		struct timerqueue_node *next;
4789bc74919SThomas Gleixner 		struct hrtimer *timer;
4799bc74919SThomas Gleixner 
48034aee88aSThomas Gleixner 		if (!(active & 0x01))
4819bc74919SThomas Gleixner 			continue;
4829bc74919SThomas Gleixner 
48334aee88aSThomas Gleixner 		next = timerqueue_getnext(&base->active);
4849bc74919SThomas Gleixner 		timer = container_of(next, struct hrtimer, node);
4859bc74919SThomas Gleixner 		expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
486895bdfa7SThomas Gleixner 		if (expires.tv64 < expires_next.tv64) {
4879bc74919SThomas Gleixner 			expires_next = expires;
488895bdfa7SThomas Gleixner 			hrtimer_update_next_timer(cpu_base, timer);
489895bdfa7SThomas Gleixner 		}
4909bc74919SThomas Gleixner 	}
4919bc74919SThomas Gleixner 	/*
4929bc74919SThomas Gleixner 	 * clock_was_set() might have changed base->offset of any of
4939bc74919SThomas Gleixner 	 * the clock bases so the result might be negative. Fix it up
4949bc74919SThomas Gleixner 	 * to prevent a false positive in clockevents_program_event().
4959bc74919SThomas Gleixner 	 */
4969bc74919SThomas Gleixner 	if (expires_next.tv64 < 0)
4979bc74919SThomas Gleixner 		expires_next.tv64 = 0;
4989bc74919SThomas Gleixner 	return expires_next;
4999bc74919SThomas Gleixner }
5009bc74919SThomas Gleixner #endif
5019bc74919SThomas Gleixner 
50221d6d52aSThomas Gleixner static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
50321d6d52aSThomas Gleixner {
50421d6d52aSThomas Gleixner 	ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
50521d6d52aSThomas Gleixner 	ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
50621d6d52aSThomas Gleixner 	ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
50721d6d52aSThomas Gleixner 
508868a3e91SThomas Gleixner 	return ktime_get_update_offsets_now(&base->clock_was_set_seq,
509868a3e91SThomas Gleixner 					    offs_real, offs_boot, offs_tai);
51021d6d52aSThomas Gleixner }
51121d6d52aSThomas Gleixner 
5125cee9645SThomas Gleixner /* High resolution timer related functions */
5135cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
5145cee9645SThomas Gleixner 
5155cee9645SThomas Gleixner /*
5165cee9645SThomas Gleixner  * High resolution timer enabled ?
5175cee9645SThomas Gleixner  */
5185cee9645SThomas Gleixner static int hrtimer_hres_enabled __read_mostly  = 1;
519398ca17fSThomas Gleixner unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
520398ca17fSThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_resolution);
5215cee9645SThomas Gleixner 
5225cee9645SThomas Gleixner /*
5235cee9645SThomas Gleixner  * Enable / Disable high resolution mode
5245cee9645SThomas Gleixner  */
5255cee9645SThomas Gleixner static int __init setup_hrtimer_hres(char *str)
5265cee9645SThomas Gleixner {
5275cee9645SThomas Gleixner 	if (!strcmp(str, "off"))
5285cee9645SThomas Gleixner 		hrtimer_hres_enabled = 0;
5295cee9645SThomas Gleixner 	else if (!strcmp(str, "on"))
5305cee9645SThomas Gleixner 		hrtimer_hres_enabled = 1;
5315cee9645SThomas Gleixner 	else
5325cee9645SThomas Gleixner 		return 0;
5335cee9645SThomas Gleixner 	return 1;
5345cee9645SThomas Gleixner }
5355cee9645SThomas Gleixner 
5365cee9645SThomas Gleixner __setup("highres=", setup_hrtimer_hres);
5375cee9645SThomas Gleixner 
5385cee9645SThomas Gleixner /*
5395cee9645SThomas Gleixner  * hrtimer_high_res_enabled - query, if the highres mode is enabled
5405cee9645SThomas Gleixner  */
5415cee9645SThomas Gleixner static inline int hrtimer_is_hres_enabled(void)
5425cee9645SThomas Gleixner {
5435cee9645SThomas Gleixner 	return hrtimer_hres_enabled;
5445cee9645SThomas Gleixner }
5455cee9645SThomas Gleixner 
5465cee9645SThomas Gleixner /*
5475cee9645SThomas Gleixner  * Is the high resolution mode active ?
5485cee9645SThomas Gleixner  */
549e19ffe8bSThomas Gleixner static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
550e19ffe8bSThomas Gleixner {
551e19ffe8bSThomas Gleixner 	return cpu_base->hres_active;
552e19ffe8bSThomas Gleixner }
553e19ffe8bSThomas Gleixner 
5545cee9645SThomas Gleixner static inline int hrtimer_hres_active(void)
5555cee9645SThomas Gleixner {
556e19ffe8bSThomas Gleixner 	return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
5575cee9645SThomas Gleixner }
5585cee9645SThomas Gleixner 
5595cee9645SThomas Gleixner /*
5605cee9645SThomas Gleixner  * Reprogram the event source with checking both queues for the
5615cee9645SThomas Gleixner  * next event
5625cee9645SThomas Gleixner  * Called with interrupts disabled and base->lock held
5635cee9645SThomas Gleixner  */
5645cee9645SThomas Gleixner static void
5655cee9645SThomas Gleixner hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
5665cee9645SThomas Gleixner {
56721d6d52aSThomas Gleixner 	ktime_t expires_next;
56821d6d52aSThomas Gleixner 
56921d6d52aSThomas Gleixner 	if (!cpu_base->hres_active)
57021d6d52aSThomas Gleixner 		return;
57121d6d52aSThomas Gleixner 
57221d6d52aSThomas Gleixner 	expires_next = __hrtimer_get_next_event(cpu_base);
5735cee9645SThomas Gleixner 
5745cee9645SThomas Gleixner 	if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
5755cee9645SThomas Gleixner 		return;
5765cee9645SThomas Gleixner 
5775cee9645SThomas Gleixner 	cpu_base->expires_next.tv64 = expires_next.tv64;
5785cee9645SThomas Gleixner 
5795cee9645SThomas Gleixner 	/*
5805cee9645SThomas Gleixner 	 * If a hang was detected in the last timer interrupt then we
5815cee9645SThomas Gleixner 	 * leave the hang delay active in the hardware. We want the
5825cee9645SThomas Gleixner 	 * system to make progress. That also prevents the following
5835cee9645SThomas Gleixner 	 * scenario:
5845cee9645SThomas Gleixner 	 * T1 expires 50ms from now
5855cee9645SThomas Gleixner 	 * T2 expires 5s from now
5865cee9645SThomas Gleixner 	 *
5875cee9645SThomas Gleixner 	 * T1 is removed, so this code is called and would reprogram
5885cee9645SThomas Gleixner 	 * the hardware to 5s from now. Any hrtimer_start after that
5895cee9645SThomas Gleixner 	 * will not reprogram the hardware due to hang_detected being
5905cee9645SThomas Gleixner 	 * set. So we'd effectivly block all timers until the T2 event
5915cee9645SThomas Gleixner 	 * fires.
5925cee9645SThomas Gleixner 	 */
5935cee9645SThomas Gleixner 	if (cpu_base->hang_detected)
5945cee9645SThomas Gleixner 		return;
5955cee9645SThomas Gleixner 
5965cee9645SThomas Gleixner 	tick_program_event(cpu_base->expires_next, 1);
5975cee9645SThomas Gleixner }
5985cee9645SThomas Gleixner 
5995cee9645SThomas Gleixner /*
6005cee9645SThomas Gleixner  * When a timer is enqueued and expires earlier than the already enqueued
6015cee9645SThomas Gleixner  * timers, we have to check, whether it expires earlier than the timer for
6025cee9645SThomas Gleixner  * which the clock event device was armed.
6035cee9645SThomas Gleixner  *
6045cee9645SThomas Gleixner  * Called with interrupts disabled and base->cpu_base.lock held
6055cee9645SThomas Gleixner  */
606c6eb3f70SThomas Gleixner static void hrtimer_reprogram(struct hrtimer *timer,
6075cee9645SThomas Gleixner 			      struct hrtimer_clock_base *base)
6085cee9645SThomas Gleixner {
609dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
6105cee9645SThomas Gleixner 	ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
6115cee9645SThomas Gleixner 
6125cee9645SThomas Gleixner 	WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
6135cee9645SThomas Gleixner 
6145cee9645SThomas Gleixner 	/*
615c6eb3f70SThomas Gleixner 	 * If the timer is not on the current cpu, we cannot reprogram
616c6eb3f70SThomas Gleixner 	 * the other cpus clock event device.
6175cee9645SThomas Gleixner 	 */
618c6eb3f70SThomas Gleixner 	if (base->cpu_base != cpu_base)
619c6eb3f70SThomas Gleixner 		return;
620c6eb3f70SThomas Gleixner 
621c6eb3f70SThomas Gleixner 	/*
622c6eb3f70SThomas Gleixner 	 * If the hrtimer interrupt is running, then it will
623c6eb3f70SThomas Gleixner 	 * reevaluate the clock bases and reprogram the clock event
624c6eb3f70SThomas Gleixner 	 * device. The callbacks are always executed in hard interrupt
625c6eb3f70SThomas Gleixner 	 * context so we don't need an extra check for a running
626c6eb3f70SThomas Gleixner 	 * callback.
627c6eb3f70SThomas Gleixner 	 */
628c6eb3f70SThomas Gleixner 	if (cpu_base->in_hrtirq)
629c6eb3f70SThomas Gleixner 		return;
6305cee9645SThomas Gleixner 
6315cee9645SThomas Gleixner 	/*
6325cee9645SThomas Gleixner 	 * CLOCK_REALTIME timer might be requested with an absolute
633c6eb3f70SThomas Gleixner 	 * expiry time which is less than base->offset. Set it to 0.
6345cee9645SThomas Gleixner 	 */
6355cee9645SThomas Gleixner 	if (expires.tv64 < 0)
636c6eb3f70SThomas Gleixner 		expires.tv64 = 0;
6375cee9645SThomas Gleixner 
6385cee9645SThomas Gleixner 	if (expires.tv64 >= cpu_base->expires_next.tv64)
639c6eb3f70SThomas Gleixner 		return;
6405cee9645SThomas Gleixner 
641c6eb3f70SThomas Gleixner 	/* Update the pointer to the next expiring timer */
642895bdfa7SThomas Gleixner 	cpu_base->next_timer = timer;
6439bc74919SThomas Gleixner 
6449bc74919SThomas Gleixner 	/*
6455cee9645SThomas Gleixner 	 * If a hang was detected in the last timer interrupt then we
6465cee9645SThomas Gleixner 	 * do not schedule a timer which is earlier than the expiry
6475cee9645SThomas Gleixner 	 * which we enforced in the hang detection. We want the system
6485cee9645SThomas Gleixner 	 * to make progress.
6495cee9645SThomas Gleixner 	 */
6505cee9645SThomas Gleixner 	if (cpu_base->hang_detected)
651c6eb3f70SThomas Gleixner 		return;
6525cee9645SThomas Gleixner 
6535cee9645SThomas Gleixner 	/*
654c6eb3f70SThomas Gleixner 	 * Program the timer hardware. We enforce the expiry for
655c6eb3f70SThomas Gleixner 	 * events which are already in the past.
6565cee9645SThomas Gleixner 	 */
6575cee9645SThomas Gleixner 	cpu_base->expires_next = expires;
658c6eb3f70SThomas Gleixner 	tick_program_event(expires, 1);
6595cee9645SThomas Gleixner }
6605cee9645SThomas Gleixner 
6615cee9645SThomas Gleixner /*
6625cee9645SThomas Gleixner  * Initialize the high resolution related parts of cpu_base
6635cee9645SThomas Gleixner  */
6645cee9645SThomas Gleixner static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
6655cee9645SThomas Gleixner {
6665cee9645SThomas Gleixner 	base->expires_next.tv64 = KTIME_MAX;
6675cee9645SThomas Gleixner 	base->hres_active = 0;
6685cee9645SThomas Gleixner }
6695cee9645SThomas Gleixner 
6705cee9645SThomas Gleixner /*
6715cee9645SThomas Gleixner  * Retrigger next event is called after clock was set
6725cee9645SThomas Gleixner  *
6735cee9645SThomas Gleixner  * Called with interrupts disabled via on_each_cpu()
6745cee9645SThomas Gleixner  */
6755cee9645SThomas Gleixner static void retrigger_next_event(void *arg)
6765cee9645SThomas Gleixner {
677dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
6785cee9645SThomas Gleixner 
679e19ffe8bSThomas Gleixner 	if (!base->hres_active)
6805cee9645SThomas Gleixner 		return;
6815cee9645SThomas Gleixner 
6825cee9645SThomas Gleixner 	raw_spin_lock(&base->lock);
6835cee9645SThomas Gleixner 	hrtimer_update_base(base);
6845cee9645SThomas Gleixner 	hrtimer_force_reprogram(base, 0);
6855cee9645SThomas Gleixner 	raw_spin_unlock(&base->lock);
6865cee9645SThomas Gleixner }
6875cee9645SThomas Gleixner 
6885cee9645SThomas Gleixner /*
6895cee9645SThomas Gleixner  * Switch to high resolution mode
6905cee9645SThomas Gleixner  */
69175e3b37dSLuiz Capitulino static void hrtimer_switch_to_hres(void)
6925cee9645SThomas Gleixner {
693c6eb3f70SThomas Gleixner 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
6945cee9645SThomas Gleixner 
6955cee9645SThomas Gleixner 	if (tick_init_highres()) {
6965cee9645SThomas Gleixner 		printk(KERN_WARNING "Could not switch to high resolution "
697c6eb3f70SThomas Gleixner 				    "mode on CPU %d\n", base->cpu);
69885e1cd6eSGuenter Roeck 		return;
6995cee9645SThomas Gleixner 	}
7005cee9645SThomas Gleixner 	base->hres_active = 1;
701398ca17fSThomas Gleixner 	hrtimer_resolution = HIGH_RES_NSEC;
7025cee9645SThomas Gleixner 
7035cee9645SThomas Gleixner 	tick_setup_sched_timer();
7045cee9645SThomas Gleixner 	/* "Retrigger" the interrupt to get things going */
7055cee9645SThomas Gleixner 	retrigger_next_event(NULL);
7065cee9645SThomas Gleixner }
7075cee9645SThomas Gleixner 
7085cee9645SThomas Gleixner static void clock_was_set_work(struct work_struct *work)
7095cee9645SThomas Gleixner {
7105cee9645SThomas Gleixner 	clock_was_set();
7115cee9645SThomas Gleixner }
7125cee9645SThomas Gleixner 
7135cee9645SThomas Gleixner static DECLARE_WORK(hrtimer_work, clock_was_set_work);
7145cee9645SThomas Gleixner 
7155cee9645SThomas Gleixner /*
7165cee9645SThomas Gleixner  * Called from timekeeping and resume code to reprogramm the hrtimer
7175cee9645SThomas Gleixner  * interrupt device on all cpus.
7185cee9645SThomas Gleixner  */
7195cee9645SThomas Gleixner void clock_was_set_delayed(void)
7205cee9645SThomas Gleixner {
7215cee9645SThomas Gleixner 	schedule_work(&hrtimer_work);
7225cee9645SThomas Gleixner }
7235cee9645SThomas Gleixner 
7245cee9645SThomas Gleixner #else
7255cee9645SThomas Gleixner 
726e19ffe8bSThomas Gleixner static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *b) { return 0; }
7275cee9645SThomas Gleixner static inline int hrtimer_hres_active(void) { return 0; }
7285cee9645SThomas Gleixner static inline int hrtimer_is_hres_enabled(void) { return 0; }
72975e3b37dSLuiz Capitulino static inline void hrtimer_switch_to_hres(void) { }
7305cee9645SThomas Gleixner static inline void
7315cee9645SThomas Gleixner hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
7329e1e01ddSViresh Kumar static inline int hrtimer_reprogram(struct hrtimer *timer,
7335cee9645SThomas Gleixner 				    struct hrtimer_clock_base *base)
7345cee9645SThomas Gleixner {
7355cee9645SThomas Gleixner 	return 0;
7365cee9645SThomas Gleixner }
7375cee9645SThomas Gleixner static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
7385cee9645SThomas Gleixner static inline void retrigger_next_event(void *arg) { }
7395cee9645SThomas Gleixner 
7405cee9645SThomas Gleixner #endif /* CONFIG_HIGH_RES_TIMERS */
7415cee9645SThomas Gleixner 
7425cee9645SThomas Gleixner /*
7435cee9645SThomas Gleixner  * Clock realtime was set
7445cee9645SThomas Gleixner  *
7455cee9645SThomas Gleixner  * Change the offset of the realtime clock vs. the monotonic
7465cee9645SThomas Gleixner  * clock.
7475cee9645SThomas Gleixner  *
7485cee9645SThomas Gleixner  * We might have to reprogram the high resolution timer interrupt. On
7495cee9645SThomas Gleixner  * SMP we call the architecture specific code to retrigger _all_ high
7505cee9645SThomas Gleixner  * resolution timer interrupts. On UP we just disable interrupts and
7515cee9645SThomas Gleixner  * call the high resolution interrupt code.
7525cee9645SThomas Gleixner  */
7535cee9645SThomas Gleixner void clock_was_set(void)
7545cee9645SThomas Gleixner {
7555cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
7565cee9645SThomas Gleixner 	/* Retrigger the CPU local events everywhere */
7575cee9645SThomas Gleixner 	on_each_cpu(retrigger_next_event, NULL, 1);
7585cee9645SThomas Gleixner #endif
7595cee9645SThomas Gleixner 	timerfd_clock_was_set();
7605cee9645SThomas Gleixner }
7615cee9645SThomas Gleixner 
7625cee9645SThomas Gleixner /*
7635cee9645SThomas Gleixner  * During resume we might have to reprogram the high resolution timer
7645cee9645SThomas Gleixner  * interrupt on all online CPUs.  However, all other CPUs will be
7655cee9645SThomas Gleixner  * stopped with IRQs interrupts disabled so the clock_was_set() call
7665cee9645SThomas Gleixner  * must be deferred.
7675cee9645SThomas Gleixner  */
7685cee9645SThomas Gleixner void hrtimers_resume(void)
7695cee9645SThomas Gleixner {
7705cee9645SThomas Gleixner 	WARN_ONCE(!irqs_disabled(),
7715cee9645SThomas Gleixner 		  KERN_INFO "hrtimers_resume() called with IRQs enabled!");
7725cee9645SThomas Gleixner 
7735cee9645SThomas Gleixner 	/* Retrigger on the local CPU */
7745cee9645SThomas Gleixner 	retrigger_next_event(NULL);
7755cee9645SThomas Gleixner 	/* And schedule a retrigger for all others */
7765cee9645SThomas Gleixner 	clock_was_set_delayed();
7775cee9645SThomas Gleixner }
7785cee9645SThomas Gleixner 
7795cee9645SThomas Gleixner static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
7805cee9645SThomas Gleixner {
7815cee9645SThomas Gleixner #ifdef CONFIG_TIMER_STATS
7825cee9645SThomas Gleixner 	if (timer->start_site)
7835cee9645SThomas Gleixner 		return;
7845cee9645SThomas Gleixner 	timer->start_site = __builtin_return_address(0);
7855cee9645SThomas Gleixner 	memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
7865cee9645SThomas Gleixner 	timer->start_pid = current->pid;
7875cee9645SThomas Gleixner #endif
7885cee9645SThomas Gleixner }
7895cee9645SThomas Gleixner 
7905cee9645SThomas Gleixner static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
7915cee9645SThomas Gleixner {
7925cee9645SThomas Gleixner #ifdef CONFIG_TIMER_STATS
7935cee9645SThomas Gleixner 	timer->start_site = NULL;
7945cee9645SThomas Gleixner #endif
7955cee9645SThomas Gleixner }
7965cee9645SThomas Gleixner 
7975cee9645SThomas Gleixner static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
7985cee9645SThomas Gleixner {
7995cee9645SThomas Gleixner #ifdef CONFIG_TIMER_STATS
8005cee9645SThomas Gleixner 	if (likely(!timer_stats_active))
8015cee9645SThomas Gleixner 		return;
8025cee9645SThomas Gleixner 	timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
8035cee9645SThomas Gleixner 				 timer->function, timer->start_comm, 0);
8045cee9645SThomas Gleixner #endif
8055cee9645SThomas Gleixner }
8065cee9645SThomas Gleixner 
8075cee9645SThomas Gleixner /*
8085cee9645SThomas Gleixner  * Counterpart to lock_hrtimer_base above:
8095cee9645SThomas Gleixner  */
8105cee9645SThomas Gleixner static inline
8115cee9645SThomas Gleixner void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
8125cee9645SThomas Gleixner {
8135cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
8145cee9645SThomas Gleixner }
8155cee9645SThomas Gleixner 
8165cee9645SThomas Gleixner /**
8175cee9645SThomas Gleixner  * hrtimer_forward - forward the timer expiry
8185cee9645SThomas Gleixner  * @timer:	hrtimer to forward
8195cee9645SThomas Gleixner  * @now:	forward past this time
8205cee9645SThomas Gleixner  * @interval:	the interval to forward
8215cee9645SThomas Gleixner  *
8225cee9645SThomas Gleixner  * Forward the timer expiry so it will expire in the future.
8235cee9645SThomas Gleixner  * Returns the number of overruns.
82491e5a217SThomas Gleixner  *
82591e5a217SThomas Gleixner  * Can be safely called from the callback function of @timer. If
82691e5a217SThomas Gleixner  * called from other contexts @timer must neither be enqueued nor
82791e5a217SThomas Gleixner  * running the callback and the caller needs to take care of
82891e5a217SThomas Gleixner  * serialization.
82991e5a217SThomas Gleixner  *
83091e5a217SThomas Gleixner  * Note: This only updates the timer expiry value and does not requeue
83191e5a217SThomas Gleixner  * the timer.
8325cee9645SThomas Gleixner  */
8335cee9645SThomas Gleixner u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
8345cee9645SThomas Gleixner {
8355cee9645SThomas Gleixner 	u64 orun = 1;
8365cee9645SThomas Gleixner 	ktime_t delta;
8375cee9645SThomas Gleixner 
8385cee9645SThomas Gleixner 	delta = ktime_sub(now, hrtimer_get_expires(timer));
8395cee9645SThomas Gleixner 
8405cee9645SThomas Gleixner 	if (delta.tv64 < 0)
8415cee9645SThomas Gleixner 		return 0;
8425cee9645SThomas Gleixner 
8435de2755cSPeter Zijlstra 	if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
8445de2755cSPeter Zijlstra 		return 0;
8455de2755cSPeter Zijlstra 
846398ca17fSThomas Gleixner 	if (interval.tv64 < hrtimer_resolution)
847398ca17fSThomas Gleixner 		interval.tv64 = hrtimer_resolution;
8485cee9645SThomas Gleixner 
8495cee9645SThomas Gleixner 	if (unlikely(delta.tv64 >= interval.tv64)) {
8505cee9645SThomas Gleixner 		s64 incr = ktime_to_ns(interval);
8515cee9645SThomas Gleixner 
8525cee9645SThomas Gleixner 		orun = ktime_divns(delta, incr);
8535cee9645SThomas Gleixner 		hrtimer_add_expires_ns(timer, incr * orun);
8545cee9645SThomas Gleixner 		if (hrtimer_get_expires_tv64(timer) > now.tv64)
8555cee9645SThomas Gleixner 			return orun;
8565cee9645SThomas Gleixner 		/*
8575cee9645SThomas Gleixner 		 * This (and the ktime_add() below) is the
8585cee9645SThomas Gleixner 		 * correction for exact:
8595cee9645SThomas Gleixner 		 */
8605cee9645SThomas Gleixner 		orun++;
8615cee9645SThomas Gleixner 	}
8625cee9645SThomas Gleixner 	hrtimer_add_expires(timer, interval);
8635cee9645SThomas Gleixner 
8645cee9645SThomas Gleixner 	return orun;
8655cee9645SThomas Gleixner }
8665cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_forward);
8675cee9645SThomas Gleixner 
8685cee9645SThomas Gleixner /*
8695cee9645SThomas Gleixner  * enqueue_hrtimer - internal function to (re)start a timer
8705cee9645SThomas Gleixner  *
8715cee9645SThomas Gleixner  * The timer is inserted in expiry order. Insertion into the
8725cee9645SThomas Gleixner  * red black tree is O(log(n)). Must hold the base lock.
8735cee9645SThomas Gleixner  *
8745cee9645SThomas Gleixner  * Returns 1 when the new timer is the leftmost timer in the tree.
8755cee9645SThomas Gleixner  */
8765cee9645SThomas Gleixner static int enqueue_hrtimer(struct hrtimer *timer,
8775cee9645SThomas Gleixner 			   struct hrtimer_clock_base *base)
8785cee9645SThomas Gleixner {
8795cee9645SThomas Gleixner 	debug_activate(timer);
8805cee9645SThomas Gleixner 
8815cee9645SThomas Gleixner 	base->cpu_base->active_bases |= 1 << base->index;
8825cee9645SThomas Gleixner 
883887d9dc9SPeter Zijlstra 	timer->state = HRTIMER_STATE_ENQUEUED;
8845cee9645SThomas Gleixner 
885b97f44c9SThomas Gleixner 	return timerqueue_add(&base->active, &timer->node);
8865cee9645SThomas Gleixner }
8875cee9645SThomas Gleixner 
8885cee9645SThomas Gleixner /*
8895cee9645SThomas Gleixner  * __remove_hrtimer - internal function to remove a timer
8905cee9645SThomas Gleixner  *
8915cee9645SThomas Gleixner  * Caller must hold the base lock.
8925cee9645SThomas Gleixner  *
8935cee9645SThomas Gleixner  * High resolution timer mode reprograms the clock event device when the
8945cee9645SThomas Gleixner  * timer is the one which expires next. The caller can disable this by setting
8955cee9645SThomas Gleixner  * reprogram to zero. This is useful, when the context does a reprogramming
8965cee9645SThomas Gleixner  * anyway (e.g. timer interrupt)
8975cee9645SThomas Gleixner  */
8985cee9645SThomas Gleixner static void __remove_hrtimer(struct hrtimer *timer,
8995cee9645SThomas Gleixner 			     struct hrtimer_clock_base *base,
9005cee9645SThomas Gleixner 			     unsigned long newstate, int reprogram)
9015cee9645SThomas Gleixner {
902e19ffe8bSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = base->cpu_base;
903895bdfa7SThomas Gleixner 	unsigned int state = timer->state;
9045cee9645SThomas Gleixner 
9055cee9645SThomas Gleixner 	timer->state = newstate;
906895bdfa7SThomas Gleixner 	if (!(state & HRTIMER_STATE_ENQUEUED))
907895bdfa7SThomas Gleixner 		return;
9085cee9645SThomas Gleixner 
909b97f44c9SThomas Gleixner 	if (!timerqueue_del(&base->active, &timer->node))
910e19ffe8bSThomas Gleixner 		cpu_base->active_bases &= ~(1 << base->index);
911d9f0acdeSViresh Kumar 
9125cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
913895bdfa7SThomas Gleixner 	/*
914895bdfa7SThomas Gleixner 	 * Note: If reprogram is false we do not update
915895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. This happens when we remove the first
916895bdfa7SThomas Gleixner 	 * timer on a remote cpu. No harm as we never dereference
917895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. So the worst thing what can happen is
918895bdfa7SThomas Gleixner 	 * an superflous call to hrtimer_force_reprogram() on the
919895bdfa7SThomas Gleixner 	 * remote cpu later on if the same timer gets enqueued again.
920895bdfa7SThomas Gleixner 	 */
921895bdfa7SThomas Gleixner 	if (reprogram && timer == cpu_base->next_timer)
922e19ffe8bSThomas Gleixner 		hrtimer_force_reprogram(cpu_base, 1);
9235cee9645SThomas Gleixner #endif
9245cee9645SThomas Gleixner }
9255cee9645SThomas Gleixner 
9265cee9645SThomas Gleixner /*
9275cee9645SThomas Gleixner  * remove hrtimer, called with base lock held
9285cee9645SThomas Gleixner  */
9295cee9645SThomas Gleixner static inline int
9308edfb036SPeter Zijlstra remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart)
9315cee9645SThomas Gleixner {
9325cee9645SThomas Gleixner 	if (hrtimer_is_queued(timer)) {
9338edfb036SPeter Zijlstra 		unsigned long state = timer->state;
9345cee9645SThomas Gleixner 		int reprogram;
9355cee9645SThomas Gleixner 
9365cee9645SThomas Gleixner 		/*
9375cee9645SThomas Gleixner 		 * Remove the timer and force reprogramming when high
9385cee9645SThomas Gleixner 		 * resolution mode is active and the timer is on the current
9395cee9645SThomas Gleixner 		 * CPU. If we remove a timer on another CPU, reprogramming is
9405cee9645SThomas Gleixner 		 * skipped. The interrupt event on this CPU is fired and
9415cee9645SThomas Gleixner 		 * reprogramming happens in the interrupt handler. This is a
9425cee9645SThomas Gleixner 		 * rare case and less expensive than a smp call.
9435cee9645SThomas Gleixner 		 */
9445cee9645SThomas Gleixner 		debug_deactivate(timer);
9455cee9645SThomas Gleixner 		timer_stats_hrtimer_clear_start_info(timer);
946dc5df73bSChristoph Lameter 		reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
9478edfb036SPeter Zijlstra 
948887d9dc9SPeter Zijlstra 		if (!restart)
949887d9dc9SPeter Zijlstra 			state = HRTIMER_STATE_INACTIVE;
950887d9dc9SPeter Zijlstra 
9515cee9645SThomas Gleixner 		__remove_hrtimer(timer, base, state, reprogram);
9525cee9645SThomas Gleixner 		return 1;
9535cee9645SThomas Gleixner 	}
9545cee9645SThomas Gleixner 	return 0;
9555cee9645SThomas Gleixner }
9565cee9645SThomas Gleixner 
95758f1f803SThomas Gleixner /**
95858f1f803SThomas Gleixner  * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
95958f1f803SThomas Gleixner  * @timer:	the timer to be added
96058f1f803SThomas Gleixner  * @tim:	expiry time
96158f1f803SThomas Gleixner  * @delta_ns:	"slack" range for the timer
96258f1f803SThomas Gleixner  * @mode:	expiry mode: absolute (HRTIMER_MODE_ABS) or
96358f1f803SThomas Gleixner  *		relative (HRTIMER_MODE_REL)
96458f1f803SThomas Gleixner  */
96561699e13SThomas Gleixner void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
96658f1f803SThomas Gleixner 			    unsigned long delta_ns, const enum hrtimer_mode mode)
9675cee9645SThomas Gleixner {
9685cee9645SThomas Gleixner 	struct hrtimer_clock_base *base, *new_base;
9695cee9645SThomas Gleixner 	unsigned long flags;
97061699e13SThomas Gleixner 	int leftmost;
9715cee9645SThomas Gleixner 
9725cee9645SThomas Gleixner 	base = lock_hrtimer_base(timer, &flags);
9735cee9645SThomas Gleixner 
9745cee9645SThomas Gleixner 	/* Remove an active timer from the queue: */
9758edfb036SPeter Zijlstra 	remove_hrtimer(timer, base, true);
9765cee9645SThomas Gleixner 
9775cee9645SThomas Gleixner 	if (mode & HRTIMER_MODE_REL) {
9785cee9645SThomas Gleixner 		tim = ktime_add_safe(tim, base->get_time());
9795cee9645SThomas Gleixner 		/*
9805cee9645SThomas Gleixner 		 * CONFIG_TIME_LOW_RES is a temporary way for architectures
9815cee9645SThomas Gleixner 		 * to signal that they simply return xtime in
9825cee9645SThomas Gleixner 		 * do_gettimeoffset(). In this case we want to round up by
9835cee9645SThomas Gleixner 		 * resolution when starting a relative timer, to avoid short
9845cee9645SThomas Gleixner 		 * timeouts. This will go away with the GTOD framework.
9855cee9645SThomas Gleixner 		 */
9865cee9645SThomas Gleixner #ifdef CONFIG_TIME_LOW_RES
987398ca17fSThomas Gleixner 		tim = ktime_add_safe(tim, ktime_set(0, hrtimer_resolution));
9885cee9645SThomas Gleixner #endif
9895cee9645SThomas Gleixner 	}
9905cee9645SThomas Gleixner 
9915cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(timer, tim, delta_ns);
9925cee9645SThomas Gleixner 
9935cee9645SThomas Gleixner 	/* Switch the timer base, if necessary: */
9945cee9645SThomas Gleixner 	new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
9955cee9645SThomas Gleixner 
9965cee9645SThomas Gleixner 	timer_stats_hrtimer_set_start_info(timer);
9975cee9645SThomas Gleixner 
9985cee9645SThomas Gleixner 	leftmost = enqueue_hrtimer(timer, new_base);
99961699e13SThomas Gleixner 	if (!leftmost)
100061699e13SThomas Gleixner 		goto unlock;
100149a2a075SViresh Kumar 
100249a2a075SViresh Kumar 	if (!hrtimer_is_hres_active(timer)) {
100349a2a075SViresh Kumar 		/*
100449a2a075SViresh Kumar 		 * Kick to reschedule the next tick to handle the new timer
100549a2a075SViresh Kumar 		 * on dynticks target.
100649a2a075SViresh Kumar 		 */
1007683be13aSThomas Gleixner 		if (new_base->cpu_base->nohz_active)
100849a2a075SViresh Kumar 			wake_up_nohz_cpu(new_base->cpu_base->cpu);
10095cee9645SThomas Gleixner 	} else {
1010c6eb3f70SThomas Gleixner 		hrtimer_reprogram(timer, new_base);
10115cee9645SThomas Gleixner 	}
101261699e13SThomas Gleixner unlock:
10135cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
10145cee9645SThomas Gleixner }
10155cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
10165cee9645SThomas Gleixner 
10175cee9645SThomas Gleixner /**
10185cee9645SThomas Gleixner  * hrtimer_try_to_cancel - try to deactivate a timer
10195cee9645SThomas Gleixner  * @timer:	hrtimer to stop
10205cee9645SThomas Gleixner  *
10215cee9645SThomas Gleixner  * Returns:
10225cee9645SThomas Gleixner  *  0 when the timer was not active
10235cee9645SThomas Gleixner  *  1 when the timer was active
10245cee9645SThomas Gleixner  * -1 when the timer is currently excuting the callback function and
10255cee9645SThomas Gleixner  *    cannot be stopped
10265cee9645SThomas Gleixner  */
10275cee9645SThomas Gleixner int hrtimer_try_to_cancel(struct hrtimer *timer)
10285cee9645SThomas Gleixner {
10295cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
10305cee9645SThomas Gleixner 	unsigned long flags;
10315cee9645SThomas Gleixner 	int ret = -1;
10325cee9645SThomas Gleixner 
103319d9f422SThomas Gleixner 	/*
103419d9f422SThomas Gleixner 	 * Check lockless first. If the timer is not active (neither
103519d9f422SThomas Gleixner 	 * enqueued nor running the callback, nothing to do here.  The
103619d9f422SThomas Gleixner 	 * base lock does not serialize against a concurrent enqueue,
103719d9f422SThomas Gleixner 	 * so we can avoid taking it.
103819d9f422SThomas Gleixner 	 */
103919d9f422SThomas Gleixner 	if (!hrtimer_active(timer))
104019d9f422SThomas Gleixner 		return 0;
104119d9f422SThomas Gleixner 
10425cee9645SThomas Gleixner 	base = lock_hrtimer_base(timer, &flags);
10435cee9645SThomas Gleixner 
10445cee9645SThomas Gleixner 	if (!hrtimer_callback_running(timer))
10458edfb036SPeter Zijlstra 		ret = remove_hrtimer(timer, base, false);
10465cee9645SThomas Gleixner 
10475cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
10485cee9645SThomas Gleixner 
10495cee9645SThomas Gleixner 	return ret;
10505cee9645SThomas Gleixner 
10515cee9645SThomas Gleixner }
10525cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
10535cee9645SThomas Gleixner 
10545cee9645SThomas Gleixner /**
10555cee9645SThomas Gleixner  * hrtimer_cancel - cancel a timer and wait for the handler to finish.
10565cee9645SThomas Gleixner  * @timer:	the timer to be cancelled
10575cee9645SThomas Gleixner  *
10585cee9645SThomas Gleixner  * Returns:
10595cee9645SThomas Gleixner  *  0 when the timer was not active
10605cee9645SThomas Gleixner  *  1 when the timer was active
10615cee9645SThomas Gleixner  */
10625cee9645SThomas Gleixner int hrtimer_cancel(struct hrtimer *timer)
10635cee9645SThomas Gleixner {
10645cee9645SThomas Gleixner 	for (;;) {
10655cee9645SThomas Gleixner 		int ret = hrtimer_try_to_cancel(timer);
10665cee9645SThomas Gleixner 
10675cee9645SThomas Gleixner 		if (ret >= 0)
10685cee9645SThomas Gleixner 			return ret;
10695cee9645SThomas Gleixner 		cpu_relax();
10705cee9645SThomas Gleixner 	}
10715cee9645SThomas Gleixner }
10725cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_cancel);
10735cee9645SThomas Gleixner 
10745cee9645SThomas Gleixner /**
10755cee9645SThomas Gleixner  * hrtimer_get_remaining - get remaining time for the timer
10765cee9645SThomas Gleixner  * @timer:	the timer to read
10775cee9645SThomas Gleixner  */
10785cee9645SThomas Gleixner ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
10795cee9645SThomas Gleixner {
10805cee9645SThomas Gleixner 	unsigned long flags;
10815cee9645SThomas Gleixner 	ktime_t rem;
10825cee9645SThomas Gleixner 
10835cee9645SThomas Gleixner 	lock_hrtimer_base(timer, &flags);
10845cee9645SThomas Gleixner 	rem = hrtimer_expires_remaining(timer);
10855cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
10865cee9645SThomas Gleixner 
10875cee9645SThomas Gleixner 	return rem;
10885cee9645SThomas Gleixner }
10895cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
10905cee9645SThomas Gleixner 
10915cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
10925cee9645SThomas Gleixner /**
10935cee9645SThomas Gleixner  * hrtimer_get_next_event - get the time until next expiry event
10945cee9645SThomas Gleixner  *
1095c1ad348bSThomas Gleixner  * Returns the next expiry time or KTIME_MAX if no timer is pending.
10965cee9645SThomas Gleixner  */
1097c1ad348bSThomas Gleixner u64 hrtimer_get_next_event(void)
10985cee9645SThomas Gleixner {
1099dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1100c1ad348bSThomas Gleixner 	u64 expires = KTIME_MAX;
11015cee9645SThomas Gleixner 	unsigned long flags;
11025cee9645SThomas Gleixner 
11035cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
11045cee9645SThomas Gleixner 
1105e19ffe8bSThomas Gleixner 	if (!__hrtimer_hres_active(cpu_base))
1106c1ad348bSThomas Gleixner 		expires = __hrtimer_get_next_event(cpu_base).tv64;
11075cee9645SThomas Gleixner 
11085cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
11095cee9645SThomas Gleixner 
1110c1ad348bSThomas Gleixner 	return expires;
11115cee9645SThomas Gleixner }
11125cee9645SThomas Gleixner #endif
11135cee9645SThomas Gleixner 
11145cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
11155cee9645SThomas Gleixner 			   enum hrtimer_mode mode)
11165cee9645SThomas Gleixner {
11175cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base;
11185cee9645SThomas Gleixner 	int base;
11195cee9645SThomas Gleixner 
11205cee9645SThomas Gleixner 	memset(timer, 0, sizeof(struct hrtimer));
11215cee9645SThomas Gleixner 
112222127e93SChristoph Lameter 	cpu_base = raw_cpu_ptr(&hrtimer_bases);
11235cee9645SThomas Gleixner 
11245cee9645SThomas Gleixner 	if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
11255cee9645SThomas Gleixner 		clock_id = CLOCK_MONOTONIC;
11265cee9645SThomas Gleixner 
11275cee9645SThomas Gleixner 	base = hrtimer_clockid_to_base(clock_id);
11285cee9645SThomas Gleixner 	timer->base = &cpu_base->clock_base[base];
11295cee9645SThomas Gleixner 	timerqueue_init(&timer->node);
11305cee9645SThomas Gleixner 
11315cee9645SThomas Gleixner #ifdef CONFIG_TIMER_STATS
11325cee9645SThomas Gleixner 	timer->start_site = NULL;
11335cee9645SThomas Gleixner 	timer->start_pid = -1;
11345cee9645SThomas Gleixner 	memset(timer->start_comm, 0, TASK_COMM_LEN);
11355cee9645SThomas Gleixner #endif
11365cee9645SThomas Gleixner }
11375cee9645SThomas Gleixner 
11385cee9645SThomas Gleixner /**
11395cee9645SThomas Gleixner  * hrtimer_init - initialize a timer to the given clock
11405cee9645SThomas Gleixner  * @timer:	the timer to be initialized
11415cee9645SThomas Gleixner  * @clock_id:	the clock to be used
11425cee9645SThomas Gleixner  * @mode:	timer mode abs/rel
11435cee9645SThomas Gleixner  */
11445cee9645SThomas Gleixner void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
11455cee9645SThomas Gleixner 		  enum hrtimer_mode mode)
11465cee9645SThomas Gleixner {
11475cee9645SThomas Gleixner 	debug_init(timer, clock_id, mode);
11485cee9645SThomas Gleixner 	__hrtimer_init(timer, clock_id, mode);
11495cee9645SThomas Gleixner }
11505cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init);
11515cee9645SThomas Gleixner 
1152887d9dc9SPeter Zijlstra /*
1153887d9dc9SPeter Zijlstra  * A timer is active, when it is enqueued into the rbtree or the
1154887d9dc9SPeter Zijlstra  * callback function is running or it's in the state of being migrated
1155887d9dc9SPeter Zijlstra  * to another cpu.
11565cee9645SThomas Gleixner  *
1157887d9dc9SPeter Zijlstra  * It is important for this function to not return a false negative.
11585cee9645SThomas Gleixner  */
1159887d9dc9SPeter Zijlstra bool hrtimer_active(const struct hrtimer *timer)
11605cee9645SThomas Gleixner {
11615cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base;
1162887d9dc9SPeter Zijlstra 	unsigned int seq;
11635cee9645SThomas Gleixner 
1164887d9dc9SPeter Zijlstra 	do {
1165887d9dc9SPeter Zijlstra 		cpu_base = READ_ONCE(timer->base->cpu_base);
1166887d9dc9SPeter Zijlstra 		seq = raw_read_seqcount_begin(&cpu_base->seq);
11675cee9645SThomas Gleixner 
1168887d9dc9SPeter Zijlstra 		if (timer->state != HRTIMER_STATE_INACTIVE ||
1169887d9dc9SPeter Zijlstra 		    cpu_base->running == timer)
1170887d9dc9SPeter Zijlstra 			return true;
1171887d9dc9SPeter Zijlstra 
1172887d9dc9SPeter Zijlstra 	} while (read_seqcount_retry(&cpu_base->seq, seq) ||
1173887d9dc9SPeter Zijlstra 		 cpu_base != READ_ONCE(timer->base->cpu_base));
1174887d9dc9SPeter Zijlstra 
1175887d9dc9SPeter Zijlstra 	return false;
11765cee9645SThomas Gleixner }
1177887d9dc9SPeter Zijlstra EXPORT_SYMBOL_GPL(hrtimer_active);
11785cee9645SThomas Gleixner 
1179887d9dc9SPeter Zijlstra /*
1180887d9dc9SPeter Zijlstra  * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1181887d9dc9SPeter Zijlstra  * distinct sections:
1182887d9dc9SPeter Zijlstra  *
1183887d9dc9SPeter Zijlstra  *  - queued:	the timer is queued
1184887d9dc9SPeter Zijlstra  *  - callback:	the timer is being ran
1185887d9dc9SPeter Zijlstra  *  - post:	the timer is inactive or (re)queued
1186887d9dc9SPeter Zijlstra  *
1187887d9dc9SPeter Zijlstra  * On the read side we ensure we observe timer->state and cpu_base->running
1188887d9dc9SPeter Zijlstra  * from the same section, if anything changed while we looked at it, we retry.
1189887d9dc9SPeter Zijlstra  * This includes timer->base changing because sequence numbers alone are
1190887d9dc9SPeter Zijlstra  * insufficient for that.
1191887d9dc9SPeter Zijlstra  *
1192887d9dc9SPeter Zijlstra  * The sequence numbers are required because otherwise we could still observe
1193887d9dc9SPeter Zijlstra  * a false negative if the read side got smeared over multiple consequtive
1194887d9dc9SPeter Zijlstra  * __run_hrtimer() invocations.
1195887d9dc9SPeter Zijlstra  */
1196887d9dc9SPeter Zijlstra 
119721d6d52aSThomas Gleixner static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
119821d6d52aSThomas Gleixner 			  struct hrtimer_clock_base *base,
119921d6d52aSThomas Gleixner 			  struct hrtimer *timer, ktime_t *now)
12005cee9645SThomas Gleixner {
12015cee9645SThomas Gleixner 	enum hrtimer_restart (*fn)(struct hrtimer *);
12025cee9645SThomas Gleixner 	int restart;
12035cee9645SThomas Gleixner 
1204887d9dc9SPeter Zijlstra 	lockdep_assert_held(&cpu_base->lock);
12055cee9645SThomas Gleixner 
12065cee9645SThomas Gleixner 	debug_deactivate(timer);
1207887d9dc9SPeter Zijlstra 	cpu_base->running = timer;
1208887d9dc9SPeter Zijlstra 
1209887d9dc9SPeter Zijlstra 	/*
1210887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1211887d9dc9SPeter Zijlstra 	 *
1212887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
1213887d9dc9SPeter Zijlstra 	 * hrtimer_active() cannot observe cpu_base->running == NULL &&
1214887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
1215887d9dc9SPeter Zijlstra 	 */
1216887d9dc9SPeter Zijlstra 	raw_write_seqcount_barrier(&cpu_base->seq);
1217887d9dc9SPeter Zijlstra 
1218887d9dc9SPeter Zijlstra 	__remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
12195cee9645SThomas Gleixner 	timer_stats_account_hrtimer(timer);
12205cee9645SThomas Gleixner 	fn = timer->function;
12215cee9645SThomas Gleixner 
12225cee9645SThomas Gleixner 	/*
12235cee9645SThomas Gleixner 	 * Because we run timers from hardirq context, there is no chance
12245cee9645SThomas Gleixner 	 * they get migrated to another cpu, therefore its safe to unlock
12255cee9645SThomas Gleixner 	 * the timer base.
12265cee9645SThomas Gleixner 	 */
12275cee9645SThomas Gleixner 	raw_spin_unlock(&cpu_base->lock);
12285cee9645SThomas Gleixner 	trace_hrtimer_expire_entry(timer, now);
12295cee9645SThomas Gleixner 	restart = fn(timer);
12305cee9645SThomas Gleixner 	trace_hrtimer_expire_exit(timer);
12315cee9645SThomas Gleixner 	raw_spin_lock(&cpu_base->lock);
12325cee9645SThomas Gleixner 
12335cee9645SThomas Gleixner 	/*
1234887d9dc9SPeter Zijlstra 	 * Note: We clear the running state after enqueue_hrtimer and
12355cee9645SThomas Gleixner 	 * we do not reprogramm the event hardware. Happens either in
12365cee9645SThomas Gleixner 	 * hrtimer_start_range_ns() or in hrtimer_interrupt()
12375de2755cSPeter Zijlstra 	 *
12385de2755cSPeter Zijlstra 	 * Note: Because we dropped the cpu_base->lock above,
12395de2755cSPeter Zijlstra 	 * hrtimer_start_range_ns() can have popped in and enqueued the timer
12405de2755cSPeter Zijlstra 	 * for us already.
12415cee9645SThomas Gleixner 	 */
12425de2755cSPeter Zijlstra 	if (restart != HRTIMER_NORESTART &&
12435de2755cSPeter Zijlstra 	    !(timer->state & HRTIMER_STATE_ENQUEUED))
12445cee9645SThomas Gleixner 		enqueue_hrtimer(timer, base);
12455cee9645SThomas Gleixner 
12465cee9645SThomas Gleixner 	/*
1247887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1248887d9dc9SPeter Zijlstra 	 *
1249887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
1250887d9dc9SPeter Zijlstra 	 * hrtimer_active() cannot observe cpu_base->running == NULL &&
1251887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
12525cee9645SThomas Gleixner 	 */
1253887d9dc9SPeter Zijlstra 	raw_write_seqcount_barrier(&cpu_base->seq);
12545cee9645SThomas Gleixner 
1255887d9dc9SPeter Zijlstra 	WARN_ON_ONCE(cpu_base->running != timer);
1256887d9dc9SPeter Zijlstra 	cpu_base->running = NULL;
12575cee9645SThomas Gleixner }
12585cee9645SThomas Gleixner 
125921d6d52aSThomas Gleixner static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
12605cee9645SThomas Gleixner {
126134aee88aSThomas Gleixner 	struct hrtimer_clock_base *base = cpu_base->clock_base;
126234aee88aSThomas Gleixner 	unsigned int active = cpu_base->active_bases;
12635cee9645SThomas Gleixner 
126434aee88aSThomas Gleixner 	for (; active; base++, active >>= 1) {
12655cee9645SThomas Gleixner 		struct timerqueue_node *node;
12665cee9645SThomas Gleixner 		ktime_t basenow;
12675cee9645SThomas Gleixner 
126834aee88aSThomas Gleixner 		if (!(active & 0x01))
12695cee9645SThomas Gleixner 			continue;
12705cee9645SThomas Gleixner 
12715cee9645SThomas Gleixner 		basenow = ktime_add(now, base->offset);
12725cee9645SThomas Gleixner 
12735cee9645SThomas Gleixner 		while ((node = timerqueue_getnext(&base->active))) {
12745cee9645SThomas Gleixner 			struct hrtimer *timer;
12755cee9645SThomas Gleixner 
12765cee9645SThomas Gleixner 			timer = container_of(node, struct hrtimer, node);
12775cee9645SThomas Gleixner 
12785cee9645SThomas Gleixner 			/*
12795cee9645SThomas Gleixner 			 * The immediate goal for using the softexpires is
12805cee9645SThomas Gleixner 			 * minimizing wakeups, not running timers at the
12815cee9645SThomas Gleixner 			 * earliest interrupt after their soft expiration.
12825cee9645SThomas Gleixner 			 * This allows us to avoid using a Priority Search
12835cee9645SThomas Gleixner 			 * Tree, which can answer a stabbing querry for
12845cee9645SThomas Gleixner 			 * overlapping intervals and instead use the simple
12855cee9645SThomas Gleixner 			 * BST we already have.
12865cee9645SThomas Gleixner 			 * We don't add extra wakeups by delaying timers that
12875cee9645SThomas Gleixner 			 * are right-of a not yet expired timer, because that
12885cee9645SThomas Gleixner 			 * timer will have to trigger a wakeup anyway.
12895cee9645SThomas Gleixner 			 */
12909bc74919SThomas Gleixner 			if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
12915cee9645SThomas Gleixner 				break;
12925cee9645SThomas Gleixner 
129321d6d52aSThomas Gleixner 			__run_hrtimer(cpu_base, base, timer, &basenow);
12945cee9645SThomas Gleixner 		}
12955cee9645SThomas Gleixner 	}
129621d6d52aSThomas Gleixner }
129721d6d52aSThomas Gleixner 
129821d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
129921d6d52aSThomas Gleixner 
130021d6d52aSThomas Gleixner /*
130121d6d52aSThomas Gleixner  * High resolution timer interrupt
130221d6d52aSThomas Gleixner  * Called with interrupts disabled
130321d6d52aSThomas Gleixner  */
130421d6d52aSThomas Gleixner void hrtimer_interrupt(struct clock_event_device *dev)
130521d6d52aSThomas Gleixner {
130621d6d52aSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
130721d6d52aSThomas Gleixner 	ktime_t expires_next, now, entry_time, delta;
130821d6d52aSThomas Gleixner 	int retries = 0;
130921d6d52aSThomas Gleixner 
131021d6d52aSThomas Gleixner 	BUG_ON(!cpu_base->hres_active);
131121d6d52aSThomas Gleixner 	cpu_base->nr_events++;
131221d6d52aSThomas Gleixner 	dev->next_event.tv64 = KTIME_MAX;
131321d6d52aSThomas Gleixner 
131421d6d52aSThomas Gleixner 	raw_spin_lock(&cpu_base->lock);
131521d6d52aSThomas Gleixner 	entry_time = now = hrtimer_update_base(cpu_base);
131621d6d52aSThomas Gleixner retry:
131721d6d52aSThomas Gleixner 	cpu_base->in_hrtirq = 1;
131821d6d52aSThomas Gleixner 	/*
131921d6d52aSThomas Gleixner 	 * We set expires_next to KTIME_MAX here with cpu_base->lock
132021d6d52aSThomas Gleixner 	 * held to prevent that a timer is enqueued in our queue via
132121d6d52aSThomas Gleixner 	 * the migration code. This does not affect enqueueing of
132221d6d52aSThomas Gleixner 	 * timers which run their callback and need to be requeued on
132321d6d52aSThomas Gleixner 	 * this CPU.
132421d6d52aSThomas Gleixner 	 */
132521d6d52aSThomas Gleixner 	cpu_base->expires_next.tv64 = KTIME_MAX;
132621d6d52aSThomas Gleixner 
132721d6d52aSThomas Gleixner 	__hrtimer_run_queues(cpu_base, now);
132821d6d52aSThomas Gleixner 
13299bc74919SThomas Gleixner 	/* Reevaluate the clock bases for the next expiry */
13309bc74919SThomas Gleixner 	expires_next = __hrtimer_get_next_event(cpu_base);
13315cee9645SThomas Gleixner 	/*
13325cee9645SThomas Gleixner 	 * Store the new expiry value so the migration code can verify
13335cee9645SThomas Gleixner 	 * against it.
13345cee9645SThomas Gleixner 	 */
13355cee9645SThomas Gleixner 	cpu_base->expires_next = expires_next;
13369bc74919SThomas Gleixner 	cpu_base->in_hrtirq = 0;
13375cee9645SThomas Gleixner 	raw_spin_unlock(&cpu_base->lock);
13385cee9645SThomas Gleixner 
13395cee9645SThomas Gleixner 	/* Reprogramming necessary ? */
1340d2540875SViresh Kumar 	if (!tick_program_event(expires_next, 0)) {
13415cee9645SThomas Gleixner 		cpu_base->hang_detected = 0;
13425cee9645SThomas Gleixner 		return;
13435cee9645SThomas Gleixner 	}
13445cee9645SThomas Gleixner 
13455cee9645SThomas Gleixner 	/*
13465cee9645SThomas Gleixner 	 * The next timer was already expired due to:
13475cee9645SThomas Gleixner 	 * - tracing
13485cee9645SThomas Gleixner 	 * - long lasting callbacks
13495cee9645SThomas Gleixner 	 * - being scheduled away when running in a VM
13505cee9645SThomas Gleixner 	 *
13515cee9645SThomas Gleixner 	 * We need to prevent that we loop forever in the hrtimer
13525cee9645SThomas Gleixner 	 * interrupt routine. We give it 3 attempts to avoid
13535cee9645SThomas Gleixner 	 * overreacting on some spurious event.
13545cee9645SThomas Gleixner 	 *
13555cee9645SThomas Gleixner 	 * Acquire base lock for updating the offsets and retrieving
13565cee9645SThomas Gleixner 	 * the current time.
13575cee9645SThomas Gleixner 	 */
13585cee9645SThomas Gleixner 	raw_spin_lock(&cpu_base->lock);
13595cee9645SThomas Gleixner 	now = hrtimer_update_base(cpu_base);
13605cee9645SThomas Gleixner 	cpu_base->nr_retries++;
13615cee9645SThomas Gleixner 	if (++retries < 3)
13625cee9645SThomas Gleixner 		goto retry;
13635cee9645SThomas Gleixner 	/*
13645cee9645SThomas Gleixner 	 * Give the system a chance to do something else than looping
13655cee9645SThomas Gleixner 	 * here. We stored the entry time, so we know exactly how long
13665cee9645SThomas Gleixner 	 * we spent here. We schedule the next event this amount of
13675cee9645SThomas Gleixner 	 * time away.
13685cee9645SThomas Gleixner 	 */
13695cee9645SThomas Gleixner 	cpu_base->nr_hangs++;
13705cee9645SThomas Gleixner 	cpu_base->hang_detected = 1;
13715cee9645SThomas Gleixner 	raw_spin_unlock(&cpu_base->lock);
13725cee9645SThomas Gleixner 	delta = ktime_sub(now, entry_time);
1373a6ffebceSThomas Gleixner 	if ((unsigned int)delta.tv64 > cpu_base->max_hang_time)
1374a6ffebceSThomas Gleixner 		cpu_base->max_hang_time = (unsigned int) delta.tv64;
13755cee9645SThomas Gleixner 	/*
13765cee9645SThomas Gleixner 	 * Limit it to a sensible value as we enforce a longer
13775cee9645SThomas Gleixner 	 * delay. Give the CPU at least 100ms to catch up.
13785cee9645SThomas Gleixner 	 */
13795cee9645SThomas Gleixner 	if (delta.tv64 > 100 * NSEC_PER_MSEC)
13805cee9645SThomas Gleixner 		expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
13815cee9645SThomas Gleixner 	else
13825cee9645SThomas Gleixner 		expires_next = ktime_add(now, delta);
13835cee9645SThomas Gleixner 	tick_program_event(expires_next, 1);
13845cee9645SThomas Gleixner 	printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
13855cee9645SThomas Gleixner 		    ktime_to_ns(delta));
13865cee9645SThomas Gleixner }
13875cee9645SThomas Gleixner 
13885cee9645SThomas Gleixner /*
13895cee9645SThomas Gleixner  * local version of hrtimer_peek_ahead_timers() called with interrupts
13905cee9645SThomas Gleixner  * disabled.
13915cee9645SThomas Gleixner  */
1392c6eb3f70SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void)
13935cee9645SThomas Gleixner {
13945cee9645SThomas Gleixner 	struct tick_device *td;
13955cee9645SThomas Gleixner 
13965cee9645SThomas Gleixner 	if (!hrtimer_hres_active())
13975cee9645SThomas Gleixner 		return;
13985cee9645SThomas Gleixner 
139922127e93SChristoph Lameter 	td = this_cpu_ptr(&tick_cpu_device);
14005cee9645SThomas Gleixner 	if (td && td->evtdev)
14015cee9645SThomas Gleixner 		hrtimer_interrupt(td->evtdev);
14025cee9645SThomas Gleixner }
14035cee9645SThomas Gleixner 
14045cee9645SThomas Gleixner #else /* CONFIG_HIGH_RES_TIMERS */
14055cee9645SThomas Gleixner 
14065cee9645SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void) { }
14075cee9645SThomas Gleixner 
14085cee9645SThomas Gleixner #endif	/* !CONFIG_HIGH_RES_TIMERS */
14095cee9645SThomas Gleixner 
14105cee9645SThomas Gleixner /*
1411c6eb3f70SThomas Gleixner  * Called from run_local_timers in hardirq context every jiffy
14125cee9645SThomas Gleixner  */
14135cee9645SThomas Gleixner void hrtimer_run_queues(void)
14145cee9645SThomas Gleixner {
1415dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
141621d6d52aSThomas Gleixner 	ktime_t now;
14175cee9645SThomas Gleixner 
1418e19ffe8bSThomas Gleixner 	if (__hrtimer_hres_active(cpu_base))
14195cee9645SThomas Gleixner 		return;
14205cee9645SThomas Gleixner 
1421c6eb3f70SThomas Gleixner 	/*
1422c6eb3f70SThomas Gleixner 	 * This _is_ ugly: We have to check periodically, whether we
1423c6eb3f70SThomas Gleixner 	 * can switch to highres and / or nohz mode. The clocksource
1424c6eb3f70SThomas Gleixner 	 * switch happens with xtime_lock held. Notification from
1425c6eb3f70SThomas Gleixner 	 * there only sets the check bit in the tick_oneshot code,
1426c6eb3f70SThomas Gleixner 	 * otherwise we might deadlock vs. xtime_lock.
1427c6eb3f70SThomas Gleixner 	 */
1428c6eb3f70SThomas Gleixner 	if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
1429c6eb3f70SThomas Gleixner 		hrtimer_switch_to_hres();
1430c6eb3f70SThomas Gleixner 		return;
14315cee9645SThomas Gleixner 	}
14325cee9645SThomas Gleixner 
14335cee9645SThomas Gleixner 	raw_spin_lock(&cpu_base->lock);
143421d6d52aSThomas Gleixner 	now = hrtimer_update_base(cpu_base);
143521d6d52aSThomas Gleixner 	__hrtimer_run_queues(cpu_base, now);
14365cee9645SThomas Gleixner 	raw_spin_unlock(&cpu_base->lock);
14375cee9645SThomas Gleixner }
14385cee9645SThomas Gleixner 
14395cee9645SThomas Gleixner /*
14405cee9645SThomas Gleixner  * Sleep related functions:
14415cee9645SThomas Gleixner  */
14425cee9645SThomas Gleixner static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
14435cee9645SThomas Gleixner {
14445cee9645SThomas Gleixner 	struct hrtimer_sleeper *t =
14455cee9645SThomas Gleixner 		container_of(timer, struct hrtimer_sleeper, timer);
14465cee9645SThomas Gleixner 	struct task_struct *task = t->task;
14475cee9645SThomas Gleixner 
14485cee9645SThomas Gleixner 	t->task = NULL;
14495cee9645SThomas Gleixner 	if (task)
14505cee9645SThomas Gleixner 		wake_up_process(task);
14515cee9645SThomas Gleixner 
14525cee9645SThomas Gleixner 	return HRTIMER_NORESTART;
14535cee9645SThomas Gleixner }
14545cee9645SThomas Gleixner 
14555cee9645SThomas Gleixner void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
14565cee9645SThomas Gleixner {
14575cee9645SThomas Gleixner 	sl->timer.function = hrtimer_wakeup;
14585cee9645SThomas Gleixner 	sl->task = task;
14595cee9645SThomas Gleixner }
14605cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
14615cee9645SThomas Gleixner 
14625cee9645SThomas Gleixner static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
14635cee9645SThomas Gleixner {
14645cee9645SThomas Gleixner 	hrtimer_init_sleeper(t, current);
14655cee9645SThomas Gleixner 
14665cee9645SThomas Gleixner 	do {
14675cee9645SThomas Gleixner 		set_current_state(TASK_INTERRUPTIBLE);
14685cee9645SThomas Gleixner 		hrtimer_start_expires(&t->timer, mode);
14695cee9645SThomas Gleixner 
14705cee9645SThomas Gleixner 		if (likely(t->task))
14715cee9645SThomas Gleixner 			freezable_schedule();
14725cee9645SThomas Gleixner 
14735cee9645SThomas Gleixner 		hrtimer_cancel(&t->timer);
14745cee9645SThomas Gleixner 		mode = HRTIMER_MODE_ABS;
14755cee9645SThomas Gleixner 
14765cee9645SThomas Gleixner 	} while (t->task && !signal_pending(current));
14775cee9645SThomas Gleixner 
14785cee9645SThomas Gleixner 	__set_current_state(TASK_RUNNING);
14795cee9645SThomas Gleixner 
14805cee9645SThomas Gleixner 	return t->task == NULL;
14815cee9645SThomas Gleixner }
14825cee9645SThomas Gleixner 
14835cee9645SThomas Gleixner static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
14845cee9645SThomas Gleixner {
14855cee9645SThomas Gleixner 	struct timespec rmt;
14865cee9645SThomas Gleixner 	ktime_t rem;
14875cee9645SThomas Gleixner 
14885cee9645SThomas Gleixner 	rem = hrtimer_expires_remaining(timer);
14895cee9645SThomas Gleixner 	if (rem.tv64 <= 0)
14905cee9645SThomas Gleixner 		return 0;
14915cee9645SThomas Gleixner 	rmt = ktime_to_timespec(rem);
14925cee9645SThomas Gleixner 
14935cee9645SThomas Gleixner 	if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
14945cee9645SThomas Gleixner 		return -EFAULT;
14955cee9645SThomas Gleixner 
14965cee9645SThomas Gleixner 	return 1;
14975cee9645SThomas Gleixner }
14985cee9645SThomas Gleixner 
14995cee9645SThomas Gleixner long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
15005cee9645SThomas Gleixner {
15015cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
15025cee9645SThomas Gleixner 	struct timespec __user  *rmtp;
15035cee9645SThomas Gleixner 	int ret = 0;
15045cee9645SThomas Gleixner 
15055cee9645SThomas Gleixner 	hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
15065cee9645SThomas Gleixner 				HRTIMER_MODE_ABS);
15075cee9645SThomas Gleixner 	hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
15085cee9645SThomas Gleixner 
15095cee9645SThomas Gleixner 	if (do_nanosleep(&t, HRTIMER_MODE_ABS))
15105cee9645SThomas Gleixner 		goto out;
15115cee9645SThomas Gleixner 
15125cee9645SThomas Gleixner 	rmtp = restart->nanosleep.rmtp;
15135cee9645SThomas Gleixner 	if (rmtp) {
15145cee9645SThomas Gleixner 		ret = update_rmtp(&t.timer, rmtp);
15155cee9645SThomas Gleixner 		if (ret <= 0)
15165cee9645SThomas Gleixner 			goto out;
15175cee9645SThomas Gleixner 	}
15185cee9645SThomas Gleixner 
15195cee9645SThomas Gleixner 	/* The other values in restart are already filled in */
15205cee9645SThomas Gleixner 	ret = -ERESTART_RESTARTBLOCK;
15215cee9645SThomas Gleixner out:
15225cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
15235cee9645SThomas Gleixner 	return ret;
15245cee9645SThomas Gleixner }
15255cee9645SThomas Gleixner 
15265cee9645SThomas Gleixner long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
15275cee9645SThomas Gleixner 		       const enum hrtimer_mode mode, const clockid_t clockid)
15285cee9645SThomas Gleixner {
15295cee9645SThomas Gleixner 	struct restart_block *restart;
15305cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
15315cee9645SThomas Gleixner 	int ret = 0;
15325cee9645SThomas Gleixner 	unsigned long slack;
15335cee9645SThomas Gleixner 
15345cee9645SThomas Gleixner 	slack = current->timer_slack_ns;
15355cee9645SThomas Gleixner 	if (dl_task(current) || rt_task(current))
15365cee9645SThomas Gleixner 		slack = 0;
15375cee9645SThomas Gleixner 
15385cee9645SThomas Gleixner 	hrtimer_init_on_stack(&t.timer, clockid, mode);
15395cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
15405cee9645SThomas Gleixner 	if (do_nanosleep(&t, mode))
15415cee9645SThomas Gleixner 		goto out;
15425cee9645SThomas Gleixner 
15435cee9645SThomas Gleixner 	/* Absolute timers do not update the rmtp value and restart: */
15445cee9645SThomas Gleixner 	if (mode == HRTIMER_MODE_ABS) {
15455cee9645SThomas Gleixner 		ret = -ERESTARTNOHAND;
15465cee9645SThomas Gleixner 		goto out;
15475cee9645SThomas Gleixner 	}
15485cee9645SThomas Gleixner 
15495cee9645SThomas Gleixner 	if (rmtp) {
15505cee9645SThomas Gleixner 		ret = update_rmtp(&t.timer, rmtp);
15515cee9645SThomas Gleixner 		if (ret <= 0)
15525cee9645SThomas Gleixner 			goto out;
15535cee9645SThomas Gleixner 	}
15545cee9645SThomas Gleixner 
1555f56141e3SAndy Lutomirski 	restart = &current->restart_block;
15565cee9645SThomas Gleixner 	restart->fn = hrtimer_nanosleep_restart;
15575cee9645SThomas Gleixner 	restart->nanosleep.clockid = t.timer.base->clockid;
15585cee9645SThomas Gleixner 	restart->nanosleep.rmtp = rmtp;
15595cee9645SThomas Gleixner 	restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
15605cee9645SThomas Gleixner 
15615cee9645SThomas Gleixner 	ret = -ERESTART_RESTARTBLOCK;
15625cee9645SThomas Gleixner out:
15635cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
15645cee9645SThomas Gleixner 	return ret;
15655cee9645SThomas Gleixner }
15665cee9645SThomas Gleixner 
15675cee9645SThomas Gleixner SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
15685cee9645SThomas Gleixner 		struct timespec __user *, rmtp)
15695cee9645SThomas Gleixner {
15705cee9645SThomas Gleixner 	struct timespec tu;
15715cee9645SThomas Gleixner 
15725cee9645SThomas Gleixner 	if (copy_from_user(&tu, rqtp, sizeof(tu)))
15735cee9645SThomas Gleixner 		return -EFAULT;
15745cee9645SThomas Gleixner 
15755cee9645SThomas Gleixner 	if (!timespec_valid(&tu))
15765cee9645SThomas Gleixner 		return -EINVAL;
15775cee9645SThomas Gleixner 
15785cee9645SThomas Gleixner 	return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
15795cee9645SThomas Gleixner }
15805cee9645SThomas Gleixner 
15815cee9645SThomas Gleixner /*
15825cee9645SThomas Gleixner  * Functions related to boot-time initialization:
15835cee9645SThomas Gleixner  */
15845cee9645SThomas Gleixner static void init_hrtimers_cpu(int cpu)
15855cee9645SThomas Gleixner {
15865cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
15875cee9645SThomas Gleixner 	int i;
15885cee9645SThomas Gleixner 
15895cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
15905cee9645SThomas Gleixner 		cpu_base->clock_base[i].cpu_base = cpu_base;
15915cee9645SThomas Gleixner 		timerqueue_init_head(&cpu_base->clock_base[i].active);
15925cee9645SThomas Gleixner 	}
15935cee9645SThomas Gleixner 
1594cddd0248SViresh Kumar 	cpu_base->cpu = cpu;
15955cee9645SThomas Gleixner 	hrtimer_init_hres(cpu_base);
15965cee9645SThomas Gleixner }
15975cee9645SThomas Gleixner 
15985cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
15995cee9645SThomas Gleixner 
16005cee9645SThomas Gleixner static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
16015cee9645SThomas Gleixner 				struct hrtimer_clock_base *new_base)
16025cee9645SThomas Gleixner {
16035cee9645SThomas Gleixner 	struct hrtimer *timer;
16045cee9645SThomas Gleixner 	struct timerqueue_node *node;
16055cee9645SThomas Gleixner 
16065cee9645SThomas Gleixner 	while ((node = timerqueue_getnext(&old_base->active))) {
16075cee9645SThomas Gleixner 		timer = container_of(node, struct hrtimer, node);
16085cee9645SThomas Gleixner 		BUG_ON(hrtimer_callback_running(timer));
16095cee9645SThomas Gleixner 		debug_deactivate(timer);
16105cee9645SThomas Gleixner 
16115cee9645SThomas Gleixner 		/*
1612c04dca02SOleg Nesterov 		 * Mark it as ENQUEUED not INACTIVE otherwise the
16135cee9645SThomas Gleixner 		 * timer could be seen as !active and just vanish away
16145cee9645SThomas Gleixner 		 * under us on another CPU
16155cee9645SThomas Gleixner 		 */
1616c04dca02SOleg Nesterov 		__remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
16175cee9645SThomas Gleixner 		timer->base = new_base;
16185cee9645SThomas Gleixner 		/*
16195cee9645SThomas Gleixner 		 * Enqueue the timers on the new cpu. This does not
16205cee9645SThomas Gleixner 		 * reprogram the event device in case the timer
16215cee9645SThomas Gleixner 		 * expires before the earliest on this CPU, but we run
16225cee9645SThomas Gleixner 		 * hrtimer_interrupt after we migrated everything to
16235cee9645SThomas Gleixner 		 * sort out already expired timers and reprogram the
16245cee9645SThomas Gleixner 		 * event device.
16255cee9645SThomas Gleixner 		 */
16265cee9645SThomas Gleixner 		enqueue_hrtimer(timer, new_base);
16275cee9645SThomas Gleixner 	}
16285cee9645SThomas Gleixner }
16295cee9645SThomas Gleixner 
16305cee9645SThomas Gleixner static void migrate_hrtimers(int scpu)
16315cee9645SThomas Gleixner {
16325cee9645SThomas Gleixner 	struct hrtimer_cpu_base *old_base, *new_base;
16335cee9645SThomas Gleixner 	int i;
16345cee9645SThomas Gleixner 
16355cee9645SThomas Gleixner 	BUG_ON(cpu_online(scpu));
16365cee9645SThomas Gleixner 	tick_cancel_sched_timer(scpu);
16375cee9645SThomas Gleixner 
16385cee9645SThomas Gleixner 	local_irq_disable();
16395cee9645SThomas Gleixner 	old_base = &per_cpu(hrtimer_bases, scpu);
1640dc5df73bSChristoph Lameter 	new_base = this_cpu_ptr(&hrtimer_bases);
16415cee9645SThomas Gleixner 	/*
16425cee9645SThomas Gleixner 	 * The caller is globally serialized and nobody else
16435cee9645SThomas Gleixner 	 * takes two locks at once, deadlock is not possible.
16445cee9645SThomas Gleixner 	 */
16455cee9645SThomas Gleixner 	raw_spin_lock(&new_base->lock);
16465cee9645SThomas Gleixner 	raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
16475cee9645SThomas Gleixner 
16485cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
16495cee9645SThomas Gleixner 		migrate_hrtimer_list(&old_base->clock_base[i],
16505cee9645SThomas Gleixner 				     &new_base->clock_base[i]);
16515cee9645SThomas Gleixner 	}
16525cee9645SThomas Gleixner 
16535cee9645SThomas Gleixner 	raw_spin_unlock(&old_base->lock);
16545cee9645SThomas Gleixner 	raw_spin_unlock(&new_base->lock);
16555cee9645SThomas Gleixner 
16565cee9645SThomas Gleixner 	/* Check, if we got expired work to do */
16575cee9645SThomas Gleixner 	__hrtimer_peek_ahead_timers();
16585cee9645SThomas Gleixner 	local_irq_enable();
16595cee9645SThomas Gleixner }
16605cee9645SThomas Gleixner 
16615cee9645SThomas Gleixner #endif /* CONFIG_HOTPLUG_CPU */
16625cee9645SThomas Gleixner 
16635cee9645SThomas Gleixner static int hrtimer_cpu_notify(struct notifier_block *self,
16645cee9645SThomas Gleixner 					unsigned long action, void *hcpu)
16655cee9645SThomas Gleixner {
16665cee9645SThomas Gleixner 	int scpu = (long)hcpu;
16675cee9645SThomas Gleixner 
16685cee9645SThomas Gleixner 	switch (action) {
16695cee9645SThomas Gleixner 
16705cee9645SThomas Gleixner 	case CPU_UP_PREPARE:
16715cee9645SThomas Gleixner 	case CPU_UP_PREPARE_FROZEN:
16725cee9645SThomas Gleixner 		init_hrtimers_cpu(scpu);
16735cee9645SThomas Gleixner 		break;
16745cee9645SThomas Gleixner 
16755cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
16765cee9645SThomas Gleixner 	case CPU_DEAD:
16775cee9645SThomas Gleixner 	case CPU_DEAD_FROZEN:
16785cee9645SThomas Gleixner 		migrate_hrtimers(scpu);
16795cee9645SThomas Gleixner 		break;
16805cee9645SThomas Gleixner #endif
16815cee9645SThomas Gleixner 
16825cee9645SThomas Gleixner 	default:
16835cee9645SThomas Gleixner 		break;
16845cee9645SThomas Gleixner 	}
16855cee9645SThomas Gleixner 
16865cee9645SThomas Gleixner 	return NOTIFY_OK;
16875cee9645SThomas Gleixner }
16885cee9645SThomas Gleixner 
16895cee9645SThomas Gleixner static struct notifier_block hrtimers_nb = {
16905cee9645SThomas Gleixner 	.notifier_call = hrtimer_cpu_notify,
16915cee9645SThomas Gleixner };
16925cee9645SThomas Gleixner 
16935cee9645SThomas Gleixner void __init hrtimers_init(void)
16945cee9645SThomas Gleixner {
16955cee9645SThomas Gleixner 	hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
16965cee9645SThomas Gleixner 			  (void *)(long)smp_processor_id());
16975cee9645SThomas Gleixner 	register_cpu_notifier(&hrtimers_nb);
16985cee9645SThomas Gleixner }
16995cee9645SThomas Gleixner 
17005cee9645SThomas Gleixner /**
17015cee9645SThomas Gleixner  * schedule_hrtimeout_range_clock - sleep until timeout
17025cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
17035cee9645SThomas Gleixner  * @delta:	slack in expires timeout (ktime_t)
17045cee9645SThomas Gleixner  * @mode:	timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
17055cee9645SThomas Gleixner  * @clock:	timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
17065cee9645SThomas Gleixner  */
17075cee9645SThomas Gleixner int __sched
17085cee9645SThomas Gleixner schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
17095cee9645SThomas Gleixner 			       const enum hrtimer_mode mode, int clock)
17105cee9645SThomas Gleixner {
17115cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
17125cee9645SThomas Gleixner 
17135cee9645SThomas Gleixner 	/*
17145cee9645SThomas Gleixner 	 * Optimize when a zero timeout value is given. It does not
17155cee9645SThomas Gleixner 	 * matter whether this is an absolute or a relative time.
17165cee9645SThomas Gleixner 	 */
17175cee9645SThomas Gleixner 	if (expires && !expires->tv64) {
17185cee9645SThomas Gleixner 		__set_current_state(TASK_RUNNING);
17195cee9645SThomas Gleixner 		return 0;
17205cee9645SThomas Gleixner 	}
17215cee9645SThomas Gleixner 
17225cee9645SThomas Gleixner 	/*
17235cee9645SThomas Gleixner 	 * A NULL parameter means "infinite"
17245cee9645SThomas Gleixner 	 */
17255cee9645SThomas Gleixner 	if (!expires) {
17265cee9645SThomas Gleixner 		schedule();
17275cee9645SThomas Gleixner 		return -EINTR;
17285cee9645SThomas Gleixner 	}
17295cee9645SThomas Gleixner 
17305cee9645SThomas Gleixner 	hrtimer_init_on_stack(&t.timer, clock, mode);
17315cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
17325cee9645SThomas Gleixner 
17335cee9645SThomas Gleixner 	hrtimer_init_sleeper(&t, current);
17345cee9645SThomas Gleixner 
17355cee9645SThomas Gleixner 	hrtimer_start_expires(&t.timer, mode);
17365cee9645SThomas Gleixner 
17375cee9645SThomas Gleixner 	if (likely(t.task))
17385cee9645SThomas Gleixner 		schedule();
17395cee9645SThomas Gleixner 
17405cee9645SThomas Gleixner 	hrtimer_cancel(&t.timer);
17415cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
17425cee9645SThomas Gleixner 
17435cee9645SThomas Gleixner 	__set_current_state(TASK_RUNNING);
17445cee9645SThomas Gleixner 
17455cee9645SThomas Gleixner 	return !t.task ? 0 : -EINTR;
17465cee9645SThomas Gleixner }
17475cee9645SThomas Gleixner 
17485cee9645SThomas Gleixner /**
17495cee9645SThomas Gleixner  * schedule_hrtimeout_range - sleep until timeout
17505cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
17515cee9645SThomas Gleixner  * @delta:	slack in expires timeout (ktime_t)
17525cee9645SThomas Gleixner  * @mode:	timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
17535cee9645SThomas Gleixner  *
17545cee9645SThomas Gleixner  * Make the current task sleep until the given expiry time has
17555cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
17565cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
17575cee9645SThomas Gleixner  *
17585cee9645SThomas Gleixner  * The @delta argument gives the kernel the freedom to schedule the
17595cee9645SThomas Gleixner  * actual wakeup to a time that is both power and performance friendly.
17605cee9645SThomas Gleixner  * The kernel give the normal best effort behavior for "@expires+@delta",
17615cee9645SThomas Gleixner  * but may decide to fire the timer earlier, but no earlier than @expires.
17625cee9645SThomas Gleixner  *
17635cee9645SThomas Gleixner  * You can set the task state as follows -
17645cee9645SThomas Gleixner  *
17655cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
17665cee9645SThomas Gleixner  * pass before the routine returns.
17675cee9645SThomas Gleixner  *
17685cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
17695cee9645SThomas Gleixner  * delivered to the current task.
17705cee9645SThomas Gleixner  *
17715cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
17725cee9645SThomas Gleixner  * routine returns.
17735cee9645SThomas Gleixner  *
17745cee9645SThomas Gleixner  * Returns 0 when the timer has expired otherwise -EINTR
17755cee9645SThomas Gleixner  */
17765cee9645SThomas Gleixner int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
17775cee9645SThomas Gleixner 				     const enum hrtimer_mode mode)
17785cee9645SThomas Gleixner {
17795cee9645SThomas Gleixner 	return schedule_hrtimeout_range_clock(expires, delta, mode,
17805cee9645SThomas Gleixner 					      CLOCK_MONOTONIC);
17815cee9645SThomas Gleixner }
17825cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
17835cee9645SThomas Gleixner 
17845cee9645SThomas Gleixner /**
17855cee9645SThomas Gleixner  * schedule_hrtimeout - sleep until timeout
17865cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
17875cee9645SThomas Gleixner  * @mode:	timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
17885cee9645SThomas Gleixner  *
17895cee9645SThomas Gleixner  * Make the current task sleep until the given expiry time has
17905cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
17915cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
17925cee9645SThomas Gleixner  *
17935cee9645SThomas Gleixner  * You can set the task state as follows -
17945cee9645SThomas Gleixner  *
17955cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
17965cee9645SThomas Gleixner  * pass before the routine returns.
17975cee9645SThomas Gleixner  *
17985cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
17995cee9645SThomas Gleixner  * delivered to the current task.
18005cee9645SThomas Gleixner  *
18015cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
18025cee9645SThomas Gleixner  * routine returns.
18035cee9645SThomas Gleixner  *
18045cee9645SThomas Gleixner  * Returns 0 when the timer has expired otherwise -EINTR
18055cee9645SThomas Gleixner  */
18065cee9645SThomas Gleixner int __sched schedule_hrtimeout(ktime_t *expires,
18075cee9645SThomas Gleixner 			       const enum hrtimer_mode mode)
18085cee9645SThomas Gleixner {
18095cee9645SThomas Gleixner 	return schedule_hrtimeout_range(expires, 0, mode);
18105cee9645SThomas Gleixner }
18115cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout);
1812