linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] [ARM] fix reference leak in locomo_init_one_child()
@ 2025-01-05 11:11 Ma Ke
  2025-01-05 12:28 ` Russell King (Oracle)
  0 siblings, 1 reply; 2+ messages in thread
From: Ma Ke @ 2025-01-05 11:11 UTC (permalink / raw)
  To: linux, sumit.garg, make24, gregkh, elder
  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>
---
 arch/arm/common/locomo.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/arm/common/locomo.c b/arch/arm/common/locomo.c
index cb6ef449b987..7274010218ec 100644
--- a/arch/arm/common/locomo.c
+++ b/arch/arm/common/locomo.c
@@ -255,6 +255,7 @@ locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
 
 	ret = device_register(&dev->dev);
 	if (ret) {
+		put_device(&dev->dev);
  out:
 		kfree(dev);
 	}
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] [ARM] fix reference leak in locomo_init_one_child()
  2025-01-05 11:11 [PATCH] [ARM] fix reference leak in locomo_init_one_child() Ma Ke
@ 2025-01-05 12:28 ` Russell King (Oracle)
  0 siblings, 0 replies; 2+ messages in thread
From: Russell King (Oracle) @ 2025-01-05 12:28 UTC (permalink / raw)
  To: Ma Ke; +Cc: sumit.garg, gregkh, elder, linux-arm-kernel, linux-kernel, stable

On Sun, Jan 05, 2025 at 07:11:56PM +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'.

The commit message is not quite correct:

"After calling device_register(), the correct way to dispose of the
device is to call put_device() as per the device_register()
documentation rather than kfree()."

This reveals that your patch is not completely correct.

> diff --git a/arch/arm/common/locomo.c b/arch/arm/common/locomo.c
> index cb6ef449b987..7274010218ec 100644
> --- a/arch/arm/common/locomo.c
> +++ b/arch/arm/common/locomo.c
> @@ -255,6 +255,7 @@ locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
>  
>  	ret = device_register(&dev->dev);
>  	if (ret) {
> +		put_device(&dev->dev);
>   out:
>  		kfree(dev);

... and that leads to the second problem here - this kfree() will lead
to a double-free of the device. Once by the reference count dropping to
zero, resulting in locomo_dev_release() being called, and then this
kfree().

Thanks.

-- 
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-05 12:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-05 11:11 [PATCH] [ARM] fix reference leak in locomo_init_one_child() Ma Ke
2025-01-05 12:28 ` 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).