xref: /openbmc/linux/kernel/time/hrtimer.c (revision e71a4153b7c256ec103e79875398553808aeffd2)
135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
25cee9645SThomas Gleixner /*
35cee9645SThomas Gleixner  *  Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
45cee9645SThomas Gleixner  *  Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
55cee9645SThomas Gleixner  *  Copyright(C) 2006-2007  Timesys Corp., Thomas Gleixner
65cee9645SThomas Gleixner  *
75cee9645SThomas Gleixner  *  High-resolution kernel timers
85cee9645SThomas Gleixner  *
958c5fc2bSThomas Gleixner  *  In contrast to the low-resolution timeout API, aka timer wheel,
1058c5fc2bSThomas Gleixner  *  hrtimers provide finer resolution and accuracy depending on system
1158c5fc2bSThomas Gleixner  *  configuration and capabilities.
125cee9645SThomas Gleixner  *
135cee9645SThomas Gleixner  *  Started by: Thomas Gleixner and Ingo Molnar
145cee9645SThomas Gleixner  *
155cee9645SThomas Gleixner  *  Credits:
1658c5fc2bSThomas Gleixner  *	Based on the original timer wheel code
175cee9645SThomas Gleixner  *
185cee9645SThomas Gleixner  *	Help, testing, suggestions, bugfixes, improvements were
195cee9645SThomas Gleixner  *	provided by:
205cee9645SThomas Gleixner  *
215cee9645SThomas Gleixner  *	George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
225cee9645SThomas Gleixner  *	et. al.
235cee9645SThomas Gleixner  */
245cee9645SThomas Gleixner 
255cee9645SThomas Gleixner #include <linux/cpu.h>
265cee9645SThomas Gleixner #include <linux/export.h>
275cee9645SThomas Gleixner #include <linux/percpu.h>
285cee9645SThomas Gleixner #include <linux/hrtimer.h>
295cee9645SThomas Gleixner #include <linux/notifier.h>
305cee9645SThomas Gleixner #include <linux/syscalls.h>
315cee9645SThomas Gleixner #include <linux/interrupt.h>
325cee9645SThomas Gleixner #include <linux/tick.h>
335cee9645SThomas Gleixner #include <linux/err.h>
345cee9645SThomas Gleixner #include <linux/debugobjects.h>
35174cd4b1SIngo Molnar #include <linux/sched/signal.h>
365cee9645SThomas Gleixner #include <linux/sched/sysctl.h>
375cee9645SThomas Gleixner #include <linux/sched/rt.h>
385cee9645SThomas Gleixner #include <linux/sched/deadline.h>
39370c9135SIngo Molnar #include <linux/sched/nohz.h>
40b17b0153SIngo Molnar #include <linux/sched/debug.h>
415cee9645SThomas Gleixner #include <linux/timer.h>
425cee9645SThomas Gleixner #include <linux/freezer.h>
43edbeda46SAl Viro #include <linux/compat.h>
445cee9645SThomas Gleixner 
457c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
465cee9645SThomas Gleixner 
475cee9645SThomas Gleixner #include <trace/events/timer.h>
485cee9645SThomas Gleixner 
49c1797bafSThomas Gleixner #include "tick-internal.h"
508b094cd0SThomas Gleixner 
515cee9645SThomas Gleixner /*
52c458b1d1SAnna-Maria Gleixner  * Masks for selecting the soft and hard context timers from
53c458b1d1SAnna-Maria Gleixner  * cpu_base->active
54c458b1d1SAnna-Maria Gleixner  */
55c458b1d1SAnna-Maria Gleixner #define MASK_SHIFT		(HRTIMER_BASE_MONOTONIC_SOFT)
56c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_HARD	((1U << MASK_SHIFT) - 1)
57c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_SOFT	(HRTIMER_ACTIVE_HARD << MASK_SHIFT)
58c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_ALL	(HRTIMER_ACTIVE_SOFT | HRTIMER_ACTIVE_HARD)
59c458b1d1SAnna-Maria Gleixner 
60c458b1d1SAnna-Maria Gleixner /*
615cee9645SThomas Gleixner  * The timer bases:
625cee9645SThomas Gleixner  *
63571af55aSZhen Lei  * There are more clockids than hrtimer bases. Thus, we index
645cee9645SThomas Gleixner  * into the timer bases by the hrtimer_base_type enum. When trying
655cee9645SThomas Gleixner  * to reach a base using a clockid, hrtimer_clockid_to_base()
665cee9645SThomas Gleixner  * is used to convert from clockid to the proper hrtimer_base_type.
675cee9645SThomas Gleixner  */
685cee9645SThomas Gleixner DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
695cee9645SThomas Gleixner {
705cee9645SThomas Gleixner 	.lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
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 		{
84a3ed0e43SThomas Gleixner 			.index = HRTIMER_BASE_BOOTTIME,
85a3ed0e43SThomas Gleixner 			.clockid = CLOCK_BOOTTIME,
86a3ed0e43SThomas Gleixner 			.get_time = &ktime_get_boottime,
87a3ed0e43SThomas Gleixner 		},
88a3ed0e43SThomas Gleixner 		{
895cee9645SThomas Gleixner 			.index = HRTIMER_BASE_TAI,
905cee9645SThomas Gleixner 			.clockid = CLOCK_TAI,
915cee9645SThomas Gleixner 			.get_time = &ktime_get_clocktai,
925cee9645SThomas Gleixner 		},
9398ecadd4SAnna-Maria Gleixner 		{
9498ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_MONOTONIC_SOFT,
9598ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_MONOTONIC,
9698ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get,
9798ecadd4SAnna-Maria Gleixner 		},
9898ecadd4SAnna-Maria Gleixner 		{
9998ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_REALTIME_SOFT,
10098ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_REALTIME,
10198ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get_real,
10298ecadd4SAnna-Maria Gleixner 		},
10398ecadd4SAnna-Maria Gleixner 		{
104a3ed0e43SThomas Gleixner 			.index = HRTIMER_BASE_BOOTTIME_SOFT,
105a3ed0e43SThomas Gleixner 			.clockid = CLOCK_BOOTTIME,
106a3ed0e43SThomas Gleixner 			.get_time = &ktime_get_boottime,
107a3ed0e43SThomas Gleixner 		},
108a3ed0e43SThomas Gleixner 		{
10998ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_TAI_SOFT,
11098ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_TAI,
11198ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get_clocktai,
11298ecadd4SAnna-Maria Gleixner 		},
1135cee9645SThomas Gleixner 	}
1145cee9645SThomas Gleixner };
1155cee9645SThomas Gleixner 
1165cee9645SThomas Gleixner static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
117336a9cdeSMarc Zyngier 	/* Make sure we catch unsupported clockids */
118336a9cdeSMarc Zyngier 	[0 ... MAX_CLOCKS - 1]	= HRTIMER_MAX_CLOCK_BASES,
119336a9cdeSMarc Zyngier 
1205cee9645SThomas Gleixner 	[CLOCK_REALTIME]	= HRTIMER_BASE_REALTIME,
1215cee9645SThomas Gleixner 	[CLOCK_MONOTONIC]	= HRTIMER_BASE_MONOTONIC,
122a3ed0e43SThomas Gleixner 	[CLOCK_BOOTTIME]	= HRTIMER_BASE_BOOTTIME,
1235cee9645SThomas Gleixner 	[CLOCK_TAI]		= HRTIMER_BASE_TAI,
1245cee9645SThomas Gleixner };
1255cee9645SThomas Gleixner 
1265cee9645SThomas Gleixner /*
1275cee9645SThomas Gleixner  * Functions and macros which are different for UP/SMP systems are kept in a
1285cee9645SThomas Gleixner  * single place
1295cee9645SThomas Gleixner  */
1305cee9645SThomas Gleixner #ifdef CONFIG_SMP
1315cee9645SThomas Gleixner 
1325cee9645SThomas Gleixner /*
133887d9dc9SPeter Zijlstra  * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
134887d9dc9SPeter Zijlstra  * such that hrtimer_callback_running() can unconditionally dereference
135887d9dc9SPeter Zijlstra  * timer->base->cpu_base
136887d9dc9SPeter Zijlstra  */
137887d9dc9SPeter Zijlstra static struct hrtimer_cpu_base migration_cpu_base = {
138af5a06b5SAhmed S. Darwish 	.clock_base = { {
139af5a06b5SAhmed S. Darwish 		.cpu_base = &migration_cpu_base,
140af5a06b5SAhmed S. Darwish 		.seq      = SEQCNT_RAW_SPINLOCK_ZERO(migration_cpu_base.seq,
141af5a06b5SAhmed S. Darwish 						     &migration_cpu_base.lock),
142af5a06b5SAhmed S. Darwish 	}, },
143887d9dc9SPeter Zijlstra };
144887d9dc9SPeter Zijlstra 
145887d9dc9SPeter Zijlstra #define migration_base	migration_cpu_base.clock_base[0]
146887d9dc9SPeter Zijlstra 
1475d2295f3SSebastian Andrzej Siewior static inline bool is_migration_base(struct hrtimer_clock_base *base)
1485d2295f3SSebastian Andrzej Siewior {
1495d2295f3SSebastian Andrzej Siewior 	return base == &migration_base;
1505d2295f3SSebastian Andrzej Siewior }
1515d2295f3SSebastian Andrzej Siewior 
152887d9dc9SPeter Zijlstra /*
1535cee9645SThomas Gleixner  * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
1545cee9645SThomas Gleixner  * means that all timers which are tied to this base via timer->base are
1555cee9645SThomas Gleixner  * locked, and the base itself is locked too.
1565cee9645SThomas Gleixner  *
1575cee9645SThomas Gleixner  * So __run_timers/migrate_timers can safely modify all timers which could
1585cee9645SThomas Gleixner  * be found on the lists/queues.
1595cee9645SThomas Gleixner  *
1605cee9645SThomas Gleixner  * When the timer's base is locked, and the timer removed from list, it is
161887d9dc9SPeter Zijlstra  * possible to set timer->base = &migration_base and drop the lock: the timer
162887d9dc9SPeter Zijlstra  * remains locked.
1635cee9645SThomas Gleixner  */
1645cee9645SThomas Gleixner static
1655cee9645SThomas Gleixner struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
1665cee9645SThomas Gleixner 					     unsigned long *flags)
1675cee9645SThomas Gleixner {
1685cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
1695cee9645SThomas Gleixner 
1705cee9645SThomas Gleixner 	for (;;) {
171ff229eeeSEric Dumazet 		base = READ_ONCE(timer->base);
172887d9dc9SPeter Zijlstra 		if (likely(base != &migration_base)) {
1735cee9645SThomas Gleixner 			raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
1745cee9645SThomas Gleixner 			if (likely(base == timer->base))
1755cee9645SThomas Gleixner 				return base;
1765cee9645SThomas Gleixner 			/* The timer has migrated to another CPU: */
1775cee9645SThomas Gleixner 			raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
1785cee9645SThomas Gleixner 		}
1795cee9645SThomas Gleixner 		cpu_relax();
1805cee9645SThomas Gleixner 	}
1815cee9645SThomas Gleixner }
1825cee9645SThomas Gleixner 
1835cee9645SThomas Gleixner /*
18407a9a7eaSAnna-Maria Gleixner  * We do not migrate the timer when it is expiring before the next
18507a9a7eaSAnna-Maria Gleixner  * event on the target cpu. When high resolution is enabled, we cannot
18607a9a7eaSAnna-Maria Gleixner  * reprogram the target cpu hardware and we would cause it to fire
18707a9a7eaSAnna-Maria Gleixner  * late. To keep it simple, we handle the high resolution enabled and
18807a9a7eaSAnna-Maria Gleixner  * disabled case similar.
1895cee9645SThomas Gleixner  *
1905cee9645SThomas Gleixner  * Called with cpu_base->lock of target cpu held.
1915cee9645SThomas Gleixner  */
1925cee9645SThomas Gleixner static int
1935cee9645SThomas Gleixner hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
1945cee9645SThomas Gleixner {
1955cee9645SThomas Gleixner 	ktime_t expires;
1965cee9645SThomas Gleixner 
1975cee9645SThomas Gleixner 	expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
1982ac2dcccSAnna-Maria Gleixner 	return expires < new_base->cpu_base->expires_next;
1995cee9645SThomas Gleixner }
2005cee9645SThomas Gleixner 
201bc7a34b8SThomas Gleixner static inline
202bc7a34b8SThomas Gleixner struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
203bc7a34b8SThomas Gleixner 					 int pinned)
204bc7a34b8SThomas Gleixner {
205ae67badaSThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
206ae67badaSThomas Gleixner 	if (static_branch_likely(&timers_migration_enabled) && !pinned)
207bc7a34b8SThomas Gleixner 		return &per_cpu(hrtimer_bases, get_nohz_timer_target());
208ae67badaSThomas Gleixner #endif
209662b3e19SFrederic Weisbecker 	return base;
210bc7a34b8SThomas Gleixner }
211bc7a34b8SThomas Gleixner 
2125cee9645SThomas Gleixner /*
213b48362d8SFrederic Weisbecker  * We switch the timer base to a power-optimized selected CPU target,
214b48362d8SFrederic Weisbecker  * if:
215b48362d8SFrederic Weisbecker  *	- NO_HZ_COMMON is enabled
216b48362d8SFrederic Weisbecker  *	- timer migration is enabled
217b48362d8SFrederic Weisbecker  *	- the timer callback is not running
218b48362d8SFrederic Weisbecker  *	- the timer is not the first expiring timer on the new target
219b48362d8SFrederic Weisbecker  *
220b48362d8SFrederic Weisbecker  * If one of the above requirements is not fulfilled we move the timer
221b48362d8SFrederic Weisbecker  * to the current CPU or leave it on the previously assigned CPU if
222b48362d8SFrederic Weisbecker  * the timer callback is currently running.
2235cee9645SThomas Gleixner  */
2245cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
2255cee9645SThomas Gleixner switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
2265cee9645SThomas Gleixner 		    int pinned)
2275cee9645SThomas Gleixner {
228b48362d8SFrederic Weisbecker 	struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
2295cee9645SThomas Gleixner 	struct hrtimer_clock_base *new_base;
2305cee9645SThomas Gleixner 	int basenum = base->index;
2315cee9645SThomas Gleixner 
232b48362d8SFrederic Weisbecker 	this_cpu_base = this_cpu_ptr(&hrtimer_bases);
233b48362d8SFrederic Weisbecker 	new_cpu_base = get_target_base(this_cpu_base, pinned);
2345cee9645SThomas Gleixner again:
2355cee9645SThomas Gleixner 	new_base = &new_cpu_base->clock_base[basenum];
2365cee9645SThomas Gleixner 
2375cee9645SThomas Gleixner 	if (base != new_base) {
2385cee9645SThomas Gleixner 		/*
2395cee9645SThomas Gleixner 		 * We are trying to move timer to new_base.
2405cee9645SThomas Gleixner 		 * However we can't change timer's base while it is running,
2415cee9645SThomas Gleixner 		 * so we keep it on the same CPU. No hassle vs. reprogramming
2425cee9645SThomas Gleixner 		 * the event source in the high resolution case. The softirq
2435cee9645SThomas Gleixner 		 * code will take care of this when the timer function has
2445cee9645SThomas Gleixner 		 * completed. There is no conflict as we hold the lock until
2455cee9645SThomas Gleixner 		 * the timer is enqueued.
2465cee9645SThomas Gleixner 		 */
2475cee9645SThomas Gleixner 		if (unlikely(hrtimer_callback_running(timer)))
2485cee9645SThomas Gleixner 			return base;
2495cee9645SThomas Gleixner 
250887d9dc9SPeter Zijlstra 		/* See the comment in lock_hrtimer_base() */
251ff229eeeSEric Dumazet 		WRITE_ONCE(timer->base, &migration_base);
2525cee9645SThomas Gleixner 		raw_spin_unlock(&base->cpu_base->lock);
2535cee9645SThomas Gleixner 		raw_spin_lock(&new_base->cpu_base->lock);
2545cee9645SThomas Gleixner 
255b48362d8SFrederic Weisbecker 		if (new_cpu_base != this_cpu_base &&
256bc7a34b8SThomas Gleixner 		    hrtimer_check_target(timer, new_base)) {
2575cee9645SThomas Gleixner 			raw_spin_unlock(&new_base->cpu_base->lock);
2585cee9645SThomas Gleixner 			raw_spin_lock(&base->cpu_base->lock);
259b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
260ff229eeeSEric Dumazet 			WRITE_ONCE(timer->base, base);
2615cee9645SThomas Gleixner 			goto again;
2625cee9645SThomas Gleixner 		}
263ff229eeeSEric Dumazet 		WRITE_ONCE(timer->base, new_base);
2645cee9645SThomas Gleixner 	} else {
265b48362d8SFrederic Weisbecker 		if (new_cpu_base != this_cpu_base &&
266bc7a34b8SThomas Gleixner 		    hrtimer_check_target(timer, new_base)) {
267b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
2685cee9645SThomas Gleixner 			goto again;
2695cee9645SThomas Gleixner 		}
2705cee9645SThomas Gleixner 	}
2715cee9645SThomas Gleixner 	return new_base;
2725cee9645SThomas Gleixner }
2735cee9645SThomas Gleixner 
2745cee9645SThomas Gleixner #else /* CONFIG_SMP */
2755cee9645SThomas Gleixner 
2765d2295f3SSebastian Andrzej Siewior static inline bool is_migration_base(struct hrtimer_clock_base *base)
2775d2295f3SSebastian Andrzej Siewior {
2785d2295f3SSebastian Andrzej Siewior 	return false;
2795d2295f3SSebastian Andrzej Siewior }
2805d2295f3SSebastian Andrzej Siewior 
2815cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
2825cee9645SThomas Gleixner lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
2835cee9645SThomas Gleixner {
2845cee9645SThomas Gleixner 	struct hrtimer_clock_base *base = timer->base;
2855cee9645SThomas Gleixner 
2865cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
2875cee9645SThomas Gleixner 
2885cee9645SThomas Gleixner 	return base;
2895cee9645SThomas Gleixner }
2905cee9645SThomas Gleixner 
2915cee9645SThomas Gleixner # define switch_hrtimer_base(t, b, p)	(b)
2925cee9645SThomas Gleixner 
2935cee9645SThomas Gleixner #endif	/* !CONFIG_SMP */
2945cee9645SThomas Gleixner 
2955cee9645SThomas Gleixner /*
2965cee9645SThomas Gleixner  * Functions for the union type storage format of ktime_t which are
2975cee9645SThomas Gleixner  * too large for inlining:
2985cee9645SThomas Gleixner  */
2995cee9645SThomas Gleixner #if BITS_PER_LONG < 64
3005cee9645SThomas Gleixner /*
3015cee9645SThomas Gleixner  * Divide a ktime value by a nanosecond value
3025cee9645SThomas Gleixner  */
303f7bcb70eSJohn Stultz s64 __ktime_divns(const ktime_t kt, s64 div)
3045cee9645SThomas Gleixner {
3055cee9645SThomas Gleixner 	int sft = 0;
306f7bcb70eSJohn Stultz 	s64 dclc;
307f7bcb70eSJohn Stultz 	u64 tmp;
3085cee9645SThomas Gleixner 
3095cee9645SThomas Gleixner 	dclc = ktime_to_ns(kt);
310f7bcb70eSJohn Stultz 	tmp = dclc < 0 ? -dclc : dclc;
311f7bcb70eSJohn Stultz 
3125cee9645SThomas Gleixner 	/* Make sure the divisor is less than 2^32: */
3135cee9645SThomas Gleixner 	while (div >> 32) {
3145cee9645SThomas Gleixner 		sft++;
3155cee9645SThomas Gleixner 		div >>= 1;
3165cee9645SThomas Gleixner 	}
317f7bcb70eSJohn Stultz 	tmp >>= sft;
31838f7b0b1SWen Yang 	do_div(tmp, (u32) div);
319f7bcb70eSJohn Stultz 	return dclc < 0 ? -tmp : tmp;
3205cee9645SThomas Gleixner }
3218b618628SNicolas Pitre EXPORT_SYMBOL_GPL(__ktime_divns);
3225cee9645SThomas Gleixner #endif /* BITS_PER_LONG >= 64 */
3235cee9645SThomas Gleixner 
3245cee9645SThomas Gleixner /*
3255cee9645SThomas Gleixner  * Add two ktime values and do a safety check for overflow:
3265cee9645SThomas Gleixner  */
3275cee9645SThomas Gleixner ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
3285cee9645SThomas Gleixner {
329979515c5SVegard Nossum 	ktime_t res = ktime_add_unsafe(lhs, rhs);
3305cee9645SThomas Gleixner 
3315cee9645SThomas Gleixner 	/*
3325cee9645SThomas Gleixner 	 * We use KTIME_SEC_MAX here, the maximum timeout which we can
3335cee9645SThomas Gleixner 	 * return to user space in a timespec:
3345cee9645SThomas Gleixner 	 */
3352456e855SThomas Gleixner 	if (res < 0 || res < lhs || res < rhs)
3365cee9645SThomas Gleixner 		res = ktime_set(KTIME_SEC_MAX, 0);
3375cee9645SThomas Gleixner 
3385cee9645SThomas Gleixner 	return res;
3395cee9645SThomas Gleixner }
3405cee9645SThomas Gleixner 
3415cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_add_safe);
3425cee9645SThomas Gleixner 
3435cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
3445cee9645SThomas Gleixner 
345f9e62f31SStephen Boyd static const struct debug_obj_descr hrtimer_debug_descr;
3465cee9645SThomas Gleixner 
3475cee9645SThomas Gleixner static void *hrtimer_debug_hint(void *addr)
3485cee9645SThomas Gleixner {
3495cee9645SThomas Gleixner 	return ((struct hrtimer *) addr)->function;
3505cee9645SThomas Gleixner }
3515cee9645SThomas Gleixner 
3525cee9645SThomas Gleixner /*
3535cee9645SThomas Gleixner  * fixup_init is called when:
3545cee9645SThomas Gleixner  * - an active object is initialized
3555cee9645SThomas Gleixner  */
356e3252464SDu, Changbin static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
3575cee9645SThomas Gleixner {
3585cee9645SThomas Gleixner 	struct hrtimer *timer = addr;
3595cee9645SThomas Gleixner 
3605cee9645SThomas Gleixner 	switch (state) {
3615cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3625cee9645SThomas Gleixner 		hrtimer_cancel(timer);
3635cee9645SThomas Gleixner 		debug_object_init(timer, &hrtimer_debug_descr);
364e3252464SDu, Changbin 		return true;
3655cee9645SThomas Gleixner 	default:
366e3252464SDu, Changbin 		return false;
3675cee9645SThomas Gleixner 	}
3685cee9645SThomas Gleixner }
3695cee9645SThomas Gleixner 
3705cee9645SThomas Gleixner /*
3715cee9645SThomas Gleixner  * fixup_activate is called when:
3725cee9645SThomas Gleixner  * - an active object is activated
373b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
3745cee9645SThomas Gleixner  */
375e3252464SDu, Changbin static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
3765cee9645SThomas Gleixner {
3775cee9645SThomas Gleixner 	switch (state) {
3785cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3795cee9645SThomas Gleixner 		WARN_ON(1);
380df561f66SGustavo A. R. Silva 		fallthrough;
3815cee9645SThomas Gleixner 	default:
382e3252464SDu, Changbin 		return false;
3835cee9645SThomas Gleixner 	}
3845cee9645SThomas Gleixner }
3855cee9645SThomas Gleixner 
3865cee9645SThomas Gleixner /*
3875cee9645SThomas Gleixner  * fixup_free is called when:
3885cee9645SThomas Gleixner  * - an active object is freed
3895cee9645SThomas Gleixner  */
390e3252464SDu, Changbin static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
3915cee9645SThomas Gleixner {
3925cee9645SThomas Gleixner 	struct hrtimer *timer = addr;
3935cee9645SThomas Gleixner 
3945cee9645SThomas Gleixner 	switch (state) {
3955cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3965cee9645SThomas Gleixner 		hrtimer_cancel(timer);
3975cee9645SThomas Gleixner 		debug_object_free(timer, &hrtimer_debug_descr);
398e3252464SDu, Changbin 		return true;
3995cee9645SThomas Gleixner 	default:
400e3252464SDu, Changbin 		return false;
4015cee9645SThomas Gleixner 	}
4025cee9645SThomas Gleixner }
4035cee9645SThomas Gleixner 
404f9e62f31SStephen Boyd static const struct debug_obj_descr hrtimer_debug_descr = {
4055cee9645SThomas Gleixner 	.name		= "hrtimer",
4065cee9645SThomas Gleixner 	.debug_hint	= hrtimer_debug_hint,
4075cee9645SThomas Gleixner 	.fixup_init	= hrtimer_fixup_init,
4085cee9645SThomas Gleixner 	.fixup_activate	= hrtimer_fixup_activate,
4095cee9645SThomas Gleixner 	.fixup_free	= hrtimer_fixup_free,
4105cee9645SThomas Gleixner };
4115cee9645SThomas Gleixner 
4125cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer)
4135cee9645SThomas Gleixner {
4145cee9645SThomas Gleixner 	debug_object_init(timer, &hrtimer_debug_descr);
4155cee9645SThomas Gleixner }
4165cee9645SThomas Gleixner 
4175da70160SAnna-Maria Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer,
4185da70160SAnna-Maria Gleixner 					  enum hrtimer_mode mode)
4195cee9645SThomas Gleixner {
4205cee9645SThomas Gleixner 	debug_object_activate(timer, &hrtimer_debug_descr);
4215cee9645SThomas Gleixner }
4225cee9645SThomas Gleixner 
4235cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
4245cee9645SThomas Gleixner {
4255cee9645SThomas Gleixner 	debug_object_deactivate(timer, &hrtimer_debug_descr);
4265cee9645SThomas Gleixner }
4275cee9645SThomas Gleixner 
4285cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
4295cee9645SThomas Gleixner 			   enum hrtimer_mode mode);
4305cee9645SThomas Gleixner 
4315cee9645SThomas Gleixner void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
4325cee9645SThomas Gleixner 			   enum hrtimer_mode mode)
4335cee9645SThomas Gleixner {
4345cee9645SThomas Gleixner 	debug_object_init_on_stack(timer, &hrtimer_debug_descr);
4355cee9645SThomas Gleixner 	__hrtimer_init(timer, clock_id, mode);
4365cee9645SThomas Gleixner }
4375cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
4385cee9645SThomas Gleixner 
439dbc1625fSSebastian Andrzej Siewior static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
440dbc1625fSSebastian Andrzej Siewior 				   clockid_t clock_id, enum hrtimer_mode mode);
441dbc1625fSSebastian Andrzej Siewior 
442dbc1625fSSebastian Andrzej Siewior void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
443dbc1625fSSebastian Andrzej Siewior 				   clockid_t clock_id, enum hrtimer_mode mode)
444dbc1625fSSebastian Andrzej Siewior {
445dbc1625fSSebastian Andrzej Siewior 	debug_object_init_on_stack(&sl->timer, &hrtimer_debug_descr);
446dbc1625fSSebastian Andrzej Siewior 	__hrtimer_init_sleeper(sl, clock_id, mode);
447dbc1625fSSebastian Andrzej Siewior }
448dbc1625fSSebastian Andrzej Siewior EXPORT_SYMBOL_GPL(hrtimer_init_sleeper_on_stack);
449dbc1625fSSebastian Andrzej Siewior 
4505cee9645SThomas Gleixner void destroy_hrtimer_on_stack(struct hrtimer *timer)
4515cee9645SThomas Gleixner {
4525cee9645SThomas Gleixner 	debug_object_free(timer, &hrtimer_debug_descr);
4535cee9645SThomas Gleixner }
454c08376acSGuenter Roeck EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
4555cee9645SThomas Gleixner 
4565cee9645SThomas Gleixner #else
4575da70160SAnna-Maria Gleixner 
4585cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer) { }
4595da70160SAnna-Maria Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer,
4605da70160SAnna-Maria Gleixner 					  enum hrtimer_mode mode) { }
4615cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
4625cee9645SThomas Gleixner #endif
4635cee9645SThomas Gleixner 
4645cee9645SThomas Gleixner static inline void
4655cee9645SThomas Gleixner debug_init(struct hrtimer *timer, clockid_t clockid,
4665cee9645SThomas Gleixner 	   enum hrtimer_mode mode)
4675cee9645SThomas Gleixner {
4685cee9645SThomas Gleixner 	debug_hrtimer_init(timer);
4695cee9645SThomas Gleixner 	trace_hrtimer_init(timer, clockid, mode);
4705cee9645SThomas Gleixner }
4715cee9645SThomas Gleixner 
47263e2ed36SAnna-Maria Gleixner static inline void debug_activate(struct hrtimer *timer,
47363e2ed36SAnna-Maria Gleixner 				  enum hrtimer_mode mode)
4745cee9645SThomas Gleixner {
4755da70160SAnna-Maria Gleixner 	debug_hrtimer_activate(timer, mode);
47663e2ed36SAnna-Maria Gleixner 	trace_hrtimer_start(timer, mode);
4775cee9645SThomas Gleixner }
4785cee9645SThomas Gleixner 
4795cee9645SThomas Gleixner static inline void debug_deactivate(struct hrtimer *timer)
4805cee9645SThomas Gleixner {
4815cee9645SThomas Gleixner 	debug_hrtimer_deactivate(timer);
4825cee9645SThomas Gleixner 	trace_hrtimer_cancel(timer);
4835cee9645SThomas Gleixner }
4845cee9645SThomas Gleixner 
485c272ca58SAnna-Maria Gleixner static struct hrtimer_clock_base *
486c272ca58SAnna-Maria Gleixner __next_base(struct hrtimer_cpu_base *cpu_base, unsigned int *active)
487c272ca58SAnna-Maria Gleixner {
488c272ca58SAnna-Maria Gleixner 	unsigned int idx;
489c272ca58SAnna-Maria Gleixner 
490c272ca58SAnna-Maria Gleixner 	if (!*active)
491c272ca58SAnna-Maria Gleixner 		return NULL;
492c272ca58SAnna-Maria Gleixner 
493c272ca58SAnna-Maria Gleixner 	idx = __ffs(*active);
494c272ca58SAnna-Maria Gleixner 	*active &= ~(1U << idx);
495c272ca58SAnna-Maria Gleixner 
496c272ca58SAnna-Maria Gleixner 	return &cpu_base->clock_base[idx];
497c272ca58SAnna-Maria Gleixner }
498c272ca58SAnna-Maria Gleixner 
499c272ca58SAnna-Maria Gleixner #define for_each_active_base(base, cpu_base, active)	\
500c272ca58SAnna-Maria Gleixner 	while ((base = __next_base((cpu_base), &(active))))
501c272ca58SAnna-Maria Gleixner 
502ad38f596SAnna-Maria Gleixner static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base,
503a59855cdSRafael J. Wysocki 					 const struct hrtimer *exclude,
504ad38f596SAnna-Maria Gleixner 					 unsigned int active,
505ad38f596SAnna-Maria Gleixner 					 ktime_t expires_next)
5069bc74919SThomas Gleixner {
507c272ca58SAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
508ad38f596SAnna-Maria Gleixner 	ktime_t expires;
5099bc74919SThomas Gleixner 
510c272ca58SAnna-Maria Gleixner 	for_each_active_base(base, cpu_base, active) {
5119bc74919SThomas Gleixner 		struct timerqueue_node *next;
5129bc74919SThomas Gleixner 		struct hrtimer *timer;
5139bc74919SThomas Gleixner 
51434aee88aSThomas Gleixner 		next = timerqueue_getnext(&base->active);
5159bc74919SThomas Gleixner 		timer = container_of(next, struct hrtimer, node);
516a59855cdSRafael J. Wysocki 		if (timer == exclude) {
517a59855cdSRafael J. Wysocki 			/* Get to the next timer in the queue. */
5187d2f6abbSRafael J. Wysocki 			next = timerqueue_iterate_next(next);
519a59855cdSRafael J. Wysocki 			if (!next)
520a59855cdSRafael J. Wysocki 				continue;
521a59855cdSRafael J. Wysocki 
522a59855cdSRafael J. Wysocki 			timer = container_of(next, struct hrtimer, node);
523a59855cdSRafael J. Wysocki 		}
5249bc74919SThomas Gleixner 		expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
5252456e855SThomas Gleixner 		if (expires < expires_next) {
5269bc74919SThomas Gleixner 			expires_next = expires;
527a59855cdSRafael J. Wysocki 
528a59855cdSRafael J. Wysocki 			/* Skip cpu_base update if a timer is being excluded. */
529a59855cdSRafael J. Wysocki 			if (exclude)
530a59855cdSRafael J. Wysocki 				continue;
531a59855cdSRafael J. Wysocki 
5325da70160SAnna-Maria Gleixner 			if (timer->is_soft)
5335da70160SAnna-Maria Gleixner 				cpu_base->softirq_next_timer = timer;
5345da70160SAnna-Maria Gleixner 			else
535eb27926bSAnna-Maria Gleixner 				cpu_base->next_timer = timer;
536895bdfa7SThomas Gleixner 		}
5379bc74919SThomas Gleixner 	}
5389bc74919SThomas Gleixner 	/*
5399bc74919SThomas Gleixner 	 * clock_was_set() might have changed base->offset of any of
5409bc74919SThomas Gleixner 	 * the clock bases so the result might be negative. Fix it up
5419bc74919SThomas Gleixner 	 * to prevent a false positive in clockevents_program_event().
5429bc74919SThomas Gleixner 	 */
5432456e855SThomas Gleixner 	if (expires_next < 0)
5442456e855SThomas Gleixner 		expires_next = 0;
5459bc74919SThomas Gleixner 	return expires_next;
5469bc74919SThomas Gleixner }
5479bc74919SThomas Gleixner 
548c458b1d1SAnna-Maria Gleixner /*
54946eb1701SAnna-Maria Behnsen  * Recomputes cpu_base::*next_timer and returns the earliest expires_next
55046eb1701SAnna-Maria Behnsen  * but does not set cpu_base::*expires_next, that is done by
55146eb1701SAnna-Maria Behnsen  * hrtimer[_force]_reprogram and hrtimer_interrupt only. When updating
55246eb1701SAnna-Maria Behnsen  * cpu_base::*expires_next right away, reprogramming logic would no longer
55346eb1701SAnna-Maria Behnsen  * work.
554c458b1d1SAnna-Maria Gleixner  *
5555da70160SAnna-Maria Gleixner  * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
5565da70160SAnna-Maria Gleixner  * those timers will get run whenever the softirq gets handled, at the end of
5575da70160SAnna-Maria Gleixner  * hrtimer_run_softirq(), hrtimer_update_softirq_timer() will re-add these bases.
5585da70160SAnna-Maria Gleixner  *
5595da70160SAnna-Maria Gleixner  * Therefore softirq values are those from the HRTIMER_ACTIVE_SOFT clock bases.
5605da70160SAnna-Maria Gleixner  * The !softirq values are the minima across HRTIMER_ACTIVE_ALL, unless an actual
5615da70160SAnna-Maria Gleixner  * softirq is pending, in which case they're the minima of HRTIMER_ACTIVE_HARD.
5625da70160SAnna-Maria Gleixner  *
563c458b1d1SAnna-Maria Gleixner  * @active_mask must be one of:
5645da70160SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_ALL,
565c458b1d1SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_SOFT, or
566c458b1d1SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_HARD.
567c458b1d1SAnna-Maria Gleixner  */
5685da70160SAnna-Maria Gleixner static ktime_t
5695da70160SAnna-Maria Gleixner __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_mask)
570ad38f596SAnna-Maria Gleixner {
571c458b1d1SAnna-Maria Gleixner 	unsigned int active;
5725da70160SAnna-Maria Gleixner 	struct hrtimer *next_timer = NULL;
573ad38f596SAnna-Maria Gleixner 	ktime_t expires_next = KTIME_MAX;
574ad38f596SAnna-Maria Gleixner 
5755da70160SAnna-Maria Gleixner 	if (!cpu_base->softirq_activated && (active_mask & HRTIMER_ACTIVE_SOFT)) {
5765da70160SAnna-Maria Gleixner 		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
5775da70160SAnna-Maria Gleixner 		cpu_base->softirq_next_timer = NULL;
578a59855cdSRafael J. Wysocki 		expires_next = __hrtimer_next_event_base(cpu_base, NULL,
579a59855cdSRafael J. Wysocki 							 active, KTIME_MAX);
580ad38f596SAnna-Maria Gleixner 
5815da70160SAnna-Maria Gleixner 		next_timer = cpu_base->softirq_next_timer;
5825da70160SAnna-Maria Gleixner 	}
5835da70160SAnna-Maria Gleixner 
5845da70160SAnna-Maria Gleixner 	if (active_mask & HRTIMER_ACTIVE_HARD) {
5855da70160SAnna-Maria Gleixner 		active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
5865da70160SAnna-Maria Gleixner 		cpu_base->next_timer = next_timer;
587a59855cdSRafael J. Wysocki 		expires_next = __hrtimer_next_event_base(cpu_base, NULL, active,
588a59855cdSRafael J. Wysocki 							 expires_next);
5895da70160SAnna-Maria Gleixner 	}
590ad38f596SAnna-Maria Gleixner 
591ad38f596SAnna-Maria Gleixner 	return expires_next;
592ad38f596SAnna-Maria Gleixner }
593ad38f596SAnna-Maria Gleixner 
59446eb1701SAnna-Maria Behnsen static ktime_t hrtimer_update_next_event(struct hrtimer_cpu_base *cpu_base)
59546eb1701SAnna-Maria Behnsen {
59646eb1701SAnna-Maria Behnsen 	ktime_t expires_next, soft = KTIME_MAX;
59746eb1701SAnna-Maria Behnsen 
59846eb1701SAnna-Maria Behnsen 	/*
59946eb1701SAnna-Maria Behnsen 	 * If the soft interrupt has already been activated, ignore the
60046eb1701SAnna-Maria Behnsen 	 * soft bases. They will be handled in the already raised soft
60146eb1701SAnna-Maria Behnsen 	 * interrupt.
60246eb1701SAnna-Maria Behnsen 	 */
60346eb1701SAnna-Maria Behnsen 	if (!cpu_base->softirq_activated) {
60446eb1701SAnna-Maria Behnsen 		soft = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
60546eb1701SAnna-Maria Behnsen 		/*
60646eb1701SAnna-Maria Behnsen 		 * Update the soft expiry time. clock_settime() might have
60746eb1701SAnna-Maria Behnsen 		 * affected it.
60846eb1701SAnna-Maria Behnsen 		 */
60946eb1701SAnna-Maria Behnsen 		cpu_base->softirq_expires_next = soft;
61046eb1701SAnna-Maria Behnsen 	}
61146eb1701SAnna-Maria Behnsen 
61246eb1701SAnna-Maria Behnsen 	expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD);
61346eb1701SAnna-Maria Behnsen 	/*
61446eb1701SAnna-Maria Behnsen 	 * If a softirq timer is expiring first, update cpu_base->next_timer
61546eb1701SAnna-Maria Behnsen 	 * and program the hardware with the soft expiry time.
61646eb1701SAnna-Maria Behnsen 	 */
61746eb1701SAnna-Maria Behnsen 	if (expires_next > soft) {
61846eb1701SAnna-Maria Behnsen 		cpu_base->next_timer = cpu_base->softirq_next_timer;
61946eb1701SAnna-Maria Behnsen 		expires_next = soft;
62046eb1701SAnna-Maria Behnsen 	}
62146eb1701SAnna-Maria Behnsen 
62246eb1701SAnna-Maria Behnsen 	return expires_next;
62346eb1701SAnna-Maria Behnsen }
62446eb1701SAnna-Maria Behnsen 
62521d6d52aSThomas Gleixner static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
62621d6d52aSThomas Gleixner {
62721d6d52aSThomas Gleixner 	ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
628a3ed0e43SThomas Gleixner 	ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
62921d6d52aSThomas Gleixner 	ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
63021d6d52aSThomas Gleixner 
6315da70160SAnna-Maria Gleixner 	ktime_t now = ktime_get_update_offsets_now(&base->clock_was_set_seq,
632a3ed0e43SThomas Gleixner 					    offs_real, offs_boot, offs_tai);
6335da70160SAnna-Maria Gleixner 
6345da70160SAnna-Maria Gleixner 	base->clock_base[HRTIMER_BASE_REALTIME_SOFT].offset = *offs_real;
635a3ed0e43SThomas Gleixner 	base->clock_base[HRTIMER_BASE_BOOTTIME_SOFT].offset = *offs_boot;
6365da70160SAnna-Maria Gleixner 	base->clock_base[HRTIMER_BASE_TAI_SOFT].offset = *offs_tai;
6375da70160SAnna-Maria Gleixner 
6385da70160SAnna-Maria Gleixner 	return now;
63921d6d52aSThomas Gleixner }
64021d6d52aSThomas Gleixner 
64128bfd18bSAnna-Maria Gleixner /*
64228bfd18bSAnna-Maria Gleixner  * Is the high resolution mode active ?
64328bfd18bSAnna-Maria Gleixner  */
64428bfd18bSAnna-Maria Gleixner static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
64528bfd18bSAnna-Maria Gleixner {
64628bfd18bSAnna-Maria Gleixner 	return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
64728bfd18bSAnna-Maria Gleixner 		cpu_base->hres_active : 0;
64828bfd18bSAnna-Maria Gleixner }
64928bfd18bSAnna-Maria Gleixner 
65028bfd18bSAnna-Maria Gleixner static inline int hrtimer_hres_active(void)
65128bfd18bSAnna-Maria Gleixner {
65228bfd18bSAnna-Maria Gleixner 	return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
65328bfd18bSAnna-Maria Gleixner }
65428bfd18bSAnna-Maria Gleixner 
6555cee9645SThomas Gleixner static void
656b14bca97SPeter Zijlstra __hrtimer_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal,
657b14bca97SPeter Zijlstra 		    struct hrtimer *next_timer, ktime_t expires_next)
6585cee9645SThomas Gleixner {
659b14bca97SPeter Zijlstra 	/*
660b14bca97SPeter Zijlstra 	 * If the hrtimer interrupt is running, then it will reevaluate the
661b14bca97SPeter Zijlstra 	 * clock bases and reprogram the clock event device.
662b14bca97SPeter Zijlstra 	 */
663b14bca97SPeter Zijlstra 	if (cpu_base->in_hrtirq)
664b14bca97SPeter Zijlstra 		return;
66521d6d52aSThomas Gleixner 
666b14bca97SPeter Zijlstra 	if (expires_next > cpu_base->expires_next)
667b14bca97SPeter Zijlstra 		return;
6685cee9645SThomas Gleixner 
6692456e855SThomas Gleixner 	if (skip_equal && expires_next == cpu_base->expires_next)
6705cee9645SThomas Gleixner 		return;
6715cee9645SThomas Gleixner 
672b14bca97SPeter Zijlstra 	cpu_base->next_timer = next_timer;
6732456e855SThomas Gleixner 	cpu_base->expires_next = expires_next;
6745cee9645SThomas Gleixner 
6755cee9645SThomas Gleixner 	/*
67661bb4bcbSAnna-Maria Gleixner 	 * If hres is not active, hardware does not have to be
67761bb4bcbSAnna-Maria Gleixner 	 * reprogrammed yet.
67861bb4bcbSAnna-Maria Gleixner 	 *
6795cee9645SThomas Gleixner 	 * If a hang was detected in the last timer interrupt then we
6805cee9645SThomas Gleixner 	 * leave the hang delay active in the hardware. We want the
6815cee9645SThomas Gleixner 	 * system to make progress. That also prevents the following
6825cee9645SThomas Gleixner 	 * scenario:
6835cee9645SThomas Gleixner 	 * T1 expires 50ms from now
6845cee9645SThomas Gleixner 	 * T2 expires 5s from now
6855cee9645SThomas Gleixner 	 *
6865cee9645SThomas Gleixner 	 * T1 is removed, so this code is called and would reprogram
6875cee9645SThomas Gleixner 	 * the hardware to 5s from now. Any hrtimer_start after that
6885cee9645SThomas Gleixner 	 * will not reprogram the hardware due to hang_detected being
6894bf07f65SIngo Molnar 	 * set. So we'd effectively block all timers until the T2 event
6905cee9645SThomas Gleixner 	 * fires.
6915cee9645SThomas Gleixner 	 */
69261bb4bcbSAnna-Maria Gleixner 	if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
6935cee9645SThomas Gleixner 		return;
6945cee9645SThomas Gleixner 
695b14bca97SPeter Zijlstra 	tick_program_event(expires_next, 1);
696b14bca97SPeter Zijlstra }
697b14bca97SPeter Zijlstra 
698b14bca97SPeter Zijlstra /*
699b14bca97SPeter Zijlstra  * Reprogram the event source with checking both queues for the
700b14bca97SPeter Zijlstra  * next event
701b14bca97SPeter Zijlstra  * Called with interrupts disabled and base->lock held
702b14bca97SPeter Zijlstra  */
703b14bca97SPeter Zijlstra static void
704b14bca97SPeter Zijlstra hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
705b14bca97SPeter Zijlstra {
706b14bca97SPeter Zijlstra 	ktime_t expires_next;
707b14bca97SPeter Zijlstra 
708b14bca97SPeter Zijlstra 	expires_next = hrtimer_update_next_event(cpu_base);
709b14bca97SPeter Zijlstra 
710b14bca97SPeter Zijlstra 	__hrtimer_reprogram(cpu_base, skip_equal, cpu_base->next_timer,
711b14bca97SPeter Zijlstra 			    expires_next);
7125cee9645SThomas Gleixner }
7135cee9645SThomas Gleixner 
714ebba2c72SAnna-Maria Gleixner /* High resolution timer related functions */
715ebba2c72SAnna-Maria Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
716ebba2c72SAnna-Maria Gleixner 
717ebba2c72SAnna-Maria Gleixner /*
718ebba2c72SAnna-Maria Gleixner  * High resolution timer enabled ?
719ebba2c72SAnna-Maria Gleixner  */
720ebba2c72SAnna-Maria Gleixner static bool hrtimer_hres_enabled __read_mostly  = true;
721ebba2c72SAnna-Maria Gleixner unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
722ebba2c72SAnna-Maria Gleixner EXPORT_SYMBOL_GPL(hrtimer_resolution);
723ebba2c72SAnna-Maria Gleixner 
724ebba2c72SAnna-Maria Gleixner /*
725ebba2c72SAnna-Maria Gleixner  * Enable / Disable high resolution mode
726ebba2c72SAnna-Maria Gleixner  */
727ebba2c72SAnna-Maria Gleixner static int __init setup_hrtimer_hres(char *str)
728ebba2c72SAnna-Maria Gleixner {
729ebba2c72SAnna-Maria Gleixner 	return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
730ebba2c72SAnna-Maria Gleixner }
731ebba2c72SAnna-Maria Gleixner 
732ebba2c72SAnna-Maria Gleixner __setup("highres=", setup_hrtimer_hres);
733ebba2c72SAnna-Maria Gleixner 
734ebba2c72SAnna-Maria Gleixner /*
735ebba2c72SAnna-Maria Gleixner  * hrtimer_high_res_enabled - query, if the highres mode is enabled
736ebba2c72SAnna-Maria Gleixner  */
737ebba2c72SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void)
738ebba2c72SAnna-Maria Gleixner {
739ebba2c72SAnna-Maria Gleixner 	return hrtimer_hres_enabled;
740ebba2c72SAnna-Maria Gleixner }
741ebba2c72SAnna-Maria Gleixner 
742*e71a4153SThomas Gleixner static void retrigger_next_event(void *arg);
74311a9fe06SAnna-Maria Gleixner 
74411a9fe06SAnna-Maria Gleixner /*
74511a9fe06SAnna-Maria Gleixner  * Switch to high resolution mode
74611a9fe06SAnna-Maria Gleixner  */
74711a9fe06SAnna-Maria Gleixner static void hrtimer_switch_to_hres(void)
74811a9fe06SAnna-Maria Gleixner {
74911a9fe06SAnna-Maria Gleixner 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
75011a9fe06SAnna-Maria Gleixner 
75111a9fe06SAnna-Maria Gleixner 	if (tick_init_highres()) {
7527a6e5537SGeert Uytterhoeven 		pr_warn("Could not switch to high resolution mode on CPU %u\n",
7537a6e5537SGeert Uytterhoeven 			base->cpu);
75411a9fe06SAnna-Maria Gleixner 		return;
75511a9fe06SAnna-Maria Gleixner 	}
75611a9fe06SAnna-Maria Gleixner 	base->hres_active = 1;
75711a9fe06SAnna-Maria Gleixner 	hrtimer_resolution = HIGH_RES_NSEC;
75811a9fe06SAnna-Maria Gleixner 
75911a9fe06SAnna-Maria Gleixner 	tick_setup_sched_timer();
76011a9fe06SAnna-Maria Gleixner 	/* "Retrigger" the interrupt to get things going */
76111a9fe06SAnna-Maria Gleixner 	retrigger_next_event(NULL);
76211a9fe06SAnna-Maria Gleixner }
76311a9fe06SAnna-Maria Gleixner 
76411a9fe06SAnna-Maria Gleixner #else
76511a9fe06SAnna-Maria Gleixner 
76611a9fe06SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void) { return 0; }
76711a9fe06SAnna-Maria Gleixner static inline void hrtimer_switch_to_hres(void) { }
76811a9fe06SAnna-Maria Gleixner 
76911a9fe06SAnna-Maria Gleixner #endif /* CONFIG_HIGH_RES_TIMERS */
770*e71a4153SThomas Gleixner /*
771*e71a4153SThomas Gleixner  * Retrigger next event is called after clock was set with interrupts
772*e71a4153SThomas Gleixner  * disabled through an SMP function call or directly from low level
773*e71a4153SThomas Gleixner  * resume code.
774*e71a4153SThomas Gleixner  *
775*e71a4153SThomas Gleixner  * This is only invoked when:
776*e71a4153SThomas Gleixner  *	- CONFIG_HIGH_RES_TIMERS is enabled.
777*e71a4153SThomas Gleixner  *	- CONFIG_NOHZ_COMMON is enabled
778*e71a4153SThomas Gleixner  *
779*e71a4153SThomas Gleixner  * For the other cases this function is empty and because the call sites
780*e71a4153SThomas Gleixner  * are optimized out it vanishes as well, i.e. no need for lots of
781*e71a4153SThomas Gleixner  * #ifdeffery.
782*e71a4153SThomas Gleixner  */
783*e71a4153SThomas Gleixner static void retrigger_next_event(void *arg)
784*e71a4153SThomas Gleixner {
785*e71a4153SThomas Gleixner 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
786*e71a4153SThomas Gleixner 
787*e71a4153SThomas Gleixner 	/*
788*e71a4153SThomas Gleixner 	 * When high resolution mode or nohz is active, then the offsets of
789*e71a4153SThomas Gleixner 	 * CLOCK_REALTIME/TAI/BOOTTIME have to be updated. Otherwise the
790*e71a4153SThomas Gleixner 	 * next tick will take care of that.
791*e71a4153SThomas Gleixner 	 *
792*e71a4153SThomas Gleixner 	 * If high resolution mode is active then the next expiring timer
793*e71a4153SThomas Gleixner 	 * must be reevaluated and the clock event device reprogrammed if
794*e71a4153SThomas Gleixner 	 * necessary.
795*e71a4153SThomas Gleixner 	 *
796*e71a4153SThomas Gleixner 	 * In the NOHZ case the update of the offset and the reevaluation
797*e71a4153SThomas Gleixner 	 * of the next expiring timer is enough. The return from the SMP
798*e71a4153SThomas Gleixner 	 * function call will take care of the reprogramming in case the
799*e71a4153SThomas Gleixner 	 * CPU was in a NOHZ idle sleep.
800*e71a4153SThomas Gleixner 	 */
801*e71a4153SThomas Gleixner 	if (!__hrtimer_hres_active(base) && !tick_nohz_active)
802*e71a4153SThomas Gleixner 		return;
803*e71a4153SThomas Gleixner 
804*e71a4153SThomas Gleixner 	raw_spin_lock(&base->lock);
805*e71a4153SThomas Gleixner 	hrtimer_update_base(base);
806*e71a4153SThomas Gleixner 	if (__hrtimer_hres_active(base))
807*e71a4153SThomas Gleixner 		hrtimer_force_reprogram(base, 0);
808*e71a4153SThomas Gleixner 	else
809*e71a4153SThomas Gleixner 		hrtimer_update_next_event(base);
810*e71a4153SThomas Gleixner 	raw_spin_unlock(&base->lock);
811*e71a4153SThomas Gleixner }
81211a9fe06SAnna-Maria Gleixner 
81311a9fe06SAnna-Maria Gleixner /*
8145cee9645SThomas Gleixner  * When a timer is enqueued and expires earlier than the already enqueued
8155cee9645SThomas Gleixner  * timers, we have to check, whether it expires earlier than the timer for
8165cee9645SThomas Gleixner  * which the clock event device was armed.
8175cee9645SThomas Gleixner  *
8185cee9645SThomas Gleixner  * Called with interrupts disabled and base->cpu_base.lock held
8195cee9645SThomas Gleixner  */
8205da70160SAnna-Maria Gleixner static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
8215cee9645SThomas Gleixner {
822dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
8233ec7a3eeSAnna-Maria Gleixner 	struct hrtimer_clock_base *base = timer->base;
8245cee9645SThomas Gleixner 	ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
8255cee9645SThomas Gleixner 
8265cee9645SThomas Gleixner 	WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
8275cee9645SThomas Gleixner 
8285cee9645SThomas Gleixner 	/*
8295da70160SAnna-Maria Gleixner 	 * CLOCK_REALTIME timer might be requested with an absolute
8305da70160SAnna-Maria Gleixner 	 * expiry time which is less than base->offset. Set it to 0.
8315da70160SAnna-Maria Gleixner 	 */
8325da70160SAnna-Maria Gleixner 	if (expires < 0)
8335da70160SAnna-Maria Gleixner 		expires = 0;
8345da70160SAnna-Maria Gleixner 
8355da70160SAnna-Maria Gleixner 	if (timer->is_soft) {
8365da70160SAnna-Maria Gleixner 		/*
8375da70160SAnna-Maria Gleixner 		 * soft hrtimer could be started on a remote CPU. In this
8385da70160SAnna-Maria Gleixner 		 * case softirq_expires_next needs to be updated on the
8395da70160SAnna-Maria Gleixner 		 * remote CPU. The soft hrtimer will not expire before the
8405da70160SAnna-Maria Gleixner 		 * first hard hrtimer on the remote CPU -
8415da70160SAnna-Maria Gleixner 		 * hrtimer_check_target() prevents this case.
8425da70160SAnna-Maria Gleixner 		 */
8435da70160SAnna-Maria Gleixner 		struct hrtimer_cpu_base *timer_cpu_base = base->cpu_base;
8445da70160SAnna-Maria Gleixner 
8455da70160SAnna-Maria Gleixner 		if (timer_cpu_base->softirq_activated)
8465da70160SAnna-Maria Gleixner 			return;
8475da70160SAnna-Maria Gleixner 
8485da70160SAnna-Maria Gleixner 		if (!ktime_before(expires, timer_cpu_base->softirq_expires_next))
8495da70160SAnna-Maria Gleixner 			return;
8505da70160SAnna-Maria Gleixner 
8515da70160SAnna-Maria Gleixner 		timer_cpu_base->softirq_next_timer = timer;
8525da70160SAnna-Maria Gleixner 		timer_cpu_base->softirq_expires_next = expires;
8535da70160SAnna-Maria Gleixner 
8545da70160SAnna-Maria Gleixner 		if (!ktime_before(expires, timer_cpu_base->expires_next) ||
8555da70160SAnna-Maria Gleixner 		    !reprogram)
8565da70160SAnna-Maria Gleixner 			return;
8575da70160SAnna-Maria Gleixner 	}
8585da70160SAnna-Maria Gleixner 
8595da70160SAnna-Maria Gleixner 	/*
860c6eb3f70SThomas Gleixner 	 * If the timer is not on the current cpu, we cannot reprogram
861c6eb3f70SThomas Gleixner 	 * the other cpus clock event device.
8625cee9645SThomas Gleixner 	 */
863c6eb3f70SThomas Gleixner 	if (base->cpu_base != cpu_base)
864c6eb3f70SThomas Gleixner 		return;
865c6eb3f70SThomas Gleixner 
866b14bca97SPeter Zijlstra 	__hrtimer_reprogram(cpu_base, true, timer, expires);
8675cee9645SThomas Gleixner }
8685cee9645SThomas Gleixner 
8695cee9645SThomas Gleixner /*
870*e71a4153SThomas Gleixner  * Clock was set. This might affect CLOCK_REALTIME, CLOCK_TAI and
871*e71a4153SThomas Gleixner  * CLOCK_BOOTTIME (for late sleep time injection).
8725cee9645SThomas Gleixner  *
873*e71a4153SThomas Gleixner  * This requires to update the offsets for these clocks
874*e71a4153SThomas Gleixner  * vs. CLOCK_MONOTONIC. When high resolution timers are enabled, then this
875*e71a4153SThomas Gleixner  * also requires to eventually reprogram the per CPU clock event devices
876*e71a4153SThomas Gleixner  * when the change moves an affected timer ahead of the first expiring
877*e71a4153SThomas Gleixner  * timer on that CPU. Obviously remote per CPU clock event devices cannot
878*e71a4153SThomas Gleixner  * be reprogrammed. The other reason why an IPI has to be sent is when the
879*e71a4153SThomas Gleixner  * system is in !HIGH_RES and NOHZ mode. The NOHZ mode updates the offsets
880*e71a4153SThomas Gleixner  * in the tick, which obviously might be stopped, so this has to bring out
881*e71a4153SThomas Gleixner  * the remote CPU which might sleep in idle to get this sorted.
8825cee9645SThomas Gleixner  */
8835cee9645SThomas Gleixner void clock_was_set(void)
8845cee9645SThomas Gleixner {
885*e71a4153SThomas Gleixner 	if (!hrtimer_hres_active() && !tick_nohz_active)
886*e71a4153SThomas Gleixner 		goto out_timerfd;
887*e71a4153SThomas Gleixner 
8885cee9645SThomas Gleixner 	/* Retrigger the CPU local events everywhere */
8895cee9645SThomas Gleixner 	on_each_cpu(retrigger_next_event, NULL, 1);
890*e71a4153SThomas Gleixner 
891*e71a4153SThomas Gleixner out_timerfd:
8925cee9645SThomas Gleixner 	timerfd_clock_was_set();
8935cee9645SThomas Gleixner }
8945cee9645SThomas Gleixner 
8958c3b5e6eSThomas Gleixner static void clock_was_set_work(struct work_struct *work)
8968c3b5e6eSThomas Gleixner {
8978c3b5e6eSThomas Gleixner 	clock_was_set();
8988c3b5e6eSThomas Gleixner }
8998c3b5e6eSThomas Gleixner 
9008c3b5e6eSThomas Gleixner static DECLARE_WORK(hrtimer_work, clock_was_set_work);
9018c3b5e6eSThomas Gleixner 
9028c3b5e6eSThomas Gleixner /*
9038c3b5e6eSThomas Gleixner  * Called from timekeeping and resume code to reprogram the hrtimer
9048c3b5e6eSThomas Gleixner  * interrupt device on all cpus and to notify timerfd.
9058c3b5e6eSThomas Gleixner  */
9068c3b5e6eSThomas Gleixner void clock_was_set_delayed(void)
9078c3b5e6eSThomas Gleixner {
9088c3b5e6eSThomas Gleixner 	schedule_work(&hrtimer_work);
9098c3b5e6eSThomas Gleixner }
9108c3b5e6eSThomas Gleixner 
9115cee9645SThomas Gleixner /*
9125cee9645SThomas Gleixner  * During resume we might have to reprogram the high resolution timer
9135cee9645SThomas Gleixner  * interrupt on all online CPUs.  However, all other CPUs will be
9145cee9645SThomas Gleixner  * stopped with IRQs interrupts disabled so the clock_was_set() call
9155cee9645SThomas Gleixner  * must be deferred.
9165cee9645SThomas Gleixner  */
9175cee9645SThomas Gleixner void hrtimers_resume(void)
9185cee9645SThomas Gleixner {
91953bef3fdSFrederic Weisbecker 	lockdep_assert_irqs_disabled();
9205cee9645SThomas Gleixner 	/* Retrigger on the local CPU */
9215cee9645SThomas Gleixner 	retrigger_next_event(NULL);
9225cee9645SThomas Gleixner 	/* And schedule a retrigger for all others */
9235cee9645SThomas Gleixner 	clock_was_set_delayed();
9245cee9645SThomas Gleixner }
9255cee9645SThomas Gleixner 
9265cee9645SThomas Gleixner /*
9275cee9645SThomas Gleixner  * Counterpart to lock_hrtimer_base above:
9285cee9645SThomas Gleixner  */
9295cee9645SThomas Gleixner static inline
9305cee9645SThomas Gleixner void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
9315cee9645SThomas Gleixner {
9325cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
9335cee9645SThomas Gleixner }
9345cee9645SThomas Gleixner 
9355cee9645SThomas Gleixner /**
9365cee9645SThomas Gleixner  * hrtimer_forward - forward the timer expiry
9375cee9645SThomas Gleixner  * @timer:	hrtimer to forward
9385cee9645SThomas Gleixner  * @now:	forward past this time
9395cee9645SThomas Gleixner  * @interval:	the interval to forward
9405cee9645SThomas Gleixner  *
9415cee9645SThomas Gleixner  * Forward the timer expiry so it will expire in the future.
9425cee9645SThomas Gleixner  * Returns the number of overruns.
94391e5a217SThomas Gleixner  *
94491e5a217SThomas Gleixner  * Can be safely called from the callback function of @timer. If
94591e5a217SThomas Gleixner  * called from other contexts @timer must neither be enqueued nor
94691e5a217SThomas Gleixner  * running the callback and the caller needs to take care of
94791e5a217SThomas Gleixner  * serialization.
94891e5a217SThomas Gleixner  *
94991e5a217SThomas Gleixner  * Note: This only updates the timer expiry value and does not requeue
95091e5a217SThomas Gleixner  * the timer.
9515cee9645SThomas Gleixner  */
9525cee9645SThomas Gleixner u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
9535cee9645SThomas Gleixner {
9545cee9645SThomas Gleixner 	u64 orun = 1;
9555cee9645SThomas Gleixner 	ktime_t delta;
9565cee9645SThomas Gleixner 
9575cee9645SThomas Gleixner 	delta = ktime_sub(now, hrtimer_get_expires(timer));
9585cee9645SThomas Gleixner 
9592456e855SThomas Gleixner 	if (delta < 0)
9605cee9645SThomas Gleixner 		return 0;
9615cee9645SThomas Gleixner 
9625de2755cSPeter Zijlstra 	if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
9635de2755cSPeter Zijlstra 		return 0;
9645de2755cSPeter Zijlstra 
9652456e855SThomas Gleixner 	if (interval < hrtimer_resolution)
9662456e855SThomas Gleixner 		interval = hrtimer_resolution;
9675cee9645SThomas Gleixner 
9682456e855SThomas Gleixner 	if (unlikely(delta >= interval)) {
9695cee9645SThomas Gleixner 		s64 incr = ktime_to_ns(interval);
9705cee9645SThomas Gleixner 
9715cee9645SThomas Gleixner 		orun = ktime_divns(delta, incr);
9725cee9645SThomas Gleixner 		hrtimer_add_expires_ns(timer, incr * orun);
9732456e855SThomas Gleixner 		if (hrtimer_get_expires_tv64(timer) > now)
9745cee9645SThomas Gleixner 			return orun;
9755cee9645SThomas Gleixner 		/*
9765cee9645SThomas Gleixner 		 * This (and the ktime_add() below) is the
9775cee9645SThomas Gleixner 		 * correction for exact:
9785cee9645SThomas Gleixner 		 */
9795cee9645SThomas Gleixner 		orun++;
9805cee9645SThomas Gleixner 	}
9815cee9645SThomas Gleixner 	hrtimer_add_expires(timer, interval);
9825cee9645SThomas Gleixner 
9835cee9645SThomas Gleixner 	return orun;
9845cee9645SThomas Gleixner }
9855cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_forward);
9865cee9645SThomas Gleixner 
9875cee9645SThomas Gleixner /*
9885cee9645SThomas Gleixner  * enqueue_hrtimer - internal function to (re)start a timer
9895cee9645SThomas Gleixner  *
9905cee9645SThomas Gleixner  * The timer is inserted in expiry order. Insertion into the
9915cee9645SThomas Gleixner  * red black tree is O(log(n)). Must hold the base lock.
9925cee9645SThomas Gleixner  *
9935cee9645SThomas Gleixner  * Returns 1 when the new timer is the leftmost timer in the tree.
9945cee9645SThomas Gleixner  */
9955cee9645SThomas Gleixner static int enqueue_hrtimer(struct hrtimer *timer,
99663e2ed36SAnna-Maria Gleixner 			   struct hrtimer_clock_base *base,
99763e2ed36SAnna-Maria Gleixner 			   enum hrtimer_mode mode)
9985cee9645SThomas Gleixner {
99963e2ed36SAnna-Maria Gleixner 	debug_activate(timer, mode);
10005cee9645SThomas Gleixner 
10015cee9645SThomas Gleixner 	base->cpu_base->active_bases |= 1 << base->index;
10025cee9645SThomas Gleixner 
100356144737SEric Dumazet 	/* Pairs with the lockless read in hrtimer_is_queued() */
100456144737SEric Dumazet 	WRITE_ONCE(timer->state, HRTIMER_STATE_ENQUEUED);
10055cee9645SThomas Gleixner 
1006b97f44c9SThomas Gleixner 	return timerqueue_add(&base->active, &timer->node);
10075cee9645SThomas Gleixner }
10085cee9645SThomas Gleixner 
10095cee9645SThomas Gleixner /*
10105cee9645SThomas Gleixner  * __remove_hrtimer - internal function to remove a timer
10115cee9645SThomas Gleixner  *
10125cee9645SThomas Gleixner  * Caller must hold the base lock.
10135cee9645SThomas Gleixner  *
10145cee9645SThomas Gleixner  * High resolution timer mode reprograms the clock event device when the
10155cee9645SThomas Gleixner  * timer is the one which expires next. The caller can disable this by setting
10165cee9645SThomas Gleixner  * reprogram to zero. This is useful, when the context does a reprogramming
10175cee9645SThomas Gleixner  * anyway (e.g. timer interrupt)
10185cee9645SThomas Gleixner  */
10195cee9645SThomas Gleixner static void __remove_hrtimer(struct hrtimer *timer,
10205cee9645SThomas Gleixner 			     struct hrtimer_clock_base *base,
1021203cbf77SThomas Gleixner 			     u8 newstate, int reprogram)
10225cee9645SThomas Gleixner {
1023e19ffe8bSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1024203cbf77SThomas Gleixner 	u8 state = timer->state;
10255cee9645SThomas Gleixner 
102656144737SEric Dumazet 	/* Pairs with the lockless read in hrtimer_is_queued() */
102756144737SEric Dumazet 	WRITE_ONCE(timer->state, newstate);
1028895bdfa7SThomas Gleixner 	if (!(state & HRTIMER_STATE_ENQUEUED))
1029895bdfa7SThomas Gleixner 		return;
10305cee9645SThomas Gleixner 
1031b97f44c9SThomas Gleixner 	if (!timerqueue_del(&base->active, &timer->node))
1032e19ffe8bSThomas Gleixner 		cpu_base->active_bases &= ~(1 << base->index);
1033d9f0acdeSViresh Kumar 
1034895bdfa7SThomas Gleixner 	/*
1035895bdfa7SThomas Gleixner 	 * Note: If reprogram is false we do not update
1036895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. This happens when we remove the first
1037895bdfa7SThomas Gleixner 	 * timer on a remote cpu. No harm as we never dereference
1038895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. So the worst thing what can happen is
10394bf07f65SIngo Molnar 	 * an superfluous call to hrtimer_force_reprogram() on the
1040895bdfa7SThomas Gleixner 	 * remote cpu later on if the same timer gets enqueued again.
1041895bdfa7SThomas Gleixner 	 */
1042895bdfa7SThomas Gleixner 	if (reprogram && timer == cpu_base->next_timer)
1043e19ffe8bSThomas Gleixner 		hrtimer_force_reprogram(cpu_base, 1);
10445cee9645SThomas Gleixner }
10455cee9645SThomas Gleixner 
10465cee9645SThomas Gleixner /*
10475cee9645SThomas Gleixner  * remove hrtimer, called with base lock held
10485cee9645SThomas Gleixner  */
10495cee9645SThomas Gleixner static inline int
1050627ef5aeSThomas Gleixner remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base,
1051627ef5aeSThomas Gleixner 	       bool restart, bool keep_local)
10525cee9645SThomas Gleixner {
1053203cbf77SThomas Gleixner 	u8 state = timer->state;
105456144737SEric Dumazet 
105556144737SEric Dumazet 	if (state & HRTIMER_STATE_ENQUEUED) {
1056627ef5aeSThomas Gleixner 		bool reprogram;
10575cee9645SThomas Gleixner 
10585cee9645SThomas Gleixner 		/*
10595cee9645SThomas Gleixner 		 * Remove the timer and force reprogramming when high
10605cee9645SThomas Gleixner 		 * resolution mode is active and the timer is on the current
10615cee9645SThomas Gleixner 		 * CPU. If we remove a timer on another CPU, reprogramming is
10625cee9645SThomas Gleixner 		 * skipped. The interrupt event on this CPU is fired and
10635cee9645SThomas Gleixner 		 * reprogramming happens in the interrupt handler. This is a
10645cee9645SThomas Gleixner 		 * rare case and less expensive than a smp call.
10655cee9645SThomas Gleixner 		 */
10665cee9645SThomas Gleixner 		debug_deactivate(timer);
1067dc5df73bSChristoph Lameter 		reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
10688edfb036SPeter Zijlstra 
1069627ef5aeSThomas Gleixner 		/*
1070627ef5aeSThomas Gleixner 		 * If the timer is not restarted then reprogramming is
1071627ef5aeSThomas Gleixner 		 * required if the timer is local. If it is local and about
1072627ef5aeSThomas Gleixner 		 * to be restarted, avoid programming it twice (on removal
1073627ef5aeSThomas Gleixner 		 * and a moment later when it's requeued).
1074627ef5aeSThomas Gleixner 		 */
1075887d9dc9SPeter Zijlstra 		if (!restart)
1076887d9dc9SPeter Zijlstra 			state = HRTIMER_STATE_INACTIVE;
1077627ef5aeSThomas Gleixner 		else
1078627ef5aeSThomas Gleixner 			reprogram &= !keep_local;
1079887d9dc9SPeter Zijlstra 
10805cee9645SThomas Gleixner 		__remove_hrtimer(timer, base, state, reprogram);
10815cee9645SThomas Gleixner 		return 1;
10825cee9645SThomas Gleixner 	}
10835cee9645SThomas Gleixner 	return 0;
10845cee9645SThomas Gleixner }
10855cee9645SThomas Gleixner 
1086203cbf77SThomas Gleixner static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
1087203cbf77SThomas Gleixner 					    const enum hrtimer_mode mode)
1088203cbf77SThomas Gleixner {
1089203cbf77SThomas Gleixner #ifdef CONFIG_TIME_LOW_RES
1090203cbf77SThomas Gleixner 	/*
1091203cbf77SThomas Gleixner 	 * CONFIG_TIME_LOW_RES indicates that the system has no way to return
1092203cbf77SThomas Gleixner 	 * granular time values. For relative timers we add hrtimer_resolution
1093203cbf77SThomas Gleixner 	 * (i.e. one jiffie) to prevent short timeouts.
1094203cbf77SThomas Gleixner 	 */
1095203cbf77SThomas Gleixner 	timer->is_rel = mode & HRTIMER_MODE_REL;
1096203cbf77SThomas Gleixner 	if (timer->is_rel)
10978b0e1953SThomas Gleixner 		tim = ktime_add_safe(tim, hrtimer_resolution);
1098203cbf77SThomas Gleixner #endif
1099203cbf77SThomas Gleixner 	return tim;
1100203cbf77SThomas Gleixner }
1101203cbf77SThomas Gleixner 
11025da70160SAnna-Maria Gleixner static void
11035da70160SAnna-Maria Gleixner hrtimer_update_softirq_timer(struct hrtimer_cpu_base *cpu_base, bool reprogram)
11045da70160SAnna-Maria Gleixner {
11055da70160SAnna-Maria Gleixner 	ktime_t expires;
11065da70160SAnna-Maria Gleixner 
11075da70160SAnna-Maria Gleixner 	/*
11085da70160SAnna-Maria Gleixner 	 * Find the next SOFT expiration.
11095da70160SAnna-Maria Gleixner 	 */
11105da70160SAnna-Maria Gleixner 	expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
11115da70160SAnna-Maria Gleixner 
11125da70160SAnna-Maria Gleixner 	/*
11135da70160SAnna-Maria Gleixner 	 * reprogramming needs to be triggered, even if the next soft
11145da70160SAnna-Maria Gleixner 	 * hrtimer expires at the same time than the next hard
11155da70160SAnna-Maria Gleixner 	 * hrtimer. cpu_base->softirq_expires_next needs to be updated!
11165da70160SAnna-Maria Gleixner 	 */
11175da70160SAnna-Maria Gleixner 	if (expires == KTIME_MAX)
11185da70160SAnna-Maria Gleixner 		return;
11195da70160SAnna-Maria Gleixner 
11205da70160SAnna-Maria Gleixner 	/*
11215da70160SAnna-Maria Gleixner 	 * cpu_base->*next_timer is recomputed by __hrtimer_get_next_event()
11225da70160SAnna-Maria Gleixner 	 * cpu_base->*expires_next is only set by hrtimer_reprogram()
11235da70160SAnna-Maria Gleixner 	 */
11245da70160SAnna-Maria Gleixner 	hrtimer_reprogram(cpu_base->softirq_next_timer, reprogram);
11255da70160SAnna-Maria Gleixner }
11265da70160SAnna-Maria Gleixner 
1127138a6b7aSAnna-Maria Gleixner static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1128138a6b7aSAnna-Maria Gleixner 				    u64 delta_ns, const enum hrtimer_mode mode,
1129138a6b7aSAnna-Maria Gleixner 				    struct hrtimer_clock_base *base)
11305cee9645SThomas Gleixner {
1131138a6b7aSAnna-Maria Gleixner 	struct hrtimer_clock_base *new_base;
1132627ef5aeSThomas Gleixner 	bool force_local, first;
11335cee9645SThomas Gleixner 
1134627ef5aeSThomas Gleixner 	/*
1135627ef5aeSThomas Gleixner 	 * If the timer is on the local cpu base and is the first expiring
1136627ef5aeSThomas Gleixner 	 * timer then this might end up reprogramming the hardware twice
1137627ef5aeSThomas Gleixner 	 * (on removal and on enqueue). To avoid that by prevent the
1138627ef5aeSThomas Gleixner 	 * reprogram on removal, keep the timer local to the current CPU
1139627ef5aeSThomas Gleixner 	 * and enforce reprogramming after it is queued no matter whether
1140627ef5aeSThomas Gleixner 	 * it is the new first expiring timer again or not.
1141627ef5aeSThomas Gleixner 	 */
1142627ef5aeSThomas Gleixner 	force_local = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
1143627ef5aeSThomas Gleixner 	force_local &= base->cpu_base->next_timer == timer;
1144627ef5aeSThomas Gleixner 
1145627ef5aeSThomas Gleixner 	/*
1146627ef5aeSThomas Gleixner 	 * Remove an active timer from the queue. In case it is not queued
1147627ef5aeSThomas Gleixner 	 * on the current CPU, make sure that remove_hrtimer() updates the
1148627ef5aeSThomas Gleixner 	 * remote data correctly.
1149627ef5aeSThomas Gleixner 	 *
1150627ef5aeSThomas Gleixner 	 * If it's on the current CPU and the first expiring timer, then
1151627ef5aeSThomas Gleixner 	 * skip reprogramming, keep the timer local and enforce
1152627ef5aeSThomas Gleixner 	 * reprogramming later if it was the first expiring timer.  This
1153627ef5aeSThomas Gleixner 	 * avoids programming the underlying clock event twice (once at
1154627ef5aeSThomas Gleixner 	 * removal and once after enqueue).
1155627ef5aeSThomas Gleixner 	 */
1156627ef5aeSThomas Gleixner 	remove_hrtimer(timer, base, true, force_local);
11575cee9645SThomas Gleixner 
1158203cbf77SThomas Gleixner 	if (mode & HRTIMER_MODE_REL)
11595cee9645SThomas Gleixner 		tim = ktime_add_safe(tim, base->get_time());
1160203cbf77SThomas Gleixner 
1161203cbf77SThomas Gleixner 	tim = hrtimer_update_lowres(timer, tim, mode);
11625cee9645SThomas Gleixner 
11635cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(timer, tim, delta_ns);
11645cee9645SThomas Gleixner 
11655cee9645SThomas Gleixner 	/* Switch the timer base, if necessary: */
1166627ef5aeSThomas Gleixner 	if (!force_local) {
1167627ef5aeSThomas Gleixner 		new_base = switch_hrtimer_base(timer, base,
1168627ef5aeSThomas Gleixner 					       mode & HRTIMER_MODE_PINNED);
1169627ef5aeSThomas Gleixner 	} else {
1170627ef5aeSThomas Gleixner 		new_base = base;
1171627ef5aeSThomas Gleixner 	}
11725cee9645SThomas Gleixner 
1173627ef5aeSThomas Gleixner 	first = enqueue_hrtimer(timer, new_base, mode);
1174627ef5aeSThomas Gleixner 	if (!force_local)
1175627ef5aeSThomas Gleixner 		return first;
1176627ef5aeSThomas Gleixner 
1177627ef5aeSThomas Gleixner 	/*
1178627ef5aeSThomas Gleixner 	 * Timer was forced to stay on the current CPU to avoid
1179627ef5aeSThomas Gleixner 	 * reprogramming on removal and enqueue. Force reprogram the
1180627ef5aeSThomas Gleixner 	 * hardware by evaluating the new first expiring timer.
1181627ef5aeSThomas Gleixner 	 */
1182627ef5aeSThomas Gleixner 	hrtimer_force_reprogram(new_base->cpu_base, 1);
1183627ef5aeSThomas Gleixner 	return 0;
1184138a6b7aSAnna-Maria Gleixner }
11855da70160SAnna-Maria Gleixner 
1186138a6b7aSAnna-Maria Gleixner /**
1187138a6b7aSAnna-Maria Gleixner  * hrtimer_start_range_ns - (re)start an hrtimer
1188138a6b7aSAnna-Maria Gleixner  * @timer:	the timer to be added
1189138a6b7aSAnna-Maria Gleixner  * @tim:	expiry time
1190138a6b7aSAnna-Maria Gleixner  * @delta_ns:	"slack" range for the timer
1191138a6b7aSAnna-Maria Gleixner  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
11925da70160SAnna-Maria Gleixner  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
11935da70160SAnna-Maria Gleixner  *		softirq based mode is considered for debug purpose only!
1194138a6b7aSAnna-Maria Gleixner  */
1195138a6b7aSAnna-Maria Gleixner void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1196138a6b7aSAnna-Maria Gleixner 			    u64 delta_ns, const enum hrtimer_mode mode)
1197138a6b7aSAnna-Maria Gleixner {
1198138a6b7aSAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1199138a6b7aSAnna-Maria Gleixner 	unsigned long flags;
120049a2a075SViresh Kumar 
12015da70160SAnna-Maria Gleixner 	/*
12025da70160SAnna-Maria Gleixner 	 * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
12030ab6a3ddSThomas Gleixner 	 * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard
12040ab6a3ddSThomas Gleixner 	 * expiry mode because unmarked timers are moved to softirq expiry.
12055da70160SAnna-Maria Gleixner 	 */
12060ab6a3ddSThomas Gleixner 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
12075da70160SAnna-Maria Gleixner 		WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft);
12080ab6a3ddSThomas Gleixner 	else
12090ab6a3ddSThomas Gleixner 		WARN_ON_ONCE(!(mode & HRTIMER_MODE_HARD) ^ !timer->is_hard);
12105da70160SAnna-Maria Gleixner 
1211138a6b7aSAnna-Maria Gleixner 	base = lock_hrtimer_base(timer, &flags);
1212138a6b7aSAnna-Maria Gleixner 
1213138a6b7aSAnna-Maria Gleixner 	if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base))
12145da70160SAnna-Maria Gleixner 		hrtimer_reprogram(timer, true);
1215138a6b7aSAnna-Maria Gleixner 
12165cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
12175cee9645SThomas Gleixner }
12185cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
12195cee9645SThomas Gleixner 
12205cee9645SThomas Gleixner /**
12215cee9645SThomas Gleixner  * hrtimer_try_to_cancel - try to deactivate a timer
12225cee9645SThomas Gleixner  * @timer:	hrtimer to stop
12235cee9645SThomas Gleixner  *
12245cee9645SThomas Gleixner  * Returns:
122551633704SMauro Carvalho Chehab  *
122651633704SMauro Carvalho Chehab  *  *  0 when the timer was not active
122751633704SMauro Carvalho Chehab  *  *  1 when the timer was active
122851633704SMauro Carvalho Chehab  *  * -1 when the timer is currently executing the callback function and
12295cee9645SThomas Gleixner  *    cannot be stopped
12305cee9645SThomas Gleixner  */
12315cee9645SThomas Gleixner int hrtimer_try_to_cancel(struct hrtimer *timer)
12325cee9645SThomas Gleixner {
12335cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
12345cee9645SThomas Gleixner 	unsigned long flags;
12355cee9645SThomas Gleixner 	int ret = -1;
12365cee9645SThomas Gleixner 
123719d9f422SThomas Gleixner 	/*
123819d9f422SThomas Gleixner 	 * Check lockless first. If the timer is not active (neither
123919d9f422SThomas Gleixner 	 * enqueued nor running the callback, nothing to do here.  The
124019d9f422SThomas Gleixner 	 * base lock does not serialize against a concurrent enqueue,
124119d9f422SThomas Gleixner 	 * so we can avoid taking it.
124219d9f422SThomas Gleixner 	 */
124319d9f422SThomas Gleixner 	if (!hrtimer_active(timer))
124419d9f422SThomas Gleixner 		return 0;
124519d9f422SThomas Gleixner 
12465cee9645SThomas Gleixner 	base = lock_hrtimer_base(timer, &flags);
12475cee9645SThomas Gleixner 
12485cee9645SThomas Gleixner 	if (!hrtimer_callback_running(timer))
1249627ef5aeSThomas Gleixner 		ret = remove_hrtimer(timer, base, false, false);
12505cee9645SThomas Gleixner 
12515cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
12525cee9645SThomas Gleixner 
12535cee9645SThomas Gleixner 	return ret;
12545cee9645SThomas Gleixner 
12555cee9645SThomas Gleixner }
12565cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
12575cee9645SThomas Gleixner 
1258f61eff83SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT
1259f61eff83SAnna-Maria Gleixner static void hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base)
1260f61eff83SAnna-Maria Gleixner {
1261f61eff83SAnna-Maria Gleixner 	spin_lock_init(&base->softirq_expiry_lock);
1262f61eff83SAnna-Maria Gleixner }
1263f61eff83SAnna-Maria Gleixner 
1264f61eff83SAnna-Maria Gleixner static void hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base)
1265f61eff83SAnna-Maria Gleixner {
1266f61eff83SAnna-Maria Gleixner 	spin_lock(&base->softirq_expiry_lock);
1267f61eff83SAnna-Maria Gleixner }
1268f61eff83SAnna-Maria Gleixner 
1269f61eff83SAnna-Maria Gleixner static void hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base)
1270f61eff83SAnna-Maria Gleixner {
1271f61eff83SAnna-Maria Gleixner 	spin_unlock(&base->softirq_expiry_lock);
1272f61eff83SAnna-Maria Gleixner }
1273f61eff83SAnna-Maria Gleixner 
1274f61eff83SAnna-Maria Gleixner /*
1275f61eff83SAnna-Maria Gleixner  * The counterpart to hrtimer_cancel_wait_running().
1276f61eff83SAnna-Maria Gleixner  *
1277f61eff83SAnna-Maria Gleixner  * If there is a waiter for cpu_base->expiry_lock, then it was waiting for
12784bf07f65SIngo Molnar  * the timer callback to finish. Drop expiry_lock and reacquire it. That
1279f61eff83SAnna-Maria Gleixner  * allows the waiter to acquire the lock and make progress.
1280f61eff83SAnna-Maria Gleixner  */
1281f61eff83SAnna-Maria Gleixner static void hrtimer_sync_wait_running(struct hrtimer_cpu_base *cpu_base,
1282f61eff83SAnna-Maria Gleixner 				      unsigned long flags)
1283f61eff83SAnna-Maria Gleixner {
1284f61eff83SAnna-Maria Gleixner 	if (atomic_read(&cpu_base->timer_waiters)) {
1285f61eff83SAnna-Maria Gleixner 		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1286f61eff83SAnna-Maria Gleixner 		spin_unlock(&cpu_base->softirq_expiry_lock);
1287f61eff83SAnna-Maria Gleixner 		spin_lock(&cpu_base->softirq_expiry_lock);
1288f61eff83SAnna-Maria Gleixner 		raw_spin_lock_irq(&cpu_base->lock);
1289f61eff83SAnna-Maria Gleixner 	}
1290f61eff83SAnna-Maria Gleixner }
1291f61eff83SAnna-Maria Gleixner 
1292f61eff83SAnna-Maria Gleixner /*
1293f61eff83SAnna-Maria Gleixner  * This function is called on PREEMPT_RT kernels when the fast path
1294f61eff83SAnna-Maria Gleixner  * deletion of a timer failed because the timer callback function was
1295f61eff83SAnna-Maria Gleixner  * running.
1296f61eff83SAnna-Maria Gleixner  *
12970bee3b60SFrederic Weisbecker  * This prevents priority inversion: if the soft irq thread is preempted
12980bee3b60SFrederic Weisbecker  * in the middle of a timer callback, then calling del_timer_sync() can
12990bee3b60SFrederic Weisbecker  * lead to two issues:
13000bee3b60SFrederic Weisbecker  *
13010bee3b60SFrederic Weisbecker  *  - If the caller is on a remote CPU then it has to spin wait for the timer
13020bee3b60SFrederic Weisbecker  *    handler to complete. This can result in unbound priority inversion.
13030bee3b60SFrederic Weisbecker  *
13040bee3b60SFrederic Weisbecker  *  - If the caller originates from the task which preempted the timer
13050bee3b60SFrederic Weisbecker  *    handler on the same CPU, then spin waiting for the timer handler to
13060bee3b60SFrederic Weisbecker  *    complete is never going to end.
1307f61eff83SAnna-Maria Gleixner  */
1308f61eff83SAnna-Maria Gleixner void hrtimer_cancel_wait_running(const struct hrtimer *timer)
1309f61eff83SAnna-Maria Gleixner {
1310dd2261edSJulien Grall 	/* Lockless read. Prevent the compiler from reloading it below */
1311dd2261edSJulien Grall 	struct hrtimer_clock_base *base = READ_ONCE(timer->base);
1312f61eff83SAnna-Maria Gleixner 
131368b2c8c1SJulien Grall 	/*
131468b2c8c1SJulien Grall 	 * Just relax if the timer expires in hard interrupt context or if
131568b2c8c1SJulien Grall 	 * it is currently on the migration base.
131668b2c8c1SJulien Grall 	 */
13175d2295f3SSebastian Andrzej Siewior 	if (!timer->is_soft || is_migration_base(base)) {
1318f61eff83SAnna-Maria Gleixner 		cpu_relax();
1319f61eff83SAnna-Maria Gleixner 		return;
1320f61eff83SAnna-Maria Gleixner 	}
1321f61eff83SAnna-Maria Gleixner 
1322f61eff83SAnna-Maria Gleixner 	/*
1323f61eff83SAnna-Maria Gleixner 	 * Mark the base as contended and grab the expiry lock, which is
1324f61eff83SAnna-Maria Gleixner 	 * held by the softirq across the timer callback. Drop the lock
1325f61eff83SAnna-Maria Gleixner 	 * immediately so the softirq can expire the next timer. In theory
1326f61eff83SAnna-Maria Gleixner 	 * the timer could already be running again, but that's more than
1327f61eff83SAnna-Maria Gleixner 	 * unlikely and just causes another wait loop.
1328f61eff83SAnna-Maria Gleixner 	 */
1329f61eff83SAnna-Maria Gleixner 	atomic_inc(&base->cpu_base->timer_waiters);
1330f61eff83SAnna-Maria Gleixner 	spin_lock_bh(&base->cpu_base->softirq_expiry_lock);
1331f61eff83SAnna-Maria Gleixner 	atomic_dec(&base->cpu_base->timer_waiters);
1332f61eff83SAnna-Maria Gleixner 	spin_unlock_bh(&base->cpu_base->softirq_expiry_lock);
1333f61eff83SAnna-Maria Gleixner }
1334f61eff83SAnna-Maria Gleixner #else
1335f61eff83SAnna-Maria Gleixner static inline void
1336f61eff83SAnna-Maria Gleixner hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) { }
1337f61eff83SAnna-Maria Gleixner static inline void
1338f61eff83SAnna-Maria Gleixner hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) { }
1339f61eff83SAnna-Maria Gleixner static inline void
1340f61eff83SAnna-Maria Gleixner hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) { }
1341f61eff83SAnna-Maria Gleixner static inline void hrtimer_sync_wait_running(struct hrtimer_cpu_base *base,
1342f61eff83SAnna-Maria Gleixner 					     unsigned long flags) { }
1343f61eff83SAnna-Maria Gleixner #endif
1344f61eff83SAnna-Maria Gleixner 
13455cee9645SThomas Gleixner /**
13465cee9645SThomas Gleixner  * hrtimer_cancel - cancel a timer and wait for the handler to finish.
13475cee9645SThomas Gleixner  * @timer:	the timer to be cancelled
13485cee9645SThomas Gleixner  *
13495cee9645SThomas Gleixner  * Returns:
13505cee9645SThomas Gleixner  *  0 when the timer was not active
13515cee9645SThomas Gleixner  *  1 when the timer was active
13525cee9645SThomas Gleixner  */
13535cee9645SThomas Gleixner int hrtimer_cancel(struct hrtimer *timer)
13545cee9645SThomas Gleixner {
1355f61eff83SAnna-Maria Gleixner 	int ret;
13565cee9645SThomas Gleixner 
1357f61eff83SAnna-Maria Gleixner 	do {
1358f61eff83SAnna-Maria Gleixner 		ret = hrtimer_try_to_cancel(timer);
1359f61eff83SAnna-Maria Gleixner 
1360f61eff83SAnna-Maria Gleixner 		if (ret < 0)
1361f61eff83SAnna-Maria Gleixner 			hrtimer_cancel_wait_running(timer);
1362f61eff83SAnna-Maria Gleixner 	} while (ret < 0);
13635cee9645SThomas Gleixner 	return ret;
13645cee9645SThomas Gleixner }
13655cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_cancel);
13665cee9645SThomas Gleixner 
13675cee9645SThomas Gleixner /**
136866981c37SMauro Carvalho Chehab  * __hrtimer_get_remaining - get remaining time for the timer
13695cee9645SThomas Gleixner  * @timer:	the timer to read
1370203cbf77SThomas Gleixner  * @adjust:	adjust relative timers when CONFIG_TIME_LOW_RES=y
13715cee9645SThomas Gleixner  */
1372203cbf77SThomas Gleixner ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
13735cee9645SThomas Gleixner {
13745cee9645SThomas Gleixner 	unsigned long flags;
13755cee9645SThomas Gleixner 	ktime_t rem;
13765cee9645SThomas Gleixner 
13775cee9645SThomas Gleixner 	lock_hrtimer_base(timer, &flags);
1378203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1379203cbf77SThomas Gleixner 		rem = hrtimer_expires_remaining_adjusted(timer);
1380203cbf77SThomas Gleixner 	else
13815cee9645SThomas Gleixner 		rem = hrtimer_expires_remaining(timer);
13825cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
13835cee9645SThomas Gleixner 
13845cee9645SThomas Gleixner 	return rem;
13855cee9645SThomas Gleixner }
1386203cbf77SThomas Gleixner EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
13875cee9645SThomas Gleixner 
13885cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
13895cee9645SThomas Gleixner /**
13905cee9645SThomas Gleixner  * hrtimer_get_next_event - get the time until next expiry event
13915cee9645SThomas Gleixner  *
1392c1ad348bSThomas Gleixner  * Returns the next expiry time or KTIME_MAX if no timer is pending.
13935cee9645SThomas Gleixner  */
1394c1ad348bSThomas Gleixner u64 hrtimer_get_next_event(void)
13955cee9645SThomas Gleixner {
1396dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1397c1ad348bSThomas Gleixner 	u64 expires = KTIME_MAX;
13985cee9645SThomas Gleixner 	unsigned long flags;
13995cee9645SThomas Gleixner 
14005cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
14015cee9645SThomas Gleixner 
1402e19ffe8bSThomas Gleixner 	if (!__hrtimer_hres_active(cpu_base))
14035da70160SAnna-Maria Gleixner 		expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
14045cee9645SThomas Gleixner 
14055cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
14065cee9645SThomas Gleixner 
1407c1ad348bSThomas Gleixner 	return expires;
14085cee9645SThomas Gleixner }
1409a59855cdSRafael J. Wysocki 
1410a59855cdSRafael J. Wysocki /**
1411a59855cdSRafael J. Wysocki  * hrtimer_next_event_without - time until next expiry event w/o one timer
1412a59855cdSRafael J. Wysocki  * @exclude:	timer to exclude
1413a59855cdSRafael J. Wysocki  *
1414a59855cdSRafael J. Wysocki  * Returns the next expiry time over all timers except for the @exclude one or
1415a59855cdSRafael J. Wysocki  * KTIME_MAX if none of them is pending.
1416a59855cdSRafael J. Wysocki  */
1417a59855cdSRafael J. Wysocki u64 hrtimer_next_event_without(const struct hrtimer *exclude)
1418a59855cdSRafael J. Wysocki {
1419a59855cdSRafael J. Wysocki 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1420a59855cdSRafael J. Wysocki 	u64 expires = KTIME_MAX;
1421a59855cdSRafael J. Wysocki 	unsigned long flags;
1422a59855cdSRafael J. Wysocki 
1423a59855cdSRafael J. Wysocki 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
1424a59855cdSRafael J. Wysocki 
1425a59855cdSRafael J. Wysocki 	if (__hrtimer_hres_active(cpu_base)) {
1426a59855cdSRafael J. Wysocki 		unsigned int active;
1427a59855cdSRafael J. Wysocki 
1428a59855cdSRafael J. Wysocki 		if (!cpu_base->softirq_activated) {
1429a59855cdSRafael J. Wysocki 			active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
1430a59855cdSRafael J. Wysocki 			expires = __hrtimer_next_event_base(cpu_base, exclude,
1431a59855cdSRafael J. Wysocki 							    active, KTIME_MAX);
1432a59855cdSRafael J. Wysocki 		}
1433a59855cdSRafael J. Wysocki 		active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
1434a59855cdSRafael J. Wysocki 		expires = __hrtimer_next_event_base(cpu_base, exclude, active,
1435a59855cdSRafael J. Wysocki 						    expires);
1436a59855cdSRafael J. Wysocki 	}
1437a59855cdSRafael J. Wysocki 
1438a59855cdSRafael J. Wysocki 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1439a59855cdSRafael J. Wysocki 
1440a59855cdSRafael J. Wysocki 	return expires;
1441a59855cdSRafael J. Wysocki }
14425cee9645SThomas Gleixner #endif
14435cee9645SThomas Gleixner 
1444336a9cdeSMarc Zyngier static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1445336a9cdeSMarc Zyngier {
1446336a9cdeSMarc Zyngier 	if (likely(clock_id < MAX_CLOCKS)) {
1447336a9cdeSMarc Zyngier 		int base = hrtimer_clock_to_base_table[clock_id];
1448336a9cdeSMarc Zyngier 
1449336a9cdeSMarc Zyngier 		if (likely(base != HRTIMER_MAX_CLOCK_BASES))
1450336a9cdeSMarc Zyngier 			return base;
1451336a9cdeSMarc Zyngier 	}
1452336a9cdeSMarc Zyngier 	WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1453336a9cdeSMarc Zyngier 	return HRTIMER_BASE_MONOTONIC;
1454336a9cdeSMarc Zyngier }
1455336a9cdeSMarc Zyngier 
14565cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
14575cee9645SThomas Gleixner 			   enum hrtimer_mode mode)
14585cee9645SThomas Gleixner {
145942f42da4SAnna-Maria Gleixner 	bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
14605cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base;
1461f5c2f021SSebastian Andrzej Siewior 	int base;
1462f5c2f021SSebastian Andrzej Siewior 
1463f5c2f021SSebastian Andrzej Siewior 	/*
14644bf07f65SIngo Molnar 	 * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
1465f5c2f021SSebastian Andrzej Siewior 	 * marked for hard interrupt expiry mode are moved into soft
1466f5c2f021SSebastian Andrzej Siewior 	 * interrupt context for latency reasons and because the callbacks
1467f5c2f021SSebastian Andrzej Siewior 	 * can invoke functions which might sleep on RT, e.g. spin_lock().
1468f5c2f021SSebastian Andrzej Siewior 	 */
1469f5c2f021SSebastian Andrzej Siewior 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD))
1470f5c2f021SSebastian Andrzej Siewior 		softtimer = true;
14715cee9645SThomas Gleixner 
14725cee9645SThomas Gleixner 	memset(timer, 0, sizeof(struct hrtimer));
14735cee9645SThomas Gleixner 
147422127e93SChristoph Lameter 	cpu_base = raw_cpu_ptr(&hrtimer_bases);
14755cee9645SThomas Gleixner 
147648d0c9beSAnna-Maria Gleixner 	/*
147748d0c9beSAnna-Maria Gleixner 	 * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
147848d0c9beSAnna-Maria Gleixner 	 * clock modifications, so they needs to become CLOCK_MONOTONIC to
147948d0c9beSAnna-Maria Gleixner 	 * ensure POSIX compliance.
148048d0c9beSAnna-Maria Gleixner 	 */
148148d0c9beSAnna-Maria Gleixner 	if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
14825cee9645SThomas Gleixner 		clock_id = CLOCK_MONOTONIC;
14835cee9645SThomas Gleixner 
1484f5c2f021SSebastian Andrzej Siewior 	base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
148542f42da4SAnna-Maria Gleixner 	base += hrtimer_clockid_to_base(clock_id);
148642f42da4SAnna-Maria Gleixner 	timer->is_soft = softtimer;
148740db1739SSebastian Andrzej Siewior 	timer->is_hard = !!(mode & HRTIMER_MODE_HARD);
14885cee9645SThomas Gleixner 	timer->base = &cpu_base->clock_base[base];
14895cee9645SThomas Gleixner 	timerqueue_init(&timer->node);
14905cee9645SThomas Gleixner }
14915cee9645SThomas Gleixner 
14925cee9645SThomas Gleixner /**
14935cee9645SThomas Gleixner  * hrtimer_init - initialize a timer to the given clock
14945cee9645SThomas Gleixner  * @timer:	the timer to be initialized
14955cee9645SThomas Gleixner  * @clock_id:	the clock to be used
14964bf07f65SIngo Molnar  * @mode:       The modes which are relevant for initialization:
149742f42da4SAnna-Maria Gleixner  *              HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
149842f42da4SAnna-Maria Gleixner  *              HRTIMER_MODE_REL_SOFT
149942f42da4SAnna-Maria Gleixner  *
150042f42da4SAnna-Maria Gleixner  *              The PINNED variants of the above can be handed in,
150142f42da4SAnna-Maria Gleixner  *              but the PINNED bit is ignored as pinning happens
150242f42da4SAnna-Maria Gleixner  *              when the hrtimer is started
15035cee9645SThomas Gleixner  */
15045cee9645SThomas Gleixner void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
15055cee9645SThomas Gleixner 		  enum hrtimer_mode mode)
15065cee9645SThomas Gleixner {
15075cee9645SThomas Gleixner 	debug_init(timer, clock_id, mode);
15085cee9645SThomas Gleixner 	__hrtimer_init(timer, clock_id, mode);
15095cee9645SThomas Gleixner }
15105cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init);
15115cee9645SThomas Gleixner 
1512887d9dc9SPeter Zijlstra /*
1513887d9dc9SPeter Zijlstra  * A timer is active, when it is enqueued into the rbtree or the
1514887d9dc9SPeter Zijlstra  * callback function is running or it's in the state of being migrated
1515887d9dc9SPeter Zijlstra  * to another cpu.
15165cee9645SThomas Gleixner  *
1517887d9dc9SPeter Zijlstra  * It is important for this function to not return a false negative.
15185cee9645SThomas Gleixner  */
1519887d9dc9SPeter Zijlstra bool hrtimer_active(const struct hrtimer *timer)
15205cee9645SThomas Gleixner {
15213f0b9e8eSAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1522887d9dc9SPeter Zijlstra 	unsigned int seq;
15235cee9645SThomas Gleixner 
1524887d9dc9SPeter Zijlstra 	do {
15253f0b9e8eSAnna-Maria Gleixner 		base = READ_ONCE(timer->base);
15263f0b9e8eSAnna-Maria Gleixner 		seq = raw_read_seqcount_begin(&base->seq);
15275cee9645SThomas Gleixner 
1528887d9dc9SPeter Zijlstra 		if (timer->state != HRTIMER_STATE_INACTIVE ||
15293f0b9e8eSAnna-Maria Gleixner 		    base->running == timer)
1530887d9dc9SPeter Zijlstra 			return true;
1531887d9dc9SPeter Zijlstra 
15323f0b9e8eSAnna-Maria Gleixner 	} while (read_seqcount_retry(&base->seq, seq) ||
15333f0b9e8eSAnna-Maria Gleixner 		 base != READ_ONCE(timer->base));
1534887d9dc9SPeter Zijlstra 
1535887d9dc9SPeter Zijlstra 	return false;
15365cee9645SThomas Gleixner }
1537887d9dc9SPeter Zijlstra EXPORT_SYMBOL_GPL(hrtimer_active);
15385cee9645SThomas Gleixner 
1539887d9dc9SPeter Zijlstra /*
1540887d9dc9SPeter Zijlstra  * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1541887d9dc9SPeter Zijlstra  * distinct sections:
1542887d9dc9SPeter Zijlstra  *
1543887d9dc9SPeter Zijlstra  *  - queued:	the timer is queued
1544887d9dc9SPeter Zijlstra  *  - callback:	the timer is being ran
1545887d9dc9SPeter Zijlstra  *  - post:	the timer is inactive or (re)queued
1546887d9dc9SPeter Zijlstra  *
1547887d9dc9SPeter Zijlstra  * On the read side we ensure we observe timer->state and cpu_base->running
1548887d9dc9SPeter Zijlstra  * from the same section, if anything changed while we looked at it, we retry.
1549887d9dc9SPeter Zijlstra  * This includes timer->base changing because sequence numbers alone are
1550887d9dc9SPeter Zijlstra  * insufficient for that.
1551887d9dc9SPeter Zijlstra  *
1552887d9dc9SPeter Zijlstra  * The sequence numbers are required because otherwise we could still observe
15534bf07f65SIngo Molnar  * a false negative if the read side got smeared over multiple consecutive
1554887d9dc9SPeter Zijlstra  * __run_hrtimer() invocations.
1555887d9dc9SPeter Zijlstra  */
1556887d9dc9SPeter Zijlstra 
155721d6d52aSThomas Gleixner static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
155821d6d52aSThomas Gleixner 			  struct hrtimer_clock_base *base,
1559dd934aa8SAnna-Maria Gleixner 			  struct hrtimer *timer, ktime_t *now,
1560eb5a4d0aSJules Irenge 			  unsigned long flags) __must_hold(&cpu_base->lock)
15615cee9645SThomas Gleixner {
15625cee9645SThomas Gleixner 	enum hrtimer_restart (*fn)(struct hrtimer *);
156373d20564SSebastian Andrzej Siewior 	bool expires_in_hardirq;
15645cee9645SThomas Gleixner 	int restart;
15655cee9645SThomas Gleixner 
1566887d9dc9SPeter Zijlstra 	lockdep_assert_held(&cpu_base->lock);
15675cee9645SThomas Gleixner 
15685cee9645SThomas Gleixner 	debug_deactivate(timer);
15693f0b9e8eSAnna-Maria Gleixner 	base->running = timer;
1570887d9dc9SPeter Zijlstra 
1571887d9dc9SPeter Zijlstra 	/*
1572887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1573887d9dc9SPeter Zijlstra 	 *
1574887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
15753f0b9e8eSAnna-Maria Gleixner 	 * hrtimer_active() cannot observe base->running == NULL &&
1576887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
1577887d9dc9SPeter Zijlstra 	 */
15783f0b9e8eSAnna-Maria Gleixner 	raw_write_seqcount_barrier(&base->seq);
1579887d9dc9SPeter Zijlstra 
1580887d9dc9SPeter Zijlstra 	__remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
15815cee9645SThomas Gleixner 	fn = timer->function;
15825cee9645SThomas Gleixner 
15835cee9645SThomas Gleixner 	/*
1584203cbf77SThomas Gleixner 	 * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
1585203cbf77SThomas Gleixner 	 * timer is restarted with a period then it becomes an absolute
1586203cbf77SThomas Gleixner 	 * timer. If its not restarted it does not matter.
1587203cbf77SThomas Gleixner 	 */
1588203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES))
1589203cbf77SThomas Gleixner 		timer->is_rel = false;
1590203cbf77SThomas Gleixner 
1591203cbf77SThomas Gleixner 	/*
1592d05ca13bSThomas Gleixner 	 * The timer is marked as running in the CPU base, so it is
1593d05ca13bSThomas Gleixner 	 * protected against migration to a different CPU even if the lock
1594d05ca13bSThomas Gleixner 	 * is dropped.
15955cee9645SThomas Gleixner 	 */
1596dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
15975cee9645SThomas Gleixner 	trace_hrtimer_expire_entry(timer, now);
159873d20564SSebastian Andrzej Siewior 	expires_in_hardirq = lockdep_hrtimer_enter(timer);
159940db1739SSebastian Andrzej Siewior 
16005cee9645SThomas Gleixner 	restart = fn(timer);
160140db1739SSebastian Andrzej Siewior 
160273d20564SSebastian Andrzej Siewior 	lockdep_hrtimer_exit(expires_in_hardirq);
16035cee9645SThomas Gleixner 	trace_hrtimer_expire_exit(timer);
1604dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irq(&cpu_base->lock);
16055cee9645SThomas Gleixner 
16065cee9645SThomas Gleixner 	/*
1607887d9dc9SPeter Zijlstra 	 * Note: We clear the running state after enqueue_hrtimer and
1608b4d90e9fSPratyush Patel 	 * we do not reprogram the event hardware. Happens either in
16095cee9645SThomas Gleixner 	 * hrtimer_start_range_ns() or in hrtimer_interrupt()
16105de2755cSPeter Zijlstra 	 *
16115de2755cSPeter Zijlstra 	 * Note: Because we dropped the cpu_base->lock above,
16125de2755cSPeter Zijlstra 	 * hrtimer_start_range_ns() can have popped in and enqueued the timer
16135de2755cSPeter Zijlstra 	 * for us already.
16145cee9645SThomas Gleixner 	 */
16155de2755cSPeter Zijlstra 	if (restart != HRTIMER_NORESTART &&
16165de2755cSPeter Zijlstra 	    !(timer->state & HRTIMER_STATE_ENQUEUED))
161763e2ed36SAnna-Maria Gleixner 		enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS);
16185cee9645SThomas Gleixner 
16195cee9645SThomas Gleixner 	/*
1620887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1621887d9dc9SPeter Zijlstra 	 *
1622887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
16233f0b9e8eSAnna-Maria Gleixner 	 * hrtimer_active() cannot observe base->running.timer == NULL &&
1624887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
16255cee9645SThomas Gleixner 	 */
16263f0b9e8eSAnna-Maria Gleixner 	raw_write_seqcount_barrier(&base->seq);
16275cee9645SThomas Gleixner 
16283f0b9e8eSAnna-Maria Gleixner 	WARN_ON_ONCE(base->running != timer);
16293f0b9e8eSAnna-Maria Gleixner 	base->running = NULL;
16305cee9645SThomas Gleixner }
16315cee9645SThomas Gleixner 
1632dd934aa8SAnna-Maria Gleixner static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
1633c458b1d1SAnna-Maria Gleixner 				 unsigned long flags, unsigned int active_mask)
16345cee9645SThomas Gleixner {
1635c272ca58SAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1636c458b1d1SAnna-Maria Gleixner 	unsigned int active = cpu_base->active_bases & active_mask;
16375cee9645SThomas Gleixner 
1638c272ca58SAnna-Maria Gleixner 	for_each_active_base(base, cpu_base, active) {
16395cee9645SThomas Gleixner 		struct timerqueue_node *node;
16405cee9645SThomas Gleixner 		ktime_t basenow;
16415cee9645SThomas Gleixner 
16425cee9645SThomas Gleixner 		basenow = ktime_add(now, base->offset);
16435cee9645SThomas Gleixner 
16445cee9645SThomas Gleixner 		while ((node = timerqueue_getnext(&base->active))) {
16455cee9645SThomas Gleixner 			struct hrtimer *timer;
16465cee9645SThomas Gleixner 
16475cee9645SThomas Gleixner 			timer = container_of(node, struct hrtimer, node);
16485cee9645SThomas Gleixner 
16495cee9645SThomas Gleixner 			/*
16505cee9645SThomas Gleixner 			 * The immediate goal for using the softexpires is
16515cee9645SThomas Gleixner 			 * minimizing wakeups, not running timers at the
16525cee9645SThomas Gleixner 			 * earliest interrupt after their soft expiration.
16535cee9645SThomas Gleixner 			 * This allows us to avoid using a Priority Search
16544bf07f65SIngo Molnar 			 * Tree, which can answer a stabbing query for
16555cee9645SThomas Gleixner 			 * overlapping intervals and instead use the simple
16565cee9645SThomas Gleixner 			 * BST we already have.
16575cee9645SThomas Gleixner 			 * We don't add extra wakeups by delaying timers that
16585cee9645SThomas Gleixner 			 * are right-of a not yet expired timer, because that
16595cee9645SThomas Gleixner 			 * timer will have to trigger a wakeup anyway.
16605cee9645SThomas Gleixner 			 */
16612456e855SThomas Gleixner 			if (basenow < hrtimer_get_softexpires_tv64(timer))
16625cee9645SThomas Gleixner 				break;
16635cee9645SThomas Gleixner 
1664dd934aa8SAnna-Maria Gleixner 			__run_hrtimer(cpu_base, base, timer, &basenow, flags);
1665f61eff83SAnna-Maria Gleixner 			if (active_mask == HRTIMER_ACTIVE_SOFT)
1666f61eff83SAnna-Maria Gleixner 				hrtimer_sync_wait_running(cpu_base, flags);
16675cee9645SThomas Gleixner 		}
16685cee9645SThomas Gleixner 	}
166921d6d52aSThomas Gleixner }
167021d6d52aSThomas Gleixner 
16715da70160SAnna-Maria Gleixner static __latent_entropy void hrtimer_run_softirq(struct softirq_action *h)
16725da70160SAnna-Maria Gleixner {
16735da70160SAnna-Maria Gleixner 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
16745da70160SAnna-Maria Gleixner 	unsigned long flags;
16755da70160SAnna-Maria Gleixner 	ktime_t now;
16765da70160SAnna-Maria Gleixner 
1677f61eff83SAnna-Maria Gleixner 	hrtimer_cpu_base_lock_expiry(cpu_base);
16785da70160SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
16795da70160SAnna-Maria Gleixner 
16805da70160SAnna-Maria Gleixner 	now = hrtimer_update_base(cpu_base);
16815da70160SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_SOFT);
16825da70160SAnna-Maria Gleixner 
16835da70160SAnna-Maria Gleixner 	cpu_base->softirq_activated = 0;
16845da70160SAnna-Maria Gleixner 	hrtimer_update_softirq_timer(cpu_base, true);
16855da70160SAnna-Maria Gleixner 
16865da70160SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1687f61eff83SAnna-Maria Gleixner 	hrtimer_cpu_base_unlock_expiry(cpu_base);
16885da70160SAnna-Maria Gleixner }
16895da70160SAnna-Maria Gleixner 
169021d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
169121d6d52aSThomas Gleixner 
169221d6d52aSThomas Gleixner /*
169321d6d52aSThomas Gleixner  * High resolution timer interrupt
169421d6d52aSThomas Gleixner  * Called with interrupts disabled
169521d6d52aSThomas Gleixner  */
169621d6d52aSThomas Gleixner void hrtimer_interrupt(struct clock_event_device *dev)
169721d6d52aSThomas Gleixner {
169821d6d52aSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
169921d6d52aSThomas Gleixner 	ktime_t expires_next, now, entry_time, delta;
1700dd934aa8SAnna-Maria Gleixner 	unsigned long flags;
170121d6d52aSThomas Gleixner 	int retries = 0;
170221d6d52aSThomas Gleixner 
170321d6d52aSThomas Gleixner 	BUG_ON(!cpu_base->hres_active);
170421d6d52aSThomas Gleixner 	cpu_base->nr_events++;
17052456e855SThomas Gleixner 	dev->next_event = KTIME_MAX;
170621d6d52aSThomas Gleixner 
1707dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
170821d6d52aSThomas Gleixner 	entry_time = now = hrtimer_update_base(cpu_base);
170921d6d52aSThomas Gleixner retry:
171021d6d52aSThomas Gleixner 	cpu_base->in_hrtirq = 1;
171121d6d52aSThomas Gleixner 	/*
171221d6d52aSThomas Gleixner 	 * We set expires_next to KTIME_MAX here with cpu_base->lock
171321d6d52aSThomas Gleixner 	 * held to prevent that a timer is enqueued in our queue via
171421d6d52aSThomas Gleixner 	 * the migration code. This does not affect enqueueing of
171521d6d52aSThomas Gleixner 	 * timers which run their callback and need to be requeued on
171621d6d52aSThomas Gleixner 	 * this CPU.
171721d6d52aSThomas Gleixner 	 */
17182456e855SThomas Gleixner 	cpu_base->expires_next = KTIME_MAX;
171921d6d52aSThomas Gleixner 
17205da70160SAnna-Maria Gleixner 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
17215da70160SAnna-Maria Gleixner 		cpu_base->softirq_expires_next = KTIME_MAX;
17225da70160SAnna-Maria Gleixner 		cpu_base->softirq_activated = 1;
17235da70160SAnna-Maria Gleixner 		raise_softirq_irqoff(HRTIMER_SOFTIRQ);
17245da70160SAnna-Maria Gleixner 	}
17255da70160SAnna-Maria Gleixner 
1726c458b1d1SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
172721d6d52aSThomas Gleixner 
172846eb1701SAnna-Maria Behnsen 	/* Reevaluate the clock bases for the [soft] next expiry */
172946eb1701SAnna-Maria Behnsen 	expires_next = hrtimer_update_next_event(cpu_base);
17305cee9645SThomas Gleixner 	/*
17315cee9645SThomas Gleixner 	 * Store the new expiry value so the migration code can verify
17325cee9645SThomas Gleixner 	 * against it.
17335cee9645SThomas Gleixner 	 */
17345cee9645SThomas Gleixner 	cpu_base->expires_next = expires_next;
17359bc74919SThomas Gleixner 	cpu_base->in_hrtirq = 0;
1736dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
17375cee9645SThomas Gleixner 
17385cee9645SThomas Gleixner 	/* Reprogramming necessary ? */
1739d2540875SViresh Kumar 	if (!tick_program_event(expires_next, 0)) {
17405cee9645SThomas Gleixner 		cpu_base->hang_detected = 0;
17415cee9645SThomas Gleixner 		return;
17425cee9645SThomas Gleixner 	}
17435cee9645SThomas Gleixner 
17445cee9645SThomas Gleixner 	/*
17455cee9645SThomas Gleixner 	 * The next timer was already expired due to:
17465cee9645SThomas Gleixner 	 * - tracing
17475cee9645SThomas Gleixner 	 * - long lasting callbacks
17485cee9645SThomas Gleixner 	 * - being scheduled away when running in a VM
17495cee9645SThomas Gleixner 	 *
17505cee9645SThomas Gleixner 	 * We need to prevent that we loop forever in the hrtimer
17515cee9645SThomas Gleixner 	 * interrupt routine. We give it 3 attempts to avoid
17525cee9645SThomas Gleixner 	 * overreacting on some spurious event.
17535cee9645SThomas Gleixner 	 *
17545cee9645SThomas Gleixner 	 * Acquire base lock for updating the offsets and retrieving
17555cee9645SThomas Gleixner 	 * the current time.
17565cee9645SThomas Gleixner 	 */
1757dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
17585cee9645SThomas Gleixner 	now = hrtimer_update_base(cpu_base);
17595cee9645SThomas Gleixner 	cpu_base->nr_retries++;
17605cee9645SThomas Gleixner 	if (++retries < 3)
17615cee9645SThomas Gleixner 		goto retry;
17625cee9645SThomas Gleixner 	/*
17635cee9645SThomas Gleixner 	 * Give the system a chance to do something else than looping
17645cee9645SThomas Gleixner 	 * here. We stored the entry time, so we know exactly how long
17655cee9645SThomas Gleixner 	 * we spent here. We schedule the next event this amount of
17665cee9645SThomas Gleixner 	 * time away.
17675cee9645SThomas Gleixner 	 */
17685cee9645SThomas Gleixner 	cpu_base->nr_hangs++;
17695cee9645SThomas Gleixner 	cpu_base->hang_detected = 1;
1770dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1771dd934aa8SAnna-Maria Gleixner 
17725cee9645SThomas Gleixner 	delta = ktime_sub(now, entry_time);
17732456e855SThomas Gleixner 	if ((unsigned int)delta > cpu_base->max_hang_time)
17742456e855SThomas Gleixner 		cpu_base->max_hang_time = (unsigned int) delta;
17755cee9645SThomas Gleixner 	/*
17765cee9645SThomas Gleixner 	 * Limit it to a sensible value as we enforce a longer
17775cee9645SThomas Gleixner 	 * delay. Give the CPU at least 100ms to catch up.
17785cee9645SThomas Gleixner 	 */
17792456e855SThomas Gleixner 	if (delta > 100 * NSEC_PER_MSEC)
17805cee9645SThomas Gleixner 		expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
17815cee9645SThomas Gleixner 	else
17825cee9645SThomas Gleixner 		expires_next = ktime_add(now, delta);
17835cee9645SThomas Gleixner 	tick_program_event(expires_next, 1);
17847a6e5537SGeert Uytterhoeven 	pr_warn_once("hrtimer: interrupt took %llu ns\n", ktime_to_ns(delta));
17855cee9645SThomas Gleixner }
17865cee9645SThomas Gleixner 
1787016da201SStephen Boyd /* called with interrupts disabled */
1788c6eb3f70SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void)
17895cee9645SThomas Gleixner {
17905cee9645SThomas Gleixner 	struct tick_device *td;
17915cee9645SThomas Gleixner 
17925cee9645SThomas Gleixner 	if (!hrtimer_hres_active())
17935cee9645SThomas Gleixner 		return;
17945cee9645SThomas Gleixner 
179522127e93SChristoph Lameter 	td = this_cpu_ptr(&tick_cpu_device);
17965cee9645SThomas Gleixner 	if (td && td->evtdev)
17975cee9645SThomas Gleixner 		hrtimer_interrupt(td->evtdev);
17985cee9645SThomas Gleixner }
17995cee9645SThomas Gleixner 
18005cee9645SThomas Gleixner #else /* CONFIG_HIGH_RES_TIMERS */
18015cee9645SThomas Gleixner 
18025cee9645SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void) { }
18035cee9645SThomas Gleixner 
18045cee9645SThomas Gleixner #endif	/* !CONFIG_HIGH_RES_TIMERS */
18055cee9645SThomas Gleixner 
18065cee9645SThomas Gleixner /*
1807c6eb3f70SThomas Gleixner  * Called from run_local_timers in hardirq context every jiffy
18085cee9645SThomas Gleixner  */
18095cee9645SThomas Gleixner void hrtimer_run_queues(void)
18105cee9645SThomas Gleixner {
1811dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1812dd934aa8SAnna-Maria Gleixner 	unsigned long flags;
181321d6d52aSThomas Gleixner 	ktime_t now;
18145cee9645SThomas Gleixner 
1815e19ffe8bSThomas Gleixner 	if (__hrtimer_hres_active(cpu_base))
18165cee9645SThomas Gleixner 		return;
18175cee9645SThomas Gleixner 
1818c6eb3f70SThomas Gleixner 	/*
1819c6eb3f70SThomas Gleixner 	 * This _is_ ugly: We have to check periodically, whether we
1820c6eb3f70SThomas Gleixner 	 * can switch to highres and / or nohz mode. The clocksource
1821c6eb3f70SThomas Gleixner 	 * switch happens with xtime_lock held. Notification from
1822c6eb3f70SThomas Gleixner 	 * there only sets the check bit in the tick_oneshot code,
1823c6eb3f70SThomas Gleixner 	 * otherwise we might deadlock vs. xtime_lock.
1824c6eb3f70SThomas Gleixner 	 */
1825c6eb3f70SThomas Gleixner 	if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
1826c6eb3f70SThomas Gleixner 		hrtimer_switch_to_hres();
1827c6eb3f70SThomas Gleixner 		return;
18285cee9645SThomas Gleixner 	}
18295cee9645SThomas Gleixner 
1830dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
183121d6d52aSThomas Gleixner 	now = hrtimer_update_base(cpu_base);
18325da70160SAnna-Maria Gleixner 
18335da70160SAnna-Maria Gleixner 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
18345da70160SAnna-Maria Gleixner 		cpu_base->softirq_expires_next = KTIME_MAX;
18355da70160SAnna-Maria Gleixner 		cpu_base->softirq_activated = 1;
18365da70160SAnna-Maria Gleixner 		raise_softirq_irqoff(HRTIMER_SOFTIRQ);
18375da70160SAnna-Maria Gleixner 	}
18385da70160SAnna-Maria Gleixner 
1839c458b1d1SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
1840dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
18415cee9645SThomas Gleixner }
18425cee9645SThomas Gleixner 
18435cee9645SThomas Gleixner /*
18445cee9645SThomas Gleixner  * Sleep related functions:
18455cee9645SThomas Gleixner  */
18465cee9645SThomas Gleixner static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
18475cee9645SThomas Gleixner {
18485cee9645SThomas Gleixner 	struct hrtimer_sleeper *t =
18495cee9645SThomas Gleixner 		container_of(timer, struct hrtimer_sleeper, timer);
18505cee9645SThomas Gleixner 	struct task_struct *task = t->task;
18515cee9645SThomas Gleixner 
18525cee9645SThomas Gleixner 	t->task = NULL;
18535cee9645SThomas Gleixner 	if (task)
18545cee9645SThomas Gleixner 		wake_up_process(task);
18555cee9645SThomas Gleixner 
18565cee9645SThomas Gleixner 	return HRTIMER_NORESTART;
18575cee9645SThomas Gleixner }
18585cee9645SThomas Gleixner 
185901656464SThomas Gleixner /**
186001656464SThomas Gleixner  * hrtimer_sleeper_start_expires - Start a hrtimer sleeper timer
186101656464SThomas Gleixner  * @sl:		sleeper to be started
186201656464SThomas Gleixner  * @mode:	timer mode abs/rel
186301656464SThomas Gleixner  *
186401656464SThomas Gleixner  * Wrapper around hrtimer_start_expires() for hrtimer_sleeper based timers
186501656464SThomas Gleixner  * to allow PREEMPT_RT to tweak the delivery mode (soft/hardirq context)
186601656464SThomas Gleixner  */
186701656464SThomas Gleixner void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
186801656464SThomas Gleixner 				   enum hrtimer_mode mode)
186901656464SThomas Gleixner {
18701842f5a4SSebastian Andrzej Siewior 	/*
18711842f5a4SSebastian Andrzej Siewior 	 * Make the enqueue delivery mode check work on RT. If the sleeper
18721842f5a4SSebastian Andrzej Siewior 	 * was initialized for hard interrupt delivery, force the mode bit.
18731842f5a4SSebastian Andrzej Siewior 	 * This is a special case for hrtimer_sleepers because
18741842f5a4SSebastian Andrzej Siewior 	 * hrtimer_init_sleeper() determines the delivery mode on RT so the
18751842f5a4SSebastian Andrzej Siewior 	 * fiddling with this decision is avoided at the call sites.
18761842f5a4SSebastian Andrzej Siewior 	 */
18771842f5a4SSebastian Andrzej Siewior 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && sl->timer.is_hard)
18781842f5a4SSebastian Andrzej Siewior 		mode |= HRTIMER_MODE_HARD;
18791842f5a4SSebastian Andrzej Siewior 
188001656464SThomas Gleixner 	hrtimer_start_expires(&sl->timer, mode);
188101656464SThomas Gleixner }
188201656464SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_sleeper_start_expires);
188301656464SThomas Gleixner 
1884dbc1625fSSebastian Andrzej Siewior static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
1885dbc1625fSSebastian Andrzej Siewior 				   clockid_t clock_id, enum hrtimer_mode mode)
18865cee9645SThomas Gleixner {
18871842f5a4SSebastian Andrzej Siewior 	/*
18884bf07f65SIngo Molnar 	 * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
18891842f5a4SSebastian Andrzej Siewior 	 * marked for hard interrupt expiry mode are moved into soft
18901842f5a4SSebastian Andrzej Siewior 	 * interrupt context either for latency reasons or because the
18911842f5a4SSebastian Andrzej Siewior 	 * hrtimer callback takes regular spinlocks or invokes other
18921842f5a4SSebastian Andrzej Siewior 	 * functions which are not suitable for hard interrupt context on
18931842f5a4SSebastian Andrzej Siewior 	 * PREEMPT_RT.
18941842f5a4SSebastian Andrzej Siewior 	 *
18951842f5a4SSebastian Andrzej Siewior 	 * The hrtimer_sleeper callback is RT compatible in hard interrupt
18961842f5a4SSebastian Andrzej Siewior 	 * context, but there is a latency concern: Untrusted userspace can
18971842f5a4SSebastian Andrzej Siewior 	 * spawn many threads which arm timers for the same expiry time on
18981842f5a4SSebastian Andrzej Siewior 	 * the same CPU. That causes a latency spike due to the wakeup of
18991842f5a4SSebastian Andrzej Siewior 	 * a gazillion threads.
19001842f5a4SSebastian Andrzej Siewior 	 *
19014bf07f65SIngo Molnar 	 * OTOH, privileged real-time user space applications rely on the
19021842f5a4SSebastian Andrzej Siewior 	 * low latency of hard interrupt wakeups. If the current task is in
19031842f5a4SSebastian Andrzej Siewior 	 * a real-time scheduling class, mark the mode for hard interrupt
19041842f5a4SSebastian Andrzej Siewior 	 * expiry.
19051842f5a4SSebastian Andrzej Siewior 	 */
19061842f5a4SSebastian Andrzej Siewior 	if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
19071842f5a4SSebastian Andrzej Siewior 		if (task_is_realtime(current) && !(mode & HRTIMER_MODE_SOFT))
19081842f5a4SSebastian Andrzej Siewior 			mode |= HRTIMER_MODE_HARD;
19091842f5a4SSebastian Andrzej Siewior 	}
19101842f5a4SSebastian Andrzej Siewior 
1911dbc1625fSSebastian Andrzej Siewior 	__hrtimer_init(&sl->timer, clock_id, mode);
19125cee9645SThomas Gleixner 	sl->timer.function = hrtimer_wakeup;
1913b7449487SThomas Gleixner 	sl->task = current;
19145cee9645SThomas Gleixner }
1915dbc1625fSSebastian Andrzej Siewior 
1916dbc1625fSSebastian Andrzej Siewior /**
1917dbc1625fSSebastian Andrzej Siewior  * hrtimer_init_sleeper - initialize sleeper to the given clock
1918dbc1625fSSebastian Andrzej Siewior  * @sl:		sleeper to be initialized
1919dbc1625fSSebastian Andrzej Siewior  * @clock_id:	the clock to be used
1920dbc1625fSSebastian Andrzej Siewior  * @mode:	timer mode abs/rel
1921dbc1625fSSebastian Andrzej Siewior  */
1922dbc1625fSSebastian Andrzej Siewior void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
1923dbc1625fSSebastian Andrzej Siewior 			  enum hrtimer_mode mode)
1924dbc1625fSSebastian Andrzej Siewior {
1925dbc1625fSSebastian Andrzej Siewior 	debug_init(&sl->timer, clock_id, mode);
1926dbc1625fSSebastian Andrzej Siewior 	__hrtimer_init_sleeper(sl, clock_id, mode);
1927dbc1625fSSebastian Andrzej Siewior 
1928dbc1625fSSebastian Andrzej Siewior }
19295cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
19305cee9645SThomas Gleixner 
1931c0edd7c9SDeepa Dinamani int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
1932ce41aaf4SAl Viro {
1933ce41aaf4SAl Viro 	switch(restart->nanosleep.type) {
19340fe27955SArnd Bergmann #ifdef CONFIG_COMPAT_32BIT_TIME
1935ce41aaf4SAl Viro 	case TT_COMPAT:
19369afc5eeeSArnd Bergmann 		if (put_old_timespec32(ts, restart->nanosleep.compat_rmtp))
1937ce41aaf4SAl Viro 			return -EFAULT;
1938ce41aaf4SAl Viro 		break;
1939ce41aaf4SAl Viro #endif
1940ce41aaf4SAl Viro 	case TT_NATIVE:
1941c0edd7c9SDeepa Dinamani 		if (put_timespec64(ts, restart->nanosleep.rmtp))
1942ce41aaf4SAl Viro 			return -EFAULT;
1943ce41aaf4SAl Viro 		break;
1944ce41aaf4SAl Viro 	default:
1945ce41aaf4SAl Viro 		BUG();
1946ce41aaf4SAl Viro 	}
1947ce41aaf4SAl Viro 	return -ERESTART_RESTARTBLOCK;
1948ce41aaf4SAl Viro }
1949ce41aaf4SAl Viro 
19505cee9645SThomas Gleixner static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
19515cee9645SThomas Gleixner {
1952edbeda46SAl Viro 	struct restart_block *restart;
1953edbeda46SAl Viro 
19545cee9645SThomas Gleixner 	do {
19555cee9645SThomas Gleixner 		set_current_state(TASK_INTERRUPTIBLE);
195601656464SThomas Gleixner 		hrtimer_sleeper_start_expires(t, mode);
19575cee9645SThomas Gleixner 
19585cee9645SThomas Gleixner 		if (likely(t->task))
19595cee9645SThomas Gleixner 			freezable_schedule();
19605cee9645SThomas Gleixner 
19615cee9645SThomas Gleixner 		hrtimer_cancel(&t->timer);
19625cee9645SThomas Gleixner 		mode = HRTIMER_MODE_ABS;
19635cee9645SThomas Gleixner 
19645cee9645SThomas Gleixner 	} while (t->task && !signal_pending(current));
19655cee9645SThomas Gleixner 
19665cee9645SThomas Gleixner 	__set_current_state(TASK_RUNNING);
19675cee9645SThomas Gleixner 
1968a7602681SAl Viro 	if (!t->task)
1969a7602681SAl Viro 		return 0;
19705cee9645SThomas Gleixner 
1971edbeda46SAl Viro 	restart = &current->restart_block;
1972edbeda46SAl Viro 	if (restart->nanosleep.type != TT_NONE) {
1973a7602681SAl Viro 		ktime_t rem = hrtimer_expires_remaining(&t->timer);
1974c0edd7c9SDeepa Dinamani 		struct timespec64 rmt;
1975edbeda46SAl Viro 
19762456e855SThomas Gleixner 		if (rem <= 0)
19775cee9645SThomas Gleixner 			return 0;
1978c0edd7c9SDeepa Dinamani 		rmt = ktime_to_timespec64(rem);
19795cee9645SThomas Gleixner 
1980ce41aaf4SAl Viro 		return nanosleep_copyout(restart, &rmt);
1981a7602681SAl Viro 	}
1982a7602681SAl Viro 	return -ERESTART_RESTARTBLOCK;
19835cee9645SThomas Gleixner }
19845cee9645SThomas Gleixner 
1985fb923c4aSAl Viro static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
19865cee9645SThomas Gleixner {
19875cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
1988a7602681SAl Viro 	int ret;
19895cee9645SThomas Gleixner 
1990dbc1625fSSebastian Andrzej Siewior 	hrtimer_init_sleeper_on_stack(&t, restart->nanosleep.clockid,
19915cee9645SThomas Gleixner 				      HRTIMER_MODE_ABS);
19925cee9645SThomas Gleixner 	hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1993a7602681SAl Viro 	ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
19945cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
19955cee9645SThomas Gleixner 	return ret;
19965cee9645SThomas Gleixner }
19975cee9645SThomas Gleixner 
1998ea2d1f7fSAndrei Vagin long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
1999ea2d1f7fSAndrei Vagin 		       const clockid_t clockid)
20005cee9645SThomas Gleixner {
2001a7602681SAl Viro 	struct restart_block *restart;
20025cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
20035cee9645SThomas Gleixner 	int ret = 0;
2004da8b44d5SJohn Stultz 	u64 slack;
20055cee9645SThomas Gleixner 
20065cee9645SThomas Gleixner 	slack = current->timer_slack_ns;
20075cee9645SThomas Gleixner 	if (dl_task(current) || rt_task(current))
20085cee9645SThomas Gleixner 		slack = 0;
20095cee9645SThomas Gleixner 
2010dbc1625fSSebastian Andrzej Siewior 	hrtimer_init_sleeper_on_stack(&t, clockid, mode);
2011ea2d1f7fSAndrei Vagin 	hrtimer_set_expires_range_ns(&t.timer, rqtp, slack);
2012a7602681SAl Viro 	ret = do_nanosleep(&t, mode);
2013a7602681SAl Viro 	if (ret != -ERESTART_RESTARTBLOCK)
20145cee9645SThomas Gleixner 		goto out;
20155cee9645SThomas Gleixner 
20165cee9645SThomas Gleixner 	/* Absolute timers do not update the rmtp value and restart: */
20175cee9645SThomas Gleixner 	if (mode == HRTIMER_MODE_ABS) {
20185cee9645SThomas Gleixner 		ret = -ERESTARTNOHAND;
20195cee9645SThomas Gleixner 		goto out;
20205cee9645SThomas Gleixner 	}
20215cee9645SThomas Gleixner 
2022a7602681SAl Viro 	restart = &current->restart_block;
20235cee9645SThomas Gleixner 	restart->nanosleep.clockid = t.timer.base->clockid;
20245cee9645SThomas Gleixner 	restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
20255abbe51aSOleg Nesterov 	set_restart_fn(restart, hrtimer_nanosleep_restart);
20265cee9645SThomas Gleixner out:
20275cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
20285cee9645SThomas Gleixner 	return ret;
20295cee9645SThomas Gleixner }
20305cee9645SThomas Gleixner 
20313ca47e95SArnd Bergmann #ifdef CONFIG_64BIT
203201909974SDeepa Dinamani 
203301909974SDeepa Dinamani SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp,
203401909974SDeepa Dinamani 		struct __kernel_timespec __user *, rmtp)
20355cee9645SThomas Gleixner {
2036c0edd7c9SDeepa Dinamani 	struct timespec64 tu;
20375cee9645SThomas Gleixner 
2038c0edd7c9SDeepa Dinamani 	if (get_timespec64(&tu, rqtp))
20395cee9645SThomas Gleixner 		return -EFAULT;
20405cee9645SThomas Gleixner 
2041c0edd7c9SDeepa Dinamani 	if (!timespec64_valid(&tu))
20425cee9645SThomas Gleixner 		return -EINVAL;
20435cee9645SThomas Gleixner 
2044edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
2045192a82f9SAl Viro 	current->restart_block.nanosleep.rmtp = rmtp;
2046ea2d1f7fSAndrei Vagin 	return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL,
2047ea2d1f7fSAndrei Vagin 				 CLOCK_MONOTONIC);
20485cee9645SThomas Gleixner }
20495cee9645SThomas Gleixner 
205001909974SDeepa Dinamani #endif
205101909974SDeepa Dinamani 
2052b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
2053edbeda46SAl Viro 
20548dabe724SArnd Bergmann SYSCALL_DEFINE2(nanosleep_time32, struct old_timespec32 __user *, rqtp,
20559afc5eeeSArnd Bergmann 		       struct old_timespec32 __user *, rmtp)
2056edbeda46SAl Viro {
2057c0edd7c9SDeepa Dinamani 	struct timespec64 tu;
2058edbeda46SAl Viro 
20599afc5eeeSArnd Bergmann 	if (get_old_timespec32(&tu, rqtp))
2060edbeda46SAl Viro 		return -EFAULT;
2061edbeda46SAl Viro 
2062c0edd7c9SDeepa Dinamani 	if (!timespec64_valid(&tu))
2063edbeda46SAl Viro 		return -EINVAL;
2064edbeda46SAl Viro 
2065edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
2066edbeda46SAl Viro 	current->restart_block.nanosleep.compat_rmtp = rmtp;
2067ea2d1f7fSAndrei Vagin 	return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL,
2068ea2d1f7fSAndrei Vagin 				 CLOCK_MONOTONIC);
2069edbeda46SAl Viro }
2070edbeda46SAl Viro #endif
2071edbeda46SAl Viro 
20725cee9645SThomas Gleixner /*
20735cee9645SThomas Gleixner  * Functions related to boot-time initialization:
20745cee9645SThomas Gleixner  */
207527590dc1SThomas Gleixner int hrtimers_prepare_cpu(unsigned int cpu)
20765cee9645SThomas Gleixner {
20775cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
20785cee9645SThomas Gleixner 	int i;
20795cee9645SThomas Gleixner 
20805cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
2081af5a06b5SAhmed S. Darwish 		struct hrtimer_clock_base *clock_b = &cpu_base->clock_base[i];
2082af5a06b5SAhmed S. Darwish 
2083af5a06b5SAhmed S. Darwish 		clock_b->cpu_base = cpu_base;
2084af5a06b5SAhmed S. Darwish 		seqcount_raw_spinlock_init(&clock_b->seq, &cpu_base->lock);
2085af5a06b5SAhmed S. Darwish 		timerqueue_init_head(&clock_b->active);
20865cee9645SThomas Gleixner 	}
20875cee9645SThomas Gleixner 
2088cddd0248SViresh Kumar 	cpu_base->cpu = cpu;
2089303c146dSThomas Gleixner 	cpu_base->active_bases = 0;
209028bfd18bSAnna-Maria Gleixner 	cpu_base->hres_active = 0;
2091303c146dSThomas Gleixner 	cpu_base->hang_detected = 0;
2092303c146dSThomas Gleixner 	cpu_base->next_timer = NULL;
2093303c146dSThomas Gleixner 	cpu_base->softirq_next_timer = NULL;
209407a9a7eaSAnna-Maria Gleixner 	cpu_base->expires_next = KTIME_MAX;
20955da70160SAnna-Maria Gleixner 	cpu_base->softirq_expires_next = KTIME_MAX;
2096f61eff83SAnna-Maria Gleixner 	hrtimer_cpu_base_init_expiry_lock(cpu_base);
209727590dc1SThomas Gleixner 	return 0;
20985cee9645SThomas Gleixner }
20995cee9645SThomas Gleixner 
21005cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
21015cee9645SThomas Gleixner 
21025cee9645SThomas Gleixner static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
21035cee9645SThomas Gleixner 				struct hrtimer_clock_base *new_base)
21045cee9645SThomas Gleixner {
21055cee9645SThomas Gleixner 	struct hrtimer *timer;
21065cee9645SThomas Gleixner 	struct timerqueue_node *node;
21075cee9645SThomas Gleixner 
21085cee9645SThomas Gleixner 	while ((node = timerqueue_getnext(&old_base->active))) {
21095cee9645SThomas Gleixner 		timer = container_of(node, struct hrtimer, node);
21105cee9645SThomas Gleixner 		BUG_ON(hrtimer_callback_running(timer));
21115cee9645SThomas Gleixner 		debug_deactivate(timer);
21125cee9645SThomas Gleixner 
21135cee9645SThomas Gleixner 		/*
2114c04dca02SOleg Nesterov 		 * Mark it as ENQUEUED not INACTIVE otherwise the
21155cee9645SThomas Gleixner 		 * timer could be seen as !active and just vanish away
21165cee9645SThomas Gleixner 		 * under us on another CPU
21175cee9645SThomas Gleixner 		 */
2118c04dca02SOleg Nesterov 		__remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
21195cee9645SThomas Gleixner 		timer->base = new_base;
21205cee9645SThomas Gleixner 		/*
21215cee9645SThomas Gleixner 		 * Enqueue the timers on the new cpu. This does not
21225cee9645SThomas Gleixner 		 * reprogram the event device in case the timer
21235cee9645SThomas Gleixner 		 * expires before the earliest on this CPU, but we run
21245cee9645SThomas Gleixner 		 * hrtimer_interrupt after we migrated everything to
21255cee9645SThomas Gleixner 		 * sort out already expired timers and reprogram the
21265cee9645SThomas Gleixner 		 * event device.
21275cee9645SThomas Gleixner 		 */
212863e2ed36SAnna-Maria Gleixner 		enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS);
21295cee9645SThomas Gleixner 	}
21305cee9645SThomas Gleixner }
21315cee9645SThomas Gleixner 
213227590dc1SThomas Gleixner int hrtimers_dead_cpu(unsigned int scpu)
21335cee9645SThomas Gleixner {
21345cee9645SThomas Gleixner 	struct hrtimer_cpu_base *old_base, *new_base;
21355cee9645SThomas Gleixner 	int i;
21365cee9645SThomas Gleixner 
21375cee9645SThomas Gleixner 	BUG_ON(cpu_online(scpu));
21385cee9645SThomas Gleixner 	tick_cancel_sched_timer(scpu);
21395cee9645SThomas Gleixner 
21405da70160SAnna-Maria Gleixner 	/*
21415da70160SAnna-Maria Gleixner 	 * this BH disable ensures that raise_softirq_irqoff() does
21425da70160SAnna-Maria Gleixner 	 * not wakeup ksoftirqd (and acquire the pi-lock) while
21435da70160SAnna-Maria Gleixner 	 * holding the cpu_base lock
21445da70160SAnna-Maria Gleixner 	 */
21455da70160SAnna-Maria Gleixner 	local_bh_disable();
21465cee9645SThomas Gleixner 	local_irq_disable();
21475cee9645SThomas Gleixner 	old_base = &per_cpu(hrtimer_bases, scpu);
2148dc5df73bSChristoph Lameter 	new_base = this_cpu_ptr(&hrtimer_bases);
21495cee9645SThomas Gleixner 	/*
21505cee9645SThomas Gleixner 	 * The caller is globally serialized and nobody else
21515cee9645SThomas Gleixner 	 * takes two locks at once, deadlock is not possible.
21525cee9645SThomas Gleixner 	 */
21535cee9645SThomas Gleixner 	raw_spin_lock(&new_base->lock);
21545cee9645SThomas Gleixner 	raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
21555cee9645SThomas Gleixner 
21565cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
21575cee9645SThomas Gleixner 		migrate_hrtimer_list(&old_base->clock_base[i],
21585cee9645SThomas Gleixner 				     &new_base->clock_base[i]);
21595cee9645SThomas Gleixner 	}
21605cee9645SThomas Gleixner 
21615da70160SAnna-Maria Gleixner 	/*
21625da70160SAnna-Maria Gleixner 	 * The migration might have changed the first expiring softirq
21635da70160SAnna-Maria Gleixner 	 * timer on this CPU. Update it.
21645da70160SAnna-Maria Gleixner 	 */
21655da70160SAnna-Maria Gleixner 	hrtimer_update_softirq_timer(new_base, false);
21665da70160SAnna-Maria Gleixner 
21675cee9645SThomas Gleixner 	raw_spin_unlock(&old_base->lock);
21685cee9645SThomas Gleixner 	raw_spin_unlock(&new_base->lock);
21695cee9645SThomas Gleixner 
21705cee9645SThomas Gleixner 	/* Check, if we got expired work to do */
21715cee9645SThomas Gleixner 	__hrtimer_peek_ahead_timers();
21725cee9645SThomas Gleixner 	local_irq_enable();
21735da70160SAnna-Maria Gleixner 	local_bh_enable();
217427590dc1SThomas Gleixner 	return 0;
21755cee9645SThomas Gleixner }
21765cee9645SThomas Gleixner 
21775cee9645SThomas Gleixner #endif /* CONFIG_HOTPLUG_CPU */
21785cee9645SThomas Gleixner 
21795cee9645SThomas Gleixner void __init hrtimers_init(void)
21805cee9645SThomas Gleixner {
218127590dc1SThomas Gleixner 	hrtimers_prepare_cpu(smp_processor_id());
21825da70160SAnna-Maria Gleixner 	open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
21835cee9645SThomas Gleixner }
21845cee9645SThomas Gleixner 
21855cee9645SThomas Gleixner /**
21865cee9645SThomas Gleixner  * schedule_hrtimeout_range_clock - sleep until timeout
21875cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
21885cee9645SThomas Gleixner  * @delta:	slack in expires timeout (ktime_t)
218990777713SAnna-Maria Gleixner  * @mode:	timer mode
219090777713SAnna-Maria Gleixner  * @clock_id:	timer clock to be used
21915cee9645SThomas Gleixner  */
21925cee9645SThomas Gleixner int __sched
2193da8b44d5SJohn Stultz schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
219490777713SAnna-Maria Gleixner 			       const enum hrtimer_mode mode, clockid_t clock_id)
21955cee9645SThomas Gleixner {
21965cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
21975cee9645SThomas Gleixner 
21985cee9645SThomas Gleixner 	/*
21995cee9645SThomas Gleixner 	 * Optimize when a zero timeout value is given. It does not
22005cee9645SThomas Gleixner 	 * matter whether this is an absolute or a relative time.
22015cee9645SThomas Gleixner 	 */
22022456e855SThomas Gleixner 	if (expires && *expires == 0) {
22035cee9645SThomas Gleixner 		__set_current_state(TASK_RUNNING);
22045cee9645SThomas Gleixner 		return 0;
22055cee9645SThomas Gleixner 	}
22065cee9645SThomas Gleixner 
22075cee9645SThomas Gleixner 	/*
22085cee9645SThomas Gleixner 	 * A NULL parameter means "infinite"
22095cee9645SThomas Gleixner 	 */
22105cee9645SThomas Gleixner 	if (!expires) {
22115cee9645SThomas Gleixner 		schedule();
22125cee9645SThomas Gleixner 		return -EINTR;
22135cee9645SThomas Gleixner 	}
22145cee9645SThomas Gleixner 
2215dbc1625fSSebastian Andrzej Siewior 	hrtimer_init_sleeper_on_stack(&t, clock_id, mode);
22165cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
221701656464SThomas Gleixner 	hrtimer_sleeper_start_expires(&t, mode);
22185cee9645SThomas Gleixner 
22195cee9645SThomas Gleixner 	if (likely(t.task))
22205cee9645SThomas Gleixner 		schedule();
22215cee9645SThomas Gleixner 
22225cee9645SThomas Gleixner 	hrtimer_cancel(&t.timer);
22235cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
22245cee9645SThomas Gleixner 
22255cee9645SThomas Gleixner 	__set_current_state(TASK_RUNNING);
22265cee9645SThomas Gleixner 
22275cee9645SThomas Gleixner 	return !t.task ? 0 : -EINTR;
22285cee9645SThomas Gleixner }
22295cee9645SThomas Gleixner 
22305cee9645SThomas Gleixner /**
22315cee9645SThomas Gleixner  * schedule_hrtimeout_range - sleep until timeout
22325cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
22335cee9645SThomas Gleixner  * @delta:	slack in expires timeout (ktime_t)
223490777713SAnna-Maria Gleixner  * @mode:	timer mode
22355cee9645SThomas Gleixner  *
22365cee9645SThomas Gleixner  * Make the current task sleep until the given expiry time has
22375cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
22385cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
22395cee9645SThomas Gleixner  *
22405cee9645SThomas Gleixner  * The @delta argument gives the kernel the freedom to schedule the
22415cee9645SThomas Gleixner  * actual wakeup to a time that is both power and performance friendly.
22425cee9645SThomas Gleixner  * The kernel give the normal best effort behavior for "@expires+@delta",
22435cee9645SThomas Gleixner  * but may decide to fire the timer earlier, but no earlier than @expires.
22445cee9645SThomas Gleixner  *
22455cee9645SThomas Gleixner  * You can set the task state as follows -
22465cee9645SThomas Gleixner  *
22475cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
22484b7e9cf9SDouglas Anderson  * pass before the routine returns unless the current task is explicitly
22494b7e9cf9SDouglas Anderson  * woken up, (e.g. by wake_up_process()).
22505cee9645SThomas Gleixner  *
22515cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
22524b7e9cf9SDouglas Anderson  * delivered to the current task or the current task is explicitly woken
22534b7e9cf9SDouglas Anderson  * up.
22545cee9645SThomas Gleixner  *
22555cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
22565cee9645SThomas Gleixner  * routine returns.
22575cee9645SThomas Gleixner  *
22584b7e9cf9SDouglas Anderson  * Returns 0 when the timer has expired. If the task was woken before the
22594b7e9cf9SDouglas Anderson  * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
22604b7e9cf9SDouglas Anderson  * by an explicit wakeup, it returns -EINTR.
22615cee9645SThomas Gleixner  */
2262da8b44d5SJohn Stultz int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
22635cee9645SThomas Gleixner 				     const enum hrtimer_mode mode)
22645cee9645SThomas Gleixner {
22655cee9645SThomas Gleixner 	return schedule_hrtimeout_range_clock(expires, delta, mode,
22665cee9645SThomas Gleixner 					      CLOCK_MONOTONIC);
22675cee9645SThomas Gleixner }
22685cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
22695cee9645SThomas Gleixner 
22705cee9645SThomas Gleixner /**
22715cee9645SThomas Gleixner  * schedule_hrtimeout - sleep until timeout
22725cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
227390777713SAnna-Maria Gleixner  * @mode:	timer mode
22745cee9645SThomas Gleixner  *
22755cee9645SThomas Gleixner  * Make the current task sleep until the given expiry time has
22765cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
22775cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
22785cee9645SThomas Gleixner  *
22795cee9645SThomas Gleixner  * You can set the task state as follows -
22805cee9645SThomas Gleixner  *
22815cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
22824b7e9cf9SDouglas Anderson  * pass before the routine returns unless the current task is explicitly
22834b7e9cf9SDouglas Anderson  * woken up, (e.g. by wake_up_process()).
22845cee9645SThomas Gleixner  *
22855cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
22864b7e9cf9SDouglas Anderson  * delivered to the current task or the current task is explicitly woken
22874b7e9cf9SDouglas Anderson  * up.
22885cee9645SThomas Gleixner  *
22895cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
22905cee9645SThomas Gleixner  * routine returns.
22915cee9645SThomas Gleixner  *
22924b7e9cf9SDouglas Anderson  * Returns 0 when the timer has expired. If the task was woken before the
22934b7e9cf9SDouglas Anderson  * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
22944b7e9cf9SDouglas Anderson  * by an explicit wakeup, it returns -EINTR.
22955cee9645SThomas Gleixner  */
22965cee9645SThomas Gleixner int __sched schedule_hrtimeout(ktime_t *expires,
22975cee9645SThomas Gleixner 			       const enum hrtimer_mode mode)
22985cee9645SThomas Gleixner {
22995cee9645SThomas Gleixner 	return schedule_hrtimeout_range(expires, 0, mode);
23005cee9645SThomas Gleixner }
23015cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout);
2302