]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
sparc: fix error handling in scan_one_device()
authorMa Ke <make24@iscas.ac.cn>
Sat, 20 Sep 2025 12:53:12 +0000 (20:53 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 19 Oct 2025 14:21:47 +0000 (16:21 +0200)
commit 302c04110f0ce70d25add2496b521132548cd408 upstream.

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>
Reviewed-by: Andreas Larsson <andreas@gaisler.com>
Signed-off-by: Andreas Larsson <andreas@gaisler.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/sparc/kernel/of_device_32.c
arch/sparc/kernel/of_device_64.c

index 4ebf51e6e78ec1a8d12f4cabbd3106b7cace359c..b06b92c9a375101ad0617be4852b48c1243b78e7 100644 (file)
@@ -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;
        }
index 5a9f86b1d4e7e33cb7b60cca06b0c99e1f696dbf..6e616bd5cc46ab01d0aad97120e0dc070af5168f 100644 (file)
@@ -680,6 +680,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;
        }