Skip to contentSkip to conversation

PM: EM: Fix potential division-by-zero error in em_compute_costs()

PATCH RESEND Applied Yaxiong Tian 2 participants 2 messages Apr 18, 2025 – Apr 28, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND v4 1/1 PM: EM: Fix potential division-by-zero error in em_compute_costs()

From: Yaxiong Tian <[email protected]>

When the device is of a non-CPU type, table[i].performance won't be initialized in the previous em_init_performance(), resulting in division by zero when calculating costs in em_compute_costs().

Since the 'cost' algorithm is only used for EAS energy efficiency calculations and is currently not utilized by other device drivers, we should add the _is_cpu_device(dev) check to prevent this division-by-zero issue.

Fixes 1b600da51073 ("PM: EM: Optimize em_cpu_energy() and remove division")
---
 kernel/power/energy_model.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/power/energy_model.c +4 -0
index d9b7e2b38c7a..41606247c277 100644
@@ -233,6 +233,10 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table,
 	unsigned long prev_cost = ULONG_MAX;
 	int i, ret;
 
+	/* This is needed only for CPUs and EAS skip other devices */
+	if (!_is_cpu_device(dev))
+		return 0;
+
 	/* Compute the cost of each performance state. */
 	for (i = nr_states - 1; i >= 0; i--) {
 		unsigned long power_res, cost;
Signature · 1 line
2.25.1
maintainer Applied
#1 lore raw
Quoted 25 lines · Yaxiong Tian

From: Yaxiong Tian <[email protected]>

When the device is of a non-CPU type, table[i].performance won't be initialized in the previous em_init_performance(), resulting in division by zero when calculating costs in em_compute_costs().

Since the 'cost' algorithm is only used for EAS energy efficiency calculations and is currently not utilized by other device drivers, we should add the _is_cpu_device(dev) check to prevent this division-by-zero issue.

Fixes 1b600da51073 ("PM: EM: Optimize em_cpu_energy() and remove division")
---
 kernel/power/energy_model.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/power/energy_model.c +0 -0
index d9b7e2b38c7a..41606247c277 100644
@@ -233,6 +233,10 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table,
        unsigned long prev_cost = ULONG_MAX;

kernel/power/energy_model.c static int em_compute_costs(struct device *dev, struct em_perf_state *table,

Quoted 10 lines
kernel/power/energy_model.c +4 -1
        int i, ret;
 
+       /* This is needed only for CPUs and EAS skip other devices */
+       if (!_is_cpu_device(dev))
+               return 0;
+
        /* Compute the cost of each performance state. */
        for (i = nr_states - 1; i >= 0; i--) {
                unsigned long power_res, cost;
--

Applied as 6.16 material, thanks!