mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RESEND] sparc: fix error handling in scan_one_device()
@ 2025-04-14 11:18 Ma Ke
  2025-04-14 18:07 ` Rob Gardner
  0 siblings, 1 reply; 3+ messages in thread
From: Ma Ke @ 2025-04-14 11:18 UTC (permalink / raw)
  To: davem, andreas, make24, sam, dawei.li
  Cc: sparclinux, linux-kernel, akpm, stable

Once of_device_register() failed, we should call put_device() to
decrement reference count for cleanup. Or it could cause memory leak.
So fix this by calling put_device(), then the name can be freed in
kobject_cleanup().

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: cf44bbc26cf1 ("[SPARC]: Beginnings of generic of_device framework.")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 arch/sparc/kernel/of_device_64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/sparc/kernel/of_device_64.c b/arch/sparc/kernel/of_device_64.c
index f98c2901f335..4272746d7166 100644
--- a/arch/sparc/kernel/of_device_64.c
+++ b/arch/sparc/kernel/of_device_64.c
@@ -677,7 +677,7 @@ static struct platform_device * __init scan_one_device(struct device_node *dp,
 
 	if (of_device_register(op)) {
 		printk("%pOF: Could not register of device.\n", dp);
-		kfree(op);
+		put_device(&op->dev);
 		op = NULL;
 	}
 
-- 
2.25.1


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

* Re: [PATCH RESEND] sparc: fix error handling in scan_one_device()
  2025-04-14 11:18 [PATCH RESEND] sparc: fix error handling in scan_one_device() Ma Ke
@ 2025-04-14 18:07 ` Rob Gardner
  0 siblings, 0 replies; 3+ messages in thread
From: Rob Gardner @ 2025-04-14 18:07 UTC (permalink / raw)
  To: Ma Ke, davem, andreas, sam, dawei.li
  Cc: sparclinux, linux-kernel, akpm, stable

On 4/14/25 05:18, Ma Ke wrote:
> Once of_device_register() failed, we should call put_device() to
> decrement reference count for cleanup. Or it could cause memory leak.
> So fix this by calling put_device(), then the name can be freed in
> kobject_cleanup().

This seems ok but it's not clear why you delete "kfree(op)" here.

scan_one_device() allocates the memory for op via kzalloc, and in the 
case where of_device_register() fails, it sets op = NULL. This 
superficially looks like you *create* a memory leak here, since I don't 
see any other obvious reference to that kzalloc'd memory after 
scan_one_device() returns. Is the "op" pointer saved somewhere quietly 
by build_device_resources()?

I think you should still free the memory (that you allocated) after the 
call to put_device(), or explain why this is not necessary.


>
> 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: cf44bbc26cf1 ("[SPARC]: Beginnings of generic of_device framework.")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
>   arch/sparc/kernel/of_device_64.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/sparc/kernel/of_device_64.c b/arch/sparc/kernel/of_device_64.c
> index f98c2901f335..4272746d7166 100644
> --- a/arch/sparc/kernel/of_device_64.c
> +++ b/arch/sparc/kernel/of_device_64.c
> @@ -677,7 +677,7 @@ static struct platform_device * __init scan_one_device(struct device_node *dp,
>   
>   	if (of_device_register(op)) {
>   		printk("%pOF: Could not register of device.\n", dp);
> -		kfree(op);
> +		put_device(&op->dev);
>   		op = NULL;
>   	}
>   


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

* [PATCH RESEND] sparc: fix error handling in scan_one_device()
@ 2025-04-07  7:41 Ma Ke
  0 siblings, 0 replies; 3+ messages in thread
From: Ma Ke @ 2025-04-07  7:41 UTC (permalink / raw)
  To: davem, andreas, make24, sam, dawei.li
  Cc: sparclinux, linux-kernel, akpm, stable

Once of_device_register() failed, we should call put_device() to
decrement reference count for cleanup. Or it could cause memory leak.
So fix this by calling put_device(), then the name can be freed in
kobject_cleanup().

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: cf44bbc26cf1 ("[SPARC]: Beginnings of generic of_device framework.")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 arch/sparc/kernel/of_device_64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/sparc/kernel/of_device_64.c b/arch/sparc/kernel/of_device_64.c
index f98c2901f335..4272746d7166 100644
--- a/arch/sparc/kernel/of_device_64.c
+++ b/arch/sparc/kernel/of_device_64.c
@@ -677,7 +677,7 @@ static struct platform_device * __init scan_one_device(struct device_node *dp,
 
 	if (of_device_register(op)) {
 		printk("%pOF: Could not register of device.\n", dp);
-		kfree(op);
+		put_device(&op->dev);
 		op = NULL;
 	}
 
-- 
2.25.1


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

end of thread, other threads:[~2025-04-14 18:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-14 11:18 [PATCH RESEND] sparc: fix error handling in scan_one_device() Ma Ke
2025-04-14 18:07 ` Rob Gardner
  -- strict thread matches above, loose matches on Subject: below --
2025-04-07  7:41 Ma Ke

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®