* [PATCH v2] mm: huge_memory: Fix kobject cleanup in thpsize_create error
@ 2026-07-13 5:41 Hongling Zeng
2026-07-14 10:54 ` Lorenzo Stoakes (ARM)
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Hongling Zeng @ 2026-07-13 5:41 UTC (permalink / raw)
To: akpm, david, ljs, ziy, baolin.wang, liam, npache, ryan.roberts,
dev.jain, baohua, lance.yang
Cc: linux-mm, linux-kernel, zhongling0719, Hongling Zeng
When kobject_init_and_add() fails, the kobject API requires calling
kobject_put() to properly clean up the memory, not direct kfree().
According to the kobject API documentation, kobject_init_and_add()
calls kobject_init() internally. If the subsequent kobject_add()
fails, the kobject has still been initialized and must be cleaned up
via the reference count mechanism (kobject_put), not direct kfree().
Direct kfree() leaves the kobject's internal state (including the
reference count and kset membership) uncleaned, which can cause:
- Memory leaks of kobject internal structures
- Potential use-after-free if there are pending references
- Inconsistent state with the rest of the error handling code
This fix matches the pattern used elsewhere in the kernel and in the
same function (err_put label) which correctly uses kobject_put().
Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
Suggested-by: Baolin Wang <baolin.wang@linux.alibaba.com>
---
Change in v2:
- Use goto err_put instead of kobject_put() + goto err for consistency
- Add suggested
---
mm/huge_memory.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 2bccb0a53a0a..589f7bf1f19d 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -818,10 +818,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);
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v2] mm: huge_memory: Fix kobject cleanup in thpsize_create error
2026-07-13 5:41 [PATCH v2] mm: huge_memory: Fix kobject cleanup in thpsize_create error Hongling Zeng
@ 2026-07-14 10:54 ` Lorenzo Stoakes (ARM)
2026-07-15 1:03 ` Zi Yan
2026-07-15 6:40 ` Baolin Wang
2 siblings, 0 replies; 4+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-07-14 10:54 UTC (permalink / raw)
To: Hongling Zeng
Cc: akpm, david, ziy, baolin.wang, liam, npache, ryan.roberts,
dev.jain, baohua, lance.yang, linux-mm, linux-kernel,
zhongling0719
On Mon, Jul 13, 2026 at 01:41:54PM +0800, Hongling Zeng wrote:
> When kobject_init_and_add() fails, the kobject API requires calling
> kobject_put() to properly clean up the memory, not direct kfree().
>
> According to the kobject API documentation, kobject_init_and_add()
> calls kobject_init() internally. If the subsequent kobject_add()
> fails, the kobject has still been initialized and must be cleaned up
> via the reference count mechanism (kobject_put), not direct kfree().
>
> Direct kfree() leaves the kobject's internal state (including the
> reference count and kset membership) uncleaned, which can cause:
> - Memory leaks of kobject internal structures
> - Potential use-after-free if there are pending references
> - Inconsistent state with the rest of the error handling code
>
> This fix matches the pattern used elsewhere in the kernel and in the
> same function (err_put label) which correctly uses kobject_put().
>
> Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> Suggested-by: Baolin Wang <baolin.wang@linux.alibaba.com>
LGTM, so:
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
>
> ---
> Change in v2:
> - Use goto err_put instead of kobject_put() + goto err for consistency
> - Add suggested
> ---
> mm/huge_memory.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 2bccb0a53a0a..589f7bf1f19d 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -818,10 +818,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);
> --
> 2.25.1
>
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v2] mm: huge_memory: Fix kobject cleanup in thpsize_create error
2026-07-13 5:41 [PATCH v2] mm: huge_memory: Fix kobject cleanup in thpsize_create error Hongling Zeng
2026-07-14 10:54 ` Lorenzo Stoakes (ARM)
@ 2026-07-15 1:03 ` Zi Yan
2026-07-15 6:40 ` Baolin Wang
2 siblings, 0 replies; 4+ messages in thread
From: Zi Yan @ 2026-07-15 1:03 UTC (permalink / raw)
To: Hongling Zeng, akpm, david, ljs, baolin.wang, liam, npache,
ryan.roberts, dev.jain, baohua, lance.yang
Cc: linux-mm, linux-kernel, zhongling0719
On Mon Jul 13, 2026 at 1:41 AM EDT, Hongling Zeng wrote:
> When kobject_init_and_add() fails, the kobject API requires calling
> kobject_put() to properly clean up the memory, not direct kfree().
>
> According to the kobject API documentation, kobject_init_and_add()
> calls kobject_init() internally. If the subsequent kobject_add()
> fails, the kobject has still been initialized and must be cleaned up
> via the reference count mechanism (kobject_put), not direct kfree().
>
> Direct kfree() leaves the kobject's internal state (including the
> reference count and kset membership) uncleaned, which can cause:
> - Memory leaks of kobject internal structures
> - Potential use-after-free if there are pending references
> - Inconsistent state with the rest of the error handling code
>
> This fix matches the pattern used elsewhere in the kernel and in the
> same function (err_put label) which correctly uses kobject_put().
>
> Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> Suggested-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>
> ---
> Change in v2:
> - Use goto err_put instead of kobject_put() + goto err for consistency
> - Add suggested
> ---
> mm/huge_memory.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
LGTM.
Acked-by: Zi Yan <ziy@nvidia.com>
--
Best Regards,
Yan, Zi
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v2] mm: huge_memory: Fix kobject cleanup in thpsize_create error
2026-07-13 5:41 [PATCH v2] mm: huge_memory: Fix kobject cleanup in thpsize_create error Hongling Zeng
2026-07-14 10:54 ` Lorenzo Stoakes (ARM)
2026-07-15 1:03 ` Zi Yan
@ 2026-07-15 6:40 ` Baolin Wang
2 siblings, 0 replies; 4+ messages in thread
From: Baolin Wang @ 2026-07-15 6:40 UTC (permalink / raw)
To: Hongling Zeng, akpm, david, ljs, ziy, liam, npache, ryan.roberts,
dev.jain, baohua, lance.yang
Cc: linux-mm, linux-kernel, zhongling0719
On 7/13/26 1:41 PM, Hongling Zeng wrote:
> When kobject_init_and_add() fails, the kobject API requires calling
> kobject_put() to properly clean up the memory, not direct kfree().
>
> According to the kobject API documentation, kobject_init_and_add()
> calls kobject_init() internally. If the subsequent kobject_add()
> fails, the kobject has still been initialized and must be cleaned up
> via the reference count mechanism (kobject_put), not direct kfree().
>
> Direct kfree() leaves the kobject's internal state (including the
> reference count and kset membership) uncleaned, which can cause:
> - Memory leaks of kobject internal structures
> - Potential use-after-free if there are pending references
> - Inconsistent state with the rest of the error handling code
>
> This fix matches the pattern used elsewhere in the kernel and in the
> same function (err_put label) which correctly uses kobject_put().
>
> Fixes: 3485b88390b0 ("mm: thp: introduce multi-size THP sysfs interface")
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> Suggested-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>
> ---
LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
By the way, I don't think my Suggested-by tag is needed, as it was just
a simple comment (But no need to respin).
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-07-15 6:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-13 5:41 [PATCH v2] mm: huge_memory: Fix kobject cleanup in thpsize_create error Hongling Zeng
2026-07-14 10:54 ` Lorenzo Stoakes (ARM)
2026-07-15 1:03 ` Zi Yan
2026-07-15 6:40 ` Baolin Wang
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.