dma-buf: improve robustness of dma_fence_unwrap_for_each
Subsystems (10)
PATCH 1/1 dma-buf: improve robustness of dma_fence_unwrap_for_each
The macro dma_fence_unwrap_for_each() is used to deep dive into fences in chain and array containers.
To guarantee correct functionality it does this by keeping a reference to chain elements in the iterator structure. But so far there was no well defined cleanup procedure for the iterator when breaking out of the loop early.
Some code implemented dropping the reference manually, but Erhan now found another issue in an error path in amdgpu.
Fix this for good, provide a proper cleanup function which is automatically used with a guard and remove the workarounds spread around in the calling code.
--- drivers/dma-buf/dma-buf.c | 5 +- drivers/dma-buf/dma-fence-unwrap.c | 58 ++++++++++++------- drivers/dma-buf/st-dma-fence-unwrap.c | 27 +++------ drivers/dma-buf/sync_file.c | 5 +- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 +- drivers/gpu/drm/imagination/pvr_sync.c | 14 ++--- drivers/gpu/drm/tegra/submit.c | 6 +- drivers/gpu/drm/virtio/virtgpu_submit.c | 7 +-- include/linux/dma-fence-unwrap.h | 35 ++++++----- 9 files changed, 79 insertions(+), 84 deletions(-)
drivers/dma-buf/dma-buf.c +2 -3
@@ -494,7 +494,6 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf,struct dma_buf_import_sync_file arg;struct dma_fence *fence, *f;enum dma_resv_usage usage; -struct dma_fence_unwrap iter;unsigned int num_fences;int ret =0 ;
@@ -515,7 +514,7 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf,DMA_RESV_USAGE_READ ; num_fences =0 ; -dma_fence_unwrap_for_each (f, &iter, fence) +dma_fence_unwrap_for_each (f, fence) ++num_fences;if (num_fences >0 ) {
@@ -523,7 +522,7 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf, ret =dma_resv_reserve_fences (dmabuf->resv, num_fences);if (!ret) { -dma_fence_unwrap_for_each (f, &iter, fence) +dma_fence_unwrap_for_each (f, fence)dma_resv_add_fence (dmabuf->resv, f, usage); }
drivers/dma-buf/dma-fence-unwrap.c +36 -22
109 lines not on this page · raw message
drivers/dma-buf/st-dma-fence-unwrap.c +9 -18
@@ -117,7 +117,6 @@ static void test_sanitycheck(struct kunit *test)static void test_unwrap_array (struct kunit *test) {struct dma_fence *fence, *f1, *f2, *array; -struct dma_fence_unwrap iter; f1 =mock_fence ();KUNIT_ASSERT_NOT_NULL (test, f1);
@@ -136,7 +135,7 @@ static void test_unwrap_array(struct kunit *test) array =mock_array (2 , f1, f2);KUNIT_ASSERT_NOT_NULL (test, array); -dma_fence_unwrap_for_each (fence, &iter, array) { +dma_fence_unwrap_for_each (fence, array) {if (fence == f1) { f1 =NULL ; }else if (fence == f2) {
@@ -155,7 +154,6 @@ static void test_unwrap_array(struct kunit *test)static void test_unwrap_chain (struct kunit *test) {struct dma_fence *fence, *f1, *f2, *chain; -struct dma_fence_unwrap iter; f1 =mock_fence ();KUNIT_ASSERT_NOT_NULL (test, f1);
@@ -174,7 +172,7 @@ static void test_unwrap_chain(struct kunit *test) chain =mock_chain (f1, f2);KUNIT_ASSERT_NOT_NULL (test, chain); -dma_fence_unwrap_for_each (fence, &iter, chain) { +dma_fence_unwrap_for_each (fence, chain) {if (fence == f1) { f1 =NULL ; }else if (fence == f2) {
@@ -193,7 +191,6 @@ static void test_unwrap_chain(struct kunit *test)static void test_unwrap_chain_array (struct kunit *test) {struct dma_fence *fence, *f1, *f2, *array, *chain; -struct dma_fence_unwrap iter; f1 =mock_fence ();KUNIT_ASSERT_NOT_NULL (test, f1);
@@ -215,7 +212,7 @@ static void test_unwrap_chain_array(struct kunit *test) chain =mock_chain (NULL , array);KUNIT_ASSERT_NOT_NULL (test, chain); -dma_fence_unwrap_for_each (fence, &iter, chain) { +dma_fence_unwrap_for_each (fence, chain) {if (fence == f1) { f1 =NULL ; }else if (fence == f2) {
@@ -234,7 +231,6 @@ static void test_unwrap_chain_array(struct kunit *test)static void test_unwrap_merge (struct kunit *test) {struct dma_fence *fence, *f1, *f2, *f3; -struct dma_fence_unwrap iter; f1 =mock_fence ();KUNIT_ASSERT_NOT_NULL (test, f1);
@@ -255,7 +251,7 @@ static void test_unwrap_merge(struct kunit *test) goto error_put_f2; } -dma_fence_unwrap_for_each (fence, &iter, f3) { +dma_fence_unwrap_for_each (fence, f3) {if (fence == f1) {dma_fence_put (f1); f1 =NULL ;
@@ -280,7 +276,6 @@ static void test_unwrap_merge(struct kunit *test)static void test_unwrap_merge_duplicate (struct kunit *test) {struct dma_fence *fence, *f1, *f2; -struct dma_fence_unwrap iter; f1 =mock_fence ();KUNIT_ASSERT_NOT_NULL (test, f1);
@@ -293,7 +288,7 @@ static void test_unwrap_merge_duplicate(struct kunit *test) goto error_put_f1; } -dma_fence_unwrap_for_each (fence, &iter, f2) { +dma_fence_unwrap_for_each (fence, f2) {if (fence == f1) {dma_fence_put (f1); f1 =NULL ;
@@ -313,7 +308,6 @@ static void test_unwrap_merge_duplicate(struct kunit *test)static void test_unwrap_merge_seqno (struct kunit *test) {struct dma_fence *fence, *f1, *f2, *f3, *f4; -struct dma_fence_unwrap iter;u64 ctx[2 ]; ctx[0 ] =dma_fence_context_alloc (1 );
@@ -346,7 +340,7 @@ static void test_unwrap_merge_seqno(struct kunit *test) goto error_put_f3; } -dma_fence_unwrap_for_each (fence, &iter, f4) { +dma_fence_unwrap_for_each (fence, f4) {if (fence == f3 && f2) {dma_fence_put (f3); f3 =NULL ;
@@ -382,7 +376,6 @@ static void test_dedup_empty(struct kunit *test)static void test_unwrap_merge_order (struct kunit *test) {struct dma_fence *fence, *f1, *f2, *a1, *a2, *c1, *c2; -struct dma_fence_unwrap iter; f1 =mock_fence ();KUNIT_ASSERT_NOT_NULL (test, f1);
@@ -419,7 +412,7 @@ static void test_unwrap_merge_order(struct kunit *test)*/ a2 =dma_fence_unwrap_merge (a1, c2); -dma_fence_unwrap_for_each (fence, &iter, a2) { +dma_fence_unwrap_for_each (fence, a2) {if (fence == f1) { f1 =NULL ;if (!f2)
@@ -446,7 +439,6 @@ static void test_unwrap_merge_order(struct kunit *test)static void test_unwrap_merge_complex (struct kunit *test) {struct dma_fence *fence, *f1, *f2, *f3, *f4, *f5; -struct dma_fence_unwrap iter; f1 =mock_fence ();KUNIT_ASSERT_NOT_NULL (test, f1);
@@ -481,7 +473,7 @@ static void test_unwrap_merge_complex(struct kunit *test) goto error_put_f4; } -dma_fence_unwrap_for_each (fence, &iter, f5) { +dma_fence_unwrap_for_each (fence, f5) {if (fence == f1) {dma_fence_put (f1); f1 =NULL ;
@@ -510,7 +502,6 @@ static void test_unwrap_merge_complex(struct kunit *test)static void test_unwrap_merge_complex_seqno (struct kunit *test) {struct dma_fence *fence, *f1, *f2, *f3, *f4, *f5, *f6, *f7; -struct dma_fence_unwrap iter;u64 ctx[2 ]; ctx[0 ] =dma_fence_context_alloc (1 );
@@ -563,7 +554,7 @@ static void test_unwrap_merge_complex_seqno(struct kunit *test) goto error_put_f6; } -dma_fence_unwrap_for_each (fence, &iter, f7) { +dma_fence_unwrap_for_each (fence, f7) {if (fence == f1 && f4) {dma_fence_put (f1); f1 =NULL ;
drivers/dma-buf/sync_file.c +2 -3
@@ -296,7 +296,6 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file, unsigned long arg) {struct sync_fence_info *fence_info =NULL ; -struct dma_fence_unwrap iter;struct sync_file_info info;unsigned int num_fences;struct dma_fence *fence;
@@ -310,7 +309,7 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,return -EINVAL ; num_fences =0 ; -dma_fence_unwrap_for_each (fence, &iter, sync_file->fence) +dma_fence_unwrap_for_each (fence, sync_file->fence) ++num_fences;/*
@@ -335,7 +334,7 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,return -ENOMEM ; num_fences =0 ; -dma_fence_unwrap_for_each (fence, &iter, sync_file->fence) { +dma_fence_unwrap_for_each (fence, sync_file->fence) {int status; status =sync_fill_fence_info (fence, &fence_info[num_fences++]);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -4
@@ -643,7 +643,6 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp, struct drm_gem_object **gobj_read) {int num_read_bo_handles, num_write_bo_handles; -struct dma_fence_unwrap iter;struct dma_fence *fence, *f;unsigned int num_fences =0 ;struct drm_exec exec;
@@ -663,7 +662,7 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp,if (r)return r; -dma_fence_unwrap_for_each (f, &iter, fence) +dma_fence_unwrap_for_each (f, fence) num_fences++;dma_fence_put (fence);
@@ -758,7 +757,6 @@ amdgpu_userq_wait_return_fence_info(struct drm_device *dev, struct drm_file *filint num_read_bo_handles, num_write_bo_handles;struct amdgpu_usermode_queue *waitq;struct dma_fence **fences, *fence, *f; -struct dma_fence_unwrap iter;int num_points, num_syncobj;unsigned int num_fences =0 ;struct drm_exec exec;
@@ -784,7 +782,7 @@ amdgpu_userq_wait_return_fence_info(struct drm_device *dev, struct drm_file *filif (r) goto free_fences; -dma_fence_unwrap_for_each (f, &iter, fence) { +dma_fence_unwrap_for_each (f, fence) { r =amdgpu_userq_wait_add_fence (wait_info, fences, &num_fences, f);if (r) {
drivers/gpu/drm/imagination/pvr_sync.c +4 -10
@@ -205,12 +205,11 @@ pvr_sync_signal_array_push_fences(struct xarray *array)static int pvr_sync_add_dep_to_job (struct drm_sched_job *job,struct dma_fence *f) { -struct dma_fence_unwrap iter;u32 native_fence_count =0 ;struct dma_fence *uf;int err =0 ; -dma_fence_unwrap_for_each (uf, &iter, f) { +dma_fence_unwrap_for_each (uf, f) {if (pvr_queue_fence_is_native (uf)) native_fence_count++; }
@@ -219,14 +218,7 @@ pvr_sync_add_dep_to_job(struct drm_sched_job *job, struct dma_fence *f)if (!native_fence_count)return drm_sched_job_add_dependency (job, f); -dma_fence_unwrap_for_each (uf, &iter, f) { -/* There's no dma_fence_unwrap_stop() helper cleaning up the refs -* owned by dma_fence_unwrap(), so let's just iterate over all -* entries without doing anything when something failed. -*/ -if (err) -continue ; - +dma_fence_unwrap_for_each (uf, f) {if (pvr_queue_fence_is_native (uf)) {struct drm_sched_fence *s_fence =to_drm_sched_fence (uf);
@@ -238,6 +230,8 @@ pvr_sync_add_dep_to_job(struct drm_sched_job *job, struct dma_fence *f) }else { err =drm_sched_job_add_dependency (job,dma_fence_get (uf)); } +if (err) +break ; }dma_fence_put (f);
drivers/gpu/drm/tegra/submit.c +2 -4
@@ -353,14 +353,13 @@ static int submit_get_syncpt(struct tegra_drm_context *context, struct host1x_jostatic int submit_handle_in_fence (struct tegra_drm_context *context,struct host1x *host1x,struct dma_fence *fence,u32 *num_waits) { -struct dma_fence_unwrap iter;struct host1x *fence_host1x;struct dma_fence *f;int err =0 ; *num_waits =0 ; -dma_fence_unwrap_for_each (f, &iter, fence) { +dma_fence_unwrap_for_each (f, fence) {long wait_err;if (err)
@@ -394,10 +393,9 @@ static int submit_handle_in_fence(struct tegra_drm_context *context, struct hoststatic void submit_job_add_prefence (struct host1x_job *job,struct dma_fence *fence,u32 class) { -struct dma_fence_unwrap iter;struct dma_fence *f; -dma_fence_unwrap_for_each (f, &iter, fence) { +dma_fence_unwrap_for_each (f, fence) {u32 id, threshold;if (dma_fence_is_signaled (f))
drivers/gpu/drm/virtio/virtgpu_submit.c +2 -5
@@ -59,16 +59,13 @@ static int virtio_gpu_do_fence_wait(struct virtio_gpu_submit *submit,static int virtio_gpu_dma_fence_wait (struct virtio_gpu_submit *submit,struct dma_fence *fence) { -struct dma_fence_unwrap itr;struct dma_fence *f;int err; -dma_fence_unwrap_for_each (f, &itr, fence) { +dma_fence_unwrap_for_each (f, fence) { err =virtio_gpu_do_fence_wait (submit, f); -if (err) { -dma_fence_put (itr.chain); +if (err)return err; - } }return 0 ;
include/linux/dma-fence-unwrap.h +20 -15
@@ -32,27 +32,33 @@ struct dma_fence_unwrap {unsigned int index; }; -struct dma_fence *dma_fence_unwrap_first (struct dma_fence *head, -struct dma_fence_unwrap *cursor); -struct dma_fence *dma_fence_unwrap_next (struct dma_fence_unwrap *cursor); +struct dma_fence_unwrap dma_fence_unwrap_first (struct dma_fence *head, +struct dma_fence **fence); +void dma_fence_unwrap_next (struct dma_fence_unwrap *cursor, +struct dma_fence **fence); +void dma_fence_unwrap_cleanup (struct dma_fence_unwrap *cursor); + +/* Helper for dma_fence_unwrap_for_each(). Internal use only. */ +#define __dma_fence_unwrap_for_each (fence, cursor, head) \ + for (struct dma_fence_unwrap cursor \ + __cleanup(dma_fence_unwrap_cleanup) \ + = dma_fence_unwrap_first(head, &(fence)); fence; \ + dma_fence_unwrap_next(&cursor, &(fence)))/** * dma_fence_unwrap_for_each - iterate over all fences in containers * @fence: current fence -* @cursor: current position inside the containers * @head: starting point for the iterator * * Unwrap dma_fence_chain and dma_fence_array containers and deep dive into all * potential fences in them. If @head is just a normal fence only that one is * returned. */ -#define dma_fence_unwrap_for_each (fence, cursor, head) \ - for (fence = dma_fence_unwrap_first(head, cursor); fence; \ - fence = dma_fence_unwrap_next(cursor)) +#define dma_fence_unwrap_for_each (fence, head) \ + __dma_fence_unwrap_for_each(fence, __UNIQUE_ID(cursor), head)struct dma_fence *__dma_fence_unwrap_merge (size_t num_fences, -struct dma_fence **fences, -struct dma_fence_unwrap *cursors); +struct dma_fence **fences);size_t dma_fence_dedup_array (struct dma_fence **array,size_t num_fences);
@@ -68,12 +74,11 @@ size_t dma_fence_dedup_array(struct dma_fence **array, size_t num_fences);* Returns NULL on memory allocation failure, a dma_fence object representing * all the given fences otherwise. */ -#define dma_fence_unwrap_merge (...) \ - ({ \ - struct dma_fence *__f[] = { __VA_ARGS__ }; \ - struct dma_fence_unwrap __c[ARRAY_SIZE(__f)]; \ - \ - __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f, __c); \ +#define dma_fence_unwrap_merge (...) \ + ({ \ + struct dma_fence *__f[] = { __VA_ARGS__ }; \ + \ + __dma_fence_unwrap_merge(ARRAY_SIZE(__f), __f); \ })#endif
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] dma-buf: build failure in xe_sync_entry_add_deps() after unwrap macro change --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] dma-buf: build failure in xe_sync_entry_add_deps() after unwrap macro change
--
commit aec8a67605f7a8aa3f0d24386ca64c094e746176
Author: Christian König <[email protected]>
dma-buf: improve robustness of dma_fence_unwrap_for_each
This commit adds a cleanup function for struct dma_fence_unwrap, embeds the cursor inside dma_fence_unwrap_for_each() with __cleanup(), and updates callers to pass two arguments instead of three.
Quoted 29 lines
include/linux/dma-fence-unwrap.h +12
-4@@ -32,27 +32,33 @@ struct dma_fence_unwrap {unsigned int index; }; -struct dma_fence *dma_fence_unwrap_first (struct dma_fence *head, -struct dma_fence_unwrap *cursor); -struct dma_fence *dma_fence_unwrap_next (struct dma_fence_unwrap *cursor); +struct dma_fence_unwrap dma_fence_unwrap_first (struct dma_fence *head, +struct dma_fence **fence); +void dma_fence_unwrap_next (struct dma_fence_unwrap *cursor, +struct dma_fence **fence); +void dma_fence_unwrap_cleanup (struct dma_fence_unwrap *cursor); + +/* Helper for dma_fence_unwrap_for_each(). Internal use only. */ +#define __dma_fence_unwrap_for_each (fence, cursor, head) \ + for (struct dma_fence_unwrap cursor \ + __cleanup(dma_fence_unwrap_cleanup) \ + = dma_fence_unwrap_first(head, &(fence)); fence; \ + dma_fence_unwrap_next(&cursor, &(fence)))/** * dma_fence_unwrap_for_each - iterate over all fences in containers * @fence: current fence -* @cursor: current position inside the containers * @head: starting point for the iterator
include/linux/dma-fence-unwrap.h struct dma_fence_unwrap { · line 58
Quoted 10 lines
include/linux/dma-fence-unwrap.h +2
-3* * Unwrap dma_fence_chain and dma_fence_array containers and deep dive into all * potential fences in them. If @head is just a normal fence only that one is * returned. */ -#define dma_fence_unwrap_for_each (fence, cursor, head) \ - for (fence = dma_fence_unwrap_first(head, cursor); fence; \ - fence = dma_fence_unwrap_next(cursor)) +#define dma_fence_unwrap_for_each (fence, head) \ + __dma_fence_unwrap_for_each(fence, __UNIQUE_ID(cursor), head)
[Severity: High]
Does changing dma_fence_unwrap_for_each() to take two arguments break the build in xe_sync_entry_add_deps() when CONFIG_DRM_XE is enabled?
drivers/gpu/drm/xe/xe_sync.c:xe_sync_entry_add_deps() {
struct dma_fence_unwrap iter;
...
dma_fence_unwrap_for_each(fence, &iter, sync->fence) {
/*
* There is no dma_fence_unwrap_stop() helper to drop the
* reference the iterator holds, so keep iterating without
* doing anything once an error has occurred.
*/
if (err || dma_fence_is_signaled(fence))
continue;
err = drm_sched_job_add_dependency(&job->drm,
dma_fence_get(fence));
}
...
}xe_sync_entry_add_deps() still passes three arguments to dma_fence_unwrap_for_each() and retains the manual error-continuation workaround.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1