Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp185219lqb; Thu, 14 Mar 2024 08:34:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX/YgNKC0DdHFYTfOJ9s3R2G1WHKQHf9u8vb+e4NUW+uQaQUxdJGKeZ8MTAz/TtRoYCYaL8zmli9QibulTpD9rFKjo9MsFt+u/hYLtMag== X-Google-Smtp-Source: AGHT+IHLuSzgndycys97TncZ5EF8Akyqx3r2EMuoshdmG2wPUqGlx6pJl5jrzW3DvodOYw5kjL4v X-Received: by 2002:a92:c70b:0:b0:366:3766:6c4b with SMTP id a11-20020a92c70b000000b0036637666c4bmr505632ilp.16.1710430449987; Thu, 14 Mar 2024 08:34:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710430449; cv=pass; d=google.com; s=arc-20160816; b=iV4X3C5d+XNEO/uoA1TzJiKwntkWDmdFS+gtgey4BJwQhWgSObcL3zb4jxeSLeI4da +qJNWJ3AIz/IHTN9+bk0dQJYPoM2d3sp4J8FPEwJgUmhBQh7+ZLh0svwdyDe2bX71zlv 5ife8QhF7dtYs8YaAPSRzQ7EKvIjgoM8Zk83kwVU7Dl1LsOlP0ugUXi8sVTMXOcEPBR2 80nr3CPwI/ygAeUhgIQvGNfiXyuyxy4z0sZaO+57zG6OR7NnLfwVUPeVcqropTb9pMEI XzH1dqGTaMPKFkaAFm6/ukFdElBtoDuOC9gyxsvVeigTo4cP1IEYdvwneMh53W4WT6pn bfIQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Gmr/TEwr9G5kIWTTrQS2OFIyiBKx5dZcN9qpV1MfCKM=; fh=zOBpKhMTeRbrgfYir35sIvSkMQrxOs+orb+lNlEOd4k=; b=EwtevU/knWmFw089BCYwFGrEsC/LggmHM45JB5Ual90G+RxM4CTODYCtv/gfSdfQvt Rs13oRcFuANsfABSZUO+RkL3fr+YKNI9jZAgcq2Umn9gvNhBQz1lCkv2kFGOg8QK9zQY Wm3L2tsnHl2IQOidOnIgV5y8l7NKpsZjlBcxuxYyb+Far/CSF4Wx2fBXo5eEA2D0o15I 0hCYzMdkHEasspR/J56GBNDhjr6M0I7PTfMvO1nk6KbrFylSMsSK4v/SAz7X2ZFmhISS IQTx3qrdY/23kZdVmtvaBdeDQOCQpFHL3uixw+Y0tEVogq0EKw7Sq3t0x60/TAQAWTci DsIg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=x5DNPNbB; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-103501-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103501-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id k126-20020a632484000000b005e2b17c97efsi794873pgk.28.2024.03.14.08.34.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 08:34:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-103501-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=x5DNPNbB; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-103501-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103501-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 4536EB2183F for ; Thu, 14 Mar 2024 15:33:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8F06774416; Thu, 14 Mar 2024 15:32:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="x5DNPNbB" 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 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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 -- 2.40.1