From patchwork Thu Sep 14 13:32:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 139885 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp676153vqi; Thu, 14 Sep 2023 16:02:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGCWZQPodUbOcxfAYapdtN8n400cQXk105v+lU5cK1XDlH8mQUVrEImCDRRaSOjhyKeBVKz X-Received: by 2002:a05:6a20:13d0:b0:137:7198:af9b with SMTP id ho16-20020a056a2013d000b001377198af9bmr127460pzc.56.1694732556751; Thu, 14 Sep 2023 16:02:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694732556; cv=none; d=google.com; s=arc-20160816; b=e1janOnFvRMXPXhHQOvTpy7PC6o9JGybgYxOu4296kkKZLyOUo0ZisOsP/jQqbJ6tH JO5wMRd381xWG5nt+iZkmKdwF6a7AiIkTNYsQhd1+4EMofYh9+iuFqs7iE6eDzyo9HBo dNcVmU93cBhbMHzODYUyPwdY1LggFhJoo2ok9+F1j9SdHL9UT3pJjJmPN04FXGevsuef YJ/+nTL1IM+YN37y63Cp7Gq2am901SqavUBJOY+InIMw+I5iuhkcSdvcj9svt0x8cvLW MEAQM4UHqXvC0B81w5vXdqCRaiXSZZvvXlhRfPCmkHTsZXHG7iNO520vjl8k/rbAkyU8 gR3Q== 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=ZT6qpEIWTsFbh9WlxSnIhuZFGBr6DbFddhHhWu0eaq0=; fh=ceA81l8d2hZW6pWH7VE5TwJxtgXUzAubfWIMcikPA1M=; b=AqI0/m7ELEGEgo7o+RlAS66M3Rs4KkEkZtf9QNN+iayjJu/+y7WTi6T1e7Lj/9VxVE aEO5wDUyClqanKPpWJb1E57rw2x54J2wU1OKYOQ5yxg0LTpLA27770sNAboKFnvKX+Vd Qv5orzCRIyn5Pw3J6Do98cdysP1mjJYh8cEWahMfuathTnEo8IbGjzjk1zpVJueSrSNS l9HQKMMo18f/06kUT/rtTrYucruPtwHeh9WhJ+q3brGYNj9Vi9duahdTYsvuOMm64U3I gTXwpp9RlWyeo8mc30eI2IDVLXYcr8fLadpHpkjmXoVM3Llp+Sp3rukNvLRhK3OUzRCc nOHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=BkSQDKxJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bv9-20020a056a00414900b00690228b1d45si2262935pfb.342.2023.09.14.16.02.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 16:02:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=BkSQDKxJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 C024181CC868; Thu, 14 Sep 2023 06:34:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239253AbjINNeJ (ORCPT + 35 others); Thu, 14 Sep 2023 09:34:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239147AbjINNdr (ORCPT ); Thu, 14 Sep 2023 09:33:47 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE26D1FE1; Thu, 14 Sep 2023 06:33:38 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:36f2:37bd:ccbb:373f]) (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 612806607365; Thu, 14 Sep 2023 14:33:37 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1694698417; bh=h2BeQ/NsbyEyCup+psJf2BQPK31aCDVGYIe1zsQ/Ukk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BkSQDKxJG9PHx15MgEGbhvOhxGON2OX+XrIsfFLfmVHIQXRmAtX7C4+ObDqVTJ4NP fuYnRhdYpFUEzaRxavWzKe/MTH/oYmejP7qYPmUCkPIVBg3u3dmn/v+cMtlXfpdWxv iujzhY7/Jtb4Kv5Em3V+MChHN1zK8UNQ2iZTY0ySybOzg11WIlF83hFaO9zkHI3CZb 2v78ToQRMQRFJ00eTdYFbnJFCaYYBe3H6RZJLiRcdX7zPznLTqpMX1iph6ST3pQ2Ao JmeBykpk0I1C4vLbh86E1irHwchpS87Q7vLnzEj/+TZLlji7BCVqqjD+vzKr/JumjD mOaJWI+aEQWNw== 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 v7 12/49] media: videobuf2: Be more flexible on the number of queue stored buffers Date: Thu, 14 Sep 2023 15:32:46 +0200 Message-Id: <20230914133323.198857-13-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230914133323.198857-1-benjamin.gaignard@collabora.com> References: <20230914133323.198857-1-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, 14 Sep 2023 06:34:18 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777055885196705683 X-GMAIL-MSGID: 1777055885196705683 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 Signed-off-by: Hans Verkuil Signed-off-by: Hans Verkuil --- .../media/common/videobuf2/videobuf2-core.c | 40 ++++++++++++++----- .../media/common/videobuf2/videobuf2-v4l2.c | 4 +- include/media/videobuf2-core.h | 4 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index afe76577acc1..ee4df7c68397 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -411,7 +411,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, unsigned 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; @@ -428,7 +428,7 @@ static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, uns */ 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; } @@ -449,9 +449,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 */ @@ -814,7 +814,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, unsigned plane_sizes[VB2_MAX_PLANES] = { }; bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; unsigned int i; - int ret; + int ret = 0; if (q->streaming) { dprintk(q, 1, "streaming active\n"); @@ -858,17 +858,23 @@ 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 * in the queue_setup op. */ mutex_lock(&q->mmap_lock); + if (!q->bufs) + q->bufs = kcalloc(q->max_allowed_buffers, sizeof(*q->bufs), GFP_KERNEL); + if (!q->bufs) + ret = -ENOMEM; q->memory = memory; mutex_unlock(&q->mmap_lock); + if (ret) + return ret; set_queue_coherency(q, non_coherent_mem); /* @@ -974,9 +980,9 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, unsigned plane_sizes[VB2_MAX_PLANES] = { }; bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; bool no_previous_buffers = !q->num_buffers; - int ret; + int ret = 0; - 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; } @@ -993,7 +999,13 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, */ mutex_lock(&q->mmap_lock); q->memory = memory; + if (!q->bufs) + q->bufs = kcalloc(q->max_allowed_buffers, sizeof(*q->bufs), GFP_KERNEL); + if (!q->bufs) + ret = -ENOMEM; mutex_unlock(&q->mmap_lock); + if (ret) + return ret; q->waiting_for_buffers = !q->is_output; set_queue_coherency(q, non_coherent_mem); } else { @@ -1005,7 +1017,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; @@ -2515,6 +2527,12 @@ 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; + + /* The maximum is limited by offset cookie encoding pattern */ + q->max_allowed_buffers = min_t(unsigned int, q->max_allowed_buffers, BUFFER_INDEX_MASK + 1); + if (q->buf_struct_size == 0) q->buf_struct_size = sizeof(struct vb2_buffer); @@ -2539,6 +2557,8 @@ 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); + q->bufs = NULL; mutex_unlock(&q->mmap_lock); } EXPORT_SYMBOL_GPL(vb2_core_queue_release); diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/media/common/videobuf2/videobuf2-v4l2.c index f460cac560f6..87c2d5916960 100644 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c @@ -1156,7 +1156,7 @@ int _vb2_fop_release(struct file *file, struct mutex *lock) if (lock) mutex_lock(lock); - if (file->private_data == vdev->queue->owner) { + if (!vdev->queue->owner || file->private_data == vdev->queue->owner) { vb2_queue_release(vdev->queue); vdev->queue->owner = NULL; } @@ -1284,7 +1284,7 @@ void vb2_video_unregister_device(struct video_device *vdev) */ get_device(&vdev->dev); video_unregister_device(vdev); - if (vdev->queue && vdev->queue->owner) { + if (vdev->queue) { struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock; diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index cd3ff1cd759d..97153c69583f 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;