From: Alex Elder <elder@ieee.org>
To: Ma Ke <make24@iscas.ac.cn>,
linux@armlinux.org.uk, sumit.garg@linaro.org,
gregkh@linuxfoundation.org, elder@kernel.org
Cc: linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v4] [ARM] fix reference leak in locomo_init_one_child()
Date: Wed, 8 Jan 2025 21:06:16 -0600 [thread overview]
Message-ID: <229506bf-e675-4f17-b1fa-f9f7d62fcd45@ieee.org> (raw)
In-Reply-To: <20250108033049.1206055-1-make24@iscas.ac.cn>
On 1/7/25 9:30 PM, Ma Ke wrote:
> Once device_register() failed, we should call put_device() to
> decrement reference count for cleanup. Or it could cause memory leak.
>
> device_register() includes device_add(). As comment of device_add()
> says, 'if device_add() succeeds, you should call device_del() when you
> want to get rid of it. If device_add() has not succeeded, use only
> put_device() to drop the reference count'.
And above device_register() it says:
* 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.
> Found by code review.
>
> Cc: stable@vger.kernel.org
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
Looks good to me. Much cleaner result.
Reviewed-by: Alex Elder <elder@kernel.org>
> ---
> Changes in v4:
> - deleted the redundant initialization;
> Changes in v3:
> - modified the patch as suggestions;
> Changes in v2:
> - modified the patch as suggestions.
> ---
> arch/arm/common/locomo.c | 13 +++++--------
> 1 file changed, 5 insertions(+), 8 deletions(-)
>
> diff --git a/arch/arm/common/locomo.c b/arch/arm/common/locomo.c
> index cb6ef449b987..45106066a17f 100644
> --- a/arch/arm/common/locomo.c
> +++ b/arch/arm/common/locomo.c
> @@ -223,10 +223,8 @@ locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
> int ret;
>
> dev = kzalloc(sizeof(struct locomo_dev), GFP_KERNEL);
> - if (!dev) {
> - ret = -ENOMEM;
> - goto out;
> - }
> + if (!dev)
> + return -ENOMEM;
>
> /*
> * If the parent device has a DMA mask associated with it,
> @@ -254,10 +252,9 @@ locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
> NO_IRQ : lchip->irq_base + info->irq[0];
>
> ret = device_register(&dev->dev);
> - if (ret) {
> - out:
> - kfree(dev);
> - }
> + if (ret)
> + put_device(&dev->dev);
> +
> return ret;
> }
>
next prev parent reply other threads:[~2025-01-09 3:06 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-08 3:30 Ma Ke
2025-01-09 3:06 ` Alex Elder [this message]
2025-01-13 12:45 ` Russell King (Oracle)
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=229506bf-e675-4f17-b1fa-f9f7d62fcd45@ieee.org \
--to=elder@ieee.org \
--cc=elder@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=make24@iscas.ac.cn \
--cc=stable@vger.kernel.org \
--cc=sumit.garg@linaro.org \
/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®