DRM standardized memory stats
Subsystems (3)
cover DRM standardized memory stats
Floating a simple idea born from an user interest to be able to correctly size the swap space during OS installation, based on the size of the VRAM a discrete GPU might have. And to be able to do it in a vendor agnostic way.
Idea is to expose a standardized scheme in sysfs, under the DRM class card, and under a new 'memstat' directory. Such as, example from amdgpu:
/sys/class/drm/card1/memstat/
├── gtt
│ ├── total_mb
│ └── used_mb
└── vram
├── total_mb
└── used_mbOr with concrete numbers:
$ grep -Hr . /sys/class/drm/card1/memstat/ /sys/class/drm/card1/memstat/vram/total_mb:1024 /sys/class/drm/card1/memstat/vram/used_mb:445 /sys/class/drm/card1/memstat/gtt/total_mb:7394 /sys/class/drm/card1/memstat/gtt/used_mb:71
Drivers need to implement a simple DRM driver level callback which needs to report a stable list of interesting memory regions and their respective stats. The region names then become sub-directory names under the new 'memstat' directory, with each region exposing the total size and the current usage.
Similar data can already be queried if the dmem cgroup controller is enabled, also only for the participating drivers, by querying the root cgroup. But perhaps sysfs is easier, or perhaps it is too much code for too little benefit. I am curious to hear any opinions.
Tvrtko Ursulin (2):
drm: Allow drivers to report standardized memory stats drm/amdgpu: Wire up DRM memory stats reporting
drivers/gpu/drm/amd/amdgpu/amdgpu.h | 6 + drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 40 +++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 37 +++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 3 + drivers/gpu/drm/drm_drv.c | 10 ++ drivers/gpu/drm/drm_sysfs.c | 123 +++++++++++++++++++++ include/drm/drm_device.h | 19 ++++ include/drm/drm_drv.h | 8 ++ include/drm/drm_file.h | 9 ++ include/drm/drm_sysfs.h | 4 + 11 files changed, 248 insertions(+), 13 deletions(-)
Signature · 1 line
2.52.0
RFC 1/2 drm: Allow drivers to report standardized memory stats
Add a DRM driver level vfunc which allows drivers to opt-in into reporting standardized memory stats via sysfs.
Via the vfunc drivers can report a list of memory regions in a stable order, with the names following the memory region names used for fdinfo memory stats. The region names become directory names under the parent 'memstat' directory.
Each region reports the total size of the region and its current usage, represented respectively in the total_mb and used_mb files present under the aforementioned memory region named directories.
The card level 'memstat' directory is only created for drivers who report at least one memory region.
Amdgpu example:
/sys/class/drm/card1/memstat/
├── gtt
│ ├── total_mb
│ └── used_mb
└── vram
├── total_mb
└── used_mbOr with concrete numbers:
$ grep -Hr . /sys/class/drm/card1/memstat/ /sys/class/drm/card1/memstat/vram/total_mb:1024 /sys/class/drm/card1/memstat/vram/used_mb:445 /sys/class/drm/card1/memstat/gtt/total_mb:7394 /sys/class/drm/card1/memstat/gtt/used_mb:71
--- drivers/gpu/drm/drm_drv.c | 10 +++ drivers/gpu/drm/drm_sysfs.c | 123 ++++++++++++++++++++++++++++++++++++ include/drm/drm_device.h | 19 ++++++ include/drm/drm_drv.h | 8 +++ include/drm/drm_file.h | 9 +++ include/drm/drm_sysfs.h | 4 ++ 6 files changed, 173 insertions(+)
drivers/gpu/drm/drm_drv.c +10 -0
@@ -54,6 +54,7 @@#include <drm/drm_print.h> #include <drm/drm_privacy_screen_machine.h> #include <drm/drm_ras_genl_family.h> +#include <drm/drm_sysfs.h> #include "drm_crtc_internal.h" #include "drm_internal.h"
@@ -196,10 +197,15 @@ static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type)if (ret) goto err_debugfs; + ret =drm_sysfs_register_memstat (minor); +if (ret) + goto err_kdev; +/* replace NULL with @minor so lookups will succeed from now on */ entry =xa_store (drm_minor_get_xa (type), minor->index, minor,GFP_KERNEL );if (xa_is_err (entry)) { ret =xa_err (entry); +drm_sysfs_unregister_memstat (minor); goto err_debugfs; }WARN_ON (entry);
@@ -207,6 +213,9 @@ static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type)DRM_DEBUG ("new minor registered %d\n" , minor->index);return 0 ; +err_kdev: +device_del (minor->kdev); + err_debugfs:drm_debugfs_unregister (minor);return ret;
@@ -223,6 +232,7 @@ static void drm_minor_unregister(struct drm_device *dev, enum drm_minor_type typ/* replace @minor with NULL so lookups will fail from now on */ xa_store (drm_minor_get_xa (type), minor->index,NULL ,GFP_KERNEL ); +drm_sysfs_unregister_memstat (minor);device_del (minor->kdev);dev_set_drvdata (minor->kdev,NULL );/* safety belt */ drm_debugfs_unregister (minor);
drivers/gpu/drm/drm_sysfs.c +123 -0
132 lines not on this page · raw message
include/drm/drm_device.h +19 -0
@@ -66,6 +66,25 @@ enum switch_power_state { DRM_SWITCH_POWER_DYNAMIC_OFF = 3, }; +/** +* struct drm_memory_region_info - Memory name and stats for a region +* @name: Name of the memory region +* @total_mb: Total available memory in mebibytes +* @used_mb: Used memory in mebibytes +*/ +struct drm_memory_region_info { +char name[24 ]; + +u32 total_mb; +u32 used_mb; +}; + +struct drm_memory_info { +unsigned int num_regions; + +struct drm_memory_region_info region[]; +}; +/** * struct drm_device - DRM device structure *
include/drm/drm_drv.h +8 -0
@@ -44,6 +44,7 @@ struct drm_minor;struct dma_buf ;struct dma_buf_attachment ;struct drm_display_mode ; +struct drm_device_memory_info ;struct drm_mode_create_dumb ;struct drm_printer ;struct sg_table ;
@@ -392,6 +393,13 @@ struct drm_driver {*/ void (*show_fdinfo)(struct drm_printer *p,struct drm_file *f); +/** +* @get_memory_info: +* +* Get device specific memory info. See Documentation/gpu/drm-memory-info.rst. +*/ +const struct drm_memory_info * (*get_memory_info)(struct drm_device *dev); +/** @major: driver major number */ int major;/** @minor: driver minor number */
include/drm/drm_file.h +9 -0
@@ -66,6 +66,12 @@ enum drm_minor_type { DRM_MINOR_ACCEL = 32, }; +struct drm_memstat_attr { +struct kobject kobj; +unsigned int region; +struct drm_device *drm; +}; +/** * struct drm_minor - DRM device minor structure *
@@ -82,6 +88,9 @@ struct drm_minor {struct device *kdev;/* Linux device */ struct drm_device *dev; +struct kobject *sysfs_memstat; +struct drm_memstat_attr *sysfs_memstat_regions; +struct dentry *debugfs_symlink;struct dentry *debugfs_root; };
include/drm/drm_sysfs.h +4 -0
@@ -5,6 +5,7 @@struct drm_device ;struct device ;struct drm_connector ; +struct drm_minor ;struct drm_property ;int drm_class_device_register (struct device *dev);
@@ -14,4 +15,7 @@ void drm_sysfs_hotplug_event(struct drm_device *dev);void drm_sysfs_connector_hotplug_event (struct drm_connector *connector);void drm_sysfs_connector_property_event (struct drm_connector *connector,struct drm_property *property); + +int drm_sysfs_register_memstat (struct drm_minor *minor); +void drm_sysfs_unregister_memstat (struct drm_minor *minor);#endif
Signature · 1 line
2.52.0
RFC 2/2 drm/amdgpu: Wire up DRM memory stats reporting
In reply to Tvrtko UrsulinWire up the driver agnostic DRM memory reporting for the VRAM and GTT memory regions.
As the list of regions and their stats needs to be returned to the DRM core for inspection and remain valid after the callback had existed, the persistent storage is kept at the device level and updated on each query.
The current list of memory region names as reported for fdinfo memory stats is exported as a helper in order to achieve standardized names, as required by the DRM contract.
--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 6 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 40 +++++++++++++++------- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 37 ++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 3 ++ 5 files changed, 75 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +6 -0
@@ -1195,6 +1195,9 @@ struct amdgpu_device {struct amdgpu_uma_carveout_info uma_info; +struct drm_memory_info memory_info; +struct drm_memory_region_info __memory_region_info[2 ];/* Storage for memory_info */ +/* KFD * Must be last --ends in a flexible-array member. */
@@ -1665,4 +1668,7 @@ void amdgpu_device_set_uid(struct amdgpu_uid *uid_info, uint64_t uid);uint64_t amdgpu_device_get_uid (struct amdgpu_uid *uid_info,enum amdgpu_uid_type type,uint8_t inst); + +const struct drm_memory_info *amdgpu_drm_memory_info (struct drm_device *drm); +#endif
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -1
@@ -3085,7 +3085,7 @@ static const struct drm_driver amdgpu_kms_driver = {#ifdef CONFIG_PROC_FS .show_fdinfo = amdgpu_show_fdinfo, #endif - + .get_memory_info = amdgpu_drm_memory_info, .gem_prime_import = amdgpu_gem_prime_import, .name = DRIVER_NAME,
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +28 -12
@@ -55,14 +55,9 @@ static const char *amdgpu_ip_name[AMDGPU_HW_IP_NUM] = { [AMDGPU_HW_IP_VPE] = "vpe", }; -void amdgpu_show_fdinfo (struct drm_printer *p,struct drm_file *file) +const char *amdgpu_ttm_pl_to_name (unsigned int placement) { -struct amdgpu_fpriv *fpriv = file->driver_priv; -struct amdgpu_vm *vm = &fpriv->vm; - -struct amdgpu_mem_stats stats[__AMDGPU_PL_NUM]; -ktime_t usage[AMDGPU_HW_IP_NUM ]; -const char *pl_name[] = { +static const char *names[] = { [TTM_PL_VRAM ] ="vram" , [TTM_PL_TT ] ="gtt" , [TTM_PL_SYSTEM ] ="cpu" ,
@@ -72,6 +67,30 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file) [AMDGPU_PL_DOORBELL] = "doorbell", [AMDGPU_PL_MMIO_REMAP] = "mmioremap", }; + +if (WARN_ON_ONCE (placement >=ARRAY_SIZE (names))) +return "unknown" ; + +return names[placement]; +} + +void amdgpu_show_fdinfo (struct drm_printer *p,struct drm_file *file) +{ +struct amdgpu_fpriv *fpriv = file->driver_priv; +struct amdgpu_vm *vm = &fpriv->vm; + +struct amdgpu_mem_stats stats[__AMDGPU_PL_NUM]; +ktime_t usage[AMDGPU_HW_IP_NUM ]; +static const unsigned int regions[] = { +TTM_PL_VRAM , +TTM_PL_TT , +TTM_PL_SYSTEM , +AMDGPU_PL_GDS , +AMDGPU_PL_GWS , +AMDGPU_PL_OA , +AMDGPU_PL_DOORBELL , +AMDGPU_PL_MMIO_REMAP , + };unsigned int hw_ip, i;amdgpu_vm_get_memory (vm, stats);
@@ -85,15 +104,12 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)drm_printf (p,"pasid:\t%u\n" , fpriv->vm.pasid); -for (i =0 ; i <ARRAY_SIZE (pl_name); i++) { -if (!pl_name[i]) -continue ; - +for (i =0 ; i <ARRAY_SIZE (regions); i++) {drm_print_memory_stats (p, &stats[i].drm,DRM_GEM_OBJECT_RESIDENT |DRM_GEM_OBJECT_PURGEABLE , - pl_name[i]); +amdgpu_ttm_pl_to_name (regions[i])); }/* Legacy amdgpu keys, alias to drm-resident-memory-: */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +37 -0
@@ -2917,3 +2917,40 @@ void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)#endif } + +const struct drm_memory_info *amdgpu_drm_memory_info (struct drm_device *drm) +{ +struct amdgpu_device *adev =drm_to_adev (drm); +struct drm_memory_info *info = &adev->memory_info; +static const unsigned int regions[] = { +TTM_PL_VRAM , +TTM_PL_TT , + }; +struct drm_memory_region_info *region; +struct ttm_resource_manager *man; +unsigned int i; + +if (WARN_ON_ONCE (ARRAY_SIZE (adev->__memory_region_info) != +ARRAY_SIZE (regions))) +return NULL ; + +if (!info->num_regions) { +for (i =0 ; i <ARRAY_SIZE (regions); i++) { + region = &info->region[i]; +strscpy (region->name, +amdgpu_ttm_pl_to_name (regions[i]), +sizeof (region->name)); + man =ttm_manager_type (&adev->mman.bdev, regions[i]); + region->total_mb = man->size >>20 ; + info->num_regions++; + } + } + +for (i =0 ; i <ARRAY_SIZE (regions); i++) { + region = &info->region[i]; + man =ttm_manager_type (&adev->mman.bdev, regions[i]); + region->used_mb =ttm_resource_manager_usage (man) >>20 ; + } + +return &adev->memory_info; +}
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -0
@@ -269,4 +269,7 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,enum dma_data_direction dir, struct sg_table *sgt); +const char *amdgpu_ttm_pl_to_name (unsigned int placement); +const struct drm_memory_info *amdgpu_drm_memory_info (struct drm_device *drm); +#endif
Signature · 1 line
2.52.0
kernel test robot
build botRe: [RFC 2/2] drm/amdgpu: Wire up DRM memory stats reporting
kernel test robot noticed the following build warnings:
Hi Tvrtko,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on drm-misc/drm-misc-next]
[also build test WARNING on daeinki-drm-exynos/exynos-drm-next drm/drm-next drm-i915/for-linux-next drm-i915/for-linux-next-fixes drm-tip/drm-tip next-20260430] [cannot apply to linus/master v6.16-rc1]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Tvrtko-Ursulin/drm-Allow-drivers-to-report-standardized-memory-stats/20260430-153342 base: https://gitlab.freedesktop.org/drm/misc/kernel.git drm-misc-next patch link: https://lore.kernel.org/r/20260429130653.73854-3-tvrtko.ursulin%40igalia.com patch subject: [RFC 2/2] drm/amdgpu: Wire up DRM memory stats reporting compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) docutils: docutils (Docutils 0.21.2, Python 3.13.5, on linux) reproduce: (https://download.01.org/0day-ci/archive/20260501/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
Warning: Documentation/translations/zh_CN/scsi/scsi_mid_low_api.rst references a file that doesn't exist: Documentation/Configure.help
Warning: MAINTAINERS references a file that doesn't exist: Documentation/ABI/testing/sysfs-platform-ayaneo
Warning: MAINTAINERS references a file that doesn't exist: Documentation/devicetree/bindings/display/bridge/megachips-stdpxxxx-ge-b850v3-fw.txt
Warning: arch/powerpc/sysdev/mpic.c references a file that doesn't exist: Documentation/devicetree/bindings/powerpc/fsl/mpic.txt
Warning: drivers/net/ethernet/smsc/Kconfig references a file that doesn't exist: file:Documentation/networking/device_drivers/ethernet/smsc/smc9.rst
>> Warning: include/drm/drm_drv.h references a file that doesn't exist: Documentation/gpu/drm-memory-info.rst
Warning: rust/kernel/sync/atomic/ordering.rs references a file that doesn't exist: srctree/tools/memory-model/Documentation/explanation.txt
Warning: tools/docs/documentation-file-ref-check references a file that doesn't exist: Documentation/virtual/lguest/lguest.c
Warning: tools/docs/documentation-file-ref-check references a file that doesn't exist: m,\b(\S*)(Documentation/[A-Za-z0-9
Warning: tools/docs/documentation-file-ref-check references a file that doesn't exist: Documentation/devicetree/dt-object-internal.txt
Warning: tools/docs/documentation-file-ref-check references a file that doesn't exist: m,^Documentation/scheduler/sched-pelt
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki