Skip to contentSkip to conversation

sched/topology: remove sysctl_sched_energy_aware depending on the architecture

PATCH Superseded Shrikanth Hegde 2 participants 3 messages Aug 29, 2023 – Aug 30, 2023 Scheduler

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/topology: remove sysctl_sched_energy_aware depending on the architecture

Currently sysctl_sched_energy_aware doesn't alter the said behaviour on some of the architectures. IIUC its meant to either force rebuild the perf domains or cleanup the perf domains by echoing 1 or 0 respectively.

perf domains are not built when there is SMT, or when there is no Asymmetric CPU topologies or when there is no frequency invariance. Since such cases EAS is not set and perf domains are not built. By changing the values of sysctl_sched_energy_aware, its not possible to force build the perf domains. Hence remove this sysctl on such platforms that dont support it at boot. Some of the settings can be changed later such as smt_active by offlining the CPU's, In those cases build_perf_domains returns true, in that case re-enable the sysctl.

---
 kernel/sched/topology.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)
kernel/sched/topology.c +26 -10
index 05a5bc678c08..ff68366be280 100644
@@ -208,7 +208,8 @@ sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
 
 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
 DEFINE_STATIC_KEY_FALSE(sched_energy_present);
-static unsigned int sysctl_sched_energy_aware = 1;
+static unsigned int sysctl_sched_energy_aware;
+static struct ctl_table_header *sysctl_eas_header;
 static DEFINE_MUTEX(sched_energy_mutex);
 static bool sched_energy_update;
 
@@ -226,6 +227,7 @@ static int sched_energy_aware_handler(struct ctl_table *table, int write,
 		void *buffer, size_t *lenp, loff_t *ppos)
 {
 	int ret, state;
+	int prev_val = sysctl_sched_energy_aware;
 
 	if (write && !capable(CAP_SYS_ADMIN))
 		return -EPERM;
@@ -233,8 +235,11 @@ static int sched_energy_aware_handler(struct ctl_table *table, int write,
 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
 	if (!ret && write) {
 		state = static_branch_unlikely(&sched_energy_present);
-		if (state != sysctl_sched_energy_aware)
+		if (state != sysctl_sched_energy_aware && prev_val != sysctl_sched_energy_aware) {
+			if (sysctl_sched_energy_aware && !state)
+				pr_warn("Attempt to build energy domains when EAS is disabled\n");
 			rebuild_sched_domains_energy();
+		}
 	}
 
 	return ret;
@@ -255,7 +260,14 @@ static struct ctl_table sched_energy_aware_sysctls[] = {
 
 static int __init sched_energy_aware_sysctl_init(void)
 {
-	register_sysctl_init("kernel", sched_energy_aware_sysctls);
+	int cpu = cpumask_first(cpu_active_mask);
+
+	if (sched_smt_active() || !per_cpu(sd_asym_cpucapacity, cpu) ||
+	    !arch_scale_freq_invariant())
+		return 0;
+
+	sysctl_eas_header = register_sysctl("kernel", sched_energy_aware_sysctls);
+	sysctl_sched_energy_aware = 1;
 	return 0;
 }
 
@@ -336,10 +348,18 @@ static void sched_energy_set(bool has_eas)
 		if (sched_debug())
 			pr_info("%s: stopping EAS\n", __func__);
 		static_branch_disable_cpuslocked(&sched_energy_present);
+#ifdef CONFIG_PROC_SYSCTL
+		unregister_sysctl_table(sysctl_eas_header);
+#endif
+		sysctl_sched_energy_aware = 0;
 	} else if (has_eas && !static_branch_unlikely(&sched_energy_present)) {
 		if (sched_debug())
 			pr_info("%s: starting EAS\n", __func__);
 		static_branch_enable_cpuslocked(&sched_energy_present);
+#ifdef CONFIG_PROC_SYSCTL
+		sysctl_eas_header = register_sysctl("kernel", sched_energy_aware_sysctls);
+#endif
+		sysctl_sched_energy_aware = 1;
 	}
 }
 
@@ -380,15 +400,11 @@ static bool build_perf_domains(const struct cpumask *cpu_map)
 	struct cpufreq_policy *policy;
 	struct cpufreq_governor *gov;
 
-	if (!sysctl_sched_energy_aware)
-		goto free;
-
 	/* EAS is enabled for asymmetric CPU capacity topologies. */
 	if (!per_cpu(sd_asym_cpucapacity, cpu)) {
-		if (sched_debug()) {
-			pr_info("rd %*pbl: CPUs do not have asymmetric capacities\n",
-					cpumask_pr_args(cpu_map));
-		}
+		if (sched_debug())
+			pr_info("rd %*pbl: Disabling EAS,  CPUs do not have asymmetric capacities\n",
+				cpumask_pr_args(cpu_map));
 		goto free;
 	}
 

--
2.31.1

#1 lore raw

Hi Shrikanth,

Quoted 15 lines · Shrikanth Hegde

Currently sysctl_sched_energy_aware doesn't alter the said behaviour on some of the architectures. IIUC its meant to either force rebuild the perf domains or cleanup the perf domains by echoing 1 or 0 respectively.

perf domains are not built when there is SMT, or when there is no Asymmetric CPU topologies or when there is no frequency invariance. Since such cases EAS is not set and perf domains are not built. By changing the values of sysctl_sched_energy_aware, its not possible to force build the perf domains. Hence remove this sysctl on such platforms that dont support it at boot. Some of the settings can be changed later such as smt_active by offlining the CPU's, In those cases build_perf_domains returns true, in that case re-enable the sysctl.

[snip...]

kernel/sched/topology.c static bool build_perf_domains(const struct cpumask *cpu_map) · line 385

Quoted 7 lines
kernel/sched/topology.c +0 -3
@@ -380,15 +400,11 @@ static bool build_perf_domains(const struct cpumask *cpu_map)
 	struct cpufreq_policy *policy;
 	struct cpufreq_governor *gov;
 
-	if (!sysctl_sched_energy_aware)
-		goto free;
-

I tried to ramp up the EAS and maybe I overlooked it, why do we remove above check? If the system boots with EAS enabled and sysctl_sched_energy_aware set to 1, then the user wants to disable EAS then changing sysctl_sched_energy_aware to 0. Without above check, how could the perf domain be freed?

thanks,
Chenyu

#2 lore raw
Quoted 25 lines · Chen Yu

Hi Shrikanth,

Quoted 15 lines · Shrikanth Hegde

Currently sysctl_sched_energy_aware doesn't alter the said behaviour on some of the architectures. IIUC its meant to either force rebuild the perf domains or cleanup the perf domains by echoing 1 or 0 respectively.

perf domains are not built when there is SMT, or when there is no Asymmetric CPU topologies or when there is no frequency invariance. Since such cases EAS is not set and perf domains are not built. By changing the values of sysctl_sched_energy_aware, its not possible to force build the perf domains. Hence remove this sysctl on such platforms that dont support it at boot. Some of the settings can be changed later such as smt_active by offlining the CPU's, In those cases build_perf_domains returns true, in that case re-enable the sysctl.

[snip...]

Quoted 4 lines
kernel/sched/topology.c +0 -0
@@ -380,15 +400,11 @@ static bool build_perf_domains(const struct cpumask *cpu_map)
 	struct cpufreq_policy *policy;
 	struct cpufreq_governor *gov;
 
Quoted 9 lines

kernel/sched/topology.c static bool build_perf_domains(const struct cpumask *cpu_map) · line 385

Quoted 3 lines
kernel/sched/topology.c +0 -3
-	if (!sysctl_sched_energy_aware)
-		goto free;
-

I tried to ramp up the EAS and maybe I overlooked it, why do we remove above check? If the system boots with EAS enabled and sysctl_sched_energy_aware set to 1, then the user wants to disable EAS then changing sysctl_sched_energy_aware to 0. Without above check, how could the perf domain be freed?

Thank you very much Chen Yu, for taking a look. I had missed that case.

The reason for removing it was, conditions which are checked while doing build_perf_domains can be changed after system boot. like by off-lining the SMT siblings, sched_smt_active can be false, it is possible in future for the other conditions to be false as well. In that case, having the sysctl check wouldn't allow it to build the perf domains.

Since any change in this sysctl, will set sched_energy_update. That can be used in combination to detect whether change is coming due to sysctl change or due to system becoming capable of doing perf domains.

Something like below would work.

+ if (!sysctl_sched_energy_aware && sched_energy_update)
+ goto free;

This will introduce another issue, that it will remove the sysctl after setting it to 0. Have taken care of that with below code change.

@@ -349,7 +349,16 @@ static void sched_energy_set(bool has_eas)

                        pr_info("%s: stopping EAS\n", __func__);
                static_branch_disable_cpuslocked(&sched_energy_present);
 #ifdef CONFIG_PROC_SYSCTL
-               unregister_sysctl_table(sysctl_eas_header);
+               /*
+                * if the architecture supports EAS and forcefully
+                * perf domains are destroyed, there should be a sysctl
+                * to eanble it later. If this was due to dynamic system
+                * change such as SMT<->NON_SMT then remove sysctl.
+                */

+ if (sysctl_eas_header && !sched_energy_update) {
+ unregister_sysctl_table(sysctl_eas_header);
+ sysctl_eas_header = NULL;
+ }

 #endif
                sysctl_sched_energy_aware = 0;
        } else if (has_eas && !static_branch_unlikely(&sched_energy_present)) {
@@ -357,7 +366,8 @@ static void sched_energy_set(bool has_eas)
                        pr_info("%s: starting EAS\n", __func__);
                static_branch_enable_cpuslocked(&sched_energy_present);

#ifdef CONFIG_PROC_SYSCTL
- sysctl_eas_header = register_sysctl("kernel", sched_energy_aware_sysctls);
+ if (!sysctl_eas_header)
+ sysctl_eas_header = register_sysctl("kernel", sched_energy_aware_sysctls);

 #endif
                sysctl_sched_energy_aware = 1;
        }

Different Cases:

Case1. system while booting has EAS capability, sysctl will be set 1. Hence perf domains will be built On changing the sysctl to 0, above condition will be true and hence it would be freed and sysctl will not be removed. later sysctl is changed to 1, enabling the perf domains rebuilt again. Since sysctl is already there, it will skip register.

Case2. System while booting doesn't have EAS Capability. Later after system change it becomes capable of EAS. sched_energy_update is false. Hence though sysctl is 0, will go ahead and try to enable eas. if has_eas is true, then sysctl will be registered. After that it any sysctl change is same as Case1.

Case3. System becomes not capable of EAS due to system change. Here since sched_energy_update is false. build_perf_domains return has_eas as false and Since this is dynamic change remove the sysctl.

A bit convoluted because the design here, is considering dynamically system becoming capable of EAS. Will send out a V2 with the above change after some testing (have to try with HACK since EAS is by default not enabled on power10)