From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 7255330C142 for ; Tue, 7 Jul 2026 20:46:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783457177; cv=none; b=oQPrnPyZ8GxO6gKSW+33FuzGXWoe1BuRIqUHaVWh4AcnlUaIPzyL3Ff3l1d2PTyWyhyDKrKHobohfBNq9T0FarsXD7rIslXJuhNSmcGKlNyr8/dNpCEbTeiqIboRj70aLW8IOT92GFqQIDqwY7aQQAh1GxyNLe86799z5ln3IIM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783457177; c=relaxed/simple; bh=3qSbeshutJQrqanyiBV5+kKek6b+adOK4DrRJA11o8M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=S584zyGpM8CR4XQ/DRv7/Ly79vkTlbZPBCsLCp8BVXeOx8MRq7l7J7wDa/fL+BfG0Ze+jOYlun8Rf09Xa6axe0zLA4/zhNDN/X4vJLNayhFuEX6HxXsdh8D5SuzonB1jcIJQDLW7AE5mnhyySaRSF0V5leshyAcmtT1zD43W1kI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hXge1C4n; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hXge1C4n" Received: by smtp.kernel.org (Postfix) with ESMTPS id 2F0E5C2BCF6; Tue, 7 Jul 2026 20:46:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783457177; bh=3qSbeshutJQrqanyiBV5+kKek6b+adOK4DrRJA11o8M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=hXge1C4nzY7dlpABVxo8H1hHfpEOcioGCds0whqNw7XUAqLLC+OyKLiiE89xBmpkr eyvtt+Zfo8RXrxaADTvR8cswT8R09rMTJ5AfZoff2Z/eR3JFLy/HaXi4Onjxl6+Lmq Fyyq7QJnIZz+5F/IDNzivbSYP6++jmCF+A+eReEVOcGusuZtbya/JgJ6NLVSzZZzey ap3jSinQCDmgOBUHS8kAX77CG+GobdNhTzzn0ltGrUbqYHzXTgvb3/HUPYNeahQnHj BZNQ48L5Om12You5Xji34f+kfTNukI3EQi1t7p0J4yYtLw5Ou3yemRPMe69F+Pp5iO oRc7rMtwmUvAw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09746C44501; Tue, 7 Jul 2026 20:46:17 +0000 (UTC) From: Ackerley Tng via B4 Relay Date: Tue, 07 Jul 2026 13:46:08 -0700 Subject: [PATCH 1/4] mm: hugetlb: Fix subpool usage leak on allocation failure 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="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260707-hugetlb-alloc-failure-fixes-v1-1-5bbd3a4b836d@google.com> References: <20260707-hugetlb-alloc-failure-fixes-v1-0-5bbd3a4b836d@google.com> In-Reply-To: <20260707-hugetlb-alloc-failure-fixes-v1-0-5bbd3a4b836d@google.com> To: Muchun Song , Oscar Salvador , David Hildenbrand , Joshua Hahn , Shakeel Butt , Nhat Pham , Andrew Morton , Peter Xu , Wupeng Ma , fvdl@google.com, rientjes@google.com Cc: vannapurve@google.com, erdemaktas@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Ackerley Tng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783457176; l=2054; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=cA05z4q3cmpRjd2OhLZkp4otFIh88A72+UVO+VC1THA=; b=SPjMgY+qFAw75FTtWJ8Y+fwGaYQBAgZo0MU0E6d8mHmDzPWGEkAKa+/7oTZ0ZFgY6e1EP7FLK W4tZXsQrZSdBwM4aM0O+kIAwzsobl1+hO80X/KRmZ4v33M0ebtmoEIR X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Endpoint-Received: by B4 Relay for ackerleytng@google.com/20260225 with auth_id=649 X-Original-From: Ackerley Tng Reply-To: ackerleytng@google.com From: Ackerley Tng When alloc_hugetlb_folio() fails early (e.g. buddy allocation failure or hugetlb cgroup charging failure) and gbl_chg == 1 (meaning a reservation was not used, but a global page was allocated instead), the subpool page acquired via hugepage_subpool_get_pages() must still be returned. Currently, the error path out_subpool_put: only calls hugepage_subpool_put_pages() if !gbl_chg is true. If gbl_chg is 1, it skips it, permanently leaking the subpool's used_hpages counter. Fix this by calling hugepage_subpool_put_pages() unconditionally if map_chg is true. Only call hugetlb_acct_memory() to adjust global reservations if !gbl_chg is true, to prevent underflowing the global resv_huge_pages counter. Fixes: a833a693a490e ("mm: hugetlb: fix incorrect fallback for subpool") Signed-off-by: Ackerley Tng --- mm/hugetlb.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 571212b80835e..e607dcbb2fc3e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2858,7 +2858,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, struct hugepage_subpool *spool = subpool_vma(vma); struct hstate *h = hstate_vma(vma); struct folio *folio; - long retval, gbl_chg, gbl_reserve; + long retval, gbl_chg; map_chg_state map_chg; int ret, idx; struct hugetlb_cgroup *h_cg = NULL; @@ -3009,13 +3009,11 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h), h_cg_rsvd); out_subpool_put: - /* - * put page to subpool iff the quota of subpool's rsv_hpages is used - * during hugepage_subpool_get_pages. - */ - if (map_chg && !gbl_chg) { - gbl_reserve = hugepage_subpool_put_pages(spool, 1); - hugetlb_acct_memory(h, -gbl_reserve); + if (map_chg) { + long gbl_reserve = hugepage_subpool_put_pages(spool, 1); + + if (!gbl_chg) + hugetlb_acct_memory(h, -gbl_reserve); } -- 2.55.0.795.g602f6c329a-goog