[PATCH v2 05/10] dmaengine: idxd: take an mm_users reference for the completion record copy

From: Daniel Byrne

Date: Mon Nov 30 2026 - 14:54:07 EST


The user context records the opener's mm without a users reference. On
exit, exit_mm() runs before the files are closed, so the context's
release, which removes it from the work queue's PASID xarray, runs after
the address space is torn down, and the drain the release issues
completes the context's in-flight descriptors. A completion record write
of one of them faults, and the event log fault work finds the context in
the xarray and calls kthread_use_mm() on an mm whose users count is zero
to copy the record into it. The mm_struct itself is kept by the SVA
binding, but an address space in that state must not be used. The copy
normally fails, and when it races with exit_mmap() it can install page
table entries that free_pgtables() abandons.

Take a users reference for the copy with mmget_not_zero(), and skip the
copy when the address space is gone, which counts as a failed copy.
Remove the context from the xarray before unbinding the SVA handle, so
that the mm_struct is alive whenever the fault work can find the
context.

Fixes: b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")
Cc: stable@xxxxxxxxxxxxxxx # 6.4+
Signed-off-by: Daniel Byrne <daniel1.byrne@xxxxxxxxx>
---
New in v2.

drivers/dma/idxd/cdev.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/dma/idxd/cdev.c b/drivers/dma/idxd/cdev.c
index 50cd2e3f342d..542d0b275a71 100644
--- a/drivers/dma/idxd/cdev.c
+++ b/drivers/dma/idxd/cdev.c
@@ -6,6 +6,7 @@
#include <linux/pci.h>
#include <linux/device.h>
#include <linux/sched/task.h>
+#include <linux/sched/mm.h>
#include <linux/io-64-nonatomic-lo-hi.h>
#include <linux/cdev.h>
#include <linux/fs.h>
@@ -140,8 +141,13 @@ static void idxd_file_dev_release(struct device *dev)

if (ctx->sva) {
idxd_cdev_evl_drain_pasid(wq, ctx->pasid);
- iommu_sva_unbind_device(ctx->sva);
+ /*
+ * Once the context is no longer in the xarray no fault work
+ * can find it. The SVA binding keeps its mm_struct alive
+ * until then.
+ */
idxd_xa_pasid_remove(ctx);
+ iommu_sva_unbind_device(ctx->sva);
}
kfree(ctx);
mutex_lock(&wq->wq_lock);
@@ -774,7 +780,16 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr,
goto out;
}

+ /*
+ * The context records the opener's mm without a users reference of
+ * its own. The file's release, which removes the context from the
+ * xarray, runs after exit_mm() when the opener exits. Take one for
+ * the copy, and skip it if the address space is already gone.
+ */
mm = ctx->mm;
+ if (!mmget_not_zero(mm))
+ goto out;
+
/*
* The completion record fault handling work is running in kernel
* thread context. It temporarily switches to the mm to copy cr
@@ -805,6 +820,7 @@ int idxd_copy_cr(struct idxd_wq *wq, ioasid_t pasid, unsigned long addr,
left += status_size;
}
kthread_unuse_mm(mm);
+ mmput(mm);

out:
mutex_unlock(&wq->uc_lock);
--
2.43.0