Re: [PATCH 01/21] mm/hugetlb: charge the copy made at fork to the parent
From: Hongfu Li
Date: Thu Nov 26 2026 - 06:17:25 EST
在 11/26/26 06:32, David Hildenbrand (Arm) 写道:
When copying page tables during fork(), the child is not yet assigned to
the right memcg. When copying possibly pinned anon hugetlb folios, we
must charge them to the parent's memcg, otherwise we can end up
allocating more hugetlb folios than allowed.
Let's fix it by charging the copy to the parent's mm, like we do when
copying small folios in copy_present_page(), introducing a new helper
function alloc_hugetlb_folio_for_fork().
This is a hotfix for v7.4.
Fixes: c2deb1356d82 ("mm/hugetlb: charge folios to the target mm's memcg")
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Acked-by: Michal Hocko <mhocko@xxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Tested-by: Hongfu Li <lihongfu@xxxxxxxxxx>
Reviewed-by: Hongfu Li <lihongfu@xxxxxxxxxx>
---
mm/hugetlb.c | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index d0e66fb306221..f46c85fc0cd0b 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -3103,8 +3103,9 @@ typedef enum {
* allocation). New call sites should (probably) never set it to true!!
* When it's set, the allocation will bypass all vma level reservations.
*/
-struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
- unsigned long addr, bool cow_from_owner)
+static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
+ unsigned long addr, bool cow_from_owner,
+ struct mm_struct *memcg_mm)
{
struct hugepage_subpool *spool = subpool_vma(vma);
struct hstate *h = hstate_vma(vma);
@@ -3187,7 +3188,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
.nodemask = nodemask,
};
- folio = hugetlb_alloc_folio(h, &mpoli, vma->vm_mm, alloc_flags);
+ folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags);
mpol_cond_put(mpol);
@@ -3249,6 +3250,23 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
return ERR_PTR(ret);
}
+struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
+ unsigned long addr, bool cow_from_owner)
+{
+ return __alloc_hugetlb_folio(vma, addr, cow_from_owner, vma->vm_mm);
+}
+
+/*
+ * Allocate a folio for a copy in the child VMA during fork(), charged to the
+ * parent's mm: the child is not in its memory cgroup yet.
+ */
+static struct folio *alloc_hugetlb_folio_for_fork(struct vm_area_struct *dst_vma,
+ unsigned long addr, struct mm_struct *src_mm)
+{
+ return __alloc_hugetlb_folio(dst_vma, addr, /* cow_from_owner = */ false,
+ src_mm);
+}
+
static __init void *alloc_bootmem(struct hstate *h, int nid, bool node_exact)
{
if (hugetlb_early_cma(h))
@@ -5110,7 +5128,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
spin_unlock(src_ptl);
spin_unlock(dst_ptl);
/* Do not use reserve as it's private owned */
- new_folio = alloc_hugetlb_folio(dst_vma, addr, false);
+ new_folio = alloc_hugetlb_folio_for_fork(dst_vma, addr, src);
if (IS_ERR(new_folio)) {
folio_put(pte_folio);
ret = PTR_ERR(new_folio);