Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iommu/sva: Charge IOPF allocations to the faulting mm's memcg
@ 2026-10-12 20:08 Peizhe Liu
  2026-10-12 23:07 ` Shakeel Butt
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Peizhe Liu @ 2026-10-12 20:08 UTC (permalink / raw)
  To: Joerg Roedel, Will Deacon
  Cc: Robin Murphy, Lu Baolu, Jason Gunthorpe, Kevin Tian,
	Jean-Philippe Brucker, Johannes Weiner, Michal Hocko,
	Roman Gushchin, Shakeel Butt, Muchun Song, iommu, linux-mm,
	cgroups, linux-kernel

iommu_sva_handle_mm() runs from the IOPF workqueue and calls
handle_mm_fault() for the faulting mm, but does not set the active
memcg. File-backed faults can allocate page cache through
mem_cgroup_charge(folio, NULL, gfp), which falls back to the root memcg
when the worker has neither an active memcg nor a current->mm. Page
cache faulted in by a device is therefore charged to root and escapes
the task's memory.max.

Set the active memcg to the one associated with the faulting mm while
handling the fault, and restore the previous value afterwards.

Fixes: fc36479db74e ("iommu: Add a page fault handler")
Signed-off-by: Peizhe Liu <peizheliu@utexas.edu>
---
Tested on a Xeon Gold 5512U with Intel DSA, based on iommu/next.
Before/after testing confirmed that the block-on-fault case charges
the page cache to the task's memcg after this change and is subject
to memory.max.

 drivers/iommu/iommu-sva.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c
index 9827cb804363..9a825fac61a5 100644
--- a/drivers/iommu/iommu-sva.c
+++ b/drivers/iommu/iommu-sva.c
@@ -2,6 +2,7 @@
 /*
  * Helpers for IOMMU drivers implementing SVA
  */
+#include <linux/memcontrol.h>
 #include <linux/mmu_context.h>
 #include <linux/mmu_notifier.h>
 #include <linux/mutex.h>
@@ -220,6 +221,7 @@ iommu_sva_handle_mm(struct iommu_fault *fault, struct mm_struct *mm)
 	unsigned int access_flags = 0;
 	unsigned int fault_flags = FAULT_FLAG_REMOTE;
 	struct iommu_fault_page_request *prm = &fault->prm;
+	struct mem_cgroup *memcg, *old_memcg;
 	enum iommu_page_response_code status = IOMMU_PAGE_RESP_INVALID;
 
 	if (!(prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID))
@@ -255,7 +257,11 @@ iommu_sva_handle_mm(struct iommu_fault *fault, struct mm_struct *mm)
 		/* Access fault */
 		goto out_put_mm;
 
+	memcg = get_mem_cgroup_from_mm(mm);
+	old_memcg = set_active_memcg(memcg);
 	ret = handle_mm_fault(vma, prm->addr, fault_flags, NULL);
+	set_active_memcg(old_memcg);
+	mem_cgroup_put(memcg);
 	status = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID :
 		IOMMU_PAGE_RESP_SUCCESS;
 

base-commit: c233d59828627bfaab750ec5546e81970475f951
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] iommu/sva: Charge IOPF allocations to the faulting mm's memcg
  2026-10-12 20:08 [PATCH] iommu/sva: Charge IOPF allocations to the faulting mm's memcg Peizhe Liu
@ 2026-10-12 23:07 ` Shakeel Butt
  2026-10-13  2:23 ` Muchun Song
  2026-10-13 12:22 ` Jason Gunthorpe
  2 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-10-12 23:07 UTC (permalink / raw)
  To: Peizhe Liu
  Cc: Joerg Roedel, Will Deacon, Robin Murphy, Lu Baolu,
	Jason Gunthorpe, Kevin Tian, Jean-Philippe Brucker,
	Johannes Weiner, Michal Hocko, Roman Gushchin, Muchun Song, iommu,
	linux-mm, cgroups, linux-kernel

On Mon, Oct 12, 2026 at 03:08:23PM -0500, Peizhe Liu wrote:
> iommu_sva_handle_mm() runs from the IOPF workqueue and calls
> handle_mm_fault() for the faulting mm, but does not set the active
> memcg. File-backed faults can allocate page cache through
> mem_cgroup_charge(folio, NULL, gfp), which falls back to the root memcg
> when the worker has neither an active memcg nor a current->mm. Page
> cache faulted in by a device is therefore charged to root and escapes
> the task's memory.max.
> 
> Set the active memcg to the one associated with the faulting mm while
> handling the fault, and restore the previous value afterwards.
> 
> Fixes: fc36479db74e ("iommu: Add a page fault handler")
> Signed-off-by: Peizhe Liu <peizheliu@utexas.edu>

