Skip to contentSkip to conversation

drm/amdkfd: fix kgd_mem memory leak when importing dmabuf

PATCH In review Lang Yu 2 participants 3 messages Jul 26, 2022 – Jul 27, 2022

Subsystems (3)
lore
Share & tools
lore

Lang Yu

author
#0 lore raw

PATCH 1/1 drm/amdkfd: fix kgd_mem memory leak when importing dmabuf

The kgd_mem memory allocated in amdgpu_amdkfd_gpuvm_import_dmabuf() is not freed properly.

Explicitly free it in amdgpu_amdkfd_gpuvm_free_memory_of_gpu() under condition "mem->bo->kfd_bo != mem".

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +8 -0
index 6bba6961eee7..086bed40cf34 100644
@@ -1803,6 +1803,7 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
 		uint64_t *size)
 {
 	struct amdkfd_process_info *process_info = mem->process_info;
+	struct kgd_mem *tmp_mem = mem->bo->kfd_bo;
 	unsigned long bo_size = mem->bo->tbo.base.size;
 	struct kfd_mem_attachment *entry, *tmp;
 	struct bo_vm_reservation_context ctx;
@@ -1895,6 +1896,13 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
 	 */
 	drm_gem_object_put(&mem->bo->tbo.base);
 
+	/*
+	 * For kgd_mem allocated in amdgpu_amdkfd_gpuvm_import_dmabuf(),
+	 * explicitly free it here.
+	 */
+	if (mem != tmp_mem)
+		kfree(mem);
+
 	return ret;
 }
 
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Am 2022-07-26 um 07:15 schrieb Lang Yu:

Quoted 8 lines

The kgd_mem memory allocated in amdgpu_amdkfd_gpuvm_import_dmabuf() is not freed properly.

Explicitly free it in amdgpu_amdkfd_gpuvm_free_memory_of_gpu() under condition "mem->bo->kfd_bo != mem".

Some suggestions inline to make the code more readable. Other than that, the patch is

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1806

Quoted 13 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 8 ++++++++
  1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
index 6bba6961eee7..086bed40cf34 100644
@@ -1803,6 +1803,7 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  		uint64_t *size)
  {
  	struct amdkfd_process_info *process_info = mem->process_info;
+	struct kgd_mem *tmp_mem = mem->bo->kfd_bo;

Right, you need this because using mem after drm_gem_object_put would be a potential use-after-free. Instead of saving this pointer with some obscure variable name, you could just save a bool with a more meaningful name. E.g.:

	bool use_release_notifier = (mem->bo->kfd_bo == mem);

This way you have the entire condition in one place, and the variable name explains the meaning.

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1903

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +5 -0
  	unsigned long bo_size = mem->bo->tbo.base.size;
  	struct kfd_mem_attachment *entry, *tmp;
  	struct bo_vm_reservation_context ctx;
@@ -1895,6 +1896,13 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  	 */
  	drm_gem_object_put(&mem->bo->tbo.base);
 
+	/*
+	 * For kgd_mem allocated in amdgpu_amdkfd_gpuvm_import_dmabuf(),
+	 * explicitly free it here.
+	 */
+	if (mem != tmp_mem)
	if (!use_release_notifier)
		kfree(mem);

Regards,
  Felix

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -0
+		kfree(mem);
+
  	return ret;
  }
 
author
#2 lore raw
Quoted 34 lines · Felix Kuehling

Am 2022-07-26 um 07:15 schrieb Lang Yu:

Quoted 8 lines

The kgd_mem memory allocated in amdgpu_amdkfd_gpuvm_import_dmabuf() is not freed properly.

Explicitly free it in amdgpu_amdkfd_gpuvm_free_memory_of_gpu() under condition "mem->bo->kfd_bo != mem".

Some suggestions inline to make the code more readable. Other than that, the patch is

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1806

Quoted 13 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 8 ++++++++
  1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
index 6bba6961eee7..086bed40cf34 100644
@@ -1803,6 +1803,7 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  		uint64_t *size)
  {
  	struct amdkfd_process_info *process_info = mem->process_info;
+	struct kgd_mem *tmp_mem = mem->bo->kfd_bo;

Right, you need this because using mem after drm_gem_object_put would be a potential use-after-free. Instead of saving this pointer with some obscure variable name, you could just save a bool with a more meaningful name. E.g.:

Quoted 4 lines
	bool use_release_notifier = (mem->bo->kfd_bo == mem);

This way you have the entire condition in one place, and the variable name explains the meaning.

Got it. Thanks!

Regards,
Lang

Quoted 24 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( · line 1902

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +5 -0
  	unsigned long bo_size = mem->bo->tbo.base.size;
  	struct kfd_mem_attachment *entry, *tmp;
  	struct bo_vm_reservation_context ctx;
@@ -1895,6 +1896,13 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  	 */
  	drm_gem_object_put(&mem->bo->tbo.base);
+	/*
+	 * For kgd_mem allocated in amdgpu_amdkfd_gpuvm_import_dmabuf(),
+	 * explicitly free it here.
+	 */
+	if (mem != tmp_mem)
	if (!use_release_notifier)
		kfree(mem);

Regards,
  Felix

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -0
+		kfree(mem);
+
  	return ret;
  }