Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  Kernel internal timers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Copyright (C) 1991, 1992  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *  1997-01-28  Modified by Finn Arne Gangstad to make timers scale better.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *  1997-09-10  Updated NTP code according to technical memorandum Jan '96
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *              "A Kernel Model for Precision Timekeeping" by Dave Mills
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *  1998-12-24  Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *              serialize accesses to xtime/lost_ticks).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *                              Copyright (C) 1998  Andrea Arcangeli
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *  1999-03-10  Improved NTP compatibility by Ulrich Windl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *  2002-05-31	Move sys_sysinfo here and make its locking sane, Robert Love
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *  2000-10-05  Implemented scalable SMP per-CPU timer handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *                              Copyright (C) 2000, 2001, 2002  Ingo Molnar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *              Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/kernel_stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/pid_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/thread_info.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/posix-timers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/irq_work.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/sched/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/sched/nohz.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #include <asm/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #include <asm/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #include "tick-internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #include <trace/events/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #undef CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #include <trace/hooks/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) EXPORT_TRACEPOINT_SYMBOL_GPL(hrtimer_expire_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) EXPORT_TRACEPOINT_SYMBOL_GPL(hrtimer_expire_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) __visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) EXPORT_SYMBOL(jiffies_64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * LVL_SIZE buckets. Each level is driven by its own clock and therefor each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * level has a different granularity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * The level granularity is:		LVL_CLK_DIV ^ lvl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * The level clock frequency is:	HZ / (LVL_CLK_DIV ^ level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * The array level of a newly armed timer depends on the relative expiry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * time. The farther the expiry time is away the higher the array level and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * therefor the granularity becomes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  * Contrary to the original timer wheel implementation, which aims for 'exact'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * expiry of the timers, this implementation removes the need for recascading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * the timers into the lower array levels. The previous 'classic' timer wheel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * implementation of the kernel already violated the 'exact' expiry by adding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * slack to the expiry time to provide batched expiration. The granularity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * levels provide implicit batching.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * This is an optimization of the original timer wheel implementation for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * majority of the timer wheel use cases: timeouts. The vast majority of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * timeout timers (networking, disk I/O ...) are canceled before expiry. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * the timeout expires it indicates that normal operation is disturbed, so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * does not matter much whether the timeout comes with a slight delay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * The only exception to this are networking timers with a small expiry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * time. They rely on the granularity. Those fit into the first wheel level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * which has HZ granularity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * We don't have cascading anymore. timers with a expiry time above the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * capacity of the last wheel level are force expired at the maximum timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * value of the last wheel level. From data sampling we know that the maximum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * value observed is 5 days (network connection tracking), so this should not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * be an issue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * The currently chosen array constants values are a good compromise between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * array size and granularity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * This results in the following granularity and range levels:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * HZ 1000 steps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  * Level Offset  Granularity            Range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  *  0      0         1 ms                0 ms -         63 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  *  1     64         8 ms               64 ms -        511 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  *  2    128        64 ms              512 ms -       4095 ms (512ms - ~4s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  *  3    192       512 ms             4096 ms -      32767 ms (~4s - ~32s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  *  4    256      4096 ms (~4s)      32768 ms -     262143 ms (~32s - ~4m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  *  5    320     32768 ms (~32s)    262144 ms -    2097151 ms (~4m - ~34m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  *  6    384    262144 ms (~4m)    2097152 ms -   16777215 ms (~34m - ~4h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  *  7    448   2097152 ms (~34m)  16777216 ms -  134217727 ms (~4h - ~1d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  *  8    512  16777216 ms (~4h)  134217728 ms - 1073741822 ms (~1d - ~12d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * HZ  300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  * Level Offset  Granularity            Range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  *  0	   0         3 ms                0 ms -        210 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  *  1	  64        26 ms              213 ms -       1703 ms (213ms - ~1s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  *  2	 128       213 ms             1706 ms -      13650 ms (~1s - ~13s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  *  3	 192      1706 ms (~1s)      13653 ms -     109223 ms (~13s - ~1m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  *  4	 256     13653 ms (~13s)    109226 ms -     873810 ms (~1m - ~14m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  *  5	 320    109226 ms (~1m)     873813 ms -    6990503 ms (~14m - ~1h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128)  *  6	 384    873813 ms (~14m)   6990506 ms -   55924050 ms (~1h - ~15h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  *  7	 448   6990506 ms (~1h)   55924053 ms -  447392423 ms (~15h - ~5d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  *  8    512  55924053 ms (~15h) 447392426 ms - 3579139406 ms (~5d - ~41d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132)  * HZ  250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133)  * Level Offset  Granularity            Range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  *  0	   0         4 ms                0 ms -        255 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  *  1	  64        32 ms              256 ms -       2047 ms (256ms - ~2s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136)  *  2	 128       256 ms             2048 ms -      16383 ms (~2s - ~16s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137)  *  3	 192      2048 ms (~2s)      16384 ms -     131071 ms (~16s - ~2m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138)  *  4	 256     16384 ms (~16s)    131072 ms -    1048575 ms (~2m - ~17m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139)  *  5	 320    131072 ms (~2m)    1048576 ms -    8388607 ms (~17m - ~2h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140)  *  6	 384   1048576 ms (~17m)   8388608 ms -   67108863 ms (~2h - ~18h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141)  *  7	 448   8388608 ms (~2h)   67108864 ms -  536870911 ms (~18h - ~6d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  *  8    512  67108864 ms (~18h) 536870912 ms - 4294967288 ms (~6d - ~49d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144)  * HZ  100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145)  * Level Offset  Granularity            Range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  *  0	   0         10 ms               0 ms -        630 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  *  1	  64         80 ms             640 ms -       5110 ms (640ms - ~5s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  *  2	 128        640 ms            5120 ms -      40950 ms (~5s - ~40s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  *  3	 192       5120 ms (~5s)     40960 ms -     327670 ms (~40s - ~5m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  *  4	 256      40960 ms (~40s)   327680 ms -    2621430 ms (~5m - ~43m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151)  *  5	 320     327680 ms (~5m)   2621440 ms -   20971510 ms (~43m - ~5h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152)  *  6	 384    2621440 ms (~43m) 20971520 ms -  167772150 ms (~5h - ~1d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153)  *  7	 448   20971520 ms (~5h) 167772160 ms - 1342177270 ms (~1d - ~15d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) /* Clock divisor for the next level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) #define LVL_CLK_SHIFT	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) #define LVL_CLK_DIV	(1UL << LVL_CLK_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) #define LVL_CLK_MASK	(LVL_CLK_DIV - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) #define LVL_SHIFT(n)	((n) * LVL_CLK_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) #define LVL_GRAN(n)	(1UL << LVL_SHIFT(n))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  * The time start value for each level to select the bucket at enqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  * time. We start from the last possible delta of the previous level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * so that we can later add an extra LVL_GRAN(n) to n (see calc_index()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) #define LVL_START(n)	((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) /* Size of each clock level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) #define LVL_BITS	6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) #define LVL_SIZE	(1UL << LVL_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) #define LVL_MASK	(LVL_SIZE - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) #define LVL_OFFS(n)	((n) * LVL_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) /* Level depth */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) #if HZ > 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) # define LVL_DEPTH	9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) # else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) # define LVL_DEPTH	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) /* The cutoff (max. capacity of the wheel) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) #define WHEEL_TIMEOUT_CUTOFF	(LVL_START(LVL_DEPTH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) #define WHEEL_TIMEOUT_MAX	(WHEEL_TIMEOUT_CUTOFF - LVL_GRAN(LVL_DEPTH - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188)  * The resulting wheel size. If NOHZ is configured we allocate two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189)  * wheels so we have a separate storage for the deferrable timers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) #define WHEEL_SIZE	(LVL_SIZE * LVL_DEPTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) #ifdef CONFIG_NO_HZ_COMMON
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) # define NR_BASES	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) # define BASE_STD	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) # define BASE_DEF	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) # define NR_BASES	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) # define BASE_STD	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) # define BASE_DEF	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) struct timer_base {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	raw_spinlock_t		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	struct timer_list	*running_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) #ifdef CONFIG_PREEMPT_RT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	spinlock_t		expiry_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	atomic_t		timer_waiters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	unsigned long		clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	unsigned long		next_expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	unsigned int		cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	bool			next_expiry_recalc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	bool			is_idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	bool			timers_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	DECLARE_BITMAP(pending_map, WHEEL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	struct hlist_head	vectors[WHEEL_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) } ____cacheline_aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) #ifdef CONFIG_NO_HZ_COMMON
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) static DEFINE_STATIC_KEY_FALSE(timers_nohz_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) static DEFINE_MUTEX(timer_keys_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) static void timer_update_keys(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) static DECLARE_WORK(timer_update_work, timer_update_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) unsigned int sysctl_timer_migration = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) DEFINE_STATIC_KEY_FALSE(timers_migration_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) static void timers_update_migration(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	if (sysctl_timer_migration && tick_nohz_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		static_branch_enable(&timers_migration_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		static_branch_disable(&timers_migration_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) static inline void timers_update_migration(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) #endif /* !CONFIG_SMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) static void timer_update_keys(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	mutex_lock(&timer_keys_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	timers_update_migration();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	static_branch_enable(&timers_nohz_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	mutex_unlock(&timer_keys_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) void timers_update_nohz(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	schedule_work(&timer_update_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) int timer_migration_handler(struct ctl_table *table, int write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 			    void *buffer, size_t *lenp, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	mutex_lock(&timer_keys_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	if (!ret && write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		timers_update_migration();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	mutex_unlock(&timer_keys_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) static inline bool is_timers_nohz_active(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	return static_branch_unlikely(&timers_nohz_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) static inline bool is_timers_nohz_active(void) { return false; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) #endif /* NO_HZ_COMMON */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) static unsigned long round_jiffies_common(unsigned long j, int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		bool force_up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	int rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	unsigned long original = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	 * We don't want all cpus firing their timers at once hitting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	 * same lock or cachelines, so we skew each extra cpu with an extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	 * 3 jiffies. This 3 jiffies came originally from the mm/ code which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	 * already did this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	 * The skew is done by adding 3*cpunr, then round, then subtract this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	 * extra offset again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	j += cpu * 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	rem = j % HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	 * If the target jiffie is just after a whole second (which can happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	 * due to delays of the timer irq, long irq off times etc etc) then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	 * we should round down to the whole second, not up. Use 1/4th second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	 * as cutoff for this rounding as an extreme upper bound for this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	 * But never round down if @force_up is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	if (rem < HZ/4 && !force_up) /* round down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		j = j - rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	else /* round up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		j = j - rem + HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	/* now that we have rounded, subtract the extra skew again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	j -= cpu * 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	 * Make sure j is still in the future. Otherwise return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	 * unmodified value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	return time_is_after_jiffies(j) ? j : original;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321)  * __round_jiffies - function to round jiffies to a full second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322)  * @j: the time in (absolute) jiffies that should be rounded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323)  * @cpu: the processor number on which the timeout will happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325)  * __round_jiffies() rounds an absolute time in the future (in jiffies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326)  * up or down to (approximately) full seconds. This is useful for timers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  * for which the exact time they fire does not matter too much, as long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328)  * they fire approximately every X seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330)  * By rounding these timers to whole seconds, all such timers will fire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331)  * at the same time, rather than at various times spread out. The goal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332)  * of this is to have the CPU wake up less, which saves power.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334)  * The exact rounding is skewed for each processor to avoid all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * processors firing at the exact same time, which could lead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * to lock contention or spurious cache line bouncing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * The return value is the rounded version of the @j parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) unsigned long __round_jiffies(unsigned long j, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	return round_jiffies_common(j, cpu, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) EXPORT_SYMBOL_GPL(__round_jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347)  * __round_jiffies_relative - function to round jiffies to a full second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348)  * @j: the time in (relative) jiffies that should be rounded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349)  * @cpu: the processor number on which the timeout will happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351)  * __round_jiffies_relative() rounds a time delta  in the future (in jiffies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352)  * up or down to (approximately) full seconds. This is useful for timers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  * for which the exact time they fire does not matter too much, as long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354)  * they fire approximately every X seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  * By rounding these timers to whole seconds, all such timers will fire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  * at the same time, rather than at various times spread out. The goal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  * of this is to have the CPU wake up less, which saves power.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  * The exact rounding is skewed for each processor to avoid all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  * processors firing at the exact same time, which could lead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362)  * to lock contention or spurious cache line bouncing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364)  * The return value is the rounded version of the @j parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) unsigned long __round_jiffies_relative(unsigned long j, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	unsigned long j0 = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	/* Use j0 because jiffies might change while we run */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	return round_jiffies_common(j + j0, cpu, false) - j0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) EXPORT_SYMBOL_GPL(__round_jiffies_relative);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  * round_jiffies - function to round jiffies to a full second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  * @j: the time in (absolute) jiffies that should be rounded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  * round_jiffies() rounds an absolute time in the future (in jiffies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  * up or down to (approximately) full seconds. This is useful for timers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  * for which the exact time they fire does not matter too much, as long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382)  * they fire approximately every X seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384)  * By rounding these timers to whole seconds, all such timers will fire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385)  * at the same time, rather than at various times spread out. The goal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386)  * of this is to have the CPU wake up less, which saves power.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388)  * The return value is the rounded version of the @j parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) unsigned long round_jiffies(unsigned long j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	return round_jiffies_common(j, raw_smp_processor_id(), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) EXPORT_SYMBOL_GPL(round_jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  * round_jiffies_relative - function to round jiffies to a full second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * @j: the time in (relative) jiffies that should be rounded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400)  * round_jiffies_relative() rounds a time delta  in the future (in jiffies)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401)  * up or down to (approximately) full seconds. This is useful for timers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402)  * for which the exact time they fire does not matter too much, as long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403)  * they fire approximately every X seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  * By rounding these timers to whole seconds, all such timers will fire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  * at the same time, rather than at various times spread out. The goal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  * of this is to have the CPU wake up less, which saves power.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409)  * The return value is the rounded version of the @j parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) unsigned long round_jiffies_relative(unsigned long j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	return __round_jiffies_relative(j, raw_smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) EXPORT_SYMBOL_GPL(round_jiffies_relative);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  * __round_jiffies_up - function to round jiffies up to a full second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * @j: the time in (absolute) jiffies that should be rounded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  * @cpu: the processor number on which the timeout will happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  * This is the same as __round_jiffies() except that it will never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  * round down.  This is useful for timeouts for which the exact time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  * of firing does not matter too much, as long as they don't fire too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425)  * early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) unsigned long __round_jiffies_up(unsigned long j, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	return round_jiffies_common(j, cpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) EXPORT_SYMBOL_GPL(__round_jiffies_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  * __round_jiffies_up_relative - function to round jiffies up to a full second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  * @j: the time in (relative) jiffies that should be rounded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436)  * @cpu: the processor number on which the timeout will happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  * This is the same as __round_jiffies_relative() except that it will never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439)  * round down.  This is useful for timeouts for which the exact time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440)  * of firing does not matter too much, as long as they don't fire too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441)  * early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) unsigned long __round_jiffies_up_relative(unsigned long j, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	unsigned long j0 = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	/* Use j0 because jiffies might change while we run */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	return round_jiffies_common(j + j0, cpu, true) - j0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  * round_jiffies_up - function to round jiffies up to a full second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * @j: the time in (absolute) jiffies that should be rounded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456)  * This is the same as round_jiffies() except that it will never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  * round down.  This is useful for timeouts for which the exact time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458)  * of firing does not matter too much, as long as they don't fire too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459)  * early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) unsigned long round_jiffies_up(unsigned long j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	return round_jiffies_common(j, raw_smp_processor_id(), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) EXPORT_SYMBOL_GPL(round_jiffies_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468)  * round_jiffies_up_relative - function to round jiffies up to a full second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469)  * @j: the time in (relative) jiffies that should be rounded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471)  * This is the same as round_jiffies_relative() except that it will never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472)  * round down.  This is useful for timeouts for which the exact time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473)  * of firing does not matter too much, as long as they don't fire too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474)  * early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) unsigned long round_jiffies_up_relative(unsigned long j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	return __round_jiffies_up_relative(j, raw_smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static inline unsigned int timer_get_idx(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) static inline void timer_set_idx(struct timer_list *timer, unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	timer->flags = (timer->flags & ~TIMER_ARRAYMASK) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 			idx << TIMER_ARRAYSHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  * Helper function to calculate the array index for a given expiry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) static inline unsigned calc_index(unsigned long expires, unsigned lvl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 				  unsigned long *bucket_expiry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	 * The timer wheel has to guarantee that a timer does not fire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	 * early. Early expiry can happen due to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	 * - Timer is armed at the edge of a tick
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	 * - Truncation of the expiry time in the outer wheel levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	 * Round up with level granularity to prevent this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	trace_android_vh_timer_calc_index(lvl, &expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	expires = (expires + LVL_GRAN(lvl)) >> LVL_SHIFT(lvl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	*bucket_expiry = expires << LVL_SHIFT(lvl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	return LVL_OFFS(lvl) + (expires & LVL_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) static int calc_wheel_index(unsigned long expires, unsigned long clk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 			    unsigned long *bucket_expiry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	unsigned long delta = expires - clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	if (delta < LVL_START(1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		idx = calc_index(expires, 0, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	} else if (delta < LVL_START(2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		idx = calc_index(expires, 1, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	} else if (delta < LVL_START(3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		idx = calc_index(expires, 2, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	} else if (delta < LVL_START(4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		idx = calc_index(expires, 3, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	} else if (delta < LVL_START(5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		idx = calc_index(expires, 4, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	} else if (delta < LVL_START(6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		idx = calc_index(expires, 5, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	} else if (delta < LVL_START(7)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		idx = calc_index(expires, 6, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	} else if (LVL_DEPTH > 8 && delta < LVL_START(8)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		idx = calc_index(expires, 7, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	} else if ((long) delta < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		idx = clk & LVL_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		*bucket_expiry = clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		 * Force expire obscene large timeouts to expire at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		 * capacity limit of the wheel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		if (delta >= WHEEL_TIMEOUT_CUTOFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			expires = clk + WHEEL_TIMEOUT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		idx = calc_index(expires, LVL_DEPTH - 1, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	if (!is_timers_nohz_active())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	 * TODO: This wants some optimizing similar to the code below, but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	 * will do that when we switch from push to pull for deferrable timers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	if (timer->flags & TIMER_DEFERRABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		if (tick_nohz_full_cpu(base->cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 			wake_up_nohz_cpu(base->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	 * We might have to IPI the remote CPU if the base is idle and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	 * timer is not deferrable. If the other CPU is on the way to idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	 * then it can't set base->is_idle as we hold the base lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	if (base->is_idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		wake_up_nohz_cpu(base->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580)  * Enqueue the timer into the hash bucket, mark it pending in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581)  * the bitmap, store the index in the timer flags then wake up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582)  * the target CPU if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) static void enqueue_timer(struct timer_base *base, struct timer_list *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			  unsigned int idx, unsigned long bucket_expiry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	hlist_add_head(&timer->entry, base->vectors + idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	__set_bit(idx, base->pending_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	timer_set_idx(timer, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	trace_timer_start(timer, timer->expires, timer->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	 * Check whether this is the new first expiring timer. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	 * effective expiry time of the timer is required here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	 * (bucket_expiry) instead of timer->expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	if (time_before(bucket_expiry, base->next_expiry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		 * Set the next expiry time and kick the CPU so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		 * can reevaluate the wheel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		base->next_expiry = bucket_expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		base->timers_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		base->next_expiry_recalc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		trigger_dyntick_cpu(base, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) static void internal_add_timer(struct timer_base *base, struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	unsigned long bucket_expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	idx = calc_wheel_index(timer->expires, base->clk, &bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	enqueue_timer(base, timer, idx, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) static const struct debug_obj_descr timer_debug_descr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) static void *timer_debug_hint(void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	return ((struct timer_list *) addr)->function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) static bool timer_is_static_object(void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	struct timer_list *timer = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	return (timer->entry.pprev == NULL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		timer->entry.next == TIMER_ENTRY_STATIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638)  * fixup_init is called when:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639)  * - an active object is initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) static bool timer_fixup_init(void *addr, enum debug_obj_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	struct timer_list *timer = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	case ODEBUG_STATE_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		del_timer_sync(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		debug_object_init(timer, &timer_debug_descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) /* Stub timer callback for improperly used timers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) static void stub_timer(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662)  * fixup_activate is called when:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663)  * - an active object is activated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664)  * - an unknown non-static object is activated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) static bool timer_fixup_activate(void *addr, enum debug_obj_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	struct timer_list *timer = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	case ODEBUG_STATE_NOTAVAILABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		timer_setup(timer, stub_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	case ODEBUG_STATE_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684)  * fixup_free is called when:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685)  * - an active object is freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) static bool timer_fixup_free(void *addr, enum debug_obj_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	struct timer_list *timer = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	case ODEBUG_STATE_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		del_timer_sync(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		debug_object_free(timer, &timer_debug_descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702)  * fixup_assert_init is called when:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703)  * - an untracked/uninit-ed object is found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) static bool timer_fixup_assert_init(void *addr, enum debug_obj_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	struct timer_list *timer = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	case ODEBUG_STATE_NOTAVAILABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		timer_setup(timer, stub_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) static const struct debug_obj_descr timer_debug_descr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	.name			= "timer_list",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	.debug_hint		= timer_debug_hint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	.is_static_object	= timer_is_static_object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	.fixup_init		= timer_fixup_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	.fixup_activate		= timer_fixup_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	.fixup_free		= timer_fixup_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	.fixup_assert_init	= timer_fixup_assert_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) static inline void debug_timer_init(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	debug_object_init(timer, &timer_debug_descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) static inline void debug_timer_activate(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	debug_object_activate(timer, &timer_debug_descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) static inline void debug_timer_deactivate(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	debug_object_deactivate(timer, &timer_debug_descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) static inline void debug_timer_assert_init(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	debug_object_assert_init(timer, &timer_debug_descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) static void do_init_timer(struct timer_list *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			  void (*func)(struct timer_list *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			  unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			  const char *name, struct lock_class_key *key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) void init_timer_on_stack_key(struct timer_list *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 			     void (*func)(struct timer_list *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			     unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 			     const char *name, struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	debug_object_init_on_stack(timer, &timer_debug_descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	do_init_timer(timer, func, flags, name, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) void destroy_timer_on_stack(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	debug_object_free(timer, &timer_debug_descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) EXPORT_SYMBOL_GPL(destroy_timer_on_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) static inline void debug_timer_init(struct timer_list *timer) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) static inline void debug_timer_activate(struct timer_list *timer) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) static inline void debug_timer_deactivate(struct timer_list *timer) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) static inline void debug_timer_assert_init(struct timer_list *timer) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) static inline void debug_init(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	debug_timer_init(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	trace_timer_init(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) static inline void debug_deactivate(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	debug_timer_deactivate(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	trace_timer_cancel(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) static inline void debug_assert_init(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	debug_timer_assert_init(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) static void do_init_timer(struct timer_list *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 			  void (*func)(struct timer_list *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			  unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			  const char *name, struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	timer->entry.pprev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	timer->function = func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (WARN_ON_ONCE(flags & ~TIMER_INIT_FLAGS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		flags &= TIMER_INIT_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	timer->flags = flags | raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	lockdep_init_map(&timer->lockdep_map, name, key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807)  * init_timer_key - initialize a timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808)  * @timer: the timer to be initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809)  * @func: timer callback function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810)  * @flags: timer flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811)  * @name: name of the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)  * @key: lockdep class key of the fake lock used for tracking timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813)  *       sync lock dependencies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815)  * init_timer_key() must be done to a timer prior calling *any* of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816)  * other timer functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) void init_timer_key(struct timer_list *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		    void (*func)(struct timer_list *), unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		    const char *name, struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	debug_init(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	do_init_timer(timer, func, flags, name, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) EXPORT_SYMBOL(init_timer_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) static inline void detach_timer(struct timer_list *timer, bool clear_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	struct hlist_node *entry = &timer->entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	debug_deactivate(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	__hlist_del(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	if (clear_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		entry->pprev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	entry->next = LIST_POISON2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) static int detach_if_pending(struct timer_list *timer, struct timer_base *base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			     bool clear_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	unsigned idx = timer_get_idx(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	if (!timer_pending(timer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	if (hlist_is_singular_node(&timer->entry, base->vectors + idx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		__clear_bit(idx, base->pending_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		base->next_expiry_recalc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	detach_timer(timer, clear_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_STD], cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	 * If the timer is deferrable and NO_HZ_COMMON is set then we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	 * to use the deferrable base.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	return base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	 * If the timer is deferrable and NO_HZ_COMMON is set then we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	 * to use the deferrable base.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		base = this_cpu_ptr(&timer_bases[BASE_DEF]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	return base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) static inline struct timer_base *get_timer_base(u32 tflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) static inline struct timer_base *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) get_target_base(struct timer_base *base, unsigned tflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (static_branch_likely(&timers_migration_enabled) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	    !(tflags & TIMER_PINNED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		return get_timer_cpu_base(tflags, get_nohz_timer_target());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	return get_timer_this_cpu_base(tflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) static inline void forward_timer_base(struct timer_base *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	unsigned long jnow = READ_ONCE(jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	 * No need to forward if we are close enough below jiffies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	 * Also while executing timers, base->clk is 1 offset ahead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	 * of jiffies to avoid endless requeuing to current jffies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if ((long)(jnow - base->clk) < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	 * If the next expiry value is > jiffies, then we fast forward to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	 * jiffies otherwise we forward to the next expiry value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	if (time_after(base->next_expiry, jnow)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		base->clk = jnow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (WARN_ON_ONCE(time_before(base->next_expiry, base->clk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		base->clk = base->next_expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925)  * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926)  * that all timers which are tied to this base are locked, and the base itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927)  * is locked too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929)  * So __run_timers/migrate_timers can safely modify all timers which could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930)  * be found in the base->vectors array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932)  * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933)  * to wait until the migration is done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) static struct timer_base *lock_timer_base(struct timer_list *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 					  unsigned long *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	__acquires(timer->base->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		struct timer_base *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		u32 tf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		 * We need to use READ_ONCE() here, otherwise the compiler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		 * might re-read @tf between the check for TIMER_MIGRATING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		 * and spin_lock().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		tf = READ_ONCE(timer->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		if (!(tf & TIMER_MIGRATING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			base = get_timer_base(tf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			raw_spin_lock_irqsave(&base->lock, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			if (timer->flags == tf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 				return base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			raw_spin_unlock_irqrestore(&base->lock, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) #define MOD_TIMER_PENDING_ONLY		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) #define MOD_TIMER_REDUCE		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) #define MOD_TIMER_NOTPENDING		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	unsigned long clk = 0, flags, bucket_expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	struct timer_base *base, *new_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	unsigned int idx = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	BUG_ON(!timer->function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	 * This is a common optimization triggered by the networking code - if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	 * the timer is re-modified to have the same timeout or ends up in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	 * same array bucket then just return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	if (!(options & MOD_TIMER_NOTPENDING) && timer_pending(timer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		 * The downside of this optimization is that it can result in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		 * larger granularity than you would get from adding a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		 * timer with this expiry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		long diff = timer->expires - expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		if (!diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		if (options & MOD_TIMER_REDUCE && diff <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		 * We lock timer base and calculate the bucket index right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		 * here. If the timer ends up in the same bucket, then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		 * just update the expiry time and avoid the whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		 * dequeue/enqueue dance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		base = lock_timer_base(timer, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		forward_timer_base(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		    time_before_eq(timer->expires, expires)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		clk = base->clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		idx = calc_wheel_index(expires, clk, &bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		 * Retrieve and compare the array index of the pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		 * timer. If it matches set the expiry to the new value so a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		 * subsequent call will exit in the expires check above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		if (idx == timer_get_idx(timer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 			if (!(options & MOD_TIMER_REDUCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 				timer->expires = expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			else if (time_after(timer->expires, expires))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 				timer->expires = expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		base = lock_timer_base(timer, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		forward_timer_base(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	ret = detach_if_pending(timer, base, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	if (!ret && (options & MOD_TIMER_PENDING_ONLY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	new_base = get_target_base(base, timer->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	if (base != new_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		 * We are trying to schedule the timer on the new base.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		 * However we can't change timer's base while it is running,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		 * otherwise del_timer_sync() can't detect that the timer's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		 * handler yet has not finished. This also guarantees that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		 * timer is serialized wrt itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		if (likely(base->running_timer != timer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			/* See the comment in lock_timer_base() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			timer->flags |= TIMER_MIGRATING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 			raw_spin_unlock(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			base = new_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			raw_spin_lock(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			WRITE_ONCE(timer->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 				   (timer->flags & ~TIMER_BASEMASK) | base->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			forward_timer_base(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	debug_timer_activate(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	timer->expires = expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	 * If 'idx' was calculated above and the base time did not advance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	 * between calculating 'idx' and possibly switching the base, only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	 * enqueue_timer() is required. Otherwise we need to (re)calculate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	 * the wheel index via internal_add_timer().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	if (idx != UINT_MAX && clk == base->clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		enqueue_timer(base, timer, idx, bucket_expiry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		internal_add_timer(base, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	raw_spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)  * mod_timer_pending - modify a pending timer's timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)  * @timer: the pending timer to be modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)  * @expires: new timeout in jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)  * mod_timer_pending() is the same for pending timers as mod_timer(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)  * but will not re-activate and modify already deleted timers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)  * It is useful for unserialized use of timers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) int mod_timer_pending(struct timer_list *timer, unsigned long expires)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) EXPORT_SYMBOL(mod_timer_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)  * mod_timer - modify a timer's timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)  * @timer: the timer to be modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)  * @expires: new timeout in jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)  * mod_timer() is a more efficient way to update the expire field of an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)  * active timer (if the timer is inactive it will be activated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)  * mod_timer(timer, expires) is equivalent to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)  *     del_timer(timer); timer->expires = expires; add_timer(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)  * Note that if there are multiple unserialized concurrent users of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)  * same timer, then mod_timer() is the only safe way to modify the timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)  * since add_timer() cannot modify an already running timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)  * The function returns whether it has modified a pending timer or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)  * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)  * active timer returns 1.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) int mod_timer(struct timer_list *timer, unsigned long expires)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	return __mod_timer(timer, expires, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) EXPORT_SYMBOL(mod_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)  * timer_reduce - Modify a timer's timeout if it would reduce the timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)  * @timer:	The timer to be modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)  * @expires:	New timeout in jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)  * timer_reduce() is very similar to mod_timer(), except that it will only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)  * modify a running timer if that would reduce the expiration time (it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)  * start a timer that isn't running).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) int timer_reduce(struct timer_list *timer, unsigned long expires)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	return __mod_timer(timer, expires, MOD_TIMER_REDUCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) EXPORT_SYMBOL(timer_reduce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)  * add_timer - start a timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)  * @timer: the timer to be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)  * The kernel will do a ->function(@timer) callback from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)  * timer interrupt at the ->expires point in the future. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)  * current time is 'jiffies'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)  * The timer's ->expires, ->function fields must be set prior calling this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)  * function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)  * Timers with an ->expires field in the past will be executed in the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)  * timer tick.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) void add_timer(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	BUG_ON(timer_pending(timer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	__mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) EXPORT_SYMBOL(add_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)  * add_timer_on - start a timer on a particular CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)  * @timer: the timer to be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)  * @cpu: the CPU to start it on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)  * This is not very scalable on SMP. Double adds are not possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) void add_timer_on(struct timer_list *timer, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	struct timer_base *new_base, *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	BUG_ON(timer_pending(timer) || !timer->function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	new_base = get_timer_cpu_base(timer->flags, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	 * If @timer was on a different CPU, it should be migrated with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	 * old base locked to prevent other operations proceeding with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	 * wrong base locked.  See lock_timer_base().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	base = lock_timer_base(timer, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	if (base != new_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		timer->flags |= TIMER_MIGRATING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		raw_spin_unlock(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		base = new_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		raw_spin_lock(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		WRITE_ONCE(timer->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			   (timer->flags & ~TIMER_BASEMASK) | cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	forward_timer_base(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	debug_timer_activate(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	internal_add_timer(base, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	raw_spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) EXPORT_SYMBOL_GPL(add_timer_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)  * del_timer - deactivate a timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)  * @timer: the timer to be deactivated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)  * del_timer() deactivates a timer - this works on both active and inactive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)  * timers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)  * The function returns whether it has deactivated a pending timer or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)  * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)  * active timer returns 1.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) int del_timer(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	struct timer_base *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	debug_assert_init(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	if (timer_pending(timer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		base = lock_timer_base(timer, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		ret = detach_if_pending(timer, base, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		raw_spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) EXPORT_SYMBOL(del_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)  * try_to_del_timer_sync - Try to deactivate a timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)  * @timer: timer to delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)  * This function tries to deactivate a timer. Upon successful (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)  * exit the timer is not queued and the handler is not running on any CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) int try_to_del_timer_sync(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	struct timer_base *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	debug_assert_init(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	base = lock_timer_base(timer, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	if (base->running_timer != timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		ret = detach_if_pending(timer, base, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	raw_spin_unlock_irqrestore(&base->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) EXPORT_SYMBOL(try_to_del_timer_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) #ifdef CONFIG_PREEMPT_RT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) static __init void timer_base_init_expiry_lock(struct timer_base *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	spin_lock_init(&base->expiry_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) static inline void timer_base_lock_expiry(struct timer_base *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	spin_lock(&base->expiry_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) static inline void timer_base_unlock_expiry(struct timer_base *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	spin_unlock(&base->expiry_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265)  * The counterpart to del_timer_wait_running().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)  * If there is a waiter for base->expiry_lock, then it was waiting for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)  * timer callback to finish. Drop expiry_lock and reaquire it. That allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)  * the waiter to acquire the lock and make progress.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) static void timer_sync_wait_running(struct timer_base *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	if (atomic_read(&base->timer_waiters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		raw_spin_unlock_irq(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		spin_unlock(&base->expiry_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		spin_lock(&base->expiry_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		raw_spin_lock_irq(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)  * This function is called on PREEMPT_RT kernels when the fast path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)  * deletion of a timer failed because the timer callback function was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)  * running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)  * This prevents priority inversion, if the softirq thread on a remote CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)  * got preempted, and it prevents a life lock when the task which tries to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288)  * delete a timer preempted the softirq thread running the timer callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)  * function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) static void del_timer_wait_running(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	u32 tf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	tf = READ_ONCE(timer->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	if (!(tf & TIMER_MIGRATING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		struct timer_base *base = get_timer_base(tf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		 * Mark the base as contended and grab the expiry lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		 * which is held by the softirq across the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		 * callback. Drop the lock immediately so the softirq can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		 * expire the next timer. In theory the timer could already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		 * be running again, but that's more than unlikely and just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		 * causes another wait loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		atomic_inc(&base->timer_waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		spin_lock_bh(&base->expiry_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		atomic_dec(&base->timer_waiters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		spin_unlock_bh(&base->expiry_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) static inline void timer_base_init_expiry_lock(struct timer_base *base) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) static inline void timer_base_lock_expiry(struct timer_base *base) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) static inline void timer_base_unlock_expiry(struct timer_base *base) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) static inline void timer_sync_wait_running(struct timer_base *base) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) static inline void del_timer_wait_running(struct timer_list *timer) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)  * del_timer_sync - deactivate a timer and wait for the handler to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)  * @timer: the timer to be deactivated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326)  * This function only differs from del_timer() on SMP: besides deactivating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)  * the timer it also makes sure the handler has finished executing on other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)  * CPUs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)  * Synchronization rules: Callers must prevent restarting of the timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)  * otherwise this function is meaningless. It must not be called from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)  * interrupt contexts unless the timer is an irqsafe one. The caller must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)  * not hold locks which would prevent completion of the timer's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)  * handler. The timer's handler must not call add_timer_on(). Upon exit the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)  * timer is not queued and the handler is not running on any CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)  * Note: For !irqsafe timers, you must not hold locks that are held in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)  *   interrupt context while calling this function. Even if the lock has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)  *   nothing to do with the timer in question.  Here's why::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)  *    CPU0                             CPU1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)  *    ----                             ----
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343)  *                                     <SOFTIRQ>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)  *                                       call_timer_fn();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)  *                                       base->running_timer = mytimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)  *    spin_lock_irq(somelock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)  *                                     <IRQ>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)  *                                        spin_lock(somelock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)  *    del_timer_sync(mytimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350)  *    while (base->running_timer == mytimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)  * Now del_timer_sync() will never return and never release somelock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)  * The interrupt on the other CPU is waiting to grab somelock but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)  * it has interrupted the softirq that CPU0 is waiting to finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356)  * The function returns whether it has deactivated a pending timer or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) int del_timer_sync(struct timer_list *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) #ifdef CONFIG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	 * If lockdep gives a backtrace here, please reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	 * the synchronization rules above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	lock_map_acquire(&timer->lockdep_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	lock_map_release(&timer->lockdep_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	 * don't use it in hardirq context, because it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	 * could lead to deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		ret = try_to_del_timer_sync(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		if (unlikely(ret < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 			del_timer_wait_running(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	} while (ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) EXPORT_SYMBOL(del_timer_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) static void call_timer_fn(struct timer_list *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			  void (*fn)(struct timer_list *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			  unsigned long baseclk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	int count = preempt_count();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) #ifdef CONFIG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	 * It is permissible to free the timer from inside the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	 * function that is called from it, this we need to take into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	 * account for lockdep too. To avoid bogus "held lock freed"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	 * warnings as well as problems when looking into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	 * timer->lockdep_map, make a copy and use that here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	struct lockdep_map lockdep_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	lockdep_copy_map(&lockdep_map, &timer->lockdep_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	 * Couple the lock chain with the lock chain at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	 * del_timer_sync() by acquiring the lock_map around the fn()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	 * call here and in del_timer_sync().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	lock_map_acquire(&lockdep_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	trace_timer_expire_entry(timer, baseclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	fn(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	trace_timer_expire_exit(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	lock_map_release(&lockdep_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	if (count != preempt_count()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		WARN_ONCE(1, "timer: %pS preempt leak: %08x -> %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			  fn, count, preempt_count());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		 * Restore the preempt count. That gives us a decent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		 * chance to survive and extract information. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		 * callback kept a lock held, bad luck, but not worse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		 * than the BUG() we had.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		preempt_count_set(count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) static void expire_timers(struct timer_base *base, struct hlist_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	 * This value is required only for tracing. base->clk was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	 * incremented directly before expire_timers was called. But expiry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	 * is related to the old base->clk value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	unsigned long baseclk = base->clk - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	while (!hlist_empty(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		struct timer_list *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		void (*fn)(struct timer_list *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		timer = hlist_entry(head->first, struct timer_list, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		base->running_timer = timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		detach_timer(timer, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		fn = timer->function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		if (timer->flags & TIMER_IRQSAFE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 			raw_spin_unlock(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 			call_timer_fn(timer, fn, baseclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 			raw_spin_lock(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 			base->running_timer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 			raw_spin_unlock_irq(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 			call_timer_fn(timer, fn, baseclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 			raw_spin_lock_irq(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 			base->running_timer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 			timer_sync_wait_running(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) static int collect_expired_timers(struct timer_base *base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 				  struct hlist_head *heads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	unsigned long clk = base->clk = base->next_expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	struct hlist_head *vec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	int i, levels = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	for (i = 0; i < LVL_DEPTH; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		idx = (clk & LVL_MASK) + i * LVL_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		if (__test_and_clear_bit(idx, base->pending_map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 			vec = base->vectors + idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 			hlist_move_list(vec, heads++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 			levels++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		/* Is it time to look at the next level? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		if (clk & LVL_CLK_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		/* Shift clock for the next level granularity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		clk >>= LVL_CLK_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	return levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)  * Find the next pending bucket of a level. Search from level start (@offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)  * + @clk upwards and if nothing there, search from start of the level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)  * (@offset) up to @offset + clk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) static int next_pending_bucket(struct timer_base *base, unsigned offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 			       unsigned clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	unsigned pos, start = offset + clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	unsigned end = offset + LVL_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	pos = find_next_bit(base->pending_map, end, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	if (pos < end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		return pos - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	pos = find_next_bit(base->pending_map, start, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	return pos < start ? pos + LVL_SIZE - start : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518)  * Search the first expiring timer in the various clock levels. Caller must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)  * hold base->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) static unsigned long __next_timer_interrupt(struct timer_base *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	unsigned long clk, next, adj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	unsigned lvl, offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	next = base->clk + NEXT_TIMER_MAX_DELTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	clk = base->clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	for (lvl = 0; lvl < LVL_DEPTH; lvl++, offset += LVL_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		int pos = next_pending_bucket(base, offset, clk & LVL_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		unsigned long lvl_clk = clk & LVL_CLK_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		if (pos >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			unsigned long tmp = clk + (unsigned long) pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 			tmp <<= LVL_SHIFT(lvl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			if (time_before(tmp, next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 				next = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			 * If the next expiration happens before we reach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 			 * the next level, no need to check further.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 			if (pos <= ((LVL_CLK_DIV - lvl_clk) & LVL_CLK_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		 * Clock for the next level. If the current level clock lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		 * bits are zero, we look at the next level as is. If not we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		 * need to advance it by one because that's going to be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		 * next expiring bucket in that level. base->clk is the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		 * expiring jiffie. So in case of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		 *  0    0    0    0    0    0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		 * we have to look at all levels @index 0. With
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		 *  0    0    0    0    0    2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		 * LVL0 has the next expiring bucket @index 2. The upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		 * levels have the next expiring bucket @index 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		 * In case that the propagation wraps the next level the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		 * rules apply:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		 * LVL5 LVL4 LVL3 LVL2 LVL1 LVL0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		 *  0    0    0    0    F    2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		 * So after looking at LVL0 we get:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 		 * LVL5 LVL4 LVL3 LVL2 LVL1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		 *  0    0    0    1    0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 		 * So no propagation from LVL1 to LVL2 because that happened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		 * with the add already, but then we need to propagate further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		 * from LVL2 to LVL3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		 * So the simple check whether the lower bits of the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		 * level are 0 or not is sufficient for all cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 		adj = lvl_clk ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		clk >>= LVL_CLK_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		clk += adj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	base->next_expiry_recalc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	base->timers_pending = !(next == base->clk + NEXT_TIMER_MAX_DELTA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	return next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) #ifdef CONFIG_NO_HZ_COMMON
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)  * Check, if the next hrtimer event is before the next timer wheel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)  * event:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) static u64 cmp_next_hrtimer_event(u64 basem, u64 expires)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	u64 nextevt = hrtimer_get_next_event();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	 * If high resolution timers are enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	 * hrtimer_get_next_event() returns KTIME_MAX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	if (expires <= nextevt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		return expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	 * If the next timer is already expired, return the tick base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	 * time so the tick is fired immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	if (nextevt <= basem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		return basem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	 * Round up to the next jiffie. High resolution timers are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	 * off, so the hrtimers are expired in the tick and we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	 * make sure that this tick really expires the timer to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	 * a ping pong of the nohz stop code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	 * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)  * get_next_timer_interrupt - return the time (clock mono) of the next timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)  * @basej:	base time jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)  * @basem:	base time clock monotonic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)  * Returns the tick aligned clock monotonic time of the next pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)  * timer or KTIME_MAX if no timer is pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	u64 expires = KTIME_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	unsigned long nextevt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	 * Pretend that there is no timer pending if the cpu is offline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	 * Possible pending timers will be migrated later to an active cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	if (cpu_is_offline(smp_processor_id()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		return expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	raw_spin_lock(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	if (base->next_expiry_recalc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		base->next_expiry = __next_timer_interrupt(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	nextevt = base->next_expiry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	 * We have a fresh next event. Check whether we can forward the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 	 * base. We can only do that when @basej is past base->clk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	 * otherwise we might rewind base->clk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	if (time_after(basej, base->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		if (time_after(nextevt, basej))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 			base->clk = basej;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		else if (time_after(nextevt, base->clk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			base->clk = nextevt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	if (time_before_eq(nextevt, basej)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		expires = basem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 		base->is_idle = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		if (base->timers_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 			expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		 * If we expect to sleep more than a tick, mark the base idle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		 * Also the tick is stopped so any added timer must forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		 * the base clk itself to keep granularity small. This idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		 * logic is only maintained for the BASE_STD base, deferrable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		 * timers may still see large granularity skew (by design).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		if ((expires - basem) > TICK_NSEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 			base->is_idle = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	raw_spin_unlock(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	return cmp_next_hrtimer_event(basem, expires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)  * timer_clear_idle - Clear the idle state of the timer base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689)  * Called with interrupts disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) void timer_clear_idle(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	 * We do this unlocked. The worst outcome is a remote enqueue sending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	 * a pointless IPI, but taking the lock would just make the window for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	 * sending the IPI a few instructions smaller for the cost of taking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	 * the lock in the exit from idle path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	base->is_idle = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)  * Called from the timer interrupt handler to charge one tick to the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)  * process.  user_tick is 1 if the tick is user time, 0 for system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) void update_process_times(int user_tick)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	struct task_struct *p = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	PRANDOM_ADD_NOISE(jiffies, user_tick, p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	/* Note: this timer irq context must be accounted for as well. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	account_process_tick(p, user_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	run_local_timers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	rcu_sched_clock_irq(user_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) #ifdef CONFIG_IRQ_WORK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	if (in_irq())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		irq_work_tick();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	scheduler_tick();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	if (IS_ENABLED(CONFIG_POSIX_TIMERS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		run_posix_cpu_timers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729)  * __run_timers - run all expired timers (if any) on this CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730)  * @base: the timer vector to be processed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) static inline void __run_timers(struct timer_base *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	struct hlist_head heads[LVL_DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	int levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	if (time_before(jiffies, base->next_expiry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	timer_base_lock_expiry(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	raw_spin_lock_irq(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	while (time_after_eq(jiffies, base->clk) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	       time_after_eq(jiffies, base->next_expiry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		levels = collect_expired_timers(base, heads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 		 * The only possible reason for not finding any expired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		 * timer at this clk is that all matching timers have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		 * dequeued.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 		WARN_ON_ONCE(!levels && !base->next_expiry_recalc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		base->clk++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		base->next_expiry = __next_timer_interrupt(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		while (levels--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 			expire_timers(base, heads + levels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	raw_spin_unlock_irq(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	timer_base_unlock_expiry(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763)  * This function runs timers and the timer-tq in bottom half context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) static __latent_entropy void run_timer_softirq(struct softirq_action *h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	__run_timers(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	if (IS_ENABLED(CONFIG_NO_HZ_COMMON))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		__run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775)  * Called by the local, per-CPU timer interrupt on SMP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) void run_local_timers(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	hrtimer_run_queues();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	/* Raise the softirq only if required. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	if (time_before(jiffies, base->next_expiry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		if (!IS_ENABLED(CONFIG_NO_HZ_COMMON))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		/* CPU is awake, so check the deferrable base. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		base++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		if (time_before(jiffies, base->next_expiry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	raise_softirq(TIMER_SOFTIRQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795)  * Since schedule_timeout()'s timer is defined on the stack, it must store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796)  * the target task on the stack as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) struct process_timer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	struct timer_list timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) static void process_timeout(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	struct process_timer *timeout = from_timer(timeout, t, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	wake_up_process(timeout->task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811)  * schedule_timeout - sleep until timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812)  * @timeout: timeout value in jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814)  * Make the current task sleep until @timeout jiffies have elapsed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)  * The function behavior depends on the current task state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816)  * (see also set_current_state() description):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818)  * %TASK_RUNNING - the scheduler is called, but the task does not sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819)  * at all. That happens because sched_submit_work() does nothing for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)  * tasks in %TASK_RUNNING state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822)  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823)  * pass before the routine returns unless the current task is explicitly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)  * woken up, (e.g. by wake_up_process()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826)  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)  * delivered to the current task or the current task is explicitly woken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828)  * up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830)  * The current task state is guaranteed to be %TASK_RUNNING when this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831)  * routine returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834)  * the CPU away without a bound on the timeout. In this case the return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835)  * value will be %MAX_SCHEDULE_TIMEOUT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837)  * Returns 0 when the timer has expired otherwise the remaining time in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838)  * jiffies will be returned. In all cases the return value is guaranteed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)  * to be non-negative.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) signed long __sched schedule_timeout(signed long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	struct process_timer timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	unsigned long expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	switch (timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	case MAX_SCHEDULE_TIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		 * These two special cases are useful to be comfortable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 		 * in the caller. Nothing more. We could take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 		 * MAX_SCHEDULE_TIMEOUT from one of the negative value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		 * but I' d like to return a valid offset (>=0) to allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		 * the caller to do everything it want with the retval.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		 * Another bit of PARANOID. Note that the retval will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 		 * 0 since no piece of kernel is supposed to do a check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		 * for a negative retval of schedule_timeout() (since it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		 * should never happens anyway). You just have the printk()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 		 * that will tell you if something is gone wrong and where.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		if (timeout < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 			printk(KERN_ERR "schedule_timeout: wrong timeout "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 				"value %lx\n", timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 			dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 			current->state = TASK_RUNNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	expire = timeout + jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	timer.task = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	timer_setup_on_stack(&timer.timer, process_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	__mod_timer(&timer.timer, expire, MOD_TIMER_NOTPENDING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	del_singleshot_timer_sync(&timer.timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	/* Remove the timer from the object tracker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	destroy_timer_on_stack(&timer.timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	timeout = expire - jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	return timeout < 0 ? 0 : timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) EXPORT_SYMBOL(schedule_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)  * We can use __set_current_state() here because schedule_timeout() calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)  * schedule() unconditionally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) signed long __sched schedule_timeout_interruptible(signed long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	__set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	return schedule_timeout(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) EXPORT_SYMBOL(schedule_timeout_interruptible);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) signed long __sched schedule_timeout_killable(signed long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 	__set_current_state(TASK_KILLABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	return schedule_timeout(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) EXPORT_SYMBOL(schedule_timeout_killable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) signed long __sched schedule_timeout_uninterruptible(signed long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	__set_current_state(TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	return schedule_timeout(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) EXPORT_SYMBOL(schedule_timeout_uninterruptible);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919)  * Like schedule_timeout_uninterruptible(), except this task will not contribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)  * to load average.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) signed long __sched schedule_timeout_idle(signed long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	__set_current_state(TASK_IDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	return schedule_timeout(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) EXPORT_SYMBOL(schedule_timeout_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) #ifdef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) static void migrate_timer_list(struct timer_base *new_base, struct hlist_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	struct timer_list *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	int cpu = new_base->cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	while (!hlist_empty(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 		timer = hlist_entry(head->first, struct timer_list, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		detach_timer(timer, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 		internal_add_timer(new_base, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) int timers_prepare_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	struct timer_base *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	int b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	for (b = 0; b < NR_BASES; b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		base = per_cpu_ptr(&timer_bases[b], cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		base->clk = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		base->timers_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		base->is_idle = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) int timers_dead_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	struct timer_base *old_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	struct timer_base *new_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	int b, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	BUG_ON(cpu_online(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	for (b = 0; b < NR_BASES; b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 		old_base = per_cpu_ptr(&timer_bases[b], cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 		new_base = get_cpu_ptr(&timer_bases[b]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		 * The caller is globally serialized and nobody else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 		 * takes two locks at once, deadlock is not possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 		raw_spin_lock_irq(&new_base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 		raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		 * The current CPUs base clock might be stale. Update it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		 * before moving the timers over.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 		forward_timer_base(new_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		BUG_ON(old_base->running_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		for (i = 0; i < WHEEL_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 			migrate_timer_list(new_base, old_base->vectors + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		raw_spin_unlock(&old_base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		raw_spin_unlock_irq(&new_base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 		put_cpu_ptr(&timer_bases);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) #endif /* CONFIG_HOTPLUG_CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) static void __init init_timer_cpu(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	struct timer_base *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	for (i = 0; i < NR_BASES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		base = per_cpu_ptr(&timer_bases[i], cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		base->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		raw_spin_lock_init(&base->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		base->clk = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		timer_base_init_expiry_lock(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) static void __init init_timer_cpus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	for_each_possible_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		init_timer_cpu(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) void __init init_timers(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	init_timer_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	posix_cputimers_init_work();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027)  * msleep - sleep safely even with waitqueue interruptions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028)  * @msecs: Time in milliseconds to sleep for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) void msleep(unsigned int msecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	while (timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		timeout = schedule_timeout_uninterruptible(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) EXPORT_SYMBOL(msleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041)  * msleep_interruptible - sleep waiting for signals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042)  * @msecs: Time in milliseconds to sleep for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) unsigned long msleep_interruptible(unsigned int msecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	unsigned long timeout = msecs_to_jiffies(msecs) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	while (timeout && !signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 		timeout = schedule_timeout_interruptible(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	return jiffies_to_msecs(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) EXPORT_SYMBOL(msleep_interruptible);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056)  * usleep_range_state - Sleep for an approximate time in a given state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057)  * @min:	Minimum time in usecs to sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058)  * @max:	Maximum time in usecs to sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059)  * @state:	State of the current task that will be while sleeping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061)  * In non-atomic context where the exact wakeup time is flexible, use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)  * usleep_range_state() instead of udelay().  The sleep improves responsiveness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063)  * by avoiding the CPU-hogging busy-wait of udelay(), and the range reduces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064)  * power usage by allowing hrtimers to take advantage of an already-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065)  * scheduled interrupt instead of scheduling a new one just for this sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) void __sched usleep_range_state(unsigned long min, unsigned long max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 				unsigned int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	ktime_t exp = ktime_add_us(ktime_get(), min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	u64 delta = (u64)(max - min) * NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 		__set_current_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		/* Do not return before the requested sleep time has elapsed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 		if (!schedule_hrtimeout_range(&exp, delta, HRTIMER_MODE_ABS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082)  * usleep_range - Sleep for an approximate time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083)  * @min: Minimum time in usecs to sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084)  * @max: Maximum time in usecs to sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)  * In non-atomic context where the exact wakeup time is flexible, use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087)  * usleep_range() instead of udelay().  The sleep improves responsiveness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)  * by avoiding the CPU-hogging busy-wait of udelay(), and the range reduces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)  * power usage by allowing hrtimers to take advantage of an already-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090)  * scheduled interrupt instead of scheduling a new one just for this sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) void __sched usleep_range(unsigned long min, unsigned long max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	usleep_range_state(min, max, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) EXPORT_SYMBOL(usleep_range);