Skip to contentSkip to conversation

Make IPA use PM_EM

PATCH Superseded Quentin Perret 2 participants 9 messages Apr 17, 2019 – Apr 23, 2019

Subsystems (2)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover Make IPA use PM_EM

Changes in v2:
**************
- Fixed patch 01/03 to actually enable CONFIG_ENERGY_MODEL
- Added "depends on ENERGY_MODEL" to IPA (Daniel)
- Added check to bail out if the freq table is unsorted (Viresh)

Cover letter:
*************

The Intelligent Power Allocator (IPA) thermal governor uses an Energy Model (or EM) of the CPUs to re-distribute the power budget. To do so, it builds a table of <frequency, power> tuples where the power values are computed using the 'dynamic-power-coefficient' DT property. All of this is done in and only for the thermal subsystem, and more specifically for CPUs -- the power of other types of devices is obtained differently.

Recently, the CPU scheduler has seen the introduction of Energy Aware Scheduling (EAS) patches, which also rely on an EM of the CPUs. This EM, however, is managed by an independent framework, called PM_EM, aimed to be used by all kernel subsystems interested in the power consumed by CPUs, and not only the scheduler.

This patch series follows this logic and removes the (now redundant) thermal-specific EM computation code to migrate IPA to use PM_EM instead.

Doing so should have no visible functional impact for existing users of IPA since:

- during the 5.1 development cycle, a series of patches [1] introduced in PM_OPP some infrastructure (dev_pm_opp_of_register_em()) enabling the registration of EMs in PM_EM using the DT property used by IPA;

- the existing upstream cpufreq drivers marked with the 'CPUFREQ_IS_COOLING_DEV' flag all call dev_pm_opp_of_register_em(), which means they all support PM_EM (the only two exceptions are qoriq-cpufreq which doesn't in fact use an EM and scmi-cpufreq which already supports PM_EM without using the PM_OPP infrastructurei because it read power costs directly from firmware);

So, migrating IPA to using PM_EM should effectively be just plumbing since for the existing IPA users the PM_EM tables will contain the exact same power values that IPA used to compute on its own until now. The only new dependency is to compile in CONFIG_ENERGY_MODEL.

Why is this migration still a good thing ? For three main reasons.

1. it removes redundant code;

2. it introduces an abstraction layer between IPA and the EM computation. PM_EM offers to EAS and IPA (and potentially other clients) standardized EM tables and hides 'how' these tables have been obtained. PM_EM as of now supports power values either coming from the 'dynamic-power-coefficient' DT property or obtained directly from firmware using SCMI. The latter is a new feature for IPA and that comes 'for free' with the migration. This will also be true in the future every time PM_EM gets support for other ways of loading the EM. Moreover, PM_EM is documented and has a debugfs interface which should help adding support for new platforms.

3. it builds a consistent view of the EM of CPUs across kernel subsystems, which is a pre-requisite for any kind of future work aiming at a smarter power allocation using scheduler knowledge about the system for example.

[1] https://lore.kernel.org/lkml/[email protected]/

Quentin Perret (3):
arm64: defconfig: Enable CONFIG_ENERGY_MODEL PM / EM: Expose perf domain struct
thermal: cpu_cooling: Migrate to using the EM framework

 arch/arm64/configs/defconfig  |   1 +
 drivers/thermal/Kconfig       |   1 +
 drivers/thermal/cpu_cooling.c | 237 ++++++++++++----------------------
 include/linux/energy_model.h  |   3 +-
 4 files changed, 83 insertions(+), 159 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 1/3 arm64: defconfig: Enable CONFIG_ENERGY_MODEL

The recently introduced Energy Model (EM) framework manages power cost tables for the CPUs of the system. Its only user right now is the scheduler, in the context of Energy Aware Scheduling (EAS).

However, the EM framework also offers a generic infrastructure that could replace subsystem-specific implementations of the same concepts, as this is the case in the thermal framework.

So, in order to prepare the migration of the thermal subsystem to use the EM framework, enable it in the default arm64 defconfig, which is the most commonly used architecture for IPA. This will also compile-in all of the EAS code, although it won't be enabled by default -- EAS requires to use the 'schedutil' CPUFreq governor while arm64 defaults to 'performance'.

---
 arch/arm64/configs/defconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/configs/defconfig +1 -0
index 2d9c39033c1a..4881a752ab3f 100644
@@ -97,6 +97,7 @@ CONFIG_XEN=y
 CONFIG_COMPAT=y
 CONFIG_HIBERNATION=y
 CONFIG_WQ_POWER_EFFICIENT_DEFAULT=y
+CONFIG_ENERGY_MODEL=y
 CONFIG_ARM_CPUIDLE=y
 CONFIG_CPU_FREQ=y
 CONFIG_CPU_FREQ_STAT=y
Signature · 1 line
2.21.0
#2 lore raw

PATCH v2 2/3 PM / EM: Expose perf domain struct

In reply to Quentin Perret

In the current state, the perf_domain struct is fully defined only when CONFIG_ENERGY_MODEL=y. Since we need to write code that compiles both with or without that option in the thermal framework, make sure to actually define the struct regardless of the config option. That allows to avoid using stubbed accessor functions all the time in code paths that use the EM.

---
 include/linux/energy_model.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/energy_model.h +1 -2
index aa027f7bcb3e..fb32b86a467d 100644
@@ -9,7 +9,6 @@
 #include <linux/sched/topology.h>
 #include <linux/types.h>
 
-#ifdef CONFIG_ENERGY_MODEL
 /**
  * em_cap_state - Capacity state of a performance domain
  * @frequency:	The CPU frequency in KHz, for consistency with CPUFreq
@@ -40,6 +39,7 @@ struct em_perf_domain {
 	unsigned long cpus[0];
 };
 
+#ifdef CONFIG_ENERGY_MODEL
 #define EM_CPU_MAX_POWER 0xFFFF
 
 struct em_data_callback {
@@ -160,7 +160,6 @@ static inline int em_pd_nr_cap_states(struct em_perf_domain *pd)
 }
 
 #else
-struct em_perf_domain {};
 struct em_data_callback {};
 #define EM_DATA_CB(_active_power_cb) { }
 
Signature · 1 line
2.21.0
#3 lore raw

PATCH v2 3/3 thermal: cpu_cooling: Migrate to using the EM framework

In reply to Quentin Perret

The newly introduced Energy Model framework manages power cost tables in a generic way. Moreover, it supports a several types of models since the tables can come from DT or firmware (through SCMI) for example. On the other hand, the cpu_cooling subsystem manages its own power cost tables using only DT data.

In order to avoid the duplication of data in the kernel, and in order to enable IPA with EMs coming from more than just DT, remove the private tables from cpu_cooling.c and migrate it to using the centralized EM framework.

The case where the thermal subsystem is used without an Energy Model (cpufreq_cooling_ops) is handled by looking directly at CPUFreq's frequency table which is already a dependency for cpu_cooling.c anyway. Since the thermal framework expects the cooling states in a particular order, bail out whenever the CPUFreq table is unsorted, since that is fairly uncommon in general, and there are currently no users of cpu_cooling for this use-case.

---
 drivers/thermal/Kconfig       |   1 +
 drivers/thermal/cpu_cooling.c | 237 ++++++++++++----------------------
 2 files changed, 81 insertions(+), 157 deletions(-)
drivers/thermal/Kconfig +1 -0
index 653aa27a25a4..d695bd33c440 100644
@@ -144,6 +144,7 @@ config THERMAL_GOV_USER_SPACE
 
 config THERMAL_GOV_POWER_ALLOCATOR
 	bool "Power allocator thermal governor"
+	depends on ENERGY_MODEL
 	help
 	  Enable this to manage platform thermals by dynamically
 	  allocating and limiting power to devices.
drivers/thermal/cpu_cooling.c +80 -157

401 lines not on this page · raw message

Signature · 1 line
2.21.0
#4 lore raw

Re: [PATCH v2 3/3] thermal: cpu_cooling: Migrate to using the EM framework

Quoted 5 lines · Quentin Perret

static struct thermal_cooling_device * __cpufreq_cooling_register(struct device_node *np,

drivers/thermal/cpu_cooling.c +2 -1
-			struct cpufreq_policy *policy, u32 capacitance)
+			struct cpufreq_policy *policy,
+			struct em_perf_domain *em)

drivers/thermal/cpu_cooling.c __cpufreq_cooling_register(struct device_node *np, · line 612

Quoted 10 lines
drivers/thermal/cpu_cooling.c +5 -1
 {
 
+	if (em_is_sane(cpufreq_cdev, em)) {
+		cpufreq_cdev->em = em;
 		cooling_ops = &cpufreq_power_cooling_ops;
-	} else {
+	} else if (policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED) {
 		cooling_ops = &cpufreq_cooling_ops;
+	} else {
+		WARN(1, "cpu_cooling: no valid frequency table found\n");

Well the frequency table is valid, isn't it ? Maybe something like: "cpu_cooling doesn't support unsorted frequency tables" ?

Quoted 63 lines
drivers/thermal/cpu_cooling.c +6 -9
+		cdev = ERR_PTR(-EINVAL);
+		goto remove_ida;
 	}
 
 	cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
@@ -691,7 +619,7 @@ __cpufreq_cooling_register(struct device_node *np,
 	if (IS_ERR(cdev))
 		goto remove_ida;
 
-	cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
+	cpufreq_cdev->clipped_freq = get_state_freq(cpufreq_cdev, 0);
 	cpufreq_cdev->cdev = cdev;
 
 	mutex_lock(&cooling_list_lock);
@@ -708,8 +636,6 @@ __cpufreq_cooling_register(struct device_node *np,
 
 remove_ida:
 	ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
-free_table:
-	kfree(cpufreq_cdev->freq_table);
 free_idle_time:
 	kfree(cpufreq_cdev->idle_time);
 free_cdev:
@@ -731,7 +657,7 @@ __cpufreq_cooling_register(struct device_node *np,
 struct thermal_cooling_device *
 cpufreq_cooling_register(struct cpufreq_policy *policy)
 {
-	return __cpufreq_cooling_register(NULL, policy, 0);
+	return __cpufreq_cooling_register(NULL, policy, NULL);
 }
 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
 
@@ -759,7 +685,6 @@ of_cpufreq_cooling_register(struct cpufreq_policy *policy)
 {
 	struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
 	struct thermal_cooling_device *cdev = NULL;
-	u32 capacitance = 0;
 
 	if (!np) {
 		pr_err("cpu_cooling: OF node not available for cpu%d\n",
@@ -768,10 +693,9 @@ of_cpufreq_cooling_register(struct cpufreq_policy *policy)
 	}
 
 	if (of_find_property(np, "#cooling-cells", NULL)) {
-		of_property_read_u32(np, "dynamic-power-coefficient",
-				     &capacitance);
+		struct em_perf_domain *em = em_cpu_get(policy->cpu);
 
-		cdev = __cpufreq_cooling_register(np, policy, capacitance);
+		cdev = __cpufreq_cooling_register(np, policy, em);
 		if (IS_ERR(cdev)) {
 			pr_err("cpu_cooling: cpu%d failed to register as cooling device: %ld\n",
 			       policy->cpu, PTR_ERR(cdev));
@@ -813,7 +737,6 @@ void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
 	thermal_cooling_device_unregister(cpufreq_cdev->cdev);
 	ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
 	kfree(cpufreq_cdev->idle_time);
-	kfree(cpufreq_cdev->freq_table);
 	kfree(cpufreq_cdev);
 }
 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);
Signature · 1 line
2.21.0
Signature · 1 line
viresh
#5 lore raw

Re: [PATCH v2 3/3] thermal: cpu_cooling: Migrate to using the EM framework

Quoted 8 lines · Viresh Kumar
Quoted 6 lines · Quentin Perret

static struct thermal_cooling_device * __cpufreq_cooling_register(struct device_node *np,

drivers/thermal/cpu_cooling.c +2 -1
-			struct cpufreq_policy *policy, u32 capacitance)
+			struct cpufreq_policy *policy,
+			struct em_perf_domain *em)
 {
Quoted 10 lines

drivers/thermal/cpu_cooling.c __cpufreq_cooling_register(struct device_node *np, · line 612

Quoted 8 lines
drivers/thermal/cpu_cooling.c +5 -1
+	if (em_is_sane(cpufreq_cdev, em)) {
+		cpufreq_cdev->em = em;
 		cooling_ops = &cpufreq_power_cooling_ops;
-	} else {
+	} else if (policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED) {
 		cooling_ops = &cpufreq_cooling_ops;
+	} else {
+		WARN(1, "cpu_cooling: no valid frequency table found\n");

Well the frequency table is valid, isn't it ?

True ...

Quoted 1 line

Maybe something like: "cpu_cooling doesn't support unsorted frequency tables" ?

Right, otherwise I guess that could be confused with the check on cpu_table_count_valid_entries() above. And while I'm thinking about it perhaps WARN is a bit too much here ? We can handle the error safely so pr_err() should be enough ?

Thanks,
Quentin

#6 lore raw

Re: [PATCH v2 3/3] thermal: cpu_cooling: Migrate to using the EM framework

Quoted 20 lines · Quentin Perret
Quoted 8 lines · Viresh Kumar
Quoted 6 lines · Quentin Perret

static struct thermal_cooling_device * __cpufreq_cooling_register(struct device_node *np,

drivers/thermal/cpu_cooling.c +2 -1
-			struct cpufreq_policy *policy, u32 capacitance)
+			struct cpufreq_policy *policy,
+			struct em_perf_domain *em)
 {
Quoted 10 lines

drivers/thermal/cpu_cooling.c __cpufreq_cooling_register(struct device_node *np, · line 612

Quoted 8 lines
drivers/thermal/cpu_cooling.c +5 -1
+	if (em_is_sane(cpufreq_cdev, em)) {
+		cpufreq_cdev->em = em;
 		cooling_ops = &cpufreq_power_cooling_ops;
-	} else {
+	} else if (policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED) {
 		cooling_ops = &cpufreq_cooling_ops;
+	} else {
+		WARN(1, "cpu_cooling: no valid frequency table found\n");

Well the frequency table is valid, isn't it ?

Quoted 8 lines

True ...

Quoted 1 line

Maybe something like: "cpu_cooling doesn't support unsorted frequency tables" ?

Right, otherwise I guess that could be confused with the check on cpu_table_count_valid_entries() above. And while I'm thinking about it perhaps WARN is a bit too much here ? We can handle the error safely so pr_err() should be enough ?

Hmm, I would keep the WARN as it is hard to miss it compared to a simple pr_err.

Signature · 1 line
viresh
#7 lore raw

Re: [PATCH v2 3/3] thermal: cpu_cooling: Migrate to using the EM framework

Quoted 25 lines · Viresh Kumar
Quoted 24 lines · Quentin Perret
Quoted 8 lines · Viresh Kumar
Quoted 6 lines · Quentin Perret

static struct thermal_cooling_device * __cpufreq_cooling_register(struct device_node *np,

drivers/thermal/cpu_cooling.c +2 -1
-			struct cpufreq_policy *policy, u32 capacitance)
+			struct cpufreq_policy *policy,
+			struct em_perf_domain *em)
 {
Quoted 10 lines

drivers/thermal/cpu_cooling.c __cpufreq_cooling_register(struct device_node *np, · line 612

Quoted 8 lines
drivers/thermal/cpu_cooling.c +5 -1
+	if (em_is_sane(cpufreq_cdev, em)) {
+		cpufreq_cdev->em = em;
 		cooling_ops = &cpufreq_power_cooling_ops;
-	} else {
+	} else if (policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED) {
 		cooling_ops = &cpufreq_cooling_ops;
+	} else {
+		WARN(1, "cpu_cooling: no valid frequency table found\n");

Well the frequency table is valid, isn't it ?

True ...

Quoted 1 line

Maybe something like: "cpu_cooling doesn't support unsorted frequency tables" ?

Quoted 7 lines
Quoted 4 lines

Right, otherwise I guess that could be confused with the check on cpu_table_count_valid_entries() above. And while I'm thinking about it perhaps WARN is a bit too much here ? We can handle the error safely so pr_err() should be enough ?

Hmm, I would keep the WARN as it is hard to miss it compared to a simple pr_err.

Right, I don't really mind either way TBH. But is this worse than having a NULL policy for example ? We have a standard pr_err() in this case. And same thing if the cooling device registration failed actually. Do you see a good reason to deal with EM stuff differently ?

Thanks,
Quentin

#8 lore raw

Re: [PATCH v2 3/3] thermal: cpu_cooling: Migrate to using the EM framework

Quoted 31 lines · Quentin Perret
Quoted 30 lines · Viresh Kumar
Quoted 28 lines · Quentin Perret
Quoted 8 lines · Viresh Kumar
Quoted 6 lines · Quentin Perret

static struct thermal_cooling_device * __cpufreq_cooling_register(struct device_node *np,

drivers/thermal/cpu_cooling.c +2 -1
-			struct cpufreq_policy *policy, u32 capacitance)
+			struct cpufreq_policy *policy,
+			struct em_perf_domain *em)
 {
Quoted 10 lines

drivers/thermal/cpu_cooling.c __cpufreq_cooling_register(struct device_node *np, · line 612

Quoted 8 lines
drivers/thermal/cpu_cooling.c +5 -1
+	if (em_is_sane(cpufreq_cdev, em)) {
+		cpufreq_cdev->em = em;
 		cooling_ops = &cpufreq_power_cooling_ops;
-	} else {
+	} else if (policy->freq_table_sorted != CPUFREQ_TABLE_UNSORTED) {
 		cooling_ops = &cpufreq_cooling_ops;
+	} else {
+		WARN(1, "cpu_cooling: no valid frequency table found\n");

Well the frequency table is valid, isn't it ?

True ...

Quoted 1 line

Maybe something like: "cpu_cooling doesn't support unsorted frequency tables" ?

Right, otherwise I guess that could be confused with the check on cpu_table_count_valid_entries() above. And while I'm thinking about it perhaps WARN is a bit too much here ? We can handle the error safely so pr_err() should be enough ?

Quoted 7 lines
Quoted 2 lines

Hmm, I would keep the WARN as it is hard to miss it compared to a simple pr_err.

Right, I don't really mind either way TBH. But is this worse than having a NULL policy for example ? We have a standard pr_err() in this case. And same thing if the cooling device registration failed actually. Do you see a good reason to deal with EM stuff differently ?

Okay, that should be fine then. Use pr_err().

Signature · 1 line
viresh