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 <vireshk@kernel.org>,
	Nishanth Menon <nm@ti.com>, Stephen Boyd <sboyd@kernel.org>,
	Miguel Ojeda <ojeda@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] rust: opp: clean up OF OPP table on error paths
Date: Mon, 30 Nov 2026 21:05:54 +0800	[thread overview]
Message-ID: <8c1cd0c9-b854-4516-9915-8d3498d49e47@oss.qualcomm.com> (raw)
In-Reply-To: <20261127121129.1150-1-AeeSec@126.com>

On 11/27/2026 8:11 PM, Bilal Hashim wrote:
> In Table::from_of(), if Self::from_dev(dev) fails after
> dev_pm_opp_of_add_table_indexed() has succeeded, the newly added C-side
> OF OPP table is leaked because Table is never constructed and Drop does
> not run.
> 
> Similarly, in Table::from_of_cpumask(), if either Self::from_dev(dev) or
> CpumaskVar::try_clone(cpumask) fails after
> dev_pm_opp_of_cpumask_add_table() succeeds, the C-side cpumask OPP table
> is leaked.
> 
> Fix both paths by rolling back dev_pm_opp_of_remove_table() /
> dev_pm_opp_of_cpumask_remove_table() when Self::from_dev(dev) fails, and
> by cloning the cpumask before calling dev_pm_opp_of_cpumask_add_table().
> 
> Signed-off-by: Bilal Hashim <AeeSec@126.com>
> ---
> --- a/rust/kernel/opp.rs
> +++ b/rust/kernel/opp.rs
> @@ -667,7 +667,14 @@ impl Table {
>           to_result(unsafe { bindings::dev_pm_opp_of_add_table_indexed(dev.as_raw(), index) })?;
>   
>           // Get the newly created [`Table`].
> -        let mut table = Self::from_dev(dev)?;
> +        let mut table = match Self::from_dev(dev) {
> +            Ok(table) => table,
> +            Err(e) => {
> +                // SAFETY: `dev` is valid and we just added the OF OPP table above.
> +                unsafe { bindings::dev_pm_opp_of_remove_table(dev.as_raw()) };
> +                return Err(e);
> +            }
> +        };

This rollback is unreachable/ a no-op. Once
dev_pm_opp_of_add_table_indexed() succeeds, dev is already on the
table's dev_list. dev_list entries are only removed in
_opp_table_kref_release(), so from_dev() cannot fail here.

Even if it did fail, dev_pm_opp_of_remove_table(dev) would repeat the
same _find_opp_table(dev) lookup. It would get -ENODEV and return
without doing anything.

Please drop this hunk.

>           table.of = true;
>   
>           Ok(table)
> @@ -689,14 +696,23 @@ impl Table {
>       pub fn from_of_cpumask(dev: &Device, cpumask: &mut Cpumask) -> Result<Self> {
> +        let cpus = CpumaskVar::try_clone(cpumask)?;
> +
>           // SAFETY: The cpumask is valid and the returned pointer will be owned by the [`Table`]
>           // instance.
>           //
>           // INVARIANT: The reference-count is incremented by the C code and is decremented when
>           // [`Table`] goes out of scope.
>           to_result(unsafe { bindings::dev_pm_opp_of_cpumask_add_table(cpumask.as_raw()) })?;
>   
>           // Fetch the newly created table.
> -        let mut table = Self::from_dev(dev)?;
> -        table.cpus = Some(CpumaskVar::try_clone(cpumask)?);
> +        let mut table = match Self::from_dev(dev) {
> +            Ok(table) => table,
> +            Err(e) => {
> +                // SAFETY: The cpumask is valid and we just added the OF OPP table above.
> +                unsafe { bindings::dev_pm_opp_of_cpumask_remove_table(cpumask.as_raw()) };

This is only reached if dev is not one of the CPUs in cpumask. The only
in-tree user, rcpufreq_dt, derives dev and the mask from the same policy
CPU, so from_dev() cannot fail there, for the same reason as in
from_of() above.

Should we consider dropping it as well?


> +                return Err(e);
> +            }
> +        };
> +        table.cpus = Some(cpus);
>   
>           Ok(table)
>       }
> 

Please keep only the clone change, update the commit message
accordingly, and add a Fixes: tag.


> 


-- 
Thx and BRs,
Zhongqiu Han

      reply	other threads:[~2026-11-30 13:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 12:11 Bilal Hashim
2026-11-30 13:05 ` Zhongqiu Han [this message]

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=8c1cd0c9-b854-4516-9915-8d3498d49e47@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=nm@ti.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=vireshk@kernel.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®