linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing.
@ 2025-09-18  6:49 shechenglong
  2025-09-18 11:28 ` Catalin Marinas
                   ` (4 more replies)
  0 siblings, 5 replies; 20+ messages in thread
From: shechenglong @ 2025-09-18  6:49 UTC (permalink / raw)
  To: catalin.marinas
  Cc: will, linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating, shechenglong

Context of the Issue:
In an ARM64 environment, the following steps were performed:

1. Repeatedly ran stress-ng to stress the CPU, memory, and I/O.
2. Cyclically executed test case pty06 from the LTP test suite.
3. Added mitigations=off to the GRUB parameters.

After 1–2 hours of stress testing, a hardlockup occurred,
causing a system crash.

Root Cause of the Hardlockup:
Each time stress-ng starts, it invokes the /sys/kernel/debug/clear_warn_once
interface, which clears the values in the memory section from __start_once
to __end_once. This caused functions like pr_info_once() — originally
designed to print only once — to print again every time stress-ng was called.
If the pty06 test case happened to be using the serial module at that same
moment, it would sleep in waiter.list within the __down_common function.

After pr_info_once() completed its output using the serial module,
it invoked the semaphore up() function to wake up the process waiting
in waiter.list. This sequence triggered an A-A deadlock, ultimately
leading to a hardlockup and system crash.

To prevent this, a local variable should be used to control and ensure
the print operation occurs only once.

Hard lockup call stack:

_raw_spin_lock_nested+168
ttwu_queue+180 (rq_lock(rq, &rf); 2nd acquiring the rq->__lock)
try_to_wake_up+548
wake_up_process+32
__up+88
up+100
__up_console_sem+96
console_unlock+696
vprintk_emit+428
vprintk_default+64
vprintk_func+220
printk+104
spectre_v4_enable_task_mitigation+344
__switch_to+100
__schedule+1028 (rq_lock(rq, &rf); 1st acquiring the rq->__lock)
schedule_idle+48
do_idle+388
cpu_startup_entry+44
secondary_start_kernel+352

Signed-off-by: shechenglong <shechenglong@xfusion.com>
---
 arch/arm64/kernel/proton-pack.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
index edf1783ffc81..f8663157e041 100644
--- a/arch/arm64/kernel/proton-pack.c
+++ b/arch/arm64/kernel/proton-pack.c
@@ -424,8 +424,10 @@ static bool spectre_v4_mitigations_off(void)
 	bool ret = cpu_mitigations_off() ||
 		   __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
 
-	if (ret)
-		pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
+	static atomic_t __printk_once = ATOMIC_INIT(0);
+
+	if (ret && !atomic_cmpxchg(&__printk_once, 0, 1))
+		pr_info("spectre-v4 mitigation disabled by command-line option\n");
 
 	return ret;
 }
-- 
2.33.0



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing.
  2025-09-18  6:49 [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing shechenglong
@ 2025-09-18 11:28 ` Catalin Marinas
  2025-09-19 12:05   ` 答复: " shechenglong
  2025-09-22 16:08   ` Mark Rutland
  2025-09-24 12:32 ` [PATCH] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 20+ messages in thread
From: Catalin Marinas @ 2025-09-18 11:28 UTC (permalink / raw)
  To: shechenglong
  Cc: will, linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating

On Thu, Sep 18, 2025 at 02:49:07PM +0800, shechenglong wrote:
> Context of the Issue:
> In an ARM64 environment, the following steps were performed:
> 
> 1. Repeatedly ran stress-ng to stress the CPU, memory, and I/O.
> 2. Cyclically executed test case pty06 from the LTP test suite.
> 3. Added mitigations=off to the GRUB parameters.
> 
> After 1–2 hours of stress testing, a hardlockup occurred,
> causing a system crash.
> 
> Root Cause of the Hardlockup:
> Each time stress-ng starts, it invokes the /sys/kernel/debug/clear_warn_once
> interface, which clears the values in the memory section from __start_once
> to __end_once. This caused functions like pr_info_once() — originally
> designed to print only once — to print again every time stress-ng was called.
> If the pty06 test case happened to be using the serial module at that same
> moment, it would sleep in waiter.list within the __down_common function.
> 
> After pr_info_once() completed its output using the serial module,
> it invoked the semaphore up() function to wake up the process waiting
> in waiter.list. This sequence triggered an A-A deadlock, ultimately
> leading to a hardlockup and system crash.
> 
> To prevent this, a local variable should be used to control and ensure
> the print operation occurs only once.
> 
> Hard lockup call stack:
> 
> _raw_spin_lock_nested+168
> ttwu_queue+180 (rq_lock(rq, &rf); 2nd acquiring the rq->__lock)
> try_to_wake_up+548
> wake_up_process+32
> __up+88
> up+100
> __up_console_sem+96
> console_unlock+696
> vprintk_emit+428
> vprintk_default+64
> vprintk_func+220
> printk+104
> spectre_v4_enable_task_mitigation+344
> __switch_to+100
> __schedule+1028 (rq_lock(rq, &rf); 1st acquiring the rq->__lock)
> schedule_idle+48
> do_idle+388
> cpu_startup_entry+44
> secondary_start_kernel+352

Is the problem actually that we call the spectre v4 stuff on the
switch_to() path (we can't change this) under the rq_lock() and it
subsequently calls printk() which takes the console semaphore? I think
the "once" aspect makes it less likely but does not address the actual
problem.

> diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
> index edf1783ffc81..f8663157e041 100644
> --- a/arch/arm64/kernel/proton-pack.c
> +++ b/arch/arm64/kernel/proton-pack.c
> @@ -424,8 +424,10 @@ static bool spectre_v4_mitigations_off(void)
>  	bool ret = cpu_mitigations_off() ||
>  		   __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
>  
> -	if (ret)
> -		pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
> +	static atomic_t __printk_once = ATOMIC_INIT(0);
> +
> +	if (ret && !atomic_cmpxchg(&__printk_once, 0, 1))
> +		pr_info("spectre-v4 mitigation disabled by command-line option\n");
>  
>  	return ret;
>  }

I think we should just avoid the printk() on the
spectre_v4_enable_task_mitigation() path. Well, I'd remove it altogether
from the spectre_v4_mitigations_off() as it's called on kernel entry as
well. Just add a different way to print the status during kernel boot if
there isn't one already, maybe an initcall.

-- 
Catalin


^ permalink raw reply	[flat|nested] 20+ messages in thread

* 答复: [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing.
  2025-09-18 11:28 ` Catalin Marinas
@ 2025-09-19 12:05   ` shechenglong
  2025-09-22 16:54     ` Catalin Marinas
  2025-09-22 16:08   ` Mark Rutland
  1 sibling, 1 reply; 20+ messages in thread
From: shechenglong @ 2025-09-19 12:05 UTC (permalink / raw)
  To: Catalin Marinas
  Cc: will@kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, xulei, chenjialong, yuxiating

Okay, understood. Thank you! May I ask when the fix/patch is expected to be available?

-----邮件原件-----
发件人: Catalin Marinas <catalin.marinas@arm.com> 
发送时间: 2025年9月18日 19:28
收件人: shechenglong <shechenglong@xfusion.com>
抄送: will@kernel.org; linux-arm-kernel@lists.infradead.org; linux-kernel@vger.kernel.org; xulei <stone.xulei@xfusion.com>; chenjialong <chenjialong@xfusion.com>; yuxiating <yuxiating@xfusion.com>
主题: Re: [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing.

On Thu, Sep 18, 2025 at 02:49:07PM +0800, shechenglong wrote:
> Context of the Issue:
> In an ARM64 environment, the following steps were performed:
> 
> 1. Repeatedly ran stress-ng to stress the CPU, memory, and I/O.
> 2. Cyclically executed test case pty06 from the LTP test suite.
> 3. Added mitigations=off to the GRUB parameters.
> 
> After 1–2 hours of stress testing, a hardlockup occurred, causing a 
> system crash.
> 
> Root Cause of the Hardlockup:
> Each time stress-ng starts, it invokes the 
> /sys/kernel/debug/clear_warn_once interface, which clears the values 
> in the memory section from __start_once to __end_once. This caused 
> functions like pr_info_once() — originally designed to print only once — to print again every time stress-ng was called.
> If the pty06 test case happened to be using the serial module at that 
> same moment, it would sleep in waiter.list within the __down_common function.
> 
> After pr_info_once() completed its output using the serial module, it 
> invoked the semaphore up() function to wake up the process waiting in 
> waiter.list. This sequence triggered an A-A deadlock, ultimately 
> leading to a hardlockup and system crash.
> 
> To prevent this, a local variable should be used to control and ensure 
> the print operation occurs only once.
> 
> Hard lockup call stack:
> 
> _raw_spin_lock_nested+168
> ttwu_queue+180 (rq_lock(rq, &rf); 2nd acquiring the rq->__lock)
> try_to_wake_up+548
> wake_up_process+32
> __up+88
> up+100
> __up_console_sem+96
> console_unlock+696
> vprintk_emit+428
> vprintk_default+64
> vprintk_func+220
> printk+104
> spectre_v4_enable_task_mitigation+344
> __switch_to+100
> __schedule+1028 (rq_lock(rq, &rf); 1st acquiring the rq->__lock)
> schedule_idle+48
> do_idle+388
> cpu_startup_entry+44
> secondary_start_kernel+352

Is the problem actually that we call the spectre v4 stuff on the
switch_to() path (we can't change this) under the rq_lock() and it subsequently calls printk() which takes the console semaphore? I think the "once" aspect makes it less likely but does not address the actual problem.

> diff --git a/arch/arm64/kernel/proton-pack.c 
> b/arch/arm64/kernel/proton-pack.c index edf1783ffc81..f8663157e041 
> 100644
> --- a/arch/arm64/kernel/proton-pack.c
> +++ b/arch/arm64/kernel/proton-pack.c
> @@ -424,8 +424,10 @@ static bool spectre_v4_mitigations_off(void)
>  	bool ret = cpu_mitigations_off() ||
>  		   __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
>  
> -	if (ret)
> -		pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
> +	static atomic_t __printk_once = ATOMIC_INIT(0);
> +
> +	if (ret && !atomic_cmpxchg(&__printk_once, 0, 1))
> +		pr_info("spectre-v4 mitigation disabled by command-line option\n");
>  
>  	return ret;
>  }

I think we should just avoid the printk() on the
spectre_v4_enable_task_mitigation() path. Well, I'd remove it altogether from the spectre_v4_mitigations_off() as it's called on kernel entry as well. Just add a different way to print the status during kernel boot if there isn't one already, maybe an initcall.

--
Catalin

^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing.
  2025-09-18 11:28 ` Catalin Marinas
  2025-09-19 12:05   ` 答复: " shechenglong
@ 2025-09-22 16:08   ` Mark Rutland
  1 sibling, 0 replies; 20+ messages in thread
From: Mark Rutland @ 2025-09-22 16:08 UTC (permalink / raw)
  To: Catalin Marinas
  Cc: shechenglong, will, linux-arm-kernel, linux-kernel, stone.xulei,
	chenjialong, yuxiating

On Thu, Sep 18, 2025 at 12:28:05PM +0100, Catalin Marinas wrote:
> On Thu, Sep 18, 2025 at 02:49:07PM +0800, shechenglong wrote:
> > Context of the Issue:
> > In an ARM64 environment, the following steps were performed:
> > 
> > 1. Repeatedly ran stress-ng to stress the CPU, memory, and I/O.
> > 2. Cyclically executed test case pty06 from the LTP test suite.
> > 3. Added mitigations=off to the GRUB parameters.
> > 
> > After 1–2 hours of stress testing, a hardlockup occurred,
> > causing a system crash.
> > 
> > Root Cause of the Hardlockup:
> > Each time stress-ng starts, it invokes the /sys/kernel/debug/clear_warn_once
> > interface, which clears the values in the memory section from __start_once
> > to __end_once. This caused functions like pr_info_once() — originally
> > designed to print only once — to print again every time stress-ng was called.
> > If the pty06 test case happened to be using the serial module at that same
> > moment, it would sleep in waiter.list within the __down_common function.
> > 
> > After pr_info_once() completed its output using the serial module,
> > it invoked the semaphore up() function to wake up the process waiting
> > in waiter.list. This sequence triggered an A-A deadlock, ultimately
> > leading to a hardlockup and system crash.
> > 
> > To prevent this, a local variable should be used to control and ensure
> > the print operation occurs only once.
> > 
> > Hard lockup call stack:
> > 
> > _raw_spin_lock_nested+168
> > ttwu_queue+180 (rq_lock(rq, &rf); 2nd acquiring the rq->__lock)
> > try_to_wake_up+548
> > wake_up_process+32
> > __up+88
> > up+100
> > __up_console_sem+96
> > console_unlock+696
> > vprintk_emit+428
> > vprintk_default+64
> > vprintk_func+220
> > printk+104
> > spectre_v4_enable_task_mitigation+344
> > __switch_to+100
> > __schedule+1028 (rq_lock(rq, &rf); 1st acquiring the rq->__lock)
> > schedule_idle+48
> > do_idle+388
> > cpu_startup_entry+44
> > secondary_start_kernel+352
> 
> Is the problem actually that we call the spectre v4 stuff on the
> switch_to() path (we can't change this) under the rq_lock() and it
> subsequently calls printk() which takes the console semaphore? I think
> the "once" aspect makes it less likely but does not address the actual
> problem.

Agreed; I think what we do here is structurally wrong, even if (in the
asbence of writes to the 'clear_warn_once' file) this happens to largely
do what we want today.

We really shouldn't print in accessors for kernel state.

> > diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
> > index edf1783ffc81..f8663157e041 100644
> > --- a/arch/arm64/kernel/proton-pack.c
> > +++ b/arch/arm64/kernel/proton-pack.c
> > @@ -424,8 +424,10 @@ static bool spectre_v4_mitigations_off(void)
> >  	bool ret = cpu_mitigations_off() ||
> >  		   __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
> >  
> > -	if (ret)
> > -		pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
> > +	static atomic_t __printk_once = ATOMIC_INIT(0);
> > +
> > +	if (ret && !atomic_cmpxchg(&__printk_once, 0, 1))
> > +		pr_info("spectre-v4 mitigation disabled by command-line option\n");
> >  
> >  	return ret;
> >  }
> 
> I think we should just avoid the printk() on the
> spectre_v4_enable_task_mitigation() path. Well, I'd remove it altogether
> from the spectre_v4_mitigations_off() as it's called on kernel entry as
> well. Just add a different way to print the status during kernel boot if
> there isn't one already, maybe an initcall.

I agree; I think we want to rip that out of spectre_v2_mitigations_off()
too.

We print a bunch of things under setup_system_capabilities(), so hanging
something off that feels like the right thing to do.

Mark.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: 答复: [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing.
  2025-09-19 12:05   ` 答复: " shechenglong
@ 2025-09-22 16:54     ` Catalin Marinas
  0 siblings, 0 replies; 20+ messages in thread
From: Catalin Marinas @ 2025-09-22 16:54 UTC (permalink / raw)
  To: shechenglong
  Cc: will@kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, xulei, chenjialong, yuxiating

On Fri, Sep 19, 2025 at 12:05:38PM +0000, shechenglong wrote:
> Okay, understood. Thank you! May I ask when the fix/patch is expected
> to be available?

If you send one, that could be really soon ;). See Mark's suggestions
for where to add the pr_info().

-- 
Catalin


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH] cpu: fix hard lockup triggered by printk calls within scheduling context
  2025-09-18  6:49 [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing shechenglong
  2025-09-18 11:28 ` Catalin Marinas
@ 2025-09-24 12:32 ` shechenglong
  2025-09-25 13:48   ` Catalin Marinas
  2025-10-03 14:23   ` Will Deacon
  2025-10-20 14:51 ` [PATCH v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 20+ messages in thread
From: shechenglong @ 2025-09-24 12:32 UTC (permalink / raw)
  To: catalin.marinas
  Cc: will, linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating, shechenglong, Mark Rutland

relocate the printk() calls from spectre_v4_mitigations_off() and
spectre_v2_mitigations_off() into setup_system_capabilities() function,
preventing hard lockups that occur when printk() is invoked from scheduler context.

Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
Suggested-by: Mark Rutland <mark.rutland@arm.com>
Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: shechenglong <shechenglong@xfusion.com>
---
 arch/arm64/include/asm/spectre.h |  3 +++
 arch/arm64/kernel/cpufeature.c   |  9 +++++++++
 arch/arm64/kernel/proton-pack.c  | 18 ++++--------------
 3 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/arch/arm64/include/asm/spectre.h b/arch/arm64/include/asm/spectre.h
index 8fef12626090..6fe29df41788 100644
--- a/arch/arm64/include/asm/spectre.h
+++ b/arch/arm64/include/asm/spectre.h
@@ -118,5 +118,8 @@ void spectre_bhb_patch_wa3(struct alt_instr *alt,
 void spectre_bhb_patch_clearbhb(struct alt_instr *alt,
 				__le32 *origptr, __le32 *updptr, int nr_inst);
 
+bool spectre_v2_mitigations_off(void);
+bool spectre_v4_mitigations_off(void);
+
 #endif	/* __ASSEMBLY__ */
 #endif	/* __ASM_SPECTRE_H */
diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
index ef269a5a37e1..7d1f541e66a0 100644
--- a/arch/arm64/kernel/cpufeature.c
+++ b/arch/arm64/kernel/cpufeature.c
@@ -94,6 +94,7 @@
 #include <asm/traps.h>
 #include <asm/vectors.h>
 #include <asm/virt.h>
+#include <asm/spectre.h>
 
 /* Kernel representation of AT_HWCAP and AT_HWCAP2 */
 static DECLARE_BITMAP(elf_hwcap, MAX_CPU_FEATURES) __read_mostly;
@@ -3942,6 +3943,14 @@ static void __init setup_system_capabilities(void)
 	 */
 	if (system_uses_ttbr0_pan())
 		pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
+
+	/*
+	 * Report Spectre mitigations status.
+	 */
+	if (spectre_v2_mitigations_off())
+		pr_info("spectre-v2 mitigation disabled by command line option\n");
+	if (spectre_v4_mitigations_off())
+		pr_info("spectre-v4 mitigation disabled by command-line option\n");
 }
 
 void __init setup_system_features(void)
diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
index edf1783ffc81..0d4a8a123e07 100644
--- a/arch/arm64/kernel/proton-pack.c
+++ b/arch/arm64/kernel/proton-pack.c
@@ -89,14 +89,9 @@ static int __init parse_spectre_v2_param(char *str)
 }
 early_param("nospectre_v2", parse_spectre_v2_param);
 
-static bool spectre_v2_mitigations_off(void)
+bool spectre_v2_mitigations_off(void)
 {
-	bool ret = __nospectre_v2 || cpu_mitigations_off();
-
-	if (ret)
-		pr_info_once("spectre-v2 mitigation disabled by command line option\n");
-
-	return ret;
+	return __nospectre_v2 || cpu_mitigations_off();
 }
 
 static const char *get_bhb_affected_string(enum mitigation_state bhb_state)
@@ -419,15 +414,10 @@ early_param("ssbd", parse_spectre_v4_param);
  * with contradictory parameters. The mitigation is always either "off",
  * "dynamic" or "on".
  */
-static bool spectre_v4_mitigations_off(void)
+bool spectre_v4_mitigations_off(void)
 {
-	bool ret = cpu_mitigations_off() ||
+	return cpu_mitigations_off() ||
 		   __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
-
-	if (ret)
-		pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
-
-	return ret;
 }
 
 /* Do we need to toggle the mitigation state on entry to/exit from the kernel? */
-- 
2.33.0



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH] cpu: fix hard lockup triggered by printk calls within scheduling context
  2025-09-24 12:32 ` [PATCH] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
@ 2025-09-25 13:48   ` Catalin Marinas
  2025-10-03 14:23   ` Will Deacon
  1 sibling, 0 replies; 20+ messages in thread
From: Catalin Marinas @ 2025-09-25 13:48 UTC (permalink / raw)
  To: shechenglong
  Cc: will, linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating, Mark Rutland

On Wed, Sep 24, 2025 at 08:32:47PM +0800, shechenglong wrote:
> relocate the printk() calls from spectre_v4_mitigations_off() and
> spectre_v2_mitigations_off() into setup_system_capabilities() function,
> preventing hard lockups that occur when printk() is invoked from scheduler context.
> 
> Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
> Suggested-by: Mark Rutland <mark.rutland@arm.com>
> Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
> Signed-off-by: shechenglong <shechenglong@xfusion.com>

It looks fine to me. Thanks.

Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>

Will, do you want to take this for 6.18? It might be worth a cc stable.
In general it's unlikely to happen unless you keep writing to
/sys/kernel/debug/clear_warn_once.

-- 
Catalin


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH] cpu: fix hard lockup triggered by printk calls within scheduling context
  2025-09-24 12:32 ` [PATCH] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
  2025-09-25 13:48   ` Catalin Marinas
@ 2025-10-03 14:23   ` Will Deacon
  1 sibling, 0 replies; 20+ messages in thread
From: Will Deacon @ 2025-10-03 14:23 UTC (permalink / raw)
  To: shechenglong
  Cc: catalin.marinas, linux-arm-kernel, linux-kernel, stone.xulei,
	chenjialong, yuxiating, Mark Rutland

On Wed, Sep 24, 2025 at 08:32:47PM +0800, shechenglong wrote:
> relocate the printk() calls from spectre_v4_mitigations_off() and
> spectre_v2_mitigations_off() into setup_system_capabilities() function,
> preventing hard lockups that occur when printk() is invoked from scheduler context.
> 
> Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
> Suggested-by: Mark Rutland <mark.rutland@arm.com>
> Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
> Signed-off-by: shechenglong <shechenglong@xfusion.com>
> ---
>  arch/arm64/include/asm/spectre.h |  3 +++
>  arch/arm64/kernel/cpufeature.c   |  9 +++++++++
>  arch/arm64/kernel/proton-pack.c  | 18 ++++--------------
>  3 files changed, 16 insertions(+), 14 deletions(-)

Thanks for posting the patch!

> diff --git a/arch/arm64/include/asm/spectre.h b/arch/arm64/include/asm/spectre.h
> index 8fef12626090..6fe29df41788 100644
> --- a/arch/arm64/include/asm/spectre.h
> +++ b/arch/arm64/include/asm/spectre.h
> @@ -118,5 +118,8 @@ void spectre_bhb_patch_wa3(struct alt_instr *alt,
>  void spectre_bhb_patch_clearbhb(struct alt_instr *alt,
>  				__le32 *origptr, __le32 *updptr, int nr_inst);
>  
> +bool spectre_v2_mitigations_off(void);
> +bool spectre_v4_mitigations_off(void);
> +
>  #endif	/* __ASSEMBLY__ */
>  #endif	/* __ASM_SPECTRE_H */
> diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
> index ef269a5a37e1..7d1f541e66a0 100644
> --- a/arch/arm64/kernel/cpufeature.c
> +++ b/arch/arm64/kernel/cpufeature.c
> @@ -94,6 +94,7 @@
>  #include <asm/traps.h>
>  #include <asm/vectors.h>
>  #include <asm/virt.h>
> +#include <asm/spectre.h>
>  
>  /* Kernel representation of AT_HWCAP and AT_HWCAP2 */
>  static DECLARE_BITMAP(elf_hwcap, MAX_CPU_FEATURES) __read_mostly;
> @@ -3942,6 +3943,14 @@ static void __init setup_system_capabilities(void)
>  	 */
>  	if (system_uses_ttbr0_pan())
>  		pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
> +
> +	/*
> +	 * Report Spectre mitigations status.
> +	 */
> +	if (spectre_v2_mitigations_off())
> +		pr_info("spectre-v2 mitigation disabled by command line option\n");

nit: Let's fix the message here for consistency and use "command-line".

> +	if (spectre_v4_mitigations_off())
> +		pr_info("spectre-v4 mitigation disabled by command-line option\n");

I think it would be cleaner to have a new function in proton-pack.c, say
"spectre_print_disabled_mitigations()" which can then print the messages
for spectre v2, v4 and also spectre-bhb (currently done in
spectre_bhb_enable_mitigation()).

While you're at it, spectre-bhb is weird because it also prints a message
when the Kconfig is disabled. We should actually just get rid of that
Kconfig option altogether (in a separate patch) like we did for the other
spectre mitigations.

What do you think?

Cheers,

Will


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages
  2025-09-18  6:49 [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing shechenglong
  2025-09-18 11:28 ` Catalin Marinas
  2025-09-24 12:32 ` [PATCH] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
@ 2025-10-20 14:51 ` shechenglong
  2025-10-20 14:51   ` [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
  2025-10-20 14:51   ` [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
  2025-10-29  3:45 ` [RESEND v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
  2025-10-31  9:15 ` [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
  4 siblings, 2 replies; 20+ messages in thread
From: shechenglong @ 2025-10-20 14:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: mark.rutland, catalin.marinas, will, linux-kernel, stone.xulei,
	chenjialong, yuxiating

This v2 series addresses your comments: 
- Fixed the message to use "command-line" consistently 
- Created spectre_print_disabled_mitigations() function to handle all spectre mitigation messages 
- Added a separate patch to remove the CONFIG_MITIGATE_SPECTRE_BHB option

The series includes two patches:

Patch 1: "fix hard lockup triggered by printk calls within scheduling context" 
- Moves printk calls from scheduler context to setup_system_capabilities() 
- Prevents hard lockups by avoiding printk in unsafe contexts 
- Consolidates spectre mitigation status reporting

Patch 2: "Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled" 
- Removes the obsolete CONFIG_MITIGATE_SPECTRE_BHB Kconfig option 
- Cleans up the spectre mitigation code as suggested

shechenglong (2):
  arm64: spectre: fix hard lockup triggered by printk calls within scheduling context
  arm64: spectre: Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled

arch/arm64/include/asm/spectre.h |  2 ++
arch/arm64/kernel/cpufeature.c   |  7 ++++++-
arch/arm64/kernel/proton-pack.c  | 28 ++++++++++++++--------------
3 files changed, 22 insertions(+), 17 deletions(-)
In-Reply-To: 20250918064907.1832-1-shechenglong@xfusion.com




^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled.
  2025-10-20 14:51 ` [PATCH v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
@ 2025-10-20 14:51   ` shechenglong
  2025-10-20 14:51   ` [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
  1 sibling, 0 replies; 20+ messages in thread
From: shechenglong @ 2025-10-20 14:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: mark.rutland, catalin.marinas, will, linux-kernel, stone.xulei,
	chenjialong, yuxiating, shechenglong

Following the pattern established with other Spectre mitigations,
do not prints a message when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
Kconfig option is disabled.

Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
Suggested-by: Will Deacon <will@kernel.org>
Signed-off-by: shechenglong <shechenglong@xfusion.com>
---
 arch/arm64/kernel/proton-pack.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
index f9a32dfde006..d833b7c1bba8 100644
--- a/arch/arm64/kernel/proton-pack.c
+++ b/arch/arm64/kernel/proton-pack.c
@@ -1042,8 +1042,6 @@ void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)

        if (arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) {
                /* No point mitigating Spectre-BHB alone. */
-       } else if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY)) {
-               pr_info_once("spectre-bhb mitigation disabled by compile time option\n");
        } else if (cpu_mitigations_off() || __nospectre_bhb) {
                pr_info_once("spectre-bhb mitigation disabled by command line option\n");
        } else if (supports_ecbhb(SCOPE_LOCAL_CPU)) {
--
2.33.0


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context
  2025-10-20 14:51 ` [PATCH v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
  2025-10-20 14:51   ` [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
@ 2025-10-20 14:51   ` shechenglong
  1 sibling, 0 replies; 20+ messages in thread
From: shechenglong @ 2025-10-20 14:51 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: mark.rutland, catalin.marinas, will, linux-kernel, stone.xulei,
	chenjialong, yuxiating, shechenglong

relocate the printk() calls from spectre_v4_mitigations_off() and
spectre_v2_mitigations_off() into setup_system_capabilities() function,
preventing hard lockups that occur when printk() is invoked from scheduler context.

Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
Suggested-by: Mark Rutland <mark.rutland@arm.com>
Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
Suggested-by: Will Deacon <will@kernel.org>
Signed-off-by: shechenglong <shechenglong@xfusion.com>
---
 arch/arm64/include/asm/spectre.h |  2 ++
 arch/arm64/kernel/cpufeature.c   |  7 ++++++-
 arch/arm64/kernel/proton-pack.c  | 28 ++++++++++++++--------------
 3 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/arch/arm64/include/asm/spectre.h b/arch/arm64/include/asm/spectre.h
index 8fef12626090..f244b52fb123 100644
--- a/arch/arm64/include/asm/spectre.h
+++ b/arch/arm64/include/asm/spectre.h
@@ -118,5 +118,7 @@ void spectre_bhb_patch_wa3(struct alt_instr *alt,
 void spectre_bhb_patch_clearbhb(struct alt_instr *alt,
                                __le32 *origptr, __le32 *updptr, int nr_inst);

+void spectre_print_disabled_mitigations(void);
+
 #endif /* __ASSEMBLY__ */
 #endif /* __ASM_SPECTRE_H */
diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
index 5ed401ff79e3..6836e314fd30 100644
--- a/arch/arm64/kernel/cpufeature.c
+++ b/arch/arm64/kernel/cpufeature.c
@@ -94,7 +94,7 @@
 #include <asm/traps.h>
 #include <asm/vectors.h>
 #include <asm/virt.h>
-
+#include <asm/spectre.h>
 /* Kernel representation of AT_HWCAP and AT_HWCAP2 */
 static DECLARE_BITMAP(elf_hwcap, MAX_CPU_FEATURES) __read_mostly;

@@ -3875,6 +3875,11 @@ static void __init setup_system_capabilities(void)
         */
        if (system_uses_ttbr0_pan())
                pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
+
+       /*
+        * Report Spectre mitigations status.
+        */
+       spectre_print_disabled_mitigations();
 }

 void __init setup_system_features(void)
diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
index d833b7c1bba8..386f986e6b5d 100644
--- a/arch/arm64/kernel/proton-pack.c
+++ b/arch/arm64/kernel/proton-pack.c
@@ -91,12 +91,7 @@ early_param("nospectre_v2", parse_spectre_v2_param);

 static bool spectre_v2_mitigations_off(void)
 {
-       bool ret = __nospectre_v2 || cpu_mitigations_off();
-
-       if (ret)
-               pr_info_once("spectre-v2 mitigation disabled by command line option\n");
-
-       return ret;
+       return __nospectre_v2 || cpu_mitigations_off();
 }

 static const char *get_bhb_affected_string(enum mitigation_state bhb_state)
@@ -421,13 +416,8 @@ early_param("ssbd", parse_spectre_v4_param);
  */
 static bool spectre_v4_mitigations_off(void)
 {
-       bool ret = cpu_mitigations_off() ||
+       return cpu_mitigations_off() ||
                   __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
-
-       if (ret)
-               pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
-
-       return ret;
 }

 /* Do we need to toggle the mitigation state on entry to/exit from the kernel? */
@@ -1042,8 +1032,6 @@ void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)

        if (arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) {
                /* No point mitigating Spectre-BHB alone. */
-       } else if (cpu_mitigations_off() || __nospectre_bhb) {
-               pr_info_once("spectre-bhb mitigation disabled by command line option\n");
        } else if (supports_ecbhb(SCOPE_LOCAL_CPU)) {
                state = SPECTRE_MITIGATED;
                set_bit(BHB_HW, &system_bhb_mitigations);
@@ -1197,3 +1185,15 @@ void unpriv_ebpf_notify(int new_state)
                pr_err("WARNING: %s", EBPF_WARN);
 }
 #endif
+
+void spectre_print_disabled_mitigations(void)
+{
+       if (spectre_v2_mitigations_off())
+               pr_info("spectre-v2 mitigation disabled by command-line option\n");
+
+       if (spectre_v4_mitigations_off())
+               pr_info("spectre-v4 mitigation disabled by command-line option\n");
+
+       if (__nospectre_bhb || cpu_mitigations_off())
+               pr_info("spectre-bhb mitigation disabled by command-line option\n");
+}
--
2.33.0


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [RESEND v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages
  2025-09-18  6:49 [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing shechenglong
                   ` (2 preceding siblings ...)
  2025-10-20 14:51 ` [PATCH v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
@ 2025-10-29  3:45 ` shechenglong
  2025-10-29  3:45   ` [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
  2025-10-29  3:45   ` [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
  2025-10-31  9:15 ` [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
  4 siblings, 2 replies; 20+ messages in thread
From: shechenglong @ 2025-10-29  3:45 UTC (permalink / raw)
  To: mark.rutland, catalin.marinas, will
  Cc: linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating

On Wed, Sep 24, 2025 at 08:32:47PM +0800, shechenglong wrote:
> relocate the printk() calls from spectre_v4_mitigations_off() and
> spectre_v2_mitigations_off() into setup_system_capabilities() 
> function, preventing hard lockups that occur when printk() is invoked from scheduler context.
> 
> Link: 
> https://patchwork.kernel.org/project/linux-arm-kernel/patch/2025091806
> 4907.1832-1-shechenglong@xfusion.com/
> Suggested-by: Mark Rutland <mark.rutland@arm.com>
> Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
> Signed-off-by: shechenglong <shechenglong@xfusion.com>

Thanks for the review and suggestions, Will!

This v2 series addresses your comments:
- Fixed the message to use "command-line" consistently
- Created spectre_print_disabled_mitigations() function to handle all spectre mitigation messages
- Added a separate patch to remove the CONFIG_MITIGATE_SPECTRE_BHB option

The series includes two patches:

Patch 1: "fix hard lockup triggered by printk calls within scheduling context"
- Moves printk calls from scheduler context to setup_system_capabilities()
- Prevents hard lockups by avoiding printk in unsafe contexts
- Consolidates spectre mitigation status reporting

Patch 2: "Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled"
- Removes the obsolete CONFIG_MITIGATE_SPECTRE_BHB Kconfig option
- Cleans up the spectre mitigation code as suggested

shechenglong (2):
  arm64: spectre: fix hard lockup triggered by printk calls within scheduling context
  arm64: spectre: Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled

 arch/arm64/include/asm/spectre.h |  2 ++
 arch/arm64/kernel/cpufeature.c   |  7 ++++++-
 arch/arm64/kernel/proton-pack.c  | 28 ++++++++++++++--------------
 3 files changed, 22 insertions(+), 17 deletions(-)

-- 
2.25.1

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled.
  2025-10-29  3:45 ` [RESEND v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
@ 2025-10-29  3:45   ` shechenglong
  2025-10-30 14:48     ` Will Deacon
  2025-10-29  3:45   ` [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
  1 sibling, 1 reply; 20+ messages in thread
From: shechenglong @ 2025-10-29  3:45 UTC (permalink / raw)
  To: mark.rutland, catalin.marinas, will
  Cc: linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating, shechenglong

Following the pattern established with other Spectre mitigations,
do not prints a message when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
Kconfig option is disabled.

Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
Suggested-by: Will Deacon <will@kernel.org>
Signed-off-by: shechenglong <shechenglong@xfusion.com>
---
 arch/arm64/kernel/proton-pack.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
index f9a32dfde006..d833b7c1bba8 100644
--- a/arch/arm64/kernel/proton-pack.c
+++ b/arch/arm64/kernel/proton-pack.c
@@ -1042,8 +1042,6 @@ void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)

        if (arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) {
                /* No point mitigating Spectre-BHB alone. */
-       } else if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY)) {
-               pr_info_once("spectre-bhb mitigation disabled by compile time option\n");
        } else if (cpu_mitigations_off() || __nospectre_bhb) {
                pr_info_once("spectre-bhb mitigation disabled by command line option\n");
        } else if (supports_ecbhb(SCOPE_LOCAL_CPU)) {
--
2.33.0


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context
  2025-10-29  3:45 ` [RESEND v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
  2025-10-29  3:45   ` [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
@ 2025-10-29  3:45   ` shechenglong
  2025-10-30 14:50     ` Will Deacon
  1 sibling, 1 reply; 20+ messages in thread
From: shechenglong @ 2025-10-29  3:45 UTC (permalink / raw)
  To: mark.rutland, catalin.marinas, will
  Cc: linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating, shechenglong

relocate the printk() calls from spectre_v4_mitigations_off() and
spectre_v2_mitigations_off() into setup_system_capabilities() function,
preventing hard lockups that occur when printk() is invoked from scheduler context.

Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
Suggested-by: Mark Rutland <mark.rutland@arm.com>
Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
Suggested-by: Will Deacon <will@kernel.org>
Signed-off-by: shechenglong <shechenglong@xfusion.com>
---
 arch/arm64/include/asm/spectre.h |  2 ++
 arch/arm64/kernel/cpufeature.c   |  7 ++++++-
 arch/arm64/kernel/proton-pack.c  | 28 ++++++++++++++--------------
 3 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/arch/arm64/include/asm/spectre.h b/arch/arm64/include/asm/spectre.h
index 8fef12626090..f244b52fb123 100644
--- a/arch/arm64/include/asm/spectre.h
+++ b/arch/arm64/include/asm/spectre.h
@@ -118,5 +118,7 @@ void spectre_bhb_patch_wa3(struct alt_instr *alt,
 void spectre_bhb_patch_clearbhb(struct alt_instr *alt,
                                __le32 *origptr, __le32 *updptr, int nr_inst);

+void spectre_print_disabled_mitigations(void);
+
 #endif /* __ASSEMBLY__ */
 #endif /* __ASM_SPECTRE_H */
diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
index 5ed401ff79e3..6836e314fd30 100644
--- a/arch/arm64/kernel/cpufeature.c
+++ b/arch/arm64/kernel/cpufeature.c
@@ -94,7 +94,7 @@
 #include <asm/traps.h>
 #include <asm/vectors.h>
 #include <asm/virt.h>
-
+#include <asm/spectre.h>
 /* Kernel representation of AT_HWCAP and AT_HWCAP2 */
 static DECLARE_BITMAP(elf_hwcap, MAX_CPU_FEATURES) __read_mostly;

@@ -3875,6 +3875,11 @@ static void __init setup_system_capabilities(void)
         */
        if (system_uses_ttbr0_pan())
                pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
+
+       /*
+        * Report Spectre mitigations status.
+        */
+       spectre_print_disabled_mitigations();
 }

 void __init setup_system_features(void)
diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
index d833b7c1bba8..386f986e6b5d 100644
--- a/arch/arm64/kernel/proton-pack.c
+++ b/arch/arm64/kernel/proton-pack.c
@@ -91,12 +91,7 @@ early_param("nospectre_v2", parse_spectre_v2_param);

 static bool spectre_v2_mitigations_off(void)
 {
-       bool ret = __nospectre_v2 || cpu_mitigations_off();
-
-       if (ret)
-               pr_info_once("spectre-v2 mitigation disabled by command line option\n");
-
-       return ret;
+       return __nospectre_v2 || cpu_mitigations_off();
 }

 static const char *get_bhb_affected_string(enum mitigation_state bhb_state)
@@ -421,13 +416,8 @@ early_param("ssbd", parse_spectre_v4_param);
  */
 static bool spectre_v4_mitigations_off(void)
 {
-       bool ret = cpu_mitigations_off() ||
+       return cpu_mitigations_off() ||
                   __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
-
-       if (ret)
-               pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
-
-       return ret;
 }

 /* Do we need to toggle the mitigation state on entry to/exit from the kernel? */
@@ -1042,8 +1032,6 @@ void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)

        if (arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) {
                /* No point mitigating Spectre-BHB alone. */
-       } else if (cpu_mitigations_off() || __nospectre_bhb) {
-               pr_info_once("spectre-bhb mitigation disabled by command line option\n");
        } else if (supports_ecbhb(SCOPE_LOCAL_CPU)) {
                state = SPECTRE_MITIGATED;
                set_bit(BHB_HW, &system_bhb_mitigations);
@@ -1197,3 +1185,15 @@ void unpriv_ebpf_notify(int new_state)
                pr_err("WARNING: %s", EBPF_WARN);
 }
 #endif
+
+void spectre_print_disabled_mitigations(void)
+{
+       if (spectre_v2_mitigations_off())
+               pr_info("spectre-v2 mitigation disabled by command-line option\n");
+
+       if (spectre_v4_mitigations_off())
+               pr_info("spectre-v4 mitigation disabled by command-line option\n");
+
+       if (__nospectre_bhb || cpu_mitigations_off())
+               pr_info("spectre-bhb mitigation disabled by command-line option\n");
+}
--
2.33.0


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled.
  2025-10-29  3:45   ` [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
@ 2025-10-30 14:48     ` Will Deacon
  0 siblings, 0 replies; 20+ messages in thread
From: Will Deacon @ 2025-10-30 14:48 UTC (permalink / raw)
  To: shechenglong
  Cc: mark.rutland, catalin.marinas, linux-arm-kernel, linux-kernel,
	stone.xulei, chenjialong, yuxiating

On Wed, Oct 29, 2025 at 11:45:53AM +0800, shechenglong wrote:
> Following the pattern established with other Spectre mitigations,
> do not prints a message when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
> Kconfig option is disabled.
> 
> Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
> Suggested-by: Will Deacon <will@kernel.org>
> Signed-off-by: shechenglong <shechenglong@xfusion.com>
> ---
>  arch/arm64/kernel/proton-pack.c | 2 --
>  1 file changed, 2 deletions(-)
> 
> diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
> index f9a32dfde006..d833b7c1bba8 100644
> --- a/arch/arm64/kernel/proton-pack.c
> +++ b/arch/arm64/kernel/proton-pack.c
> @@ -1042,8 +1042,6 @@ void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)
> 
>         if (arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) {
>                 /* No point mitigating Spectre-BHB alone. */
> -       } else if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY)) {
> -               pr_info_once("spectre-bhb mitigation disabled by compile time option\n");
>         } else if (cpu_mitigations_off() || __nospectre_bhb) {
>                 pr_info_once("spectre-bhb mitigation disabled by command line option\n");
>         } else if (supports_ecbhb(SCOPE_LOCAL_CPU)) {

I'm not able to apply this -- I think you've somehow converted some tabs
to spaces in the whitespace.

Please can you fix that and send a v3?

Will


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context
  2025-10-29  3:45   ` [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
@ 2025-10-30 14:50     ` Will Deacon
  0 siblings, 0 replies; 20+ messages in thread
From: Will Deacon @ 2025-10-30 14:50 UTC (permalink / raw)
  To: shechenglong
  Cc: mark.rutland, catalin.marinas, linux-arm-kernel, linux-kernel,
	stone.xulei, chenjialong, yuxiating

On Wed, Oct 29, 2025 at 11:45:54AM +0800, shechenglong wrote:
> @@ -1197,3 +1185,15 @@ void unpriv_ebpf_notify(int new_state)
>                 pr_err("WARNING: %s", EBPF_WARN);
>  }
>  #endif
> +
> +void spectre_print_disabled_mitigations(void)
> +{
> +       if (spectre_v2_mitigations_off())
> +               pr_info("spectre-v2 mitigation disabled by command-line option\n");
> +
> +       if (spectre_v4_mitigations_off())
> +               pr_info("spectre-v4 mitigation disabled by command-line option\n");
> +
> +       if (__nospectre_bhb || cpu_mitigations_off())
> +               pr_info("spectre-bhb mitigation disabled by command-line option\n");

Is the compiler smart enough to store a single string for the "mitigation
disabled by command-line option\n" part? If not, you might want to use %s
to avoid wasting memory. (I was going to check with llvm but I'm unable
to apply your changes due to whitespace corruption).

Will


^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages
  2025-09-18  6:49 [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing shechenglong
                   ` (3 preceding siblings ...)
  2025-10-29  3:45 ` [RESEND v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
@ 2025-10-31  9:15 ` shechenglong
  2025-10-31  9:15   ` [PATCH v3 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
                     ` (2 more replies)
  4 siblings, 3 replies; 20+ messages in thread
From: shechenglong @ 2025-10-31  9:15 UTC (permalink / raw)
  To: mark.rutland, catalin.marinas, will
  Cc: linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating, shechenglong


On Wed, Oct 29, 2025 at 11:45:54AM +0800, Will Deacon wrote:
> Is the compiler smart enough to store a single string for the
> "mitigation disabled by command-line option\n" part? If not,
> you might want to use %s to avoid wasting memory. (I was going to
> check with llvm but I'm unable to apply your changes due to whitespace
> corruption).

Thanks, Will, for the helpful review. v3 incorporates your suggestion by
factoring the common suffix into a single const string and switching the
pr_info() calls to use "%s". The whitespace corruption has also been fixed
(restore tabs, no line-wrapped literals), so this version should apply
cleanly.


This series addresses one main issues around Spectre mitigation messages:

  1) Avoid multiple copies of the common suffix
     "mitigation disabled by command-line option\n" by using a single
     constant string and "%s" in pr_info().

v3 changes:
  - Fix whitespace corruption (tabs vs spaces).
  - Factor out the common suffix into a single static const and
    use "%s" as suggested.

shechenglong (2):
  cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
    Kconfig option is disabled.
  cpu: fix hard lockup triggered by printk calls within scheduling
    context

 arch/arm64/include/asm/spectre.h |  1 +
 arch/arm64/kernel/cpufeature.c   |  6 ++++++
 arch/arm64/kernel/proton-pack.c  | 37 +++++++++++++++++---------------
 3 files changed, 27 insertions(+), 17 deletions(-)

-- 
2.33.0



^ permalink raw reply	[flat|nested] 20+ messages in thread

* [PATCH v3 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled.
  2025-10-31  9:15 ` [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
@ 2025-10-31  9:15   ` shechenglong
  2025-10-31  9:15   ` [PATCH v3 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
  2025-11-07 15:53   ` [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages Will Deacon
  2 siblings, 0 replies; 20+ messages in thread
From: shechenglong @ 2025-10-31  9:15 UTC (permalink / raw)
  To: mark.rutland, catalin.marinas, will
  Cc: linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating, shechenglong

Following the pattern established with other Spectre mitigations,
do not prints a message when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
Kconfig option is disabled.

Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
Suggested-by: Will Deacon <will@kernel.org>
Signed-off-by: shechenglong <shechenglong@xfusion.com>
---
 arch/arm64/kernel/proton-pack.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
index f9a32dfde006..d833b7c1bba8 100644
--- a/arch/arm64/kernel/proton-pack.c
+++ b/arch/arm64/kernel/proton-pack.c
@@ -1042,8 +1042,6 @@ void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)
 
 	if (arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) {
 		/* No point mitigating Spectre-BHB alone. */
-	} else if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY)) {
-		pr_info_once("spectre-bhb mitigation disabled by compile time option\n");
 	} else if (cpu_mitigations_off() || __nospectre_bhb) {
 		pr_info_once("spectre-bhb mitigation disabled by command line option\n");
 	} else if (supports_ecbhb(SCOPE_LOCAL_CPU)) {
-- 
2.33.0



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [PATCH v3 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context
  2025-10-31  9:15 ` [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
  2025-10-31  9:15   ` [PATCH v3 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
@ 2025-10-31  9:15   ` shechenglong
  2025-11-07 15:53   ` [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages Will Deacon
  2 siblings, 0 replies; 20+ messages in thread
From: shechenglong @ 2025-10-31  9:15 UTC (permalink / raw)
  To: mark.rutland, catalin.marinas, will
  Cc: linux-arm-kernel, linux-kernel, stone.xulei, chenjialong,
	yuxiating, shechenglong

relocate the printk() calls from spectre_v4_mitigations_off() and
spectre_v2_mitigations_off() into setup_system_capabilities() function,
preventing hard lockups caused by printk calls in scheduler context.

Link: https://patchwork.kernel.org/project/linux-arm-kernel/patch/20250918064907.1832-1-shechenglong@xfusion.com/
Suggested-by: Mark Rutland <mark.rutland@arm.com>
Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
Suggested-by: Will Deacon <will@kernel.org>
Signed-off-by: shechenglong <shechenglong@xfusion.com>
---
 arch/arm64/include/asm/spectre.h |  1 +
 arch/arm64/kernel/cpufeature.c   |  6 ++++++
 arch/arm64/kernel/proton-pack.c  | 33 +++++++++++++++++---------------
 3 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/arch/arm64/include/asm/spectre.h b/arch/arm64/include/asm/spectre.h
index 8fef12626090..900454aaa292 100644
--- a/arch/arm64/include/asm/spectre.h
+++ b/arch/arm64/include/asm/spectre.h
@@ -117,6 +117,7 @@ void spectre_bhb_patch_wa3(struct alt_instr *alt,
 			   __le32 *origptr, __le32 *updptr, int nr_inst);
 void spectre_bhb_patch_clearbhb(struct alt_instr *alt,
 				__le32 *origptr, __le32 *updptr, int nr_inst);
+void spectre_print_disabled_mitigations(void);
 
 #endif	/* __ASSEMBLY__ */
 #endif	/* __ASM_SPECTRE_H */
diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
index 5ed401ff79e3..e25b0f84a22d 100644
--- a/arch/arm64/kernel/cpufeature.c
+++ b/arch/arm64/kernel/cpufeature.c
@@ -95,6 +95,7 @@
 #include <asm/vectors.h>
 #include <asm/virt.h>
 
+#include <asm/spectre.h>
 /* Kernel representation of AT_HWCAP and AT_HWCAP2 */
 static DECLARE_BITMAP(elf_hwcap, MAX_CPU_FEATURES) __read_mostly;
 
@@ -3875,6 +3876,11 @@ static void __init setup_system_capabilities(void)
 	 */
 	if (system_uses_ttbr0_pan())
 		pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
+
+	/*
+	 * Report Spectre mitigations status.
+	 */
+	spectre_print_disabled_mitigations();
 }
 
 void __init setup_system_features(void)
diff --git a/arch/arm64/kernel/proton-pack.c b/arch/arm64/kernel/proton-pack.c
index d833b7c1bba8..c7d70d04c164 100644
--- a/arch/arm64/kernel/proton-pack.c
+++ b/arch/arm64/kernel/proton-pack.c
@@ -91,12 +91,7 @@ early_param("nospectre_v2", parse_spectre_v2_param);
 
 static bool spectre_v2_mitigations_off(void)
 {
-	bool ret = __nospectre_v2 || cpu_mitigations_off();
-
-	if (ret)
-		pr_info_once("spectre-v2 mitigation disabled by command line option\n");
-
-	return ret;
+	return __nospectre_v2 || cpu_mitigations_off();
 }
 
 static const char *get_bhb_affected_string(enum mitigation_state bhb_state)
@@ -421,13 +416,8 @@ early_param("ssbd", parse_spectre_v4_param);
  */
 static bool spectre_v4_mitigations_off(void)
 {
-	bool ret = cpu_mitigations_off() ||
-		   __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
-
-	if (ret)
-		pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
-
-	return ret;
+	return cpu_mitigations_off() ||
+	       __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
 }
 
 /* Do we need to toggle the mitigation state on entry to/exit from the kernel? */
@@ -1042,8 +1032,6 @@ void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)
 
 	if (arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) {
 		/* No point mitigating Spectre-BHB alone. */
-	} else if (cpu_mitigations_off() || __nospectre_bhb) {
-		pr_info_once("spectre-bhb mitigation disabled by command line option\n");
 	} else if (supports_ecbhb(SCOPE_LOCAL_CPU)) {
 		state = SPECTRE_MITIGATED;
 		set_bit(BHB_HW, &system_bhb_mitigations);
@@ -1197,3 +1185,18 @@ void unpriv_ebpf_notify(int new_state)
 		pr_err("WARNING: %s", EBPF_WARN);
 }
 #endif
+
+void spectre_print_disabled_mitigations(void)
+{
+	/* Keep a single copy of the common message suffix to avoid duplication. */
+	const char *spectre_disabled_suffix = "mitigation disabled by command-line option\n";
+
+	if (spectre_v2_mitigations_off())
+		pr_info("spectre-v2 %s", spectre_disabled_suffix);
+
+	if (spectre_v4_mitigations_off())
+		pr_info("spectre-v4 %s", spectre_disabled_suffix);
+
+	if (__nospectre_bhb || cpu_mitigations_off())
+		pr_info("spectre-bhb %s", spectre_disabled_suffix);
+}
-- 
2.33.0



^ permalink raw reply related	[flat|nested] 20+ messages in thread

* Re: [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages
  2025-10-31  9:15 ` [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
  2025-10-31  9:15   ` [PATCH v3 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
  2025-10-31  9:15   ` [PATCH v3 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
@ 2025-11-07 15:53   ` Will Deacon
  2 siblings, 0 replies; 20+ messages in thread
From: Will Deacon @ 2025-11-07 15:53 UTC (permalink / raw)
  To: mark.rutland, catalin.marinas, shechenglong
  Cc: kernel-team, Will Deacon, linux-arm-kernel, linux-kernel,
	stone.xulei, chenjialong, yuxiating

On Fri, 31 Oct 2025 17:15:04 +0800, shechenglong wrote:
> On Wed, Oct 29, 2025 at 11:45:54AM +0800, Will Deacon wrote:
> > Is the compiler smart enough to store a single string for the
> > "mitigation disabled by command-line option\n" part? If not,
> > you might want to use %s to avoid wasting memory. (I was going to
> > check with llvm but I'm unable to apply your changes due to whitespace
> > corruption).
> 
> [...]

Applied to arm64 (for-next/fixes), thanks!

[1/2] arm64: proton-pack: Drop print when !CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY
      https://git.kernel.org/arm64/c/62e72463ca71
[2/2] arm64: proton-pack: Fix hard lockup due to print in scheduler context
      https://git.kernel.org/arm64/c/7f1635737823

Cheers,
-- 
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2025-11-07 15:54 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-18  6:49 [PATCH] cpu: fix hard lockup triggered during stress-ng stress testing shechenglong
2025-09-18 11:28 ` Catalin Marinas
2025-09-19 12:05   ` 答复: " shechenglong
2025-09-22 16:54     ` Catalin Marinas
2025-09-22 16:08   ` Mark Rutland
2025-09-24 12:32 ` [PATCH] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
2025-09-25 13:48   ` Catalin Marinas
2025-10-03 14:23   ` Will Deacon
2025-10-20 14:51 ` [PATCH v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
2025-10-20 14:51   ` [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
2025-10-20 14:51   ` [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
2025-10-29  3:45 ` [RESEND v2 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
2025-10-29  3:45   ` [PATCH v2 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
2025-10-30 14:48     ` Will Deacon
2025-10-29  3:45   ` [PATCH v2 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
2025-10-30 14:50     ` Will Deacon
2025-10-31  9:15 ` [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages shechenglong
2025-10-31  9:15   ` [PATCH v3 1/2] cpu:Remove the print when the CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY Kconfig option is disabled shechenglong
2025-10-31  9:15   ` [PATCH v3 2/2] cpu: fix hard lockup triggered by printk calls within scheduling context shechenglong
2025-11-07 15:53   ` [PATCH v3 0/2] arm64: spectre: Fix hard lockup and cleanup mitigation messages Will Deacon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).