Skip to contentSkip to conversation

drm/amdgpu: increment share sched score on entity selection

PATCH In review Pierre-Eric Pelloux-Prayer 6 participants 13 messages Aug 22, 2025 – Sep 26, 2025

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

PATCH 1/2 drm/amdgpu: increment share sched score on entity selection

For hw engines that can't load balance jobs, entities are "statically" load balanced: on their first submit, they select the best scheduler based on its score.
The score is made up of 2 parts:
* the job queue depth (how much jobs are executing/waiting)
* the number of entities assigned

The second part is only relevant for the static load balance:
it's a way to consider how many entities are attached to this scheduler, knowing that if they ever submit jobs they will go to this one.

For rings that can load balance jobs freely, idle entities aren't a concern and shouldn't impact the scheduler's decisions.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 23 ++++++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
 2 files changed, 19 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +18 -5
index f5d5c45ddc0d..4a078d2d98c5 100644
@@ -206,9 +206,11 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
 {
 	struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
 	struct amdgpu_device *adev = ctx->mgr->adev;
+	bool static_load_balancing = false;
 	struct amdgpu_ctx_entity *entity;
 	enum drm_sched_priority drm_prio;
 	unsigned int hw_prio, num_scheds;
+	struct amdgpu_ring *aring;
 	int32_t ctx_prio;
 	int r;
 
@@ -236,17 +238,22 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
 		r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
 						&num_scheds, &scheds);
 		if (r)
-			goto cleanup_entity;
+			goto error_free_entity;
 	}
 
 	/* disable load balance if the hw engine retains context among dependent jobs */
-	if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
-	    hw_ip == AMDGPU_HW_IP_VCN_DEC ||
-	    hw_ip == AMDGPU_HW_IP_UVD_ENC ||
-	    hw_ip == AMDGPU_HW_IP_UVD) {
+	static_load_balancing = hw_ip == AMDGPU_HW_IP_VCN_ENC ||
+				hw_ip == AMDGPU_HW_IP_VCN_DEC ||
+				hw_ip == AMDGPU_HW_IP_UVD_ENC ||
+				hw_ip == AMDGPU_HW_IP_UVD;
+
+	if (static_load_balancing) {
 		sched = drm_sched_pick_best(scheds, num_scheds);
 		scheds = &sched;
 		num_scheds = 1;
+		aring = container_of(sched, struct amdgpu_ring, sched);
+		entity->sched_ring_score = aring->sched_score;
+		atomic_inc(entity->sched_ring_score);
 	}
 
 	r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
@@ -264,6 +271,9 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
 	drm_sched_entity_fini(&entity->entity);
 
 error_free_entity:
+	if (static_load_balancing)
+		atomic_dec(entity->sched_ring_score);
+
 	kfree(entity);
 
 	return r;
@@ -514,6 +524,9 @@ static void amdgpu_ctx_do_release(struct kref *ref)
 			if (!ctx->entities[i][j])
 				continue;
 
+			if (ctx->entities[i][j]->sched_ring_score)
+				atomic_dec(ctx->entities[i][j]->sched_ring_score);
+
 			drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
 		}
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
index 090dfe86f75b..076a0e165ce0 100644
@@ -39,6 +39,7 @@ struct amdgpu_ctx_entity {
 	uint32_t		hw_ip;
 	uint64_t		sequence;
 	struct drm_sched_entity	entity;
+	atomic_t		*sched_ring_score;
 	struct dma_fence	*fences[];
 };
 
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 drm/sched: limit sched score update to jobs change

Currently, the scheduler score is incremented when a job is pushed to an entity and when an entity is attached to the scheduler.

This leads to some bad scheduling decision where the score value is largely made of idle entities.

For instance, a scenario with 2 schedulers and where 10 entities submit a single job, then do nothing, each scheduler will probably end up with a score of 5.
Now, 5 userspace apps exit, so their entities will be dropped. In the worst case, these apps' entities where all attached to the same scheduler and we end up with score=5 (the 5 remaining entities) and score=0, despite the 2 schedulers being idle.
When new entities show up, they will all select the second scheduler based on its low score value, instead of alternating between the 2.

Some amdgpu rings depended on this feature, but the previous commit implemented the same thing in amdgpu directly so it can be safely removed from drm/sched.

---
 drivers/gpu/drm/scheduler/sched_main.c | 2 --
 1 file changed, 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -2
index 5a550fd76bf0..e6d232a8ec58 100644
@@ -206,7 +206,6 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
 	if (!list_empty(&entity->list))
 		return;
 
-	atomic_inc(rq->sched->score);
 	list_add_tail(&entity->list, &rq->entities);
 }
 
@@ -228,7 +227,6 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
 
 	spin_lock(&rq->lock);
 
-	atomic_dec(rq->sched->score);
 	list_del_init(&entity->list);
 
 	if (rq->current_entity == entity)
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v1 2/2] drm/sched: limit sched score update to jobs change

