xref: /linux-6.15/kernel/time/timer.c (revision dce3e8fd)
135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
25cee9645SThomas Gleixner /*
35cee9645SThomas Gleixner  *  Kernel internal timers
45cee9645SThomas Gleixner  *
55cee9645SThomas Gleixner  *  Copyright (C) 1991, 1992  Linus Torvalds
65cee9645SThomas Gleixner  *
75cee9645SThomas Gleixner  *  1997-01-28  Modified by Finn Arne Gangstad to make timers scale better.
85cee9645SThomas Gleixner  *
95cee9645SThomas Gleixner  *  1997-09-10  Updated NTP code according to technical memorandum Jan '96
105cee9645SThomas Gleixner  *              "A Kernel Model for Precision Timekeeping" by Dave Mills
115cee9645SThomas Gleixner  *  1998-12-24  Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
125cee9645SThomas Gleixner  *              serialize accesses to xtime/lost_ticks).
135cee9645SThomas Gleixner  *                              Copyright (C) 1998  Andrea Arcangeli
145cee9645SThomas Gleixner  *  1999-03-10  Improved NTP compatibility by Ulrich Windl
155cee9645SThomas Gleixner  *  2002-05-31	Move sys_sysinfo here and make its locking sane, Robert Love
165cee9645SThomas Gleixner  *  2000-10-05  Implemented scalable SMP per-CPU timer handling.
175cee9645SThomas Gleixner  *                              Copyright (C) 2000, 2001, 2002  Ingo Molnar
185cee9645SThomas Gleixner  *              Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
195cee9645SThomas Gleixner  */
205cee9645SThomas Gleixner 
215cee9645SThomas Gleixner #include <linux/kernel_stat.h>
225cee9645SThomas Gleixner #include <linux/export.h>
235cee9645SThomas Gleixner #include <linux/interrupt.h>
245cee9645SThomas Gleixner #include <linux/percpu.h>
255cee9645SThomas Gleixner #include <linux/init.h>
265cee9645SThomas Gleixner #include <linux/mm.h>
275cee9645SThomas Gleixner #include <linux/swap.h>
285cee9645SThomas Gleixner #include <linux/pid_namespace.h>
295cee9645SThomas Gleixner #include <linux/notifier.h>
305cee9645SThomas Gleixner #include <linux/thread_info.h>
315cee9645SThomas Gleixner #include <linux/time.h>
325cee9645SThomas Gleixner #include <linux/jiffies.h>
335cee9645SThomas Gleixner #include <linux/posix-timers.h>
345cee9645SThomas Gleixner #include <linux/cpu.h>
355cee9645SThomas Gleixner #include <linux/syscalls.h>
365cee9645SThomas Gleixner #include <linux/delay.h>
375cee9645SThomas Gleixner #include <linux/tick.h>
385cee9645SThomas Gleixner #include <linux/kallsyms.h>
395cee9645SThomas Gleixner #include <linux/irq_work.h>
40174cd4b1SIngo Molnar #include <linux/sched/signal.h>
415cee9645SThomas Gleixner #include <linux/sched/sysctl.h>
42370c9135SIngo Molnar #include <linux/sched/nohz.h>
43b17b0153SIngo Molnar #include <linux/sched/debug.h>
445cee9645SThomas Gleixner #include <linux/slab.h>
455cee9645SThomas Gleixner #include <linux/compat.h>
465cee9645SThomas Gleixner 
477c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
485cee9645SThomas Gleixner #include <asm/unistd.h>
495cee9645SThomas Gleixner #include <asm/div64.h>
505cee9645SThomas Gleixner #include <asm/timex.h>
515cee9645SThomas Gleixner #include <asm/io.h>
525cee9645SThomas Gleixner 
53c1ad348bSThomas Gleixner #include "tick-internal.h"
54c1ad348bSThomas Gleixner 
555cee9645SThomas Gleixner #define CREATE_TRACE_POINTS
565cee9645SThomas Gleixner #include <trace/events/timer.h>
575cee9645SThomas Gleixner 
585cee9645SThomas Gleixner __visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
595cee9645SThomas Gleixner 
605cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64);
615cee9645SThomas Gleixner 
625cee9645SThomas Gleixner /*
63500462a9SThomas Gleixner  * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
64500462a9SThomas Gleixner  * LVL_SIZE buckets. Each level is driven by its own clock and therefor each
65500462a9SThomas Gleixner  * level has a different granularity.
66500462a9SThomas Gleixner  *
67500462a9SThomas Gleixner  * The level granularity is:		LVL_CLK_DIV ^ lvl
68500462a9SThomas Gleixner  * The level clock frequency is:	HZ / (LVL_CLK_DIV ^ level)
69500462a9SThomas Gleixner  *
70500462a9SThomas Gleixner  * The array level of a newly armed timer depends on the relative expiry
71500462a9SThomas Gleixner  * time. The farther the expiry time is away the higher the array level and
72500462a9SThomas Gleixner  * therefor the granularity becomes.
73500462a9SThomas Gleixner  *
74500462a9SThomas Gleixner  * Contrary to the original timer wheel implementation, which aims for 'exact'
75500462a9SThomas Gleixner  * expiry of the timers, this implementation removes the need for recascading
76500462a9SThomas Gleixner  * the timers into the lower array levels. The previous 'classic' timer wheel
77500462a9SThomas Gleixner  * implementation of the kernel already violated the 'exact' expiry by adding
78500462a9SThomas Gleixner  * slack to the expiry time to provide batched expiration. The granularity
79500462a9SThomas Gleixner  * levels provide implicit batching.
80500462a9SThomas Gleixner  *
81500462a9SThomas Gleixner  * This is an optimization of the original timer wheel implementation for the
82500462a9SThomas Gleixner  * majority of the timer wheel use cases: timeouts. The vast majority of
83500462a9SThomas Gleixner  * timeout timers (networking, disk I/O ...) are canceled before expiry. If
84500462a9SThomas Gleixner  * the timeout expires it indicates that normal operation is disturbed, so it
85500462a9SThomas Gleixner  * does not matter much whether the timeout comes with a slight delay.
86500462a9SThomas Gleixner  *
87500462a9SThomas Gleixner  * The only exception to this are networking timers with a small expiry
88500462a9SThomas Gleixner  * time. They rely on the granularity. Those fit into the first wheel level,
89500462a9SThomas Gleixner  * which has HZ granularity.
90500462a9SThomas Gleixner  *
91500462a9SThomas Gleixner  * We don't have cascading anymore. timers with a expiry time above the
92500462a9SThomas Gleixner  * capacity of the last wheel level are force expired at the maximum timeout
93500462a9SThomas Gleixner  * value of the last wheel level. From data sampling we know that the maximum
94500462a9SThomas Gleixner  * value observed is 5 days (network connection tracking), so this should not
95500462a9SThomas Gleixner  * be an issue.
96500462a9SThomas Gleixner  *
97500462a9SThomas Gleixner  * The currently chosen array constants values are a good compromise between
98500462a9SThomas Gleixner  * array size and granularity.
99500462a9SThomas Gleixner  *
100500462a9SThomas Gleixner  * This results in the following granularity and range levels:
101500462a9SThomas Gleixner  *
102500462a9SThomas Gleixner  * HZ 1000 steps
103500462a9SThomas Gleixner  * Level Offset  Granularity            Range
104500462a9SThomas Gleixner  *  0      0         1 ms                0 ms -         63 ms
105500462a9SThomas Gleixner  *  1     64         8 ms               64 ms -        511 ms
106500462a9SThomas Gleixner  *  2    128        64 ms              512 ms -       4095 ms (512ms - ~4s)
107500462a9SThomas Gleixner  *  3    192       512 ms             4096 ms -      32767 ms (~4s - ~32s)
108500462a9SThomas Gleixner  *  4    256      4096 ms (~4s)      32768 ms -     262143 ms (~32s - ~4m)
109500462a9SThomas Gleixner  *  5    320     32768 ms (~32s)    262144 ms -    2097151 ms (~4m - ~34m)
110500462a9SThomas Gleixner  *  6    384    262144 ms (~4m)    2097152 ms -   16777215 ms (~34m - ~4h)
111500462a9SThomas Gleixner  *  7    448   2097152 ms (~34m)  16777216 ms -  134217727 ms (~4h - ~1d)
112500462a9SThomas Gleixner  *  8    512  16777216 ms (~4h)  134217728 ms - 1073741822 ms (~1d - ~12d)
113500462a9SThomas Gleixner  *
114500462a9SThomas Gleixner  * HZ  300
115500462a9SThomas Gleixner  * Level Offset  Granularity            Range
116500462a9SThomas Gleixner  *  0	   0         3 ms                0 ms -        210 ms
117500462a9SThomas Gleixner  *  1	  64        26 ms              213 ms -       1703 ms (213ms - ~1s)
118500462a9SThomas Gleixner  *  2	 128       213 ms             1706 ms -      13650 ms (~1s - ~13s)
119500462a9SThomas Gleixner  *  3	 192      1706 ms (~1s)      13653 ms -     109223 ms (~13s - ~1m)
120500462a9SThomas Gleixner  *  4	 256     13653 ms (~13s)    109226 ms -     873810 ms (~1m - ~14m)
121500462a9SThomas Gleixner  *  5	 320    109226 ms (~1m)     873813 ms -    6990503 ms (~14m - ~1h)
122500462a9SThomas Gleixner  *  6	 384    873813 ms (~14m)   6990506 ms -   55924050 ms (~1h - ~15h)
123500462a9SThomas Gleixner  *  7	 448   6990506 ms (~1h)   55924053 ms -  447392423 ms (~15h - ~5d)
124500462a9SThomas Gleixner  *  8    512  55924053 ms (~15h) 447392426 ms - 3579139406 ms (~5d - ~41d)
125500462a9SThomas Gleixner  *
126500462a9SThomas Gleixner  * HZ  250
127500462a9SThomas Gleixner  * Level Offset  Granularity            Range
128500462a9SThomas Gleixner  *  0	   0         4 ms                0 ms -        255 ms
129500462a9SThomas Gleixner  *  1	  64        32 ms              256 ms -       2047 ms (256ms - ~2s)
130500462a9SThomas Gleixner  *  2	 128       256 ms             2048 ms -      16383 ms (~2s - ~16s)
131500462a9SThomas Gleixner  *  3	 192      2048 ms (~2s)      16384 ms -     131071 ms (~16s - ~2m)
132500462a9SThomas Gleixner  *  4	 256     16384 ms (~16s)    131072 ms -    1048575 ms (~2m - ~17m)
133500462a9SThomas Gleixner  *  5	 320    131072 ms (~2m)    1048576 ms -    8388607 ms (~17m - ~2h)
134500462a9SThomas Gleixner  *  6	 384   1048576 ms (~17m)   8388608 ms -   67108863 ms (~2h - ~18h)
135500462a9SThomas Gleixner  *  7	 448   8388608 ms (~2h)   67108864 ms -  536870911 ms (~18h - ~6d)
136500462a9SThomas Gleixner  *  8    512  67108864 ms (~18h) 536870912 ms - 4294967288 ms (~6d - ~49d)
137500462a9SThomas Gleixner  *
138500462a9SThomas Gleixner  * HZ  100
139500462a9SThomas Gleixner  * Level Offset  Granularity            Range
140500462a9SThomas Gleixner  *  0	   0         10 ms               0 ms -        630 ms
141500462a9SThomas Gleixner  *  1	  64         80 ms             640 ms -       5110 ms (640ms - ~5s)
142500462a9SThomas Gleixner  *  2	 128        640 ms            5120 ms -      40950 ms (~5s - ~40s)
143500462a9SThomas Gleixner  *  3	 192       5120 ms (~5s)     40960 ms -     327670 ms (~40s - ~5m)
144500462a9SThomas Gleixner  *  4	 256      40960 ms (~40s)   327680 ms -    2621430 ms (~5m - ~43m)
145500462a9SThomas Gleixner  *  5	 320     327680 ms (~5m)   2621440 ms -   20971510 ms (~43m - ~5h)
146500462a9SThomas Gleixner  *  6	 384    2621440 ms (~43m) 20971520 ms -  167772150 ms (~5h - ~1d)
147500462a9SThomas Gleixner  *  7	 448   20971520 ms (~5h) 167772160 ms - 1342177270 ms (~1d - ~15d)
1485cee9645SThomas Gleixner  */
1495cee9645SThomas Gleixner 
150500462a9SThomas Gleixner /* Clock divisor for the next level */
151500462a9SThomas Gleixner #define LVL_CLK_SHIFT	3
152500462a9SThomas Gleixner #define LVL_CLK_DIV	(1UL << LVL_CLK_SHIFT)
153500462a9SThomas Gleixner #define LVL_CLK_MASK	(LVL_CLK_DIV - 1)
154500462a9SThomas Gleixner #define LVL_SHIFT(n)	((n) * LVL_CLK_SHIFT)
155500462a9SThomas Gleixner #define LVL_GRAN(n)	(1UL << LVL_SHIFT(n))
1565cee9645SThomas Gleixner 
157500462a9SThomas Gleixner /*
158500462a9SThomas Gleixner  * The time start value for each level to select the bucket at enqueue
159500462a9SThomas Gleixner  * time.
160500462a9SThomas Gleixner  */
161500462a9SThomas Gleixner #define LVL_START(n)	((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
1625cee9645SThomas Gleixner 
163500462a9SThomas Gleixner /* Size of each clock level */
164500462a9SThomas Gleixner #define LVL_BITS	6
165500462a9SThomas Gleixner #define LVL_SIZE	(1UL << LVL_BITS)
166500462a9SThomas Gleixner #define LVL_MASK	(LVL_SIZE - 1)
167500462a9SThomas Gleixner #define LVL_OFFS(n)	((n) * LVL_SIZE)
168500462a9SThomas Gleixner 
169500462a9SThomas Gleixner /* Level depth */
170500462a9SThomas Gleixner #if HZ > 100
171500462a9SThomas Gleixner # define LVL_DEPTH	9
172500462a9SThomas Gleixner # else
173500462a9SThomas Gleixner # define LVL_DEPTH	8
174500462a9SThomas Gleixner #endif
175500462a9SThomas Gleixner 
176500462a9SThomas Gleixner /* The cutoff (max. capacity of the wheel) */
177500462a9SThomas Gleixner #define WHEEL_TIMEOUT_CUTOFF	(LVL_START(LVL_DEPTH))
178500462a9SThomas Gleixner #define WHEEL_TIMEOUT_MAX	(WHEEL_TIMEOUT_CUTOFF - LVL_GRAN(LVL_DEPTH - 1))
179500462a9SThomas Gleixner 
180500462a9SThomas Gleixner /*
181500462a9SThomas Gleixner  * The resulting wheel size. If NOHZ is configured we allocate two
182500462a9SThomas Gleixner  * wheels so we have a separate storage for the deferrable timers.
183500462a9SThomas Gleixner  */
184500462a9SThomas Gleixner #define WHEEL_SIZE	(LVL_SIZE * LVL_DEPTH)
185500462a9SThomas Gleixner 
186500462a9SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
187500462a9SThomas Gleixner # define NR_BASES	2
188500462a9SThomas Gleixner # define BASE_STD	0
189500462a9SThomas Gleixner # define BASE_DEF	1
190500462a9SThomas Gleixner #else
191500462a9SThomas Gleixner # define NR_BASES	1
192500462a9SThomas Gleixner # define BASE_STD	0
193500462a9SThomas Gleixner # define BASE_DEF	0
194500462a9SThomas Gleixner #endif
1955cee9645SThomas Gleixner 
196494af3edSThomas Gleixner struct timer_base {
1972287d866SSebastian Andrzej Siewior 	raw_spinlock_t		lock;
1985cee9645SThomas Gleixner 	struct timer_list	*running_timer;
199030dcdd1SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT
200030dcdd1SAnna-Maria Gleixner 	spinlock_t		expiry_lock;
201030dcdd1SAnna-Maria Gleixner 	atomic_t		timer_waiters;
202030dcdd1SAnna-Maria Gleixner #endif
203494af3edSThomas Gleixner 	unsigned long		clk;
204a683f390SThomas Gleixner 	unsigned long		next_expiry;
205500462a9SThomas Gleixner 	unsigned int		cpu;
206a683f390SThomas Gleixner 	bool			is_idle;
2072fe59f50SNicholas Piggin 	bool			must_forward_clk;
208500462a9SThomas Gleixner 	DECLARE_BITMAP(pending_map, WHEEL_SIZE);
209500462a9SThomas Gleixner 	struct hlist_head	vectors[WHEEL_SIZE];
2105cee9645SThomas Gleixner } ____cacheline_aligned;
2115cee9645SThomas Gleixner 
212500462a9SThomas Gleixner static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
2135cee9645SThomas Gleixner 
214ae67badaSThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
215ae67badaSThomas Gleixner 
21614c80341SAnna-Maria Gleixner static DEFINE_STATIC_KEY_FALSE(timers_nohz_active);
217ae67badaSThomas Gleixner static DEFINE_MUTEX(timer_keys_mutex);
218ae67badaSThomas Gleixner 
219ae67badaSThomas Gleixner static void timer_update_keys(struct work_struct *work);
220ae67badaSThomas Gleixner static DECLARE_WORK(timer_update_work, timer_update_keys);
221ae67badaSThomas Gleixner 
222ae67badaSThomas Gleixner #ifdef CONFIG_SMP
223bc7a34b8SThomas Gleixner unsigned int sysctl_timer_migration = 1;
224bc7a34b8SThomas Gleixner 
225ae67badaSThomas Gleixner DEFINE_STATIC_KEY_FALSE(timers_migration_enabled);
226ae67badaSThomas Gleixner 
227ae67badaSThomas Gleixner static void timers_update_migration(void)
228bc7a34b8SThomas Gleixner {
229ae67badaSThomas Gleixner 	if (sysctl_timer_migration && tick_nohz_active)
230ae67badaSThomas Gleixner 		static_branch_enable(&timers_migration_enabled);
231ae67badaSThomas Gleixner 	else
232ae67badaSThomas Gleixner 		static_branch_disable(&timers_migration_enabled);
233bc7a34b8SThomas Gleixner }
234ae67badaSThomas Gleixner #else
235ae67badaSThomas Gleixner static inline void timers_update_migration(void) { }
236ae67badaSThomas Gleixner #endif /* !CONFIG_SMP */
237ae67badaSThomas Gleixner 
238ae67badaSThomas Gleixner static void timer_update_keys(struct work_struct *work)
239ae67badaSThomas Gleixner {
240ae67badaSThomas Gleixner 	mutex_lock(&timer_keys_mutex);
241ae67badaSThomas Gleixner 	timers_update_migration();
242ae67badaSThomas Gleixner 	static_branch_enable(&timers_nohz_active);
243ae67badaSThomas Gleixner 	mutex_unlock(&timer_keys_mutex);
244ae67badaSThomas Gleixner }
245ae67badaSThomas Gleixner 
246ae67badaSThomas Gleixner void timers_update_nohz(void)
247ae67badaSThomas Gleixner {
248ae67badaSThomas Gleixner 	schedule_work(&timer_update_work);
249bc7a34b8SThomas Gleixner }
250bc7a34b8SThomas Gleixner 
251bc7a34b8SThomas Gleixner int timer_migration_handler(struct ctl_table *table, int write,
252bc7a34b8SThomas Gleixner 			    void __user *buffer, size_t *lenp,
253bc7a34b8SThomas Gleixner 			    loff_t *ppos)
254bc7a34b8SThomas Gleixner {
255bc7a34b8SThomas Gleixner 	int ret;
256bc7a34b8SThomas Gleixner 
257ae67badaSThomas Gleixner 	mutex_lock(&timer_keys_mutex);
258b94bf594SMyungho Jung 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
259bc7a34b8SThomas Gleixner 	if (!ret && write)
260ae67badaSThomas Gleixner 		timers_update_migration();
261ae67badaSThomas Gleixner 	mutex_unlock(&timer_keys_mutex);
262bc7a34b8SThomas Gleixner 	return ret;
263bc7a34b8SThomas Gleixner }
26414c80341SAnna-Maria Gleixner 
26514c80341SAnna-Maria Gleixner static inline bool is_timers_nohz_active(void)
26614c80341SAnna-Maria Gleixner {
26714c80341SAnna-Maria Gleixner 	return static_branch_unlikely(&timers_nohz_active);
26814c80341SAnna-Maria Gleixner }
26914c80341SAnna-Maria Gleixner #else
27014c80341SAnna-Maria Gleixner static inline bool is_timers_nohz_active(void) { return false; }
271ae67badaSThomas Gleixner #endif /* NO_HZ_COMMON */
272bc7a34b8SThomas Gleixner 
2735cee9645SThomas Gleixner static unsigned long round_jiffies_common(unsigned long j, int cpu,
2745cee9645SThomas Gleixner 		bool force_up)
2755cee9645SThomas Gleixner {
2765cee9645SThomas Gleixner 	int rem;
2775cee9645SThomas Gleixner 	unsigned long original = j;
2785cee9645SThomas Gleixner 
2795cee9645SThomas Gleixner 	/*
2805cee9645SThomas Gleixner 	 * We don't want all cpus firing their timers at once hitting the
2815cee9645SThomas Gleixner 	 * same lock or cachelines, so we skew each extra cpu with an extra
2825cee9645SThomas Gleixner 	 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
2835cee9645SThomas Gleixner 	 * already did this.
2845cee9645SThomas Gleixner 	 * The skew is done by adding 3*cpunr, then round, then subtract this
2855cee9645SThomas Gleixner 	 * extra offset again.
2865cee9645SThomas Gleixner 	 */
2875cee9645SThomas Gleixner 	j += cpu * 3;
2885cee9645SThomas Gleixner 
2895cee9645SThomas Gleixner 	rem = j % HZ;
2905cee9645SThomas Gleixner 
2915cee9645SThomas Gleixner 	/*
2925cee9645SThomas Gleixner 	 * If the target jiffie is just after a whole second (which can happen
2935cee9645SThomas Gleixner 	 * due to delays of the timer irq, long irq off times etc etc) then
2945cee9645SThomas Gleixner 	 * we should round down to the whole second, not up. Use 1/4th second
2955cee9645SThomas Gleixner 	 * as cutoff for this rounding as an extreme upper bound for this.
2965cee9645SThomas Gleixner 	 * But never round down if @force_up is set.
2975cee9645SThomas Gleixner 	 */
2985cee9645SThomas Gleixner 	if (rem < HZ/4 && !force_up) /* round down */
2995cee9645SThomas Gleixner 		j = j - rem;
3005cee9645SThomas Gleixner 	else /* round up */
3015cee9645SThomas Gleixner 		j = j - rem + HZ;
3025cee9645SThomas Gleixner 
3035cee9645SThomas Gleixner 	/* now that we have rounded, subtract the extra skew again */
3045cee9645SThomas Gleixner 	j -= cpu * 3;
3055cee9645SThomas Gleixner 
3065cee9645SThomas Gleixner 	/*
3075cee9645SThomas Gleixner 	 * Make sure j is still in the future. Otherwise return the
3085cee9645SThomas Gleixner 	 * unmodified value.
3095cee9645SThomas Gleixner 	 */
3105cee9645SThomas Gleixner 	return time_is_after_jiffies(j) ? j : original;
3115cee9645SThomas Gleixner }
3125cee9645SThomas Gleixner 
3135cee9645SThomas Gleixner /**
3145cee9645SThomas Gleixner  * __round_jiffies - function to round jiffies to a full second
3155cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
3165cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
3175cee9645SThomas Gleixner  *
3185cee9645SThomas Gleixner  * __round_jiffies() rounds an absolute time in the future (in jiffies)
3195cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3205cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3215cee9645SThomas Gleixner  * they fire approximately every X seconds.
3225cee9645SThomas Gleixner  *
3235cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3245cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3255cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3265cee9645SThomas Gleixner  *
3275cee9645SThomas Gleixner  * The exact rounding is skewed for each processor to avoid all
3285cee9645SThomas Gleixner  * processors firing at the exact same time, which could lead
3295cee9645SThomas Gleixner  * to lock contention or spurious cache line bouncing.
3305cee9645SThomas Gleixner  *
3315cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3325cee9645SThomas Gleixner  */
3335cee9645SThomas Gleixner unsigned long __round_jiffies(unsigned long j, int cpu)
3345cee9645SThomas Gleixner {
3355cee9645SThomas Gleixner 	return round_jiffies_common(j, cpu, false);
3365cee9645SThomas Gleixner }
3375cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies);
3385cee9645SThomas Gleixner 
3395cee9645SThomas Gleixner /**
3405cee9645SThomas Gleixner  * __round_jiffies_relative - function to round jiffies to a full second
3415cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
3425cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
3435cee9645SThomas Gleixner  *
3445cee9645SThomas Gleixner  * __round_jiffies_relative() rounds a time delta  in the future (in jiffies)
3455cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3465cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3475cee9645SThomas Gleixner  * they fire approximately every X seconds.
3485cee9645SThomas Gleixner  *
3495cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3505cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3515cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3525cee9645SThomas Gleixner  *
3535cee9645SThomas Gleixner  * The exact rounding is skewed for each processor to avoid all
3545cee9645SThomas Gleixner  * processors firing at the exact same time, which could lead
3555cee9645SThomas Gleixner  * to lock contention or spurious cache line bouncing.
3565cee9645SThomas Gleixner  *
3575cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3585cee9645SThomas Gleixner  */
3595cee9645SThomas Gleixner unsigned long __round_jiffies_relative(unsigned long j, int cpu)
3605cee9645SThomas Gleixner {
3615cee9645SThomas Gleixner 	unsigned long j0 = jiffies;
3625cee9645SThomas Gleixner 
3635cee9645SThomas Gleixner 	/* Use j0 because jiffies might change while we run */
3645cee9645SThomas Gleixner 	return round_jiffies_common(j + j0, cpu, false) - j0;
3655cee9645SThomas Gleixner }
3665cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_relative);
3675cee9645SThomas Gleixner 
3685cee9645SThomas Gleixner /**
3695cee9645SThomas Gleixner  * round_jiffies - function to round jiffies to a full second
3705cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
3715cee9645SThomas Gleixner  *
3725cee9645SThomas Gleixner  * round_jiffies() rounds an absolute time in the future (in jiffies)
3735cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3745cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3755cee9645SThomas Gleixner  * they fire approximately every X seconds.
3765cee9645SThomas Gleixner  *
3775cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3785cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3795cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3805cee9645SThomas Gleixner  *
3815cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3825cee9645SThomas Gleixner  */
3835cee9645SThomas Gleixner unsigned long round_jiffies(unsigned long j)
3845cee9645SThomas Gleixner {
3855cee9645SThomas Gleixner 	return round_jiffies_common(j, raw_smp_processor_id(), false);
3865cee9645SThomas Gleixner }
3875cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies);
3885cee9645SThomas Gleixner 
3895cee9645SThomas Gleixner /**
3905cee9645SThomas Gleixner  * round_jiffies_relative - function to round jiffies to a full second
3915cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
3925cee9645SThomas Gleixner  *
3935cee9645SThomas Gleixner  * round_jiffies_relative() rounds a time delta  in the future (in jiffies)
3945cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3955cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3965cee9645SThomas Gleixner  * they fire approximately every X seconds.
3975cee9645SThomas Gleixner  *
3985cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3995cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
4005cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
4015cee9645SThomas Gleixner  *
4025cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
4035cee9645SThomas Gleixner  */
4045cee9645SThomas Gleixner unsigned long round_jiffies_relative(unsigned long j)
4055cee9645SThomas Gleixner {
4065cee9645SThomas Gleixner 	return __round_jiffies_relative(j, raw_smp_processor_id());
4075cee9645SThomas Gleixner }
4085cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_relative);
4095cee9645SThomas Gleixner 
4105cee9645SThomas Gleixner /**
4115cee9645SThomas Gleixner  * __round_jiffies_up - function to round jiffies up to a full second
4125cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
4135cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
4145cee9645SThomas Gleixner  *
4155cee9645SThomas Gleixner  * This is the same as __round_jiffies() except that it will never
4165cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4175cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4185cee9645SThomas Gleixner  * early.
4195cee9645SThomas Gleixner  */
4205cee9645SThomas Gleixner unsigned long __round_jiffies_up(unsigned long j, int cpu)
4215cee9645SThomas Gleixner {
4225cee9645SThomas Gleixner 	return round_jiffies_common(j, cpu, true);
4235cee9645SThomas Gleixner }
4245cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up);
4255cee9645SThomas Gleixner 
4265cee9645SThomas Gleixner /**
4275cee9645SThomas Gleixner  * __round_jiffies_up_relative - function to round jiffies up to a full second
4285cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
4295cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
4305cee9645SThomas Gleixner  *
4315cee9645SThomas Gleixner  * This is the same as __round_jiffies_relative() except that it will never
4325cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4335cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4345cee9645SThomas Gleixner  * early.
4355cee9645SThomas Gleixner  */
4365cee9645SThomas Gleixner unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
4375cee9645SThomas Gleixner {
4385cee9645SThomas Gleixner 	unsigned long j0 = jiffies;
4395cee9645SThomas Gleixner 
4405cee9645SThomas Gleixner 	/* Use j0 because jiffies might change while we run */
4415cee9645SThomas Gleixner 	return round_jiffies_common(j + j0, cpu, true) - j0;
4425cee9645SThomas Gleixner }
4435cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
4445cee9645SThomas Gleixner 
4455cee9645SThomas Gleixner /**
4465cee9645SThomas Gleixner  * round_jiffies_up - function to round jiffies up to a full second
4475cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
4485cee9645SThomas Gleixner  *
4495cee9645SThomas Gleixner  * This is the same as round_jiffies() except that it will never
4505cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4515cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4525cee9645SThomas Gleixner  * early.
4535cee9645SThomas Gleixner  */
4545cee9645SThomas Gleixner unsigned long round_jiffies_up(unsigned long j)
4555cee9645SThomas Gleixner {
4565cee9645SThomas Gleixner 	return round_jiffies_common(j, raw_smp_processor_id(), true);
4575cee9645SThomas Gleixner }
4585cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up);
4595cee9645SThomas Gleixner 
4605cee9645SThomas Gleixner /**
4615cee9645SThomas Gleixner  * round_jiffies_up_relative - function to round jiffies up to a full second
4625cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
4635cee9645SThomas Gleixner  *
4645cee9645SThomas Gleixner  * This is the same as round_jiffies_relative() except that it will never
4655cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4665cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4675cee9645SThomas Gleixner  * early.
4685cee9645SThomas Gleixner  */
4695cee9645SThomas Gleixner unsigned long round_jiffies_up_relative(unsigned long j)
4705cee9645SThomas Gleixner {
4715cee9645SThomas Gleixner 	return __round_jiffies_up_relative(j, raw_smp_processor_id());
4725cee9645SThomas Gleixner }
4735cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
4745cee9645SThomas Gleixner 
4755cee9645SThomas Gleixner 
476500462a9SThomas Gleixner static inline unsigned int timer_get_idx(struct timer_list *timer)
4775cee9645SThomas Gleixner {
478500462a9SThomas Gleixner 	return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT;
4795cee9645SThomas Gleixner }
480500462a9SThomas Gleixner 
481500462a9SThomas Gleixner static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
482500462a9SThomas Gleixner {
483500462a9SThomas Gleixner 	timer->flags = (timer->flags & ~TIMER_ARRAYMASK) |
484500462a9SThomas Gleixner 			idx << TIMER_ARRAYSHIFT;
485500462a9SThomas Gleixner }
486500462a9SThomas Gleixner 
487500462a9SThomas Gleixner /*
488500462a9SThomas Gleixner  * Helper function to calculate the array index for a given expiry
489500462a9SThomas Gleixner  * time.
490500462a9SThomas Gleixner  */
491500462a9SThomas Gleixner static inline unsigned calc_index(unsigned expires, unsigned lvl)
492500462a9SThomas Gleixner {
493500462a9SThomas Gleixner 	expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
494500462a9SThomas Gleixner 	return LVL_OFFS(lvl) + (expires & LVL_MASK);
495500462a9SThomas Gleixner }
496500462a9SThomas Gleixner 
497ffdf0477SAnna-Maria Gleixner static int calc_wheel_index(unsigned long expires, unsigned long clk)
4985cee9645SThomas Gleixner {
499ffdf0477SAnna-Maria Gleixner 	unsigned long delta = expires - clk;
500500462a9SThomas Gleixner 	unsigned int idx;
5015cee9645SThomas Gleixner 
502500462a9SThomas Gleixner 	if (delta < LVL_START(1)) {
503500462a9SThomas Gleixner 		idx = calc_index(expires, 0);
504500462a9SThomas Gleixner 	} else if (delta < LVL_START(2)) {
505500462a9SThomas Gleixner 		idx = calc_index(expires, 1);
506500462a9SThomas Gleixner 	} else if (delta < LVL_START(3)) {
507500462a9SThomas Gleixner 		idx = calc_index(expires, 2);
508500462a9SThomas Gleixner 	} else if (delta < LVL_START(4)) {
509500462a9SThomas Gleixner 		idx = calc_index(expires, 3);
510500462a9SThomas Gleixner 	} else if (delta < LVL_START(5)) {
511500462a9SThomas Gleixner 		idx = calc_index(expires, 4);
512500462a9SThomas Gleixner 	} else if (delta < LVL_START(6)) {
513500462a9SThomas Gleixner 		idx = calc_index(expires, 5);
514500462a9SThomas Gleixner 	} else if (delta < LVL_START(7)) {
515500462a9SThomas Gleixner 		idx = calc_index(expires, 6);
516500462a9SThomas Gleixner 	} else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
517500462a9SThomas Gleixner 		idx = calc_index(expires, 7);
518500462a9SThomas Gleixner 	} else if ((long) delta < 0) {
519ffdf0477SAnna-Maria Gleixner 		idx = clk & LVL_MASK;
5205cee9645SThomas Gleixner 	} else {
521500462a9SThomas Gleixner 		/*
522500462a9SThomas Gleixner 		 * Force expire obscene large timeouts to expire at the
523500462a9SThomas Gleixner 		 * capacity limit of the wheel.
5245cee9645SThomas Gleixner 		 */
525500462a9SThomas Gleixner 		if (expires >= WHEEL_TIMEOUT_CUTOFF)
526500462a9SThomas Gleixner 			expires = WHEEL_TIMEOUT_MAX;
5271bd04bf6SThomas Gleixner 
528500462a9SThomas Gleixner 		idx = calc_index(expires, LVL_DEPTH - 1);
529500462a9SThomas Gleixner 	}
530ffdf0477SAnna-Maria Gleixner 	return idx;
531ffdf0477SAnna-Maria Gleixner }
532ffdf0477SAnna-Maria Gleixner 
533500462a9SThomas Gleixner /*
534ffdf0477SAnna-Maria Gleixner  * Enqueue the timer into the hash bucket, mark it pending in
535500462a9SThomas Gleixner  * the bitmap and store the index in the timer flags.
536500462a9SThomas Gleixner  */
537ffdf0477SAnna-Maria Gleixner static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
538ffdf0477SAnna-Maria Gleixner 			  unsigned int idx)
539ffdf0477SAnna-Maria Gleixner {
540ffdf0477SAnna-Maria Gleixner 	hlist_add_head(&timer->entry, base->vectors + idx);
541500462a9SThomas Gleixner 	__set_bit(idx, base->pending_map);
542500462a9SThomas Gleixner 	timer_set_idx(timer, idx);
543dc1e7dc5SAnna-Maria Gleixner 
544dc1e7dc5SAnna-Maria Gleixner 	trace_timer_start(timer, timer->expires, timer->flags);
5455cee9645SThomas Gleixner }
5465cee9645SThomas Gleixner 
5475cee9645SThomas Gleixner static void
548ffdf0477SAnna-Maria Gleixner __internal_add_timer(struct timer_base *base, struct timer_list *timer)
5495cee9645SThomas Gleixner {
550ffdf0477SAnna-Maria Gleixner 	unsigned int idx;
5515cee9645SThomas Gleixner 
552ffdf0477SAnna-Maria Gleixner 	idx = calc_wheel_index(timer->expires, base->clk);
553ffdf0477SAnna-Maria Gleixner 	enqueue_timer(base, timer, idx);
5545cee9645SThomas Gleixner }
5555cee9645SThomas Gleixner 
556ffdf0477SAnna-Maria Gleixner static void
557ffdf0477SAnna-Maria Gleixner trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
5585cee9645SThomas Gleixner {
559ae67badaSThomas Gleixner 	if (!is_timers_nohz_active())
560a683f390SThomas Gleixner 		return;
5613bb475a3SThomas Gleixner 
5625cee9645SThomas Gleixner 	/*
563a683f390SThomas Gleixner 	 * TODO: This wants some optimizing similar to the code below, but we
564a683f390SThomas Gleixner 	 * will do that when we switch from push to pull for deferrable timers.
5655cee9645SThomas Gleixner 	 */
566a683f390SThomas Gleixner 	if (timer->flags & TIMER_DEFERRABLE) {
567a683f390SThomas Gleixner 		if (tick_nohz_full_cpu(base->cpu))
5689f6d9baaSViresh Kumar 			wake_up_nohz_cpu(base->cpu);
569a683f390SThomas Gleixner 		return;
5705cee9645SThomas Gleixner 	}
5715cee9645SThomas Gleixner 
5725cee9645SThomas Gleixner 	/*
573a683f390SThomas Gleixner 	 * We might have to IPI the remote CPU if the base is idle and the
574a683f390SThomas Gleixner 	 * timer is not deferrable. If the other CPU is on the way to idle
575a683f390SThomas Gleixner 	 * then it can't set base->is_idle as we hold the base lock:
5765cee9645SThomas Gleixner 	 */
577a683f390SThomas Gleixner 	if (!base->is_idle)
578a683f390SThomas Gleixner 		return;
579a683f390SThomas Gleixner 
580a683f390SThomas Gleixner 	/* Check whether this is the new first expiring timer: */
581a683f390SThomas Gleixner 	if (time_after_eq(timer->expires, base->next_expiry))
582a683f390SThomas Gleixner 		return;
583a683f390SThomas Gleixner 
584a683f390SThomas Gleixner 	/*
585a683f390SThomas Gleixner 	 * Set the next expiry time and kick the CPU so it can reevaluate the
586a683f390SThomas Gleixner 	 * wheel:
587a683f390SThomas Gleixner 	 */
588a683f390SThomas Gleixner 	base->next_expiry = timer->expires;
5895cee9645SThomas Gleixner 	wake_up_nohz_cpu(base->cpu);
5905cee9645SThomas Gleixner }
5915cee9645SThomas Gleixner 
592ffdf0477SAnna-Maria Gleixner static void
593ffdf0477SAnna-Maria Gleixner internal_add_timer(struct timer_base *base, struct timer_list *timer)
594ffdf0477SAnna-Maria Gleixner {
595ffdf0477SAnna-Maria Gleixner 	__internal_add_timer(base, timer);
596ffdf0477SAnna-Maria Gleixner 	trigger_dyntick_cpu(base, timer);
5975cee9645SThomas Gleixner }
5985cee9645SThomas Gleixner 
5995cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
6005cee9645SThomas Gleixner 
6015cee9645SThomas Gleixner static struct debug_obj_descr timer_debug_descr;
6025cee9645SThomas Gleixner 
6035cee9645SThomas Gleixner static void *timer_debug_hint(void *addr)
6045cee9645SThomas Gleixner {
6055cee9645SThomas Gleixner 	return ((struct timer_list *) addr)->function;
6065cee9645SThomas Gleixner }
6075cee9645SThomas Gleixner 
608b9fdac7fSDu, Changbin static bool timer_is_static_object(void *addr)
609b9fdac7fSDu, Changbin {
610b9fdac7fSDu, Changbin 	struct timer_list *timer = addr;
611b9fdac7fSDu, Changbin 
612b9fdac7fSDu, Changbin 	return (timer->entry.pprev == NULL &&
613b9fdac7fSDu, Changbin 		timer->entry.next == TIMER_ENTRY_STATIC);
614b9fdac7fSDu, Changbin }
615b9fdac7fSDu, Changbin 
6165cee9645SThomas Gleixner /*
6175cee9645SThomas Gleixner  * fixup_init is called when:
6185cee9645SThomas Gleixner  * - an active object is initialized
6195cee9645SThomas Gleixner  */
620e3252464SDu, Changbin static bool timer_fixup_init(void *addr, enum debug_obj_state state)
6215cee9645SThomas Gleixner {
6225cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6235cee9645SThomas Gleixner 
6245cee9645SThomas Gleixner 	switch (state) {
6255cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
6265cee9645SThomas Gleixner 		del_timer_sync(timer);
6275cee9645SThomas Gleixner 		debug_object_init(timer, &timer_debug_descr);
628e3252464SDu, Changbin 		return true;
6295cee9645SThomas Gleixner 	default:
630e3252464SDu, Changbin 		return false;
6315cee9645SThomas Gleixner 	}
6325cee9645SThomas Gleixner }
6335cee9645SThomas Gleixner 
6345cee9645SThomas Gleixner /* Stub timer callback for improperly used timers. */
635ba16490eSThomas Gleixner static void stub_timer(struct timer_list *unused)
6365cee9645SThomas Gleixner {
6375cee9645SThomas Gleixner 	WARN_ON(1);
6385cee9645SThomas Gleixner }
6395cee9645SThomas Gleixner 
6405cee9645SThomas Gleixner /*
6415cee9645SThomas Gleixner  * fixup_activate is called when:
6425cee9645SThomas Gleixner  * - an active object is activated
643b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
6445cee9645SThomas Gleixner  */
645e3252464SDu, Changbin static bool timer_fixup_activate(void *addr, enum debug_obj_state state)
6465cee9645SThomas Gleixner {
6475cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6485cee9645SThomas Gleixner 
6495cee9645SThomas Gleixner 	switch (state) {
6505cee9645SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
651ba16490eSThomas Gleixner 		timer_setup(timer, stub_timer, 0);
652e3252464SDu, Changbin 		return true;
6535cee9645SThomas Gleixner 
6545cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
6555cee9645SThomas Gleixner 		WARN_ON(1);
65675b710afSGustavo A. R. Silva 		/* fall through */
6575cee9645SThomas Gleixner 	default:
658e3252464SDu, Changbin 		return false;
6595cee9645SThomas Gleixner 	}
6605cee9645SThomas Gleixner }
6615cee9645SThomas Gleixner 
6625cee9645SThomas Gleixner /*
6635cee9645SThomas Gleixner  * fixup_free is called when:
6645cee9645SThomas Gleixner  * - an active object is freed
6655cee9645SThomas Gleixner  */
666e3252464SDu, Changbin static bool timer_fixup_free(void *addr, enum debug_obj_state state)
6675cee9645SThomas Gleixner {
6685cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6695cee9645SThomas Gleixner 
6705cee9645SThomas Gleixner 	switch (state) {
6715cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
6725cee9645SThomas Gleixner 		del_timer_sync(timer);
6735cee9645SThomas Gleixner 		debug_object_free(timer, &timer_debug_descr);
674e3252464SDu, Changbin 		return true;
6755cee9645SThomas Gleixner 	default:
676e3252464SDu, Changbin 		return false;
6775cee9645SThomas Gleixner 	}
6785cee9645SThomas Gleixner }
6795cee9645SThomas Gleixner 
6805cee9645SThomas Gleixner /*
6815cee9645SThomas Gleixner  * fixup_assert_init is called when:
6825cee9645SThomas Gleixner  * - an untracked/uninit-ed object is found
6835cee9645SThomas Gleixner  */
684e3252464SDu, Changbin static bool timer_fixup_assert_init(void *addr, enum debug_obj_state state)
6855cee9645SThomas Gleixner {
6865cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6875cee9645SThomas Gleixner 
6885cee9645SThomas Gleixner 	switch (state) {
6895cee9645SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
690ba16490eSThomas Gleixner 		timer_setup(timer, stub_timer, 0);
691e3252464SDu, Changbin 		return true;
6925cee9645SThomas Gleixner 	default:
693e3252464SDu, Changbin 		return false;
6945cee9645SThomas Gleixner 	}
6955cee9645SThomas Gleixner }
6965cee9645SThomas Gleixner 
6975cee9645SThomas Gleixner static struct debug_obj_descr timer_debug_descr = {
6985cee9645SThomas Gleixner 	.name			= "timer_list",
6995cee9645SThomas Gleixner 	.debug_hint		= timer_debug_hint,
700b9fdac7fSDu, Changbin 	.is_static_object	= timer_is_static_object,
7015cee9645SThomas Gleixner 	.fixup_init		= timer_fixup_init,
7025cee9645SThomas Gleixner 	.fixup_activate		= timer_fixup_activate,
7035cee9645SThomas Gleixner 	.fixup_free		= timer_fixup_free,
7045cee9645SThomas Gleixner 	.fixup_assert_init	= timer_fixup_assert_init,
7055cee9645SThomas Gleixner };
7065cee9645SThomas Gleixner 
7075cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer)
7085cee9645SThomas Gleixner {
7095cee9645SThomas Gleixner 	debug_object_init(timer, &timer_debug_descr);
7105cee9645SThomas Gleixner }
7115cee9645SThomas Gleixner 
7125cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer)
7135cee9645SThomas Gleixner {
7145cee9645SThomas Gleixner 	debug_object_activate(timer, &timer_debug_descr);
7155cee9645SThomas Gleixner }
7165cee9645SThomas Gleixner 
7175cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer)
7185cee9645SThomas Gleixner {
7195cee9645SThomas Gleixner 	debug_object_deactivate(timer, &timer_debug_descr);
7205cee9645SThomas Gleixner }
7215cee9645SThomas Gleixner 
7225cee9645SThomas Gleixner static inline void debug_timer_free(struct timer_list *timer)
7235cee9645SThomas Gleixner {
7245cee9645SThomas Gleixner 	debug_object_free(timer, &timer_debug_descr);
7255cee9645SThomas Gleixner }
7265cee9645SThomas Gleixner 
7275cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer)
7285cee9645SThomas Gleixner {
7295cee9645SThomas Gleixner 	debug_object_assert_init(timer, &timer_debug_descr);
7305cee9645SThomas Gleixner }
7315cee9645SThomas Gleixner 
732188665b2SKees Cook static void do_init_timer(struct timer_list *timer,
733188665b2SKees Cook 			  void (*func)(struct timer_list *),
734188665b2SKees Cook 			  unsigned int flags,
7355cee9645SThomas Gleixner 			  const char *name, struct lock_class_key *key);
7365cee9645SThomas Gleixner 
737188665b2SKees Cook void init_timer_on_stack_key(struct timer_list *timer,
738188665b2SKees Cook 			     void (*func)(struct timer_list *),
739188665b2SKees Cook 			     unsigned int flags,
7405cee9645SThomas Gleixner 			     const char *name, struct lock_class_key *key)
7415cee9645SThomas Gleixner {
7425cee9645SThomas Gleixner 	debug_object_init_on_stack(timer, &timer_debug_descr);
743188665b2SKees Cook 	do_init_timer(timer, func, flags, name, key);
7445cee9645SThomas Gleixner }
7455cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
7465cee9645SThomas Gleixner 
7475cee9645SThomas Gleixner void destroy_timer_on_stack(struct timer_list *timer)
7485cee9645SThomas Gleixner {
7495cee9645SThomas Gleixner 	debug_object_free(timer, &timer_debug_descr);
7505cee9645SThomas Gleixner }
7515cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
7525cee9645SThomas Gleixner 
7535cee9645SThomas Gleixner #else
7545cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer) { }
7555cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer) { }
7565cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer) { }
7575cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer) { }
7585cee9645SThomas Gleixner #endif
7595cee9645SThomas Gleixner 
7605cee9645SThomas Gleixner static inline void debug_init(struct timer_list *timer)
7615cee9645SThomas Gleixner {
7625cee9645SThomas Gleixner 	debug_timer_init(timer);
7635cee9645SThomas Gleixner 	trace_timer_init(timer);
7645cee9645SThomas Gleixner }
7655cee9645SThomas Gleixner 
7665cee9645SThomas Gleixner static inline void debug_deactivate(struct timer_list *timer)
7675cee9645SThomas Gleixner {
7685cee9645SThomas Gleixner 	debug_timer_deactivate(timer);
7695cee9645SThomas Gleixner 	trace_timer_cancel(timer);
7705cee9645SThomas Gleixner }
7715cee9645SThomas Gleixner 
7725cee9645SThomas Gleixner static inline void debug_assert_init(struct timer_list *timer)
7735cee9645SThomas Gleixner {
7745cee9645SThomas Gleixner 	debug_timer_assert_init(timer);
7755cee9645SThomas Gleixner }
7765cee9645SThomas Gleixner 
777188665b2SKees Cook static void do_init_timer(struct timer_list *timer,
778188665b2SKees Cook 			  void (*func)(struct timer_list *),
779188665b2SKees Cook 			  unsigned int flags,
7805cee9645SThomas Gleixner 			  const char *name, struct lock_class_key *key)
7815cee9645SThomas Gleixner {
7821dabbcecSThomas Gleixner 	timer->entry.pprev = NULL;
783188665b2SKees Cook 	timer->function = func;
7840eeda71bSThomas Gleixner 	timer->flags = flags | raw_smp_processor_id();
7855cee9645SThomas Gleixner 	lockdep_init_map(&timer->lockdep_map, name, key, 0);
7865cee9645SThomas Gleixner }
7875cee9645SThomas Gleixner 
7885cee9645SThomas Gleixner /**
7895cee9645SThomas Gleixner  * init_timer_key - initialize a timer
7905cee9645SThomas Gleixner  * @timer: the timer to be initialized
791188665b2SKees Cook  * @func: timer callback function
7925cee9645SThomas Gleixner  * @flags: timer flags
7935cee9645SThomas Gleixner  * @name: name of the timer
7945cee9645SThomas Gleixner  * @key: lockdep class key of the fake lock used for tracking timer
7955cee9645SThomas Gleixner  *       sync lock dependencies
7965cee9645SThomas Gleixner  *
7975cee9645SThomas Gleixner  * init_timer_key() must be done to a timer prior calling *any* of the
7985cee9645SThomas Gleixner  * other timer functions.
7995cee9645SThomas Gleixner  */
800188665b2SKees Cook void init_timer_key(struct timer_list *timer,
801188665b2SKees Cook 		    void (*func)(struct timer_list *), unsigned int flags,
8025cee9645SThomas Gleixner 		    const char *name, struct lock_class_key *key)
8035cee9645SThomas Gleixner {
8045cee9645SThomas Gleixner 	debug_init(timer);
805188665b2SKees Cook 	do_init_timer(timer, func, flags, name, key);
8065cee9645SThomas Gleixner }
8075cee9645SThomas Gleixner EXPORT_SYMBOL(init_timer_key);
8085cee9645SThomas Gleixner 
8095cee9645SThomas Gleixner static inline void detach_timer(struct timer_list *timer, bool clear_pending)
8105cee9645SThomas Gleixner {
8111dabbcecSThomas Gleixner 	struct hlist_node *entry = &timer->entry;
8125cee9645SThomas Gleixner 
8135cee9645SThomas Gleixner 	debug_deactivate(timer);
8145cee9645SThomas Gleixner 
8151dabbcecSThomas Gleixner 	__hlist_del(entry);
8165cee9645SThomas Gleixner 	if (clear_pending)
8171dabbcecSThomas Gleixner 		entry->pprev = NULL;
8181dabbcecSThomas Gleixner 	entry->next = LIST_POISON2;
8195cee9645SThomas Gleixner }
8205cee9645SThomas Gleixner 
821494af3edSThomas Gleixner static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
8225cee9645SThomas Gleixner 			     bool clear_pending)
8235cee9645SThomas Gleixner {
824500462a9SThomas Gleixner 	unsigned idx = timer_get_idx(timer);
825500462a9SThomas Gleixner 
8265cee9645SThomas Gleixner 	if (!timer_pending(timer))
8275cee9645SThomas Gleixner 		return 0;
8285cee9645SThomas Gleixner 
829500462a9SThomas Gleixner 	if (hlist_is_singular_node(&timer->entry, base->vectors + idx))
830500462a9SThomas Gleixner 		__clear_bit(idx, base->pending_map);
831500462a9SThomas Gleixner 
8325cee9645SThomas Gleixner 	detach_timer(timer, clear_pending);
8335cee9645SThomas Gleixner 	return 1;
8345cee9645SThomas Gleixner }
8355cee9645SThomas Gleixner 
836500462a9SThomas Gleixner static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
837500462a9SThomas Gleixner {
838500462a9SThomas Gleixner 	struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu);
839500462a9SThomas Gleixner 
8405cee9645SThomas Gleixner 	/*
841ced6d5c1SAnna-Maria Gleixner 	 * If the timer is deferrable and NO_HZ_COMMON is set then we need
842ced6d5c1SAnna-Maria Gleixner 	 * to use the deferrable base.
843500462a9SThomas Gleixner 	 */
844ced6d5c1SAnna-Maria Gleixner 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
845500462a9SThomas Gleixner 		base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
846500462a9SThomas Gleixner 	return base;
847500462a9SThomas Gleixner }
848500462a9SThomas Gleixner 
849500462a9SThomas Gleixner static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
850500462a9SThomas Gleixner {
851500462a9SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
852500462a9SThomas Gleixner 
853500462a9SThomas Gleixner 	/*
854ced6d5c1SAnna-Maria Gleixner 	 * If the timer is deferrable and NO_HZ_COMMON is set then we need
855ced6d5c1SAnna-Maria Gleixner 	 * to use the deferrable base.
856500462a9SThomas Gleixner 	 */
857ced6d5c1SAnna-Maria Gleixner 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
858500462a9SThomas Gleixner 		base = this_cpu_ptr(&timer_bases[BASE_DEF]);
859500462a9SThomas Gleixner 	return base;
860500462a9SThomas Gleixner }
861500462a9SThomas Gleixner 
862500462a9SThomas Gleixner static inline struct timer_base *get_timer_base(u32 tflags)
863500462a9SThomas Gleixner {
864500462a9SThomas Gleixner 	return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
865500462a9SThomas Gleixner }
866500462a9SThomas Gleixner 
867a683f390SThomas Gleixner static inline struct timer_base *
8686bad6bccSThomas Gleixner get_target_base(struct timer_base *base, unsigned tflags)
869500462a9SThomas Gleixner {
870ae67badaSThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
871ae67badaSThomas Gleixner 	if (static_branch_likely(&timers_migration_enabled) &&
872ae67badaSThomas Gleixner 	    !(tflags & TIMER_PINNED))
873500462a9SThomas Gleixner 		return get_timer_cpu_base(tflags, get_nohz_timer_target());
874500462a9SThomas Gleixner #endif
875ae67badaSThomas Gleixner 	return get_timer_this_cpu_base(tflags);
876500462a9SThomas Gleixner }
877500462a9SThomas Gleixner 
878a683f390SThomas Gleixner static inline void forward_timer_base(struct timer_base *base)
879a683f390SThomas Gleixner {
880ae67badaSThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
8812fe59f50SNicholas Piggin 	unsigned long jnow;
8826bad6bccSThomas Gleixner 
883a683f390SThomas Gleixner 	/*
8842fe59f50SNicholas Piggin 	 * We only forward the base when we are idle or have just come out of
8852fe59f50SNicholas Piggin 	 * idle (must_forward_clk logic), and have a delta between base clock
8862fe59f50SNicholas Piggin 	 * and jiffies. In the common case, run_timers will take care of it.
887a683f390SThomas Gleixner 	 */
8882fe59f50SNicholas Piggin 	if (likely(!base->must_forward_clk))
8892fe59f50SNicholas Piggin 		return;
8902fe59f50SNicholas Piggin 
8912fe59f50SNicholas Piggin 	jnow = READ_ONCE(jiffies);
8922fe59f50SNicholas Piggin 	base->must_forward_clk = base->is_idle;
8932fe59f50SNicholas Piggin 	if ((long)(jnow - base->clk) < 2)
894a683f390SThomas Gleixner 		return;
895a683f390SThomas Gleixner 
896a683f390SThomas Gleixner 	/*
897a683f390SThomas Gleixner 	 * If the next expiry value is > jiffies, then we fast forward to
898a683f390SThomas Gleixner 	 * jiffies otherwise we forward to the next expiry value.
899a683f390SThomas Gleixner 	 */
9006bad6bccSThomas Gleixner 	if (time_after(base->next_expiry, jnow))
9016bad6bccSThomas Gleixner 		base->clk = jnow;
902a683f390SThomas Gleixner 	else
903a683f390SThomas Gleixner 		base->clk = base->next_expiry;
904a683f390SThomas Gleixner #endif
905ae67badaSThomas Gleixner }
906a683f390SThomas Gleixner 
907a683f390SThomas Gleixner 
908500462a9SThomas Gleixner /*
909500462a9SThomas Gleixner  * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
910500462a9SThomas Gleixner  * that all timers which are tied to this base are locked, and the base itself
911500462a9SThomas Gleixner  * is locked too.
9125cee9645SThomas Gleixner  *
9135cee9645SThomas Gleixner  * So __run_timers/migrate_timers can safely modify all timers which could
914500462a9SThomas Gleixner  * be found in the base->vectors array.
9155cee9645SThomas Gleixner  *
916500462a9SThomas Gleixner  * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
917500462a9SThomas Gleixner  * to wait until the migration is done.
9185cee9645SThomas Gleixner  */
919494af3edSThomas Gleixner static struct timer_base *lock_timer_base(struct timer_list *timer,
9205cee9645SThomas Gleixner 					  unsigned long *flags)
9215cee9645SThomas Gleixner 	__acquires(timer->base->lock)
9225cee9645SThomas Gleixner {
9230eeda71bSThomas Gleixner 	for (;;) {
924494af3edSThomas Gleixner 		struct timer_base *base;
925b831275aSThomas Gleixner 		u32 tf;
926b831275aSThomas Gleixner 
927b831275aSThomas Gleixner 		/*
928b831275aSThomas Gleixner 		 * We need to use READ_ONCE() here, otherwise the compiler
929b831275aSThomas Gleixner 		 * might re-read @tf between the check for TIMER_MIGRATING
930b831275aSThomas Gleixner 		 * and spin_lock().
931b831275aSThomas Gleixner 		 */
932b831275aSThomas Gleixner 		tf = READ_ONCE(timer->flags);
9335cee9645SThomas Gleixner 
9340eeda71bSThomas Gleixner 		if (!(tf & TIMER_MIGRATING)) {
935500462a9SThomas Gleixner 			base = get_timer_base(tf);
9362287d866SSebastian Andrzej Siewior 			raw_spin_lock_irqsave(&base->lock, *flags);
9370eeda71bSThomas Gleixner 			if (timer->flags == tf)
9385cee9645SThomas Gleixner 				return base;
9392287d866SSebastian Andrzej Siewior 			raw_spin_unlock_irqrestore(&base->lock, *flags);
9405cee9645SThomas Gleixner 		}
9415cee9645SThomas Gleixner 		cpu_relax();
9425cee9645SThomas Gleixner 	}
9435cee9645SThomas Gleixner }
9445cee9645SThomas Gleixner 
945b24591e2SDavid Howells #define MOD_TIMER_PENDING_ONLY		0x01
946b24591e2SDavid Howells #define MOD_TIMER_REDUCE		0x02
947b24591e2SDavid Howells 
9485cee9645SThomas Gleixner static inline int
949b24591e2SDavid Howells __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options)
9505cee9645SThomas Gleixner {
951494af3edSThomas Gleixner 	struct timer_base *base, *new_base;
952f00c0afdSAnna-Maria Gleixner 	unsigned int idx = UINT_MAX;
953f00c0afdSAnna-Maria Gleixner 	unsigned long clk = 0, flags;
954bc7a34b8SThomas Gleixner 	int ret = 0;
9555cee9645SThomas Gleixner 
9564da9152aSThomas Gleixner 	BUG_ON(!timer->function);
9574da9152aSThomas Gleixner 
958500462a9SThomas Gleixner 	/*
959f00c0afdSAnna-Maria Gleixner 	 * This is a common optimization triggered by the networking code - if
960f00c0afdSAnna-Maria Gleixner 	 * the timer is re-modified to have the same timeout or ends up in the
961f00c0afdSAnna-Maria Gleixner 	 * same array bucket then just return:
962500462a9SThomas Gleixner 	 */
963500462a9SThomas Gleixner 	if (timer_pending(timer)) {
9642fe59f50SNicholas Piggin 		/*
9652fe59f50SNicholas Piggin 		 * The downside of this optimization is that it can result in
9662fe59f50SNicholas Piggin 		 * larger granularity than you would get from adding a new
9672fe59f50SNicholas Piggin 		 * timer with this expiry.
9682fe59f50SNicholas Piggin 		 */
969b24591e2SDavid Howells 		long diff = timer->expires - expires;
970b24591e2SDavid Howells 
971b24591e2SDavid Howells 		if (!diff)
972b24591e2SDavid Howells 			return 1;
973b24591e2SDavid Howells 		if (options & MOD_TIMER_REDUCE && diff <= 0)
974500462a9SThomas Gleixner 			return 1;
975f00c0afdSAnna-Maria Gleixner 
9764da9152aSThomas Gleixner 		/*
9774da9152aSThomas Gleixner 		 * We lock timer base and calculate the bucket index right
9784da9152aSThomas Gleixner 		 * here. If the timer ends up in the same bucket, then we
9794da9152aSThomas Gleixner 		 * just update the expiry time and avoid the whole
9804da9152aSThomas Gleixner 		 * dequeue/enqueue dance.
9814da9152aSThomas Gleixner 		 */
9824da9152aSThomas Gleixner 		base = lock_timer_base(timer, &flags);
9832fe59f50SNicholas Piggin 		forward_timer_base(base);
9844da9152aSThomas Gleixner 
985b24591e2SDavid Howells 		if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) &&
986b24591e2SDavid Howells 		    time_before_eq(timer->expires, expires)) {
987b24591e2SDavid Howells 			ret = 1;
988b24591e2SDavid Howells 			goto out_unlock;
989b24591e2SDavid Howells 		}
990b24591e2SDavid Howells 
9914da9152aSThomas Gleixner 		clk = base->clk;
992f00c0afdSAnna-Maria Gleixner 		idx = calc_wheel_index(expires, clk);
993f00c0afdSAnna-Maria Gleixner 
994f00c0afdSAnna-Maria Gleixner 		/*
995f00c0afdSAnna-Maria Gleixner 		 * Retrieve and compare the array index of the pending
996f00c0afdSAnna-Maria Gleixner 		 * timer. If it matches set the expiry to the new value so a
997f00c0afdSAnna-Maria Gleixner 		 * subsequent call will exit in the expires check above.
998f00c0afdSAnna-Maria Gleixner 		 */
999f00c0afdSAnna-Maria Gleixner 		if (idx == timer_get_idx(timer)) {
1000b24591e2SDavid Howells 			if (!(options & MOD_TIMER_REDUCE))
1001b24591e2SDavid Howells 				timer->expires = expires;
1002b24591e2SDavid Howells 			else if (time_after(timer->expires, expires))
1003f00c0afdSAnna-Maria Gleixner 				timer->expires = expires;
10044da9152aSThomas Gleixner 			ret = 1;
10054da9152aSThomas Gleixner 			goto out_unlock;
1006f00c0afdSAnna-Maria Gleixner 		}
10074da9152aSThomas Gleixner 	} else {
10084da9152aSThomas Gleixner 		base = lock_timer_base(timer, &flags);
10092fe59f50SNicholas Piggin 		forward_timer_base(base);
1010500462a9SThomas Gleixner 	}
1011500462a9SThomas Gleixner 
10125cee9645SThomas Gleixner 	ret = detach_if_pending(timer, base, false);
1013b24591e2SDavid Howells 	if (!ret && (options & MOD_TIMER_PENDING_ONLY))
10145cee9645SThomas Gleixner 		goto out_unlock;
10155cee9645SThomas Gleixner 
1016500462a9SThomas Gleixner 	new_base = get_target_base(base, timer->flags);
10175cee9645SThomas Gleixner 
10185cee9645SThomas Gleixner 	if (base != new_base) {
10195cee9645SThomas Gleixner 		/*
1020500462a9SThomas Gleixner 		 * We are trying to schedule the timer on the new base.
10215cee9645SThomas Gleixner 		 * However we can't change timer's base while it is running,
10225cee9645SThomas Gleixner 		 * otherwise del_timer_sync() can't detect that the timer's
1023500462a9SThomas Gleixner 		 * handler yet has not finished. This also guarantees that the
1024500462a9SThomas Gleixner 		 * timer is serialized wrt itself.
10255cee9645SThomas Gleixner 		 */
10265cee9645SThomas Gleixner 		if (likely(base->running_timer != timer)) {
10275cee9645SThomas Gleixner 			/* See the comment in lock_timer_base() */
10280eeda71bSThomas Gleixner 			timer->flags |= TIMER_MIGRATING;
10290eeda71bSThomas Gleixner 
10302287d866SSebastian Andrzej Siewior 			raw_spin_unlock(&base->lock);
10315cee9645SThomas Gleixner 			base = new_base;
10322287d866SSebastian Andrzej Siewior 			raw_spin_lock(&base->lock);
1033d0023a14SEric Dumazet 			WRITE_ONCE(timer->flags,
1034d0023a14SEric Dumazet 				   (timer->flags & ~TIMER_BASEMASK) | base->cpu);
10356bad6bccSThomas Gleixner 			forward_timer_base(base);
10362fe59f50SNicholas Piggin 		}
10372fe59f50SNicholas Piggin 	}
10386bad6bccSThomas Gleixner 
1039dc1e7dc5SAnna-Maria Gleixner 	debug_timer_activate(timer);
1040fd45bb77SThomas Gleixner 
10415cee9645SThomas Gleixner 	timer->expires = expires;
1042f00c0afdSAnna-Maria Gleixner 	/*
1043f00c0afdSAnna-Maria Gleixner 	 * If 'idx' was calculated above and the base time did not advance
10444da9152aSThomas Gleixner 	 * between calculating 'idx' and possibly switching the base, only
10454da9152aSThomas Gleixner 	 * enqueue_timer() and trigger_dyntick_cpu() is required. Otherwise
10464da9152aSThomas Gleixner 	 * we need to (re)calculate the wheel index via
10474da9152aSThomas Gleixner 	 * internal_add_timer().
1048f00c0afdSAnna-Maria Gleixner 	 */
1049f00c0afdSAnna-Maria Gleixner 	if (idx != UINT_MAX && clk == base->clk) {
1050f00c0afdSAnna-Maria Gleixner 		enqueue_timer(base, timer, idx);
1051f00c0afdSAnna-Maria Gleixner 		trigger_dyntick_cpu(base, timer);
1052f00c0afdSAnna-Maria Gleixner 	} else {
10535cee9645SThomas Gleixner 		internal_add_timer(base, timer);
1054f00c0afdSAnna-Maria Gleixner 	}
10555cee9645SThomas Gleixner 
10565cee9645SThomas Gleixner out_unlock:
10572287d866SSebastian Andrzej Siewior 	raw_spin_unlock_irqrestore(&base->lock, flags);
10585cee9645SThomas Gleixner 
10595cee9645SThomas Gleixner 	return ret;
10605cee9645SThomas Gleixner }
10615cee9645SThomas Gleixner 
10625cee9645SThomas Gleixner /**
10635cee9645SThomas Gleixner  * mod_timer_pending - modify a pending timer's timeout
10645cee9645SThomas Gleixner  * @timer: the pending timer to be modified
10655cee9645SThomas Gleixner  * @expires: new timeout in jiffies
10665cee9645SThomas Gleixner  *
10675cee9645SThomas Gleixner  * mod_timer_pending() is the same for pending timers as mod_timer(),
10685cee9645SThomas Gleixner  * but will not re-activate and modify already deleted timers.
10695cee9645SThomas Gleixner  *
10705cee9645SThomas Gleixner  * It is useful for unserialized use of timers.
10715cee9645SThomas Gleixner  */
10725cee9645SThomas Gleixner int mod_timer_pending(struct timer_list *timer, unsigned long expires)
10735cee9645SThomas Gleixner {
1074b24591e2SDavid Howells 	return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY);
10755cee9645SThomas Gleixner }
10765cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer_pending);
10775cee9645SThomas Gleixner 
10785cee9645SThomas Gleixner /**
10795cee9645SThomas Gleixner  * mod_timer - modify a timer's timeout
10805cee9645SThomas Gleixner  * @timer: the timer to be modified
10815cee9645SThomas Gleixner  * @expires: new timeout in jiffies
10825cee9645SThomas Gleixner  *
10835cee9645SThomas Gleixner  * mod_timer() is a more efficient way to update the expire field of an
10845cee9645SThomas Gleixner  * active timer (if the timer is inactive it will be activated)
10855cee9645SThomas Gleixner  *
10865cee9645SThomas Gleixner  * mod_timer(timer, expires) is equivalent to:
10875cee9645SThomas Gleixner  *
10885cee9645SThomas Gleixner  *     del_timer(timer); timer->expires = expires; add_timer(timer);
10895cee9645SThomas Gleixner  *
10905cee9645SThomas Gleixner  * Note that if there are multiple unserialized concurrent users of the
10915cee9645SThomas Gleixner  * same timer, then mod_timer() is the only safe way to modify the timeout,
10925cee9645SThomas Gleixner  * since add_timer() cannot modify an already running timer.
10935cee9645SThomas Gleixner  *
10945cee9645SThomas Gleixner  * The function returns whether it has modified a pending timer or not.
10955cee9645SThomas Gleixner  * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
10965cee9645SThomas Gleixner  * active timer returns 1.)
10975cee9645SThomas Gleixner  */
10985cee9645SThomas Gleixner int mod_timer(struct timer_list *timer, unsigned long expires)
10995cee9645SThomas Gleixner {
1100b24591e2SDavid Howells 	return __mod_timer(timer, expires, 0);
11015cee9645SThomas Gleixner }
11025cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer);
11035cee9645SThomas Gleixner 
11045cee9645SThomas Gleixner /**
1105b24591e2SDavid Howells  * timer_reduce - Modify a timer's timeout if it would reduce the timeout
1106b24591e2SDavid Howells  * @timer:	The timer to be modified
1107b24591e2SDavid Howells  * @expires:	New timeout in jiffies
1108b24591e2SDavid Howells  *
1109b24591e2SDavid Howells  * timer_reduce() is very similar to mod_timer(), except that it will only
1110b24591e2SDavid Howells  * modify a running timer if that would reduce the expiration time (it will
1111b24591e2SDavid Howells  * start a timer that isn't running).
1112b24591e2SDavid Howells  */
1113b24591e2SDavid Howells int timer_reduce(struct timer_list *timer, unsigned long expires)
1114b24591e2SDavid Howells {
1115b24591e2SDavid Howells 	return __mod_timer(timer, expires, MOD_TIMER_REDUCE);
1116b24591e2SDavid Howells }
1117b24591e2SDavid Howells EXPORT_SYMBOL(timer_reduce);
1118b24591e2SDavid Howells 
1119b24591e2SDavid Howells /**
11205cee9645SThomas Gleixner  * add_timer - start a timer
11215cee9645SThomas Gleixner  * @timer: the timer to be added
11225cee9645SThomas Gleixner  *
1123c1eba5bcSKees Cook  * The kernel will do a ->function(@timer) callback from the
11245cee9645SThomas Gleixner  * timer interrupt at the ->expires point in the future. The
11255cee9645SThomas Gleixner  * current time is 'jiffies'.
11265cee9645SThomas Gleixner  *
1127c1eba5bcSKees Cook  * The timer's ->expires, ->function fields must be set prior calling this
1128c1eba5bcSKees Cook  * function.
11295cee9645SThomas Gleixner  *
11305cee9645SThomas Gleixner  * Timers with an ->expires field in the past will be executed in the next
11315cee9645SThomas Gleixner  * timer tick.
11325cee9645SThomas Gleixner  */
11335cee9645SThomas Gleixner void add_timer(struct timer_list *timer)
11345cee9645SThomas Gleixner {
11355cee9645SThomas Gleixner 	BUG_ON(timer_pending(timer));
11365cee9645SThomas Gleixner 	mod_timer(timer, timer->expires);
11375cee9645SThomas Gleixner }
11385cee9645SThomas Gleixner EXPORT_SYMBOL(add_timer);
11395cee9645SThomas Gleixner 
11405cee9645SThomas Gleixner /**
11415cee9645SThomas Gleixner  * add_timer_on - start a timer on a particular CPU
11425cee9645SThomas Gleixner  * @timer: the timer to be added
11435cee9645SThomas Gleixner  * @cpu: the CPU to start it on
11445cee9645SThomas Gleixner  *
11455cee9645SThomas Gleixner  * This is not very scalable on SMP. Double adds are not possible.
11465cee9645SThomas Gleixner  */
11475cee9645SThomas Gleixner void add_timer_on(struct timer_list *timer, int cpu)
11485cee9645SThomas Gleixner {
1149500462a9SThomas Gleixner 	struct timer_base *new_base, *base;
11505cee9645SThomas Gleixner 	unsigned long flags;
11515cee9645SThomas Gleixner 
11525cee9645SThomas Gleixner 	BUG_ON(timer_pending(timer) || !timer->function);
115322b886ddSTejun Heo 
1154500462a9SThomas Gleixner 	new_base = get_timer_cpu_base(timer->flags, cpu);
1155500462a9SThomas Gleixner 
115622b886ddSTejun Heo 	/*
115722b886ddSTejun Heo 	 * If @timer was on a different CPU, it should be migrated with the
115822b886ddSTejun Heo 	 * old base locked to prevent other operations proceeding with the
115922b886ddSTejun Heo 	 * wrong base locked.  See lock_timer_base().
116022b886ddSTejun Heo 	 */
116122b886ddSTejun Heo 	base = lock_timer_base(timer, &flags);
116222b886ddSTejun Heo 	if (base != new_base) {
116322b886ddSTejun Heo 		timer->flags |= TIMER_MIGRATING;
116422b886ddSTejun Heo 
11652287d866SSebastian Andrzej Siewior 		raw_spin_unlock(&base->lock);
116622b886ddSTejun Heo 		base = new_base;
11672287d866SSebastian Andrzej Siewior 		raw_spin_lock(&base->lock);
116822b886ddSTejun Heo 		WRITE_ONCE(timer->flags,
116922b886ddSTejun Heo 			   (timer->flags & ~TIMER_BASEMASK) | cpu);
117022b886ddSTejun Heo 	}
11712fe59f50SNicholas Piggin 	forward_timer_base(base);
117222b886ddSTejun Heo 
1173dc1e7dc5SAnna-Maria Gleixner 	debug_timer_activate(timer);
11745cee9645SThomas Gleixner 	internal_add_timer(base, timer);
11752287d866SSebastian Andrzej Siewior 	raw_spin_unlock_irqrestore(&base->lock, flags);
11765cee9645SThomas Gleixner }
11775cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(add_timer_on);
11785cee9645SThomas Gleixner 
11795cee9645SThomas Gleixner /**
11800ba42a59SMasanari Iida  * del_timer - deactivate a timer.
11815cee9645SThomas Gleixner  * @timer: the timer to be deactivated
11825cee9645SThomas Gleixner  *
11835cee9645SThomas Gleixner  * del_timer() deactivates a timer - this works on both active and inactive
11845cee9645SThomas Gleixner  * timers.
11855cee9645SThomas Gleixner  *
11865cee9645SThomas Gleixner  * The function returns whether it has deactivated a pending timer or not.
11875cee9645SThomas Gleixner  * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
11885cee9645SThomas Gleixner  * active timer returns 1.)
11895cee9645SThomas Gleixner  */
11905cee9645SThomas Gleixner int del_timer(struct timer_list *timer)
11915cee9645SThomas Gleixner {
1192494af3edSThomas Gleixner 	struct timer_base *base;
11935cee9645SThomas Gleixner 	unsigned long flags;
11945cee9645SThomas Gleixner 	int ret = 0;
11955cee9645SThomas Gleixner 
11965cee9645SThomas Gleixner 	debug_assert_init(timer);
11975cee9645SThomas Gleixner 
11985cee9645SThomas Gleixner 	if (timer_pending(timer)) {
11995cee9645SThomas Gleixner 		base = lock_timer_base(timer, &flags);
12005cee9645SThomas Gleixner 		ret = detach_if_pending(timer, base, true);
12012287d866SSebastian Andrzej Siewior 		raw_spin_unlock_irqrestore(&base->lock, flags);
12025cee9645SThomas Gleixner 	}
12035cee9645SThomas Gleixner 
12045cee9645SThomas Gleixner 	return ret;
12055cee9645SThomas Gleixner }
12065cee9645SThomas Gleixner EXPORT_SYMBOL(del_timer);
12075cee9645SThomas Gleixner 
12085cee9645SThomas Gleixner /**
12095cee9645SThomas Gleixner  * try_to_del_timer_sync - Try to deactivate a timer
1210d15bc69aSPeter Meerwald-Stadler  * @timer: timer to delete
12115cee9645SThomas Gleixner  *
12125cee9645SThomas Gleixner  * This function tries to deactivate a timer. Upon successful (ret >= 0)
12135cee9645SThomas Gleixner  * exit the timer is not queued and the handler is not running on any CPU.
12145cee9645SThomas Gleixner  */
12155cee9645SThomas Gleixner int try_to_del_timer_sync(struct timer_list *timer)
12165cee9645SThomas Gleixner {
1217494af3edSThomas Gleixner 	struct timer_base *base;
12185cee9645SThomas Gleixner 	unsigned long flags;
12195cee9645SThomas Gleixner 	int ret = -1;
12205cee9645SThomas Gleixner 
12215cee9645SThomas Gleixner 	debug_assert_init(timer);
12225cee9645SThomas Gleixner 
12235cee9645SThomas Gleixner 	base = lock_timer_base(timer, &flags);
12245cee9645SThomas Gleixner 
1225dfb4357dSKees Cook 	if (base->running_timer != timer)
12265cee9645SThomas Gleixner 		ret = detach_if_pending(timer, base, true);
1227dfb4357dSKees Cook 
12282287d866SSebastian Andrzej Siewior 	raw_spin_unlock_irqrestore(&base->lock, flags);
12295cee9645SThomas Gleixner 
12305cee9645SThomas Gleixner 	return ret;
12315cee9645SThomas Gleixner }
12325cee9645SThomas Gleixner EXPORT_SYMBOL(try_to_del_timer_sync);
12335cee9645SThomas Gleixner 
1234030dcdd1SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT
1235030dcdd1SAnna-Maria Gleixner static __init void timer_base_init_expiry_lock(struct timer_base *base)
1236030dcdd1SAnna-Maria Gleixner {
1237030dcdd1SAnna-Maria Gleixner 	spin_lock_init(&base->expiry_lock);
1238030dcdd1SAnna-Maria Gleixner }
1239030dcdd1SAnna-Maria Gleixner 
1240030dcdd1SAnna-Maria Gleixner static inline void timer_base_lock_expiry(struct timer_base *base)
1241030dcdd1SAnna-Maria Gleixner {
1242030dcdd1SAnna-Maria Gleixner 	spin_lock(&base->expiry_lock);
1243030dcdd1SAnna-Maria Gleixner }
1244030dcdd1SAnna-Maria Gleixner 
1245030dcdd1SAnna-Maria Gleixner static inline void timer_base_unlock_expiry(struct timer_base *base)
1246030dcdd1SAnna-Maria Gleixner {
1247030dcdd1SAnna-Maria Gleixner 	spin_unlock(&base->expiry_lock);
1248030dcdd1SAnna-Maria Gleixner }
1249030dcdd1SAnna-Maria Gleixner 
1250030dcdd1SAnna-Maria Gleixner /*
1251030dcdd1SAnna-Maria Gleixner  * The counterpart to del_timer_wait_running().
1252030dcdd1SAnna-Maria Gleixner  *
1253030dcdd1SAnna-Maria Gleixner  * If there is a waiter for base->expiry_lock, then it was waiting for the
1254030dcdd1SAnna-Maria Gleixner  * timer callback to finish. Drop expiry_lock and reaquire it. That allows
1255030dcdd1SAnna-Maria Gleixner  * the waiter to acquire the lock and make progress.
1256030dcdd1SAnna-Maria Gleixner  */
1257030dcdd1SAnna-Maria Gleixner static void timer_sync_wait_running(struct timer_base *base)
1258030dcdd1SAnna-Maria Gleixner {
1259030dcdd1SAnna-Maria Gleixner 	if (atomic_read(&base->timer_waiters)) {
1260030dcdd1SAnna-Maria Gleixner 		spin_unlock(&base->expiry_lock);
1261030dcdd1SAnna-Maria Gleixner 		spin_lock(&base->expiry_lock);
1262030dcdd1SAnna-Maria Gleixner 	}
1263030dcdd1SAnna-Maria Gleixner }
1264030dcdd1SAnna-Maria Gleixner 
1265030dcdd1SAnna-Maria Gleixner /*
1266030dcdd1SAnna-Maria Gleixner  * This function is called on PREEMPT_RT kernels when the fast path
1267030dcdd1SAnna-Maria Gleixner  * deletion of a timer failed because the timer callback function was
1268030dcdd1SAnna-Maria Gleixner  * running.
1269030dcdd1SAnna-Maria Gleixner  *
1270030dcdd1SAnna-Maria Gleixner  * This prevents priority inversion, if the softirq thread on a remote CPU
1271030dcdd1SAnna-Maria Gleixner  * got preempted, and it prevents a life lock when the task which tries to
1272030dcdd1SAnna-Maria Gleixner  * delete a timer preempted the softirq thread running the timer callback
1273030dcdd1SAnna-Maria Gleixner  * function.
1274030dcdd1SAnna-Maria Gleixner  */
1275030dcdd1SAnna-Maria Gleixner static void del_timer_wait_running(struct timer_list *timer)
1276030dcdd1SAnna-Maria Gleixner {
1277030dcdd1SAnna-Maria Gleixner 	u32 tf;
1278030dcdd1SAnna-Maria Gleixner 
1279030dcdd1SAnna-Maria Gleixner 	tf = READ_ONCE(timer->flags);
1280030dcdd1SAnna-Maria Gleixner 	if (!(tf & TIMER_MIGRATING)) {
1281030dcdd1SAnna-Maria Gleixner 		struct timer_base *base = get_timer_base(tf);
1282030dcdd1SAnna-Maria Gleixner 
1283030dcdd1SAnna-Maria Gleixner 		/*
1284030dcdd1SAnna-Maria Gleixner 		 * Mark the base as contended and grab the expiry lock,
1285030dcdd1SAnna-Maria Gleixner 		 * which is held by the softirq across the timer
1286030dcdd1SAnna-Maria Gleixner 		 * callback. Drop the lock immediately so the softirq can
1287030dcdd1SAnna-Maria Gleixner 		 * expire the next timer. In theory the timer could already
1288030dcdd1SAnna-Maria Gleixner 		 * be running again, but that's more than unlikely and just
1289030dcdd1SAnna-Maria Gleixner 		 * causes another wait loop.
1290030dcdd1SAnna-Maria Gleixner 		 */
1291030dcdd1SAnna-Maria Gleixner 		atomic_inc(&base->timer_waiters);
1292030dcdd1SAnna-Maria Gleixner 		spin_lock_bh(&base->expiry_lock);
1293030dcdd1SAnna-Maria Gleixner 		atomic_dec(&base->timer_waiters);
1294030dcdd1SAnna-Maria Gleixner 		spin_unlock_bh(&base->expiry_lock);
1295030dcdd1SAnna-Maria Gleixner 	}
1296030dcdd1SAnna-Maria Gleixner }
1297030dcdd1SAnna-Maria Gleixner #else
1298030dcdd1SAnna-Maria Gleixner static inline void timer_base_init_expiry_lock(struct timer_base *base) { }
1299030dcdd1SAnna-Maria Gleixner static inline void timer_base_lock_expiry(struct timer_base *base) { }
1300030dcdd1SAnna-Maria Gleixner static inline void timer_base_unlock_expiry(struct timer_base *base) { }
1301030dcdd1SAnna-Maria Gleixner static inline void timer_sync_wait_running(struct timer_base *base) { }
1302030dcdd1SAnna-Maria Gleixner static inline void del_timer_wait_running(struct timer_list *timer) { }
1303030dcdd1SAnna-Maria Gleixner #endif
1304030dcdd1SAnna-Maria Gleixner 
1305030dcdd1SAnna-Maria Gleixner #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
13065cee9645SThomas Gleixner /**
13075cee9645SThomas Gleixner  * del_timer_sync - deactivate a timer and wait for the handler to finish.
13085cee9645SThomas Gleixner  * @timer: the timer to be deactivated
13095cee9645SThomas Gleixner  *
13105cee9645SThomas Gleixner  * This function only differs from del_timer() on SMP: besides deactivating
13115cee9645SThomas Gleixner  * the timer it also makes sure the handler has finished executing on other
13125cee9645SThomas Gleixner  * CPUs.
13135cee9645SThomas Gleixner  *
13145cee9645SThomas Gleixner  * Synchronization rules: Callers must prevent restarting of the timer,
13155cee9645SThomas Gleixner  * otherwise this function is meaningless. It must not be called from
13165cee9645SThomas Gleixner  * interrupt contexts unless the timer is an irqsafe one. The caller must
13175cee9645SThomas Gleixner  * not hold locks which would prevent completion of the timer's
13185cee9645SThomas Gleixner  * handler. The timer's handler must not call add_timer_on(). Upon exit the
13195cee9645SThomas Gleixner  * timer is not queued and the handler is not running on any CPU.
13205cee9645SThomas Gleixner  *
13215cee9645SThomas Gleixner  * Note: For !irqsafe timers, you must not hold locks that are held in
13225cee9645SThomas Gleixner  *   interrupt context while calling this function. Even if the lock has
1323bf9c96beSMauro Carvalho Chehab  *   nothing to do with the timer in question.  Here's why::
13245cee9645SThomas Gleixner  *
13255cee9645SThomas Gleixner  *    CPU0                             CPU1
13265cee9645SThomas Gleixner  *    ----                             ----
13275cee9645SThomas Gleixner  *                                     <SOFTIRQ>
13285cee9645SThomas Gleixner  *                                       call_timer_fn();
13295cee9645SThomas Gleixner  *                                       base->running_timer = mytimer;
13305cee9645SThomas Gleixner  *    spin_lock_irq(somelock);
13315cee9645SThomas Gleixner  *                                     <IRQ>
13325cee9645SThomas Gleixner  *                                        spin_lock(somelock);
13335cee9645SThomas Gleixner  *    del_timer_sync(mytimer);
13345cee9645SThomas Gleixner  *    while (base->running_timer == mytimer);
13355cee9645SThomas Gleixner  *
13365cee9645SThomas Gleixner  * Now del_timer_sync() will never return and never release somelock.
13375cee9645SThomas Gleixner  * The interrupt on the other CPU is waiting to grab somelock but
13385cee9645SThomas Gleixner  * it has interrupted the softirq that CPU0 is waiting to finish.
13395cee9645SThomas Gleixner  *
13405cee9645SThomas Gleixner  * The function returns whether it has deactivated a pending timer or not.
13415cee9645SThomas Gleixner  */
13425cee9645SThomas Gleixner int del_timer_sync(struct timer_list *timer)
13435cee9645SThomas Gleixner {
1344030dcdd1SAnna-Maria Gleixner 	int ret;
1345030dcdd1SAnna-Maria Gleixner 
13465cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP
13475cee9645SThomas Gleixner 	unsigned long flags;
13485cee9645SThomas Gleixner 
13495cee9645SThomas Gleixner 	/*
13505cee9645SThomas Gleixner 	 * If lockdep gives a backtrace here, please reference
13515cee9645SThomas Gleixner 	 * the synchronization rules above.
13525cee9645SThomas Gleixner 	 */
13535cee9645SThomas Gleixner 	local_irq_save(flags);
13545cee9645SThomas Gleixner 	lock_map_acquire(&timer->lockdep_map);
13555cee9645SThomas Gleixner 	lock_map_release(&timer->lockdep_map);
13565cee9645SThomas Gleixner 	local_irq_restore(flags);
13575cee9645SThomas Gleixner #endif
13585cee9645SThomas Gleixner 	/*
13595cee9645SThomas Gleixner 	 * don't use it in hardirq context, because it
13605cee9645SThomas Gleixner 	 * could lead to deadlock.
13615cee9645SThomas Gleixner 	 */
13620eeda71bSThomas Gleixner 	WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
1363030dcdd1SAnna-Maria Gleixner 
1364030dcdd1SAnna-Maria Gleixner 	do {
1365030dcdd1SAnna-Maria Gleixner 		ret = try_to_del_timer_sync(timer);
1366030dcdd1SAnna-Maria Gleixner 
1367030dcdd1SAnna-Maria Gleixner 		if (unlikely(ret < 0)) {
1368030dcdd1SAnna-Maria Gleixner 			del_timer_wait_running(timer);
13695cee9645SThomas Gleixner 			cpu_relax();
13705cee9645SThomas Gleixner 		}
1371030dcdd1SAnna-Maria Gleixner 	} while (ret < 0);
1372030dcdd1SAnna-Maria Gleixner 
1373030dcdd1SAnna-Maria Gleixner 	return ret;
13745cee9645SThomas Gleixner }
13755cee9645SThomas Gleixner EXPORT_SYMBOL(del_timer_sync);
13765cee9645SThomas Gleixner #endif
13775cee9645SThomas Gleixner 
1378f28d3d53SAnna-Maria Gleixner static void call_timer_fn(struct timer_list *timer,
1379f28d3d53SAnna-Maria Gleixner 			  void (*fn)(struct timer_list *),
1380f28d3d53SAnna-Maria Gleixner 			  unsigned long baseclk)
13815cee9645SThomas Gleixner {
13825cee9645SThomas Gleixner 	int count = preempt_count();
13835cee9645SThomas Gleixner 
13845cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP
13855cee9645SThomas Gleixner 	/*
13865cee9645SThomas Gleixner 	 * It is permissible to free the timer from inside the
13875cee9645SThomas Gleixner 	 * function that is called from it, this we need to take into
13885cee9645SThomas Gleixner 	 * account for lockdep too. To avoid bogus "held lock freed"
13895cee9645SThomas Gleixner 	 * warnings as well as problems when looking into
13905cee9645SThomas Gleixner 	 * timer->lockdep_map, make a copy and use that here.
13915cee9645SThomas Gleixner 	 */
13925cee9645SThomas Gleixner 	struct lockdep_map lockdep_map;
13935cee9645SThomas Gleixner 
13945cee9645SThomas Gleixner 	lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
13955cee9645SThomas Gleixner #endif
13965cee9645SThomas Gleixner 	/*
13975cee9645SThomas Gleixner 	 * Couple the lock chain with the lock chain at
13985cee9645SThomas Gleixner 	 * del_timer_sync() by acquiring the lock_map around the fn()
13995cee9645SThomas Gleixner 	 * call here and in del_timer_sync().
14005cee9645SThomas Gleixner 	 */
14015cee9645SThomas Gleixner 	lock_map_acquire(&lockdep_map);
14025cee9645SThomas Gleixner 
1403f28d3d53SAnna-Maria Gleixner 	trace_timer_expire_entry(timer, baseclk);
1404354b46b1SKees Cook 	fn(timer);
14055cee9645SThomas Gleixner 	trace_timer_expire_exit(timer);
14065cee9645SThomas Gleixner 
14075cee9645SThomas Gleixner 	lock_map_release(&lockdep_map);
14085cee9645SThomas Gleixner 
14095cee9645SThomas Gleixner 	if (count != preempt_count()) {
1410d75f773cSSakari Ailus 		WARN_ONCE(1, "timer: %pS preempt leak: %08x -> %08x\n",
14115cee9645SThomas Gleixner 			  fn, count, preempt_count());
14125cee9645SThomas Gleixner 		/*
14135cee9645SThomas Gleixner 		 * Restore the preempt count. That gives us a decent
14145cee9645SThomas Gleixner 		 * chance to survive and extract information. If the
14155cee9645SThomas Gleixner 		 * callback kept a lock held, bad luck, but not worse
14165cee9645SThomas Gleixner 		 * than the BUG() we had.
14175cee9645SThomas Gleixner 		 */
14185cee9645SThomas Gleixner 		preempt_count_set(count);
14195cee9645SThomas Gleixner 	}
14205cee9645SThomas Gleixner }
14215cee9645SThomas Gleixner 
1422500462a9SThomas Gleixner static void expire_timers(struct timer_base *base, struct hlist_head *head)
14235cee9645SThomas Gleixner {
1424f28d3d53SAnna-Maria Gleixner 	/*
1425f28d3d53SAnna-Maria Gleixner 	 * This value is required only for tracing. base->clk was
1426f28d3d53SAnna-Maria Gleixner 	 * incremented directly before expire_timers was called. But expiry
1427f28d3d53SAnna-Maria Gleixner 	 * is related to the old base->clk value.
1428f28d3d53SAnna-Maria Gleixner 	 */
1429f28d3d53SAnna-Maria Gleixner 	unsigned long baseclk = base->clk - 1;
1430f28d3d53SAnna-Maria Gleixner 
14311dabbcecSThomas Gleixner 	while (!hlist_empty(head)) {
1432500462a9SThomas Gleixner 		struct timer_list *timer;
1433354b46b1SKees Cook 		void (*fn)(struct timer_list *);
14345cee9645SThomas Gleixner 
14351dabbcecSThomas Gleixner 		timer = hlist_entry(head->first, struct timer_list, entry);
14365cee9645SThomas Gleixner 
14375cee9645SThomas Gleixner 		base->running_timer = timer;
1438500462a9SThomas Gleixner 		detach_timer(timer, true);
14395cee9645SThomas Gleixner 
1440500462a9SThomas Gleixner 		fn = timer->function;
1441500462a9SThomas Gleixner 
1442500462a9SThomas Gleixner 		if (timer->flags & TIMER_IRQSAFE) {
14432287d866SSebastian Andrzej Siewior 			raw_spin_unlock(&base->lock);
1444f28d3d53SAnna-Maria Gleixner 			call_timer_fn(timer, fn, baseclk);
1445030dcdd1SAnna-Maria Gleixner 			base->running_timer = NULL;
14462287d866SSebastian Andrzej Siewior 			raw_spin_lock(&base->lock);
14475cee9645SThomas Gleixner 		} else {
14482287d866SSebastian Andrzej Siewior 			raw_spin_unlock_irq(&base->lock);
1449f28d3d53SAnna-Maria Gleixner 			call_timer_fn(timer, fn, baseclk);
1450030dcdd1SAnna-Maria Gleixner 			base->running_timer = NULL;
1451030dcdd1SAnna-Maria Gleixner 			timer_sync_wait_running(base);
14522287d866SSebastian Andrzej Siewior 			raw_spin_lock_irq(&base->lock);
14535cee9645SThomas Gleixner 		}
14545cee9645SThomas Gleixner 	}
14555cee9645SThomas Gleixner }
1456500462a9SThomas Gleixner 
145723696838SAnna-Maria Gleixner static int __collect_expired_timers(struct timer_base *base,
1458500462a9SThomas Gleixner 				    struct hlist_head *heads)
1459500462a9SThomas Gleixner {
1460500462a9SThomas Gleixner 	unsigned long clk = base->clk;
1461500462a9SThomas Gleixner 	struct hlist_head *vec;
1462500462a9SThomas Gleixner 	int i, levels = 0;
1463500462a9SThomas Gleixner 	unsigned int idx;
1464500462a9SThomas Gleixner 
1465500462a9SThomas Gleixner 	for (i = 0; i < LVL_DEPTH; i++) {
1466500462a9SThomas Gleixner 		idx = (clk & LVL_MASK) + i * LVL_SIZE;
1467500462a9SThomas Gleixner 
1468500462a9SThomas Gleixner 		if (__test_and_clear_bit(idx, base->pending_map)) {
1469500462a9SThomas Gleixner 			vec = base->vectors + idx;
1470500462a9SThomas Gleixner 			hlist_move_list(vec, heads++);
1471500462a9SThomas Gleixner 			levels++;
1472500462a9SThomas Gleixner 		}
1473500462a9SThomas Gleixner 		/* Is it time to look at the next level? */
1474500462a9SThomas Gleixner 		if (clk & LVL_CLK_MASK)
1475500462a9SThomas Gleixner 			break;
1476500462a9SThomas Gleixner 		/* Shift clock for the next level granularity */
1477500462a9SThomas Gleixner 		clk >>= LVL_CLK_SHIFT;
1478500462a9SThomas Gleixner 	}
1479500462a9SThomas Gleixner 	return levels;
14805cee9645SThomas Gleixner }
14815cee9645SThomas Gleixner 
14825cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
14835cee9645SThomas Gleixner /*
148423696838SAnna-Maria Gleixner  * Find the next pending bucket of a level. Search from level start (@offset)
148523696838SAnna-Maria Gleixner  * + @clk upwards and if nothing there, search from start of the level
148623696838SAnna-Maria Gleixner  * (@offset) up to @offset + clk.
14875cee9645SThomas Gleixner  */
1488500462a9SThomas Gleixner static int next_pending_bucket(struct timer_base *base, unsigned offset,
1489500462a9SThomas Gleixner 			       unsigned clk)
14905cee9645SThomas Gleixner {
1491500462a9SThomas Gleixner 	unsigned pos, start = offset + clk;
1492500462a9SThomas Gleixner 	unsigned end = offset + LVL_SIZE;
14935cee9645SThomas Gleixner 
1494500462a9SThomas Gleixner 	pos = find_next_bit(base->pending_map, end, start);
1495500462a9SThomas Gleixner 	if (pos < end)
1496500462a9SThomas Gleixner 		return pos - start;
14975cee9645SThomas Gleixner 
1498500462a9SThomas Gleixner 	pos = find_next_bit(base->pending_map, start, offset);
1499500462a9SThomas Gleixner 	return pos < start ? pos + LVL_SIZE - start : -1;
15005cee9645SThomas Gleixner }
15015cee9645SThomas Gleixner 
1502500462a9SThomas Gleixner /*
150323696838SAnna-Maria Gleixner  * Search the first expiring timer in the various clock levels. Caller must
150423696838SAnna-Maria Gleixner  * hold base->lock.
15055cee9645SThomas Gleixner  */
1506494af3edSThomas Gleixner static unsigned long __next_timer_interrupt(struct timer_base *base)
15075cee9645SThomas Gleixner {
1508500462a9SThomas Gleixner 	unsigned long clk, next, adj;
1509500462a9SThomas Gleixner 	unsigned lvl, offset = 0;
15105cee9645SThomas Gleixner 
1511500462a9SThomas Gleixner 	next = base->clk + NEXT_TIMER_MAX_DELTA;
1512500462a9SThomas Gleixner 	clk = base->clk;
1513500462a9SThomas Gleixner 	for (lvl = 0; lvl < LVL_DEPTH; lvl++, offset += LVL_SIZE) {
1514500462a9SThomas Gleixner 		int pos = next_pending_bucket(base, offset, clk & LVL_MASK);
15155cee9645SThomas Gleixner 
1516500462a9SThomas Gleixner 		if (pos >= 0) {
1517500462a9SThomas Gleixner 			unsigned long tmp = clk + (unsigned long) pos;
15185cee9645SThomas Gleixner 
1519500462a9SThomas Gleixner 			tmp <<= LVL_SHIFT(lvl);
1520500462a9SThomas Gleixner 			if (time_before(tmp, next))
1521500462a9SThomas Gleixner 				next = tmp;
15225cee9645SThomas Gleixner 		}
15235cee9645SThomas Gleixner 		/*
1524500462a9SThomas Gleixner 		 * Clock for the next level. If the current level clock lower
1525500462a9SThomas Gleixner 		 * bits are zero, we look at the next level as is. If not we
1526500462a9SThomas Gleixner 		 * need to advance it by one because that's going to be the
1527500462a9SThomas Gleixner 		 * next expiring bucket in that level. base->clk is the next
1528500462a9SThomas Gleixner 		 * expiring jiffie. So in case of:
1529500462a9SThomas Gleixner 		 *
1530500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1531500462a9SThomas Gleixner 		 *  0    0    0    0    0    0
1532500462a9SThomas Gleixner 		 *
1533500462a9SThomas Gleixner 		 * we have to look at all levels @index 0. With
1534500462a9SThomas Gleixner 		 *
1535500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1536500462a9SThomas Gleixner 		 *  0    0    0    0    0    2
1537500462a9SThomas Gleixner 		 *
1538500462a9SThomas Gleixner 		 * LVL0 has the next expiring bucket @index 2. The upper
1539500462a9SThomas Gleixner 		 * levels have the next expiring bucket @index 1.
1540500462a9SThomas Gleixner 		 *
1541500462a9SThomas Gleixner 		 * In case that the propagation wraps the next level the same
1542500462a9SThomas Gleixner 		 * rules apply:
1543500462a9SThomas Gleixner 		 *
1544500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1545500462a9SThomas Gleixner 		 *  0    0    0    0    F    2
1546500462a9SThomas Gleixner 		 *
1547500462a9SThomas Gleixner 		 * So after looking at LVL0 we get:
1548500462a9SThomas Gleixner 		 *
1549500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1
1550500462a9SThomas Gleixner 		 *  0    0    0    1    0
1551500462a9SThomas Gleixner 		 *
1552500462a9SThomas Gleixner 		 * So no propagation from LVL1 to LVL2 because that happened
1553500462a9SThomas Gleixner 		 * with the add already, but then we need to propagate further
1554500462a9SThomas Gleixner 		 * from LVL2 to LVL3.
1555500462a9SThomas Gleixner 		 *
1556500462a9SThomas Gleixner 		 * So the simple check whether the lower bits of the current
1557500462a9SThomas Gleixner 		 * level are 0 or not is sufficient for all cases.
15585cee9645SThomas Gleixner 		 */
1559500462a9SThomas Gleixner 		adj = clk & LVL_CLK_MASK ? 1 : 0;
1560500462a9SThomas Gleixner 		clk >>= LVL_CLK_SHIFT;
1561500462a9SThomas Gleixner 		clk += adj;
15625cee9645SThomas Gleixner 	}
1563500462a9SThomas Gleixner 	return next;
15645cee9645SThomas Gleixner }
15655cee9645SThomas Gleixner 
15665cee9645SThomas Gleixner /*
15675cee9645SThomas Gleixner  * Check, if the next hrtimer event is before the next timer wheel
15685cee9645SThomas Gleixner  * event:
15695cee9645SThomas Gleixner  */
1570c1ad348bSThomas Gleixner static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
15715cee9645SThomas Gleixner {
1572c1ad348bSThomas Gleixner 	u64 nextevt = hrtimer_get_next_event();
15735cee9645SThomas Gleixner 
1574c1ad348bSThomas Gleixner 	/*
1575c1ad348bSThomas Gleixner 	 * If high resolution timers are enabled
1576c1ad348bSThomas Gleixner 	 * hrtimer_get_next_event() returns KTIME_MAX.
1577c1ad348bSThomas Gleixner 	 */
1578c1ad348bSThomas Gleixner 	if (expires <= nextevt)
15795cee9645SThomas Gleixner 		return expires;
15805cee9645SThomas Gleixner 
15815cee9645SThomas Gleixner 	/*
1582c1ad348bSThomas Gleixner 	 * If the next timer is already expired, return the tick base
1583c1ad348bSThomas Gleixner 	 * time so the tick is fired immediately.
15845cee9645SThomas Gleixner 	 */
1585c1ad348bSThomas Gleixner 	if (nextevt <= basem)
1586c1ad348bSThomas Gleixner 		return basem;
15875cee9645SThomas Gleixner 
15885cee9645SThomas Gleixner 	/*
1589c1ad348bSThomas Gleixner 	 * Round up to the next jiffie. High resolution timers are
1590c1ad348bSThomas Gleixner 	 * off, so the hrtimers are expired in the tick and we need to
1591c1ad348bSThomas Gleixner 	 * make sure that this tick really expires the timer to avoid
1592c1ad348bSThomas Gleixner 	 * a ping pong of the nohz stop code.
1593c1ad348bSThomas Gleixner 	 *
1594c1ad348bSThomas Gleixner 	 * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
15955cee9645SThomas Gleixner 	 */
1596c1ad348bSThomas Gleixner 	return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
15975cee9645SThomas Gleixner }
15985cee9645SThomas Gleixner 
15995cee9645SThomas Gleixner /**
1600c1ad348bSThomas Gleixner  * get_next_timer_interrupt - return the time (clock mono) of the next timer
1601c1ad348bSThomas Gleixner  * @basej:	base time jiffies
1602c1ad348bSThomas Gleixner  * @basem:	base time clock monotonic
1603c1ad348bSThomas Gleixner  *
1604c1ad348bSThomas Gleixner  * Returns the tick aligned clock monotonic time of the next pending
1605c1ad348bSThomas Gleixner  * timer or KTIME_MAX if no timer is pending.
16065cee9645SThomas Gleixner  */
1607c1ad348bSThomas Gleixner u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
16085cee9645SThomas Gleixner {
1609500462a9SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1610c1ad348bSThomas Gleixner 	u64 expires = KTIME_MAX;
1611c1ad348bSThomas Gleixner 	unsigned long nextevt;
161246c8f0b0SChris Metcalf 	bool is_max_delta;
16135cee9645SThomas Gleixner 
16145cee9645SThomas Gleixner 	/*
16155cee9645SThomas Gleixner 	 * Pretend that there is no timer pending if the cpu is offline.
16165cee9645SThomas Gleixner 	 * Possible pending timers will be migrated later to an active cpu.
16175cee9645SThomas Gleixner 	 */
16185cee9645SThomas Gleixner 	if (cpu_is_offline(smp_processor_id()))
16195cee9645SThomas Gleixner 		return expires;
16205cee9645SThomas Gleixner 
16212287d866SSebastian Andrzej Siewior 	raw_spin_lock(&base->lock);
1622500462a9SThomas Gleixner 	nextevt = __next_timer_interrupt(base);
162346c8f0b0SChris Metcalf 	is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA);
1624a683f390SThomas Gleixner 	base->next_expiry = nextevt;
1625a683f390SThomas Gleixner 	/*
1626041ad7bcSThomas Gleixner 	 * We have a fresh next event. Check whether we can forward the
1627041ad7bcSThomas Gleixner 	 * base. We can only do that when @basej is past base->clk
1628041ad7bcSThomas Gleixner 	 * otherwise we might rewind base->clk.
1629a683f390SThomas Gleixner 	 */
1630041ad7bcSThomas Gleixner 	if (time_after(basej, base->clk)) {
1631041ad7bcSThomas Gleixner 		if (time_after(nextevt, basej))
1632041ad7bcSThomas Gleixner 			base->clk = basej;
1633a683f390SThomas Gleixner 		else if (time_after(nextevt, base->clk))
1634a683f390SThomas Gleixner 			base->clk = nextevt;
1635041ad7bcSThomas Gleixner 	}
1636a683f390SThomas Gleixner 
1637a683f390SThomas Gleixner 	if (time_before_eq(nextevt, basej)) {
1638c1ad348bSThomas Gleixner 		expires = basem;
1639a683f390SThomas Gleixner 		base->is_idle = false;
1640a683f390SThomas Gleixner 	} else {
164146c8f0b0SChris Metcalf 		if (!is_max_delta)
164234f41c03SMatija Glavinic Pecotic 			expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
1643a683f390SThomas Gleixner 		/*
16442fe59f50SNicholas Piggin 		 * If we expect to sleep more than a tick, mark the base idle.
16452fe59f50SNicholas Piggin 		 * Also the tick is stopped so any added timer must forward
16462fe59f50SNicholas Piggin 		 * the base clk itself to keep granularity small. This idle
16472fe59f50SNicholas Piggin 		 * logic is only maintained for the BASE_STD base, deferrable
16482fe59f50SNicholas Piggin 		 * timers may still see large granularity skew (by design).
1649a683f390SThomas Gleixner 		 */
16502fe59f50SNicholas Piggin 		if ((expires - basem) > TICK_NSEC) {
16512fe59f50SNicholas Piggin 			base->must_forward_clk = true;
1652a683f390SThomas Gleixner 			base->is_idle = true;
16535cee9645SThomas Gleixner 		}
16542fe59f50SNicholas Piggin 	}
16552287d866SSebastian Andrzej Siewior 	raw_spin_unlock(&base->lock);
16565cee9645SThomas Gleixner 
1657c1ad348bSThomas Gleixner 	return cmp_next_hrtimer_event(basem, expires);
16585cee9645SThomas Gleixner }
165923696838SAnna-Maria Gleixner 
1660a683f390SThomas Gleixner /**
1661a683f390SThomas Gleixner  * timer_clear_idle - Clear the idle state of the timer base
1662a683f390SThomas Gleixner  *
1663a683f390SThomas Gleixner  * Called with interrupts disabled
1664a683f390SThomas Gleixner  */
1665a683f390SThomas Gleixner void timer_clear_idle(void)
1666a683f390SThomas Gleixner {
1667a683f390SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1668a683f390SThomas Gleixner 
1669a683f390SThomas Gleixner 	/*
1670a683f390SThomas Gleixner 	 * We do this unlocked. The worst outcome is a remote enqueue sending
1671a683f390SThomas Gleixner 	 * a pointless IPI, but taking the lock would just make the window for
1672a683f390SThomas Gleixner 	 * sending the IPI a few instructions smaller for the cost of taking
1673a683f390SThomas Gleixner 	 * the lock in the exit from idle path.
1674a683f390SThomas Gleixner 	 */
1675a683f390SThomas Gleixner 	base->is_idle = false;
1676a683f390SThomas Gleixner }
1677a683f390SThomas Gleixner 
167823696838SAnna-Maria Gleixner static int collect_expired_timers(struct timer_base *base,
167923696838SAnna-Maria Gleixner 				  struct hlist_head *heads)
168023696838SAnna-Maria Gleixner {
168123696838SAnna-Maria Gleixner 	/*
168223696838SAnna-Maria Gleixner 	 * NOHZ optimization. After a long idle sleep we need to forward the
168323696838SAnna-Maria Gleixner 	 * base to current jiffies. Avoid a loop by searching the bitfield for
168423696838SAnna-Maria Gleixner 	 * the next expiring timer.
168523696838SAnna-Maria Gleixner 	 */
168623696838SAnna-Maria Gleixner 	if ((long)(jiffies - base->clk) > 2) {
168723696838SAnna-Maria Gleixner 		unsigned long next = __next_timer_interrupt(base);
168823696838SAnna-Maria Gleixner 
168923696838SAnna-Maria Gleixner 		/*
169023696838SAnna-Maria Gleixner 		 * If the next timer is ahead of time forward to current
1691a683f390SThomas Gleixner 		 * jiffies, otherwise forward to the next expiry time:
169223696838SAnna-Maria Gleixner 		 */
169323696838SAnna-Maria Gleixner 		if (time_after(next, jiffies)) {
1694c310ce4dSZhenzhong Duan 			/*
1695c310ce4dSZhenzhong Duan 			 * The call site will increment base->clk and then
1696c310ce4dSZhenzhong Duan 			 * terminate the expiry loop immediately.
1697c310ce4dSZhenzhong Duan 			 */
1698c310ce4dSZhenzhong Duan 			base->clk = jiffies;
169923696838SAnna-Maria Gleixner 			return 0;
170023696838SAnna-Maria Gleixner 		}
170123696838SAnna-Maria Gleixner 		base->clk = next;
170223696838SAnna-Maria Gleixner 	}
170323696838SAnna-Maria Gleixner 	return __collect_expired_timers(base, heads);
170423696838SAnna-Maria Gleixner }
170523696838SAnna-Maria Gleixner #else
170623696838SAnna-Maria Gleixner static inline int collect_expired_timers(struct timer_base *base,
170723696838SAnna-Maria Gleixner 					 struct hlist_head *heads)
170823696838SAnna-Maria Gleixner {
170923696838SAnna-Maria Gleixner 	return __collect_expired_timers(base, heads);
171023696838SAnna-Maria Gleixner }
17115cee9645SThomas Gleixner #endif
17125cee9645SThomas Gleixner 
17135cee9645SThomas Gleixner /*
17145cee9645SThomas Gleixner  * Called from the timer interrupt handler to charge one tick to the current
17155cee9645SThomas Gleixner  * process.  user_tick is 1 if the tick is user time, 0 for system.
17165cee9645SThomas Gleixner  */
17175cee9645SThomas Gleixner void update_process_times(int user_tick)
17185cee9645SThomas Gleixner {
17195cee9645SThomas Gleixner 	struct task_struct *p = current;
17205cee9645SThomas Gleixner 
17215cee9645SThomas Gleixner 	/* Note: this timer irq context must be accounted for as well. */
17225cee9645SThomas Gleixner 	account_process_tick(p, user_tick);
17235cee9645SThomas Gleixner 	run_local_timers();
1724c98cac60SPaul E. McKenney 	rcu_sched_clock_irq(user_tick);
17255cee9645SThomas Gleixner #ifdef CONFIG_IRQ_WORK
17265cee9645SThomas Gleixner 	if (in_irq())
172776a33061SFrederic Weisbecker 		irq_work_tick();
17285cee9645SThomas Gleixner #endif
17295cee9645SThomas Gleixner 	scheduler_tick();
1730baa73d9eSNicolas Pitre 	if (IS_ENABLED(CONFIG_POSIX_TIMERS))
1731*dce3e8fdSThomas Gleixner 		run_posix_cpu_timers();
17325cee9645SThomas Gleixner }
17335cee9645SThomas Gleixner 
173473420feaSAnna-Maria Gleixner /**
173573420feaSAnna-Maria Gleixner  * __run_timers - run all expired timers (if any) on this CPU.
173673420feaSAnna-Maria Gleixner  * @base: the timer vector to be processed.
173773420feaSAnna-Maria Gleixner  */
173873420feaSAnna-Maria Gleixner static inline void __run_timers(struct timer_base *base)
173973420feaSAnna-Maria Gleixner {
174073420feaSAnna-Maria Gleixner 	struct hlist_head heads[LVL_DEPTH];
174173420feaSAnna-Maria Gleixner 	int levels;
174273420feaSAnna-Maria Gleixner 
174373420feaSAnna-Maria Gleixner 	if (!time_after_eq(jiffies, base->clk))
174473420feaSAnna-Maria Gleixner 		return;
174573420feaSAnna-Maria Gleixner 
1746030dcdd1SAnna-Maria Gleixner 	timer_base_lock_expiry(base);
17472287d866SSebastian Andrzej Siewior 	raw_spin_lock_irq(&base->lock);
174873420feaSAnna-Maria Gleixner 
1749363e934dSGaurav Kohli 	/*
1750363e934dSGaurav Kohli 	 * timer_base::must_forward_clk must be cleared before running
1751363e934dSGaurav Kohli 	 * timers so that any timer functions that call mod_timer() will
1752363e934dSGaurav Kohli 	 * not try to forward the base. Idle tracking / clock forwarding
1753363e934dSGaurav Kohli 	 * logic is only used with BASE_STD timers.
1754363e934dSGaurav Kohli 	 *
1755363e934dSGaurav Kohli 	 * The must_forward_clk flag is cleared unconditionally also for
1756363e934dSGaurav Kohli 	 * the deferrable base. The deferrable base is not affected by idle
1757363e934dSGaurav Kohli 	 * tracking and never forwarded, so clearing the flag is a NOOP.
1758363e934dSGaurav Kohli 	 *
1759363e934dSGaurav Kohli 	 * The fact that the deferrable base is never forwarded can cause
1760363e934dSGaurav Kohli 	 * large variations in granularity for deferrable timers, but they
1761363e934dSGaurav Kohli 	 * can be deferred for long periods due to idle anyway.
1762363e934dSGaurav Kohli 	 */
1763363e934dSGaurav Kohli 	base->must_forward_clk = false;
1764363e934dSGaurav Kohli 
176573420feaSAnna-Maria Gleixner 	while (time_after_eq(jiffies, base->clk)) {
176673420feaSAnna-Maria Gleixner 
176773420feaSAnna-Maria Gleixner 		levels = collect_expired_timers(base, heads);
176873420feaSAnna-Maria Gleixner 		base->clk++;
176973420feaSAnna-Maria Gleixner 
177073420feaSAnna-Maria Gleixner 		while (levels--)
177173420feaSAnna-Maria Gleixner 			expire_timers(base, heads + levels);
177273420feaSAnna-Maria Gleixner 	}
17732287d866SSebastian Andrzej Siewior 	raw_spin_unlock_irq(&base->lock);
1774030dcdd1SAnna-Maria Gleixner 	timer_base_unlock_expiry(base);
177573420feaSAnna-Maria Gleixner }
177673420feaSAnna-Maria Gleixner 
17775cee9645SThomas Gleixner /*
17785cee9645SThomas Gleixner  * This function runs timers and the timer-tq in bottom half context.
17795cee9645SThomas Gleixner  */
17800766f788SEmese Revfy static __latent_entropy void run_timer_softirq(struct softirq_action *h)
17815cee9645SThomas Gleixner {
1782500462a9SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
17835cee9645SThomas Gleixner 
17845cee9645SThomas Gleixner 	__run_timers(base);
1785ced6d5c1SAnna-Maria Gleixner 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON))
1786500462a9SThomas Gleixner 		__run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
17875cee9645SThomas Gleixner }
17885cee9645SThomas Gleixner 
17895cee9645SThomas Gleixner /*
17905cee9645SThomas Gleixner  * Called by the local, per-CPU timer interrupt on SMP.
17915cee9645SThomas Gleixner  */
17925cee9645SThomas Gleixner void run_local_timers(void)
17935cee9645SThomas Gleixner {
17944e85876aSThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
17954e85876aSThomas Gleixner 
17965cee9645SThomas Gleixner 	hrtimer_run_queues();
17974e85876aSThomas Gleixner 	/* Raise the softirq only if required. */
17984e85876aSThomas Gleixner 	if (time_before(jiffies, base->clk)) {
1799ed4bbf79SThomas Gleixner 		if (!IS_ENABLED(CONFIG_NO_HZ_COMMON))
18004e85876aSThomas Gleixner 			return;
18014e85876aSThomas Gleixner 		/* CPU is awake, so check the deferrable base. */
18024e85876aSThomas Gleixner 		base++;
18034e85876aSThomas Gleixner 		if (time_before(jiffies, base->clk))
18044e85876aSThomas Gleixner 			return;
18054e85876aSThomas Gleixner 	}
18065cee9645SThomas Gleixner 	raise_softirq(TIMER_SOFTIRQ);
18075cee9645SThomas Gleixner }
18085cee9645SThomas Gleixner 
180958e1177bSKees Cook /*
181058e1177bSKees Cook  * Since schedule_timeout()'s timer is defined on the stack, it must store
181158e1177bSKees Cook  * the target task on the stack as well.
181258e1177bSKees Cook  */
181358e1177bSKees Cook struct process_timer {
181458e1177bSKees Cook 	struct timer_list timer;
181558e1177bSKees Cook 	struct task_struct *task;
181658e1177bSKees Cook };
181758e1177bSKees Cook 
181858e1177bSKees Cook static void process_timeout(struct timer_list *t)
18195cee9645SThomas Gleixner {
182058e1177bSKees Cook 	struct process_timer *timeout = from_timer(timeout, t, timer);
182158e1177bSKees Cook 
182258e1177bSKees Cook 	wake_up_process(timeout->task);
18235cee9645SThomas Gleixner }
18245cee9645SThomas Gleixner 
18255cee9645SThomas Gleixner /**
18265cee9645SThomas Gleixner  * schedule_timeout - sleep until timeout
18275cee9645SThomas Gleixner  * @timeout: timeout value in jiffies
18285cee9645SThomas Gleixner  *
18295cee9645SThomas Gleixner  * Make the current task sleep until @timeout jiffies have
18305cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
18315cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
18325cee9645SThomas Gleixner  *
18335cee9645SThomas Gleixner  * You can set the task state as follows -
18345cee9645SThomas Gleixner  *
18355cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
18364b7e9cf9SDouglas Anderson  * pass before the routine returns unless the current task is explicitly
18374b7e9cf9SDouglas Anderson  * woken up, (e.g. by wake_up_process())".
18385cee9645SThomas Gleixner  *
18395cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
18404b7e9cf9SDouglas Anderson  * delivered to the current task or the current task is explicitly woken
18414b7e9cf9SDouglas Anderson  * up.
18425cee9645SThomas Gleixner  *
18435cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
18445cee9645SThomas Gleixner  * routine returns.
18455cee9645SThomas Gleixner  *
18465cee9645SThomas Gleixner  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
18475cee9645SThomas Gleixner  * the CPU away without a bound on the timeout. In this case the return
18485cee9645SThomas Gleixner  * value will be %MAX_SCHEDULE_TIMEOUT.
18495cee9645SThomas Gleixner  *
18504b7e9cf9SDouglas Anderson  * Returns 0 when the timer has expired otherwise the remaining time in
18514b7e9cf9SDouglas Anderson  * jiffies will be returned.  In all cases the return value is guaranteed
18524b7e9cf9SDouglas Anderson  * to be non-negative.
18535cee9645SThomas Gleixner  */
18545cee9645SThomas Gleixner signed long __sched schedule_timeout(signed long timeout)
18555cee9645SThomas Gleixner {
185658e1177bSKees Cook 	struct process_timer timer;
18575cee9645SThomas Gleixner 	unsigned long expire;
18585cee9645SThomas Gleixner 
18595cee9645SThomas Gleixner 	switch (timeout)
18605cee9645SThomas Gleixner 	{
18615cee9645SThomas Gleixner 	case MAX_SCHEDULE_TIMEOUT:
18625cee9645SThomas Gleixner 		/*
18635cee9645SThomas Gleixner 		 * These two special cases are useful to be comfortable
18645cee9645SThomas Gleixner 		 * in the caller. Nothing more. We could take
18655cee9645SThomas Gleixner 		 * MAX_SCHEDULE_TIMEOUT from one of the negative value
18665cee9645SThomas Gleixner 		 * but I' d like to return a valid offset (>=0) to allow
18675cee9645SThomas Gleixner 		 * the caller to do everything it want with the retval.
18685cee9645SThomas Gleixner 		 */
18695cee9645SThomas Gleixner 		schedule();
18705cee9645SThomas Gleixner 		goto out;
18715cee9645SThomas Gleixner 	default:
18725cee9645SThomas Gleixner 		/*
18735cee9645SThomas Gleixner 		 * Another bit of PARANOID. Note that the retval will be
18745cee9645SThomas Gleixner 		 * 0 since no piece of kernel is supposed to do a check
18755cee9645SThomas Gleixner 		 * for a negative retval of schedule_timeout() (since it
18765cee9645SThomas Gleixner 		 * should never happens anyway). You just have the printk()
18775cee9645SThomas Gleixner 		 * that will tell you if something is gone wrong and where.
18785cee9645SThomas Gleixner 		 */
18795cee9645SThomas Gleixner 		if (timeout < 0) {
18805cee9645SThomas Gleixner 			printk(KERN_ERR "schedule_timeout: wrong timeout "
18815cee9645SThomas Gleixner 				"value %lx\n", timeout);
18825cee9645SThomas Gleixner 			dump_stack();
18835cee9645SThomas Gleixner 			current->state = TASK_RUNNING;
18845cee9645SThomas Gleixner 			goto out;
18855cee9645SThomas Gleixner 		}
18865cee9645SThomas Gleixner 	}
18875cee9645SThomas Gleixner 
18885cee9645SThomas Gleixner 	expire = timeout + jiffies;
18895cee9645SThomas Gleixner 
189058e1177bSKees Cook 	timer.task = current;
189158e1177bSKees Cook 	timer_setup_on_stack(&timer.timer, process_timeout, 0);
1892b24591e2SDavid Howells 	__mod_timer(&timer.timer, expire, 0);
18935cee9645SThomas Gleixner 	schedule();
189458e1177bSKees Cook 	del_singleshot_timer_sync(&timer.timer);
18955cee9645SThomas Gleixner 
18965cee9645SThomas Gleixner 	/* Remove the timer from the object tracker */
189758e1177bSKees Cook 	destroy_timer_on_stack(&timer.timer);
18985cee9645SThomas Gleixner 
18995cee9645SThomas Gleixner 	timeout = expire - jiffies;
19005cee9645SThomas Gleixner 
19015cee9645SThomas Gleixner  out:
19025cee9645SThomas Gleixner 	return timeout < 0 ? 0 : timeout;
19035cee9645SThomas Gleixner }
19045cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout);
19055cee9645SThomas Gleixner 
19065cee9645SThomas Gleixner /*
19075cee9645SThomas Gleixner  * We can use __set_current_state() here because schedule_timeout() calls
19085cee9645SThomas Gleixner  * schedule() unconditionally.
19095cee9645SThomas Gleixner  */
19105cee9645SThomas Gleixner signed long __sched schedule_timeout_interruptible(signed long timeout)
19115cee9645SThomas Gleixner {
19125cee9645SThomas Gleixner 	__set_current_state(TASK_INTERRUPTIBLE);
19135cee9645SThomas Gleixner 	return schedule_timeout(timeout);
19145cee9645SThomas Gleixner }
19155cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_interruptible);
19165cee9645SThomas Gleixner 
19175cee9645SThomas Gleixner signed long __sched schedule_timeout_killable(signed long timeout)
19185cee9645SThomas Gleixner {
19195cee9645SThomas Gleixner 	__set_current_state(TASK_KILLABLE);
19205cee9645SThomas Gleixner 	return schedule_timeout(timeout);
19215cee9645SThomas Gleixner }
19225cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_killable);
19235cee9645SThomas Gleixner 
19245cee9645SThomas Gleixner signed long __sched schedule_timeout_uninterruptible(signed long timeout)
19255cee9645SThomas Gleixner {
19265cee9645SThomas Gleixner 	__set_current_state(TASK_UNINTERRUPTIBLE);
19275cee9645SThomas Gleixner 	return schedule_timeout(timeout);
19285cee9645SThomas Gleixner }
19295cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_uninterruptible);
19305cee9645SThomas Gleixner 
193169b27bafSAndrew Morton /*
193269b27bafSAndrew Morton  * Like schedule_timeout_uninterruptible(), except this task will not contribute
193369b27bafSAndrew Morton  * to load average.
193469b27bafSAndrew Morton  */
193569b27bafSAndrew Morton signed long __sched schedule_timeout_idle(signed long timeout)
193669b27bafSAndrew Morton {
193769b27bafSAndrew Morton 	__set_current_state(TASK_IDLE);
193869b27bafSAndrew Morton 	return schedule_timeout(timeout);
193969b27bafSAndrew Morton }
194069b27bafSAndrew Morton EXPORT_SYMBOL(schedule_timeout_idle);
194169b27bafSAndrew Morton 
19425cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
1943494af3edSThomas Gleixner static void migrate_timer_list(struct timer_base *new_base, struct hlist_head *head)
19445cee9645SThomas Gleixner {
19455cee9645SThomas Gleixner 	struct timer_list *timer;
19460eeda71bSThomas Gleixner 	int cpu = new_base->cpu;
19475cee9645SThomas Gleixner 
19481dabbcecSThomas Gleixner 	while (!hlist_empty(head)) {
19491dabbcecSThomas Gleixner 		timer = hlist_entry(head->first, struct timer_list, entry);
19505cee9645SThomas Gleixner 		detach_timer(timer, false);
19510eeda71bSThomas Gleixner 		timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
19525cee9645SThomas Gleixner 		internal_add_timer(new_base, timer);
19535cee9645SThomas Gleixner 	}
19545cee9645SThomas Gleixner }
19555cee9645SThomas Gleixner 
195626456f87SThomas Gleixner int timers_prepare_cpu(unsigned int cpu)
195726456f87SThomas Gleixner {
195826456f87SThomas Gleixner 	struct timer_base *base;
195926456f87SThomas Gleixner 	int b;
196026456f87SThomas Gleixner 
196126456f87SThomas Gleixner 	for (b = 0; b < NR_BASES; b++) {
196226456f87SThomas Gleixner 		base = per_cpu_ptr(&timer_bases[b], cpu);
196326456f87SThomas Gleixner 		base->clk = jiffies;
196426456f87SThomas Gleixner 		base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA;
196526456f87SThomas Gleixner 		base->is_idle = false;
196626456f87SThomas Gleixner 		base->must_forward_clk = true;
196726456f87SThomas Gleixner 	}
196826456f87SThomas Gleixner 	return 0;
196926456f87SThomas Gleixner }
197026456f87SThomas Gleixner 
197124f73b99SRichard Cochran int timers_dead_cpu(unsigned int cpu)
19725cee9645SThomas Gleixner {
1973494af3edSThomas Gleixner 	struct timer_base *old_base;
1974494af3edSThomas Gleixner 	struct timer_base *new_base;
1975500462a9SThomas Gleixner 	int b, i;
19765cee9645SThomas Gleixner 
19775cee9645SThomas Gleixner 	BUG_ON(cpu_online(cpu));
1978500462a9SThomas Gleixner 
1979500462a9SThomas Gleixner 	for (b = 0; b < NR_BASES; b++) {
1980500462a9SThomas Gleixner 		old_base = per_cpu_ptr(&timer_bases[b], cpu);
1981500462a9SThomas Gleixner 		new_base = get_cpu_ptr(&timer_bases[b]);
19825cee9645SThomas Gleixner 		/*
19835cee9645SThomas Gleixner 		 * The caller is globally serialized and nobody else
19845cee9645SThomas Gleixner 		 * takes two locks at once, deadlock is not possible.
19855cee9645SThomas Gleixner 		 */
19862287d866SSebastian Andrzej Siewior 		raw_spin_lock_irq(&new_base->lock);
19872287d866SSebastian Andrzej Siewior 		raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
19885cee9645SThomas Gleixner 
1989c52232a4SLingutla Chandrasekhar 		/*
1990c52232a4SLingutla Chandrasekhar 		 * The current CPUs base clock might be stale. Update it
1991c52232a4SLingutla Chandrasekhar 		 * before moving the timers over.
1992c52232a4SLingutla Chandrasekhar 		 */
1993c52232a4SLingutla Chandrasekhar 		forward_timer_base(new_base);
1994c52232a4SLingutla Chandrasekhar 
19955cee9645SThomas Gleixner 		BUG_ON(old_base->running_timer);
19965cee9645SThomas Gleixner 
1997500462a9SThomas Gleixner 		for (i = 0; i < WHEEL_SIZE; i++)
1998500462a9SThomas Gleixner 			migrate_timer_list(new_base, old_base->vectors + i);
19998def9060SViresh Kumar 
20002287d866SSebastian Andrzej Siewior 		raw_spin_unlock(&old_base->lock);
20012287d866SSebastian Andrzej Siewior 		raw_spin_unlock_irq(&new_base->lock);
2002494af3edSThomas Gleixner 		put_cpu_ptr(&timer_bases);
20035cee9645SThomas Gleixner 	}
200424f73b99SRichard Cochran 	return 0;
20055cee9645SThomas Gleixner }
20065cee9645SThomas Gleixner 
20073650b57fSPeter Zijlstra #endif /* CONFIG_HOTPLUG_CPU */
20085cee9645SThomas Gleixner 
20090eeda71bSThomas Gleixner static void __init init_timer_cpu(int cpu)
20108def9060SViresh Kumar {
2011500462a9SThomas Gleixner 	struct timer_base *base;
2012500462a9SThomas Gleixner 	int i;
20133650b57fSPeter Zijlstra 
2014500462a9SThomas Gleixner 	for (i = 0; i < NR_BASES; i++) {
2015500462a9SThomas Gleixner 		base = per_cpu_ptr(&timer_bases[i], cpu);
20168def9060SViresh Kumar 		base->cpu = cpu;
20172287d866SSebastian Andrzej Siewior 		raw_spin_lock_init(&base->lock);
2018494af3edSThomas Gleixner 		base->clk = jiffies;
2019030dcdd1SAnna-Maria Gleixner 		timer_base_init_expiry_lock(base);
2020500462a9SThomas Gleixner 	}
20218def9060SViresh Kumar }
20228def9060SViresh Kumar 
20238def9060SViresh Kumar static void __init init_timer_cpus(void)
20248def9060SViresh Kumar {
20258def9060SViresh Kumar 	int cpu;
20268def9060SViresh Kumar 
20270eeda71bSThomas Gleixner 	for_each_possible_cpu(cpu)
20280eeda71bSThomas Gleixner 		init_timer_cpu(cpu);
20298def9060SViresh Kumar }
20305cee9645SThomas Gleixner 
20315cee9645SThomas Gleixner void __init init_timers(void)
20325cee9645SThomas Gleixner {
20338def9060SViresh Kumar 	init_timer_cpus();
20345cee9645SThomas Gleixner 	open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
20355cee9645SThomas Gleixner }
20365cee9645SThomas Gleixner 
20375cee9645SThomas Gleixner /**
20385cee9645SThomas Gleixner  * msleep - sleep safely even with waitqueue interruptions
20395cee9645SThomas Gleixner  * @msecs: Time in milliseconds to sleep for
20405cee9645SThomas Gleixner  */
20415cee9645SThomas Gleixner void msleep(unsigned int msecs)
20425cee9645SThomas Gleixner {
20435cee9645SThomas Gleixner 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
20445cee9645SThomas Gleixner 
20455cee9645SThomas Gleixner 	while (timeout)
20465cee9645SThomas Gleixner 		timeout = schedule_timeout_uninterruptible(timeout);
20475cee9645SThomas Gleixner }
20485cee9645SThomas Gleixner 
20495cee9645SThomas Gleixner EXPORT_SYMBOL(msleep);
20505cee9645SThomas Gleixner 
20515cee9645SThomas Gleixner /**
20525cee9645SThomas Gleixner  * msleep_interruptible - sleep waiting for signals
20535cee9645SThomas Gleixner  * @msecs: Time in milliseconds to sleep for
20545cee9645SThomas Gleixner  */
20555cee9645SThomas Gleixner unsigned long msleep_interruptible(unsigned int msecs)
20565cee9645SThomas Gleixner {
20575cee9645SThomas Gleixner 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
20585cee9645SThomas Gleixner 
20595cee9645SThomas Gleixner 	while (timeout && !signal_pending(current))
20605cee9645SThomas Gleixner 		timeout = schedule_timeout_interruptible(timeout);
20615cee9645SThomas Gleixner 	return jiffies_to_msecs(timeout);
20625cee9645SThomas Gleixner }
20635cee9645SThomas Gleixner 
20645cee9645SThomas Gleixner EXPORT_SYMBOL(msleep_interruptible);
20655cee9645SThomas Gleixner 
20665cee9645SThomas Gleixner /**
2067b5227d03SBjorn Helgaas  * usleep_range - Sleep for an approximate time
20685cee9645SThomas Gleixner  * @min: Minimum time in usecs to sleep
20695cee9645SThomas Gleixner  * @max: Maximum time in usecs to sleep
2070b5227d03SBjorn Helgaas  *
2071b5227d03SBjorn Helgaas  * In non-atomic context where the exact wakeup time is flexible, use
2072b5227d03SBjorn Helgaas  * usleep_range() instead of udelay().  The sleep improves responsiveness
2073b5227d03SBjorn Helgaas  * by avoiding the CPU-hogging busy-wait of udelay(), and the range reduces
2074b5227d03SBjorn Helgaas  * power usage by allowing hrtimers to take advantage of an already-
2075b5227d03SBjorn Helgaas  * scheduled interrupt instead of scheduling a new one just for this sleep.
20765cee9645SThomas Gleixner  */
20772ad5d327SThomas Gleixner void __sched usleep_range(unsigned long min, unsigned long max)
20785cee9645SThomas Gleixner {
20796c5e9059SDouglas Anderson 	ktime_t exp = ktime_add_us(ktime_get(), min);
20806c5e9059SDouglas Anderson 	u64 delta = (u64)(max - min) * NSEC_PER_USEC;
20816c5e9059SDouglas Anderson 
20826c5e9059SDouglas Anderson 	for (;;) {
20835cee9645SThomas Gleixner 		__set_current_state(TASK_UNINTERRUPTIBLE);
20846c5e9059SDouglas Anderson 		/* Do not return before the requested sleep time has elapsed */
20856c5e9059SDouglas Anderson 		if (!schedule_hrtimeout_range(&exp, delta, HRTIMER_MODE_ABS))
20866c5e9059SDouglas Anderson 			break;
20876c5e9059SDouglas Anderson 	}
20885cee9645SThomas Gleixner }
20895cee9645SThomas Gleixner EXPORT_SYMBOL(usleep_range);
2090