Skip to contentSkip to conversation

DRM scheduler fixes, or not, or incorrect kind

PATCH Superseded Tvrtko Ursulin 6 participants 29 messages Sep 9, 2024 – Sep 24, 2024

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover DRM scheduler fixes, or not, or incorrect kind

From: Tvrtko Ursulin <[email protected]>

Re-spin of the series from two days ago with review feedback addressed and some new patches added.

Changelog is in individual patches but essentially new patches are renames and struct members re-ordering as discussed in v1, plus one more optimisation when I noticed we can save another spinlock re-lock cycle this time on rq->lock.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

Tvrtko Ursulin (8):
drm/sched: Add locking to drm_sched_entity_modify_sched drm/sched: Always wake up correct scheduler in
drm_sched_entity_push_job
drm/sched: Always increment correct scheduler score drm/sched: Optimise drm_sched_entity_push_job drm/sched: Stop setting current entity in FIFO mode drm/sched: Re-order struct drm_sched_rq members for clarity drm/sched: Re-group and rename the entity run-queue lock drm/sched: Further optimise drm_sched_entity_push_job

 drivers/gpu/drm/scheduler/sched_entity.c | 40 +++++++++++------
 drivers/gpu/drm/scheduler/sched_main.c   | 57 ++++++++++++++----------
 include/drm/gpu_scheduler.h              | 31 +++++++------
 3 files changed, 77 insertions(+), 51 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH v2 1/8 drm/sched: Add locking to drm_sched_entity_modify_sched

From: Tvrtko Ursulin <[email protected]>

Without the locking amdgpu currently can race between amdgpu_ctx_set_entity_priority() (via drm_sched_entity_modify_sched()) and drm_sched_job_arm(), leading to the latter accesing potentially inconsitent entity->sched_list and entity->num_sched_list pair.

v2:
* Improve commit message. (Philipp)

Fixes b37aced31eb0 ("drm/scheduler: implement a function to modify sched list")
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc <[email protected]> # v5.7+
---
 drivers/gpu/drm/scheduler/sched_entity.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/scheduler/sched_entity.c +2 -0
index 58c8161289fe..ae8be30472cd 100644
@@ -133,8 +133,10 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
 {
 	WARN_ON(!num_sched_list || !sched_list);
 
+	spin_lock(&entity->rq_lock);
 	entity->sched_list = sched_list;
 	entity->num_sched_list = num_sched_list;
+	spin_unlock(&entity->rq_lock);
 }
 EXPORT_SYMBOL(drm_sched_entity_modify_sched);
 
Signature · 1 line
2.46.0
reviewer Reviewed-by on v2 1/8
#2 lore raw

Re: [PATCH 1/8] drm/sched: Add locking to drm_sched_entity_modify_sched

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 10 lines

From: Tvrtko Ursulin <[email protected]>

Without the locking amdgpu currently can race between amdgpu_ctx_set_entity_priority() (via drm_sched_entity_modify_sched()) and drm_sched_job_arm(), leading to the latter accesing potentially inconsitent entity->sched_list and entity->num_sched_list pair.

v2:
* Improve commit message. (Philipp)

Quoted 11 lines
Fixes b37aced31eb0 ("drm/scheduler: implement a function to modify sched list")
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc <[email protected]> # v5.7+
Quoted 19 lines
---
  drivers/gpu/drm/scheduler/sched_entity.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/gpu/drm/scheduler/sched_entity.c +2 -0
index 58c8161289fe..ae8be30472cd 100644
@@ -133,8 +133,10 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
  {
  	WARN_ON(!num_sched_list || !sched_list);
 
+	spin_lock(&entity->rq_lock);
  	entity->sched_list = sched_list;
  	entity->num_sched_list = num_sched_list;
+	spin_unlock(&entity->rq_lock);
  }
  EXPORT_SYMBOL(drm_sched_entity_modify_sched);
 
#3 lore raw

PATCH v2 2/8 drm/sched: Always wake up correct scheduler in drm_sched_entity_push_job

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Since drm_sched_entity_modify_sched() can modify the entities run queue, lets make sure to only dereference the pointer once so both adding and waking up are guaranteed to be consistent.

Alternative of moving the spin_unlock to after the wake up would for now be more problematic since the same lock is taken inside drm_sched_rq_update_fifo().

v2:
* Improve commit message. (Philipp)
* Cache the scheduler pointer directly. (Christian)

Fixes b37aced31eb0 ("drm/scheduler: implement a function to modify sched list")
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc <[email protected]> # v5.7+
---
 drivers/gpu/drm/scheduler/sched_entity.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +8 -2
index ae8be30472cd..76e422548d40 100644
@@ -599,6 +599,9 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 
 	/* first job wakes up scheduler */
 	if (first) {
+		struct drm_gpu_scheduler *sched;
+		struct drm_sched_rq *rq;
+
 		/* Add the entity to the run queue */
 		spin_lock(&entity->rq_lock);
 		if (entity->stopped) {
@@ -608,13 +611,16 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 			return;
 		}
 
-		drm_sched_rq_add_entity(entity->rq, entity);
+		rq = entity->rq;
+		sched = rq->sched;
+
+		drm_sched_rq_add_entity(rq, entity);
 		spin_unlock(&entity->rq_lock);
 
 		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
 			drm_sched_rq_update_fifo(entity, submit_ts);
 
-		drm_sched_wakeup(entity->rq->sched, entity);
+		drm_sched_wakeup(sched, entity);
 	}
 }
 EXPORT_SYMBOL(drm_sched_entity_push_job);
Signature · 1 line
2.46.0
#4 lore raw

Re: [PATCH 2/8] drm/sched: Always wake up correct scheduler in drm_sched_entity_push_job

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 44 lines

From: Tvrtko Ursulin <[email protected]>

Since drm_sched_entity_modify_sched() can modify the entities run queue, lets make sure to only dereference the pointer once so both adding and waking up are guaranteed to be consistent.

Alternative of moving the spin_unlock to after the wake up would for now be more problematic since the same lock is taken inside drm_sched_rq_update_fifo().

v2:
* Improve commit message. (Philipp)
* Cache the scheduler pointer directly. (Christian)

Fixes b37aced31eb0 ("drm/scheduler: implement a function to modify sched list")
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc <[email protected]> # v5.7+
---
  drivers/gpu/drm/scheduler/sched_entity.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +3 -0
index ae8be30472cd..76e422548d40 100644
@@ -599,6 +599,9 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 
  	/* first job wakes up scheduler */
  	if (first) {
+		struct drm_gpu_scheduler *sched;
+		struct drm_sched_rq *rq;
+
  		/* Add the entity to the run queue */
  		spin_lock(&entity->rq_lock);
  		if (entity->stopped) {

drivers/gpu/drm/scheduler/sched_entity.c void drm_sched_entity_push_job(struct drm_sched_job *sched_job) · line 615

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_entity.c +2 -1
@@ -608,13 +611,16 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  			return;
  		}
 
-		drm_sched_rq_add_entity(entity->rq, entity);
+		rq = entity->rq;
+		sched = rq->sched;

The rq variable is now not needed any more except for getting the scheduler, isn't it? If yes I would ditch that as well.

Either way Reviewed-by: Christian König <[email protected]> to this patch.

Regards,
Christian.

Quoted 12 lines
drivers/gpu/drm/scheduler/sched_entity.c +3 -1
+
+		drm_sched_rq_add_entity(rq, entity);
  		spin_unlock(&entity->rq_lock);
 
  		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
  			drm_sched_rq_update_fifo(entity, submit_ts);
 
-		drm_sched_wakeup(entity->rq->sched, entity);
+		drm_sched_wakeup(sched, entity);
  	}
  }
  EXPORT_SYMBOL(drm_sched_entity_push_job);
#5 lore raw

PATCH v2 3/8 drm/sched: Always increment correct scheduler score

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Entities run queue can change during drm_sched_entity_push_job() so make sure to update the score consistently.

Fixes d41a39dda140 ("drm/scheduler: improve job distribution with multiple queues")
Cc Nirmoy Das <[email protected]>
Cc Christian König <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc <[email protected]> # v5.9+
---
 drivers/gpu/drm/scheduler/sched_entity.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_entity.c +1 -1
index 76e422548d40..6645a8524699 100644
@@ -586,7 +586,6 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 	ktime_t submit_ts;
 
 	trace_drm_sched_job(sched_job, entity);
-	atomic_inc(entity->rq->sched->score);
 	WRITE_ONCE(entity->last_user, current->group_leader);
 
 	/*
@@ -614,6 +613,7 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 		rq = entity->rq;
 		sched = rq->sched;
 
+		atomic_inc(sched->score);
 		drm_sched_rq_add_entity(rq, entity);
 		spin_unlock(&entity->rq_lock);
 
Signature · 1 line
2.46.0
#6 lore raw

PATCH v2 4/8 drm/sched: Optimise drm_sched_entity_push_job

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

In FIFO mode We can avoid dropping the lock only to immediately re-acquire by adding a new drm_sched_rq_update_fifo_locked() helper.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
 drivers/gpu/drm/scheduler/sched_entity.c |  5 +++--
 drivers/gpu/drm/scheduler/sched_main.c   | 21 ++++++++++++++-------
 include/drm/gpu_scheduler.h              |  1 +
 3 files changed, 18 insertions(+), 9 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +3 -2
index 6645a8524699..2da677681291 100644
@@ -615,10 +615,11 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 
 		atomic_inc(sched->score);
 		drm_sched_rq_add_entity(rq, entity);
-		spin_unlock(&entity->rq_lock);
 
 		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-			drm_sched_rq_update_fifo(entity, submit_ts);
+			drm_sched_rq_update_fifo_locked(entity, submit_ts);
+
+		spin_unlock(&entity->rq_lock);
 
 		drm_sched_wakeup(sched, entity);
 	}
drivers/gpu/drm/scheduler/sched_main.c +14 -7
index ab53ab486fe6..10abbcefe9d8 100644
@@ -163,14 +163,10 @@ static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *enti
 	}
 }
 
-void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts)
 {
-	/*
-	 * Both locks need to be grabbed, one to protect from entity->rq change
-	 * for entity from within concurrent drm_sched_entity_select_rq and the
-	 * other to update the rb tree structure.
-	 */
-	spin_lock(&entity->rq_lock);
+	lockdep_assert_held(&entity->rq_lock);
+
 	spin_lock(&entity->rq->lock);
 
 	drm_sched_rq_remove_fifo_locked(entity);
@@ -181,6 +177,17 @@ void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
 		      drm_sched_entity_compare_before);
 
 	spin_unlock(&entity->rq->lock);
+}
+
+void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
+{
+	/*
+	 * Both locks need to be grabbed, one to protect from entity->rq change
+	 * for entity from within concurrent drm_sched_entity_select_rq and the
+	 * other to update the rb tree structure.
+	 */
+	spin_lock(&entity->rq_lock);
+	drm_sched_rq_update_fifo_locked(entity, ts);
 	spin_unlock(&entity->rq_lock);
 }
 
include/drm/gpu_scheduler.h +1 -0
index fe8edb917360..a06753987d93 100644
@@ -594,6 +594,7 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
 				struct drm_sched_entity *entity);
 
 void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts);
 
 int drm_sched_entity_init(struct drm_sched_entity *entity,
 			  enum drm_sched_priority priority,
Signature · 1 line
2.46.0
#7 lore raw

Re: [PATCH 4/8] drm/sched: Optimise drm_sched_entity_push_job

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 64 lines

From: Tvrtko Ursulin <[email protected]>

In FIFO mode We can avoid dropping the lock only to immediately re-acquire by adding a new drm_sched_rq_update_fifo_locked() helper.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
  drivers/gpu/drm/scheduler/sched_entity.c |  5 +++--
  drivers/gpu/drm/scheduler/sched_main.c   | 21 ++++++++++++++-------
  include/drm/gpu_scheduler.h              |  1 +
  3 files changed, 18 insertions(+), 9 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +3 -2
index 6645a8524699..2da677681291 100644
@@ -615,10 +615,11 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 
  		atomic_inc(sched->score);
  		drm_sched_rq_add_entity(rq, entity);
-		spin_unlock(&entity->rq_lock);
 
  		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-			drm_sched_rq_update_fifo(entity, submit_ts);
+			drm_sched_rq_update_fifo_locked(entity, submit_ts);
+
+		spin_unlock(&entity->rq_lock);
 
  		drm_sched_wakeup(sched, entity);
  	}
drivers/gpu/drm/scheduler/sched_main.c +6 -7
index ab53ab486fe6..10abbcefe9d8 100644
@@ -163,14 +163,10 @@ static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *enti
  	}
  }
 
-void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts)
  {
-	/*
-	 * Both locks need to be grabbed, one to protect from entity->rq change
-	 * for entity from within concurrent drm_sched_entity_select_rq and the
-	 * other to update the rb tree structure.
-	 */
-	spin_lock(&entity->rq_lock);
+	lockdep_assert_held(&entity->rq_lock);
+
  	spin_lock(&entity->rq->lock);
 
  	drm_sched_rq_remove_fifo_locked(entity);
@@ -181,6 +177,17 @@ void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
  		      drm_sched_entity_compare_before);
 
  	spin_unlock(&entity->rq->lock);
+}
+
+void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts) · line 192

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +8 -0
+{
+	/*
+	 * Both locks need to be grabbed, one to protect from entity->rq change
+	 * for entity from within concurrent drm_sched_entity_select_rq and the
+	 * other to update the rb tree structure.
+	 */
+	spin_lock(&entity->rq_lock);
+	drm_sched_rq_update_fifo_locked(entity, ts);
  	spin_unlock(&entity->rq_lock);
  }

I wonder if we shouldn't change the only other occasion calling this to grab the lock manually as well.

Christian.

