iommu/sva: Charge IOPF allocations to the faulting mm's memcg
Subsystems (3)
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.
---
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
@@ -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
Quoted 8 lines · Peizhe Liu
iommu_sva_handle_mm()runs from the IOPF workqueue and callshandle_mm_fault()for the faulting mm, but does not set the active memcg. File-backed faults can allocate page cache throughmem_cgroup_charge(folio, NULL, gfp), which falls back to the root memcg when the worker has neither an active memcg nor acurrent->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")Signed-off-by Peizhe Liu <[email protected]>
This looks fine from memcg perspective.
Quoted 12 lines · Peizhe Liu
iommu_sva_handle_mm()runs from the IOPF workqueue and callshandle_mm_fault()for the faulting mm, but does not set the active memcg. File-backed faults can allocate page cache throughmem_cgroup_charge(folio, NULL, gfp), which falls back to the root memcg when the worker has neither an active memcg nor acurrent->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")Signed-off-by Peizhe Liu <[email protected]>
Thanks.
Quoted 13 lines · Peizhe Liu
iommu_sva_handle_mm()runs from the IOPF workqueue and callshandle_mm_fault()for the faulting mm, but does not set the active memcg. File-backed faults can allocate page cache throughmem_cgroup_charge(folio, NULL, gfp), which falls back to the root memcg when the worker has neither an active memcg nor acurrent->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 <[email protected]>
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
Quoted 13 lines · Peizhe Liu
iommu_sva_handle_mm()runs from the IOPF workqueue and callshandle_mm_fault()for the faulting mm, but does not set the active memcg. File-backed faults can allocate page cache throughmem_cgroup_charge(folio, NULL, gfp), which falls back to the root memcg when the worker has neither an active memcg nor acurrent->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<[email protected]>
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(+)
Quoted 2 lines · Peizhe Liu
drivers/iommu/iommu-sva.c | 6 ++++++ 1 file changed, 6 insertions(+)
Applied, thanks.