From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D96DB373BEC for ; Tue, 14 Jul 2026 10:54:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784026463; cv=none; b=qGqTV22mka25+KOVIex+316UwJ8+ZdhAG6qmvK6igfVlJJuZGIYCcUPJVflOeABptYIHuuaRjJ3z2UmLiYQehuMDo7zkTiGz1UzeWxS5IVd2zi3KXD1vv0O582m0KbfSX/truFP5vUOvGawb4Oezy8eU45ZUWd404XWaHgILq1Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784026463; c=relaxed/simple; bh=HFgXndS6jr2+HJmUJb2IAcb9XyM+IC2kLOddISuYcZU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=oChKFKegtkBNY3j6mG+A/r7lQpjwsbX6USTJJ3bQ1DBjOLb0lC40GMDp8ZzgIEw4Fn1GjCZ6s23lP3Z5z3j4QeLEHLyNqG36t3XNeQ1OtzI5HfSQX9cdsAykgKubVm/8zUR3CygZLM1gtYg66M9jUc0PrVh/PY05+SUuylhNg0Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nHm1Td1r; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nHm1Td1r" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F4A21F000E9; Tue, 14 Jul 2026 10:54:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784026461; bh=wMpVtaWlfMhMEV83r6KXcc/9zGUoXbNewrSrXtzotb4=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=nHm1Td1rOz2dbi5zyg/MSbrv/ONV7kCR0SkN8ZJQLOqydK7XNUIMxK/k+oPpZZEup LZ1FEgRVzVPUZsqiDS0qpDbDLD00YbqsWFg6KPelmOQhM31b0RIS1tfZjWZq179cue Bk89rn7RJ9mqAcoMWsUqfrvq5qXbH9ljv1tU+WQkKVrXXPd77La3ul4nCzAGb8wrn4 ECOWIXRlDDcTgbCocjNxrt2XpLE0Y8keeyUZT214Evi+0ghlPoD7jATa8ZyY4RSnf0 p0lQyHlzfOTkLXfl2HC1Bk6vSF2WWLDl2rBtDWebP6RAeKM0W5Lu7sGwJop5qkfSAP CAANSqydU4e0Q== Date: Tue, 14 Jul 2026 11:54:08 +0100 From: "Lorenzo Stoakes (ARM)" To: Hongling Zeng 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 Message-ID: References: <20260713054154.120915-1-zenghongling@kylinos.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 > Suggested-by: Baolin Wang LGTM, so: Reviewed-by: Lorenzo Stoakes (ARM) > > --- > 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