linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()
@ 2019-05-29 13:09 Young Xiao
  2019-06-03 11:55 ` Hans Verkuil
  0 siblings, 1 reply; 9+ messages in thread
From: Young Xiao @ 2019-05-29 13:09 UTC (permalink / raw)
  To: prabhakar.csengg, mchehab, linux-media, linux-kernel; +Cc: Young Xiao

If vpif_probe() fails on vpif_probe_complete(), then memory
allocated at initialize_vpif() for global vpif_obj.dev[i]
become unreleased.

The patch adds deallocation of vpif_obj.dev[i] on the error path.

Signed-off-by: Young Xiao <92siuyang@gmail.com>
---
 drivers/media/platform/davinci/vpif_capture.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
index b5aacb0..63e6ec4 100644
--- a/drivers/media/platform/davinci/vpif_capture.c
+++ b/drivers/media/platform/davinci/vpif_capture.c
@@ -1621,6 +1621,14 @@ vpif_capture_get_pdata(struct platform_device *pdev)
 	return NULL;
 }
 
+static void free_vpif_objs(void)
+{
+	int i;
+
+	for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
+		kfree(vpif_obj.dev[i]);
+}
+
 /**
  * vpif_probe : This function probes the vpif capture driver
  * @pdev: platform device pointer
@@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
 				  "registered sub device %s\n",
 				   subdevdata->name);
 		}
-		vpif_probe_complete();
+		err = vpif_probe_complete();
+		if (err) {
+			goto probe_subdev_out;
+		}
 	} else {
 		vpif_obj.notifier.ops = &vpif_async_ops;
 		err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
@@ -1722,6 +1733,7 @@ static __init int vpif_probe(struct platform_device *pdev)
 	v4l2_device_unregister(&vpif_obj.v4l2_dev);
 cleanup:
 	v4l2_async_notifier_cleanup(&vpif_obj.notifier);
+	free_vpif_objs();
 
 	return err;
 }
@@ -1748,8 +1760,8 @@ static int vpif_remove(struct platform_device *device)
 		ch = vpif_obj.dev[i];
 		/* Unregister video device */
 		video_unregister_device(&ch->video_dev);
-		kfree(vpif_obj.dev[i]);
 	}
+	free_vpif_objs()
 	return 0;
 }
 
-- 
2.7.4


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

* Re: [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()
  2019-05-29 13:09 [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe() Young Xiao
@ 2019-06-03 11:55 ` Hans Verkuil
  2019-06-04  7:15   ` Yang Xiao
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Verkuil @ 2019-06-03 11:55 UTC (permalink / raw)
  To: Young Xiao, prabhakar.csengg, mchehab, linux-media, linux-kernel

On 5/29/19 3:09 PM, Young Xiao wrote:
> If vpif_probe() fails on vpif_probe_complete(), then memory
> allocated at initialize_vpif() for global vpif_obj.dev[i]
> become unreleased.
> 
> The patch adds deallocation of vpif_obj.dev[i] on the error path.
> 
> Signed-off-by: Young Xiao <92siuyang@gmail.com>
> ---
>  drivers/media/platform/davinci/vpif_capture.c | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
> index b5aacb0..63e6ec4 100644
> --- a/drivers/media/platform/davinci/vpif_capture.c
> +++ b/drivers/media/platform/davinci/vpif_capture.c
> @@ -1621,6 +1621,14 @@ vpif_capture_get_pdata(struct platform_device *pdev)
>  	return NULL;
>  }
>  
> +static void free_vpif_objs(void)
> +{
> +	int i;
> +
> +	for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
> +		kfree(vpif_obj.dev[i]);
> +}
> +
>  /**
>   * vpif_probe : This function probes the vpif capture driver
>   * @pdev: platform device pointer
> @@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
>  				  "registered sub device %s\n",
>  				   subdevdata->name);
>  		}
> -		vpif_probe_complete();
> +		err = vpif_probe_complete();
> +		if (err) {
> +			goto probe_subdev_out;
> +		}

No need for { and } as per kernel coding style.

>  	} else {
>  		vpif_obj.notifier.ops = &vpif_async_ops;
>  		err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
> @@ -1722,6 +1733,7 @@ static __init int vpif_probe(struct platform_device *pdev)
>  	v4l2_device_unregister(&vpif_obj.v4l2_dev);
>  cleanup:
>  	v4l2_async_notifier_cleanup(&vpif_obj.notifier);
> +	free_vpif_objs();

This would break the case where initialize_vpif() returns an error, since
initialize_vpif already frees these objects on error.

In this case the easiest way of doing this is to just return if initialize_vpif
returns an error. No need to clean up anything.

Regards,

	Hans

>  
>  	return err;
>  }
> @@ -1748,8 +1760,8 @@ static int vpif_remove(struct platform_device *device)
>  		ch = vpif_obj.dev[i];
>  		/* Unregister video device */
>  		video_unregister_device(&ch->video_dev);
> -		kfree(vpif_obj.dev[i]);
>  	}
> +	free_vpif_objs()
>  	return 0;
>  }
>  
> 


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

* Re: [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()
  2019-06-03 11:55 ` Hans Verkuil
