xref: /openbmc/linux/kernel/time/hrtimer.c (revision 35728b8209ee7d25b6241a56304ee926469bd154)
1*35728b82SThomas 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  *  For licencing details see kernel-base/COPYING
255cee9645SThomas Gleixner  */
265cee9645SThomas Gleixner 
275cee9645SThomas Gleixner #include <linux/cpu.h>
285cee9645SThomas Gleixner #include <linux/export.h>
295cee9645SThomas Gleixner #include <linux/percpu.h>
305cee9645SThomas Gleixner #include <linux/hrtimer.h>
315cee9645SThomas Gleixner #include <linux/notifier.h>
325cee9645SThomas Gleixner #include <linux/syscalls.h>
335cee9645SThomas Gleixner #include <linux/interrupt.h>
345cee9645SThomas Gleixner #include <linux/tick.h>
355cee9645SThomas Gleixner #include <linux/seq_file.h>
365cee9645SThomas Gleixner #include <linux/err.h>
375cee9645SThomas Gleixner #include <linux/debugobjects.h>
38174cd4b1SIngo Molnar #include <linux/sched/signal.h>
395cee9645SThomas Gleixner #include <linux/sched/sysctl.h>
405cee9645SThomas Gleixner #include <linux/sched/rt.h>
415cee9645SThomas Gleixner #include <linux/sched/deadline.h>
42370c9135SIngo Molnar #include <linux/sched/nohz.h>
43b17b0153SIngo Molnar #include <linux/sched/debug.h>
445cee9645SThomas Gleixner #include <linux/timer.h>
455cee9645SThomas Gleixner #include <linux/freezer.h>
46edbeda46SAl Viro #include <linux/compat.h>
475cee9645SThomas Gleixner 
487c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
495cee9645SThomas Gleixner 
505cee9645SThomas Gleixner #include <trace/events/timer.h>
515cee9645SThomas Gleixner 
52c1797bafSThomas Gleixner #include "tick-internal.h"
538b094cd0SThomas Gleixner 
545cee9645SThomas Gleixner /*
55c458b1d1SAnna-Maria Gleixner  * Masks for selecting the soft and hard context timers from
56c458b1d1SAnna-Maria Gleixner  * cpu_base->active
57c458b1d1SAnna-Maria Gleixner  */
58c458b1d1SAnna-Maria Gleixner #define MASK_SHIFT		(HRTIMER_BASE_MONOTONIC_SOFT)
59c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_HARD	((1U << MASK_SHIFT) - 1)
60c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_SOFT	(HRTIMER_ACTIVE_HARD << MASK_SHIFT)
61c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_ALL	(HRTIMER_ACTIVE_SOFT | HRTIMER_ACTIVE_HARD)
62c458b1d1SAnna-Maria Gleixner 
63c458b1d1SAnna-Maria Gleixner /*
645cee9645SThomas Gleixner  * The timer bases:
655cee9645SThomas Gleixner  *
66571af55aSZhen Lei  * There are more clockids than hrtimer bases. Thus, we index
675cee9645SThomas Gleixner  * into the timer bases by the hrtimer_base_type enum. When trying
685cee9645SThomas Gleixner  * to reach a base using a clockid, hrtimer_clockid_to_base()
695cee9645SThomas Gleixner  * is used to convert from clockid to the proper hrtimer_base_type.
705cee9645SThomas Gleixner  */
715cee9645SThomas Gleixner DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
725cee9645SThomas Gleixner {
735cee9645SThomas Gleixner 	.lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
745cee9645SThomas Gleixner 	.clock_base =
755cee9645SThomas Gleixner 	{
765cee9645SThomas Gleixner 		{
775cee9645SThomas Gleixner 			.index = HRTIMER_BASE_MONOTONIC,
785cee9645SThomas Gleixner 			.clockid = CLOCK_MONOTONIC,
795cee9645SThomas Gleixner 			.get_time = &ktime_get,
805cee9645SThomas Gleixner 		},
815cee9645SThomas Gleixner 		{
825cee9645SThomas Gleixner 			.index = HRTIMER_BASE_REALTIME,
835cee9645SThomas Gleixner 			.clockid = CLOCK_REALTIME,
845cee9645SThomas Gleixner 			.get_time = &ktime_get_real,
855cee9645SThomas Gleixner 		},
865cee9645SThomas Gleixner 		{
87a3ed0e43SThomas Gleixner 			.index = HRTIMER_BASE_BOOTTIME,
88a3ed0e43SThomas Gleixner 			.clockid = CLOCK_BOOTTIME,
89a3ed0e43SThomas Gleixner 			.get_time = &ktime_get_boottime,
90a3ed0e43SThomas Gleixner 		},
91a3ed0e43SThomas Gleixner 		{
925cee9645SThomas Gleixner 			.index = HRTIMER_BASE_TAI,
935cee9645SThomas Gleixner 			.clockid = CLOCK_TAI,
945cee9645SThomas Gleixner 			.get_time = &ktime_get_clocktai,
955cee9645SThomas Gleixner 		},
9698ecadd4SAnna-Maria Gleixner 		{
9798ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_MONOTONIC_SOFT,
9898ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_MONOTONIC,
9998ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get,
10098ecadd4SAnna-Maria Gleixner 		},
10198ecadd4SAnna-Maria Gleixner 		{
10298ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_REALTIME_SOFT,
10398ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_REALTIME,
10498ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get_real,
10598ecadd4SAnna-Maria Gleixner 		},
10698ecadd4SAnna-Maria Gleixner 		{
107a3ed0e43SThomas Gleixner 			.index = HRTIMER_BASE_BOOTTIME_SOFT,
108a3ed0e43SThomas Gleixner 			.clockid = CLOCK_BOOTTIME,
109a3ed0e43SThomas Gleixner 			.get_time = &ktime_get_boottime,
110a3ed0e43SThomas Gleixner 		},
111a3ed0e43SThomas Gleixner 		{
11298ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_TAI_SOFT,
11398ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_TAI,
11498ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get_clocktai,
11598ecadd4SAnna-Maria Gleixner 		},
1165cee9645SThomas Gleixner 	}
1175cee9645SThomas Gleixner };
1185cee9645SThomas Gleixner 
1195cee9645SThomas Gleixner static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
120336a9cdeSMarc Zyngier 	/* Make sure we catch unsupported clockids */
121336a9cdeSMarc Zyngier 	[0 ... MAX_CLOCKS - 1]	= HRTIMER_MAX_CLOCK_BASES,
122336a9cdeSMarc Zyngier 
1235cee9645SThomas Gleixner 	[CLOCK_REALTIME]	= HRTIMER_BASE_REALTIME,
1245cee9645SThomas Gleixner 	[CLOCK_MONOTONIC]	= HRTIMER_BASE_MONOTONIC,
125a3ed0e43SThomas Gleixner 	[CLOCK_BOOTTIME]	= HRTIMER_BASE_BOOTTIME,
1265cee9645SThomas Gleixner 	[CLOCK_TAI]		= HRTIMER_BASE_TAI,
1275cee9645SThomas Gleixner };
1285cee9645SThomas Gleixner 
1295cee9645SThomas Gleixner /*
1305cee9645SThomas Gleixner  * Functions and macros which are different for UP/SMP systems are kept in a
1315cee9645SThomas Gleixner  * single place
1325cee9645SThomas Gleixner  */
1335cee9645SThomas Gleixner #ifdef CONFIG_SMP
1345cee9645SThomas Gleixner 
1355cee9645SThomas Gleixner /*
136887d9dc9SPeter Zijlstra  * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
137887d9dc9SPeter Zijlstra  * such that hrtimer_callback_running() can unconditionally dereference
138887d9dc9SPeter Zijlstra  * timer->base->cpu_base
139887d9dc9SPeter Zijlstra  */
140887d9dc9SPeter Zijlstra static struct hrtimer_cpu_base migration_cpu_base = {
141887d9dc9SPeter Zijlstra 	.clock_base = { { .cpu_base = &migration_cpu_base, }, },
142887d9dc9SPeter Zijlstra };
143887d9dc9SPeter Zijlstra 
144887d9dc9SPeter Zijlstra #define migration_base	migration_cpu_base.clock_base[0]
145887d9dc9SPeter Zijlstra 
146887d9dc9SPeter Zijlstra /*
1475cee9645SThomas Gleixner  * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
1485cee9645SThomas Gleixner  * means that all timers which are tied to this base via timer->base are
1495cee9645SThomas Gleixner  * locked, and the base itself is locked too.
1505cee9645SThomas Gleixner  *
1515cee9645SThomas Gleixner  * So __run_timers/migrate_timers can safely modify all timers which could
1525cee9645SThomas Gleixner  * be found on the lists/queues.
1535cee9645SThomas Gleixner  *
1545cee9645SThomas Gleixner  * When the timer's base is locked, and the timer removed from list, it is
155887d9dc9SPeter Zijlstra  * possible to set timer->base = &migration_base and drop the lock: the timer
156887d9dc9SPeter Zijlstra  * remains locked.
1575cee9645SThomas Gleixner  */
1585cee9645SThomas Gleixner static
1595cee9645SThomas Gleixner struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
1605cee9645SThomas Gleixner 					     unsigned long *flags)
1615cee9645SThomas Gleixner {
1625cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
1635cee9645SThomas Gleixner 
1645cee9645SThomas Gleixner 	for (;;) {
1655cee9645SThomas Gleixner 		base = timer->base;
166887d9dc9SPeter Zijlstra 		if (likely(base != &migration_base)) {
1675cee9645SThomas Gleixner 			raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
1685cee9645SThomas Gleixner 			if (likely(base == timer->base))
1695cee9645SThomas Gleixner 				return base;
1705cee9645SThomas Gleixner 			/* The timer has migrated to another CPU: */
1715cee9645SThomas Gleixner 			raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
1725cee9645SThomas Gleixner 		}
1735cee9645SThomas Gleixner 		cpu_relax();
1745cee9645SThomas Gleixner 	}
1755cee9645SThomas Gleixner }
1765cee9645SThomas Gleixner 
1775cee9645SThomas Gleixner /*
17807a9a7eaSAnna-Maria Gleixner  * We do not migrate the timer when it is expiring before the next
17907a9a7eaSAnna-Maria Gleixner  * event on the target cpu. When high resolution is enabled, we cannot
18007a9a7eaSAnna-Maria Gleixner  * reprogram the target cpu hardware and we would cause it to fire
18107a9a7eaSAnna-Maria Gleixner  * late. To keep it simple, we handle the high resolution enabled and
18207a9a7eaSAnna-Maria Gleixner  * disabled case similar.
1835cee9645SThomas Gleixner  *
1845cee9645SThomas Gleixner  * Called with cpu_base->lock of target cpu held.
1855cee9645SThomas Gleixner  */
1865cee9645SThomas Gleixner static int
1875cee9645SThomas Gleixner hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
1885cee9645SThomas Gleixner {
1895cee9645SThomas Gleixner 	ktime_t expires;
1905cee9645SThomas Gleixner 
1915cee9645SThomas Gleixner 	expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
1922ac2dcccSAnna-Maria Gleixner 	return expires < new_base->cpu_base->expires_next;
1935cee9645SThomas Gleixner }
1945cee9645SThomas Gleixner 
195bc7a34b8SThomas Gleixner static inline
196bc7a34b8SThomas Gleixner struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
197bc7a34b8SThomas Gleixner 					 int pinned)
198bc7a34b8SThomas Gleixner {
199ae67badaSThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
200ae67badaSThomas Gleixner 	if (static_branch_likely(&timers_migration_enabled) && !pinned)
201bc7a34b8SThomas Gleixner 		return &per_cpu(hrtimer_bases, get_nohz_timer_target());
202ae67badaSThomas Gleixner #endif
203662b3e19SFrederic Weisbecker 	return base;
204bc7a34b8SThomas Gleixner }
205bc7a34b8SThomas Gleixner 
2065cee9645SThomas Gleixner /*
207b48362d8SFrederic Weisbecker  * We switch the timer base to a power-optimized selected CPU target,
208b48362d8SFrederic Weisbecker  * if:
209b48362d8SFrederic Weisbecker  *	- NO_HZ_COMMON is enabled
210b48362d8SFrederic Weisbecker  *	- timer migration is enabled
211b48362d8SFrederic Weisbecker  *	- the timer callback is not running
212b48362d8SFrederic Weisbecker  *	- the timer is not the first expiring timer on the new target
213b48362d8SFrederic Weisbecker  *
214b48362d8SFrederic Weisbecker  * If one of the above requirements is not fulfilled we move the timer
215b48362d8SFrederic Weisbecker  * to the current CPU or leave it on the previously assigned CPU if
216b48362d8SFrederic Weisbecker  * the timer callback is currently running.
2175cee9645SThomas Gleixner  */
2185cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
2195cee9645SThomas Gleixner switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
2205cee9645SThomas Gleixner 		    int pinned)
2215cee9645SThomas Gleixner {
222b48362d8SFrederic Weisbecker 	struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
2235cee9645SThomas Gleixner 	struct hrtimer_clock_base *new_base;
2245cee9645SThomas Gleixner 	int basenum = base->index;
2255cee9645SThomas Gleixner 
226b48362d8SFrederic Weisbecker 	this_cpu_base = this_cpu_ptr(&hrtimer_bases);
227b48362d8SFrederic Weisbecker 	new_cpu_base = get_target_base(this_cpu_base, pinned);
2285cee9645SThomas Gleixner again:
2295cee9645SThomas Gleixner 	new_base = &new_cpu_base->clock_base[basenum];
2305cee9645SThomas Gleixner 
2315cee9645SThomas Gleixner 	if (base != new_base) {
2325cee9645SThomas Gleixner 		/*
2335cee9645SThomas Gleixner 		 * We are trying to move timer to new_base.
2345cee9645SThomas Gleixner 		 * However we can't change timer's base while it is running,
2355cee9645SThomas Gleixner 		 * so we keep it on the same CPU. No hassle vs. reprogramming
2365cee9645SThomas Gleixner 		 * the event source in the high resolution case. The softirq
2375cee9645SThomas Gleixner 		 * code will take care of this when the timer function has
2385cee9645SThomas Gleixner 		 * completed. There is no conflict as we hold the lock until
2395cee9645SThomas Gleixner 		 * the timer is enqueued.
2405cee9645SThomas Gleixner 		 */
2415cee9645SThomas Gleixner 		if (unlikely(hrtimer_callback_running(timer)))
2425cee9645SThomas Gleixner 			return base;
2435cee9645SThomas Gleixner 
244887d9dc9SPeter Zijlstra 		/* See the comment in lock_hrtimer_base() */
245887d9dc9SPeter Zijlstra 		timer->base = &migration_base;
2465cee9645SThomas Gleixner 		raw_spin_unlock(&base->cpu_base->lock);
2475cee9645SThomas Gleixner 		raw_spin_lock(&new_base->cpu_base->lock);
2485cee9645SThomas Gleixner 
249b48362d8SFrederic Weisbecker 		if (new_cpu_base != this_cpu_base &&
250bc7a34b8SThomas Gleixner 		    hrtimer_check_target(timer, new_base)) {
2515cee9645SThomas Gleixner 			raw_spin_unlock(&new_base->cpu_base->lock);
2525cee9645SThomas Gleixner 			raw_spin_lock(&base->cpu_base->lock);
253b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
2545cee9645SThomas Gleixner 			timer->base = base;
2555cee9645SThomas Gleixner 			goto again;
2565cee9645SThomas Gleixner 		}
2575cee9645SThomas Gleixner 		timer->base = new_base;
2585cee9645SThomas Gleixner 	} else {
259b48362d8SFrederic Weisbecker 		if (new_cpu_base != this_cpu_base &&
260bc7a34b8SThomas Gleixner 		    hrtimer_check_target(timer, new_base)) {
261b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
2625cee9645SThomas Gleixner 			goto again;
2635cee9645SThomas Gleixner 		}
2645cee9645SThomas Gleixner 	}
2655cee9645SThomas Gleixner 	return new_base;
2665cee9645SThomas Gleixner }
2675cee9645SThomas Gleixner 
2685cee9645SThomas Gleixner #else /* CONFIG_SMP */
2695cee9645SThomas Gleixner 
2705cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
2715cee9645SThomas Gleixner lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
2725cee9645SThomas Gleixner {
2735cee9645SThomas Gleixner 	struct hrtimer_clock_base *base = timer->base;
2745cee9645SThomas Gleixner 
2755cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
2765cee9645SThomas Gleixner 
2775cee9645SThomas Gleixner 	return base;
2785cee9645SThomas Gleixner }
2795cee9645SThomas Gleixner 
2805cee9645SThomas Gleixner # define switch_hrtimer_base(t, b, p)	(b)
2815cee9645SThomas Gleixner 
2825cee9645SThomas Gleixner #endif	/* !CONFIG_SMP */
2835cee9645SThomas Gleixner 
2845cee9645SThomas Gleixner /*
2855cee9645SThomas Gleixner  * Functions for the union type storage format of ktime_t which are
2865cee9645SThomas Gleixner  * too large for inlining:
2875cee9645SThomas Gleixner  */
2885cee9645SThomas Gleixner #if BITS_PER_LONG < 64
2895cee9645SThomas Gleixner /*
2905cee9645SThomas Gleixner  * Divide a ktime value by a nanosecond value
2915cee9645SThomas Gleixner  */
292f7bcb70eSJohn Stultz s64 __ktime_divns(const ktime_t kt, s64 div)
2935cee9645SThomas Gleixner {
2945cee9645SThomas Gleixner 	int sft = 0;
295f7bcb70eSJohn Stultz 	s64 dclc;
296f7bcb70eSJohn Stultz 	u64 tmp;
2975cee9645SThomas Gleixner 
2985cee9645SThomas Gleixner 	dclc = ktime_to_ns(kt);
299f7bcb70eSJohn Stultz 	tmp = dclc < 0 ? -dclc : dclc;
300f7bcb70eSJohn Stultz 
3015cee9645SThomas Gleixner 	/* Make sure the divisor is less than 2^32: */
3025cee9645SThomas Gleixner 	while (div >> 32) {
3035cee9645SThomas Gleixner 		sft++;
3045cee9645SThomas Gleixner 		div >>= 1;
3055cee9645SThomas Gleixner 	}
306f7bcb70eSJohn Stultz 	tmp >>= sft;
307f7bcb70eSJohn Stultz 	do_div(tmp, (unsigned long) div);
308f7bcb70eSJohn Stultz 	return dclc < 0 ? -tmp : tmp;
3095cee9645SThomas Gleixner }
3108b618628SNicolas Pitre EXPORT_SYMBOL_GPL(__ktime_divns);
3115cee9645SThomas Gleixner #endif /* BITS_PER_LONG >= 64 */
3125cee9645SThomas Gleixner 
3135cee9645SThomas Gleixner /*
3145cee9645SThomas Gleixner  * Add two ktime values and do a safety check for overflow:
3155cee9645SThomas Gleixner  */
3165cee9645SThomas Gleixner ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
3175cee9645SThomas Gleixner {
318979515c5SVegard Nossum 	ktime_t res = ktime_add_unsafe(lhs, rhs);
3195cee9645SThomas Gleixner 
3205cee9645SThomas Gleixner 	/*
3215cee9645SThomas Gleixner 	 * We use KTIME_SEC_MAX here, the maximum timeout which we can
3225cee9645SThomas Gleixner 	 * return to user space in a timespec:
3235cee9645SThomas Gleixner 	 */
3242456e855SThomas Gleixner 	if (res < 0 || res < lhs || res < rhs)
3255cee9645SThomas Gleixner 		res = ktime_set(KTIME_SEC_MAX, 0);
3265cee9645SThomas Gleixner 
3275cee9645SThomas Gleixner 	return res;
3285cee9645SThomas Gleixner }
3295cee9645SThomas Gleixner 
3305cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_add_safe);
3315cee9645SThomas Gleixner 
3325cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
3335cee9645SThomas Gleixner 
3345cee9645SThomas Gleixner static struct debug_obj_descr hrtimer_debug_descr;
3355cee9645SThomas Gleixner 
3365cee9645SThomas Gleixner static void *hrtimer_debug_hint(void *addr)
3375cee9645SThomas Gleixner {
3385cee9645SThomas Gleixner 	return ((struct hrtimer *) addr)->function;
3395cee9645SThomas Gleixner }
3405cee9645SThomas Gleixner 
3415cee9645SThomas Gleixner /*
3425cee9645SThomas Gleixner  * fixup_init is called when:
3435cee9645SThomas Gleixner  * - an active object is initialized
3445cee9645SThomas Gleixner  */
345e3252464SDu, Changbin static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
3465cee9645SThomas Gleixner {
3475cee9645SThomas Gleixner 	struct hrtimer *timer = addr;
3485cee9645SThomas Gleixner 
3495cee9645SThomas Gleixner 	switch (state) {
3505cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3515cee9645SThomas Gleixner 		hrtimer_cancel(timer);
3525cee9645SThomas Gleixner 		debug_object_init(timer, &hrtimer_debug_descr);
353e3252464SDu, Changbin 		return true;
3545cee9645SThomas Gleixner 	default:
355e3252464SDu, Changbin 		return false;
3565cee9645SThomas Gleixner 	}
3575cee9645SThomas Gleixner }
3585cee9645SThomas Gleixner 
3595cee9645SThomas Gleixner /*
3605cee9645SThomas Gleixner  * fixup_activate is called when:
3615cee9645SThomas Gleixner  * - an active object is activated
362b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
3635cee9645SThomas Gleixner  */
364e3252464SDu, Changbin static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
3655cee9645SThomas Gleixner {
3665cee9645SThomas Gleixner 	switch (state) {
3675cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3685cee9645SThomas Gleixner 		WARN_ON(1);
3695cee9645SThomas Gleixner 
3705cee9645SThomas Gleixner 	default:
371e3252464SDu, Changbin 		return false;
3725cee9645SThomas Gleixner 	}
3735cee9645SThomas Gleixner }
3745cee9645SThomas Gleixner 
3755cee9645SThomas Gleixner /*
3765cee9645SThomas Gleixner  * fixup_free is called when:
3775cee9645SThomas Gleixner  * - an active object is freed
3785cee9645SThomas Gleixner  */
379e3252464SDu, Changbin static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
3805cee9645SThomas Gleixner {
3815cee9645SThomas Gleixner 	struct hrtimer *timer = addr;
3825cee9645SThomas Gleixner 
3835cee9645SThomas Gleixner 	switch (state) {
3845cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3855cee9645SThomas Gleixner 		hrtimer_cancel(timer);
3865cee9645SThomas Gleixner 		debug_object_free(timer, &hrtimer_debug_descr);
387e3252464SDu, Changbin 		return true;
3885cee9645SThomas Gleixner 	default:
389e3252464SDu, Changbin 		return false;
3905cee9645SThomas Gleixner 	}
3915cee9645SThomas Gleixner }
3925cee9645SThomas Gleixner 
3935cee9645SThomas Gleixner static struct debug_obj_descr hrtimer_debug_descr = {
3945cee9645SThomas Gleixner 	.name		= "hrtimer",
3955cee9645SThomas Gleixner 	.debug_hint	= hrtimer_debug_hint,
3965cee9645SThomas Gleixner 	.fixup_init	= hrtimer_fixup_init,
3975cee9645SThomas Gleixner 	.fixup_activate	= hrtimer_fixup_activate,
3985cee9645SThomas Gleixner 	.fixup_free	= hrtimer_fixup_free,
3995cee9645SThomas Gleixner };
4005cee9645SThomas Gleixner 
4015cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer)
4025cee9645SThomas Gleixner {
4035cee9645SThomas Gleixner 	debug_object_init(timer, &hrtimer_debug_descr);
4045cee9645SThomas Gleixner }
4055cee9645SThomas Gleixner 
4065da70160SAnna-Maria Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer,
4075da70160SAnna-Maria Gleixner 					  enum hrtimer_mode mode)
4085cee9645SThomas Gleixner {
4095cee9645SThomas Gleixner 	debug_object_activate(timer, &hrtimer_debug_descr);
4105cee9645SThomas Gleixner }
4115cee9645SThomas Gleixner 
4125cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
4135cee9645SThomas Gleixner {
4145cee9645SThomas Gleixner 	debug_object_deactivate(timer, &hrtimer_debug_descr);
4155cee9645SThomas Gleixner }
4165cee9645SThomas Gleixner 
4175cee9645SThomas Gleixner static inline void debug_hrtimer_free(struct hrtimer *timer)
4185cee9645SThomas Gleixner {
4195cee9645SThomas Gleixner 	debug_object_free(timer, &hrtimer_debug_descr);
4205cee9645SThomas Gleixner }
4215cee9645SThomas Gleixner 
4225cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
4235cee9645SThomas Gleixner 			   enum hrtimer_mode mode);
4245cee9645SThomas Gleixner 
4255cee9645SThomas Gleixner void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
4265cee9645SThomas Gleixner 			   enum hrtimer_mode mode)
4275cee9645SThomas Gleixner {
4285cee9645SThomas Gleixner 	debug_object_init_on_stack(timer, &hrtimer_debug_descr);
4295cee9645SThomas Gleixner 	__hrtimer_init(timer, clock_id, mode);
4305cee9645SThomas Gleixner }
4315cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
4325cee9645SThomas Gleixner 
4335cee9645SThomas Gleixner void destroy_hrtimer_on_stack(struct hrtimer *timer)
4345cee9645SThomas Gleixner {
4355cee9645SThomas Gleixner 	debug_object_free(timer, &hrtimer_debug_descr);
4365cee9645SThomas Gleixner }
437c08376acSGuenter Roeck EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
4385cee9645SThomas Gleixner 
4395cee9645SThomas Gleixner #else
4405da70160SAnna-Maria Gleixner 
4415cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer) { }
4425da70160SAnna-Maria Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer,
4435da70160SAnna-Maria Gleixner 					  enum hrtimer_mode mode) { }
4445cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
4455cee9645SThomas Gleixner #endif
4465cee9645SThomas Gleixner 
4475cee9645SThomas Gleixner static inline void
4485cee9645SThomas Gleixner debug_init(struct hrtimer *timer, clockid_t clockid,
4495cee9645SThomas Gleixner 	   enum hrtimer_mode mode)
4505cee9645SThomas Gleixner {
4515cee9645SThomas Gleixner 	debug_hrtimer_init(timer);
4525cee9645SThomas Gleixner 	trace_hrtimer_init(timer, clockid, mode);
4535cee9645SThomas Gleixner }
4545cee9645SThomas Gleixner 
45563e2ed36SAnna-Maria Gleixner static inline void debug_activate(struct hrtimer *timer,
45663e2ed36SAnna-Maria Gleixner 				  enum hrtimer_mode mode)
4575cee9645SThomas Gleixner {
4585da70160SAnna-Maria Gleixner 	debug_hrtimer_activate(timer, mode);
45963e2ed36SAnna-Maria Gleixner 	trace_hrtimer_start(timer, mode);
4605cee9645SThomas Gleixner }
4615cee9645SThomas Gleixner 
4625cee9645SThomas Gleixner static inline void debug_deactivate(struct hrtimer *timer)
4635cee9645SThomas Gleixner {
4645cee9645SThomas Gleixner 	debug_hrtimer_deactivate(timer);
4655cee9645SThomas Gleixner 	trace_hrtimer_cancel(timer);
4665cee9645SThomas Gleixner }
4675cee9645SThomas Gleixner 
468c272ca58SAnna-Maria Gleixner static struct hrtimer_clock_base *
469c272ca58SAnna-Maria Gleixner __next_base(struct hrtimer_cpu_base *cpu_base, unsigned int *active)
470c272ca58SAnna-Maria Gleixner {
471c272ca58SAnna-Maria Gleixner 	unsigned int idx;
472c272ca58SAnna-Maria Gleixner 
473c272ca58SAnna-Maria Gleixner 	if (!*active)
474c272ca58SAnna-Maria Gleixner 		return NULL;
475c272ca58SAnna-Maria Gleixner 
476c272ca58SAnna-Maria Gleixner 	idx = __ffs(*active);
477c272ca58SAnna-Maria Gleixner 	*active &= ~(1U << idx);
478c272ca58SAnna-Maria Gleixner 
479c272ca58SAnna-Maria Gleixner 	return &cpu_base->clock_base[idx];
480c272ca58SAnna-Maria Gleixner }
481c272ca58SAnna-Maria Gleixner 
482c272ca58SAnna-Maria Gleixner #define for_each_active_base(base, cpu_base, active)	\
483c272ca58SAnna-Maria Gleixner 	while ((base = __next_base((cpu_base), &(active))))
484c272ca58SAnna-Maria Gleixner 
485ad38f596SAnna-Maria Gleixner static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base,
486a59855cdSRafael J. Wysocki 					 const struct hrtimer *exclude,
487ad38f596SAnna-Maria Gleixner 					 unsigned int active,
488ad38f596SAnna-Maria Gleixner 					 ktime_t expires_next)
4899bc74919SThomas Gleixner {
490c272ca58SAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
491ad38f596SAnna-Maria Gleixner 	ktime_t expires;
4929bc74919SThomas Gleixner 
493c272ca58SAnna-Maria Gleixner 	for_each_active_base(base, cpu_base, active) {
4949bc74919SThomas Gleixner 		struct timerqueue_node *next;
4959bc74919SThomas Gleixner 		struct hrtimer *timer;
4969bc74919SThomas Gleixner 
49734aee88aSThomas Gleixner 		next = timerqueue_getnext(&base->active);
4989bc74919SThomas Gleixner 		timer = container_of(next, struct hrtimer, node);
499a59855cdSRafael J. Wysocki 		if (timer == exclude) {
500a59855cdSRafael J. Wysocki 			/* Get to the next timer in the queue. */
5017d2f6abbSRafael J. Wysocki 			next = timerqueue_iterate_next(next);
502a59855cdSRafael J. Wysocki 			if (!next)
503a59855cdSRafael J. Wysocki 				continue;
504a59855cdSRafael J. Wysocki 
505a59855cdSRafael J. Wysocki 			timer = container_of(next, struct hrtimer, node);
506a59855cdSRafael J. Wysocki 		}
5079bc74919SThomas Gleixner 		expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
5082456e855SThomas Gleixner 		if (expires < expires_next) {
5099bc74919SThomas Gleixner 			expires_next = expires;
510a59855cdSRafael J. Wysocki 
511a59855cdSRafael J. Wysocki 			/* Skip cpu_base update if a timer is being excluded. */
512a59855cdSRafael J. Wysocki 			if (exclude)
513a59855cdSRafael J. Wysocki 				continue;
514a59855cdSRafael J. Wysocki 
5155da70160SAnna-Maria Gleixner 			if (timer->is_soft)
5165da70160SAnna-Maria Gleixner 				cpu_base->softirq_next_timer = timer;
5175da70160SAnna-Maria Gleixner 			else
518eb27926bSAnna-Maria Gleixner 				cpu_base->next_timer = timer;
519895bdfa7SThomas Gleixner 		}
5209bc74919SThomas Gleixner 	}
5219bc74919SThomas Gleixner 	/*
5229bc74919SThomas Gleixner 	 * clock_was_set() might have changed base->offset of any of
5239bc74919SThomas Gleixner 	 * the clock bases so the result might be negative. Fix it up
5249bc74919SThomas Gleixner 	 * to prevent a false positive in clockevents_program_event().
5259bc74919SThomas Gleixner 	 */
5262456e855SThomas Gleixner 	if (expires_next < 0)
5272456e855SThomas Gleixner 		expires_next = 0;
5289bc74919SThomas Gleixner 	return expires_next;
5299bc74919SThomas Gleixner }
5309bc74919SThomas Gleixner 
531c458b1d1SAnna-Maria Gleixner /*
532c458b1d1SAnna-Maria Gleixner  * Recomputes cpu_base::*next_timer and returns the earliest expires_next but
533c458b1d1SAnna-Maria Gleixner  * does not set cpu_base::*expires_next, that is done by hrtimer_reprogram.
534c458b1d1SAnna-Maria Gleixner  *
5355da70160SAnna-Maria Gleixner  * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
5365da70160SAnna-Maria Gleixner  * those timers will get run whenever the softirq gets handled, at the end of
5375da70160SAnna-Maria Gleixner  * hrtimer_run_softirq(), hrtimer_update_softirq_timer() will re-add these bases.
5385da70160SAnna-Maria Gleixner  *
5395da70160SAnna-Maria Gleixner  * Therefore softirq values are those from the HRTIMER_ACTIVE_SOFT clock bases.
5405da70160SAnna-Maria Gleixner  * The !softirq values are the minima across HRTIMER_ACTIVE_ALL, unless an actual
5415da70160SAnna-Maria Gleixner  * softirq is pending, in which case they're the minima of HRTIMER_ACTIVE_HARD.
5425da70160SAnna-Maria Gleixner  *
543c458b1d1SAnna-Maria Gleixner  * @active_mask must be one of:
5445da70160SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_ALL,
545c458b1d1SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_SOFT, or
546c458b1d1SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_HARD.
547c458b1d1SAnna-Maria Gleixner  */
5485da70160SAnna-Maria Gleixner static ktime_t
5495da70160SAnna-Maria Gleixner __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_mask)
550ad38f596SAnna-Maria Gleixner {
551c458b1d1SAnna-Maria Gleixner 	unsigned int active;
5525da70160SAnna-Maria Gleixner 	struct hrtimer *next_timer = NULL;
553ad38f596SAnna-Maria Gleixner 	ktime_t expires_next = KTIME_MAX;
554ad38f596SAnna-Maria Gleixner 
5555da70160SAnna-Maria Gleixner 	if (!cpu_base->softirq_activated && (active_mask & HRTIMER_ACTIVE_SOFT)) {
5565da70160SAnna-Maria Gleixner 		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
5575da70160SAnna-Maria Gleixner 		cpu_base->softirq_next_timer = NULL;
558a59855cdSRafael J. Wysocki 		expires_next = __hrtimer_next_event_base(cpu_base, NULL,
559a59855cdSRafael J. Wysocki 							 active, KTIME_MAX);
560ad38f596SAnna-Maria Gleixner 
5615da70160SAnna-Maria Gleixner 		next_timer = cpu_base->softirq_next_timer;
5625da70160SAnna-Maria Gleixner 	}
5635da70160SAnna-Maria Gleixner 
5645da70160SAnna-Maria Gleixner 	if (active_mask & HRTIMER_ACTIVE_HARD) {
5655da70160SAnna-Maria Gleixner 		active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
5665da70160SAnna-Maria Gleixner 		cpu_base->next_timer = next_timer;
567a59855cdSRafael J. Wysocki 		expires_next = __hrtimer_next_event_base(cpu_base, NULL, active,
568a59855cdSRafael J. Wysocki 							 expires_next);
5695da70160SAnna-Maria Gleixner 	}
570ad38f596SAnna-Maria Gleixner 
571ad38f596SAnna-Maria Gleixner 	return expires_next;
572ad38f596SAnna-Maria Gleixner }
573ad38f596SAnna-Maria Gleixner 
57421d6d52aSThomas Gleixner static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
57521d6d52aSThomas Gleixner {
57621d6d52aSThomas Gleixner 	ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
577a3ed0e43SThomas Gleixner 	ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
57821d6d52aSThomas Gleixner 	ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
57921d6d52aSThomas Gleixner 
5805da70160SAnna-Maria Gleixner 	ktime_t now = ktime_get_update_offsets_now(&base->clock_was_set_seq,
581a3ed0e43SThomas Gleixner 					    offs_real, offs_boot, offs_tai);
5825da70160SAnna-Maria Gleixner 
5835da70160SAnna-Maria Gleixner 	base->clock_base[HRTIMER_BASE_REALTIME_SOFT].offset = *offs_real;
584a3ed0e43SThomas Gleixner 	base->clock_base[HRTIMER_BASE_BOOTTIME_SOFT].offset = *offs_boot;
5855da70160SAnna-Maria Gleixner 	base->clock_base[HRTIMER_BASE_TAI_SOFT].offset = *offs_tai;
5865da70160SAnna-Maria Gleixner 
5875da70160SAnna-Maria Gleixner 	return now;
58821d6d52aSThomas Gleixner }
58921d6d52aSThomas Gleixner 
59028bfd18bSAnna-Maria Gleixner /*
59128bfd18bSAnna-Maria Gleixner  * Is the high resolution mode active ?
59228bfd18bSAnna-Maria Gleixner  */
59328bfd18bSAnna-Maria Gleixner static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
59428bfd18bSAnna-Maria Gleixner {
59528bfd18bSAnna-Maria Gleixner 	return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
59628bfd18bSAnna-Maria Gleixner 		cpu_base->hres_active : 0;
59728bfd18bSAnna-Maria Gleixner }
59828bfd18bSAnna-Maria Gleixner 
59928bfd18bSAnna-Maria Gleixner static inline int hrtimer_hres_active(void)
60028bfd18bSAnna-Maria Gleixner {
60128bfd18bSAnna-Maria Gleixner 	return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
60228bfd18bSAnna-Maria Gleixner }
60328bfd18bSAnna-Maria Gleixner 
6045cee9645SThomas Gleixner /*
6055cee9645SThomas Gleixner  * Reprogram the event source with checking both queues for the
6065cee9645SThomas Gleixner  * next event
6075cee9645SThomas Gleixner  * Called with interrupts disabled and base->lock held
6085cee9645SThomas Gleixner  */
6095cee9645SThomas Gleixner static void
6105cee9645SThomas Gleixner hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
6115cee9645SThomas Gleixner {
61221d6d52aSThomas Gleixner 	ktime_t expires_next;
61321d6d52aSThomas Gleixner 
6145da70160SAnna-Maria Gleixner 	/*
6155da70160SAnna-Maria Gleixner 	 * Find the current next expiration time.
6165da70160SAnna-Maria Gleixner 	 */
6175da70160SAnna-Maria Gleixner 	expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
6185da70160SAnna-Maria Gleixner 
6195da70160SAnna-Maria Gleixner 	if (cpu_base->next_timer && cpu_base->next_timer->is_soft) {
6205da70160SAnna-Maria Gleixner 		/*
6215da70160SAnna-Maria Gleixner 		 * When the softirq is activated, hrtimer has to be
6225da70160SAnna-Maria Gleixner 		 * programmed with the first hard hrtimer because soft
6235da70160SAnna-Maria Gleixner 		 * timer interrupt could occur too late.
6245da70160SAnna-Maria Gleixner 		 */
6255da70160SAnna-Maria Gleixner 		if (cpu_base->softirq_activated)
6265da70160SAnna-Maria Gleixner 			expires_next = __hrtimer_get_next_event(cpu_base,
6275da70160SAnna-Maria Gleixner 								HRTIMER_ACTIVE_HARD);
6285da70160SAnna-Maria Gleixner 		else
6295da70160SAnna-Maria Gleixner 			cpu_base->softirq_expires_next = expires_next;
6305da70160SAnna-Maria Gleixner 	}
6315cee9645SThomas Gleixner 
6322456e855SThomas Gleixner 	if (skip_equal && expires_next == cpu_base->expires_next)
6335cee9645SThomas Gleixner 		return;
6345cee9645SThomas Gleixner 
6352456e855SThomas Gleixner 	cpu_base->expires_next = expires_next;
6365cee9645SThomas Gleixner 
6375cee9645SThomas Gleixner 	/*
63861bb4bcbSAnna-Maria Gleixner 	 * If hres is not active, hardware does not have to be
63961bb4bcbSAnna-Maria Gleixner 	 * reprogrammed yet.
64061bb4bcbSAnna-Maria Gleixner 	 *
6415cee9645SThomas Gleixner 	 * If a hang was detected in the last timer interrupt then we
6425cee9645SThomas Gleixner 	 * leave the hang delay active in the hardware. We want the
6435cee9645SThomas Gleixner 	 * system to make progress. That also prevents the following
6445cee9645SThomas Gleixner 	 * scenario:
6455cee9645SThomas Gleixner 	 * T1 expires 50ms from now
6465cee9645SThomas Gleixner 	 * T2 expires 5s from now
6475cee9645SThomas Gleixner 	 *
6485cee9645SThomas Gleixner 	 * T1 is removed, so this code is called and would reprogram
6495cee9645SThomas Gleixner 	 * the hardware to 5s from now. Any hrtimer_start after that
6505cee9645SThomas Gleixner 	 * will not reprogram the hardware due to hang_detected being
6515cee9645SThomas Gleixner 	 * set. So we'd effectivly block all timers until the T2 event
6525cee9645SThomas Gleixner 	 * fires.
6535cee9645SThomas Gleixner 	 */
65461bb4bcbSAnna-Maria Gleixner 	if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
6555cee9645SThomas Gleixner 		return;
6565cee9645SThomas Gleixner 
6575cee9645SThomas Gleixner 	tick_program_event(cpu_base->expires_next, 1);
6585cee9645SThomas Gleixner }
6595cee9645SThomas Gleixner 
660ebba2c72SAnna-Maria Gleixner /* High resolution timer related functions */
661ebba2c72SAnna-Maria Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
662ebba2c72SAnna-Maria Gleixner 
663ebba2c72SAnna-Maria Gleixner /*
664ebba2c72SAnna-Maria Gleixner  * High resolution timer enabled ?
665ebba2c72SAnna-Maria Gleixner  */
666ebba2c72SAnna-Maria Gleixner static bool hrtimer_hres_enabled __read_mostly  = true;
667ebba2c72SAnna-Maria Gleixner unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
668ebba2c72SAnna-Maria Gleixner EXPORT_SYMBOL_GPL(hrtimer_resolution);
669ebba2c72SAnna-Maria Gleixner 
670ebba2c72SAnna-Maria Gleixner /*
671ebba2c72SAnna-Maria Gleixner  * Enable / Disable high resolution mode
672ebba2c72SAnna-Maria Gleixner  */
673ebba2c72SAnna-Maria Gleixner static int __init setup_hrtimer_hres(char *str)
674ebba2c72SAnna-Maria Gleixner {
675ebba2c72SAnna-Maria Gleixner 	return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
676ebba2c72SAnna-Maria Gleixner }
677ebba2c72SAnna-Maria Gleixner 
678ebba2c72SAnna-Maria Gleixner __setup("highres=", setup_hrtimer_hres);
679ebba2c72SAnna-Maria Gleixner 
680ebba2c72SAnna-Maria Gleixner /*
681ebba2c72SAnna-Maria Gleixner  * hrtimer_high_res_enabled - query, if the highres mode is enabled
682ebba2c72SAnna-Maria Gleixner  */
683ebba2c72SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void)
684ebba2c72SAnna-Maria Gleixner {
685ebba2c72SAnna-Maria Gleixner 	return hrtimer_hres_enabled;
686ebba2c72SAnna-Maria Gleixner }
687ebba2c72SAnna-Maria Gleixner 
6885cee9645SThomas Gleixner /*
68911a9fe06SAnna-Maria Gleixner  * Retrigger next event is called after clock was set
69011a9fe06SAnna-Maria Gleixner  *
69111a9fe06SAnna-Maria Gleixner  * Called with interrupts disabled via on_each_cpu()
69211a9fe06SAnna-Maria Gleixner  */
69311a9fe06SAnna-Maria Gleixner static void retrigger_next_event(void *arg)
69411a9fe06SAnna-Maria Gleixner {
69511a9fe06SAnna-Maria Gleixner 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
69611a9fe06SAnna-Maria Gleixner 
69711a9fe06SAnna-Maria Gleixner 	if (!__hrtimer_hres_active(base))
69811a9fe06SAnna-Maria Gleixner 		return;
69911a9fe06SAnna-Maria Gleixner 
70011a9fe06SAnna-Maria Gleixner 	raw_spin_lock(&base->lock);
70111a9fe06SAnna-Maria Gleixner 	hrtimer_update_base(base);
70211a9fe06SAnna-Maria Gleixner 	hrtimer_force_reprogram(base, 0);
70311a9fe06SAnna-Maria Gleixner 	raw_spin_unlock(&base->lock);
70411a9fe06SAnna-Maria Gleixner }
70511a9fe06SAnna-Maria Gleixner 
70611a9fe06SAnna-Maria Gleixner /*
70711a9fe06SAnna-Maria Gleixner  * Switch to high resolution mode
70811a9fe06SAnna-Maria Gleixner  */
70911a9fe06SAnna-Maria Gleixner static void hrtimer_switch_to_hres(void)
71011a9fe06SAnna-Maria Gleixner {
71111a9fe06SAnna-Maria Gleixner 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
71211a9fe06SAnna-Maria Gleixner 
71311a9fe06SAnna-Maria Gleixner 	if (tick_init_highres()) {
7147a6e5537SGeert Uytterhoeven 		pr_warn("Could not switch to high resolution mode on CPU %u\n",
7157a6e5537SGeert Uytterhoeven 			base->cpu);
71611a9fe06SAnna-Maria Gleixner 		return;
71711a9fe06SAnna-Maria Gleixner 	}
71811a9fe06SAnna-Maria Gleixner 	base->hres_active = 1;
71911a9fe06SAnna-Maria Gleixner 	hrtimer_resolution = HIGH_RES_NSEC;
72011a9fe06SAnna-Maria Gleixner 
72111a9fe06SAnna-Maria Gleixner 	tick_setup_sched_timer();
72211a9fe06SAnna-Maria Gleixner 	/* "Retrigger" the interrupt to get things going */
72311a9fe06SAnna-Maria Gleixner 	retrigger_next_event(NULL);
72411a9fe06SAnna-Maria Gleixner }
72511a9fe06SAnna-Maria Gleixner 
72611a9fe06SAnna-Maria Gleixner static void clock_was_set_work(struct work_struct *work)
72711a9fe06SAnna-Maria Gleixner {
72811a9fe06SAnna-Maria Gleixner 	clock_was_set();
72911a9fe06SAnna-Maria Gleixner }
73011a9fe06SAnna-Maria Gleixner 
73111a9fe06SAnna-Maria Gleixner static DECLARE_WORK(hrtimer_work, clock_was_set_work);
73211a9fe06SAnna-Maria Gleixner 
73311a9fe06SAnna-Maria Gleixner /*
73411a9fe06SAnna-Maria Gleixner  * Called from timekeeping and resume code to reprogram the hrtimer
73511a9fe06SAnna-Maria Gleixner  * interrupt device on all cpus.
73611a9fe06SAnna-Maria Gleixner  */
73711a9fe06SAnna-Maria Gleixner void clock_was_set_delayed(void)
73811a9fe06SAnna-Maria Gleixner {
73911a9fe06SAnna-Maria Gleixner 	schedule_work(&hrtimer_work);
74011a9fe06SAnna-Maria Gleixner }
74111a9fe06SAnna-Maria Gleixner 
74211a9fe06SAnna-Maria Gleixner #else
74311a9fe06SAnna-Maria Gleixner 
74411a9fe06SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void) { return 0; }
74511a9fe06SAnna-Maria Gleixner static inline void hrtimer_switch_to_hres(void) { }
74611a9fe06SAnna-Maria Gleixner static inline void retrigger_next_event(void *arg) { }
74711a9fe06SAnna-Maria Gleixner 
74811a9fe06SAnna-Maria Gleixner #endif /* CONFIG_HIGH_RES_TIMERS */
74911a9fe06SAnna-Maria Gleixner 
75011a9fe06SAnna-Maria Gleixner /*
7515cee9645SThomas Gleixner  * When a timer is enqueued and expires earlier than the already enqueued
7525cee9645SThomas Gleixner  * timers, we have to check, whether it expires earlier than the timer for
7535cee9645SThomas Gleixner  * which the clock event device was armed.
7545cee9645SThomas Gleixner  *
7555cee9645SThomas Gleixner  * Called with interrupts disabled and base->cpu_base.lock held
7565cee9645SThomas Gleixner  */
7575da70160SAnna-Maria Gleixner static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
7585cee9645SThomas Gleixner {
759dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
7603ec7a3eeSAnna-Maria Gleixner 	struct hrtimer_clock_base *base = timer->base;
7615cee9645SThomas Gleixner 	ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
7625cee9645SThomas Gleixner 
7635cee9645SThomas Gleixner 	WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
7645cee9645SThomas Gleixner 
7655cee9645SThomas Gleixner 	/*
7665da70160SAnna-Maria Gleixner 	 * CLOCK_REALTIME timer might be requested with an absolute
7675da70160SAnna-Maria Gleixner 	 * expiry time which is less than base->offset. Set it to 0.
7685da70160SAnna-Maria Gleixner 	 */
7695da70160SAnna-Maria Gleixner 	if (expires < 0)
7705da70160SAnna-Maria Gleixner 		expires = 0;
7715da70160SAnna-Maria Gleixner 
7725da70160SAnna-Maria Gleixner 	if (timer->is_soft) {
7735da70160SAnna-Maria Gleixner 		/*
7745da70160SAnna-Maria Gleixner 		 * soft hrtimer could be started on a remote CPU. In this
7755da70160SAnna-Maria Gleixner 		 * case softirq_expires_next needs to be updated on the
7765da70160SAnna-Maria Gleixner 		 * remote CPU. The soft hrtimer will not expire before the
7775da70160SAnna-Maria Gleixner 		 * first hard hrtimer on the remote CPU -
7785da70160SAnna-Maria Gleixner 		 * hrtimer_check_target() prevents this case.
7795da70160SAnna-Maria Gleixner 		 */
7805da70160SAnna-Maria Gleixner 		struct hrtimer_cpu_base *timer_cpu_base = base->cpu_base;
7815da70160SAnna-Maria Gleixner 
7825da70160SAnna-Maria Gleixner 		if (timer_cpu_base->softirq_activated)
7835da70160SAnna-Maria Gleixner 			return;
7845da70160SAnna-Maria Gleixner 
7855da70160SAnna-Maria Gleixner 		if (!ktime_before(expires, timer_cpu_base->softirq_expires_next))
7865da70160SAnna-Maria Gleixner 			return;
7875da70160SAnna-Maria Gleixner 
7885da70160SAnna-Maria Gleixner 		timer_cpu_base->softirq_next_timer = timer;
7895da70160SAnna-Maria Gleixner 		timer_cpu_base->softirq_expires_next = expires;
7905da70160SAnna-Maria Gleixner 
7915da70160SAnna-Maria Gleixner 		if (!ktime_before(expires, timer_cpu_base->expires_next) ||
7925da70160SAnna-Maria Gleixner 		    !reprogram)
7935da70160SAnna-Maria Gleixner 			return;
7945da70160SAnna-Maria Gleixner 	}
7955da70160SAnna-Maria Gleixner 
7965da70160SAnna-Maria Gleixner 	/*
797c6eb3f70SThomas Gleixner 	 * If the timer is not on the current cpu, we cannot reprogram
798c6eb3f70SThomas Gleixner 	 * the other cpus clock event device.
7995cee9645SThomas Gleixner 	 */
800c6eb3f70SThomas Gleixner 	if (base->cpu_base != cpu_base)
801c6eb3f70SThomas Gleixner 		return;
802c6eb3f70SThomas Gleixner 
803c6eb3f70SThomas Gleixner 	/*
804c6eb3f70SThomas Gleixner 	 * If the hrtimer interrupt is running, then it will
805c6eb3f70SThomas Gleixner 	 * reevaluate the clock bases and reprogram the clock event
806c6eb3f70SThomas Gleixner 	 * device. The callbacks are always executed in hard interrupt
807c6eb3f70SThomas Gleixner 	 * context so we don't need an extra check for a running
808c6eb3f70SThomas Gleixner 	 * callback.
809c6eb3f70SThomas Gleixner 	 */
810c6eb3f70SThomas Gleixner 	if (cpu_base->in_hrtirq)
811c6eb3f70SThomas Gleixner 		return;
8125cee9645SThomas Gleixner 
8132456e855SThomas Gleixner 	if (expires >= cpu_base->expires_next)
814c6eb3f70SThomas Gleixner 		return;
8155cee9645SThomas Gleixner 
816c6eb3f70SThomas Gleixner 	/* Update the pointer to the next expiring timer */
817895bdfa7SThomas Gleixner 	cpu_base->next_timer = timer;
81814c80341SAnna-Maria Gleixner 	cpu_base->expires_next = expires;
8199bc74919SThomas Gleixner 
8209bc74919SThomas Gleixner 	/*
82114c80341SAnna-Maria Gleixner 	 * If hres is not active, hardware does not have to be
82214c80341SAnna-Maria Gleixner 	 * programmed yet.
82314c80341SAnna-Maria Gleixner 	 *
8245cee9645SThomas Gleixner 	 * If a hang was detected in the last timer interrupt then we
8255cee9645SThomas Gleixner 	 * do not schedule a timer which is earlier than the expiry
8265cee9645SThomas Gleixner 	 * which we enforced in the hang detection. We want the system
8275cee9645SThomas Gleixner 	 * to make progress.
8285cee9645SThomas Gleixner 	 */
82914c80341SAnna-Maria Gleixner 	if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
830c6eb3f70SThomas Gleixner 		return;
8315cee9645SThomas Gleixner 
8325cee9645SThomas Gleixner 	/*
833c6eb3f70SThomas Gleixner 	 * Program the timer hardware. We enforce the expiry for
834c6eb3f70SThomas Gleixner 	 * events which are already in the past.
8355cee9645SThomas Gleixner 	 */
836c6eb3f70SThomas Gleixner 	tick_program_event(expires, 1);
8375cee9645SThomas Gleixner }
8385cee9645SThomas Gleixner 
8395cee9645SThomas Gleixner /*
8405cee9645SThomas Gleixner  * Clock realtime was set
8415cee9645SThomas Gleixner  *
8425cee9645SThomas Gleixner  * Change the offset of the realtime clock vs. the monotonic
8435cee9645SThomas Gleixner  * clock.
8445cee9645SThomas Gleixner  *
8455cee9645SThomas Gleixner  * We might have to reprogram the high resolution timer interrupt. On
8465cee9645SThomas Gleixner  * SMP we call the architecture specific code to retrigger _all_ high
8475cee9645SThomas Gleixner  * resolution timer interrupts. On UP we just disable interrupts and
8485cee9645SThomas Gleixner  * call the high resolution interrupt code.
8495cee9645SThomas Gleixner  */
8505cee9645SThomas Gleixner void clock_was_set(void)
8515cee9645SThomas Gleixner {
8525cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
8535cee9645SThomas Gleixner 	/* Retrigger the CPU local events everywhere */
8545cee9645SThomas Gleixner 	on_each_cpu(retrigger_next_event, NULL, 1);
8555cee9645SThomas Gleixner #endif
8565cee9645SThomas Gleixner 	timerfd_clock_was_set();
8575cee9645SThomas Gleixner }
8585cee9645SThomas Gleixner 
8595cee9645SThomas Gleixner /*
8605cee9645SThomas Gleixner  * During resume we might have to reprogram the high resolution timer
8615cee9645SThomas Gleixner  * interrupt on all online CPUs.  However, all other CPUs will be
8625cee9645SThomas Gleixner  * stopped with IRQs interrupts disabled so the clock_was_set() call
8635cee9645SThomas Gleixner  * must be deferred.
8645cee9645SThomas Gleixner  */
8655cee9645SThomas Gleixner void hrtimers_resume(void)
8665cee9645SThomas Gleixner {
86753bef3fdSFrederic Weisbecker 	lockdep_assert_irqs_disabled();
8685cee9645SThomas Gleixner 	/* Retrigger on the local CPU */
8695cee9645SThomas Gleixner 	retrigger_next_event(NULL);
8705cee9645SThomas Gleixner 	/* And schedule a retrigger for all others */
8715cee9645SThomas Gleixner 	clock_was_set_delayed();
8725cee9645SThomas Gleixner }
8735cee9645SThomas Gleixner 
8745cee9645SThomas Gleixner /*
8755cee9645SThomas Gleixner  * Counterpart to lock_hrtimer_base above:
8765cee9645SThomas Gleixner  */
8775cee9645SThomas Gleixner static inline
8785cee9645SThomas Gleixner void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
8795cee9645SThomas Gleixner {
8805cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
8815cee9645SThomas Gleixner }
8825cee9645SThomas Gleixner 
8835cee9645SThomas Gleixner /**
8845cee9645SThomas Gleixner  * hrtimer_forward - forward the timer expiry
8855cee9645SThomas Gleixner  * @timer:	hrtimer to forward
8865cee9645SThomas Gleixner  * @now:	forward past this time
8875cee9645SThomas Gleixner  * @interval:	the interval to forward
8885cee9645SThomas Gleixner  *
8895cee9645SThomas Gleixner  * Forward the timer expiry so it will expire in the future.
8905cee9645SThomas Gleixner  * Returns the number of overruns.
89191e5a217SThomas Gleixner  *
89291e5a217SThomas Gleixner  * Can be safely called from the callback function of @timer. If
89391e5a217SThomas Gleixner  * called from other contexts @timer must neither be enqueued nor
89491e5a217SThomas Gleixner  * running the callback and the caller needs to take care of
89591e5a217SThomas Gleixner  * serialization.
89691e5a217SThomas Gleixner  *
89791e5a217SThomas Gleixner  * Note: This only updates the timer expiry value and does not requeue
89891e5a217SThomas Gleixner  * the timer.
8995cee9645SThomas Gleixner  */
9005cee9645SThomas Gleixner u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
9015cee9645SThomas Gleixner {
9025cee9645SThomas Gleixner 	u64 orun = 1;
9035cee9645SThomas Gleixner 	ktime_t delta;
9045cee9645SThomas Gleixner 
9055cee9645SThomas Gleixner 	delta = ktime_sub(now, hrtimer_get_expires(timer));
9065cee9645SThomas Gleixner 
9072456e855SThomas Gleixner 	if (delta < 0)
9085cee9645SThomas Gleixner 		return 0;
9095cee9645SThomas Gleixner 
9105de2755cSPeter Zijlstra 	if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
9115de2755cSPeter Zijlstra 		return 0;
9125de2755cSPeter Zijlstra 
9132456e855SThomas Gleixner 	if (interval < hrtimer_resolution)
9142456e855SThomas Gleixner 		interval = hrtimer_resolution;
9155cee9645SThomas Gleixner 
9162456e855SThomas Gleixner 	if (unlikely(delta >= interval)) {
9175cee9645SThomas Gleixner 		s64 incr = ktime_to_ns(interval);
9185cee9645SThomas Gleixner 
9195cee9645SThomas Gleixner 		orun = ktime_divns(delta, incr);
9205cee9645SThomas Gleixner 		hrtimer_add_expires_ns(timer, incr * orun);
9212456e855SThomas Gleixner 		if (hrtimer_get_expires_tv64(timer) > now)
9225cee9645SThomas Gleixner 			return orun;
9235cee9645SThomas Gleixner 		/*
9245cee9645SThomas Gleixner 		 * This (and the ktime_add() below) is the
9255cee9645SThomas Gleixner 		 * correction for exact:
9265cee9645SThomas Gleixner 		 */
9275cee9645SThomas Gleixner 		orun++;
9285cee9645SThomas Gleixner 	}
9295cee9645SThomas Gleixner 	hrtimer_add_expires(timer, interval);
9305cee9645SThomas Gleixner 
9315cee9645SThomas Gleixner 	return orun;
9325cee9645SThomas Gleixner }
9335cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_forward);
9345cee9645SThomas Gleixner 
9355cee9645SThomas Gleixner /*
9365cee9645SThomas Gleixner  * enqueue_hrtimer - internal function to (re)start a timer
9375cee9645SThomas Gleixner  *
9385cee9645SThomas Gleixner  * The timer is inserted in expiry order. Insertion into the
9395cee9645SThomas Gleixner  * red black tree is O(log(n)). Must hold the base lock.
9405cee9645SThomas Gleixner  *
9415cee9645SThomas Gleixner  * Returns 1 when the new timer is the leftmost timer in the tree.
9425cee9645SThomas Gleixner  */
9435cee9645SThomas Gleixner static int enqueue_hrtimer(struct hrtimer *timer,
94463e2ed36SAnna-Maria Gleixner 			   struct hrtimer_clock_base *base,
94563e2ed36SAnna-Maria Gleixner 			   enum hrtimer_mode mode)
9465cee9645SThomas Gleixner {
94763e2ed36SAnna-Maria Gleixner 	debug_activate(timer, mode);
9485cee9645SThomas Gleixner 
9495cee9645SThomas Gleixner 	base->cpu_base->active_bases |= 1 << base->index;
9505cee9645SThomas Gleixner 
951887d9dc9SPeter Zijlstra 	timer->state = HRTIMER_STATE_ENQUEUED;
9525cee9645SThomas Gleixner 
953b97f44c9SThomas Gleixner 	return timerqueue_add(&base->active, &timer->node);
9545cee9645SThomas Gleixner }
9555cee9645SThomas Gleixner 
9565cee9645SThomas Gleixner /*
9575cee9645SThomas Gleixner  * __remove_hrtimer - internal function to remove a timer
9585cee9645SThomas Gleixner  *
9595cee9645SThomas Gleixner  * Caller must hold the base lock.
9605cee9645SThomas Gleixner  *
9615cee9645SThomas Gleixner  * High resolution timer mode reprograms the clock event device when the
9625cee9645SThomas Gleixner  * timer is the one which expires next. The caller can disable this by setting
9635cee9645SThomas Gleixner  * reprogram to zero. This is useful, when the context does a reprogramming
9645cee9645SThomas Gleixner  * anyway (e.g. timer interrupt)
9655cee9645SThomas Gleixner  */
9665cee9645SThomas Gleixner static void __remove_hrtimer(struct hrtimer *timer,
9675cee9645SThomas Gleixner 			     struct hrtimer_clock_base *base,
968203cbf77SThomas Gleixner 			     u8 newstate, int reprogram)
9695cee9645SThomas Gleixner {
970e19ffe8bSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = base->cpu_base;
971203cbf77SThomas Gleixner 	u8 state = timer->state;
9725cee9645SThomas Gleixner 
9735cee9645SThomas Gleixner 	timer->state = newstate;
974895bdfa7SThomas Gleixner 	if (!(state & HRTIMER_STATE_ENQUEUED))
975895bdfa7SThomas Gleixner 		return;
9765cee9645SThomas Gleixner 
977b97f44c9SThomas Gleixner 	if (!timerqueue_del(&base->active, &timer->node))
978e19ffe8bSThomas Gleixner 		cpu_base->active_bases &= ~(1 << base->index);
979d9f0acdeSViresh Kumar 
980895bdfa7SThomas Gleixner 	/*
981895bdfa7SThomas Gleixner 	 * Note: If reprogram is false we do not update
982895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. This happens when we remove the first
983895bdfa7SThomas Gleixner 	 * timer on a remote cpu. No harm as we never dereference
984895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. So the worst thing what can happen is
985895bdfa7SThomas Gleixner 	 * an superflous call to hrtimer_force_reprogram() on the
986895bdfa7SThomas Gleixner 	 * remote cpu later on if the same timer gets enqueued again.
987895bdfa7SThomas Gleixner 	 */
988895bdfa7SThomas Gleixner 	if (reprogram && timer == cpu_base->next_timer)
989e19ffe8bSThomas Gleixner 		hrtimer_force_reprogram(cpu_base, 1);
9905cee9645SThomas Gleixner }
9915cee9645SThomas Gleixner 
9925cee9645SThomas Gleixner /*
9935cee9645SThomas Gleixner  * remove hrtimer, called with base lock held
9945cee9645SThomas Gleixner  */
9955cee9645SThomas Gleixner static inline int
9968edfb036SPeter Zijlstra remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart)
9975cee9645SThomas Gleixner {
9985cee9645SThomas Gleixner 	if (hrtimer_is_queued(timer)) {
999203cbf77SThomas Gleixner 		u8 state = timer->state;
10005cee9645SThomas Gleixner 		int reprogram;
10015cee9645SThomas Gleixner 
10025cee9645SThomas Gleixner 		/*
10035cee9645SThomas Gleixner 		 * Remove the timer and force reprogramming when high
10045cee9645SThomas Gleixner 		 * resolution mode is active and the timer is on the current
10055cee9645SThomas Gleixner 		 * CPU. If we remove a timer on another CPU, reprogramming is
10065cee9645SThomas Gleixner 		 * skipped. The interrupt event on this CPU is fired and
10075cee9645SThomas Gleixner 		 * reprogramming happens in the interrupt handler. This is a
10085cee9645SThomas Gleixner 		 * rare case and less expensive than a smp call.
10095cee9645SThomas Gleixner 		 */
10105cee9645SThomas Gleixner 		debug_deactivate(timer);
1011dc5df73bSChristoph Lameter 		reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
10128edfb036SPeter Zijlstra 
1013887d9dc9SPeter Zijlstra 		if (!restart)
1014887d9dc9SPeter Zijlstra 			state = HRTIMER_STATE_INACTIVE;
1015887d9dc9SPeter Zijlstra 
10165cee9645SThomas Gleixner 		__remove_hrtimer(timer, base, state, reprogram);
10175cee9645SThomas Gleixner 		return 1;
10185cee9645SThomas Gleixner 	}
10195cee9645SThomas Gleixner 	return 0;
10205cee9645SThomas Gleixner }
10215cee9645SThomas Gleixner 
1022203cbf77SThomas Gleixner static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
1023203cbf77SThomas Gleixner 					    const enum hrtimer_mode mode)
1024203cbf77SThomas Gleixner {
1025203cbf77SThomas Gleixner #ifdef CONFIG_TIME_LOW_RES
1026203cbf77SThomas Gleixner 	/*
1027203cbf77SThomas Gleixner 	 * CONFIG_TIME_LOW_RES indicates that the system has no way to return
1028203cbf77SThomas Gleixner 	 * granular time values. For relative timers we add hrtimer_resolution
1029203cbf77SThomas Gleixner 	 * (i.e. one jiffie) to prevent short timeouts.
1030203cbf77SThomas Gleixner 	 */
1031203cbf77SThomas Gleixner 	timer->is_rel = mode & HRTIMER_MODE_REL;
1032203cbf77SThomas Gleixner 	if (timer->is_rel)
10338b0e1953SThomas Gleixner 		tim = ktime_add_safe(tim, hrtimer_resolution);
1034203cbf77SThomas Gleixner #endif
1035203cbf77SThomas Gleixner 	return tim;
1036203cbf77SThomas Gleixner }
1037203cbf77SThomas Gleixner 
10385da70160SAnna-Maria Gleixner static void
10395da70160SAnna-Maria Gleixner hrtimer_update_softirq_timer(struct hrtimer_cpu_base *cpu_base, bool reprogram)
10405da70160SAnna-Maria Gleixner {
10415da70160SAnna-Maria Gleixner 	ktime_t expires;
10425da70160SAnna-Maria Gleixner 
10435da70160SAnna-Maria Gleixner 	/*
10445da70160SAnna-Maria Gleixner 	 * Find the next SOFT expiration.
10455da70160SAnna-Maria Gleixner 	 */
10465da70160SAnna-Maria Gleixner 	expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
10475da70160SAnna-Maria Gleixner 
10485da70160SAnna-Maria Gleixner 	/*
10495da70160SAnna-Maria Gleixner 	 * reprogramming needs to be triggered, even if the next soft
10505da70160SAnna-Maria Gleixner 	 * hrtimer expires at the same time than the next hard
10515da70160SAnna-Maria Gleixner 	 * hrtimer. cpu_base->softirq_expires_next needs to be updated!
10525da70160SAnna-Maria Gleixner 	 */
10535da70160SAnna-Maria Gleixner 	if (expires == KTIME_MAX)
10545da70160SAnna-Maria Gleixner 		return;
10555da70160SAnna-Maria Gleixner 
10565da70160SAnna-Maria Gleixner 	/*
10575da70160SAnna-Maria Gleixner 	 * cpu_base->*next_timer is recomputed by __hrtimer_get_next_event()
10585da70160SAnna-Maria Gleixner 	 * cpu_base->*expires_next is only set by hrtimer_reprogram()
10595da70160SAnna-Maria Gleixner 	 */
10605da70160SAnna-Maria Gleixner 	hrtimer_reprogram(cpu_base->softirq_next_timer, reprogram);
10615da70160SAnna-Maria Gleixner }
10625da70160SAnna-Maria Gleixner 
1063138a6b7aSAnna-Maria Gleixner static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1064138a6b7aSAnna-Maria Gleixner 				    u64 delta_ns, const enum hrtimer_mode mode,
1065138a6b7aSAnna-Maria Gleixner 				    struct hrtimer_clock_base *base)
10665cee9645SThomas Gleixner {
1067138a6b7aSAnna-Maria Gleixner 	struct hrtimer_clock_base *new_base;
10685cee9645SThomas Gleixner 
10695cee9645SThomas Gleixner 	/* Remove an active timer from the queue: */
10708edfb036SPeter Zijlstra 	remove_hrtimer(timer, base, true);
10715cee9645SThomas Gleixner 
1072203cbf77SThomas Gleixner 	if (mode & HRTIMER_MODE_REL)
10735cee9645SThomas Gleixner 		tim = ktime_add_safe(tim, base->get_time());
1074203cbf77SThomas Gleixner 
1075203cbf77SThomas Gleixner 	tim = hrtimer_update_lowres(timer, tim, mode);
10765cee9645SThomas Gleixner 
10775cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(timer, tim, delta_ns);
10785cee9645SThomas Gleixner 
10795cee9645SThomas Gleixner 	/* Switch the timer base, if necessary: */
10805cee9645SThomas Gleixner 	new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
10815cee9645SThomas Gleixner 
1082138a6b7aSAnna-Maria Gleixner 	return enqueue_hrtimer(timer, new_base, mode);
1083138a6b7aSAnna-Maria Gleixner }
10845da70160SAnna-Maria Gleixner 
1085138a6b7aSAnna-Maria Gleixner /**
1086138a6b7aSAnna-Maria Gleixner  * hrtimer_start_range_ns - (re)start an hrtimer
1087138a6b7aSAnna-Maria Gleixner  * @timer:	the timer to be added
1088138a6b7aSAnna-Maria Gleixner  * @tim:	expiry time
1089138a6b7aSAnna-Maria Gleixner  * @delta_ns:	"slack" range for the timer
1090138a6b7aSAnna-Maria Gleixner  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
10915da70160SAnna-Maria Gleixner  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
10925da70160SAnna-Maria Gleixner  *		softirq based mode is considered for debug purpose only!
1093138a6b7aSAnna-Maria Gleixner  */
1094138a6b7aSAnna-Maria Gleixner void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1095138a6b7aSAnna-Maria Gleixner 			    u64 delta_ns, const enum hrtimer_mode mode)
1096138a6b7aSAnna-Maria Gleixner {
1097138a6b7aSAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1098138a6b7aSAnna-Maria Gleixner 	unsigned long flags;
109949a2a075SViresh Kumar 
11005da70160SAnna-Maria Gleixner 	/*
11015da70160SAnna-Maria Gleixner 	 * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
11025da70160SAnna-Maria Gleixner 	 * match.
11035da70160SAnna-Maria Gleixner 	 */
11045da70160SAnna-Maria Gleixner 	WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft);
11055da70160SAnna-Maria Gleixner 
1106138a6b7aSAnna-Maria Gleixner 	base = lock_hrtimer_base(timer, &flags);
1107138a6b7aSAnna-Maria Gleixner 
1108138a6b7aSAnna-Maria Gleixner 	if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base))
11095da70160SAnna-Maria Gleixner 		hrtimer_reprogram(timer, true);
1110138a6b7aSAnna-Maria Gleixner 
11115cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
11125cee9645SThomas Gleixner }
11135cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
11145cee9645SThomas Gleixner 
11155cee9645SThomas Gleixner /**
11165cee9645SThomas Gleixner  * hrtimer_try_to_cancel - try to deactivate a timer
11175cee9645SThomas Gleixner  * @timer:	hrtimer to stop
11185cee9645SThomas Gleixner  *
11195cee9645SThomas Gleixner  * Returns:
11205cee9645SThomas Gleixner  *  0 when the timer was not active
11215cee9645SThomas Gleixner  *  1 when the timer was active
11220ba42a59SMasanari Iida  * -1 when the timer is currently executing the callback function and
11235cee9645SThomas Gleixner  *    cannot be stopped
11245cee9645SThomas Gleixner  */
11255cee9645SThomas Gleixner int hrtimer_try_to_cancel(struct hrtimer *timer)
11265cee9645SThomas Gleixner {
11275cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
11285cee9645SThomas Gleixner 	unsigned long flags;
11295cee9645SThomas Gleixner 	int ret = -1;
11305cee9645SThomas Gleixner 
113119d9f422SThomas Gleixner 	/*
113219d9f422SThomas Gleixner 	 * Check lockless first. If the timer is not active (neither
113319d9f422SThomas Gleixner 	 * enqueued nor running the callback, nothing to do here.  The
113419d9f422SThomas Gleixner 	 * base lock does not serialize against a concurrent enqueue,
113519d9f422SThomas Gleixner 	 * so we can avoid taking it.
113619d9f422SThomas Gleixner 	 */
113719d9f422SThomas Gleixner 	if (!hrtimer_active(timer))
113819d9f422SThomas Gleixner 		return 0;
113919d9f422SThomas Gleixner 
11405cee9645SThomas Gleixner 	base = lock_hrtimer_base(timer, &flags);
11415cee9645SThomas Gleixner 
11425cee9645SThomas Gleixner 	if (!hrtimer_callback_running(timer))
11438edfb036SPeter Zijlstra 		ret = remove_hrtimer(timer, base, false);
11445cee9645SThomas Gleixner 
11455cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
11465cee9645SThomas Gleixner 
11475cee9645SThomas Gleixner 	return ret;
11485cee9645SThomas Gleixner 
11495cee9645SThomas Gleixner }
11505cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
11515cee9645SThomas Gleixner 
11525cee9645SThomas Gleixner /**
11535cee9645SThomas Gleixner  * hrtimer_cancel - cancel a timer and wait for the handler to finish.
11545cee9645SThomas Gleixner  * @timer:	the timer to be cancelled
11555cee9645SThomas Gleixner  *
11565cee9645SThomas Gleixner  * Returns:
11575cee9645SThomas Gleixner  *  0 when the timer was not active
11585cee9645SThomas Gleixner  *  1 when the timer was active
11595cee9645SThomas Gleixner  */
11605cee9645SThomas Gleixner int hrtimer_cancel(struct hrtimer *timer)
11615cee9645SThomas Gleixner {
11625cee9645SThomas Gleixner 	for (;;) {
11635cee9645SThomas Gleixner 		int ret = hrtimer_try_to_cancel(timer);
11645cee9645SThomas Gleixner 
11655cee9645SThomas Gleixner 		if (ret >= 0)
11665cee9645SThomas Gleixner 			return ret;
11675cee9645SThomas Gleixner 		cpu_relax();
11685cee9645SThomas Gleixner 	}
11695cee9645SThomas Gleixner }
11705cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_cancel);
11715cee9645SThomas Gleixner 
11725cee9645SThomas Gleixner /**
11735cee9645SThomas Gleixner  * hrtimer_get_remaining - get remaining time for the timer
11745cee9645SThomas Gleixner  * @timer:	the timer to read
1175203cbf77SThomas Gleixner  * @adjust:	adjust relative timers when CONFIG_TIME_LOW_RES=y
11765cee9645SThomas Gleixner  */
1177203cbf77SThomas Gleixner ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
11785cee9645SThomas Gleixner {
11795cee9645SThomas Gleixner 	unsigned long flags;
11805cee9645SThomas Gleixner 	ktime_t rem;
11815cee9645SThomas Gleixner 
11825cee9645SThomas Gleixner 	lock_hrtimer_base(timer, &flags);
1183203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1184203cbf77SThomas Gleixner 		rem = hrtimer_expires_remaining_adjusted(timer);
1185203cbf77SThomas Gleixner 	else
11865cee9645SThomas Gleixner 		rem = hrtimer_expires_remaining(timer);
11875cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
11885cee9645SThomas Gleixner 
11895cee9645SThomas Gleixner 	return rem;
11905cee9645SThomas Gleixner }
1191203cbf77SThomas Gleixner EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
11925cee9645SThomas Gleixner 
11935cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
11945cee9645SThomas Gleixner /**
11955cee9645SThomas Gleixner  * hrtimer_get_next_event - get the time until next expiry event
11965cee9645SThomas Gleixner  *
1197c1ad348bSThomas Gleixner  * Returns the next expiry time or KTIME_MAX if no timer is pending.
11985cee9645SThomas Gleixner  */
1199c1ad348bSThomas Gleixner u64 hrtimer_get_next_event(void)
12005cee9645SThomas Gleixner {
1201dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1202c1ad348bSThomas Gleixner 	u64 expires = KTIME_MAX;
12035cee9645SThomas Gleixner 	unsigned long flags;
12045cee9645SThomas Gleixner 
12055cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
12065cee9645SThomas Gleixner 
1207e19ffe8bSThomas Gleixner 	if (!__hrtimer_hres_active(cpu_base))
12085da70160SAnna-Maria Gleixner 		expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
12095cee9645SThomas Gleixner 
12105cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
12115cee9645SThomas Gleixner 
1212c1ad348bSThomas Gleixner 	return expires;
12135cee9645SThomas Gleixner }
1214a59855cdSRafael J. Wysocki 
1215a59855cdSRafael J. Wysocki /**
1216a59855cdSRafael J. Wysocki  * hrtimer_next_event_without - time until next expiry event w/o one timer
1217a59855cdSRafael J. Wysocki  * @exclude:	timer to exclude
1218a59855cdSRafael J. Wysocki  *
1219a59855cdSRafael J. Wysocki  * Returns the next expiry time over all timers except for the @exclude one or
1220a59855cdSRafael J. Wysocki  * KTIME_MAX if none of them is pending.
1221a59855cdSRafael J. Wysocki  */
1222a59855cdSRafael J. Wysocki u64 hrtimer_next_event_without(const struct hrtimer *exclude)
1223a59855cdSRafael J. Wysocki {
1224a59855cdSRafael J. Wysocki 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1225a59855cdSRafael J. Wysocki 	u64 expires = KTIME_MAX;
1226a59855cdSRafael J. Wysocki 	unsigned long flags;
1227a59855cdSRafael J. Wysocki 
1228a59855cdSRafael J. Wysocki 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
1229a59855cdSRafael J. Wysocki 
1230a59855cdSRafael J. Wysocki 	if (__hrtimer_hres_active(cpu_base)) {
1231a59855cdSRafael J. Wysocki 		unsigned int active;
1232a59855cdSRafael J. Wysocki 
1233a59855cdSRafael J. Wysocki 		if (!cpu_base->softirq_activated) {
1234a59855cdSRafael J. Wysocki 			active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
1235a59855cdSRafael J. Wysocki 			expires = __hrtimer_next_event_base(cpu_base, exclude,
1236a59855cdSRafael J. Wysocki 							    active, KTIME_MAX);
1237a59855cdSRafael J. Wysocki 		}
1238a59855cdSRafael J. Wysocki 		active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
1239a59855cdSRafael J. Wysocki 		expires = __hrtimer_next_event_base(cpu_base, exclude, active,
1240a59855cdSRafael J. Wysocki 						    expires);
1241a59855cdSRafael J. Wysocki 	}
1242a59855cdSRafael J. Wysocki 
1243a59855cdSRafael J. Wysocki 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1244a59855cdSRafael J. Wysocki 
1245a59855cdSRafael J. Wysocki 	return expires;
1246a59855cdSRafael J. Wysocki }
12475cee9645SThomas Gleixner #endif
12485cee9645SThomas Gleixner 
1249336a9cdeSMarc Zyngier static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1250336a9cdeSMarc Zyngier {
1251336a9cdeSMarc Zyngier 	if (likely(clock_id < MAX_CLOCKS)) {
1252336a9cdeSMarc Zyngier 		int base = hrtimer_clock_to_base_table[clock_id];
1253336a9cdeSMarc Zyngier 
1254336a9cdeSMarc Zyngier 		if (likely(base != HRTIMER_MAX_CLOCK_BASES))
1255336a9cdeSMarc Zyngier 			return base;
1256336a9cdeSMarc Zyngier 	}
1257336a9cdeSMarc Zyngier 	WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1258336a9cdeSMarc Zyngier 	return HRTIMER_BASE_MONOTONIC;
1259336a9cdeSMarc Zyngier }
1260336a9cdeSMarc Zyngier 
12615cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
12625cee9645SThomas Gleixner 			   enum hrtimer_mode mode)
12635cee9645SThomas Gleixner {
126442f42da4SAnna-Maria Gleixner 	bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
126542f42da4SAnna-Maria Gleixner 	int base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
12665cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base;
12675cee9645SThomas Gleixner 
12685cee9645SThomas Gleixner 	memset(timer, 0, sizeof(struct hrtimer));
12695cee9645SThomas Gleixner 
127022127e93SChristoph Lameter 	cpu_base = raw_cpu_ptr(&hrtimer_bases);
12715cee9645SThomas Gleixner 
127248d0c9beSAnna-Maria Gleixner 	/*
127348d0c9beSAnna-Maria Gleixner 	 * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
127448d0c9beSAnna-Maria Gleixner 	 * clock modifications, so they needs to become CLOCK_MONOTONIC to
127548d0c9beSAnna-Maria Gleixner 	 * ensure POSIX compliance.
127648d0c9beSAnna-Maria Gleixner 	 */
127748d0c9beSAnna-Maria Gleixner 	if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
12785cee9645SThomas Gleixner 		clock_id = CLOCK_MONOTONIC;
12795cee9645SThomas Gleixner 
128042f42da4SAnna-Maria Gleixner 	base += hrtimer_clockid_to_base(clock_id);
128142f42da4SAnna-Maria Gleixner 	timer->is_soft = softtimer;
12825cee9645SThomas Gleixner 	timer->base = &cpu_base->clock_base[base];
12835cee9645SThomas Gleixner 	timerqueue_init(&timer->node);
12845cee9645SThomas Gleixner }
12855cee9645SThomas Gleixner 
12865cee9645SThomas Gleixner /**
12875cee9645SThomas Gleixner  * hrtimer_init - initialize a timer to the given clock
12885cee9645SThomas Gleixner  * @timer:	the timer to be initialized
12895cee9645SThomas Gleixner  * @clock_id:	the clock to be used
129042f42da4SAnna-Maria Gleixner  * @mode:       The modes which are relevant for intitialization:
129142f42da4SAnna-Maria Gleixner  *              HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
129242f42da4SAnna-Maria Gleixner  *              HRTIMER_MODE_REL_SOFT
129342f42da4SAnna-Maria Gleixner  *
129442f42da4SAnna-Maria Gleixner  *              The PINNED variants of the above can be handed in,
129542f42da4SAnna-Maria Gleixner  *              but the PINNED bit is ignored as pinning happens
129642f42da4SAnna-Maria Gleixner  *              when the hrtimer is started
12975cee9645SThomas Gleixner  */
12985cee9645SThomas Gleixner void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
12995cee9645SThomas Gleixner 		  enum hrtimer_mode mode)
13005cee9645SThomas Gleixner {
13015cee9645SThomas Gleixner 	debug_init(timer, clock_id, mode);
13025cee9645SThomas Gleixner 	__hrtimer_init(timer, clock_id, mode);
13035cee9645SThomas Gleixner }
13045cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init);
13055cee9645SThomas Gleixner 
1306887d9dc9SPeter Zijlstra /*
1307887d9dc9SPeter Zijlstra  * A timer is active, when it is enqueued into the rbtree or the
1308887d9dc9SPeter Zijlstra  * callback function is running or it's in the state of being migrated
1309887d9dc9SPeter Zijlstra  * to another cpu.
13105cee9645SThomas Gleixner  *
1311887d9dc9SPeter Zijlstra  * It is important for this function to not return a false negative.
13125cee9645SThomas Gleixner  */
1313887d9dc9SPeter Zijlstra bool hrtimer_active(const struct hrtimer *timer)
13145cee9645SThomas Gleixner {
13153f0b9e8eSAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1316887d9dc9SPeter Zijlstra 	unsigned int seq;
13175cee9645SThomas Gleixner 
1318887d9dc9SPeter Zijlstra 	do {
13193f0b9e8eSAnna-Maria Gleixner 		base = READ_ONCE(timer->base);
13203f0b9e8eSAnna-Maria Gleixner 		seq = raw_read_seqcount_begin(&base->seq);
13215cee9645SThomas Gleixner 
1322887d9dc9SPeter Zijlstra 		if (timer->state != HRTIMER_STATE_INACTIVE ||
13233f0b9e8eSAnna-Maria Gleixner 		    base->running == timer)
1324887d9dc9SPeter Zijlstra 			return true;
1325887d9dc9SPeter Zijlstra 
13263f0b9e8eSAnna-Maria Gleixner 	} while (read_seqcount_retry(&base->seq, seq) ||
13273f0b9e8eSAnna-Maria Gleixner 		 base != READ_ONCE(timer->base));
1328887d9dc9SPeter Zijlstra 
1329887d9dc9SPeter Zijlstra 	return false;
13305cee9645SThomas Gleixner }
1331887d9dc9SPeter Zijlstra EXPORT_SYMBOL_GPL(hrtimer_active);
13325cee9645SThomas Gleixner 
1333887d9dc9SPeter Zijlstra /*
1334887d9dc9SPeter Zijlstra  * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1335887d9dc9SPeter Zijlstra  * distinct sections:
1336887d9dc9SPeter Zijlstra  *
1337887d9dc9SPeter Zijlstra  *  - queued:	the timer is queued
1338887d9dc9SPeter Zijlstra  *  - callback:	the timer is being ran
1339887d9dc9SPeter Zijlstra  *  - post:	the timer is inactive or (re)queued
1340887d9dc9SPeter Zijlstra  *
1341887d9dc9SPeter Zijlstra  * On the read side we ensure we observe timer->state and cpu_base->running
1342887d9dc9SPeter Zijlstra  * from the same section, if anything changed while we looked at it, we retry.
1343887d9dc9SPeter Zijlstra  * This includes timer->base changing because sequence numbers alone are
1344887d9dc9SPeter Zijlstra  * insufficient for that.
1345887d9dc9SPeter Zijlstra  *
1346887d9dc9SPeter Zijlstra  * The sequence numbers are required because otherwise we could still observe
1347887d9dc9SPeter Zijlstra  * a false negative if the read side got smeared over multiple consequtive
1348887d9dc9SPeter Zijlstra  * __run_hrtimer() invocations.
1349887d9dc9SPeter Zijlstra  */
1350887d9dc9SPeter Zijlstra 
135121d6d52aSThomas Gleixner static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
135221d6d52aSThomas Gleixner 			  struct hrtimer_clock_base *base,
1353dd934aa8SAnna-Maria Gleixner 			  struct hrtimer *timer, ktime_t *now,
1354dd934aa8SAnna-Maria Gleixner 			  unsigned long flags)
13555cee9645SThomas Gleixner {
13565cee9645SThomas Gleixner 	enum hrtimer_restart (*fn)(struct hrtimer *);
13575cee9645SThomas Gleixner 	int restart;
13585cee9645SThomas Gleixner 
1359887d9dc9SPeter Zijlstra 	lockdep_assert_held(&cpu_base->lock);
13605cee9645SThomas Gleixner 
13615cee9645SThomas Gleixner 	debug_deactivate(timer);
13623f0b9e8eSAnna-Maria Gleixner 	base->running = timer;
1363887d9dc9SPeter Zijlstra 
1364887d9dc9SPeter Zijlstra 	/*
1365887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1366887d9dc9SPeter Zijlstra 	 *
1367887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
13683f0b9e8eSAnna-Maria Gleixner 	 * hrtimer_active() cannot observe base->running == NULL &&
1369887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
1370887d9dc9SPeter Zijlstra 	 */
13713f0b9e8eSAnna-Maria Gleixner 	raw_write_seqcount_barrier(&base->seq);
1372887d9dc9SPeter Zijlstra 
1373887d9dc9SPeter Zijlstra 	__remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
13745cee9645SThomas Gleixner 	fn = timer->function;
13755cee9645SThomas Gleixner 
13765cee9645SThomas Gleixner 	/*
1377203cbf77SThomas Gleixner 	 * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
1378203cbf77SThomas Gleixner 	 * timer is restarted with a period then it becomes an absolute
1379203cbf77SThomas Gleixner 	 * timer. If its not restarted it does not matter.
1380203cbf77SThomas Gleixner 	 */
1381203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES))
1382203cbf77SThomas Gleixner 		timer->is_rel = false;
1383203cbf77SThomas Gleixner 
1384203cbf77SThomas Gleixner 	/*
1385d05ca13bSThomas Gleixner 	 * The timer is marked as running in the CPU base, so it is
1386d05ca13bSThomas Gleixner 	 * protected against migration to a different CPU even if the lock
1387d05ca13bSThomas Gleixner 	 * is dropped.
13885cee9645SThomas Gleixner 	 */
1389dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
13905cee9645SThomas Gleixner 	trace_hrtimer_expire_entry(timer, now);
13915cee9645SThomas Gleixner 	restart = fn(timer);
13925cee9645SThomas Gleixner 	trace_hrtimer_expire_exit(timer);
1393dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irq(&cpu_base->lock);
13945cee9645SThomas Gleixner 
13955cee9645SThomas Gleixner 	/*
1396887d9dc9SPeter Zijlstra 	 * Note: We clear the running state after enqueue_hrtimer and
1397b4d90e9fSPratyush Patel 	 * we do not reprogram the event hardware. Happens either in
13985cee9645SThomas Gleixner 	 * hrtimer_start_range_ns() or in hrtimer_interrupt()
13995de2755cSPeter Zijlstra 	 *
14005de2755cSPeter Zijlstra 	 * Note: Because we dropped the cpu_base->lock above,
14015de2755cSPeter Zijlstra 	 * hrtimer_start_range_ns() can have popped in and enqueued the timer
14025de2755cSPeter Zijlstra 	 * for us already.
14035cee9645SThomas Gleixner 	 */
14045de2755cSPeter Zijlstra 	if (restart != HRTIMER_NORESTART &&
14055de2755cSPeter Zijlstra 	    !(timer->state & HRTIMER_STATE_ENQUEUED))
140663e2ed36SAnna-Maria Gleixner 		enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS);
14075cee9645SThomas Gleixner 
14085cee9645SThomas Gleixner 	/*
1409887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1410887d9dc9SPeter Zijlstra 	 *
1411887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
14123f0b9e8eSAnna-Maria Gleixner 	 * hrtimer_active() cannot observe base->running.timer == NULL &&
1413887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
14145cee9645SThomas Gleixner 	 */
14153f0b9e8eSAnna-Maria Gleixner 	raw_write_seqcount_barrier(&base->seq);
14165cee9645SThomas Gleixner 
14173f0b9e8eSAnna-Maria Gleixner 	WARN_ON_ONCE(base->running != timer);
14183f0b9e8eSAnna-Maria Gleixner 	base->running = NULL;
14195cee9645SThomas Gleixner }
14205cee9645SThomas Gleixner 
1421dd934aa8SAnna-Maria Gleixner static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
1422c458b1d1SAnna-Maria Gleixner 				 unsigned long flags, unsigned int active_mask)
14235cee9645SThomas Gleixner {
1424c272ca58SAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1425c458b1d1SAnna-Maria Gleixner 	unsigned int active = cpu_base->active_bases & active_mask;
14265cee9645SThomas Gleixner 
1427c272ca58SAnna-Maria Gleixner 	for_each_active_base(base, cpu_base, active) {
14285cee9645SThomas Gleixner 		struct timerqueue_node *node;
14295cee9645SThomas Gleixner 		ktime_t basenow;
14305cee9645SThomas Gleixner 
14315cee9645SThomas Gleixner 		basenow = ktime_add(now, base->offset);
14325cee9645SThomas Gleixner 
14335cee9645SThomas Gleixner 		while ((node = timerqueue_getnext(&base->active))) {
14345cee9645SThomas Gleixner 			struct hrtimer *timer;
14355cee9645SThomas Gleixner 
14365cee9645SThomas Gleixner 			timer = container_of(node, struct hrtimer, node);
14375cee9645SThomas Gleixner 
14385cee9645SThomas Gleixner 			/*
14395cee9645SThomas Gleixner 			 * The immediate goal for using the softexpires is
14405cee9645SThomas Gleixner 			 * minimizing wakeups, not running timers at the
14415cee9645SThomas Gleixner 			 * earliest interrupt after their soft expiration.
14425cee9645SThomas Gleixner 			 * This allows us to avoid using a Priority Search
14435cee9645SThomas Gleixner 			 * Tree, which can answer a stabbing querry for
14445cee9645SThomas Gleixner 			 * overlapping intervals and instead use the simple
14455cee9645SThomas Gleixner 			 * BST we already have.
14465cee9645SThomas Gleixner 			 * We don't add extra wakeups by delaying timers that
14475cee9645SThomas Gleixner 			 * are right-of a not yet expired timer, because that
14485cee9645SThomas Gleixner 			 * timer will have to trigger a wakeup anyway.
14495cee9645SThomas Gleixner 			 */
14502456e855SThomas Gleixner 			if (basenow < hrtimer_get_softexpires_tv64(timer))
14515cee9645SThomas Gleixner 				break;
14525cee9645SThomas Gleixner 
1453dd934aa8SAnna-Maria Gleixner 			__run_hrtimer(cpu_base, base, timer, &basenow, flags);
14545cee9645SThomas Gleixner 		}
14555cee9645SThomas Gleixner 	}
145621d6d52aSThomas Gleixner }
145721d6d52aSThomas Gleixner 
14585da70160SAnna-Maria Gleixner static __latent_entropy void hrtimer_run_softirq(struct softirq_action *h)
14595da70160SAnna-Maria Gleixner {
14605da70160SAnna-Maria Gleixner 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
14615da70160SAnna-Maria Gleixner 	unsigned long flags;
14625da70160SAnna-Maria Gleixner 	ktime_t now;
14635da70160SAnna-Maria Gleixner 
14645da70160SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
14655da70160SAnna-Maria Gleixner 
14665da70160SAnna-Maria Gleixner 	now = hrtimer_update_base(cpu_base);
14675da70160SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_SOFT);
14685da70160SAnna-Maria Gleixner 
14695da70160SAnna-Maria Gleixner 	cpu_base->softirq_activated = 0;
14705da70160SAnna-Maria Gleixner 	hrtimer_update_softirq_timer(cpu_base, true);
14715da70160SAnna-Maria Gleixner 
14725da70160SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
14735da70160SAnna-Maria Gleixner }
14745da70160SAnna-Maria Gleixner 
147521d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
147621d6d52aSThomas Gleixner 
147721d6d52aSThomas Gleixner /*
147821d6d52aSThomas Gleixner  * High resolution timer interrupt
147921d6d52aSThomas Gleixner  * Called with interrupts disabled
148021d6d52aSThomas Gleixner  */
148121d6d52aSThomas Gleixner void hrtimer_interrupt(struct clock_event_device *dev)
148221d6d52aSThomas Gleixner {
148321d6d52aSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
148421d6d52aSThomas Gleixner 	ktime_t expires_next, now, entry_time, delta;
1485dd934aa8SAnna-Maria Gleixner 	unsigned long flags;
148621d6d52aSThomas Gleixner 	int retries = 0;
148721d6d52aSThomas Gleixner 
148821d6d52aSThomas Gleixner 	BUG_ON(!cpu_base->hres_active);
148921d6d52aSThomas Gleixner 	cpu_base->nr_events++;
14902456e855SThomas Gleixner 	dev->next_event = KTIME_MAX;
149121d6d52aSThomas Gleixner 
1492dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
149321d6d52aSThomas Gleixner 	entry_time = now = hrtimer_update_base(cpu_base);
149421d6d52aSThomas Gleixner retry:
149521d6d52aSThomas Gleixner 	cpu_base->in_hrtirq = 1;
149621d6d52aSThomas Gleixner 	/*
149721d6d52aSThomas Gleixner 	 * We set expires_next to KTIME_MAX here with cpu_base->lock
149821d6d52aSThomas Gleixner 	 * held to prevent that a timer is enqueued in our queue via
149921d6d52aSThomas Gleixner 	 * the migration code. This does not affect enqueueing of
150021d6d52aSThomas Gleixner 	 * timers which run their callback and need to be requeued on
150121d6d52aSThomas Gleixner 	 * this CPU.
150221d6d52aSThomas Gleixner 	 */
15032456e855SThomas Gleixner 	cpu_base->expires_next = KTIME_MAX;
150421d6d52aSThomas Gleixner 
15055da70160SAnna-Maria Gleixner 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
15065da70160SAnna-Maria Gleixner 		cpu_base->softirq_expires_next = KTIME_MAX;
15075da70160SAnna-Maria Gleixner 		cpu_base->softirq_activated = 1;
15085da70160SAnna-Maria Gleixner 		raise_softirq_irqoff(HRTIMER_SOFTIRQ);
15095da70160SAnna-Maria Gleixner 	}
15105da70160SAnna-Maria Gleixner 
1511c458b1d1SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
151221d6d52aSThomas Gleixner 
15139bc74919SThomas Gleixner 	/* Reevaluate the clock bases for the next expiry */
15145da70160SAnna-Maria Gleixner 	expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
15155cee9645SThomas Gleixner 	/*
15165cee9645SThomas Gleixner 	 * Store the new expiry value so the migration code can verify
15175cee9645SThomas Gleixner 	 * against it.
15185cee9645SThomas Gleixner 	 */
15195cee9645SThomas Gleixner 	cpu_base->expires_next = expires_next;
15209bc74919SThomas Gleixner 	cpu_base->in_hrtirq = 0;
1521dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
15225cee9645SThomas Gleixner 
15235cee9645SThomas Gleixner 	/* Reprogramming necessary ? */
1524d2540875SViresh Kumar 	if (!tick_program_event(expires_next, 0)) {
15255cee9645SThomas Gleixner 		cpu_base->hang_detected = 0;
15265cee9645SThomas Gleixner 		return;
15275cee9645SThomas Gleixner 	}
15285cee9645SThomas Gleixner 
15295cee9645SThomas Gleixner 	/*
15305cee9645SThomas Gleixner 	 * The next timer was already expired due to:
15315cee9645SThomas Gleixner 	 * - tracing
15325cee9645SThomas Gleixner 	 * - long lasting callbacks
15335cee9645SThomas Gleixner 	 * - being scheduled away when running in a VM
15345cee9645SThomas Gleixner 	 *
15355cee9645SThomas Gleixner 	 * We need to prevent that we loop forever in the hrtimer
15365cee9645SThomas Gleixner 	 * interrupt routine. We give it 3 attempts to avoid
15375cee9645SThomas Gleixner 	 * overreacting on some spurious event.
15385cee9645SThomas Gleixner 	 *
15395cee9645SThomas Gleixner 	 * Acquire base lock for updating the offsets and retrieving
15405cee9645SThomas Gleixner 	 * the current time.
15415cee9645SThomas Gleixner 	 */
1542dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
15435cee9645SThomas Gleixner 	now = hrtimer_update_base(cpu_base);
15445cee9645SThomas Gleixner 	cpu_base->nr_retries++;
15455cee9645SThomas Gleixner 	if (++retries < 3)
15465cee9645SThomas Gleixner 		goto retry;
15475cee9645SThomas Gleixner 	/*
15485cee9645SThomas Gleixner 	 * Give the system a chance to do something else than looping
15495cee9645SThomas Gleixner 	 * here. We stored the entry time, so we know exactly how long
15505cee9645SThomas Gleixner 	 * we spent here. We schedule the next event this amount of
15515cee9645SThomas Gleixner 	 * time away.
15525cee9645SThomas Gleixner 	 */
15535cee9645SThomas Gleixner 	cpu_base->nr_hangs++;
15545cee9645SThomas Gleixner 	cpu_base->hang_detected = 1;
1555dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1556dd934aa8SAnna-Maria Gleixner 
15575cee9645SThomas Gleixner 	delta = ktime_sub(now, entry_time);
15582456e855SThomas Gleixner 	if ((unsigned int)delta > cpu_base->max_hang_time)
15592456e855SThomas Gleixner 		cpu_base->max_hang_time = (unsigned int) delta;
15605cee9645SThomas Gleixner 	/*
15615cee9645SThomas Gleixner 	 * Limit it to a sensible value as we enforce a longer
15625cee9645SThomas Gleixner 	 * delay. Give the CPU at least 100ms to catch up.
15635cee9645SThomas Gleixner 	 */
15642456e855SThomas Gleixner 	if (delta > 100 * NSEC_PER_MSEC)
15655cee9645SThomas Gleixner 		expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
15665cee9645SThomas Gleixner 	else
15675cee9645SThomas Gleixner 		expires_next = ktime_add(now, delta);
15685cee9645SThomas Gleixner 	tick_program_event(expires_next, 1);
15697a6e5537SGeert Uytterhoeven 	pr_warn_once("hrtimer: interrupt took %llu ns\n", ktime_to_ns(delta));
15705cee9645SThomas Gleixner }
15715cee9645SThomas Gleixner 
1572016da201SStephen Boyd /* called with interrupts disabled */
1573c6eb3f70SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void)
15745cee9645SThomas Gleixner {
15755cee9645SThomas Gleixner 	struct tick_device *td;
15765cee9645SThomas Gleixner 
15775cee9645SThomas Gleixner 	if (!hrtimer_hres_active())
15785cee9645SThomas Gleixner 		return;
15795cee9645SThomas Gleixner 
158022127e93SChristoph Lameter 	td = this_cpu_ptr(&tick_cpu_device);
15815cee9645SThomas Gleixner 	if (td && td->evtdev)
15825cee9645SThomas Gleixner 		hrtimer_interrupt(td->evtdev);
15835cee9645SThomas Gleixner }
15845cee9645SThomas Gleixner 
15855cee9645SThomas Gleixner #else /* CONFIG_HIGH_RES_TIMERS */
15865cee9645SThomas Gleixner 
15875cee9645SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void) { }
15885cee9645SThomas Gleixner 
15895cee9645SThomas Gleixner #endif	/* !CONFIG_HIGH_RES_TIMERS */
15905cee9645SThomas Gleixner 
15915cee9645SThomas Gleixner /*
1592c6eb3f70SThomas Gleixner  * Called from run_local_timers in hardirq context every jiffy
15935cee9645SThomas Gleixner  */
15945cee9645SThomas Gleixner void hrtimer_run_queues(void)
15955cee9645SThomas Gleixner {
1596dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1597dd934aa8SAnna-Maria Gleixner 	unsigned long flags;
159821d6d52aSThomas Gleixner 	ktime_t now;
15995cee9645SThomas Gleixner 
1600e19ffe8bSThomas Gleixner 	if (__hrtimer_hres_active(cpu_base))
16015cee9645SThomas Gleixner 		return;
16025cee9645SThomas Gleixner 
1603c6eb3f70SThomas Gleixner 	/*
1604c6eb3f70SThomas Gleixner 	 * This _is_ ugly: We have to check periodically, whether we
1605c6eb3f70SThomas Gleixner 	 * can switch to highres and / or nohz mode. The clocksource
1606c6eb3f70SThomas Gleixner 	 * switch happens with xtime_lock held. Notification from
1607c6eb3f70SThomas Gleixner 	 * there only sets the check bit in the tick_oneshot code,
1608c6eb3f70SThomas Gleixner 	 * otherwise we might deadlock vs. xtime_lock.
1609c6eb3f70SThomas Gleixner 	 */
1610c6eb3f70SThomas Gleixner 	if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
1611c6eb3f70SThomas Gleixner 		hrtimer_switch_to_hres();
1612c6eb3f70SThomas Gleixner 		return;
16135cee9645SThomas Gleixner 	}
16145cee9645SThomas Gleixner 
1615dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
161621d6d52aSThomas Gleixner 	now = hrtimer_update_base(cpu_base);
16175da70160SAnna-Maria Gleixner 
16185da70160SAnna-Maria Gleixner 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
16195da70160SAnna-Maria Gleixner 		cpu_base->softirq_expires_next = KTIME_MAX;
16205da70160SAnna-Maria Gleixner 		cpu_base->softirq_activated = 1;
16215da70160SAnna-Maria Gleixner 		raise_softirq_irqoff(HRTIMER_SOFTIRQ);
16225da70160SAnna-Maria Gleixner 	}
16235da70160SAnna-Maria Gleixner 
1624c458b1d1SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
1625dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
16265cee9645SThomas Gleixner }
16275cee9645SThomas Gleixner 
16285cee9645SThomas Gleixner /*
16295cee9645SThomas Gleixner  * Sleep related functions:
16305cee9645SThomas Gleixner  */
16315cee9645SThomas Gleixner static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
16325cee9645SThomas Gleixner {
16335cee9645SThomas Gleixner 	struct hrtimer_sleeper *t =
16345cee9645SThomas Gleixner 		container_of(timer, struct hrtimer_sleeper, timer);
16355cee9645SThomas Gleixner 	struct task_struct *task = t->task;
16365cee9645SThomas Gleixner 
16375cee9645SThomas Gleixner 	t->task = NULL;
16385cee9645SThomas Gleixner 	if (task)
16395cee9645SThomas Gleixner 		wake_up_process(task);
16405cee9645SThomas Gleixner 
16415cee9645SThomas Gleixner 	return HRTIMER_NORESTART;
16425cee9645SThomas Gleixner }
16435cee9645SThomas Gleixner 
16445cee9645SThomas Gleixner void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
16455cee9645SThomas Gleixner {
16465cee9645SThomas Gleixner 	sl->timer.function = hrtimer_wakeup;
16475cee9645SThomas Gleixner 	sl->task = task;
16485cee9645SThomas Gleixner }
16495cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
16505cee9645SThomas Gleixner 
1651c0edd7c9SDeepa Dinamani int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
1652ce41aaf4SAl Viro {
1653ce41aaf4SAl Viro 	switch(restart->nanosleep.type) {
16540fe27955SArnd Bergmann #ifdef CONFIG_COMPAT_32BIT_TIME
1655ce41aaf4SAl Viro 	case TT_COMPAT:
16569afc5eeeSArnd Bergmann 		if (put_old_timespec32(ts, restart->nanosleep.compat_rmtp))
1657ce41aaf4SAl Viro 			return -EFAULT;
1658ce41aaf4SAl Viro 		break;
1659ce41aaf4SAl Viro #endif
1660ce41aaf4SAl Viro 	case TT_NATIVE:
1661c0edd7c9SDeepa Dinamani 		if (put_timespec64(ts, restart->nanosleep.rmtp))
1662ce41aaf4SAl Viro 			return -EFAULT;
1663ce41aaf4SAl Viro 		break;
1664ce41aaf4SAl Viro 	default:
1665ce41aaf4SAl Viro 		BUG();
1666ce41aaf4SAl Viro 	}
1667ce41aaf4SAl Viro 	return -ERESTART_RESTARTBLOCK;
1668ce41aaf4SAl Viro }
1669ce41aaf4SAl Viro 
16705cee9645SThomas Gleixner static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
16715cee9645SThomas Gleixner {
1672edbeda46SAl Viro 	struct restart_block *restart;
1673edbeda46SAl Viro 
16745cee9645SThomas Gleixner 	hrtimer_init_sleeper(t, current);
16755cee9645SThomas Gleixner 
16765cee9645SThomas Gleixner 	do {
16775cee9645SThomas Gleixner 		set_current_state(TASK_INTERRUPTIBLE);
16785cee9645SThomas Gleixner 		hrtimer_start_expires(&t->timer, mode);
16795cee9645SThomas Gleixner 
16805cee9645SThomas Gleixner 		if (likely(t->task))
16815cee9645SThomas Gleixner 			freezable_schedule();
16825cee9645SThomas Gleixner 
16835cee9645SThomas Gleixner 		hrtimer_cancel(&t->timer);
16845cee9645SThomas Gleixner 		mode = HRTIMER_MODE_ABS;
16855cee9645SThomas Gleixner 
16865cee9645SThomas Gleixner 	} while (t->task && !signal_pending(current));
16875cee9645SThomas Gleixner 
16885cee9645SThomas Gleixner 	__set_current_state(TASK_RUNNING);
16895cee9645SThomas Gleixner 
1690a7602681SAl Viro 	if (!t->task)
1691a7602681SAl Viro 		return 0;
16925cee9645SThomas Gleixner 
1693edbeda46SAl Viro 	restart = &current->restart_block;
1694edbeda46SAl Viro 	if (restart->nanosleep.type != TT_NONE) {
1695a7602681SAl Viro 		ktime_t rem = hrtimer_expires_remaining(&t->timer);
1696c0edd7c9SDeepa Dinamani 		struct timespec64 rmt;
1697edbeda46SAl Viro 
16982456e855SThomas Gleixner 		if (rem <= 0)
16995cee9645SThomas Gleixner 			return 0;
1700c0edd7c9SDeepa Dinamani 		rmt = ktime_to_timespec64(rem);
17015cee9645SThomas Gleixner 
1702ce41aaf4SAl Viro 		return nanosleep_copyout(restart, &rmt);
1703a7602681SAl Viro 	}
1704a7602681SAl Viro 	return -ERESTART_RESTARTBLOCK;
17055cee9645SThomas Gleixner }
17065cee9645SThomas Gleixner 
1707fb923c4aSAl Viro static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
17085cee9645SThomas Gleixner {
17095cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
1710a7602681SAl Viro 	int ret;
17115cee9645SThomas Gleixner 
17125cee9645SThomas Gleixner 	hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
17135cee9645SThomas Gleixner 				HRTIMER_MODE_ABS);
17145cee9645SThomas Gleixner 	hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
17155cee9645SThomas Gleixner 
1716a7602681SAl Viro 	ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
17175cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
17185cee9645SThomas Gleixner 	return ret;
17195cee9645SThomas Gleixner }
17205cee9645SThomas Gleixner 
1721938e7cf2SThomas Gleixner long hrtimer_nanosleep(const struct timespec64 *rqtp,
17225cee9645SThomas Gleixner 		       const enum hrtimer_mode mode, const clockid_t clockid)
17235cee9645SThomas Gleixner {
1724a7602681SAl Viro 	struct restart_block *restart;
17255cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
17265cee9645SThomas Gleixner 	int ret = 0;
1727da8b44d5SJohn Stultz 	u64 slack;
17285cee9645SThomas Gleixner 
17295cee9645SThomas Gleixner 	slack = current->timer_slack_ns;
17305cee9645SThomas Gleixner 	if (dl_task(current) || rt_task(current))
17315cee9645SThomas Gleixner 		slack = 0;
17325cee9645SThomas Gleixner 
17335cee9645SThomas Gleixner 	hrtimer_init_on_stack(&t.timer, clockid, mode);
1734ad196384SDeepa Dinamani 	hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
1735a7602681SAl Viro 	ret = do_nanosleep(&t, mode);
1736a7602681SAl Viro 	if (ret != -ERESTART_RESTARTBLOCK)
17375cee9645SThomas Gleixner 		goto out;
17385cee9645SThomas Gleixner 
17395cee9645SThomas Gleixner 	/* Absolute timers do not update the rmtp value and restart: */
17405cee9645SThomas Gleixner 	if (mode == HRTIMER_MODE_ABS) {
17415cee9645SThomas Gleixner 		ret = -ERESTARTNOHAND;
17425cee9645SThomas Gleixner 		goto out;
17435cee9645SThomas Gleixner 	}
17445cee9645SThomas Gleixner 
1745a7602681SAl Viro 	restart = &current->restart_block;
17465cee9645SThomas Gleixner 	restart->fn = hrtimer_nanosleep_restart;
17475cee9645SThomas Gleixner 	restart->nanosleep.clockid = t.timer.base->clockid;
17485cee9645SThomas Gleixner 	restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
17495cee9645SThomas Gleixner out:
17505cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
17515cee9645SThomas Gleixner 	return ret;
17525cee9645SThomas Gleixner }
17535cee9645SThomas Gleixner 
175401909974SDeepa Dinamani #if !defined(CONFIG_64BIT_TIME) || defined(CONFIG_64BIT)
175501909974SDeepa Dinamani 
175601909974SDeepa Dinamani SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp,
175701909974SDeepa Dinamani 		struct __kernel_timespec __user *, rmtp)
17585cee9645SThomas Gleixner {
1759c0edd7c9SDeepa Dinamani 	struct timespec64 tu;
17605cee9645SThomas Gleixner 
1761c0edd7c9SDeepa Dinamani 	if (get_timespec64(&tu, rqtp))
17625cee9645SThomas Gleixner 		return -EFAULT;
17635cee9645SThomas Gleixner 
1764c0edd7c9SDeepa Dinamani 	if (!timespec64_valid(&tu))
17655cee9645SThomas Gleixner 		return -EINVAL;
17665cee9645SThomas Gleixner 
1767edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1768192a82f9SAl Viro 	current->restart_block.nanosleep.rmtp = rmtp;
1769c0edd7c9SDeepa Dinamani 	return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
17705cee9645SThomas Gleixner }
17715cee9645SThomas Gleixner 
177201909974SDeepa Dinamani #endif
177301909974SDeepa Dinamani 
1774b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
1775edbeda46SAl Viro 
17769afc5eeeSArnd Bergmann COMPAT_SYSCALL_DEFINE2(nanosleep, struct old_timespec32 __user *, rqtp,
17779afc5eeeSArnd Bergmann 		       struct old_timespec32 __user *, rmtp)
1778edbeda46SAl Viro {
1779c0edd7c9SDeepa Dinamani 	struct timespec64 tu;
1780edbeda46SAl Viro 
17819afc5eeeSArnd Bergmann 	if (get_old_timespec32(&tu, rqtp))
1782edbeda46SAl Viro 		return -EFAULT;
1783edbeda46SAl Viro 
1784c0edd7c9SDeepa Dinamani 	if (!timespec64_valid(&tu))
1785edbeda46SAl Viro 		return -EINVAL;
1786edbeda46SAl Viro 
1787edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1788edbeda46SAl Viro 	current->restart_block.nanosleep.compat_rmtp = rmtp;
1789c0edd7c9SDeepa Dinamani 	return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1790edbeda46SAl Viro }
1791edbeda46SAl Viro #endif
1792edbeda46SAl Viro 
17935cee9645SThomas Gleixner /*
17945cee9645SThomas Gleixner  * Functions related to boot-time initialization:
17955cee9645SThomas Gleixner  */
179627590dc1SThomas Gleixner int hrtimers_prepare_cpu(unsigned int cpu)
17975cee9645SThomas Gleixner {
17985cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
17995cee9645SThomas Gleixner 	int i;
18005cee9645SThomas Gleixner 
18015cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
18025cee9645SThomas Gleixner 		cpu_base->clock_base[i].cpu_base = cpu_base;
18035cee9645SThomas Gleixner 		timerqueue_init_head(&cpu_base->clock_base[i].active);
18045cee9645SThomas Gleixner 	}
18055cee9645SThomas Gleixner 
1806cddd0248SViresh Kumar 	cpu_base->cpu = cpu;
1807303c146dSThomas Gleixner 	cpu_base->active_bases = 0;
180828bfd18bSAnna-Maria Gleixner 	cpu_base->hres_active = 0;
1809303c146dSThomas Gleixner 	cpu_base->hang_detected = 0;
1810303c146dSThomas Gleixner 	cpu_base->next_timer = NULL;
1811303c146dSThomas Gleixner 	cpu_base->softirq_next_timer = NULL;
181207a9a7eaSAnna-Maria Gleixner 	cpu_base->expires_next = KTIME_MAX;
18135da70160SAnna-Maria Gleixner 	cpu_base->softirq_expires_next = KTIME_MAX;
181427590dc1SThomas Gleixner 	return 0;
18155cee9645SThomas Gleixner }
18165cee9645SThomas Gleixner 
18175cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
18185cee9645SThomas Gleixner 
18195cee9645SThomas Gleixner static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
18205cee9645SThomas Gleixner 				struct hrtimer_clock_base *new_base)
18215cee9645SThomas Gleixner {
18225cee9645SThomas Gleixner 	struct hrtimer *timer;
18235cee9645SThomas Gleixner 	struct timerqueue_node *node;
18245cee9645SThomas Gleixner 
18255cee9645SThomas Gleixner 	while ((node = timerqueue_getnext(&old_base->active))) {
18265cee9645SThomas Gleixner 		timer = container_of(node, struct hrtimer, node);
18275cee9645SThomas Gleixner 		BUG_ON(hrtimer_callback_running(timer));
18285cee9645SThomas Gleixner 		debug_deactivate(timer);
18295cee9645SThomas Gleixner 
18305cee9645SThomas Gleixner 		/*
1831c04dca02SOleg Nesterov 		 * Mark it as ENQUEUED not INACTIVE otherwise the
18325cee9645SThomas Gleixner 		 * timer could be seen as !active and just vanish away
18335cee9645SThomas Gleixner 		 * under us on another CPU
18345cee9645SThomas Gleixner 		 */
1835c04dca02SOleg Nesterov 		__remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
18365cee9645SThomas Gleixner 		timer->base = new_base;
18375cee9645SThomas Gleixner 		/*
18385cee9645SThomas Gleixner 		 * Enqueue the timers on the new cpu. This does not
18395cee9645SThomas Gleixner 		 * reprogram the event device in case the timer
18405cee9645SThomas Gleixner 		 * expires before the earliest on this CPU, but we run
18415cee9645SThomas Gleixner 		 * hrtimer_interrupt after we migrated everything to
18425cee9645SThomas Gleixner 		 * sort out already expired timers and reprogram the
18435cee9645SThomas Gleixner 		 * event device.
18445cee9645SThomas Gleixner 		 */
184563e2ed36SAnna-Maria Gleixner 		enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS);
18465cee9645SThomas Gleixner 	}
18475cee9645SThomas Gleixner }
18485cee9645SThomas Gleixner 
184927590dc1SThomas Gleixner int hrtimers_dead_cpu(unsigned int scpu)
18505cee9645SThomas Gleixner {
18515cee9645SThomas Gleixner 	struct hrtimer_cpu_base *old_base, *new_base;
18525cee9645SThomas Gleixner 	int i;
18535cee9645SThomas Gleixner 
18545cee9645SThomas Gleixner 	BUG_ON(cpu_online(scpu));
18555cee9645SThomas Gleixner 	tick_cancel_sched_timer(scpu);
18565cee9645SThomas Gleixner 
18575da70160SAnna-Maria Gleixner 	/*
18585da70160SAnna-Maria Gleixner 	 * this BH disable ensures that raise_softirq_irqoff() does
18595da70160SAnna-Maria Gleixner 	 * not wakeup ksoftirqd (and acquire the pi-lock) while
18605da70160SAnna-Maria Gleixner 	 * holding the cpu_base lock
18615da70160SAnna-Maria Gleixner 	 */
18625da70160SAnna-Maria Gleixner 	local_bh_disable();
18635cee9645SThomas Gleixner 	local_irq_disable();
18645cee9645SThomas Gleixner 	old_base = &per_cpu(hrtimer_bases, scpu);
1865dc5df73bSChristoph Lameter 	new_base = this_cpu_ptr(&hrtimer_bases);
18665cee9645SThomas Gleixner 	/*
18675cee9645SThomas Gleixner 	 * The caller is globally serialized and nobody else
18685cee9645SThomas Gleixner 	 * takes two locks at once, deadlock is not possible.
18695cee9645SThomas Gleixner 	 */
18705cee9645SThomas Gleixner 	raw_spin_lock(&new_base->lock);
18715cee9645SThomas Gleixner 	raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
18725cee9645SThomas Gleixner 
18735cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
18745cee9645SThomas Gleixner 		migrate_hrtimer_list(&old_base->clock_base[i],
18755cee9645SThomas Gleixner 				     &new_base->clock_base[i]);
18765cee9645SThomas Gleixner 	}
18775cee9645SThomas Gleixner 
18785da70160SAnna-Maria Gleixner 	/*
18795da70160SAnna-Maria Gleixner 	 * The migration might have changed the first expiring softirq
18805da70160SAnna-Maria Gleixner 	 * timer on this CPU. Update it.
18815da70160SAnna-Maria Gleixner 	 */
18825da70160SAnna-Maria Gleixner 	hrtimer_update_softirq_timer(new_base, false);
18835da70160SAnna-Maria Gleixner 
18845cee9645SThomas Gleixner 	raw_spin_unlock(&old_base->lock);
18855cee9645SThomas Gleixner 	raw_spin_unlock(&new_base->lock);
18865cee9645SThomas Gleixner 
18875cee9645SThomas Gleixner 	/* Check, if we got expired work to do */
18885cee9645SThomas Gleixner 	__hrtimer_peek_ahead_timers();
18895cee9645SThomas Gleixner 	local_irq_enable();
18905da70160SAnna-Maria Gleixner 	local_bh_enable();
189127590dc1SThomas Gleixner 	return 0;
18925cee9645SThomas Gleixner }
18935cee9645SThomas Gleixner 
18945cee9645SThomas Gleixner #endif /* CONFIG_HOTPLUG_CPU */
18955cee9645SThomas Gleixner 
18965cee9645SThomas Gleixner void __init hrtimers_init(void)
18975cee9645SThomas Gleixner {
189827590dc1SThomas Gleixner 	hrtimers_prepare_cpu(smp_processor_id());
18995da70160SAnna-Maria Gleixner 	open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
19005cee9645SThomas Gleixner }
19015cee9645SThomas Gleixner 
19025cee9645SThomas Gleixner /**
19035cee9645SThomas Gleixner  * schedule_hrtimeout_range_clock - sleep until timeout
19045cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
19055cee9645SThomas Gleixner  * @delta:	slack in expires timeout (ktime_t)
190690777713SAnna-Maria Gleixner  * @mode:	timer mode
190790777713SAnna-Maria Gleixner  * @clock_id:	timer clock to be used
19085cee9645SThomas Gleixner  */
19095cee9645SThomas Gleixner int __sched
1910da8b44d5SJohn Stultz schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
191190777713SAnna-Maria Gleixner 			       const enum hrtimer_mode mode, clockid_t clock_id)
19125cee9645SThomas Gleixner {
19135cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
19145cee9645SThomas Gleixner 
19155cee9645SThomas Gleixner 	/*
19165cee9645SThomas Gleixner 	 * Optimize when a zero timeout value is given. It does not
19175cee9645SThomas Gleixner 	 * matter whether this is an absolute or a relative time.
19185cee9645SThomas Gleixner 	 */
19192456e855SThomas Gleixner 	if (expires && *expires == 0) {
19205cee9645SThomas Gleixner 		__set_current_state(TASK_RUNNING);
19215cee9645SThomas Gleixner 		return 0;
19225cee9645SThomas Gleixner 	}
19235cee9645SThomas Gleixner 
19245cee9645SThomas Gleixner 	/*
19255cee9645SThomas Gleixner 	 * A NULL parameter means "infinite"
19265cee9645SThomas Gleixner 	 */
19275cee9645SThomas Gleixner 	if (!expires) {
19285cee9645SThomas Gleixner 		schedule();
19295cee9645SThomas Gleixner 		return -EINTR;
19305cee9645SThomas Gleixner 	}
19315cee9645SThomas Gleixner 
193290777713SAnna-Maria Gleixner 	hrtimer_init_on_stack(&t.timer, clock_id, mode);
19335cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
19345cee9645SThomas Gleixner 
19355cee9645SThomas Gleixner 	hrtimer_init_sleeper(&t, current);
19365cee9645SThomas Gleixner 
19375cee9645SThomas Gleixner 	hrtimer_start_expires(&t.timer, mode);
19385cee9645SThomas Gleixner 
19395cee9645SThomas Gleixner 	if (likely(t.task))
19405cee9645SThomas Gleixner 		schedule();
19415cee9645SThomas Gleixner 
19425cee9645SThomas Gleixner 	hrtimer_cancel(&t.timer);
19435cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
19445cee9645SThomas Gleixner 
19455cee9645SThomas Gleixner 	__set_current_state(TASK_RUNNING);
19465cee9645SThomas Gleixner 
19475cee9645SThomas Gleixner 	return !t.task ? 0 : -EINTR;
19485cee9645SThomas Gleixner }
19495cee9645SThomas Gleixner 
19505cee9645SThomas Gleixner /**
19515cee9645SThomas Gleixner  * schedule_hrtimeout_range - sleep until timeout
19525cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
19535cee9645SThomas Gleixner  * @delta:	slack in expires timeout (ktime_t)
195490777713SAnna-Maria Gleixner  * @mode:	timer mode
19555cee9645SThomas Gleixner  *
19565cee9645SThomas Gleixner  * Make the current task sleep until the given expiry time has
19575cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
19585cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
19595cee9645SThomas Gleixner  *
19605cee9645SThomas Gleixner  * The @delta argument gives the kernel the freedom to schedule the
19615cee9645SThomas Gleixner  * actual wakeup to a time that is both power and performance friendly.
19625cee9645SThomas Gleixner  * The kernel give the normal best effort behavior for "@expires+@delta",
19635cee9645SThomas Gleixner  * but may decide to fire the timer earlier, but no earlier than @expires.
19645cee9645SThomas Gleixner  *
19655cee9645SThomas Gleixner  * You can set the task state as follows -
19665cee9645SThomas Gleixner  *
19675cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
19684b7e9cf9SDouglas Anderson  * pass before the routine returns unless the current task is explicitly
19694b7e9cf9SDouglas Anderson  * woken up, (e.g. by wake_up_process()).
19705cee9645SThomas Gleixner  *
19715cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
19724b7e9cf9SDouglas Anderson  * delivered to the current task or the current task is explicitly woken
19734b7e9cf9SDouglas Anderson  * up.
19745cee9645SThomas Gleixner  *
19755cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
19765cee9645SThomas Gleixner  * routine returns.
19775cee9645SThomas Gleixner  *
19784b7e9cf9SDouglas Anderson  * Returns 0 when the timer has expired. If the task was woken before the
19794b7e9cf9SDouglas Anderson  * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
19804b7e9cf9SDouglas Anderson  * by an explicit wakeup, it returns -EINTR.
19815cee9645SThomas Gleixner  */
1982da8b44d5SJohn Stultz int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
19835cee9645SThomas Gleixner 				     const enum hrtimer_mode mode)
19845cee9645SThomas Gleixner {
19855cee9645SThomas Gleixner 	return schedule_hrtimeout_range_clock(expires, delta, mode,
19865cee9645SThomas Gleixner 					      CLOCK_MONOTONIC);
19875cee9645SThomas Gleixner }
19885cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
19895cee9645SThomas Gleixner 
19905cee9645SThomas Gleixner /**
19915cee9645SThomas Gleixner  * schedule_hrtimeout - sleep until timeout
19925cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
199390777713SAnna-Maria Gleixner  * @mode:	timer mode
19945cee9645SThomas Gleixner  *
19955cee9645SThomas Gleixner  * Make the current task sleep until the given expiry time has
19965cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
19975cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
19985cee9645SThomas Gleixner  *
19995cee9645SThomas Gleixner  * You can set the task state as follows -
20005cee9645SThomas Gleixner  *
20015cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
20024b7e9cf9SDouglas Anderson  * pass before the routine returns unless the current task is explicitly
20034b7e9cf9SDouglas Anderson  * woken up, (e.g. by wake_up_process()).
20045cee9645SThomas Gleixner  *
20055cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
20064b7e9cf9SDouglas Anderson  * delivered to the current task or the current task is explicitly woken
20074b7e9cf9SDouglas Anderson  * up.
20085cee9645SThomas Gleixner  *
20095cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
20105cee9645SThomas Gleixner  * routine returns.
20115cee9645SThomas Gleixner  *
20124b7e9cf9SDouglas Anderson  * Returns 0 when the timer has expired. If the task was woken before the
20134b7e9cf9SDouglas Anderson  * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
20144b7e9cf9SDouglas Anderson  * by an explicit wakeup, it returns -EINTR.
20155cee9645SThomas Gleixner  */
20165cee9645SThomas Gleixner int __sched schedule_hrtimeout(ktime_t *expires,
20175cee9645SThomas Gleixner 			       const enum hrtimer_mode mode)
20185cee9645SThomas Gleixner {
20195cee9645SThomas Gleixner 	return schedule_hrtimeout_range(expires, 0, mode);
20205cee9645SThomas Gleixner }
20215cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout);
2022