media: amphion: Add DMA memory tracking support
Subsystems (4)
cover media: amphion: Add DMA memory tracking support
From: Ming Qian <[email protected]>
This series adds DMA memory tracking support to the Amphion VPU driver, allowing userspace to monitor memory consumption of codec instances.
To implement this feature in a reusable way, a new V4L2 memory tracking infrastructure (v4l2-memtrack) is introduced, which can be adopted by other V4L2 drivers in the future.
Background
==========
The Amphion VPU codec driver allocates various DMA buffers internally:
- Firmware boot region and RPC buffers
- Stream ring buffers
- Codec-specific buffers (MBI, DCP, reference frames)
- Encoder/decoder working buffers
These allocations are not visible to userspace, making it difficult to debug memory issues or monitor resource usage in multi-instance scenarios (e.g., transcoding servers with multiple simultaneous streams).
Solution
========
This series introduces:
1. V4L2_CID_MEMORY_USAGE control
- New read-only control reporting memory usage in bytes
- Standard V4L2 interface, queryable via VIDIOC_G_CTRL
2. v4l2-memtrack infrastructure
- Hierarchical memory tracking (device -> instance -> queue)
- debugfs interface at /sys/kernel/debug/v4l2-memtrack/
- Reusable by other V4L2 drivers
3. videobuf2 integration
- Automatic tracking of vb2 buffer allocations
4. Amphion VPU integration
- Tracks all internal DMA allocations
- Organized as: device -> instance -> buffers
Ming Qian (7):
media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
docs: media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
media: v4l2-memtrack: Add V4L2 memory tracking infrastructure
docs: media: v4l2-memtrack: Add driver API documentation
MAINTAINERS: Add entry for V4L2 memory usage tracker
media: videobuf2: Add memory tracking support
media: amphion: Add V4L2 memory tracking support
Documentation/driver-api/media/v4l2-core.rst | 1 + .../driver-api/media/v4l2-memtrack.rst | 140 +++ .../userspace-api/media/v4l/control.rst | 22 +- MAINTAINERS | 8 + drivers/media/common/Kconfig | 1 + drivers/media/common/Makefile | 2 +- drivers/media/common/v4l2-memtrack/Kconfig | 19 + drivers/media/common/v4l2-memtrack/Makefile | 3 + .../common/v4l2-memtrack/v4l2-memtrack.c | 825 ++++++++++++++++++ .../media/common/videobuf2/videobuf2-core.c | 13 + drivers/media/platform/amphion/Kconfig | 1 + drivers/media/platform/amphion/vdec.c | 9 + drivers/media/platform/amphion/venc.c | 9 + drivers/media/platform/amphion/vpu.h | 7 + drivers/media/platform/amphion/vpu_core.c | 6 + drivers/media/platform/amphion/vpu_dbg.c | 5 + drivers/media/platform/amphion/vpu_drv.c | 2 + drivers/media/platform/amphion/vpu_v4l2.c | 35 +- drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 + include/media/v4l2-memtrack.h | 220 +++++ include/media/videobuf2-core.h | 4 + include/uapi/linux/v4l2-controls.h | 4 +- 22 files changed, 1340 insertions(+), 4 deletions(-) create mode 100644 Documentation/driver-api/media/v4l2-memtrack.rst create mode 100644 drivers/media/common/v4l2-memtrack/Kconfig create mode 100644 drivers/media/common/v4l2-memtrack/Makefile create mode 100644 drivers/media/common/v4l2-memtrack/v4l2-memtrack.c create mode 100644 include/media/v4l2-memtrack.h
base-commit: 4fbeef21f5387234111b5d52924e77757626faa5
prerequisite-patch-id: 0000000000000000000000000000000000000000
Signature · 1 line
2.53.0
RFC PATCH 1/7 media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
From: Ming Qian <[email protected]>
Add a new read-only control V4L2_CID_MEMORY_USAGE that allows applications to query the total amount of memory currently used by a device instance.
This control reports the memory consumption in bytes, including internal buffers, intermediate processing data, and other driver-managed allocations. Applications can use this information for debugging, resource monitoring, or making informed decisions about buffer allocation strategies.
--- drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++++++++ include/uapi/linux/v4l2-controls.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-ctrls-defs.c +8 -0
@@ -831,6 +831,7 @@ const char *v4l2_ctrl_get_name(u32 id)case V4L2_CID_ALPHA_COMPONENT :return "Alpha Component" ;case V4L2_CID_COLORFX_CBCR :return "Color Effects, CbCr" ;case V4L2_CID_COLORFX_RGB :return "Color Effects, RGB" ; +case V4L2_CID_MEMORY_USAGE :return "Memory Usage" ;/* * Codec controls
@@ -1476,6 +1477,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *min =0 ; *max =0xffff ;break ; +case V4L2_CID_MEMORY_USAGE : + *type =V4L2_CTRL_TYPE_INTEGER64 ; + *flags |=V4L2_CTRL_FLAG_READ_ONLY ; + *min =0 ; + *max =S64_MAX ; + *step =1 ; +break ;case V4L2_CID_FLASH_FAULT :case V4L2_CID_JPEG_ACTIVE_MARKER :case V4L2_CID_3A_LOCK :
include/uapi/linux/v4l2-controls.h +3 -1
@@ -110,8 +110,10 @@ enum v4l2_colorfx {#define V4L2_CID_COLORFX_CBCR (V4L2_CID_BASE+42)#define V4L2_CID_COLORFX_RGB (V4L2_CID_BASE+43) +#define V4L2_CID_MEMORY_USAGE (V4L2_CID_BASE+44) +/* last CID + 1 */ -#define V4L2_CID_LASTP1 (V4L2_CID_BASE+44) +#define V4L2_CID_LASTP1 (V4L2_CID_BASE+45)/* USER-class private control IDs */
Signature · 1 line
2.53.0
Frank Li
maintainerRe: [RFC PATCH 1/7] media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
Quoted 6 lines · On Tue, Mar 31, 2026 at 03:23:11PM +0800, [email protected] wrote:
From: Ming Qian <[email protected]>
Add a new read-only control
V4L2_CID_MEMORY_USAGEthat allows applications to query the total amount of memory currently used by a device instance.
Quoted 8 lines
This control reports the memory consumption in bytes, including internal buffers, intermediate processing data, and other driver-managed allocations. Applications can use this information for debugging, resource monitoring, or making informed decisions about buffer allocation strategies.
Signed-off-by Ming Qian <[email protected]>---
Not sure why not export these information by debugfs, or any benefit vs debugfs?
Generanlly document should be first patch, then driver change.
Frank
Quoted 49 lines
drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++++++++ include/uapi/linux/v4l2-controls.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-)drivers/media/v4l2-core/v4l2-ctrls-defs.c +8
-0@@ -831,6 +831,7 @@ const char *v4l2_ctrl_get_name(u32 id)case V4L2_CID_ALPHA_COMPONENT :return "Alpha Component" ;case V4L2_CID_COLORFX_CBCR :return "Color Effects, CbCr" ;case V4L2_CID_COLORFX_RGB :return "Color Effects, RGB" ; +case V4L2_CID_MEMORY_USAGE :return "Memory Usage" ;/* * Codec controls @@ -1476,6 +1477,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *min =0 ; *max =0xffff ;break ; +case V4L2_CID_MEMORY_USAGE : + *type =V4L2_CTRL_TYPE_INTEGER64 ; + *flags |=V4L2_CTRL_FLAG_READ_ONLY ; + *min =0 ; + *max =S64_MAX ; + *step =1 ; +break ;case V4L2_CID_FLASH_FAULT :case V4L2_CID_JPEG_ACTIVE_MARKER :case V4L2_CID_3A_LOCK :include/uapi/linux/v4l2-controls.h +3
-2@@ -110,8 +110,10 @@ enum v4l2_colorfx {#define V4L2_CID_COLORFX_CBCR (V4L2_CID_BASE+42)#define V4L2_CID_COLORFX_RGB (V4L2_CID_BASE+43) +#define V4L2_CID_MEMORY_USAGE (V4L2_CID_BASE+44) +/* last CID + 1 */ -#define V4L2_CID_LASTP1 (V4L2_CID_BASE+44) +#define V4L2_CID_LASTP1 (V4L2_CID_BASE+45)/* USER-class private control IDs */ --2.53.0
Re: [RFC PATCH 1/7] media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
Le mardi 31 mars 2026 à 10:33 -0400, Frank Li a écrit :
Quoted 13 lines · On Tue, Mar 31, 2026 at 03:23:11PM +0800, [email protected] wrote:
Quoted 12 lines
From: Ming Qian <[email protected]>
Add a new read-only control
V4L2_CID_MEMORY_USAGEthat allows applications to query the total amount of memory currently used by a device instance.This control reports the memory consumption in bytes, including internal buffers, intermediate processing data, and other driver-managed allocations. Applications can use this information for debugging, resource monitoring, or making informed decisions about buffer allocation strategies.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Ming Qian <[email protected]>---
Not sure why not export these information by debugfs, or any benefit vs debugfs?
There is also a on-going proposal that uses fdinfo.
Nicolas
Quoted 54 lines
Generanlly document should be first patch, then driver change.
Frank
Quoted 49 lines
drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++++++++ include/uapi/linux/v4l2-controls.h | 4 +++-
2 files changed, 11 insertions(+), 1 deletion(-)drivers/media/v4l2-core/v4l2-ctrls-defs.c +8
-0@@ -831,6 +831,7 @@ const char *v4l2_ctrl_get_name(u32 id) case V4L2_CID_ALPHA_COMPONENT: return "Alpha Component"; case V4L2_CID_COLORFX_CBCR: return "Color Effects, CbCr"; case V4L2_CID_COLORFX_RGB: return "Color Effects, RGB"; +case V4L2_CID_MEMORY_USAGE :return "Memory Usage" ; /** Codec controls @@ -1476,6 +1477,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *min = 0; *max = 0xffff; break; +case V4L2_CID_MEMORY_USAGE : + *type =V4L2_CTRL_TYPE_INTEGER64 ; + *flags |=V4L2_CTRL_FLAG_READ_ONLY ; + *min =0 ; + *max =S64_MAX ; + *step =1 ; +break ;case
V4L2_CID_FLASH_FAULT:
caseV4L2_CID_JPEG_ACTIVE_MARKER:
caseV4L2_CID_3A_LOCK:include/uapi/linux/v4l2-controls.h +3
-2@@ -110,8 +110,10 @@ enum v4l2_colorfx { #define V4L2_CID_COLORFX_CBCR (V4L2_CID_BASE+42) #define V4L2_CID_COLORFX_RGB (V4L2_CID_BASE+43) +#define V4L2_CID_MEMORY_USAGE (V4L2_CID_BASE+44) + /* last CID + 1 */ -#define V4L2_CID_LASTP1 (V4L2_CID_BASE+44) +#define V4L2_CID_LASTP1 (V4L2_CID_BASE+45) /* USER-class private control IDs */ --2.53.0
Re: [RFC PATCH 1/7] media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
Hi Nicolas,
Quoted 17 lines · Nicolas Dufresne
Quoted 16 lines · Frank Li
Quoted 14 lines · On Tue, Mar 31, 2026 at 03:23:11PM +0800, [email protected] wrote:
From: Ming Qian <[email protected]>
Add a new read-only control
V4L2_CID_MEMORY_USAGEthat allows applications to query the total amount of memory currently used by a device instance.This control reports the memory consumption in bytes, including internal buffers, intermediate processing data, and other driver-managed allocations. Applications can use this information for debugging, resource monitoring, or making informed decisions about buffer allocation strategies.
Signed-off-by Ming Qian <[email protected]>---
Quoted 7 lines
Quoted 2 lines
Not sure why not export these information by debugfs, or any benefit vs debugfs?
There is also a on-going proposal that uses fdinfo.
Nicolas
Thanks for the reminder about the ongoing fdinfo proposal.
Just to confirm, you are referring to Detlev’s ongoing fdinfo proposal, specifically this series:
https://lore.kernel.org/lkml/[email protected]/
I will align my work with it and switch to using fdinfo. Once the show_fdinfo support from that series is merged, I will prepare the next revision of my patch accordingly.
Regards,
Ming
Quoted 54 lines
Quoted 54 lines
Generanlly document should be first patch, then driver change.
Frank
Quoted 49 lines
drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++++++++ include/uapi/linux/v4l2-controls.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-)drivers/media/v4l2-core/v4l2-ctrls-defs.c +8
-0@@ -831,6 +831,7 @@ const char *v4l2_ctrl_get_name(u32 id)case V4L2_CID_ALPHA_COMPONENT :return "Alpha Component" ;case V4L2_CID_COLORFX_CBCR :return "Color Effects, CbCr" ;case V4L2_CID_COLORFX_RGB :return "Color Effects, RGB" ; +case V4L2_CID_MEMORY_USAGE :return "Memory Usage" ;/* * Codec controls @@ -1476,6 +1477,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *min =0 ; *max =0xffff ;break ; +case V4L2_CID_MEMORY_USAGE : + *type =V4L2_CTRL_TYPE_INTEGER64 ; + *flags |=V4L2_CTRL_FLAG_READ_ONLY ; + *min =0 ; + *max =S64_MAX ; + *step =1 ; +break ;case V4L2_CID_FLASH_FAULT :case V4L2_CID_JPEG_ACTIVE_MARKER :case V4L2_CID_3A_LOCK :include/uapi/linux/v4l2-controls.h +3
-2@@ -110,8 +110,10 @@ enum v4l2_colorfx {#define V4L2_CID_COLORFX_CBCR (V4L2_CID_BASE+42)#define V4L2_CID_COLORFX_RGB (V4L2_CID_BASE+43) +#define V4L2_CID_MEMORY_USAGE (V4L2_CID_BASE+44) +/* last CID + 1 */ -#define V4L2_CID_LASTP1 (V4L2_CID_BASE+44) +#define V4L2_CID_LASTP1 (V4L2_CID_BASE+45)/* USER-class private control IDs */ --2.53.0
Re: [RFC PATCH 1/7] media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
Hi Nicolas,
Quoted 34 lines · Ming Qian(OSS)
Hi Nicolas,
Quoted 17 lines · Nicolas Dufresne
Quoted 16 lines · Frank Li
Quoted 14 lines · On Tue, Mar 31, 2026 at 03:23:11PM +0800, [email protected] wrote:
From: Ming Qian <[email protected]>
Add a new read-only control
V4L2_CID_MEMORY_USAGEthat allows applications to query the total amount of memory currently used by a device instance.This control reports the memory consumption in bytes, including internal buffers, intermediate processing data, and other driver-managed allocations. Applications can use this information for debugging, resource monitoring, or making informed decisions about buffer allocation strategies.
Signed-off-by Ming Qian <[email protected]>---
Quoted 7 lines
Quoted 2 lines
Not sure why not export these information by debugfs, or any benefit vs debugfs?
There is also a on-going proposal that uses fdinfo.
Nicolas
Thanks for the reminder about the ongoing fdinfo proposal.
Just to confirm, you are referring to Detlev’s ongoing fdinfo proposal, specifically this series:
https://lore.kernel.org/lkml/20260212162328.192217-1- [email protected]/
Quoted 7 lines
I will align my work with it and switch to using fdinfo. Once the
show_fdinfosupport from that series is merged, I will prepare the next revision of my patch accordingly.Regards,
Ming
Regarding the discussion about using fdinfo instead of a V4L2 control, I have two questions:
1. Key consistency in fdinfo
fdinfo uses key–value pairs, which is flexible, but if multiple drivers want to expose the same “memory usage” information, they need to agree on a common key name and meaning. Otherwise user‑space must handle each driver differently. A V4L2 control naturally provides a unified interface without this coordination effort.
2. Lack of notification in fdinfo
With a control, user‑space can subscribe to control events and receive notifications when the memory usage changes. fdinfo does not have a built‑in event mechanism, so users must either poll or rely on additional eventfd‑like or custom event mechanisms.
Do you have any suggestions or existing practices to address these two issues when using fdinfo?
Thanks again for your time and comments.
Regards,
Ming
Quoted 57 lines
Quoted 56 lines
Quoted 56 lines
Generanlly document should be first patch, then driver change.
Frank
Quoted 51 lines
drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++++++++ include/uapi/linux/v4l2-controls.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-)drivers/media/v4l2-core/v4l2-ctrls-defs.c +1
-0@@ -831,6 +831,7 @@ const char *v4l2_ctrl_get_name(u32 id)case V4L2_CID_ALPHA_COMPONENT :return "Alpha Component" ;case V4L2_CID_COLORFX_CBCR :return "Color Effects, CbCr" ;case V4L2_CID_COLORFX_RGB :return "Color Effects, RGB"; +case V4L2_CID_MEMORY_USAGE :return "Memory Usage" ;/* * Codec controls
drivers/media/v4l2-core/v4l2-ctrls-defs.c +7
-0@@ -1476,6 +1477,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *min =0 ; *max =0xffff ;break ; +case V4L2_CID_MEMORY_USAGE : + *type =V4L2_CTRL_TYPE_INTEGER64 ; + *flags |=V4L2_CTRL_FLAG_READ_ONLY ; + *min =0 ; + *max =S64_MAX ; + *step =1 ; +break ;case V4L2_CID_FLASH_FAULT :case V4L2_CID_JPEG_ACTIVE_MARKER :case V4L2_CID_3A_LOCK:include/uapi/linux/v4l2-controls.h +3
-1@@ -110,8 +110,10 @@ enum v4l2_colorfx {#define V4L2_CID_COLORFX_CBCR (V4L2_CID_BASE+42)#define V4L2_CID_COLORFX_RGB (V4L2_CID_BASE+43) +#define V4L2_CID_MEMORY_USAGE (V4L2_CID_BASE+44) +/* last CID + 1 */ -#define V4L2_CID_LASTP1 (V4L2_CID_BASE+44) +#define V4L2_CID_LASTP1 (V4L2_CID_BASE+45)/* USER-class private control IDs */ Signature · 1 line
2.53.0
Re: [RFC PATCH 1/7] media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
Le jeudi 02 avril 2026 à 11:14 +0800, Ming Qian(OSS) a écrit :
Quoted 64 lines
Hi Nicolas,
Quoted 35 lines · Ming Qian(OSS)
Hi Nicolas,
Quoted 17 lines · Nicolas Dufresne
Quoted 16 lines · Frank Li
Quoted 14 lines · On Tue, Mar 31, 2026 at 03:23:11PM +0800, [email protected] wrote:
From: Ming Qian <[email protected]>
Add a new read-only control
V4L2_CID_MEMORY_USAGEthat allows applications to query the total amount of memory currently used by a device instance.This control reports the memory consumption in bytes, including internal buffers, intermediate processing data, and other driver-managed allocations. Applications can use this information for debugging, resource monitoring, or making informed decisions about buffer allocation strategies.
Signed-off-by Ming Qian <[email protected]>---
Quoted 7 lines
Quoted 2 lines
Not sure why not export these information by debugfs, or any benefit vs debugfs?
There is also a on-going proposal that uses fdinfo.
Nicolas
Thanks for the reminder about the ongoing fdinfo proposal.
Just to confirm, you are referring to Detlev’s ongoing fdinfo proposal, specifically this series:
https://lore.kernel.org/lkml/20260212162328.192217-1-
[email protected]/Quoted 7 lines
I will align my work with it and switch to using fdinfo. Once the
show_fdinfosupport from that series is merged, I will prepare the next revision of my patch accordingly.Regards,
MingRegarding the discussion about using fdinfo instead of a V4L2 control, I have two questions:
1. Key consistency in fdinfo
fdinfo uses key–value pairs, which is flexible, but if multiple drivers want to expose the same “memory usage” information, they need to agree on a common key name and meaning. Otherwise user‑space must handle each driver differently. A V4L2 control naturally provides a unified interface without this coordination effort.2. Lack of notification in fdinfo
With a control, user‑space can subscribe to control events and receive notifications when the memory usage changes. fdinfo does not have a built‑in event mechanism, so users must either poll or rely on additional eventfd‑like or custom event mechanisms.
Quoted 4 lines
Do you have any suggestions or existing practices to address these two issues when using fdinfo?
Thanks again for your time and comments.
Added Detlev in CC. You can also refer to his work through:
https://lore.kernel.org/all/[email protected]/
Nicolas
Quoted 63 lines
Regards,
MingQuoted 59 lines
Quoted 58 lines
Quoted 58 lines
Generanlly document should be first patch, then driver change.
Frank
Quoted 53 lines
drivers/media/v4l2-core/v4l2-ctrls-defs.c| 8 ++++++++ include/uapi/linux/v4l2-controls.h | 4 +++-2 files changed, 11 insertions(+), 1 deletion(-)drivers/ renamed from drivers/media/v4l2-core/v4l2-ctrls-defs.c +0
-0
media/v4l2-core/v4l2-ctrls-defs.c
index551426c4cd01..053db78ff661100644drivers/media/v4l2-core/v4l2-ctrls-defs.c +1
-0@@ -831,6 +831,7 @@ const char *v4l2_ctrl_get_name(u32 id) case V4L2_CID_ALPHA_COMPONENT: return "Alpha Component"; case V4L2_CID_COLORFX_CBCR: return "Color Effects, CbCr"; case V4L2_CID_COLORFX_RGB: return "Color Effects, RGB"; + case V4L2_CID_MEMORY_USAGE: return "Memory Usage"; /** Codec controls
drivers/media/v4l2-core/v4l2-ctrls-defs.c +7
-0@@ -1476,6 +1477,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *min = 0; *max = 0xffff; break; + case V4L2_CID_MEMORY_USAGE: + *type = V4L2_CTRL_TYPE_INTEGER64; + *flags |= V4L2_CTRL_FLAG_READ_ONLY; + *min = 0; + *max = S64_MAX; + *step = 1; + break;case
V4L2_CID_FLASH_FAULT:
caseV4L2_CID_JPEG_ACTIVE_MARKER:
caseV4L2_CID_3A_LOCK:include/uapi/ renamed from include/uapi/linux/v4l2-controls.h +0
-0
linux/v4l2-controls.h
index68dd0c4e47b2..02c6f960d38e100644include/uapi/linux/v4l2-controls.h +3
-1@@ -110,8 +110,10 @@ enum v4l2_colorfx { #define V4L2_CID_COLORFX_CBCR (V4L2_CID_BASE+42) #define V4L2_CID_COLORFX_RGB (V4L2_CID_BASE+43) +#define V4L2_CID_MEMORY_USAGE (V4L2_CID_BASE+44) + /* last CID + 1 */ -#define V4L2_CID_LASTP1 (V4L2_CID_BASE+44) +#define V4L2_CID_LASTP1 (V4L2_CID_BASE+45) /* USER-class private control IDs */Signature · 1 line
2.53.0
Re: [RFC PATCH 1/7] media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
Quoted 64 lines · Nicolas Dufresne
Le jeudi 02 avril 2026 à 11:14 +0800, Ming Qian(OSS) a écrit :
Quoted 63 lines
Hi Nicolas,
Quoted 32 lines · Ming Qian(OSS)
Hi Nicolas,
Quoted 14 lines · Nicolas Dufresne
Quoted 13 lines · Frank Li
Quoted 12 lines · On Tue, Mar 31, 2026 at 03:23:11PM +0800, [email protected] wrote:
From: Ming Qian <[email protected]>
Add a new read-only control
V4L2_CID_MEMORY_USAGEthat allows applications to query the total amount of memory currently used by a device instance.This control reports the memory consumption in bytes, including internal buffers, intermediate processing data, and other driver-managed allocations. Applications can use this information for debugging, resource monitoring, or making informed decisions about buffer allocation strategies.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Signed-off-by Ming Qian <[email protected]>---
Not sure why not export these information by debugfs, or any benefit vs debugfs?
There is also a on-going proposal that uses fdinfo.
Nicolas
Thanks for the reminder about the ongoing fdinfo proposal.
Just to confirm, you are referring to Detlev’s ongoing fdinfo proposal, specifically this series:
https://lore.kernel.org/lkml/20260212162328.192217-1-
[email protected]/Quoted 7 lines
I will align my work with it and switch to using fdinfo. Once the
show_fdinfosupport from that series is merged, I will prepare the next revision of my patch accordingly.Regards,
MingRegarding the discussion about using fdinfo instead of a V4L2 control, I have two questions:
1. Key consistency in fdinfo
fdinfo uses key–value pairs, which is flexible, but if multiple drivers want to expose the same “memory usage” information, they need to agree on a common key name and meaning. Otherwise user‑space must handle each driver differently. A V4L2 control naturally provides a unified interface without this coordination effort.2. Lack of notification in fdinfo
With a control, user‑space can subscribe to control events and receive notifications when the memory usage changes. fdinfo does not have a built‑in event mechanism, so users must either poll or rely on additional eventfd‑like or custom event mechanisms.Do you have any suggestions or existing practices to address these two issues when using fdinfo?
Quoted 6 lines
Quoted 1 line
Thanks again for your time and comments.
Added Detlev in CC. You can also refer to his work through:
https://lore.kernel.org/all/[email protected]/
Nicolas
Hi Ming !
One of the reasons for using fdinfo is that it's already being used in the drm subsystem and it is working well.
Of course, in DRM, drivers don't allocate a lot of memory themselves, userspace drivers (in mesa) go through the DRM uAPI to allocate buffers, making the DRM subsystem aware of all allocated memory. That lets DRM show memory stats in a standard way for all drm drivers. In v4l2, memory allocation is shared between userspace and the driver.
We could have drivers report memory usage through a callback and v4l2-core can add the standard field based on that.
For notifications, I don't really see a need for that, most tracing tools will use polling (I'm thinking perfetto, but also top-like tools). We could have a max-mem-usage field if we'd want to make sure we don't miss the maximum memory usage between 2 polls.
Finally, I think v4l2 controls should only be used to control, configure and exchange data with video devices, not get stat information on what the driver is doing.
Detlev.
Quoted 61 lines
Quoted 61 lines
Regards,
MingQuoted 57 lines
Quoted 57 lines
Quoted 57 lines
Generanlly document should be first patch, then driver change.
Frank
Quoted 53 lines
drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++++++++ include/uapi/linux/v4l2-controls.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-)drivers/ renamed from drivers/media/v4l2-core/v4l2-ctrls-defs.c +0
-0
media/v4l2-core/v4l2-ctrls-defs.c
index551426c4cd01..053db78ff661100644drivers/media/v4l2-core/v4l2-ctrls-defs.c +1
-0@@ -831,6 +831,7 @@ const char *v4l2_ctrl_get_name(u32 id) case V4L2_CID_ALPHA_COMPONENT: return "Alpha Component"; case V4L2_CID_COLORFX_CBCR: return "Color Effects, CbCr"; case V4L2_CID_COLORFX_RGB: return "Color Effects, RGB"; + case V4L2_CID_MEMORY_USAGE: return "Memory Usage"; /** Codec controls
drivers/media/v4l2-core/v4l2-ctrls-defs.c +7
-0@@ -1476,6 +1477,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *min = 0; *max = 0xffff; break; + case V4L2_CID_MEMORY_USAGE: + *type = V4L2_CTRL_TYPE_INTEGER64; + *flags |= V4L2_CTRL_FLAG_READ_ONLY; + *min = 0; + *max = S64_MAX; + *step = 1; + break; case V4L2_CID_FLASH_FAULT: case V4L2_CID_JPEG_ACTIVE_MARKER:case
V4L2_CID_3A_LOCK:include/uapi/ renamed from include/uapi/linux/v4l2-controls.h +0
-0
linux/v4l2-controls.h
index68dd0c4e47b2..02c6f960d38e100644include/uapi/linux/v4l2-controls.h +3
-1@@ -110,8 +110,10 @@ enum v4l2_colorfx { #define V4L2_CID_COLORFX_CBCR (V4L2_CID_BASE+42) #define V4L2_CID_COLORFX_RGB (V4L2_CID_BASE+43) +#define V4L2_CID_MEMORY_USAGE (V4L2_CID_BASE+44) + /* last CID + 1 */ -#define V4L2_CID_LASTP1 (V4L2_CID_BASE+44) +#define V4L2_CID_LASTP1 (V4L2_CID_BASE+45) /* USER-class private control IDs */Signature · 1 line
2.53.0
Re: [RFC PATCH 1/7] media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
Hi Detlev,
Quoted 80 lines · Detlev Casanova
Quoted 64 lines · Nicolas Dufresne
Quoted 63 lines · Ming Qian(OSS)
Hi Nicolas,
Quoted 32 lines · Ming Qian(OSS)
Hi Nicolas,
Quoted 14 lines · Nicolas Dufresne
Quoted 13 lines · Frank Li
Quoted 12 lines · On Tue, Mar 31, 2026 at 03:23:11PM +0800, [email protected] wrote:
From: Ming Qian <[email protected]>
Add a new read-only control
V4L2_CID_MEMORY_USAGEthat allows applications to query the total amount of memory currently used by a device instance.This control reports the memory consumption in bytes, including internal buffers, intermediate processing data, and other driver-managed allocations. Applications can use this information for debugging, resource monitoring, or making informed decisions about buffer allocation strategies.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Signed-off-by Ming Qian <[email protected]>---
Not sure why not export these information by debugfs, or any benefit vs debugfs?
There is also a on-going proposal that uses fdinfo.
Nicolas
Thanks for the reminder about the ongoing fdinfo proposal.
Just to confirm, you are referring to Detlev’s ongoing fdinfo proposal, specifically this series:
https://lore.kernel.org/lkml/20260212162328.192217-1-
[email protected]/Quoted 7 lines
I will align my work with it and switch to using fdinfo. Once the
show_fdinfosupport from that series is merged, I will prepare the next revision of my patch accordingly.Regards,
MingRegarding the discussion about using fdinfo instead of a V4L2 control, I have two questions:
1. Key consistency in fdinfo
fdinfo uses key–value pairs, which is flexible, but if multiple drivers want to expose the same “memory usage” information, they need to agree on a common key name and meaning. Otherwise user‑space must handle each driver differently. A V4L2 control naturally provides a unified interface without this coordination effort.2. Lack of notification in fdinfo
With a control, user‑space can subscribe to control events and receive notifications when the memory usage changes. fdinfo does not have a built‑in event mechanism, so users must either poll or rely on additional eventfd‑like or custom event mechanisms.Do you have any suggestions or existing practices to address these two issues when using fdinfo?
Quoted 6 lines
Quoted 1 line
Thanks again for your time and comments.
Added Detlev in CC. You can also refer to his work through:
https://lore.kernel.org/all/20260212162328.192217-1- [email protected]/
Nicolas
Hi Ming !
One of the reasons for using fdinfo is that it's already being used in the drm subsystem and it is working well.
Of course, in DRM, drivers don't allocate a lot of memory themselves, userspace drivers (in mesa) go through the DRM uAPI to allocate buffers, making the DRM subsystem aware of all allocated memory. That lets DRM show memory stats in a standard way for all drm drivers. In v4l2, memory allocation is shared between userspace and the driver.
We could have drivers report memory usage through a callback and v4l2- core can add the standard field based on that.
Quoted 6 lines
For notifications, I don't really see a need for that, most tracing tools will use polling (I'm thinking perfetto, but also top-like tools). We could have a max-mem-usage field if we'd want to make sure we don't miss the maximum memory usage between 2 polls.
Finally, I think v4l2 controls should only be used to control, configure and exchange data with video devices, not get stat information on what the driver is doing.
Detlev.
Thank you for your detailed explanation and suggestions. Your points make sense, especially regarding fdinfo for standardized stats reporting, polling being sufficient for tracing tools, and keeping V4L2 controls focused on device control rather than driver statistics.
I'll revisit the design accordingly.
Regards,
Ming
Quoted 65 lines
Quoted 64 lines
Quoted 64 lines
Regards,
MingQuoted 60 lines
Quoted 60 lines
Quoted 60 lines
Generanlly document should be first patch, then driver change.
Frank
Quoted 56 lines
drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++++++++ include/uapi/linux/v4l2-controls.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-)drivers/ renamed from drivers/media/v4l2-core/v4l2-ctrls-defs.c +0
-0
media/v4l2-core/v4l2-ctrls-defs.c
index551426c4cd01..053db78ff661100644drivers/media/v4l2-core/v4l2-ctrls-defs.c +0
-0@@ -831,6 +831,7 @@ const char *v4l2_ctrl_get_name(u32 id)case V4L2_CID_ALPHA_COMPONENT :return "Alpha Component";case V4L2_CID_COLORFX_CBCR :return "Color Effects, CbCr"; case V4L2_CID_COLORFX_RGB: return "Color Effects,RGB";
drivers/media/v4l2-core/v4l2-ctrls-defs.c +8
-0+case V4L2_CID_MEMORY_USAGE :return "Memory Usage" ;/* * Codec controls @@ -1476,6 +1477,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *min =0 ; *max =0xffff ;break ; +case V4L2_CID_MEMORY_USAGE : + *type =V4L2_CTRL_TYPE_INTEGER64 ; + *flags |=V4L2_CTRL_FLAG_READ_ONLY ; + *min =0 ; + *max =S64_MAX ; + *step =1 ; +break ;case V4L2_CID_FLASH_FAULT :case V4L2_CID_JPEG_ACTIVE_MARKER :case V4L2_CID_3A_LOCK:include/uapi/ renamed from include/uapi/linux/v4l2-controls.h +0
-0
linux/v4l2-controls.h
index68dd0c4e47b2..02c6f960d38e100644include/uapi/linux/v4l2-controls.h +3
-1@@ -110,8 +110,10 @@ enum v4l2_colorfx {#define V4L2_CID_COLORFX_CBCR (V4L2_CID_BASE+42)#define V4L2_CID_COLORFX_RGB (V4L2_CID_BASE+43) +#define V4L2_CID_MEMORY_USAGE (V4L2_CID_BASE+44) +/* last CID + 1 */ -#define V4L2_CID_LASTP1 (V4L2_CID_BASE+44) +#define V4L2_CID_LASTP1 (V4L2_CID_BASE+45)/* USER-class private control IDs */ Signature · 1 line
2.53.0
Re: [RFC PATCH 1/7] media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
In reply to Frank LiHi Frank,
Quoted 16 lines · Frank Li
Quoted 6 lines · On Tue, Mar 31, 2026 at 03:23:11PM +0800, [email protected] wrote:
From: Ming Qian <[email protected]>
Add a new read-only control
V4L2_CID_MEMORY_USAGEthat allows applications to query the total amount of memory currently used by a device instance.Quoted 8 lines
This control reports the memory consumption in bytes, including internal buffers, intermediate processing data, and other driver-managed allocations. Applications can use this information for debugging, resource monitoring, or making informed decisions about buffer allocation strategies.
Signed-off-by Ming Qian <[email protected]>---
Quoted 7 lines
Not sure why not export these information by debugfs, or any benefit vs debugfs?
Generanlly document should be first patch, then driver change.
Frank
Thanks for the comments.
The key reason for adding a V4L2 ctrl is that the original requirement (from Android) is to query the memory usage per VPU instance in a simple and stable way. A ctrl naturally fits this use case, as userspace already has the instance fd and does not need to locate or map any external paths. In contrast, relying only on debugfs would require userspace to figure out the relationship between an instance and a debugfs node, which is neither stable nor friendly for production systems where debugfs may not even be available.
Debugfs is added only to expose the full hierarchical memory‑tracking structure (device → instance → queue) for debugging and analysis. It complements the ctrl, but cannot replace it for the original functional requirement.
I will reorder the documentation patch to come first in the next revision.
Regards,
Ming
Quoted 50 lines
Quoted 50 lines
drivers/media/v4l2-core/v4l2-ctrls-defs.c | 8 ++++++++ include/uapi/linux/v4l2-controls.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-)drivers/media/v4l2-core/v4l2-ctrls-defs.c +8
-0@@ -831,6 +831,7 @@ const char *v4l2_ctrl_get_name(u32 id)case V4L2_CID_ALPHA_COMPONENT :return "Alpha Component" ;case V4L2_CID_COLORFX_CBCR :return "Color Effects, CbCr" ;case V4L2_CID_COLORFX_RGB :return "Color Effects, RGB" ; +case V4L2_CID_MEMORY_USAGE :return "Memory Usage" ;/* * Codec controls @@ -1476,6 +1477,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *min =0 ; *max =0xffff ;break ; +case V4L2_CID_MEMORY_USAGE : + *type =V4L2_CTRL_TYPE_INTEGER64 ; + *flags |=V4L2_CTRL_FLAG_READ_ONLY ; + *min =0 ; + *max =S64_MAX ; + *step =1 ; +break ;case V4L2_CID_FLASH_FAULT :case V4L2_CID_JPEG_ACTIVE_MARKER :case V4L2_CID_3A_LOCK :include/uapi/linux/v4l2-controls.h +3
-2@@ -110,8 +110,10 @@ enum v4l2_colorfx {#define V4L2_CID_COLORFX_CBCR (V4L2_CID_BASE+42)#define V4L2_CID_COLORFX_RGB (V4L2_CID_BASE+43) +#define V4L2_CID_MEMORY_USAGE (V4L2_CID_BASE+44) +/* last CID + 1 */ -#define V4L2_CID_LASTP1 (V4L2_CID_BASE+44) +#define V4L2_CID_LASTP1 (V4L2_CID_BASE+45)/* USER-class private control IDs */ --2.53.0
RFC PATCH 2/7 docs: media: v4l2-ctrls: Add V4L2_CID_MEMORY_USAGE control
In reply to [email protected]From: Ming Qian <[email protected]>
Add documentation for the V4L2_CID_MEMORY_USAGE control in the userspace API documentation. This read-only control allows applications to query the total amount of memory currently used by a device instance.
The documentation explains:
- The control reports memory usage in bytes for driver-managed allocations
- It includes internal buffers, intermediate processing data, and other driver allocations associated with the instance
- Applications can use it for debugging, resource monitoring, and making informed buffer allocation decisions
- The value may change dynamically during device operation
- It only reports driver-managed memory, not application-allocated buffers
Update V4L2_CID_LASTP1 definition to reflect the new control as the last predefined control ID.
--- .../userspace-api/media/v4l/control.rst | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
Documentation/userspace-api/media/v4l/control.rst +21 -1
@@ -316,9 +316,29 @@ Control IDs set the alpha component value of all pixels for further processing in the device. +``V4L2_CID_MEMORY_USAGE`` ``(integer64)`` + This is an optional read-only control that reports the total amount of + memory, in bytes, currently allocated and used by this device instance. + This includes memory used for internal buffers, intermediate processing + data, and any other driver-managed allocations associated with the + instance. + + Applications can use this control to monitor memory consumption for + debugging purposes, resource management, or to make informed decisions + about buffer allocation strategies. The value may change dynamically + as the device operates, for example when buffers are allocated or + released during streaming. + + Note that this control only reports memory managed by the driver for + this specific instance; it does not include memory allocated by the + application itself (e.g., userspace buffers passed to the driver). + + Not all drivers support this control. Applications should query for + its availability before use. + ``V4L2_CID_LASTP1`` End of the predefined control IDs (currently - ``V4L2_CID_ALPHA_COMPONENT`` + 1). + ``V4L2_CID_MEMORY_USAGE`` + 1). ``V4L2_CID_PRIVATE_BASE`` ID of the first custom (driver specific) control. Applications
Signature · 1 line
2.53.0
RFC PATCH 3/7 media: v4l2-memtrack: Add V4L2 memory tracking infrastructure
In reply to [email protected]From: Ming Qian <[email protected]>
Add a hierarchical memory tracking system for V4L2 devices that allows drivers to monitor and report memory usage. This infrastructure provides:
- Tree-structured memory tracking with parent-child relationships
- Per-node memory accounting with allocation counts
- Automatic aggregation of memory usage up the hierarchy
- debugfs interface for inspecting memory usage trees
- Notification callbacks for memory usage changes
The tracking system uses a context-based design where each root node creates its own tracking tree. Memory changes propagate notifications up the tree, allowing drivers to update controls or take other actions.
The debugfs interface creates entries under <debugfs>/v4l2-memtrack/ showing hierarchical memory usage with indentation, allocation counts, and process information.
Drivers can use this to track internal buffers, intermediate processing data, and other allocations, providing visibility into memory consumption for debugging and resource monitoring.
--- drivers/media/common/Kconfig | 1 + drivers/media/common/Makefile | 2 +- drivers/media/common/v4l2-memtrack/Kconfig | 19 + drivers/media/common/v4l2-memtrack/Makefile | 3 + .../common/v4l2-memtrack/v4l2-memtrack.c | 825 ++++++++++++++++++ include/media/v4l2-memtrack.h | 220 +++++ 6 files changed, 1069 insertions(+), 1 deletion(-) create mode 100644 drivers/media/common/v4l2-memtrack/Kconfig create mode 100644 drivers/media/common/v4l2-memtrack/Makefile create mode 100644 drivers/media/common/v4l2-memtrack/v4l2-memtrack.c create mode 100644 include/media/v4l2-memtrack.h
drivers/media/common/Kconfig +1 -0
@@ -27,5 +27,6 @@ config VIDEO_TVEEPROM source "drivers/media/common/b2c2/Kconfig" source "drivers/media/common/saa7146/Kconfig" source "drivers/media/common/siano/Kconfig" +source "drivers/media/common/v4l2-memtrack/Kconfig" source "drivers/media/common/v4l2-tpg/Kconfig" source "drivers/media/common/videobuf2/Kconfig"
drivers/media/common/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y += b2c2/ saa7146/ siano/ v4l2-tpg/ videobuf2/ +obj-y += b2c2/ saa7146/ siano/ v4l2-memtrack/ v4l2-tpg/ videobuf2/ # Please keep it alphabetically sorted by Kconfig name # (e. g. LC_ALL=C sort Makefile)
drivers/media/common/v4l2-memtrack/Kconfig new file +19 -0
@@ -0,0 +1,19 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config V4L2_MEMTRACK + tristate "V4L2 memory usage tracking support" + help + This option enables hierarchical memory allocation tracking + for V4L2 devices. It provides: + + - Tree-structured memory usage monitoring + - Per-device and per-context tracking + - debugfs interface at /sys/kernel/debug/v4l2-memtrack/ + - Optional V4L2 control for userspace queries + + This is useful for debugging memory leaks and monitoring + buffer allocation in video drivers. + + To compile this as a module, choose M here. + + If unsure, say N.
drivers/media/common/v4l2-memtrack/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_V4L2_MEMTRACK) += v4l2-memtrack.o
drivers/media/common/v4l2-memtrack/v4l2-memtrack.c new file +825 -0
825 lines not on this page · raw message
include/media/v4l2-memtrack.h new file +220 -0
220 lines not on this page · raw message
Signature · 1 line
2.53.0
RFC PATCH 4/7 docs: media: v4l2-memtrack: Add driver API documentation
In reply to [email protected]From: Ming Qian <[email protected]>
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.
--- 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
Documentation/driver-api/media/v4l2-core.rst +1 -0
@@ -28,3 +28,4 @@ Video4Linux devices v4l2-tveeprom v4l2-jpeg v4l2-isp + v4l2-memtrack
Documentation/driver-api/media/v4l2-memtrack.rst new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
2.53.0
RFC PATCH 5/7 MAINTAINERS: Add entry for V4L2 memory usage tracker
In reply to [email protected]From: Ming Qian <[email protected]>
Add a MAINTAINERS entry for the V4L2 memory tracking infrastructure, covering the driver API documentation, core implementation, and header files.
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -27667,6 +27667,14 @@ F: drivers/media/v4l2-core/v4l2-isp.c F: include/media/v4l2-isp.h F: include/uapi/linux/media/v4l2-isp.h +V4L2 MEMORY USAGE TRACKER +M: Ming Qian <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/driver-api/media/v4l2-memtrack.rst +F: drivers/media/common/v4l2-memtrack/v4l2-memtrack.c +F: include/media/v4l2-memtrack.h + VF610 NAND DRIVER M: Stefan Agner <[email protected]> L: [email protected]
Signature · 1 line
2.53.0
RFC PATCH 6/7 media: videobuf2: Add memory tracking support
In reply to [email protected]From: Ming Qian <[email protected]>
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.
--- drivers/media/common/videobuf2/videobuf2-core.c | 13 +++++++++++++ include/media/videobuf2-core.h | 4 ++++ 2 files changed, 17 insertions(+)
drivers/media/common/videobuf2/videobuf2-core.c +13 -0
@@ -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",
include/media/videobuf2-core.h +4 -0
@@ -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;
Signature · 1 line
2.53.0
RFC PATCH 7/7 media: amphion: Add V4L2 memory tracking support
In reply to [email protected]From: Ming Qian <[email protected]>
Integrate V4L2 memtrack framework to track DMA buffer allocations in the Amphion VPU driver. Memory usage is organized hierarchically (device -> instance -> queue) and exposed via V4L2_CID_MEMORY_USAGE control and debugfs.
Tracked buffers include firmware boot region, RPC, stream ring buffers, and codec-specific frame buffers (MBI, DCP, enc/ref frames).
--- drivers/media/platform/amphion/Kconfig | 1 + drivers/media/platform/amphion/vdec.c | 9 ++++++ drivers/media/platform/amphion/venc.c | 9 ++++++ drivers/media/platform/amphion/vpu.h | 7 +++++ drivers/media/platform/amphion/vpu_core.c | 6 ++++ drivers/media/platform/amphion/vpu_dbg.c | 5 ++++ drivers/media/platform/amphion/vpu_drv.c | 2 ++ drivers/media/platform/amphion/vpu_v4l2.c | 35 ++++++++++++++++++++++- 8 files changed, 73 insertions(+), 1 deletion(-)
drivers/media/platform/amphion/Kconfig +1 -0
@@ -12,6 +12,7 @@ config VIDEO_AMPHION_VPU select V4L2_MEM2MEM_DEV select VIDEOBUF2_DMA_CONTIG select VIDEOBUF2_VMALLOC + select V4L2_MEMTRACK help Amphion VPU Codec IP contains two parts: Windsor and Malone. Windsor is encoder that supports H.264, and Malone is decoder
drivers/media/platform/amphion/vdec.c +9 -0
@@ -279,6 +279,9 @@ static int vdec_ctrl_init(struct vpu_inst *inst)if (ctrl) ctrl->flags |=V4L2_CTRL_FLAG_VOLATILE ; +v4l2_ctrl_new_std (&inst->ctrl_handler,NULL , +V4L2_CID_MEMORY_USAGE ,0 ,S64_MAX ,1 ,0 ); +if (inst->ctrl_handler.error) { ret = inst->ctrl_handler.error;v4l2_ctrl_handler_free (&inst->ctrl_handler);
@@ -1069,6 +1072,8 @@ static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs)vpu_free_dma (buffer); buffer->length = fs->size; + buffer->memtrack = inst->memtrack; + buffer->label = fs->type ==MEM_RES_MBI ?"mbi" :"dcp" ;return vpu_alloc_dma (inst->core, buffer); }
@@ -1683,6 +1688,8 @@ static int vdec_start(struct vpu_inst *inst)vpu_trace (inst->dev,"[%d]\n" , inst->id);if (!vdec->udata.virt) { vdec->udata.length =0x1000 ; + vdec->udata.memtrack = inst->memtrack; + vdec->udata.label ="udata" ; ret =vpu_alloc_dma (inst->core, &vdec->udata);if (ret) {dev_err (inst->dev,"[%d] alloc udata fail\n" , inst->id);
@@ -1694,6 +1701,8 @@ static int vdec_start(struct vpu_inst *inst) stream_buffer_size =vpu_iface_get_stream_buffer_size (inst->core);if (stream_buffer_size >0 ) { inst->stream_buffer.length = stream_buffer_size; + inst->stream_buffer.memtrack = inst->memtrack; + inst->stream_buffer.label ="bitstream-ring-buffer" ; ret =vpu_alloc_dma (inst->core, &inst->stream_buffer);if (ret) {dev_err (inst->dev,"[%d] alloc stream buffer fail\n" , inst->id);
drivers/media/platform/amphion/venc.c +9 -0
@@ -678,6 +678,9 @@ static int venc_ctrl_init(struct vpu_inst *inst)v4l2_ctrl_new_std (&inst->ctrl_handler,NULL ,V4L2_CID_MPEG_VIDEO_AVERAGE_QP ,0 ,51 ,1 ,0 ); +v4l2_ctrl_new_std (&inst->ctrl_handler,NULL , +V4L2_CID_MEMORY_USAGE ,0 ,S64_MAX ,1 ,0 ); +if (inst->ctrl_handler.error) { ret = inst->ctrl_handler.error;v4l2_ctrl_handler_free (&inst->ctrl_handler);
@@ -929,6 +932,8 @@ static int venc_start_session(struct vpu_inst *inst, u32 type) stream_buffer_size =vpu_iface_get_stream_buffer_size (inst->core);if (stream_buffer_size >0 ) { inst->stream_buffer.length =max_t (u32, stream_buffer_size, venc->cpb_size *3 ); + inst->stream_buffer.memtrack = inst->memtrack; + inst->stream_buffer.label ="bitstream-ring-buffer" ; ret =vpu_alloc_dma (inst->core, &inst->stream_buffer);if (ret) goto error;
@@ -1027,6 +1032,8 @@ static void venc_request_mem_resource(struct vpu_inst *inst,for (i =0 ; i < enc_frame_num; i++) { venc->enc[i].length = enc_frame_size; + venc->enc[i].memtrack = inst->memtrack; + venc->enc[i].label ="enc-frame" ; ret =vpu_alloc_dma (inst->core, &venc->enc[i]);if (ret) {venc_cleanup_mem_resource (inst);
@@ -1035,6 +1042,8 @@ static void venc_request_mem_resource(struct vpu_inst *inst, }for (i =0 ; i < ref_frame_num; i++) { venc->ref[i].length = ref_frame_size; + venc->ref[i].memtrack = inst->memtrack; + venc->ref[i].label ="ref-frame" ; ret =vpu_alloc_dma (inst->core, &venc->ref[i]);if (ret) {venc_cleanup_mem_resource (inst);
drivers/media/platform/amphion/vpu.h +7 -0
@@ -9,6 +9,7 @@#include <media/v4l2-device.h> #include <media/v4l2-ctrls.h> #include <media/v4l2-mem2mem.h> +#include <media/v4l2-memtrack.h> #include <linux/mailbox_client.h> #include <linux/mailbox_controller.h> #include <linux/kfifo.h>
@@ -17,6 +18,7 @@#define VPU_TIMEOUT msecs_to_jiffies(1000)#define VPU_INST_NULL_ID (-1L)#define VPU_MSG_BUFFER_SIZE (8192) +#define VPU_NOTIFY_DELAY_MS (200)enum imx_plat_type {IMX8QXP =0 ,
@@ -47,6 +49,8 @@ struct vpu_buffer {u32 length;u32 bytesused;struct device *dev; +struct v4l2_memtrack_node *memtrack; +const char *label; };struct vpu_func {
@@ -81,6 +85,7 @@ struct vpu_dev {atomic_t ref_dec;struct dentry *debugfs; +struct v4l2_memtrack_node *memtrack; };struct vpu_format {
@@ -279,6 +284,8 @@ struct vpu_inst {pid_t tgid;struct dentry *debugfs; +struct v4l2_memtrack_node *memtrack; +void *priv; };
drivers/media/platform/amphion/vpu_core.c +6 -0
@@ -150,6 +150,8 @@ static int __vpu_alloc_dma(struct device *dev, struct vpu_buffer *buf)if (!buf->virt)return -ENOMEM ; +if (buf->memtrack) +v4l2_memtrack_add (buf->memtrack, buf->length, buf->label); buf->dev = dev;return 0 ;
@@ -160,6 +162,8 @@ void vpu_free_dma(struct vpu_buffer *buf)if (!buf->virt || !buf->dev)return ; +if (buf->memtrack) +v4l2_memtrack_sub (buf->memtrack, buf->length, buf->label);dma_free_coherent (buf->dev, buf->length, buf->virt, buf->phys); buf->virt =NULL ; buf->phys =0 ;
@@ -550,6 +554,7 @@ static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np) core->fw.phys = res.start; core->fw.length =resource_size (&res); +v4l2_memtrack_add (core->vpu->memtrack, core->fw.length,"fw" ); ret =of_reserved_mem_region_to_resource (np,1 , &res);if (ret) {
@@ -559,6 +564,7 @@ static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np) core->rpc.phys = res.start; core->rpc.length =resource_size (&res); +v4l2_memtrack_add (core->vpu->memtrack, core->rpc.length,"rpc" );if (core->rpc.length < core->res->rpc_size + core->res->fwlog_size) { dev_err(core->dev, "the rpc-region <%pad, 0x%x> is not enough\n",
drivers/media/platform/amphion/vpu_dbg.c +5 -0
@@ -212,6 +212,11 @@ static int vpu_dbg_instance(struct seq_file *s, void *data)if (seq_write (s, str, num))return 0 ; + num =scnprintf (str,sizeof (str),"memory usage = %ld\n" , +v4l2_memtrack_read (inst->memtrack)); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"flow :\n" );if (seq_write (s, str, num))return 0 ;
drivers/media/platform/amphion/vpu_drv.c +2 -0
@@ -138,6 +138,7 @@ static int vpu_probe(struct platform_device *pdev)if (ret) goto err_vpu_media; vpu->debugfs =debugfs_create_dir ("amphion_vpu" ,NULL ); + vpu->memtrack =v4l2_memtrack_create_root ("amphion-vpu" );of_platform_populate (dev->of_node,NULL ,NULL , dev);
@@ -162,6 +163,7 @@ static void vpu_remove(struct platform_device *pdev)struct vpu_dev *vpu =platform_get_drvdata (pdev);struct device *dev = &pdev->dev; +v4l2_memtrack_destroy_node (vpu->memtrack);debugfs_remove_recursive (vpu->debugfs); vpu->debugfs =NULL ;
drivers/media/platform/amphion/vpu_v4l2.c +34 -1
@@ -651,6 +651,14 @@ static const struct vb2_ops vpu_vb2_ops = { .buf_queue = vpu_vb2_buf_queue, }; +static void vpu_memtrack_ctrl_notify (struct v4l2_memtrack_node *node,size_t total,void *priv) +{ +struct v4l2_ctrl *ctrl = priv; + +if (ctrl) +v4l2_ctrl_s_ctrl_int64 (ctrl, total); +} +static int vpu_m2m_queue_init (void *priv,struct vb2_queue *src_vq,struct vb2_queue *dst_vq) {struct vpu_inst *inst = priv;
@@ -668,9 +676,13 @@ static int vpu_m2m_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_q src_vq->buf_struct_size =sizeof (struct vpu_vb2_buffer ); src_vq->dev = inst->vpu->dev; src_vq->lock = &inst->lock; +if (inst->memtrack) + src_vq->memtrack =v4l2_memtrack_create_node (inst->memtrack,"output" ); ret =vb2_queue_init (src_vq); -if (ret) +if (ret) { +v4l2_memtrack_destroy_node (src_vq->memtrack);return ret; + } dst_vq->type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ; inst->cap_format.type = dst_vq->type;
@@ -684,8 +696,12 @@ static int vpu_m2m_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_q dst_vq->buf_struct_size =sizeof (struct vpu_vb2_buffer ); dst_vq->dev = inst->vpu->dev; dst_vq->lock = &inst->lock; +if (inst->memtrack) + dst_vq->memtrack =v4l2_memtrack_create_node (inst->memtrack,"capture" ); ret =vb2_queue_init (dst_vq);if (ret) { +v4l2_memtrack_destroy_node (src_vq->memtrack); +v4l2_memtrack_destroy_node (dst_vq->memtrack);vb2_queue_release (src_vq);return ret; }
@@ -706,6 +722,12 @@ static int vpu_v4l2_release(struct vpu_inst *inst)vpu_release_core (inst->core);put_device (inst->dev); +if (inst->memtrack) { +v4l2_memtrack_unregister_notify (inst->memtrack); +v4l2_memtrack_destroy_node (inst->memtrack); + inst->memtrack =NULL ; + } +v4l2_ctrl_handler_free (&inst->ctrl_handler);mutex_destroy (&inst->lock);
@@ -745,6 +767,8 @@ int vpu_v4l2_open(struct file *file, struct vpu_inst *inst) inst->min_buffer_out =2 ;v4l2_fh_init (&inst->fh, func->vfd);v4l2_fh_add (&inst->fh, file); +if (vpu->memtrack) + inst->memtrack =v4l2_memtrack_create_node (vpu->memtrack,"instance" ); ret =call_vop (inst, ctrl_init);if (ret)
@@ -757,6 +781,14 @@ int vpu_v4l2_open(struct file *file, struct vpu_inst *inst) goto error; } +if (inst->memtrack) { +v4l2_memtrack_set_notify_delay (inst->memtrack,VPU_NOTIFY_DELAY_MS ); +v4l2_memtrack_register_notify (inst->memtrack, + vpu_memtrack_ctrl_notify, +v4l2_ctrl_find (&inst->ctrl_handler, +V4L2_CID_MEMORY_USAGE )); + } + inst->fh.ctrl_handler = &inst->ctrl_handler; inst->state =VPU_CODEC_STATE_DEINIT ; inst->workqueue =alloc_ordered_workqueue ("vpu_inst" ,WQ_MEM_RECLAIM );
@@ -775,6 +807,7 @@ int vpu_v4l2_open(struct file *file, struct vpu_inst *inst)return 0 ; error: +v4l2_memtrack_destroy_node (inst->memtrack);v4l2_fh_del (&inst->fh, file);v4l2_fh_exit (&inst->fh);vpu_inst_put (inst);
Signature · 1 line
2.53.0