Quoted 13 lines
include/drm/gpu_scheduler.h +1 -0
index fe8edb917360..a06753987d93 100644
@@ -594,6 +594,7 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity);
 
  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts);
 
  int drm_sched_entity_init(struct drm_sched_entity *entity,
  			  enum drm_sched_priority priority,
#8 lore raw

PATCH v2 5/8 drm/sched: Stop setting current entity in FIFO mode

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

It does not seem there is a need to set the current entity in FIFO mode since ot only serves as being a "cursor" in round-robin mode. Even if scheduling mode is changed at runtime the change in behaviour is simply to restart from the first entity, instead of continuing in RR mode from where FIFO left it, and that sounds completely fine.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
 drivers/gpu/drm/scheduler/sched_main.c | 1 -
 1 file changed, 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -1
index 10abbcefe9d8..54c5fe7a7d1d 100644
@@ -356,7 +356,6 @@ drm_sched_rq_select_entity_fifo(struct drm_gpu_scheduler *sched,
 				return ERR_PTR(-ENOSPC);
 			}
 
-			rq->current_entity = entity;
 			reinit_completion(&entity->entity_idle);
 			break;
 		}
Signature · 1 line
2.46.0
Acked-by on v2 5/8
#9 lore raw

Re: [PATCH 5/8] drm/sched: Stop setting current entity in FIFO mode

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 8 lines

From: Tvrtko Ursulin <[email protected]>

It does not seem there is a need to set the current entity in FIFO mode since ot only serves as being a "cursor" in round-robin mode. Even if scheduling mode is changed at runtime the change in behaviour is simply to restart from the first entity, instead of continuing in RR mode from where FIFO left it, and that sounds completely fine.

Quoted 6 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
Quoted 16 lines
---
  drivers/gpu/drm/scheduler/sched_main.c | 1 -
  1 file changed, 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -1
index 10abbcefe9d8..54c5fe7a7d1d 100644
@@ -356,7 +356,6 @@ drm_sched_rq_select_entity_fifo(struct drm_gpu_scheduler *sched,
  				return ERR_PTR(-ENOSPC);
  			}
 
-			rq->current_entity = entity;
  			reinit_completion(&entity->entity_idle);
  			break;
  		}
Reviewed-by on v2 5/8
#10 lore raw

Re: [PATCH 5/8] drm/sched: Stop setting current entity in FIFO mode

In reply to Tvrtko Ursulin
Quoted 11 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

It does not seem there is a need to set the current entity in FIFO mode
since ot only serves as being a "cursor" in round-robin mode. Even if scheduling mode is changed at runtime the change in behaviour is simply
to restart from the first entity, instead of continuing in RR mode from
where FIFO left it, and that sounds completely fine.

Quoted 1 line

I went through the code and agree that this looks good.

Quoted 23 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
 drivers/gpu/drm/scheduler/sched_main.c | 1 -

 1 file changed, 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 10abbcefe9d8..54c5fe7a7d1d 100644
@@ -356,7 +356,6 @@ drm_sched_rq_select_entity_fifo(struct
 drm_gpu_scheduler *sched,
  				return ERR_PTR(-ENOSPC);
  			}

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-			rq->current_entity = entity;

  reinit_completion(&entity->entity_idle); break;
  }

#11 lore raw

PATCH v2 6/8 drm/sched: Re-order struct drm_sched_rq members for clarity

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Lets re-order the members to make it clear which are protected by the lock and at the same time document it via kerneldoc.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
 include/drm/gpu_scheduler.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
