Received: by 10.223.176.46 with SMTP id f43csp284788wra; Thu, 25 Jan 2018 22:05:23 -0800 (PST) X-Google-Smtp-Source: AH8x224GyKLn/uysEh15lp+5nsi3EmR2/BlSjWeUzWShnSxo/6DJnxhvJTw5kxBqBB0P0t1enQT5 X-Received: by 2002:a17:902:e2:: with SMTP id a89-v6mr13280121pla.98.1516946722934; Thu, 25 Jan 2018 22:05:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516946722; cv=none; d=google.com; s=arc-20160816; b=VWsEKXfD+zRD0dq5S+8cTuyIto85zK+QaWZZpzWV3GEn488EKEG5EYktUj/LxdJnu9 Q6TusKBZQkBglJdzz5ThnWvOYR/uXzqaX2XSXqUx6uD45T6ZW70fEfZdpFpRElXZIBxi A8gHHMMHU19pQKq4DhWBHF5vx0N9TOXy601jXYPwZY9fjZP1vnPTZsTIcZZS8OyKrG00 Pmm7HGsAqNmokLb8UtzZdaPSlSkvdh2RxDZhi7ltd1N7COeiP4Ti40H0BVA1ttH1bcNm 9PRDZOAEZouqvhTqyIKEAYjeDa/Mxd9lHQy1ZVvWILnABVqHWmnCTSWCXA2nSG3iX2LS Sn+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Edg7XKfwlgZzybceqfL7cDh9Kc4867L0NgYGQz4+UWg=; b=CWa+8+uOJMjwLIrNCgXaNOk1l+Xl+ZV1uM7Z5L/AIb9qL9xxItvt0Af3zNXr7WcjcA 0GuKodbZVnURYe9fwWnPCfv4vOk55P+372jtwkfE5ivhwiUickMKI6JogbGkL85jnggt 1oBnvImeGbb9MmjIxK7ek4+4WPOuGcMjhAqO9P70HKuTXhXkrq1zXOzFaHlEvfDIN0M+ Yqy+1AGjd6Z2FHboAmH9OE1NF5EADUF/5Y7/fa0GsUG8kbl7aYFVg4EySluQ9E3exw1m jCPjmTZpgjPdp3ai0Wwl+HkYRm6VtAbtqA2IDQpS3DQ0KDdVk1TeuB1Rfpw1PyzUZcSq tP0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jVOuN02R; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c22si2584712pgn.460.2018.01.25.22.05.08; Thu, 25 Jan 2018 22:05:22 -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; dkim=pass header.i=@chromium.org header.s=google header.b=jVOuN02R; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751886AbeAZGCz (ORCPT + 99 others); Fri, 26 Jan 2018 01:02:55 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:46273 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751860AbeAZGCu (ORCPT ); Fri, 26 Jan 2018 01:02:50 -0500 Received: by mail-pg0-f65.google.com with SMTP id s9so6607717pgq.13 for ; Thu, 25 Jan 2018 22:02:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Edg7XKfwlgZzybceqfL7cDh9Kc4867L0NgYGQz4+UWg=; b=jVOuN02Ryp4SlS9t9GeuymtQwwNiPvEvs7eYgBdOZrQUr9cYjwwhqyMAh+8Ogoqm+d 1nNj/rt7iR4By2jSSVGkKuz/cn7e7q43NU5446mqKc7dYiM6afKjbBcqd1h40eJUDoST f0DzvsevdELTcjHmEtkIR2j3ezA/KdBiEiYeY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Edg7XKfwlgZzybceqfL7cDh9Kc4867L0NgYGQz4+UWg=; b=hmtDREdezzzs+vFVFdTJ7iFUn+uK6Ccmml3jqivtbXEbsyw0Jgb4B3l3qmQm9uEjC/ 5tTHfWelmSegqXfykwBq8ai1Cfn/Eho/ZYyKSc1yaiCTPUcDTGPmtgsQiXdJbY51Z5FA zCMRamtLL+u22gLwf4AnXi1pNYxQDH9F+EtQwAxh/mekx5pHtxdr5DlzOnetgzM1xzcf 1aDlD5fJwwjirCpDo+dqCFfoRdUG1yLliLdtKfSsm2bG/3n7BOPuZLsanJYujQ3UV8KR xiuPI9EU1T2slyJxj60+IRZ1NHoHa0VdB8hF/A6QzxFtwdKhoTHEnXOCmQsokpawL6SR W45A== X-Gm-Message-State: AKwxyteeKBH7rIaRTOVBiezImhM36DYnfbWdHZ3VnLzoMxyNSRJQ08Dl JG7OXLYW5cZT1CfzXIfCxwYovQ== X-Received: by 10.98.91.193 with SMTP id p184mr18036801pfb.16.1516946569867; Thu, 25 Jan 2018 22:02:49 -0800 (PST) Received: from acourbot.tok.corp.google.com ([2401:fa00:4:1002:a6cd:a898:e07b:a331]) by smtp.gmail.com with ESMTPSA id j3sm14543201pfh.39.2018.01.25.22.02.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Jan 2018 22:02:49 -0800 (PST) From: Alexandre Courbot To: Mauro Carvalho Chehab , Hans Verkuil , Laurent Pinchart , Pawel Osciak , Marek Szyprowski , Tomasz Figa , Sakari Ailus , Gustavo Padovan Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Courbot Subject: [RFC PATCH 3/8] media: videobuf2: add support for requests Date: Fri, 26 Jan 2018 15:02:11 +0900 Message-Id: <20180126060216.147918-4-acourbot@chromium.org> X-Mailer: git-send-email 2.16.0.rc1.238.g530d649a79-goog In-Reply-To: <20180126060216.147918-1-acourbot@chromium.org> References: <20180126060216.147918-1-acourbot@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) { + 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); + } /* 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. + */ +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 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 + */ + 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 -- 2.16.0.rc1.238.g530d649a79-goog