Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error
@ 2026-11-27 12:40 Bilal Hashim
  0 siblings, 0 replies; only message 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] only message in thread

only message in thread, other threads:[~2026-11-27 12:41 UTC | newest]

Thread overview: (only message) (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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox