Skip to contentSkip to conversation

cpu_cooling: Make IPA use PM_EM

PATCH Superseded Quentin Perret 3 participants 16 messages Mar 28, 2019 – Apr 10, 2019

Subsystems (2)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover cpu_cooling: Make IPA use PM_EM

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/cpu_cooling.c | 231 +++++++++++-----------------------
 include/linux/energy_model.h  |   3 +-
 3 files changed, 77 insertions(+), 158 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH 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..3c09bdaaefd3 100644
@@ -97,6 +97,7 @@ CONFIG_XEN=y
 CONFIG_COMPAT=y
 CONFIG_HIBERNATION=y
 CONFIG_WQ_POWER_EFFICIENT_DEFAULT=y
+CONFIG_ENERGY_MODEL=n
 CONFIG_ARM_CPUIDLE=y
 CONFIG_CPU_FREQ=y
 CONFIG_CPU_FREQ_STAT=y
Signature · 1 line
2.21.0
#7 lore raw

PATCH 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
#8 lore raw

PATCH 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.

---
 drivers/thermal/cpu_cooling.c | 231 +++++++++++-----------------------
 1 file changed, 75 insertions(+), 156 deletions(-)
drivers/thermal/cpu_cooling.c +75 -156
index f7c1f49ec87f..a74ec8269b7b 100644
@@ -31,6 +31,7 @@
 #include <linux/slab.h>
 #include <linux/cpu.h>
 #include <linux/cpu_cooling.h>
+#include <linux/energy_model.h>
 
 #include <trace/events/thermal.h>
 
@@ -48,19 +49,6 @@
  *	...
  */
 
-/**
- * struct freq_table - frequency table along with power entries
- * @frequency:	frequency in KHz
- * @power:	power in mW
- *
- * This structure is built when the cooling device registers and helps
- * in translating frequency to power and vice versa.
- */
-struct freq_table {
-	u32 frequency;
-	u32 power;
-};
-
 /**
  * struct time_in_idle - Idle time stats
  * @time: previous reading of the absolute time that this cpu was idle
@@ -82,7 +70,7 @@ struct time_in_idle {
  *	frequency.
  * @max_level: maximum cooling level. One less than total number of valid
  *	cpufreq frequencies.
- * @freq_table: Freq table in descending order of frequencies
+ * @em: Reference on the Energy Model of the device
  * @cdev: thermal_cooling_device pointer to keep track of the
  *	registered cooling device.
  * @policy: cpufreq policy.
@@ -98,7 +86,7 @@ struct cpufreq_cooling_device {
 	unsigned int cpufreq_state;
 	unsigned int clipped_freq;
 	unsigned int max_level;
-	struct freq_table *freq_table;	/* In descending order */
+	struct em_perf_domain *em;
 	struct thermal_cooling_device *cdev;
 	struct cpufreq_policy *policy;
 	struct list_head node;
@@ -121,14 +109,14 @@ static LIST_HEAD(cpufreq_cdev_list);
 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
 			       unsigned int freq)
 {
-	struct freq_table *freq_table = cpufreq_cdev->freq_table;
-	unsigned long level;
+	int i;
 
-	for (level = 1; level <= cpufreq_cdev->max_level; level++)
-		if (freq > freq_table[level].frequency)
+	for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
+		if (freq > cpufreq_cdev->em->table[i].frequency)
 			break;
+	}
 
