From patchwork Wed Jul 5 12:10:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 116095 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp1821744vqx; Wed, 5 Jul 2023 05:13:37 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7BByvV1eM23xhsT8/NGHbjkcX2PePZDMX5jwsl/ygU6lCCWMT09w+jFt0CUNgZQabbzccf X-Received: by 2002:a9d:7e81:0:b0:6a5:f786:d763 with SMTP id m1-20020a9d7e81000000b006a5f786d763mr18048911otp.5.1688559216854; Wed, 05 Jul 2023 05:13:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688559216; cv=none; d=google.com; s=arc-20160816; b=fo0Q3tjRXZNSjzedVJ+CstPaZ0hTVcydAqH9gk5Il283ZY26RKLXx9Amuf8fbFp5US Ku9WStPQNhVO5J7DrlHQJJ7N26iUhgV7luBx5/xSE6qPG1pc/CBaXvi6E35okvoy2NSV KoFlIV2/B56tejQOIcGfwo6HRHhGI70J9LMifacdutdsyIsLVf23TtTlRvSIDx+VQYMh FMhqeJvnHpZx4vI2rJ6bERXylC6okWQE6mglA6kbSlKyWBbsrKjiY/uNs80qtuE6/bRL TjGjmW7Zk9Uj4RbWBq470x/ZShmeWmCh9yMENHZjd86ib4DWpbM2H3sa/wvndJGeIRgY eHNQ== 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=JBagmau/2xEJCpME8IgMg/QnYPDGbs2TDGrCPX3Vyxk=; fh=ceA81l8d2hZW6pWH7VE5TwJxtgXUzAubfWIMcikPA1M=; b=WgFVwQE2zJ0Xf+Bbw4iTTov8QJzihkJfSweg8A52q/hrKLFm3njM9QFEPQfNAWu+nB NiAGE2uA0cOrboCl2dUnEdK62AjV3JyaLJen+15AVhYZ4XxkeR7euKzKs4baXJtjOiao XiFDWU6n9NkELBRA5wCkf10J5+TGzGGWuk82yQGV4m26BtfhXPb46r3uTZkMiQk5V8Be E/Ul0xmefAHl4faXelJiUG5X+uSnbyKITle79RRNCuH1SRf6EwtBMCztxnf6WA2g3dAK SgYRZXuD1MRz+NIFJfsVWXGgMXqpVEmWYPizGe9vo4+Dw1fh4jw8AzoCdsa+RI5zYFUc M+5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=ZcMU1y9E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 14-20020a630f4e000000b005501b2005fasi22226236pgp.161.2023.07.05.05.13.23; Wed, 05 Jul 2023 05:13:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=ZcMU1y9E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232001AbjGEMLJ (ORCPT + 99 others); Wed, 5 Jul 2023 08:11:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231965AbjGEMLH (ORCPT ); Wed, 5 Jul 2023 08:11:07 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 224B1D8; Wed, 5 Jul 2023 05:11:06 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:cbfb:e358:222c:d8c1]) (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 494256606FB0; Wed, 5 Jul 2023 13:11:04 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1688559064; bh=KRDWgSGJLJJkZ4YtMc9THdOTvbmNUOepA0+ihzaVNOw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZcMU1y9EUXHri9xNIMVSQgGfsIxsb4k9NexSZvNAzIxyrv92JPCztdcNThKs+rPTD yE9dBa4VclSkMCl72vxAU6gGnMBfjj/moi3iDAkxiu6fJqvNlt5tpyQKIJlTx725SB 697NDE9taZcN72QTr4rAutJ7efLEGPDIALQSWYuL0mY9mspeUKGzpu0sAL01a6DPP9 Ix3jBJKvB2vwaARdqD/OYAe8aKbv8lD02on7+e8sE+yLZZemPUYAZXfxDtyEON+bjJ 7pjP8kTY4uP3+Nov0Rq8yTDL0yxucgyqc243/SB41KjcwqfwETylY1h3hgd+lP0HR+ W9YAVLFvYswtw== 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 v4 02/10] media: videobuf2: Be more flexible on the number of queue stored buffers Date: Wed, 5 Jul 2023 14:10:48 +0200 Message-Id: <20230705121056.37017-3-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230705121056.37017-1-benjamin.gaignard@collabora.com> References: <20230705121056.37017-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770582669514018085?= X-GMAIL-MSGID: =?utf-8?q?1770582669514018085?= Add 'max_allowed_buffers' field in vb2_queue struct to let drivers decide how many buffers could be stored in a queue. This request 'bufs' array to be allocated at queue init time and freed when releasing the queue. By default VB2_MAX_FRAME remains the limit. Signed-off-by: Benjamin Gaignard --- version 4: - no Xarray here but instead a new field to let queues decide of their own maximum. .../media/common/videobuf2/videobuf2-core.c | 22 ++++++++++++------- include/media/videobuf2-core.h | 4 +++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 42fd3984c2bc..77e69b01d88d 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -413,7 +413,7 @@ static void init_buffer_cache_hints(struct vb2_queue *q, struct vb2_buffer *vb) */ static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, int index) { - if (index < VB2_MAX_FRAME && !q->bufs[index]) { + if (index < q->max_allowed_buffers && !q->bufs[index]) { q->bufs[index] = vb; vb->index = index; vb->vb2_queue = q; @@ -430,7 +430,7 @@ static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, int */ static void vb2_queue_remove_buffer(struct vb2_queue *q, struct vb2_buffer *vb) { - if (vb->index < VB2_MAX_FRAME) { + if (vb->index < q->max_allowed_buffers) { q->bufs[vb->index] = NULL; vb->vb2_queue = NULL; } @@ -451,9 +451,9 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, struct vb2_buffer *vb; int ret; - /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */ + /* Ensure that q->num_buffers+num_buffers is below q->max_allowed_buffers */ num_buffers = min_t(unsigned int, num_buffers, - VB2_MAX_FRAME - q->num_buffers); + q->max_allowed_buffers - q->num_buffers); for (buffer = 0; buffer < num_buffers; ++buffer) { /* Allocate vb2 buffer structures */ @@ -854,9 +854,9 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, /* * Make sure the requested values and current defaults are sane. */ - WARN_ON(q->min_buffers_needed > VB2_MAX_FRAME); + WARN_ON(q->min_buffers_needed > q->max_allowed_buffers); num_buffers = max_t(unsigned int, *count, q->min_buffers_needed); - num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME); + num_buffers = min_t(unsigned int, num_buffers, q->max_allowed_buffers); memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); /* * Set this now to ensure that drivers see the correct q->memory value @@ -972,7 +972,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, bool no_previous_buffers = !q->num_buffers; int ret; - if (q->num_buffers == VB2_MAX_FRAME) { + if (q->num_buffers == q->max_allowed_buffers) { dprintk(q, 1, "maximum number of buffers already allocated\n"); return -ENOBUFS; } @@ -1001,7 +1001,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, return -EINVAL; } - num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers); + num_buffers = min(*count, q->max_allowed_buffers - q->num_buffers); if (requested_planes && requested_sizes) { num_planes = requested_planes; @@ -2549,6 +2549,11 @@ int vb2_core_queue_init(struct vb2_queue *q) q->memory = VB2_MEMORY_UNKNOWN; + if (!q->max_allowed_buffers) + q->max_allowed_buffers = VB2_MAX_FRAME; + + q->bufs = kcalloc(q->max_allowed_buffers, sizeof(*q->bufs), GFP_KERNEL); + if (q->buf_struct_size == 0) q->buf_struct_size = sizeof(struct vb2_buffer); @@ -2573,6 +2578,7 @@ void vb2_core_queue_release(struct vb2_queue *q) __vb2_queue_cancel(q); mutex_lock(&q->mmap_lock); __vb2_queue_free(q, q->num_buffers); + kfree(q->bufs); mutex_unlock(&q->mmap_lock); } EXPORT_SYMBOL_GPL(vb2_core_queue_release); diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 4b6a9d2ea372..ee9161b9fd64 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -558,6 +558,7 @@ struct vb2_buf_ops { * @dma_dir: DMA mapping direction. * @bufs: videobuf2 buffer structures * @num_buffers: number of allocated/used buffers + * @max_allowed_buffers: upper limit of number of allocated/used buffers * @queued_list: list of buffers currently queued from userspace * @queued_count: number of buffers queued and ready for streaming. * @owned_by_drv_count: number of buffers owned by the driver @@ -619,8 +620,9 @@ struct vb2_queue { struct mutex mmap_lock; unsigned int memory; enum dma_data_direction dma_dir; - struct vb2_buffer *bufs[VB2_MAX_FRAME]; + struct vb2_buffer **bufs; unsigned int num_buffers; + unsigned int max_allowed_buffers; struct list_head queued_list; unsigned int queued_count;