Hi all,
This patch set adds a new feature which allows to modify Energy Model (EM)
power values at runtime. It will allow to better reflect power model of
a recent SoCs and silicon. Different characteristics of the power usage
can be leveraged and thus better decisions made during task placement in EAS.
It's part of feature set know as Dynamic Energy Model. It has been presented
and discussed recently at OSPM2023 [3]. This patch set implements the 1st
improvement for the EM.
The concepts:
1. The CPU power usage can vary due to the workload that it's running or due
to the temperature of the SoC. The same workload can use more power when the
temperature of the silicon has increased (e.g. due to hot GPU or ISP).
In such situation or EM can be adjusted and reflect the fact of increased
power usage. That power increase is due to a factor called static power
(sometimes called simply: leakage). The CPUs in recent SoCs are different.
We have heterogeneous SoCs with 3 (or even 4) different microarchitectures.
They are also built differently with High Performance (HP) cells or
Low Power (LP) cells. They are affected by the temperature increase
differently: HP cells have bigger leakage. The SW model can leverage that
knowledge.
2. It is also possible to change the EM to better reflect the currently
running workload. Usually the EM is derived from some average power values
taken from experiments with benchmark (e.g. Dhrystone). The model derived
from such scenario might not represent properly the workloads usually running
on the device. Therefore, runtime modification of the EM allows to switch to
a different model, when there is a need.
3. The EM can be adjusted after boot, when all the modules are loaded and
more information about the SoC is available e.g. chip binning. This would help
to better reflect the silicon characteristics. Thus, this EM modification
API allows it now. It wasn't possible in the past and the EM had to be
'set in stone'.
Some design details:
The internal mechanisms for the memory allocation are handled internally in the
EM. Kernel modules can just call the new API to update the EM data and the
new memory would be provided and owned by the EM. The EM memory is used by
EAS, which impacts those design decisions. The EM writers are protected by
a mutex. This new runtime modified EM table is protected using RCU mechanism,
which fits the current EAS hot path (which already uses RCU read lock).
The unregister API handles only non-CPU (e.g. GPU, ISP) devices and uses the
same mutex as EM modifiers to make sure the memory is safely freed.
More detailed explanation and background can be found in presentations
during LPC2022 [1][2] or in the documentation patches.
The time cost to update EM for 11 OPPs can be found here [6]. It's roughly
1.5us per 1 OPP while doing this on Little CPU at max frequency (1.8GHz).
Changelog:
v3
- adjusted inline comments for structure doc (Dietmar)
- extended patch header with infromation that only EAS will use the feature
and it was driving the design (Dietmar)
- changed patch header and put shorter comment (Dietmar)
- moved the 'refactoring' patch that adds 'default_table' before the
introduction of runtime modifiable feature as sugested by Dietmar in
numerous patches v2
- merged documentation patches into one
- added more explenation about the 2 tables design into the Doc (Dietmar)
- removed the CPPC+EM patch for runtime modification
- removed the trace patch, since the trace point would be added after a while
- renamed 'tmp' to 'runtime_table' variable in the unregister function,
to better highlight the memory pointer checks (it is possible after
moving the 'default_table' earlier)
- and added '__rcu' in the unregister function which should calm down
the test bot warning
- renamed 'create' to 'refactor' in the patch header (Dietmar)
v2 [5]:
- solved build warning of unused variable in patch 13/17 when EM is
not compiled in, e.g. on Intel platform for this cpufreq_cooling
- re-based on top of v6.4-rc1
v1:
- implementation can be found here [4]
Regards,
Lukasz Luba
[1] https://lpc.events/event/16/contributions/1341/attachments/955/1873/Dynamic_Energy_Model_to_handle_leakage_power.pdf
[2] https://lpc.events/event/16/contributions/1194/attachments/1114/2139/LPC2022_Energy_model_accuracy.pdf
[3] https://www.youtube.com/watch?v=2C-5uikSbtM&list=PL0fKordpLTjKsBOUcZqnzlHShri4YBL1H
[4] https://lore.kernel.org/lkml/[email protected]/
[5] https://lore.kernel.org/lkml/[email protected]/
[6] https://lore.kernel.org/lkml/[email protected]/
Lukasz Luba (12):
PM: EM: Refactor em_cpufreq_update_efficiencies() arguments
PM: EM: Find first CPU online while updating OPP efficiency
PM: EM: Refactor em_pd_get_efficient_state() to be more flexible
PM: EM: Refactor a new function em_compute_costs()
PM: EM: Check if the get_cost() callback is present in
em_compute_costs()
PM: EM: Refactor struct em_perf_domain and add default_table
PM: EM: Add update_power() callback for runtime modifications
PM: EM: Introduce runtime modifiable table
PM: EM: Add RCU mechanism which safely cleans the old data
PM: EM: Add runtime update interface to modify EM power
PM: EM: Use runtime modified EM for CPUs energy estimation in EAS
Documentation: EM: Update with runtime modification design
Documentation/power/energy-model.rst | 150 +++++++++++++-
drivers/powercap/dtpm_cpu.c | 27 ++-
drivers/powercap/dtpm_devfreq.c | 23 ++-
drivers/thermal/cpufreq_cooling.c | 23 ++-
drivers/thermal/devfreq_cooling.c | 23 ++-
include/linux/energy_model.h | 85 ++++++--
kernel/power/energy_model.c | 288 +++++++++++++++++++++++----
7 files changed, 525 insertions(+), 94 deletions(-)
--
2.25.1
The EM is going to support runtime modifications of the power data.
Introduce RCU safe mechanism to clean up the old allocated EM data.
It also adds a mutex for the EM structure to serialize the modifiers.
Signed-off-by: Lukasz Luba <[email protected]>
---
kernel/power/energy_model.c | 42 +++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index c2f8a0046f8a..4596bfe7398e 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -23,6 +23,9 @@
*/
static DEFINE_MUTEX(em_pd_mutex);
+static void em_cpufreq_update_efficiencies(struct device *dev,
+ struct em_perf_state *table);
+
static bool _is_cpu_device(struct device *dev)
{
return (dev->bus == &cpu_subsys);
@@ -104,6 +107,45 @@ static void em_debug_create_pd(struct device *dev) {}
static void em_debug_remove_pd(struct device *dev) {}
#endif
+static void em_destroy_rt_table_rcu(struct rcu_head *rp)
+{
+ struct em_perf_table *runtime_table;
+
+ runtime_table = container_of(rp, struct em_perf_table, rcu);
+ kfree(runtime_table->state);
+ kfree(runtime_table);
+}
+
+static void em_destroy_tmp_setup_rcu(struct rcu_head *rp)
+{
+ struct em_perf_table *runtime_table;
+
+ runtime_table = container_of(rp, struct em_perf_table, rcu);
+ kfree(runtime_table);
+}
+
+static void em_perf_runtime_table_set(struct device *dev,
+ struct em_perf_table *runtime_table)
+{
+ struct em_perf_domain *pd = dev->em_pd;
+ struct em_perf_table *tmp;
+
+ tmp = pd->runtime_table;
+
+ rcu_assign_pointer(pd->runtime_table, runtime_table);
+
+ em_cpufreq_update_efficiencies(dev, runtime_table->state);
+
+ /*
+ * Check if the 'state' array is not actually the one from setup.
+ * If it is then don't free it.
+ */
+ if (tmp->state == pd->default_table->state)
+ call_rcu(&tmp->rcu, em_destroy_tmp_setup_rcu);
+ else
+ call_rcu(&tmp->rcu, em_destroy_rt_table_rcu);
+}
+
static int em_compute_costs(struct device *dev, struct em_perf_state *table,
struct em_data_callback *cb, int nr_states,
unsigned long flags)
--
2.25.1
This patch introduces the new feature: modifiable EM perf_state table.
The new runtime table would be populated with a new power data to better
reflect the actual power. The power can vary over time e.g. due to the
SoC temperature change. Higher temperature can increase power values.
For longer running scenarios, such as game or camera, when also other
devices are used (e.g. GPU, ISP) the CPU power can change. The new
EM framework is able to addresses this issue and change the data
at runtime safely.
The runtime modifiable EM data is used by the Energy Aware Scheduler (EAS)
for the task placement. The EAS is the only user of the 'runtime
modifiable EM'. All the other users (thermal, etc.) are still using the
default (basic) EM. This fact drove the design of this feature.
Signed-off-by: Lukasz Luba <[email protected]>
---
include/linux/energy_model.h | 4 +++-
kernel/power/energy_model.c | 26 ++++++++++++++++++++++++++
2 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h
index 9b67f54ddcf0..cfb1759ffd45 100644
--- a/include/linux/energy_model.h
+++ b/include/linux/energy_model.h
@@ -39,7 +39,7 @@ struct em_perf_state {
/**
* struct em_perf_table - Performance states table
* @state: List of performance states, in ascending order
- * @rcu: RCU used for safe access and destruction
+ * @rcu: RCU used only for runtime modifiable table
*/
struct em_perf_table {
struct em_perf_state *state;
@@ -49,6 +49,7 @@ struct em_perf_table {
/**
* struct em_perf_domain - Performance domain
* @default_table: Pointer to the default em_perf_table
+ * @runtime_table: Pointer to the runtime modifiable em_perf_table
* @nr_perf_states: Number of performance states
* @flags: See "em_perf_domain flags"
* @cpus: Cpumask covering the CPUs of the domain. It's here
@@ -64,6 +65,7 @@ struct em_perf_table {
*/
struct em_perf_domain {
struct em_perf_table *default_table;
+ struct em_perf_table __rcu *runtime_table;
int nr_perf_states;
unsigned long flags;
unsigned long cpus[];
diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index 6cd94f92701d..c2f8a0046f8a 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -212,6 +212,7 @@ static int em_create_pd(struct device *dev, int nr_states,
unsigned long flags)
{
struct em_perf_table *default_table;
+ struct em_perf_table *runtime_table;
struct em_perf_domain *pd;
struct device *cpu_dev;
int cpu, ret, num_cpus;
@@ -244,13 +245,25 @@ static int em_create_pd(struct device *dev, int nr_states,
pd->default_table = default_table;
+ runtime_table = kzalloc(sizeof(*runtime_table), GFP_KERNEL);
+ if (!runtime_table) {
+ kfree(default_table);
+ kfree(pd);
+ return -ENOMEM;
+ }
+
ret = em_create_perf_table(dev, pd, nr_states, cb, flags);
if (ret) {
kfree(default_table);
+ kfree(runtime_table);
kfree(pd);
return ret;
}
+ /* Re-use temporally (till 1st modification) the memory */
+ runtime_table->state = default_table->state;
+ rcu_assign_pointer(pd->runtime_table, runtime_table);
+
if (_is_cpu_device(dev))
for_each_cpu(cpu, cpus) {
cpu_dev = get_cpu_device(cpu);
@@ -448,23 +461,36 @@ EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
*/
void em_dev_unregister_perf_domain(struct device *dev)
{
+ struct em_perf_table __rcu *runtime_table;
+ struct em_perf_domain *pd;
+
if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
return;
if (_is_cpu_device(dev))
return;
+ pd = dev->em_pd;
/*
* The mutex separates all register/unregister requests and protects
* from potential clean-up/setup issues in the debugfs directories.
* The debugfs directory name is the same as device's name.
*/
mutex_lock(&em_pd_mutex);
+
em_debug_remove_pd(dev);
+ runtime_table = pd->runtime_table;
+
+ rcu_assign_pointer(pd->runtime_table, NULL);
+ synchronize_rcu();
+
+ kfree(runtime_table);
+
kfree(pd->default_table->state);
kfree(pd->default_table);
kfree(dev->em_pd);
+
dev->em_pd = NULL;
mutex_unlock(&em_pd_mutex);
}
--
2.25.1
Refactor a dedicated function which will be easier to maintain and re-use
in future. The upcoming changes for the modifiable EM perf_state table
will use it (instead of duplicating the code).
Signed-off-by: Lukasz Luba <[email protected]>
---
kernel/power/energy_model.c | 72 ++++++++++++++++++++++---------------
1 file changed, 43 insertions(+), 29 deletions(-)
diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index 85a70b7da023..fd1066dcf38b 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -103,14 +103,52 @@ static void em_debug_create_pd(struct device *dev) {}
static void em_debug_remove_pd(struct device *dev) {}
#endif
+static int em_compute_costs(struct device *dev, struct em_perf_state *table,
+ struct em_data_callback *cb, int nr_states,
+ unsigned long flags)
+{
+ unsigned long prev_cost = ULONG_MAX;
+ u64 fmax;
+ int i, ret;
+
+ /* Compute the cost of each performance state. */
+ fmax = (u64) table[nr_states - 1].frequency;
+ for (i = nr_states - 1; i >= 0; i--) {
+ unsigned long power_res, cost;
+
+ if (flags & EM_PERF_DOMAIN_ARTIFICIAL) {
+ ret = cb->get_cost(dev, table[i].frequency, &cost);
+ if (ret || !cost || cost > EM_MAX_POWER) {
+ dev_err(dev, "EM: invalid cost %lu %d\n",
+ cost, ret);
+ return -EINVAL;
+ }
+ } else {
+ power_res = table[i].power;
+ cost = div64_u64(fmax * power_res, table[i].frequency);
+ }
+
+ table[i].cost = cost;
+
+ if (table[i].cost >= prev_cost) {
+ table[i].flags = EM_PERF_STATE_INEFFICIENT;
+ dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
+ table[i].frequency);
+ } else {
+ prev_cost = table[i].cost;
+ }
+ }
+
+ return 0;
+}
+
static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
int nr_states, struct em_data_callback *cb,
unsigned long flags)
{
- unsigned long power, freq, prev_freq = 0, prev_cost = ULONG_MAX;
+ unsigned long power, freq, prev_freq = 0;
struct em_perf_state *table;
int i, ret;
- u64 fmax;
table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
if (!table)
@@ -154,33 +192,9 @@ static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
table[i].frequency = prev_freq = freq;
}
- /* Compute the cost of each performance state. */
- fmax = (u64) table[nr_states - 1].frequency;
- for (i = nr_states - 1; i >= 0; i--) {
- unsigned long power_res, cost;
-
- if (flags & EM_PERF_DOMAIN_ARTIFICIAL) {
- ret = cb->get_cost(dev, table[i].frequency, &cost);
- if (ret || !cost || cost > EM_MAX_POWER) {
- dev_err(dev, "EM: invalid cost %lu %d\n",
- cost, ret);
- goto free_ps_table;
- }
- } else {
- power_res = table[i].power;
- cost = div64_u64(fmax * power_res, table[i].frequency);
- }
-
- table[i].cost = cost;
-
- if (table[i].cost >= prev_cost) {
- table[i].flags = EM_PERF_STATE_INEFFICIENT;
- dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
- table[i].frequency);
- } else {
- prev_cost = table[i].cost;
- }
- }
+ ret = em_compute_costs(dev, table, cb, nr_states, flags);
+ if (ret)
+ goto free_ps_table;
pd->table = table;
pd->nr_perf_states = nr_states;
--
2.25.1
In order to prepare the code for the modifiable EM perf_state table,
refactor existing function em_cpufreq_update_efficiencies().
Signed-off-by: Lukasz Luba <[email protected]>
---
kernel/power/energy_model.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index 7b44f5b89fa1..0d037f3c4e58 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -237,10 +237,10 @@ static int em_create_pd(struct device *dev, int nr_states,
return 0;
}
-static void em_cpufreq_update_efficiencies(struct device *dev)
+static void
+em_cpufreq_update_efficiencies(struct device *dev, struct em_perf_state *table)
{
struct em_perf_domain *pd = dev->em_pd;
- struct em_perf_state *table;
struct cpufreq_policy *policy;
int found = 0;
int i;
@@ -254,8 +254,6 @@ static void em_cpufreq_update_efficiencies(struct device *dev)
return;
}
- table = pd->table;
-
for (i = 0; i < pd->nr_perf_states; i++) {
if (!(table[i].flags & EM_PERF_STATE_INEFFICIENT))
continue;
@@ -397,7 +395,7 @@ int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
dev->em_pd->flags |= flags;
- em_cpufreq_update_efficiencies(dev);
+ em_cpufreq_update_efficiencies(dev, dev->em_pd->table);
em_debug_create_pd(dev);
dev_info(dev, "EM: created perf domain\n");
--
2.25.1
Add an interface which allows to modify EM power data at runtime.
The new power information is populated by the provided callback, which
is called for each performance state. The CPU frequencies' efficiency is
re-calculated since that might be affected as well. The old EM memory
is going to be freed later using RCU mechanism.
Signed-off-by: Lukasz Luba <[email protected]>
---
include/linux/energy_model.h | 8 +++
kernel/power/energy_model.c | 109 +++++++++++++++++++++++++++++++++++
2 files changed, 117 insertions(+)
diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h
index cfb1759ffd45..fd4110166e97 100644
--- a/include/linux/energy_model.h
+++ b/include/linux/energy_model.h
@@ -201,6 +201,8 @@ struct em_data_callback {
struct em_perf_domain *em_cpu_get(int cpu);
struct em_perf_domain *em_pd_get(struct device *dev);
+int em_dev_update_perf_domain(struct device *dev, struct em_data_callback *cb,
+ void *priv);
int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
struct em_data_callback *cb, cpumask_t *span,
bool microwatts);
@@ -381,6 +383,12 @@ static inline int em_pd_nr_perf_states(struct em_perf_domain *pd)
{
return 0;
}
+static inline
+int em_dev_update_perf_domain(struct device *dev, struct em_data_callback *cb,
+ void *priv)
+{
+ return -EINVAL;
+}
#endif
#endif
diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index 4596bfe7398e..10180c776c5b 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -185,6 +185,101 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table,
return 0;
}
+/**
+ * em_dev_update_perf_domain() - Update run-time EM table for a device
+ * @dev : Device for which the EM is to be updated
+ * @cb : Callback function providing the power data for the EM
+ * @priv : Pointer to private data useful for passing context
+ * which might be required while calling @cb
+ *
+ * Update EM run-time modifiable table for a @dev using the callback
+ * defined in @cb. The EM new power values are then used for calculating
+ * the em_perf_state::cost for associated performance state.
+ *
+ * This function uses mutex to serialize writers, so it must not be called
+ * from non-sleeping context.
+ *
+ * Return 0 on success or a proper error in case of failure.
+ */
+int em_dev_update_perf_domain(struct device *dev, struct em_data_callback *cb,
+ void *priv)
+{
+ struct em_perf_table *runtime_table;
+ unsigned long power, freq;
+ struct em_perf_domain *pd;
+ int ret, i;
+
+ if (!cb || !cb->update_power)
+ return -EINVAL;
+
+ /*
+ * The lock serializes update and unregister code paths. When the
+ * EM has been unregistered in the meantime, we should capture that
+ * when entering this critical section. It also makes sure that
+ * two concurrent updates will be serialized.
+ */
+ mutex_lock(&em_pd_mutex);
+
+ if (!dev || !dev->em_pd) {
+ ret = -EINVAL;
+ goto unlock_em;
+ }
+
+ pd = dev->em_pd;
+
+ runtime_table = kzalloc(sizeof(*runtime_table), GFP_KERNEL);
+ if (!runtime_table) {
+ ret = -ENOMEM;
+ goto unlock_em;
+ }
+
+ runtime_table->state = kcalloc(pd->nr_perf_states,
+ sizeof(struct em_perf_state),
+ GFP_KERNEL);
+ if (!runtime_table->state) {
+ ret = -ENOMEM;
+ goto free_runtime_table;
+ }
+
+ /* Populate runtime table with updated values using driver callback */
+ for (i = 0; i < pd->nr_perf_states; i++) {
+ freq = pd->default_table->state[i].frequency;
+ runtime_table->state[i].frequency = freq;
+
+ /*
+ * Call driver callback to get a new power value for
+ * a given frequency.
+ */
+ ret = cb->update_power(dev, freq, &power, priv);
+ if (ret) {
+ dev_dbg(dev, "EM: run-time update error: %d\n", ret);
+ goto free_runtime_state_table;
+ }
+
+ runtime_table->state[i].power = power;
+ }
+
+ ret = em_compute_costs(dev, runtime_table->state, cb,
+ pd->nr_perf_states, pd->flags);
+ if (ret)
+ goto free_runtime_state_table;
+
+ em_perf_runtime_table_set(dev, runtime_table);
+
+ mutex_unlock(&em_pd_mutex);
+ return 0;
+
+free_runtime_state_table:
+ kfree(runtime_table->state);
+free_runtime_table:
+ kfree(runtime_table);
+unlock_em:
+ mutex_unlock(&em_pd_mutex);
+
+ return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(em_dev_update_perf_domain);
+
static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
int nr_states, struct em_data_callback *cb,
unsigned long flags)
@@ -517,6 +612,8 @@ void em_dev_unregister_perf_domain(struct device *dev)
* The mutex separates all register/unregister requests and protects
* from potential clean-up/setup issues in the debugfs directories.
* The debugfs directory name is the same as device's name.
+ * The lock also protects the updater of the runtime modifiable
+ * EM and this remover.
*/
mutex_lock(&em_pd_mutex);
@@ -524,9 +621,21 @@ void em_dev_unregister_perf_domain(struct device *dev)
runtime_table = pd->runtime_table;
+ /*
+ * Safely destroy runtime modifiable EM. By using the call
+ * synchronize_rcu() we make sure we don't progress till last user
+ * finished the RCU section and our update got applied.
+ */
rcu_assign_pointer(pd->runtime_table, NULL);
synchronize_rcu();
+ /*
+ * After the sync no updates will be in-flight, so free the old
+ * memory allocated for runtime EM.
+ */
+ if (runtime_table->state != pd->default_table->state)
+ kfree(runtime_table->state);
+
kfree(runtime_table);
kfree(pd->default_table->state);
--
2.25.1
The em_compute_cost() is going to be re-used in runtime modified EM
code path. Thus, make sure that this common code is safe and won't
try to use the NULL pointer. The former em_compute_cost() didn't have to
care about runtime modification code path. The upcoming changes introduce
such option, but with different callback. Those two paths which use
get_cost() (during first EM registration) or update_power() (during
runtime modification) need to be safely handled in em_compute_costs().
Signed-off-by: Lukasz Luba <[email protected]>
---
kernel/power/energy_model.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index fd1066dcf38b..5ecb73b36995 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -116,7 +116,7 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table,
for (i = nr_states - 1; i >= 0; i--) {
unsigned long power_res, cost;
- if (flags & EM_PERF_DOMAIN_ARTIFICIAL) {
+ if (flags & EM_PERF_DOMAIN_ARTIFICIAL && cb->get_cost) {
ret = cb->get_cost(dev, table[i].frequency, &cost);
if (ret || !cost || cost > EM_MAX_POWER) {
dev_err(dev, "EM: invalid cost %lu %d\n",
--
2.25.1
On 21/07/2023 17:50, Lukasz Luba wrote:
> Hi all,
>
> This patch set adds a new feature which allows to modify Energy Model (EM)
> power values at runtime. It will allow to better reflect power model of
> a recent SoCs and silicon. Different characteristics of the power usage
> can be leveraged and thus better decisions made during task placement in EAS.
>
> It's part of feature set know as Dynamic Energy Model. It has been presented
> and discussed recently at OSPM2023 [3]. This patch set implements the 1st
> improvement for the EM.
>
> The concepts:
> 1. The CPU power usage can vary due to the workload that it's running or due
> to the temperature of the SoC. The same workload can use more power when the
> temperature of the silicon has increased (e.g. due to hot GPU or ISP).
> In such situation or EM can be adjusted and reflect the fact of increased
> power usage. That power increase is due to a factor called static power
> (sometimes called simply: leakage). The CPUs in recent SoCs are different.
> We have heterogeneous SoCs with 3 (or even 4) different microarchitectures.
> They are also built differently with High Performance (HP) cells or
> Low Power (LP) cells. They are affected by the temperature increase
> differently: HP cells have bigger leakage. The SW model can leverage that
> knowledge.
IMHO it's important to note that this feature will add support for a
'single EM which can be changed during runtime according to the
workload' design.
Instead of the 'single and during the entire runtime static EM' design
we have today.
It won't support a 'multiple EMs and tasks can choose which model to use
based on some form of classification' design.
> 2. It is also possible to change the EM to better reflect the currently
> running workload. Usually the EM is derived from some average power values
> taken from experiments with benchmark (e.g. Dhrystone). The model derived
> from such scenario might not represent properly the workloads usually running
> on the device. Therefore, runtime modification of the EM allows to switch to
> a different model, when there is a need.
> 3. The EM can be adjusted after boot, when all the modules are loaded and
> more information about the SoC is available e.g. chip binning. This would help
> to better reflect the silicon characteristics. Thus, this EM modification
> API allows it now. It wasn't possible in the past and the EM had to be
> 'set in stone'.
Testing perspective:
I know that there is a test module with which we can test the new
em_dev_update_perf_domain() together with CPU hotplug etc.
What's missing is IMHO a test case showing the benefit of this new
feature for at least one of the use-cases (1. - 3.) described above.
Would it be possible to test a workload W at normal temperature with
EM_1 then head up the system and use EM_2 and spot performance/energy
consumption benefits against a vanilla system (case 1.) on Pixel6? This
would actually proof that this more on code complexity pays off.
--
We know that Google uses something similar in there Android kernel for
Pixel7 (CONFIG_PIXEL_EM). The EM is chosen from different EM profiles in
find_energy_efficient_cpu() -> compute_energy() -> em_cpu_energy().
In case we would have evidence that Google is switching their
proprietary implementation to this mainline one in the Android kernel
this would definitely also boost the confidence that we do need this
feature in mainline right now.
> Some design details:
> The internal mechanisms for the memory allocation are handled internally in the
> EM. Kernel modules can just call the new API to update the EM data and the
> new memory would be provided and owned by the EM. The EM memory is used by
> EAS, which impacts those design decisions. The EM writers are protected by
> a mutex. This new runtime modified EM table is protected using RCU mechanism,
> which fits the current EAS hot path (which already uses RCU read lock).
> The unregister API handles only non-CPU (e.g. GPU, ISP) devices and uses the
> same mutex as EM modifiers to make sure the memory is safely freed.
>
> More detailed explanation and background can be found in presentations
> during LPC2022 [1][2] or in the documentation patches.
>
> The time cost to update EM for 11 OPPs can be found here [6]. It's roughly
> 1.5us per 1 OPP while doing this on Little CPU at max frequency (1.8GHz).
I would list those results in this cover letter in a processed form and
also mention the target platform (Pixel6).
[...]
On 21/07/2023 17:50, Lukasz Luba wrote:
> This patch introduces the new feature: modifiable EM perf_state table.
nit pick: The first sentence doesn't add any information. I would skip it.
[...]
> The runtime modifiable EM data is used by the Energy Aware Scheduler (EAS)
> for the task placement. The EAS is the only user of the 'runtime
> modifiable EM'.
The runtime modifiable EM is currently only used ...
The you can skip the next sentence: "The EAS is the only user ..."
All the other users (thermal, etc.) are still using the
> default (basic) EM. This fact drove the design of this feature.
[...]
On 21/07/2023 17:50, Lukasz Luba wrote:
> The EM is going to support runtime modifications of the power data.
> Introduce RCU safe mechanism to clean up the old allocated EM data.
> It also adds a mutex for the EM structure to serialize the modifiers.
>
> Signed-off-by: Lukasz Luba <[email protected]>
> ---
> kernel/power/energy_model.c | 42 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 42 insertions(+)
>
> diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
> index c2f8a0046f8a..4596bfe7398e 100644
> --- a/kernel/power/energy_model.c
> +++ b/kernel/power/energy_model.c
> @@ -23,6 +23,9 @@
> */
> static DEFINE_MUTEX(em_pd_mutex);
>
> +static void em_cpufreq_update_efficiencies(struct device *dev,
> + struct em_perf_state *table);
> +
> static bool _is_cpu_device(struct device *dev)
> {
> return (dev->bus == &cpu_subsys);
> @@ -104,6 +107,45 @@ static void em_debug_create_pd(struct device *dev) {}
> static void em_debug_remove_pd(struct device *dev) {}
> #endif
>
> +static void em_destroy_rt_table_rcu(struct rcu_head *rp)
> +{
> + struct em_perf_table *runtime_table;
> +
> + runtime_table = container_of(rp, struct em_perf_table, rcu);
> + kfree(runtime_table->state);
> + kfree(runtime_table);
> +}
> +
> +static void em_destroy_tmp_setup_rcu(struct rcu_head *rp)
> +{
> + struct em_perf_table *runtime_table;
> +
> + runtime_table = container_of(rp, struct em_perf_table, rcu);
> + kfree(runtime_table);
> +}
Still don't like that we have to have 2 rcu callbacks here. In case we
could assign default_table to runtime_table in em_create_pd() (and not
just default_table->state to runtime_table->state) IMHO we would only
need one rcu callback?
-->8--
-static void em_destroy_tmp_setup_rcu(struct rcu_head *rp)
-{
- struct em_perf_table *runtime_table;
-
- runtime_table = container_of(rp, struct em_perf_table, rcu);
- kfree(runtime_table);
-}
-
static void em_perf_runtime_table_set(struct device *dev,
struct em_perf_table *runtime_table)
{
@@ -136,13 +128,8 @@ static void em_perf_runtime_table_set(struct device *dev,
em_cpufreq_update_efficiencies(dev, runtime_table->state);
- /*
- * Check if the 'state' array is not actually the one from setup.
- * If it is then don't free it.
- */
- if (tmp->state == pd->default_table->state)
- call_rcu(&tmp->rcu, em_destroy_tmp_setup_rcu);
- else
+ /* Don't free default table (inital value of runtime table) */
+ if (tmp != pd->default_table)
call_rcu(&tmp->rcu, em_destroy_rt_table_rcu);
}
@@ -349,7 +336,6 @@ static int em_create_pd(struct device *dev, int nr_states,
unsigned long flags)
{
struct em_perf_table *default_table;
- struct em_perf_table *runtime_table;
struct em_perf_domain *pd;
struct device *cpu_dev;
int cpu, ret, num_cpus;
@@ -382,24 +368,15 @@ static int em_create_pd(struct device *dev, int nr_states,
pd->default_table = default_table;
- runtime_table = kzalloc(sizeof(*runtime_table), GFP_KERNEL);
- if (!runtime_table) {
- kfree(default_table);
- kfree(pd);
- return -ENOMEM;
- }
-
ret = em_create_perf_table(dev, pd, nr_states, cb, flags);
if (ret) {
kfree(default_table);
- kfree(runtime_table);
kfree(pd);
return ret;
}
- /* Re-use temporally (till 1st modification) the memory */
- runtime_table->state = default_table->state;
- rcu_assign_pointer(pd->runtime_table, runtime_table);
+ /* Initialize runtime table as default table */
+ rcu_assign_pointer(pd->runtime_table, default_table);
if (_is_cpu_device(dev))
for_each_cpu(cpu, cpus) {