include/drm/gpu_scheduler.h +6 -4
index a06753987d93..d4a3ba333568 100644
@@ -243,10 +243,10 @@ struct drm_sched_entity {
 /**
  * struct drm_sched_rq - queue of entities to be scheduled.
  *
- * @lock: to modify the entities list.
  * @sched: the scheduler to which this rq belongs to.
- * @entities: list of the entities to be scheduled.
+ * @lock: protects the list, tree and current entity.
  * @current_entity: the entity which is to be scheduled.
+ * @entities: list of the entities to be scheduled.
  * @rb_tree_root: root of time based priory queue of entities for FIFO scheduling
  *
  * Run queue is a set of entities scheduling command submissions for
@@ -254,10 +254,12 @@ struct drm_sched_entity {
  * the next entity to emit commands from.
  */
 struct drm_sched_rq {
-	spinlock_t			lock;
 	struct drm_gpu_scheduler	*sched;
-	struct list_head		entities;
+
+	spinlock_t			lock;
+	/* Following members are protected by the @lock: */
 	struct drm_sched_entity		*current_entity;
+	struct list_head		entities;
 	struct rb_root_cached		rb_tree_root;
 };
 
Signature · 1 line
2.46.0
#12 lore raw

Re: [PATCH 6/8] drm/sched: Re-order struct drm_sched_rq members for clarity

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 35 lines

From: Tvrtko Ursulin <[email protected]>

Lets re-order the members to make it clear which are protected by the lock and at the same time document it via kerneldoc.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
  include/drm/gpu_scheduler.h | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
include/drm/gpu_scheduler.h +2 -2
index a06753987d93..d4a3ba333568 100644
@@ -243,10 +243,10 @@ struct drm_sched_entity {
  /**
   * struct drm_sched_rq - queue of entities to be scheduled.
   *
- * @lock: to modify the entities list.
   * @sched: the scheduler to which this rq belongs to.
- * @entities: list of the entities to be scheduled.
+ * @lock: protects the list, tree and current entity.
   * @current_entity: the entity which is to be scheduled.
+ * @entities: list of the entities to be scheduled.
   * @rb_tree_root: root of time based priory queue of entities for FIFO scheduling
   *
   * Run queue is a set of entities scheduling command submissions for
@@ -254,10 +254,12 @@ struct drm_sched_entity {
   * the next entity to emit commands from.
   */

include/drm/gpu_scheduler.h struct drm_sched_entity { · line 263

Quoted 10 lines
include/drm/gpu_scheduler.h +4 -2
  struct drm_sched_rq {
-	spinlock_t			lock;
  	struct drm_gpu_scheduler	*sched;
-	struct list_head		entities;
+
+	spinlock_t			lock;
+	/* Following members are protected by the @lock: */
  	struct drm_sched_entity		*current_entity;
+	struct list_head		entities;
  	struct rb_root_cached		rb_tree_root;

Just suggestion for further improvement:

struct {
    struct drm_sched_entity    *last_selected;
    struct list_head                  entities;
} rr;
struct {
    struct rb_root_cached        rb_tree_root;
} fifo;

But even without that the patch is Reviewed-by: Christian König <[email protected]>

Regards,
Christian.

Quoted 2 lines
include/drm/gpu_scheduler.h +0 -0
  };
 
#13 lore raw

Re: [PATCH 6/8] drm/sched: Re-order struct drm_sched_rq members for clarity

In reply to Tvrtko Ursulin
Quoted 5 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Lets re-order the members to make it clear which are protected by the lock
and at the same time document it via kerneldoc.

I'd prefer if commit messages follow the idiomatic kernel style of that order:
1. Describe the current situation
2. State why it's bad or undesirable
3. (describe the solution)
4. Conclude commit message through sentences in imperative stating what the commit does.

In this case I would go for:
"struct drm_sched_rq contains a spinlock that protects several struct members. The current documentation incorrectly states that this lock only guards the entities list. In truth, it guards that list, the rb_tree and the current entity.

Document what the lock actually guards. Rearrange struct members so that this becomes even more visible."

Quoted 17 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
 include/drm/gpu_scheduler.h | 10 ++++++----

 1 file changed, 6 insertions(+), 4 deletions(-)
include/drm/gpu_scheduler.h +0 -0
index a06753987d93..d4a3ba333568 100644

include/drm/gpu_scheduler.h struct drm_sched_entity { · line 247

Quoted 7 lines
include/drm/gpu_scheduler.h +1 -2
@@ -243,10 +243,10 @@ struct drm_sched_entity {
  /**
   * struct drm_sched_rq - queue of entities to be scheduled.
   *
- * @lock: to modify the entities list.
   * @sched: the scheduler to which this rq belongs to.
- * @entities: list of the entities to be scheduled.
+ * @lock: protects the list, tree and current entity.

Would be more consistent with the below comment if you'd address them with their full name, aka "protects @entities, @rb_tree_root and @current_entity".

Thanks,
P.

Quoted 21 lines
include/drm/gpu_scheduler.h +1 -0
   * @current_entity: the entity which is to be scheduled.
+ * @entities: list of the entities to be scheduled.
   * @rb_tree_root: root of time based priory queue of entities for

FIFO scheduling
  *
  * Run queue is a set of entities scheduling command submissions for

include/drm/gpu_scheduler.h +4 -2
@@ -254,10 +254,12 @@ struct drm_sched_entity {
   * the next entity to emit commands from.
   */
  struct drm_sched_rq {
- spinlock_t lock;
   struct drm_gpu_scheduler *sched;
- struct list_head entities;
+
+ spinlock_t lock;
+ /* Following members are protected by the @lock: */
   struct drm_sched_entity *current_entity;
+ struct list_head entities;
   struct rb_root_cached rb_tree_root;
  };

#14 lore raw

Re: [PATCH 6/8] drm/sched: Re-order struct drm_sched_rq members for clarity

Quoted 15 lines · Philipp Stanner
Quoted 5 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Lets re-order the members to make it clear which are protected by the lock
and at the same time document it via kerneldoc.

I'd prefer if commit messages follow the idiomatic kernel style of that order:
1. Describe the current situation
2. State why it's bad or undesirable
3. (describe the solution)
4. Conclude commit message through sentences in imperative stating what the commit does.

Quoted 8 lines

In this case I would go for:
"struct drm_sched_rq contains a spinlock that protects several struct members. The current documentation incorrectly states that this lock only guards the entities list. In truth, it guards that list, the rb_tree and the current entity.

Document what the lock actually guards. Rearrange struct members so that this becomes even more visible."

IMO a bit much to ask for a text book format, for a trivial patch, when all points are already implicitly obvious. That is "lets make it clear" = current situation is not clear -> obviously bad with no need to explain; "and the same time document" = means it is currently not documented -> again obviously not desirable.

But okay, since I agree with the point below (*), I can explode the text for maximum redundancy.

Quoted 19 lines
Quoted 19 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
  include/drm/gpu_scheduler.h | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
include/drm/gpu_scheduler.h +0 -0
index a06753987d93..d4a3ba333568 100644
@@ -243,10 +243,10 @@ struct drm_sched_entity {
  /**
   * struct drm_sched_rq - queue of entities to be scheduled.
   *
Quoted 8 lines

include/drm/gpu_scheduler.h struct drm_sched_entity { · line 247

Quoted 4 lines
include/drm/gpu_scheduler.h +1 -2
- * @lock: to modify the entities list.
   * @sched: the scheduler to which this rq belongs to.
- * @entities: list of the entities to be scheduled.
+ * @lock: protects the list, tree and current entity.

Would be more consistent with the below comment if you'd address them with their full name, aka "protects @entities, @rb_tree_root and @current_entity".

*) this one I agree with.

Regards,

Tvrtko

Quoted 27 lines

Thanks,
P.

Quoted 21 lines
include/drm/gpu_scheduler.h +1 -0
   * @current_entity: the entity which is to be scheduled.
+ * @entities: list of the entities to be scheduled.
   * @rb_tree_root: root of time based priory queue of entities for

FIFO scheduling
  *
  * Run queue is a set of entities scheduling command submissions for

include/drm/gpu_scheduler.h +4 -2
@@ -254,10 +254,12 @@ struct drm_sched_entity {
   * the next entity to emit commands from.
   */
  struct drm_sched_rq {
- spinlock_t lock;
   struct drm_gpu_scheduler *sched;
- struct list_head entities;
+
+ spinlock_t lock;
+ /* Following members are protected by the @lock: */
   struct drm_sched_entity *current_entity;
+ struct list_head entities;
   struct rb_root_cached rb_tree_root;
  };
 
#15 lore raw

Re: [PATCH 6/8] drm/sched: Re-order struct drm_sched_rq members for clarity

Quoted 39 lines · Tvrtko Ursulin
Quoted 26 lines · Philipp Stanner
Quoted 6 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Lets re-order the members to make it clear which are protected by the
lock
and at the same time document it via kerneldoc.

I'd prefer if commit messages follow the idiomatic kernel style of that
order:
    1. Describe the current situation
    2. State why it's bad or undesirable
    3. (describe the solution)
    4. Conclude commit message through sentences in imperative stating
       what the commit does.

In this case I would go for:
"struct drm_sched_rq contains a spinlock that protects several struct
members. The current documentation incorrectly states that this lock
only guards the entities list. In truth, it guards that list, the rb_tree and the current entity.

Quoted 2 lines

Document what the lock actually guards. Rearrange struct members so that this becomes even more visible."

IMO a bit much to ask for a text book format, for a trivial patch, when
all points are already implicitly obvious. That is "lets make it clear"
= current situation is not clear -> obviously bad with no need to explain; "and the same time document" = means it is currently not documented -> again obviously not desirable.

Quoted 3 lines

But okay, since I agree with the point below (*), I can explode the text
for maximum redundancy.

I agree that for very short / trivial changes one can keep it short. But the line separating what is obvious for oneself and for others is often thin.

P.

Quoted 66 lines
Quoted 21 lines
Quoted 21 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
  include/drm/gpu_scheduler.h | 10 ++++++----

  1 file changed, 6 insertions(+), 4 deletions(-)
include/drm/gpu_scheduler.h +0 -1
index a06753987d93..d4a3ba333568 100644
@@ -243,10 +243,10 @@ struct drm_sched_entity {
   /**
    * struct drm_sched_rq - queue of entities to be scheduled.
    *
- * @lock: to modify the entities list.
Quoted 8 lines

include/drm/gpu_scheduler.h struct drm_sched_entity { · line 247

Quoted 3 lines
include/drm/gpu_scheduler.h +1 -1
    * @sched: the scheduler to which this rq belongs to.
- * @entities: list of the entities to be scheduled.
+ * @lock: protects the list, tree and current entity.

Would be more consistent with the below comment if you'd address them
with their full name, aka "protects @entities, @rb_tree_root and @current_entity".

*) this one I agree with.

Regards,

Tvrtko

Quoted 29 lines

Thanks,
P.

Quoted 23 lines
include/drm/gpu_scheduler.h +1 -0
    * @current_entity: the entity which is to be scheduled.
+ * @entities: list of the entities to be scheduled.
    * @rb_tree_root: root of time based priory queue of entities

for
FIFO scheduling
   *
   * Run queue is a set of entities scheduling command submissions for

include/drm/gpu_scheduler.h +4 -2
@@ -254,10 +254,12 @@ struct drm_sched_entity {
    * the next entity to emit commands from.
    */
   struct drm_sched_rq {
- spinlock_t lock;
    struct drm_gpu_scheduler *sched;
- struct list_head entities;
+
+ spinlock_t lock;
+ /* Following members are protected by the @lock: */
    struct drm_sched_entity *current_entity;
+ struct list_head entities;
    struct rb_root_cached rb_tree_root;
   };

#16 lore raw

Re: [PATCH 6/8] drm/sched: Re-order struct drm_sched_rq members for clarity

In reply to Tvrtko Ursulin
Quoted 33 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Lets re-order the members to make it clear which are protected by the lock and at the same time document it via kerneldoc.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
 include/drm/gpu_scheduler.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
include/drm/gpu_scheduler.h +2 -2
index a06753987d93..d4a3ba333568 100644
@@ -243,10 +243,10 @@ struct drm_sched_entity {
 /**
  * struct drm_sched_rq - queue of entities to be scheduled.
  *
- * @lock: to modify the entities list.
  * @sched: the scheduler to which this rq belongs to.
- * @entities: list of the entities to be scheduled.
+ * @lock: protects the list, tree and current entity.
  * @current_entity: the entity which is to be scheduled.
+ * @entities: list of the entities to be scheduled.
  * @rb_tree_root: root of time based priory queue of entities for FIFO scheduling
  *
  * Run queue is a set of entities scheduling command submissions for

include/drm/gpu_scheduler.h struct drm_sched_entity { · line 260

Quoted 9 lines
include/drm/gpu_scheduler.h +3 -2
@@ -254,10 +254,12 @@ struct drm_sched_entity {
  * the next entity to emit commands from.
  */
 struct drm_sched_rq {
-	spinlock_t			lock;
 	struct drm_gpu_scheduler	*sched;
-	struct list_head		entities;
+
+	spinlock_t			lock;
+	/* Following members are protected by the @lock: */

Bit a bikeshed, but I think the moment you feel like you need to sprinkle additional comments around you should switch over to the in-line documentation style, where you can have all the space to add everything. For structures in general I think the top comment style for members isn't a good idea except for really trivial structs.

But yeah I know it's a bit of work to shuffle stuff around, so maybe next time ..
-Sima

Quoted 8 lines
include/drm/gpu_scheduler.h +1 -0
 	struct drm_sched_entity		*current_entity;
+	struct list_head		entities;
 	struct rb_root_cached		rb_tree_root;
 };
 
Signature · 1 line
2.46.0
Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#17 lore raw

PATCH v2 7/8 drm/sched: Re-group and rename the entity run-queue lock

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Christian suggested to rename the lock and improve the documentation of what it protects. And to also re-order the structure members so all protected by the lock are together in a block.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
 drivers/gpu/drm/scheduler/sched_entity.c | 24 ++++++++++++------------
 drivers/gpu/drm/scheduler/sched_main.c   |  6 +++---
 include/drm/gpu_scheduler.h              | 15 ++++++++-------
 3 files changed, 23 insertions(+), 22 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +12 -12
index 2da677681291..b4c4f9923e0b 100644
@@ -105,7 +105,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 	/* We start in an idle state. */
 	complete_all(&entity->entity_idle);
 
-	spin_lock_init(&entity->rq_lock);
+	spin_lock_init(&entity->lock);
 	spsc_queue_init(&entity->job_queue);
 
 	atomic_set(&entity->fence_seq, 0);
@@ -133,10 +133,10 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
 {
 	WARN_ON(!num_sched_list || !sched_list);
 
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
 	entity->sched_list = sched_list;
 	entity->num_sched_list = num_sched_list;
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
 }
 EXPORT_SYMBOL(drm_sched_entity_modify_sched);
 
@@ -244,10 +244,10 @@ static void drm_sched_entity_kill(struct drm_sched_entity *entity)
 	if (!entity->rq)
 		return;
 
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
 	entity->stopped = true;
 	drm_sched_rq_remove_entity(entity->rq, entity);
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
 
 	/* Make sure this entity is not used by the scheduler at the moment */
 	wait_for_completion(&entity->entity_idle);
@@ -396,9 +396,9 @@ static void drm_sched_entity_wakeup(struct dma_fence *f,
 void drm_sched_entity_set_priority(struct drm_sched_entity *entity,
 				   enum drm_sched_priority priority)
 {
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
 	entity->priority = priority;
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
 }
 EXPORT_SYMBOL(drm_sched_entity_set_priority);
 
@@ -555,14 +555,14 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 	if (fence && !dma_fence_is_signaled(fence))
 		return;
 
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
 	sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list);
 	rq = sched ? sched->sched_rq[entity->priority] : NULL;
 	if (rq != entity->rq) {
 		drm_sched_rq_remove_entity(entity->rq, entity);
 		entity->rq = rq;
 	}
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
 
 	if (entity->num_sched_list == 1)
 		entity->sched_list = NULL;
@@ -602,9 +602,9 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 		struct drm_sched_rq *rq;
 
 		/* Add the entity to the run queue */
-		spin_lock(&entity->rq_lock);
+		spin_lock(&entity->lock);
 		if (entity->stopped) {
-			spin_unlock(&entity->rq_lock);
+			spin_unlock(&entity->lock);
 
 			DRM_ERROR("Trying to push to a killed entity\n");
 			return;
@@ -619,7 +619,7 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
 			drm_sched_rq_update_fifo_locked(entity, submit_ts);
 
-		spin_unlock(&entity->rq_lock);
+		spin_unlock(&entity->lock);
 
 		drm_sched_wakeup(sched, entity);
 	}
drivers/gpu/drm/scheduler/sched_main.c +3 -3
index 54c5fe7a7d1d..937e7d1cfc49 100644
@@ -165,7 +165,7 @@ static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *enti
 
 void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts)
 {
-	lockdep_assert_held(&entity->rq_lock);
+	lockdep_assert_held(&entity->lock);
 
 	spin_lock(&entity->rq->lock);
 
@@ -186,9 +186,9 @@ void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
 	 * for entity from within concurrent drm_sched_entity_select_rq and the
 	 * other to update the rb tree structure.
 	 */
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
 	drm_sched_rq_update_fifo_locked(entity, ts);
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
 }
 
 /**
include/drm/gpu_scheduler.h +8 -7
index d4a3ba333568..5a1e4c803b90 100644
@@ -96,6 +96,14 @@ struct drm_sched_entity {
 	 */
 	struct list_head		list;
 
+	/**
+	 * @lock:
+	 *
+	 * Lock protecting the run-queue (@rq) to which this entity belongs,
+	 * @priority and the list of schedulers (@sched_list, @num_sched_list).
+	 */
+	spinlock_t			lock;
+
 	/**
 	 * @rq:
 	 *
@@ -140,13 +148,6 @@ struct drm_sched_entity {
 	 */
 	enum drm_sched_priority         priority;
 
-	/**
-	 * @rq_lock:
-	 *
-	 * Lock to modify the runqueue to which this entity belongs.
-	 */
-	spinlock_t			rq_lock;
-
 	/**
 	 * @job_queue: the list of jobs of this entity.
 	 */
Signature · 1 line
2.46.0
Reviewed-by on v2 7/8
#18 lore raw

Re: [PATCH 7/8] drm/sched: Re-group and rename the entity run-queue lock

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 6 lines

From: Tvrtko Ursulin <[email protected]>

Christian suggested to rename the lock and improve the documentation of what it protects. And to also re-order the structure members so all protected by the lock are together in a block.

Quoted 6 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
Quoted 153 lines
---
  drivers/gpu/drm/scheduler/sched_entity.c | 24 ++++++++++++------------
  drivers/gpu/drm/scheduler/sched_main.c   |  6 +++---
  include/drm/gpu_scheduler.h              | 15 ++++++++-------
  3 files changed, 23 insertions(+), 22 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +12 -12
index 2da677681291..b4c4f9923e0b 100644
@@ -105,7 +105,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  	/* We start in an idle state. */
  	complete_all(&entity->entity_idle);
 
-	spin_lock_init(&entity->rq_lock);
+	spin_lock_init(&entity->lock);
  	spsc_queue_init(&entity->job_queue);
 
  	atomic_set(&entity->fence_seq, 0);
@@ -133,10 +133,10 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
  {
  	WARN_ON(!num_sched_list || !sched_list);
 
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
  	entity->sched_list = sched_list;
  	entity->num_sched_list = num_sched_list;
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
  }
  EXPORT_SYMBOL(drm_sched_entity_modify_sched);
 
@@ -244,10 +244,10 @@ static void drm_sched_entity_kill(struct drm_sched_entity *entity)
  	if (!entity->rq)
  		return;
 
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
  	entity->stopped = true;
  	drm_sched_rq_remove_entity(entity->rq, entity);
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
 
  	/* Make sure this entity is not used by the scheduler at the moment */
  	wait_for_completion(&entity->entity_idle);
@@ -396,9 +396,9 @@ static void drm_sched_entity_wakeup(struct dma_fence *f,
  void drm_sched_entity_set_priority(struct drm_sched_entity *entity,
  				   enum drm_sched_priority priority)
  {
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
  	entity->priority = priority;
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
  }
  EXPORT_SYMBOL(drm_sched_entity_set_priority);
 
@@ -555,14 +555,14 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
  	if (fence && !dma_fence_is_signaled(fence))
  		return;
 
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
  	sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list);
  	rq = sched ? sched->sched_rq[entity->priority] : NULL;
  	if (rq != entity->rq) {
  		drm_sched_rq_remove_entity(entity->rq, entity);
  		entity->rq = rq;
  	}
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
 
  	if (entity->num_sched_list == 1)
  		entity->sched_list = NULL;
@@ -602,9 +602,9 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  		struct drm_sched_rq *rq;
 
  		/* Add the entity to the run queue */
-		spin_lock(&entity->rq_lock);
+		spin_lock(&entity->lock);
  		if (entity->stopped) {
-			spin_unlock(&entity->rq_lock);
+			spin_unlock(&entity->lock);
 
  			DRM_ERROR("Trying to push to a killed entity\n");
  			return;
@@ -619,7 +619,7 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
  			drm_sched_rq_update_fifo_locked(entity, submit_ts);
 
-		spin_unlock(&entity->rq_lock);
+		spin_unlock(&entity->lock);
 
  		drm_sched_wakeup(sched, entity);
  	}
drivers/gpu/drm/scheduler/sched_main.c +3 -3
index 54c5fe7a7d1d..937e7d1cfc49 100644
@@ -165,7 +165,7 @@ static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *enti
 
  void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts)
  {
-	lockdep_assert_held(&entity->rq_lock);
+	lockdep_assert_held(&entity->lock);
 
  	spin_lock(&entity->rq->lock);
 
@@ -186,9 +186,9 @@ void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
  	 * for entity from within concurrent drm_sched_entity_select_rq and the
  	 * other to update the rb tree structure.
  	 */
-	spin_lock(&entity->rq_lock);
+	spin_lock(&entity->lock);
  	drm_sched_rq_update_fifo_locked(entity, ts);
-	spin_unlock(&entity->rq_lock);
+	spin_unlock(&entity->lock);
  }
 
  /**
include/drm/gpu_scheduler.h +8 -7
index d4a3ba333568..5a1e4c803b90 100644
@@ -96,6 +96,14 @@ struct drm_sched_entity {
  	 */
  	struct list_head		list;
 
+	/**
+	 * @lock:
+	 *
+	 * Lock protecting the run-queue (@rq) to which this entity belongs,
+	 * @priority and the list of schedulers (@sched_list, @num_sched_list).
+	 */
+	spinlock_t			lock;
+
  	/**
  	 * @rq:
  	 *
@@ -140,13 +148,6 @@ struct drm_sched_entity {
  	 */
  	enum drm_sched_priority         priority;
 
-	/**
-	 * @rq_lock:
-	 *
-	 * Lock to modify the runqueue to which this entity belongs.
-	 */
-	spinlock_t			rq_lock;
-
  	/**
  	 * @job_queue: the list of jobs of this entity.
  	 */
#19 lore raw

PATCH v2 8/8 drm/sched: Further optimise drm_sched_entity_push_job

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled "drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit larger refactoring we can do the same optimisation on the rq->lock. (Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

To achieve this we rename drm_sched_rq_add_entity() to drm_sched_rq_add_entity_locked(), making it expect the rq->lock to be held, and also add the same expectation to drm_sched_rq_update_fifo_locked().

For more stream-lining we also add the run-queue as an explicit parameter to drm_sched_rq_remove_fifo_locked() to avoid both callers and callee having to dereference entity->rq.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
 drivers/gpu/drm/scheduler/sched_entity.c |  7 ++--
 drivers/gpu/drm/scheduler/sched_main.c   | 41 +++++++++++++-----------
 include/drm/gpu_scheduler.h              |  7 ++--
 3 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +5 -2
index b4c4f9923e0b..2102c726d275 100644
@@ -614,11 +614,14 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 		sched = rq->sched;
 
 		atomic_inc(sched->score);
-		drm_sched_rq_add_entity(rq, entity);
+
+		spin_lock(&rq->lock);
+		drm_sched_rq_add_entity_locked(rq, entity);
 
 		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-			drm_sched_rq_update_fifo_locked(entity, submit_ts);
+			drm_sched_rq_update_fifo_locked(entity, rq, submit_ts);
 
+		spin_unlock(&rq->lock);
 		spin_unlock(&entity->lock);
 
 		drm_sched_wakeup(sched, entity);
drivers/gpu/drm/scheduler/sched_main.c +22 -19

101 lines not on this page · raw message

include/drm/gpu_scheduler.h +4 -3
index 5a1e4c803b90..2ad33e2fe2d2 100644
@@ -591,13 +591,14 @@ bool drm_sched_dependency_optimized(struct dma_fence* fence,
 				    struct drm_sched_entity *entity);
 void drm_sched_fault(struct drm_gpu_scheduler *sched);
 
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity);
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity *entity);
 void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
 				struct drm_sched_entity *entity);
 
 void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts);
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,
+				     struct drm_sched_rq *rq, ktime_t ts);
 
 int drm_sched_entity_init(struct drm_sched_entity *entity,
 			  enum drm_sched_priority priority,
Signature · 1 line
2.46.0
#20 lore raw

Re: [PATCH 8/8] drm/sched: Further optimise drm_sched_entity_push_job

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 7 lines

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled "drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit larger refactoring we can do the same optimisation on the rq->lock. (Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

I think that goes into the wrong direction.

Probably better to move this here into drm_sched_rq_add_entity():

          if (drm_sched_policy == DRM_SCHED_POLICY_FIFO) drm_sched_rq_update_fifo_locked(entity, submit_ts);

We can then also drop adding the entity to the rr list when FIFO is in use.

Regards,
Christian.

Quoted 174 lines

To achieve this we rename drm_sched_rq_add_entity() to drm_sched_rq_add_entity_locked(), making it expect the rq->lock to be held, and also add the same expectation to drm_sched_rq_update_fifo_locked().

For more stream-lining we also add the run-queue as an explicit parameter to drm_sched_rq_remove_fifo_locked() to avoid both callers and callee having to dereference entity->rq.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
  drivers/gpu/drm/scheduler/sched_entity.c |  7 ++--
  drivers/gpu/drm/scheduler/sched_main.c   | 41 +++++++++++++-----------
  include/drm/gpu_scheduler.h              |  7 ++--
  3 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +5 -2
index b4c4f9923e0b..2102c726d275 100644
@@ -614,11 +614,14 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  		sched = rq->sched;
 
  		atomic_inc(sched->score);
-		drm_sched_rq_add_entity(rq, entity);
+
+		spin_lock(&rq->lock);
+		drm_sched_rq_add_entity_locked(rq, entity);
 
  		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-			drm_sched_rq_update_fifo_locked(entity, submit_ts);
+			drm_sched_rq_update_fifo_locked(entity, rq, submit_ts);
 
+		spin_unlock(&rq->lock);
  		spin_unlock(&entity->lock);
 
  		drm_sched_wakeup(sched, entity);
drivers/gpu/drm/scheduler/sched_main.c +22 -19
index 937e7d1cfc49..1ccd2aed2d32 100644
@@ -153,41 +153,44 @@ static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a,
  	return ktime_before(ent_a->oldest_job_waiting, ent_b->oldest_job_waiting);
  }
 
-static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *entity)
+static void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *entity,
+					    struct drm_sched_rq *rq)
  {
-	struct drm_sched_rq *rq = entity->rq;
-
  	if (!RB_EMPTY_NODE(&entity->rb_tree_node)) {
  		rb_erase_cached(&entity->rb_tree_node, &rq->rb_tree_root);
  		RB_CLEAR_NODE(&entity->rb_tree_node);
  	}
  }
 
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,
+				     struct drm_sched_rq *rq,
+				     ktime_t ts)
  {
  	lockdep_assert_held(&entity->lock);
+	lockdep_assert_held(&rq->lock);
 
-	spin_lock(&entity->rq->lock);
-
-	drm_sched_rq_remove_fifo_locked(entity);
+	drm_sched_rq_remove_fifo_locked(entity, rq);
 
  	entity->oldest_job_waiting = ts;
 
-	rb_add_cached(&entity->rb_tree_node, &entity->rq->rb_tree_root,
+	rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root,
  		      drm_sched_entity_compare_before);
-
-	spin_unlock(&entity->rq->lock);
  }
 
  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
  {
+	struct drm_sched_rq *rq;
+
  	/*
  	 * Both locks need to be grabbed, one to protect from entity->rq change
  	 * for entity from within concurrent drm_sched_entity_select_rq and the
  	 * other to update the rb tree structure.
  	 */
  	spin_lock(&entity->lock);
-	drm_sched_rq_update_fifo_locked(entity, ts);
+	rq = entity->rq;
+	spin_lock(&rq->lock);
+	drm_sched_rq_update_fifo_locked(entity, rq, ts);
+	spin_unlock(&rq->lock);
  	spin_unlock(&entity->lock);
  }
 
