When vc4_save_hang_state() encounters an early return condition, it
returns without freeing the previously allocated `kernel_state`,
leaking memory.

Add the missing kfree() calls by consolidating the early return paths
into a single place.

Fixes: 214613656b51 ("drm/vc4: Add an interface for capturing the GPU state 
after a hang.")
Signed-off-by: Maíra Canal <[email protected]>
---
 drivers/gpu/drm/vc4/vc4_gem.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/vc4/vc4_gem.c b/drivers/gpu/drm/vc4/vc4_gem.c
index 
248c47665a193fa639a481f97ac2af634f415384..ab3c6d5d4eb446804877d6b8da01c3a9f65800df
 100644
--- a/drivers/gpu/drm/vc4/vc4_gem.c
+++ b/drivers/gpu/drm/vc4/vc4_gem.c
@@ -171,10 +171,8 @@ vc4_save_hang_state(struct drm_device *dev)
        spin_lock_irqsave(&vc4->job_lock, irqflags);
        exec[0] = vc4_first_bin_job(vc4);
        exec[1] = vc4_first_render_job(vc4);
-       if (!exec[0] && !exec[1]) {
-               spin_unlock_irqrestore(&vc4->job_lock, irqflags);
-               return;
-       }
+       if (!exec[0] && !exec[1])
+               goto err_free_state;
 
        /* Get the bos from both binner and renderer into hang state. */
        state->bo_count = 0;
@@ -191,10 +189,8 @@ vc4_save_hang_state(struct drm_device *dev)
        kernel_state->bo = kzalloc_objs(*kernel_state->bo, state->bo_count,
                                        GFP_ATOMIC);
 
-       if (!kernel_state->bo) {
-               spin_unlock_irqrestore(&vc4->job_lock, irqflags);
-               return;
-       }
+       if (!kernel_state->bo)
+               goto err_free_state;
 
        k = 0;
        for (i = 0; i < 2; i++) {
@@ -286,6 +282,12 @@ vc4_save_hang_state(struct drm_device *dev)
                vc4->hang_state = kernel_state;
                spin_unlock_irqrestore(&vc4->job_lock, irqflags);
        }
+
+       return;
+
+err_free_state:
+       spin_unlock_irqrestore(&vc4->job_lock, irqflags);
+       kfree(kernel_state);
 }
 
 static void

-- 
2.53.0

Reply via email to