135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
25cee9645SThomas Gleixner /*
35cee9645SThomas Gleixner * Kernel internal timers
45cee9645SThomas Gleixner *
55cee9645SThomas Gleixner * Copyright (C) 1991, 1992 Linus Torvalds
65cee9645SThomas Gleixner *
75cee9645SThomas Gleixner * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
85cee9645SThomas Gleixner *
95cee9645SThomas Gleixner * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
105cee9645SThomas Gleixner * "A Kernel Model for Precision Timekeeping" by Dave Mills
115cee9645SThomas Gleixner * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
125cee9645SThomas Gleixner * serialize accesses to xtime/lost_ticks).
135cee9645SThomas Gleixner * Copyright (C) 1998 Andrea Arcangeli
145cee9645SThomas Gleixner * 1999-03-10 Improved NTP compatibility by Ulrich Windl
155cee9645SThomas Gleixner * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
165cee9645SThomas Gleixner * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
175cee9645SThomas Gleixner * Copyright (C) 2000, 2001, 2002 Ingo Molnar
185cee9645SThomas Gleixner * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
195cee9645SThomas Gleixner */
205cee9645SThomas Gleixner
215cee9645SThomas Gleixner #include <linux/kernel_stat.h>
225cee9645SThomas Gleixner #include <linux/export.h>
235cee9645SThomas Gleixner #include <linux/interrupt.h>
245cee9645SThomas Gleixner #include <linux/percpu.h>
255cee9645SThomas Gleixner #include <linux/init.h>
265cee9645SThomas Gleixner #include <linux/mm.h>
275cee9645SThomas Gleixner #include <linux/swap.h>
285cee9645SThomas Gleixner #include <linux/pid_namespace.h>
295cee9645SThomas Gleixner #include <linux/notifier.h>
305cee9645SThomas Gleixner #include <linux/thread_info.h>
315cee9645SThomas Gleixner #include <linux/time.h>
325cee9645SThomas Gleixner #include <linux/jiffies.h>
335cee9645SThomas Gleixner #include <linux/posix-timers.h>
345cee9645SThomas Gleixner #include <linux/cpu.h>
355cee9645SThomas Gleixner #include <linux/syscalls.h>
365cee9645SThomas Gleixner #include <linux/delay.h>
375cee9645SThomas Gleixner #include <linux/tick.h>
385cee9645SThomas Gleixner #include <linux/kallsyms.h>
395cee9645SThomas Gleixner #include <linux/irq_work.h>
405cee9645SThomas Gleixner #include <linux/sched/sysctl.h>
41370c9135SIngo Molnar #include <linux/sched/nohz.h>
42b17b0153SIngo Molnar #include <linux/sched/debug.h>
435cee9645SThomas Gleixner #include <linux/slab.h>
445cee9645SThomas Gleixner #include <linux/compat.h>
45f227e3ecSWilly Tarreau #include <linux/random.h>
46efaa0227Stangmeng #include <linux/sysctl.h>
475cee9645SThomas Gleixner
487c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
495cee9645SThomas Gleixner #include <asm/unistd.h>
505cee9645SThomas Gleixner #include <asm/div64.h>
515cee9645SThomas Gleixner #include <asm/timex.h>
525cee9645SThomas Gleixner #include <asm/io.h>
535cee9645SThomas Gleixner
54c1ad348bSThomas Gleixner #include "tick-internal.h"
557ee98877SAnna-Maria Behnsen #include "timer_migration.h"
56c1ad348bSThomas Gleixner
575cee9645SThomas Gleixner #define CREATE_TRACE_POINTS
585cee9645SThomas Gleixner #include <trace/events/timer.h>
595cee9645SThomas Gleixner
605cee9645SThomas Gleixner __visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
615cee9645SThomas Gleixner
625cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64);
635cee9645SThomas Gleixner
645cee9645SThomas Gleixner /*
65500462a9SThomas Gleixner * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
669e643ab5SRandy Dunlap * LVL_SIZE buckets. Each level is driven by its own clock and therefore each
67500462a9SThomas Gleixner * level has a different granularity.
68500462a9SThomas Gleixner *
699e643ab5SRandy Dunlap * The level granularity is: LVL_CLK_DIV ^ level
70500462a9SThomas Gleixner * The level clock frequency is: HZ / (LVL_CLK_DIV ^ level)
71500462a9SThomas Gleixner *
72500462a9SThomas Gleixner * The array level of a newly armed timer depends on the relative expiry
73500462a9SThomas Gleixner * time. The farther the expiry time is away the higher the array level and
749e643ab5SRandy Dunlap * therefore the granularity becomes.
75500462a9SThomas Gleixner *
76500462a9SThomas Gleixner * Contrary to the original timer wheel implementation, which aims for 'exact'
77500462a9SThomas Gleixner * expiry of the timers, this implementation removes the need for recascading
78500462a9SThomas Gleixner * the timers into the lower array levels. The previous 'classic' timer wheel
79500462a9SThomas Gleixner * implementation of the kernel already violated the 'exact' expiry by adding
80500462a9SThomas Gleixner * slack to the expiry time to provide batched expiration. The granularity
81500462a9SThomas Gleixner * levels provide implicit batching.
82500462a9SThomas Gleixner *
83500462a9SThomas Gleixner * This is an optimization of the original timer wheel implementation for the
84500462a9SThomas Gleixner * majority of the timer wheel use cases: timeouts. The vast majority of
85500462a9SThomas Gleixner * timeout timers (networking, disk I/O ...) are canceled before expiry. If
86500462a9SThomas Gleixner * the timeout expires it indicates that normal operation is disturbed, so it
87500462a9SThomas Gleixner * does not matter much whether the timeout comes with a slight delay.
88500462a9SThomas Gleixner *
89500462a9SThomas Gleixner * The only exception to this are networking timers with a small expiry
90500462a9SThomas Gleixner * time. They rely on the granularity. Those fit into the first wheel level,
91500462a9SThomas Gleixner * which has HZ granularity.
92500462a9SThomas Gleixner *
93500462a9SThomas Gleixner * We don't have cascading anymore. timers with a expiry time above the
94500462a9SThomas Gleixner * capacity of the last wheel level are force expired at the maximum timeout
95500462a9SThomas Gleixner * value of the last wheel level. From data sampling we know that the maximum
96500462a9SThomas Gleixner * value observed is 5 days (network connection tracking), so this should not
97500462a9SThomas Gleixner * be an issue.
98500462a9SThomas Gleixner *
99500462a9SThomas Gleixner * The currently chosen array constants values are a good compromise between
100500462a9SThomas Gleixner * array size and granularity.
101500462a9SThomas Gleixner *
102500462a9SThomas Gleixner * This results in the following granularity and range levels:
103500462a9SThomas Gleixner *
104500462a9SThomas Gleixner * HZ 1000 steps
105500462a9SThomas Gleixner * Level Offset Granularity Range
106500462a9SThomas Gleixner * 0 0 1 ms 0 ms - 63 ms
107500462a9SThomas Gleixner * 1 64 8 ms 64 ms - 511 ms
108500462a9SThomas Gleixner * 2 128 64 ms 512 ms - 4095 ms (512ms - ~4s)
109500462a9SThomas Gleixner * 3 192 512 ms 4096 ms - 32767 ms (~4s - ~32s)
110500462a9SThomas Gleixner * 4 256 4096 ms (~4s) 32768 ms - 262143 ms (~32s - ~4m)
111500462a9SThomas Gleixner * 5 320 32768 ms (~32s) 262144 ms - 2097151 ms (~4m - ~34m)
112500462a9SThomas Gleixner * 6 384 262144 ms (~4m) 2097152 ms - 16777215 ms (~34m - ~4h)
113500462a9SThomas Gleixner * 7 448 2097152 ms (~34m) 16777216 ms - 134217727 ms (~4h - ~1d)
114500462a9SThomas Gleixner * 8 512 16777216 ms (~4h) 134217728 ms - 1073741822 ms (~1d - ~12d)
115500462a9SThomas Gleixner *
116500462a9SThomas Gleixner * HZ 300
117500462a9SThomas Gleixner * Level Offset Granularity Range
118500462a9SThomas Gleixner * 0 0 3 ms 0 ms - 210 ms
119500462a9SThomas Gleixner * 1 64 26 ms 213 ms - 1703 ms (213ms - ~1s)
120500462a9SThomas Gleixner * 2 128 213 ms 1706 ms - 13650 ms (~1s - ~13s)
121500462a9SThomas Gleixner * 3 192 1706 ms (~1s) 13653 ms - 109223 ms (~13s - ~1m)
122500462a9SThomas Gleixner * 4 256 13653 ms (~13s) 109226 ms - 873810 ms (~1m - ~14m)
123500462a9SThomas Gleixner * 5 320 109226 ms (~1m) 873813 ms - 6990503 ms (~14m - ~1h)
124500462a9SThomas Gleixner * 6 384 873813 ms (~14m) 6990506 ms - 55924050 ms (~1h - ~15h)
125500462a9SThomas Gleixner * 7 448 6990506 ms (~1h) 55924053 ms - 447392423 ms (~15h - ~5d)
126500462a9SThomas Gleixner * 8 512 55924053 ms (~15h) 447392426 ms - 3579139406 ms (~5d - ~41d)
127500462a9SThomas Gleixner *
128500462a9SThomas Gleixner * HZ 250
129500462a9SThomas Gleixner * Level Offset Granularity Range
130500462a9SThomas Gleixner * 0 0 4 ms 0 ms - 255 ms
131500462a9SThomas Gleixner * 1 64 32 ms 256 ms - 2047 ms (256ms - ~2s)
132500462a9SThomas Gleixner * 2 128 256 ms 2048 ms - 16383 ms (~2s - ~16s)
133500462a9SThomas Gleixner * 3 192 2048 ms (~2s) 16384 ms - 131071 ms (~16s - ~2m)
134500462a9SThomas Gleixner * 4 256 16384 ms (~16s) 131072 ms - 1048575 ms (~2m - ~17m)
135500462a9SThomas Gleixner * 5 320 131072 ms (~2m) 1048576 ms - 8388607 ms (~17m - ~2h)
136500462a9SThomas Gleixner * 6 384 1048576 ms (~17m) 8388608 ms - 67108863 ms (~2h - ~18h)
137500462a9SThomas Gleixner * 7 448 8388608 ms (~2h) 67108864 ms - 536870911 ms (~18h - ~6d)
138500462a9SThomas Gleixner * 8 512 67108864 ms (~18h) 536870912 ms - 4294967288 ms (~6d - ~49d)
139500462a9SThomas Gleixner *
140500462a9SThomas Gleixner * HZ 100
141500462a9SThomas Gleixner * Level Offset Granularity Range
142500462a9SThomas Gleixner * 0 0 10 ms 0 ms - 630 ms
143500462a9SThomas Gleixner * 1 64 80 ms 640 ms - 5110 ms (640ms - ~5s)
144500462a9SThomas Gleixner * 2 128 640 ms 5120 ms - 40950 ms (~5s - ~40s)
145500462a9SThomas Gleixner * 3 192 5120 ms (~5s) 40960 ms - 327670 ms (~40s - ~5m)
146500462a9SThomas Gleixner * 4 256 40960 ms (~40s) 327680 ms - 2621430 ms (~5m - ~43m)
147500462a9SThomas Gleixner * 5 320 327680 ms (~5m) 2621440 ms - 20971510 ms (~43m - ~5h)
148500462a9SThomas Gleixner * 6 384 2621440 ms (~43m) 20971520 ms - 167772150 ms (~5h - ~1d)
149500462a9SThomas Gleixner * 7 448 20971520 ms (~5h) 167772160 ms - 1342177270 ms (~1d - ~15d)
1505cee9645SThomas Gleixner */
1515cee9645SThomas Gleixner
152500462a9SThomas Gleixner /* Clock divisor for the next level */
153500462a9SThomas Gleixner #define LVL_CLK_SHIFT 3
154500462a9SThomas Gleixner #define LVL_CLK_DIV (1UL << LVL_CLK_SHIFT)
155500462a9SThomas Gleixner #define LVL_CLK_MASK (LVL_CLK_DIV - 1)
156500462a9SThomas Gleixner #define LVL_SHIFT(n) ((n) * LVL_CLK_SHIFT)
157500462a9SThomas Gleixner #define LVL_GRAN(n) (1UL << LVL_SHIFT(n))
1585cee9645SThomas Gleixner
159500462a9SThomas Gleixner /*
160500462a9SThomas Gleixner * The time start value for each level to select the bucket at enqueue
16144688972SFrederic Weisbecker * time. We start from the last possible delta of the previous level
16244688972SFrederic Weisbecker * so that we can later add an extra LVL_GRAN(n) to n (see calc_index()).
163500462a9SThomas Gleixner */
164500462a9SThomas Gleixner #define LVL_START(n) ((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
1655cee9645SThomas Gleixner
166500462a9SThomas Gleixner /* Size of each clock level */
167500462a9SThomas Gleixner #define LVL_BITS 6
168500462a9SThomas Gleixner #define LVL_SIZE (1UL << LVL_BITS)
169500462a9SThomas Gleixner #define LVL_MASK (LVL_SIZE - 1)
170500462a9SThomas Gleixner #define LVL_OFFS(n) ((n) * LVL_SIZE)
171500462a9SThomas Gleixner
172500462a9SThomas Gleixner /* Level depth */
173500462a9SThomas Gleixner #if HZ > 100
174500462a9SThomas Gleixner # define LVL_DEPTH 9
175500462a9SThomas Gleixner # else
176500462a9SThomas Gleixner # define LVL_DEPTH 8
177500462a9SThomas Gleixner #endif
178500462a9SThomas Gleixner
179500462a9SThomas Gleixner /* The cutoff (max. capacity of the wheel) */
180500462a9SThomas Gleixner #define WHEEL_TIMEOUT_CUTOFF (LVL_START(LVL_DEPTH))
181500462a9SThomas Gleixner #define WHEEL_TIMEOUT_MAX (WHEEL_TIMEOUT_CUTOFF - LVL_GRAN(LVL_DEPTH - 1))
182500462a9SThomas Gleixner
183500462a9SThomas Gleixner /*
184500462a9SThomas Gleixner * The resulting wheel size. If NOHZ is configured we allocate two
185500462a9SThomas Gleixner * wheels so we have a separate storage for the deferrable timers.
186500462a9SThomas Gleixner */
187500462a9SThomas Gleixner #define WHEEL_SIZE (LVL_SIZE * LVL_DEPTH)
188500462a9SThomas Gleixner
189500462a9SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
19083a665dcSAnna-Maria Behnsen /*
19183a665dcSAnna-Maria Behnsen * If multiple bases need to be locked, use the base ordering for lock
19283a665dcSAnna-Maria Behnsen * nesting, i.e. lowest number first.
19383a665dcSAnna-Maria Behnsen */
19483a665dcSAnna-Maria Behnsen # define NR_BASES 3
19583a665dcSAnna-Maria Behnsen # define BASE_LOCAL 0
19683a665dcSAnna-Maria Behnsen # define BASE_GLOBAL 1
19783a665dcSAnna-Maria Behnsen # define BASE_DEF 2
198500462a9SThomas Gleixner #else
199500462a9SThomas Gleixner # define NR_BASES 1
20083a665dcSAnna-Maria Behnsen # define BASE_LOCAL 0
20183a665dcSAnna-Maria Behnsen # define BASE_GLOBAL 0
202500462a9SThomas Gleixner # define BASE_DEF 0
203500462a9SThomas Gleixner #endif
2045cee9645SThomas Gleixner
205892abd35SAnna-Maria Behnsen /**
206892abd35SAnna-Maria Behnsen * struct timer_base - Per CPU timer base (number of base depends on config)
207892abd35SAnna-Maria Behnsen * @lock: Lock protecting the timer_base
208892abd35SAnna-Maria Behnsen * @running_timer: When expiring timers, the lock is dropped. To make
2099e643ab5SRandy Dunlap * sure not to race against deleting/modifying a
210892abd35SAnna-Maria Behnsen * currently running timer, the pointer is set to the
211892abd35SAnna-Maria Behnsen * timer, which expires at the moment. If no timer is
212892abd35SAnna-Maria Behnsen * running, the pointer is NULL.
213892abd35SAnna-Maria Behnsen * @expiry_lock: PREEMPT_RT only: Lock is taken in softirq around
214892abd35SAnna-Maria Behnsen * timer expiry callback execution and when trying to
215892abd35SAnna-Maria Behnsen * delete a running timer and it wasn't successful in
216892abd35SAnna-Maria Behnsen * the first glance. It prevents priority inversion
217892abd35SAnna-Maria Behnsen * when callback was preempted on a remote CPU and a
218892abd35SAnna-Maria Behnsen * caller tries to delete the running timer. It also
219892abd35SAnna-Maria Behnsen * prevents a life lock, when the task which tries to
220892abd35SAnna-Maria Behnsen * delete a timer preempted the softirq thread which
221892abd35SAnna-Maria Behnsen * is running the timer callback function.
222892abd35SAnna-Maria Behnsen * @timer_waiters: PREEMPT_RT only: Tells, if there is a waiter
223892abd35SAnna-Maria Behnsen * waiting for the end of the timer callback function
224892abd35SAnna-Maria Behnsen * execution.
225892abd35SAnna-Maria Behnsen * @clk: clock of the timer base; is updated before enqueue
226892abd35SAnna-Maria Behnsen * of a timer; during expiry, it is 1 offset ahead of
227892abd35SAnna-Maria Behnsen * jiffies to avoid endless requeuing to current
228892abd35SAnna-Maria Behnsen * jiffies
229892abd35SAnna-Maria Behnsen * @next_expiry: expiry value of the first timer; it is updated when
230892abd35SAnna-Maria Behnsen * finding the next timer and during enqueue; the
231892abd35SAnna-Maria Behnsen * value is not valid, when next_expiry_recalc is set
232892abd35SAnna-Maria Behnsen * @cpu: Number of CPU the timer base belongs to
233892abd35SAnna-Maria Behnsen * @next_expiry_recalc: States, whether a recalculation of next_expiry is
234892abd35SAnna-Maria Behnsen * required. Value is set true, when a timer was
235892abd35SAnna-Maria Behnsen * deleted.
236892abd35SAnna-Maria Behnsen * @is_idle: Is set, when timer_base is idle. It is triggered by NOHZ
237892abd35SAnna-Maria Behnsen * code. This state is only used in standard
238892abd35SAnna-Maria Behnsen * base. Deferrable timers, which are enqueued remotely
239892abd35SAnna-Maria Behnsen * never wake up an idle CPU. So no matter of supporting it
240892abd35SAnna-Maria Behnsen * for this base.
241892abd35SAnna-Maria Behnsen * @timers_pending: Is set, when a timer is pending in the base. It is only
242892abd35SAnna-Maria Behnsen * reliable when next_expiry_recalc is not set.
243892abd35SAnna-Maria Behnsen * @pending_map: bitmap of the timer wheel; each bit reflects a
244892abd35SAnna-Maria Behnsen * bucket of the wheel. When a bit is set, at least a
245892abd35SAnna-Maria Behnsen * single timer is enqueued in the related bucket.
246892abd35SAnna-Maria Behnsen * @vectors: Array of lists; Each array member reflects a bucket
247892abd35SAnna-Maria Behnsen * of the timer wheel. The list contains all timers
248892abd35SAnna-Maria Behnsen * which are enqueued into a specific bucket.
249892abd35SAnna-Maria Behnsen */
250494af3edSThomas Gleixner struct timer_base {
2512287d866SSebastian Andrzej Siewior raw_spinlock_t lock;
2525cee9645SThomas Gleixner struct timer_list *running_timer;
253030dcdd1SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT
254030dcdd1SAnna-Maria Gleixner spinlock_t expiry_lock;
255030dcdd1SAnna-Maria Gleixner atomic_t timer_waiters;
256030dcdd1SAnna-Maria Gleixner #endif
257494af3edSThomas Gleixner unsigned long clk;
258a683f390SThomas Gleixner unsigned long next_expiry;
259500462a9SThomas Gleixner unsigned int cpu;
26031cd0e11SFrederic Weisbecker bool next_expiry_recalc;
261a683f390SThomas Gleixner bool is_idle;
262aebacb7fSNicolas Saenz Julienne bool timers_pending;
263500462a9SThomas Gleixner DECLARE_BITMAP(pending_map, WHEEL_SIZE);
264500462a9SThomas Gleixner struct hlist_head vectors[WHEEL_SIZE];
2655cee9645SThomas Gleixner } ____cacheline_aligned;
2665cee9645SThomas Gleixner
267500462a9SThomas Gleixner static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
2685cee9645SThomas Gleixner
269ae67badaSThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON
270ae67badaSThomas Gleixner
27114c80341SAnna-Maria Gleixner static DEFINE_STATIC_KEY_FALSE(timers_nohz_active);
272ae67badaSThomas Gleixner static DEFINE_MUTEX(timer_keys_mutex);
273ae67badaSThomas Gleixner
274ae67badaSThomas Gleixner static void timer_update_keys(struct work_struct *work);
275ae67badaSThomas Gleixner static DECLARE_WORK(timer_update_work, timer_update_keys);
276ae67badaSThomas Gleixner
277ae67badaSThomas Gleixner #ifdef CONFIG_SMP
278efaa0227Stangmeng static unsigned int sysctl_timer_migration = 1;
279bc7a34b8SThomas Gleixner
280ae67badaSThomas Gleixner DEFINE_STATIC_KEY_FALSE(timers_migration_enabled);
281ae67badaSThomas Gleixner
timers_update_migration(void)282ae67badaSThomas Gleixner static void timers_update_migration(void)
283bc7a34b8SThomas Gleixner {
284ae67badaSThomas Gleixner if (sysctl_timer_migration && tick_nohz_active)
285ae67badaSThomas Gleixner static_branch_enable(&timers_migration_enabled);
286ae67badaSThomas Gleixner else
287ae67badaSThomas Gleixner static_branch_disable(&timers_migration_enabled);
288bc7a34b8SThomas Gleixner }
289efaa0227Stangmeng
290efaa0227Stangmeng #ifdef CONFIG_SYSCTL
timer_migration_handler(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)29178eb4ea2SJoel Granados static int timer_migration_handler(const struct ctl_table *table, int write,
292efaa0227Stangmeng void *buffer, size_t *lenp, loff_t *ppos)
293efaa0227Stangmeng {
294efaa0227Stangmeng int ret;
295efaa0227Stangmeng
296efaa0227Stangmeng mutex_lock(&timer_keys_mutex);
297efaa0227Stangmeng ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
298efaa0227Stangmeng if (!ret && write)
299efaa0227Stangmeng timers_update_migration();
300efaa0227Stangmeng mutex_unlock(&timer_keys_mutex);
301efaa0227Stangmeng return ret;
302efaa0227Stangmeng }
303efaa0227Stangmeng
3041751f872SJoel Granados static const struct ctl_table timer_sysctl[] = {
305efaa0227Stangmeng {
306efaa0227Stangmeng .procname = "timer_migration",
307efaa0227Stangmeng .data = &sysctl_timer_migration,
308efaa0227Stangmeng .maxlen = sizeof(unsigned int),
309efaa0227Stangmeng .mode = 0644,
310efaa0227Stangmeng .proc_handler = timer_migration_handler,
311efaa0227Stangmeng .extra1 = SYSCTL_ZERO,
312efaa0227Stangmeng .extra2 = SYSCTL_ONE,
313efaa0227Stangmeng },
314efaa0227Stangmeng };
315efaa0227Stangmeng
timer_sysctl_init(void)316efaa0227Stangmeng static int __init timer_sysctl_init(void)
317efaa0227Stangmeng {
318efaa0227Stangmeng register_sysctl("kernel", timer_sysctl);
319efaa0227Stangmeng return 0;
320efaa0227Stangmeng }
321efaa0227Stangmeng device_initcall(timer_sysctl_init);
322efaa0227Stangmeng #endif /* CONFIG_SYSCTL */
323efaa0227Stangmeng #else /* CONFIG_SMP */
timers_update_migration(void)324ae67badaSThomas Gleixner static inline void timers_update_migration(void) { }
325ae67badaSThomas Gleixner #endif /* !CONFIG_SMP */
326ae67badaSThomas Gleixner
timer_update_keys(struct work_struct * work)327ae67badaSThomas Gleixner static void timer_update_keys(struct work_struct *work)
328ae67badaSThomas Gleixner {
329ae67badaSThomas Gleixner mutex_lock(&timer_keys_mutex);
330ae67badaSThomas Gleixner timers_update_migration();
331ae67badaSThomas Gleixner static_branch_enable(&timers_nohz_active);
332ae67badaSThomas Gleixner mutex_unlock(&timer_keys_mutex);
333ae67badaSThomas Gleixner }
334ae67badaSThomas Gleixner
timers_update_nohz(void)335ae67badaSThomas Gleixner void timers_update_nohz(void)
336ae67badaSThomas Gleixner {
337ae67badaSThomas Gleixner schedule_work(&timer_update_work);
338bc7a34b8SThomas Gleixner }
339bc7a34b8SThomas Gleixner
is_timers_nohz_active(void)34014c80341SAnna-Maria Gleixner static inline bool is_timers_nohz_active(void)
34114c80341SAnna-Maria Gleixner {
34214c80341SAnna-Maria Gleixner return static_branch_unlikely(&timers_nohz_active);
34314c80341SAnna-Maria Gleixner }
34414c80341SAnna-Maria Gleixner #else
is_timers_nohz_active(void)34514c80341SAnna-Maria Gleixner static inline bool is_timers_nohz_active(void) { return false; }
346ae67badaSThomas Gleixner #endif /* NO_HZ_COMMON */
347bc7a34b8SThomas Gleixner
round_jiffies_common(unsigned long j,int cpu,bool force_up)3485cee9645SThomas Gleixner static unsigned long round_jiffies_common(unsigned long j, int cpu,
3495cee9645SThomas Gleixner bool force_up)
3505cee9645SThomas Gleixner {
3515cee9645SThomas Gleixner int rem;
3525cee9645SThomas Gleixner unsigned long original = j;
3535cee9645SThomas Gleixner
3545cee9645SThomas Gleixner /*
3555cee9645SThomas Gleixner * We don't want all cpus firing their timers at once hitting the
3565cee9645SThomas Gleixner * same lock or cachelines, so we skew each extra cpu with an extra
3575cee9645SThomas Gleixner * 3 jiffies. This 3 jiffies came originally from the mm/ code which
3585cee9645SThomas Gleixner * already did this.
3595cee9645SThomas Gleixner * The skew is done by adding 3*cpunr, then round, then subtract this
3605cee9645SThomas Gleixner * extra offset again.
3615cee9645SThomas Gleixner */
3625cee9645SThomas Gleixner j += cpu * 3;
3635cee9645SThomas Gleixner
3645cee9645SThomas Gleixner rem = j % HZ;
3655cee9645SThomas Gleixner
3665cee9645SThomas Gleixner /*
367bd7c8ff9SAnna-Maria Behnsen * If the target jiffy is just after a whole second (which can happen
3685cee9645SThomas Gleixner * due to delays of the timer irq, long irq off times etc etc) then
3695cee9645SThomas Gleixner * we should round down to the whole second, not up. Use 1/4th second
3705cee9645SThomas Gleixner * as cutoff for this rounding as an extreme upper bound for this.
3715cee9645SThomas Gleixner * But never round down if @force_up is set.
3725cee9645SThomas Gleixner */
3735cee9645SThomas Gleixner if (rem < HZ/4 && !force_up) /* round down */
3745cee9645SThomas Gleixner j = j - rem;
3755cee9645SThomas Gleixner else /* round up */
3765cee9645SThomas Gleixner j = j - rem + HZ;
3775cee9645SThomas Gleixner
3785cee9645SThomas Gleixner /* now that we have rounded, subtract the extra skew again */
3795cee9645SThomas Gleixner j -= cpu * 3;
3805cee9645SThomas Gleixner
3815cee9645SThomas Gleixner /*
3825cee9645SThomas Gleixner * Make sure j is still in the future. Otherwise return the
3835cee9645SThomas Gleixner * unmodified value.
3845cee9645SThomas Gleixner */
3855cee9645SThomas Gleixner return time_is_after_jiffies(j) ? j : original;
3865cee9645SThomas Gleixner }
3875cee9645SThomas Gleixner
3885cee9645SThomas Gleixner /**
3895cee9645SThomas Gleixner * __round_jiffies - function to round jiffies to a full second
3905cee9645SThomas Gleixner * @j: the time in (absolute) jiffies that should be rounded
3915cee9645SThomas Gleixner * @cpu: the processor number on which the timeout will happen
3925cee9645SThomas Gleixner *
3935cee9645SThomas Gleixner * __round_jiffies() rounds an absolute time in the future (in jiffies)
3945cee9645SThomas Gleixner * up or down to (approximately) full seconds. This is useful for timers
3955cee9645SThomas Gleixner * for which the exact time they fire does not matter too much, as long as
3965cee9645SThomas Gleixner * they fire approximately every X seconds.
3975cee9645SThomas Gleixner *
3985cee9645SThomas Gleixner * By rounding these timers to whole seconds, all such timers will fire
3995cee9645SThomas Gleixner * at the same time, rather than at various times spread out. The goal
4005cee9645SThomas Gleixner * of this is to have the CPU wake up less, which saves power.
4015cee9645SThomas Gleixner *
4025cee9645SThomas Gleixner * The exact rounding is skewed for each processor to avoid all
4035cee9645SThomas Gleixner * processors firing at the exact same time, which could lead
4045cee9645SThomas Gleixner * to lock contention or spurious cache line bouncing.
4055cee9645SThomas Gleixner *
4065cee9645SThomas Gleixner * The return value is the rounded version of the @j parameter.
4075cee9645SThomas Gleixner */
__round_jiffies(unsigned long j,int cpu)4085cee9645SThomas Gleixner unsigned long __round_jiffies(unsigned long j, int cpu)
4095cee9645SThomas Gleixner {
4105cee9645SThomas Gleixner return round_jiffies_common(j, cpu, false);
4115cee9645SThomas Gleixner }
4125cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies);
4135cee9645SThomas Gleixner
4145cee9645SThomas Gleixner /**
4155cee9645SThomas Gleixner * __round_jiffies_relative - function to round jiffies to a full second
4165cee9645SThomas Gleixner * @j: the time in (relative) jiffies that should be rounded
4175cee9645SThomas Gleixner * @cpu: the processor number on which the timeout will happen
4185cee9645SThomas Gleixner *
4195cee9645SThomas Gleixner * __round_jiffies_relative() rounds a time delta in the future (in jiffies)
4205cee9645SThomas Gleixner * up or down to (approximately) full seconds. This is useful for timers
4215cee9645SThomas Gleixner * for which the exact time they fire does not matter too much, as long as
4225cee9645SThomas Gleixner * they fire approximately every X seconds.
4235cee9645SThomas Gleixner *
4245cee9645SThomas Gleixner * By rounding these timers to whole seconds, all such timers will fire
4255cee9645SThomas Gleixner * at the same time, rather than at various times spread out. The goal
4265cee9645SThomas Gleixner * of this is to have the CPU wake up less, which saves power.
4275cee9645SThomas Gleixner *
4285cee9645SThomas Gleixner * The exact rounding is skewed for each processor to avoid all
4295cee9645SThomas Gleixner * processors firing at the exact same time, which could lead
4305cee9645SThomas Gleixner * to lock contention or spurious cache line bouncing.
4315cee9645SThomas Gleixner *
4325cee9645SThomas Gleixner * The return value is the rounded version of the @j parameter.
4335cee9645SThomas Gleixner */
__round_jiffies_relative(unsigned long j,int cpu)4345cee9645SThomas Gleixner unsigned long __round_jiffies_relative(unsigned long j, int cpu)
4355cee9645SThomas Gleixner {
4365cee9645SThomas Gleixner unsigned long j0 = jiffies;
4375cee9645SThomas Gleixner
4385cee9645SThomas Gleixner /* Use j0 because jiffies might change while we run */
4395cee9645SThomas Gleixner return round_jiffies_common(j + j0, cpu, false) - j0;
4405cee9645SThomas Gleixner }
4415cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_relative);
4425cee9645SThomas Gleixner
4435cee9645SThomas Gleixner /**
4445cee9645SThomas Gleixner * round_jiffies - function to round jiffies to a full second
4455cee9645SThomas Gleixner * @j: the time in (absolute) jiffies that should be rounded
4465cee9645SThomas Gleixner *
4475cee9645SThomas Gleixner * round_jiffies() rounds an absolute time in the future (in jiffies)
4485cee9645SThomas Gleixner * up or down to (approximately) full seconds. This is useful for timers
4495cee9645SThomas Gleixner * for which the exact time they fire does not matter too much, as long as
4505cee9645SThomas Gleixner * they fire approximately every X seconds.
4515cee9645SThomas Gleixner *
4525cee9645SThomas Gleixner * By rounding these timers to whole seconds, all such timers will fire
4535cee9645SThomas Gleixner * at the same time, rather than at various times spread out. The goal
4545cee9645SThomas Gleixner * of this is to have the CPU wake up less, which saves power.
4555cee9645SThomas Gleixner *
4565cee9645SThomas Gleixner * The return value is the rounded version of the @j parameter.
4575cee9645SThomas Gleixner */
round_jiffies(unsigned long j)4585cee9645SThomas Gleixner unsigned long round_jiffies(unsigned long j)
4595cee9645SThomas Gleixner {
4605cee9645SThomas Gleixner return round_jiffies_common(j, raw_smp_processor_id(), false);
4615cee9645SThomas Gleixner }
4625cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies);
4635cee9645SThomas Gleixner
4645cee9645SThomas Gleixner /**
4655cee9645SThomas Gleixner * round_jiffies_relative - function to round jiffies to a full second
4665cee9645SThomas Gleixner * @j: the time in (relative) jiffies that should be rounded
4675cee9645SThomas Gleixner *
4685cee9645SThomas Gleixner * round_jiffies_relative() rounds a time delta in the future (in jiffies)
4695cee9645SThomas Gleixner * up or down to (approximately) full seconds. This is useful for timers
4705cee9645SThomas Gleixner * for which the exact time they fire does not matter too much, as long as
4715cee9645SThomas Gleixner * they fire approximately every X seconds.
4725cee9645SThomas Gleixner *
4735cee9645SThomas Gleixner * By rounding these timers to whole seconds, all such timers will fire
4745cee9645SThomas Gleixner * at the same time, rather than at various times spread out. The goal
4755cee9645SThomas Gleixner * of this is to have the CPU wake up less, which saves power.
4765cee9645SThomas Gleixner *
4775cee9645SThomas Gleixner * The return value is the rounded version of the @j parameter.
4785cee9645SThomas Gleixner */
round_jiffies_relative(unsigned long j)4795cee9645SThomas Gleixner unsigned long round_jiffies_relative(unsigned long j)
4805cee9645SThomas Gleixner {
4815cee9645SThomas Gleixner return __round_jiffies_relative(j, raw_smp_processor_id());
4825cee9645SThomas Gleixner }
4835cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_relative);
4845cee9645SThomas Gleixner
4855cee9645SThomas Gleixner /**
4865cee9645SThomas Gleixner * __round_jiffies_up - function to round jiffies up to a full second
4875cee9645SThomas Gleixner * @j: the time in (absolute) jiffies that should be rounded
4885cee9645SThomas Gleixner * @cpu: the processor number on which the timeout will happen
4895cee9645SThomas Gleixner *
4905cee9645SThomas Gleixner * This is the same as __round_jiffies() except that it will never
4915cee9645SThomas Gleixner * round down. This is useful for timeouts for which the exact time
4925cee9645SThomas Gleixner * of firing does not matter too much, as long as they don't fire too
4935cee9645SThomas Gleixner * early.
4945cee9645SThomas Gleixner */
__round_jiffies_up(unsigned long j,int cpu)4955cee9645SThomas Gleixner unsigned long __round_jiffies_up(unsigned long j, int cpu)
4965cee9645SThomas Gleixner {
4975cee9645SThomas Gleixner return round_jiffies_common(j, cpu, true);
4985cee9645SThomas Gleixner }
4995cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up);
5005cee9645SThomas Gleixner
5015cee9645SThomas Gleixner /**
5025cee9645SThomas Gleixner * __round_jiffies_up_relative - function to round jiffies up to a full second
5035cee9645SThomas Gleixner * @j: the time in (relative) jiffies that should be rounded
5045cee9645SThomas Gleixner * @cpu: the processor number on which the timeout will happen
5055cee9645SThomas Gleixner *
5065cee9645SThomas Gleixner * This is the same as __round_jiffies_relative() except that it will never
5075cee9645SThomas Gleixner * round down. This is useful for timeouts for which the exact time
5085cee9645SThomas Gleixner * of firing does not matter too much, as long as they don't fire too
5095cee9645SThomas Gleixner * early.
5105cee9645SThomas Gleixner */
__round_jiffies_up_relative(unsigned long j,int cpu)5115cee9645SThomas Gleixner unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
5125cee9645SThomas Gleixner {
5135cee9645SThomas Gleixner unsigned long j0 = jiffies;
5145cee9645SThomas Gleixner
5155cee9645SThomas Gleixner /* Use j0 because jiffies might change while we run */
5165cee9645SThomas Gleixner return round_jiffies_common(j + j0, cpu, true) - j0;
5175cee9645SThomas Gleixner }
5185cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
5195cee9645SThomas Gleixner
5205cee9645SThomas Gleixner /**
5215cee9645SThomas Gleixner * round_jiffies_up - function to round jiffies up to a full second
5225cee9645SThomas Gleixner * @j: the time in (absolute) jiffies that should be rounded
5235cee9645SThomas Gleixner *
5245cee9645SThomas Gleixner * This is the same as round_jiffies() except that it will never
5255cee9645SThomas Gleixner * round down. This is useful for timeouts for which the exact time
5265cee9645SThomas Gleixner * of firing does not matter too much, as long as they don't fire too
5275cee9645SThomas Gleixner * early.
5285cee9645SThomas Gleixner */
round_jiffies_up(unsigned long j)5295cee9645SThomas Gleixner unsigned long round_jiffies_up(unsigned long j)
5305cee9645SThomas Gleixner {
5315cee9645SThomas Gleixner return round_jiffies_common(j, raw_smp_processor_id(), true);
5325cee9645SThomas Gleixner }
5335cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up);
5345cee9645SThomas Gleixner
5355cee9645SThomas Gleixner /**
5365cee9645SThomas Gleixner * round_jiffies_up_relative - function to round jiffies up to a full second
5375cee9645SThomas Gleixner * @j: the time in (relative) jiffies that should be rounded
5385cee9645SThomas Gleixner *
5395cee9645SThomas Gleixner * This is the same as round_jiffies_relative() except that it will never
5405cee9645SThomas Gleixner * round down. This is useful for timeouts for which the exact time
5415cee9645SThomas Gleixner * of firing does not matter too much, as long as they don't fire too
5425cee9645SThomas Gleixner * early.
5435cee9645SThomas Gleixner */
round_jiffies_up_relative(unsigned long j)5445cee9645SThomas Gleixner unsigned long round_jiffies_up_relative(unsigned long j)
5455cee9645SThomas Gleixner {
5465cee9645SThomas Gleixner return __round_jiffies_up_relative(j, raw_smp_processor_id());
5475cee9645SThomas Gleixner }
5485cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
5495cee9645SThomas Gleixner
5505cee9645SThomas Gleixner
timer_get_idx(struct timer_list * timer)551500462a9SThomas Gleixner static inline unsigned int timer_get_idx(struct timer_list *timer)
5525cee9645SThomas Gleixner {
553500462a9SThomas Gleixner return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT;
5545cee9645SThomas Gleixner }
555500462a9SThomas Gleixner
timer_set_idx(struct timer_list * timer,unsigned int idx)556500462a9SThomas Gleixner static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
557500462a9SThomas Gleixner {
558500462a9SThomas Gleixner timer->flags = (timer->flags & ~TIMER_ARRAYMASK) |
559500462a9SThomas Gleixner idx << TIMER_ARRAYSHIFT;
560500462a9SThomas Gleixner }
561500462a9SThomas Gleixner
562500462a9SThomas Gleixner /*
563500462a9SThomas Gleixner * Helper function to calculate the array index for a given expiry
564500462a9SThomas Gleixner * time.
565500462a9SThomas Gleixner */
calc_index(unsigned long expires,unsigned lvl,unsigned long * bucket_expiry)5661f32cab0SAnna-Maria Behnsen static inline unsigned calc_index(unsigned long expires, unsigned lvl,
5671f32cab0SAnna-Maria Behnsen unsigned long *bucket_expiry)
568500462a9SThomas Gleixner {
56944688972SFrederic Weisbecker
57044688972SFrederic Weisbecker /*
57144688972SFrederic Weisbecker * The timer wheel has to guarantee that a timer does not fire
57244688972SFrederic Weisbecker * early. Early expiry can happen due to:
57344688972SFrederic Weisbecker * - Timer is armed at the edge of a tick
57444688972SFrederic Weisbecker * - Truncation of the expiry time in the outer wheel levels
57544688972SFrederic Weisbecker *
57644688972SFrederic Weisbecker * Round up with level granularity to prevent this.
57744688972SFrederic Weisbecker */
578a2026e44SThomas Gleixner expires = (expires >> LVL_SHIFT(lvl)) + 1;
5791f32cab0SAnna-Maria Behnsen *bucket_expiry = expires << LVL_SHIFT(lvl);
580500462a9SThomas Gleixner return LVL_OFFS(lvl) + (expires & LVL_MASK);
581500462a9SThomas Gleixner }
582500462a9SThomas Gleixner
calc_wheel_index(unsigned long expires,unsigned long clk,unsigned long * bucket_expiry)5831f32cab0SAnna-Maria Behnsen static int calc_wheel_index(unsigned long expires, unsigned long clk,
5841f32cab0SAnna-Maria Behnsen unsigned long *bucket_expiry)
5855cee9645SThomas Gleixner {
586ffdf0477SAnna-Maria Gleixner unsigned long delta = expires - clk;
587500462a9SThomas Gleixner unsigned int idx;
5885cee9645SThomas Gleixner
589500462a9SThomas Gleixner if (delta < LVL_START(1)) {
5901f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 0, bucket_expiry);
591500462a9SThomas Gleixner } else if (delta < LVL_START(2)) {
5921f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 1, bucket_expiry);
593500462a9SThomas Gleixner } else if (delta < LVL_START(3)) {
5941f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 2, bucket_expiry);
595500462a9SThomas Gleixner } else if (delta < LVL_START(4)) {
5961f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 3, bucket_expiry);
597500462a9SThomas Gleixner } else if (delta < LVL_START(5)) {
5981f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 4, bucket_expiry);
599500462a9SThomas Gleixner } else if (delta < LVL_START(6)) {
6001f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 5, bucket_expiry);
601500462a9SThomas Gleixner } else if (delta < LVL_START(7)) {
6021f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 6, bucket_expiry);
603500462a9SThomas Gleixner } else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
6041f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 7, bucket_expiry);
605500462a9SThomas Gleixner } else if ((long) delta < 0) {
606ffdf0477SAnna-Maria Gleixner idx = clk & LVL_MASK;
6071f32cab0SAnna-Maria Behnsen *bucket_expiry = clk;
6085cee9645SThomas Gleixner } else {
609500462a9SThomas Gleixner /*
610500462a9SThomas Gleixner * Force expire obscene large timeouts to expire at the
611500462a9SThomas Gleixner * capacity limit of the wheel.
6125cee9645SThomas Gleixner */
613e2a71bdeSFrederic Weisbecker if (delta >= WHEEL_TIMEOUT_CUTOFF)
614e2a71bdeSFrederic Weisbecker expires = clk + WHEEL_TIMEOUT_MAX;
6151bd04bf6SThomas Gleixner
6161f32cab0SAnna-Maria Behnsen idx = calc_index(expires, LVL_DEPTH - 1, bucket_expiry);
617500462a9SThomas Gleixner }
618ffdf0477SAnna-Maria Gleixner return idx;
619ffdf0477SAnna-Maria Gleixner }
620ffdf0477SAnna-Maria Gleixner
621ffdf0477SAnna-Maria Gleixner static void
trigger_dyntick_cpu(struct timer_base * base,struct timer_list * timer)622ffdf0477SAnna-Maria Gleixner trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
6235cee9645SThomas Gleixner {
6245cee9645SThomas Gleixner /*
625d124c339SAnna-Maria Behnsen * Deferrable timers do not prevent the CPU from entering dynticks and
626d124c339SAnna-Maria Behnsen * are not taken into account on the idle/nohz_full path. An IPI when a
627d124c339SAnna-Maria Behnsen * new deferrable timer is enqueued will wake up the remote CPU but
628d124c339SAnna-Maria Behnsen * nothing will be done with the deferrable timer base. Therefore skip
629d124c339SAnna-Maria Behnsen * the remote IPI for deferrable timers completely.
6305cee9645SThomas Gleixner */
631d124c339SAnna-Maria Behnsen if (!is_timers_nohz_active() || timer->flags & TIMER_DEFERRABLE)
632a683f390SThomas Gleixner return;
6339f6d9baaSViresh Kumar
6349f6d9baaSViresh Kumar /*
635a683f390SThomas Gleixner * We might have to IPI the remote CPU if the base is idle and the
636b2cf7507SAnna-Maria Behnsen * timer is pinned. If it is a non pinned timer, it is only queued
637b2cf7507SAnna-Maria Behnsen * on the remote CPU, when timer was running during queueing. Then
638b2cf7507SAnna-Maria Behnsen * everything is handled by remote CPU anyway. If the other CPU is
639b2cf7507SAnna-Maria Behnsen * on the way to idle then it can't set base->is_idle as we hold
640b2cf7507SAnna-Maria Behnsen * the base lock:
6419f6d9baaSViresh Kumar */
642b2cf7507SAnna-Maria Behnsen if (base->is_idle) {
64303877039SFrederic Weisbecker WARN_ON_ONCE(!(timer->flags & TIMER_PINNED ||
64403877039SFrederic Weisbecker tick_nohz_full_cpu(base->cpu)));
6459f6d9baaSViresh Kumar wake_up_nohz_cpu(base->cpu);
6465cee9645SThomas Gleixner }
647b2cf7507SAnna-Maria Behnsen }
6485cee9645SThomas Gleixner
6499a2b764bSFrederic Weisbecker /*
6509a2b764bSFrederic Weisbecker * Enqueue the timer into the hash bucket, mark it pending in
6519a2b764bSFrederic Weisbecker * the bitmap, store the index in the timer flags then wake up
6529a2b764bSFrederic Weisbecker * the target CPU if needed.
6539a2b764bSFrederic Weisbecker */
enqueue_timer(struct timer_base * base,struct timer_list * timer,unsigned int idx,unsigned long bucket_expiry)6549a2b764bSFrederic Weisbecker static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
6559a2b764bSFrederic Weisbecker unsigned int idx, unsigned long bucket_expiry)
656ffdf0477SAnna-Maria Gleixner {
657dc2a0f1fSFrederic Weisbecker
6589a2b764bSFrederic Weisbecker hlist_add_head(&timer->entry, base->vectors + idx);
6599a2b764bSFrederic Weisbecker __set_bit(idx, base->pending_map);
6609a2b764bSFrederic Weisbecker timer_set_idx(timer, idx);
6619a2b764bSFrederic Weisbecker
662dbcdcb62SAnna-Maria Behnsen trace_timer_start(timer, bucket_expiry);
663dc2a0f1fSFrederic Weisbecker
664dc2a0f1fSFrederic Weisbecker /*
665dc2a0f1fSFrederic Weisbecker * Check whether this is the new first expiring timer. The
666dc2a0f1fSFrederic Weisbecker * effective expiry time of the timer is required here
667dc2a0f1fSFrederic Weisbecker * (bucket_expiry) instead of timer->expires.
668dc2a0f1fSFrederic Weisbecker */
669dc2a0f1fSFrederic Weisbecker if (time_before(bucket_expiry, base->next_expiry)) {
670dc2a0f1fSFrederic Weisbecker /*
671dc2a0f1fSFrederic Weisbecker * Set the next expiry time and kick the CPU so it
672dc2a0f1fSFrederic Weisbecker * can reevaluate the wheel:
673dc2a0f1fSFrederic Weisbecker */
67479f8b28eSAnna-Maria Behnsen WRITE_ONCE(base->next_expiry, bucket_expiry);
675aebacb7fSNicolas Saenz Julienne base->timers_pending = true;
67631cd0e11SFrederic Weisbecker base->next_expiry_recalc = false;
677ffdf0477SAnna-Maria Gleixner trigger_dyntick_cpu(base, timer);
6785cee9645SThomas Gleixner }
6799a2b764bSFrederic Weisbecker }
6809a2b764bSFrederic Weisbecker
internal_add_timer(struct timer_base * base,struct timer_list * timer)6819a2b764bSFrederic Weisbecker static void internal_add_timer(struct timer_base *base, struct timer_list *timer)
6825cee9645SThomas Gleixner {
6831f32cab0SAnna-Maria Behnsen unsigned long bucket_expiry;
6849a2b764bSFrederic Weisbecker unsigned int idx;
6851f32cab0SAnna-Maria Behnsen
6869a2b764bSFrederic Weisbecker idx = calc_wheel_index(timer->expires, base->clk, &bucket_expiry);
6879a2b764bSFrederic Weisbecker enqueue_timer(base, timer, idx, bucket_expiry);
6885cee9645SThomas Gleixner }
6895cee9645SThomas Gleixner
6905cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
6915cee9645SThomas Gleixner
692f9e62f31SStephen Boyd static const struct debug_obj_descr timer_debug_descr;
6935cee9645SThomas Gleixner
694317f29c1SStephen Boyd struct timer_hint {
695317f29c1SStephen Boyd void (*function)(struct timer_list *t);
696317f29c1SStephen Boyd long offset;
697317f29c1SStephen Boyd };
698317f29c1SStephen Boyd
699317f29c1SStephen Boyd #define TIMER_HINT(fn, container, timr, hintfn) \
700317f29c1SStephen Boyd { \
701317f29c1SStephen Boyd .function = fn, \
702317f29c1SStephen Boyd .offset = offsetof(container, hintfn) - \
703317f29c1SStephen Boyd offsetof(container, timr) \
704317f29c1SStephen Boyd }
705317f29c1SStephen Boyd
706317f29c1SStephen Boyd static const struct timer_hint timer_hints[] = {
707317f29c1SStephen Boyd TIMER_HINT(delayed_work_timer_fn,
708317f29c1SStephen Boyd struct delayed_work, timer, work.func),
709317f29c1SStephen Boyd TIMER_HINT(kthread_delayed_work_timer_fn,
710317f29c1SStephen Boyd struct kthread_delayed_work, timer, work.func),
711317f29c1SStephen Boyd };
712317f29c1SStephen Boyd
timer_debug_hint(void * addr)7135cee9645SThomas Gleixner static void *timer_debug_hint(void *addr)
7145cee9645SThomas Gleixner {
715317f29c1SStephen Boyd struct timer_list *timer = addr;
716317f29c1SStephen Boyd int i;
717317f29c1SStephen Boyd
718317f29c1SStephen Boyd for (i = 0; i < ARRAY_SIZE(timer_hints); i++) {
719317f29c1SStephen Boyd if (timer_hints[i].function == timer->function) {
720317f29c1SStephen Boyd void (**fn)(void) = addr + timer_hints[i].offset;
721317f29c1SStephen Boyd
722317f29c1SStephen Boyd return *fn;
723317f29c1SStephen Boyd }
724317f29c1SStephen Boyd }
725317f29c1SStephen Boyd
726317f29c1SStephen Boyd return timer->function;
7275cee9645SThomas Gleixner }
7285cee9645SThomas Gleixner
timer_is_static_object(void * addr)729b9fdac7fSDu, Changbin static bool timer_is_static_object(void *addr)
730b9fdac7fSDu, Changbin {
731b9fdac7fSDu, Changbin struct timer_list *timer = addr;
732b9fdac7fSDu, Changbin
733b9fdac7fSDu, Changbin return (timer->entry.pprev == NULL &&
734b9fdac7fSDu, Changbin timer->entry.next == TIMER_ENTRY_STATIC);
735b9fdac7fSDu, Changbin }
736b9fdac7fSDu, Changbin
7375cee9645SThomas Gleixner /*
7389e643ab5SRandy Dunlap * timer_fixup_init is called when:
7395cee9645SThomas Gleixner * - an active object is initialized
7405cee9645SThomas Gleixner */
timer_fixup_init(void * addr,enum debug_obj_state state)741e3252464SDu, Changbin static bool timer_fixup_init(void *addr, enum debug_obj_state state)
7425cee9645SThomas Gleixner {
7435cee9645SThomas Gleixner struct timer_list *timer = addr;
7445cee9645SThomas Gleixner
7455cee9645SThomas Gleixner switch (state) {
7465cee9645SThomas Gleixner case ODEBUG_STATE_ACTIVE:
747*8fa7292fSThomas Gleixner timer_delete_sync(timer);
7485cee9645SThomas Gleixner debug_object_init(timer, &timer_debug_descr);
749e3252464SDu, Changbin return true;
7505cee9645SThomas Gleixner default:
751e3252464SDu, Changbin return false;
7525cee9645SThomas Gleixner }
7535cee9645SThomas Gleixner }
7545cee9645SThomas Gleixner
7555cee9645SThomas Gleixner /* Stub timer callback for improperly used timers. */
stub_timer(struct timer_list * unused)756ba16490eSThomas Gleixner static void stub_timer(struct timer_list *unused)
7575cee9645SThomas Gleixner {
7585cee9645SThomas Gleixner WARN_ON(1);
7595cee9645SThomas Gleixner }
7605cee9645SThomas Gleixner
7615cee9645SThomas Gleixner /*
7629e643ab5SRandy Dunlap * timer_fixup_activate is called when:
7635cee9645SThomas Gleixner * - an active object is activated
764b9fdac7fSDu, Changbin * - an unknown non-static object is activated
7655cee9645SThomas Gleixner */
timer_fixup_activate(void * addr,enum debug_obj_state state)766e3252464SDu, Changbin static bool timer_fixup_activate(void *addr, enum debug_obj_state state)
7675cee9645SThomas Gleixner {
7685cee9645SThomas Gleixner struct timer_list *timer = addr;
7695cee9645SThomas Gleixner
7705cee9645SThomas Gleixner switch (state) {
7715cee9645SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE:
772ba16490eSThomas Gleixner timer_setup(timer, stub_timer, 0);
773e3252464SDu, Changbin return true;
7745cee9645SThomas Gleixner
7755cee9645SThomas Gleixner case ODEBUG_STATE_ACTIVE:
7765cee9645SThomas Gleixner WARN_ON(1);
777df561f66SGustavo A. R. Silva fallthrough;
7785cee9645SThomas Gleixner default:
779e3252464SDu, Changbin return false;
7805cee9645SThomas Gleixner }
7815cee9645SThomas Gleixner }
7825cee9645SThomas Gleixner
7835cee9645SThomas Gleixner /*
7849e643ab5SRandy Dunlap * timer_fixup_free is called when:
7855cee9645SThomas Gleixner * - an active object is freed
7865cee9645SThomas Gleixner */
timer_fixup_free(void * addr,enum debug_obj_state state)787e3252464SDu, Changbin static bool timer_fixup_free(void *addr, enum debug_obj_state state)
7885cee9645SThomas Gleixner {
7895cee9645SThomas Gleixner struct timer_list *timer = addr;
7905cee9645SThomas Gleixner
7915cee9645SThomas Gleixner switch (state) {
7925cee9645SThomas Gleixner case ODEBUG_STATE_ACTIVE:
793*8fa7292fSThomas Gleixner timer_delete_sync(timer);
7945cee9645SThomas Gleixner debug_object_free(timer, &timer_debug_descr);
795e3252464SDu, Changbin return true;
7965cee9645SThomas Gleixner default:
797e3252464SDu, Changbin return false;
7985cee9645SThomas Gleixner }
7995cee9645SThomas Gleixner }
8005cee9645SThomas Gleixner
8015cee9645SThomas Gleixner /*
8029e643ab5SRandy Dunlap * timer_fixup_assert_init is called when:
8035cee9645SThomas Gleixner * - an untracked/uninit-ed object is found
8045cee9645SThomas Gleixner */
timer_fixup_assert_init(void * addr,enum debug_obj_state state)805e3252464SDu, Changbin static bool timer_fixup_assert_init(void *addr, enum debug_obj_state state)
8065cee9645SThomas Gleixner {
8075cee9645SThomas Gleixner struct timer_list *timer = addr;
8085cee9645SThomas Gleixner
8095cee9645SThomas Gleixner switch (state) {
8105cee9645SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE:
811ba16490eSThomas Gleixner timer_setup(timer, stub_timer, 0);
812e3252464SDu, Changbin return true;
8135cee9645SThomas Gleixner default:
814e3252464SDu, Changbin return false;
8155cee9645SThomas Gleixner }
8165cee9645SThomas Gleixner }
8175cee9645SThomas Gleixner
818f9e62f31SStephen Boyd static const struct debug_obj_descr timer_debug_descr = {
8195cee9645SThomas Gleixner .name = "timer_list",
8205cee9645SThomas Gleixner .debug_hint = timer_debug_hint,
821b9fdac7fSDu, Changbin .is_static_object = timer_is_static_object,
8225cee9645SThomas Gleixner .fixup_init = timer_fixup_init,
8235cee9645SThomas Gleixner .fixup_activate = timer_fixup_activate,
8245cee9645SThomas Gleixner .fixup_free = timer_fixup_free,
8255cee9645SThomas Gleixner .fixup_assert_init = timer_fixup_assert_init,
8265cee9645SThomas Gleixner };
8275cee9645SThomas Gleixner
debug_timer_init(struct timer_list * timer)8285cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer)
8295cee9645SThomas Gleixner {
8305cee9645SThomas Gleixner debug_object_init(timer, &timer_debug_descr);
8315cee9645SThomas Gleixner }
8325cee9645SThomas Gleixner
debug_timer_activate(struct timer_list * timer)8335cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer)
8345cee9645SThomas Gleixner {
8355cee9645SThomas Gleixner debug_object_activate(timer, &timer_debug_descr);
8365cee9645SThomas Gleixner }
8375cee9645SThomas Gleixner
debug_timer_deactivate(struct timer_list * timer)8385cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer)
8395cee9645SThomas Gleixner {
8405cee9645SThomas Gleixner debug_object_deactivate(timer, &timer_debug_descr);
8415cee9645SThomas Gleixner }
8425cee9645SThomas Gleixner
debug_timer_assert_init(struct timer_list * timer)8435cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer)
8445cee9645SThomas Gleixner {
8455cee9645SThomas Gleixner debug_object_assert_init(timer, &timer_debug_descr);
8465cee9645SThomas Gleixner }
8475cee9645SThomas Gleixner
848188665b2SKees Cook static void do_init_timer(struct timer_list *timer,
849188665b2SKees Cook void (*func)(struct timer_list *),
850188665b2SKees Cook unsigned int flags,
8515cee9645SThomas Gleixner const char *name, struct lock_class_key *key);
8525cee9645SThomas Gleixner
init_timer_on_stack_key(struct timer_list * timer,void (* func)(struct timer_list *),unsigned int flags,const char * name,struct lock_class_key * key)853188665b2SKees Cook void init_timer_on_stack_key(struct timer_list *timer,
854188665b2SKees Cook void (*func)(struct timer_list *),
855188665b2SKees Cook unsigned int flags,
8565cee9645SThomas Gleixner const char *name, struct lock_class_key *key)
8575cee9645SThomas Gleixner {
8585cee9645SThomas Gleixner debug_object_init_on_stack(timer, &timer_debug_descr);
859188665b2SKees Cook do_init_timer(timer, func, flags, name, key);
8605cee9645SThomas Gleixner }
8615cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
8625cee9645SThomas Gleixner
destroy_timer_on_stack(struct timer_list * timer)8635cee9645SThomas Gleixner void destroy_timer_on_stack(struct timer_list *timer)
8645cee9645SThomas Gleixner {
8655cee9645SThomas Gleixner debug_object_free(timer, &timer_debug_descr);
8665cee9645SThomas Gleixner }
8675cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
8685cee9645SThomas Gleixner
8695cee9645SThomas Gleixner #else
debug_timer_init(struct timer_list * timer)8705cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer) { }
debug_timer_activate(struct timer_list * timer)8715cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer) { }
debug_timer_deactivate(struct timer_list * timer)8725cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer) { }
debug_timer_assert_init(struct timer_list * timer)8735cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer) { }
8745cee9645SThomas Gleixner #endif
8755cee9645SThomas Gleixner
debug_init(struct timer_list * timer)8765cee9645SThomas Gleixner static inline void debug_init(struct timer_list *timer)
8775cee9645SThomas Gleixner {
8785cee9645SThomas Gleixner debug_timer_init(timer);
8795cee9645SThomas Gleixner trace_timer_init(timer);
8805cee9645SThomas Gleixner }
8815cee9645SThomas Gleixner
debug_deactivate(struct timer_list * timer)8825cee9645SThomas Gleixner static inline void debug_deactivate(struct timer_list *timer)
8835cee9645SThomas Gleixner {
8845cee9645SThomas Gleixner debug_timer_deactivate(timer);
8855cee9645SThomas Gleixner trace_timer_cancel(timer);
8865cee9645SThomas Gleixner }
8875cee9645SThomas Gleixner
debug_assert_init(struct timer_list * timer)8885cee9645SThomas Gleixner static inline void debug_assert_init(struct timer_list *timer)
8895cee9645SThomas Gleixner {
8905cee9645SThomas Gleixner debug_timer_assert_init(timer);
8915cee9645SThomas Gleixner }
8925cee9645SThomas Gleixner
do_init_timer(struct timer_list * timer,void (* func)(struct timer_list *),unsigned int flags,const char * name,struct lock_class_key * key)893188665b2SKees Cook static void do_init_timer(struct timer_list *timer,
894188665b2SKees Cook void (*func)(struct timer_list *),
895188665b2SKees Cook unsigned int flags,
8965cee9645SThomas Gleixner const char *name, struct lock_class_key *key)
8975cee9645SThomas Gleixner {
8981dabbcecSThomas Gleixner timer->entry.pprev = NULL;
899188665b2SKees Cook timer->function = func;
900b952caf2SQianli Zhao if (WARN_ON_ONCE(flags & ~TIMER_INIT_FLAGS))
901b952caf2SQianli Zhao flags &= TIMER_INIT_FLAGS;
9020eeda71bSThomas Gleixner timer->flags = flags | raw_smp_processor_id();
9035cee9645SThomas Gleixner lockdep_init_map(&timer->lockdep_map, name, key, 0);
9045cee9645SThomas Gleixner }
9055cee9645SThomas Gleixner
9065cee9645SThomas Gleixner /**
9075cee9645SThomas Gleixner * init_timer_key - initialize a timer
9085cee9645SThomas Gleixner * @timer: the timer to be initialized
909188665b2SKees Cook * @func: timer callback function
9105cee9645SThomas Gleixner * @flags: timer flags
9115cee9645SThomas Gleixner * @name: name of the timer
9125cee9645SThomas Gleixner * @key: lockdep class key of the fake lock used for tracking timer
9135cee9645SThomas Gleixner * sync lock dependencies
9145cee9645SThomas Gleixner *
9159e643ab5SRandy Dunlap * init_timer_key() must be done to a timer prior to calling *any* of the
9165cee9645SThomas Gleixner * other timer functions.
9175cee9645SThomas Gleixner */
init_timer_key(struct timer_list * timer,void (* func)(struct timer_list *),unsigned int flags,const char * name,struct lock_class_key * key)918188665b2SKees Cook void init_timer_key(struct timer_list *timer,
919188665b2SKees Cook void (*func)(struct timer_list *), unsigned int flags,
9205cee9645SThomas Gleixner const char *name, struct lock_class_key *key)
9215cee9645SThomas Gleixner {
9225cee9645SThomas Gleixner debug_init(timer);
923188665b2SKees Cook do_init_timer(timer, func, flags, name, key);
9245cee9645SThomas Gleixner }
9255cee9645SThomas Gleixner EXPORT_SYMBOL(init_timer_key);
9265cee9645SThomas Gleixner
detach_timer(struct timer_list * timer,bool clear_pending)9275cee9645SThomas Gleixner static inline void detach_timer(struct timer_list *timer, bool clear_pending)
9285cee9645SThomas Gleixner {
9291dabbcecSThomas Gleixner struct hlist_node *entry = &timer->entry;
9305cee9645SThomas Gleixner
9315cee9645SThomas Gleixner debug_deactivate(timer);
9325cee9645SThomas Gleixner
9331dabbcecSThomas Gleixner __hlist_del(entry);
9345cee9645SThomas Gleixner if (clear_pending)
9351dabbcecSThomas Gleixner entry->pprev = NULL;
9361dabbcecSThomas Gleixner entry->next = LIST_POISON2;
9375cee9645SThomas Gleixner }
9385cee9645SThomas Gleixner
detach_if_pending(struct timer_list * timer,struct timer_base * base,bool clear_pending)939494af3edSThomas Gleixner static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
9405cee9645SThomas Gleixner bool clear_pending)
9415cee9645SThomas Gleixner {
942500462a9SThomas Gleixner unsigned idx = timer_get_idx(timer);
943500462a9SThomas Gleixner
9445cee9645SThomas Gleixner if (!timer_pending(timer))
9455cee9645SThomas Gleixner return 0;
9465cee9645SThomas Gleixner
94731cd0e11SFrederic Weisbecker if (hlist_is_singular_node(&timer->entry, base->vectors + idx)) {
948500462a9SThomas Gleixner __clear_bit(idx, base->pending_map);
94931cd0e11SFrederic Weisbecker base->next_expiry_recalc = true;
95031cd0e11SFrederic Weisbecker }
951500462a9SThomas Gleixner
9525cee9645SThomas Gleixner detach_timer(timer, clear_pending);
9535cee9645SThomas Gleixner return 1;
9545cee9645SThomas Gleixner }
9555cee9645SThomas Gleixner
get_timer_cpu_base(u32 tflags,u32 cpu)956500462a9SThomas Gleixner static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
957500462a9SThomas Gleixner {
95883a665dcSAnna-Maria Behnsen int index = tflags & TIMER_PINNED ? BASE_LOCAL : BASE_GLOBAL;
959500462a9SThomas Gleixner
9605cee9645SThomas Gleixner /*
961ced6d5c1SAnna-Maria Gleixner * If the timer is deferrable and NO_HZ_COMMON is set then we need
962ced6d5c1SAnna-Maria Gleixner * to use the deferrable base.
963500462a9SThomas Gleixner */
964ced6d5c1SAnna-Maria Gleixner if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
9653ec95571SZhongqiu Han index = BASE_DEF;
9663ec95571SZhongqiu Han
9673ec95571SZhongqiu Han return per_cpu_ptr(&timer_bases[index], cpu);
968500462a9SThomas Gleixner }
969500462a9SThomas Gleixner
get_timer_this_cpu_base(u32 tflags)970500462a9SThomas Gleixner static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
971500462a9SThomas Gleixner {
97283a665dcSAnna-Maria Behnsen int index = tflags & TIMER_PINNED ? BASE_LOCAL : BASE_GLOBAL;
973500462a9SThomas Gleixner
974500462a9SThomas Gleixner /*
975ced6d5c1SAnna-Maria Gleixner * If the timer is deferrable and NO_HZ_COMMON is set then we need
976ced6d5c1SAnna-Maria Gleixner * to use the deferrable base.
977500462a9SThomas Gleixner */
978ced6d5c1SAnna-Maria Gleixner if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
9793ec95571SZhongqiu Han index = BASE_DEF;
9803ec95571SZhongqiu Han
9813ec95571SZhongqiu Han return this_cpu_ptr(&timer_bases[index]);
982500462a9SThomas Gleixner }
983500462a9SThomas Gleixner
get_timer_base(u32 tflags)984500462a9SThomas Gleixner static inline struct timer_base *get_timer_base(u32 tflags)
985500462a9SThomas Gleixner {
986500462a9SThomas Gleixner return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
987500462a9SThomas Gleixner }
988500462a9SThomas Gleixner
__forward_timer_base(struct timer_base * base,unsigned long basej)9891e490484SAnna-Maria Behnsen static inline void __forward_timer_base(struct timer_base *base,
9901e490484SAnna-Maria Behnsen unsigned long basej)
991a683f390SThomas Gleixner {
992a683f390SThomas Gleixner /*
9938a2c9c7eSAnna-Maria Behnsen * Check whether we can forward the base. We can only do that when
9948a2c9c7eSAnna-Maria Behnsen * @basej is past base->clk otherwise we might rewind base->clk.
995a683f390SThomas Gleixner */
9961e490484SAnna-Maria Behnsen if (time_before_eq(basej, base->clk))
997a683f390SThomas Gleixner return;
998a683f390SThomas Gleixner
999a683f390SThomas Gleixner /*
1000a683f390SThomas Gleixner * If the next expiry value is > jiffies, then we fast forward to
1001a683f390SThomas Gleixner * jiffies otherwise we forward to the next expiry value.
1002a683f390SThomas Gleixner */
10031e490484SAnna-Maria Behnsen if (time_after(base->next_expiry, basej)) {
10041e490484SAnna-Maria Behnsen base->clk = basej;
100530c66fc3SFrederic Weisbecker } else {
100630c66fc3SFrederic Weisbecker if (WARN_ON_ONCE(time_before(base->next_expiry, base->clk)))
100730c66fc3SFrederic Weisbecker return;
1008a683f390SThomas Gleixner base->clk = base->next_expiry;
100930c66fc3SFrederic Weisbecker }
10101e490484SAnna-Maria Behnsen
1011ae67badaSThomas Gleixner }
1012a683f390SThomas Gleixner
forward_timer_base(struct timer_base * base)10131e490484SAnna-Maria Behnsen static inline void forward_timer_base(struct timer_base *base)
10141e490484SAnna-Maria Behnsen {
10151e490484SAnna-Maria Behnsen __forward_timer_base(base, READ_ONCE(jiffies));
10161e490484SAnna-Maria Behnsen }
1017a683f390SThomas Gleixner
1018500462a9SThomas Gleixner /*
1019500462a9SThomas Gleixner * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
1020500462a9SThomas Gleixner * that all timers which are tied to this base are locked, and the base itself
1021500462a9SThomas Gleixner * is locked too.
10225cee9645SThomas Gleixner *
10235cee9645SThomas Gleixner * So __run_timers/migrate_timers can safely modify all timers which could
1024500462a9SThomas Gleixner * be found in the base->vectors array.
10255cee9645SThomas Gleixner *
1026500462a9SThomas Gleixner * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
1027500462a9SThomas Gleixner * to wait until the migration is done.
10285cee9645SThomas Gleixner */
lock_timer_base(struct timer_list * timer,unsigned long * flags)1029494af3edSThomas Gleixner static struct timer_base *lock_timer_base(struct timer_list *timer,
10305cee9645SThomas Gleixner unsigned long *flags)
10315cee9645SThomas Gleixner __acquires(timer->base->lock)
10325cee9645SThomas Gleixner {
10330eeda71bSThomas Gleixner for (;;) {
1034494af3edSThomas Gleixner struct timer_base *base;
1035b831275aSThomas Gleixner u32 tf;
1036b831275aSThomas Gleixner
1037b831275aSThomas Gleixner /*
1038b831275aSThomas Gleixner * We need to use READ_ONCE() here, otherwise the compiler
1039b831275aSThomas Gleixner * might re-read @tf between the check for TIMER_MIGRATING
1040b831275aSThomas Gleixner * and spin_lock().
1041b831275aSThomas Gleixner */
1042b831275aSThomas Gleixner tf = READ_ONCE(timer->flags);
10435cee9645SThomas Gleixner
10440eeda71bSThomas Gleixner if (!(tf & TIMER_MIGRATING)) {
1045500462a9SThomas Gleixner base = get_timer_base(tf);
10462287d866SSebastian Andrzej Siewior raw_spin_lock_irqsave(&base->lock, *flags);
10470eeda71bSThomas Gleixner if (timer->flags == tf)
10485cee9645SThomas Gleixner return base;
10492287d866SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&base->lock, *flags);
10505cee9645SThomas Gleixner }
10515cee9645SThomas Gleixner cpu_relax();
10525cee9645SThomas Gleixner }
10535cee9645SThomas Gleixner }
10545cee9645SThomas Gleixner
1055b24591e2SDavid Howells #define MOD_TIMER_PENDING_ONLY 0x01
1056b24591e2SDavid Howells #define MOD_TIMER_REDUCE 0x02
105790c01894SEric Dumazet #define MOD_TIMER_NOTPENDING 0x04
1058b24591e2SDavid Howells
10595cee9645SThomas Gleixner static inline int
__mod_timer(struct timer_list * timer,unsigned long expires,unsigned int options)1060b24591e2SDavid Howells __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options)
10615cee9645SThomas Gleixner {
10621f32cab0SAnna-Maria Behnsen unsigned long clk = 0, flags, bucket_expiry;
1063494af3edSThomas Gleixner struct timer_base *base, *new_base;
1064f00c0afdSAnna-Maria Gleixner unsigned int idx = UINT_MAX;
1065bc7a34b8SThomas Gleixner int ret = 0;
10665cee9645SThomas Gleixner
1067d02e382cSThomas Gleixner debug_assert_init(timer);
10684da9152aSThomas Gleixner
1069500462a9SThomas Gleixner /*
1070f00c0afdSAnna-Maria Gleixner * This is a common optimization triggered by the networking code - if
1071f00c0afdSAnna-Maria Gleixner * the timer is re-modified to have the same timeout or ends up in the
1072f00c0afdSAnna-Maria Gleixner * same array bucket then just return:
1073500462a9SThomas Gleixner */
107490c01894SEric Dumazet if (!(options & MOD_TIMER_NOTPENDING) && timer_pending(timer)) {
10752fe59f50SNicholas Piggin /*
10762fe59f50SNicholas Piggin * The downside of this optimization is that it can result in
10772fe59f50SNicholas Piggin * larger granularity than you would get from adding a new
10782fe59f50SNicholas Piggin * timer with this expiry.
10792fe59f50SNicholas Piggin */
1080b24591e2SDavid Howells long diff = timer->expires - expires;
1081b24591e2SDavid Howells
1082b24591e2SDavid Howells if (!diff)
1083b24591e2SDavid Howells return 1;
1084b24591e2SDavid Howells if (options & MOD_TIMER_REDUCE && diff <= 0)
1085500462a9SThomas Gleixner return 1;
1086f00c0afdSAnna-Maria Gleixner
10874da9152aSThomas Gleixner /*
10884da9152aSThomas Gleixner * We lock timer base and calculate the bucket index right
10894da9152aSThomas Gleixner * here. If the timer ends up in the same bucket, then we
10904da9152aSThomas Gleixner * just update the expiry time and avoid the whole
10914da9152aSThomas Gleixner * dequeue/enqueue dance.
10924da9152aSThomas Gleixner */
10934da9152aSThomas Gleixner base = lock_timer_base(timer, &flags);
1094d02e382cSThomas Gleixner /*
1095d02e382cSThomas Gleixner * Has @timer been shutdown? This needs to be evaluated
1096d02e382cSThomas Gleixner * while holding base lock to prevent a race against the
1097d02e382cSThomas Gleixner * shutdown code.
1098d02e382cSThomas Gleixner */
1099d02e382cSThomas Gleixner if (!timer->function)
1100d02e382cSThomas Gleixner goto out_unlock;
1101d02e382cSThomas Gleixner
11022fe59f50SNicholas Piggin forward_timer_base(base);
11034da9152aSThomas Gleixner
1104b24591e2SDavid Howells if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) &&
1105b24591e2SDavid Howells time_before_eq(timer->expires, expires)) {
1106b24591e2SDavid Howells ret = 1;
1107b24591e2SDavid Howells goto out_unlock;
1108b24591e2SDavid Howells }
1109b24591e2SDavid Howells
11104da9152aSThomas Gleixner clk = base->clk;
11111f32cab0SAnna-Maria Behnsen idx = calc_wheel_index(expires, clk, &bucket_expiry);
1112f00c0afdSAnna-Maria Gleixner
1113f00c0afdSAnna-Maria Gleixner /*
1114f00c0afdSAnna-Maria Gleixner * Retrieve and compare the array index of the pending
1115f00c0afdSAnna-Maria Gleixner * timer. If it matches set the expiry to the new value so a
1116f00c0afdSAnna-Maria Gleixner * subsequent call will exit in the expires check above.
1117f00c0afdSAnna-Maria Gleixner */
1118f00c0afdSAnna-Maria Gleixner if (idx == timer_get_idx(timer)) {
1119b24591e2SDavid Howells if (!(options & MOD_TIMER_REDUCE))
1120b24591e2SDavid Howells timer->expires = expires;
1121b24591e2SDavid Howells else if (time_after(timer->expires, expires))
1122f00c0afdSAnna-Maria Gleixner timer->expires = expires;
11234da9152aSThomas Gleixner ret = 1;
11244da9152aSThomas Gleixner goto out_unlock;
1125f00c0afdSAnna-Maria Gleixner }
11264da9152aSThomas Gleixner } else {
11274da9152aSThomas Gleixner base = lock_timer_base(timer, &flags);
1128d02e382cSThomas Gleixner /*
1129d02e382cSThomas Gleixner * Has @timer been shutdown? This needs to be evaluated
1130d02e382cSThomas Gleixner * while holding base lock to prevent a race against the
1131d02e382cSThomas Gleixner * shutdown code.
1132d02e382cSThomas Gleixner */
1133d02e382cSThomas Gleixner if (!timer->function)
1134d02e382cSThomas Gleixner goto out_unlock;
1135d02e382cSThomas Gleixner
11362fe59f50SNicholas Piggin forward_timer_base(base);
1137500462a9SThomas Gleixner }
1138500462a9SThomas Gleixner
11395cee9645SThomas Gleixner ret = detach_if_pending(timer, base, false);
1140b24591e2SDavid Howells if (!ret && (options & MOD_TIMER_PENDING_ONLY))
11415cee9645SThomas Gleixner goto out_unlock;
11425cee9645SThomas Gleixner
1143b2cf7507SAnna-Maria Behnsen new_base = get_timer_this_cpu_base(timer->flags);
11445cee9645SThomas Gleixner
11455cee9645SThomas Gleixner if (base != new_base) {
11465cee9645SThomas Gleixner /*
1147500462a9SThomas Gleixner * We are trying to schedule the timer on the new base.
11485cee9645SThomas Gleixner * However we can't change timer's base while it is running,
11499b13df3fSThomas Gleixner * otherwise timer_delete_sync() can't detect that the timer's
1150500462a9SThomas Gleixner * handler yet has not finished. This also guarantees that the
1151500462a9SThomas Gleixner * timer is serialized wrt itself.
11525cee9645SThomas Gleixner */
11535cee9645SThomas Gleixner if (likely(base->running_timer != timer)) {
11545cee9645SThomas Gleixner /* See the comment in lock_timer_base() */
11550eeda71bSThomas Gleixner timer->flags |= TIMER_MIGRATING;
11560eeda71bSThomas Gleixner
11572287d866SSebastian Andrzej Siewior raw_spin_unlock(&base->lock);
11585cee9645SThomas Gleixner base = new_base;
11592287d866SSebastian Andrzej Siewior raw_spin_lock(&base->lock);
1160d0023a14SEric Dumazet WRITE_ONCE(timer->flags,
1161d0023a14SEric Dumazet (timer->flags & ~TIMER_BASEMASK) | base->cpu);
11626bad6bccSThomas Gleixner forward_timer_base(base);
11632fe59f50SNicholas Piggin }
11642fe59f50SNicholas Piggin }
11656bad6bccSThomas Gleixner
1166dc1e7dc5SAnna-Maria Gleixner debug_timer_activate(timer);
1167fd45bb77SThomas Gleixner
11685cee9645SThomas Gleixner timer->expires = expires;
1169f00c0afdSAnna-Maria Gleixner /*
1170f00c0afdSAnna-Maria Gleixner * If 'idx' was calculated above and the base time did not advance
11714da9152aSThomas Gleixner * between calculating 'idx' and possibly switching the base, only
11729a2b764bSFrederic Weisbecker * enqueue_timer() is required. Otherwise we need to (re)calculate
11739a2b764bSFrederic Weisbecker * the wheel index via internal_add_timer().
1174f00c0afdSAnna-Maria Gleixner */
11759a2b764bSFrederic Weisbecker if (idx != UINT_MAX && clk == base->clk)
11769a2b764bSFrederic Weisbecker enqueue_timer(base, timer, idx, bucket_expiry);
11779a2b764bSFrederic Weisbecker else
11785cee9645SThomas Gleixner internal_add_timer(base, timer);
11795cee9645SThomas Gleixner
11805cee9645SThomas Gleixner out_unlock:
11812287d866SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&base->lock, flags);
11825cee9645SThomas Gleixner
11835cee9645SThomas Gleixner return ret;
11845cee9645SThomas Gleixner }
11855cee9645SThomas Gleixner
11865cee9645SThomas Gleixner /**
118714f043f1SThomas Gleixner * mod_timer_pending - Modify a pending timer's timeout
118814f043f1SThomas Gleixner * @timer: The pending timer to be modified
118914f043f1SThomas Gleixner * @expires: New absolute timeout in jiffies
11905cee9645SThomas Gleixner *
119114f043f1SThomas Gleixner * mod_timer_pending() is the same for pending timers as mod_timer(), but
119214f043f1SThomas Gleixner * will not activate inactive timers.
11935cee9645SThomas Gleixner *
1194d02e382cSThomas Gleixner * If @timer->function == NULL then the start operation is silently
1195d02e382cSThomas Gleixner * discarded.
1196d02e382cSThomas Gleixner *
119714f043f1SThomas Gleixner * Return:
1198d02e382cSThomas Gleixner * * %0 - The timer was inactive and not modified or was in
1199d02e382cSThomas Gleixner * shutdown state and the operation was discarded
120014f043f1SThomas Gleixner * * %1 - The timer was active and requeued to expire at @expires
12015cee9645SThomas Gleixner */
mod_timer_pending(struct timer_list * timer,unsigned long expires)12025cee9645SThomas Gleixner int mod_timer_pending(struct timer_list *timer, unsigned long expires)
12035cee9645SThomas Gleixner {
1204b24591e2SDavid Howells return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY);
12055cee9645SThomas Gleixner }
12065cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer_pending);
12075cee9645SThomas Gleixner
12085cee9645SThomas Gleixner /**
120914f043f1SThomas Gleixner * mod_timer - Modify a timer's timeout
121014f043f1SThomas Gleixner * @timer: The timer to be modified
121114f043f1SThomas Gleixner * @expires: New absolute timeout in jiffies
12125cee9645SThomas Gleixner *
12135cee9645SThomas Gleixner * mod_timer(timer, expires) is equivalent to:
12145cee9645SThomas Gleixner *
1215*8fa7292fSThomas Gleixner * timer_delete(timer); timer->expires = expires; add_timer(timer);
12165cee9645SThomas Gleixner *
121714f043f1SThomas Gleixner * mod_timer() is more efficient than the above open coded sequence. In
1218*8fa7292fSThomas Gleixner * case that the timer is inactive, the timer_delete() part is a NOP. The
121914f043f1SThomas Gleixner * timer is in any case activated with the new expiry time @expires.
122014f043f1SThomas Gleixner *
12215cee9645SThomas Gleixner * Note that if there are multiple unserialized concurrent users of the
12225cee9645SThomas Gleixner * same timer, then mod_timer() is the only safe way to modify the timeout,
12235cee9645SThomas Gleixner * since add_timer() cannot modify an already running timer.
12245cee9645SThomas Gleixner *
1225d02e382cSThomas Gleixner * If @timer->function == NULL then the start operation is silently
1226d02e382cSThomas Gleixner * discarded. In this case the return value is 0 and meaningless.
1227d02e382cSThomas Gleixner *
122814f043f1SThomas Gleixner * Return:
1229d02e382cSThomas Gleixner * * %0 - The timer was inactive and started or was in shutdown
1230d02e382cSThomas Gleixner * state and the operation was discarded
123114f043f1SThomas Gleixner * * %1 - The timer was active and requeued to expire at @expires or
123214f043f1SThomas Gleixner * the timer was active and not modified because @expires did
123314f043f1SThomas Gleixner * not change the effective expiry time
12345cee9645SThomas Gleixner */
mod_timer(struct timer_list * timer,unsigned long expires)12355cee9645SThomas Gleixner int mod_timer(struct timer_list *timer, unsigned long expires)
12365cee9645SThomas Gleixner {
1237b24591e2SDavid Howells return __mod_timer(timer, expires, 0);
12385cee9645SThomas Gleixner }
12395cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer);
12405cee9645SThomas Gleixner
12415cee9645SThomas Gleixner /**
1242b24591e2SDavid Howells * timer_reduce - Modify a timer's timeout if it would reduce the timeout
1243b24591e2SDavid Howells * @timer: The timer to be modified
124414f043f1SThomas Gleixner * @expires: New absolute timeout in jiffies
1245b24591e2SDavid Howells *
1246b24591e2SDavid Howells * timer_reduce() is very similar to mod_timer(), except that it will only
124714f043f1SThomas Gleixner * modify an enqueued timer if that would reduce the expiration time. If
124814f043f1SThomas Gleixner * @timer is not enqueued it starts the timer.
124914f043f1SThomas Gleixner *
1250d02e382cSThomas Gleixner * If @timer->function == NULL then the start operation is silently
1251d02e382cSThomas Gleixner * discarded.
1252d02e382cSThomas Gleixner *
125314f043f1SThomas Gleixner * Return:
1254d02e382cSThomas Gleixner * * %0 - The timer was inactive and started or was in shutdown
1255d02e382cSThomas Gleixner * state and the operation was discarded
125614f043f1SThomas Gleixner * * %1 - The timer was active and requeued to expire at @expires or
125714f043f1SThomas Gleixner * the timer was active and not modified because @expires
125814f043f1SThomas Gleixner * did not change the effective expiry time such that the
125914f043f1SThomas Gleixner * timer would expire earlier than already scheduled
1260b24591e2SDavid Howells */
timer_reduce(struct timer_list * timer,unsigned long expires)1261b24591e2SDavid Howells int timer_reduce(struct timer_list *timer, unsigned long expires)
1262b24591e2SDavid Howells {
1263b24591e2SDavid Howells return __mod_timer(timer, expires, MOD_TIMER_REDUCE);
1264b24591e2SDavid Howells }
1265b24591e2SDavid Howells EXPORT_SYMBOL(timer_reduce);
1266b24591e2SDavid Howells
1267b24591e2SDavid Howells /**
126814f043f1SThomas Gleixner * add_timer - Start a timer
126914f043f1SThomas Gleixner * @timer: The timer to be started
12705cee9645SThomas Gleixner *
127114f043f1SThomas Gleixner * Start @timer to expire at @timer->expires in the future. @timer->expires
127214f043f1SThomas Gleixner * is the absolute expiry time measured in 'jiffies'. When the timer expires
127314f043f1SThomas Gleixner * timer->function(timer) will be invoked from soft interrupt context.
12745cee9645SThomas Gleixner *
127514f043f1SThomas Gleixner * The @timer->expires and @timer->function fields must be set prior
127614f043f1SThomas Gleixner * to calling this function.
12775cee9645SThomas Gleixner *
1278d02e382cSThomas Gleixner * If @timer->function == NULL then the start operation is silently
1279d02e382cSThomas Gleixner * discarded.
1280d02e382cSThomas Gleixner *
128114f043f1SThomas Gleixner * If @timer->expires is already in the past @timer will be queued to
128214f043f1SThomas Gleixner * expire at the next timer tick.
128314f043f1SThomas Gleixner *
128414f043f1SThomas Gleixner * This can only operate on an inactive timer. Attempts to invoke this on
128514f043f1SThomas Gleixner * an active timer are rejected with a warning.
12865cee9645SThomas Gleixner */
add_timer(struct timer_list * timer)12875cee9645SThomas Gleixner void add_timer(struct timer_list *timer)
12885cee9645SThomas Gleixner {
128982ed6f7eSThomas Gleixner if (WARN_ON_ONCE(timer_pending(timer)))
129082ed6f7eSThomas Gleixner return;
129190c01894SEric Dumazet __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);
12925cee9645SThomas Gleixner }
12935cee9645SThomas Gleixner EXPORT_SYMBOL(add_timer);
12945cee9645SThomas Gleixner
12955cee9645SThomas Gleixner /**
12968e7e247fSAnna-Maria Behnsen * add_timer_local() - Start a timer on the local CPU
12978e7e247fSAnna-Maria Behnsen * @timer: The timer to be started
12988e7e247fSAnna-Maria Behnsen *
12998e7e247fSAnna-Maria Behnsen * Same as add_timer() except that the timer flag TIMER_PINNED is set.
13008e7e247fSAnna-Maria Behnsen *
13018e7e247fSAnna-Maria Behnsen * See add_timer() for further details.
13028e7e247fSAnna-Maria Behnsen */
add_timer_local(struct timer_list * timer)13038e7e247fSAnna-Maria Behnsen void add_timer_local(struct timer_list *timer)
13048e7e247fSAnna-Maria Behnsen {
13058e7e247fSAnna-Maria Behnsen if (WARN_ON_ONCE(timer_pending(timer)))
13068e7e247fSAnna-Maria Behnsen return;
13078e7e247fSAnna-Maria Behnsen timer->flags |= TIMER_PINNED;
13088e7e247fSAnna-Maria Behnsen __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);
13098e7e247fSAnna-Maria Behnsen }
13108e7e247fSAnna-Maria Behnsen EXPORT_SYMBOL(add_timer_local);
13118e7e247fSAnna-Maria Behnsen
13128e7e247fSAnna-Maria Behnsen /**
13138e7e247fSAnna-Maria Behnsen * add_timer_global() - Start a timer without TIMER_PINNED flag set
13148e7e247fSAnna-Maria Behnsen * @timer: The timer to be started
13158e7e247fSAnna-Maria Behnsen *
13168e7e247fSAnna-Maria Behnsen * Same as add_timer() except that the timer flag TIMER_PINNED is unset.
13178e7e247fSAnna-Maria Behnsen *
13188e7e247fSAnna-Maria Behnsen * See add_timer() for further details.
13198e7e247fSAnna-Maria Behnsen */
add_timer_global(struct timer_list * timer)13208e7e247fSAnna-Maria Behnsen void add_timer_global(struct timer_list *timer)
13218e7e247fSAnna-Maria Behnsen {
13228e7e247fSAnna-Maria Behnsen if (WARN_ON_ONCE(timer_pending(timer)))
13238e7e247fSAnna-Maria Behnsen return;
13248e7e247fSAnna-Maria Behnsen timer->flags &= ~TIMER_PINNED;
13258e7e247fSAnna-Maria Behnsen __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);
13268e7e247fSAnna-Maria Behnsen }
13278e7e247fSAnna-Maria Behnsen EXPORT_SYMBOL(add_timer_global);
13288e7e247fSAnna-Maria Behnsen
13298e7e247fSAnna-Maria Behnsen /**
133014f043f1SThomas Gleixner * add_timer_on - Start a timer on a particular CPU
133114f043f1SThomas Gleixner * @timer: The timer to be started
133214f043f1SThomas Gleixner * @cpu: The CPU to start it on
13335cee9645SThomas Gleixner *
1334aae55e9fSAnna-Maria Behnsen * Same as add_timer() except that it starts the timer on the given CPU and
1335aae55e9fSAnna-Maria Behnsen * the TIMER_PINNED flag is set. When timer shouldn't be a pinned timer in
1336aae55e9fSAnna-Maria Behnsen * the next round, add_timer_global() should be used instead as it unsets
1337aae55e9fSAnna-Maria Behnsen * the TIMER_PINNED flag.
133814f043f1SThomas Gleixner *
133914f043f1SThomas Gleixner * See add_timer() for further details.
13405cee9645SThomas Gleixner */
add_timer_on(struct timer_list * timer,int cpu)13415cee9645SThomas Gleixner void add_timer_on(struct timer_list *timer, int cpu)
13425cee9645SThomas Gleixner {
1343500462a9SThomas Gleixner struct timer_base *new_base, *base;
13445cee9645SThomas Gleixner unsigned long flags;
13455cee9645SThomas Gleixner
1346d02e382cSThomas Gleixner debug_assert_init(timer);
1347d02e382cSThomas Gleixner
1348d02e382cSThomas Gleixner if (WARN_ON_ONCE(timer_pending(timer)))
134982ed6f7eSThomas Gleixner return;
135022b886ddSTejun Heo
1351aae55e9fSAnna-Maria Behnsen /* Make sure timer flags have TIMER_PINNED flag set */
1352aae55e9fSAnna-Maria Behnsen timer->flags |= TIMER_PINNED;
1353aae55e9fSAnna-Maria Behnsen
1354500462a9SThomas Gleixner new_base = get_timer_cpu_base(timer->flags, cpu);
1355500462a9SThomas Gleixner
135622b886ddSTejun Heo /*
135722b886ddSTejun Heo * If @timer was on a different CPU, it should be migrated with the
135822b886ddSTejun Heo * old base locked to prevent other operations proceeding with the
135922b886ddSTejun Heo * wrong base locked. See lock_timer_base().
136022b886ddSTejun Heo */
136122b886ddSTejun Heo base = lock_timer_base(timer, &flags);
1362d02e382cSThomas Gleixner /*
1363d02e382cSThomas Gleixner * Has @timer been shutdown? This needs to be evaluated while
1364d02e382cSThomas Gleixner * holding base lock to prevent a race against the shutdown code.
1365d02e382cSThomas Gleixner */
1366d02e382cSThomas Gleixner if (!timer->function)
1367d02e382cSThomas Gleixner goto out_unlock;
1368d02e382cSThomas Gleixner
136922b886ddSTejun Heo if (base != new_base) {
137022b886ddSTejun Heo timer->flags |= TIMER_MIGRATING;
137122b886ddSTejun Heo
13722287d866SSebastian Andrzej Siewior raw_spin_unlock(&base->lock);
137322b886ddSTejun Heo base = new_base;
13742287d866SSebastian Andrzej Siewior raw_spin_lock(&base->lock);
137522b886ddSTejun Heo WRITE_ONCE(timer->flags,
137622b886ddSTejun Heo (timer->flags & ~TIMER_BASEMASK) | cpu);
137722b886ddSTejun Heo }
13782fe59f50SNicholas Piggin forward_timer_base(base);
137922b886ddSTejun Heo
1380dc1e7dc5SAnna-Maria Gleixner debug_timer_activate(timer);
13815cee9645SThomas Gleixner internal_add_timer(base, timer);
1382d02e382cSThomas Gleixner out_unlock:
13832287d866SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&base->lock, flags);
13845cee9645SThomas Gleixner }
13855cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(add_timer_on);
13865cee9645SThomas Gleixner
13875cee9645SThomas Gleixner /**
13888553b5f2SThomas Gleixner * __timer_delete - Internal function: Deactivate a timer
138914f043f1SThomas Gleixner * @timer: The timer to be deactivated
13900cc04e80SThomas Gleixner * @shutdown: If true, this indicates that the timer is about to be
13910cc04e80SThomas Gleixner * shutdown permanently.
13920cc04e80SThomas Gleixner *
13930cc04e80SThomas Gleixner * If @shutdown is true then @timer->function is set to NULL under the
13940cc04e80SThomas Gleixner * timer base lock which prevents further rearming of the time. In that
13950cc04e80SThomas Gleixner * case any attempt to rearm @timer after this function returns will be
13960cc04e80SThomas Gleixner * silently ignored.
13975cee9645SThomas Gleixner *
139814f043f1SThomas Gleixner * Return:
139914f043f1SThomas Gleixner * * %0 - The timer was not pending
140014f043f1SThomas Gleixner * * %1 - The timer was pending and deactivated
14015cee9645SThomas Gleixner */
__timer_delete(struct timer_list * timer,bool shutdown)14020cc04e80SThomas Gleixner static int __timer_delete(struct timer_list *timer, bool shutdown)
14035cee9645SThomas Gleixner {
1404494af3edSThomas Gleixner struct timer_base *base;
14055cee9645SThomas Gleixner unsigned long flags;
14065cee9645SThomas Gleixner int ret = 0;
14075cee9645SThomas Gleixner
14085cee9645SThomas Gleixner debug_assert_init(timer);
14095cee9645SThomas Gleixner
14100cc04e80SThomas Gleixner /*
14110cc04e80SThomas Gleixner * If @shutdown is set then the lock has to be taken whether the
14120cc04e80SThomas Gleixner * timer is pending or not to protect against a concurrent rearm
14130cc04e80SThomas Gleixner * which might hit between the lockless pending check and the lock
14149e643ab5SRandy Dunlap * acquisition. By taking the lock it is ensured that such a newly
14150cc04e80SThomas Gleixner * enqueued timer is dequeued and cannot end up with
14160cc04e80SThomas Gleixner * timer->function == NULL in the expiry code.
14170cc04e80SThomas Gleixner *
14180cc04e80SThomas Gleixner * If timer->function is currently executed, then this makes sure
14190cc04e80SThomas Gleixner * that the callback cannot requeue the timer.
14200cc04e80SThomas Gleixner */
14210cc04e80SThomas Gleixner if (timer_pending(timer) || shutdown) {
14225cee9645SThomas Gleixner base = lock_timer_base(timer, &flags);
14235cee9645SThomas Gleixner ret = detach_if_pending(timer, base, true);
14240cc04e80SThomas Gleixner if (shutdown)
14250cc04e80SThomas Gleixner timer->function = NULL;
14262287d866SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&base->lock, flags);
14275cee9645SThomas Gleixner }
14285cee9645SThomas Gleixner
14295cee9645SThomas Gleixner return ret;
14305cee9645SThomas Gleixner }
14318553b5f2SThomas Gleixner
14328553b5f2SThomas Gleixner /**
14338553b5f2SThomas Gleixner * timer_delete - Deactivate a timer
14348553b5f2SThomas Gleixner * @timer: The timer to be deactivated
14358553b5f2SThomas Gleixner *
14368553b5f2SThomas Gleixner * The function only deactivates a pending timer, but contrary to
14378553b5f2SThomas Gleixner * timer_delete_sync() it does not take into account whether the timer's
14388553b5f2SThomas Gleixner * callback function is concurrently executed on a different CPU or not.
14398553b5f2SThomas Gleixner * It neither prevents rearming of the timer. If @timer can be rearmed
14408553b5f2SThomas Gleixner * concurrently then the return value of this function is meaningless.
14418553b5f2SThomas Gleixner *
14428553b5f2SThomas Gleixner * Return:
14438553b5f2SThomas Gleixner * * %0 - The timer was not pending
14448553b5f2SThomas Gleixner * * %1 - The timer was pending and deactivated
14458553b5f2SThomas Gleixner */
timer_delete(struct timer_list * timer)14468553b5f2SThomas Gleixner int timer_delete(struct timer_list *timer)
14478553b5f2SThomas Gleixner {
14480cc04e80SThomas Gleixner return __timer_delete(timer, false);
14498553b5f2SThomas Gleixner }
1450bb663f0fSThomas Gleixner EXPORT_SYMBOL(timer_delete);
14515cee9645SThomas Gleixner
14525cee9645SThomas Gleixner /**
1453f571faf6SThomas Gleixner * timer_shutdown - Deactivate a timer and prevent rearming
1454f571faf6SThomas Gleixner * @timer: The timer to be deactivated
1455f571faf6SThomas Gleixner *
1456f571faf6SThomas Gleixner * The function does not wait for an eventually running timer callback on a
1457f571faf6SThomas Gleixner * different CPU but it prevents rearming of the timer. Any attempt to arm
1458f571faf6SThomas Gleixner * @timer after this function returns will be silently ignored.
1459f571faf6SThomas Gleixner *
1460f571faf6SThomas Gleixner * This function is useful for teardown code and should only be used when
1461f571faf6SThomas Gleixner * timer_shutdown_sync() cannot be invoked due to locking or context constraints.
1462f571faf6SThomas Gleixner *
1463f571faf6SThomas Gleixner * Return:
1464f571faf6SThomas Gleixner * * %0 - The timer was not pending
1465f571faf6SThomas Gleixner * * %1 - The timer was pending
1466f571faf6SThomas Gleixner */
timer_shutdown(struct timer_list * timer)1467f571faf6SThomas Gleixner int timer_shutdown(struct timer_list *timer)
1468f571faf6SThomas Gleixner {
1469f571faf6SThomas Gleixner return __timer_delete(timer, true);
1470f571faf6SThomas Gleixner }
1471f571faf6SThomas Gleixner EXPORT_SYMBOL_GPL(timer_shutdown);
1472f571faf6SThomas Gleixner
1473f571faf6SThomas Gleixner /**
14748553b5f2SThomas Gleixner * __try_to_del_timer_sync - Internal function: Try to deactivate a timer
14758553b5f2SThomas Gleixner * @timer: Timer to deactivate
14760cc04e80SThomas Gleixner * @shutdown: If true, this indicates that the timer is about to be
14770cc04e80SThomas Gleixner * shutdown permanently.
14780cc04e80SThomas Gleixner *
14790cc04e80SThomas Gleixner * If @shutdown is true then @timer->function is set to NULL under the
14800cc04e80SThomas Gleixner * timer base lock which prevents further rearming of the timer. Any
14810cc04e80SThomas Gleixner * attempt to rearm @timer after this function returns will be silently
14820cc04e80SThomas Gleixner * ignored.
14830cc04e80SThomas Gleixner *
14840cc04e80SThomas Gleixner * This function cannot guarantee that the timer cannot be rearmed
14850cc04e80SThomas Gleixner * right after dropping the base lock if @shutdown is false. That
14860cc04e80SThomas Gleixner * needs to be prevented by the calling code if necessary.
14878553b5f2SThomas Gleixner *
14888553b5f2SThomas Gleixner * Return:
14898553b5f2SThomas Gleixner * * %0 - The timer was not pending
14908553b5f2SThomas Gleixner * * %1 - The timer was pending and deactivated
14918553b5f2SThomas Gleixner * * %-1 - The timer callback function is running on a different CPU
14928553b5f2SThomas Gleixner */
__try_to_del_timer_sync(struct timer_list * timer,bool shutdown)14930cc04e80SThomas Gleixner static int __try_to_del_timer_sync(struct timer_list *timer, bool shutdown)
14948553b5f2SThomas Gleixner {
14958553b5f2SThomas Gleixner struct timer_base *base;
14968553b5f2SThomas Gleixner unsigned long flags;
14978553b5f2SThomas Gleixner int ret = -1;
14988553b5f2SThomas Gleixner
14998553b5f2SThomas Gleixner debug_assert_init(timer);
15008553b5f2SThomas Gleixner
15018553b5f2SThomas Gleixner base = lock_timer_base(timer, &flags);
15028553b5f2SThomas Gleixner
15038553b5f2SThomas Gleixner if (base->running_timer != timer)
15048553b5f2SThomas Gleixner ret = detach_if_pending(timer, base, true);
15050cc04e80SThomas Gleixner if (shutdown)
15060cc04e80SThomas Gleixner timer->function = NULL;
15078553b5f2SThomas Gleixner
15088553b5f2SThomas Gleixner raw_spin_unlock_irqrestore(&base->lock, flags);
15098553b5f2SThomas Gleixner
15108553b5f2SThomas Gleixner return ret;
15118553b5f2SThomas Gleixner }
15128553b5f2SThomas Gleixner
15138553b5f2SThomas Gleixner /**
15145cee9645SThomas Gleixner * try_to_del_timer_sync - Try to deactivate a timer
151514f043f1SThomas Gleixner * @timer: Timer to deactivate
15165cee9645SThomas Gleixner *
151714f043f1SThomas Gleixner * This function tries to deactivate a timer. On success the timer is not
151814f043f1SThomas Gleixner * queued and the timer callback function is not running on any CPU.
151914f043f1SThomas Gleixner *
152014f043f1SThomas Gleixner * This function does not guarantee that the timer cannot be rearmed right
152114f043f1SThomas Gleixner * after dropping the base lock. That needs to be prevented by the calling
152214f043f1SThomas Gleixner * code if necessary.
152314f043f1SThomas Gleixner *
152414f043f1SThomas Gleixner * Return:
152514f043f1SThomas Gleixner * * %0 - The timer was not pending
152614f043f1SThomas Gleixner * * %1 - The timer was pending and deactivated
152714f043f1SThomas Gleixner * * %-1 - The timer callback function is running on a different CPU
15285cee9645SThomas Gleixner */
try_to_del_timer_sync(struct timer_list * timer)15295cee9645SThomas Gleixner int try_to_del_timer_sync(struct timer_list *timer)
15305cee9645SThomas Gleixner {
15310cc04e80SThomas Gleixner return __try_to_del_timer_sync(timer, false);
15325cee9645SThomas Gleixner }
15335cee9645SThomas Gleixner EXPORT_SYMBOL(try_to_del_timer_sync);
15345cee9645SThomas Gleixner
1535030dcdd1SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT
timer_base_init_expiry_lock(struct timer_base * base)1536030dcdd1SAnna-Maria Gleixner static __init void timer_base_init_expiry_lock(struct timer_base *base)
1537030dcdd1SAnna-Maria Gleixner {
1538030dcdd1SAnna-Maria Gleixner spin_lock_init(&base->expiry_lock);
1539030dcdd1SAnna-Maria Gleixner }
1540030dcdd1SAnna-Maria Gleixner
timer_base_lock_expiry(struct timer_base * base)1541030dcdd1SAnna-Maria Gleixner static inline void timer_base_lock_expiry(struct timer_base *base)
1542030dcdd1SAnna-Maria Gleixner {
1543030dcdd1SAnna-Maria Gleixner spin_lock(&base->expiry_lock);
1544030dcdd1SAnna-Maria Gleixner }
1545030dcdd1SAnna-Maria Gleixner
timer_base_unlock_expiry(struct timer_base * base)1546030dcdd1SAnna-Maria Gleixner static inline void timer_base_unlock_expiry(struct timer_base *base)
1547030dcdd1SAnna-Maria Gleixner {
1548030dcdd1SAnna-Maria Gleixner spin_unlock(&base->expiry_lock);
1549030dcdd1SAnna-Maria Gleixner }
1550030dcdd1SAnna-Maria Gleixner
1551030dcdd1SAnna-Maria Gleixner /*
1552030dcdd1SAnna-Maria Gleixner * The counterpart to del_timer_wait_running().
1553030dcdd1SAnna-Maria Gleixner *
1554030dcdd1SAnna-Maria Gleixner * If there is a waiter for base->expiry_lock, then it was waiting for the
15554bf07f65SIngo Molnar * timer callback to finish. Drop expiry_lock and reacquire it. That allows
1556030dcdd1SAnna-Maria Gleixner * the waiter to acquire the lock and make progress.
1557030dcdd1SAnna-Maria Gleixner */
timer_sync_wait_running(struct timer_base * base)1558030dcdd1SAnna-Maria Gleixner static void timer_sync_wait_running(struct timer_base *base)
155938cd4ceeSSebastian Andrzej Siewior __releases(&base->lock) __releases(&base->expiry_lock)
156038cd4ceeSSebastian Andrzej Siewior __acquires(&base->expiry_lock) __acquires(&base->lock)
1561030dcdd1SAnna-Maria Gleixner {
1562030dcdd1SAnna-Maria Gleixner if (atomic_read(&base->timer_waiters)) {
1563bb7262b2SThomas Gleixner raw_spin_unlock_irq(&base->lock);
1564030dcdd1SAnna-Maria Gleixner spin_unlock(&base->expiry_lock);
1565030dcdd1SAnna-Maria Gleixner spin_lock(&base->expiry_lock);
1566bb7262b2SThomas Gleixner raw_spin_lock_irq(&base->lock);
1567030dcdd1SAnna-Maria Gleixner }
1568030dcdd1SAnna-Maria Gleixner }
1569030dcdd1SAnna-Maria Gleixner
1570030dcdd1SAnna-Maria Gleixner /*
1571030dcdd1SAnna-Maria Gleixner * This function is called on PREEMPT_RT kernels when the fast path
1572030dcdd1SAnna-Maria Gleixner * deletion of a timer failed because the timer callback function was
1573030dcdd1SAnna-Maria Gleixner * running.
1574030dcdd1SAnna-Maria Gleixner *
1575030dcdd1SAnna-Maria Gleixner * This prevents priority inversion, if the softirq thread on a remote CPU
1576030dcdd1SAnna-Maria Gleixner * got preempted, and it prevents a life lock when the task which tries to
1577030dcdd1SAnna-Maria Gleixner * delete a timer preempted the softirq thread running the timer callback
1578030dcdd1SAnna-Maria Gleixner * function.
1579030dcdd1SAnna-Maria Gleixner */
del_timer_wait_running(struct timer_list * timer)1580030dcdd1SAnna-Maria Gleixner static void del_timer_wait_running(struct timer_list *timer)
1581030dcdd1SAnna-Maria Gleixner {
1582030dcdd1SAnna-Maria Gleixner u32 tf;
1583030dcdd1SAnna-Maria Gleixner
1584030dcdd1SAnna-Maria Gleixner tf = READ_ONCE(timer->flags);
1585c725dafcSSebastian Andrzej Siewior if (!(tf & (TIMER_MIGRATING | TIMER_IRQSAFE))) {
1586030dcdd1SAnna-Maria Gleixner struct timer_base *base = get_timer_base(tf);
1587030dcdd1SAnna-Maria Gleixner
1588030dcdd1SAnna-Maria Gleixner /*
1589030dcdd1SAnna-Maria Gleixner * Mark the base as contended and grab the expiry lock,
1590030dcdd1SAnna-Maria Gleixner * which is held by the softirq across the timer
1591030dcdd1SAnna-Maria Gleixner * callback. Drop the lock immediately so the softirq can
1592030dcdd1SAnna-Maria Gleixner * expire the next timer. In theory the timer could already
1593030dcdd1SAnna-Maria Gleixner * be running again, but that's more than unlikely and just
1594030dcdd1SAnna-Maria Gleixner * causes another wait loop.
1595030dcdd1SAnna-Maria Gleixner */
1596030dcdd1SAnna-Maria Gleixner atomic_inc(&base->timer_waiters);
1597030dcdd1SAnna-Maria Gleixner spin_lock_bh(&base->expiry_lock);
1598030dcdd1SAnna-Maria Gleixner atomic_dec(&base->timer_waiters);
1599030dcdd1SAnna-Maria Gleixner spin_unlock_bh(&base->expiry_lock);
1600030dcdd1SAnna-Maria Gleixner }
1601030dcdd1SAnna-Maria Gleixner }
1602030dcdd1SAnna-Maria Gleixner #else
timer_base_init_expiry_lock(struct timer_base * base)1603030dcdd1SAnna-Maria Gleixner static inline void timer_base_init_expiry_lock(struct timer_base *base) { }
timer_base_lock_expiry(struct timer_base * base)1604030dcdd1SAnna-Maria Gleixner static inline void timer_base_lock_expiry(struct timer_base *base) { }
timer_base_unlock_expiry(struct timer_base * base)1605030dcdd1SAnna-Maria Gleixner static inline void timer_base_unlock_expiry(struct timer_base *base) { }
timer_sync_wait_running(struct timer_base * base)1606030dcdd1SAnna-Maria Gleixner static inline void timer_sync_wait_running(struct timer_base *base) { }
del_timer_wait_running(struct timer_list * timer)1607030dcdd1SAnna-Maria Gleixner static inline void del_timer_wait_running(struct timer_list *timer) { }
1608030dcdd1SAnna-Maria Gleixner #endif
1609030dcdd1SAnna-Maria Gleixner
16105cee9645SThomas Gleixner /**
16118553b5f2SThomas Gleixner * __timer_delete_sync - Internal function: Deactivate a timer and wait
16128553b5f2SThomas Gleixner * for the handler to finish.
16138553b5f2SThomas Gleixner * @timer: The timer to be deactivated
16140cc04e80SThomas Gleixner * @shutdown: If true, @timer->function will be set to NULL under the
16150cc04e80SThomas Gleixner * timer base lock which prevents rearming of @timer
16160cc04e80SThomas Gleixner *
16170cc04e80SThomas Gleixner * If @shutdown is not set the timer can be rearmed later. If the timer can
16180cc04e80SThomas Gleixner * be rearmed concurrently, i.e. after dropping the base lock then the
16190cc04e80SThomas Gleixner * return value is meaningless.
16200cc04e80SThomas Gleixner *
16210cc04e80SThomas Gleixner * If @shutdown is set then @timer->function is set to NULL under timer
16220cc04e80SThomas Gleixner * base lock which prevents rearming of the timer. Any attempt to rearm
16230cc04e80SThomas Gleixner * a shutdown timer is silently ignored.
16240cc04e80SThomas Gleixner *
16250cc04e80SThomas Gleixner * If the timer should be reused after shutdown it has to be initialized
16260cc04e80SThomas Gleixner * again.
16278553b5f2SThomas Gleixner *
16288553b5f2SThomas Gleixner * Return:
16298553b5f2SThomas Gleixner * * %0 - The timer was not pending
16308553b5f2SThomas Gleixner * * %1 - The timer was pending and deactivated
16318553b5f2SThomas Gleixner */
__timer_delete_sync(struct timer_list * timer,bool shutdown)16320cc04e80SThomas Gleixner static int __timer_delete_sync(struct timer_list *timer, bool shutdown)
16338553b5f2SThomas Gleixner {
16348553b5f2SThomas Gleixner int ret;
16358553b5f2SThomas Gleixner
16368553b5f2SThomas Gleixner #ifdef CONFIG_LOCKDEP
16378553b5f2SThomas Gleixner unsigned long flags;
16388553b5f2SThomas Gleixner
16398553b5f2SThomas Gleixner /*
16408553b5f2SThomas Gleixner * If lockdep gives a backtrace here, please reference
16418553b5f2SThomas Gleixner * the synchronization rules above.
16428553b5f2SThomas Gleixner */
16438553b5f2SThomas Gleixner local_irq_save(flags);
16448553b5f2SThomas Gleixner lock_map_acquire(&timer->lockdep_map);
16458553b5f2SThomas Gleixner lock_map_release(&timer->lockdep_map);
16468553b5f2SThomas Gleixner local_irq_restore(flags);
16478553b5f2SThomas Gleixner #endif
16488553b5f2SThomas Gleixner /*
16498553b5f2SThomas Gleixner * don't use it in hardirq context, because it
16508553b5f2SThomas Gleixner * could lead to deadlock.
16518553b5f2SThomas Gleixner */
16528553b5f2SThomas Gleixner WARN_ON(in_hardirq() && !(timer->flags & TIMER_IRQSAFE));
16538553b5f2SThomas Gleixner
16548553b5f2SThomas Gleixner /*
16558553b5f2SThomas Gleixner * Must be able to sleep on PREEMPT_RT because of the slowpath in
16568553b5f2SThomas Gleixner * del_timer_wait_running().
16578553b5f2SThomas Gleixner */
16588553b5f2SThomas Gleixner if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(timer->flags & TIMER_IRQSAFE))
16598553b5f2SThomas Gleixner lockdep_assert_preemption_enabled();
16608553b5f2SThomas Gleixner
16618553b5f2SThomas Gleixner do {
16620cc04e80SThomas Gleixner ret = __try_to_del_timer_sync(timer, shutdown);
16638553b5f2SThomas Gleixner
16648553b5f2SThomas Gleixner if (unlikely(ret < 0)) {
16658553b5f2SThomas Gleixner del_timer_wait_running(timer);
16668553b5f2SThomas Gleixner cpu_relax();
16678553b5f2SThomas Gleixner }
16688553b5f2SThomas Gleixner } while (ret < 0);
16698553b5f2SThomas Gleixner
16708553b5f2SThomas Gleixner return ret;
16718553b5f2SThomas Gleixner }
16728553b5f2SThomas Gleixner
16738553b5f2SThomas Gleixner /**
16749b13df3fSThomas Gleixner * timer_delete_sync - Deactivate a timer and wait for the handler to finish.
167514f043f1SThomas Gleixner * @timer: The timer to be deactivated
16765cee9645SThomas Gleixner *
16775cee9645SThomas Gleixner * Synchronization rules: Callers must prevent restarting of the timer,
16785cee9645SThomas Gleixner * otherwise this function is meaningless. It must not be called from
16795cee9645SThomas Gleixner * interrupt contexts unless the timer is an irqsafe one. The caller must
168014f043f1SThomas Gleixner * not hold locks which would prevent completion of the timer's callback
168114f043f1SThomas Gleixner * function. The timer's handler must not call add_timer_on(). Upon exit
168214f043f1SThomas Gleixner * the timer is not queued and the handler is not running on any CPU.
16835cee9645SThomas Gleixner *
168414f043f1SThomas Gleixner * For !irqsafe timers, the caller must not hold locks that are held in
168514f043f1SThomas Gleixner * interrupt context. Even if the lock has nothing to do with the timer in
168614f043f1SThomas Gleixner * question. Here's why::
16875cee9645SThomas Gleixner *
16885cee9645SThomas Gleixner * CPU0 CPU1
16895cee9645SThomas Gleixner * ---- ----
16905cee9645SThomas Gleixner * <SOFTIRQ>
16915cee9645SThomas Gleixner * call_timer_fn();
16925cee9645SThomas Gleixner * base->running_timer = mytimer;
16935cee9645SThomas Gleixner * spin_lock_irq(somelock);
16945cee9645SThomas Gleixner * <IRQ>
16955cee9645SThomas Gleixner * spin_lock(somelock);
16969b13df3fSThomas Gleixner * timer_delete_sync(mytimer);
16975cee9645SThomas Gleixner * while (base->running_timer == mytimer);
16985cee9645SThomas Gleixner *
16999b13df3fSThomas Gleixner * Now timer_delete_sync() will never return and never release somelock.
170014f043f1SThomas Gleixner * The interrupt on the other CPU is waiting to grab somelock but it has
170114f043f1SThomas Gleixner * interrupted the softirq that CPU0 is waiting to finish.
17025cee9645SThomas Gleixner *
170314f043f1SThomas Gleixner * This function cannot guarantee that the timer is not rearmed again by
170414f043f1SThomas Gleixner * some concurrent or preempting code, right after it dropped the base
170514f043f1SThomas Gleixner * lock. If there is the possibility of a concurrent rearm then the return
170614f043f1SThomas Gleixner * value of the function is meaningless.
170714f043f1SThomas Gleixner *
1708f571faf6SThomas Gleixner * If such a guarantee is needed, e.g. for teardown situations then use
1709f571faf6SThomas Gleixner * timer_shutdown_sync() instead.
1710f571faf6SThomas Gleixner *
171114f043f1SThomas Gleixner * Return:
171214f043f1SThomas Gleixner * * %0 - The timer was not pending
171314f043f1SThomas Gleixner * * %1 - The timer was pending and deactivated
17145cee9645SThomas Gleixner */
timer_delete_sync(struct timer_list * timer)17159b13df3fSThomas Gleixner int timer_delete_sync(struct timer_list *timer)
17165cee9645SThomas Gleixner {
17170cc04e80SThomas Gleixner return __timer_delete_sync(timer, false);
17185cee9645SThomas Gleixner }
17199b13df3fSThomas Gleixner EXPORT_SYMBOL(timer_delete_sync);
17205cee9645SThomas Gleixner
1721f571faf6SThomas Gleixner /**
1722f571faf6SThomas Gleixner * timer_shutdown_sync - Shutdown a timer and prevent rearming
1723f571faf6SThomas Gleixner * @timer: The timer to be shutdown
1724f571faf6SThomas Gleixner *
1725f571faf6SThomas Gleixner * When the function returns it is guaranteed that:
1726f571faf6SThomas Gleixner * - @timer is not queued
1727f571faf6SThomas Gleixner * - The callback function of @timer is not running
1728f571faf6SThomas Gleixner * - @timer cannot be enqueued again. Any attempt to rearm
1729f571faf6SThomas Gleixner * @timer is silently ignored.
1730f571faf6SThomas Gleixner *
1731f571faf6SThomas Gleixner * See timer_delete_sync() for synchronization rules.
1732f571faf6SThomas Gleixner *
1733f571faf6SThomas Gleixner * This function is useful for final teardown of an infrastructure where
1734f571faf6SThomas Gleixner * the timer is subject to a circular dependency problem.
1735f571faf6SThomas Gleixner *
1736f571faf6SThomas Gleixner * A common pattern for this is a timer and a workqueue where the timer can
1737f571faf6SThomas Gleixner * schedule work and work can arm the timer. On shutdown the workqueue must
1738f571faf6SThomas Gleixner * be destroyed and the timer must be prevented from rearming. Unless the
1739f571faf6SThomas Gleixner * code has conditionals like 'if (mything->in_shutdown)' to prevent that
1740f571faf6SThomas Gleixner * there is no way to get this correct with timer_delete_sync().
1741f571faf6SThomas Gleixner *
1742f571faf6SThomas Gleixner * timer_shutdown_sync() is solving the problem. The correct ordering of
1743f571faf6SThomas Gleixner * calls in this case is:
1744f571faf6SThomas Gleixner *
1745f571faf6SThomas Gleixner * timer_shutdown_sync(&mything->timer);
1746f571faf6SThomas Gleixner * workqueue_destroy(&mything->workqueue);
1747f571faf6SThomas Gleixner *
1748f571faf6SThomas Gleixner * After this 'mything' can be safely freed.
1749f571faf6SThomas Gleixner *
1750f571faf6SThomas Gleixner * This obviously implies that the timer is not required to be functional
1751f571faf6SThomas Gleixner * for the rest of the shutdown operation.
1752f571faf6SThomas Gleixner *
1753f571faf6SThomas Gleixner * Return:
1754f571faf6SThomas Gleixner * * %0 - The timer was not pending
1755f571faf6SThomas Gleixner * * %1 - The timer was pending
1756f571faf6SThomas Gleixner */
timer_shutdown_sync(struct timer_list * timer)1757f571faf6SThomas Gleixner int timer_shutdown_sync(struct timer_list *timer)
1758f571faf6SThomas Gleixner {
1759f571faf6SThomas Gleixner return __timer_delete_sync(timer, true);
1760f571faf6SThomas Gleixner }
1761f571faf6SThomas Gleixner EXPORT_SYMBOL_GPL(timer_shutdown_sync);
1762f571faf6SThomas Gleixner
call_timer_fn(struct timer_list * timer,void (* fn)(struct timer_list *),unsigned long baseclk)1763f28d3d53SAnna-Maria Gleixner static void call_timer_fn(struct timer_list *timer,
1764f28d3d53SAnna-Maria Gleixner void (*fn)(struct timer_list *),
1765f28d3d53SAnna-Maria Gleixner unsigned long baseclk)
17665cee9645SThomas Gleixner {
17675cee9645SThomas Gleixner int count = preempt_count();
17685cee9645SThomas Gleixner
17695cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP
17705cee9645SThomas Gleixner /*
17715cee9645SThomas Gleixner * It is permissible to free the timer from inside the
17725cee9645SThomas Gleixner * function that is called from it, this we need to take into
17735cee9645SThomas Gleixner * account for lockdep too. To avoid bogus "held lock freed"
17745cee9645SThomas Gleixner * warnings as well as problems when looking into
17755cee9645SThomas Gleixner * timer->lockdep_map, make a copy and use that here.
17765cee9645SThomas Gleixner */
17775cee9645SThomas Gleixner struct lockdep_map lockdep_map;
17785cee9645SThomas Gleixner
17795cee9645SThomas Gleixner lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
17805cee9645SThomas Gleixner #endif
17815cee9645SThomas Gleixner /*
17825cee9645SThomas Gleixner * Couple the lock chain with the lock chain at
17839b13df3fSThomas Gleixner * timer_delete_sync() by acquiring the lock_map around the fn()
17849b13df3fSThomas Gleixner * call here and in timer_delete_sync().
17855cee9645SThomas Gleixner */
17865cee9645SThomas Gleixner lock_map_acquire(&lockdep_map);
17875cee9645SThomas Gleixner
1788f28d3d53SAnna-Maria Gleixner trace_timer_expire_entry(timer, baseclk);
1789354b46b1SKees Cook fn(timer);
17905cee9645SThomas Gleixner trace_timer_expire_exit(timer);
17915cee9645SThomas Gleixner
17925cee9645SThomas Gleixner lock_map_release(&lockdep_map);
17935cee9645SThomas Gleixner
17945cee9645SThomas Gleixner if (count != preempt_count()) {
1795d75f773cSSakari Ailus WARN_ONCE(1, "timer: %pS preempt leak: %08x -> %08x\n",
17965cee9645SThomas Gleixner fn, count, preempt_count());
17975cee9645SThomas Gleixner /*
17985cee9645SThomas Gleixner * Restore the preempt count. That gives us a decent
17995cee9645SThomas Gleixner * chance to survive and extract information. If the
18005cee9645SThomas Gleixner * callback kept a lock held, bad luck, but not worse
18015cee9645SThomas Gleixner * than the BUG() we had.
18025cee9645SThomas Gleixner */
18035cee9645SThomas Gleixner preempt_count_set(count);
18045cee9645SThomas Gleixner }
18055cee9645SThomas Gleixner }
18065cee9645SThomas Gleixner
expire_timers(struct timer_base * base,struct hlist_head * head)1807500462a9SThomas Gleixner static void expire_timers(struct timer_base *base, struct hlist_head *head)
18085cee9645SThomas Gleixner {
1809f28d3d53SAnna-Maria Gleixner /*
1810f28d3d53SAnna-Maria Gleixner * This value is required only for tracing. base->clk was
1811f28d3d53SAnna-Maria Gleixner * incremented directly before expire_timers was called. But expiry
1812f28d3d53SAnna-Maria Gleixner * is related to the old base->clk value.
1813f28d3d53SAnna-Maria Gleixner */
1814f28d3d53SAnna-Maria Gleixner unsigned long baseclk = base->clk - 1;
1815f28d3d53SAnna-Maria Gleixner
18161dabbcecSThomas Gleixner while (!hlist_empty(head)) {
1817500462a9SThomas Gleixner struct timer_list *timer;
1818354b46b1SKees Cook void (*fn)(struct timer_list *);
18195cee9645SThomas Gleixner
18201dabbcecSThomas Gleixner timer = hlist_entry(head->first, struct timer_list, entry);
18215cee9645SThomas Gleixner
18225cee9645SThomas Gleixner base->running_timer = timer;
1823500462a9SThomas Gleixner detach_timer(timer, true);
18245cee9645SThomas Gleixner
1825500462a9SThomas Gleixner fn = timer->function;
1826500462a9SThomas Gleixner
1827d02e382cSThomas Gleixner if (WARN_ON_ONCE(!fn)) {
1828d02e382cSThomas Gleixner /* Should never happen. Emphasis on should! */
1829d02e382cSThomas Gleixner base->running_timer = NULL;
1830d02e382cSThomas Gleixner continue;
1831d02e382cSThomas Gleixner }
1832d02e382cSThomas Gleixner
1833500462a9SThomas Gleixner if (timer->flags & TIMER_IRQSAFE) {
18342287d866SSebastian Andrzej Siewior raw_spin_unlock(&base->lock);
1835f28d3d53SAnna-Maria Gleixner call_timer_fn(timer, fn, baseclk);
18362287d866SSebastian Andrzej Siewior raw_spin_lock(&base->lock);
1837bb7262b2SThomas Gleixner base->running_timer = NULL;
18385cee9645SThomas Gleixner } else {
18392287d866SSebastian Andrzej Siewior raw_spin_unlock_irq(&base->lock);
1840f28d3d53SAnna-Maria Gleixner call_timer_fn(timer, fn, baseclk);
1841bb7262b2SThomas Gleixner raw_spin_lock_irq(&base->lock);
1842030dcdd1SAnna-Maria Gleixner base->running_timer = NULL;
1843030dcdd1SAnna-Maria Gleixner timer_sync_wait_running(base);
18445cee9645SThomas Gleixner }
18455cee9645SThomas Gleixner }
18465cee9645SThomas Gleixner }
1847500462a9SThomas Gleixner
collect_expired_timers(struct timer_base * base,struct hlist_head * heads)1848d4f7dae8SFrederic Weisbecker static int collect_expired_timers(struct timer_base *base,
1849500462a9SThomas Gleixner struct hlist_head *heads)
1850500462a9SThomas Gleixner {
1851d4f7dae8SFrederic Weisbecker unsigned long clk = base->clk = base->next_expiry;
1852500462a9SThomas Gleixner struct hlist_head *vec;
1853500462a9SThomas Gleixner int i, levels = 0;
1854500462a9SThomas Gleixner unsigned int idx;
1855500462a9SThomas Gleixner
1856500462a9SThomas Gleixner for (i = 0; i < LVL_DEPTH; i++) {
1857500462a9SThomas Gleixner idx = (clk & LVL_MASK) + i * LVL_SIZE;
1858500462a9SThomas Gleixner
1859500462a9SThomas Gleixner if (__test_and_clear_bit(idx, base->pending_map)) {
1860500462a9SThomas Gleixner vec = base->vectors + idx;
1861500462a9SThomas Gleixner hlist_move_list(vec, heads++);
1862500462a9SThomas Gleixner levels++;
1863500462a9SThomas Gleixner }
1864500462a9SThomas Gleixner /* Is it time to look at the next level? */
1865500462a9SThomas Gleixner if (clk & LVL_CLK_MASK)
1866500462a9SThomas Gleixner break;
1867500462a9SThomas Gleixner /* Shift clock for the next level granularity */
1868500462a9SThomas Gleixner clk >>= LVL_CLK_SHIFT;
1869500462a9SThomas Gleixner }
1870500462a9SThomas Gleixner return levels;
18715cee9645SThomas Gleixner }
18725cee9645SThomas Gleixner
18735cee9645SThomas Gleixner /*
187423696838SAnna-Maria Gleixner * Find the next pending bucket of a level. Search from level start (@offset)
187523696838SAnna-Maria Gleixner * + @clk upwards and if nothing there, search from start of the level
187623696838SAnna-Maria Gleixner * (@offset) up to @offset + clk.
18775cee9645SThomas Gleixner */
next_pending_bucket(struct timer_base * base,unsigned offset,unsigned clk)1878500462a9SThomas Gleixner static int next_pending_bucket(struct timer_base *base, unsigned offset,
1879500462a9SThomas Gleixner unsigned clk)
18805cee9645SThomas Gleixner {
1881500462a9SThomas Gleixner unsigned pos, start = offset + clk;
1882500462a9SThomas Gleixner unsigned end = offset + LVL_SIZE;
18835cee9645SThomas Gleixner
1884500462a9SThomas Gleixner pos = find_next_bit(base->pending_map, end, start);
1885500462a9SThomas Gleixner if (pos < end)
1886500462a9SThomas Gleixner return pos - start;
18875cee9645SThomas Gleixner
1888500462a9SThomas Gleixner pos = find_next_bit(base->pending_map, start, offset);
1889500462a9SThomas Gleixner return pos < start ? pos + LVL_SIZE - start : -1;
18905cee9645SThomas Gleixner }
18915cee9645SThomas Gleixner
1892500462a9SThomas Gleixner /*
189323696838SAnna-Maria Gleixner * Search the first expiring timer in the various clock levels. Caller must
189423696838SAnna-Maria Gleixner * hold base->lock.
1895b5e6f598SAnna-Maria Behnsen *
1896b5e6f598SAnna-Maria Behnsen * Store next expiry time in base->next_expiry.
18975cee9645SThomas Gleixner */
timer_recalc_next_expiry(struct timer_base * base)1898fe90c5baSAnna-Maria Behnsen static void timer_recalc_next_expiry(struct timer_base *base)
18995cee9645SThomas Gleixner {
1900500462a9SThomas Gleixner unsigned long clk, next, adj;
1901500462a9SThomas Gleixner unsigned lvl, offset = 0;
19025cee9645SThomas Gleixner
1903500462a9SThomas Gleixner next = base->clk + NEXT_TIMER_MAX_DELTA;
1904500462a9SThomas Gleixner clk = base->clk;
1905500462a9SThomas Gleixner for (lvl = 0; lvl < LVL_DEPTH; lvl++, offset += LVL_SIZE) {
1906500462a9SThomas Gleixner int pos = next_pending_bucket(base, offset, clk & LVL_MASK);
1907001ec1b3SFrederic Weisbecker unsigned long lvl_clk = clk & LVL_CLK_MASK;
19085cee9645SThomas Gleixner
1909500462a9SThomas Gleixner if (pos >= 0) {
1910500462a9SThomas Gleixner unsigned long tmp = clk + (unsigned long) pos;
19115cee9645SThomas Gleixner
1912500462a9SThomas Gleixner tmp <<= LVL_SHIFT(lvl);
1913500462a9SThomas Gleixner if (time_before(tmp, next))
1914500462a9SThomas Gleixner next = tmp;
1915001ec1b3SFrederic Weisbecker
1916001ec1b3SFrederic Weisbecker /*
1917001ec1b3SFrederic Weisbecker * If the next expiration happens before we reach
1918001ec1b3SFrederic Weisbecker * the next level, no need to check further.
1919001ec1b3SFrederic Weisbecker */
1920001ec1b3SFrederic Weisbecker if (pos <= ((LVL_CLK_DIV - lvl_clk) & LVL_CLK_MASK))
1921001ec1b3SFrederic Weisbecker break;
19225cee9645SThomas Gleixner }
19235cee9645SThomas Gleixner /*
1924500462a9SThomas Gleixner * Clock for the next level. If the current level clock lower
1925500462a9SThomas Gleixner * bits are zero, we look at the next level as is. If not we
1926500462a9SThomas Gleixner * need to advance it by one because that's going to be the
1927500462a9SThomas Gleixner * next expiring bucket in that level. base->clk is the next
1928bd7c8ff9SAnna-Maria Behnsen * expiring jiffy. So in case of:
1929500462a9SThomas Gleixner *
1930500462a9SThomas Gleixner * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1931500462a9SThomas Gleixner * 0 0 0 0 0 0
1932500462a9SThomas Gleixner *
1933500462a9SThomas Gleixner * we have to look at all levels @index 0. With
1934500462a9SThomas Gleixner *
1935500462a9SThomas Gleixner * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1936500462a9SThomas Gleixner * 0 0 0 0 0 2
1937500462a9SThomas Gleixner *
1938500462a9SThomas Gleixner * LVL0 has the next expiring bucket @index 2. The upper
1939500462a9SThomas Gleixner * levels have the next expiring bucket @index 1.
1940500462a9SThomas Gleixner *
1941500462a9SThomas Gleixner * In case that the propagation wraps the next level the same
1942500462a9SThomas Gleixner * rules apply:
1943500462a9SThomas Gleixner *
1944500462a9SThomas Gleixner * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
1945500462a9SThomas Gleixner * 0 0 0 0 F 2
1946500462a9SThomas Gleixner *
1947500462a9SThomas Gleixner * So after looking at LVL0 we get:
1948500462a9SThomas Gleixner *
1949500462a9SThomas Gleixner * LVL5 LVL4 LVL3 LVL2 LVL1
1950500462a9SThomas Gleixner * 0 0 0 1 0
1951500462a9SThomas Gleixner *
1952500462a9SThomas Gleixner * So no propagation from LVL1 to LVL2 because that happened
1953500462a9SThomas Gleixner * with the add already, but then we need to propagate further
1954500462a9SThomas Gleixner * from LVL2 to LVL3.
1955500462a9SThomas Gleixner *
1956500462a9SThomas Gleixner * So the simple check whether the lower bits of the current
1957500462a9SThomas Gleixner * level are 0 or not is sufficient for all cases.
19585cee9645SThomas Gleixner */
1959001ec1b3SFrederic Weisbecker adj = lvl_clk ? 1 : 0;
1960500462a9SThomas Gleixner clk >>= LVL_CLK_SHIFT;
1961500462a9SThomas Gleixner clk += adj;
19625cee9645SThomas Gleixner }
196331cd0e11SFrederic Weisbecker
196479f8b28eSAnna-Maria Behnsen WRITE_ONCE(base->next_expiry, next);
196531cd0e11SFrederic Weisbecker base->next_expiry_recalc = false;
1966aebacb7fSNicolas Saenz Julienne base->timers_pending = !(next == base->clk + NEXT_TIMER_MAX_DELTA);
19675cee9645SThomas Gleixner }
19685cee9645SThomas Gleixner
1969dc2a0f1fSFrederic Weisbecker #ifdef CONFIG_NO_HZ_COMMON
19705cee9645SThomas Gleixner /*
19715cee9645SThomas Gleixner * Check, if the next hrtimer event is before the next timer wheel
19725cee9645SThomas Gleixner * event:
19735cee9645SThomas Gleixner */
cmp_next_hrtimer_event(u64 basem,u64 expires)1974c1ad348bSThomas Gleixner static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
19755cee9645SThomas Gleixner {
1976c1ad348bSThomas Gleixner u64 nextevt = hrtimer_get_next_event();
19775cee9645SThomas Gleixner
1978c1ad348bSThomas Gleixner /*
1979c1ad348bSThomas Gleixner * If high resolution timers are enabled
1980c1ad348bSThomas Gleixner * hrtimer_get_next_event() returns KTIME_MAX.
1981c1ad348bSThomas Gleixner */
1982c1ad348bSThomas Gleixner if (expires <= nextevt)
19835cee9645SThomas Gleixner return expires;
19845cee9645SThomas Gleixner
19855cee9645SThomas Gleixner /*
1986c1ad348bSThomas Gleixner * If the next timer is already expired, return the tick base
1987c1ad348bSThomas Gleixner * time so the tick is fired immediately.
19885cee9645SThomas Gleixner */
1989c1ad348bSThomas Gleixner if (nextevt <= basem)
1990c1ad348bSThomas Gleixner return basem;
19915cee9645SThomas Gleixner
19925cee9645SThomas Gleixner /*
1993bd7c8ff9SAnna-Maria Behnsen * Round up to the next jiffy. High resolution timers are
1994c1ad348bSThomas Gleixner * off, so the hrtimers are expired in the tick and we need to
1995c1ad348bSThomas Gleixner * make sure that this tick really expires the timer to avoid
1996c1ad348bSThomas Gleixner * a ping pong of the nohz stop code.
1997c1ad348bSThomas Gleixner *
1998c1ad348bSThomas Gleixner * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
19995cee9645SThomas Gleixner */
2000c1ad348bSThomas Gleixner return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
20015cee9645SThomas Gleixner }
20025cee9645SThomas Gleixner
next_timer_interrupt(struct timer_base * base,unsigned long basej)20039f6a3c60SAnna-Maria Behnsen static unsigned long next_timer_interrupt(struct timer_base *base,
20049f6a3c60SAnna-Maria Behnsen unsigned long basej)
20059f6a3c60SAnna-Maria Behnsen {
20069f6a3c60SAnna-Maria Behnsen if (base->next_expiry_recalc)
2007fe90c5baSAnna-Maria Behnsen timer_recalc_next_expiry(base);
20089f6a3c60SAnna-Maria Behnsen
20099f6a3c60SAnna-Maria Behnsen /*
20109f6a3c60SAnna-Maria Behnsen * Move next_expiry for the empty base into the future to prevent an
20119f6a3c60SAnna-Maria Behnsen * unnecessary raise of the timer softirq when the next_expiry value
20129f6a3c60SAnna-Maria Behnsen * will be reached even if there is no timer pending.
201383a665dcSAnna-Maria Behnsen *
201483a665dcSAnna-Maria Behnsen * This update is also required to make timer_base::next_expiry values
201583a665dcSAnna-Maria Behnsen * easy comparable to find out which base holds the first pending timer.
20169f6a3c60SAnna-Maria Behnsen */
20179f6a3c60SAnna-Maria Behnsen if (!base->timers_pending)
201879f8b28eSAnna-Maria Behnsen WRITE_ONCE(base->next_expiry, basej + NEXT_TIMER_MAX_DELTA);
20199f6a3c60SAnna-Maria Behnsen
20209f6a3c60SAnna-Maria Behnsen return base->next_expiry;
20219f6a3c60SAnna-Maria Behnsen }
20229f6a3c60SAnna-Maria Behnsen
fetch_next_timer_interrupt(unsigned long basej,u64 basem,struct timer_base * base_local,struct timer_base * base_global,struct timer_events * tevt)202370b4cf84SAnna-Maria Behnsen static unsigned long fetch_next_timer_interrupt(unsigned long basej, u64 basem,
202470b4cf84SAnna-Maria Behnsen struct timer_base *base_local,
202570b4cf84SAnna-Maria Behnsen struct timer_base *base_global,
202670b4cf84SAnna-Maria Behnsen struct timer_events *tevt)
202770b4cf84SAnna-Maria Behnsen {
202870b4cf84SAnna-Maria Behnsen unsigned long nextevt, nextevt_local, nextevt_global;
202970b4cf84SAnna-Maria Behnsen bool local_first;
203070b4cf84SAnna-Maria Behnsen
203170b4cf84SAnna-Maria Behnsen nextevt_local = next_timer_interrupt(base_local, basej);
203270b4cf84SAnna-Maria Behnsen nextevt_global = next_timer_interrupt(base_global, basej);
203370b4cf84SAnna-Maria Behnsen
203470b4cf84SAnna-Maria Behnsen local_first = time_before_eq(nextevt_local, nextevt_global);
203570b4cf84SAnna-Maria Behnsen
203670b4cf84SAnna-Maria Behnsen nextevt = local_first ? nextevt_local : nextevt_global;
203770b4cf84SAnna-Maria Behnsen
203870b4cf84SAnna-Maria Behnsen /*
203970b4cf84SAnna-Maria Behnsen * If the @nextevt is at max. one tick away, use @nextevt and store
204070b4cf84SAnna-Maria Behnsen * it in the local expiry value. The next global event is irrelevant in
204170b4cf84SAnna-Maria Behnsen * this case and can be left as KTIME_MAX.
204270b4cf84SAnna-Maria Behnsen */
204370b4cf84SAnna-Maria Behnsen if (time_before_eq(nextevt, basej + 1)) {
204470b4cf84SAnna-Maria Behnsen /* If we missed a tick already, force 0 delta */
204570b4cf84SAnna-Maria Behnsen if (time_before(nextevt, basej))
204670b4cf84SAnna-Maria Behnsen nextevt = basej;
204770b4cf84SAnna-Maria Behnsen tevt->local = basem + (u64)(nextevt - basej) * TICK_NSEC;
2048f73d9257SAnna-Maria Behnsen
2049f73d9257SAnna-Maria Behnsen /*
2050f73d9257SAnna-Maria Behnsen * This is required for the remote check only but it doesn't
2051f73d9257SAnna-Maria Behnsen * hurt, when it is done for both call sites:
2052f73d9257SAnna-Maria Behnsen *
2053f73d9257SAnna-Maria Behnsen * * The remote callers will only take care of the global timers
2054f73d9257SAnna-Maria Behnsen * as local timers will be handled by CPU itself. When not
2055f73d9257SAnna-Maria Behnsen * updating tevt->global with the already missed first global
2056f73d9257SAnna-Maria Behnsen * timer, it is possible that it will be missed completely.
2057f73d9257SAnna-Maria Behnsen *
2058f73d9257SAnna-Maria Behnsen * * The local callers will ignore the tevt->global anyway, when
2059f73d9257SAnna-Maria Behnsen * nextevt is max. one tick away.
2060f73d9257SAnna-Maria Behnsen */
2061f73d9257SAnna-Maria Behnsen if (!local_first)
2062f73d9257SAnna-Maria Behnsen tevt->global = tevt->local;
206370b4cf84SAnna-Maria Behnsen return nextevt;
206470b4cf84SAnna-Maria Behnsen }
206570b4cf84SAnna-Maria Behnsen
206670b4cf84SAnna-Maria Behnsen /*
206770b4cf84SAnna-Maria Behnsen * Update tevt.* values:
206870b4cf84SAnna-Maria Behnsen *
206970b4cf84SAnna-Maria Behnsen * If the local queue expires first, then the global event can be
207070b4cf84SAnna-Maria Behnsen * ignored. If the global queue is empty, nothing to do either.
207170b4cf84SAnna-Maria Behnsen */
207270b4cf84SAnna-Maria Behnsen if (!local_first && base_global->timers_pending)
207370b4cf84SAnna-Maria Behnsen tevt->global = basem + (u64)(nextevt_global - basej) * TICK_NSEC;
207470b4cf84SAnna-Maria Behnsen
207570b4cf84SAnna-Maria Behnsen if (base_local->timers_pending)
207670b4cf84SAnna-Maria Behnsen tevt->local = basem + (u64)(nextevt_local - basej) * TICK_NSEC;
207770b4cf84SAnna-Maria Behnsen
207870b4cf84SAnna-Maria Behnsen return nextevt;
207970b4cf84SAnna-Maria Behnsen }
208070b4cf84SAnna-Maria Behnsen
2081f73d9257SAnna-Maria Behnsen # ifdef CONFIG_SMP
2082f73d9257SAnna-Maria Behnsen /**
2083f73d9257SAnna-Maria Behnsen * fetch_next_timer_interrupt_remote() - Store next timers into @tevt
2084f73d9257SAnna-Maria Behnsen * @basej: base time jiffies
2085f73d9257SAnna-Maria Behnsen * @basem: base time clock monotonic
2086f73d9257SAnna-Maria Behnsen * @tevt: Pointer to the storage for the expiry values
2087f73d9257SAnna-Maria Behnsen * @cpu: Remote CPU
2088f73d9257SAnna-Maria Behnsen *
2089f73d9257SAnna-Maria Behnsen * Stores the next pending local and global timer expiry values in the
2090f73d9257SAnna-Maria Behnsen * struct pointed to by @tevt. If a queue is empty the corresponding
2091f73d9257SAnna-Maria Behnsen * field is set to KTIME_MAX. If local event expires before global
2092f73d9257SAnna-Maria Behnsen * event, global event is set to KTIME_MAX as well.
2093f73d9257SAnna-Maria Behnsen *
2094f73d9257SAnna-Maria Behnsen * Caller needs to make sure timer base locks are held (use
2095f73d9257SAnna-Maria Behnsen * timer_lock_remote_bases() for this purpose).
2096f73d9257SAnna-Maria Behnsen */
fetch_next_timer_interrupt_remote(unsigned long basej,u64 basem,struct timer_events * tevt,unsigned int cpu)2097f73d9257SAnna-Maria Behnsen void fetch_next_timer_interrupt_remote(unsigned long basej, u64 basem,
2098f73d9257SAnna-Maria Behnsen struct timer_events *tevt,
2099f73d9257SAnna-Maria Behnsen unsigned int cpu)
2100f73d9257SAnna-Maria Behnsen {
2101f73d9257SAnna-Maria Behnsen struct timer_base *base_local, *base_global;
2102f73d9257SAnna-Maria Behnsen
2103f73d9257SAnna-Maria Behnsen /* Preset local / global events */
2104f73d9257SAnna-Maria Behnsen tevt->local = tevt->global = KTIME_MAX;
2105f73d9257SAnna-Maria Behnsen
2106f73d9257SAnna-Maria Behnsen base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu);
2107f73d9257SAnna-Maria Behnsen base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu);
2108f73d9257SAnna-Maria Behnsen
2109f73d9257SAnna-Maria Behnsen lockdep_assert_held(&base_local->lock);
2110f73d9257SAnna-Maria Behnsen lockdep_assert_held(&base_global->lock);
2111f73d9257SAnna-Maria Behnsen
2112f73d9257SAnna-Maria Behnsen fetch_next_timer_interrupt(basej, basem, base_local, base_global, tevt);
2113f73d9257SAnna-Maria Behnsen }
2114f73d9257SAnna-Maria Behnsen
2115f73d9257SAnna-Maria Behnsen /**
2116f73d9257SAnna-Maria Behnsen * timer_unlock_remote_bases - unlock timer bases of cpu
2117f73d9257SAnna-Maria Behnsen * @cpu: Remote CPU
2118f73d9257SAnna-Maria Behnsen *
2119f73d9257SAnna-Maria Behnsen * Unlocks the remote timer bases.
2120f73d9257SAnna-Maria Behnsen */
timer_unlock_remote_bases(unsigned int cpu)2121f73d9257SAnna-Maria Behnsen void timer_unlock_remote_bases(unsigned int cpu)
2122f73d9257SAnna-Maria Behnsen __releases(timer_bases[BASE_LOCAL]->lock)
2123f73d9257SAnna-Maria Behnsen __releases(timer_bases[BASE_GLOBAL]->lock)
2124f73d9257SAnna-Maria Behnsen {
2125f73d9257SAnna-Maria Behnsen struct timer_base *base_local, *base_global;
2126f73d9257SAnna-Maria Behnsen
2127f73d9257SAnna-Maria Behnsen base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu);
2128f73d9257SAnna-Maria Behnsen base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu);
2129f73d9257SAnna-Maria Behnsen
2130f73d9257SAnna-Maria Behnsen raw_spin_unlock(&base_global->lock);
2131f73d9257SAnna-Maria Behnsen raw_spin_unlock(&base_local->lock);
2132f73d9257SAnna-Maria Behnsen }
2133f73d9257SAnna-Maria Behnsen
2134f73d9257SAnna-Maria Behnsen /**
2135f73d9257SAnna-Maria Behnsen * timer_lock_remote_bases - lock timer bases of cpu
2136f73d9257SAnna-Maria Behnsen * @cpu: Remote CPU
2137f73d9257SAnna-Maria Behnsen *
2138f73d9257SAnna-Maria Behnsen * Locks the remote timer bases.
2139f73d9257SAnna-Maria Behnsen */
timer_lock_remote_bases(unsigned int cpu)2140f73d9257SAnna-Maria Behnsen void timer_lock_remote_bases(unsigned int cpu)
2141f73d9257SAnna-Maria Behnsen __acquires(timer_bases[BASE_LOCAL]->lock)
2142f73d9257SAnna-Maria Behnsen __acquires(timer_bases[BASE_GLOBAL]->lock)
2143f73d9257SAnna-Maria Behnsen {
2144f73d9257SAnna-Maria Behnsen struct timer_base *base_local, *base_global;
2145f73d9257SAnna-Maria Behnsen
2146f73d9257SAnna-Maria Behnsen base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu);
2147f73d9257SAnna-Maria Behnsen base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu);
2148f73d9257SAnna-Maria Behnsen
2149f73d9257SAnna-Maria Behnsen lockdep_assert_irqs_disabled();
2150f73d9257SAnna-Maria Behnsen
2151f73d9257SAnna-Maria Behnsen raw_spin_lock(&base_local->lock);
2152f73d9257SAnna-Maria Behnsen raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING);
2153f73d9257SAnna-Maria Behnsen }
215457e95a5cSAnna-Maria Behnsen
215557e95a5cSAnna-Maria Behnsen /**
215657e95a5cSAnna-Maria Behnsen * timer_base_is_idle() - Return whether timer base is set idle
215757e95a5cSAnna-Maria Behnsen *
215857e95a5cSAnna-Maria Behnsen * Returns value of local timer base is_idle value.
215957e95a5cSAnna-Maria Behnsen */
timer_base_is_idle(void)216057e95a5cSAnna-Maria Behnsen bool timer_base_is_idle(void)
216157e95a5cSAnna-Maria Behnsen {
216257e95a5cSAnna-Maria Behnsen return __this_cpu_read(timer_bases[BASE_LOCAL].is_idle);
216357e95a5cSAnna-Maria Behnsen }
21647ee98877SAnna-Maria Behnsen
21657ee98877SAnna-Maria Behnsen static void __run_timer_base(struct timer_base *base);
21667ee98877SAnna-Maria Behnsen
21677ee98877SAnna-Maria Behnsen /**
21687ee98877SAnna-Maria Behnsen * timer_expire_remote() - expire global timers of cpu
21697ee98877SAnna-Maria Behnsen * @cpu: Remote CPU
21707ee98877SAnna-Maria Behnsen *
21717ee98877SAnna-Maria Behnsen * Expire timers of global base of remote CPU.
21727ee98877SAnna-Maria Behnsen */
timer_expire_remote(unsigned int cpu)21737ee98877SAnna-Maria Behnsen void timer_expire_remote(unsigned int cpu)
21747ee98877SAnna-Maria Behnsen {
21757ee98877SAnna-Maria Behnsen struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu);
21767ee98877SAnna-Maria Behnsen
21777ee98877SAnna-Maria Behnsen __run_timer_base(base);
21787ee98877SAnna-Maria Behnsen }
21797ee98877SAnna-Maria Behnsen
timer_use_tmigr(unsigned long basej,u64 basem,unsigned long * nextevt,bool * tick_stop_path,bool timer_base_idle,struct timer_events * tevt)21807ee98877SAnna-Maria Behnsen static void timer_use_tmigr(unsigned long basej, u64 basem,
21817ee98877SAnna-Maria Behnsen unsigned long *nextevt, bool *tick_stop_path,
21827ee98877SAnna-Maria Behnsen bool timer_base_idle, struct timer_events *tevt)
21837ee98877SAnna-Maria Behnsen {
21847ee98877SAnna-Maria Behnsen u64 next_tmigr;
21857ee98877SAnna-Maria Behnsen
21867ee98877SAnna-Maria Behnsen if (timer_base_idle)
21877ee98877SAnna-Maria Behnsen next_tmigr = tmigr_cpu_new_timer(tevt->global);
21887ee98877SAnna-Maria Behnsen else if (tick_stop_path)
21897ee98877SAnna-Maria Behnsen next_tmigr = tmigr_cpu_deactivate(tevt->global);
21907ee98877SAnna-Maria Behnsen else
21917ee98877SAnna-Maria Behnsen next_tmigr = tmigr_quick_check(tevt->global);
21927ee98877SAnna-Maria Behnsen
21937ee98877SAnna-Maria Behnsen /*
21947ee98877SAnna-Maria Behnsen * If the CPU is the last going idle in timer migration hierarchy, make
21957ee98877SAnna-Maria Behnsen * sure the CPU will wake up in time to handle remote timers.
21967ee98877SAnna-Maria Behnsen * next_tmigr == KTIME_MAX if other CPUs are still active.
21977ee98877SAnna-Maria Behnsen */
21987ee98877SAnna-Maria Behnsen if (next_tmigr < tevt->local) {
21997ee98877SAnna-Maria Behnsen u64 tmp;
22007ee98877SAnna-Maria Behnsen
22017ee98877SAnna-Maria Behnsen /* If we missed a tick already, force 0 delta */
22027ee98877SAnna-Maria Behnsen if (next_tmigr < basem)
22037ee98877SAnna-Maria Behnsen next_tmigr = basem;
22047ee98877SAnna-Maria Behnsen
22057ee98877SAnna-Maria Behnsen tmp = div_u64(next_tmigr - basem, TICK_NSEC);
22067ee98877SAnna-Maria Behnsen
22077ee98877SAnna-Maria Behnsen *nextevt = basej + (unsigned long)tmp;
22087ee98877SAnna-Maria Behnsen tevt->local = next_tmigr;
22097ee98877SAnna-Maria Behnsen }
22107ee98877SAnna-Maria Behnsen }
22117ee98877SAnna-Maria Behnsen # else
timer_use_tmigr(unsigned long basej,u64 basem,unsigned long * nextevt,bool * tick_stop_path,bool timer_base_idle,struct timer_events * tevt)22127ee98877SAnna-Maria Behnsen static void timer_use_tmigr(unsigned long basej, u64 basem,
22137ee98877SAnna-Maria Behnsen unsigned long *nextevt, bool *tick_stop_path,
22147ee98877SAnna-Maria Behnsen bool timer_base_idle, struct timer_events *tevt)
22157ee98877SAnna-Maria Behnsen {
22167ee98877SAnna-Maria Behnsen /*
22177ee98877SAnna-Maria Behnsen * Make sure first event is written into tevt->local to not miss a
22187ee98877SAnna-Maria Behnsen * timer on !SMP systems.
22197ee98877SAnna-Maria Behnsen */
22207ee98877SAnna-Maria Behnsen tevt->local = min_t(u64, tevt->local, tevt->global);
22217ee98877SAnna-Maria Behnsen }
2222f73d9257SAnna-Maria Behnsen # endif /* CONFIG_SMP */
2223f73d9257SAnna-Maria Behnsen
__get_next_timer_interrupt(unsigned long basej,u64 basem,bool * idle)2224e2e1d724SAnna-Maria Behnsen static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,
2225e2e1d724SAnna-Maria Behnsen bool *idle)
22265cee9645SThomas Gleixner {
222721927fc8SAnna-Maria Behnsen struct timer_events tevt = { .local = KTIME_MAX, .global = KTIME_MAX };
222883a665dcSAnna-Maria Behnsen struct timer_base *base_local, *base_global;
222970b4cf84SAnna-Maria Behnsen unsigned long nextevt;
22307ee98877SAnna-Maria Behnsen bool idle_is_possible;
22315cee9645SThomas Gleixner
22325cee9645SThomas Gleixner /*
223319b344a9SFrederic Weisbecker * When the CPU is offline, the tick is cancelled and nothing is supposed
223419b344a9SFrederic Weisbecker * to try to stop it.
22355cee9645SThomas Gleixner */
223619b344a9SFrederic Weisbecker if (WARN_ON_ONCE(cpu_is_offline(smp_processor_id()))) {
2237e2e1d724SAnna-Maria Behnsen if (idle)
2238e2e1d724SAnna-Maria Behnsen *idle = true;
223921927fc8SAnna-Maria Behnsen return tevt.local;
2240e2e1d724SAnna-Maria Behnsen }
22415cee9645SThomas Gleixner
224283a665dcSAnna-Maria Behnsen base_local = this_cpu_ptr(&timer_bases[BASE_LOCAL]);
224383a665dcSAnna-Maria Behnsen base_global = this_cpu_ptr(&timer_bases[BASE_GLOBAL]);
224431cd0e11SFrederic Weisbecker
224583a665dcSAnna-Maria Behnsen raw_spin_lock(&base_local->lock);
224683a665dcSAnna-Maria Behnsen raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING);
224783a665dcSAnna-Maria Behnsen
224870b4cf84SAnna-Maria Behnsen nextevt = fetch_next_timer_interrupt(basej, basem, base_local,
224970b4cf84SAnna-Maria Behnsen base_global, &tevt);
225083a665dcSAnna-Maria Behnsen
225121927fc8SAnna-Maria Behnsen /*
2252bd7c8ff9SAnna-Maria Behnsen * If the next event is only one jiffy ahead there is no need to call
22537ee98877SAnna-Maria Behnsen * timer migration hierarchy related functions. The value for the next
22547ee98877SAnna-Maria Behnsen * global timer in @tevt struct equals then KTIME_MAX. This is also
22557ee98877SAnna-Maria Behnsen * true, when the timer base is idle.
22567ee98877SAnna-Maria Behnsen *
22577ee98877SAnna-Maria Behnsen * The proper timer migration hierarchy function depends on the callsite
22587ee98877SAnna-Maria Behnsen * and whether timer base is idle or not. @nextevt will be updated when
22597ee98877SAnna-Maria Behnsen * this CPU needs to handle the first timer migration hierarchy
22607ee98877SAnna-Maria Behnsen * event. See timer_use_tmigr() for detailed information.
22617ee98877SAnna-Maria Behnsen */
22627ee98877SAnna-Maria Behnsen idle_is_possible = time_after(nextevt, basej + 1);
22637ee98877SAnna-Maria Behnsen if (idle_is_possible)
22647ee98877SAnna-Maria Behnsen timer_use_tmigr(basej, basem, &nextevt, idle,
22657ee98877SAnna-Maria Behnsen base_local->is_idle, &tevt);
22667ee98877SAnna-Maria Behnsen
22677ee98877SAnna-Maria Behnsen /*
2268bebed664SAnna-Maria Behnsen * We have a fresh next event. Check whether we can forward the
2269bebed664SAnna-Maria Behnsen * base.
2270bebed664SAnna-Maria Behnsen */
227183a665dcSAnna-Maria Behnsen __forward_timer_base(base_local, basej);
227283a665dcSAnna-Maria Behnsen __forward_timer_base(base_global, basej);
2273bebed664SAnna-Maria Behnsen
2274bebed664SAnna-Maria Behnsen /*
2275e2e1d724SAnna-Maria Behnsen * Set base->is_idle only when caller is timer_base_try_to_set_idle()
2276e2e1d724SAnna-Maria Behnsen */
2277e2e1d724SAnna-Maria Behnsen if (idle) {
2278e2e1d724SAnna-Maria Behnsen /*
22797ee98877SAnna-Maria Behnsen * Bases are idle if the next event is more than a tick
22807ee98877SAnna-Maria Behnsen * away. Caution: @nextevt could have changed by enqueueing a
22817ee98877SAnna-Maria Behnsen * global timer into timer migration hierarchy. Therefore a new
22827ee98877SAnna-Maria Behnsen * check is required here.
2283bb8caad5SThomas Gleixner *
2284e2e1d724SAnna-Maria Behnsen * If the base is marked idle then any timer add operation must
2285e2e1d724SAnna-Maria Behnsen * forward the base clk itself to keep granularity small. This
228683a665dcSAnna-Maria Behnsen * idle logic is only maintained for the BASE_LOCAL and
228783a665dcSAnna-Maria Behnsen * BASE_GLOBAL base, deferrable timers may still see large
228883a665dcSAnna-Maria Behnsen * granularity skew (by design).
2289a683f390SThomas Gleixner */
229083a665dcSAnna-Maria Behnsen if (!base_local->is_idle && time_after(nextevt, basej + 1)) {
2291b2cf7507SAnna-Maria Behnsen base_local->is_idle = true;
229203877039SFrederic Weisbecker /*
229303877039SFrederic Weisbecker * Global timers queued locally while running in a task
229403877039SFrederic Weisbecker * in nohz_full mode need a self-IPI to kick reprogramming
229503877039SFrederic Weisbecker * in IRQ tail.
229603877039SFrederic Weisbecker */
229703877039SFrederic Weisbecker if (tick_nohz_full_cpu(base_local->cpu))
229803877039SFrederic Weisbecker base_global->is_idle = true;
229983a665dcSAnna-Maria Behnsen trace_timer_base_idle(true, base_local->cpu);
2300e2e1d724SAnna-Maria Behnsen }
230183a665dcSAnna-Maria Behnsen *idle = base_local->is_idle;
23027ee98877SAnna-Maria Behnsen
23037ee98877SAnna-Maria Behnsen /*
23047ee98877SAnna-Maria Behnsen * When timer base is not set idle, undo the effect of
23059e643ab5SRandy Dunlap * tmigr_cpu_deactivate() to prevent inconsistent states - active
23067ee98877SAnna-Maria Behnsen * timer base but inactive timer migration hierarchy.
23077ee98877SAnna-Maria Behnsen *
23087ee98877SAnna-Maria Behnsen * When timer base was already marked idle, nothing will be
23097ee98877SAnna-Maria Behnsen * changed here.
23107ee98877SAnna-Maria Behnsen */
23117ee98877SAnna-Maria Behnsen if (!base_local->is_idle && idle_is_possible)
23127ee98877SAnna-Maria Behnsen tmigr_cpu_activate();
2313e2e1d724SAnna-Maria Behnsen }
2314bb8caad5SThomas Gleixner
231583a665dcSAnna-Maria Behnsen raw_spin_unlock(&base_global->lock);
231683a665dcSAnna-Maria Behnsen raw_spin_unlock(&base_local->lock);
23175cee9645SThomas Gleixner
23187ee98877SAnna-Maria Behnsen return cmp_next_hrtimer_event(basem, tevt.local);
23195cee9645SThomas Gleixner }
232023696838SAnna-Maria Gleixner
2321a683f390SThomas Gleixner /**
232239ed699fSAnna-Maria Behnsen * get_next_timer_interrupt() - return the time (clock mono) of the next timer
232339ed699fSAnna-Maria Behnsen * @basej: base time jiffies
232439ed699fSAnna-Maria Behnsen * @basem: base time clock monotonic
232539ed699fSAnna-Maria Behnsen *
23267ee98877SAnna-Maria Behnsen * Returns the tick aligned clock monotonic time of the next pending timer or
23277ee98877SAnna-Maria Behnsen * KTIME_MAX if no timer is pending. If timer of global base was queued into
23287ee98877SAnna-Maria Behnsen * timer migration hierarchy, first global timer is not taken into account. If
23297ee98877SAnna-Maria Behnsen * it was the last CPU of timer migration hierarchy going idle, first global
23307ee98877SAnna-Maria Behnsen * event is taken into account.
233139ed699fSAnna-Maria Behnsen */
get_next_timer_interrupt(unsigned long basej,u64 basem)233239ed699fSAnna-Maria Behnsen u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
233339ed699fSAnna-Maria Behnsen {
2334e2e1d724SAnna-Maria Behnsen return __get_next_timer_interrupt(basej, basem, NULL);
2335e2e1d724SAnna-Maria Behnsen }
2336e2e1d724SAnna-Maria Behnsen
2337e2e1d724SAnna-Maria Behnsen /**
2338e2e1d724SAnna-Maria Behnsen * timer_base_try_to_set_idle() - Try to set the idle state of the timer bases
2339e2e1d724SAnna-Maria Behnsen * @basej: base time jiffies
2340e2e1d724SAnna-Maria Behnsen * @basem: base time clock monotonic
234173129cf4SAnna-Maria Behnsen * @idle: pointer to store the value of timer_base->is_idle on return;
234273129cf4SAnna-Maria Behnsen * *idle contains the information whether tick was already stopped
2343e2e1d724SAnna-Maria Behnsen *
234473129cf4SAnna-Maria Behnsen * Returns the tick aligned clock monotonic time of the next pending timer or
234573129cf4SAnna-Maria Behnsen * KTIME_MAX if no timer is pending. When tick was already stopped KTIME_MAX is
234673129cf4SAnna-Maria Behnsen * returned as well.
2347e2e1d724SAnna-Maria Behnsen */
timer_base_try_to_set_idle(unsigned long basej,u64 basem,bool * idle)2348e2e1d724SAnna-Maria Behnsen u64 timer_base_try_to_set_idle(unsigned long basej, u64 basem, bool *idle)
2349e2e1d724SAnna-Maria Behnsen {
235073129cf4SAnna-Maria Behnsen if (*idle)
235173129cf4SAnna-Maria Behnsen return KTIME_MAX;
235273129cf4SAnna-Maria Behnsen
2353e2e1d724SAnna-Maria Behnsen return __get_next_timer_interrupt(basej, basem, idle);
235439ed699fSAnna-Maria Behnsen }
235539ed699fSAnna-Maria Behnsen
235639ed699fSAnna-Maria Behnsen /**
2357a683f390SThomas Gleixner * timer_clear_idle - Clear the idle state of the timer base
2358a683f390SThomas Gleixner *
2359a683f390SThomas Gleixner * Called with interrupts disabled
2360a683f390SThomas Gleixner */
timer_clear_idle(void)2361a683f390SThomas Gleixner void timer_clear_idle(void)
2362a683f390SThomas Gleixner {
2363a683f390SThomas Gleixner /*
2364b2cf7507SAnna-Maria Behnsen * We do this unlocked. The worst outcome is a remote pinned timer
2365b2cf7507SAnna-Maria Behnsen * enqueue sending a pointless IPI, but taking the lock would just
2366b2cf7507SAnna-Maria Behnsen * make the window for sending the IPI a few instructions smaller
2367b2cf7507SAnna-Maria Behnsen * for the cost of taking the lock in the exit from idle
2368b2cf7507SAnna-Maria Behnsen * path. Required for BASE_LOCAL only.
2369a683f390SThomas Gleixner */
237083a665dcSAnna-Maria Behnsen __this_cpu_write(timer_bases[BASE_LOCAL].is_idle, false);
237103877039SFrederic Weisbecker if (tick_nohz_full_cpu(smp_processor_id()))
237203877039SFrederic Weisbecker __this_cpu_write(timer_bases[BASE_GLOBAL].is_idle, false);
2373b573c731SAnna-Maria Behnsen trace_timer_base_idle(false, smp_processor_id());
23747ee98877SAnna-Maria Behnsen
23757ee98877SAnna-Maria Behnsen /* Activate without holding the timer_base->lock */
23767ee98877SAnna-Maria Behnsen tmigr_cpu_activate();
2377b573c731SAnna-Maria Behnsen }
23785cee9645SThomas Gleixner #endif
23795cee9645SThomas Gleixner
238073420feaSAnna-Maria Gleixner /**
238173420feaSAnna-Maria Gleixner * __run_timers - run all expired timers (if any) on this CPU.
238273420feaSAnna-Maria Gleixner * @base: the timer vector to be processed.
238373420feaSAnna-Maria Gleixner */
__run_timers(struct timer_base * base)238473420feaSAnna-Maria Gleixner static inline void __run_timers(struct timer_base *base)
238573420feaSAnna-Maria Gleixner {
238673420feaSAnna-Maria Gleixner struct hlist_head heads[LVL_DEPTH];
238773420feaSAnna-Maria Gleixner int levels;
238873420feaSAnna-Maria Gleixner
238990f5df66SRichard Cochran (linutronix GmbH) lockdep_assert_held(&base->lock);
239073420feaSAnna-Maria Gleixner
239189f01e10SAnna-Maria Behnsen if (base->running_timer)
239289f01e10SAnna-Maria Behnsen return;
239389f01e10SAnna-Maria Behnsen
2394d4f7dae8SFrederic Weisbecker while (time_after_eq(jiffies, base->clk) &&
2395d4f7dae8SFrederic Weisbecker time_after_eq(jiffies, base->next_expiry)) {
239673420feaSAnna-Maria Gleixner levels = collect_expired_timers(base, heads);
239731cd0e11SFrederic Weisbecker /*
2398c54bc0fcSAnna-Maria Behnsen * The two possible reasons for not finding any expired
2399c54bc0fcSAnna-Maria Behnsen * timer at this clk are that all matching timers have been
2400c54bc0fcSAnna-Maria Behnsen * dequeued or no timer has been queued since
2401c54bc0fcSAnna-Maria Behnsen * base::next_expiry was set to base::clk +
2402c54bc0fcSAnna-Maria Behnsen * NEXT_TIMER_MAX_DELTA.
240331cd0e11SFrederic Weisbecker */
2404c54bc0fcSAnna-Maria Behnsen WARN_ON_ONCE(!levels && !base->next_expiry_recalc
2405c54bc0fcSAnna-Maria Behnsen && base->timers_pending);
24068a2c9c7eSAnna-Maria Behnsen /*
24078a2c9c7eSAnna-Maria Behnsen * While executing timers, base->clk is set 1 offset ahead of
24088a2c9c7eSAnna-Maria Behnsen * jiffies to avoid endless requeuing to current jiffies.
24098a2c9c7eSAnna-Maria Behnsen */
241073420feaSAnna-Maria Gleixner base->clk++;
2411fe90c5baSAnna-Maria Behnsen timer_recalc_next_expiry(base);
241273420feaSAnna-Maria Gleixner
241373420feaSAnna-Maria Gleixner while (levels--)
241473420feaSAnna-Maria Gleixner expire_timers(base, heads + levels);
241573420feaSAnna-Maria Gleixner }
241690f5df66SRichard Cochran (linutronix GmbH) }
241790f5df66SRichard Cochran (linutronix GmbH)
__run_timer_base(struct timer_base * base)241890f5df66SRichard Cochran (linutronix GmbH) static void __run_timer_base(struct timer_base *base)
241990f5df66SRichard Cochran (linutronix GmbH) {
24201d4199cbSThomas Gleixner /* Can race against a remote CPU updating next_expiry under the lock */
24211d4199cbSThomas Gleixner if (time_before(jiffies, READ_ONCE(base->next_expiry)))
242290f5df66SRichard Cochran (linutronix GmbH) return;
242390f5df66SRichard Cochran (linutronix GmbH)
242490f5df66SRichard Cochran (linutronix GmbH) timer_base_lock_expiry(base);
242590f5df66SRichard Cochran (linutronix GmbH) raw_spin_lock_irq(&base->lock);
242690f5df66SRichard Cochran (linutronix GmbH) __run_timers(base);
24272287d866SSebastian Andrzej Siewior raw_spin_unlock_irq(&base->lock);
2428030dcdd1SAnna-Maria Gleixner timer_base_unlock_expiry(base);
242973420feaSAnna-Maria Gleixner }
243073420feaSAnna-Maria Gleixner
run_timer_base(int index)243190f5df66SRichard Cochran (linutronix GmbH) static void run_timer_base(int index)
243290f5df66SRichard Cochran (linutronix GmbH) {
243390f5df66SRichard Cochran (linutronix GmbH) struct timer_base *base = this_cpu_ptr(&timer_bases[index]);
243490f5df66SRichard Cochran (linutronix GmbH)
243590f5df66SRichard Cochran (linutronix GmbH) __run_timer_base(base);
243690f5df66SRichard Cochran (linutronix GmbH) }
243790f5df66SRichard Cochran (linutronix GmbH)
24385cee9645SThomas Gleixner /*
24395cee9645SThomas Gleixner * This function runs timers and the timer-tq in bottom half context.
24405cee9645SThomas Gleixner */
run_timer_softirq(void)2441e68ac2b4SCaleb Sander Mateos static __latent_entropy void run_timer_softirq(void)
24425cee9645SThomas Gleixner {
244390f5df66SRichard Cochran (linutronix GmbH) run_timer_base(BASE_LOCAL);
244483a665dcSAnna-Maria Behnsen if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) {
244590f5df66SRichard Cochran (linutronix GmbH) run_timer_base(BASE_GLOBAL);
244690f5df66SRichard Cochran (linutronix GmbH) run_timer_base(BASE_DEF);
24477ee98877SAnna-Maria Behnsen
24487ee98877SAnna-Maria Behnsen if (is_timers_nohz_active())
24497ee98877SAnna-Maria Behnsen tmigr_handle_remote();
24505cee9645SThomas Gleixner }
245183a665dcSAnna-Maria Behnsen }
24525cee9645SThomas Gleixner
24535cee9645SThomas Gleixner /*
24545cee9645SThomas Gleixner * Called by the local, per-CPU timer interrupt on SMP.
24555cee9645SThomas Gleixner */
run_local_timers(void)2456cc947f2bSThomas Gleixner static void run_local_timers(void)
24575cee9645SThomas Gleixner {
245883a665dcSAnna-Maria Behnsen struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_LOCAL]);
24594e85876aSThomas Gleixner
24605cee9645SThomas Gleixner hrtimer_run_queues();
2461af68cb3fSAnna-Maria Behnsen
2462af68cb3fSAnna-Maria Behnsen for (int i = 0; i < NR_BASES; i++, base++) {
246379f8b28eSAnna-Maria Behnsen /*
246479f8b28eSAnna-Maria Behnsen * Raise the softirq only if required.
246579f8b28eSAnna-Maria Behnsen *
246679f8b28eSAnna-Maria Behnsen * timer_base::next_expiry can be written by a remote CPU while
246779f8b28eSAnna-Maria Behnsen * holding the lock. If this write happens at the same time than
246879f8b28eSAnna-Maria Behnsen * the lockless local read, sanity checker could complain about
246979f8b28eSAnna-Maria Behnsen * data corruption.
247079f8b28eSAnna-Maria Behnsen *
247179f8b28eSAnna-Maria Behnsen * There are two possible situations where
247279f8b28eSAnna-Maria Behnsen * timer_base::next_expiry is written by a remote CPU:
247379f8b28eSAnna-Maria Behnsen *
247479f8b28eSAnna-Maria Behnsen * 1. Remote CPU expires global timers of this CPU and updates
247579f8b28eSAnna-Maria Behnsen * timer_base::next_expiry of BASE_GLOBAL afterwards in
247679f8b28eSAnna-Maria Behnsen * next_timer_interrupt() or timer_recalc_next_expiry(). The
247779f8b28eSAnna-Maria Behnsen * worst outcome is a superfluous raise of the timer softirq
247879f8b28eSAnna-Maria Behnsen * when the not yet updated value is read.
247979f8b28eSAnna-Maria Behnsen *
248079f8b28eSAnna-Maria Behnsen * 2. A new first pinned timer is enqueued by a remote CPU
248179f8b28eSAnna-Maria Behnsen * and therefore timer_base::next_expiry of BASE_LOCAL is
248279f8b28eSAnna-Maria Behnsen * updated. When this update is missed, this isn't a
248379f8b28eSAnna-Maria Behnsen * problem, as an IPI is executed nevertheless when the CPU
248479f8b28eSAnna-Maria Behnsen * was idle before. When the CPU wasn't idle but the update
2485bd7c8ff9SAnna-Maria Behnsen * is missed, then the timer would expire one jiffy late -
248679f8b28eSAnna-Maria Behnsen * bad luck.
248779f8b28eSAnna-Maria Behnsen *
248879f8b28eSAnna-Maria Behnsen * Those unlikely corner cases where the worst outcome is only a
2489bd7c8ff9SAnna-Maria Behnsen * one jiffy delay or a superfluous raise of the softirq are
249079f8b28eSAnna-Maria Behnsen * not that expensive as doing the check always while holding
249179f8b28eSAnna-Maria Behnsen * the lock.
249279f8b28eSAnna-Maria Behnsen *
249379f8b28eSAnna-Maria Behnsen * Possible remote writers are using WRITE_ONCE(). Local reader
249479f8b28eSAnna-Maria Behnsen * uses therefore READ_ONCE().
249579f8b28eSAnna-Maria Behnsen */
249679f8b28eSAnna-Maria Behnsen if (time_after_eq(jiffies, READ_ONCE(base->next_expiry)) ||
24977ee98877SAnna-Maria Behnsen (i == BASE_DEF && tmigr_requires_handle_remote())) {
249849a17639SSebastian Andrzej Siewior raise_timer_softirq(TIMER_SOFTIRQ);
24994e85876aSThomas Gleixner return;
25004e85876aSThomas Gleixner }
2501af68cb3fSAnna-Maria Behnsen }
25025cee9645SThomas Gleixner }
25035cee9645SThomas Gleixner
250458e1177bSKees Cook /*
2505cc947f2bSThomas Gleixner * Called from the timer interrupt handler to charge one tick to the current
2506cc947f2bSThomas Gleixner * process. user_tick is 1 if the tick is user time, 0 for system.
2507cc947f2bSThomas Gleixner */
update_process_times(int user_tick)2508cc947f2bSThomas Gleixner void update_process_times(int user_tick)
2509cc947f2bSThomas Gleixner {
2510cc947f2bSThomas Gleixner struct task_struct *p = current;
2511cc947f2bSThomas Gleixner
2512cc947f2bSThomas Gleixner /* Note: this timer irq context must be accounted for as well. */
2513cc947f2bSThomas Gleixner account_process_tick(p, user_tick);
2514cc947f2bSThomas Gleixner run_local_timers();
2515cc947f2bSThomas Gleixner rcu_sched_clock_irq(user_tick);
2516cc947f2bSThomas Gleixner #ifdef CONFIG_IRQ_WORK
2517cc947f2bSThomas Gleixner if (in_irq())
2518cc947f2bSThomas Gleixner irq_work_tick();
2519cc947f2bSThomas Gleixner #endif
252086dd6c04SIngo Molnar sched_tick();
2521cc947f2bSThomas Gleixner if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2522cc947f2bSThomas Gleixner run_posix_cpu_timers();
2523cc947f2bSThomas Gleixner }
2524cc947f2bSThomas Gleixner
25255cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
migrate_timer_list(struct timer_base * new_base,struct hlist_head * head)2526494af3edSThomas Gleixner static void migrate_timer_list(struct timer_base *new_base, struct hlist_head *head)
25275cee9645SThomas Gleixner {
25285cee9645SThomas Gleixner struct timer_list *timer;
25290eeda71bSThomas Gleixner int cpu = new_base->cpu;
25305cee9645SThomas Gleixner
25311dabbcecSThomas Gleixner while (!hlist_empty(head)) {
25321dabbcecSThomas Gleixner timer = hlist_entry(head->first, struct timer_list, entry);
25335cee9645SThomas Gleixner detach_timer(timer, false);
25340eeda71bSThomas Gleixner timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
25355cee9645SThomas Gleixner internal_add_timer(new_base, timer);
25365cee9645SThomas Gleixner }
25375cee9645SThomas Gleixner }
25385cee9645SThomas Gleixner
timers_prepare_cpu(unsigned int cpu)253926456f87SThomas Gleixner int timers_prepare_cpu(unsigned int cpu)
254026456f87SThomas Gleixner {
254126456f87SThomas Gleixner struct timer_base *base;
254226456f87SThomas Gleixner int b;
254326456f87SThomas Gleixner
254426456f87SThomas Gleixner for (b = 0; b < NR_BASES; b++) {
254526456f87SThomas Gleixner base = per_cpu_ptr(&timer_bases[b], cpu);
254626456f87SThomas Gleixner base->clk = jiffies;
254726456f87SThomas Gleixner base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA;
25482731aa7dSAnna-Maria Behnsen base->next_expiry_recalc = false;
2549aebacb7fSNicolas Saenz Julienne base->timers_pending = false;
255026456f87SThomas Gleixner base->is_idle = false;
255126456f87SThomas Gleixner }
255226456f87SThomas Gleixner return 0;
255326456f87SThomas Gleixner }
255426456f87SThomas Gleixner
timers_dead_cpu(unsigned int cpu)255524f73b99SRichard Cochran int timers_dead_cpu(unsigned int cpu)
25565cee9645SThomas Gleixner {
2557494af3edSThomas Gleixner struct timer_base *old_base;
2558494af3edSThomas Gleixner struct timer_base *new_base;
2559500462a9SThomas Gleixner int b, i;
25605cee9645SThomas Gleixner
2561500462a9SThomas Gleixner for (b = 0; b < NR_BASES; b++) {
2562500462a9SThomas Gleixner old_base = per_cpu_ptr(&timer_bases[b], cpu);
2563500462a9SThomas Gleixner new_base = get_cpu_ptr(&timer_bases[b]);
25645cee9645SThomas Gleixner /*
25655cee9645SThomas Gleixner * The caller is globally serialized and nobody else
25665cee9645SThomas Gleixner * takes two locks at once, deadlock is not possible.
25675cee9645SThomas Gleixner */
25682287d866SSebastian Andrzej Siewior raw_spin_lock_irq(&new_base->lock);
25692287d866SSebastian Andrzej Siewior raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
25705cee9645SThomas Gleixner
2571c52232a4SLingutla Chandrasekhar /*
2572c52232a4SLingutla Chandrasekhar * The current CPUs base clock might be stale. Update it
2573c52232a4SLingutla Chandrasekhar * before moving the timers over.
2574c52232a4SLingutla Chandrasekhar */
2575c52232a4SLingutla Chandrasekhar forward_timer_base(new_base);
2576c52232a4SLingutla Chandrasekhar
257782ed6f7eSThomas Gleixner WARN_ON_ONCE(old_base->running_timer);
257882ed6f7eSThomas Gleixner old_base->running_timer = NULL;
25795cee9645SThomas Gleixner
2580500462a9SThomas Gleixner for (i = 0; i < WHEEL_SIZE; i++)
2581500462a9SThomas Gleixner migrate_timer_list(new_base, old_base->vectors + i);
25828def9060SViresh Kumar
25832287d866SSebastian Andrzej Siewior raw_spin_unlock(&old_base->lock);
25842287d866SSebastian Andrzej Siewior raw_spin_unlock_irq(&new_base->lock);
2585494af3edSThomas Gleixner put_cpu_ptr(&timer_bases);
25865cee9645SThomas Gleixner }
258724f73b99SRichard Cochran return 0;
25885cee9645SThomas Gleixner }
25895cee9645SThomas Gleixner
25903650b57fSPeter Zijlstra #endif /* CONFIG_HOTPLUG_CPU */
25915cee9645SThomas Gleixner
init_timer_cpu(int cpu)25920eeda71bSThomas Gleixner static void __init init_timer_cpu(int cpu)
25938def9060SViresh Kumar {
2594500462a9SThomas Gleixner struct timer_base *base;
2595500462a9SThomas Gleixner int i;
25963650b57fSPeter Zijlstra
2597500462a9SThomas Gleixner for (i = 0; i < NR_BASES; i++) {
2598500462a9SThomas Gleixner base = per_cpu_ptr(&timer_bases[i], cpu);
25998def9060SViresh Kumar base->cpu = cpu;
26002287d866SSebastian Andrzej Siewior raw_spin_lock_init(&base->lock);
2601494af3edSThomas Gleixner base->clk = jiffies;
2602dc2a0f1fSFrederic Weisbecker base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA;
2603030dcdd1SAnna-Maria Gleixner timer_base_init_expiry_lock(base);
2604500462a9SThomas Gleixner }
26058def9060SViresh Kumar }
26068def9060SViresh Kumar
init_timer_cpus(void)26078def9060SViresh Kumar static void __init init_timer_cpus(void)
26088def9060SViresh Kumar {
26098def9060SViresh Kumar int cpu;
26108def9060SViresh Kumar
26110eeda71bSThomas Gleixner for_each_possible_cpu(cpu)
26120eeda71bSThomas Gleixner init_timer_cpu(cpu);
26138def9060SViresh Kumar }
26145cee9645SThomas Gleixner
init_timers(void)26155cee9645SThomas Gleixner void __init init_timers(void)
26165cee9645SThomas Gleixner {
26178def9060SViresh Kumar init_timer_cpus();
26181fb497ddSThomas Gleixner posix_cputimers_init_work();
26195cee9645SThomas Gleixner open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
26205cee9645SThomas Gleixner }
2621