xref: /linux-6.15/kernel/time/hrtimer.c (revision 2424e146)
135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
25cee9645SThomas Gleixner /*
35cee9645SThomas Gleixner  *  Copyright(C) 2005-2006, Thomas Gleixner <[email protected]>
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>
4156c2cb10SCosta Shulyupin #include <linux/sched/isolation.h>
425cee9645SThomas Gleixner #include <linux/timer.h>
435cee9645SThomas Gleixner #include <linux/freezer.h>
44edbeda46SAl Viro #include <linux/compat.h>
455cee9645SThomas Gleixner 
467c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
475cee9645SThomas Gleixner 
485cee9645SThomas Gleixner #include <trace/events/timer.h>
495cee9645SThomas Gleixner 
50c1797bafSThomas Gleixner #include "tick-internal.h"
518b094cd0SThomas Gleixner 
525cee9645SThomas Gleixner /*
53c458b1d1SAnna-Maria Gleixner  * Masks for selecting the soft and hard context timers from
54c458b1d1SAnna-Maria Gleixner  * cpu_base->active
55c458b1d1SAnna-Maria Gleixner  */
56c458b1d1SAnna-Maria Gleixner #define MASK_SHIFT		(HRTIMER_BASE_MONOTONIC_SOFT)
57c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_HARD	((1U << MASK_SHIFT) - 1)
58c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_SOFT	(HRTIMER_ACTIVE_HARD << MASK_SHIFT)
59c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_ALL	(HRTIMER_ACTIVE_SOFT | HRTIMER_ACTIVE_HARD)
60c458b1d1SAnna-Maria Gleixner 
6153dac345SFrederic Weisbecker static void retrigger_next_event(void *arg);
6253dac345SFrederic Weisbecker 
63c458b1d1SAnna-Maria Gleixner /*
645cee9645SThomas Gleixner  * The timer bases:
655cee9645SThomas Gleixner  *
66571af55aSZhen Lei  * There are more clockids than hrtimer bases. Thus, we index
675cee9645SThomas Gleixner  * into the timer bases by the hrtimer_base_type enum. When trying
685cee9645SThomas Gleixner  * to reach a base using a clockid, hrtimer_clockid_to_base()
695cee9645SThomas Gleixner  * is used to convert from clockid to the proper hrtimer_base_type.
705cee9645SThomas Gleixner  */
715cee9645SThomas Gleixner DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
725cee9645SThomas Gleixner {
735cee9645SThomas Gleixner 	.lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
745cee9645SThomas Gleixner 	.clock_base =
755cee9645SThomas Gleixner 	{
765cee9645SThomas Gleixner 		{
775cee9645SThomas Gleixner 			.index = HRTIMER_BASE_MONOTONIC,
785cee9645SThomas Gleixner 			.clockid = CLOCK_MONOTONIC,
795cee9645SThomas Gleixner 			.get_time = &ktime_get,
805cee9645SThomas Gleixner 		},
815cee9645SThomas Gleixner 		{
825cee9645SThomas Gleixner 			.index = HRTIMER_BASE_REALTIME,
835cee9645SThomas Gleixner 			.clockid = CLOCK_REALTIME,
845cee9645SThomas Gleixner 			.get_time = &ktime_get_real,
855cee9645SThomas Gleixner 		},
865cee9645SThomas Gleixner 		{
87a3ed0e43SThomas Gleixner 			.index = HRTIMER_BASE_BOOTTIME,
88a3ed0e43SThomas Gleixner 			.clockid = CLOCK_BOOTTIME,
89a3ed0e43SThomas Gleixner 			.get_time = &ktime_get_boottime,
90a3ed0e43SThomas Gleixner 		},
91a3ed0e43SThomas Gleixner 		{
925cee9645SThomas Gleixner 			.index = HRTIMER_BASE_TAI,
935cee9645SThomas Gleixner 			.clockid = CLOCK_TAI,
945cee9645SThomas Gleixner 			.get_time = &ktime_get_clocktai,
955cee9645SThomas Gleixner 		},
9698ecadd4SAnna-Maria Gleixner 		{
9798ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_MONOTONIC_SOFT,
9898ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_MONOTONIC,
9998ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get,
10098ecadd4SAnna-Maria Gleixner 		},
10198ecadd4SAnna-Maria Gleixner 		{
10298ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_REALTIME_SOFT,
10398ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_REALTIME,
10498ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get_real,
10598ecadd4SAnna-Maria Gleixner 		},
10698ecadd4SAnna-Maria Gleixner 		{
107a3ed0e43SThomas Gleixner 			.index = HRTIMER_BASE_BOOTTIME_SOFT,
108a3ed0e43SThomas Gleixner 			.clockid = CLOCK_BOOTTIME,
109a3ed0e43SThomas Gleixner 			.get_time = &ktime_get_boottime,
110a3ed0e43SThomas Gleixner 		},
111a3ed0e43SThomas Gleixner 		{
11298ecadd4SAnna-Maria Gleixner 			.index = HRTIMER_BASE_TAI_SOFT,
11398ecadd4SAnna-Maria Gleixner 			.clockid = CLOCK_TAI,
11498ecadd4SAnna-Maria Gleixner 			.get_time = &ktime_get_clocktai,
11598ecadd4SAnna-Maria Gleixner 		},
11653dac345SFrederic Weisbecker 	},
11753dac345SFrederic Weisbecker 	.csd = CSD_INIT(retrigger_next_event, NULL)
1185cee9645SThomas Gleixner };
1195cee9645SThomas Gleixner 
hrtimer_base_is_online(struct hrtimer_cpu_base * base)12053dac345SFrederic Weisbecker static inline bool hrtimer_base_is_online(struct hrtimer_cpu_base *base)
12153dac345SFrederic Weisbecker {
12253dac345SFrederic Weisbecker 	if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
12353dac345SFrederic Weisbecker 		return true;
12453dac345SFrederic Weisbecker 	else
12553dac345SFrederic Weisbecker 		return likely(base->online);
12653dac345SFrederic Weisbecker }
12753dac345SFrederic Weisbecker 
1285cee9645SThomas Gleixner /*
1295cee9645SThomas Gleixner  * Functions and macros which are different for UP/SMP systems are kept in a
1305cee9645SThomas Gleixner  * single place
1315cee9645SThomas Gleixner  */
1325cee9645SThomas Gleixner #ifdef CONFIG_SMP
1335cee9645SThomas Gleixner 
1345cee9645SThomas Gleixner /*
135887d9dc9SPeter Zijlstra  * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base()
136887d9dc9SPeter Zijlstra  * such that hrtimer_callback_running() can unconditionally dereference
137887d9dc9SPeter Zijlstra  * timer->base->cpu_base
138887d9dc9SPeter Zijlstra  */
139887d9dc9SPeter Zijlstra static struct hrtimer_cpu_base migration_cpu_base = {
140af5a06b5SAhmed S. Darwish 	.clock_base = { {
141af5a06b5SAhmed S. Darwish 		.cpu_base = &migration_cpu_base,
142af5a06b5SAhmed S. Darwish 		.seq      = SEQCNT_RAW_SPINLOCK_ZERO(migration_cpu_base.seq,
143af5a06b5SAhmed S. Darwish 						     &migration_cpu_base.lock),
144af5a06b5SAhmed S. Darwish 	}, },
145887d9dc9SPeter Zijlstra };
146887d9dc9SPeter Zijlstra 
147887d9dc9SPeter Zijlstra #define migration_base	migration_cpu_base.clock_base[0]
148887d9dc9SPeter Zijlstra 
149887d9dc9SPeter Zijlstra /*
1505cee9645SThomas Gleixner  * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
1515cee9645SThomas Gleixner  * means that all timers which are tied to this base via timer->base are
1525cee9645SThomas Gleixner  * locked, and the base itself is locked too.
1535cee9645SThomas Gleixner  *
1545cee9645SThomas Gleixner  * So __run_timers/migrate_timers can safely modify all timers which could
1555cee9645SThomas Gleixner  * be found on the lists/queues.
1565cee9645SThomas Gleixner  *
1575cee9645SThomas Gleixner  * When the timer's base is locked, and the timer removed from list, it is
158887d9dc9SPeter Zijlstra  * possible to set timer->base = &migration_base and drop the lock: the timer
159887d9dc9SPeter Zijlstra  * remains locked.
1605cee9645SThomas Gleixner  */
1615cee9645SThomas Gleixner static
lock_hrtimer_base(const struct hrtimer * timer,unsigned long * flags)1625cee9645SThomas Gleixner struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
1635cee9645SThomas Gleixner 					     unsigned long *flags)
164ccaa4926SBen Dooks 	__acquires(&timer->base->lock)
1655cee9645SThomas Gleixner {
1665cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
1675cee9645SThomas Gleixner 
1685cee9645SThomas Gleixner 	for (;;) {
169ff229eeeSEric Dumazet 		base = READ_ONCE(timer->base);
170887d9dc9SPeter Zijlstra 		if (likely(base != &migration_base)) {
1715cee9645SThomas Gleixner 			raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
1725cee9645SThomas Gleixner 			if (likely(base == timer->base))
1735cee9645SThomas Gleixner 				return base;
1745cee9645SThomas Gleixner 			/* The timer has migrated to another CPU: */
1755cee9645SThomas Gleixner 			raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
1765cee9645SThomas Gleixner 		}
1775cee9645SThomas Gleixner 		cpu_relax();
1785cee9645SThomas Gleixner 	}
1795cee9645SThomas Gleixner }
1805cee9645SThomas Gleixner 
1815cee9645SThomas Gleixner /*
18253dac345SFrederic Weisbecker  * Check if the elected target is suitable considering its next
18353dac345SFrederic Weisbecker  * event and the hotplug state of the current CPU.
18453dac345SFrederic Weisbecker  *
18553dac345SFrederic Weisbecker  * If the elected target is remote and its next event is after the timer
18653dac345SFrederic Weisbecker  * to queue, then a remote reprogram is necessary. However there is no
18753dac345SFrederic Weisbecker  * guarantee the IPI handling the operation would arrive in time to meet
18853dac345SFrederic Weisbecker  * the high resolution deadline. In this case the local CPU becomes a
18953dac345SFrederic Weisbecker  * preferred target, unless it is offline.
19053dac345SFrederic Weisbecker  *
19153dac345SFrederic Weisbecker  * High and low resolution modes are handled the same way for simplicity.
1925cee9645SThomas Gleixner  *
1935cee9645SThomas Gleixner  * Called with cpu_base->lock of target cpu held.
1945cee9645SThomas Gleixner  */
hrtimer_suitable_target(struct hrtimer * timer,struct hrtimer_clock_base * new_base,struct hrtimer_cpu_base * new_cpu_base,struct hrtimer_cpu_base * this_cpu_base)19553dac345SFrederic Weisbecker static bool hrtimer_suitable_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base,
19653dac345SFrederic Weisbecker 				    struct hrtimer_cpu_base *new_cpu_base,
19753dac345SFrederic Weisbecker 				    struct hrtimer_cpu_base *this_cpu_base)
1985cee9645SThomas Gleixner {
1995cee9645SThomas Gleixner 	ktime_t expires;
2005cee9645SThomas Gleixner 
20153dac345SFrederic Weisbecker 	/*
20253dac345SFrederic Weisbecker 	 * The local CPU clockevent can be reprogrammed. Also get_target_base()
20353dac345SFrederic Weisbecker 	 * guarantees it is online.
20453dac345SFrederic Weisbecker 	 */
20553dac345SFrederic Weisbecker 	if (new_cpu_base == this_cpu_base)
20653dac345SFrederic Weisbecker 		return true;
20753dac345SFrederic Weisbecker 
20853dac345SFrederic Weisbecker 	/*
20953dac345SFrederic Weisbecker 	 * The offline local CPU can't be the default target if the
21053dac345SFrederic Weisbecker 	 * next remote target event is after this timer. Keep the
21153dac345SFrederic Weisbecker 	 * elected new base. An IPI will we issued to reprogram
21253dac345SFrederic Weisbecker 	 * it as a last resort.
21353dac345SFrederic Weisbecker 	 */
21453dac345SFrederic Weisbecker 	if (!hrtimer_base_is_online(this_cpu_base))
21553dac345SFrederic Weisbecker 		return true;
21653dac345SFrederic Weisbecker 
2175cee9645SThomas Gleixner 	expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
21853dac345SFrederic Weisbecker 
21953dac345SFrederic Weisbecker 	return expires >= new_base->cpu_base->expires_next;
2205cee9645SThomas Gleixner }
2215cee9645SThomas Gleixner 
get_target_base(struct hrtimer_cpu_base * base,int pinned)22253dac345SFrederic Weisbecker static inline struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base, int pinned)
223bc7a34b8SThomas Gleixner {
22453dac345SFrederic Weisbecker 	if (!hrtimer_base_is_online(base)) {
22553dac345SFrederic Weisbecker 		int cpu = cpumask_any_and(cpu_online_mask, housekeeping_cpumask(HK_TYPE_TIMER));
22653dac345SFrederic Weisbecker 
22753dac345SFrederic Weisbecker 		return &per_cpu(hrtimer_bases, cpu);
22853dac345SFrederic Weisbecker 	}
22953dac345SFrederic Weisbecker 
230ae67badaSThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
231ae67badaSThomas Gleixner 	if (static_branch_likely(&timers_migration_enabled) && !pinned)
232bc7a34b8SThomas Gleixner 		return &per_cpu(hrtimer_bases, get_nohz_timer_target());
233ae67badaSThomas Gleixner #endif
234662b3e19SFrederic Weisbecker 	return base;
235bc7a34b8SThomas Gleixner }
236bc7a34b8SThomas Gleixner 
2375cee9645SThomas Gleixner /*
238b48362d8SFrederic Weisbecker  * We switch the timer base to a power-optimized selected CPU target,
239b48362d8SFrederic Weisbecker  * if:
240b48362d8SFrederic Weisbecker  *	- NO_HZ_COMMON is enabled
241b48362d8SFrederic Weisbecker  *	- timer migration is enabled
242b48362d8SFrederic Weisbecker  *	- the timer callback is not running
243b48362d8SFrederic Weisbecker  *	- the timer is not the first expiring timer on the new target
244b48362d8SFrederic Weisbecker  *
245b48362d8SFrederic Weisbecker  * If one of the above requirements is not fulfilled we move the timer
246b48362d8SFrederic Weisbecker  * to the current CPU or leave it on the previously assigned CPU if
247b48362d8SFrederic Weisbecker  * the timer callback is currently running.
2485cee9645SThomas Gleixner  */
2495cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
switch_hrtimer_base(struct hrtimer * timer,struct hrtimer_clock_base * base,int pinned)2505cee9645SThomas Gleixner switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
2515cee9645SThomas Gleixner 		    int pinned)
2525cee9645SThomas Gleixner {
253b48362d8SFrederic Weisbecker 	struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base;
2545cee9645SThomas Gleixner 	struct hrtimer_clock_base *new_base;
2555cee9645SThomas Gleixner 	int basenum = base->index;
2565cee9645SThomas Gleixner 
257b48362d8SFrederic Weisbecker 	this_cpu_base = this_cpu_ptr(&hrtimer_bases);
258b48362d8SFrederic Weisbecker 	new_cpu_base = get_target_base(this_cpu_base, pinned);
2595cee9645SThomas Gleixner again:
2605cee9645SThomas Gleixner 	new_base = &new_cpu_base->clock_base[basenum];
2615cee9645SThomas Gleixner 
2625cee9645SThomas Gleixner 	if (base != new_base) {
2635cee9645SThomas Gleixner 		/*
2645cee9645SThomas Gleixner 		 * We are trying to move timer to new_base.
2655cee9645SThomas Gleixner 		 * However we can't change timer's base while it is running,
2665cee9645SThomas Gleixner 		 * so we keep it on the same CPU. No hassle vs. reprogramming
2675cee9645SThomas Gleixner 		 * the event source in the high resolution case. The softirq
2685cee9645SThomas Gleixner 		 * code will take care of this when the timer function has
2695cee9645SThomas Gleixner 		 * completed. There is no conflict as we hold the lock until
2705cee9645SThomas Gleixner 		 * the timer is enqueued.
2715cee9645SThomas Gleixner 		 */
2725cee9645SThomas Gleixner 		if (unlikely(hrtimer_callback_running(timer)))
2735cee9645SThomas Gleixner 			return base;
2745cee9645SThomas Gleixner 
275887d9dc9SPeter Zijlstra 		/* See the comment in lock_hrtimer_base() */
276ff229eeeSEric Dumazet 		WRITE_ONCE(timer->base, &migration_base);
2775cee9645SThomas Gleixner 		raw_spin_unlock(&base->cpu_base->lock);
2785cee9645SThomas Gleixner 		raw_spin_lock(&new_base->cpu_base->lock);
2795cee9645SThomas Gleixner 
28053dac345SFrederic Weisbecker 		if (!hrtimer_suitable_target(timer, new_base, new_cpu_base,
28153dac345SFrederic Weisbecker 					     this_cpu_base)) {
2825cee9645SThomas Gleixner 			raw_spin_unlock(&new_base->cpu_base->lock);
2835cee9645SThomas Gleixner 			raw_spin_lock(&base->cpu_base->lock);
284b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
285ff229eeeSEric Dumazet 			WRITE_ONCE(timer->base, base);
2865cee9645SThomas Gleixner 			goto again;
2875cee9645SThomas Gleixner 		}
288ff229eeeSEric Dumazet 		WRITE_ONCE(timer->base, new_base);
2895cee9645SThomas Gleixner 	} else {
29053dac345SFrederic Weisbecker 		if (!hrtimer_suitable_target(timer, new_base,  new_cpu_base, this_cpu_base)) {
291b48362d8SFrederic Weisbecker 			new_cpu_base = this_cpu_base;
2925cee9645SThomas Gleixner 			goto again;
2935cee9645SThomas Gleixner 		}
2945cee9645SThomas Gleixner 	}
2955cee9645SThomas Gleixner 	return new_base;
2965cee9645SThomas Gleixner }
2975cee9645SThomas Gleixner 
2985cee9645SThomas Gleixner #else /* CONFIG_SMP */
2995cee9645SThomas Gleixner 
3005cee9645SThomas Gleixner static inline struct hrtimer_clock_base *
lock_hrtimer_base(const struct hrtimer * timer,unsigned long * flags)3015cee9645SThomas Gleixner lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
302ccaa4926SBen Dooks 	__acquires(&timer->base->cpu_base->lock)
3035cee9645SThomas Gleixner {
3045cee9645SThomas Gleixner 	struct hrtimer_clock_base *base = timer->base;
3055cee9645SThomas Gleixner 
3065cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
3075cee9645SThomas Gleixner 
3085cee9645SThomas Gleixner 	return base;
3095cee9645SThomas Gleixner }
3105cee9645SThomas Gleixner 
3115cee9645SThomas Gleixner # define switch_hrtimer_base(t, b, p)	(b)
3125cee9645SThomas Gleixner 
3135cee9645SThomas Gleixner #endif	/* !CONFIG_SMP */
3145cee9645SThomas Gleixner 
3155cee9645SThomas Gleixner /*
3165cee9645SThomas Gleixner  * Functions for the union type storage format of ktime_t which are
3175cee9645SThomas Gleixner  * too large for inlining:
3185cee9645SThomas Gleixner  */
3195cee9645SThomas Gleixner #if BITS_PER_LONG < 64
3205cee9645SThomas Gleixner /*
3215cee9645SThomas Gleixner  * Divide a ktime value by a nanosecond value
3225cee9645SThomas Gleixner  */
__ktime_divns(const ktime_t kt,s64 div)323f7bcb70eSJohn Stultz s64 __ktime_divns(const ktime_t kt, s64 div)
3245cee9645SThomas Gleixner {
3255cee9645SThomas Gleixner 	int sft = 0;
326f7bcb70eSJohn Stultz 	s64 dclc;
327f7bcb70eSJohn Stultz 	u64 tmp;
3285cee9645SThomas Gleixner 
3295cee9645SThomas Gleixner 	dclc = ktime_to_ns(kt);
330f7bcb70eSJohn Stultz 	tmp = dclc < 0 ? -dclc : dclc;
331f7bcb70eSJohn Stultz 
3325cee9645SThomas Gleixner 	/* Make sure the divisor is less than 2^32: */
3335cee9645SThomas Gleixner 	while (div >> 32) {
3345cee9645SThomas Gleixner 		sft++;
3355cee9645SThomas Gleixner 		div >>= 1;
3365cee9645SThomas Gleixner 	}
337f7bcb70eSJohn Stultz 	tmp >>= sft;
33838f7b0b1SWen Yang 	do_div(tmp, (u32) div);
339f7bcb70eSJohn Stultz 	return dclc < 0 ? -tmp : tmp;
3405cee9645SThomas Gleixner }
3418b618628SNicolas Pitre EXPORT_SYMBOL_GPL(__ktime_divns);
3425cee9645SThomas Gleixner #endif /* BITS_PER_LONG >= 64 */
3435cee9645SThomas Gleixner 
3445cee9645SThomas Gleixner /*
3455cee9645SThomas Gleixner  * Add two ktime values and do a safety check for overflow:
3465cee9645SThomas Gleixner  */
ktime_add_safe(const ktime_t lhs,const ktime_t rhs)3475cee9645SThomas Gleixner ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
3485cee9645SThomas Gleixner {
349979515c5SVegard Nossum 	ktime_t res = ktime_add_unsafe(lhs, rhs);
3505cee9645SThomas Gleixner 
3515cee9645SThomas Gleixner 	/*
3525cee9645SThomas Gleixner 	 * We use KTIME_SEC_MAX here, the maximum timeout which we can
3535cee9645SThomas Gleixner 	 * return to user space in a timespec:
3545cee9645SThomas Gleixner 	 */
3552456e855SThomas Gleixner 	if (res < 0 || res < lhs || res < rhs)
3565cee9645SThomas Gleixner 		res = ktime_set(KTIME_SEC_MAX, 0);
3575cee9645SThomas Gleixner 
3585cee9645SThomas Gleixner 	return res;
3595cee9645SThomas Gleixner }
3605cee9645SThomas Gleixner 
3615cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_add_safe);
3625cee9645SThomas Gleixner 
3635cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
3645cee9645SThomas Gleixner 
365f9e62f31SStephen Boyd static const struct debug_obj_descr hrtimer_debug_descr;
3665cee9645SThomas Gleixner 
hrtimer_debug_hint(void * addr)3675cee9645SThomas Gleixner static void *hrtimer_debug_hint(void *addr)
3685cee9645SThomas Gleixner {
369*2424e146SNam Cao 	return ACCESS_PRIVATE((struct hrtimer *)addr, function);
3705cee9645SThomas Gleixner }
3715cee9645SThomas Gleixner 
3725cee9645SThomas Gleixner /*
3735cee9645SThomas Gleixner  * fixup_init is called when:
3745cee9645SThomas Gleixner  * - an active object is initialized
3755cee9645SThomas Gleixner  */
hrtimer_fixup_init(void * addr,enum debug_obj_state state)376e3252464SDu, Changbin static bool hrtimer_fixup_init(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_init(timer, &hrtimer_debug_descr);
384e3252464SDu, Changbin 		return true;
3855cee9645SThomas Gleixner 	default:
386e3252464SDu, Changbin 		return false;
3875cee9645SThomas Gleixner 	}
3885cee9645SThomas Gleixner }
3895cee9645SThomas Gleixner 
3905cee9645SThomas Gleixner /*
3915cee9645SThomas Gleixner  * fixup_activate is called when:
3925cee9645SThomas Gleixner  * - an active object is activated
393b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
3945cee9645SThomas Gleixner  */
hrtimer_fixup_activate(void * addr,enum debug_obj_state state)395e3252464SDu, Changbin static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
3965cee9645SThomas Gleixner {
3975cee9645SThomas Gleixner 	switch (state) {
3985cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
3995cee9645SThomas Gleixner 		WARN_ON(1);
400df561f66SGustavo A. R. Silva 		fallthrough;
4015cee9645SThomas Gleixner 	default:
402e3252464SDu, Changbin 		return false;
4035cee9645SThomas Gleixner 	}
4045cee9645SThomas Gleixner }
4055cee9645SThomas Gleixner 
4065cee9645SThomas Gleixner /*
4075cee9645SThomas Gleixner  * fixup_free is called when:
4085cee9645SThomas Gleixner  * - an active object is freed
4095cee9645SThomas Gleixner  */
hrtimer_fixup_free(void * addr,enum debug_obj_state state)410e3252464SDu, Changbin static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state)
4115cee9645SThomas Gleixner {
4125cee9645SThomas Gleixner 	struct hrtimer *timer = addr;
4135cee9645SThomas Gleixner 
4145cee9645SThomas Gleixner 	switch (state) {
4155cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
4165cee9645SThomas Gleixner 		hrtimer_cancel(timer);
4175cee9645SThomas Gleixner 		debug_object_free(timer, &hrtimer_debug_descr);
418e3252464SDu, Changbin 		return true;
4195cee9645SThomas Gleixner 	default:
420e3252464SDu, Changbin 		return false;
4215cee9645SThomas Gleixner 	}
4225cee9645SThomas Gleixner }
4235cee9645SThomas Gleixner 
424f9e62f31SStephen Boyd static const struct debug_obj_descr hrtimer_debug_descr = {
4255cee9645SThomas Gleixner 	.name		= "hrtimer",
4265cee9645SThomas Gleixner 	.debug_hint	= hrtimer_debug_hint,
4275cee9645SThomas Gleixner 	.fixup_init	= hrtimer_fixup_init,
4285cee9645SThomas Gleixner 	.fixup_activate	= hrtimer_fixup_activate,
4295cee9645SThomas Gleixner 	.fixup_free	= hrtimer_fixup_free,
4305cee9645SThomas Gleixner };
4315cee9645SThomas Gleixner 
debug_hrtimer_init(struct hrtimer * timer)4325cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer)
4335cee9645SThomas Gleixner {
4345cee9645SThomas Gleixner 	debug_object_init(timer, &hrtimer_debug_descr);
4355cee9645SThomas Gleixner }
4365cee9645SThomas Gleixner 
debug_hrtimer_init_on_stack(struct hrtimer * timer)437fbf920f2SNam Cao static inline void debug_hrtimer_init_on_stack(struct hrtimer *timer)
438fbf920f2SNam Cao {
439fbf920f2SNam Cao 	debug_object_init_on_stack(timer, &hrtimer_debug_descr);
440fbf920f2SNam Cao }
441fbf920f2SNam Cao 
debug_hrtimer_activate(struct hrtimer * timer,enum hrtimer_mode mode)4425da70160SAnna-Maria Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer,
4435da70160SAnna-Maria Gleixner 					  enum hrtimer_mode mode)
4445cee9645SThomas Gleixner {
4455cee9645SThomas Gleixner 	debug_object_activate(timer, &hrtimer_debug_descr);
4465cee9645SThomas Gleixner }
4475cee9645SThomas Gleixner 
debug_hrtimer_deactivate(struct hrtimer * timer)4485cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
4495cee9645SThomas Gleixner {
4505cee9645SThomas Gleixner 	debug_object_deactivate(timer, &hrtimer_debug_descr);
4515cee9645SThomas Gleixner }
4525cee9645SThomas Gleixner 
destroy_hrtimer_on_stack(struct hrtimer * timer)4535cee9645SThomas Gleixner void destroy_hrtimer_on_stack(struct hrtimer *timer)
4545cee9645SThomas Gleixner {
4555cee9645SThomas Gleixner 	debug_object_free(timer, &hrtimer_debug_descr);
4565cee9645SThomas Gleixner }
457c08376acSGuenter Roeck EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack);
4585cee9645SThomas Gleixner 
4595cee9645SThomas Gleixner #else
4605da70160SAnna-Maria Gleixner 
debug_hrtimer_init(struct hrtimer * timer)4615cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer) { }
debug_hrtimer_init_on_stack(struct hrtimer * timer)462fbf920f2SNam Cao static inline void debug_hrtimer_init_on_stack(struct hrtimer *timer) { }
debug_hrtimer_activate(struct hrtimer * timer,enum hrtimer_mode mode)4635da70160SAnna-Maria Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer,
4645da70160SAnna-Maria Gleixner 					  enum hrtimer_mode mode) { }
debug_hrtimer_deactivate(struct hrtimer * timer)4655cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
4665cee9645SThomas Gleixner #endif
4675cee9645SThomas Gleixner 
debug_setup(struct hrtimer * timer,clockid_t clockid,enum hrtimer_mode mode)468e9ef2093SNam Cao static inline void debug_setup(struct hrtimer *timer, clockid_t clockid, enum hrtimer_mode mode)
4695cee9645SThomas Gleixner {
4705cee9645SThomas Gleixner 	debug_hrtimer_init(timer);
471244132c4SNam Cao 	trace_hrtimer_setup(timer, clockid, mode);
4725cee9645SThomas Gleixner }
4735cee9645SThomas Gleixner 
debug_setup_on_stack(struct hrtimer * timer,clockid_t clockid,enum hrtimer_mode mode)47459c9edafSNam Cao static inline void debug_setup_on_stack(struct hrtimer *timer, clockid_t clockid,
475fbf920f2SNam Cao 					enum hrtimer_mode mode)
476fbf920f2SNam Cao {
477fbf920f2SNam Cao 	debug_hrtimer_init_on_stack(timer);
478244132c4SNam Cao 	trace_hrtimer_setup(timer, clockid, mode);
479fbf920f2SNam Cao }
480fbf920f2SNam Cao 
debug_activate(struct hrtimer * timer,enum hrtimer_mode mode)48163e2ed36SAnna-Maria Gleixner static inline void debug_activate(struct hrtimer *timer,
48263e2ed36SAnna-Maria Gleixner 				  enum hrtimer_mode mode)
4835cee9645SThomas Gleixner {
4845da70160SAnna-Maria Gleixner 	debug_hrtimer_activate(timer, mode);
48563e2ed36SAnna-Maria Gleixner 	trace_hrtimer_start(timer, mode);
4865cee9645SThomas Gleixner }
4875cee9645SThomas Gleixner 
debug_deactivate(struct hrtimer * timer)4885cee9645SThomas Gleixner static inline void debug_deactivate(struct hrtimer *timer)
4895cee9645SThomas Gleixner {
4905cee9645SThomas Gleixner 	debug_hrtimer_deactivate(timer);
4915cee9645SThomas Gleixner 	trace_hrtimer_cancel(timer);
4925cee9645SThomas Gleixner }
4935cee9645SThomas Gleixner 
494c272ca58SAnna-Maria Gleixner static struct hrtimer_clock_base *
__next_base(struct hrtimer_cpu_base * cpu_base,unsigned int * active)495c272ca58SAnna-Maria Gleixner __next_base(struct hrtimer_cpu_base *cpu_base, unsigned int *active)
496c272ca58SAnna-Maria Gleixner {
497c272ca58SAnna-Maria Gleixner 	unsigned int idx;
498c272ca58SAnna-Maria Gleixner 
499c272ca58SAnna-Maria Gleixner 	if (!*active)
500c272ca58SAnna-Maria Gleixner 		return NULL;
501c272ca58SAnna-Maria Gleixner 
502c272ca58SAnna-Maria Gleixner 	idx = __ffs(*active);
503c272ca58SAnna-Maria Gleixner 	*active &= ~(1U << idx);
504c272ca58SAnna-Maria Gleixner 
505c272ca58SAnna-Maria Gleixner 	return &cpu_base->clock_base[idx];
506c272ca58SAnna-Maria Gleixner }
507c272ca58SAnna-Maria Gleixner 
508c272ca58SAnna-Maria Gleixner #define for_each_active_base(base, cpu_base, active)	\
509c272ca58SAnna-Maria Gleixner 	while ((base = __next_base((cpu_base), &(active))))
510c272ca58SAnna-Maria Gleixner 
__hrtimer_next_event_base(struct hrtimer_cpu_base * cpu_base,const struct hrtimer * exclude,unsigned int active,ktime_t expires_next)511ad38f596SAnna-Maria Gleixner static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base,
512a59855cdSRafael J. Wysocki 					 const struct hrtimer *exclude,
513ad38f596SAnna-Maria Gleixner 					 unsigned int active,
514ad38f596SAnna-Maria Gleixner 					 ktime_t expires_next)
5159bc74919SThomas Gleixner {
516c272ca58SAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
517ad38f596SAnna-Maria Gleixner 	ktime_t expires;
5189bc74919SThomas Gleixner 
519c272ca58SAnna-Maria Gleixner 	for_each_active_base(base, cpu_base, active) {
5209bc74919SThomas Gleixner 		struct timerqueue_node *next;
5219bc74919SThomas Gleixner 		struct hrtimer *timer;
5229bc74919SThomas Gleixner 
52334aee88aSThomas Gleixner 		next = timerqueue_getnext(&base->active);
5249bc74919SThomas Gleixner 		timer = container_of(next, struct hrtimer, node);
525a59855cdSRafael J. Wysocki 		if (timer == exclude) {
526a59855cdSRafael J. Wysocki 			/* Get to the next timer in the queue. */
5277d2f6abbSRafael J. Wysocki 			next = timerqueue_iterate_next(next);
528a59855cdSRafael J. Wysocki 			if (!next)
529a59855cdSRafael J. Wysocki 				continue;
530a59855cdSRafael J. Wysocki 
531a59855cdSRafael J. Wysocki 			timer = container_of(next, struct hrtimer, node);
532a59855cdSRafael J. Wysocki 		}
5339bc74919SThomas Gleixner 		expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
5342456e855SThomas Gleixner 		if (expires < expires_next) {
5359bc74919SThomas Gleixner 			expires_next = expires;
536a59855cdSRafael J. Wysocki 
537a59855cdSRafael J. Wysocki 			/* Skip cpu_base update if a timer is being excluded. */
538a59855cdSRafael J. Wysocki 			if (exclude)
539a59855cdSRafael J. Wysocki 				continue;
540a59855cdSRafael J. Wysocki 
5415da70160SAnna-Maria Gleixner 			if (timer->is_soft)
5425da70160SAnna-Maria Gleixner 				cpu_base->softirq_next_timer = timer;
5435da70160SAnna-Maria Gleixner 			else
544eb27926bSAnna-Maria Gleixner 				cpu_base->next_timer = timer;
545895bdfa7SThomas Gleixner 		}
5469bc74919SThomas Gleixner 	}
5479bc74919SThomas Gleixner 	/*
5489bc74919SThomas Gleixner 	 * clock_was_set() might have changed base->offset of any of
5499bc74919SThomas Gleixner 	 * the clock bases so the result might be negative. Fix it up
5509bc74919SThomas Gleixner 	 * to prevent a false positive in clockevents_program_event().
5519bc74919SThomas Gleixner 	 */
5522456e855SThomas Gleixner 	if (expires_next < 0)
5532456e855SThomas Gleixner 		expires_next = 0;
5549bc74919SThomas Gleixner 	return expires_next;
5559bc74919SThomas Gleixner }
5569bc74919SThomas Gleixner 
557c458b1d1SAnna-Maria Gleixner /*
55846eb1701SAnna-Maria Behnsen  * Recomputes cpu_base::*next_timer and returns the earliest expires_next
55946eb1701SAnna-Maria Behnsen  * but does not set cpu_base::*expires_next, that is done by
56046eb1701SAnna-Maria Behnsen  * hrtimer[_force]_reprogram and hrtimer_interrupt only. When updating
56146eb1701SAnna-Maria Behnsen  * cpu_base::*expires_next right away, reprogramming logic would no longer
56246eb1701SAnna-Maria Behnsen  * work.
563c458b1d1SAnna-Maria Gleixner  *
5645da70160SAnna-Maria Gleixner  * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
5655da70160SAnna-Maria Gleixner  * those timers will get run whenever the softirq gets handled, at the end of
5665da70160SAnna-Maria Gleixner  * hrtimer_run_softirq(), hrtimer_update_softirq_timer() will re-add these bases.
5675da70160SAnna-Maria Gleixner  *
5685da70160SAnna-Maria Gleixner  * Therefore softirq values are those from the HRTIMER_ACTIVE_SOFT clock bases.
5695da70160SAnna-Maria Gleixner  * The !softirq values are the minima across HRTIMER_ACTIVE_ALL, unless an actual
5705da70160SAnna-Maria Gleixner  * softirq is pending, in which case they're the minima of HRTIMER_ACTIVE_HARD.
5715da70160SAnna-Maria Gleixner  *
572c458b1d1SAnna-Maria Gleixner  * @active_mask must be one of:
5735da70160SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_ALL,
574c458b1d1SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_SOFT, or
575c458b1d1SAnna-Maria Gleixner  *  - HRTIMER_ACTIVE_HARD.
576c458b1d1SAnna-Maria Gleixner  */
5775da70160SAnna-Maria Gleixner static ktime_t
__hrtimer_get_next_event(struct hrtimer_cpu_base * cpu_base,unsigned int active_mask)5785da70160SAnna-Maria Gleixner __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_mask)
579ad38f596SAnna-Maria Gleixner {
580c458b1d1SAnna-Maria Gleixner 	unsigned int active;
5815da70160SAnna-Maria Gleixner 	struct hrtimer *next_timer = NULL;
582ad38f596SAnna-Maria Gleixner 	ktime_t expires_next = KTIME_MAX;
583ad38f596SAnna-Maria Gleixner 
5845da70160SAnna-Maria Gleixner 	if (!cpu_base->softirq_activated && (active_mask & HRTIMER_ACTIVE_SOFT)) {
5855da70160SAnna-Maria Gleixner 		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
5865da70160SAnna-Maria Gleixner 		cpu_base->softirq_next_timer = NULL;
587a59855cdSRafael J. Wysocki 		expires_next = __hrtimer_next_event_base(cpu_base, NULL,
588a59855cdSRafael J. Wysocki 							 active, KTIME_MAX);
589ad38f596SAnna-Maria Gleixner 
5905da70160SAnna-Maria Gleixner 		next_timer = cpu_base->softirq_next_timer;
5915da70160SAnna-Maria Gleixner 	}
5925da70160SAnna-Maria Gleixner 
5935da70160SAnna-Maria Gleixner 	if (active_mask & HRTIMER_ACTIVE_HARD) {
5945da70160SAnna-Maria Gleixner 		active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
5955da70160SAnna-Maria Gleixner 		cpu_base->next_timer = next_timer;
596a59855cdSRafael J. Wysocki 		expires_next = __hrtimer_next_event_base(cpu_base, NULL, active,
597a59855cdSRafael J. Wysocki 							 expires_next);
5985da70160SAnna-Maria Gleixner 	}
599ad38f596SAnna-Maria Gleixner 
600ad38f596SAnna-Maria Gleixner 	return expires_next;
601ad38f596SAnna-Maria Gleixner }
602ad38f596SAnna-Maria Gleixner 
hrtimer_update_next_event(struct hrtimer_cpu_base * cpu_base)60346eb1701SAnna-Maria Behnsen static ktime_t hrtimer_update_next_event(struct hrtimer_cpu_base *cpu_base)
60446eb1701SAnna-Maria Behnsen {
60546eb1701SAnna-Maria Behnsen 	ktime_t expires_next, soft = KTIME_MAX;
60646eb1701SAnna-Maria Behnsen 
60746eb1701SAnna-Maria Behnsen 	/*
60846eb1701SAnna-Maria Behnsen 	 * If the soft interrupt has already been activated, ignore the
60946eb1701SAnna-Maria Behnsen 	 * soft bases. They will be handled in the already raised soft
61046eb1701SAnna-Maria Behnsen 	 * interrupt.
61146eb1701SAnna-Maria Behnsen 	 */
61246eb1701SAnna-Maria Behnsen 	if (!cpu_base->softirq_activated) {
61346eb1701SAnna-Maria Behnsen 		soft = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
61446eb1701SAnna-Maria Behnsen 		/*
61546eb1701SAnna-Maria Behnsen 		 * Update the soft expiry time. clock_settime() might have
61646eb1701SAnna-Maria Behnsen 		 * affected it.
61746eb1701SAnna-Maria Behnsen 		 */
61846eb1701SAnna-Maria Behnsen 		cpu_base->softirq_expires_next = soft;
61946eb1701SAnna-Maria Behnsen 	}
62046eb1701SAnna-Maria Behnsen 
62146eb1701SAnna-Maria Behnsen 	expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD);
62246eb1701SAnna-Maria Behnsen 	/*
62346eb1701SAnna-Maria Behnsen 	 * If a softirq timer is expiring first, update cpu_base->next_timer
62446eb1701SAnna-Maria Behnsen 	 * and program the hardware with the soft expiry time.
62546eb1701SAnna-Maria Behnsen 	 */
62646eb1701SAnna-Maria Behnsen 	if (expires_next > soft) {
62746eb1701SAnna-Maria Behnsen 		cpu_base->next_timer = cpu_base->softirq_next_timer;
62846eb1701SAnna-Maria Behnsen 		expires_next = soft;
62946eb1701SAnna-Maria Behnsen 	}
63046eb1701SAnna-Maria Behnsen 
63146eb1701SAnna-Maria Behnsen 	return expires_next;
63246eb1701SAnna-Maria Behnsen }
63346eb1701SAnna-Maria Behnsen 
hrtimer_update_base(struct hrtimer_cpu_base * base)63421d6d52aSThomas Gleixner static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
63521d6d52aSThomas Gleixner {
63621d6d52aSThomas Gleixner 	ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
637a3ed0e43SThomas Gleixner 	ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
63821d6d52aSThomas Gleixner 	ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
63921d6d52aSThomas Gleixner 
6405da70160SAnna-Maria Gleixner 	ktime_t now = ktime_get_update_offsets_now(&base->clock_was_set_seq,
641a3ed0e43SThomas Gleixner 					    offs_real, offs_boot, offs_tai);
6425da70160SAnna-Maria Gleixner 
6435da70160SAnna-Maria Gleixner 	base->clock_base[HRTIMER_BASE_REALTIME_SOFT].offset = *offs_real;
644a3ed0e43SThomas Gleixner 	base->clock_base[HRTIMER_BASE_BOOTTIME_SOFT].offset = *offs_boot;
6455da70160SAnna-Maria Gleixner 	base->clock_base[HRTIMER_BASE_TAI_SOFT].offset = *offs_tai;
6465da70160SAnna-Maria Gleixner 
6475da70160SAnna-Maria Gleixner 	return now;
64821d6d52aSThomas Gleixner }
64921d6d52aSThomas Gleixner 
65028bfd18bSAnna-Maria Gleixner /*
65128bfd18bSAnna-Maria Gleixner  * Is the high resolution mode active ?
65228bfd18bSAnna-Maria Gleixner  */
hrtimer_hres_active(struct hrtimer_cpu_base * cpu_base)653b7c8e1f8SJiapeng Chong static inline int hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base)
65428bfd18bSAnna-Maria Gleixner {
65528bfd18bSAnna-Maria Gleixner 	return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
65628bfd18bSAnna-Maria Gleixner 		cpu_base->hres_active : 0;
65728bfd18bSAnna-Maria Gleixner }
65828bfd18bSAnna-Maria Gleixner 
__hrtimer_reprogram(struct hrtimer_cpu_base * cpu_base,struct hrtimer * next_timer,ktime_t expires_next)659f80e2148SThomas Gleixner static void __hrtimer_reprogram(struct hrtimer_cpu_base *cpu_base,
660f80e2148SThomas Gleixner 				struct hrtimer *next_timer,
661f80e2148SThomas Gleixner 				ktime_t expires_next)
6625cee9645SThomas Gleixner {
6632456e855SThomas Gleixner 	cpu_base->expires_next = expires_next;
6645cee9645SThomas Gleixner 
6655cee9645SThomas Gleixner 	/*
66661bb4bcbSAnna-Maria Gleixner 	 * If hres is not active, hardware does not have to be
66761bb4bcbSAnna-Maria Gleixner 	 * reprogrammed yet.
66861bb4bcbSAnna-Maria Gleixner 	 *
6695cee9645SThomas Gleixner 	 * If a hang was detected in the last timer interrupt then we
6705cee9645SThomas Gleixner 	 * leave the hang delay active in the hardware. We want the
6715cee9645SThomas Gleixner 	 * system to make progress. That also prevents the following
6725cee9645SThomas Gleixner 	 * scenario:
6735cee9645SThomas Gleixner 	 * T1 expires 50ms from now
6745cee9645SThomas Gleixner 	 * T2 expires 5s from now
6755cee9645SThomas Gleixner 	 *
6765cee9645SThomas Gleixner 	 * T1 is removed, so this code is called and would reprogram
6775cee9645SThomas Gleixner 	 * the hardware to 5s from now. Any hrtimer_start after that
6785cee9645SThomas Gleixner 	 * will not reprogram the hardware due to hang_detected being
6794bf07f65SIngo Molnar 	 * set. So we'd effectively block all timers until the T2 event
6805cee9645SThomas Gleixner 	 * fires.
6815cee9645SThomas Gleixner 	 */
682b7c8e1f8SJiapeng Chong 	if (!hrtimer_hres_active(cpu_base) || cpu_base->hang_detected)
6835cee9645SThomas Gleixner 		return;
6845cee9645SThomas Gleixner 
685b14bca97SPeter Zijlstra 	tick_program_event(expires_next, 1);
686b14bca97SPeter Zijlstra }
687b14bca97SPeter Zijlstra 
688b14bca97SPeter Zijlstra /*
689b14bca97SPeter Zijlstra  * Reprogram the event source with checking both queues for the
690b14bca97SPeter Zijlstra  * next event
691b14bca97SPeter Zijlstra  * Called with interrupts disabled and base->lock held
692b14bca97SPeter Zijlstra  */
693b14bca97SPeter Zijlstra static void
hrtimer_force_reprogram(struct hrtimer_cpu_base * cpu_base,int skip_equal)694b14bca97SPeter Zijlstra hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
695b14bca97SPeter Zijlstra {
696b14bca97SPeter Zijlstra 	ktime_t expires_next;
697b14bca97SPeter Zijlstra 
698b14bca97SPeter Zijlstra 	expires_next = hrtimer_update_next_event(cpu_base);
699b14bca97SPeter Zijlstra 
700f80e2148SThomas Gleixner 	if (skip_equal && expires_next == cpu_base->expires_next)
701f80e2148SThomas Gleixner 		return;
702f80e2148SThomas Gleixner 
703f80e2148SThomas Gleixner 	__hrtimer_reprogram(cpu_base, cpu_base->next_timer, expires_next);
7045cee9645SThomas Gleixner }
7055cee9645SThomas Gleixner 
706ebba2c72SAnna-Maria Gleixner /* High resolution timer related functions */
707ebba2c72SAnna-Maria Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
708ebba2c72SAnna-Maria Gleixner 
709ebba2c72SAnna-Maria Gleixner /*
710ebba2c72SAnna-Maria Gleixner  * High resolution timer enabled ?
711ebba2c72SAnna-Maria Gleixner  */
712ebba2c72SAnna-Maria Gleixner static bool hrtimer_hres_enabled __read_mostly  = true;
713ebba2c72SAnna-Maria Gleixner unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC;
714ebba2c72SAnna-Maria Gleixner EXPORT_SYMBOL_GPL(hrtimer_resolution);
715ebba2c72SAnna-Maria Gleixner 
716ebba2c72SAnna-Maria Gleixner /*
717ebba2c72SAnna-Maria Gleixner  * Enable / Disable high resolution mode
718ebba2c72SAnna-Maria Gleixner  */
setup_hrtimer_hres(char * str)719ebba2c72SAnna-Maria Gleixner static int __init setup_hrtimer_hres(char *str)
720ebba2c72SAnna-Maria Gleixner {
721ebba2c72SAnna-Maria Gleixner 	return (kstrtobool(str, &hrtimer_hres_enabled) == 0);
722ebba2c72SAnna-Maria Gleixner }
723ebba2c72SAnna-Maria Gleixner 
724ebba2c72SAnna-Maria Gleixner __setup("highres=", setup_hrtimer_hres);
725ebba2c72SAnna-Maria Gleixner 
726ebba2c72SAnna-Maria Gleixner /*
727ebba2c72SAnna-Maria Gleixner  * hrtimer_high_res_enabled - query, if the highres mode is enabled
728ebba2c72SAnna-Maria Gleixner  */
hrtimer_is_hres_enabled(void)729ebba2c72SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void)
730ebba2c72SAnna-Maria Gleixner {
731ebba2c72SAnna-Maria Gleixner 	return hrtimer_hres_enabled;
732ebba2c72SAnna-Maria Gleixner }
733ebba2c72SAnna-Maria Gleixner 
73411a9fe06SAnna-Maria Gleixner /*
73511a9fe06SAnna-Maria Gleixner  * Switch to high resolution mode
73611a9fe06SAnna-Maria Gleixner  */
hrtimer_switch_to_hres(void)73711a9fe06SAnna-Maria Gleixner static void hrtimer_switch_to_hres(void)
73811a9fe06SAnna-Maria Gleixner {
73911a9fe06SAnna-Maria Gleixner 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
74011a9fe06SAnna-Maria Gleixner 
74111a9fe06SAnna-Maria Gleixner 	if (tick_init_highres()) {
7427a6e5537SGeert Uytterhoeven 		pr_warn("Could not switch to high resolution mode on CPU %u\n",
7437a6e5537SGeert Uytterhoeven 			base->cpu);
74411a9fe06SAnna-Maria Gleixner 		return;
74511a9fe06SAnna-Maria Gleixner 	}
74611a9fe06SAnna-Maria Gleixner 	base->hres_active = 1;
74711a9fe06SAnna-Maria Gleixner 	hrtimer_resolution = HIGH_RES_NSEC;
74811a9fe06SAnna-Maria Gleixner 
7497988e5aeSFrederic Weisbecker 	tick_setup_sched_timer(true);
75011a9fe06SAnna-Maria Gleixner 	/* "Retrigger" the interrupt to get things going */
75111a9fe06SAnna-Maria Gleixner 	retrigger_next_event(NULL);
75211a9fe06SAnna-Maria Gleixner }
75311a9fe06SAnna-Maria Gleixner 
75411a9fe06SAnna-Maria Gleixner #else
75511a9fe06SAnna-Maria Gleixner 
hrtimer_is_hres_enabled(void)75611a9fe06SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void) { return 0; }
hrtimer_switch_to_hres(void)75711a9fe06SAnna-Maria Gleixner static inline void hrtimer_switch_to_hres(void) { }
75811a9fe06SAnna-Maria Gleixner 
75911a9fe06SAnna-Maria Gleixner #endif /* CONFIG_HIGH_RES_TIMERS */
760e71a4153SThomas Gleixner /*
761e71a4153SThomas Gleixner  * Retrigger next event is called after clock was set with interrupts
762e71a4153SThomas Gleixner  * disabled through an SMP function call or directly from low level
763e71a4153SThomas Gleixner  * resume code.
764e71a4153SThomas Gleixner  *
765e71a4153SThomas Gleixner  * This is only invoked when:
766e71a4153SThomas Gleixner  *	- CONFIG_HIGH_RES_TIMERS is enabled.
767e71a4153SThomas Gleixner  *	- CONFIG_NOHZ_COMMON is enabled
768e71a4153SThomas Gleixner  *
769e71a4153SThomas Gleixner  * For the other cases this function is empty and because the call sites
770e71a4153SThomas Gleixner  * are optimized out it vanishes as well, i.e. no need for lots of
771e71a4153SThomas Gleixner  * #ifdeffery.
772e71a4153SThomas Gleixner  */
retrigger_next_event(void * arg)773e71a4153SThomas Gleixner static void retrigger_next_event(void *arg)
774e71a4153SThomas Gleixner {
775e71a4153SThomas Gleixner 	struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases);
776e71a4153SThomas Gleixner 
777e71a4153SThomas Gleixner 	/*
778e71a4153SThomas Gleixner 	 * When high resolution mode or nohz is active, then the offsets of
779e71a4153SThomas Gleixner 	 * CLOCK_REALTIME/TAI/BOOTTIME have to be updated. Otherwise the
780e71a4153SThomas Gleixner 	 * next tick will take care of that.
781e71a4153SThomas Gleixner 	 *
782e71a4153SThomas Gleixner 	 * If high resolution mode is active then the next expiring timer
783e71a4153SThomas Gleixner 	 * must be reevaluated and the clock event device reprogrammed if
784e71a4153SThomas Gleixner 	 * necessary.
785e71a4153SThomas Gleixner 	 *
786e71a4153SThomas Gleixner 	 * In the NOHZ case the update of the offset and the reevaluation
787e71a4153SThomas Gleixner 	 * of the next expiring timer is enough. The return from the SMP
788e71a4153SThomas Gleixner 	 * function call will take care of the reprogramming in case the
789e71a4153SThomas Gleixner 	 * CPU was in a NOHZ idle sleep.
790e71a4153SThomas Gleixner 	 */
791b7c8e1f8SJiapeng Chong 	if (!hrtimer_hres_active(base) && !tick_nohz_active)
792e71a4153SThomas Gleixner 		return;
793e71a4153SThomas Gleixner 
794e71a4153SThomas Gleixner 	raw_spin_lock(&base->lock);
795e71a4153SThomas Gleixner 	hrtimer_update_base(base);
796b7c8e1f8SJiapeng Chong 	if (hrtimer_hres_active(base))
797e71a4153SThomas Gleixner 		hrtimer_force_reprogram(base, 0);
798e71a4153SThomas Gleixner 	else
799e71a4153SThomas Gleixner 		hrtimer_update_next_event(base);
800e71a4153SThomas Gleixner 	raw_spin_unlock(&base->lock);
801e71a4153SThomas Gleixner }
80211a9fe06SAnna-Maria Gleixner 
80311a9fe06SAnna-Maria Gleixner /*
8045cee9645SThomas Gleixner  * When a timer is enqueued and expires earlier than the already enqueued
8055cee9645SThomas Gleixner  * timers, we have to check, whether it expires earlier than the timer for
8065cee9645SThomas Gleixner  * which the clock event device was armed.
8075cee9645SThomas Gleixner  *
8085cee9645SThomas Gleixner  * Called with interrupts disabled and base->cpu_base.lock held
8095cee9645SThomas Gleixner  */
hrtimer_reprogram(struct hrtimer * timer,bool reprogram)8105da70160SAnna-Maria Gleixner static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
8115cee9645SThomas Gleixner {
812dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
8133ec7a3eeSAnna-Maria Gleixner 	struct hrtimer_clock_base *base = timer->base;
8145cee9645SThomas Gleixner 	ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
8155cee9645SThomas Gleixner 
8165cee9645SThomas Gleixner 	WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
8175cee9645SThomas Gleixner 
8185cee9645SThomas Gleixner 	/*
8195da70160SAnna-Maria Gleixner 	 * CLOCK_REALTIME timer might be requested with an absolute
8205da70160SAnna-Maria Gleixner 	 * expiry time which is less than base->offset. Set it to 0.
8215da70160SAnna-Maria Gleixner 	 */
8225da70160SAnna-Maria Gleixner 	if (expires < 0)
8235da70160SAnna-Maria Gleixner 		expires = 0;
8245da70160SAnna-Maria Gleixner 
8255da70160SAnna-Maria Gleixner 	if (timer->is_soft) {
8265da70160SAnna-Maria Gleixner 		/*
8275da70160SAnna-Maria Gleixner 		 * soft hrtimer could be started on a remote CPU. In this
8285da70160SAnna-Maria Gleixner 		 * case softirq_expires_next needs to be updated on the
8295da70160SAnna-Maria Gleixner 		 * remote CPU. The soft hrtimer will not expire before the
8305da70160SAnna-Maria Gleixner 		 * first hard hrtimer on the remote CPU -
8315da70160SAnna-Maria Gleixner 		 * hrtimer_check_target() prevents this case.
8325da70160SAnna-Maria Gleixner 		 */
8335da70160SAnna-Maria Gleixner 		struct hrtimer_cpu_base *timer_cpu_base = base->cpu_base;
8345da70160SAnna-Maria Gleixner 
8355da70160SAnna-Maria Gleixner 		if (timer_cpu_base->softirq_activated)
8365da70160SAnna-Maria Gleixner 			return;
8375da70160SAnna-Maria Gleixner 
8385da70160SAnna-Maria Gleixner 		if (!ktime_before(expires, timer_cpu_base->softirq_expires_next))
8395da70160SAnna-Maria Gleixner 			return;
8405da70160SAnna-Maria Gleixner 
8415da70160SAnna-Maria Gleixner 		timer_cpu_base->softirq_next_timer = timer;
8425da70160SAnna-Maria Gleixner 		timer_cpu_base->softirq_expires_next = expires;
8435da70160SAnna-Maria Gleixner 
8445da70160SAnna-Maria Gleixner 		if (!ktime_before(expires, timer_cpu_base->expires_next) ||
8455da70160SAnna-Maria Gleixner 		    !reprogram)
8465da70160SAnna-Maria Gleixner 			return;
8475da70160SAnna-Maria Gleixner 	}
8485da70160SAnna-Maria Gleixner 
8495da70160SAnna-Maria Gleixner 	/*
850c6eb3f70SThomas Gleixner 	 * If the timer is not on the current cpu, we cannot reprogram
851c6eb3f70SThomas Gleixner 	 * the other cpus clock event device.
8525cee9645SThomas Gleixner 	 */
853c6eb3f70SThomas Gleixner 	if (base->cpu_base != cpu_base)
854c6eb3f70SThomas Gleixner 		return;
855c6eb3f70SThomas Gleixner 
856f80e2148SThomas Gleixner 	if (expires >= cpu_base->expires_next)
857f80e2148SThomas Gleixner 		return;
858f80e2148SThomas Gleixner 
859f80e2148SThomas Gleixner 	/*
860f80e2148SThomas Gleixner 	 * If the hrtimer interrupt is running, then it will reevaluate the
861f80e2148SThomas Gleixner 	 * clock bases and reprogram the clock event device.
862f80e2148SThomas Gleixner 	 */
863f80e2148SThomas Gleixner 	if (cpu_base->in_hrtirq)
864f80e2148SThomas Gleixner 		return;
865f80e2148SThomas Gleixner 
866f80e2148SThomas Gleixner 	cpu_base->next_timer = timer;
867f80e2148SThomas Gleixner 
868f80e2148SThomas Gleixner 	__hrtimer_reprogram(cpu_base, timer, expires);
8695cee9645SThomas Gleixner }
8705cee9645SThomas Gleixner 
update_needs_ipi(struct hrtimer_cpu_base * cpu_base,unsigned int active)8711e7f7fbcSThomas Gleixner static bool update_needs_ipi(struct hrtimer_cpu_base *cpu_base,
8721e7f7fbcSThomas Gleixner 			     unsigned int active)
8731e7f7fbcSThomas Gleixner {
8741e7f7fbcSThomas Gleixner 	struct hrtimer_clock_base *base;
8751e7f7fbcSThomas Gleixner 	unsigned int seq;
8761e7f7fbcSThomas Gleixner 	ktime_t expires;
8771e7f7fbcSThomas Gleixner 
8781e7f7fbcSThomas Gleixner 	/*
8791e7f7fbcSThomas Gleixner 	 * Update the base offsets unconditionally so the following
8801e7f7fbcSThomas Gleixner 	 * checks whether the SMP function call is required works.
8811e7f7fbcSThomas Gleixner 	 *
8821e7f7fbcSThomas Gleixner 	 * The update is safe even when the remote CPU is in the hrtimer
8831e7f7fbcSThomas Gleixner 	 * interrupt or the hrtimer soft interrupt and expiring affected
8841e7f7fbcSThomas Gleixner 	 * bases. Either it will see the update before handling a base or
8851e7f7fbcSThomas Gleixner 	 * it will see it when it finishes the processing and reevaluates
8861e7f7fbcSThomas Gleixner 	 * the next expiring timer.
8871e7f7fbcSThomas Gleixner 	 */
8881e7f7fbcSThomas Gleixner 	seq = cpu_base->clock_was_set_seq;
8891e7f7fbcSThomas Gleixner 	hrtimer_update_base(cpu_base);
8901e7f7fbcSThomas Gleixner 
8911e7f7fbcSThomas Gleixner 	/*
8921e7f7fbcSThomas Gleixner 	 * If the sequence did not change over the update then the
8931e7f7fbcSThomas Gleixner 	 * remote CPU already handled it.
8941e7f7fbcSThomas Gleixner 	 */
8951e7f7fbcSThomas Gleixner 	if (seq == cpu_base->clock_was_set_seq)
8961e7f7fbcSThomas Gleixner 		return false;
8971e7f7fbcSThomas Gleixner 
8981e7f7fbcSThomas Gleixner 	/*
8991e7f7fbcSThomas Gleixner 	 * If the remote CPU is currently handling an hrtimer interrupt, it
9001e7f7fbcSThomas Gleixner 	 * will reevaluate the first expiring timer of all clock bases
9011e7f7fbcSThomas Gleixner 	 * before reprogramming. Nothing to do here.
9021e7f7fbcSThomas Gleixner 	 */
9031e7f7fbcSThomas Gleixner 	if (cpu_base->in_hrtirq)
9041e7f7fbcSThomas Gleixner 		return false;
9051e7f7fbcSThomas Gleixner 
9061e7f7fbcSThomas Gleixner 	/*
9071e7f7fbcSThomas Gleixner 	 * Walk the affected clock bases and check whether the first expiring
9081e7f7fbcSThomas Gleixner 	 * timer in a clock base is moving ahead of the first expiring timer of
9091e7f7fbcSThomas Gleixner 	 * @cpu_base. If so, the IPI must be invoked because per CPU clock
9101e7f7fbcSThomas Gleixner 	 * event devices cannot be remotely reprogrammed.
9111e7f7fbcSThomas Gleixner 	 */
9121e7f7fbcSThomas Gleixner 	active &= cpu_base->active_bases;
9131e7f7fbcSThomas Gleixner 
9141e7f7fbcSThomas Gleixner 	for_each_active_base(base, cpu_base, active) {
9151e7f7fbcSThomas Gleixner 		struct timerqueue_node *next;
9161e7f7fbcSThomas Gleixner 
9171e7f7fbcSThomas Gleixner 		next = timerqueue_getnext(&base->active);
9181e7f7fbcSThomas Gleixner 		expires = ktime_sub(next->expires, base->offset);
9191e7f7fbcSThomas Gleixner 		if (expires < cpu_base->expires_next)
9201e7f7fbcSThomas Gleixner 			return true;
9211e7f7fbcSThomas Gleixner 
9221e7f7fbcSThomas Gleixner 		/* Extra check for softirq clock bases */
9231e7f7fbcSThomas Gleixner 		if (base->clockid < HRTIMER_BASE_MONOTONIC_SOFT)
9241e7f7fbcSThomas Gleixner 			continue;
9251e7f7fbcSThomas Gleixner 		if (cpu_base->softirq_activated)
9261e7f7fbcSThomas Gleixner 			continue;
9271e7f7fbcSThomas Gleixner 		if (expires < cpu_base->softirq_expires_next)
9281e7f7fbcSThomas Gleixner 			return true;
9291e7f7fbcSThomas Gleixner 	}
9301e7f7fbcSThomas Gleixner 	return false;
9311e7f7fbcSThomas Gleixner }
9321e7f7fbcSThomas Gleixner 
9335cee9645SThomas Gleixner /*
934e71a4153SThomas Gleixner  * Clock was set. This might affect CLOCK_REALTIME, CLOCK_TAI and
935e71a4153SThomas Gleixner  * CLOCK_BOOTTIME (for late sleep time injection).
9365cee9645SThomas Gleixner  *
937e71a4153SThomas Gleixner  * This requires to update the offsets for these clocks
938e71a4153SThomas Gleixner  * vs. CLOCK_MONOTONIC. When high resolution timers are enabled, then this
939e71a4153SThomas Gleixner  * also requires to eventually reprogram the per CPU clock event devices
940e71a4153SThomas Gleixner  * when the change moves an affected timer ahead of the first expiring
941e71a4153SThomas Gleixner  * timer on that CPU. Obviously remote per CPU clock event devices cannot
942e71a4153SThomas Gleixner  * be reprogrammed. The other reason why an IPI has to be sent is when the
943e71a4153SThomas Gleixner  * system is in !HIGH_RES and NOHZ mode. The NOHZ mode updates the offsets
944e71a4153SThomas Gleixner  * in the tick, which obviously might be stopped, so this has to bring out
945e71a4153SThomas Gleixner  * the remote CPU which might sleep in idle to get this sorted.
9465cee9645SThomas Gleixner  */
clock_was_set(unsigned int bases)94717a1b882SThomas Gleixner void clock_was_set(unsigned int bases)
9485cee9645SThomas Gleixner {
9499482fd71SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = raw_cpu_ptr(&hrtimer_bases);
95081d741d3SMarcelo Tosatti 	cpumask_var_t mask;
95181d741d3SMarcelo Tosatti 	int cpu;
95281d741d3SMarcelo Tosatti 
953b7c8e1f8SJiapeng Chong 	if (!hrtimer_hres_active(cpu_base) && !tick_nohz_active)
954e71a4153SThomas Gleixner 		goto out_timerfd;
955e71a4153SThomas Gleixner 
95681d741d3SMarcelo Tosatti 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
9575cee9645SThomas Gleixner 		on_each_cpu(retrigger_next_event, NULL, 1);
95881d741d3SMarcelo Tosatti 		goto out_timerfd;
95981d741d3SMarcelo Tosatti 	}
96081d741d3SMarcelo Tosatti 
96181d741d3SMarcelo Tosatti 	/* Avoid interrupting CPUs if possible */
96281d741d3SMarcelo Tosatti 	cpus_read_lock();
96381d741d3SMarcelo Tosatti 	for_each_online_cpu(cpu) {
96481d741d3SMarcelo Tosatti 		unsigned long flags;
96581d741d3SMarcelo Tosatti 
9669482fd71SThomas Gleixner 		cpu_base = &per_cpu(hrtimer_bases, cpu);
96781d741d3SMarcelo Tosatti 		raw_spin_lock_irqsave(&cpu_base->lock, flags);
9681e7f7fbcSThomas Gleixner 
9691e7f7fbcSThomas Gleixner 		if (update_needs_ipi(cpu_base, bases))
97081d741d3SMarcelo Tosatti 			cpumask_set_cpu(cpu, mask);
9711e7f7fbcSThomas Gleixner 
97281d741d3SMarcelo Tosatti 		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
97381d741d3SMarcelo Tosatti 	}
97481d741d3SMarcelo Tosatti 
97581d741d3SMarcelo Tosatti 	preempt_disable();
97681d741d3SMarcelo Tosatti 	smp_call_function_many(mask, retrigger_next_event, NULL, 1);
97781d741d3SMarcelo Tosatti 	preempt_enable();
97881d741d3SMarcelo Tosatti 	cpus_read_unlock();
97981d741d3SMarcelo Tosatti 	free_cpumask_var(mask);
980e71a4153SThomas Gleixner 
981e71a4153SThomas Gleixner out_timerfd:
9825cee9645SThomas Gleixner 	timerfd_clock_was_set();
9835cee9645SThomas Gleixner }
9845cee9645SThomas Gleixner 
clock_was_set_work(struct work_struct * work)9858c3b5e6eSThomas Gleixner static void clock_was_set_work(struct work_struct *work)
9868c3b5e6eSThomas Gleixner {
98717a1b882SThomas Gleixner 	clock_was_set(CLOCK_SET_WALL);
9888c3b5e6eSThomas Gleixner }
9898c3b5e6eSThomas Gleixner 
9908c3b5e6eSThomas Gleixner static DECLARE_WORK(hrtimer_work, clock_was_set_work);
9918c3b5e6eSThomas Gleixner 
9928c3b5e6eSThomas Gleixner /*
993a761a67fSThomas Gleixner  * Called from timekeeping code to reprogram the hrtimer interrupt device
994a761a67fSThomas Gleixner  * on all cpus and to notify timerfd.
9958c3b5e6eSThomas Gleixner  */
clock_was_set_delayed(void)9968c3b5e6eSThomas Gleixner void clock_was_set_delayed(void)
9978c3b5e6eSThomas Gleixner {
9988c3b5e6eSThomas Gleixner 	schedule_work(&hrtimer_work);
9998c3b5e6eSThomas Gleixner }
10008c3b5e6eSThomas Gleixner 
10015cee9645SThomas Gleixner /*
1002a761a67fSThomas Gleixner  * Called during resume either directly from via timekeeping_resume()
1003a761a67fSThomas Gleixner  * or in the case of s2idle from tick_unfreeze() to ensure that the
1004a761a67fSThomas Gleixner  * hrtimers are up to date.
10055cee9645SThomas Gleixner  */
hrtimers_resume_local(void)1006a761a67fSThomas Gleixner void hrtimers_resume_local(void)
10075cee9645SThomas Gleixner {
100853bef3fdSFrederic Weisbecker 	lockdep_assert_irqs_disabled();
10095cee9645SThomas Gleixner 	/* Retrigger on the local CPU */
10105cee9645SThomas Gleixner 	retrigger_next_event(NULL);
10115cee9645SThomas Gleixner }
10125cee9645SThomas Gleixner 
10135cee9645SThomas Gleixner /*
10145cee9645SThomas Gleixner  * Counterpart to lock_hrtimer_base above:
10155cee9645SThomas Gleixner  */
10165cee9645SThomas Gleixner static inline
unlock_hrtimer_base(const struct hrtimer * timer,unsigned long * flags)10175cee9645SThomas Gleixner void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
1018ccaa4926SBen Dooks 	__releases(&timer->base->cpu_base->lock)
10195cee9645SThomas Gleixner {
10205cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
10215cee9645SThomas Gleixner }
10225cee9645SThomas Gleixner 
10235cee9645SThomas Gleixner /**
1024ca2768bbSAnna-Maria Behnsen  * hrtimer_forward() - forward the timer expiry
10255cee9645SThomas Gleixner  * @timer:	hrtimer to forward
10265cee9645SThomas Gleixner  * @now:	forward past this time
10275cee9645SThomas Gleixner  * @interval:	the interval to forward
10285cee9645SThomas Gleixner  *
10295cee9645SThomas Gleixner  * Forward the timer expiry so it will expire in the future.
103091e5a217SThomas Gleixner  *
1031ca2768bbSAnna-Maria Behnsen  * .. note::
1032ca2768bbSAnna-Maria Behnsen  *  This only updates the timer expiry value and does not requeue the timer.
103391e5a217SThomas Gleixner  *
1034ca2768bbSAnna-Maria Behnsen  * There is also a variant of the function hrtimer_forward_now().
1035ca2768bbSAnna-Maria Behnsen  *
1036ca2768bbSAnna-Maria Behnsen  * Context: Can be safely called from the callback function of @timer. If called
1037ca2768bbSAnna-Maria Behnsen  *          from other contexts @timer must neither be enqueued nor running the
1038ca2768bbSAnna-Maria Behnsen  *          callback and the caller needs to take care of serialization.
1039ca2768bbSAnna-Maria Behnsen  *
1040ca2768bbSAnna-Maria Behnsen  * Return: The number of overruns are returned.
10415cee9645SThomas Gleixner  */
hrtimer_forward(struct hrtimer * timer,ktime_t now,ktime_t interval)10425cee9645SThomas Gleixner u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
10435cee9645SThomas Gleixner {
10445cee9645SThomas Gleixner 	u64 orun = 1;
10455cee9645SThomas Gleixner 	ktime_t delta;
10465cee9645SThomas Gleixner 
10475cee9645SThomas Gleixner 	delta = ktime_sub(now, hrtimer_get_expires(timer));
10485cee9645SThomas Gleixner 
10492456e855SThomas Gleixner 	if (delta < 0)
10505cee9645SThomas Gleixner 		return 0;
10515cee9645SThomas Gleixner 
10525de2755cSPeter Zijlstra 	if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED))
10535de2755cSPeter Zijlstra 		return 0;
10545de2755cSPeter Zijlstra 
10552456e855SThomas Gleixner 	if (interval < hrtimer_resolution)
10562456e855SThomas Gleixner 		interval = hrtimer_resolution;
10575cee9645SThomas Gleixner 
10582456e855SThomas Gleixner 	if (unlikely(delta >= interval)) {
10595cee9645SThomas Gleixner 		s64 incr = ktime_to_ns(interval);
10605cee9645SThomas Gleixner 
10615cee9645SThomas Gleixner 		orun = ktime_divns(delta, incr);
10625cee9645SThomas Gleixner 		hrtimer_add_expires_ns(timer, incr * orun);
10632456e855SThomas Gleixner 		if (hrtimer_get_expires_tv64(timer) > now)
10645cee9645SThomas Gleixner 			return orun;
10655cee9645SThomas Gleixner 		/*
10665cee9645SThomas Gleixner 		 * This (and the ktime_add() below) is the
10675cee9645SThomas Gleixner 		 * correction for exact:
10685cee9645SThomas Gleixner 		 */
10695cee9645SThomas Gleixner 		orun++;
10705cee9645SThomas Gleixner 	}
10715cee9645SThomas Gleixner 	hrtimer_add_expires(timer, interval);
10725cee9645SThomas Gleixner 
10735cee9645SThomas Gleixner 	return orun;
10745cee9645SThomas Gleixner }
10755cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_forward);
10765cee9645SThomas Gleixner 
10775cee9645SThomas Gleixner /*
10785cee9645SThomas Gleixner  * enqueue_hrtimer - internal function to (re)start a timer
10795cee9645SThomas Gleixner  *
10805cee9645SThomas Gleixner  * The timer is inserted in expiry order. Insertion into the
10815cee9645SThomas Gleixner  * red black tree is O(log(n)). Must hold the base lock.
10825cee9645SThomas Gleixner  *
1083da7100d3SRichard Clark  * Returns true when the new timer is the leftmost timer in the tree.
10845cee9645SThomas Gleixner  */
enqueue_hrtimer(struct hrtimer * timer,struct hrtimer_clock_base * base,enum hrtimer_mode mode)1085da7100d3SRichard Clark static bool enqueue_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base,
108663e2ed36SAnna-Maria Gleixner 			    enum hrtimer_mode mode)
10875cee9645SThomas Gleixner {
108863e2ed36SAnna-Maria Gleixner 	debug_activate(timer, mode);
1089dad6a09fSFrederic Weisbecker 	WARN_ON_ONCE(!base->cpu_base->online);
10905cee9645SThomas Gleixner 
10915cee9645SThomas Gleixner 	base->cpu_base->active_bases |= 1 << base->index;
10925cee9645SThomas Gleixner 
109356144737SEric Dumazet 	/* Pairs with the lockless read in hrtimer_is_queued() */
109456144737SEric Dumazet 	WRITE_ONCE(timer->state, HRTIMER_STATE_ENQUEUED);
10955cee9645SThomas Gleixner 
1096b97f44c9SThomas Gleixner 	return timerqueue_add(&base->active, &timer->node);
10975cee9645SThomas Gleixner }
10985cee9645SThomas Gleixner 
10995cee9645SThomas Gleixner /*
11005cee9645SThomas Gleixner  * __remove_hrtimer - internal function to remove a timer
11015cee9645SThomas Gleixner  *
11025cee9645SThomas Gleixner  * Caller must hold the base lock.
11035cee9645SThomas Gleixner  *
11045cee9645SThomas Gleixner  * High resolution timer mode reprograms the clock event device when the
11055cee9645SThomas Gleixner  * timer is the one which expires next. The caller can disable this by setting
11065cee9645SThomas Gleixner  * reprogram to zero. This is useful, when the context does a reprogramming
11075cee9645SThomas Gleixner  * anyway (e.g. timer interrupt)
11085cee9645SThomas Gleixner  */
__remove_hrtimer(struct hrtimer * timer,struct hrtimer_clock_base * base,u8 newstate,int reprogram)11095cee9645SThomas Gleixner static void __remove_hrtimer(struct hrtimer *timer,
11105cee9645SThomas Gleixner 			     struct hrtimer_clock_base *base,
1111203cbf77SThomas Gleixner 			     u8 newstate, int reprogram)
11125cee9645SThomas Gleixner {
1113e19ffe8bSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1114203cbf77SThomas Gleixner 	u8 state = timer->state;
11155cee9645SThomas Gleixner 
111656144737SEric Dumazet 	/* Pairs with the lockless read in hrtimer_is_queued() */
111756144737SEric Dumazet 	WRITE_ONCE(timer->state, newstate);
1118895bdfa7SThomas Gleixner 	if (!(state & HRTIMER_STATE_ENQUEUED))
1119895bdfa7SThomas Gleixner 		return;
11205cee9645SThomas Gleixner 
1121b97f44c9SThomas Gleixner 	if (!timerqueue_del(&base->active, &timer->node))
1122e19ffe8bSThomas Gleixner 		cpu_base->active_bases &= ~(1 << base->index);
1123d9f0acdeSViresh Kumar 
1124895bdfa7SThomas Gleixner 	/*
1125895bdfa7SThomas Gleixner 	 * Note: If reprogram is false we do not update
1126895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. This happens when we remove the first
1127895bdfa7SThomas Gleixner 	 * timer on a remote cpu. No harm as we never dereference
1128895bdfa7SThomas Gleixner 	 * cpu_base->next_timer. So the worst thing what can happen is
11294bf07f65SIngo Molnar 	 * an superfluous call to hrtimer_force_reprogram() on the
1130895bdfa7SThomas Gleixner 	 * remote cpu later on if the same timer gets enqueued again.
1131895bdfa7SThomas Gleixner 	 */
1132895bdfa7SThomas Gleixner 	if (reprogram && timer == cpu_base->next_timer)
1133e19ffe8bSThomas Gleixner 		hrtimer_force_reprogram(cpu_base, 1);
11345cee9645SThomas Gleixner }
11355cee9645SThomas Gleixner 
11365cee9645SThomas Gleixner /*
11375cee9645SThomas Gleixner  * remove hrtimer, called with base lock held
11385cee9645SThomas Gleixner  */
11395cee9645SThomas Gleixner static inline int
remove_hrtimer(struct hrtimer * timer,struct hrtimer_clock_base * base,bool restart,bool keep_local)1140627ef5aeSThomas Gleixner remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base,
1141627ef5aeSThomas Gleixner 	       bool restart, bool keep_local)
11425cee9645SThomas Gleixner {
1143203cbf77SThomas Gleixner 	u8 state = timer->state;
114456144737SEric Dumazet 
114556144737SEric Dumazet 	if (state & HRTIMER_STATE_ENQUEUED) {
1146627ef5aeSThomas Gleixner 		bool reprogram;
11475cee9645SThomas Gleixner 
11485cee9645SThomas Gleixner 		/*
11495cee9645SThomas Gleixner 		 * Remove the timer and force reprogramming when high
11505cee9645SThomas Gleixner 		 * resolution mode is active and the timer is on the current
11515cee9645SThomas Gleixner 		 * CPU. If we remove a timer on another CPU, reprogramming is
11525cee9645SThomas Gleixner 		 * skipped. The interrupt event on this CPU is fired and
11535cee9645SThomas Gleixner 		 * reprogramming happens in the interrupt handler. This is a
11545cee9645SThomas Gleixner 		 * rare case and less expensive than a smp call.
11555cee9645SThomas Gleixner 		 */
11565cee9645SThomas Gleixner 		debug_deactivate(timer);
1157dc5df73bSChristoph Lameter 		reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases);
11588edfb036SPeter Zijlstra 
1159627ef5aeSThomas Gleixner 		/*
1160627ef5aeSThomas Gleixner 		 * If the timer is not restarted then reprogramming is
1161627ef5aeSThomas Gleixner 		 * required if the timer is local. If it is local and about
1162627ef5aeSThomas Gleixner 		 * to be restarted, avoid programming it twice (on removal
1163627ef5aeSThomas Gleixner 		 * and a moment later when it's requeued).
1164627ef5aeSThomas Gleixner 		 */
1165887d9dc9SPeter Zijlstra 		if (!restart)
1166887d9dc9SPeter Zijlstra 			state = HRTIMER_STATE_INACTIVE;
1167627ef5aeSThomas Gleixner 		else
1168627ef5aeSThomas Gleixner 			reprogram &= !keep_local;
1169887d9dc9SPeter Zijlstra 
11705cee9645SThomas Gleixner 		__remove_hrtimer(timer, base, state, reprogram);
11715cee9645SThomas Gleixner 		return 1;
11725cee9645SThomas Gleixner 	}
11735cee9645SThomas Gleixner 	return 0;
11745cee9645SThomas Gleixner }
11755cee9645SThomas Gleixner 
hrtimer_update_lowres(struct hrtimer * timer,ktime_t tim,const enum hrtimer_mode mode)1176203cbf77SThomas Gleixner static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim,
1177203cbf77SThomas Gleixner 					    const enum hrtimer_mode mode)
1178203cbf77SThomas Gleixner {
1179203cbf77SThomas Gleixner #ifdef CONFIG_TIME_LOW_RES
1180203cbf77SThomas Gleixner 	/*
1181203cbf77SThomas Gleixner 	 * CONFIG_TIME_LOW_RES indicates that the system has no way to return
1182203cbf77SThomas Gleixner 	 * granular time values. For relative timers we add hrtimer_resolution
1183bd7c8ff9SAnna-Maria Behnsen 	 * (i.e. one jiffy) to prevent short timeouts.
1184203cbf77SThomas Gleixner 	 */
1185203cbf77SThomas Gleixner 	timer->is_rel = mode & HRTIMER_MODE_REL;
1186203cbf77SThomas Gleixner 	if (timer->is_rel)
11878b0e1953SThomas Gleixner 		tim = ktime_add_safe(tim, hrtimer_resolution);
1188203cbf77SThomas Gleixner #endif
1189203cbf77SThomas Gleixner 	return tim;
1190203cbf77SThomas Gleixner }
1191203cbf77SThomas Gleixner 
11925da70160SAnna-Maria Gleixner static void
hrtimer_update_softirq_timer(struct hrtimer_cpu_base * cpu_base,bool reprogram)11935da70160SAnna-Maria Gleixner hrtimer_update_softirq_timer(struct hrtimer_cpu_base *cpu_base, bool reprogram)
11945da70160SAnna-Maria Gleixner {
11955da70160SAnna-Maria Gleixner 	ktime_t expires;
11965da70160SAnna-Maria Gleixner 
11975da70160SAnna-Maria Gleixner 	/*
11985da70160SAnna-Maria Gleixner 	 * Find the next SOFT expiration.
11995da70160SAnna-Maria Gleixner 	 */
12005da70160SAnna-Maria Gleixner 	expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
12015da70160SAnna-Maria Gleixner 
12025da70160SAnna-Maria Gleixner 	/*
12035da70160SAnna-Maria Gleixner 	 * reprogramming needs to be triggered, even if the next soft
12045da70160SAnna-Maria Gleixner 	 * hrtimer expires at the same time than the next hard
12055da70160SAnna-Maria Gleixner 	 * hrtimer. cpu_base->softirq_expires_next needs to be updated!
12065da70160SAnna-Maria Gleixner 	 */
12075da70160SAnna-Maria Gleixner 	if (expires == KTIME_MAX)
12085da70160SAnna-Maria Gleixner 		return;
12095da70160SAnna-Maria Gleixner 
12105da70160SAnna-Maria Gleixner 	/*
12115da70160SAnna-Maria Gleixner 	 * cpu_base->*next_timer is recomputed by __hrtimer_get_next_event()
12125da70160SAnna-Maria Gleixner 	 * cpu_base->*expires_next is only set by hrtimer_reprogram()
12135da70160SAnna-Maria Gleixner 	 */
12145da70160SAnna-Maria Gleixner 	hrtimer_reprogram(cpu_base->softirq_next_timer, reprogram);
12155da70160SAnna-Maria Gleixner }
12165da70160SAnna-Maria Gleixner 
__hrtimer_start_range_ns(struct hrtimer * timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode,struct hrtimer_clock_base * base)1217138a6b7aSAnna-Maria Gleixner static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1218138a6b7aSAnna-Maria Gleixner 				    u64 delta_ns, const enum hrtimer_mode mode,
1219138a6b7aSAnna-Maria Gleixner 				    struct hrtimer_clock_base *base)
12205cee9645SThomas Gleixner {
122153dac345SFrederic Weisbecker 	struct hrtimer_cpu_base *this_cpu_base = this_cpu_ptr(&hrtimer_bases);
1222138a6b7aSAnna-Maria Gleixner 	struct hrtimer_clock_base *new_base;
1223627ef5aeSThomas Gleixner 	bool force_local, first;
12245cee9645SThomas Gleixner 
1225627ef5aeSThomas Gleixner 	/*
1226627ef5aeSThomas Gleixner 	 * If the timer is on the local cpu base and is the first expiring
1227627ef5aeSThomas Gleixner 	 * timer then this might end up reprogramming the hardware twice
1228627ef5aeSThomas Gleixner 	 * (on removal and on enqueue). To avoid that by prevent the
1229627ef5aeSThomas Gleixner 	 * reprogram on removal, keep the timer local to the current CPU
1230627ef5aeSThomas Gleixner 	 * and enforce reprogramming after it is queued no matter whether
1231627ef5aeSThomas Gleixner 	 * it is the new first expiring timer again or not.
1232627ef5aeSThomas Gleixner 	 */
123353dac345SFrederic Weisbecker 	force_local = base->cpu_base == this_cpu_base;
1234627ef5aeSThomas Gleixner 	force_local &= base->cpu_base->next_timer == timer;
1235627ef5aeSThomas Gleixner 
1236627ef5aeSThomas Gleixner 	/*
123753dac345SFrederic Weisbecker 	 * Don't force local queuing if this enqueue happens on a unplugged
123853dac345SFrederic Weisbecker 	 * CPU after hrtimer_cpu_dying() has been invoked.
123953dac345SFrederic Weisbecker 	 */
124053dac345SFrederic Weisbecker 	force_local &= this_cpu_base->online;
124153dac345SFrederic Weisbecker 
124253dac345SFrederic Weisbecker 	/*
1243627ef5aeSThomas Gleixner 	 * Remove an active timer from the queue. In case it is not queued
1244627ef5aeSThomas Gleixner 	 * on the current CPU, make sure that remove_hrtimer() updates the
1245627ef5aeSThomas Gleixner 	 * remote data correctly.
1246627ef5aeSThomas Gleixner 	 *
1247627ef5aeSThomas Gleixner 	 * If it's on the current CPU and the first expiring timer, then
1248627ef5aeSThomas Gleixner 	 * skip reprogramming, keep the timer local and enforce
1249627ef5aeSThomas Gleixner 	 * reprogramming later if it was the first expiring timer.  This
1250627ef5aeSThomas Gleixner 	 * avoids programming the underlying clock event twice (once at
1251627ef5aeSThomas Gleixner 	 * removal and once after enqueue).
1252627ef5aeSThomas Gleixner 	 */
1253627ef5aeSThomas Gleixner 	remove_hrtimer(timer, base, true, force_local);
12545cee9645SThomas Gleixner 
1255203cbf77SThomas Gleixner 	if (mode & HRTIMER_MODE_REL)
12565cee9645SThomas Gleixner 		tim = ktime_add_safe(tim, base->get_time());
1257203cbf77SThomas Gleixner 
1258203cbf77SThomas Gleixner 	tim = hrtimer_update_lowres(timer, tim, mode);
12595cee9645SThomas Gleixner 
12605cee9645SThomas Gleixner 	hrtimer_set_expires_range_ns(timer, tim, delta_ns);
12615cee9645SThomas Gleixner 
12625cee9645SThomas Gleixner 	/* Switch the timer base, if necessary: */
1263627ef5aeSThomas Gleixner 	if (!force_local) {
1264627ef5aeSThomas Gleixner 		new_base = switch_hrtimer_base(timer, base,
1265627ef5aeSThomas Gleixner 					       mode & HRTIMER_MODE_PINNED);
1266627ef5aeSThomas Gleixner 	} else {
1267627ef5aeSThomas Gleixner 		new_base = base;
1268627ef5aeSThomas Gleixner 	}
12695cee9645SThomas Gleixner 
1270627ef5aeSThomas Gleixner 	first = enqueue_hrtimer(timer, new_base, mode);
127153dac345SFrederic Weisbecker 	if (!force_local) {
127253dac345SFrederic Weisbecker 		/*
127353dac345SFrederic Weisbecker 		 * If the current CPU base is online, then the timer is
127453dac345SFrederic Weisbecker 		 * never queued on a remote CPU if it would be the first
127553dac345SFrederic Weisbecker 		 * expiring timer there.
127653dac345SFrederic Weisbecker 		 */
127753dac345SFrederic Weisbecker 		if (hrtimer_base_is_online(this_cpu_base))
1278627ef5aeSThomas Gleixner 			return first;
1279627ef5aeSThomas Gleixner 
1280627ef5aeSThomas Gleixner 		/*
128153dac345SFrederic Weisbecker 		 * Timer was enqueued remote because the current base is
128253dac345SFrederic Weisbecker 		 * already offline. If the timer is the first to expire,
128353dac345SFrederic Weisbecker 		 * kick the remote CPU to reprogram the clock event.
128453dac345SFrederic Weisbecker 		 */
128553dac345SFrederic Weisbecker 		if (first) {
128653dac345SFrederic Weisbecker 			struct hrtimer_cpu_base *new_cpu_base = new_base->cpu_base;
128753dac345SFrederic Weisbecker 
128853dac345SFrederic Weisbecker 			smp_call_function_single_async(new_cpu_base->cpu, &new_cpu_base->csd);
128953dac345SFrederic Weisbecker 		}
129053dac345SFrederic Weisbecker 		return 0;
129153dac345SFrederic Weisbecker 	}
129253dac345SFrederic Weisbecker 
129353dac345SFrederic Weisbecker 	/*
1294627ef5aeSThomas Gleixner 	 * Timer was forced to stay on the current CPU to avoid
1295627ef5aeSThomas Gleixner 	 * reprogramming on removal and enqueue. Force reprogram the
1296627ef5aeSThomas Gleixner 	 * hardware by evaluating the new first expiring timer.
1297627ef5aeSThomas Gleixner 	 */
1298627ef5aeSThomas Gleixner 	hrtimer_force_reprogram(new_base->cpu_base, 1);
1299627ef5aeSThomas Gleixner 	return 0;
1300138a6b7aSAnna-Maria Gleixner }
13015da70160SAnna-Maria Gleixner 
1302138a6b7aSAnna-Maria Gleixner /**
1303138a6b7aSAnna-Maria Gleixner  * hrtimer_start_range_ns - (re)start an hrtimer
1304138a6b7aSAnna-Maria Gleixner  * @timer:	the timer to be added
1305138a6b7aSAnna-Maria Gleixner  * @tim:	expiry time
1306138a6b7aSAnna-Maria Gleixner  * @delta_ns:	"slack" range for the timer
1307138a6b7aSAnna-Maria Gleixner  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
13085da70160SAnna-Maria Gleixner  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
13095da70160SAnna-Maria Gleixner  *		softirq based mode is considered for debug purpose only!
1310138a6b7aSAnna-Maria Gleixner  */
hrtimer_start_range_ns(struct hrtimer * timer,ktime_t tim,u64 delta_ns,const enum hrtimer_mode mode)1311138a6b7aSAnna-Maria Gleixner void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1312138a6b7aSAnna-Maria Gleixner 			    u64 delta_ns, const enum hrtimer_mode mode)
1313138a6b7aSAnna-Maria Gleixner {
1314138a6b7aSAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1315138a6b7aSAnna-Maria Gleixner 	unsigned long flags;
131649a2a075SViresh Kumar 
13175da70160SAnna-Maria Gleixner 	/*
13185da70160SAnna-Maria Gleixner 	 * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft
13190ab6a3ddSThomas Gleixner 	 * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard
13200ab6a3ddSThomas Gleixner 	 * expiry mode because unmarked timers are moved to softirq expiry.
13215da70160SAnna-Maria Gleixner 	 */
13220ab6a3ddSThomas Gleixner 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
13235da70160SAnna-Maria Gleixner 		WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft);
13240ab6a3ddSThomas Gleixner 	else
13250ab6a3ddSThomas Gleixner 		WARN_ON_ONCE(!(mode & HRTIMER_MODE_HARD) ^ !timer->is_hard);
13265da70160SAnna-Maria Gleixner 
1327138a6b7aSAnna-Maria Gleixner 	base = lock_hrtimer_base(timer, &flags);
1328138a6b7aSAnna-Maria Gleixner 
1329138a6b7aSAnna-Maria Gleixner 	if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base))
13305da70160SAnna-Maria Gleixner 		hrtimer_reprogram(timer, true);
1331138a6b7aSAnna-Maria Gleixner 
13325cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
13335cee9645SThomas Gleixner }
13345cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
13355cee9645SThomas Gleixner 
13365cee9645SThomas Gleixner /**
13375cee9645SThomas Gleixner  * hrtimer_try_to_cancel - try to deactivate a timer
13385cee9645SThomas Gleixner  * @timer:	hrtimer to stop
13395cee9645SThomas Gleixner  *
13405cee9645SThomas Gleixner  * Returns:
134151633704SMauro Carvalho Chehab  *
134251633704SMauro Carvalho Chehab  *  *  0 when the timer was not active
134351633704SMauro Carvalho Chehab  *  *  1 when the timer was active
134451633704SMauro Carvalho Chehab  *  * -1 when the timer is currently executing the callback function and
13455cee9645SThomas Gleixner  *    cannot be stopped
13465cee9645SThomas Gleixner  */
hrtimer_try_to_cancel(struct hrtimer * timer)13475cee9645SThomas Gleixner int hrtimer_try_to_cancel(struct hrtimer *timer)
13485cee9645SThomas Gleixner {
13495cee9645SThomas Gleixner 	struct hrtimer_clock_base *base;
13505cee9645SThomas Gleixner 	unsigned long flags;
13515cee9645SThomas Gleixner 	int ret = -1;
13525cee9645SThomas Gleixner 
135319d9f422SThomas Gleixner 	/*
135419d9f422SThomas Gleixner 	 * Check lockless first. If the timer is not active (neither
135519d9f422SThomas Gleixner 	 * enqueued nor running the callback, nothing to do here.  The
135619d9f422SThomas Gleixner 	 * base lock does not serialize against a concurrent enqueue,
135719d9f422SThomas Gleixner 	 * so we can avoid taking it.
135819d9f422SThomas Gleixner 	 */
135919d9f422SThomas Gleixner 	if (!hrtimer_active(timer))
136019d9f422SThomas Gleixner 		return 0;
136119d9f422SThomas Gleixner 
13625cee9645SThomas Gleixner 	base = lock_hrtimer_base(timer, &flags);
13635cee9645SThomas Gleixner 
13645cee9645SThomas Gleixner 	if (!hrtimer_callback_running(timer))
1365627ef5aeSThomas Gleixner 		ret = remove_hrtimer(timer, base, false, false);
13665cee9645SThomas Gleixner 
13675cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
13685cee9645SThomas Gleixner 
13695cee9645SThomas Gleixner 	return ret;
13705cee9645SThomas Gleixner 
13715cee9645SThomas Gleixner }
13725cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
13735cee9645SThomas Gleixner 
1374f61eff83SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT
hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base * base)1375f61eff83SAnna-Maria Gleixner static void hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base)
1376f61eff83SAnna-Maria Gleixner {
1377f61eff83SAnna-Maria Gleixner 	spin_lock_init(&base->softirq_expiry_lock);
1378f61eff83SAnna-Maria Gleixner }
1379f61eff83SAnna-Maria Gleixner 
hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base * base)1380f61eff83SAnna-Maria Gleixner static void hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base)
1381330dd6d9SSebastian Andrzej Siewior 	__acquires(&base->softirq_expiry_lock)
1382f61eff83SAnna-Maria Gleixner {
1383f61eff83SAnna-Maria Gleixner 	spin_lock(&base->softirq_expiry_lock);
1384f61eff83SAnna-Maria Gleixner }
1385f61eff83SAnna-Maria Gleixner 
hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base * base)1386f61eff83SAnna-Maria Gleixner static void hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base)
1387330dd6d9SSebastian Andrzej Siewior 	__releases(&base->softirq_expiry_lock)
1388f61eff83SAnna-Maria Gleixner {
1389f61eff83SAnna-Maria Gleixner 	spin_unlock(&base->softirq_expiry_lock);
1390f61eff83SAnna-Maria Gleixner }
1391f61eff83SAnna-Maria Gleixner 
1392f61eff83SAnna-Maria Gleixner /*
1393f61eff83SAnna-Maria Gleixner  * The counterpart to hrtimer_cancel_wait_running().
1394f61eff83SAnna-Maria Gleixner  *
1395f61eff83SAnna-Maria Gleixner  * If there is a waiter for cpu_base->expiry_lock, then it was waiting for
13964bf07f65SIngo Molnar  * the timer callback to finish. Drop expiry_lock and reacquire it. That
1397f61eff83SAnna-Maria Gleixner  * allows the waiter to acquire the lock and make progress.
1398f61eff83SAnna-Maria Gleixner  */
hrtimer_sync_wait_running(struct hrtimer_cpu_base * cpu_base,unsigned long flags)1399f61eff83SAnna-Maria Gleixner static void hrtimer_sync_wait_running(struct hrtimer_cpu_base *cpu_base,
1400f61eff83SAnna-Maria Gleixner 				      unsigned long flags)
1401f61eff83SAnna-Maria Gleixner {
1402f61eff83SAnna-Maria Gleixner 	if (atomic_read(&cpu_base->timer_waiters)) {
1403f61eff83SAnna-Maria Gleixner 		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1404f61eff83SAnna-Maria Gleixner 		spin_unlock(&cpu_base->softirq_expiry_lock);
1405f61eff83SAnna-Maria Gleixner 		spin_lock(&cpu_base->softirq_expiry_lock);
1406f61eff83SAnna-Maria Gleixner 		raw_spin_lock_irq(&cpu_base->lock);
1407f61eff83SAnna-Maria Gleixner 	}
1408f61eff83SAnna-Maria Gleixner }
1409f61eff83SAnna-Maria Gleixner 
141027af31e4SAndy Shevchenko #ifdef CONFIG_SMP
is_migration_base(struct hrtimer_clock_base * base)141127af31e4SAndy Shevchenko static __always_inline bool is_migration_base(struct hrtimer_clock_base *base)
141227af31e4SAndy Shevchenko {
141327af31e4SAndy Shevchenko 	return base == &migration_base;
141427af31e4SAndy Shevchenko }
141527af31e4SAndy Shevchenko #else
is_migration_base(struct hrtimer_clock_base * base)141627af31e4SAndy Shevchenko static __always_inline bool is_migration_base(struct hrtimer_clock_base *base)
141727af31e4SAndy Shevchenko {
141827af31e4SAndy Shevchenko 	return false;
141927af31e4SAndy Shevchenko }
142027af31e4SAndy Shevchenko #endif
142127af31e4SAndy Shevchenko 
1422f61eff83SAnna-Maria Gleixner /*
1423f61eff83SAnna-Maria Gleixner  * This function is called on PREEMPT_RT kernels when the fast path
1424f61eff83SAnna-Maria Gleixner  * deletion of a timer failed because the timer callback function was
1425f61eff83SAnna-Maria Gleixner  * running.
1426f61eff83SAnna-Maria Gleixner  *
14270bee3b60SFrederic Weisbecker  * This prevents priority inversion: if the soft irq thread is preempted
14288fa7292fSThomas Gleixner  * in the middle of a timer callback, then calling hrtimer_cancel() can
14290bee3b60SFrederic Weisbecker  * lead to two issues:
14300bee3b60SFrederic Weisbecker  *
14310bee3b60SFrederic Weisbecker  *  - If the caller is on a remote CPU then it has to spin wait for the timer
14320bee3b60SFrederic Weisbecker  *    handler to complete. This can result in unbound priority inversion.
14330bee3b60SFrederic Weisbecker  *
14340bee3b60SFrederic Weisbecker  *  - If the caller originates from the task which preempted the timer
14350bee3b60SFrederic Weisbecker  *    handler on the same CPU, then spin waiting for the timer handler to
14360bee3b60SFrederic Weisbecker  *    complete is never going to end.
1437f61eff83SAnna-Maria Gleixner  */
hrtimer_cancel_wait_running(const struct hrtimer * timer)1438f61eff83SAnna-Maria Gleixner void hrtimer_cancel_wait_running(const struct hrtimer *timer)
1439f61eff83SAnna-Maria Gleixner {
1440dd2261edSJulien Grall 	/* Lockless read. Prevent the compiler from reloading it below */
1441dd2261edSJulien Grall 	struct hrtimer_clock_base *base = READ_ONCE(timer->base);
1442f61eff83SAnna-Maria Gleixner 
144368b2c8c1SJulien Grall 	/*
144468b2c8c1SJulien Grall 	 * Just relax if the timer expires in hard interrupt context or if
144568b2c8c1SJulien Grall 	 * it is currently on the migration base.
144668b2c8c1SJulien Grall 	 */
14475d2295f3SSebastian Andrzej Siewior 	if (!timer->is_soft || is_migration_base(base)) {
1448f61eff83SAnna-Maria Gleixner 		cpu_relax();
1449f61eff83SAnna-Maria Gleixner 		return;
1450f61eff83SAnna-Maria Gleixner 	}
1451f61eff83SAnna-Maria Gleixner 
1452f61eff83SAnna-Maria Gleixner 	/*
1453f61eff83SAnna-Maria Gleixner 	 * Mark the base as contended and grab the expiry lock, which is
1454f61eff83SAnna-Maria Gleixner 	 * held by the softirq across the timer callback. Drop the lock
1455f61eff83SAnna-Maria Gleixner 	 * immediately so the softirq can expire the next timer. In theory
1456f61eff83SAnna-Maria Gleixner 	 * the timer could already be running again, but that's more than
1457f61eff83SAnna-Maria Gleixner 	 * unlikely and just causes another wait loop.
1458f61eff83SAnna-Maria Gleixner 	 */
1459f61eff83SAnna-Maria Gleixner 	atomic_inc(&base->cpu_base->timer_waiters);
1460f61eff83SAnna-Maria Gleixner 	spin_lock_bh(&base->cpu_base->softirq_expiry_lock);
1461f61eff83SAnna-Maria Gleixner 	atomic_dec(&base->cpu_base->timer_waiters);
1462f61eff83SAnna-Maria Gleixner 	spin_unlock_bh(&base->cpu_base->softirq_expiry_lock);
1463f61eff83SAnna-Maria Gleixner }
1464f61eff83SAnna-Maria Gleixner #else
1465f61eff83SAnna-Maria Gleixner static inline void
hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base * base)1466f61eff83SAnna-Maria Gleixner hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) { }
1467f61eff83SAnna-Maria Gleixner static inline void
hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base * base)1468f61eff83SAnna-Maria Gleixner hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) { }
1469f61eff83SAnna-Maria Gleixner static inline void
hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base * base)1470f61eff83SAnna-Maria Gleixner hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) { }
hrtimer_sync_wait_running(struct hrtimer_cpu_base * base,unsigned long flags)1471f61eff83SAnna-Maria Gleixner static inline void hrtimer_sync_wait_running(struct hrtimer_cpu_base *base,
1472f61eff83SAnna-Maria Gleixner 					     unsigned long flags) { }
1473f61eff83SAnna-Maria Gleixner #endif
1474f61eff83SAnna-Maria Gleixner 
14755cee9645SThomas Gleixner /**
14765cee9645SThomas Gleixner  * hrtimer_cancel - cancel a timer and wait for the handler to finish.
14775cee9645SThomas Gleixner  * @timer:	the timer to be cancelled
14785cee9645SThomas Gleixner  *
14795cee9645SThomas Gleixner  * Returns:
14805cee9645SThomas Gleixner  *  0 when the timer was not active
14815cee9645SThomas Gleixner  *  1 when the timer was active
14825cee9645SThomas Gleixner  */
hrtimer_cancel(struct hrtimer * timer)14835cee9645SThomas Gleixner int hrtimer_cancel(struct hrtimer *timer)
14845cee9645SThomas Gleixner {
1485f61eff83SAnna-Maria Gleixner 	int ret;
14865cee9645SThomas Gleixner 
1487f61eff83SAnna-Maria Gleixner 	do {
1488f61eff83SAnna-Maria Gleixner 		ret = hrtimer_try_to_cancel(timer);
1489f61eff83SAnna-Maria Gleixner 
1490f61eff83SAnna-Maria Gleixner 		if (ret < 0)
1491f61eff83SAnna-Maria Gleixner 			hrtimer_cancel_wait_running(timer);
1492f61eff83SAnna-Maria Gleixner 	} while (ret < 0);
14935cee9645SThomas Gleixner 	return ret;
14945cee9645SThomas Gleixner }
14955cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_cancel);
14965cee9645SThomas Gleixner 
14975cee9645SThomas Gleixner /**
149866981c37SMauro Carvalho Chehab  * __hrtimer_get_remaining - get remaining time for the timer
14995cee9645SThomas Gleixner  * @timer:	the timer to read
1500203cbf77SThomas Gleixner  * @adjust:	adjust relative timers when CONFIG_TIME_LOW_RES=y
15015cee9645SThomas Gleixner  */
__hrtimer_get_remaining(const struct hrtimer * timer,bool adjust)1502203cbf77SThomas Gleixner ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust)
15035cee9645SThomas Gleixner {
15045cee9645SThomas Gleixner 	unsigned long flags;
15055cee9645SThomas Gleixner 	ktime_t rem;
15065cee9645SThomas Gleixner 
15075cee9645SThomas Gleixner 	lock_hrtimer_base(timer, &flags);
1508203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust)
1509203cbf77SThomas Gleixner 		rem = hrtimer_expires_remaining_adjusted(timer);
1510203cbf77SThomas Gleixner 	else
15115cee9645SThomas Gleixner 		rem = hrtimer_expires_remaining(timer);
15125cee9645SThomas Gleixner 	unlock_hrtimer_base(timer, &flags);
15135cee9645SThomas Gleixner 
15145cee9645SThomas Gleixner 	return rem;
15155cee9645SThomas Gleixner }
1516203cbf77SThomas Gleixner EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);
15175cee9645SThomas Gleixner 
15185cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
15195cee9645SThomas Gleixner /**
15205cee9645SThomas Gleixner  * hrtimer_get_next_event - get the time until next expiry event
15215cee9645SThomas Gleixner  *
1522c1ad348bSThomas Gleixner  * Returns the next expiry time or KTIME_MAX if no timer is pending.
15235cee9645SThomas Gleixner  */
hrtimer_get_next_event(void)1524c1ad348bSThomas Gleixner u64 hrtimer_get_next_event(void)
15255cee9645SThomas Gleixner {
1526dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1527c1ad348bSThomas Gleixner 	u64 expires = KTIME_MAX;
15285cee9645SThomas Gleixner 	unsigned long flags;
15295cee9645SThomas Gleixner 
15305cee9645SThomas Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
15315cee9645SThomas Gleixner 
1532b7c8e1f8SJiapeng Chong 	if (!hrtimer_hres_active(cpu_base))
15335da70160SAnna-Maria Gleixner 		expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
15345cee9645SThomas Gleixner 
15355cee9645SThomas Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
15365cee9645SThomas Gleixner 
1537c1ad348bSThomas Gleixner 	return expires;
15385cee9645SThomas Gleixner }
1539a59855cdSRafael J. Wysocki 
1540a59855cdSRafael J. Wysocki /**
1541a59855cdSRafael J. Wysocki  * hrtimer_next_event_without - time until next expiry event w/o one timer
1542a59855cdSRafael J. Wysocki  * @exclude:	timer to exclude
1543a59855cdSRafael J. Wysocki  *
1544a59855cdSRafael J. Wysocki  * Returns the next expiry time over all timers except for the @exclude one or
1545a59855cdSRafael J. Wysocki  * KTIME_MAX if none of them is pending.
1546a59855cdSRafael J. Wysocki  */
hrtimer_next_event_without(const struct hrtimer * exclude)1547a59855cdSRafael J. Wysocki u64 hrtimer_next_event_without(const struct hrtimer *exclude)
1548a59855cdSRafael J. Wysocki {
1549a59855cdSRafael J. Wysocki 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1550a59855cdSRafael J. Wysocki 	u64 expires = KTIME_MAX;
1551a59855cdSRafael J. Wysocki 	unsigned long flags;
1552a59855cdSRafael J. Wysocki 
1553a59855cdSRafael J. Wysocki 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
1554a59855cdSRafael J. Wysocki 
1555b7c8e1f8SJiapeng Chong 	if (hrtimer_hres_active(cpu_base)) {
1556a59855cdSRafael J. Wysocki 		unsigned int active;
1557a59855cdSRafael J. Wysocki 
1558a59855cdSRafael J. Wysocki 		if (!cpu_base->softirq_activated) {
1559a59855cdSRafael J. Wysocki 			active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
1560a59855cdSRafael J. Wysocki 			expires = __hrtimer_next_event_base(cpu_base, exclude,
1561a59855cdSRafael J. Wysocki 							    active, KTIME_MAX);
1562a59855cdSRafael J. Wysocki 		}
1563a59855cdSRafael J. Wysocki 		active = cpu_base->active_bases & HRTIMER_ACTIVE_HARD;
1564a59855cdSRafael J. Wysocki 		expires = __hrtimer_next_event_base(cpu_base, exclude, active,
1565a59855cdSRafael J. Wysocki 						    expires);
1566a59855cdSRafael J. Wysocki 	}
1567a59855cdSRafael J. Wysocki 
1568a59855cdSRafael J. Wysocki 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1569a59855cdSRafael J. Wysocki 
1570a59855cdSRafael J. Wysocki 	return expires;
1571a59855cdSRafael J. Wysocki }
15725cee9645SThomas Gleixner #endif
15735cee9645SThomas Gleixner 
hrtimer_clockid_to_base(clockid_t clock_id)1574336a9cdeSMarc Zyngier static inline int hrtimer_clockid_to_base(clockid_t clock_id)
1575336a9cdeSMarc Zyngier {
15764441b976SAndy Shevchenko 	switch (clock_id) {
15774441b976SAndy Shevchenko 	case CLOCK_REALTIME:
15784441b976SAndy Shevchenko 		return HRTIMER_BASE_REALTIME;
15794441b976SAndy Shevchenko 	case CLOCK_MONOTONIC:
15804441b976SAndy Shevchenko 		return HRTIMER_BASE_MONOTONIC;
15814441b976SAndy Shevchenko 	case CLOCK_BOOTTIME:
15824441b976SAndy Shevchenko 		return HRTIMER_BASE_BOOTTIME;
15834441b976SAndy Shevchenko 	case CLOCK_TAI:
15844441b976SAndy Shevchenko 		return HRTIMER_BASE_TAI;
15854441b976SAndy Shevchenko 	default:
1586336a9cdeSMarc Zyngier 		WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id);
1587336a9cdeSMarc Zyngier 		return HRTIMER_BASE_MONOTONIC;
1588336a9cdeSMarc Zyngier 	}
15894441b976SAndy Shevchenko }
1590336a9cdeSMarc Zyngier 
__hrtimer_setup(struct hrtimer * timer,enum hrtimer_restart (* function)(struct hrtimer *),clockid_t clock_id,enum hrtimer_mode mode)159187d82cffSNam Cao static void __hrtimer_setup(struct hrtimer *timer,
159287d82cffSNam Cao 			    enum hrtimer_restart (*function)(struct hrtimer *),
159387d82cffSNam Cao 			    clockid_t clock_id, enum hrtimer_mode mode)
15945cee9645SThomas Gleixner {
159542f42da4SAnna-Maria Gleixner 	bool softtimer = !!(mode & HRTIMER_MODE_SOFT);
15965cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base;
1597f5c2f021SSebastian Andrzej Siewior 	int base;
1598f5c2f021SSebastian Andrzej Siewior 
1599f5c2f021SSebastian Andrzej Siewior 	/*
16004bf07f65SIngo Molnar 	 * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
1601f5c2f021SSebastian Andrzej Siewior 	 * marked for hard interrupt expiry mode are moved into soft
1602f5c2f021SSebastian Andrzej Siewior 	 * interrupt context for latency reasons and because the callbacks
1603f5c2f021SSebastian Andrzej Siewior 	 * can invoke functions which might sleep on RT, e.g. spin_lock().
1604f5c2f021SSebastian Andrzej Siewior 	 */
1605f5c2f021SSebastian Andrzej Siewior 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD))
1606f5c2f021SSebastian Andrzej Siewior 		softtimer = true;
16075cee9645SThomas Gleixner 
16085cee9645SThomas Gleixner 	memset(timer, 0, sizeof(struct hrtimer));
16095cee9645SThomas Gleixner 
161022127e93SChristoph Lameter 	cpu_base = raw_cpu_ptr(&hrtimer_bases);
16115cee9645SThomas Gleixner 
161248d0c9beSAnna-Maria Gleixner 	/*
161348d0c9beSAnna-Maria Gleixner 	 * POSIX magic: Relative CLOCK_REALTIME timers are not affected by
161448d0c9beSAnna-Maria Gleixner 	 * clock modifications, so they needs to become CLOCK_MONOTONIC to
161548d0c9beSAnna-Maria Gleixner 	 * ensure POSIX compliance.
161648d0c9beSAnna-Maria Gleixner 	 */
161748d0c9beSAnna-Maria Gleixner 	if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL)
16185cee9645SThomas Gleixner 		clock_id = CLOCK_MONOTONIC;
16195cee9645SThomas Gleixner 
1620f5c2f021SSebastian Andrzej Siewior 	base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0;
162142f42da4SAnna-Maria Gleixner 	base += hrtimer_clockid_to_base(clock_id);
162242f42da4SAnna-Maria Gleixner 	timer->is_soft = softtimer;
162340db1739SSebastian Andrzej Siewior 	timer->is_hard = !!(mode & HRTIMER_MODE_HARD);
16245cee9645SThomas Gleixner 	timer->base = &cpu_base->clock_base[base];
16255cee9645SThomas Gleixner 	timerqueue_init(&timer->node);
1626908a1d77SNam Cao 
1627908a1d77SNam Cao 	if (WARN_ON_ONCE(!function))
162804257da0SNam Cao 		ACCESS_PRIVATE(timer, function) = hrtimer_dummy_timeout;
1629908a1d77SNam Cao 	else
163004257da0SNam Cao 		ACCESS_PRIVATE(timer, function) = function;
1631908a1d77SNam Cao }
1632908a1d77SNam Cao 
16335cee9645SThomas Gleixner /**
1634908a1d77SNam Cao  * hrtimer_setup - initialize a timer to the given clock
1635908a1d77SNam Cao  * @timer:	the timer to be initialized
1636908a1d77SNam Cao  * @function:	the callback function
1637908a1d77SNam Cao  * @clock_id:	the clock to be used
1638908a1d77SNam Cao  * @mode:       The modes which are relevant for initialization:
1639908a1d77SNam Cao  *              HRTIMER_MODE_ABS, HRTIMER_MODE_REL, HRTIMER_MODE_ABS_SOFT,
1640908a1d77SNam Cao  *              HRTIMER_MODE_REL_SOFT
1641908a1d77SNam Cao  *
1642908a1d77SNam Cao  *              The PINNED variants of the above can be handed in,
1643908a1d77SNam Cao  *              but the PINNED bit is ignored as pinning happens
1644908a1d77SNam Cao  *              when the hrtimer is started
1645908a1d77SNam Cao  */
hrtimer_setup(struct hrtimer * timer,enum hrtimer_restart (* function)(struct hrtimer *),clockid_t clock_id,enum hrtimer_mode mode)1646908a1d77SNam Cao void hrtimer_setup(struct hrtimer *timer, enum hrtimer_restart (*function)(struct hrtimer *),
1647908a1d77SNam Cao 		   clockid_t clock_id, enum hrtimer_mode mode)
1648908a1d77SNam Cao {
1649e9ef2093SNam Cao 	debug_setup(timer, clock_id, mode);
1650908a1d77SNam Cao 	__hrtimer_setup(timer, function, clock_id, mode);
1651908a1d77SNam Cao }
1652908a1d77SNam Cao EXPORT_SYMBOL_GPL(hrtimer_setup);
1653908a1d77SNam Cao 
1654908a1d77SNam Cao /**
1655444cb7dbSNam Cao  * hrtimer_setup_on_stack - initialize a timer on stack memory
1656444cb7dbSNam Cao  * @timer:	The timer to be initialized
1657444cb7dbSNam Cao  * @function:	the callback function
1658444cb7dbSNam Cao  * @clock_id:	The clock to be used
1659444cb7dbSNam Cao  * @mode:       The timer mode
1660444cb7dbSNam Cao  *
1661444cb7dbSNam Cao  * Similar to hrtimer_setup(), except that this one must be used if struct hrtimer is in stack
1662444cb7dbSNam Cao  * memory.
1663444cb7dbSNam Cao  */
hrtimer_setup_on_stack(struct hrtimer * timer,enum hrtimer_restart (* function)(struct hrtimer *),clockid_t clock_id,enum hrtimer_mode mode)1664444cb7dbSNam Cao void hrtimer_setup_on_stack(struct hrtimer *timer,
1665444cb7dbSNam Cao 			    enum hrtimer_restart (*function)(struct hrtimer *),
1666444cb7dbSNam Cao 			    clockid_t clock_id, enum hrtimer_mode mode)
1667444cb7dbSNam Cao {
166859c9edafSNam Cao 	debug_setup_on_stack(timer, clock_id, mode);
1669444cb7dbSNam Cao 	__hrtimer_setup(timer, function, clock_id, mode);
1670444cb7dbSNam Cao }
1671444cb7dbSNam Cao EXPORT_SYMBOL_GPL(hrtimer_setup_on_stack);
1672444cb7dbSNam Cao 
1673887d9dc9SPeter Zijlstra /*
1674887d9dc9SPeter Zijlstra  * A timer is active, when it is enqueued into the rbtree or the
1675887d9dc9SPeter Zijlstra  * callback function is running or it's in the state of being migrated
1676887d9dc9SPeter Zijlstra  * to another cpu.
16775cee9645SThomas Gleixner  *
1678887d9dc9SPeter Zijlstra  * It is important for this function to not return a false negative.
16795cee9645SThomas Gleixner  */
hrtimer_active(const struct hrtimer * timer)1680887d9dc9SPeter Zijlstra bool hrtimer_active(const struct hrtimer *timer)
16815cee9645SThomas Gleixner {
16823f0b9e8eSAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1683887d9dc9SPeter Zijlstra 	unsigned int seq;
16845cee9645SThomas Gleixner 
1685887d9dc9SPeter Zijlstra 	do {
16863f0b9e8eSAnna-Maria Gleixner 		base = READ_ONCE(timer->base);
16873f0b9e8eSAnna-Maria Gleixner 		seq = raw_read_seqcount_begin(&base->seq);
16885cee9645SThomas Gleixner 
1689887d9dc9SPeter Zijlstra 		if (timer->state != HRTIMER_STATE_INACTIVE ||
16903f0b9e8eSAnna-Maria Gleixner 		    base->running == timer)
1691887d9dc9SPeter Zijlstra 			return true;
1692887d9dc9SPeter Zijlstra 
16933f0b9e8eSAnna-Maria Gleixner 	} while (read_seqcount_retry(&base->seq, seq) ||
16943f0b9e8eSAnna-Maria Gleixner 		 base != READ_ONCE(timer->base));
1695887d9dc9SPeter Zijlstra 
1696887d9dc9SPeter Zijlstra 	return false;
16975cee9645SThomas Gleixner }
1698887d9dc9SPeter Zijlstra EXPORT_SYMBOL_GPL(hrtimer_active);
16995cee9645SThomas Gleixner 
1700887d9dc9SPeter Zijlstra /*
1701887d9dc9SPeter Zijlstra  * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3
1702887d9dc9SPeter Zijlstra  * distinct sections:
1703887d9dc9SPeter Zijlstra  *
1704887d9dc9SPeter Zijlstra  *  - queued:	the timer is queued
1705887d9dc9SPeter Zijlstra  *  - callback:	the timer is being ran
1706887d9dc9SPeter Zijlstra  *  - post:	the timer is inactive or (re)queued
1707887d9dc9SPeter Zijlstra  *
1708887d9dc9SPeter Zijlstra  * On the read side we ensure we observe timer->state and cpu_base->running
1709887d9dc9SPeter Zijlstra  * from the same section, if anything changed while we looked at it, we retry.
1710887d9dc9SPeter Zijlstra  * This includes timer->base changing because sequence numbers alone are
1711887d9dc9SPeter Zijlstra  * insufficient for that.
1712887d9dc9SPeter Zijlstra  *
1713887d9dc9SPeter Zijlstra  * The sequence numbers are required because otherwise we could still observe
17144bf07f65SIngo Molnar  * a false negative if the read side got smeared over multiple consecutive
1715887d9dc9SPeter Zijlstra  * __run_hrtimer() invocations.
1716887d9dc9SPeter Zijlstra  */
1717887d9dc9SPeter Zijlstra 
__run_hrtimer(struct hrtimer_cpu_base * cpu_base,struct hrtimer_clock_base * base,struct hrtimer * timer,ktime_t * now,unsigned long flags)171821d6d52aSThomas Gleixner static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
171921d6d52aSThomas Gleixner 			  struct hrtimer_clock_base *base,
1720dd934aa8SAnna-Maria Gleixner 			  struct hrtimer *timer, ktime_t *now,
1721eb5a4d0aSJules Irenge 			  unsigned long flags) __must_hold(&cpu_base->lock)
17225cee9645SThomas Gleixner {
17235cee9645SThomas Gleixner 	enum hrtimer_restart (*fn)(struct hrtimer *);
172473d20564SSebastian Andrzej Siewior 	bool expires_in_hardirq;
17255cee9645SThomas Gleixner 	int restart;
17265cee9645SThomas Gleixner 
1727887d9dc9SPeter Zijlstra 	lockdep_assert_held(&cpu_base->lock);
17285cee9645SThomas Gleixner 
17295cee9645SThomas Gleixner 	debug_deactivate(timer);
17303f0b9e8eSAnna-Maria Gleixner 	base->running = timer;
1731887d9dc9SPeter Zijlstra 
1732887d9dc9SPeter Zijlstra 	/*
1733887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1734887d9dc9SPeter Zijlstra 	 *
1735887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
17363f0b9e8eSAnna-Maria Gleixner 	 * hrtimer_active() cannot observe base->running == NULL &&
1737887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
1738887d9dc9SPeter Zijlstra 	 */
17393f0b9e8eSAnna-Maria Gleixner 	raw_write_seqcount_barrier(&base->seq);
1740887d9dc9SPeter Zijlstra 
1741887d9dc9SPeter Zijlstra 	__remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
174204257da0SNam Cao 	fn = ACCESS_PRIVATE(timer, function);
17435cee9645SThomas Gleixner 
17445cee9645SThomas Gleixner 	/*
1745203cbf77SThomas Gleixner 	 * Clear the 'is relative' flag for the TIME_LOW_RES case. If the
1746203cbf77SThomas Gleixner 	 * timer is restarted with a period then it becomes an absolute
1747203cbf77SThomas Gleixner 	 * timer. If its not restarted it does not matter.
1748203cbf77SThomas Gleixner 	 */
1749203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES))
1750203cbf77SThomas Gleixner 		timer->is_rel = false;
1751203cbf77SThomas Gleixner 
1752203cbf77SThomas Gleixner 	/*
1753d05ca13bSThomas Gleixner 	 * The timer is marked as running in the CPU base, so it is
1754d05ca13bSThomas Gleixner 	 * protected against migration to a different CPU even if the lock
1755d05ca13bSThomas Gleixner 	 * is dropped.
17565cee9645SThomas Gleixner 	 */
1757dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
17585cee9645SThomas Gleixner 	trace_hrtimer_expire_entry(timer, now);
175973d20564SSebastian Andrzej Siewior 	expires_in_hardirq = lockdep_hrtimer_enter(timer);
176040db1739SSebastian Andrzej Siewior 
17615cee9645SThomas Gleixner 	restart = fn(timer);
176240db1739SSebastian Andrzej Siewior 
176373d20564SSebastian Andrzej Siewior 	lockdep_hrtimer_exit(expires_in_hardirq);
17645cee9645SThomas Gleixner 	trace_hrtimer_expire_exit(timer);
1765dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irq(&cpu_base->lock);
17665cee9645SThomas Gleixner 
17675cee9645SThomas Gleixner 	/*
1768887d9dc9SPeter Zijlstra 	 * Note: We clear the running state after enqueue_hrtimer and
1769b4d90e9fSPratyush Patel 	 * we do not reprogram the event hardware. Happens either in
17705cee9645SThomas Gleixner 	 * hrtimer_start_range_ns() or in hrtimer_interrupt()
17715de2755cSPeter Zijlstra 	 *
17725de2755cSPeter Zijlstra 	 * Note: Because we dropped the cpu_base->lock above,
17735de2755cSPeter Zijlstra 	 * hrtimer_start_range_ns() can have popped in and enqueued the timer
17745de2755cSPeter Zijlstra 	 * for us already.
17755cee9645SThomas Gleixner 	 */
17765de2755cSPeter Zijlstra 	if (restart != HRTIMER_NORESTART &&
17775de2755cSPeter Zijlstra 	    !(timer->state & HRTIMER_STATE_ENQUEUED))
177863e2ed36SAnna-Maria Gleixner 		enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS);
17795cee9645SThomas Gleixner 
17805cee9645SThomas Gleixner 	/*
1781887d9dc9SPeter Zijlstra 	 * Separate the ->running assignment from the ->state assignment.
1782887d9dc9SPeter Zijlstra 	 *
1783887d9dc9SPeter Zijlstra 	 * As with a regular write barrier, this ensures the read side in
17843f0b9e8eSAnna-Maria Gleixner 	 * hrtimer_active() cannot observe base->running.timer == NULL &&
1785887d9dc9SPeter Zijlstra 	 * timer->state == INACTIVE.
17865cee9645SThomas Gleixner 	 */
17873f0b9e8eSAnna-Maria Gleixner 	raw_write_seqcount_barrier(&base->seq);
17885cee9645SThomas Gleixner 
17893f0b9e8eSAnna-Maria Gleixner 	WARN_ON_ONCE(base->running != timer);
17903f0b9e8eSAnna-Maria Gleixner 	base->running = NULL;
17915cee9645SThomas Gleixner }
17925cee9645SThomas Gleixner 
__hrtimer_run_queues(struct hrtimer_cpu_base * cpu_base,ktime_t now,unsigned long flags,unsigned int active_mask)1793dd934aa8SAnna-Maria Gleixner static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now,
1794c458b1d1SAnna-Maria Gleixner 				 unsigned long flags, unsigned int active_mask)
17955cee9645SThomas Gleixner {
1796c272ca58SAnna-Maria Gleixner 	struct hrtimer_clock_base *base;
1797c458b1d1SAnna-Maria Gleixner 	unsigned int active = cpu_base->active_bases & active_mask;
17985cee9645SThomas Gleixner 
1799c272ca58SAnna-Maria Gleixner 	for_each_active_base(base, cpu_base, active) {
18005cee9645SThomas Gleixner 		struct timerqueue_node *node;
18015cee9645SThomas Gleixner 		ktime_t basenow;
18025cee9645SThomas Gleixner 
18035cee9645SThomas Gleixner 		basenow = ktime_add(now, base->offset);
18045cee9645SThomas Gleixner 
18055cee9645SThomas Gleixner 		while ((node = timerqueue_getnext(&base->active))) {
18065cee9645SThomas Gleixner 			struct hrtimer *timer;
18075cee9645SThomas Gleixner 
18085cee9645SThomas Gleixner 			timer = container_of(node, struct hrtimer, node);
18095cee9645SThomas Gleixner 
18105cee9645SThomas Gleixner 			/*
18115cee9645SThomas Gleixner 			 * The immediate goal for using the softexpires is
18125cee9645SThomas Gleixner 			 * minimizing wakeups, not running timers at the
18135cee9645SThomas Gleixner 			 * earliest interrupt after their soft expiration.
18145cee9645SThomas Gleixner 			 * This allows us to avoid using a Priority Search
18154bf07f65SIngo Molnar 			 * Tree, which can answer a stabbing query for
18165cee9645SThomas Gleixner 			 * overlapping intervals and instead use the simple
18175cee9645SThomas Gleixner 			 * BST we already have.
18185cee9645SThomas Gleixner 			 * We don't add extra wakeups by delaying timers that
18195cee9645SThomas Gleixner 			 * are right-of a not yet expired timer, because that
18205cee9645SThomas Gleixner 			 * timer will have to trigger a wakeup anyway.
18215cee9645SThomas Gleixner 			 */
18222456e855SThomas Gleixner 			if (basenow < hrtimer_get_softexpires_tv64(timer))
18235cee9645SThomas Gleixner 				break;
18245cee9645SThomas Gleixner 
1825dd934aa8SAnna-Maria Gleixner 			__run_hrtimer(cpu_base, base, timer, &basenow, flags);
1826f61eff83SAnna-Maria Gleixner 			if (active_mask == HRTIMER_ACTIVE_SOFT)
1827f61eff83SAnna-Maria Gleixner 				hrtimer_sync_wait_running(cpu_base, flags);
18285cee9645SThomas Gleixner 		}
18295cee9645SThomas Gleixner 	}
183021d6d52aSThomas Gleixner }
183121d6d52aSThomas Gleixner 
hrtimer_run_softirq(void)1832e68ac2b4SCaleb Sander Mateos static __latent_entropy void hrtimer_run_softirq(void)
18335da70160SAnna-Maria Gleixner {
18345da70160SAnna-Maria Gleixner 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
18355da70160SAnna-Maria Gleixner 	unsigned long flags;
18365da70160SAnna-Maria Gleixner 	ktime_t now;
18375da70160SAnna-Maria Gleixner 
1838f61eff83SAnna-Maria Gleixner 	hrtimer_cpu_base_lock_expiry(cpu_base);
18395da70160SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
18405da70160SAnna-Maria Gleixner 
18415da70160SAnna-Maria Gleixner 	now = hrtimer_update_base(cpu_base);
18425da70160SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_SOFT);
18435da70160SAnna-Maria Gleixner 
18445da70160SAnna-Maria Gleixner 	cpu_base->softirq_activated = 0;
18455da70160SAnna-Maria Gleixner 	hrtimer_update_softirq_timer(cpu_base, true);
18465da70160SAnna-Maria Gleixner 
18475da70160SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1848f61eff83SAnna-Maria Gleixner 	hrtimer_cpu_base_unlock_expiry(cpu_base);
18495da70160SAnna-Maria Gleixner }
18505da70160SAnna-Maria Gleixner 
185121d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
185221d6d52aSThomas Gleixner 
185321d6d52aSThomas Gleixner /*
185421d6d52aSThomas Gleixner  * High resolution timer interrupt
185521d6d52aSThomas Gleixner  * Called with interrupts disabled
185621d6d52aSThomas Gleixner  */
hrtimer_interrupt(struct clock_event_device * dev)185721d6d52aSThomas Gleixner void hrtimer_interrupt(struct clock_event_device *dev)
185821d6d52aSThomas Gleixner {
185921d6d52aSThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
186021d6d52aSThomas Gleixner 	ktime_t expires_next, now, entry_time, delta;
1861dd934aa8SAnna-Maria Gleixner 	unsigned long flags;
186221d6d52aSThomas Gleixner 	int retries = 0;
186321d6d52aSThomas Gleixner 
186421d6d52aSThomas Gleixner 	BUG_ON(!cpu_base->hres_active);
186521d6d52aSThomas Gleixner 	cpu_base->nr_events++;
18662456e855SThomas Gleixner 	dev->next_event = KTIME_MAX;
186721d6d52aSThomas Gleixner 
1868dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
186921d6d52aSThomas Gleixner 	entry_time = now = hrtimer_update_base(cpu_base);
187021d6d52aSThomas Gleixner retry:
187121d6d52aSThomas Gleixner 	cpu_base->in_hrtirq = 1;
187221d6d52aSThomas Gleixner 	/*
187321d6d52aSThomas Gleixner 	 * We set expires_next to KTIME_MAX here with cpu_base->lock
187421d6d52aSThomas Gleixner 	 * held to prevent that a timer is enqueued in our queue via
187521d6d52aSThomas Gleixner 	 * the migration code. This does not affect enqueueing of
187621d6d52aSThomas Gleixner 	 * timers which run their callback and need to be requeued on
187721d6d52aSThomas Gleixner 	 * this CPU.
187821d6d52aSThomas Gleixner 	 */
18792456e855SThomas Gleixner 	cpu_base->expires_next = KTIME_MAX;
188021d6d52aSThomas Gleixner 
18815da70160SAnna-Maria Gleixner 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
18825da70160SAnna-Maria Gleixner 		cpu_base->softirq_expires_next = KTIME_MAX;
18835da70160SAnna-Maria Gleixner 		cpu_base->softirq_activated = 1;
188449a17639SSebastian Andrzej Siewior 		raise_timer_softirq(HRTIMER_SOFTIRQ);
18855da70160SAnna-Maria Gleixner 	}
18865da70160SAnna-Maria Gleixner 
1887c458b1d1SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
188821d6d52aSThomas Gleixner 
188946eb1701SAnna-Maria Behnsen 	/* Reevaluate the clock bases for the [soft] next expiry */
189046eb1701SAnna-Maria Behnsen 	expires_next = hrtimer_update_next_event(cpu_base);
18915cee9645SThomas Gleixner 	/*
18925cee9645SThomas Gleixner 	 * Store the new expiry value so the migration code can verify
18935cee9645SThomas Gleixner 	 * against it.
18945cee9645SThomas Gleixner 	 */
18955cee9645SThomas Gleixner 	cpu_base->expires_next = expires_next;
18969bc74919SThomas Gleixner 	cpu_base->in_hrtirq = 0;
1897dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
18985cee9645SThomas Gleixner 
18995cee9645SThomas Gleixner 	/* Reprogramming necessary ? */
1900d2540875SViresh Kumar 	if (!tick_program_event(expires_next, 0)) {
19015cee9645SThomas Gleixner 		cpu_base->hang_detected = 0;
19025cee9645SThomas Gleixner 		return;
19035cee9645SThomas Gleixner 	}
19045cee9645SThomas Gleixner 
19055cee9645SThomas Gleixner 	/*
19065cee9645SThomas Gleixner 	 * The next timer was already expired due to:
19075cee9645SThomas Gleixner 	 * - tracing
19085cee9645SThomas Gleixner 	 * - long lasting callbacks
19095cee9645SThomas Gleixner 	 * - being scheduled away when running in a VM
19105cee9645SThomas Gleixner 	 *
19115cee9645SThomas Gleixner 	 * We need to prevent that we loop forever in the hrtimer
19125cee9645SThomas Gleixner 	 * interrupt routine. We give it 3 attempts to avoid
19135cee9645SThomas Gleixner 	 * overreacting on some spurious event.
19145cee9645SThomas Gleixner 	 *
19155cee9645SThomas Gleixner 	 * Acquire base lock for updating the offsets and retrieving
19165cee9645SThomas Gleixner 	 * the current time.
19175cee9645SThomas Gleixner 	 */
1918dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
19195cee9645SThomas Gleixner 	now = hrtimer_update_base(cpu_base);
19205cee9645SThomas Gleixner 	cpu_base->nr_retries++;
19215cee9645SThomas Gleixner 	if (++retries < 3)
19225cee9645SThomas Gleixner 		goto retry;
19235cee9645SThomas Gleixner 	/*
19245cee9645SThomas Gleixner 	 * Give the system a chance to do something else than looping
19255cee9645SThomas Gleixner 	 * here. We stored the entry time, so we know exactly how long
19265cee9645SThomas Gleixner 	 * we spent here. We schedule the next event this amount of
19275cee9645SThomas Gleixner 	 * time away.
19285cee9645SThomas Gleixner 	 */
19295cee9645SThomas Gleixner 	cpu_base->nr_hangs++;
19305cee9645SThomas Gleixner 	cpu_base->hang_detected = 1;
1931dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1932dd934aa8SAnna-Maria Gleixner 
19335cee9645SThomas Gleixner 	delta = ktime_sub(now, entry_time);
19342456e855SThomas Gleixner 	if ((unsigned int)delta > cpu_base->max_hang_time)
19352456e855SThomas Gleixner 		cpu_base->max_hang_time = (unsigned int) delta;
19365cee9645SThomas Gleixner 	/*
19375cee9645SThomas Gleixner 	 * Limit it to a sensible value as we enforce a longer
19385cee9645SThomas Gleixner 	 * delay. Give the CPU at least 100ms to catch up.
19395cee9645SThomas Gleixner 	 */
19402456e855SThomas Gleixner 	if (delta > 100 * NSEC_PER_MSEC)
19415cee9645SThomas Gleixner 		expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
19425cee9645SThomas Gleixner 	else
19435cee9645SThomas Gleixner 		expires_next = ktime_add(now, delta);
19445cee9645SThomas Gleixner 	tick_program_event(expires_next, 1);
19457a6e5537SGeert Uytterhoeven 	pr_warn_once("hrtimer: interrupt took %llu ns\n", ktime_to_ns(delta));
19465cee9645SThomas Gleixner }
19475cee9645SThomas Gleixner #endif /* !CONFIG_HIGH_RES_TIMERS */
19485cee9645SThomas Gleixner 
19495cee9645SThomas Gleixner /*
1950c6eb3f70SThomas Gleixner  * Called from run_local_timers in hardirq context every jiffy
19515cee9645SThomas Gleixner  */
hrtimer_run_queues(void)19525cee9645SThomas Gleixner void hrtimer_run_queues(void)
19535cee9645SThomas Gleixner {
1954dc5df73bSChristoph Lameter 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
1955dd934aa8SAnna-Maria Gleixner 	unsigned long flags;
195621d6d52aSThomas Gleixner 	ktime_t now;
19575cee9645SThomas Gleixner 
1958b7c8e1f8SJiapeng Chong 	if (hrtimer_hres_active(cpu_base))
19595cee9645SThomas Gleixner 		return;
19605cee9645SThomas Gleixner 
1961c6eb3f70SThomas Gleixner 	/*
1962c6eb3f70SThomas Gleixner 	 * This _is_ ugly: We have to check periodically, whether we
1963c6eb3f70SThomas Gleixner 	 * can switch to highres and / or nohz mode. The clocksource
1964c6eb3f70SThomas Gleixner 	 * switch happens with xtime_lock held. Notification from
1965c6eb3f70SThomas Gleixner 	 * there only sets the check bit in the tick_oneshot code,
1966c6eb3f70SThomas Gleixner 	 * otherwise we might deadlock vs. xtime_lock.
1967c6eb3f70SThomas Gleixner 	 */
1968c6eb3f70SThomas Gleixner 	if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) {
1969c6eb3f70SThomas Gleixner 		hrtimer_switch_to_hres();
1970c6eb3f70SThomas Gleixner 		return;
19715cee9645SThomas Gleixner 	}
19725cee9645SThomas Gleixner 
1973dd934aa8SAnna-Maria Gleixner 	raw_spin_lock_irqsave(&cpu_base->lock, flags);
197421d6d52aSThomas Gleixner 	now = hrtimer_update_base(cpu_base);
19755da70160SAnna-Maria Gleixner 
19765da70160SAnna-Maria Gleixner 	if (!ktime_before(now, cpu_base->softirq_expires_next)) {
19775da70160SAnna-Maria Gleixner 		cpu_base->softirq_expires_next = KTIME_MAX;
19785da70160SAnna-Maria Gleixner 		cpu_base->softirq_activated = 1;
197949a17639SSebastian Andrzej Siewior 		raise_timer_softirq(HRTIMER_SOFTIRQ);
19805da70160SAnna-Maria Gleixner 	}
19815da70160SAnna-Maria Gleixner 
1982c458b1d1SAnna-Maria Gleixner 	__hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
1983dd934aa8SAnna-Maria Gleixner 	raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
19845cee9645SThomas Gleixner }
19855cee9645SThomas Gleixner 
19865cee9645SThomas Gleixner /*
19875cee9645SThomas Gleixner  * Sleep related functions:
19885cee9645SThomas Gleixner  */
hrtimer_wakeup(struct hrtimer * timer)19895cee9645SThomas Gleixner static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
19905cee9645SThomas Gleixner {
19915cee9645SThomas Gleixner 	struct hrtimer_sleeper *t =
19925cee9645SThomas Gleixner 		container_of(timer, struct hrtimer_sleeper, timer);
19935cee9645SThomas Gleixner 	struct task_struct *task = t->task;
19945cee9645SThomas Gleixner 
19955cee9645SThomas Gleixner 	t->task = NULL;
19965cee9645SThomas Gleixner 	if (task)
19975cee9645SThomas Gleixner 		wake_up_process(task);
19985cee9645SThomas Gleixner 
19995cee9645SThomas Gleixner 	return HRTIMER_NORESTART;
20005cee9645SThomas Gleixner }
20015cee9645SThomas Gleixner 
200201656464SThomas Gleixner /**
200301656464SThomas Gleixner  * hrtimer_sleeper_start_expires - Start a hrtimer sleeper timer
200401656464SThomas Gleixner  * @sl:		sleeper to be started
200501656464SThomas Gleixner  * @mode:	timer mode abs/rel
200601656464SThomas Gleixner  *
200701656464SThomas Gleixner  * Wrapper around hrtimer_start_expires() for hrtimer_sleeper based timers
200801656464SThomas Gleixner  * to allow PREEMPT_RT to tweak the delivery mode (soft/hardirq context)
200901656464SThomas Gleixner  */
hrtimer_sleeper_start_expires(struct hrtimer_sleeper * sl,enum hrtimer_mode mode)201001656464SThomas Gleixner void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
201101656464SThomas Gleixner 				   enum hrtimer_mode mode)
201201656464SThomas Gleixner {
20131842f5a4SSebastian Andrzej Siewior 	/*
20141842f5a4SSebastian Andrzej Siewior 	 * Make the enqueue delivery mode check work on RT. If the sleeper
20151842f5a4SSebastian Andrzej Siewior 	 * was initialized for hard interrupt delivery, force the mode bit.
20161842f5a4SSebastian Andrzej Siewior 	 * This is a special case for hrtimer_sleepers because
2017fcea1ccfSNam Cao 	 * __hrtimer_setup_sleeper() determines the delivery mode on RT so the
20181842f5a4SSebastian Andrzej Siewior 	 * fiddling with this decision is avoided at the call sites.
20191842f5a4SSebastian Andrzej Siewior 	 */
20201842f5a4SSebastian Andrzej Siewior 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && sl->timer.is_hard)
20211842f5a4SSebastian Andrzej Siewior 		mode |= HRTIMER_MODE_HARD;
20221842f5a4SSebastian Andrzej Siewior 
202301656464SThomas Gleixner 	hrtimer_start_expires(&sl->timer, mode);
202401656464SThomas Gleixner }
202501656464SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_sleeper_start_expires);
202601656464SThomas Gleixner 
__hrtimer_setup_sleeper(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)2027fcea1ccfSNam Cao static void __hrtimer_setup_sleeper(struct hrtimer_sleeper *sl,
2028dbc1625fSSebastian Andrzej Siewior 				    clockid_t clock_id, enum hrtimer_mode mode)
20295cee9645SThomas Gleixner {
20301842f5a4SSebastian Andrzej Siewior 	/*
20314bf07f65SIngo Molnar 	 * On PREEMPT_RT enabled kernels hrtimers which are not explicitly
20321842f5a4SSebastian Andrzej Siewior 	 * marked for hard interrupt expiry mode are moved into soft
20331842f5a4SSebastian Andrzej Siewior 	 * interrupt context either for latency reasons or because the
20341842f5a4SSebastian Andrzej Siewior 	 * hrtimer callback takes regular spinlocks or invokes other
20351842f5a4SSebastian Andrzej Siewior 	 * functions which are not suitable for hard interrupt context on
20361842f5a4SSebastian Andrzej Siewior 	 * PREEMPT_RT.
20371842f5a4SSebastian Andrzej Siewior 	 *
20381842f5a4SSebastian Andrzej Siewior 	 * The hrtimer_sleeper callback is RT compatible in hard interrupt
20391842f5a4SSebastian Andrzej Siewior 	 * context, but there is a latency concern: Untrusted userspace can
20401842f5a4SSebastian Andrzej Siewior 	 * spawn many threads which arm timers for the same expiry time on
20411842f5a4SSebastian Andrzej Siewior 	 * the same CPU. That causes a latency spike due to the wakeup of
20421842f5a4SSebastian Andrzej Siewior 	 * a gazillion threads.
20431842f5a4SSebastian Andrzej Siewior 	 *
20444bf07f65SIngo Molnar 	 * OTOH, privileged real-time user space applications rely on the
20451842f5a4SSebastian Andrzej Siewior 	 * low latency of hard interrupt wakeups. If the current task is in
20461842f5a4SSebastian Andrzej Siewior 	 * a real-time scheduling class, mark the mode for hard interrupt
20471842f5a4SSebastian Andrzej Siewior 	 * expiry.
20481842f5a4SSebastian Andrzej Siewior 	 */
20491842f5a4SSebastian Andrzej Siewior 	if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
2050ae04f69dSQais Yousef 		if (rt_or_dl_task_policy(current) && !(mode & HRTIMER_MODE_SOFT))
20511842f5a4SSebastian Andrzej Siewior 			mode |= HRTIMER_MODE_HARD;
20521842f5a4SSebastian Andrzej Siewior 	}
20531842f5a4SSebastian Andrzej Siewior 
205450177a8bSNam Cao 	__hrtimer_setup(&sl->timer, hrtimer_wakeup, clock_id, mode);
2055b7449487SThomas Gleixner 	sl->task = current;
20565cee9645SThomas Gleixner }
2057dbc1625fSSebastian Andrzej Siewior 
2058dbc1625fSSebastian Andrzej Siewior /**
2059c9bd83abSNam Cao  * hrtimer_setup_sleeper_on_stack - initialize a sleeper in stack memory
2060dbc1625fSSebastian Andrzej Siewior  * @sl:		sleeper to be initialized
2061dbc1625fSSebastian Andrzej Siewior  * @clock_id:	the clock to be used
2062dbc1625fSSebastian Andrzej Siewior  * @mode:	timer mode abs/rel
2063dbc1625fSSebastian Andrzej Siewior  */
hrtimer_setup_sleeper_on_stack(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)2064c9bd83abSNam Cao void hrtimer_setup_sleeper_on_stack(struct hrtimer_sleeper *sl,
2065c9bd83abSNam Cao 				    clockid_t clock_id, enum hrtimer_mode mode)
2066dbc1625fSSebastian Andrzej Siewior {
206759c9edafSNam Cao 	debug_setup_on_stack(&sl->timer, clock_id, mode);
2068fcea1ccfSNam Cao 	__hrtimer_setup_sleeper(sl, clock_id, mode);
2069dbc1625fSSebastian Andrzej Siewior }
2070c9bd83abSNam Cao EXPORT_SYMBOL_GPL(hrtimer_setup_sleeper_on_stack);
20715cee9645SThomas Gleixner 
nanosleep_copyout(struct restart_block * restart,struct timespec64 * ts)2072c0edd7c9SDeepa Dinamani int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
2073ce41aaf4SAl Viro {
2074ce41aaf4SAl Viro 	switch(restart->nanosleep.type) {
20750fe27955SArnd Bergmann #ifdef CONFIG_COMPAT_32BIT_TIME
2076ce41aaf4SAl Viro 	case TT_COMPAT:
20779afc5eeeSArnd Bergmann 		if (put_old_timespec32(ts, restart->nanosleep.compat_rmtp))
2078ce41aaf4SAl Viro 			return -EFAULT;
2079ce41aaf4SAl Viro 		break;
2080ce41aaf4SAl Viro #endif
2081ce41aaf4SAl Viro 	case TT_NATIVE:
2082c0edd7c9SDeepa Dinamani 		if (put_timespec64(ts, restart->nanosleep.rmtp))
2083ce41aaf4SAl Viro 			return -EFAULT;
2084ce41aaf4SAl Viro 		break;
2085ce41aaf4SAl Viro 	default:
2086ce41aaf4SAl Viro 		BUG();
2087ce41aaf4SAl Viro 	}
2088ce41aaf4SAl Viro 	return -ERESTART_RESTARTBLOCK;
2089ce41aaf4SAl Viro }
2090ce41aaf4SAl Viro 
do_nanosleep(struct hrtimer_sleeper * t,enum hrtimer_mode mode)20915cee9645SThomas Gleixner static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
20925cee9645SThomas Gleixner {
2093edbeda46SAl Viro 	struct restart_block *restart;
2094edbeda46SAl Viro 
20955cee9645SThomas Gleixner 	do {
2096f5d39b02SPeter Zijlstra 		set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
209701656464SThomas Gleixner 		hrtimer_sleeper_start_expires(t, mode);
20985cee9645SThomas Gleixner 
20995cee9645SThomas Gleixner 		if (likely(t->task))
2100f5d39b02SPeter Zijlstra 			schedule();
21015cee9645SThomas Gleixner 
21025cee9645SThomas Gleixner 		hrtimer_cancel(&t->timer);
21035cee9645SThomas Gleixner 		mode = HRTIMER_MODE_ABS;
21045cee9645SThomas Gleixner 
21055cee9645SThomas Gleixner 	} while (t->task && !signal_pending(current));
21065cee9645SThomas Gleixner 
21075cee9645SThomas Gleixner 	__set_current_state(TASK_RUNNING);
21085cee9645SThomas Gleixner 
2109a7602681SAl Viro 	if (!t->task)
2110a7602681SAl Viro 		return 0;
21115cee9645SThomas Gleixner 
2112edbeda46SAl Viro 	restart = &current->restart_block;
2113edbeda46SAl Viro 	if (restart->nanosleep.type != TT_NONE) {
2114a7602681SAl Viro 		ktime_t rem = hrtimer_expires_remaining(&t->timer);
2115c0edd7c9SDeepa Dinamani 		struct timespec64 rmt;
2116edbeda46SAl Viro 
21172456e855SThomas Gleixner 		if (rem <= 0)
21185cee9645SThomas Gleixner 			return 0;
2119c0edd7c9SDeepa Dinamani 		rmt = ktime_to_timespec64(rem);
21205cee9645SThomas Gleixner 
2121ce41aaf4SAl Viro 		return nanosleep_copyout(restart, &rmt);
2122a7602681SAl Viro 	}
2123a7602681SAl Viro 	return -ERESTART_RESTARTBLOCK;
21245cee9645SThomas Gleixner }
21255cee9645SThomas Gleixner 
hrtimer_nanosleep_restart(struct restart_block * restart)2126fb923c4aSAl Viro static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
21275cee9645SThomas Gleixner {
21285cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
2129a7602681SAl Viro 	int ret;
21305cee9645SThomas Gleixner 
21318fae1411SNam Cao 	hrtimer_setup_sleeper_on_stack(&t, restart->nanosleep.clockid, HRTIMER_MODE_ABS);
21325cee9645SThomas Gleixner 	hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
2133a7602681SAl Viro 	ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
21345cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
21355cee9645SThomas Gleixner 	return ret;
21365cee9645SThomas Gleixner }
21375cee9645SThomas Gleixner 
hrtimer_nanosleep(ktime_t rqtp,const enum hrtimer_mode mode,const clockid_t clockid)2138ea2d1f7fSAndrei Vagin long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
2139ea2d1f7fSAndrei Vagin 		       const clockid_t clockid)
21405cee9645SThomas Gleixner {
2141a7602681SAl Viro 	struct restart_block *restart;
21425cee9645SThomas Gleixner 	struct hrtimer_sleeper t;
21435cee9645SThomas Gleixner 	int ret = 0;
21445cee9645SThomas Gleixner 
21458fae1411SNam Cao 	hrtimer_setup_sleeper_on_stack(&t, clockid, mode);
2146ed4fb6d7SFelix Moessbauer 	hrtimer_set_expires_range_ns(&t.timer, rqtp, current->timer_slack_ns);
2147a7602681SAl Viro 	ret = do_nanosleep(&t, mode);
2148a7602681SAl Viro 	if (ret != -ERESTART_RESTARTBLOCK)
21495cee9645SThomas Gleixner 		goto out;
21505cee9645SThomas Gleixner 
21515cee9645SThomas Gleixner 	/* Absolute timers do not update the rmtp value and restart: */
21525cee9645SThomas Gleixner 	if (mode == HRTIMER_MODE_ABS) {
21535cee9645SThomas Gleixner 		ret = -ERESTARTNOHAND;
21545cee9645SThomas Gleixner 		goto out;
21555cee9645SThomas Gleixner 	}
21565cee9645SThomas Gleixner 
2157a7602681SAl Viro 	restart = &current->restart_block;
21585cee9645SThomas Gleixner 	restart->nanosleep.clockid = t.timer.base->clockid;
21595cee9645SThomas Gleixner 	restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
21605abbe51aSOleg Nesterov 	set_restart_fn(restart, hrtimer_nanosleep_restart);
21615cee9645SThomas Gleixner out:
21625cee9645SThomas Gleixner 	destroy_hrtimer_on_stack(&t.timer);
21635cee9645SThomas Gleixner 	return ret;
21645cee9645SThomas Gleixner }
21655cee9645SThomas Gleixner 
21663ca47e95SArnd Bergmann #ifdef CONFIG_64BIT
216701909974SDeepa Dinamani 
SYSCALL_DEFINE2(nanosleep,struct __kernel_timespec __user *,rqtp,struct __kernel_timespec __user *,rmtp)216801909974SDeepa Dinamani SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp,
216901909974SDeepa Dinamani 		struct __kernel_timespec __user *, rmtp)
21705cee9645SThomas Gleixner {
2171c0edd7c9SDeepa Dinamani 	struct timespec64 tu;
21725cee9645SThomas Gleixner 
2173c0edd7c9SDeepa Dinamani 	if (get_timespec64(&tu, rqtp))
21745cee9645SThomas Gleixner 		return -EFAULT;
21755cee9645SThomas Gleixner 
2176c0edd7c9SDeepa Dinamani 	if (!timespec64_valid(&tu))
21775cee9645SThomas Gleixner 		return -EINVAL;
21785cee9645SThomas Gleixner 
21799f76d591SJann Horn 	current->restart_block.fn = do_no_restart_syscall;
2180edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
2181192a82f9SAl Viro 	current->restart_block.nanosleep.rmtp = rmtp;
2182ea2d1f7fSAndrei Vagin 	return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL,
2183ea2d1f7fSAndrei Vagin 				 CLOCK_MONOTONIC);
21845cee9645SThomas Gleixner }
21855cee9645SThomas Gleixner 
218601909974SDeepa Dinamani #endif
218701909974SDeepa Dinamani 
2188b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
2189edbeda46SAl Viro 
SYSCALL_DEFINE2(nanosleep_time32,struct old_timespec32 __user *,rqtp,struct old_timespec32 __user *,rmtp)21908dabe724SArnd Bergmann SYSCALL_DEFINE2(nanosleep_time32, struct old_timespec32 __user *, rqtp,
21919afc5eeeSArnd Bergmann 		       struct old_timespec32 __user *, rmtp)
2192edbeda46SAl Viro {
2193c0edd7c9SDeepa Dinamani 	struct timespec64 tu;
2194edbeda46SAl Viro 
21959afc5eeeSArnd Bergmann 	if (get_old_timespec32(&tu, rqtp))
2196edbeda46SAl Viro 		return -EFAULT;
2197edbeda46SAl Viro 
2198c0edd7c9SDeepa Dinamani 	if (!timespec64_valid(&tu))
2199edbeda46SAl Viro 		return -EINVAL;
2200edbeda46SAl Viro 
22019f76d591SJann Horn 	current->restart_block.fn = do_no_restart_syscall;
2202edbeda46SAl Viro 	current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
2203edbeda46SAl Viro 	current->restart_block.nanosleep.compat_rmtp = rmtp;
2204ea2d1f7fSAndrei Vagin 	return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL,
2205ea2d1f7fSAndrei Vagin 				 CLOCK_MONOTONIC);
2206edbeda46SAl Viro }
2207edbeda46SAl Viro #endif
2208edbeda46SAl Viro 
22095cee9645SThomas Gleixner /*
22105cee9645SThomas Gleixner  * Functions related to boot-time initialization:
22115cee9645SThomas Gleixner  */
hrtimers_prepare_cpu(unsigned int cpu)221227590dc1SThomas Gleixner int hrtimers_prepare_cpu(unsigned int cpu)
22135cee9645SThomas Gleixner {
22145cee9645SThomas Gleixner 	struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
22155cee9645SThomas Gleixner 	int i;
22165cee9645SThomas Gleixner 
22175cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
2218af5a06b5SAhmed S. Darwish 		struct hrtimer_clock_base *clock_b = &cpu_base->clock_base[i];
2219af5a06b5SAhmed S. Darwish 
2220af5a06b5SAhmed S. Darwish 		clock_b->cpu_base = cpu_base;
2221af5a06b5SAhmed S. Darwish 		seqcount_raw_spinlock_init(&clock_b->seq, &cpu_base->lock);
2222af5a06b5SAhmed S. Darwish 		timerqueue_init_head(&clock_b->active);
22235cee9645SThomas Gleixner 	}
22245cee9645SThomas Gleixner 
2225cddd0248SViresh Kumar 	cpu_base->cpu = cpu;
22262f8dea16SKoichiro Den 	hrtimer_cpu_base_init_expiry_lock(cpu_base);
22272f8dea16SKoichiro Den 	return 0;
22282f8dea16SKoichiro Den }
22292f8dea16SKoichiro Den 
hrtimers_cpu_starting(unsigned int cpu)22302f8dea16SKoichiro Den int hrtimers_cpu_starting(unsigned int cpu)
22312f8dea16SKoichiro Den {
22322f8dea16SKoichiro Den 	struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
22332f8dea16SKoichiro Den 
22342f8dea16SKoichiro Den 	/* Clear out any left over state from a CPU down operation */
2235303c146dSThomas Gleixner 	cpu_base->active_bases = 0;
223628bfd18bSAnna-Maria Gleixner 	cpu_base->hres_active = 0;
2237303c146dSThomas Gleixner 	cpu_base->hang_detected = 0;
2238303c146dSThomas Gleixner 	cpu_base->next_timer = NULL;
2239303c146dSThomas Gleixner 	cpu_base->softirq_next_timer = NULL;
224007a9a7eaSAnna-Maria Gleixner 	cpu_base->expires_next = KTIME_MAX;
22415da70160SAnna-Maria Gleixner 	cpu_base->softirq_expires_next = KTIME_MAX;
2242dad6a09fSFrederic Weisbecker 	cpu_base->online = 1;
224327590dc1SThomas Gleixner 	return 0;
22445cee9645SThomas Gleixner }
22455cee9645SThomas Gleixner 
22465cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
22475cee9645SThomas Gleixner 
migrate_hrtimer_list(struct hrtimer_clock_base * old_base,struct hrtimer_clock_base * new_base)22485cee9645SThomas Gleixner static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
22495cee9645SThomas Gleixner 				struct hrtimer_clock_base *new_base)
22505cee9645SThomas Gleixner {
22515cee9645SThomas Gleixner 	struct hrtimer *timer;
22525cee9645SThomas Gleixner 	struct timerqueue_node *node;
22535cee9645SThomas Gleixner 
22545cee9645SThomas Gleixner 	while ((node = timerqueue_getnext(&old_base->active))) {
22555cee9645SThomas Gleixner 		timer = container_of(node, struct hrtimer, node);
22565cee9645SThomas Gleixner 		BUG_ON(hrtimer_callback_running(timer));
22575cee9645SThomas Gleixner 		debug_deactivate(timer);
22585cee9645SThomas Gleixner 
22595cee9645SThomas Gleixner 		/*
2260c04dca02SOleg Nesterov 		 * Mark it as ENQUEUED not INACTIVE otherwise the
22615cee9645SThomas Gleixner 		 * timer could be seen as !active and just vanish away
22625cee9645SThomas Gleixner 		 * under us on another CPU
22635cee9645SThomas Gleixner 		 */
2264c04dca02SOleg Nesterov 		__remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0);
22655cee9645SThomas Gleixner 		timer->base = new_base;
22665cee9645SThomas Gleixner 		/*
22675cee9645SThomas Gleixner 		 * Enqueue the timers on the new cpu. This does not
22685cee9645SThomas Gleixner 		 * reprogram the event device in case the timer
22695cee9645SThomas Gleixner 		 * expires before the earliest on this CPU, but we run
22705cee9645SThomas Gleixner 		 * hrtimer_interrupt after we migrated everything to
22715cee9645SThomas Gleixner 		 * sort out already expired timers and reprogram the
22725cee9645SThomas Gleixner 		 * event device.
22735cee9645SThomas Gleixner 		 */
227463e2ed36SAnna-Maria Gleixner 		enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS);
22755cee9645SThomas Gleixner 	}
22765cee9645SThomas Gleixner }
22775cee9645SThomas Gleixner 
hrtimers_cpu_dying(unsigned int dying_cpu)22785c0930ccSThomas Gleixner int hrtimers_cpu_dying(unsigned int dying_cpu)
22795cee9645SThomas Gleixner {
228056c2cb10SCosta Shulyupin 	int i, ncpu = cpumask_any_and(cpu_active_mask, housekeeping_cpumask(HK_TYPE_TIMER));
22815cee9645SThomas Gleixner 	struct hrtimer_cpu_base *old_base, *new_base;
22825cee9645SThomas Gleixner 
22835c0930ccSThomas Gleixner 	old_base = this_cpu_ptr(&hrtimer_bases);
22845c0930ccSThomas Gleixner 	new_base = &per_cpu(hrtimer_bases, ncpu);
22855c0930ccSThomas Gleixner 
22865cee9645SThomas Gleixner 	/*
22875cee9645SThomas Gleixner 	 * The caller is globally serialized and nobody else
22885cee9645SThomas Gleixner 	 * takes two locks at once, deadlock is not possible.
22895cee9645SThomas Gleixner 	 */
22905c0930ccSThomas Gleixner 	raw_spin_lock(&old_base->lock);
22915c0930ccSThomas Gleixner 	raw_spin_lock_nested(&new_base->lock, SINGLE_DEPTH_NESTING);
22925cee9645SThomas Gleixner 
22935cee9645SThomas Gleixner 	for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
22945cee9645SThomas Gleixner 		migrate_hrtimer_list(&old_base->clock_base[i],
22955cee9645SThomas Gleixner 				     &new_base->clock_base[i]);
22965cee9645SThomas Gleixner 	}
22975cee9645SThomas Gleixner 
22985da70160SAnna-Maria Gleixner 	/*
22995da70160SAnna-Maria Gleixner 	 * The migration might have changed the first expiring softirq
23005da70160SAnna-Maria Gleixner 	 * timer on this CPU. Update it.
23015da70160SAnna-Maria Gleixner 	 */
23025c0930ccSThomas Gleixner 	__hrtimer_get_next_event(new_base, HRTIMER_ACTIVE_SOFT);
23035c0930ccSThomas Gleixner 	/* Tell the other CPU to retrigger the next event */
23045c0930ccSThomas Gleixner 	smp_call_function_single(ncpu, retrigger_next_event, NULL, 0);
23055da70160SAnna-Maria Gleixner 
23065cee9645SThomas Gleixner 	raw_spin_unlock(&new_base->lock);
2307dad6a09fSFrederic Weisbecker 	old_base->online = 0;
23085c0930ccSThomas Gleixner 	raw_spin_unlock(&old_base->lock);
23095cee9645SThomas Gleixner 
231027590dc1SThomas Gleixner 	return 0;
23115cee9645SThomas Gleixner }
23125cee9645SThomas Gleixner 
23135cee9645SThomas Gleixner #endif /* CONFIG_HOTPLUG_CPU */
23145cee9645SThomas Gleixner 
hrtimers_init(void)23155cee9645SThomas Gleixner void __init hrtimers_init(void)
23165cee9645SThomas Gleixner {
231727590dc1SThomas Gleixner 	hrtimers_prepare_cpu(smp_processor_id());
23182f8dea16SKoichiro Den 	hrtimers_cpu_starting(smp_processor_id());
23195da70160SAnna-Maria Gleixner 	open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
23205cee9645SThomas Gleixner }
2321