[PATCH] cpu/SMT: recover global num_threads after disable smt switch fail

Bowen You posted 1 patch 2 months, 2 weeks ago
kernel/cpu.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
[PATCH] cpu/SMT: recover global num_threads after disable smt switch fail
Posted by Bowen You 2 months, 2 weeks ago
When attempting to disable SMT with the following command:
    `echo off > /sys/devices/system/cpu/smt/control`
if `cpuhp_smt_disable` fails due to a "resource busy" error, the global
variable `cpu_smt_num_threads` is incorrectly updated to the target value
despite the failure.

This state inconsistency leads to subsequent attempts to disable SMT being
 ineffective since `cpu_smt_num_threads` equals `orig_threads`, preventing
 `cpuhp_smt_disable` from being invoked. To recover functionality, users
must manually execute `echo on > /sys/devices/system/cpu/smt/control`,
 which is undesirable.

This commit removes the `orig_threads` variable and update the global
`cpu_smt_num_threads` only after successfully switching the SMT state.
This ensures proper state management and prevents the system from entering
 an inconsistent state.

Cc: stable@vger.kernel.org # v6.5+
Fixes: 7f48405c3c34 ("cpu/SMT: Allow enabling partial SMT states via sysfs")
Signed-off-by: Bowen You <youbowen2@huawei.com>
---
 kernel/cpu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/cpu.c b/kernel/cpu.c
index db9f6c539b28..a2420d8a6ae0 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -2902,7 +2902,7 @@ static ssize_t
 __store_smt_control(struct device *dev, struct device_attribute *attr,
 		    const char *buf, size_t count)
 {
-	int ctrlval, ret, num_threads, orig_threads;
+	int ctrlval, ret, num_threads;
 	bool force_off;
 
 	if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
@@ -2935,16 +2935,16 @@ __store_smt_control(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	orig_threads = cpu_smt_num_threads;
-	cpu_smt_num_threads = num_threads;
-
 	force_off = ctrlval != cpu_smt_control && ctrlval == CPU_SMT_FORCE_DISABLED;
 
-	if (num_threads > orig_threads)
+	if (num_threads > cpu_smt_num_threads)
 		ret = cpuhp_smt_enable();
-	else if (num_threads < orig_threads || force_off)
+	else if (num_threads < cpu_smt_num_threads || force_off)
 		ret = cpuhp_smt_disable(ctrlval);
 
+	if (!ret)
+		cpu_smt_num_threads = num_threads;
+
 	unlock_device_hotplug();
 	return ret ? ret : count;
 }
-- 
2.34.1
Re: [PATCH] cpu/SMT: recover global num_threads after disable smt switch fail
Posted by youbowen (A) 3 weeks ago
Gentle ping on this patch.