@ 2019-06-04  7:15   ` Yang Xiao
  0 siblings, 0 replies; 9+ messages in thread
From: Yang Xiao @ 2019-06-04  7:15 UTC (permalink / raw)
  To: Hans Verkuil; +Cc: prabhakar.csengg, mchehab, linux-media, LKML

Yes, you are correct. I will fix the issue and resubmit the patch again.

On Mon, Jun 3, 2019 at 7:55 PM Hans Verkuil <hverkuil@xs4all.nl> wrote:
>
> On 5/29/19 3:09 PM, Young Xiao wrote:
> > If vpif_probe() fails on vpif_probe_complete(), then memory
> > allocated at initialize_vpif() for global vpif_obj.dev[i]
> > become unreleased.
> >
> > The patch adds deallocation of vpif_obj.dev[i] on the error path.
> >
> > Signed-off-by: Young Xiao <92siuyang@gmail.com>
> > ---
> >  drivers/media/platform/davinci/vpif_capture.c | 16 ++++++++++++++--
> >  1 file changed, 14 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
> > index b5aacb0..63e6ec4 100644
> > --- a/drivers/media/platform/davinci/vpif_capture.c
> > +++ b/drivers/media/platform/davinci/vpif_capture.c
> > @@ -1621,6 +1621,14 @@ vpif_capture_get_pdata(struct platform_device *pdev)
> >       return NULL;
> >  }
> >
> > +static void free_vpif_objs(void)
> > +{
> > +     int i;
> > +
> > +     for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
> > +             kfree(vpif_obj.dev[i]);
> > +}
> > +
> >  /**
> >   * vpif_probe : This function probes the vpif capture driver
> >   * @pdev: platform device pointer
> > @@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
> >                                 "registered sub device %s\n",
> >                                  subdevdata->name);
> >               }
> > -             vpif_probe_complete();
> > +             err = vpif_probe_complete();
> > +             if (err) {
> > +                     goto probe_subdev_out;
> > +             }
>
> No need for { and } as per kernel coding style.
>
> >       } else {
> >               vpif_obj.notifier.ops = &vpif_async_ops;
> >               err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
> > @@ -1722,6 +1733,7 @@ static __init int vpif_probe(struct platform_device *pdev)
> >       v4l2_device_unregister(&vpif_obj.v4l2_dev);
> >  cleanup:
> >       v4l2_async_notifier_cleanup(&vpif_obj.notifier);
> > +     free_vpif_objs();
>
> This would break the case where initialize_vpif() returns an error, since
> initialize_vpif already frees these objects on error.
>
> In this case the easiest way of doing this is to just return if initialize_vpif
> returns an error. No need to clean up anything.
>
> Regards,
>
>         Hans
>
> >
> >       return err;
> >  }
> > @@ -1748,8 +1760,8 @@ static int vpif_remove(struct platform_device *device)
> >               ch = vpif_obj.dev[i];
> >               /* Unregister video device */
> >               video_unregister_device(&ch->video_dev);
> > -             kfree(vpif_obj.dev[i]);
> >       }
> > +     free_vpif_objs()
> >       return 0;
> >  }
> >
> >
>

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

* Re: [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()
  2019-06-04 12:26 Young Xiao
@ 2019-06-05 20:20 ` Lad, Prabhakar
  0 siblings, 0 replies; 9+ messages in thread
From: Lad, Prabhakar @ 2019-06-05 20:20 UTC (permalink / raw)
  To: Young Xiao; +Cc: Mauro Carvalho Chehab, Hans Verkuil, linux-media, LKML

On Tue, Jun 4, 2019 at 1:25 PM Young Xiao <92siuyang@gmail.com> wrote:
>
> If vpif_probe() fails on v4l2_device_register() and vpif_probe_complete(),
> then memory allocated at initialize_vpif() for global vpif_obj.dev[i]
> become unreleased.
>
> The patch adds deallocation of vpif_obj.dev[i] on the error path.
>
> Signed-off-by: Young Xiao <92siuyang@gmail.com>
> ---
>  drivers/media/platform/davinci/vpif_capture.c | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
>
Acked-by: Lad, Prabhakar <prabhakar.csengg@gmail.com>

Cheers,
--Prabhakar Lad

> diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
> index b5aacb0..75c2c10 100644
> --- a/drivers/media/platform/davinci/vpif_capture.c
> +++ b/drivers/media/platform/davinci/vpif_capture.c
> @@ -1385,6 +1385,14 @@ static int initialize_vpif(void)
>         return err;
>  }
>
> +static inline void free_vpif_objs(void)
> +{
> +       int i;
> +
> +       for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
> +               kfree(vpif_obj.dev[i]);
> +}
> +
>  static int vpif_async_bound(struct v4l2_async_notifier *notifier,
>                             struct v4l2_subdev *subdev,
>                             struct v4l2_async_subdev *asd)
> @@ -1654,7 +1662,7 @@ static __init int vpif_probe(struct platform_device *pdev)
>         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
>         if (err) {
>                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
> -               goto cleanup;
> +               goto vpif_free;
>         }
>
>         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
> @@ -1701,7 +1709,9 @@ static __init int vpif_probe(struct platform_device *pdev)
>                                   "registered sub device %s\n",
>                                    subdevdata->name);
>                 }
> -               vpif_probe_complete();
> +               err = vpif_probe_complete();
> +               if (err)
> +                       goto probe_subdev_out;
>         } else {
>                 vpif_obj.notifier.ops = &vpif_async_ops;
>                 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
> @@ -1720,6 +1730,8 @@ static __init int vpif_probe(struct platform_device *pdev)
>         kfree(vpif_obj.sd);
>  vpif_unregister:
>         v4l2_device_unregister(&vpif_obj.v4l2_dev);
> +vpif_free:
> +       free_vpif_objs();
>  cleanup:
>         v4l2_async_notifier_cleanup(&vpif_obj.notifier);
>
> --
> 2.7.4
>

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

* [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()
@ 2019-06-04 12:26 Young Xiao
  2019-06-05 20:20 ` Lad, Prabhakar
  0 siblings, 1 reply; 9+ messages in thread
From: Young Xiao @ 2019-06-04 12:26 UTC (permalink / raw)
  To: prabhakar.csengg, mchehab, hverkuil, linux-media, linux-kernel; +Cc: Young Xiao

If vpif_probe() fails on v4l2_device_register() and vpif_probe_complete(),
then memory allocated at initialize_vpif() for global vpif_obj.dev[i]
become unreleased.

The patch adds deallocation of vpif_obj.dev[i] on the error path.

Signed-off-by: Young Xiao <92siuyang@gmail.com>
---
 drivers/media/platform/davinci/vpif_capture.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
index b5aacb0..75c2c10 100644
--- a/drivers/media/platform/davinci/vpif_capture.c
+++ b/drivers/media/platform/davinci/vpif_capture.c
@@ -1385,6 +1385,14 @@ static int initialize_vpif(void)
 	return err;
 }
 
+static inline void free_vpif_objs(void)
+{
+	int i;
+
+	for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
+		kfree(vpif_obj.dev[i]);
+}
+
 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
 			    struct v4l2_subdev *subdev,
 			    struct v4l2_async_subdev *asd)
@@ -1654,7 +1662,7 @@ static __init int vpif_probe(struct platform_device *pdev)
 	err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
 	if (err) {
 		v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
-		goto cleanup;
+		goto vpif_free;
 	}
 
 	while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
@@ -1701,7 +1709,9 @@ static __init int vpif_probe(struct platform_device *pdev)
 				  "registered sub device %s\n",
 				   subdevdata->name);
 		}
-		vpif_probe_complete();
+		err = vpif_probe_complete();
+		if (err)
+			goto probe_subdev_out;
 	} else {
 		vpif_obj.notifier.ops = &vpif_async_ops;
 		err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
@@ -1720,6 +1730,8 @@ static __init int vpif_probe(struct platform_device *pdev)
 	kfree(vpif_obj.sd);
 vpif_unregister:
 	v4l2_device_unregister(&vpif_obj.v4l2_dev);
+vpif_free:
+	free_vpif_objs();
 cleanup:
 	v4l2_async_notifier_cleanup(&vpif_obj.notifier);
 
-- 
2.7.4


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

* Re: [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()
  2019-06-04  8:33   ` Yang Xiao
@ 2019-06-04  8:47     ` Lad, Prabhakar
  0 siblings, 0 replies; 9+ messages in thread
From: Lad, Prabhakar @ 2019-06-04  8:47 UTC (permalink / raw)
  To: Yang Xiao; +Cc: Mauro Carvalho Chehab, linux-media, LKML, Hans Verkuil

Cheers,
--Prabhakar Lad

On Tue, Jun 4, 2019 at 9:34 AM Yang Xiao <92siuyang@gmail.com> wrote:
>
> On Tue, Jun 4, 2019 at 4:15 PM Lad, Prabhakar
> <prabhakar.csengg@gmail.com> wrote:
> >
> > Hi Young,
> >
> > Thanks for the patch.
> >
> > On Tue, Jun 4, 2019 at 8:49 AM Young Xiao <92siuyang@gmail.com> wrote:
> > >
> > > If vpif_probe() fails on v4l2_device_register() and vpif_probe_complete(),
> > > then memory allocated at initialize_vpif() for global vpif_obj.dev[i]
> > > become unreleased.
> > >
> > > The patch adds deallocation of vpif_obj.dev[i] on the error path.
> > >
> > > Signed-off-by: Young Xiao <92siuyang@gmail.com>
> > > ---
> > >  drivers/media/platform/davinci/vpif_capture.c | 19 ++++++++++++++++---
> > >  1 file changed, 16 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
> > > index b5aacb0..277d500 100644
> > > --- a/drivers/media/platform/davinci/vpif_capture.c
> > > +++ b/drivers/media/platform/davinci/vpif_capture.c
> > > @@ -1385,6 +1385,14 @@ static int initialize_vpif(void)
> > >         return err;
> > >  }
> > >
> > > +static void free_vpif_objs(void)
> > > +{
> > function could be made inline.
> >
> > > +       int i;
> > > +
> > > +       for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
> >
> > VPIF_DISPLAY_MAX_DEVICES ? this should be  VPIF_CAPTURE_MAX_DEVICES
> >
> > > +               kfree(vpif_obj.dev[i]);
> > > +}
> > > +
> > >  static int vpif_async_bound(struct v4l2_async_notifier *notifier,
> > >                             struct v4l2_subdev *subdev,
> > >                             struct v4l2_async_subdev *asd)
> > > @@ -1654,7 +1662,7 @@ static __init int vpif_probe(struct platform_device *pdev)
> > >         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
> > >         if (err) {
> > >                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
> > > -               goto cleanup;
> > > +               goto vpif_free;
> > >         }
> > >
> > >         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
> > > @@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
> > >                                   "registered sub device %s\n",
> > >                                    subdevdata->name);
> > >                 }
> > > -               vpif_probe_complete();
> > > +               err = vpif_probe_complete();
> > > +               if (err) {
> > > +                       goto probe_subdev_out;
> > > +               }
> >
> > No need for { and } as per kernel coding style.
>
> Sorry, I can not get your point here.
> There is no need to check the return value of vpif_probe_complete(), isn't it?
> So, we just fix the memory leak in the error path of v4l2_device_register()?
>
Not sure if you got my statement here, it means do not add {} braces
around the if statement,
so for example the code should look like something below (refer the
link [1] for more reading)

 err = vpif_probe_complete();
 if (err)
    goto probe_subdev_out;


[1] https://www.kernel.org/doc/html/v4.10/process/coding-style.html#placing-braces-and-spaces

Cheers,
--Prabhakar Lad

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

* Re: [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()
  2019-06-04  8:15 ` Lad, Prabhakar
@ 2019-06-04  8:33   ` Yang Xiao
  2019-06-04  8:47     ` Lad, Prabhakar
  0 siblings, 1 reply; 9+ messages in thread
From: Yang Xiao @ 2019-06-04  8:33 UTC (permalink / raw)
  To: Lad, Prabhakar; +Cc: Mauro Carvalho Chehab, linux-media, LKML, Hans Verkuil

On Tue, Jun 4, 2019 at 4:15 PM Lad, Prabhakar
<prabhakar.csengg@gmail.com> wrote:
>
> Hi Young,
>
> Thanks for the patch.
>
> On Tue, Jun 4, 2019 at 8:49 AM Young Xiao <92siuyang@gmail.com> wrote:
> >
> > If vpif_probe() fails on v4l2_device_register() and vpif_probe_complete(),
> > then memory allocated at initialize_vpif() for global vpif_obj.dev[i]
> > become unreleased.
> >
> > The patch adds deallocation of vpif_obj.dev[i] on the error path.
> >
> > Signed-off-by: Young Xiao <92siuyang@gmail.com>
> > ---
> >  drivers/media/platform/davinci/vpif_capture.c | 19 ++++++++++++++++---
> >  1 file changed, 16 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
> > index b5aacb0..277d500 100644
> > --- a/drivers/media/platform/davinci/vpif_capture.c
> > +++ b/drivers/media/platform/davinci/vpif_capture.c
> > @@ -1385,6 +1385,14 @@ static int initialize_vpif(void)
> >         return err;
> >  }
> >
> > +static void free_vpif_objs(void)
> > +{
> function could be made inline.
>
> > +       int i;
> > +
> > +       for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
>
> VPIF_DISPLAY_MAX_DEVICES ? this should be  VPIF_CAPTURE_MAX_DEVICES
>
> > +               kfree(vpif_obj.dev[i]);
> > +}
> > +
> >  static int vpif_async_bound(struct v4l2_async_notifier *notifier,
> >                             struct v4l2_subdev *subdev,
> >                             struct v4l2_async_subdev *asd)
> > @@ -1654,7 +1662,7 @@ static __init int vpif_probe(struct platform_device *pdev)
> >         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
> >         if (err) {
> >                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
> > -               goto cleanup;
> > +               goto vpif_free;
> >         }
> >
> >         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
> > @@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
> >                                   "registered sub device %s\n",
> >                                    subdevdata->name);
> >                 }
> > -               vpif_probe_complete();
> > +               err = vpif_probe_complete();
> > +               if (err) {
> > +                       goto probe_subdev_out;
> > +               }
>
> No need for { and } as per kernel coding style.

