drm/scheduler: Fix hang when sched_entity released
Subsystems (3)
PATCH v2 1/1 drm/scheduler: Fix hang when sched_entity released
Problem: If scheduler is already stopped by the time sched_entity is released and entity's job_queue not empty I encountred
a hang in drm_sched_entity_flush. This is because drm_sched_entity_is_idle never becomes false.
Fix: In drm_sched_fini detach all sched_entities from the scheduler's run queues. This will satisfy drm_sched_entity_is_idle. Also wakeup all those processes stuck in sched_entity flushing as the scheduler main thread which wakes them up is stopped by now.
v2:
Reverse order of drm_sched_rq_remove_entity and marking s_entity as stopped to prevent reinserion back to rq due to race.
--- drivers/gpu/drm/scheduler/sched_main.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +31 -0
@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini (struct drm_gpu_scheduler *sched) { +int i; +struct drm_sched_entity *s_entity;if (sched->thread)kthread_stop (sched->thread); +/* Detach all sched_entites from this scheduler once it's stopped */ +for (i =DRM_SCHED_PRIORITY_COUNT -1 ; i >=DRM_SCHED_PRIORITY_MIN ; i--) { +struct drm_sched_rq *rq = &sched->sched_rq[i]; + +if (!rq) +continue ; + +/* Loop this way because rq->lock is taken in drm_sched_rq_remove_entity */ +spin_lock (&rq->lock); +while ((s_entity =list_first_entry_or_null (&rq->entities, + struct drm_sched_entity, + list))) { +spin_unlock (&rq->lock); + +/* Prevent reinsertion and remove */ +spin_lock (&s_entity->rq_lock); + s_entity->stopped = true; +drm_sched_rq_remove_entity (rq, s_entity); +spin_unlock (&s_entity->rq_lock); + +spin_lock (&rq->lock); + } +spin_unlock (&rq->lock); + + } + +/* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */ +wake_up_all (&sched->job_scheduled); +/* Confirm no work left behind accessing device structures */ cancel_delayed_work_sync (&sched->work_tdr);
Signature · 6 lines
2.7.4 _______________________________________________ amd-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Christian König
reviewerAm 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 25 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>--- drivers/gpu/drm/scheduler/sched_main.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0
drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini (struct drm_gpu_scheduler *sched) { +int i; +struct drm_sched_entity *s_entity;
BTW: Please order that so that i is declared last.
Quoted 12 lines
drivers/gpu/drm/scheduler/sched_main.c +9
-0if (sched->thread)kthread_stop (sched->thread); +/* Detach all sched_entites from this scheduler once it's stopped */ +for (i =DRM_SCHED_PRIORITY_COUNT -1 ; i >=DRM_SCHED_PRIORITY_MIN ; i--) { +struct drm_sched_rq *rq = &sched->sched_rq[i]; + +if (!rq) +continue ; + +/* Loop this way because rq->lock is taken in drm_sched_rq_remove_entity */ +spin_lock (&rq->lock);
drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 923
Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +10
-0+while ((s_entity =list_first_entry_or_null (&rq->entities, + struct drm_sched_entity, + list))) { +spin_unlock (&rq->lock); + +/* Prevent reinsertion and remove */ +spin_lock (&s_entity->rq_lock); + s_entity->stopped = true; +drm_sched_rq_remove_entity (rq, s_entity); +spin_unlock (&s_entity->rq_lock);
Well this spin_unlock/lock dance here doesn't look correct at all now.
Christian.
drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 936
Quoted 13 lines
drivers/gpu/drm/scheduler/sched_main.c +10
-0+ +spin_lock (&rq->lock); + } +spin_unlock (&rq->lock); + + } + +/* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */ +wake_up_all (&sched->job_scheduled); +/* Confirm no work left behind accessing device structures */ cancel_delayed_work_sync (&sched->work_tdr);
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 61 lines · Christian König
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 25 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31 +++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 14 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) { + structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities, + structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity); +spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see drm_sched_entity_push_job and drm_sched_entity_flush). If i just locked rq->lock and did list_for_each_entry_safe while manually deleting entity->list instead of calling drm_sched_rq_remove_entity this still would not be possible as the order of lock acquisition between s_entity->rq_lock and rq->lock would be reverse compared to the call sites mentioned above.
Andrey
Quoted 14 lines
Quoted 11 lines
drivers/gpu/drm/scheduler/sched_main.c +10
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */ + wake_up_all(&sched->job_scheduled); +Quoted 2 lines
/* Confirm no work left behind accessing device structures */
cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Christian König
reviewerAm 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 65 lines · Christian König
Quoted 63 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities, + structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity); +spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.
Ah, now I understand. You need this because drm_sched_rq_remove_entity() will grab the rq lock again!
Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Quoted 20 lines
Andrey
Quoted 15 lines
Quoted 14 lines
drivers/gpu/drm/scheduler/sched_main.c +8
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush for thisscheduler */
+wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Quoted 73 lines · Christian König
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 64 lines · Christian König
Quoted 62 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 25 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31 +++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities, + structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity); +spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.
Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately) sched_entity is part of amdgpu_ctx_entity and amdgpu_ctx_entity is refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigning sched_entity to new rq (e.g. before drm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (in amdgpu_cs_parser_init and amdgpu_cs_parser_fini by calling amdgpu_ctx_get and amdgpu_ctx_put). But this seems a bit tricky due to all the drm_sched_entity_select_rq logic.
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around drm_sched_fini when finalizing the fence driver and around idr iteration in amdgpu_ctx_mgr_fini (which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.
Andrey
Quoted 7 lines
Quoted 7 lines
Andrey
Quoted 1 line
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); +Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
+ }
drivers/gpu/drm/scheduler/sched_main.c +4
-0+ + /* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */ + wake_up_all(&sched->job_scheduled); +/* Confirm no work left behind accessing device structures */
cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Ping
Andrey
Quoted 88 lines · Andrey Grodzovsky
Quoted 73 lines · Christian König
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 64 lines · Christian König
Quoted 62 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 25 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31 +++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities, + structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity); +spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.
Quoted 28 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.Andrey
Quoted 21 lines
Quoted 20 lines
Andrey
Quoted 14 lines
Quoted 13 lines
drivers/gpu/drm/scheduler/sched_main.c +10
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */ + wake_up_all(&sched->job_scheduled); +/* Confirm no work left behind accessing device structures */
cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Christian König
reviewerAm 18.02.21 um 17:41 schrieb Andrey Grodzovsky:
Quoted 89 lines · Christian König
Quoted 74 lines
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 65 lines · Christian König
Quoted 63 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities, + structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity); +spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.
Quoted 3 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.
Puh, that's rather complicated as well. Ok let's look at it from the other side for a moment.
Why do we have to remove the entities from the rq in the first place?
Wouldn't it be sufficient to just set all of them to stopped?
Christian.
Quoted 26 lines
Andrey
Quoted 22 lines
Quoted 21 lines
Andrey
Quoted 15 lines
Quoted 14 lines
drivers/gpu/drm/scheduler/sched_main.c +8
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush for thisscheduler */
+wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 96 lines · Christian König
Am 18.02.21 um 17:41 schrieb Andrey Grodzovsky:
Quoted 89 lines · Christian König
Quoted 74 lines
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 65 lines · Christian König
Quoted 63 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities, + structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity); +spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.Quoted 3 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.
Quoted 7 lines
Puh, that's rather complicated as well. Ok let's look at it from the other side for a moment.
Why do we have to remove the entities from the rq in the first place?
Wouldn't it be sufficient to just set all of them to stopped?
Christian.
And adding it as another condition in drm_sched_entity_is_idle ?
Andrey
Quoted 12 lines
Quoted 12 lines
Andrey
Quoted 7 lines
Quoted 7 lines
Andrey
Quoted 1 line
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/scheduler/sched_main.c +7
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } +Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
+ /* Wakeup everyone stuck in
drm_sched_entity_flushfor this scheduler */
+wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Ping
Andrey
Quoted 140 lines · Andrey Grodzovsky
Quoted 96 lines · Christian König
Am 18.02.21 um 17:41 schrieb Andrey Grodzovsky:
Quoted 89 lines · Christian König
Quoted 74 lines
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 65 lines · Christian König
Quoted 63 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities, + structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity); +spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.Quoted 3 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.Quoted 7 lines
Puh, that's rather complicated as well. Ok let's look at it from the other side for a moment.
Why do we have to remove the entities from the rq in the first place?
Wouldn't it be sufficient to just set all of them to stopped?
Christian.
And adding it as another condition in
drm_sched_entity_is_idle?Andrey
Quoted 12 lines
Quoted 12 lines
Andrey
Quoted 7 lines
Quoted 7 lines
Andrey
Quoted 1 line
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/scheduler/sched_main.c +7
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } +Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
+ /* Wakeup everyone stuck in
drm_sched_entity_flushfor this scheduler */
+wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
Quoted 5 lines
_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&data=04%7C01%7Candrey.grodzovsky%40amd.com%7Cd948a126eeb84abf3e1308d8d598ca2f%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637494199501121764%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=SAWjfsFZ%2BTi6jKVcyJtIC9qM8EgGthmt3MnSFlxbut8%3D&reserved=0
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Ping
Andrey
Quoted 102 lines · Andrey Grodzovsky
Quoted 100 lines · Christian König
Am 18.02.21 um 17:41 schrieb Andrey Grodzovsky:
Quoted 89 lines · Christian König
Quoted 74 lines
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 65 lines · Christian König
Quoted 63 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities, + structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity); +spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.Quoted 3 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.Puh, that's rather complicated as well. Ok let's look at it from the other side for a moment.
Why do we have to remove the entities from the rq in the first place?
Quoted 38 lines
Quoted 3 lines
Wouldn't it be sufficient to just set all of them to stopped?
Christian.
And adding it as another condition in
drm_sched_entity_is_idle?Andrey
Quoted 28 lines
Quoted 27 lines
Andrey
Quoted 22 lines
Quoted 21 lines
Andrey
Quoted 15 lines
Quoted 14 lines
drivers/gpu/drm/scheduler/sched_main.c +8
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush for thisscheduler */
+wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Christian König
reviewerAm 24.02.21 um 16:13 schrieb Andrey Grodzovsky:
Quoted 1 line
Ping
Sorry, I've been on vacation this week.
Quoted 113 lines
Andrey
Quoted 109 lines · Andrey Grodzovsky
Quoted 97 lines · Christian König
Am 18.02.21 um 17:41 schrieb Andrey Grodzovsky:
Quoted 90 lines · Christian König
Quoted 75 lines
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 66 lines · Christian König
Quoted 64 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities,
+ structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity); +spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.Quoted 3 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.Quoted 7 lines
Puh, that's rather complicated as well. Ok let's look at it from the other side for a moment.
Why do we have to remove the entities from the rq in the first place?
Wouldn't it be sufficient to just set all of them to stopped?
Christian.
And adding it as another condition in
drm_sched_entity_is_idle?
Yes, I think that should work.
Christian.
Quoted 16 lines
Quoted 16 lines
Andrey
Quoted 12 lines
Quoted 12 lines
Andrey
Quoted 7 lines
Quoted 7 lines
Andrey
Quoted 1 line
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +8
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush forQuoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
this scheduler */
+wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Quoted 112 lines · Christian König
Am 24.02.21 um 16:13 schrieb Andrey Grodzovsky:
Quoted 1 line
Ping
Sorry, I've been on vacation this week.
Quoted 107 lines
Andrey
Quoted 103 lines · Andrey Grodzovsky
Quoted 101 lines · Christian König
Am 18.02.21 um 17:41 schrieb Andrey Grodzovsky:
Quoted 90 lines · Christian König
Quoted 75 lines
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 66 lines · Christian König
Quoted 64 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities,
+ structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */ +spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity);
+spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.Quoted 3 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.Puh, that's rather complicated as well. Ok let's look at it from the other side for a moment.
Why do we have to remove the entities from the rq in the first place?
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Wouldn't it be sufficient to just set all of them to stopped?
Christian.
And adding it as another condition in
drm_sched_entity_is_idle?Yes, I think that should work.
In this case reverse locking order is created, In drm_sched_entity_push_job and drm_sched_entity_flush lock entity->rq_lock locked first and rq->lock locked second. In drm_sched_fini on the other hand, I need to lock rq->lock first to iterate rq->entities and then lock s_entity->rq_lock for each entity to modify s_entity->stopped. I guess we could change s_entity->stopped to atomic ?
Andrey
Quoted 37 lines
Christian.
Quoted 32 lines
Quoted 32 lines
Andrey
Quoted 28 lines
Quoted 27 lines
Andrey
Quoted 22 lines
Quoted 21 lines
Andrey
Quoted 15 lines
Quoted 14 lines
drivers/gpu/drm/scheduler/sched_main.c +8
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush forthis scheduler */
+wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Christian König
reviewerAm 25.02.21 um 17:03 schrieb Andrey Grodzovsky:
Quoted 119 lines · Christian König
Quoted 117 lines
Am 24.02.21 um 16:13 schrieb Andrey Grodzovsky:
Quoted 1 line
Ping
Sorry, I've been on vacation this week.
Quoted 112 lines
Andrey
Quoted 108 lines · Andrey Grodzovsky
Quoted 104 lines · Christian König
Am 18.02.21 um 17:41 schrieb Andrey Grodzovsky:
Quoted 90 lines · Christian König
Quoted 75 lines
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 66 lines · Christian König
Quoted 64 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities,
+ structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */
+spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity);
+spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.Quoted 3 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.Puh, that's rather complicated as well. Ok let's look at it from the other side for a moment.
Why do we have to remove the entities from the rq in the first place?
Wouldn't it be sufficient to just set all of them to stopped?
Christian.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
And adding it as another condition in
drm_sched_entity_is_idle?Yes, I think that should work.
In this case reverse locking order is created, In
drm_sched_entity_push_jobanddrm_sched_entity_flushlockentity->rq_locklocked first andrq->locklocked second. Indrm_sched_finion the other hand, I need to lockrq->lockfirst to iteraterq->entitiesand then locks_entity->rq_lockfor each entity to modifys_entity->stopped. I guess we could changes_entity->stoppedto atomic ?
Good point. But I think the memory barrier inserted by wake_up_all(&sched->job_scheduled); should be sufficient.
If I see this correctly we have the entity->rq_lock mainly to protect concurrent changes of entity->rq.
But when two CPUs both set entity->stopped to true at the same time we don't really care about it as long drm_sched_entity_is_idle() sees it.
Regards,
Christian.
Quoted 41 lines
Andrey
Quoted 37 lines
Christian.
Quoted 32 lines
Quoted 32 lines
Andrey
Quoted 28 lines
Quoted 27 lines
Andrey
Quoted 22 lines
Quoted 21 lines
Andrey
Quoted 15 lines
Quoted 14 lines
drivers/gpu/drm/scheduler/sched_main.c +8
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush forthis scheduler */
+wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Quoted 129 lines · Christian König
Am 25.02.21 um 17:03 schrieb Andrey Grodzovsky:
Quoted 119 lines · Christian König
Quoted 117 lines
Am 24.02.21 um 16:13 schrieb Andrey Grodzovsky:
Quoted 1 line
Ping
Sorry, I've been on vacation this week.
Quoted 112 lines
Andrey
Quoted 108 lines · Andrey Grodzovsky
Quoted 104 lines · Christian König
Am 18.02.21 um 17:41 schrieb Andrey Grodzovsky:
Quoted 90 lines · Christian König
Quoted 75 lines
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 66 lines · Christian König
Quoted 64 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities,
+ structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */
+spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity);
+spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.Quoted 3 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.Puh, that's rather complicated as well. Ok let's look at it from the other side for a moment.
Why do we have to remove the entities from the rq in the first place?
Wouldn't it be sufficient to just set all of them to stopped?
Christian.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
And adding it as another condition in
drm_sched_entity_is_idle?Yes, I think that should work.
In this case reverse locking order is created, In
drm_sched_entity_push_jobanddrm_sched_entity_flushlockentity->rq_locklocked first andrq->locklocked second. Indrm_sched_finion the other hand, I need to lockrq->lockfirst to iteraterq->entitiesand then locks_entity->rq_lockfor each entity to modifys_entity->stopped. I guess we could changes_entity->stoppedto atomic ?
Quoted 8 lines
Good point. But I think the memory barrier inserted by
wake_up_all(&sched->job_scheduled); should be sufficient.If I see this correctly we have the
entity->rq_lockmainly to protect concurrent changes ofentity->rq.But when two CPUs both set
entity->stoppedto true at the same time we don't really care about it as longdrm_sched_entity_is_idle()sees it.Regards,
Christian.
I was more thinking about integrity of reading/writing entity->stopped from different threads. I guess since it's bool it's guaranteed to be atomic from HW perspective anyway ? Will send V3 soon.
Andrey
Quoted 34 lines
Quoted 34 lines
Andrey
Quoted 29 lines
Christian.
Quoted 25 lines
Quoted 25 lines
Andrey
Quoted 21 lines
Quoted 21 lines
Andrey
Quoted 16 lines
Quoted 16 lines
Andrey
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +8
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush forthis scheduler */
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
+
wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Christian König
reviewerAm 25.02.21 um 22:27 schrieb Andrey Grodzovsky:
Quoted 135 lines · Christian König
Quoted 133 lines
Am 25.02.21 um 17:03 schrieb Andrey Grodzovsky:
Quoted 119 lines · Christian König
Quoted 117 lines
Am 24.02.21 um 16:13 schrieb Andrey Grodzovsky:
Quoted 1 line
Ping
Sorry, I've been on vacation this week.
Quoted 112 lines
Andrey
Quoted 108 lines · Andrey Grodzovsky
Quoted 104 lines · Christian König
Am 18.02.21 um 17:41 schrieb Andrey Grodzovsky:
Quoted 90 lines · Christian König
Quoted 75 lines
Am 18.02.21 um 16:05 schrieb Andrey Grodzovsky:
Quoted 66 lines · Christian König
Quoted 64 lines
Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:
Quoted 26 lines
Problem: If scheduler is already stopped by the time
sched_entityis released and entity'sjob_queuenot empty I encountred
a hang indrm_sched_entity_flush. This is becausedrm_sched_entity_is_idlenever becomes false.Fix: In
drm_sched_finidetach allsched_entitiesfrom the scheduler's run queues. This will satisfydrm_sched_entity_is_idle. Also wakeup all those processes stuck insched_entityflushing as the scheduler main thread which wakes them up is stopped by now.v2:
Reverse order ofdrm_sched_rq_remove_entityand markings_entityas stopped to prevent reinserion back to rq due to race.Signed-off-by Andrey Grodzovsky <[email protected]>---
drivers/gpu/drm/scheduler/sched_main.c| 31
+++++++++++++++++++++++++++++++1 file changed, 31 insertions(+)drivers/gpu/drm/scheduler/sched_main.c +0
-0drivers/gpu/drm/scheduler/sched_main.c EXPORT_SYMBOL(drm_sched_init); · line 901
Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +2
-0@@ -897,9 +897,40 @@ EXPORT_SYMBOL(drm_sched_init);*/ void drm_sched_fini(struct drm_gpu_scheduler *sched) { + int i; + struct drm_sched_entity *s_entity;BTW: Please order that so that i is declared last.
Quoted 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0
-0if (sched->thread) kthread_stop(sched->thread);+ /* Detach all
sched_entitesfrom this scheduler once it's stopped */ + for (i =DRM_SCHED_PRIORITY_COUNT- 1; i >=DRM_SCHED_PRIORITY_MIN; i--) {
+ structdrm_sched_rq*rq = &sched->sched_rq[i];drivers/gpu/drm/scheduler/sched_main.c +5
-0+ + if (!rq) + continue; + + /* Loop this way because rq->lock is taken in
drm_sched_rq_remove_entity*/
+spin_lock(&rq->lock);
+ while ((s_entity= list_first_entry_or_null(&rq->entities,
+ structdrm_sched_entity,Quoted 8 lines
+ list))) {
+spin_unlock(&rq->lock);
+
+ /* Prevent reinsertion and remove */
+spin_lock(&s_entity->rq_lock);
+s_entity->stopped= true;
+drm_sched_rq_remove_entity(rq, s_entity);
+spin_unlock(&s_entity->rq_lock);Well this spin_unlock/lock dance here doesn't look correct at all now.
Quoted 7 lines
Quoted 1 line
Christian.
In what way ? It's in the same same order as in other call sites (see
drm_sched_entity_push_jobanddrm_sched_entity_flush). If i just lockedrq->lockand didlist_for_each_entry_safewhile manually deletingentity->listinstead of callingdrm_sched_rq_remove_entitythis still would not be possible as the order of lock acquisition betweens_entity->rq_lockandrq->lockwould be reverse compared to the call sites mentioned above.Quoted 5 lines
Ah, now I understand. You need this because
drm_sched_rq_remove_entity()will grab the rq lock again!Problem is now what prevents the entity from being destroyed while you remove it?
Christian.
Right, well, since (unfortunately)
sched_entityis part ofamdgpu_ctx_entityandamdgpu_ctx_entityis refcounted there is a problem here that we don't increment amdgpu_ctx.refcount when assigningsched_entityto new rq (e.g. beforedrm_sched_rq_add_entity) and not decrement before removing. We do it for amdgpu_cs_parser.entity for example (inamdgpu_cs_parser_initandamdgpu_cs_parser_finiby callingamdgpu_ctx_getandamdgpu_ctx_put). But this seems a bit tricky due to all thedrm_sched_entity_select_rqlogic.Quoted 3 lines
Another, kind of a band aid fix, would probably be just locking amdgpu_ctx_mgr.lock around
drm_sched_finiwhen finalizing the fence driver and around idr iteration inamdgpu_ctx_mgr_fini(which should be lock protected anyway as I see from other idr usages in the code) ... This should prevent this use after free.Puh, that's rather complicated as well. Ok let's look at it from the other side for a moment.
Why do we have to remove the entities from the rq in the first place?
Wouldn't it be sufficient to just set all of them to stopped?
Christian.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
And adding it as another condition in
drm_sched_entity_is_idle?Yes, I think that should work.
In this case reverse locking order is created, In
drm_sched_entity_push_jobanddrm_sched_entity_flushlockentity->rq_locklocked first andrq->locklocked second. Indrm_sched_finion the other hand, I need to lockrq->lockfirst to iteraterq->entitiesand then locks_entity->rq_lockfor each entity to modifys_entity->stopped. I guess we could changes_entity->stoppedto atomic ?Good point. But I think the memory barrier inserted by
wake_up_all(&sched->job_scheduled); should be sufficient.If I see this correctly we have the
entity->rq_lockmainly to protect concurrent changes ofentity->rq.
Quoted 7 lines
Quoted 4 lines
But when two CPUs both set
entity->stoppedto true at the same time we don't really care about it as longdrm_sched_entity_is_idle()sees it.Regards,
Christian.I was more thinking about integrity of reading/writing
entity->stoppedfrom different threads. I guess since it's bool it's guaranteed to be atomic from HW perspective anyway ?
More or less yes. The key point is that we only change it from false -> true and never the other way around. All that you need then for other CPUs to see the value is a barrier.
Christian.
Quoted 48 lines
Will send V3 soon.
Andrey
Quoted 43 lines
Quoted 42 lines
Andrey
Quoted 37 lines
Christian.
Quoted 32 lines
Quoted 32 lines
Andrey
Quoted 28 lines
Quoted 27 lines
Andrey
Quoted 22 lines
Quoted 21 lines
Andrey
Quoted 15 lines
Quoted 14 lines
drivers/gpu/drm/scheduler/sched_main.c +8
-0+ + spin_lock(&rq->lock); + } + spin_unlock(&rq->lock); + + } + + /* Wakeup everyone stuck in drm_sched_entity_flush forthis scheduler */
+wake_up_all(&sched->job_scheduled);
+
/* Confirm no work left behind accessing device structures */cancel_delayed_work_sync(&sched->work_tdr);
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel