rust: opp: simplify callers of `to_c_str_array`
Subsystems (2)
Tamir Duberstein
reviewerPATCH 1/1 rust: opp: simplify callers of `to_c_str_array`
Use Option combinators to make this a bit less noisy.
--- rust/kernel/opp.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)
rust/kernel/opp.rs +8 -17
@@ -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,
Signature · 1 line
Tamir Duberstein <[email protected]>
Hi Tamir,
Quoted 30 lines · Tamir Duberstein
Use
Optioncombinators to make this a bit less noisy.Signed-off-by Tamir Duberstein <[email protected]>--- rust/kernel/opp.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)rust/kernel/opp.rs +0
-15@@ -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) - }
rust/kernel/opp.rs pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> { · line 453
Quoted 10 lines
rust/kernel/opp.rs +8
-2- 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 ?
Signature · 1 line
viresh
Tamir Duberstein
reviewerHi Viresh,
Quoted 40 lines · Viresh Kumar
Hi Tamir,
Quoted 36 lines · Tamir Duberstein
Use
Optioncombinators to make this a bit less noisy.Signed-off-by Tamir Duberstein <[email protected]>--- rust/kernel/opp.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)rust/kernel/opp.rs +4
-17@@ -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
Quoted 8 lines
rust/kernel/opp.rs pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> { · line 453
Quoted 4 lines
rust/kernel/opp.rs +4
-0+ .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_listand_regulator_listearlier to make sure the list isn't freed while its pointer is still used (sent todev_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
Quoted 3 lines · Tamir Duberstein
I believe the
{clk,regulator}_namesvector 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:
drivers/opp/core.c +1 -0
@@ -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:
rust/kernel/opp.rs +2 -2
@@ -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
Signature · 1 line
viresh
Tamir Duberstein
reviewerQuoted 48 lines · Viresh Kumar
Quoted 3 lines · Tamir Duberstein
I believe the
{clk,regulator}_namesvector 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:
drivers/opp/core.c +1
-0@@ -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:
rust/kernel/opp.rs +2
-2@@ -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
Quoted 3 lines
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.