From patchwork Thu Mar 14 15:32:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 780437 Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 538D371749; Thu, 14 Mar 2024 15:32:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430358; cv=none; b=VmyYXW2VLGAtjSiLcXawpNeneQ4Xf9z/op1fqYfu6h+kPuFB8REyvQASePjgop0QvnR7frJjphLpeK/mEV/HQlo9GVxkIMdWNs5WKm0FtD8tPkPdI0jpgmXggJhg91eEa6yN826c+AP2QN8x622coyzuAAWQk6Vwm8jiXYsf9Qw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430358; c=relaxed/simple; bh=N3rnu1TmY3k6wbtelOYee/CdzF0xUD+mi+uZbUMnoKk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=STRfkCixiTxNgjcEYGDls8U0V45ryVZLCM8MGlwp0me0IPqMiOx+vxUz5kUgJxt8j0EKhonKMKP/bWxOOIo8b0/UO9HJOphY7JvkT1k9qGi19jzp1zKUR6TyrVmchAjB5gtGLGsg1Imnm9eiY5P3orrlff95sYpaG9cIlkge33I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=sVTs57DZ; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="sVTs57DZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1710430354; bh=N3rnu1TmY3k6wbtelOYee/CdzF0xUD+mi+uZbUMnoKk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sVTs57DZ9qUSowSL1icrzBSodQAEwsbR0SiVRw9FaNKa2MQ9FfzQCfLd3ieLvCWuB nTHyOLXLT6Mz4a9egPEzjrHXe9SXI9LRLUHkojKeMqD+pWJwGmEeW3+pZVP984BcGU oEcpsVQBAHCc/vO5fO0o2gsBPlElvvXv5znRmjbyWyWWZzR/aDp4EESmTtXGEP/TJI JOo6ZQCRYiR5EhBQfcBqCDc/GnoKjtGA+hrrt0h0Sui4Su6RZrkEEPJaKv5t81hn0S BIT4Dr8yliQTm2GYQaO3n/aDmbMLUlKAwikRLPepMkfLzs51iRqRps5LyyrNtg49IK RLzsuxwA0Lv9Q== Received: from benjamin-XPS-13-9310.. (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: benjamin.gaignard) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 0CAAD37820F7; Thu, 14 Mar 2024 15:32:34 +0000 (UTC) From: Benjamin Gaignard To: mchehab@kernel.org, tfiga@chromium.org, m.szyprowski@samsung.com, ezequiel@vanguardiasur.com.ar, p.zabel@pengutronix.de, hverkuil-cisco@xs4all.nl, nicolas@ndufresne.ca Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v21 2/9] videobuf2: Add min_reqbufs_allocation field to vb2_queue structure Date: Thu, 14 Mar 2024 16:32:19 +0100 Message-Id: <20240314153226.197445-3-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240314153226.197445-1-benjamin.gaignard@collabora.com> References: <20240314153226.197445-1-benjamin.gaignard@collabora.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add 'min_reqbufs_allocation' field in the vb2_queue structure so drivers can specify the minimum number of buffers to allocate when calling VIDIOC_REQBUFS. When initializing the queue, v4l2 core makes sure that the following constraints are respected: - the minimum number of buffers to allocate must be at least 2 because one buffer is used by the hardware while the other is being processed by userspace. -if the driver needs 'min_queued_buffers' in the queue before calling start_streaming(), then the minimum requirement is 'min_queued_buffers + 1' to keep at least one buffer available for userspace. Simplify __vb2_init_fileio() by using 'min_reqbufs_allocation' directly to avoid duplicating the minimum number of buffers to allocate computation. Signed-off-by: Benjamin Gaignard Reviewed-by: Mauro Carvalho Chehab --- .../media/common/videobuf2/videobuf2-core.c | 38 +++++++++++-------- include/media/videobuf2-core.h | 15 +++++++- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index d8b3c04cb3b5..58c495b253ce 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -866,7 +866,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, /* * Make sure the requested values and current defaults are sane. */ - num_buffers = max_t(unsigned int, *count, q->min_queued_buffers); + num_buffers = max_t(unsigned int, *count, q->min_reqbufs_allocation); num_buffers = min_t(unsigned int, num_buffers, q->max_num_buffers); memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); /* @@ -918,7 +918,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, * There is no point in continuing if we can't allocate the minimum * number of buffers needed by this vb2_queue. */ - if (allocated_buffers < q->min_queued_buffers) + if (allocated_buffers < q->min_reqbufs_allocation) ret = -ENOMEM; /* @@ -2524,6 +2524,25 @@ int vb2_core_queue_init(struct vb2_queue *q) if (WARN_ON(q->supports_requests && q->min_queued_buffers)) return -EINVAL; + /* + * The minimum requirement is 2: one buffer is used + * by the hardware while the other is being processed by userspace. + */ + if (q->min_reqbufs_allocation < 2) + q->min_reqbufs_allocation = 2; + + /* + * If the driver needs 'min_queued_buffers' in the queue before + * calling start_streaming() then the minimum requirement is + * 'min_queued_buffers + 1' to keep at least one buffer available + * for userspace. + */ + if (q->min_reqbufs_allocation < q->min_queued_buffers + 1) + q->min_reqbufs_allocation = q->min_queued_buffers + 1; + + if (WARN_ON(q->min_reqbufs_allocation > q->max_num_buffers)) + return -EINVAL; + INIT_LIST_HEAD(&q->queued_list); INIT_LIST_HEAD(&q->done_list); spin_lock_init(&q->done_lock); @@ -2717,7 +2736,6 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) struct vb2_fileio_data *fileio; struct vb2_buffer *vb; int i, ret; - unsigned int count = 0; /* * Sanity check @@ -2738,18 +2756,8 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) if (q->streaming || vb2_get_num_buffers(q) > 0) return -EBUSY; - /* - * Start with q->min_queued_buffers + 1, driver can increase it in - * queue_setup() - * - * 'min_queued_buffers' buffers need to be queued up before you - * can start streaming, plus 1 for userspace (or in this case, - * kernelspace) processing. - */ - count = max(2, q->min_queued_buffers + 1); - dprintk(q, 3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n", - (read) ? "read" : "write", count, q->fileio_read_once, + (read) ? "read" : "write", q->min_reqbufs_allocation, q->fileio_read_once, q->fileio_write_immediately); fileio = kzalloc(sizeof(*fileio), GFP_KERNEL); @@ -2763,7 +2771,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) * Request buffers and use MMAP type to force driver * to allocate buffers by itself. */ - fileio->count = count; + fileio->count = q->min_reqbufs_allocation; fileio->memory = VB2_MEMORY_MMAP; fileio->type = q->type; q->fileio = fileio; diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 667bf9ee1101..4a8b9135cec8 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -549,9 +549,21 @@ struct vb2_buf_ops { * @start_streaming can be called. Used when a DMA engine * cannot be started unless at least this number of buffers * have been queued into the driver. - * VIDIOC_REQBUFS will ensure at least @min_queued_buffers + * VIDIOC_REQBUFS will ensure at least @min_queued_buffers + 1 * buffers will be allocated. Note that VIDIOC_CREATE_BUFS will not * modify the requested buffer count. + * @min_reqbufs_allocation: the minimum number of buffers to be allocated when + * calling VIDIOC_REQBUFS. Note that VIDIOC_CREATE_BUFS will *not* + * modify the requested buffer count and does not use this field. + * Drivers can set this if there has to be a certain number of + * buffers available for the hardware to work effectively. + * This allows calling VIDIOC_REQBUFS with a buffer count of 1 and + * it will be automatically adjusted to a workable buffer count. + * If set, then @min_reqbufs_allocation must be larger than + * @min_queued_buffers + 1. + * If this field is > 3, then it is highly recommended that the + * driver implements the V4L2_CID_MIN_BUFFERS_FOR_CAPTURE/OUTPUT + * control. * @alloc_devs: &struct device memory type/allocator-specific per-plane device */ /* @@ -622,6 +634,7 @@ struct vb2_queue { u32 timestamp_flags; gfp_t gfp_flags; u32 min_queued_buffers; + u32 min_reqbufs_allocation; struct device *alloc_devs[VB2_MAX_PLANES]; From patchwork Thu Mar 14 15:32:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 780436 Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D071C73161; Thu, 14 Mar 2024 15:32:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430359; cv=none; b=hARzmeor3QwRYJgsXYmmq1VV4XDn5EOLlD4E+qynRmWBvYTGzoPUbab3VuWMkBr6qK19vp3CfoHMqBZ7U7nLHKeTvmLOzEKzb0+qzmt/3DlTUX9xKJ6dqDQ7hSsGtZr43JX+hwEy5+n6wQXpTYJXczuOjvdYvVvwcdG/UiL3jb0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430359; c=relaxed/simple; bh=ovTlFb/DiuVq4Pi+VEIOih5rZkmJWCtlpntl+9y2cPo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OSvgz1ltDMdM/5YG8tObHS9mZa678V0wZIzDbnhWoMz8lP93z+Pe8Z1pcZYuSamLu01t7Y5tvFGyD1HYUvjUbSSJPyB3Nw55TifU/Hw+JNWYFKCBzpYAqqPwvYdxB1AdA/ugsEObl4IvpT+BsOf/FpMkDER1n+fkGi/NMp1BDrQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=x5DNPNbB; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="x5DNPNbB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1710430356; bh=ovTlFb/DiuVq4Pi+VEIOih5rZkmJWCtlpntl+9y2cPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=x5DNPNbBoNW617w3hvCnLw62AbDR32TJ05CPvIvACrWH+0xIXgHOah1X7OT0HO8T/ E78i3NNsNMOCrnhz/t/LVznvtNDdpDb6LQ0z+1Fhw6Q08bVig/ceKDKy2oVBdGjzHa MzbZocRju26qctghEgLKqo5nSW3e3El8tQ6xUe8QOfQQCDp3nA+XgDHIw/EGVmroCB A4kgsMfwLn6UxsuPludsn0qgf5P/uk/JtQ9htz3w0LTe0w+utIF2DkjMnMpc6lnU3a mNqySoQkSHj0xy78mjY3NLb5rIaTdFcp01BNHsxeQZB4nK2eev4Wf7oQmElnxZg/Po 6b6IelOuyhvwg== Received: from benjamin-XPS-13-9310.. (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: benjamin.gaignard) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 8915637820F9; Thu, 14 Mar 2024 15:32:35 +0000 (UTC) From: Benjamin Gaignard To: mchehab@kernel.org, tfiga@chromium.org, m.szyprowski@samsung.com, ezequiel@vanguardiasur.com.ar, p.zabel@pengutronix.de, hverkuil-cisco@xs4all.nl, nicolas@ndufresne.ca Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v21 4/9] media: core: Rework how create_buf index returned value is computed Date: Thu, 14 Mar 2024 16:32:21 +0100 Message-Id: <20240314153226.197445-5-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240314153226.197445-1-benjamin.gaignard@collabora.com> References: <20240314153226.197445-1-benjamin.gaignard@collabora.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When REMOVE_BUFS will be introduced holes could created in bufs array. To be able to reuse these unused indices reworking how create->index is set is mandatory. Let __vb2_queue_alloc() decide which first index is correct and forward this to the caller. Signed-off-by: Benjamin Gaignard --- .../media/common/videobuf2/videobuf2-core.c | 18 +++++++++++++----- .../media/common/videobuf2/videobuf2-v4l2.c | 14 +++++++++----- include/media/videobuf2-core.h | 5 ++++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 58c495b253ce..8e819d198c34 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -442,12 +442,15 @@ static void vb2_queue_remove_buffer(struct vb2_buffer *vb) * __vb2_queue_alloc() - allocate vb2 buffer structures and (for MMAP type) * video buffer memory for all buffers/planes on the queue and initializes the * queue + * @first_index: index of the first created buffer, all newly allocated buffers + * have indices in the range [first_index..first_index+count-1] * * Returns the number of buffers successfully allocated. */ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, unsigned int num_buffers, unsigned int num_planes, - const unsigned plane_sizes[VB2_MAX_PLANES]) + const unsigned int plane_sizes[VB2_MAX_PLANES], + unsigned int *first_index) { unsigned int q_num_buffers = vb2_get_num_buffers(q); unsigned int buffer, plane; @@ -461,6 +464,8 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, num_buffers = min_t(unsigned int, num_buffers, q->max_num_buffers - q_num_buffers); + *first_index = q_num_buffers; + for (buffer = 0; buffer < num_buffers; ++buffer) { /* Allocate vb2 buffer structures */ vb = kzalloc(q->buf_struct_size, GFP_KERNEL); @@ -820,7 +825,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, unsigned int q_num_bufs = vb2_get_num_buffers(q); unsigned plane_sizes[VB2_MAX_PLANES] = { }; bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; - unsigned int i; + unsigned int i, first_index; int ret = 0; if (q->streaming) { @@ -907,8 +912,10 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, /* Finally, allocate buffers and video memory */ allocated_buffers = - __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes); + __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes, &first_index); if (allocated_buffers == 0) { + /* There shouldn't be any buffers allocated, so first_index == 0 */ + WARN_ON(first_index); dprintk(q, 1, "memory allocation failed\n"); ret = -ENOMEM; goto error; @@ -982,7 +989,8 @@ EXPORT_SYMBOL_GPL(vb2_core_reqbufs); int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, unsigned int flags, unsigned int *count, unsigned int requested_planes, - const unsigned int requested_sizes[]) + const unsigned int requested_sizes[], + unsigned int *first_index) { unsigned int num_planes = 0, num_buffers, allocated_buffers; unsigned plane_sizes[VB2_MAX_PLANES] = { }; @@ -1044,7 +1052,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, /* Finally, allocate buffers and video memory */ allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers, - num_planes, plane_sizes); + num_planes, plane_sizes, first_index); if (allocated_buffers == 0) { dprintk(q, 1, "memory allocation failed\n"); ret = -ENOMEM; diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/media/common/videobuf2/videobuf2-v4l2.c index c575198e8354..03e8080a68a8 100644 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c @@ -795,11 +795,15 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) for (i = 0; i < requested_planes; i++) if (requested_sizes[i] == 0) return -EINVAL; - return ret ? ret : vb2_core_create_bufs(q, create->memory, - create->flags, - &create->count, - requested_planes, - requested_sizes); + if (ret) + return ret; + + return vb2_core_create_bufs(q, create->memory, + create->flags, + &create->count, + requested_planes, + requested_sizes, + &create->index); } EXPORT_SYMBOL_GPL(vb2_create_bufs); diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 4a8b9135cec8..42526e289c8e 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -826,6 +826,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, * @count: requested buffer count. * @requested_planes: number of planes requested. * @requested_sizes: array with the size of the planes. + * @first_index: index of the first created buffer, all allocated buffers have + * indices in the range [first_index..first_index+count-1] * * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is * called internally by VB2 by an API-specific handler, like @@ -842,7 +844,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, unsigned int flags, unsigned int *count, unsigned int requested_planes, - const unsigned int requested_sizes[]); + const unsigned int requested_sizes[], + unsigned int *first_index); /** * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace From patchwork Thu Mar 14 15:32:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 780435 Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 59B5673522; Thu, 14 Mar 2024 15:32:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430361; cv=none; b=d356N9asptSj3D1mOwcpX4SzRCxIfJq7DR9p6pyka2qNW4ntkLFKsYRfUG8q788p4ouY5kQHhUcvoUugB9Tj0lDq6vSMHU/DnvvcRG9WjuAZcbrrnFr7p44QOhAGfLad5nIYWG8WbFAGFfY5jdUjYn5YfmnQX4UeRVEvndH/V/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430361; c=relaxed/simple; bh=xBHj61ms77yh6TofIx3mWpzyvQ5wa//86PofqFg3BeI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PNPHL4Zi0j9P5MatFCECBvYYpZL8mFN+8Ki8Tuknc5KgkbsZE+5wJQb292EYsbxUYRv/uPG9VL6C1zGwQYeieLTNtfqOwszP0JzhJ5tI6sLj+O+FMUF0THQWxCMcQvgs7U8bO9mtegvHhqnchU9HFFFDGdiib1Zsz/htdPttjV0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=p7nYbKC3; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="p7nYbKC3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1710430357; bh=xBHj61ms77yh6TofIx3mWpzyvQ5wa//86PofqFg3BeI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p7nYbKC3xSaooL0Nw/TKIywmhw4GrFuvAtutCivQm4yokjbdA9bEkmZO3zSURJ00L 2jaMEd7N0qo9yFI2SFMB88xRbrqd0YSvJf5FeQMlILOa5PWuHfgCJedCA68OMJYQXt vJs454tzlVDwi32Tq9iSxnNJ3Vc7AtE1ZddXUkzGYoiSmnwps43JKdAAqm0x+8KM5B 0MR0TBRGkr7zJE7LFpkKwlEIfqHee1cPy8YcJFRanralcsukd4Av/XEVZkTt5gSTSk SYQjYyBnLSh4y7egHB+h3SOlRbugk1btXz+uKPd8+kGrmoEWWVxEuxx7l+2PPWTiE5 g0/18KKvuV9IQ== Received: from benjamin-XPS-13-9310.. (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: benjamin.gaignard) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 1E40837820FF; Thu, 14 Mar 2024 15:32:37 +0000 (UTC) From: Benjamin Gaignard To: mchehab@kernel.org, tfiga@chromium.org, m.szyprowski@samsung.com, ezequiel@vanguardiasur.com.ar, p.zabel@pengutronix.de, hverkuil-cisco@xs4all.nl, nicolas@ndufresne.ca Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v21 6/9] media: core: Free range of buffers Date: Thu, 14 Mar 2024 16:32:23 +0100 Message-Id: <20240314153226.197445-7-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240314153226.197445-1-benjamin.gaignard@collabora.com> References: <20240314153226.197445-1-benjamin.gaignard@collabora.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Improve __vb2_queue_free() and __vb2_free_mem() to free range of buffers and not only the last few buffers. Introduce starting index to be flexible on range and change the loops according to this parameter. Signed-off-by: Benjamin Gaignard Acked-by: Mauro Carvalho Chehab --- .../media/common/videobuf2/videobuf2-core.c | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index ec81426d4d79..009cea95d662 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -540,17 +540,16 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, } /* - * __vb2_free_mem() - release all video buffer memory for a given queue + * __vb2_free_mem() - release video buffer memory for a given range of + * buffers in a given queue */ -static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) +static void __vb2_free_mem(struct vb2_queue *q, unsigned int start, unsigned int count) { - unsigned int buffer; + unsigned int i; struct vb2_buffer *vb; - unsigned int q_num_buffers = vb2_get_num_buffers(q); - for (buffer = q_num_buffers - buffers; buffer < q_num_buffers; - ++buffer) { - vb = vb2_get_buffer(q, buffer); + for (i = start; i < start + count; i++) { + vb = vb2_get_buffer(q, i); if (!vb) continue; @@ -565,35 +564,33 @@ static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) } /* - * __vb2_queue_free() - free buffers at the end of the queue - video memory and + * __vb2_queue_free() - free @count buffers from @start index of the queue - video memory and * related information, if no buffers are left return the queue to an * uninitialized state. Might be called even if the queue has already been freed. */ -static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) +static void __vb2_queue_free(struct vb2_queue *q, unsigned int start, unsigned int count) { - unsigned int buffer; - unsigned int q_num_buffers = vb2_get_num_buffers(q); + unsigned int i; lockdep_assert_held(&q->mmap_lock); /* Call driver-provided cleanup function for each buffer, if provided */ - for (buffer = q_num_buffers - buffers; buffer < q_num_buffers; - ++buffer) { - struct vb2_buffer *vb = vb2_get_buffer(q, buffer); + for (i = start; i < start + count; i++) { + struct vb2_buffer *vb = vb2_get_buffer(q, i); if (vb && vb->planes[0].mem_priv) call_void_vb_qop(vb, buf_cleanup, vb); } /* Release video buffer memory */ - __vb2_free_mem(q, buffers); + __vb2_free_mem(q, start, count); #ifdef CONFIG_VIDEO_ADV_DEBUG /* * Check that all the calls were balanced during the life-time of this * queue. If not then dump the counters to the kernel log. */ - if (q_num_buffers) { + if (vb2_get_num_buffers(q)) { bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming || q->cnt_prepare_streaming != q->cnt_unprepare_streaming || q->cnt_wait_prepare != q->cnt_wait_finish; @@ -619,8 +616,8 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) q->cnt_stop_streaming = 0; q->cnt_unprepare_streaming = 0; } - for (buffer = 0; buffer < vb2_get_num_buffers(q); buffer++) { - struct vb2_buffer *vb = vb2_get_buffer(q, buffer); + for (i = start; i < start + count; i++) { + struct vb2_buffer *vb = vb2_get_buffer(q, i); bool unbalanced; if (!vb) @@ -637,7 +634,7 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) if (unbalanced) { pr_info("unbalanced counters for queue %p, buffer %d:\n", - q, buffer); + q, i); if (vb->cnt_buf_init != vb->cnt_buf_cleanup) pr_info(" buf_init: %u buf_cleanup: %u\n", vb->cnt_buf_init, vb->cnt_buf_cleanup); @@ -671,9 +668,8 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) #endif /* Free vb2 buffers */ - for (buffer = q_num_buffers - buffers; buffer < q_num_buffers; - ++buffer) { - struct vb2_buffer *vb = vb2_get_buffer(q, buffer); + for (i = start; i < start + count; i++) { + struct vb2_buffer *vb = vb2_get_buffer(q, i); if (!vb) continue; @@ -713,7 +709,7 @@ EXPORT_SYMBOL(vb2_buffer_in_use); static bool __buffers_in_use(struct vb2_queue *q) { unsigned int buffer; - for (buffer = 0; buffer < vb2_get_num_buffers(q); ++buffer) { + for (buffer = 0; buffer < q->max_num_buffers; ++buffer) { struct vb2_buffer *vb = vb2_get_buffer(q, buffer); if (!vb) @@ -899,7 +895,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, * queued without ever calling STREAMON. */ __vb2_queue_cancel(q); - __vb2_queue_free(q, q_num_bufs); + __vb2_queue_free(q, 0, q->max_num_buffers); mutex_unlock(&q->mmap_lock); q->is_busy = 0; @@ -1001,7 +997,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, * from already queued buffers and it will reset q->memory to * VB2_MEMORY_UNKNOWN. */ - __vb2_queue_free(q, allocated_buffers); + __vb2_queue_free(q, first_index, allocated_buffers); mutex_unlock(&q->mmap_lock); return ret; } @@ -1126,7 +1122,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, * from already queued buffers and it will reset q->memory to * VB2_MEMORY_UNKNOWN. */ - __vb2_queue_free(q, allocated_buffers); + __vb2_queue_free(q, *first_index, allocated_buffers); mutex_unlock(&q->mmap_lock); return -ENOMEM; } @@ -1741,7 +1737,7 @@ static int vb2_start_streaming(struct vb2_queue *q) * Forcefully reclaim buffers if the driver did not * correctly return them to vb2. */ - for (i = 0; i < vb2_get_num_buffers(q); ++i) { + for (i = 0; i < q->max_num_buffers; ++i) { vb = vb2_get_buffer(q, i); if (!vb) @@ -2147,7 +2143,7 @@ static void __vb2_queue_cancel(struct vb2_queue *q) * to vb2 in stop_streaming(). */ if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { - for (i = 0; i < vb2_get_num_buffers(q); i++) { + for (i = 0; i < q->max_num_buffers; i++) { struct vb2_buffer *vb = vb2_get_buffer(q, i); if (!vb) @@ -2191,7 +2187,7 @@ static void __vb2_queue_cancel(struct vb2_queue *q) * call to __fill_user_buffer() after buf_finish(). That order can't * be changed, so we can't move the buf_finish() to __vb2_dqbuf(). */ - for (i = 0; i < vb2_get_num_buffers(q); i++) { + for (i = 0; i < q->max_num_buffers; i++) { struct vb2_buffer *vb; struct media_request *req; @@ -2618,7 +2614,7 @@ void vb2_core_queue_release(struct vb2_queue *q) __vb2_cleanup_fileio(q); __vb2_queue_cancel(q); mutex_lock(&q->mmap_lock); - __vb2_queue_free(q, vb2_get_num_buffers(q)); + __vb2_queue_free(q, 0, q->max_num_buffers); vb2_core_free_buffers_storage(q); q->is_busy = 0; mutex_unlock(&q->mmap_lock); From patchwork Thu Mar 14 15:32:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 780434 Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB89F74421; Thu, 14 Mar 2024 15:32:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430362; cv=none; b=SajWJGinzXiV4DWO1O+fYvJFzfMO9F/HDrRNJcYEf0LRda1eJQJTaEaH/y16TYisZ3TuBSsnoGT7ae5jhnIr6NSwJdgO9ffnXUZrrQlMe5nqeeEZSzZl0spfmCwh3oJbf16stqNacRNRrQJRdw/2gNsethirWpdVf7mXMNA5i24= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430362; c=relaxed/simple; bh=S8BY+YnoHLjL4nNyT8UzD8yD98Cb9AAqKgtqpMGHJiE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=g5r47p7b3lyI+1wtaSKXAvJBYBm4Y3eaaKMo6xlowvqUNCOLC0OMwPfaSD6tp6nYuz7Xs5EwltQhX3CzakseNTqaZYA8JBYsbO6FjW/VHXC6qJZ6a8/Cwbb0H4N9Ep3g8d6TMA1ra6LUj+Np4Fk8eK2cK6P3g5+gkmSCe0Yx2vg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=mbqxPj3H; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="mbqxPj3H" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1710430359; bh=S8BY+YnoHLjL4nNyT8UzD8yD98Cb9AAqKgtqpMGHJiE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mbqxPj3Hqp+HnBC/OztP1uSvHr3X9Hv0Oi2/teHC5NWUI9ab0Bk7eabZZNRw2mi+T L/HZpMkcc9RFLsXI7fLC1wY4HkHBlFFk52FViixLlXF8WdHV/3QUimRlEibzD12z88 FF1FAFnJB53vfUhjBfGnqlQCGbGHoFqYQXtumQekq01+weHEsJ1cttw/1PhW9aIMEU Xfs1LQpO9pI2o43TSoCZJwYvku2gr7Ec5lYB0CfKmNga2rM9OfYCqKGRGA8XzNjRIl YJbwMS5/e19Fa5gNMo6+udn4THCuCy+3KVks0saPdxby53S86/7u1UtTNc/4Ky8t1h D6COXqgBgHD8g== Received: from benjamin-XPS-13-9310.. (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: benjamin.gaignard) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 975B9378210F; Thu, 14 Mar 2024 15:32:38 +0000 (UTC) From: Benjamin Gaignard To: mchehab@kernel.org, tfiga@chromium.org, m.szyprowski@samsung.com, ezequiel@vanguardiasur.com.ar, p.zabel@pengutronix.de, hverkuil-cisco@xs4all.nl, nicolas@ndufresne.ca Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v21 8/9] media: v4l2: Add mem2mem helpers for REMOVE_BUFS ioctl Date: Thu, 14 Mar 2024 16:32:25 +0100 Message-Id: <20240314153226.197445-9-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240314153226.197445-1-benjamin.gaignard@collabora.com> References: <20240314153226.197445-1-benjamin.gaignard@collabora.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Create v4l2-mem2mem helpers for VIDIOC_REMOVE_BUFS ioctl and make test drivers use it. Signed-off-by: Benjamin Gaignard Reviewed-by: Mauro Carvalho Chehab --- changes in version 21: - In v4l2_m2m_ioctl_remove_bufs() check if the queue exists and if it type macth request type. - Do not set vidioc_remove_bufs for vim2m driver. drivers/media/test-drivers/vicodec/vicodec-core.c | 1 + drivers/media/test-drivers/vimc/vimc-capture.c | 1 + drivers/media/test-drivers/visl/visl-video.c | 1 + drivers/media/test-drivers/vivid/vivid-core.c | 1 + drivers/media/v4l2-core/v4l2-mem2mem.c | 15 +++++++++++++++ include/media/v4l2-mem2mem.h | 2 ++ 6 files changed, 21 insertions(+) diff --git a/drivers/media/test-drivers/vicodec/vicodec-core.c b/drivers/media/test-drivers/vicodec/vicodec-core.c index e13f5452b927..3e011fe62ae1 100644 --- a/drivers/media/test-drivers/vicodec/vicodec-core.c +++ b/drivers/media/test-drivers/vicodec/vicodec-core.c @@ -1345,6 +1345,7 @@ static const struct v4l2_ioctl_ops vicodec_ioctl_ops = { .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, + .vidioc_remove_bufs = v4l2_m2m_ioctl_remove_bufs, .vidioc_streamon = v4l2_m2m_ioctl_streamon, .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, diff --git a/drivers/media/test-drivers/vimc/vimc-capture.c b/drivers/media/test-drivers/vimc/vimc-capture.c index 97693561f1e4..ba7550b8ba7e 100644 --- a/drivers/media/test-drivers/vimc/vimc-capture.c +++ b/drivers/media/test-drivers/vimc/vimc-capture.c @@ -221,6 +221,7 @@ static const struct v4l2_ioctl_ops vimc_capture_ioctl_ops = { .vidioc_expbuf = vb2_ioctl_expbuf, .vidioc_streamon = vb2_ioctl_streamon, .vidioc_streamoff = vb2_ioctl_streamoff, + .vidioc_remove_bufs = vb2_ioctl_remove_bufs, }; static void vimc_capture_return_all_buffers(struct vimc_capture_device *vcapture, diff --git a/drivers/media/test-drivers/visl/visl-video.c b/drivers/media/test-drivers/visl/visl-video.c index b9a4b44bd0ed..f8d970319764 100644 --- a/drivers/media/test-drivers/visl/visl-video.c +++ b/drivers/media/test-drivers/visl/visl-video.c @@ -539,6 +539,7 @@ const struct v4l2_ioctl_ops visl_ioctl_ops = { .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, + .vidioc_remove_bufs = v4l2_m2m_ioctl_remove_bufs, .vidioc_streamon = v4l2_m2m_ioctl_streamon, .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, diff --git a/drivers/media/test-drivers/vivid/vivid-core.c b/drivers/media/test-drivers/vivid/vivid-core.c index 11b8520d9f57..771392f67dda 100644 --- a/drivers/media/test-drivers/vivid/vivid-core.c +++ b/drivers/media/test-drivers/vivid/vivid-core.c @@ -769,6 +769,7 @@ static const struct v4l2_ioctl_ops vivid_ioctl_ops = { .vidioc_expbuf = vb2_ioctl_expbuf, .vidioc_streamon = vb2_ioctl_streamon, .vidioc_streamoff = vb2_ioctl_streamoff, + .vidioc_remove_bufs = vb2_ioctl_remove_bufs, .vidioc_enum_input = vivid_enum_input, .vidioc_g_input = vivid_g_input, diff --git a/drivers/media/v4l2-core/v4l2-mem2mem.c b/drivers/media/v4l2-core/v4l2-mem2mem.c index 75517134a5e9..eb22d6172462 100644 --- a/drivers/media/v4l2-core/v4l2-mem2mem.c +++ b/drivers/media/v4l2-core/v4l2-mem2mem.c @@ -1386,6 +1386,21 @@ int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv, } EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs); +int v4l2_m2m_ioctl_remove_bufs(struct file *file, void *priv, + struct v4l2_remove_buffers *remove) +{ + struct v4l2_fh *fh = file->private_data; + struct vb2_queue *q = v4l2_m2m_get_vq(fh->m2m_ctx, remove->type); + + if (!q) + return -EINVAL; + if (q->type != remove->type) + return -EINVAL; + + return vb2_core_remove_bufs(q, remove->index, remove->count); +} +EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_remove_bufs); + int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *buf) { diff --git a/include/media/v4l2-mem2mem.h b/include/media/v4l2-mem2mem.h index 7f1af1f7f912..0af330cf91c3 100644 --- a/include/media/v4l2-mem2mem.h +++ b/include/media/v4l2-mem2mem.h @@ -867,6 +867,8 @@ int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv, struct v4l2_requestbuffers *rb); int v4l2_m2m_ioctl_create_bufs(struct file *file, void *fh, struct v4l2_create_buffers *create); +int v4l2_m2m_ioctl_remove_bufs(struct file *file, void *priv, + struct v4l2_remove_buffers *d); int v4l2_m2m_ioctl_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf); int v4l2_m2m_ioctl_expbuf(struct file *file, void *fh,