135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0 25cee9645SThomas Gleixner /* 35cee9645SThomas Gleixner * Kernel internal timers 45cee9645SThomas Gleixner * 55cee9645SThomas Gleixner * Copyright (C) 1991, 1992 Linus Torvalds 65cee9645SThomas Gleixner * 75cee9645SThomas Gleixner * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better. 85cee9645SThomas Gleixner * 95cee9645SThomas Gleixner * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 105cee9645SThomas Gleixner * "A Kernel Model for Precision Timekeeping" by Dave Mills 115cee9645SThomas Gleixner * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to 125cee9645SThomas Gleixner * serialize accesses to xtime/lost_ticks). 135cee9645SThomas Gleixner * Copyright (C) 1998 Andrea Arcangeli 145cee9645SThomas Gleixner * 1999-03-10 Improved NTP compatibility by Ulrich Windl 155cee9645SThomas Gleixner * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love 165cee9645SThomas Gleixner * 2000-10-05 Implemented scalable SMP per-CPU timer handling. 175cee9645SThomas Gleixner * Copyright (C) 2000, 2001, 2002 Ingo Molnar 185cee9645SThomas Gleixner * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar 195cee9645SThomas Gleixner */ 205cee9645SThomas Gleixner 215cee9645SThomas Gleixner #include <linux/kernel_stat.h> 225cee9645SThomas Gleixner #include <linux/export.h> 235cee9645SThomas Gleixner #include <linux/interrupt.h> 245cee9645SThomas Gleixner #include <linux/percpu.h> 255cee9645SThomas Gleixner #include <linux/init.h> 265cee9645SThomas Gleixner #include <linux/mm.h> 275cee9645SThomas Gleixner #include <linux/swap.h> 285cee9645SThomas Gleixner #include <linux/pid_namespace.h> 295cee9645SThomas Gleixner #include <linux/notifier.h> 305cee9645SThomas Gleixner #include <linux/thread_info.h> 315cee9645SThomas Gleixner #include <linux/time.h> 325cee9645SThomas Gleixner #include <linux/jiffies.h> 335cee9645SThomas Gleixner #include <linux/posix-timers.h> 345cee9645SThomas Gleixner #include <linux/cpu.h> 355cee9645SThomas Gleixner #include <linux/syscalls.h> 365cee9645SThomas Gleixner #include <linux/delay.h> 375cee9645SThomas Gleixner #include <linux/tick.h> 385cee9645SThomas Gleixner #include <linux/kallsyms.h> 395cee9645SThomas Gleixner #include <linux/irq_work.h> 40174cd4b1SIngo Molnar #include <linux/sched/signal.h> 415cee9645SThomas Gleixner #include <linux/sched/sysctl.h> 42370c9135SIngo Molnar #include <linux/sched/nohz.h> 43b17b0153SIngo Molnar #include <linux/sched/debug.h> 445cee9645SThomas Gleixner #include <linux/slab.h> 455cee9645SThomas Gleixner #include <linux/compat.h> 46f227e3ecSWilly Tarreau #include <linux/random.h> 47efaa0227Stangmeng #include <linux/sysctl.h> 485cee9645SThomas Gleixner 497c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 505cee9645SThomas Gleixner #include <asm/unistd.h> 515cee9645SThomas Gleixner #include <asm/div64.h> 525cee9645SThomas Gleixner #include <asm/timex.h> 535cee9645SThomas Gleixner #include <asm/io.h> 545cee9645SThomas Gleixner 55c1ad348bSThomas Gleixner #include "tick-internal.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 66500462a9SThomas Gleixner * LVL_SIZE buckets. Each level is driven by its own clock and therefor each 67500462a9SThomas Gleixner * level has a different granularity. 68500462a9SThomas Gleixner * 69500462a9SThomas Gleixner * The level granularity is: LVL_CLK_DIV ^ lvl 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 74500462a9SThomas Gleixner * therefor 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 190500462a9SThomas Gleixner # define NR_BASES 2 191500462a9SThomas Gleixner # define BASE_STD 0 192500462a9SThomas Gleixner # define BASE_DEF 1 193500462a9SThomas Gleixner #else 194500462a9SThomas Gleixner # define NR_BASES 1 195500462a9SThomas Gleixner # define BASE_STD 0 196500462a9SThomas Gleixner # define BASE_DEF 0 197500462a9SThomas Gleixner #endif 1985cee9645SThomas Gleixner 199494af3edSThomas Gleixner struct timer_base { 2002287d866SSebastian Andrzej Siewior raw_spinlock_t lock; 2015cee9645SThomas Gleixner struct timer_list *running_timer; 202030dcdd1SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT 203030dcdd1SAnna-Maria Gleixner spinlock_t expiry_lock; 204030dcdd1SAnna-Maria Gleixner atomic_t timer_waiters; 205030dcdd1SAnna-Maria Gleixner #endif 206494af3edSThomas Gleixner unsigned long clk; 207a683f390SThomas Gleixner unsigned long next_expiry; 208500462a9SThomas Gleixner unsigned int cpu; 20931cd0e11SFrederic Weisbecker bool next_expiry_recalc; 210a683f390SThomas Gleixner bool is_idle; 211aebacb7fSNicolas Saenz Julienne bool timers_pending; 212500462a9SThomas Gleixner DECLARE_BITMAP(pending_map, WHEEL_SIZE); 213500462a9SThomas Gleixner struct hlist_head vectors[WHEEL_SIZE]; 2145cee9645SThomas Gleixner } ____cacheline_aligned; 2155cee9645SThomas Gleixner 216500462a9SThomas Gleixner static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]); 2175cee9645SThomas Gleixner 218ae67badaSThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON 219ae67badaSThomas Gleixner 22014c80341SAnna-Maria Gleixner static DEFINE_STATIC_KEY_FALSE(timers_nohz_active); 221ae67badaSThomas Gleixner static DEFINE_MUTEX(timer_keys_mutex); 222ae67badaSThomas Gleixner 223ae67badaSThomas Gleixner static void timer_update_keys(struct work_struct *work); 224ae67badaSThomas Gleixner static DECLARE_WORK(timer_update_work, timer_update_keys); 225ae67badaSThomas Gleixner 226ae67badaSThomas Gleixner #ifdef CONFIG_SMP 227efaa0227Stangmeng static unsigned int sysctl_timer_migration = 1; 228bc7a34b8SThomas Gleixner 229ae67badaSThomas Gleixner DEFINE_STATIC_KEY_FALSE(timers_migration_enabled); 230ae67badaSThomas Gleixner 231ae67badaSThomas Gleixner static void timers_update_migration(void) 232bc7a34b8SThomas Gleixner { 233ae67badaSThomas Gleixner if (sysctl_timer_migration && tick_nohz_active) 234ae67badaSThomas Gleixner static_branch_enable(&timers_migration_enabled); 235ae67badaSThomas Gleixner else 236ae67badaSThomas Gleixner static_branch_disable(&timers_migration_enabled); 237bc7a34b8SThomas Gleixner } 238efaa0227Stangmeng 239efaa0227Stangmeng #ifdef CONFIG_SYSCTL 240efaa0227Stangmeng static int timer_migration_handler(struct ctl_table *table, int write, 241efaa0227Stangmeng void *buffer, size_t *lenp, loff_t *ppos) 242efaa0227Stangmeng { 243efaa0227Stangmeng int ret; 244efaa0227Stangmeng 245efaa0227Stangmeng mutex_lock(&timer_keys_mutex); 246efaa0227Stangmeng ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 247efaa0227Stangmeng if (!ret && write) 248efaa0227Stangmeng timers_update_migration(); 249efaa0227Stangmeng mutex_unlock(&timer_keys_mutex); 250efaa0227Stangmeng return ret; 251efaa0227Stangmeng } 252efaa0227Stangmeng 253efaa0227Stangmeng static struct ctl_table timer_sysctl[] = { 254efaa0227Stangmeng { 255efaa0227Stangmeng .procname = "timer_migration", 256efaa0227Stangmeng .data = &sysctl_timer_migration, 257efaa0227Stangmeng .maxlen = sizeof(unsigned int), 258efaa0227Stangmeng .mode = 0644, 259efaa0227Stangmeng .proc_handler = timer_migration_handler, 260efaa0227Stangmeng .extra1 = SYSCTL_ZERO, 261efaa0227Stangmeng .extra2 = SYSCTL_ONE, 262efaa0227Stangmeng }, 263efaa0227Stangmeng {} 264efaa0227Stangmeng }; 265efaa0227Stangmeng 266efaa0227Stangmeng static int __init timer_sysctl_init(void) 267efaa0227Stangmeng { 268efaa0227Stangmeng register_sysctl("kernel", timer_sysctl); 269efaa0227Stangmeng return 0; 270efaa0227Stangmeng } 271efaa0227Stangmeng device_initcall(timer_sysctl_init); 272efaa0227Stangmeng #endif /* CONFIG_SYSCTL */ 273efaa0227Stangmeng #else /* CONFIG_SMP */ 274ae67badaSThomas Gleixner static inline void timers_update_migration(void) { } 275ae67badaSThomas Gleixner #endif /* !CONFIG_SMP */ 276ae67badaSThomas Gleixner 277ae67badaSThomas Gleixner static void timer_update_keys(struct work_struct *work) 278ae67badaSThomas Gleixner { 279ae67badaSThomas Gleixner mutex_lock(&timer_keys_mutex); 280ae67badaSThomas Gleixner timers_update_migration(); 281ae67badaSThomas Gleixner static_branch_enable(&timers_nohz_active); 282ae67badaSThomas Gleixner mutex_unlock(&timer_keys_mutex); 283ae67badaSThomas Gleixner } 284ae67badaSThomas Gleixner 285ae67badaSThomas Gleixner void timers_update_nohz(void) 286ae67badaSThomas Gleixner { 287ae67badaSThomas Gleixner schedule_work(&timer_update_work); 288bc7a34b8SThomas Gleixner } 289bc7a34b8SThomas Gleixner 29014c80341SAnna-Maria Gleixner static inline bool is_timers_nohz_active(void) 29114c80341SAnna-Maria Gleixner { 29214c80341SAnna-Maria Gleixner return static_branch_unlikely(&timers_nohz_active); 29314c80341SAnna-Maria Gleixner } 29414c80341SAnna-Maria Gleixner #else 29514c80341SAnna-Maria Gleixner static inline bool is_timers_nohz_active(void) { return false; } 296ae67badaSThomas Gleixner #endif /* NO_HZ_COMMON */ 297bc7a34b8SThomas Gleixner 2985cee9645SThomas Gleixner static unsigned long round_jiffies_common(unsigned long j, int cpu, 2995cee9645SThomas Gleixner bool force_up) 3005cee9645SThomas Gleixner { 3015cee9645SThomas Gleixner int rem; 3025cee9645SThomas Gleixner unsigned long original = j; 3035cee9645SThomas Gleixner 3045cee9645SThomas Gleixner /* 3055cee9645SThomas Gleixner * We don't want all cpus firing their timers at once hitting the 3065cee9645SThomas Gleixner * same lock or cachelines, so we skew each extra cpu with an extra 3075cee9645SThomas Gleixner * 3 jiffies. This 3 jiffies came originally from the mm/ code which 3085cee9645SThomas Gleixner * already did this. 3095cee9645SThomas Gleixner * The skew is done by adding 3*cpunr, then round, then subtract this 3105cee9645SThomas Gleixner * extra offset again. 3115cee9645SThomas Gleixner */ 3125cee9645SThomas Gleixner j += cpu * 3; 3135cee9645SThomas Gleixner 3145cee9645SThomas Gleixner rem = j % HZ; 3155cee9645SThomas Gleixner 3165cee9645SThomas Gleixner /* 3175cee9645SThomas Gleixner * If the target jiffie is just after a whole second (which can happen 3185cee9645SThomas Gleixner * due to delays of the timer irq, long irq off times etc etc) then 3195cee9645SThomas Gleixner * we should round down to the whole second, not up. Use 1/4th second 3205cee9645SThomas Gleixner * as cutoff for this rounding as an extreme upper bound for this. 3215cee9645SThomas Gleixner * But never round down if @force_up is set. 3225cee9645SThomas Gleixner */ 3235cee9645SThomas Gleixner if (rem < HZ/4 && !force_up) /* round down */ 3245cee9645SThomas Gleixner j = j - rem; 3255cee9645SThomas Gleixner else /* round up */ 3265cee9645SThomas Gleixner j = j - rem + HZ; 3275cee9645SThomas Gleixner 3285cee9645SThomas Gleixner /* now that we have rounded, subtract the extra skew again */ 3295cee9645SThomas Gleixner j -= cpu * 3; 3305cee9645SThomas Gleixner 3315cee9645SThomas Gleixner /* 3325cee9645SThomas Gleixner * Make sure j is still in the future. Otherwise return the 3335cee9645SThomas Gleixner * unmodified value. 3345cee9645SThomas Gleixner */ 3355cee9645SThomas Gleixner return time_is_after_jiffies(j) ? j : original; 3365cee9645SThomas Gleixner } 3375cee9645SThomas Gleixner 3385cee9645SThomas Gleixner /** 3395cee9645SThomas Gleixner * __round_jiffies - function to round jiffies to a full second 3405cee9645SThomas Gleixner * @j: the time in (absolute) jiffies that should be rounded 3415cee9645SThomas Gleixner * @cpu: the processor number on which the timeout will happen 3425cee9645SThomas Gleixner * 3435cee9645SThomas Gleixner * __round_jiffies() rounds an absolute time in the future (in jiffies) 3445cee9645SThomas Gleixner * up or down to (approximately) full seconds. This is useful for timers 3455cee9645SThomas Gleixner * for which the exact time they fire does not matter too much, as long as 3465cee9645SThomas Gleixner * they fire approximately every X seconds. 3475cee9645SThomas Gleixner * 3485cee9645SThomas Gleixner * By rounding these timers to whole seconds, all such timers will fire 3495cee9645SThomas Gleixner * at the same time, rather than at various times spread out. The goal 3505cee9645SThomas Gleixner * of this is to have the CPU wake up less, which saves power. 3515cee9645SThomas Gleixner * 3525cee9645SThomas Gleixner * The exact rounding is skewed for each processor to avoid all 3535cee9645SThomas Gleixner * processors firing at the exact same time, which could lead 3545cee9645SThomas Gleixner * to lock contention or spurious cache line bouncing. 3555cee9645SThomas Gleixner * 3565cee9645SThomas Gleixner * The return value is the rounded version of the @j parameter. 3575cee9645SThomas Gleixner */ 3585cee9645SThomas Gleixner unsigned long __round_jiffies(unsigned long j, int cpu) 3595cee9645SThomas Gleixner { 3605cee9645SThomas Gleixner return round_jiffies_common(j, cpu, false); 3615cee9645SThomas Gleixner } 3625cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies); 3635cee9645SThomas Gleixner 3645cee9645SThomas Gleixner /** 3655cee9645SThomas Gleixner * __round_jiffies_relative - function to round jiffies to a full second 3665cee9645SThomas Gleixner * @j: the time in (relative) jiffies that should be rounded 3675cee9645SThomas Gleixner * @cpu: the processor number on which the timeout will happen 3685cee9645SThomas Gleixner * 3695cee9645SThomas Gleixner * __round_jiffies_relative() rounds a time delta in the future (in jiffies) 3705cee9645SThomas Gleixner * up or down to (approximately) full seconds. This is useful for timers 3715cee9645SThomas Gleixner * for which the exact time they fire does not matter too much, as long as 3725cee9645SThomas Gleixner * they fire approximately every X seconds. 3735cee9645SThomas Gleixner * 3745cee9645SThomas Gleixner * By rounding these timers to whole seconds, all such timers will fire 3755cee9645SThomas Gleixner * at the same time, rather than at various times spread out. The goal 3765cee9645SThomas Gleixner * of this is to have the CPU wake up less, which saves power. 3775cee9645SThomas Gleixner * 3785cee9645SThomas Gleixner * The exact rounding is skewed for each processor to avoid all 3795cee9645SThomas Gleixner * processors firing at the exact same time, which could lead 3805cee9645SThomas Gleixner * to lock contention or spurious cache line bouncing. 3815cee9645SThomas Gleixner * 3825cee9645SThomas Gleixner * The return value is the rounded version of the @j parameter. 3835cee9645SThomas Gleixner */ 3845cee9645SThomas Gleixner unsigned long __round_jiffies_relative(unsigned long j, int cpu) 3855cee9645SThomas Gleixner { 3865cee9645SThomas Gleixner unsigned long j0 = jiffies; 3875cee9645SThomas Gleixner 3885cee9645SThomas Gleixner /* Use j0 because jiffies might change while we run */ 3895cee9645SThomas Gleixner return round_jiffies_common(j + j0, cpu, false) - j0; 3905cee9645SThomas Gleixner } 3915cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_relative); 3925cee9645SThomas Gleixner 3935cee9645SThomas Gleixner /** 3945cee9645SThomas Gleixner * round_jiffies - function to round jiffies to a full second 3955cee9645SThomas Gleixner * @j: the time in (absolute) jiffies that should be rounded 3965cee9645SThomas Gleixner * 3975cee9645SThomas Gleixner * round_jiffies() rounds an absolute time in the future (in jiffies) 3985cee9645SThomas Gleixner * up or down to (approximately) full seconds. This is useful for timers 3995cee9645SThomas Gleixner * for which the exact time they fire does not matter too much, as long as 4005cee9645SThomas Gleixner * they fire approximately every X seconds. 4015cee9645SThomas Gleixner * 4025cee9645SThomas Gleixner * By rounding these timers to whole seconds, all such timers will fire 4035cee9645SThomas Gleixner * at the same time, rather than at various times spread out. The goal 4045cee9645SThomas Gleixner * of this is to have the CPU wake up less, which saves power. 4055cee9645SThomas Gleixner * 4065cee9645SThomas Gleixner * The return value is the rounded version of the @j parameter. 4075cee9645SThomas Gleixner */ 4085cee9645SThomas Gleixner unsigned long round_jiffies(unsigned long j) 4095cee9645SThomas Gleixner { 4105cee9645SThomas Gleixner return round_jiffies_common(j, raw_smp_processor_id(), 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 * 4185cee9645SThomas Gleixner * round_jiffies_relative() rounds a time delta in the future (in jiffies) 4195cee9645SThomas Gleixner * up or down to (approximately) full seconds. This is useful for timers 4205cee9645SThomas Gleixner * for which the exact time they fire does not matter too much, as long as 4215cee9645SThomas Gleixner * they fire approximately every X seconds. 4225cee9645SThomas Gleixner * 4235cee9645SThomas Gleixner * By rounding these timers to whole seconds, all such timers will fire 4245cee9645SThomas Gleixner * at the same time, rather than at various times spread out. The goal 4255cee9645SThomas Gleixner * of this is to have the CPU wake up less, which saves power. 4265cee9645SThomas Gleixner * 4275cee9645SThomas Gleixner * The return value is the rounded version of the @j parameter. 4285cee9645SThomas Gleixner */ 4295cee9645SThomas Gleixner unsigned long round_jiffies_relative(unsigned long j) 4305cee9645SThomas Gleixner { 4315cee9645SThomas Gleixner return __round_jiffies_relative(j, raw_smp_processor_id()); 4325cee9645SThomas Gleixner } 4335cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_relative); 4345cee9645SThomas Gleixner 4355cee9645SThomas Gleixner /** 4365cee9645SThomas Gleixner * __round_jiffies_up - function to round jiffies up to a full second 4375cee9645SThomas Gleixner * @j: the time in (absolute) jiffies that should be rounded 4385cee9645SThomas Gleixner * @cpu: the processor number on which the timeout will happen 4395cee9645SThomas Gleixner * 4405cee9645SThomas Gleixner * This is the same as __round_jiffies() except that it will never 4415cee9645SThomas Gleixner * round down. This is useful for timeouts for which the exact time 4425cee9645SThomas Gleixner * of firing does not matter too much, as long as they don't fire too 4435cee9645SThomas Gleixner * early. 4445cee9645SThomas Gleixner */ 4455cee9645SThomas Gleixner unsigned long __round_jiffies_up(unsigned long j, int cpu) 4465cee9645SThomas Gleixner { 4475cee9645SThomas Gleixner return round_jiffies_common(j, cpu, true); 4485cee9645SThomas Gleixner } 4495cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up); 4505cee9645SThomas Gleixner 4515cee9645SThomas Gleixner /** 4525cee9645SThomas Gleixner * __round_jiffies_up_relative - function to round jiffies up to a full second 4535cee9645SThomas Gleixner * @j: the time in (relative) jiffies that should be rounded 4545cee9645SThomas Gleixner * @cpu: the processor number on which the timeout will happen 4555cee9645SThomas Gleixner * 4565cee9645SThomas Gleixner * This is the same as __round_jiffies_relative() except that it will never 4575cee9645SThomas Gleixner * round down. This is useful for timeouts for which the exact time 4585cee9645SThomas Gleixner * of firing does not matter too much, as long as they don't fire too 4595cee9645SThomas Gleixner * early. 4605cee9645SThomas Gleixner */ 4615cee9645SThomas Gleixner unsigned long __round_jiffies_up_relative(unsigned long j, int cpu) 4625cee9645SThomas Gleixner { 4635cee9645SThomas Gleixner unsigned long j0 = jiffies; 4645cee9645SThomas Gleixner 4655cee9645SThomas Gleixner /* Use j0 because jiffies might change while we run */ 4665cee9645SThomas Gleixner return round_jiffies_common(j + j0, cpu, true) - j0; 4675cee9645SThomas Gleixner } 4685cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(__round_jiffies_up_relative); 4695cee9645SThomas Gleixner 4705cee9645SThomas Gleixner /** 4715cee9645SThomas Gleixner * round_jiffies_up - function to round jiffies up to a full second 4725cee9645SThomas Gleixner * @j: the time in (absolute) jiffies that should be rounded 4735cee9645SThomas Gleixner * 4745cee9645SThomas Gleixner * This is the same as round_jiffies() except that it will never 4755cee9645SThomas Gleixner * round down. This is useful for timeouts for which the exact time 4765cee9645SThomas Gleixner * of firing does not matter too much, as long as they don't fire too 4775cee9645SThomas Gleixner * early. 4785cee9645SThomas Gleixner */ 4795cee9645SThomas Gleixner unsigned long round_jiffies_up(unsigned long j) 4805cee9645SThomas Gleixner { 4815cee9645SThomas Gleixner return round_jiffies_common(j, raw_smp_processor_id(), true); 4825cee9645SThomas Gleixner } 4835cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up); 4845cee9645SThomas Gleixner 4855cee9645SThomas Gleixner /** 4865cee9645SThomas Gleixner * round_jiffies_up_relative - function to round jiffies up to a full second 4875cee9645SThomas Gleixner * @j: the time in (relative) jiffies that should be rounded 4885cee9645SThomas Gleixner * 4895cee9645SThomas Gleixner * This is the same as round_jiffies_relative() except that it will never 4905cee9645SThomas Gleixner * round down. This is useful for timeouts for which the exact time 4915cee9645SThomas Gleixner * of firing does not matter too much, as long as they don't fire too 4925cee9645SThomas Gleixner * early. 4935cee9645SThomas Gleixner */ 4945cee9645SThomas Gleixner unsigned long round_jiffies_up_relative(unsigned long j) 4955cee9645SThomas Gleixner { 4965cee9645SThomas Gleixner return __round_jiffies_up_relative(j, raw_smp_processor_id()); 4975cee9645SThomas Gleixner } 4985cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(round_jiffies_up_relative); 4995cee9645SThomas Gleixner 5005cee9645SThomas Gleixner 501500462a9SThomas Gleixner static inline unsigned int timer_get_idx(struct timer_list *timer) 5025cee9645SThomas Gleixner { 503500462a9SThomas Gleixner return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT; 5045cee9645SThomas Gleixner } 505500462a9SThomas Gleixner 506500462a9SThomas Gleixner static inline void timer_set_idx(struct timer_list *timer, unsigned int idx) 507500462a9SThomas Gleixner { 508500462a9SThomas Gleixner timer->flags = (timer->flags & ~TIMER_ARRAYMASK) | 509500462a9SThomas Gleixner idx << TIMER_ARRAYSHIFT; 510500462a9SThomas Gleixner } 511500462a9SThomas Gleixner 512500462a9SThomas Gleixner /* 513500462a9SThomas Gleixner * Helper function to calculate the array index for a given expiry 514500462a9SThomas Gleixner * time. 515500462a9SThomas Gleixner */ 5161f32cab0SAnna-Maria Behnsen static inline unsigned calc_index(unsigned long expires, unsigned lvl, 5171f32cab0SAnna-Maria Behnsen unsigned long *bucket_expiry) 518500462a9SThomas Gleixner { 51944688972SFrederic Weisbecker 52044688972SFrederic Weisbecker /* 52144688972SFrederic Weisbecker * The timer wheel has to guarantee that a timer does not fire 52244688972SFrederic Weisbecker * early. Early expiry can happen due to: 52344688972SFrederic Weisbecker * - Timer is armed at the edge of a tick 52444688972SFrederic Weisbecker * - Truncation of the expiry time in the outer wheel levels 52544688972SFrederic Weisbecker * 52644688972SFrederic Weisbecker * Round up with level granularity to prevent this. 52744688972SFrederic Weisbecker */ 528a2026e44SThomas Gleixner expires = (expires >> LVL_SHIFT(lvl)) + 1; 5291f32cab0SAnna-Maria Behnsen *bucket_expiry = expires << LVL_SHIFT(lvl); 530500462a9SThomas Gleixner return LVL_OFFS(lvl) + (expires & LVL_MASK); 531500462a9SThomas Gleixner } 532500462a9SThomas Gleixner 5331f32cab0SAnna-Maria Behnsen static int calc_wheel_index(unsigned long expires, unsigned long clk, 5341f32cab0SAnna-Maria Behnsen unsigned long *bucket_expiry) 5355cee9645SThomas Gleixner { 536ffdf0477SAnna-Maria Gleixner unsigned long delta = expires - clk; 537500462a9SThomas Gleixner unsigned int idx; 5385cee9645SThomas Gleixner 539500462a9SThomas Gleixner if (delta < LVL_START(1)) { 5401f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 0, bucket_expiry); 541500462a9SThomas Gleixner } else if (delta < LVL_START(2)) { 5421f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 1, bucket_expiry); 543500462a9SThomas Gleixner } else if (delta < LVL_START(3)) { 5441f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 2, bucket_expiry); 545500462a9SThomas Gleixner } else if (delta < LVL_START(4)) { 5461f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 3, bucket_expiry); 547500462a9SThomas Gleixner } else if (delta < LVL_START(5)) { 5481f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 4, bucket_expiry); 549500462a9SThomas Gleixner } else if (delta < LVL_START(6)) { 5501f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 5, bucket_expiry); 551500462a9SThomas Gleixner } else if (delta < LVL_START(7)) { 5521f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 6, bucket_expiry); 553500462a9SThomas Gleixner } else if (LVL_DEPTH > 8 && delta < LVL_START(8)) { 5541f32cab0SAnna-Maria Behnsen idx = calc_index(expires, 7, bucket_expiry); 555500462a9SThomas Gleixner } else if ((long) delta < 0) { 556ffdf0477SAnna-Maria Gleixner idx = clk & LVL_MASK; 5571f32cab0SAnna-Maria Behnsen *bucket_expiry = clk; 5585cee9645SThomas Gleixner } else { 559500462a9SThomas Gleixner /* 560500462a9SThomas Gleixner * Force expire obscene large timeouts to expire at the 561500462a9SThomas Gleixner * capacity limit of the wheel. 5625cee9645SThomas Gleixner */ 563e2a71bdeSFrederic Weisbecker if (delta >= WHEEL_TIMEOUT_CUTOFF) 564e2a71bdeSFrederic Weisbecker expires = clk + WHEEL_TIMEOUT_MAX; 5651bd04bf6SThomas Gleixner 5661f32cab0SAnna-Maria Behnsen idx = calc_index(expires, LVL_DEPTH - 1, bucket_expiry); 567500462a9SThomas Gleixner } 568ffdf0477SAnna-Maria Gleixner return idx; 569ffdf0477SAnna-Maria Gleixner } 570ffdf0477SAnna-Maria Gleixner 571ffdf0477SAnna-Maria Gleixner static void 572ffdf0477SAnna-Maria Gleixner trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer) 5735cee9645SThomas Gleixner { 5745cee9645SThomas Gleixner /* 575d124c339SAnna-Maria Behnsen * Deferrable timers do not prevent the CPU from entering dynticks and 576d124c339SAnna-Maria Behnsen * are not taken into account on the idle/nohz_full path. An IPI when a 577d124c339SAnna-Maria Behnsen * new deferrable timer is enqueued will wake up the remote CPU but 578d124c339SAnna-Maria Behnsen * nothing will be done with the deferrable timer base. Therefore skip 579d124c339SAnna-Maria Behnsen * the remote IPI for deferrable timers completely. 5805cee9645SThomas Gleixner */ 581d124c339SAnna-Maria Behnsen if (!is_timers_nohz_active() || timer->flags & TIMER_DEFERRABLE) 582a683f390SThomas Gleixner return; 5839f6d9baaSViresh Kumar 5849f6d9baaSViresh Kumar /* 585a683f390SThomas Gleixner * We might have to IPI the remote CPU if the base is idle and the 586a683f390SThomas Gleixner * timer is not deferrable. If the other CPU is on the way to idle 587a683f390SThomas Gleixner * then it can't set base->is_idle as we hold the base lock: 5889f6d9baaSViresh Kumar */ 589dc2a0f1fSFrederic Weisbecker if (base->is_idle) 5909f6d9baaSViresh Kumar wake_up_nohz_cpu(base->cpu); 5915cee9645SThomas Gleixner } 5925cee9645SThomas Gleixner 5939a2b764bSFrederic Weisbecker /* 5949a2b764bSFrederic Weisbecker * Enqueue the timer into the hash bucket, mark it pending in 5959a2b764bSFrederic Weisbecker * the bitmap, store the index in the timer flags then wake up 5969a2b764bSFrederic Weisbecker * the target CPU if needed. 5979a2b764bSFrederic Weisbecker */ 5989a2b764bSFrederic Weisbecker static void enqueue_timer(struct timer_base *base, struct timer_list *timer, 5999a2b764bSFrederic Weisbecker unsigned int idx, unsigned long bucket_expiry) 600ffdf0477SAnna-Maria Gleixner { 601dc2a0f1fSFrederic Weisbecker 6029a2b764bSFrederic Weisbecker hlist_add_head(&timer->entry, base->vectors + idx); 6039a2b764bSFrederic Weisbecker __set_bit(idx, base->pending_map); 6049a2b764bSFrederic Weisbecker timer_set_idx(timer, idx); 6059a2b764bSFrederic Weisbecker 606dbcdcb62SAnna-Maria Behnsen trace_timer_start(timer, bucket_expiry); 607dc2a0f1fSFrederic Weisbecker 608dc2a0f1fSFrederic Weisbecker /* 609dc2a0f1fSFrederic Weisbecker * Check whether this is the new first expiring timer. The 610dc2a0f1fSFrederic Weisbecker * effective expiry time of the timer is required here 611dc2a0f1fSFrederic Weisbecker * (bucket_expiry) instead of timer->expires. 612dc2a0f1fSFrederic Weisbecker */ 613dc2a0f1fSFrederic Weisbecker if (time_before(bucket_expiry, base->next_expiry)) { 614dc2a0f1fSFrederic Weisbecker /* 615dc2a0f1fSFrederic Weisbecker * Set the next expiry time and kick the CPU so it 616dc2a0f1fSFrederic Weisbecker * can reevaluate the wheel: 617dc2a0f1fSFrederic Weisbecker */ 618dc2a0f1fSFrederic Weisbecker base->next_expiry = bucket_expiry; 619aebacb7fSNicolas Saenz Julienne base->timers_pending = true; 62031cd0e11SFrederic Weisbecker base->next_expiry_recalc = false; 621ffdf0477SAnna-Maria Gleixner trigger_dyntick_cpu(base, timer); 6225cee9645SThomas Gleixner } 6239a2b764bSFrederic Weisbecker } 6249a2b764bSFrederic Weisbecker 6259a2b764bSFrederic Weisbecker static void internal_add_timer(struct timer_base *base, struct timer_list *timer) 6265cee9645SThomas Gleixner { 6271f32cab0SAnna-Maria Behnsen unsigned long bucket_expiry; 6289a2b764bSFrederic Weisbecker unsigned int idx; 6291f32cab0SAnna-Maria Behnsen 6309a2b764bSFrederic Weisbecker idx = calc_wheel_index(timer->expires, base->clk, &bucket_expiry); 6319a2b764bSFrederic Weisbecker enqueue_timer(base, timer, idx, bucket_expiry); 6325cee9645SThomas Gleixner } 6335cee9645SThomas Gleixner 6345cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS 6355cee9645SThomas Gleixner 636f9e62f31SStephen Boyd static const struct debug_obj_descr timer_debug_descr; 6375cee9645SThomas Gleixner 638317f29c1SStephen Boyd struct timer_hint { 639317f29c1SStephen Boyd void (*function)(struct timer_list *t); 640317f29c1SStephen Boyd long offset; 641317f29c1SStephen Boyd }; 642317f29c1SStephen Boyd 643317f29c1SStephen Boyd #define TIMER_HINT(fn, container, timr, hintfn) \ 644317f29c1SStephen Boyd { \ 645317f29c1SStephen Boyd .function = fn, \ 646317f29c1SStephen Boyd .offset = offsetof(container, hintfn) - \ 647317f29c1SStephen Boyd offsetof(container, timr) \ 648317f29c1SStephen Boyd } 649317f29c1SStephen Boyd 650317f29c1SStephen Boyd static const struct timer_hint timer_hints[] = { 651317f29c1SStephen Boyd TIMER_HINT(delayed_work_timer_fn, 652317f29c1SStephen Boyd struct delayed_work, timer, work.func), 653317f29c1SStephen Boyd TIMER_HINT(kthread_delayed_work_timer_fn, 654317f29c1SStephen Boyd struct kthread_delayed_work, timer, work.func), 655317f29c1SStephen Boyd }; 656317f29c1SStephen Boyd 6575cee9645SThomas Gleixner static void *timer_debug_hint(void *addr) 6585cee9645SThomas Gleixner { 659317f29c1SStephen Boyd struct timer_list *timer = addr; 660317f29c1SStephen Boyd int i; 661317f29c1SStephen Boyd 662317f29c1SStephen Boyd for (i = 0; i < ARRAY_SIZE(timer_hints); i++) { 663317f29c1SStephen Boyd if (timer_hints[i].function == timer->function) { 664317f29c1SStephen Boyd void (**fn)(void) = addr + timer_hints[i].offset; 665317f29c1SStephen Boyd 666317f29c1SStephen Boyd return *fn; 667317f29c1SStephen Boyd } 668317f29c1SStephen Boyd } 669317f29c1SStephen Boyd 670317f29c1SStephen Boyd return timer->function; 6715cee9645SThomas Gleixner } 6725cee9645SThomas Gleixner 673b9fdac7fSDu, Changbin static bool timer_is_static_object(void *addr) 674b9fdac7fSDu, Changbin { 675b9fdac7fSDu, Changbin struct timer_list *timer = addr; 676b9fdac7fSDu, Changbin 677b9fdac7fSDu, Changbin return (timer->entry.pprev == NULL && 678b9fdac7fSDu, Changbin timer->entry.next == TIMER_ENTRY_STATIC); 679b9fdac7fSDu, Changbin } 680b9fdac7fSDu, Changbin 6815cee9645SThomas Gleixner /* 6825cee9645SThomas Gleixner * fixup_init is called when: 6835cee9645SThomas Gleixner * - an active object is initialized 6845cee9645SThomas Gleixner */ 685e3252464SDu, Changbin static bool timer_fixup_init(void *addr, enum debug_obj_state state) 6865cee9645SThomas Gleixner { 6875cee9645SThomas Gleixner struct timer_list *timer = addr; 6885cee9645SThomas Gleixner 6895cee9645SThomas Gleixner switch (state) { 6905cee9645SThomas Gleixner case ODEBUG_STATE_ACTIVE: 6915cee9645SThomas Gleixner del_timer_sync(timer); 6925cee9645SThomas Gleixner debug_object_init(timer, &timer_debug_descr); 693e3252464SDu, Changbin return true; 6945cee9645SThomas Gleixner default: 695e3252464SDu, Changbin return false; 6965cee9645SThomas Gleixner } 6975cee9645SThomas Gleixner } 6985cee9645SThomas Gleixner 6995cee9645SThomas Gleixner /* Stub timer callback for improperly used timers. */ 700ba16490eSThomas Gleixner static void stub_timer(struct timer_list *unused) 7015cee9645SThomas Gleixner { 7025cee9645SThomas Gleixner WARN_ON(1); 7035cee9645SThomas Gleixner } 7045cee9645SThomas Gleixner 7055cee9645SThomas Gleixner /* 7065cee9645SThomas Gleixner * fixup_activate is called when: 7075cee9645SThomas Gleixner * - an active object is activated 708b9fdac7fSDu, Changbin * - an unknown non-static object is activated 7095cee9645SThomas Gleixner */ 710e3252464SDu, Changbin static bool timer_fixup_activate(void *addr, enum debug_obj_state state) 7115cee9645SThomas Gleixner { 7125cee9645SThomas Gleixner struct timer_list *timer = addr; 7135cee9645SThomas Gleixner 7145cee9645SThomas Gleixner switch (state) { 7155cee9645SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 716ba16490eSThomas Gleixner timer_setup(timer, stub_timer, 0); 717e3252464SDu, Changbin return true; 7185cee9645SThomas Gleixner 7195cee9645SThomas Gleixner case ODEBUG_STATE_ACTIVE: 7205cee9645SThomas Gleixner WARN_ON(1); 721df561f66SGustavo A. R. Silva fallthrough; 7225cee9645SThomas Gleixner default: 723e3252464SDu, Changbin return false; 7245cee9645SThomas Gleixner } 7255cee9645SThomas Gleixner } 7265cee9645SThomas Gleixner 7275cee9645SThomas Gleixner /* 7285cee9645SThomas Gleixner * fixup_free is called when: 7295cee9645SThomas Gleixner * - an active object is freed 7305cee9645SThomas Gleixner */ 731e3252464SDu, Changbin static bool timer_fixup_free(void *addr, enum debug_obj_state state) 7325cee9645SThomas Gleixner { 7335cee9645SThomas Gleixner struct timer_list *timer = addr; 7345cee9645SThomas Gleixner 7355cee9645SThomas Gleixner switch (state) { 7365cee9645SThomas Gleixner case ODEBUG_STATE_ACTIVE: 7375cee9645SThomas Gleixner del_timer_sync(timer); 7385cee9645SThomas Gleixner debug_object_free(timer, &timer_debug_descr); 739e3252464SDu, Changbin return true; 7405cee9645SThomas Gleixner default: 741e3252464SDu, Changbin return false; 7425cee9645SThomas Gleixner } 7435cee9645SThomas Gleixner } 7445cee9645SThomas Gleixner 7455cee9645SThomas Gleixner /* 7465cee9645SThomas Gleixner * fixup_assert_init is called when: 7475cee9645SThomas Gleixner * - an untracked/uninit-ed object is found 7485cee9645SThomas Gleixner */ 749e3252464SDu, Changbin static bool timer_fixup_assert_init(void *addr, enum debug_obj_state state) 7505cee9645SThomas Gleixner { 7515cee9645SThomas Gleixner struct timer_list *timer = addr; 7525cee9645SThomas Gleixner 7535cee9645SThomas Gleixner switch (state) { 7545cee9645SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 755ba16490eSThomas Gleixner timer_setup(timer, stub_timer, 0); 756e3252464SDu, Changbin return true; 7575cee9645SThomas Gleixner default: 758e3252464SDu, Changbin return false; 7595cee9645SThomas Gleixner } 7605cee9645SThomas Gleixner } 7615cee9645SThomas Gleixner 762f9e62f31SStephen Boyd static const struct debug_obj_descr timer_debug_descr = { 7635cee9645SThomas Gleixner .name = "timer_list", 7645cee9645SThomas Gleixner .debug_hint = timer_debug_hint, 765b9fdac7fSDu, Changbin .is_static_object = timer_is_static_object, 7665cee9645SThomas Gleixner .fixup_init = timer_fixup_init, 7675cee9645SThomas Gleixner .fixup_activate = timer_fixup_activate, 7685cee9645SThomas Gleixner .fixup_free = timer_fixup_free, 7695cee9645SThomas Gleixner .fixup_assert_init = timer_fixup_assert_init, 7705cee9645SThomas Gleixner }; 7715cee9645SThomas Gleixner 7725cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer) 7735cee9645SThomas Gleixner { 7745cee9645SThomas Gleixner debug_object_init(timer, &timer_debug_descr); 7755cee9645SThomas Gleixner } 7765cee9645SThomas Gleixner 7775cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer) 7785cee9645SThomas Gleixner { 7795cee9645SThomas Gleixner debug_object_activate(timer, &timer_debug_descr); 7805cee9645SThomas Gleixner } 7815cee9645SThomas Gleixner 7825cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer) 7835cee9645SThomas Gleixner { 7845cee9645SThomas Gleixner debug_object_deactivate(timer, &timer_debug_descr); 7855cee9645SThomas Gleixner } 7865cee9645SThomas Gleixner 7875cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer) 7885cee9645SThomas Gleixner { 7895cee9645SThomas Gleixner debug_object_assert_init(timer, &timer_debug_descr); 7905cee9645SThomas Gleixner } 7915cee9645SThomas Gleixner 792188665b2SKees Cook static void do_init_timer(struct timer_list *timer, 793188665b2SKees Cook void (*func)(struct timer_list *), 794188665b2SKees Cook unsigned int flags, 7955cee9645SThomas Gleixner const char *name, struct lock_class_key *key); 7965cee9645SThomas Gleixner 797188665b2SKees Cook void init_timer_on_stack_key(struct timer_list *timer, 798188665b2SKees Cook void (*func)(struct timer_list *), 799188665b2SKees Cook unsigned int flags, 8005cee9645SThomas Gleixner const char *name, struct lock_class_key *key) 8015cee9645SThomas Gleixner { 8025cee9645SThomas Gleixner debug_object_init_on_stack(timer, &timer_debug_descr); 803188665b2SKees Cook do_init_timer(timer, func, flags, name, key); 8045cee9645SThomas Gleixner } 8055cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(init_timer_on_stack_key); 8065cee9645SThomas Gleixner 8075cee9645SThomas Gleixner void destroy_timer_on_stack(struct timer_list *timer) 8085cee9645SThomas Gleixner { 8095cee9645SThomas Gleixner debug_object_free(timer, &timer_debug_descr); 8105cee9645SThomas Gleixner } 8115cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_timer_on_stack); 8125cee9645SThomas Gleixner 8135cee9645SThomas Gleixner #else 8145cee9645SThomas Gleixner static inline void debug_timer_init(struct timer_list *timer) { } 8155cee9645SThomas Gleixner static inline void debug_timer_activate(struct timer_list *timer) { } 8165cee9645SThomas Gleixner static inline void debug_timer_deactivate(struct timer_list *timer) { } 8175cee9645SThomas Gleixner static inline void debug_timer_assert_init(struct timer_list *timer) { } 8185cee9645SThomas Gleixner #endif 8195cee9645SThomas Gleixner 8205cee9645SThomas Gleixner static inline void debug_init(struct timer_list *timer) 8215cee9645SThomas Gleixner { 8225cee9645SThomas Gleixner debug_timer_init(timer); 8235cee9645SThomas Gleixner trace_timer_init(timer); 8245cee9645SThomas Gleixner } 8255cee9645SThomas Gleixner 8265cee9645SThomas Gleixner static inline void debug_deactivate(struct timer_list *timer) 8275cee9645SThomas Gleixner { 8285cee9645SThomas Gleixner debug_timer_deactivate(timer); 8295cee9645SThomas Gleixner trace_timer_cancel(timer); 8305cee9645SThomas Gleixner } 8315cee9645SThomas Gleixner 8325cee9645SThomas Gleixner static inline void debug_assert_init(struct timer_list *timer) 8335cee9645SThomas Gleixner { 8345cee9645SThomas Gleixner debug_timer_assert_init(timer); 8355cee9645SThomas Gleixner } 8365cee9645SThomas Gleixner 837188665b2SKees Cook static void do_init_timer(struct timer_list *timer, 838188665b2SKees Cook void (*func)(struct timer_list *), 839188665b2SKees Cook unsigned int flags, 8405cee9645SThomas Gleixner const char *name, struct lock_class_key *key) 8415cee9645SThomas Gleixner { 8421dabbcecSThomas Gleixner timer->entry.pprev = NULL; 843188665b2SKees Cook timer->function = func; 844b952caf2SQianli Zhao if (WARN_ON_ONCE(flags & ~TIMER_INIT_FLAGS)) 845b952caf2SQianli Zhao flags &= TIMER_INIT_FLAGS; 8460eeda71bSThomas Gleixner timer->flags = flags | raw_smp_processor_id(); 8475cee9645SThomas Gleixner lockdep_init_map(&timer->lockdep_map, name, key, 0); 8485cee9645SThomas Gleixner } 8495cee9645SThomas Gleixner 8505cee9645SThomas Gleixner /** 8515cee9645SThomas Gleixner * init_timer_key - initialize a timer 8525cee9645SThomas Gleixner * @timer: the timer to be initialized 853188665b2SKees Cook * @func: timer callback function 8545cee9645SThomas Gleixner * @flags: timer flags 8555cee9645SThomas Gleixner * @name: name of the timer 8565cee9645SThomas Gleixner * @key: lockdep class key of the fake lock used for tracking timer 8575cee9645SThomas Gleixner * sync lock dependencies 8585cee9645SThomas Gleixner * 8595cee9645SThomas Gleixner * init_timer_key() must be done to a timer prior calling *any* of the 8605cee9645SThomas Gleixner * other timer functions. 8615cee9645SThomas Gleixner */ 862188665b2SKees Cook void init_timer_key(struct timer_list *timer, 863188665b2SKees Cook void (*func)(struct timer_list *), unsigned int flags, 8645cee9645SThomas Gleixner const char *name, struct lock_class_key *key) 8655cee9645SThomas Gleixner { 8665cee9645SThomas Gleixner debug_init(timer); 867188665b2SKees Cook do_init_timer(timer, func, flags, name, key); 8685cee9645SThomas Gleixner } 8695cee9645SThomas Gleixner EXPORT_SYMBOL(init_timer_key); 8705cee9645SThomas Gleixner 8715cee9645SThomas Gleixner static inline void detach_timer(struct timer_list *timer, bool clear_pending) 8725cee9645SThomas Gleixner { 8731dabbcecSThomas Gleixner struct hlist_node *entry = &timer->entry; 8745cee9645SThomas Gleixner 8755cee9645SThomas Gleixner debug_deactivate(timer); 8765cee9645SThomas Gleixner 8771dabbcecSThomas Gleixner __hlist_del(entry); 8785cee9645SThomas Gleixner if (clear_pending) 8791dabbcecSThomas Gleixner entry->pprev = NULL; 8801dabbcecSThomas Gleixner entry->next = LIST_POISON2; 8815cee9645SThomas Gleixner } 8825cee9645SThomas Gleixner 883494af3edSThomas Gleixner static int detach_if_pending(struct timer_list *timer, struct timer_base *base, 8845cee9645SThomas Gleixner bool clear_pending) 8855cee9645SThomas Gleixner { 886500462a9SThomas Gleixner unsigned idx = timer_get_idx(timer); 887500462a9SThomas Gleixner 8885cee9645SThomas Gleixner if (!timer_pending(timer)) 8895cee9645SThomas Gleixner return 0; 8905cee9645SThomas Gleixner 89131cd0e11SFrederic Weisbecker if (hlist_is_singular_node(&timer->entry, base->vectors + idx)) { 892500462a9SThomas Gleixner __clear_bit(idx, base->pending_map); 89331cd0e11SFrederic Weisbecker base->next_expiry_recalc = true; 89431cd0e11SFrederic Weisbecker } 895500462a9SThomas Gleixner 8965cee9645SThomas Gleixner detach_timer(timer, clear_pending); 8975cee9645SThomas Gleixner return 1; 8985cee9645SThomas Gleixner } 8995cee9645SThomas Gleixner 900500462a9SThomas Gleixner static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu) 901500462a9SThomas Gleixner { 902500462a9SThomas Gleixner struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu); 903500462a9SThomas Gleixner 9045cee9645SThomas Gleixner /* 905ced6d5c1SAnna-Maria Gleixner * If the timer is deferrable and NO_HZ_COMMON is set then we need 906ced6d5c1SAnna-Maria Gleixner * to use the deferrable base. 907500462a9SThomas Gleixner */ 908ced6d5c1SAnna-Maria Gleixner if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE)) 909500462a9SThomas Gleixner base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu); 910500462a9SThomas Gleixner return base; 911500462a9SThomas Gleixner } 912500462a9SThomas Gleixner 913500462a9SThomas Gleixner static inline struct timer_base *get_timer_this_cpu_base(u32 tflags) 914500462a9SThomas Gleixner { 915500462a9SThomas Gleixner struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); 916500462a9SThomas Gleixner 917500462a9SThomas Gleixner /* 918ced6d5c1SAnna-Maria Gleixner * If the timer is deferrable and NO_HZ_COMMON is set then we need 919ced6d5c1SAnna-Maria Gleixner * to use the deferrable base. 920500462a9SThomas Gleixner */ 921ced6d5c1SAnna-Maria Gleixner if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE)) 922500462a9SThomas Gleixner base = this_cpu_ptr(&timer_bases[BASE_DEF]); 923500462a9SThomas Gleixner return base; 924500462a9SThomas Gleixner } 925500462a9SThomas Gleixner 926500462a9SThomas Gleixner static inline struct timer_base *get_timer_base(u32 tflags) 927500462a9SThomas Gleixner { 928500462a9SThomas Gleixner return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK); 929500462a9SThomas Gleixner } 930500462a9SThomas Gleixner 931a683f390SThomas Gleixner static inline struct timer_base * 9326bad6bccSThomas Gleixner get_target_base(struct timer_base *base, unsigned tflags) 933500462a9SThomas Gleixner { 934ae67badaSThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON) 935ae67badaSThomas Gleixner if (static_branch_likely(&timers_migration_enabled) && 936ae67badaSThomas Gleixner !(tflags & TIMER_PINNED)) 937500462a9SThomas Gleixner return get_timer_cpu_base(tflags, get_nohz_timer_target()); 938500462a9SThomas Gleixner #endif 939ae67badaSThomas Gleixner return get_timer_this_cpu_base(tflags); 940500462a9SThomas Gleixner } 941500462a9SThomas Gleixner 9421e490484SAnna-Maria Behnsen static inline void __forward_timer_base(struct timer_base *base, 9431e490484SAnna-Maria Behnsen unsigned long basej) 944a683f390SThomas Gleixner { 945a683f390SThomas Gleixner /* 9468a2c9c7eSAnna-Maria Behnsen * Check whether we can forward the base. We can only do that when 9478a2c9c7eSAnna-Maria Behnsen * @basej is past base->clk otherwise we might rewind base->clk. 948a683f390SThomas Gleixner */ 9491e490484SAnna-Maria Behnsen if (time_before_eq(basej, base->clk)) 950a683f390SThomas Gleixner return; 951a683f390SThomas Gleixner 952a683f390SThomas Gleixner /* 953a683f390SThomas Gleixner * If the next expiry value is > jiffies, then we fast forward to 954a683f390SThomas Gleixner * jiffies otherwise we forward to the next expiry value. 955a683f390SThomas Gleixner */ 9561e490484SAnna-Maria Behnsen if (time_after(base->next_expiry, basej)) { 9571e490484SAnna-Maria Behnsen base->clk = basej; 95830c66fc3SFrederic Weisbecker } else { 95930c66fc3SFrederic Weisbecker if (WARN_ON_ONCE(time_before(base->next_expiry, base->clk))) 96030c66fc3SFrederic Weisbecker return; 961a683f390SThomas Gleixner base->clk = base->next_expiry; 96230c66fc3SFrederic Weisbecker } 9631e490484SAnna-Maria Behnsen 964ae67badaSThomas Gleixner } 965a683f390SThomas Gleixner 9661e490484SAnna-Maria Behnsen static inline void forward_timer_base(struct timer_base *base) 9671e490484SAnna-Maria Behnsen { 9681e490484SAnna-Maria Behnsen __forward_timer_base(base, READ_ONCE(jiffies)); 9691e490484SAnna-Maria Behnsen } 970a683f390SThomas Gleixner 971500462a9SThomas Gleixner /* 972500462a9SThomas Gleixner * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means 973500462a9SThomas Gleixner * that all timers which are tied to this base are locked, and the base itself 974500462a9SThomas Gleixner * is locked too. 9755cee9645SThomas Gleixner * 9765cee9645SThomas Gleixner * So __run_timers/migrate_timers can safely modify all timers which could 977500462a9SThomas Gleixner * be found in the base->vectors array. 9785cee9645SThomas Gleixner * 979500462a9SThomas Gleixner * When a timer is migrating then the TIMER_MIGRATING flag is set and we need 980500462a9SThomas Gleixner * to wait until the migration is done. 9815cee9645SThomas Gleixner */ 982494af3edSThomas Gleixner static struct timer_base *lock_timer_base(struct timer_list *timer, 9835cee9645SThomas Gleixner unsigned long *flags) 9845cee9645SThomas Gleixner __acquires(timer->base->lock) 9855cee9645SThomas Gleixner { 9860eeda71bSThomas Gleixner for (;;) { 987494af3edSThomas Gleixner struct timer_base *base; 988b831275aSThomas Gleixner u32 tf; 989b831275aSThomas Gleixner 990b831275aSThomas Gleixner /* 991b831275aSThomas Gleixner * We need to use READ_ONCE() here, otherwise the compiler 992b831275aSThomas Gleixner * might re-read @tf between the check for TIMER_MIGRATING 993b831275aSThomas Gleixner * and spin_lock(). 994b831275aSThomas Gleixner */ 995b831275aSThomas Gleixner tf = READ_ONCE(timer->flags); 9965cee9645SThomas Gleixner 9970eeda71bSThomas Gleixner if (!(tf & TIMER_MIGRATING)) { 998500462a9SThomas Gleixner base = get_timer_base(tf); 9992287d866SSebastian Andrzej Siewior raw_spin_lock_irqsave(&base->lock, *flags); 10000eeda71bSThomas Gleixner if (timer->flags == tf) 10015cee9645SThomas Gleixner return base; 10022287d866SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&base->lock, *flags); 10035cee9645SThomas Gleixner } 10045cee9645SThomas Gleixner cpu_relax(); 10055cee9645SThomas Gleixner } 10065cee9645SThomas Gleixner } 10075cee9645SThomas Gleixner 1008b24591e2SDavid Howells #define MOD_TIMER_PENDING_ONLY 0x01 1009b24591e2SDavid Howells #define MOD_TIMER_REDUCE 0x02 101090c01894SEric Dumazet #define MOD_TIMER_NOTPENDING 0x04 1011b24591e2SDavid Howells 10125cee9645SThomas Gleixner static inline int 1013b24591e2SDavid Howells __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options) 10145cee9645SThomas Gleixner { 10151f32cab0SAnna-Maria Behnsen unsigned long clk = 0, flags, bucket_expiry; 1016494af3edSThomas Gleixner struct timer_base *base, *new_base; 1017f00c0afdSAnna-Maria Gleixner unsigned int idx = UINT_MAX; 1018bc7a34b8SThomas Gleixner int ret = 0; 10195cee9645SThomas Gleixner 1020d02e382cSThomas Gleixner debug_assert_init(timer); 10214da9152aSThomas Gleixner 1022500462a9SThomas Gleixner /* 1023f00c0afdSAnna-Maria Gleixner * This is a common optimization triggered by the networking code - if 1024f00c0afdSAnna-Maria Gleixner * the timer is re-modified to have the same timeout or ends up in the 1025f00c0afdSAnna-Maria Gleixner * same array bucket then just return: 1026500462a9SThomas Gleixner */ 102790c01894SEric Dumazet if (!(options & MOD_TIMER_NOTPENDING) && timer_pending(timer)) { 10282fe59f50SNicholas Piggin /* 10292fe59f50SNicholas Piggin * The downside of this optimization is that it can result in 10302fe59f50SNicholas Piggin * larger granularity than you would get from adding a new 10312fe59f50SNicholas Piggin * timer with this expiry. 10322fe59f50SNicholas Piggin */ 1033b24591e2SDavid Howells long diff = timer->expires - expires; 1034b24591e2SDavid Howells 1035b24591e2SDavid Howells if (!diff) 1036b24591e2SDavid Howells return 1; 1037b24591e2SDavid Howells if (options & MOD_TIMER_REDUCE && diff <= 0) 1038500462a9SThomas Gleixner return 1; 1039f00c0afdSAnna-Maria Gleixner 10404da9152aSThomas Gleixner /* 10414da9152aSThomas Gleixner * We lock timer base and calculate the bucket index right 10424da9152aSThomas Gleixner * here. If the timer ends up in the same bucket, then we 10434da9152aSThomas Gleixner * just update the expiry time and avoid the whole 10444da9152aSThomas Gleixner * dequeue/enqueue dance. 10454da9152aSThomas Gleixner */ 10464da9152aSThomas Gleixner base = lock_timer_base(timer, &flags); 1047d02e382cSThomas Gleixner /* 1048d02e382cSThomas Gleixner * Has @timer been shutdown? This needs to be evaluated 1049d02e382cSThomas Gleixner * while holding base lock to prevent a race against the 1050d02e382cSThomas Gleixner * shutdown code. 1051d02e382cSThomas Gleixner */ 1052d02e382cSThomas Gleixner if (!timer->function) 1053d02e382cSThomas Gleixner goto out_unlock; 1054d02e382cSThomas Gleixner 10552fe59f50SNicholas Piggin forward_timer_base(base); 10564da9152aSThomas Gleixner 1057b24591e2SDavid Howells if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) && 1058b24591e2SDavid Howells time_before_eq(timer->expires, expires)) { 1059b24591e2SDavid Howells ret = 1; 1060b24591e2SDavid Howells goto out_unlock; 1061b24591e2SDavid Howells } 1062b24591e2SDavid Howells 10634da9152aSThomas Gleixner clk = base->clk; 10641f32cab0SAnna-Maria Behnsen idx = calc_wheel_index(expires, clk, &bucket_expiry); 1065f00c0afdSAnna-Maria Gleixner 1066f00c0afdSAnna-Maria Gleixner /* 1067f00c0afdSAnna-Maria Gleixner * Retrieve and compare the array index of the pending 1068f00c0afdSAnna-Maria Gleixner * timer. If it matches set the expiry to the new value so a 1069f00c0afdSAnna-Maria Gleixner * subsequent call will exit in the expires check above. 1070f00c0afdSAnna-Maria Gleixner */ 1071f00c0afdSAnna-Maria Gleixner if (idx == timer_get_idx(timer)) { 1072b24591e2SDavid Howells if (!(options & MOD_TIMER_REDUCE)) 1073b24591e2SDavid Howells timer->expires = expires; 1074b24591e2SDavid Howells else if (time_after(timer->expires, expires)) 1075f00c0afdSAnna-Maria Gleixner timer->expires = expires; 10764da9152aSThomas Gleixner ret = 1; 10774da9152aSThomas Gleixner goto out_unlock; 1078f00c0afdSAnna-Maria Gleixner } 10794da9152aSThomas Gleixner } else { 10804da9152aSThomas Gleixner base = lock_timer_base(timer, &flags); 1081d02e382cSThomas Gleixner /* 1082d02e382cSThomas Gleixner * Has @timer been shutdown? This needs to be evaluated 1083d02e382cSThomas Gleixner * while holding base lock to prevent a race against the 1084d02e382cSThomas Gleixner * shutdown code. 1085d02e382cSThomas Gleixner */ 1086d02e382cSThomas Gleixner if (!timer->function) 1087d02e382cSThomas Gleixner goto out_unlock; 1088d02e382cSThomas Gleixner 10892fe59f50SNicholas Piggin forward_timer_base(base); 1090500462a9SThomas Gleixner } 1091500462a9SThomas Gleixner 10925cee9645SThomas Gleixner ret = detach_if_pending(timer, base, false); 1093b24591e2SDavid Howells if (!ret && (options & MOD_TIMER_PENDING_ONLY)) 10945cee9645SThomas Gleixner goto out_unlock; 10955cee9645SThomas Gleixner 1096500462a9SThomas Gleixner new_base = get_target_base(base, timer->flags); 10975cee9645SThomas Gleixner 10985cee9645SThomas Gleixner if (base != new_base) { 10995cee9645SThomas Gleixner /* 1100500462a9SThomas Gleixner * We are trying to schedule the timer on the new base. 11015cee9645SThomas Gleixner * However we can't change timer's base while it is running, 11029b13df3fSThomas Gleixner * otherwise timer_delete_sync() can't detect that the timer's 1103500462a9SThomas Gleixner * handler yet has not finished. This also guarantees that the 1104500462a9SThomas Gleixner * timer is serialized wrt itself. 11055cee9645SThomas Gleixner */ 11065cee9645SThomas Gleixner if (likely(base->running_timer != timer)) { 11075cee9645SThomas Gleixner /* See the comment in lock_timer_base() */ 11080eeda71bSThomas Gleixner timer->flags |= TIMER_MIGRATING; 11090eeda71bSThomas Gleixner 11102287d866SSebastian Andrzej Siewior raw_spin_unlock(&base->lock); 11115cee9645SThomas Gleixner base = new_base; 11122287d866SSebastian Andrzej Siewior raw_spin_lock(&base->lock); 1113d0023a14SEric Dumazet WRITE_ONCE(timer->flags, 1114d0023a14SEric Dumazet (timer->flags & ~TIMER_BASEMASK) | base->cpu); 11156bad6bccSThomas Gleixner forward_timer_base(base); 11162fe59f50SNicholas Piggin } 11172fe59f50SNicholas Piggin } 11186bad6bccSThomas Gleixner 1119dc1e7dc5SAnna-Maria Gleixner debug_timer_activate(timer); 1120fd45bb77SThomas Gleixner 11215cee9645SThomas Gleixner timer->expires = expires; 1122f00c0afdSAnna-Maria Gleixner /* 1123f00c0afdSAnna-Maria Gleixner * If 'idx' was calculated above and the base time did not advance 11244da9152aSThomas Gleixner * between calculating 'idx' and possibly switching the base, only 11259a2b764bSFrederic Weisbecker * enqueue_timer() is required. Otherwise we need to (re)calculate 11269a2b764bSFrederic Weisbecker * the wheel index via internal_add_timer(). 1127f00c0afdSAnna-Maria Gleixner */ 11289a2b764bSFrederic Weisbecker if (idx != UINT_MAX && clk == base->clk) 11299a2b764bSFrederic Weisbecker enqueue_timer(base, timer, idx, bucket_expiry); 11309a2b764bSFrederic Weisbecker else 11315cee9645SThomas Gleixner internal_add_timer(base, timer); 11325cee9645SThomas Gleixner 11335cee9645SThomas Gleixner out_unlock: 11342287d866SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&base->lock, flags); 11355cee9645SThomas Gleixner 11365cee9645SThomas Gleixner return ret; 11375cee9645SThomas Gleixner } 11385cee9645SThomas Gleixner 11395cee9645SThomas Gleixner /** 114014f043f1SThomas Gleixner * mod_timer_pending - Modify a pending timer's timeout 114114f043f1SThomas Gleixner * @timer: The pending timer to be modified 114214f043f1SThomas Gleixner * @expires: New absolute timeout in jiffies 11435cee9645SThomas Gleixner * 114414f043f1SThomas Gleixner * mod_timer_pending() is the same for pending timers as mod_timer(), but 114514f043f1SThomas Gleixner * will not activate inactive timers. 11465cee9645SThomas Gleixner * 1147d02e382cSThomas Gleixner * If @timer->function == NULL then the start operation is silently 1148d02e382cSThomas Gleixner * discarded. 1149d02e382cSThomas Gleixner * 115014f043f1SThomas Gleixner * Return: 1151d02e382cSThomas Gleixner * * %0 - The timer was inactive and not modified or was in 1152d02e382cSThomas Gleixner * shutdown state and the operation was discarded 115314f043f1SThomas Gleixner * * %1 - The timer was active and requeued to expire at @expires 11545cee9645SThomas Gleixner */ 11555cee9645SThomas Gleixner int mod_timer_pending(struct timer_list *timer, unsigned long expires) 11565cee9645SThomas Gleixner { 1157b24591e2SDavid Howells return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY); 11585cee9645SThomas Gleixner } 11595cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer_pending); 11605cee9645SThomas Gleixner 11615cee9645SThomas Gleixner /** 116214f043f1SThomas Gleixner * mod_timer - Modify a timer's timeout 116314f043f1SThomas Gleixner * @timer: The timer to be modified 116414f043f1SThomas Gleixner * @expires: New absolute timeout in jiffies 11655cee9645SThomas Gleixner * 11665cee9645SThomas Gleixner * mod_timer(timer, expires) is equivalent to: 11675cee9645SThomas Gleixner * 11685cee9645SThomas Gleixner * del_timer(timer); timer->expires = expires; add_timer(timer); 11695cee9645SThomas Gleixner * 117014f043f1SThomas Gleixner * mod_timer() is more efficient than the above open coded sequence. In 117114f043f1SThomas Gleixner * case that the timer is inactive, the del_timer() part is a NOP. The 117214f043f1SThomas Gleixner * timer is in any case activated with the new expiry time @expires. 117314f043f1SThomas Gleixner * 11745cee9645SThomas Gleixner * Note that if there are multiple unserialized concurrent users of the 11755cee9645SThomas Gleixner * same timer, then mod_timer() is the only safe way to modify the timeout, 11765cee9645SThomas Gleixner * since add_timer() cannot modify an already running timer. 11775cee9645SThomas Gleixner * 1178d02e382cSThomas Gleixner * If @timer->function == NULL then the start operation is silently 1179d02e382cSThomas Gleixner * discarded. In this case the return value is 0 and meaningless. 1180d02e382cSThomas Gleixner * 118114f043f1SThomas Gleixner * Return: 1182d02e382cSThomas Gleixner * * %0 - The timer was inactive and started or was in shutdown 1183d02e382cSThomas Gleixner * state and the operation was discarded 118414f043f1SThomas Gleixner * * %1 - The timer was active and requeued to expire at @expires or 118514f043f1SThomas Gleixner * the timer was active and not modified because @expires did 118614f043f1SThomas Gleixner * not change the effective expiry time 11875cee9645SThomas Gleixner */ 11885cee9645SThomas Gleixner int mod_timer(struct timer_list *timer, unsigned long expires) 11895cee9645SThomas Gleixner { 1190b24591e2SDavid Howells return __mod_timer(timer, expires, 0); 11915cee9645SThomas Gleixner } 11925cee9645SThomas Gleixner EXPORT_SYMBOL(mod_timer); 11935cee9645SThomas Gleixner 11945cee9645SThomas Gleixner /** 1195b24591e2SDavid Howells * timer_reduce - Modify a timer's timeout if it would reduce the timeout 1196b24591e2SDavid Howells * @timer: The timer to be modified 119714f043f1SThomas Gleixner * @expires: New absolute timeout in jiffies 1198b24591e2SDavid Howells * 1199b24591e2SDavid Howells * timer_reduce() is very similar to mod_timer(), except that it will only 120014f043f1SThomas Gleixner * modify an enqueued timer if that would reduce the expiration time. If 120114f043f1SThomas Gleixner * @timer is not enqueued it starts the timer. 120214f043f1SThomas Gleixner * 1203d02e382cSThomas Gleixner * If @timer->function == NULL then the start operation is silently 1204d02e382cSThomas Gleixner * discarded. 1205d02e382cSThomas Gleixner * 120614f043f1SThomas Gleixner * Return: 1207d02e382cSThomas Gleixner * * %0 - The timer was inactive and started or was in shutdown 1208d02e382cSThomas Gleixner * state and the operation was discarded 120914f043f1SThomas Gleixner * * %1 - The timer was active and requeued to expire at @expires or 121014f043f1SThomas Gleixner * the timer was active and not modified because @expires 121114f043f1SThomas Gleixner * did not change the effective expiry time such that the 121214f043f1SThomas Gleixner * timer would expire earlier than already scheduled 1213b24591e2SDavid Howells */ 1214b24591e2SDavid Howells int timer_reduce(struct timer_list *timer, unsigned long expires) 1215b24591e2SDavid Howells { 1216b24591e2SDavid Howells return __mod_timer(timer, expires, MOD_TIMER_REDUCE); 1217b24591e2SDavid Howells } 1218b24591e2SDavid Howells EXPORT_SYMBOL(timer_reduce); 1219b24591e2SDavid Howells 1220b24591e2SDavid Howells /** 122114f043f1SThomas Gleixner * add_timer - Start a timer 122214f043f1SThomas Gleixner * @timer: The timer to be started 12235cee9645SThomas Gleixner * 122414f043f1SThomas Gleixner * Start @timer to expire at @timer->expires in the future. @timer->expires 122514f043f1SThomas Gleixner * is the absolute expiry time measured in 'jiffies'. When the timer expires 122614f043f1SThomas Gleixner * timer->function(timer) will be invoked from soft interrupt context. 12275cee9645SThomas Gleixner * 122814f043f1SThomas Gleixner * The @timer->expires and @timer->function fields must be set prior 122914f043f1SThomas Gleixner * to calling this function. 12305cee9645SThomas Gleixner * 1231d02e382cSThomas Gleixner * If @timer->function == NULL then the start operation is silently 1232d02e382cSThomas Gleixner * discarded. 1233d02e382cSThomas Gleixner * 123414f043f1SThomas Gleixner * If @timer->expires is already in the past @timer will be queued to 123514f043f1SThomas Gleixner * expire at the next timer tick. 123614f043f1SThomas Gleixner * 123714f043f1SThomas Gleixner * This can only operate on an inactive timer. Attempts to invoke this on 123814f043f1SThomas Gleixner * an active timer are rejected with a warning. 12395cee9645SThomas Gleixner */ 12405cee9645SThomas Gleixner void add_timer(struct timer_list *timer) 12415cee9645SThomas Gleixner { 124282ed6f7eSThomas Gleixner if (WARN_ON_ONCE(timer_pending(timer))) 124382ed6f7eSThomas Gleixner return; 124490c01894SEric Dumazet __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING); 12455cee9645SThomas Gleixner } 12465cee9645SThomas Gleixner EXPORT_SYMBOL(add_timer); 12475cee9645SThomas Gleixner 12485cee9645SThomas Gleixner /** 124914f043f1SThomas Gleixner * add_timer_on - Start a timer on a particular CPU 125014f043f1SThomas Gleixner * @timer: The timer to be started 125114f043f1SThomas Gleixner * @cpu: The CPU to start it on 12525cee9645SThomas Gleixner * 125314f043f1SThomas Gleixner * Same as add_timer() except that it starts the timer on the given CPU. 125414f043f1SThomas Gleixner * 125514f043f1SThomas Gleixner * See add_timer() for further details. 12565cee9645SThomas Gleixner */ 12575cee9645SThomas Gleixner void add_timer_on(struct timer_list *timer, int cpu) 12585cee9645SThomas Gleixner { 1259500462a9SThomas Gleixner struct timer_base *new_base, *base; 12605cee9645SThomas Gleixner unsigned long flags; 12615cee9645SThomas Gleixner 1262d02e382cSThomas Gleixner debug_assert_init(timer); 1263d02e382cSThomas Gleixner 1264d02e382cSThomas Gleixner if (WARN_ON_ONCE(timer_pending(timer))) 126582ed6f7eSThomas Gleixner return; 126622b886ddSTejun Heo 1267500462a9SThomas Gleixner new_base = get_timer_cpu_base(timer->flags, cpu); 1268500462a9SThomas Gleixner 126922b886ddSTejun Heo /* 127022b886ddSTejun Heo * If @timer was on a different CPU, it should be migrated with the 127122b886ddSTejun Heo * old base locked to prevent other operations proceeding with the 127222b886ddSTejun Heo * wrong base locked. See lock_timer_base(). 127322b886ddSTejun Heo */ 127422b886ddSTejun Heo base = lock_timer_base(timer, &flags); 1275d02e382cSThomas Gleixner /* 1276d02e382cSThomas Gleixner * Has @timer been shutdown? This needs to be evaluated while 1277d02e382cSThomas Gleixner * holding base lock to prevent a race against the shutdown code. 1278d02e382cSThomas Gleixner */ 1279d02e382cSThomas Gleixner if (!timer->function) 1280d02e382cSThomas Gleixner goto out_unlock; 1281d02e382cSThomas Gleixner 128222b886ddSTejun Heo if (base != new_base) { 128322b886ddSTejun Heo timer->flags |= TIMER_MIGRATING; 128422b886ddSTejun Heo 12852287d866SSebastian Andrzej Siewior raw_spin_unlock(&base->lock); 128622b886ddSTejun Heo base = new_base; 12872287d866SSebastian Andrzej Siewior raw_spin_lock(&base->lock); 128822b886ddSTejun Heo WRITE_ONCE(timer->flags, 128922b886ddSTejun Heo (timer->flags & ~TIMER_BASEMASK) | cpu); 129022b886ddSTejun Heo } 12912fe59f50SNicholas Piggin forward_timer_base(base); 129222b886ddSTejun Heo 1293dc1e7dc5SAnna-Maria Gleixner debug_timer_activate(timer); 12945cee9645SThomas Gleixner internal_add_timer(base, timer); 1295d02e382cSThomas Gleixner out_unlock: 12962287d866SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&base->lock, flags); 12975cee9645SThomas Gleixner } 12985cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(add_timer_on); 12995cee9645SThomas Gleixner 13005cee9645SThomas Gleixner /** 13018553b5f2SThomas Gleixner * __timer_delete - Internal function: Deactivate a timer 130214f043f1SThomas Gleixner * @timer: The timer to be deactivated 13030cc04e80SThomas Gleixner * @shutdown: If true, this indicates that the timer is about to be 13040cc04e80SThomas Gleixner * shutdown permanently. 13050cc04e80SThomas Gleixner * 13060cc04e80SThomas Gleixner * If @shutdown is true then @timer->function is set to NULL under the 13070cc04e80SThomas Gleixner * timer base lock which prevents further rearming of the time. In that 13080cc04e80SThomas Gleixner * case any attempt to rearm @timer after this function returns will be 13090cc04e80SThomas Gleixner * silently ignored. 13105cee9645SThomas Gleixner * 131114f043f1SThomas Gleixner * Return: 131214f043f1SThomas Gleixner * * %0 - The timer was not pending 131314f043f1SThomas Gleixner * * %1 - The timer was pending and deactivated 13145cee9645SThomas Gleixner */ 13150cc04e80SThomas Gleixner static int __timer_delete(struct timer_list *timer, bool shutdown) 13165cee9645SThomas Gleixner { 1317494af3edSThomas Gleixner struct timer_base *base; 13185cee9645SThomas Gleixner unsigned long flags; 13195cee9645SThomas Gleixner int ret = 0; 13205cee9645SThomas Gleixner 13215cee9645SThomas Gleixner debug_assert_init(timer); 13225cee9645SThomas Gleixner 13230cc04e80SThomas Gleixner /* 13240cc04e80SThomas Gleixner * If @shutdown is set then the lock has to be taken whether the 13250cc04e80SThomas Gleixner * timer is pending or not to protect against a concurrent rearm 13260cc04e80SThomas Gleixner * which might hit between the lockless pending check and the lock 13270cc04e80SThomas Gleixner * aquisition. By taking the lock it is ensured that such a newly 13280cc04e80SThomas Gleixner * enqueued timer is dequeued and cannot end up with 13290cc04e80SThomas Gleixner * timer->function == NULL in the expiry code. 13300cc04e80SThomas Gleixner * 13310cc04e80SThomas Gleixner * If timer->function is currently executed, then this makes sure 13320cc04e80SThomas Gleixner * that the callback cannot requeue the timer. 13330cc04e80SThomas Gleixner */ 13340cc04e80SThomas Gleixner if (timer_pending(timer) || shutdown) { 13355cee9645SThomas Gleixner base = lock_timer_base(timer, &flags); 13365cee9645SThomas Gleixner ret = detach_if_pending(timer, base, true); 13370cc04e80SThomas Gleixner if (shutdown) 13380cc04e80SThomas Gleixner timer->function = NULL; 13392287d866SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&base->lock, flags); 13405cee9645SThomas Gleixner } 13415cee9645SThomas Gleixner 13425cee9645SThomas Gleixner return ret; 13435cee9645SThomas Gleixner } 13448553b5f2SThomas Gleixner 13458553b5f2SThomas Gleixner /** 13468553b5f2SThomas Gleixner * timer_delete - Deactivate a timer 13478553b5f2SThomas Gleixner * @timer: The timer to be deactivated 13488553b5f2SThomas Gleixner * 13498553b5f2SThomas Gleixner * The function only deactivates a pending timer, but contrary to 13508553b5f2SThomas Gleixner * timer_delete_sync() it does not take into account whether the timer's 13518553b5f2SThomas Gleixner * callback function is concurrently executed on a different CPU or not. 13528553b5f2SThomas Gleixner * It neither prevents rearming of the timer. If @timer can be rearmed 13538553b5f2SThomas Gleixner * concurrently then the return value of this function is meaningless. 13548553b5f2SThomas Gleixner * 13558553b5f2SThomas Gleixner * Return: 13568553b5f2SThomas Gleixner * * %0 - The timer was not pending 13578553b5f2SThomas Gleixner * * %1 - The timer was pending and deactivated 13588553b5f2SThomas Gleixner */ 13598553b5f2SThomas Gleixner int timer_delete(struct timer_list *timer) 13608553b5f2SThomas Gleixner { 13610cc04e80SThomas Gleixner return __timer_delete(timer, false); 13628553b5f2SThomas Gleixner } 1363bb663f0fSThomas Gleixner EXPORT_SYMBOL(timer_delete); 13645cee9645SThomas Gleixner 13655cee9645SThomas Gleixner /** 1366f571faf6SThomas Gleixner * timer_shutdown - Deactivate a timer and prevent rearming 1367f571faf6SThomas Gleixner * @timer: The timer to be deactivated 1368f571faf6SThomas Gleixner * 1369f571faf6SThomas Gleixner * The function does not wait for an eventually running timer callback on a 1370f571faf6SThomas Gleixner * different CPU but it prevents rearming of the timer. Any attempt to arm 1371f571faf6SThomas Gleixner * @timer after this function returns will be silently ignored. 1372f571faf6SThomas Gleixner * 1373f571faf6SThomas Gleixner * This function is useful for teardown code and should only be used when 1374f571faf6SThomas Gleixner * timer_shutdown_sync() cannot be invoked due to locking or context constraints. 1375f571faf6SThomas Gleixner * 1376f571faf6SThomas Gleixner * Return: 1377f571faf6SThomas Gleixner * * %0 - The timer was not pending 1378f571faf6SThomas Gleixner * * %1 - The timer was pending 1379f571faf6SThomas Gleixner */ 1380f571faf6SThomas Gleixner int timer_shutdown(struct timer_list *timer) 1381f571faf6SThomas Gleixner { 1382f571faf6SThomas Gleixner return __timer_delete(timer, true); 1383f571faf6SThomas Gleixner } 1384f571faf6SThomas Gleixner EXPORT_SYMBOL_GPL(timer_shutdown); 1385f571faf6SThomas Gleixner 1386f571faf6SThomas Gleixner /** 13878553b5f2SThomas Gleixner * __try_to_del_timer_sync - Internal function: Try to deactivate a timer 13888553b5f2SThomas Gleixner * @timer: Timer to deactivate 13890cc04e80SThomas Gleixner * @shutdown: If true, this indicates that the timer is about to be 13900cc04e80SThomas Gleixner * shutdown permanently. 13910cc04e80SThomas Gleixner * 13920cc04e80SThomas Gleixner * If @shutdown is true then @timer->function is set to NULL under the 13930cc04e80SThomas Gleixner * timer base lock which prevents further rearming of the timer. Any 13940cc04e80SThomas Gleixner * attempt to rearm @timer after this function returns will be silently 13950cc04e80SThomas Gleixner * ignored. 13960cc04e80SThomas Gleixner * 13970cc04e80SThomas Gleixner * This function cannot guarantee that the timer cannot be rearmed 13980cc04e80SThomas Gleixner * right after dropping the base lock if @shutdown is false. That 13990cc04e80SThomas Gleixner * needs to be prevented by the calling code if necessary. 14008553b5f2SThomas Gleixner * 14018553b5f2SThomas Gleixner * Return: 14028553b5f2SThomas Gleixner * * %0 - The timer was not pending 14038553b5f2SThomas Gleixner * * %1 - The timer was pending and deactivated 14048553b5f2SThomas Gleixner * * %-1 - The timer callback function is running on a different CPU 14058553b5f2SThomas Gleixner */ 14060cc04e80SThomas Gleixner static int __try_to_del_timer_sync(struct timer_list *timer, bool shutdown) 14078553b5f2SThomas Gleixner { 14088553b5f2SThomas Gleixner struct timer_base *base; 14098553b5f2SThomas Gleixner unsigned long flags; 14108553b5f2SThomas Gleixner int ret = -1; 14118553b5f2SThomas Gleixner 14128553b5f2SThomas Gleixner debug_assert_init(timer); 14138553b5f2SThomas Gleixner 14148553b5f2SThomas Gleixner base = lock_timer_base(timer, &flags); 14158553b5f2SThomas Gleixner 14168553b5f2SThomas Gleixner if (base->running_timer != timer) 14178553b5f2SThomas Gleixner ret = detach_if_pending(timer, base, true); 14180cc04e80SThomas Gleixner if (shutdown) 14190cc04e80SThomas Gleixner timer->function = NULL; 14208553b5f2SThomas Gleixner 14218553b5f2SThomas Gleixner raw_spin_unlock_irqrestore(&base->lock, flags); 14228553b5f2SThomas Gleixner 14238553b5f2SThomas Gleixner return ret; 14248553b5f2SThomas Gleixner } 14258553b5f2SThomas Gleixner 14268553b5f2SThomas Gleixner /** 14275cee9645SThomas Gleixner * try_to_del_timer_sync - Try to deactivate a timer 142814f043f1SThomas Gleixner * @timer: Timer to deactivate 14295cee9645SThomas Gleixner * 143014f043f1SThomas Gleixner * This function tries to deactivate a timer. On success the timer is not 143114f043f1SThomas Gleixner * queued and the timer callback function is not running on any CPU. 143214f043f1SThomas Gleixner * 143314f043f1SThomas Gleixner * This function does not guarantee that the timer cannot be rearmed right 143414f043f1SThomas Gleixner * after dropping the base lock. That needs to be prevented by the calling 143514f043f1SThomas Gleixner * code if necessary. 143614f043f1SThomas Gleixner * 143714f043f1SThomas Gleixner * Return: 143814f043f1SThomas Gleixner * * %0 - The timer was not pending 143914f043f1SThomas Gleixner * * %1 - The timer was pending and deactivated 144014f043f1SThomas Gleixner * * %-1 - The timer callback function is running on a different CPU 14415cee9645SThomas Gleixner */ 14425cee9645SThomas Gleixner int try_to_del_timer_sync(struct timer_list *timer) 14435cee9645SThomas Gleixner { 14440cc04e80SThomas Gleixner return __try_to_del_timer_sync(timer, false); 14455cee9645SThomas Gleixner } 14465cee9645SThomas Gleixner EXPORT_SYMBOL(try_to_del_timer_sync); 14475cee9645SThomas Gleixner 1448030dcdd1SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT 1449030dcdd1SAnna-Maria Gleixner static __init void timer_base_init_expiry_lock(struct timer_base *base) 1450030dcdd1SAnna-Maria Gleixner { 1451030dcdd1SAnna-Maria Gleixner spin_lock_init(&base->expiry_lock); 1452030dcdd1SAnna-Maria Gleixner } 1453030dcdd1SAnna-Maria Gleixner 1454030dcdd1SAnna-Maria Gleixner static inline void timer_base_lock_expiry(struct timer_base *base) 1455030dcdd1SAnna-Maria Gleixner { 1456030dcdd1SAnna-Maria Gleixner spin_lock(&base->expiry_lock); 1457030dcdd1SAnna-Maria Gleixner } 1458030dcdd1SAnna-Maria Gleixner 1459030dcdd1SAnna-Maria Gleixner static inline void timer_base_unlock_expiry(struct timer_base *base) 1460030dcdd1SAnna-Maria Gleixner { 1461030dcdd1SAnna-Maria Gleixner spin_unlock(&base->expiry_lock); 1462030dcdd1SAnna-Maria Gleixner } 1463030dcdd1SAnna-Maria Gleixner 1464030dcdd1SAnna-Maria Gleixner /* 1465030dcdd1SAnna-Maria Gleixner * The counterpart to del_timer_wait_running(). 1466030dcdd1SAnna-Maria Gleixner * 1467030dcdd1SAnna-Maria Gleixner * If there is a waiter for base->expiry_lock, then it was waiting for the 14684bf07f65SIngo Molnar * timer callback to finish. Drop expiry_lock and reacquire it. That allows 1469030dcdd1SAnna-Maria Gleixner * the waiter to acquire the lock and make progress. 1470030dcdd1SAnna-Maria Gleixner */ 1471030dcdd1SAnna-Maria Gleixner static void timer_sync_wait_running(struct timer_base *base) 1472030dcdd1SAnna-Maria Gleixner { 1473030dcdd1SAnna-Maria Gleixner if (atomic_read(&base->timer_waiters)) { 1474bb7262b2SThomas Gleixner raw_spin_unlock_irq(&base->lock); 1475030dcdd1SAnna-Maria Gleixner spin_unlock(&base->expiry_lock); 1476030dcdd1SAnna-Maria Gleixner spin_lock(&base->expiry_lock); 1477bb7262b2SThomas Gleixner raw_spin_lock_irq(&base->lock); 1478030dcdd1SAnna-Maria Gleixner } 1479030dcdd1SAnna-Maria Gleixner } 1480030dcdd1SAnna-Maria Gleixner 1481030dcdd1SAnna-Maria Gleixner /* 1482030dcdd1SAnna-Maria Gleixner * This function is called on PREEMPT_RT kernels when the fast path 1483030dcdd1SAnna-Maria Gleixner * deletion of a timer failed because the timer callback function was 1484030dcdd1SAnna-Maria Gleixner * running. 1485030dcdd1SAnna-Maria Gleixner * 1486030dcdd1SAnna-Maria Gleixner * This prevents priority inversion, if the softirq thread on a remote CPU 1487030dcdd1SAnna-Maria Gleixner * got preempted, and it prevents a life lock when the task which tries to 1488030dcdd1SAnna-Maria Gleixner * delete a timer preempted the softirq thread running the timer callback 1489030dcdd1SAnna-Maria Gleixner * function. 1490030dcdd1SAnna-Maria Gleixner */ 1491030dcdd1SAnna-Maria Gleixner static void del_timer_wait_running(struct timer_list *timer) 1492030dcdd1SAnna-Maria Gleixner { 1493030dcdd1SAnna-Maria Gleixner u32 tf; 1494030dcdd1SAnna-Maria Gleixner 1495030dcdd1SAnna-Maria Gleixner tf = READ_ONCE(timer->flags); 1496c725dafcSSebastian Andrzej Siewior if (!(tf & (TIMER_MIGRATING | TIMER_IRQSAFE))) { 1497030dcdd1SAnna-Maria Gleixner struct timer_base *base = get_timer_base(tf); 1498030dcdd1SAnna-Maria Gleixner 1499030dcdd1SAnna-Maria Gleixner /* 1500030dcdd1SAnna-Maria Gleixner * Mark the base as contended and grab the expiry lock, 1501030dcdd1SAnna-Maria Gleixner * which is held by the softirq across the timer 1502030dcdd1SAnna-Maria Gleixner * callback. Drop the lock immediately so the softirq can 1503030dcdd1SAnna-Maria Gleixner * expire the next timer. In theory the timer could already 1504030dcdd1SAnna-Maria Gleixner * be running again, but that's more than unlikely and just 1505030dcdd1SAnna-Maria Gleixner * causes another wait loop. 1506030dcdd1SAnna-Maria Gleixner */ 1507030dcdd1SAnna-Maria Gleixner atomic_inc(&base->timer_waiters); 1508030dcdd1SAnna-Maria Gleixner spin_lock_bh(&base->expiry_lock); 1509030dcdd1SAnna-Maria Gleixner atomic_dec(&base->timer_waiters); 1510030dcdd1SAnna-Maria Gleixner spin_unlock_bh(&base->expiry_lock); 1511030dcdd1SAnna-Maria Gleixner } 1512030dcdd1SAnna-Maria Gleixner } 1513030dcdd1SAnna-Maria Gleixner #else 1514030dcdd1SAnna-Maria Gleixner static inline void timer_base_init_expiry_lock(struct timer_base *base) { } 1515030dcdd1SAnna-Maria Gleixner static inline void timer_base_lock_expiry(struct timer_base *base) { } 1516030dcdd1SAnna-Maria Gleixner static inline void timer_base_unlock_expiry(struct timer_base *base) { } 1517030dcdd1SAnna-Maria Gleixner static inline void timer_sync_wait_running(struct timer_base *base) { } 1518030dcdd1SAnna-Maria Gleixner static inline void del_timer_wait_running(struct timer_list *timer) { } 1519030dcdd1SAnna-Maria Gleixner #endif 1520030dcdd1SAnna-Maria Gleixner 15215cee9645SThomas Gleixner /** 15228553b5f2SThomas Gleixner * __timer_delete_sync - Internal function: Deactivate a timer and wait 15238553b5f2SThomas Gleixner * for the handler to finish. 15248553b5f2SThomas Gleixner * @timer: The timer to be deactivated 15250cc04e80SThomas Gleixner * @shutdown: If true, @timer->function will be set to NULL under the 15260cc04e80SThomas Gleixner * timer base lock which prevents rearming of @timer 15270cc04e80SThomas Gleixner * 15280cc04e80SThomas Gleixner * If @shutdown is not set the timer can be rearmed later. If the timer can 15290cc04e80SThomas Gleixner * be rearmed concurrently, i.e. after dropping the base lock then the 15300cc04e80SThomas Gleixner * return value is meaningless. 15310cc04e80SThomas Gleixner * 15320cc04e80SThomas Gleixner * If @shutdown is set then @timer->function is set to NULL under timer 15330cc04e80SThomas Gleixner * base lock which prevents rearming of the timer. Any attempt to rearm 15340cc04e80SThomas Gleixner * a shutdown timer is silently ignored. 15350cc04e80SThomas Gleixner * 15360cc04e80SThomas Gleixner * If the timer should be reused after shutdown it has to be initialized 15370cc04e80SThomas Gleixner * again. 15388553b5f2SThomas Gleixner * 15398553b5f2SThomas Gleixner * Return: 15408553b5f2SThomas Gleixner * * %0 - The timer was not pending 15418553b5f2SThomas Gleixner * * %1 - The timer was pending and deactivated 15428553b5f2SThomas Gleixner */ 15430cc04e80SThomas Gleixner static int __timer_delete_sync(struct timer_list *timer, bool shutdown) 15448553b5f2SThomas Gleixner { 15458553b5f2SThomas Gleixner int ret; 15468553b5f2SThomas Gleixner 15478553b5f2SThomas Gleixner #ifdef CONFIG_LOCKDEP 15488553b5f2SThomas Gleixner unsigned long flags; 15498553b5f2SThomas Gleixner 15508553b5f2SThomas Gleixner /* 15518553b5f2SThomas Gleixner * If lockdep gives a backtrace here, please reference 15528553b5f2SThomas Gleixner * the synchronization rules above. 15538553b5f2SThomas Gleixner */ 15548553b5f2SThomas Gleixner local_irq_save(flags); 15558553b5f2SThomas Gleixner lock_map_acquire(&timer->lockdep_map); 15568553b5f2SThomas Gleixner lock_map_release(&timer->lockdep_map); 15578553b5f2SThomas Gleixner local_irq_restore(flags); 15588553b5f2SThomas Gleixner #endif 15598553b5f2SThomas Gleixner /* 15608553b5f2SThomas Gleixner * don't use it in hardirq context, because it 15618553b5f2SThomas Gleixner * could lead to deadlock. 15628553b5f2SThomas Gleixner */ 15638553b5f2SThomas Gleixner WARN_ON(in_hardirq() && !(timer->flags & TIMER_IRQSAFE)); 15648553b5f2SThomas Gleixner 15658553b5f2SThomas Gleixner /* 15668553b5f2SThomas Gleixner * Must be able to sleep on PREEMPT_RT because of the slowpath in 15678553b5f2SThomas Gleixner * del_timer_wait_running(). 15688553b5f2SThomas Gleixner */ 15698553b5f2SThomas Gleixner if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(timer->flags & TIMER_IRQSAFE)) 15708553b5f2SThomas Gleixner lockdep_assert_preemption_enabled(); 15718553b5f2SThomas Gleixner 15728553b5f2SThomas Gleixner do { 15730cc04e80SThomas Gleixner ret = __try_to_del_timer_sync(timer, shutdown); 15748553b5f2SThomas Gleixner 15758553b5f2SThomas Gleixner if (unlikely(ret < 0)) { 15768553b5f2SThomas Gleixner del_timer_wait_running(timer); 15778553b5f2SThomas Gleixner cpu_relax(); 15788553b5f2SThomas Gleixner } 15798553b5f2SThomas Gleixner } while (ret < 0); 15808553b5f2SThomas Gleixner 15818553b5f2SThomas Gleixner return ret; 15828553b5f2SThomas Gleixner } 15838553b5f2SThomas Gleixner 15848553b5f2SThomas Gleixner /** 15859b13df3fSThomas Gleixner * timer_delete_sync - Deactivate a timer and wait for the handler to finish. 158614f043f1SThomas Gleixner * @timer: The timer to be deactivated 15875cee9645SThomas Gleixner * 15885cee9645SThomas Gleixner * Synchronization rules: Callers must prevent restarting of the timer, 15895cee9645SThomas Gleixner * otherwise this function is meaningless. It must not be called from 15905cee9645SThomas Gleixner * interrupt contexts unless the timer is an irqsafe one. The caller must 159114f043f1SThomas Gleixner * not hold locks which would prevent completion of the timer's callback 159214f043f1SThomas Gleixner * function. The timer's handler must not call add_timer_on(). Upon exit 159314f043f1SThomas Gleixner * the timer is not queued and the handler is not running on any CPU. 15945cee9645SThomas Gleixner * 159514f043f1SThomas Gleixner * For !irqsafe timers, the caller must not hold locks that are held in 159614f043f1SThomas Gleixner * interrupt context. Even if the lock has nothing to do with the timer in 159714f043f1SThomas Gleixner * question. Here's why:: 15985cee9645SThomas Gleixner * 15995cee9645SThomas Gleixner * CPU0 CPU1 16005cee9645SThomas Gleixner * ---- ---- 16015cee9645SThomas Gleixner * <SOFTIRQ> 16025cee9645SThomas Gleixner * call_timer_fn(); 16035cee9645SThomas Gleixner * base->running_timer = mytimer; 16045cee9645SThomas Gleixner * spin_lock_irq(somelock); 16055cee9645SThomas Gleixner * <IRQ> 16065cee9645SThomas Gleixner * spin_lock(somelock); 16079b13df3fSThomas Gleixner * timer_delete_sync(mytimer); 16085cee9645SThomas Gleixner * while (base->running_timer == mytimer); 16095cee9645SThomas Gleixner * 16109b13df3fSThomas Gleixner * Now timer_delete_sync() will never return and never release somelock. 161114f043f1SThomas Gleixner * The interrupt on the other CPU is waiting to grab somelock but it has 161214f043f1SThomas Gleixner * interrupted the softirq that CPU0 is waiting to finish. 16135cee9645SThomas Gleixner * 161414f043f1SThomas Gleixner * This function cannot guarantee that the timer is not rearmed again by 161514f043f1SThomas Gleixner * some concurrent or preempting code, right after it dropped the base 161614f043f1SThomas Gleixner * lock. If there is the possibility of a concurrent rearm then the return 161714f043f1SThomas Gleixner * value of the function is meaningless. 161814f043f1SThomas Gleixner * 1619f571faf6SThomas Gleixner * If such a guarantee is needed, e.g. for teardown situations then use 1620f571faf6SThomas Gleixner * timer_shutdown_sync() instead. 1621f571faf6SThomas Gleixner * 162214f043f1SThomas Gleixner * Return: 162314f043f1SThomas Gleixner * * %0 - The timer was not pending 162414f043f1SThomas Gleixner * * %1 - The timer was pending and deactivated 16255cee9645SThomas Gleixner */ 16269b13df3fSThomas Gleixner int timer_delete_sync(struct timer_list *timer) 16275cee9645SThomas Gleixner { 16280cc04e80SThomas Gleixner return __timer_delete_sync(timer, false); 16295cee9645SThomas Gleixner } 16309b13df3fSThomas Gleixner EXPORT_SYMBOL(timer_delete_sync); 16315cee9645SThomas Gleixner 1632f571faf6SThomas Gleixner /** 1633f571faf6SThomas Gleixner * timer_shutdown_sync - Shutdown a timer and prevent rearming 1634f571faf6SThomas Gleixner * @timer: The timer to be shutdown 1635f571faf6SThomas Gleixner * 1636f571faf6SThomas Gleixner * When the function returns it is guaranteed that: 1637f571faf6SThomas Gleixner * - @timer is not queued 1638f571faf6SThomas Gleixner * - The callback function of @timer is not running 1639f571faf6SThomas Gleixner * - @timer cannot be enqueued again. Any attempt to rearm 1640f571faf6SThomas Gleixner * @timer is silently ignored. 1641f571faf6SThomas Gleixner * 1642f571faf6SThomas Gleixner * See timer_delete_sync() for synchronization rules. 1643f571faf6SThomas Gleixner * 1644f571faf6SThomas Gleixner * This function is useful for final teardown of an infrastructure where 1645f571faf6SThomas Gleixner * the timer is subject to a circular dependency problem. 1646f571faf6SThomas Gleixner * 1647f571faf6SThomas Gleixner * A common pattern for this is a timer and a workqueue where the timer can 1648f571faf6SThomas Gleixner * schedule work and work can arm the timer. On shutdown the workqueue must 1649f571faf6SThomas Gleixner * be destroyed and the timer must be prevented from rearming. Unless the 1650f571faf6SThomas Gleixner * code has conditionals like 'if (mything->in_shutdown)' to prevent that 1651f571faf6SThomas Gleixner * there is no way to get this correct with timer_delete_sync(). 1652f571faf6SThomas Gleixner * 1653f571faf6SThomas Gleixner * timer_shutdown_sync() is solving the problem. The correct ordering of 1654f571faf6SThomas Gleixner * calls in this case is: 1655f571faf6SThomas Gleixner * 1656f571faf6SThomas Gleixner * timer_shutdown_sync(&mything->timer); 1657f571faf6SThomas Gleixner * workqueue_destroy(&mything->workqueue); 1658f571faf6SThomas Gleixner * 1659f571faf6SThomas Gleixner * After this 'mything' can be safely freed. 1660f571faf6SThomas Gleixner * 1661f571faf6SThomas Gleixner * This obviously implies that the timer is not required to be functional 1662f571faf6SThomas Gleixner * for the rest of the shutdown operation. 1663f571faf6SThomas Gleixner * 1664f571faf6SThomas Gleixner * Return: 1665f571faf6SThomas Gleixner * * %0 - The timer was not pending 1666f571faf6SThomas Gleixner * * %1 - The timer was pending 1667f571faf6SThomas Gleixner */ 1668f571faf6SThomas Gleixner int timer_shutdown_sync(struct timer_list *timer) 1669f571faf6SThomas Gleixner { 1670f571faf6SThomas Gleixner return __timer_delete_sync(timer, true); 1671f571faf6SThomas Gleixner } 1672f571faf6SThomas Gleixner EXPORT_SYMBOL_GPL(timer_shutdown_sync); 1673f571faf6SThomas Gleixner 1674f28d3d53SAnna-Maria Gleixner static void call_timer_fn(struct timer_list *timer, 1675f28d3d53SAnna-Maria Gleixner void (*fn)(struct timer_list *), 1676f28d3d53SAnna-Maria Gleixner unsigned long baseclk) 16775cee9645SThomas Gleixner { 16785cee9645SThomas Gleixner int count = preempt_count(); 16795cee9645SThomas Gleixner 16805cee9645SThomas Gleixner #ifdef CONFIG_LOCKDEP 16815cee9645SThomas Gleixner /* 16825cee9645SThomas Gleixner * It is permissible to free the timer from inside the 16835cee9645SThomas Gleixner * function that is called from it, this we need to take into 16845cee9645SThomas Gleixner * account for lockdep too. To avoid bogus "held lock freed" 16855cee9645SThomas Gleixner * warnings as well as problems when looking into 16865cee9645SThomas Gleixner * timer->lockdep_map, make a copy and use that here. 16875cee9645SThomas Gleixner */ 16885cee9645SThomas Gleixner struct lockdep_map lockdep_map; 16895cee9645SThomas Gleixner 16905cee9645SThomas Gleixner lockdep_copy_map(&lockdep_map, &timer->lockdep_map); 16915cee9645SThomas Gleixner #endif 16925cee9645SThomas Gleixner /* 16935cee9645SThomas Gleixner * Couple the lock chain with the lock chain at 16949b13df3fSThomas Gleixner * timer_delete_sync() by acquiring the lock_map around the fn() 16959b13df3fSThomas Gleixner * call here and in timer_delete_sync(). 16965cee9645SThomas Gleixner */ 16975cee9645SThomas Gleixner lock_map_acquire(&lockdep_map); 16985cee9645SThomas Gleixner 1699f28d3d53SAnna-Maria Gleixner trace_timer_expire_entry(timer, baseclk); 1700354b46b1SKees Cook fn(timer); 17015cee9645SThomas Gleixner trace_timer_expire_exit(timer); 17025cee9645SThomas Gleixner 17035cee9645SThomas Gleixner lock_map_release(&lockdep_map); 17045cee9645SThomas Gleixner 17055cee9645SThomas Gleixner if (count != preempt_count()) { 1706d75f773cSSakari Ailus WARN_ONCE(1, "timer: %pS preempt leak: %08x -> %08x\n", 17075cee9645SThomas Gleixner fn, count, preempt_count()); 17085cee9645SThomas Gleixner /* 17095cee9645SThomas Gleixner * Restore the preempt count. That gives us a decent 17105cee9645SThomas Gleixner * chance to survive and extract information. If the 17115cee9645SThomas Gleixner * callback kept a lock held, bad luck, but not worse 17125cee9645SThomas Gleixner * than the BUG() we had. 17135cee9645SThomas Gleixner */ 17145cee9645SThomas Gleixner preempt_count_set(count); 17155cee9645SThomas Gleixner } 17165cee9645SThomas Gleixner } 17175cee9645SThomas Gleixner 1718500462a9SThomas Gleixner static void expire_timers(struct timer_base *base, struct hlist_head *head) 17195cee9645SThomas Gleixner { 1720f28d3d53SAnna-Maria Gleixner /* 1721f28d3d53SAnna-Maria Gleixner * This value is required only for tracing. base->clk was 1722f28d3d53SAnna-Maria Gleixner * incremented directly before expire_timers was called. But expiry 1723f28d3d53SAnna-Maria Gleixner * is related to the old base->clk value. 1724f28d3d53SAnna-Maria Gleixner */ 1725f28d3d53SAnna-Maria Gleixner unsigned long baseclk = base->clk - 1; 1726f28d3d53SAnna-Maria Gleixner 17271dabbcecSThomas Gleixner while (!hlist_empty(head)) { 1728500462a9SThomas Gleixner struct timer_list *timer; 1729354b46b1SKees Cook void (*fn)(struct timer_list *); 17305cee9645SThomas Gleixner 17311dabbcecSThomas Gleixner timer = hlist_entry(head->first, struct timer_list, entry); 17325cee9645SThomas Gleixner 17335cee9645SThomas Gleixner base->running_timer = timer; 1734500462a9SThomas Gleixner detach_timer(timer, true); 17355cee9645SThomas Gleixner 1736500462a9SThomas Gleixner fn = timer->function; 1737500462a9SThomas Gleixner 1738d02e382cSThomas Gleixner if (WARN_ON_ONCE(!fn)) { 1739d02e382cSThomas Gleixner /* Should never happen. Emphasis on should! */ 1740d02e382cSThomas Gleixner base->running_timer = NULL; 1741d02e382cSThomas Gleixner continue; 1742d02e382cSThomas Gleixner } 1743d02e382cSThomas Gleixner 1744500462a9SThomas Gleixner if (timer->flags & TIMER_IRQSAFE) { 17452287d866SSebastian Andrzej Siewior raw_spin_unlock(&base->lock); 1746f28d3d53SAnna-Maria Gleixner call_timer_fn(timer, fn, baseclk); 17472287d866SSebastian Andrzej Siewior raw_spin_lock(&base->lock); 1748bb7262b2SThomas Gleixner base->running_timer = NULL; 17495cee9645SThomas Gleixner } else { 17502287d866SSebastian Andrzej Siewior raw_spin_unlock_irq(&base->lock); 1751f28d3d53SAnna-Maria Gleixner call_timer_fn(timer, fn, baseclk); 1752bb7262b2SThomas Gleixner raw_spin_lock_irq(&base->lock); 1753030dcdd1SAnna-Maria Gleixner base->running_timer = NULL; 1754030dcdd1SAnna-Maria Gleixner timer_sync_wait_running(base); 17555cee9645SThomas Gleixner } 17565cee9645SThomas Gleixner } 17575cee9645SThomas Gleixner } 1758500462a9SThomas Gleixner 1759d4f7dae8SFrederic Weisbecker static int collect_expired_timers(struct timer_base *base, 1760500462a9SThomas Gleixner struct hlist_head *heads) 1761500462a9SThomas Gleixner { 1762d4f7dae8SFrederic Weisbecker unsigned long clk = base->clk = base->next_expiry; 1763500462a9SThomas Gleixner struct hlist_head *vec; 1764500462a9SThomas Gleixner int i, levels = 0; 1765500462a9SThomas Gleixner unsigned int idx; 1766500462a9SThomas Gleixner 1767500462a9SThomas Gleixner for (i = 0; i < LVL_DEPTH; i++) { 1768500462a9SThomas Gleixner idx = (clk & LVL_MASK) + i * LVL_SIZE; 1769500462a9SThomas Gleixner 1770500462a9SThomas Gleixner if (__test_and_clear_bit(idx, base->pending_map)) { 1771500462a9SThomas Gleixner vec = base->vectors + idx; 1772500462a9SThomas Gleixner hlist_move_list(vec, heads++); 1773500462a9SThomas Gleixner levels++; 1774500462a9SThomas Gleixner } 1775500462a9SThomas Gleixner /* Is it time to look at the next level? */ 1776500462a9SThomas Gleixner if (clk & LVL_CLK_MASK) 1777500462a9SThomas Gleixner break; 1778500462a9SThomas Gleixner /* Shift clock for the next level granularity */ 1779500462a9SThomas Gleixner clk >>= LVL_CLK_SHIFT; 1780500462a9SThomas Gleixner } 1781500462a9SThomas Gleixner return levels; 17825cee9645SThomas Gleixner } 17835cee9645SThomas Gleixner 17845cee9645SThomas Gleixner /* 178523696838SAnna-Maria Gleixner * Find the next pending bucket of a level. Search from level start (@offset) 178623696838SAnna-Maria Gleixner * + @clk upwards and if nothing there, search from start of the level 178723696838SAnna-Maria Gleixner * (@offset) up to @offset + clk. 17885cee9645SThomas Gleixner */ 1789500462a9SThomas Gleixner static int next_pending_bucket(struct timer_base *base, unsigned offset, 1790500462a9SThomas Gleixner unsigned clk) 17915cee9645SThomas Gleixner { 1792500462a9SThomas Gleixner unsigned pos, start = offset + clk; 1793500462a9SThomas Gleixner unsigned end = offset + LVL_SIZE; 17945cee9645SThomas Gleixner 1795500462a9SThomas Gleixner pos = find_next_bit(base->pending_map, end, start); 1796500462a9SThomas Gleixner if (pos < end) 1797500462a9SThomas Gleixner return pos - start; 17985cee9645SThomas Gleixner 1799500462a9SThomas Gleixner pos = find_next_bit(base->pending_map, start, offset); 1800500462a9SThomas Gleixner return pos < start ? pos + LVL_SIZE - start : -1; 18015cee9645SThomas Gleixner } 18025cee9645SThomas Gleixner 1803500462a9SThomas Gleixner /* 180423696838SAnna-Maria Gleixner * Search the first expiring timer in the various clock levels. Caller must 180523696838SAnna-Maria Gleixner * hold base->lock. 1806b5e6f598SAnna-Maria Behnsen * 1807b5e6f598SAnna-Maria Behnsen * Store next expiry time in base->next_expiry. 18085cee9645SThomas Gleixner */ 1809b5e6f598SAnna-Maria Behnsen static void next_expiry_recalc(struct timer_base *base) 18105cee9645SThomas Gleixner { 1811500462a9SThomas Gleixner unsigned long clk, next, adj; 1812500462a9SThomas Gleixner unsigned lvl, offset = 0; 18135cee9645SThomas Gleixner 1814500462a9SThomas Gleixner next = base->clk + NEXT_TIMER_MAX_DELTA; 1815500462a9SThomas Gleixner clk = base->clk; 1816500462a9SThomas Gleixner for (lvl = 0; lvl < LVL_DEPTH; lvl++, offset += LVL_SIZE) { 1817500462a9SThomas Gleixner int pos = next_pending_bucket(base, offset, clk & LVL_MASK); 1818001ec1b3SFrederic Weisbecker unsigned long lvl_clk = clk & LVL_CLK_MASK; 18195cee9645SThomas Gleixner 1820500462a9SThomas Gleixner if (pos >= 0) { 1821500462a9SThomas Gleixner unsigned long tmp = clk + (unsigned long) pos; 18225cee9645SThomas Gleixner 1823500462a9SThomas Gleixner tmp <<= LVL_SHIFT(lvl); 1824500462a9SThomas Gleixner if (time_before(tmp, next)) 1825500462a9SThomas Gleixner next = tmp; 1826001ec1b3SFrederic Weisbecker 1827001ec1b3SFrederic Weisbecker /* 1828001ec1b3SFrederic Weisbecker * If the next expiration happens before we reach 1829001ec1b3SFrederic Weisbecker * the next level, no need to check further. 1830001ec1b3SFrederic Weisbecker */ 1831001ec1b3SFrederic Weisbecker if (pos <= ((LVL_CLK_DIV - lvl_clk) & LVL_CLK_MASK)) 1832001ec1b3SFrederic Weisbecker break; 18335cee9645SThomas Gleixner } 18345cee9645SThomas Gleixner /* 1835500462a9SThomas Gleixner * Clock for the next level. If the current level clock lower 1836500462a9SThomas Gleixner * bits are zero, we look at the next level as is. If not we 1837500462a9SThomas Gleixner * need to advance it by one because that's going to be the 1838500462a9SThomas Gleixner * next expiring bucket in that level. base->clk is the next 1839500462a9SThomas Gleixner * expiring jiffie. So in case of: 1840500462a9SThomas Gleixner * 1841500462a9SThomas Gleixner * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0 1842500462a9SThomas Gleixner * 0 0 0 0 0 0 1843500462a9SThomas Gleixner * 1844500462a9SThomas Gleixner * we have to look at all levels @index 0. With 1845500462a9SThomas Gleixner * 1846500462a9SThomas Gleixner * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0 1847500462a9SThomas Gleixner * 0 0 0 0 0 2 1848500462a9SThomas Gleixner * 1849500462a9SThomas Gleixner * LVL0 has the next expiring bucket @index 2. The upper 1850500462a9SThomas Gleixner * levels have the next expiring bucket @index 1. 1851500462a9SThomas Gleixner * 1852500462a9SThomas Gleixner * In case that the propagation wraps the next level the same 1853500462a9SThomas Gleixner * rules apply: 1854500462a9SThomas Gleixner * 1855500462a9SThomas Gleixner * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0 1856500462a9SThomas Gleixner * 0 0 0 0 F 2 1857500462a9SThomas Gleixner * 1858500462a9SThomas Gleixner * So after looking at LVL0 we get: 1859500462a9SThomas Gleixner * 1860500462a9SThomas Gleixner * LVL5 LVL4 LVL3 LVL2 LVL1 1861500462a9SThomas Gleixner * 0 0 0 1 0 1862500462a9SThomas Gleixner * 1863500462a9SThomas Gleixner * So no propagation from LVL1 to LVL2 because that happened 1864500462a9SThomas Gleixner * with the add already, but then we need to propagate further 1865500462a9SThomas Gleixner * from LVL2 to LVL3. 1866500462a9SThomas Gleixner * 1867500462a9SThomas Gleixner * So the simple check whether the lower bits of the current 1868500462a9SThomas Gleixner * level are 0 or not is sufficient for all cases. 18695cee9645SThomas Gleixner */ 1870001ec1b3SFrederic Weisbecker adj = lvl_clk ? 1 : 0; 1871500462a9SThomas Gleixner clk >>= LVL_CLK_SHIFT; 1872500462a9SThomas Gleixner clk += adj; 18735cee9645SThomas Gleixner } 187431cd0e11SFrederic Weisbecker 1875b5e6f598SAnna-Maria Behnsen base->next_expiry = next; 187631cd0e11SFrederic Weisbecker base->next_expiry_recalc = false; 1877aebacb7fSNicolas Saenz Julienne base->timers_pending = !(next == base->clk + NEXT_TIMER_MAX_DELTA); 18785cee9645SThomas Gleixner } 18795cee9645SThomas Gleixner 1880dc2a0f1fSFrederic Weisbecker #ifdef CONFIG_NO_HZ_COMMON 18815cee9645SThomas Gleixner /* 18825cee9645SThomas Gleixner * Check, if the next hrtimer event is before the next timer wheel 18835cee9645SThomas Gleixner * event: 18845cee9645SThomas Gleixner */ 1885c1ad348bSThomas Gleixner static u64 cmp_next_hrtimer_event(u64 basem, u64 expires) 18865cee9645SThomas Gleixner { 1887c1ad348bSThomas Gleixner u64 nextevt = hrtimer_get_next_event(); 18885cee9645SThomas Gleixner 1889c1ad348bSThomas Gleixner /* 1890c1ad348bSThomas Gleixner * If high resolution timers are enabled 1891c1ad348bSThomas Gleixner * hrtimer_get_next_event() returns KTIME_MAX. 1892c1ad348bSThomas Gleixner */ 1893c1ad348bSThomas Gleixner if (expires <= nextevt) 18945cee9645SThomas Gleixner return expires; 18955cee9645SThomas Gleixner 18965cee9645SThomas Gleixner /* 1897c1ad348bSThomas Gleixner * If the next timer is already expired, return the tick base 1898c1ad348bSThomas Gleixner * time so the tick is fired immediately. 18995cee9645SThomas Gleixner */ 1900c1ad348bSThomas Gleixner if (nextevt <= basem) 1901c1ad348bSThomas Gleixner return basem; 19025cee9645SThomas Gleixner 19035cee9645SThomas Gleixner /* 1904c1ad348bSThomas Gleixner * Round up to the next jiffie. High resolution timers are 1905c1ad348bSThomas Gleixner * off, so the hrtimers are expired in the tick and we need to 1906c1ad348bSThomas Gleixner * make sure that this tick really expires the timer to avoid 1907c1ad348bSThomas Gleixner * a ping pong of the nohz stop code. 1908c1ad348bSThomas Gleixner * 1909c1ad348bSThomas Gleixner * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3 19105cee9645SThomas Gleixner */ 1911c1ad348bSThomas Gleixner return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC; 19125cee9645SThomas Gleixner } 19135cee9645SThomas Gleixner 19145cee9645SThomas Gleixner /** 1915c1ad348bSThomas Gleixner * get_next_timer_interrupt - return the time (clock mono) of the next timer 1916c1ad348bSThomas Gleixner * @basej: base time jiffies 1917c1ad348bSThomas Gleixner * @basem: base time clock monotonic 1918c1ad348bSThomas Gleixner * 1919c1ad348bSThomas Gleixner * Returns the tick aligned clock monotonic time of the next pending 1920c1ad348bSThomas Gleixner * timer or KTIME_MAX if no timer is pending. 19215cee9645SThomas Gleixner */ 1922c1ad348bSThomas Gleixner u64 get_next_timer_interrupt(unsigned long basej, u64 basem) 19235cee9645SThomas Gleixner { 1924500462a9SThomas Gleixner struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); 1925c1ad348bSThomas Gleixner u64 expires = KTIME_MAX; 1926c1ad348bSThomas Gleixner unsigned long nextevt; 1927*bb8caad5SThomas Gleixner bool was_idle; 19285cee9645SThomas Gleixner 19295cee9645SThomas Gleixner /* 19305cee9645SThomas Gleixner * Pretend that there is no timer pending if the cpu is offline. 19315cee9645SThomas Gleixner * Possible pending timers will be migrated later to an active cpu. 19325cee9645SThomas Gleixner */ 19335cee9645SThomas Gleixner if (cpu_is_offline(smp_processor_id())) 19345cee9645SThomas Gleixner return expires; 19355cee9645SThomas Gleixner 19362287d866SSebastian Andrzej Siewior raw_spin_lock(&base->lock); 193731cd0e11SFrederic Weisbecker if (base->next_expiry_recalc) 1938b5e6f598SAnna-Maria Behnsen next_expiry_recalc(base); 193931cd0e11SFrederic Weisbecker nextevt = base->next_expiry; 194031cd0e11SFrederic Weisbecker 1941a683f390SThomas Gleixner /* 1942041ad7bcSThomas Gleixner * We have a fresh next event. Check whether we can forward the 19437a39a508SAnna-Maria Behnsen * base. 1944a683f390SThomas Gleixner */ 19457a39a508SAnna-Maria Behnsen __forward_timer_base(base, basej); 1946a683f390SThomas Gleixner 1947*bb8caad5SThomas Gleixner if (base->timers_pending) { 1948*bb8caad5SThomas Gleixner /* If we missed a tick already, force 0 delta */ 1949*bb8caad5SThomas Gleixner if (time_before(nextevt, basej)) 1950*bb8caad5SThomas Gleixner nextevt = basej; 195134f41c03SMatija Glavinic Pecotic expires = basem + (u64)(nextevt - basej) * TICK_NSEC; 1952*bb8caad5SThomas Gleixner } 1953*bb8caad5SThomas Gleixner 1954a683f390SThomas Gleixner /* 1955*bb8caad5SThomas Gleixner * Base is idle if the next event is more than a tick away. 1956*bb8caad5SThomas Gleixner * 1957*bb8caad5SThomas Gleixner * If the base is marked idle then any timer add operation must forward 1958*bb8caad5SThomas Gleixner * the base clk itself to keep granularity small. This idle logic is 1959*bb8caad5SThomas Gleixner * only maintained for the BASE_STD base, deferrable timers may still 1960*bb8caad5SThomas Gleixner * see large granularity skew (by design). 1961a683f390SThomas Gleixner */ 1962*bb8caad5SThomas Gleixner was_idle = base->is_idle; 1963*bb8caad5SThomas Gleixner base->is_idle = time_after(nextevt, basej + 1); 1964*bb8caad5SThomas Gleixner if (was_idle != base->is_idle) 1965*bb8caad5SThomas Gleixner trace_timer_base_idle(base->is_idle, base->cpu); 1966*bb8caad5SThomas Gleixner 19672287d866SSebastian Andrzej Siewior raw_spin_unlock(&base->lock); 19685cee9645SThomas Gleixner 1969c1ad348bSThomas Gleixner return cmp_next_hrtimer_event(basem, expires); 19705cee9645SThomas Gleixner } 197123696838SAnna-Maria Gleixner 1972a683f390SThomas Gleixner /** 1973a683f390SThomas Gleixner * timer_clear_idle - Clear the idle state of the timer base 1974a683f390SThomas Gleixner * 1975a683f390SThomas Gleixner * Called with interrupts disabled 1976a683f390SThomas Gleixner */ 1977a683f390SThomas Gleixner void timer_clear_idle(void) 1978a683f390SThomas Gleixner { 1979a683f390SThomas Gleixner struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); 1980a683f390SThomas Gleixner 1981a683f390SThomas Gleixner /* 1982a683f390SThomas Gleixner * We do this unlocked. The worst outcome is a remote enqueue sending 1983a683f390SThomas Gleixner * a pointless IPI, but taking the lock would just make the window for 1984a683f390SThomas Gleixner * sending the IPI a few instructions smaller for the cost of taking 1985a683f390SThomas Gleixner * the lock in the exit from idle path. 1986a683f390SThomas Gleixner */ 1987b573c731SAnna-Maria Behnsen if (base->is_idle) { 1988a683f390SThomas Gleixner base->is_idle = false; 1989b573c731SAnna-Maria Behnsen trace_timer_base_idle(false, smp_processor_id()); 1990b573c731SAnna-Maria Behnsen } 1991a683f390SThomas Gleixner } 19925cee9645SThomas Gleixner #endif 19935cee9645SThomas Gleixner 199473420feaSAnna-Maria Gleixner /** 199573420feaSAnna-Maria Gleixner * __run_timers - run all expired timers (if any) on this CPU. 199673420feaSAnna-Maria Gleixner * @base: the timer vector to be processed. 199773420feaSAnna-Maria Gleixner */ 199873420feaSAnna-Maria Gleixner static inline void __run_timers(struct timer_base *base) 199973420feaSAnna-Maria Gleixner { 200073420feaSAnna-Maria Gleixner struct hlist_head heads[LVL_DEPTH]; 200173420feaSAnna-Maria Gleixner int levels; 200273420feaSAnna-Maria Gleixner 2003d4f7dae8SFrederic Weisbecker if (time_before(jiffies, base->next_expiry)) 200473420feaSAnna-Maria Gleixner return; 200573420feaSAnna-Maria Gleixner 2006030dcdd1SAnna-Maria Gleixner timer_base_lock_expiry(base); 20072287d866SSebastian Andrzej Siewior raw_spin_lock_irq(&base->lock); 200873420feaSAnna-Maria Gleixner 2009d4f7dae8SFrederic Weisbecker while (time_after_eq(jiffies, base->clk) && 2010d4f7dae8SFrederic Weisbecker time_after_eq(jiffies, base->next_expiry)) { 201173420feaSAnna-Maria Gleixner levels = collect_expired_timers(base, heads); 201231cd0e11SFrederic Weisbecker /* 2013c54bc0fcSAnna-Maria Behnsen * The two possible reasons for not finding any expired 2014c54bc0fcSAnna-Maria Behnsen * timer at this clk are that all matching timers have been 2015c54bc0fcSAnna-Maria Behnsen * dequeued or no timer has been queued since 2016c54bc0fcSAnna-Maria Behnsen * base::next_expiry was set to base::clk + 2017c54bc0fcSAnna-Maria Behnsen * NEXT_TIMER_MAX_DELTA. 201831cd0e11SFrederic Weisbecker */ 2019c54bc0fcSAnna-Maria Behnsen WARN_ON_ONCE(!levels && !base->next_expiry_recalc 2020c54bc0fcSAnna-Maria Behnsen && base->timers_pending); 20218a2c9c7eSAnna-Maria Behnsen /* 20228a2c9c7eSAnna-Maria Behnsen * While executing timers, base->clk is set 1 offset ahead of 20238a2c9c7eSAnna-Maria Behnsen * jiffies to avoid endless requeuing to current jiffies. 20248a2c9c7eSAnna-Maria Behnsen */ 202573420feaSAnna-Maria Gleixner base->clk++; 2026b5e6f598SAnna-Maria Behnsen next_expiry_recalc(base); 202773420feaSAnna-Maria Gleixner 202873420feaSAnna-Maria Gleixner while (levels--) 202973420feaSAnna-Maria Gleixner expire_timers(base, heads + levels); 203073420feaSAnna-Maria Gleixner } 20312287d866SSebastian Andrzej Siewior raw_spin_unlock_irq(&base->lock); 2032030dcdd1SAnna-Maria Gleixner timer_base_unlock_expiry(base); 203373420feaSAnna-Maria Gleixner } 203473420feaSAnna-Maria Gleixner 20355cee9645SThomas Gleixner /* 20365cee9645SThomas Gleixner * This function runs timers and the timer-tq in bottom half context. 20375cee9645SThomas Gleixner */ 20380766f788SEmese Revfy static __latent_entropy void run_timer_softirq(struct softirq_action *h) 20395cee9645SThomas Gleixner { 2040500462a9SThomas Gleixner struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); 20415cee9645SThomas Gleixner 20425cee9645SThomas Gleixner __run_timers(base); 2043ced6d5c1SAnna-Maria Gleixner if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) 2044500462a9SThomas Gleixner __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF])); 20455cee9645SThomas Gleixner } 20465cee9645SThomas Gleixner 20475cee9645SThomas Gleixner /* 20485cee9645SThomas Gleixner * Called by the local, per-CPU timer interrupt on SMP. 20495cee9645SThomas Gleixner */ 2050cc947f2bSThomas Gleixner static void run_local_timers(void) 20515cee9645SThomas Gleixner { 20524e85876aSThomas Gleixner struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); 20534e85876aSThomas Gleixner 20545cee9645SThomas Gleixner hrtimer_run_queues(); 20554e85876aSThomas Gleixner /* Raise the softirq only if required. */ 2056d4f7dae8SFrederic Weisbecker if (time_before(jiffies, base->next_expiry)) { 2057ed4bbf79SThomas Gleixner if (!IS_ENABLED(CONFIG_NO_HZ_COMMON)) 20584e85876aSThomas Gleixner return; 20594e85876aSThomas Gleixner /* CPU is awake, so check the deferrable base. */ 20604e85876aSThomas Gleixner base++; 2061d4f7dae8SFrederic Weisbecker if (time_before(jiffies, base->next_expiry)) 20624e85876aSThomas Gleixner return; 20634e85876aSThomas Gleixner } 20645cee9645SThomas Gleixner raise_softirq(TIMER_SOFTIRQ); 20655cee9645SThomas Gleixner } 20665cee9645SThomas Gleixner 206758e1177bSKees Cook /* 2068cc947f2bSThomas Gleixner * Called from the timer interrupt handler to charge one tick to the current 2069cc947f2bSThomas Gleixner * process. user_tick is 1 if the tick is user time, 0 for system. 2070cc947f2bSThomas Gleixner */ 2071cc947f2bSThomas Gleixner void update_process_times(int user_tick) 2072cc947f2bSThomas Gleixner { 2073cc947f2bSThomas Gleixner struct task_struct *p = current; 2074cc947f2bSThomas Gleixner 2075cc947f2bSThomas Gleixner /* Note: this timer irq context must be accounted for as well. */ 2076cc947f2bSThomas Gleixner account_process_tick(p, user_tick); 2077cc947f2bSThomas Gleixner run_local_timers(); 2078cc947f2bSThomas Gleixner rcu_sched_clock_irq(user_tick); 2079cc947f2bSThomas Gleixner #ifdef CONFIG_IRQ_WORK 2080cc947f2bSThomas Gleixner if (in_irq()) 2081cc947f2bSThomas Gleixner irq_work_tick(); 2082cc947f2bSThomas Gleixner #endif 2083cc947f2bSThomas Gleixner scheduler_tick(); 2084cc947f2bSThomas Gleixner if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2085cc947f2bSThomas Gleixner run_posix_cpu_timers(); 2086cc947f2bSThomas Gleixner } 2087cc947f2bSThomas Gleixner 2088cc947f2bSThomas Gleixner /* 208958e1177bSKees Cook * Since schedule_timeout()'s timer is defined on the stack, it must store 209058e1177bSKees Cook * the target task on the stack as well. 209158e1177bSKees Cook */ 209258e1177bSKees Cook struct process_timer { 209358e1177bSKees Cook struct timer_list timer; 209458e1177bSKees Cook struct task_struct *task; 209558e1177bSKees Cook }; 209658e1177bSKees Cook 209758e1177bSKees Cook static void process_timeout(struct timer_list *t) 20985cee9645SThomas Gleixner { 209958e1177bSKees Cook struct process_timer *timeout = from_timer(timeout, t, timer); 210058e1177bSKees Cook 210158e1177bSKees Cook wake_up_process(timeout->task); 21025cee9645SThomas Gleixner } 21035cee9645SThomas Gleixner 21045cee9645SThomas Gleixner /** 21055cee9645SThomas Gleixner * schedule_timeout - sleep until timeout 21065cee9645SThomas Gleixner * @timeout: timeout value in jiffies 21075cee9645SThomas Gleixner * 21086e317c32SAlexander Popov * Make the current task sleep until @timeout jiffies have elapsed. 21096e317c32SAlexander Popov * The function behavior depends on the current task state 21106e317c32SAlexander Popov * (see also set_current_state() description): 21115cee9645SThomas Gleixner * 21126e317c32SAlexander Popov * %TASK_RUNNING - the scheduler is called, but the task does not sleep 21136e317c32SAlexander Popov * at all. That happens because sched_submit_work() does nothing for 21146e317c32SAlexander Popov * tasks in %TASK_RUNNING state. 21155cee9645SThomas Gleixner * 21165cee9645SThomas Gleixner * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to 21174b7e9cf9SDouglas Anderson * pass before the routine returns unless the current task is explicitly 21186e317c32SAlexander Popov * woken up, (e.g. by wake_up_process()). 21195cee9645SThomas Gleixner * 21205cee9645SThomas Gleixner * %TASK_INTERRUPTIBLE - the routine may return early if a signal is 21214b7e9cf9SDouglas Anderson * delivered to the current task or the current task is explicitly woken 21224b7e9cf9SDouglas Anderson * up. 21235cee9645SThomas Gleixner * 21246e317c32SAlexander Popov * The current task state is guaranteed to be %TASK_RUNNING when this 21255cee9645SThomas Gleixner * routine returns. 21265cee9645SThomas Gleixner * 21275cee9645SThomas Gleixner * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule 21285cee9645SThomas Gleixner * the CPU away without a bound on the timeout. In this case the return 21295cee9645SThomas Gleixner * value will be %MAX_SCHEDULE_TIMEOUT. 21305cee9645SThomas Gleixner * 21314b7e9cf9SDouglas Anderson * Returns 0 when the timer has expired otherwise the remaining time in 21324b7e9cf9SDouglas Anderson * jiffies will be returned. In all cases the return value is guaranteed 21334b7e9cf9SDouglas Anderson * to be non-negative. 21345cee9645SThomas Gleixner */ 21355cee9645SThomas Gleixner signed long __sched schedule_timeout(signed long timeout) 21365cee9645SThomas Gleixner { 213758e1177bSKees Cook struct process_timer timer; 21385cee9645SThomas Gleixner unsigned long expire; 21395cee9645SThomas Gleixner 21405cee9645SThomas Gleixner switch (timeout) 21415cee9645SThomas Gleixner { 21425cee9645SThomas Gleixner case MAX_SCHEDULE_TIMEOUT: 21435cee9645SThomas Gleixner /* 21445cee9645SThomas Gleixner * These two special cases are useful to be comfortable 21455cee9645SThomas Gleixner * in the caller. Nothing more. We could take 21465cee9645SThomas Gleixner * MAX_SCHEDULE_TIMEOUT from one of the negative value 21475cee9645SThomas Gleixner * but I' d like to return a valid offset (>=0) to allow 21485cee9645SThomas Gleixner * the caller to do everything it want with the retval. 21495cee9645SThomas Gleixner */ 21505cee9645SThomas Gleixner schedule(); 21515cee9645SThomas Gleixner goto out; 21525cee9645SThomas Gleixner default: 21535cee9645SThomas Gleixner /* 21545cee9645SThomas Gleixner * Another bit of PARANOID. Note that the retval will be 21555cee9645SThomas Gleixner * 0 since no piece of kernel is supposed to do a check 21565cee9645SThomas Gleixner * for a negative retval of schedule_timeout() (since it 21575cee9645SThomas Gleixner * should never happens anyway). You just have the printk() 21585cee9645SThomas Gleixner * that will tell you if something is gone wrong and where. 21595cee9645SThomas Gleixner */ 21605cee9645SThomas Gleixner if (timeout < 0) { 21615cee9645SThomas Gleixner printk(KERN_ERR "schedule_timeout: wrong timeout " 21625cee9645SThomas Gleixner "value %lx\n", timeout); 21635cee9645SThomas Gleixner dump_stack(); 2164600642aeSPeter Zijlstra __set_current_state(TASK_RUNNING); 21655cee9645SThomas Gleixner goto out; 21665cee9645SThomas Gleixner } 21675cee9645SThomas Gleixner } 21685cee9645SThomas Gleixner 21695cee9645SThomas Gleixner expire = timeout + jiffies; 21705cee9645SThomas Gleixner 217158e1177bSKees Cook timer.task = current; 217258e1177bSKees Cook timer_setup_on_stack(&timer.timer, process_timeout, 0); 217390c01894SEric Dumazet __mod_timer(&timer.timer, expire, MOD_TIMER_NOTPENDING); 21745cee9645SThomas Gleixner schedule(); 21759a5a3056SThomas Gleixner del_timer_sync(&timer.timer); 21765cee9645SThomas Gleixner 21775cee9645SThomas Gleixner /* Remove the timer from the object tracker */ 217858e1177bSKees Cook destroy_timer_on_stack(&timer.timer); 21795cee9645SThomas Gleixner 21805cee9645SThomas Gleixner timeout = expire - jiffies; 21815cee9645SThomas Gleixner 21825cee9645SThomas Gleixner out: 21835cee9645SThomas Gleixner return timeout < 0 ? 0 : timeout; 21845cee9645SThomas Gleixner } 21855cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout); 21865cee9645SThomas Gleixner 21875cee9645SThomas Gleixner /* 21885cee9645SThomas Gleixner * We can use __set_current_state() here because schedule_timeout() calls 21895cee9645SThomas Gleixner * schedule() unconditionally. 21905cee9645SThomas Gleixner */ 21915cee9645SThomas Gleixner signed long __sched schedule_timeout_interruptible(signed long timeout) 21925cee9645SThomas Gleixner { 21935cee9645SThomas Gleixner __set_current_state(TASK_INTERRUPTIBLE); 21945cee9645SThomas Gleixner return schedule_timeout(timeout); 21955cee9645SThomas Gleixner } 21965cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_interruptible); 21975cee9645SThomas Gleixner 21985cee9645SThomas Gleixner signed long __sched schedule_timeout_killable(signed long timeout) 21995cee9645SThomas Gleixner { 22005cee9645SThomas Gleixner __set_current_state(TASK_KILLABLE); 22015cee9645SThomas Gleixner return schedule_timeout(timeout); 22025cee9645SThomas Gleixner } 22035cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_killable); 22045cee9645SThomas Gleixner 22055cee9645SThomas Gleixner signed long __sched schedule_timeout_uninterruptible(signed long timeout) 22065cee9645SThomas Gleixner { 22075cee9645SThomas Gleixner __set_current_state(TASK_UNINTERRUPTIBLE); 22085cee9645SThomas Gleixner return schedule_timeout(timeout); 22095cee9645SThomas Gleixner } 22105cee9645SThomas Gleixner EXPORT_SYMBOL(schedule_timeout_uninterruptible); 22115cee9645SThomas Gleixner 221269b27bafSAndrew Morton /* 221369b27bafSAndrew Morton * Like schedule_timeout_uninterruptible(), except this task will not contribute 221469b27bafSAndrew Morton * to load average. 221569b27bafSAndrew Morton */ 221669b27bafSAndrew Morton signed long __sched schedule_timeout_idle(signed long timeout) 221769b27bafSAndrew Morton { 221869b27bafSAndrew Morton __set_current_state(TASK_IDLE); 221969b27bafSAndrew Morton return schedule_timeout(timeout); 222069b27bafSAndrew Morton } 222169b27bafSAndrew Morton EXPORT_SYMBOL(schedule_timeout_idle); 222269b27bafSAndrew Morton 22235cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 2224494af3edSThomas Gleixner static void migrate_timer_list(struct timer_base *new_base, struct hlist_head *head) 22255cee9645SThomas Gleixner { 22265cee9645SThomas Gleixner struct timer_list *timer; 22270eeda71bSThomas Gleixner int cpu = new_base->cpu; 22285cee9645SThomas Gleixner 22291dabbcecSThomas Gleixner while (!hlist_empty(head)) { 22301dabbcecSThomas Gleixner timer = hlist_entry(head->first, struct timer_list, entry); 22315cee9645SThomas Gleixner detach_timer(timer, false); 22320eeda71bSThomas Gleixner timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu; 22335cee9645SThomas Gleixner internal_add_timer(new_base, timer); 22345cee9645SThomas Gleixner } 22355cee9645SThomas Gleixner } 22365cee9645SThomas Gleixner 223726456f87SThomas Gleixner int timers_prepare_cpu(unsigned int cpu) 223826456f87SThomas Gleixner { 223926456f87SThomas Gleixner struct timer_base *base; 224026456f87SThomas Gleixner int b; 224126456f87SThomas Gleixner 224226456f87SThomas Gleixner for (b = 0; b < NR_BASES; b++) { 224326456f87SThomas Gleixner base = per_cpu_ptr(&timer_bases[b], cpu); 224426456f87SThomas Gleixner base->clk = jiffies; 224526456f87SThomas Gleixner base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA; 22462731aa7dSAnna-Maria Behnsen base->next_expiry_recalc = false; 2247aebacb7fSNicolas Saenz Julienne base->timers_pending = false; 224826456f87SThomas Gleixner base->is_idle = false; 224926456f87SThomas Gleixner } 225026456f87SThomas Gleixner return 0; 225126456f87SThomas Gleixner } 225226456f87SThomas Gleixner 225324f73b99SRichard Cochran int timers_dead_cpu(unsigned int cpu) 22545cee9645SThomas Gleixner { 2255494af3edSThomas Gleixner struct timer_base *old_base; 2256494af3edSThomas Gleixner struct timer_base *new_base; 2257500462a9SThomas Gleixner int b, i; 22585cee9645SThomas Gleixner 2259500462a9SThomas Gleixner for (b = 0; b < NR_BASES; b++) { 2260500462a9SThomas Gleixner old_base = per_cpu_ptr(&timer_bases[b], cpu); 2261500462a9SThomas Gleixner new_base = get_cpu_ptr(&timer_bases[b]); 22625cee9645SThomas Gleixner /* 22635cee9645SThomas Gleixner * The caller is globally serialized and nobody else 22645cee9645SThomas Gleixner * takes two locks at once, deadlock is not possible. 22655cee9645SThomas Gleixner */ 22662287d866SSebastian Andrzej Siewior raw_spin_lock_irq(&new_base->lock); 22672287d866SSebastian Andrzej Siewior raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); 22685cee9645SThomas Gleixner 2269c52232a4SLingutla Chandrasekhar /* 2270c52232a4SLingutla Chandrasekhar * The current CPUs base clock might be stale. Update it 2271c52232a4SLingutla Chandrasekhar * before moving the timers over. 2272c52232a4SLingutla Chandrasekhar */ 2273c52232a4SLingutla Chandrasekhar forward_timer_base(new_base); 2274c52232a4SLingutla Chandrasekhar 227582ed6f7eSThomas Gleixner WARN_ON_ONCE(old_base->running_timer); 227682ed6f7eSThomas Gleixner old_base->running_timer = NULL; 22775cee9645SThomas Gleixner 2278500462a9SThomas Gleixner for (i = 0; i < WHEEL_SIZE; i++) 2279500462a9SThomas Gleixner migrate_timer_list(new_base, old_base->vectors + i); 22808def9060SViresh Kumar 22812287d866SSebastian Andrzej Siewior raw_spin_unlock(&old_base->lock); 22822287d866SSebastian Andrzej Siewior raw_spin_unlock_irq(&new_base->lock); 2283494af3edSThomas Gleixner put_cpu_ptr(&timer_bases); 22845cee9645SThomas Gleixner } 228524f73b99SRichard Cochran return 0; 22865cee9645SThomas Gleixner } 22875cee9645SThomas Gleixner 22883650b57fSPeter Zijlstra #endif /* CONFIG_HOTPLUG_CPU */ 22895cee9645SThomas Gleixner 22900eeda71bSThomas Gleixner static void __init init_timer_cpu(int cpu) 22918def9060SViresh Kumar { 2292500462a9SThomas Gleixner struct timer_base *base; 2293500462a9SThomas Gleixner int i; 22943650b57fSPeter Zijlstra 2295500462a9SThomas Gleixner for (i = 0; i < NR_BASES; i++) { 2296500462a9SThomas Gleixner base = per_cpu_ptr(&timer_bases[i], cpu); 22978def9060SViresh Kumar base->cpu = cpu; 22982287d866SSebastian Andrzej Siewior raw_spin_lock_init(&base->lock); 2299494af3edSThomas Gleixner base->clk = jiffies; 2300dc2a0f1fSFrederic Weisbecker base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA; 2301030dcdd1SAnna-Maria Gleixner timer_base_init_expiry_lock(base); 2302500462a9SThomas Gleixner } 23038def9060SViresh Kumar } 23048def9060SViresh Kumar 23058def9060SViresh Kumar static void __init init_timer_cpus(void) 23068def9060SViresh Kumar { 23078def9060SViresh Kumar int cpu; 23088def9060SViresh Kumar 23090eeda71bSThomas Gleixner for_each_possible_cpu(cpu) 23100eeda71bSThomas Gleixner init_timer_cpu(cpu); 23118def9060SViresh Kumar } 23125cee9645SThomas Gleixner 23135cee9645SThomas Gleixner void __init init_timers(void) 23145cee9645SThomas Gleixner { 23158def9060SViresh Kumar init_timer_cpus(); 23161fb497ddSThomas Gleixner posix_cputimers_init_work(); 23175cee9645SThomas Gleixner open_softirq(TIMER_SOFTIRQ, run_timer_softirq); 23185cee9645SThomas Gleixner } 23195cee9645SThomas Gleixner 23205cee9645SThomas Gleixner /** 23215cee9645SThomas Gleixner * msleep - sleep safely even with waitqueue interruptions 23225cee9645SThomas Gleixner * @msecs: Time in milliseconds to sleep for 23235cee9645SThomas Gleixner */ 23245cee9645SThomas Gleixner void msleep(unsigned int msecs) 23255cee9645SThomas Gleixner { 23265cee9645SThomas Gleixner unsigned long timeout = msecs_to_jiffies(msecs) + 1; 23275cee9645SThomas Gleixner 23285cee9645SThomas Gleixner while (timeout) 23295cee9645SThomas Gleixner timeout = schedule_timeout_uninterruptible(timeout); 23305cee9645SThomas Gleixner } 23315cee9645SThomas Gleixner 23325cee9645SThomas Gleixner EXPORT_SYMBOL(msleep); 23335cee9645SThomas Gleixner 23345cee9645SThomas Gleixner /** 23355cee9645SThomas Gleixner * msleep_interruptible - sleep waiting for signals 23365cee9645SThomas Gleixner * @msecs: Time in milliseconds to sleep for 23375cee9645SThomas Gleixner */ 23385cee9645SThomas Gleixner unsigned long msleep_interruptible(unsigned int msecs) 23395cee9645SThomas Gleixner { 23405cee9645SThomas Gleixner unsigned long timeout = msecs_to_jiffies(msecs) + 1; 23415cee9645SThomas Gleixner 23425cee9645SThomas Gleixner while (timeout && !signal_pending(current)) 23435cee9645SThomas Gleixner timeout = schedule_timeout_interruptible(timeout); 23445cee9645SThomas Gleixner return jiffies_to_msecs(timeout); 23455cee9645SThomas Gleixner } 23465cee9645SThomas Gleixner 23475cee9645SThomas Gleixner EXPORT_SYMBOL(msleep_interruptible); 23485cee9645SThomas Gleixner 23495cee9645SThomas Gleixner /** 2350e4779015SSeongJae Park * usleep_range_state - Sleep for an approximate time in a given state 23515cee9645SThomas Gleixner * @min: Minimum time in usecs to sleep 23525cee9645SThomas Gleixner * @max: Maximum time in usecs to sleep 2353e4779015SSeongJae Park * @state: State of the current task that will be while sleeping 2354b5227d03SBjorn Helgaas * 2355b5227d03SBjorn Helgaas * In non-atomic context where the exact wakeup time is flexible, use 2356e4779015SSeongJae Park * usleep_range_state() instead of udelay(). The sleep improves responsiveness 2357b5227d03SBjorn Helgaas * by avoiding the CPU-hogging busy-wait of udelay(), and the range reduces 2358b5227d03SBjorn Helgaas * power usage by allowing hrtimers to take advantage of an already- 2359b5227d03SBjorn Helgaas * scheduled interrupt instead of scheduling a new one just for this sleep. 23605cee9645SThomas Gleixner */ 2361e4779015SSeongJae Park void __sched usleep_range_state(unsigned long min, unsigned long max, 2362e4779015SSeongJae Park unsigned int state) 23635cee9645SThomas Gleixner { 23646c5e9059SDouglas Anderson ktime_t exp = ktime_add_us(ktime_get(), min); 23656c5e9059SDouglas Anderson u64 delta = (u64)(max - min) * NSEC_PER_USEC; 23666c5e9059SDouglas Anderson 23676c5e9059SDouglas Anderson for (;;) { 2368e4779015SSeongJae Park __set_current_state(state); 23696c5e9059SDouglas Anderson /* Do not return before the requested sleep time has elapsed */ 23706c5e9059SDouglas Anderson if (!schedule_hrtimeout_range(&exp, delta, HRTIMER_MODE_ABS)) 23716c5e9059SDouglas Anderson break; 23726c5e9059SDouglas Anderson } 23735cee9645SThomas Gleixner } 2374e4779015SSeongJae Park EXPORT_SYMBOL(usleep_range_state); 2375