From patchwork Thu Nov 9 16:35:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 163509 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp561196vqs; Thu, 9 Nov 2023 08:43:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IGL1hLJYpn2kclmAYvrgyAVCbg8zNfEH/hXf+cM86kdKPsVKfaJz5VxHi1VJ+8jnHTRKJDB X-Received: by 2002:a05:6a20:3c8d:b0:181:7d6d:c0f1 with SMTP id b13-20020a056a203c8d00b001817d6dc0f1mr6448493pzj.37.1699548181167; Thu, 09 Nov 2023 08:43:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699548181; cv=none; d=google.com; s=arc-20160816; b=GHyZ8/NMh2U3vDf6XZTCa9fuLEJaPKInwyuUFclGvi2wcdH3kmLeVThNznp4kCVHG+ xKl6MUgicHgoVf9Rvt6LW49UmRGjTcJkdJMbjDuQluUHki2TEa8U3+c9HVpmHSFTseVf 9hQjt/DkLwFHsl2+pR41YEI9TSY4X826ogEqnhBhdB+EHaOiaAUkmCOttUlC0CMc8uzO v26Gx7Fv2DxVnJcSZ7YQil2TJDCIP9uvOMK0ZYCaWRBqxxknRlZYCBu9AGyGWVqftyX9 TAGkH7U4qD09j/Zvh7HQp9idO617lcfle1UQLDLQuKvSuXQQfkMT2L1L2ujzaEncZG5m 9lIg== 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=edf+ohzUpoaOEKpC5wNHHoMaAIUcaD5PUybt7hl08Rk=; fh=ceA81l8d2hZW6pWH7VE5TwJxtgXUzAubfWIMcikPA1M=; b=Wa/lVfLNyEwwf00ngYD3mMtGvOhrw79OhX93Hlr3XOCu8uYoIm42bwUMTFr97u8bp3 mdtM/0HZpGGoKWpsuotrzyiqzGw8Fr6Sp2e6WsUPkplrIO8j1rotsp8r/4SMLaCWV9PX KbJw3L5w8yZ/ZkZZuiiGnx7ZxhmyAmDzKDO1CNpJKJx5YjlCKc2f2+5lfGh/NIIMBBJZ Dfe8YdvDQ04LJtHxIc1Y9DLH0NXg2i+z+qP2mBC9aoJJFT2DlP1IeND/agxQCOQtL07p VdM/ysdTIXyeQPwlDyAhlcufWg7oExmLE9RB/P9Q+gcfsxYKIXbAkixSKYO2Z2wrjuML yx9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=RyxI0M7C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id p31-20020a056a000a1f00b006b7b42fe43asi16635867pfh.185.2023.11.09.08.43.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 08:43:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=RyxI0M7C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 6DFF182224D2; Thu, 9 Nov 2023 08:41:29 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344783AbjKIQk5 (ORCPT + 31 others); Thu, 9 Nov 2023 11:40:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344810AbjKIQkO (ORCPT ); Thu, 9 Nov 2023 11:40:14 -0500 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D8063865; Thu, 9 Nov 2023 08:38:44 -0800 (PST) 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 madras.collabora.co.uk (Postfix) with ESMTPSA id 7CA7866076B5; Thu, 9 Nov 2023 16:38:42 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1699547923; bh=q6teYNLwg2RL/8yABC/oMDatBUsEiFCKqynpiz0I5uk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RyxI0M7CXkMWitZiULa+ioTDod+cRABc1W75FIKDWzWvdeciyXmeJKgJ8XPVGWp8R 2v57Kl1GzIRf5QCEk232qphDy9akxcaklYSNaSYNvNsPIwbgfMAnPnj0GYFdVeL5Be hj82EqtqWC0FVyGJ8rhJk8pZr6Mgd7j+dAN0hxjF8a5uPrAgFT+wGKVV49KD7hP+hy xa9C/EXZgcW+VfFdN7Bf5dH2GO1nSNAupgG0sK1GFWnlrp6ApEhnzZpOPzSb7i5Ttu OuO1TmNhorujLWfcNINv61WVTJJCe+anjuEFx3WPz+K4JJvpOvVjUaUoFe3lyAMuY8 Iy3oi+wdSH2Gg== 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 v15 51/56] media: core: Rework how create_buf index returned value is computed Date: Thu, 9 Nov 2023 17:35:07 +0100 Message-Id: <20231109163512.179524-52-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231109163512.179524-14-benjamin.gaignard@collabora.com> References: <20231109163512.179524-14-benjamin.gaignard@collabora.com> MIME-Version: 1.0 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 (snail.vger.email [0.0.0.0]); Thu, 09 Nov 2023 08:41:29 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782105433967144797 X-GMAIL-MSGID: 1782105433967144797 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 | 22 ++++++++++++------- .../media/common/videobuf2/videobuf2-v4l2.c | 20 +++++++++++------ include/media/videobuf2-core.h | 5 ++++- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 8c1df829745b..6379f8171405 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -447,11 +447,12 @@ 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 int plane_sizes[VB2_MAX_PLANES], + unsigned int *first_index) { - unsigned int q_num_buffers = vb2_get_num_buffers(q); unsigned int buffer, plane; struct vb2_buffer *vb; + unsigned long index; int ret; /* @@ -459,7 +460,11 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, * in the queue 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 */ @@ -479,7 +484,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 */ @@ -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) { @@ -906,7 +911,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; @@ -980,7 +985,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] = { }; @@ -1042,7 +1048,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 440c3b1c18ec..d50ba1fb7f05 100644 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c @@ -799,11 +799,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); @@ -1031,15 +1036,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 5557d78b6f20..dbcdfcc4dc31 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -805,6 +805,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 @@ -821,7 +823,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