dmaengine.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] dmaengine: idxd: fix cdev locking for open and release
@ 2020-06-22 20:38 Dave Jiang
  2020-06-24  5:48 ` Vinod Koul
  0 siblings, 1 reply; 2+ messages in thread
From: Dave Jiang @ 2020-06-22 20:38 UTC (permalink / raw)
  To: vkoul; +Cc: Nikhil Rao, dmaengine

From: Nikhil Rao <nikhil.rao@intel.com>

add the wq lock in cdev open and release call. This fixes
race conditions observed in the open and close routines.

Fixes: 42d279f9137a ("dmaengine: idxd: add char driver to expose submission portal to userland")

Signed-off-by: Nikhil Rao <nikhil.rao@intel.com>
Signed-off-by: Dave Jiang <dave.jiang@intel.com>
---
v3:
- Caught uninit rc variable.

v2:
- Added kbuild fix from Dan Carpenter re:
smatch warnings:
drivers/dma/idxd/cdev.c:106 idxd_cdev_open() error: uninitialized symbol 'ctx'.

 drivers/dma/idxd/cdev.c |   19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/drivers/dma/idxd/cdev.c b/drivers/dma/idxd/cdev.c
index ff49847e37a8..cb376cf6a2d2 100644
--- a/drivers/dma/idxd/cdev.c
+++ b/drivers/dma/idxd/cdev.c
@@ -74,6 +74,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 	struct idxd_device *idxd;
 	struct idxd_wq *wq;
 	struct device *dev;
+	int rc = 0;
 
 	wq = inode_wq(inode);
 	idxd = wq->idxd;
@@ -81,17 +82,27 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 
 	dev_dbg(dev, "%s called: %d\n", __func__, idxd_wq_refcount(wq));
 
-	if (idxd_wq_refcount(wq) > 0 && wq_dedicated(wq))
-		return -EBUSY;
-
 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return -ENOMEM;
 
+	mutex_lock(&wq->wq_lock);
+
+	if (idxd_wq_refcount(wq) > 0 && wq_dedicated(wq)) {
+		rc = -EBUSY;
+		goto failed;
+	}
+
 	ctx->wq = wq;
 	filp->private_data = ctx;
 	idxd_wq_get(wq);
+	mutex_unlock(&wq->wq_lock);
 	return 0;
+
+ failed:
+	mutex_unlock(&wq->wq_lock);
+	kfree(ctx);
+	return rc;
 }
 
 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)
 	filep->private_data = NULL;
 
 	kfree(ctx);
+	mutex_lock(&wq->wq_lock);
 	idxd_wq_put(wq);
+	mutex_unlock(&wq->wq_lock);
 	return 0;
 }
 


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] dmaengine: idxd: fix cdev locking for open and release
  2020-06-22 20:38 [PATCH v3] dmaengine: idxd: fix cdev locking for open and release Dave Jiang
@ 2020-06-24  5:48 ` Vinod Koul
  0 siblings, 0 replies; 2+ messages in thread
From: Vinod Koul @ 2020-06-24  5:48 UTC (permalink / raw)
  To: Dave Jiang; +Cc: Nikhil Rao, dmaengine

On 22-06-20, 13:38, Dave Jiang wrote:
> From: Nikhil Rao <nikhil.rao@intel.com>
> 
> add the wq lock in cdev open and release call. This fixes
> race conditions observed in the open and close routines.
> 
> Fixes: 42d279f9137a ("dmaengine: idxd: add char driver to expose submission portal to userland")
> 

Please don't keep blank lines b/w Fixes and S-o-b lines!

> Signed-off-by: Nikhil Rao <nikhil.rao@intel.com>
> Signed-off-by: Dave Jiang <dave.jiang@intel.com>

Applied after fixing the blank line, thanks
-- 
~Vinod

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-06-24  5:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-22 20:38 [PATCH v3] dmaengine: idxd: fix cdev locking for open and release Dave Jiang
2020-06-24  5:48 ` Vinod Koul

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).