Received: by 2002:a05:7412:8d11:b0:fa:4934:9f with SMTP id bj17csp593752rdb; Mon, 15 Jan 2024 07:17:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IGesVlF3X6g6N0vjyFbvj1EKK36zh3cwBiwTCsU3QOAB+KzaVHfHIk9o4dIEJv+YeTw3IlB X-Received: by 2002:a05:6358:56:b0:175:d8d2:5b2c with SMTP id 22-20020a056358005600b00175d8d25b2cmr551551rwx.31.1705331853549; Mon, 15 Jan 2024 07:17:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705331853; cv=none; d=google.com; s=arc-20160816; b=hm4mlL0HpgqWolZNMhvGGiycy8MsFthgdz6n3M9PTjCOMkwZ4tt83uaadXbcLIoZ6D cM61u7TG+eD77EPtwkfvhwBzkf5XXqh9EshBqV51U0Vmrm300T5JCboy68EGno+QbmrW KaMotfv7aeK9NvQR+r656ycUHvvZ5XmfV0vt+K1zuDH/Z53ys1zNFGt+hZTQTQvsH9PH UCRSeFEUGMa4CBdtMsRFUokJ0THKGpIGMHxtStTdXd3Gca+aElpkwj+zd5HcA3vENqZP wo1ds2a1V5TRcwff1K1gzjrUaqSwhVaNoHuvsp1QcLQd296sX8nGt+1yLnqLWdSK7DrB MIDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id; bh=KLFwM7DNVbzorweyJaIKjY7u8WGA03SvmgoDZjcwXQ8=; fh=JA5CkwWQaCWbhBvBUXS2prPFrwCGT4l48Srp7tLe1bE=; b=iFyvUapA0GdBzE/U776DjqL05ZbE60kLlP0C8XeKYTv5cW3Hf0zIoC/Muw7Fij9hZQ zi+2w138EFZhfdiaSrHo2YnDmp1qOeMxWuYeMpCCxGIO92Vk+LA9Ag9O60ehVPWu3rr8 ycwFpz+Mu8h7FSaZ2oEdvSz3ZML+//Vp+M6gqHMqjsy7Ma6UNASB2ZGRZ+s0onFmm6ES C8xlAf4HrPfKcenSuvqgqJBM4KhjHTOUr7gNuGCIijfhF7IOHIlecAXVrXsNnyQ+PmM4 5HFLR29LmlH5GN/uP8uVwTHYuG8sIN7pH0oGQ06SdEk3LKEFKJGWV8GURZnHVt3OIdxD HO6w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-26165-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-26165-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id e15-20020a63744f000000b005ca5b61cac5si9305616pgn.121.2024.01.15.07.17.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 07:17:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-26165-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-26165-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-26165-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 2791E282A58 for ; Mon, 15 Jan 2024 15:17:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0D88417748; Mon, 15 Jan 2024 15:17:23 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 330E3171C1; Mon, 15 Jan 2024 15:17:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 633B6C433F1; Mon, 15 Jan 2024 15:17:20 +0000 (UTC) Message-ID: Date: Mon, 15 Jan 2024 16:17:18 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v16 3/8] media: core: Rework how create_buf index returned value is computed Content-Language: en-US, nl To: Benjamin Gaignard , mchehab@kernel.org Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, kernel@collabora.com References: <20231215090813.15610-1-benjamin.gaignard@collabora.com> <20231215090813.15610-4-benjamin.gaignard@collabora.com> <2d9296a1-1fbc-4d85-b147-90f8f10c8f5f@collabora.com> From: Hans Verkuil Autocrypt: addr=hverkuil@xs4all.nl; keydata= xsFNBFQ84W0BEAC7EF1iL4s3tY8cRTVkJT/297h0Hz0ypA+ByVM4CdU9sN6ua/YoFlr9k0K4 BFUlg7JzJoUuRbKxkYb8mmqOe722j7N3HO8+ofnio5cAP5W0WwDpM0kM84BeHU0aPSTsWiGR yw55SOK2JBSq7hueotWLfJLobMWhQii0Zd83hGT9SIt9uHaHjgwmtTH7MSTIiaY6N14nw2Ud C6Uykc1va0Wqqc2ov5ihgk/2k2SKa02ookQI3e79laOrbZl5BOXNKR9LguuOZdX4XYR3Zi6/ BsJ7pVCK9xkiVf8svlEl94IHb+sa1KrlgGv3fn5xgzDw8Z222TfFceDL/2EzUyTdWc4GaPMC E/c1B4UOle6ZHg02+I8tZic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uvkv0Ilelt3Pb9J7zhcx6sm5T7v16VceF96jG61bnJ2GFS+QZerZp3PY27XgtPxRxYj AmFUeF486PHx/2Yi4u1rQpIpC5inPxIgR1+ZFvQrAV36SvLFfuMhyCAxV6WBlQc85ArOiQZB Wm7L0repwr7zEJFEkdy8C81WRhMdPvHkAIh3RoY1SGcdB7rB3wCzfYkAuCBqaF7Zgfw8xkad KEiQTexRbM1sc/I8ACpla3N26SfQwrfg6V7TIoweP0RwDrcf5PVvwSWsRQp2LxFCkwnCXOra gYmkrmv0duG1FStpY+IIQn1TOkuXrciTVfZY1cZD0aVxwlxXBnUNZZNslldvXFtndxR0SFat sflovhDxKyhFwXOP0Rv8H378/+14TaykknRBIKEc0+lcr+EMOSUR5eg4aURb8Gc3Uc7fgQ6q UssTXzHPyj1hAyDpfu8DzAwlh4kKFTodxSsKAjI45SLjadSc94/5Gy8645Y1KgBzBPTH7Q== In-Reply-To: <2d9296a1-1fbc-4d85-b147-90f8f10c8f5f@collabora.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 15/01/2024 15:52, Benjamin Gaignard wrote: > > Le 15/01/2024 à 13:11, Hans Verkuil a écrit : >> On 15/12/2023 10:08, Benjamin Gaignard wrote: >>> When DELETE_BUFS will be introduced holes could created in bufs array. >>> To be able to reuse these unused indices reworking how create->index >>> is set is mandatory. >>> Let __vb2_queue_alloc() decide which first index is correct and >>> forward this to the caller. >>> >>> Signed-off-by: Benjamin Gaignard >>> --- >>>   .../media/common/videobuf2/videobuf2-core.c   | 22 ++++++++++++------- >>>   .../media/common/videobuf2/videobuf2-v4l2.c   | 20 +++++++++++------ >>>   include/media/videobuf2-core.h                |  5 ++++- >>>   3 files changed, 31 insertions(+), 16 deletions(-) >>> >>> diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c >>> index a183edf11586..cd2b9e51b9b0 100644 >>> --- a/drivers/media/common/videobuf2/videobuf2-core.c >>> +++ b/drivers/media/common/videobuf2/videobuf2-core.c >>> @@ -447,11 +447,12 @@ static void vb2_queue_remove_buffer(struct vb2_buffer *vb) >>>    */ >>>   static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, >>>                    unsigned int num_buffers, unsigned int num_planes, >>> -                 const unsigned plane_sizes[VB2_MAX_PLANES]) >>> +                 const unsigned int plane_sizes[VB2_MAX_PLANES], >>> +                 unsigned int *first_index) >>>   { >>> -    unsigned int q_num_buffers = vb2_get_num_buffers(q); >>>       unsigned int buffer, plane; >>>       struct vb2_buffer *vb; >>> +    unsigned long index; >>>       int ret; >>>         /* >>> @@ -459,7 +460,11 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, >>>        * in the queue is below q->max_num_buffers >>>        */ >>>       num_buffers = min_t(unsigned int, num_buffers, >>> -                q->max_num_buffers - q_num_buffers); >>> +                q->max_num_buffers - vb2_get_num_buffers(q)); >>> + >>> +    index = vb2_get_num_buffers(q); >>> + >>> +    *first_index = index; >>>         for (buffer = 0; buffer < num_buffers; ++buffer) { >>>           /* Allocate vb2 buffer structures */ >>> @@ -479,7 +484,7 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, >>>               vb->planes[plane].min_length = plane_sizes[plane]; >>>           } >>>   -        vb2_queue_add_buffer(q, vb, q_num_buffers + buffer); >>> +        vb2_queue_add_buffer(q, vb, index++); >>>           call_void_bufop(q, init_buffer, vb); >>>             /* Allocate video buffer memory for the MMAP type */ >>> @@ -820,7 +825,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, >>>       unsigned int q_num_bufs = vb2_get_num_buffers(q); >>>       unsigned plane_sizes[VB2_MAX_PLANES] = { }; >>>       bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; >>> -    unsigned int i; >>> +    unsigned int i, first_index; >>>       int ret = 0; >>>         if (q->streaming) { >>> @@ -906,7 +911,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, >>>         /* Finally, allocate buffers and video memory */ >>>       allocated_buffers = >>> -        __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes); >>> +        __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes, &first_index); >>>       if (allocated_buffers == 0) { >>>           dprintk(q, 1, "memory allocation failed\n"); >>>           ret = -ENOMEM; >>> @@ -980,7 +985,8 @@ EXPORT_SYMBOL_GPL(vb2_core_reqbufs); >>>   int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, >>>                unsigned int flags, unsigned int *count, >>>                unsigned int requested_planes, >>> -             const unsigned int requested_sizes[]) >>> +             const unsigned int requested_sizes[], >>> +             unsigned int *first_index) >>>   { >>>       unsigned int num_planes = 0, num_buffers, allocated_buffers; >>>       unsigned plane_sizes[VB2_MAX_PLANES] = { }; >>> @@ -1042,7 +1048,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, >>>         /* Finally, allocate buffers and video memory */ >>>       allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers, >>> -                num_planes, plane_sizes); >>> +                num_planes, plane_sizes, first_index); >>>       if (allocated_buffers == 0) { >>>           dprintk(q, 1, "memory allocation failed\n"); >>>           ret = -ENOMEM; >>> diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/media/common/videobuf2/videobuf2-v4l2.c >>> index 54d572c3b515..3c0c423c5674 100644 >>> --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c >>> +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c >>> @@ -797,11 +797,16 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) >>>       for (i = 0; i < requested_planes; i++) >>>           if (requested_sizes[i] == 0) >>>               return -EINVAL; >>> -    return ret ? ret : vb2_core_create_bufs(q, create->memory, >>> -                        create->flags, >>> -                        &create->count, >>> -                        requested_planes, >>> -                        requested_sizes); >>> +    if (ret) >>> +        return ret; >>> + >>> +    ret = vb2_core_create_bufs(q, create->memory, >>> +                   create->flags, >>> +                   &create->count, >>> +                   requested_planes, >>> +                   requested_sizes, >>> +                   &create->index); >>> +    return ret; >>>   } >>>   EXPORT_SYMBOL_GPL(vb2_create_bufs); >>>   @@ -1029,15 +1034,16 @@ int vb2_ioctl_create_bufs(struct file *file, void *priv, >>>       int res = vb2_verify_memory_type(vdev->queue, p->memory, >>>               p->format.type); >>>   -    p->index = vdev->queue->num_buffers; >>>       fill_buf_caps(vdev->queue, &p->capabilities); >>>       validate_memory_flags(vdev->queue, p->memory, &p->flags); >> While reviewing this, I think I found a bug in the current code: >> >> vb2_create_bufs() sets V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS, but >> if p->count == 0, then that function isn't called... >> >>>       /* >>>        * If count == 0, then just check if memory and type are valid. >>>        * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0. >>>        */ >>> -    if (p->count == 0) >>> +    if (p->count == 0) { >>> +        p->index = vb2_get_num_buffers(vdev->queue); >>>           return res != -EBUSY ? res : 0; >> ...instead it just falls in this 'if'. >> >> It would be better to refactor this so that vb2_ioctl_create_bufs() >> relies on vb2_create_bufs for most of the work. >> >> The reason for the messy code is that if p->count == 0, then it >> should ignore any EBUSY results, since that should always work. >> >> Alternatively, just copy the code from vb2_create_bufs here so the >> flag is properly set. >> >> In any case, fixing this is a separate patch that should go to v6.8. > > Do you want this new patch to be in the next version of this series or completely > separated ? Completely separate. It's a fix that needs to go to v6.8. Please prioritize this, once rc1 is released I'll prepare a PR with several that need to go in v6.8. Regards, Hans > > Regards, > Benjamin > >> >> Regards, >> >>     Hans >> >>> +    } >>>       if (res) >>>           return res; >>>       if (vb2_queue_is_busy(vdev->queue, file)) >>> diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h >>> index 7b84b4e2e273..607f2ba7a905 100644 >>> --- a/include/media/videobuf2-core.h >>> +++ b/include/media/videobuf2-core.h >>> @@ -821,6 +821,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, >>>    * @count: requested buffer count. >>>    * @requested_planes: number of planes requested. >>>    * @requested_sizes: array with the size of the planes. >>> + * @first_index: index of the first created buffer, all allocated buffers have >>> + *         indices in the range [first..first+count] >>>    * >>>    * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is >>>    * called internally by VB2 by an API-specific handler, like >>> @@ -837,7 +839,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, >>>   int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, >>>                unsigned int flags, unsigned int *count, >>>                unsigned int requested_planes, >>> -             const unsigned int requested_sizes[]); >>> +             const unsigned int requested_sizes[], >>> +             unsigned int *first_index); >>>     /** >>>    * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace