Re: [PATCH v2 1/2] powercap: dtpm_cpu: Guard em_cpu_get() against NULL return in sysfs callbacks
From: Daniel Lezcano
Date: Wed Jun 24 2026 - 16:49:01 EST
On 6/24/26 22:31, sivany32@xxxxxxxxx wrote:
From: Sivan Zohar-Kotzer <sivany32@xxxxxxxxx>
em_cpu_get() can return NULL when a CPU becomes impossible. Two call
sites miss the NULL test:
1. set_pd_power_limit() — reachable from sysfs via the powercap
constraint power_limit_uw store path. If the CPU has become
impossible then pd will be NULL, return the current power limit
unchanged (no-op) instead of crashing.
Can you provide a script or a recipe spotting these issues ?
2. update_pd_power_uw() — called from CPU hotplug handlers via
dtpm_update_power(). While reaching this with a NULL em may be
technically impossible today, the check is kept for defense in
depth against future refactoring or subtle races.
The other sysfs-reachable path, get_pd_power_uw(), already has a NULL
guard returning 0.
Fixes: 0e8f68d7f048 ("powercap/drivers/dtpm: Add CPU energy model based support")
Signed-off-by: Sivan Zohar-Kotzer <sivany32@xxxxxxxxx>
Co-developed-by: Elazar Leibovich <elazarl@xxxxxxxxx>
Signed-off-by: Elazar Leibovich <elazarl@xxxxxxxxx>
---
drivers/powercap/dtpm_cpu.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/powercap/dtpm_cpu.c b/drivers/powercap/dtpm_cpu.c
index 21355db64..886cbe922 100644
--- a/drivers/powercap/dtpm_cpu.c
+++ b/drivers/powercap/dtpm_cpu.c
@@ -47,6 +47,9 @@ static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)
u64 power;
int i, nr_cpus;
+ if (!pd)
+ return dtpm->power_limit;
+
nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(pd->cpus));
rcu_read_lock();
@@ -125,6 +128,9 @@ static int update_pd_power_uw(struct dtpm *dtpm)
struct em_perf_state *table;
int nr_cpus;
+ if (!em)
+ return -EINVAL;
+
nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(em->cpus));
rcu_read_lock();