public inbox for linux-arm-kernel@lists.infradead.org
 help / color / mirror / Atom feed
From: ming.qian@oss.nxp.com
To: linux-media@vger.kernel.org
Cc: mchehab@kernel.org, hverkuil-cisco@xs4all.nl,
	nicolas@ndufresne.ca, sebastian.fricke@collabora.com,
	shawnguo@kernel.org, s.hauer@pengutronix.de,
	kernel@pengutronix.de, festevam@gmail.com, linux-imx@nxp.com,
	Frank.li@nxp.com, xiahong.bao@nxp.com, eagle.zhou@nxp.com,
	imx@lists.linux.dev, linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org
Subject: [RFC PATCH 4/7] docs: media: v4l2-memtrack: Add driver API documentation
Date: Tue, 31 Mar 2026 15:23:14 +0800	[thread overview]
Message-ID: <20260331072347.253-5-ming.qian@oss.nxp.com> (raw)
In-Reply-To: <20260331072347.253-1-ming.qian@oss.nxp.com>

From: Ming Qian <ming.qian@oss.nxp.com>

Add comprehensive documentation for the V4L2 memory tracking infrastructure
in the driver API documentation. This documentation covers:

- Overview of the hierarchical memory tracking system
- Basic usage examples showing root node creation and memory tracking
- Hierarchical tracking with parent-child node relationships
- debugfs interface for runtime memory inspection
- Notification callback registration and usage warnings
- Visual examples of tree structure and debugfs output

The documentation provides driver developers with practical examples
of how to integrate memory tracking into their V4L2 drivers, including
proper setup during probe/remove, buffer allocation tracking, and
per-context memory monitoring.

Add the new v4l2-memtrack documentation to the v4l2-core index to make
it accessible in the generated documentation.

Signed-off-by: Ming Qian <ming.qian@oss.nxp.com>
---
 Documentation/driver-api/media/v4l2-core.rst  |   1 +
 .../driver-api/media/v4l2-memtrack.rst        | 140 ++++++++++++++++++
 2 files changed, 141 insertions(+)
 create mode 100644 Documentation/driver-api/media/v4l2-memtrack.rst

diff --git a/Documentation/driver-api/media/v4l2-core.rst b/Documentation/driver-api/media/v4l2-core.rst
index a5f5102c64cc..09765d028375 100644
--- a/Documentation/driver-api/media/v4l2-core.rst
+++ b/Documentation/driver-api/media/v4l2-core.rst
@@ -28,3 +28,4 @@ Video4Linux devices
     v4l2-tveeprom
     v4l2-jpeg
     v4l2-isp
