On 19/11/2026 10:57, Christian König wrote:
On 11/17/26 11:39, Tvrtko Ursulin wrote:
Running the Cyberpunk 2077 benchmark we can observe that waiting on DRM
sycobjs is relatively hot, but the 96% of the calls are for a single
object. (~4% for two points, and never more than three points. While
a more trivial workload like vkmark under Plasma is even more skewed
to single point waits.)

Therefore lets add a fast path to bypass the kcalloc/kfree and use a pre-
allocated stack array for those cases.

In general it seems valid to optimize that stuff, but I'm a bit concerned about 
the complexity and opportunity for coding errors which are hard to detect.

How about we have a macro which creates a guarded stack allocation which is 
either using the stack when it is small enough or kvzalloc_objs() as fallback?

Well it's doable, and I did sketch something up. I just need to test it a bit more and then I'll send out an RFC. But for now, as an advance "warning", whether or not it has more or less chance of adding bugs, we will see once the review starts. :)

Point being it is a larger change since I started by converting most of drm_syncobj.c to scoped cleanup first. Otherwise it did not make sense to me to have a mix of allocations. With those prep patches done it was much easier to add a macro as you suggest. Anyway, stay tuned.

Tvrtko

Signed-off-by: Tvrtko Ursulin <[email protected]>
Reviewed-by: Maíra Canal <[email protected]> # v3
Cc: Fei Yu <[email protected]>
Cc: Christian König <[email protected]>
---
v2:
  * Document rationale for stack array in a comment.

v3:
  * Added DRM_SYNCOBJ_FAST_PATH_ENTRIES to avoid hardcoding fast path array
    size.

v4:
  * Rebased to be standalone.

v5:
  * Rebased for kzalloc_objs.

v6:
  * Rebased for kvmalloc_objs and kvzalloc_objs.
---
  drivers/gpu/drm/drm_syncobj.c | 44 ++++++++++++++++++++++++++---------
  1 file changed, 33 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c
index 663d6cb8dbf8..b76b957b1fff 100644
--- a/drivers/gpu/drm/drm_syncobj.c
+++ b/drivers/gpu/drm/drm_syncobj.c
@@ -248,6 +248,14 @@ static void
  syncobj_eventfd_entry_func(struct drm_syncobj *syncobj,
                           struct syncobj_eventfd_entry *entry);
+/*
+ * Empirically vast majority of ioctls pass in a single syncobj (96%) and never
+ * more than three points. Therefore implement a fast path with a small stack
+ * array to avoid going into the allocator sometimes several times per
+ * userspace rendered frame.
+ */
+#define DRM_SYNCOBJ_FAST_PATH_ENTRIES 4
+
  /**
   * drm_syncobj_find - lookup and reference a sync object.
   * @file_private: drm file private pointer
@@ -1051,10 +1059,12 @@ static signed long 
drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
                                                  uint32_t *idx,
                                                  ktime_t *deadline)
  {
+       struct syncobj_wait_entry stack_entries[DRM_SYNCOBJ_FAST_PATH_ENTRIES];
+       u64 stack_points[DRM_SYNCOBJ_FAST_PATH_ENTRIES];
        struct syncobj_wait_entry *entries;
        struct dma_fence *fence;
-       uint64_t *points;
        uint32_t signaled_count, i;
+       uint64_t *points;
if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
                     DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) {
@@ -1062,24 +1072,33 @@ static signed long 
drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
                lockdep_assert_none_held_once();
        }
- points = kvmalloc_objs(*points, count);
-       if (points == NULL)
-               return -ENOMEM;
+       if (count > ARRAY_SIZE(stack_points)) {
+               points = kvmalloc_objs(*points, count);
+               if (!points)
+                       return -ENOMEM;
+       } else {
+               points = stack_points;
+       }
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;
        }
- entries = kvzalloc_objs(*entries, count);
-       if (!entries) {
-               timeout = -ENOMEM;
-               goto err_free_points;
+       if (count > ARRAY_SIZE(stack_entries)) {
+               entries = kvzalloc_objs(*entries, count);
+               if (!entries) {
+                       timeout = -ENOMEM;
+                       goto err_free_points;
+               }
+       } else {
+               memset(stack_entries, 0, sizeof(stack_entries));
+               entries = stack_entries;
        }
+
        /* 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
@@ -1196,10 +1215,13 @@ static signed long 
drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
                                                  &entries[i].fence_cb);
                dma_fence_put(entries[i].fence);
        }
-       kvfree(entries);
+
+       if (entries != stack_entries)
+               kvfree(entries);
err_free_points:
-       kvfree(points);
+       if (points != stack_points)
+               kvfree(points);
return timeout;
  }


Reply via email to