hrtimers: skip cpu_base->lock in hrtimer_get_next_event() when hres_active
PATCH 1/1 hrtimers: skip cpu_base->lock in hrtimer_get_next_event() when hres_active
hrtimer_get_next_event() runs on every tick-stop decision via get_next_timer_interrupt() -> cmp_next_hrtimer_event(). When high resolution timers are active it must return KTIME_MAX -- the caller documents and depends on this.
Yet the current body takes cpu_base->lock, checks hres_active, and returns KTIME_MAX anyway. On any modern kernel HRES is on continuously after boot, so the lock acquisition is pure overhead on every idle-decision path.
On a 176-thread AMD EPYC 9D64 running a Meta production workload, bucketing callers of native_queued_spin_lock_slowpath by the _raw_spin_lock*, the slowpath had 199 samples total in total, of which 54 are attributed to hrtimer_get_next_event+0x2b (the guard() lock take), i.e. this lock take accounts for ~27% of irqsave slowpath hits on this specific workload.
cpu_base->hres_active is only written by the local CPU (hrtimer_switch_to_hres() from hardirq context, hrtimers_cpu_starting() during bring-up). All callers of hrtimer_get_next_event() reach it from the tick-stop / cpuidle paths with interrupts disabled on that CPU (__get_next_timer_interrupt() itself takes base_local->lock with raw_spin_lock(), not raw_spin_lock_irqsave()). No writer can therefore race with the read, so an unlocked hres_active check is stable and the lock can be skipped in the common case.
--- kernel/time/hrtimer.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
kernel/time/hrtimer.c +11 -5
@@ -1786,13 +1786,19 @@ EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);ktime_t hrtimer_get_next_event (void ) {struct hrtimer_cpu_base *cpu_base =this_cpu_ptr (&hrtimer_bases); -ktime_t expires =KTIME_MAX ; -guard (raw_spinlock_irqsave)(&cpu_base->lock); -if (!hrtimer_hres_active (cpu_base)) - expires =__hrtimer_get_next_event (cpu_base,HRTIMER_ACTIVE_ALL ); +/* +* When HRES is active the caller (cmp_next_hrtimer_event()) expects +* KTIME_MAX. cpu_base->hres_active is written only by the local CPU +* (hrtimer_switch_to_hres() from hardirq, hrtimers_cpu_starting() +* during bring-up), and all callers reach this with IRQs disabled on +* the same CPU, so an unlocked read is stable across the lock below. +*/ +if (hrtimer_hres_active (cpu_base)) +return KTIME_MAX ; -return expires; +guard (raw_spinlock_irqsave)(&cpu_base->lock); +return __hrtimer_get_next_event (cpu_base,HRTIMER_ACTIVE_ALL ); }/**
Signature · 1 line
2.53.0-Meta
[tip: timers/core] hrtimer: Don't take cpu_base::lock in hrtimer_get_next_event() when hres_active
The following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: c4415c993fc2c8bdf2cf0bfbcbb1ac0e0f7a9eaf Gitweb: https://git.kernel.org/tip/c4415c993fc2c8bdf2cf0bfbcbb1ac0e0f7a9eaf Author: Usama Arif <[email protected]> AuthorDate: Tue, 07 Jul 2026 08:38:00 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Tue, 07 Jul 2026 23:26:58 +02:00
hrtimer: Don't take cpu_base::lock in hrtimer_get_next_event() when hres_active
hrtimer_get_next_event() runs on every tick-stop decision via get_next_timer_interrupt() -> cmp_next_hrtimer_event(). When high resolution timers are active it must return KTIME_MAX -- the caller documents and depends on this.
The function takes cpu_base->lock, checks hres_active, and returns KTIME_MAX.
Taking the lock is not required because cpu_base->hres_active is only written by the local CPU in hrtimer_switch_to_hres() from hard interrupt context and in hrtimers_cpu_starting() during bring-up.
All callers of hrtimer_get_next_event() reach it from the tick-stop / cpuidle paths with interrupts disabled on that CPU. No writer can therefore race with the read, so an unlocked hres_active check is stable and the lock can be skipped in this case.
On a 176-thread AMD EPYC 9D64 running a production workload, bucketing callers of native_queued_spin_lock_slowpath(), the slowpath had 199 samples in total, of which 54 are attributed to hrtimer_get_next_event(), i.e. this accounts for ~27% of slowpath hits on this specific workload.
Move the hres_active check before the lock guard region to address this.
[ tglx: Massaged change log and comments ]
--- kernel/time/hrtimer.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
kernel/time/hrtimer.c +13 -5
@@ -1786,13 +1786,21 @@ EXPORT_SYMBOL_GPL(__hrtimer_get_remaining);ktime_t hrtimer_get_next_event (void ) {struct hrtimer_cpu_base *cpu_base =this_cpu_ptr (&hrtimer_bases); -ktime_t expires =KTIME_MAX ; -guard (raw_spinlock_irqsave)(&cpu_base->lock); -if (!hrtimer_hres_active (cpu_base)) - expires =__hrtimer_get_next_event (cpu_base,HRTIMER_ACTIVE_ALL ); +/* +* When HRES is active cmp_next_hrtimer_event() expects KTIME_MAX. +* +* cpu_base->hres_active is written only by the local CPU in +* hrtimer_switch_to_hres() from hard interrupt context and in +* hrtimers_cpu_starting() during CPU bring-up, and all callers reach +* this with interrupts disabled on the same CPU, so an unlocked read is +* stable without holding the lock. +*/ +if (hrtimer_hres_active (cpu_base)) +return KTIME_MAX ; -return expires; +guard (raw_spinlock_irqsave)(&cpu_base->lock); +return __hrtimer_get_next_event (cpu_base,HRTIMER_ACTIVE_ALL ); }/**