A few drm_syncobj optimisations
Subsystems (2)
v2 cover A few drm_syncobj optimisations
A small set of drm_syncobj optimisations which should make things a tiny bit more efficient on the CPU side of things.
Improvement seems to be around 1.5%* more FPS if observed with "vkgears -present-mailbox" on a Steam Deck Plasma desktop, but I am reluctant to make a definitive claim on the numbers since there is some run to run variance. But, as suggested by Michel Dänzer, I did do a five ~100 second runs on the each kernel to be able to show the ministat analysis.
x before
+ after
+------------------------------------------------------------+
| x + |
| x x + |
| x xx ++++ |
| x x xx x ++++ |
| x xx x xx x+ ++++ |
| xxxxx xxxxxx+ ++++ + + |
| xxxxxxx xxxxxx+x ++++ +++ |
| x xxxxxxxxxxx*xx+* x++++++++ ++ |
| x x xxxxxxxxxxxx**x*+*+*++++++++ ++++ + |
| xx x xxxxxxxxxx*x****+***+**+++++ ++++++ |
|x xxx x xxxxx*x****x***********+*++**+++++++ + + +|
| |_______A______| |
| |______A_______| |
+------------------------------------------------------------+
N Min Max Median Avg Stddev
x 135 21697.58 22809.467 22321.396 22307.707 198.75011
+ 118 22200.746 23277.09 22661.4 22671.442 192.10609
Difference at 95.0% confidence
363.735 +/- 48.3345
1.63054% +/- 0.216672%
(Student's t, pooled s = 195.681)v2:
* Implemented review feedback - see patch change logs.
Tvrtko Ursulin (7):
drm/syncobj: Remove unhelpful helper
drm/syncobj: Do not allocate an array to store zeros when waiting drm/syncobj: Avoid one temporary allocation in drm_syncobj_array_find drm/syncobj: Use put_user in drm_syncobj_query_ioctl drm/syncobj: Avoid temporary allocation in
drm_syncobj_timeline_signal_ioctl
drm/syncobj: Add a fast path to drm_syncobj_array_wait_timeout drm/syncobj: Add a fast path to drm_syncobj_array_find
drivers/gpu/drm/drm_syncobj.c | 286 ++++++++++++++++++---------------- 1 file changed, 154 insertions(+), 132 deletions(-)
Signature · 1 line
2.48.0
PATCH v2 1/7 drm/syncobj: Remove unhelpful helper
Helper which fails to consolidate the code and instead just forks into two copies of the code based on a boolean parameter is not very helpful or readable. Lets just remove it and proof in the pudding is the net smaller code.
---
v2:
* Assign timeout to a local variable.
--- drivers/gpu/drm/drm_syncobj.c | 100 ++++++++++++++++------------------ 1 file changed, 46 insertions(+), 54 deletions(-)
drivers/gpu/drm/drm_syncobj.c +46 -54
@@ -1221,42 +1221,6 @@ signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec) }EXPORT_SYMBOL (drm_timeout_abs_to_jiffies); -static int drm_syncobj_array_wait (struct drm_device *dev, -struct drm_file *file_private, -struct drm_syncobj_wait *wait, -struct drm_syncobj_timeline_wait *timeline_wait, -struct drm_syncobj **syncobjs,bool timeline, -ktime_t *deadline) -{ -signed long timeout =0 ; -uint32_t first = ~0 ; - -if (!timeline) { - timeout =drm_timeout_abs_to_jiffies (wait->timeout_nsec); - timeout =drm_syncobj_array_wait_timeout (syncobjs, -NULL , - wait->count_handles, - wait->flags, - timeout, &first, - deadline); -if (timeout <0 ) -return timeout; - wait->first_signaled = first; - }else { - timeout =drm_timeout_abs_to_jiffies (timeline_wait->timeout_nsec); - timeout =drm_syncobj_array_wait_timeout (syncobjs, -u64_to_user_ptr (timeline_wait->points), - timeline_wait->count_handles, - timeline_wait->flags, - timeout, &first, - deadline); -if (timeout <0 ) -return timeout; - timeline_wait->first_signaled = first; - } -return 0 ; -} - static int drm_syncobj_array_find(struct drm_file *file_private, void __user *user_handles, uint32_t count_handles,
@@ -1319,9 +1283,12 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_wait *args = data; +ktime_t deadline, *pdeadline =NULL ; +u32 count = args->count_handles;struct drm_syncobj **syncobjs;unsigned int possible_flags; -ktime_t t, *tp =NULL ; +u32 first = ~0 ; +long timeout;int ret =0 ;if (!drm_core_check_feature (dev,DRIVER_SYNCOBJ ))
@@ -1334,27 +1301,38 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,if (args->flags & ~possible_flags)return -EINVAL ; -if (args->count_handles ==0 ) +if (count ==0 )return 0 ; ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), - args->count_handles, + count, &syncobjs);if (ret <0 )return ret;if (args->flags &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_DEADLINE ) { - t =ns_to_ktime (args->deadline_nsec); - tp = &t; + deadline =ns_to_ktime (args->deadline_nsec); + pdeadline = &deadline; } - ret =drm_syncobj_array_wait (dev, file_private, - args,NULL , syncobjs, false, tp); + timeout =drm_timeout_abs_to_jiffies (args->timeout_nsec); + timeout =drm_syncobj_array_wait_timeout (syncobjs, +NULL , + count, + args->flags, + timeout, + &first, + pdeadline); -drm_syncobj_array_free (syncobjs, args->count_handles); +drm_syncobj_array_free (syncobjs, count); -return ret; +if (timeout <0 ) +return timeout; + + args->first_signaled = first; + +return 0 ; }int
@@ -1362,9 +1340,12 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_wait *args = data; +ktime_t deadline, *pdeadline =NULL ; +u32 count = args->count_handles;struct drm_syncobj **syncobjs;unsigned int possible_flags; -ktime_t t, *tp =NULL ; +u32 first = ~0 ; +long timeout;int ret =0 ;if (!drm_core_check_feature (dev,DRIVER_SYNCOBJ_TIMELINE ))
@@ -1378,27 +1359,38 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data,if (args->flags & ~possible_flags)return -EINVAL ; -if (args->count_handles ==0 ) +if (count ==0 )return 0 ; ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), - args->count_handles, + count, &syncobjs);if (ret <0 )return ret;if (args->flags &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_DEADLINE ) { - t =ns_to_ktime (args->deadline_nsec); - tp = &t; + deadline =ns_to_ktime (args->deadline_nsec); + pdeadline = &deadline; } - ret =drm_syncobj_array_wait (dev, file_private, -NULL , args, syncobjs, true, tp); + timeout =drm_timeout_abs_to_jiffies (args->timeout_nsec); + timeout =drm_syncobj_array_wait_timeout (syncobjs, +u64_to_user_ptr (args->points), + count, + args->flags, + timeout, + &first, + pdeadline); -drm_syncobj_array_free (syncobjs, args->count_handles); +drm_syncobj_array_free (syncobjs, count); -return ret; +if (timeout <0 ) +return timeout; + + args->first_signaled = first; + +return 0 ; } static void syncobj_eventfd_entry_fence_func(struct dma_fence *fence,
Signature · 1 line
2.48.0
PATCH v2 2/7 drm/syncobj: Do not allocate an array to store zeros when waiting
In reply to Tvrtko UrsulinWhen waiting on syncobjs the current code allocates a temporary array only to fill it up with all zeros.
We can avoid that by relying on the allocated entry array already being zero allocated.
For the timeline mode we can fetch the timeline point values as we populate the entries array so also do not need this additional temporary allocation.
--- drivers/gpu/drm/drm_syncobj.c | 39 ++++++++++++++--------------------- 1 file changed, 15 insertions(+), 24 deletions(-)
drivers/gpu/drm/drm_syncobj.c +15 -24
@@ -1028,7 +1028,7 @@ static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj, } static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, - void __user *user_points, + u64 __user *user_points, uint32_t count, uint32_t flags, signed long timeout,
@@ -1036,9 +1036,8 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, ktime_t *deadline) {struct syncobj_wait_entry *entries; -struct dma_fence *fence; -uint64_t *points;uint32_t signaled_count, i; +struct dma_fence *fence;if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE )) {
@@ -1046,24 +1045,14 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,lockdep_assert_none_held_once (); } - points =kmalloc_array (count,sizeof (*points),GFP_KERNEL ); -if (points ==NULL ) -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 && + !access_ok (user_points, count *sizeof (*user_points))) +return -EFAULT ; entries =kcalloc (count,sizeof (*entries),GFP_KERNEL ); -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
@@ -1074,9 +1063,14 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,struct dma_fence *fence; entries[i].task = current; - entries[i].point = points[i]; +if (user_points && +__get_user (entries[i].point, user_points++)) { + timeout = -EFAULT ; + goto cleanup_entries; + } fence =drm_syncobj_fence_get (syncobjs[i]); -if (!fence ||dma_fence_chain_find_seqno (&fence, points[i])) { +if (!fence || +dma_fence_chain_find_seqno (&fence, entries[i].point)) {dma_fence_put (fence);if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE )) {
@@ -1182,9 +1176,6 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, }kfree (entries); -err_free_points: -kfree (points); -return timeout; }
Signature · 1 line
2.48.0
PATCH v2 3/7 drm/syncobj: Avoid one temporary allocation in drm_syncobj_array_find
In reply to Tvrtko UrsulinDrm_syncobj_array_find() helper is used from many userspace ioctl entry points with the task of looking up userspace handles to internal objects.
We can easily avoid one temporary allocation by making it read the handles as it is looking them up.
---
v2:
* Fix handle type.
* Undo pointless unwind change.
--- drivers/gpu/drm/drm_syncobj.c | 36 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 21 deletions(-)
drivers/gpu/drm/drm_syncobj.c +15 -21
@@ -1213,39 +1213,35 @@ signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)EXPORT_SYMBOL (drm_timeout_abs_to_jiffies);static int drm_syncobj_array_find (struct drm_file *file_private, -void __user *user_handles, -uint32_t count_handles, +u32 __user *handles, +uint32_t count,struct drm_syncobj ***syncobjs_out) { -uint32_t i, *handles;struct drm_syncobj **syncobjs; +uint32_t i;int ret; - handles =kmalloc_array (count_handles,sizeof (*handles),GFP_KERNEL ); -if (handles ==NULL ) +if (!access_ok (handles, count *sizeof (*handles))) +return -EFAULT ; + + syncobjs =kmalloc_array (count,sizeof (*syncobjs),GFP_KERNEL ); +if (!syncobjs)return -ENOMEM ; -if (copy_from_user (handles, user_handles, -sizeof (uint32_t ) * count_handles)) { - ret = -EFAULT ; - goto err_free_handles; - } +for (i =0 ; i < count; i++) { +u32 handle; - syncobjs =kmalloc_array (count_handles,sizeof (*syncobjs),GFP_KERNEL ); -if (syncobjs ==NULL ) { - ret = -ENOMEM ; - goto err_free_handles; - } - -for (i =0 ; i < count_handles; i++) { - syncobjs[i] =drm_syncobj_find (file_private, handles[i]); +if (__get_user (handle, handles++)) { + ret = -EFAULT ; + goto err_put_syncobjs; + } + syncobjs[i] =drm_syncobj_find (file_private, handle);if (!syncobjs[i]) { ret = -ENOENT ; goto err_put_syncobjs; } } -kfree (handles); *syncobjs_out = syncobjs;return 0 ;
@@ -1253,8 +1249,6 @@ static int drm_syncobj_array_find(struct drm_file *file_private,while (i-- >0 )drm_syncobj_put (syncobjs[i]);kfree (syncobjs); -err_free_handles: -kfree (handles);return ret; }
Signature · 1 line
2.48.0
Re: [PATCH v2 3/7] drm/syncobj: Avoid one temporary allocation in drm_syncobj_array_find
Hi Tvrtko,
Quoted 7 lines · Tvrtko Ursulin
Drm_syncobj_array_find()helper is used from many userspace ioctl entry points with the task of looking up userspace handles to internal objects.We can easily avoid one temporary allocation by making it read the handles as it is looking them up.
Signed-off-by Tvrtko Ursulin <[email protected]>
Best Regards,
- Maíra
Quoted 76 lines
---
v2:
* Fix handle type.
* Undo pointless unwind change.--- drivers/gpu/drm/drm_syncobj.c | 36 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 21 deletions(-)drivers/gpu/drm/drm_syncobj.c +15
-21@@ -1213,39 +1213,35 @@ signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)EXPORT_SYMBOL (drm_timeout_abs_to_jiffies);static int drm_syncobj_array_find (struct drm_file *file_private, -void __user *user_handles, -uint32_t count_handles, +u32 __user *handles, +uint32_t count,struct drm_syncobj ***syncobjs_out) { -uint32_t i, *handles;struct drm_syncobj **syncobjs; +uint32_t i;int ret; - handles =kmalloc_array (count_handles,sizeof (*handles),GFP_KERNEL ); -if (handles ==NULL ) +if (!access_ok (handles, count *sizeof (*handles))) +return -EFAULT ; + + syncobjs =kmalloc_array (count,sizeof (*syncobjs),GFP_KERNEL ); +if (!syncobjs)return -ENOMEM ; -if (copy_from_user (handles, user_handles, -sizeof (uint32_t ) * count_handles)) { - ret = -EFAULT ; - goto err_free_handles; - } +for (i =0 ; i < count; i++) { +u32 handle; - syncobjs =kmalloc_array (count_handles,sizeof (*syncobjs),GFP_KERNEL ); -if (syncobjs ==NULL ) { - ret = -ENOMEM ; - goto err_free_handles; - } - -for (i =0 ; i < count_handles; i++) { - syncobjs[i] =drm_syncobj_find (file_private, handles[i]); +if (__get_user (handle, handles++)) { + ret = -EFAULT ; + goto err_put_syncobjs; + } + syncobjs[i] =drm_syncobj_find (file_private, handle);if (!syncobjs[i]) { ret = -ENOENT ; goto err_put_syncobjs; } } -kfree (handles); *syncobjs_out = syncobjs;return 0 ;@@ -1253,8 +1249,6 @@ static int drm_syncobj_array_find(struct drm_file *file_private,while (i-- >0 )drm_syncobj_put (syncobjs[i]);kfree (syncobjs); -err_free_handles: -kfree (handles);return ret; }
PATCH v2 4/7 drm/syncobj: Use put_user in drm_syncobj_query_ioctl
In reply to Tvrtko UrsulinSince the query loop is using copy_to_user() to write out a single u64 at a time it feels more natural (and is a tiny bit more compact) to replace it with put_user().
Access_ok() check is added to the input checking for an early bailout in case of a bad buffer passed in.
--- drivers/gpu/drm/drm_syncobj.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_syncobj.c +6 -3
@@ -1641,6 +1641,9 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,if (args->count_handles ==0 )return -EINVAL ; +if (!access_ok (points, args->count_handles *sizeof (*points))) +return -EFAULT ; + ret = drm_syncobj_array_find(file_private, u64_to_user_ptr(args->handles), args->count_handles,
@@ -1682,10 +1685,10 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data, point =0 ; }dma_fence_put (fence); - ret =copy_to_user (&points[i], &point,sizeof (uint64_t )); - ret = ret ? -EFAULT :0 ; -if (ret) +if (__put_user (point, points++)) { + ret = -EFAULT ;break ; + } }drm_syncobj_array_free (syncobjs, args->count_handles);
Signature · 1 line
2.48.0
PATCH v2 5/7 drm/syncobj: Avoid temporary allocation in drm_syncobj_timeline_signal_ioctl
In reply to Tvrtko UrsulinWe can avoid one of the two temporary allocations if we read the userspace supplied timeline points as we go along.
The only new complication is to unwind unused fence chains on the error path, but even that code was already present in the function.
--- drivers/gpu/drm/drm_syncobj.c | 46 +++++++++++++++-------------------- 1 file changed, 20 insertions(+), 26 deletions(-)
drivers/gpu/drm/drm_syncobj.c +20 -26
@@ -1555,10 +1555,10 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_array *args = data; +uint64_t __user *points =u64_to_user_ptr (args->points); +uint32_t i, j, count = args->count_handles;struct drm_syncobj **syncobjs;struct dma_fence_chain **chains; -uint64_t *points; -uint32_t i, j;int ret;if (!drm_core_check_feature (dev,DRIVER_SYNCOBJ_TIMELINE ))
@@ -1570,33 +1570,22 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,if (args->count_handles ==0 )return -EINVAL ; +if (!access_ok (points, count *sizeof (*points))) +return -EFAULT ; + ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), - args->count_handles, + count, &syncobjs);if (ret <0 )return ret; - points =kmalloc_array (args->count_handles,sizeof (*points), -GFP_KERNEL ); -if (!points) { - ret = -ENOMEM ; - goto out; - } -if (!u64_to_user_ptr (args->points)) { -memset (points,0 , args->count_handles *sizeof (uint64_t )); - }else if (copy_from_user (points,u64_to_user_ptr (args->points), -sizeof (uint64_t ) * args->count_handles)) { - ret = -EFAULT ; - goto err_points; - } - - chains =kmalloc_array (args->count_handles,sizeof (void *),GFP_KERNEL ); + chains =kmalloc_array (count,sizeof (void *),GFP_KERNEL );if (!chains) { ret = -ENOMEM ; - goto err_points; + goto out; } -for (i =0 ; i < args->count_handles; i++) { +for (i =0 ; i < count; i++) { chains[i] =dma_fence_chain_alloc ();if (!chains[i]) {for (j =0 ; j < i; j++)
@@ -1606,19 +1595,24 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, } } -for (i =0 ; i < args->count_handles; i++) { +for (i =0 ; i < count; i++) {struct dma_fence *fence =dma_fence_get_stub (); +u64 point =0 ; -drm_syncobj_add_point (syncobjs[i], chains[i], - fence, points[i]); +if (points &&__get_user (point, points++)) { + ret = -EFAULT ; +for (j = i; j < count; j++) +dma_fence_chain_free (chains[j]); + goto err_chains; + } + +drm_syncobj_add_point (syncobjs[i], chains[i], fence, point);dma_fence_put (fence); } err_chains:kfree (chains); -err_points: -kfree (points); out: -drm_syncobj_array_free (syncobjs, args->count_handles); +drm_syncobj_array_free (syncobjs, count);return ret; }
Signature · 1 line
2.48.0
PATCH v2 6/7 drm/syncobj: Add a fast path to drm_syncobj_array_wait_timeout
In reply to Tvrtko UrsulinRunning 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.
---
v2:
* Document rationale for stack array in a comment.
--- drivers/gpu/drm/drm_syncobj.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_syncobj.c +17 -4
@@ -1035,6 +1035,12 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, uint32_t *idx, ktime_t *deadline) { +/* +* Empirically vast majority of calls here works with just a single +* point (96%) and never more than three points. Therefore a small stack +* array can cheaply avoid multiple per frame allocations. +*/ +struct syncobj_wait_entry stack_entries[4 ];struct syncobj_wait_entry *entries;uint32_t signaled_count, i;struct dma_fence *fence;
@@ -1049,9 +1055,14 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, !access_ok(user_points, count * sizeof(*user_points)))return -EFAULT ; - entries =kcalloc (count,sizeof (*entries),GFP_KERNEL ); -if (!entries) -return -ENOMEM ; +if (count >ARRAY_SIZE (stack_entries)) { + entries =kcalloc (count,sizeof (*entries),GFP_KERNEL ); +if (!entries) +return -ENOMEM ; + }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
@@ -1174,7 +1185,9 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, &entries[i].fence_cb);dma_fence_put (entries[i].fence); } -kfree (entries); + +if (entries != stack_entries) +kfree (entries);return timeout; }
Signature · 1 line
2.48.0
PATCH v2 7/7 drm/syncobj: Add a fast path to drm_syncobj_array_find
In reply to Tvrtko UrsulinRunning the Cyberpunk 2077 benchmark we can observe that the lookup helper is relatively hot, but the 97% of the calls are for a single object. (~3% 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 lookups.)
Therefore lets add a fast path to bypass the kmalloc_array/kfree and use a pre-allocated stack array for those cases.
---
v2:
* Added comments describing how the fast path arrays were sized.
* Make container freeing criteria clearer by using a boolean.
--- drivers/gpu/drm/drm_syncobj.c | 71 ++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 18 deletions(-)
drivers/gpu/drm/drm_syncobj.c +53 -18
@@ -236,6 +236,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
@@ -1035,12 +1043,7 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,uint32_t *idx, ktime_t *deadline) { -/* -* Empirically vast majority of calls here works with just a single -* point (96%) and never more than three points. Therefore a small stack -* array can cheaply avoid multiple per frame allocations. -*/ -struct syncobj_wait_entry stack_entries[4 ]; + struct syncobj_wait_entry stack_entries[DRM_SYNCOBJ_FAST_PATH_ENTRIES];struct syncobj_wait_entry *entries;uint32_t signaled_count, i;struct dma_fence *fence;
@@ -1228,6 +1231,8 @@ EXPORT_SYMBOL(drm_timeout_abs_to_jiffies);static int drm_syncobj_array_find (struct drm_file *file_private,u32 __user *handles,uint32_t count, +struct drm_syncobj **stack_syncobjs, +u32 stack_count,struct drm_syncobj ***syncobjs_out) {struct drm_syncobj **syncobjs;
@@ -1237,9 +1242,13 @@ static int drm_syncobj_array_find(struct drm_file *file_private,if (!access_ok (handles, count *sizeof (*handles)))return -EFAULT ; - syncobjs =kmalloc_array (count,sizeof (*syncobjs),GFP_KERNEL ); -if (!syncobjs) -return -ENOMEM ; +if (count > stack_count) { + syncobjs =kmalloc_array (count,sizeof (*syncobjs),GFP_KERNEL ); +if (!syncobjs) +return -ENOMEM ; + }else { + syncobjs = stack_syncobjs; + }for (i =0 ; i < count; i++) {u32 handle;
@@ -1261,25 +1270,31 @@ static int drm_syncobj_array_find(struct drm_file *file_private, err_put_syncobjs:while (i-- >0 )drm_syncobj_put (syncobjs[i]); -kfree (syncobjs); + +if (syncobjs != stack_syncobjs) +kfree (syncobjs);return ret; }static void drm_syncobj_array_free (struct drm_syncobj **syncobjs, -uint32_t count) +uint32_t count, +bool free_container) {uint32_t i;for (i =0 ; i < count; i++)drm_syncobj_put (syncobjs[i]); -kfree (syncobjs); + +if (free_container) +kfree (syncobjs); }int drm_syncobj_wait_ioctl (struct drm_device *dev,void *data,struct drm_file *file_private) { +struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES ];struct drm_syncobj_wait *args = data;ktime_t deadline, *pdeadline =NULL ;u32 count = args->count_handles;
@@ -1305,6 +1320,8 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), count, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;
@@ -1323,7 +1340,7 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, &first, pdeadline); -drm_syncobj_array_free (syncobjs, count); +drm_syncobj_array_free (syncobjs, count, syncobjs != stack_syncobjs);if (timeout <0 )return timeout;
@@ -1337,6 +1354,7 @@ int drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES];struct drm_syncobj_timeline_wait *args = data;ktime_t deadline, *pdeadline =NULL ;u32 count = args->count_handles;
@@ -1363,6 +1381,8 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), count, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;
@@ -1381,7 +1401,7 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, &first, pdeadline); -drm_syncobj_array_free (syncobjs, count); +drm_syncobj_array_free (syncobjs, count, syncobjs != stack_syncobjs);if (timeout <0 )return timeout;
@@ -1498,6 +1518,7 @@ int drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES];struct drm_syncobj_array *args = data;struct drm_syncobj **syncobjs;uint32_t i;
@@ -1515,6 +1536,8 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), args->count_handles, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;
@@ -1522,7 +1545,8 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,for (i =0 ; i < args->count_handles; i++)drm_syncobj_replace_fence (syncobjs[i],NULL ); -drm_syncobj_array_free (syncobjs, args->count_handles); +drm_syncobj_array_free (syncobjs, args->count_handles, + syncobjs != stack_syncobjs);return 0 ; }
@@ -1531,6 +1555,7 @@ int drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES];struct drm_syncobj_array *args = data;struct drm_syncobj **syncobjs;uint32_t i;
@@ -1548,6 +1573,8 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), args->count_handles, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;
@@ -1558,7 +1585,8 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,break ; } -drm_syncobj_array_free (syncobjs, args->count_handles); +drm_syncobj_array_free (syncobjs, args->count_handles, + syncobjs != stack_syncobjs);return ret; }
@@ -1567,6 +1595,7 @@ int drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES];struct drm_syncobj_timeline_array *args = data;uint64_t __user *points =u64_to_user_ptr (args->points);uint32_t i, j, count = args->count_handles;
@@ -1589,6 +1618,8 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), count, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;
@@ -1625,7 +1656,7 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, err_chains:kfree (chains); out: -drm_syncobj_array_free (syncobjs, count); +drm_syncobj_array_free (syncobjs, count, syncobjs != stack_syncobjs);return ret; }
@@ -1633,6 +1664,7 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,int drm_syncobj_query_ioctl (struct drm_device *dev,void *data,struct drm_file *file_private) { +struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES ];struct drm_syncobj_timeline_array *args = data;struct drm_syncobj **syncobjs;uint64_t __user *points =u64_to_user_ptr (args->points);
@@ -1654,6 +1686,8 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), args->count_handles, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;
@@ -1697,7 +1731,8 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,break ; } } -drm_syncobj_array_free (syncobjs, args->count_handles); +drm_syncobj_array_free (syncobjs, args->count_handles, + syncobjs != stack_syncobjs);return ret; }
Signature · 1 line
2.48.0
Re: [PATCH v2 7/7] drm/syncobj: Add a fast path to drm_syncobj_array_find
Hi Tvrtko,
Quoted 39 lines · Tvrtko Ursulin
Running the Cyberpunk 2077 benchmark we can observe that the lookup helper is relatively hot, but the 97% of the calls are for a single object. (~3% 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 lookups.)
Therefore lets add a fast path to bypass the kmalloc_array/kfree and use a pre-allocated stack array for those cases.
Signed-off-by Tvrtko Ursulin <[email protected]>Reviewed-by Maíra Canal <[email protected]>---
v2:
* Added comments describing how the fast path arrays were sized.
* Make container freeing criteria clearer by using a boolean.--- drivers/gpu/drm/drm_syncobj.c | 71 ++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 18 deletions(-)drivers/gpu/drm/drm_syncobj.c +8
-0@@ -236,6 +236,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
drivers/gpu/drm/drm_syncobj.c static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, · line 1046
Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +1
-6@@ -1035,12 +1043,7 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,uint32_t *idx, ktime_t *deadline) { -/* -* Empirically vast majority of calls here works with just a single -* point (96%) and never more than three points. Therefore a small stack -* array can cheaply avoid multiple per frame allocations. -*/ -struct syncobj_wait_entry stack_entries[4 ]; + struct syncobj_wait_entry stack_entries[DRM_SYNCOBJ_FAST_PATH_ENTRIES];
Could you introduce this change in 6/7 to avoid changing the lines you introduced earlier?
Best Regards,
- Maíra
Quoted 215 lines
drivers/gpu/drm/drm_syncobj.c +44
-12struct syncobj_wait_entry *entries;uint32_t signaled_count, i;struct dma_fence *fence;@@ -1228,6 +1231,8 @@ EXPORT_SYMBOL(drm_timeout_abs_to_jiffies);static int drm_syncobj_array_find (struct drm_file *file_private,u32 __user *handles,uint32_t count, +struct drm_syncobj **stack_syncobjs, +u32 stack_count,struct drm_syncobj ***syncobjs_out) {struct drm_syncobj **syncobjs;@@ -1237,9 +1242,13 @@ static int drm_syncobj_array_find(struct drm_file *file_private,if (!access_ok (handles, count *sizeof (*handles)))return -EFAULT ; - syncobjs =kmalloc_array (count,sizeof (*syncobjs),GFP_KERNEL ); -if (!syncobjs) -return -ENOMEM ; +if (count > stack_count) { + syncobjs =kmalloc_array (count,sizeof (*syncobjs),GFP_KERNEL ); +if (!syncobjs) +return -ENOMEM ; + }else { + syncobjs = stack_syncobjs; + }for (i =0 ; i < count; i++) {u32 handle;@@ -1261,25 +1270,31 @@ static int drm_syncobj_array_find(struct drm_file *file_private, err_put_syncobjs:while (i-- >0 )drm_syncobj_put (syncobjs[i]); -kfree (syncobjs); + +if (syncobjs != stack_syncobjs) +kfree (syncobjs);return ret; }static void drm_syncobj_array_free (struct drm_syncobj **syncobjs, -uint32_t count) +uint32_t count, +bool free_container) {uint32_t i;for (i =0 ; i < count; i++)drm_syncobj_put (syncobjs[i]); -kfree (syncobjs); + +if (free_container) +kfree (syncobjs); }int drm_syncobj_wait_ioctl (struct drm_device *dev,void *data,struct drm_file *file_private) { +struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES ];struct drm_syncobj_wait *args = data;ktime_t deadline, *pdeadline =NULL ;u32 count = args->count_handles;@@ -1305,6 +1320,8 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), count, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;@@ -1323,7 +1340,7 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, &first, pdeadline); -drm_syncobj_array_free (syncobjs, count); +drm_syncobj_array_free (syncobjs, count, syncobjs != stack_syncobjs);if (timeout <0 )return timeout;@@ -1337,6 +1354,7 @@ int drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES];struct drm_syncobj_timeline_wait *args = data;ktime_t deadline, *pdeadline =NULL ;u32 count = args->count_handles;@@ -1363,6 +1381,8 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), count, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;@@ -1381,7 +1401,7 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, &first, pdeadline); -drm_syncobj_array_free (syncobjs, count); +drm_syncobj_array_free (syncobjs, count, syncobjs != stack_syncobjs);if (timeout <0 )return timeout;@@ -1498,6 +1518,7 @@ int drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES];struct drm_syncobj_array *args = data;struct drm_syncobj **syncobjs;uint32_t i;@@ -1515,6 +1536,8 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), args->count_handles, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;@@ -1522,7 +1545,8 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,for (i =0 ; i < args->count_handles; i++)drm_syncobj_replace_fence (syncobjs[i],NULL ); -drm_syncobj_array_free (syncobjs, args->count_handles); +drm_syncobj_array_free (syncobjs, args->count_handles, + syncobjs != stack_syncobjs);return 0 ; }@@ -1531,6 +1555,7 @@ int drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES];struct drm_syncobj_array *args = data;struct drm_syncobj **syncobjs;uint32_t i;@@ -1548,6 +1573,8 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), args->count_handles, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;@@ -1558,7 +1585,8 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,break ; } -drm_syncobj_array_free (syncobjs, args->count_handles); +drm_syncobj_array_free (syncobjs, args->count_handles, + syncobjs != stack_syncobjs);return ret; }@@ -1567,6 +1595,7 @@ int drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) { + struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES];struct drm_syncobj_timeline_array *args = data;uint64_t __user *points =u64_to_user_ptr (args->points);uint32_t i, j, count = args->count_handles;@@ -1589,6 +1618,8 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), count, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;@@ -1625,7 +1656,7 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, err_chains:kfree (chains); out: -drm_syncobj_array_free (syncobjs, count); +drm_syncobj_array_free (syncobjs, count, syncobjs != stack_syncobjs);return ret; }@@ -1633,6 +1664,7 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,int drm_syncobj_query_ioctl (struct drm_device *dev,void *data,struct drm_file *file_private) { +struct drm_syncobj *stack_syncobjs[DRM_SYNCOBJ_FAST_PATH_ENTRIES ];struct drm_syncobj_timeline_array *args = data;struct drm_syncobj **syncobjs;uint64_t __user *points =u64_to_user_ptr (args->points);@@ -1654,6 +1686,8 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), args->count_handles, + stack_syncobjs, +ARRAY_SIZE (stack_syncobjs), &syncobjs);if (ret <0 )return ret;@@ -1697,7 +1731,8 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,break ; } } -drm_syncobj_array_free (syncobjs, args->count_handles); +drm_syncobj_array_free (syncobjs, args->count_handles, + syncobjs != stack_syncobjs);return ret; }