From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) (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 4D002416D07 for ; Tue, 9 Jun 2026 13:14:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781010868; cv=none; b=CzdgAjLHi4FhBR/lXyGVcXoMwdTuWBEYunOLqYfWi4Pa8uSBP/5YYiP5N2ujAXaQ675jeYEGomwiOVaDEssgSLGkiF4U0CYZKthf6eJsxVDp88zBGlYneQzOKMeLKrSZ2AXh1Lrj5byee4gOE291ECqzg7fRzcJVhXjSexCD3xM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781010868; c=relaxed/simple; bh=n2OPP0er3QRrfjKaiKzhtRwB4jz2bCGHPt9Jvad0+r8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=mXXkYfqiepDvMBdcQnXu6emOMXHhLg5uby/Ueve3v4G4QdfPLpGansiZYrVqh93Mm8A4k5YQhH/a2z29IdovboE7t/x+ssmx1XES1t5ea5wKglV4ad+3Y2dQwZUl5ryJCO8NPl1w5v2VEL3sS3JYWeE4xfSvuq8tiVlBkSHBQGg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=JgWY+XUA; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="JgWY+XUA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=A0 tvLB5YUuFv5cwftes5RlfOdEv+BFowRNJ3KqYcQJM=; b=JgWY+XUA2ppeD8CDgo Qs37i5s/s647ojAG2/VNwqFq/x7bM8f3Ek9302nvbb3enn4wetL+CC53OVIUgeeK FrR8ST2UPNH9M1UybTj46nEzKj5J76CragV3rAnLDowrWX/QDtr7L+PVaZqoUMwv hE908Ali84eGMhp4XaYM19i2Q= Received: from ubuntu24-z.. (unknown []) by gzsmtp5 (Coremail) with SMTP id QCgvCgC3j71TEShqyBQjBQ--.54036S2; Tue, 09 Jun 2026 21:12:51 +0800 (CST) From: ranxiaokai627@163.com To: david@kernel.org, akpm@linux-foundation.org, ljs@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 Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Ran Xiaokai Subject: [PATCH] mm/huge_memory: fix memory leak when kobject_init_and_add() fails Date: Tue, 9 Jun 2026 13:12:49 +0000 Message-ID: <20260609131249.89288-1-ranxiaokai627@163.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:QCgvCgC3j71TEShqyBQjBQ--.54036S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7ZF1xGF4UKF4xJry3tF43KFg_yoW8JFyfpr 93JwnrK3y0vrs7X3yUAan5Z348GwsYgry5uFWYvw1Iy343JFnFyF1FyFWDZayUWrZayF47 ZF4qyasYq3Z8uF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jeFAJUUUUU= X-CM-SenderInfo: xudq5x5drntxqwsxqiywtou0bp/xtbCxhP8SWooEVO8qAAA30 From: Ran Xiaokai As documented in the comments for kobject_init_and_add(): "If this function returns an error, kobject_put() must be called to properly clean up the memory associated with the object. This is the same type of error handling after a call to kobject_add() and kobject lifetime rules are the same here." This is because kobject_init_and_add() may have already allocated memory internally for the kobject name (kobj->name), and leaving the refcount at 1 prevents its release callback from being triggered. Fixes: 3485b88390b0a ("mm: thp: introduce multi-size THP sysfs interface") Signed-off-by: Ran Xiaokai --- mm/huge_memory.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 653f2dc03403..601750dbe79f 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -790,11 +790,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); if (ret) -- 2.25.1