Linux-mediatek Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Benjamin Gaignard <benjamin.gaignard@collabora.com>
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 <benjamin.gaignard@collabora.com>
Subject: [PATCH v7 45/49] media: core: Add bitmap manage bufs array entries
Date: Thu, 14 Sep 2023 15:33:19 +0200	[thread overview]
Message-ID: <20230914133323.198857-46-benjamin.gaignard@collabora.com> (raw)
In-Reply-To: <20230914133323.198857-1-benjamin.gaignard@collabora.com>

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 <benjamin.gaignard@collabora.com>
---
 .../media/common/videobuf2/videobuf2-core.c   | 55 +++++++++++++++----
 include/media/videobuf2-core.h                |  9 ++-
 2 files changed, 51 insertions(+), 13 deletions(-)

diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c
index a4c2fae8705d..c5d4a388331b 100644
--- a/drivers/media/common/videobuf2/videobuf2-core.c
+++ b/drivers/media/common/videobuf2/videobuf2-core.c
@@ -411,10 +411,11 @@ static void init_buffer_cache_hints(struct vb2_queue *q, struct vb2_buffer *vb)
  */
 static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, unsigned int index)
 {
-	if (index < q->max_allowed_buffers && !q->bufs[index]) {
+	if (index < q->max_allowed_buffers && !test_bit(index, q->bufs_map)) {
 		q->bufs[index] = vb;
 		vb->index = index;
 		vb->vb2_queue = q;
+		set_bit(index, q->bufs_map);
 		return true;
 	}
 
@@ -428,9 +429,10 @@ static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, uns
  */
 static void vb2_queue_remove_buffer(struct vb2_queue *q, struct vb2_buffer *vb)
 {
-	if (vb->index < q->max_allowed_buffers) {
+	if (vb->index < q->max_allowed_buffers && test_bit(vb->index, q->bufs_map)) {
 		q->bufs[vb->index] = NULL;
 		vb->vb2_queue = NULL;
+		clear_bit(vb->index, q->bufs_map);
 	}
 }
 
@@ -451,11 +453,12 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
 	unsigned long first_index;
 	int ret;
 
-	/* Ensure that q->num_buffers+num_buffers is below q->max_allowed_buffers */
+	/* Ensure that the number of already queue + num_buffers is below q->max_allowed_buffers */
 	num_buffers = min_t(unsigned int, num_buffers,
 			    q->max_allowed_buffers - vb2_get_num_buffers(q));
 
-	first_index = vb2_get_num_buffers(q);
+	first_index = bitmap_find_next_zero_area(q->bufs_map, q->max_allowed_buffers,
+						 0, num_buffers, 0);
 
 	if (first_index >= q->max_allowed_buffers)
 		return 0;
@@ -675,7 +678,13 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
 
 struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q, unsigned int index)
 {
-	if (index < q->num_buffers)
+	if (!q->bufs_map || !q->bufs)
+		return NULL;
+
+	if (index >= q->max_allowed_buffers)
+		return NULL;
+
+	if (test_bit(index, q->bufs_map))
 		return q->bufs[index];
 	return NULL;
 }
@@ -683,7 +692,10 @@ EXPORT_SYMBOL_GPL(vb2_get_buffer);
 
 unsigned int vb2_get_num_buffers(struct vb2_queue *q)
 {
-	return q->num_buffers;
+	if (!q->bufs_map)
+		return 0;
+
+	return bitmap_weight(q->bufs_map, q->max_allowed_buffers);
 }
 EXPORT_SYMBOL_GPL(vb2_get_num_buffers);
 
@@ -899,6 +911,14 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 		q->bufs = kcalloc(q->max_allowed_buffers, sizeof(*q->bufs), GFP_KERNEL);
 	if (!q->bufs)
 		ret = -ENOMEM;
+
+	if (!q->bufs_map)
+		q->bufs_map = bitmap_zalloc(q->max_allowed_buffers, GFP_KERNEL);
+	if (!q->bufs_map) {
+		ret = -ENOMEM;
+		kfree(q->bufs);
+		q->bufs = NULL;
+	}
 	q->memory = memory;
 	mutex_unlock(&q->mmap_lock);
 	if (ret)
@@ -968,7 +988,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) {
 		/*
@@ -995,6 +1014,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_map);
+	q->bufs_map = NULL;
 	return ret;
 }
 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
@@ -1031,9 +1054,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_allowed_buffers, sizeof(*q->bufs), GFP_KERNEL);
-		if (!q->bufs)
+		if (!q->bufs) {
+			ret = -ENOMEM;
+			goto unlock;
+		}
+		if (!q->bufs_map)
+			q->bufs_map = bitmap_zalloc(q->max_allowed_buffers, GFP_KERNEL);
+		if (!q->bufs_map) {
 			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;
@@ -1095,7 +1128,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) {
 		/*
@@ -2588,6 +2620,9 @@ void vb2_core_queue_release(struct vb2_queue *q)
 	__vb2_queue_free(q, q->max_allowed_buffers);
 	kfree(q->bufs);
 	q->bufs = NULL;
+	bitmap_free(q->bufs_map);
+	q->bufs_map = NULL;
+
 	mutex_unlock(&q->mmap_lock);
 }
 EXPORT_SYMBOL_GPL(vb2_core_queue_release);
@@ -2944,7 +2979,7 @@ static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_
 	 * Check if we need to dequeue the buffer.
 	 */
 	index = fileio->cur_index;
-	if (index >= q->num_buffers) {
+	if (!test_bit(index, q->bufs_map)) {
 		struct vb2_buffer *b;
 
 		/*
diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h
index 19c93d8eb7c8..734437236cc4 100644
--- a/include/media/videobuf2-core.h
+++ b/include/media/videobuf2-core.h
@@ -557,7 +557,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_map:	bitmap to manage bufs entries.
  * @max_allowed_buffers: upper limit of number of allocated/used buffers
  * @queued_list: list of buffers currently queued from userspace
  * @queued_count: number of buffers queued and ready for streaming.
@@ -621,7 +621,7 @@ struct vb2_queue {
 	unsigned int			memory;
 	enum dma_data_direction		dma_dir;
 	struct vb2_buffer		**bufs;
-	unsigned int			num_buffers;
+	unsigned long			*bufs_map;
 	unsigned int			max_allowed_buffers;
 
 	struct list_head		queued_list;
@@ -1151,7 +1151,10 @@ static inline bool vb2_fileio_is_active(struct vb2_queue *q)
  */
 static inline bool vb2_is_busy(struct vb2_queue *q)
 {
-	return (q->num_buffers > 0);
+	if (!q->bufs_map)
+		return false;
+
+	return (bitmap_weight(q->bufs_map, q->max_allowed_buffers) > 0);
 }
 
 /**
-- 
2.39.2



  parent reply	other threads:[~2023-09-14 13:37 UTC|newest]

Thread overview: 98+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-14 13:32 [PATCH v7 00/49] Add DELETE_BUF ioctl Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 01/49] media: videobuf2: Rework offset 'cookie' encoding pattern Benjamin Gaignard
2023-09-19  9:15   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 02/49] media: videobuf2: Stop spamming kernel log with all queue counter Benjamin Gaignard
2023-09-19  9:22   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 03/49] media: videobuf2: Use vb2_buffer instead of index Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 04/49] media: amphion: Use vb2_get_buffer() instead of directly access to buffers array Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 05/49] media: mediatek: jpeg: " Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 06/49] media: mediatek: vdec: " Benjamin Gaignard
2023-09-19  9:37   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 07/49] media: sti: hva: " Benjamin Gaignard
2023-09-19  9:31   ` Hans Verkuil
2023-09-19 10:26     ` Benjamin Gaignard
2023-09-19 11:20       ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 08/49] media: visl: " Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 09/49] media: atomisp: " Benjamin Gaignard
2023-09-19  9:41   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 10/49] media: dvb-core: " Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 11/49] media: videobuf2: Access vb2_queue bufs array through helper functions Benjamin Gaignard
2023-09-19 10:33   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 12/49] media: videobuf2: Be more flexible on the number of queue stored buffers Benjamin Gaignard
2023-09-19 10:55   ` Hans Verkuil
2023-09-19 12:42   ` Hans Verkuil
2023-09-20  8:56     ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 13/49] media: verisilicon: Refactor postprocessor to store more buffers Benjamin Gaignard
2023-09-19 10:57   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 14/49] media: verisilicon: Store chroma and motion vectors offset Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 15/49] media: verisilicon: g2: Use common helpers to compute chroma and mv offsets Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 16/49] media: verisilicon: postproc: Fix down scale test Benjamin Gaignard
2023-09-19 11:16   ` Hans Verkuil
2023-09-20  7:44     ` Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 17/49] media: verisilicon: vp9: Allow to change resolution while streaming Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 18/49] media: Remove duplicated index vs q->num_buffers check Benjamin Gaignard
2023-09-19 12:21   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 19/49] media: core: Add helper to get queue number of buffers Benjamin Gaignard
2023-09-14 13:32 ` [PATCH v7 20/49] media: core: Rework how create_buf index returned value is computed Benjamin Gaignard
2023-09-19 12:34   ` Hans Verkuil
2023-09-19 14:50   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 21/49] media: dvb: Stop direct calls to queue num_buffers field Benjamin Gaignard
2023-09-19 13:40   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 22/49] media: i2c: " Benjamin Gaignard
2023-09-19  9:27   ` Hans Verkuil
2023-09-19 13:42   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 23/49] media: pci: cx18: " Benjamin Gaignard
2023-09-19 13:42   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 24/49] media: pci: dt3155: " Benjamin Gaignard
2023-09-19 13:43   ` Hans Verkuil
2023-09-14 13:32 ` [PATCH v7 25/49] media: pci: netup_unidvb: " Benjamin Gaignard
2023-09-19 13:52   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 26/49] media: pci: tw68: " Benjamin Gaignard
2023-09-19 13:56   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 27/49] media: pci: tw686x: " Benjamin Gaignard
2023-09-19 13:57   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 28/49] media: amphion: " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 29/49] media: coda: " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 30/49] media: mediatek: vcodec: " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 31/49] media: nxp: " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 32/49] media: renesas: " Benjamin Gaignard
2023-09-19 14:05   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 33/49] media: sti: hva: " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 34/49] media: ti: " Benjamin Gaignard
2023-09-19 14:10   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 35/49] media: verisilicon: " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 36/49] media: test-drivers: " Benjamin Gaignard
2023-09-19 14:15   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 37/49] media: usb: airspy: " Benjamin Gaignard
2023-09-19 14:16   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 38/49] media: usb: cx231xx: " Benjamin Gaignard
2023-09-19 14:19   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 39/49] media: usb: hackrf: " Benjamin Gaignard
2023-09-19 14:20   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 40/49] media: usb: usbtv: " Benjamin Gaignard
2023-09-19 14:21   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 41/49] media: atomisp: " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 42/49] media: imx: " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 43/49] media: meson: vdec: " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 44/49] media: cedrus: " Benjamin Gaignard
2023-09-19 14:26   ` Hans Verkuil
2023-09-14 13:33 ` Benjamin Gaignard [this message]
2023-09-15  0:47   ` [PATCH v7 45/49] media: core: Add bitmap manage bufs array entries kernel test robot
2023-09-15 13:02     ` Benjamin Gaignard
2023-09-19 15:00   ` Hans Verkuil
2023-09-20 14:30     ` Benjamin Gaignard
2023-09-20 14:56       ` Hans Verkuil
2023-09-20 15:17         ` Benjamin Gaignard
2023-09-21  9:28         ` Benjamin Gaignard
2023-09-21 10:24           ` Hans Verkuil
2023-09-21 12:05             ` Benjamin Gaignard
2023-09-21 12:13               ` Hans Verkuil
2023-09-21 12:46                 ` Benjamin Gaignard
2023-09-21 13:07                   ` Benjamin Gaignard
2023-09-21 13:48                   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 46/49] media: core: Free range of buffers Benjamin Gaignard
2023-09-19 15:09   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 47/49] media: v4l2: Add DELETE_BUFS ioctl Benjamin Gaignard
2023-09-19 15:14   ` Hans Verkuil
2023-09-14 13:33 ` [PATCH v7 48/49] media: v4l2: Add mem2mem helpers for " Benjamin Gaignard
2023-09-14 13:33 ` [PATCH v7 49/49] media: test-drivers: Use helper " Benjamin Gaignard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230914133323.198857-46-benjamin.gaignard@collabora.com \
    --to=benjamin.gaignard@collabora.com \
    --cc=ezequiel@vanguardiasur.com.ar \
    --cc=gregkh@linuxfoundation.org \
    --cc=hverkuil-cisco@xs4all.nl \
    --cc=kernel@collabora.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=m.szyprowski@samsung.com \
    --cc=mchehab@kernel.org \
    --cc=ming.qian@nxp.com \
    --cc=nicolas.dufresne@collabora.com \
    --cc=p.zabel@pengutronix.de \
    --cc=tfiga@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox