On 12/11/2026 08:48, Philipp Stanner wrote:
On Wed, 2026-11-11 at 15:05 +0000, Tvrtko Ursulin wrote:
jobs. It did not feel worth explicitly putting cc: stable on the patch.

OK.

This is the *next* job's scheduled fence, so this sounds suspiciously
racy. Testing the bit and overwriting the timestamp, can't that race?

I have a feeling the code was assuming the default ordered workqueue
ensures either run_job or free_job runs at a time. So seeing timestamped
next fence guarantees free_job has exclusive right to the timestamp write.

That is not true though for the three drivers which use own unordered
workqueues - pvr, nouveau and panthor.

If we look at the drm_sched_run_job_work():
...
        drm_sched_job_begin(sched_job);
   -> puts on a pending_list for free job to find
...
drm_sched_fence_scheduled(s_fence, fence);
   -> signals the scheduled fence
...

If free_job interleaves in the middle, so it sees the next job but it
looks unscheduled it will skip the correction.

I'm not even sure why this is done here. We just overwrite the fence's
timestamp. Why?

It is essential for accurate GPU time accounting for all drivers where drm_sched_init_args.credit_limit > 1. Otherwise N jobs can be submitted to the hardware at the ~same time, and have a ~same scheduled timestamp, while in reality the execute serially one after another.

The fence gets marked (signaled) as scheduled in
drm_sched_run_job_work(). Hence, that should be the relevant point in
time. Why should free_job() have anything to do with that?

Indeed it would be nicer, in theory, if it could be done in for example drm_sched_job_done_cb. I *think* I tried it couple years back or so but now from the top of my head I don't remember what was the gotcha. Probably either lock inversions or job lifetime issues.

Looks very suspicious. As if there are two redundant mechanisms here.

Yes I think you are right. As said I think there is no real bug due race
with unordered workqueues, but for formal correctness it would require a
memory barrier after test_bit, or full blown locking.

Is it guaranteed that this fence is signaled and, thus, has a
timestamp, or is it not? If it's guaranteed, the test_bit() is actually
unnecessary. If it is not guaranteed, then there can be misordering
between the two operations and it's UB and, thus, a bug.

As I wrote yesterday, for formal correctness I agree a barrier or lock is missing. But in practice, unless my analysis was wrong, there is no actual problem stemming from that omission due false negative meaning next fence's scheduled timestamp hasn't been timestamped in the past so it doesn't need correcting.

That said, I'm OK with your patch here being merged since you did not
cause that chaos / problem, but a second patch with a FIXME about the
issue that links this mail-thread would be good.

Great, thanks, and no problem, I am marking this as TODO and will send a patch once this one is in the tree.

Regards,

Tvrtko

Reply via email to