Fixes etc for fence merging
cover Fixes etc for fence merging
From: Tvrtko Ursulin <[email protected]>
Consolidated series as an simpler alternative to
https://lore.kernel.org/dri-devel/[email protected]/.
Hopefully fixes https://gitlab.freedesktop.org/drm/amd/-/issues/3617.
First two patches are fixes followed by one optimisation and then Christian's and my selftest additions.
Cover letter now because it grew to five patches.
Please test and/or suggest more test cases to look at.
Christian König (1):
dma-buf: add selftest for fence order after merge
Tvrtko Ursulin (4):
dma-fence: Fix reference leak on fence merge failure path
dma-fence: Use kernel's sort for merging fences
dma-fence: Add a single fence fast path for fence merging
dma-fence: Add some more fence-merge-unwrap tests
drivers/dma-buf/dma-fence-unwrap.c | 137 ++++++------- drivers/dma-buf/st-dma-fence-unwrap.c | 268 +++++++++++++++++++++++++- 2 files changed, 335 insertions(+), 70 deletions(-)
Signature · 1 line
2.46.0
PATCH 1/5 dma-fence: Fix reference leak on fence merge failure path
From: Tvrtko Ursulin <[email protected]>
Release all fence references if the output dma-fence-array could not be allocated.
--- drivers/dma-buf/dma-fence-unwrap.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/dma-buf/dma-fence-unwrap.c +2 -0
@@ -164,6 +164,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,dma_fence_context_alloc (1 ), 1, false);if (!result) { +for (i =0 ; i < count; i++) +dma_fence_put (array[i]); tmp =NULL ; goto return_tmp; }
Signature · 1 line
2.46.0
PATCH 2/5 dma-fence: Use kernel's sort for merging fences
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
One alternative to the fix Christian proposed in
https://lore.kernel.org/dri-devel/[email protected]/
is to replace the rather complex open coded sorting loops with the kernel standard sort followed by a context squashing pass.
Proposed advantage of this would be readability but one concern Christian raised was that there could be many fences, that they are typically mostly sorted, and so the kernel's heap sort would be much worse by the proposed algorithm.
I had a look running some games and vkcube to see what are the typical number of input fences. Tested scenarios:
1) Hogwarts Legacy under Gamescope
450 calls per second to __dma_fence_unwrap_merge.
Percentages per number of fences buckets, before and after checking for signalled status, sorting and flattening:
N Before After
0 0.91%
1 69.40%
2-3 28.72% 9.4% (90.6% resolved to one fence)
4-5 0.93%
6-9 0.03%
10+
2) Cyberpunk 2077 under Gamescope
1050 calls per second, amounting to 0.01% CPU time according to perf top.
N Before After
0 1.13%
1 52.30%
2-3 40.34% 55.57%
4-5 1.46% 0.50% 6-9 2.44% 10+ 2.34%
3) vkcube under Plasma
90 calls per second.
N Before After
0
1
2-3 100% 0% (Ie. all resolved to a single fence)
4-5
6-9
10+
In the case of vkcube all invocations in the 2-3 bucket were actually just two input fences.
From these numbers it looks like the heap sort should not be a disadvantage, given how the dominant case is <= 2 input fences which heap sort solves with just one compare and swap. (And for the case of one input fence we have a fast path in the previous patch.)
A complementary possibility is to implement a different sorting algorithm under the same API as the kernel's sort() and so keep the simplicity, potentially moving the new sort under lib/ if it would be found more widely useful.
v2:
* Hold on to fence references and reduce commentary. (Christian)
* Record and use latest signaled timestamp in the 2nd loop too.
* Consolidate zero or one fences fast paths.
v3:
* Reverse the seqno sort order for a simpler squashing pass. (Christian)
--- drivers/dma-buf/dma-fence-unwrap.c | 128 ++++++++++++++--------------- 1 file changed, 61 insertions(+), 67 deletions(-)
drivers/dma-buf/dma-fence-unwrap.c +61 -67
@@ -12,6 +12,7 @@#include <linux/dma-fence-chain.h> #include <linux/dma-fence-unwrap.h> #include <linux/slab.h> +#include <linux/sort.h> /* Internal helper to start new array iteration, don't use directly */ static struct dma_fence *
@@ -59,6 +60,25 @@ struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor) }EXPORT_SYMBOL_GPL (dma_fence_unwrap_next); + +static int fence_cmp (const void *_a,const void *_b) +{ +struct dma_fence *a = *(struct dma_fence **)_a; +struct dma_fence *b = *(struct dma_fence **)_b; + +if (a->context < b->context) +return -1 ; +else if (a->context > b->context) +return 1 ; + +if (dma_fence_is_later (b, a)) +return 1 ; +else if (dma_fence_is_later (a, b)) +return -1 ; + +return 0 ; +} +/* Implementation for the dma_fence_merge() marco, don't use directly */ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences, struct dma_fence **fences,
@@ -67,8 +87,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence_array *result;struct dma_fence *tmp, **array;ktime_t timestamp; -unsigned int i; -size_t count; +int i, j, count; count =0 ; timestamp =ns_to_ktime (0 );
@@ -96,80 +115,55 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,if (!array)return NULL ; -/* -* This trashes the input fence array and uses it as position for the -* following merge loop. This works because the dma_fence_merge() -* wrapper macro is creating this temporary array on the stack together -* with the iterators. -*/ -for (i =0 ; i < num_fences; ++i) - fences[i] =dma_fence_unwrap_first (fences[i], &iter[i]); - count =0 ; -do { -unsigned int sel; - -restart: - tmp =NULL ; -for (i =0 ; i < num_fences; ++i) { -struct dma_fence *next; - -while (fences[i] &&dma_fence_is_signaled (fences[i])) - fences[i] =dma_fence_unwrap_next (&iter[i]); - - next = fences[i]; -if (!next) -continue ; - -/* -* We can't guarantee that inpute fences are ordered by -* context, but it is still quite likely when this -* function is used multiple times. So attempt to order -* the fences by context as we pass over them and merge -* fences with the same context. -*/ -if (!tmp || tmp->context > next->context) { - tmp = next; - sel = i; - - }else if (tmp->context < next->context) { -continue ; - - }else if (dma_fence_is_later (tmp, next)) { - fences[i] =dma_fence_unwrap_next (&iter[i]); - goto restart; +for (i =0 ; i < num_fences; ++i) { +dma_fence_unwrap_for_each (tmp, &iter[i], fences[i]) { +if (!dma_fence_is_signaled (tmp)) { + array[count++] =dma_fence_get (tmp); }else { - fences[sel] =dma_fence_unwrap_next (&iter[sel]); - goto restart; +ktime_t t =dma_fence_timestamp (tmp); + +if (ktime_after (t, timestamp)) + timestamp = t; } } - -if (tmp) { - array[count++] =dma_fence_get (tmp); - fences[sel] =dma_fence_unwrap_next (&iter[sel]); - } - }while (tmp); - -if (count ==0 ) { - tmp =dma_fence_allocate_private_stub (ktime_get ()); - goto return_tmp; } -if (count ==1 ) { - tmp = array[0 ]; - goto return_tmp; - } +if (count ==0 || count ==1 ) + goto return_fastpath; + +sort (array, count,sizeof (*array), fence_cmp,NULL ); - result =dma_fence_array_create (count, array, -dma_fence_context_alloc (1 ), -1 , false); -if (!result) { -for (i =0 ; i < count; i++) +/* +* Only keep the most recent fence for each context. +*/ + j =0 ; +for (i =1 ; i < count; i++) { +if (array[i]->context == array[j]->context)dma_fence_put (array[i]); - tmp =NULL ; - goto return_tmp; +else + array[++j] = array[i]; } -return &result->base; + count = ++j; + +if (count >1 ) { + result =dma_fence_array_create (count, array, +dma_fence_context_alloc (1 ), +1 , false); +if (!result) { +for (i =0 ; i < count; i++) +dma_fence_put (array[i]); + tmp =NULL ; + goto return_tmp; + } +return &result->base; + } + +return_fastpath: +if (count ==0 ) + tmp =dma_fence_allocate_private_stub (timestamp); +else + tmp = array[0 ]; return_tmp:kfree (array);
Signature · 1 line
2.46.0
PATCH 3/5 dma-fence: Add a single fence fast path for fence merging
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Testing some workloads in two different scenarios, such as games running under Gamescope on a Steam Deck, or vkcube under a Plasma desktop, shows that in a significant portion of calls the dma_fence_unwrap_merge helper is called with just a single unsignalled fence.
Therefore it is worthile to add a fast path for that case and so bypass the memory allocation and insertion sort attempts.
Tested scenarios:
1) Hogwarts Legacy under Gamescope
~1500 calls per second to __dma_fence_unwrap_merge.
Percentages per number of fences buckets, before and after checking for signalled status, sorting and flattening:
N Before After
0 0.85%
1 69.80% -> The new fast path. 2-9 29.36% 9% (Ie. 91% of this bucket flattened to 1 fence)
10-19
20-40
50+
2) Cyberpunk 2077 under Gamescope
~2400 calls per second.
N Before After
0 0.71%
1 52.53% -> The new fast path. 2-9 44.38% 50.60% (Ie. half resolved to a single fence)
10-19 2.34%
20-40 0.06%
50+
3) vkcube under Plasma
90 calls per second.
N Before After
0
1
2-9 100% 0% (Ie. all resolved to a single fence)
10-19
20-40
50+
In the case of vkcube all invocations in the 2-9 bucket were actually just two input fences.
v2:
* Correct local variable name and hold on to unsignaled reference. (Chistian)
--- drivers/dma-buf/dma-fence-unwrap.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-fence-unwrap.c +10 -1
@@ -84,8 +84,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence **fences, struct dma_fence_unwrap *iter) { + struct dma_fence *tmp, *unsignaled = NULL, **array;struct dma_fence_array *result; -struct dma_fence *tmp, **array;ktime_t timestamp;int i, j, count;
@@ -94,6 +94,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,for (i =0 ; i < num_fences; ++i) {dma_fence_unwrap_for_each (tmp, &iter[i], fences[i]) {if (!dma_fence_is_signaled (tmp)) { +dma_fence_put (unsignaled); + unsignaled =dma_fence_get (tmp); ++count; }else {ktime_t t =dma_fence_timestamp (tmp);
@@ -107,9 +109,16 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,/* * If we couldn't find a pending fence just return a private signaled * fence with the timestamp of the last signaled one. +* +* Or if there was a single unsignaled fence left we can return it +* directly and early since that is a major path on many workloads. */ if (count ==0 )return dma_fence_allocate_private_stub (timestamp); +else if (count ==1 ) +return unsignaled; + +dma_fence_put (unsignaled); array =kmalloc_array (count,sizeof (*array),GFP_KERNEL );if (!array)
Signature · 1 line
2.46.0
Re: [PATCH 3/5] dma-fence: Add a single fence fast path for fence merging
Christian - it looks this patch could be merged now.
Thanks,
Tvrtko
Quoted 104 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
Testing some workloads in two different scenarios, such as games running under Gamescope on a Steam Deck, or vkcube under a Plasma desktop, shows that in a significant portion of calls the
dma_fence_unwrap_mergehelper is called with just a single unsignalled fence.Therefore it is worthile to add a fast path for that case and so bypass the memory allocation and insertion sort attempts.
Tested scenarios:
1) Hogwarts Legacy under Gamescope
~1500 calls per second to
__dma_fence_unwrap_merge.Percentages per number of fences buckets, before and after checking for signalled status, sorting and flattening:
N Before After 0 0.85% 1 69.80% -> The new fast path.2-9 29.36% 9% (Ie. 91% of this bucket flattened to 1 fence)
10-19
20-40
50+2) Cyberpunk 2077 under Gamescope
~2400 calls per second.
N Before After 0 0.71% 1 52.53% -> The new fast path.2-9 44.38% 50.60% (Ie. half resolved to a single fence)
10-19 2.34%
20-40 0.06%
50+3) vkcube under Plasma
90 calls per second.
N Before After 0 12-9 100% 0% (Ie. all resolved to a single fence)
10-19
20-40
50+In the case of vkcube all invocations in the 2-9 bucket were actually just two input fences.
v2:
* Correct local variable name and hold on to unsignaled reference. (Chistian)Signed-off-by Tvrtko Ursulin <[email protected]>Cc Christian König <[email protected]>Cc Friedrich Vock <[email protected]>--- drivers/dma-buf/dma-fence-unwrap.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)drivers/dma-buf/dma-fence-unwrap.c +10
-1@@ -84,8 +84,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence **fences, struct dma_fence_unwrap *iter) { + struct dma_fence *tmp, *unsignaled = NULL, **array;struct dma_fence_array *result; -struct dma_fence *tmp, **array;ktime_t timestamp;int i, j, count;@@ -94,6 +94,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,for (i =0 ; i < num_fences; ++i) {dma_fence_unwrap_for_each (tmp, &iter[i], fences[i]) {if (!dma_fence_is_signaled (tmp)) { +dma_fence_put (unsignaled); + unsignaled =dma_fence_get (tmp); ++count; }else {ktime_t t =dma_fence_timestamp (tmp);@@ -107,9 +109,16 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,/* * If we couldn't find a pending fence just return a private signaled * fence with the timestamp of the last signaled one. +* +* Or if there was a single unsignaled fence left we can return it +* directly and early since that is a major path on many workloads. */ if (count ==0 )return dma_fence_allocate_private_stub (timestamp); +else if (count ==1 ) +return unsignaled; + +dma_fence_put (unsignaled); array =kmalloc_array (count,sizeof (*array),GFP_KERNEL );if (!array)
Christian König
maintainerRe: [PATCH 3/5] dma-fence: Add a single fence fast path for fence merging
And pushed to drm-misc-next.
Sorry I'm still catching up from the holidays,
Christian.
Am 09.01.25 um 11:53 schrieb Tvrtko Ursulin:
Quoted 112 lines
Christian - it looks this patch could be merged now.
Thanks,
Tvrtko
Quoted 104 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
Testing some workloads in two different scenarios, such as games running under Gamescope on a Steam Deck, or vkcube under a Plasma desktop, shows that in a significant portion of calls the
dma_fence_unwrap_mergehelper is called with just a single unsignalled fence.Therefore it is worthile to add a fast path for that case and so bypass the memory allocation and insertion sort attempts.
Tested scenarios:
1) Hogwarts Legacy under Gamescope
~1500 calls per second to
__dma_fence_unwrap_merge.Percentages per number of fences buckets, before and after checking for signalled status, sorting and flattening:
N Before After
0 0.85%
1 69.80% -> The new fast path.
2-9 29.36% 9% (Ie. 91% of this bucket flattened to 1 fence) 10-19
20-40
50+2) Cyberpunk 2077 under Gamescope
~2400 calls per second.
N Before After
0 0.71%
1 52.53% -> The new fast path.
2-9 44.38% 50.60% (Ie. half resolved to a single fence) 10-19 2.34%
20-40 0.06%
50+3) vkcube under Plasma
90 calls per second.
N Before After
0
1
2-9 100% 0% (Ie. all resolved to a single fence) 10-19
20-40
50+In the case of vkcube all invocations in the 2-9 bucket were actually just two input fences.
v2:
* Correct local variable name and hold on to unsignaled reference. (Chistian)Signed-off-by Tvrtko Ursulin <[email protected]>Cc Christian König <[email protected]>Cc Friedrich Vock <[email protected]>---
drivers/dma-buf/dma-fence-unwrap.c| 11 ++++++++++-1 file changed, 10 insertions(+), 1 deletion(-)drivers/dma-buf/dma-fence-unwrap.c +1
-1@@ -84,8 +84,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences, struct dma_fence **fences, struct dma_fence_unwrap *iter) { + struct dma_fence *tmp, *unsignaled = NULL, **array; struct dma_fence_array *result; - struct dma_fence *tmp, **array; ktime_t timestamp; int i, j, count;@@ -94,6 +94,8 @@ struct
dma_fence*__dma_fence_unwrap_merge(unsigned intnum_fences, for (i = 0; i <num_fences; ++i) {
dma_fence_unwrap_for_each(tmp, &iter[i], fences[i]){
if (!dma_fence_is_signaled(tmp)) {
+dma_fence_put(unsignaled);
+ unsignaled =dma_fence_get(tmp);
++count;
} else {
ktime_tt =dma_fence_timestamp(tmp);drivers/dma-buf/dma-fence-unwrap.c +7
-0@@ -107,9 +109,16 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences, /* * If we couldn't find a pending fence just return a private signaled * fence with the timestamp of the last signaled one. + * + * Or if there was a single unsignaled fence left we can return it + * directly and early since that is a major path on many workloads. */ if (count == 0) return dma_fence_allocate_private_stub(timestamp); + else if (count == 1) + return unsignaled; + + dma_fence_put(unsignaled); array = kmalloc_array(count, sizeof(*array), GFP_KERNEL); if (!array)
Re: [PATCH 3/5] dma-fence: Add a single fence fast path for fence merging
Quoted 3 lines · Christian König
And pushed to drm-misc-next.
Sorry I'm still catching up from the holidays,
Thank you! I afforded myself an extra ping with all this talk about unreliable/corrupt email.
Regards,
Tvrtko
Quoted 114 lines
Am 09.01.25 um 11:53 schrieb Tvrtko Ursulin:
Quoted 112 lines
Christian - it looks this patch could be merged now.
Thanks,
Tvrtko
Quoted 104 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
Testing some workloads in two different scenarios, such as games running under Gamescope on a Steam Deck, or vkcube under a Plasma desktop, shows that in a significant portion of calls the
dma_fence_unwrap_mergehelper is called with just a single unsignalled fence.Therefore it is worthile to add a fast path for that case and so bypass the memory allocation and insertion sort attempts.
Tested scenarios:
1) Hogwarts Legacy under Gamescope
~1500 calls per second to
__dma_fence_unwrap_merge.Percentages per number of fences buckets, before and after checking for signalled status, sorting and flattening:
N Before After
0 0.85%
1 69.80% -> The new fast path.
2-9 29.36% 9% (Ie. 91% of this bucket flattened to 1 fence) 10-19
20-40
50+2) Cyberpunk 2077 under Gamescope
~2400 calls per second.
N Before After
0 0.71%
1 52.53% -> The new fast path.
2-9 44.38% 50.60% (Ie. half resolved to a single fence) 10-19 2.34%
20-40 0.06%
50+3) vkcube under Plasma
90 calls per second.
N Before After
0
1
2-9 100% 0% (Ie. all resolved to a single fence) 10-19
20-40
50+In the case of vkcube all invocations in the 2-9 bucket were actually just two input fences.
v2:
* Correct local variable name and hold on to unsignaled reference. (Chistian)Signed-off-by Tvrtko Ursulin <[email protected]>Cc Christian König <[email protected]>Cc Friedrich Vock <[email protected]>---
drivers/dma-buf/dma-fence-unwrap.c| 11 ++++++++++-1 file changed, 10 insertions(+), 1 deletion(-)drivers/dma-buf/dma-fence-unwrap.c +1
-1@@ -84,8 +84,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences, struct dma_fence **fences, struct dma_fence_unwrap *iter) { + struct dma_fence *tmp, *unsignaled = NULL, **array; struct dma_fence_array *result; - struct dma_fence *tmp, **array; ktime_t timestamp; int i, j, count;@@ -94,6 +94,8 @@ struct
dma_fence*__dma_fence_unwrap_merge(unsigned intnum_fences, for (i = 0; i <num_fences; ++i) {
dma_fence_unwrap_for_each(tmp, &iter[i], fences[i]){
if (!dma_fence_is_signaled(tmp)) {
+dma_fence_put(unsignaled);
+ unsignaled =dma_fence_get(tmp);
++count;
} else {
ktime_tt =dma_fence_timestamp(tmp);drivers/dma-buf/dma-fence-unwrap.c +7
-0@@ -107,9 +109,16 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences, /* * If we couldn't find a pending fence just return a private signaled * fence with the timestamp of the last signaled one. + * + * Or if there was a single unsignaled fence left we can return it + * directly and early since that is a major path on many workloads. */ if (count == 0) return dma_fence_allocate_private_stub(timestamp); + else if (count == 1) + return unsignaled; + + dma_fence_put(unsignaled); array = kmalloc_array(count, sizeof(*array), GFP_KERNEL); if (!array)
PATCH 4/5 dma-buf: add selftest for fence order after merge
In reply to Tvrtko UrsulinFrom: Christian König <[email protected]>
Add a test which double checks that fences are in the expected order after a merge.
While at it also switch to using a mock array for the complex test instead of a merge.
--- drivers/dma-buf/st-dma-fence-unwrap.c | 69 ++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-)
drivers/dma-buf/st-dma-fence-unwrap.c +68 -1
@@ -304,6 +304,72 @@ static int unwrap_merge(void *arg)return err; } +static int unwrap_merge_order (void *arg) +{ +struct dma_fence *fence, *f1, *f2, *a1, *a2, *c1, *c2; +struct dma_fence_unwrap iter; +int err =0 ; + + f1 =mock_fence (); +if (!f1) +return -ENOMEM ; + +dma_fence_enable_sw_signaling (f1); + + f2 =mock_fence (); +if (!f2) { +dma_fence_put (f1); +return -ENOMEM ; + } + +dma_fence_enable_sw_signaling (f2); + + a1 =mock_array (2 , f1, f2); +if (!a1) +return -ENOMEM ; + + c1 =mock_chain (NULL ,dma_fence_get (f1)); +if (!c1) + goto error_put_a1; + + c2 =mock_chain (c1,dma_fence_get (f2)); +if (!c2) + goto error_put_a1; + +/* +* The fences in the chain are the same as in a1 but in oposite order, +* the dma_fence_merge() function should be able to handle that. +*/ + a2 =dma_fence_unwrap_merge (a1, c2); + +dma_fence_unwrap_for_each (fence, &iter, a2) { +if (fence == f1) { + f1 =NULL ; +if (!f2) +pr_err ("Unexpected order!\n" ); + }else if (fence == f2) { + f2 =NULL ; +if (f1) +pr_err ("Unexpected order!\n" ); + }else { +pr_err ("Unexpected fence!\n" ); + err = -EINVAL ; + } + } + +if (f1 || f2) { +pr_err ("Not all fences seen!\n" ); + err = -EINVAL ; + } + +dma_fence_put (a2); +return err; + +error_put_a1: +dma_fence_put (a1); +return -ENOMEM ; +} +static int unwrap_merge_complex (void *arg) {struct dma_fence *fence, *f1, *f2, *f3, *f4, *f5;
@@ -327,7 +393,7 @@ static int unwrap_merge_complex(void *arg) goto error_put_f2;/* The resulting array has the fences in reverse */ - f4 =dma_fence_unwrap_merge (f2, f1); + f4 =mock_array (2 ,dma_fence_get (f2),dma_fence_get (f1));if (!f4) goto error_put_f3;
@@ -375,6 +441,7 @@ int dma_fence_unwrap(void)SUBTEST (unwrap_chain),SUBTEST (unwrap_chain_array),SUBTEST (unwrap_merge), +SUBTEST (unwrap_merge_order), SUBTEST(unwrap_merge_complex), };
Signature · 1 line
2.46.0
PATCH 5/5 dma-fence: Add some more fence-merge-unwrap tests
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
So far all tests use seqno one and only vary the context. Lets add some tests which vary the seqno too.
--- drivers/dma-buf/st-dma-fence-unwrap.c | 199 +++++++++++++++++++++++++- 1 file changed, 196 insertions(+), 3 deletions(-)
drivers/dma-buf/st-dma-fence-unwrap.c +196 -3
@@ -28,7 +28,7 @@ static const struct dma_fence_ops mock_ops = { .get_timeline_name = mock_name, }; -static struct dma_fence *mock_fence (void ) +static struct dma_fence *__mock_fence (u64 context,u64 seqno) {struct mock_fence *f;
@@ -37,12 +37,16 @@ static struct dma_fence *mock_fence(void)return NULL ;spin_lock_init (&f->lock); -dma_fence_init (&f->base, &mock_ops, &f->lock, -dma_fence_context_alloc (1 ),1 ); +dma_fence_init (&f->base, &mock_ops, &f->lock, context, seqno);return &f->base; } +static struct dma_fence *mock_fence (void ) +{ +return __mock_fence (dma_fence_context_alloc (1 ),1 ); +} +static struct dma_fence *mock_array (unsigned int num_fences, ...) {struct dma_fence_array *array;
@@ -304,6 +308,111 @@ static int unwrap_merge(void *arg)return err; } +static int unwrap_merge_duplicate (void *arg) +{ +struct dma_fence *fence, *f1, *f2; +struct dma_fence_unwrap iter; +int err =0 ; + + f1 =mock_fence (); +if (!f1) +return -ENOMEM ; + +dma_fence_enable_sw_signaling (f1); + + f2 =dma_fence_unwrap_merge (f1, f1); +if (!f2) { + err = -ENOMEM ; + goto error_put_f1; + } + +dma_fence_unwrap_for_each (fence, &iter, f2) { +if (fence == f1) { +dma_fence_put (f1); + f1 =NULL ; + }else { +pr_err ("Unexpected fence!\n" ); + err = -EINVAL ; + } + } + +if (f1) { +pr_err ("Not all fences seen!\n" ); + err = -EINVAL ; + } + +dma_fence_put (f2); +error_put_f1: +dma_fence_put (f1); +return err; +} + +static int unwrap_merge_seqno (void *arg) +{ +struct dma_fence *fence, *f1, *f2, *f3, *f4; +struct dma_fence_unwrap iter; +int err =0 ; +u64 ctx[2 ]; + + ctx[0 ] =dma_fence_context_alloc (1 ); + ctx[1 ] =dma_fence_context_alloc (1 ); + + f1 =__mock_fence (ctx[1 ],1 ); +if (!f1) +return -ENOMEM ; + +dma_fence_enable_sw_signaling (f1); + + f2 =__mock_fence (ctx[1 ],2 ); +if (!f2) { + err = -ENOMEM ; + goto error_put_f1; + } + +dma_fence_enable_sw_signaling (f2); + + f3 =__mock_fence (ctx[0 ],1 ); +if (!f3) { + err = -ENOMEM ; + goto error_put_f2; + } + +dma_fence_enable_sw_signaling (f3); + + f4 =dma_fence_unwrap_merge (f1, f2, f3); +if (!f4) { + err = -ENOMEM ; + goto error_put_f3; + } + +dma_fence_unwrap_for_each (fence, &iter, f4) { +if (fence == f3 && f2) { +dma_fence_put (f3); + f3 =NULL ; + }else if (fence == f2 && !f3) { +dma_fence_put (f2); + f2 =NULL ; + }else { +pr_err ("Unexpected fence!\n" ); + err = -EINVAL ; + } + } + +if (f2 || f3) { +pr_err ("Not all fences seen!\n" ); + err = -EINVAL ; + } + +dma_fence_put (f4); +error_put_f3: +dma_fence_put (f3); +error_put_f2: +dma_fence_put (f2); +error_put_f1: +dma_fence_put (f1); +return err; +} +static int unwrap_merge_order (void *arg) {struct dma_fence *fence, *f1, *f2, *a1, *a2, *c1, *c2;
@@ -433,6 +542,87 @@ static int unwrap_merge_complex(void *arg)return err; } +static int unwrap_merge_complex_seqno (void *arg) +{ +struct dma_fence *fence, *f1, *f2, *f3, *f4, *f5, *f6, *f7; +struct dma_fence_unwrap iter; +int err = -ENOMEM ; +u64 ctx[2 ]; + + ctx[0 ] =dma_fence_context_alloc (1 ); + ctx[1 ] =dma_fence_context_alloc (1 ); + + f1 =__mock_fence (ctx[0 ],2 ); +if (!f1) +return -ENOMEM ; + +dma_fence_enable_sw_signaling (f1); + + f2 =__mock_fence (ctx[1 ],1 ); +if (!f2) + goto error_put_f1; + +dma_fence_enable_sw_signaling (f2); + + f3 =__mock_fence (ctx[0 ],1 ); +if (!f3) + goto error_put_f2; + +dma_fence_enable_sw_signaling (f3); + + f4 =__mock_fence (ctx[1 ],2 ); +if (!f4) + goto error_put_f3; + +dma_fence_enable_sw_signaling (f4); + + f5 =mock_array (2 ,dma_fence_get (f1),dma_fence_get (f2)); +if (!f5) + goto error_put_f4; + + f6 =mock_array (2 ,dma_fence_get (f3),dma_fence_get (f4)); +if (!f6) + goto error_put_f5; + + f7 =dma_fence_unwrap_merge (f5, f6); +if (!f7) + goto error_put_f6; + + err =0 ; +dma_fence_unwrap_for_each (fence, &iter, f7) { +if (fence == f1 && f4) { +dma_fence_put (f1); + f1 =NULL ; + }else if (fence == f4 && !f1) { +dma_fence_put (f4); + f4 =NULL ; + }else { +pr_err ("Unexpected fence!\n" ); + err = -EINVAL ; + } + } + +if (f1 || f4) { +pr_err ("Not all fences seen!\n" ); + err = -EINVAL ; + } + +dma_fence_put (f7); +error_put_f6: +dma_fence_put (f6); +error_put_f5: +dma_fence_put (f5); +error_put_f4: +dma_fence_put (f4); +error_put_f3: +dma_fence_put (f3); +error_put_f2: +dma_fence_put (f2); +error_put_f1: +dma_fence_put (f1); +return err; +} +int dma_fence_unwrap (void ) {static const struct subtest tests[] = {
@@ -441,8 +631,11 @@ int dma_fence_unwrap(void)SUBTEST (unwrap_chain),SUBTEST (unwrap_chain_array),SUBTEST (unwrap_merge), +SUBTEST (unwrap_merge_duplicate), +SUBTEST (unwrap_merge_seqno),SUBTEST (unwrap_merge_order),SUBTEST (unwrap_merge_complex), + SUBTEST(unwrap_merge_complex_seqno), };return subtests (tests,NULL );
Signature · 1 line
2.46.0
Am 15.11.24 um 11:21 schrieb Tvrtko Ursulin:
Quoted 10 lines
From: Tvrtko Ursulin <[email protected]>
Consolidated series as an simpler alternative to
https://lore.kernel.org/dri-devel/[email protected]/.Hopefully fixes https://gitlab.freedesktop.org/drm/amd/-/issues/3617.
First two patches are fixes followed by one optimisation and then Christian's and my selftest additions.
Quoted 6 lines
Cover letter now because it grew to five patches.
Please test and/or suggest more test cases to look at.
Cc Christian König <[email protected]>Cc Friedrich Vock <[email protected]
I've already pushed the first two patch to drm-misc-fixes on Friday, going to request a backmerge as soon as those land upstream so that we can push drm-misc-next as well.
Quoted 14 lines
Christian König (1):
dma-buf: add selftest for fence order after mergeTvrtko Ursulin (4):
dma-fence: Fix reference leak on fence merge failure path
dma-fence: Use kernel's sort for merging fences
dma-fence: Add a single fence fast path for fence merging
dma-fence: Add some more fence-merge-unwrap testsdrivers/dma-buf/dma-fence-unwrap.c | 137 ++++++------- drivers/dma-buf/st-dma-fence-unwrap.c | 268 +++++++++++++++++++++++++- 2 files changed, 335 insertions(+), 70 deletions(-)
Quoted 13 lines · Christian König
Am 15.11.24 um 11:21 schrieb Tvrtko Ursulin:
Quoted 12 lines
From: Tvrtko Ursulin <[email protected]>
Consolidated series as an simpler alternative to
https://lore.kernel.org/dri-devel/[email protected]/.Hopefully fixes https://gitlab.freedesktop.org/drm/amd/-/issues/3617.
First two patches are fixes followed by one optimisation and then Christian's and my selftest additions.
Cover letter now because it grew to five patches.
Quoted 8 lines
Quoted 4 lines
Please test and/or suggest more test cases to look at.
Cc Christian König <[email protected]>Cc Friedrich Vock <[email protected]I've already pushed the first two patch to drm-misc-fixes on Friday, going to request a backmerge as soon as those land upstream so that we can push drm-misc-next as well.
Looks like drm-misc-next is ready to take this.
Regards,
Tvrtko