diff mbox

pwm: core: Use devm_kzalloc instead of kzalloc

Message ID 1389074700-1825-1-git-send-email-Li.Xiubo@freescale.com
State Superseded
Headers show

Commit Message

Xiubo Li Jan. 7, 2014, 6:05 a.m. UTC
Use devm_kzalloc instead of kzalloc to free automatically and make
the cleanup paths simpler and the code slightly shorter.

Signed-off-by: Xiubo Li <Li.Xiubo@freescale.com>
---
 drivers/pwm/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Joe Perches Jan. 7, 2014, 7:14 a.m. UTC | #1
On Tue, 2014-01-07 at 14:05 +0800, Xiubo Li wrote:
> Use devm_kzalloc instead of kzalloc to free automatically and make
> the cleanup paths simpler and the code slightly shorter.
[]
> diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
[]
> @@ -245,7 +244,9 @@ int pwmchip_add(struct pwm_chip *chip)
>  	if (ret < 0)
>  		goto out;
>  
> -	chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
> +	chip->pwms = devm_kzalloc(chip->dev,
> +				  chip->npwm * sizeof(*pwm),
> +				  GFP_KERNEL);

There is a devm_kcalloc and this should likely use it.


--
To unsubscribe from this list: send the line "unsubscribe linux-pwm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Xiubo Li Jan. 7, 2014, 7:36 a.m. UTC | #2
> > Use devm_kzalloc instead of kzalloc to free automatically and make
> > the cleanup paths simpler and the code slightly shorter.
> []
> > diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> []
> > @@ -245,7 +244,9 @@ int pwmchip_add(struct pwm_chip *chip)
> >  	if (ret < 0)
> >  		goto out;
> >
> > -	chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
> > +	chip->pwms = devm_kzalloc(chip->dev,
> > +				  chip->npwm * sizeof(*pwm),
> > +				  GFP_KERNEL);
> 
> There is a devm_kcalloc and this should likely use it.
> 

Yes, I will use it.

Thanks,

 

--
To unsubscribe from this list: send the line "unsubscribe linux-pwm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Dmitry Torokhov Jan. 7, 2014, 8:01 a.m. UTC | #3
Hi Xiubo,

On Tue, Jan 07, 2014 at 02:05:00PM +0800, Xiubo Li wrote:
> Use devm_kzalloc instead of kzalloc to free automatically and make
> the cleanup paths simpler and the code slightly shorter.
> 
> Signed-off-by: Xiubo Li <Li.Xiubo@freescale.com>
> ---
>  drivers/pwm/core.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> index 2ca9504..74c9f9a 100644
> --- a/drivers/pwm/core.c
> +++ b/drivers/pwm/core.c
> @@ -80,7 +80,6 @@ static void free_pwms(struct pwm_chip *chip)
>  
>  	bitmap_clear(allocated_pwms, chip->base, chip->npwm);
>  
> -	kfree(chip->pwms);
>  	chip->pwms = NULL;
>  }
>  
> @@ -245,7 +244,9 @@ int pwmchip_add(struct pwm_chip *chip)
>  	if (ret < 0)
>  		goto out;
>  
> -	chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
> +	chip->pwms = devm_kzalloc(chip->dev,
> +				  chip->npwm * sizeof(*pwm),
> +				  GFP_KERNEL);
>  	if (!chip->pwms) {
>  		ret = -ENOMEM;
>  		goto out;

Is it guaranteed that pwmchip_add()/free_pwms() will only be called in
probe() and remove() paths? It is probably safe assumption, but maybe it
should be mentioned in comments now that we definitely have this
restricion.

Thanks.
Xiubo Li Jan. 8, 2014, 4:58 a.m. UTC | #4
> >  drivers/pwm/core.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> > index 2ca9504..74c9f9a 100644
> > --- a/drivers/pwm/core.c
> > +++ b/drivers/pwm/core.c
> > @@ -80,7 +80,6 @@ static void free_pwms(struct pwm_chip *chip)
> >
> >  	bitmap_clear(allocated_pwms, chip->base, chip->npwm);
> >
> > -	kfree(chip->pwms);
> >  	chip->pwms = NULL;
> >  }
> >
> > @@ -245,7 +244,9 @@ int pwmchip_add(struct pwm_chip *chip)
> >  	if (ret < 0)
> >  		goto out;
> >
> > -	chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
> > +	chip->pwms = devm_kzalloc(chip->dev,
> > +				  chip->npwm * sizeof(*pwm),
> > +				  GFP_KERNEL);
> >  	if (!chip->pwms) {
> >  		ret = -ENOMEM;
> >  		goto out;
> 
> Is it guaranteed that pwmchip_add()/free_pwms() will only be called in
> probe() and remove() paths? It is probably safe assumption, but maybe it
> should be mentioned in comments now that we definitely have this
> restricion.
>

Yes, for now they are.
 
Thanks.

--
Best Regards,
Xiubo
--
To unsubscribe from this list: send the line "unsubscribe linux-pwm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 2ca9504..74c9f9a 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -80,7 +80,6 @@  static void free_pwms(struct pwm_chip *chip)
 
 	bitmap_clear(allocated_pwms, chip->base, chip->npwm);
 
-	kfree(chip->pwms);
 	chip->pwms = NULL;
 }
 
@@ -245,7 +244,9 @@  int pwmchip_add(struct pwm_chip *chip)
 	if (ret < 0)
 		goto out;
 
-	chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL);
+	chip->pwms = devm_kzalloc(chip->dev,
+				  chip->npwm * sizeof(*pwm),
+				  GFP_KERNEL);
 	if (!chip->pwms) {
 		ret = -ENOMEM;
 		goto out;