linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: opp: simplify callers of `to_c_str_array`
@ 2025-10-20 13:07 Tamir Duberstein
  2025-10-22  5:40 ` Viresh Kumar
  0 siblings, 1 reply; 5+ messages in thread
From: Tamir Duberstein @ 2025-10-20 13:07 UTC (permalink / raw)
  To: Viresh Kumar, Nishanth Menon, Stephen Boyd, Miguel Ojeda,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich
  Cc: linux-pm, rust-for-linux, linux-kernel, Tamir Duberstein

Use `Option` combinators to make this a bit less noisy.

Signed-off-by: Tamir Duberstein <tamird@kernel.org>
---
 rust/kernel/opp.rs | 25 ++++++++-----------------
 1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
index 9d6c58178a6f..b84786f45522 100644
--- a/rust/kernel/opp.rs
+++ b/rust/kernel/opp.rs
@@ -443,23 +443,14 @@ pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> {
     ///
     /// The returned [`ConfigToken`] will remove the configuration when dropped.
     pub fn set(self, dev: &Device) -> Result<ConfigToken> {
-        let (_clk_list, clk_names) = match &self.clk_names {
-            Some(x) => {
-                let list = to_c_str_array(x)?;
-                let ptr = list.as_ptr();
-                (Some(list), ptr)
-            }
-            None => (None, ptr::null()),
-        };
-
-        let (_regulator_list, regulator_names) = match &self.regulator_names {
-            Some(x) => {
-                let list = to_c_str_array(x)?;
-                let ptr = list.as_ptr();
-                (Some(list), ptr)
-            }
-            None => (None, ptr::null()),
-        };
+        let clk_names = self.clk_names.as_deref().map(to_c_str_array).transpose()?;
+        let clk_names = clk_names.map_or(ptr::null(), |c| c.as_ptr());
+        let regulator_names = self
+            .regulator_names
+            .as_deref()
+            .map(to_c_str_array)
+            .transpose()?;
+        let regulator_names = regulator_names.map_or(ptr::null(), |c| c.as_ptr());
 
         let prop_name = self
             .prop_name

---
base-commit: a1ec674cd709fec213acbb567e699c5f6f58cb60
change-id: 20251018-opp-simpler-code-58662c627bf4

Best regards,
--  
Tamir Duberstein <tamird@kernel.org>


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: opp: simplify callers of `to_c_str_array`
  2025-10-20 13:07 [PATCH] rust: opp: simplify callers of `to_c_str_array` Tamir Duberstein
@ 2025-10-22  5:40 ` Viresh Kumar
  2025-10-22 11:22   ` Tamir Duberstein
  0 siblings, 1 reply; 5+ messages in thread
From: Viresh Kumar @ 2025-10-22  5:40 UTC (permalink / raw)
  To: Tamir Duberstein
  Cc: Viresh Kumar, Nishanth Menon, Stephen Boyd, Miguel Ojeda,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, linux-pm, rust-for-linux, linux-kernel

Hi Tamir,

On 20-10-25, 09:07, Tamir Duberstein wrote:
> Use `Option` combinators to make this a bit less noisy.
> 
> Signed-off-by: Tamir Duberstein <tamird@kernel.org>
> ---
>  rust/kernel/opp.rs | 25 ++++++++-----------------
>  1 file changed, 8 insertions(+), 17 deletions(-)
> 
> diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
> index 9d6c58178a6f..b84786f45522 100644
> --- a/rust/kernel/opp.rs
> +++ b/rust/kernel/opp.rs
> @@ -443,23 +443,14 @@ pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> {
>      ///
>      /// The returned [`ConfigToken`] will remove the configuration when dropped.
>      pub fn set(self, dev: &Device) -> Result<ConfigToken> {
> -        let (_clk_list, clk_names) = match &self.clk_names {
> -            Some(x) => {
> -                let list = to_c_str_array(x)?;
> -                let ptr = list.as_ptr();
> -                (Some(list), ptr)
> -            }
> -            None => (None, ptr::null()),
> -        };
> -
> -        let (_regulator_list, regulator_names) = match &self.regulator_names {
> -            Some(x) => {
> -                let list = to_c_str_array(x)?;
> -                let ptr = list.as_ptr();
> -                (Some(list), ptr)
> -            }
> -            None => (None, ptr::null()),
> -        };
> +        let clk_names = self.clk_names.as_deref().map(to_c_str_array).transpose()?;
> +        let clk_names = clk_names.map_or(ptr::null(), |c| c.as_ptr());
> +        let regulator_names = self
> +            .regulator_names
> +            .as_deref()
> +            .map(to_c_str_array)
> +            .transpose()?;
> +        let regulator_names = regulator_names.map_or(ptr::null(), |c| c.as_ptr());

I had to keep _clk_list and _regulator_list earlier to make sure the list isn't
freed while its pointer is still used (sent to dev_pm_opp_set_config()). Won't
this change cause an issue here ?

-- 
viresh

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: opp: simplify callers of `to_c_str_array`
  2025-10-22  5:40 ` Viresh Kumar
