From 323dedc88de6651a1c9c878bf1bdad5c3eb9db68 Mon Sep 17 00:00:00 2001 From: Adeel Raza Date: Tue, 19 Feb 2019 15:05:20 -0800 Subject: [PATCH] gpu: nvgpu: posix: fix mem leak in sgt create nvgpu_sgt_os_create_from_mem() had a memory leak where an sgt struct was being malloced but not being returned to the caller. This patch fixes the bug by changing the execution sequence. Change-Id: Id3474fc9a15a9463e589b39565a57d10d3f0804a Signed-off-by: Adeel Raza Reviewed-on: https://git-master.nvidia.com/r/2023266 Reviewed-by: svc-misra-checker Reviewed-by: Alex Waterman Reviewed-by: svc-mobile-coverity Reviewed-by: svc-mobile-misra GVS: Gerrit_Virtual_Submit Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/os/posix/posix-nvgpu_mem.c | 33 ++++++++++---------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/drivers/gpu/nvgpu/os/posix/posix-nvgpu_mem.c b/drivers/gpu/nvgpu/os/posix/posix-nvgpu_mem.c index 421203043..531b040e2 100644 --- a/drivers/gpu/nvgpu/os/posix/posix-nvgpu_mem.c +++ b/drivers/gpu/nvgpu/os/posix/posix-nvgpu_mem.c @@ -208,34 +208,35 @@ struct nvgpu_sgt *nvgpu_sgt_os_create_from_mem(struct gk20a *g, struct nvgpu_mem *mem) { struct nvgpu_mem_sgl *sgl; - struct nvgpu_sgt *sgt = nvgpu_kzalloc(g, sizeof(*sgt)); + struct nvgpu_sgt *sgt; + if (mem->priv.sgt != NULL) { + return mem->priv.sgt; + } + + sgt = nvgpu_kzalloc(g, sizeof(*sgt)); if (sgt == NULL) { return NULL; } + sgt->ops = &nvgpu_sgt_posix_ops; + /* * The userspace implementation is simple: a single 'entry' (which we * only need the nvgpu_mem_sgl struct to describe). A unit test can * easily replace it if needed. */ - sgt->ops = &nvgpu_sgt_posix_ops; - - if (mem->priv.sgt != NULL) { - return mem->priv.sgt; - } else { - sgl = (struct nvgpu_mem_sgl *) nvgpu_kzalloc(g, sizeof( - struct nvgpu_mem_sgl)); - if (sgl == NULL) { - nvgpu_kfree(g, sgt); - return NULL; - } - - sgl->length = mem->size; - sgl->phys = (u64) mem->cpu_va; - sgt->sgl = (struct nvgpu_sgl *) sgl; + sgl = (struct nvgpu_mem_sgl *) nvgpu_kzalloc(g, sizeof( + struct nvgpu_mem_sgl)); + if (sgl == NULL) { + nvgpu_kfree(g, sgt); + return NULL; } + sgl->length = mem->size; + sgl->phys = (u64) mem->cpu_va; + sgt->sgl = (struct nvgpu_sgl *) sgl; + return sgt; }