* [PATCH v3] sparc: fix error handling in scan_one_device()
@ 2025-09-20 12:53 Ma Ke
2025-09-22 5:48 ` Andreas Larsson
0 siblings, 1 reply; 2+ messages in thread
From: Ma Ke @ 2025-09-20 12:53 UTC (permalink / raw)
To: davem, andreas, make24; +Cc: sparclinux, linux-kernel, 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().
Calling path: of_device_register() -> of_device_add() -> 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: cf44bbc26cf1 ("[SPARC]: Beginnings of generic of_device framework.")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
Changes in v3:
- also fixed the same problem in arch/sparc/kernel/of_device_32.c as suggestions.
Changes in v2:
- retained kfree() manually due to the lack of a release callback function.
---
arch/sparc/kernel/of_device_32.c | 1 +
arch/sparc/kernel/of_device_64.c | 1 +
2 files changed, 2 insertions(+)
diff --git a/arch/sparc/kernel/of_device_32.c b/arch/sparc/kernel/of_device_32.c
index 06012e68bdca..284a4cafa432 100644
--- a/arch/sparc/kernel/of_device_32.c
+++ b/arch/sparc/kernel/of_device_32.c
@@ -387,6 +387,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);
+ put_device(&op->dev);
kfree(op);
op = NULL;
}
diff --git a/arch/sparc/kernel/of_device_64.c b/arch/sparc/kernel/of_device_64.c
index f98c2901f335..f53092b07b9e 100644
--- a/arch/sparc/kernel/of_device_64.c
+++ b/arch/sparc/kernel/of_device_64.c
@@ -677,6 +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);
+ put_device(&op->dev);
kfree(op);
op = NULL;
}
--
2.17.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v3] sparc: fix error handling in scan_one_device()
2025-09-20 12:53 [PATCH v3] sparc: fix error handling in scan_one_device() Ma Ke
@ 2025-09-22 5:48 ` Andreas Larsson
0 siblings, 0 replies; 2+ messages in thread
From: Andreas Larsson @ 2025-09-22 5:48 UTC (permalink / raw)
To: Ma Ke, davem; +Cc: sparclinux, linux-kernel, stable
On 2025-09-20 14:53, 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().
>
> Calling path: of_device_register() -> of_device_add() -> 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: cf44bbc26cf1 ("[SPARC]: Beginnings of generic of_device framework.")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
> Changes in v3:
> - also fixed the same problem in arch/sparc/kernel/of_device_32.c as suggestions.
> Changes in v2:
> - retained kfree() manually due to the lack of a release callback function.
> ---
> arch/sparc/kernel/of_device_32.c | 1 +
> arch/sparc/kernel/of_device_64.c | 1 +
> 2 files changed, 2 insertions(+)
>
> diff --git a/arch/sparc/kernel/of_device_32.c b/arch/sparc/kernel/of_device_32.c
> index 06012e68bdca..284a4cafa432 100644
> --- a/arch/sparc/kernel/of_device_32.c
> +++ b/arch/sparc/kernel/of_device_32.c
> @@ -387,6 +387,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);
> + put_device(&op->dev);
> kfree(op);
> op = NULL;
> }
> diff --git a/arch/sparc/kernel/of_device_64.c b/arch/sparc/kernel/of_device_64.c
> index f98c2901f335..f53092b07b9e 100644
> --- a/arch/sparc/kernel/of_device_64.c
> +++ b/arch/sparc/kernel/of_device_64.c
> @@ -677,6 +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);
> + put_device(&op->dev);
> kfree(op);
> op = NULL;
> }
Reviewed-by: Andreas Larsson <andreas@gaisler.com>
Picking this up to my for-next.
Thanks,
Andreas
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-09-22 5:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-20 12:53 [PATCH v3] sparc: fix error handling in scan_one_device() Ma Ke
2025-09-22 5:48 ` Andreas Larsson
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®