mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
To: Bilal Hashim <AeeSec@126.com>,
	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,
	Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
Subject: Re: [PATCH] cpufreq: rcpufreq_dt: avoid dangling policy->freq_table on init error
Date: Mon, 30 Nov 2026 16:23:24 +0800	[thread overview]
Message-ID: <e3b35f70-bfee-4308-8794-6f23a1a2f460@oss.qualcomm.com> (raw)
In-Reply-To: <20261127124018.972-1-AeeSec@126.com>

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

  reply	other threads:[~2026-11-30  8:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 12:40 Bilal Hashim
2026-11-30  8:23 ` Zhongqiu Han [this message]
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=e3b35f70-bfee-4308-8794-6f23a1a2f460@oss.qualcomm.com \
    --to=zhongqiu.han@oss.qualcomm.com \
    --cc=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 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®