Skip to contentSkip to conversation

drm/amdgpu: fix the memleak caused by fence not released

PATCH Superseded Le Ma 2 participants 2 messages Feb 12, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Le Ma

author
#0 lore raw

PATCH 1/1 drm/amdgpu: fix the memleak caused by fence not released

On systems with CONFIG_SLUB_DEBUG enabled, the memleak like below will show up explicitly during driver unloading if created bo without drm_timeline object before.

    BUG drm_sched_fence (Tainted: G           OE     ): Objects remaining in drm_sched_fence on __kmem_cache_shutdown()
    -----------------------------------------------------------------------------
    Call Trace:
    <TASK>
    dump_stack_lvl+0x4c/0x70
    dump_stack+0x14/0x20
    slab_err+0xb0/0xf0
    ? srso_alias_return_thunk+0x5/0xfbef5
    ? flush_work+0x12/0x20
    ? srso_alias_return_thunk+0x5/0xfbef5
    __kmem_cache_shutdown+0x163/0x2e0
    kmem_cache_destroy+0x61/0x170
    drm_sched_fence_slab_fini+0x19/0x900

Thus call dma_fence_put properly to avoid the memleak.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4 -1
index 8b67aae6c2fe..ad3c5fdf6970 100644
@@ -125,8 +125,11 @@ amdgpu_gem_update_bo_mapping(struct drm_file *filp,
 	struct amdgpu_vm *vm = &fpriv->vm;
 	struct dma_fence *last_update;
 
-	if (!syncobj)
+	if (!syncobj) {
+		dma_fence_wait(fence, false);
+		dma_fence_put(fence);
 		return;
+	}
 
 	/* Find the last update fence */
 	switch (operation) {
Signature · 1 line
2.43.2
maintainer
#1 lore raw

Am 12.02.25 um 12:30 schrieb Le Ma:

Quoted 18 lines

On systems with CONFIG_SLUB_DEBUG enabled, the memleak like below will show up explicitly during driver unloading if created bo without drm_timeline object before.

     BUG drm_sched_fence (Tainted: G           OE     ): Objects remaining in drm_sched_fence on __kmem_cache_shutdown()
     -----------------------------------------------------------------------------
     Call Trace:
     <TASK>
     dump_stack_lvl+0x4c/0x70
     dump_stack+0x14/0x20
     slab_err+0xb0/0xf0
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? flush_work+0x12/0x20
     ? srso_alias_return_thunk+0x5/0xfbef5
     __kmem_cache_shutdown+0x163/0x2e0
     kmem_cache_destroy+0x61/0x170
     drm_sched_fence_slab_fini+0x19/0x900
Quoted 1 line

Thus call dma_fence_put properly to avoid the memleak.

Good catch, but there is even more broken here.

The call to amdgpu_gem_update_timeline_node() comes way to late, we need to make sure that this is called *before* doing the update. Probably best to just inline the functionality.

Then AMDGPU_VM_DELAY_UPDATE and is mutual exclusive to specifying the syncobj when the update is completed.

Quoted 11 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
index 8b67aae6c2fe..ad3c5fdf6970 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_update_bo_mapping(struct drm_file *filp, · line 129

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
@@ -125,8 +125,11 @@ amdgpu_gem_update_bo_mapping(struct drm_file *filp,
  	struct amdgpu_vm *vm = &fpriv->vm;
  	struct dma_fence *last_update;
 
-	if (!syncobj)
+	if (!syncobj) {
+		dma_fence_wait(fence, false);

Please drop that wait, this is just nonsense.

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_update_bo_mapping(struct drm_file *filp, · line 132

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -0
+		dma_fence_put(fence);
  		return;
+	}

We might want to move all this into amdgpu_gem_va_update_vm().

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
  	/* Find the last update fence */
  	switch (operation) {