[PATCH] drm/nouveau: Fix memory leak in nouveau_channel_ctor()

Zilin Guan posted 1 patch 1 week, 6 days ago
drivers/gpu/drm/nouveau/nouveau_chan.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] drm/nouveau: Fix memory leak in nouveau_channel_ctor()
Posted by Zilin Guan 1 week, 6 days ago
In nouveau_channel_ctor(), nouveau_channel_prep() allocates the channel
structure. If nvif_mem_ctor() fails to allocate the userd memory, the
function returns without freeing the channel, resulting in a memory leak.

Add nouveau_channel_del() to the error path to ensure the allocated
channel is properly freed.

Compile tested only. Issue found using a prototype static analysis tool
and code review.

Fixes: 06db7fded6de ("drm/nouveau/fifo: add new channel classes")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
 drivers/gpu/drm/nouveau/nouveau_chan.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/nouveau/nouveau_chan.c b/drivers/gpu/drm/nouveau/nouveau_chan.c
index b1e92b1f7a26..a421c32fda02 100644
--- a/drivers/gpu/drm/nouveau/nouveau_chan.c
+++ b/drivers/gpu/drm/nouveau/nouveau_chan.c
@@ -322,8 +322,10 @@ nouveau_channel_ctor(struct nouveau_cli *cli, bool priv, u64 runm,
 		ret = nvif_mem_ctor(&cli->mmu, "abi16ChanUSERD", NVIF_CLASS_MEM_GF100,
 				    NVIF_MEM_VRAM | NVIF_MEM_COHERENT | NVIF_MEM_MAPPABLE,
 				    0, PAGE_SIZE, NULL, 0, &chan->mem_userd);
-		if (ret)
+		if (ret) {
+			nouveau_channel_del(pchan);
 			return ret;
+		}
 
 		args->huserd = nvif_handle(&chan->mem_userd.object);
 		args->ouserd = 0;
-- 
2.34.1
Re: [PATCH] drm/nouveau: Fix memory leak in nouveau_channel_ctor()
Posted by Markus Elfring 1 week, 5 days ago
…
> Add nouveau_channel_del() to the error path to ensure the allocated
> channel is properly freed.
…

https://elixir.bootlin.com/linux/v6.19-rc5/source/drivers/gpu/drm/nouveau/nouveau_chan.c#L247-L351

* Would you like to complete the exception handling by using another goto chain?

* How do you think about to increase the application of scope-based resource management?


See also once more:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v6.19-rc7#n34

Regards,
Markus