[PATCH 1/3] x86/sched: Add arch_disable_hybrid_capacity_scale() interface

Yaxiong Tian posted 3 patches 2 weeks, 4 days ago
[PATCH 1/3] x86/sched: Add arch_disable_hybrid_capacity_scale() interface
Posted by Yaxiong Tian 2 weeks, 4 days ago
Users can enable/disable SMT (Simultaneous Multi-Threading) via
/sys/devices/system/cpu/smt, which means hybrid_capacity() may not
remain constant on heterogeneous platforms.

Therefore, add the arch_disable_hybrid_capacity_scale() interface
to handle dynamic SMT state changes.

Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
---
 arch/x86/include/asm/topology.h  |  2 ++
 arch/x86/kernel/cpu/aperfmperf.c | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/arch/x86/include/asm/topology.h b/arch/x86/include/asm/topology.h
index 1fadf0cf520c..a0c1205513e3 100644
--- a/arch/x86/include/asm/topology.h
+++ b/arch/x86/include/asm/topology.h
@@ -303,6 +303,7 @@ static inline long arch_scale_freq_capacity(int cpu)
 #define arch_scale_freq_capacity arch_scale_freq_capacity
 
 bool arch_enable_hybrid_capacity_scale(void);
+void arch_disable_hybrid_capacity_scale(void);
 void arch_set_cpu_capacity(int cpu, unsigned long cap, unsigned long max_cap,
 			   unsigned long cap_freq, unsigned long base_freq);
 
@@ -313,6 +314,7 @@ extern void arch_set_max_freq_ratio(bool turbo_disabled);
 extern void freq_invariance_set_perf_ratio(u64 ratio, bool turbo_disabled);
 #else
 static inline bool arch_enable_hybrid_capacity_scale(void) { return false; }
+static inline void arch_disable_hybrid_capacity_scale(void) { return; }
 static inline void arch_set_cpu_capacity(int cpu, unsigned long cap,
 					 unsigned long max_cap,
 					 unsigned long cap_freq,
diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c
index 7ffc78d5ebf2..a499eccf2f5c 100644
--- a/arch/x86/kernel/cpu/aperfmperf.c
+++ b/arch/x86/kernel/cpu/aperfmperf.c
@@ -362,6 +362,7 @@ struct arch_hybrid_cpu_scale {
 };
 
 static struct arch_hybrid_cpu_scale __percpu *arch_cpu_scale;
+static DEFINE_MUTEX(arch_hybrid_mutex);
 
 /**
  * arch_enable_hybrid_capacity_scale() - Enable hybrid CPU capacity scaling
@@ -375,11 +376,19 @@ bool arch_enable_hybrid_capacity_scale(void)
 {
 	int cpu;
 
+	guard(mutex)(&arch_hybrid_mutex);
+
 	if (static_branch_unlikely(&arch_hybrid_cap_scale_key)) {
 		WARN_ONCE(1, "Hybrid CPU capacity scaling already enabled");
 		return true;
 	}
 
+	if (arch_cpu_scale) {
+		static_branch_enable(&arch_hybrid_cap_scale_key);
+		pr_info("Hybrid CPU capacity scaling enabled\n");
+		return true;
+	}
+
 	arch_cpu_scale = alloc_percpu(struct arch_hybrid_cpu_scale);
 	if (!arch_cpu_scale)
 		return false;
@@ -396,6 +405,20 @@ bool arch_enable_hybrid_capacity_scale(void)
 	return true;
 }
 
+/**
+ * arch_disable_hybrid_capacity_scale() - Disable hybrid CPU capacity scaling
+ */
+void arch_disable_hybrid_capacity_scale(void)
+{
+	guard(mutex)(&arch_hybrid_mutex);
+
+	if (static_branch_unlikely(&arch_hybrid_cap_scale_key)) {
+		static_branch_disable(&arch_hybrid_cap_scale_key);
+		pr_info("Hybrid CPU capacity scaling disable\n");
+		return;
+	}
+}
+
 /**
  * arch_set_cpu_capacity() - Set scale-invariance parameters for a CPU
  * @cpu: Target CPU.
-- 
2.25.1