diff mbox

tvp7002: use devm_kzalloc() instead of kzalloc()

Message ID 1357219362-9080-4-git-send-email-prabhakar.lad@ti.com (mailing list archive)
State Not Applicable
Headers show

Commit Message

Lad, Prabhakar Jan. 3, 2013, 1:22 p.m. UTC
I2C drivers can use devm_kzalloc() too in their .probe() methods. Doing so
simplifies their clean up paths.

Signed-off-by: Lad, Prabhakar <prabhakar.lad@ti.com>
Signed-off-by: Manjunath Hadli <manjunath.hadli@ti.com>
---
 drivers/media/i2c/tvp7002.c |   10 ++--------
 1 files changed, 2 insertions(+), 8 deletions(-)

Comments

Laurent Pinchart Jan. 3, 2013, 1:31 p.m. UTC | #1
Hi Prabhakar,

Thanks for the patch.

On Thursday 03 January 2013 18:52:42 Lad, Prabhakar wrote:
> I2C drivers can use devm_kzalloc() too in their .probe() methods. Doing so
> simplifies their clean up paths.
> 
> Signed-off-by: Lad, Prabhakar <prabhakar.lad@ti.com>
> Signed-off-by: Manjunath Hadli <manjunath.hadli@ti.com>
> ---
>  drivers/media/i2c/tvp7002.c |   10 ++--------
>  1 files changed, 2 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/media/i2c/tvp7002.c b/drivers/media/i2c/tvp7002.c
> index fb6a5b5..2d4c86e 100644
> --- a/drivers/media/i2c/tvp7002.c
> +++ b/drivers/media/i2c/tvp7002.c
> @@ -1036,7 +1036,7 @@ static int tvp7002_probe(struct i2c_client *c, const
> struct i2c_device_id *id) return -ENODEV;
>  	}
> 
> -	device = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
> +	device = devm_kzalloc(&c->dev, sizeof(struct tvp7002), GFP_KERNEL);
> 
>  	if (!device)
>  		return -ENOMEM;
> @@ -1088,17 +1088,12 @@ static int tvp7002_probe(struct i2c_client *c, const
> struct i2c_device_id *id) V4L2_CID_GAIN, 0, 255, 1, 0);
>  	sd->ctrl_handler = &device->hdl;
>  	if (device->hdl.error) {
> -		int err = device->hdl.error;
> -
>  		v4l2_ctrl_handler_free(&device->hdl);
> -		kfree(device);
> -		return err;
> +		return device->hdl.error;

At this point device->hdl as been freed (or rather uninitialized, as the 
structure is not dynamically allocated by the control framework), so device-
>hdl.error is undefined. That's why you need the local err variable.

>  	}
>  	v4l2_ctrl_handler_setup(&device->hdl);
> 
>  found_error:
> -	if (error < 0)
> -		kfree(device);

You can remove the found_error label and return errors directly instead of 
using goto's.

>  	return error;

And this can then be turned into return 0.

>  }
> @@ -1120,7 +1115,6 @@ static int tvp7002_remove(struct i2c_client *c)
> 
>  	v4l2_device_unregister_subdev(sd);
>  	v4l2_ctrl_handler_free(&device->hdl);
> -	kfree(device);
>  	return 0;
>  }
Laurent Pinchart Jan. 3, 2013, 1:32 p.m. UTC | #2
On Thursday 03 January 2013 14:31:20 Laurent Pinchart wrote:
> On Thursday 03 January 2013 18:52:42 Lad, Prabhakar wrote:
> > I2C drivers can use devm_kzalloc() too in their .probe() methods. Doing so
> > simplifies their clean up paths.
> > 
> > Signed-off-by: Lad, Prabhakar <prabhakar.lad@ti.com>
> > Signed-off-by: Manjunath Hadli <manjunath.hadli@ti.com>
> > ---
> > 
> >  drivers/media/i2c/tvp7002.c |   10 ++--------
> >  1 files changed, 2 insertions(+), 8 deletions(-)
> > 
> > diff --git a/drivers/media/i2c/tvp7002.c b/drivers/media/i2c/tvp7002.c
> > index fb6a5b5..2d4c86e 100644
> > --- a/drivers/media/i2c/tvp7002.c
> > +++ b/drivers/media/i2c/tvp7002.c
> > @@ -1036,7 +1036,7 @@ static int tvp7002_probe(struct i2c_client *c, const
> > struct i2c_device_id *id) return -ENODEV;
> > 
> >  	}
> > 
> > -	device = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
> > +	device = devm_kzalloc(&c->dev, sizeof(struct tvp7002), GFP_KERNEL);
> > 
> >  	if (!device)
> >  	
> >  		return -ENOMEM;
> > 
> > @@ -1088,17 +1088,12 @@ static int tvp7002_probe(struct i2c_client *c,
> > const struct i2c_device_id *id) V4L2_CID_GAIN, 0, 255, 1, 0);
> > 
> >  	sd->ctrl_handler = &device->hdl;
> >  	if (device->hdl.error) {
> > 
> > -		int err = device->hdl.error;
> > -
> > 
> >  		v4l2_ctrl_handler_free(&device->hdl);
> > 
> > -		kfree(device);
> > -		return err;
> > +		return device->hdl.error;
> 
> At this point device->hdl as been freed (or rather uninitialized, as the
> structure is not dynamically allocated by the control framework), so device-
> >hdl.error is undefined. That's why you need the local err variable.

And this comment holds true for the other patches in this series.

> >
> >  	}
> >  	v4l2_ctrl_handler_setup(&device->hdl);
> >  
> >  found_error:
> > -	if (error < 0)
> > -		kfree(device);
> 
> You can remove the found_error label and return errors directly instead of
> using goto's.
> 
> >  	return error;
> 
> And this can then be turned into return 0.
> 
> >  }
> > 
> > @@ -1120,7 +1115,6 @@ static int tvp7002_remove(struct i2c_client *c)
> > 
> >  	v4l2_device_unregister_subdev(sd);
> >  	v4l2_ctrl_handler_free(&device->hdl);
> > 
> > -	kfree(device);
> > 
> >  	return 0;
> >  
> >  }
Lad, Prabhakar Jan. 3, 2013, 1:33 p.m. UTC | #3
Hi Laurent,

