FAILED: patch "[PATCH] mm/hugetlb: charge folios to the target mm's memcg" failed to apply to 6.18-stable tree
Subsystems (4)
Sasha Levin
maintainerThe patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x c2deb1356d82c9d54bd2fcf1241cc97eb66fd81f
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From c2deb1356d82c9d54bd2fcf1241cc97eb66fd81f Mon Sep 17 00:00:00 2001
From: Jinmeng Zhou <[email protected]>
Date: Thu, 3 Sep 2026 15:50:48 +0800
Subject: [PATCH] mm/hugetlb: charge folios to the target mm's memcg
HugeTLB folios are currently charged to the memcg of the allocating task. This gives the wrong result when a userfaultfd handler populates a HugeTLB VMA that belongs to another process. The UFFDIO_COPY ioctl operates on the userfaultfd context's mm, but get_mem_cgroup_from_current() charges the folio to the handler's memcg instead.
This can be reproduced by placing the faulting process and its userfaultfd handler in different memory cgroups. Have the target process register a HugeTLB mapping with userfaultfd, trigger a missing fault, and let the handler resolve it with UFFDIO_COPY. The hugepage usage is then reported in the handler's memory.current instead of the target's.
The generic userfaultfd population path avoids this problem by charging folios to dst_vma->vm_mm.
Pass the target mm through hugetlb_alloc_folio() and charge the folio by using get_mem_cgroup_from_mm(). This preserves the existing charge timing and error handling while making HugeTLB userfaultfd population consistent with the generic path.
--- include/linux/hugetlb.h | 3 ++- include/linux/memcontrol.h | 8 +++++--- mm/hugetlb.c | 9 ++++++--- mm/memcontrol.c | 6 ++++-- 4 files changed, 17 insertions(+), 9 deletions(-)
include/linux/hugetlb.h +2 -1
@@ -694,7 +694,8 @@ enum hugetlb_alloc_flag {#define HUGETLB_ALLOC_USE_GLOBAL_RESERVATIONS BIT(HUGETLB_ALLOC_USE_GLOBAL_RESERVATIONS_BIT)struct folio *hugetlb_alloc_folio (struct hstate *h, -struct mempolicy_interpreted *mpoli,u8 alloc_flags); +struct mempolicy_interpreted *mpoli,struct mm_struct *mm, +u8 alloc_flags);struct folio *alloc_hugetlb_folio (struct vm_area_struct *vma,unsigned long addr,bool cow_from_owner); struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
include/linux/memcontrol.h +5 -3
@@ -647,7 +647,8 @@ static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm,return __mem_cgroup_charge (folio, mm, gfp); } -int mem_cgroup_charge_hugetlb (struct folio * folio,gfp_t gfp); +int mem_cgroup_charge_hugetlb (struct folio *folio,struct mm_struct *mm, +gfp_t gfp);int mem_cgroup_swapin_charge_folio (struct folio *folio,unsigned short id,struct mm_struct *mm,gfp_t gfp);
@@ -1146,9 +1147,10 @@ static inline int mem_cgroup_charge(struct folio *folio,return 0 ; } -static inline int mem_cgroup_charge_hugetlb (struct folio * folio,gfp_t gfp) +static inline int mem_cgroup_charge_hugetlb (struct folio *folio, +struct mm_struct *mm,gfp_t gfp) { -return 0 ; +return 0 ; } static inline int mem_cgroup_swapin_charge_folio(struct folio *folio,
mm/hugetlb.c +6 -3
@@ -2844,6 +2844,7 @@ void wait_for_freed_hugetlb_folios(void)* hugetlb_alloc_folio - Allocate a hugetlb folio. * @h: Hugetlb state control block. * @mpoli: Interpreted memory policy to use for allocation. +* @mm: Memory descriptor of the allocation target. * @alloc_flags: Flags controlling the allocation behavior. * * Allocates a hugetlb folio and handles cgroup charging and global hstate
@@ -2853,7 +2854,8 @@ void wait_for_freed_hugetlb_folios(void)* -ENOSPC if cgroup charging fails or no folio is available. */ struct folio *hugetlb_alloc_folio (struct hstate *h, -struct mempolicy_interpreted *mpoli,u8 alloc_flags) +struct mempolicy_interpreted *mpoli,struct mm_struct *mm, +u8 alloc_flags) {bool charge_hugetlb_cgroup_rsvd = alloc_flags &HUGETLB_ALLOC_CHARG_CGROUP_RSVD ;
@@ -2908,7 +2910,8 @@ struct folio *hugetlb_alloc_folio(struct hstate *h,spin_unlock_irq (&hugetlb_lock); - ret =mem_cgroup_charge_hugetlb (folio, gfp | __GFP_RETRY_MAYFAIL); + ret =mem_cgroup_charge_hugetlb (folio, mm, + gfp | __GFP_RETRY_MAYFAIL);/* * Unconditionally increment NR_HUGETLB here because if * mem_cgroup_charge_hugetlb failed, freeing the page will
@@ -3051,7 +3054,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, .nodemask = nodemask, }; - folio =hugetlb_alloc_folio (h, &mpoli, alloc_flags); + folio =hugetlb_alloc_folio (h, &mpoli, vma->vm_mm, alloc_flags);mpol_cond_put (mpol);
mm/memcontrol.c +4 -2
@@ -5265,6 +5265,7 @@ int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp)/** * mem_cgroup_charge_hugetlb - charge the memcg for a hugetlb folio * @folio: folio being charged +* @mm: mm context of the allocation target * @gfp: reclaim mode * * This function is called when allocating a huge page folio, after the page has
@@ -5274,9 +5275,10 @@ int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp)* Returns ENOMEM if the memcg is already full. * Returns 0 if either the charge was successful, or if we skip the charging. */ -int mem_cgroup_charge_hugetlb (struct folio *folio,gfp_t gfp) +int mem_cgroup_charge_hugetlb (struct folio *folio,struct mm_struct *mm, +gfp_t gfp) { -struct mem_cgroup *memcg =get_mem_cgroup_from_current (); +struct mem_cgroup *memcg =get_mem_cgroup_from_mm (mm);int ret =0 ;/*
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/1 mm/hugetlb: charge folios to the target mm's memcg
From: Jinmeng Zhou <[email protected]>
[ Upstream commit c2deb1356d82c9d54bd2fcf1241cc97eb66fd81f ]
HugeTLB folios are currently charged to the memcg of the allocating task. This gives the wrong result when a userfaultfd handler populates a HugeTLB VMA that belongs to another process. The UFFDIO_COPY ioctl operates on the userfaultfd context's mm, but get_mem_cgroup_from_current() charges the folio to the handler's memcg instead.
This can be reproduced by placing the faulting process and its userfaultfd handler in different memory cgroups. Have the target process register a HugeTLB mapping with userfaultfd, trigger a missing fault, and let the handler resolve it with UFFDIO_COPY. The hugepage usage is then reported in the handler's memory.current instead of the target's.
The generic userfaultfd population path avoids this problem by charging folios to dst_vma->vm_mm.
Pass the target mm through hugetlb_alloc_folio() and charge the folio by using get_mem_cgroup_from_mm(). This preserves the existing charge timing and error handling while making HugeTLB userfaultfd population consistent with the generic path.
--- include/linux/memcontrol.h | 8 +++++--- mm/hugetlb.c | 2 +- mm/memcontrol.c | 6 ++++-- 3 files changed, 10 insertions(+), 6 deletions(-)
include/linux/memcontrol.h +5 -3
@@ -662,7 +662,8 @@ static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm,return __mem_cgroup_charge (folio, mm, gfp); } -int mem_cgroup_charge_hugetlb (struct folio * folio,gfp_t gfp); +int mem_cgroup_charge_hugetlb (struct folio *folio,struct mm_struct *mm, +gfp_t gfp);int mem_cgroup_swapin_charge_folio (struct folio *folio,struct mm_struct *mm,gfp_t gfp,swp_entry_t entry);
@@ -1172,9 +1173,10 @@ static inline int mem_cgroup_charge(struct folio *folio,return 0 ; } -static inline int mem_cgroup_charge_hugetlb (struct folio * folio,gfp_t gfp) +static inline int mem_cgroup_charge_hugetlb (struct folio *folio, +struct mm_struct *mm,gfp_t gfp) { -return 0 ; +return 0 ; } static inline int mem_cgroup_swapin_charge_folio(struct folio *folio,
mm/hugetlb.c +1 -1
@@ -3124,7 +3124,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, } } - ret =mem_cgroup_charge_hugetlb (folio, gfp); + ret =mem_cgroup_charge_hugetlb (folio, vma->vm_mm, gfp);/* * Unconditionally increment NR_HUGETLB here. If it turns out that * mem_cgroup_charge_hugetlb failed, then immediately free the page and
mm/memcontrol.c +4 -2
@@ -4768,6 +4768,7 @@ int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp)/** * mem_cgroup_charge_hugetlb - charge the memcg for a hugetlb folio * @folio: folio being charged +* @mm: mm context of the allocation target * @gfp: reclaim mode * * This function is called when allocating a huge page folio, after the page has
@@ -4777,9 +4778,10 @@ int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp)* Returns ENOMEM if the memcg is already full. * Returns 0 if either the charge was successful, or if we skip the charging. */ -int mem_cgroup_charge_hugetlb (struct folio *folio,gfp_t gfp) +int mem_cgroup_charge_hugetlb (struct folio *folio,struct mm_struct *mm, +gfp_t gfp) { -struct mem_cgroup *memcg =get_mem_cgroup_from_current (); +struct mem_cgroup *memcg =get_mem_cgroup_from_mm (mm);int ret =0 ;/*
Signature · 1 line
2.53.0