Skip to contentSkip to conversation

drm/amdgpu: Fix double free in amdgpu_userq_fence_driver_alloc()

PATCH Superseded Dan Carpenter 2 participants 3 messages Apr 10, 2025 – Apr 12, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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")

---

---
 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
#1 lore raw

Please change this also instead of 'goto free_fence_drv' just return err.

        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; // this should be replace by return. }

~arvind

Quoted 31 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")

---

---
  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));
 
#2 lore raw
Quoted 10 lines · Arvind

Please change this also instead of 'goto free_fence_drv' just return err.

        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; // this should be replace by return. }

~arvind

I noticed that when I was writing my patch as well. I'm always in favor of direct returns, but it makes the patch confusing to add this unrelated cleanup... I'll send it as a separate patch.

regards,
dan carpenter