From nobody Thu Nov 14 05:34:58 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F1BACDB483 for ; Tue, 17 Oct 2023 14:51:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344434AbjJQOvu (ORCPT ); Tue, 17 Oct 2023 10:51:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344457AbjJQOtB (ORCPT ); Tue, 17 Oct 2023 10:49:01 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 138FB12E; Tue, 17 Oct 2023 07:48:33 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:7205:da49:a7e8:59f8]) (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 2BECC66073C3; Tue, 17 Oct 2023 15:48:31 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1697554111; bh=u0Q5LhMlMmzDnDRMmnfWjZ04KVEnwRGQKtzWOZE3C0U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V40MUL4HW+y/BWDGqaGnMj4TjmrqWytxA9dBPpxU8ttVkos7zB5OebknGYD80hikS LNwS6RdSLl1NvBhxqSAMCSK4lmKZNKUo9dMLcMdN2fwsAoq6xytu5wPoTVOXQR4e9F jEXu23yWd/3QZJDBY4uYNgeKlBUsJro3v/prih5VdHrFGIWPbnA90imkMbDf+zRE09 5r/Uiii9Mf+qQgtlmd0I+rI/aLch0cvL7JnZ0G/vDWSudubFTfcP87xLAm0GMhPBvW l+FUVqHB1D83ZXOAZDFL4YXG15SweVJA1yrQFRcbWojydAqafNq8RvuoBlLtNwbOSR OvdhRJxafreRw== 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 v12 53/56] media: core: Free range of buffers Date: Tue, 17 Oct 2023 16:47:53 +0200 Message-Id: <20231017144756.34719-54-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231017144756.34719-1-benjamin.gaignard@collabora.com> References: <20231017144756.34719-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- .../media/common/videobuf2/videobuf2-core.c | 59 +++++++++---------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/medi= a/common/videobuf2/videobuf2-core.c index 6e88406fcae9..010a8bca0240 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -519,15 +519,13 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enu= m 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, unsign= ed int count) { - unsigned int buffer; + unsigned int i; struct vb2_buffer *vb; - unsigned int q_num_buffers =3D vb2_get_num_buffers(q); =20 - for (buffer =3D q_num_buffers - buffers; buffer < q_num_buffers; - ++buffer) { - vb =3D vb2_get_buffer(q, buffer); + for (i =3D start; i < q->max_num_buffers && i < start + count; i++) { + vb =3D vb2_get_buffer(q, i); if (!vb) continue; =20 @@ -542,35 +540,35 @@ static void __vb2_free_mem(struct vb2_queue *q, unsig= ned int buffers) } =20 /* - * __vb2_queue_free() - free buffers at the end of the queue - video memor= y 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, unsi= gned int count) { - unsigned int buffer; - unsigned int q_num_buffers =3D vb2_get_num_buffers(q); + unsigned int i; =20 lockdep_assert_held(&q->mmap_lock); =20 /* Call driver-provided cleanup function for each buffer, if provided */ - for (buffer =3D q_num_buffers - buffers; buffer < q_num_buffers; - ++buffer) { - struct vb2_buffer *vb =3D vb2_get_buffer(q, buffer); + for (i =3D start; i < q->max_num_buffers && i < start + count; i++) { + struct vb2_buffer *vb =3D vb2_get_buffer(q, i); =20 - if (vb && vb->planes[0].mem_priv) + if (!vb) + continue; + if (vb->planes[0].mem_priv) call_void_vb_qop(vb, buf_cleanup, vb); } =20 /* Release video buffer memory */ - __vb2_free_mem(q, buffers); + __vb2_free_mem(q, start, count); =20 #ifdef CONFIG_VIDEO_ADV_DEBUG /* * Check that all the calls were balanced during the life-time of this * queue. If not then dump the counters to the kernel log. */ - if (q_num_buffers) { + if (vb2_get_num_buffers(q)) { bool unbalanced =3D q->cnt_start_streaming !=3D q->cnt_stop_streaming || q->cnt_prepare_streaming !=3D q->cnt_unprepare_streaming || q->cnt_wait_prepare !=3D q->cnt_wait_finish; @@ -596,8 +594,8 @@ static void __vb2_queue_free(struct vb2_queue *q, unsig= ned int buffers) q->cnt_stop_streaming =3D 0; q->cnt_unprepare_streaming =3D 0; } - for (buffer =3D 0; buffer < vb2_get_num_buffers(q); buffer++) { - struct vb2_buffer *vb =3D vb2_get_buffer(q, buffer); + for (i =3D start; i < q->max_num_buffers && i < start + count; i++) { + struct vb2_buffer *vb =3D vb2_get_buffer(q, i); bool unbalanced; =20 if (!vb) @@ -614,7 +612,7 @@ static void __vb2_queue_free(struct vb2_queue *q, unsig= ned int buffers) =20 if (unbalanced) { pr_info("unbalanced counters for queue %p, buffer %d:\n", - q, buffer); + q, i); if (vb->cnt_buf_init !=3D vb->cnt_buf_cleanup) pr_info(" buf_init: %u buf_cleanup: %u\n", vb->cnt_buf_init, vb->cnt_buf_cleanup); @@ -648,9 +646,8 @@ static void __vb2_queue_free(struct vb2_queue *q, unsig= ned int buffers) #endif =20 /* Free vb2 buffers */ - for (buffer =3D q_num_buffers - buffers; buffer < q_num_buffers; - ++buffer) { - struct vb2_buffer *vb =3D vb2_get_buffer(q, buffer); + for (i =3D start; i < q->max_num_buffers && i < start + count; i++) { + struct vb2_buffer *vb =3D vb2_get_buffer(q, i); =20 if (!vb) continue; @@ -690,7 +687,7 @@ EXPORT_SYMBOL(vb2_buffer_in_use); static bool __buffers_in_use(struct vb2_queue *q) { unsigned int buffer; - for (buffer =3D 0; buffer < vb2_get_num_buffers(q); ++buffer) { + for (buffer =3D 0; buffer < q->max_num_buffers; ++buffer) { struct vb2_buffer *vb =3D vb2_get_buffer(q, buffer); =20 if (!vb) @@ -850,7 +847,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memo= ry memory, * queued without ever calling STREAMON. */ __vb2_queue_cancel(q); - __vb2_queue_free(q, q_num_bufs); + __vb2_queue_free(q, 0, q->max_num_buffers); mutex_unlock(&q->mmap_lock); =20 /* @@ -960,7 +957,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memo= ry 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; } @@ -1000,7 +997,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2= _memory memory, bool no_previous_buffers =3D !q_num_bufs; int ret =3D 0; =20 - if (q->num_buffers =3D=3D q->max_num_buffers) { + if (q_num_bufs =3D=3D q->max_num_buffers) { dprintk(q, 1, "maximum number of buffers already allocated\n"); return -ENOBUFS; } @@ -1100,7 +1097,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb= 2_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; } @@ -1714,7 +1711,7 @@ static int vb2_start_streaming(struct vb2_queue *q) * Forcefully reclaim buffers if the driver did not * correctly return them to vb2. */ - for (i =3D 0; i < vb2_get_num_buffers(q); ++i) { + for (i =3D 0; i < q->max_num_buffers; ++i) { vb =3D vb2_get_buffer(q, i); =20 if (!vb) @@ -2120,7 +2117,7 @@ static void __vb2_queue_cancel(struct vb2_queue *q) * to vb2 in stop_streaming(). */ if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { - for (i =3D 0; i < vb2_get_num_buffers(q); i++) { + for (i =3D 0; i < q->max_num_buffers; i++) { struct vb2_buffer *vb =3D vb2_get_buffer(q, i); =20 if (!vb) @@ -2164,7 +2161,7 @@ static void __vb2_queue_cancel(struct vb2_queue *q) * call to __fill_user_buffer() after buf_finish(). That order can't * be changed, so we can't move the buf_finish() to __vb2_dqbuf(). */ - for (i =3D 0; i < vb2_get_num_buffers(q); i++) { + for (i =3D 0; i < q->max_num_buffers; i++) { struct vb2_buffer *vb; struct media_request *req; =20 @@ -2568,7 +2565,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 =3D NULL; bitmap_free(q->bufs_bitmap); --=20 2.39.2