Sorry, I can not get your point here.
There is no need to check the return value of vpif_probe_complete(), isn't it?
So, we just fix the memory leak in the error path of v4l2_device_register()?

>
> >         } else {
> >                 vpif_obj.notifier.ops = &vpif_async_ops;
> >                 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
> > @@ -1720,6 +1731,8 @@ static __init int vpif_probe(struct platform_device *pdev)
> >         kfree(vpif_obj.sd);
> >  vpif_unregister:
> >         v4l2_device_unregister(&vpif_obj.v4l2_dev);
> > +vpif_free:
> > +       free_vpif_objs();
> >  cleanup:
> >         v4l2_async_notifier_cleanup(&vpif_obj.notifier);
> >
> > @@ -1748,8 +1761,8 @@ static int vpif_remove(struct platform_device *device)
> >                 ch = vpif_obj.dev[i];
> >                 /* Unregister video device */
> >                 video_unregister_device(&ch->video_dev);
> > -               kfree(vpif_obj.dev[i]);
> >         }
> > +       free_vpif_objs();
>
> no need for this change, leave it as it is.
>
> Cheers,
> Prabhakar Lad



-- 
Best regards!

Young
-----------------------------------------------------------

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

* Re: [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()
  2019-06-04  7:50 Young Xiao
@ 2019-06-04  8:15 ` Lad, Prabhakar
  2019-06-04  8:33   ` Yang Xiao
  0 siblings, 1 reply; 9+ messages in thread
From: Lad, Prabhakar @ 2019-06-04  8:15 UTC (permalink / raw)
  To: Young Xiao; +Cc: Mauro Carvalho Chehab, linux-media, LKML, Hans Verkuil

Hi Young,

Thanks for the patch.

On Tue, Jun 4, 2019 at 8:49 AM Young Xiao <92siuyang@gmail.com> wrote:
>
> If vpif_probe() fails on v4l2_device_register() and vpif_probe_complete(),
> then memory allocated at initialize_vpif() for global vpif_obj.dev[i]
> become unreleased.
>
> The patch adds deallocation of vpif_obj.dev[i] on the error path.
>
> Signed-off-by: Young Xiao <92siuyang@gmail.com>
> ---
>  drivers/media/platform/davinci/vpif_capture.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
> index b5aacb0..277d500 100644
> --- a/drivers/media/platform/davinci/vpif_capture.c
> +++ b/drivers/media/platform/davinci/vpif_capture.c
> @@ -1385,6 +1385,14 @@ static int initialize_vpif(void)
>         return err;
>  }
>
> +static void free_vpif_objs(void)
> +{
function could be made inline.

> +       int i;
> +
> +       for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)