@@ -210,25 +213,23 @@ static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
  }
 
  /**
- * drm_sched_rq_add_entity - add an entity
+ * drm_sched_rq_add_entity_locked - add an entity
   *
   * @rq: scheduler run queue
   * @entity: scheduler entity
   *
   * Adds a scheduler entity to the run queue.
   */
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity)
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity *entity)
  {
+	lockdep_assert_held(&rq->lock);
+
  	if (!list_empty(&entity->list))
  		return;
 
-	spin_lock(&rq->lock);
-
  	atomic_inc(rq->sched->score);
  	list_add_tail(&entity->list, &rq->entities);
-
-	spin_unlock(&rq->lock);
  }
 
  /**
@@ -242,6 +243,8 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity)
  {
+	lockdep_assert_held(&entity->lock);
+
  	if (list_empty(&entity->list))
  		return;
 
@@ -254,7 +257,7 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  		rq->current_entity = NULL;
 
  	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-		drm_sched_rq_remove_fifo_locked(entity);
+		drm_sched_rq_remove_fifo_locked(entity, rq);
 
  	spin_unlock(&rq->lock);
  }
include/drm/gpu_scheduler.h +4 -3
index 5a1e4c803b90..2ad33e2fe2d2 100644
@@ -591,13 +591,14 @@ bool drm_sched_dependency_optimized(struct dma_fence* fence,
  				    struct drm_sched_entity *entity);
  void drm_sched_fault(struct drm_gpu_scheduler *sched);
 
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity);
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity *entity);
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity);
 
  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts);
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,
+				     struct drm_sched_rq *rq, ktime_t ts);
 
  int drm_sched_entity_init(struct drm_sched_entity *entity,
  			  enum drm_sched_priority priority,
#21 lore raw

Re: [PATCH 8/8] drm/sched: Further optimise drm_sched_entity_push_job

Quoted 9 lines · Christian König

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 7 lines

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled "drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit larger refactoring we can do the same optimisation on the rq->lock. (Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

Quoted 8 lines

I think that goes into the wrong direction.

Probably better to move this here into drm_sched_rq_add_entity():

          if (drm_sched_policy == DRM_SCHED_POLICY_FIFO) drm_sched_rq_update_fifo_locked(entity, submit_ts);

We can then also drop adding the entity to the rr list when FIFO is in use.

Unfortuntely there is a few other places which appear to rely on the list. Like drm_sched_fini, drm_sched_increase_karma and even amdgpu_job_stop_all_jobs_on_sched. Latter could perhaps be solved by adding an iterator helper to the scheduler, which would perhaps be a good move for component isolation. And first two could be handled by implementing a complete and mutually exclusive duality of how entities are walked depending on scheduling mode. Plus making the scheduling mode only be configurable at boot. It feels doable but significant work and in the meantime removing the double re-lock maybe acceptable?

Regards,

Tvrtko

Quoted 164 lines
Quoted 163 lines

To achieve this we rename drm_sched_rq_add_entity() to drm_sched_rq_add_entity_locked(), making it expect the rq->lock to be held, and also add the same expectation to drm_sched_rq_update_fifo_locked().

For more stream-lining we also add the run-queue as an explicit parameter to drm_sched_rq_remove_fifo_locked() to avoid both callers and callee having to dereference entity->rq.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
  drivers/gpu/drm/scheduler/sched_entity.c |  7 ++-- drivers/gpu/drm/scheduler/sched_main.c   | 41 +++++++++++++----------- include/drm/gpu_scheduler.h              |  7 ++--

  3 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +5 -2
index b4c4f9923e0b..2102c726d275 100644
@@ -614,11 +614,14 @@ void drm_sched_entity_push_job(struct 
 drm_sched_job *sched_job)
           sched = rq->sched;
           atomic_inc(sched->score);
-        drm_sched_rq_add_entity(rq, entity);
+
+        spin_lock(&rq->lock);
+        drm_sched_rq_add_entity_locked(rq, entity);
           if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-            drm_sched_rq_update_fifo_locked(entity, submit_ts);
+            drm_sched_rq_update_fifo_locked(entity, rq, submit_ts);
+        spin_unlock(&rq->lock);
           spin_unlock(&entity->lock);
           drm_sched_wakeup(sched, entity);
drivers/gpu/drm/scheduler/sched_main.c +22 -19
index 937e7d1cfc49..1ccd2aed2d32 100644
@@ -153,41 +153,44 @@ static __always_inline bool 
 drm_sched_entity_compare_before(struct rb_node *a,
       return ktime_before(ent_a->oldest_job_waiting, ent_b->oldest_job_waiting);
   }
-static inline void drm_sched_rq_remove_fifo_locked(struct 
 drm_sched_entity *entity)
+static void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity 
 *entity,
+                        struct drm_sched_rq *rq)
   {
-    struct drm_sched_rq *rq = entity->rq;
-
       if (!RB_EMPTY_NODE(&entity->rb_tree_node)) {
           rb_erase_cached(&entity->rb_tree_node, &rq->rb_tree_root);
           RB_CLEAR_NODE(&entity->rb_tree_node);
       }
   }
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, 
 ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,
+                     struct drm_sched_rq *rq,
+                     ktime_t ts)
   {
       lockdep_assert_held(&entity->lock);
+    lockdep_assert_held(&rq->lock);
-    spin_lock(&entity->rq->lock);
-
-    drm_sched_rq_remove_fifo_locked(entity);
+    drm_sched_rq_remove_fifo_locked(entity, rq);
       entity->oldest_job_waiting = ts;
-    rb_add_cached(&entity->rb_tree_node, &entity->rq->rb_tree_root,
+    rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root,
                 drm_sched_entity_compare_before);
-
-    spin_unlock(&entity->rq->lock);
   }
   void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
   {
+    struct drm_sched_rq *rq;
+
       /*
        * Both locks need to be grabbed, one to protect from entity->rq change
        * for entity from within concurrent drm_sched_entity_select_rq and the
        * other to update the rb tree structure.
        */
       spin_lock(&entity->lock);
-    drm_sched_rq_update_fifo_locked(entity, ts);
+    rq = entity->rq;
+    spin_lock(&rq->lock);
+    drm_sched_rq_update_fifo_locked(entity, rq, ts);
+    spin_unlock(&rq->lock);
       spin_unlock(&entity->lock);
   }
