From: Daniel Byrne <daniel1.byrne@intel.com>
To: Vinod Koul <vkoul@kernel.org>, dmaengine@vger.kernel.org
Cc: Vinicius Costa Gomes <vinicius.gomes@intel.com>,
Dave Jiang <dave.jiang@intel.com>, Frank Li <Frank.Li@kernel.org>,
Fenghua Yu <fenghuay@nvidia.com>, Dan Williams <djbw@kernel.org>,
linux-kernel@vger.kernel.org,
Daniel Byrne <daniel1.byrne@intel.com>,
stable@vger.kernel.org
Subject: [PATCH v2 05/10] dmaengine: idxd: take an mm_users reference for the completion record copy
Date: Mon, 30 Nov 2026 11:47:16 -0800 [thread overview]
Message-ID: <20261130194721.1900266-6-daniel1.byrne@intel.com> (raw)
In-Reply-To: <20261130194721.1900266-1-daniel1.byrne@intel.com>
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@vger.kernel.org # 6.4+
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
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
next prev parent reply other threads:[~2026-11-30 19:53 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 19:47 [PATCH v2 00/10] dmaengine: idxd: event log, cdev and prep fixes Daniel Byrne
2026-11-30 19:47 ` [PATCH v2 01/10] dmaengine: idxd: don't queue EVL fault work for kernel WQs Daniel Byrne
2026-11-30 19:47 ` [PATCH v2 02/10] dmaengine: idxd: detach the user fault workqueue under the event log lock Daniel Byrne
2026-11-30 19:47 ` [PATCH v2 03/10] dmaengine: idxd: free the event log fault item when no record is copied Daniel Byrne
2026-11-30 19:47 ` [PATCH v2 04/10] dmaengine: idxd: refuse a memcpy longer than the WQ max transfer size Daniel Byrne
2026-11-30 19:47 ` Daniel Byrne [this message]
2026-11-30 19:47 ` [PATCH v2 06/10] dmaengine: idxd: read GENCFG before clearing the event log enable Daniel Byrne
2026-11-30 19:47 ` [PATCH v2 07/10] dmaengine: idxd: don't sleep for a descriptor in the prep callbacks Daniel Byrne
2026-11-30 19:47 ` [PATCH v2 08/10] dmaengine: idxd: read the event log size under its lock in the setup Daniel Byrne
2026-11-30 19:47 ` [PATCH v2 09/10] dmaengine: idxd: free the queue interrupt when the percpu_ref setup fails Daniel Byrne
2026-11-30 19:47 ` [PATCH v2 10/10] dmaengine: idxd: test a queue under its lock before quiescing it Daniel Byrne
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130194721.1900266-6-daniel1.byrne@intel.com \
--to=daniel1.byrne@intel.com \
--cc=Frank.Li@kernel.org \
--cc=dave.jiang@intel.com \
--cc=djbw@kernel.org \
--cc=dmaengine@vger.kernel.org \
--cc=fenghuay@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=vinicius.gomes@intel.com \
--cc=vkoul@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox