linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jon Hunter <jonathanh@nvidia.com>
To: Gen Zhang <blackgod016574@gmail.com>, <lgirdwood@gmail.com>,
	<perex@perex.cz>
Cc: <alsa-devel@alsa-project.org>, <linux-tegra@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] tegra_wm9712: Fix a memory leaking bug in tegra_wm9712_driver_probe()
Date: Fri, 24 May 2019 09:33:13 +0100	[thread overview]
Message-ID: <b2d43dfe-17e5-a975-435b-49f2aa2ad550@nvidia.com> (raw)
In-Reply-To: <20190524005014.GA2289@zhanggen-UX430UQ>


On 24/05/2019 01:50, Gen Zhang wrote:
> In tegra_wm9712_driver_probe(), 'machine->codec' is allocated by
> platform_device_alloc(). When it is NULL, function returns ENOMEM.
> However, 'machine' is allocated by devm_kzalloc() before this site.
> Thus we should free 'machine' before function ends to prevent memory
> leaking.

Memory allocated by devm_xxx() is automatically freed on failure so this
is not correct.

> Further, we should free 'machine->util_data', 'machine->codec' and
> 'machine' before this function normally ends to prevent memory leaking.

This is also incorrect. Why would we free all resources after
successfully initialising the driver?

> Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
> ---
> diff --git a/sound/soc/tegra/tegra_wm9712.c b/sound/soc/tegra/tegra_wm9712.c
> index 864a334..295c41d 100644
> --- a/sound/soc/tegra/tegra_wm9712.c
> +++ b/sound/soc/tegra/tegra_wm9712.c
> @@ -86,7 +86,8 @@ static int tegra_wm9712_driver_probe(struct platform_device *pdev)
>  	machine->codec = platform_device_alloc("wm9712-codec", -1);
>  	if (!machine->codec) {
>  		dev_err(&pdev->dev, "Can't allocate wm9712 platform device\n");
> -		return -ENOMEM;
> +		ret = -ENOMEM;
> +		goto codec_free;
>  	}
>  
>  	ret = platform_device_add(machine->codec);
> @@ -127,6 +128,10 @@ static int tegra_wm9712_driver_probe(struct platform_device *pdev)
>  		goto asoc_utils_fini;
>  	}
>  
> +	tegra_asoc_utils_fini(&machine->util_data);
> +	platform_device_del(machine->codec);
> +	platform_device_put(machine->codec);
> +	devm_kfree(&pdev->dev, machine);
>  	return 0;

As stated above, this is incorrect.

Did you actually test this? I think you would find this would break the
driver.

Jon

-- 
nvpublic

  reply	other threads:[~2019-05-24  8:33 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-24  0:50 [PATCH] tegra_wm9712: Fix a memory leaking bug in tegra_wm9712_driver_probe() Gen Zhang
2019-05-24  8:33 ` Jon Hunter [this message]
2019-05-24 14:33   ` Gen Zhang
2019-05-24 14:47     ` Jon Hunter
2019-05-24 15:00       ` Gen Zhang
2019-05-24 15:36         ` Jon Hunter
2019-05-24 15:42           ` Gen Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b2d43dfe-17e5-a975-435b-49f2aa2ad550@nvidia.com \
    --to=jonathanh@nvidia.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=blackgod016574@gmail.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=perex@perex.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).