drm/amdgpu: add engine_retains_context to amdgpu_ring_funcs
Subsystems (4)
PATCH v3 1/4 drm/amdgpu: add engine_retains_context to amdgpu_ring_funcs
If true, the hw engine retains context among dependent jobs, which means load balancing between schedulers cannot be used at the job level.
amdgpu_ctx_init_entity uses this information to disable load balancing, but it's best to store it as a property rather than deduce it based on hw_ip.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h | 1 + drivers/gpu/drm/amd/amdgpu/uvd_v3_1.c | 1 + drivers/gpu/drm/amd/amdgpu/uvd_v4_2.c | 1 + drivers/gpu/drm/amd/amdgpu/uvd_v5_0.c | 1 + drivers/gpu/drm/amd/amdgpu/uvd_v6_0.c | 3 +++ drivers/gpu/drm/amd/amdgpu/uvd_v7_0.c | 2 ++ drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c | 2 ++ drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c | 2 ++ drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 2 ++ drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 3 +++ drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c | 1 + drivers/gpu/drm/amd/amdgpu/vcn_v4_0_3.c | 1 + drivers/gpu/drm/amd/amdgpu/vcn_v4_0_5.c | 1 + drivers/gpu/drm/amd/amdgpu/vcn_v5_0_0.c | 1 + drivers/gpu/drm/amd/amdgpu/vcn_v5_0_1.c | 1 + 15 files changed, 23 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -0
@@ -211,6 +211,7 @@ struct amdgpu_ring_funcs {bool support_64bit_ptrs;bool no_user_fence;bool secure_submission_supported; +bool engine_retains_context;/** * @extra_bytes:
drivers/gpu/drm/amd/amdgpu/uvd_v3_1.c +1 -0
@@ -181,6 +181,7 @@ static const struct amdgpu_ring_funcs uvd_v3_1_ring_funcs = { .align_mask = 0xf, .support_64bit_ptrs = false, .no_user_fence = true, + .engine_retains_context = true, .get_rptr = uvd_v3_1_ring_get_rptr, .get_wptr = uvd_v3_1_ring_get_wptr, .set_wptr = uvd_v3_1_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/uvd_v4_2.c +1 -0
@@ -775,6 +775,7 @@ static const struct amdgpu_ring_funcs uvd_v4_2_ring_funcs = { .align_mask = 0xf, .support_64bit_ptrs = false, .no_user_fence = true, + .engine_retains_context = true, .get_rptr = uvd_v4_2_ring_get_rptr, .get_wptr = uvd_v4_2_ring_get_wptr, .set_wptr = uvd_v4_2_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/uvd_v5_0.c +1 -0
@@ -882,6 +882,7 @@ static const struct amdgpu_ring_funcs uvd_v5_0_ring_funcs = { .align_mask = 0xf, .support_64bit_ptrs = false, .no_user_fence = true, + .engine_retains_context = true, .get_rptr = uvd_v5_0_ring_get_rptr, .get_wptr = uvd_v5_0_ring_get_wptr, .set_wptr = uvd_v5_0_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/uvd_v6_0.c +3 -0
@@ -1552,6 +1552,7 @@ static const struct amdgpu_ring_funcs uvd_v6_0_ring_phys_funcs = { .align_mask = 0xf, .support_64bit_ptrs = false, .no_user_fence = true, + .engine_retains_context = true, .get_rptr = uvd_v6_0_ring_get_rptr, .get_wptr = uvd_v6_0_ring_get_wptr, .set_wptr = uvd_v6_0_ring_set_wptr,
@@ -1578,6 +1579,7 @@ static const struct amdgpu_ring_funcs uvd_v6_0_ring_vm_funcs = { .align_mask = 0xf, .support_64bit_ptrs = false, .no_user_fence = true, + .engine_retains_context = true, .get_rptr = uvd_v6_0_ring_get_rptr, .get_wptr = uvd_v6_0_ring_get_wptr, .set_wptr = uvd_v6_0_ring_set_wptr,
@@ -1607,6 +1609,7 @@ static const struct amdgpu_ring_funcs uvd_v6_0_enc_ring_vm_funcs = { .nop = HEVC_ENC_CMD_NO_OP, .support_64bit_ptrs = false, .no_user_fence = true, + .engine_retains_context = true, .get_rptr = uvd_v6_0_enc_ring_get_rptr, .get_wptr = uvd_v6_0_enc_ring_get_wptr, .set_wptr = uvd_v6_0_enc_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/uvd_v7_0.c +2 -0
@@ -1539,6 +1539,7 @@ static const struct amdgpu_ring_funcs uvd_v7_0_ring_vm_funcs = { .align_mask = 0xf, .support_64bit_ptrs = false, .no_user_fence = true, + .engine_retains_context = true, .get_rptr = uvd_v7_0_ring_get_rptr, .get_wptr = uvd_v7_0_ring_get_wptr, .set_wptr = uvd_v7_0_ring_set_wptr,
@@ -1571,6 +1572,7 @@ static const struct amdgpu_ring_funcs uvd_v7_0_enc_ring_vm_funcs = { .nop = HEVC_ENC_CMD_NO_OP, .support_64bit_ptrs = false, .no_user_fence = true, + .engine_retains_context = true, .get_rptr = uvd_v7_0_enc_ring_get_rptr, .get_wptr = uvd_v7_0_enc_ring_get_wptr, .set_wptr = uvd_v7_0_enc_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/vcn_v1_0.c +2 -0
@@ -2117,6 +2117,7 @@ static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = { .support_64bit_ptrs = false, .no_user_fence = true, .secure_submission_supported = true, + .engine_retains_context = true, .get_rptr = vcn_v1_0_dec_ring_get_rptr, .get_wptr = vcn_v1_0_dec_ring_get_wptr, .set_wptr = vcn_v1_0_dec_ring_set_wptr,
@@ -2150,6 +2151,7 @@ static const struct amdgpu_ring_funcs vcn_v1_0_enc_ring_vm_funcs = { .align_mask = 0x3f, .nop = VCN_ENC_CMD_NO_OP, .support_64bit_ptrs = false, + .engine_retains_context = true, .no_user_fence = true, .get_rptr = vcn_v1_0_enc_ring_get_rptr, .get_wptr = vcn_v1_0_enc_ring_get_wptr,
drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c +2 -0
@@ -2113,6 +2113,7 @@ static const struct amdgpu_ring_funcs vcn_v2_0_dec_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_DEC, .align_mask = 0xf, .secure_submission_supported = true, + .engine_retains_context = true, .get_rptr = vcn_v2_0_dec_ring_get_rptr, .get_wptr = vcn_v2_0_dec_ring_get_wptr, .set_wptr = vcn_v2_0_dec_ring_set_wptr,
@@ -2144,6 +2145,7 @@ static const struct amdgpu_ring_funcs vcn_v2_0_enc_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_ENC, .align_mask = 0x3f, .nop = VCN_ENC_CMD_NO_OP, + .engine_retains_context = true, .get_rptr = vcn_v2_0_enc_ring_get_rptr, .get_wptr = vcn_v2_0_enc_ring_get_wptr, .set_wptr = vcn_v2_0_enc_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +2 -0
@@ -1777,6 +1777,7 @@ static const struct amdgpu_ring_funcs vcn_v2_5_dec_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_DEC, .align_mask = 0xf, .secure_submission_supported = true, + .engine_retains_context = true, .get_rptr = vcn_v2_5_dec_ring_get_rptr, .get_wptr = vcn_v2_5_dec_ring_get_wptr, .set_wptr = vcn_v2_5_dec_ring_set_wptr,
@@ -1877,6 +1878,7 @@ static const struct amdgpu_ring_funcs vcn_v2_5_enc_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_ENC, .align_mask = 0x3f, .nop = VCN_ENC_CMD_NO_OP, + .engine_retains_context = true, .get_rptr = vcn_v2_5_enc_ring_get_rptr, .get_wptr = vcn_v2_5_enc_ring_get_wptr, .set_wptr = vcn_v2_5_enc_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c +3 -0
@@ -1857,6 +1857,7 @@ static const struct amdgpu_ring_funcs vcn_v3_0_dec_sw_ring_vm_funcs = { .align_mask = 0x3f, .nop = VCN_DEC_SW_CMD_NO_OP, .secure_submission_supported = true, + .engine_retains_context = true, .get_rptr = vcn_v3_0_dec_ring_get_rptr, .get_wptr = vcn_v3_0_dec_ring_get_wptr, .set_wptr = vcn_v3_0_dec_ring_set_wptr,
@@ -2021,6 +2022,7 @@ static const struct amdgpu_ring_funcs vcn_v3_0_dec_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_DEC, .align_mask = 0xf, .secure_submission_supported = true, + .engine_retains_context = true, .get_rptr = vcn_v3_0_dec_ring_get_rptr, .get_wptr = vcn_v3_0_dec_ring_get_wptr, .set_wptr = vcn_v3_0_dec_ring_set_wptr,
@@ -2122,6 +2124,7 @@ static const struct amdgpu_ring_funcs vcn_v3_0_enc_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_ENC, .align_mask = 0x3f, .nop = VCN_ENC_CMD_NO_OP, + .engine_retains_context = true, .get_rptr = vcn_v3_0_enc_ring_get_rptr, .get_wptr = vcn_v3_0_enc_ring_get_wptr, .set_wptr = vcn_v3_0_enc_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c +1 -0
@@ -1977,6 +1977,7 @@ static struct amdgpu_ring_funcs vcn_v4_0_unified_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_ENC, .align_mask = 0x3f, .nop = VCN_ENC_CMD_NO_OP, + .engine_retains_context = true, .extra_bytes = sizeof(struct amdgpu_vcn_rb_metadata), .get_rptr = vcn_v4_0_unified_ring_get_rptr, .get_wptr = vcn_v4_0_unified_ring_get_wptr,
drivers/gpu/drm/amd/amdgpu/vcn_v4_0_3.c +1 -0
@@ -1628,6 +1628,7 @@ static const struct amdgpu_ring_funcs vcn_v4_0_3_unified_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_ENC, .align_mask = 0x3f, .nop = VCN_ENC_CMD_NO_OP, + .engine_retains_context = true, .get_rptr = vcn_v4_0_3_unified_ring_get_rptr, .get_wptr = vcn_v4_0_3_unified_ring_get_wptr, .set_wptr = vcn_v4_0_3_unified_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/vcn_v4_0_5.c +1 -0
@@ -1481,6 +1481,7 @@ static struct amdgpu_ring_funcs vcn_v4_0_5_unified_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_ENC, .align_mask = 0x3f, .nop = VCN_ENC_CMD_NO_OP, + .engine_retains_context = true, .get_rptr = vcn_v4_0_5_unified_ring_get_rptr, .get_wptr = vcn_v4_0_5_unified_ring_get_wptr, .set_wptr = vcn_v4_0_5_unified_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/vcn_v5_0_0.c +1 -0
@@ -1203,6 +1203,7 @@ static const struct amdgpu_ring_funcs vcn_v5_0_0_unified_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_ENC, .align_mask = 0x3f, .nop = VCN_ENC_CMD_NO_OP, + .engine_retains_context = true, .get_rptr = vcn_v5_0_0_unified_ring_get_rptr, .get_wptr = vcn_v5_0_0_unified_ring_get_wptr, .set_wptr = vcn_v5_0_0_unified_ring_set_wptr,
drivers/gpu/drm/amd/amdgpu/vcn_v5_0_1.c +1 -0
@@ -1328,6 +1328,7 @@ static const struct amdgpu_ring_funcs vcn_v5_0_1_unified_ring_vm_funcs = { .type = AMDGPU_RING_TYPE_VCN_ENC, .align_mask = 0x3f, .nop = VCN_ENC_CMD_NO_OP, + .engine_retains_context = true, .get_rptr = vcn_v5_0_1_unified_ring_get_rptr, .get_wptr = vcn_v5_0_1_unified_ring_get_wptr, .set_wptr = vcn_v5_0_1_unified_ring_set_wptr,
Signature · 1 line
2.43.0
PATCH v3 2/4 drm/amdgpu: jump to the correct label on failure
drm_sched_entity_init wasn't called yet, so the only thing to do is to release allocated memory.
This doesn't fix any bug since entity is zero allocated and drm_sched_entity_fini does nothing in this case.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
@@ -236,7 +236,7 @@ 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 */
Signature · 1 line
2.43.0
Re: [PATCH v3 2/4] drm/amdgpu: jump to the correct label on failure
Quoted 15 lines · Pierre-Eric Pelloux-Prayer
drm_sched_entity_initwasn't called yet, so the only thing to do is to release allocated memory.
This doesn't fix any bug since entity is zero allocated anddrm_sched_entity_finidoes nothing in this case.Signed-off-by Pierre-Eric Pelloux-Prayer <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 242
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1
-1@@ -236,7 +236,7 @@ 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 */
Regards,
Tvrtko
Re: [PATCH v3 2/4] drm/amdgpu: jump to the correct label on failure
Quoted 12 lines · Tvrtko Ursulin
Quoted 10 lines · Pierre-Eric Pelloux-Prayer
drm_sched_entity_initwasn't called yet, so the only thing to do is to release allocated memory.
This doesn't fix any bug since entity is zero allocated anddrm_sched_entity_finidoes nothing in this case.Signed-off-by Pierre-Eric Pelloux-Prayer <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 241
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1
-1@@ -236,7 +236,7 @@ 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 */Reviewed-by Tvrtko Ursulin <[email protected]>
Since this is still a fix please push it to amd-staging-drm-next independent of the remaining patch set.
Thanks,
Christian.
Quoted 5 lines
Regards,
Tvrtko
Re: [PATCH v3 2/4] drm/amdgpu: jump to the correct label on failure
Le 06/11/2025 à 11:21, Christian König a écrit :
Quoted 28 lines · Tvrtko Ursulin
Quoted 12 lines · Pierre-Eric Pelloux-Prayer
Quoted 10 lines
drm_sched_entity_initwasn't called yet, so the only thing to do is to release allocated memory.
This doesn't fix any bug since entity is zero allocated anddrm_sched_entity_finidoes nothing in this case.Signed-off-by Pierre-Eric Pelloux-Prayer <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 241
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1
-1@@ -236,7 +236,7 @@ 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 */Reviewed-by Tvrtko Ursulin <[email protected]>
Quoted 3 lines
Acked-by Christian König <[email protected]>Since this is still a fix please push it to amd-staging-drm-next independent of the remaining patch set.
Ok, I removed this patch from v4 and will push it to amd-staging-drm-next.
Pierre-Eric
PATCH v3 3/4 drm/amdgpu: increment sched score on entity selection
In reply to Pierre-Eric Pelloux-PrayerFor 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 | 21 ++++++++++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 1 + 2 files changed, 17 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +16 -5
@@ -209,6 +209,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,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;
@@ -239,11 +240,13 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, 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 ) { + sched = scheds[0 ]; + aring = container_of(sched, struct amdgpu_ring, sched); + +if (aring->funcs->engine_retains_context) { +/* Disable load balancing between multiple schedulers if the hw +* engine retains context among dependent jobs. +*/ sched =drm_sched_pick_best (scheds, num_scheds); scheds = &sched; num_scheds =1 ;
@@ -258,6 +261,11 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,if (cmpxchg (&ctx->entities[hw_ip][ring],NULL , entity)) goto cleanup_entity; +if (aring->funcs->engine_retains_context) { + entity->sched_score = sched->score; +atomic_inc (entity->sched_score); + } +return 0 ; cleanup_entity:
@@ -514,6 +522,9 @@ static void amdgpu_ctx_do_release(struct kref *ref)if (!ctx->entities[i][j])continue ; +if (ctx->entities[i][j]->sched_score) +atomic_dec (ctx->entities[i][j]->sched_score); +drm_sched_entity_destroy (&ctx->entities[i][j]->entity); } }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
@@ -39,6 +39,7 @@ struct amdgpu_ctx_entity {uint32_t hw_ip;uint64_t sequence;struct drm_sched_entity entity; +atomic_t *sched_score;struct dma_fence *fences[]; };
Signature · 1 line
2.43.0
Re: [PATCH v3 3/4] drm/amdgpu: increment sched score on entity selection
Quoted 53 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 assignedThe 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.
Signed-off-by Pierre-Eric Pelloux-Prayer <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 21 ++++++++++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 1 + 2 files changed, 17 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +8
-5@@ -209,6 +209,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,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;@@ -239,11 +240,13 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, 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 ) { + sched = scheds[0 ]; + aring = container_of(sched, struct amdgpu_ring, sched); + +if (aring->funcs->engine_retains_context) { +/* Disable load balancing between multiple schedulers if the hw +* engine retains context among dependent jobs. +*/ sched =drm_sched_pick_best (scheds, num_scheds); scheds = &sched; num_scheds =1 ;
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 266
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3
-0@@ -258,6 +261,11 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,if (cmpxchg (&ctx->entities[hw_ip][ring],NULL , entity)) goto cleanup_entity; +if (aring->funcs->engine_retains_context) { + entity->sched_score = sched->score; +atomic_inc (entity->sched_score);
Maybe you missed it, in the last round I asked this:
"""
Here is would always be sched->score == aring->sched_score, right?
If so it would probably be good to either add that assert, or even to just fetch it from there. Otherwise it can look potentially concerning to be fishing out the pointer from scheduler internals.
The rest looks good to me.
"""
Because grabbing a pointer from drm_sched->score and storing it in AMD entity can look scary, since sched->score can be scheduler owned.
Hence I was suggesting to either fish it out from aring->sched_score. If it is true that they are always the same atomic_t at this point.
Regards,
Tvrtko
Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +5
-0+ } +return 0 ; cleanup_entity:@@ -514,6 +522,9 @@ static void amdgpu_ctx_do_release(struct kref *ref)if (!ctx->entities[i][j])continue ; +if (ctx->entities[i][j]->sched_score) +atomic_dec (ctx->entities[i][j]->sched_score); +drm_sched_entity_destroy (&ctx->entities[i][j]->entity); } }drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1
-0@@ -39,6 +39,7 @@ struct amdgpu_ctx_entity {uint32_t hw_ip;uint64_t sequence;struct drm_sched_entity entity; +atomic_t *sched_score;struct dma_fence *fences[]; };
Re: [PATCH v3 3/4] drm/amdgpu: increment sched score on entity selection
Le 06/11/2025 à 11:00, Tvrtko Ursulin a écrit :
Quoted 56 lines · Pierre-Eric Pelloux-Prayer
Quoted 54 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 assignedThe 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.
Signed-off-by Pierre-Eric Pelloux-Prayer <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 21 ++++++++++++++++-----drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h| 1 +2 files changed, 17 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1
-1@@ -209,6 +209,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, 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;@@ -239,11 +240,13 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, goto error_free_entity; } - /* disable load balance if the hw engine retains context among dependentjobs */
- 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) {
+ sched = scheds[0];
+ aring =container_of(sched, struct amdgpu_ring, sched); +
+ if (aring->funcs->engine_retains_context) {
+ /* Disable load balancing between multiple schedulers if the hw + * engine retains context among dependent jobs.
+ */
sched =drm_sched_pick_best(scheds, num_scheds); scheds = &sched;
num_scheds= 1;drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0
-0@@ -258,6 +261,11 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx,
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 266
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3
-0u32 hw_ip, if (cmpxchg(&ctx->entities[hw_ip][ring], NULL, entity)) goto cleanup_entity; + if (aring->funcs->engine_retains_context) { + entity->sched_score = sched->score; + atomic_inc(entity->sched_score);Maybe you missed it, in the last round I asked this:
I missed it, sorry.
Quoted 3 lines
"""
Here is would always besched->score==aring->sched_score, right?
Yes because drm_sched_init is called with args.score = ring->sched_score
Quoted 9 lines
If so it would probably be good to either add that assert, or even to just fetch it from there. Otherwise it can look potentially concerning to be fishing out the pointer from scheduler internals.
The rest looks good to me.
"""Because grabbing a pointer from
drm_sched->scoreand storing it in AMD entity can look scary, sincesched->scorecan be scheduler owned.Hence I was suggesting to either fish it out from
aring->sched_score. If it is true that they are always the sameatomic_tat this point.
I used sched->score, because aring->sched_score is not the one we want (the existing aring points to scheds[0], not the selected sched). But I can change the code to:
if (aring->funcs->engine_retains_context) {
aring = container_of(sched, struct amdgpu_ring, sched) entity->sched_score = aring->sched_score; atomic_inc(entity->sched_score);
}
If it's preferred.
Pierre-Eric
Quoted 29 lines
Regards,
Tvrtko
Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2
-0+ } + return 0;
cleanup_entity:drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3
-0@@ -514,6 +522,9 @@ static void amdgpu_ctx_do_release(struct kref *ref) if (!ctx->entities[i][j]) continue; + if (ctx->entities[i][j]->sched_score) + atomic_dec(ctx->entities[i][j]->sched_score); + drm_sched_entity_destroy(&ctx->entities[i][j]->entity); } }drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1
-0@@ -39,6 +39,7 @@ struct amdgpu_ctx_entity { uint32_t hw_ip; uint64_t sequence; struct drm_sched_entity entity; + atomic_t *sched_score; struct dma_fence *fences[]; };
Tvrtko Ursulin
Reviewed-byRe: [PATCH v3 3/4] drm/amdgpu: increment sched score on entity selection
Quoted 88 lines · Pierre-Eric Pelloux-Prayer
Le 06/11/2025 à 11:00, Tvrtko Ursulin a écrit :
Quoted 56 lines · Pierre-Eric Pelloux-Prayer
Quoted 54 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 assignedThe 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.
Signed-off-by Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux- [email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 21 ++++++++++++++++-----drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h| 1 +2 files changed, 17 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1
-1@@ -209,6 +209,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, 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;@@ -239,11 +240,13 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, goto error_free_entity; } - /* disable load balance if the hw engine retains context amongdependent 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) {
+ sched = scheds[0];
+ aring =container_of(sched, struct amdgpu_ring, sched); +
+ if (aring->funcs->engine_retains_context) {
+ /* Disable load balancing between multiple schedulers if the hw + * engine retains context among dependent jobs.
+ */
sched =drm_sched_pick_best(scheds, num_scheds); scheds = &sched;
num_scheds= 1;drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0
-0@@ -258,6 +261,11 @@ static int amdgpu_ctx_init_entity(structQuoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 266
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3
-0amdgpu_ctx *ctx, u32 hw_ip, if (cmpxchg(&ctx->entities[hw_ip][ring], NULL, entity)) goto cleanup_entity; + if (aring->funcs->engine_retains_context) { + entity->sched_score = sched->score; + atomic_inc(entity->sched_score);Maybe you missed it, in the last round I asked this:
I missed it, sorry.
Quoted 3 lines
"""
Here is would always besched->score==aring->sched_score, right?Yes because
drm_sched_initis called with args.score =ring->sched_scoreQuoted 9 lines
If so it would probably be good to either add that assert, or even to just fetch it from there. Otherwise it can look potentially concerning to be fishing out the pointer from scheduler internals.
The rest looks good to me.
"""Because grabbing a pointer from
drm_sched->scoreand storing it in AMD entity can look scary, sincesched->scorecan be scheduler owned.Hence I was suggesting to either fish it out from
aring->sched_score. If it is true that they are always the sameatomic_tat this point.I used
sched->score, becausearing->sched_scoreis not the one we want (the existing aring points to scheds[0], not the selected sched). But I can change the code to:
Quoted 7 lines
if (
aring->funcs->engine_retains_context) {
aring =container_of(sched, struct amdgpu_ring, sched)entity->sched_score=aring->sched_score;atomic_inc(entity->sched_score);
}If it's preferred.
For me it is, yes. Because it removes any doubt on who owns the atomic_t pointed to. And it also isolates the driver from any changes in DRM scheduler structures.
With that:
Regards,
Tvrtko
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2
-0+ } + return 0;
cleanup_entity:drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3
-0@@ -514,6 +522,9 @@ static void amdgpu_ctx_do_release(struct kref *ref) if (!ctx->entities[i][j]) continue; + if (ctx->entities[i][j]->sched_score) + atomic_dec(ctx->entities[i][j]->sched_score); + drm_sched_entity_destroy(&ctx->entities[i][j]->entity); } }drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1
-0@@ -39,6 +39,7 @@ struct amdgpu_ctx_entity { uint32_t hw_ip; uint64_t sequence; struct drm_sched_entity entity; + atomic_t *sched_score; struct dma_fence *fences[]; };
PATCH v3 4/4 drm/sched: limit sched score update to jobs change
In reply to Pierre-Eric Pelloux-PrayerCurrently, 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
@@ -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
Re: [PATCH v3 4/4] drm/sched: limit sched score update to jobs change
nit: s/limit/Limit
Quoted 10 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
s/Now,/Now, let's imagine
Quoted 1 line
the worst case, these apps' entities where all attached to the same
s/where/were
Quoted 5 lines
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.
Quoted 7 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.
Signed-off-by Pierre-Eric Pelloux-Prayer <[email protected]>Reviewed-by Tvrtko Ursulin <[email protected]>Acked-by Tomeu Vizoso <[email protected]>
With the commit message fixed up a little bit:
Apply how you want :)
P.
Quoted 24 lines
---
drivers/gpu/drm/scheduler/sched_main.c | 2 --1 file changed, 2 deletions(-)drivers/gpu/drm/scheduler/sched_main.c +0
-0@@ -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)