mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Russ Weight <russell.h.weight@intel.com>
To: Jinjie Ruan <ruanjinjie@huawei.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: Thu, 28 Sep 2023 08:45:11 -0700	[thread overview]
Message-ID: <9842e1cc-3e61-3759-30f0-b4968622e0a7@intel.com> (raw)
In-Reply-To: <20230928091636.1209914-1-ruanjinjie@huawei.com>



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.

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

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:


  parent reply	other threads:[~2023-09-28 15:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-28  9:16 Jinjie Ruan
2023-09-28 14:02 ` Xu Yilun
2023-09-28 15:45 ` Russ Weight [this message]
2023-10-07  9:15   ` Ruan Jinjie

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=9842e1cc-3e61-3759-30f0-b4968622e0a7@intel.com \
    --to=russell.h.weight@intel.com \
    --cc=hao.wu@intel.com \
    --cc=linux-fpga@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mdf@kernel.org \
    --cc=ruanjinjie@huawei.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

all inboxes | Powered by JetHome®