Skip to contentSkip to conversation

drm/amdgpu: Fix potential dma_fence leak in amdgpu_ttm_clear_buffer

PATCH NAK Li Ma 3 participants 5 messages May 29, 2025 – Jun 2, 2025

Subsystems (2)
lore
Share & tools
lore

Li Ma

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix potential dma_fence leak in amdgpu_ttm_clear_buffer

The original code did not properly release the dma_fence next in case amdgpu_ttm_fill_mem failed during buffer clearing.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -1
index 9c5df35f05b7..b7284f0a5ac0 100644
@@ -2296,6 +2296,7 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
 	struct amdgpu_res_cursor cursor;
+	struct dma_fence *next = NULL;
 	u64 addr;
 	int r = 0;
 
@@ -2311,7 +2312,6 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
 	mutex_lock(&adev->mman.gtt_window_lock);
 	while (cursor.remaining) {
-		struct dma_fence *next = NULL;
 		u64 size;
 
 		if (amdgpu_res_cleared(&cursor)) {
@@ -2334,10 +2334,13 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
 		dma_fence_put(*fence);
 		*fence = next;
+		next = NULL;
 
 		amdgpu_res_next(&cursor, size);
 	}
 err:
+	if (next)
+		dma_fence_put(next);
 	mutex_unlock(&adev->mman.gtt_window_lock);
 
 	return r;
Signature · 1 line
2.25.1
#1 lore raw

Hi Ma,

Quoted 29 lines · Li Ma

The original code did not properly release the dma_fence next in case amdgpu_ttm_fill_mem failed during buffer clearing.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
index 9c5df35f05b7..b7284f0a5ac0 100644
@@ -2296,6 +2296,7 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
  	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  	struct amdgpu_res_cursor cursor;
+	struct dma_fence *next = NULL;
  	u64 addr;
  	int r = 0;
 
@@ -2311,7 +2312,6 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
  	mutex_lock(&adev->mman.gtt_window_lock);
  	while (cursor.remaining) {
-		struct dma_fence *next = NULL;
  		u64 size;
 
  		if (amdgpu_res_cleared(&cursor)) {

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo, · line 2343

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
@@ -2334,10 +2334,13 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
  		dma_fence_put(*fence);
  		*fence = next;
+		next = NULL;
 
  		amdgpu_res_next(&cursor, size);
  	}
  err:
+	if (next)
+		dma_fence_put(next);

Since you are observing use-after-free warning for the compute dispatch test in amdgpu_test with this patch, can we try the below code in amdgpu_bo_create() function,

r = amdgpu_ttm_clear_buffer(bo, bo->tbo.base.resv, &fence); if (unlikely(r)) {
                        if (fence) dma_fence_put(fence);

                        goto fail_unreserve;
                }
Regards,
Arun.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
  	mutex_unlock(&adev->mman.gtt_window_lock);
 
  	return r;
author
#2 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Arun,

This patch is not for the issue we discussed in the other mail. There is a risk of 'next' fence leak when amdgpu_ttm_fill_mem failed.

Best regards,
Li

Quoted 68 lines

-----Original Message-----
From: Paneer Selvam, Arunpravin <[email protected]>
Sent: Friday, May 30, 2025 1:57 PM
To: Ma, Li <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Yuan, Perry <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix potential dma_fence leak in amdgpu_ttm_clear_buffer

Hi Ma,

Review

Quoted 9 lines · Li Ma

The original code did not properly release the dma_fence next in case amdgpu_ttm_fill_mem failed during buffer clearing.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c

Quoted 20 lines

index 9c5df35f05b7..b7284f0a5ac0 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
@@ -2296,6 +2296,7 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
    struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
    struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
    struct amdgpu_res_cursor cursor;
+   struct dma_fence *next = NULL;
    u64 addr;
    int r = 0;
 
@@ -2311,7 +2312,6 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
    mutex_lock(&adev->mman.gtt_window_lock);
    while (cursor.remaining) {
-           struct dma_fence *next = NULL;
            u64 size;
 
            if (amdgpu_res_cleared(&cursor)) {

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo, · line 2343

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
@@ -2334,10 +2334,13 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
            dma_fence_put(*fence);
            *fence = next;
+           next = NULL;
 
            amdgpu_res_next(&cursor, size);
    }
  err:
+   if (next)
+           dma_fence_put(next);

Since you are observing use-after-free warning for the compute dispatch test in amdgpu_test with this patch,
can we try the below code in amdgpu_bo_create() function,

r = amdgpu_ttm_clear_buffer(bo, bo->tbo.base.resv, &fence);
                 if (unlikely(r)) {
                         if (fence)
                                 dma_fence_put(fence);

                         goto fail_unreserve;
                 }

Regards,
Arun.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
    mutex_unlock(&adev->mman.gtt_window_lock);
 
    return r;
#3 lore raw
Quoted 57 lines · Li

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Arun,

This patch is not for the issue we discussed in the other mail. There is a risk of 'next' fence leak when amdgpu_ttm_fill_mem failed.

Best regards,
Li

Quoted 47 lines

-----Original Message-----
From: Paneer Selvam, Arunpravin <[email protected]>
Sent: Friday, May 30, 2025 1:57 PM
To: Ma, Li <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Yuan, Perry <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Fix potential dma_fence leak in amdgpu_ttm_clear_buffer

Hi Ma,

Review

Quoted 9 lines · Li Ma

The original code did not properly release the dma_fence next in case amdgpu_ttm_fill_mem failed during buffer clearing.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c

Quoted 25 lines

index 9c5df35f05b7..b7284f0a5ac0 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -1
@@ -2296,6 +2296,7 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
     struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
     struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
     struct amdgpu_res_cursor cursor;
+   struct dma_fence *next = NULL;
     u64 addr;
     int r = 0;
 
@@ -2311,7 +2312,6 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
     mutex_lock(&adev->mman.gtt_window_lock);
     while (cursor.remaining) {
-           struct dma_fence *next = NULL;
             u64 size;
 
             if (amdgpu_res_cleared(&cursor)) {
@@ -2334,10 +2334,13 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
             dma_fence_put(*fence);
             *fence = next;
+           next = NULL;
 
Quoted 5 lines
Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo, · line 2343

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
             amdgpu_res_next(&cursor, size);
     }
   err:
+   if (next)
+           dma_fence_put(next);

This is okay for error case, but in success case we are dropping the same fence twice. We are adding the last returned fence to the bo and then we are already dropping the fence there below the amdgpu_ttm_clear_buffer() function call in amdgpu_bo_create() function.

Regards,
Arun.

Quoted 16 lines
Quoted 16 lines

Since you are observing use-after-free warning for the compute dispatch test in amdgpu_test with this patch,
can we try the below code in amdgpu_bo_create() function,

r = amdgpu_ttm_clear_buffer(bo, bo->tbo.base.resv, &fence);
                  if (unlikely(r)) {
                          if (fence)
                                  dma_fence_put(fence);

                          goto fail_unreserve;
                  }

Regards,
Arun.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
     mutex_unlock(&adev->mman.gtt_window_lock);
 
     return r;
maintainer NAK
#4 lore raw
In reply to Li Ma
Quoted 2 lines · Li Ma

The original code did not properly release the dma_fence next in case amdgpu_ttm_fill_mem failed during buffer clearing.

NAK, that is completely intentional.

We must return the fence even on a partial failed clear or otherwise the already submitted clears could work with freed up memory.

That's also why the *fence pointer is initialized with the stub fence before the loop.

Regards,
Christian.

Quoted 40 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -1
index 9c5df35f05b7..b7284f0a5ac0 100644
@@ -2296,6 +2296,7 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
 	struct amdgpu_res_cursor cursor;
+	struct dma_fence *next = NULL;
 	u64 addr;
 	int r = 0;
 
@@ -2311,7 +2312,6 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
 	mutex_lock(&adev->mman.gtt_window_lock);
 	while (cursor.remaining) {
-		struct dma_fence *next = NULL;
 		u64 size;
 
 		if (amdgpu_res_cleared(&cursor)) {
@@ -2334,10 +2334,13 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
 
 		dma_fence_put(*fence);
 		*fence = next;
+		next = NULL;
 
 		amdgpu_res_next(&cursor, size);
 	}
 err:
+	if (next)
+		dma_fence_put(next);
 	mutex_unlock(&adev->mman.gtt_window_lock);
 
 	return r;