Quoted 2 lines · Pierre-Eric Pelloux-Prayer

Currently, the scheduler score is incremented when a job is pushed to an entity and when an entity is attached to the scheduler.

It's indeed awkward why attaching is treated equivalently to job submission.

Can you expand the documentation for drm_sched_init_args a bit so that it gets clearer what the score is supposed to do?

Quoted 9 lines

This leads to some bad scheduling decision where the score value is largely made of idle entities.

For instance, a scenario with 2 schedulers and where 10 entities submit a single job, then do nothing, each scheduler will probably end up with a score of 5.
Now, 5 userspace apps exit, so their entities will be dropped.

"entities will be dropped" == "drm_sched_entity_kill() gets called", right?

Quoted 2 lines

In
the worst case, these apps' entities where all attached to the same

s/where/were

or better yet: "could be"

Quoted 2 lines

scheduler and we end up with score=5 (the 5 remaining entities) and score=0, despite the 2 schedulers being idle.

Sounds indeed like a (small) problem to me.

Quoted 6 lines

When new entities show up, they will all select the second scheduler based on its low score value, instead of alternating between the 2.

Some amdgpu rings depended on this feature, but the previous commit implemented the same thing in amdgpu directly so it can be safely removed from drm/sched.

Can we be that sure that other drivers don't depend on it, though? I suspect it's likely that it's just amdgpu, but…

BTW, since you're cleaning up related stuff currently: I saw that it seems that the only driver that sets &struct drm_sched_init_args.score is amdgpu. Would be cool if you can take a look whether that's still needed.

P.

Quoted 26 lines

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

 1 file changed, 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 5a550fd76bf0..e6d232a8ec58 100644
@@ -206,7 +206,6 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  	if (!list_empty(&entity->list))
  		return;

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-	atomic_inc(rq->sched->score);

  list_add_tail(&entity->list, &rq->entities); }

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -228,7 +227,6 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,


  spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-	atomic_dec(rq->sched->score);

  list_del_init(&entity->list);

  if (rq->current_entity == entity)

#3 lore raw

Re: [PATCH v1 2/2] drm/sched: limit sched score update to jobs change

Le 25/08/2025 à 15:13, Philipp Stanner a écrit :

Quoted 9 lines · Pierre-Eric Pelloux-Prayer
Quoted 2 lines

Currently, the scheduler score is incremented when a job is pushed to an entity and when an entity is attached to the scheduler.

It's indeed awkward why attaching is treated equivalently to job submission.

Can you expand the documentation for drm_sched_init_args a bit so that it gets clearer what the score is supposed to do?

drm_sched_init_args.score is the feature allowing multiple schedulers to share a score so I suppose you meant drm_gpu_scheduler.score?

The doc currently says "score to help loadbalancer pick a idle sched" which is a bit vague. It could be modified to become:

@score: holds the number of yet-to-be-completed jobs pushed to each scheduler.
It's used when load balancing between different schedulers.

What do you think?

Quoted 5 lines
Quoted 5 lines

This leads to some bad scheduling decision where the score value is largely made of idle entities.

Quoted 8 lines
Quoted 5 lines

For instance, a scenario with 2 schedulers and where 10 entities submit a single job, then do nothing, each scheduler will probably end up with a score of 5.
Now, 5 userspace apps exit, so their entities will be dropped.

"entities will be dropped" == "drm_sched_entity_kill() gets called", right?

Yes.

Quoted 6 lines
Quoted 2 lines

In
the worst case, these apps' entities where all attached to the same

s/where/were

or better yet: "could be"

Will fix, thanks.

Quoted 10 lines
Quoted 3 lines

scheduler and we end up with score=5 (the 5 remaining entities) and score=0, despite the 2 schedulers being idle.

Sounds indeed like a (small) problem to me.

Quoted 3 lines

When new entities show up, they will all select the second scheduler based on its low score value, instead of alternating between the 2.

Quoted 7 lines
Quoted 3 lines

Some amdgpu rings depended on this feature, but the previous commit implemented the same thing in amdgpu directly so it can be safely removed from drm/sched.

Can we be that sure that other drivers don't depend on it, though? I suspect it's likely that it's just amdgpu, but…

Aside from the new "rocket" as pointed out by Tvrtko, amdgpu is the only driver passing more than one schedulers to entities so they're the only ones that could be affected.

I verified amdgpu and Tvrtko pinged the rocket maintainers in the other thread.

Quoted 6 lines

BTW, since you're cleaning up related stuff currently: I saw that it seems that the only driver that sets &struct drm_sched_init_args.score is amdgpu. Would be cool if you can take a look whether that's still needed.

It cannot really be removed yet as it's useful when a single hardware block is exposed through different schedulers (so pushing jobs to one of the schedulers should increase the load of the underlying hw).

