Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1807256rdb; Tue, 3 Oct 2023 01:12:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+rxQPpHlosTQiMWQX+TR/rwHRC9DmoC/coQ7wlzMflK4XsfgiTdBkG9kJYoGCb81RUPi6 X-Received: by 2002:a17:902:dac7:b0:1b6:6f12:502e with SMTP id q7-20020a170902dac700b001b66f12502emr15218238plx.49.1696320719689; Tue, 03 Oct 2023 01:11:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696320719; cv=none; d=google.com; s=arc-20160816; b=dpMeIWKYvBNx/hcmj/+XVzb1s2HmC/SqffDYwIIwkJmO1+WiqJ3ZQizqjP6dtcrS4j wUW0pv+i55z4yD7RRBPQUvh+/Q1cCPv5AUa/dnN8H0ed/5sZRDAiINB2yHQ2XJ1MCdmq d3yjx/yoRAwE3pHHsBdwPHkVyZadj9h6/pm1DQtVnEOWF9tt9ykCXGX33xLtLYdGQkR5 S1l0p/TzbT7esHFoZIm4Dz54oupf2qtapfwfZLYfy0cs4ccYNwWqO7f9mg1ri+KPHS38 TGbFKR+EJEEgZi7zi8oN2Dl41mdUxEpW1/E26yuhDyadM6Lmv+DHh//pOd2Bb39NsZq3 5sFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Og4LlbJBiymUpAzpII335idKUT/YD2hVLoI7j+fK+Do=; fh=ceA81l8d2hZW6pWH7VE5TwJxtgXUzAubfWIMcikPA1M=; b=U1bKRxPcJyOBje8pIGskF9uWiS32/gNMaoWWLrVFtnvwammOGqwZsEsKO1VZwLbqQt NML5oyyq/+Yh5vM875mVN0y8aL55lRpLJpx7J9+VWxG0R+md5VDQJg6+JBaKFgkoFwVq S2L/8FWeTcNDXRB86ZHYUUb6pBUT+OTG1fvg1uD/IQ/9sHXkqwdaWex6fFiY/mqf6bm3 It6O2MjdhuR8VuFbBh8Mg07pJjsIesNghFIDK2CVIE9Jr2oRLLuu2BlaYtXl0j0abs9u z2M3exIq6Xlm3rnlFekI6ucqOFR7JXN5xoCo6GWbVUP/YQ7abmtg3JVtSD3oADyml1Lq TeBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=O2cJokiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id d9-20020a170902cec900b001bc5a4f56fcsi942122plg.554.2023.10.03.01.11.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 01:11:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=O2cJokiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id E1AD78074530; Tue, 3 Oct 2023 01:11:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239372AbjJCIKu (ORCPT + 99 others); Tue, 3 Oct 2023 04:10:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239603AbjJCIID (ORCPT ); Tue, 3 Oct 2023 04:08:03 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 876CACCD; Tue, 3 Oct 2023 01:07:40 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:b6df:b784:6a0f:8451]) (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 73F44660735F; Tue, 3 Oct 2023 09:07:38 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1696320458; bh=wB4sQiMR9YdZLlurPoJdXQiGaX0CoGGpXiBFK9+XFCI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O2cJokiLRxG/YD7xdQKkS1vBqzB4psIO4k5MD3aEjjDk0Jh4oq75A77DkV8PChVC+ 0ixtP0ze4Cyw99Alaby7ILg3eVsKBcQ8iuz0RhrMWC1RkTMoZ2SV+4vU1ZJhEQh2ZS BSfHaRoDF8ROJuxk14DG+Jqm7ys3syfYpMKJ7pUFM0LPKCXNk5T5YhgdiCJMkUcIDu 6KrHXI66QTEpSiOrzOc+wTpf3DUbR2LYwLoUL8EywBAduEz6tK/vVhRorxA1cY2eWi 64aw7OcnNoJw4wo5IsmQ9vtjRL5+VmQ17XZVqUcrjn2r3fbM4MAkKLCBJxBplSN+c8 dlamxslWhVT7Q== 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 v10 49/54] media: core: Rework how create_buf index returned value is computed Date: Tue, 3 Oct 2023 10:06:58 +0200 Message-Id: <20231003080704.43911-50-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231003080704.43911-1-benjamin.gaignard@collabora.com> References: <20231003080704.43911-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 03 Oct 2023 01:11:36 -0700 (PDT) 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/media/common/videobuf2/videobuf2-core.c index 62e987ad9b33..21ea096864a5 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -441,15 +441,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; /* Ensure that the number of already queue + num_buffers is below q->max_num_buffers */ num_buffers = min_t(unsigned int, num_buffers, - q->max_num_buffers - q->num_buffers); + q->max_num_buffers - vb2_get_num_buffers(q)); + + index = vb2_get_num_buffers(q); + + *first_index = index; for (buffer = 0; buffer < num_buffers; ++buffer) { /* Allocate vb2 buffer structures */ @@ -469,7 +475,7 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, vb->planes[plane].min_length = plane_sizes[plane]; } - vb2_queue_add_buffer(q, vb, q->num_buffers + buffer); + vb2_queue_add_buffer(q, vb, index++); call_void_bufop(q, init_buffer, vb); /* Allocate video buffer memory for the MMAP type */ @@ -812,7 +818,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) { @@ -898,7 +904,7 @@ 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) { dprintk(q, 1, "memory allocation failed\n"); ret = -ENOMEM; @@ -972,7 +978,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] = { }; @@ -1034,7 +1041,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 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 = 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; + + ret = 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); @@ -1040,15 +1045,16 @@ int vb2_ioctl_create_bufs(struct file *file, void *priv, int res = vb2_verify_memory_type(vdev->queue, p->memory, p->format.type); - p->index = vdev->queue->num_buffers; fill_buf_caps(vdev->queue, &p->capabilities); validate_memory_flags(vdev->queue, p->memory, &p->flags); /* * If count == 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 == 0) + if (p->count == 0) { + p->index = vb2_get_num_buffers(vdev->queue); return res != -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_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..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_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 -- 2.39.2