The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Usama Arif <usama.arif@linux.dev>
To: anna-maria@linutronix.de, frederic@kernel.org,
	linux-kernel@vger.kernel.org, tglx@kernel.org, d@ilvokhin.com,
	shakeel.butt@linux.dev, hannes@cmpxchg.org, riel@surriel.com,
	kernel-team@meta.com
Cc: Usama Arif <usama.arif@linux.dev>
Subject: [PATCH] hrtimers: skip cpu_base->lock in hrtimer_get_next_event() when hres_active
Date: Tue,  7 Jul 2026 08:38:00 -0700	[thread overview]
Message-ID: <20260707153800.542394-1-usama.arif@linux.dev> (raw)

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.

Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
 kernel/time/hrtimer.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 313dcea127fe..5be2cb035b86 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -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);
 }
 
 /**
-- 
2.53.0-Meta


             reply	other threads:[~2026-07-07 15:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07 15:38 Usama Arif [this message]
2026-07-07 21:32 ` [tip: timers/core] hrtimer: Don't take cpu_base::lock in hrtimer_get_next_event() when hres_active tip-bot2 for Usama Arif

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260707153800.542394-1-usama.arif@linux.dev \
    --to=usama.arif@linux.dev \
    --cc=anna-maria@linutronix.de \
    --cc=d@ilvokhin.com \
    --cc=frederic@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=riel@surriel.com \
    --cc=shakeel.butt@linux.dev \
    --cc=tglx@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox