[PATCH v2 5/8] dmaengine: idxd: Fix error handling in idxd_cdev_open()

From: Vinicius Costa Gomes

Date: Wed Nov 25 2026 - 18:49:52 EST


Three error paths of idxd_cdev_open() are wrong:

- An ida_alloc() failure jumps to failed_ida without setting rc. The
open usually returns 0 after freeing its context, and the release of
the file then uses the freed filp->private_data.

- xa_insert() errors other than -EBUSY are ignored. The open succeeds
without the context in the xarray, and the completion records of its
page faults are dropped.

- On a dedicated WQ, the failed_ida path doesn't undo
idxd_wq_set_pasid(). The WQ keeps the PASID of an SVA bind that is
released right after.

Set rc on the ida_alloc() failure. Fail the open on xa_insert() errors
other than -EBUSY, and fix the "exist -> exists" typo in its warning.
Disable the PASID of a dedicated WQ on the failed_ida path, as
idxd_file_dev_release() does.

Fixes: e6fd6d7e5f0f ("dmaengine: idxd: add a device to represent the file opened")
Fixes: b022f59725f0 ("dmaengine: idxd: add idxd_copy_cr() to copy user completion record during page fault handling")
Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@xxxxxxxxx>
---
drivers/dma/idxd/cdev.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/dma/idxd/cdev.c b/drivers/dma/idxd/cdev.c
index f1af7f0d25d3..9343deb2e858 100644
--- a/drivers/dma/idxd/cdev.c
+++ b/drivers/dma/idxd/cdev.c
@@ -264,8 +264,12 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
mutex_lock(&wq->uc_lock);
rc = xa_insert(&wq->upasid_xa, pasid, ctx, GFP_KERNEL);
mutex_unlock(&wq->uc_lock);
- if (rc < 0)
- dev_warn(dev, "PASID entry already exist in xarray.\n");
+ if (rc == -EBUSY) {
+ /* Another open of this WQ by the same mm added it */
+ dev_warn(dev, "PASID entry already exists in xarray.\n");
+ } else if (rc < 0) {
+ goto failed_get_pasid;
+ }

if (wq_dedicated(wq)) {
rc = idxd_wq_set_pasid(wq, pasid);
@@ -279,6 +283,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
idxd_cdev = wq->idxd_cdev;
ctx->id = ida_alloc(&file_ida, GFP_KERNEL);
if (ctx->id < 0) {
+ rc = ctx->id;
dev_warn(dev, "ida alloc failure\n");
goto failed_ida;
}
@@ -311,6 +316,9 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
put_device(fdev);
return rc;
failed_ida:
+ if (device_user_pasid_enabled(idxd) && wq_dedicated(wq) &&
+ idxd_wq_disable_pasid(wq) < 0)
+ dev_err(dev, "wq disable pasid failed.\n");
failed_set_pasid:
if (device_user_pasid_enabled(idxd))
idxd_xa_pasid_remove(ctx);

--
2.56.1