xref: /linux-6.15/kernel/time/timer.c (revision ffdf0477)
15cee9645SThomas Gleixner /*
25cee9645SThomas Gleixner  *  linux/kernel/timer.c
35cee9645SThomas Gleixner  *
45cee9645SThomas Gleixner  *  Kernel internal timers
55cee9645SThomas Gleixner  *
65cee9645SThomas Gleixner  *  Copyright (C) 1991, 1992  Linus Torvalds
75cee9645SThomas Gleixner  *
85cee9645SThomas Gleixner  *  1997-01-28  Modified by Finn Arne Gangstad to make timers scale better.
95cee9645SThomas Gleixner  *
105cee9645SThomas Gleixner  *  1997-09-10  Updated NTP code according to technical memorandum Jan '96
115cee9645SThomas Gleixner  *              "A Kernel Model for Precision Timekeeping" by Dave Mills
125cee9645SThomas Gleixner  *  1998-12-24  Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
135cee9645SThomas Gleixner  *              serialize accesses to xtime/lost_ticks).
145cee9645SThomas Gleixner  *                              Copyright (C) 1998  Andrea Arcangeli
155cee9645SThomas Gleixner  *  1999-03-10  Improved NTP compatibility by Ulrich Windl
165cee9645SThomas Gleixner  *  2002-05-31	Move sys_sysinfo here and make its locking sane, Robert Love
175cee9645SThomas Gleixner  *  2000-10-05  Implemented scalable SMP per-CPU timer handling.
185cee9645SThomas Gleixner  *                              Copyright (C) 2000, 2001, 2002  Ingo Molnar
195cee9645SThomas Gleixner  *              Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
205cee9645SThomas Gleixner  */
215cee9645SThomas Gleixner 
225cee9645SThomas Gleixner #include <linux/kernel_stat.h>
235cee9645SThomas Gleixner #include <linux/export.h>
245cee9645SThomas Gleixner #include <linux/interrupt.h>
255cee9645SThomas Gleixner #include <linux/percpu.h>
265cee9645SThomas Gleixner #include <linux/init.h>
275cee9645SThomas Gleixner #include <linux/mm.h>
285cee9645SThomas Gleixner #include <linux/swap.h>
295cee9645SThomas Gleixner #include <linux/pid_namespace.h>
305cee9645SThomas Gleixner #include <linux/notifier.h>
315cee9645SThomas Gleixner #include <linux/thread_info.h>
325cee9645SThomas Gleixner #include <linux/time.h>
335cee9645SThomas Gleixner #include <linux/jiffies.h>
345cee9645SThomas Gleixner #include <linux/posix-timers.h>
355cee9645SThomas Gleixner #include <linux/cpu.h>
365cee9645SThomas Gleixner #include <linux/syscalls.h>
375cee9645SThomas Gleixner #include <linux/delay.h>
385cee9645SThomas Gleixner #include <linux/tick.h>
395cee9645SThomas Gleixner #include <linux/kallsyms.h>
405cee9645SThomas Gleixner #include <linux/irq_work.h>
415cee9645SThomas Gleixner #include <linux/sched.h>
425cee9645SThomas Gleixner #include <linux/sched/sysctl.h>
435cee9645SThomas Gleixner #include <linux/slab.h>
445cee9645SThomas Gleixner #include <linux/compat.h>
455cee9645SThomas Gleixner 
465cee9645SThomas Gleixner #include <asm/uaccess.h>
475cee9645SThomas Gleixner #include <asm/unistd.h>
485cee9645SThomas Gleixner #include <asm/div64.h>
495cee9645SThomas Gleixner #include <asm/timex.h>
505cee9645SThomas Gleixner #include <asm/io.h>
515cee9645SThomas Gleixner 
52c1ad348bSThomas Gleixner #include "tick-internal.h"
53c1ad348bSThomas Gleixner 
545cee9645SThomas Gleixner #define CREATE_TRACE_POINTS
555cee9645SThomas Gleixner #include <trace/events/timer.h>
565cee9645SThomas Gleixner 
575cee9645SThomas Gleixner __visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
585cee9645SThomas Gleixner 
595cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64);
605cee9645SThomas Gleixner 
615cee9645SThomas Gleixner /*
62500462a9SThomas Gleixner  * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
63500462a9SThomas Gleixner  * LVL_SIZE buckets. Each level is driven by its own clock and therefor each
64500462a9SThomas Gleixner  * level has a different granularity.
65500462a9SThomas Gleixner  *
66500462a9SThomas Gleixner  * The level granularity is:		LVL_CLK_DIV ^ lvl
67500462a9SThomas Gleixner  * The level clock frequency is:	HZ / (LVL_CLK_DIV ^ level)
68500462a9SThomas Gleixner  *
69500462a9SThomas Gleixner  * The array level of a newly armed timer depends on the relative expiry
70500462a9SThomas Gleixner  * time. The farther the expiry time is away the higher the array level and
71500462a9SThomas Gleixner  * therefor the granularity becomes.
72500462a9SThomas Gleixner  *
73500462a9SThomas Gleixner  * Contrary to the original timer wheel implementation, which aims for 'exact'
74500462a9SThomas Gleixner  * expiry of the timers, this implementation removes the need for recascading
75500462a9SThomas Gleixner  * the timers into the lower array levels. The previous 'classic' timer wheel
76500462a9SThomas Gleixner  * implementation of the kernel already violated the 'exact' expiry by adding
77500462a9SThomas Gleixner  * slack to the expiry time to provide batched expiration. The granularity
78500462a9SThomas Gleixner  * levels provide implicit batching.
79500462a9SThomas Gleixner  *
80500462a9SThomas Gleixner  * This is an optimization of the original timer wheel implementation for the
81500462a9SThomas Gleixner  * majority of the timer wheel use cases: timeouts. The vast majority of
82500462a9SThomas Gleixner  * timeout timers (networking, disk I/O ...) are canceled before expiry. If
83500462a9SThomas Gleixner  * the timeout expires it indicates that normal operation is disturbed, so it
84500462a9SThomas Gleixner  * does not matter much whether the timeout comes with a slight delay.
85500462a9SThomas Gleixner  *
86500462a9SThomas Gleixner  * The only exception to this are networking timers with a small expiry
87500462a9SThomas Gleixner  * time. They rely on the granularity. Those fit into the first wheel level,
88500462a9SThomas Gleixner  * which has HZ granularity.
89500462a9SThomas Gleixner  *
90500462a9SThomas Gleixner  * We don't have cascading anymore. timers with a expiry time above the
91500462a9SThomas Gleixner  * capacity of the last wheel level are force expired at the maximum timeout
92500462a9SThomas Gleixner  * value of the last wheel level. From data sampling we know that the maximum
93500462a9SThomas Gleixner  * value observed is 5 days (network connection tracking), so this should not
94500462a9SThomas Gleixner  * be an issue.
95500462a9SThomas Gleixner  *
96500462a9SThomas Gleixner  * The currently chosen array constants values are a good compromise between
97500462a9SThomas Gleixner  * array size and granularity.
98500462a9SThomas Gleixner  *
99500462a9SThomas Gleixner  * This results in the following granularity and range levels:
100500462a9SThomas Gleixner  *
101500462a9SThomas Gleixner  * HZ 1000 steps
102500462a9SThomas Gleixner  * Level Offset  Granularity            Range
103500462a9SThomas Gleixner  *  0      0         1 ms                0 ms -         63 ms
104500462a9SThomas Gleixner  *  1     64         8 ms               64 ms -        511 ms
105500462a9SThomas Gleixner  *  2    128        64 ms              512 ms -       4095 ms (512ms - ~4s)
106500462a9SThomas Gleixner  *  3    192       512 ms             4096 ms -      32767 ms (~4s - ~32s)
107500462a9SThomas Gleixner  *  4    256      4096 ms (~4s)      32768 ms -     262143 ms (~32s - ~4m)
108500462a9SThomas Gleixner  *  5    320     32768 ms (~32s)    262144 ms -    2097151 ms (~4m - ~34m)
109500462a9SThomas Gleixner  *  6    384    262144 ms (~4m)    2097152 ms -   16777215 ms (~34m - ~4h)
110500462a9SThomas Gleixner  *  7    448   2097152 ms (~34m)  16777216 ms -  134217727 ms (~4h - ~1d)
111500462a9SThomas Gleixner  *  8    512  16777216 ms (~4h)  134217728 ms - 1073741822 ms (~1d - ~12d)
112500462a9SThomas Gleixner  *
113500462a9SThomas Gleixner  * HZ  300
114500462a9SThomas Gleixner  * Level Offset  Granularity            Range
115500462a9SThomas Gleixner  *  0	   0         3 ms                0 ms -        210 ms
116500462a9SThomas Gleixner  *  1	  64        26 ms              213 ms -       1703 ms (213ms - ~1s)
117500462a9SThomas Gleixner  *  2	 128       213 ms             1706 ms -      13650 ms (~1s - ~13s)
118500462a9SThomas Gleixner  *  3	 192      1706 ms (~1s)      13653 ms -     109223 ms (~13s - ~1m)
119500462a9SThomas Gleixner  *  4	 256     13653 ms (~13s)    109226 ms -     873810 ms (~1m - ~14m)
120500462a9SThomas Gleixner  *  5	 320    109226 ms (~1m)     873813 ms -    6990503 ms (~14m - ~1h)
121500462a9SThomas Gleixner  *  6	 384    873813 ms (~14m)   6990506 ms -   55924050 ms (~1h - ~15h)
122500462a9SThomas Gleixner  *  7	 448   6990506 ms (~1h)   55924053 ms -  447392423 ms (~15h - ~5d)
123500462a9SThomas Gleixner  *  8    512  55924053 ms (~15h) 447392426 ms - 3579139406 ms (~5d - ~41d)
124500462a9SThomas Gleixner  *
125500462a9SThomas Gleixner  * HZ  250
126500462a9SThomas Gleixner  * Level Offset  Granularity            Range
127500462a9SThomas Gleixner  *  0	   0         4 ms                0 ms -        255 ms
128500462a9SThomas Gleixner  *  1	  64        32 ms              256 ms -       2047 ms (256ms - ~2s)
129500462a9SThomas Gleixner  *  2	 128       256 ms             2048 ms -      16383 ms (~2s - ~16s)
130500462a9SThomas Gleixner  *  3	 192      2048 ms (~2s)      16384 ms -     131071 ms (~16s - ~2m)
131500462a9SThomas Gleixner  *  4	 256     16384 ms (~16s)    131072 ms -    1048575 ms (~2m - ~17m)
132500462a9SThomas Gleixner  *  5	 320    131072 ms (~2m)    1048576 ms -    8388607 ms (~17m - ~2h)
133500462a9SThomas Gleixner  *  6	 384   1048576 ms (~17m)   8388608 ms -   67108863 ms (~2h - ~18h)
134500462a9SThomas Gleixner  *  7	 448   8388608 ms (~2h)   67108864 ms -  536870911 ms (~18h - ~6d)
135500462a9SThomas Gleixner  *  8    512  67108864 ms (~18h) 536870912 ms - 4294967288 ms (~6d - ~49d)
136500462a9SThomas Gleixner  *
137500462a9SThomas Gleixner  * HZ  100
138500462a9SThomas Gleixner  * Level Offset  Granularity            Range
139500462a9SThomas Gleixner  *  0	   0         10 ms               0 ms -        630 ms
140500462a9SThomas Gleixner  *  1	  64         80 ms             640 ms -       5110 ms (640ms - ~5s)
141500462a9SThomas Gleixner  *  2	 128        640 ms            5120 ms -      40950 ms (~5s - ~40s)
142500462a9SThomas Gleixner  *  3	 192       5120 ms (~5s)     40960 ms -     327670 ms (~40s - ~5m)
143500462a9SThomas Gleixner  *  4	 256      40960 ms (~40s)   327680 ms -    2621430 ms (~5m - ~43m)
144500462a9SThomas Gleixner  *  5	 320     327680 ms (~5m)   2621440 ms -   20971510 ms (~43m - ~5h)
145500462a9SThomas Gleixner  *  6	 384    2621440 ms (~43m) 20971520 ms -  167772150 ms (~5h - ~1d)
146500462a9SThomas Gleixner  *  7	 448   20971520 ms (~5h) 167772160 ms - 1342177270 ms (~1d - ~15d)
1475cee9645SThomas Gleixner  */
1485cee9645SThomas Gleixner 
149500462a9SThomas Gleixner /* Clock divisor for the next level */
150500462a9SThomas Gleixner #define LVL_CLK_SHIFT	3
151500462a9SThomas Gleixner #define LVL_CLK_DIV	(1UL << LVL_CLK_SHIFT)
152500462a9SThomas Gleixner #define LVL_CLK_MASK	(LVL_CLK_DIV - 1)
153500462a9SThomas Gleixner #define LVL_SHIFT(n)	((n) * LVL_CLK_SHIFT)
154500462a9SThomas Gleixner #define LVL_GRAN(n)	(1UL << LVL_SHIFT(n))
1555cee9645SThomas Gleixner 
156500462a9SThomas Gleixner /*
157500462a9SThomas Gleixner  * The time start value for each level to select the bucket at enqueue
158500462a9SThomas Gleixner  * time.
159500462a9SThomas Gleixner  */
160500462a9SThomas Gleixner #define LVL_START(n)	((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
161500462a9SThomas Gleixner 
162500462a9SThomas Gleixner /* Size of each clock level */
163500462a9SThomas Gleixner #define LVL_BITS	6
164500462a9SThomas Gleixner #define LVL_SIZE	(1UL << LVL_BITS)
165500462a9SThomas Gleixner #define LVL_MASK	(LVL_SIZE - 1)
166500462a9SThomas Gleixner #define LVL_OFFS(n)	((n) * LVL_SIZE)
167500462a9SThomas Gleixner 
168500462a9SThomas Gleixner /* Level depth */
169500462a9SThomas Gleixner #if HZ > 100
170500462a9SThomas Gleixner # define LVL_DEPTH	9
171500462a9SThomas Gleixner # else
172500462a9SThomas Gleixner # define LVL_DEPTH	8
173500462a9SThomas Gleixner #endif
174500462a9SThomas Gleixner 
175500462a9SThomas Gleixner /* The cutoff (max. capacity of the wheel) */
176500462a9SThomas Gleixner #define WHEEL_TIMEOUT_CUTOFF	(LVL_START(LVL_DEPTH))
177500462a9SThomas Gleixner #define WHEEL_TIMEOUT_MAX	(WHEEL_TIMEOUT_CUTOFF - LVL_GRAN(LVL_DEPTH - 1))
178500462a9SThomas Gleixner 
179500462a9SThomas Gleixner /*
180500462a9SThomas Gleixner  * The resulting wheel size. If NOHZ is configured we allocate two
181500462a9SThomas Gleixner  * wheels so we have a separate storage for the deferrable timers.
182500462a9SThomas Gleixner  */
183500462a9SThomas Gleixner #define WHEEL_SIZE	(LVL_SIZE * LVL_DEPTH)
184500462a9SThomas Gleixner 
185500462a9SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
186500462a9SThomas Gleixner # define NR_BASES	2
187500462a9SThomas Gleixner # define BASE_STD	0
188500462a9SThomas Gleixner # define BASE_DEF	1
189500462a9SThomas Gleixner #else
190500462a9SThomas Gleixner # define NR_BASES	1
191500462a9SThomas Gleixner # define BASE_STD	0
192500462a9SThomas Gleixner # define BASE_DEF	0
193500462a9SThomas Gleixner #endif
1945cee9645SThomas Gleixner 
195494af3edSThomas Gleixner struct timer_base {
1965cee9645SThomas Gleixner 	spinlock_t		lock;
1975cee9645SThomas Gleixner 	struct timer_list	*running_timer;
198494af3edSThomas Gleixner 	unsigned long		clk;
199a683f390SThomas Gleixner 	unsigned long		next_expiry;
200500462a9SThomas Gleixner 	unsigned int		cpu;
201bc7a34b8SThomas Gleixner 	bool			migration_enabled;
202683be13aSThomas Gleixner 	bool			nohz_active;
203a683f390SThomas Gleixner 	bool			is_idle;
204500462a9SThomas Gleixner 	DECLARE_BITMAP(pending_map, WHEEL_SIZE);
205500462a9SThomas Gleixner 	struct hlist_head	vectors[WHEEL_SIZE];
2065cee9645SThomas Gleixner } ____cacheline_aligned;
2075cee9645SThomas Gleixner 
208500462a9SThomas Gleixner static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
2095cee9645SThomas Gleixner 
210bc7a34b8SThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
211bc7a34b8SThomas Gleixner unsigned int sysctl_timer_migration = 1;
212bc7a34b8SThomas Gleixner 
213683be13aSThomas Gleixner void timers_update_migration(bool update_nohz)
214bc7a34b8SThomas Gleixner {
215bc7a34b8SThomas Gleixner 	bool on = sysctl_timer_migration && tick_nohz_active;
216bc7a34b8SThomas Gleixner 	unsigned int cpu;
217bc7a34b8SThomas Gleixner 
218bc7a34b8SThomas Gleixner 	/* Avoid the loop, if nothing to update */
219500462a9SThomas Gleixner 	if (this_cpu_read(timer_bases[BASE_STD].migration_enabled) == on)
220bc7a34b8SThomas Gleixner 		return;
221bc7a34b8SThomas Gleixner 
222bc7a34b8SThomas Gleixner 	for_each_possible_cpu(cpu) {
223500462a9SThomas Gleixner 		per_cpu(timer_bases[BASE_STD].migration_enabled, cpu) = on;
224500462a9SThomas Gleixner 		per_cpu(timer_bases[BASE_DEF].migration_enabled, cpu) = on;
225bc7a34b8SThomas Gleixner 		per_cpu(hrtimer_bases.migration_enabled, cpu) = on;
226683be13aSThomas Gleixner 		if (!update_nohz)
227683be13aSThomas Gleixner 			continue;
228500462a9SThomas Gleixner 		per_cpu(timer_bases[BASE_STD].nohz_active, cpu) = true;
229500462a9SThomas Gleixner 		per_cpu(timer_bases[BASE_DEF].nohz_active, cpu) = true;
230683be13aSThomas Gleixner 		per_cpu(hrtimer_bases.nohz_active, cpu) = true;
231bc7a34b8SThomas Gleixner 	}
232bc7a34b8SThomas Gleixner }
233bc7a34b8SThomas Gleixner 
234bc7a34b8SThomas Gleixner int timer_migration_handler(struct ctl_table *table, int write,
235bc7a34b8SThomas Gleixner 			    void __user *buffer, size_t *lenp,
236bc7a34b8SThomas Gleixner 			    loff_t *ppos)
237bc7a34b8SThomas Gleixner {
238bc7a34b8SThomas Gleixner 	static DEFINE_MUTEX(mutex);
239bc7a34b8SThomas Gleixner 	int ret;
240bc7a34b8SThomas Gleixner 
241bc7a34b8SThomas Gleixner 	mutex_lock(&mutex);
242bc7a34b8SThomas Gleixner 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
243bc7a34b8SThomas Gleixner 	if (!ret && write)
244683be13aSThomas Gleixner 		timers_update_migration(false);
245bc7a34b8SThomas Gleixner 	mutex_unlock(&mutex);
246bc7a34b8SThomas Gleixner 	return ret;
247bc7a34b8SThomas Gleixner }
248bc7a34b8SThomas Gleixner #endif
249bc7a34b8SThomas Gleixner 
2505cee9645SThomas Gleixner static unsigned long round_jiffies_common(unsigned long j, int cpu,
2515cee9645SThomas Gleixner 		bool force_up)
2525cee9645SThomas Gleixner {
2535cee9645SThomas Gleixner 	int rem;
2545cee9645SThomas Gleixner 	unsigned long original = j;
2555cee9645SThomas Gleixner 
2565cee9645SThomas Gleixner 	/*
2575cee9645SThomas Gleixner 	 * We don't want all cpus firing their timers at once hitting the
2585cee9645SThomas Gleixner 	 * same lock or cachelines, so we skew each extra cpu with an extra
2595cee9645SThomas Gleixner 	 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
2605cee9645SThomas Gleixner 	 * already did this.
2615cee9645SThomas Gleixner 	 * The skew is done by adding 3*cpunr, then round, then subtract this
2625cee9645SThomas Gleixner 	 * extra offset again.
2635cee9645SThomas Gleixner 	 */
2645cee9645SThomas Gleixner 	j += cpu * 3;
2655cee9645SThomas Gleixner 
2665cee9645SThomas Gleixner 	rem = j % HZ;
2675cee9645SThomas Gleixner 
2685cee9645SThomas Gleixner 	/*
2695cee9645SThomas Gleixner 	 * If the target jiffie is just after a whole second (which can happen
2705cee9645SThomas Gleixner 	 * due to delays of the timer irq, long irq off times etc etc) then
2715cee9645SThomas Gleixner 	 * we should round down to the whole second, not up. Use 1/4th second
2725cee9645SThomas Gleixner 	 * as cutoff for this rounding as an extreme upper bound for this.
2735cee9645SThomas Gleixner 	 * But never round down if @force_up is set.
2745cee9645SThomas Gleixner 	 */
2755cee9645SThomas Gleixner 	if (rem < HZ/4 && !force_up) /* round down */
2765cee9645SThomas Gleixner 		j = j - rem;
2775cee9645SThomas Gleixner 	else /* round up */
2785cee9645SThomas Gleixner 		j = j - rem + HZ;
2795cee9645SThomas Gleixner 
2805cee9645SThomas Gleixner 	/* now that we have rounded, subtract the extra skew again */
2815cee9645SThomas Gleixner 	j -= cpu * 3;
2825cee9645SThomas Gleixner 
2835cee9645SThomas Gleixner 	/*
2845cee9645SThomas Gleixner 	 * Make sure j is still in the future. Otherwise return the
2855cee9645SThomas Gleixner 	 * unmodified value.
2865cee9645SThomas Gleixner 	 */
2875cee9645SThomas Gleixner 	return time_is_after_jiffies(j) ? j : original;
2885cee9645SThomas Gleixner }
2895cee9645SThomas Gleixner 
2905cee9645SThomas Gleixner /**
2915cee9645SThomas Gleixner  * __round_jiffies - function to round jiffies to a full second
2925cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
2935cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
2945cee9645SThomas Gleixner  *
2955cee9645SThomas Gleixner  * __round_jiffies() rounds an absolute time in the future (in jiffies)
2965cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
2975cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
2985cee9645SThomas Gleixner  * they fire approximately every X seconds.
2995cee9645SThomas Gleixner  *
3005cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3015cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3025cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3035cee9645SThomas Gleixner  *
3045cee9645SThomas Gleixner  * The exact rounding is skewed for each processor to avoid all
3055cee9645SThomas Gleixner  * processors firing at the exact same time, which could lead
3065cee9645SThomas Gleixner  * to lock contention or spurious cache line bouncing.
3075cee9645SThomas Gleixner  *
3085cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3095cee9645SThomas Gleixner  */
3105cee9645SThomas Gleixner unsigned long __round_jiffies(unsigned long j, int cpu)
3115cee9645SThomas Gleixner {
3125cee9645SThomas Gleixner 	return round_jiffies_common(j, cpu, false);
3135cee9645SThomas Gleixner }
3145cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies);
3155cee9645SThomas Gleixner 
3165cee9645SThomas Gleixner /**
3175cee9645SThomas Gleixner  * __round_jiffies_relative - function to round jiffies to a full second
3185cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
3195cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
3205cee9645SThomas Gleixner  *
3215cee9645SThomas Gleixner  * __round_jiffies_relative() rounds a time delta  in the future (in jiffies)
3225cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3235cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3245cee9645SThomas Gleixner  * they fire approximately every X seconds.
3255cee9645SThomas Gleixner  *
3265cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3275cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3285cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3295cee9645SThomas Gleixner  *
3305cee9645SThomas Gleixner  * The exact rounding is skewed for each processor to avoid all
3315cee9645SThomas Gleixner  * processors firing at the exact same time, which could lead
3325cee9645SThomas Gleixner  * to lock contention or spurious cache line bouncing.
3335cee9645SThomas Gleixner  *
3345cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3355cee9645SThomas Gleixner  */
3365cee9645SThomas Gleixner unsigned long __round_jiffies_relative(unsigned long j, int cpu)
3375cee9645SThomas Gleixner {
3385cee9645SThomas Gleixner 	unsigned long j0 = jiffies;
3395cee9645SThomas Gleixner 
3405cee9645SThomas Gleixner 	/* Use j0 because jiffies might change while we run */
3415cee9645SThomas Gleixner 	return round_jiffies_common(j + j0, cpu, false) - j0;
3425cee9645SThomas Gleixner }
3435cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_relative);
3445cee9645SThomas Gleixner 
3455cee9645SThomas Gleixner /**
3465cee9645SThomas Gleixner  * round_jiffies - function to round jiffies to a full second
3475cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
3485cee9645SThomas Gleixner  *
3495cee9645SThomas Gleixner  * round_jiffies() rounds an absolute time in the future (in jiffies)
3505cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3515cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3525cee9645SThomas Gleixner  * they fire approximately every X seconds.
3535cee9645SThomas Gleixner  *
3545cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3555cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3565cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3575cee9645SThomas Gleixner  *
3585cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3595cee9645SThomas Gleixner  */
3605cee9645SThomas Gleixner unsigned long round_jiffies(unsigned long j)
3615cee9645SThomas Gleixner {
3625cee9645SThomas Gleixner 	return round_jiffies_common(j, raw_smp_processor_id(), false);
3635cee9645SThomas Gleixner }
3645cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies);
3655cee9645SThomas Gleixner 
3665cee9645SThomas Gleixner /**
3675cee9645SThomas Gleixner  * round_jiffies_relative - function to round jiffies to a full second
3685cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
3695cee9645SThomas Gleixner  *
3705cee9645SThomas Gleixner  * round_jiffies_relative() rounds a time delta  in the future (in jiffies)
3715cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3725cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3735cee9645SThomas Gleixner  * they fire approximately every X seconds.
3745cee9645SThomas Gleixner  *
3755cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3765cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3775cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3785cee9645SThomas Gleixner  *
3795cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3805cee9645SThomas Gleixner  */
3815cee9645SThomas Gleixner unsigned long round_jiffies_relative(unsigned long j)
3825cee9645SThomas Gleixner {
3835cee9645SThomas Gleixner 	return __round_jiffies_relative(j, raw_smp_processor_id());
3845cee9645SThomas Gleixner }
3855cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_relative);
3865cee9645SThomas Gleixner 
3875cee9645SThomas Gleixner /**
3885cee9645SThomas Gleixner  * __round_jiffies_up - function to round jiffies up to a full second
3895cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
3905cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
3915cee9645SThomas Gleixner  *
3925cee9645SThomas Gleixner  * This is the same as __round_jiffies() except that it will never
3935cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
3945cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
3955cee9645SThomas Gleixner  * early.
3965cee9645SThomas Gleixner  */
3975cee9645SThomas Gleixner unsigned long __round_jiffies_up(unsigned long j, int cpu)
3985cee9645SThomas Gleixner {
3995cee9645SThomas Gleixner 	return round_jiffies_common(j, cpu, true);
4005cee9645SThomas Gleixner }
4015cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up);
4025cee9645SThomas Gleixner 
4035cee9645SThomas Gleixner /**
4045cee9645SThomas Gleixner  * __round_jiffies_up_relative - function to round jiffies up to a full second
4055cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
4065cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
4075cee9645SThomas Gleixner  *
4085cee9645SThomas Gleixner  * This is the same as __round_jiffies_relative() except that it will never
4095cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4105cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4115cee9645SThomas Gleixner  * early.
4125cee9645SThomas Gleixner  */
4135cee9645SThomas Gleixner unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
4145cee9645SThomas Gleixner {
4155cee9645SThomas Gleixner 	unsigned long j0 = jiffies;
4165cee9645SThomas Gleixner 
4175cee9645SThomas Gleixner 	/* Use j0 because jiffies might change while we run */
4185cee9645SThomas Gleixner 	return round_jiffies_common(j + j0, cpu, true) - j0;
4195cee9645SThomas Gleixner }
4205cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
4215cee9645SThomas Gleixner 
4225cee9645SThomas Gleixner /**
4235cee9645SThomas Gleixner  * round_jiffies_up - function to round jiffies up to a full second
4245cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
4255cee9645SThomas Gleixner  *
4265cee9645SThomas Gleixner  * This is the same as round_jiffies() except that it will never
4275cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4285cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4295cee9645SThomas Gleixner  * early.
4305cee9645SThomas Gleixner  */
4315cee9645SThomas Gleixner unsigned long round_jiffies_up(unsigned long j)
4325cee9645SThomas Gleixner {
4335cee9645SThomas Gleixner 	return round_jiffies_common(j, raw_smp_processor_id(), true);
4345cee9645SThomas Gleixner }
4355cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up);
4365cee9645SThomas Gleixner 
4375cee9645SThomas Gleixner /**
4385cee9645SThomas Gleixner  * round_jiffies_up_relative - function to round jiffies up to a full second
4395cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
4405cee9645SThomas Gleixner  *
4415cee9645SThomas Gleixner  * This is the same as round_jiffies_relative() except that it will never
4425cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4435cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4445cee9645SThomas Gleixner  * early.
4455cee9645SThomas Gleixner  */
4465cee9645SThomas Gleixner unsigned long round_jiffies_up_relative(unsigned long j)
4475cee9645SThomas Gleixner {
4485cee9645SThomas Gleixner 	return __round_jiffies_up_relative(j, raw_smp_processor_id());
4495cee9645SThomas Gleixner }
4505cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
4515cee9645SThomas Gleixner 
4525cee9645SThomas Gleixner 
453500462a9SThomas Gleixner static inline unsigned int timer_get_idx(struct timer_list *timer)
454500462a9SThomas Gleixner {
455500462a9SThomas Gleixner 	return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT;
456500462a9SThomas Gleixner }
457500462a9SThomas Gleixner 
458500462a9SThomas Gleixner static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
459500462a9SThomas Gleixner {
460500462a9SThomas Gleixner 	timer->flags = (timer->flags & ~TIMER_ARRAYMASK) |
461500462a9SThomas Gleixner 			idx << TIMER_ARRAYSHIFT;
462500462a9SThomas Gleixner }
463500462a9SThomas Gleixner 
464500462a9SThomas Gleixner /*
465500462a9SThomas Gleixner  * Helper function to calculate the array index for a given expiry
466500462a9SThomas Gleixner  * time.
467500462a9SThomas Gleixner  */
468500462a9SThomas Gleixner static inline unsigned calc_index(unsigned expires, unsigned lvl)
469500462a9SThomas Gleixner {
470500462a9SThomas Gleixner 	expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
471500462a9SThomas Gleixner 	return LVL_OFFS(lvl) + (expires & LVL_MASK);
472500462a9SThomas Gleixner }
473500462a9SThomas Gleixner 
474*ffdf0477SAnna-Maria Gleixner static int calc_wheel_index(unsigned long expires, unsigned long clk)
4755cee9645SThomas Gleixner {
476*ffdf0477SAnna-Maria Gleixner 	unsigned long delta = expires - clk;
477500462a9SThomas Gleixner 	unsigned int idx;
4785cee9645SThomas Gleixner 
479500462a9SThomas Gleixner 	if (delta < LVL_START(1)) {
480500462a9SThomas Gleixner 		idx = calc_index(expires, 0);
481500462a9SThomas Gleixner 	} else if (delta < LVL_START(2)) {
482500462a9SThomas Gleixner 		idx = calc_index(expires, 1);
483500462a9SThomas Gleixner 	} else if (delta < LVL_START(3)) {
484500462a9SThomas Gleixner 		idx = calc_index(expires, 2);
485500462a9SThomas Gleixner 	} else if (delta < LVL_START(4)) {
486500462a9SThomas Gleixner 		idx = calc_index(expires, 3);
487500462a9SThomas Gleixner 	} else if (delta < LVL_START(5)) {
488500462a9SThomas Gleixner 		idx = calc_index(expires, 4);
489500462a9SThomas Gleixner 	} else if (delta < LVL_START(6)) {
490500462a9SThomas Gleixner 		idx = calc_index(expires, 5);
491500462a9SThomas Gleixner 	} else if (delta < LVL_START(7)) {
492500462a9SThomas Gleixner 		idx = calc_index(expires, 6);
493500462a9SThomas Gleixner 	} else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
494500462a9SThomas Gleixner 		idx = calc_index(expires, 7);
495500462a9SThomas Gleixner 	} else if ((long) delta < 0) {
496*ffdf0477SAnna-Maria Gleixner 		idx = clk & LVL_MASK;
4975cee9645SThomas Gleixner 	} else {
498500462a9SThomas Gleixner 		/*
499500462a9SThomas Gleixner 		 * Force expire obscene large timeouts to expire at the
500500462a9SThomas Gleixner 		 * capacity limit of the wheel.
5015cee9645SThomas Gleixner 		 */
502500462a9SThomas Gleixner 		if (expires >= WHEEL_TIMEOUT_CUTOFF)
503500462a9SThomas Gleixner 			expires = WHEEL_TIMEOUT_MAX;
5041bd04bf6SThomas Gleixner 
505500462a9SThomas Gleixner 		idx = calc_index(expires, LVL_DEPTH - 1);
506500462a9SThomas Gleixner 	}
507*ffdf0477SAnna-Maria Gleixner 	return idx;
508*ffdf0477SAnna-Maria Gleixner }
509*ffdf0477SAnna-Maria Gleixner 
510500462a9SThomas Gleixner /*
511*ffdf0477SAnna-Maria Gleixner  * Enqueue the timer into the hash bucket, mark it pending in
512500462a9SThomas Gleixner  * the bitmap and store the index in the timer flags.
513500462a9SThomas Gleixner  */
514*ffdf0477SAnna-Maria Gleixner static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
515*ffdf0477SAnna-Maria Gleixner 			  unsigned int idx)
516*ffdf0477SAnna-Maria Gleixner {
517*ffdf0477SAnna-Maria Gleixner 	hlist_add_head(&timer->entry, base->vectors + idx);
518500462a9SThomas Gleixner 	__set_bit(idx, base->pending_map);
519500462a9SThomas Gleixner 	timer_set_idx(timer, idx);
5205cee9645SThomas Gleixner }
5215cee9645SThomas Gleixner 
522*ffdf0477SAnna-Maria Gleixner static void
523*ffdf0477SAnna-Maria Gleixner __internal_add_timer(struct timer_base *base, struct timer_list *timer)
5245cee9645SThomas Gleixner {
525*ffdf0477SAnna-Maria Gleixner 	unsigned int idx;
5269f6d9baaSViresh Kumar 
527*ffdf0477SAnna-Maria Gleixner 	idx = calc_wheel_index(timer->expires, base->clk);
528*ffdf0477SAnna-Maria Gleixner 	enqueue_timer(base, timer, idx);
529*ffdf0477SAnna-Maria Gleixner }
530*ffdf0477SAnna-Maria Gleixner 
531*ffdf0477SAnna-Maria Gleixner static void
532*ffdf0477SAnna-Maria Gleixner trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
533*ffdf0477SAnna-Maria Gleixner {
534a683f390SThomas Gleixner 	if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active)
535a683f390SThomas Gleixner 		return;
536a683f390SThomas Gleixner 
5379f6d9baaSViresh Kumar 	/*
538a683f390SThomas Gleixner 	 * TODO: This wants some optimizing similar to the code below, but we
539a683f390SThomas Gleixner 	 * will do that when we switch from push to pull for deferrable timers.
5409f6d9baaSViresh Kumar 	 */
541a683f390SThomas Gleixner 	if (timer->flags & TIMER_DEFERRABLE) {
542a683f390SThomas Gleixner 		if (tick_nohz_full_cpu(base->cpu))
5439f6d9baaSViresh Kumar 			wake_up_nohz_cpu(base->cpu);
544a683f390SThomas Gleixner 		return;
5455cee9645SThomas Gleixner 	}
546a683f390SThomas Gleixner 
547a683f390SThomas Gleixner 	/*
548a683f390SThomas Gleixner 	 * We might have to IPI the remote CPU if the base is idle and the
549a683f390SThomas Gleixner 	 * timer is not deferrable. If the other CPU is on the way to idle
550a683f390SThomas Gleixner 	 * then it can't set base->is_idle as we hold the base lock:
551a683f390SThomas Gleixner 	 */
552a683f390SThomas Gleixner 	if (!base->is_idle)
553a683f390SThomas Gleixner 		return;
554a683f390SThomas Gleixner 
555a683f390SThomas Gleixner 	/* Check whether this is the new first expiring timer: */
556a683f390SThomas Gleixner 	if (time_after_eq(timer->expires, base->next_expiry))
557a683f390SThomas Gleixner 		return;
558a683f390SThomas Gleixner 
559a683f390SThomas Gleixner 	/*
560a683f390SThomas Gleixner 	 * Set the next expiry time and kick the CPU so it can reevaluate the
561a683f390SThomas Gleixner 	 * wheel:
562a683f390SThomas Gleixner 	 */
563a683f390SThomas Gleixner 	base->next_expiry = timer->expires;
564a683f390SThomas Gleixner 		wake_up_nohz_cpu(base->cpu);
565683be13aSThomas Gleixner }
5665cee9645SThomas Gleixner 
567*ffdf0477SAnna-Maria Gleixner static void
568*ffdf0477SAnna-Maria Gleixner internal_add_timer(struct timer_base *base, struct timer_list *timer)
569*ffdf0477SAnna-Maria Gleixner {
570*ffdf0477SAnna-Maria Gleixner 	__internal_add_timer(base, timer);
571*ffdf0477SAnna-Maria Gleixner 	trigger_dyntick_cpu(base, timer);
572*ffdf0477SAnna-Maria Gleixner }
573*ffdf0477SAnna-Maria Gleixner 
5745cee9645SThomas Gleixner #ifdef CONFIG_TIMER_STATS
5755cee9645SThomas Gleixner void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr)
5765cee9645SThomas Gleixner {
5775cee9645SThomas Gleixner 	if (timer->start_site)
5785cee9645SThomas Gleixner 		return;
5795cee9645SThomas Gleixner 
5805cee9645SThomas Gleixner 	timer->start_site = addr;
5815cee9645SThomas Gleixner 	memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
5825cee9645SThomas Gleixner 	timer->start_pid = current->pid;
5835cee9645SThomas Gleixner }
5845cee9645SThomas Gleixner 
5855cee9645SThomas Gleixner static void timer_stats_account_timer(struct timer_list *timer)
5865cee9645SThomas Gleixner {
5873ed769bdSDmitry Vyukov 	void *site;
5883ed769bdSDmitry Vyukov 
5893ed769bdSDmitry Vyukov 	/*
5903ed769bdSDmitry Vyukov 	 * start_site can be concurrently reset by
5913ed769bdSDmitry Vyukov 	 * timer_stats_timer_clear_start_info()
5923ed769bdSDmitry Vyukov 	 */
5933ed769bdSDmitry Vyukov 	site = READ_ONCE(timer->start_site);
5943ed769bdSDmitry Vyukov 	if (likely(!site))
5955cee9645SThomas Gleixner 		return;
5965cee9645SThomas Gleixner 
5973ed769bdSDmitry Vyukov 	timer_stats_update_stats(timer, timer->start_pid, site,
598c74441a1SThomas Gleixner 				 timer->function, timer->start_comm,
599c74441a1SThomas Gleixner 				 timer->flags);
6005cee9645SThomas Gleixner }
6015cee9645SThomas Gleixner 
6025cee9645SThomas Gleixner #else
6035cee9645SThomas Gleixner static void timer_stats_account_timer(struct timer_list *timer) {}
6045cee9645SThomas Gleixner #endif
6055cee9645SThomas Gleixner 
6065cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
6075cee9645SThomas Gleixner 
6085cee9645SThomas Gleixner static struct debug_obj_descr timer_debug_descr;
6095cee9645SThomas Gleixner 
6105cee9645SThomas Gleixner static void *timer_debug_hint(void *addr)
6115cee9645SThomas Gleixner {
6125cee9645SThomas Gleixner 	return ((struct timer_list *) addr)->function;
6135cee9645SThomas Gleixner }
6145cee9645SThomas Gleixner 
615b9fdac7fSDu, Changbin static bool timer_is_static_object(void *addr)
616b9fdac7fSDu, Changbin {
617b9fdac7fSDu, Changbin 	struct timer_list *timer = addr;
618b9fdac7fSDu, Changbin 
619b9fdac7fSDu, Changbin 	return (timer->entry.pprev == NULL &&
620b9fdac7fSDu, Changbin 		timer->entry.next == TIMER_ENTRY_STATIC);
621b9fdac7fSDu, Changbin }
622b9fdac7fSDu, Changbin 
6235cee9645SThomas Gleixner /*
6245cee9645SThomas Gleixner  * fixup_init is called when:
6255cee9645SThomas Gleixner  * - an active object is initialized
6265cee9645SThomas Gleixner  */
627e3252464SDu, Changbin static bool timer_fixup_init(void *addr, enum debug_obj_state state)
6285cee9645SThomas Gleixner {
6295cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6305cee9645SThomas Gleixner 
6315cee9645SThomas Gleixner 	switch (state) {
6325cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
6335cee9645SThomas Gleixner 		del_timer_sync(timer);
6345cee9645SThomas Gleixner 		debug_object_init(timer, &timer_debug_descr);
635e3252464SDu, Changbin 		return true;
6365cee9645SThomas Gleixner 	default:
637e3252464SDu, Changbin 		return false;
6385cee9645SThomas Gleixner 	}
6395cee9645SThomas Gleixner }
6405cee9645SThomas Gleixner 
6415cee9645SThomas Gleixner /* Stub timer callback for improperly used timers. */
6425cee9645SThomas Gleixner static void stub_timer(unsigned long data)
6435cee9645SThomas Gleixner {
6445cee9645SThomas Gleixner 	WARN_ON(1);
6455cee9645SThomas Gleixner }
6465cee9645SThomas Gleixner 
6475cee9645SThomas Gleixner /*
6485cee9645SThomas Gleixner  * fixup_activate is called when:
6495cee9645SThomas Gleixner  * - an active object is activated
650b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
6515cee9645SThomas Gleixner  */
652e3252464SDu, Changbin static bool timer_fixup_activate(void *addr, enum debug_obj_state state)
6535cee9645SThomas Gleixner {
6545cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6555cee9645SThomas Gleixner 
6565cee9645SThomas Gleixner 	switch (state) {
6575cee9645SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
6585cee9645SThomas Gleixner 		setup_timer(timer, stub_timer, 0);
659e3252464SDu, Changbin 		return true;
6605cee9645SThomas Gleixner 
6615cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
6625cee9645SThomas Gleixner 		WARN_ON(1);
6635cee9645SThomas Gleixner 
6645cee9645SThomas Gleixner 	default:
665e3252464SDu, Changbin 		return false;
6665cee9645SThomas Gleixner 	}
6675cee9645SThomas Gleixner }
6685cee9645SThomas Gleixner 
6695cee9645SThomas Gleixner /*
6705cee9645SThomas Gleixner  * fixup_free is called when:
6715cee9645SThomas Gleixner  * - an active object is freed
6725cee9645SThomas Gleixner  */
673e3252464SDu, Changbin static bool timer_fixup_free(void *addr, enum debug_obj_state state)
6745cee9645SThomas Gleixner {
6755cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6765cee9645SThomas Gleixner 
6775cee9645SThomas Gleixner 	switch (state) {
6785cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
6795cee9645SThomas Gleixner 		del_timer_sync(timer);
6805cee9645SThomas Gleixner 		debug_object_free(timer, &timer_debug_descr);
681e3252464SDu, Changbin 		return true;
6825cee9645SThomas Gleixner 	default:
683e3252464SDu, Changbin 		return false;
6845cee9645SThomas Gleixner 	}
6855cee9645SThomas Gleixner }
6865cee9645SThomas Gleixner 
6875cee9645SThomas Gleixner /*
6885cee9645SThomas Gleixner  * fixup_assert_init is called when:
6895cee9645SThomas Gleixner  * - an untracked/uninit-ed object is found
6905cee9645SThomas Gleixner  */
691e3252464SDu, Changbin static bool timer_fixup_assert_init(void *addr, enum debug_obj_state state)
6925cee9645SThomas Gleixner {
6935cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6945cee9645SThomas Gleixner 
6955cee9645SThomas Gleixner 	switch (state) {
6965cee9645SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
6975cee9645SThomas Gleixner 		setup_timer(timer, stub_timer, 0);
698e3252464SDu, Changbin 		return true;
6995cee9645SThomas Gleixner 	default:
700e3252464SDu, Changbin 		return false;
7015cee9645SThomas Gleixner 	}
7025cee9645SThomas Gleixner }
7035cee9645SThomas Gleixner 
7045cee9645SThomas Gleixner static struct debug_obj_descr timer_debug_descr = {
7055cee9645SThomas Gleixner 	.name			= "timer_list",
7065cee9645SThomas Gleixner 	.debug_hint		= timer_debug_hint,
707b9fdac7fSDu, Changbin 	.is_static_object	= timer_is_static_object,
7085cee9645SThomas Gleixner 	.fixup_init		= timer_fixup_init,
7095cee9645SThomas Gleixner 	.fixup_activate		= timer_fixup_activate,
7105cee9645SThomas Gleixner 	.fixup_free		= timer_fixup_free,
7115cee9645SThomas Gleixner 	.fixup_assert_init	= timer_fixup_assert_init,
7125cee9645SThomas Gleixner };
7135cee9645SThomas Gleixner 
7145cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer)
7155cee9645SThomas Gleixner {
7165cee9645SThomas Gleixner 	debug_object_init(timer, &timer_debug_descr);
7175cee9645SThomas Gleixner }
7185cee9645SThomas Gleixner 
7195cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer)
7205cee9645SThomas Gleixner {
7215cee9645SThomas Gleixner 	debug_object_activate(timer, &timer_debug_descr);
7225cee9645SThomas Gleixner }
7235cee9645SThomas Gleixner 
7245cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer)
7255cee9645SThomas Gleixner {
7265cee9645SThomas Gleixner 	debug_object_deactivate(timer, &timer_debug_descr);
7275cee9645SThomas Gleixner }
7285cee9645SThomas Gleixner 
7295cee9645SThomas Gleixner static inline void debug_timer_free(struct timer_list *timer)
7305cee9645SThomas Gleixner {
7315cee9645SThomas Gleixner 	debug_object_free(timer, &timer_debug_descr);
7325cee9645SThomas Gleixner }
7335cee9645SThomas Gleixner 
7345cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer)
7355cee9645SThomas Gleixner {
7365cee9645SThomas Gleixner 	debug_object_assert_init(timer, &timer_debug_descr);
7375cee9645SThomas Gleixner }
7385cee9645SThomas Gleixner 
7395cee9645SThomas Gleixner static void do_init_timer(struct timer_list *timer, unsigned int flags,
7405cee9645SThomas Gleixner 			  const char *name, struct lock_class_key *key);
7415cee9645SThomas Gleixner 
7425cee9645SThomas Gleixner void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
7435cee9645SThomas Gleixner 			     const char *name, struct lock_class_key *key)
7445cee9645SThomas Gleixner {
7455cee9645SThomas Gleixner 	debug_object_init_on_stack(timer, &timer_debug_descr);
7465cee9645SThomas Gleixner 	do_init_timer(timer, flags, name, key);
7475cee9645SThomas Gleixner }
7485cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
7495cee9645SThomas Gleixner 
7505cee9645SThomas Gleixner void destroy_timer_on_stack(struct timer_list *timer)
7515cee9645SThomas Gleixner {
7525cee9645SThomas Gleixner 	debug_object_free(timer, &timer_debug_descr);
7535cee9645SThomas Gleixner }
7545cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
7555cee9645SThomas Gleixner 
7565cee9645SThomas Gleixner #else
7575cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer) { }
7585cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer) { }
7595cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer) { }
7605cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer) { }
7615cee9645SThomas Gleixner #endif
7625cee9645SThomas Gleixner 
7635cee9645SThomas Gleixner static inline void debug_init(struct timer_list *timer)
7645cee9645SThomas Gleixner {
7655cee9645SThomas Gleixner 	debug_timer_init(timer);
7665cee9645SThomas Gleixner 	trace_timer_init(timer);
7675cee9645SThomas Gleixner }
7685cee9645SThomas Gleixner 
7695cee9645SThomas Gleixner static inline void
7705cee9645SThomas Gleixner debug_activate(struct timer_list *timer, unsigned long expires)
7715cee9645SThomas Gleixner {
7725cee9645SThomas Gleixner 	debug_timer_activate(timer);
7730eeda71bSThomas Gleixner 	trace_timer_start(timer, expires, timer->flags);
7745cee9645SThomas Gleixner }
7755cee9645SThomas Gleixner 
7765cee9645SThomas Gleixner static inline void debug_deactivate(struct timer_list *timer)
7775cee9645SThomas Gleixner {
7785cee9645SThomas Gleixner 	debug_timer_deactivate(timer);
7795cee9645SThomas Gleixner 	trace_timer_cancel(timer);
7805cee9645SThomas Gleixner }
7815cee9645SThomas Gleixner 
7825cee9645SThomas Gleixner static inline void debug_assert_init(struct timer_list *timer)
7835cee9645SThomas Gleixner {
7845cee9645SThomas Gleixner 	debug_timer_assert_init(timer);
7855cee9645SThomas Gleixner }
7865cee9645SThomas Gleixner 
7875cee9645SThomas Gleixner static void do_init_timer(struct timer_list *timer, unsigned int flags,
7885cee9645SThomas Gleixner 			  const char *name, struct lock_class_key *key)
7895cee9645SThomas Gleixner {
7901dabbcecSThomas Gleixner 	timer->entry.pprev = NULL;
7910eeda71bSThomas Gleixner 	timer->flags = flags | raw_smp_processor_id();
7925cee9645SThomas Gleixner #ifdef CONFIG_TIMER_STATS
7935cee9645SThomas Gleixner 	timer->start_site = NULL;
7945cee9645SThomas Gleixner 	timer->start_pid = -1;
7955cee9645SThomas Gleixner 	memset(timer->start_comm, 0, TASK_COMM_LEN);
7965cee9645SThomas Gleixner #endif
7975cee9645SThomas Gleixner 	lockdep_init_map(&timer->lockdep_map, name, key, 0);
7985cee9645SThomas Gleixner }
7995cee9645SThomas Gleixner 
8005cee9645SThomas Gleixner /**
8015cee9645SThomas Gleixner  * init_timer_key - initialize a timer
8025cee9645SThomas Gleixner  * @timer: the timer to be initialized
8035cee9645SThomas Gleixner  * @flags: timer flags
8045cee9645SThomas Gleixner  * @name: name of the timer
8055cee9645SThomas Gleixner  * @key: lockdep class key of the fake lock used for tracking timer
8065cee9645SThomas Gleixner  *       sync lock dependencies
8075cee9645SThomas Gleixner  *
8085cee9645SThomas Gleixner  * init_timer_key() must be done to a timer prior calling *any* of the
8095cee9645SThomas Gleixner  * other timer functions.
8105cee9645SThomas Gleixner  */
8115cee9645SThomas Gleixner void init_timer_key(struct timer_list *timer, unsigned int flags,
8125cee9645SThomas Gleixner 		    const char *name, struct lock_class_key *key)
8135cee9645SThomas Gleixner {
8145cee9645SThomas Gleixner 	debug_init(timer);
8155cee9645SThomas Gleixner 	do_init_timer(timer, flags, name, key);
8165cee9645SThomas Gleixner }
8175cee9645SThomas Gleixner EXPORT_SYMBOL(init_timer_key);
8185cee9645SThomas Gleixner 
8195cee9645SThomas Gleixner static inline void detach_timer(struct timer_list *timer, bool clear_pending)
8205cee9645SThomas Gleixner {
8211dabbcecSThomas Gleixner 	struct hlist_node *entry = &timer->entry;
8225cee9645SThomas Gleixner 
8235cee9645SThomas Gleixner 	debug_deactivate(timer);
8245cee9645SThomas Gleixner 
8251dabbcecSThomas Gleixner 	__hlist_del(entry);
8265cee9645SThomas Gleixner 	if (clear_pending)
8271dabbcecSThomas Gleixner 		entry->pprev = NULL;
8281dabbcecSThomas Gleixner 	entry->next = LIST_POISON2;
8295cee9645SThomas Gleixner }
8305cee9645SThomas Gleixner 
831494af3edSThomas Gleixner static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
8325cee9645SThomas Gleixner 			     bool clear_pending)
8335cee9645SThomas Gleixner {
834500462a9SThomas Gleixner 	unsigned idx = timer_get_idx(timer);
835500462a9SThomas Gleixner 
8365cee9645SThomas Gleixner 	if (!timer_pending(timer))
8375cee9645SThomas Gleixner 		return 0;
8385cee9645SThomas Gleixner 
839500462a9SThomas Gleixner 	if (hlist_is_singular_node(&timer->entry, base->vectors + idx))
840500462a9SThomas Gleixner 		__clear_bit(idx, base->pending_map);
841500462a9SThomas Gleixner 
8425cee9645SThomas Gleixner 	detach_timer(timer, clear_pending);
8435cee9645SThomas Gleixner 	return 1;
8445cee9645SThomas Gleixner }
8455cee9645SThomas Gleixner 
846500462a9SThomas Gleixner static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
847500462a9SThomas Gleixner {
848500462a9SThomas Gleixner 	struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu);
849500462a9SThomas Gleixner 
8505cee9645SThomas Gleixner 	/*
851500462a9SThomas Gleixner 	 * If the timer is deferrable and nohz is active then we need to use
852500462a9SThomas Gleixner 	 * the deferrable base.
853500462a9SThomas Gleixner 	 */
854500462a9SThomas Gleixner 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active &&
855500462a9SThomas Gleixner 	    (tflags & TIMER_DEFERRABLE))
856500462a9SThomas Gleixner 		base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
857500462a9SThomas Gleixner 	return base;
858500462a9SThomas Gleixner }
859500462a9SThomas Gleixner 
860500462a9SThomas Gleixner static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
861500462a9SThomas Gleixner {
862500462a9SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
863500462a9SThomas Gleixner 
864500462a9SThomas Gleixner 	/*
865500462a9SThomas Gleixner 	 * If the timer is deferrable and nohz is active then we need to use
866500462a9SThomas Gleixner 	 * the deferrable base.
867500462a9SThomas Gleixner 	 */
868500462a9SThomas Gleixner 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active &&
869500462a9SThomas Gleixner 	    (tflags & TIMER_DEFERRABLE))
870500462a9SThomas Gleixner 		base = this_cpu_ptr(&timer_bases[BASE_DEF]);
871500462a9SThomas Gleixner 	return base;
872500462a9SThomas Gleixner }
873500462a9SThomas Gleixner 
874500462a9SThomas Gleixner static inline struct timer_base *get_timer_base(u32 tflags)
875500462a9SThomas Gleixner {
876500462a9SThomas Gleixner 	return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
877500462a9SThomas Gleixner }
878500462a9SThomas Gleixner 
879a683f390SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
880a683f390SThomas Gleixner static inline struct timer_base *
881a683f390SThomas Gleixner __get_target_base(struct timer_base *base, unsigned tflags)
882500462a9SThomas Gleixner {
883a683f390SThomas Gleixner #ifdef CONFIG_SMP
884500462a9SThomas Gleixner 	if ((tflags & TIMER_PINNED) || !base->migration_enabled)
885500462a9SThomas Gleixner 		return get_timer_this_cpu_base(tflags);
886500462a9SThomas Gleixner 	return get_timer_cpu_base(tflags, get_nohz_timer_target());
887500462a9SThomas Gleixner #else
888500462a9SThomas Gleixner 	return get_timer_this_cpu_base(tflags);
889500462a9SThomas Gleixner #endif
890500462a9SThomas Gleixner }
891500462a9SThomas Gleixner 
892a683f390SThomas Gleixner static inline void forward_timer_base(struct timer_base *base)
893a683f390SThomas Gleixner {
894a683f390SThomas Gleixner 	/*
895a683f390SThomas Gleixner 	 * We only forward the base when it's idle and we have a delta between
896a683f390SThomas Gleixner 	 * base clock and jiffies.
897a683f390SThomas Gleixner 	 */
898a683f390SThomas Gleixner 	if (!base->is_idle || (long) (jiffies - base->clk) < 2)
899a683f390SThomas Gleixner 		return;
900a683f390SThomas Gleixner 
901a683f390SThomas Gleixner 	/*
902a683f390SThomas Gleixner 	 * If the next expiry value is > jiffies, then we fast forward to
903a683f390SThomas Gleixner 	 * jiffies otherwise we forward to the next expiry value.
904a683f390SThomas Gleixner 	 */
905a683f390SThomas Gleixner 	if (time_after(base->next_expiry, jiffies))
906a683f390SThomas Gleixner 		base->clk = jiffies;
907a683f390SThomas Gleixner 	else
908a683f390SThomas Gleixner 		base->clk = base->next_expiry;
909a683f390SThomas Gleixner }
910a683f390SThomas Gleixner #else
911a683f390SThomas Gleixner static inline struct timer_base *
912a683f390SThomas Gleixner __get_target_base(struct timer_base *base, unsigned tflags)
913a683f390SThomas Gleixner {
914a683f390SThomas Gleixner 	return get_timer_this_cpu_base(tflags);
915a683f390SThomas Gleixner }
916a683f390SThomas Gleixner 
917a683f390SThomas Gleixner static inline void forward_timer_base(struct timer_base *base) { }
918a683f390SThomas Gleixner #endif
919a683f390SThomas Gleixner 
920a683f390SThomas Gleixner static inline struct timer_base *
921a683f390SThomas Gleixner get_target_base(struct timer_base *base, unsigned tflags)
922a683f390SThomas Gleixner {
923a683f390SThomas Gleixner 	struct timer_base *target = __get_target_base(base, tflags);
924a683f390SThomas Gleixner 
925a683f390SThomas Gleixner 	forward_timer_base(target);
926a683f390SThomas Gleixner 	return target;
927a683f390SThomas Gleixner }
928a683f390SThomas Gleixner 
929500462a9SThomas Gleixner /*
930500462a9SThomas Gleixner  * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
931500462a9SThomas Gleixner  * that all timers which are tied to this base are locked, and the base itself
932500462a9SThomas Gleixner  * is locked too.
9335cee9645SThomas Gleixner  *
9345cee9645SThomas Gleixner  * So __run_timers/migrate_timers can safely modify all timers which could
935500462a9SThomas Gleixner  * be found in the base->vectors array.
9365cee9645SThomas Gleixner  *
937500462a9SThomas Gleixner  * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
938500462a9SThomas Gleixner  * to wait until the migration is done.
9395cee9645SThomas Gleixner  */
940494af3edSThomas Gleixner static struct timer_base *lock_timer_base(struct timer_list *timer,
9415cee9645SThomas Gleixner 					  unsigned long *flags)
9425cee9645SThomas Gleixner 	__acquires(timer->base->lock)
9435cee9645SThomas Gleixner {
9440eeda71bSThomas Gleixner 	for (;;) {
945494af3edSThomas Gleixner 		struct timer_base *base;
946500462a9SThomas Gleixner 		u32 tf = timer->flags;
9475cee9645SThomas Gleixner 
9480eeda71bSThomas Gleixner 		if (!(tf & TIMER_MIGRATING)) {
949500462a9SThomas Gleixner 			base = get_timer_base(tf);
9505cee9645SThomas Gleixner 			spin_lock_irqsave(&base->lock, *flags);
9510eeda71bSThomas Gleixner 			if (timer->flags == tf)
9525cee9645SThomas Gleixner 				return base;
9535cee9645SThomas Gleixner 			spin_unlock_irqrestore(&base->lock, *flags);
9545cee9645SThomas Gleixner 		}
9555cee9645SThomas Gleixner 		cpu_relax();
9565cee9645SThomas Gleixner 	}
9575cee9645SThomas Gleixner }
9585cee9645SThomas Gleixner 
9595cee9645SThomas Gleixner static inline int
960177ec0a0SThomas Gleixner __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
9615cee9645SThomas Gleixner {
962494af3edSThomas Gleixner 	struct timer_base *base, *new_base;
9635cee9645SThomas Gleixner 	unsigned long flags;
964bc7a34b8SThomas Gleixner 	int ret = 0;
9655cee9645SThomas Gleixner 
966500462a9SThomas Gleixner 	/*
967500462a9SThomas Gleixner 	 * TODO: Calculate the array bucket of the timer right here w/o
968500462a9SThomas Gleixner 	 * holding the base lock. This allows to check not only
969500462a9SThomas Gleixner 	 * timer->expires == expires below, but also whether the timer
970500462a9SThomas Gleixner 	 * ends up in the same bucket. If we really need to requeue
971500462a9SThomas Gleixner 	 * the timer then we check whether base->clk have
972500462a9SThomas Gleixner 	 * advanced between here and locking the timer base. If
973500462a9SThomas Gleixner 	 * jiffies advanced we have to recalc the array bucket with the
974500462a9SThomas Gleixner 	 * lock held.
975500462a9SThomas Gleixner 	 */
976500462a9SThomas Gleixner 
977500462a9SThomas Gleixner 	/*
978500462a9SThomas Gleixner 	 * This is a common optimization triggered by the
979500462a9SThomas Gleixner 	 * networking code - if the timer is re-modified
980500462a9SThomas Gleixner 	 * to be the same thing then just return:
981500462a9SThomas Gleixner 	 */
982500462a9SThomas Gleixner 	if (timer_pending(timer)) {
983500462a9SThomas Gleixner 		if (timer->expires == expires)
984500462a9SThomas Gleixner 			return 1;
985500462a9SThomas Gleixner 	}
986500462a9SThomas Gleixner 
9875cee9645SThomas Gleixner 	timer_stats_timer_set_start_info(timer);
9885cee9645SThomas Gleixner 	BUG_ON(!timer->function);
9895cee9645SThomas Gleixner 
9905cee9645SThomas Gleixner 	base = lock_timer_base(timer, &flags);
9915cee9645SThomas Gleixner 
9925cee9645SThomas Gleixner 	ret = detach_if_pending(timer, base, false);
9935cee9645SThomas Gleixner 	if (!ret && pending_only)
9945cee9645SThomas Gleixner 		goto out_unlock;
9955cee9645SThomas Gleixner 
9965cee9645SThomas Gleixner 	debug_activate(timer, expires);
9975cee9645SThomas Gleixner 
998500462a9SThomas Gleixner 	new_base = get_target_base(base, timer->flags);
9995cee9645SThomas Gleixner 
10005cee9645SThomas Gleixner 	if (base != new_base) {
10015cee9645SThomas Gleixner 		/*
1002500462a9SThomas Gleixner 		 * We are trying to schedule the timer on the new base.
10035cee9645SThomas Gleixner 		 * However we can't change timer's base while it is running,
10045cee9645SThomas Gleixner 		 * otherwise del_timer_sync() can't detect that the timer's
1005500462a9SThomas Gleixner 		 * handler yet has not finished. This also guarantees that the
1006500462a9SThomas Gleixner 		 * timer is serialized wrt itself.
10075cee9645SThomas Gleixner 		 */
10085cee9645SThomas Gleixner 		if (likely(base->running_timer != timer)) {
10095cee9645SThomas Gleixner 			/* See the comment in lock_timer_base() */
10100eeda71bSThomas Gleixner 			timer->flags |= TIMER_MIGRATING;
10110eeda71bSThomas Gleixner 
10125cee9645SThomas Gleixner 			spin_unlock(&base->lock);
10135cee9645SThomas Gleixner 			base = new_base;
10145cee9645SThomas Gleixner 			spin_lock(&base->lock);
1015d0023a14SEric Dumazet 			WRITE_ONCE(timer->flags,
1016d0023a14SEric Dumazet 				   (timer->flags & ~TIMER_BASEMASK) | base->cpu);
10175cee9645SThomas Gleixner 		}
10185cee9645SThomas Gleixner 	}
10195cee9645SThomas Gleixner 
10205cee9645SThomas Gleixner 	timer->expires = expires;
10215cee9645SThomas Gleixner 	internal_add_timer(base, timer);
10225cee9645SThomas Gleixner 
10235cee9645SThomas Gleixner out_unlock:
10245cee9645SThomas Gleixner 	spin_unlock_irqrestore(&base->lock, flags);
10255cee9645SThomas Gleixner 
10265cee9645SThomas Gleixner 	return ret;
10275cee9645SThomas Gleixner }
10285cee9645SThomas Gleixner 
10295cee9645SThomas Gleixner /**
10305cee9645SThomas Gleixner  * mod_timer_pending - modify a pending timer's timeout
10315cee9645SThomas Gleixner  * @timer: the pending timer to be modified
10325cee9645SThomas Gleixner  * @expires: new timeout in jiffies
10335cee9645SThomas Gleixner  *
10345cee9645SThomas Gleixner  * mod_timer_pending() is the same for pending timers as mod_timer(),
10355cee9645SThomas Gleixner  * but will not re-activate and modify already deleted timers.
10365cee9645SThomas Gleixner  *
10375cee9645SThomas Gleixner  * It is useful for unserialized use of timers.
10385cee9645SThomas Gleixner  */
10395cee9645SThomas Gleixner int mod_timer_pending(struct timer_list *timer, unsigned long expires)
10405cee9645SThomas Gleixner {
1041177ec0a0SThomas Gleixner 	return __mod_timer(timer, expires, true);
10425cee9645SThomas Gleixner }
10435cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer_pending);
10445cee9645SThomas Gleixner 
10455cee9645SThomas Gleixner /**
10465cee9645SThomas Gleixner  * mod_timer - modify a timer's timeout
10475cee9645SThomas Gleixner  * @timer: the timer to be modified
10485cee9645SThomas Gleixner  * @expires: new timeout in jiffies
10495cee9645SThomas Gleixner  *
10505cee9645SThomas Gleixner  * mod_timer() is a more efficient way to update the expire field of an
10515cee9645SThomas Gleixner  * active timer (if the timer is inactive it will be activated)
10525cee9645SThomas Gleixner  *
10535cee9645SThomas Gleixner  * mod_timer(timer, expires) is equivalent to:
10545cee9645SThomas Gleixner  *
10555cee9645SThomas Gleixner  *     del_timer(timer); timer->expires = expires; add_timer(timer);
10565cee9645SThomas Gleixner  *
10575cee9645SThomas Gleixner  * Note that if there are multiple unserialized concurrent users of the
10585cee9645SThomas Gleixner  * same timer, then mod_timer() is the only safe way to modify the timeout,
10595cee9645SThomas Gleixner  * since add_timer() cannot modify an already running timer.
10605cee9645SThomas Gleixner  *
10615cee9645SThomas Gleixner  * The function returns whether it has modified a pending timer or not.
10625cee9645SThomas Gleixner  * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
10635cee9645SThomas Gleixner  * active timer returns 1.)
10645cee9645SThomas Gleixner  */
10655cee9645SThomas Gleixner int mod_timer(struct timer_list *timer, unsigned long expires)
10665cee9645SThomas Gleixner {
1067177ec0a0SThomas Gleixner 	return __mod_timer(timer, expires, false);
10685cee9645SThomas Gleixner }
10695cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer);
10705cee9645SThomas Gleixner 
10715cee9645SThomas Gleixner /**
10725cee9645SThomas Gleixner  * add_timer - start a timer
10735cee9645SThomas Gleixner  * @timer: the timer to be added
10745cee9645SThomas Gleixner  *
10755cee9645SThomas Gleixner  * The kernel will do a ->function(->data) callback from the
10765cee9645SThomas Gleixner  * timer interrupt at the ->expires point in the future. The
10775cee9645SThomas Gleixner  * current time is 'jiffies'.
10785cee9645SThomas Gleixner  *
10795cee9645SThomas Gleixner  * The timer's ->expires, ->function (and if the handler uses it, ->data)
10805cee9645SThomas Gleixner  * fields must be set prior calling this function.
10815cee9645SThomas Gleixner  *
10825cee9645SThomas Gleixner  * Timers with an ->expires field in the past will be executed in the next
10835cee9645SThomas Gleixner  * timer tick.
10845cee9645SThomas Gleixner  */
10855cee9645SThomas Gleixner void add_timer(struct timer_list *timer)
10865cee9645SThomas Gleixner {
10875cee9645SThomas Gleixner 	BUG_ON(timer_pending(timer));
10885cee9645SThomas Gleixner 	mod_timer(timer, timer->expires);
10895cee9645SThomas Gleixner }
10905cee9645SThomas Gleixner EXPORT_SYMBOL(add_timer);
10915cee9645SThomas Gleixner 
10925cee9645SThomas Gleixner /**
10935cee9645SThomas Gleixner  * add_timer_on - start a timer on a particular CPU
10945cee9645SThomas Gleixner  * @timer: the timer to be added
10955cee9645SThomas Gleixner  * @cpu: the CPU to start it on
10965cee9645SThomas Gleixner  *
10975cee9645SThomas Gleixner  * This is not very scalable on SMP. Double adds are not possible.
10985cee9645SThomas Gleixner  */
10995cee9645SThomas Gleixner void add_timer_on(struct timer_list *timer, int cpu)
11005cee9645SThomas Gleixner {
1101500462a9SThomas Gleixner 	struct timer_base *new_base, *base;
11025cee9645SThomas Gleixner 	unsigned long flags;
11035cee9645SThomas Gleixner 
11045cee9645SThomas Gleixner 	timer_stats_timer_set_start_info(timer);
11055cee9645SThomas Gleixner 	BUG_ON(timer_pending(timer) || !timer->function);
110622b886ddSTejun Heo 
1107500462a9SThomas Gleixner 	new_base = get_timer_cpu_base(timer->flags, cpu);
1108500462a9SThomas Gleixner 
110922b886ddSTejun Heo 	/*
111022b886ddSTejun Heo 	 * If @timer was on a different CPU, it should be migrated with the
111122b886ddSTejun Heo 	 * old base locked to prevent other operations proceeding with the
111222b886ddSTejun Heo 	 * wrong base locked.  See lock_timer_base().
111322b886ddSTejun Heo 	 */
111422b886ddSTejun Heo 	base = lock_timer_base(timer, &flags);
111522b886ddSTejun Heo 	if (base != new_base) {
111622b886ddSTejun Heo 		timer->flags |= TIMER_MIGRATING;
111722b886ddSTejun Heo 
111822b886ddSTejun Heo 		spin_unlock(&base->lock);
111922b886ddSTejun Heo 		base = new_base;
112022b886ddSTejun Heo 		spin_lock(&base->lock);
112122b886ddSTejun Heo 		WRITE_ONCE(timer->flags,
112222b886ddSTejun Heo 			   (timer->flags & ~TIMER_BASEMASK) | cpu);
112322b886ddSTejun Heo 	}
112422b886ddSTejun Heo 
11255cee9645SThomas Gleixner 	debug_activate(timer, timer->expires);
11265cee9645SThomas Gleixner 	internal_add_timer(base, timer);
11275cee9645SThomas Gleixner 	spin_unlock_irqrestore(&base->lock, flags);
11285cee9645SThomas Gleixner }
11295cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(add_timer_on);
11305cee9645SThomas Gleixner 
11315cee9645SThomas Gleixner /**
11325cee9645SThomas Gleixner  * del_timer - deactive a timer.
11335cee9645SThomas Gleixner  * @timer: the timer to be deactivated
11345cee9645SThomas Gleixner  *
11355cee9645SThomas Gleixner  * del_timer() deactivates a timer - this works on both active and inactive
11365cee9645SThomas Gleixner  * timers.
11375cee9645SThomas Gleixner  *
11385cee9645SThomas Gleixner  * The function returns whether it has deactivated a pending timer or not.
11395cee9645SThomas Gleixner  * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
11405cee9645SThomas Gleixner  * active timer returns 1.)
11415cee9645SThomas Gleixner  */
11425cee9645SThomas Gleixner int del_timer(struct timer_list *timer)
11435cee9645SThomas Gleixner {
1144494af3edSThomas Gleixner 	struct timer_base *base;
11455cee9645SThomas Gleixner 	unsigned long flags;
11465cee9645SThomas Gleixner 	int ret = 0;
11475cee9645SThomas Gleixner 
11485cee9645SThomas Gleixner 	debug_assert_init(timer);
11495cee9645SThomas Gleixner 
11505cee9645SThomas Gleixner 	timer_stats_timer_clear_start_info(timer);
11515cee9645SThomas Gleixner 	if (timer_pending(timer)) {
11525cee9645SThomas Gleixner 		base = lock_timer_base(timer, &flags);
11535cee9645SThomas Gleixner 		ret = detach_if_pending(timer, base, true);
11545cee9645SThomas Gleixner 		spin_unlock_irqrestore(&base->lock, flags);
11555cee9645SThomas Gleixner 	}
11565cee9645SThomas Gleixner 
11575cee9645SThomas Gleixner 	return ret;
11585cee9645SThomas Gleixner }
11595cee9645SThomas Gleixner EXPORT_SYMBOL(del_timer);
11605cee9645SThomas Gleixner 
11615cee9645SThomas Gleixner /**
11625cee9645SThomas Gleixner  * try_to_del_timer_sync - Try to deactivate a timer
11635cee9645SThomas Gleixner  * @timer: timer do del
11645cee9645SThomas Gleixner  *
11655cee9645SThomas Gleixner  * This function tries to deactivate a timer. Upon successful (ret >= 0)
11665cee9645SThomas Gleixner  * exit the timer is not queued and the handler is not running on any CPU.
11675cee9645SThomas Gleixner  */
11685cee9645SThomas Gleixner int try_to_del_timer_sync(struct timer_list *timer)
11695cee9645SThomas Gleixner {
1170494af3edSThomas Gleixner 	struct timer_base *base;
11715cee9645SThomas Gleixner 	unsigned long flags;
11725cee9645SThomas Gleixner 	int ret = -1;
11735cee9645SThomas Gleixner 
11745cee9645SThomas Gleixner 	debug_assert_init(timer);
11755cee9645SThomas Gleixner 
11765cee9645SThomas Gleixner 	base = lock_timer_base(timer, &flags);
11775cee9645SThomas Gleixner 
11785cee9645SThomas Gleixner 	if (base->running_timer != timer) {
11795cee9645SThomas Gleixner 		timer_stats_timer_clear_start_info(timer);
11805cee9645SThomas Gleixner 		ret = detach_if_pending(timer, base, true);
11815cee9645SThomas Gleixner 	}
11825cee9645SThomas Gleixner 	spin_unlock_irqrestore(&base->lock, flags);
11835cee9645SThomas Gleixner 
11845cee9645SThomas Gleixner 	return ret;
11855cee9645SThomas Gleixner }
11865cee9645SThomas Gleixner EXPORT_SYMBOL(try_to_del_timer_sync);
11875cee9645SThomas Gleixner 
11885cee9645SThomas Gleixner #ifdef CONFIG_SMP
11895cee9645SThomas Gleixner /**
11905cee9645SThomas Gleixner  * del_timer_sync - deactivate a timer and wait for the handler to finish.
11915cee9645SThomas Gleixner  * @timer: the timer to be deactivated
11925cee9645SThomas Gleixner  *
11935cee9645SThomas Gleixner  * This function only differs from del_timer() on SMP: besides deactivating
11945cee9645SThomas Gleixner  * the timer it also makes sure the handler has finished executing on other
11955cee9645SThomas Gleixner  * CPUs.
11965cee9645SThomas Gleixner  *
11975cee9645SThomas Gleixner  * Synchronization rules: Callers must prevent restarting of the timer,
11985cee9645SThomas Gleixner  * otherwise this function is meaningless. It must not be called from
11995cee9645SThomas Gleixner  * interrupt contexts unless the timer is an irqsafe one. The caller must
12005cee9645SThomas Gleixner  * not hold locks which would prevent completion of the timer's
12015cee9645SThomas Gleixner  * handler. The timer's handler must not call add_timer_on(). Upon exit the
12025cee9645SThomas Gleixner  * timer is not queued and the handler is not running on any CPU.
12035cee9645SThomas Gleixner  *
12045cee9645SThomas Gleixner  * Note: For !irqsafe timers, you must not hold locks that are held in
12055cee9645SThomas Gleixner  *   interrupt context while calling this function. Even if the lock has
12065cee9645SThomas Gleixner  *   nothing to do with the timer in question.  Here's why:
12075cee9645SThomas Gleixner  *
12085cee9645SThomas Gleixner  *    CPU0                             CPU1
12095cee9645SThomas Gleixner  *    ----                             ----
12105cee9645SThomas Gleixner  *                                   <SOFTIRQ>
12115cee9645SThomas Gleixner  *                                   call_timer_fn();
12125cee9645SThomas Gleixner  *                                     base->running_timer = mytimer;
12135cee9645SThomas Gleixner  *  spin_lock_irq(somelock);
12145cee9645SThomas Gleixner  *                                     <IRQ>
12155cee9645SThomas Gleixner  *                                        spin_lock(somelock);
12165cee9645SThomas Gleixner  *  del_timer_sync(mytimer);
12175cee9645SThomas Gleixner  *   while (base->running_timer == mytimer);
12185cee9645SThomas Gleixner  *
12195cee9645SThomas Gleixner  * Now del_timer_sync() will never return and never release somelock.
12205cee9645SThomas Gleixner  * The interrupt on the other CPU is waiting to grab somelock but
12215cee9645SThomas Gleixner  * it has interrupted the softirq that CPU0 is waiting to finish.
12225cee9645SThomas Gleixner  *
12235cee9645SThomas Gleixner  * The function returns whether it has deactivated a pending timer or not.
12245cee9645SThomas Gleixner  */
12255cee9645SThomas Gleixner int del_timer_sync(struct timer_list *timer)
12265cee9645SThomas Gleixner {
12275cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP
12285cee9645SThomas Gleixner 	unsigned long flags;
12295cee9645SThomas Gleixner 
12305cee9645SThomas Gleixner 	/*
12315cee9645SThomas Gleixner 	 * If lockdep gives a backtrace here, please reference
12325cee9645SThomas Gleixner 	 * the synchronization rules above.
12335cee9645SThomas Gleixner 	 */
12345cee9645SThomas Gleixner 	local_irq_save(flags);
12355cee9645SThomas Gleixner 	lock_map_acquire(&timer->lockdep_map);
12365cee9645SThomas Gleixner 	lock_map_release(&timer->lockdep_map);
12375cee9645SThomas Gleixner 	local_irq_restore(flags);
12385cee9645SThomas Gleixner #endif
12395cee9645SThomas Gleixner 	/*
12405cee9645SThomas Gleixner 	 * don't use it in hardirq context, because it
12415cee9645SThomas Gleixner 	 * could lead to deadlock.
12425cee9645SThomas Gleixner 	 */
12430eeda71bSThomas Gleixner 	WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
12445cee9645SThomas Gleixner 	for (;;) {
12455cee9645SThomas Gleixner 		int ret = try_to_del_timer_sync(timer);
12465cee9645SThomas Gleixner 		if (ret >= 0)
12475cee9645SThomas Gleixner 			return ret;
12485cee9645SThomas Gleixner 		cpu_relax();
12495cee9645SThomas Gleixner 	}
12505cee9645SThomas Gleixner }
12515cee9645SThomas Gleixner EXPORT_SYMBOL(del_timer_sync);
12525cee9645SThomas Gleixner #endif
12535cee9645SThomas Gleixner 
12545cee9645SThomas Gleixner static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
12555cee9645SThomas Gleixner 			  unsigned long data)
12565cee9645SThomas Gleixner {
12575cee9645SThomas Gleixner 	int count = preempt_count();
12585cee9645SThomas Gleixner 
12595cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP
12605cee9645SThomas Gleixner 	/*
12615cee9645SThomas Gleixner 	 * It is permissible to free the timer from inside the
12625cee9645SThomas Gleixner 	 * function that is called from it, this we need to take into
12635cee9645SThomas Gleixner 	 * account for lockdep too. To avoid bogus "held lock freed"
12645cee9645SThomas Gleixner 	 * warnings as well as problems when looking into
12655cee9645SThomas Gleixner 	 * timer->lockdep_map, make a copy and use that here.
12665cee9645SThomas Gleixner 	 */
12675cee9645SThomas Gleixner 	struct lockdep_map lockdep_map;
12685cee9645SThomas Gleixner 
12695cee9645SThomas Gleixner 	lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
12705cee9645SThomas Gleixner #endif
12715cee9645SThomas Gleixner 	/*
12725cee9645SThomas Gleixner 	 * Couple the lock chain with the lock chain at
12735cee9645SThomas Gleixner 	 * del_timer_sync() by acquiring the lock_map around the fn()
12745cee9645SThomas Gleixner 	 * call here and in del_timer_sync().
12755cee9645SThomas Gleixner 	 */
12765cee9645SThomas Gleixner 	lock_map_acquire(&lockdep_map);
12775cee9645SThomas Gleixner 
12785cee9645SThomas Gleixner 	trace_timer_expire_entry(timer);
12795cee9645SThomas Gleixner 	fn(data);
12805cee9645SThomas Gleixner 	trace_timer_expire_exit(timer);
12815cee9645SThomas Gleixner 
12825cee9645SThomas Gleixner 	lock_map_release(&lockdep_map);
12835cee9645SThomas Gleixner 
12845cee9645SThomas Gleixner 	if (count != preempt_count()) {
12855cee9645SThomas Gleixner 		WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
12865cee9645SThomas Gleixner 			  fn, count, preempt_count());
12875cee9645SThomas Gleixner 		/*
12885cee9645SThomas Gleixner 		 * Restore the preempt count. That gives us a decent
12895cee9645SThomas Gleixner 		 * chance to survive and extract information. If the
12905cee9645SThomas Gleixner 		 * callback kept a lock held, bad luck, but not worse
12915cee9645SThomas Gleixner 		 * than the BUG() we had.
12925cee9645SThomas Gleixner 		 */
12935cee9645SThomas Gleixner 		preempt_count_set(count);
12945cee9645SThomas Gleixner 	}
12955cee9645SThomas Gleixner }
12965cee9645SThomas Gleixner 
1297500462a9SThomas Gleixner static void expire_timers(struct timer_base *base, struct hlist_head *head)
12985cee9645SThomas Gleixner {
12991dabbcecSThomas Gleixner 	while (!hlist_empty(head)) {
1300500462a9SThomas Gleixner 		struct timer_list *timer;
13015cee9645SThomas Gleixner 		void (*fn)(unsigned long);
13025cee9645SThomas Gleixner 		unsigned long data;
13035cee9645SThomas Gleixner 
13041dabbcecSThomas Gleixner 		timer = hlist_entry(head->first, struct timer_list, entry);
13055cee9645SThomas Gleixner 		timer_stats_account_timer(timer);
13065cee9645SThomas Gleixner 
13075cee9645SThomas Gleixner 		base->running_timer = timer;
1308500462a9SThomas Gleixner 		detach_timer(timer, true);
13095cee9645SThomas Gleixner 
1310500462a9SThomas Gleixner 		fn = timer->function;
1311500462a9SThomas Gleixner 		data = timer->data;
1312500462a9SThomas Gleixner 
1313500462a9SThomas Gleixner 		if (timer->flags & TIMER_IRQSAFE) {
13145cee9645SThomas Gleixner 			spin_unlock(&base->lock);
13155cee9645SThomas Gleixner 			call_timer_fn(timer, fn, data);
13165cee9645SThomas Gleixner 			spin_lock(&base->lock);
13175cee9645SThomas Gleixner 		} else {
13185cee9645SThomas Gleixner 			spin_unlock_irq(&base->lock);
13195cee9645SThomas Gleixner 			call_timer_fn(timer, fn, data);
13205cee9645SThomas Gleixner 			spin_lock_irq(&base->lock);
13215cee9645SThomas Gleixner 		}
13225cee9645SThomas Gleixner 	}
13235cee9645SThomas Gleixner }
1324500462a9SThomas Gleixner 
132523696838SAnna-Maria Gleixner static int __collect_expired_timers(struct timer_base *base,
1326500462a9SThomas Gleixner 				    struct hlist_head *heads)
1327500462a9SThomas Gleixner {
1328500462a9SThomas Gleixner 	unsigned long clk = base->clk;
1329500462a9SThomas Gleixner 	struct hlist_head *vec;
1330500462a9SThomas Gleixner 	int i, levels = 0;
1331500462a9SThomas Gleixner 	unsigned int idx;
1332500462a9SThomas Gleixner 
1333500462a9SThomas Gleixner 	for (i = 0; i < LVL_DEPTH; i++) {
1334500462a9SThomas Gleixner 		idx = (clk & LVL_MASK) + i * LVL_SIZE;
1335500462a9SThomas Gleixner 
1336500462a9SThomas Gleixner 		if (__test_and_clear_bit(idx, base->pending_map)) {
1337500462a9SThomas Gleixner 			vec = base->vectors + idx;
1338500462a9SThomas Gleixner 			hlist_move_list(vec, heads++);
1339500462a9SThomas Gleixner 			levels++;
1340500462a9SThomas Gleixner 		}
1341500462a9SThomas Gleixner 		/* Is it time to look at the next level? */
1342500462a9SThomas Gleixner 		if (clk & LVL_CLK_MASK)
1343500462a9SThomas Gleixner 			break;
1344500462a9SThomas Gleixner 		/* Shift clock for the next level granularity */
1345500462a9SThomas Gleixner 		clk >>= LVL_CLK_SHIFT;
1346500462a9SThomas Gleixner 	}
1347500462a9SThomas Gleixner 	return levels;
1348500462a9SThomas Gleixner }
1349500462a9SThomas Gleixner 
13505cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
13515cee9645SThomas Gleixner /*
135223696838SAnna-Maria Gleixner  * Find the next pending bucket of a level. Search from level start (@offset)
135323696838SAnna-Maria Gleixner  * + @clk upwards and if nothing there, search from start of the level
135423696838SAnna-Maria Gleixner  * (@offset) up to @offset + clk.
1355500462a9SThomas Gleixner  */
1356500462a9SThomas Gleixner static int next_pending_bucket(struct timer_base *base, unsigned offset,
1357500462a9SThomas Gleixner 			       unsigned clk)
1358500462a9SThomas Gleixner {
1359500462a9SThomas Gleixner 	unsigned pos, start = offset + clk;
1360500462a9SThomas Gleixner 	unsigned end = offset + LVL_SIZE;
1361500462a9SThomas Gleixner 
1362500462a9SThomas Gleixner 	pos = find_next_bit(base->pending_map, end, start);
1363500462a9SThomas Gleixner 	if (pos < end)
1364500462a9SThomas Gleixner 		return pos - start;
1365500462a9SThomas Gleixner 
1366500462a9SThomas Gleixner 	pos = find_next_bit(base->pending_map, start, offset);
1367500462a9SThomas Gleixner 	return pos < start ? pos + LVL_SIZE - start : -1;
1368500462a9SThomas Gleixner }
1369500462a9SThomas Gleixner 
1370500462a9SThomas Gleixner /*
137123696838SAnna-Maria Gleixner  * Search the first expiring timer in the various clock levels. Caller must
137223696838SAnna-Maria Gleixner  * hold base->lock.
13735cee9645SThomas Gleixner  */
1374494af3edSThomas Gleixner static unsigned long __next_timer_interrupt(struct timer_base *base)
13755cee9645SThomas Gleixner {
1376500462a9SThomas Gleixner 	unsigned long clk, next, adj;
1377500462a9SThomas Gleixner 	unsigned lvl, offset = 0;
13785cee9645SThomas Gleixner 
1379500462a9SThomas Gleixner 	next = base->clk + NEXT_TIMER_MAX_DELTA;
1380500462a9SThomas Gleixner 	clk = base->clk;
1381500462a9SThomas Gleixner 	for (lvl = 0; lvl < LVL_DEPTH; lvl++, offset += LVL_SIZE) {
1382500462a9SThomas Gleixner 		int pos = next_pending_bucket(base, offset, clk & LVL_MASK);
13835cee9645SThomas Gleixner 
1384500462a9SThomas Gleixner 		if (pos >= 0) {
1385500462a9SThomas Gleixner 			unsigned long tmp = clk + (unsigned long) pos;
13865cee9645SThomas Gleixner 
1387500462a9SThomas Gleixner 			tmp <<= LVL_SHIFT(lvl);
1388500462a9SThomas Gleixner 			if (time_before(tmp, next))
1389500462a9SThomas Gleixner 				next = tmp;
13905cee9645SThomas Gleixner 		}
13915cee9645SThomas Gleixner 		/*
1392500462a9SThomas Gleixner 		 * Clock for the next level. If the current level clock lower
1393500462a9SThomas Gleixner 		 * bits are zero, we look at the next level as is. If not we
1394500462a9SThomas Gleixner 		 * need to advance it by one because that's going to be the
1395500462a9SThomas Gleixner 		 * next expiring bucket in that level. base->clk is the next
1396500462a9SThomas Gleixner 		 * expiring jiffie. So in case of:
1397500462a9SThomas Gleixner 		 *
1398500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1399500462a9SThomas Gleixner 		 *  0    0    0    0    0    0
1400500462a9SThomas Gleixner 		 *
1401500462a9SThomas Gleixner 		 * we have to look at all levels @index 0. With
1402500462a9SThomas Gleixner 		 *
1403500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1404500462a9SThomas Gleixner 		 *  0    0    0    0    0    2
1405500462a9SThomas Gleixner 		 *
1406500462a9SThomas Gleixner 		 * LVL0 has the next expiring bucket @index 2. The upper
1407500462a9SThomas Gleixner 		 * levels have the next expiring bucket @index 1.
1408500462a9SThomas Gleixner 		 *
1409500462a9SThomas Gleixner 		 * In case that the propagation wraps the next level the same
1410500462a9SThomas Gleixner 		 * rules apply:
1411500462a9SThomas Gleixner 		 *
1412500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1413500462a9SThomas Gleixner 		 *  0    0    0    0    F    2
1414500462a9SThomas Gleixner 		 *
1415500462a9SThomas Gleixner 		 * So after looking at LVL0 we get:
1416500462a9SThomas Gleixner 		 *
1417500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1
1418500462a9SThomas Gleixner 		 *  0    0    0    1    0
1419500462a9SThomas Gleixner 		 *
1420500462a9SThomas Gleixner 		 * So no propagation from LVL1 to LVL2 because that happened
1421500462a9SThomas Gleixner 		 * with the add already, but then we need to propagate further
1422500462a9SThomas Gleixner 		 * from LVL2 to LVL3.
1423500462a9SThomas Gleixner 		 *
1424500462a9SThomas Gleixner 		 * So the simple check whether the lower bits of the current
1425500462a9SThomas Gleixner 		 * level are 0 or not is sufficient for all cases.
14265cee9645SThomas Gleixner 		 */
1427500462a9SThomas Gleixner 		adj = clk & LVL_CLK_MASK ? 1 : 0;
1428500462a9SThomas Gleixner 		clk >>= LVL_CLK_SHIFT;
1429500462a9SThomas Gleixner 		clk += adj;
14305cee9645SThomas Gleixner 	}
1431500462a9SThomas Gleixner 	return next;
14325cee9645SThomas Gleixner }
14335cee9645SThomas Gleixner 
14345cee9645SThomas Gleixner /*
14355cee9645SThomas Gleixner  * Check, if the next hrtimer event is before the next timer wheel
14365cee9645SThomas Gleixner  * event:
14375cee9645SThomas Gleixner  */
1438c1ad348bSThomas Gleixner static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
14395cee9645SThomas Gleixner {
1440c1ad348bSThomas Gleixner 	u64 nextevt = hrtimer_get_next_event();
14415cee9645SThomas Gleixner 
1442c1ad348bSThomas Gleixner 	/*
1443c1ad348bSThomas Gleixner 	 * If high resolution timers are enabled
1444c1ad348bSThomas Gleixner 	 * hrtimer_get_next_event() returns KTIME_MAX.
1445c1ad348bSThomas Gleixner 	 */
1446c1ad348bSThomas Gleixner 	if (expires <= nextevt)
14475cee9645SThomas Gleixner 		return expires;
14485cee9645SThomas Gleixner 
14495cee9645SThomas Gleixner 	/*
1450c1ad348bSThomas Gleixner 	 * If the next timer is already expired, return the tick base
1451c1ad348bSThomas Gleixner 	 * time so the tick is fired immediately.
14525cee9645SThomas Gleixner 	 */
1453c1ad348bSThomas Gleixner 	if (nextevt <= basem)
1454c1ad348bSThomas Gleixner 		return basem;
14555cee9645SThomas Gleixner 
14565cee9645SThomas Gleixner 	/*
1457c1ad348bSThomas Gleixner 	 * Round up to the next jiffie. High resolution timers are
1458c1ad348bSThomas Gleixner 	 * off, so the hrtimers are expired in the tick and we need to
1459c1ad348bSThomas Gleixner 	 * make sure that this tick really expires the timer to avoid
1460c1ad348bSThomas Gleixner 	 * a ping pong of the nohz stop code.
1461c1ad348bSThomas Gleixner 	 *
1462c1ad348bSThomas Gleixner 	 * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
14635cee9645SThomas Gleixner 	 */
1464c1ad348bSThomas Gleixner 	return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
14655cee9645SThomas Gleixner }
14665cee9645SThomas Gleixner 
14675cee9645SThomas Gleixner /**
1468c1ad348bSThomas Gleixner  * get_next_timer_interrupt - return the time (clock mono) of the next timer
1469c1ad348bSThomas Gleixner  * @basej:	base time jiffies
1470c1ad348bSThomas Gleixner  * @basem:	base time clock monotonic
1471c1ad348bSThomas Gleixner  *
1472c1ad348bSThomas Gleixner  * Returns the tick aligned clock monotonic time of the next pending
1473c1ad348bSThomas Gleixner  * timer or KTIME_MAX if no timer is pending.
14745cee9645SThomas Gleixner  */
1475c1ad348bSThomas Gleixner u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
14765cee9645SThomas Gleixner {
1477500462a9SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1478c1ad348bSThomas Gleixner 	u64 expires = KTIME_MAX;
1479c1ad348bSThomas Gleixner 	unsigned long nextevt;
14805cee9645SThomas Gleixner 
14815cee9645SThomas Gleixner 	/*
14825cee9645SThomas Gleixner 	 * Pretend that there is no timer pending if the cpu is offline.
14835cee9645SThomas Gleixner 	 * Possible pending timers will be migrated later to an active cpu.
14845cee9645SThomas Gleixner 	 */
14855cee9645SThomas Gleixner 	if (cpu_is_offline(smp_processor_id()))
14865cee9645SThomas Gleixner 		return expires;
14875cee9645SThomas Gleixner 
148823696838SAnna-Maria Gleixner 	spin_lock(&base->lock);
1489500462a9SThomas Gleixner 	nextevt = __next_timer_interrupt(base);
1490a683f390SThomas Gleixner 	base->next_expiry = nextevt;
1491a683f390SThomas Gleixner 	/*
1492a683f390SThomas Gleixner 	 * We have a fresh next event. Check whether we can forward the base:
1493a683f390SThomas Gleixner 	 */
1494a683f390SThomas Gleixner 	if (time_after(nextevt, jiffies))
1495a683f390SThomas Gleixner 		base->clk = jiffies;
1496a683f390SThomas Gleixner 	else if (time_after(nextevt, base->clk))
1497a683f390SThomas Gleixner 		base->clk = nextevt;
1498a683f390SThomas Gleixner 
1499a683f390SThomas Gleixner 	if (time_before_eq(nextevt, basej)) {
1500a683f390SThomas Gleixner 		expires = basem;
1501a683f390SThomas Gleixner 		base->is_idle = false;
1502a683f390SThomas Gleixner 	} else {
1503a683f390SThomas Gleixner 		expires = basem + (nextevt - basej) * TICK_NSEC;
1504a683f390SThomas Gleixner 		/*
1505a683f390SThomas Gleixner 		 * If we expect to sleep more than a tick, mark the base idle:
1506a683f390SThomas Gleixner 		 */
1507a683f390SThomas Gleixner 		if ((expires - basem) > TICK_NSEC)
1508a683f390SThomas Gleixner 			base->is_idle = true;
1509a683f390SThomas Gleixner 	}
151023696838SAnna-Maria Gleixner 	spin_unlock(&base->lock);
151123696838SAnna-Maria Gleixner 
1512c1ad348bSThomas Gleixner 	return cmp_next_hrtimer_event(basem, expires);
15135cee9645SThomas Gleixner }
151423696838SAnna-Maria Gleixner 
1515a683f390SThomas Gleixner /**
1516a683f390SThomas Gleixner  * timer_clear_idle - Clear the idle state of the timer base
1517a683f390SThomas Gleixner  *
1518a683f390SThomas Gleixner  * Called with interrupts disabled
1519a683f390SThomas Gleixner  */
1520a683f390SThomas Gleixner void timer_clear_idle(void)
1521a683f390SThomas Gleixner {
1522a683f390SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1523a683f390SThomas Gleixner 
1524a683f390SThomas Gleixner 	/*
1525a683f390SThomas Gleixner 	 * We do this unlocked. The worst outcome is a remote enqueue sending
1526a683f390SThomas Gleixner 	 * a pointless IPI, but taking the lock would just make the window for
1527a683f390SThomas Gleixner 	 * sending the IPI a few instructions smaller for the cost of taking
1528a683f390SThomas Gleixner 	 * the lock in the exit from idle path.
1529a683f390SThomas Gleixner 	 */
1530a683f390SThomas Gleixner 	base->is_idle = false;
1531a683f390SThomas Gleixner }
1532a683f390SThomas Gleixner 
153323696838SAnna-Maria Gleixner static int collect_expired_timers(struct timer_base *base,
153423696838SAnna-Maria Gleixner 				  struct hlist_head *heads)
153523696838SAnna-Maria Gleixner {
153623696838SAnna-Maria Gleixner 	/*
153723696838SAnna-Maria Gleixner 	 * NOHZ optimization. After a long idle sleep we need to forward the
153823696838SAnna-Maria Gleixner 	 * base to current jiffies. Avoid a loop by searching the bitfield for
153923696838SAnna-Maria Gleixner 	 * the next expiring timer.
154023696838SAnna-Maria Gleixner 	 */
154123696838SAnna-Maria Gleixner 	if ((long)(jiffies - base->clk) > 2) {
154223696838SAnna-Maria Gleixner 		unsigned long next = __next_timer_interrupt(base);
154323696838SAnna-Maria Gleixner 
154423696838SAnna-Maria Gleixner 		/*
154523696838SAnna-Maria Gleixner 		 * If the next timer is ahead of time forward to current
1546a683f390SThomas Gleixner 		 * jiffies, otherwise forward to the next expiry time:
154723696838SAnna-Maria Gleixner 		 */
154823696838SAnna-Maria Gleixner 		if (time_after(next, jiffies)) {
154923696838SAnna-Maria Gleixner 			/* The call site will increment clock! */
155023696838SAnna-Maria Gleixner 			base->clk = jiffies - 1;
155123696838SAnna-Maria Gleixner 			return 0;
155223696838SAnna-Maria Gleixner 		}
155323696838SAnna-Maria Gleixner 		base->clk = next;
155423696838SAnna-Maria Gleixner 	}
155523696838SAnna-Maria Gleixner 	return __collect_expired_timers(base, heads);
155623696838SAnna-Maria Gleixner }
155723696838SAnna-Maria Gleixner #else
155823696838SAnna-Maria Gleixner static inline int collect_expired_timers(struct timer_base *base,
155923696838SAnna-Maria Gleixner 					 struct hlist_head *heads)
156023696838SAnna-Maria Gleixner {
156123696838SAnna-Maria Gleixner 	return __collect_expired_timers(base, heads);
156223696838SAnna-Maria Gleixner }
15635cee9645SThomas Gleixner #endif
15645cee9645SThomas Gleixner 
15655cee9645SThomas Gleixner /*
15665cee9645SThomas Gleixner  * Called from the timer interrupt handler to charge one tick to the current
15675cee9645SThomas Gleixner  * process.  user_tick is 1 if the tick is user time, 0 for system.
15685cee9645SThomas Gleixner  */
15695cee9645SThomas Gleixner void update_process_times(int user_tick)
15705cee9645SThomas Gleixner {
15715cee9645SThomas Gleixner 	struct task_struct *p = current;
15725cee9645SThomas Gleixner 
15735cee9645SThomas Gleixner 	/* Note: this timer irq context must be accounted for as well. */
15745cee9645SThomas Gleixner 	account_process_tick(p, user_tick);
15755cee9645SThomas Gleixner 	run_local_timers();
1576c3377c2dSPaul E. McKenney 	rcu_check_callbacks(user_tick);
15775cee9645SThomas Gleixner #ifdef CONFIG_IRQ_WORK
15785cee9645SThomas Gleixner 	if (in_irq())
157976a33061SFrederic Weisbecker 		irq_work_tick();
15805cee9645SThomas Gleixner #endif
15815cee9645SThomas Gleixner 	scheduler_tick();
15825cee9645SThomas Gleixner 	run_posix_cpu_timers(p);
15835cee9645SThomas Gleixner }
15845cee9645SThomas Gleixner 
158573420feaSAnna-Maria Gleixner /**
158673420feaSAnna-Maria Gleixner  * __run_timers - run all expired timers (if any) on this CPU.
158773420feaSAnna-Maria Gleixner  * @base: the timer vector to be processed.
158873420feaSAnna-Maria Gleixner  */
158973420feaSAnna-Maria Gleixner static inline void __run_timers(struct timer_base *base)
159073420feaSAnna-Maria Gleixner {
159173420feaSAnna-Maria Gleixner 	struct hlist_head heads[LVL_DEPTH];
159273420feaSAnna-Maria Gleixner 	int levels;
159373420feaSAnna-Maria Gleixner 
159473420feaSAnna-Maria Gleixner 	if (!time_after_eq(jiffies, base->clk))
159573420feaSAnna-Maria Gleixner 		return;
159673420feaSAnna-Maria Gleixner 
159773420feaSAnna-Maria Gleixner 	spin_lock_irq(&base->lock);
159873420feaSAnna-Maria Gleixner 
159973420feaSAnna-Maria Gleixner 	while (time_after_eq(jiffies, base->clk)) {
160073420feaSAnna-Maria Gleixner 
160173420feaSAnna-Maria Gleixner 		levels = collect_expired_timers(base, heads);
160273420feaSAnna-Maria Gleixner 		base->clk++;
160373420feaSAnna-Maria Gleixner 
160473420feaSAnna-Maria Gleixner 		while (levels--)
160573420feaSAnna-Maria Gleixner 			expire_timers(base, heads + levels);
160673420feaSAnna-Maria Gleixner 	}
160773420feaSAnna-Maria Gleixner 	base->running_timer = NULL;
160873420feaSAnna-Maria Gleixner 	spin_unlock_irq(&base->lock);
160973420feaSAnna-Maria Gleixner }
161073420feaSAnna-Maria Gleixner 
16115cee9645SThomas Gleixner /*
16125cee9645SThomas Gleixner  * This function runs timers and the timer-tq in bottom half context.
16135cee9645SThomas Gleixner  */
16145cee9645SThomas Gleixner static void run_timer_softirq(struct softirq_action *h)
16155cee9645SThomas Gleixner {
1616500462a9SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
16175cee9645SThomas Gleixner 
16185cee9645SThomas Gleixner 	__run_timers(base);
1619500462a9SThomas Gleixner 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active)
1620500462a9SThomas Gleixner 		__run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
16215cee9645SThomas Gleixner }
16225cee9645SThomas Gleixner 
16235cee9645SThomas Gleixner /*
16245cee9645SThomas Gleixner  * Called by the local, per-CPU timer interrupt on SMP.
16255cee9645SThomas Gleixner  */
16265cee9645SThomas Gleixner void run_local_timers(void)
16275cee9645SThomas Gleixner {
16284e85876aSThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
16294e85876aSThomas Gleixner 
16305cee9645SThomas Gleixner 	hrtimer_run_queues();
16314e85876aSThomas Gleixner 	/* Raise the softirq only if required. */
16324e85876aSThomas Gleixner 	if (time_before(jiffies, base->clk)) {
16334e85876aSThomas Gleixner 		if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active)
16344e85876aSThomas Gleixner 			return;
16354e85876aSThomas Gleixner 		/* CPU is awake, so check the deferrable base. */
16364e85876aSThomas Gleixner 		base++;
16374e85876aSThomas Gleixner 		if (time_before(jiffies, base->clk))
16384e85876aSThomas Gleixner 			return;
16394e85876aSThomas Gleixner 	}
16405cee9645SThomas Gleixner 	raise_softirq(TIMER_SOFTIRQ);
16415cee9645SThomas Gleixner }
16425cee9645SThomas Gleixner 
16435cee9645SThomas Gleixner #ifdef __ARCH_WANT_SYS_ALARM
16445cee9645SThomas Gleixner 
16455cee9645SThomas Gleixner /*
16465cee9645SThomas Gleixner  * For backwards compatibility?  This can be done in libc so Alpha
16475cee9645SThomas Gleixner  * and all newer ports shouldn't need it.
16485cee9645SThomas Gleixner  */
16495cee9645SThomas Gleixner SYSCALL_DEFINE1(alarm, unsigned int, seconds)
16505cee9645SThomas Gleixner {
16515cee9645SThomas Gleixner 	return alarm_setitimer(seconds);
16525cee9645SThomas Gleixner }
16535cee9645SThomas Gleixner 
16545cee9645SThomas Gleixner #endif
16555cee9645SThomas Gleixner 
16565cee9645SThomas Gleixner static void process_timeout(unsigned long __data)
16575cee9645SThomas Gleixner {
16585cee9645SThomas Gleixner 	wake_up_process((struct task_struct *)__data);
16595cee9645SThomas Gleixner }
16605cee9645SThomas Gleixner 
16615cee9645SThomas Gleixner /**
16625cee9645SThomas Gleixner  * schedule_timeout - sleep until timeout
16635cee9645SThomas Gleixner  * @timeout: timeout value in jiffies
16645cee9645SThomas Gleixner  *
16655cee9645SThomas Gleixner  * Make the current task sleep until @timeout jiffies have
16665cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
16675cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
16685cee9645SThomas Gleixner  *
16695cee9645SThomas Gleixner  * You can set the task state as follows -
16705cee9645SThomas Gleixner  *
16715cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
16725cee9645SThomas Gleixner  * pass before the routine returns. The routine will return 0
16735cee9645SThomas Gleixner  *
16745cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
16755cee9645SThomas Gleixner  * delivered to the current task. In this case the remaining time
16765cee9645SThomas Gleixner  * in jiffies will be returned, or 0 if the timer expired in time
16775cee9645SThomas Gleixner  *
16785cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
16795cee9645SThomas Gleixner  * routine returns.
16805cee9645SThomas Gleixner  *
16815cee9645SThomas Gleixner  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
16825cee9645SThomas Gleixner  * the CPU away without a bound on the timeout. In this case the return
16835cee9645SThomas Gleixner  * value will be %MAX_SCHEDULE_TIMEOUT.
16845cee9645SThomas Gleixner  *
16855cee9645SThomas Gleixner  * In all cases the return value is guaranteed to be non-negative.
16865cee9645SThomas Gleixner  */
16875cee9645SThomas Gleixner signed long __sched schedule_timeout(signed long timeout)
16885cee9645SThomas Gleixner {
16895cee9645SThomas Gleixner 	struct timer_list timer;
16905cee9645SThomas Gleixner 	unsigned long expire;
16915cee9645SThomas Gleixner 
16925cee9645SThomas Gleixner 	switch (timeout)
16935cee9645SThomas Gleixner 	{
16945cee9645SThomas Gleixner 	case MAX_SCHEDULE_TIMEOUT:
16955cee9645SThomas Gleixner 		/*
16965cee9645SThomas Gleixner 		 * These two special cases are useful to be comfortable
16975cee9645SThomas Gleixner 		 * in the caller. Nothing more. We could take
16985cee9645SThomas Gleixner 		 * MAX_SCHEDULE_TIMEOUT from one of the negative value
16995cee9645SThomas Gleixner 		 * but I' d like to return a valid offset (>=0) to allow
17005cee9645SThomas Gleixner 		 * the caller to do everything it want with the retval.
17015cee9645SThomas Gleixner 		 */
17025cee9645SThomas Gleixner 		schedule();
17035cee9645SThomas Gleixner 		goto out;
17045cee9645SThomas Gleixner 	default:
17055cee9645SThomas Gleixner 		/*
17065cee9645SThomas Gleixner 		 * Another bit of PARANOID. Note that the retval will be
17075cee9645SThomas Gleixner 		 * 0 since no piece of kernel is supposed to do a check
17085cee9645SThomas Gleixner 		 * for a negative retval of schedule_timeout() (since it
17095cee9645SThomas Gleixner 		 * should never happens anyway). You just have the printk()
17105cee9645SThomas Gleixner 		 * that will tell you if something is gone wrong and where.
17115cee9645SThomas Gleixner 		 */
17125cee9645SThomas Gleixner 		if (timeout < 0) {
17135cee9645SThomas Gleixner 			printk(KERN_ERR "schedule_timeout: wrong timeout "
17145cee9645SThomas Gleixner 				"value %lx\n", timeout);
17155cee9645SThomas Gleixner 			dump_stack();
17165cee9645SThomas Gleixner 			current->state = TASK_RUNNING;
17175cee9645SThomas Gleixner 			goto out;
17185cee9645SThomas Gleixner 		}
17195cee9645SThomas Gleixner 	}
17205cee9645SThomas Gleixner 
17215cee9645SThomas Gleixner 	expire = timeout + jiffies;
17225cee9645SThomas Gleixner 
17235cee9645SThomas Gleixner 	setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
1724177ec0a0SThomas Gleixner 	__mod_timer(&timer, expire, false);
17255cee9645SThomas Gleixner 	schedule();
17265cee9645SThomas Gleixner 	del_singleshot_timer_sync(&timer);
17275cee9645SThomas Gleixner 
17285cee9645SThomas Gleixner 	/* Remove the timer from the object tracker */
17295cee9645SThomas Gleixner 	destroy_timer_on_stack(&timer);
17305cee9645SThomas Gleixner 
17315cee9645SThomas Gleixner 	timeout = expire - jiffies;
17325cee9645SThomas Gleixner 
17335cee9645SThomas Gleixner  out:
17345cee9645SThomas Gleixner 	return timeout < 0 ? 0 : timeout;
17355cee9645SThomas Gleixner }
17365cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout);
17375cee9645SThomas Gleixner 
17385cee9645SThomas Gleixner /*
17395cee9645SThomas Gleixner  * We can use __set_current_state() here because schedule_timeout() calls
17405cee9645SThomas Gleixner  * schedule() unconditionally.
17415cee9645SThomas Gleixner  */
17425cee9645SThomas Gleixner signed long __sched schedule_timeout_interruptible(signed long timeout)
17435cee9645SThomas Gleixner {
17445cee9645SThomas Gleixner 	__set_current_state(TASK_INTERRUPTIBLE);
17455cee9645SThomas Gleixner 	return schedule_timeout(timeout);
17465cee9645SThomas Gleixner }
17475cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_interruptible);
17485cee9645SThomas Gleixner 
17495cee9645SThomas Gleixner signed long __sched schedule_timeout_killable(signed long timeout)
17505cee9645SThomas Gleixner {
17515cee9645SThomas Gleixner 	__set_current_state(TASK_KILLABLE);
17525cee9645SThomas Gleixner 	return schedule_timeout(timeout);
17535cee9645SThomas Gleixner }
17545cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_killable);
17555cee9645SThomas Gleixner 
17565cee9645SThomas Gleixner signed long __sched schedule_timeout_uninterruptible(signed long timeout)
17575cee9645SThomas Gleixner {
17585cee9645SThomas Gleixner 	__set_current_state(TASK_UNINTERRUPTIBLE);
17595cee9645SThomas Gleixner 	return schedule_timeout(timeout);
17605cee9645SThomas Gleixner }
17615cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_uninterruptible);
17625cee9645SThomas Gleixner 
176369b27bafSAndrew Morton /*
176469b27bafSAndrew Morton  * Like schedule_timeout_uninterruptible(), except this task will not contribute
176569b27bafSAndrew Morton  * to load average.
176669b27bafSAndrew Morton  */
176769b27bafSAndrew Morton signed long __sched schedule_timeout_idle(signed long timeout)
176869b27bafSAndrew Morton {
176969b27bafSAndrew Morton 	__set_current_state(TASK_IDLE);
177069b27bafSAndrew Morton 	return schedule_timeout(timeout);
177169b27bafSAndrew Morton }
177269b27bafSAndrew Morton EXPORT_SYMBOL(schedule_timeout_idle);
177369b27bafSAndrew Morton 
17745cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
1775494af3edSThomas Gleixner static void migrate_timer_list(struct timer_base *new_base, struct hlist_head *head)
17765cee9645SThomas Gleixner {
17775cee9645SThomas Gleixner 	struct timer_list *timer;
17780eeda71bSThomas Gleixner 	int cpu = new_base->cpu;
17795cee9645SThomas Gleixner 
17801dabbcecSThomas Gleixner 	while (!hlist_empty(head)) {
17811dabbcecSThomas Gleixner 		timer = hlist_entry(head->first, struct timer_list, entry);
17825cee9645SThomas Gleixner 		detach_timer(timer, false);
17830eeda71bSThomas Gleixner 		timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
17845cee9645SThomas Gleixner 		internal_add_timer(new_base, timer);
17855cee9645SThomas Gleixner 	}
17865cee9645SThomas Gleixner }
17875cee9645SThomas Gleixner 
17885cee9645SThomas Gleixner static void migrate_timers(int cpu)
17895cee9645SThomas Gleixner {
1790494af3edSThomas Gleixner 	struct timer_base *old_base;
1791494af3edSThomas Gleixner 	struct timer_base *new_base;
1792500462a9SThomas Gleixner 	int b, i;
17935cee9645SThomas Gleixner 
17945cee9645SThomas Gleixner 	BUG_ON(cpu_online(cpu));
1795500462a9SThomas Gleixner 
1796500462a9SThomas Gleixner 	for (b = 0; b < NR_BASES; b++) {
1797500462a9SThomas Gleixner 		old_base = per_cpu_ptr(&timer_bases[b], cpu);
1798500462a9SThomas Gleixner 		new_base = get_cpu_ptr(&timer_bases[b]);
17995cee9645SThomas Gleixner 		/*
18005cee9645SThomas Gleixner 		 * The caller is globally serialized and nobody else
18015cee9645SThomas Gleixner 		 * takes two locks at once, deadlock is not possible.
18025cee9645SThomas Gleixner 		 */
18035cee9645SThomas Gleixner 		spin_lock_irq(&new_base->lock);
18045cee9645SThomas Gleixner 		spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
18055cee9645SThomas Gleixner 
18065cee9645SThomas Gleixner 		BUG_ON(old_base->running_timer);
18075cee9645SThomas Gleixner 
1808500462a9SThomas Gleixner 		for (i = 0; i < WHEEL_SIZE; i++)
1809500462a9SThomas Gleixner 			migrate_timer_list(new_base, old_base->vectors + i);
18108def9060SViresh Kumar 
18115cee9645SThomas Gleixner 		spin_unlock(&old_base->lock);
18125cee9645SThomas Gleixner 		spin_unlock_irq(&new_base->lock);
1813494af3edSThomas Gleixner 		put_cpu_ptr(&timer_bases);
18145cee9645SThomas Gleixner 	}
1815500462a9SThomas Gleixner }
18165cee9645SThomas Gleixner 
18175cee9645SThomas Gleixner static int timer_cpu_notify(struct notifier_block *self,
18185cee9645SThomas Gleixner 				unsigned long action, void *hcpu)
18195cee9645SThomas Gleixner {
18208def9060SViresh Kumar 	switch (action) {
18215cee9645SThomas Gleixner 	case CPU_DEAD:
18225cee9645SThomas Gleixner 	case CPU_DEAD_FROZEN:
18238def9060SViresh Kumar 		migrate_timers((long)hcpu);
18245cee9645SThomas Gleixner 		break;
18255cee9645SThomas Gleixner 	default:
18265cee9645SThomas Gleixner 		break;
18275cee9645SThomas Gleixner 	}
18283650b57fSPeter Zijlstra 
18295cee9645SThomas Gleixner 	return NOTIFY_OK;
18305cee9645SThomas Gleixner }
18315cee9645SThomas Gleixner 
18323650b57fSPeter Zijlstra static inline void timer_register_cpu_notifier(void)
18333650b57fSPeter Zijlstra {
18343650b57fSPeter Zijlstra 	cpu_notifier(timer_cpu_notify, 0);
18353650b57fSPeter Zijlstra }
18363650b57fSPeter Zijlstra #else
18373650b57fSPeter Zijlstra static inline void timer_register_cpu_notifier(void) { }
18383650b57fSPeter Zijlstra #endif /* CONFIG_HOTPLUG_CPU */
18395cee9645SThomas Gleixner 
18400eeda71bSThomas Gleixner static void __init init_timer_cpu(int cpu)
18418def9060SViresh Kumar {
1842500462a9SThomas Gleixner 	struct timer_base *base;
1843500462a9SThomas Gleixner 	int i;
18443650b57fSPeter Zijlstra 
1845500462a9SThomas Gleixner 	for (i = 0; i < NR_BASES; i++) {
1846500462a9SThomas Gleixner 		base = per_cpu_ptr(&timer_bases[i], cpu);
18478def9060SViresh Kumar 		base->cpu = cpu;
18488def9060SViresh Kumar 		spin_lock_init(&base->lock);
1849494af3edSThomas Gleixner 		base->clk = jiffies;
1850500462a9SThomas Gleixner 	}
18518def9060SViresh Kumar }
18528def9060SViresh Kumar 
18538def9060SViresh Kumar static void __init init_timer_cpus(void)
18548def9060SViresh Kumar {
18558def9060SViresh Kumar 	int cpu;
18568def9060SViresh Kumar 
18570eeda71bSThomas Gleixner 	for_each_possible_cpu(cpu)
18580eeda71bSThomas Gleixner 		init_timer_cpu(cpu);
18598def9060SViresh Kumar }
18605cee9645SThomas Gleixner 
18615cee9645SThomas Gleixner void __init init_timers(void)
18625cee9645SThomas Gleixner {
18638def9060SViresh Kumar 	init_timer_cpus();
18645cee9645SThomas Gleixner 	init_timer_stats();
18653650b57fSPeter Zijlstra 	timer_register_cpu_notifier();
18665cee9645SThomas Gleixner 	open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
18675cee9645SThomas Gleixner }
18685cee9645SThomas Gleixner 
18695cee9645SThomas Gleixner /**
18705cee9645SThomas Gleixner  * msleep - sleep safely even with waitqueue interruptions
18715cee9645SThomas Gleixner  * @msecs: Time in milliseconds to sleep for
18725cee9645SThomas Gleixner  */
18735cee9645SThomas Gleixner void msleep(unsigned int msecs)
18745cee9645SThomas Gleixner {
18755cee9645SThomas Gleixner 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
18765cee9645SThomas Gleixner 
18775cee9645SThomas Gleixner 	while (timeout)
18785cee9645SThomas Gleixner 		timeout = schedule_timeout_uninterruptible(timeout);
18795cee9645SThomas Gleixner }
18805cee9645SThomas Gleixner 
18815cee9645SThomas Gleixner EXPORT_SYMBOL(msleep);
18825cee9645SThomas Gleixner 
18835cee9645SThomas Gleixner /**
18845cee9645SThomas Gleixner  * msleep_interruptible - sleep waiting for signals
18855cee9645SThomas Gleixner  * @msecs: Time in milliseconds to sleep for
18865cee9645SThomas Gleixner  */
18875cee9645SThomas Gleixner unsigned long msleep_interruptible(unsigned int msecs)
18885cee9645SThomas Gleixner {
18895cee9645SThomas Gleixner 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
18905cee9645SThomas Gleixner 
18915cee9645SThomas Gleixner 	while (timeout && !signal_pending(current))
18925cee9645SThomas Gleixner 		timeout = schedule_timeout_interruptible(timeout);
18935cee9645SThomas Gleixner 	return jiffies_to_msecs(timeout);
18945cee9645SThomas Gleixner }
18955cee9645SThomas Gleixner 
18965cee9645SThomas Gleixner EXPORT_SYMBOL(msleep_interruptible);
18975cee9645SThomas Gleixner 
18986deba083SThomas Gleixner static void __sched do_usleep_range(unsigned long min, unsigned long max)
18995cee9645SThomas Gleixner {
19005cee9645SThomas Gleixner 	ktime_t kmin;
1901da8b44d5SJohn Stultz 	u64 delta;
19025cee9645SThomas Gleixner 
19035cee9645SThomas Gleixner 	kmin = ktime_set(0, min * NSEC_PER_USEC);
1904da8b44d5SJohn Stultz 	delta = (u64)(max - min) * NSEC_PER_USEC;
19056deba083SThomas Gleixner 	schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL);
19065cee9645SThomas Gleixner }
19075cee9645SThomas Gleixner 
19085cee9645SThomas Gleixner /**
19095cee9645SThomas Gleixner  * usleep_range - Drop in replacement for udelay where wakeup is flexible
19105cee9645SThomas Gleixner  * @min: Minimum time in usecs to sleep
19115cee9645SThomas Gleixner  * @max: Maximum time in usecs to sleep
19125cee9645SThomas Gleixner  */
19132ad5d327SThomas Gleixner void __sched usleep_range(unsigned long min, unsigned long max)
19145cee9645SThomas Gleixner {
19155cee9645SThomas Gleixner 	__set_current_state(TASK_UNINTERRUPTIBLE);
19165cee9645SThomas Gleixner 	do_usleep_range(min, max);
19175cee9645SThomas Gleixner }
19185cee9645SThomas Gleixner EXPORT_SYMBOL(usleep_range);
1919