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 --- 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 #include #include #include @@ -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