Skip to contentSkip to conversation

drm/sched/tests: Use one lock for fence context

PATCH Applied Philipp Stanner 3 participants 3 messages May 27, 2025 – Jun 2, 2025

Subsystems (3)
lore
Share & tools
lore

Philipp Stanner

author maintainer
#0 lore raw

PATCH v2 1/1 drm/sched/tests: Use one lock for fence context

There is no need for separate locks for single jobs and the entire scheduler. The dma_fence context can be protected by the scheduler lock, allowing for removing the jobs' locks. This simplifies things and reduces the likelyhood of deadlocks etc.

Replace the jobs' locks with the mock scheduler lock.

---
Changes in v2:
- Make commit message more neutral by stating it's about simplifying the code. (Tvrtko)

---
 drivers/gpu/drm/scheduler/tests/mock_scheduler.c | 5 ++---
 drivers/gpu/drm/scheduler/tests/sched_tests.h    | 1 -
 2 files changed, 2 insertions(+), 4 deletions(-)
drivers/gpu/drm/scheduler/tests/mock_scheduler.c +2 -3
index f999c8859cf7..17023276f4b0 100644
@@ -64,7 +64,7 @@ static void drm_mock_sched_job_complete(struct drm_mock_sched_job *job)
 
 	job->flags |= DRM_MOCK_SCHED_JOB_DONE;
 	list_move_tail(&job->link, &sched->done_list);
-	dma_fence_signal(&job->hw_fence);
+	dma_fence_signal_locked(&job->hw_fence);
 	complete(&job->done);
 }
 
