Skip to contentSkip to conversation

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

PATCH In review Le Ma 4 participants 6 messages Feb 14, 2025

Subsystems (2)
lore
Share & tools
lore

Le Ma

author
#0 lore raw

PATCH v2 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.

v2: call dma_fence_put in amdgpu_gem_va_update_vm

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +7 -2
index 8b67aae6c2fe..00f1f34705c0 100644
@@ -759,7 +759,8 @@ static struct dma_fence *
 amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 			struct amdgpu_vm *vm,
 			struct amdgpu_bo_va *bo_va,
-			uint32_t operation)
+			uint32_t operation,
+			uint32_t syncobj_handle)
 {
 	struct dma_fence *fence = dma_fence_get_stub();
 	int r;
@@ -771,6 +772,9 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 	if (r)
 		goto error;
 
+	if (!syncobj_handle)
+		dma_fence_put(fence);
+
 	if (operation == AMDGPU_VA_OP_MAP ||
 	    operation == AMDGPU_VA_OP_REPLACE) {
 		r = amdgpu_vm_bo_update(adev, bo_va, false);
@@ -965,7 +969,8 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 						    &timeline_chain);
 
 		fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
-						args->operation);
+						args->operation,
+						args->vm_timeline_syncobj_out);
 
 		if (!r)
 			amdgpu_gem_update_bo_mapping(filp, bo_va,
Signature · 1 line
2.43.2
maintainer
#1 lore raw

Adding Arvind, please make sure to keep him in the loop.

Am 14.02.25 um 11:07 schrieb Le Ma:

Quoted 42 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

Thus call dma_fence_put properly to avoid the memleak.

v2: call dma_fence_put in amdgpu_gem_va_update_vm

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
index 8b67aae6c2fe..00f1f34705c0 100644
@@ -759,7 +759,8 @@ static struct dma_fence *
 amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 			struct amdgpu_vm *vm,
 			struct amdgpu_bo_va *bo_va,
-			uint32_t operation)
+			uint32_t operation,
+			uint32_t syncobj_handle)
 {
 	struct dma_fence *fence = dma_fence_get_stub();
 	int r;

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_va_update_vm(struct amdgpu_device *adev, · line 777

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -0
@@ -771,6 +772,9 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 	if (r)
 		goto error;
 
+	if (!syncobj_handle)
+		dma_fence_put(fence);
+

Having that check inside amdgpu_gem_update_bo_mapping() was actually correct. Here it doesn't make much sense.

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 969

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
 	if (operation == AMDGPU_VA_OP_MAP ||
 	    operation == AMDGPU_VA_OP_REPLACE) {
 		r = amdgpu_vm_bo_update(adev, bo_va, false);
@@ -965,7 +969,8 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 						    &timeline_chain);

Right before this here is a call to amdgpu_gem_update_timeline_node() which is incorrectly placed.

That needs to come much earlier, above the switch (args->operation)....

Regards,
Christian.

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
 		fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
-						args->operation);
+						args->operation,
+						args->vm_timeline_syncobj_out);
 
 		if (!r)
 			amdgpu_gem_update_bo_mapping(filp, bo_va,
#2 lore raw
Quoted 48 lines · Christian König

Adding Arvind, please make sure to keep him in the loop.

Am 14.02.25 um 11:07 schrieb Le Ma:

Quoted 45 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

Thus call dma_fence_put properly to avoid the memleak.

v2: call dma_fence_put in amdgpu_gem_va_update_vm

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
index 8b67aae6c2fe..00f1f34705c0 100644
@@ -759,7 +759,8 @@ static struct dma_fence *
  amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
  			struct amdgpu_vm *vm,
  			struct amdgpu_bo_va *bo_va,
-			uint32_t operation)
+			uint32_t operation,
+			uint32_t syncobj_handle)
  {
  	struct dma_fence *fence = dma_fence_get_stub();
  	int r;
@@ -771,6 +772,9 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
  	if (r)
  		goto error;
 
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_va_update_vm(struct amdgpu_device *adev, · line 777

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -0
+	if (!syncobj_handle)
+		dma_fence_put(fence);
+

Having that check inside amdgpu_gem_update_bo_mapping() was actually correct. Here it doesn't make much sense.

Agreed,

Regards,
~Arvind

Quoted 21 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 969

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
  	if (operation == AMDGPU_VA_OP_MAP ||
  	    operation == AMDGPU_VA_OP_REPLACE) {
  		r = amdgpu_vm_bo_update(adev, bo_va, false);
@@ -965,7 +969,8 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
  						    &timeline_chain);

Right before this here is a call to amdgpu_gem_update_timeline_node() which is incorrectly placed.

That needs to come much earlier, above the switch (args->operation)....

Regards,
Christian.

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
  		fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
-						args->operation);
+						args->operation,
+						args->vm_timeline_syncobj_out);
 
  		if (!r)
  			amdgpu_gem_update_bo_mapping(filp, bo_va,
#3 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Better to put the fence outside amdgpu_gem_va_update_vm. Since it is passed to the caller, and the caller must keep one reference at least until this fence is no longer needed.

Thanks
River

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Yadav, Arvind
Sent: Friday, February 14, 2025 7:42 PM
To: Koenig, Christian <[email protected]>; Ma, Le <[email protected]>; [email protected]; Yadav, Arvind <[email protected]>
Cc: Zhang, Hawking <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH v2] drm/amdgpu: fix the memleak caused by fence not released

