Skip to contentSkip to conversation

drm/amdgpu: Fixes to amdgpu_userq_fence_driver_alloc()

PATCH Applied Dan Carpenter 3 participants 6 messages Apr 12, 2025 – Apr 14, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover drm/amdgpu: Fixes to amdgpu_userq_fence_driver_alloc()

This fixes a double free on one error path in amdgpu_userq_fence_driver_alloc() and cleans up another error path.

v2:
Did some additional cleanup

Dan Carpenter (2):
drm/amdgpu: Fix double free in amdgpu_userq_fence_driver_alloc() drm/amdgpu: Clean up error handling in
amdgpu_userq_fence_driver_alloc()

 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH v2 1/2 drm/amdgpu: Fix double free in amdgpu_userq_fence_driver_alloc()

The goto frees "fence_drv" so this is a double free bug. There is no need to call amdgpu_seq64_free(adev, fence_drv->va) since the seq64 allocation failed so change the goto to goto free_fence_drv. Also propagate the error code from amdgpu_seq64_alloc() instead of hard coding it to -ENOMEM.

Fixes e7cf21fbb277 ("drm/amdgpu: Few optimization and fixes for userq fence driver")

---
v2: No change.

 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -5
index a4953d668972..b012fece91e8 100644
@@ -84,11 +84,8 @@ int amdgpu_userq_fence_driver_alloc(struct amdgpu_device *adev,
 	/* Acquire seq64 memory */
 	r = amdgpu_seq64_alloc(adev, &fence_drv->va, &fence_drv->gpu_addr,
 			       &fence_drv->cpu_addr);
-	if (r) {
-		kfree(fence_drv);
-		r = -ENOMEM;
-		goto free_seq64;
-	}
+	if (r)
+		goto free_fence_drv;
 
 	memset(fence_drv->cpu_addr, 0, sizeof(u64));
 
Signature · 1 line
2.47.2
#3 lore raw

PATCH v2 2/2 drm/amdgpu: Clean up error handling in amdgpu_userq_fence_driver_alloc()

In reply to Dan Carpenter

1) Checkpatch complains if we print an error message for kzalloc() failure. The kzalloc() failure already has it's own error messages built in. Also this allocation is small enough that it is guaranteed to succeed.
2) Return directly instead of doing a goto free_fence_drv. The "fence_drv" is already NULL so no cleanup is necessary.

---
v2: New patch

 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -5
index b012fece91e8..86eab5461162 100644
@@ -75,11 +75,8 @@ int amdgpu_userq_fence_driver_alloc(struct amdgpu_device *adev,
 	int r;
 
 	fence_drv = kzalloc(sizeof(*fence_drv), GFP_KERNEL);
-	if (!fence_drv) {
-		DRM_ERROR("Failed to allocate memory for fence driver\n");
-		r = -ENOMEM;
-		goto free_fence_drv;
-	}
+	if (!fence_drv)
+		return -ENOMEM;
 
 	/* Acquire seq64 memory */
 	r = amdgpu_seq64_alloc(adev, &fence_drv->va, &fence_drv->gpu_addr,
Signature · 1 line
2.47.2
Reviewed-by Reviewed-by:Arvind Yadav on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] drm/amdgpu: Fix double free in amdgpu_userq_fence_driver_alloc()

In reply to Dan Carpenter
Quoted 32 lines · Dan Carpenter

The goto frees "fence_drv" so this is a double free bug. There is no need to call amdgpu_seq64_free(adev, fence_drv->va) since the seq64 allocation failed so change the goto to goto free_fence_drv. Also propagate the error code from amdgpu_seq64_alloc() instead of hard coding it to -ENOMEM.

Fixes e7cf21fbb277 ("drm/amdgpu: Few optimization and fixes for userq fence driver")

---
v2: No change.

  drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -5
index a4953d668972..b012fece91e8 100644
@@ -84,11 +84,8 @@ int amdgpu_userq_fence_driver_alloc(struct amdgpu_device *adev,
  	/* Acquire seq64 memory */
  	r = amdgpu_seq64_alloc(adev, &fence_drv->va, &fence_drv->gpu_addr,
  			       &fence_drv->cpu_addr);
-	if (r) {
-		kfree(fence_drv);
-		r = -ENOMEM;
-		goto free_seq64;
-	}
+	if (r)
+		goto free_fence_drv;
 
  	memset(fence_drv->cpu_addr, 0, sizeof(u64));
 
Reviewed-by Arvind Yadav on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] drm/amdgpu: Clean up error handling in amdgpu_userq_fence_driver_alloc()

In reply to Dan Carpenter
Quoted 32 lines · Dan Carpenter

1) Checkpatch complains if we print an error message for kzalloc() failure. The kzalloc() failure already has it's own error messages built in. Also this allocation is small enough that it is guaranteed to succeed.
2) Return directly instead of doing a goto free_fence_drv. The "fence_drv" is already NULL so no cleanup is necessary.

---
v2: New patch

  drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -5
index b012fece91e8..86eab5461162 100644
@@ -75,11 +75,8 @@ int amdgpu_userq_fence_driver_alloc(struct amdgpu_device *adev,
  	int r;
 
  	fence_drv = kzalloc(sizeof(*fence_drv), GFP_KERNEL);
-	if (!fence_drv) {
-		DRM_ERROR("Failed to allocate memory for fence driver\n");
-		r = -ENOMEM;
-		goto free_fence_drv;
-	}
+	if (!fence_drv)
+		return -ENOMEM;
 
  	/* Acquire seq64 memory */
  	r = amdgpu_seq64_alloc(adev, &fence_drv->va, &fence_drv->gpu_addr,
Applied
#5 lore raw

Re: [PATCH v2 2/2] drm/amdgpu: Clean up error handling in amdgpu_userq_fence_driver_alloc()

Applied the series. Thanks!

Quoted 36 lines · Arvind
Quoted 32 lines · Dan Carpenter

1) Checkpatch complains if we print an error message for kzalloc() failure. The kzalloc() failure already has it's own error messages built in. Also this allocation is small enough that it is guaranteed to succeed.
2) Return directly instead of doing a goto free_fence_drv. The "fence_drv" is already NULL so no cleanup is necessary.

---
v2: New patch

  drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -5
index b012fece91e8..86eab5461162 100644
@@ -75,11 +75,8 @@ int amdgpu_userq_fence_driver_alloc(struct amdgpu_device *adev,
      int r;
 
      fence_drv = kzalloc(sizeof(*fence_drv), GFP_KERNEL);
-     if (!fence_drv) {
-             DRM_ERROR("Failed to allocate memory for fence driver\n");
-             r = -ENOMEM;
-             goto free_fence_drv;
-     }
+     if (!fence_drv)
+             return -ENOMEM;
 
      /* Acquire seq64 memory */
      r = amdgpu_seq64_alloc(adev, &fence_drv->va, &fence_drv->gpu_addr,