Lines Matching +full:timer +full:- +full:cannot +full:- +full:wake +full:- +full:cpu

1 // SPDX-License-Identifier: GPL-2.0
7 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
9 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
14 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
15 * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
16 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
33 #include <linux/posix-timers.h>
34 #include <linux/cpu.h>
55 #include "tick-internal.h"
59 #include <trace/events/timer.h>
66 * The timer wheel has LVL_DEPTH array levels. Each level provides an array of
73 * The array level of a newly armed timer depends on the relative expiry
77 * Contrary to the original timer wheel implementation, which aims for 'exact'
79 * the timers into the lower array levels. The previous 'classic' timer wheel
84 * This is an optimization of the original timer wheel implementation for the
85 * majority of the timer wheel use cases: timeouts. The vast majority of
107 * 0 0 1 ms 0 ms - 63 ms
108 * 1 64 8 ms 64 ms - 511 ms
109 * 2 128 64 ms 512 ms - 4095 ms (512ms - ~4s)
110 * 3 192 512 ms 4096 ms - 32767 ms (~4s - ~32s)
111 * 4 256 4096 ms (~4s) 32768 ms - 262143 ms (~32s - ~4m)
112 * 5 320 32768 ms (~32s) 262144 ms - 2097151 ms (~4m - ~34m)
113 * 6 384 262144 ms (~4m) 2097152 ms - 16777215 ms (~34m - ~4h)
114 * 7 448 2097152 ms (~34m) 16777216 ms - 134217727 ms (~4h - ~1d)
115 * 8 512 16777216 ms (~4h) 134217728 ms - 1073741822 ms (~1d - ~12d)
119 * 0 0 3 ms 0 ms - 210 ms
120 * 1 64 26 ms 213 ms - 1703 ms (213ms - ~1s)
121 * 2 128 213 ms 1706 ms - 13650 ms (~1s - ~13s)
122 * 3 192 1706 ms (~1s) 13653 ms - 109223 ms (~13s - ~1m)
123 * 4 256 13653 ms (~13s) 109226 ms - 873810 ms (~1m - ~14m)
124 * 5 320 109226 ms (~1m) 873813 ms - 6990503 ms (~14m - ~1h)
125 * 6 384 873813 ms (~14m) 6990506 ms - 55924050 ms (~1h - ~15h)
126 * 7 448 6990506 ms (~1h) 55924053 ms - 447392423 ms (~15h - ~5d)
127 * 8 512 55924053 ms (~15h) 447392426 ms - 3579139406 ms (~5d - ~41d)
131 * 0 0 4 ms 0 ms - 255 ms
132 * 1 64 32 ms 256 ms - 2047 ms (256ms - ~2s)
133 * 2 128 256 ms 2048 ms - 16383 ms (~2s - ~16s)
134 * 3 192 2048 ms (~2s) 16384 ms - 131071 ms (~16s - ~2m)
135 * 4 256 16384 ms (~16s) 131072 ms - 1048575 ms (~2m - ~17m)
136 * 5 320 131072 ms (~2m) 1048576 ms - 8388607 ms (~17m - ~2h)
137 * 6 384 1048576 ms (~17m) 8388608 ms - 67108863 ms (~2h - ~18h)
138 * 7 448 8388608 ms (~2h) 67108864 ms - 536870911 ms (~18h - ~6d)
139 * 8 512 67108864 ms (~18h) 536870912 ms - 4294967288 ms (~6d - ~49d)
143 * 0 0 10 ms 0 ms - 630 ms
144 * 1 64 80 ms 640 ms - 5110 ms (640ms - ~5s)
145 * 2 128 640 ms 5120 ms - 40950 ms (~5s - ~40s)
146 * 3 192 5120 ms (~5s) 40960 ms - 327670 ms (~40s - ~5m)
147 * 4 256 40960 ms (~40s) 327680 ms - 2621430 ms (~5m - ~43m)
148 * 5 320 327680 ms (~5m) 2621440 ms - 20971510 ms (~43m - ~5h)
149 * 6 384 2621440 ms (~43m) 20971520 ms - 167772150 ms (~5h - ~1d)
150 * 7 448 20971520 ms (~5h) 167772160 ms - 1342177270 ms (~1d - ~15d)
156 #define LVL_CLK_MASK (LVL_CLK_DIV - 1)
165 #define LVL_START(n) ((LVL_SIZE - 1) << (((n) - 1) * LVL_CLK_SHIFT))
170 #define LVL_MASK (LVL_SIZE - 1)
182 #define WHEEL_TIMEOUT_MAX (WHEEL_TIMEOUT_CUTOFF - LVL_GRAN(LVL_DEPTH - 1))
207 * struct timer_base - Per CPU timer base (number of base depends on config)
211 * currently running timer, the pointer is set to the
212 * timer, which expires at the moment. If no timer is
215 * timer expiry callback execution and when trying to
216 * delete a running timer and it wasn't successful in
218 * when callback was preempted on a remote CPU and a
219 * caller tries to delete the running timer. It also
221 * delete a timer preempted the softirq thread which
222 * is running the timer callback function.
224 * waiting for the end of the timer callback function
226 * @clk: clock of the timer base; is updated before enqueue
227 * of a timer; during expiry, it is 1 offset ahead of
230 * @next_expiry: expiry value of the first timer; it is updated when
231 * finding the next timer and during enqueue; the
233 * @cpu: Number of CPU the timer base belongs to
235 * required. Value is set true, when a timer was
240 * never wake up an idle CPU. So no matter of supporting it
242 * @timers_pending: Is set, when a timer is pending in the base. It is only
244 * @pending_map: bitmap of the timer wheel; each bit reflects a
246 * single timer is enqueued in the related bucket.
248 * of the timer wheel. The list contains all timers
260 unsigned int cpu; member
349 static unsigned long round_jiffies_common(unsigned long j, int cpu, in round_jiffies_common() argument
357 * same lock or cachelines, so we skew each extra cpu with an extra in round_jiffies_common()
363 j += cpu * 3; in round_jiffies_common()
369 * due to delays of the timer irq, long irq off times etc etc) then in round_jiffies_common()
375 j = j - rem; in round_jiffies_common()
377 j = j - rem + HZ; in round_jiffies_common()
380 j -= cpu * 3; in round_jiffies_common()
390 * __round_jiffies - function to round jiffies to a full second
392 * @cpu: the processor number on which the timeout will happen
401 * of this is to have the CPU wake up less, which saves power.
409 unsigned long __round_jiffies(unsigned long j, int cpu) in __round_jiffies() argument
411 return round_jiffies_common(j, cpu, false); in __round_jiffies()
416 * __round_jiffies_relative - function to round jiffies to a full second
418 * @cpu: the processor number on which the timeout will happen
427 * of this is to have the CPU wake up less, which saves power.
435 unsigned long __round_jiffies_relative(unsigned long j, int cpu) in __round_jiffies_relative() argument
440 return round_jiffies_common(j + j0, cpu, false) - j0; in __round_jiffies_relative()
445 * round_jiffies - function to round jiffies to a full second
455 * of this is to have the CPU wake up less, which saves power.
466 * round_jiffies_relative - function to round jiffies to a full second
476 * of this is to have the CPU wake up less, which saves power.
487 * __round_jiffies_up - function to round jiffies up to a full second
489 * @cpu: the processor number on which the timeout will happen
496 unsigned long __round_jiffies_up(unsigned long j, int cpu) in __round_jiffies_up() argument
498 return round_jiffies_common(j, cpu, true); in __round_jiffies_up()
503 * __round_jiffies_up_relative - function to round jiffies up to a full second
505 * @cpu: the processor number on which the timeout will happen
512 unsigned long __round_jiffies_up_relative(unsigned long j, int cpu) in __round_jiffies_up_relative() argument
517 return round_jiffies_common(j + j0, cpu, true) - j0; in __round_jiffies_up_relative()
522 * round_jiffies_up - function to round jiffies up to a full second
537 * round_jiffies_up_relative - function to round jiffies up to a full second
552 static inline unsigned int timer_get_idx(struct timer_list *timer) in timer_get_idx() argument
554 return (timer->flags & TIMER_ARRAYMASK) >> TIMER_ARRAYSHIFT; in timer_get_idx()
557 static inline void timer_set_idx(struct timer_list *timer, unsigned int idx) in timer_set_idx() argument
559 timer->flags = (timer->flags & ~TIMER_ARRAYMASK) | in timer_set_idx()
572 * The timer wheel has to guarantee that a timer does not fire in calc_index()
574 * - Timer is armed at the edge of a tick in calc_index()
575 * - Truncation of the expiry time in the outer wheel levels in calc_index()
587 unsigned long delta = expires - clk; in calc_wheel_index()
617 idx = calc_index(expires, LVL_DEPTH - 1, bucket_expiry); in calc_wheel_index()
623 trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer) in trigger_dyntick_cpu() argument
626 * Deferrable timers do not prevent the CPU from entering dynticks and in trigger_dyntick_cpu()
628 * new deferrable timer is enqueued will wake up the remote CPU but in trigger_dyntick_cpu()
629 * nothing will be done with the deferrable timer base. Therefore skip in trigger_dyntick_cpu()
632 if (!is_timers_nohz_active() || timer->flags & TIMER_DEFERRABLE) in trigger_dyntick_cpu()
636 * We might have to IPI the remote CPU if the base is idle and the in trigger_dyntick_cpu()
637 * timer is pinned. If it is a non pinned timer, it is only queued in trigger_dyntick_cpu()
638 * on the remote CPU, when timer was running during queueing. Then in trigger_dyntick_cpu()
639 * everything is handled by remote CPU anyway. If the other CPU is in trigger_dyntick_cpu()
640 * on the way to idle then it can't set base->is_idle as we hold in trigger_dyntick_cpu()
643 if (base->is_idle) { in trigger_dyntick_cpu()
644 WARN_ON_ONCE(!(timer->flags & TIMER_PINNED || in trigger_dyntick_cpu()
645 tick_nohz_full_cpu(base->cpu))); in trigger_dyntick_cpu()
646 wake_up_nohz_cpu(base->cpu); in trigger_dyntick_cpu()
651 * Enqueue the timer into the hash bucket, mark it pending in
652 * the bitmap, store the index in the timer flags then wake up
653 * the target CPU if needed.
655 static void enqueue_timer(struct timer_base *base, struct timer_list *timer, in enqueue_timer() argument
659 hlist_add_head(&timer->entry, base->vectors + idx); in enqueue_timer()
660 __set_bit(idx, base->pending_map); in enqueue_timer()
661 timer_set_idx(timer, idx); in enqueue_timer()
663 trace_timer_start(timer, bucket_expiry); in enqueue_timer()
666 * Check whether this is the new first expiring timer. The in enqueue_timer()
667 * effective expiry time of the timer is required here in enqueue_timer()
668 * (bucket_expiry) instead of timer->expires. in enqueue_timer()
670 if (time_before(bucket_expiry, base->next_expiry)) { in enqueue_timer()
672 * Set the next expiry time and kick the CPU so it in enqueue_timer()
675 WRITE_ONCE(base->next_expiry, bucket_expiry); in enqueue_timer()
676 base->timers_pending = true; in enqueue_timer()
677 base->next_expiry_recalc = false; in enqueue_timer()
678 trigger_dyntick_cpu(base, timer); in enqueue_timer()
682 static void internal_add_timer(struct timer_base *base, struct timer_list *timer) in internal_add_timer() argument
687 idx = calc_wheel_index(timer->expires, base->clk, &bucket_expiry); in internal_add_timer()
688 enqueue_timer(base, timer, idx, bucket_expiry); in internal_add_timer()
703 .offset = offsetof(container, hintfn) - \
709 struct delayed_work, timer, work.func),
711 struct kthread_delayed_work, timer, work.func),
716 struct timer_list *timer = addr; in timer_debug_hint() local
720 if (timer_hints[i].function == timer->function) { in timer_debug_hint()
727 return timer->function; in timer_debug_hint()
732 struct timer_list *timer = addr; in timer_is_static_object() local
734 return (timer->entry.pprev == NULL && in timer_is_static_object()
735 timer->entry.next == TIMER_ENTRY_STATIC); in timer_is_static_object()
740 * - an active object is initialized
744 struct timer_list *timer = addr; in timer_fixup_init() local
748 del_timer_sync(timer); in timer_fixup_init()
749 debug_object_init(timer, &timer_debug_descr); in timer_fixup_init()
756 /* Stub timer callback for improperly used timers. */
764 * - an active object is activated
765 * - an unknown non-static object is activated
769 struct timer_list *timer = addr; in timer_fixup_activate() local
773 timer_setup(timer, stub_timer, 0); in timer_fixup_activate()
786 * - an active object is freed
790 struct timer_list *timer = addr; in timer_fixup_free() local
794 del_timer_sync(timer); in timer_fixup_free()
795 debug_object_free(timer, &timer_debug_descr); in timer_fixup_free()
804 * - an untracked/uninit-ed object is found
808 struct timer_list *timer = addr; in timer_fixup_assert_init() local
812 timer_setup(timer, stub_timer, 0); in timer_fixup_assert_init()
829 static inline void debug_timer_init(struct timer_list *timer) in debug_timer_init() argument
831 debug_object_init(timer, &timer_debug_descr); in debug_timer_init()
834 static inline void debug_timer_activate(struct timer_list *timer) in debug_timer_activate() argument
836 debug_object_activate(timer, &timer_debug_descr); in debug_timer_activate()
839 static inline void debug_timer_deactivate(struct timer_list *timer) in debug_timer_deactivate() argument
841 debug_object_deactivate(timer, &timer_debug_descr); in debug_timer_deactivate()
844 static inline void debug_timer_assert_init(struct timer_list *timer) in debug_timer_assert_init() argument
846 debug_object_assert_init(timer, &timer_debug_descr); in debug_timer_assert_init()
849 static void do_init_timer(struct timer_list *timer,
854 void init_timer_on_stack_key(struct timer_list *timer, in init_timer_on_stack_key() argument
859 debug_object_init_on_stack(timer, &timer_debug_descr); in init_timer_on_stack_key()
860 do_init_timer(timer, func, flags, name, key); in init_timer_on_stack_key()
864 void destroy_timer_on_stack(struct timer_list *timer) in destroy_timer_on_stack() argument
866 debug_object_free(timer, &timer_debug_descr); in destroy_timer_on_stack()
871 static inline void debug_timer_init(struct timer_list *timer) { } in debug_timer_init() argument
872 static inline void debug_timer_activate(struct timer_list *timer) { } in debug_timer_activate() argument
873 static inline void debug_timer_deactivate(struct timer_list *timer) { } in debug_timer_deactivate() argument
874 static inline void debug_timer_assert_init(struct timer_list *timer) { } in debug_timer_assert_init() argument
877 static inline void debug_init(struct timer_list *timer) in debug_init() argument
879 debug_timer_init(timer); in debug_init()
880 trace_timer_init(timer); in debug_init()
883 static inline void debug_deactivate(struct timer_list *timer) in debug_deactivate() argument
885 debug_timer_deactivate(timer); in debug_deactivate()
886 trace_timer_cancel(timer); in debug_deactivate()
889 static inline void debug_assert_init(struct timer_list *timer) in debug_assert_init() argument
891 debug_timer_assert_init(timer); in debug_assert_init()
894 static void do_init_timer(struct timer_list *timer, in do_init_timer() argument
899 timer->entry.pprev = NULL; in do_init_timer()
900 timer->function = func; in do_init_timer()
903 timer->flags = flags | raw_smp_processor_id(); in do_init_timer()
904 lockdep_init_map(&timer->lockdep_map, name, key, 0); in do_init_timer()
908 * init_timer_key - initialize a timer
909 * @timer: the timer to be initialized
910 * @func: timer callback function
911 * @flags: timer flags
912 * @name: name of the timer
913 * @key: lockdep class key of the fake lock used for tracking timer
916 * init_timer_key() must be done to a timer prior to calling *any* of the
917 * other timer functions.
919 void init_timer_key(struct timer_list *timer, in init_timer_key() argument
923 debug_init(timer); in init_timer_key()
924 do_init_timer(timer, func, flags, name, key); in init_timer_key()
928 static inline void detach_timer(struct timer_list *timer, bool clear_pending) in detach_timer() argument
930 struct hlist_node *entry = &timer->entry; in detach_timer()
932 debug_deactivate(timer); in detach_timer()
936 entry->pprev = NULL; in detach_timer()
937 entry->next = LIST_POISON2; in detach_timer()
940 static int detach_if_pending(struct timer_list *timer, struct timer_base *base, in detach_if_pending() argument
943 unsigned idx = timer_get_idx(timer); in detach_if_pending()
945 if (!timer_pending(timer)) in detach_if_pending()
948 if (hlist_is_singular_node(&timer->entry, base->vectors + idx)) { in detach_if_pending()
949 __clear_bit(idx, base->pending_map); in detach_if_pending()
950 base->next_expiry_recalc = true; in detach_if_pending()
953 detach_timer(timer, clear_pending); in detach_if_pending()
957 static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu) in get_timer_cpu_base() argument
962 base = per_cpu_ptr(&timer_bases[index], cpu); in get_timer_cpu_base()
965 * If the timer is deferrable and NO_HZ_COMMON is set then we need in get_timer_cpu_base()
969 base = per_cpu_ptr(&timer_bases[BASE_DEF], cpu); in get_timer_cpu_base()
981 * If the timer is deferrable and NO_HZ_COMMON is set then we need in get_timer_this_cpu_base()
999 * @basej is past base->clk otherwise we might rewind base->clk. in __forward_timer_base()
1001 if (time_before_eq(basej, base->clk)) in __forward_timer_base()
1008 if (time_after(base->next_expiry, basej)) { in __forward_timer_base()
1009 base->clk = basej; in __forward_timer_base()
1011 if (WARN_ON_ONCE(time_before(base->next_expiry, base->clk))) in __forward_timer_base()
1013 base->clk = base->next_expiry; in __forward_timer_base()
1029 * be found in the base->vectors array.
1031 * When a timer is migrating then the TIMER_MIGRATING flag is set and we need
1034 static struct timer_base *lock_timer_base(struct timer_list *timer, in lock_timer_base() argument
1036 __acquires(timer->base->lock) in lock_timer_base()
1044 * might re-read @tf between the check for TIMER_MIGRATING in lock_timer_base()
1047 tf = READ_ONCE(timer->flags); in lock_timer_base()
1051 raw_spin_lock_irqsave(&base->lock, *flags); in lock_timer_base()
1052 if (timer->flags == tf) in lock_timer_base()
1054 raw_spin_unlock_irqrestore(&base->lock, *flags); in lock_timer_base()
1065 __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options) in __mod_timer() argument
1072 debug_assert_init(timer); in __mod_timer()
1075 * This is a common optimization triggered by the networking code - if in __mod_timer()
1076 * the timer is re-modified to have the same timeout or ends up in the in __mod_timer()
1079 if (!(options & MOD_TIMER_NOTPENDING) && timer_pending(timer)) { in __mod_timer()
1083 * timer with this expiry. in __mod_timer()
1085 long diff = timer->expires - expires; in __mod_timer()
1093 * We lock timer base and calculate the bucket index right in __mod_timer()
1094 * here. If the timer ends up in the same bucket, then we in __mod_timer()
1098 base = lock_timer_base(timer, &flags); in __mod_timer()
1100 * Has @timer been shutdown? This needs to be evaluated in __mod_timer()
1104 if (!timer->function) in __mod_timer()
1109 if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) && in __mod_timer()
1110 time_before_eq(timer->expires, expires)) { in __mod_timer()
1115 clk = base->clk; in __mod_timer()
1120 * timer. If it matches set the expiry to the new value so a in __mod_timer()
1123 if (idx == timer_get_idx(timer)) { in __mod_timer()
1125 timer->expires = expires; in __mod_timer()
1126 else if (time_after(timer->expires, expires)) in __mod_timer()
1127 timer->expires = expires; in __mod_timer()
1132 base = lock_timer_base(timer, &flags); in __mod_timer()
1134 * Has @timer been shutdown? This needs to be evaluated in __mod_timer()
1138 if (!timer->function) in __mod_timer()
1144 ret = detach_if_pending(timer, base, false); in __mod_timer()
1148 new_base = get_timer_this_cpu_base(timer->flags); in __mod_timer()
1152 * We are trying to schedule the timer on the new base. in __mod_timer()
1153 * However we can't change timer's base while it is running, in __mod_timer()
1154 * otherwise timer_delete_sync() can't detect that the timer's in __mod_timer()
1156 * timer is serialized wrt itself. in __mod_timer()
1158 if (likely(base->running_timer != timer)) { in __mod_timer()
1160 timer->flags |= TIMER_MIGRATING; in __mod_timer()
1162 raw_spin_unlock(&base->lock); in __mod_timer()
1164 raw_spin_lock(&base->lock); in __mod_timer()
1165 WRITE_ONCE(timer->flags, in __mod_timer()
1166 (timer->flags & ~TIMER_BASEMASK) | base->cpu); in __mod_timer()
1171 debug_timer_activate(timer); in __mod_timer()
1173 timer->expires = expires; in __mod_timer()
1180 if (idx != UINT_MAX && clk == base->clk) in __mod_timer()
1181 enqueue_timer(base, timer, idx, bucket_expiry); in __mod_timer()
1183 internal_add_timer(base, timer); in __mod_timer()
1186 raw_spin_unlock_irqrestore(&base->lock, flags); in __mod_timer()
1192 * mod_timer_pending - Modify a pending timer's timeout
1193 * @timer: The pending timer to be modified
1199 * If @timer->function == NULL then the start operation is silently
1203 * * %0 - The timer was inactive and not modified or was in
1205 * * %1 - The timer was active and requeued to expire at @expires
1207 int mod_timer_pending(struct timer_list *timer, unsigned long expires) in mod_timer_pending() argument
1209 return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY); in mod_timer_pending()
1214 * mod_timer - Modify a timer's timeout
1215 * @timer: The timer to be modified
1218 * mod_timer(timer, expires) is equivalent to:
1220 * del_timer(timer); timer->expires = expires; add_timer(timer);
1223 * case that the timer is inactive, the del_timer() part is a NOP. The
1224 * timer is in any case activated with the new expiry time @expires.
1227 * same timer, then mod_timer() is the only safe way to modify the timeout,
1228 * since add_timer() cannot modify an already running timer.
1230 * If @timer->function == NULL then the start operation is silently
1234 * * %0 - The timer was inactive and started or was in shutdown
1236 * * %1 - The timer was active and requeued to expire at @expires or
1237 * the timer was active and not modified because @expires did
1240 int mod_timer(struct timer_list *timer, unsigned long expires) in mod_timer() argument
1242 return __mod_timer(timer, expires, 0); in mod_timer()
1247 * timer_reduce - Modify a timer's timeout if it would reduce the timeout
1248 * @timer: The timer to be modified
1252 * modify an enqueued timer if that would reduce the expiration time. If
1253 * @timer is not enqueued it starts the timer.
1255 * If @timer->function == NULL then the start operation is silently
1259 * * %0 - The timer was inactive and started or was in shutdown
1261 * * %1 - The timer was active and requeued to expire at @expires or
1262 * the timer was active and not modified because @expires
1264 * timer would expire earlier than already scheduled
1266 int timer_reduce(struct timer_list *timer, unsigned long expires) in timer_reduce() argument
1268 return __mod_timer(timer, expires, MOD_TIMER_REDUCE); in timer_reduce()
1273 * add_timer - Start a timer
1274 * @timer: The timer to be started
1276 * Start @timer to expire at @timer->expires in the future. @timer->expires
1277 * is the absolute expiry time measured in 'jiffies'. When the timer expires
1278 * timer->function(timer) will be invoked from soft interrupt context.
1280 * The @timer->expires and @timer->function fields must be set prior
1283 * If @timer->function == NULL then the start operation is silently
1286 * If @timer->expires is already in the past @timer will be queued to
1287 * expire at the next timer tick.
1289 * This can only operate on an inactive timer. Attempts to invoke this on
1290 * an active timer are rejected with a warning.
1292 void add_timer(struct timer_list *timer) in add_timer() argument
1294 if (WARN_ON_ONCE(timer_pending(timer))) in add_timer()
1296 __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING); in add_timer()
1301 * add_timer_local() - Start a timer on the local CPU
1302 * @timer: The timer to be started
1304 * Same as add_timer() except that the timer flag TIMER_PINNED is set.
1308 void add_timer_local(struct timer_list *timer) in add_timer_local() argument
1310 if (WARN_ON_ONCE(timer_pending(timer))) in add_timer_local()
1312 timer->flags |= TIMER_PINNED; in add_timer_local()
1313 __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING); in add_timer_local()
1318 * add_timer_global() - Start a timer without TIMER_PINNED flag set
1319 * @timer: The timer to be started
1321 * Same as add_timer() except that the timer flag TIMER_PINNED is unset.
1325 void add_timer_global(struct timer_list *timer) in add_timer_global() argument
1327 if (WARN_ON_ONCE(timer_pending(timer))) in add_timer_global()
1329 timer->flags &= ~TIMER_PINNED; in add_timer_global()
1330 __mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING); in add_timer_global()
1335 * add_timer_on - Start a timer on a particular CPU
1336 * @timer: The timer to be started
1337 * @cpu: The CPU to start it on
1339 * Same as add_timer() except that it starts the timer on the given CPU and
1340 * the TIMER_PINNED flag is set. When timer shouldn't be a pinned timer in
1346 void add_timer_on(struct timer_list *timer, int cpu) in add_timer_on() argument
1351 debug_assert_init(timer); in add_timer_on()
1353 if (WARN_ON_ONCE(timer_pending(timer))) in add_timer_on()
1356 /* Make sure timer flags have TIMER_PINNED flag set */ in add_timer_on()
1357 timer->flags |= TIMER_PINNED; in add_timer_on()
1359 new_base = get_timer_cpu_base(timer->flags, cpu); in add_timer_on()
1362 * If @timer was on a different CPU, it should be migrated with the in add_timer_on()
1366 base = lock_timer_base(timer, &flags); in add_timer_on()
1368 * Has @timer been shutdown? This needs to be evaluated while in add_timer_on()
1371 if (!timer->function) in add_timer_on()
1375 timer->flags |= TIMER_MIGRATING; in add_timer_on()
1377 raw_spin_unlock(&base->lock); in add_timer_on()
1379 raw_spin_lock(&base->lock); in add_timer_on()
1380 WRITE_ONCE(timer->flags, in add_timer_on()
1381 (timer->flags & ~TIMER_BASEMASK) | cpu); in add_timer_on()
1385 debug_timer_activate(timer); in add_timer_on()
1386 internal_add_timer(base, timer); in add_timer_on()
1388 raw_spin_unlock_irqrestore(&base->lock, flags); in add_timer_on()
1393 * __timer_delete - Internal function: Deactivate a timer
1394 * @timer: The timer to be deactivated
1395 * @shutdown: If true, this indicates that the timer is about to be
1398 * If @shutdown is true then @timer->function is set to NULL under the
1399 * timer base lock which prevents further rearming of the time. In that
1400 * case any attempt to rearm @timer after this function returns will be
1404 * * %0 - The timer was not pending
1405 * * %1 - The timer was pending and deactivated
1407 static int __timer_delete(struct timer_list *timer, bool shutdown) in __timer_delete() argument
1413 debug_assert_init(timer); in __timer_delete()
1417 * timer is pending or not to protect against a concurrent rearm in __timer_delete()
1420 * enqueued timer is dequeued and cannot end up with in __timer_delete()
1421 * timer->function == NULL in the expiry code. in __timer_delete()
1423 * If timer->function is currently executed, then this makes sure in __timer_delete()
1424 * that the callback cannot requeue the timer. in __timer_delete()
1426 if (timer_pending(timer) || shutdown) { in __timer_delete()
1427 base = lock_timer_base(timer, &flags); in __timer_delete()
1428 ret = detach_if_pending(timer, base, true); in __timer_delete()
1430 timer->function = NULL; in __timer_delete()
1431 raw_spin_unlock_irqrestore(&base->lock, flags); in __timer_delete()
1438 * timer_delete - Deactivate a timer
1439 * @timer: The timer to be deactivated
1441 * The function only deactivates a pending timer, but contrary to
1442 * timer_delete_sync() it does not take into account whether the timer's
1443 * callback function is concurrently executed on a different CPU or not.
1444 * It neither prevents rearming of the timer. If @timer can be rearmed
1448 * * %0 - The timer was not pending
1449 * * %1 - The timer was pending and deactivated
1451 int timer_delete(struct timer_list *timer) in timer_delete() argument
1453 return __timer_delete(timer, false); in timer_delete()
1458 * timer_shutdown - Deactivate a timer and prevent rearming
1459 * @timer: The timer to be deactivated
1461 * The function does not wait for an eventually running timer callback on a
1462 * different CPU but it prevents rearming of the timer. Any attempt to arm
1463 * @timer after this function returns will be silently ignored.
1466 * timer_shutdown_sync() cannot be invoked due to locking or context constraints.
1469 * * %0 - The timer was not pending
1470 * * %1 - The timer was pending
1472 int timer_shutdown(struct timer_list *timer) in timer_shutdown() argument
1474 return __timer_delete(timer, true); in timer_shutdown()
1479 * __try_to_del_timer_sync - Internal function: Try to deactivate a timer
1480 * @timer: Timer to deactivate
1481 * @shutdown: If true, this indicates that the timer is about to be
1484 * If @shutdown is true then @timer->function is set to NULL under the
1485 * timer base lock which prevents further rearming of the timer. Any
1486 * attempt to rearm @timer after this function returns will be silently
1489 * This function cannot guarantee that the timer cannot be rearmed
1494 * * %0 - The timer was not pending
1495 * * %1 - The timer was pending and deactivated
1496 * * %-1 - The timer callback function is running on a different CPU
1498 static int __try_to_del_timer_sync(struct timer_list *timer, bool shutdown) in __try_to_del_timer_sync() argument
1502 int ret = -1; in __try_to_del_timer_sync()
1504 debug_assert_init(timer); in __try_to_del_timer_sync()
1506 base = lock_timer_base(timer, &flags); in __try_to_del_timer_sync()
1508 if (base->running_timer != timer) in __try_to_del_timer_sync()
1509 ret = detach_if_pending(timer, base, true); in __try_to_del_timer_sync()
1511 timer->function = NULL; in __try_to_del_timer_sync()
1513 raw_spin_unlock_irqrestore(&base->lock, flags); in __try_to_del_timer_sync()
1519 * try_to_del_timer_sync - Try to deactivate a timer
1520 * @timer: Timer to deactivate
1522 * This function tries to deactivate a timer. On success the timer is not
1523 * queued and the timer callback function is not running on any CPU.
1525 * This function does not guarantee that the timer cannot be rearmed right
1530 * * %0 - The timer was not pending
1531 * * %1 - The timer was pending and deactivated
1532 * * %-1 - The timer callback function is running on a different CPU
1534 int try_to_del_timer_sync(struct timer_list *timer) in try_to_del_timer_sync() argument
1536 return __try_to_del_timer_sync(timer, false); in try_to_del_timer_sync()
1543 spin_lock_init(&base->expiry_lock); in timer_base_init_expiry_lock()
1548 spin_lock(&base->expiry_lock); in timer_base_lock_expiry()
1553 spin_unlock(&base->expiry_lock); in timer_base_unlock_expiry()
1559 * If there is a waiter for base->expiry_lock, then it was waiting for the
1560 * timer callback to finish. Drop expiry_lock and reacquire it. That allows
1564 __releases(&base->lock) __releases(&base->expiry_lock) in timer_sync_wait_running()
1565 __acquires(&base->expiry_lock) __acquires(&base->lock) in timer_sync_wait_running()
1567 if (atomic_read(&base->timer_waiters)) { in timer_sync_wait_running()
1568 raw_spin_unlock_irq(&base->lock); in timer_sync_wait_running()
1569 spin_unlock(&base->expiry_lock); in timer_sync_wait_running()
1570 spin_lock(&base->expiry_lock); in timer_sync_wait_running()
1571 raw_spin_lock_irq(&base->lock); in timer_sync_wait_running()
1577 * deletion of a timer failed because the timer callback function was
1580 * This prevents priority inversion, if the softirq thread on a remote CPU
1582 * delete a timer preempted the softirq thread running the timer callback
1585 static void del_timer_wait_running(struct timer_list *timer) in del_timer_wait_running() argument
1589 tf = READ_ONCE(timer->flags); in del_timer_wait_running()
1595 * which is held by the softirq across the timer in del_timer_wait_running()
1597 * expire the next timer. In theory the timer could already in del_timer_wait_running()
1601 atomic_inc(&base->timer_waiters); in del_timer_wait_running()
1602 spin_lock_bh(&base->expiry_lock); in del_timer_wait_running()
1603 atomic_dec(&base->timer_waiters); in del_timer_wait_running()
1604 spin_unlock_bh(&base->expiry_lock); in del_timer_wait_running()
1612 static inline void del_timer_wait_running(struct timer_list *timer) { } in del_timer_wait_running() argument
1616 * __timer_delete_sync - Internal function: Deactivate a timer and wait
1618 * @timer: The timer to be deactivated
1619 * @shutdown: If true, @timer->function will be set to NULL under the
1620 * timer base lock which prevents rearming of @timer
1622 * If @shutdown is not set the timer can be rearmed later. If the timer can
1626 * If @shutdown is set then @timer->function is set to NULL under timer
1627 * base lock which prevents rearming of the timer. Any attempt to rearm
1628 * a shutdown timer is silently ignored.
1630 * If the timer should be reused after shutdown it has to be initialized
1634 * * %0 - The timer was not pending
1635 * * %1 - The timer was pending and deactivated
1637 static int __timer_delete_sync(struct timer_list *timer, bool shutdown) in __timer_delete_sync() argument
1649 lock_map_acquire(&timer->lockdep_map); in __timer_delete_sync()
1650 lock_map_release(&timer->lockdep_map); in __timer_delete_sync()
1657 WARN_ON(in_hardirq() && !(timer->flags & TIMER_IRQSAFE)); in __timer_delete_sync()
1663 if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(timer->flags & TIMER_IRQSAFE)) in __timer_delete_sync()
1667 ret = __try_to_del_timer_sync(timer, shutdown); in __timer_delete_sync()
1670 del_timer_wait_running(timer); in __timer_delete_sync()
1679 * timer_delete_sync - Deactivate a timer and wait for the handler to finish.
1680 * @timer: The timer to be deactivated
1682 * Synchronization rules: Callers must prevent restarting of the timer,
1684 * interrupt contexts unless the timer is an irqsafe one. The caller must
1685 * not hold locks which would prevent completion of the timer's callback
1686 * function. The timer's handler must not call add_timer_on(). Upon exit
1687 * the timer is not queued and the handler is not running on any CPU.
1690 * interrupt context. Even if the lock has nothing to do with the timer in
1694 * ---- ----
1697 * base->running_timer = mytimer;
1702 * while (base->running_timer == mytimer);
1705 * The interrupt on the other CPU is waiting to grab somelock but it has
1708 * This function cannot guarantee that the timer is not rearmed again by
1717 * * %0 - The timer was not pending
1718 * * %1 - The timer was pending and deactivated
1720 int timer_delete_sync(struct timer_list *timer) in timer_delete_sync() argument
1722 return __timer_delete_sync(timer, false); in timer_delete_sync()
1727 * timer_shutdown_sync - Shutdown a timer and prevent rearming
1728 * @timer: The timer to be shutdown
1731 * - @timer is not queued
1732 * - The callback function of @timer is not running
1733 * - @timer cannot be enqueued again. Any attempt to rearm
1734 * @timer is silently ignored.
1739 * the timer is subject to a circular dependency problem.
1741 * A common pattern for this is a timer and a workqueue where the timer can
1742 * schedule work and work can arm the timer. On shutdown the workqueue must
1743 * be destroyed and the timer must be prevented from rearming. Unless the
1744 * code has conditionals like 'if (mything->in_shutdown)' to prevent that
1750 * timer_shutdown_sync(&mything->timer);
1751 * workqueue_destroy(&mything->workqueue);
1755 * This obviously implies that the timer is not required to be functional
1759 * * %0 - The timer was not pending
1760 * * %1 - The timer was pending
1762 int timer_shutdown_sync(struct timer_list *timer) in timer_shutdown_sync() argument
1764 return __timer_delete_sync(timer, true); in timer_shutdown_sync()
1768 static void call_timer_fn(struct timer_list *timer, in call_timer_fn() argument
1776 * It is permissible to free the timer from inside the in call_timer_fn()
1780 * timer->lockdep_map, make a copy and use that here. in call_timer_fn()
1784 lockdep_copy_map(&lockdep_map, &timer->lockdep_map); in call_timer_fn()
1793 trace_timer_expire_entry(timer, baseclk); in call_timer_fn()
1794 fn(timer); in call_timer_fn()
1795 trace_timer_expire_exit(timer); in call_timer_fn()
1800 WARN_ONCE(1, "timer: %pS preempt leak: %08x -> %08x\n", in call_timer_fn()
1815 * This value is required only for tracing. base->clk was in expire_timers()
1817 * is related to the old base->clk value. in expire_timers()
1819 unsigned long baseclk = base->clk - 1; in expire_timers()
1822 struct timer_list *timer; in expire_timers() local
1825 timer = hlist_entry(head->first, struct timer_list, entry); in expire_timers()
1827 base->running_timer = timer; in expire_timers()
1828 detach_timer(timer, true); in expire_timers()
1830 fn = timer->function; in expire_timers()
1834 base->running_timer = NULL; in expire_timers()
1838 if (timer->flags & TIMER_IRQSAFE) { in expire_timers()
1839 raw_spin_unlock(&base->lock); in expire_timers()
1840 call_timer_fn(timer, fn, baseclk); in expire_timers()
1841 raw_spin_lock(&base->lock); in expire_timers()
1842 base->running_timer = NULL; in expire_timers()
1844 raw_spin_unlock_irq(&base->lock); in expire_timers()
1845 call_timer_fn(timer, fn, baseclk); in expire_timers()
1846 raw_spin_lock_irq(&base->lock); in expire_timers()
1847 base->running_timer = NULL; in expire_timers()
1856 unsigned long clk = base->clk = base->next_expiry; in collect_expired_timers()
1864 if (__test_and_clear_bit(idx, base->pending_map)) { in collect_expired_timers()
1865 vec = base->vectors + idx; in collect_expired_timers()
1889 pos = find_next_bit(base->pending_map, end, start); in next_pending_bucket()
1891 return pos - start; in next_pending_bucket()
1893 pos = find_next_bit(base->pending_map, start, offset); in next_pending_bucket()
1894 return pos < start ? pos + LVL_SIZE - start : -1; in next_pending_bucket()
1898 * Search the first expiring timer in the various clock levels. Caller must
1899 * hold base->lock.
1901 * Store next expiry time in base->next_expiry.
1908 next = base->clk + NEXT_TIMER_MAX_DELTA; in timer_recalc_next_expiry()
1909 clk = base->clk; in timer_recalc_next_expiry()
1925 if (pos <= ((LVL_CLK_DIV - lvl_clk) & LVL_CLK_MASK)) in timer_recalc_next_expiry()
1932 * next expiring bucket in that level. base->clk is the next in timer_recalc_next_expiry()
1969 WRITE_ONCE(base->next_expiry, next); in timer_recalc_next_expiry()
1970 base->next_expiry_recalc = false; in timer_recalc_next_expiry()
1971 base->timers_pending = !(next == base->clk + NEXT_TIMER_MAX_DELTA); in timer_recalc_next_expiry()
1976 * Check, if the next hrtimer event is before the next timer wheel
1991 * If the next timer is already expired, return the tick base in cmp_next_hrtimer_event()
2000 * make sure that this tick really expires the timer to avoid in cmp_next_hrtimer_event()
2011 if (base->next_expiry_recalc) in next_timer_interrupt()
2016 * unnecessary raise of the timer softirq when the next_expiry value in next_timer_interrupt()
2017 * will be reached even if there is no timer pending. in next_timer_interrupt()
2020 * easy comparable to find out which base holds the first pending timer. in next_timer_interrupt()
2022 if (!base->timers_pending) in next_timer_interrupt()
2023 WRITE_ONCE(base->next_expiry, basej + NEXT_TIMER_MAX_DELTA); in next_timer_interrupt()
2025 return base->next_expiry; in next_timer_interrupt()
2052 tevt->local = basem + (u64)(nextevt - basej) * TICK_NSEC; in fetch_next_timer_interrupt()
2059 * as local timers will be handled by CPU itself. When not in fetch_next_timer_interrupt()
2060 * updating tevt->global with the already missed first global in fetch_next_timer_interrupt()
2061 * timer, it is possible that it will be missed completely. in fetch_next_timer_interrupt()
2063 * * The local callers will ignore the tevt->global anyway, when in fetch_next_timer_interrupt()
2067 tevt->global = tevt->local; in fetch_next_timer_interrupt()
2077 if (!local_first && base_global->timers_pending) in fetch_next_timer_interrupt()
2078 tevt->global = basem + (u64)(nextevt_global - basej) * TICK_NSEC; in fetch_next_timer_interrupt()
2080 if (base_local->timers_pending) in fetch_next_timer_interrupt()
2081 tevt->local = basem + (u64)(nextevt_local - basej) * TICK_NSEC; in fetch_next_timer_interrupt()
2088 * fetch_next_timer_interrupt_remote() - Store next timers into @tevt
2092 * @cpu: Remote CPU
2094 * Stores the next pending local and global timer expiry values in the
2099 * Caller needs to make sure timer base locks are held (use
2104 unsigned int cpu) in fetch_next_timer_interrupt_remote() argument
2109 tevt->local = tevt->global = KTIME_MAX; in fetch_next_timer_interrupt_remote()
2111 base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu); in fetch_next_timer_interrupt_remote()
2112 base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu); in fetch_next_timer_interrupt_remote()
2114 lockdep_assert_held(&base_local->lock); in fetch_next_timer_interrupt_remote()
2115 lockdep_assert_held(&base_global->lock); in fetch_next_timer_interrupt_remote()
2121 * timer_unlock_remote_bases - unlock timer bases of cpu
2122 * @cpu: Remote CPU
2124 * Unlocks the remote timer bases.
2126 void timer_unlock_remote_bases(unsigned int cpu) in timer_unlock_remote_bases() argument
2127 __releases(timer_bases[BASE_LOCAL]->lock) in timer_unlock_remote_bases()
2128 __releases(timer_bases[BASE_GLOBAL]->lock) in timer_unlock_remote_bases()
2132 base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu); in timer_unlock_remote_bases()
2133 base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu); in timer_unlock_remote_bases()
2135 raw_spin_unlock(&base_global->lock); in timer_unlock_remote_bases()
2136 raw_spin_unlock(&base_local->lock); in timer_unlock_remote_bases()
2140 * timer_lock_remote_bases - lock timer bases of cpu
2141 * @cpu: Remote CPU
2143 * Locks the remote timer bases.
2145 void timer_lock_remote_bases(unsigned int cpu) in timer_lock_remote_bases() argument
2146 __acquires(timer_bases[BASE_LOCAL]->lock) in timer_lock_remote_bases()
2147 __acquires(timer_bases[BASE_GLOBAL]->lock) in timer_lock_remote_bases()
2151 base_local = per_cpu_ptr(&timer_bases[BASE_LOCAL], cpu); in timer_lock_remote_bases()
2152 base_global = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu); in timer_lock_remote_bases()
2156 raw_spin_lock(&base_local->lock); in timer_lock_remote_bases()
2157 raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING); in timer_lock_remote_bases()
2161 * timer_base_is_idle() - Return whether timer base is set idle
2163 * Returns value of local timer base is_idle value.
2173 * timer_expire_remote() - expire global timers of cpu
2174 * @cpu: Remote CPU
2176 * Expire timers of global base of remote CPU.
2178 void timer_expire_remote(unsigned int cpu) in timer_expire_remote() argument
2180 struct timer_base *base = per_cpu_ptr(&timer_bases[BASE_GLOBAL], cpu); in timer_expire_remote()
2192 next_tmigr = tmigr_cpu_new_timer(tevt->global); in timer_use_tmigr()
2194 next_tmigr = tmigr_cpu_deactivate(tevt->global); in timer_use_tmigr()
2196 next_tmigr = tmigr_quick_check(tevt->global); in timer_use_tmigr()
2199 * If the CPU is the last going idle in timer migration hierarchy, make in timer_use_tmigr()
2200 * sure the CPU will wake up in time to handle remote timers. in timer_use_tmigr()
2203 if (next_tmigr < tevt->local) { in timer_use_tmigr()
2210 tmp = div_u64(next_tmigr - basem, TICK_NSEC); in timer_use_tmigr()
2213 tevt->local = next_tmigr; in timer_use_tmigr()
2222 * Make sure first event is written into tevt->local to not miss a in timer_use_tmigr()
2223 * timer on !SMP systems. in timer_use_tmigr()
2225 tevt->local = min_t(u64, tevt->local, tevt->global); in timer_use_tmigr()
2238 * When the CPU is offline, the tick is cancelled and nothing is supposed in __get_next_timer_interrupt()
2250 raw_spin_lock(&base_local->lock); in __get_next_timer_interrupt()
2251 raw_spin_lock_nested(&base_global->lock, SINGLE_DEPTH_NESTING); in __get_next_timer_interrupt()
2258 * timer migration hierarchy related functions. The value for the next in __get_next_timer_interrupt()
2259 * global timer in @tevt struct equals then KTIME_MAX. This is also in __get_next_timer_interrupt()
2260 * true, when the timer base is idle. in __get_next_timer_interrupt()
2262 * The proper timer migration hierarchy function depends on the callsite in __get_next_timer_interrupt()
2263 * and whether timer base is idle or not. @nextevt will be updated when in __get_next_timer_interrupt()
2264 * this CPU needs to handle the first timer migration hierarchy in __get_next_timer_interrupt()
2270 base_local->is_idle, &tevt); in __get_next_timer_interrupt()
2280 * Set base->is_idle only when caller is timer_base_try_to_set_idle() in __get_next_timer_interrupt()
2286 * global timer into timer migration hierarchy. Therefore a new in __get_next_timer_interrupt()
2289 * If the base is marked idle then any timer add operation must in __get_next_timer_interrupt()
2295 if (!base_local->is_idle && time_after(nextevt, basej + 1)) { in __get_next_timer_interrupt()
2296 base_local->is_idle = true; in __get_next_timer_interrupt()
2299 * in nohz_full mode need a self-IPI to kick reprogramming in __get_next_timer_interrupt()
2302 if (tick_nohz_full_cpu(base_local->cpu)) in __get_next_timer_interrupt()
2303 base_global->is_idle = true; in __get_next_timer_interrupt()
2304 trace_timer_base_idle(true, base_local->cpu); in __get_next_timer_interrupt()
2306 *idle = base_local->is_idle; in __get_next_timer_interrupt()
2309 * When timer base is not set idle, undo the effect of in __get_next_timer_interrupt()
2310 * tmigr_cpu_deactivate() to prevent inconsistent states - active in __get_next_timer_interrupt()
2311 * timer base but inactive timer migration hierarchy. in __get_next_timer_interrupt()
2313 * When timer base was already marked idle, nothing will be in __get_next_timer_interrupt()
2316 if (!base_local->is_idle && idle_is_possible) in __get_next_timer_interrupt()
2320 raw_spin_unlock(&base_global->lock); in __get_next_timer_interrupt()
2321 raw_spin_unlock(&base_local->lock); in __get_next_timer_interrupt()
2327 * get_next_timer_interrupt() - return the time (clock mono) of the next timer
2331 * Returns the tick aligned clock monotonic time of the next pending timer or
2332 * KTIME_MAX if no timer is pending. If timer of global base was queued into
2333 * timer migration hierarchy, first global timer is not taken into account. If
2334 * it was the last CPU of timer migration hierarchy going idle, first global
2343 * timer_base_try_to_set_idle() - Try to set the idle state of the timer bases
2346 * @idle: pointer to store the value of timer_base->is_idle on return;
2349 * Returns the tick aligned clock monotonic time of the next pending timer or
2350 * KTIME_MAX if no timer is pending. When tick was already stopped KTIME_MAX is
2362 * timer_clear_idle - Clear the idle state of the timer base
2369 * We do this unlocked. The worst outcome is a remote pinned timer in timer_clear_idle()
2380 /* Activate without holding the timer_base->lock */ in timer_clear_idle()
2386 * __run_timers - run all expired timers (if any) on this CPU.
2387 * @base: the timer vector to be processed.
2394 lockdep_assert_held(&base->lock); in __run_timers()
2396 if (base->running_timer) in __run_timers()
2399 while (time_after_eq(jiffies, base->clk) && in __run_timers()
2400 time_after_eq(jiffies, base->next_expiry)) { in __run_timers()
2404 * timer at this clk are that all matching timers have been in __run_timers()
2405 * dequeued or no timer has been queued since in __run_timers()
2409 WARN_ON_ONCE(!levels && !base->next_expiry_recalc in __run_timers()
2410 && base->timers_pending); in __run_timers()
2412 * While executing timers, base->clk is set 1 offset ahead of in __run_timers()
2415 base->clk++; in __run_timers()
2418 while (levels--) in __run_timers()
2425 if (time_before(jiffies, base->next_expiry)) in __run_timer_base()
2429 raw_spin_lock_irq(&base->lock); in __run_timer_base()
2431 raw_spin_unlock_irq(&base->lock); in __run_timer_base()
2443 * This function runs timers and the timer-tq in bottom half context.
2458 * Called by the local, per-CPU timer interrupt on SMP.
2470 * timer_base::next_expiry can be written by a remote CPU while in run_local_timers()
2476 * timer_base::next_expiry is written by a remote CPU: in run_local_timers()
2478 * 1. Remote CPU expires global timers of this CPU and updates in run_local_timers()
2481 * worst outcome is a superfluous raise of the timer softirq in run_local_timers()
2484 * 2. A new first pinned timer is enqueued by a remote CPU in run_local_timers()
2487 * problem, as an IPI is executed nevertheless when the CPU in run_local_timers()
2488 * was idle before. When the CPU wasn't idle but the update in run_local_timers()
2489 * is missed, then the timer would expire one jiffy late - in run_local_timers()
2500 if (time_after_eq(jiffies, READ_ONCE(base->next_expiry)) || in run_local_timers()
2509 * Called from the timer interrupt handler to charge one tick to the current
2516 /* Note: this timer irq context must be accounted for as well. */ in update_process_times()
2530 * Since schedule_timeout()'s timer is defined on the stack, it must store
2534 struct timer_list timer; member
2540 struct process_timer *timeout = from_timer(timeout, t, timer); in process_timeout()
2542 wake_up_process(timeout->task); in process_timeout()
2546 * schedule_timeout - sleep until timeout
2553 * %TASK_RUNNING - the scheduler is called, but the task does not sleep
2557 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
2561 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
2569 * the CPU away without a bound on the timeout. In this case the return
2572 * Returns 0 when the timer has expired otherwise the remaining time in
2574 * to be non-negative.
2578 struct process_timer timer; in schedule_timeout() local
2612 timer.task = current; in schedule_timeout()
2613 timer_setup_on_stack(&timer.timer, process_timeout, 0); in schedule_timeout()
2614 __mod_timer(&timer.timer, expire, MOD_TIMER_NOTPENDING); in schedule_timeout()
2616 del_timer_sync(&timer.timer); in schedule_timeout()
2618 /* Remove the timer from the object tracker */ in schedule_timeout()
2619 destroy_timer_on_stack(&timer.timer); in schedule_timeout()
2621 timeout = expire - jiffies; in schedule_timeout()
2667 struct timer_list *timer; in migrate_timer_list() local
2668 int cpu = new_base->cpu; in migrate_timer_list() local
2671 timer = hlist_entry(head->first, struct timer_list, entry); in migrate_timer_list()
2672 detach_timer(timer, false); in migrate_timer_list()
2673 timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu; in migrate_timer_list()
2674 internal_add_timer(new_base, timer); in migrate_timer_list()
2678 int timers_prepare_cpu(unsigned int cpu) in timers_prepare_cpu() argument
2684 base = per_cpu_ptr(&timer_bases[b], cpu); in timers_prepare_cpu()
2685 base->clk = jiffies; in timers_prepare_cpu()
2686 base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA; in timers_prepare_cpu()
2687 base->next_expiry_recalc = false; in timers_prepare_cpu()
2688 base->timers_pending = false; in timers_prepare_cpu()
2689 base->is_idle = false; in timers_prepare_cpu()
2694 int timers_dead_cpu(unsigned int cpu) in timers_dead_cpu() argument
2701 old_base = per_cpu_ptr(&timer_bases[b], cpu); in timers_dead_cpu()
2707 raw_spin_lock_irq(&new_base->lock); in timers_dead_cpu()
2708 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); in timers_dead_cpu()
2716 WARN_ON_ONCE(old_base->running_timer); in timers_dead_cpu()
2717 old_base->running_timer = NULL; in timers_dead_cpu()
2720 migrate_timer_list(new_base, old_base->vectors + i); in timers_dead_cpu()
2722 raw_spin_unlock(&old_base->lock); in timers_dead_cpu()
2723 raw_spin_unlock_irq(&new_base->lock); in timers_dead_cpu()
2731 static void __init init_timer_cpu(int cpu) in init_timer_cpu() argument
2737 base = per_cpu_ptr(&timer_bases[i], cpu); in init_timer_cpu()
2738 base->cpu = cpu; in init_timer_cpu()
2739 raw_spin_lock_init(&base->lock); in init_timer_cpu()
2740 base->clk = jiffies; in init_timer_cpu()
2741 base->next_expiry = base->clk + NEXT_TIMER_MAX_DELTA; in init_timer_cpu()
2748 int cpu; in init_timer_cpus() local
2750 for_each_possible_cpu(cpu) in init_timer_cpus()
2751 init_timer_cpu(cpu); in init_timer_cpus()
2762 * msleep - sleep safely even with waitqueue interruptions
2776 * msleep_interruptible - sleep waiting for signals
2791 * usleep_range_state - Sleep for an approximate time in a given state
2796 * In non-atomic context where the exact wakeup time is flexible, use
2798 * by avoiding the CPU-hogging busy-wait of udelay(), and the range reduces
2799 * power usage by allowing hrtimers to take advantage of an already-
2806 u64 delta = (u64)(max - min) * NSEC_PER_USEC; in usleep_range_state()