dma-buf: add dma_fence_was_initialized function v2

Some driver use fence->ops to test if a fence was initialized or not.
The problem is that this utilizes internal behavior of the dma_fence
implementation.

So better abstract that into a function.

v2: use a flag instead of testing fence->ops, rename the function, move
    to the beginning of the patch set.

Signed-off-by: Christian König <christian.koenig@amd.com>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
Link: https://lore.kernel.org/r/20260120105655.7134-2-christian.koenig@amd.com
This commit is contained in:
Christian König
2025-12-19 11:41:54 +01:00
parent 2bebc88d5e
commit 2bcbc706df
4 changed files with 24 additions and 8 deletions

View File

@@ -1054,7 +1054,7 @@ __dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
fence->lock = lock;
fence->context = context;
fence->seqno = seqno;
fence->flags = flags;
fence->flags = flags | BIT(DMA_FENCE_FLAG_INITIALIZED_BIT);
fence->error = 0;
trace_dma_fence_init(fence);

View File

@@ -282,9 +282,10 @@ void amdgpu_job_free_resources(struct amdgpu_job *job)
unsigned i;
/* Check if any fences were initialized */
if (job->base.s_fence && job->base.s_fence->finished.ops)
if (job->base.s_fence &&
dma_fence_was_initialized(&job->base.s_fence->finished))
f = &job->base.s_fence->finished;
else if (job->hw_fence && job->hw_fence->base.ops)
else if (dma_fence_was_initialized(&job->hw_fence->base))
f = &job->hw_fence->base;
else
f = NULL;
@@ -301,11 +302,11 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
amdgpu_sync_free(&job->explicit_sync);
if (job->hw_fence->base.ops)
if (dma_fence_was_initialized(&job->hw_fence->base))
dma_fence_put(&job->hw_fence->base);
else
kfree(job->hw_fence);
if (job->hw_vm_fence->base.ops)
if (dma_fence_was_initialized(&job->hw_vm_fence->base))
dma_fence_put(&job->hw_vm_fence->base);
else
kfree(job->hw_vm_fence);
@@ -339,11 +340,11 @@ void amdgpu_job_free(struct amdgpu_job *job)
if (job->gang_submit != &job->base.s_fence->scheduled)
dma_fence_put(job->gang_submit);
if (job->hw_fence->base.ops)
if (dma_fence_was_initialized(&job->hw_fence->base))
dma_fence_put(&job->hw_fence->base);
else
kfree(job->hw_fence);
if (job->hw_vm_fence->base.ops)
if (dma_fence_was_initialized(&job->hw_vm_fence->base))
dma_fence_put(&job->hw_vm_fence->base);
else
kfree(job->hw_vm_fence);

View File

@@ -146,7 +146,7 @@ qxl_release_free(struct qxl_device *qdev,
idr_remove(&qdev->release_idr, release->id);
spin_unlock(&qdev->release_idr_lock);
if (release->base.ops) {
if (dma_fence_was_initialized(&release->base)) {
WARN_ON(list_empty(&release->bos));
qxl_release_free_list(release);

View File

@@ -48,6 +48,7 @@ struct seq_file;
* atomic ops (bit_*), so taking the spinlock will not be needed most
* of the time.
*
* DMA_FENCE_FLAG_INITIALIZED_BIT - fence was initialized
* DMA_FENCE_FLAG_SIGNALED_BIT - fence is already signaled
* DMA_FENCE_FLAG_TIMESTAMP_BIT - timestamp recorded for fence signaling
* DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called
@@ -98,6 +99,7 @@ struct dma_fence {
};
enum dma_fence_flag_bits {
DMA_FENCE_FLAG_INITIALIZED_BIT,
DMA_FENCE_FLAG_SEQNO64_BIT,
DMA_FENCE_FLAG_SIGNALED_BIT,
DMA_FENCE_FLAG_TIMESTAMP_BIT,
@@ -263,6 +265,19 @@ void dma_fence_release(struct kref *kref);
void dma_fence_free(struct dma_fence *fence);
void dma_fence_describe(struct dma_fence *fence, struct seq_file *seq);
/**
* dma_fence_was_initialized - test if fence was initialized
* @fence: fence to test
*
* Return: True if fence was ever initialized, false otherwise. Works correctly
* only when memory backing the fence structure is zero initialized on
* allocation.
*/
static inline bool dma_fence_was_initialized(struct dma_fence *fence)
{
return fence && test_bit(DMA_FENCE_FLAG_INITIALIZED_BIT, &fence->flags);
}
/**
* dma_fence_put - decreases refcount of the fence
* @fence: fence to reduce refcount of