* [PATCH v3] [ARM] fix reference leak in locomo_init_one_child()
@ 2025-01-07 2:07 Ma Ke
2025-01-07 17:29 ` Russell King (Oracle)
0 siblings, 1 reply; 2+ messages in thread
From: Ma Ke @ 2025-01-07 2:07 UTC (permalink / raw)
To: linux, elder, sumit.garg, gregkh, make24
Cc: linux-arm-kernel, linux-kernel, stable
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'.
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>
---
Changes in v3:
- modified the patch as suggestions;
Changes in v2:
- modified the patch as suggestions.
---
arch/arm/common/locomo.c | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)
diff --git a/arch/arm/common/locomo.c b/arch/arm/common/locomo.c
index cb6ef449b987..9e275b2105c2 100644
--- a/arch/arm/common/locomo.c
+++ b/arch/arm/common/locomo.c
@@ -220,13 +220,11 @@ static int
locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
{
struct locomo_dev *dev;
- int ret;
+ int ret = 0;
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;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v3] [ARM] fix reference leak in locomo_init_one_child()
2025-01-07 2:07 [PATCH v3] [ARM] fix reference leak in locomo_init_one_child() Ma Ke
@ 2025-01-07 17:29 ` Russell King (Oracle)
0 siblings, 0 replies; 2+ messages in thread
From: Russell King (Oracle) @ 2025-01-07 17:29 UTC (permalink / raw)
To: Ma Ke; +Cc: elder, sumit.garg, gregkh, linux-arm-kernel, linux-kernel, stable
On Tue, Jan 07, 2025 at 10:07:14AM +0800, 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'.
>
> 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>
> ---
> Changes in v3:
> - modified the patch as suggestions;
> Changes in v2:
> - modified the patch as suggestions.
> ---
> arch/arm/common/locomo.c | 15 ++++++---------
> 1 file changed, 6 insertions(+), 9 deletions(-)
>
> diff --git a/arch/arm/common/locomo.c b/arch/arm/common/locomo.c
> index cb6ef449b987..9e275b2105c2 100644
> --- a/arch/arm/common/locomo.c
> +++ b/arch/arm/common/locomo.c
> @@ -220,13 +220,11 @@ static int
> locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
> {
> struct locomo_dev *dev;
> - int ret;
> + int ret = 0;
The code around "ret" becomes:
int ret = 0;
...
ret = device_register(&dev->dev);
Nothing between these two statements references "ret", and the present
goto is eliminated in your patch.
So, why do we need to initialise ret to zero where it is declared?
--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-01-07 17:46 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-07 2:07 [PATCH v3] [ARM] fix reference leak in locomo_init_one_child() Ma Ke
2025-01-07 17:29 ` Russell King (Oracle)
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).