Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1314424rdb; Mon, 4 Sep 2023 09:27:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFEZGDy6ua1wwoqRrJW3e+d4iTUxE7e9IGd9bzkWPlapy25mDEeSzktDa1ohJFceokr2gY5 X-Received: by 2002:a05:6a00:2193:b0:68a:4d66:caf with SMTP id h19-20020a056a00219300b0068a4d660cafmr10526196pfi.34.1693844832644; Mon, 04 Sep 2023 09:27:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693844832; cv=none; d=google.com; s=arc-20160816; b=kMYb80UxRnm1wGSvQVd186U2N2tbANEKjxB8IuGVVW6o1vTbZKIMjijq7xpr1EYgrW RZb8KNOCUwh4T1TgSgLfDDgyzOFuJV93pA3kj+hPVOE63B9aJmo0Mw5tOeN2zi65SH4X A/1cWOfckA46W7ncmDW+2Tl2rUU7Hq+sDCEb35fsMeqUoBLuDR8ojqa+zOdcXVmGJ4lh 5imy8P1cbVvwINvkjc/TQ15m0pnla+ry8PDunQ/rfhYE2kSoJMzVVim6kWDC3QnxxPiY FmXb9szI1SddWC8GxoBOlxWpbl8PcGAVTCXUEt9jcYHFQwUaG6uQXPlifJ79nqPpuyIE Z6hw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=JKmiMGdwozMXQrEhND7Pp+RULMgiMuqJIn4L7tuhh3g=; fh=rvfjkWKWQ7H3dxwU/Mrzlf1SBsZ3Tuo/EBElQFetDNM=; b=L670ZI0QozJELmODr2HABTCRqVwKm/CvDE1ObpS18SW0yNkvvUn95AhBom4Lyq8G0t F8P3r7hRZWaZJBIQ0CPgbRaOcjNHtP+sLAYzw2dqJTwkuch3Bbd/64fvRzRRDMbACFpE orjbLrvTjg6VqmzCkIo5PL6cNh9b15Temu8jxHFFUPYkt8DIvHcXx7tXn7vBhJhaWenz 97BGcY4fVARhGpe/fnBz3tbvclg+MFrUtKf9WLWKi0mcs/0rmXxMbm2NahHvGv4Au+lY wvw+cSTwE/zSMx5Rk897HWOR+6dx0NI3b3Dq5mnGAVaqVUNvH0pyLGgUUsGmF1bYVzmT IwIA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h190-20020a6383c7000000b00565ec06bf77si7730045pge.154.2023.09.04.09.26.59; Mon, 04 Sep 2023 09:27:12 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238375AbjIDOJW (ORCPT + 99 others); Mon, 4 Sep 2023 10:09:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232822AbjIDOJP (ORCPT ); Mon, 4 Sep 2023 10:09:15 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8AC9CDB; Mon, 4 Sep 2023 07:09:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 74E07B80E3B; Mon, 4 Sep 2023 14:09:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A9A6C433C8; Mon, 4 Sep 2023 14:09:05 +0000 (UTC) Message-ID: Date: Mon, 4 Sep 2023 16:09:03 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [PATCH v6 11/18] media: videobuf2: Be more flexible on the number of queue stored buffers Content-Language: en-US, nl To: Benjamin Gaignard , 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, 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 References: <20230901124414.48497-1-benjamin.gaignard@collabora.com> <20230901124414.48497-12-benjamin.gaignard@collabora.com> <37e5e418-c38a-b863-ffdf-72ce300cf227@xs4all.nl> <319d6103-26c3-bc02-3f80-90e653c8ee37@collabora.com> From: Hans Verkuil In-Reply-To: <319d6103-26c3-bc02-3f80-90e653c8ee37@collabora.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 Hi Benjamin, This patch can be folded into 11/18 to make it work properly. vb2_core_queue_release() is also called when the file handle of the video device is closed and it is also the owner of the currently allocated buffers. This will free q->bufs, but queue_init isn't called a second time for non-m2m devices. So move the allocation of q->bufs from queue_init to reqbufs/create_buffers. And when releasing the file handle we also check if there is no owner at all: in that case vb2_queue_release() must still be called to free q->bufs. Signed-off-by: Hans Verkuil --- drivers/media/common/videobuf2/videobuf2-core.c | 15 +++++++++++++-- drivers/media/common/videobuf2/videobuf2-v4l2.c | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index dc7f6b59d237..202d7c80ffd2 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -859,6 +859,12 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, return 0; } + if (!q->bufs) { + q->bufs = kcalloc(q->max_allowed_buffers, sizeof(*q->bufs), GFP_KERNEL); + if (!q->bufs) + return -ENOMEM; + } + /* * Make sure the requested values and current defaults are sane. */ @@ -985,6 +991,12 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, return -ENOBUFS; } + if (!q->bufs) { + q->bufs = kcalloc(q->max_allowed_buffers, sizeof(*q->bufs), GFP_KERNEL); + if (!q->bufs) + return -ENOMEM; + } + if (no_previous_buffers) { if (q->waiting_in_dqbuf && *count) { dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n"); @@ -2525,8 +2537,6 @@ int vb2_core_queue_init(struct vb2_queue *q) /* The maximum is limited by offset cookie encoding pattern */ q->max_allowed_buffers = min_t(unsigned int, q->max_allowed_buffers, BUFFER_INDEX_MASK); - 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); @@ -2552,6 +2562,7 @@ void vb2_core_queue_release(struct vb2_queue *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 8ba658ad9891..104fc5c4f574 100644 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c @@ -1148,7 +1148,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; } @@ -1276,7 +1276,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; -- 2.40.1