Skip to contentSkip to conversation

drm/amdgpu: Fix double free of dmabuf

PATCH In review xinhui pan 3 participants 4 messages Nov 19, 2021

Subsystems (3)
lore
Share & tools
lore

xinhui pan

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix double free of dmabuf

amdgpu_amdkfd_gpuvm_free_memory_of_gpu drop dmabuf reference increased in amdgpu_gem_prime_export.
amdgpu_bo_destroy drop dmabuf reference increased in amdgpu_gem_prime_import.

So remove this extra dma_buf_put to avoid double free.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -6
index 6c6d5d994b5d..0f68d28e002e 100644
@@ -665,12 +665,6 @@ kfd_mem_attach_dmabuf(struct amdgpu_device *adev, struct kgd_mem *mem,
 	if (IS_ERR(gobj))
 		return PTR_ERR(gobj);
 
-	/* Import takes an extra reference on the dmabuf. Drop it now to
-	 * avoid leaking it. We only need the one reference in
-	 * kgd_mem->dmabuf.
-	 */
-	dma_buf_put(mem->dmabuf);
-
 	*bo = gem_to_amdgpu_bo(gobj);
 	(*bo)->flags |= AMDGPU_GEM_CREATE_PREEMPTIBLE;
 	(*bo)->parent = amdgpu_bo_ref(mem->bo);
Signature · 1 line
2.25.1
maintainer Acked-by on v1 1/1
#1 lore raw

Am 19.11.21 um 11:17 schrieb xinhui pan:

Quoted 8 lines

amdgpu_amdkfd_gpuvm_free_memory_of_gpu drop dmabuf reference increased in amdgpu_gem_prime_export.
amdgpu_bo_destroy drop dmabuf reference increased in amdgpu_gem_prime_import.

So remove this extra dma_buf_put to avoid double free.

Yeah, Felix pinged me as well that the DMA-buf import code looks odd and he is dropping the extra reference which didn't made much sense to me.

Regards,
Christian.

Quoted 21 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 6 ------
  1 file changed, 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -6
index 6c6d5d994b5d..0f68d28e002e 100644
@@ -665,12 +665,6 @@ kfd_mem_attach_dmabuf(struct amdgpu_device *adev, struct kgd_mem *mem,
  	if (IS_ERR(gobj))
  		return PTR_ERR(gobj);
 
-	/* Import takes an extra reference on the dmabuf. Drop it now to
-	 * avoid leaking it. We only need the one reference in
-	 * kgd_mem->dmabuf.
-	 */
-	dma_buf_put(mem->dmabuf);
-
  	*bo = gem_to_amdgpu_bo(gobj);
  	(*bo)->flags |= AMDGPU_GEM_CREATE_PREEMPTIBLE;
  	(*bo)->parent = amdgpu_bo_ref(mem->bo);
maintainer
#2 lore raw

I'm pretty sure I needed this dma_buf_put to avoid a memory leak. I will double check later today.

Regards,
  Felix

Am 2021-11-19 um 5:52 a.m. schrieb Christian König:

Quoted 44 lines

Am 19.11.21 um 11:17 schrieb xinhui pan:

Quoted 9 lines

amdgpu_amdkfd_gpuvm_free_memory_of_gpu drop dmabuf reference increased in
amdgpu_gem_prime_export.
amdgpu_bo_destroy drop dmabuf reference increased in amdgpu_gem_prime_import.

So remove this extra dma_buf_put to avoid double free.

Yeah, Felix pinged me as well that the DMA-buf import code looks odd and he is dropping the extra reference which didn't made much sense to me.

for now, but I'm going to take another look at this when I have time.

Regards,
Christian.

Quoted 22 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 6 ------

  1 file changed, 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
index 6c6d5d994b5d..0f68d28e002e 100644
@@ -665,12 +665,6 @@ kfd_mem_attach_dmabuf(struct amdgpu_device
 *adev, struct kgd_mem *mem,
       if (IS_ERR(gobj))
           return PTR_ERR(gobj);

  -    /* Import takes an extra reference on the dmabuf. Drop it now to -     * avoid leaking it. We only need the one reference in -     * kgd_mem->dmabuf.
-     */
-    dma_buf_put(mem->dmabuf);
-
      *bo = gem_to_amdgpu_bo(gobj);
      (*bo)->flags |= AMDGPU_GEM_CREATE_PREEMPTIBLE; (*bo)->parent = amdgpu_bo_ref(mem->bo);

maintainer Tested-by Reviewed-by on v1 1/1
#3 lore raw
In reply to xinhui pan
Quoted 4 lines · xinhui pan

amdgpu_amdkfd_gpuvm_free_memory_of_gpu drop dmabuf reference increased in amdgpu_gem_prime_export.
amdgpu_bo_destroy drop dmabuf reference increased in amdgpu_gem_prime_import.

OK. This was not obvious at all. The get_dma_buf in amdgpu_gem_prime_import could use a comment that it's balanced by the dma_buf_put in drm_prime_gem_destroy, which gets called from amdgpu_bo_destroy for any bo that has a bo->tbo.base.import_attach.

I confirmed the bug and tested the fix by running the following test on a 2-GPU system

    HSA_USERPTR_FOR_PAGED_MEM=0 run_kfdtest.sh --gtest_filter=KFDMemoryTest.MMBench

Quoted 4 lines

So remove this extra dma_buf_put to avoid double free.

Quoted 21 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 6 ------
  1 file changed, 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -6
index 6c6d5d994b5d..0f68d28e002e 100644
@@ -665,12 +665,6 @@ kfd_mem_attach_dmabuf(struct amdgpu_device *adev, struct kgd_mem *mem,
  	if (IS_ERR(gobj))
  		return PTR_ERR(gobj);
 
-	/* Import takes an extra reference on the dmabuf. Drop it now to
-	 * avoid leaking it. We only need the one reference in
-	 * kgd_mem->dmabuf.
-	 */
-	dma_buf_put(mem->dmabuf);
-
  	*bo = gem_to_amdgpu_bo(gobj);
  	(*bo)->flags |= AMDGPU_GEM_CREATE_PREEMPTIBLE;
  	(*bo)->parent = amdgpu_bo_ref(mem->bo);