[PATCH -v2] x86/split_lock: Restructure the unwieldy switch-case in sld_state_show()

Borislav Petkov posted 1 patch 1 month, 2 weeks ago
There is a newer version of this series
arch/x86/kernel/cpu/bus_lock.c | 49 +++++++++++++++++-----------------
1 file changed, 25 insertions(+), 24 deletions(-)
[PATCH -v2] x86/split_lock: Restructure the unwieldy switch-case in sld_state_show()
Posted by Borislav Petkov 1 month, 2 weeks ago
A bit better version:

---

From: "Borislav Petkov (AMD)" <bp@alien8.de>
Date: Sun, 4 Jan 2026 14:40:23 +0100

Split the handling in two parts:

1. handle the sld_state option first

2. handle X86_FEATURE flag-based printing afterwards

This splits the function nicely into two, separate logical things which
are easier to parse and understand.

Also, zap the printing in the disabled case.

Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
---
 arch/x86/kernel/cpu/bus_lock.c | 49 +++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 24 deletions(-)

diff --git a/arch/x86/kernel/cpu/bus_lock.c b/arch/x86/kernel/cpu/bus_lock.c
index fb166662bc0d..153cfeb99dca 100644
--- a/arch/x86/kernel/cpu/bus_lock.c
+++ b/arch/x86/kernel/cpu/bus_lock.c
@@ -391,34 +391,35 @@ static void __init split_lock_setup(struct cpuinfo_x86 *c)
 
 static void sld_state_show(void)
 {
-	if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) &&
-	    !boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
+	const char *action = "warning";
+
+	if ((!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) &&
+	     !boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) ||
+	    (sld_state == sld_off))
 		return;
 
-	switch (sld_state) {
-	case sld_off:
-		pr_info("disabled\n");
-		break;
-	case sld_warn:
-		if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
-			pr_info("#AC: crashing the kernel on kernel split_locks and warning on user-space split_locks\n");
-			if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
-					      "x86/splitlock", NULL, splitlock_cpu_offline) < 0)
-				pr_warn("No splitlock CPU offline handler\n");
-		} else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
-			pr_info("#DB: warning on user-space bus_locks\n");
-		}
-		break;
-	case sld_fatal:
-		if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
-			pr_info("#AC: crashing the kernel on kernel split_locks and sending SIGBUS on user-space split_locks\n");
-		else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
-			pr_info("#DB: sending SIGBUS on user-space bus_locks\n");
-		break;
-	case sld_ratelimit:
+	if (sld_state == sld_ratelimit) {
 		if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
 			pr_info("#DB: setting system wide bus lock rate limit to %u/sec\n", bld_ratelimit.burst);
-		break;
+		return;
+	} else if (sld_state == sld_fatal) {
+		action = "sending SIGBUS";
+	}
+
+	if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
+		pr_info("#AC: crashing the kernel on kernel split_locks and %s on user-space split_locks\n", action);
+
+		/*
+		 * This is handling the case where a CPU goes offline at the
+		 * moment where split lock detection is disabled in the warn
+		 * setting, see split_lock_warn(). It doesn't have any effect
+		 * in the fatal case.
+                 */
+		if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/splitlock", NULL, splitlock_cpu_offline) < 0)
+			pr_warn("No splitlock CPU offline handler\n");
+
+	} else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
+		pr_info("#DB: %s on user-space bus_locks\n", action);
 	}
 }
 
-- 
2.51.0


-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH -v2] x86/split_lock: Restructure the unwieldy switch-case in sld_state_show()
Posted by Xiaoyao Li 1 month, 1 week ago
On 2/26/2026 10:50 PM, Borislav Petkov wrote:
> From: "Borislav Petkov (AMD)" <bp@alien8.de>
> Date: Sun, 4 Jan 2026 14:40:23 +0100
> 
> Split the handling in two parts:
> 
> 1. handle the sld_state option first
> 
> 2. handle X86_FEATURE flag-based printing afterwards
> 
> This splits the function nicely into two, separate logical things which
> are easier to parse and understand.
> 
> Also, zap the printing in the disabled case.

How about mentioning the change of registering CPU offline callback for 
fatal mode as well?

> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>

Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com>
Re: [PATCH -v2] x86/split_lock: Restructure the unwieldy switch-case in sld_state_show()
Posted by Borislav Petkov 1 month ago
On Fri, Mar 06, 2026 at 10:57:31AM +0800, Xiaoyao Li wrote:
> How about mentioning the change of registering CPU offline callback for
> fatal mode as well?

It's in the comment above it.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH -v2] x86/split_lock: Restructure the unwieldy switch-case in sld_state_show()
Posted by Luck, Tony 1 month, 2 weeks ago
On Thu, Feb 26, 2026 at 03:50:33PM +0100, Borislav Petkov wrote:

Only need the cpuhp_setup_state() for the sld_state == sld_warn case.

