The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] rust: opp: clean up OF OPP table on error paths
@ 2026-11-27 12:11 Bilal Hashim
  0 siblings, 0 replies; only message in thread
From: Bilal Hashim @ 2026-11-27 12:11 UTC (permalink / raw)
  To: Viresh Kumar, Nishanth Menon, Stephen Boyd, Miguel Ojeda
  Cc: linux-pm, rust-for-linux, linux-kernel, Bilal Hashim

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);
+            }
+        };
         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()) };
+                return Err(e);
+            }
+        };
+        table.cpus = Some(cpus);
 
         Ok(table)
     }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-27 12:12 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:11 [PATCH] rust: opp: clean up OF OPP table on error paths Bilal Hashim

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