From patchwork Thu Aug 24 09:21:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 13363813 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C70C1C7113B for ; Thu, 24 Aug 2023 09:22:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=EeLAkZC0Uq6aJm1jI+obVigtd+Pzi4kupG4q6Vi4RUg=; b=D6qw1xhtR98qqP9QrNZhv+CeKp tufxaujb6ZM3kL/wkdtEEJPzCtzG4bNfvzsbYtHRKrxMwnjKfAiZc18LQUu3KmPU3yZwaGQugb73O 2TjYHAXFpfN3UwD9LBJjBhRaVLP43ilHrR7LNwLQtf6I6IELQN8IMG4dOTIHkjVM4RC36f8KLIhJ2 jHL/FPZ0FmUXGtBeuvvHoFuCwid/SjHzkjw61EcJ78HKh0ckayrX9mF3s/imQi3JmR7WjcX/d6PnL +rcnFl7oe9D/Ol9Ayiwh6BW+2DbVZfBnFgVWsKj1/HRT0rPN5Ay24wVx7BEJibCAv0vk0XYr6W1qb ryMmtOiQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qZ6Xa-002gJb-1x; Thu, 24 Aug 2023 09:22:10 +0000 Received: from madras.collabora.co.uk ([46.235.227.172]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qZ6XD-002g42-20; Thu, 24 Aug 2023 09:21:51 +0000 Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:672:c310:4c8a:3a97]) (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 2C6396607298; Thu, 24 Aug 2023 10:21:43 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1692868903; bh=GJNKi5NuuSLWK7PozA+X2l2tr8rdGL1VU7W/PWSM0Qg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CbsU2h3lrlKgQ+EQRrW9BO2PmU4sBpRUUMKjJ5yDvZlXAe5751vX0SNbK0P8zClve PXQ1rFMhIqBHRIgEl/3yuwZhn2VpGXBSEyis6htorrAx/4kAB4e0ztKhYcW+XjABAx ZMo26KoTk+gY+6qI4bDf/srreA9r06uvOe+VY5xFpA5IVQ02/fLLbnt0e05vgrIL0u FRCvcUonoN1kqZQ+8d5fG8vYc925yp3mDXx5fhreZ1sGzzC/bCsV82839Llc0i/6jr Ow7M+XeWY/bNjU+h2n7qBpM+qUzx4m1Te0UphNfuZREUP9W/A9xePZqP1+5QBkVa7m lrb2MwIqm6hXw== 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 v5 03/10] media: videobuf2: Be more flexible on the number of queue stored buffers Date: Thu, 24 Aug 2023 11:21:26 +0200 Message-Id: <20230824092133.39510-4-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230824092133.39510-1-benjamin.gaignard@collabora.com> References: <20230824092133.39510-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230824_022147_983950_9CBDB0A3 X-CRM114-Status: GOOD ( 16.42 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org Add 'max_allowed_buffers' field in vb2_queue struct to let drivers decide how many buffers could be stored in a queue. This request '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 --- .../media/common/videobuf2/videobuf2-core.c | 25 +++++++++++++------ include/media/videobuf2-core.h | 4 ++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 8aa13591b782..70e36389b704 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -411,7 +411,7 @@ static void init_buffer_cache_hints(struct vb2_queue *q, struct vb2_buffer *vb) */ static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, int index) { - if (index < VB2_MAX_FRAME && !q->bufs[index]) { + if (index < q->max_allowed_buffers && !q->bufs[index]) { q->bufs[index] = vb; vb->index = index; vb->vb2_queue = q; @@ -428,7 +428,7 @@ static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, int */ static void vb2_queue_remove_buffer(struct vb2_queue *q, struct vb2_buffer *vb) { - if (vb->index < VB2_MAX_FRAME) { + if (vb->index < q->max_allowed_buffers) { q->bufs[vb->index] = NULL; vb->vb2_queue = NULL; } @@ -449,9 +449,9 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, struct vb2_buffer *vb; int ret; - /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */ + /* Ensure that q->num_buffers+num_buffers is below q->max_allowed_buffers */ num_buffers = min_t(unsigned int, num_buffers, - VB2_MAX_FRAME - q->num_buffers); + q->max_allowed_buffers - q->num_buffers); for (buffer = 0; buffer < num_buffers; ++buffer) { /* Allocate vb2 buffer structures */ @@ -852,9 +852,9 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, /* * Make sure the requested values and current defaults are sane. */ - WARN_ON(q->min_buffers_needed > VB2_MAX_FRAME); + WARN_ON(q->min_buffers_needed > q->max_allowed_buffers); num_buffers = max_t(unsigned int, *count, q->min_buffers_needed); - num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME); + num_buffers = min_t(unsigned int, num_buffers, q->max_allowed_buffers); memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); /* * Set this now to ensure that drivers see the correct q->memory value @@ -970,7 +970,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, bool no_previous_buffers = !q->num_buffers; int ret; - if (q->num_buffers == VB2_MAX_FRAME) { + if (q->num_buffers == q->max_allowed_buffers) { dprintk(q, 1, "maximum number of buffers already allocated\n"); return -ENOBUFS; } @@ -999,7 +999,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, return -EINVAL; } - num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers); + num_buffers = min(*count, q->max_allowed_buffers - q->num_buffers); if (requested_planes && requested_sizes) { num_planes = requested_planes; @@ -2541,6 +2541,14 @@ int vb2_core_queue_init(struct vb2_queue *q) q->memory = VB2_MEMORY_UNKNOWN; + if (!q->max_allowed_buffers) + q->max_allowed_buffers = VB2_MAX_FRAME; + + /* The maximum is limited by offset cookie encoding pattern */ + q->max_allowed_buffers = min_t(unsigned int, q->max_allowed_buffers, MAX_BUFFERS); + + q->bufs = kcalloc(q->max_allowed_buffers, sizeof(*q->bufs), GFP_KERNEL); + if (q->buf_struct_size == 0) q->buf_struct_size = sizeof(struct vb2_buffer); @@ -2565,6 +2573,7 @@ void vb2_core_queue_release(struct vb2_queue *q) __vb2_queue_cancel(q); mutex_lock(&q->mmap_lock); __vb2_queue_free(q, q->num_buffers); + kfree(q->bufs); mutex_unlock(&q->mmap_lock); } EXPORT_SYMBOL_GPL(vb2_core_queue_release); diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 4b6a9d2ea372..ee9161b9fd64 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_allowed_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_allowed_buffers; struct list_head queued_list; unsigned int queued_count;