Quoted 49 lines · Christian König

Adding Arvind, please make sure to keep him in the loop.

Am 14.02.25 um 11:07 schrieb Le Ma:

Quoted 46 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

Thus call dma_fence_put properly to avoid the memleak.

v2: call dma_fence_put in amdgpu_gem_va_update_vm

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
index 8b67aae6c2fe..00f1f34705c0 100644
@@ -759,7 +759,8 @@ static struct dma_fence *
  amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
                     struct amdgpu_vm *vm,
                     struct amdgpu_bo_va *bo_va,
-                    uint32_t operation)
+                    uint32_t operation,
+                    uint32_t syncobj_handle)
  {
     struct dma_fence *fence = dma_fence_get_stub();
     int r;
@@ -771,6 +772,9 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
     if (r)
             goto error;
 
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_va_update_vm(struct amdgpu_device *adev, · line 777

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -0
+    if (!syncobj_handle)
+            dma_fence_put(fence);
+

Having that check inside amdgpu_gem_update_bo_mapping() was actually correct. Here it doesn't make much sense.

Agreed,

Regards,
~Arvind

Quoted 21 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
     if (operation == AMDGPU_VA_OP_MAP ||
         operation == AMDGPU_VA_OP_REPLACE) {
             r = amdgpu_vm_bo_update(adev, bo_va, false); @@ -965,7 +969,8 @@

int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,

                                                 &timeline_chain);

Right before this here is a call to amdgpu_gem_update_timeline_node() which is incorrectly placed.

That needs to come much earlier, above the switch (args->operation)....

Regards,
Christian.

Quoted 8 lines
             fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
-                                            args->operation);
+                                            args->operation,
+                                            args->vm_timeline_syncobj_out);
 
             if (!r)
                     amdgpu_gem_update_bo_mapping(filp, bo_va,
maintainer
#4 lore raw

Yeah, completely agree.

But not checking the syncobj handle before doing the update is actually even more problematic than leaking the memory.

This could be used by userspace to put the kernel into a broken situation it can't come out any more.

Arvin can you take care of the complete fix?

Thanks,
Christian.

Am 14.02.25 um 13:14 schrieb YuanShang Mao (River):

Quoted 93 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Better to put the fence outside amdgpu_gem_va_update_vm. Since it is passed to the caller, and the caller must keep one reference at least until this fence is no longer needed.

Thanks
River

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Yadav, Arvind
Sent: Friday, February 14, 2025 7:42 PM
To: Koenig, Christian <[email protected]>; Ma, Le <[email protected]>; [email protected]; Yadav, Arvind <[email protected]>
Cc: Zhang, Hawking <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH v2] drm/amdgpu: fix the memleak caused by fence not released

Quoted 49 lines · Christian König

Adding Arvind, please make sure to keep him in the loop.

Am 14.02.25 um 11:07 schrieb Le Ma:

Quoted 46 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

Thus call dma_fence_put properly to avoid the memleak.

v2: call dma_fence_put in amdgpu_gem_va_update_vm

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
index 8b67aae6c2fe..00f1f34705c0 100644
@@ -759,7 +759,8 @@ static struct dma_fence *
  amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
                     struct amdgpu_vm *vm,
                     struct amdgpu_bo_va *bo_va,