Thanks for the quick review!

On Thu, Jan 3, 2013 at 7:02 PM, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
> On Thursday 03 January 2013 14:31:20 Laurent Pinchart wrote:
>> On Thursday 03 January 2013 18:52:42 Lad, Prabhakar wrote:
>> > I2C drivers can use devm_kzalloc() too in their .probe() methods. Doing so
>> > simplifies their clean up paths.
>> >
>> > Signed-off-by: Lad, Prabhakar <prabhakar.lad@ti.com>
>> > Signed-off-by: Manjunath Hadli <manjunath.hadli@ti.com>
>> > ---
>> >
>> >  drivers/media/i2c/tvp7002.c |   10 ++--------
>> >  1 files changed, 2 insertions(+), 8 deletions(-)
>> >
>> > diff --git a/drivers/media/i2c/tvp7002.c b/drivers/media/i2c/tvp7002.c
>> > index fb6a5b5..2d4c86e 100644
>> > --- a/drivers/media/i2c/tvp7002.c
>> > +++ b/drivers/media/i2c/tvp7002.c
>> > @@ -1036,7 +1036,7 @@ static int tvp7002_probe(struct i2c_client *c, const
>> > struct i2c_device_id *id) return -ENODEV;
>> >
>> >     }
>> >
>> > -   device = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
>> > +   device = devm_kzalloc(&c->dev, sizeof(struct tvp7002), GFP_KERNEL);
>> >
>> >     if (!device)
>> >
>> >             return -ENOMEM;
>> >
>> > @@ -1088,17 +1088,12 @@ static int tvp7002_probe(struct i2c_client *c,
>> > const struct i2c_device_id *id) V4L2_CID_GAIN, 0, 255, 1, 0);
>> >
>> >     sd->ctrl_handler = &device->hdl;
>> >     if (device->hdl.error) {
>> >
>> > -           int err = device->hdl.error;
>> > -
>> >
>> >             v4l2_ctrl_handler_free(&device->hdl);
>> >
>> > -           kfree(device);
>> > -           return err;
>> > +           return device->hdl.error;
>>
>> At this point device->hdl as been freed (or rather uninitialized, as the
>> structure is not dynamically allocated by the control framework), so device-
>> >hdl.error is undefined. That's why you need the local err variable.
>
> And this comment holds true for the other patches in this series.
>
Ok I'll fix it respin a v2.

>> >
>> >     }
>> >     v4l2_ctrl_handler_setup(&device->hdl);
>> >
>> >  found_error:
>> > -   if (error < 0)
>> > -           kfree(device);
>>
>> You can remove the found_error label and return errors directly instead of
>> using goto's.
>>
Ok

>> >     return error;
>>
>> And this can then be turned into return 0.
Ok.

Regards,
--Prabhakar

>>
>> >  }
>> >
>> > @@ -1120,7 +1115,6 @@ static int tvp7002_remove(struct i2c_client *c)
>> >
>> >     v4l2_device_unregister_subdev(sd);
>> >     v4l2_ctrl_handler_free(&device->hdl);
>> >
>> > -   kfree(device);
>> >
>> >     return 0;
>> >
>> >  }
>
> --
> Regards,
>
> Laurent Pinchart
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-media" 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/media/i2c/tvp7002.c b/drivers/media/i2c/tvp7002.c
index fb6a5b5..2d4c86e 100644
--- a/drivers/media/i2c/tvp7002.c
+++ b/drivers/media/i2c/tvp7002.c
@@ -1036,7 +1036,7 @@  static int tvp7002_probe(struct i2c_client *c, const struct i2c_device_id *id)
 		return -ENODEV;
 	}
 
-	device = kzalloc(sizeof(struct tvp7002), GFP_KERNEL);
+	device = devm_kzalloc(&c->dev, sizeof(struct tvp7002), GFP_KERNEL);
 
 	if (!device)
 		return -ENOMEM;
@@ -1088,17 +1088,12 @@  static int tvp7002_probe(struct i2c_client *c, const struct i2c_device_id *id)
 			V4L2_CID_GAIN, 0, 255, 1, 0);
 	sd->ctrl_handler = &device->hdl;
 	if (device->hdl.error) {
-		int err = device->hdl.error;
-
 		v4l2_ctrl_handler_free(&device->hdl);
-		kfree(device);
-		return err;
+		return device->hdl.error;
 	}
 	v4l2_ctrl_handler_setup(&device->hdl);
 
 found_error:
-	if (error < 0)
-		kfree(device);
 
 	return error;
 }
@@ -1120,7 +1115,6 @@  static int tvp7002_remove(struct i2c_client *c)
 
 	v4l2_device_unregister_subdev(sd);
 	v4l2_ctrl_handler_free(&device->hdl);
-	kfree(device);
 	return 0;
 }