From: Hans Verkuil <hans.verk...@cisco.com>

Add proper locking to the file operations, allowing for the removal
of the V4L2_FL_LOCK_ALL_FOPS flag.

Signed-off-by: Hans Verkuil <hans.verk...@cisco.com>
---
 drivers/media/video/mem2mem_testdev.c |   29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/drivers/media/video/mem2mem_testdev.c 
b/drivers/media/video/mem2mem_testdev.c
index d2dec58..595e268 100644
--- a/drivers/media/video/mem2mem_testdev.c
+++ b/drivers/media/video/mem2mem_testdev.c
@@ -849,10 +849,15 @@ static int m2mtest_open(struct file *file)
 {
        struct m2mtest_dev *dev = video_drvdata(file);
        struct m2mtest_ctx *ctx = NULL;
+       int rc = 0;
 
+       if (mutex_lock_interruptible(&dev->dev_mutex))
+               return -ERESTARTSYS;
        ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
-       if (!ctx)
-               return -ENOMEM;
+       if (!ctx) {
+               rc = -ENOMEM;
+               goto open_unlock;
+       }
 
        file->private_data = ctx;
        ctx->dev = dev;
@@ -863,16 +868,18 @@ static int m2mtest_open(struct file *file)
        ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
 
        if (IS_ERR(ctx->m2m_ctx)) {
-               int ret = PTR_ERR(ctx->m2m_ctx);
+               rc = PTR_ERR(ctx->m2m_ctx);
 
                kfree(ctx);
-               return ret;
+               goto open_unlock;
        }
 
        atomic_inc(&dev->num_inst);
 
        dprintk(dev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
 
+open_unlock:
+       mutex_unlock(&dev->dev_mutex);
        return 0;
 }
 
@@ -883,7 +890,9 @@ static int m2mtest_release(struct file *file)
 
        dprintk(dev, "Releasing instance %p\n", ctx);
 
+       mutex_lock(&dev->dev_mutex);
        v4l2_m2m_ctx_release(ctx->m2m_ctx);
+       mutex_unlock(&dev->dev_mutex);
        kfree(ctx);
 
        atomic_dec(&dev->num_inst);
@@ -901,9 +910,15 @@ static unsigned int m2mtest_poll(struct file *file,
 
 static int m2mtest_mmap(struct file *file, struct vm_area_struct *vma)
 {
+       struct m2mtest_dev *dev = video_drvdata(file);
        struct m2mtest_ctx *ctx = file->private_data;
+       int res;
 
-       return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
+       if (mutex_lock_interruptible(&dev->dev_mutex))
+               return -ERESTARTSYS;
+       res = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
+       mutex_unlock(&dev->dev_mutex);
+       return res;
 }
 
 static const struct v4l2_file_operations m2mtest_fops = {
@@ -958,10 +973,6 @@ static int m2mtest_probe(struct platform_device *pdev)
        }
 
        *vfd = m2mtest_videodev;
-       /* Locking in file operations other than ioctl should be done
-          by the driver, not the V4L2 core.
-          This driver needs auditing so that this flag can be removed. */
-       set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
        vfd->lock = &dev->dev_mutex;
 
        ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
-- 
1.7.10

--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to