@ 2025-10-22 11:22   ` Tamir Duberstein
  2025-10-23 11:32     ` Viresh Kumar
  0 siblings, 1 reply; 5+ messages in thread
From: Tamir Duberstein @ 2025-10-22 11:22 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Viresh Kumar, Nishanth Menon, Stephen Boyd, Miguel Ojeda,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, linux-pm, rust-for-linux, linux-kernel

Hi Viresh,

On Wed, Oct 22, 2025 at 1:40 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> Hi Tamir,
>
> On 20-10-25, 09:07, Tamir Duberstein wrote:
> > Use `Option` combinators to make this a bit less noisy.
> >
> > Signed-off-by: Tamir Duberstein <tamird@kernel.org>
> > ---
> >  rust/kernel/opp.rs | 25 ++++++++-----------------
> >  1 file changed, 8 insertions(+), 17 deletions(-)
> >
> > diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
> > index 9d6c58178a6f..b84786f45522 100644
> > --- a/rust/kernel/opp.rs
> > +++ b/rust/kernel/opp.rs
> > @@ -443,23 +443,14 @@ pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> {
> >      ///
> >      /// The returned [`ConfigToken`] will remove the configuration when dropped.
> >      pub fn set(self, dev: &Device) -> Result<ConfigToken> {
> > -        let (_clk_list, clk_names) = match &self.clk_names {
> > -            Some(x) => {
> > -                let list = to_c_str_array(x)?;
> > -                let ptr = list.as_ptr();
> > -                (Some(list), ptr)
> > -            }
> > -            None => (None, ptr::null()),
> > -        };
> > -
> > -        let (_regulator_list, regulator_names) = match &self.regulator_names {
> > -            Some(x) => {
> > -                let list = to_c_str_array(x)?;
> > -                let ptr = list.as_ptr();
> > -                (Some(list), ptr)
> > -            }
> > -            None => (None, ptr::null()),
> > -        };
> > +        let clk_names = self.clk_names.as_deref().map(to_c_str_array).transpose()?;
> > +        let clk_names = clk_names.map_or(ptr::null(), |c| c.as_ptr());
> > +        let regulator_names = self
> > +            .regulator_names
> > +            .as_deref()
> > +            .map(to_c_str_array)
> > +            .transpose()?;
> > +        let regulator_names = regulator_names.map_or(ptr::null(), |c| c.as_ptr());
>
> I had to keep _clk_list and _regulator_list earlier to make sure the list isn't
> freed while its pointer is still used (sent to dev_pm_opp_set_config()). Won't
> this change cause an issue here ?

I believe the `{clk,regulator}_names` vector bindings remain alive for
the whole scope, even if they are shadowed. See
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=800b334c514c2024d7b5e47fc54c1f2d.

Cheers.
Tamir

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: opp: simplify callers of `to_c_str_array`
  2025-10-22 11:22   ` Tamir Duberstein
@ 2025-10-23 11:32     ` Viresh Kumar
  2025-10-23 13:24       ` Tamir Duberstein
  0 siblings, 1 reply; 5+ messages in thread
From: Viresh Kumar @ 2025-10-23 11:32 UTC (permalink / raw)
  To: Tamir Duberstein
  Cc: Viresh Kumar, Nishanth Menon, Stephen Boyd, Miguel Ojeda,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, linux-pm, rust-for-linux, linux-kernel

On 22-10-25, 07:22, Tamir Duberstein wrote:
> I believe the `{clk,regulator}_names` vector bindings remain alive for
> the whole scope, even if they are shadowed. See
> https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=800b334c514c2024d7b5e47fc54c1f2d.

I tried to print the name of the regulator and clk in the C code:

diff --git a/drivers/opp/core.c b/drivers/opp/core.c
index d67d392e16d4..89b65fe07b99 100644
--- a/drivers/opp/core.c
+++ b/drivers/opp/core.c
@@ -2565,6 +2565,8 @@ int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
                goto err;
        }

+       pr_info("%s: %d: %s: %s\n", __func__, __LINE__, config->clk_names[0], config->regulator_names[0]);

and I get this:

[    2.358437] core: dev_pm_opp_set_config: 2568: �<��y����<��y���x��: �<��y����<��y���x��



With following diff applied over your patch, it works again:

diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
index 743080e14292..02cb754c4abd 100644
--- a/rust/kernel/opp.rs
+++ b/rust/kernel/opp.rs
@@ -444,13 +444,13 @@ pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> {
     /// The returned [`ConfigToken`] will remove the configuration when dropped.
     pub fn set(self, dev: &Device) -> Result<ConfigToken> {
         let clk_names = self.clk_names.as_deref().map(to_c_str_array).transpose()?;
-        let clk_names = clk_names.map_or(ptr::null(), |c| c.as_ptr());
+        let clk_names = clk_names.as_ref().map_or(ptr::null(), |c| c.as_ptr());
         let regulator_names = self
             .regulator_names
             .as_deref()
             .map(to_c_str_array)
             .transpose()?;
-        let regulator_names = regulator_names.map_or(ptr::null(), |c| c.as_ptr());
+        let regulator_names = regulator_names.as_ref().map_or(ptr::null(), |c| c.as_ptr());

         let prop_name = self
             .prop_name


and I get:

[    2.460149] core: dev_pm_opp_set_config: 2568: dummy_clk: dummy_reg

-- 
viresh

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: opp: simplify callers of `to_c_str_array`
  2025-10-23 11:32     ` Viresh Kumar
@ 2025-10-23 13:24       ` Tamir Duberstein
  0 siblings, 0 replies; 5+ messages in thread
From: Tamir Duberstein @ 2025-10-23 13:24 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Viresh Kumar, Nishanth Menon, Stephen Boyd, Miguel Ojeda,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, linux-pm, rust-for-linux, linux-kernel

On Thu, Oct 23, 2025 at 7:32 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> On 22-10-25, 07:22, Tamir Duberstein wrote:
> > I believe the `{clk,regulator}_names` vector bindings remain alive for
> > the whole scope, even if they are shadowed. See
> > https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=800b334c514c2024d7b5e47fc54c1f2d.
>
> I tried to print the name of the regulator and clk in the C code:
>
> diff --git a/drivers/opp/core.c b/drivers/opp/core.c
> index d67d392e16d4..89b65fe07b99 100644
> --- a/drivers/opp/core.c
> +++ b/drivers/opp/core.c
> @@ -2565,6 +2565,8 @@ int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
>                 goto err;
>         }
>
> +       pr_info("%s: %d: %s: %s\n", __func__, __LINE__, config->clk_names[0], config->regulator_names[0]);
>
> and I get this:
>
> [    2.358437] core: dev_pm_opp_set_config: 2568: �<��y����<��y���x��: �<��y����<��y���x��
>
>
>
> With following diff applied over your patch, it works again:
>
> diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
> index 743080e14292..02cb754c4abd 100644
> --- a/rust/kernel/opp.rs
> +++ b/rust/kernel/opp.rs
> @@ -444,13 +444,13 @@ pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> {
>      /// The returned [`ConfigToken`] will remove the configuration when dropped.
>      pub fn set(self, dev: &Device) -> Result<ConfigToken> {
>          let clk_names = self.clk_names.as_deref().map(to_c_str_array).transpose()?;
> -        let clk_names = clk_names.map_or(ptr::null(), |c| c.as_ptr());
> +        let clk_names = clk_names.as_ref().map_or(ptr::null(), |c| c.as_ptr());
>          let regulator_names = self
>              .regulator_names
>              .as_deref()
>              .map(to_c_str_array)
>              .transpose()?;
> -        let regulator_names = regulator_names.map_or(ptr::null(), |c| c.as_ptr());
> +        let regulator_names = regulator_names.as_ref().map_or(ptr::null(), |c| c.as_ptr());
>
>          let prop_name = self
>              .prop_name
>
>
> and I get:
>
> [    2.460149] core: dev_pm_opp_set_config: 2568: dummy_clk: dummy_reg

Good catch. This should really be a lot more defensive so the compiler
catches this kind of use-after-free. I'll send v2 shortly.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-10-23 13:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-20 13:07 [PATCH] rust: opp: simplify callers of `to_c_str_array` Tamir Duberstein
2025-10-22  5:40 ` Viresh Kumar
2025-10-22 11:22   ` Tamir Duberstein
2025-10-23 11:32     ` Viresh Kumar
2025-10-23 13:24       ` Tamir Duberstein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).