+    v4l2-memtrack
diff --git a/Documentation/driver-api/media/v4l2-memtrack.rst b/Documentation/driver-api/media/v4l2-memtrack.rst
new file mode 100644
index 000000000000..bca6954cfa0b
--- /dev/null
+++ b/Documentation/driver-api/media/v4l2-memtrack.rst
@@ -0,0 +1,140 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+V4L2 Memory Usage Tracker
+=========================
+
+Overview
+--------
+
+The V4L2 memory tracking module provides hierarchical memory allocation
+tracking for V4L2 devices. It is useful for debugging memory leaks and
+monitoring buffer usage in video drivers.
+
+Features:
+
+- Tree-structured memory usage monitoring
+- Per-device and per-context tracking
+- debugfs interface for runtime inspection
+- Optional notification callbacks for memory changes
+- V4L2 control integration
+
+Usage Example
+-------------
+
+Basic usage in a V4L2 driver:
+
+.. code-block:: c
+
+    #include <media/v4l2-memtrack.h>
+
+    struct my_device {
+        struct v4l2_memtrack_node *memtrack;
+        /* ... */
+    };
+
+    static int my_probe(struct platform_device *pdev)
+    {
+        struct my_device *dev;
+
+        dev->memtrack = v4l2_memtrack_create_root("my-device");
+        if (!dev->memtrack)
+            return -ENOMEM;
+
+        return 0;
+    }
+
+    static void my_remove(struct platform_device *pdev)
+    {
+        v4l2_memtrack_destroy_node(dev->memtrack);
+    }
+
+    static int my_alloc_buffer(struct my_device *dev, size_t size)
+    {
+        void *buf = dma_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
+        if (!buf)
+            return -ENOMEM;
+
+        v4l2_memtrack_add(dev->memtrack, size);
+        return 0;
+    }
+
+    static void my_free_buffer(struct my_device *dev, size_t size)
+    {
+        dma_free_coherent(dev, size, buf, dma_addr);
+        v4l2_memtrack_sub(dev->memtrack, size);
+    }
+
+Hierarchical Tracking
+---------------------
+
+For more detailed tracking, create child nodes:
+
+.. code-block:: c
+
+    struct my_context {
+        struct v4l2_memtrack_node *memtrack;
+    };
+
+    static int my_open(struct file *file)
+    {
+        struct my_context *ctx;
+
+        ctx->memtrack = v4l2_memtrack_create_node(dev->memtrack, "context");
+        return 0;
+    }
+
+    static int my_close(struct file *file)
+    {
+        v4l2_memtrack_destroy_node(ctx->memtrack);
+        return 0;
+    }
+
+This creates a tree structure::
+
+    my-device (root)
+    ├── context (pid=1234)
+    │   ├── buffer: 4096 bytes
+    │   └── buffer: 8192 bytes
+    └── context (pid=5678)
+        └── buffer: 4096 bytes
+
+debugfs Interface
+-----------------
+
+Memory usage is exposed via debugfs at::
+
+    /sys/kernel/debug/v4l2-memtrack/<device-name>
+
+Example output::
+
+    Total memory usage: 16384 bytes
+    my-device (tgid=1000, pid=1000) usage: 16384
+      context (tgid=1234, pid=1234) usage: 12288 (count=2)
+      context (tgid=5678, pid=5678) usage: 4096 (count=1)
+
+Notification Callbacks
+----------------------
+
+Register callbacks to be notified of memory changes:
+
+.. code-block:: c
+
+    static void my_notify(struct v4l2_memtrack_node *node,
+                          size_t total, void *priv)
+    {
+        pr_info("Memory usage changed: %zu bytes\n", total);
+    }
+
+    v4l2_memtrack_register_notify(dev->memtrack, my_notify, dev);
+
+.. warning::
+
+   Callbacks are executed without holding internal locks, so most kernel
+   functions may be called safely. However, the callback MUST NOT call
+   v4l2_memtrack_unregister_notify() or v4l2_memtrack_destroy_node() on
+   the same node, as this will cause a deadlock.
+
+API Reference
+-------------
+
+.. kernel-doc:: include/media/v4l2-memtrack.h
-- 
2.53.0



  parent reply	other threads:[~2026-03-31  7:25 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-31  7:23 [RFC PATCH 0/7] media: amphion: Add DMA memory tracking support ming.qian
2026-03-31  7:23 ` [RFC PATCH 1/7] media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control ming.qian
2026-03-31 14:33   ` Frank Li
2026-03-31 14:54     ` Nicolas Dufresne
2026-04-01  2:23       ` Ming Qian(OSS)
2026-04-02  3:14         ` Ming Qian(OSS)
2026-04-08 21:11           ` Nicolas Dufresne
2026-04-09 14:29             ` Detlev Casanova
2026-04-10  2:53               ` Ming Qian(OSS)
2026-04-01  2:07     ` Ming Qian(OSS)
2026-03-31  7:23 ` [RFC PATCH 2/7] docs: " ming.qian
2026-03-31  7:23 ` [RFC PATCH 3/7] media: v4l2-memtrack: Add V4L2 memory tracking infrastructure ming.qian
2026-03-31  7:23 ` ming.qian [this message]
2026-03-31  7:23 ` [RFC PATCH 5/7] MAINTAINERS: Add entry for V4L2 memory usage tracker ming.qian
2026-03-31  7:23 ` [RFC PATCH 6/7] media: videobuf2: Add memory tracking support ming.qian
2026-03-31  7:23 ` [RFC PATCH 7/7] media: amphion: Add V4L2 " ming.qian

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=20260331072347.253-5-ming.qian@oss.nxp.com \
    --to=ming.qian@oss.nxp.com \
    --cc=Frank.li@nxp.com \
    --cc=eagle.zhou@nxp.com \
    --cc=festevam@gmail.com \
    --cc=hverkuil-cisco@xs4all.nl \
    --cc=imx@lists.linux.dev \
    --cc=kernel@pengutronix.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-imx@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=nicolas@ndufresne.ca \
    --cc=s.hauer@pengutronix.de \
    --cc=sebastian.fricke@collabora.com \
    --cc=shawnguo@kernel.org \
    --cc=xiahong.bao@nxp.com \
    /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