From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7E982874E6 for ; Tue, 7 Jul 2026 15:38:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783438694; cv=none; b=Wz4gxo7TwgBzmaLI15ADfcIegbtlTfek0ALkrJsy9ZC1PF6vPaQv4ltxRBy/bbB4sK6CNgkTg9QLjBnynSHSzNFLJb9nzu4kV5L4zPGn6aOYnRlwrCEybTc/ZjrdmVkllUtQY2/Fer86Tz6GkWrPB0c7W9F4YjtLIBTqF5QO2VI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783438694; c=relaxed/simple; bh=nQLYWNu8G9l0Q49KQyPEZSrvgx2nPhjLqX2g/u6+S+w=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=S0I07pqHyslVnnVjddF+FqF3SRtbMHYT4jRRqt52ihlHAfLNQtwPe7FAk76koQIh3kXbirny8jVUpsL9Vmahz76dyboUuLMQ5vNiGVZMOsd5ehE203ZOyMXo42BqwvDZp+4OsVDNg0P3/PjKg7jpmChiirW75ahwC/JPaUZliNs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=bnJb43pQ; arc=none smtp.client-ip=95.215.58.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="bnJb43pQ" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1783438690; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=rt1CEWERQyMuQohqHgJ3MyAf04ugg1kfEfZDRfH8FsI=; b=bnJb43pQ7NZAM/6gAiF4WopKbLHrU/gAWOA9J1+Sdpc1dhs+0HPNh/I9JiczxtjC7r3TuH rdwEdQMHSFYzfSfzxqXKNLS4MkTc0S0SaOj8KV6TJvrmGPsU04bo+XvJCWuKcPVGJ/t1k+ NhbYjAyI5Ok3Fh2sKcqSqEfRrjCHqFo= From: Usama Arif 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 Subject: [PATCH] hrtimers: skip cpu_base->lock in hrtimer_get_next_event() when hres_active Date: Tue, 7 Jul 2026 08:38:00 -0700 Message-ID: <20260707153800.542394-1-usama.arif@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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 --- 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