Skip to content

Commit b2cf750

Browse files
anna-marialxKAGA-KOKO
authored andcommitted
timers: Always queue timers on the local CPU
The timer pull model is in place so we can remove the heuristics which try to guess the best target CPU at enqueue/modification time. All non pinned timers are queued on the local CPU in the separate storage and eventually pulled at expiry time to a remote CPU. Originally-by: Richard Cochran (linutronix GmbH) <richardcochran@gmail.com> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Frederic Weisbecker <frederic@kernel.org> Link: https://lore.kernel.org/r/20240221090548.36600-21-anna-maria@linutronix.de
1 parent 36e40df commit b2cf750

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

include/linux/timer.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,10 @@
3636
* workqueue locking issues. It's not meant for executing random crap
3737
* with interrupts disabled. Abuse is monitored!
3838
*
39-
* @TIMER_PINNED: A pinned timer will not be affected by any timer
40-
* placement heuristics (like, NOHZ) and will always expire on the CPU
41-
* on which the timer was enqueued.
42-
*
43-
* Note: Because enqueuing of timers can migrate the timer from one
44-
* CPU to another, pinned timers are not guaranteed to stay on the
45-
* initialy selected CPU. They move to the CPU on which the enqueue
46-
* function is invoked via mod_timer() or add_timer(). If the timer
47-
* should be placed on a particular CPU, then add_timer_on() has to be
48-
* used.
39+
* @TIMER_PINNED: A pinned timer will always expire on the CPU on which the
40+
* timer was enqueued. When a particular CPU is required, add_timer_on()
41+
* has to be used. Enqueue via mod_timer() and add_timer() is always done
42+
* on the local CPU.
4943
*/
5044
#define TIMER_CPUMASK 0x0003FFFF
5145
#define TIMER_MIGRATING 0x00040000

kernel/time/timer.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,16 @@ trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
635635

636636
/*
637637
* We might have to IPI the remote CPU if the base is idle and the
638-
* timer is not deferrable. If the other CPU is on the way to idle
639-
* then it can't set base->is_idle as we hold the base lock:
638+
* timer is pinned. If it is a non pinned timer, it is only queued
639+
* on the remote CPU, when timer was running during queueing. Then
640+
* everything is handled by remote CPU anyway. If the other CPU is
641+
* on the way to idle then it can't set base->is_idle as we hold
642+
* the base lock:
640643
*/
641-
if (base->is_idle)
644+
if (base->is_idle) {
645+
WARN_ON_ONCE(!(timer->flags & TIMER_PINNED));
642646
wake_up_nohz_cpu(base->cpu);
647+
}
643648
}
644649

645650
/*
@@ -986,17 +991,6 @@ static inline struct timer_base *get_timer_base(u32 tflags)
986991
return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
987992
}
988993

989-
static inline struct timer_base *
990-
get_target_base(struct timer_base *base, unsigned tflags)
991-
{
992-
#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
993-
if (static_branch_likely(&timers_migration_enabled) &&
994-
!(tflags & TIMER_PINNED))
995-
return get_timer_cpu_base(tflags, get_nohz_timer_target());
996-
#endif
997-
return get_timer_this_cpu_base(tflags);
998-
}
999-
1000994
static inline void __forward_timer_base(struct timer_base *base,
1001995
unsigned long basej)
1002996
{
@@ -1151,7 +1145,7 @@ __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int option
11511145
if (!ret && (options & MOD_TIMER_PENDING_ONLY))
11521146
goto out_unlock;
11531147

1154-
new_base = get_target_base(base, timer->flags);
1148+
new_base = get_timer_this_cpu_base(timer->flags);
11551149

11561150
if (base != new_base) {
11571151
/*
@@ -2297,7 +2291,7 @@ static inline u64 __get_next_timer_interrupt(unsigned long basej, u64 basem,
22972291
* granularity skew (by design).
22982292
*/
22992293
if (!base_local->is_idle && time_after(nextevt, basej + 1)) {
2300-
base_local->is_idle = base_global->is_idle = true;
2294+
base_local->is_idle = true;
23012295
trace_timer_base_idle(true, base_local->cpu);
23022296
}
23032297
*idle = base_local->is_idle;
@@ -2363,13 +2357,13 @@ u64 timer_base_try_to_set_idle(unsigned long basej, u64 basem, bool *idle)
23632357
void timer_clear_idle(void)
23642358
{
23652359
/*
2366-
* We do this unlocked. The worst outcome is a remote enqueue sending
2367-
* a pointless IPI, but taking the lock would just make the window for
2368-
* sending the IPI a few instructions smaller for the cost of taking
2369-
* the lock in the exit from idle path.
2360+
* We do this unlocked. The worst outcome is a remote pinned timer
2361+
* enqueue sending a pointless IPI, but taking the lock would just
2362+
* make the window for sending the IPI a few instructions smaller
2363+
* for the cost of taking the lock in the exit from idle
2364+
* path. Required for BASE_LOCAL only.
23702365
*/
23712366
__this_cpu_write(timer_bases[BASE_LOCAL].is_idle, false);
2372-
__this_cpu_write(timer_bases[BASE_GLOBAL].is_idle, false);
23732367
trace_timer_base_idle(false, smp_processor_id());
23742368

23752369
/* Activate without holding the timer_base->lock */

0 commit comments

Comments
 (0)