* [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error
@ 2026-11-27 12:40 Bilal Hashim
2026-11-30 8:23 ` Zhongqiu Han
0 siblings, 1 reply; 3+ messages in thread
From: Bilal Hashim @ 2026-11-27 12:40 UTC (permalink / raw)
To: Viresh Kumar, Rafael J . Wysocki
Cc: linux-pm, rust-for-linux, linux-kernel, Bilal Hashim
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
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@126.com>
---
--- 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 {
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error
2026-11-27 12:40 [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error Bilal Hashim
@ 2026-11-30 8:23 ` Zhongqiu Han
2026-11-30 8:39 ` Bilal Hashim
0 siblings, 1 reply; 3+ messages in thread
From: Zhongqiu Han @ 2026-11-30 8:23 UTC (permalink / raw)
To: Bilal Hashim, Viresh Kumar, Rafael J . Wysocki
Cc: linux-pm, rust-for-linux, linux-kernel, Zhongqiu Han
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@126.com>
> ---
> --- 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
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error
2026-11-30 8:23 ` Zhongqiu Han
@ 2026-11-30 8:39 ` Bilal Hashim
0 siblings, 0 replies; 3+ messages in thread
From: Bilal Hashim @ 2026-11-30 8:39 UTC (permalink / raw)
To: Zhongqiu Han, Viresh Kumar, Rafael J. Wysocki
Cc: linux-pm, rust-for-linux, linux-kernel
Hi Zhongqiu,
Thank you for reviewing and pointing this out! Indeed, cpufreq_online()
frees the policy on ->init() failure rather than reusing it, so there is
no real dangling pointer bug here.
I will drop this patch. Thanks again for your time!
Best regards,
Bilal
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 8:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 12:40 [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error Bilal Hashim
2026-11-30 8:23 ` Zhongqiu Han
2026-11-30 8:39 ` Bilal Hashim
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®