VPIF_DISPLAY_MAX_DEVICES ? this should be  VPIF_CAPTURE_MAX_DEVICES

> +               kfree(vpif_obj.dev[i]);
> +}
> +
>  static int vpif_async_bound(struct v4l2_async_notifier *notifier,
>                             struct v4l2_subdev *subdev,
>                             struct v4l2_async_subdev *asd)
> @@ -1654,7 +1662,7 @@ static __init int vpif_probe(struct platform_device *pdev)
>         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
>         if (err) {
>                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
> -               goto cleanup;
> +               goto vpif_free;
>         }
>
>         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
> @@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
>                                   "registered sub device %s\n",
>                                    subdevdata->name);
>                 }
> -               vpif_probe_complete();
> +               err = vpif_probe_complete();
> +               if (err) {
> +                       goto probe_subdev_out;
> +               }

No need for { and } as per kernel coding style.

>         } else {
>                 vpif_obj.notifier.ops = &vpif_async_ops;
>                 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
> @@ -1720,6 +1731,8 @@ static __init int vpif_probe(struct platform_device *pdev)
>         kfree(vpif_obj.sd);
>  vpif_unregister:
>         v4l2_device_unregister(&vpif_obj.v4l2_dev);
> +vpif_free:
> +       free_vpif_objs();
>  cleanup:
>         v4l2_async_notifier_cleanup(&vpif_obj.notifier);
>
> @@ -1748,8 +1761,8 @@ static int vpif_remove(struct platform_device *device)
>                 ch = vpif_obj.dev[i];
>                 /* Unregister video device */
>                 video_unregister_device(&ch->video_dev);
> -               kfree(vpif_obj.dev[i]);
>         }
> +       free_vpif_objs();

