DRM syncobj scoped cleanup and fast path
Subsystems (2)
cover DRM syncobj scoped cleanup and fast path
A different flavour of:
https://lore.kernel.org/dri-devel/[email protected]/
Where Christian suggested to try with scope based cleanup macros. This is that attempt.
We start with converting all the functions which use temporary arrays to scoped cleanup and then in the last patch fast path is added via two handy macros.
Syncobj IGT pass, light gaming and light desktop as well, and there are no kmemleak reports. Nevertheless this version is a bit more invasive so if the approach is looking good a careful review will be required.
Tvrtko Ursulin (7):
drm/syncobj: Use vmemdup_array_user in drm_syncobj_array_find drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout
points
drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout
syncobjs
drm/syncobj: Use scoped cleanup for drm_syncobj_timeline_signal_ioctl
points
drm/syncobj: Use scoped cleanup for drm_syncobj_timeline_signal_ioctl
chains
drm/syncobj: Use scoped cleanup in drm_syncobj_array_find and callers drm/syncobj: Add temporary array allocation fast paths
drivers/gpu/drm/drm_syncobj.c | 384 ++++++++++++++++++++-------------- 1 file changed, 224 insertions(+), 160 deletions(-)
Signature · 1 line
2.56.0
PATCH 1/7 drm/syncobj: Use vmemdup_array_user in drm_syncobj_array_find
Using a helper shrinks the code and separating the user and kernel slabs is more secure.
--- drivers/gpu/drm/drm_syncobj.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_syncobj.c +4 -9
@@ -1252,15 +1252,10 @@ static int drm_syncobj_array_find(struct drm_file *file_private,if (count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; - handles =kvmalloc_objs (*handles, count_handles); -if (handles ==NULL ) -return -ENOMEM ; - -if (copy_from_user (handles, user_handles, -sizeof (uint32_t ) * count_handles)) { - ret = -EFAULT ; - goto err_free_handles; - } + handles =vmemdup_array_user (user_handles, count_handles, +sizeof (*handles)); +if (IS_ERR (handles)) +return PTR_ERR (handles); syncobjs =kvmalloc_objs (*syncobjs, count_handles);if (syncobjs ==NULL ) {
Signature · 1 line
2.56.0
Re: [PATCH 1/7] drm/syncobj: Use vmemdup_array_user in drm_syncobj_array_find
Quoted 6 lines · Tvrtko Ursulin
Using a helper shrinks the code and separating the user and kernel slabs is more secure.
Signed-off-by Tvrtko Ursulin <[email protected]>Cc Fei Yu <[email protected]>Cc Christian König <[email protected]>
Quoted 28 lines
--- drivers/gpu/drm/drm_syncobj.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)drivers/gpu/drm/drm_syncobj.c +4
-9@@ -1252,15 +1252,10 @@ static int drm_syncobj_array_find(struct drm_file *file_private,if (count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; - handles =kvmalloc_objs (*handles, count_handles); -if (handles ==NULL ) -return -ENOMEM ; - -if (copy_from_user (handles, user_handles, -sizeof (uint32_t ) * count_handles)) { - ret = -EFAULT ; - goto err_free_handles; - } + handles =vmemdup_array_user (user_handles, count_handles, +sizeof (*handles)); +if (IS_ERR (handles)) +return PTR_ERR (handles); syncobjs =kvmalloc_objs (*syncobjs, count_handles);if (syncobjs ==NULL ) {
PATCH 2/7 drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout points
In reply to Tvrtko UrsulinUse 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.
--- drivers/gpu/drm/drm_syncobj.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-)
drivers/gpu/drm/drm_syncobj.c +10 -17
@@ -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 ; 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; }
Signature · 1 line
2.56.0
Re: [PATCH 2/7] drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout points
Quoted 47 lines · Tvrtko Ursulin
Use scoped cleanup for
drm_syncobj_array_wait_timeoutpoints 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(-)drivers/gpu/drm/drm_syncobj.c +3
-4@@ -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 ));
drivers/gpu/drm/drm_syncobj.c static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, · line 1073
Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +4
-6- - }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.
Regards,
Christian.
Quoted 22 lines
drivers/gpu/drm/drm_syncobj.c +3
-7entries =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; }
Re: [PATCH 2/7] drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout points
Quoted 61 lines · Christian König
Quoted 47 lines · Tvrtko Ursulin
Use scoped cleanup for
drm_syncobj_array_wait_timeoutpoints 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(-)drivers/gpu/drm/drm_syncobj.c +3
-4@@ -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 ));drivers/gpu/drm/drm_syncobj.c static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, · line 1073
Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +4
-6- - }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_pointsis not given.
Quoted 1 line
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
Quoted 23 lines
Quoted 22 lines
drivers/gpu/drm/drm_syncobj.c +3
-7entries =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; }
PATCH 3/7 drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout syncobjs
In reply to Tvrtko UrsulinUse 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.
--- drivers/gpu/drm/drm_syncobj.c | 61 +++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 21 deletions(-)
drivers/gpu/drm/drm_syncobj.c +40 -21
@@ -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; }
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH 3/7] drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout syncobjs
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/syncobj: separate declaration and init of __free() in wait_timeout --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/syncobj: separate declaration and init of __free() in wait_timeout
--
commit 7b5691e728edfdbf53ebabd68fe3be6523faee57
Author: Tvrtko Ursulin <[email protected]>
drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout syncobjs
This commit converts the entries allocation and syncobj wait cleanup in drm_syncobj_array_wait_timeout() to use a DEFINE_FREE() scope-managed cleanup helper instead of goto labels.
Quoted 4 lines
drivers/gpu/drm/drm_syncobj.c +0
-0
[ ... ]
Quoted 9 lines
drivers/gpu/drm/drm_syncobj.c +1
-0@@ -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;
drivers/gpu/drm/drm_syncobj.c static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, · line 1108
Quoted 7 lines
drivers/gpu/drm/drm_syncobj.c +4
-0@@ -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;
[Severity: Low]
Could cleanup be defined and initialized in a single statement right after entries is allocated rather than declared empty at the top of the function? Per include/linux/cleanup.h conventions, defining __free() variables at their point of initialization preserves LIFO destruction order relative to points and avoids running syncobj_wait_entries_free() on early returns before entries is allocated.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/7] drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout syncobjs
In reply to Tvrtko UrsulinQuoted 51 lines · Tvrtko Ursulin
Use scoped cleanup for
drm_syncobj_array_wait_timeoutentries 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(-)drivers/gpu/drm/drm_syncobj.c +25
-0@@ -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,
drivers/gpu/drm/drm_syncobj.c static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, · line 1080
Quoted 4 lines
drivers/gpu/drm/drm_syncobj.c +1
-0@@ -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) = {};
Oh that looks ugly, I would seriously not do it this way.
Replacing the extra kvfree() is ok put I would keep the ref-counting cleanup in the same function.
Regards,
Christian.
Quoted 86 lines
drivers/gpu/drm/drm_syncobj.c +14
-21struct 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; }
Re: [PATCH 3/7] drm/syncobj: Use scoped cleanup for drm_syncobj_array_wait_timeout syncobjs
Quoted 59 lines · Christian König
Quoted 51 lines · Tvrtko Ursulin
Use scoped cleanup for
drm_syncobj_array_wait_timeoutentries 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(-)drivers/gpu/drm/drm_syncobj.c +25
-0@@ -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,drivers/gpu/drm/drm_syncobj.c static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, · line 1080
Quoted 4 lines
drivers/gpu/drm/drm_syncobj.c +1
-0@@ -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) = {};Oh that looks ugly, I would seriously not do it this way.
Quoted 1 line
Replacing the extra
kvfree()is ok put I would keep the ref-counting cleanup in the same function.
I assume you meant s/put/but/?
My main counter argument here will be that having a mixture of scope based cleanup and goto's would also not be great. Hence I opted to just convert it all.
Which part is for you the ugly one?
Regards,
Tvrtko
Quoted 87 lines
Quoted 86 lines
drivers/gpu/drm/drm_syncobj.c +14
-21struct 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; }
PATCH 4/7 drm/syncobj: Use scoped cleanup for drm_syncobj_timeline_signal_ioctl points
In reply to Tvrtko UrsulinUse scoped cleanup for drm_syncobj_timeline_signal_ioctl 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.
--- drivers/gpu/drm/drm_syncobj.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/gpu/drm/drm_syncobj.c +5 -7
@@ -1571,9 +1571,9 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_array *args = data; + u64 *points __free(kvfree) = NULL;struct drm_syncobj **syncobjs;struct dma_fence_chain **chains; -uint64_t *points;uint32_t i, j;int ret;
@@ -1599,17 +1599,17 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, goto out; }if (!u64_to_user_ptr (args->points)) { -memset (points,0 , args->count_handles *sizeof (uint64_t )); +memset (points,0 , args->count_handles *sizeof (u64)); }else if (copy_from_user (points,u64_to_user_ptr (args->points), -sizeof (uint64_t ) * args->count_handles)) { +sizeof (u64) * args->count_handles)) { ret = -EFAULT ; - goto err_points; + goto out; } chains =kvmalloc_objs (*chains, args->count_handles);if (!chains) { ret = -ENOMEM ; - goto err_points; + goto out; }for (i =0 ; i < args->count_handles; i++) { chains[i] =dma_fence_chain_alloc ();
@@ -1630,8 +1630,6 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, } err_chains:kvfree (chains); -err_points: -kvfree (points); out:drm_syncobj_array_free (syncobjs, args->count_handles);
Signature · 1 line
2.56.0
Re: [PATCH 4/7] drm/syncobj: Use scoped cleanup for drm_syncobj_timeline_signal_ioctl points
Quoted 31 lines · Tvrtko Ursulin
Use scoped cleanup for
drm_syncobj_timeline_signal_ioctlpoints 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 | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)drivers/gpu/drm/drm_syncobj.c +1
-1@@ -1571,9 +1571,9 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_array *args = data; + u64 *points __free(kvfree) = NULL;struct drm_syncobj **syncobjs;struct dma_fence_chain **chains; -uint64_t *points;uint32_t i, j;int ret;
drivers/gpu/drm/drm_syncobj.c drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, · line 1604
Quoted 8 lines
drivers/gpu/drm/drm_syncobj.c +2
-2@@ -1599,17 +1599,17 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, goto out; }if (!u64_to_user_ptr (args->points)) { -memset (points,0 , args->count_handles *sizeof (uint64_t )); +memset (points,0 , args->count_handles *sizeof (u64)); }else if (copy_from_user (points,u64_to_user_ptr (args->points), -sizeof (uint64_t ) * args->count_handles)) { +sizeof (u64) * args->count_handles)) {
Yeah same comment as for the last patch, the approach of allocating points even when it isn't used is rather questionable.
Regards,
Christian.
Quoted 22 lines
drivers/gpu/drm/drm_syncobj.c +2
-4ret = -EFAULT ; - goto err_points; + goto out; } chains =kvmalloc_objs (*chains, args->count_handles);if (!chains) { ret = -ENOMEM ; - goto err_points; + goto out; }for (i =0 ; i < args->count_handles; i++) { chains[i] =dma_fence_chain_alloc ();@@ -1630,8 +1630,6 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, } err_chains:kvfree (chains); -err_points: -kvfree (points); out:drm_syncobj_array_free (syncobjs, args->count_handles);
PATCH 5/7 drm/syncobj: Use scoped cleanup for drm_syncobj_timeline_signal_ioctl chains
In reply to Tvrtko UrsulinUse 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.
--- drivers/gpu/drm/drm_syncobj.c | 41 +++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 12 deletions(-)
drivers/gpu/drm/drm_syncobj.c +29 -12
@@ -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);
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH 5/7] drm/syncobj: Use scoped cleanup for drm_syncobj_timeline_signal_ioctl chains
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/syncobj: mixed goto and __free() in drm_syncobj_timeline_signal_ioctl() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/syncobj: mixed goto and __free() in drm_syncobj_timeline_signal_ioctl()
--
commit 97e96ebc6a98c85d2c6da0c4d034fb53f985a940
Author: Tvrtko Ursulin <[email protected]>
drm/syncobj: Use scoped cleanup for drm_syncobj_timeline_signal_ioctl chains
This commit introduces a syncobj_chains helper struct with DEFINE_FREE() to manage the dma_fence_chain array in drm_syncobj_timeline_signal_ioctl() using __free(syncobj_chains).
Quoted 69 lines
drivers/gpu/drm/drm_syncobj.c +26
-10@@ -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],
drivers/gpu/drm/drm_syncobj.c drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, · line 1651
Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +3
-2+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);
[Severity: Low]
This isn't a bug, but drm_syncobj_timeline_signal_ioctl() mixes __free() cleanup with goto out unwinding for syncobjs, and declares chains empty at the top of the function (before points) instead of defining and initializing it where the array is allocated. Follow-up commit "drm/syncobj: Use scoped cleanup in drm_syncobj_array_find and callers" removes the goto label, though both it and commit "drm/syncobj: Add temporary array allocation fast paths" still declare chains at the top of the function before points, which reverses the LIFO cleanup order relative to the allocation order.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH 5/7] drm/syncobj: Use scoped cleanup for drm_syncobj_timeline_signal_ioctl chains
In reply to Tvrtko UrsulinQuoted 27 lines · Tvrtko Ursulin
Use scoped cleanup for
drm_syncobj_timeline_signal_ioctlchains 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(-)drivers/gpu/drm/drm_syncobj.c +4
-0@@ -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; +};
drivers/gpu/drm/drm_syncobj.c drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, · line 1582
Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +10
-0+ +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);
Yeah same here, I would only remove the kvfree and not the refcounting cleanup.
Regards,
Christian.
Quoted 58 lines
drivers/gpu/drm/drm_syncobj.c +15
-12+} + +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);
PATCH 6/7 drm/syncobj: Use scoped cleanup in drm_syncobj_array_find and callers
In reply to Tvrtko UrsulinUse scoped cleanup in drm_syncobj_array_find and its callers for simpler error handling and easier extending with a fast path in the future.
Arrays which hold the drm_syncobj objects are now allocated by the callers, removing the output parameter.
Maximum number of handles check is moved to the callers as well.
--- drivers/gpu/drm/drm_syncobj.c | 195 +++++++++++++++++----------------- 1 file changed, 97 insertions(+), 98 deletions(-)
drivers/gpu/drm/drm_syncobj.c +97 -98
@@ -1252,59 +1252,38 @@ static int drm_syncobj_array_wait(struct drm_device *dev,return 0 ; } +struct syncobj_array { +struct drm_syncobj **syncobjs; +unsigned int count; +}; + +static void syncobj_array_free (struct syncobj_array *a) +{ +for (unsigned int i =0 ; i < a->count; i++) +drm_syncobj_put (a->syncobjs[i]); +} + +DEFINE_FREE(syncobj_array, struct syncobj_array, syncobj_array_free(&_T)) +static int drm_syncobj_array_find (struct drm_file *file_private, -void __user *user_handles, -uint32_t count_handles, -struct drm_syncobj ***syncobjs_out) +void __user *user_handles,unsigned int count, +struct syncobj_array *array) { -uint32_t i, *handles; -struct drm_syncobj **syncobjs; -int ret; + u32 *handles __free(kvfree) = NULL; +unsigned int i; -if (count_handles >DRM_SYNCOBJ_MAX_HANDLES ) -return -EINVAL ; - - handles =vmemdup_array_user (user_handles, count_handles, -sizeof (*handles)); + handles =vmemdup_array_user (user_handles, count,sizeof (*handles));if (IS_ERR (handles)) -return PTR_ERR (handles); +return PTR_ERR (no_free_ptr (handles)); - syncobjs =kvmalloc_objs (*syncobjs, count_handles); -if (syncobjs ==NULL ) { - ret = -ENOMEM ; - goto err_free_handles; +for (i =0 ; i < count; i++) { + array->syncobjs[i] =drm_syncobj_find (file_private, handles[i]); +if (!array->syncobjs[i]) +return -ENOENT ; + array->count++; } -for (i =0 ; i < count_handles; i++) { - syncobjs[i] =drm_syncobj_find (file_private, handles[i]); -if (!syncobjs[i]) { - ret = -ENOENT ; - goto err_put_syncobjs; - } - } - -kvfree (handles); - *syncobjs_out = syncobjs;return 0 ; - -err_put_syncobjs: -while (i-- >0 ) -drm_syncobj_put (syncobjs[i]); -kvfree (syncobjs); -err_free_handles: -kvfree (handles); - -return ret; -} - -static void drm_syncobj_array_free (struct drm_syncobj **syncobjs, -uint32_t count) -{ -uint32_t i; - -for (i =0 ; i < count; i++) -drm_syncobj_put (syncobjs[i]); -kvfree (syncobjs); }int
@@ -1312,10 +1291,11 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_wait *args = data; -struct drm_syncobj **syncobjs; + struct drm_syncobj **syncobjs __free(kvfree) = NULL; + struct syncobj_array array __free(syncobj_array) = {};unsigned int possible_flags;ktime_t t, *tp =NULL ; -int ret =0 ; +int ret;if (!drm_core_check_feature (dev,DRIVER_SYNCOBJ ))return -EOPNOTSUPP ;
@@ -1329,11 +1309,17 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,if (args->count_handles ==0 )return 0 ; +else if (args->count_handles >DRM_SYNCOBJ_MAX_HANDLES ) +return -EINVAL ; + syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); +if (!syncobjs) +return -ENOMEM ; + + array.syncobjs = syncobjs; ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), - args->count_handles, - &syncobjs); + args->count_handles, &array);if (ret <0 )return ret;
@@ -1345,8 +1331,6 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_wait (dev, file_private, args,NULL , syncobjs, false, tp); -drm_syncobj_array_free (syncobjs, args->count_handles); -return ret; }
@@ -1355,7 +1339,8 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_wait *args = data; -struct drm_syncobj **syncobjs; + struct drm_syncobj **syncobjs __free(kvfree) = NULL; + struct syncobj_array array __free(syncobj_array) = {};unsigned int possible_flags;ktime_t t, *tp =NULL ;int ret =0 ;
@@ -1373,11 +1358,17 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data,if (args->count_handles ==0 )return 0 ; +else if (args->count_handles >DRM_SYNCOBJ_MAX_HANDLES ) +return -EINVAL ; + syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); +if (!syncobjs) +return -ENOMEM ; + + array.syncobjs = syncobjs; ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), - args->count_handles, - &syncobjs); + args->count_handles, &array);if (ret <0 )return ret;
@@ -1389,8 +1380,6 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, ret =drm_syncobj_array_wait (dev, file_private,NULL , args, syncobjs, true, tp); -drm_syncobj_array_free (syncobjs, args->count_handles); -return ret; }
@@ -1502,7 +1491,8 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_array *args = data; -struct drm_syncobj **syncobjs; + struct drm_syncobj **syncobjs __free(kvfree) = NULL; + struct syncobj_array array __free(syncobj_array) = {};uint32_t i;int ret;
@@ -1512,21 +1502,24 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,if (args->pad !=0 )return -EINVAL ; -if (args->count_handles ==0 ) +if (args->count_handles ==0 || + args->count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; + syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); +if (!syncobjs) +return -ENOMEM ; + + array.syncobjs = syncobjs; ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), - args->count_handles, - &syncobjs); + args->count_handles, &array);if (ret <0 )return ret;for (i =0 ; i < args->count_handles; i++)drm_syncobj_replace_fence (syncobjs[i],NULL ); -drm_syncobj_array_free (syncobjs, args->count_handles); -return 0 ; }
@@ -1535,7 +1528,8 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_array *args = data; -struct drm_syncobj **syncobjs; + struct drm_syncobj **syncobjs __free(kvfree) = NULL; + struct syncobj_array array __free(syncobj_array) = {};uint32_t i;int ret;
@@ -1545,13 +1539,18 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,if (args->pad !=0 )return -EINVAL ; -if (args->count_handles ==0 ) +if (args->count_handles ==0 || + args->count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; + syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); +if (!syncobjs) +return -ENOMEM ; + + array.syncobjs = syncobjs; ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), - args->count_handles, - &syncobjs); + args->count_handles, &array);if (ret <0 )return ret;
@@ -1561,8 +1560,6 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,break ; } -drm_syncobj_array_free (syncobjs, args->count_handles); -return ret; }
@@ -1589,9 +1586,10 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_array *args = data; + struct drm_syncobj **syncobjs __free(kvfree) = NULL; + struct syncobj_array array __free(syncobj_array) = {}; struct syncobj_chains chains __free(syncobj_chains) = {}; u64 *points __free(kvfree) = NULL; -struct drm_syncobj **syncobjs;unsigned int i;int ret;
@@ -1601,40 +1599,39 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,if (args->flags !=0 )return -EINVAL ; -if (args->count_handles ==0 ) +if (args->count_handles ==0 || + args->count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; + syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); +if (!syncobjs) +return -ENOMEM ; + + array.syncobjs = syncobjs; ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), - args->count_handles, - &syncobjs); + args->count_handles, &array);if (ret <0 )return ret; points =kvmalloc_objs (*points, args->count_handles); -if (!points) { - ret = -ENOMEM ; - goto out; - } -if (!u64_to_user_ptr (args->points)) { +if (!points) +return -ENOMEM ; + +if (!u64_to_user_ptr (args->points))memset (points,0 , args->count_handles *sizeof (u64)); - }else if (copy_from_user (points,u64_to_user_ptr (args->points), -sizeof (u64) * args->count_handles)) { - ret = -EFAULT ; - goto out; - } +else if (copy_from_user (points,u64_to_user_ptr (args->points), +sizeof (u64) * args->count_handles)) +return -EFAULT ; chains.chains =kvmalloc_objs (*chains.chains, args->count_handles); -if (!chains.chains) { - ret = -ENOMEM ; - goto out; - } +if (!chains.chains) +return -ENOMEM ; +for (i =0 ; i < args->count_handles; i++) { chains.chains[i] =dma_fence_chain_alloc (); -if (!chains.chains[i]) { - ret = -ENOMEM ; - goto out; - } +if (!chains.chains[i]) +return -ENOMEM ; chains.count++; }
@@ -1647,9 +1644,6 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,dma_fence_put (fence); } -out: -drm_syncobj_array_free (syncobjs, args->count_handles); -return ret; }
@@ -1657,7 +1651,8 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_array *args = data; -struct drm_syncobj **syncobjs; + struct drm_syncobj **syncobjs __free(kvfree) = NULL; + struct syncobj_array array __free(syncobj_array) = {};uint64_t __user *points =u64_to_user_ptr (args->points);uint32_t i;int ret;
@@ -1668,13 +1663,18 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,if (args->flags & ~DRM_SYNCOBJ_QUERY_FLAGS_LAST_SUBMITTED )return -EINVAL ; -if (args->count_handles ==0 ) +if (args->count_handles ==0 || + args->count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; + syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); +if (!syncobjs) +return -ENOMEM ; + + array.syncobjs = syncobjs; ret =drm_syncobj_array_find (file_private,u64_to_user_ptr (args->handles), - args->count_handles, - &syncobjs); + args->count_handles, &array);if (ret <0 )return ret;
@@ -1717,7 +1717,6 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,if (ret)break ; } -drm_syncobj_array_free (syncobjs, args->count_handles);return ret; }
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH 6/7] drm/syncobj: Use scoped cleanup in drm_syncobj_array_find and callers
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/syncobj: __free() variables not initialized at declaration site --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/syncobj: __free() variables not initialized at declaration site
--
commit d2db4e35813dabf4fb7e2f45ff5acb392b0b03ac
Author: Tvrtko Ursulin <[email protected]>
drm/syncobj: Use scoped cleanup in drm_syncobj_array_find and callers
This commit converts drm_syncobj_array_find() and its ioctl callers to use scoped cleanup (__free) and moves the syncobj array allocation and maximum handle count checks into the callers.
Quoted 53 lines
drivers/gpu/drm/drm_syncobj.c +16
-24@@ -1252,59 +1252,38 @@ static int drm_syncobj_array_wait(struct drm_device *dev,return 0 ; } -static int drm_syncobj_array_find (struct drm_file *file_private, -void __user *user_handles, -uint32_t count_handles, -struct drm_syncobj ***syncobjs_out) -{ -uint32_t i, *handles; +struct syncobj_array {struct drm_syncobj **syncobjs; -int ret; +unsigned int count; +}; -if (count_handles >DRM_SYNCOBJ_MAX_HANDLES ) -return -EINVAL ; +static void syncobj_array_free (struct syncobj_array *a) +{ +for (unsigned int i =0 ; i < a->count; i++) +drm_syncobj_put (a->syncobjs[i]); +} - handles =vmemdup_array_user (user_handles, count_handles, -sizeof (*handles)); -if (IS_ERR (handles)) -return PTR_ERR (handles); +DEFINE_FREE(syncobj_array, struct syncobj_array, syncobj_array_free(&_T)) - syncobjs =kvmalloc_objs (*syncobjs, count_handles); -if (syncobjs ==NULL ) { - ret = -ENOMEM ; - goto err_free_handles; - } +static int drm_syncobj_array_find (struct drm_file *file_private, +void __user *user_handles,unsigned int count, +struct syncobj_array *array) +{ + u32 *handles __free(kvfree) = NULL; +unsigned int i; -for (i =0 ; i < count_handles; i++) { - syncobjs[i] =drm_syncobj_find (file_private, handles[i]); -if (!syncobjs[i]) { - ret = -ENOENT ; - goto err_put_syncobjs; - } + handles =vmemdup_array_user (user_handles, count,sizeof (*handles));
[ ... ]
Quoted 16 lines
drivers/gpu/drm/drm_syncobj.c +3
-2@@ -1312,10 +1291,11 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_wait *args = data; -struct drm_syncobj **syncobjs; + struct drm_syncobj **syncobjs __free(kvfree) = NULL; + struct syncobj_array array __free(syncobj_array) = {};unsigned int possible_flags;ktime_t t, *tp =NULL ; -int ret =0 ; +int ret;if (!drm_core_check_feature (dev,DRIVER_SYNCOBJ ))return -EOPNOTSUPP ;@@ -1329,11 +1309,17 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
drivers/gpu/drm/drm_syncobj.c drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, · line 1319
Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +7
-0if (args->count_handles ==0 )return 0 ; +else if (args->count_handles >DRM_SYNCOBJ_MAX_HANDLES ) +return -EINVAL ; + + syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); +if (!syncobjs) +return -ENOMEM ; + array.syncobjs = syncobjs;
[Severity: Low]
This isn't a bug, but cleanup.h recommends defining and initializing __free() variables in a single statement at their allocation site rather than declaring them with = NULL at the top of the function and assigning them later. Doing that for handles in drm_syncobj_array_find() and for syncobjs (and array) in drm_syncobj_wait_ioctl() and the other five ioctl callers in this patch also ensures reverse-acquisition cleanup order naturally without relying on manual declaration ordering at the top of each function.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH 7/7 drm/syncobj: Add temporary array allocation fast paths
In reply to Tvrtko UrsulinRunning the Cyberpunk 2077 benchmark we can observe that waiting on DRM sycobjs (drm_syncobj_array_wait_timeout) is relatively hot, but that the 96% of the calls are for a single object, ~4% for two points, and never more than three points. The lookup helper (drm_syncobj_array_find) many other ioctls go through is in the similar situation with 97% of the calls being for a single object, ~3% for two points, and never more than three points. A more trivial workload like vkmark under KDE Plasma is even more skewed to single point lookups.
Therefore lets add a fast path to bypass the temporary array allocation and use a pre-allocated stack array for those cases.
We do this by adding two macros for declaring and initializing those arrays. Because we have previously converted all impacted functions to fully use scoped cleanup this is now quite easy to do.
--- drivers/gpu/drm/drm_syncobj.c | 167 +++++++++++++++++++++------------- 1 file changed, 105 insertions(+), 62 deletions(-)
drivers/gpu/drm/drm_syncobj.c +105 -62
@@ -214,6 +214,53 @@#include "drm_internal.h" +/* +* 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 + +#define DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (_type, _name) \ + _type _name##_stack[DRM_SYNCOBJ_FAST_PATH_ENTRIES]; \ + _type * _name##_heap __free(kvfree) = NULL; \ + _type * _name + +#define DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (_name, _src, _count) ({ \ + int __ret = 0; \ + \ + if ((_count) > ARRAY_SIZE(_name##_stack)) { \ + if (_src) { \ + _name##_heap = vmemdup_array_user((_src), \ + (_count), \ + sizeof(*(_name))); \ + if (IS_ERR(_name##_heap)) { \ + __ret = PTR_ERR(no_free_ptr(_name##_heap)); \ + } else { \ + _name = _name##_heap; \ + } \ + } else { \ + _name##_heap = kvzalloc_objs(*(_name), (_count)); \ + if (!_name##_heap) \ + __ret = -ENOMEM; \ + else \ + _name = _name##_heap; \ + } \ + } else { \ + size_t __size = (_count) * sizeof(*(_name)); \ + \ + _name = _name##_stack; \ + if (_src) { \ + if (copy_from_user(_name, (_src), __size)) \ + __ret = -EFAULT; \ + } else { \ + memset(_name, 0, __size); \ + } \ + } \ + __ret; \ +}) +struct syncobj_wait_entry {struct list_head node;struct task_struct *task;
@@ -1062,8 +1109,6 @@ static void syncobj_wait_entries_free(struct syncobj_wait_entries *e) &e->entries[i].fence_cb);dma_fence_put (e->entries[i].fence); } - -kvfree (e->entries); } DEFINE_FREE(syncobj_wait_entries, struct syncobj_wait_entries,
@@ -1077,11 +1122,12 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,uint32_t *idx, ktime_t *deadline) { +DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (struct syncobj_wait_entry ,entries ); struct syncobj_wait_entries cleanup __free(syncobj_wait_entries) = {}; -struct syncobj_wait_entry *entries; - u64 *points __free(kvfree) = NULL; -struct dma_fence *fence; +DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (u64, points);uint32_t signaled_count, i; +struct dma_fence *fence; +int ret;if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE )) {
@@ -1089,18 +1135,13 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,lockdep_assert_none_held_once (); } - points =kvmalloc_objs (*points, count); -if (!points) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (points, user_points, count); +if (ret) +return ret; -if (!user_points) -memset (points,0 , count *sizeof (u64)); -else if (copy_from_user (points, user_points,sizeof (u64) * count)) -return -EFAULT ; - - entries =kvzalloc_objs (*entries, count); -if (!entries) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (entries,NULL , count); +if (ret) +return ret;/* After this the entries cleanup is fully automatic. */ cleanup.syncobjs = syncobjs;
@@ -1291,7 +1332,7 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_wait *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};unsigned int possible_flags;ktime_t t, *tp =NULL ;
@@ -1312,9 +1353,10 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,else if (args->count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1339,11 +1381,11 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_wait *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};unsigned int possible_flags;ktime_t t, *tp =NULL ; -int ret =0 ; +int ret;if (!drm_core_check_feature (dev,DRIVER_SYNCOBJ_TIMELINE ))return -EOPNOTSUPP ;
@@ -1361,9 +1403,10 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data,else if (args->count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1491,7 +1534,7 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_array *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};uint32_t i;int ret;
@@ -1506,9 +1549,10 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, args->count_handles > DRM_SYNCOBJ_MAX_HANDLES)return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1528,7 +1572,7 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_array *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};uint32_t i;int ret;
@@ -1543,9 +1587,9 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, args->count_handles > DRM_SYNCOBJ_MAX_HANDLES)return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1575,8 +1619,6 @@ static void syncobj_chains_free(struct syncobj_chains *c)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))
@@ -1586,10 +1628,11 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_array *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {}; - struct syncobj_chains chains __free(syncobj_chains) = {}; - u64 *points __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct dma_fence_chain *, chains); + struct syncobj_chains cleanup_chains __free(syncobj_chains) = {}; +DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (u64, points);unsigned int i;int ret;
@@ -1603,9 +1646,10 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, args->count_handles > DRM_SYNCOBJ_MAX_HANDLES)return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1614,33 +1658,31 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,if (ret <0 )return ret; - points =kvmalloc_objs (*points, args->count_handles); -if (!points) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (points, +u64_to_user_ptr (args->points), + args->count_handles); +if (ret) +return ret; -if (!u64_to_user_ptr (args->points)) -memset (points,0 , args->count_handles *sizeof (u64)); -else if (copy_from_user (points,u64_to_user_ptr (args->points), -sizeof (u64) * args->count_handles)) -return -EFAULT ; - - chains.chains =kvmalloc_objs (*chains.chains, args->count_handles); -if (!chains.chains) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (chains,NULL , + args->count_handles); +if (ret) +return ret; + cleanup_chains.chains = chains;for (i =0 ; i < args->count_handles; i++) { - chains.chains[i] =dma_fence_chain_alloc (); -if (!chains.chains[i]) + chains[i] =dma_fence_chain_alloc (); +if (!chains[i])return -ENOMEM ; - chains.count++; + cleanup_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.chains[i], +drm_syncobj_add_point (syncobjs[i], chains[i], fence, points[i]); - chains.chains[i] =NULL ;/* Ownership transferred to syncobj */ + chains[i] =NULL ;/* Ownership transferred to syncobj */ dma_fence_put (fence); }
@@ -1651,7 +1693,7 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_array *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};uint64_t __user *points =u64_to_user_ptr (args->points);uint32_t i;
@@ -1667,9 +1709,10 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data, args->count_handles > DRM_SYNCOBJ_MAX_HANDLES)return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH 7/7] drm/syncobj: Add temporary array allocation fast paths
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/syncobj: unconverted handles allocation in drm_syncobj_array_find() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/syncobj: unconverted handles allocation in drm_syncobj_array_find()
--
commit 9cfbb7c668583619e7699957c17e9063e116a22e
Author: Tvrtko Ursulin <[email protected]>
drm/syncobj: Add temporary array allocation fast paths
This commit adds stack-backed fast-path macros for temporary syncobj array allocations up to four elements and converts the syncobj wait, reset, signal, and query paths to use them.
Quoted 44 lines
drivers/gpu/drm/drm_syncobj.c +36
-0@@ -214,6 +214,53 @@#include "drm_internal.h" +/* +* 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 + +#define DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (_type, _name) \ + _type _name##_stack[DRM_SYNCOBJ_FAST_PATH_ENTRIES]; \ + _type * _name##_heap __free(kvfree) = NULL; \ + _type * _name + +#define DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (_name, _src, _count) ({ \ + int __ret = 0; \ + \ + if ((_count) > ARRAY_SIZE(_name##_stack)) { \ + if (_src) { \ + _name##_heap = vmemdup_array_user((_src), \ + (_count), \ + sizeof(*(_name))); \ + if (IS_ERR(_name##_heap)) { \ + __ret = PTR_ERR(no_free_ptr(_name##_heap)); \ + } else { \ + _name = _name##_heap; \ + } \ + } else { \ + _name##_heap = kvzalloc_objs(*(_name), (_count)); \ + if (!_name##_heap) \ + __ret = -ENOMEM; \ + else \ + _name = _name##_heap; \ + } \ + } else { \ + size_t __size = (_count) * sizeof(*(_name)); \ + \
drivers/gpu/drm/drm_syncobj.c line 262
Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +10
-0+ _name = _name##_stack; \ +if (_src) { \ +if (copy_from_user (_name, (_src), __size)) \ + __ret = -EFAULT ; \ + }else { \ +memset (_name,0 , __size); \ + } \ + } \ + __ret; \ +})
[Severity: Medium]
Was drm_syncobj_array_find() meant to be converted to use the fast-path array macros for its temporary handles array as well?
The commit message notes that drm_syncobj_array_find() is called with one to three objects in almost all calls, and commit "drm/syncobj: Use scoped cleanup in drm_syncobj_array_find and callers" converted handles to scoped cleanup, but drm_syncobj_array_find() still unconditionally allocates and frees handles on the heap on every syncobj array ioctl:
drivers/gpu/drm/drm_syncobj.c:drm_syncobj_array_find() {
u32 *handles __free(kvfree) = NULL;
unsigned int i;
handles = vmemdup_array_user(user_handles, count, sizeof(*handles));
if (IS_ERR(handles))
return PTR_ERR(no_free_ptr(handles));
...
}Note that if drm_syncobj_array_find() is converted to DRM_SYNCOBJ_INIT_FASTPATH_ARRAY(), a runtime NULL user_handles pointer (args->handles == 0) must still return -EFAULT rather than taking the !_src zero-initialization branch and looking up handle 0 (-ENOENT).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v2 7/7 drm/syncobj: Add temporary array allocation fast paths
In reply to Tvrtko UrsulinRunning the Cyberpunk 2077 benchmark we can observe that waiting on DRM sycobjs (drm_syncobj_array_wait_timeout) is relatively hot, but that the 96% of the calls are for a single object, ~4% for two points, and never more than three points. The lookup helper (drm_syncobj_array_find) many other ioctls go through is in the similar situation with 97% of the calls being for a single object, ~3% for two points, and never more than three points. A more trivial workload like vkmark under KDE Plasma is even more skewed to single point lookups.
Therefore lets add a fast path to bypass the temporary array allocation and use a pre-allocated stack array for those cases.
We do this by adding two macros for declaring and initializing those arrays. Because we have previously converted all impacted functions to fully use scoped cleanup this is now quite easy to do.
---
v2:
* Use fast path in drm_syncobj_array_find itself!
--- drivers/gpu/drm/drm_syncobj.c | 179 +++++++++++++++++++++------------- 1 file changed, 113 insertions(+), 66 deletions(-)
drivers/gpu/drm/drm_syncobj.c +113 -66
@@ -214,6 +214,53 @@#include "drm_internal.h" +/* +* 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 + +#define DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (_type, _name) \ + _type _name##_stack[DRM_SYNCOBJ_FAST_PATH_ENTRIES]; \ + _type * _name##_heap __free(kvfree) = NULL; \ + _type * _name + +#define DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (_name, _src, _count) ({ \ + int __ret = 0; \ + \ + if ((_count) > ARRAY_SIZE(_name##_stack)) { \ + if (_src) { \ + _name##_heap = vmemdup_array_user((_src), \ + (_count), \ + sizeof(*(_name))); \ + if (IS_ERR(_name##_heap)) { \ + __ret = PTR_ERR(no_free_ptr(_name##_heap)); \ + } else { \ + _name = _name##_heap; \ + } \ + } else { \ + _name##_heap = kvzalloc_objs(*(_name), (_count)); \ + if (!_name##_heap) \ + __ret = -ENOMEM; \ + else \ + _name = _name##_heap; \ + } \ + } else { \ + size_t __size = (_count) * sizeof(*(_name)); \ + \ + _name = _name##_stack; \ + if (_src) { \ + if (copy_from_user(_name, (_src), __size)) \ + __ret = -EFAULT; \ + } else { \ + memset(_name, 0, __size); \ + } \ + } \ + __ret; \ +}) +struct syncobj_wait_entry {struct list_head node;struct task_struct *task;
@@ -1062,8 +1109,6 @@ static void syncobj_wait_entries_free(struct syncobj_wait_entries *e) &e->entries[i].fence_cb);dma_fence_put (e->entries[i].fence); } - -kvfree (e->entries); } DEFINE_FREE(syncobj_wait_entries, struct syncobj_wait_entries,
@@ -1077,11 +1122,12 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,uint32_t *idx, ktime_t *deadline) { +DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (struct syncobj_wait_entry ,entries ); struct syncobj_wait_entries cleanup __free(syncobj_wait_entries) = {}; -struct syncobj_wait_entry *entries; - u64 *points __free(kvfree) = NULL; -struct dma_fence *fence; +DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (u64, points);uint32_t signaled_count, i; +struct dma_fence *fence; +int ret;if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE )) {
@@ -1089,18 +1135,13 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,lockdep_assert_none_held_once (); } - points =kvmalloc_objs (*points, count); -if (!points) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (points, user_points, count); +if (ret) +return ret; -if (!user_points) -memset (points,0 , count *sizeof (u64)); -else if (copy_from_user (points, user_points,sizeof (u64) * count)) -return -EFAULT ; - - entries =kvzalloc_objs (*entries, count); -if (!entries) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (entries,NULL , count); +if (ret) +return ret;/* After this the entries cleanup is fully automatic. */ cleanup.syncobjs = syncobjs;
@@ -1269,12 +1310,16 @@ static int drm_syncobj_array_find(struct drm_file *file_private, void __user *user_handles, unsigned int count, struct syncobj_array *array) { - u32 *handles __free(kvfree) = NULL; +DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (u32 ,handles );unsigned int i; +int ret; - handles =vmemdup_array_user (user_handles, count,sizeof (*handles)); -if (IS_ERR (handles)) -return PTR_ERR (no_free_ptr (handles)); +if (!user_handles) +return -EFAULT ;/* Preserve ABI error code */ + + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (handles, user_handles, count); +if (ret) +return ret;for (i =0 ; i < count; i++) { array->syncobjs[i] =drm_syncobj_find (file_private, handles[i]);
@@ -1291,7 +1336,7 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_wait *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};unsigned int possible_flags;ktime_t t, *tp =NULL ;
@@ -1312,9 +1357,10 @@ drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,else if (args->count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1339,11 +1385,11 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_wait *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};unsigned int possible_flags;ktime_t t, *tp =NULL ; -int ret =0 ; +int ret;if (!drm_core_check_feature (dev,DRIVER_SYNCOBJ_TIMELINE ))return -EOPNOTSUPP ;
@@ -1361,9 +1407,10 @@ drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data,else if (args->count_handles >DRM_SYNCOBJ_MAX_HANDLES )return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1491,7 +1538,7 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_array *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};uint32_t i;int ret;
@@ -1506,9 +1553,10 @@ drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, args->count_handles > DRM_SYNCOBJ_MAX_HANDLES)return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1528,7 +1576,7 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_array *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};uint32_t i;int ret;
@@ -1543,9 +1591,9 @@ drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, args->count_handles > DRM_SYNCOBJ_MAX_HANDLES)return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1575,8 +1623,6 @@ static void syncobj_chains_free(struct syncobj_chains *c)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))
@@ -1586,10 +1632,11 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_array *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {}; - struct syncobj_chains chains __free(syncobj_chains) = {}; - u64 *points __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct dma_fence_chain *, chains); + struct syncobj_chains cleanup_chains __free(syncobj_chains) = {}; +DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY (u64, points);unsigned int i;int ret;
@@ -1603,9 +1650,10 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data, args->count_handles > DRM_SYNCOBJ_MAX_HANDLES)return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
@@ -1614,33 +1662,31 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,if (ret <0 )return ret; - points =kvmalloc_objs (*points, args->count_handles); -if (!points) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (points, +u64_to_user_ptr (args->points), + args->count_handles); +if (ret) +return ret; -if (!u64_to_user_ptr (args->points)) -memset (points,0 , args->count_handles *sizeof (u64)); -else if (copy_from_user (points,u64_to_user_ptr (args->points), -sizeof (u64) * args->count_handles)) -return -EFAULT ; - - chains.chains =kvmalloc_objs (*chains.chains, args->count_handles); -if (!chains.chains) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (chains,NULL , + args->count_handles); +if (ret) +return ret; + cleanup_chains.chains = chains;for (i =0 ; i < args->count_handles; i++) { - chains.chains[i] =dma_fence_chain_alloc (); -if (!chains.chains[i]) + chains[i] =dma_fence_chain_alloc (); +if (!chains[i])return -ENOMEM ; - chains.count++; + cleanup_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.chains[i], +drm_syncobj_add_point (syncobjs[i], chains[i], fence, points[i]); - chains.chains[i] =NULL ;/* Ownership transferred to syncobj */ + chains[i] =NULL ;/* Ownership transferred to syncobj */ dma_fence_put (fence); }
@@ -1651,7 +1697,7 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file_private) {struct drm_syncobj_timeline_array *args = data; - struct drm_syncobj **syncobjs __free(kvfree) = NULL; + DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(struct drm_syncobj *, syncobjs); struct syncobj_array array __free(syncobj_array) = {};uint64_t __user *points =u64_to_user_ptr (args->points);uint32_t i;
@@ -1667,9 +1713,10 @@ int drm_syncobj_query_ioctl(struct drm_device *dev, void *data, args->count_handles > DRM_SYNCOBJ_MAX_HANDLES)return -EINVAL ; - syncobjs =kvmalloc_objs (*syncobjs, args->count_handles); -if (!syncobjs) -return -ENOMEM ; + ret =DRM_SYNCOBJ_INIT_FASTPATH_ARRAY (syncobjs,NULL , + args->count_handles); +if (ret) +return ret; array.syncobjs = syncobjs; ret = drm_syncobj_array_find(file_private,
Signature · 1 line
2.56.0
Re: [PATCH v2 7/7] drm/syncobj: Add temporary array allocation fast paths
Quoted 2 lines · Tvrtko Ursulin
Running the Cyberpunk 2077 benchmark we can observe that waiting on DRM sycobjs (
drm_syncobj_array_wait_timeout) is relatively hot, but that the
Nit: "sycobjs" -> "syncobjs".
Quoted 2 lines
v2:
* Use fast path indrm_syncobj_array_finditself!
Thanks, that drops the handles allocation from every array ioctl (and covers the sashiko comment on v1).
Quoted 1 line
+#define
DRM_SYNCOBJ_INIT_FASTPATH_ARRAY(_name, _src, _count)({ \
[...]
drivers/gpu/drm/drm_syncobj.c line 262
Quoted 13 lines
drivers/gpu/drm/drm_syncobj.c +13
-0+ }else { \ +size_t __size = (_count) *sizeof (*(_name)); \ + \ + _name = _name##_stack; \ +if (_src) { \ +if (copy_from_user (_name, (_src), __size)) \ + __ret = -EFAULT ; \ + }else { \ +memset (_name,0 , __size); \ + } \ + } \ + __ret; \ +})
A NULL _src means "zero-fill" here. That is the right thing for the points arrays, but it is also why array_find needs its own check:
drivers/gpu/drm/drm_syncobj.c static int drm_syncobj_array_find(struct drm_file *file_private, · line 1318
Quoted 2 lines
drivers/gpu/drm/drm_syncobj.c +2
-0+if (!user_handles) +return -EFAULT ;/* Preserve ABI error code */
A future caller that forgets this check would silently get zeroes instead of -EFAULT for a NULL user pointer. Maybe split the init into a copy variant and a zero variant (a NULL user pointer then fails in copy_from_user() / vmemdup_array_user() as before, and this check can go), or at least say in a comment above the macro that NULL means zero-fill?
Thanks,
Fei
Quoted 3 lines · Tvrtko Ursulin
Syncobj IGT pass, light gaming and light desktop as well, and there are no kmemleak reports. Nevertheless this version is a bit more invasive so if the approach is looking good a careful review will be required.
Tested patches 1-6 plus v2 of 7/7 on v7.4-rc4 (x86_64, KASAN + KASAN_STACK, lockdep, kmemleak, failslab/fail_page_alloc/fail_usercopy, virtio-gpu in QEMU), base and patched with the same config:
- IGT syncobj_basic, syncobj_wait, syncobj_timeline: 202/202 on both, identical per subtest, dmesg clean.
- The targeted test from the previous version, 1..8 handles on all six array ioctls (success, -ENOENT at every position, -EFAULT for bad / partially unmapped handle and point arrays, sw_sync and WAIT_FOR_SUBMIT wake-ups, -ETIME / -EINTR followed by late signalling, 20 s stress), and 1M handles OK / 1M + 1 -EINVAL:
all pass on both, no splats, kmemleak clean.
- Since the error paths are what changed most, I also ran every ioctl with n = 1, 2, 4, 5, 8 under fail-nth, failing each allocation / user copy in turn (~340 injected faults on the patched kernel, among them partial dma_fence_chain_alloc() in timeline_signal and the kvzalloc / vmemdup fallbacks above 4 entries), plus blocking WAIT_FOR_SUBMIT waits the same way. Every call returns -ENOMEM / -EFAULT or succeeds, each syncobj can be destroyed exactly once afterwards, no KASAN report, kmemleak clean. Same on base. A NULL handles pointer still gives -EFAULT, and a NULL points pointer behaves the same as before for n = 1..8.
- Every patch builds with W=1 and sparse clean, i386 too.
So whatever the outcome of the discussion on 2-5, the scoped conversion did not introduce any error path bug in my testing.
On points when user_points is NULL (2/7 and 4/7): with 7/7 that is a memset of at most 32 bytes on the stack for n <= 4, so skipping it only matters for the slow path.
Stack (KASAN build, so inflated): the wait path goes from 640 to 1160 bytes (wait_ioctl 184 -> 296, array_wait_timeout 280 -> 688), timeline_signal_ioctl 224 -> 472, the other ioctls +104..+112, array_find 88 -> 224.
Thanks,
Fei