From patchwork Tue Feb 6 08:02:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 197246 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1389335dyb; Tue, 6 Feb 2024 00:08:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IGOVcKJYgzxerFRzHANPm43NHmXcKev5V7NUXzFMJK72ECENwGnnzBT9p/k7zSrH7EFrRsO X-Received: by 2002:a05:6871:10f:b0:219:5a9c:8f28 with SMTP id y15-20020a056871010f00b002195a9c8f28mr2295774oab.48.1707206898907; Tue, 06 Feb 2024 00:08:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707206898; cv=pass; d=google.com; s=arc-20160816; b=y6qEUESDXquS31z6R3CmLut3nEFfHts7cVe6sefq3LsHu6UJJtXndRxz7VLNQW+L+B gDjOIJlOQEnr8YwuqHKMjpfUZSm0YrUQ5QNOkjWvhcnBmmrqH+9MpQxEeoKZgNmBTL2y mIi9sXn9PZSxy+OUG8S5Eh/gxwKqeRiiUjrB3HHEFwVeUkdK92qPMmmHF1G7JbJfHYA1 2jytbs/1CAIHkVNKS6ygXLjO29CrGJqvlR7ilZiHlGa3bQymx7qfaHUBjZrBfrXbuHaZ WRK0Stnc2NubJxrPHHTyQJAiwpQ2Kex4+uesBemblqtUpGWDXRSldyhK6QfZj7Gh6mBr wKWQ== 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=WYi1IOB1F0W/Q1KnOLosm7CYJk6GpDkA7xYln8JSdCs=; fh=ziQkBQQPykLrjwRFbWUMsKPwOzrjjb9es8c5P2t8xBc=; b=nAdDNEQJJ/GBDsO8A0tnVWAdmzwfgLQVcx0VkLuBQKtLa46cD2rJWO9xbUtDW2O6Yi nWMfODY0CNXwIMKH3TgMgIpV6M07axoFISFhVSKkam6XmLRvFzEMqxtRErS79eFOmQfQ 26lRgtD4Jq53MYZYoIcZD9/3XIEnolKvtHDaGFhWtcFKLxc02AQ4yzMsfspo3rY+FERi CQUtfu03MaNOkHl2lZtcF2c6EYOtYpaXsr+OSCu0w1ZdHKprwFqQjzh9kPlWVM7PLI6G I5cdHMS2rE/fAxGe05bDK0mRIHKxZ7p9odwFQa0/IQtjsiL1fRTQPvYXItF8U8//PSaw d+/A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=pYOl9wai; 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-54461-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54461-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com X-Forwarded-Encrypted: i=1; AJvYcCWhVMP9GuHmGVwNLGxXtP965nEoSf/QbUSdhd5nYnY5rkJIlGxSZ8XWIJ2SnpIn13jSN9bqCleH6teOxvTvnp1YDjmHyw== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id c8-20020a63ef48000000b005d8e22b5258si1247535pgk.879.2024.02.06.00.08.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 00:08:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54461-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=pYOl9wai; 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-54461-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54461-ouuuleilei=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 E5F60B246B1 for ; Tue, 6 Feb 2024 08:05:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BB5DF12D75F; Tue, 6 Feb 2024 08:02:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="pYOl9wai" 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 52DD012B147; Tue, 6 Feb 2024 08:02:35 +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=1707206557; cv=none; b=eodgVnRNUbLQx4IFtPS2NPACUFkWDHg+1qUI6S+daN4DcWzuJSDrsJtZVmwxyYTbh31WwtOLWcm+uv49rb+4/WQWmXG57RNe6PYJV9Xb334ziAidNkkw+FPjJFalqThKqZbHnQAREVNBueH4fjlqdO5mIBYcQ8fE+2b+M1aKaJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707206557; c=relaxed/simple; bh=PVD6UbpGFZ3dXoMYr+PQDkBOztGFikpm2rdgUqdMX08=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UuNFxJsTXjYiCrffhbbfAm4dBrLjyEHTH430C2Ehi7y2zWx0k2wV72nRuKJc+gEzkIVKMDD5ECNYYQgIp0T5u4h0PIOLgj2eFQh+PJly6WzUmfnrP0Tm8OYdwTAgEmvANnc+buvv/dsunyTq8UKJ7XmiLKfxGDYgOu+z+4Qft/o= 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=pYOl9wai; 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=1707206547; bh=PVD6UbpGFZ3dXoMYr+PQDkBOztGFikpm2rdgUqdMX08=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pYOl9wai3W/2P+M0/Z3xC+cFBZvAXtE8ex/W87SajXhBqpPHrJ/KD9VUMJ+gJmUgr Ra2qrGIgD1oZNAWfwnUDGqtIQ4l79DWdz5QEhSeiWvK8PxhyWaIfqzmVZkkR3poQxi tlF+0ov7EkCHca3IvD6LO6JrkEPkkaHdbd2/rqZ75B/JQH0OVN7ubrTkKff4xJ+0Yw L23xLLkvwiG5oIxwrGLP0bUdEotQ7SnKFz+ZIR/zdjrCVsIJyIHfesMVB07e/RjEEs DYEx3GMYAqce0wMpaYGnBuQHbuhhwMGuSlrf88f+35R4Er/v5g2L6SvUzKo4VwIbb3 m8a9di2PIvy6Q== 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 60F813782076; Tue, 6 Feb 2024 08:02:27 +0000 (UTC) From: Benjamin Gaignard To: hverkuil@xs4all.nl, mchehab@kernel.org Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v19 4/9] media: core: Rework how create_buf index returned value is computed Date: Tue, 6 Feb 2024 09:02:14 +0100 Message-Id: <20240206080219.11951-5-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240206080219.11951-1-benjamin.gaignard@collabora.com> References: <20240206080219.11951-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790136181434166465 X-GMAIL-MSGID: 1790136181434166465 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 | 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 ef5500d14c09..2a9ca70e99c7 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -823,6 +823,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 @@ -839,7 +841,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