pinctrl: pinconf-generic: clean up pinconf_parse_dt_config()
Subsystems (2)
cover pinctrl: pinconf-generic: clean up pinconf_parse_dt_config()
Masahiro Yamada (2):
pinctrl: pinconf-generic: resize the pin config array directly
pinctrl: pinconf-generic: remove the special handling for no config
case
drivers/pinctrl/pinconf-generic.c | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-)
Signature · 1 line
2.40.1
PATCH 1/2 pinctrl: pinconf-generic: resize the pin config array directly
pinconf_generic_parse_dt_config() allocates memory that is large enough to contain all the config parameters. Then, kmemdup() copies the found configs to the memory with the exact size.
There is no need to allocate memory twice; you can directly resize the initial memory using krealloc_array().
I also changed kcalloc() to kmalloc_array() to keep the consistency with krealloc_array(). This change has no impact because you do not need to zero out the 'cfg' array.
---
drivers/pinctrl/pinconf-generic.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)
drivers/pinctrl/pinconf-generic.c +8 -17
@@ -247,7 +247,6 @@ int pinconf_generic_parse_dt_config(struct device_node *np, {unsigned long *cfg;unsigned int max_cfg, ncfg =0 ; -int ret;if (!np)return -EINVAL ;
@@ -256,7 +255,7 @@ int pinconf_generic_parse_dt_config(struct device_node *np, max_cfg =ARRAY_SIZE (dt_params);if (pctldev) max_cfg += pctldev->desc->num_custom_params; - cfg =kcalloc (max_cfg,sizeof (*cfg),GFP_KERNEL ); + cfg =kmalloc_array (max_cfg,sizeof (*cfg),GFP_KERNEL );if (!cfg)return -ENOMEM ;
@@ -266,30 +265,22 @@ int pinconf_generic_parse_dt_config(struct device_node *np,parse_dt_cfg (np, pctldev->desc->custom_params, pctldev->desc->num_custom_params, cfg, &ncfg); - ret =0 ; -/* no configs found at all */ if (ncfg ==0 ) { +kfree (cfg); *configs =NULL ; *nconfigs =0 ; - goto out; +return 0 ; } -/* -* Now limit the number of configs to the real number of -* found properties. -*/ - *configs =kmemdup (cfg, ncfg *sizeof (unsigned long ),GFP_KERNEL ); -if (!*configs) { - ret = -ENOMEM ; - goto out; - } +/* Now resize the array to store the real number of found properties. */ + *configs =krealloc_array (cfg, ncfg,sizeof (unsigned long ),GFP_KERNEL ); +if (!*configs) +return -ENOMEM ; *nconfigs = ncfg; -out: -kfree (cfg); -return ret; +return 0 ; }EXPORT_SYMBOL_GPL (pinconf_generic_parse_dt_config);
Signature · 1 line
2.40.1
Re: [PATCH 1/2] pinctrl: pinconf-generic: resize the pin config array directly
Quoted 5 lines · Masahiro Yamada
pinconf_generic_parse_dt_config()allocates memory that is large enough to contain all the config parameters. Then,kmemdup()copies the found configs to the memory with the exact size.
Quoted 8 lines
There is no need to allocate memory twice; you can directly resize the initial memory using
krealloc_array().I also changed
kcalloc()tokmalloc_array()to keep the consistency withkrealloc_array(). This change has no impact because you do not need to zero out the 'cfg' array.Signed-off-by Masahiro Yamada <[email protected]>
Sorry, I retract this patch set.
krealloc() does not save any memory
when the new_size is smaller than the current size.
Quoted 67 lines
---
drivers/pinctrl/pinconf-generic.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)drivers/pinctrl/pinconf-generic.c +8
-18@@ -247,7 +247,6 @@ int pinconf_generic_parse_dt_config(struct device_node *np, {unsigned long *cfg;unsigned int max_cfg, ncfg =0 ; -int ret;if (!np)return -EINVAL ;@@ -256,7 +255,7 @@ int pinconf_generic_parse_dt_config(struct device_node *np, max_cfg =ARRAY_SIZE (dt_params);if (pctldev) max_cfg += pctldev->desc->num_custom_params; - cfg =kcalloc (max_cfg,sizeof (*cfg),GFP_KERNEL ); + cfg =kmalloc_array (max_cfg,sizeof (*cfg),GFP_KERNEL );if (!cfg)return -ENOMEM ;@@ -266,30 +265,22 @@ int pinconf_generic_parse_dt_config(struct device_node *np,parse_dt_cfg (np, pctldev->desc->custom_params, pctldev->desc->num_custom_params, cfg, &ncfg); - ret =0 ; -/* no configs found at all */ if (ncfg ==0 ) { +kfree (cfg); *configs =NULL ; *nconfigs =0 ; - goto out; +return 0 ; } -/* -* Now limit the number of configs to the real number of -* found properties. -*/ - *configs =kmemdup (cfg, ncfg *sizeof (unsigned long ),GFP_KERNEL ); -if (!*configs) { - ret = -ENOMEM ; - goto out; - } +/* Now resize the array to store the real number of found properties. */ + *configs =krealloc_array (cfg, ncfg,sizeof (unsigned long ),GFP_KERNEL ); +if (!*configs) +return -ENOMEM ; *nconfigs = ncfg; -out: -kfree (cfg); -return ret; +return 0 ; }EXPORT_SYMBOL_GPL (pinconf_generic_parse_dt_config); --2.40.1
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH 1/2] pinctrl: pinconf-generic: resize the pin config array directly
Hi Masahiro,
Quoted 13 lines · Masahiro Yamada
Quoted 12 lines · Masahiro Yamada
pinconf_generic_parse_dt_config()allocates memory that is large enough to contain all the config parameters. Then,kmemdup()copies the found configs to the memory with the exact size.There is no need to allocate memory twice; you can directly resize the initial memory using
krealloc_array().I also changed
kcalloc()tokmalloc_array()to keep the consistency withkrealloc_array(). This change has no impact because you do not need to zero out the 'cfg' array.
Quoted 6 lines
Quoted 1 line
Signed-off-by Masahiro Yamada <[email protected]>Sorry, I retract this patch set.
krealloc()does not save any memory
when thenew_sizeis smaller than the current size.
But the first part where you switch to kmalloc_array() is still a nice change.
The fact that we use kmemdup to be able to also shrink the allocation is a bit of an oddity I guess, but let's run this patch by Andy Shevchenko, and ask what he thinks about simply introducing kmemdup_array() or if he has other ideas for this.
Yours,
Linus Walleij
Re: [PATCH 1/2] pinctrl: pinconf-generic: resize the pin config array directly
Quoted 21 lines · Linus Walleij
Quoted 13 lines · Masahiro Yamada
Quoted 12 lines · Masahiro Yamada
pinconf_generic_parse_dt_config()allocates memory that is large enough to contain all the config parameters. Then,kmemdup()copies the found configs to the memory with the exact size.There is no need to allocate memory twice; you can directly resize the initial memory using
krealloc_array().I also changed
kcalloc()tokmalloc_array()to keep the consistency withkrealloc_array(). This change has no impact because you do not need to zero out the 'cfg' array.Quoted 6 lines
Quoted 1 line
Signed-off-by Masahiro Yamada <[email protected]>Sorry, I retract this patch set.
krealloc()does not save any memory
when thenew_sizeis smaller than the current size.
Quoted 6 lines
But the first part where you switch to
kmalloc_array()is still a nice change.The fact that we use kmemdup to be able to also shrink the allocation is a bit of an oddity I guess, but let's run this patch by Andy Shevchenko, and ask what he thinks about simply introducing
kmemdup_array()or if he has other ideas for this.
https://lore.kernel.org/all/[email protected]/
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 1/2] pinctrl: pinconf-generic: resize the pin config array directly
Quoted 23 lines · Andy Shevchenko
Quoted 21 lines · Linus Walleij
Quoted 19 lines · Masahiro Yamada
Quoted 5 lines · Masahiro Yamada
pinconf_generic_parse_dt_config()allocates memory that is large enough to contain all the config parameters. Then,kmemdup()copies the found configs to the memory with the exact size.Quoted 8 lines
There is no need to allocate memory twice; you can directly resize the initial memory using
krealloc_array().I also changed
kcalloc()tokmalloc_array()to keep the consistency withkrealloc_array(). This change has no impact because you do not need to zero out the 'cfg' array.Signed-off-by Masahiro Yamada <[email protected]>Sorry, I retract this patch set.
krealloc()does not save any memory
when thenew_sizeis smaller than the current size.
Quoted 8 lines
Quoted 6 lines
But the first part where you switch to
kmalloc_array()is still a nice change.The fact that we use kmemdup to be able to also shrink the allocation is a bit of an oddity I guess, but let's run this patch by Andy Shevchenko, and ask what he thinks about simply introducing
kmemdup_array()or if he has other ideas for this.
Ok, I will come back when kmemdup_array() is upstreamed.
Quoted 5 lines
--
With Best Regards,
Andy Shevchenko
Signature · 2 lines
Best Regards Masahiro Yamada
PATCH 2/2 pinctrl: pinconf-generic: remove the special handling for no config case
In reply to Masahiro YamadaTo further simplify pinconf_generic_parse_dt_config(), eliminate the handling of the case where no configuration is found.
When ncfg is zero, krealloc_array() will set ZERO_SIZE_PTR to *configs, which is a natural approach for managing a zero-size buffer.
This should have no impact because none of the callers accesses 'configs' when ncfg is zero. Also, it is safe to pass ZERO_SIZE_PTR to kfree().
---
drivers/pinctrl/pinconf-generic.c | 8 -------- 1 file changed, 8 deletions(-)
drivers/pinctrl/pinconf-generic.c +0 -8
@@ -265,14 +265,6 @@ int pinconf_generic_parse_dt_config(struct device_node *np,parse_dt_cfg (np, pctldev->desc->custom_params, pctldev->desc->num_custom_params, cfg, &ncfg); -/* no configs found at all */ -if (ncfg ==0 ) { -kfree (cfg); - *configs =NULL ; - *nconfigs =0 ; -return 0 ; - } -/* Now resize the array to store the real number of found properties. */ *configs =krealloc_array (cfg, ncfg,sizeof (unsigned long ),GFP_KERNEL );if (!*configs)
Signature · 1 line
2.40.1