2019-10-26 04:27:41

by Navid Emamdoost

[permalink] [raw]
Subject: [PATCH] media: aspeed-video: Fix memory leaks in aspeed_video_probe

In the implementation of aspeed_video_probe() the allocated memory for
video should be released in case of failure. Release video if either
devm_ioremap_resource() or aspeed_video_init() or
aspeed_video_setup_video() fails.

Fixes: d2b4387f3bdf ("media: platform: Add Aspeed Video Engine driver")
Signed-off-by: Navid Emamdoost <[email protected]>
---
drivers/media/platform/aspeed-video.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/media/platform/aspeed-video.c b/drivers/media/platform/aspeed-video.c
index eb12f3793062..8c473356132d 100644
--- a/drivers/media/platform/aspeed-video.c
+++ b/drivers/media/platform/aspeed-video.c
@@ -1663,18 +1663,24 @@ static int aspeed_video_probe(struct platform_device *pdev)

video->base = devm_ioremap_resource(video->dev, res);

- if (IS_ERR(video->base))
- return PTR_ERR(video->base);
+ if (IS_ERR(video->base)) {
+ rc = PTR_ERR(video->base);
+ goto free_video;
+ }

rc = aspeed_video_init(video);
if (rc)
- return rc;
+ goto free_video;

rc = aspeed_video_setup_video(video);
if (rc)
- return rc;
+ goto free_video;

return 0;
+
+free_video:
+ kfree(video);
+ return rc;
}

static int aspeed_video_remove(struct platform_device *pdev)
--
2.17.1


2019-10-27 06:56:51

by Markus Elfring

[permalink] [raw]
Subject: Re: [PATCH] media: aspeed-video: Fix memory leaks in aspeed_video_probe


> +++ b/drivers/media/platform/aspeed-video.c
> @@ -1663,18 +1663,24 @@ static int aspeed_video_probe(struct platform_device *pdev)

> +free_video:
> + kfree(video);


I am curious if there is a need for such a memory release at another place.
How do you think about to add this function call also to the implementation
of the function “aspeed_video_remove”?

Regards,
Markus

2019-10-28 21:16:06

by Jae Hyun Yoo

[permalink] [raw]
Subject: Re: [PATCH] media: aspeed-video: Fix memory leaks in aspeed_video_probe

Hi Navid,

On 10/25/2019 9:25 PM, Navid Emamdoost wrote:
> In the implementation of aspeed_video_probe() the allocated memory for
> video should be released in case of failure. Release video if either
> devm_ioremap_resource() or aspeed_video_init() or
> aspeed_video_setup_video() fails.
>
> Fixes: d2b4387f3bdf ("media: platform: Add Aspeed Video Engine driver")
> Signed-off-by: Navid Emamdoost <[email protected]>
> ---
> drivers/media/platform/aspeed-video.c | 14 ++++++++++----
> 1 file changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/media/platform/aspeed-video.c b/drivers/media/platform/aspeed-video.c
> index eb12f3793062..8c473356132d 100644
> --- a/drivers/media/platform/aspeed-video.c
> +++ b/drivers/media/platform/aspeed-video.c
> @@ -1663,18 +1663,24 @@ static int aspeed_video_probe(struct platform_device *pdev)
>
> video->base = devm_ioremap_resource(video->dev, res);
>
> - if (IS_ERR(video->base))
> - return PTR_ERR(video->base);
> + if (IS_ERR(video->base)) {
> + rc = PTR_ERR(video->base);
> + goto free_video;
> + }
>
> rc = aspeed_video_init(video);
> if (rc)
> - return rc;
> + goto free_video;
>
> rc = aspeed_video_setup_video(video);
> if (rc)
> - return rc;
> + goto free_video;
>
> return 0;
> +
> +free_video:
> + kfree(video);
> + return rc;
> }
>
> static int aspeed_video_remove(struct platform_device *pdev)
>

