Skip to contentSkip to conversation

iommu/sva: Charge IOPF allocations to the faulting mm's memcg

PATCH Applied Peizhe Liu 6 participants 6 messages Oct 12 – Nov 23

Subsystems (3)
lore
Share & tools
lore

Peizhe Liu

author
#0 lore raw

PATCH 1/1 iommu/sva: Charge IOPF allocations to the faulting mm's memcg

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")

---
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(+)
drivers/iommu/iommu-sva.c +6 -0
index 9827cb804363..9a825fac61a5 100644
@@ -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

Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Peizhe Liu

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.

Quoted 5 lines

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")

This looks fine from memcg perspective.

Acked-by on v1 1/1
#2 lore raw
In reply to Peizhe Liu
Quoted 12 lines · Peizhe Liu

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.

Quoted 5 lines

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")

Thanks.

Reviewed-by on v1 1/1
#3 lore raw
In reply to Peizhe Liu
Quoted 13 lines · Peizhe Liu

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")
Quoted 8 lines

---
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

Jason

Reviewed-by on v1 1/1
#4 lore raw
In reply to Peizhe Liu
Quoted 13 lines · Peizhe Liu

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")
Quoted 8 lines

---
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(+)