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 17AE83314C4 for ; Thu, 2 Jul 2026 16:21:52 +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=1783009312; cv=none; b=lS0rhF+2QU69h4UX7IMDrcKiolsOIOLv5q28/U29OSaBIGUDQaMk2KkqKWJWIQ8Hy/MGNTsnstiTTGO4It1vkNzX7CbiuGT8aD61TaODuzyBFMw3izyVrIBKV4nbRl9tsxrbjdglF8VUJT4ysQAugofkk27nehUhLztVowHo72U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783009312; c=relaxed/simple; bh=ScYrHpTLaEGY4fjKrOJyaCi7PO9G1jigzJNU7rgVUWE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bGiAXuDDF6wK9O0A0eEN8AYJcJkenjaTWB3GJt1KBPreb26DGv2FTGjzMkm4Q3oe+ZcN4Fjuszp4aPCq9Ac7+M9oa2P9DoLOoLpPldptvpG8AReJB8yxHb44E/Bz2CL2UO+N0MxN3MZWUT2QvLiszg3TPksYxnfG1SkM4iAcT6o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=k9IwhmZN; 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="k9IwhmZN" Received: by smtp.kernel.org (Postfix) with ESMTPS id DFACCC2BCFD; Thu, 2 Jul 2026 16:21:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783009311; bh=ScYrHpTLaEGY4fjKrOJyaCi7PO9G1jigzJNU7rgVUWE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=k9IwhmZNmdFARvVe86zxnQudgxEJ1/UiBIiBdyzt6YCNeIdDEKl4m7FTKNw4BqVtk U8jmtXFcl3uSkBrf5CxKlQ8u53YmDA6a9z/1nP/TZrLwuW22V3hRg0mNN/+uPzOR6E mETvzcZzZ9f6CzDL1DDxYCDjkMgS+7LCIFCvc63hE5IArSrzRjx7noVDLL00k56HPj NEuiaT8NevDexniM5A/z58PTGt9MkB+OZ6JBR/Pvc8zMWQQipvjiWJFIckx3Sj3xXF t8TKqPgMVfhx94g6GuXigauE3CRjYpK57OgTdAQaKrPiwg2CM+nl5K+ri982CYroSL WXfsanCfk4SJQ== 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 CCED2C43458; Thu, 2 Jul 2026 16:21:51 +0000 (UTC) From: Ackerley Tng via B4 Relay Date: Thu, 02 Jul 2026 09:21:49 -0700 Subject: [PATCH v4 5/6] mm: hugetlb: Move mem_cgroup_charge_hugetlb() earlier in allocation 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: <20260702-hugetlb-open-up-v4-5-d53cefcccf34@google.com> References: <20260702-hugetlb-open-up-v4-0-d53cefcccf34@google.com> In-Reply-To: <20260702-hugetlb-open-up-v4-0-d53cefcccf34@google.com> To: Muchun Song , Oscar Salvador , David Hildenbrand , Andrew Morton , fvdl@google.com, jiaqiyan@google.com, joshua.hahnjy@gmail.com, jthoughton@google.com, mhocko@kernel.org, michael.roth@amd.com, pasha.tatashin@soleen.com, pbonzini@redhat.com, peterx@redhat.com, pratyush@kernel.org, rick.p.edgecombe@intel.com, rientjes@google.com, roman.gushchin@linux.dev, seanjc@google.com, shakeel.butt@linux.dev, shivankg@amd.com, vannapurve@google.com, yan.y.zhao@intel.com, Zi Yan , Matthew Brost , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Dan Williams , Jason Gunthorpe Cc: 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=1783009311; l=2464; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=YM0eoEwVRqN15NAQGhn2SLgSWfw138+SGW7+3TQlWbo=; b=F0Gzg9RzlR5MOiq9ompvYrScSReWJeOqgKYjCXThSWDFTc0gk7RyAq2vOnuPy5b9E46AKtr8U eM0sqLwP6l+AlfwMsVz9SUsbxBp563HNl6n7VndW6W6zk72Qk86xXe3 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 Move mem_cgroup_charge_hugetlb() earlier in the folio allocation process. This change draws a cleaner line between memcg charging and the subsequent hugetlb-specific reservation logic for VMAs and subpools. While it would be ideal to make all accounting and reservations perfectly symmetric, mem_cgroup_charge_hugetlb() is a complex operation that cannot be performed under the hugetlb_lock. Moving the charge to this earlier point ensures that memcg charging is handled before the code begins manipulating subpool and VMA-specific state. These two types of accounting will be separated in a future patch. If mem_cgroup_charge_hugetlb() fails, the code now branches to out_subpool_put to ensure the folio is freed and the subpool references are handled correctly. Signed-off-by: Ackerley Tng --- mm/hugetlb.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 176b7e9130a38..08ac28111e694 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2978,6 +2978,24 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, spin_unlock_irq(&hugetlb_lock); + ret = mem_cgroup_charge_hugetlb(folio, gfp | __GFP_RETRY_MAYFAIL); + /* + * 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) { + free_huge_folio(folio); + /* + * Skip uncharging hugetlb_cgroup since the charges + * were committed to the folio and freeing the folio + * would have cleared those up. + */ + goto out_subpool_put; + } + hugetlb_set_folio_subpool(folio, spool); if (map_chg != MAP_CHG_ENFORCED) { @@ -3005,19 +3023,6 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, } } - ret = mem_cgroup_charge_hugetlb(folio, gfp | __GFP_RETRY_MAYFAIL); - /* - * 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) { - free_huge_folio(folio); - return ERR_PTR(-ENOMEM); - } - return folio; out_uncharge_cgroup: -- 2.55.0.rc0.799.gd6f94ed593-goog