From nobody Thu Nov 14 05:13:02 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 D09AAE784B6 for ; Mon, 2 Oct 2023 12:08:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237455AbjJBMIx (ORCPT ); Mon, 2 Oct 2023 08:08:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237161AbjJBMHE (ORCPT ); Mon, 2 Oct 2023 08:07:04 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C890C1B7; Mon, 2 Oct 2023 05:06:51 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:9537:67ca:c85e:d0ae]) (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 2B3956607313; Mon, 2 Oct 2023 13:06:50 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1696248410; bh=U9ROfIR67B3sHpLRNcsAThJry0XaT/ic5m5AuSEVfvE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h0XuPJn4AfQCknWERmaVsWi/kSQVauUkKkGjuStU+RF3ShKl7tRLwYAUeP8RcQHo1 4dL0JzBtDwr3VTCVZ/82mLb2HqH+de2lL+kuZp+VgV/U4FZJsqctiwBhWxwOGCHiF+ +CcN6r1StYB0S04mCOn3Efh420QITN5wvZpHwpKujoV1ZBO4pqLsMPIo3AJzk+vihf n1IeHLobnvoL6ZzgZiIQiFQ97wjArR4o0jAkKaxIgFVYmSuTAKoWT61T8vsr9CeXcY Gyhhrqo7Z8A51tYccQtiWS3ikLHSYIGa1oUhaX6M0YPQmRuFOSBRO8WPPfDriVKk3B 1ArrYZm+SNHtg== 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 v9 48/53] media: core: Rework how create_buf index returned value is computed Date: Mon, 2 Oct 2023 14:06:12 +0200 Message-Id: <20231002120617.119602-49-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231002120617.119602-1-benjamin.gaignard@collabora.com> References: <20231002120617.119602-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" When DELETE_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 | 21 ++++++++++++------- .../media/common/videobuf2/videobuf2-v4l2.c | 20 +++++++++++------- include/media/videobuf2-core.h | 5 ++++- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/medi= a/common/videobuf2/videobuf2-core.c index 8da8a7dfff44..92de8af58bdc 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -440,15 +440,21 @@ static void vb2_queue_remove_buffer(struct vb2_buffer= *vb) */ 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 plane_sizes[VB2_MAX_PLANES], + unsigned int *first_index) { unsigned int buffer, plane; struct vb2_buffer *vb; + unsigned long index; int ret; =20 /* Ensure that the number of already queue + num_buffers is below q->max_= num_buffers */ num_buffers =3D min_t(unsigned int, num_buffers, - q->max_num_buffers - q->num_buffers); + q->max_num_buffers - vb2_get_num_buffers(q)); + + index =3D vb2_get_num_buffers(q); + + *first_index =3D index; =20 for (buffer =3D 0; buffer < num_buffers; ++buffer) { /* Allocate vb2 buffer structures */ @@ -468,7 +474,7 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum = vb2_memory memory, vb->planes[plane].min_length =3D plane_sizes[plane]; } =20 - vb2_queue_add_buffer(q, vb, q->num_buffers + buffer); + vb2_queue_add_buffer(q, vb, index++); call_void_bufop(q, init_buffer, vb); =20 /* Allocate video buffer memory for the MMAP type */ @@ -811,7 +817,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memo= ry memory, unsigned int q_num_bufs =3D vb2_get_num_buffers(q); unsigned plane_sizes[VB2_MAX_PLANES] =3D { }; bool non_coherent_mem =3D flags & V4L2_MEMORY_FLAG_NON_COHERENT; - unsigned int i; + unsigned int i, first_index; int ret =3D 0; =20 if (q->streaming) { @@ -897,7 +903,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memo= ry memory, =20 /* Finally, allocate buffers and video memory */ allocated_buffers =3D - __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes); + __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes, &firs= t_index); if (allocated_buffers =3D=3D 0) { dprintk(q, 1, "memory allocation failed\n"); ret =3D -ENOMEM; @@ -971,7 +977,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 =3D 0, num_buffers, allocated_buffers; unsigned plane_sizes[VB2_MAX_PLANES] =3D { }; @@ -1033,7 +1040,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb= 2_memory memory, =20 /* Finally, allocate buffers and video memory */ allocated_buffers =3D __vb2_queue_alloc(q, memory, num_buffers, - num_planes, plane_sizes); + num_planes, plane_sizes, first_index); if (allocated_buffers =3D=3D 0) { dprintk(q, 1, "memory allocation failed\n"); ret =3D -ENOMEM; diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/medi= a/common/videobuf2/videobuf2-v4l2.c index a4ebef82d94e..6d9f1be41a19 100644 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c @@ -808,11 +808,16 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_= create_buffers *create) for (i =3D 0; i < requested_planes; i++) if (requested_sizes[i] =3D=3D 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; + + ret =3D vb2_core_create_bufs(q, create->memory, + create->flags, + &create->count, + requested_planes, + requested_sizes, + &create->index); + return ret; } EXPORT_SYMBOL_GPL(vb2_create_bufs); =20 @@ -1040,15 +1045,16 @@ int vb2_ioctl_create_bufs(struct file *file, void *= priv, int res =3D vb2_verify_memory_type(vdev->queue, p->memory, p->format.type); =20 - p->index =3D vdev->queue->num_buffers; fill_buf_caps(vdev->queue, &p->capabilities); validate_memory_flags(vdev->queue, p->memory, &p->flags); /* * If count =3D=3D 0, then just check if memory and type are valid. * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0. */ - if (p->count =3D=3D 0) + if (p->count =3D=3D 0) { + p->index =3D vb2_get_num_buffers(vdev->queue); return res !=3D -EBUSY ? res : 0; + } if (res) return res; if (vb2_queue_is_busy(vdev->queue, file)) diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index dffb9647d4d1..d433bf84e30c 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -803,6 +803,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memo= ry 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..first+count] * * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is * called internally by VB2 by an API-specific handler, like @@ -819,7 +821,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memo= ry 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); =20 /** * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace --=20 2.39.2