All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Hongling Zeng <zenghongling@kylinos.cn>
Cc: akpm@linux-foundation.org, david@kernel.org, ziy@nvidia.com,
	 baolin.wang@linux.alibaba.com, liam@infradead.org,
	npache@redhat.com, ryan.roberts@arm.com,  dev.jain@arm.com,
	baohua@kernel.org, lance.yang@linux.dev, linux-mm@kvack.org,
	 linux-kernel@vger.kernel.org, zhongling0719@126.com
Subject: Re: [PATCH v2] mm: huge_memory: Fix kobject cleanup in thpsize_create error
Date: Tue, 14 Jul 2026 11:54:08 +0100	[thread overview]
Message-ID: <alYU423yLkWZlO6a@lucifer> (raw)
In-Reply-To: <20260713054154.120915-1-zenghongling@kylinos.cn>

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


  reply	other threads:[~2026-07-14 10:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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) [this message]
2026-07-15  1:03 ` Zi Yan
2026-07-15  6:40 ` Baolin Wang

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=alYU423yLkWZlO6a@lucifer \
    --to=ljs@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --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=npache@redhat.com \
    --cc=ryan.roberts@arm.com \
    --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 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.