Thanks,
Pierre-Eric

Quoted 30 lines

P.

Quoted 26 lines
---
  drivers/gpu/drm/scheduler/sched_main.c | 2 --
  1 file changed, 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -2
index 5a550fd76bf0..e6d232a8ec58 100644
@@ -206,7 +206,6 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  	if (!list_empty(&entity->list))
  		return;
 
-	atomic_inc(rq->sched->score);
  	list_add_tail(&entity->list, &rq->entities);
  }
 
@@ -228,7 +227,6 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
 
  	spin_lock(&rq->lock);
 
-	atomic_dec(rq->sched->score);
  	list_del_init(&entity->list);
 
  	if (rq->current_entity == entity)
#4 lore raw

Re: [PATCH v1 2/2] drm/sched: limit sched score update to jobs change

Quoted 10 lines · Pierre-Eric Pelloux-Prayer

Le 25/08/2025 à 15:13, Philipp Stanner a écrit :

Quoted 7 lines · Pierre-Eric Pelloux-Prayer
Quoted 2 lines

Currently, the scheduler score is incremented when a job is pushed to an entity and when an entity is attached to the scheduler.

It's indeed awkward why attaching is treated equivalently to job submission.

Quoted 6 lines
Quoted 2 lines

Can you expand the documentation for drm_sched_init_args a bit so that it gets clearer what the score is supposed to do?

drm_sched_init_args.score is the feature allowing multiple schedulers to share a score so I suppose you meant drm_gpu_scheduler.score?

I mean both.

struct drm_sched_init_args has a @score which is passed by the driver during init and will then be stored in drm_gpu_scheduler.score

The docu should be improved for both.

Quoted 6 lines

The doc currently says "score to help loadbalancer pick a idle sched" which is a bit vague. It could be modified to become:

   @score: holds the number of yet-to-be-completed jobs pushed to each scheduler. It's used when load balancing between different schedulers.

"to each scheduler" reads a bit as if each scheduler has an individual score. Is it used that way, though? I think it's a pointer because all schedulers share this atomic. Thus, it "holds the total number of yet- to-be-completed jobs for all schedulers sharing this atomic", right?

Quoted 14 lines

What do you think?

Quoted 11 lines
Quoted 10 lines

This leads to some bad scheduling decision where the score value is largely made of idle entities.

For instance, a scenario with 2 schedulers and where 10 entities submit a single job, then do nothing, each scheduler will probably end up with a score of 5.
Now, 5 userspace apps exit, so their entities will be dropped.

Quoted 4 lines
Quoted 2 lines

"entities will be dropped" == "drm_sched_entity_kill() gets called", right?

Yes.

then nit: better say "their entities will be killed" instead of dropped.

Quoted 28 lines
Quoted 7 lines
Quoted 3 lines

In
the worst case, these apps' entities where all attached to the same

s/where/were

or better yet: "could be"

Will fix, thanks.

Quoted 10 lines
Quoted 3 lines

scheduler and we end up with score=5 (the 5 remaining entities) and score=0, despite the 2 schedulers being idle.

Sounds indeed like a (small) problem to me.

Quoted 3 lines

When new entities show up, they will all select the second scheduler based on its low score value, instead of alternating between the 2.

Quoted 7 lines
Quoted 3 lines

Some amdgpu rings depended on this feature, but the previous commit implemented the same thing in amdgpu directly so it can be safely removed from drm/sched.

Can we be that sure that other drivers don't depend on it, though? I suspect it's likely that it's just amdgpu, but…

Quoted 5 lines

Aside from the new "rocket" as pointed out by Tvrtko, amdgpu is the only driver passing more than one schedulers to entities so they're the only ones that could be affected.

I verified amdgpu and Tvrtko pinged the rocket maintainers in the other thread.

Very good! Then let's give those guys a few days to jump into the discussion.

Quoted 11 lines
Quoted 7 lines

BTW, since you're cleaning up related stuff currently: I saw that it seems that the only driver that sets &struct drm_sched_init_args.score is amdgpu. Would be cool if you can take a look whether that's still needed.

It cannot really be removed yet as it's useful when a single hardware block is exposed through different schedulers (so pushing jobs to one of the schedulers should increase the load of the underlying hw).

OK.

Thx
P.

Quoted 34 lines

Thanks,
Pierre-Eric

Quoted 30 lines

P.

Quoted 26 lines

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

  1 file changed, 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 5a550fd76bf0..e6d232a8ec58 100644
@@ -206,7 +206,6 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
   	if (!list_empty(&entity->list))
   		return;

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-	atomic_inc(rq->sched->score);

   list_add_tail(&entity->list, &rq->entities); }

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -228,7 +227,6 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,


   spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-	atomic_dec(rq->sched->score);

   list_del_init(&entity->list);

   if (rq->current_entity == entity)

Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH v1 2/2] drm/sched: limit sched score update to jobs change

