All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hans Verkuil <hverkuil@xs4all.nl>
To: linux-media@vger.kernel.org
Cc: Hans Verkuil <hans.verkuil@cisco.com>
Subject: [RFCv12 PATCH 04/29] v4l2-dev: lock req_queue_mutex
Date: Tue,  1 May 2018 11:00:26 +0200	[thread overview]
Message-ID: <20180501090051.9321-5-hverkuil@xs4all.nl> (raw)
In-Reply-To: <20180501090051.9321-1-hverkuil@xs4all.nl>

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

Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
---
 drivers/media/v4l2-core/v4l2-dev.c | 37 +++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)

diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c
index 1d0b2208e8fb..3368bd5537a7 100644
--- a/drivers/media/v4l2-core/v4l2-dev.c
+++ b/drivers/media/v4l2-core/v4l2-dev.c
@@ -353,13 +353,36 @@ static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 
 	if (vdev->fops->unlocked_ioctl) {
 		struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd);
+		struct mutex *queue_lock = NULL;
 
-		if (lock && mutex_lock_interruptible(lock))
+		/*
+		 * We need to serialize streamon/off with queuing new requests.
+		 * These ioctls may trigger the cancellation of a streaming
+		 * operation, and that should not be mixed with queuing a new
+		 * request at the same time.
+		 *
+		 * Also TRY/S_EXT_CTRLS needs this lock to correctly serialize
+		 * with MEDIA_REQUEST_IOC_QUEUE.
+		 */
+		if (vdev->v4l2_dev->mdev &&
+		    (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF ||
+		     cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_TRY_EXT_CTRLS))
+			queue_lock = &vdev->v4l2_dev->mdev->req_queue_mutex;
+
+		if (queue_lock && mutex_lock_interruptible(queue_lock))
+			return -ERESTARTSYS;
+
+		if (lock && mutex_lock_interruptible(lock)) {
+			if (queue_lock)
+				mutex_unlock(queue_lock);
 			return -ERESTARTSYS;
+		}
 		if (video_is_registered(vdev))
 			ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
 		if (lock)
 			mutex_unlock(lock);
+		if (queue_lock)
+			mutex_unlock(queue_lock);
 	} else
 		ret = -ENOTTY;
 
@@ -442,8 +465,20 @@ static int v4l2_release(struct inode *inode, struct file *filp)
 	struct video_device *vdev = video_devdata(filp);
 	int ret = 0;
 
+	/*
+	 * We need to serialize the release() with queuing new requests.
+	 * The release() may trigger the cancellation of a streaming
+	 * operation, and that should not be mixed with queuing a new
+	 * request at the same time.
+	 */
+	if (vdev->v4l2_dev->mdev)
+		mutex_lock(&vdev->v4l2_dev->mdev->req_queue_mutex);
+
 	if (vdev->fops->release)
 		ret = vdev->fops->release(filp);
+
+	if (vdev->v4l2_dev->mdev)
+		mutex_unlock(&vdev->v4l2_dev->mdev->req_queue_mutex);
 	if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
 		printk(KERN_DEBUG "%s: release\n",
 			video_device_node_name(vdev));
-- 
2.17.0

  parent reply	other threads:[~2018-05-01  9:00 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-01  9:00 [RFCv12 PATCH 00/29] Request API Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 01/29] v4l2-device.h: always expose mdev Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 02/29] uapi/linux/media.h: add request API Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 03/29] media-request: implement media requests Hans Verkuil
2018-05-02 12:24   ` Hans Verkuil
2018-05-02 22:24   ` Sakari Ailus
2018-05-03  8:21     ` Hans Verkuil
2018-05-03 10:12       ` Sakari Ailus
2018-05-03 10:29         ` Hans Verkuil
2018-05-01  9:00 ` Hans Verkuil [this message]
2018-05-02  8:10   ` [RFCv12 PATCH 04/29] v4l2-dev: lock req_queue_mutex Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 05/29] media-request: add media_request_find Hans Verkuil
2018-05-02 22:06   ` Sakari Ailus
2018-05-03  8:31     ` Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 06/29] media-request: add media_request_object_find Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 07/29] videodev2.h: add request_fd field to v4l2_ext_controls Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 08/29] v4l2-ctrls: v4l2_ctrl_add_handler: add from_other_dev Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 09/29] v4l2-ctrls: prepare internal structs for request API Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 10/29] v4l2-ctrls: alloc memory for p_req Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 11/29] v4l2-ctrls: use ref in helper instead of ctrl Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 12/29] v4l2-ctrls: add core request support Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 13/29] v4l2-ctrls: support g/s_ext_ctrls for requests Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 14/29] videodev2.h: Add request_fd field to v4l2_buffer Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 15/29] vb2: store userspace data in vb2_v4l2_buffer Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 16/29] videobuf2-core: embed media_request_object Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 17/29] videobuf2-core: integrate with media requests Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 18/29] videobuf2-v4l2: " Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 19/29] videobuf2-core: add vb2_core_request_has_buffers Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 20/29] videobuf2-v4l2: add vb2_request_queue/validate helpers Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 21/29] videobuf2-v4l2: export request_fd Hans Verkuil
2018-05-02  8:14   ` Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 22/29] v4l2-mem2mem: add vb2_m2m_request_queue Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 23/29] Documentation: v4l: document request API Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 24/29] media: vim2m: add media device Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 25/29] vim2m: use workqueue Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 26/29] vim2m: support requests Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 27/29] vivid: add mc Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 28/29] vivid: add request support Hans Verkuil
2018-05-01  9:00 ` [RFCv12 PATCH 29/29] RFC: media-requests: add debugfs node Hans Verkuil

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=20180501090051.9321-5-hverkuil@xs4all.nl \
    --to=hverkuil@xs4all.nl \
    --cc=hans.verkuil@cisco.com \
    --cc=linux-media@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.