mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mm: huge_memory: Fix kobject cleanup in thpsize_create error
@ 2026-07-11  8:46 Hongling Zeng
  2026-07-13  4:00 ` Baolin Wang
  2026-07-15  1:02 ` Zi Yan
  0 siblings, 2 replies; 4+ messages in thread
From: Hongling Zeng @ 2026-07-11  8:46 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, stable

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")
Cc: stable@vger.kernel.org
Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
---
 mm/huge_memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 2bccb0a53a0a..7aeb17d60ac7 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -819,7 +819,7 @@ 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);
+		kobject_put(&thpsize->kobj);
 		goto err;
 	}
 
-- 
2.25.1


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

* Re: [PATCH] mm: huge_memory: Fix kobject cleanup in thpsize_create error
  2026-07-11  8:46 [PATCH] mm: huge_memory: Fix kobject cleanup in thpsize_create error Hongling Zeng
@ 2026-07-13  4:00 ` Baolin Wang
  2026-07-15  1:02 ` Zi Yan
  1 sibling, 0 replies; 4+ messages in thread
From: Baolin Wang @ 2026-07-13  4:00 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, stable



On 7/11/26 4:46 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")
> Cc: stable@vger.kernel.org

I don't think this need to CC stable.

> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> ---

A related fix was already sent before[1]. As noted in the previous 
comments, just change it to 'goto err_put'.

[1] 
https://lore.kernel.org/all/20260412175428.2613383-1-lgs201920130244@gmail.com/

>   mm/huge_memory.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 2bccb0a53a0a..7aeb17d60ac7 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -819,7 +819,7 @@ 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);
> +		kobject_put(&thpsize->kobj);
>   		goto err;
>   	}
>   


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

* Re: [PATCH] mm: huge_memory: Fix kobject cleanup in thpsize_create error
  2026-07-11  8:46 [PATCH] mm: huge_memory: Fix kobject cleanup in thpsize_create error Hongling Zeng
  2026-07-13  4:00 ` Baolin Wang
@ 2026-07-15  1:02 ` Zi Yan
  2026-07-15  1:05   ` Zi Yan
  1 sibling, 1 reply; 4+ messages in thread
From: Zi Yan @ 2026-07-15  1:02 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, stable

On Sat Jul 11, 2026 at 4:46 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
> ---
>  mm/huge_memory.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
LGTM.

Acked-by: Zi Yan <ziy@nvidia.com>



-- 
Best Regards,
Yan, Zi


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

* Re: [PATCH] mm: huge_memory: Fix kobject cleanup in thpsize_create error
  2026-07-15  1:02 ` Zi Yan
@ 2026-07-15  1:05   ` Zi Yan
  0 siblings, 0 replies; 4+ messages in thread
From: Zi Yan @ 2026-07-15  1:05 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, stable

On Tue Jul 14, 2026 at 9:02 PM EDT, Zi Yan wrote:
> On Sat Jul 11, 2026 at 4:46 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")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Hongling Zeng <zenghongling@kylinos.cn>
>> ---
>>  mm/huge_memory.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
> LGTM.
>
> Acked-by: Zi Yan <ziy@nvidia.com>

Oops, forgot that V2 was sent and missed Baolin's comment. Sorry for the
noise.


-- 
Best Regards,
Yan, Zi


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

end of thread, other threads:[~2026-07-15  1:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-11  8:46 [PATCH] mm: huge_memory: Fix kobject cleanup in thpsize_create error Hongling Zeng
2026-07-13  4:00 ` Baolin Wang
2026-07-15  1:02 ` Zi Yan
2026-07-15  1:05   ` Zi Yan

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

Powered by JetHome