All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bilal Hashim <AeeSec@126.com>
To: Viresh Kumar <viresh.kumar@linaro.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>
Cc: linux-pm@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org, Bilal Hashim <AeeSec@126.com>
Subject: [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error
Date: Fri, 27 Nov 2026 20:40:18 +0800	[thread overview]
Message-ID: <20261127124018.972-1-AeeSec@126.com> (raw)

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 {


             reply	other threads:[~2026-11-27 12:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 12:40 Bilal Hashim [this message]
2026-11-30  8:23 ` [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error Zhongqiu Han
2026-11-30  8:39   ` Bilal Hashim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127124018.972-1-AeeSec@126.com \
    --to=aeesec@126.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.