Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2398011rdg; Mon, 16 Oct 2023 03:18:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF/GhwpJGSNagVXQRuiXlnMu3bsc3ZUR/QK1ftKY08MKxJOtWH41GBXEvauQkHlhwP4Cy+Z X-Received: by 2002:a9d:4e91:0:b0:6b8:f588:2c79 with SMTP id v17-20020a9d4e91000000b006b8f5882c79mr37506078otk.1.1697451530999; Mon, 16 Oct 2023 03:18:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697451530; cv=none; d=google.com; s=arc-20160816; b=aY+UjjmLYKK31GXYhzjG7p0C93XyUjhYG2Wf7u4+cc7iY/wwmP1l1r3xADkTMlhxvz Pci+3ZieDgO4RTsL+DRiWztMQ+n01IIV+pR/qRA/M28zXXF6KMdnkfAx+ZlE7Mh8wzSv q650Zhg2fBqYEPl1HRpFZglICKWWyGQ18uljXbT/sUrXiTY1RD7HLjUNtqi/7iRhuukr iFtIwuWaaR7KZeO9Ea7WZdldc56xs2+6MHvA7jnKZbpdQ5XSxhYf8mpzD4Gd4Y+rVKsr AR9CqX6wzrQ7yycm+IoGjKx1c7oNHmSUd0MoZYZJwALU2yquttDXaKLuFVgHDu6UjvQl wd4Q== 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:references :cc:to:from:content-language:subject:user-agent:mime-version:date :message-id; bh=LqKJtm/QsuBwr0h6mxKx6BL+ADVMUUVfgNZ1o+22kdM=; fh=rvfjkWKWQ7H3dxwU/Mrzlf1SBsZ3Tuo/EBElQFetDNM=; b=IivJn/qiYCavauAwegrPrcuyvJZYjszvsgd5snuUQShm5IHuuWh7IeC5JE2zj8t/oa Et5xXj8Mt8LE1pZIDpjkbVrJIQWQzNk0mlp5U+qa1gZ1gHX527DJmz+krXdgjJ8BG7Fs ZcXxnPf2fh+vZnrFzHUORn1EoNiBum+TV0SCXxL3ZHnXAaP1eYostbbvm9y4ru91nSGl TWvRx5wpkH2QD81f4RaARzvynCyn/XHXE0ehBA0TuX4k2mWMop6EG/TRBwxhwnsgCVht qULbEDTshky+IhaMc305ZwSzooDk2bvqJOtCS3VWJxo84wYeHQHPLt3ALxfwHJGu+yeH U3Fw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id e4-20020a636904000000b005b3e6867a01si3982693pgc.427.2023.10.16.03.18.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 03:18:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 623648051908; Mon, 16 Oct 2023 03:18:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233276AbjJPKSg (ORCPT + 99 others); Mon, 16 Oct 2023 06:18:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232828AbjJPKSP (ORCPT ); Mon, 16 Oct 2023 06:18:15 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1E37131; Mon, 16 Oct 2023 03:18:04 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 896D5C433C8; Mon, 16 Oct 2023 10:18:00 +0000 (UTC) Message-ID: <1b49bfc4-808a-48fe-97b3-b49067e5e18e@xs4all.nl> Date: Mon, 16 Oct 2023 12:17:58 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v11 40/56] sample: v4l: Stop direct calls to queue num_buffers field Content-Language: en-US, nl 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: <20231012114642.19040-1-benjamin.gaignard@collabora.com> <20231012114642.19040-41-benjamin.gaignard@collabora.com> <21864437-bfdd-4d39-91fa-f24fc1c7cf97@xs4all.nl> In-Reply-To: <21864437-bfdd-4d39-91fa-f24fc1c7cf97@xs4all.nl> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email 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 (howler.vger.email [0.0.0.0]); Mon, 16 Oct 2023 03:18:48 -0700 (PDT) On 16/10/2023 10:23, Hans Verkuil wrote: > On 12/10/2023 13:46, Benjamin Gaignard wrote: >> Use vb2_get_num_buffers() to avoid using queue num_buffers field directly. >> >> Signed-off-by: Benjamin Gaignard >> --- >> samples/v4l/v4l2-pci-skeleton.c | 5 +++-- >> 1 file changed, 3 insertions(+), 2 deletions(-) >> >> diff --git a/samples/v4l/v4l2-pci-skeleton.c b/samples/v4l/v4l2-pci-skeleton.c >> index a61f94db18d9..a65aa9d1e9da 100644 >> --- a/samples/v4l/v4l2-pci-skeleton.c >> +++ b/samples/v4l/v4l2-pci-skeleton.c >> @@ -155,6 +155,7 @@ static int queue_setup(struct vb2_queue *vq, >> unsigned int sizes[], struct device *alloc_devs[]) >> { >> struct skeleton *skel = vb2_get_drv_priv(vq); >> + unsigned int q_num_bufs = vb2_get_num_buffers(vq); >> >> skel->field = skel->format.field; >> if (skel->field == V4L2_FIELD_ALTERNATE) { >> @@ -167,8 +168,8 @@ static int queue_setup(struct vb2_queue *vq, >> skel->field = V4L2_FIELD_TOP; >> } >> >> - if (vq->num_buffers + *nbuffers < 3) >> - *nbuffers = 3 - vq->num_buffers; >> + if (q_num_bufs + *nbuffers < 3) >> + *nbuffers = 3 - q_num_bufs; > > This should be dropped, and instead update q->min_buffers_needed from > 2 to 3. Actually, that's not quite true. I realized that there is a subtle bug in the vb2 core and a general misunderstanding of min_buffers_needed in a lot of drivers. The min_buffers_needed field describes the minimum number of buffers needed before the DMA engine can start. This is typically 0, 1 or 2. Once that many buffers have been queued, then start_streaming callback is called. With fewer buffers queued the DMA engine cannot start, so this represents a DMA engine limitation. Currently vb2 also uses this field as the minimum number of buffers to allocate. However, that should be one more, so min_buffers_needed+1: 'min_buffers_needed' buffers are in-flight, and you need one more that is owned by userspace, otherwise you would never be able to dequeue a buffer if you only created 'min_buffers_needed' buffers. But I noticed a lot of drivers that misinterpret this value as 'the minimum number of buffers to allocate', unrelated to any DMA engine limitations. This is most likely a bug, since this would unnecessarily delay the call to start_streaming(). In other words, it is a mess. I think my earlier advice to change min_buffers_needed and drop the check in queue_setup should be disregarded. If the min_buffers_needed value is >= the value checked in queue_setup, then you can drop the check. In all other cases it is safer to keep it. So in other words, this patch is fine. But e.g. patch 21 needs to keep the check (although with a fix: *nbuffers = 2 - q_num_bufs). When this work is done, then I think I need to take a close review at all the drivers that set min_buffers_needed and/or check the number of buffers in queue_setup and fix it properly. Likely we need two different fields: one for the minimum number of buffers that need to be allocated, and one for the minimum number of buffers that need to be queued before start_streaming can be called. But that raises the question how the 'minimum number of buffers that need to be allocated' would interact with deleting buffers. It's actually not all that easy. Regards, Hans > > Regards, > > Hans > >> >> if (*nplanes) >> return sizes[0] < skel->format.sizeimage ? -EINVAL : 0; >