In reply to Pierre-Eric Pelloux-Prayer

+ Tomeu and Oded

Quoted 38 lines · Pierre-Eric Pelloux-Prayer

Currently, the scheduler score is incremented when a job is pushed to an entity and when an entity is attached to the scheduler.

This leads to some bad scheduling decision where the score value is largely made of idle entities.

For instance, a scenario with 2 schedulers and where 10 entities submit a single job, then do nothing, each scheduler will probably end up with a score of 5.
Now, 5 userspace apps exit, so their entities will be dropped. In the worst case, these apps' entities where all attached to the same scheduler and we end up with score=5 (the 5 remaining entities) and score=0, despite the 2 schedulers being idle.
When new entities show up, they will all select the second scheduler based on its low score value, instead of alternating between the 2.

Some amdgpu rings depended on this feature, but the previous commit implemented the same thing in amdgpu directly so it can be safely removed from drm/sched.

---
  drivers/gpu/drm/scheduler/sched_main.c | 2 --
  1 file changed, 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -1
index 5a550fd76bf0..e6d232a8ec58 100644
@@ -206,7 +206,6 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  	if (!list_empty(&entity->list))
  		return;
 
-	atomic_inc(rq->sched->score);
  	list_add_tail(&entity->list, &rq->entities);
  }
 

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, · line 232

Quoted 7 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -1
@@ -228,7 +227,6 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
 
  	spin_lock(&rq->lock);
 
-	atomic_dec(rq->sched->score);
  	list_del_init(&entity->list);
 
  	if (rq->current_entity == entity)

LGTM.

Only detail is, I did a revisit of the scheduler users and it looks like the new rocket driver is the only one other than amdgpu which passes a list of more than one scheduler to drm_sched_entity_init. I don't *think* it would be affected though. It would still pick the least loaded (based on active jobs) scheduler at job submit time. Unless there is some hidden behaviour in that driver where it would be important to consider number of entities too. Anyway, it would be good for rocket driver to double-check and ack.

Regards,

Tvrtko

Acked-by
#6 lore raw

Re: [PATCH v1 2/2] drm/sched: limit sched score update to jobs change

Quoted 56 lines · Tvrtko Ursulin

+ Tomeu and Oded

Quoted 38 lines · Pierre-Eric Pelloux-Prayer

Currently, the scheduler score is incremented when a job is pushed to an entity and when an entity is attached to the scheduler.

This leads to some bad scheduling decision where the score value is largely made of idle entities.

For instance, a scenario with 2 schedulers and where 10 entities submit a single job, then do nothing, each scheduler will probably end up with a score of 5.
Now, 5 userspace apps exit, so their entities will be dropped. In the worst case, these apps' entities where all attached to the same scheduler and we end up with score=5 (the 5 remaining entities) and score=0, despite the 2 schedulers being idle.
When new entities show up, they will all select the second scheduler based on its low score value, instead of alternating between the 2.

Some amdgpu rings depended on this feature, but the previous commit implemented the same thing in amdgpu directly so it can be safely removed from drm/sched.

---
  drivers/gpu/drm/scheduler/sched_main.c | 2 --
  1 file changed, 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -1
index 5a550fd76bf0..e6d232a8ec58 100644
@@ -206,7 +206,6 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
      if (!list_empty(&entity->list))
              return;
 
-     atomic_inc(rq->sched->score);
      list_add_tail(&entity->list, &rq->entities);
  }
 

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, · line 232

Quoted 7 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -1
@@ -228,7 +227,6 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
 
      spin_lock(&rq->lock);
 
-     atomic_dec(rq->sched->score);
      list_del_init(&entity->list);
 
      if (rq->current_entity == entity)

LGTM.

Quoted 8 lines

Only detail is, I did a revisit of the scheduler users and it looks like the new rocket driver is the only one other than amdgpu which passes a list of more than one scheduler to drm_sched_entity_init. I don't *think* it would be affected though. It would still pick the least loaded (based on active jobs) scheduler at job submit time. Unless there is some hidden behaviour in that driver where it would be important to consider number of entities too. Anyway, it would be good for rocket driver to double-check and ack.

Hello,

thanks for pinging. I think it should be fine for Rocket.

Regards,

Tomeu

#7 lore raw

Re: [PATCH v1 2/2] drm/sched: limit sched score update to jobs change

In reply to Tvrtko Ursulin

Hi,

Le 01/09/2025 à 11:20, Tvrtko Ursulin a écrit :

Quoted 48 lines

+ Tomeu and Oded

Quoted 36 lines · Pierre-Eric Pelloux-Prayer

Currently, the scheduler score is incremented when a job is pushed to an entity and when an entity is attached to the scheduler.

This leads to some bad scheduling decision where the score value is largely made of idle entities.

