From patchwork Tue Feb 6 08:02:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 197241 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1387797dyb; Tue, 6 Feb 2024 00:04:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IFeydgBzCq+yky45sUs9oYdXu2uLJlB6+0e8FFGscVRBxgJNYJ/5fsT/sUc/qFoU7FZzxQX X-Received: by 2002:aa7:d4d9:0:b0:55e:b943:6277 with SMTP id t25-20020aa7d4d9000000b0055eb9436277mr1190389edr.22.1707206693306; Tue, 06 Feb 2024 00:04:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707206693; cv=pass; d=google.com; s=arc-20160816; b=m3/GjkiIZGqf4F6FYWMixncP+mcDa5dRB8miefvrwo19ci1PSoCkP2VxXvR7RePYHC jy02s9ix7DAXThw4JUhJcqMbKJQ4s0UpwCdF1WkYD6kg7nzNEghiTWJxqdEKkQmGhYvI ddsSSE9Xo414u9vFIH00ca0qTm0T0E1DdxY+XVFvxdmg5TYSg8bZVQr3gZAP1L2ja7w8 87kHMse1A/xZ8aw9PFdNOlXJZtXLHZV6P9dcG9tLWHue+3BhmmwzirwcvAkNP6kueB/O bEkD2QDgp+xb6w88b7Ub+qcHlkIg+JbnVBWziZrYieDbFIsoaDDPEFnO7Eec/TcjD6XA gE7w== 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=lzBdo5uhF49H2M6hM8Rv+zG41LTjIeM6H1hyhWLsGcQ=; fh=9PBRoIEobQe55dPP8gmHjtD6wBzuQwfNi47ckgQ/Dmo=; b=bndqq54e7JgTbEqEgYzCG3uUnc48iUHyOJUGyxQO6xNlqKZe140iA1Rht+UkOzR7z+ +3cw0wA6gXqPUyJ70Z8a4ekKQRsGPpEgd9xH3fHPnow7I6No3AWpt4it/fF6p1p6Uohf CkTqwVzc3iFI9QlKvAroUrGvA6tq1oF4VKWlh5d5ZaigeKJu7X5pLZml9QR07//HqdJ7 FGEQodrpjhfd6LsFzU+yvRyeToKoGHt//Qxm7M1zzGozH8Mm5FtMaZ0MDUDuwdF38rPy 2A2T6VrkPuY56LtegEFoyWjmdBWS+mrOHiF+P7uFI8F9f6jTkhFEgXzbeR3BKP4l7Wle Ykzw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=rUaBzNaj; 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-54460-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54460-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com X-Forwarded-Encrypted: i=1; AJvYcCUofRj/HinCKVAQji5vWxsjNntl1jFC1tWE0IJ4oNqkLwBNg+nf+zJMIIgggHqOPvdwF36WJGaT+orcAXdJKqeSEyJYhA== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id bf11-20020a0564021a4b00b005600d2d3eb0si858674edb.405.2024.02.06.00.04.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 00:04:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54460-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=rUaBzNaj; 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-54460-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54460-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 am.mirrors.kernel.org (Postfix) with ESMTPS id BA9391F22860 for ; Tue, 6 Feb 2024 08:04:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3149812D178; 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="rUaBzNaj" 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 57C0012AAD8; Tue, 6 Feb 2024 08:02:34 +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=1707206556; cv=none; b=HbTkgxcpCpXjNQXV2v9fuP+stIT3TQFXoSwPe3Aq47dtqLZMNgmnPvYQ9FvFNOWZwsaVpUFvLr2EXi7qdnVvupOGG4s0vff0pRuSGXIa+gxsNje+GFGhkY8Qg9KrtzkXQPQZqgT8n0JRScfjf1b2np27rSRqBpJmrO/UfAeBQho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707206556; c=relaxed/simple; bh=QYDpAlGq9YsqF3WDMJ7UmmQwxC4xGnj+UUm3x0lfpSQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SpgzuM+fr40zpKkPaBOS9Hg7IKPofWPIWv/jovdVdQ0qRafzWysY5jMHZFmxt9nwk0j52EUjQdpjzDcuZ/K/1U9F0Foddj6cRJ6cd2n0MWkohBf+sRSPBWqfFV6azKFX2ZWB47/ke0zT1kyAswUgo/+J1ehjnWm5eYspPK0AZSA= 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=rUaBzNaj; 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=1707206546; bh=QYDpAlGq9YsqF3WDMJ7UmmQwxC4xGnj+UUm3x0lfpSQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rUaBzNajRYTJFDx21FaJSfXmsRp7bSfeMMG2WVxoMXrFbdv7+jk72F1j15NfdYJOY r04CtYXnXrj+aF4BxPdoTU+Llxz8j+eFPydwbEflCEGLTT7HeALN8tmRn7S3Z06rL1 sP7HaQaOQpFWFs6aNuA/JWqKCEbnBP+BjNZgjH2TO3Itb3ZbYP5WPMIOU1mBQU5J2f id3PLOIOhtb2tnCUs4snz5HI6zuco33lDd5jTGF1h90CuHa7Z1NrdfiVirNGWcWC3g JM1FT6CxIb/kKqRt+tvszKTZupan6N+bdW6qWI9R0XA+NV2NQEhX/bL0InBkvmRL5m zfx8DUMvZNUUg== 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 40AAB378206B; Tue, 6 Feb 2024 08:02:26 +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 2/9] videobuf2: Add min_reqbufs_allocation field to vb2_queue structure Date: Tue, 6 Feb 2024 09:02:12 +0100 Message-Id: <20240206080219.11951-3-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: 1790135965635939290 X-GMAIL-MSGID: 1790135965635939290 Add 'min_reqbufs_allocation' field in the vb2_queue structure so drivers can specify the minimum number of buffers to allocate when calling VIDIOC_REQBUFS. When initializing the queue, v4l2 core makes sure that the following constraints are respected: - the minimum number of buffers to allocate must be at least 2 because one buffer is used by the hardware while the other is being processed by userspace. -if the driver needs 'min_queued_buffers' in the queue before calling start_streaming(), then the minimum requirement is 'min_queued_buffers + 1' to keep at least one buffer available for userspace. Simplify __vb2_init_fileio() by using 'min_reqbufs_allocation' directly to avoid duplicating the minimum number of buffers to allocate computation. Signed-off-by: Benjamin Gaignard --- .../media/common/videobuf2/videobuf2-core.c | 38 +++++++++++-------- include/media/videobuf2-core.h | 15 +++++++- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index d8b3c04cb3b5..58c495b253ce 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -866,7 +866,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, /* * Make sure the requested values and current defaults are sane. */ - num_buffers = max_t(unsigned int, *count, q->min_queued_buffers); + num_buffers = max_t(unsigned int, *count, q->min_reqbufs_allocation); num_buffers = min_t(unsigned int, num_buffers, q->max_num_buffers); memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); /* @@ -918,7 +918,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, * There is no point in continuing if we can't allocate the minimum * number of buffers needed by this vb2_queue. */ - if (allocated_buffers < q->min_queued_buffers) + if (allocated_buffers < q->min_reqbufs_allocation) ret = -ENOMEM; /* @@ -2524,6 +2524,25 @@ int vb2_core_queue_init(struct vb2_queue *q) if (WARN_ON(q->supports_requests && q->min_queued_buffers)) return -EINVAL; + /* + * The minimum requirement is 2: one buffer is used + * by the hardware while the other is being processed by userspace. + */ + if (q->min_reqbufs_allocation < 2) + q->min_reqbufs_allocation = 2; + + /* + * If the driver needs 'min_queued_buffers' in the queue before + * calling start_streaming() then the minimum requirement is + * 'min_queued_buffers + 1' to keep at least one buffer available + * for userspace. + */ + if (q->min_reqbufs_allocation < q->min_queued_buffers + 1) + q->min_reqbufs_allocation = q->min_queued_buffers + 1; + + if (WARN_ON(q->min_reqbufs_allocation > q->max_num_buffers)) + return -EINVAL; + INIT_LIST_HEAD(&q->queued_list); INIT_LIST_HEAD(&q->done_list); spin_lock_init(&q->done_lock); @@ -2717,7 +2736,6 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) struct vb2_fileio_data *fileio; struct vb2_buffer *vb; int i, ret; - unsigned int count = 0; /* * Sanity check @@ -2738,18 +2756,8 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) if (q->streaming || vb2_get_num_buffers(q) > 0) return -EBUSY; - /* - * Start with q->min_queued_buffers + 1, driver can increase it in - * queue_setup() - * - * 'min_queued_buffers' buffers need to be queued up before you - * can start streaming, plus 1 for userspace (or in this case, - * kernelspace) processing. - */ - count = max(2, q->min_queued_buffers + 1); - dprintk(q, 3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n", - (read) ? "read" : "write", count, q->fileio_read_once, + (read) ? "read" : "write", q->min_reqbufs_allocation, q->fileio_read_once, q->fileio_write_immediately); fileio = kzalloc(sizeof(*fileio), GFP_KERNEL); @@ -2763,7 +2771,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) * Request buffers and use MMAP type to force driver * to allocate buffers by itself. */ - fileio->count = count; + fileio->count = q->min_reqbufs_allocation; fileio->memory = VB2_MEMORY_MMAP; fileio->type = q->type; q->fileio = fileio; diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index b317286a7b08..ef5500d14c09 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -550,9 +550,21 @@ struct vb2_buf_ops { * @start_streaming can be called. Used when a DMA engine * cannot be started unless at least this number of buffers * have been queued into the driver. - * VIDIOC_REQBUFS will ensure at least @min_queued_buffers + * VIDIOC_REQBUFS will ensure at least @min_queued_buffers + 1 * buffers will be allocated. Note that VIDIOC_CREATE_BUFS will not * modify the requested buffer count. + * @min_reqbufs_allocation: the minimum number of buffers to be allocated when + * calling VIDIOC_REQBUFS. Note that VIDIOC_CREATE_BUFS will *not* + * modify the requested buffer count and does not use this field. + * Drivers can set this if there has to be a certain number of + * buffers available for the hardware to work effectively. + * This allows calling VIDIOC_REQBUFS with a buffer count of 1 and + * it will be automatically adjusted to a workable buffer count. + * If set, then @min_reqbufs_allocation must be larger than + * @min_queued_buffers + 1. + * If this field is > 3, then it is highly recommended that the + * driver implements the V4L2_CID_MIN_BUFFERS_FOR_CAPTURE/OUTPUT + * control. */ /* * Private elements (won't appear at the uAPI book): @@ -619,6 +631,7 @@ struct vb2_queue { u32 timestamp_flags; gfp_t gfp_flags; u32 min_queued_buffers; + u32 min_reqbufs_allocation; struct device *alloc_devs[VB2_MAX_PLANES];