xref: /linux-6.15/kernel/time/timer.c (revision c1eba5bc)
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>
41174cd4b1SIngo Molnar #include <linux/sched/signal.h>
425cee9645SThomas Gleixner #include <linux/sched/sysctl.h>
43370c9135SIngo Molnar #include <linux/sched/nohz.h>
44b17b0153SIngo Molnar #include <linux/sched/debug.h>
455cee9645SThomas Gleixner #include <linux/slab.h>
465cee9645SThomas Gleixner #include <linux/compat.h>
475cee9645SThomas Gleixner 
487c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
495cee9645SThomas Gleixner #include <asm/unistd.h>
505cee9645SThomas Gleixner #include <asm/div64.h>
515cee9645SThomas Gleixner #include <asm/timex.h>
525cee9645SThomas Gleixner #include <asm/io.h>
535cee9645SThomas Gleixner 
54c1ad348bSThomas Gleixner #include "tick-internal.h"
55c1ad348bSThomas Gleixner 
565cee9645SThomas Gleixner #define CREATE_TRACE_POINTS
575cee9645SThomas Gleixner #include <trace/events/timer.h>
585cee9645SThomas Gleixner 
595cee9645SThomas Gleixner __visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
605cee9645SThomas Gleixner 
615cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64);
625cee9645SThomas Gleixner 
635cee9645SThomas Gleixner /*
64500462a9SThomas Gleixner  * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
65500462a9SThomas Gleixner  * LVL_SIZE buckets. Each level is driven by its own clock and therefor each
66500462a9SThomas Gleixner  * level has a different granularity.
67500462a9SThomas Gleixner  *
68500462a9SThomas Gleixner  * The level granularity is:		LVL_CLK_DIV ^ lvl
69500462a9SThomas Gleixner  * The level clock frequency is:	HZ / (LVL_CLK_DIV ^ level)
70500462a9SThomas Gleixner  *
71500462a9SThomas Gleixner  * The array level of a newly armed timer depends on the relative expiry
72500462a9SThomas Gleixner  * time. The farther the expiry time is away the higher the array level and
73500462a9SThomas Gleixner  * therefor the granularity becomes.
74500462a9SThomas Gleixner  *
75500462a9SThomas Gleixner  * Contrary to the original timer wheel implementation, which aims for 'exact'
76500462a9SThomas Gleixner  * expiry of the timers, this implementation removes the need for recascading
77500462a9SThomas Gleixner  * the timers into the lower array levels. The previous 'classic' timer wheel
78500462a9SThomas Gleixner  * implementation of the kernel already violated the 'exact' expiry by adding
79500462a9SThomas Gleixner  * slack to the expiry time to provide batched expiration. The granularity
80500462a9SThomas Gleixner  * levels provide implicit batching.
81500462a9SThomas Gleixner  *
82500462a9SThomas Gleixner  * This is an optimization of the original timer wheel implementation for the
83500462a9SThomas Gleixner  * majority of the timer wheel use cases: timeouts. The vast majority of
84500462a9SThomas Gleixner  * timeout timers (networking, disk I/O ...) are canceled before expiry. If
85500462a9SThomas Gleixner  * the timeout expires it indicates that normal operation is disturbed, so it
86500462a9SThomas Gleixner  * does not matter much whether the timeout comes with a slight delay.
87500462a9SThomas Gleixner  *
88500462a9SThomas Gleixner  * The only exception to this are networking timers with a small expiry
89500462a9SThomas Gleixner  * time. They rely on the granularity. Those fit into the first wheel level,
90500462a9SThomas Gleixner  * which has HZ granularity.
91500462a9SThomas Gleixner  *
92500462a9SThomas Gleixner  * We don't have cascading anymore. timers with a expiry time above the
93500462a9SThomas Gleixner  * capacity of the last wheel level are force expired at the maximum timeout
94500462a9SThomas Gleixner  * value of the last wheel level. From data sampling we know that the maximum
95500462a9SThomas Gleixner  * value observed is 5 days (network connection tracking), so this should not
96500462a9SThomas Gleixner  * be an issue.
97500462a9SThomas Gleixner  *
98500462a9SThomas Gleixner  * The currently chosen array constants values are a good compromise between
99500462a9SThomas Gleixner  * array size and granularity.
100500462a9SThomas Gleixner  *
101500462a9SThomas Gleixner  * This results in the following granularity and range levels:
102500462a9SThomas Gleixner  *
103500462a9SThomas Gleixner  * HZ 1000 steps
104500462a9SThomas Gleixner  * Level Offset  Granularity            Range
105500462a9SThomas Gleixner  *  0      0         1 ms                0 ms -         63 ms
106500462a9SThomas Gleixner  *  1     64         8 ms               64 ms -        511 ms
107500462a9SThomas Gleixner  *  2    128        64 ms              512 ms -       4095 ms (512ms - ~4s)
108500462a9SThomas Gleixner  *  3    192       512 ms             4096 ms -      32767 ms (~4s - ~32s)
109500462a9SThomas Gleixner  *  4    256      4096 ms (~4s)      32768 ms -     262143 ms (~32s - ~4m)
110500462a9SThomas Gleixner  *  5    320     32768 ms (~32s)    262144 ms -    2097151 ms (~4m - ~34m)
111500462a9SThomas Gleixner  *  6    384    262144 ms (~4m)    2097152 ms -   16777215 ms (~34m - ~4h)
112500462a9SThomas Gleixner  *  7    448   2097152 ms (~34m)  16777216 ms -  134217727 ms (~4h - ~1d)
113500462a9SThomas Gleixner  *  8    512  16777216 ms (~4h)  134217728 ms - 1073741822 ms (~1d - ~12d)
114500462a9SThomas Gleixner  *
115500462a9SThomas Gleixner  * HZ  300
116500462a9SThomas Gleixner  * Level Offset  Granularity            Range
117500462a9SThomas Gleixner  *  0	   0         3 ms                0 ms -        210 ms
118500462a9SThomas Gleixner  *  1	  64        26 ms              213 ms -       1703 ms (213ms - ~1s)
119500462a9SThomas Gleixner  *  2	 128       213 ms             1706 ms -      13650 ms (~1s - ~13s)
120500462a9SThomas Gleixner  *  3	 192      1706 ms (~1s)      13653 ms -     109223 ms (~13s - ~1m)
121500462a9SThomas Gleixner  *  4	 256     13653 ms (~13s)    109226 ms -     873810 ms (~1m - ~14m)
122500462a9SThomas Gleixner  *  5	 320    109226 ms (~1m)     873813 ms -    6990503 ms (~14m - ~1h)
123500462a9SThomas Gleixner  *  6	 384    873813 ms (~14m)   6990506 ms -   55924050 ms (~1h - ~15h)
124500462a9SThomas Gleixner  *  7	 448   6990506 ms (~1h)   55924053 ms -  447392423 ms (~15h - ~5d)
125500462a9SThomas Gleixner  *  8    512  55924053 ms (~15h) 447392426 ms - 3579139406 ms (~5d - ~41d)
126500462a9SThomas Gleixner  *
127500462a9SThomas Gleixner  * HZ  250
128500462a9SThomas Gleixner  * Level Offset  Granularity            Range
129500462a9SThomas Gleixner  *  0	   0         4 ms                0 ms -        255 ms
130500462a9SThomas Gleixner  *  1	  64        32 ms              256 ms -       2047 ms (256ms - ~2s)
131500462a9SThomas Gleixner  *  2	 128       256 ms             2048 ms -      16383 ms (~2s - ~16s)
132500462a9SThomas Gleixner  *  3	 192      2048 ms (~2s)      16384 ms -     131071 ms (~16s - ~2m)
133500462a9SThomas Gleixner  *  4	 256     16384 ms (~16s)    131072 ms -    1048575 ms (~2m - ~17m)
134500462a9SThomas Gleixner  *  5	 320    131072 ms (~2m)    1048576 ms -    8388607 ms (~17m - ~2h)
135500462a9SThomas Gleixner  *  6	 384   1048576 ms (~17m)   8388608 ms -   67108863 ms (~2h - ~18h)
136500462a9SThomas Gleixner  *  7	 448   8388608 ms (~2h)   67108864 ms -  536870911 ms (~18h - ~6d)
137500462a9SThomas Gleixner  *  8    512  67108864 ms (~18h) 536870912 ms - 4294967288 ms (~6d - ~49d)
138500462a9SThomas Gleixner  *
139500462a9SThomas Gleixner  * HZ  100
140500462a9SThomas Gleixner  * Level Offset  Granularity            Range
141500462a9SThomas Gleixner  *  0	   0         10 ms               0 ms -        630 ms
142500462a9SThomas Gleixner  *  1	  64         80 ms             640 ms -       5110 ms (640ms - ~5s)
143500462a9SThomas Gleixner  *  2	 128        640 ms            5120 ms -      40950 ms (~5s - ~40s)
144500462a9SThomas Gleixner  *  3	 192       5120 ms (~5s)     40960 ms -     327670 ms (~40s - ~5m)
145500462a9SThomas Gleixner  *  4	 256      40960 ms (~40s)   327680 ms -    2621430 ms (~5m - ~43m)
146500462a9SThomas Gleixner  *  5	 320     327680 ms (~5m)   2621440 ms -   20971510 ms (~43m - ~5h)
147500462a9SThomas Gleixner  *  6	 384    2621440 ms (~43m) 20971520 ms -  167772150 ms (~5h - ~1d)
148500462a9SThomas Gleixner  *  7	 448   20971520 ms (~5h) 167772160 ms - 1342177270 ms (~1d - ~15d)
1495cee9645SThomas Gleixner  */
1505cee9645SThomas Gleixner 
151500462a9SThomas Gleixner /* Clock divisor for the next level */
152500462a9SThomas Gleixner #define LVL_CLK_SHIFT	3
153500462a9SThomas Gleixner #define LVL_CLK_DIV	(1UL << LVL_CLK_SHIFT)
154500462a9SThomas Gleixner #define LVL_CLK_MASK	(LVL_CLK_DIV - 1)
155500462a9SThomas Gleixner #define LVL_SHIFT(n)	((n) * LVL_CLK_SHIFT)
156500462a9SThomas Gleixner #define LVL_GRAN(n)	(1UL << LVL_SHIFT(n))
1575cee9645SThomas Gleixner 
158500462a9SThomas Gleixner /*
159500462a9SThomas Gleixner  * The time start value for each level to select the bucket at enqueue
160500462a9SThomas Gleixner  * time.
161500462a9SThomas Gleixner  */
162500462a9SThomas Gleixner #define LVL_START(n)	((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
1635cee9645SThomas Gleixner 
164500462a9SThomas Gleixner /* Size of each clock level */
165500462a9SThomas Gleixner #define LVL_BITS	6
166500462a9SThomas Gleixner #define LVL_SIZE	(1UL << LVL_BITS)
167500462a9SThomas Gleixner #define LVL_MASK	(LVL_SIZE - 1)
168500462a9SThomas Gleixner #define LVL_OFFS(n)	((n) * LVL_SIZE)
169500462a9SThomas Gleixner 
170500462a9SThomas Gleixner /* Level depth */
171500462a9SThomas Gleixner #if HZ > 100
172500462a9SThomas Gleixner # define LVL_DEPTH	9
173500462a9SThomas Gleixner # else
174500462a9SThomas Gleixner # define LVL_DEPTH	8
175500462a9SThomas Gleixner #endif
176500462a9SThomas Gleixner 
177500462a9SThomas Gleixner /* The cutoff (max. capacity of the wheel) */
178500462a9SThomas Gleixner #define WHEEL_TIMEOUT_CUTOFF	(LVL_START(LVL_DEPTH))
179500462a9SThomas Gleixner #define WHEEL_TIMEOUT_MAX	(WHEEL_TIMEOUT_CUTOFF - LVL_GRAN(LVL_DEPTH - 1))
180500462a9SThomas Gleixner 
181500462a9SThomas Gleixner /*
182500462a9SThomas Gleixner  * The resulting wheel size. If NOHZ is configured we allocate two
183500462a9SThomas Gleixner  * wheels so we have a separate storage for the deferrable timers.
184500462a9SThomas Gleixner  */
185500462a9SThomas Gleixner #define WHEEL_SIZE	(LVL_SIZE * LVL_DEPTH)
186500462a9SThomas Gleixner 
187500462a9SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
188500462a9SThomas Gleixner # define NR_BASES	2
189500462a9SThomas Gleixner # define BASE_STD	0
190500462a9SThomas Gleixner # define BASE_DEF	1
191500462a9SThomas Gleixner #else
192500462a9SThomas Gleixner # define NR_BASES	1
193500462a9SThomas Gleixner # define BASE_STD	0
194500462a9SThomas Gleixner # define BASE_DEF	0
195500462a9SThomas Gleixner #endif
1965cee9645SThomas Gleixner 
197494af3edSThomas Gleixner struct timer_base {
1982287d866SSebastian Andrzej Siewior 	raw_spinlock_t		lock;
1995cee9645SThomas Gleixner 	struct timer_list	*running_timer;
200494af3edSThomas Gleixner 	unsigned long		clk;
201a683f390SThomas Gleixner 	unsigned long		next_expiry;
202500462a9SThomas Gleixner 	unsigned int		cpu;
203bc7a34b8SThomas Gleixner 	bool			migration_enabled;
204683be13aSThomas Gleixner 	bool			nohz_active;
205a683f390SThomas Gleixner 	bool			is_idle;
2062fe59f50SNicholas Piggin 	bool			must_forward_clk;
207500462a9SThomas Gleixner 	DECLARE_BITMAP(pending_map, WHEEL_SIZE);
208500462a9SThomas Gleixner 	struct hlist_head	vectors[WHEEL_SIZE];
2095cee9645SThomas Gleixner } ____cacheline_aligned;
2105cee9645SThomas Gleixner 
211500462a9SThomas Gleixner static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
2125cee9645SThomas Gleixner 
213bc7a34b8SThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
214bc7a34b8SThomas Gleixner unsigned int sysctl_timer_migration = 1;
215bc7a34b8SThomas Gleixner 
216683be13aSThomas Gleixner void timers_update_migration(bool update_nohz)
217bc7a34b8SThomas Gleixner {
218bc7a34b8SThomas Gleixner 	bool on = sysctl_timer_migration && tick_nohz_active;
219bc7a34b8SThomas Gleixner 	unsigned int cpu;
220bc7a34b8SThomas Gleixner 
221bc7a34b8SThomas Gleixner 	/* Avoid the loop, if nothing to update */
222500462a9SThomas Gleixner 	if (this_cpu_read(timer_bases[BASE_STD].migration_enabled) == on)
223bc7a34b8SThomas Gleixner 		return;
224bc7a34b8SThomas Gleixner 
225bc7a34b8SThomas Gleixner 	for_each_possible_cpu(cpu) {
226500462a9SThomas Gleixner 		per_cpu(timer_bases[BASE_STD].migration_enabled, cpu) = on;
227500462a9SThomas Gleixner 		per_cpu(timer_bases[BASE_DEF].migration_enabled, cpu) = on;
228bc7a34b8SThomas Gleixner 		per_cpu(hrtimer_bases.migration_enabled, cpu) = on;
229683be13aSThomas Gleixner 		if (!update_nohz)
230683be13aSThomas Gleixner 			continue;
231500462a9SThomas Gleixner 		per_cpu(timer_bases[BASE_STD].nohz_active, cpu) = true;
232500462a9SThomas Gleixner 		per_cpu(timer_bases[BASE_DEF].nohz_active, cpu) = true;
233683be13aSThomas Gleixner 		per_cpu(hrtimer_bases.nohz_active, cpu) = true;
234bc7a34b8SThomas Gleixner 	}
235bc7a34b8SThomas Gleixner }
236bc7a34b8SThomas Gleixner 
237bc7a34b8SThomas Gleixner int timer_migration_handler(struct ctl_table *table, int write,
238bc7a34b8SThomas Gleixner 			    void __user *buffer, size_t *lenp,
239bc7a34b8SThomas Gleixner 			    loff_t *ppos)
240bc7a34b8SThomas Gleixner {
241bc7a34b8SThomas Gleixner 	static DEFINE_MUTEX(mutex);
242bc7a34b8SThomas Gleixner 	int ret;
243bc7a34b8SThomas Gleixner 
244bc7a34b8SThomas Gleixner 	mutex_lock(&mutex);
245b94bf594SMyungho Jung 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
246bc7a34b8SThomas Gleixner 	if (!ret && write)
247683be13aSThomas Gleixner 		timers_update_migration(false);
248bc7a34b8SThomas Gleixner 	mutex_unlock(&mutex);
249bc7a34b8SThomas Gleixner 	return ret;
250bc7a34b8SThomas Gleixner }
251bc7a34b8SThomas Gleixner #endif
252bc7a34b8SThomas Gleixner 
2535cee9645SThomas Gleixner static unsigned long round_jiffies_common(unsigned long j, int cpu,
2545cee9645SThomas Gleixner 		bool force_up)
2555cee9645SThomas Gleixner {
2565cee9645SThomas Gleixner 	int rem;
2575cee9645SThomas Gleixner 	unsigned long original = j;
2585cee9645SThomas Gleixner 
2595cee9645SThomas Gleixner 	/*
2605cee9645SThomas Gleixner 	 * We don't want all cpus firing their timers at once hitting the
2615cee9645SThomas Gleixner 	 * same lock or cachelines, so we skew each extra cpu with an extra
2625cee9645SThomas Gleixner 	 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
2635cee9645SThomas Gleixner 	 * already did this.
2645cee9645SThomas Gleixner 	 * The skew is done by adding 3*cpunr, then round, then subtract this
2655cee9645SThomas Gleixner 	 * extra offset again.
2665cee9645SThomas Gleixner 	 */
2675cee9645SThomas Gleixner 	j += cpu * 3;
2685cee9645SThomas Gleixner 
2695cee9645SThomas Gleixner 	rem = j % HZ;
2705cee9645SThomas Gleixner 
2715cee9645SThomas Gleixner 	/*
2725cee9645SThomas Gleixner 	 * If the target jiffie is just after a whole second (which can happen
2735cee9645SThomas Gleixner 	 * due to delays of the timer irq, long irq off times etc etc) then
2745cee9645SThomas Gleixner 	 * we should round down to the whole second, not up. Use 1/4th second
2755cee9645SThomas Gleixner 	 * as cutoff for this rounding as an extreme upper bound for this.
2765cee9645SThomas Gleixner 	 * But never round down if @force_up is set.
2775cee9645SThomas Gleixner 	 */
2785cee9645SThomas Gleixner 	if (rem < HZ/4 && !force_up) /* round down */
2795cee9645SThomas Gleixner 		j = j - rem;
2805cee9645SThomas Gleixner 	else /* round up */
2815cee9645SThomas Gleixner 		j = j - rem + HZ;
2825cee9645SThomas Gleixner 
2835cee9645SThomas Gleixner 	/* now that we have rounded, subtract the extra skew again */
2845cee9645SThomas Gleixner 	j -= cpu * 3;
2855cee9645SThomas Gleixner 
2865cee9645SThomas Gleixner 	/*
2875cee9645SThomas Gleixner 	 * Make sure j is still in the future. Otherwise return the
2885cee9645SThomas Gleixner 	 * unmodified value.
2895cee9645SThomas Gleixner 	 */
2905cee9645SThomas Gleixner 	return time_is_after_jiffies(j) ? j : original;
2915cee9645SThomas Gleixner }
2925cee9645SThomas Gleixner 
2935cee9645SThomas Gleixner /**
2945cee9645SThomas Gleixner  * __round_jiffies - function to round jiffies to a full second
2955cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
2965cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
2975cee9645SThomas Gleixner  *
2985cee9645SThomas Gleixner  * __round_jiffies() rounds an absolute time in the future (in jiffies)
2995cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3005cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3015cee9645SThomas Gleixner  * they fire approximately every X seconds.
3025cee9645SThomas Gleixner  *
3035cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3045cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3055cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3065cee9645SThomas Gleixner  *
3075cee9645SThomas Gleixner  * The exact rounding is skewed for each processor to avoid all
3085cee9645SThomas Gleixner  * processors firing at the exact same time, which could lead
3095cee9645SThomas Gleixner  * to lock contention or spurious cache line bouncing.
3105cee9645SThomas Gleixner  *
3115cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3125cee9645SThomas Gleixner  */
3135cee9645SThomas Gleixner unsigned long __round_jiffies(unsigned long j, int cpu)
3145cee9645SThomas Gleixner {
3155cee9645SThomas Gleixner 	return round_jiffies_common(j, cpu, false);
3165cee9645SThomas Gleixner }
3175cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies);
3185cee9645SThomas Gleixner 
3195cee9645SThomas Gleixner /**
3205cee9645SThomas Gleixner  * __round_jiffies_relative - function to round jiffies to a full second
3215cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
3225cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
3235cee9645SThomas Gleixner  *
3245cee9645SThomas Gleixner  * __round_jiffies_relative() rounds a time delta  in the future (in jiffies)
3255cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3265cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3275cee9645SThomas Gleixner  * they fire approximately every X seconds.
3285cee9645SThomas Gleixner  *
3295cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3305cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3315cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3325cee9645SThomas Gleixner  *
3335cee9645SThomas Gleixner  * The exact rounding is skewed for each processor to avoid all
3345cee9645SThomas Gleixner  * processors firing at the exact same time, which could lead
3355cee9645SThomas Gleixner  * to lock contention or spurious cache line bouncing.
3365cee9645SThomas Gleixner  *
3375cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3385cee9645SThomas Gleixner  */
3395cee9645SThomas Gleixner unsigned long __round_jiffies_relative(unsigned long j, int cpu)
3405cee9645SThomas Gleixner {
3415cee9645SThomas Gleixner 	unsigned long j0 = jiffies;
3425cee9645SThomas Gleixner 
3435cee9645SThomas Gleixner 	/* Use j0 because jiffies might change while we run */
3445cee9645SThomas Gleixner 	return round_jiffies_common(j + j0, cpu, false) - j0;
3455cee9645SThomas Gleixner }
3465cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_relative);
3475cee9645SThomas Gleixner 
3485cee9645SThomas Gleixner /**
3495cee9645SThomas Gleixner  * round_jiffies - function to round jiffies to a full second
3505cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
3515cee9645SThomas Gleixner  *
3525cee9645SThomas Gleixner  * round_jiffies() rounds an absolute time in the future (in jiffies)
3535cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3545cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3555cee9645SThomas Gleixner  * they fire approximately every X seconds.
3565cee9645SThomas Gleixner  *
3575cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3585cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3595cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3605cee9645SThomas Gleixner  *
3615cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3625cee9645SThomas Gleixner  */
3635cee9645SThomas Gleixner unsigned long round_jiffies(unsigned long j)
3645cee9645SThomas Gleixner {
3655cee9645SThomas Gleixner 	return round_jiffies_common(j, raw_smp_processor_id(), false);
3665cee9645SThomas Gleixner }
3675cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies);
3685cee9645SThomas Gleixner 
3695cee9645SThomas Gleixner /**
3705cee9645SThomas Gleixner  * round_jiffies_relative - function to round jiffies to a full second
3715cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
3725cee9645SThomas Gleixner  *
3735cee9645SThomas Gleixner  * round_jiffies_relative() rounds a time delta  in the future (in jiffies)
3745cee9645SThomas Gleixner  * up or down to (approximately) full seconds. This is useful for timers
3755cee9645SThomas Gleixner  * for which the exact time they fire does not matter too much, as long as
3765cee9645SThomas Gleixner  * they fire approximately every X seconds.
3775cee9645SThomas Gleixner  *
3785cee9645SThomas Gleixner  * By rounding these timers to whole seconds, all such timers will fire
3795cee9645SThomas Gleixner  * at the same time, rather than at various times spread out. The goal
3805cee9645SThomas Gleixner  * of this is to have the CPU wake up less, which saves power.
3815cee9645SThomas Gleixner  *
3825cee9645SThomas Gleixner  * The return value is the rounded version of the @j parameter.
3835cee9645SThomas Gleixner  */
3845cee9645SThomas Gleixner unsigned long round_jiffies_relative(unsigned long j)
3855cee9645SThomas Gleixner {
3865cee9645SThomas Gleixner 	return __round_jiffies_relative(j, raw_smp_processor_id());
3875cee9645SThomas Gleixner }
3885cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_relative);
3895cee9645SThomas Gleixner 
3905cee9645SThomas Gleixner /**
3915cee9645SThomas Gleixner  * __round_jiffies_up - function to round jiffies up to a full second
3925cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
3935cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
3945cee9645SThomas Gleixner  *
3955cee9645SThomas Gleixner  * This is the same as __round_jiffies() except that it will never
3965cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
3975cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
3985cee9645SThomas Gleixner  * early.
3995cee9645SThomas Gleixner  */
4005cee9645SThomas Gleixner unsigned long __round_jiffies_up(unsigned long j, int cpu)
4015cee9645SThomas Gleixner {
4025cee9645SThomas Gleixner 	return round_jiffies_common(j, cpu, true);
4035cee9645SThomas Gleixner }
4045cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up);
4055cee9645SThomas Gleixner 
4065cee9645SThomas Gleixner /**
4075cee9645SThomas Gleixner  * __round_jiffies_up_relative - function to round jiffies up to a full second
4085cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
4095cee9645SThomas Gleixner  * @cpu: the processor number on which the timeout will happen
4105cee9645SThomas Gleixner  *
4115cee9645SThomas Gleixner  * This is the same as __round_jiffies_relative() except that it will never
4125cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4135cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4145cee9645SThomas Gleixner  * early.
4155cee9645SThomas Gleixner  */
4165cee9645SThomas Gleixner unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
4175cee9645SThomas Gleixner {
4185cee9645SThomas Gleixner 	unsigned long j0 = jiffies;
4195cee9645SThomas Gleixner 
4205cee9645SThomas Gleixner 	/* Use j0 because jiffies might change while we run */
4215cee9645SThomas Gleixner 	return round_jiffies_common(j + j0, cpu, true) - j0;
4225cee9645SThomas Gleixner }
4235cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
4245cee9645SThomas Gleixner 
4255cee9645SThomas Gleixner /**
4265cee9645SThomas Gleixner  * round_jiffies_up - function to round jiffies up to a full second
4275cee9645SThomas Gleixner  * @j: the time in (absolute) jiffies that should be rounded
4285cee9645SThomas Gleixner  *
4295cee9645SThomas Gleixner  * This is the same as round_jiffies() except that it will never
4305cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4315cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4325cee9645SThomas Gleixner  * early.
4335cee9645SThomas Gleixner  */
4345cee9645SThomas Gleixner unsigned long round_jiffies_up(unsigned long j)
4355cee9645SThomas Gleixner {
4365cee9645SThomas Gleixner 	return round_jiffies_common(j, raw_smp_processor_id(), true);
4375cee9645SThomas Gleixner }
4385cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up);
4395cee9645SThomas Gleixner 
4405cee9645SThomas Gleixner /**
4415cee9645SThomas Gleixner  * round_jiffies_up_relative - function to round jiffies up to a full second
4425cee9645SThomas Gleixner  * @j: the time in (relative) jiffies that should be rounded
4435cee9645SThomas Gleixner  *
4445cee9645SThomas Gleixner  * This is the same as round_jiffies_relative() except that it will never
4455cee9645SThomas Gleixner  * round down.  This is useful for timeouts for which the exact time
4465cee9645SThomas Gleixner  * of firing does not matter too much, as long as they don't fire too
4475cee9645SThomas Gleixner  * early.
4485cee9645SThomas Gleixner  */
4495cee9645SThomas Gleixner unsigned long round_jiffies_up_relative(unsigned long j)
4505cee9645SThomas Gleixner {
4515cee9645SThomas Gleixner 	return __round_jiffies_up_relative(j, raw_smp_processor_id());
4525cee9645SThomas Gleixner }
4535cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
4545cee9645SThomas Gleixner 
4555cee9645SThomas Gleixner 
456500462a9SThomas Gleixner static inline unsigned int timer_get_idx(struct timer_list *timer)
4575cee9645SThomas Gleixner {
458500462a9SThomas Gleixner 	return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT;
4595cee9645SThomas Gleixner }
460500462a9SThomas Gleixner 
461500462a9SThomas Gleixner static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
462500462a9SThomas Gleixner {
463500462a9SThomas Gleixner 	timer->flags = (timer->flags & ~TIMER_ARRAYMASK) |
464500462a9SThomas Gleixner 			idx << TIMER_ARRAYSHIFT;
465500462a9SThomas Gleixner }
466500462a9SThomas Gleixner 
467500462a9SThomas Gleixner /*
468500462a9SThomas Gleixner  * Helper function to calculate the array index for a given expiry
469500462a9SThomas Gleixner  * time.
470500462a9SThomas Gleixner  */
471500462a9SThomas Gleixner static inline unsigned calc_index(unsigned expires, unsigned lvl)
472500462a9SThomas Gleixner {
473500462a9SThomas Gleixner 	expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
474500462a9SThomas Gleixner 	return LVL_OFFS(lvl) + (expires & LVL_MASK);
475500462a9SThomas Gleixner }
476500462a9SThomas Gleixner 
477ffdf0477SAnna-Maria Gleixner static int calc_wheel_index(unsigned long expires, unsigned long clk)
4785cee9645SThomas Gleixner {
479ffdf0477SAnna-Maria Gleixner 	unsigned long delta = expires - clk;
480500462a9SThomas Gleixner 	unsigned int idx;
4815cee9645SThomas Gleixner 
482500462a9SThomas Gleixner 	if (delta < LVL_START(1)) {
483500462a9SThomas Gleixner 		idx = calc_index(expires, 0);
484500462a9SThomas Gleixner 	} else if (delta < LVL_START(2)) {
485500462a9SThomas Gleixner 		idx = calc_index(expires, 1);
486500462a9SThomas Gleixner 	} else if (delta < LVL_START(3)) {
487500462a9SThomas Gleixner 		idx = calc_index(expires, 2);
488500462a9SThomas Gleixner 	} else if (delta < LVL_START(4)) {
489500462a9SThomas Gleixner 		idx = calc_index(expires, 3);
490500462a9SThomas Gleixner 	} else if (delta < LVL_START(5)) {
491500462a9SThomas Gleixner 		idx = calc_index(expires, 4);
492500462a9SThomas Gleixner 	} else if (delta < LVL_START(6)) {
493500462a9SThomas Gleixner 		idx = calc_index(expires, 5);
494500462a9SThomas Gleixner 	} else if (delta < LVL_START(7)) {
495500462a9SThomas Gleixner 		idx = calc_index(expires, 6);
496500462a9SThomas Gleixner 	} else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
497500462a9SThomas Gleixner 		idx = calc_index(expires, 7);
498500462a9SThomas Gleixner 	} else if ((long) delta < 0) {
499ffdf0477SAnna-Maria Gleixner 		idx = clk & LVL_MASK;
5005cee9645SThomas Gleixner 	} else {
501500462a9SThomas Gleixner 		/*
502500462a9SThomas Gleixner 		 * Force expire obscene large timeouts to expire at the
503500462a9SThomas Gleixner 		 * capacity limit of the wheel.
5045cee9645SThomas Gleixner 		 */
505500462a9SThomas Gleixner 		if (expires >= WHEEL_TIMEOUT_CUTOFF)
506500462a9SThomas Gleixner 			expires = WHEEL_TIMEOUT_MAX;
5071bd04bf6SThomas Gleixner 
508500462a9SThomas Gleixner 		idx = calc_index(expires, LVL_DEPTH - 1);
509500462a9SThomas Gleixner 	}
510ffdf0477SAnna-Maria Gleixner 	return idx;
511ffdf0477SAnna-Maria Gleixner }
512ffdf0477SAnna-Maria Gleixner 
513500462a9SThomas Gleixner /*
514ffdf0477SAnna-Maria Gleixner  * Enqueue the timer into the hash bucket, mark it pending in
515500462a9SThomas Gleixner  * the bitmap and store the index in the timer flags.
516500462a9SThomas Gleixner  */
517ffdf0477SAnna-Maria Gleixner static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
518ffdf0477SAnna-Maria Gleixner 			  unsigned int idx)
519ffdf0477SAnna-Maria Gleixner {
520ffdf0477SAnna-Maria Gleixner 	hlist_add_head(&timer->entry, base->vectors + idx);
521500462a9SThomas Gleixner 	__set_bit(idx, base->pending_map);
522500462a9SThomas Gleixner 	timer_set_idx(timer, idx);
5235cee9645SThomas Gleixner }
5245cee9645SThomas Gleixner 
5255cee9645SThomas Gleixner static void
526ffdf0477SAnna-Maria Gleixner __internal_add_timer(struct timer_base *base, struct timer_list *timer)
5275cee9645SThomas Gleixner {
528ffdf0477SAnna-Maria Gleixner 	unsigned int idx;
5295cee9645SThomas Gleixner 
530ffdf0477SAnna-Maria Gleixner 	idx = calc_wheel_index(timer->expires, base->clk);
531ffdf0477SAnna-Maria Gleixner 	enqueue_timer(base, timer, idx);
5325cee9645SThomas Gleixner }
5335cee9645SThomas Gleixner 
534ffdf0477SAnna-Maria Gleixner static void
535ffdf0477SAnna-Maria Gleixner trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
5365cee9645SThomas Gleixner {
537a683f390SThomas Gleixner 	if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active)
538a683f390SThomas Gleixner 		return;
5393bb475a3SThomas Gleixner 
5405cee9645SThomas Gleixner 	/*
541a683f390SThomas Gleixner 	 * TODO: This wants some optimizing similar to the code below, but we
542a683f390SThomas Gleixner 	 * will do that when we switch from push to pull for deferrable timers.
5435cee9645SThomas Gleixner 	 */
544a683f390SThomas Gleixner 	if (timer->flags & TIMER_DEFERRABLE) {
545a683f390SThomas Gleixner 		if (tick_nohz_full_cpu(base->cpu))
5469f6d9baaSViresh Kumar 			wake_up_nohz_cpu(base->cpu);
547a683f390SThomas Gleixner 		return;
5485cee9645SThomas Gleixner 	}
5495cee9645SThomas Gleixner 
5505cee9645SThomas Gleixner 	/*
551a683f390SThomas Gleixner 	 * We might have to IPI the remote CPU if the base is idle and the
552a683f390SThomas Gleixner 	 * timer is not deferrable. If the other CPU is on the way to idle
553a683f390SThomas Gleixner 	 * then it can't set base->is_idle as we hold the base lock:
5545cee9645SThomas Gleixner 	 */
555a683f390SThomas Gleixner 	if (!base->is_idle)
556a683f390SThomas Gleixner 		return;
557a683f390SThomas Gleixner 
558a683f390SThomas Gleixner 	/* Check whether this is the new first expiring timer: */
559a683f390SThomas Gleixner 	if (time_after_eq(timer->expires, base->next_expiry))
560a683f390SThomas Gleixner 		return;
561a683f390SThomas Gleixner 
562a683f390SThomas Gleixner 	/*
563a683f390SThomas Gleixner 	 * Set the next expiry time and kick the CPU so it can reevaluate the
564a683f390SThomas Gleixner 	 * wheel:
565a683f390SThomas Gleixner 	 */
566a683f390SThomas Gleixner 	base->next_expiry = timer->expires;
5675cee9645SThomas Gleixner 		wake_up_nohz_cpu(base->cpu);
5685cee9645SThomas Gleixner }
5695cee9645SThomas Gleixner 
570ffdf0477SAnna-Maria Gleixner static void
571ffdf0477SAnna-Maria Gleixner internal_add_timer(struct timer_base *base, struct timer_list *timer)
572ffdf0477SAnna-Maria Gleixner {
573ffdf0477SAnna-Maria Gleixner 	__internal_add_timer(base, timer);
574ffdf0477SAnna-Maria Gleixner 	trigger_dyntick_cpu(base, timer);
5755cee9645SThomas Gleixner }
5765cee9645SThomas Gleixner 
5775cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
5785cee9645SThomas Gleixner 
5795cee9645SThomas Gleixner static struct debug_obj_descr timer_debug_descr;
5805cee9645SThomas Gleixner 
5815cee9645SThomas Gleixner static void *timer_debug_hint(void *addr)
5825cee9645SThomas Gleixner {
5835cee9645SThomas Gleixner 	return ((struct timer_list *) addr)->function;
5845cee9645SThomas Gleixner }
5855cee9645SThomas Gleixner 
586b9fdac7fSDu, Changbin static bool timer_is_static_object(void *addr)
587b9fdac7fSDu, Changbin {
588b9fdac7fSDu, Changbin 	struct timer_list *timer = addr;
589b9fdac7fSDu, Changbin 
590b9fdac7fSDu, Changbin 	return (timer->entry.pprev == NULL &&
591b9fdac7fSDu, Changbin 		timer->entry.next == TIMER_ENTRY_STATIC);
592b9fdac7fSDu, Changbin }
593b9fdac7fSDu, Changbin 
5945cee9645SThomas Gleixner /*
5955cee9645SThomas Gleixner  * fixup_init is called when:
5965cee9645SThomas Gleixner  * - an active object is initialized
5975cee9645SThomas Gleixner  */
598e3252464SDu, Changbin static bool timer_fixup_init(void *addr, enum debug_obj_state state)
5995cee9645SThomas Gleixner {
6005cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6015cee9645SThomas Gleixner 
6025cee9645SThomas Gleixner 	switch (state) {
6035cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
6045cee9645SThomas Gleixner 		del_timer_sync(timer);
6055cee9645SThomas Gleixner 		debug_object_init(timer, &timer_debug_descr);
606e3252464SDu, Changbin 		return true;
6075cee9645SThomas Gleixner 	default:
608e3252464SDu, Changbin 		return false;
6095cee9645SThomas Gleixner 	}
6105cee9645SThomas Gleixner }
6115cee9645SThomas Gleixner 
6125cee9645SThomas Gleixner /* Stub timer callback for improperly used timers. */
613ba16490eSThomas Gleixner static void stub_timer(struct timer_list *unused)
6145cee9645SThomas Gleixner {
6155cee9645SThomas Gleixner 	WARN_ON(1);
6165cee9645SThomas Gleixner }
6175cee9645SThomas Gleixner 
6185cee9645SThomas Gleixner /*
6195cee9645SThomas Gleixner  * fixup_activate is called when:
6205cee9645SThomas Gleixner  * - an active object is activated
621b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
6225cee9645SThomas Gleixner  */
623e3252464SDu, Changbin static bool timer_fixup_activate(void *addr, enum debug_obj_state state)
6245cee9645SThomas Gleixner {
6255cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6265cee9645SThomas Gleixner 
6275cee9645SThomas Gleixner 	switch (state) {
6285cee9645SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
629ba16490eSThomas Gleixner 		timer_setup(timer, stub_timer, 0);
630e3252464SDu, Changbin 		return true;
6315cee9645SThomas Gleixner 
6325cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
6335cee9645SThomas Gleixner 		WARN_ON(1);
6345cee9645SThomas Gleixner 
6355cee9645SThomas Gleixner 	default:
636e3252464SDu, Changbin 		return false;
6375cee9645SThomas Gleixner 	}
6385cee9645SThomas Gleixner }
6395cee9645SThomas Gleixner 
6405cee9645SThomas Gleixner /*
6415cee9645SThomas Gleixner  * fixup_free is called when:
6425cee9645SThomas Gleixner  * - an active object is freed
6435cee9645SThomas Gleixner  */
644e3252464SDu, Changbin static bool timer_fixup_free(void *addr, enum debug_obj_state state)
6455cee9645SThomas Gleixner {
6465cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6475cee9645SThomas Gleixner 
6485cee9645SThomas Gleixner 	switch (state) {
6495cee9645SThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
6505cee9645SThomas Gleixner 		del_timer_sync(timer);
6515cee9645SThomas Gleixner 		debug_object_free(timer, &timer_debug_descr);
652e3252464SDu, Changbin 		return true;
6535cee9645SThomas Gleixner 	default:
654e3252464SDu, Changbin 		return false;
6555cee9645SThomas Gleixner 	}
6565cee9645SThomas Gleixner }
6575cee9645SThomas Gleixner 
6585cee9645SThomas Gleixner /*
6595cee9645SThomas Gleixner  * fixup_assert_init is called when:
6605cee9645SThomas Gleixner  * - an untracked/uninit-ed object is found
6615cee9645SThomas Gleixner  */
662e3252464SDu, Changbin static bool timer_fixup_assert_init(void *addr, enum debug_obj_state state)
6635cee9645SThomas Gleixner {
6645cee9645SThomas Gleixner 	struct timer_list *timer = addr;
6655cee9645SThomas Gleixner 
6665cee9645SThomas Gleixner 	switch (state) {
6675cee9645SThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
668ba16490eSThomas Gleixner 		timer_setup(timer, stub_timer, 0);
669e3252464SDu, Changbin 		return true;
6705cee9645SThomas Gleixner 	default:
671e3252464SDu, Changbin 		return false;
6725cee9645SThomas Gleixner 	}
6735cee9645SThomas Gleixner }
6745cee9645SThomas Gleixner 
6755cee9645SThomas Gleixner static struct debug_obj_descr timer_debug_descr = {
6765cee9645SThomas Gleixner 	.name			= "timer_list",
6775cee9645SThomas Gleixner 	.debug_hint		= timer_debug_hint,
678b9fdac7fSDu, Changbin 	.is_static_object	= timer_is_static_object,
6795cee9645SThomas Gleixner 	.fixup_init		= timer_fixup_init,
6805cee9645SThomas Gleixner 	.fixup_activate		= timer_fixup_activate,
6815cee9645SThomas Gleixner 	.fixup_free		= timer_fixup_free,
6825cee9645SThomas Gleixner 	.fixup_assert_init	= timer_fixup_assert_init,
6835cee9645SThomas Gleixner };
6845cee9645SThomas Gleixner 
6855cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer)
6865cee9645SThomas Gleixner {
6875cee9645SThomas Gleixner 	debug_object_init(timer, &timer_debug_descr);
6885cee9645SThomas Gleixner }
6895cee9645SThomas Gleixner 
6905cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer)
6915cee9645SThomas Gleixner {
6925cee9645SThomas Gleixner 	debug_object_activate(timer, &timer_debug_descr);
6935cee9645SThomas Gleixner }
6945cee9645SThomas Gleixner 
6955cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer)
6965cee9645SThomas Gleixner {
6975cee9645SThomas Gleixner 	debug_object_deactivate(timer, &timer_debug_descr);
6985cee9645SThomas Gleixner }
6995cee9645SThomas Gleixner 
7005cee9645SThomas Gleixner static inline void debug_timer_free(struct timer_list *timer)
7015cee9645SThomas Gleixner {
7025cee9645SThomas Gleixner 	debug_object_free(timer, &timer_debug_descr);
7035cee9645SThomas Gleixner }
7045cee9645SThomas Gleixner 
7055cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer)
7065cee9645SThomas Gleixner {
7075cee9645SThomas Gleixner 	debug_object_assert_init(timer, &timer_debug_descr);
7085cee9645SThomas Gleixner }
7095cee9645SThomas Gleixner 
7105cee9645SThomas Gleixner static void do_init_timer(struct timer_list *timer, unsigned int flags,
7115cee9645SThomas Gleixner 			  const char *name, struct lock_class_key *key);
7125cee9645SThomas Gleixner 
7135cee9645SThomas Gleixner void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
7145cee9645SThomas Gleixner 			     const char *name, struct lock_class_key *key)
7155cee9645SThomas Gleixner {
7165cee9645SThomas Gleixner 	debug_object_init_on_stack(timer, &timer_debug_descr);
7175cee9645SThomas Gleixner 	do_init_timer(timer, flags, name, key);
7185cee9645SThomas Gleixner }
7195cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
7205cee9645SThomas Gleixner 
7215cee9645SThomas Gleixner void destroy_timer_on_stack(struct timer_list *timer)
7225cee9645SThomas Gleixner {
7235cee9645SThomas Gleixner 	debug_object_free(timer, &timer_debug_descr);
7245cee9645SThomas Gleixner }
7255cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
7265cee9645SThomas Gleixner 
7275cee9645SThomas Gleixner #else
7285cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer) { }
7295cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer) { }
7305cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer) { }
7315cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer) { }
7325cee9645SThomas Gleixner #endif
7335cee9645SThomas Gleixner 
7345cee9645SThomas Gleixner static inline void debug_init(struct timer_list *timer)
7355cee9645SThomas Gleixner {
7365cee9645SThomas Gleixner 	debug_timer_init(timer);
7375cee9645SThomas Gleixner 	trace_timer_init(timer);
7385cee9645SThomas Gleixner }
7395cee9645SThomas Gleixner 
7405cee9645SThomas Gleixner static inline void
7415cee9645SThomas Gleixner debug_activate(struct timer_list *timer, unsigned long expires)
7425cee9645SThomas Gleixner {
7435cee9645SThomas Gleixner 	debug_timer_activate(timer);
7440eeda71bSThomas Gleixner 	trace_timer_start(timer, expires, timer->flags);
7455cee9645SThomas Gleixner }
7465cee9645SThomas Gleixner 
7475cee9645SThomas Gleixner static inline void debug_deactivate(struct timer_list *timer)
7485cee9645SThomas Gleixner {
7495cee9645SThomas Gleixner 	debug_timer_deactivate(timer);
7505cee9645SThomas Gleixner 	trace_timer_cancel(timer);
7515cee9645SThomas Gleixner }
7525cee9645SThomas Gleixner 
7535cee9645SThomas Gleixner static inline void debug_assert_init(struct timer_list *timer)
7545cee9645SThomas Gleixner {
7555cee9645SThomas Gleixner 	debug_timer_assert_init(timer);
7565cee9645SThomas Gleixner }
7575cee9645SThomas Gleixner 
7585cee9645SThomas Gleixner static void do_init_timer(struct timer_list *timer, unsigned int flags,
7595cee9645SThomas Gleixner 			  const char *name, struct lock_class_key *key)
7605cee9645SThomas Gleixner {
7611dabbcecSThomas Gleixner 	timer->entry.pprev = NULL;
7620eeda71bSThomas Gleixner 	timer->flags = flags | raw_smp_processor_id();
7635cee9645SThomas Gleixner 	lockdep_init_map(&timer->lockdep_map, name, key, 0);
7645cee9645SThomas Gleixner }
7655cee9645SThomas Gleixner 
7665cee9645SThomas Gleixner /**
7675cee9645SThomas Gleixner  * init_timer_key - initialize a timer
7685cee9645SThomas Gleixner  * @timer: the timer to be initialized
7695cee9645SThomas Gleixner  * @flags: timer flags
7705cee9645SThomas Gleixner  * @name: name of the timer
7715cee9645SThomas Gleixner  * @key: lockdep class key of the fake lock used for tracking timer
7725cee9645SThomas Gleixner  *       sync lock dependencies
7735cee9645SThomas Gleixner  *
7745cee9645SThomas Gleixner  * init_timer_key() must be done to a timer prior calling *any* of the
7755cee9645SThomas Gleixner  * other timer functions.
7765cee9645SThomas Gleixner  */
7775cee9645SThomas Gleixner void init_timer_key(struct timer_list *timer, unsigned int flags,
7785cee9645SThomas Gleixner 		    const char *name, struct lock_class_key *key)
7795cee9645SThomas Gleixner {
7805cee9645SThomas Gleixner 	debug_init(timer);
7815cee9645SThomas Gleixner 	do_init_timer(timer, flags, name, key);
7825cee9645SThomas Gleixner }
7835cee9645SThomas Gleixner EXPORT_SYMBOL(init_timer_key);
7845cee9645SThomas Gleixner 
7855cee9645SThomas Gleixner static inline void detach_timer(struct timer_list *timer, bool clear_pending)
7865cee9645SThomas Gleixner {
7871dabbcecSThomas Gleixner 	struct hlist_node *entry = &timer->entry;
7885cee9645SThomas Gleixner 
7895cee9645SThomas Gleixner 	debug_deactivate(timer);
7905cee9645SThomas Gleixner 
7911dabbcecSThomas Gleixner 	__hlist_del(entry);
7925cee9645SThomas Gleixner 	if (clear_pending)
7931dabbcecSThomas Gleixner 		entry->pprev = NULL;
7941dabbcecSThomas Gleixner 	entry->next = LIST_POISON2;
7955cee9645SThomas Gleixner }
7965cee9645SThomas Gleixner 
797494af3edSThomas Gleixner static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
7985cee9645SThomas Gleixner 			     bool clear_pending)
7995cee9645SThomas Gleixner {
800500462a9SThomas Gleixner 	unsigned idx = timer_get_idx(timer);
801500462a9SThomas Gleixner 
8025cee9645SThomas Gleixner 	if (!timer_pending(timer))
8035cee9645SThomas Gleixner 		return 0;
8045cee9645SThomas Gleixner 
805500462a9SThomas Gleixner 	if (hlist_is_singular_node(&timer->entry, base->vectors + idx))
806500462a9SThomas Gleixner 		__clear_bit(idx, base->pending_map);
807500462a9SThomas Gleixner 
8085cee9645SThomas Gleixner 	detach_timer(timer, clear_pending);
8095cee9645SThomas Gleixner 	return 1;
8105cee9645SThomas Gleixner }
8115cee9645SThomas Gleixner 
812500462a9SThomas Gleixner static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
813500462a9SThomas Gleixner {
814500462a9SThomas Gleixner 	struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu);
815500462a9SThomas Gleixner 
8165cee9645SThomas Gleixner 	/*
817500462a9SThomas Gleixner 	 * If the timer is deferrable and nohz is active then we need to use
818500462a9SThomas Gleixner 	 * the deferrable base.
819500462a9SThomas Gleixner 	 */
820500462a9SThomas Gleixner 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active &&
821500462a9SThomas Gleixner 	    (tflags & TIMER_DEFERRABLE))
822500462a9SThomas Gleixner 		base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
823500462a9SThomas Gleixner 	return base;
824500462a9SThomas Gleixner }
825500462a9SThomas Gleixner 
826500462a9SThomas Gleixner static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
827500462a9SThomas Gleixner {
828500462a9SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
829500462a9SThomas Gleixner 
830500462a9SThomas Gleixner 	/*
831500462a9SThomas Gleixner 	 * If the timer is deferrable and nohz is active then we need to use
832500462a9SThomas Gleixner 	 * the deferrable base.
833500462a9SThomas Gleixner 	 */
834500462a9SThomas Gleixner 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active &&
835500462a9SThomas Gleixner 	    (tflags & TIMER_DEFERRABLE))
836500462a9SThomas Gleixner 		base = this_cpu_ptr(&timer_bases[BASE_DEF]);
837500462a9SThomas Gleixner 	return base;
838500462a9SThomas Gleixner }
839500462a9SThomas Gleixner 
840500462a9SThomas Gleixner static inline struct timer_base *get_timer_base(u32 tflags)
841500462a9SThomas Gleixner {
842500462a9SThomas Gleixner 	return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
843500462a9SThomas Gleixner }
844500462a9SThomas Gleixner 
845a683f390SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
846a683f390SThomas Gleixner static inline struct timer_base *
8476bad6bccSThomas Gleixner get_target_base(struct timer_base *base, unsigned tflags)
848500462a9SThomas Gleixner {
849a683f390SThomas Gleixner #ifdef CONFIG_SMP
850500462a9SThomas Gleixner 	if ((tflags & TIMER_PINNED) || !base->migration_enabled)
851500462a9SThomas Gleixner 		return get_timer_this_cpu_base(tflags);
852500462a9SThomas Gleixner 	return get_timer_cpu_base(tflags, get_nohz_timer_target());
853500462a9SThomas Gleixner #else
854500462a9SThomas Gleixner 	return get_timer_this_cpu_base(tflags);
855500462a9SThomas Gleixner #endif
856500462a9SThomas Gleixner }
857500462a9SThomas Gleixner 
858a683f390SThomas Gleixner static inline void forward_timer_base(struct timer_base *base)
859a683f390SThomas Gleixner {
8602fe59f50SNicholas Piggin 	unsigned long jnow;
8616bad6bccSThomas Gleixner 
862a683f390SThomas Gleixner 	/*
8632fe59f50SNicholas Piggin 	 * We only forward the base when we are idle or have just come out of
8642fe59f50SNicholas Piggin 	 * idle (must_forward_clk logic), and have a delta between base clock
8652fe59f50SNicholas Piggin 	 * and jiffies. In the common case, run_timers will take care of it.
866a683f390SThomas Gleixner 	 */
8672fe59f50SNicholas Piggin 	if (likely(!base->must_forward_clk))
8682fe59f50SNicholas Piggin 		return;
8692fe59f50SNicholas Piggin 
8702fe59f50SNicholas Piggin 	jnow = READ_ONCE(jiffies);
8712fe59f50SNicholas Piggin 	base->must_forward_clk = base->is_idle;
8722fe59f50SNicholas Piggin 	if ((long)(jnow - base->clk) < 2)
873a683f390SThomas Gleixner 		return;
874a683f390SThomas Gleixner 
875a683f390SThomas Gleixner 	/*
876a683f390SThomas Gleixner 	 * If the next expiry value is > jiffies, then we fast forward to
877a683f390SThomas Gleixner 	 * jiffies otherwise we forward to the next expiry value.
878a683f390SThomas Gleixner 	 */
8796bad6bccSThomas Gleixner 	if (time_after(base->next_expiry, jnow))
8806bad6bccSThomas Gleixner 		base->clk = jnow;
881a683f390SThomas Gleixner 	else
882a683f390SThomas Gleixner 		base->clk = base->next_expiry;
883a683f390SThomas Gleixner }
884a683f390SThomas Gleixner #else
885a683f390SThomas Gleixner static inline struct timer_base *
8866bad6bccSThomas Gleixner get_target_base(struct timer_base *base, unsigned tflags)
887a683f390SThomas Gleixner {
888a683f390SThomas Gleixner 	return get_timer_this_cpu_base(tflags);
889a683f390SThomas Gleixner }
890a683f390SThomas Gleixner 
891a683f390SThomas Gleixner static inline void forward_timer_base(struct timer_base *base) { }
892a683f390SThomas Gleixner #endif
893a683f390SThomas Gleixner 
894a683f390SThomas Gleixner 
895500462a9SThomas Gleixner /*
896500462a9SThomas Gleixner  * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
897500462a9SThomas Gleixner  * that all timers which are tied to this base are locked, and the base itself
898500462a9SThomas Gleixner  * is locked too.
8995cee9645SThomas Gleixner  *
9005cee9645SThomas Gleixner  * So __run_timers/migrate_timers can safely modify all timers which could
901500462a9SThomas Gleixner  * be found in the base->vectors array.
9025cee9645SThomas Gleixner  *
903500462a9SThomas Gleixner  * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
904500462a9SThomas Gleixner  * to wait until the migration is done.
9055cee9645SThomas Gleixner  */
906494af3edSThomas Gleixner static struct timer_base *lock_timer_base(struct timer_list *timer,
9075cee9645SThomas Gleixner 					  unsigned long *flags)
9085cee9645SThomas Gleixner 	__acquires(timer->base->lock)
9095cee9645SThomas Gleixner {
9100eeda71bSThomas Gleixner 	for (;;) {
911494af3edSThomas Gleixner 		struct timer_base *base;
912b831275aSThomas Gleixner 		u32 tf;
913b831275aSThomas Gleixner 
914b831275aSThomas Gleixner 		/*
915b831275aSThomas Gleixner 		 * We need to use READ_ONCE() here, otherwise the compiler
916b831275aSThomas Gleixner 		 * might re-read @tf between the check for TIMER_MIGRATING
917b831275aSThomas Gleixner 		 * and spin_lock().
918b831275aSThomas Gleixner 		 */
919b831275aSThomas Gleixner 		tf = READ_ONCE(timer->flags);
9205cee9645SThomas Gleixner 
9210eeda71bSThomas Gleixner 		if (!(tf & TIMER_MIGRATING)) {
922500462a9SThomas Gleixner 			base = get_timer_base(tf);
9232287d866SSebastian Andrzej Siewior 			raw_spin_lock_irqsave(&base->lock, *flags);
9240eeda71bSThomas Gleixner 			if (timer->flags == tf)
9255cee9645SThomas Gleixner 				return base;
9262287d866SSebastian Andrzej Siewior 			raw_spin_unlock_irqrestore(&base->lock, *flags);
9275cee9645SThomas Gleixner 		}
9285cee9645SThomas Gleixner 		cpu_relax();
9295cee9645SThomas Gleixner 	}
9305cee9645SThomas Gleixner }
9315cee9645SThomas Gleixner 
932b24591e2SDavid Howells #define MOD_TIMER_PENDING_ONLY		0x01
933b24591e2SDavid Howells #define MOD_TIMER_REDUCE		0x02
934b24591e2SDavid Howells 
9355cee9645SThomas Gleixner static inline int
936b24591e2SDavid Howells __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options)
9375cee9645SThomas Gleixner {
938494af3edSThomas Gleixner 	struct timer_base *base, *new_base;
939f00c0afdSAnna-Maria Gleixner 	unsigned int idx = UINT_MAX;
940f00c0afdSAnna-Maria Gleixner 	unsigned long clk = 0, flags;
941bc7a34b8SThomas Gleixner 	int ret = 0;
9425cee9645SThomas Gleixner 
9434da9152aSThomas Gleixner 	BUG_ON(!timer->function);
9444da9152aSThomas Gleixner 
945500462a9SThomas Gleixner 	/*
946f00c0afdSAnna-Maria Gleixner 	 * This is a common optimization triggered by the networking code - if
947f00c0afdSAnna-Maria Gleixner 	 * the timer is re-modified to have the same timeout or ends up in the
948f00c0afdSAnna-Maria Gleixner 	 * same array bucket then just return:
949500462a9SThomas Gleixner 	 */
950500462a9SThomas Gleixner 	if (timer_pending(timer)) {
9512fe59f50SNicholas Piggin 		/*
9522fe59f50SNicholas Piggin 		 * The downside of this optimization is that it can result in
9532fe59f50SNicholas Piggin 		 * larger granularity than you would get from adding a new
9542fe59f50SNicholas Piggin 		 * timer with this expiry.
9552fe59f50SNicholas Piggin 		 */
956b24591e2SDavid Howells 		long diff = timer->expires - expires;
957b24591e2SDavid Howells 
958b24591e2SDavid Howells 		if (!diff)
959b24591e2SDavid Howells 			return 1;
960b24591e2SDavid Howells 		if (options & MOD_TIMER_REDUCE && diff <= 0)
961500462a9SThomas Gleixner 			return 1;
962f00c0afdSAnna-Maria Gleixner 
9634da9152aSThomas Gleixner 		/*
9644da9152aSThomas Gleixner 		 * We lock timer base and calculate the bucket index right
9654da9152aSThomas Gleixner 		 * here. If the timer ends up in the same bucket, then we
9664da9152aSThomas Gleixner 		 * just update the expiry time and avoid the whole
9674da9152aSThomas Gleixner 		 * dequeue/enqueue dance.
9684da9152aSThomas Gleixner 		 */
9694da9152aSThomas Gleixner 		base = lock_timer_base(timer, &flags);
9702fe59f50SNicholas Piggin 		forward_timer_base(base);
9714da9152aSThomas Gleixner 
972b24591e2SDavid Howells 		if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) &&
973b24591e2SDavid Howells 		    time_before_eq(timer->expires, expires)) {
974b24591e2SDavid Howells 			ret = 1;
975b24591e2SDavid Howells 			goto out_unlock;
976b24591e2SDavid Howells 		}
977b24591e2SDavid Howells 
9784da9152aSThomas Gleixner 		clk = base->clk;
979f00c0afdSAnna-Maria Gleixner 		idx = calc_wheel_index(expires, clk);
980f00c0afdSAnna-Maria Gleixner 
981f00c0afdSAnna-Maria Gleixner 		/*
982f00c0afdSAnna-Maria Gleixner 		 * Retrieve and compare the array index of the pending
983f00c0afdSAnna-Maria Gleixner 		 * timer. If it matches set the expiry to the new value so a
984f00c0afdSAnna-Maria Gleixner 		 * subsequent call will exit in the expires check above.
985f00c0afdSAnna-Maria Gleixner 		 */
986f00c0afdSAnna-Maria Gleixner 		if (idx == timer_get_idx(timer)) {
987b24591e2SDavid Howells 			if (!(options & MOD_TIMER_REDUCE))
988b24591e2SDavid Howells 				timer->expires = expires;
989b24591e2SDavid Howells 			else if (time_after(timer->expires, expires))
990f00c0afdSAnna-Maria Gleixner 				timer->expires = expires;
9914da9152aSThomas Gleixner 			ret = 1;
9924da9152aSThomas Gleixner 			goto out_unlock;
993f00c0afdSAnna-Maria Gleixner 		}
9944da9152aSThomas Gleixner 	} else {
9954da9152aSThomas Gleixner 		base = lock_timer_base(timer, &flags);
9962fe59f50SNicholas Piggin 		forward_timer_base(base);
997500462a9SThomas Gleixner 	}
998500462a9SThomas Gleixner 
9995cee9645SThomas Gleixner 	ret = detach_if_pending(timer, base, false);
1000b24591e2SDavid Howells 	if (!ret && (options & MOD_TIMER_PENDING_ONLY))
10015cee9645SThomas Gleixner 		goto out_unlock;
10025cee9645SThomas Gleixner 
10035cee9645SThomas Gleixner 	debug_activate(timer, expires);
10045cee9645SThomas Gleixner 
1005500462a9SThomas Gleixner 	new_base = get_target_base(base, timer->flags);
10065cee9645SThomas Gleixner 
10075cee9645SThomas Gleixner 	if (base != new_base) {
10085cee9645SThomas Gleixner 		/*
1009500462a9SThomas Gleixner 		 * We are trying to schedule the timer on the new base.
10105cee9645SThomas Gleixner 		 * However we can't change timer's base while it is running,
10115cee9645SThomas Gleixner 		 * otherwise del_timer_sync() can't detect that the timer's
1012500462a9SThomas Gleixner 		 * handler yet has not finished. This also guarantees that the
1013500462a9SThomas Gleixner 		 * timer is serialized wrt itself.
10145cee9645SThomas Gleixner 		 */
10155cee9645SThomas Gleixner 		if (likely(base->running_timer != timer)) {
10165cee9645SThomas Gleixner 			/* See the comment in lock_timer_base() */
10170eeda71bSThomas Gleixner 			timer->flags |= TIMER_MIGRATING;
10180eeda71bSThomas Gleixner 
10192287d866SSebastian Andrzej Siewior 			raw_spin_unlock(&base->lock);
10205cee9645SThomas Gleixner 			base = new_base;
10212287d866SSebastian Andrzej Siewior 			raw_spin_lock(&base->lock);
1022d0023a14SEric Dumazet 			WRITE_ONCE(timer->flags,
1023d0023a14SEric Dumazet 				   (timer->flags & ~TIMER_BASEMASK) | base->cpu);
10246bad6bccSThomas Gleixner 			forward_timer_base(base);
10252fe59f50SNicholas Piggin 		}
10262fe59f50SNicholas Piggin 	}
10276bad6bccSThomas Gleixner 
10285cee9645SThomas Gleixner 	timer->expires = expires;
1029f00c0afdSAnna-Maria Gleixner 	/*
1030f00c0afdSAnna-Maria Gleixner 	 * If 'idx' was calculated above and the base time did not advance
10314da9152aSThomas Gleixner 	 * between calculating 'idx' and possibly switching the base, only
10324da9152aSThomas Gleixner 	 * enqueue_timer() and trigger_dyntick_cpu() is required. Otherwise
10334da9152aSThomas Gleixner 	 * we need to (re)calculate the wheel index via
10344da9152aSThomas Gleixner 	 * internal_add_timer().
1035f00c0afdSAnna-Maria Gleixner 	 */
1036f00c0afdSAnna-Maria Gleixner 	if (idx != UINT_MAX && clk == base->clk) {
1037f00c0afdSAnna-Maria Gleixner 		enqueue_timer(base, timer, idx);
1038f00c0afdSAnna-Maria Gleixner 		trigger_dyntick_cpu(base, timer);
1039f00c0afdSAnna-Maria Gleixner 	} else {
10405cee9645SThomas Gleixner 		internal_add_timer(base, timer);
1041f00c0afdSAnna-Maria Gleixner 	}
10425cee9645SThomas Gleixner 
10435cee9645SThomas Gleixner out_unlock:
10442287d866SSebastian Andrzej Siewior 	raw_spin_unlock_irqrestore(&base->lock, flags);
10455cee9645SThomas Gleixner 
10465cee9645SThomas Gleixner 	return ret;
10475cee9645SThomas Gleixner }
10485cee9645SThomas Gleixner 
10495cee9645SThomas Gleixner /**
10505cee9645SThomas Gleixner  * mod_timer_pending - modify a pending timer's timeout
10515cee9645SThomas Gleixner  * @timer: the pending timer to be modified
10525cee9645SThomas Gleixner  * @expires: new timeout in jiffies
10535cee9645SThomas Gleixner  *
10545cee9645SThomas Gleixner  * mod_timer_pending() is the same for pending timers as mod_timer(),
10555cee9645SThomas Gleixner  * but will not re-activate and modify already deleted timers.
10565cee9645SThomas Gleixner  *
10575cee9645SThomas Gleixner  * It is useful for unserialized use of timers.
10585cee9645SThomas Gleixner  */
10595cee9645SThomas Gleixner int mod_timer_pending(struct timer_list *timer, unsigned long expires)
10605cee9645SThomas Gleixner {
1061b24591e2SDavid Howells 	return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY);
10625cee9645SThomas Gleixner }
10635cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer_pending);
10645cee9645SThomas Gleixner 
10655cee9645SThomas Gleixner /**
10665cee9645SThomas Gleixner  * mod_timer - modify a timer's timeout
10675cee9645SThomas Gleixner  * @timer: the timer to be modified
10685cee9645SThomas Gleixner  * @expires: new timeout in jiffies
10695cee9645SThomas Gleixner  *
10705cee9645SThomas Gleixner  * mod_timer() is a more efficient way to update the expire field of an
10715cee9645SThomas Gleixner  * active timer (if the timer is inactive it will be activated)
10725cee9645SThomas Gleixner  *
10735cee9645SThomas Gleixner  * mod_timer(timer, expires) is equivalent to:
10745cee9645SThomas Gleixner  *
10755cee9645SThomas Gleixner  *     del_timer(timer); timer->expires = expires; add_timer(timer);
10765cee9645SThomas Gleixner  *
10775cee9645SThomas Gleixner  * Note that if there are multiple unserialized concurrent users of the
10785cee9645SThomas Gleixner  * same timer, then mod_timer() is the only safe way to modify the timeout,
10795cee9645SThomas Gleixner  * since add_timer() cannot modify an already running timer.
10805cee9645SThomas Gleixner  *
10815cee9645SThomas Gleixner  * The function returns whether it has modified a pending timer or not.
10825cee9645SThomas Gleixner  * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
10835cee9645SThomas Gleixner  * active timer returns 1.)
10845cee9645SThomas Gleixner  */
10855cee9645SThomas Gleixner int mod_timer(struct timer_list *timer, unsigned long expires)
10865cee9645SThomas Gleixner {
1087b24591e2SDavid Howells 	return __mod_timer(timer, expires, 0);
10885cee9645SThomas Gleixner }
10895cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer);
10905cee9645SThomas Gleixner 
10915cee9645SThomas Gleixner /**
1092b24591e2SDavid Howells  * timer_reduce - Modify a timer's timeout if it would reduce the timeout
1093b24591e2SDavid Howells  * @timer:	The timer to be modified
1094b24591e2SDavid Howells  * @expires:	New timeout in jiffies
1095b24591e2SDavid Howells  *
1096b24591e2SDavid Howells  * timer_reduce() is very similar to mod_timer(), except that it will only
1097b24591e2SDavid Howells  * modify a running timer if that would reduce the expiration time (it will
1098b24591e2SDavid Howells  * start a timer that isn't running).
1099b24591e2SDavid Howells  */
1100b24591e2SDavid Howells int timer_reduce(struct timer_list *timer, unsigned long expires)
1101b24591e2SDavid Howells {
1102b24591e2SDavid Howells 	return __mod_timer(timer, expires, MOD_TIMER_REDUCE);
1103b24591e2SDavid Howells }
1104b24591e2SDavid Howells EXPORT_SYMBOL(timer_reduce);
1105b24591e2SDavid Howells 
1106b24591e2SDavid Howells /**
11075cee9645SThomas Gleixner  * add_timer - start a timer
11085cee9645SThomas Gleixner  * @timer: the timer to be added
11095cee9645SThomas Gleixner  *
1110*c1eba5bcSKees Cook  * The kernel will do a ->function(@timer) callback from the
11115cee9645SThomas Gleixner  * timer interrupt at the ->expires point in the future. The
11125cee9645SThomas Gleixner  * current time is 'jiffies'.
11135cee9645SThomas Gleixner  *
1114*c1eba5bcSKees Cook  * The timer's ->expires, ->function fields must be set prior calling this
1115*c1eba5bcSKees Cook  * function.
11165cee9645SThomas Gleixner  *
11175cee9645SThomas Gleixner  * Timers with an ->expires field in the past will be executed in the next
11185cee9645SThomas Gleixner  * timer tick.
11195cee9645SThomas Gleixner  */
11205cee9645SThomas Gleixner void add_timer(struct timer_list *timer)
11215cee9645SThomas Gleixner {
11225cee9645SThomas Gleixner 	BUG_ON(timer_pending(timer));
11235cee9645SThomas Gleixner 	mod_timer(timer, timer->expires);
11245cee9645SThomas Gleixner }
11255cee9645SThomas Gleixner EXPORT_SYMBOL(add_timer);
11265cee9645SThomas Gleixner 
11275cee9645SThomas Gleixner /**
11285cee9645SThomas Gleixner  * add_timer_on - start a timer on a particular CPU
11295cee9645SThomas Gleixner  * @timer: the timer to be added
11305cee9645SThomas Gleixner  * @cpu: the CPU to start it on
11315cee9645SThomas Gleixner  *
11325cee9645SThomas Gleixner  * This is not very scalable on SMP. Double adds are not possible.
11335cee9645SThomas Gleixner  */
11345cee9645SThomas Gleixner void add_timer_on(struct timer_list *timer, int cpu)
11355cee9645SThomas Gleixner {
1136500462a9SThomas Gleixner 	struct timer_base *new_base, *base;
11375cee9645SThomas Gleixner 	unsigned long flags;
11385cee9645SThomas Gleixner 
11395cee9645SThomas Gleixner 	BUG_ON(timer_pending(timer) || !timer->function);
114022b886ddSTejun Heo 
1141500462a9SThomas Gleixner 	new_base = get_timer_cpu_base(timer->flags, cpu);
1142500462a9SThomas Gleixner 
114322b886ddSTejun Heo 	/*
114422b886ddSTejun Heo 	 * If @timer was on a different CPU, it should be migrated with the
114522b886ddSTejun Heo 	 * old base locked to prevent other operations proceeding with the
114622b886ddSTejun Heo 	 * wrong base locked.  See lock_timer_base().
114722b886ddSTejun Heo 	 */
114822b886ddSTejun Heo 	base = lock_timer_base(timer, &flags);
114922b886ddSTejun Heo 	if (base != new_base) {
115022b886ddSTejun Heo 		timer->flags |= TIMER_MIGRATING;
115122b886ddSTejun Heo 
11522287d866SSebastian Andrzej Siewior 		raw_spin_unlock(&base->lock);
115322b886ddSTejun Heo 		base = new_base;
11542287d866SSebastian Andrzej Siewior 		raw_spin_lock(&base->lock);
115522b886ddSTejun Heo 		WRITE_ONCE(timer->flags,
115622b886ddSTejun Heo 			   (timer->flags & ~TIMER_BASEMASK) | cpu);
115722b886ddSTejun Heo 	}
11582fe59f50SNicholas Piggin 	forward_timer_base(base);
115922b886ddSTejun Heo 
11605cee9645SThomas Gleixner 	debug_activate(timer, timer->expires);
11615cee9645SThomas Gleixner 	internal_add_timer(base, timer);
11622287d866SSebastian Andrzej Siewior 	raw_spin_unlock_irqrestore(&base->lock, flags);
11635cee9645SThomas Gleixner }
11645cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(add_timer_on);
11655cee9645SThomas Gleixner 
11665cee9645SThomas Gleixner /**
11670ba42a59SMasanari Iida  * del_timer - deactivate a timer.
11685cee9645SThomas Gleixner  * @timer: the timer to be deactivated
11695cee9645SThomas Gleixner  *
11705cee9645SThomas Gleixner  * del_timer() deactivates a timer - this works on both active and inactive
11715cee9645SThomas Gleixner  * timers.
11725cee9645SThomas Gleixner  *
11735cee9645SThomas Gleixner  * The function returns whether it has deactivated a pending timer or not.
11745cee9645SThomas Gleixner  * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
11755cee9645SThomas Gleixner  * active timer returns 1.)
11765cee9645SThomas Gleixner  */
11775cee9645SThomas Gleixner int del_timer(struct timer_list *timer)
11785cee9645SThomas Gleixner {
1179494af3edSThomas Gleixner 	struct timer_base *base;
11805cee9645SThomas Gleixner 	unsigned long flags;
11815cee9645SThomas Gleixner 	int ret = 0;
11825cee9645SThomas Gleixner 
11835cee9645SThomas Gleixner 	debug_assert_init(timer);
11845cee9645SThomas Gleixner 
11855cee9645SThomas Gleixner 	if (timer_pending(timer)) {
11865cee9645SThomas Gleixner 		base = lock_timer_base(timer, &flags);
11875cee9645SThomas Gleixner 		ret = detach_if_pending(timer, base, true);
11882287d866SSebastian Andrzej Siewior 		raw_spin_unlock_irqrestore(&base->lock, flags);
11895cee9645SThomas Gleixner 	}
11905cee9645SThomas Gleixner 
11915cee9645SThomas Gleixner 	return ret;
11925cee9645SThomas Gleixner }
11935cee9645SThomas Gleixner EXPORT_SYMBOL(del_timer);
11945cee9645SThomas Gleixner 
11955cee9645SThomas Gleixner /**
11965cee9645SThomas Gleixner  * try_to_del_timer_sync - Try to deactivate a timer
1197d15bc69aSPeter Meerwald-Stadler  * @timer: timer to delete
11985cee9645SThomas Gleixner  *
11995cee9645SThomas Gleixner  * This function tries to deactivate a timer. Upon successful (ret >= 0)
12005cee9645SThomas Gleixner  * exit the timer is not queued and the handler is not running on any CPU.
12015cee9645SThomas Gleixner  */
12025cee9645SThomas Gleixner int try_to_del_timer_sync(struct timer_list *timer)
12035cee9645SThomas Gleixner {
1204494af3edSThomas Gleixner 	struct timer_base *base;
12055cee9645SThomas Gleixner 	unsigned long flags;
12065cee9645SThomas Gleixner 	int ret = -1;
12075cee9645SThomas Gleixner 
12085cee9645SThomas Gleixner 	debug_assert_init(timer);
12095cee9645SThomas Gleixner 
12105cee9645SThomas Gleixner 	base = lock_timer_base(timer, &flags);
12115cee9645SThomas Gleixner 
1212dfb4357dSKees Cook 	if (base->running_timer != timer)
12135cee9645SThomas Gleixner 		ret = detach_if_pending(timer, base, true);
1214dfb4357dSKees Cook 
12152287d866SSebastian Andrzej Siewior 	raw_spin_unlock_irqrestore(&base->lock, flags);
12165cee9645SThomas Gleixner 
12175cee9645SThomas Gleixner 	return ret;
12185cee9645SThomas Gleixner }
12195cee9645SThomas Gleixner EXPORT_SYMBOL(try_to_del_timer_sync);
12205cee9645SThomas Gleixner 
12215cee9645SThomas Gleixner #ifdef CONFIG_SMP
12225cee9645SThomas Gleixner /**
12235cee9645SThomas Gleixner  * del_timer_sync - deactivate a timer and wait for the handler to finish.
12245cee9645SThomas Gleixner  * @timer: the timer to be deactivated
12255cee9645SThomas Gleixner  *
12265cee9645SThomas Gleixner  * This function only differs from del_timer() on SMP: besides deactivating
12275cee9645SThomas Gleixner  * the timer it also makes sure the handler has finished executing on other
12285cee9645SThomas Gleixner  * CPUs.
12295cee9645SThomas Gleixner  *
12305cee9645SThomas Gleixner  * Synchronization rules: Callers must prevent restarting of the timer,
12315cee9645SThomas Gleixner  * otherwise this function is meaningless. It must not be called from
12325cee9645SThomas Gleixner  * interrupt contexts unless the timer is an irqsafe one. The caller must
12335cee9645SThomas Gleixner  * not hold locks which would prevent completion of the timer's
12345cee9645SThomas Gleixner  * handler. The timer's handler must not call add_timer_on(). Upon exit the
12355cee9645SThomas Gleixner  * timer is not queued and the handler is not running on any CPU.
12365cee9645SThomas Gleixner  *
12375cee9645SThomas Gleixner  * Note: For !irqsafe timers, you must not hold locks that are held in
12385cee9645SThomas Gleixner  *   interrupt context while calling this function. Even if the lock has
12395cee9645SThomas Gleixner  *   nothing to do with the timer in question.  Here's why:
12405cee9645SThomas Gleixner  *
12415cee9645SThomas Gleixner  *    CPU0                             CPU1
12425cee9645SThomas Gleixner  *    ----                             ----
12435cee9645SThomas Gleixner  *                                   <SOFTIRQ>
12445cee9645SThomas Gleixner  *                                   call_timer_fn();
12455cee9645SThomas Gleixner  *                                     base->running_timer = mytimer;
12465cee9645SThomas Gleixner  *  spin_lock_irq(somelock);
12475cee9645SThomas Gleixner  *                                     <IRQ>
12485cee9645SThomas Gleixner  *                                        spin_lock(somelock);
12495cee9645SThomas Gleixner  *  del_timer_sync(mytimer);
12505cee9645SThomas Gleixner  *   while (base->running_timer == mytimer);
12515cee9645SThomas Gleixner  *
12525cee9645SThomas Gleixner  * Now del_timer_sync() will never return and never release somelock.
12535cee9645SThomas Gleixner  * The interrupt on the other CPU is waiting to grab somelock but
12545cee9645SThomas Gleixner  * it has interrupted the softirq that CPU0 is waiting to finish.
12555cee9645SThomas Gleixner  *
12565cee9645SThomas Gleixner  * The function returns whether it has deactivated a pending timer or not.
12575cee9645SThomas Gleixner  */
12585cee9645SThomas Gleixner int del_timer_sync(struct timer_list *timer)
12595cee9645SThomas Gleixner {
12605cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP
12615cee9645SThomas Gleixner 	unsigned long flags;
12625cee9645SThomas Gleixner 
12635cee9645SThomas Gleixner 	/*
12645cee9645SThomas Gleixner 	 * If lockdep gives a backtrace here, please reference
12655cee9645SThomas Gleixner 	 * the synchronization rules above.
12665cee9645SThomas Gleixner 	 */
12675cee9645SThomas Gleixner 	local_irq_save(flags);
12685cee9645SThomas Gleixner 	lock_map_acquire(&timer->lockdep_map);
12695cee9645SThomas Gleixner 	lock_map_release(&timer->lockdep_map);
12705cee9645SThomas Gleixner 	local_irq_restore(flags);
12715cee9645SThomas Gleixner #endif
12725cee9645SThomas Gleixner 	/*
12735cee9645SThomas Gleixner 	 * don't use it in hardirq context, because it
12745cee9645SThomas Gleixner 	 * could lead to deadlock.
12755cee9645SThomas Gleixner 	 */
12760eeda71bSThomas Gleixner 	WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
12775cee9645SThomas Gleixner 	for (;;) {
12785cee9645SThomas Gleixner 		int ret = try_to_del_timer_sync(timer);
12795cee9645SThomas Gleixner 		if (ret >= 0)
12805cee9645SThomas Gleixner 			return ret;
12815cee9645SThomas Gleixner 		cpu_relax();
12825cee9645SThomas Gleixner 	}
12835cee9645SThomas Gleixner }
12845cee9645SThomas Gleixner EXPORT_SYMBOL(del_timer_sync);
12855cee9645SThomas Gleixner #endif
12865cee9645SThomas Gleixner 
1287*c1eba5bcSKees Cook static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long))
12885cee9645SThomas Gleixner {
12895cee9645SThomas Gleixner 	int count = preempt_count();
12905cee9645SThomas Gleixner 
12915cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP
12925cee9645SThomas Gleixner 	/*
12935cee9645SThomas Gleixner 	 * It is permissible to free the timer from inside the
12945cee9645SThomas Gleixner 	 * function that is called from it, this we need to take into
12955cee9645SThomas Gleixner 	 * account for lockdep too. To avoid bogus "held lock freed"
12965cee9645SThomas Gleixner 	 * warnings as well as problems when looking into
12975cee9645SThomas Gleixner 	 * timer->lockdep_map, make a copy and use that here.
12985cee9645SThomas Gleixner 	 */
12995cee9645SThomas Gleixner 	struct lockdep_map lockdep_map;
13005cee9645SThomas Gleixner 
13015cee9645SThomas Gleixner 	lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
13025cee9645SThomas Gleixner #endif
13035cee9645SThomas Gleixner 	/*
13045cee9645SThomas Gleixner 	 * Couple the lock chain with the lock chain at
13055cee9645SThomas Gleixner 	 * del_timer_sync() by acquiring the lock_map around the fn()
13065cee9645SThomas Gleixner 	 * call here and in del_timer_sync().
13075cee9645SThomas Gleixner 	 */
13085cee9645SThomas Gleixner 	lock_map_acquire(&lockdep_map);
13095cee9645SThomas Gleixner 
13105cee9645SThomas Gleixner 	trace_timer_expire_entry(timer);
1311*c1eba5bcSKees Cook 	fn((TIMER_DATA_TYPE)timer);
13125cee9645SThomas Gleixner 	trace_timer_expire_exit(timer);
13135cee9645SThomas Gleixner 
13145cee9645SThomas Gleixner 	lock_map_release(&lockdep_map);
13155cee9645SThomas Gleixner 
13165cee9645SThomas Gleixner 	if (count != preempt_count()) {
13175cee9645SThomas Gleixner 		WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
13185cee9645SThomas Gleixner 			  fn, count, preempt_count());
13195cee9645SThomas Gleixner 		/*
13205cee9645SThomas Gleixner 		 * Restore the preempt count. That gives us a decent
13215cee9645SThomas Gleixner 		 * chance to survive and extract information. If the
13225cee9645SThomas Gleixner 		 * callback kept a lock held, bad luck, but not worse
13235cee9645SThomas Gleixner 		 * than the BUG() we had.
13245cee9645SThomas Gleixner 		 */
13255cee9645SThomas Gleixner 		preempt_count_set(count);
13265cee9645SThomas Gleixner 	}
13275cee9645SThomas Gleixner }
13285cee9645SThomas Gleixner 
1329500462a9SThomas Gleixner static void expire_timers(struct timer_base *base, struct hlist_head *head)
13305cee9645SThomas Gleixner {
13311dabbcecSThomas Gleixner 	while (!hlist_empty(head)) {
1332500462a9SThomas Gleixner 		struct timer_list *timer;
13335cee9645SThomas Gleixner 		void (*fn)(unsigned long);
13345cee9645SThomas Gleixner 
13351dabbcecSThomas Gleixner 		timer = hlist_entry(head->first, struct timer_list, entry);
13365cee9645SThomas Gleixner 
13375cee9645SThomas Gleixner 		base->running_timer = timer;
1338500462a9SThomas Gleixner 		detach_timer(timer, true);
13395cee9645SThomas Gleixner 
1340500462a9SThomas Gleixner 		fn = timer->function;
1341500462a9SThomas Gleixner 
1342500462a9SThomas Gleixner 		if (timer->flags & TIMER_IRQSAFE) {
13432287d866SSebastian Andrzej Siewior 			raw_spin_unlock(&base->lock);
1344*c1eba5bcSKees Cook 			call_timer_fn(timer, fn);
13452287d866SSebastian Andrzej Siewior 			raw_spin_lock(&base->lock);
13465cee9645SThomas Gleixner 		} else {
13472287d866SSebastian Andrzej Siewior 			raw_spin_unlock_irq(&base->lock);
1348*c1eba5bcSKees Cook 			call_timer_fn(timer, fn);
13492287d866SSebastian Andrzej Siewior 			raw_spin_lock_irq(&base->lock);
13505cee9645SThomas Gleixner 		}
13515cee9645SThomas Gleixner 	}
13525cee9645SThomas Gleixner }
1353500462a9SThomas Gleixner 
135423696838SAnna-Maria Gleixner static int __collect_expired_timers(struct timer_base *base,
1355500462a9SThomas Gleixner 				    struct hlist_head *heads)
1356500462a9SThomas Gleixner {
1357500462a9SThomas Gleixner 	unsigned long clk = base->clk;
1358500462a9SThomas Gleixner 	struct hlist_head *vec;
1359500462a9SThomas Gleixner 	int i, levels = 0;
1360500462a9SThomas Gleixner 	unsigned int idx;
1361500462a9SThomas Gleixner 
1362500462a9SThomas Gleixner 	for (i = 0; i < LVL_DEPTH; i++) {
1363500462a9SThomas Gleixner 		idx = (clk & LVL_MASK) + i * LVL_SIZE;
1364500462a9SThomas Gleixner 
1365500462a9SThomas Gleixner 		if (__test_and_clear_bit(idx, base->pending_map)) {
1366500462a9SThomas Gleixner 			vec = base->vectors + idx;
1367500462a9SThomas Gleixner 			hlist_move_list(vec, heads++);
1368500462a9SThomas Gleixner 			levels++;
1369500462a9SThomas Gleixner 		}
1370500462a9SThomas Gleixner 		/* Is it time to look at the next level? */
1371500462a9SThomas Gleixner 		if (clk & LVL_CLK_MASK)
1372500462a9SThomas Gleixner 			break;
1373500462a9SThomas Gleixner 		/* Shift clock for the next level granularity */
1374500462a9SThomas Gleixner 		clk >>= LVL_CLK_SHIFT;
1375500462a9SThomas Gleixner 	}
1376500462a9SThomas Gleixner 	return levels;
13775cee9645SThomas Gleixner }
13785cee9645SThomas Gleixner 
13795cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
13805cee9645SThomas Gleixner /*
138123696838SAnna-Maria Gleixner  * Find the next pending bucket of a level. Search from level start (@offset)
138223696838SAnna-Maria Gleixner  * + @clk upwards and if nothing there, search from start of the level
138323696838SAnna-Maria Gleixner  * (@offset) up to @offset + clk.
13845cee9645SThomas Gleixner  */
1385500462a9SThomas Gleixner static int next_pending_bucket(struct timer_base *base, unsigned offset,
1386500462a9SThomas Gleixner 			       unsigned clk)
13875cee9645SThomas Gleixner {
1388500462a9SThomas Gleixner 	unsigned pos, start = offset + clk;
1389500462a9SThomas Gleixner 	unsigned end = offset + LVL_SIZE;
13905cee9645SThomas Gleixner 
1391500462a9SThomas Gleixner 	pos = find_next_bit(base->pending_map, end, start);
1392500462a9SThomas Gleixner 	if (pos < end)
1393500462a9SThomas Gleixner 		return pos - start;
13945cee9645SThomas Gleixner 
1395500462a9SThomas Gleixner 	pos = find_next_bit(base->pending_map, start, offset);
1396500462a9SThomas Gleixner 	return pos < start ? pos + LVL_SIZE - start : -1;
13975cee9645SThomas Gleixner }
13985cee9645SThomas Gleixner 
1399500462a9SThomas Gleixner /*
140023696838SAnna-Maria Gleixner  * Search the first expiring timer in the various clock levels. Caller must
140123696838SAnna-Maria Gleixner  * hold base->lock.
14025cee9645SThomas Gleixner  */
1403494af3edSThomas Gleixner static unsigned long __next_timer_interrupt(struct timer_base *base)
14045cee9645SThomas Gleixner {
1405500462a9SThomas Gleixner 	unsigned long clk, next, adj;
1406500462a9SThomas Gleixner 	unsigned lvl, offset = 0;
14075cee9645SThomas Gleixner 
1408500462a9SThomas Gleixner 	next = base->clk + NEXT_TIMER_MAX_DELTA;
1409500462a9SThomas Gleixner 	clk = base->clk;
1410500462a9SThomas Gleixner 	for (lvl = 0; lvl < LVL_DEPTH; lvl++, offset += LVL_SIZE) {
1411500462a9SThomas Gleixner 		int pos = next_pending_bucket(base, offset, clk & LVL_MASK);
14125cee9645SThomas Gleixner 
1413500462a9SThomas Gleixner 		if (pos >= 0) {
1414500462a9SThomas Gleixner 			unsigned long tmp = clk + (unsigned long) pos;
14155cee9645SThomas Gleixner 
1416500462a9SThomas Gleixner 			tmp <<= LVL_SHIFT(lvl);
1417500462a9SThomas Gleixner 			if (time_before(tmp, next))
1418500462a9SThomas Gleixner 				next = tmp;
14195cee9645SThomas Gleixner 		}
14205cee9645SThomas Gleixner 		/*
1421500462a9SThomas Gleixner 		 * Clock for the next level. If the current level clock lower
1422500462a9SThomas Gleixner 		 * bits are zero, we look at the next level as is. If not we
1423500462a9SThomas Gleixner 		 * need to advance it by one because that's going to be the
1424500462a9SThomas Gleixner 		 * next expiring bucket in that level. base->clk is the next
1425500462a9SThomas Gleixner 		 * expiring jiffie. So in case of:
1426500462a9SThomas Gleixner 		 *
1427500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1428500462a9SThomas Gleixner 		 *  0    0    0    0    0    0
1429500462a9SThomas Gleixner 		 *
1430500462a9SThomas Gleixner 		 * we have to look at all levels @index 0. With
1431500462a9SThomas Gleixner 		 *
1432500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1433500462a9SThomas Gleixner 		 *  0    0    0    0    0    2
1434500462a9SThomas Gleixner 		 *
1435500462a9SThomas Gleixner 		 * LVL0 has the next expiring bucket @index 2. The upper
1436500462a9SThomas Gleixner 		 * levels have the next expiring bucket @index 1.
1437500462a9SThomas Gleixner 		 *
1438500462a9SThomas Gleixner 		 * In case that the propagation wraps the next level the same
1439500462a9SThomas Gleixner 		 * rules apply:
1440500462a9SThomas Gleixner 		 *
1441500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1442500462a9SThomas Gleixner 		 *  0    0    0    0    F    2
1443500462a9SThomas Gleixner 		 *
1444500462a9SThomas Gleixner 		 * So after looking at LVL0 we get:
1445500462a9SThomas Gleixner 		 *
1446500462a9SThomas Gleixner 		 * LVL5 LVL4 LVL3 LVL2 LVL1
1447500462a9SThomas Gleixner 		 *  0    0    0    1    0
1448500462a9SThomas Gleixner 		 *
1449500462a9SThomas Gleixner 		 * So no propagation from LVL1 to LVL2 because that happened
1450500462a9SThomas Gleixner 		 * with the add already, but then we need to propagate further
1451500462a9SThomas Gleixner 		 * from LVL2 to LVL3.
1452500462a9SThomas Gleixner 		 *
1453500462a9SThomas Gleixner 		 * So the simple check whether the lower bits of the current
1454500462a9SThomas Gleixner 		 * level are 0 or not is sufficient for all cases.
14555cee9645SThomas Gleixner 		 */
1456500462a9SThomas Gleixner 		adj = clk & LVL_CLK_MASK ? 1 : 0;
1457500462a9SThomas Gleixner 		clk >>= LVL_CLK_SHIFT;
1458500462a9SThomas Gleixner 		clk += adj;
14595cee9645SThomas Gleixner 	}
1460500462a9SThomas Gleixner 	return next;
14615cee9645SThomas Gleixner }
14625cee9645SThomas Gleixner 
14635cee9645SThomas Gleixner /*
14645cee9645SThomas Gleixner  * Check, if the next hrtimer event is before the next timer wheel
14655cee9645SThomas Gleixner  * event:
14665cee9645SThomas Gleixner  */
1467c1ad348bSThomas Gleixner static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
14685cee9645SThomas Gleixner {
1469c1ad348bSThomas Gleixner 	u64 nextevt = hrtimer_get_next_event();
14705cee9645SThomas Gleixner 
1471c1ad348bSThomas Gleixner 	/*
1472c1ad348bSThomas Gleixner 	 * If high resolution timers are enabled
1473c1ad348bSThomas Gleixner 	 * hrtimer_get_next_event() returns KTIME_MAX.
1474c1ad348bSThomas Gleixner 	 */
1475c1ad348bSThomas Gleixner 	if (expires <= nextevt)
14765cee9645SThomas Gleixner 		return expires;
14775cee9645SThomas Gleixner 
14785cee9645SThomas Gleixner 	/*
1479c1ad348bSThomas Gleixner 	 * If the next timer is already expired, return the tick base
1480c1ad348bSThomas Gleixner 	 * time so the tick is fired immediately.
14815cee9645SThomas Gleixner 	 */
1482c1ad348bSThomas Gleixner 	if (nextevt <= basem)
1483c1ad348bSThomas Gleixner 		return basem;
14845cee9645SThomas Gleixner 
14855cee9645SThomas Gleixner 	/*
1486c1ad348bSThomas Gleixner 	 * Round up to the next jiffie. High resolution timers are
1487c1ad348bSThomas Gleixner 	 * off, so the hrtimers are expired in the tick and we need to
1488c1ad348bSThomas Gleixner 	 * make sure that this tick really expires the timer to avoid
1489c1ad348bSThomas Gleixner 	 * a ping pong of the nohz stop code.
1490c1ad348bSThomas Gleixner 	 *
1491c1ad348bSThomas Gleixner 	 * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
14925cee9645SThomas Gleixner 	 */
1493c1ad348bSThomas Gleixner 	return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
14945cee9645SThomas Gleixner }
14955cee9645SThomas Gleixner 
14965cee9645SThomas Gleixner /**
1497c1ad348bSThomas Gleixner  * get_next_timer_interrupt - return the time (clock mono) of the next timer
1498c1ad348bSThomas Gleixner  * @basej:	base time jiffies
1499c1ad348bSThomas Gleixner  * @basem:	base time clock monotonic
1500c1ad348bSThomas Gleixner  *
1501c1ad348bSThomas Gleixner  * Returns the tick aligned clock monotonic time of the next pending
1502c1ad348bSThomas Gleixner  * timer or KTIME_MAX if no timer is pending.
15035cee9645SThomas Gleixner  */
1504c1ad348bSThomas Gleixner u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
15055cee9645SThomas Gleixner {
1506500462a9SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1507c1ad348bSThomas Gleixner 	u64 expires = KTIME_MAX;
1508c1ad348bSThomas Gleixner 	unsigned long nextevt;
150946c8f0b0SChris Metcalf 	bool is_max_delta;
15105cee9645SThomas Gleixner 
15115cee9645SThomas Gleixner 	/*
15125cee9645SThomas Gleixner 	 * Pretend that there is no timer pending if the cpu is offline.
15135cee9645SThomas Gleixner 	 * Possible pending timers will be migrated later to an active cpu.
15145cee9645SThomas Gleixner 	 */
15155cee9645SThomas Gleixner 	if (cpu_is_offline(smp_processor_id()))
15165cee9645SThomas Gleixner 		return expires;
15175cee9645SThomas Gleixner 
15182287d866SSebastian Andrzej Siewior 	raw_spin_lock(&base->lock);
1519500462a9SThomas Gleixner 	nextevt = __next_timer_interrupt(base);
152046c8f0b0SChris Metcalf 	is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA);
1521a683f390SThomas Gleixner 	base->next_expiry = nextevt;
1522a683f390SThomas Gleixner 	/*
1523041ad7bcSThomas Gleixner 	 * We have a fresh next event. Check whether we can forward the
1524041ad7bcSThomas Gleixner 	 * base. We can only do that when @basej is past base->clk
1525041ad7bcSThomas Gleixner 	 * otherwise we might rewind base->clk.
1526a683f390SThomas Gleixner 	 */
1527041ad7bcSThomas Gleixner 	if (time_after(basej, base->clk)) {
1528041ad7bcSThomas Gleixner 		if (time_after(nextevt, basej))
1529041ad7bcSThomas Gleixner 			base->clk = basej;
1530a683f390SThomas Gleixner 		else if (time_after(nextevt, base->clk))
1531a683f390SThomas Gleixner 			base->clk = nextevt;
1532041ad7bcSThomas Gleixner 	}
1533a683f390SThomas Gleixner 
1534a683f390SThomas Gleixner 	if (time_before_eq(nextevt, basej)) {
1535c1ad348bSThomas Gleixner 		expires = basem;
1536a683f390SThomas Gleixner 		base->is_idle = false;
1537a683f390SThomas Gleixner 	} else {
153846c8f0b0SChris Metcalf 		if (!is_max_delta)
153934f41c03SMatija Glavinic Pecotic 			expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
1540a683f390SThomas Gleixner 		/*
15412fe59f50SNicholas Piggin 		 * If we expect to sleep more than a tick, mark the base idle.
15422fe59f50SNicholas Piggin 		 * Also the tick is stopped so any added timer must forward
15432fe59f50SNicholas Piggin 		 * the base clk itself to keep granularity small. This idle
15442fe59f50SNicholas Piggin 		 * logic is only maintained for the BASE_STD base, deferrable
15452fe59f50SNicholas Piggin 		 * timers may still see large granularity skew (by design).
1546a683f390SThomas Gleixner 		 */
15472fe59f50SNicholas Piggin 		if ((expires - basem) > TICK_NSEC) {
15482fe59f50SNicholas Piggin 			base->must_forward_clk = true;
1549a683f390SThomas Gleixner 			base->is_idle = true;
15505cee9645SThomas Gleixner 		}
15512fe59f50SNicholas Piggin 	}
15522287d866SSebastian Andrzej Siewior 	raw_spin_unlock(&base->lock);
15535cee9645SThomas Gleixner 
1554c1ad348bSThomas Gleixner 	return cmp_next_hrtimer_event(basem, expires);
15555cee9645SThomas Gleixner }
155623696838SAnna-Maria Gleixner 
1557a683f390SThomas Gleixner /**
1558a683f390SThomas Gleixner  * timer_clear_idle - Clear the idle state of the timer base
1559a683f390SThomas Gleixner  *
1560a683f390SThomas Gleixner  * Called with interrupts disabled
1561a683f390SThomas Gleixner  */
1562a683f390SThomas Gleixner void timer_clear_idle(void)
1563a683f390SThomas Gleixner {
1564a683f390SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1565a683f390SThomas Gleixner 
1566a683f390SThomas Gleixner 	/*
1567a683f390SThomas Gleixner 	 * We do this unlocked. The worst outcome is a remote enqueue sending
1568a683f390SThomas Gleixner 	 * a pointless IPI, but taking the lock would just make the window for
1569a683f390SThomas Gleixner 	 * sending the IPI a few instructions smaller for the cost of taking
1570a683f390SThomas Gleixner 	 * the lock in the exit from idle path.
1571a683f390SThomas Gleixner 	 */
1572a683f390SThomas Gleixner 	base->is_idle = false;
1573a683f390SThomas Gleixner }
1574a683f390SThomas Gleixner 
157523696838SAnna-Maria Gleixner static int collect_expired_timers(struct timer_base *base,
157623696838SAnna-Maria Gleixner 				  struct hlist_head *heads)
157723696838SAnna-Maria Gleixner {
157823696838SAnna-Maria Gleixner 	/*
157923696838SAnna-Maria Gleixner 	 * NOHZ optimization. After a long idle sleep we need to forward the
158023696838SAnna-Maria Gleixner 	 * base to current jiffies. Avoid a loop by searching the bitfield for
158123696838SAnna-Maria Gleixner 	 * the next expiring timer.
158223696838SAnna-Maria Gleixner 	 */
158323696838SAnna-Maria Gleixner 	if ((long)(jiffies - base->clk) > 2) {
158423696838SAnna-Maria Gleixner 		unsigned long next = __next_timer_interrupt(base);
158523696838SAnna-Maria Gleixner 
158623696838SAnna-Maria Gleixner 		/*
158723696838SAnna-Maria Gleixner 		 * If the next timer is ahead of time forward to current
1588a683f390SThomas Gleixner 		 * jiffies, otherwise forward to the next expiry time:
158923696838SAnna-Maria Gleixner 		 */
159023696838SAnna-Maria Gleixner 		if (time_after(next, jiffies)) {
1591c310ce4dSZhenzhong Duan 			/*
1592c310ce4dSZhenzhong Duan 			 * The call site will increment base->clk and then
1593c310ce4dSZhenzhong Duan 			 * terminate the expiry loop immediately.
1594c310ce4dSZhenzhong Duan 			 */
1595c310ce4dSZhenzhong Duan 			base->clk = jiffies;
159623696838SAnna-Maria Gleixner 			return 0;
159723696838SAnna-Maria Gleixner 		}
159823696838SAnna-Maria Gleixner 		base->clk = next;
159923696838SAnna-Maria Gleixner 	}
160023696838SAnna-Maria Gleixner 	return __collect_expired_timers(base, heads);
160123696838SAnna-Maria Gleixner }
160223696838SAnna-Maria Gleixner #else
160323696838SAnna-Maria Gleixner static inline int collect_expired_timers(struct timer_base *base,
160423696838SAnna-Maria Gleixner 					 struct hlist_head *heads)
160523696838SAnna-Maria Gleixner {
160623696838SAnna-Maria Gleixner 	return __collect_expired_timers(base, heads);
160723696838SAnna-Maria Gleixner }
16085cee9645SThomas Gleixner #endif
16095cee9645SThomas Gleixner 
16105cee9645SThomas Gleixner /*
16115cee9645SThomas Gleixner  * Called from the timer interrupt handler to charge one tick to the current
16125cee9645SThomas Gleixner  * process.  user_tick is 1 if the tick is user time, 0 for system.
16135cee9645SThomas Gleixner  */
16145cee9645SThomas Gleixner void update_process_times(int user_tick)
16155cee9645SThomas Gleixner {
16165cee9645SThomas Gleixner 	struct task_struct *p = current;
16175cee9645SThomas Gleixner 
16185cee9645SThomas Gleixner 	/* Note: this timer irq context must be accounted for as well. */
16195cee9645SThomas Gleixner 	account_process_tick(p, user_tick);
16205cee9645SThomas Gleixner 	run_local_timers();
1621c3377c2dSPaul E. McKenney 	rcu_check_callbacks(user_tick);
16225cee9645SThomas Gleixner #ifdef CONFIG_IRQ_WORK
16235cee9645SThomas Gleixner 	if (in_irq())
162476a33061SFrederic Weisbecker 		irq_work_tick();
16255cee9645SThomas Gleixner #endif
16265cee9645SThomas Gleixner 	scheduler_tick();
1627baa73d9eSNicolas Pitre 	if (IS_ENABLED(CONFIG_POSIX_TIMERS))
16285cee9645SThomas Gleixner 		run_posix_cpu_timers(p);
16295cee9645SThomas Gleixner }
16305cee9645SThomas Gleixner 
163173420feaSAnna-Maria Gleixner /**
163273420feaSAnna-Maria Gleixner  * __run_timers - run all expired timers (if any) on this CPU.
163373420feaSAnna-Maria Gleixner  * @base: the timer vector to be processed.
163473420feaSAnna-Maria Gleixner  */
163573420feaSAnna-Maria Gleixner static inline void __run_timers(struct timer_base *base)
163673420feaSAnna-Maria Gleixner {
163773420feaSAnna-Maria Gleixner 	struct hlist_head heads[LVL_DEPTH];
163873420feaSAnna-Maria Gleixner 	int levels;
163973420feaSAnna-Maria Gleixner 
164073420feaSAnna-Maria Gleixner 	if (!time_after_eq(jiffies, base->clk))
164173420feaSAnna-Maria Gleixner 		return;
164273420feaSAnna-Maria Gleixner 
16432287d866SSebastian Andrzej Siewior 	raw_spin_lock_irq(&base->lock);
164473420feaSAnna-Maria Gleixner 
164573420feaSAnna-Maria Gleixner 	while (time_after_eq(jiffies, base->clk)) {
164673420feaSAnna-Maria Gleixner 
164773420feaSAnna-Maria Gleixner 		levels = collect_expired_timers(base, heads);
164873420feaSAnna-Maria Gleixner 		base->clk++;
164973420feaSAnna-Maria Gleixner 
165073420feaSAnna-Maria Gleixner 		while (levels--)
165173420feaSAnna-Maria Gleixner 			expire_timers(base, heads + levels);
165273420feaSAnna-Maria Gleixner 	}
165373420feaSAnna-Maria Gleixner 	base->running_timer = NULL;
16542287d866SSebastian Andrzej Siewior 	raw_spin_unlock_irq(&base->lock);
165573420feaSAnna-Maria Gleixner }
165673420feaSAnna-Maria Gleixner 
16575cee9645SThomas Gleixner /*
16585cee9645SThomas Gleixner  * This function runs timers and the timer-tq in bottom half context.
16595cee9645SThomas Gleixner  */
16600766f788SEmese Revfy static __latent_entropy void run_timer_softirq(struct softirq_action *h)
16615cee9645SThomas Gleixner {
1662500462a9SThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
16635cee9645SThomas Gleixner 
16642fe59f50SNicholas Piggin 	/*
16652fe59f50SNicholas Piggin 	 * must_forward_clk must be cleared before running timers so that any
16662fe59f50SNicholas Piggin 	 * timer functions that call mod_timer will not try to forward the
16672fe59f50SNicholas Piggin 	 * base. idle trcking / clock forwarding logic is only used with
16682fe59f50SNicholas Piggin 	 * BASE_STD timers.
16692fe59f50SNicholas Piggin 	 *
16702fe59f50SNicholas Piggin 	 * The deferrable base does not do idle tracking at all, so we do
16712fe59f50SNicholas Piggin 	 * not forward it. This can result in very large variations in
16722fe59f50SNicholas Piggin 	 * granularity for deferrable timers, but they can be deferred for
16732fe59f50SNicholas Piggin 	 * long periods due to idle.
16742fe59f50SNicholas Piggin 	 */
16752fe59f50SNicholas Piggin 	base->must_forward_clk = false;
16762fe59f50SNicholas Piggin 
16775cee9645SThomas Gleixner 	__run_timers(base);
1678500462a9SThomas Gleixner 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active)
1679500462a9SThomas Gleixner 		__run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
16805cee9645SThomas Gleixner }
16815cee9645SThomas Gleixner 
16825cee9645SThomas Gleixner /*
16835cee9645SThomas Gleixner  * Called by the local, per-CPU timer interrupt on SMP.
16845cee9645SThomas Gleixner  */
16855cee9645SThomas Gleixner void run_local_timers(void)
16865cee9645SThomas Gleixner {
16874e85876aSThomas Gleixner 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
16884e85876aSThomas Gleixner 
16895cee9645SThomas Gleixner 	hrtimer_run_queues();
16904e85876aSThomas Gleixner 	/* Raise the softirq only if required. */
16914e85876aSThomas Gleixner 	if (time_before(jiffies, base->clk)) {
16924e85876aSThomas Gleixner 		if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active)
16934e85876aSThomas Gleixner 			return;
16944e85876aSThomas Gleixner 		/* CPU is awake, so check the deferrable base. */
16954e85876aSThomas Gleixner 		base++;
16964e85876aSThomas Gleixner 		if (time_before(jiffies, base->clk))
16974e85876aSThomas Gleixner 			return;
16984e85876aSThomas Gleixner 	}
16995cee9645SThomas Gleixner 	raise_softirq(TIMER_SOFTIRQ);
17005cee9645SThomas Gleixner }
17015cee9645SThomas Gleixner 
170258e1177bSKees Cook /*
170358e1177bSKees Cook  * Since schedule_timeout()'s timer is defined on the stack, it must store
170458e1177bSKees Cook  * the target task on the stack as well.
170558e1177bSKees Cook  */
170658e1177bSKees Cook struct process_timer {
170758e1177bSKees Cook 	struct timer_list timer;
170858e1177bSKees Cook 	struct task_struct *task;
170958e1177bSKees Cook };
171058e1177bSKees Cook 
171158e1177bSKees Cook static void process_timeout(struct timer_list *t)
17125cee9645SThomas Gleixner {
171358e1177bSKees Cook 	struct process_timer *timeout = from_timer(timeout, t, timer);
171458e1177bSKees Cook 
171558e1177bSKees Cook 	wake_up_process(timeout->task);
17165cee9645SThomas Gleixner }
17175cee9645SThomas Gleixner 
17185cee9645SThomas Gleixner /**
17195cee9645SThomas Gleixner  * schedule_timeout - sleep until timeout
17205cee9645SThomas Gleixner  * @timeout: timeout value in jiffies
17215cee9645SThomas Gleixner  *
17225cee9645SThomas Gleixner  * Make the current task sleep until @timeout jiffies have
17235cee9645SThomas Gleixner  * elapsed. The routine will return immediately unless
17245cee9645SThomas Gleixner  * the current task state has been set (see set_current_state()).
17255cee9645SThomas Gleixner  *
17265cee9645SThomas Gleixner  * You can set the task state as follows -
17275cee9645SThomas Gleixner  *
17285cee9645SThomas Gleixner  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
17294b7e9cf9SDouglas Anderson  * pass before the routine returns unless the current task is explicitly
17304b7e9cf9SDouglas Anderson  * woken up, (e.g. by wake_up_process())".
17315cee9645SThomas Gleixner  *
17325cee9645SThomas Gleixner  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
17334b7e9cf9SDouglas Anderson  * delivered to the current task or the current task is explicitly woken
17344b7e9cf9SDouglas Anderson  * up.
17355cee9645SThomas Gleixner  *
17365cee9645SThomas Gleixner  * The current task state is guaranteed to be TASK_RUNNING when this
17375cee9645SThomas Gleixner  * routine returns.
17385cee9645SThomas Gleixner  *
17395cee9645SThomas Gleixner  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
17405cee9645SThomas Gleixner  * the CPU away without a bound on the timeout. In this case the return
17415cee9645SThomas Gleixner  * value will be %MAX_SCHEDULE_TIMEOUT.
17425cee9645SThomas Gleixner  *
17434b7e9cf9SDouglas Anderson  * Returns 0 when the timer has expired otherwise the remaining time in
17444b7e9cf9SDouglas Anderson  * jiffies will be returned.  In all cases the return value is guaranteed
17454b7e9cf9SDouglas Anderson  * to be non-negative.
17465cee9645SThomas Gleixner  */
17475cee9645SThomas Gleixner signed long __sched schedule_timeout(signed long timeout)
17485cee9645SThomas Gleixner {
174958e1177bSKees Cook 	struct process_timer timer;
17505cee9645SThomas Gleixner 	unsigned long expire;
17515cee9645SThomas Gleixner 
17525cee9645SThomas Gleixner 	switch (timeout)
17535cee9645SThomas Gleixner 	{
17545cee9645SThomas Gleixner 	case MAX_SCHEDULE_TIMEOUT:
17555cee9645SThomas Gleixner 		/*
17565cee9645SThomas Gleixner 		 * These two special cases are useful to be comfortable
17575cee9645SThomas Gleixner 		 * in the caller. Nothing more. We could take
17585cee9645SThomas Gleixner 		 * MAX_SCHEDULE_TIMEOUT from one of the negative value
17595cee9645SThomas Gleixner 		 * but I' d like to return a valid offset (>=0) to allow
17605cee9645SThomas Gleixner 		 * the caller to do everything it want with the retval.
17615cee9645SThomas Gleixner 		 */
17625cee9645SThomas Gleixner 		schedule();
17635cee9645SThomas Gleixner 		goto out;
17645cee9645SThomas Gleixner 	default:
17655cee9645SThomas Gleixner 		/*
17665cee9645SThomas Gleixner 		 * Another bit of PARANOID. Note that the retval will be
17675cee9645SThomas Gleixner 		 * 0 since no piece of kernel is supposed to do a check
17685cee9645SThomas Gleixner 		 * for a negative retval of schedule_timeout() (since it
17695cee9645SThomas Gleixner 		 * should never happens anyway). You just have the printk()
17705cee9645SThomas Gleixner 		 * that will tell you if something is gone wrong and where.
17715cee9645SThomas Gleixner 		 */
17725cee9645SThomas Gleixner 		if (timeout < 0) {
17735cee9645SThomas Gleixner 			printk(KERN_ERR "schedule_timeout: wrong timeout "
17745cee9645SThomas Gleixner 				"value %lx\n", timeout);
17755cee9645SThomas Gleixner 			dump_stack();
17765cee9645SThomas Gleixner 			current->state = TASK_RUNNING;
17775cee9645SThomas Gleixner 			goto out;
17785cee9645SThomas Gleixner 		}
17795cee9645SThomas Gleixner 	}
17805cee9645SThomas Gleixner 
17815cee9645SThomas Gleixner 	expire = timeout + jiffies;
17825cee9645SThomas Gleixner 
178358e1177bSKees Cook 	timer.task = current;
178458e1177bSKees Cook 	timer_setup_on_stack(&timer.timer, process_timeout, 0);
1785b24591e2SDavid Howells 	__mod_timer(&timer.timer, expire, 0);
17865cee9645SThomas Gleixner 	schedule();
178758e1177bSKees Cook 	del_singleshot_timer_sync(&timer.timer);
17885cee9645SThomas Gleixner 
17895cee9645SThomas Gleixner 	/* Remove the timer from the object tracker */
179058e1177bSKees Cook 	destroy_timer_on_stack(&timer.timer);
17915cee9645SThomas Gleixner 
17925cee9645SThomas Gleixner 	timeout = expire - jiffies;
17935cee9645SThomas Gleixner 
17945cee9645SThomas Gleixner  out:
17955cee9645SThomas Gleixner 	return timeout < 0 ? 0 : timeout;
17965cee9645SThomas Gleixner }
17975cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout);
17985cee9645SThomas Gleixner 
17995cee9645SThomas Gleixner /*
18005cee9645SThomas Gleixner  * We can use __set_current_state() here because schedule_timeout() calls
18015cee9645SThomas Gleixner  * schedule() unconditionally.
18025cee9645SThomas Gleixner  */
18035cee9645SThomas Gleixner signed long __sched schedule_timeout_interruptible(signed long timeout)
18045cee9645SThomas Gleixner {
18055cee9645SThomas Gleixner 	__set_current_state(TASK_INTERRUPTIBLE);
18065cee9645SThomas Gleixner 	return schedule_timeout(timeout);
18075cee9645SThomas Gleixner }
18085cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_interruptible);
18095cee9645SThomas Gleixner 
18105cee9645SThomas Gleixner signed long __sched schedule_timeout_killable(signed long timeout)
18115cee9645SThomas Gleixner {
18125cee9645SThomas Gleixner 	__set_current_state(TASK_KILLABLE);
18135cee9645SThomas Gleixner 	return schedule_timeout(timeout);
18145cee9645SThomas Gleixner }
18155cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_killable);
18165cee9645SThomas Gleixner 
18175cee9645SThomas Gleixner signed long __sched schedule_timeout_uninterruptible(signed long timeout)
18185cee9645SThomas Gleixner {
18195cee9645SThomas Gleixner 	__set_current_state(TASK_UNINTERRUPTIBLE);
18205cee9645SThomas Gleixner 	return schedule_timeout(timeout);
18215cee9645SThomas Gleixner }
18225cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_uninterruptible);
18235cee9645SThomas Gleixner 
182469b27bafSAndrew Morton /*
182569b27bafSAndrew Morton  * Like schedule_timeout_uninterruptible(), except this task will not contribute
182669b27bafSAndrew Morton  * to load average.
182769b27bafSAndrew Morton  */
182869b27bafSAndrew Morton signed long __sched schedule_timeout_idle(signed long timeout)
182969b27bafSAndrew Morton {
183069b27bafSAndrew Morton 	__set_current_state(TASK_IDLE);
183169b27bafSAndrew Morton 	return schedule_timeout(timeout);
183269b27bafSAndrew Morton }
183369b27bafSAndrew Morton EXPORT_SYMBOL(schedule_timeout_idle);
183469b27bafSAndrew Morton 
18355cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
1836494af3edSThomas Gleixner static void migrate_timer_list(struct timer_base *new_base, struct hlist_head *head)
18375cee9645SThomas Gleixner {
18385cee9645SThomas Gleixner 	struct timer_list *timer;
18390eeda71bSThomas Gleixner 	int cpu = new_base->cpu;
18405cee9645SThomas Gleixner 
18411dabbcecSThomas Gleixner 	while (!hlist_empty(head)) {
18421dabbcecSThomas Gleixner 		timer = hlist_entry(head->first, struct timer_list, entry);
18435cee9645SThomas Gleixner 		detach_timer(timer, false);
18440eeda71bSThomas Gleixner 		timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
18455cee9645SThomas Gleixner 		internal_add_timer(new_base, timer);
18465cee9645SThomas Gleixner 	}
18475cee9645SThomas Gleixner }
18485cee9645SThomas Gleixner 
184924f73b99SRichard Cochran int timers_dead_cpu(unsigned int cpu)
18505cee9645SThomas Gleixner {
1851494af3edSThomas Gleixner 	struct timer_base *old_base;
1852494af3edSThomas Gleixner 	struct timer_base *new_base;
1853500462a9SThomas Gleixner 	int b, i;
18545cee9645SThomas Gleixner 
18555cee9645SThomas Gleixner 	BUG_ON(cpu_online(cpu));
1856500462a9SThomas Gleixner 
1857500462a9SThomas Gleixner 	for (b = 0; b < NR_BASES; b++) {
1858500462a9SThomas Gleixner 		old_base = per_cpu_ptr(&timer_bases[b], cpu);
1859500462a9SThomas Gleixner 		new_base = get_cpu_ptr(&timer_bases[b]);
18605cee9645SThomas Gleixner 		/*
18615cee9645SThomas Gleixner 		 * The caller is globally serialized and nobody else
18625cee9645SThomas Gleixner 		 * takes two locks at once, deadlock is not possible.
18635cee9645SThomas Gleixner 		 */
18642287d866SSebastian Andrzej Siewior 		raw_spin_lock_irq(&new_base->lock);
18652287d866SSebastian Andrzej Siewior 		raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
18665cee9645SThomas Gleixner 
18675cee9645SThomas Gleixner 		BUG_ON(old_base->running_timer);
18685cee9645SThomas Gleixner 
1869500462a9SThomas Gleixner 		for (i = 0; i < WHEEL_SIZE; i++)
1870500462a9SThomas Gleixner 			migrate_timer_list(new_base, old_base->vectors + i);
18718def9060SViresh Kumar 
18722287d866SSebastian Andrzej Siewior 		raw_spin_unlock(&old_base->lock);
18732287d866SSebastian Andrzej Siewior 		raw_spin_unlock_irq(&new_base->lock);
1874494af3edSThomas Gleixner 		put_cpu_ptr(&timer_bases);
18755cee9645SThomas Gleixner 	}
187624f73b99SRichard Cochran 	return 0;
18775cee9645SThomas Gleixner }
18785cee9645SThomas Gleixner 
18793650b57fSPeter Zijlstra #endif /* CONFIG_HOTPLUG_CPU */
18805cee9645SThomas Gleixner 
18810eeda71bSThomas Gleixner static void __init init_timer_cpu(int cpu)
18828def9060SViresh Kumar {
1883500462a9SThomas Gleixner 	struct timer_base *base;
1884500462a9SThomas Gleixner 	int i;
18853650b57fSPeter Zijlstra 
1886500462a9SThomas Gleixner 	for (i = 0; i < NR_BASES; i++) {
1887500462a9SThomas Gleixner 		base = per_cpu_ptr(&timer_bases[i], cpu);
18888def9060SViresh Kumar 		base->cpu = cpu;
18892287d866SSebastian Andrzej Siewior 		raw_spin_lock_init(&base->lock);
1890494af3edSThomas Gleixner 		base->clk = jiffies;
1891500462a9SThomas Gleixner 	}
18928def9060SViresh Kumar }
18938def9060SViresh Kumar 
18948def9060SViresh Kumar static void __init init_timer_cpus(void)
18958def9060SViresh Kumar {
18968def9060SViresh Kumar 	int cpu;
18978def9060SViresh Kumar 
18980eeda71bSThomas Gleixner 	for_each_possible_cpu(cpu)
18990eeda71bSThomas Gleixner 		init_timer_cpu(cpu);
19008def9060SViresh Kumar }
19015cee9645SThomas Gleixner 
19025cee9645SThomas Gleixner void __init init_timers(void)
19035cee9645SThomas Gleixner {
19048def9060SViresh Kumar 	init_timer_cpus();
19055cee9645SThomas Gleixner 	open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
19065cee9645SThomas Gleixner }
19075cee9645SThomas Gleixner 
19085cee9645SThomas Gleixner /**
19095cee9645SThomas Gleixner  * msleep - sleep safely even with waitqueue interruptions
19105cee9645SThomas Gleixner  * @msecs: Time in milliseconds to sleep for
19115cee9645SThomas Gleixner  */
19125cee9645SThomas Gleixner void msleep(unsigned int msecs)
19135cee9645SThomas Gleixner {
19145cee9645SThomas Gleixner 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
19155cee9645SThomas Gleixner 
19165cee9645SThomas Gleixner 	while (timeout)
19175cee9645SThomas Gleixner 		timeout = schedule_timeout_uninterruptible(timeout);
19185cee9645SThomas Gleixner }
19195cee9645SThomas Gleixner 
19205cee9645SThomas Gleixner EXPORT_SYMBOL(msleep);
19215cee9645SThomas Gleixner 
19225cee9645SThomas Gleixner /**
19235cee9645SThomas Gleixner  * msleep_interruptible - sleep waiting for signals
19245cee9645SThomas Gleixner  * @msecs: Time in milliseconds to sleep for
19255cee9645SThomas Gleixner  */
19265cee9645SThomas Gleixner unsigned long msleep_interruptible(unsigned int msecs)
19275cee9645SThomas Gleixner {
19285cee9645SThomas Gleixner 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
19295cee9645SThomas Gleixner 
19305cee9645SThomas Gleixner 	while (timeout && !signal_pending(current))
19315cee9645SThomas Gleixner 		timeout = schedule_timeout_interruptible(timeout);
19325cee9645SThomas Gleixner 	return jiffies_to_msecs(timeout);
19335cee9645SThomas Gleixner }
19345cee9645SThomas Gleixner 
19355cee9645SThomas Gleixner EXPORT_SYMBOL(msleep_interruptible);
19365cee9645SThomas Gleixner 
19375cee9645SThomas Gleixner /**
1938b5227d03SBjorn Helgaas  * usleep_range - Sleep for an approximate time
19395cee9645SThomas Gleixner  * @min: Minimum time in usecs to sleep
19405cee9645SThomas Gleixner  * @max: Maximum time in usecs to sleep
1941b5227d03SBjorn Helgaas  *
1942b5227d03SBjorn Helgaas  * In non-atomic context where the exact wakeup time is flexible, use
1943b5227d03SBjorn Helgaas  * usleep_range() instead of udelay().  The sleep improves responsiveness
1944b5227d03SBjorn Helgaas  * by avoiding the CPU-hogging busy-wait of udelay(), and the range reduces
1945b5227d03SBjorn Helgaas  * power usage by allowing hrtimers to take advantage of an already-
1946b5227d03SBjorn Helgaas  * scheduled interrupt instead of scheduling a new one just for this sleep.
19475cee9645SThomas Gleixner  */
19482ad5d327SThomas Gleixner void __sched usleep_range(unsigned long min, unsigned long max)
19495cee9645SThomas Gleixner {
19506c5e9059SDouglas Anderson 	ktime_t exp = ktime_add_us(ktime_get(), min);
19516c5e9059SDouglas Anderson 	u64 delta = (u64)(max - min) * NSEC_PER_USEC;
19526c5e9059SDouglas Anderson 
19536c5e9059SDouglas Anderson 	for (;;) {
19545cee9645SThomas Gleixner 		__set_current_state(TASK_UNINTERRUPTIBLE);
19556c5e9059SDouglas Anderson 		/* Do not return before the requested sleep time has elapsed */
19566c5e9059SDouglas Anderson 		if (!schedule_hrtimeout_range(&exp, delta, HRTIMER_MODE_ABS))
19576c5e9059SDouglas Anderson 			break;
19586c5e9059SDouglas Anderson 	}
19595cee9645SThomas Gleixner }
19605cee9645SThomas Gleixner EXPORT_SYMBOL(usleep_range);
1961