Files
metal2vulkan/scripts/dma-buf-leak/kernel-holds-gpuvm.bt
T
Anees Iqbal 849f7250d9 kernel-holds-gpuvm.bt: probe only the DRM core, key probe counts by probe name, read arg0 before the loop
Three fixes the Asahi run needed, folded into the committed copy:

- The asahi driver's Rust functions (its GEM close callback and its gpuvm
  step_map/step_unmap callbacks) are not in available_filter_functions, so
  their kprobes failed to attach. They are gone, with their three table
  columns; the kernel stacks of the unlinks and puts show those callbacks.
- @n[func] keyed the map by a ksym in kprobe context while the other keys
  are strings; it is @n[probe] now.
- drm_gpuvm_fini read arg0 inside the for-loop over @vbvm, which bpftrace
  rejects; it copies arg0 to $vm first.

Verified: the Asahi session ran the same three fixes as a local copy
(round 3, 19 probes attached, tables and stacks printed). This file is that
copy with the dropped columns removed instead of held at zero; this exact
file has not run yet.
2026-11-11 07:58:49 -08:00

270 lines
7.6 KiB
Plaintext

/*
* What holds the GEM objects of kept dma-bufs: their GPU VM bookkeeping (drm_gpuvm), per object.
*
* Follows each GEM object the DRM core imports from a dma-buf named "m2vleak*" and counts, per
* object: its import order among its dma-buf's imports (1st, 2nd, ...), its vm_bos created
* (drm_gpuvm_bo_create), its VAs linked and unlinked (drm_gpuva_link, drm_gpuva_unlink,
* drm_gpuva_unlink_defer), its vm_bo puts (drm_gpuvm_bo_put, drm_gpuvm_bo_put_deferred), the
* vm_bos moved to a VM's deferred list (drm_gpuvm_bo_into_zombie, drm_gpuvm_bo_defer_free) and
* destroyed (drm_gpuvm_bo_destroy, drm_gpuvm_bo_destroy_not_in_lists), and the object's reference
* count when its last handle closed. It counts the VMs' deferred cleanups
* (drm_gpuvm_bo_deferred_cleanup) with the kernel stacks that ran them, and at each VM's teardown
* (drm_gpuvm_fini), the probe's vm_bos still alive in it.
*
* Only the DRM core's C functions are probed. The asahi driver's Rust functions (its GEM close
* callback, its gpuvm step callbacks) are not in available_filter_functions, so they cannot be
* kprobed; they show up in the kernel stacks instead.
*
* On exit (SIGINT) it prints one table for the objects released (drm_gem_object_release) and
* one for those still alive, keyed by:
* (import order, vm_bos created, VAs linked, unlinked, unlinked deferred, vm_bo puts,
* deferred puts, into the deferred list, vm_bos destroyed, references at the last handle close)
* and the kernel stacks of the deferred puts, of the deferred cleanups, and of the VA unlinks.
*
* Probes the kernel lacks are skipped with a warning. Run it as root across the run and stop it
* with SIGINT a few seconds after the process exits: sudo bpftrace kernel-holds-gpuvm.bt
*/
config = {
missing_probes = "warn"
}
BEGIN
{
printf("kernel-holds-gpuvm: following the GEM objects of dma-bufs named m2vleak*\n");
}
kretprobe:drm_gem_prime_import_dev
{
if (retval != 0 && retval < 0xfffffffffffff000) {
$o = (struct drm_gem_object *)retval;
if ($o->import_attach != 0) {
$d = $o->import_attach->dmabuf;
if ($d->name != 0 && strncmp(str($d->name), "m2vleak", 7) == 0) {
@nimp[(uint64)$d] = @nimp[(uint64)$d] + 1;
@ord[retval] = @nimp[(uint64)$d];
@objd[retval] = (uint64)$d;
@n["import"] = count();
}
}
}
}
/* Whether a dma-buf's imports are in one DRM file (its PRIME cache) or in several. */
kprobe:drm_prime_add_buf_handle
{
if (@nimp[arg1]) {
if (@file[arg1] == 0) {
@file[arg1] = arg0;
} else if (@file[arg1] == arg0) {
@n["import again in the same DRM file"] = count();
} else {
@n["import again in another DRM file"] = count();
}
}
}
kprobe:drm_gem_object_release_handle
{
if (@objd[arg1]) {
$obj = (struct drm_gem_object *)arg1;
@refs_close[arg1] = $obj->refcount.refcount.refs.counter;
@n["handle release"] = count();
}
}
kretprobe:drm_gpuvm_bo_create
{
if (retval != 0 && retval < 0xfffffffffffff000) {
$vb = (struct drm_gpuvm_bo *)retval;
$o = (uint64)$vb->obj;
if (@objd[$o]) {
@vbobj[retval] = $o;
@vbvm[retval] = (uint64)$vb->vm;
@vbcreate[$o] = @vbcreate[$o] + 1;
@n["vm_bo create"] = count();
}
}
}
kprobe:drm_gpuva_link
{
$va = (struct drm_gpuva *)arg0;
$o = (uint64)$va->gem.obj;
if (@objd[$o]) {
@link[$o] = @link[$o] + 1;
@n["va link"] = count();
}
}
kprobe:drm_gpuva_unlink
{
$va = (struct drm_gpuva *)arg0;
$o = (uint64)$va->gem.obj;
if (@objd[$o]) {
@unlink[$o] = @unlink[$o] + 1;
@unlink_stack[kstack(8)] = count();
@n["va unlink"] = count();
}
}
kprobe:drm_gpuva_unlink_defer
{
$va = (struct drm_gpuva *)arg0;
$o = (uint64)$va->gem.obj;
if (@objd[$o]) {
@unlinkdef[$o] = @unlinkdef[$o] + 1;
@unlink_stack[kstack(8)] = count();
@n["va unlink deferred"] = count();
}
}
kprobe:drm_gpuvm_bo_put
{
$o = @vbobj[arg0];
if ($o) {
@vbput[$o] = @vbput[$o] + 1;
@n["vm_bo put"] = count();
}
}
kprobe:drm_gpuvm_bo_put_deferred
{
$o = @vbobj[arg0];
if ($o) {
@vbputdef[$o] = @vbputdef[$o] + 1;
@putdef_stack[kstack(8)] = count();
@n["vm_bo put deferred"] = count();
}
}
/* Their argument is the vm_bo's kref, or the vm_bo: both are tried. */
kprobe:drm_gpuvm_bo_into_zombie,
kprobe:drm_gpuvm_bo_defer_free
{
$o = @vbobj[arg0];
if (!$o) {
$o = @vbobj[arg0 - offsetof(struct drm_gpuvm_bo, kref)];
}
if ($o) {
@zombie[$o] = @zombie[$o] + 1;
@n[probe] = count();
}
}
kprobe:drm_gpuvm_bo_destroy
{
$vb = arg0 - offsetof(struct drm_gpuvm_bo, kref);
if (!@vbobj[$vb]) {
$vb = arg0;
}
$o = @vbobj[$vb];
if ($o) {
@vbdestroy[$o] = @vbdestroy[$o] + 1;
delete(@vbobj[$vb]);
delete(@vbvm[$vb]);
@n[probe] = count();
}
}
kprobe:drm_gpuvm_bo_destroy_not_in_lists
{
$o = @vbobj[arg0];
if ($o) {
@vbdestroy[$o] = @vbdestroy[$o] + 1;
delete(@vbobj[arg0]);
delete(@vbvm[arg0]);
@n[probe] = count();
}
}
kprobe:drm_gpuvm_bo_deferred_cleanup
{
@n[probe] = count();
@cleanup_stack[kstack(10)] = count();
}
kprobe:drm_gpuvm_fini
{
@n[probe] = count();
$vm = arg0;
for ($kv : @vbvm) {
if ($kv.1 == $vm) {
@vm_bos_alive_at_vm_fini = count();
}
}
}
kprobe:drm_gem_object_release
{
if (@objd[arg0]) {
$o = arg0;
@released[@ord[$o], @vbcreate[$o], @link[$o], @unlink[$o], @unlinkdef[$o], @vbput[$o],
@vbputdef[$o], @zombie[$o], @vbdestroy[$o], @refs_close[$o]] = count();
@n["gem release"] = count();
delete(@objd[$o]);
delete(@ord[$o]);
delete(@vbcreate[$o]);
delete(@link[$o]);
delete(@unlink[$o]);
delete(@unlinkdef[$o]);
delete(@vbput[$o]);
delete(@vbputdef[$o]);
delete(@zombie[$o]);
delete(@vbdestroy[$o]);
delete(@refs_close[$o]);
}
}
kprobe:release_udmabuf
{
delete(@nimp[arg0]);
delete(@file[arg0]);
}
END
{
for ($kv : @objd) {
$o = $kv.0;
@alive[@ord[$o], @vbcreate[$o], @link[$o], @unlink[$o], @unlinkdef[$o], @vbput[$o],
@vbputdef[$o], @zombie[$o], @vbdestroy[$o], @refs_close[$o]] = count();
}
printf("\nkey: (import order, vm_bos created, VAs linked, unlinked, unlinked deferred, vm_bo puts, deferred puts, into the deferred list, vm_bos destroyed, refs at last handle close)\n");
printf("\nGEM objects released:\n");
print(@released);
printf("\nGEM objects still alive at exit:\n");
print(@alive);
printf("\nvm_bos of the probe's objects alive at a VM's teardown:\n");
print(@vm_bos_alive_at_vm_fini);
printf("\ncalls seen:\n");
print(@n);
printf("\nstacks of the deferred vm_bo puts:\n");
print(@putdef_stack);
printf("\nstacks of the deferred cleanups:\n");
print(@cleanup_stack);
printf("\nstacks of the VA unlinks:\n");
print(@unlink_stack);
clear(@objd);
clear(@ord);
clear(@nimp);
clear(@file);
clear(@vbobj);
clear(@vbvm);
clear(@vbcreate);
clear(@link);
clear(@unlink);
clear(@unlinkdef);
clear(@vbput);
clear(@vbputdef);
clear(@zombie);
clear(@vbdestroy);
clear(@refs_close);
clear(@released);
clear(@alive);
clear(@vm_bos_alive_at_vm_fini);
clear(@n);
clear(@putdef_stack);
clear(@cleanup_stack);
clear(@unlink_stack);
}