vramgaze_buddy.ko: a linux kernel module to restore kernel 5.x amdgpu_vram_mm meta-data on kernel 6.x

on blog at

Download: vramgaze_buddy.c, vramgaze-gtk3-kernelbuddy.pl.

I developed vramgaze (for amdgpu) on a kernel 5.x machine. 5.x kernels expose per-region VRAM allocation data because amdgpu_vram_mm used the old drm_mm allocator which dumped every individual used/free node. On kernel 6.1, the amdgpu driver switched to the buddy allocator which only reports summary statistics. This means vramgaze can't see the VRAM regions/blocks and it just sees one big block; no hints where interesting stuff might be or what PID owns that range. This made using vramgaze on my 6.x boxes less fun and less useful.

The 6.x amdgpu driver does not export this information in any form. The only possible solution was to re-implement the kernel 5.x feature using a custom kernel module written for 6.x kernels. After hard freezing and crashing my test 6.x box a dozen times during development I finally have a custom kernel module, vramgaze_buddy, which restores the lost functionality. When loaded and runnning it reconstructs the per-node allocation map of the AMD GPU VRAM buddy allocator (drm_buddy) and exposes it as a human-readable debugfs seq_file (/sys/kernel/debug/vramgaze_buddy_nodes). This replicates the flat per-block view that existed in 5.x's drm_mm interface.

vramgaze-gtk3-kernelbuddy.pl has been altered to use this kernel module's interface at /sys/kernel/debug/vramgaze_buddy_nodes to fill in the gaps if it's available. So now vramgaze *can* work the same way on linux 6.x machines. Yay. The new version also works fine without the kernel module loaded on 5.x systems. And it'll work without the kernel module on 6.x but not give as much information. This new version also includes a few other unrelated bug fixes for automatically finding the right DRI and new features like an explicit BO map.

Now I know no one is going to want to run a random *vibe coded* kernel module. Doing vibe coded stuff in userspace is bad enough. But I'm putting it up here for those unlikely adventurous souls with linux kernel 6.x and a desire to view their amdgpu's VRAM.

To compile it the 'Makefile' is simple (and also included as a comment in the vramgaze_buddy.c source code).

obj-m += vramgaze_buddy.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Once that text is in the 'Makefile' file in the dir with vramgaze_buddy.c you can build it and load it like,

$ make
$ sudo insmod vramgaze_buddy.ko

Kernel module output format (for manual reading):

$ sudo bash -c 'cat /sys/kernel/debug/vramgaze_buddy_nodes'
drm_buddy @ ffff9ac9a3fc5f58  size=2147483648  chunk=4096  n_roots=1
--- root[0] ---
node 0x000000000000: 1048576 bytes (256 pages), used
node 0x000000100000: 524288 bytes (128 pages), free
...

Each line after the header is one leaf node in the buddy tree:

[comment on this post] Append "/@say/your message here" to the URL in the location bar and hit enter.

[webmention/pingback] Did you respond to this post? What's the URL?