dma-fence: Add helper to sort and deduplicate dma_fence arrays
Subsystems (4)
PATCH v4 1/2 dma-fence: Add helper to sort and deduplicate dma_fence arrays
Export a new helper function dma_fence_dedup_array() that sorts an array of dma_fence pointers by context, then deduplicates the array by retaining only the most recent fence per context.
This utility is useful when merging or optimizing sets of fences where redundant entries from the same context can be pruned. The operation is performed in-place and releases references to dropped fences using dma_fence_put().
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).
v4: - Export dma_fence_dedub_array and use it(by Christian).
--- drivers/dma-buf/dma-fence-unwrap.c | 51 ++++++++++++++++++++++-------- include/linux/dma-fence-unwrap.h | 2 ++ 2 files changed, 39 insertions(+), 14 deletions(-)
drivers/dma-buf/dma-fence-unwrap.c +37 -14
@@ -79,6 +79,41 @@ static int fence_cmp(const void *_a, const void *_b)return 0 ; } +/** +* dma_fence_dedup_array - Sort and deduplicate an array of dma_fence pointers +* @fences: Array of dma_fence pointers to be deduplicated +* @num_fences: Number of entries in the @fences array +* +* Sorts the input array by context, then removes duplicate +* fences with the same context, keeping only the most recent one. +* +* The array is modified in-place and unreferenced duplicate fences are released +* via dma_fence_put(). The function returns the new number of fences after +* deduplication. +* +* Return: Number of unique fences remaining in the array. +*/ +int dma_fence_dedup_array (struct dma_fence **fences,int num_fences) +{ +int i, j; + +sort (fences, num_fences,sizeof (*fences), fence_cmp,NULL ); + +/* +* Only keep the most recent fence for each context. +*/ + j =0 ; +for (i =1 ; i < num_fences; i++) { +if (fences[i]->context == fences[j]->context) +dma_fence_put (fences[i]); +else + fences[++j] = fences[i]; + } + +return ++j; +} +EXPORT_SYMBOL_GPL (dma_fence_dedup_array); +/* 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,
@@ -87,7 +122,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence *tmp, *unsignaled =NULL , **array;struct dma_fence_array *result;ktime_t timestamp; -int i, j, count; +int i, count; count =0 ; timestamp =ns_to_ktime (0 );
@@ -141,19 +176,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; + count =dma_fence_dedup_array (array, count);if (count >1 ) { result = dma_fence_array_create(count, array,
include/linux/dma-fence-unwrap.h +2 -0
@@ -52,6 +52,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence **fences, struct dma_fence_unwrap *cursors); +int dma_fence_dedup_array (struct dma_fence **array,int num_fences); +/** * dma_fence_unwrap_merge - unwrap and merge fences *
Signature · 1 line
2.34.1
PATCH v4 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).
v4: Export dma_fence_dedub_array function and used it(by Christian).
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +6 -0
@@ -851,6 +851,12 @@ 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. +*/ + num_fences =dma_fence_dedup_array (fences, num_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
maintainerRe: [PATCH v4 2/2] drm/amdgpu: only keep most recent fence for each context
Quoted 24 lines · Arvind Yadav
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 adma_bufpatch and amd userq patch(by Sunil).
- No need to add a new function just re-use existing(by Christian).
v4: Exportdma_fence_dedub_arrayfunction and used it(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/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data, · line 857
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +4
-0@@ -851,6 +851,12 @@ 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. +*/
Drop or at least change the comment. You should never document what the code does, that should be obvious by reading the code.
Only document why the code does something, e.g. in this case here something like "Keep only the latest fences to reduce the number of values given back to userspace" would be appropriate.
With that done the patch is Reviewed-by: Christian König <[email protected]>.
Regards,
Christian.
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2
-0+ num_fences =dma_fence_dedup_array (fences, num_fences); + waitq =idr_find (&userq_mgr->userq_idr, wait_info->waitq_id);if (!waitq) goto free_fences;
RE: [PATCH v4 2/2] drm/amdgpu: only keep most recent fence for each context
[AMD Official Use Only - AMD Internal Distribution Only]
With the update to commit message, some thing small and precise like Christian recommended sounds good. With that Reviewed-by: Sunil Khatri <[email protected]>
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Friday, May 2, 2025 1:35 PM
To: Yadav, Arvind <[email protected]>; Deucher, Alexander <[email protected]>; Khatri, Sunil <[email protected]>; Paneer Selvam, Arunpravin <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v4 2/2] drm/amdgpu: only keep most recent fence for each context
Quoted 25 lines · Arvind Yadav
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 adma_bufpatch and amd userq patch(by Sunil).
- No need to add a new function just re-use existing(by Christian).
v4: Exportdma_fence_dedub_arrayfunction and used it(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/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data, · line 857
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +4
-0@@ -851,6 +851,12 @@ 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. +*/
Drop or at least change the comment. You should never document what the code does, that should be obvious by reading the code.
Only document why the code does something, e.g. in this case here something like "Keep only the latest fences to reduce the number of values given back to userspace" would be appropriate.
With that done the patch is Reviewed-by: Christian König <[email protected]>.
Regards,
Christian.
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2
-0+ num_fences =dma_fence_dedup_array (fences, num_fences); + waitq =idr_find (&userq_mgr->userq_idr, wait_info->waitq_id);if (!waitq) goto free_fences;
Quoted 14 lines · Arvind Yadav
Export a new helper function
dma_fence_dedup_array()that sorts an array ofdma_fencepointers by context, then deduplicates the array by retaining only the most recent fence per context.This utility is useful when merging or optimizing sets of fences where redundant entries from the same context can be pruned. The operation is performed in-place and releases references to dropped fences using
dma_fence_put().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).
v4: - Exportdma_fence_dedub_arrayand use it(by Christian).
Quoted 5 lines
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]>
Quoted 94 lines
--- drivers/dma-buf/dma-fence-unwrap.c | 51 ++++++++++++++++++++++-------- include/linux/dma-fence-unwrap.h | 2 ++ 2 files changed, 39 insertions(+), 14 deletions(-)drivers/dma-buf/dma-fence-unwrap.c +37
-14@@ -79,6 +79,41 @@ static int fence_cmp(const void *_a, const void *_b)return 0 ; } +/** +* dma_fence_dedup_array - Sort and deduplicate an array of dma_fence pointers +* @fences: Array of dma_fence pointers to be deduplicated +* @num_fences: Number of entries in the @fences array +* +* Sorts the input array by context, then removes duplicate +* fences with the same context, keeping only the most recent one. +* +* The array is modified in-place and unreferenced duplicate fences are released +* via dma_fence_put(). The function returns the new number of fences after +* deduplication. +* +* Return: Number of unique fences remaining in the array. +*/ +int dma_fence_dedup_array (struct dma_fence **fences,int num_fences) +{ +int i, j; + +sort (fences, num_fences,sizeof (*fences), fence_cmp,NULL ); + +/* +* Only keep the most recent fence for each context. +*/ + j =0 ; +for (i =1 ; i < num_fences; i++) { +if (fences[i]->context == fences[j]->context) +dma_fence_put (fences[i]); +else + fences[++j] = fences[i]; + } + +return ++j; +} +EXPORT_SYMBOL_GPL (dma_fence_dedup_array); +/* 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,@@ -87,7 +122,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence *tmp, *unsignaled =NULL , **array;struct dma_fence_array *result;ktime_t timestamp; -int i, j, count; +int i, count; count =0 ; timestamp =ns_to_ktime (0 );@@ -141,19 +176,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; + count =dma_fence_dedup_array (array, count);if (count >1 ) { result = dma_fence_array_create(count, array,include/linux/dma-fence-unwrap.h +2
-0@@ -52,6 +52,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence **fences, struct dma_fence_unwrap *cursors); +int dma_fence_dedup_array (struct dma_fence **array,int num_fences); +/** * dma_fence_unwrap_merge - unwrap and merge fences *
[AMD Official Use Only - AMD Internal Distribution Only]
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Friday, May 2, 2025 1:25 PM
To: Yadav, Arvind <[email protected]>; Deucher, Alexander <[email protected]>; Khatri, Sunil <[email protected]>; Paneer Selvam, Arunpravin <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v4 1/2] dma-fence: Add helper to sort and deduplicate dma_fence arrays
Quoted 14 lines · Arvind Yadav
Export a new helper function
dma_fence_dedup_array()that sorts an array ofdma_fencepointers by context, then deduplicates the array by retaining only the most recent fence per context.This utility is useful when merging or optimizing sets of fences where redundant entries from the same context can be pruned. The operation is performed in-place and releases references to dropped fences using
dma_fence_put().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).
v4: - Exportdma_fence_dedub_arrayand use it(by Christian).
Quoted 5 lines
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]>
Quoted 97 lines
--- drivers/dma-buf/dma-fence-unwrap.c | 51 ++++++++++++++++++++++-------- include/linux/dma-fence-unwrap.h | 2 ++ 2 files changed, 39 insertions(+), 14 deletions(-)drivers/dma-buf/dma-fence-unwrap.c +37
-0@@ -79,6 +79,41 @@ static int fence_cmp(const void *_a, const void *_b)return 0 ; } +/** +* dma_fence_dedup_array - Sort and deduplicate an array of dma_fence pointers +* @fences: Array of dma_fence pointers to be deduplicated +* @num_fences: Number of entries in the @fences array +* +* Sorts the input array by context, then removes duplicate +* fences with the same context, keeping only the most recent one. +* +* The array is modified in-place and unreferenced duplicate fences +are released +* via dma_fence_put(). The function returns the new number of fences +after +* deduplication. +* +* Return: Number of unique fences remaining in the array. +*/ +int dma_fence_dedup_array (struct dma_fence **fences,int num_fences) +{ +int i, j; + +sort (fences, num_fences,sizeof (*fences), fence_cmp,NULL ); + +/* +* Only keep the most recent fence for each context. +*/ + j =0 ; +for (i =1 ; i < num_fences; i++) { +if (fences[i]->context == fences[j]->context) +dma_fence_put (fences[i]); +else + fences[++j] = fences[i]; + } + +return ++j; +} +EXPORT_SYMBOL_GPL (dma_fence_dedup_array); +/* Implementation for the dma_fence_merge() marco, don't use directly */ struct
dma_fence*__dma_fence_unwrap_merge(unsigned intnum_fences,
structdma_fence**fences,drivers/dma-buf/dma-fence-unwrap.c +2
-14@@ -87,7 +122,7 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence *tmp, *unsignaled =NULL , **array;struct dma_fence_array *result;ktime_t timestamp; -int i, j, count; +int i, count; count =0 ; timestamp =ns_to_ktime (0 );@@ -141,19 +176,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; + count =dma_fence_dedup_array (array, count);if (count >1 ) { result = dma_fence_array_create(count, array, diff --git
a/include/linux/dma-fence-unwrap.hb/include/linux/dma-fence-unwrap.hindex66b1e56fbb81..62df222fe0f1100644include/linux/dma-fence-unwrap.h +2
-0@@ -52,6 +52,8 @@ struct dma_fence *__dma_fence_unwrap_merge(unsigned int num_fences,struct dma_fence **fences, struct dma_fence_unwrap *cursors); +int dma_fence_dedup_array (struct dma_fence **array,int num_fences); +/** * dma_fence_unwrap_merge - unwrap and merge fences *