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 7270930F806 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=YirywGAaTHyCFArgUgEGHvy6fKS+rE6XIUQ12mOBhc2mv0Pbg6OEI13bmmhMVM/PQG7pAwpm0JzNcqm9KSsNnKmANvtXG8Mq56Be5TnmP3zI4iWXedbSMCXzIlWtQsHZEmrPjoCkK+1ej9M4BeIkAAp3kvB4YlVcByb8zM/DsTE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783457177; c=relaxed/simple; bh=+0kuj1bG/SlCGtU90RU3Z+4wHYzXatUwjgJpAiL34Ng=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PFS/Y0ktlC2If5mOWTqRuuCbjh6cUBCDWTSxg/9dCGYZ2UpmBvjj4VEi6+tbIDB0uHwktddy497HCLoEZJvxTK505xbsaBn6bSzTLTl2xeHbqbUbCtmJ+Hhuo3+B9swKeGsPZswOgGVdI8VI8Pl3UWxHaGZEBBS3wY9uUurfUoU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dBNMRtRM; 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="dBNMRtRM" Received: by smtp.kernel.org (Postfix) with ESMTPS id 466C6C2BCFF; 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=+0kuj1bG/SlCGtU90RU3Z+4wHYzXatUwjgJpAiL34Ng=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=dBNMRtRM3esgz9pnFgibSTCLZmEmjDGAK9WRa7MJb36G+jj7Xf21lXdQM6yfTwD+f k66oCSCQCh7C9aBtU7zcGojC3Q8uES6Ot8MBUwvqyYTc381vT2K3uBXW8BrW7b4JCm d1LbrWFeCRDxmQymq/XxU7MDKJ0HY59CJBcv7L8536SUE19KZ/796qFrsMh8l6H/FK d/gmGJ+tOeMg/EwVhika3ZVpBd/mIdoUXPhjFPYGruLxddJC5VblKcQWT2yve6Bwhw MtIrFUCbR4cc6YiPSC0hYhrVv1BtxhAv+vXNOfQNXq8mm2CT4vn9bv3tubLcgTE6qB OSXWJ+7i+Dj0Q== 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 32FFDC44506; Tue, 7 Jul 2026 20:46:17 +0000 (UTC) From: Ackerley Tng via B4 Relay Date: Tue, 07 Jul 2026 13:46:11 -0700 Subject: [PATCH 4/4] mm: hugetlb: Move memcg charge earlier to prevent reservation leak 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-4-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=2702; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=byYmVafBqzJqhYJN9f0HauTLLeaXGUi0G31EyYblf3o=; b=LCCWWXJth+OA0REwYwzLBuY8O/lKZjNXSmDWNmT81I+NFmGnhEJ5SQsbNpo6kQIBhWeYFuVMV DdkSTRX873dB9r/qxv8pO0g7vNYeRSDdlMN2+qe4e1MoF21xRLxS0Oq 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 mem_cgroup_charge_hugetlb() fails, alloc_hugetlb_folio() clears the charges and frees the folio. The reservation committed via vma_commit_reservation() was not undone, leaving the reservation map in an inconsistent state, causing resv_huge_pages to leak when the process exited. Fix this by moving the mem_cgroup_charge_hugetlb() call earlier in alloc_hugetlb_folio(), before vma_commit_reservation() is called. If the charge fails now, the reservation is not yet committed. Jump to out_subpool_put, which will then call vma_end_reservation() to abort the reservation and keep the reservation map and resv_huge_pages counter consistent. Fixes: 991135774c0e0 ("memcg/hugetlb: introduce mem_cgroup_charge_hugetlb") Signed-off-by: Ackerley Tng --- mm/hugetlb.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index e42049e2c038b..6e4acb1f021a5 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2960,6 +2960,25 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, spin_unlock_irq(&hugetlb_lock); + ret = mem_cgroup_charge_hugetlb(folio, gfp); + /* + * Unconditionally increment NR_HUGETLB here. If it turns out that + * mem_cgroup_charge_hugetlb failed, then immediately free the page and + * decrement NR_HUGETLB. + */ + lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h)); + + if (ret == -ENOMEM) { + folio_put(folio); + /* + * Charges to hugetlb_cgroup for usage and + * reservations were already committed, so folio_put() + * would have uncharged those. Go straight to undoing + * subpool charges. + */ + goto out_subpool_put; + } + hugetlb_set_folio_subpool(folio, spool); if (map_chg != MAP_CHG_ENFORCED) { @@ -2987,19 +3006,6 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, } } - ret = mem_cgroup_charge_hugetlb(folio, gfp); - /* - * Unconditionally increment NR_HUGETLB here. If it turns out that - * mem_cgroup_charge_hugetlb failed, then immediately free the page and - * decrement NR_HUGETLB. - */ - lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h)); - - if (ret == -ENOMEM) { - folio_put(folio); - goto err; - } - return folio; out_uncharge_cgroup: @@ -3020,7 +3026,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, out_end_reservation: if (map_chg != MAP_CHG_ENFORCED) vma_end_reservation(h, vma, addr); -err: + /* * Return -ENOSPC when this function fails to allocate or * charge a huge page. If a standard (PAGE_SIZE) page -- 2.55.0.795.g602f6c329a-goog