Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1367590rdb; Mon, 4 Sep 2023 11:16:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFaaZ7ul64S5rrO/2yvVblfRHKgM9Ih95j2BRYEDNw3LhczlwFG4xbjwCJOFEW1WY2roetE X-Received: by 2002:a05:6a20:938f:b0:14c:dc8e:9d3e with SMTP id x15-20020a056a20938f00b0014cdc8e9d3emr10674798pzh.26.1693851395126; Mon, 04 Sep 2023 11:16:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693851395; cv=none; d=google.com; s=arc-20160816; b=JQGYkC6n1kgy7OpyELqyJgTJWVDFwtBinQciVdwp26Fo68w197c7Q2n6gD8r1CzQyS xtiDeMO2YLRrUwXtHnAwA+rRA+3mLFPChh0D/jp0gNetdDy/9X97+51dmoe3cE8vKGXo fLLwVYjpRrrqfD85SbZ+QwtRuBSaHkHkPyZBB9eYaavDCIgAGIc1wtZgly8H/2wRF5hY EgbEmaSSwk3xc+zejCkk452eVjkh0sVVbsRcLL4+M1pgdIe6uqLkDhYqpIp/MNgfcQ5W TmuEbuwTht1XJNjM8R1JzTffmVjRLHjHWmgCskiPw9pGkzCLgpiDuN0ptW85Q0jk80t2 wLPg== 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 :content-language:references:cc:to:from:subject:user-agent :mime-version:date:message-id; bh=TErTK+bgTEpRzMxalfzQylKDcmksVFKwDfRaAOQIcvg=; fh=rvfjkWKWQ7H3dxwU/Mrzlf1SBsZ3Tuo/EBElQFetDNM=; b=eWCiGD2kVeGMKhn4IDT64HsU+Ap0hIbpS1FP2WgQ57/WJlJ9ZA3xypI0GAKWL/eqFj mZmDD5OH5Lh2cn/xENmP/bH88YZ5E3dJXZHFN4TxXmRaxkfVu8vx7cK9+0q0/XB05eF8 qHArcmcvc4rlcroX4dwyikSAtBbUiiWaAi3Ki91JHI7wnrEEdjhSbdoG+7udEJ3h4o8H UgWd+vAFO5Pn6VQ5ZHXmvDzaBpH6ixrfJoaIrZb7QAtF+hP4ndTq3TmY4IuxB86PKOiF Nas+rCu8prutv9jpSjSfIxYVZvzTnYi2CeDYE5OXzjysHz6cPe3iyGKlEAzJ5bx4Ff4+ tedQ== 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 c11-20020a65618b000000b00563dde13953si8147955pgv.739.2023.09.04.11.16.19; Mon, 04 Sep 2023 11:16:35 -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 S236448AbjIDPFc (ORCPT + 99 others); Mon, 4 Sep 2023 11:05:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240568AbjIDPF3 (ORCPT ); Mon, 4 Sep 2023 11:05:29 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35CD71B7; Mon, 4 Sep 2023 08:05:26 -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 dfw.source.kernel.org (Postfix) with ESMTPS id C1FEC61838; Mon, 4 Sep 2023 15:05:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D2FFC433C9; Mon, 4 Sep 2023 15:05:22 +0000 (UTC) Message-ID: Date: Mon, 4 Sep 2023 17:05:20 +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 From: Hans Verkuil 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> Content-Language: en-US, nl In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,NICE_REPLY_A,RCVD_IN_DNSWL_BLOCKED, 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 A follow-up to my follow-up... I realized that it is wise to do the allocation with mmap_lock held, since that is also held when freeing q->bufs. Signed-off-by: Hans Verkuil --- .../media/common/videobuf2/videobuf2-core.c | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index fe15e583b52a..dd25937c6dc8 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -818,7 +818,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"); @@ -859,12 +859,6 @@ 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. */ @@ -877,8 +871,14 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, * 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); /* @@ -984,19 +984,13 @@ 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 == q->max_allowed_buffers) { dprintk(q, 1, "maximum number of buffers already allocated\n"); 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"); @@ -1009,7 +1003,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 { -- 2.40.1