For instance, a scenario with 2 schedulers and where 10 entities submit a single job, then do nothing, each scheduler will probably end up with a score of 5.
Now, 5 userspace apps exit, so their entities will be dropped. In the worst case, these apps' entities where all attached to the same scheduler and we end up with score=5 (the 5 remaining entities) and score=0, despite the 2 schedulers being idle.
When new entities show up, they will all select the second scheduler based on its low score value, instead of alternating between the 2.

Some amdgpu rings depended on this feature, but the previous commit implemented the same thing in amdgpu directly so it can be safely removed from drm/sched.

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

  1 file changed, 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -1
index 5a550fd76bf0..e6d232a8ec58 100644
@@ -206,7 +206,6 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
       if (!list_empty(&entity->list))
           return;
-    atomic_inc(rq->sched->score);
       list_add_tail(&entity->list, &rq->entities);
   }

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, · line 229

Quoted 4 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -1
@@ -228,7 +227,6 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
       spin_lock(&rq->lock);
-    atomic_dec(rq->sched->score);
       list_del_init(&entity->list);
       if (rq->current_entity == entity)

LGTM.

Quoted 2 lines

Thanks.

Quoted 2 lines

Only detail is, I did a revisit of the scheduler users and it looks like the new rocket driver is the only one other than amdgpu which passes a list of more than one scheduler to drm_sched_entity_init. I don't *think* it would be affected though. It would still pick the least loaded (based on active jobs) scheduler at job submit time. Unless there is some hidden behaviour in that driver where it would be important to consider number of entities too. Anyway, it would be good for rocket driver to double-check and ack.

Tomeu, Oded: any concerns about this change?

#8 lore raw

Re: [PATCH v1 2/2] drm/sched: limit sched score update to jobs change

Quoted 56 lines · Pierre-Eric Pelloux-Prayer

Hi,

Le 01/09/2025 à 11:20, Tvrtko Ursulin a écrit :

Quoted 50 lines

+ Tomeu and Oded

Quoted 37 lines · Pierre-Eric Pelloux-Prayer

Currently, the scheduler score is incremented when a job is pushed to an entity and when an entity is attached to the scheduler.

This leads to some bad scheduling decision where the score value is largely made of idle entities.

For instance, a scenario with 2 schedulers and where 10 entities submit a single job, then do nothing, each scheduler will probably end up with a score of 5.
Now, 5 userspace apps exit, so their entities will be dropped. In the worst case, these apps' entities where all attached to the same scheduler and we end up with score=5 (the 5 remaining entities) and score=0, despite the 2 schedulers being idle.
When new entities show up, they will all select the second scheduler based on its low score value, instead of alternating between the 2.

Some amdgpu rings depended on this feature, but the previous commit implemented the same thing in amdgpu directly so it can be safely removed from drm/sched.

Signed-off-by Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux- [email protected]>

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

  1 file changed, 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -1
index 5a550fd76bf0..e6d232a8ec58 100644
@@ -206,7 +206,6 @@ void drm_sched_rq_add_entity(struct drm_sched_rq 
 *rq,
       if (!list_empty(&entity->list))
           return;
-    atomic_inc(rq->sched->score);
       list_add_tail(&entity->list, &rq->entities);
   }

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_rq_remove_entity(struct · line 230

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -1
@@ -228,7 +227,6 @@ void drm_sched_rq_remove_entity(struct 
 drm_sched_rq *rq,
       spin_lock(&rq->lock);
-    atomic_dec(rq->sched->score);
       list_del_init(&entity->list);
       if (rq->current_entity == entity)

LGTM.

Quoted 2 lines
Quoted 6 lines

Thanks.

Quoted 2 lines

Only detail is, I did a revisit of the scheduler users and it looks like the new rocket driver is the only one other than amdgpu which passes a list of more than one scheduler to drm_sched_entity_init. I don't *think* it would be affected though. It would still pick the least loaded (based on active jobs) scheduler at job submit time. Unless there is some hidden behaviour in that driver where it would be important to consider number of entities too. Anyway, it would be good for rocket driver to double-check and ack.

Tomeu, Oded: any concerns about this change?

Tomeu acked it in case you missed it:

https://lore.kernel.org/dri-devel/[email protected]/T/#m4efa0d69211b2bb378cfb302e459faa30e57f58c

Regards,

Tvrtko

#9 lore raw

Re: [PATCH v1 2/2] drm/sched: limit sched score update to jobs change

Le 26/09/2025 à 10:24, Tvrtko Ursulin a écrit :

Quoted 58 lines · Pierre-Eric Pelloux-Prayer
Quoted 56 lines

Hi,

Le 01/09/2025 à 11:20, Tvrtko Ursulin a écrit :

Quoted 48 lines

+ Tomeu and Oded

Quoted 36 lines · Pierre-Eric Pelloux-Prayer

Currently, the scheduler score is incremented when a job is pushed to an entity and when an entity is attached to the scheduler.

This leads to some bad scheduling decision where the score value is largely made of idle entities.

For instance, a scenario with 2 schedulers and where 10 entities submit a single job, then do nothing, each scheduler will probably end up with a score of 5.
Now, 5 userspace apps exit, so their entities will be dropped. In the worst case, these apps' entities where all attached to the same scheduler and we end up with score=5 (the 5 remaining entities) and score=0, despite the 2 schedulers being idle.
When new entities show up, they will all select the second scheduler based on its low score value, instead of alternating between the 2.

Some amdgpu rings depended on this feature, but the previous commit implemented the same thing in amdgpu directly so it can be safely removed from drm/sched.

Signed-off-by Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux- [email protected]>

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

  1 file changed, 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -1
index 5a550fd76bf0..e6d232a8ec58 100644
@@ -206,7 +206,6 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
       if (!list_empty(&entity->list))
           return;
-    atomic_inc(rq->sched->score);
       list_add_tail(&entity->list, &rq->entities);
   }

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, · line 229

Quoted 4 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -1
@@ -228,7 +227,6 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
       spin_lock(&rq->lock);
-    atomic_dec(rq->sched->score);
       list_del_init(&entity->list);
       if (rq->current_entity == entity)

LGTM.

Quoted 2 lines

Thanks.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Only detail is, I did a revisit of the scheduler users and it looks like the new rocket driver is the only one other than amdgpu which passes a list of more than one scheduler to drm_sched_entity_init. I don't *think* it would be affected though. It would still pick the least loaded (based on active jobs) scheduler at job submit time. Unless there is some hidden behaviour in that driver where it would be important to consider number of entities too. Anyway, it would be good for rocket driver to double-check and ack.

Tomeu, Oded: any concerns about this change?

Tomeu acked it in case you missed it:

https://lore.kernel.org/dri-devel/20250822134348.6819-1-pierre-eric.pelloux- [email protected]/T/#m4efa0d69211b2bb378cfb302e459faa30e57f58c

Indeed I missed it, sorry!

I'll update patch 1/2 based on Philipp's comment and send v2 then.

Thanks.

#10 lore raw
In reply to Pierre-Eric Pelloux-Prayer
Quoted 38 lines · Pierre-Eric Pelloux-Prayer

For hw engines that can't load balance jobs, entities are "statically" load balanced: on their first submit, they select the best scheduler based on its score.
The score is made up of 2 parts:
* the job queue depth (how much jobs are executing/waiting)
* the number of entities assigned

The second part is only relevant for the static load balance:
it's a way to consider how many entities are attached to this scheduler, knowing that if they ever submit jobs they will go to this one.

For rings that can load balance jobs freely, idle entities aren't a concern and shouldn't impact the scheduler's decisions.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 23 ++++++++++++++++++-----
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
  2 files changed, 19 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -0
index f5d5c45ddc0d..4a078d2d98c5 100644
@@ -206,9 +206,11 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
  {
  	struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
  	struct amdgpu_device *adev = ctx->mgr->adev;
+	bool static_load_balancing = false;
  	struct amdgpu_ctx_entity *entity;
  	enum drm_sched_priority drm_prio;
  	unsigned int hw_prio, num_scheds;
+	struct amdgpu_ring *aring;
  	int32_t ctx_prio;
  	int r;
 

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 241

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
@@ -236,17 +238,22 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
  		r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
  						&num_scheds, &scheds);
  		if (r)
-			goto cleanup_entity;
+			goto error_free_entity;

Indeed, previously this was calling drm_sched_entity_fini() before drm_sched_entity_init() and it only worked because of kzalloc.

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -4
  	}
 
  	/* disable load balance if the hw engine retains context among dependent jobs */
-	if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
-	    hw_ip == AMDGPU_HW_IP_VCN_DEC ||
-	    hw_ip == AMDGPU_HW_IP_UVD_ENC ||
-	    hw_ip == AMDGPU_HW_IP_UVD) {
+	static_load_balancing = hw_ip == AMDGPU_HW_IP_VCN_ENC ||
+				hw_ip == AMDGPU_HW_IP_VCN_DEC ||

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 256

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +7 -0
+				hw_ip == AMDGPU_HW_IP_UVD_ENC ||
+				hw_ip == AMDGPU_HW_IP_UVD;
+
+	if (static_load_balancing) {
  		sched = drm_sched_pick_best(scheds, num_scheds);
  		scheds = &sched;
  		num_scheds = 1;
+		aring = container_of(sched, struct amdgpu_ring, sched);
+		entity->sched_ring_score = aring->sched_score;
+		atomic_inc(entity->sched_ring_score);

If we were to bike-shed we could find a way to avoid the new local variables. Keeping the if as is and assign to entity->sched_ring_score directly, and then checking for that on the cleanup path. Still works due kzalloc. Or if relying on kzalloc is not desired, at least bool static_load_balance could be replaced by re-naming the aring local as static_aring and using it like the name suggests.

Could also move the atomic_inc to the success path to avoid having to add code to error unwind.

Both cases are I think equally racy in the sense that parallel amdgpu_ctx_init_entity invocations can all pick the same sched. But that is true today AFAICT because score is not incremented until later in the job submit process.

I suppose one way to make the assignment more robust would be to "rotate" (or randomize) the sched list atomically before calling drm_sched_pick_best. Thoughts?

Regards,

Tvrtko

Quoted 35 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -0
  	}
 
  	r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
@@ -264,6 +271,9 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
  	drm_sched_entity_fini(&entity->entity);
 
  error_free_entity:
+	if (static_load_balancing)
+		atomic_dec(entity->sched_ring_score);
+
  	kfree(entity);
 
  	return r;
@@ -514,6 +524,9 @@ static void amdgpu_ctx_do_release(struct kref *ref)
  			if (!ctx->entities[i][j])
  				continue;
 
+			if (ctx->entities[i][j]->sched_ring_score)
+				atomic_dec(ctx->entities[i][j]->sched_ring_score);
+
  			drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
  		}
  	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
