Skip to content

Commit 66983bc

Browse files
nikhilpraovinodkoul
authored andcommitted
dmaengine: idxd: fix cdev locking for open and release
add the wq lock in cdev open and release call. This fixes race conditions observed in the open and close routines. Fixes: 42d279f ("dmaengine: idxd: add char driver to expose submission portal to userland") Signed-off-by: Nikhil Rao <[email protected]> Signed-off-by: Dave Jiang <[email protected]> Link: https://lore.kernel.org/r/159285824892.64944.2905413694915141834.stgit@djiang5-desk3.ch.intel.com Signed-off-by: Vinod Koul <[email protected]>
1 parent 2f57b8d commit 66983bc

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

drivers/dma/idxd/cdev.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,35 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
7474
struct idxd_device *idxd;
7575
struct idxd_wq *wq;
7676
struct device *dev;
77+
int rc = 0;
7778

7879
wq = inode_wq(inode);
7980
idxd = wq->idxd;
8081
dev = &idxd->pdev->dev;
8182

8283
dev_dbg(dev, "%s called: %d\n", __func__, idxd_wq_refcount(wq));
8384

84-
if (idxd_wq_refcount(wq) > 0 && wq_dedicated(wq))
85-
return -EBUSY;
86-
8785
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8886
if (!ctx)
8987
return -ENOMEM;
9088

89+
mutex_lock(&wq->wq_lock);
90+
91+
if (idxd_wq_refcount(wq) > 0 && wq_dedicated(wq)) {
92+
rc = -EBUSY;
93+
goto failed;
94+
}
95+
9196
ctx->wq = wq;
9297
filp->private_data = ctx;
9398
idxd_wq_get(wq);
99+
mutex_unlock(&wq->wq_lock);
94100
return 0;
101+
102+
failed:
103+
mutex_unlock(&wq->wq_lock);
104+
kfree(ctx);
105+
return rc;
95106
}
96107

97108
static int idxd_cdev_release(struct inode *node, struct file *filep)
@@ -105,7 +116,9 @@ static int idxd_cdev_release(struct inode *node, struct file *filep)
105116
filep->private_data = NULL;
106117

107118
kfree(ctx);
119+
mutex_lock(&wq->wq_lock);
108120
idxd_wq_put(wq);
121+
mutex_unlock(&wq->wq_lock);
109122
return 0;
110123
}
111124

0 commit comments

Comments
 (0)