Skip to contentSkip to conversation

DRM scheduler fixes and improvements

PATCH In review Tvrtko Ursulin 3 participants 13 messages Sep 24, 2024

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

v3 cover DRM scheduler fixes and improvements

From: Tvrtko Ursulin <[email protected]>

All reviewed now, re-sending after rebasing on latest drm-tip so it is in a mergeable state.

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 | 53 +++++++++++++++++-------
 drivers/gpu/drm/scheduler/sched_main.c   | 32 +++++++-------
 include/drm/gpu_scheduler.h              | 28 +++++++------
 3 files changed, 68 insertions(+), 45 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH v3 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 567e5ace6d0c..0e002c17fcb6 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
#2 lore raw

PATCH v3 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 0e002c17fcb6..a75eede8bf8d 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);
+		drm_sched_wakeup(sched);
 	}
 }
 EXPORT_SYMBOL(drm_sched_entity_push_job);
Signature · 1 line
2.46.0
#3 lore raw

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

I've pushed the first to drm-misc-next, but that one here fails to apply cleanly.

Christian.

Am 24.09.24 um 12:19 schrieb Tvrtko Ursulin:

Quoted 63 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 +8 -2
index 0e002c17fcb6..a75eede8bf8d 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);
+		drm_sched_wakeup(sched);
  	}
  }
  EXPORT_SYMBOL(drm_sched_entity_push_job);
#4 lore raw

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

Quoted 1 line · Christian König

I've pushed the first to drm-misc-next, but that one here fails to apply cleanly.

This appears due 440d52b370b0 ("drm/sched: Fix dynamic job-flow control race") in drm-misc-fixes.

In theory 1-3 from my series are fixes. Should they also go to drm-misc-fixes? I am not too familiar with the drm-misc flow.

Or the series now needs to wait for some backmerge?

Regards,

Tvrtko

Quoted 63 lines

Am 24.09.24 um 12:19 schrieb Tvrtko Ursulin:

Quoted 61 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 +8 -2
index 0e002c17fcb6..a75eede8bf8d 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);
+        drm_sched_wakeup(sched);
       }
   }
   EXPORT_SYMBOL(drm_sched_entity_push_job);
#5 lore raw

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

Am 24.09.24 um 16:12 schrieb Tvrtko Ursulin:

Quoted 7 lines · Christian König
Quoted 1 line

I've pushed the first to drm-misc-next, but that one here fails to apply cleanly.

This appears due 440d52b370b0 ("drm/sched: Fix dynamic job-flow control race") in drm-misc-fixes.

In theory 1-3 from my series are fixes. Should they also go to drm-misc-fixes? I am not too familiar with the drm-misc flow.

Ah shit, in that case you should have spitted the patches up into fixes and next. Going to push the first 3 to fixes.

Quoted 1 line

Or the series now needs to wait for some backmerge?

Are the remaining 3 patches independent? If not then we need to wait for a backmerge.

Regards,
Christian.

Quoted 68 lines

Regards,

Tvrtko

Quoted 63 lines

Am 24.09.24 um 12:19 schrieb Tvrtko Ursulin:

Quoted 61 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 +8 -2
index 0e002c17fcb6..a75eede8bf8d 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);
+        drm_sched_wakeup(sched);
       }
   }
   EXPORT_SYMBOL(drm_sched_entity_push_job);
#6 lore raw

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

Quoted 10 lines · Christian König

Am 24.09.24 um 16:12 schrieb Tvrtko Ursulin:

Quoted 7 lines · Christian König
Quoted 1 line

I've pushed the first to drm-misc-next, but that one here fails to apply cleanly.

This appears due 440d52b370b0 ("drm/sched: Fix dynamic job-flow control race") in drm-misc-fixes.

In theory 1-3 from my series are fixes. Should they also go to drm-misc-fixes? I am not too familiar with the drm-misc flow.

Ah shit, in that case you should have spitted the patches up into fixes and next. Going to push the first 3 to fixes.

Sorry my drm-intel ways of thinking (cherry picked fixes) are hard to get rid of. Hence the series was structured as 1-3 fixes, 4-8 refactors etc.

Now appears it is too late to pull out the first one from drm-misc-next.

Quoted 3 lines
Quoted 1 line

Or the series now needs to wait for some backmerge?

Are the remaining 3 patches independent? If not then we need to wait for a backmerge.

These are independent:

Fixes:

1/8 "drm/sched: Add locking to drm_sched_entity_modify_sched"

Not fixes:

5/8 "drm/sched: Stop setting current entity in FIFO mode" 6/8 "drm/sched: Re-order struct drm_sched_rq members for clarity"

While the rest touch at least some common areas.

2/8 and 3/8 are also fixes.

4/8, 7/8 and 8/8 not fixes but depend on 2/8 and 3/8.

Regards,

Tvrtko

Quoted 64 lines
Quoted 63 lines
Quoted 63 lines

Am 24.09.24 um 12:19 schrieb Tvrtko Ursulin:

Quoted 61 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 +8 -2
index 0e002c17fcb6..a75eede8bf8d 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);
+        drm_sched_wakeup(sched);
       }
   }
   EXPORT_SYMBOL(drm_sched_entity_push_job);
#7 lore raw

PATCH v3 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 a75eede8bf8d..b2cf3e0c1838 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
#8 lore raw

PATCH v3 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.

v2:
* Remove drm_sched_rq_update_fifo() altogether. (Christian)

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 | 13 +++++++++----
 drivers/gpu/drm/scheduler/sched_main.c   |  6 +++---
 include/drm/gpu_scheduler.h              |  2 +-
 3 files changed, 13 insertions(+), 8 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +9 -4
