On 26/11/2026 15:59, Philipp Stanner wrote:
On Thu, 2026-11-26 at 15:42 +0000, Tvrtko Ursulin wrote:
Philipp reports the current usage of overwriting the dma-fence timestamp
from the free_job worker is racy and unsafe due missing memory ordering
and especially with drivers which use own unordered submit workqueue for
the scheduler. Consider also that the timestamp field is the union in the
dma-fence object.
Yeah, the union thing is also something I realized during the last
days. If that should ever be read misordered it would blow up the
entire scheduling algorithm because the fence's callback-list pointer
would be interpreted as a timestamp…
Algorithm? More like the kernel. :)
On top of these serious issue there is also a smaller problem where the
said race can cause GPU time overaccounting.
Lets try and solve all issues in one swoop by stopping messing with
dma-fence internals and just track what we want in our own derived fence
object.
We care about fence submit and finish times and by adding a dedicated
lock to manage them we can safely access both and adjust the former
without any races.
Signed-off-by: Tvrtko Ursulin <[email protected]>
References:
https://lore.kernel.org/dri-devel/[email protected]/
After this thread, I started investigating timestamp-ordering in fence.
I have ideas how we might fix it and have started preparing patches,
also one that would touch the scheduler. But yours here seems to do
more of the heavy lifting.
References:
https://lore.kernel.org/dri-devel/[email protected]/
Cc: Christian König <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Philipp Stanner <[email protected]>
Cc: [email protected]
---
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 15 ++++----
drivers/gpu/drm/scheduler/sched_entity.c | 6 ++--
drivers/gpu/drm/scheduler/sched_fence.c | 20 +++++++++--
drivers/gpu/drm/scheduler/sched_internal.h | 41 ++++++++++++++++++++++
drivers/gpu/drm/scheduler/sched_main.c | 8 ++---
include/drm/gpu_scheduler.h | 39 ++++++++++++++++++++
6 files changed, 113 insertions(+), 16 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
index 2f0349233695..1a6985283acc 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -171,21 +171,20 @@ static unsigned int amdgpu_ctx_get_hw_prio(struct
amdgpu_ctx *ctx, u32 hw_ip)
static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence)
{
struct drm_sched_fence *s_fence;
+ ktime_t start, end;
if (!fence)
return ns_to_ktime(0);
- /* When the fence is not even scheduled it can't have spend time */
s_fence = to_drm_sched_fence(fence);
- if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->scheduled.flags))
- return ns_to_ktime(0);
+ drm_sched_fence_get_times(s_fence, &start, &end);
+
+ /* When the fence is not even scheduled it can't have spend time */
+ if (!start)
+ return start;
/* When it is still running account how much already spend */
- if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->finished.flags))
- return ktime_sub(ktime_get(), s_fence->scheduled.timestamp);
-
- return ktime_sub(s_fence->finished.timestamp,
- s_fence->scheduled.timestamp);
+ return ktime_sub(end ?: ktime_get(), start);
That's also a sequence I worked on.
First I wanted to fully lock dma_fence_timestamp() and use it here.
Right, I though as long as we need to lock something we might as well
lock our own thing and then no need to come up with API. Existing
dma_fence_timestamp is also a quite an ugly function.
But guess what, I then couldn't use it because amdgpu_ctx_fence_time()
is called under fctx lock, which is the same as the fence lock ->
deadlock.
Obviously then you need an unlocked version of the API! :D
[…]
void drm_sched_fence_scheduled(struct drm_sched_fence *fence,
struct dma_fence *parent)
{
+ ktime_t now = ktime_get();
+ unsigned long flags;
+
/* Set the parent before signaling the scheduled fence, such that,
* any waiter expecting the parent to be filled after the job has
* been scheduled (which is the case for drivers delegating waits
@@ -74,14 +77,26 @@ void drm_sched_fence_scheduled(struct drm_sched_fence
*fence,
if (!IS_ERR_OR_NULL(parent))
drm_sched_fence_set_parent(fence, parent);
- dma_fence_signal(&fence->scheduled);
+ spin_lock_irqsave(&fence->ts_lock, flags);
+ fence->start_ts = now;
+ spin_unlock_irqrestore(&fence->ts_lock, flags);
I get what you are doing here. I think it'll work, and also it's
partially necessary because we should not overwrite a dma_fence's
timestamp.
Nevertheless, it seems that you don't use dma_fence_timestamp() at all
anymore. Not even in amdgpu above.
Yep, that's another benefit. As said, it's one ugly function.
So we are then tempted to wonder what dma_fence->timestamp is even good
for anymore, and if we're running danger of re-implementing parts of
the fence functionality in sched_fence?
Same as the pesky timeline and driver names. It's ABI courtesy of
sync_file. :(
Christian?
+
+ dma_fence_signal_timestamp(&fence->scheduled, now);
You write the fence timestamp, but it seems it's not read anymore since
sched_fence now does that? IOW, whom are you setting the timestamp for,
who will ever evaluate it who shouldn't instead evaluate the time
stamps of sched_fence?
This is not really required, it could have stayed as dma_fence_signal.
It's only so the two timestamps are aligned, but again, it would have no
ill effect if they were different.
}
void drm_sched_fence_finished(struct drm_sched_fence *fence, int result)
{
+ ktime_t now = ktime_get();
+ unsigned long flags;
+
if (result)
dma_fence_set_error(&fence->finished, result);
- dma_fence_signal(&fence->finished);
+
+ spin_lock_irqsave(&fence->ts_lock, flags);
+ fence->finished_ts = now;
+ spin_unlock_irqrestore(&fence->ts_lock, flags);
+
+ dma_fence_signal_timestamp(&fence->finished, now);
input from Christian will be needed; just recently we were envisioning
to reduce the number of signal-functions.
If you can wait a few days I would also like to present an RFC for the
memory ordering in dma_fence_timestamp().
Sure. The over-accounting race I found is not that big deal for now.
Tvrtko