From nobody Tue Dec 30 12:49:31 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 65A53C2BB3F for ; Wed, 15 Nov 2023 15:14:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344431AbjKOPOM (ORCPT ); Wed, 15 Nov 2023 10:14:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344468AbjKOPOE (ORCPT ); Wed, 15 Nov 2023 10:14:04 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6ED45D41 for ; Wed, 15 Nov 2023 07:14:01 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9651C433CA; Wed, 15 Nov 2023 15:13:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700061240; bh=CGc4yN675I1g1HtS2yRFvKBFqp1N2IpYVLKrHGqJRhc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i5OSOvr+26OB70bGr8parKnbffR+qe+cbu6cntwePyetORAYqXomqnEf+fL9pK1nA oil1YHr0Ngj4yeGAdJjXSgqCzf8HyyZtltBVN7hrWyy5TY4NJBonMHGbhwVNGYME+h cfwWz8A4lZ6utJQNakqG1cT/t6t1aOb4N1PnGxbvfRag/XsbyQcu13wz8fAtMNlhGu +30WIPYtzl4OUvZvcFZgCYAU00jt01npgmCgwmDV+3cc5oXnM1FHxsLKs2ITAFFryn 7Mt+n70WFKOhDEQKJhwn8+i92ASJo9pZ0RVp+XGD/esOsLCDE5v3zC0qY5wne766bW Zh76QhVKMQPBQ== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Peter Zijlstra , x86@kernel.org, "Rafael J . Wysocki" Subject: [PATCH 1/4] x86: Add a comment about the "magic" behind shadow sti before mwait Date: Wed, 15 Nov 2023 10:13:22 -0500 Message-ID: <20231115151325.6262-2-frederic@kernel.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231115151325.6262-1-frederic@kernel.org> References: <20231115151325.6262-1-frederic@kernel.org> 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" Add a note to make sure we never miss and break the requirements behind it. Acked-by: Rafael J. Wysocki Signed-off-by: Frederic Weisbecker --- arch/x86/include/asm/mwait.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index 778df05f8539..341ee4f1d91e 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -87,6 +87,15 @@ static __always_inline void __mwaitx(unsigned long eax, = unsigned long ebx, :: "a" (eax), "b" (ebx), "c" (ecx)); } =20 +/* + * Re-enable interrupts right upon calling mwait in such a way that + * no interrupt can fire _before_ the execution of mwait, ie: no + * instruction must be placed between "sti" and "mwait". + * + * This is necessary because if an interrupt queues a timer before + * executing mwait, it would otherwise go unnoticed and the next tick + * would not be reprogrammed accordingly before mwait ever wakes up. + */ static __always_inline void __sti_mwait(unsigned long eax, unsigned long e= cx) { mds_idle_clear_cpu_buffers(); --=20 2.42.1 From nobody Tue Dec 30 12:49:31 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 EA3B4C2BB3F for ; Wed, 15 Nov 2023 15:14:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344460AbjKOPOP (ORCPT ); Wed, 15 Nov 2023 10:14:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344441AbjKOPOH (ORCPT ); Wed, 15 Nov 2023 10:14:07 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44298187 for ; Wed, 15 Nov 2023 07:14:04 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7693BC433C8; Wed, 15 Nov 2023 15:14:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700061243; bh=KLrlZdWeoxjPSxqCeWK5YXQdueYFpYJySJ8TOt2dOEY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UqzQv9sFvb6sCefg7/xL0Sv84ZnEJPUhXfAiXi9JnJkJkJTI57wGnSbLSuYsnIZ6t NDGhVcd3iFdEcq1UJtQqfmPS89fVn8PIB9VlKLacTgjgky9PTMJBNf2YubHupmmo9k ciqp3s+vmxryhNZPVVsvgnJhxBakJ/rD7zKpDEwOqP4BkLN1yvNYAsEsHwPnvdZgOq 82786xqgaMSSTEsHqsonJ2+VUpk5mvKhuWBFR6PVF8tMDyjmayANExKWRS4cWzZbzq rv7dDsFATmh+3KMfBuUtJE8mbKbPpHbNxekhKhxNyxV1odpclLDNECXY3EVE7SlPi3 oIJdXmO2C4E7A== From: Frederic Weisbecker To: LKML Cc: Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "Rafael J . Wysocki" , Frederic Weisbecker Subject: [PATCH 2/4] x86: Fix CPUIDLE_FLAG_IRQ_ENABLE leaking timer reprogram Date: Wed, 15 Nov 2023 10:13:23 -0500 Message-ID: <20231115151325.6262-3-frederic@kernel.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231115151325.6262-1-frederic@kernel.org> References: <20231115151325.6262-1-frederic@kernel.org> 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: Peter Zijlstra intel_idle_irq() re-enables IRQs very early. As a result, an interrupt may fire before mwait() is eventually called. If such an interrupt queues a timer, it may go unnoticed until mwait returns and the idle loop handles the tick re-evaluation. And monitoring TIF_NEED_RESCHED doesn't help because a local timer enqueue doesn't set that flag. The issue is mitigated by the fact that this idle handler is only invoked for shallow C-states when, presumably, the next tick is supposed to be close enough. There may still be rare cases though when the next tick is far away and the selected C-state is shallow, resulting in a timer getting ignored for a while. Fix this with using sti_mwait() whose IRQ-reenablement only triggers upon calling mwait(), dealing with the race while keeping the interrupt latency within acceptable bounds. Fixes: c227233ad64c (intel_idle: enable interrupts before C1 on Xeons) Not-yet-signed-off-by: Peter Zijlstra Acked-by: Rafael J. Wysocki Signed-off-by: Frederic Weisbecker --- arch/x86/include/asm/mwait.h | 11 +++++++++-- drivers/idle/intel_idle.c | 19 +++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index 341ee4f1d91e..920426d691ce 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -124,8 +124,15 @@ static __always_inline void mwait_idle_with_hints(unsi= gned long eax, unsigned lo } =20 __monitor((void *)¤t_thread_info()->flags, 0, 0); - if (!need_resched()) - __mwait(eax, ecx); + + if (!need_resched()) { + if (ecx & 1) { + __mwait(eax, ecx); + } else { + __sti_mwait(eax, ecx); + raw_local_irq_disable(); + } + } } current_clr_polling(); } diff --git a/drivers/idle/intel_idle.c b/drivers/idle/intel_idle.c index dcda0afecfc5..3e01a6b23e75 100644 --- a/drivers/idle/intel_idle.c +++ b/drivers/idle/intel_idle.c @@ -131,11 +131,12 @@ static unsigned int mwait_substates __initdata; #define MWAIT2flg(eax) ((eax & 0xFF) << 24) =20 static __always_inline int __intel_idle(struct cpuidle_device *dev, - struct cpuidle_driver *drv, int index) + struct cpuidle_driver *drv, + int index, bool irqoff) { struct cpuidle_state *state =3D &drv->states[index]; unsigned long eax =3D flg2MWAIT(state->flags); - unsigned long ecx =3D 1; /* break on interrupt flag */ + unsigned long ecx =3D 1*irqoff; /* break on interrupt flag */ =20 mwait_idle_with_hints(eax, ecx); =20 @@ -159,19 +160,13 @@ static __always_inline int __intel_idle(struct cpuidl= e_device *dev, static __cpuidle int intel_idle(struct cpuidle_device *dev, struct cpuidle_driver *drv, int index) { - return __intel_idle(dev, drv, index); + return __intel_idle(dev, drv, index, true); } =20 static __cpuidle int intel_idle_irq(struct cpuidle_device *dev, struct cpuidle_driver *drv, int index) { - int ret; - - raw_local_irq_enable(); - ret =3D __intel_idle(dev, drv, index); - raw_local_irq_disable(); - - return ret; + return __intel_idle(dev, drv, index, false); } =20 static __cpuidle int intel_idle_ibrs(struct cpuidle_device *dev, @@ -184,7 +179,7 @@ static __cpuidle int intel_idle_ibrs(struct cpuidle_dev= ice *dev, if (smt_active) __update_spec_ctrl(0); =20 - ret =3D __intel_idle(dev, drv, index); + ret =3D __intel_idle(dev, drv, index, true); =20 if (smt_active) __update_spec_ctrl(spec_ctrl); @@ -196,7 +191,7 @@ static __cpuidle int intel_idle_xstate(struct cpuidle_d= evice *dev, struct cpuidle_driver *drv, int index) { fpu_idle_fpregs(); - return __intel_idle(dev, drv, index); + return __intel_idle(dev, drv, index, true); } =20 /** --=20 2.42.1 From nobody Tue Dec 30 12:49:31 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 12C57C072A2 for ; Wed, 15 Nov 2023 15:14:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344505AbjKOPOU (ORCPT ); Wed, 15 Nov 2023 10:14:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344461AbjKOPOI (ORCPT ); Wed, 15 Nov 2023 10:14:08 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3967919F for ; Wed, 15 Nov 2023 07:14:06 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D18EC433C9; Wed, 15 Nov 2023 15:14:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700061245; bh=FusWax7dBGiMSsQErHycJtFZnQRX1tPbGgeRj6wuVTc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bLxROmPDxBB6f9W3KC+aeY3ToBV7kGUt66qKwhArVFuIVl87ae+RbGrQ74Ga8mVV3 M/lVlVMjqtzBH4RnCGlTlUqbhkdg8LAH9b1rr2R4MrF8J7gRa9gaPT1YcUm1AE1WDZ IhOLJpdDo1RaSybJc+2cJVSJ937/aUnnkCBeizYwLgrHawuWVWsp7f6e/9PXdrrcNG XNJmPZSZTDb8ClGnMixOvjNO6bSNkvG70y/Nkn96YV5XhKWq3eldjYJjwTbN+fvwPK pDudPFcrP2qXQidfvj2nXoOzY1YrXC7jB9kNpGuq3N/GvjSZgFaqiGAnbImq42a3cJ vNxgJheaI2lAw== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Peter Zijlstra , x86@kernel.org, "Rafael J . Wysocki" Subject: [PATCH 3/4] x86: Remove __current_clr_polling() from mwait_idle() Date: Wed, 15 Nov 2023 10:13:24 -0500 Message-ID: <20231115151325.6262-4-frederic@kernel.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231115151325.6262-1-frederic@kernel.org> References: <20231115151325.6262-1-frederic@kernel.org> 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" mwait_idle() is only ever called through by cpuidle, either from default_idle_call() or from cpuidle_enter(). In any case cpuidle_idle_call() sets again TIF_NR_POLLING after calling it so there is no point for this atomic operation upon idle exit. Acked-by: Rafael J. Wysocki Signed-off-by: Frederic Weisbecker --- arch/x86/kernel/process.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index b6f4e8399fca..fc7a38084606 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -930,7 +930,6 @@ static __cpuidle void mwait_idle(void) raw_local_irq_disable(); } } - __current_clr_polling(); } =20 void select_idle_routine(const struct cpuinfo_x86 *c) --=20 2.42.1 From nobody Tue Dec 30 12:49:31 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 7EBD4C2BB3F for ; Wed, 15 Nov 2023 15:14:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344486AbjKOPOX (ORCPT ); Wed, 15 Nov 2023 10:14:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344472AbjKOPOL (ORCPT ); Wed, 15 Nov 2023 10:14:11 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9CAA1A7 for ; Wed, 15 Nov 2023 07:14:08 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41DAFC433C7; Wed, 15 Nov 2023 15:14:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700061248; bh=9FyYN/LMD2/fmw8fX0LrTB664VhgSDnzIJOwUJw0Y/0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s5m+7PM5vOqzBpNbL1+fn6Ycsl2BUOIivbf/wNwef9DrWYraapI6EjD0i1SiJvFMy 6TbCpVDi4b1Ihc+IpXrvrfcwYC5GveDUg7zyIv9CYD/e7bENZGW2mW6W2h7ibG4AT6 5JrpGoK8wnI64jYVgpKnlQcXkth5RCKe3bnztzcitkEoihyqPiN5MaSlRB4X5LQkjV vlIGQT6RpjUI1ouzn6pt89RalvhyDri6tqBPdFa25uSkP4gOZwr4vZxUGNogdnRnm0 suOyFAz8RHMJQA9Ct/cpWVLqqueumDGCWhU9BtTcV6cj/fSOHRsNR0gTBVKs4Y9RQx b5GfqpUBu5tUw== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Peter Zijlstra , x86@kernel.org, "Rafael J . Wysocki" Subject: [PATCH 4/4] x86: Remove the current_clr_polling() call upon mwait exit Date: Wed, 15 Nov 2023 10:13:25 -0500 Message-ID: <20231115151325.6262-5-frederic@kernel.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231115151325.6262-1-frederic@kernel.org> References: <20231115151325.6262-1-frederic@kernel.org> 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" mwait_idle_with_hints() is mostly called from cpuidle which already sets back TIF_NR_POLLING and fold TIF_NEED_RESCHED if necessary. The only non-cpuidle caller is power_saving_thread() which acts as an idle loop and is the only reason why mwait_idle_with_hints() carries a costly fully ordered atomic operation upon idle exit. Make this overhead proper to power_saving_thread() instead which already duplicates quite some cpuidle code. Acked-by: Rafael J. Wysocki Signed-off-by: Frederic Weisbecker --- arch/x86/include/asm/mwait.h | 1 - drivers/acpi/acpi_pad.c | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index 920426d691ce..c1be3775b94a 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -134,7 +134,6 @@ static __always_inline void mwait_idle_with_hints(unsig= ned long eax, unsigned lo } } } - current_clr_polling(); } =20 /* diff --git a/drivers/acpi/acpi_pad.c b/drivers/acpi/acpi_pad.c index bd1ad07f0290..86b57123713f 100644 --- a/drivers/acpi/acpi_pad.c +++ b/drivers/acpi/acpi_pad.c @@ -175,6 +175,7 @@ static int power_saving_thread(void *data) stop_critical_timings(); =20 mwait_idle_with_hints(power_saving_mwait_eax, 1); + current_clr_polling(); =20 start_critical_timings(); tick_broadcast_exit(); --=20 2.42.1