index b2cf3e0c1838..59f710afe992 100644
@@ -514,8 +514,12 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
 		struct drm_sched_job *next;
 
 		next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
-		if (next)
-			drm_sched_rq_update_fifo(entity, next->submit_ts);
+		if (next) {
+			spin_lock(&entity->rq_lock);
+			drm_sched_rq_update_fifo_locked(entity,
+							next->submit_ts);
+			spin_unlock(&entity->rq_lock);
+		}
 	}
 
 	/* Jobs and entities might have different lifecycles. Since we're
@@ -615,10 +619,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);
 	}
drivers/gpu/drm/scheduler/sched_main.c +3 -3
index 03c532590e2a..e312eb6ac85a 100644
@@ -163,14 +163,15 @@ 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,7 +182,6 @@ void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
 		      drm_sched_entity_compare_before);
 
 	spin_unlock(&entity->rq->lock);
-	spin_unlock(&entity->rq_lock);
 }
 
 /**
include/drm/gpu_scheduler.h +1 -1
index 0b679700a63a..f62cc31fea18 100644
@@ -593,7 +593,7 @@ 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);
 
-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
#9 lore raw

PATCH v3 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 e312eb6ac85a..130b53f02bbf 100644
@@ -349,7 +349,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
#10 lore raw

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

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Current kerneldoc for struct drm_sched_rq incompletely documents what fields are protected by the lock.

This is not good because it is misleading.

Lets fix it by listing all the elements which are protected by the lock.

While at it, lets also re-order the members so all protected by the lock are in a single group.

v2:
* Refer variables by kerneldoc syntax, more verbose commit text. (Philipp)

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 f62cc31fea18..33c60889e0a3 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 @entities, @rb_tree_root 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
#11 lore raw

PATCH v3 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 | 28 ++++++++++++------------
 drivers/gpu/drm/scheduler/sched_main.c   |  2 +-
 include/drm/gpu_scheduler.h              | 15 +++++++------
 3 files changed, 23 insertions(+), 22 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +14 -14
index 59f710afe992..5ebbba77e77d 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);
 
@@ -515,10 +515,10 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
 
 		next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
 		if (next) {
-			spin_lock(&entity->rq_lock);
+			spin_lock(&entity->lock);
 			drm_sched_rq_update_fifo_locked(entity,
 							next->submit_ts);
-			spin_unlock(&entity->rq_lock);
+			spin_unlock(&entity->lock);
 		}
 	}
 
@@ -559,14 +559,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;
@@ -606,9 +606,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;
@@ -623,7 +623,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);
 	}
drivers/gpu/drm/scheduler/sched_main.c +1 -1
index 130b53f02bbf..5628a4c78242 100644
@@ -170,7 +170,7 @@ void drm_sched_rq_update_fifo_locked(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.
 	 */
-	lockdep_assert_held(&entity->rq_lock);
+	lockdep_assert_held(&entity->lock);
 
 	spin_lock(&entity->rq->lock);
 
include/drm/gpu_scheduler.h +8 -7
index 33c60889e0a3..6db30fcb18dc 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
#12 lore raw

PATCH v3 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 make drm_sched_rq_update_fifo_locked() and drm_sched_rq_add_entity() expect the rq->lock to be held.

We also align drm_sched_rq_update_fifo_locked(), drm_sched_rq_add_entity() and drm_sched_rq_remove_fifo_locked() function signatures, by adding rq as a parameter to the latter.

v2:
* Fix after rebase of the series.
* Avoid naming incosistency between drm_sched_rq_add/remove. (Christian)

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 | 12 ++++++++--
 drivers/gpu/drm/scheduler/sched_main.c   | 29 ++++++++++++------------
 include/drm/gpu_scheduler.h              |  3 ++-
 3 files changed, 26 insertions(+), 18 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +10 -2
index 5ebbba77e77d..0aa90829c1d2 100644
@@ -515,9 +515,14 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
 
 		next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
 		if (next) {
+			struct drm_sched_rq *rq;
+
 			spin_lock(&entity->lock);
-			drm_sched_rq_update_fifo_locked(entity,
+			rq = entity->rq;
+			spin_lock(&rq->lock);
+			drm_sched_rq_update_fifo_locked(entity, rq,
 							next->submit_ts);
+			spin_unlock(&rq->lock);
 			spin_unlock(&entity->lock);
 		}
 	}
@@ -618,11 +623,14 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 		sched = rq->sched;
 
 		atomic_inc(sched->score);
+
+		spin_lock(&rq->lock);
 		drm_sched_rq_add_entity(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);
drivers/gpu/drm/scheduler/sched_main.c +14 -15
index 5628a4c78242..bdb55545b57c 100644
@@ -153,17 +153,18 @@ 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)
 {
 	/*
 	 * Both locks need to be grabbed, one to protect from entity->rq change
@@ -171,17 +172,14 @@ void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, ktime_t ts
 	 * other to update the rb tree structure.
 	 */
 	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);
 }
 
 /**
@@ -213,15 +211,14 @@ static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
 void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
 			     struct drm_sched_entity *entity)
 {
+	lockdep_assert_held(&entity->lock);
+	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);
 }
 
 /**
@@ -235,6 +232,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;
 
@@ -247,7 +246,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 -1
index 6db30fcb18dc..b1dc57fd1c5c 100644
@@ -596,7 +596,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);
 
-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