From: Ming Qian <ming.qian@oss.nxp.com>
Add optional memtrack field to vb2_queue for tracking MMAP buffer
memory usage. When a driver sets q->memtrack, the core automatically
tracks allocations via v4l2_memtrack_add/sub calls.
Only MMAP type buffers are tracked since DMABUF memory is allocated
externally by userspace.
Signed-off-by: Ming Qian <ming.qian@oss.nxp.com>
---
drivers/media/common/videobuf2/videobuf2-core.c | 13 +++++++++++++
include/media/videobuf2-core.h | 4 ++++
2 files changed, 17 insertions(+)
diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c
index adf668b213c2..e42fdf829b22 100644
--- a/drivers/media/common/videobuf2/videobuf2-core.c
+++ b/drivers/media/common/videobuf2/videobuf2-core.c
@@ -28,6 +28,7 @@
#include <media/videobuf2-core.h>
#include <media/v4l2-mc.h>
+#include <media/v4l2-memtrack.h>
#include <trace/events/vb2.h>
@@ -251,12 +252,19 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
/* Associate allocator private data with this plane */
vb->planes[plane].mem_priv = mem_priv;
+
+ if (q->memtrack)
+ v4l2_memtrack_add(q->memtrack, size, NULL);
}
return 0;
free:
/* Free already allocated memory if one of the allocations failed */
for (; plane > 0; --plane) {
+ unsigned long size = PAGE_ALIGN(vb->planes[plane - 1].length);
+
+ if (q->memtrack)
+ v4l2_memtrack_sub(q->memtrack, size, NULL);
call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
vb->planes[plane - 1].mem_priv = NULL;
}
@@ -269,9 +277,14 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
*/
static void __vb2_buf_mem_free(struct vb2_buffer *vb)
{
+ struct vb2_queue *q = vb->vb2_queue;
unsigned int plane;
for (plane = 0; plane < vb->num_planes; ++plane) {
+ unsigned long size = PAGE_ALIGN(vb->planes[plane].length);
+
+ if (q->memtrack)
+ v4l2_memtrack_sub(q->memtrack, size, NULL);
call_void_memop(vb, put, vb->planes[plane].mem_priv);
vb->planes[plane].mem_priv = NULL;
dprintk(vb->vb2_queue, 3, "freed plane %d of buffer %d\n",
diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h
index 4424d481d7f7..7cb6ff2fc892 100644
--- a/include/media/videobuf2-core.h
+++ b/include/media/videobuf2-core.h
@@ -47,6 +47,7 @@ enum vb2_memory {
struct vb2_fileio_data;
struct vb2_threadio_data;
struct vb2_buffer;
+struct v4l2_memtrack_node;
/**
* struct vb2_mem_ops - memory handling/memory allocator operations.
@@ -558,6 +559,7 @@ struct vb2_buf_ops {
* driver implements the V4L2_CID_MIN_BUFFERS_FOR_CAPTURE/OUTPUT
* control.
* @alloc_devs: &struct device memory type/allocator-specific per-plane device
+ * @memtrack: optional memory tracking node for debugging and monitoring.
*/
/*
* Private elements (won't appear at the uAPI book):
@@ -632,6 +634,8 @@ struct vb2_queue {
struct device *alloc_devs[VB2_MAX_PLANES];
+ struct v4l2_memtrack_node *memtrack;
+
/* private: internal use only */
struct mutex mmap_lock;
unsigned int memory;
--
2.53.0