-	return level - 1;
+	return cpufreq_cdev->max_level - i - 1;
 }
 
 /**
@@ -184,105 +172,30 @@ static int cpufreq_thermal_notifier(struct notifier_block *nb,
 	return NOTIFY_OK;
 }
 
-/**
- * update_freq_table() - Update the freq table with power numbers
- * @cpufreq_cdev:	the cpufreq cooling device in which to update the table
- * @capacitance: dynamic power coefficient for these cpus
- *
- * Update the freq table with power numbers.  This table will be used in
- * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and
- * frequency efficiently.  Power is stored in mW, frequency in KHz.  The
- * resulting table is in descending order.
- *
- * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
- * or -ENOMEM if we run out of memory.
- */
-static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
-			     u32 capacitance)
-{
-	struct freq_table *freq_table = cpufreq_cdev->freq_table;
-	struct dev_pm_opp *opp;
-	struct device *dev = NULL;
-	int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
-
-	dev = get_cpu_device(cpu);
-	if (unlikely(!dev)) {
-		dev_warn(&cpufreq_cdev->cdev->device,
-			 "No cpu device for cpu %d\n", cpu);
-		return -ENODEV;
-	}
-
-	num_opps = dev_pm_opp_get_opp_count(dev);
-	if (num_opps < 0)
-		return num_opps;
-
-	/*
-	 * The cpufreq table is also built from the OPP table and so the count
-	 * should match.
-	 */
-	if (num_opps != cpufreq_cdev->max_level + 1) {
-		dev_warn(dev, "Number of OPPs not matching with max_levels\n");
-		return -EINVAL;
-	}
-
-	for (i = 0; i <= cpufreq_cdev->max_level; i++) {
-		unsigned long freq = freq_table[i].frequency * 1000;
-		u32 freq_mhz = freq_table[i].frequency / 1000;
-		u64 power;
-		u32 voltage_mv;
-
-		/*
-		 * Find ceil frequency as 'freq' may be slightly lower than OPP
-		 * freq due to truncation while converting to kHz.
-		 */
-		opp = dev_pm_opp_find_freq_ceil(dev, &freq);
-		if (IS_ERR(opp)) {
-			dev_err(dev, "failed to get opp for %lu frequency\n",
-				freq);
-			return -EINVAL;
-		}
-
-		voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
-		dev_pm_opp_put(opp);
-
-		/*
-		 * Do the multiplication with MHz and millivolt so as
-		 * to not overflow.
-		 */
-		power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
-		do_div(power, 1000000000);
-
-		/* power is stored in mW */
-		freq_table[i].power = power;
-	}
-
-	return 0;
-}
-
 static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
 			     u32 freq)
 {
 	int i;
-	struct freq_table *freq_table = cpufreq_cdev->freq_table;
 
-	for (i = 1; i <= cpufreq_cdev->max_level; i++)
-		if (freq > freq_table[i].frequency)
+	for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
+		if (freq > cpufreq_cdev->em->table[i].frequency)
 			break;
+	}
 
-	return freq_table[i - 1].power;
+	return cpufreq_cdev->em->table[i + 1].power;
 }
 
 static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
 			     u32 power)
 {
 	int i;
-	struct freq_table *freq_table = cpufreq_cdev->freq_table;
 
-	for (i = 1; i <= cpufreq_cdev->max_level; i++)
-		if (power > freq_table[i].power)
+	for (i = cpufreq_cdev->max_level - 1; i >= 0; i--) {
+		if (power > cpufreq_cdev->em->table[i].power)
 			break;
+	}
 
-	return freq_table[i - 1].frequency;
+	return cpufreq_cdev->em->table[i + 1].frequency;
 }
 
 /**
@@ -374,6 +287,28 @@ static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
 	return 0;
 }
 
+static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
+			      unsigned long state)
+{
+	struct cpufreq_policy *policy;
+	unsigned long idx;
+
+	/* Use the Energy Model table if available */
+	if (cpufreq_cdev->em) {
+		idx = cpufreq_cdev->max_level - state;
+		return cpufreq_cdev->em->table[idx].frequency;
+	}
+
+	/* Otherwise, fallback on the CPUFreq table */
+	policy = cpufreq_cdev->policy;
+	if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
+		idx = cpufreq_cdev->max_level - state;
+	else
+		idx = state;
+
+	return policy->freq_table[idx].frequency;
+}
+
 /**
  * cpufreq_set_cur_state - callback function to set the current cooling state.
  * @cdev: thermal cooling device pointer.
@@ -398,7 +333,7 @@ static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
 	if (cpufreq_cdev->cpufreq_state == state)
 		return 0;
 
-	clip_freq = cpufreq_cdev->freq_table[state].frequency;
+	clip_freq = get_state_freq(cpufreq_cdev, state);
 	cpufreq_cdev->cpufreq_state = state;
 	cpufreq_cdev->clipped_freq = clip_freq;
 
@@ -497,7 +432,7 @@ static int cpufreq_state2power(struct thermal_cooling_device *cdev,
 			       struct thermal_zone_device *tz,
 			       unsigned long state, u32 *power)
 {
-	unsigned int freq, num_cpus;
+	unsigned int freq, num_cpus, idx;
 	struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
 
 	/* Request state should be less than max_level */
@@ -506,7 +441,8 @@ static int cpufreq_state2power(struct thermal_cooling_device *cdev,
 
 	num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
 
-	freq = cpufreq_cdev->freq_table[state].frequency;
+	idx = cpufreq_cdev->max_level - state;
+	freq = cpufreq_cdev->em->table[idx].frequency;
 	*power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
 
 	return 0;
@@ -559,7 +495,6 @@ static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
 	.get_cur_state = cpufreq_get_cur_state,
 	.set_cur_state = cpufreq_set_cur_state,
 };
-
 static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
 	.get_max_state		= cpufreq_get_max_state,
 	.get_cur_state		= cpufreq_get_cur_state,
@@ -574,18 +509,31 @@ static struct notifier_block thermal_cpufreq_notifier_block = {
 	.notifier_call = cpufreq_thermal_notifier,
 };
 
-static unsigned int find_next_max(struct cpufreq_frequency_table *table,
-				  unsigned int prev_max)
-{
-	struct cpufreq_frequency_table *pos;
-	unsigned int max = 0;
+static inline bool em_is_sane(struct cpufreq_cooling_device *cpufreq_cdev,
+			      struct em_perf_domain *em) {
+	struct cpufreq_policy *policy;
+	unsigned int nr_levels;
+
+	if (!em)
+		return false;
+
+	policy = cpufreq_cdev->policy;
+	if (!cpumask_equal(policy->related_cpus, to_cpumask(em->cpus))) {
+		pr_err("The span of pd %*pbl is misaligned with cpufreq policy %*pbl\n",
+			cpumask_pr_args(to_cpumask(em->cpus)),
+			cpumask_pr_args(policy->related_cpus));
+		return false;
+	}
 
-	cpufreq_for_each_valid_entry(pos, table) {
-		if (pos->frequency > max && pos->frequency < prev_max)
-			max = pos->frequency;
+	nr_levels = cpufreq_cdev->max_level + 1;
+	if (em->nr_cap_states != nr_levels) {
+		pr_err("The number of cap states in pd %*pbl (%u) doesn't match the number of cooling levels (%u)\n",
+			cpumask_pr_args(to_cpumask(em->cpus)),
+			em->nr_cap_states, nr_levels);
+		return false;
 	}
 
-	return max;
+	return true;
 }
 
 /**
@@ -593,7 +541,7 @@ static unsigned int find_next_max(struct cpufreq_frequency_table *table,
  * @np: a valid struct device_node to the cooling device device tree node
  * @policy: cpufreq policy
  * Normally this should be same as cpufreq policy->related_cpus.
- * @capacitance: dynamic power coefficient for these cpus
+ * @em: Energy Model of the cpufreq policy
  *
  * This interface function registers the cpufreq cooling device with the name
  * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
@@ -605,12 +553,13 @@ static unsigned int find_next_max(struct cpufreq_frequency_table *table,
  */
 static struct thermal_cooling_device *
 __cpufreq_cooling_register(struct device_node *np,
-			struct cpufreq_policy *policy, u32 capacitance)
+			struct cpufreq_policy *policy,
+			struct em_perf_domain *em)
 {
 	struct thermal_cooling_device *cdev;
 	struct cpufreq_cooling_device *cpufreq_cdev;
 	char dev_name[THERMAL_NAME_LENGTH];
-	unsigned int freq, i, num_cpus;
+	unsigned int i, num_cpus;
 	int ret;
 	struct thermal_cooling_device_ops *cooling_ops;
 	bool first;
@@ -644,43 +593,18 @@ __cpufreq_cooling_register(struct device_node *np,
 	/* max_level is an index, not a counter */
 	cpufreq_cdev->max_level = i - 1;
 
-	cpufreq_cdev->freq_table = kmalloc_array(i,
-					sizeof(*cpufreq_cdev->freq_table),
-					GFP_KERNEL);
-	if (!cpufreq_cdev->freq_table) {
-		cdev = ERR_PTR(-ENOMEM);
-		goto free_idle_time;
-	}
-
 	ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
 	if (ret < 0) {
 		cdev = ERR_PTR(ret);
-		goto free_table;
+		goto free_idle_time;
 	}
 	cpufreq_cdev->id = ret;
 
 	snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
 		 cpufreq_cdev->id);
 
-	/* Fill freq-table in descending order of frequencies */
-	for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
-		freq = find_next_max(policy->freq_table, freq);
-		cpufreq_cdev->freq_table[i].frequency = freq;
-
-		/* Warn for duplicate entries */
-		if (!freq)
-			pr_warn("%s: table has duplicate entries\n", __func__);
-		else
-			pr_debug("%s: freq:%u KHz\n", __func__, freq);
-	}
-
-	if (capacitance) {
-		ret = update_freq_table(cpufreq_cdev, capacitance);
-		if (ret) {
-			cdev = ERR_PTR(ret);
-			goto remove_ida;
-		}
-
+	if (em_is_sane(cpufreq_cdev, em)) {
+		cpufreq_cdev->em = em;
 		cooling_ops = &cpufreq_power_cooling_ops;
 	} else {
 		cooling_ops = &cpufreq_cooling_ops;
@@ -691,7 +615,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 +632,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 +653,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 +681,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 +689,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 +733,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
#2 lore raw

Re: [PATCH 1/3] arm64: defconfig: Enable CONFIG_ENERGY_MODEL

In reply to Quentin Perret
Quoted 25 lines · Quentin Perret

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 +0 -0
index 2d9c39033c1a..3c09bdaaefd3 100644

arch/arm64/configs/defconfig CONFIG_XEN=y · line 100

Quoted 4 lines
arch/arm64/configs/defconfig +1 -0
@@ -97,6 +97,7 @@ CONFIG_XEN=y
 CONFIG_COMPAT=y
 CONFIG_HIBERNATION=y
 CONFIG_WQ_POWER_EFFICIENT_DEFAULT=y
+CONFIG_ENERGY_MODEL=n

Hmm, sorry I turned this to '=n' for testing and forgot to update the patch. This obviously should be '=y' ...

Quoted 6 lines
arch/arm64/configs/defconfig +0 -0
 CONFIG_ARM_CPUIDLE=y
 CONFIG_CPU_FREQ=y
 CONFIG_CPU_FREQ_STAT=y
Signature · 1 line
2.21.0
#3 lore raw

Re: [PATCH 1/3] arm64: defconfig: Enable CONFIG_ENERGY_MODEL

Quoted 21 lines · Quentin Perret
Quoted 20 lines · Quentin Perret

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(+)
Quoted 12 lines

arch/arm64/configs/defconfig CONFIG_XEN=y · line 100

Quoted 9 lines
arch/arm64/configs/defconfig +1 -0
index 2d9c39033c1a..3c09bdaaefd3 100644
@@ -97,6 +97,7 @@ CONFIG_XEN=y
 CONFIG_COMPAT=y
 CONFIG_HIBERNATION=y
 CONFIG_WQ_POWER_EFFICIENT_DEFAULT=y
+CONFIG_ENERGY_MODEL=n

Hmm, sorry I turned this to '=n' for testing and forgot to update the patch. This obviously should be '=y' ...

I did a test without the ENERGY_MODEL config option set, dhrystone and the power_allocator policy on the hikey. The board did not mitigate well and ended up rebooting. May be the cpu cooling Kconfig option should add a SELECT or a DEPENDS on ENERGY_MODEL ?

Quoted 6 lines
Quoted 6 lines
arch/arm64/configs/defconfig +0 -0
 CONFIG_ARM_CPUIDLE=y
 CONFIG_CPU_FREQ=y
 CONFIG_CPU_FREQ_STAT=y
Signature · 1 line
2.21.0
Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#4 lore raw

Re: [PATCH 1/3] arm64: defconfig: Enable CONFIG_ENERGY_MODEL

Hi Daniel,

Quoted 14 lines · Daniel Lezcano
Quoted 4 lines · Quentin Perret
Quoted 4 lines
arch/arm64/configs/defconfig +0 -0
index 2d9c39033c1a..3c09bdaaefd3 100644
Quoted 8 lines

arch/arm64/configs/defconfig CONFIG_XEN=y · line 100

Quoted 5 lines
arch/arm64/configs/defconfig +1 -0
@@ -97,6 +97,7 @@ CONFIG_XEN=y
 CONFIG_COMPAT=y
 CONFIG_HIBERNATION=y
 CONFIG_WQ_POWER_EFFICIENT_DEFAULT=y
+CONFIG_ENERGY_MODEL=n

Hmm, sorry I turned this to '=n' for testing and forgot to update the patch. This obviously should be '=y' ...

Quoted 3 lines

I did a test without the ENERGY_MODEL config option set, dhrystone and the power_allocator policy on the hikey. The board did not mitigate well and ended up rebooting.

OK ... And is the same thing happening if you just run mainline w/o the dynamic-power-coefficient binding set for example ? The result _should_ be the same. If not, then perhaps I missed something. I'll try to reproduce on my end. Just to be sure, when you say hikey, you mean hikey960 ? Or 620 ? In any case, thanks for testing :-)

Quoted 2 lines

May be the cpu cooling Kconfig option should add
a SELECT or a DEPENDS on ENERGY_MODEL ?

Right, I've been wondering the same thing. I'm not a big fan of 'select' because enabling ENERGY_MODEL automatically for the thermal stuff will also happen to enable other things (EAS) without the user knowing. So I'd rather keep the ENERGY_MODEL option explicit.

But perhaps having THERMAL_GOV_POWER_ALLOCATOR 'depend on ENERGY_MODEL' could work. It's just that there is no _strong_ dependency, the IPA code isn't supposed to crash even if there is no EM ...

Thanks,
Quentin

#5 lore raw

Re: [PATCH 1/3] arm64: defconfig: Enable CONFIG_ENERGY_MODEL

Quoted 17 lines · Quentin Perret

Hi Daniel,

Quoted 14 lines · Daniel Lezcano
Quoted 12 lines · Quentin Perret

arch/arm64/configs/defconfig CONFIG_XEN=y · line 100

Quoted 9 lines
arch/arm64/configs/defconfig +1 -0
index 2d9c39033c1a..3c09bdaaefd3 100644
@@ -97,6 +97,7 @@ CONFIG_XEN=y
 CONFIG_COMPAT=y
 CONFIG_HIBERNATION=y
 CONFIG_WQ_POWER_EFFICIENT_DEFAULT=y
+CONFIG_ENERGY_MODEL=n

Hmm, sorry I turned this to '=n' for testing and forgot to update the patch. This obviously should be '=y' ...

Quoted 7 lines
Quoted 3 lines

I did a test without the ENERGY_MODEL config option set, dhrystone and the power_allocator policy on the hikey. The board did not mitigate well and ended up rebooting.

OK ... And is the same thing happening if you just run mainline w/o the dynamic-power-coefficient binding set for example ? The result _should_ be the same.

Right, it is the same.

Quoted 3 lines

If not, then perhaps I missed something. I'll try to reproduce on my end. Just to be sure, when you say hikey, you mean hikey960 ? Or 620 ? In any case, thanks for testing :-)

hikey620

Quoted 11 lines
Quoted 2 lines

May be the cpu cooling Kconfig option should add
a SELECT or a DEPENDS on ENERGY_MODEL ?

Right, I've been wondering the same thing. I'm not a big fan of 'select' because enabling ENERGY_MODEL automatically for the thermal stuff will also happen to enable other things (EAS) without the user knowing. So I'd rather keep the ENERGY_MODEL option explicit.

But perhaps having THERMAL_GOV_POWER_ALLOCATOR 'depend on ENERGY_MODEL' could work. It's just that there is no _strong_ dependency, the IPA code isn't supposed to crash even if there is no EM ...

Given if the ENERGY_MODEL is not set there is a regression we should add the dependency IMO.

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#9 lore raw

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

In reply to Quentin Perret
Quoted 62 lines · 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.

---
 drivers/thermal/cpu_cooling.c | 231 +++++++++++-----------------------
 1 file changed, 75 insertions(+), 156 deletions(-)
drivers/thermal/cpu_cooling.c +2 -14
index f7c1f49ec87f..a74ec8269b7b 100644
@@ -31,6 +31,7 @@
 #include <linux/slab.h>
 #include <linux/cpu.h>
 #include <linux/cpu_cooling.h>
+#include <linux/energy_model.h>
 
 #include <trace/events/thermal.h>
 
@@ -48,19 +49,6 @@
  *	...
  */
 
-/**
- * struct freq_table - frequency table along with power entries
- * @frequency:	frequency in KHz
- * @power:	power in mW
- *
- * This structure is built when the cooling device registers and helps
- * in translating frequency to power and vice versa.
- */
-struct freq_table {
-	u32 frequency;
-	u32 power;
-};
-
 /**
  * struct time_in_idle - Idle time stats
  * @time: previous reading of the absolute time that this cpu was idle
@@ -82,7 +70,7 @@ struct time_in_idle {
  *	frequency.
  * @max_level: maximum cooling level. One less than total number of valid
  *	cpufreq frequencies.
- * @freq_table: Freq table in descending order of frequencies
+ * @em: Reference on the Energy Model of the device
  * @cdev: thermal_cooling_device pointer to keep track of the
  *	registered cooling device.
  * @policy: cpufreq policy.

drivers/thermal/cpu_cooling.c struct cpufreq_cooling_device { · line 89

Quoted 5 lines
drivers/thermal/cpu_cooling.c +1 -1
@@ -98,7 +86,7 @@ struct cpufreq_cooling_device {
 	unsigned int cpufreq_state;
 	unsigned int clipped_freq;
 	unsigned int max_level;
-	struct freq_table *freq_table;	/* In descending order */
+	struct em_perf_domain *em;

Why do you need to add this field? it will be accessible via policy->em, no?

Quoted 5 lines
drivers/thermal/cpu_cooling.c +0 -0
 	struct thermal_cooling_device *cdev;
 	struct cpufreq_policy *policy;
 	struct list_head node;
@@ -121,14 +109,14 @@ static LIST_HEAD(cpufreq_cdev_list);
 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,

[ ... ]

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#6 lore raw

Re: [PATCH 1/3] arm64: defconfig: Enable CONFIG_ENERGY_MODEL

In reply to Daniel Lezcano
Quoted 22 lines · Daniel Lezcano
Quoted 21 lines · Quentin Perret

Hi Daniel,

Quoted 17 lines · Daniel Lezcano
Quoted 4 lines · Quentin Perret
Quoted 4 lines
arch/arm64/configs/defconfig +0 -0
index 2d9c39033c1a..3c09bdaaefd3 100644
Quoted 8 lines

arch/arm64/configs/defconfig CONFIG_XEN=y · line 100

Quoted 5 lines
arch/arm64/configs/defconfig +1 -0
@@ -97,6 +97,7 @@ CONFIG_XEN=y
 CONFIG_COMPAT=y
 CONFIG_HIBERNATION=y
 CONFIG_WQ_POWER_EFFICIENT_DEFAULT=y
+CONFIG_ENERGY_MODEL=n

Hmm, sorry I turned this to '=n' for testing and forgot to update the patch. This obviously should be '=y' ...

I did a test without the ENERGY_MODEL config option set, dhrystone and the power_allocator policy on the hikey. The board did not mitigate well and ended up rebooting.

Quoted 5 lines
Quoted 3 lines

OK ... And is the same thing happening if you just run mainline w/o the dynamic-power-coefficient binding set for example ? The result _should_ be the same.

Right, it is the same.

OK.

Quoted 15 lines
Quoted 4 lines

If not, then perhaps I missed something. I'll try to reproduce on my end. Just to be sure, when you say hikey, you mean hikey960 ? Or 620 ? In any case, thanks for testing :-)

hikey620

Quoted 8 lines
Quoted 2 lines

May be the cpu cooling Kconfig option should add
a SELECT or a DEPENDS on ENERGY_MODEL ?

Right, I've been wondering the same thing. I'm not a big fan of 'select' because enabling ENERGY_MODEL automatically for the thermal stuff will also happen to enable other things (EAS) without the user knowing. So I'd rather keep the ENERGY_MODEL option explicit.

Quoted 6 lines
Quoted 3 lines

But perhaps having THERMAL_GOV_POWER_ALLOCATOR 'depend on ENERGY_MODEL' could work. It's just that there is no _strong_ dependency, the IPA code isn't supposed to crash even if there is no EM ...

Given if the ENERGY_MODEL is not set there is a regression we should add the dependency IMO.

Right, that is true. And there are folks who don't use the arm64 defconfig downstream, so I guess we need to make it clear for them they need to enable ENERGY_MODEL from now on.

I'll add a 'depends on ENERGY_MODEL' to 'THERMAL_GOV_POWER_ALLOCATOR' in patch 3 for v2.

Thanks,
Quentin

#10 lore raw

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

In reply to Daniel Lezcano

Hi Daniel,

Quoted 12 lines · Daniel Lezcano
Quoted 12 lines

/**
* struct time_in_idle - Idle time stats
* @time: previous reading of the absolute time that this cpu was idle

drivers/thermal/cpu_cooling.c +1 -1
@@ -82,7 +70,7 @@ struct time_in_idle {
  *	frequency.
  * @max_level: maximum cooling level. One less than total number of valid
  *	cpufreq frequencies.
- * @freq_table: Freq table in descending order of frequencies
+ * @em: Reference on the Energy Model of the device
  * @cdev: thermal_cooling_device pointer to keep track of the
  *	registered cooling device.
  * @policy: cpufreq policy.
Quoted 8 lines

drivers/thermal/cpu_cooling.c struct cpufreq_cooling_device { · line 89

Quoted 6 lines
drivers/thermal/cpu_cooling.c +1 -1
@@ -98,7 +86,7 @@ struct cpufreq_cooling_device {
 	unsigned int cpufreq_state;
 	unsigned int clipped_freq;
 	unsigned int max_level;
-	struct freq_table *freq_table;	/* In descending order */
+	struct em_perf_domain *em;

Why do you need to add this field? it will be accessible via policy->em, no?

You mean via the CPUFreq policy ? Then no, the EM isn't attached to the CPUFreq policy. And we can't attach it directly to the CPUFreq policy since in *theory* it is not required to map 1:1 to CPUFreq policies (even though that _is_ true for all existing platforms). That's one of the things this patch checks in that em_is_sane() function below.

FWIW, the idea of the design is, the EM framework is 'independent' and it's up to the client subsystems (scheduler, IPA) to check if it actually works for them. In the case of the scheduler, for example, we can't use an EM that's too complex because that would cause too much overhead, so we don't start EAS if that's not the case. See:

https://elixir.bootlin.com/linux/latest/source/kernel/sched/topology.c#L367

In the case of IPA, we need to do something similar. We can't use an EM that doesn't map 1:1 to CPUFreq policies, so we bail out if that's not true, etc, ... This isn't supposed to trigger any time soon, but it's good to have a check just to be on the safe side I think.

Quoted 6 lines

drivers/thermal/cpu_cooling.c static LIST_HEAD(cpufreq_cdev_list); · line 109

Quoted 6 lines
drivers/thermal/cpu_cooling.c +0 -0
 	struct thermal_cooling_device *cdev;
 	struct cpufreq_policy *policy;
 	struct list_head node;
@@ -121,14 +109,14 @@ static LIST_HEAD(cpufreq_cdev_list);
 static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,

Thanks,
Quentin

#11 lore raw

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

Quoted 36 lines · Quentin Perret

Hi Daniel,

Quoted 12 lines · Daniel Lezcano
Quoted 12 lines

/**
* struct time_in_idle - Idle time stats
* @time: previous reading of the absolute time that this cpu was idle

drivers/thermal/cpu_cooling.c +1 -1
@@ -82,7 +70,7 @@ struct time_in_idle {
  *	frequency.
  * @max_level: maximum cooling level. One less than total number of valid
  *	cpufreq frequencies.
- * @freq_table: Freq table in descending order of frequencies
+ * @em: Reference on the Energy Model of the device
  * @cdev: thermal_cooling_device pointer to keep track of the
  *	registered cooling device.
  * @policy: cpufreq policy.
Quoted 8 lines

drivers/thermal/cpu_cooling.c struct cpufreq_cooling_device { · line 89

Quoted 6 lines
drivers/thermal/cpu_cooling.c +1 -1
@@ -98,7 +86,7 @@ struct cpufreq_cooling_device {
 	unsigned int cpufreq_state;
 	unsigned int clipped_freq;
 	unsigned int max_level;
-	struct freq_table *freq_table;	/* In descending order */
+	struct em_perf_domain *em;

Why do you need to add this field? it will be accessible via policy->em, no?

You mean via the CPUFreq policy ? Then no, the EM isn't attached to the CPUFreq policy. And we can't attach it directly to the CPUFreq policy since in *theory* it is not required to map 1:1 to CPUFreq policies (even though that _is_ true for all existing platforms). That's one of the things this patch checks in that em_is_sane() function below.

FWIW, the idea of the design is, the EM framework is 'independent' and it's up to the client subsystems (scheduler, IPA) to check if it actually works for them. In the case of the scheduler, for example, we can't use an EM that's too complex because that would cause too much overhead, so we don't start EAS if that's not the case. See:

Quoted 6 lines

https://elixir.bootlin.com/linux/latest/source/kernel/sched/topology.c#L367

In the case of IPA, we need to do something similar. We can't use an EM that doesn't map 1:1 to CPUFreq policies, so we bail out if that's not true, etc, ... This isn't supposed to trigger any time soon, but it's good to have a check just to be on the safe side I think.

Ok, makes sense. Thanks for the clarification.

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#12 lore raw

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

In reply to Quentin Perret
Quoted 5 lines · Quentin Perret
drivers/thermal/cpu_cooling.c +5 -0
+static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
+			      unsigned long state)
+{
+	struct cpufreq_policy *policy;
+	unsigned long idx;

drivers/thermal/cpu_cooling.c static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, · line 304

Quoted 10 lines
drivers/thermal/cpu_cooling.c +10 -0
+
+	/* Use the Energy Model table if available */
+	if (cpufreq_cdev->em) {
+		idx = cpufreq_cdev->max_level - state;
+		return cpufreq_cdev->em->table[idx].frequency;
+	}
+
+	/* Otherwise, fallback on the CPUFreq table */
+	policy = cpufreq_cdev->policy;
+	if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)

It is not guaranteed that the frequency table is sorted in any order, isn't it ?

Quoted 3 lines
drivers/thermal/cpu_cooling.c +3 -0
+		idx = cpufreq_cdev->max_level - state;
+	else
+		idx = state;
Signature · 1 line
viresh
#13 lore raw

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

Quoted 10 lines · Viresh Kumar
Quoted 9 lines · Quentin Perret
drivers/thermal/cpu_cooling.c +9 -0
+static unsigned int get_state_freq(struct cpufreq_cooling_device *cpufreq_cdev,
+			      unsigned long state)
+{
+	struct cpufreq_policy *policy;
+	unsigned long idx;
+
+	/* Use the Energy Model table if available */
+	if (cpufreq_cdev->em) {
+		idx = cpufreq_cdev->max_level - state;
Quoted 8 lines

drivers/thermal/cpu_cooling.c static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, · line 304

Quoted 6 lines
drivers/thermal/cpu_cooling.c +6 -0
+		return cpufreq_cdev->em->table[idx].frequency;
+	}
+
+	/* Otherwise, fallback on the CPUFreq table */
+	policy = cpufreq_cdev->policy;
+	if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)

It is not guaranteed that the frequency table is sorted in any order, isn't it ?

Hmm, indeed... I thought cpufreq_table_validate_and_sort() was actively sorting the table but it seems I was wrong.

But I _think_ in practice the freq table actually happens to be sorted for the upstream cpufreq drivers with the CPUFREQ_IS_COOLING_DEV flag set. Most of them use dev_pm_opp_init_cpufreq_table() which guarantees the table is sorted and qoriq-cpufreq explicitly sorts the table. But I'm not sure about qcom-cpufreq-hw ...

So, if the above is true, perhaps I could simply add a check to mandate that policy->freq_table_sorted != CPUFREQ_TABLE_SORTED_UNSORTED for cpu_cooling ? That shouldn't harm the existing users.

Do you happen to know a board where the table is unsorted ? Is it a common use-case ?

If yes, then I'll probably need to drop the dependency on cpufreq's freq_table and use something else to convert indexes into frequencies (PM_OPP ?). Unless we can force-sort the table in the cpufreq core, but that might require lots of changes to lots of drivers too.

Quoted 7 lines
Quoted 4 lines
drivers/thermal/cpu_cooling.c +3 -0
+		idx = cpufreq_cdev->max_level - state;
+	else
+		idx = state;
Signature · 1 line
viresh

Thanks,
Quentin

#14 lore raw

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

Quoted 9 lines · Quentin Perret

Hmm, indeed... I thought cpufreq_table_validate_and_sort() was actively sorting the table but it seems I was wrong.

But I _think_ in practice the freq table actually happens to be sorted for the upstream cpufreq drivers with the CPUFREQ_IS_COOLING_DEV flag set. Most of them use dev_pm_opp_init_cpufreq_table() which guarantees the table is sorted and qoriq-cpufreq explicitly sorts the table. But I'm not sure about qcom-cpufreq-hw ...

Quoted 3 lines

So, if the above is true, perhaps I could simply add a check to mandate that policy->freq_table_sorted != CPUFREQ_TABLE_SORTED_UNSORTED for cpu_cooling ? That shouldn't harm the existing users.

Right, I think most of the platforms will have it sorted anyway right now, but you never know if one or two of them don't. Maybe just add the above conditional and put out an error or WARN or something, so people know that something broke.

Signature · 1 line
viresh
#15 lore raw

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

Quoted 10 lines · Viresh Kumar
Quoted 9 lines · Quentin Perret

Hmm, indeed... I thought cpufreq_table_validate_and_sort() was actively sorting the table but it seems I was wrong.

But I _think_ in practice the freq table actually happens to be sorted for the upstream cpufreq drivers with the CPUFREQ_IS_COOLING_DEV flag set. Most of them use dev_pm_opp_init_cpufreq_table() which guarantees the table is sorted and qoriq-cpufreq explicitly sorts the table. But I'm not sure about qcom-cpufreq-hw ...

Quoted 7 lines
Quoted 3 lines

So, if the above is true, perhaps I could simply add a check to mandate that policy->freq_table_sorted != CPUFREQ_TABLE_SORTED_UNSORTED for cpu_cooling ? That shouldn't harm the existing users.

Right, I think most of the platforms will have it sorted anyway right now, but you never know if one or two of them don't. Maybe just add the above conditional and put out an error or WARN or something, so people know that something broke.

Right, WARN + bail out should do it. I'll do the change in v2.

Thanks !
Quentin