Skip to contentSkip to conversation

drm/scheduler: Fix hang when sched_entity released

PATCH Superseded Andrey Grodzovsky 2 participants 15 messages Feb 17, 2021 – Feb 26, 2021

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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
index 908b0b5..c6b7947 100644
@@ -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
#1 lore raw

Am 17.02.21 um 22:59 schrieb Andrey Grodzovsky:

Quoted 25 lines

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 +0 -0
index 908b0b5..c6b7947 100644

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 -0
  	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);

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

#2 lore raw
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_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 +0 -0
index 908b0b5..c6b7947 100644

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 14 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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, +                                struct drm_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

#3 lore raw

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_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 +0 -0
index 908b0b5..c6b7947 100644

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 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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, +                                struct drm_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_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.

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 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

#4 lore raw
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_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 +0 -0
index 908b0b5..c6b7947 100644

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 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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, +                                struct drm_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_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.

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

#5 lore raw

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_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 +0 -0
index 908b0b5..c6b7947 100644

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 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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, +                                struct drm_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_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.

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.

Quoted 28 lines

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 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

#6 lore raw
In reply to Andrey Grodzovsky

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_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 +0 -0
index 908b0b5..c6b7947 100644

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 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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, +                                struct drm_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_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.

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.

Quoted 3 lines

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.

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 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

#7 lore raw
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_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 +0 -0
index 908b0b5..c6b7947 100644

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 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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, +                                struct drm_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_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.

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.

Quoted 3 lines

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.

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_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);

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#8 lore raw

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_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 +0 -0
index 908b0b5..c6b7947 100644

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 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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, +                                struct drm_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_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.

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.

Quoted 3 lines

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.

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_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);

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

#9 lore raw
In reply to Andrey Grodzovsky

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_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 +0 -0
index 908b0b5..c6b7947 100644

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 15 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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, +                                struct drm_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_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.

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.

Quoted 3 lines

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.

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 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

#10 lore raw

Am 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_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 +0 -0
index 908b0b5..c6b7947 100644

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 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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,
+                                struct drm_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_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.

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.

Quoted 3 lines

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.

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 for 
Quoted 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

#11 lore raw
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_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 +0 -0
index 908b0b5..c6b7947 100644

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 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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,
+                                struct drm_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_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.

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.

Quoted 3 lines

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.

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 for 

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

#12 lore raw

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_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 +0 -0
index 908b0b5..c6b7947 100644

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 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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,
+                                struct drm_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_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.

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.

Quoted 3 lines

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.

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_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 ?

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 for 

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

#13 lore raw
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_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 +0 -0
index 908b0b5..c6b7947 100644

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 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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,
+                                struct drm_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_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.

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.

Quoted 3 lines

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.

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_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 ?

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_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.

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 for 

this 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

#14 lore raw

Am 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_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 +0 -0
index 908b0b5..c6b7947 100644

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 16 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
       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];

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,
+                                struct drm_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_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.

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.

Quoted 3 lines

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.

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_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 ?

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.

Quoted 7 lines
Quoted 4 lines

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.

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 ?

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 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