drm/sched: Entity handling cleanups
Subsystems (8)
v2 cover drm/sched: Entity handling cleanups
Stricter rules around entity initialization, removing the single scheduler special case which fixes the priority override for amdgpu single IP instance entities and addressing of fence_seq FIXME by pulling the entity->lock up from drm_sched_entity_select_rq().
In the last round sashiko complained about the first patch breaking legacy v3d but Maíra has confirmed that is an unused path so no problem to make it stricter. Lets see what sashiko finds in this round.
v2:
* New patches in the series:
* Fix amdgpu VCN potential usage of un-initialized schedulers.
* Make entaviv and msm check drm_sched_entity_init return value.
* Mark drm_sched_entity_init as __must_check.
* Clamp priority levels against all schedulers in drm_sched_entity_init().
Tvrtko Ursulin (10):
drm/amdgpu/vcn3: Fail job submission if VCN0 decode is disabled under
SR-IOV
drm/etnaviv: Release context id on failure
drm/etnaviv: Check drm_sched_entity_init() return value drm/msm: Check drm_sched_entity_init() return value drm/sched: Mark drm_sched_entity_init() as __must_check drm/sched: Disallow initializing entities with un-initialized
schedulers
drm/sched: Clamp priority againsts all provided schedulers drm/sched: Ignore stopped entity attempt to change its run-queue drm/sched: Remove the single scheduler special case drm/sched: Address the fence_seq fixme
drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 4 ++ drivers/gpu/drm/etnaviv/etnaviv_drv.c | 26 ++++--- drivers/gpu/drm/msm/msm_submitqueue.c | 9 ++- drivers/gpu/drm/scheduler/sched_entity.c | 89 +++++++++++++++--------- drivers/gpu/drm/scheduler/sched_fence.c | 6 +- drivers/gpu/drm/scheduler/sched_main.c | 7 +- include/drm/gpu_scheduler.h | 59 +++++++++------- 7 files changed, 129 insertions(+), 71 deletions(-)
Signature · 1 line
2.56.0
PATCH v2 1/10 drm/amdgpu/vcn3: Fail job submission if VCN0 decode is disabled under SR-IOV
So far vcn_v3_0_limit_sched() handles the harvested config and fails AV1 decoding job submissions when ring is not available, but under SR-IOV ring and it's scheduler can also be disabled in vcn_v3_0_hw_init() via the amdgpu_vcn_is_disabled_vcn() check.
In this case sched.ready is set to false, and vcn_v3_0_limit_sched() calls drm_sched_entity_modify_sched() with an un-ready scheduler, which makes drm_sched_entity_select_rq() null-ify entity->rq and the following job submission would trigger a null pointer dereference.
Fix it by repeating the same amdgpu_vcn_is_disabled_vcn() check inside vcn_v3_0_limit_sched().
---
I am not entirely sure whether this is the right Fixes: target, but it appears to have added sched->ready = false for the decode ring so I *think* it may just as well be first time NULL pointer deref became possible.
Compile tested only.
--- drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c +4 -0
@@ -1902,6 +1902,10 @@ static int vcn_v3_0_limit_sched(struct amdgpu_cs_parser *p,if (p->adev->vcn.harvest_config &AMDGPU_VCN_HARVEST_VCN0 )return -EINVAL ; +/* likewise if VCN0 decode is disabled (e.g. SR-IOV) */ +if (amdgpu_vcn_is_disabled_vcn (p->adev,VCN_DECODE_RING ,0 )) +return -EINVAL ; +/* wait for all jobs to finish before switching to instance 0 */ fence =amdgpu_ctx_get_fence (p->ctx, job->base.entity, ~0ull );if (fence) {
Signature · 1 line
2.56.0
PATCH v2 2/10 drm/etnaviv: Release context id on failure
In reply to Tvrtko UrsulinCleanup on the etnaviv_open() failure path looks incomplete in that the context id is left allocated. Fix it by adding proper onion unwind.
---
Compile tested only.
And as I cannot see that these ids are used anywhere I am not marking this as a fix.
--- drivers/gpu/drm/etnaviv/etnaviv_drv.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +3 -1
@@ -79,7 +79,7 @@ static int etnaviv_open(struct drm_device *dev, struct drm_file *file) priv->cmdbuf_suballoc);if (!ctx->mmu) { ret = -ENOMEM ; - goto out_free; + goto out_id; }for (i =0 ; i <ETNA_MAX_PIPES ; i++) {
@@ -98,6 +98,8 @@ static int etnaviv_open(struct drm_device *dev, struct drm_file *file)return 0 ; +out_id: +xa_erase (&priv->active_contexts, ctx->id); out_free:kfree (ctx);return ret;
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 02/10] drm/etnaviv: Release context id on failure
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/etnaviv: missing Fixes trailer for etnaviv_open() XArray leak fix --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/etnaviv: missing Fixes trailer for etnaviv_open() XArray leak fix
--
commit 5c40c43dfcc5cb8de64c961ddd4b379057974735
Author: Tvrtko Ursulin <[email protected]>
Quoted 10 lines
drm/etnaviv: Release context id on failure
Cleanup on the
etnaviv_open()failure path looks incomplete in that the context id is left allocated. Fix it by adding proper onion unwind.Signed-off-by Tvrtko Ursulin <[email protected]>Cc Lucas Stach <[email protected]>Cc Russell King <[email protected]>Cc Christian Gmeiner <[email protected]>
[Severity: Low]
Even though the context ids are unused, leaving them in priv->active_contexts leaks XArray slots and xa_node slab memory (and a dangling ctx pointer) until etnaviv_unbind().
On IOMMUv2, etnaviv_iommuv2_context_alloc() fails once 512 contexts are open, so repeated failed opens can leak unbounded kernel memory without memory pressure.
Since this fixes a memory leak introduced when xa_alloc_cyclic() was added to etnaviv_open(), should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/10 drm/etnaviv: Check drm_sched_entity_init() return value
In reply to Tvrtko UrsulinStart checking drm_sched_entity_init() return value in preparation of making that API a bit stricter.
No functional change intended as the failure path should never trigger in this driver.
---
Compile tested only.
--- drivers/gpu/drm/etnaviv/etnaviv_drv.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +15 -7
@@ -83,21 +83,29 @@ static int etnaviv_open(struct drm_device *dev, struct drm_file *file) }for (i =0 ; i <ETNA_MAX_PIPES ; i++) { -struct etnaviv_gpu *gpu = priv->gpu[i];struct drm_gpu_scheduler *sched; -if (gpu) { - sched = &gpu->sched; -drm_sched_entity_init (&ctx->sched_entity[i], -DRM_SCHED_PRIORITY_NORMAL , &sched, -1 ,NULL ); - } +if (!priv->gpu[i]) +continue ; + + sched = &priv->gpu[i]->sched; + ret =drm_sched_entity_init (&ctx->sched_entity[i], +DRM_SCHED_PRIORITY_NORMAL , + &sched,1 ,NULL ); +if (ret) + goto out_entity; } file->driver_priv = ctx;return 0 ; +out_entity: +while (i--) { +if (priv->gpu[i]) +drm_sched_entity_destroy (&ctx->sched_entity[i]); + } +etnaviv_iommu_context_put (ctx->mmu); out_id:xa_erase (&priv->active_contexts, ctx->id); out_free:
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 03/10] drm/etnaviv: Check drm_sched_entity_init() return value
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/etnaviv: inaccurate commit message and missing Fixes tag --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/etnaviv: inaccurate commit message and missing Fixes tag --
commit 1bcc1945503d97a4e3b2616be71622735bd4fe37
Author: Tvrtko Ursulin <[email protected]>
Quoted 5 lines
drm/etnaviv: Check
drm_sched_entity_init()return valueStart checking
drm_sched_entity_init()return value in preparation of making that API a bit stricter.
Quoted 8 lines
No functional change intended as the failure path should never trigger in this driver.
Signed-off-by Tvrtko Ursulin <[email protected]>Cc Lucas Stach <[email protected]>Cc Russell King <[email protected]>Cc Christian Gmeiner <[email protected]>
[Severity: Low]
Is the commit message accurate that the failure path can never trigger in this driver?
Since commit fd177135f0e6 ("drm/sched: Account entity GPU time"), drm_sched_entity_init() allocates entity->stats via drm_sched_entity_stats_new() and can return -ENOMEM even with valid arguments.
Without checking the return value, etnaviv_open() succeeds with a NULL entity->rq, causing a NULL pointer dereference in drm_sched_entity_flush() when etnaviv_postclose() calls drm_sched_entity_destroy().
Since this fixes a reachable bug, should the commit message be updated and include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v2 03/10] drm/etnaviv: Check drm_sched_entity_init() return value
In reply to Tvrtko UrsulinQuoted 5 lines · Tvrtko Ursulin
Start checking
drm_sched_entity_init()return value in preparation of making that API a bit stricter.No functional change intended as the failure path should never trigger in this driver.
Sashiko raised a good point, it can still fail on memory allocation failures. Need to rewrite the commit message and add the below tags.
Quoted 5 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Cc Lucas Stach <[email protected]>Cc Russell King <[email protected]>Cc Christian Gmeiner <[email protected]>
Regards,
Tvrtko
Quoted 47 lines
---
Compile tested only.--- drivers/gpu/drm/etnaviv/etnaviv_drv.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)drivers/gpu/drm/etnaviv/etnaviv_drv.c +15
-7@@ -83,21 +83,29 @@ static int etnaviv_open(struct drm_device *dev, struct drm_file *file) }for (i =0 ; i <ETNA_MAX_PIPES ; i++) { -struct etnaviv_gpu *gpu = priv->gpu[i];struct drm_gpu_scheduler *sched; -if (gpu) { - sched = &gpu->sched; -drm_sched_entity_init (&ctx->sched_entity[i], -DRM_SCHED_PRIORITY_NORMAL , &sched, -1 ,NULL ); - } +if (!priv->gpu[i]) +continue ; + + sched = &priv->gpu[i]->sched; + ret =drm_sched_entity_init (&ctx->sched_entity[i], +DRM_SCHED_PRIORITY_NORMAL , + &sched,1 ,NULL ); +if (ret) + goto out_entity; } file->driver_priv = ctx;return 0 ; +out_entity: +while (i--) { +if (priv->gpu[i]) +drm_sched_entity_destroy (&ctx->sched_entity[i]); + } +etnaviv_iommu_context_put (ctx->mmu); out_id:xa_erase (&priv->active_contexts, ctx->id); out_free:
PATCH v2 4/10 drm/msm: Check drm_sched_entity_init() return value
In reply to Tvrtko UrsulinStart checking drm_sched_entity_init() return value in preparation of making that API a bit stricter.
No functional change intended as the failure path should never trigger in this driver given drm_sched_init() happens before and it's failure is always fatal.
---
Compile tested only.
--- drivers/gpu/drm/msm/msm_submitqueue.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/msm/msm_submitqueue.c +7 -2
@@ -227,8 +227,13 @@ int msm_submitqueue_create(struct drm_device *drm, struct msm_context *ctx, queue->entity = &queue->_vm_bind_entity[0 ]; -drm_sched_entity_init (queue->entity,DRM_SCHED_PRIORITY_KERNEL , - &sched,1 ,NULL ); + ret =drm_sched_entity_init (queue->entity, +DRM_SCHED_PRIORITY_KERNEL , + &sched,1 ,NULL ); +if (ret) { +kfree (queue); +return ret; + } }else { queue->ring_nr = ring_nr;
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 04/10] drm/msm: Check drm_sched_entity_init() return value
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/msm: inaccurate commit message and missing Fixes tag --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/msm: inaccurate commit message and missing Fixes tag --
commit 21999ea1c8eea57d5acd6db9fdbfa393532b555c
Author: Tvrtko Ursulin <[email protected]>
Quoted 9 lines
drm/msm: Check
drm_sched_entity_init()return valueStart checking
drm_sched_entity_init()return value in preparation of making that API a bit stricter.No functional change intended as the failure path should never trigger in this driver given
drm_sched_init()happens before and it's failure is always fatal.
Quoted 5 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Cc Rob Clark <[email protected]>Cc Dmitry Baryshkov <[email protected]>
[Severity: Low]
Is the "no functional change intended" claim accurate here?
drm_sched_entity_init() allocates entity->stats via drm_sched_entity_stats_new() and returns -ENOMEM on allocation failure even when drm_sched_init() succeeded. Previously, ignoring that error left queue->entity zeroed while still registering the queue, leading to a NULL pointer dereference in drm_sched_entity_stats_put() on teardown.
Since this fixes a reachable allocation-failure crash, should the commit message be updated and include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v2 04/10] drm/msm: Check drm_sched_entity_init() return value
In reply to Tvrtko UrsulinQuoted 6 lines · Tvrtko Ursulin
Start checking
drm_sched_entity_init()return value in preparation of making that API a bit stricter.No functional change intended as the failure path should never trigger in this driver given
drm_sched_init()happens before and it's failure is always fatal.
Sashiko raised a good point, it can still fail on memory allocation failures. Need to rewrite the commit message and add the below tags
Quoted 5 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Cc Rob Clark <[email protected]>Cc Dmitry Baryshkov <[email protected]>
Regards,
Tvrtko
Quoted 26 lines
---
Compile tested only.--- drivers/gpu/drm/msm/msm_submitqueue.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/gpu/drm/msm/msm_submitqueue.c +7
-2@@ -227,8 +227,13 @@ int msm_submitqueue_create(struct drm_device *drm, struct msm_context *ctx, queue->entity = &queue->_vm_bind_entity[0 ]; -drm_sched_entity_init (queue->entity,DRM_SCHED_PRIORITY_KERNEL , - &sched,1 ,NULL ); + ret =drm_sched_entity_init (queue->entity, +DRM_SCHED_PRIORITY_KERNEL , + &sched,1 ,NULL ); +if (ret) { +kfree (queue); +return ret; + } }else { queue->ring_nr = ring_nr;
PATCH v2 5/10 drm/sched: Mark drm_sched_entity_init() as __must_check
In reply to Tvrtko UrsulinMark drm_sched_entity_init() as __must_check as preparation to make the API a bit stricter in what input combinations it accepts.
--- include/drm/gpu_scheduler.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
include/drm/gpu_scheduler.h +5 -5
@@ -718,11 +718,11 @@ static inline bool drm_sched_invalidate_job(struct drm_sched_job *s_job,/* Entities */ -int drm_sched_entity_init (struct drm_sched_entity *entity, -enum drm_sched_priority priority, -struct drm_gpu_scheduler **sched_list, -unsigned int num_sched_list, -atomic_t *guilty); +int __must_check drm_sched_entity_init (struct drm_sched_entity *entity, +enum drm_sched_priority priority, +struct drm_gpu_scheduler **sched_list, +unsigned int num_sched_list, +atomic_t *guilty);long drm_sched_entity_flush (struct drm_sched_entity *entity,long timeout);void drm_sched_entity_kill (struct drm_sched_entity *entity);void drm_sched_entity_fini (struct drm_sched_entity *entity);
Signature · 1 line
2.56.0
PATCH v2 6/10 drm/sched: Disallow initializing entities with un-initialized schedulers
In reply to Tvrtko UrsulinSimplify the code and theoretical runtime combinations by disallowing initializing entities with un-initialized schedulers.
---
I do not *think* any of the drivers relies on this inverted initialization order but it needs more eyes for sure.
--- drivers/gpu/drm/scheduler/sched_entity.c | 36 ++++++++++++++++-------- 1 file changed, 25 insertions(+), 11 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +25 -11
@@ -90,6 +90,19 @@ ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job)return duration; } +static bool drm_sched_list_is_initialized (struct drm_gpu_scheduler **sched_list, +unsigned int num_sched_list) +{ +unsigned int i; + +for (i =0 ; i < num_sched_list; i++) { +if (!sched_list[i] || !sched_list[i]->sched_rq) +return false; + } + +return true; +} +/** * drm_sched_entity_init - Init a context entity used by scheduler when * submit to HW ring.
@@ -119,7 +132,10 @@ int drm_sched_entity_init(struct drm_sched_entity *entity, unsigned int num_sched_list, atomic_t *guilty) { -if (!entity || !sched_list || !num_sched_list || !sched_list[0 ]) +if (!entity || !sched_list || !num_sched_list) +return -EINVAL ; + +if (!drm_sched_list_is_initialized (sched_list, num_sched_list))return -EINVAL ;memset (entity,0 ,sizeof (struct drm_sched_entity ));
@@ -135,13 +151,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity, entity->sched_list = num_sched_list >1 ? sched_list :NULL ;RB_CLEAR_NODE (&entity->rb_tree_node); -if (!sched_list[0 ]->sched_rq) { -/* Since every entry covered by num_sched_list -* should be non-NULL and therefore we warn drivers -* not to do this and to fix their DRM calling order. -*/ -pr_warn ("%s: called with uninitialized scheduler\n" , __func__); - }else if (priority >= sched_list[0 ]->num_user_rqs) { +if (priority >= sched_list[0 ]->num_user_rqs) {/* * The "priority" of an entity cannot exceed the number of * run-queues of a scheduler. Protect against num_rqs being 0,
@@ -158,8 +168,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity, entity->priority = priority; entity->rq_priority = drm_sched_policy ==DRM_SCHED_POLICY_FAIR ?DRM_SCHED_PRIORITY_KERNEL : priority; -if (sched_list[0 ]->sched_rq) - entity->rq = sched_list[0 ]->sched_rq[entity->rq_priority]; + entity->rq = sched_list[0 ]->sched_rq[entity->rq_priority];init_completion (&entity->entity_idle);
@@ -192,7 +201,12 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity, struct drm_gpu_scheduler **sched_list, unsigned int num_sched_list) { - WARN_ON(!num_sched_list || !sched_list); + if (WARN_ON(!num_sched_list || !sched_list)) + return; + +if (WARN_ON_ONCE (!drm_sched_list_is_initialized (sched_list, + num_sched_list))) +return ;spin_lock (&entity->lock); entity->sched_list = sched_list;
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 06/10] drm/sched: Disallow initializing entities with un-initialized schedulers
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/sched: drm_sched_entity_init() breaks v3d_open() on V3D 3.3 --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/sched: drm_sched_entity_init() breaks v3d_open() on V3D 3.3 --
commit c97e2213d0d6c3fa3066c68d77008ddc604e5ff1
Author: Tvrtko Ursulin <[email protected]>
drm/sched: Disallow initializing entities with un-initialized schedulers
This commit rejects uninitialized schedulers in drm_sched_entity_init() and drm_sched_entity_modify_sched() by checking that every scheduler in sched_list has a non-NULL sched_rq.
Quoted 25 lines
drivers/gpu/drm/scheduler/sched_entity.c +13
-0@@ -90,6 +90,19 @@ ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job)return duration; } +static bool drm_sched_list_is_initialized (struct drm_gpu_scheduler **sched_list, +unsigned int num_sched_list) +{ +unsigned int i; + +for (i =0 ; i < num_sched_list; i++) { +if (!sched_list[i] || !sched_list[i]->sched_rq) +return false; + } + +return true; +} +/** * drm_sched_entity_init - Init a context entity used by scheduler when * submit to HW ring.
drivers/gpu/drm/scheduler/sched_entity.c int drm_sched_entity_init(struct drm_sched_entity *entity, · line 139
Quoted 9 lines
drivers/gpu/drm/scheduler/sched_entity.c +4
-1@@ -119,7 +132,10 @@ int drm_sched_entity_init(struct drm_sched_entity *entity, unsigned int num_sched_list, atomic_t *guilty) { -if (!entity || !sched_list || !num_sched_list || !sched_list[0 ]) +if (!entity || !sched_list || !num_sched_list) +return -EINVAL ; + +if (!drm_sched_list_is_initialized (sched_list, num_sched_list))return -EINVAL ;
[Severity: High]
Does returning -EINVAL here break v3d_open() on V3D 3.3?
On V3D 3.3 (brcm,7268-v3d), v3d_has_csd() is false, so v3d_sched_init() in drivers/gpu/drm/v3d/v3d_sched.c skips initializing the V3D_CSD and V3D_CACHE_CLEAN schedulers and leaves their sched_rq NULL.
v3d_open() in drivers/gpu/drm/v3d/v3d_drv.c still calls drm_sched_entity_init() for all V3D_MAX_QUEUES, so every open() of the V3D device node now fails with -EINVAL.
v3d_open() (and v3d_postclose(), which calls drm_sched_entity_destroy() for all queues) would need to skip V3D_CSD and V3D_CACHE_CLEAN when !v3d_has_csd(v3d) before enforcing this check.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v2 06/10] drm/sched: Disallow initializing entities with un-initialized schedulers
In reply to Tvrtko UrsulinQuoted 2 lines · Tvrtko Ursulin
I do not *think* any of the drivers relies on this inverted initialization order but it needs more eyes for sure.
accel/rocket can hit this while it probes: rocket_probe() increments num_cores before drm_sched_init() runs for that core, and rocket_job_open() lists num_cores schedulers. An open() in that window succeeds today and returns -EINVAL with this patch.
I reproduced it on a ROCK 5B+ by polling open() while the module loads, with only this patch's check backported to a 7.2 kernel.
This is for rocket to fix. I'll send a patch.
Thanks,
Sidong
PATCH v2 7/10 drm/sched: Clamp priority againsts all provided schedulers
In reply to Tvrtko UrsulinCurrently the priority is only clamped against the first scheduler in the provided list at drm_sched_entity_init() time. Now that we are ensuring all schedulers have been initialized lets expand the clamp against all and by doing so make this check more robust.
--- drivers/gpu/drm/scheduler/sched_entity.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +9 -4
@@ -132,6 +132,8 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,unsigned int num_sched_list, atomic_t *guilty) { + unsigned int i; +if (!entity || !sched_list || !num_sched_list)return -EINVAL ;
@@ -151,17 +153,20 @@ int drm_sched_entity_init(struct drm_sched_entity *entity, entity->sched_list = num_sched_list >1 ? sched_list :NULL ;RB_CLEAR_NODE (&entity->rb_tree_node); -if (priority >= sched_list[0 ]->num_user_rqs) { +for (i =0 ; i < num_sched_list; i++) { +if (priority < sched_list[i]->num_user_rqs) +continue ; +/* * The "priority" of an entity cannot exceed the number of * run-queues of a scheduler. Protect against num_rqs being 0, * by converting to signed. Choose the lowest priority * available. */ -dev_err (sched_list[0 ]->dev,"entity with out-of-bounds priority:%u num_user_rqs:%u\n" , - priority, sched_list[0 ]->num_user_rqs); +dev_err (sched_list[i]->dev,"entity with out-of-bounds priority:%u num_user_rqs:%u\n" , + priority, sched_list[i]->num_user_rqs); priority =max_t (s32, - (s32 )sched_list[0 ]->num_user_rqs -1 , + (s32 )sched_list[i]->num_user_rqs -1 , (s32 )DRM_SCHED_PRIORITY_KERNEL ); }
Signature · 1 line
2.56.0
PATCH v2 8/10 drm/sched: Ignore stopped entity attempt to change its run-queue
In reply to Tvrtko UrsulinJobs submitted to a stopped entity do not go anywhere due how they are handled in drm_sched_rq_add_entity() so there is no point to allow them to change their run-queue. We can simply bail out early which makes entity->stopped a clearer boundary than it currently is.
This is completely safe because it is always guaranteed entity->rq is valid if drm_sched_entity_init() succeeded.
--- drivers/gpu/drm/scheduler/sched_entity.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +8 -4
@@ -605,13 +605,16 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)return ;spin_lock (&entity->lock); + +/* Cannot submit to stopped entity anyway so bail out early. */ +if (entity->stopped) + goto out; + fence = entity->last_scheduled;/* stay on the same engine if the previous job hasn't finished */ -if (fence && !dma_fence_is_signaled (fence)) { -spin_unlock (&entity->lock); -return ; - } +if (fence && !dma_fence_is_signaled (fence)) + goto out; sched =drm_sched_pick_best (entity->sched_list, entity->num_sched_list); rq = sched ? sched->sched_rq[entity->rq_priority] :NULL ;
@@ -623,6 +626,7 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)if (entity->num_sched_list ==1 ) entity->sched_list =NULL ; +out:spin_unlock (&entity->lock); }
Signature · 1 line
2.56.0
PATCH v2 9/10 drm/sched: Remove the single scheduler special case
In reply to Tvrtko UrsulinLong time ago in commitb3ac17667f11 ("drm/scheduler: rework entity creation")
a change was made which prevented priority changes for entities with only one assigned scheduler.
That commit reduced drm_sched_entity_set_priority() to simply update the entities priority, but the run queue selection logic in drm_sched_entity_select_rq() was never able to actually change the originally assigned run queue.
In practice that only affected amdgpu, being the only driver which can do dynamic priority changes, and it appears it was attempted to rectify this in commit2316a86bde49 ("drm/amdgpu: change hw sched list on ctx priority override") .
A few unresolved problems however remained:
The above only fixed drm_sched_entity_set_priority()
if drm_sched_entity_modify_sched() was called first which was not documented anywhere.
Secondly, that only worked if drm_sched_entity_modify_sched() is actually called, which in amdgpu's case is true only for gfx and compute. Priority changes for other engines, which have only one scheduler assigned such as jpeg and video decode still did not work.
(Note that this was also noticed in 981b04d96856 ("drm/sched: improve docs around drm_sched_entity").)
Completely different set of non-obvious confusion was that, whereas drm_sched_entity_init() was not keeping the passed in list of schedulers (courtesy of commit8c23056bdc7a ("drm/scheduler: do not keep a copy of sched list") ), drm_sched_entity_modify_sched() was disagreeing with that and would simply assign the single item list. That incosistency appears to have been semi-silently fixed in commitac4eb83ab255 ("drm/sched: select new rq even if there is only one v3") .
What was also not documented is why it was important to not keep the list of schedulers when there is only one scheduler given. This was in fact due many of the callers passing in a stack container for this case. For the multiple schedulers on the other hand amdgpu and rocket are the only such callers and there the container is not on the stack but correctly follows the lifetime requirement. Keeping a stack backed list in the entity would obviously be undefined behaviour if the list was kept.
Having said that, amdgpu however did only stop passing in stack backed container for the more than one scheduler case in commit 977f7e1068be ("drm/amdgpu: allocate entities on demand")
. Until then dereferencing freed stack from drm_sched_entity_select_rq() was a possibility.
In order to untangle all this lets make this area behave consistently and store the single scheduler case directly in the entity. We can do that easily by creating an union between the current pointer to a list of schedulers, and a new pointer to the single scheduler.
This allows removal of the special cases which artificially cleared entity->sched_list for a single scheduler case and fixes the amdgpu dynamic priority changes for the single IP instances.
References: 8c23056bdc7a ("drm/scheduler: do not keep a copy of sched list")
References: 977f7e1068be ("drm/amdgpu: allocate entities on demand")
References: 2316a86bde49 ("drm/amdgpu: change hw sched list on ctx priority override")
References: ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3")
References: 981b04d96856 ("drm/sched: improve docs around drm_sched_entity")
References: 77a6809f1dc3 ("drm/sched: Remove FIFO and RR and simplify to a single run queue")
Cc: Christian König <[email protected]>
Cc: Alex Deucher <[email protected]>
Cc: Luben Tuikov <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: Philipp Stanner <[email protected]>
Cc: Cc: Tomeu Vizoso <[email protected]>
Cc: Sidong Yang <[email protected]>
Cc: [email protected]
--- drivers/gpu/drm/scheduler/sched_entity.c | 30 ++++++++++------- include/drm/gpu_scheduler.h | 42 ++++++++++++++---------- 2 files changed, 44 insertions(+), 28 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +19 -11
@@ -115,7 +115,9 @@ static bool drm_sched_list_is_initialized(struct drm_gpu_scheduler **sched_list,* @guilty: atomic_t set to 1 when a job on this queue * is found to be guilty causing a timeout * -* Note that the &sched_list must have at least one element to schedule the entity. +* Note that the &sched_list must have exactly one element to schedule the +* entity. Callers which pass more than one scheduler on the @sched_list must +* ensure the list remains valid for the lifetime of the entity. * * For changing @priority later on at runtime see * drm_sched_entity_set_priority(). For changing the set of schedulers
@@ -150,7 +152,10 @@ int drm_sched_entity_init(struct drm_sched_entity *entity, entity->guilty = guilty; entity->last_user = current->group_leader; entity->num_sched_list = num_sched_list; - entity->sched_list = num_sched_list >1 ? sched_list :NULL ; +if (num_sched_list ==1 ) + entity->sched = sched_list[0 ]; +else + entity->sched_list = sched_list;RB_CLEAR_NODE (&entity->rb_tree_node);for (i =0 ; i < num_sched_list; i++) {
@@ -197,6 +202,9 @@ EXPORT_SYMBOL(drm_sched_entity_init);* existing entity->sched_list * @num_sched_list: number of drm sched in sched_list * +* Callers which pass more than one scheduler on the @sched_list must +* ensure the list remains valid for the lifetime of the entity. +* * Note that this must be called under the same common lock for @entity as * drm_sched_job_arm() and drm_sched_entity_push_job(), or the driver needs to * guarantee through some other means that this is never called while new jobs
@@ -214,7 +222,10 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,return ;spin_lock (&entity->lock); - entity->sched_list = sched_list; +if (num_sched_list ==1 ) + entity->sched = sched_list[0 ]; +else + entity->sched_list = sched_list; entity->num_sched_list = num_sched_list;spin_unlock (&entity->lock); }
@@ -596,10 +607,6 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)struct drm_gpu_scheduler *sched;struct drm_sched_rq *rq; -/* single possible engine and already selected */ -if (!entity->sched_list) -return ; -/* queue non-empty, stay on the same engine */ if (spsc_queue_count (&entity->job_queue))return ;
@@ -616,16 +623,17 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)if (fence && !dma_fence_is_signaled (fence)) goto out; - sched =drm_sched_pick_best (entity->sched_list, entity->num_sched_list); +if (entity->num_sched_list ==1 ) + sched = entity->sched; +else + sched =drm_sched_pick_best (entity->sched_list, + entity->num_sched_list); rq = sched ? sched->sched_rq[entity->rq_priority] :NULL ;if (rq != entity->rq) {drm_sched_rq_remove_entity (entity->rq, entity); entity->rq = rq; } -if (entity->num_sched_list ==1 ) - entity->sched_list =NULL ; - out:spin_unlock (&entity->lock); }
include/drm/gpu_scheduler.h +25 -17
48 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 09/10] drm/sched: Remove the single scheduler special case
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/sched: contradictory sched_list count in drm_sched_entity_init() doc --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/sched: contradictory sched_list count in drm_sched_entity_init() doc
--
commit 2a2c1e952ba97527448ca31cea515eb052a0d67c
Author: Tvrtko Ursulin <[email protected]>
drm/sched: Remove the single scheduler special case
This commit replaces the single-scheduler special case in struct drm_sched_entity with a union holding either a single scheduler pointer or a scheduler list, allowing dynamic priority changes on single-scheduler entities.
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_entity.c +0
-0
drivers/gpu/drm/scheduler/sched_entity.c static bool drm_sched_list_is_initialized(struct drm_gpu_scheduler **sched_list, · line 123
Quoted 10 lines
drivers/gpu/drm/scheduler/sched_entity.c +3
-1@@ -115,7 +115,9 @@ static bool drm_sched_list_is_initialized(struct drm_gpu_scheduler **sched_list,* @guilty: atomic_t set to 1 when a job on this queue * is found to be guilty causing a timeout * -* Note that the &sched_list must have at least one element to schedule the entity. +* Note that the &sched_list must have exactly one element to schedule the +* entity. Callers which pass more than one scheduler on the @sched_list must +* ensure the list remains valid for the lifetime of the entity. * * For changing @priority later on at runtime see * drm_sched_entity_set_priority(). For changing the set of schedulers
[Severity: Low]
Should this still say "at least one element" rather than "exactly one element"? drm_sched_entity_init() accepts any non-zero num_sched_list, and the very next sentence describes callers passing more than one scheduler.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v2 10/10 drm/sched: Address the fence_seq fixme
In reply to Tvrtko UrsulinAddress a FIXME against struct drm_sched_entity->fence_seq which documented this field does not need to be atomic_t.
Indeed now that most of the flow is guarded with entity->lock we can pull out the lock a step up, to drm_sched_job_arm(), which also simplifies drm_sched_entity_select_rq() and convert the field to a standard unsigned long.
--- drivers/gpu/drm/scheduler/sched_entity.c | 12 ++++-------- drivers/gpu/drm/scheduler/sched_fence.c | 6 ++++-- drivers/gpu/drm/scheduler/sched_main.c | 7 ++++++- include/drm/gpu_scheduler.h | 7 ++----- 4 files changed, 16 insertions(+), 16 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +4 -8
@@ -188,7 +188,6 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,spin_lock_init (&entity->lock);spsc_queue_init (&entity->job_queue); -atomic_set (&entity->fence_seq,0 ); entity->fence_context =dma_fence_context_alloc (2 );return 0 ;
@@ -607,21 +606,21 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)struct drm_gpu_scheduler *sched;struct drm_sched_rq *rq; +lockdep_assert_held (&entity->lock); +/* queue non-empty, stay on the same engine */ if (spsc_queue_count (&entity->job_queue))return ; -spin_lock (&entity->lock); -/* Cannot submit to stopped entity anyway so bail out early. */ if (entity->stopped) - goto out; +return ; fence = entity->last_scheduled;/* stay on the same engine if the previous job hasn't finished */ if (fence && !dma_fence_is_signaled (fence)) - goto out; +return ;if (entity->num_sched_list ==1 ) sched = entity->sched;
@@ -633,9 +632,6 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)drm_sched_rq_remove_entity (entity->rq, entity); entity->rq = rq; } - -out: -spin_unlock (&entity->lock); }/**
drivers/gpu/drm/scheduler/sched_fence.c +4 -2
@@ -224,10 +224,12 @@ struct drm_sched_fence *drm_sched_fence_alloc(void *owner,void drm_sched_fence_init (struct drm_sched_fence *fence,struct drm_sched_entity *entity) { -unsigned seq; +unsigned long seq; + +lockdep_assert_held (&entity->lock); fence->sched = entity->rq->sched; - seq =atomic_inc_return (&entity->fence_seq); + seq = ++entity->fence_seq;dma_fence_init (&fence->scheduled, &drm_sched_fence_ops_scheduled, &fence->lock, entity->fence_context, seq,0 ); dma_fence_init(&fence->finished, &drm_sched_fence_ops_finished,
drivers/gpu/drm/scheduler/sched_main.c +6 -1
@@ -646,6 +646,9 @@ void drm_sched_job_arm(struct drm_sched_job *job)struct drm_sched_entity *entity = job->entity;BUG_ON (!entity); + +spin_lock (&entity->lock); +drm_sched_entity_select_rq (entity); sched = entity->rq->sched;
@@ -653,7 +656,9 @@ void drm_sched_job_arm(struct drm_sched_job *job) job->s_priority = entity->priority; job->entity_stats =drm_sched_entity_stats_get (entity->stats); -drm_sched_fence_init (job->s_fence, job->entity); +drm_sched_fence_init (job->s_fence, entity); + +spin_unlock (&entity->lock); }EXPORT_SYMBOL (drm_sched_job_arm);
include/drm/gpu_scheduler.h +2 -5
@@ -101,7 +101,7 @@ struct drm_sched_entity {* * Lock protecting the run-queue (@rq) to which this entity belongs, * @priority, @last_scheduled and the list of schedulers (@sched_list, -* @num_sched_list), @rr_ts and @stopped. +* @num_sched_list), @rr_ts, @stopped and @fence_seq. */ spinlock_t lock;
@@ -184,11 +184,8 @@ struct drm_sched_entity {* * A linearly increasing seqno incremented with each new * &drm_sched_fence which is part of the entity. -* -* FIXME: Callers of drm_sched_job_arm() need to ensure correct locking, -* this doesn't need to be atomic. */ -atomic_t fence_seq; +unsigned long fence_seq;/** * @fence_context:
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 10/10] drm/sched: Address the fence_seq fixme
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/sched: unconditional entity->lock in drm_sched_job_arm() regresses hot path --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/sched: unconditional entity->lock in drm_sched_job_arm() regresses hot path
--
commit e186c89420ee185250eb89c11064089f96c74447
Author: Tvrtko Ursulin <[email protected]>
drm/sched: Address the fence_seq fixme
This commit moves entity->lock acquisition from drm_sched_entity_select_rq() up into drm_sched_job_arm() and converts entity->fence_seq from atomic_t to unsigned long.
Quoted 15 lines
drivers/gpu/drm/scheduler/sched_main.c +3
-0@@ -646,6 +646,9 @@ void drm_sched_job_arm(struct drm_sched_job *job)struct drm_sched_entity *entity = job->entity;BUG_ON (!entity); + +spin_lock (&entity->lock); +drm_sched_entity_select_rq (entity); sched = entity->rq->sched;
drivers/gpu/drm/scheduler/sched_main.c void drm_sched_job_arm(struct drm_sched_job *job) · line 663
Quoted 9 lines
drivers/gpu/drm/scheduler/sched_main.c +3
-1@@ -653,7 +656,9 @@ void drm_sched_job_arm(struct drm_sched_job *job) job->s_priority = entity->priority; job->entity_stats =drm_sched_entity_stats_get (entity->stats); -drm_sched_fence_init (job->s_fence, job->entity); +drm_sched_fence_init (job->s_fence, entity); + +spin_unlock (&entity->lock); }EXPORT_SYMBOL (drm_sched_job_arm);
[Severity: Medium]
Does taking entity->lock unconditionally in drm_sched_job_arm() regress the submission hot path on busy entities?
Previously, drm_sched_entity_select_rq() returned before acquiring entity->lock whenever spsc_queue_count(&entity->job_queue) was non-zero, leaving drm_sched_job_arm() lockless on pipelined workloads with only a producer-local atomic_inc_return(&entity->fence_seq).
Now every job submission acquires entity->lock here (in addition to drm_sched_entity_push_job()) and holds it across drm_sched_entity_stats_get() and both dma_fence_init() calls in drm_sched_fence_init().
Because the scheduler worker acquires entity->lock in drm_sched_entity_pop_job() and nests rq->lock in drm_sched_rq_pop_entity(), this trades an uncontended atomic increment for cross-CPU spinlock contention and stalls submitters behind rq->lock.
Since callers already serialize drm_sched_job_arm() and drm_sched_entity_push_job() per entity, fence_seq can be converted without widening entity->lock across the non-empty queue fast path and fence init.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Hi Tvrtko,
Quoted 8 lines · Tvrtko Ursulin
Stricter rules around entity initialization, removing the single scheduler special case which fixes the priority override for amdgpu single IP instance entities and addressing of
fence_seqFIXME by pulling theentity->lockup fromdrm_sched_entity_select_rq().In the last round sashiko complained about the first patch breaking legacy v3d but Maíra has confirmed that is an unused path so no problem to make it stricter. Lets see what sashiko finds in this round.
Just to let you know, I already submitted a series to remove V3D < 4.2 [1]. I'm just waiting for DT maintainers feedback.
Thank you for the series!
[1] https://lore.kernel.org/dri-devel/[email protected]/T/
Best regards,
- Maíra
Quoted 46 lines
v2:
* New patches in the series:
* Fix amdgpu VCN potential usage of un-initialized schedulers.
* Make entaviv and msm checkdrm_sched_entity_initreturn value.
* Markdrm_sched_entity_initas__must_check.
* Clamp priority levels against all schedulers indrm_sched_entity_init().Cc Christian König <[email protected]>Cc Danilo Krummrich <[email protected]>Cc Matthew Brost <[email protected]>Cc Philipp Stanner <[email protected]>Cc Maíra Canal <[email protected]>Cc Lucas Stach <[email protected]>Cc Russell King <[email protected]>Cc Christian Gmeiner <[email protected]>Cc Rob Clark <[email protected]>Cc Dmitry Baryshkov <[email protected]Tvrtko Ursulin (10):
drm/amdgpu/vcn3: Fail job submission if VCN0 decode is disabled under
SR-IOV
drm/etnaviv: Release context id on failure
drm/etnaviv: Checkdrm_sched_entity_init()return value drm/msm: Checkdrm_sched_entity_init()return value drm/sched: Markdrm_sched_entity_init()as__must_checkdrm/sched: Disallow initializing entities with un-initialized
schedulers
drm/sched: Clamp priority againsts all provided schedulers drm/sched: Ignore stopped entity attempt to change its run-queue drm/sched: Remove the single scheduler special case drm/sched: Address thefence_seqfixmedrivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 4 ++ drivers/gpu/drm/etnaviv/etnaviv_drv.c | 26 ++++--- drivers/gpu/drm/msm/msm_submitqueue.c | 9 ++- drivers/gpu/drm/scheduler/sched_entity.c | 89 +++++++++++++++--------- drivers/gpu/drm/scheduler/sched_fence.c | 6 +- drivers/gpu/drm/scheduler/sched_main.c | 7 +- include/drm/gpu_scheduler.h | 59 +++++++++------- 7 files changed, 129 insertions(+), 71 deletions(-)