On 26/11/2026 12:09, Christian König wrote:
On 11/26/26 12:11, Tvrtko Ursulin wrote:
Use scoped cleanup for drm_syncobj_array_wait_timeout points 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 | 27 ++++++++++-----------------
  1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c
index 663d6cb8dbf8..bd3f9e74e678 100644
--- a/drivers/gpu/drm/drm_syncobj.c
+++ b/drivers/gpu/drm/drm_syncobj.c
@@ -193,6 +193,7 @@
   */
#include <linux/anon_inodes.h>
+#include <linux/cleanup.h>
  #include <linux/dma-fence-unwrap.h>
  #include <linux/eventfd.h>
  #include <linux/export.h>
@@ -1052,8 +1053,8 @@ static signed long drm_syncobj_array_wait_timeout(struct 
drm_syncobj **syncobjs,
                                                  ktime_t *deadline)
  {
        struct syncobj_wait_entry *entries;
+       u64 *points __free(kvfree) = NULL;
        struct dma_fence *fence;
-       uint64_t *points;
        uint32_t signaled_count, i;
if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
@@ -1063,23 +1064,18 @@ static signed long 
drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
        }
points = kvmalloc_objs(*points, count);
-       if (points == NULL)
+       if (!points)
                return -ENOMEM;
- if (!user_points) {
-               memset(points, 0, count * sizeof(uint64_t));
-
-       } else if (copy_from_user(points, user_points,
-                                 sizeof(uint64_t) * count)) {
-               timeout = -EFAULT;
-               goto err_free_points;
-       }
+       if (!user_points)
+               memset(points, 0, count * sizeof(u64));
+       else if (copy_from_user(points, user_points, sizeof(u64) * count))
+               return -EFAULT;

I would go a step further here and don't even allocate points when user_points 
is not given.

There are only two uses of points[i] below and that can easily be replaced by a single 
"points ? points[i] : 0" making the additional allocation superfluous for this 
use case.

I actually attempted to do this few years ago by dropping this temporary storage altogether and just reading the points as we go. I think your concern back then was potential extra overhead from copy_from_user done per point (and it cannot be get_user since 32-bit arm cannot do 64-bit get/put user).

The alternative of splitting into no user_points and user_points paths is something I would need to see how it would look. Lets have some conclusions on other patches and then see.

Tvrtko

entries = kvzalloc_objs(*entries, count);
-       if (!entries) {
-               timeout = -ENOMEM;
-               goto err_free_points;
-       }
+       if (!entries)
+               return -ENOMEM;
+
        /* 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
@@ -1198,9 +1194,6 @@ static signed long drm_syncobj_array_wait_timeout(struct 
drm_syncobj **syncobjs,
        }
        kvfree(entries);
-err_free_points:
-       kvfree(points);
-
        return timeout;
  }


Reply via email to