From nobody Wed Sep 10 02:01:38 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B246DE7B601 for ; Wed, 4 Oct 2023 12:35:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242466AbjJDMfz (ORCPT ); Wed, 4 Oct 2023 08:35:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242371AbjJDMfe (ORCPT ); Wed, 4 Oct 2023 08:35:34 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3BC4C1 for ; Wed, 4 Oct 2023 05:35:31 -0700 (PDT) From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1696422930; 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: in-reply-to:in-reply-to:references:references; bh=dxpi78IDwxZZgGLEvaVUb//vKcdGLGnLWfwEyUx5Nmw=; b=BDLOxBrpk3OptCr9bdEYLu1NT+9K7T/RiwoEvkZ/7gmRSnp35L1Ky0L5I76iA0nT6WpyF/ /UAflRMfwUegkPMshVRxDOX46Ng8CXg+0RjKSp/JD2ZfmUJWF9HQP4JG0d0gHiPQO3XDKU CRAtp0MTRbGrsi04whb4Q2ET2AKv4V/35kS1QR0LIvvXkwump5FR6wUEgQV7bpRHOjPkhe W3ZaAJsdUFWBJ4WjU4cNDcyE5jk21IpJU1lECmoFHmKS0Shy1EWc5eGhkr6LeAyqL1kAh2 7qHcP0n+Y6206fWDV9dDpDFhzqn/y8ODkl0V/hSIapte4S78tQBHOzW+/jS05g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1696422930; 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: in-reply-to:in-reply-to:references:references; bh=dxpi78IDwxZZgGLEvaVUb//vKcdGLGnLWfwEyUx5Nmw=; b=ZjvV/Gf61Hjp7Rgy1OmNsLZBFCK3wKahqbT70dfSzdcmtdWYmwpBuE+3dGglFB3SyiNNCP Nks9H9Zliz8MaGCA== To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , John Stultz , Thomas Gleixner , Eric Dumazet , "Rafael J . Wysocki" , Arjan van de Ven , "Paul E . McKenney" , Frederic Weisbecker , Rik van Riel , Steven Rostedt , Sebastian Siewior , Giovanni Gherdovich , Lukasz Luba , "Gautham R . Shenoy" , Srinivas Pandruvada , K Prateek Nayak , Anna-Maria Behnsen Subject: [PATCH v8 08/25] timer: Rework idle logic Date: Wed, 4 Oct 2023 14:34:37 +0200 Message-Id: <20231004123454.15691-9-anna-maria@linutronix.de> In-Reply-To: <20231004123454.15691-1-anna-maria@linutronix.de> References: <20231004123454.15691-1-anna-maria@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner To improve readability of the code, split base->idle calculation and expires calculation into separate parts. Thereby the following subtle change happens if the next event is just one jiffy ahead and the tick was already stopped: Originally base->is_idle remains true in this situation. Now base->is_idle turns to false. This may spare an IPI if a timer is enqueued remotely to an idle CPU that is going to tick on the next jiffy. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen Reviewed-by: Frederic Weisbecker --- v4: Change condition to force 0 delta and update commit message (Frederic) --- kernel/time/timer.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index dc58c479d35a..18f8aac9b19a 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1943,21 +1943,20 @@ u64 get_next_timer_interrupt(unsigned long basej, u= 64 basem) */ __forward_timer_base(base, basej); =20 - if (time_before_eq(nextevt, basej)) { - expires =3D basem; - base->is_idle =3D false; - } else { - if (base->timers_pending) - expires =3D basem + (u64)(nextevt - basej) * TICK_NSEC; - /* - * If we expect to sleep more than a tick, mark the base idle. - * Also the tick is stopped so any added timer must forward - * the base clk itself to keep granularity small. This idle - * logic is only maintained for the BASE_STD base, deferrable - * timers may still see large granularity skew (by design). - */ - if ((expires - basem) > TICK_NSEC) - base->is_idle =3D true; + /* + * Base is idle if the next event is more than a tick away. Also + * the tick is stopped so any added timer must forward the base clk + * itself to keep granularity small. This idle logic is only + * maintained for the BASE_STD base, deferrable timers may still + * see large granularity skew (by design). + */ + base->is_idle =3D time_after(nextevt, basej + 1); + + if (base->timers_pending) { + /* If we missed a tick already, force 0 delta */ + if (time_before(nextevt, basej)) + nextevt =3D basej; + expires =3D basem + (u64)(nextevt - basej) * TICK_NSEC; } raw_spin_unlock(&base->lock); =20 --=20 2.39.2