@@ -210,25 +213,23 @@ static void drm_sched_rq_init(struct 
 drm_gpu_scheduler *sched,
   }
   /**
- * drm_sched_rq_add_entity - add an entity
+ * drm_sched_rq_add_entity_locked - add an entity
    *
    * @rq: scheduler run queue
    * @entity: scheduler entity
    *
    * Adds a scheduler entity to the run queue.
    */
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-                 struct drm_sched_entity *entity)
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+                    struct drm_sched_entity *entity)
   {
+    lockdep_assert_held(&rq->lock);
+
       if (!list_empty(&entity->list))
           return;
-    spin_lock(&rq->lock);
-
       atomic_inc(rq->sched->score);
       list_add_tail(&entity->list, &rq->entities);
-
-    spin_unlock(&rq->lock);
   }
   /**
@@ -242,6 +243,8 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
   void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
                   struct drm_sched_entity *entity)
   {
+    lockdep_assert_held(&entity->lock);
+
       if (list_empty(&entity->list))
           return;
@@ -254,7 +257,7 @@ void drm_sched_rq_remove_entity(struct 
 drm_sched_rq *rq,
           rq->current_entity = NULL;
       if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-        drm_sched_rq_remove_fifo_locked(entity);
+        drm_sched_rq_remove_fifo_locked(entity, rq);
       spin_unlock(&rq->lock);
   }
include/drm/gpu_scheduler.h +4 -3
index 5a1e4c803b90..2ad33e2fe2d2 100644
@@ -591,13 +591,14 @@ bool drm_sched_dependency_optimized(struct 
 dma_fence* fence,
                       struct drm_sched_entity *entity);
   void drm_sched_fault(struct drm_gpu_scheduler *sched);
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-                 struct drm_sched_entity *entity);
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+                    struct drm_sched_entity *entity);
   void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
                   struct drm_sched_entity *entity);
   void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts);
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, 
 ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,
+                     struct drm_sched_rq *rq, ktime_t ts);
   int drm_sched_entity_init(struct drm_sched_entity *entity,
                 enum drm_sched_priority priority,
#22 lore raw

Re: [PATCH 8/8] drm/sched: Further optimise drm_sched_entity_push_job

Am 10.09.24 um 11:46 schrieb Tvrtko Ursulin:

Quoted 13 lines · Christian König
Quoted 11 lines

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 7 lines

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled "drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit larger refactoring we can do the same optimisation on the rq->lock. (Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

I think that goes into the wrong direction.

Quoted 8 lines
Quoted 6 lines

Probably better to move this here into drm_sched_rq_add_entity():

           if (drm_sched_policy == DRM_SCHED_POLICY_FIFO) drm_sched_rq_update_fifo_locked(entity, submit_ts);

We can then also drop adding the entity to the rr list when FIFO is in use.

Unfortuntely there is a few other places which appear to rely on the list. Like drm_sched_fini,

That should be only a warning.

Quoted 1 line

drm_sched_increase_karma and

The karma handling was another bad idea from AMD how to populate back errors to userspace and I've just recently documented together with Sima that we should use dma-fence errors instead.

Just didn't had time to tackle cleaning that up yet.

Quoted 1 line

even amdgpu_job_stop_all_jobs_on_sched.

Uff, seeing that for the first time just now. Another bad idea how to handle things which doesn't take the appropriate locks and looks racy to me.

Quoted 1 line

Latter could perhaps be solved by adding an iterator helper to the scheduler, which would perhaps be a good move for component isolation. And first two could be handled by implementing a complete and mutually exclusive duality of how entities are walked depending on scheduling mode. Plus making the scheduling mode only be configurable at boot. It feels doable but significant work and in the meantime removing the double re-lock maybe acceptable?

I don't think we should optimize for something we want to remove in the long term.

If possible I would rather say that we should completely drop the RR approach and only use FIFO or even something more sophisticated.

Regards,
Christian.

Quoted 170 lines

Regards,

Tvrtko

Quoted 166 lines
Quoted 165 lines

To achieve this we rename drm_sched_rq_add_entity() to drm_sched_rq_add_entity_locked(), making it expect the rq->lock to be held, and also add the same expectation to drm_sched_rq_update_fifo_locked().

For more stream-lining we also add the run-queue as an explicit parameter to drm_sched_rq_remove_fifo_locked() to avoid both callers and callee having to dereference entity->rq.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
  drivers/gpu/drm/scheduler/sched_entity.c |  7 ++-- drivers/gpu/drm/scheduler/sched_main.c   | 41
+++++++++++++-----------
  include/drm/gpu_scheduler.h              |  7 ++--

  3 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +5 -2
index b4c4f9923e0b..2102c726d275 100644
@@ -614,11 +614,14 @@ void drm_sched_entity_push_job(struct 
 drm_sched_job *sched_job)
           sched = rq->sched;
           atomic_inc(sched->score);
-        drm_sched_rq_add_entity(rq, entity);
+
+        spin_lock(&rq->lock);
+        drm_sched_rq_add_entity_locked(rq, entity);
           if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-            drm_sched_rq_update_fifo_locked(entity, submit_ts);
+            drm_sched_rq_update_fifo_locked(entity, rq, submit_ts);
+        spin_unlock(&rq->lock);
           spin_unlock(&entity->lock);
           drm_sched_wakeup(sched, entity);
drivers/gpu/drm/scheduler/sched_main.c +22 -19
index 937e7d1cfc49..1ccd2aed2d32 100644
@@ -153,41 +153,44 @@ static __always_inline bool 
 drm_sched_entity_compare_before(struct rb_node *a,
       return ktime_before(ent_a->oldest_job_waiting, ent_b->oldest_job_waiting);
   }
-static inline void drm_sched_rq_remove_fifo_locked(struct 
 drm_sched_entity *entity)
+static void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity 
 *entity,
+                        struct drm_sched_rq *rq)
   {
-    struct drm_sched_rq *rq = entity->rq;
-
       if (!RB_EMPTY_NODE(&entity->rb_tree_node)) {
           rb_erase_cached(&entity->rb_tree_node, &rq->rb_tree_root);
           RB_CLEAR_NODE(&entity->rb_tree_node);
       }
   }
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity 
 *entity, ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,
+                     struct drm_sched_rq *rq,
+                     ktime_t ts)
   {
       lockdep_assert_held(&entity->lock);
+    lockdep_assert_held(&rq->lock);
-    spin_lock(&entity->rq->lock);
-
-    drm_sched_rq_remove_fifo_locked(entity);
+    drm_sched_rq_remove_fifo_locked(entity, rq);
       entity->oldest_job_waiting = ts;
-    rb_add_cached(&entity->rb_tree_node, &entity->rq->rb_tree_root,
+    rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root,
                 drm_sched_entity_compare_before);
-
-    spin_unlock(&entity->rq->lock);
   }
   void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
   {
+    struct drm_sched_rq *rq;
+
       /*
        * Both locks need to be grabbed, one to protect from entity->rq change
        * for entity from within concurrent drm_sched_entity_select_rq and the
        * other to update the rb tree structure.
        */
       spin_lock(&entity->lock);
-    drm_sched_rq_update_fifo_locked(entity, ts);
+    rq = entity->rq;
+    spin_lock(&rq->lock);
+    drm_sched_rq_update_fifo_locked(entity, rq, ts);
+    spin_unlock(&rq->lock);
       spin_unlock(&entity->lock);
   }
@@ -210,25 +213,23 @@ static void drm_sched_rq_init(struct 
 drm_gpu_scheduler *sched,
   }
   /**
- * drm_sched_rq_add_entity - add an entity
+ * drm_sched_rq_add_entity_locked - add an entity
    *
    * @rq: scheduler run queue
    * @entity: scheduler entity
    *
    * Adds a scheduler entity to the run queue.
    */
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-                 struct drm_sched_entity *entity)
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+                    struct drm_sched_entity *entity)
   {
+    lockdep_assert_held(&rq->lock);
+
       if (!list_empty(&entity->list))
           return;
-    spin_lock(&rq->lock);
-
       atomic_inc(rq->sched->score);
       list_add_tail(&entity->list, &rq->entities);
-
-    spin_unlock(&rq->lock);
   }
   /**
@@ -242,6 +243,8 @@ void drm_sched_rq_add_entity(struct drm_sched_rq 
 *rq,
   void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
                   struct drm_sched_entity *entity)
   {
+    lockdep_assert_held(&entity->lock);
+
       if (list_empty(&entity->list))
           return;
@@ -254,7 +257,7 @@ void drm_sched_rq_remove_entity(struct 
 drm_sched_rq *rq,
           rq->current_entity = NULL;
       if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-        drm_sched_rq_remove_fifo_locked(entity);
+        drm_sched_rq_remove_fifo_locked(entity, rq);
       spin_unlock(&rq->lock);
   }
include/drm/gpu_scheduler.h +4 -3
index 5a1e4c803b90..2ad33e2fe2d2 100644
@@ -591,13 +591,14 @@ bool drm_sched_dependency_optimized(struct 
 dma_fence* fence,
                       struct drm_sched_entity *entity);
   void drm_sched_fault(struct drm_gpu_scheduler *sched);
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-                 struct drm_sched_entity *entity);
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+                    struct drm_sched_entity *entity);
   void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
                   struct drm_sched_entity *entity);
   void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts);
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity 
 *entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,
+                     struct drm_sched_rq *rq, ktime_t ts);
   int drm_sched_entity_init(struct drm_sched_entity *entity,
                 enum drm_sched_priority priority,
#23 lore raw

Re: [PATCH 8/8] drm/sched: Further optimise drm_sched_entity_push_job

Quoted 16 lines · Christian König

Am 10.09.24 um 11:46 schrieb Tvrtko Ursulin:

Quoted 15 lines · Christian König
Quoted 13 lines

Am 09.09.24 um 19:19 schrieb Tvrtko Ursulin:

Quoted 7 lines

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled "drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit larger refactoring we can do the same optimisation on the rq->lock. (Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

I think that goes into the wrong direction.

Probably better to move this here into drm_sched_rq_add_entity():

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

           if (drm_sched_policy == DRM_SCHED_POLICY_FIFO) drm_sched_rq_update_fifo_locked(entity, submit_ts);

We can then also drop adding the entity to the rr list when FIFO is in use.

Unfortuntely there is a few other places which appear to rely on the list. Like drm_sched_fini,

That should be only a warning.

Warning as in?

Quoted 6 lines
Quoted 1 line

drm_sched_increase_karma and

The karma handling was another bad idea from AMD how to populate back errors to userspace and I've just recently documented together with Sima that we should use dma-fence errors instead.

Just didn't had time to tackle cleaning that up yet.

Quoted 8 lines
Quoted 1 line

even amdgpu_job_stop_all_jobs_on_sched.

Uff, seeing that for the first time just now. Another bad idea how to handle things which doesn't take the appropriate locks and looks racy to me.

Quoted 1 line

Latter could perhaps be solved by adding an iterator helper to the scheduler, which would perhaps be a good move for component isolation. And first two could be handled by implementing a complete and mutually exclusive duality of how entities are walked depending on scheduling mode. Plus making the scheduling mode only be configurable at boot. It feels doable but significant work and in the meantime removing the double re-lock maybe acceptable?

I don't think we should optimize for something we want to remove in the long term.

I knew using the term optimise would just making things more difficult for myself. :) Lets view this as cleaning up the API to avoid the inelegance of taking the same lock twice right next to each other.

If we can achieve this while not making the API worse then there is nothing to lose either short, med or long term.

Quoted 1 line

If possible I would rather say that we should completely drop the RR approach and only use FIFO or even something more sophisticated.

No complaints from me, but I don't know how that would work other than putting a depreciation warning if someone selected RR. And keeping that for a good number of kernel releases. Any other ideas?

Regards,

Tvrtko

#24 lore raw

Re: [PATCH 8/8] drm/sched: Further optimise drm_sched_entity_push_job

In reply to Tvrtko Ursulin
Quoted 14 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled
"drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit larger refactoring we can do the same optimisation on the rq->lock (Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

To achieve this we rename drm_sched_rq_add_entity() to drm_sched_rq_add_entity_locked(), making it expect the rq->lock to be held, and also add the same expectation to drm_sched_rq_update_fifo_locked().

Quoted 4 lines

For more stream-lining we also add the run-queue as an explicit parameter
to drm_sched_rq_remove_fifo_locked() to avoid both callers and callee having to dereference entity->rq.

Why is dereferencing it a problem?

Quoted 54 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
 drivers/gpu/drm/scheduler/sched_entity.c |  7 ++-- drivers/gpu/drm/scheduler/sched_main.c   | 41 +++++++++++++--------- --
 include/drm/gpu_scheduler.h              |  7 ++--

 3 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +0 -0
index b4c4f9923e0b..2102c726d275 100644
@@ -614,11 +614,14 @@ void drm_sched_entity_push_job(struct
 drm_sched_job *sched_job)
  		sched = rq->sched;


  atomic_inc(sched->score);

drivers/gpu/drm/scheduler/sched_entity.c +3 -1
-		drm_sched_rq_add_entity(rq, entity);
+
+		spin_lock(&rq->lock);
+		drm_sched_rq_add_entity_locked(rq, entity);


  if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)

drivers/gpu/drm/scheduler/sched_entity.c +0 -1
-			drm_sched_rq_update_fifo_locked(entity,

submit_ts);

drivers/gpu/drm/scheduler/sched_entity.c +1 -0
+			drm_sched_rq_update_fifo_locked(entity, rq,

submit_ts);

drivers/gpu/drm/scheduler/sched_entity.c +1 -0
+		spin_unlock(&rq->lock);

  spin_unlock(&entity->lock);

  drm_sched_wakeup(sched, entity);

drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 937e7d1cfc49..1ccd2aed2d32 100644
@@ -153,41 +153,44 @@ static __always_inline bool
 drm_sched_entity_compare_before(struct rb_node *a,

  return ktime_before(ent_a->oldest_job_waiting, ent_b-

Quoted 1 line

oldest_job_waiting);

 }

-static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *entity)
+static void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *entity,

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 157

Quoted 1 line
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+					    struct drm_sched_rq *rq)

So here we'd add a new function parameter that still doesn't allow for getting rid of 'entity' as a parameter.

The API gets larger that way and readers will immediately wonder why sth is passed as a separate variable that could also be obtained through the pointer.

Quoted 14 lines

 {

drivers/gpu/drm/scheduler/sched_main.c +0 -2
-	struct drm_sched_rq *rq = entity->rq;
-

  if (!RB_EMPTY_NODE(&entity->rb_tree_node)) { rb_erase_cached(&entity->rb_tree_node, &rq-

Quoted 1 line

rb_tree_root);

  RB_CLEAR_NODE(&entity->rb_tree_node); }
 }

-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 167

Quoted 2 lines
drivers/gpu/drm/scheduler/sched_main.c +2 -0
+				     struct drm_sched_rq *rq,
+				     ktime_t ts)

The function is still called _locked. That implies to the reader that this function takes care of locking. But it doesn't anymore. Instead,

Quoted 32 lines

 {
  lockdep_assert_held(&entity->lock);

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+	lockdep_assert_held(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +1 -3
-	spin_lock(&entity->rq->lock);
-
-	drm_sched_rq_remove_fifo_locked(entity);
+	drm_sched_rq_remove_fifo_locked(entity, rq);


  entity->oldest_job_waiting = ts;

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-	rb_add_cached(&entity->rb_tree_node, &entity->rq-
Quoted 1 line

rb_tree_root,

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+	rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root,

        drm_sched_entity_compare_before);

drivers/gpu/drm/scheduler/sched_main.c +0 -2
-
-	spin_unlock(&entity->rq->lock);

 }

 void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
 {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	struct drm_sched_rq *rq;
+

  /*
  * Both locks need to be grabbed, one to protect from entity->rq change
  * for entity from within concurrent drm_sched_entity_select_rq and the
  * other to update the rb tree structure. */
  spin_lock(&entity->lock);

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 193

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +4 -1
-	drm_sched_rq_update_fifo_locked(entity, ts);
+	rq = entity->rq;
+	spin_lock(&rq->lock);
+	drm_sched_rq_update_fifo_locked(entity, rq, ts);
+	spin_unlock(&rq->lock);

