From patchwork Wed Sep 27 15:35:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 145502 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2797899vqu; Wed, 27 Sep 2023 10:52:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3UjAIri5i4r3IM132UuqNdBFAOd4goFGUAgI1emWcE/LLRig8Z9Ud/jT0NB3mIyr1BvD+ X-Received: by 2002:a17:903:120c:b0:1c5:de06:9e5a with SMTP id l12-20020a170903120c00b001c5de069e5amr2498060plh.21.1695837142620; Wed, 27 Sep 2023 10:52:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695837142; cv=none; d=google.com; s=arc-20160816; b=TDSWpOztS17gHX9JSdOewN78HZtm9XpWwyDRS/NtO+TdbK9Rwlb0reLGFzS4fOH0wR bj9zYqriGquf/RfgbHQWjJJ799ZD5CxYdpdBJJUkMbiMdozdpZbBfTqrcnzA2Numcjyh LVn1ioYnGI5RN83jFfqQhc8hjgc7XK11ARwiAr3CCrcPzkhYrKFDjwjOpfZUZ6Eg29lv +PgXCaUZSCXwY3d1rq4GUR2xvqc9oQ8HzAU4yHs1Pkragk14DXQ4kwJi1LGTDAvYafzf +bK+UhRXCaJo/SUR2XpymrQh2Sqs9b0rWyqKnPgbzGhbMBKecRHvtEgFvgt9Irsd/gpS Np5Q== 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=2qWN9gcWE1cCd4QDbOtoUFT3dABxhAehI8TgvGuMY4I=; fh=ceA81l8d2hZW6pWH7VE5TwJxtgXUzAubfWIMcikPA1M=; b=g/zBXGFzkOQH/VIeFduZxlthuj31JJqzw1LTq9Pg2wfgbDRji6gZWtAJEDN/M/u05H 9OpGjQ2R7RVEzDcMbn+chfNCcbsKM7280J5pfe6ppG9Yc6bLPZ/GEYr8Re1UImtcm1UW 3mAQpOewaAvjbP1bCk035MQBUnv8/grZCr23FbxXNvO6pSoPAQLdQ98dUoNnZ5JxKMJ4 ySPQID/BMS2GGAhPpPaVwXSBZVveURwKmGQLcVzq+AnIDi2g4W6Q3T2+O7yDwhdlssiR R+XCpTnhZmNyrjIvcAkrYpm5HEdUfj9dMov+Txf/0NI/s9wBG/BngXS41uqVdI5iRzEt kwUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=l7hvUjhR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id q15-20020a170902dacf00b001c434b3659bsi17394321plx.354.2023.09.27.10.52.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 10:52:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=l7hvUjhR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id BCC7B81DFD54; Wed, 27 Sep 2023 08:39:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233077AbjI0Pis (ORCPT + 19 others); Wed, 27 Sep 2023 11:38:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232769AbjI0Pgf (ORCPT ); Wed, 27 Sep 2023 11:36:35 -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 BEDCBCDD; Wed, 27 Sep 2023 08:36:33 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:672:46bd:3ec7:6cdf]) (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 1DC526607381; Wed, 27 Sep 2023 16:36:32 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1695828992; bh=Tn/0+SEs92pxnf0JZtLBn6EtBJkC+KjsYnF3S+7Li74=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l7hvUjhRCDvOCfXpTo+2MjkpJn4DcC9K4yXxmAhH1ri88E6W2bLcmZNcP21AdwLfU 4HhtSPFyBFX4ETWeOuF5us2fENDMrD7axP6qDageUb1fvZqUZis8nrtwOt+EDf/yBt FVN72t4l3bJYRbns8Ehf3KNXbDUHoTDVzXQLUvmWRjRuQTLXAnr4xEaiQLmTYQbv1h zmDpUqjYDOEGKhglbQByAQ2laL2RiWPbT4kSYQ0QC/46/M5KC7zQVY/0LpfQnJbM62 7OZqO4nkGpjZ7z5t6W5ybB5ZSAn6v+obR/jP6Ed1VRGaa/cULLkRsWgmXtUsCmlZS4 9h+pmqY0RrdwA== 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 v8 50/53] media: core: Free range of buffers Date: Wed, 27 Sep 2023 17:35:55 +0200 Message-Id: <20230927153558.159278-51-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230927153558.159278-1-benjamin.gaignard@collabora.com> References: <20230927153558.159278-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,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 agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 27 Sep 2023 08:39:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778214127861964855 X-GMAIL-MSGID: 1778214127861964855 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 | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 8d35cf7cef42..3bf455f03461 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -518,13 +518,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; @@ -536,36 +535,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 /* @@ -650,15 +645,14 @@ 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--) { - struct vb2_buffer *vb = vb2_get_buffer(q, buffer); + for (i = start; i < q->max_num_buffers && i < start + count; i++) { + struct vb2_buffer *vb = vb2_get_buffer(q, i); if (!vb) continue; vb2_queue_remove_buffer(vb); kfree(vb); - buffer++; } if (!vb2_get_num_buffers(q)) { @@ -852,7 +846,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); /* @@ -962,7 +956,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; } @@ -1102,7 +1096,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; } @@ -2568,7 +2562,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);