amdgpu_ttm_buffer_entity_init() checks num_gart_windows after
drm_sched_entity_init() has already attached the entity to a
scheduler run-queue. Returning -EINVAL from that check leaks the
entity.

Move the ARRAY_SIZE() check before any resources are acquired.

Fixes: 2c3725572561 ("drm/amdgpu: statically assign gart windows to ttm 
entities")
Signed-off-by: Perry Yuan <[email protected]>
Reviewed-by: Christian König <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index 3d620ec2937f..5465787f730a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -2076,14 +2076,15 @@ static int amdgpu_ttm_buffer_entity_init(struct 
amdgpu_gtt_mgr *mgr,
        int i, r, num_pages;
        const u64 GTT_MAX_PAGES = (AMDGPU_GTT_MAX_TRANSFER_SIZE >> PAGE_SHIFT);
 
+       if (ARRAY_SIZE(entity->gart_window_offs) < num_gart_windows)
+               return -EINVAL;
+
        r = drm_sched_entity_init(&entity->base, prio, scheds, num_schedulers, 
NULL);
        if (r)
                return r;
 
        mutex_init(&entity->lock);
 
-       if (ARRAY_SIZE(entity->gart_window_offs) < num_gart_windows)
-               return -EINVAL;
        if (num_gart_windows == 0)
                return 0;
 
-- 
2.34.1

Reply via email to