dma-fence: Add unwrap mode enum and macro for in-place deduplication
Subsystems (4)
PATCH v3 1/2 dma-fence: Add unwrap mode enum and macro for in-place deduplication
Introduce a new enum dma_fence_unwrap_mode to support multiple modes of fence unwrapping and merging behavior. This lays the groundwork for more flexible and efficient fence operations.
Add a new macro dma_fence_unwrap_shrink_array() to allow callers to unwrap, deduplicate, and sort dma_fence arrays in-place, avoiding additional memory allocations. This mode filters out already signaled fences and keeps only the latest fence per context.
To support this, refactor __dma_fence_unwrap_merge() to handle both merge and shrink modes through the new dma_fence_unwrap_mode parameter. The sorting and deduplication logic is extracted into a new helper function dma_fence_unwrap_dedup_sort().
These changes allow clients to efficiently compact arrays of fences with minimal overhead, improving memory usage and performance in common scenarios like command submission or dependency tracking.
v2: - Export this code from dma-fence-unwrap.c(by Christian).
v3: - To split this in a dma_buf patch and amd userq patch(by Sunil).
- No need to add a new function just re-use existing(by Christian).
--- drivers/dma-buf/dma-fence-unwrap.c | 54 +++++++++++++++++++++--------- include/linux/dma-fence-unwrap.h | 44 ++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 17 deletions(-)
drivers/dma-buf/dma-fence-unwrap.c +39 -15
@@ -79,15 +79,51 @@ static int fence_cmp(const void *_a, const void *_b)return 0 ; } +static void dma_fence_unwrap_dedup_sort (struct dma_fence **array, +unsigned int *count) +{ +int i, j; + +sort (array, *count,sizeof (*array), fence_cmp,NULL ); + +/* +* 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]); +else + array[++j] = array[i]; + } + + *count = ++j; +} +/* 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, -struct dma_fence_unwrap *iter) +struct dma_fence_unwrap *iter, +enum dma_fence_unwrap_mode mode, +unsigned int *out_count) {struct dma_fence *tmp, *unsignaled =NULL , **array;struct dma_fence_array *result;ktime_t timestamp; -int i, j, count; +int i, count; + +if (mode ==DMA_FENCE_UNWRAP_ARRAY ) { + array = fences; + count = num_fences; + +if (count >1 ) +dma_fence_unwrap_dedup_sort (array, &count); + +if (out_count) + *out_count = count; + +return NULL ; + } count =0 ; timestamp =ns_to_ktime (0 );
@@ -141,19 +177,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,if (count ==0 || count ==1 ) goto return_fastpath; -sort (array, count,sizeof (*array), fence_cmp,NULL ); - -/* -* 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]); -else - array[++j] = array[i]; - } - count = ++j; +dma_fence_unwrap_dedup_sort (array, &count);if (count >1 ) { result = dma_fence_array_create(count, array,
include/linux/dma-fence-unwrap.h +42 -2
68 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 2/2 drm/amdgpu: only keep most recent fence for each context
Mesa passes shared bo, fence syncobj to userq_ioctl. There can be duplicates here or some fences that are old. This patch is remove duplicates fence and only keep the most recent fence for each context.
v2: - Export this code from dma-fence-unwrap.c(by Christian).
v3: - To split this in a dma_buf patch and amd userq patch(by Sunil).
- No need to add a new function just re-use existing(by Christian).
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +8 -0
@@ -606,6 +606,7 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,struct drm_gem_object **gobj_read;struct dma_fence **fences =NULL ;u16 num_points, num_fences =0 ; +unsigned int num_shrink_fences =0 ;int r, i, rentry, wentry, cnt;struct drm_exec exec;
@@ -851,6 +852,13 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data, fences[num_fences++] = fence; } +/* +* Remove duplicates fence and only keep the most recent fence for +* each context. +*/ +dma_fence_unwrap_shrink_array (num_fences, fences, num_shrink_fences); + num_fences = num_shrink_fences; + waitq =idr_find (&userq_mgr->userq_idr, wait_info->waitq_id);if (!waitq) goto free_fences;
Signature · 1 line
2.34.1
Christian König
maintainerQuoted 37 lines · Arvind Yadav
Introduce a new
enum dma_fence_unwrap_modeto support multiple modes of fence unwrapping and merging behavior. This lays the groundwork for more flexible and efficient fence operations.Add a new macro
dma_fence_unwrap_shrink_array()to allow callers to unwrap, deduplicate, and sortdma_fencearrays in-place, avoiding additional memory allocations. This mode filters out already signaled fences and keeps only the latest fence per context.To support this, refactor
__dma_fence_unwrap_merge()to handle both merge and shrink modes through the newdma_fence_unwrap_modeparameter. The sorting and deduplication logic is extracted into a new helper functiondma_fence_unwrap_dedup_sort().These changes allow clients to efficiently compact arrays of fences with minimal overhead, improving memory usage and performance in common scenarios like command submission or dependency tracking.
v2: - Export this code from dma-fence-unwrap.c(by Christian).
v3: - To split this in adma_bufpatch and amd userq patch(by Sunil).
- No need to add a new function just re-use existing(by Christian).Cc Alex Deucher <[email protected]>Cc Christian Koenig <[email protected]>Cc Sunil Khatri <[email protected]>Cc Arunpravin Paneer Selvam <[email protected]>Signed-off-by Arvind Yadav <[email protected]>--- drivers/dma-buf/dma-fence-unwrap.c | 54 +++++++++++++++++++++--------- include/linux/dma-fence-unwrap.h | 44 ++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 17 deletions(-)drivers/dma-buf/dma-fence-unwrap.c +0
-0
drivers/dma-buf/dma-fence-unwrap.c static int fence_cmp(const void *_a, const void *_b) · line 83
Quoted 5 lines
drivers/dma-buf/dma-fence-unwrap.c +2
-0@@ -79,15 +79,51 @@ static int fence_cmp(const void *_a, const void *_b)return 0 ; } +static void dma_fence_unwrap_dedup_sort (struct dma_fence **array, +unsigned int *count)
Just call that dma_fence_dedup_array() (e.g. without the unwrap).
Quoted 8 lines
drivers/dma-buf/dma-fence-unwrap.c +8
-0+{ +int i, j; + +sort (array, *count,sizeof (*array), fence_cmp,NULL ); + +/* +* Only keep the most recent fence for each context. +*/
drivers/dma-buf/dma-fence-unwrap.c static int fence_cmp(const void *_a, const void *_b) · line 101
Quoted 10 lines
drivers/dma-buf/dma-fence-unwrap.c +10
-0+ j =0 ; +for (i =1 ; i < *count; i++) { +if (array[i]->context == array[j]->context) +dma_fence_put (array[i]); +else + array[++j] = array[i]; + } + + *count = ++j; +}
Instead of making count a pointer just return the resulting count.
Quoted 17 lines
drivers/dma-buf/dma-fence-unwrap.c +8
-2+/* 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, -struct dma_fence_unwrap *iter) +struct dma_fence_unwrap *iter, +enum dma_fence_unwrap_mode mode, +unsigned int *out_count) {struct dma_fence *tmp, *unsignaled =NULL , **array;struct dma_fence_array *result;ktime_t timestamp; -int i, j, count; +int i, count; + +if (mode ==DMA_FENCE_UNWRAP_ARRAY ) { + array = fences;
drivers/dma-buf/dma-fence-unwrap.c static int fence_cmp(const void *_a, const void *_b) · line 126
Quoted 10 lines
drivers/dma-buf/dma-fence-unwrap.c +10
-0+ count = num_fences; + +if (count >1 ) +dma_fence_unwrap_dedup_sort (array, &count); + +if (out_count) + *out_count = count; + +return NULL ; + }
Completely drop that. Just export the dma_fence_dedup_array() function and use that directly.
Regards,
Christian.
Quoted 100 lines
drivers/dma-buf/dma-fence-unwrap.c +1
-13count =0 ; timestamp =ns_to_ktime (0 );@@ -141,19 +177,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,if (count ==0 || count ==1 ) goto return_fastpath; -sort (array, count,sizeof (*array), fence_cmp,NULL ); - -/* -* 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]); -else - array[++j] = array[i]; - } - count = ++j; +dma_fence_unwrap_dedup_sort (array, &count);if (count >1 ) { result = dma_fence_array_create(count, array,include/linux/dma-fence-unwrap.h +42
-2@@ -10,6 +10,18 @@struct dma_fence ; +/** +* enum dma_fence_unwrap_mode - Mode of operation for fence unwrap and merge +* @DMA_FENCE_UNWRAP_MERGE: Merge all unsignaled fences into a new dma_fence object. +* Returns a single merged fence or stub. +* @DMA_FENCE_UNWRAP_ARRAY: Deduplicate and sort fences in-place. +* Returns nothing, but updates the input array and count. +*/ +enum dma_fence_unwrap_mode { +DMA_FENCE_UNWRAP_MERGE ,/* Return a single merged dma_fence or NULL on error */ +DMA_FENCE_UNWRAP_ARRAY ,/* Return deduplicated, sorted in-place array */ +}; +/** * struct dma_fence_unwrap - cursor into the container structure * @@ -50,7 +62,9 @@ struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor);struct dma_fence *__dma_fence_unwrap_merge (unsigned int num_fences,struct dma_fence **fences, -struct dma_fence_unwrap *cursors); +struct dma_fence_unwrap *cursors, +enum dma_fence_unwrap_mode mode, +unsigned int *out_count);/** * dma_fence_unwrap_merge - unwrap and merge fences @@ -58,6 +72,9 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,* All fences given as parameters are unwrapped and merged back together as flat * dma_fence_array. Useful if multiple containers need to be merged together. * +* Internally uses the DMA_FENCE_UNWRAP_MERGE mode to return a single merged +* dma_fence (or a stub if all fences are signaled). +* * Implemented as a macro to allocate the necessary arrays on the stack and * account the stack frame size to the caller. * @@ -69,7 +86,30 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence *__f[] = { __VA_ARGS__ }; \struct dma_fence_unwrap __c[ARRAY_SIZE (__f)]; \ \ -__dma_fence_unwrap_merge (ARRAY_SIZE (__f), __f, __c); \ +__dma_fence_unwrap_merge (ARRAY_SIZE (__f), __f, __c, \ +DMA_FENCE_UNWRAP_MERGE ,NULL ); \ + }) + +/** +* dma_fence_unwrap_shrink_array - Deduplicate and sort an array of fences in-place +* @ _num_fences: Number of input fences +* @ _fences: Array of dma_fence pointers to be deduplicated and sorted +* @ _out_fences: Output variable updated with the number of fences after deduplication +* +* This macro unwraps each fence (handling dma_fence_array if needed), +* filters out signaled fences, sorts the remaining ones by context and +* timestamp, and removes duplicates (keeping only the most recent fence per context). +* +* Internally uses the DMA_FENCE_UNWRAP_ARRAY mode to perform in-place filtering and +* sorting. +* +* The result is stored back in the input array (_fences) and the final count +* is written to @_out_fences. No memory allocation is performed. +*/ +#define dma_fence_unwrap_shrink_array (_num_fences, _fences, _out_fences) \ + ({ \ + __dma_fence_unwrap_merge((_num_fences), (_fences), NULL, \ + DMA_FENCE_UNWRAP_ARRAY, &_out_fences); \ })#endif
Quoted 41 lines · Christian König
Quoted 40 lines · Arvind Yadav
Introduce a new
enum dma_fence_unwrap_modeto support multiple modes of fence unwrapping and merging behavior. This lays the groundwork for more flexible and efficient fence operations.Add a new macro
dma_fence_unwrap_shrink_array()to allow callers to unwrap, deduplicate, and sortdma_fencearrays in-place, avoiding additional memory allocations. This mode filters out already signaled fences and keeps only the latest fence per context.To support this, refactor
__dma_fence_unwrap_merge()to handle both merge and shrink modes through the newdma_fence_unwrap_modeparameter. The sorting and deduplication logic is extracted into a new helper functiondma_fence_unwrap_dedup_sort().These changes allow clients to efficiently compact arrays of fences with minimal overhead, improving memory usage and performance in common scenarios like command submission or dependency tracking.
v2: - Export this code from dma-fence-unwrap.c(by Christian).
v3: - To split this in adma_bufpatch and amd userq patch(by Sunil).
- No need to add a new function just re-use existing(by Christian).Cc Alex Deucher <[email protected]>Cc Christian Koenig <[email protected]>Cc Sunil Khatri <[email protected]>Cc Arunpravin Paneer Selvam <[email protected]>Signed-off-by Arvind Yadav <[email protected]>--- drivers/dma-buf/dma-fence-unwrap.c | 54 +++++++++++++++++++++--------- include/linux/dma-fence-unwrap.h | 44 ++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 17 deletions(-)drivers/dma-buf/dma-fence-unwrap.c +0
-0@@ -79,15 +79,51 @@ static int fence_cmp(const void *_a, const void *_b)return 0 ; }
Quoted 3 lines
drivers/dma-buf/dma-fence-unwrap.c static int fence_cmp(const void *_a, const void *_b) · line 83
Quoted 2 lines
drivers/dma-buf/dma-fence-unwrap.c +2
-0+static void dma_fence_unwrap_dedup_sort (struct dma_fence **array, +unsigned int *count)Just call that
dma_fence_dedup_array()(e.g. without the unwrap).
Noted.
Quoted 12 lines
Quoted 12 lines
drivers/dma-buf/dma-fence-unwrap.c +11
-0+{ +int i, j; + +sort (array, *count,sizeof (*array), fence_cmp,NULL ); + +/* +* Only keep the most recent fence for each context. +*/ + j =0 ; +for (i =1 ; i < *count; i++) { +if (array[i]->context == array[j]->context)
Quoted 8 lines
drivers/dma-buf/dma-fence-unwrap.c static int fence_cmp(const void *_a, const void *_b) · line 101
Quoted 7 lines
drivers/dma-buf/dma-fence-unwrap.c +7
-0+dma_fence_put (array[i]); + else + array[++j] = array[i]; + } + + *count = ++j; +}Instead of making count a pointer just return the resulting count.
Noted.
Quoted 22 lines
Quoted 22 lines
drivers/dma-buf/dma-fence-unwrap.c +12
-2+/* 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, -struct dma_fence_unwrap *iter) +struct dma_fence_unwrap *iter, +enum dma_fence_unwrap_mode mode, +unsigned int *out_count) {struct dma_fence *tmp, *unsignaled =NULL , **array;struct dma_fence_array *result;ktime_t timestamp; -int i, j, count; +int i, count; + +if (mode ==DMA_FENCE_UNWRAP_ARRAY ) { + array = fences; + count = num_fences; + +if (count >1 ) +dma_fence_unwrap_dedup_sort (array, &count);
Quoted 8 lines
drivers/dma-buf/dma-fence-unwrap.c static int fence_cmp(const void *_a, const void *_b) · line 126
Quoted 6 lines
drivers/dma-buf/dma-fence-unwrap.c +6
-0+ +if (out_count) + *out_count = count; + +return NULL ; + }Completely drop that. Just export the
dma_fence_dedup_array()function and use that directly.
Sure, I will export dma_fence_dedup_array.
Regards,
~arvind
Quoted 104 lines
Regards,
Christian.Quoted 100 lines
drivers/dma-buf/dma-fence-unwrap.c +1
-13count =0 ; timestamp =ns_to_ktime (0 );@@ -141,19 +177,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,if (count ==0 || count ==1 ) goto return_fastpath; -sort (array, count,sizeof (*array), fence_cmp,NULL ); - -/* -* 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]); -else - array[++j] = array[i]; - } - count = ++j; +dma_fence_unwrap_dedup_sort (array, &count);if (count >1 ) { result = dma_fence_array_create(count, array,include/linux/dma-fence-unwrap.h +42
-2@@ -10,6 +10,18 @@struct dma_fence ; +/** +* enum dma_fence_unwrap_mode - Mode of operation for fence unwrap and merge +* @DMA_FENCE_UNWRAP_MERGE: Merge all unsignaled fences into a new dma_fence object. +* Returns a single merged fence or stub. +* @DMA_FENCE_UNWRAP_ARRAY: Deduplicate and sort fences in-place. +* Returns nothing, but updates the input array and count. +*/ +enum dma_fence_unwrap_mode { +DMA_FENCE_UNWRAP_MERGE ,/* Return a single merged dma_fence or NULL on error */ +DMA_FENCE_UNWRAP_ARRAY ,/* Return deduplicated, sorted in-place array */ +}; +/** * struct dma_fence_unwrap - cursor into the container structure * @@ -50,7 +62,9 @@ struct dma_fence *dma_fence_unwrap_next(struct dma_fence_unwrap *cursor);struct dma_fence *__dma_fence_unwrap_merge (unsigned int num_fences,struct dma_fence **fences, -struct dma_fence_unwrap *cursors); +struct dma_fence_unwrap *cursors, +enum dma_fence_unwrap_mode mode, +unsigned int *out_count);/** * dma_fence_unwrap_merge - unwrap and merge fences @@ -58,6 +72,9 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,* All fences given as parameters are unwrapped and merged back together as flat * dma_fence_array. Useful if multiple containers need to be merged together. * +* Internally uses the DMA_FENCE_UNWRAP_MERGE mode to return a single merged +* dma_fence (or a stub if all fences are signaled). +* * Implemented as a macro to allocate the necessary arrays on the stack and * account the stack frame size to the caller. * @@ -69,7 +86,30 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence *__f[] = { __VA_ARGS__ }; \struct dma_fence_unwrap __c[ARRAY_SIZE (__f)]; \ \ -__dma_fence_unwrap_merge (ARRAY_SIZE (__f), __f, __c); \ +__dma_fence_unwrap_merge (ARRAY_SIZE (__f), __f, __c, \ +DMA_FENCE_UNWRAP_MERGE ,NULL ); \ + }) + +/** +* dma_fence_unwrap_shrink_array - Deduplicate and sort an array of fences in-place +* @ _num_fences: Number of input fences +* @ _fences: Array of dma_fence pointers to be deduplicated and sorted +* @ _out_fences: Output variable updated with the number of fences after deduplication +* +* This macro unwraps each fence (handling dma_fence_array if needed), +* filters out signaled fences, sorts the remaining ones by context and +* timestamp, and removes duplicates (keeping only the most recent fence per context). +* +* Internally uses the DMA_FENCE_UNWRAP_ARRAY mode to perform in-place filtering and +* sorting. +* +* The result is stored back in the input array (_fences) and the final count +* is written to @_out_fences. No memory allocation is performed. +*/ +#define dma_fence_unwrap_shrink_array (_num_fences, _fences, _out_fences) \ + ({ \ + __dma_fence_unwrap_merge((_num_fences), (_fences), NULL, \ + DMA_FENCE_UNWRAP_ARRAY, &_out_fences); \ })#endif