2019-06-04 12:27:31

by Young Xiao

[permalink] [raw]
Subject: [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()

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 <[email protected]>
---
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


2019-06-05 20:22:54

by Lad, Prabhakar

[permalink] [raw]
Subject: Re: [PATCH] media: davinci: vpif_capture: fix memory leak in vpif_probe()

On Tue, Jun 4, 2019 at 1:25 PM Young Xiao <[email protected]> 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 <[email protected]>
> ---
> drivers/media/platform/davinci/vpif_capture.c | 16 ++++++++++++++--
> 1 file changed, 14 insertions(+), 2 deletions(-)
>
Acked-by: Lad, Prabhakar <[email protected]>

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
>