Skip to contentSkip to conversation

drm/sched: Do not overaccount job runtime

PATCH In review Tvrtko Ursulin 2 participants 3 messages Nov 17 – Nov 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/sched: Do not overaccount job runtime

Currently the starting timestamp of the next job on the timeline is adjusted to the end of a preceding job. This works if the next job has been submitted while the preceding job was still executing, but not if it was submitted in the window between it finishing and free job work running the timestamp adjustment.

This can happen because the run job and free job work items share the same ordered workqueue and run job can easily submit the next job before the free job gets a chance to run. This over accounting window is present especially with single credit schedulers, but not limited too.

Fix it by only adjusting the starting timestamp if it is not already after the finish time of the preceding job.

While at it move the logic to a helper to keep indendation at bay.

Fixes 1774baa64f93 ("drm/scheduler: Change scheduled fence track v2")
Cc Christian König <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
v2:
* Add drm_sched_job_ prefix to the local helper.

---
 drivers/gpu/drm/scheduler/sched_main.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +20 -5
index 5a2c99b0b38f..d0d34c523615 100644
@@ -898,6 +898,25 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
 	return IS_ERR(entity) ? NULL : entity;
 }
 
+static void
+drm_sched_job_adjust_next_start(struct drm_sched_job *job,
+				struct drm_sched_job *next)
+{
+	struct dma_fence *next_scheduled = &next->s_fence->scheduled;
+	ktime_t ts;
+
+	if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &next_scheduled->flags))
+		return;
+
+	/*
+	 * If the next job has been submitted while the previous one was still
+	 * running adjust it's starting timestamp to avoid accounting overlap.
+	 */
+	ts = dma_fence_timestamp(&job->s_fence->finished);
+	if (ktime_after(ts, next_scheduled->timestamp))
+		next_scheduled->timestamp = ts;
+}
+
 /**
  * drm_sched_get_finished_job - fetch the next finished job to be destroyed
  *
@@ -929,11 +948,7 @@ drm_sched_get_finished_job(struct drm_gpu_scheduler *sched)
 						typeof(*next), list);
 		if (next) {
 			/* make the scheduled timestamp more accurate */
-			if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT,
-				     &next->s_fence->scheduled.flags))
-				next->s_fence->scheduled.timestamp =
-					dma_fence_timestamp(&job->s_fence->finished);
-
+			drm_sched_job_adjust_next_start(job, next);
 			/* start TO timer for next job */
 			drm_sched_start_timeout(sched);
 		}
Signature · 1 line
2.56.0
Acked-by on v2 1/1
#1 lore raw
Quoted 10 lines · Tvrtko Ursulin

Currently the starting timestamp of the next job on the timeline is adjusted to the end of a preceding job. This works if the next job has been submitted while the preceding job was still executing, but not if it was submitted in the window between it finishing and free job work running the timestamp adjustment.

This can happen because the run job and free job work items share the same ordered workqueue and run job can easily submit the next job before the free job gets a chance to run. This over accounting window is present especially with single credit schedulers, but not limited too.

What's a single credit scheduler? Maybe rephrase that?

Quoted 5 lines

Fix it by only adjusting the starting timestamp if it is not already after the finish time of the preceding job.

While at it move the logic to a helper to keep indendation at bay.

typo, indentation.

Feel free to push yourself, with the above addressed if you like.

P.

#2 lore raw
In reply to Tvrtko Ursulin

On Tue, 2026-11-17 at 09:43 +0000, Tvrtko Ursulin wrote:

[…]

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +6 -0
+static void
+drm_sched_job_adjust_next_start(struct drm_sched_job *job,
+				struct drm_sched_job *next)
+{
+	struct dma_fence *next_scheduled = &next->s_fence->scheduled;
+	ktime_t ts;

drivers/gpu/drm/scheduler/sched_main.c drm_sched_select_entity(struct drm_gpu_scheduler *sched) · line 916

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +10 -0
+
+	if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &next_scheduled->flags))
+		return;
+
+	/*
+	 * If the next job has been submitted while the previous one was still
+	 * running adjust it's starting timestamp to avoid accounting overlap.
+	 */
+	ts = dma_fence_timestamp(&job->s_fence->finished);
+	if (ktime_after(ts, next_scheduled->timestamp))

sry for the back and forth, just seeing this

Why is ts accessed through the accessor function and the scheduled timestamp via a direct pointer access? That's inconsistent and shouldn't be done.

P.