All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jon Hunter <jonathanh@nvidia.com>
To: Gen Zhang <blackgod016574@gmail.com>
Cc: lgirdwood@gmail.com, perex@perex.cz, 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 16:36:54 +0100	[thread overview]
Message-ID: <1740686f-a466-430c-9d01-ab83ea6998ac@nvidia.com> (raw)
In-Reply-To: <20190524150053.GA9235@zhanggen-UX430UQ>


On 24/05/2019 16:00, Gen Zhang wrote:
> On Fri, May 24, 2019 at 03:47:34PM +0100, Jon Hunter wrote:
>>
>> On 24/05/2019 15:33, Gen Zhang wrote:
>>> On Fri, May 24, 2019 at 09:33:13AM +0100, Jon Hunter wrote:
>>>>
>>>> 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.
>>> Thanks for your comments, Jon. But after I examined the code, I am still
>>> confused about the usage of devm_kmalloc(). You can kindly refer to 
>>> hisi_sas_debugfs_init() in drivers/scsi/hisi_sas/hisi_sas_main.c. And
>>> devm_kfree() is used to free a memory allocated by devm_kmalloc(). And
>>> I found other situations similar to this in other files.
>>>
>>> So, I hope you can give me some guidance on this. Thanks!
>>
>> Please refer to the devres documentation [0].
>>
>> Cheers,
>> Jon
>>
>> [0] https://www.kernel.org/doc/Documentation/driver-model/devres.txt
>>
>> -- 
>> nvpublic
> Thanks for your reply. I figured out that devm_kmalloc will free the 
> memory no matter fail or not. But I still want to ask why other codes
> as I above mentioned use devm_kfree() to free memory allocated by 
> devm_kmalloc(). If the memory is automatically freed, is this 
> devm_kfee() redundant codes that should be removed? Am I 
> misunderstanding this again or it is something else?

There could well be cases where you need to explicitly call
devm_kfree(), but having a quick glance at the example above, I don't
see why you would call devm_kfree() here and yes looks like that code
could be simplified significantly. Notice that hisi_sas_debugfs_exit()
does not free any memory as it is not necessary to explicitly do so.

Cheers
Jon

-- 
nvpublic

WARNING: multiple messages have this Message-ID (diff)
From: Jon Hunter <jonathanh@nvidia.com>
To: Gen Zhang <blackgod016574@gmail.com>
Cc: <lgirdwood@gmail.com>, <perex@perex.cz>,
	<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 16:36:54 +0100	[thread overview]
Message-ID: <1740686f-a466-430c-9d01-ab83ea6998ac@nvidia.com> (raw)
In-Reply-To: <20190524150053.GA9235@zhanggen-UX430UQ>


On 24/05/2019 16:00, Gen Zhang wrote:
> On Fri, May 24, 2019 at 03:47:34PM +0100, Jon Hunter wrote:
>>
>> On 24/05/2019 15:33, Gen Zhang wrote:
>>> On Fri, May 24, 2019 at 09:33:13AM +0100, Jon Hunter wrote:
>>>>
>>>> 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.
>>> Thanks for your comments, Jon. But after I examined the code, I am still
>>> confused about the usage of devm_kmalloc(). You can kindly refer to 
>>> hisi_sas_debugfs_init() in drivers/scsi/hisi_sas/hisi_sas_main.c. And
>>> devm_kfree() is used to free a memory allocated by devm_kmalloc(). And
>>> I found other situations similar to this in other files.
>>>
>>> So, I hope you can give me some guidance on this. Thanks!
>>
>> Please refer to the devres documentation [0].
>>
>> Cheers,
>> Jon
>>
>> [0] https://www.kernel.org/doc/Documentation/driver-model/devres.txt
>>
>> -- 
>> nvpublic
> Thanks for your reply. I figured out that devm_kmalloc will free the 
> memory no matter fail or not. But I still want to ask why other codes
> as I above mentioned use devm_kfree() to free memory allocated by 
> devm_kmalloc(). If the memory is automatically freed, is this 
> devm_kfee() redundant codes that should be removed? Am I 
> misunderstanding this again or it is something else?

There could well be cases where you need to explicitly call
devm_kfree(), but having a quick glance at the example above, I don't
see why you would call devm_kfree() here and yes looks like that code
could be simplified significantly. Notice that hisi_sas_debugfs_exit()
does not free any memory as it is not necessary to explicitly do so.

Cheers
Jon

-- 
nvpublic

  reply	other threads:[~2019-05-24 15:36 UTC|newest]

Thread overview: 10+ 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
2019-05-24  8:33   ` Jon Hunter
2019-05-24 14:33   ` Gen Zhang
2019-05-24 14:47     ` Jon Hunter
2019-05-24 14:47       ` Jon Hunter
2019-05-24 15:00       ` Gen Zhang
2019-05-24 15:36         ` Jon Hunter [this message]
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=1740686f-a466-430c-9d01-ab83ea6998ac@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.