> +	if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
> +		pr_info("#AC: crashing the kernel on kernel split_locks and %s on user-space split_locks\n", action);
> +
	    if (sld_state == sld_warn) {
> +		/*
> +		 * This is handling the case where a CPU goes offline at the
> +		 * moment where split lock detection is disabled in the warn
> +		 * setting, see split_lock_warn(). It doesn't have any effect
> +		 * in the fatal case.
> +                 */
> +		if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/splitlock", NULL, splitlock_cpu_offline) < 0)
> +			pr_warn("No splitlock CPU offline handler\n");
            }
> +
> +	} else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
> +		pr_info("#DB: %s on user-space bus_locks\n", action);
>  	}
>  }

-Tony
Re: [PATCH -v2] x86/split_lock: Restructure the unwieldy switch-case in sld_state_show()
Posted by Borislav Petkov 1 month, 2 weeks ago
On Thu, Feb 26, 2026 at 01:01:44PM -0800, Luck, Tony wrote:
> Only need the cpuhp_setup_state() for the sld_state == sld_warn case.
> 
> > +	if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
> > +		pr_info("#AC: crashing the kernel on kernel split_locks and %s on user-space split_locks\n", action);
> > +
> 	    if (sld_state == sld_warn) {

Remember that simplification?

https://lore.kernel.org/r/SJ1PR11MB6083BFCB1FFCE6DFE6F40D38FC84A@SJ1PR11MB6083.namprd11.prod.outlook.com

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
[tip: x86/cleanups] x86/split_lock: Restructure the unwieldy switch-case in sld_state_show()
Posted by tip-bot2 for Borislav Petkov 1 month ago
The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     04e43ec9f002ed1041b41a6df4c645ef3148da9f
Gitweb:        https://git.kernel.org/tip/04e43ec9f002ed1041b41a6df4c645ef3148da9f
Author:        Borislav Petkov <bp@alien8.de>
AuthorDate:    Thu, 26 Feb 2026 15:50:33 +01:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Fri, 13 Mar 2026 16:57:02 +01:00

x86/split_lock: Restructure the unwieldy switch-case in sld_state_show()

Split the handling in two parts:

1. handle the sld_state option first

2. handle X86_FEATURE flag-based printing afterwards

This splits the function nicely into two, separate logical things which
are easier to parse and understand.

Also, zap the printing in the disabled case.

Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com>
Link: https://patch.msgid.link/20260226145033.GAaaBduQ0rWXydOkAm@fat_crate.local
---
 arch/x86/kernel/cpu/bus_lock.c | 49 ++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 24 deletions(-)

diff --git a/arch/x86/kernel/cpu/bus_lock.c b/arch/x86/kernel/cpu/bus_lock.c
index fb16666..660aa9a 100644
--- a/arch/x86/kernel/cpu/bus_lock.c
+++ b/arch/x86/kernel/cpu/bus_lock.c
@@ -391,34 +391,35 @@ supported:
 
 static void sld_state_show(void)
 {
-	if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) &&
-	    !boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
+	const char *action = "warning";
+
+	if ((!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) &&
+	     !boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) ||
+	    (sld_state == sld_off))
 		return;
 
-	switch (sld_state) {
-	case sld_off:
-		pr_info("disabled\n");
-		break;
-	case sld_warn:
-		if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
-			pr_info("#AC: crashing the kernel on kernel split_locks and warning on user-space split_locks\n");
-			if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
-					      "x86/splitlock", NULL, splitlock_cpu_offline) < 0)
-				pr_warn("No splitlock CPU offline handler\n");
-		} else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
-			pr_info("#DB: warning on user-space bus_locks\n");
-		}
-		break;
-	case sld_fatal:
-		if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
-			pr_info("#AC: crashing the kernel on kernel split_locks and sending SIGBUS on user-space split_locks\n");
-		else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
-			pr_info("#DB: sending SIGBUS on user-space bus_locks\n");
-		break;
-	case sld_ratelimit:
+	if (sld_state == sld_ratelimit) {
 		if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
 			pr_info("#DB: setting system wide bus lock rate limit to %u/sec\n", bld_ratelimit.burst);
-		break;
+		return;
+	} else if (sld_state == sld_fatal) {
+		action = "sending SIGBUS";
+	}
+
+	if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
+		pr_info("#AC: crashing the kernel on kernel split_locks and %s on user-space split_locks\n", action);
+
+		/*
+		 * This is handling the case where a CPU goes offline at the
+		 * moment where split lock detection is disabled in the warn
+		 * setting, see split_lock_warn(). It doesn't have any effect
+		 * in the fatal case.
+		 */
+		if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/splitlock", NULL, splitlock_cpu_offline) < 0)
+			pr_warn("No splitlock CPU offline handler\n");
+
+	} else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
+		pr_info("#DB: %s on user-space bus_locks\n", action);
 	}
 }