Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp525499rdb; Fri, 26 Jan 2024 03:03:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IH6zxkoDnBSkygl08ajhUkgUwphike8bpILpRNIjGPrCohyQOlXXKm0gRnSPO7eHqvaS0ie X-Received: by 2002:a05:6808:1983:b0:3bd:e8a7:d695 with SMTP id bj3-20020a056808198300b003bde8a7d695mr797988oib.22.1706267029080; Fri, 26 Jan 2024 03:03:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706267029; cv=pass; d=google.com; s=arc-20160816; b=omydZ0Ktq4ycYh7LshwT+bOZNYghKgQcoGvmfUegJICqCR2NwBsHNbUq2mGalKve7I EYw1wRmISJt0hFcZN6H9IBMSkraZVPXkA+7Cs0UdKUqTPWXzP0lczGhiUJngMhos/CYN 58/a3zX0KBBVHKd8EX6JvwEfFYu0D8GZry1bcDm9Zw6IVJHJeKkOaYlguH+kKSGLUDAc R1geHZZkamSEYbM9g07JQ2KCdpKvGAIst4q+5L0IyURBP98/WY3N48FU4nwwETsVTnnz 9W7RyLDDnvhhNFuZdxo4BuTscAHEgWQNktdtxjn2kpAiPYi+AHUTZ8CaWMMApzbllMVl kBnQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=obFoYl4IQ+mAw4OmKwjUOVfGYaYeD0imvyb0QC/5yek=; fh=kJFWdpgao/cSBC9hXz8xDSwlGqQgdrU0x08uvgLC+qU=; b=vPSp4HtBysZDG110Jj/1JXUtFDj19cOepll2rlVYvwVanpe/okKKuVmI/cFimr4/i3 XhjJnpcDstiZ08nq6xlCmly4/sw4tPq5Hq0OmRfFtIqtL4AeZnp5dm7fX/X6a778QFhL G0qQdxj32ghNPhNjRN6hWKNuciF3XEVeJzewDkPSQbp9EwKblCsewADmm2X7UfpyN1n7 aSUQgBFN0GRinvQ2R6dNbjJYoHHBlz/u1jiTtdZf9/B0xk8F4qaIKzo9CgQhbNlypypK m2SI8jEecFjdcUKAPRD5sYqQJkXAM3mbC+QWvg1JPBGzgz1r3bH9xK1/EVn8B5LTrR2m bgeA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=ayU+3cA+; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-40021-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40021-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id i9-20020ac85e49000000b0042a4771cdbesi962209qtx.365.2024.01.26.03.03.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 03:03:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40021-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=ayU+3cA+; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-40021-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40021-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 2BE621C2625A for ; Fri, 26 Jan 2024 11:03:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6FDF41B972; Fri, 26 Jan 2024 11:01:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="ayU+3cA+" Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (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 EA08E175A1; Fri, 26 Jan 2024 11:01:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706266914; cv=none; b=dQThsQYNv67OprLpGu6V8p4J52igpJXHDeYftgzknpui3gPl5RrjPAvb61RW1fWGy5e5vE6OxQZQa4yJ2Xz+W3lhQFFbgKL+/ZYaqGFVCp3X+BFfwLEeyBvAaZo66gS7WVcO3jYeBAM8/AVOSu3W125h6KsLMsNHo1sotkp20F4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706266914; c=relaxed/simple; bh=IeCGuRou4s5Cc9FsPQtJVHkud7Qiujt0hIax6T7pJBo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GQ++W37eE71X6ejz9E044E5Nyj9UUQ1koLQvXPEoGRLOt8CTvFMJnAVlcmTuHKLpl6/ziAi/rOFk9jcnsRH+aLR42AF8Vlab4ynBL4iUl928Tc6DTMNayvKAD/SagL9wmpdpUkY0GT7lqQ/B0gmSbNCWeib4m3sBWlkiZTvVR1w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=ayU+3cA+; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1706266910; bh=IeCGuRou4s5Cc9FsPQtJVHkud7Qiujt0hIax6T7pJBo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ayU+3cA+wEhblkHFlkxfdVkAU080ueLEEGEgahyrilV1Q8tSpXmm1NLLs+av4Oauv ClxRwTBoFhuZT4JDciPkJDBLSWDr3coQUgiFRE90PdD9sfAHLjZyoF0Mo5kJ3fGqJv W0b11igLpZS0A8yAqxIlbJ2ihFoztQL5droSGX1g3WPA2oK/4NjxMbXn+3wqbzZQb9 H+jxwvL+/ziflINw51fxVjZy8jOB4tlqbDSQ/9Bl1z+u/urmyYipgyQDCiJyYmtLfD bS4La1KcP1wDIEcNpBTFwXHrc0wlAbSE9NUjJL6RZtoC9lS5Luo5pwPTEr0fUfGfyO ccJ+ulByHiUFA== Received: from benjamin-XPS-13-9310.. (cola.collaboradmins.com [195.201.22.229]) (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 madrid.collaboradmins.com (Postfix) with ESMTPSA id A8713378208A; Fri, 26 Jan 2024 11:01:49 +0000 (UTC) From: Benjamin Gaignard To: hverkuil@xs4all.nl, mchehab@kernel.org Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v18 6/9] media: core: Free range of buffers Date: Fri, 26 Jan 2024 12:01:38 +0100 Message-Id: <20240126110141.135896-7-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240126110141.135896-1-benjamin.gaignard@collabora.com> References: <20240126110141.135896-1-benjamin.gaignard@collabora.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 1506fbe8fc76..8486a2f2c09d 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -540,17 +540,16 @@ 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 + * __vb2_free_mem() - release video buffer memory for a given range of + * buffers in 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; + unsigned int i; struct vb2_buffer *vb; - unsigned int q_num_buffers = vb2_get_num_buffers(q); - for (buffer = q_num_buffers - buffers; buffer < q_num_buffers; - ++buffer) { - vb = vb2_get_buffer(q, buffer); + for (i = start; i < start + count; i++) { + vb = vb2_get_buffer(q, i); if (!vb) continue; @@ -565,35 +564,33 @@ static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) } /* - * __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; - unsigned int q_num_buffers = vb2_get_num_buffers(q); + unsigned int i; lockdep_assert_held(&q->mmap_lock); /* Call driver-provided cleanup function for each buffer, if provided */ - for (buffer = q_num_buffers - buffers; buffer < q_num_buffers; - ++buffer) { - struct vb2_buffer *vb = vb2_get_buffer(q, buffer); + for (i = start; i < start + count; i++) { + struct vb2_buffer *vb = vb2_get_buffer(q, i); if (vb && vb->planes[0].mem_priv) call_void_vb_qop(vb, buf_cleanup, vb); } /* Release video buffer memory */ - __vb2_free_mem(q, buffers); + __vb2_free_mem(q, start, count); #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 = q->cnt_start_streaming != q->cnt_stop_streaming || q->cnt_prepare_streaming != q->cnt_unprepare_streaming || q->cnt_wait_prepare != q->cnt_wait_finish; @@ -619,8 +616,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 < vb2_get_num_buffers(q); buffer++) { - struct vb2_buffer *vb = vb2_get_buffer(q, buffer); + for (i = start; i < start + count; i++) { + struct vb2_buffer *vb = vb2_get_buffer(q, i); bool unbalanced; if (!vb) @@ -637,7 +634,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); @@ -671,9 +668,8 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) #endif /* Free vb2 buffers */ - for (buffer = q_num_buffers - buffers; buffer < q_num_buffers; - ++buffer) { - struct vb2_buffer *vb = vb2_get_buffer(q, buffer); + for (i = start; i < start + count; i++) { + struct vb2_buffer *vb = vb2_get_buffer(q, i); if (!vb) continue; @@ -713,7 +709,7 @@ EXPORT_SYMBOL(vb2_buffer_in_use); static bool __buffers_in_use(struct vb2_queue *q) { unsigned int buffer; - for (buffer = 0; buffer < vb2_get_num_buffers(q); ++buffer) { + for (buffer = 0; buffer < q->max_num_buffers; ++buffer) { struct vb2_buffer *vb = vb2_get_buffer(q, buffer); if (!vb) @@ -899,7 +895,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->max_num_buffers); mutex_unlock(&q->mmap_lock); /* @@ -1002,7 +998,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; } @@ -1127,7 +1123,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; } @@ -1742,7 +1738,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 = 0; i < vb2_get_num_buffers(q); ++i) { + for (i = 0; i < q->max_num_buffers; ++i) { vb = vb2_get_buffer(q, i); if (!vb) @@ -2148,7 +2144,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 = 0; i < vb2_get_num_buffers(q); i++) { + for (i = 0; i < q->max_num_buffers; i++) { struct vb2_buffer *vb = vb2_get_buffer(q, i); if (!vb) @@ -2192,7 +2188,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 = 0; i < vb2_get_num_buffers(q); i++) { + for (i = 0; i < q->max_num_buffers; i++) { struct vb2_buffer *vb; struct media_request *req; @@ -2619,7 +2615,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, vb2_get_num_buffers(q)); + __vb2_queue_free(q, 0, q->max_num_buffers); vb2_core_free_buffers_storage(q); mutex_unlock(&q->mmap_lock); } -- 2.40.1