Received: by 10.223.176.5 with SMTP id f5csp1291742wra; Wed, 31 Jan 2018 04:20:39 -0800 (PST) X-Google-Smtp-Source: AH8x2264v7u63ImniheUi+lF1VB684PQvoK7JCkJphY/L0qV4FHkKwCUch3mAR0U9YMYq+0ia1WG X-Received: by 2002:a17:902:323:: with SMTP id 32-v6mr29538167pld.169.1517401239391; Wed, 31 Jan 2018 04:20:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517401239; cv=none; d=google.com; s=arc-20160816; b=MWrpgesFzq5rbV6idTH10YN2seXfyQr2TRoHQIj3mhkRan1AJPAkE91mKP3hmHUs7w od1l1k+riCboREsFyJLdpjsEQDsYl4mm8BBv1JFnEjvGZzafpN9gLISO45x/XUiaQvH5 tVqI6qtwkdMiXN2UpXCQz5uVtUuxPFsNFmMFRXYpzRxeRPfvi8Cd2LBrlHTVK/dIEJDa 3mVJRXHTzOhUT3D07SxZOxHAgrfKEbQ2W4LyJ6kIJDDQBKcIxVxX4dkbwGHAQA5LzNZQ XguF0bz4THYF83b/INC/GWkHzsLdrER24Df/LNHApuoHbsir4Q1fXRhgi3kSzfMPgIlJ zcwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=vi8cdStYD+gWyFRRiQwlb05lsh8vWk3DGV5e9RhUIpc=; b=Q0hbkIpZ3GIUnFaOIUDiNmHbnitmOtKQm+yEXdh7Aos7FVJNcz64SIjmVnpdocgKEi gLk5XgZt9Ihd/EKldUVWv6syWznOCzqC1qSKIOYKxqiYRvbfBfPGJsbGtTPMfyuS9piB u01+6hOFxCkvRq/b/JQ0wSLR1uyWxd16vsb1XSKRSyaKpCKy64uqSgPEGCsa6t8SUgxL lMCuYZ34aRZYoNCd0CH+evFSUs1e+aXOTY8PEB7zApyHl80t9miOJuGkjh3qR75yA9dx Sp6NkAURFlbjyq/ot5xa5XGhwfGYtMa6cTtXwPqPC3Dw4k4o9Ny1EgX1JiZAnwoRDhQe T7LA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bc11-v6si3887420plb.688.2018.01.31.04.20.24; Wed, 31 Jan 2018 04:20:39 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752401AbeAaMTN (ORCPT + 99 others); Wed, 31 Jan 2018 07:19:13 -0500 Received: from mga12.intel.com ([192.55.52.136]:1235 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751025AbeAaMTM (ORCPT ); Wed, 31 Jan 2018 07:19:12 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 Jan 2018 04:19:10 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,440,1511856000"; d="scan'208";a="26181517" Received: from paasikivi.fi.intel.com ([10.237.72.42]) by fmsmga004.fm.intel.com with ESMTP; 31 Jan 2018 04:19:07 -0800 Received: by paasikivi.fi.intel.com (Postfix, from userid 1000) id E25F920814; Wed, 31 Jan 2018 14:19:06 +0200 (EET) Date: Wed, 31 Jan 2018 14:19:06 +0200 From: Sakari Ailus To: Alexandre Courbot Cc: Mauro Carvalho Chehab , Hans Verkuil , Laurent Pinchart , Pawel Osciak , Marek Szyprowski , Tomasz Figa , Gustavo Padovan , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH 3/8] media: videobuf2: add support for requests Message-ID: <20180131121906.h25fll5whpk3cxsb@paasikivi.fi.intel.com> References: <20180126060216.147918-1-acourbot@chromium.org> <20180126060216.147918-4-acourbot@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180126060216.147918-4-acourbot@chromium.org> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Alexandre, On Fri, Jan 26, 2018 at 03:02:11PM +0900, Alexandre Courbot wrote: > Make vb2 aware of requests. Drivers can specify whether a given queue > can accept requests or not. Queues that accept requests will block on a > buffer that is part of a request until that request is submitted. > > Signed-off-by: Alexandre Courbot > --- > drivers/media/v4l2-core/videobuf2-core.c | 125 +++++++++++++++++++++++++++++-- > drivers/media/v4l2-core/videobuf2-v4l2.c | 28 ++++++- > include/media/videobuf2-core.h | 15 +++- > 3 files changed, 160 insertions(+), 8 deletions(-) > > diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c > index cb115ba6a1d2..f6d013b141f1 100644 > --- a/drivers/media/v4l2-core/videobuf2-core.c > +++ b/drivers/media/v4l2-core/videobuf2-core.c > @@ -26,6 +26,7 @@ > > #include > #include > +#include > > #include > > @@ -922,6 +923,17 @@ void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state) > vb->state = state; > } > atomic_dec(&q->owned_by_drv_count); > + if (vb->request) { > + struct media_request *req = vb->request; > + > + if (atomic_dec_and_test(&req->buf_cpt)) > + media_request_complete(vb->request); > + > + /* release reference acquired during qbuf */ > + vb->request = NULL; > + media_request_put(req); > + } > + > spin_unlock_irqrestore(&q->done_lock, flags); > > trace_vb2_buf_done(q, vb); > @@ -1298,6 +1310,53 @@ int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb) > } > EXPORT_SYMBOL_GPL(vb2_core_prepare_buf); > > +/** > + * vb2_check_buf_req_status() - Validate request state of a buffer > + * @vb: buffer to check > + * > + * Returns true if a buffer is ready to be passed to the driver request-wise. > + * This means that neither this buffer nor any previously-queued buffer is > + * associated to a request that is not yet submitted. > + * > + * If this function returns false, then the buffer shall not be passed to its > + * driver since the request state is not completely built yet. In that case, > + * this function will register a notifier to be called when the request is > + * submitted and the queue can be unblocked. > + * > + * This function must be called with req_lock held. > + */ > +static bool vb2_check_buf_req_status(struct vb2_buffer *vb) > +{ > + struct media_request *req = vb->request; > + struct vb2_queue *q = vb->vb2_queue; > + int ret = false; > + > + mutex_lock(&q->req_lock); > + > + if (!req) { > + ret = !q->waiting_req; > + goto done; > + } > + > + mutex_lock(&req->lock); > + if (req->state == MEDIA_REQUEST_STATE_SUBMITTED) { Would it make sense to serialise access to request state using the queue lock instead? The queue and the state are often accessed together. The function itself seems weird. The VB2 framework does not have enough information to determine whether a request is complete; this is something a driver must evaluate at the time of queueing the request. > + mutex_unlock(&req->lock); > + ret = !q->waiting_req; > + goto done; > + } > + > + if (!q->waiting_req) { > + q->waiting_req = true; > + atomic_notifier_chain_register(&req->submit_notif, > + &q->req_blk); > + } > + mutex_unlock(&req->lock); > + > +done: > + mutex_unlock(&q->req_lock); > + return ret; > +} > + > /** > * vb2_start_streaming() - Attempt to start streaming. > * @q: videobuf2 queue > @@ -1318,8 +1377,11 @@ static int vb2_start_streaming(struct vb2_queue *q) > * If any buffers were queued before streamon, > * we can now pass them to driver for processing. > */ > - list_for_each_entry(vb, &q->queued_list, queued_entry) > + list_for_each_entry(vb, &q->queued_list, queued_entry) { > + if (!vb2_check_buf_req_status(vb)) > + break; > __enqueue_in_driver(vb); Queueing buffers to drivers this way hardly makes sense with requests. The driver has no use for it, all it cares about is the request. VB2 could manage its own state of the buffer this way but that's only useful from V4L2 API point of view, it has no functional purpose. This suggests we need an overhaul of the existing buffer management for requests. > + } > > /* Tell the driver to start streaming */ > q->start_streaming_called = 1; > @@ -1361,7 +1423,46 @@ static int vb2_start_streaming(struct vb2_queue *q) > return ret; > } > > -int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb) > +/** > + * vb2_unblock_requests() - unblock a queue waiting for a request submission > + * @nb: notifier block that has been registered > + * @action: unused > + * @data: request that has been submitted > + * > + * This is a callback function that is registered when > + * vb2_check_buf_req_status() returns false. It is invoked when the request > + * blocking the queue has been submitted. This means its buffers (and all > + * following valid buffers) can be passed to drivers. This is a perplexing one. Why would you not queue the request to the driver at the time of... it is queued, by the user? The VB2 framework (nor the media or V4L2 frameworks to that matter) does not have enough information in order to help the driver here. > + */ > +static int vb2_unblock_requests(struct notifier_block *nb, unsigned long action, > + void *data) > +{ > + struct vb2_queue *q = container_of(nb, struct vb2_queue, req_blk); > + struct media_request *req = data; > + struct vb2_buffer *vb; > + bool found_request = false; > + > + mutex_lock(&q->req_lock); > + atomic_notifier_chain_unregister(&req->submit_notif, &q->req_blk); > + q->waiting_req = false; > + mutex_unlock(&q->req_lock); > + > + list_for_each_entry(vb, &q->queued_list, queued_entry) { > + /* All buffers before our request are already passed to the driver */ > + if (!found_request && vb->request != req) > + continue; > + found_request = true; > + > + if (!vb2_check_buf_req_status(vb)) > + break; > + __enqueue_in_driver(vb); > + } > + > + return 0; > +} > + > +int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, > + struct media_request *req, void *pb) > { > struct vb2_buffer *vb; > int ret; > @@ -1398,11 +1499,21 @@ int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb) > > trace_vb2_qbuf(q, vb); > > + vb->request = req; > + if (req) { > + if (!q->allow_requests) > + return -EINVAL; > + > + /* make sure the request stays alive as long as we need */ > + media_request_get(req); > + atomic_inc(&req->buf_cpt); > + } > + > /* > * If already streaming, give the buffer to driver for processing. > * If not, the buffer will be given to driver on next streamon. > */ > - if (q->start_streaming_called) > + if (q->start_streaming_called && vb2_check_buf_req_status(vb)) > __enqueue_in_driver(vb); > > /* Fill buffer information for the userspace */ > @@ -1993,6 +2104,8 @@ int vb2_core_queue_init(struct vb2_queue *q) > spin_lock_init(&q->done_lock); > mutex_init(&q->mmap_lock); > init_waitqueue_head(&q->done_wq); > + mutex_init(&q->req_lock); > + q->req_blk.notifier_call = vb2_unblock_requests; > > if (q->buf_struct_size == 0) > q->buf_struct_size = sizeof(struct vb2_buffer); > @@ -2242,7 +2355,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) > * Queue all buffers. > */ > for (i = 0; i < q->num_buffers; i++) { > - ret = vb2_core_qbuf(q, i, NULL); > + ret = vb2_core_qbuf(q, i, NULL, NULL); > if (ret) > goto err_reqbufs; > fileio->bufs[i].queued = 1; > @@ -2421,7 +2534,7 @@ static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_ > > if (copy_timestamp) > b->timestamp = ktime_get_ns(); > - ret = vb2_core_qbuf(q, index, NULL); > + ret = vb2_core_qbuf(q, index, NULL, NULL); > dprintk(5, "vb2_dbuf result: %d\n", ret); > if (ret) > return ret; > @@ -2524,7 +2637,7 @@ static int vb2_thread(void *data) > if (copy_timestamp) > vb->timestamp = ktime_get_ns();; > if (!threadio->stop) > - ret = vb2_core_qbuf(q, vb->index, NULL); > + ret = vb2_core_qbuf(q, vb->index, NULL, NULL); > call_void_qop(q, wait_prepare, q); > if (ret || threadio->stop) > break; > diff --git a/drivers/media/v4l2-core/videobuf2-v4l2.c b/drivers/media/v4l2-core/videobuf2-v4l2.c > index 0f8edbdebe30..267fe2d669b2 100644 > --- a/drivers/media/v4l2-core/videobuf2-v4l2.c > +++ b/drivers/media/v4l2-core/videobuf2-v4l2.c > @@ -30,6 +30,7 @@ > #include > > #include > +#include Alphabetical order, please. > > static int debug; > module_param(debug, int, 0644); > @@ -561,6 +562,7 @@ EXPORT_SYMBOL_GPL(vb2_create_bufs); > > int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b) > { > + struct media_request *req = NULL; > int ret; > > if (vb2_fileio_is_active(q)) { > @@ -568,8 +570,32 @@ int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b) > return -EBUSY; > } > > + /* > + * The caller should have validated that the request is valid, > + * so we just need to look it up without further checking Why? This would mean you'll need to look up a request twice per IOCTL. > + */ > + if (b->request_fd > 0) { > + req = media_request_get_from_fd(b->request_fd); > + if (!req) > + return -EINVAL; > + > + mutex_lock(&req->lock); > + if (req->state != MEDIA_REQUEST_STATE_IDLE) { > + mutex_unlock(&req->lock); > + media_request_put(req); > + return -EINVAL; > + } > + mutex_unlock(&req->lock); > + } > + > ret = vb2_queue_or_prepare_buf(q, b, "qbuf"); > - return ret ? ret : vb2_core_qbuf(q, b->index, b); > + if (!ret) > + ret = vb2_core_qbuf(q, b->index, req, b); > + > + if (req) > + media_request_put(req); > + > + return ret; > } > EXPORT_SYMBOL_GPL(vb2_qbuf); > > diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h > index ef9b64398c8c..7bb17c842ab4 100644 > --- a/include/media/videobuf2-core.h > +++ b/include/media/videobuf2-core.h > @@ -237,6 +237,7 @@ struct vb2_queue; > * on an internal driver queue > * @planes: private per-plane information; do not change > * @timestamp: frame timestamp in ns > + * @request: request the buffer belongs to, if any > */ > struct vb2_buffer { > struct vb2_queue *vb2_queue; > @@ -246,6 +247,7 @@ struct vb2_buffer { > unsigned int num_planes; > struct vb2_plane planes[VB2_MAX_PLANES]; > u64 timestamp; > + struct media_request *request; > > /* private: internal use only > * > @@ -443,6 +445,7 @@ struct vb2_buf_ops { > * @quirk_poll_must_check_waiting_for_buffers: Return POLLERR at poll when QBUF > * has not been called. This is a vb1 idiom that has been adopted > * also by vb2. > + * @allow_requests: whether requests are supported on this queue. > * @lock: pointer to a mutex that protects the vb2_queue struct. The > * driver can set this to a mutex to let the v4l2 core serialize > * the queuing ioctls. If the driver wants to handle locking > @@ -500,6 +503,9 @@ struct vb2_buf_ops { > * when a buffer with the V4L2_BUF_FLAG_LAST is dequeued. > * @fileio: file io emulator internal data, used only if emulator is active > * @threadio: thread io internal data, used only if thread is active > + * @req_lock: protects req_blk and waiting_req > + * @req_blk: notifier to be called when waiting for a request to be submitted > + * @waiting_req:whether this queue is currently waiting on a request submission > */ > struct vb2_queue { > unsigned int type; > @@ -511,6 +517,7 @@ struct vb2_queue { > unsigned fileio_write_immediately:1; > unsigned allow_zero_bytesused:1; > unsigned quirk_poll_must_check_waiting_for_buffers:1; > + unsigned allow_requests:1; > > struct mutex *lock; > void *owner; > @@ -554,6 +561,10 @@ struct vb2_queue { > struct vb2_fileio_data *fileio; > struct vb2_threadio_data *threadio; > > + struct mutex req_lock; > + struct notifier_block req_blk; > + bool waiting_req; > + > #ifdef CONFIG_VIDEO_ADV_DEBUG > /* > * Counters for how often these queue-related ops are > @@ -724,6 +735,7 @@ int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb); > * > * @q: videobuf2 queue > * @index: id number of the buffer > + * @req: request this buffer belongs to, if any > * @pb: buffer structure passed from userspace to vidioc_qbuf handler > * in driver > * > @@ -740,7 +752,8 @@ int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb); > * The return values from this function are intended to be directly returned > * from vidioc_qbuf handler in driver. > */ > -int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb); > +int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, > + struct media_request *req, void *pb); > > /** > * vb2_core_dqbuf() - Dequeue a buffer to the userspace -- Sakari Ailus sakari.ailus@linux.intel.com