index 090dfe86f75b..076a0e165ce0 100644
@@ -39,6 +39,7 @@ struct amdgpu_ctx_entity {
  	uint32_t		hw_ip;
  	uint64_t		sequence;
  	struct drm_sched_entity	entity;
+	atomic_t		*sched_ring_score;
  	struct dma_fence	*fences[];
  };
 
#11 lore raw

Hi,

Le 01/09/2025 à 11:02, Tvrtko Ursulin a écrit :

Quoted 68 lines · Pierre-Eric Pelloux-Prayer
Quoted 38 lines

For hw engines that can't load balance jobs, entities are "statically" load balanced: on their first submit, they select the best scheduler based on its score.
The score is made up of 2 parts:
* the job queue depth (how much jobs are executing/waiting)
* the number of entities assigned

The second part is only relevant for the static load balance:
it's a way to consider how many entities are attached to this scheduler, knowing that if they ever submit jobs they will go to this one.

For rings that can load balance jobs freely, idle entities aren't a concern and shouldn't impact the scheduler's decisions.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 23 ++++++++++++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +

  2 files changed, 19 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -0
index f5d5c45ddc0d..4a078d2d98c5 100644
@@ -206,9 +206,11 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, 
 u32 hw_ip,
   {
       struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
       struct amdgpu_device *adev = ctx->mgr->adev;
+    bool static_load_balancing = false;
       struct amdgpu_ctx_entity *entity;
       enum drm_sched_priority drm_prio;
       unsigned int hw_prio, num_scheds;
+    struct amdgpu_ring *aring;
       int32_t ctx_prio;
       int r;

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx · line 242

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
@@ -236,17 +238,22 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx 
 *ctx, u32 hw_ip,
           r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
                           &num_scheds, &scheds);
           if (r)
-            goto cleanup_entity;
+            goto error_free_entity;

Indeed, previously this was calling drm_sched_entity_fini() before drm_sched_entity_init() and it only worked because of kzalloc.

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -4
       }
       /* disable load balance if the hw engine retains context among dependent jobs */
-    if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
-        hw_ip == AMDGPU_HW_IP_VCN_DEC ||
-        hw_ip == AMDGPU_HW_IP_UVD_ENC ||
-        hw_ip == AMDGPU_HW_IP_UVD) {
+    static_load_balancing = hw_ip == AMDGPU_HW_IP_VCN_ENC ||
+                hw_ip == AMDGPU_HW_IP_VCN_DEC ||

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 256

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +7 -0
+                hw_ip == AMDGPU_HW_IP_UVD_ENC ||
+                hw_ip == AMDGPU_HW_IP_UVD;
+
+    if (static_load_balancing) {
           sched = drm_sched_pick_best(scheds, num_scheds);
           scheds = &sched;
           num_scheds = 1;
+        aring = container_of(sched, struct amdgpu_ring, sched);
+        entity->sched_ring_score = aring->sched_score;
+        atomic_inc(entity->sched_ring_score);
Quoted 4 lines

If we were to bike-shed we could find a way to avoid the new local variables. Keeping the if as is and assign to entity->sched_ring_score directly, and then checking for that on the cleanup path. Still works due kzalloc. Or if relying on kzalloc is not desired, at least bool static_load_balance could be replaced by re-naming the aring local as static_aring and using it like the name suggests.

Could also move the atomic_inc to the success path to avoid having to add code to error unwind.

Christian, Alex: any preferences on the code style to use here?

Quoted 4 lines

Both cases are I think equally racy in the sense that parallel amdgpu_ctx_init_entity invocations can all pick the same sched. But that is true today AFAICT because score is not incremented until later in the job submit process.

I suppose one way to make the assignment more robust would be to "rotate" (or randomize) the sched list atomically before calling drm_sched_pick_best. Thoughts?

IMO without the ability to do job level load balancing, there will always be cases where the scheduler choice is not optimal. For this reason I prefered to keep the code as simple as possible.

Thanks,
Pierre-Eric

Quoted 35 lines

Regards,

Tvrtko

Quoted 31 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
       }
       r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
@@ -264,6 +271,9 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, 
 u32 hw_ip,
       drm_sched_entity_fini(&entity->entity);

  error_free_entity:
+    if (static_load_balancing)
+        atomic_dec(entity->sched_ring_score);
+
      kfree(entity);
      return r;

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -0
@@ -514,6 +524,9 @@ static void amdgpu_ctx_do_release(struct kref *ref)
               if (!ctx->entities[i][j])
                   continue;
+            if (ctx->entities[i][j]->sched_ring_score)
+                atomic_dec(ctx->entities[i][j]->sched_ring_score);
+
               drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
           }
       }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
