Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 08C4A375AC1 for ; Sat, 28 Feb 2026 18:00:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772301638; cv=none; b=Zcop2L8E6dWdbXA2pccGBMgQbbr7wDS245tzLnwo0jp0mkA3KqLyVFDn7lfbiO4Kfxl/rUWFqUaAv41w4NAAbbOO/WTEgrSycjvkrr4CY4i8HWt0l2LdZ9WkNtsb0VFARdpMpmJffZArh5B5Xzv/X5xj7DJYgELy4IVD9o2hY+Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772301638; c=relaxed/simple; bh=HqhzGSVJRu5VvQrM4c+N5ADqSBwhj7jwDQM0fSVkkKo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H3RAvMICvTmYz/WfhLzZv26dfRcdYOWCz74twzokVZ6jv88o4o7RqkZRgMZmzFO6ZeHV9uq58WEghD1Um7t0E/KYoE+cafBg3rZ6nqIfygo5un83jlHkks+92+aQ9N+PVNPmroxzGlIgmGpXA09npoejBGCD9tEsjZV7/bvrYC0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=U/k8Rqp1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="U/k8Rqp1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6F1F4C116D0; Sat, 28 Feb 2026 18:00:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1772301637; bh=HqhzGSVJRu5VvQrM4c+N5ADqSBwhj7jwDQM0fSVkkKo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U/k8Rqp1rc2RLDcdEOBnDHXJRkbIYH+dc56Uofgh7uTxU7ykaROtoswnay8ZB3EPN zFAkHjfWK7q9IaoksEzvUEVddFbcOP6HS2XvKy1iPokSz+d2TLISoOTvYOO+mBTl7T o6TdfUuMWNxUl7Fba6VW5apGVtf1aCTy/dS1bzGhx1/w8N1hx7PHo6alU33/dR8dyC agd5a9nbTF5YgKP3F3BPODqd6wU47oEtfVM/jfFF0CTnZLcpP0GlI85STq4il/hNT1 XXAwPIUZqp6mvkIjfBiGdv7B93abDnZNQa05E4Ixtasiw+gCQgSCbV2nmpII1V0Nhn L2edhiGPKRZ+g== From: Sasha Levin To: patches@lists.linux.dev Cc: "Rafael J. Wysocki" , Christian Loehle , Sasha Levin Subject: [PATCH 6.12 031/385] cpuidle: governors: menu: Always check timers with tick stopped Date: Sat, 28 Feb 2026 12:54:13 -0500 Message-ID: <20260228180011.1568201-31-sashal@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260228180011.1568201-1-sashal@kernel.org> References: <20260228180011.1568201-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: "Rafael J. Wysocki" [ Upstream commit 80606f4eb8d7484ab7f7d6f0fd30d71e6fbcf328 ] After commit 5484e31bbbff ("cpuidle: menu: Skip tick_nohz_get_sleep_length() call in some cases"), if the return value of get_typical_interval() multiplied by NSEC_PER_USEC is not greater than RESIDENCY_THRESHOLD_NS, the menu governor will skip computing the time till the closest timer. If that happens when the tick has been stopped already, the selected idle state may be too deep due to the subsequent check comparing predicted_ns with TICK_NSEC and causing its value to be replaced with the expected time till the closest timer, which is KTIME_MAX in that case. That will cause the deepest enabled idle state to be selected, but the time till the closest timer very well may be shorter than the target residency of that state, in which case a shallower state should be used. Address this by making menu_select() always compute the time till the closest timer when the tick has been stopped. Also move the predicted_ns check mentioned above into the branch in which the time till the closest timer is determined because it only needs to be done in that case. Fixes: 5484e31bbbff ("cpuidle: menu: Skip tick_nohz_get_sleep_length() call in some cases") Signed-off-by: Rafael J. Wysocki Reviewed-by: Christian Loehle Link: https://patch.msgid.link/5959091.DvuYhMxLoT@rafael.j.wysocki Signed-off-by: Sasha Levin --- drivers/cpuidle/governors/menu.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/cpuidle/governors/menu.c b/drivers/cpuidle/governors/menu.c index 3be761961f1be..0ce7323450011 100644 --- a/drivers/cpuidle/governors/menu.c +++ b/drivers/cpuidle/governors/menu.c @@ -245,7 +245,7 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, /* Find the shortest expected idle interval. */ predicted_ns = get_typical_interval(data) * NSEC_PER_USEC; - if (predicted_ns > RESIDENCY_THRESHOLD_NS) { + if (predicted_ns > RESIDENCY_THRESHOLD_NS || tick_nohz_tick_stopped()) { unsigned int timer_us; /* Determine the time till the closest timer. */ @@ -265,6 +265,16 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, RESOLUTION * DECAY * NSEC_PER_USEC); /* Use the lowest expected idle interval to pick the idle state. */ predicted_ns = min((u64)timer_us * NSEC_PER_USEC, predicted_ns); + /* + * If the tick is already stopped, the cost of possible short + * idle duration misprediction is much higher, because the CPU + * may be stuck in a shallow idle state for a long time as a + * result of it. In that case, say we might mispredict and use + * the known time till the closest timer event for the idle + * state selection. + */ + if (tick_nohz_tick_stopped() && predicted_ns < TICK_NSEC) + predicted_ns = data->next_timer_ns; } else { /* * Because the next timer event is not going to be determined @@ -290,16 +300,6 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, return 0; } - /* - * If the tick is already stopped, the cost of possible short idle - * duration misprediction is much higher, because the CPU may be stuck - * in a shallow idle state for a long time as a result of it. In that - * case, say we might mispredict and use the known time till the closest - * timer event for the idle state selection. - */ - if (tick_nohz_tick_stopped() && predicted_ns < TICK_NSEC) - predicted_ns = data->next_timer_ns; - /* * Find the idle state with the lowest power while satisfying * our constraints. -- 2.51.0