Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/huge_memory: fix memory leak when kobject_init_and_add() fails
@ 2026-06-09 13:12 ranxiaokai627
  2026-06-09 13:19 ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 5+ messages in thread
From: ranxiaokai627 @ 2026-06-09 13:12 UTC (permalink / raw)
  To: david, akpm, ljs, ziy, baolin.wang, liam, npache, ryan.roberts,
	dev.jain, baohua, lance.yang
  Cc: linux-mm, linux-kernel, Ran Xiaokai

From: Ran Xiaokai <ran.xiaokai@zte.com.cn>

As documented in the comments for kobject_init_and_add():

"If this function returns an error, kobject_put() must be called to
properly clean up the memory associated with the object.  This is the
same type of error handling after a call to kobject_add() and kobject
lifetime rules are the same here."

This is because kobject_init_and_add() may have already allocated memory
internally for the kobject name (kobj->name), and leaving the refcount
at 1 prevents its release callback from being triggered.

Fixes: 3485b88390b0a ("mm: thp: introduce multi-size THP sysfs interface")
Signed-off-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
---
 mm/huge_memory.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 653f2dc03403..601750dbe79f 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -790,11 +790,8 @@ static struct thpsize *thpsize_create(int order, struct kobject *parent)
 
 	ret = kobject_init_and_add(&thpsize->kobj, &thpsize_ktype, parent,
 				   "hugepages-%lukB", size);
-	if (ret) {
-		kfree(thpsize);
-		goto err;
-	}
-
+	if (ret)
+		goto err_put;
 
 	ret = sysfs_add_group(&thpsize->kobj, &any_ctrl_attr_grp);
 	if (ret)
-- 
2.25.1




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

end of thread, other threads:[~2026-06-10  0:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-09 13:12 [PATCH] mm/huge_memory: fix memory leak when kobject_init_and_add() fails ranxiaokai627
2026-06-09 13:19 ` David Hildenbrand (Arm)
2026-06-09 14:04   ` Lance Yang
2026-06-09 14:20     ` Lorenzo Stoakes
2026-06-10  0:28     ` SeongJae Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox