From patchwork Mon Oct 2 12:06:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 147362 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1536462vqb; Mon, 2 Oct 2023 09:18:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE1GEYVY23rq0izsu3TD2pVxv2JnNA/5mgY/roJhqCwxOWJkCrUcLKwbqPo0U7blUu+saWa X-Received: by 2002:a05:6a20:1604:b0:15e:e0fd:98e7 with SMTP id l4-20020a056a20160400b0015ee0fd98e7mr14314351pzj.20.1696263521620; Mon, 02 Oct 2023 09:18:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696263521; cv=none; d=google.com; s=arc-20160816; b=gU/ctk7zPL0W4rDtK9pal7EBsUbgKCAPCornF5gyt2Vei1kWwXQnPAnjUl3EqfJoRC caNzestenr3mY5olmFAmP2AatThMf+RKEOjRmw7V6xqHQTh543SFdqfRXWpwWrFH+SPX ZBrOwBehDuj1qMHZrXeWpICxYrkkc6TLU/LhttKEBVCJcKOiRWGk7zMymJqhQ3wThZXL LsaC/vbB5DvT5g+nKp9VhvfezI9pfAqKDBqh4GwtwoSxsRgQtFJCHHK949u7qBRlhqyv /jgNqE+npoXZdLcJi0AjKH4jhbDv2Cb6iixTTtbTLvAZoECXNMYU8uPD3jdIIrzNHQeF n/cg== 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=T3Gk3vQKsEUZbl24nlYwB/xb8UprcBLJQHT2uhMpszk=; fh=ceA81l8d2hZW6pWH7VE5TwJxtgXUzAubfWIMcikPA1M=; b=A8+ZScmQa0KEim6GET2njih1Vnh0iUdf/TXASy5DGsy1cmAY/UWeJUiwMIPH6lCBi6 JTDruEcG1oIOeYZ+GznCxZt/KZt8my14Lp8rsV0uCTkM2rQ8X2ohIhPO8eujGw3VS8TF XpeD83W8fipXqiXfV0R/qJAh5STNsobtR2J6JNmjt7pnDSbAvqy4P1ooCoczH7s0suvt HpnWYmnAO8uzKW/XBSHmUkKlzIABres0WFnLSjBvOVD+S7HRlmbQ5tBScpcR+RpxQDgH DSJlqE9Vn7NRDGhaJHWyObp1APPLWJH3EDDFiJNJ0GITXcG7v5hxOX8+1nvFEXmAE7QL 3J9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=YpeFIL7J; 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=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id m1-20020a656a01000000b00573fd9be4bdsi32195487pgu.493.2023.10.02.09.18.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 09:18:41 -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; dkim=pass header.i=@collabora.com header.s=mail header.b=YpeFIL7J; 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=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 howler.vger.email (Postfix) with ESMTP id 6B8C0823CC54; Mon, 2 Oct 2023 05:09:24 -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 S237466AbjJBMI6 (ORCPT + 18 others); Mon, 2 Oct 2023 08:08:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237173AbjJBMHF (ORCPT ); Mon, 2 Oct 2023 08:07:05 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEE4ACC3; Mon, 2 Oct 2023 05:06:52 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:9537:67ca:c85e:d0ae]) (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 3CB2F6607395; Mon, 2 Oct 2023 13:06:51 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1696248411; bh=xNwpnkBcVLj30pw8lyVYd9bN7x+vY+ofwKdCBF1FVjY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YpeFIL7JIK2sBuiBJeqIUMq/T9mu7AuXzYTMmA75Z5f2Fya2e9eGYxNE8pNtaNgfD /mVaov7mfwbWb+gqfHA9kUc54+FLKV4BrdYfRERvkQoEFl1lPTFmiilAQD3OIaG3no DyeHnE+HweP4yu+l7kqW8FAl5OR3DI2Uhjg40cOVx87hvops7xP9usNmrz02JqIgEq Vs6GJJpsc4g4Sy7hDjQl3USFwVErKbaAkMP/xpvNuZEA0W98Y+2zZgxDJ+KKqmwCmQ qXhV7fECtGdwOEouKJhgYYx77tRFUHTXtv0vuOfSufuIVC5trEhFMxsr0qZlAwJUCw Be/99JakqD3Bg== 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 v9 50/53] media: core: Free range of buffers Date: Mon, 2 Oct 2023 14:06:14 +0200 Message-Id: <20231002120617.119602-51-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231002120617.119602-1-benjamin.gaignard@collabora.com> References: <20231002120617.119602-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 02 Oct 2023 05:09:24 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778661218338789680 X-GMAIL-MSGID: 1778661218338789680 Improve __vb2_queue_free() and __vb2_free_mem() to free range of buffers and not only the last few buffers. Intoduce starting index to be flexible on range and change the loops according to this parameters. Signed-off-by: Benjamin Gaignard --- version 9: - fix compilation issue .../media/common/videobuf2/videobuf2-core.c | 40 ++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 4cbc17e8b53e..b9b8a44b378c 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -515,13 +515,12 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, /* * __vb2_free_mem() - release all video buffer memory for a given queue */ -static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) +static void __vb2_free_mem(struct vb2_queue *q, unsigned int start, unsigned int count) { - unsigned int buffer = 0; - long i = q->max_num_buffers; + unsigned int i; struct vb2_buffer *vb; - for (i = q->max_num_buffers; i >= 0 && buffer < buffers; i--) { + for (i = start; i < q->max_num_buffers && i < start + count; i++) { vb = vb2_get_buffer(q, i); if (!vb) continue; @@ -533,36 +532,32 @@ static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) __vb2_buf_dmabuf_put(vb); else __vb2_buf_userptr_put(vb); - buffer++; } } /* - * __vb2_queue_free() - free buffers at the end of the queue - video memory and + * __vb2_queue_free() - free count buffers from start index of the queue - video memory and * related information, if no buffers are left return the queue to an * uninitialized state. Might be called even if the queue has already been freed. */ -static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) +static void __vb2_queue_free(struct vb2_queue *q, unsigned int start, unsigned int count) { - unsigned int buffer; - long i = q->max_num_buffers; + unsigned int i; lockdep_assert_held(&q->mmap_lock); /* Call driver-provided cleanup function for each buffer, if provided */ - for (i = q->max_num_buffers, buffer = 0; i >= 0 && buffer < buffers; i--) { + for (i = start; i < q->max_num_buffers && i < start + count; i++) { struct vb2_buffer *vb = vb2_get_buffer(q, i); if (!vb) continue; - if (vb->planes[0].mem_priv) { + if (vb->planes[0].mem_priv) call_void_vb_qop(vb, buf_cleanup, vb); - buffer++; - } } /* Release video buffer memory */ - __vb2_free_mem(q, buffers); + __vb2_free_mem(q, start, count); #ifdef CONFIG_VIDEO_ADV_DEBUG /* @@ -595,8 +590,8 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) q->cnt_stop_streaming = 0; q->cnt_unprepare_streaming = 0; } - for (buffer = 0; buffer < q->max_num_buffers; buffer++) { - struct vb2_buffer *vb = vb2_get_buffer(q, buffer); + for (i = 0; i < q->max_num_buffers; i++) { + struct vb2_buffer *vb = vb2_get_buffer(q, i); bool unbalanced; if (!vb) @@ -613,7 +608,7 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) if (unbalanced) { pr_info("unbalanced counters for queue %p, buffer %d:\n", - q, buffer); + q, i); if (vb->cnt_buf_init != vb->cnt_buf_cleanup) pr_info(" buf_init: %u buf_cleanup: %u\n", vb->cnt_buf_init, vb->cnt_buf_cleanup); @@ -647,7 +642,7 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) #endif /* Free vb2 buffers */ - for (i = q->max_num_buffers, buffer = 0; i >= 0 && buffer < buffers; i--) { + for (i = start; i < q->max_num_buffers && i < start + count; i++) { struct vb2_buffer *vb = vb2_get_buffer(q, i); if (!vb) @@ -655,7 +650,6 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) vb2_queue_remove_buffer(vb); kfree(vb); - buffer++; } if (!vb2_get_num_buffers(q)) { @@ -849,7 +843,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, * queued without ever calling STREAMON. */ __vb2_queue_cancel(q); - __vb2_queue_free(q, q_num_bufs); + __vb2_queue_free(q, 0, q_num_bufs); mutex_unlock(&q->mmap_lock); /* @@ -959,7 +953,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, * from already queued buffers and it will reset q->memory to * VB2_MEMORY_UNKNOWN. */ - __vb2_queue_free(q, allocated_buffers); + __vb2_queue_free(q, first_index, allocated_buffers); mutex_unlock(&q->mmap_lock); return ret; } @@ -1099,7 +1093,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, * from already queued buffers and it will reset q->memory to * VB2_MEMORY_UNKNOWN. */ - __vb2_queue_free(q, allocated_buffers); + __vb2_queue_free(q, *first_index, allocated_buffers); mutex_unlock(&q->mmap_lock); return -ENOMEM; } @@ -2565,7 +2559,7 @@ void vb2_core_queue_release(struct vb2_queue *q) __vb2_cleanup_fileio(q); __vb2_queue_cancel(q); mutex_lock(&q->mmap_lock); - __vb2_queue_free(q, q->max_num_buffers); + __vb2_queue_free(q, 0, q->max_num_buffers); kfree(q->bufs); q->bufs = NULL; bitmap_free(q->bufs_bitmap);