its caller, drm_sched_rq_update_fifo(), now takes care of the locking. So if it all drm_sched_rq_update_fifo_locked() should be called drm_sched_rq_update_fifo_unlocked().

If such a change is really being done, we have to go through the entire scheduler and make sure that the suffix "_locked" is used consistently throughout the scheduler. And even better, as consistent with the kernel as possible.

To be honest folks, I don't think this entire "optimization" patch is that much of a good idea. The scheduler has real, big problems, such as race conditions, memory leaks and lack of documentation.

I think we should for the forseeable future dedicate our attention towards solving those problems, instead of optimizing things. Especially if the optimization might decrease readability as with the naming here.

P.

Quoted 85 lines

  spin_unlock(&entity->lock);
 }

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -210,25 +213,23 @@ static void drm_sched_rq_init(struct
 drm_gpu_scheduler *sched,
  }


 /**
- * drm_sched_rq_add_entity - add an entity
+ * drm_sched_rq_add_entity_locked - add an entity *
  * @rq: scheduler run queue
  * @entity: scheduler entity
  *
  * Adds a scheduler entity to the run queue. */

drivers/gpu/drm/scheduler/sched_main.c +2 -2
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity)
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity *entity)

 {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	lockdep_assert_held(&rq->lock);
+

  if (!list_empty(&entity->list))
  return;

drivers/gpu/drm/scheduler/sched_main.c +0 -2
-	spin_lock(&rq->lock);
-

  atomic_inc(rq->sched->score); list_add_tail(&entity->list, &rq->entities);

drivers/gpu/drm/scheduler/sched_main.c +0 -2
-
-	spin_unlock(&rq->lock);

 }

 /**

drivers/gpu/drm/scheduler/sched_main.c +2 -0
@@ -242,6 +243,8 @@ void drm_sched_rq_add_entity(struct drm_sched_rq
 *rq,
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity)
  {
+	lockdep_assert_held(&entity->lock);
+
  	if (list_empty(&entity->list))
  		return;

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -254,7 +257,7 @@ void drm_sched_rq_remove_entity(struct
 drm_sched_rq *rq,
  		rq->current_entity = NULL;


  if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)

drivers/gpu/drm/scheduler/sched_main.c +1 -1
-		drm_sched_rq_remove_fifo_locked(entity);
+		drm_sched_rq_remove_fifo_locked(entity, rq);


  spin_unlock(&rq->lock);
 }

include/drm/gpu_scheduler.h +0 -0
index 5a1e4c803b90..2ad33e2fe2d2 100644
@@ -591,13 +591,14 @@ bool drm_sched_dependency_optimized(struct
 dma_fence* fence,

      struct drm_sched_entity *entity);
 void drm_sched_fault(struct drm_gpu_scheduler *sched);

include/drm/gpu_scheduler.h +2 -2
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity);
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity

*entity);
 void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, struct drm_sched_entity *entity);
 void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts);
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,

include/drm/gpu_scheduler.h +1 -0
+				     struct drm_sched_rq *rq,

ktime_t ts);

 int drm_sched_entity_init(struct drm_sched_entity *entity, enum drm_sched_priority priority,

#25 lore raw

Re: [PATCH 8/8] drm/sched: Further optimise drm_sched_entity_push_job

Am 10.09.24 um 12:25 schrieb Philipp Stanner:

Quoted 15 lines · Tvrtko Ursulin
Quoted 14 lines

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled
"drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit larger refactoring we can do the same optimisation on the rq->lock (Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

To achieve this we rename drm_sched_rq_add_entity() to drm_sched_rq_add_entity_locked(), making it expect the rq->lock to be held, and also add the same expectation to drm_sched_rq_update_fifo_locked().

Quoted 5 lines
Quoted 4 lines

For more stream-lining we also add the run-queue as an explicit parameter
to drm_sched_rq_remove_fifo_locked() to avoid both callers and callee having to dereference entity->rq.

Why is dereferencing it a problem?

Maybe add "without holding the appropriate lock".

Christian.

Quoted 226 lines
Quoted 45 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
  drivers/gpu/drm/scheduler/sched_entity.c |  7 ++--
  drivers/gpu/drm/scheduler/sched_main.c   | 41 +++++++++++++---------
--
  include/drm/gpu_scheduler.h              |  7 ++--
  3 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +5 -2
index b4c4f9923e0b..2102c726d275 100644
@@ -614,11 +614,14 @@ void drm_sched_entity_push_job(struct
 drm_sched_job *sched_job)
  		sched = rq->sched;
 
  		atomic_inc(sched->score);
-		drm_sched_rq_add_entity(rq, entity);
+
+		spin_lock(&rq->lock);
+		drm_sched_rq_add_entity_locked(rq, entity);
 
  		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-			drm_sched_rq_update_fifo_locked(entity,
 submit_ts);
+			drm_sched_rq_update_fifo_locked(entity, rq,
 submit_ts);
 
+		spin_unlock(&rq->lock);

  spin_unlock(&entity->lock);

  drm_sched_wakeup(sched, entity);

drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 937e7d1cfc49..1ccd2aed2d32 100644

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool · line 161

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +2 -1
@@ -153,41 +153,44 @@ static __always_inline bool
 drm_sched_entity_compare_before(struct rb_node *a,
  	return ktime_before(ent_a->oldest_job_waiting, ent_b-
 > oldest_job_waiting);
  }
 
-static inline void drm_sched_rq_remove_fifo_locked(struct
 drm_sched_entity *entity)
+static void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity
 *entity,
+					    struct drm_sched_rq *rq)

So here we'd add a new function parameter that still doesn't allow for getting rid of 'entity' as a parameter.

The API gets larger that way and readers will immediately wonder why sth is passed as a separate variable that could also be obtained through the pointer.

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -2
  {
-	struct drm_sched_rq *rq = entity->rq;
-
  	if (!RB_EMPTY_NODE(&entity->rb_tree_node)) {
  		rb_erase_cached(&entity->rb_tree_node, &rq-
 > rb_tree_root);

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 167

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -1
  		RB_CLEAR_NODE(&entity->rb_tree_node);
  	}
  }
 
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity
 *entity, ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity
 *entity,
+				     struct drm_sched_rq *rq,
+				     ktime_t ts)

The function is still called _locked. That implies to the reader that this function takes care of locking. But it doesn't anymore. Instead,

Quoted 32 lines
drivers/gpu/drm/scheduler/sched_main.c +5 -6
  {
  	lockdep_assert_held(&entity->lock);
+	lockdep_assert_held(&rq->lock);
 
-	spin_lock(&entity->rq->lock);
-
-	drm_sched_rq_remove_fifo_locked(entity);
+	drm_sched_rq_remove_fifo_locked(entity, rq);
 
  	entity->oldest_job_waiting = ts;
 
-	rb_add_cached(&entity->rb_tree_node, &entity->rq-
 > rb_tree_root,
+	rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root,
  		      drm_sched_entity_compare_before);
-
-	spin_unlock(&entity->rq->lock);
  }
 
  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity,
 ktime_t ts)
  {
+	struct drm_sched_rq *rq;
+
  	/*
  	 * Both locks need to be grabbed, one to protect from

entity->rq change
  * for entity from within concurrent
drm_sched_entity_select_rq and the
  * other to update the rb tree structure.
  */
  spin_lock(&entity->lock);

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 193

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +4 -1
-	drm_sched_rq_update_fifo_locked(entity, ts);
+	rq = entity->rq;
+	spin_lock(&rq->lock);
+	drm_sched_rq_update_fifo_locked(entity, rq, ts);
+	spin_unlock(&rq->lock);

its caller, drm_sched_rq_update_fifo(), now takes care of the locking. So if it all drm_sched_rq_update_fifo_locked() should be called drm_sched_rq_update_fifo_unlocked().

If such a change is really being done, we have to go through the entire scheduler and make sure that the suffix "_locked" is used consistently throughout the scheduler. And even better, as consistent with the kernel as possible.

To be honest folks, I don't think this entire "optimization" patch is that much of a good idea. The scheduler has real, big problems, such as race conditions, memory leaks and lack of documentation.

I think we should for the forseeable future dedicate our attention towards solving those problems, instead of optimizing things. Especially if the optimization might decrease readability as with the naming here.

P.

Quoted 85 lines

  spin_unlock(&entity->lock);
 }

drivers/gpu/drm/scheduler/sched_main.c +5 -7
@@ -210,25 +213,23 @@ static void drm_sched_rq_init(struct
 drm_gpu_scheduler *sched,
  }
 
  /**
- * drm_sched_rq_add_entity - add an entity
+ * drm_sched_rq_add_entity_locked - add an entity
   *
   * @rq: scheduler run queue
   * @entity: scheduler entity
   *
   * Adds a scheduler entity to the run queue.
   */
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity)
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity *entity)
  {
+	lockdep_assert_held(&rq->lock);
+
  	if (!list_empty(&entity->list))
  		return;
 
-	spin_lock(&rq->lock);
-
  	atomic_inc(rq->sched->score);
  	list_add_tail(&entity->list, &rq->entities);
-
-	spin_unlock(&rq->lock);
  }
 

 /**

drivers/gpu/drm/scheduler/sched_main.c +3 -1
@@ -242,6 +243,8 @@ void drm_sched_rq_add_entity(struct drm_sched_rq
 *rq,
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity)
  {
+	lockdep_assert_held(&entity->lock);
+
  	if (list_empty(&entity->list))
  		return;
@@ -254,7 +257,7 @@ void drm_sched_rq_remove_entity(struct
 drm_sched_rq *rq,
  		rq->current_entity = NULL;
 
  	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-		drm_sched_rq_remove_fifo_locked(entity);
+		drm_sched_rq_remove_fifo_locked(entity, rq);
 
  	spin_unlock(&rq->lock);

 }

include/drm/gpu_scheduler.h +2 -3
index 5a1e4c803b90..2ad33e2fe2d2 100644
@@ -591,13 +591,14 @@ bool drm_sched_dependency_optimized(struct
 dma_fence* fence,
  				    struct drm_sched_entity
 *entity);
  void drm_sched_fault(struct drm_gpu_scheduler *sched);
 
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity);
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity
 *entity);
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity);
 
  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity,
 ktime_t ts);
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity

*entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,

include/drm/gpu_scheduler.h +1 -0
+				     struct drm_sched_rq *rq,

ktime_t ts);

 int drm_sched_entity_init(struct drm_sched_entity *entity, enum drm_sched_priority priority,

#26 lore raw

Re: [PATCH 8/8] drm/sched: Further optimise drm_sched_entity_push_job

In reply to Philipp Stanner
Quoted 15 lines · Philipp Stanner
Quoted 14 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled
"drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit larger refactoring we can do the same optimisation on the rq->lock (Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

To achieve this we rename drm_sched_rq_add_entity() to drm_sched_rq_add_entity_locked(), making it expect the rq->lock to be held, and also add the same expectation to drm_sched_rq_update_fifo_locked().

Quoted 6 lines
Quoted 4 lines

For more stream-lining we also add the run-queue as an explicit parameter
to drm_sched_rq_remove_fifo_locked() to avoid both callers and callee having to dereference entity->rq.

Why is dereferencing it a problem?

As you have noticed below the API is a bit unsightly. Consider for example this call chain:

drm_sched_entity_kill(entity)

    drm_sched_rq_remove_entity(entity->rq, entity);
        drm_sched_rq_remove_fifo_locked(entity);
            struct drm_sched_rq *rq = entity->rq;

A bit confused, no?

I thought adding rq to remove_fifo_locked at least removes one back and forth between the entity->rq and rq.

And then if we cache the rq in a local variable, after having explicitly taken the correct lock, we have this other call chain example:

drm_sched_entity_push_job()
...

    rq = entity->rq;
    spin_lock(rq->lock);

    drm_sched_rq_add_entity_locked(rq, entity);
    drm_sched_rq_update_fifo_locked(rq, entity, submit_ts);

    spin_unlock(rq->lock);

To me at least this reads more streamlined.

Quoted 49 lines
Quoted 49 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
  drivers/gpu/drm/scheduler/sched_entity.c |  7 ++--
  drivers/gpu/drm/scheduler/sched_main.c   | 41 +++++++++++++---------
--
  include/drm/gpu_scheduler.h              |  7 ++--
  3 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +5 -2
index b4c4f9923e0b..2102c726d275 100644
@@ -614,11 +614,14 @@ void drm_sched_entity_push_job(struct
 drm_sched_job *sched_job)
  		sched = rq->sched;
 
  		atomic_inc(sched->score);
-		drm_sched_rq_add_entity(rq, entity);
+
+		spin_lock(&rq->lock);
+		drm_sched_rq_add_entity_locked(rq, entity);
 
  		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-			drm_sched_rq_update_fifo_locked(entity,
 submit_ts);
+			drm_sched_rq_update_fifo_locked(entity, rq,
 submit_ts);
 
+		spin_unlock(&rq->lock);

  spin_unlock(&entity->lock);

  drm_sched_wakeup(sched, entity);

drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 937e7d1cfc49..1ccd2aed2d32 100644
@@ -153,41 +153,44 @@ static __always_inline bool
 drm_sched_entity_compare_before(struct rb_node *a,
  	return ktime_before(ent_a->oldest_job_waiting, ent_b-
 > oldest_job_waiting);
  }
 
Quoted 8 lines

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool · line 161

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2 -1
-static inline void drm_sched_rq_remove_fifo_locked(struct
 drm_sched_entity *entity)
+static void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity
 *entity,
+					    struct drm_sched_rq *rq)

So here we'd add a new function parameter that still doesn't allow for getting rid of 'entity' as a parameter.

We can't get rid of the entity.

Maaaybe instead we could get rid of the rq in the whole chain, I mean from drm_sched_rq_add_entity and drm_sched_rq_remove_entity to start with.

But then to remove double re-lock we still (like in this patch) need to make the callers take the locks and rename the helpers with _locked suffix. Otherwise it would be incosistent that a lock is taken outside the helpers with no _locked suffix.

I am not sure if that is better. All it achieves is remove the rq as explicit parameter my making the callees dereference it from the entity.

Worst part is all these helpers have drm_sched_rq_ prefix.. which to me reads as "we operate on rq". So not passing in rq is confusing to start with.

Granted, some confusion still remains with my approach since ideally, to those helpers, I wanted to add some asserts that rq == entity->rq...

Quoted 66 lines

The API gets larger that way and readers will immediately wonder why sth is passed as a separate variable that could also be obtained through the pointer.

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -2
  {
-	struct drm_sched_rq *rq = entity->rq;
-
  	if (!RB_EMPTY_NODE(&entity->rb_tree_node)) {
  		rb_erase_cached(&entity->rb_tree_node, &rq-
 > rb_tree_root);

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 167

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -1
  		RB_CLEAR_NODE(&entity->rb_tree_node);
  	}
  }
 
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity
 *entity, ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity
 *entity,
+				     struct drm_sched_rq *rq,
+				     ktime_t ts)

The function is still called _locked. That implies to the reader that this function takes care of locking. But it doesn't anymore. Instead,

Quoted 32 lines
drivers/gpu/drm/scheduler/sched_main.c +5 -6
  {
  	lockdep_assert_held(&entity->lock);
+	lockdep_assert_held(&rq->lock);
 
-	spin_lock(&entity->rq->lock);
-
-	drm_sched_rq_remove_fifo_locked(entity);
+	drm_sched_rq_remove_fifo_locked(entity, rq);
 
  	entity->oldest_job_waiting = ts;
 
-	rb_add_cached(&entity->rb_tree_node, &entity->rq-
 > rb_tree_root,
+	rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root,
  		      drm_sched_entity_compare_before);
-
-	spin_unlock(&entity->rq->lock);
  }
 
  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity,
 ktime_t ts)
  {
+	struct drm_sched_rq *rq;
+
  	/*
  	 * Both locks need to be grabbed, one to protect from

entity->rq change
  * for entity from within concurrent
drm_sched_entity_select_rq and the
  * other to update the rb tree structure.
  */
  spin_lock(&entity->lock);

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 193

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +4 -1
-	drm_sched_rq_update_fifo_locked(entity, ts);
+	rq = entity->rq;
+	spin_lock(&rq->lock);
+	drm_sched_rq_update_fifo_locked(entity, rq, ts);
+	spin_unlock(&rq->lock);

its caller, drm_sched_rq_update_fifo(), now takes care of the locking. So if it all drm_sched_rq_update_fifo_locked() should be called drm_sched_rq_update_fifo_unlocked().

Quoted 4 lines

If such a change is really being done, we have to go through the entire scheduler and make sure that the suffix "_locked" is used consistently throughout the scheduler. And even better, as consistent with the kernel as possible.

Use of _locked follows the existing pattern of drm_sched_rq_remove_fifo_locked :shrug:

Are you referring to drm_sched_start_timeout(_unlocked) which is the opposite pattern? (Although a more recent addition.)

The wider kernel also uses both patterns so don't know. Would be nice to align in the scheduler but drm_sched_start_timeout is out of scope for this series.

Quoted 8 lines

To be honest folks, I don't think this entire "optimization" patch is that much of a good idea. The scheduler has real, big problems, such as race conditions, memory leaks and lack of documentation.

I think we should for the forseeable future dedicate our attention towards solving those problems, instead of optimizing things. Especially if the optimization might decrease readability as with the naming here.

In principle I agree, but on the other hand lets first see if this patch is really making things any worse, or is perhaps just maintaining the status quo in the API elegance department, while at the same time removing the quite lazy double re-lock from the main submission path.

Regards,

Tvrtko

Quoted 90 lines

P.

Quoted 85 lines

  spin_unlock(&entity->lock);
 }

drivers/gpu/drm/scheduler/sched_main.c +5 -7
@@ -210,25 +213,23 @@ static void drm_sched_rq_init(struct
 drm_gpu_scheduler *sched,
  }
 
  /**
- * drm_sched_rq_add_entity - add an entity
+ * drm_sched_rq_add_entity_locked - add an entity
   *
   * @rq: scheduler run queue
   * @entity: scheduler entity
   *
   * Adds a scheduler entity to the run queue.
   */
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity)
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity *entity)
  {
+	lockdep_assert_held(&rq->lock);
+
  	if (!list_empty(&entity->list))
  		return;
 
-	spin_lock(&rq->lock);
-
  	atomic_inc(rq->sched->score);
  	list_add_tail(&entity->list, &rq->entities);
-
-	spin_unlock(&rq->lock);
  }
 

 /**

drivers/gpu/drm/scheduler/sched_main.c +3 -1
@@ -242,6 +243,8 @@ void drm_sched_rq_add_entity(struct drm_sched_rq
 *rq,
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity)
  {
+	lockdep_assert_held(&entity->lock);
+
  	if (list_empty(&entity->list))
  		return;
@@ -254,7 +257,7 @@ void drm_sched_rq_remove_entity(struct
 drm_sched_rq *rq,
  		rq->current_entity = NULL;
 
  	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-		drm_sched_rq_remove_fifo_locked(entity);
+		drm_sched_rq_remove_fifo_locked(entity, rq);
 
  	spin_unlock(&rq->lock);

 }

include/drm/gpu_scheduler.h +2 -3
index 5a1e4c803b90..2ad33e2fe2d2 100644
@@ -591,13 +591,14 @@ bool drm_sched_dependency_optimized(struct
 dma_fence* fence,
  				    struct drm_sched_entity
 *entity);
  void drm_sched_fault(struct drm_gpu_scheduler *sched);
 
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity);
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity
 *entity);
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity);
 
  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity,
 ktime_t ts);
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity

*entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,

include/drm/gpu_scheduler.h +1 -0
+				     struct drm_sched_rq *rq,

ktime_t ts);

 int drm_sched_entity_init(struct drm_sched_entity *entity, enum drm_sched_priority priority,

#27 lore raw

Re: [PATCH 8/8] drm/sched: Further optimise drm_sched_entity_push_job

Quoted 52 lines · Tvrtko Ursulin
Quoted 19 lines · Philipp Stanner
Quoted 18 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled
"drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit
larger refactoring we can do the same optimisation on the rq-

Quoted 1 line

lock

(Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

To achieve this we rename drm_sched_rq_add_entity() to drm_sched_rq_add_entity_locked(), making it expect the rq->lock to be
held, and also add the same expectation to drm_sched_rq_update_fifo_locked().

Quoted 7 lines
Quoted 5 lines

For more stream-lining we also add the run-queue as an explicit parameter
to drm_sched_rq_remove_fifo_locked() to avoid both callers and callee
having to dereference entity->rq.

Why is dereferencing it a problem?

As you have noticed below the API is a bit unsightly. Consider for example this call chain:

drm_sched_entity_kill(entity) drm_sched_rq_remove_entity(entity->rq, entity); drm_sched_rq_remove_fifo_locked(entity); struct drm_sched_rq *rq = entity->rq;

A bit confused, no?

I thought adding rq to remove_fifo_locked at least removes one back and
forth between the entity->rq and rq.

And then if we cache the rq in a local variable, after having explicitly
taken the correct lock, we have this other call chain example:

drm_sched_entity_push_job()
...
     rq = entity->rq;
     spin_lock(rq->lock);

Quoted 6 lines

     drm_sched_rq_add_entity_locked(rq, entity); drm_sched_rq_update_fifo_locked(rq, entity, submit_ts);

     spin_unlock(rq->lock);

To me at least this reads more streamlined.

Alright, doesn't sound to bad, but

Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
  drivers/gpu/drm/scheduler/sched_entity.c |  7 ++-- drivers/gpu/drm/scheduler/sched_main.c   | 41 +++++++++++++----
-----
--
  include/drm/gpu_scheduler.h              |  7 ++--

  3 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +0 -0
index b4c4f9923e0b..2102c726d275 100644
@@ -614,11 +614,14 @@ void drm_sched_entity_push_job(struct
 drm_sched_job *sched_job)
   		sched = rq->sched;


   atomic_inc(sched->score);

drivers/gpu/drm/scheduler/sched_entity.c +3 -1
-		drm_sched_rq_add_entity(rq, entity);
+
+		spin_lock(&rq->lock);
+		drm_sched_rq_add_entity_locked(rq, entity);


   if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)

drivers/gpu/drm/scheduler/sched_entity.c +0 -1
-			drm_sched_rq_update_fifo_locked(entity,

submit_ts);

drivers/gpu/drm/scheduler/sched_entity.c +1 -0
+			drm_sched_rq_update_fifo_locked(entity,

rq,
submit_ts);

drivers/gpu/drm/scheduler/sched_entity.c +1 -0
+		spin_unlock(&rq->lock);

   spin_unlock(&entity->lock);

   drm_sched_wakeup(sched, entity);

drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 937e7d1cfc49..1ccd2aed2d32 100644
@@ -153,41 +153,44 @@ static __always_inline bool
 drm_sched_entity_compare_before(struct rb_node *a,

   return ktime_before(ent_a->oldest_job_waiting, ent_b-

Quoted 1 line

oldest_job_waiting);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

  }

-static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *entity)
+static void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity
*entity,

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+					    struct drm_sched_rq

*rq)

I would then at least like to see a comment somewhere telling the reader why rq is taken as a separate variable. One might otherwise easily wonder why it's not obtained through the entity and what the difference is.

Quoted 10 lines
Quoted 4 lines

So here we'd add a new function parameter that still doesn't allow for
getting rid of 'entity' as a parameter.

We can't get rid of the entity.

Maaaybe instead we could get rid of the rq in the whole chain, I mean from drm_sched_rq_add_entity and drm_sched_rq_remove_entity to start with.

Let's postpone that.

Quoted 11 lines

But then to remove double re-lock we still (like in this patch) need to
make the callers take the locks and rename the helpers with _locked suffix. Otherwise it would be incosistent that a lock is taken outside
the helpers with no _locked suffix.

I am not sure if that is better. All it achieves is remove the rq as explicit parameter my making the callees dereference it from the entity.

OK, as I see it now it would actually be desirable to have suffix _locked indicate that the caller must hold all necessary locks. So your patch would actually make that consistent within drm/sched/.

Looks good

P.

Quoted 216 lines

Worst part is all these helpers have drm_sched_rq_ prefix.. which to me
reads as "we operate on rq". So not passing in rq is confusing to start
with.

Granted, some confusion still remains with my approach since ideally, to
those helpers, I wanted to add some asserts that rq == entity->rq...

Quoted 70 lines

The API gets larger that way and readers will immediately wonder why
sth is passed as a separate variable that could also be obtained through the pointer.

Quoted 14 lines

  {

drivers/gpu/drm/scheduler/sched_main.c +0 -2
-	struct drm_sched_rq *rq = entity->rq;
-

   if (!RB_EMPTY_NODE(&entity->rb_tree_node)) { rb_erase_cached(&entity->rb_tree_node, &rq-

Quoted 1 line

rb_tree_root);

   RB_CLEAR_NODE(&entity->rb_tree_node); }
  }

-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 167

Quoted 2 lines
drivers/gpu/drm/scheduler/sched_main.c +2 -0
+				     struct drm_sched_rq *rq,
+				     ktime_t ts)

The function is still called _locked. That implies to the reader that
this function takes care of locking. But it doesn't anymore. Instead,

Quoted 32 lines

  {
   lockdep_assert_held(&entity->lock);

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+	lockdep_assert_held(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +1 -3
-	spin_lock(&entity->rq->lock);
-
-	drm_sched_rq_remove_fifo_locked(entity);
+	drm_sched_rq_remove_fifo_locked(entity, rq);


   entity->oldest_job_waiting = ts;

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-	rb_add_cached(&entity->rb_tree_node, &entity->rq-
Quoted 1 line

rb_tree_root,

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+	rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root,

         drm_sched_entity_compare_before);

drivers/gpu/drm/scheduler/sched_main.c +0 -2
-
-	spin_unlock(&entity->rq->lock);

  }

  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
  {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	struct drm_sched_rq *rq;
+

   /*
   * Both locks need to be grabbed, one to protect from entity->rq change
   * for entity from within concurrent drm_sched_entity_select_rq and the
   * other to update the rb tree structure. */
   spin_lock(&entity->lock);

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 193

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +4 -1
-	drm_sched_rq_update_fifo_locked(entity, ts);
+	rq = entity->rq;
+	spin_lock(&rq->lock);
+	drm_sched_rq_update_fifo_locked(entity, rq, ts);
+	spin_unlock(&rq->lock);

its caller, drm_sched_rq_update_fifo(), now takes care of the locking.
So if it all drm_sched_rq_update_fifo_locked() should be called drm_sched_rq_update_fifo_unlocked().

Quoted 6 lines

If such a change is really being done, we have to go through the entire
scheduler and make sure that the suffix "_locked" is used consistently
throughout the scheduler. And even better, as consistent with the kernel as possible.

Use of _locked follows the existing pattern of
drm_sched_rq_remove_fifo_locked :shrug:

Are you referring to drm_sched_start_timeout(_unlocked) which is the opposite pattern? (Although a more recent addition.)

The wider kernel also uses both patterns so don't know. Would be nice to
align in the scheduler but drm_sched_start_timeout is out of scope for
this series.

Quoted 11 lines

To be honest folks, I don't think this entire "optimization" patch is
that much of a good idea. The scheduler has real, big problems, such as
race conditions, memory leaks and lack of documentation.

I think we should for the forseeable future dedicate our attention towards solving those problems, instead of optimizing things. Especially if the optimization might decrease readability as with the
naming here.

