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 9C65D1DA0E1 for ; Tue, 19 May 2026 00:19:49 +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=1779149989; cv=none; b=LhZYhqNxHxyoyP0DjTfImPDJAYdkXlPx6fxgdmEhED86HO+kUoHMSWJYpvvcL+Xwy7yrlJ/ge+TR5pZHy8KDqzjrO4hH3dz6zUqDVTqVvolAym4gQg/6wwaPvNaG96I5tg+C42l6ZKAzIQeXf9gbNVAbocxSyV72taJdj1gPvn8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779149989; c=relaxed/simple; bh=3zXO270ajr1tnjZV8A/bAEEs7bzJ1z/L+UKQw43NjcA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RJLHk9UTEr7jya4LfjT1oByf+A/lnpt3ltbAs1Ub/OpYJ8lXTFCwJ1pa5mITpbkVdxec69OBdAHrM5POq9HyOozdouLEQ3AiUY8DyYVgvTJLhbUKB0CCupmH5ft0FuVx9y1bm3qDmYHD9NtSqZpqxenAqmuBwI0qtz8DzlOxtO0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J9DRXfHT; 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="J9DRXfHT" Received: by smtp.kernel.org (Postfix) with ESMTPS id 73B2FC2BCC7; Tue, 19 May 2026 00:19:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779149989; bh=3zXO270ajr1tnjZV8A/bAEEs7bzJ1z/L+UKQw43NjcA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=J9DRXfHTV1x2wT9FDO37latHI4bpt8gKaGiG5kfWObrWNPOZ1PwiIDrOaEJd+qvUc fX8hs18VuXivZ952QtRTEqLm9GDQOebDPvvpE9leVmnrASt8EZc/cXz6xShxEgVU0c b4smGrhdd51TB8f7BsoVCY4yPNH0LmIU3+U7u4X+CnHBCK/Bx6W2HZhu6J0m+pIZLN iHZhcVrD/U0y0EoS9jwt1VwH06vAlMgb9QNeUtqc86x6/5dUBPPKicK9yb/AdxLiPY eVFi+X0T0rI6PwlZjyYq5+elbPAY9VpQNo7nI2LWfrTevOu3tHlBQceHIEdmUp/aY8 JXySAESSXTJgQ== 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 651A9CD4F3C; Tue, 19 May 2026 00:19:49 +0000 (UTC) From: Ackerley Tng via B4 Relay Date: Mon, 18 May 2026 17:19:44 -0700 Subject: [PATCH v3 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: <20260518-hugetlb-open-up-v3-5-e14b302477f8@google.com> References: <20260518-hugetlb-open-up-v3-0-e14b302477f8@google.com> In-Reply-To: <20260518-hugetlb-open-up-v3-0-e14b302477f8@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=1779149988; l=2460; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=D0/c/tPxzDKBQ5LqjBs64oNrn/JHwmJjr3L/Nq/FGGs=; b=5rbwFVt3aoGkbaTw+PHbELOYhBY/NhAnIaG1JfuEYmejI73V/Ie6kUAohYd18a9Q8wrlAb3d0 25QVtOzNcpCBbwDanBDndQxjoPXO5fAgCNnuuhzu8IADnbhR2zQU0vp 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 ad07e72d6fac3..81e73186dff09 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2991,6 +2991,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) { @@ -3018,19 +3036,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.54.0.563.g4f69b47b94-goog