linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sakari Ailus <sakari.ailus@linux.intel.com>
To: linux-media@vger.kernel.org
Cc: hverkuil@xs4all.nl
Subject: [PATCH v14 18/36] v4l2-ctrls: Lock the request for updating during S_EXT_CTRLS
Date: Mon, 21 May 2018 11:54:43 +0300	[thread overview]
Message-ID: <20180521085501.16861-19-sakari.ailus@linux.intel.com> (raw)
In-Reply-To: <20180521085501.16861-1-sakari.ailus@linux.intel.com>

Instead of checking the media request state is idle at one point, lock the
request for updating during the S_EXT_CTRLS call. As a by-product, finding
the request has been moved out of the v4l2_ctrls_find_req_obj() in order
to keep request object lookups to the minimum.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/v4l2-core/v4l2-ctrls.c | 67 +++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 27 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c
index df58a23eb731a..1a99d6e238791 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls.c
@@ -3208,9 +3208,8 @@ int __v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl,
 
 static struct media_request_object *
 v4l2_ctrls_find_req_obj(struct v4l2_ctrl_handler *hdl,
-			struct media_device *mdev, s32 fd, bool set)
+			struct media_request *req, bool set)
 {
-	struct media_request *req = media_request_get_by_fd(mdev, fd);
 	struct media_request_object *obj;
 	struct v4l2_ctrl_handler *new_hdl;
 	int ret;
@@ -3218,36 +3217,29 @@ v4l2_ctrls_find_req_obj(struct v4l2_ctrl_handler *hdl,
 	if (IS_ERR(req))
 		return ERR_CAST(req);
 
-	if (set && atomic_read(&req->state) != MEDIA_REQUEST_STATE_IDLE) {
-		media_request_put(req);
+	if (set && WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING))
 		return ERR_PTR(-EBUSY);
-	}
 
 	obj = media_request_object_find(req, &req_ops, hdl);
-	if (obj) {
-		media_request_put(req);
+	if (obj)
 		return obj;
-	}
 
 	new_hdl = kzalloc(sizeof(*new_hdl), GFP_KERNEL);
-	if (!new_hdl) {
-		ret = -ENOMEM;
-		goto put;
-	}
+	if (!new_hdl)
+		return ERR_PTR(-ENOMEM);
+
 	obj = &new_hdl->req_obj;
 	ret = v4l2_ctrl_handler_init(new_hdl, (hdl->nr_of_buckets - 1) * 8);
 	if (!ret)
 		ret = v4l2_ctrl_request_bind(req, new_hdl, hdl);
-	if (!ret) {
-		media_request_object_get(obj);
-		media_request_put(req);
-		return obj;
+	if (ret) {
+		kfree(new_hdl);
+
+		return ERR_PTR(ret);
 	}
-	kfree(new_hdl);
 
-put:
-	media_request_put(req);
-	return ERR_PTR(ret);
+	media_request_object_get(obj);
+	return obj;
 }
 
 int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct media_device *mdev,
@@ -3257,11 +3249,21 @@ int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct media_device *mdev,
 	int ret;
 
 	if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
+		struct media_request *req;
+
 		if (!mdev || cs->request_fd < 0)
 			return -EINVAL;
-		obj = v4l2_ctrls_find_req_obj(hdl, mdev, cs->request_fd, false);
+
+		req = media_request_get_by_fd(mdev, cs->request_fd);
+		if (!req)
+			return -ENOENT;
+
+		obj = v4l2_ctrls_find_req_obj(hdl, req, false);
+		/* Reference to the request held through obj */
+		media_request_put(req);
 		if (IS_ERR(obj))
 			return PTR_ERR(obj);
+
 		hdl = container_of(obj, struct v4l2_ctrl_handler,
 				   req_obj);
 	}
@@ -3567,18 +3569,28 @@ static int try_set_ext_ctrls(struct v4l2_fh *fh,
 			     struct v4l2_ext_controls *cs, bool set)
 {
 	struct media_request_object *obj = NULL;
+	struct media_request *req = NULL;
 	int ret;
 
 	if (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL) {
 		if (!mdev || cs->request_fd < 0)
 			return -EINVAL;
-		obj = v4l2_ctrls_find_req_obj(hdl, mdev, cs->request_fd, true);
+
+		req = media_request_get_by_fd(mdev, cs->request_fd);
+		if (!req)
+			return -ENOENT;
+
+		ret = media_request_lock_for_update(req);
+		if (req) {
+			media_request_put(req);
+			return ret;
+		}
+
+		obj = v4l2_ctrls_find_req_obj(hdl, req, true);
+		/* Reference to the request held through obj */
+		media_request_object_put(obj);
 		if (IS_ERR(obj))
 			return PTR_ERR(obj);
-		if (atomic_read(&obj->req->state) != MEDIA_REQUEST_STATE_IDLE) {
-			media_request_object_put(obj);
-			return -EBUSY;
-		}
 		hdl = container_of(obj, struct v4l2_ctrl_handler,
 				   req_obj);
 	}
@@ -3586,7 +3598,8 @@ static int try_set_ext_ctrls(struct v4l2_fh *fh,
 	ret = __try_set_ext_ctrls(fh, hdl, cs, set);
 
 	if (obj)
-		media_request_object_put(obj);
+		media_request_unlock_for_update(obj->req);
+
 	return ret;
 }
 
-- 
2.11.0

  parent reply	other threads:[~2018-05-21  8:55 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-21  8:54 [PATCH v14 00/36] Request API Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 01/36] uapi/linux/media.h: add request API Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 02/36] media-request: implement media requests Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 03/36] media-request: add media_request_get_by_fd Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 04/36] media-request: add media_request_object_find Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 05/36] media-request: Make request state an enum Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 06/36] media-request: Add support for updating request objects optimally Sakari Ailus
2018-05-21 10:29   ` [PATCH v14.1 " Sakari Ailus
2018-05-21 11:33     ` [PATCH v14.2 " Sakari Ailus
2018-05-23  9:50   ` [PATCH " Hans Verkuil
2018-05-21  8:54 ` [PATCH v14 07/36] media-request: Add a sanity check for the media request state Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 08/36] media: doc: Add media-request.h header to documentation build Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 09/36] v4l2-dev: lock req_queue_mutex Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 10/36] videodev2.h: add request_fd field to v4l2_ext_controls Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 11/36] v4l2-ctrls: v4l2_ctrl_add_handler: add from_other_dev Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 12/36] v4l2-ctrls: prepare internal structs for request API Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 13/36] v4l2-ctrls: alloc memory for p_req Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 14/36] v4l2-ctrls: use ref in helper instead of ctrl Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 15/36] v4l2-ctrls: add core request support Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 16/36] v4l2-ctrls: Add documentation for control request support functions Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 17/36] v4l2-ctrls: support g/s_ext_ctrls for requests Sakari Ailus
2018-05-21  8:54 ` Sakari Ailus [this message]
2018-05-21  8:54 ` [PATCH v14 19/36] videodev2.h: Add request_fd field to v4l2_buffer Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 20/36] vb2: store userspace data in vb2_v4l2_buffer Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 21/36] videobuf2-core: embed media_request_object Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 22/36] videobuf2-core: integrate with media requests Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 23/36] videobuf2-v4l2: " Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 24/36] videobuf2-v4l2: Lock the media request for update for QBUF Sakari Ailus
2018-05-23 10:17   ` Hans Verkuil
2018-05-21  8:54 ` [PATCH v14 25/36] videobuf2-core: Make request state an enum Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 26/36] videobuf2-core: add request helper functions Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 27/36] videobuf2-v4l2: add vb2_request_queue/validate helpers Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 28/36] v4l2-mem2mem: add vb2_m2m_request_queue Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 29/36] Documentation: v4l: document request API Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 30/36] media: vim2m: add media device Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 31/36] vim2m: use workqueue Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 32/36] vim2m: support requests Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 33/36] vivid: add mc Sakari Ailus
2018-05-21  8:54 ` [PATCH v14 34/36] vivid: add request support Sakari Ailus
2018-05-21  8:55 ` [PATCH v14 35/36] RFC: media-requests: add debugfs node Sakari Ailus
2018-05-21  8:55 ` [PATCH v14 36/36] v4l: m2m: Simplify exiting the function in v4l2_m2m_try_schedule Sakari Ailus

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=20180521085501.16861-19-sakari.ailus@linux.intel.com \
    --to=sakari.ailus@linux.intel.com \
    --cc=hverkuil@xs4all.nl \
    --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 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).