mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Baolin Wang <baolin.wang@linux.alibaba.com>
To: Hongling Zeng <zenghongling@kylinos.cn>,
	akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org,
	ziy@nvidia.com, liam@infradead.org, npache@redhat.com,
	ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org,
	lance.yang@linux.dev
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	zhongling0719@126.com, stable@vger.kernel.org
Subject: Re: [PATCH] mm: huge_memory: Fix kobject cleanup in thpsize_create error
Date: Mon, 13 Jul 2026 12:00:00 +0800	[thread overview]
Message-ID: <368ddc3f-1206-4e2b-a45b-c67b0f54c58b@linux.alibaba.com> (raw)
In-Reply-To: <20260711084624.207777-1-zenghongling@kylinos.cn>



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;
>   	}
>   


  reply	other threads:[~2026-07-13  4:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-11  8:46 Hongling Zeng
2026-07-13  4:00 ` Baolin Wang [this message]
2026-07-15  1:02 ` Zi Yan
2026-07-15  1:05   ` Zi Yan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=368ddc3f-1206-4e2b-a45b-c67b0f54c58b@linux.alibaba.com \
    --to=baolin.wang@linux.alibaba.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=npache@redhat.com \
    --cc=ryan.roberts@arm.com \
    --cc=stable@vger.kernel.org \
    --cc=zenghongling@kylinos.cn \
    --cc=zhongling0719@126.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®