From nobody Thu Nov 14 05:32:08 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6721ACDB482 for ; Tue, 17 Oct 2023 14:51:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344644AbjJQOvK (ORCPT ); Tue, 17 Oct 2023 10:51:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344090AbjJQOsm (ORCPT ); Tue, 17 Oct 2023 10:48:42 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C390D49; Tue, 17 Oct 2023 07:48:28 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:7205:da49:a7e8:59f8]) (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 madras.collabora.co.uk (Postfix) with ESMTPSA id F26EB6607332; Tue, 17 Oct 2023 15:48:25 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1697554106; bh=3YwRlaXqNfFfD+MkhjUYf3+OoF9vhFr7cj3UeTq3BQw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WgNpwa35YmRKKWMJ/6vL6ApWsGPxY6f4TpcWBOsjxV5RRByxgozahD6dKjRlKYfod Nb5QnEuJCls20kDwMLYj7XVoygXW/+ZZWynz2JkTxOf9wHDLqgCw7JMJSBp/uwb4v8 GoUl9asMzOCzHwFsuTqfQVKKwr5t+oPktsXUH2e2BA53ChVTa0/RumAMAdfOyfcqM5 M2dJv31cSBZutqi0zjtwAtpU6QToscSHFhhQt99LYGEAQke+/dCiyhE97cG69qO/9K mdnnkiL551WOdrv0klZqVQLVT8EDcZ/gbTGyYmsMHzaB9lHIpqcZmyrsxqq4N6/Lpd aoDG5jeUanu9Q== From: Benjamin Gaignard To: mchehab@kernel.org, tfiga@chromium.org, m.szyprowski@samsung.com, ming.qian@nxp.com, ezequiel@vanguardiasur.com.ar, p.zabel@pengutronix.de, gregkh@linuxfoundation.org, hverkuil-cisco@xs4all.nl, nicolas.dufresne@collabora.com Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-staging@lists.linux.dev, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v12 43/56] media: videobuf2: Be more flexible on the number of queue stored buffers Date: Tue, 17 Oct 2023 16:47:43 +0200 Message-Id: <20231017144756.34719-44-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231017144756.34719-1-benjamin.gaignard@collabora.com> References: <20231017144756.34719-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add 'max_num_buffers' field in vb2_queue struct to let drivers decide how many buffers could be stored in a queue. This require 'bufs' array to be allocated at queue init time and freed when releasing the queue. By default VB2_MAX_FRAME remains the limit. Signed-off-by: Benjamin Gaignard Signed-off-by: Hans Verkuil --- .../media/common/videobuf2/videobuf2-core.c | 41 +++++++++++++++---- .../media/common/videobuf2/videobuf2-v4l2.c | 6 +-- include/media/videobuf2-core.h | 10 ++++- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/medi= a/common/videobuf2/videobuf2-core.c index c5c5ae4d213d..72ef7179d80a 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -416,7 +416,7 @@ static void init_buffer_cache_hints(struct vb2_queue *q= , struct vb2_buffer *vb) */ static void vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *v= b, unsigned int index) { - WARN_ON(index >=3D VB2_MAX_FRAME || q->bufs[index]); + WARN_ON(index >=3D q->max_num_buffers || q->bufs[index]); =20 q->bufs[index] =3D vb; vb->index =3D index; @@ -449,9 +449,9 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum = vb2_memory memory, struct vb2_buffer *vb; int ret; =20 - /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */ + /* Ensure that the number of already queue + num_buffers is below q->max_= num_buffers */ num_buffers =3D min_t(unsigned int, num_buffers, - VB2_MAX_FRAME - q_num_buffers); + q->max_num_buffers - q_num_buffers); =20 for (buffer =3D 0; buffer < num_buffers; ++buffer) { /* Allocate vb2 buffer structures */ @@ -813,7 +813,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memo= ry memory, unsigned plane_sizes[VB2_MAX_PLANES] =3D { }; bool non_coherent_mem =3D flags & V4L2_MEMORY_FLAG_NON_COHERENT; unsigned int i; - int ret; + int ret =3D 0; =20 if (q->streaming) { dprintk(q, 1, "streaming active\n"); @@ -857,17 +857,22 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_me= mory memory, /* * Make sure the requested values and current defaults are sane. */ - WARN_ON(q->min_buffers_needed > VB2_MAX_FRAME); num_buffers =3D max_t(unsigned int, *count, q->min_buffers_needed); - num_buffers =3D min_t(unsigned int, num_buffers, VB2_MAX_FRAME); + num_buffers =3D min_t(unsigned int, num_buffers, q->max_num_buffers); memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); /* * Set this now to ensure that drivers see the correct q->memory value * in the queue_setup op. */ mutex_lock(&q->mmap_lock); + if (!q->bufs) + q->bufs =3D kcalloc(q->max_num_buffers, sizeof(*q->bufs), GFP_KERNEL); + if (!q->bufs) + ret =3D -ENOMEM; q->memory =3D memory; mutex_unlock(&q->mmap_lock); + if (ret) + return ret; set_queue_coherency(q, non_coherent_mem); =20 /* @@ -976,7 +981,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_= memory memory, bool no_previous_buffers =3D !q_num_bufs; int ret =3D 0; =20 - if (q_num_bufs =3D=3D VB2_MAX_FRAME) { + if (q->num_buffers =3D=3D q->max_num_buffers) { dprintk(q, 1, "maximum number of buffers already allocated\n"); return -ENOBUFS; } @@ -993,7 +998,13 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2= _memory memory, */ mutex_lock(&q->mmap_lock); q->memory =3D memory; + if (!q->bufs) + q->bufs =3D kcalloc(q->max_num_buffers, sizeof(*q->bufs), GFP_KERNEL); + if (!q->bufs) + ret =3D -ENOMEM; mutex_unlock(&q->mmap_lock); + if (ret) + return ret; q->waiting_for_buffers =3D !q->is_output; set_queue_coherency(q, non_coherent_mem); } else { @@ -1005,7 +1016,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb= 2_memory memory, return -EINVAL; } =20 - num_buffers =3D min(*count, VB2_MAX_FRAME - q_num_bufs); + num_buffers =3D min(*count, q->max_num_buffers - q_num_bufs); =20 if (requested_planes && requested_sizes) { num_planes =3D requested_planes; @@ -2465,6 +2476,12 @@ int vb2_core_queue_init(struct vb2_queue *q) /* * Sanity check */ + if (!q->max_num_buffers) + q->max_num_buffers =3D VB2_MAX_FRAME; + + /* The maximum is limited by offset cookie encoding pattern */ + q->max_num_buffers =3D min_t(unsigned int, q->max_num_buffers, MAX_BUFFER= _INDEX); + if (WARN_ON(!q) || WARN_ON(!q->ops) || WARN_ON(!q->mem_ops) || @@ -2474,6 +2491,10 @@ int vb2_core_queue_init(struct vb2_queue *q) WARN_ON(!q->ops->buf_queue)) return -EINVAL; =20 + if (WARN_ON(q->max_num_buffers > MAX_BUFFER_INDEX) || + WARN_ON(q->min_buffers_needed > q->max_num_buffers)) + return -EINVAL; + if (WARN_ON(q->requires_requests && !q->supports_requests)) return -EINVAL; =20 @@ -2519,7 +2540,9 @@ 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, q->max_num_buffers); + kfree(q->bufs); + q->bufs =3D NULL; q->num_buffers =3D 0; mutex_unlock(&q->mmap_lock); } diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/medi= a/common/videobuf2/videobuf2-v4l2.c index 7d798fb15c0b..f3cf4b235c1f 100644 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c @@ -627,7 +627,7 @@ struct vb2_buffer *vb2_find_buffer(struct vb2_queue *q,= u64 timestamp) * This loop doesn't scale if there is a really large number of buffers. * Maybe something more efficient will be needed in this case. */ - for (i =3D 0; i < vb2_get_num_buffers(q); i++) { + for (i =3D 0; i < q->max_num_buffers; i++) { vb2 =3D vb2_get_buffer(q, i); =20 if (!vb2) @@ -1142,7 +1142,7 @@ int _vb2_fop_release(struct file *file, struct mutex = *lock) =20 if (lock) mutex_lock(lock); - if (file->private_data =3D=3D vdev->queue->owner) { + if (!vdev->queue->owner || file->private_data =3D=3D vdev->queue->owner) { vb2_queue_release(vdev->queue); vdev->queue->owner =3D NULL; } @@ -1270,7 +1270,7 @@ void vb2_video_unregister_device(struct video_device = *vdev) */ get_device(&vdev->dev); video_unregister_device(vdev); - if (vdev->queue && vdev->queue->owner) { + if (vdev->queue) { struct mutex *lock =3D vdev->queue->lock ? vdev->queue->lock : vdev->lock; =20 diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 8f9d9e4af5b1..e77a397195f2 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -558,6 +558,7 @@ struct vb2_buf_ops { * @dma_dir: DMA mapping direction. * @bufs: videobuf2 buffer structures * @num_buffers: number of allocated/used buffers + * @max_num_buffers: upper limit of number of allocated/used buffers * @queued_list: list of buffers currently queued from userspace * @queued_count: number of buffers queued and ready for streaming. * @owned_by_drv_count: number of buffers owned by the driver @@ -619,8 +620,9 @@ struct vb2_queue { struct mutex mmap_lock; unsigned int memory; enum dma_data_direction dma_dir; - struct vb2_buffer *bufs[VB2_MAX_FRAME]; + struct vb2_buffer **bufs; unsigned int num_buffers; + unsigned int max_num_buffers; =20 struct list_head queued_list; unsigned int queued_count; @@ -1248,6 +1250,12 @@ static inline void vb2_clear_last_buffer_dequeued(st= ruct vb2_queue *q) static inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q, unsigned int index) { + if (!q->bufs) + return NULL; + + if (index >=3D q->max_num_buffers) + return NULL; + if (index < q->num_buffers) return q->bufs[index]; return NULL; --=20 2.39.2