All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdkfd: fix kgd_mem memory leak when importing dmabuf
@ 2022-07-26 11:15 Lang Yu
  2022-07-26 14:03 ` Felix Kuehling
  0 siblings, 1 reply; 3+ messages in thread
From: Lang Yu @ 2022-07-26 11:15 UTC (permalink / raw)
  To: amd-gfx; +Cc: Felix Kuehling, Lang Yu

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".

Suggested-by: Felix Kuehling <Felix.Kuehling@amd.com>
Signed-off-by: Lang Yu <Lang.Yu@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
index 6bba6961eee7..086bed40cf34 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
@@ -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;
 }
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/amdkfd: fix kgd_mem memory leak when importing dmabuf
  2022-07-26 11:15 [PATCH] drm/amdkfd: fix kgd_mem memory leak when importing dmabuf Lang Yu
@ 2022-07-26 14:03 ` Felix Kuehling
  2022-07-27  1:04   ` Lang Yu
  0 siblings, 1 reply; 3+ messages in thread
From: Felix Kuehling @ 2022-07-26 14:03 UTC (permalink / raw)
  To: Lang Yu, amd-gfx

Am 2022-07-26 um 07:15 schrieb Lang Yu:
> 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".
>
> Suggested-by: Felix Kuehling <Felix.Kuehling@amd.com>
> Signed-off-by: Lang Yu <Lang.Yu@amd.com>

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

Reviewed-by: Felix Kuehling <Felix.Kuehling@amd.com>


> ---
>   drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 8 ++++++++
>   1 file changed, 8 insertions(+)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> index 6bba6961eee7..086bed40cf34 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> @@ -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.


>   	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


> +		kfree(mem);
> +
>   	return ret;
>   }
>   

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/amdkfd: fix kgd_mem memory leak when importing dmabuf
  2022-07-26 14:03 ` Felix Kuehling
@ 2022-07-27  1:04   ` Lang Yu
  0 siblings, 0 replies; 3+ messages in thread
From: Lang Yu @ 2022-07-27  1:04 UTC (permalink / raw)
  To: Felix Kuehling; +Cc: amd-gfx

On 07/26/ , Felix Kuehling wrote:
> Am 2022-07-26 um 07:15 schrieb Lang Yu:
> > 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".
> > 
> > Suggested-by: Felix Kuehling <Felix.Kuehling@amd.com>
> > Signed-off-by: Lang Yu <Lang.Yu@amd.com>
> 
> Some suggestions inline to make the code more readable. Other than that, the
> patch is
> 
> Reviewed-by: Felix Kuehling <Felix.Kuehling@amd.com>
> 
> 
> > ---
> >   drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 8 ++++++++
> >   1 file changed, 8 insertions(+)
> > 
> > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> > index 6bba6961eee7..086bed40cf34 100644
> > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> > @@ -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.

Got it. Thanks!

Regards,
Lang

> 
> >   	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
> 
> 
> > +		kfree(mem);
> > +
> >   	return ret;
> >   }

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-07-27  1:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-26 11:15 [PATCH] drm/amdkfd: fix kgd_mem memory leak when importing dmabuf Lang Yu
2022-07-26 14:03 ` Felix Kuehling
2022-07-27  1:04   ` Lang Yu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.