Use scoped cleanup for drm_syncobj_timeline_signal_ioctl chains array
allocation.

This is a split out step towards converting the whole function with the
goal of 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 | 41 +++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c
index e05051c3895f..da90ae11382e 100644
--- a/drivers/gpu/drm/drm_syncobj.c
+++ b/drivers/gpu/drm/drm_syncobj.c
@@ -1566,15 +1566,33 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void 
*data,
        return ret;
 }
 
+struct syncobj_chains {
+       struct dma_fence_chain **chains;
+       unsigned int count;
+};
+
+static void syncobj_chains_free(struct syncobj_chains *c)
+{
+       if (!c->chains)
+               return;
+
+       for (unsigned int i = 0; i < c->count; i++)
+               dma_fence_chain_free(c->chains[i]);
+
+       kvfree(c->chains);
+}
+
+DEFINE_FREE(syncobj_chains, struct syncobj_chains, syncobj_chains_free(&_T))
+
 int
 drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,
                                  struct drm_file *file_private)
 {
        struct drm_syncobj_timeline_array *args = data;
+       struct syncobj_chains chains __free(syncobj_chains) = {};
        u64 *points __free(kvfree) = NULL;
        struct drm_syncobj **syncobjs;
-       struct dma_fence_chain **chains;
-       uint32_t i, j;
+       unsigned int i;
        int ret;
 
        if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
@@ -1606,30 +1624,29 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device 
*dev, void *data,
                goto out;
        }
 
-       chains = kvmalloc_objs(*chains, args->count_handles);
-       if (!chains) {
+       chains.chains = kvmalloc_objs(*chains.chains, args->count_handles);
+       if (!chains.chains) {
                ret = -ENOMEM;
                goto out;
        }
        for (i = 0; i < args->count_handles; i++) {
-               chains[i] = dma_fence_chain_alloc();
-               if (!chains[i]) {
-                       for (j = 0; j < i; j++)
-                               dma_fence_chain_free(chains[j]);
+               chains.chains[i] = dma_fence_chain_alloc();
+               if (!chains.chains[i]) {
                        ret = -ENOMEM;
-                       goto err_chains;
+                       goto out;
                }
+               chains.count++;
        }
 
        for (i = 0; i < args->count_handles; i++) {
                struct dma_fence *fence = dma_fence_get_stub();
 
-               drm_syncobj_add_point(syncobjs[i], chains[i],
+               drm_syncobj_add_point(syncobjs[i], chains.chains[i],
                                      fence, points[i]);
+               chains.chains[i] = NULL; /* Ownership transferred to syncobj */
                dma_fence_put(fence);
        }
-err_chains:
-       kvfree(chains);
+
 out:
        drm_syncobj_array_free(syncobjs, args->count_handles);
 
-- 
2.56.0

Reply via email to