Linux-FPGA Archive mirror
 help / color / mirror / Atom feed
From: Ruan Jinjie <ruanjinjie@huawei.com>
To: Russ Weight <russell.h.weight@intel.com>, <mdf@kernel.org>,
	<hao.wu@intel.com>, <yilun.xu@intel.com>, <trix@redhat.com>,
	<linux-fpga@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH RESEND] fpga: region: Fix possible memory leak in fpga_region_register_full()
Date: Sat, 7 Oct 2023 17:15:06 +0800	[thread overview]
Message-ID: <78adf4cf-e43d-fa1b-1d65-43c25e0cba6b@huawei.com> (raw)
In-Reply-To: <9842e1cc-3e61-3759-30f0-b4968622e0a7@intel.com>



On 2023/9/28 23:45, Russ Weight wrote:
> 
> 
> On 9/28/23 02:16, Jinjie Ruan wrote:
>> If device_register() fails in fpga_region_register_full(), the region
>> allocated by kzalloc() and the id allocated by ida_alloc() also need be
>> freed otherwise will cause memory leak.
>>
>> Fixes: 8886a579744f ("fpga: region: Use standard dev_release for class driver")
>> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
>> ---
>>  drivers/fpga/fpga-region.c | 5 +++--
>>  1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/fpga/fpga-region.c b/drivers/fpga/fpga-region.c
>> index b364a929425c..9dc6314976ef 100644
>> --- a/drivers/fpga/fpga-region.c
>> +++ b/drivers/fpga/fpga-region.c
>> @@ -228,12 +228,13 @@ fpga_region_register_full(struct device *parent, const struct fpga_region_info *
>>  
>>  	ret = device_register(&region->dev);
> 
> The comments for device_register() say:
> 
>  * NOTE: _Never_ directly free @dev after calling this function, even
>  * if it returned an error! Always use put_device() to give up the
>  * reference initialized in this function instead.
> 
> Note that dev is embedded in the region structure, so freeing region
> means freeing dev.
> 
> The expectation is that after device_register() has been called, even
> if it returns an error, put_device() is used to lower the reference
> count. When the reference count is zero, the fpga_region_dev_release()
> function is called. fpga_region_dev_release() frees calls ida_free()
> and frees the region.

Right!

> 
> Have you observed different behavior? Do you have evidence of a memory
> leak?

I have noticed a memory leak in using fpga_region_register_full() in
fpga-region-test.c. I'll send the patch sooner.

> 
> Thanks,
> - Russ
> 
>>  	if (ret) {
>> -		put_device(&region->dev);
>> -		return ERR_PTR(ret);
>> +		goto err_put_device;
>>  	}
>>  
>>  	return region;
>>  
>> +err_put_device:
>> +	put_device(&region->dev);
>>  err_remove:
>>  	ida_free(&fpga_region_ida, id);
>>  err_free:
> 

      reply	other threads:[~2023-10-07  9:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-28  9:16 [PATCH RESEND] fpga: region: Fix possible memory leak in fpga_region_register_full() Jinjie Ruan
2023-09-28 14:02 ` Xu Yilun
2023-09-28 15:45 ` Russ Weight
2023-10-07  9:15   ` Ruan Jinjie [this message]

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=78adf4cf-e43d-fa1b-1d65-43c25e0cba6b@huawei.com \
    --to=ruanjinjie@huawei.com \
    --cc=hao.wu@intel.com \
    --cc=linux-fpga@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mdf@kernel.org \
    --cc=russell.h.weight@intel.com \
    --cc=trix@redhat.com \
    --cc=yilun.xu@intel.com \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).