Can we simply change kzalloc in aspeed_video_probe with devm_kzalloc
so that we don't need to take care of freeing of video? I think it would
be more simpler.

Cheers,

Jae

2019-10-28 21:17:31

by Navid Emamdoost

[permalink] [raw]
Subject: Re: [PATCH] media: aspeed-video: Fix memory leaks in aspeed_video_probe

On Mon, Oct 28, 2019 at 11:27 AM Jae Hyun Yoo
<[email protected]> wrote:
>
> Hi Navid,
>
> On 10/25/2019 9:25 PM, Navid Emamdoost wrote:
> > In the implementation of aspeed_video_probe() the allocated memory for
> > video should be released in case of failure. Release video if either
> > devm_ioremap_resource() or aspeed_video_init() or
> > aspeed_video_setup_video() fails.
> >
> > Fixes: d2b4387f3bdf ("media: platform: Add Aspeed Video Engine driver")
> > Signed-off-by: Navid Emamdoost <[email protected]>
> > ---
> > drivers/media/platform/aspeed-video.c | 14 ++++++++++----
> > 1 file changed, 10 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/media/platform/aspeed-video.c b/drivers/media/platform/aspeed-video.c
> > index eb12f3793062..8c473356132d 100644
> > --- a/drivers/media/platform/aspeed-video.c
> > +++ b/drivers/media/platform/aspeed-video.c
> > @@ -1663,18 +1663,24 @@ static int aspeed_video_probe(struct platform_device *pdev)
> >
> > video->base = devm_ioremap_resource(video->dev, res);
> >
> > - if (IS_ERR(video->base))
> > - return PTR_ERR(video->base);
> > + if (IS_ERR(video->base)) {
> > + rc = PTR_ERR(video->base);
> > + goto free_video;
> > + }
> >
> > rc = aspeed_video_init(video);
> > if (rc)
> > - return rc;
> > + goto free_video;
> >
> > rc = aspeed_video_setup_video(video);
> > if (rc)
> > - return rc;
> > + goto free_video;
> >
> > return 0;
> > +
> > +free_video:
> > + kfree(video);
> > + return rc;
> > }
> >
> > static int aspeed_video_remove(struct platform_device *pdev)
> >
>
> Can we simply change kzalloc in aspeed_video_probe with devm_kzalloc
> so that we don't need to take care of freeing of video? I think it would
> be more simpler.

Sounds a neat plan, will send v2.

>
> Cheers,
>
> Jae



--
Navid.

2019-10-29 06:21:56

by Navid Emamdoost

[permalink] [raw]
Subject: [PATCH v2] media: aspeed-video: Fix memory leaks in aspeed_video_probe

In the implementation of aspeed_video_probe() the allocated memory for
video should be released if either devm_ioremap_resource()
or aspeed_video_init() or aspeed_video_setup_video() fails. Replace
kzalloc() with devm_kzalloc to avoid explicit release for video.

Fixes: d2b4387f3bdf ("media: platform: Add Aspeed Video Engine driver")
Signed-off-by: Navid Emamdoost <[email protected]>
---
Changes in v2:
-- replace kzalloc with devm_kzalloc based on Jae Hyun Yoo
suggestion

drivers/media/platform/aspeed-video.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/platform/aspeed-video.c b/drivers/media/platform/aspeed-video.c
index eb12f3793062..2aa8ea2f9824 100644
--- a/drivers/media/platform/aspeed-video.c
+++ b/drivers/media/platform/aspeed-video.c
@@ -1646,7 +1646,7 @@ static int aspeed_video_probe(struct platform_device *pdev)
{
int rc;
struct resource *res;
- struct aspeed_video *video = kzalloc(sizeof(*video), GFP_KERNEL);
+ struct aspeed_video *video = devm_kzalloc(sizeof(*video), GFP_KERNEL);

if (!video)
return -ENOMEM;
--
2.17.1