@@ -123,7 +123,6 @@ drm_mock_sched_job_new(struct kunit *test,
 	job->test = test;
 
 	init_completion(&job->done);
-	spin_lock_init(&job->lock);
 	INIT_LIST_HEAD(&job->link);
 	hrtimer_setup(&job->timer, drm_mock_sched_job_signal_timer,
 		      CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
@@ -169,7 +168,7 @@ static struct dma_fence *mock_sched_run_job(struct drm_sched_job *sched_job)
 
 	dma_fence_init(&job->hw_fence,
 		       &drm_mock_sched_hw_fence_ops,
-		       &job->lock,
+		       &sched->lock,
 		       sched->hw_timeline.context,
 		       atomic_inc_return(&sched->hw_timeline.next_seqno));
 
drivers/gpu/drm/scheduler/tests/sched_tests.h +0 -1
index 27caf8285fb7..fbba38137f0c 100644
@@ -106,7 +106,6 @@ struct drm_mock_sched_job {
 	unsigned int		duration_us;
 	ktime_t			finish_at;
 
-	spinlock_t		lock;
 	struct dma_fence	hw_fence;
 
 	struct kunit		*test;
Signature · 1 line
2.49.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 52 lines · Philipp Stanner

There is no need for separate locks for single jobs and the entire scheduler. The dma_fence context can be protected by the scheduler lock, allowing for removing the jobs' locks. This simplifies things and reduces the likelyhood of deadlocks etc.

Replace the jobs' locks with the mock scheduler lock.

---
Changes in v2:
- Make commit message more neutral by stating it's about simplifying the code. (Tvrtko)

---
  drivers/gpu/drm/scheduler/tests/mock_scheduler.c | 5 ++---
  drivers/gpu/drm/scheduler/tests/sched_tests.h    | 1 -
  2 files changed, 2 insertions(+), 4 deletions(-)
drivers/gpu/drm/scheduler/tests/mock_scheduler.c +2 -3
index f999c8859cf7..17023276f4b0 100644
@@ -64,7 +64,7 @@ static void drm_mock_sched_job_complete(struct drm_mock_sched_job *job)
 
  	job->flags |= DRM_MOCK_SCHED_JOB_DONE;
  	list_move_tail(&job->link, &sched->done_list);
-	dma_fence_signal(&job->hw_fence);
+	dma_fence_signal_locked(&job->hw_fence);
  	complete(&job->done);
  }
 
@@ -123,7 +123,6 @@ drm_mock_sched_job_new(struct kunit *test,
  	job->test = test;
 
  	init_completion(&job->done);
-	spin_lock_init(&job->lock);
  	INIT_LIST_HEAD(&job->link);
  	hrtimer_setup(&job->timer, drm_mock_sched_job_signal_timer,
  		      CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
@@ -169,7 +168,7 @@ static struct dma_fence *mock_sched_run_job(struct drm_sched_job *sched_job)
 
  	dma_fence_init(&job->hw_fence,
  		       &drm_mock_sched_hw_fence_ops,
-		       &job->lock,
+		       &sched->lock,
  		       sched->hw_timeline.context,
  		       atomic_inc_return(&sched->hw_timeline.next_seqno));
 
drivers/gpu/drm/scheduler/tests/sched_tests.h +0 -0
index 27caf8285fb7..fbba38137f0c 100644

drivers/gpu/drm/scheduler/tests/sched_tests.h struct drm_mock_sched_job { · line 111

Quoted 7 lines
drivers/gpu/drm/scheduler/tests/sched_tests.h +0 -1
@@ -106,7 +106,6 @@ struct drm_mock_sched_job {
  	unsigned int		duration_us;
  	ktime_t			finish_at;
 
-	spinlock_t		lock;
  	struct dma_fence	hw_fence;
 
  	struct kunit		*test;

Regards,

Tvrtko

#2 lore raw
In reply to Philipp Stanner
Quoted 9 lines · Philipp Stanner

There is no need for separate locks for single jobs and the entire scheduler. The dma_fence context can be protected by the scheduler lock,
allowing for removing the jobs' locks. This simplifies things and reduces the likelyhood of deadlocks etc.

Replace the jobs' locks with the mock scheduler lock.

Applied to drm-misc-next

P.

Quoted 57 lines

---
Changes in v2:
  - Make commit message more neutral by stating it's about simplifying
    the code. (Tvrtko)
---
 drivers/gpu/drm/scheduler/tests/mock_scheduler.c | 5 ++--- drivers/gpu/drm/scheduler/tests/sched_tests.h    | 1 -

 2 files changed, 2 insertions(+), 4 deletions(-)
drivers/gpu/drm/scheduler/tests/mock_scheduler.c +0 -0
index f999c8859cf7..17023276f4b0 100644
@@ -64,7 +64,7 @@ static void drm_mock_sched_job_complete(struct
 drm_mock_sched_job *job)


  job->flags |= DRM_MOCK_SCHED_JOB_DONE; list_move_tail(&job->link, &sched->done_list);

drivers/gpu/drm/scheduler/tests/mock_scheduler.c +1 -1
-	dma_fence_signal(&job->hw_fence);
+	dma_fence_signal_locked(&job->hw_fence);

  complete(&job->done);
 }

drivers/gpu/drm/scheduler/tests/mock_scheduler.c +0 -0
@@ -123,7 +123,6 @@ drm_mock_sched_job_new(struct kunit *test,
  	job->test = test;


  init_completion(&job->done);

drivers/gpu/drm/scheduler/tests/mock_scheduler.c +0 -1
-	spin_lock_init(&job->lock);

  INIT_LIST_HEAD(&job->link); hrtimer_setup(&job->timer, drm_mock_sched_job_signal_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);

drivers/gpu/drm/scheduler/tests/mock_scheduler.c +0 -0
@@ -169,7 +168,7 @@ static struct dma_fence
 *mock_sched_run_job(struct drm_sched_job *sched_job)


  dma_fence_init(&job->hw_fence, &drm_mock_sched_hw_fence_ops,

drivers/gpu/drm/scheduler/tests/mock_scheduler.c +1 -1
-		       &job->lock,
+		       &sched->lock,

         sched->hw_timeline.context, atomic_inc_return(&sched-

Quoted 1 line

hw_timeline.next_seqno));

drivers/gpu/drm/scheduler/tests/sched_tests.h +0 -0
index 27caf8285fb7..fbba38137f0c 100644
@@ -106,7 +106,6 @@ struct drm_mock_sched_job {
  	unsigned int		duration_us;
  	ktime_t			finish_at;

drivers/gpu/drm/scheduler/tests/sched_tests.h +0 -1
-	spinlock_t		lock;

  struct dma_fence hw_fence;

  struct kunit *test;