This looks fine from memcg perspective.

Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] iommu/sva: Charge IOPF allocations to the faulting mm's memcg
  2026-10-12 20:08 [PATCH] iommu/sva: Charge IOPF allocations to the faulting mm's memcg Peizhe Liu
  2026-10-12 23:07 ` Shakeel Butt
@ 2026-10-13  2:23 ` Muchun Song
  2026-10-13 12:22 ` Jason Gunthorpe
  2 siblings, 0 replies; 4+ messages in thread
From: Muchun Song @ 2026-10-13  2:23 UTC (permalink / raw)
  To: Peizhe Liu
  Cc: Joerg Roedel, Will Deacon, Robin Murphy, Lu Baolu,
	Jason Gunthorpe, Kevin Tian, Jean-Philippe Brucker,
	Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
	iommu, linux-mm, cgroups, linux-kernel



> On Oct 13, 2026, at 04:08, Peizhe Liu <peizheliu@utexas.edu> wrote:
> 
> iommu_sva_handle_mm() runs from the IOPF workqueue and calls
> handle_mm_fault() for the faulting mm, but does not set the active
> memcg. File-backed faults can allocate page cache through
> mem_cgroup_charge(folio, NULL, gfp), which falls back to the root memcg
> when the worker has neither an active memcg nor a current->mm. Page
> cache faulted in by a device is therefore charged to root and escapes
> the task's memory.max.
> 
> Set the active memcg to the one associated with the faulting mm while
> handling the fault, and restore the previous value afterwards.
> 
> Fixes: fc36479db74e ("iommu: Add a page fault handler")
> Signed-off-by: Peizhe Liu <peizheliu@utexas.edu>

Acked-by: Muchun Song <muchun.song@linux.dev>

Thanks.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] iommu/sva: Charge IOPF allocations to the faulting mm's memcg
  2026-10-12 20:08 [PATCH] iommu/sva: Charge IOPF allocations to the faulting mm's memcg Peizhe Liu
  2026-10-12 23:07 ` Shakeel Butt
  2026-10-13  2:23 ` Muchun Song
@ 2026-10-13 12:22 ` Jason Gunthorpe
  2 siblings, 0 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2026-10-13 12:22 UTC (permalink / raw)
  To: Peizhe Liu
  Cc: Joerg Roedel, Will Deacon, Robin Murphy, Lu Baolu, Kevin Tian,
	Jean-Philippe Brucker, Johannes Weiner, Michal Hocko,
	Roman Gushchin, Shakeel Butt, Muchun Song, iommu, linux-mm,
	cgroups, linux-kernel

On Mon, Oct 12, 2026 at 03:08:23PM -0500, Peizhe Liu wrote:
> iommu_sva_handle_mm() runs from the IOPF workqueue and calls
> handle_mm_fault() for the faulting mm, but does not set the active
> memcg. File-backed faults can allocate page cache through
> mem_cgroup_charge(folio, NULL, gfp), which falls back to the root memcg
> when the worker has neither an active memcg nor a current->mm. Page
> cache faulted in by a device is therefore charged to root and escapes
> the task's memory.max.
> 
> Set the active memcg to the one associated with the faulting mm while
> handling the fault, and restore the previous value afterwards.
> 
> Fixes: fc36479db74e ("iommu: Add a page fault handler")
> Signed-off-by: Peizhe Liu <peizheliu@utexas.edu>
> ---
> Tested on a Xeon Gold 5512U with Intel DSA, based on iommu/next.
> Before/after testing confirmed that the block-on-fault case charges
> the page cache to the task's memcg after this change and is subject
> to memory.max.
> 
>  drivers/iommu/iommu-sva.c | 6 ++++++
>  1 file changed, 6 insertions(+)

It makes sense to me

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Jason


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-13 12:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-12 20:08 [PATCH] iommu/sva: Charge IOPF allocations to the faulting mm's memcg Peizhe Liu
2026-10-12 23:07 ` Shakeel Butt
2026-10-13  2:23 ` Muchun Song
2026-10-13 12:22 ` Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox