xref: /openbmc/linux/kernel/time/hrtimer.c (revision 0bee3b601b77dbe7981b5474ae8758d6bf60177a)
135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
25cee9645SThomas Gleixner /*
35cee9645SThomas Gleixner  *  Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
45cee9645SThomas Gleixner  *  Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
55cee9645SThomas Gleixner  *  Copyright(C) 2006-2007  Timesys Corp., Thomas Gleixner
65cee9645SThomas Gleixner  *
75cee9645SThomas Gleixner  *  High-resolution kernel timers
85cee9645SThomas Gleixner  *
958c5fc2bSThomas Gleixner  *  In contrast to the low-resolution timeout API, aka timer wheel,
1058c5fc2bSThomas Gleixner  *  hrtimers provide finer resolution and accuracy depending on system
1158c5fc2bSThomas Gleixner  *  configuration and capabilities.
125cee9645SThomas Gleixner  *
135cee9645SThomas Gleixner  *  Started by: Thomas Gleixner and Ingo Molnar
145cee9645SThomas Gleixner  *
155cee9645SThomas Gleixner  *  Credits:
1658c5fc2bSThomas Gleixner  *	Based on the original timer wheel code
175cee9645SThomas Gleixner  *
185cee9645SThomas Gleixner  *	Help, testing, suggestions, bugfixes, improvements were
195cee9645SThomas Gleixner  *	provided by:
205cee9645SThomas Gleixner  *
215cee9645SThomas Gleixner  *	George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
225cee9645SThomas Gleixner  *	et. al.
235cee9645SThomas Gleixner  */
245cee9645SThomas Gleixner 
255cee9645SThomas Gleixner #include <linux/cpu.h>
265cee9645SThomas Gleixner #include <linux/export.h>
275cee9645SThomas Gleixner #include <linux/percpu.h>
285cee9645SThomas Gleixner #include <linux/hrtimer.h>
295cee9645SThomas Gleixner #include <linux/notifier.h>
305cee9645SThomas Gleixner #include <linux/syscalls.h>
315cee9645SThomas Gleixner #include <linux/interrupt.h>
325cee9645SThomas Gleixner #include <linux/tick.h>
335cee9645SThomas Gleixner #include <linux/err.h>
345cee9645SThomas Gleixner #include <linux/debugobjects.h>
35174cd4b1SIngo Molnar #include <linux/sched/signal.h>
365cee9645SThomas Gleixner #include <linux/sched/sysctl.h>
375cee9645SThomas Gleixner #include <linux/sched/rt.h>
385cee9645SThomas Gleixner #include <linux/sched/deadline.h>
39370c9135SIngo Molnar #include <linux/sched/nohz.h>
40b17b0153SIngo Molnar #include <linux/sched/debug.h>
415cee9645SThomas Gleixner #include <linux/timer.h>
425cee9645SThomas Gleixner #include <linux/freezer.h>
43edbeda46SAl Viro #include <linux/compat.h>
445cee9645SThomas Gleixner 
457c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
465cee9645SThomas Gleixner 
475cee9645SThomas Gleixner #include <trace/events/timer.h>
485cee9645SThomas Gleixner 
49c1797bafSThomas Gleixner #include "tick-internal.h"
508b094cd0SThomas Gleixner 
515cee9645SThomas Gleixner /*
52c458b1d1SAnna-Maria Gleixner  * Masks for selecting the soft and hard context timers from
53c458b1d1SAnna-Maria Gleixner  * cpu_base->active
54c458b1d1SAnna-Maria Gleixner  */
55c458b1d1SAnna-Maria Gleixner #define MASK_SHIFT		(HRTIMER_BASE_MONOTONIC_SOFT)
56c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_HARD	((1U << MASK_SHIFT) - 1)
57c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_SOFT	(HRTIMER_ACTIVE_HARD << MASK_SHIFT)
58c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_ALL	(HRTIMER_ACTIVE_SOFT | HRTIMER_ACTIVE_HARD)
59c458b1d1SAnna-Maria Gleixner 
60c458b1d1SAnna-Maria Gleixner /*
615cee9645SThomas Gleixner  * The timer bases:
625cee9645SThomas Gleixner  *
63571af55aSZhen Lei  * There are more clockids than hrtimer bases. Thus, we index
645cee9645SThomas Gleixner  * into the timer bases by the hrtimer_base_type enum. When trying
655cee9645SThomas Gleixner  * to reach a base using a clockid, hrtimer_clockid_to_base()
665cee9645SThomas Gleixner  * is used to convert from clockid to the proper hrtimer_base_type.
675cee9645SThomas Gleixner  */
685cee9645SThomas Gleixner DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
695cee9645SThomas Gleixner {
705cee9645SThomas Gleixner 	.lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
715cee9645SThomas Gleixner 	.clock_base =
725cee9645SThomas Gleixner 	{
735cee9645SThomas Gleixner 		{
745cee9645SThomas Gleixner 			.index = HRTIMER_BASE_MONOTONIC,
755cee9645SThomas Gleixner 			.clockid = CLOCK_MONOTONIC,
765cee9645SThomas Gleixner 			.get_time = &ktime_get,
775cee9645SThomas Gleixner 		},
785cee9645SThomas Gleixner 		{
795cee9645SThomas Gleixner 			.index = HRTIMER_BASE_REALTIME,
805cee9645SThomas Gleixner 			.clockid = CLOCK_REALTIME,
815cee9645SThomas Gleixner 			.get_time = &ktime_get_real,
825cee9645SThomas Gleixner 		},
835cee9645SThomas Gleixner 		{
84a3ed0e43SThomas Gleixner 			.index = HRTIMER_BASE_BOOTTIME,
85a3ed0e43SThomas Gleixner 			.clockid = CLOCK_BOOTTIME,
86a3ed0e43SThomas Gleixner 			.get_time = &ktime_get_boottime,
87a3ed0e43SThomas Gleixner 		},
88a3ed0e43SThomas Gleixner 		{
895cee9645SThomas Gleixner 			.index = HRTIMER_BASE_TAI,
905cee9645SThomas Gleixner 			.clockid = CLOCK_TAI,
915cee9645SThomas Gleixner 			.get_time = &ktime_get_clocktai,
925cee9645SThomas Gleixner 		},
9398ecadd4SAnna-Maria Gleixner 		{
9498ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_MONOTONIC_SOFT,
9598ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_MONOTONIC,
9698ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get,
9798ecadd4SAnna-Maria Gleixner 		},
9898ecadd4SAnna-Maria Gleixner 		{
9998ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_REALTIME_SOFT,
10098ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_REALTIME,
10198ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get_real,
10298ecadd4SAnna-Maria Gleixner 		},
10398ecadd4SAnna-Maria Gleixner 		{
104a3ed0e43SThomas Gleixner 			.index = HRTIMER_BASE_BOOTTIME_SOFT,
105a3ed0e43SThomas Gleixner 			.clockid = CLOCK_BOOTTIME,
106a3ed0e43SThomas Gleixner 			.get_time = &ktime_get_boottime,
107a3ed0e43SThomas Gleixner 		},
108a3ed0e43SThomas Gleixner 		{
10998ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_TAI_SOFT,
11098ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_TAI,
11198ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get_clocktai,
11298ecadd4SAnna-Maria Gleixner 		},
1135cee9645SThomas Gleixner 	}
1145cee9645SThomas Gleixner };
1155cee9645SThomas Gleixner 
1165cee9645SThomas Gleixner static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
117336a9cdeSMarc Zyngier 	/* Make sure we catch unsupported clockids */
118336a9cdeSMarc Zyngier 	[0 ... MAX_CLOCKS - 1]	= HRTIMER_MAX_CLOCK_BASES,
119336a9cdeSMarc Zyngier 
1205cee9645SThomas Gleixner 	[CLOCK_REALTIME]	= HRTIMER_BASE_REALTIME,
1215cee9645SThomas Gleixner 	[CLOCK_MONOTONIC]	= HRTIMER_BASE_MONOTONIC,
122a3ed0e43SThomas Gleixner 	[CLOCK_BOOTTIME]	= HRTIMER_BASE_BOOTTIME,
1235cee9645SThomas Gleixner 	[CLOCK_TAI]		= HRTIMER_BASE_TAI,
1245cee9645SThomas Gleixner };
1255cee9645SThomas Gleixner 
1265cee9645SThomas Gleixner /*
1275cee9645SThomas Gleixner  * Functions and macros which are different for UP/SMP systems are kept in a
1285cee9645SThomas Gleixner  * single place
1295cee9645SThomas Gleixner  */
1305cee9645SThomas Gleixner #ifdef CONFIG_SMP
1315cee9645SThomas Gleixner 
1325cee9645SThomas Gleixner /*
133887d9dc9SPeter Zijlstra  * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
134887d9dc9SPeter Zijlstra  * such that hrtimer_callback_running() can unconditionally dereference
135887d9dc9SPeter Zijlstra  * timer->base->cpu_base
136887d9dc9SPeter Zijlstra  */
137887d9dc9SPeter Zijlstra static struct hrtimer_cpu_base migration_cpu_base = {
138887d9dc9SPeter Zijlstra 	.clock_base = { { .cpu_base = &migration_cpu_base, }, },
139887d9dc9SPeter Zijlstra };
140887d9dc9SPeter Zijlstra 
141887d9dc9SPeter Zijlstra #define migration_base	migration_cpu_base.clock_base[0]
142887d9dc9SPeter Zijlstra 
143887d9dc9SPeter Zijlstra /*
1445cee9645SThomas Gleixner  * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
1455cee9645SThomas Gleixner  * means that all timers which are tied to this base via timer->base are
1465cee9645SThomas Gleixner  * locked, and the base itself is locked too.
1475cee9645SThomas Gleixner  *
1485cee9645SThomas Gleixner  * So __run_timers/migrate_timers can safely modify all timers which could
1495cee9645SThomas Gleixner  * be found on the lists/queues.
1505cee9645SThomas Gleixner  *
1515cee9645SThomas Gleixner  * When the timer's base is locked, and the timer removed from list, it is
152887d9dc9SPeter Zijlstra  * possible to set timer->base = &migration_base and drop the lock: the timer
153887d9dc9SPeter Zijlstra  * remains locked.
1545cee9645SThomas Gleixner  */
1555cee9645SThomas Gleixner static
1565cee9645SThomas Gleixner struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
1575cee9645SThomas Gleixner 					     unsigned long *flags)
1585cee9645SThomas Gleixner {
1595cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
1605cee9645SThomas Gleixner 
1615cee9645SThomas Gleixner 	for (;;) {
1625cee9645SThomas Gleixner 		base = timer->base;
163887d9dc9SPeter Zijlstra 		if (likely(base != &migration_base)) {
1645cee9645SThomas Gleixner 			raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
1655cee9645SThomas Gleixner 			if (likely(base == timer->base))
1665cee9645SThomas Gleixner 				return base;
1675cee9645SThomas Gleixner 			/* The timer has migrated to another CPU: */
1685cee9645SThomas Gleixner 			raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
1695cee9645SThomas Gleixner 		}
1705cee9645SThomas Gleixner 		cpu_relax();
1715cee9645SThomas Gleixner 	}
1725cee9645SThomas Gleixner }
1735cee9645SThomas Gleixner 
1745cee9645SThomas Gleixner /*
17507a9a7eaSAnna-Maria Gleixner  * We do not migrate the timer when it is expiring before the next
17607a9a7eaSAnna-Maria Gleixner  * event on the target cpu. When high resolution is enabled, we cannot
17707a9a7eaSAnna-Maria Gleixner  * reprogram the target cpu hardware and we would cause it to fire
17807a9a7eaSAnna-Maria Gleixner  * late. To keep it simple, we handle the high resolution enabled and
17907a9a7eaSAnna-Maria Gleixner  * disabled case similar.
1805cee9645SThomas Gleixner  *
1815cee9645SThomas Gleixner  * Called with cpu_base->lock of target cpu held.
1825cee9645SThomas Gleixner  */
1835cee9645SThomas Gleixner static int
1845cee9645SThomas Gleixner hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
1855cee9645SThomas Gleixner {
1865cee9645SThomas Gleixner 	ktime_t expires;
1875cee9645SThomas Gleixner 
1885cee9645SThomas Gleixner 	expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
1892ac2dcccSAnna-Maria Gleixner 	return expires < new_base->cpu_base->expires_next;
1905cee9645SThomas Gleixner }
1915cee9645SThomas Gleixner 
192bc7a34b8SThomas Gleixner static inline
193bc7a34b8SThomas Gleixner struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base,
194bc7a34b8SThomas Gleixner 					 int pinned)
195bc7a34b8SThomas Gleixner {
196ae67badaSThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
197ae67badaSThomas Gleixner 	if (static_branch_likely(&timers_migration_enabled) && !pinned)
198bc7a34b8SThomas Gleixner 		return &per_cpu(hrtimer_bases, get_nohz_timer_target());
199ae67badaSThomas Gleixner #endif
200662b3e19SFrederic Weisbecker 	return base;
201bc7a34b8SThomas Gleixner }
202bc7a34b8SThomas Gleixner 
2035cee9645SThomas Gleixner /*
204b48362d8SFrederic Weisbecker  * We switch the timer base to a power-optimized selected CPU target,
205b48362d8SFrederic Weisbecker  * if:
206b48362d8SFrederic Weisbecker  *	- NO_HZ_COMMON is enabled
207b48362d8SFrederic Weisbecker  *	- timer migration is enabled
208b48362d8SFrederic Weisbecker  *	- the timer callback is not running
209b48362d8SFrederic Weisbecker  *	- the timer is not the first expiring timer on the new target
210b48362d8SFrederic Weisbecker  *
211b48362d8SFrederic Weisbecker  * If one of the above requirements is not fulfilled we move the timer
212b48362d8SFrederic Weisbecker  * to the current CPU or leave it on the previously assigned CPU if
213b48362d8SFrederic Weisbecker  * the timer callback is currently running.
2145cee9645SThomas Gleixner  */
2155cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
2165cee9645SThomas Gleixner switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
2175cee9645SThomas Gleixner 		    int pinned)
2185cee9645SThomas Gleixner {
219b48362d8SFrederic Weisbecker 	struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
2205cee9645SThomas Gleixner 	struct hrtimer_clock_base *new_base;
2215cee9645SThomas Gleixner 	int basenum = base->index;
2225cee9645SThomas Gleixner 
223b48362d8SFrederic Weisbecker 	this_cpu_base = this_cpu_ptr(&hrtimer_bases);
224b48362d8SFrederic Weisbecker 	new_cpu_base = get_target_base(this_cpu_base, pinned);
2255cee9645SThomas Gleixner again:
2265cee9645SThomas Gleixner 	new_base = &new_cpu_base->clock_base[basenum];
2275cee9645SThomas Gleixner 
2285cee9645SThomas Gleixner 	if (base != new_base) {
2295cee9645SThomas Gleixner 		/*
2305cee9645SThomas Gleixner 		 * We are trying to move timer to new_base.
2315cee9645SThomas Gleixner 		 * However we can't change timer's base while it is running,
2325cee9645SThomas Gleixner 		 * so we keep it on the same CPU. No hassle vs. reprogramming
2335cee9645SThomas Gleixner 		 * the event source in the high resolution case. The softirq
2345cee9645SThomas Gleixner 		 * code will take care of this when the timer function has
2355cee9645SThomas Gleixner 		 * completed. There is no conflict as we hold the lock until
2365cee9645SThomas Gleixner 		 * the timer is enqueued.
2375cee9645SThomas Gleixner 		 */
2385cee9645SThomas Gleixner 		if (unlikely(hrtimer_callback_running(timer)))
2395cee9645SThomas Gleixner 			return base;
2405cee9645SThomas Gleixner 
241887d9dc9SPeter Zijlstra 		/* See the comment in lock_hrtimer_base() */
242887d9dc9SPeter Zijlstra 		timer->base = &migration_base;
2435cee9645SThomas Gleixner 		raw_spin_unlock(&base->cpu_base->lock);
2445cee9645SThomas Gleixner 		raw_spin_lock(&new_base->cpu_base->lock);
2455cee9645SThomas Gleixner 
246b48362d8SFrederic Weisbecker 		if (new_cpu_base != this_cpu_base &&
247bc7a34b8SThomas Gleixner 		    hrtimer_check_target(timer, new_base)) {
2485cee9645SThomas Gleixner 			raw_spin_unlock(&new_base->cpu_base->lock);
2495cee9645SThomas Gleixner 			raw_spin_lock(&base->cpu_base->lock);
250b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
2515cee9645SThomas Gleixner 			timer->base = base;
2525cee9645SThomas Gleixner 			goto again;
2535cee9645SThomas Gleixner 		}
2545cee9645SThomas Gleixner 		timer->base = new_base;
2555cee9645SThomas Gleixner 	} else {
256b48362d8SFrederic Weisbecker 		if (new_cpu_base != this_cpu_base &&
257bc7a34b8SThomas Gleixner 		    hrtimer_check_target(timer, new_base)) {
258b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
2595cee9645SThomas Gleixner 			goto again;
2605cee9645SThomas Gleixner 		}
2615cee9645SThomas Gleixner 	}
2625cee9645SThomas Gleixner 	return new_base;
2635cee9645SThomas Gleixner }
2645cee9645SThomas Gleixner 
2655cee9645SThomas Gleixner #else /* CONFIG_SMP */
2665cee9645SThomas Gleixner 
2675cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
2685cee9645SThomas Gleixner lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
2695cee9645SThomas Gleixner {
2705cee9645SThomas Gleixner 	struct hrtimer_clock_base *base = timer->base;
2715cee9645SThomas Gleixner 
2725cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
2735cee9645SThomas Gleixner 
2745cee9645SThomas Gleixner 	return base;
2755cee9645SThomas Gleixner }
2765cee9645SThomas Gleixner 
2775cee9645SThomas Gleixner # define switch_hrtimer_base(t, b, p)	(b)
2785cee9645SThomas Gleixner 
2795cee9645SThomas Gleixner #endif	/* !CONFIG_SMP */
2805cee9645SThomas Gleixner 
2815cee9645SThomas Gleixner /*
2825cee9645SThomas Gleixner  * Functions for the union type storage format of ktime_t which are
2835cee9645SThomas Gleixner  * too large for inlining:
2845cee9645SThomas Gleixner  */
2855cee9645SThomas Gleixner #if BITS_PER_LONG < 64
2865cee9645SThomas Gleixner /*
2875cee9645SThomas Gleixner  * Divide a ktime value by a nanosecond value
2885cee9645SThomas Gleixner  */
289f7bcb70eSJohn Stultz s64 __ktime_divns(const ktime_t kt, s64 div)
2905cee9645SThomas Gleixner {
2915cee9645SThomas Gleixner 	int sft = 0;
292f7bcb70eSJohn Stultz 	s64 dclc;
293f7bcb70eSJohn Stultz 	u64 tmp;
2945cee9645SThomas Gleixner 
2955cee9645SThomas Gleixner 	dclc = ktime_to_ns(kt);
296f7bcb70eSJohn Stultz 	tmp = dclc < 0 ? -dclc : dclc;
297f7bcb70eSJohn Stultz 
2985cee9645SThomas Gleixner 	/* Make sure the divisor is less than 2^32: */
2995cee9645SThomas Gleixner 	while (div >> 32) {
3005cee9645SThomas Gleixner 		sft++;
3015cee9645SThomas Gleixner 		div >>= 1;
3025cee9645SThomas Gleixner 	}
303f7bcb70eSJohn Stultz 	tmp >>= sft;
304f7bcb70eSJohn Stultz 	do_div(tmp, (unsigned long) div);
305f7bcb70eSJohn Stultz 	return dclc < 0 ? -tmp : tmp;
3065cee9645SThomas Gleixner }
3078b618628SNicolas Pitre EXPORT_SYMBOL_GPL(__ktime_divns);
3085cee9645SThomas Gleixner #endif /* BITS_PER_LONG >= 64 */
3095cee9645SThomas Gleixner 
3105cee9645SThomas Gleixner /*
3115cee9645SThomas Gleixner  * Add two ktime values and do a safety check for overflow:
3125cee9645SThomas Gleixner  */
3135cee9645SThomas Gleixner ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
3145cee9645SThomas Gleixner {
315979515c5SVegard Nossum 	ktime_t res = ktime_add_unsafe(lhs, rhs);
3165cee9645SThomas Gleixner 
3175cee9645SThomas Gleixner 	/*
3185cee9645SThomas Gleixner 	 * We use KTIME_SEC_MAX here, the maximum timeout which we can
3195cee9645SThomas Gleixner 	 * return to user space in a timespec:
3205cee9645SThomas Gleixner 	 */
3212456e855SThomas Gleixner 	if (res < 0 || res < lhs || res < rhs)
3225cee9645SThomas Gleixner 		res = ktime_set(KTIME_SEC_MAX, 0);
3235cee9645SThomas Gleixner 
3245cee9645SThomas Gleixner 	return res;
3255cee9645SThomas Gleixner }
3265cee9645SThomas Gleixner 
3275cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_add_safe);
3285cee9645SThomas Gleixner 
3295cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
3305cee9645SThomas Gleixner 
3315cee9645SThomas Gleixner static struct debug_obj_descr hrtimer_debug_descr;
3325cee9645SThomas Gleixner 
3335cee9645SThomas Gleixner static void *hrtimer_debug_hint(void *addr)
3345cee9645SThomas Gleixner {
3355cee9645SThomas Gleixner 	return ((struct hrtimer *) addr)->function;
3365cee9645SThomas Gleixner }
3375cee9645SThomas Gleixner 
3385cee9645SThomas Gleixner /*
3395cee9645SThomas Gleixner  * fixup_init is called when:
3405cee9645SThomas Gleixner  * - an active object is initialized
3415cee9645SThomas Gleixner  */
342e3252464SDu, Changbin static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state)
3435cee9645SThomas Gleixner {
3445cee9645SThomas Gleixner 	struct hrtimer *timer = addr;
3455cee9645SThomas Gleixner 
3465cee9645SThomas Gleixner 	switch (state) {
3475cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3485cee9645SThomas Gleixner 		hrtimer_cancel(timer);
3495cee9645SThomas Gleixner 		debug_object_init(timer, &hrtimer_debug_descr);
350e3252464SDu, Changbin 		return true;
3515cee9645SThomas Gleixner 	default:
352e3252464SDu, Changbin 		return false;
3535cee9645SThomas Gleixner 	}
3545cee9645SThomas Gleixner }
3555cee9645SThomas Gleixner 
3565cee9645SThomas Gleixner /*
3575cee9645SThomas Gleixner  * fixup_activate is called when:
3585cee9645SThomas Gleixner  * - an active object is activated
359b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
3605cee9645SThomas Gleixner  */
361e3252464SDu, Changbin static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
3625cee9645SThomas Gleixner {
3635cee9645SThomas Gleixner 	switch (state) {
3645cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3655cee9645SThomas Gleixner 		WARN_ON(1);
36675b710afSGustavo A. R. Silva 		/* fall through */
3675cee9645SThomas Gleixner 	default:
368e3252464SDu, Changbin 		return false;
3695cee9645SThomas Gleixner 	}
3705cee9645SThomas Gleixner }
3715cee9645SThomas Gleixner 
3725cee9645SThomas Gleixner /*
3735cee9645SThomas Gleixner  * fixup_free is called when:
3745cee9645SThomas Gleixner  * - an active object is freed
3755cee9645SThomas Gleixner  */
376e3252464SDu, Changbin static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
3775cee9645SThomas Gleixner {
3785cee9645SThomas Gleixner 	struct hrtimer *timer = addr;
3795cee9645SThomas Gleixner 
3805cee9645SThomas Gleixner 	switch (state) {
3815cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3825cee9645SThomas Gleixner 		hrtimer_cancel(timer);
3835cee9645SThomas Gleixner 		debug_object_free(timer, &hrtimer_debug_descr);
384e3252464SDu, Changbin 		return true;
3855cee9645SThomas Gleixner 	default:
386e3252464SDu, Changbin 		return false;
3875cee9645SThomas Gleixner 	}
3885cee9645SThomas Gleixner }
3895cee9645SThomas Gleixner 
3905cee9645SThomas Gleixner static struct debug_obj_descr hrtimer_debug_descr = {
3915cee9645SThomas Gleixner 	.name		= "hrtimer",
3925cee9645SThomas Gleixner 	.debug_hint	= hrtimer_debug_hint,
3935cee9645SThomas Gleixner 	.fixup_init	= hrtimer_fixup_init,
3945cee9645SThomas Gleixner 	.fixup_activate	= hrtimer_fixup_activate,
3955cee9645SThomas Gleixner 	.fixup_free	= hrtimer_fixup_free,
3965cee9645SThomas Gleixner };
3975cee9645SThomas Gleixner 
3985cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer)
3995cee9645SThomas Gleixner {
4005cee9645SThomas Gleixner 	debug_object_init(timer, &hrtimer_debug_descr);
4015cee9645SThomas Gleixner }
4025cee9645SThomas Gleixner 
4035da70160SAnna-Maria Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer,
4045da70160SAnna-Maria Gleixner 					  enum hrtimer_mode mode)
4055cee9645SThomas Gleixner {
4065cee9645SThomas Gleixner 	debug_object_activate(timer, &hrtimer_debug_descr);
4075cee9645SThomas Gleixner }
4085cee9645SThomas Gleixner 
4095cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
4105cee9645SThomas Gleixner {
4115cee9645SThomas Gleixner 	debug_object_deactivate(timer, &hrtimer_debug_descr);
4125cee9645SThomas Gleixner }
4135cee9645SThomas Gleixner 
4145cee9645SThomas Gleixner static inline void debug_hrtimer_free(struct hrtimer *timer)
4155cee9645SThomas Gleixner {
4165cee9645SThomas Gleixner 	debug_object_free(timer, &hrtimer_debug_descr);
4175cee9645SThomas Gleixner }
4185cee9645SThomas Gleixner 
4195cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
4205cee9645SThomas Gleixner 			   enum hrtimer_mode mode);
4215cee9645SThomas Gleixner 
4225cee9645SThomas Gleixner void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
4235cee9645SThomas Gleixner 			   enum hrtimer_mode mode)
4245cee9645SThomas Gleixner {
4255cee9645SThomas Gleixner 	debug_object_init_on_stack(timer, &hrtimer_debug_descr);
4265cee9645SThomas Gleixner 	__hrtimer_init(timer, clock_id, mode);
4275cee9645SThomas Gleixner }
4285cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
4295cee9645SThomas Gleixner 
430dbc1625fSSebastian Andrzej Siewior static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
431dbc1625fSSebastian Andrzej Siewior 				   clockid_t clock_id, enum hrtimer_mode mode);
432dbc1625fSSebastian Andrzej Siewior 
433dbc1625fSSebastian Andrzej Siewior void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
434dbc1625fSSebastian Andrzej Siewior 				   clockid_t clock_id, enum hrtimer_mode mode)
435dbc1625fSSebastian Andrzej Siewior {
436dbc1625fSSebastian Andrzej Siewior 	debug_object_init_on_stack(&sl->timer, &hrtimer_debug_descr);
437dbc1625fSSebastian Andrzej Siewior 	__hrtimer_init_sleeper(sl, clock_id, mode);
438dbc1625fSSebastian Andrzej Siewior }
439dbc1625fSSebastian Andrzej Siewior EXPORT_SYMBOL_GPL(hrtimer_init_sleeper_on_stack);
440dbc1625fSSebastian Andrzej Siewior 
4415cee9645SThomas Gleixner void destroy_hrtimer_on_stack(struct hrtimer *timer)
4425cee9645SThomas Gleixner {
4435cee9645SThomas Gleixner 	debug_object_free(timer, &hrtimer_debug_descr);
4445cee9645SThomas Gleixner }
445c08376acSGuenter Roeck EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
4465cee9645SThomas Gleixner 
4475cee9645SThomas Gleixner #else
4485da70160SAnna-Maria Gleixner 
4495cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer) { }
4505da70160SAnna-Maria Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer,
4515da70160SAnna-Maria Gleixner 					  enum hrtimer_mode mode) { }
4525cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
4535cee9645SThomas Gleixner #endif
4545cee9645SThomas Gleixner 
4555cee9645SThomas Gleixner static inline void
4565cee9645SThomas Gleixner debug_init(struct hrtimer *timer, clockid_t clockid,
4575cee9645SThomas Gleixner 	   enum hrtimer_mode mode)
4585cee9645SThomas Gleixner {
4595cee9645SThomas Gleixner 	debug_hrtimer_init(timer);
4605cee9645SThomas Gleixner 	trace_hrtimer_init(timer, clockid, mode);
4615cee9645SThomas Gleixner }
4625cee9645SThomas Gleixner 
46363e2ed36SAnna-Maria Gleixner static inline void debug_activate(struct hrtimer *timer,
46463e2ed36SAnna-Maria Gleixner 				  enum hrtimer_mode mode)
4655cee9645SThomas Gleixner {
4665da70160SAnna-Maria Gleixner 	debug_hrtimer_activate(timer, mode);
46763e2ed36SAnna-Maria Gleixner 	trace_hrtimer_start(timer, mode);
4685cee9645SThomas Gleixner }
4695cee9645SThomas Gleixner 
4705cee9645SThomas Gleixner static inline void debug_deactivate(struct hrtimer *timer)
4715cee9645SThomas Gleixner {
4725cee9645SThomas Gleixner 	debug_hrtimer_deactivate(timer);
4735cee9645SThomas Gleixner 	trace_hrtimer_cancel(timer);
4745cee9645SThomas Gleixner }
4755cee9645SThomas Gleixner 
476c272ca58SAnna-Maria Gleixner static struct hrtimer_clock_base *
477c272ca58SAnna-Maria Gleixner __next_base(struct hrtimer_cpu_base *cpu_base, unsigned int *active)
478c272ca58SAnna-Maria Gleixner {
479c272ca58SAnna-Maria Gleixner 	unsigned int idx;
480c272ca58SAnna-Maria Gleixner 
481c272ca58SAnna-Maria Gleixner 	if (!*active)
482c272ca58SAnna-Maria Gleixner 		return NULL;
483c272ca58SAnna-Maria Gleixner 
484c272ca58SAnna-Maria Gleixner 	idx = __ffs(*active);
485c272ca58SAnna-Maria Gleixner 	*active &= ~(1U << idx);
486c272ca58SAnna-Maria Gleixner 
487c272ca58SAnna-Maria Gleixner 	return &cpu_base->clock_base[idx];
488c272ca58SAnna-Maria Gleixner }
489c272ca58SAnna-Maria Gleixner 
490c272ca58SAnna-Maria Gleixner #define for_each_active_base(base, cpu_base, active)	\
491c272ca58SAnna-Maria Gleixner 	while ((base = __next_base((cpu_base), &(active))))
492c272ca58SAnna-Maria Gleixner 
493ad38f596SAnna-Maria Gleixner static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base,
494a59855cdSRafael J. Wysocki 					 const struct hrtimer *exclude,
495ad38f596SAnna-Maria Gleixner 					 unsigned int active,
496ad38f596SAnna-Maria Gleixner 					 ktime_t expires_next)
4979bc74919SThomas Gleixner {
498c272ca58SAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
499ad38f596SAnna-Maria Gleixner 	ktime_t expires;
5009bc74919SThomas Gleixner 
501c272ca58SAnna-Maria Gleixner 	for_each_active_base(base, cpu_base, active) {
5029bc74919SThomas Gleixner 		struct timerqueue_node *next;
5039bc74919SThomas Gleixner 		struct hrtimer *timer;
5049bc74919SThomas Gleixner 
50534aee88aSThomas Gleixner 		next = timerqueue_getnext(&base->active);
5069bc74919SThomas Gleixner 		timer = container_of(next, struct hrtimer, node);
507a59855cdSRafael J. Wysocki 		if (timer == exclude) {
508a59855cdSRafael J. Wysocki 			/* Get to the next timer in the queue. */
5097d2f6abbSRafael J. Wysocki 			next = timerqueue_iterate_next(next);
510a59855cdSRafael J. Wysocki 			if (!next)
511a59855cdSRafael J. Wysocki 				continue;
512a59855cdSRafael J. Wysocki 
513a59855cdSRafael J. Wysocki 			timer = container_of(next, struct hrtimer, node);
514a59855cdSRafael J. Wysocki 		}
5159bc74919SThomas Gleixner 		expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
5162456e855SThomas Gleixner 		if (expires < expires_next) {
5179bc74919SThomas Gleixner 			expires_next = expires;
518a59855cdSRafael J. Wysocki 
519a59855cdSRafael J. Wysocki 			/* Skip cpu_base update if a timer is being excluded. */
520a59855cdSRafael J. Wysocki 			if (exclude)
521a59855cdSRafael J. Wysocki 				continue;
522a59855cdSRafael J. Wysocki 
5235da70160SAnna-Maria Gleixner 			if (timer->is_soft)
5245da70160SAnna-Maria Gleixner 				cpu_base->softirq_next_timer = timer;
5255da70160SAnna-Maria Gleixner 			else
526eb27926bSAnna-Maria Gleixner 				cpu_base->next_timer = timer;
527895bdfa7SThomas Gleixner 		}
5289bc74919SThomas Gleixner 	}
5299bc74919SThomas Gleixner 	/*
5309bc74919SThomas Gleixner 	 * clock_was_set() might have changed base->offset of any of
5319bc74919SThomas Gleixner 	 * the clock bases so the result might be negative. Fix it up
5329bc74919SThomas Gleixner 	 * to prevent a false positive in clockevents_program_event().
5339bc74919SThomas Gleixner 	 */
5342456e855SThomas Gleixner 	if (expires_next < 0)
5352456e855SThomas Gleixner 		expires_next = 0;
5369bc74919SThomas Gleixner 	return expires_next;
5379bc74919SThomas Gleixner }
5389bc74919SThomas Gleixner 
539c458b1d1SAnna-Maria Gleixner /*
540c458b1d1SAnna-Maria Gleixner  * Recomputes cpu_base::*next_timer and returns the earliest expires_next but
541c458b1d1SAnna-Maria Gleixner  * does not set cpu_base::*expires_next, that is done by hrtimer_reprogram.
542c458b1d1SAnna-Maria Gleixner  *
5435da70160SAnna-Maria Gleixner  * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
5445da70160SAnna-Maria Gleixner  * those timers will get run whenever the softirq gets handled, at the end of
5455da70160SAnna-Maria Gleixner  * hrtimer_run_softirq(), hrtimer_update_softirq_timer() will re-add these bases.
5465da70160SAnna-Maria Gleixner  *
5475da70160SAnna-Maria Gleixner  * Therefore softirq values are those from the HRTIMER_ACTIVE_SOFT clock bases.
5485da70160SAnna-Maria Gleixner  * The !softirq values are the minima across HRTIMER_ACTIVE_ALL, unless an actual
5495da70160SAnna-Maria Gleixner  * softirq is pending, in which case they're the minima of HRTIMER_ACTIVE_HARD.
5505da70160SAnna-Maria Gleixner  *
551c458b1d1SAnna-Maria Gleixner  * @active_mask must be one of:
5525da70160SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_ALL,
553c458b1d1SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_SOFT, or
554c458b1d1SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_HARD.
555c458b1d1SAnna-Maria Gleixner  */
5565da70160SAnna-Maria Gleixner static ktime_t
5575da70160SAnna-Maria Gleixner __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_mask)
558ad38f596SAnna-Maria Gleixner {
559c458b1d1SAnna-Maria Gleixner 	unsigned int active;
5605da70160SAnna-Maria Gleixner 	struct hrtimer *next_timer = NULL;
561ad38f596SAnna-Maria Gleixner 	ktime_t expires_next = KTIME_MAX;
562ad38f596SAnna-Maria Gleixner 
5635da70160SAnna-Maria Gleixner 	if (!cpu_base->softirq_activated && (active_mask & HRTIMER_ACTIVE_SOFT)) {
5645da70160SAnna-Maria Gleixner 		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
5655da70160SAnna-Maria Gleixner 		cpu_base->softirq_next_timer = NULL;
566a59855cdSRafael J. Wysocki 		expires_next = __hrtimer_next_event_base(cpu_base, NULL,
567a59855cdSRafael J. Wysocki 							 active, KTIME_MAX);
568ad38f596SAnna-Maria Gleixner 
5695da70160SAnna-Maria Gleixner 		next_timer = cpu_base->softirq_next_timer;
5705da70160SAnna-Maria Gleixner 	}
5715da70160SAnna-Maria Gleixner 
5725da70160SAnna-Maria Gleixner 	if (active_mask & HRTIMER_ACTIVE_HARD) {
5735da70160SAnna-Maria Gleixner 		active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
5745da70160SAnna-Maria Gleixner 		cpu_base->next_timer = next_timer;
575a59855cdSRafael J. Wysocki 		expires_next = __hrtimer_next_event_base(cpu_base, NULL, active,
576a59855cdSRafael J. Wysocki 							 expires_next);
5775da70160SAnna-Maria Gleixner 	}
578ad38f596SAnna-Maria Gleixner 
579ad38f596SAnna-Maria Gleixner 	return expires_next;
580ad38f596SAnna-Maria Gleixner }
581ad38f596SAnna-Maria Gleixner 
58221d6d52aSThomas Gleixner static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
58321d6d52aSThomas Gleixner {
58421d6d52aSThomas Gleixner 	ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
585a3ed0e43SThomas Gleixner 	ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
58621d6d52aSThomas Gleixner 	ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
58721d6d52aSThomas Gleixner 
5885da70160SAnna-Maria Gleixner 	ktime_t now = ktime_get_update_offsets_now(&base->clock_was_set_seq,
589a3ed0e43SThomas Gleixner 					    offs_real, offs_boot, offs_tai);
5905da70160SAnna-Maria Gleixner 
5915da70160SAnna-Maria Gleixner 	base->clock_base[HRTIMER_BASE_REALTIME_SOFT].offset = *offs_real;
592a3ed0e43SThomas Gleixner 	base->clock_base[HRTIMER_BASE_BOOTTIME_SOFT].offset = *offs_boot;
5935da70160SAnna-Maria Gleixner 	base->clock_base[HRTIMER_BASE_TAI_SOFT].offset = *offs_tai;
5945da70160SAnna-Maria Gleixner 
5955da70160SAnna-Maria Gleixner 	return now;
59621d6d52aSThomas Gleixner }
59721d6d52aSThomas Gleixner 
59828bfd18bSAnna-Maria Gleixner /*
59928bfd18bSAnna-Maria Gleixner  * Is the high resolution mode active ?
60028bfd18bSAnna-Maria Gleixner  */
60128bfd18bSAnna-Maria Gleixner static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
60228bfd18bSAnna-Maria Gleixner {
60328bfd18bSAnna-Maria Gleixner 	return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
60428bfd18bSAnna-Maria Gleixner 		cpu_base->hres_active : 0;
60528bfd18bSAnna-Maria Gleixner }
60628bfd18bSAnna-Maria Gleixner 
60728bfd18bSAnna-Maria Gleixner static inline int hrtimer_hres_active(void)
60828bfd18bSAnna-Maria Gleixner {
60928bfd18bSAnna-Maria Gleixner 	return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases));
61028bfd18bSAnna-Maria Gleixner }
61128bfd18bSAnna-Maria Gleixner 
6125cee9645SThomas Gleixner /*
6135cee9645SThomas Gleixner  * Reprogram the event source with checking both queues for the
6145cee9645SThomas Gleixner  * next event
6155cee9645SThomas Gleixner  * Called with interrupts disabled and base->lock held
6165cee9645SThomas Gleixner  */
6175cee9645SThomas Gleixner static void
6185cee9645SThomas Gleixner hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
6195cee9645SThomas Gleixner {
62021d6d52aSThomas Gleixner 	ktime_t expires_next;
62121d6d52aSThomas Gleixner 
6225da70160SAnna-Maria Gleixner 	/*
6235da70160SAnna-Maria Gleixner 	 * Find the current next expiration time.
6245da70160SAnna-Maria Gleixner 	 */
6255da70160SAnna-Maria Gleixner 	expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
6265da70160SAnna-Maria Gleixner 
6275da70160SAnna-Maria Gleixner 	if (cpu_base->next_timer && cpu_base->next_timer->is_soft) {
6285da70160SAnna-Maria Gleixner 		/*
6295da70160SAnna-Maria Gleixner 		 * When the softirq is activated, hrtimer has to be
6305da70160SAnna-Maria Gleixner 		 * programmed with the first hard hrtimer because soft
6315da70160SAnna-Maria Gleixner 		 * timer interrupt could occur too late.
6325da70160SAnna-Maria Gleixner 		 */
6335da70160SAnna-Maria Gleixner 		if (cpu_base->softirq_activated)
6345da70160SAnna-Maria Gleixner 			expires_next = __hrtimer_get_next_event(cpu_base,
6355da70160SAnna-Maria Gleixner 								HRTIMER_ACTIVE_HARD);
6365da70160SAnna-Maria Gleixner 		else
6375da70160SAnna-Maria Gleixner 			cpu_base->softirq_expires_next = expires_next;
6385da70160SAnna-Maria Gleixner 	}
6395cee9645SThomas Gleixner 
6402456e855SThomas Gleixner 	if (skip_equal && expires_next == cpu_base->expires_next)
6415cee9645SThomas Gleixner 		return;
6425cee9645SThomas Gleixner 
6432456e855SThomas Gleixner 	cpu_base->expires_next = expires_next;
6445cee9645SThomas Gleixner 
6455cee9645SThomas Gleixner 	/*
64661bb4bcbSAnna-Maria Gleixner 	 * If hres is not active, hardware does not have to be
64761bb4bcbSAnna-Maria Gleixner 	 * reprogrammed yet.
64861bb4bcbSAnna-Maria Gleixner 	 *
6495cee9645SThomas Gleixner 	 * If a hang was detected in the last timer interrupt then we
6505cee9645SThomas Gleixner 	 * leave the hang delay active in the hardware. We want the
6515cee9645SThomas Gleixner 	 * system to make progress. That also prevents the following
6525cee9645SThomas Gleixner 	 * scenario:
6535cee9645SThomas Gleixner 	 * T1 expires 50ms from now
6545cee9645SThomas Gleixner 	 * T2 expires 5s from now
6555cee9645SThomas Gleixner 	 *
6565cee9645SThomas Gleixner 	 * T1 is removed, so this code is called and would reprogram
6575cee9645SThomas Gleixner 	 * the hardware to 5s from now. Any hrtimer_start after that
6585cee9645SThomas Gleixner 	 * will not reprogram the hardware due to hang_detected being
6595cee9645SThomas Gleixner 	 * set. So we'd effectivly block all timers until the T2 event
6605cee9645SThomas Gleixner 	 * fires.
6615cee9645SThomas Gleixner 	 */
66261bb4bcbSAnna-Maria Gleixner 	if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
6635cee9645SThomas Gleixner 		return;
6645cee9645SThomas Gleixner 
6655cee9645SThomas Gleixner 	tick_program_event(cpu_base->expires_next, 1);
6665cee9645SThomas Gleixner }
6675cee9645SThomas Gleixner 
668ebba2c72SAnna-Maria Gleixner /* High resolution timer related functions */
669ebba2c72SAnna-Maria Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
670ebba2c72SAnna-Maria Gleixner 
671ebba2c72SAnna-Maria Gleixner /*
672ebba2c72SAnna-Maria Gleixner  * High resolution timer enabled ?
673ebba2c72SAnna-Maria Gleixner  */
674ebba2c72SAnna-Maria Gleixner static bool hrtimer_hres_enabled __read_mostly  = true;
675ebba2c72SAnna-Maria Gleixner unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
676ebba2c72SAnna-Maria Gleixner EXPORT_SYMBOL_GPL(hrtimer_resolution);
677ebba2c72SAnna-Maria Gleixner 
678ebba2c72SAnna-Maria Gleixner /*
679ebba2c72SAnna-Maria Gleixner  * Enable / Disable high resolution mode
680ebba2c72SAnna-Maria Gleixner  */
681ebba2c72SAnna-Maria Gleixner static int __init setup_hrtimer_hres(char *str)
682ebba2c72SAnna-Maria Gleixner {
683ebba2c72SAnna-Maria Gleixner 	return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
684ebba2c72SAnna-Maria Gleixner }
685ebba2c72SAnna-Maria Gleixner 
686ebba2c72SAnna-Maria Gleixner __setup("highres=", setup_hrtimer_hres);
687ebba2c72SAnna-Maria Gleixner 
688ebba2c72SAnna-Maria Gleixner /*
689ebba2c72SAnna-Maria Gleixner  * hrtimer_high_res_enabled - query, if the highres mode is enabled
690ebba2c72SAnna-Maria Gleixner  */
691ebba2c72SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void)
692ebba2c72SAnna-Maria Gleixner {
693ebba2c72SAnna-Maria Gleixner 	return hrtimer_hres_enabled;
694ebba2c72SAnna-Maria Gleixner }
695ebba2c72SAnna-Maria Gleixner 
6965cee9645SThomas Gleixner /*
69711a9fe06SAnna-Maria Gleixner  * Retrigger next event is called after clock was set
69811a9fe06SAnna-Maria Gleixner  *
69911a9fe06SAnna-Maria Gleixner  * Called with interrupts disabled via on_each_cpu()
70011a9fe06SAnna-Maria Gleixner  */
70111a9fe06SAnna-Maria Gleixner static void retrigger_next_event(void *arg)
70211a9fe06SAnna-Maria Gleixner {
70311a9fe06SAnna-Maria Gleixner 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
70411a9fe06SAnna-Maria Gleixner 
70511a9fe06SAnna-Maria Gleixner 	if (!__hrtimer_hres_active(base))
70611a9fe06SAnna-Maria Gleixner 		return;
70711a9fe06SAnna-Maria Gleixner 
70811a9fe06SAnna-Maria Gleixner 	raw_spin_lock(&base->lock);
70911a9fe06SAnna-Maria Gleixner 	hrtimer_update_base(base);
71011a9fe06SAnna-Maria Gleixner 	hrtimer_force_reprogram(base, 0);
71111a9fe06SAnna-Maria Gleixner 	raw_spin_unlock(&base->lock);
71211a9fe06SAnna-Maria Gleixner }
71311a9fe06SAnna-Maria Gleixner 
71411a9fe06SAnna-Maria Gleixner /*
71511a9fe06SAnna-Maria Gleixner  * Switch to high resolution mode
71611a9fe06SAnna-Maria Gleixner  */
71711a9fe06SAnna-Maria Gleixner static void hrtimer_switch_to_hres(void)
71811a9fe06SAnna-Maria Gleixner {
71911a9fe06SAnna-Maria Gleixner 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
72011a9fe06SAnna-Maria Gleixner 
72111a9fe06SAnna-Maria Gleixner 	if (tick_init_highres()) {
7227a6e5537SGeert Uytterhoeven 		pr_warn("Could not switch to high resolution mode on CPU %u\n",
7237a6e5537SGeert Uytterhoeven 			base->cpu);
72411a9fe06SAnna-Maria Gleixner 		return;
72511a9fe06SAnna-Maria Gleixner 	}
72611a9fe06SAnna-Maria Gleixner 	base->hres_active = 1;
72711a9fe06SAnna-Maria Gleixner 	hrtimer_resolution = HIGH_RES_NSEC;
72811a9fe06SAnna-Maria Gleixner 
72911a9fe06SAnna-Maria Gleixner 	tick_setup_sched_timer();
73011a9fe06SAnna-Maria Gleixner 	/* "Retrigger" the interrupt to get things going */
73111a9fe06SAnna-Maria Gleixner 	retrigger_next_event(NULL);
73211a9fe06SAnna-Maria Gleixner }
73311a9fe06SAnna-Maria Gleixner 
73411a9fe06SAnna-Maria Gleixner static void clock_was_set_work(struct work_struct *work)
73511a9fe06SAnna-Maria Gleixner {
73611a9fe06SAnna-Maria Gleixner 	clock_was_set();
73711a9fe06SAnna-Maria Gleixner }
73811a9fe06SAnna-Maria Gleixner 
73911a9fe06SAnna-Maria Gleixner static DECLARE_WORK(hrtimer_work, clock_was_set_work);
74011a9fe06SAnna-Maria Gleixner 
74111a9fe06SAnna-Maria Gleixner /*
74211a9fe06SAnna-Maria Gleixner  * Called from timekeeping and resume code to reprogram the hrtimer
74311a9fe06SAnna-Maria Gleixner  * interrupt device on all cpus.
74411a9fe06SAnna-Maria Gleixner  */
74511a9fe06SAnna-Maria Gleixner void clock_was_set_delayed(void)
74611a9fe06SAnna-Maria Gleixner {
74711a9fe06SAnna-Maria Gleixner 	schedule_work(&hrtimer_work);
74811a9fe06SAnna-Maria Gleixner }
74911a9fe06SAnna-Maria Gleixner 
75011a9fe06SAnna-Maria Gleixner #else
75111a9fe06SAnna-Maria Gleixner 
75211a9fe06SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void) { return 0; }
75311a9fe06SAnna-Maria Gleixner static inline void hrtimer_switch_to_hres(void) { }
75411a9fe06SAnna-Maria Gleixner static inline void retrigger_next_event(void *arg) { }
75511a9fe06SAnna-Maria Gleixner 
75611a9fe06SAnna-Maria Gleixner #endif /* CONFIG_HIGH_RES_TIMERS */
75711a9fe06SAnna-Maria Gleixner 
75811a9fe06SAnna-Maria Gleixner /*
7595cee9645SThomas Gleixner  * When a timer is enqueued and expires earlier than the already enqueued
7605cee9645SThomas Gleixner  * timers, we have to check, whether it expires earlier than the timer for
7615cee9645SThomas Gleixner  * which the clock event device was armed.
7625cee9645SThomas Gleixner  *
7635cee9645SThomas Gleixner  * Called with interrupts disabled and base->cpu_base.lock held
7645cee9645SThomas Gleixner  */
7655da70160SAnna-Maria Gleixner static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
7665cee9645SThomas Gleixner {
767dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
7683ec7a3eeSAnna-Maria Gleixner 	struct hrtimer_clock_base *base = timer->base;
7695cee9645SThomas Gleixner 	ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
7705cee9645SThomas Gleixner 
7715cee9645SThomas Gleixner 	WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
7725cee9645SThomas Gleixner 
7735cee9645SThomas Gleixner 	/*
7745da70160SAnna-Maria Gleixner 	 * CLOCK_REALTIME timer might be requested with an absolute
7755da70160SAnna-Maria Gleixner 	 * expiry time which is less than base->offset. Set it to 0.
7765da70160SAnna-Maria Gleixner 	 */
7775da70160SAnna-Maria Gleixner 	if (expires < 0)
7785da70160SAnna-Maria Gleixner 		expires = 0;
7795da70160SAnna-Maria Gleixner 
7805da70160SAnna-Maria Gleixner 	if (timer->is_soft) {
7815da70160SAnna-Maria Gleixner 		/*
7825da70160SAnna-Maria Gleixner 		 * soft hrtimer could be started on a remote CPU. In this
7835da70160SAnna-Maria Gleixner 		 * case softirq_expires_next needs to be updated on the
7845da70160SAnna-Maria Gleixner 		 * remote CPU. The soft hrtimer will not expire before the
7855da70160SAnna-Maria Gleixner 		 * first hard hrtimer on the remote CPU -
7865da70160SAnna-Maria Gleixner 		 * hrtimer_check_target() prevents this case.
7875da70160SAnna-Maria Gleixner 		 */
7885da70160SAnna-Maria Gleixner 		struct hrtimer_cpu_base *timer_cpu_base = base->cpu_base;
7895da70160SAnna-Maria Gleixner 
7905da70160SAnna-Maria Gleixner 		if (timer_cpu_base->softirq_activated)
7915da70160SAnna-Maria Gleixner 			return;
7925da70160SAnna-Maria Gleixner 
7935da70160SAnna-Maria Gleixner 		if (!ktime_before(expires, timer_cpu_base->softirq_expires_next))
7945da70160SAnna-Maria Gleixner 			return;
7955da70160SAnna-Maria Gleixner 
7965da70160SAnna-Maria Gleixner 		timer_cpu_base->softirq_next_timer = timer;
7975da70160SAnna-Maria Gleixner 		timer_cpu_base->softirq_expires_next = expires;
7985da70160SAnna-Maria Gleixner 
7995da70160SAnna-Maria Gleixner 		if (!ktime_before(expires, timer_cpu_base->expires_next) ||
8005da70160SAnna-Maria Gleixner 		    !reprogram)
8015da70160SAnna-Maria Gleixner 			return;
8025da70160SAnna-Maria Gleixner 	}
8035da70160SAnna-Maria Gleixner 
8045da70160SAnna-Maria Gleixner 	/*
805c6eb3f70SThomas Gleixner 	 * If the timer is not on the current cpu, we cannot reprogram
806c6eb3f70SThomas Gleixner 	 * the other cpus clock event device.
8075cee9645SThomas Gleixner 	 */
808c6eb3f70SThomas Gleixner 	if (base->cpu_base != cpu_base)
809c6eb3f70SThomas Gleixner 		return;
810c6eb3f70SThomas Gleixner 
811c6eb3f70SThomas Gleixner 	/*
812c6eb3f70SThomas Gleixner 	 * If the hrtimer interrupt is running, then it will
813c6eb3f70SThomas Gleixner 	 * reevaluate the clock bases and reprogram the clock event
814c6eb3f70SThomas Gleixner 	 * device. The callbacks are always executed in hard interrupt
815c6eb3f70SThomas Gleixner 	 * context so we don't need an extra check for a running
816c6eb3f70SThomas Gleixner 	 * callback.
817c6eb3f70SThomas Gleixner 	 */
818c6eb3f70SThomas Gleixner 	if (cpu_base->in_hrtirq)
819c6eb3f70SThomas Gleixner 		return;
8205cee9645SThomas Gleixner 
8212456e855SThomas Gleixner 	if (expires >= cpu_base->expires_next)
822c6eb3f70SThomas Gleixner 		return;
8235cee9645SThomas Gleixner 
824c6eb3f70SThomas Gleixner 	/* Update the pointer to the next expiring timer */
825895bdfa7SThomas Gleixner 	cpu_base->next_timer = timer;
82614c80341SAnna-Maria Gleixner 	cpu_base->expires_next = expires;
8279bc74919SThomas Gleixner 
8289bc74919SThomas Gleixner 	/*
82914c80341SAnna-Maria Gleixner 	 * If hres is not active, hardware does not have to be
83014c80341SAnna-Maria Gleixner 	 * programmed yet.
83114c80341SAnna-Maria Gleixner 	 *
8325cee9645SThomas Gleixner 	 * If a hang was detected in the last timer interrupt then we
8335cee9645SThomas Gleixner 	 * do not schedule a timer which is earlier than the expiry
8345cee9645SThomas Gleixner 	 * which we enforced in the hang detection. We want the system
8355cee9645SThomas Gleixner 	 * to make progress.
8365cee9645SThomas Gleixner 	 */
83714c80341SAnna-Maria Gleixner 	if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
838c6eb3f70SThomas Gleixner 		return;
8395cee9645SThomas Gleixner 
8405cee9645SThomas Gleixner 	/*
841c6eb3f70SThomas Gleixner 	 * Program the timer hardware. We enforce the expiry for
842c6eb3f70SThomas Gleixner 	 * events which are already in the past.
8435cee9645SThomas Gleixner 	 */
844c6eb3f70SThomas Gleixner 	tick_program_event(expires, 1);
8455cee9645SThomas Gleixner }
8465cee9645SThomas Gleixner 
8475cee9645SThomas Gleixner /*
8485cee9645SThomas Gleixner  * Clock realtime was set
8495cee9645SThomas Gleixner  *
8505cee9645SThomas Gleixner  * Change the offset of the realtime clock vs. the monotonic
8515cee9645SThomas Gleixner  * clock.
8525cee9645SThomas Gleixner  *
8535cee9645SThomas Gleixner  * We might have to reprogram the high resolution timer interrupt. On
8545cee9645SThomas Gleixner  * SMP we call the architecture specific code to retrigger _all_ high
8555cee9645SThomas Gleixner  * resolution timer interrupts. On UP we just disable interrupts and
8565cee9645SThomas Gleixner  * call the high resolution interrupt code.
8575cee9645SThomas Gleixner  */
8585cee9645SThomas Gleixner void clock_was_set(void)
8595cee9645SThomas Gleixner {
8605cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
8615cee9645SThomas Gleixner 	/* Retrigger the CPU local events everywhere */
8625cee9645SThomas Gleixner 	on_each_cpu(retrigger_next_event, NULL, 1);
8635cee9645SThomas Gleixner #endif
8645cee9645SThomas Gleixner 	timerfd_clock_was_set();
8655cee9645SThomas Gleixner }
8665cee9645SThomas Gleixner 
8675cee9645SThomas Gleixner /*
8685cee9645SThomas Gleixner  * During resume we might have to reprogram the high resolution timer
8695cee9645SThomas Gleixner  * interrupt on all online CPUs.  However, all other CPUs will be
8705cee9645SThomas Gleixner  * stopped with IRQs interrupts disabled so the clock_was_set() call
8715cee9645SThomas Gleixner  * must be deferred.
8725cee9645SThomas Gleixner  */
8735cee9645SThomas Gleixner void hrtimers_resume(void)
8745cee9645SThomas Gleixner {
87553bef3fdSFrederic Weisbecker 	lockdep_assert_irqs_disabled();
8765cee9645SThomas Gleixner 	/* Retrigger on the local CPU */
8775cee9645SThomas Gleixner 	retrigger_next_event(NULL);
8785cee9645SThomas Gleixner 	/* And schedule a retrigger for all others */
8795cee9645SThomas Gleixner 	clock_was_set_delayed();
8805cee9645SThomas Gleixner }
8815cee9645SThomas Gleixner 
8825cee9645SThomas Gleixner /*
8835cee9645SThomas Gleixner  * Counterpart to lock_hrtimer_base above:
8845cee9645SThomas Gleixner  */
8855cee9645SThomas Gleixner static inline
8865cee9645SThomas Gleixner void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
8875cee9645SThomas Gleixner {
8885cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
8895cee9645SThomas Gleixner }
8905cee9645SThomas Gleixner 
8915cee9645SThomas Gleixner /**
8925cee9645SThomas Gleixner  * hrtimer_forward - forward the timer expiry
8935cee9645SThomas Gleixner  * @timer:	hrtimer to forward
8945cee9645SThomas Gleixner  * @now:	forward past this time
8955cee9645SThomas Gleixner  * @interval:	the interval to forward
8965cee9645SThomas Gleixner  *
8975cee9645SThomas Gleixner  * Forward the timer expiry so it will expire in the future.
8985cee9645SThomas Gleixner  * Returns the number of overruns.
89991e5a217SThomas Gleixner  *
90091e5a217SThomas Gleixner  * Can be safely called from the callback function of @timer. If
90191e5a217SThomas Gleixner  * called from other contexts @timer must neither be enqueued nor
90291e5a217SThomas Gleixner  * running the callback and the caller needs to take care of
90391e5a217SThomas Gleixner  * serialization.
90491e5a217SThomas Gleixner  *
90591e5a217SThomas Gleixner  * Note: This only updates the timer expiry value and does not requeue
90691e5a217SThomas Gleixner  * the timer.
9075cee9645SThomas Gleixner  */
9085cee9645SThomas Gleixner u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
9095cee9645SThomas Gleixner {
9105cee9645SThomas Gleixner 	u64 orun = 1;
9115cee9645SThomas Gleixner 	ktime_t delta;
9125cee9645SThomas Gleixner 
9135cee9645SThomas Gleixner 	delta = ktime_sub(now, hrtimer_get_expires(timer));
9145cee9645SThomas Gleixner 
9152456e855SThomas Gleixner 	if (delta < 0)
9165cee9645SThomas Gleixner 		return 0;
9175cee9645SThomas Gleixner 
9185de2755cSPeter Zijlstra 	if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
9195de2755cSPeter Zijlstra 		return 0;
9205de2755cSPeter Zijlstra 
9212456e855SThomas Gleixner 	if (interval < hrtimer_resolution)
9222456e855SThomas Gleixner 		interval = hrtimer_resolution;
9235cee9645SThomas Gleixner 
9242456e855SThomas Gleixner 	if (unlikely(delta >= interval)) {
9255cee9645SThomas Gleixner 		s64 incr = ktime_to_ns(interval);
9265cee9645SThomas Gleixner 
9275cee9645SThomas Gleixner 		orun = ktime_divns(delta, incr);
9285cee9645SThomas Gleixner 		hrtimer_add_expires_ns(timer, incr * orun);
9292456e855SThomas Gleixner 		if (hrtimer_get_expires_tv64(timer) > now)
9305cee9645SThomas Gleixner 			return orun;
9315cee9645SThomas Gleixner 		/*
9325cee9645SThomas Gleixner 		 * This (and the ktime_add() below) is the
9335cee9645SThomas Gleixner 		 * correction for exact:
9345cee9645SThomas Gleixner 		 */
9355cee9645SThomas Gleixner 		orun++;
9365cee9645SThomas Gleixner 	}
9375cee9645SThomas Gleixner 	hrtimer_add_expires(timer, interval);
9385cee9645SThomas Gleixner 
9395cee9645SThomas Gleixner 	return orun;
9405cee9645SThomas Gleixner }
9415cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_forward);
9425cee9645SThomas Gleixner 
9435cee9645SThomas Gleixner /*
9445cee9645SThomas Gleixner  * enqueue_hrtimer - internal function to (re)start a timer
9455cee9645SThomas Gleixner  *
9465cee9645SThomas Gleixner  * The timer is inserted in expiry order. Insertion into the
9475cee9645SThomas Gleixner  * red black tree is O(log(n)). Must hold the base lock.
9485cee9645SThomas Gleixner  *
9495cee9645SThomas Gleixner  * Returns 1 when the new timer is the leftmost timer in the tree.
9505cee9645SThomas Gleixner  */
9515cee9645SThomas Gleixner static int enqueue_hrtimer(struct hrtimer *timer,
95263e2ed36SAnna-Maria Gleixner 			   struct hrtimer_clock_base *base,
95363e2ed36SAnna-Maria Gleixner 			   enum hrtimer_mode mode)
9545cee9645SThomas Gleixner {
95563e2ed36SAnna-Maria Gleixner 	debug_activate(timer, mode);
9565cee9645SThomas Gleixner 
9575cee9645SThomas Gleixner 	base->cpu_base->active_bases |= 1 << base->index;
9585cee9645SThomas Gleixner 
959887d9dc9SPeter Zijlstra 	timer->state = HRTIMER_STATE_ENQUEUED;
9605cee9645SThomas Gleixner 
961b97f44c9SThomas Gleixner 	return timerqueue_add(&base->active, &timer->node);
9625cee9645SThomas Gleixner }
9635cee9645SThomas Gleixner 
9645cee9645SThomas Gleixner /*
9655cee9645SThomas Gleixner  * __remove_hrtimer - internal function to remove a timer
9665cee9645SThomas Gleixner  *
9675cee9645SThomas Gleixner  * Caller must hold the base lock.
9685cee9645SThomas Gleixner  *
9695cee9645SThomas Gleixner  * High resolution timer mode reprograms the clock event device when the
9705cee9645SThomas Gleixner  * timer is the one which expires next. The caller can disable this by setting
9715cee9645SThomas Gleixner  * reprogram to zero. This is useful, when the context does a reprogramming
9725cee9645SThomas Gleixner  * anyway (e.g. timer interrupt)
9735cee9645SThomas Gleixner  */
9745cee9645SThomas Gleixner static void __remove_hrtimer(struct hrtimer *timer,
9755cee9645SThomas Gleixner 			     struct hrtimer_clock_base *base,
976203cbf77SThomas Gleixner 			     u8 newstate, int reprogram)
9775cee9645SThomas Gleixner {
978e19ffe8bSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = base->cpu_base;
979203cbf77SThomas Gleixner 	u8 state = timer->state;
9805cee9645SThomas Gleixner 
9815cee9645SThomas Gleixner 	timer->state = newstate;
982895bdfa7SThomas Gleixner 	if (!(state & HRTIMER_STATE_ENQUEUED))
983895bdfa7SThomas Gleixner 		return;
9845cee9645SThomas Gleixner 
985b97f44c9SThomas Gleixner 	if (!timerqueue_del(&base->active, &timer->node))
986e19ffe8bSThomas Gleixner 		cpu_base->active_bases &= ~(1 << base->index);
987d9f0acdeSViresh Kumar 
988895bdfa7SThomas Gleixner 	/*
989895bdfa7SThomas Gleixner 	 * Note: If reprogram is false we do not update
990895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. This happens when we remove the first
991895bdfa7SThomas Gleixner 	 * timer on a remote cpu. No harm as we never dereference
992895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. So the worst thing what can happen is
993895bdfa7SThomas Gleixner 	 * an superflous call to hrtimer_force_reprogram() on the
994895bdfa7SThomas Gleixner 	 * remote cpu later on if the same timer gets enqueued again.
995895bdfa7SThomas Gleixner 	 */
996895bdfa7SThomas Gleixner 	if (reprogram && timer == cpu_base->next_timer)
997e19ffe8bSThomas Gleixner 		hrtimer_force_reprogram(cpu_base, 1);
9985cee9645SThomas Gleixner }
9995cee9645SThomas Gleixner 
10005cee9645SThomas Gleixner /*
10015cee9645SThomas Gleixner  * remove hrtimer, called with base lock held
10025cee9645SThomas Gleixner  */
10035cee9645SThomas Gleixner static inline int
10048edfb036SPeter Zijlstra remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart)
10055cee9645SThomas Gleixner {
10065cee9645SThomas Gleixner 	if (hrtimer_is_queued(timer)) {
1007203cbf77SThomas Gleixner 		u8 state = timer->state;
10085cee9645SThomas Gleixner 		int reprogram;
10095cee9645SThomas Gleixner 
10105cee9645SThomas Gleixner 		/*
10115cee9645SThomas Gleixner 		 * Remove the timer and force reprogramming when high
10125cee9645SThomas Gleixner 		 * resolution mode is active and the timer is on the current
10135cee9645SThomas Gleixner 		 * CPU. If we remove a timer on another CPU, reprogramming is
10145cee9645SThomas Gleixner 		 * skipped. The interrupt event on this CPU is fired and
10155cee9645SThomas Gleixner 		 * reprogramming happens in the interrupt handler. This is a
10165cee9645SThomas Gleixner 		 * rare case and less expensive than a smp call.
10175cee9645SThomas Gleixner 		 */
10185cee9645SThomas Gleixner 		debug_deactivate(timer);
1019dc5df73bSChristoph Lameter 		reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
10208edfb036SPeter Zijlstra 
1021887d9dc9SPeter Zijlstra 		if (!restart)
1022887d9dc9SPeter Zijlstra 			state = HRTIMER_STATE_INACTIVE;
1023887d9dc9SPeter Zijlstra 
10245cee9645SThomas Gleixner 		__remove_hrtimer(timer, base, state, reprogram);
10255cee9645SThomas Gleixner 		return 1;
10265cee9645SThomas Gleixner 	}
10275cee9645SThomas Gleixner 	return 0;
10285cee9645SThomas Gleixner }
10295cee9645SThomas Gleixner 
1030203cbf77SThomas Gleixner static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
1031203cbf77SThomas Gleixner 					    const enum hrtimer_mode mode)
1032203cbf77SThomas Gleixner {
1033203cbf77SThomas Gleixner #ifdef CONFIG_TIME_LOW_RES
1034203cbf77SThomas Gleixner 	/*
1035203cbf77SThomas Gleixner 	 * CONFIG_TIME_LOW_RES indicates that the system has no way to return
1036203cbf77SThomas Gleixner 	 * granular time values. For relative timers we add hrtimer_resolution
1037203cbf77SThomas Gleixner 	 * (i.e. one jiffie) to prevent short timeouts.
1038203cbf77SThomas Gleixner 	 */
1039203cbf77SThomas Gleixner 	timer->is_rel = mode & HRTIMER_MODE_REL;
1040203cbf77SThomas Gleixner 	if (timer->is_rel)
10418b0e1953SThomas Gleixner 		tim = ktime_add_safe(tim, hrtimer_resolution);
1042203cbf77SThomas Gleixner #endif
1043203cbf77SThomas Gleixner 	return tim;
1044203cbf77SThomas Gleixner }
1045203cbf77SThomas Gleixner 
10465da70160SAnna-Maria Gleixner static void
10475da70160SAnna-Maria Gleixner hrtimer_update_softirq_timer(struct hrtimer_cpu_base *cpu_base, bool reprogram)
10485da70160SAnna-Maria Gleixner {
10495da70160SAnna-Maria Gleixner 	ktime_t expires;
10505da70160SAnna-Maria Gleixner 
10515da70160SAnna-Maria Gleixner 	/*
10525da70160SAnna-Maria Gleixner 	 * Find the next SOFT expiration.
10535da70160SAnna-Maria Gleixner 	 */
10545da70160SAnna-Maria Gleixner 	expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
10555da70160SAnna-Maria Gleixner 
10565da70160SAnna-Maria Gleixner 	/*
10575da70160SAnna-Maria Gleixner 	 * reprogramming needs to be triggered, even if the next soft
10585da70160SAnna-Maria Gleixner 	 * hrtimer expires at the same time than the next hard
10595da70160SAnna-Maria Gleixner 	 * hrtimer. cpu_base->softirq_expires_next needs to be updated!
10605da70160SAnna-Maria Gleixner 	 */
10615da70160SAnna-Maria Gleixner 	if (expires == KTIME_MAX)
10625da70160SAnna-Maria Gleixner 		return;
10635da70160SAnna-Maria Gleixner 
10645da70160SAnna-Maria Gleixner 	/*
10655da70160SAnna-Maria Gleixner 	 * cpu_base->*next_timer is recomputed by __hrtimer_get_next_event()
10665da70160SAnna-Maria Gleixner 	 * cpu_base->*expires_next is only set by hrtimer_reprogram()
10675da70160SAnna-Maria Gleixner 	 */
10685da70160SAnna-Maria Gleixner 	hrtimer_reprogram(cpu_base->softirq_next_timer, reprogram);
10695da70160SAnna-Maria Gleixner }
10705da70160SAnna-Maria Gleixner 
1071138a6b7aSAnna-Maria Gleixner static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1072138a6b7aSAnna-Maria Gleixner 				    u64 delta_ns, const enum hrtimer_mode mode,
1073138a6b7aSAnna-Maria Gleixner 				    struct hrtimer_clock_base *base)
10745cee9645SThomas Gleixner {
1075138a6b7aSAnna-Maria Gleixner 	struct hrtimer_clock_base *new_base;
10765cee9645SThomas Gleixner 
10775cee9645SThomas Gleixner 	/* Remove an active timer from the queue: */
10788edfb036SPeter Zijlstra 	remove_hrtimer(timer, base, true);
10795cee9645SThomas Gleixner 
1080203cbf77SThomas Gleixner 	if (mode & HRTIMER_MODE_REL)
10815cee9645SThomas Gleixner 		tim = ktime_add_safe(tim, base->get_time());
1082203cbf77SThomas Gleixner 
1083203cbf77SThomas Gleixner 	tim = hrtimer_update_lowres(timer, tim, mode);
10845cee9645SThomas Gleixner 
10855cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(timer, tim, delta_ns);
10865cee9645SThomas Gleixner 
10875cee9645SThomas Gleixner 	/* Switch the timer base, if necessary: */
10885cee9645SThomas Gleixner 	new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
10895cee9645SThomas Gleixner 
1090138a6b7aSAnna-Maria Gleixner 	return enqueue_hrtimer(timer, new_base, mode);
1091138a6b7aSAnna-Maria Gleixner }
10925da70160SAnna-Maria Gleixner 
1093138a6b7aSAnna-Maria Gleixner /**
1094138a6b7aSAnna-Maria Gleixner  * hrtimer_start_range_ns - (re)start an hrtimer
1095138a6b7aSAnna-Maria Gleixner  * @timer:	the timer to be added
1096138a6b7aSAnna-Maria Gleixner  * @tim:	expiry time
1097138a6b7aSAnna-Maria Gleixner  * @delta_ns:	"slack" range for the timer
1098138a6b7aSAnna-Maria Gleixner  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
10995da70160SAnna-Maria Gleixner  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
11005da70160SAnna-Maria Gleixner  *		softirq based mode is considered for debug purpose only!
1101138a6b7aSAnna-Maria Gleixner  */
1102138a6b7aSAnna-Maria Gleixner void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1103138a6b7aSAnna-Maria Gleixner 			    u64 delta_ns, const enum hrtimer_mode mode)
1104138a6b7aSAnna-Maria Gleixner {
1105138a6b7aSAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1106138a6b7aSAnna-Maria Gleixner 	unsigned long flags;
110749a2a075SViresh Kumar 
11085da70160SAnna-Maria Gleixner 	/*
11095da70160SAnna-Maria Gleixner 	 * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
11100ab6a3ddSThomas Gleixner 	 * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard
11110ab6a3ddSThomas Gleixner 	 * expiry mode because unmarked timers are moved to softirq expiry.
11125da70160SAnna-Maria Gleixner 	 */
11130ab6a3ddSThomas Gleixner 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
11145da70160SAnna-Maria Gleixner 		WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft);
11150ab6a3ddSThomas Gleixner 	else
11160ab6a3ddSThomas Gleixner 		WARN_ON_ONCE(!(mode & HRTIMER_MODE_HARD) ^ !timer->is_hard);
11175da70160SAnna-Maria Gleixner 
1118138a6b7aSAnna-Maria Gleixner 	base = lock_hrtimer_base(timer, &flags);
1119138a6b7aSAnna-Maria Gleixner 
1120138a6b7aSAnna-Maria Gleixner 	if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base))
11215da70160SAnna-Maria Gleixner 		hrtimer_reprogram(timer, true);
1122138a6b7aSAnna-Maria Gleixner 
11235cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
11245cee9645SThomas Gleixner }
11255cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
11265cee9645SThomas Gleixner 
11275cee9645SThomas Gleixner /**
11285cee9645SThomas Gleixner  * hrtimer_try_to_cancel - try to deactivate a timer
11295cee9645SThomas Gleixner  * @timer:	hrtimer to stop
11305cee9645SThomas Gleixner  *
11315cee9645SThomas Gleixner  * Returns:
113251633704SMauro Carvalho Chehab  *
113351633704SMauro Carvalho Chehab  *  *  0 when the timer was not active
113451633704SMauro Carvalho Chehab  *  *  1 when the timer was active
113551633704SMauro Carvalho Chehab  *  * -1 when the timer is currently executing the callback function and
11365cee9645SThomas Gleixner  *    cannot be stopped
11375cee9645SThomas Gleixner  */
11385cee9645SThomas Gleixner int hrtimer_try_to_cancel(struct hrtimer *timer)
11395cee9645SThomas Gleixner {
11405cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
11415cee9645SThomas Gleixner 	unsigned long flags;
11425cee9645SThomas Gleixner 	int ret = -1;
11435cee9645SThomas Gleixner 
114419d9f422SThomas Gleixner 	/*
114519d9f422SThomas Gleixner 	 * Check lockless first. If the timer is not active (neither
114619d9f422SThomas Gleixner 	 * enqueued nor running the callback, nothing to do here.  The
114719d9f422SThomas Gleixner 	 * base lock does not serialize against a concurrent enqueue,
114819d9f422SThomas Gleixner 	 * so we can avoid taking it.
114919d9f422SThomas Gleixner 	 */
115019d9f422SThomas Gleixner 	if (!hrtimer_active(timer))
115119d9f422SThomas Gleixner 		return 0;
115219d9f422SThomas Gleixner 
11535cee9645SThomas Gleixner 	base = lock_hrtimer_base(timer, &flags);
11545cee9645SThomas Gleixner 
11555cee9645SThomas Gleixner 	if (!hrtimer_callback_running(timer))
11568edfb036SPeter Zijlstra 		ret = remove_hrtimer(timer, base, false);
11575cee9645SThomas Gleixner 
11585cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
11595cee9645SThomas Gleixner 
11605cee9645SThomas Gleixner 	return ret;
11615cee9645SThomas Gleixner 
11625cee9645SThomas Gleixner }
11635cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
11645cee9645SThomas Gleixner 
1165f61eff83SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT
1166f61eff83SAnna-Maria Gleixner static void hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base)
1167f61eff83SAnna-Maria Gleixner {
1168f61eff83SAnna-Maria Gleixner 	spin_lock_init(&base->softirq_expiry_lock);
1169f61eff83SAnna-Maria Gleixner }
1170f61eff83SAnna-Maria Gleixner 
1171f61eff83SAnna-Maria Gleixner static void hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base)
1172f61eff83SAnna-Maria Gleixner {
1173f61eff83SAnna-Maria Gleixner 	spin_lock(&base->softirq_expiry_lock);
1174f61eff83SAnna-Maria Gleixner }
1175f61eff83SAnna-Maria Gleixner 
1176f61eff83SAnna-Maria Gleixner static void hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base)
1177f61eff83SAnna-Maria Gleixner {
1178f61eff83SAnna-Maria Gleixner 	spin_unlock(&base->softirq_expiry_lock);
1179f61eff83SAnna-Maria Gleixner }
1180f61eff83SAnna-Maria Gleixner 
1181f61eff83SAnna-Maria Gleixner /*
1182f61eff83SAnna-Maria Gleixner  * The counterpart to hrtimer_cancel_wait_running().
1183f61eff83SAnna-Maria Gleixner  *
1184f61eff83SAnna-Maria Gleixner  * If there is a waiter for cpu_base->expiry_lock, then it was waiting for
1185f61eff83SAnna-Maria Gleixner  * the timer callback to finish. Drop expiry_lock and reaquire it. That
1186f61eff83SAnna-Maria Gleixner  * allows the waiter to acquire the lock and make progress.
1187f61eff83SAnna-Maria Gleixner  */
1188f61eff83SAnna-Maria Gleixner static void hrtimer_sync_wait_running(struct hrtimer_cpu_base *cpu_base,
1189f61eff83SAnna-Maria Gleixner 				      unsigned long flags)
1190f61eff83SAnna-Maria Gleixner {
1191f61eff83SAnna-Maria Gleixner 	if (atomic_read(&cpu_base->timer_waiters)) {
1192f61eff83SAnna-Maria Gleixner 		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1193f61eff83SAnna-Maria Gleixner 		spin_unlock(&cpu_base->softirq_expiry_lock);
1194f61eff83SAnna-Maria Gleixner 		spin_lock(&cpu_base->softirq_expiry_lock);
1195f61eff83SAnna-Maria Gleixner 		raw_spin_lock_irq(&cpu_base->lock);
1196f61eff83SAnna-Maria Gleixner 	}
1197f61eff83SAnna-Maria Gleixner }
1198f61eff83SAnna-Maria Gleixner 
1199f61eff83SAnna-Maria Gleixner /*
1200f61eff83SAnna-Maria Gleixner  * This function is called on PREEMPT_RT kernels when the fast path
1201f61eff83SAnna-Maria Gleixner  * deletion of a timer failed because the timer callback function was
1202f61eff83SAnna-Maria Gleixner  * running.
1203f61eff83SAnna-Maria Gleixner  *
1204*0bee3b60SFrederic Weisbecker  * This prevents priority inversion: if the soft irq thread is preempted
1205*0bee3b60SFrederic Weisbecker  * in the middle of a timer callback, then calling del_timer_sync() can
1206*0bee3b60SFrederic Weisbecker  * lead to two issues:
1207*0bee3b60SFrederic Weisbecker  *
1208*0bee3b60SFrederic Weisbecker  *  - If the caller is on a remote CPU then it has to spin wait for the timer
1209*0bee3b60SFrederic Weisbecker  *    handler to complete. This can result in unbound priority inversion.
1210*0bee3b60SFrederic Weisbecker  *
1211*0bee3b60SFrederic Weisbecker  *  - If the caller originates from the task which preempted the timer
1212*0bee3b60SFrederic Weisbecker  *    handler on the same CPU, then spin waiting for the timer handler to
1213*0bee3b60SFrederic Weisbecker  *    complete is never going to end.
1214f61eff83SAnna-Maria Gleixner  */
1215f61eff83SAnna-Maria Gleixner void hrtimer_cancel_wait_running(const struct hrtimer *timer)
1216f61eff83SAnna-Maria Gleixner {
1217f61eff83SAnna-Maria Gleixner 	struct hrtimer_clock_base *base = timer->base;
1218f61eff83SAnna-Maria Gleixner 
1219f61eff83SAnna-Maria Gleixner 	if (!timer->is_soft || !base || !base->cpu_base) {
1220f61eff83SAnna-Maria Gleixner 		cpu_relax();
1221f61eff83SAnna-Maria Gleixner 		return;
1222f61eff83SAnna-Maria Gleixner 	}
1223f61eff83SAnna-Maria Gleixner 
1224f61eff83SAnna-Maria Gleixner 	/*
1225f61eff83SAnna-Maria Gleixner 	 * Mark the base as contended and grab the expiry lock, which is
1226f61eff83SAnna-Maria Gleixner 	 * held by the softirq across the timer callback. Drop the lock
1227f61eff83SAnna-Maria Gleixner 	 * immediately so the softirq can expire the next timer. In theory
1228f61eff83SAnna-Maria Gleixner 	 * the timer could already be running again, but that's more than
1229f61eff83SAnna-Maria Gleixner 	 * unlikely and just causes another wait loop.
1230f61eff83SAnna-Maria Gleixner 	 */
1231f61eff83SAnna-Maria Gleixner 	atomic_inc(&base->cpu_base->timer_waiters);
1232f61eff83SAnna-Maria Gleixner 	spin_lock_bh(&base->cpu_base->softirq_expiry_lock);
1233f61eff83SAnna-Maria Gleixner 	atomic_dec(&base->cpu_base->timer_waiters);
1234f61eff83SAnna-Maria Gleixner 	spin_unlock_bh(&base->cpu_base->softirq_expiry_lock);
1235f61eff83SAnna-Maria Gleixner }
1236f61eff83SAnna-Maria Gleixner #else
1237f61eff83SAnna-Maria Gleixner static inline void
1238f61eff83SAnna-Maria Gleixner hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) { }
1239f61eff83SAnna-Maria Gleixner static inline void
1240f61eff83SAnna-Maria Gleixner hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) { }
1241f61eff83SAnna-Maria Gleixner static inline void
1242f61eff83SAnna-Maria Gleixner hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) { }
1243f61eff83SAnna-Maria Gleixner static inline void hrtimer_sync_wait_running(struct hrtimer_cpu_base *base,
1244f61eff83SAnna-Maria Gleixner 					     unsigned long flags) { }
1245f61eff83SAnna-Maria Gleixner #endif
1246f61eff83SAnna-Maria Gleixner 
12475cee9645SThomas Gleixner /**
12485cee9645SThomas Gleixner  * hrtimer_cancel - cancel a timer and wait for the handler to finish.
12495cee9645SThomas Gleixner  * @timer:	the timer to be cancelled
12505cee9645SThomas Gleixner  *
12515cee9645SThomas Gleixner  * Returns:
12525cee9645SThomas Gleixner  *  0 when the timer was not active
12535cee9645SThomas Gleixner  *  1 when the timer was active
12545cee9645SThomas Gleixner  */
12555cee9645SThomas Gleixner int hrtimer_cancel(struct hrtimer *timer)
12565cee9645SThomas Gleixner {
1257f61eff83SAnna-Maria Gleixner 	int ret;
12585cee9645SThomas Gleixner 
1259f61eff83SAnna-Maria Gleixner 	do {
1260f61eff83SAnna-Maria Gleixner 		ret = hrtimer_try_to_cancel(timer);
1261f61eff83SAnna-Maria Gleixner 
1262f61eff83SAnna-Maria Gleixner 		if (ret < 0)
1263f61eff83SAnna-Maria Gleixner 			hrtimer_cancel_wait_running(timer);
1264f61eff83SAnna-Maria Gleixner 	} while (ret < 0);
12655cee9645SThomas Gleixner 	return ret;
12665cee9645SThomas Gleixner }
12675cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_cancel);
12685cee9645SThomas Gleixner 
12695cee9645SThomas Gleixner /**
12705cee9645SThomas Gleixner  * hrtimer_get_remaining - get remaining time for the timer
12715cee9645SThomas Gleixner  * @timer:	the timer to read
1272203cbf77SThomas Gleixner  * @adjust:	adjust relative timers when CONFIG_TIME_LOW_RES=y
12735cee9645SThomas Gleixner  */
1274203cbf77SThomas Gleixner ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
12755cee9645SThomas Gleixner {
12765cee9645SThomas Gleixner 	unsigned long flags;
12775cee9645SThomas Gleixner 	ktime_t rem;
12785cee9645SThomas Gleixner 
12795cee9645SThomas Gleixner 	lock_hrtimer_base(timer, &flags);
1280203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1281203cbf77SThomas Gleixner 		rem = hrtimer_expires_remaining_adjusted(timer);
1282203cbf77SThomas Gleixner 	else
12835cee9645SThomas Gleixner 		rem = hrtimer_expires_remaining(timer);
12845cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
12855cee9645SThomas Gleixner 
12865cee9645SThomas Gleixner 	return rem;
12875cee9645SThomas Gleixner }
1288203cbf77SThomas Gleixner EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
12895cee9645SThomas Gleixner 
12905cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
12915cee9645SThomas Gleixner /**
12925cee9645SThomas Gleixner  * hrtimer_get_next_event - get the time until next expiry event
12935cee9645SThomas Gleixner  *
1294c1ad348bSThomas Gleixner  * Returns the next expiry time or KTIME_MAX if no timer is pending.
12955cee9645SThomas Gleixner  */
1296c1ad348bSThomas Gleixner u64 hrtimer_get_next_event(void)
12975cee9645SThomas Gleixner {
1298dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1299c1ad348bSThomas Gleixner 	u64 expires = KTIME_MAX;
13005cee9645SThomas Gleixner 	unsigned long flags;
13015cee9645SThomas Gleixner 
13025cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
13035cee9645SThomas Gleixner 
1304e19ffe8bSThomas Gleixner 	if (!__hrtimer_hres_active(cpu_base))
13055da70160SAnna-Maria Gleixner 		expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
13065cee9645SThomas Gleixner 
13075cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
13085cee9645SThomas Gleixner 
1309c1ad348bSThomas Gleixner 	return expires;
13105cee9645SThomas Gleixner }
1311a59855cdSRafael J. Wysocki 
1312a59855cdSRafael J. Wysocki /**
1313a59855cdSRafael J. Wysocki  * hrtimer_next_event_without - time until next expiry event w/o one timer
1314a59855cdSRafael J. Wysocki  * @exclude:	timer to exclude
1315a59855cdSRafael J. Wysocki  *
1316a59855cdSRafael J. Wysocki  * Returns the next expiry time over all timers except for the @exclude one or
1317a59855cdSRafael J. Wysocki  * KTIME_MAX if none of them is pending.
1318a59855cdSRafael J. Wysocki  */
1319a59855cdSRafael J. Wysocki u64 hrtimer_next_event_without(const struct hrtimer *exclude)
1320a59855cdSRafael J. Wysocki {
1321a59855cdSRafael J. Wysocki 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1322a59855cdSRafael J. Wysocki 	u64 expires = KTIME_MAX;
1323a59855cdSRafael J. Wysocki 	unsigned long flags;
1324a59855cdSRafael J. Wysocki 
1325a59855cdSRafael J. Wysocki 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
1326a59855cdSRafael J. Wysocki 
1327a59855cdSRafael J. Wysocki 	if (__hrtimer_hres_active(cpu_base)) {
1328a59855cdSRafael J. Wysocki 		unsigned int active;
1329a59855cdSRafael J. Wysocki 
1330a59855cdSRafael J. Wysocki 		if (!cpu_base->softirq_activated) {
1331a59855cdSRafael J. Wysocki 			active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
1332a59855cdSRafael J. Wysocki 			expires = __hrtimer_next_event_base(cpu_base, exclude,
1333a59855cdSRafael J. Wysocki 							    active, KTIME_MAX);
1334a59855cdSRafael J. Wysocki 		}
1335a59855cdSRafael J. Wysocki 		active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
1336a59855cdSRafael J. Wysocki 		expires = __hrtimer_next_event_base(cpu_base, exclude, active,
1337a59855cdSRafael J. Wysocki 						    expires);
1338a59855cdSRafael J. Wysocki 	}
1339a59855cdSRafael J. Wysocki 
1340a59855cdSRafael J. Wysocki 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1341a59855cdSRafael J. Wysocki 
1342a59855cdSRafael J. Wysocki 	return expires;
1343a59855cdSRafael J. Wysocki }
13445cee9645SThomas Gleixner #endif
13455cee9645SThomas Gleixner 
1346336a9cdeSMarc Zyngier static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1347336a9cdeSMarc Zyngier {
1348336a9cdeSMarc Zyngier 	if (likely(clock_id < MAX_CLOCKS)) {
1349336a9cdeSMarc Zyngier 		int base = hrtimer_clock_to_base_table[clock_id];
1350336a9cdeSMarc Zyngier 
1351336a9cdeSMarc Zyngier 		if (likely(base != HRTIMER_MAX_CLOCK_BASES))
1352336a9cdeSMarc Zyngier 			return base;
1353336a9cdeSMarc Zyngier 	}
1354336a9cdeSMarc Zyngier 	WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1355336a9cdeSMarc Zyngier 	return HRTIMER_BASE_MONOTONIC;
1356336a9cdeSMarc Zyngier }
1357336a9cdeSMarc Zyngier 
13585cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
13595cee9645SThomas Gleixner 			   enum hrtimer_mode mode)
13605cee9645SThomas Gleixner {
136142f42da4SAnna-Maria Gleixner 	bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
13625cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base;
1363f5c2f021SSebastian Andrzej Siewior 	int base;
1364f5c2f021SSebastian Andrzej Siewior 
1365f5c2f021SSebastian Andrzej Siewior 	/*
1366f5c2f021SSebastian Andrzej Siewior 	 * On PREEMPT_RT enabled kernels hrtimers which are not explicitely
1367f5c2f021SSebastian Andrzej Siewior 	 * marked for hard interrupt expiry mode are moved into soft
1368f5c2f021SSebastian Andrzej Siewior 	 * interrupt context for latency reasons and because the callbacks
1369f5c2f021SSebastian Andrzej Siewior 	 * can invoke functions which might sleep on RT, e.g. spin_lock().
1370f5c2f021SSebastian Andrzej Siewior 	 */
1371f5c2f021SSebastian Andrzej Siewior 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD))
1372f5c2f021SSebastian Andrzej Siewior 		softtimer = true;
13735cee9645SThomas Gleixner 
13745cee9645SThomas Gleixner 	memset(timer, 0, sizeof(struct hrtimer));
13755cee9645SThomas Gleixner 
137622127e93SChristoph Lameter 	cpu_base = raw_cpu_ptr(&hrtimer_bases);
13775cee9645SThomas Gleixner 
137848d0c9beSAnna-Maria Gleixner 	/*
137948d0c9beSAnna-Maria Gleixner 	 * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
138048d0c9beSAnna-Maria Gleixner 	 * clock modifications, so they needs to become CLOCK_MONOTONIC to
138148d0c9beSAnna-Maria Gleixner 	 * ensure POSIX compliance.
138248d0c9beSAnna-Maria Gleixner 	 */
138348d0c9beSAnna-Maria Gleixner 	if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
13845cee9645SThomas Gleixner 		clock_id = CLOCK_MONOTONIC;
13855cee9645SThomas Gleixner 
1386f5c2f021SSebastian Andrzej Siewior 	base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
138742f42da4SAnna-Maria Gleixner 	base += hrtimer_clockid_to_base(clock_id);
138842f42da4SAnna-Maria Gleixner 	timer->is_soft = softtimer;
13890ab6a3ddSThomas Gleixner 	timer->is_hard = !softtimer;
13905cee9645SThomas Gleixner 	timer->base = &cpu_base->clock_base[base];
13915cee9645SThomas Gleixner 	timerqueue_init(&timer->node);
13925cee9645SThomas Gleixner }
13935cee9645SThomas Gleixner 
13945cee9645SThomas Gleixner /**
13955cee9645SThomas Gleixner  * hrtimer_init - initialize a timer to the given clock
13965cee9645SThomas Gleixner  * @timer:	the timer to be initialized
13975cee9645SThomas Gleixner  * @clock_id:	the clock to be used
139842f42da4SAnna-Maria Gleixner  * @mode:       The modes which are relevant for intitialization:
139942f42da4SAnna-Maria Gleixner  *              HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
140042f42da4SAnna-Maria Gleixner  *              HRTIMER_MODE_REL_SOFT
140142f42da4SAnna-Maria Gleixner  *
140242f42da4SAnna-Maria Gleixner  *              The PINNED variants of the above can be handed in,
140342f42da4SAnna-Maria Gleixner  *              but the PINNED bit is ignored as pinning happens
140442f42da4SAnna-Maria Gleixner  *              when the hrtimer is started
14055cee9645SThomas Gleixner  */
14065cee9645SThomas Gleixner void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
14075cee9645SThomas Gleixner 		  enum hrtimer_mode mode)
14085cee9645SThomas Gleixner {
14095cee9645SThomas Gleixner 	debug_init(timer, clock_id, mode);
14105cee9645SThomas Gleixner 	__hrtimer_init(timer, clock_id, mode);
14115cee9645SThomas Gleixner }
14125cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init);
14135cee9645SThomas Gleixner 
1414887d9dc9SPeter Zijlstra /*
1415887d9dc9SPeter Zijlstra  * A timer is active, when it is enqueued into the rbtree or the
1416887d9dc9SPeter Zijlstra  * callback function is running or it's in the state of being migrated
1417887d9dc9SPeter Zijlstra  * to another cpu.
14185cee9645SThomas Gleixner  *
1419887d9dc9SPeter Zijlstra  * It is important for this function to not return a false negative.
14205cee9645SThomas Gleixner  */
1421887d9dc9SPeter Zijlstra bool hrtimer_active(const struct hrtimer *timer)
14225cee9645SThomas Gleixner {
14233f0b9e8eSAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1424887d9dc9SPeter Zijlstra 	unsigned int seq;
14255cee9645SThomas Gleixner 
1426887d9dc9SPeter Zijlstra 	do {
14273f0b9e8eSAnna-Maria Gleixner 		base = READ_ONCE(timer->base);
14283f0b9e8eSAnna-Maria Gleixner 		seq = raw_read_seqcount_begin(&base->seq);
14295cee9645SThomas Gleixner 
1430887d9dc9SPeter Zijlstra 		if (timer->state != HRTIMER_STATE_INACTIVE ||
14313f0b9e8eSAnna-Maria Gleixner 		    base->running == timer)
1432887d9dc9SPeter Zijlstra 			return true;
1433887d9dc9SPeter Zijlstra 
14343f0b9e8eSAnna-Maria Gleixner 	} while (read_seqcount_retry(&base->seq, seq) ||
14353f0b9e8eSAnna-Maria Gleixner 		 base != READ_ONCE(timer->base));
1436887d9dc9SPeter Zijlstra 
1437887d9dc9SPeter Zijlstra 	return false;
14385cee9645SThomas Gleixner }
1439887d9dc9SPeter Zijlstra EXPORT_SYMBOL_GPL(hrtimer_active);
14405cee9645SThomas Gleixner 
1441887d9dc9SPeter Zijlstra /*
1442887d9dc9SPeter Zijlstra  * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1443887d9dc9SPeter Zijlstra  * distinct sections:
1444887d9dc9SPeter Zijlstra  *
1445887d9dc9SPeter Zijlstra  *  - queued:	the timer is queued
1446887d9dc9SPeter Zijlstra  *  - callback:	the timer is being ran
1447887d9dc9SPeter Zijlstra  *  - post:	the timer is inactive or (re)queued
1448887d9dc9SPeter Zijlstra  *
1449887d9dc9SPeter Zijlstra  * On the read side we ensure we observe timer->state and cpu_base->running
1450887d9dc9SPeter Zijlstra  * from the same section, if anything changed while we looked at it, we retry.
1451887d9dc9SPeter Zijlstra  * This includes timer->base changing because sequence numbers alone are
1452887d9dc9SPeter Zijlstra  * insufficient for that.
1453887d9dc9SPeter Zijlstra  *
1454887d9dc9SPeter Zijlstra  * The sequence numbers are required because otherwise we could still observe
1455887d9dc9SPeter Zijlstra  * a false negative if the read side got smeared over multiple consequtive
1456887d9dc9SPeter Zijlstra  * __run_hrtimer() invocations.
1457887d9dc9SPeter Zijlstra  */
1458887d9dc9SPeter Zijlstra 
145921d6d52aSThomas Gleixner static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
146021d6d52aSThomas Gleixner 			  struct hrtimer_clock_base *base,
1461dd934aa8SAnna-Maria Gleixner 			  struct hrtimer *timer, ktime_t *now,
1462dd934aa8SAnna-Maria Gleixner 			  unsigned long flags)
14635cee9645SThomas Gleixner {
14645cee9645SThomas Gleixner 	enum hrtimer_restart (*fn)(struct hrtimer *);
14655cee9645SThomas Gleixner 	int restart;
14665cee9645SThomas Gleixner 
1467887d9dc9SPeter Zijlstra 	lockdep_assert_held(&cpu_base->lock);
14685cee9645SThomas Gleixner 
14695cee9645SThomas Gleixner 	debug_deactivate(timer);
14703f0b9e8eSAnna-Maria Gleixner 	base->running = timer;
1471887d9dc9SPeter Zijlstra 
1472887d9dc9SPeter Zijlstra 	/*
1473887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1474887d9dc9SPeter Zijlstra 	 *
1475887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
14763f0b9e8eSAnna-Maria Gleixner 	 * hrtimer_active() cannot observe base->running == NULL &&
1477887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
1478887d9dc9SPeter Zijlstra 	 */
14793f0b9e8eSAnna-Maria Gleixner 	raw_write_seqcount_barrier(&base->seq);
1480887d9dc9SPeter Zijlstra 
1481887d9dc9SPeter Zijlstra 	__remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
14825cee9645SThomas Gleixner 	fn = timer->function;
14835cee9645SThomas Gleixner 
14845cee9645SThomas Gleixner 	/*
1485203cbf77SThomas Gleixner 	 * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
1486203cbf77SThomas Gleixner 	 * timer is restarted with a period then it becomes an absolute
1487203cbf77SThomas Gleixner 	 * timer. If its not restarted it does not matter.
1488203cbf77SThomas Gleixner 	 */
1489203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES))
1490203cbf77SThomas Gleixner 		timer->is_rel = false;
1491203cbf77SThomas Gleixner 
1492203cbf77SThomas Gleixner 	/*
1493d05ca13bSThomas Gleixner 	 * The timer is marked as running in the CPU base, so it is
1494d05ca13bSThomas Gleixner 	 * protected against migration to a different CPU even if the lock
1495d05ca13bSThomas Gleixner 	 * is dropped.
14965cee9645SThomas Gleixner 	 */
1497dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
14985cee9645SThomas Gleixner 	trace_hrtimer_expire_entry(timer, now);
14995cee9645SThomas Gleixner 	restart = fn(timer);
15005cee9645SThomas Gleixner 	trace_hrtimer_expire_exit(timer);
1501dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irq(&cpu_base->lock);
15025cee9645SThomas Gleixner 
15035cee9645SThomas Gleixner 	/*
1504887d9dc9SPeter Zijlstra 	 * Note: We clear the running state after enqueue_hrtimer and
1505b4d90e9fSPratyush Patel 	 * we do not reprogram the event hardware. Happens either in
15065cee9645SThomas Gleixner 	 * hrtimer_start_range_ns() or in hrtimer_interrupt()
15075de2755cSPeter Zijlstra 	 *
15085de2755cSPeter Zijlstra 	 * Note: Because we dropped the cpu_base->lock above,
15095de2755cSPeter Zijlstra 	 * hrtimer_start_range_ns() can have popped in and enqueued the timer
15105de2755cSPeter Zijlstra 	 * for us already.
15115cee9645SThomas Gleixner 	 */
15125de2755cSPeter Zijlstra 	if (restart != HRTIMER_NORESTART &&
15135de2755cSPeter Zijlstra 	    !(timer->state & HRTIMER_STATE_ENQUEUED))
151463e2ed36SAnna-Maria Gleixner 		enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS);
15155cee9645SThomas Gleixner 
15165cee9645SThomas Gleixner 	/*
1517887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1518887d9dc9SPeter Zijlstra 	 *
1519887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
15203f0b9e8eSAnna-Maria Gleixner 	 * hrtimer_active() cannot observe base->running.timer == NULL &&
1521887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
15225cee9645SThomas Gleixner 	 */
15233f0b9e8eSAnna-Maria Gleixner 	raw_write_seqcount_barrier(&base->seq);
15245cee9645SThomas Gleixner 
15253f0b9e8eSAnna-Maria Gleixner 	WARN_ON_ONCE(base->running != timer);
15263f0b9e8eSAnna-Maria Gleixner 	base->running = NULL;
15275cee9645SThomas Gleixner }
15285cee9645SThomas Gleixner 
1529dd934aa8SAnna-Maria Gleixner static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
1530c458b1d1SAnna-Maria Gleixner 				 unsigned long flags, unsigned int active_mask)
15315cee9645SThomas Gleixner {
1532c272ca58SAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1533c458b1d1SAnna-Maria Gleixner 	unsigned int active = cpu_base->active_bases & active_mask;
15345cee9645SThomas Gleixner 
1535c272ca58SAnna-Maria Gleixner 	for_each_active_base(base, cpu_base, active) {
15365cee9645SThomas Gleixner 		struct timerqueue_node *node;
15375cee9645SThomas Gleixner 		ktime_t basenow;
15385cee9645SThomas Gleixner 
15395cee9645SThomas Gleixner 		basenow = ktime_add(now, base->offset);
15405cee9645SThomas Gleixner 
15415cee9645SThomas Gleixner 		while ((node = timerqueue_getnext(&base->active))) {
15425cee9645SThomas Gleixner 			struct hrtimer *timer;
15435cee9645SThomas Gleixner 
15445cee9645SThomas Gleixner 			timer = container_of(node, struct hrtimer, node);
15455cee9645SThomas Gleixner 
15465cee9645SThomas Gleixner 			/*
15475cee9645SThomas Gleixner 			 * The immediate goal for using the softexpires is
15485cee9645SThomas Gleixner 			 * minimizing wakeups, not running timers at the
15495cee9645SThomas Gleixner 			 * earliest interrupt after their soft expiration.
15505cee9645SThomas Gleixner 			 * This allows us to avoid using a Priority Search
15515cee9645SThomas Gleixner 			 * Tree, which can answer a stabbing querry for
15525cee9645SThomas Gleixner 			 * overlapping intervals and instead use the simple
15535cee9645SThomas Gleixner 			 * BST we already have.
15545cee9645SThomas Gleixner 			 * We don't add extra wakeups by delaying timers that
15555cee9645SThomas Gleixner 			 * are right-of a not yet expired timer, because that
15565cee9645SThomas Gleixner 			 * timer will have to trigger a wakeup anyway.
15575cee9645SThomas Gleixner 			 */
15582456e855SThomas Gleixner 			if (basenow < hrtimer_get_softexpires_tv64(timer))
15595cee9645SThomas Gleixner 				break;
15605cee9645SThomas Gleixner 
1561dd934aa8SAnna-Maria Gleixner 			__run_hrtimer(cpu_base, base, timer, &basenow, flags);
1562f61eff83SAnna-Maria Gleixner 			if (active_mask == HRTIMER_ACTIVE_SOFT)
1563f61eff83SAnna-Maria Gleixner 				hrtimer_sync_wait_running(cpu_base, flags);
15645cee9645SThomas Gleixner 		}
15655cee9645SThomas Gleixner 	}
156621d6d52aSThomas Gleixner }
156721d6d52aSThomas Gleixner 
15685da70160SAnna-Maria Gleixner static __latent_entropy void hrtimer_run_softirq(struct softirq_action *h)
15695da70160SAnna-Maria Gleixner {
15705da70160SAnna-Maria Gleixner 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
15715da70160SAnna-Maria Gleixner 	unsigned long flags;
15725da70160SAnna-Maria Gleixner 	ktime_t now;
15735da70160SAnna-Maria Gleixner 
1574f61eff83SAnna-Maria Gleixner 	hrtimer_cpu_base_lock_expiry(cpu_base);
15755da70160SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
15765da70160SAnna-Maria Gleixner 
15775da70160SAnna-Maria Gleixner 	now = hrtimer_update_base(cpu_base);
15785da70160SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_SOFT);
15795da70160SAnna-Maria Gleixner 
15805da70160SAnna-Maria Gleixner 	cpu_base->softirq_activated = 0;
15815da70160SAnna-Maria Gleixner 	hrtimer_update_softirq_timer(cpu_base, true);
15825da70160SAnna-Maria Gleixner 
15835da70160SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1584f61eff83SAnna-Maria Gleixner 	hrtimer_cpu_base_unlock_expiry(cpu_base);
15855da70160SAnna-Maria Gleixner }
15865da70160SAnna-Maria Gleixner 
158721d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
158821d6d52aSThomas Gleixner 
158921d6d52aSThomas Gleixner /*
159021d6d52aSThomas Gleixner  * High resolution timer interrupt
159121d6d52aSThomas Gleixner  * Called with interrupts disabled
159221d6d52aSThomas Gleixner  */
159321d6d52aSThomas Gleixner void hrtimer_interrupt(struct clock_event_device *dev)
159421d6d52aSThomas Gleixner {
159521d6d52aSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
159621d6d52aSThomas Gleixner 	ktime_t expires_next, now, entry_time, delta;
1597dd934aa8SAnna-Maria Gleixner 	unsigned long flags;
159821d6d52aSThomas Gleixner 	int retries = 0;
159921d6d52aSThomas Gleixner 
160021d6d52aSThomas Gleixner 	BUG_ON(!cpu_base->hres_active);
160121d6d52aSThomas Gleixner 	cpu_base->nr_events++;
16022456e855SThomas Gleixner 	dev->next_event = KTIME_MAX;
160321d6d52aSThomas Gleixner 
1604dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
160521d6d52aSThomas Gleixner 	entry_time = now = hrtimer_update_base(cpu_base);
160621d6d52aSThomas Gleixner retry:
160721d6d52aSThomas Gleixner 	cpu_base->in_hrtirq = 1;
160821d6d52aSThomas Gleixner 	/*
160921d6d52aSThomas Gleixner 	 * We set expires_next to KTIME_MAX here with cpu_base->lock
161021d6d52aSThomas Gleixner 	 * held to prevent that a timer is enqueued in our queue via
161121d6d52aSThomas Gleixner 	 * the migration code. This does not affect enqueueing of
161221d6d52aSThomas Gleixner 	 * timers which run their callback and need to be requeued on
161321d6d52aSThomas Gleixner 	 * this CPU.
161421d6d52aSThomas Gleixner 	 */
16152456e855SThomas Gleixner 	cpu_base->expires_next = KTIME_MAX;
161621d6d52aSThomas Gleixner 
16175da70160SAnna-Maria Gleixner 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
16185da70160SAnna-Maria Gleixner 		cpu_base->softirq_expires_next = KTIME_MAX;
16195da70160SAnna-Maria Gleixner 		cpu_base->softirq_activated = 1;
16205da70160SAnna-Maria Gleixner 		raise_softirq_irqoff(HRTIMER_SOFTIRQ);
16215da70160SAnna-Maria Gleixner 	}
16225da70160SAnna-Maria Gleixner 
1623c458b1d1SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
162421d6d52aSThomas Gleixner 
16259bc74919SThomas Gleixner 	/* Reevaluate the clock bases for the next expiry */
16265da70160SAnna-Maria Gleixner 	expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
16275cee9645SThomas Gleixner 	/*
16285cee9645SThomas Gleixner 	 * Store the new expiry value so the migration code can verify
16295cee9645SThomas Gleixner 	 * against it.
16305cee9645SThomas Gleixner 	 */
16315cee9645SThomas Gleixner 	cpu_base->expires_next = expires_next;
16329bc74919SThomas Gleixner 	cpu_base->in_hrtirq = 0;
1633dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
16345cee9645SThomas Gleixner 
16355cee9645SThomas Gleixner 	/* Reprogramming necessary ? */
1636d2540875SViresh Kumar 	if (!tick_program_event(expires_next, 0)) {
16375cee9645SThomas Gleixner 		cpu_base->hang_detected = 0;
16385cee9645SThomas Gleixner 		return;
16395cee9645SThomas Gleixner 	}
16405cee9645SThomas Gleixner 
16415cee9645SThomas Gleixner 	/*
16425cee9645SThomas Gleixner 	 * The next timer was already expired due to:
16435cee9645SThomas Gleixner 	 * - tracing
16445cee9645SThomas Gleixner 	 * - long lasting callbacks
16455cee9645SThomas Gleixner 	 * - being scheduled away when running in a VM
16465cee9645SThomas Gleixner 	 *
16475cee9645SThomas Gleixner 	 * We need to prevent that we loop forever in the hrtimer
16485cee9645SThomas Gleixner 	 * interrupt routine. We give it 3 attempts to avoid
16495cee9645SThomas Gleixner 	 * overreacting on some spurious event.
16505cee9645SThomas Gleixner 	 *
16515cee9645SThomas Gleixner 	 * Acquire base lock for updating the offsets and retrieving
16525cee9645SThomas Gleixner 	 * the current time.
16535cee9645SThomas Gleixner 	 */
1654dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
16555cee9645SThomas Gleixner 	now = hrtimer_update_base(cpu_base);
16565cee9645SThomas Gleixner 	cpu_base->nr_retries++;
16575cee9645SThomas Gleixner 	if (++retries < 3)
16585cee9645SThomas Gleixner 		goto retry;
16595cee9645SThomas Gleixner 	/*
16605cee9645SThomas Gleixner 	 * Give the system a chance to do something else than looping
16615cee9645SThomas Gleixner 	 * here. We stored the entry time, so we know exactly how long
16625cee9645SThomas Gleixner 	 * we spent here. We schedule the next event this amount of
16635cee9645SThomas Gleixner 	 * time away.
16645cee9645SThomas Gleixner 	 */
16655cee9645SThomas Gleixner 	cpu_base->nr_hangs++;
16665cee9645SThomas Gleixner 	cpu_base->hang_detected = 1;
1667dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1668dd934aa8SAnna-Maria Gleixner 
16695cee9645SThomas Gleixner 	delta = ktime_sub(now, entry_time);
16702456e855SThomas Gleixner 	if ((unsigned int)delta > cpu_base->max_hang_time)
16712456e855SThomas Gleixner 		cpu_base->max_hang_time = (unsigned int) delta;
16725cee9645SThomas Gleixner 	/*
16735cee9645SThomas Gleixner 	 * Limit it to a sensible value as we enforce a longer
16745cee9645SThomas Gleixner 	 * delay. Give the CPU at least 100ms to catch up.
16755cee9645SThomas Gleixner 	 */
16762456e855SThomas Gleixner 	if (delta > 100 * NSEC_PER_MSEC)
16775cee9645SThomas Gleixner 		expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
16785cee9645SThomas Gleixner 	else
16795cee9645SThomas Gleixner 		expires_next = ktime_add(now, delta);
16805cee9645SThomas Gleixner 	tick_program_event(expires_next, 1);
16817a6e5537SGeert Uytterhoeven 	pr_warn_once("hrtimer: interrupt took %llu ns\n", ktime_to_ns(delta));
16825cee9645SThomas Gleixner }
16835cee9645SThomas Gleixner 
1684016da201SStephen Boyd /* called with interrupts disabled */
1685c6eb3f70SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void)
16865cee9645SThomas Gleixner {
16875cee9645SThomas Gleixner 	struct tick_device *td;
16885cee9645SThomas Gleixner 
16895cee9645SThomas Gleixner 	if (!hrtimer_hres_active())
16905cee9645SThomas Gleixner 		return;
16915cee9645SThomas Gleixner 
169222127e93SChristoph Lameter 	td = this_cpu_ptr(&tick_cpu_device);
16935cee9645SThomas Gleixner 	if (td && td->evtdev)
16945cee9645SThomas Gleixner 		hrtimer_interrupt(td->evtdev);
16955cee9645SThomas Gleixner }
16965cee9645SThomas Gleixner 
16975cee9645SThomas Gleixner #else /* CONFIG_HIGH_RES_TIMERS */
16985cee9645SThomas Gleixner 
16995cee9645SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void) { }
17005cee9645SThomas Gleixner 
17015cee9645SThomas Gleixner #endif	/* !CONFIG_HIGH_RES_TIMERS */
17025cee9645SThomas Gleixner 
17035cee9645SThomas Gleixner /*
1704c6eb3f70SThomas Gleixner  * Called from run_local_timers in hardirq context every jiffy
17055cee9645SThomas Gleixner  */
17065cee9645SThomas Gleixner void hrtimer_run_queues(void)
17075cee9645SThomas Gleixner {
1708dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1709dd934aa8SAnna-Maria Gleixner 	unsigned long flags;
171021d6d52aSThomas Gleixner 	ktime_t now;
17115cee9645SThomas Gleixner 
1712e19ffe8bSThomas Gleixner 	if (__hrtimer_hres_active(cpu_base))
17135cee9645SThomas Gleixner 		return;
17145cee9645SThomas Gleixner 
1715c6eb3f70SThomas Gleixner 	/*
1716c6eb3f70SThomas Gleixner 	 * This _is_ ugly: We have to check periodically, whether we
1717c6eb3f70SThomas Gleixner 	 * can switch to highres and / or nohz mode. The clocksource
1718c6eb3f70SThomas Gleixner 	 * switch happens with xtime_lock held. Notification from
1719c6eb3f70SThomas Gleixner 	 * there only sets the check bit in the tick_oneshot code,
1720c6eb3f70SThomas Gleixner 	 * otherwise we might deadlock vs. xtime_lock.
1721c6eb3f70SThomas Gleixner 	 */
1722c6eb3f70SThomas Gleixner 	if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
1723c6eb3f70SThomas Gleixner 		hrtimer_switch_to_hres();
1724c6eb3f70SThomas Gleixner 		return;
17255cee9645SThomas Gleixner 	}
17265cee9645SThomas Gleixner 
1727dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
172821d6d52aSThomas Gleixner 	now = hrtimer_update_base(cpu_base);
17295da70160SAnna-Maria Gleixner 
17305da70160SAnna-Maria Gleixner 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
17315da70160SAnna-Maria Gleixner 		cpu_base->softirq_expires_next = KTIME_MAX;
17325da70160SAnna-Maria Gleixner 		cpu_base->softirq_activated = 1;
17335da70160SAnna-Maria Gleixner 		raise_softirq_irqoff(HRTIMER_SOFTIRQ);
17345da70160SAnna-Maria Gleixner 	}
17355da70160SAnna-Maria Gleixner 
1736c458b1d1SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
1737dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
17385cee9645SThomas Gleixner }
17395cee9645SThomas Gleixner 
17405cee9645SThomas Gleixner /*
17415cee9645SThomas Gleixner  * Sleep related functions:
17425cee9645SThomas Gleixner  */
17435cee9645SThomas Gleixner static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
17445cee9645SThomas Gleixner {
17455cee9645SThomas Gleixner 	struct hrtimer_sleeper *t =
17465cee9645SThomas Gleixner 		container_of(timer, struct hrtimer_sleeper, timer);
17475cee9645SThomas Gleixner 	struct task_struct *task = t->task;
17485cee9645SThomas Gleixner 
17495cee9645SThomas Gleixner 	t->task = NULL;
17505cee9645SThomas Gleixner 	if (task)
17515cee9645SThomas Gleixner 		wake_up_process(task);
17525cee9645SThomas Gleixner 
17535cee9645SThomas Gleixner 	return HRTIMER_NORESTART;
17545cee9645SThomas Gleixner }
17555cee9645SThomas Gleixner 
175601656464SThomas Gleixner /**
175701656464SThomas Gleixner  * hrtimer_sleeper_start_expires - Start a hrtimer sleeper timer
175801656464SThomas Gleixner  * @sl:		sleeper to be started
175901656464SThomas Gleixner  * @mode:	timer mode abs/rel
176001656464SThomas Gleixner  *
176101656464SThomas Gleixner  * Wrapper around hrtimer_start_expires() for hrtimer_sleeper based timers
176201656464SThomas Gleixner  * to allow PREEMPT_RT to tweak the delivery mode (soft/hardirq context)
176301656464SThomas Gleixner  */
176401656464SThomas Gleixner void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
176501656464SThomas Gleixner 				   enum hrtimer_mode mode)
176601656464SThomas Gleixner {
17671842f5a4SSebastian Andrzej Siewior 	/*
17681842f5a4SSebastian Andrzej Siewior 	 * Make the enqueue delivery mode check work on RT. If the sleeper
17691842f5a4SSebastian Andrzej Siewior 	 * was initialized for hard interrupt delivery, force the mode bit.
17701842f5a4SSebastian Andrzej Siewior 	 * This is a special case for hrtimer_sleepers because
17711842f5a4SSebastian Andrzej Siewior 	 * hrtimer_init_sleeper() determines the delivery mode on RT so the
17721842f5a4SSebastian Andrzej Siewior 	 * fiddling with this decision is avoided at the call sites.
17731842f5a4SSebastian Andrzej Siewior 	 */
17741842f5a4SSebastian Andrzej Siewior 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && sl->timer.is_hard)
17751842f5a4SSebastian Andrzej Siewior 		mode |= HRTIMER_MODE_HARD;
17761842f5a4SSebastian Andrzej Siewior 
177701656464SThomas Gleixner 	hrtimer_start_expires(&sl->timer, mode);
177801656464SThomas Gleixner }
177901656464SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_sleeper_start_expires);
178001656464SThomas Gleixner 
1781dbc1625fSSebastian Andrzej Siewior static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
1782dbc1625fSSebastian Andrzej Siewior 				   clockid_t clock_id, enum hrtimer_mode mode)
17835cee9645SThomas Gleixner {
17841842f5a4SSebastian Andrzej Siewior 	/*
17851842f5a4SSebastian Andrzej Siewior 	 * On PREEMPT_RT enabled kernels hrtimers which are not explicitely
17861842f5a4SSebastian Andrzej Siewior 	 * marked for hard interrupt expiry mode are moved into soft
17871842f5a4SSebastian Andrzej Siewior 	 * interrupt context either for latency reasons or because the
17881842f5a4SSebastian Andrzej Siewior 	 * hrtimer callback takes regular spinlocks or invokes other
17891842f5a4SSebastian Andrzej Siewior 	 * functions which are not suitable for hard interrupt context on
17901842f5a4SSebastian Andrzej Siewior 	 * PREEMPT_RT.
17911842f5a4SSebastian Andrzej Siewior 	 *
17921842f5a4SSebastian Andrzej Siewior 	 * The hrtimer_sleeper callback is RT compatible in hard interrupt
17931842f5a4SSebastian Andrzej Siewior 	 * context, but there is a latency concern: Untrusted userspace can
17941842f5a4SSebastian Andrzej Siewior 	 * spawn many threads which arm timers for the same expiry time on
17951842f5a4SSebastian Andrzej Siewior 	 * the same CPU. That causes a latency spike due to the wakeup of
17961842f5a4SSebastian Andrzej Siewior 	 * a gazillion threads.
17971842f5a4SSebastian Andrzej Siewior 	 *
17981842f5a4SSebastian Andrzej Siewior 	 * OTOH, priviledged real-time user space applications rely on the
17991842f5a4SSebastian Andrzej Siewior 	 * low latency of hard interrupt wakeups. If the current task is in
18001842f5a4SSebastian Andrzej Siewior 	 * a real-time scheduling class, mark the mode for hard interrupt
18011842f5a4SSebastian Andrzej Siewior 	 * expiry.
18021842f5a4SSebastian Andrzej Siewior 	 */
18031842f5a4SSebastian Andrzej Siewior 	if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
18041842f5a4SSebastian Andrzej Siewior 		if (task_is_realtime(current) && !(mode & HRTIMER_MODE_SOFT))
18051842f5a4SSebastian Andrzej Siewior 			mode |= HRTIMER_MODE_HARD;
18061842f5a4SSebastian Andrzej Siewior 	}
18071842f5a4SSebastian Andrzej Siewior 
1808dbc1625fSSebastian Andrzej Siewior 	__hrtimer_init(&sl->timer, clock_id, mode);
18095cee9645SThomas Gleixner 	sl->timer.function = hrtimer_wakeup;
1810b7449487SThomas Gleixner 	sl->task = current;
18115cee9645SThomas Gleixner }
1812dbc1625fSSebastian Andrzej Siewior 
1813dbc1625fSSebastian Andrzej Siewior /**
1814dbc1625fSSebastian Andrzej Siewior  * hrtimer_init_sleeper - initialize sleeper to the given clock
1815dbc1625fSSebastian Andrzej Siewior  * @sl:		sleeper to be initialized
1816dbc1625fSSebastian Andrzej Siewior  * @clock_id:	the clock to be used
1817dbc1625fSSebastian Andrzej Siewior  * @mode:	timer mode abs/rel
1818dbc1625fSSebastian Andrzej Siewior  */
1819dbc1625fSSebastian Andrzej Siewior void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
1820dbc1625fSSebastian Andrzej Siewior 			  enum hrtimer_mode mode)
1821dbc1625fSSebastian Andrzej Siewior {
1822dbc1625fSSebastian Andrzej Siewior 	debug_init(&sl->timer, clock_id, mode);
1823dbc1625fSSebastian Andrzej Siewior 	__hrtimer_init_sleeper(sl, clock_id, mode);
1824dbc1625fSSebastian Andrzej Siewior 
1825dbc1625fSSebastian Andrzej Siewior }
18265cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
18275cee9645SThomas Gleixner 
1828c0edd7c9SDeepa Dinamani int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
1829ce41aaf4SAl Viro {
1830ce41aaf4SAl Viro 	switch(restart->nanosleep.type) {
18310fe27955SArnd Bergmann #ifdef CONFIG_COMPAT_32BIT_TIME
1832ce41aaf4SAl Viro 	case TT_COMPAT:
18339afc5eeeSArnd Bergmann 		if (put_old_timespec32(ts, restart->nanosleep.compat_rmtp))
1834ce41aaf4SAl Viro 			return -EFAULT;
1835ce41aaf4SAl Viro 		break;
1836ce41aaf4SAl Viro #endif
1837ce41aaf4SAl Viro 	case TT_NATIVE:
1838c0edd7c9SDeepa Dinamani 		if (put_timespec64(ts, restart->nanosleep.rmtp))
1839ce41aaf4SAl Viro 			return -EFAULT;
1840ce41aaf4SAl Viro 		break;
1841ce41aaf4SAl Viro 	default:
1842ce41aaf4SAl Viro 		BUG();
1843ce41aaf4SAl Viro 	}
1844ce41aaf4SAl Viro 	return -ERESTART_RESTARTBLOCK;
1845ce41aaf4SAl Viro }
1846ce41aaf4SAl Viro 
18475cee9645SThomas Gleixner static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
18485cee9645SThomas Gleixner {
1849edbeda46SAl Viro 	struct restart_block *restart;
1850edbeda46SAl Viro 
18515cee9645SThomas Gleixner 	do {
18525cee9645SThomas Gleixner 		set_current_state(TASK_INTERRUPTIBLE);
185301656464SThomas Gleixner 		hrtimer_sleeper_start_expires(t, mode);
18545cee9645SThomas Gleixner 
18555cee9645SThomas Gleixner 		if (likely(t->task))
18565cee9645SThomas Gleixner 			freezable_schedule();
18575cee9645SThomas Gleixner 
18585cee9645SThomas Gleixner 		hrtimer_cancel(&t->timer);
18595cee9645SThomas Gleixner 		mode = HRTIMER_MODE_ABS;
18605cee9645SThomas Gleixner 
18615cee9645SThomas Gleixner 	} while (t->task && !signal_pending(current));
18625cee9645SThomas Gleixner 
18635cee9645SThomas Gleixner 	__set_current_state(TASK_RUNNING);
18645cee9645SThomas Gleixner 
1865a7602681SAl Viro 	if (!t->task)
1866a7602681SAl Viro 		return 0;
18675cee9645SThomas Gleixner 
1868edbeda46SAl Viro 	restart = &current->restart_block;
1869edbeda46SAl Viro 	if (restart->nanosleep.type != TT_NONE) {
1870a7602681SAl Viro 		ktime_t rem = hrtimer_expires_remaining(&t->timer);
1871c0edd7c9SDeepa Dinamani 		struct timespec64 rmt;
1872edbeda46SAl Viro 
18732456e855SThomas Gleixner 		if (rem <= 0)
18745cee9645SThomas Gleixner 			return 0;
1875c0edd7c9SDeepa Dinamani 		rmt = ktime_to_timespec64(rem);
18765cee9645SThomas Gleixner 
1877ce41aaf4SAl Viro 		return nanosleep_copyout(restart, &rmt);
1878a7602681SAl Viro 	}
1879a7602681SAl Viro 	return -ERESTART_RESTARTBLOCK;
18805cee9645SThomas Gleixner }
18815cee9645SThomas Gleixner 
1882fb923c4aSAl Viro static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
18835cee9645SThomas Gleixner {
18845cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
1885a7602681SAl Viro 	int ret;
18865cee9645SThomas Gleixner 
1887dbc1625fSSebastian Andrzej Siewior 	hrtimer_init_sleeper_on_stack(&t, restart->nanosleep.clockid,
18885cee9645SThomas Gleixner 				      HRTIMER_MODE_ABS);
18895cee9645SThomas Gleixner 	hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1890a7602681SAl Viro 	ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
18915cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
18925cee9645SThomas Gleixner 	return ret;
18935cee9645SThomas Gleixner }
18945cee9645SThomas Gleixner 
1895938e7cf2SThomas Gleixner long hrtimer_nanosleep(const struct timespec64 *rqtp,
18965cee9645SThomas Gleixner 		       const enum hrtimer_mode mode, const clockid_t clockid)
18975cee9645SThomas Gleixner {
1898a7602681SAl Viro 	struct restart_block *restart;
18995cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
19005cee9645SThomas Gleixner 	int ret = 0;
1901da8b44d5SJohn Stultz 	u64 slack;
19025cee9645SThomas Gleixner 
19035cee9645SThomas Gleixner 	slack = current->timer_slack_ns;
19045cee9645SThomas Gleixner 	if (dl_task(current) || rt_task(current))
19055cee9645SThomas Gleixner 		slack = 0;
19065cee9645SThomas Gleixner 
1907dbc1625fSSebastian Andrzej Siewior 	hrtimer_init_sleeper_on_stack(&t, clockid, mode);
1908ad196384SDeepa Dinamani 	hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
1909a7602681SAl Viro 	ret = do_nanosleep(&t, mode);
1910a7602681SAl Viro 	if (ret != -ERESTART_RESTARTBLOCK)
19115cee9645SThomas Gleixner 		goto out;
19125cee9645SThomas Gleixner 
19135cee9645SThomas Gleixner 	/* Absolute timers do not update the rmtp value and restart: */
19145cee9645SThomas Gleixner 	if (mode == HRTIMER_MODE_ABS) {
19155cee9645SThomas Gleixner 		ret = -ERESTARTNOHAND;
19165cee9645SThomas Gleixner 		goto out;
19175cee9645SThomas Gleixner 	}
19185cee9645SThomas Gleixner 
1919a7602681SAl Viro 	restart = &current->restart_block;
19205cee9645SThomas Gleixner 	restart->fn = hrtimer_nanosleep_restart;
19215cee9645SThomas Gleixner 	restart->nanosleep.clockid = t.timer.base->clockid;
19225cee9645SThomas Gleixner 	restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
19235cee9645SThomas Gleixner out:
19245cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
19255cee9645SThomas Gleixner 	return ret;
19265cee9645SThomas Gleixner }
19275cee9645SThomas Gleixner 
192801909974SDeepa Dinamani #if !defined(CONFIG_64BIT_TIME) || defined(CONFIG_64BIT)
192901909974SDeepa Dinamani 
193001909974SDeepa Dinamani SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp,
193101909974SDeepa Dinamani 		struct __kernel_timespec __user *, rmtp)
19325cee9645SThomas Gleixner {
1933c0edd7c9SDeepa Dinamani 	struct timespec64 tu;
19345cee9645SThomas Gleixner 
1935c0edd7c9SDeepa Dinamani 	if (get_timespec64(&tu, rqtp))
19365cee9645SThomas Gleixner 		return -EFAULT;
19375cee9645SThomas Gleixner 
1938c0edd7c9SDeepa Dinamani 	if (!timespec64_valid(&tu))
19395cee9645SThomas Gleixner 		return -EINVAL;
19405cee9645SThomas Gleixner 
1941edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1942192a82f9SAl Viro 	current->restart_block.nanosleep.rmtp = rmtp;
1943c0edd7c9SDeepa Dinamani 	return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
19445cee9645SThomas Gleixner }
19455cee9645SThomas Gleixner 
194601909974SDeepa Dinamani #endif
194701909974SDeepa Dinamani 
1948b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
1949edbeda46SAl Viro 
19508dabe724SArnd Bergmann SYSCALL_DEFINE2(nanosleep_time32, struct old_timespec32 __user *, rqtp,
19519afc5eeeSArnd Bergmann 		       struct old_timespec32 __user *, rmtp)
1952edbeda46SAl Viro {
1953c0edd7c9SDeepa Dinamani 	struct timespec64 tu;
1954edbeda46SAl Viro 
19559afc5eeeSArnd Bergmann 	if (get_old_timespec32(&tu, rqtp))
1956edbeda46SAl Viro 		return -EFAULT;
1957edbeda46SAl Viro 
1958c0edd7c9SDeepa Dinamani 	if (!timespec64_valid(&tu))
1959edbeda46SAl Viro 		return -EINVAL;
1960edbeda46SAl Viro 
1961edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1962edbeda46SAl Viro 	current->restart_block.nanosleep.compat_rmtp = rmtp;
1963c0edd7c9SDeepa Dinamani 	return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1964edbeda46SAl Viro }
1965edbeda46SAl Viro #endif
1966edbeda46SAl Viro 
19675cee9645SThomas Gleixner /*
19685cee9645SThomas Gleixner  * Functions related to boot-time initialization:
19695cee9645SThomas Gleixner  */
197027590dc1SThomas Gleixner int hrtimers_prepare_cpu(unsigned int cpu)
19715cee9645SThomas Gleixner {
19725cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
19735cee9645SThomas Gleixner 	int i;
19745cee9645SThomas Gleixner 
19755cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
19765cee9645SThomas Gleixner 		cpu_base->clock_base[i].cpu_base = cpu_base;
19775cee9645SThomas Gleixner 		timerqueue_init_head(&cpu_base->clock_base[i].active);
19785cee9645SThomas Gleixner 	}
19795cee9645SThomas Gleixner 
1980cddd0248SViresh Kumar 	cpu_base->cpu = cpu;
1981303c146dSThomas Gleixner 	cpu_base->active_bases = 0;
198228bfd18bSAnna-Maria Gleixner 	cpu_base->hres_active = 0;
1983303c146dSThomas Gleixner 	cpu_base->hang_detected = 0;
1984303c146dSThomas Gleixner 	cpu_base->next_timer = NULL;
1985303c146dSThomas Gleixner 	cpu_base->softirq_next_timer = NULL;
198607a9a7eaSAnna-Maria Gleixner 	cpu_base->expires_next = KTIME_MAX;
19875da70160SAnna-Maria Gleixner 	cpu_base->softirq_expires_next = KTIME_MAX;
1988f61eff83SAnna-Maria Gleixner 	hrtimer_cpu_base_init_expiry_lock(cpu_base);
198927590dc1SThomas Gleixner 	return 0;
19905cee9645SThomas Gleixner }
19915cee9645SThomas Gleixner 
19925cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
19935cee9645SThomas Gleixner 
19945cee9645SThomas Gleixner static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
19955cee9645SThomas Gleixner 				struct hrtimer_clock_base *new_base)
19965cee9645SThomas Gleixner {
19975cee9645SThomas Gleixner 	struct hrtimer *timer;
19985cee9645SThomas Gleixner 	struct timerqueue_node *node;
19995cee9645SThomas Gleixner 
20005cee9645SThomas Gleixner 	while ((node = timerqueue_getnext(&old_base->active))) {
20015cee9645SThomas Gleixner 		timer = container_of(node, struct hrtimer, node);
20025cee9645SThomas Gleixner 		BUG_ON(hrtimer_callback_running(timer));
20035cee9645SThomas Gleixner 		debug_deactivate(timer);
20045cee9645SThomas Gleixner 
20055cee9645SThomas Gleixner 		/*
2006c04dca02SOleg Nesterov 		 * Mark it as ENQUEUED not INACTIVE otherwise the
20075cee9645SThomas Gleixner 		 * timer could be seen as !active and just vanish away
20085cee9645SThomas Gleixner 		 * under us on another CPU
20095cee9645SThomas Gleixner 		 */
2010c04dca02SOleg Nesterov 		__remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
20115cee9645SThomas Gleixner 		timer->base = new_base;
20125cee9645SThomas Gleixner 		/*
20135cee9645SThomas Gleixner 		 * Enqueue the timers on the new cpu. This does not
20145cee9645SThomas Gleixner 		 * reprogram the event device in case the timer
20155cee9645SThomas Gleixner 		 * expires before the earliest on this CPU, but we run
20165cee9645SThomas Gleixner 		 * hrtimer_interrupt after we migrated everything to
20175cee9645SThomas Gleixner 		 * sort out already expired timers and reprogram the
20185cee9645SThomas Gleixner 		 * event device.
20195cee9645SThomas Gleixner 		 */
202063e2ed36SAnna-Maria Gleixner 		enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS);
20215cee9645SThomas Gleixner 	}
20225cee9645SThomas Gleixner }
20235cee9645SThomas Gleixner 
202427590dc1SThomas Gleixner int hrtimers_dead_cpu(unsigned int scpu)
20255cee9645SThomas Gleixner {
20265cee9645SThomas Gleixner 	struct hrtimer_cpu_base *old_base, *new_base;
20275cee9645SThomas Gleixner 	int i;
20285cee9645SThomas Gleixner 
20295cee9645SThomas Gleixner 	BUG_ON(cpu_online(scpu));
20305cee9645SThomas Gleixner 	tick_cancel_sched_timer(scpu);
20315cee9645SThomas Gleixner 
20325da70160SAnna-Maria Gleixner 	/*
20335da70160SAnna-Maria Gleixner 	 * this BH disable ensures that raise_softirq_irqoff() does
20345da70160SAnna-Maria Gleixner 	 * not wakeup ksoftirqd (and acquire the pi-lock) while
20355da70160SAnna-Maria Gleixner 	 * holding the cpu_base lock
20365da70160SAnna-Maria Gleixner 	 */
20375da70160SAnna-Maria Gleixner 	local_bh_disable();
20385cee9645SThomas Gleixner 	local_irq_disable();
20395cee9645SThomas Gleixner 	old_base = &per_cpu(hrtimer_bases, scpu);
2040dc5df73bSChristoph Lameter 	new_base = this_cpu_ptr(&hrtimer_bases);
20415cee9645SThomas Gleixner 	/*
20425cee9645SThomas Gleixner 	 * The caller is globally serialized and nobody else
20435cee9645SThomas Gleixner 	 * takes two locks at once, deadlock is not possible.
20445cee9645SThomas Gleixner 	 */
20455cee9645SThomas Gleixner 	raw_spin_lock(&new_base->lock);
20465cee9645SThomas Gleixner 	raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
20475cee9645SThomas Gleixner 
20485cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
20495cee9645SThomas Gleixner 		migrate_hrtimer_list(&old_base->clock_base[i],
20505cee9645SThomas Gleixner 				     &new_base->clock_base[i]);
20515cee9645SThomas Gleixner 	}
20525cee9645SThomas Gleixner 
20535da70160SAnna-Maria Gleixner 	/*
20545da70160SAnna-Maria Gleixner 	 * The migration might have changed the first expiring softirq
20555da70160SAnna-Maria Gleixner 	 * timer on this CPU. Update it.
20565da70160SAnna-Maria Gleixner 	 */
20575da70160SAnna-Maria Gleixner 	hrtimer_update_softirq_timer(new_base, false);
20585da70160SAnna-Maria Gleixner 
20595cee9645SThomas Gleixner 	raw_spin_unlock(&old_base->lock);
20605cee9645SThomas Gleixner 	raw_spin_unlock(&new_base->lock);
20615cee9645SThomas Gleixner 
20625cee9645SThomas Gleixner 	/* Check, if we got expired work to do */
20635cee9645SThomas Gleixner 	__hrtimer_peek_ahead_timers();
20645cee9645SThomas Gleixner 	local_irq_enable();
20655da70160SAnna-Maria Gleixner 	local_bh_enable();
206627590dc1SThomas Gleixner 	return 0;
20675cee9645SThomas Gleixner }
20685cee9645SThomas Gleixner 
20695cee9645SThomas Gleixner #endif /* CONFIG_HOTPLUG_CPU */
20705cee9645SThomas Gleixner 
20715cee9645SThomas Gleixner void __init hrtimers_init(void)
20725cee9645SThomas Gleixner {
207327590dc1SThomas Gleixner 	hrtimers_prepare_cpu(smp_processor_id());
20745da70160SAnna-Maria Gleixner 	open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
20755cee9645SThomas Gleixner }
20765cee9645SThomas Gleixner 
20775cee9645SThomas Gleixner /**
20785cee9645SThomas Gleixner  * schedule_hrtimeout_range_clock - sleep until timeout
20795cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
20805cee9645SThomas Gleixner  * @delta:	slack in expires timeout (ktime_t)
208190777713SAnna-Maria Gleixner  * @mode:	timer mode
208290777713SAnna-Maria Gleixner  * @clock_id:	timer clock to be used
20835cee9645SThomas Gleixner  */
20845cee9645SThomas Gleixner int __sched
2085da8b44d5SJohn Stultz schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
208690777713SAnna-Maria Gleixner 			       const enum hrtimer_mode mode, clockid_t clock_id)
20875cee9645SThomas Gleixner {
20885cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
20895cee9645SThomas Gleixner 
20905cee9645SThomas Gleixner 	/*
20915cee9645SThomas Gleixner 	 * Optimize when a zero timeout value is given. It does not
20925cee9645SThomas Gleixner 	 * matter whether this is an absolute or a relative time.
20935cee9645SThomas Gleixner 	 */
20942456e855SThomas Gleixner 	if (expires && *expires == 0) {
20955cee9645SThomas Gleixner 		__set_current_state(TASK_RUNNING);
20965cee9645SThomas Gleixner 		return 0;
20975cee9645SThomas Gleixner 	}
20985cee9645SThomas Gleixner 
20995cee9645SThomas Gleixner 	/*
21005cee9645SThomas Gleixner 	 * A NULL parameter means "infinite"
21015cee9645SThomas Gleixner 	 */
21025cee9645SThomas Gleixner 	if (!expires) {
21035cee9645SThomas Gleixner 		schedule();
21045cee9645SThomas Gleixner 		return -EINTR;
21055cee9645SThomas Gleixner 	}
21065cee9645SThomas Gleixner 
2107dbc1625fSSebastian Andrzej Siewior 	hrtimer_init_sleeper_on_stack(&t, clock_id, mode);
21085cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
210901656464SThomas Gleixner 	hrtimer_sleeper_start_expires(&t, mode);
21105cee9645SThomas Gleixner 
21115cee9645SThomas Gleixner 	if (likely(t.task))
21125cee9645SThomas Gleixner 		schedule();
21135cee9645SThomas Gleixner 
21145cee9645SThomas Gleixner 	hrtimer_cancel(&t.timer);
21155cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
21165cee9645SThomas Gleixner 
21175cee9645SThomas Gleixner 	__set_current_state(TASK_RUNNING);
21185cee9645SThomas Gleixner 
21195cee9645SThomas Gleixner 	return !t.task ? 0 : -EINTR;
21205cee9645SThomas Gleixner }
21215cee9645SThomas Gleixner 
21225cee9645SThomas Gleixner /**
21235cee9645SThomas Gleixner  * schedule_hrtimeout_range - sleep until timeout
21245cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
21255cee9645SThomas Gleixner  * @delta:	slack in expires timeout (ktime_t)
212690777713SAnna-Maria Gleixner  * @mode:	timer mode
21275cee9645SThomas Gleixner  *
21285cee9645SThomas Gleixner  * Make the current task sleep until the given expiry time has
21295cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
21305cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
21315cee9645SThomas Gleixner  *
21325cee9645SThomas Gleixner  * The @delta argument gives the kernel the freedom to schedule the
21335cee9645SThomas Gleixner  * actual wakeup to a time that is both power and performance friendly.
21345cee9645SThomas Gleixner  * The kernel give the normal best effort behavior for "@expires+@delta",
21355cee9645SThomas Gleixner  * but may decide to fire the timer earlier, but no earlier than @expires.
21365cee9645SThomas Gleixner  *
21375cee9645SThomas Gleixner  * You can set the task state as follows -
21385cee9645SThomas Gleixner  *
21395cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
21404b7e9cf9SDouglas Anderson  * pass before the routine returns unless the current task is explicitly
21414b7e9cf9SDouglas Anderson  * woken up, (e.g. by wake_up_process()).
21425cee9645SThomas Gleixner  *
21435cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
21444b7e9cf9SDouglas Anderson  * delivered to the current task or the current task is explicitly woken
21454b7e9cf9SDouglas Anderson  * up.
21465cee9645SThomas Gleixner  *
21475cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
21485cee9645SThomas Gleixner  * routine returns.
21495cee9645SThomas Gleixner  *
21504b7e9cf9SDouglas Anderson  * Returns 0 when the timer has expired. If the task was woken before the
21514b7e9cf9SDouglas Anderson  * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
21524b7e9cf9SDouglas Anderson  * by an explicit wakeup, it returns -EINTR.
21535cee9645SThomas Gleixner  */
2154da8b44d5SJohn Stultz int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta,
21555cee9645SThomas Gleixner 				     const enum hrtimer_mode mode)
21565cee9645SThomas Gleixner {
21575cee9645SThomas Gleixner 	return schedule_hrtimeout_range_clock(expires, delta, mode,
21585cee9645SThomas Gleixner 					      CLOCK_MONOTONIC);
21595cee9645SThomas Gleixner }
21605cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
21615cee9645SThomas Gleixner 
21625cee9645SThomas Gleixner /**
21635cee9645SThomas Gleixner  * schedule_hrtimeout - sleep until timeout
21645cee9645SThomas Gleixner  * @expires:	timeout value (ktime_t)
216590777713SAnna-Maria Gleixner  * @mode:	timer mode
21665cee9645SThomas Gleixner  *
21675cee9645SThomas Gleixner  * Make the current task sleep until the given expiry time has
21685cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
21695cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
21705cee9645SThomas Gleixner  *
21715cee9645SThomas Gleixner  * You can set the task state as follows -
21725cee9645SThomas Gleixner  *
21735cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
21744b7e9cf9SDouglas Anderson  * pass before the routine returns unless the current task is explicitly
21754b7e9cf9SDouglas Anderson  * woken up, (e.g. by wake_up_process()).
21765cee9645SThomas Gleixner  *
21775cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
21784b7e9cf9SDouglas Anderson  * delivered to the current task or the current task is explicitly woken
21794b7e9cf9SDouglas Anderson  * up.
21805cee9645SThomas Gleixner  *
21815cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
21825cee9645SThomas Gleixner  * routine returns.
21835cee9645SThomas Gleixner  *
21844b7e9cf9SDouglas Anderson  * Returns 0 when the timer has expired. If the task was woken before the
21854b7e9cf9SDouglas Anderson  * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or
21864b7e9cf9SDouglas Anderson  * by an explicit wakeup, it returns -EINTR.
21875cee9645SThomas Gleixner  */
21885cee9645SThomas Gleixner int __sched schedule_hrtimeout(ktime_t *expires,
21895cee9645SThomas Gleixner 			       const enum hrtimer_mode mode)
21905cee9645SThomas Gleixner {
21915cee9645SThomas Gleixner 	return schedule_hrtimeout_range(expires, 0, mode);
21925cee9645SThomas Gleixner }
21935cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout);
2194