Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6540332rdb; Fri, 15 Dec 2023 01:10:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IGMt+8ZYIDwO4PCJEn1HkOjeVMaillyUgB9QvozoAnQGvmzeoUbTsRtKJfHWFxSAZWXpU92 X-Received: by 2002:a05:6870:596:b0:1fa:d01e:fcf1 with SMTP id m22-20020a056870059600b001fad01efcf1mr12197902oap.17.1702631401901; Fri, 15 Dec 2023 01:10:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702631401; cv=none; d=google.com; s=arc-20160816; b=tCkzeKwn/JSaY4qf7KGkFjTBF8NFtmygLUoWgZYNI7quoy8gaOWX2lNQ//toJ9UKTR vjc/m14UMg0vnfRGoUqeddS3BXNvGzRIEpWAILOasc3feIJHGZw83ZiFGdyKGmDrROgW VS1t0ODdqICUQGg5NWgQ1JPm0RHdVWSzpxy42hKrxqTmpuxpE2t65nzMfSTfI6BwNMaY k3u8rhkiXjiO4NrtKLmKjdwISljvBaRGuQFZlOaV0XAuwwNmx76bPUIb4ZMmUoGJsdGP +OJD8lkAhUexj+OCNPNPHrqX6nlxrRHU87DTCJCwv3sJ0Ko5jSBGxaQQigrCscwfnzFv y39A== ARC-Message-Signature: i=1; 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=/uzi7NLZfnagfy/uMT5/1bOUoFkfDEA3B2T2/5HxY2U=; fh=kJFWdpgao/cSBC9hXz8xDSwlGqQgdrU0x08uvgLC+qU=; b=GGZ3dYJ26os2sxpFghcbeH6sJeQXvXcsYPqovctpvTz48MCtZlmPmu1Nq9+0kJB9UG Ue1R9pr8j48+UrpZRHV7myCWb2lL+UNEZypWkLsu9y6UXx4yVJmb03saZGu9yCga+MQF lZHn+q2ZrfuDAWzSUeQlDykYeSrRokD0rZ+4KTauHfaLYkKFo/LohKHYjtPynWZ7ufwF ugirDlYBXHL22SQL38lUCv0MhZEfG467QYDZdULFpcPlYc9U52zSZAAm9Dvs2QLZd5pJ eOJunqmdtutUtYyvgB7F9a7pCxJwpyygM7HKGLzEnryLpOXUm3mgnTiUCemCsU3SMSZ/ KiZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=qvVyR6C7; spf=pass (google.com: domain of linux-kernel+bounces-665-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-665-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id s1-20020a6550c1000000b005c688ef7549si12503619pgp.98.2023.12.15.01.10.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 01:10:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-665-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=qvVyR6C7; spf=pass (google.com: domain of linux-kernel+bounces-665-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-665-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 187F2B2290C for ; Fri, 15 Dec 2023 09:09:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0A7F51A702; Fri, 15 Dec 2023 09:08:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="qvVyR6C7" X-Original-To: linux-kernel@vger.kernel.org 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 60A9618C15; Fri, 15 Dec 2023 09:08:26 +0000 (UTC) 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=1702631304; bh=lP5AIE9eKRsm3pAUMbb5nmwCu/ZB+PLSBGMB1wlfz6U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qvVyR6C7DX5nRPxidsIwEyp+tGYvtTsdUzwqFq5Am1ARIxl5zVIPOGaJEqshrrT9c V585HcwnLWSgJjqW1t4lVhZ/2bBykC+gu/bVTl6w/5yWoa6rLKSFauwl8hmG6EuKVd +x7WT4v4DdaVfY1JFcPwaMtdVLxWgH0OU1En9U5uAV164Uqvs8rOSbBGc/qB5SfqYO X7qrxdp0+9sBliip1LCQtmWHCAA/GTTA63SfOgwWdjzUlevDXnI4m1cdla8G1VmKq2 uJqfmGFim9sXa3NCkxOmlQk6p3ggI7QdAGgx334cg1d/P3jANnqM64DZtNPDPGGVm+ TrMBICwaph/NQ== 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 5C4073781FD6; Fri, 15 Dec 2023 09:08:24 +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 v16 4/8] media: core: Add bitmap manage bufs array entries Date: Fri, 15 Dec 2023 10:08:09 +0100 Message-Id: <20231215090813.15610-5-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231215090813.15610-1-benjamin.gaignard@collabora.com> References: <20231215090813.15610-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 Add a bitmap field to know which of bufs array entries are used or not. Remove no more used num_buffers field from queue structure. Use bitmap_find_next_zero_area() to find the first possible range when creating new buffers to fill the gaps. Signed-off-by: Benjamin Gaignard --- .../media/common/videobuf2/videobuf2-core.c | 37 ++++++++++++++++--- include/media/videobuf2-core.h | 17 +++++---- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index cd2b9e51b9b0..9509535a980c 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -421,11 +421,12 @@ static void init_buffer_cache_hints(struct vb2_queue *q, struct vb2_buffer *vb) */ static void vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, unsigned int index) { - WARN_ON(index >= q->max_num_buffers || q->bufs[index] || vb->vb2_queue); + WARN_ON(index >= q->max_num_buffers || test_bit(index, q->bufs_bitmap) || vb->vb2_queue); q->bufs[index] = vb; vb->index = index; vb->vb2_queue = q; + set_bit(index, q->bufs_bitmap); } /** @@ -434,6 +435,7 @@ static void vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, uns */ static void vb2_queue_remove_buffer(struct vb2_buffer *vb) { + clear_bit(vb->index, vb->vb2_queue->bufs_bitmap); vb->vb2_queue->bufs[vb->index] = NULL; vb->vb2_queue = NULL; } @@ -462,7 +464,8 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, num_buffers = min_t(unsigned int, num_buffers, q->max_num_buffers - vb2_get_num_buffers(q)); - index = vb2_get_num_buffers(q); + index = bitmap_find_next_zero_area(q->bufs_bitmap, q->max_num_buffers, + 0, num_buffers, 0); *first_index = index; @@ -664,7 +667,6 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) kfree(vb); } - q->num_buffers -= buffers; if (!vb2_get_num_buffers(q)) { q->memory = VB2_MEMORY_UNKNOWN; INIT_LIST_HEAD(&q->queued_list); @@ -882,6 +884,14 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, q->bufs = kcalloc(q->max_num_buffers, sizeof(*q->bufs), GFP_KERNEL); if (!q->bufs) ret = -ENOMEM; + + if (!q->bufs_bitmap) + q->bufs_bitmap = bitmap_zalloc(q->max_num_buffers, GFP_KERNEL); + if (!q->bufs_bitmap) { + ret = -ENOMEM; + kfree(q->bufs); + q->bufs = NULL; + } q->memory = memory; mutex_unlock(&q->mmap_lock); if (ret) @@ -951,7 +961,6 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, } mutex_lock(&q->mmap_lock); - q->num_buffers = allocated_buffers; if (ret < 0) { /* @@ -978,6 +987,10 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, mutex_lock(&q->mmap_lock); q->memory = VB2_MEMORY_UNKNOWN; mutex_unlock(&q->mmap_lock); + kfree(q->bufs); + q->bufs = NULL; + bitmap_free(q->bufs_bitmap); + q->bufs_bitmap = NULL; return ret; } EXPORT_SYMBOL_GPL(vb2_core_reqbufs); @@ -1014,9 +1027,19 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, q->memory = memory; if (!q->bufs) q->bufs = kcalloc(q->max_num_buffers, sizeof(*q->bufs), GFP_KERNEL); - if (!q->bufs) + if (!q->bufs) { ret = -ENOMEM; + goto unlock; + } + if (!q->bufs_bitmap) + q->bufs_bitmap = bitmap_zalloc(q->max_num_buffers, GFP_KERNEL); + if (!q->bufs_bitmap) { + ret = -ENOMEM; + kfree(q->bufs); + q->bufs = NULL; + } mutex_unlock(&q->mmap_lock); +unlock: if (ret) return ret; q->waiting_for_buffers = !q->is_output; @@ -1078,7 +1101,6 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, } mutex_lock(&q->mmap_lock); - q->num_buffers += allocated_buffers; if (ret < 0) { /* @@ -2567,6 +2589,9 @@ void vb2_core_queue_release(struct vb2_queue *q) __vb2_queue_free(q, vb2_get_num_buffers(q)); kfree(q->bufs); q->bufs = NULL; + bitmap_free(q->bufs_bitmap); + q->bufs_bitmap = NULL; + mutex_unlock(&q->mmap_lock); } EXPORT_SYMBOL_GPL(vb2_core_queue_release); diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 607f2ba7a905..e4c1fc7ae82f 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -346,8 +346,8 @@ struct vb2_buffer { * describes the requested number of planes and sizes\[\] * contains the requested plane sizes. In this case * \*num_buffers are being allocated additionally to - * q->num_buffers. If either \*num_planes or the requested - * sizes are invalid callback must return %-EINVAL. + * the buffers already in the queue. If either \*num_planes + * or the requested sizes are invalid callback must return %-EINVAL. * @wait_prepare: release any locks taken while calling vb2 functions; * it is called before an ioctl needs to wait for a new * buffer to arrive; required to avoid a deadlock in @@ -572,7 +572,7 @@ struct vb2_buf_ops { * @memory: current memory type used * @dma_dir: DMA mapping direction. * @bufs: videobuf2 buffer structures - * @num_buffers: number of allocated/used buffers + * @bufs_bitmap: bitmap tracking whether each bufs[] entry is used * @max_num_buffers: upper limit of number of allocated/used buffers. * If set to 0 v4l2 core will change it VB2_MAX_FRAME * for backward compatibility. @@ -639,7 +639,7 @@ struct vb2_queue { unsigned int memory; enum dma_data_direction dma_dir; struct vb2_buffer **bufs; - unsigned int num_buffers; + unsigned long *bufs_bitmap; unsigned int max_num_buffers; struct list_head queued_list; @@ -1168,7 +1168,10 @@ static inline bool vb2_fileio_is_active(struct vb2_queue *q) */ static inline unsigned int vb2_get_num_buffers(struct vb2_queue *q) { - return q->num_buffers; + if (!q->bufs_bitmap) + return 0; + + return bitmap_weight(q->bufs_bitmap, q->max_num_buffers); } /** @@ -1271,13 +1274,13 @@ static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q) static inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q, unsigned int index) { - if (!q->bufs) + if (!q->bufs_bitmap) return NULL; if (index >= q->max_num_buffers) return NULL; - if (index < q->num_buffers) + if (test_bit(index, q->bufs_bitmap)) return q->bufs[index]; return NULL; } -- 2.40.1