drm/msm: fix leak in failed get_pages
authorPrakash Kamliya <pkamliya@codeaurora.org>
Mon, 4 Dec 2017 13:40:15 +0000 (19:10 +0530)
committerRob Clark <robdclark@gmail.com>
Wed, 13 Dec 2017 16:12:03 +0000 (11:12 -0500)
get_pages doesn't keep a reference of the pages allocated
when it fails later in the code path. This can lead to
a memory leak. Keep reference of the allocated pages so
that it can be freed when msm_gem_free_object gets called
later during cleanup.

Signed-off-by: Prakash Kamliya <pkamliya@codeaurora.org>
Signed-off-by: Sharat Masetty <smasetty@codeaurora.org>
Signed-off-by: Rob Clark <robdclark@gmail.com>
drivers/gpu/drm/msm/msm_gem.c

index 81fe6d6740cec7582d11b4f92e267509dffb2c29..07376de9ff4cca630fb03cf1035e5be31b2ffbe8 100644 (file)
@@ -93,14 +93,17 @@ static struct page **get_pages(struct drm_gem_object *obj)
                        return p;
                }
 
+               msm_obj->pages = p;
+
                msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
                if (IS_ERR(msm_obj->sgt)) {
+                       void *ptr = ERR_CAST(msm_obj->sgt);
+
                        dev_err(dev->dev, "failed to allocate sgt\n");
-                       return ERR_CAST(msm_obj->sgt);
+                       msm_obj->sgt = NULL;
+                       return ptr;
                }
 
-               msm_obj->pages = p;
-
                /* For non-cached buffers, ensure the new pages are clean
                 * because display controller, GPU, etc. are not coherent:
                 */
@@ -135,7 +138,10 @@ static void put_pages(struct drm_gem_object *obj)
                if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
                        dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl,
                                        msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
-               sg_free_table(msm_obj->sgt);
+
+               if (msm_obj->sgt)
+                       sg_free_table(msm_obj->sgt);
+
                kfree(msm_obj->sgt);
 
                if (use_pages(obj))