Use scoped cleanup for drm_syncobj_array_wait_timeout entries array
allocation and syncobj cleanup.

This converts the remaining parts of the function resulting in simpler
error handling and easier extending with a fast path in the future.

Signed-off-by: Tvrtko Ursulin <[email protected]>
Cc: Christian König <[email protected]>
Cc: Fei Yu <[email protected]>
Cc: Maíra Canal <[email protected]>
---
 drivers/gpu/drm/drm_syncobj.c | 61 +++++++++++++++++++++++------------
 1 file changed, 40 insertions(+), 21 deletions(-)

diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c
index bd3f9e74e678..7d5c4e4e29b2 100644
--- a/drivers/gpu/drm/drm_syncobj.c
+++ b/drivers/gpu/drm/drm_syncobj.c
@@ -1044,6 +1044,31 @@ static void syncobj_wait_syncobj_func(struct drm_syncobj 
*syncobj,
        list_del_init(&wait->node);
 }
 
+struct syncobj_wait_entries {
+       struct drm_syncobj **syncobjs;
+       struct syncobj_wait_entry *entries;
+       unsigned int count;
+};
+
+static void syncobj_wait_entries_free(struct syncobj_wait_entries *e)
+{
+       if (!e->entries)
+               return;
+
+       for (unsigned int i = 0; i < e->count; i++) {
+               drm_syncobj_remove_wait(e->syncobjs[i], &e->entries[i]);
+               if (e->entries[i].fence_cb.func)
+                       dma_fence_remove_callback(e->entries[i].fence,
+                                                 &e->entries[i].fence_cb);
+               dma_fence_put(e->entries[i].fence);
+       }
+
+       kvfree(e->entries);
+}
+
+DEFINE_FREE(syncobj_wait_entries, struct syncobj_wait_entries,
+           syncobj_wait_entries_free(&_T))
+
 static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj 
**syncobjs,
                                                  void __user *user_points,
                                                  uint32_t count,
@@ -1052,6 +1077,7 @@ static signed long drm_syncobj_array_wait_timeout(struct 
drm_syncobj **syncobjs,
                                                  uint32_t *idx,
                                                  ktime_t *deadline)
 {
+       struct syncobj_wait_entries cleanup __free(syncobj_wait_entries) = {};
        struct syncobj_wait_entry *entries;
        u64 *points __free(kvfree) = NULL;
        struct dma_fence *fence;
@@ -1076,6 +1102,11 @@ static signed long drm_syncobj_array_wait_timeout(struct 
drm_syncobj **syncobjs,
        if (!entries)
                return -ENOMEM;
 
+       /* After this the entries cleanup is fully automatic. */
+       cleanup.syncobjs = syncobjs;
+       cleanup.entries = entries;
+       cleanup.count = count;
+
        /* Walk the list of sync objects and initialize entries.  We do
         * this up-front so that we can properly return -EINVAL if there is
         * a syncobj with a missing fence and then never have the chance of
@@ -1091,12 +1122,10 @@ static signed long 
drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
                if (!fence || dma_fence_chain_find_seqno(&fence, points[i])) {
                        dma_fence_put(fence);
                        if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
-                                    DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) {
+                                    DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE))
                                continue;
-                       } else {
-                               timeout = -EINVAL;
-                               goto cleanup_entries;
-                       }
+
+                       return -EINVAL;
                }
 
                if (fence)
@@ -1115,7 +1144,7 @@ static signed long drm_syncobj_array_wait_timeout(struct 
drm_syncobj **syncobjs,
        if (signaled_count == count ||
            (signaled_count > 0 &&
             !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
-               goto cleanup_entries;
+               return timeout;
 
        /* There's a very annoying laxness in the dma_fence API here, in
         * that backends are not required to automatically report when a
@@ -1160,40 +1189,30 @@ static signed long 
drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
                                } else {
                                        if (idx)
                                                *idx = i;
-                                       goto done_waiting;
+                                       __set_current_state(TASK_RUNNING);
+                                       return timeout;
                                }
                        }
                }
 
                if (signaled_count == count)
-                       goto done_waiting;
+                       break;
 
                if (timeout == 0) {
                        timeout = -ETIME;
-                       goto done_waiting;
+                       break;
                }
 
                if (signal_pending(current)) {
                        timeout = -ERESTARTSYS;
-                       goto done_waiting;
+                       break;
                }
 
                timeout = schedule_timeout(timeout);
        } while (1);
 
-done_waiting:
        __set_current_state(TASK_RUNNING);
 
-cleanup_entries:
-       for (i = 0; i < count; ++i) {
-               drm_syncobj_remove_wait(syncobjs[i], &entries[i]);
-               if (entries[i].fence_cb.func)
-                       dma_fence_remove_callback(entries[i].fence,
-                                                 &entries[i].fence_cb);
-               dma_fence_put(entries[i].fence);
-       }
-       kvfree(entries);
-
        return timeout;
 }
 
-- 
2.56.0

Reply via email to