mirror of
git://git.yoctoproject.org/linux-yocto.git
synced 2025-10-22 23:13:01 +02:00
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). Cc: Alex Deucher <alexander.deucher@amd.com> Cc: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com> Reviewed-by: Sunil Khatri <sunil.khatri@amd.com> Reviewed-by: Christian König <christian.koenig@amd.com> Signed-off-by: Arvind Yadav <Arvind.Yadav@amd.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
This commit is contained in:
parent
71353c1a4f
commit
575ec9b0c2
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue
Block a user