no need for this change, leave it as it is.

Cheers,
Prabhakar Lad

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

* [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()
@ 2019-06-04  7:50 Young Xiao
  2019-06-04  8:15 ` Lad, Prabhakar
  0 siblings, 1 reply; 9+ messages in thread
From: Young Xiao @ 2019-06-04  7:50 UTC (permalink / raw)
  To: prabhakar.csengg, mchehab, linux-media, linux-kernel, hverkuil; +Cc: Young Xiao

If vpif_probe() fails on v4l2_device_register() and vpif_probe_complete(),
then memory allocated at initialize_vpif() for global vpif_obj.dev[i]
become unreleased.

The patch adds deallocation of vpif_obj.dev[i] on the error path.

Signed-off-by: Young Xiao <92siuyang@gmail.com>
---
 drivers/media/platform/davinci/vpif_capture.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
index b5aacb0..277d500 100644
--- a/drivers/media/platform/davinci/vpif_capture.c
+++ b/drivers/media/platform/davinci/vpif_capture.c
@@ -1385,6 +1385,14 @@ static int initialize_vpif(void)
 	return err;
 }
 
+static void free_vpif_objs(void)
+{
+	int i;
+
+	for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
+		kfree(vpif_obj.dev[i]);
+}
+
 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
 			    struct v4l2_subdev *subdev,
 			    struct v4l2_async_subdev *asd)
@@ -1654,7 +1662,7 @@ static __init int vpif_probe(struct platform_device *pdev)
 	err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
 	if (err) {
 		v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
-		goto cleanup;
+		goto vpif_free;
 	}
 
 	while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
@@ -1701,7 +1709,10 @@ static __init int vpif_probe(struct platform_device *pdev)
 				  "registered sub device %s\n",
 				   subdevdata->name);
 		}
-		vpif_probe_complete();
+		err = vpif_probe_complete();
+		if (err) {
+			goto probe_subdev_out;
+		}
 	} else {
 		vpif_obj.notifier.ops = &vpif_async_ops;
 		err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
@@ -1720,6 +1731,8 @@ static __init int vpif_probe(struct platform_device *pdev)
 	kfree(vpif_obj.sd);
 vpif_unregister:
 	v4l2_device_unregister(&vpif_obj.v4l2_dev);
+vpif_free:
+	free_vpif_objs();
 cleanup:
 	v4l2_async_notifier_cleanup(&vpif_obj.notifier);
 
@@ -1748,8 +1761,8 @@ static int vpif_remove(struct platform_device *device)
 		ch = vpif_obj.dev[i];
 		/* Unregister video device */
 		video_unregister_device(&ch->video_dev);
-		kfree(vpif_obj.dev[i]);
 	}
+	free_vpif_objs();
 	return 0;
 }
 
-- 
2.7.4


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

end of thread, other threads:[~2019-06-05 20:21 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-29 13:09 [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe() Young Xiao
2019-06-03 11:55 ` Hans Verkuil
2019-06-04  7:15   ` Yang Xiao
2019-06-04  7:50 Young Xiao
2019-06-04  8:15 ` Lad, Prabhakar
2019-06-04  8:33   ` Yang Xiao
2019-06-04  8:47     ` Lad, Prabhakar
2019-06-04 12:26 Young Xiao
2019-06-05 20:20 ` Lad, Prabhakar

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).