-                    uint32_t operation)
+                    uint32_t operation,
+                    uint32_t syncobj_handle)
  {
     struct dma_fence *fence = dma_fence_get_stub();
     int r;
@@ -771,6 +772,9 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
     if (r)
             goto error;
 
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_va_update_vm(struct amdgpu_device *adev, · line 777

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -0
+    if (!syncobj_handle)
+            dma_fence_put(fence);
+

Having that check inside amdgpu_gem_update_bo_mapping() was actually correct. Here it doesn't make much sense.

Agreed,

Regards,
~Arvind

Quoted 19 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
     if (operation == AMDGPU_VA_OP_MAP ||
         operation == AMDGPU_VA_OP_REPLACE) {
             r = amdgpu_vm_bo_update(adev, bo_va, false); @@ -965,7 +969,8 @@

int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,

                                                 &timeline_chain);

Right before this here is a call to amdgpu_gem_update_timeline_node() which is incorrectly placed.

That needs to come much earlier, above the switch (args->operation)....

Regards,
Christian.

Quoted 7 lines
             fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
-                                            args->operation);
+                                            args->operation,
+                                            args->vm_timeline_syncobj_out);
 
             if (!r)
                     amdgpu_gem_update_bo_mapping(filp, bo_va,
#5 lore raw
Quoted 7 lines · Christian König

Yeah, completely agree.

But not checking the syncobj handle before doing the update is actually even more problematic than leaking the memory.

This could be used by userspace to put the kernel into a broken situation it can't come out any more.

Arvin can you take care of the complete fix?

Sure, I will do that.

Thanks,

~Arvind

Quoted 98 lines

Thanks,
Christian.

Am 14.02.25 um 13:14 schrieb YuanShang Mao (River):

Quoted 93 lines

[AMD Official Use Only - AMD Internal Distribution Only]

Better to put the fence outside amdgpu_gem_va_update_vm. Since it is passed to the caller, and the caller must keep one reference at least until this fence is no longer needed.

Thanks
River

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Yadav, Arvind
Sent: Friday, February 14, 2025 7:42 PM
To: Koenig, Christian <[email protected]>; Ma, Le <[email protected]>; [email protected]; Yadav, Arvind <[email protected]>
Cc: Zhang, Hawking <[email protected]>; Lazar, Lijo <[email protected]>
Subject: Re: [PATCH v2] drm/amdgpu: fix the memleak caused by fence not released

Quoted 49 lines · Christian König

Adding Arvind, please make sure to keep him in the loop.

Am 14.02.25 um 11:07 schrieb Le Ma:

Quoted 46 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

Thus call dma_fence_put properly to avoid the memleak.

v2: call dma_fence_put in amdgpu_gem_va_update_vm

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 9 +++++++--
   1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
index 8b67aae6c2fe..00f1f34705c0 100644
@@ -759,7 +759,8 @@ static struct dma_fence *
   amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
                      struct amdgpu_vm *vm,
                      struct amdgpu_bo_va *bo_va,
-                    uint32_t operation)
+                    uint32_t operation,
+                    uint32_t syncobj_handle)
   {
      struct dma_fence *fence = dma_fence_get_stub();
      int r;
@@ -771,6 +772,9 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
      if (r)
              goto error;
 
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_va_update_vm(struct amdgpu_device *adev, · line 777

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -0
+    if (!syncobj_handle)
+            dma_fence_put(fence);
+

Having that check inside amdgpu_gem_update_bo_mapping() was actually correct. Here it doesn't make much sense.

Agreed,

Regards,
~Arvind

Quoted 19 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
      if (operation == AMDGPU_VA_OP_MAP ||
          operation == AMDGPU_VA_OP_REPLACE) {
              r = amdgpu_vm_bo_update(adev, bo_va, false); @@ -965,7 +969,8 @@

int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,

                                                  &timeline_chain);

Right before this here is a call to amdgpu_gem_update_timeline_node() which is incorrectly placed.

That needs to come much earlier, above the switch (args->operation)....

Regards,
Christian.

Quoted 7 lines
              fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
-                                            args->operation);
+                                            args->operation,
+                                            args->vm_timeline_syncobj_out);
 
              if (!r)
                      amdgpu_gem_update_bo_mapping(filp, bo_va,