dma_fence timestamps could so far be written and read without any memory ordering, a bug that presumably got overlooked because x86 is strongly ordered. On more weakly ordered architectures, however, bugs could arise at any moment since this is UB.
The current misordering allows for the bit DMA_FENCE_FLAG_TIMESTAMP_BIT to be read as true before the union containing the timestamp actually has the timestamp set. drm_sched currently (sort of illegally) overwrites a fence's timestamp. To do that somewhat safely, you have to wait for the timestamp to be present, otherwise you overwrite another member of the union whose memory the timestamp shares. Ensure correct ordering in the scheduler's timestamp evaluation through test_bit_acquire(). Cc: [email protected] Fixes: f7fe64ad0f22 ("drm/sched: Split free_job into own work item") Signed-off-by: Philipp Stanner <[email protected]> Reviewed-by: Tvrtko Ursulin <[email protected]> --- drivers/gpu/drm/scheduler/sched_main.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c index 99252674e266..1a40a3549d7a 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -928,8 +928,19 @@ drm_sched_get_finished_job(struct drm_gpu_scheduler *sched) next = list_first_entry_or_null(&sched->pending_list, typeof(*next), list); if (next) { - /* make the scheduled timestamp more accurate */ - if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, + /* Make the scheduled timestamp more accurate. + * + * TODO: + * Stop overwriting a fence's timestamp. + * + * test_bit_acquire() ensures ordering so that only + * timestamps that are actually present already get + * overwritten (timestamp is a union). + * + * The counter-part of the barrier is located in + * dma_fence_signal_timestamp_locked(). + */ + if (test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT, &next->s_fence->scheduled.flags)) next->s_fence->scheduled.timestamp = dma_fence_timestamp(&job->s_fence->finished); -- 2.56.0
