Re: [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error

From: Zhongqiu Han

Date: Mon Nov 30 2026 - 03:23:42 EST


On 11/27/2026 8:40 PM, Bilal Hashim wrote:
In CPUFreqDTDriver::init(), policy.set_freq_table(&freq_table) stores a
raw pointer to `freq_table` inside `policy->freq_table` before
policy.set_clk() and Arc::new() are called.

If either policy.set_clk() or Arc::new() fails, Rust drops `freq_table`
(freeing it via dev_pm_opp_free_cpufreq_table()), while the C cpufreq
core's `out_clear_policy:` error path in cpufreq_online() only clears
`policy->cpus` and does not reset `policy->freq_table`. When a policy is
reused on subsequent CPU online attempts, `policy->freq_table` retains a

This should not be a real issue: a policy whose ->init() fails is freed
by cpufreq_online() right away and is never reused, so nothing
dereferences the stale policy->freq_table.

If kept as a cleanup, please reword the commit message, drop the "reused
policy" claim, and consider handling policy->clk in a similar way.


dangling pointer to freed memory.

Allocate the `Arc<CPUFreqDTDevice>` before installing `freq_table` into
`policy` so that `policy->freq_table` is only set once `init()` cannot
fail.

Signed-off-by: Bilal Hashim <AeeSec@xxxxxxx>
---
--- a/drivers/cpufreq/rcpufreq_dt.rs
+++ b/drivers/cpufreq/rcpufreq_dt.rs
@@ -134,15 +134,13 @@ impl cpufreq::Driver for CPUFreqDTDriver {
.set_transition_latency_ns(transition_latency);
let freq_table = opp_table.cpufreq_table()?;
- // SAFETY: The `freq_table` is not dropped while it is getting used by the C code.
- unsafe { policy.set_freq_table(&freq_table) };
// SAFETY: The returned `clk` is not dropped while it is getting used by the C code.
let clk = unsafe { policy.set_clk(dev, None)? };
mask.copy(policy.cpus());
- Ok(Arc::new(
+ let data = Arc::new(
CPUFreqDTDevice {
opp_table,
freq_table,
@@ -151,7 +149,12 @@ impl cpufreq::Driver for CPUFreqDTDriver {
_clk: clk,
},
GFP_KERNEL,
- )?)
+ )?;
+
+ // SAFETY: `data` owns `freq_table` and will not be dropped while in use by the C code.
+ unsafe { policy.set_freq_table(&data.freq_table) };
+
+ Ok(data)
}
fn exit(_policy: &mut cpufreq::Policy, _data: Option<Self::PData>) -> Result {




--
Thx and BRs,
Zhongqiu Han