dri-devel Archive mirror
 help / color / mirror / Atom feed
* [PATCH] drm/lima: fix a memleak in lima_heap_alloc
@ 2024-01-12  8:47 Zhipeng Lu
  2024-01-16  1:24 ` Qiang Yu
  0 siblings, 1 reply; 3+ messages in thread
From: Zhipeng Lu @ 2024-01-12  8:47 UTC (permalink / raw
  To: alexious
  Cc: Thomas Zimmermann, lima, dri-devel, linux-kernel, Maxime Ripard,
	Vasily Khoruzhick, Qiang Yu

When lima_vm_map_bo fails, the resources need to be deallocated, or
there will be memleaks.

Fixes: 6aebc51d7aef ("drm/lima: support heap buffer creation")
Signed-off-by: Zhipeng Lu <alexious@zju.edu.cn>
---
 drivers/gpu/drm/lima/lima_gem.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/lima/lima_gem.c b/drivers/gpu/drm/lima/lima_gem.c
index 4f9736e5f929..824ed22141c7 100644
--- a/drivers/gpu/drm/lima/lima_gem.c
+++ b/drivers/gpu/drm/lima/lima_gem.c
@@ -92,8 +92,13 @@ int lima_heap_alloc(struct lima_bo *bo, struct lima_vm *vm)
 
 	if (vm) {
 		ret = lima_vm_map_bo(vm, bo, old_size >> PAGE_SHIFT);
-		if (ret)
+		if (ret) {
+			dma_unmap_sgtable(dev, &sgt, DMA_BIDIRECTIONAL, 0);
+			sg_free_table(&sgt);
+			kfree(bo->base.sgt);
+			bo->base.sgt = NULL;
 			return ret;
+		}
 	}
 
 	bo->heap_size = new_size;
-- 
2.34.1


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

* Re: [PATCH] drm/lima: fix a memleak in lima_heap_alloc
  2024-01-12  8:47 [PATCH] drm/lima: fix a memleak in lima_heap_alloc Zhipeng Lu
@ 2024-01-16  1:24 ` Qiang Yu
  2024-01-17  7:10   ` alexious
  0 siblings, 1 reply; 3+ messages in thread
From: Qiang Yu @ 2024-01-16  1:24 UTC (permalink / raw
  To: Zhipeng Lu
  Cc: Thomas Zimmermann, lima, linux-kernel, Maxime Ripard,
	Vasily Khoruzhick, dri-devel, Daniel Vetter, David Airlie

Thanks for the fix. As the error handling gets longer and duplicated,
could you rearrange them like the lima_gem_submit():
err_out2:
    dma_unmap_sgtable(dev, &sgt, DMA_BIDIRECTIONAL, 0);
err_out1:
    kfree(bo->base.sgt);
    bo->base.sgt = NULL;
err_out0:
    sg_free_table(&sgt);
    return ret.

Regards,
Qiang

On Fri, Jan 12, 2024 at 4:49 PM Zhipeng Lu <alexious@zju.edu.cn> wrote:
>
> When lima_vm_map_bo fails, the resources need to be deallocated, or
> there will be memleaks.
>
> Fixes: 6aebc51d7aef ("drm/lima: support heap buffer creation")
> Signed-off-by: Zhipeng Lu <alexious@zju.edu.cn>
> ---
>  drivers/gpu/drm/lima/lima_gem.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/lima/lima_gem.c b/drivers/gpu/drm/lima/lima_gem.c
> index 4f9736e5f929..824ed22141c7 100644
> --- a/drivers/gpu/drm/lima/lima_gem.c
> +++ b/drivers/gpu/drm/lima/lima_gem.c
> @@ -92,8 +92,13 @@ int lima_heap_alloc(struct lima_bo *bo, struct lima_vm *vm)
>
>         if (vm) {
>                 ret = lima_vm_map_bo(vm, bo, old_size >> PAGE_SHIFT);
> -               if (ret)
> +               if (ret) {
> +                       dma_unmap_sgtable(dev, &sgt, DMA_BIDIRECTIONAL, 0);
> +                       sg_free_table(&sgt);
> +                       kfree(bo->base.sgt);
> +                       bo->base.sgt = NULL;
>                         return ret;
> +               }
>         }
>
>         bo->heap_size = new_size;
> --
> 2.34.1
>

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

* Re: Re: [PATCH] drm/lima: fix a memleak in lima_heap_alloc
  2024-01-16  1:24 ` Qiang Yu
@ 2024-01-17  7:10   ` alexious
  0 siblings, 0 replies; 3+ messages in thread
From: alexious @ 2024-01-17  7:10 UTC (permalink / raw
  To: Qiang Yu
  Cc: Thomas Zimmermann, lima, linux-kernel, Maxime Ripard,
	Vasily Khoruzhick, dri-devel, Daniel Vetter, David Airlie

> Thanks for the fix. As the error handling gets longer and duplicated,
> could you rearrange them like the lima_gem_submit():
> err_out2:
>     dma_unmap_sgtable(dev, &sgt, DMA_BIDIRECTIONAL, 0);
> err_out1:
>     kfree(bo->base.sgt);
>     bo->base.sgt = NULL;
> err_out0:
>     sg_free_table(&sgt);
>     return ret.
> 
> Regards,
> Qiang
> 

Sure, I'll send a v2 version of this patch later following your advise.

Regards,
Zhipeng

> On Fri, Jan 12, 2024 at 4:49 PM Zhipeng Lu <alexious@zju.edu.cn> wrote:
> >
> > When lima_vm_map_bo fails, the resources need to be deallocated, or
> > there will be memleaks.
> >
> > Fixes: 6aebc51d7aef ("drm/lima: support heap buffer creation")
> > Signed-off-by: Zhipeng Lu <alexious@zju.edu.cn>
> > ---
> >  drivers/gpu/drm/lima/lima_gem.c | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/gpu/drm/lima/lima_gem.c b/drivers/gpu/drm/lima/lima_gem.c
> > index 4f9736e5f929..824ed22141c7 100644
> > --- a/drivers/gpu/drm/lima/lima_gem.c
> > +++ b/drivers/gpu/drm/lima/lima_gem.c
> > @@ -92,8 +92,13 @@ int lima_heap_alloc(struct lima_bo *bo, struct lima_vm *vm)
> >
> >         if (vm) {
> >                 ret = lima_vm_map_bo(vm, bo, old_size >> PAGE_SHIFT);
> > -               if (ret)
> > +               if (ret) {
> > +                       dma_unmap_sgtable(dev, &sgt, DMA_BIDIRECTIONAL, 0);
> > +                       sg_free_table(&sgt);
> > +                       kfree(bo->base.sgt);
> > +                       bo->base.sgt = NULL;
> >                         return ret;
> > +               }
> >         }
> >
> >         bo->heap_size = new_size;
> > --
> > 2.34.1
> >

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

end of thread, other threads:[~2024-01-17  7:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-12  8:47 [PATCH] drm/lima: fix a memleak in lima_heap_alloc Zhipeng Lu
2024-01-16  1:24 ` Qiang Yu
2024-01-17  7:10   ` alexious

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).