In principle I agree, but on the other hand lets first see if this patch
is really making things any worse, or is perhaps just maintaining the status quo in the API elegance department, while at the same time removing the quite lazy double re-lock from the main submission path.

Regards,

Tvrtko

Quoted 93 lines

P.

Quoted 88 lines

   spin_unlock(&entity->lock);
  }

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -210,25 +213,23 @@ static void drm_sched_rq_init(struct
 drm_gpu_scheduler *sched,
   }


  /**
- * drm_sched_rq_add_entity - add an entity
+ * drm_sched_rq_add_entity_locked - add an entity *
   * @rq: scheduler run queue
   * @entity: scheduler entity
   *
   * Adds a scheduler entity to the run queue. */

drivers/gpu/drm/scheduler/sched_main.c +2 -2
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity)
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity

*entity)
  {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	lockdep_assert_held(&rq->lock);
+

   if (!list_empty(&entity->list))
   return;

drivers/gpu/drm/scheduler/sched_main.c +0 -2
-	spin_lock(&rq->lock);
-

   atomic_inc(rq->sched->score); list_add_tail(&entity->list, &rq->entities);

drivers/gpu/drm/scheduler/sched_main.c +0 -2
-
-	spin_unlock(&rq->lock);

  }

  /**

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -242,6 +243,8 @@ void drm_sched_rq_add_entity(struct

drm_sched_rq
*rq,
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, struct drm_sched_entity *entity) {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	lockdep_assert_held(&entity->lock);
+

   if (list_empty(&entity->list))
   return;

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -254,7 +257,7 @@ void drm_sched_rq_remove_entity(struct
 drm_sched_rq *rq,
   		rq->current_entity = NULL;


   if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)

drivers/gpu/drm/scheduler/sched_main.c +1 -1
-		drm_sched_rq_remove_fifo_locked(entity);
+		drm_sched_rq_remove_fifo_locked(entity, rq);


   spin_unlock(&rq->lock);
  }

include/drm/gpu_scheduler.h +0 -0
index 5a1e4c803b90..2ad33e2fe2d2 100644
@@ -591,13 +591,14 @@ bool drm_sched_dependency_optimized(struct
 dma_fence* fence,

       struct drm_sched_entity *entity);
  void drm_sched_fault(struct drm_gpu_scheduler *sched);

include/drm/gpu_scheduler.h +2 -2
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity);
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity

*entity);
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, struct drm_sched_entity *entity);

  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts);
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,

include/drm/gpu_scheduler.h +1 -0
+				     struct drm_sched_rq *rq,

ktime_t ts);

  int drm_sched_entity_init(struct drm_sched_entity *entity, enum drm_sched_priority priority,

#28 lore raw

Re: [PATCH 8/8] drm/sched: Further optimise drm_sched_entity_push_job

Quoted 114 lines · Philipp Stanner
Quoted 52 lines · Tvrtko Ursulin
Quoted 19 lines · Philipp Stanner
Quoted 18 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Having removed one re-lock cycle on the entity->lock in a patch titled
"drm/sched: Optimise drm_sched_entity_push_job", with only a tiny bit
larger refactoring we can do the same optimisation on the rq-

Quoted 1 line

lock

(Currently both drm_sched_rq_add_entity() and drm_sched_rq_update_fifo_locked() take and release the same lock.)

To achieve this we rename drm_sched_rq_add_entity() to drm_sched_rq_add_entity_locked(), making it expect the rq->lock to be
held, and also add the same expectation to drm_sched_rq_update_fifo_locked().

Quoted 7 lines
Quoted 5 lines

For more stream-lining we also add the run-queue as an explicit parameter
to drm_sched_rq_remove_fifo_locked() to avoid both callers and callee
having to dereference entity->rq.

Why is dereferencing it a problem?

As you have noticed below the API is a bit unsightly. Consider for example this call chain:

drm_sched_entity_kill(entity)
     drm_sched_rq_remove_entity(entity->rq, entity); drm_sched_rq_remove_fifo_locked(entity); struct drm_sched_rq *rq = entity->rq;

A bit confused, no?

I thought adding rq to remove_fifo_locked at least removes one back and
forth between the entity->rq and rq.

And then if we cache the rq in a local variable, after having explicitly
taken the correct lock, we have this other call chain example:

drm_sched_entity_push_job()
...
     rq = entity->rq;
     spin_lock(rq->lock);

Quoted 6 lines

     drm_sched_rq_add_entity_locked(rq, entity); drm_sched_rq_update_fifo_locked(rq, entity, submit_ts);

     spin_unlock(rq->lock);

To me at least this reads more streamlined.

Alright, doesn't sound to bad, but

Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Luben Tuikov <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
   drivers/gpu/drm/scheduler/sched_entity.c |  7 ++--
   drivers/gpu/drm/scheduler/sched_main.c   | 41 +++++++++++++----
-----
--
   include/drm/gpu_scheduler.h              |  7 ++--
   3 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +5 -2
index b4c4f9923e0b..2102c726d275 100644
@@ -614,11 +614,14 @@ void drm_sched_entity_push_job(struct
 drm_sched_job *sched_job)
   		sched = rq->sched;
 
   		atomic_inc(sched->score);
-		drm_sched_rq_add_entity(rq, entity);
+
+		spin_lock(&rq->lock);
+		drm_sched_rq_add_entity_locked(rq, entity);
 
   		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-			drm_sched_rq_update_fifo_locked(entity,
 submit_ts);
+			drm_sched_rq_update_fifo_locked(entity,
 rq,
 submit_ts);
 
+		spin_unlock(&rq->lock);
   		spin_unlock(&entity->lock);
 
   		drm_sched_wakeup(sched, entity);
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 937e7d1cfc49..1ccd2aed2d32 100644
@@ -153,41 +153,44 @@ static __always_inline bool
 drm_sched_entity_compare_before(struct rb_node *a,
   	return ktime_before(ent_a->oldest_job_waiting, ent_b-
 > oldest_job_waiting);
   }
 
Quoted 12 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
-static inline void drm_sched_rq_remove_fifo_locked(struct
 drm_sched_entity *entity)
+static void drm_sched_rq_remove_fifo_locked(struct

drm_sched_entity
*entity,

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+					    struct drm_sched_rq

*rq)

I would then at least like to see a comment somewhere telling the reader why rq is taken as a separate variable. One might otherwise easily wonder why it's not obtained through the entity and what the difference is.

I failed to find a nice place to put it. I'll send v3 of the series with some changes soo and then please have another look at this patch and see if you can think of where it would look good.

Regards,

Tvrtko

Quoted 251 lines
Quoted 11 lines
Quoted 5 lines

So here we'd add a new function parameter that still doesn't allow for
getting rid of 'entity' as a parameter.

We can't get rid of the entity.

Maaaybe instead we could get rid of the rq in the whole chain, I mean from drm_sched_rq_add_entity and drm_sched_rq_remove_entity to start with.

Let's postpone that.

Quoted 11 lines

But then to remove double re-lock we still (like in this patch) need to
make the callers take the locks and rename the helpers with _locked suffix. Otherwise it would be incosistent that a lock is taken outside
the helpers with no _locked suffix.

I am not sure if that is better. All it achieves is remove the rq as explicit parameter my making the callees dereference it from the entity.

OK, as I see it now it would actually be desirable to have suffix _locked indicate that the caller must hold all necessary locks. So your patch would actually make that consistent within drm/sched/.

Looks good

P.

Quoted 216 lines

Worst part is all these helpers have drm_sched_rq_ prefix.. which to me
reads as "we operate on rq". So not passing in rq is confusing to start
with.

Granted, some confusion still remains with my approach since ideally, to
those helpers, I wanted to add some asserts that rq == entity->rq...

Quoted 70 lines

The API gets larger that way and readers will immediately wonder why
sth is passed as a separate variable that could also be obtained through the pointer.

Quoted 14 lines

  {

drivers/gpu/drm/scheduler/sched_main.c +0 -2
-	struct drm_sched_rq *rq = entity->rq;
-
   	if (!RB_EMPTY_NODE(&entity->rb_tree_node)) {
   		rb_erase_cached(&entity->rb_tree_node, &rq-
Quoted 1 line

rb_tree_root);

   RB_CLEAR_NODE(&entity->rb_tree_node); }
  }

-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts)
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 167

Quoted 2 lines
drivers/gpu/drm/scheduler/sched_main.c +2 -0
+				     struct drm_sched_rq *rq,
+				     ktime_t ts)

The function is still called _locked. That implies to the reader that
this function takes care of locking. But it doesn't anymore. Instead,

Quoted 32 lines

  {
   lockdep_assert_held(&entity->lock);

drivers/gpu/drm/scheduler/sched_main.c +2 -4
+	lockdep_assert_held(&rq->lock);
 
-	spin_lock(&entity->rq->lock);
-
-	drm_sched_rq_remove_fifo_locked(entity);
+	drm_sched_rq_remove_fifo_locked(entity, rq);
 
   	entity->oldest_job_waiting = ts;
 
-	rb_add_cached(&entity->rb_tree_node, &entity->rq-
Quoted 1 line

rb_tree_root,

drivers/gpu/drm/scheduler/sched_main.c +1 -2
+	rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root,
   		      drm_sched_entity_compare_before);
-
-	spin_unlock(&entity->rq->lock);
   }
 
   void drm_sched_rq_update_fifo(struct drm_sched_entity *entity,

ktime_t ts)
  {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	struct drm_sched_rq *rq;
+
   	/*
   	 * Both locks need to be grabbed, one to protect from

entity->rq change
   * for entity from within concurrent
drm_sched_entity_select_rq and the
   * other to update the rb tree structure.
   */
   spin_lock(&entity->lock);

drivers/gpu/drm/scheduler/sched_main.c static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, · line 193

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +4 -1
-	drm_sched_rq_update_fifo_locked(entity, ts);
+	rq = entity->rq;
+	spin_lock(&rq->lock);
+	drm_sched_rq_update_fifo_locked(entity, rq, ts);
+	spin_unlock(&rq->lock);

its caller, drm_sched_rq_update_fifo(), now takes care of the locking.
So if it all drm_sched_rq_update_fifo_locked() should be called drm_sched_rq_update_fifo_unlocked().

Quoted 6 lines

If such a change is really being done, we have to go through the entire
scheduler and make sure that the suffix "_locked" is used consistently
throughout the scheduler. And even better, as consistent with the kernel as possible.

Use of _locked follows the existing pattern of
drm_sched_rq_remove_fifo_locked :shrug:

Are you referring to drm_sched_start_timeout(_unlocked) which is the opposite pattern? (Although a more recent addition.)

The wider kernel also uses both patterns so don't know. Would be nice to
align in the scheduler but drm_sched_start_timeout is out of scope for
this series.

Quoted 11 lines

To be honest folks, I don't think this entire "optimization" patch is
that much of a good idea. The scheduler has real, big problems, such as
race conditions, memory leaks and lack of documentation.

I think we should for the forseeable future dedicate our attention towards solving those problems, instead of optimizing things. Especially if the optimization might decrease readability as with the
naming here.

In principle I agree, but on the other hand lets first see if this patch
is really making things any worse, or is perhaps just maintaining the status quo in the API elegance department, while at the same time removing the quite lazy double re-lock from the main submission path.

Regards,

Tvrtko

Quoted 93 lines

P.

Quoted 88 lines

   spin_unlock(&entity->lock);
  }

drivers/gpu/drm/scheduler/sched_main.c +5 -7
@@ -210,25 +213,23 @@ static void drm_sched_rq_init(struct
 drm_gpu_scheduler *sched,
   }
 
   /**
- * drm_sched_rq_add_entity - add an entity
+ * drm_sched_rq_add_entity_locked - add an entity
    *
    * @rq: scheduler run queue
    * @entity: scheduler entity
    *
    * Adds a scheduler entity to the run queue.
    */
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity)
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity
 *entity)
   {
+	lockdep_assert_held(&rq->lock);
+
   	if (!list_empty(&entity->list))
   		return;
 
-	spin_lock(&rq->lock);
-
   	atomic_inc(rq->sched->score);
   	list_add_tail(&entity->list, &rq->entities);
-
-	spin_unlock(&rq->lock);
   }

  /**

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -242,6 +243,8 @@ void drm_sched_rq_add_entity(struct

drm_sched_rq
*rq,
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, struct drm_sched_entity *entity) {

drivers/gpu/drm/scheduler/sched_main.c +3 -1
+	lockdep_assert_held(&entity->lock);
+
   	if (list_empty(&entity->list))
   		return;
@@ -254,7 +257,7 @@ void drm_sched_rq_remove_entity(struct
 drm_sched_rq *rq,
   		rq->current_entity = NULL;
 
   	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
-		drm_sched_rq_remove_fifo_locked(entity);
+		drm_sched_rq_remove_fifo_locked(entity, rq);
 
   	spin_unlock(&rq->lock);

  }

include/drm/gpu_scheduler.h +2 -2
index 5a1e4c803b90..2ad33e2fe2d2 100644
@@ -591,13 +591,14 @@ bool drm_sched_dependency_optimized(struct
 dma_fence* fence,
   				    struct drm_sched_entity
 *entity);
   void drm_sched_fault(struct drm_gpu_scheduler *sched);
 
-void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
-			     struct drm_sched_entity *entity);
+void drm_sched_rq_add_entity_locked(struct drm_sched_rq *rq,
+				    struct drm_sched_entity
 *entity);
   void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
   				struct drm_sched_entity
 *entity);
 
   void drm_sched_rq_update_fifo(struct drm_sched_entity *entity,

ktime_t ts);
-void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts);
+void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity,

include/drm/gpu_scheduler.h +1 -0
+				     struct drm_sched_rq *rq,

ktime_t ts);

  int drm_sched_entity_init(struct drm_sched_entity *entity, enum drm_sched_priority priority,