blob: d111adf4a0cb4c304fe9a7cbb3fe7b3c4ae2a69b [file] [log] [blame]
Thomas Gleixner35728b82018-10-31 19:21:09 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Stephen Rothwell4a22f162013-04-30 15:27:37 -07003 * Kernel internal timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
8 *
9 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
10 * "A Kernel Model for Precision Timekeeping" by Dave Mills
11 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
12 * serialize accesses to xtime/lost_ticks).
13 * Copyright (C) 1998 Andrea Arcangeli
14 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
15 * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
16 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
17 * Copyright (C) 2000, 2001, 2002 Ingo Molnar
18 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
19 */
20
21#include <linux/kernel_stat.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040022#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/interrupt.h>
24#include <linux/percpu.h>
25#include <linux/init.h>
26#include <linux/mm.h>
27#include <linux/swap.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070028#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/notifier.h>
30#include <linux/thread_info.h>
31#include <linux/time.h>
32#include <linux/jiffies.h>
33#include <linux/posix-timers.h>
34#include <linux/cpu.h>
35#include <linux/syscalls.h>
Adrian Bunk97a41e22006-01-08 01:02:17 -080036#include <linux/delay.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080037#include <linux/tick.h>
Ingo Molnar82f67cd2007-02-16 01:28:13 -080038#include <linux/kallsyms.h>
Peter Zijlstrae360adb2010-10-14 14:01:34 +080039#include <linux/irq_work.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010040#include <linux/sched/signal.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -060041#include <linux/sched/sysctl.h>
Ingo Molnar370c9132017-02-08 18:51:35 +010042#include <linux/sched/nohz.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010043#include <linux/sched/debug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Stephen Rothwell1a0df592013-04-30 15:27:34 -070045#include <linux/compat.h>
Willy Tarreauf227e3e2020-07-10 15:23:19 +020046#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080048#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/unistd.h>
50#include <asm/div64.h>
51#include <asm/timex.h>
52#include <asm/io.h>
53
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +000054#include "tick-internal.h"
55
Xiao Guangrong2b022e32009-08-10 10:48:59 +080056#define CREATE_TRACE_POINTS
57#include <trace/events/timer.h>
58
Andi Kleen40747ff2014-02-08 08:51:59 +010059__visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
Thomas Gleixnerecea8d12005-10-30 15:03:00 -080060
61EXPORT_SYMBOL(jiffies_64);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
Thomas Gleixner500462a2016-07-04 09:50:30 +000064 * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
65 * LVL_SIZE buckets. Each level is driven by its own clock and therefor each
66 * level has a different granularity.
67 *
68 * The level granularity is: LVL_CLK_DIV ^ lvl
69 * The level clock frequency is: HZ / (LVL_CLK_DIV ^ level)
70 *
71 * The array level of a newly armed timer depends on the relative expiry
72 * time. The farther the expiry time is away the higher the array level and
73 * therefor the granularity becomes.
74 *
75 * Contrary to the original timer wheel implementation, which aims for 'exact'
76 * expiry of the timers, this implementation removes the need for recascading
77 * the timers into the lower array levels. The previous 'classic' timer wheel
78 * implementation of the kernel already violated the 'exact' expiry by adding
79 * slack to the expiry time to provide batched expiration. The granularity
80 * levels provide implicit batching.
81 *
82 * This is an optimization of the original timer wheel implementation for the
83 * majority of the timer wheel use cases: timeouts. The vast majority of
84 * timeout timers (networking, disk I/O ...) are canceled before expiry. If
85 * the timeout expires it indicates that normal operation is disturbed, so it
86 * does not matter much whether the timeout comes with a slight delay.
87 *
88 * The only exception to this are networking timers with a small expiry
89 * time. They rely on the granularity. Those fit into the first wheel level,
90 * which has HZ granularity.
91 *
92 * We don't have cascading anymore. timers with a expiry time above the
93 * capacity of the last wheel level are force expired at the maximum timeout
94 * value of the last wheel level. From data sampling we know that the maximum
95 * value observed is 5 days (network connection tracking), so this should not
96 * be an issue.
97 *
98 * The currently chosen array constants values are a good compromise between
99 * array size and granularity.
100 *
101 * This results in the following granularity and range levels:
102 *
103 * HZ 1000 steps
104 * Level Offset Granularity Range
105 * 0 0 1 ms 0 ms - 63 ms
106 * 1 64 8 ms 64 ms - 511 ms
107 * 2 128 64 ms 512 ms - 4095 ms (512ms - ~4s)
108 * 3 192 512 ms 4096 ms - 32767 ms (~4s - ~32s)
109 * 4 256 4096 ms (~4s) 32768 ms - 262143 ms (~32s - ~4m)
110 * 5 320 32768 ms (~32s) 262144 ms - 2097151 ms (~4m - ~34m)
111 * 6 384 262144 ms (~4m) 2097152 ms - 16777215 ms (~34m - ~4h)
112 * 7 448 2097152 ms (~34m) 16777216 ms - 134217727 ms (~4h - ~1d)
113 * 8 512 16777216 ms (~4h) 134217728 ms - 1073741822 ms (~1d - ~12d)
114 *
115 * HZ 300
116 * Level Offset Granularity Range
117 * 0 0 3 ms 0 ms - 210 ms
118 * 1 64 26 ms 213 ms - 1703 ms (213ms - ~1s)
119 * 2 128 213 ms 1706 ms - 13650 ms (~1s - ~13s)
120 * 3 192 1706 ms (~1s) 13653 ms - 109223 ms (~13s - ~1m)
121 * 4 256 13653 ms (~13s) 109226 ms - 873810 ms (~1m - ~14m)
122 * 5 320 109226 ms (~1m) 873813 ms - 6990503 ms (~14m - ~1h)
123 * 6 384 873813 ms (~14m) 6990506 ms - 55924050 ms (~1h - ~15h)
124 * 7 448 6990506 ms (~1h) 55924053 ms - 447392423 ms (~15h - ~5d)
125 * 8 512 55924053 ms (~15h) 447392426 ms - 3579139406 ms (~5d - ~41d)
126 *
127 * HZ 250
128 * Level Offset Granularity Range
129 * 0 0 4 ms 0 ms - 255 ms
130 * 1 64 32 ms 256 ms - 2047 ms (256ms - ~2s)
131 * 2 128 256 ms 2048 ms - 16383 ms (~2s - ~16s)
132 * 3 192 2048 ms (~2s) 16384 ms - 131071 ms (~16s - ~2m)
133 * 4 256 16384 ms (~16s) 131072 ms - 1048575 ms (~2m - ~17m)
134 * 5 320 131072 ms (~2m) 1048576 ms - 8388607 ms (~17m - ~2h)
135 * 6 384 1048576 ms (~17m) 8388608 ms - 67108863 ms (~2h - ~18h)
136 * 7 448 8388608 ms (~2h) 67108864 ms - 536870911 ms (~18h - ~6d)
137 * 8 512 67108864 ms (~18h) 536870912 ms - 4294967288 ms (~6d - ~49d)
138 *
139 * HZ 100
140 * Level Offset Granularity Range
141 * 0 0 10 ms 0 ms - 630 ms
142 * 1 64 80 ms 640 ms - 5110 ms (640ms - ~5s)
143 * 2 128 640 ms 5120 ms - 40950 ms (~5s - ~40s)
144 * 3 192 5120 ms (~5s) 40960 ms - 327670 ms (~40s - ~5m)
145 * 4 256 40960 ms (~40s) 327680 ms - 2621430 ms (~5m - ~43m)
146 * 5 320 327680 ms (~5m) 2621440 ms - 20971510 ms (~43m - ~5h)
147 * 6 384 2621440 ms (~43m) 20971520 ms - 167772150 ms (~5h - ~1d)
148 * 7 448 20971520 ms (~5h) 167772160 ms - 1342177270 ms (~1d - ~15d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Thomas Gleixner500462a2016-07-04 09:50:30 +0000151/* Clock divisor for the next level */
152#define LVL_CLK_SHIFT 3
153#define LVL_CLK_DIV (1UL << LVL_CLK_SHIFT)
154#define LVL_CLK_MASK (LVL_CLK_DIV - 1)
155#define LVL_SHIFT(n) ((n) * LVL_CLK_SHIFT)
156#define LVL_GRAN(n) (1UL << LVL_SHIFT(n))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Thomas Gleixner500462a2016-07-04 09:50:30 +0000158/*
159 * The time start value for each level to select the bucket at enqueue
Frederic Weisbecker44688972020-07-17 16:05:44 +0200160 * time. We start from the last possible delta of the previous level
161 * so that we can later add an extra LVL_GRAN(n) to n (see calc_index()).
Thomas Gleixner500462a2016-07-04 09:50:30 +0000162 */
163#define LVL_START(n) ((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Thomas Gleixner500462a2016-07-04 09:50:30 +0000165/* Size of each clock level */
166#define LVL_BITS 6
167#define LVL_SIZE (1UL << LVL_BITS)
168#define LVL_MASK (LVL_SIZE - 1)
169#define LVL_OFFS(n) ((n) * LVL_SIZE)
170
171/* Level depth */
172#if HZ > 100
173# define LVL_DEPTH 9
174# else
175# define LVL_DEPTH 8
176#endif
177
178/* The cutoff (max. capacity of the wheel) */
179#define WHEEL_TIMEOUT_CUTOFF (LVL_START(LVL_DEPTH))
180#define WHEEL_TIMEOUT_MAX (WHEEL_TIMEOUT_CUTOFF - LVL_GRAN(LVL_DEPTH - 1))
181
182/*
183 * The resulting wheel size. If NOHZ is configured we allocate two
184 * wheels so we have a separate storage for the deferrable timers.
185 */
186#define WHEEL_SIZE (LVL_SIZE * LVL_DEPTH)
187
188#ifdef CONFIG_NO_HZ_COMMON
189# define NR_BASES 2
190# define BASE_STD 0
191# define BASE_DEF 1
192#else
193# define NR_BASES 1
194# define BASE_STD 0
195# define BASE_DEF 0
196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Thomas Gleixner494af3e2016-07-04 09:50:28 +0000198struct timer_base {
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +0200199 raw_spinlock_t lock;
Thomas Gleixner500462a2016-07-04 09:50:30 +0000200 struct timer_list *running_timer;
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +0200201#ifdef CONFIG_PREEMPT_RT
202 spinlock_t expiry_lock;
203 atomic_t timer_waiters;
204#endif
Thomas Gleixner500462a2016-07-04 09:50:30 +0000205 unsigned long clk;
Thomas Gleixnera683f392016-07-04 09:50:36 +0000206 unsigned long next_expiry;
Thomas Gleixner500462a2016-07-04 09:50:30 +0000207 unsigned int cpu;
Frederic Weisbecker31cd0e12020-07-23 17:16:41 +0200208 bool next_expiry_recalc;
Thomas Gleixnera683f392016-07-04 09:50:36 +0000209 bool is_idle;
Thomas Gleixner500462a2016-07-04 09:50:30 +0000210 DECLARE_BITMAP(pending_map, WHEEL_SIZE);
211 struct hlist_head vectors[WHEEL_SIZE];
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700212} ____cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Thomas Gleixner500462a2016-07-04 09:50:30 +0000214static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
Venki Pallipadi6e453a62007-05-08 00:27:44 -0700215
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100216#ifdef CONFIG_NO_HZ_COMMON
217
Anna-Maria Gleixner14c80342017-12-21 11:41:49 +0100218static DEFINE_STATIC_KEY_FALSE(timers_nohz_active);
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100219static DEFINE_MUTEX(timer_keys_mutex);
220
221static void timer_update_keys(struct work_struct *work);
222static DECLARE_WORK(timer_update_work, timer_update_keys);
223
224#ifdef CONFIG_SMP
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000225unsigned int sysctl_timer_migration = 1;
226
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100227DEFINE_STATIC_KEY_FALSE(timers_migration_enabled);
228
229static void timers_update_migration(void)
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000230{
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100231 if (sysctl_timer_migration && tick_nohz_active)
232 static_branch_enable(&timers_migration_enabled);
233 else
234 static_branch_disable(&timers_migration_enabled);
235}
236#else
237static inline void timers_update_migration(void) { }
238#endif /* !CONFIG_SMP */
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000239
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100240static void timer_update_keys(struct work_struct *work)
241{
242 mutex_lock(&timer_keys_mutex);
243 timers_update_migration();
244 static_branch_enable(&timers_nohz_active);
245 mutex_unlock(&timer_keys_mutex);
246}
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000247
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100248void timers_update_nohz(void)
249{
250 schedule_work(&timer_update_work);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000251}
252
253int timer_migration_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200254 void *buffer, size_t *lenp, loff_t *ppos)
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000255{
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000256 int ret;
257
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100258 mutex_lock(&timer_keys_mutex);
Myungho Jungb94bf592017-04-19 15:24:50 -0700259 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000260 if (!ret && write)
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100261 timers_update_migration();
262 mutex_unlock(&timer_keys_mutex);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000263 return ret;
264}
Anna-Maria Gleixner14c80342017-12-21 11:41:49 +0100265
266static inline bool is_timers_nohz_active(void)
267{
268 return static_branch_unlikely(&timers_nohz_active);
269}
270#else
271static inline bool is_timers_nohz_active(void) { return false; }
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100272#endif /* NO_HZ_COMMON */
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000273
Alan Stern9c133c42008-11-06 08:42:48 +0100274static unsigned long round_jiffies_common(unsigned long j, int cpu,
275 bool force_up)
276{
277 int rem;
278 unsigned long original = j;
279
280 /*
281 * We don't want all cpus firing their timers at once hitting the
282 * same lock or cachelines, so we skew each extra cpu with an extra
283 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
284 * already did this.
285 * The skew is done by adding 3*cpunr, then round, then subtract this
286 * extra offset again.
287 */
288 j += cpu * 3;
289
290 rem = j % HZ;
291
292 /*
293 * If the target jiffie is just after a whole second (which can happen
294 * due to delays of the timer irq, long irq off times etc etc) then
295 * we should round down to the whole second, not up. Use 1/4th second
296 * as cutoff for this rounding as an extreme upper bound for this.
297 * But never round down if @force_up is set.
298 */
299 if (rem < HZ/4 && !force_up) /* round down */
300 j = j - rem;
301 else /* round up */
302 j = j - rem + HZ;
303
304 /* now that we have rounded, subtract the extra skew again */
305 j -= cpu * 3;
306
Bart Van Assche9e04d382013-05-21 20:43:50 +0200307 /*
308 * Make sure j is still in the future. Otherwise return the
309 * unmodified value.
310 */
311 return time_is_after_jiffies(j) ? j : original;
Alan Stern9c133c42008-11-06 08:42:48 +0100312}
313
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800314/**
315 * __round_jiffies - function to round jiffies to a full second
316 * @j: the time in (absolute) jiffies that should be rounded
317 * @cpu: the processor number on which the timeout will happen
318 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800319 * __round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800320 * up or down to (approximately) full seconds. This is useful for timers
321 * for which the exact time they fire does not matter too much, as long as
322 * they fire approximately every X seconds.
323 *
324 * By rounding these timers to whole seconds, all such timers will fire
325 * at the same time, rather than at various times spread out. The goal
326 * of this is to have the CPU wake up less, which saves power.
327 *
328 * The exact rounding is skewed for each processor to avoid all
329 * processors firing at the exact same time, which could lead
330 * to lock contention or spurious cache line bouncing.
331 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800332 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800333 */
334unsigned long __round_jiffies(unsigned long j, int cpu)
335{
Alan Stern9c133c42008-11-06 08:42:48 +0100336 return round_jiffies_common(j, cpu, false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800337}
338EXPORT_SYMBOL_GPL(__round_jiffies);
339
340/**
341 * __round_jiffies_relative - function to round jiffies to a full second
342 * @j: the time in (relative) jiffies that should be rounded
343 * @cpu: the processor number on which the timeout will happen
344 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800345 * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800346 * up or down to (approximately) full seconds. This is useful for timers
347 * for which the exact time they fire does not matter too much, as long as
348 * they fire approximately every X seconds.
349 *
350 * By rounding these timers to whole seconds, all such timers will fire
351 * at the same time, rather than at various times spread out. The goal
352 * of this is to have the CPU wake up less, which saves power.
353 *
354 * The exact rounding is skewed for each processor to avoid all
355 * processors firing at the exact same time, which could lead
356 * to lock contention or spurious cache line bouncing.
357 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800358 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800359 */
360unsigned long __round_jiffies_relative(unsigned long j, int cpu)
361{
Alan Stern9c133c42008-11-06 08:42:48 +0100362 unsigned long j0 = jiffies;
363
364 /* Use j0 because jiffies might change while we run */
365 return round_jiffies_common(j + j0, cpu, false) - j0;
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800366}
367EXPORT_SYMBOL_GPL(__round_jiffies_relative);
368
369/**
370 * round_jiffies - function to round jiffies to a full second
371 * @j: the time in (absolute) jiffies that should be rounded
372 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800373 * round_jiffies() rounds an absolute time in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800374 * up or down to (approximately) full seconds. This is useful for timers
375 * for which the exact time they fire does not matter too much, as long as
376 * they fire approximately every X seconds.
377 *
378 * By rounding these timers to whole seconds, all such timers will fire
379 * at the same time, rather than at various times spread out. The goal
380 * of this is to have the CPU wake up less, which saves power.
381 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800382 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800383 */
384unsigned long round_jiffies(unsigned long j)
385{
Alan Stern9c133c42008-11-06 08:42:48 +0100386 return round_jiffies_common(j, raw_smp_processor_id(), false);
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800387}
388EXPORT_SYMBOL_GPL(round_jiffies);
389
390/**
391 * round_jiffies_relative - function to round jiffies to a full second
392 * @j: the time in (relative) jiffies that should be rounded
393 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800394 * round_jiffies_relative() rounds a time delta in the future (in jiffies)
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800395 * up or down to (approximately) full seconds. This is useful for timers
396 * for which the exact time they fire does not matter too much, as long as
397 * they fire approximately every X seconds.
398 *
399 * By rounding these timers to whole seconds, all such timers will fire
400 * at the same time, rather than at various times spread out. The goal
401 * of this is to have the CPU wake up less, which saves power.
402 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800403 * The return value is the rounded version of the @j parameter.
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800404 */
405unsigned long round_jiffies_relative(unsigned long j)
406{
407 return __round_jiffies_relative(j, raw_smp_processor_id());
408}
409EXPORT_SYMBOL_GPL(round_jiffies_relative);
410
Alan Stern9c133c42008-11-06 08:42:48 +0100411/**
412 * __round_jiffies_up - function to round jiffies up to a full second
413 * @j: the time in (absolute) jiffies that should be rounded
414 * @cpu: the processor number on which the timeout will happen
415 *
416 * This is the same as __round_jiffies() except that it will never
417 * round down. This is useful for timeouts for which the exact time
418 * of firing does not matter too much, as long as they don't fire too
419 * early.
420 */
421unsigned long __round_jiffies_up(unsigned long j, int cpu)
422{
423 return round_jiffies_common(j, cpu, true);
424}
425EXPORT_SYMBOL_GPL(__round_jiffies_up);
426
427/**
428 * __round_jiffies_up_relative - function to round jiffies up to a full second
429 * @j: the time in (relative) jiffies that should be rounded
430 * @cpu: the processor number on which the timeout will happen
431 *
432 * This is the same as __round_jiffies_relative() except that it will never
433 * round down. This is useful for timeouts for which the exact time
434 * of firing does not matter too much, as long as they don't fire too
435 * early.
436 */
437unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
438{
439 unsigned long j0 = jiffies;
440
441 /* Use j0 because jiffies might change while we run */
442 return round_jiffies_common(j + j0, cpu, true) - j0;
443}
444EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
445
446/**
447 * round_jiffies_up - function to round jiffies up to a full second
448 * @j: the time in (absolute) jiffies that should be rounded
449 *
450 * This is the same as round_jiffies() except that it will never
451 * round down. This is useful for timeouts for which the exact time
452 * of firing does not matter too much, as long as they don't fire too
453 * early.
454 */
455unsigned long round_jiffies_up(unsigned long j)
456{
457 return round_jiffies_common(j, raw_smp_processor_id(), true);
458}
459EXPORT_SYMBOL_GPL(round_jiffies_up);
460
461/**
462 * round_jiffies_up_relative - function to round jiffies up to a full second
463 * @j: the time in (relative) jiffies that should be rounded
464 *
465 * This is the same as round_jiffies_relative() except that it will never
466 * round down. This is useful for timeouts for which the exact time
467 * of firing does not matter too much, as long as they don't fire too
468 * early.
469 */
470unsigned long round_jiffies_up_relative(unsigned long j)
471{
472 return __round_jiffies_up_relative(j, raw_smp_processor_id());
473}
474EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
475
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800476
Thomas Gleixner500462a2016-07-04 09:50:30 +0000477static inline unsigned int timer_get_idx(struct timer_list *timer)
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700478{
Thomas Gleixner500462a2016-07-04 09:50:30 +0000479 return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT;
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700480}
Thomas Gleixner500462a2016-07-04 09:50:30 +0000481
482static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
483{
484 timer->flags = (timer->flags & ~TIMER_ARRAYMASK) |
485 idx << TIMER_ARRAYSHIFT;
486}
487
488/*
489 * Helper function to calculate the array index for a given expiry
490 * time.
491 */
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200492static inline unsigned calc_index(unsigned long expires, unsigned lvl,
493 unsigned long *bucket_expiry)
Thomas Gleixner500462a2016-07-04 09:50:30 +0000494{
Frederic Weisbecker44688972020-07-17 16:05:44 +0200495
496 /*
497 * The timer wheel has to guarantee that a timer does not fire
498 * early. Early expiry can happen due to:
499 * - Timer is armed at the edge of a tick
500 * - Truncation of the expiry time in the outer wheel levels
501 *
502 * Round up with level granularity to prevent this.
503 */
Thomas Gleixner500462a2016-07-04 09:50:30 +0000504 expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200505 *bucket_expiry = expires << LVL_SHIFT(lvl);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000506 return LVL_OFFS(lvl) + (expires & LVL_MASK);
507}
508
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200509static int calc_wheel_index(unsigned long expires, unsigned long clk,
510 unsigned long *bucket_expiry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Anna-Maria Gleixnerffdf0472016-07-04 09:50:39 +0000512 unsigned long delta = expires - clk;
Thomas Gleixner500462a2016-07-04 09:50:30 +0000513 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Thomas Gleixner500462a2016-07-04 09:50:30 +0000515 if (delta < LVL_START(1)) {
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200516 idx = calc_index(expires, 0, bucket_expiry);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000517 } else if (delta < LVL_START(2)) {
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200518 idx = calc_index(expires, 1, bucket_expiry);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000519 } else if (delta < LVL_START(3)) {
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200520 idx = calc_index(expires, 2, bucket_expiry);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000521 } else if (delta < LVL_START(4)) {
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200522 idx = calc_index(expires, 3, bucket_expiry);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000523 } else if (delta < LVL_START(5)) {
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200524 idx = calc_index(expires, 4, bucket_expiry);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000525 } else if (delta < LVL_START(6)) {
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200526 idx = calc_index(expires, 5, bucket_expiry);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000527 } else if (delta < LVL_START(7)) {
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200528 idx = calc_index(expires, 6, bucket_expiry);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000529 } else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200530 idx = calc_index(expires, 7, bucket_expiry);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000531 } else if ((long) delta < 0) {
Anna-Maria Gleixnerffdf0472016-07-04 09:50:39 +0000532 idx = clk & LVL_MASK;
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200533 *bucket_expiry = clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else {
Thomas Gleixner500462a2016-07-04 09:50:30 +0000535 /*
536 * Force expire obscene large timeouts to expire at the
537 * capacity limit of the wheel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Frederic Weisbeckere2a71bd2020-07-17 16:05:40 +0200539 if (delta >= WHEEL_TIMEOUT_CUTOFF)
540 expires = clk + WHEEL_TIMEOUT_MAX;
Thomas Gleixner1bd04bf2015-05-26 22:50:26 +0000541
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200542 idx = calc_index(expires, LVL_DEPTH - 1, bucket_expiry);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000543 }
Anna-Maria Gleixnerffdf0472016-07-04 09:50:39 +0000544 return idx;
545}
546
Anna-Maria Gleixnerffdf0472016-07-04 09:50:39 +0000547static void
548trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000549{
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100550 if (!is_timers_nohz_active())
Thomas Gleixnera683f392016-07-04 09:50:36 +0000551 return;
Viresh Kumar9f6d9ba2014-06-22 01:29:14 +0200552
553 /*
Thomas Gleixnera683f392016-07-04 09:50:36 +0000554 * TODO: This wants some optimizing similar to the code below, but we
555 * will do that when we switch from push to pull for deferrable timers.
Viresh Kumar9f6d9ba2014-06-22 01:29:14 +0200556 */
Thomas Gleixnera683f392016-07-04 09:50:36 +0000557 if (timer->flags & TIMER_DEFERRABLE) {
558 if (tick_nohz_full_cpu(base->cpu))
Thomas Gleixner683be132015-05-26 22:50:35 +0000559 wake_up_nohz_cpu(base->cpu);
Thomas Gleixnera683f392016-07-04 09:50:36 +0000560 return;
Thomas Gleixner683be132015-05-26 22:50:35 +0000561 }
Thomas Gleixnera683f392016-07-04 09:50:36 +0000562
563 /*
564 * We might have to IPI the remote CPU if the base is idle and the
565 * timer is not deferrable. If the other CPU is on the way to idle
566 * then it can't set base->is_idle as we hold the base lock:
567 */
Frederic Weisbeckerdc2a0f12020-07-17 16:05:46 +0200568 if (base->is_idle)
569 wake_up_nohz_cpu(base->cpu);
Anna-Maria Gleixnerffdf0472016-07-04 09:50:39 +0000570}
571
Frederic Weisbecker9a2b7642020-07-17 16:05:43 +0200572/*
573 * Enqueue the timer into the hash bucket, mark it pending in
574 * the bitmap, store the index in the timer flags then wake up
575 * the target CPU if needed.
576 */
577static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
578 unsigned int idx, unsigned long bucket_expiry)
Anna-Maria Gleixnerffdf0472016-07-04 09:50:39 +0000579{
Frederic Weisbeckerdc2a0f12020-07-17 16:05:46 +0200580
Frederic Weisbecker9a2b7642020-07-17 16:05:43 +0200581 hlist_add_head(&timer->entry, base->vectors + idx);
582 __set_bit(idx, base->pending_map);
583 timer_set_idx(timer, idx);
584
585 trace_timer_start(timer, timer->expires, timer->flags);
Frederic Weisbeckerdc2a0f12020-07-17 16:05:46 +0200586
587 /*
588 * Check whether this is the new first expiring timer. The
589 * effective expiry time of the timer is required here
590 * (bucket_expiry) instead of timer->expires.
591 */
592 if (time_before(bucket_expiry, base->next_expiry)) {
593 /*
594 * Set the next expiry time and kick the CPU so it
595 * can reevaluate the wheel:
596 */
597 base->next_expiry = bucket_expiry;
Frederic Weisbecker31cd0e12020-07-23 17:16:41 +0200598 base->next_expiry_recalc = false;
Frederic Weisbeckerdc2a0f12020-07-17 16:05:46 +0200599 trigger_dyntick_cpu(base, timer);
600 }
Frederic Weisbecker9a2b7642020-07-17 16:05:43 +0200601}
602
603static void internal_add_timer(struct timer_base *base, struct timer_list *timer)
Anna-Maria Gleixnerffdf0472016-07-04 09:50:39 +0000604{
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200605 unsigned long bucket_expiry;
Frederic Weisbecker9a2b7642020-07-17 16:05:43 +0200606 unsigned int idx;
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200607
Frederic Weisbecker9a2b7642020-07-17 16:05:43 +0200608 idx = calc_wheel_index(timer->expires, base->clk, &bucket_expiry);
609 enqueue_timer(base, timer, idx, bucket_expiry);
Thomas Gleixnerfacbb4a2012-05-25 22:08:57 +0000610}
611
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700612#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
613
Stephen Boydf9e62f32020-08-14 17:40:27 -0700614static const struct debug_obj_descr timer_debug_descr;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700615
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100616static void *timer_debug_hint(void *addr)
617{
618 return ((struct timer_list *) addr)->function;
619}
620
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700621static bool timer_is_static_object(void *addr)
622{
623 struct timer_list *timer = addr;
624
625 return (timer->entry.pprev == NULL &&
626 timer->entry.next == TIMER_ENTRY_STATIC);
627}
628
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700629/*
630 * fixup_init is called when:
631 * - an active object is initialized
632 */
Du, Changbine3252462016-05-19 17:09:29 -0700633static bool timer_fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700634{
635 struct timer_list *timer = addr;
636
637 switch (state) {
638 case ODEBUG_STATE_ACTIVE:
639 del_timer_sync(timer);
640 debug_object_init(timer, &timer_debug_descr);
Du, Changbine3252462016-05-19 17:09:29 -0700641 return true;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700642 default:
Du, Changbine3252462016-05-19 17:09:29 -0700643 return false;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700644 }
645}
646
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800647/* Stub timer callback for improperly used timers. */
Thomas Gleixnerba164902017-10-18 16:10:19 +0200648static void stub_timer(struct timer_list *unused)
Stephen Boydfb16b8c2011-11-07 19:48:26 -0800649{
650 WARN_ON(1);
651}
652
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700653/*
654 * fixup_activate is called when:
655 * - an active object is activated
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700656 * - an unknown non-static object is activated
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700657 */
Du, Changbine3252462016-05-19 17:09:29 -0700658static bool timer_fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700659{
660 struct timer_list *timer = addr;
661
662 switch (state) {
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700663 case ODEBUG_STATE_NOTAVAILABLE:
Thomas Gleixnerba164902017-10-18 16:10:19 +0200664 timer_setup(timer, stub_timer, 0);
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700665 return true;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700666
667 case ODEBUG_STATE_ACTIVE:
668 WARN_ON(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500669 fallthrough;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700670 default:
Du, Changbine3252462016-05-19 17:09:29 -0700671 return false;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700672 }
673}
674
675/*
676 * fixup_free is called when:
677 * - an active object is freed
678 */
Du, Changbine3252462016-05-19 17:09:29 -0700679static bool timer_fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700680{
681 struct timer_list *timer = addr;
682
683 switch (state) {
684 case ODEBUG_STATE_ACTIVE:
685 del_timer_sync(timer);
686 debug_object_free(timer, &timer_debug_descr);
Du, Changbine3252462016-05-19 17:09:29 -0700687 return true;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700688 default:
Du, Changbine3252462016-05-19 17:09:29 -0700689 return false;
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700690 }
691}
692
Christine Chandc4218b2011-11-07 19:48:28 -0800693/*
694 * fixup_assert_init is called when:
695 * - an untracked/uninit-ed object is found
696 */
Du, Changbine3252462016-05-19 17:09:29 -0700697static bool timer_fixup_assert_init(void *addr, enum debug_obj_state state)
Christine Chandc4218b2011-11-07 19:48:28 -0800698{
699 struct timer_list *timer = addr;
700
701 switch (state) {
702 case ODEBUG_STATE_NOTAVAILABLE:
Thomas Gleixnerba164902017-10-18 16:10:19 +0200703 timer_setup(timer, stub_timer, 0);
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700704 return true;
Christine Chandc4218b2011-11-07 19:48:28 -0800705 default:
Du, Changbine3252462016-05-19 17:09:29 -0700706 return false;
Christine Chandc4218b2011-11-07 19:48:28 -0800707 }
708}
709
Stephen Boydf9e62f32020-08-14 17:40:27 -0700710static const struct debug_obj_descr timer_debug_descr = {
Christine Chandc4218b2011-11-07 19:48:28 -0800711 .name = "timer_list",
712 .debug_hint = timer_debug_hint,
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700713 .is_static_object = timer_is_static_object,
Christine Chandc4218b2011-11-07 19:48:28 -0800714 .fixup_init = timer_fixup_init,
715 .fixup_activate = timer_fixup_activate,
716 .fixup_free = timer_fixup_free,
717 .fixup_assert_init = timer_fixup_assert_init,
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700718};
719
720static inline void debug_timer_init(struct timer_list *timer)
721{
722 debug_object_init(timer, &timer_debug_descr);
723}
724
725static inline void debug_timer_activate(struct timer_list *timer)
726{
727 debug_object_activate(timer, &timer_debug_descr);
728}
729
730static inline void debug_timer_deactivate(struct timer_list *timer)
731{
732 debug_object_deactivate(timer, &timer_debug_descr);
733}
734
Christine Chandc4218b2011-11-07 19:48:28 -0800735static inline void debug_timer_assert_init(struct timer_list *timer)
736{
737 debug_object_assert_init(timer, &timer_debug_descr);
738}
739
Kees Cook188665b2017-10-22 18:14:46 -0700740static void do_init_timer(struct timer_list *timer,
741 void (*func)(struct timer_list *),
742 unsigned int flags,
Tejun Heofc683992012-08-08 11:10:27 -0700743 const char *name, struct lock_class_key *key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700744
Kees Cook188665b2017-10-22 18:14:46 -0700745void init_timer_on_stack_key(struct timer_list *timer,
746 void (*func)(struct timer_list *),
747 unsigned int flags,
Tejun Heofc683992012-08-08 11:10:27 -0700748 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700749{
750 debug_object_init_on_stack(timer, &timer_debug_descr);
Kees Cook188665b2017-10-22 18:14:46 -0700751 do_init_timer(timer, func, flags, name, key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700752}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100753EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700754
755void destroy_timer_on_stack(struct timer_list *timer)
756{
757 debug_object_free(timer, &timer_debug_descr);
758}
759EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
760
761#else
762static inline void debug_timer_init(struct timer_list *timer) { }
763static inline void debug_timer_activate(struct timer_list *timer) { }
764static inline void debug_timer_deactivate(struct timer_list *timer) { }
Christine Chandc4218b2011-11-07 19:48:28 -0800765static inline void debug_timer_assert_init(struct timer_list *timer) { }
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700766#endif
767
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800768static inline void debug_init(struct timer_list *timer)
769{
770 debug_timer_init(timer);
771 trace_timer_init(timer);
772}
773
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800774static inline void debug_deactivate(struct timer_list *timer)
775{
776 debug_timer_deactivate(timer);
777 trace_timer_cancel(timer);
778}
779
Christine Chandc4218b2011-11-07 19:48:28 -0800780static inline void debug_assert_init(struct timer_list *timer)
781{
782 debug_timer_assert_init(timer);
783}
784
Kees Cook188665b2017-10-22 18:14:46 -0700785static void do_init_timer(struct timer_list *timer,
786 void (*func)(struct timer_list *),
787 unsigned int flags,
Tejun Heofc683992012-08-08 11:10:27 -0700788 const char *name, struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700789{
Thomas Gleixner1dabbce2015-05-26 22:50:28 +0000790 timer->entry.pprev = NULL;
Kees Cook188665b2017-10-22 18:14:46 -0700791 timer->function = func;
Qianli Zhaob952caf2020-08-13 23:03:14 +0800792 if (WARN_ON_ONCE(flags & ~TIMER_INIT_FLAGS))
793 flags &= TIMER_INIT_FLAGS;
Thomas Gleixner0eeda712015-05-26 22:50:29 +0000794 timer->flags = flags | raw_smp_processor_id();
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100795 lockdep_init_map(&timer->lockdep_map, name, key, 0);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700796}
797
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -0700798/**
Randy Dunlap633fe792009-04-01 17:47:23 -0700799 * init_timer_key - initialize a timer
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700800 * @timer: the timer to be initialized
Kees Cook188665b2017-10-22 18:14:46 -0700801 * @func: timer callback function
Tejun Heofc683992012-08-08 11:10:27 -0700802 * @flags: timer flags
Randy Dunlap633fe792009-04-01 17:47:23 -0700803 * @name: name of the timer
804 * @key: lockdep class key of the fake lock used for tracking timer
805 * sync lock dependencies
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700806 *
Randy Dunlap633fe792009-04-01 17:47:23 -0700807 * init_timer_key() must be done to a timer prior calling *any* of the
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700808 * other timer functions.
809 */
Kees Cook188665b2017-10-22 18:14:46 -0700810void init_timer_key(struct timer_list *timer,
811 void (*func)(struct timer_list *), unsigned int flags,
Tejun Heofc683992012-08-08 11:10:27 -0700812 const char *name, struct lock_class_key *key)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700813{
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800814 debug_init(timer);
Kees Cook188665b2017-10-22 18:14:46 -0700815 do_init_timer(timer, func, flags, name, key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700816}
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100817EXPORT_SYMBOL(init_timer_key);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700818
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000819static inline void detach_timer(struct timer_list *timer, bool clear_pending)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700820{
Thomas Gleixner1dabbce2015-05-26 22:50:28 +0000821 struct hlist_node *entry = &timer->entry;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700822
Xiao Guangrong2b022e32009-08-10 10:48:59 +0800823 debug_deactivate(timer);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700824
Thomas Gleixner1dabbce2015-05-26 22:50:28 +0000825 __hlist_del(entry);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700826 if (clear_pending)
Thomas Gleixner1dabbce2015-05-26 22:50:28 +0000827 entry->pprev = NULL;
828 entry->next = LIST_POISON2;
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700829}
830
Thomas Gleixner494af3e2016-07-04 09:50:28 +0000831static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000832 bool clear_pending)
833{
Thomas Gleixner500462a2016-07-04 09:50:30 +0000834 unsigned idx = timer_get_idx(timer);
835
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000836 if (!timer_pending(timer))
837 return 0;
838
Frederic Weisbecker31cd0e12020-07-23 17:16:41 +0200839 if (hlist_is_singular_node(&timer->entry, base->vectors + idx)) {
Thomas Gleixner500462a2016-07-04 09:50:30 +0000840 __clear_bit(idx, base->pending_map);
Frederic Weisbecker31cd0e12020-07-23 17:16:41 +0200841 base->next_expiry_recalc = true;
842 }
Thomas Gleixner500462a2016-07-04 09:50:30 +0000843
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000844 detach_timer(timer, clear_pending);
Thomas Gleixnerec44bc72012-05-25 22:08:57 +0000845 return 1;
846}
847
Thomas Gleixner500462a2016-07-04 09:50:30 +0000848static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
849{
850 struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu);
851
852 /*
Anna-Maria Gleixnerced6d5c2017-12-22 15:51:12 +0100853 * If the timer is deferrable and NO_HZ_COMMON is set then we need
854 * to use the deferrable base.
Thomas Gleixner500462a2016-07-04 09:50:30 +0000855 */
Anna-Maria Gleixnerced6d5c2017-12-22 15:51:12 +0100856 if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
Thomas Gleixner500462a2016-07-04 09:50:30 +0000857 base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
858 return base;
859}
860
861static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
862{
863 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
864
865 /*
Anna-Maria Gleixnerced6d5c2017-12-22 15:51:12 +0100866 * If the timer is deferrable and NO_HZ_COMMON is set then we need
867 * to use the deferrable base.
Thomas Gleixner500462a2016-07-04 09:50:30 +0000868 */
Anna-Maria Gleixnerced6d5c2017-12-22 15:51:12 +0100869 if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
Thomas Gleixner500462a2016-07-04 09:50:30 +0000870 base = this_cpu_ptr(&timer_bases[BASE_DEF]);
871 return base;
872}
873
874static inline struct timer_base *get_timer_base(u32 tflags)
875{
876 return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
877}
878
Thomas Gleixnera683f392016-07-04 09:50:36 +0000879static inline struct timer_base *
Thomas Gleixner6bad6bc2016-10-22 11:07:37 +0000880get_target_base(struct timer_base *base, unsigned tflags)
Thomas Gleixner500462a2016-07-04 09:50:30 +0000881{
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100882#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
883 if (static_branch_likely(&timers_migration_enabled) &&
884 !(tflags & TIMER_PINNED))
885 return get_timer_cpu_base(tflags, get_nohz_timer_target());
Thomas Gleixner500462a2016-07-04 09:50:30 +0000886#endif
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100887 return get_timer_this_cpu_base(tflags);
Thomas Gleixner500462a2016-07-04 09:50:30 +0000888}
889
Thomas Gleixnera683f392016-07-04 09:50:36 +0000890static inline void forward_timer_base(struct timer_base *base)
891{
Frederic Weisbecker0975fb52020-07-17 16:05:50 +0200892 unsigned long jnow = READ_ONCE(jiffies);
Thomas Gleixner6bad6bc2016-10-22 11:07:37 +0000893
Thomas Gleixnera683f392016-07-04 09:50:36 +0000894 /*
Frederic Weisbecker0975fb52020-07-17 16:05:50 +0200895 * No need to forward if we are close enough below jiffies.
896 * Also while executing timers, base->clk is 1 offset ahead
Ingo Molnar4bf07f62021-03-22 22:39:03 +0100897 * of jiffies to avoid endless requeuing to current jiffies.
Thomas Gleixnera683f392016-07-04 09:50:36 +0000898 */
Frederic Weisbecker36cd28a2020-07-17 16:05:51 +0200899 if ((long)(jnow - base->clk) < 1)
Thomas Gleixnera683f392016-07-04 09:50:36 +0000900 return;
901
902 /*
903 * If the next expiry value is > jiffies, then we fast forward to
904 * jiffies otherwise we forward to the next expiry value.
905 */
Frederic Weisbecker30c66fc2020-07-03 03:06:57 +0200906 if (time_after(base->next_expiry, jnow)) {
Thomas Gleixner6bad6bc2016-10-22 11:07:37 +0000907 base->clk = jnow;
Frederic Weisbecker30c66fc2020-07-03 03:06:57 +0200908 } else {
909 if (WARN_ON_ONCE(time_before(base->next_expiry, base->clk)))
910 return;
Thomas Gleixnera683f392016-07-04 09:50:36 +0000911 base->clk = base->next_expiry;
Frederic Weisbecker30c66fc2020-07-03 03:06:57 +0200912 }
Thomas Gleixnerae67bad2018-01-14 23:30:51 +0100913}
Thomas Gleixnera683f392016-07-04 09:50:36 +0000914
Thomas Gleixnera683f392016-07-04 09:50:36 +0000915
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700916/*
Thomas Gleixner500462a2016-07-04 09:50:30 +0000917 * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
918 * that all timers which are tied to this base are locked, and the base itself
919 * is locked too.
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700920 *
921 * So __run_timers/migrate_timers can safely modify all timers which could
Thomas Gleixner500462a2016-07-04 09:50:30 +0000922 * be found in the base->vectors array.
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700923 *
Thomas Gleixner500462a2016-07-04 09:50:30 +0000924 * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
925 * to wait until the migration is done.
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700926 */
Thomas Gleixner494af3e2016-07-04 09:50:28 +0000927static struct timer_base *lock_timer_base(struct timer_list *timer,
Thomas Gleixner500462a2016-07-04 09:50:30 +0000928 unsigned long *flags)
Josh Triplett89e7e3742006-09-29 01:59:36 -0700929 __acquires(timer->base->lock)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700930{
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700931 for (;;) {
Thomas Gleixner494af3e2016-07-04 09:50:28 +0000932 struct timer_base *base;
Thomas Gleixnerb8312752016-10-24 11:41:56 +0200933 u32 tf;
934
935 /*
936 * We need to use READ_ONCE() here, otherwise the compiler
937 * might re-read @tf between the check for TIMER_MIGRATING
938 * and spin_lock().
939 */
940 tf = READ_ONCE(timer->flags);
Thomas Gleixner0eeda712015-05-26 22:50:29 +0000941
942 if (!(tf & TIMER_MIGRATING)) {
Thomas Gleixner500462a2016-07-04 09:50:30 +0000943 base = get_timer_base(tf);
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +0200944 raw_spin_lock_irqsave(&base->lock, *flags);
Thomas Gleixner0eeda712015-05-26 22:50:29 +0000945 if (timer->flags == tf)
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700946 return base;
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +0200947 raw_spin_unlock_irqrestore(&base->lock, *flags);
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700948 }
949 cpu_relax();
950 }
951}
952
David Howellsb24591e2017-11-09 12:35:07 +0000953#define MOD_TIMER_PENDING_ONLY 0x01
954#define MOD_TIMER_REDUCE 0x02
Eric Dumazet90c01892019-11-07 11:37:38 -0800955#define MOD_TIMER_NOTPENDING 0x04
David Howellsb24591e2017-11-09 12:35:07 +0000956
Ingo Molnar74019222009-02-18 12:23:29 +0100957static inline int
David Howellsb24591e2017-11-09 12:35:07 +0000958__mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +0200960 unsigned long clk = 0, flags, bucket_expiry;
Thomas Gleixner494af3e2016-07-04 09:50:28 +0000961 struct timer_base *base, *new_base;
Anna-Maria Gleixnerf00c0af2016-07-04 09:50:40 +0000962 unsigned int idx = UINT_MAX;
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000963 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Thomas Gleixner4da91522016-10-24 11:55:10 +0200965 BUG_ON(!timer->function);
966
Thomas Gleixner500462a2016-07-04 09:50:30 +0000967 /*
Anna-Maria Gleixnerf00c0af2016-07-04 09:50:40 +0000968 * This is a common optimization triggered by the networking code - if
969 * the timer is re-modified to have the same timeout or ends up in the
970 * same array bucket then just return:
Thomas Gleixner500462a2016-07-04 09:50:30 +0000971 */
Eric Dumazet90c01892019-11-07 11:37:38 -0800972 if (!(options & MOD_TIMER_NOTPENDING) && timer_pending(timer)) {
Nicholas Piggin2fe59f52017-08-22 18:43:48 +1000973 /*
974 * The downside of this optimization is that it can result in
975 * larger granularity than you would get from adding a new
976 * timer with this expiry.
977 */
David Howellsb24591e2017-11-09 12:35:07 +0000978 long diff = timer->expires - expires;
979
980 if (!diff)
981 return 1;
982 if (options & MOD_TIMER_REDUCE && diff <= 0)
Thomas Gleixner500462a2016-07-04 09:50:30 +0000983 return 1;
Anna-Maria Gleixnerf00c0af2016-07-04 09:50:40 +0000984
Thomas Gleixner4da91522016-10-24 11:55:10 +0200985 /*
986 * We lock timer base and calculate the bucket index right
987 * here. If the timer ends up in the same bucket, then we
988 * just update the expiry time and avoid the whole
989 * dequeue/enqueue dance.
990 */
991 base = lock_timer_base(timer, &flags);
Nicholas Piggin2fe59f52017-08-22 18:43:48 +1000992 forward_timer_base(base);
Thomas Gleixner4da91522016-10-24 11:55:10 +0200993
David Howellsb24591e2017-11-09 12:35:07 +0000994 if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) &&
995 time_before_eq(timer->expires, expires)) {
996 ret = 1;
997 goto out_unlock;
998 }
999
Thomas Gleixner4da91522016-10-24 11:55:10 +02001000 clk = base->clk;
Anna-Maria Behnsen1f32cab2020-07-17 16:05:42 +02001001 idx = calc_wheel_index(expires, clk, &bucket_expiry);
Anna-Maria Gleixnerf00c0af2016-07-04 09:50:40 +00001002
1003 /*
1004 * Retrieve and compare the array index of the pending
1005 * timer. If it matches set the expiry to the new value so a
1006 * subsequent call will exit in the expires check above.
1007 */
1008 if (idx == timer_get_idx(timer)) {
David Howellsb24591e2017-11-09 12:35:07 +00001009 if (!(options & MOD_TIMER_REDUCE))
1010 timer->expires = expires;
1011 else if (time_after(timer->expires, expires))
1012 timer->expires = expires;
Thomas Gleixner4da91522016-10-24 11:55:10 +02001013 ret = 1;
1014 goto out_unlock;
Anna-Maria Gleixnerf00c0af2016-07-04 09:50:40 +00001015 }
Thomas Gleixner4da91522016-10-24 11:55:10 +02001016 } else {
1017 base = lock_timer_base(timer, &flags);
Nicholas Piggin2fe59f52017-08-22 18:43:48 +10001018 forward_timer_base(base);
Thomas Gleixner500462a2016-07-04 09:50:30 +00001019 }
1020
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001021 ret = detach_if_pending(timer, base, false);
David Howellsb24591e2017-11-09 12:35:07 +00001022 if (!ret && (options & MOD_TIMER_PENDING_ONLY))
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001023 goto out_unlock;
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001024
Thomas Gleixner500462a2016-07-04 09:50:30 +00001025 new_base = get_target_base(base, timer->flags);
Arun R Bharadwajeea08f32009-04-16 12:16:41 +05301026
Oleg Nesterov3691c512006-03-31 02:30:30 -08001027 if (base != new_base) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001028 /*
Thomas Gleixner500462a2016-07-04 09:50:30 +00001029 * We are trying to schedule the timer on the new base.
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001030 * However we can't change timer's base while it is running,
1031 * otherwise del_timer_sync() can't detect that the timer's
Thomas Gleixner500462a2016-07-04 09:50:30 +00001032 * handler yet has not finished. This also guarantees that the
1033 * timer is serialized wrt itself.
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001034 */
Oleg Nesterova2c348f2006-03-31 02:30:31 -08001035 if (likely(base->running_timer != timer)) {
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001036 /* See the comment in lock_timer_base() */
Thomas Gleixner0eeda712015-05-26 22:50:29 +00001037 timer->flags |= TIMER_MIGRATING;
1038
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001039 raw_spin_unlock(&base->lock);
Oleg Nesterova2c348f2006-03-31 02:30:31 -08001040 base = new_base;
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001041 raw_spin_lock(&base->lock);
Eric Dumazetd0023a12015-08-17 10:18:48 -07001042 WRITE_ONCE(timer->flags,
1043 (timer->flags & ~TIMER_BASEMASK) | base->cpu);
Nicholas Piggin2fe59f52017-08-22 18:43:48 +10001044 forward_timer_base(base);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001045 }
1046 }
1047
Anna-Maria Gleixnerdc1e7dc2019-03-21 13:09:19 +01001048 debug_timer_activate(timer);
Thomas Gleixnerfd45bb72017-12-22 15:51:14 +01001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 timer->expires = expires;
Anna-Maria Gleixnerf00c0af2016-07-04 09:50:40 +00001051 /*
1052 * If 'idx' was calculated above and the base time did not advance
Thomas Gleixner4da91522016-10-24 11:55:10 +02001053 * between calculating 'idx' and possibly switching the base, only
Frederic Weisbecker9a2b7642020-07-17 16:05:43 +02001054 * enqueue_timer() is required. Otherwise we need to (re)calculate
1055 * the wheel index via internal_add_timer().
Anna-Maria Gleixnerf00c0af2016-07-04 09:50:40 +00001056 */
Frederic Weisbecker9a2b7642020-07-17 16:05:43 +02001057 if (idx != UINT_MAX && clk == base->clk)
1058 enqueue_timer(base, timer, idx, bucket_expiry);
1059 else
Anna-Maria Gleixnerf00c0af2016-07-04 09:50:40 +00001060 internal_add_timer(base, timer);
Ingo Molnar74019222009-02-18 12:23:29 +01001061
1062out_unlock:
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001063 raw_spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 return ret;
1066}
1067
Ingo Molnar74019222009-02-18 12:23:29 +01001068/**
1069 * mod_timer_pending - modify a pending timer's timeout
1070 * @timer: the pending timer to be modified
1071 * @expires: new timeout in jiffies
1072 *
1073 * mod_timer_pending() is the same for pending timers as mod_timer(),
1074 * but will not re-activate and modify already deleted timers.
1075 *
1076 * It is useful for unserialized use of timers.
1077 */
1078int mod_timer_pending(struct timer_list *timer, unsigned long expires)
1079{
David Howellsb24591e2017-11-09 12:35:07 +00001080 return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY);
Ingo Molnar74019222009-02-18 12:23:29 +01001081}
1082EXPORT_SYMBOL(mod_timer_pending);
1083
1084/**
1085 * mod_timer - modify a timer's timeout
1086 * @timer: the timer to be modified
1087 * @expires: new timeout in jiffies
1088 *
1089 * mod_timer() is a more efficient way to update the expire field of an
1090 * active timer (if the timer is inactive it will be activated)
1091 *
1092 * mod_timer(timer, expires) is equivalent to:
1093 *
1094 * del_timer(timer); timer->expires = expires; add_timer(timer);
1095 *
1096 * Note that if there are multiple unserialized concurrent users of the
1097 * same timer, then mod_timer() is the only safe way to modify the timeout,
1098 * since add_timer() cannot modify an already running timer.
1099 *
1100 * The function returns whether it has modified a pending timer or not.
1101 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
1102 * active timer returns 1.)
1103 */
1104int mod_timer(struct timer_list *timer, unsigned long expires)
1105{
David Howellsb24591e2017-11-09 12:35:07 +00001106 return __mod_timer(timer, expires, 0);
Ingo Molnar74019222009-02-18 12:23:29 +01001107}
1108EXPORT_SYMBOL(mod_timer);
1109
1110/**
David Howellsb24591e2017-11-09 12:35:07 +00001111 * timer_reduce - Modify a timer's timeout if it would reduce the timeout
1112 * @timer: The timer to be modified
1113 * @expires: New timeout in jiffies
1114 *
1115 * timer_reduce() is very similar to mod_timer(), except that it will only
1116 * modify a running timer if that would reduce the expiration time (it will
1117 * start a timer that isn't running).
1118 */
1119int timer_reduce(struct timer_list *timer, unsigned long expires)
1120{
1121 return __mod_timer(timer, expires, MOD_TIMER_REDUCE);
1122}
1123EXPORT_SYMBOL(timer_reduce);
1124
1125/**
Ingo Molnar74019222009-02-18 12:23:29 +01001126 * add_timer - start a timer
1127 * @timer: the timer to be added
1128 *
Kees Cookc1eba5b2017-10-22 18:18:19 -07001129 * The kernel will do a ->function(@timer) callback from the
Ingo Molnar74019222009-02-18 12:23:29 +01001130 * timer interrupt at the ->expires point in the future. The
1131 * current time is 'jiffies'.
1132 *
Kees Cookc1eba5b2017-10-22 18:18:19 -07001133 * The timer's ->expires, ->function fields must be set prior calling this
1134 * function.
Ingo Molnar74019222009-02-18 12:23:29 +01001135 *
1136 * Timers with an ->expires field in the past will be executed in the next
1137 * timer tick.
1138 */
1139void add_timer(struct timer_list *timer)
1140{
1141 BUG_ON(timer_pending(timer));
Eric Dumazet90c01892019-11-07 11:37:38 -08001142 __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);
Ingo Molnar74019222009-02-18 12:23:29 +01001143}
1144EXPORT_SYMBOL(add_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001146/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 * add_timer_on - start a timer on a particular CPU
1148 * @timer: the timer to be added
1149 * @cpu: the CPU to start it on
1150 *
1151 * This is not very scalable on SMP. Double adds are not possible.
1152 */
1153void add_timer_on(struct timer_list *timer, int cpu)
1154{
Thomas Gleixner500462a2016-07-04 09:50:30 +00001155 struct timer_base *new_base, *base;
Thomas Gleixner68194572007-07-19 01:49:16 -07001156 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001157
Thomas Gleixner68194572007-07-19 01:49:16 -07001158 BUG_ON(timer_pending(timer) || !timer->function);
Tejun Heo22b886d2015-11-04 12:15:33 -05001159
Thomas Gleixner500462a2016-07-04 09:50:30 +00001160 new_base = get_timer_cpu_base(timer->flags, cpu);
1161
Tejun Heo22b886d2015-11-04 12:15:33 -05001162 /*
1163 * If @timer was on a different CPU, it should be migrated with the
1164 * old base locked to prevent other operations proceeding with the
1165 * wrong base locked. See lock_timer_base().
1166 */
1167 base = lock_timer_base(timer, &flags);
1168 if (base != new_base) {
1169 timer->flags |= TIMER_MIGRATING;
1170
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001171 raw_spin_unlock(&base->lock);
Tejun Heo22b886d2015-11-04 12:15:33 -05001172 base = new_base;
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001173 raw_spin_lock(&base->lock);
Tejun Heo22b886d2015-11-04 12:15:33 -05001174 WRITE_ONCE(timer->flags,
1175 (timer->flags & ~TIMER_BASEMASK) | cpu);
1176 }
Nicholas Piggin2fe59f52017-08-22 18:43:48 +10001177 forward_timer_base(base);
Tejun Heo22b886d2015-11-04 12:15:33 -05001178
Anna-Maria Gleixnerdc1e7dc2019-03-21 13:09:19 +01001179 debug_timer_activate(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 internal_add_timer(base, timer);
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001181 raw_spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
Andi Kleena9862e02009-05-19 22:49:07 +02001183EXPORT_SYMBOL_GPL(add_timer_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001185/**
Masanari Iida0ba42a52017-03-07 20:48:02 +09001186 * del_timer - deactivate a timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 * @timer: the timer to be deactivated
1188 *
1189 * del_timer() deactivates a timer - this works on both active and inactive
1190 * timers.
1191 *
1192 * The function returns whether it has deactivated a pending timer or not.
1193 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
1194 * active timer returns 1.)
1195 */
1196int del_timer(struct timer_list *timer)
1197{
Thomas Gleixner494af3e2016-07-04 09:50:28 +00001198 struct timer_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 unsigned long flags;
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001200 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Christine Chandc4218b2011-11-07 19:48:28 -08001202 debug_assert_init(timer);
1203
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001204 if (timer_pending(timer)) {
1205 base = lock_timer_base(timer, &flags);
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001206 ret = detach_if_pending(timer, base, true);
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001207 raw_spin_unlock_irqrestore(&base->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Oleg Nesterov55c888d2005-06-23 00:08:56 -07001210 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212EXPORT_SYMBOL(del_timer);
1213
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001214/**
1215 * try_to_del_timer_sync - Try to deactivate a timer
Peter Meerwald-Stadlerd15bc692017-05-30 21:41:03 +02001216 * @timer: timer to delete
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001217 *
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001218 * This function tries to deactivate a timer. Upon successful (ret >= 0)
1219 * exit the timer is not queued and the handler is not running on any CPU.
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001220 */
1221int try_to_del_timer_sync(struct timer_list *timer)
1222{
Thomas Gleixner494af3e2016-07-04 09:50:28 +00001223 struct timer_base *base;
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001224 unsigned long flags;
1225 int ret = -1;
1226
Christine Chandc4218b2011-11-07 19:48:28 -08001227 debug_assert_init(timer);
1228
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001229 base = lock_timer_base(timer, &flags);
1230
Kees Cookdfb43572017-02-08 11:26:59 -08001231 if (base->running_timer != timer)
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001232 ret = detach_if_pending(timer, base, true);
Kees Cookdfb43572017-02-08 11:26:59 -08001233
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001234 raw_spin_unlock_irqrestore(&base->lock, flags);
Oleg Nesterovfd450b72005-06-23 00:08:59 -07001235
1236 return ret;
1237}
David Howellse19dff12007-04-26 15:46:56 -07001238EXPORT_SYMBOL(try_to_del_timer_sync);
1239
Frederic Weisbeckerdcd42592020-11-13 13:13:33 +01001240bool timer_curr_running(struct timer_list *timer)
1241{
1242 int i;
1243
1244 for (i = 0; i < NR_BASES; i++) {
1245 struct timer_base *base = this_cpu_ptr(&timer_bases[i]);
Paul E. McKenneyf7590812020-12-21 11:17:16 -08001246
Frederic Weisbeckerdcd42592020-11-13 13:13:33 +01001247 if (base->running_timer == timer)
1248 return true;
1249 }
1250
1251 return false;
1252}
1253
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001254#ifdef CONFIG_PREEMPT_RT
1255static __init void timer_base_init_expiry_lock(struct timer_base *base)
1256{
1257 spin_lock_init(&base->expiry_lock);
1258}
1259
1260static inline void timer_base_lock_expiry(struct timer_base *base)
1261{
1262 spin_lock(&base->expiry_lock);
1263}
1264
1265static inline void timer_base_unlock_expiry(struct timer_base *base)
1266{
1267 spin_unlock(&base->expiry_lock);
1268}
1269
1270/*
1271 * The counterpart to del_timer_wait_running().
1272 *
1273 * If there is a waiter for base->expiry_lock, then it was waiting for the
Ingo Molnar4bf07f62021-03-22 22:39:03 +01001274 * timer callback to finish. Drop expiry_lock and reacquire it. That allows
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001275 * the waiter to acquire the lock and make progress.
1276 */
1277static void timer_sync_wait_running(struct timer_base *base)
1278{
1279 if (atomic_read(&base->timer_waiters)) {
1280 spin_unlock(&base->expiry_lock);
1281 spin_lock(&base->expiry_lock);
1282 }
1283}
1284
1285/*
1286 * This function is called on PREEMPT_RT kernels when the fast path
1287 * deletion of a timer failed because the timer callback function was
1288 * running.
1289 *
1290 * This prevents priority inversion, if the softirq thread on a remote CPU
1291 * got preempted, and it prevents a life lock when the task which tries to
1292 * delete a timer preempted the softirq thread running the timer callback
1293 * function.
1294 */
1295static void del_timer_wait_running(struct timer_list *timer)
1296{
1297 u32 tf;
1298
1299 tf = READ_ONCE(timer->flags);
Sebastian Andrzej Siewiorc725daf2020-11-03 20:09:37 +01001300 if (!(tf & (TIMER_MIGRATING | TIMER_IRQSAFE))) {
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001301 struct timer_base *base = get_timer_base(tf);
1302
1303 /*
1304 * Mark the base as contended and grab the expiry lock,
1305 * which is held by the softirq across the timer
1306 * callback. Drop the lock immediately so the softirq can
1307 * expire the next timer. In theory the timer could already
1308 * be running again, but that's more than unlikely and just
1309 * causes another wait loop.
1310 */
1311 atomic_inc(&base->timer_waiters);
1312 spin_lock_bh(&base->expiry_lock);
1313 atomic_dec(&base->timer_waiters);
1314 spin_unlock_bh(&base->expiry_lock);
1315 }
1316}
1317#else
1318static inline void timer_base_init_expiry_lock(struct timer_base *base) { }
1319static inline void timer_base_lock_expiry(struct timer_base *base) { }
1320static inline void timer_base_unlock_expiry(struct timer_base *base) { }
1321static inline void timer_sync_wait_running(struct timer_base *base) { }
1322static inline void del_timer_wait_running(struct timer_list *timer) { }
1323#endif
1324
1325#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
Rolf Eike Beer2aae4a12006-09-29 01:59:46 -07001326/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * del_timer_sync - deactivate a timer and wait for the handler to finish.
1328 * @timer: the timer to be deactivated
1329 *
1330 * This function only differs from del_timer() on SMP: besides deactivating
1331 * the timer it also makes sure the handler has finished executing on other
1332 * CPUs.
1333 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001334 * Synchronization rules: Callers must prevent restarting of the timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * otherwise this function is meaningless. It must not be called from
Tejun Heoc5f66e92012-08-08 11:10:28 -07001336 * interrupt contexts unless the timer is an irqsafe one. The caller must
1337 * not hold locks which would prevent completion of the timer's
1338 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1339 * timer is not queued and the handler is not running on any CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 *
Tejun Heoc5f66e92012-08-08 11:10:28 -07001341 * Note: For !irqsafe timers, you must not hold locks that are held in
1342 * interrupt context while calling this function. Even if the lock has
Mauro Carvalho Chehabbf9c96b2018-05-07 06:35:48 -03001343 * nothing to do with the timer in question. Here's why::
Steven Rostedt48228f72011-02-08 12:39:54 -05001344 *
1345 * CPU0 CPU1
1346 * ---- ----
Mauro Carvalho Chehabbf9c96b2018-05-07 06:35:48 -03001347 * <SOFTIRQ>
1348 * call_timer_fn();
1349 * base->running_timer = mytimer;
1350 * spin_lock_irq(somelock);
Steven Rostedt48228f72011-02-08 12:39:54 -05001351 * <IRQ>
1352 * spin_lock(somelock);
Mauro Carvalho Chehabbf9c96b2018-05-07 06:35:48 -03001353 * del_timer_sync(mytimer);
1354 * while (base->running_timer == mytimer);
Steven Rostedt48228f72011-02-08 12:39:54 -05001355 *
1356 * Now del_timer_sync() will never return and never release somelock.
1357 * The interrupt on the other CPU is waiting to grab somelock but
1358 * it has interrupted the softirq that CPU0 is waiting to finish.
1359 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * The function returns whether it has deactivated a pending timer or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
1362int del_timer_sync(struct timer_list *timer)
1363{
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001364 int ret;
1365
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001366#ifdef CONFIG_LOCKDEP
Peter Zijlstraf266a512011-02-03 15:09:41 +01001367 unsigned long flags;
1368
Steven Rostedt48228f72011-02-08 12:39:54 -05001369 /*
1370 * If lockdep gives a backtrace here, please reference
1371 * the synchronization rules above.
1372 */
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001373 local_irq_save(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001374 lock_map_acquire(&timer->lockdep_map);
1375 lock_map_release(&timer->lockdep_map);
Peter Zijlstra7ff20792011-02-08 15:18:00 +01001376 local_irq_restore(flags);
Johannes Berg6f2b9b92009-01-29 16:03:20 +01001377#endif
Yong Zhang466bd302010-10-20 15:57:33 -07001378 /*
1379 * don't use it in hardirq context, because it
1380 * could lead to deadlock.
1381 */
Thomas Gleixner0eeda712015-05-26 22:50:29 +00001382 WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001383
Sebastian Andrzej Siewiorc725daf2020-11-03 20:09:37 +01001384 /*
1385 * Must be able to sleep on PREEMPT_RT because of the slowpath in
1386 * del_timer_wait_running().
1387 */
1388 if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(timer->flags & TIMER_IRQSAFE))
1389 lockdep_assert_preemption_enabled();
1390
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001391 do {
1392 ret = try_to_del_timer_sync(timer);
1393
1394 if (unlikely(ret < 0)) {
1395 del_timer_wait_running(timer);
1396 cpu_relax();
1397 }
1398 } while (ret < 0);
1399
1400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402EXPORT_SYMBOL(del_timer_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403#endif
1404
Anna-Maria Gleixnerf28d3d52019-03-21 13:09:21 +01001405static void call_timer_fn(struct timer_list *timer,
1406 void (*fn)(struct timer_list *),
1407 unsigned long baseclk)
Thomas Gleixner576da122010-03-12 21:10:29 +01001408{
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001409 int count = preempt_count();
Thomas Gleixner576da122010-03-12 21:10:29 +01001410
1411#ifdef CONFIG_LOCKDEP
1412 /*
1413 * It is permissible to free the timer from inside the
1414 * function that is called from it, this we need to take into
1415 * account for lockdep too. To avoid bogus "held lock freed"
1416 * warnings as well as problems when looking into
1417 * timer->lockdep_map, make a copy and use that here.
1418 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001419 struct lockdep_map lockdep_map;
1420
1421 lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
Thomas Gleixner576da122010-03-12 21:10:29 +01001422#endif
1423 /*
1424 * Couple the lock chain with the lock chain at
1425 * del_timer_sync() by acquiring the lock_map around the fn()
1426 * call here and in del_timer_sync().
1427 */
1428 lock_map_acquire(&lockdep_map);
1429
Anna-Maria Gleixnerf28d3d52019-03-21 13:09:21 +01001430 trace_timer_expire_entry(timer, baseclk);
Kees Cook354b46b12017-10-22 19:15:40 -07001431 fn(timer);
Thomas Gleixner576da122010-03-12 21:10:29 +01001432 trace_timer_expire_exit(timer);
1433
1434 lock_map_release(&lockdep_map);
1435
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001436 if (count != preempt_count()) {
Sakari Ailusd75f7732019-03-25 21:32:28 +02001437 WARN_ONCE(1, "timer: %pS preempt leak: %08x -> %08x\n",
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001438 fn, count, preempt_count());
Thomas Gleixner802702e2010-03-12 20:13:23 +01001439 /*
1440 * Restore the preempt count. That gives us a decent
1441 * chance to survive and extract information. If the
1442 * callback kept a lock held, bad luck, but not worse
1443 * than the BUG() we had.
1444 */
Peter Zijlstra4a2b4b22013-08-14 14:55:24 +02001445 preempt_count_set(count);
Thomas Gleixner576da122010-03-12 21:10:29 +01001446 }
1447}
1448
Thomas Gleixner500462a2016-07-04 09:50:30 +00001449static void expire_timers(struct timer_base *base, struct hlist_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Anna-Maria Gleixnerf28d3d52019-03-21 13:09:21 +01001451 /*
1452 * This value is required only for tracing. base->clk was
1453 * incremented directly before expire_timers was called. But expiry
1454 * is related to the old base->clk value.
1455 */
1456 unsigned long baseclk = base->clk - 1;
1457
Thomas Gleixner500462a2016-07-04 09:50:30 +00001458 while (!hlist_empty(head)) {
1459 struct timer_list *timer;
Kees Cook354b46b12017-10-22 19:15:40 -07001460 void (*fn)(struct timer_list *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Thomas Gleixner500462a2016-07-04 09:50:30 +00001462 timer = hlist_entry(head->first, struct timer_list, entry);
Thomas Gleixner3bb475a2015-05-26 22:50:24 +00001463
Thomas Gleixner500462a2016-07-04 09:50:30 +00001464 base->running_timer = timer;
1465 detach_timer(timer, true);
Thomas Gleixner3bb475a2015-05-26 22:50:24 +00001466
Thomas Gleixner500462a2016-07-04 09:50:30 +00001467 fn = timer->function;
Thomas Gleixner3bb475a2015-05-26 22:50:24 +00001468
Thomas Gleixner500462a2016-07-04 09:50:30 +00001469 if (timer->flags & TIMER_IRQSAFE) {
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001470 raw_spin_unlock(&base->lock);
Anna-Maria Gleixnerf28d3d52019-03-21 13:09:21 +01001471 call_timer_fn(timer, fn, baseclk);
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001472 base->running_timer = NULL;
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001473 raw_spin_lock(&base->lock);
Thomas Gleixner500462a2016-07-04 09:50:30 +00001474 } else {
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001475 raw_spin_unlock_irq(&base->lock);
Anna-Maria Gleixnerf28d3d52019-03-21 13:09:21 +01001476 call_timer_fn(timer, fn, baseclk);
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001477 base->running_timer = NULL;
1478 timer_sync_wait_running(base);
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001479 raw_spin_lock_irq(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 }
Thomas Gleixner500462a2016-07-04 09:50:30 +00001482}
1483
Frederic Weisbeckerd4f7dae2020-07-17 16:05:49 +02001484static int collect_expired_timers(struct timer_base *base,
1485 struct hlist_head *heads)
Thomas Gleixner500462a2016-07-04 09:50:30 +00001486{
Frederic Weisbeckerd4f7dae2020-07-17 16:05:49 +02001487 unsigned long clk = base->clk = base->next_expiry;
Thomas Gleixner500462a2016-07-04 09:50:30 +00001488 struct hlist_head *vec;
1489 int i, levels = 0;
1490 unsigned int idx;
1491
1492 for (i = 0; i < LVL_DEPTH; i++) {
1493 idx = (clk & LVL_MASK) + i * LVL_SIZE;
1494
1495 if (__test_and_clear_bit(idx, base->pending_map)) {
1496 vec = base->vectors + idx;
1497 hlist_move_list(vec, heads++);
1498 levels++;
1499 }
1500 /* Is it time to look at the next level? */
1501 if (clk & LVL_CLK_MASK)
1502 break;
1503 /* Shift clock for the next level granularity */
1504 clk >>= LVL_CLK_SHIFT;
1505 }
1506 return levels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/*
Anna-Maria Gleixner23696832016-07-04 09:50:34 +00001510 * Find the next pending bucket of a level. Search from level start (@offset)
1511 * + @clk upwards and if nothing there, search from start of the level
1512 * (@offset) up to @offset + clk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 */
Thomas Gleixner500462a2016-07-04 09:50:30 +00001514static int next_pending_bucket(struct timer_base *base, unsigned offset,
1515 unsigned clk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Thomas Gleixner500462a2016-07-04 09:50:30 +00001517 unsigned pos, start = offset + clk;
1518 unsigned end = offset + LVL_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Thomas Gleixner500462a2016-07-04 09:50:30 +00001520 pos = find_next_bit(base->pending_map, end, start);
1521 if (pos < end)
1522 return pos - start;
Venki Pallipadi6e453a62007-05-08 00:27:44 -07001523
Thomas Gleixner500462a2016-07-04 09:50:30 +00001524 pos = find_next_bit(base->pending_map, start, offset);
1525 return pos < start ? pos + LVL_SIZE - start : -1;
1526}
1527
1528/*
Anna-Maria Gleixner23696832016-07-04 09:50:34 +00001529 * Search the first expiring timer in the various clock levels. Caller must
1530 * hold base->lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 */
Thomas Gleixner494af3e2016-07-04 09:50:28 +00001532static unsigned long __next_timer_interrupt(struct timer_base *base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Thomas Gleixner500462a2016-07-04 09:50:30 +00001534 unsigned long clk, next, adj;
1535 unsigned lvl, offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Thomas Gleixner500462a2016-07-04 09:50:30 +00001537 next = base->clk + NEXT_TIMER_MAX_DELTA;
1538 clk = base->clk;
1539 for (lvl = 0; lvl < LVL_DEPTH; lvl++, offset += LVL_SIZE) {
1540 int pos = next_pending_bucket(base, offset, clk & LVL_MASK);
Frederic Weisbecker001ec1b2020-07-17 16:05:45 +02001541 unsigned long lvl_clk = clk & LVL_CLK_MASK;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001542
Thomas Gleixner500462a2016-07-04 09:50:30 +00001543 if (pos >= 0) {
1544 unsigned long tmp = clk + (unsigned long) pos;
1545
1546 tmp <<= LVL_SHIFT(lvl);
1547 if (time_before(tmp, next))
1548 next = tmp;
Frederic Weisbecker001ec1b2020-07-17 16:05:45 +02001549
1550 /*
1551 * If the next expiration happens before we reach
1552 * the next level, no need to check further.
1553 */
1554 if (pos <= ((LVL_CLK_DIV - lvl_clk) & LVL_CLK_MASK))
1555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Thomas Gleixner500462a2016-07-04 09:50:30 +00001557 /*
1558 * Clock for the next level. If the current level clock lower
1559 * bits are zero, we look at the next level as is. If not we
1560 * need to advance it by one because that's going to be the
1561 * next expiring bucket in that level. base->clk is the next
1562 * expiring jiffie. So in case of:
1563 *
1564 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1565 * 0 0 0 0 0 0
1566 *
1567 * we have to look at all levels @index 0. With
1568 *
1569 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1570 * 0 0 0 0 0 2
1571 *
1572 * LVL0 has the next expiring bucket @index 2. The upper
1573 * levels have the next expiring bucket @index 1.
1574 *
1575 * In case that the propagation wraps the next level the same
1576 * rules apply:
1577 *
1578 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1579 * 0 0 0 0 F 2
1580 *
1581 * So after looking at LVL0 we get:
1582 *
1583 * LVL5 LVL4 LVL3 LVL2 LVL1
1584 * 0 0 0 1 0
1585 *
1586 * So no propagation from LVL1 to LVL2 because that happened
1587 * with the add already, but then we need to propagate further
1588 * from LVL2 to LVL3.
1589 *
1590 * So the simple check whether the lower bits of the current
1591 * level are 0 or not is sufficient for all cases.
1592 */
Frederic Weisbecker001ec1b2020-07-17 16:05:45 +02001593 adj = lvl_clk ? 1 : 0;
Thomas Gleixner500462a2016-07-04 09:50:30 +00001594 clk >>= LVL_CLK_SHIFT;
1595 clk += adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Frederic Weisbecker31cd0e12020-07-23 17:16:41 +02001597
1598 base->next_expiry_recalc = false;
1599
Thomas Gleixner500462a2016-07-04 09:50:30 +00001600 return next;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001601}
1602
Frederic Weisbeckerdc2a0f12020-07-17 16:05:46 +02001603#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001604/*
1605 * Check, if the next hrtimer event is before the next timer wheel
1606 * event:
1607 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001608static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001609{
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001610 u64 nextevt = hrtimer_get_next_event();
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001611
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001612 /*
1613 * If high resolution timers are enabled
1614 * hrtimer_get_next_event() returns KTIME_MAX.
1615 */
1616 if (expires <= nextevt)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001617 return expires;
1618
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001619 /*
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001620 * If the next timer is already expired, return the tick base
1621 * time so the tick is fired immediately.
Thomas Gleixner9501b6c2007-03-25 14:31:17 +02001622 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001623 if (nextevt <= basem)
1624 return basem;
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001625
1626 /*
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001627 * Round up to the next jiffie. High resolution timers are
1628 * off, so the hrtimers are expired in the tick and we need to
1629 * make sure that this tick really expires the timer to avoid
1630 * a ping pong of the nohz stop code.
1631 *
1632 * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
Thomas Gleixnereaad0842007-05-29 23:47:39 +02001633 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001634 return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001635}
1636
1637/**
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001638 * get_next_timer_interrupt - return the time (clock mono) of the next timer
1639 * @basej: base time jiffies
1640 * @basem: base time clock monotonic
1641 *
1642 * Returns the tick aligned clock monotonic time of the next pending
1643 * timer or KTIME_MAX if no timer is pending.
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001644 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001645u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001646{
Thomas Gleixner500462a2016-07-04 09:50:30 +00001647 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001648 u64 expires = KTIME_MAX;
1649 unsigned long nextevt;
Chris Metcalf46c8f0b02016-08-08 16:29:07 -04001650 bool is_max_delta;
Thomas Gleixner1cfd6842007-02-16 01:27:46 -08001651
Heiko Carstensdbd87b52010-12-01 10:11:09 +01001652 /*
1653 * Pretend that there is no timer pending if the cpu is offline.
1654 * Possible pending timers will be migrated later to an active cpu.
1655 */
1656 if (cpu_is_offline(smp_processor_id()))
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001657 return expires;
1658
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001659 raw_spin_lock(&base->lock);
Frederic Weisbecker31cd0e12020-07-23 17:16:41 +02001660 if (base->next_expiry_recalc)
1661 base->next_expiry = __next_timer_interrupt(base);
1662 nextevt = base->next_expiry;
Chris Metcalf46c8f0b02016-08-08 16:29:07 -04001663 is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA);
Frederic Weisbecker31cd0e12020-07-23 17:16:41 +02001664
Thomas Gleixnera683f392016-07-04 09:50:36 +00001665 /*
Thomas Gleixner041ad7b2016-10-22 11:07:35 +00001666 * We have a fresh next event. Check whether we can forward the
1667 * base. We can only do that when @basej is past base->clk
1668 * otherwise we might rewind base->clk.
Thomas Gleixnera683f392016-07-04 09:50:36 +00001669 */
Thomas Gleixner041ad7b2016-10-22 11:07:35 +00001670 if (time_after(basej, base->clk)) {
1671 if (time_after(nextevt, basej))
1672 base->clk = basej;
1673 else if (time_after(nextevt, base->clk))
1674 base->clk = nextevt;
1675 }
Thomas Gleixnera683f392016-07-04 09:50:36 +00001676
1677 if (time_before_eq(nextevt, basej)) {
1678 expires = basem;
1679 base->is_idle = false;
1680 } else {
Chris Metcalf46c8f0b02016-08-08 16:29:07 -04001681 if (!is_max_delta)
Matija Glavinic Pecotic34f41c02017-08-01 09:11:52 +02001682 expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
Thomas Gleixnera683f392016-07-04 09:50:36 +00001683 /*
Nicholas Piggin2fe59f52017-08-22 18:43:48 +10001684 * If we expect to sleep more than a tick, mark the base idle.
1685 * Also the tick is stopped so any added timer must forward
1686 * the base clk itself to keep granularity small. This idle
1687 * logic is only maintained for the BASE_STD base, deferrable
1688 * timers may still see large granularity skew (by design).
Thomas Gleixnera683f392016-07-04 09:50:36 +00001689 */
Frederic Weisbecker1f8a4212020-07-17 16:05:48 +02001690 if ((expires - basem) > TICK_NSEC)
Thomas Gleixnera683f392016-07-04 09:50:36 +00001691 base->is_idle = true;
Thomas Gleixnere40468a2012-05-25 22:08:59 +00001692 }
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001693 raw_spin_unlock(&base->lock);
Tony Lindgren69239742006-03-06 15:42:45 -08001694
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +00001695 return cmp_next_hrtimer_event(basem, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
Anna-Maria Gleixner23696832016-07-04 09:50:34 +00001697
Thomas Gleixnera683f392016-07-04 09:50:36 +00001698/**
1699 * timer_clear_idle - Clear the idle state of the timer base
1700 *
1701 * Called with interrupts disabled
1702 */
1703void timer_clear_idle(void)
1704{
1705 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1706
1707 /*
1708 * We do this unlocked. The worst outcome is a remote enqueue sending
1709 * a pointless IPI, but taking the lock would just make the window for
1710 * sending the IPI a few instructions smaller for the cost of taking
1711 * the lock in the exit from idle path.
1712 */
1713 base->is_idle = false;
1714}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715#endif
1716
Anna-Maria Gleixner73420fe2016-07-04 09:50:33 +00001717/**
1718 * __run_timers - run all expired timers (if any) on this CPU.
1719 * @base: the timer vector to be processed.
1720 */
1721static inline void __run_timers(struct timer_base *base)
1722{
1723 struct hlist_head heads[LVL_DEPTH];
1724 int levels;
1725
Frederic Weisbeckerd4f7dae2020-07-17 16:05:49 +02001726 if (time_before(jiffies, base->next_expiry))
Anna-Maria Gleixner73420fe2016-07-04 09:50:33 +00001727 return;
1728
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001729 timer_base_lock_expiry(base);
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001730 raw_spin_lock_irq(&base->lock);
Anna-Maria Gleixner73420fe2016-07-04 09:50:33 +00001731
Frederic Weisbeckerd4f7dae2020-07-17 16:05:49 +02001732 while (time_after_eq(jiffies, base->clk) &&
1733 time_after_eq(jiffies, base->next_expiry)) {
Anna-Maria Gleixner73420fe2016-07-04 09:50:33 +00001734 levels = collect_expired_timers(base, heads);
Frederic Weisbecker31cd0e12020-07-23 17:16:41 +02001735 /*
1736 * The only possible reason for not finding any expired
1737 * timer at this clk is that all matching timers have been
1738 * dequeued.
1739 */
1740 WARN_ON_ONCE(!levels && !base->next_expiry_recalc);
Anna-Maria Gleixner73420fe2016-07-04 09:50:33 +00001741 base->clk++;
Frederic Weisbeckerdc2a0f12020-07-17 16:05:46 +02001742 base->next_expiry = __next_timer_interrupt(base);
Anna-Maria Gleixner73420fe2016-07-04 09:50:33 +00001743
1744 while (levels--)
1745 expire_timers(base, heads + levels);
1746 }
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001747 raw_spin_unlock_irq(&base->lock);
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02001748 timer_base_unlock_expiry(base);
Anna-Maria Gleixner73420fe2016-07-04 09:50:33 +00001749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 * This function runs timers and the timer-tq in bottom half context.
1753 */
Emese Revfy0766f782016-06-20 20:42:34 +02001754static __latent_entropy void run_timer_softirq(struct softirq_action *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Thomas Gleixner500462a2016-07-04 09:50:30 +00001756 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Thomas Gleixner500462a2016-07-04 09:50:30 +00001758 __run_timers(base);
Anna-Maria Gleixnerced6d5c2017-12-22 15:51:12 +01001759 if (IS_ENABLED(CONFIG_NO_HZ_COMMON))
Thomas Gleixner500462a2016-07-04 09:50:30 +00001760 __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
1763/*
1764 * Called by the local, per-CPU timer interrupt on SMP.
1765 */
Thomas Gleixnercc947f22020-11-16 10:53:38 +01001766static void run_local_timers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Thomas Gleixner4e858762016-07-04 09:50:37 +00001768 struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
1769
Peter Zijlstrad3d74452008-01-25 21:08:31 +01001770 hrtimer_run_queues();
Thomas Gleixner4e858762016-07-04 09:50:37 +00001771 /* Raise the softirq only if required. */
Frederic Weisbeckerd4f7dae2020-07-17 16:05:49 +02001772 if (time_before(jiffies, base->next_expiry)) {
Thomas Gleixnered4bbf72018-01-14 23:19:49 +01001773 if (!IS_ENABLED(CONFIG_NO_HZ_COMMON))
Thomas Gleixner4e858762016-07-04 09:50:37 +00001774 return;
1775 /* CPU is awake, so check the deferrable base. */
1776 base++;
Frederic Weisbeckerd4f7dae2020-07-17 16:05:49 +02001777 if (time_before(jiffies, base->next_expiry))
Thomas Gleixner4e858762016-07-04 09:50:37 +00001778 return;
1779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 raise_softirq(TIMER_SOFTIRQ);
1781}
1782
Kees Cook58e11772017-10-04 16:26:55 -07001783/*
Thomas Gleixnercc947f22020-11-16 10:53:38 +01001784 * Called from the timer interrupt handler to charge one tick to the current
1785 * process. user_tick is 1 if the tick is user time, 0 for system.
1786 */
1787void update_process_times(int user_tick)
1788{
1789 struct task_struct *p = current;
1790
1791 PRANDOM_ADD_NOISE(jiffies, user_tick, p, 0);
1792
1793 /* Note: this timer irq context must be accounted for as well. */
1794 account_process_tick(p, user_tick);
1795 run_local_timers();
1796 rcu_sched_clock_irq(user_tick);
1797#ifdef CONFIG_IRQ_WORK
1798 if (in_irq())
1799 irq_work_tick();
1800#endif
1801 scheduler_tick();
1802 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
1803 run_posix_cpu_timers();
1804}
1805
1806/*
Kees Cook58e11772017-10-04 16:26:55 -07001807 * Since schedule_timeout()'s timer is defined on the stack, it must store
1808 * the target task on the stack as well.
1809 */
1810struct process_timer {
1811 struct timer_list timer;
1812 struct task_struct *task;
1813};
1814
1815static void process_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Kees Cook58e11772017-10-04 16:26:55 -07001817 struct process_timer *timeout = from_timer(timeout, t, timer);
1818
1819 wake_up_process(timeout->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
1822/**
1823 * schedule_timeout - sleep until timeout
1824 * @timeout: timeout value in jiffies
1825 *
Alexander Popov6e317c32020-01-18 01:59:00 +03001826 * Make the current task sleep until @timeout jiffies have elapsed.
1827 * The function behavior depends on the current task state
1828 * (see also set_current_state() description):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 *
Alexander Popov6e317c32020-01-18 01:59:00 +03001830 * %TASK_RUNNING - the scheduler is called, but the task does not sleep
1831 * at all. That happens because sched_submit_work() does nothing for
1832 * tasks in %TASK_RUNNING state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 *
1834 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001835 * pass before the routine returns unless the current task is explicitly
Alexander Popov6e317c32020-01-18 01:59:00 +03001836 * woken up, (e.g. by wake_up_process()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 *
1838 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001839 * delivered to the current task or the current task is explicitly woken
1840 * up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 *
Alexander Popov6e317c32020-01-18 01:59:00 +03001842 * The current task state is guaranteed to be %TASK_RUNNING when this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 * routine returns.
1844 *
1845 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1846 * the CPU away without a bound on the timeout. In this case the return
1847 * value will be %MAX_SCHEDULE_TIMEOUT.
1848 *
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001849 * Returns 0 when the timer has expired otherwise the remaining time in
Alexander Popov6e317c32020-01-18 01:59:00 +03001850 * jiffies will be returned. In all cases the return value is guaranteed
Douglas Anderson4b7e9cf2016-10-21 08:58:51 -07001851 * to be non-negative.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001853signed long __sched schedule_timeout(signed long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Kees Cook58e11772017-10-04 16:26:55 -07001855 struct process_timer timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 unsigned long expire;
1857
1858 switch (timeout)
1859 {
1860 case MAX_SCHEDULE_TIMEOUT:
1861 /*
1862 * These two special cases are useful to be comfortable
1863 * in the caller. Nothing more. We could take
1864 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1865 * but I' d like to return a valid offset (>=0) to allow
1866 * the caller to do everything it want with the retval.
1867 */
1868 schedule();
1869 goto out;
1870 default:
1871 /*
1872 * Another bit of PARANOID. Note that the retval will be
1873 * 0 since no piece of kernel is supposed to do a check
1874 * for a negative retval of schedule_timeout() (since it
1875 * should never happens anyway). You just have the printk()
1876 * that will tell you if something is gone wrong and where.
1877 */
Andrew Morton5b149bc2006-12-22 01:10:14 -08001878 if (timeout < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 printk(KERN_ERR "schedule_timeout: wrong timeout "
Andrew Morton5b149bc2006-12-22 01:10:14 -08001880 "value %lx\n", timeout);
1881 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 current->state = TASK_RUNNING;
1883 goto out;
1884 }
1885 }
1886
1887 expire = timeout + jiffies;
1888
Kees Cook58e11772017-10-04 16:26:55 -07001889 timer.task = current;
1890 timer_setup_on_stack(&timer.timer, process_timeout, 0);
Eric Dumazet90c01892019-11-07 11:37:38 -08001891 __mod_timer(&timer.timer, expire, MOD_TIMER_NOTPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 schedule();
Kees Cook58e11772017-10-04 16:26:55 -07001893 del_singleshot_timer_sync(&timer.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001895 /* Remove the timer from the object tracker */
Kees Cook58e11772017-10-04 16:26:55 -07001896 destroy_timer_on_stack(&timer.timer);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 timeout = expire - jiffies;
1899
1900 out:
1901 return timeout < 0 ? 0 : timeout;
1902}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903EXPORT_SYMBOL(schedule_timeout);
1904
Andrew Morton8a1c1752005-09-13 01:25:15 -07001905/*
1906 * We can use __set_current_state() here because schedule_timeout() calls
1907 * schedule() unconditionally.
1908 */
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001909signed long __sched schedule_timeout_interruptible(signed long timeout)
1910{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001911 __set_current_state(TASK_INTERRUPTIBLE);
1912 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001913}
1914EXPORT_SYMBOL(schedule_timeout_interruptible);
1915
Matthew Wilcox294d5cc2007-12-06 11:59:46 -05001916signed long __sched schedule_timeout_killable(signed long timeout)
1917{
1918 __set_current_state(TASK_KILLABLE);
1919 return schedule_timeout(timeout);
1920}
1921EXPORT_SYMBOL(schedule_timeout_killable);
1922
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001923signed long __sched schedule_timeout_uninterruptible(signed long timeout)
1924{
Andrew Mortona5a0d522005-10-30 15:01:42 -08001925 __set_current_state(TASK_UNINTERRUPTIBLE);
1926 return schedule_timeout(timeout);
Nishanth Aravamudan64ed93a2005-09-10 00:27:21 -07001927}
1928EXPORT_SYMBOL(schedule_timeout_uninterruptible);
1929
Andrew Morton69b27ba2016-03-25 14:20:21 -07001930/*
1931 * Like schedule_timeout_uninterruptible(), except this task will not contribute
1932 * to load average.
1933 */
1934signed long __sched schedule_timeout_idle(signed long timeout)
1935{
1936 __set_current_state(TASK_IDLE);
1937 return schedule_timeout(timeout);
1938}
1939EXPORT_SYMBOL(schedule_timeout_idle);
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941#ifdef CONFIG_HOTPLUG_CPU
Thomas Gleixner494af3e2016-07-04 09:50:28 +00001942static void migrate_timer_list(struct timer_base *new_base, struct hlist_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 struct timer_list *timer;
Thomas Gleixner0eeda712015-05-26 22:50:29 +00001945 int cpu = new_base->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Thomas Gleixner1dabbce2015-05-26 22:50:28 +00001947 while (!hlist_empty(head)) {
1948 timer = hlist_entry(head->first, struct timer_list, entry);
Thomas Gleixnerec44bc72012-05-25 22:08:57 +00001949 detach_timer(timer, false);
Thomas Gleixner0eeda712015-05-26 22:50:29 +00001950 timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 internal_add_timer(new_base, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
Thomas Gleixner26456f82017-12-27 21:37:25 +01001955int timers_prepare_cpu(unsigned int cpu)
1956{
1957 struct timer_base *base;
1958 int b;
1959
1960 for (b = 0; b < NR_BASES; b++) {
1961 base = per_cpu_ptr(&timer_bases[b], cpu);
1962 base->clk = jiffies;
1963 base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA;
1964 base->is_idle = false;
Thomas Gleixner26456f82017-12-27 21:37:25 +01001965 }
1966 return 0;
1967}
1968
Richard Cochran24f73b92016-07-13 17:16:59 +00001969int timers_dead_cpu(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Thomas Gleixner494af3e2016-07-04 09:50:28 +00001971 struct timer_base *old_base;
1972 struct timer_base *new_base;
Thomas Gleixner500462a2016-07-04 09:50:30 +00001973 int b, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 BUG_ON(cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Thomas Gleixner500462a2016-07-04 09:50:30 +00001977 for (b = 0; b < NR_BASES; b++) {
1978 old_base = per_cpu_ptr(&timer_bases[b], cpu);
1979 new_base = get_cpu_ptr(&timer_bases[b]);
1980 /*
1981 * The caller is globally serialized and nobody else
1982 * takes two locks at once, deadlock is not possible.
1983 */
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001984 raw_spin_lock_irq(&new_base->lock);
1985 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
Oleg Nesterov3691c512006-03-31 02:30:30 -08001986
Lingutla Chandrasekharc52232a2018-01-18 17:20:22 +05301987 /*
1988 * The current CPUs base clock might be stale. Update it
1989 * before moving the timers over.
1990 */
1991 forward_timer_base(new_base);
1992
Thomas Gleixner500462a2016-07-04 09:50:30 +00001993 BUG_ON(old_base->running_timer);
1994
1995 for (i = 0; i < WHEEL_SIZE; i++)
1996 migrate_timer_list(new_base, old_base->vectors + i);
1997
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02001998 raw_spin_unlock(&old_base->lock);
1999 raw_spin_unlock_irq(&new_base->lock);
Thomas Gleixner500462a2016-07-04 09:50:30 +00002000 put_cpu_ptr(&timer_bases);
Oleg Nesterov55c888d2005-06-23 00:08:56 -07002001 }
Richard Cochran24f73b92016-07-13 17:16:59 +00002002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Peter Zijlstra3650b572015-03-31 20:49:02 +05302005#endif /* CONFIG_HOTPLUG_CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Thomas Gleixner0eeda712015-05-26 22:50:29 +00002007static void __init init_timer_cpu(int cpu)
Viresh Kumar8def9062015-03-31 20:49:01 +05302008{
Thomas Gleixner500462a2016-07-04 09:50:30 +00002009 struct timer_base *base;
2010 int i;
Peter Zijlstra3650b572015-03-31 20:49:02 +05302011
Thomas Gleixner500462a2016-07-04 09:50:30 +00002012 for (i = 0; i < NR_BASES; i++) {
2013 base = per_cpu_ptr(&timer_bases[i], cpu);
2014 base->cpu = cpu;
Sebastian Andrzej Siewior2287d862017-06-27 18:15:38 +02002015 raw_spin_lock_init(&base->lock);
Thomas Gleixner500462a2016-07-04 09:50:30 +00002016 base->clk = jiffies;
Frederic Weisbeckerdc2a0f12020-07-17 16:05:46 +02002017 base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA;
Anna-Maria Gleixner030dcdd2019-07-26 20:31:00 +02002018 timer_base_init_expiry_lock(base);
Thomas Gleixner500462a2016-07-04 09:50:30 +00002019 }
Viresh Kumar8def9062015-03-31 20:49:01 +05302020}
2021
2022static void __init init_timer_cpus(void)
2023{
Viresh Kumar8def9062015-03-31 20:49:01 +05302024 int cpu;
2025
Thomas Gleixner0eeda712015-05-26 22:50:29 +00002026 for_each_possible_cpu(cpu)
2027 init_timer_cpu(cpu);
Viresh Kumar8def9062015-03-31 20:49:01 +05302028}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030void __init init_timers(void)
2031{
Viresh Kumar8def9062015-03-31 20:49:01 +05302032 init_timer_cpus();
Thomas Gleixner1fb497d2020-07-30 12:14:06 +02002033 posix_cputimers_init_work();
Carlos R. Mafra962cf362008-05-15 11:15:37 -03002034 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/**
2038 * msleep - sleep safely even with waitqueue interruptions
2039 * @msecs: Time in milliseconds to sleep for
2040 */
2041void msleep(unsigned int msecs)
2042{
2043 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
2044
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002045 while (timeout)
2046 timeout = schedule_timeout_uninterruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049EXPORT_SYMBOL(msleep);
2050
2051/**
Domen Puncer96ec3ef2005-06-25 14:58:43 -07002052 * msleep_interruptible - sleep waiting for signals
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 * @msecs: Time in milliseconds to sleep for
2054 */
2055unsigned long msleep_interruptible(unsigned int msecs)
2056{
2057 unsigned long timeout = msecs_to_jiffies(msecs) + 1;
2058
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002059 while (timeout && !signal_pending(current))
2060 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return jiffies_to_msecs(timeout);
2062}
2063
2064EXPORT_SYMBOL(msleep_interruptible);
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07002065
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07002066/**
Bjorn Helgaasb5227d02016-05-31 16:23:02 -05002067 * usleep_range - Sleep for an approximate time
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07002068 * @min: Minimum time in usecs to sleep
2069 * @max: Maximum time in usecs to sleep
Bjorn Helgaasb5227d02016-05-31 16:23:02 -05002070 *
2071 * In non-atomic context where the exact wakeup time is flexible, use
2072 * usleep_range() instead of udelay(). The sleep improves responsiveness
2073 * by avoiding the CPU-hogging busy-wait of udelay(), and the range reduces
2074 * power usage by allowing hrtimers to take advantage of an already-
2075 * scheduled interrupt instead of scheduling a new one just for this sleep.
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07002076 */
Thomas Gleixner2ad5d322015-04-14 21:09:30 +00002077void __sched usleep_range(unsigned long min, unsigned long max)
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07002078{
Douglas Anderson6c5e9052016-10-21 08:58:50 -07002079 ktime_t exp = ktime_add_us(ktime_get(), min);
2080 u64 delta = (u64)(max - min) * NSEC_PER_USEC;
2081
2082 for (;;) {
2083 __set_current_state(TASK_UNINTERRUPTIBLE);
2084 /* Do not return before the requested sleep time has elapsed */
2085 if (!schedule_hrtimeout_range(&exp, delta, HRTIMER_MODE_ABS))
2086 break;
2087 }
Patrick Pannuto5e7f5a12010-08-02 15:01:04 -07002088}
2089EXPORT_SYMBOL(usleep_range);