* [PATCH v2] of: unittest: Fix memory leak in unittest_data_add()
@ 2025-12-31 11:49 Zilin Guan
2026-01-02 21:36 ` Rob Herring (Arm)
0 siblings, 1 reply; 2+ messages in thread
From: Zilin Guan @ 2025-12-31 11:49 UTC (permalink / raw)
To: robh; +Cc: saravanak, devicetree, linux-kernel, Zilin Guan, Jianhao Xu
In unittest_data_add(), if of_resolve_phandles() fails, the allocated
unittest_data is not freed, leading to a memory leak.
Fix this by using scope-based cleanup helper __free(kfree) for automatic
resource cleanup. This ensures unittest_data is automatically freed when
it goes out of scope in error paths.
For the success path, use retain_and_null_ptr() to transfer ownership
of the memory to the device tree and prevent double freeing.
Fixes: 2eb46da2a760 ("of/selftest: Use the resolver to fixup phandles")
Suggested-by: Rob Herring <robh@kernel.org>
Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
Changes in v2:
- using scope-based cleanup helper __free(kfree) for automatic resource cleanup.
drivers/of/unittest.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index 388e9ec2cccf..3b773aaf9d05 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -1985,7 +1985,6 @@ static void attach_node_and_children(struct device_node *np)
*/
static int __init unittest_data_add(void)
{
- void *unittest_data;
void *unittest_data_align;
struct device_node *unittest_data_node = NULL, *np;
/*
@@ -2004,7 +2003,7 @@ static int __init unittest_data_add(void)
}
/* creating copy */
- unittest_data = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
+ void *unittest_data __free(kfree) = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
if (!unittest_data)
return -ENOMEM;
@@ -2014,12 +2013,10 @@ static int __init unittest_data_add(void)
ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
if (!ret) {
pr_warn("%s: unflatten testcases tree failed\n", __func__);
- kfree(unittest_data);
return -ENODATA;
}
if (!unittest_data_node) {
pr_warn("%s: testcases tree is empty\n", __func__);
- kfree(unittest_data);
return -ENODATA;
}
@@ -2038,7 +2035,6 @@ static int __init unittest_data_add(void)
/* attach the sub-tree to live tree */
if (!of_root) {
pr_warn("%s: no live tree to attach sub-tree\n", __func__);
- kfree(unittest_data);
rc = -ENODEV;
goto unlock;
}
@@ -2059,6 +2055,8 @@ static int __init unittest_data_add(void)
EXPECT_END(KERN_INFO,
"Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
+ retain_and_null_ptr(unittest_data);
+
unlock:
of_overlay_mutex_unlock();
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH v2] of: unittest: Fix memory leak in unittest_data_add()
2025-12-31 11:49 [PATCH v2] of: unittest: Fix memory leak in unittest_data_add() Zilin Guan
@ 2026-01-02 21:36 ` Rob Herring (Arm)
0 siblings, 0 replies; 2+ messages in thread
From: Rob Herring (Arm) @ 2026-01-02 21:36 UTC (permalink / raw)
To: Zilin Guan; +Cc: Jianhao Xu, saravanak, devicetree, linux-kernel
On Wed, 31 Dec 2025 11:49:15 +0000, Zilin Guan wrote:
> In unittest_data_add(), if of_resolve_phandles() fails, the allocated
> unittest_data is not freed, leading to a memory leak.
>
> Fix this by using scope-based cleanup helper __free(kfree) for automatic
> resource cleanup. This ensures unittest_data is automatically freed when
> it goes out of scope in error paths.
>
> For the success path, use retain_and_null_ptr() to transfer ownership
> of the memory to the device tree and prevent double freeing.
>
> Fixes: 2eb46da2a760 ("of/selftest: Use the resolver to fixup phandles")
> Suggested-by: Rob Herring <robh@kernel.org>
> Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
> Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
> Changes in v2:
> - using scope-based cleanup helper __free(kfree) for automatic resource cleanup.
>
> drivers/of/unittest.c | 8 +++-----
> 1 file changed, 3 insertions(+), 5 deletions(-)
>
Applied, thanks!
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-01-02 21:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-31 11:49 [PATCH v2] of: unittest: Fix memory leak in unittest_data_add() Zilin Guan
2026-01-02 21:36 ` Rob Herring (Arm)
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®