index 090dfe86f75b..076a0e165ce0 100644
@@ -39,6 +39,7 @@ struct amdgpu_ctx_entity {
       uint32_t        hw_ip;
       uint64_t        sequence;
       struct drm_sched_entity    entity;
+    atomic_t        *sched_ring_score;
       struct dma_fence    *fences[];
   };
maintainer
#12 lore raw
In reply to Pierre-Eric Pelloux-Prayer
Quoted 44 lines · Pierre-Eric Pelloux-Prayer

For hw engines that can't load balance jobs, entities are "statically" load balanced: on their first submit, they select the best scheduler based on its score.
The score is made up of 2 parts:
* the job queue depth (how much jobs are executing/waiting)
* the number of entities assigned

The second part is only relevant for the static load balance:
it's a way to consider how many entities are attached to this scheduler, knowing that if they ever submit jobs they will go to this one.

For rings that can load balance jobs freely, idle entities aren't a concern and shouldn't impact the scheduler's decisions.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 23 ++++++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
 2 files changed, 19 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -1
index f5d5c45ddc0d..4a078d2d98c5 100644
@@ -206,9 +206,11 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
 {
 	struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
 	struct amdgpu_device *adev = ctx->mgr->adev;
+	bool static_load_balancing = false;
 	struct amdgpu_ctx_entity *entity;
 	enum drm_sched_priority drm_prio;
 	unsigned int hw_prio, num_scheds;
+	struct amdgpu_ring *aring;
 	int32_t ctx_prio;
 	int r;
 
@@ -236,17 +238,22 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
 		r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
 						&num_scheds, &scheds);
 		if (r)
-			goto cleanup_entity;
+			goto error_free_entity;
 	}

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 248

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +4 -4
 
 	/* disable load balance if the hw engine retains context among dependent jobs */
-	if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
-	    hw_ip == AMDGPU_HW_IP_VCN_DEC ||
-	    hw_ip == AMDGPU_HW_IP_UVD_ENC ||
-	    hw_ip == AMDGPU_HW_IP_UVD) {
+	static_load_balancing = hw_ip == AMDGPU_HW_IP_VCN_ENC ||
+				hw_ip == AMDGPU_HW_IP_VCN_DEC ||
+				hw_ip == AMDGPU_HW_IP_UVD_ENC ||
+				hw_ip == AMDGPU_HW_IP_UVD;

Please make that a property of the, e.g. a bool in struct amdgpu_ring_funcs.

Making it depend on the HW IP type was a bad idea in the first place since this all depends on the HW/FW version and not the general IP type.

Apart from that looks good to me.

Regards,
Christian.

Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +11 -0
+
+	if (static_load_balancing) {
 		sched = drm_sched_pick_best(scheds, num_scheds);
 		scheds = &sched;
 		num_scheds = 1;
+		aring = container_of(sched, struct amdgpu_ring, sched);
+		entity->sched_ring_score = aring->sched_score;
+		atomic_inc(entity->sched_ring_score);
 	}
 
 	r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
@@ -264,6 +271,9 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
 	drm_sched_entity_fini(&entity->entity);
 
 error_free_entity:
+	if (static_load_balancing)
+		atomic_dec(entity->sched_ring_score);
+
 	kfree(entity);
 
 	return r;
@@ -514,6 +524,9 @@ static void amdgpu_ctx_do_release(struct kref *ref)
 			if (!ctx->entities[i][j])
 				continue;
 
+			if (ctx->entities[i][j]->sched_ring_score)
+				atomic_dec(ctx->entities[i][j]->sched_ring_score);
+
 			drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
 		}
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
index 090dfe86f75b..076a0e165ce0 100644
@@ -39,6 +39,7 @@ struct amdgpu_ctx_entity {
 	uint32_t		hw_ip;
 	uint64_t		sequence;
 	struct drm_sched_entity	entity;
+	atomic_t		*sched_ring_score;
 	struct dma_fence	*fences[];
 };