Skip to contentSkip to conversation

drm/sched: add WARN_ON and BUG_ON to drm_sched_fini

PATCH In review Christian König 8 participants 28 messages Sep 18, 2024 – Nov 25, 2024

Subsystems (3)
lore
Share & tools
lore

Christian König

author reviewer
#0 lore raw

PATCH 1/2 drm/sched: add WARN_ON and BUG_ON to drm_sched_fini

Tearing down the scheduler with jobs still on the pending list can lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

When there are still entities with jobs the situation is even worse since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well let them run into a BUG_ON().

---
 drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +18 -1
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
 
 	drm_sched_wqueue_stop(sched);
 
+	/*
+	 * Tearing down the scheduler wile there are still unprocessed jobs can
+	 * lead to use after free issues in the scheduler fence.
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+
 	for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
 		struct drm_sched_rq *rq = sched->sched_rq[i];
 
 		spin_lock(&rq->lock);
-		list_for_each_entry(s_entity, &rq->entities, list)
+		list_for_each_entry(s_entity, &rq->entities, list) {
+			/*
+			 * The justification for this BUG_ON() is that tearing
+			 * down the scheduler while jobs are pending leaves
+			 * dma_fences unsignaled. Since we have dependencies
+			 * from the core memory management to eventually signal
+			 * dma_fences this can trivially lead to a system wide
+			 * stop because of a locked up memory management.
+			 */
+			BUG_ON(spsc_queue_count(&s_entity->job_queue));
+
 			/*
 			 * Prevents reinsertion and marks job_queue as idle,
 			 * it will removed from rq in drm_sched_entity_fini
 			 * eventually
 			 */
 			s_entity->stopped = true;
+		}
 		spin_unlock(&rq->lock);
 		kfree(sched->sched_rq[i]);
 	}
Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH 2/2 drm/sched: clarify the documentation on drm_sched_entity_error

Sima requested that in a discussion, just copy&paste my explanation from the mail.

---
 drivers/gpu/drm/scheduler/sched_entity.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +15 -2
index 58c8161289fe..571e2f2365a1 100644
@@ -166,8 +166,21 @@ bool drm_sched_entity_is_ready(struct drm_sched_entity *entity)
  * drm_sched_entity_error - return error of last scheduled job
  * @entity: scheduler entity to check
  *
- * Opportunistically return the error of the last scheduled job. Result can
- * change any time when new jobs are pushed to the hw.
+ * Drivers should use this function in two ways:
+ *
+ * 1. In it's prepare callback so that when one submission fails all following
+ * from the same ctx are marked with an error number as well.
+ *
+ * This is intentionally done in a driver callback so that driver decides if
+ * they want subsequent submissions to fail or not. That can be helpful for
+ * example for in kernel paging queues where submissions don't depend on each
+ * other and a failed submission shouldn't cancel all following.
+ *
+ * 2. In it's submission IOCTL to reject new submissions and inform userspace
+ * that it needs to kick of some error handling.
+ *
+ * Returns the error of the last scheduled job. Result can change any time when
+ * new jobs are pushed to the hw.
  */
 int drm_sched_entity_error(struct drm_sched_entity *entity)
 {
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 2/2] drm/sched: clarify the documentation on drm_sched_entity_error

Quoted 15 lines · Christian König

Sima requested that in a discussion, just copy&paste my explanation from
the mail.

---
 drivers/gpu/drm/scheduler/sched_entity.c | 17 +++++++++++++++--

 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +0 -0
index 58c8161289fe..571e2f2365a1 100644

drivers/gpu/drm/scheduler/sched_entity.c bool drm_sched_entity_is_ready(struct · line 168

Quoted 3 lines
drivers/gpu/drm/scheduler/sched_entity.c +0 -0
@@ -166,8 +166,21 @@ bool drm_sched_entity_is_ready(struct
 drm_sched_entity *entity)
   * drm_sched_entity_error - return error of last scheduled job
   * @entity: scheduler entity to check

Please move the "Returns:" section to here, so we get a unified style for the scheduler.

"Returns: the negative error code of the last scheduled job, 0 if there has been no error, or no job yet."

Regarding your sentence from below:

Quoted 3 lines

"Returns the error of the last scheduled job. Result can change any time when
+ * new jobs are pushed to the hw.

what does the last sentence mean? It seems you just want to say that each time the function is called it can return a new error code?

IMO it should be relatively obvious that this function is racy. If you want to emphasize it nevertheless I'd do that in a sentence separate from the "Returns: " section

Quoted 5 lines

  *
- * Opportunistically return the error of the last scheduled job. Result can
- * change any time when new jobs are pushed to the hw.
+ * Drivers should use this function in two ways:

maybe "in one of two ways" ?

Quoted 3 lines

+ *
+ * 1. In it's prepare callback so that when one submission fails all following

s/it's/their

Quoted 4 lines

+ * from the same ctx are marked with an error number as well.
+ *
+ * This is intentionally done in a driver callback so that driver decides if

s/so that driver/so that the driver

Quoted 3 lines

+ * they want subsequent submissions to fail or not. That can be helpful for
+ * example for in kernel paging queues where submissions don't

s/for in kernel/in kernel

Quoted 5 lines

depend on each
+ * other and a failed submission shouldn't cancel all following.
+ *
+ * 2. In it's submission IOCTL to reject new submissions and inform userspace

s/In it's/In their

Quoted 8 lines

+ * that it needs to kick of some error handling.
+ *
+ * Returns the error of the last scheduled job. Result can change any time when
+ * new jobs are pushed to the hw.
  */
 int drm_sched_entity_error(struct drm_sched_entity *entity) {

btw I talked to Dave about the other patch. I think if you provide a v2 with Janis suggestions and mine here we can merge this

Danke,
P.

#3 lore raw
In reply to Christian König
Quoted 20 lines · Christian König

Tearing down the scheduler with jobs still on the pending list can lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

When there are still entities with jobs the situation is even worse since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well let them run into a BUG_ON().

---
 drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_fini(struct drm_gpu_scheduler *sched) · line 1340

Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +5 -0
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
 
 	drm_sched_wqueue_stop(sched);
 
+	/*
+	 * Tearing down the scheduler wile there are still unprocessed jobs can
+	 * lead to use after free issues in the scheduler fence.
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));

drm_WARN_ON(sched->dev, ...) would identify the device, which I presume would be helpful in multi-GPU systems.

BR,
Jani.

Quoted 27 lines
drivers/gpu/drm/scheduler/sched_main.c +13 -1
+
 	for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
 		struct drm_sched_rq *rq = sched->sched_rq[i];
 
 		spin_lock(&rq->lock);
-		list_for_each_entry(s_entity, &rq->entities, list)
+		list_for_each_entry(s_entity, &rq->entities, list) {
+			/*
+			 * The justification for this BUG_ON() is that tearing
+			 * down the scheduler while jobs are pending leaves
+			 * dma_fences unsignaled. Since we have dependencies
+			 * from the core memory management to eventually signal
+			 * dma_fences this can trivially lead to a system wide
+			 * stop because of a locked up memory management.
+			 */
+			BUG_ON(spsc_queue_count(&s_entity->job_queue));
+
 			/*
 			 * Prevents reinsertion and marks job_queue as idle,
 			 * it will removed from rq in drm_sched_entity_fini
 			 * eventually
 			 */
 			s_entity->stopped = true;
+		}
 		spin_unlock(&rq->lock);
 		kfree(sched->sched_rq[i]);
 	}
Signature · 1 line
Jani Nikula, Intel
author reviewer
#4 lore raw

Am 18.09.24 um 16:41 schrieb Jani Nikula:

Quoted 24 lines · Christian König
Quoted 23 lines

Tearing down the scheduler with jobs still on the pending list can lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

When there are still entities with jobs the situation is even worse since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well let them run into a BUG_ON().

---
  drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-
  1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
 
  	drm_sched_wqueue_stop(sched);
 
Quoted 7 lines

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_fini(struct drm_gpu_scheduler *sched) · line 1340

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +5 -0
+	/*
+	 * Tearing down the scheduler wile there are still unprocessed jobs can
+	 * lead to use after free issues in the scheduler fence.
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));

drm_WARN_ON(sched->dev, ...) would identify the device, which I presume would be helpful in multi-GPU systems.

Good point, going to fix that.

Regards,
Christian.

Quoted 31 lines

BR,
Jani.

Quoted 27 lines
drivers/gpu/drm/scheduler/sched_main.c +13 -1
+
  	for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
  		struct drm_sched_rq *rq = sched->sched_rq[i];
 
  		spin_lock(&rq->lock);
-		list_for_each_entry(s_entity, &rq->entities, list)
+		list_for_each_entry(s_entity, &rq->entities, list) {
+			/*
+			 * The justification for this BUG_ON() is that tearing
+			 * down the scheduler while jobs are pending leaves
+			 * dma_fences unsignaled. Since we have dependencies
+			 * from the core memory management to eventually signal
+			 * dma_fences this can trivially lead to a system wide
+			 * stop because of a locked up memory management.
+			 */
+			BUG_ON(spsc_queue_count(&s_entity->job_queue));
+
  			/*
  			 * Prevents reinsertion and marks job_queue as idle,
  			 * it will removed from rq in drm_sched_entity_fini
  			 * eventually
  			 */
  			s_entity->stopped = true;
+		}
  		spin_unlock(&rq->lock);
  		kfree(sched->sched_rq[i]);
  	}
#5 lore raw
In reply to Christian König
Quoted 3 lines · Christian König

Tearing down the scheduler with jobs still on the pending list can lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

Did you have time yet to look into my proposed waitque-solution?

Quoted 18 lines

When there are still entities with jobs the situation is even worse since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well let them run into a BUG_ON().

---
 drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-

 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_fini(struct drm_gpu_scheduler · line 1333

Quoted 1 line
drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct drm_gpu_scheduler
 *sched)

I agree with Sima that it should first be documented in the function's docstring what the user is expected to have done before calling the function.

P.

Quoted 46 lines


  drm_sched_wqueue_stop(sched);

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	/*
+	 * Tearing down the scheduler wile there are still

unprocessed jobs can

drivers/gpu/drm/scheduler/sched_main.c +4 -0
+	 * lead to use after free issues in the scheduler fence.
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+

  for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
  struct drm_sched_rq *rq = sched->sched_rq[i];
  spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +3 -1
-		list_for_each_entry(s_entity, &rq->entities, list)
+		list_for_each_entry(s_entity, &rq->entities, list) {
+			/*
+			 * The justification for this BUG_ON() is

that tearing

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * down the scheduler while jobs are pending

leaves

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences unsignaled. Since we have

dependencies

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * from the core memory management to

eventually signal

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences this can trivially lead to a

system wide

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * stop because of a locked up memory

management.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			 */
+			BUG_ON(spsc_queue_count(&s_entity-
Quoted 1 line

job_queue));

+
  /*
  * Prevents reinsertion and marks job_queue as idle,
  * it will removed from rq in drm_sched_entity_fini
  * eventually
  */
  s_entity->stopped = true;

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		}

  spin_unlock(&rq->lock);
  kfree(sched->sched_rq[i]);
  }

author reviewer
#6 lore raw

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 5 lines · Christian König
Quoted 3 lines

Tearing down the scheduler with jobs still on the pending list can lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

Did you have time yet to look into my proposed waitque-solution?

I don't remember seeing anything. What have I missed?

Quoted 12 lines
Quoted 12 lines

When there are still entities with jobs the situation is even worse since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well let them run into a BUG_ON().

---
  drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-
  1 file changed, 18 insertions(+), 1 deletion(-)
Quoted 10 lines

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_fini(struct drm_gpu_scheduler · line 1333

Quoted 7 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct drm_gpu_scheduler
 *sched)

I agree with Sima that it should first be documented in the function's docstring what the user is expected to have done before calling the function.

Good point, going to update the documentation as well.

Thanks,
Christian.

Quoted 49 lines

P.

Quoted 46 lines
drivers/gpu/drm/scheduler/sched_main.c +2 -0
  	drm_sched_wqueue_stop(sched);
 
+	/*
+	 * Tearing down the scheduler wile there are still

unprocessed jobs can

drivers/gpu/drm/scheduler/sched_main.c +4 -0
+	 * lead to use after free issues in the scheduler fence.
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+
  	for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++)

{
  struct drm_sched_rq *rq = sched->sched_rq[i];

  spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +3 -1
-		list_for_each_entry(s_entity, &rq->entities, list)
+		list_for_each_entry(s_entity, &rq->entities, list) {
+			/*
+			 * The justification for this BUG_ON() is

that tearing

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * down the scheduler while jobs are pending

leaves

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences unsignaled. Since we have

dependencies

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * from the core memory management to

eventually signal

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences this can trivially lead to a

system wide

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * stop because of a locked up memory

management.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			 */
+			BUG_ON(spsc_queue_count(&s_entity-
Quoted 1 line

job_queue));

+
  /*
  * Prevents reinsertion and marks job_queue
as idle,
  * it will removed from rq in
drm_sched_entity_fini
  * eventually
  */
  s_entity->stopped = true;

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		}
  		spin_unlock(&rq->lock);
  		kfree(sched->sched_rq[i]);
  	}
#7 lore raw
Quoted 9 lines · Christian König

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 6 lines · Christian König
Quoted 4 lines

Tearing down the scheduler with jobs still on the pending list can
lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

Did you have time yet to look into my proposed waitque-solution?

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Quoted 28 lines
Quoted 15 lines
Quoted 15 lines

When there are still entities with jobs the situation is even worse
since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well
let them run into a BUG_ON().

---
  drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-

  1 file changed, 18 insertions(+), 1 deletion(-)
Quoted 12 lines
Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in the function's
docstring what the user is expected to have done before calling the function.

Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to be done before _fini() is mentioned.

One could also think about providing a hint at how the driver can do that. AFAICS the only way for the driver to ensure that is to maintain its own, separate list of submitted jobs.

P.

Quoted 61 lines

Thanks,
Christian.

Quoted 56 lines

P.

Quoted 53 lines


   drm_sched_wqueue_stop(sched);

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	/*
+	 * Tearing down the scheduler wile there are still

unprocessed jobs can

drivers/gpu/drm/scheduler/sched_main.c +4 -0
+	 * lead to use after free issues in the scheduler fence.
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+

   for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++)
{
   struct drm_sched_rq *rq = sched->sched_rq[i];
   spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-		list_for_each_entry(s_entity, &rq->entities,

list)

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		list_for_each_entry(s_entity, &rq->entities,

list) {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			/*
+			 * The justification for this BUG_ON()

is
that tearing

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * down the scheduler while jobs are

pending
leaves

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences unsignaled. Since we have

dependencies

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * from the core memory management to

eventually signal

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences this can trivially lead to

a
system wide

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * stop because of a locked up memory

management.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			 */
+			BUG_ON(spsc_queue_count(&s_entity-
Quoted 1 line

job_queue));

+
   /*
   * Prevents reinsertion and marks job_queue
as idle,
   * it will removed from rq in drm_sched_entity_fini
   * eventually
   */
   s_entity->stopped = true;

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		}

   spin_unlock(&rq->lock);
   kfree(sched->sched_rq[i]);
   }

author reviewer
#8 lore raw

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 10 lines · Christian König
Quoted 8 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 6 lines · Christian König
Quoted 4 lines

Tearing down the scheduler with jobs still on the pending list can
lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

Did you have time yet to look into my proposed waitque-solution?

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Interesting approach, I'm just not sure if we can or should wait in drm_sched_fini().

Probably better to make that a separate function, something like drm_sched_flush() or similar.

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 19 lines

When there are still entities with jobs the situation is even worse
since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well
let them run into a BUG_ON().

---
   drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-
   1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct
Quoted 2 lines

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in the function's
docstring what the user is expected to have done before calling the function.

Quoted 8 lines
Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to be done before _fini() is mentioned.

One could also think about providing a hint at how the driver can do that. AFAICS the only way for the driver to ensure that is to maintain its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do this fully cleanly.

The problem is that the dma_fence object gives no guarantee when callbacks are processed, e.g. they can be both processed from interrupt context as well as from a CPU which called dma_fence_is_signaled().

So when a driver (or drm_sched_fini) waits for the last submitted fence it actually can be that the drm_sched object still needs to do some processing. See the hack in amdgpu_vm_tlb_seq() for more background on the problem.

Regards,
Christian.

Quoted 61 lines

P.

Quoted 58 lines

Thanks,
Christian.

Quoted 55 lines

P.

Quoted 53 lines

   drm_sched_wqueue_stop(sched);

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	/*
+	 * Tearing down the scheduler wile there are still

unprocessed jobs can

drivers/gpu/drm/scheduler/sched_main.c +4 -0
+	 * lead to use after free issues in the scheduler fence.
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+
   	for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs;

i++)
{
   struct drm_sched_rq *rq = sched->sched_rq[i];

   spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-		list_for_each_entry(s_entity, &rq->entities,

list)

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		list_for_each_entry(s_entity, &rq->entities,

list) {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			/*
+			 * The justification for this BUG_ON()

is
that tearing

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * down the scheduler while jobs are

pending
leaves

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences unsignaled. Since we have

dependencies

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * from the core memory management to

eventually signal

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences this can trivially lead to

a
system wide

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * stop because of a locked up memory

management.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			 */
+			BUG_ON(spsc_queue_count(&s_entity-
Quoted 1 line

job_queue));

+
   /*
   * Prevents reinsertion and marks
job_queue
as idle,
   * it will removed from rq in
drm_sched_entity_fini
   * eventually
   */
   s_entity->stopped = true;

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		}
   		spin_unlock(&rq->lock);
   		kfree(sched->sched_rq[i]);
   	}
#9 lore raw
Quoted 12 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 10 lines · Christian König
Quoted 8 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 6 lines · Christian König
Quoted 4 lines

Tearing down the scheduler with jobs still on the pending list can
lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

Did you have time yet to look into my proposed waitque-solution?

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

Mhm, I didn't got that in my inbox for some reason.

Interesting approach, I'm just not sure if we can or should wait in drm_sched_fini().

Probably better to make that a separate function, something like drm_sched_flush() or similar.

Yeah I don't think we should smash this into drm_sched_fini unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because drm_sched_fini is called long after all the entities are gone in drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably want to nuke the context by setting the tdr timeout to zero (or maybe just as long as context preemption takes to be efficient), and relying on the normal gpu reset flow to handle things. drm_sched_entity_flush kinda does this, except not really and it's a lot more focused on the ringbuffer context. So maybe we want a new drm_sched_entity_kill.

For this case calling drm_sched_fini() after the 1:1 entity is gone should not find any linger jobs, it would actually be a bug somewhere if there's a job lingering. Maybe a sanity check that there's not just no jobs lingering, but also no entity left would be good here?

- fw scheduler without preemption support. There we kinda need the drm_sched_flush, except blocking in fops->close is not great. So instead I think the following is better:
1. drm_sched_entity_stopped, which only stops new submissions (for paranoia) but doesn't tear down the entity
2. drm_dev_get
3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b) drm_sched_entity_fini
+ drm_sched_fini c) drm_dev_put

Note that semantically this implements the refcount in the other path from Phillip:

https://lore.kernel.org/all/[email protected]/

Except it doesn't impose refcount on everyone else who doesn't need it, and it doesn't even impose refcounting on drivers that do need it because we use drm_sched_flush and a worker to achieve the same.

Essentially helper functions for the common use-cases instead of trying to solve them all by putting drm_sched_flush as a potentially very blocking function into drm_sched_fini.

Quoted 37 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 19 lines

When there are still entities with jobs the situation is even worse
since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well
let them run into a BUG_ON().

---
   drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-
   1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct
Quoted 2 lines

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in the function's
docstring what the user is expected to have done before calling the function.

Quoted 8 lines
Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to be done before _fini() is mentioned.

One could also think about providing a hint at how the driver can do that. AFAICS the only way for the driver to ensure that is to maintain its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do this fully cleanly.

Quoted 7 lines

The problem is that the dma_fence object gives no guarantee when callbacks are processed, e.g. they can be both processed from interrupt context as well as from a CPU which called dma_fence_is_signaled().

So when a driver (or drm_sched_fini) waits for the last submitted fence it actually can be that the drm_sched object still needs to do some processing. See the hack in amdgpu_vm_tlb_seq() for more background on the problem.

So I thought this should be fairly easy because of the sched hw/public fence split: If we wait for both all jobs to finish and for all the sched work/tdr work to finish, and we make sure there's no entity existing that's not yet stopped we should catch them all? Or at least I think it's a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing then I think that's just a separate concern and we can ignore that here since the fences themselves will till get rcu-delay freed even if drm_sched_fini has finished.
-Sima

Quoted 62 lines

Regards,
Christian.

Quoted 58 lines

P.

Quoted 55 lines

Thanks,
Christian.

Quoted 52 lines

P.

Quoted 50 lines

   drm_sched_wqueue_stop(sched);

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	/*
+	 * Tearing down the scheduler wile there are still

unprocessed jobs can

drivers/gpu/drm/scheduler/sched_main.c +4 -0
+	 * lead to use after free issues in the scheduler fence.
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+
   	for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs;

i++)
{
   struct drm_sched_rq *rq = sched->sched_rq[i]; spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-		list_for_each_entry(s_entity, &rq->entities,

list)

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		list_for_each_entry(s_entity, &rq->entities,

list) {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			/*
+			 * The justification for this BUG_ON()

is
that tearing

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * down the scheduler while jobs are

pending
leaves

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences unsignaled. Since we have

dependencies

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * from the core memory management to

eventually signal

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences this can trivially lead to

a
system wide

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * stop because of a locked up memory

management.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			 */
+			BUG_ON(spsc_queue_count(&s_entity-
Quoted 1 line

job_queue));

+
   /*
   * Prevents reinsertion and marks
job_queue
as idle,
   * it will removed from rq in
drm_sched_entity_fini
   * eventually
   */
   s_entity->stopped = true;

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		}
   		spin_unlock(&rq->lock);
   		kfree(sched->sched_rq[i]);
   	}
Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#10 lore raw
Quoted 13 lines · Simona Vetter
Quoted 12 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 11 lines · Christian König
Quoted 10 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 9 lines · Christian König
Quoted 6 lines

Tearing down the scheduler with jobs still on the pending list
can
lead to use after free issues. Add a warning if drivers try to
destroy a scheduler which still has work pushed to the HW.

Did you have time yet to look into my proposed waitque- solution?

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 1 line

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Interesting approach, I'm just not sure if we can or should wait in drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts is always a bug, right?

If so, what are the disadvantages of waiting in drm_sched_fini()? We could block buggy drivers as I see it. Which wouldn't be good, but could then be fixed on drivers' site.

Quoted 3 lines
Quoted 3 lines

Probably better to make that a separate function, something like drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers which are not sure whether all jobs are done before they start tearing down.

Quoted 19 lines

Yeah I don't think we should smash this into drm_sched_fini unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because drm_sched_fini is called long after all the entities are gone in drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably want to
  nuke the context by setting the tdr timeout to zero (or maybe just as
  long as context preemption takes to be efficient), and relying on the
  normal gpu reset flow to handle things. drm_sched_entity_flush kinda
  does this, except not really and it's a lot more focused on the ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 6 lines

  For this case calling drm_sched_fini() after the 1:1 entity is gone should not find any linger jobs, it would actually be a bug somewhere if
  there's a job lingering. Maybe a sanity check that there's not just no
  jobs lingering, but also no entity left would be good here?

The check for lingering ones is in Christian's patch here IISC. At which position would you imagine the check for the entity being performed?

Quoted 7 lines

- fw scheduler without preemption support. There we kinda need the drm_sched_flush, except blocking in fops->close is not great. So instead
  I think the following is better:
  1. drm_sched_entity_stopped, which only stops new submissions (for paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting new jobs from userspace to their entities, couldn't they?

Quoted 10 lines

  2. drm_dev_get
  3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b) drm_sched_entity_fini
  + drm_sched_fini c) drm_dev_put

  Note that semantically this implements the refcount in the other path
  from Phillip:

Quoted 7 lines


https://lore.kernel.org/all/[email protected]/

  Except it doesn't impose refcount on everyone else who doesn't need it,
  and it doesn't even impose refcounting on drivers that do need it because we use drm_sched_flush and a worker to achieve the same.

I indeed wasn't happy with the refcount approach for that reason, agreed.

Quoted 6 lines

Essentially helper functions for the common use-cases instead of trying to
solve them all by putting drm_sched_flush as a potentially very blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired – as far as I can see, it is only invoked on driver teardown, so not during active operation. Teardown doesn't happen that often, and it can (if implemented correctly) only block until the driver's code has signaled the last fences. If that doesn't happen, the block would reveal a bug.

But don't get me wrong: I don't want to *push* this solution. I just want to understand when it could become a problem.

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable step towards the right direction?

Quoted 55 lines
Quoted 55 lines
Quoted 39 lines
Quoted 33 lines
Quoted 20 lines
Quoted 20 lines

When there are still entities with jobs the situation is even
worse
since the dma_fences for those jobs can never signal we can just
choose between potentially locking up core memory management and
random memory corruption. When drivers really mess it up that
well
let them run into a BUG_ON().

---
   drivers/gpu/drm/scheduler/sched_main.c | 19
++++++++++++++++++-

   1 file changed, 18 insertions(+), 1 deletion(-)
Quoted 13 lines
Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in the function's
docstring what the user is expected to have done before calling the
function.

Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to be done
before _fini() is mentioned.

Quoted 5 lines

One could also think about providing a hint at how the driver can do
that. AFAICS the only way for the driver to ensure that is to maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee when callbacks
are processed, e.g. they can be both processed from interrupt context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 6 lines
Quoted 6 lines

So when a driver (or drm_sched_fini) waits for the last submitted fence it
actually can be that the drm_sched object still needs to do some processing.
See the hack in amdgpu_vm_tlb_seq() for more background on the problem.

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 93 lines

So I thought this should be fairly easy because of the sched hw/public
fence split: If we wait for both all jobs to finish and for all the sched
work/tdr work to finish, and we make sure there's no entity existing that's not yet stopped we should catch them all? Or at least I think it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing then I
think that's just a separate concern and we can ignore that here since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 73 lines

Regards,
Christian.

Quoted 69 lines

P.

Quoted 66 lines

Thanks,
Christian.

Quoted 63 lines

P.

Quoted 61 lines

    drm_sched_wqueue_stop(sched);

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	/*
+	 * Tearing down the scheduler wile there are still

unprocessed jobs can

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+	 * lead to use after free issues in the scheduler

fence.

drivers/gpu/drm/scheduler/sched_main.c +3 -0
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+

    for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched-

Quoted 1 line

num_rqs;

i++)
{
    struct drm_sched_rq *rq = sched-

Quoted 1 line

sched_rq[i];

    spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-		list_for_each_entry(s_entity, &rq-
Quoted 1 line

entities,

list)

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		list_for_each_entry(s_entity, &rq-
Quoted 1 line

entities,

list) {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			/*
+			 * The justification for this

BUG_ON()
is
that tearing

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * down the scheduler while jobs

are
pending
leaves

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences unsignaled. Since we

have
dependencies

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * from the core memory management

to
eventually signal

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences this can trivially

lead to
a
system wide

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * stop because of a locked up

memory
management.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			 */
+			BUG_ON(spsc_queue_count(&s_entity-
Quoted 1 line

job_queue));

+
    /*
    * Prevents reinsertion and marks job_queue
as idle,
    * it will removed from rq in drm_sched_entity_fini
    * eventually
    */
    s_entity->stopped = true;

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		}

    spin_unlock(&rq->lock);
    kfree(sched->sched_rq[i]);
    }

author reviewer
#11 lore raw

Am 25.09.24 um 16:53 schrieb Philipp Stanner:

Quoted 14 lines · Simona Vetter
Quoted 13 lines · Christian König
Quoted 12 lines

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 11 lines · Christian König
Quoted 10 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 9 lines · Christian König
Quoted 6 lines

Tearing down the scheduler with jobs still on the pending list
can
lead to use after free issues. Add a warning if drivers try to
destroy a scheduler which still has work pushed to the HW.

Did you have time yet to look into my proposed waitque- solution?

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Interesting approach, I'm just not sure if we can or should wait in drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts is always a bug, right?

Correct, the question is how to avoid that.

Quoted 3 lines

If so, what are the disadvantages of waiting in drm_sched_fini()? We could block buggy drivers as I see it. Which wouldn't be good, but could then be fixed on drivers' site.

Sima explained that pretty well: Don't block in fops->close, do that in fops->flush instead.

One issue this solves is that when you send a SIGTERM the tear down handling first flushes all the FDs and then closes them.

So if flushing the FDs blocks because the process initiated sending a terabyte of data over a 300bps line (for example) you can still throw a SIGKILL and abort that as well.

If you would block in fops-close() that SIGKILL won't have any effect any more because by the time close() is called the process is gone and signals are already blocked.

And yes when I learned about that issue I was also buffed that handling like this in the UNIX design is nearly 50 years old and still applies to today.

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

Probably better to make that a separate function, something like drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers which are not sure whether all jobs are done before they start tearing down.

Yes exactly that's the idea. And give that flush function a return code so that it can return -EINTR.

Quoted 69 lines
Quoted 18 lines

Yeah I don't think we should smash this into drm_sched_fini unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because drm_sched_fini is called long after all the entities are gone in drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably want to
  nuke the context by setting the tdr timeout to zero (or maybe just
as
  long as context preemption takes to be efficient), and relying on
the
  normal gpu reset flow to handle things. drm_sched_entity_flush
kinda
  does this, except not really and it's a lot more focused on the ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 6 lines

  For this case calling drm_sched_fini() after the 1:1 entity is gone should not find any linger jobs, it would actually be a bug
somewhere if
  there's a job lingering. Maybe a sanity check that there's not just
no
  jobs lingering, but also no entity left would be good here?

The check for lingering ones is in Christian's patch here IISC. At which position would you imagine the check for the entity being performed?

Quoted 6 lines

- fw scheduler without preemption support. There we kinda need the drm_sched_flush, except blocking in fops->close is not great. So
instead
  I think the following is better:
  1. drm_sched_entity_stopped, which only stops new submissions (for paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting new jobs from userspace to their entities, couldn't they?

Quoted 11 lines

  2. drm_dev_get
  3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b)
drm_sched_entity_fini
  + drm_sched_fini c) drm_dev_put

  Note that semantically this implements the refcount in the other
path
  from Phillip:

Quoted 6 lines

https://lore.kernel.org/all/[email protected]/

  Except it doesn't impose refcount on everyone else who doesn't need
it,
  and it doesn't even impose refcounting on drivers that do need it because we use drm_sched_flush and a worker to achieve the same.

I indeed wasn't happy with the refcount approach for that reason, agreed.

Quoted 5 lines

Essentially helper functions for the common use-cases instead of trying to
solve them all by putting drm_sched_flush as a potentially very blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired – as far as I can see, it is only invoked on driver teardown, so not during active operation. Teardown doesn't happen that often, and it can (if implemented correctly) only block until the driver's code has signaled the last fences. If that doesn't happen, the block would reveal a bug.

Quoted 7 lines

But don't get me wrong: I don't want to *push* this solution. I just want to understand when it could become a problem.

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable step towards the right direction?

I think that this is the right thing to do, yes.

Quoted 58 lines
Quoted 52 lines
Quoted 52 lines
Quoted 37 lines
Quoted 31 lines
Quoted 18 lines
Quoted 18 lines

When there are still entities with jobs the situation is even
worse
since the dma_fences for those jobs can never signal we can just
choose between potentially locking up core memory management and
random memory corruption. When drivers really mess it up that
well
let them run into a BUG_ON().

---
    drivers/gpu/drm/scheduler/sched_main.c | 19
++++++++++++++++++-
    1 file changed, 18 insertions(+), 1 deletion(-)
Quoted 13 lines
Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in the function's
docstring what the user is expected to have done before calling the
function.

Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to be done
before _fini() is mentioned.

Quoted 5 lines

One could also think about providing a hint at how the driver can do
that. AFAICS the only way for the driver to ensure that is to maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee when callbacks
are processed, e.g. they can be both processed from interrupt context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 6 lines
Quoted 6 lines

So when a driver (or drm_sched_fini) waits for the last submitted fence it
actually can be that the drm_sched object still needs to do some processing.
See the hack in amdgpu_vm_tlb_seq() for more background on the problem.

Quoted 13 lines

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 6 lines

So I thought this should be fairly easy because of the sched hw/public
fence split: If we wait for both all jobs to finish and for all the sched
work/tdr work to finish, and we make sure there's no entity existing that's not yet stopped we should catch them all?

Unfortunately not.

Even when you do a dma_fence_wait() on the last submission it can still be that another CPU is executing the callbacks to wake up the scheduler work item and cleanup the job.

That's one of the reasons why I think the design of keeping the job alive is so extremely awkward. The dma_fence as representation of the hw submission has a much better defined state machine and lifetime.

Regards,
Christian.

Quoted 84 lines
Quoted 84 lines

Or at least I think
it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing then I
think that's just a separate concern and we can ignore that here since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 71 lines

Regards,
Christian.

Quoted 68 lines

P.

Quoted 66 lines

Thanks,
Christian.

Quoted 63 lines

P.

Quoted 61 lines

    drm_sched_wqueue_stop(sched);

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	/*
+	 * Tearing down the scheduler wile there are still

unprocessed jobs can

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+	 * lead to use after free issues in the scheduler

fence.

drivers/gpu/drm/scheduler/sched_main.c +3 -0
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+
    	for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched-
Quoted 1 line

num_rqs;

i++)
{
    struct drm_sched_rq *rq = sched-

Quoted 1 line

sched_rq[i];

    spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-		list_for_each_entry(s_entity, &rq-
Quoted 1 line

entities,

list)

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		list_for_each_entry(s_entity, &rq-
Quoted 1 line

entities,

list) {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			/*
+			 * The justification for this

BUG_ON()
is
that tearing

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * down the scheduler while jobs

are
pending
leaves

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences unsignaled. Since we

have
dependencies

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * from the core memory management

to
eventually signal

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences this can trivially

lead to
a
system wide

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * stop because of a locked up

memory
management.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			 */
+			BUG_ON(spsc_queue_count(&s_entity-
Quoted 1 line

job_queue));

+
    /*
    * Prevents reinsertion and marks
job_queue
as idle,
    * it will removed from rq in
drm_sched_entity_fini
    * eventually
    */
    s_entity->stopped = true;

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		}
    		spin_unlock(&rq->lock);
    		kfree(sched->sched_rq[i]);
    	}
maintainer
#12 lore raw
Quoted 24 lines · Christian König

Am 25.09.24 um 16:53 schrieb Philipp Stanner:

Quoted 14 lines · Simona Vetter
Quoted 13 lines · Christian König
Quoted 12 lines

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 11 lines · Christian König
Quoted 10 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 9 lines · Christian König
Quoted 6 lines

Tearing down the scheduler with jobs still on the pending list
can
lead to use after free issues. Add a warning if drivers try to
destroy a scheduler which still has work pushed to the HW.

Did you have time yet to look into my proposed waitque- solution?

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Interesting approach, I'm just not sure if we can or should wait in drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts is always a bug, right?

Quoted 8 lines

Correct, the question is how to avoid that.

Quoted 3 lines

If so, what are the disadvantages of waiting in drm_sched_fini()? We could block buggy drivers as I see it. Which wouldn't be good, but could then be fixed on drivers' site.

Sima explained that pretty well: Don't block in fops->close, do that in fops->flush instead.

I agree that we shouldn't block in close(), but this effectively means that we need to reference count the scheduler, right?

Otherwise, if we allow to just skip / interrupt the teardown, we can still leak memory.

Quoted 268 lines

One issue this solves is that when you send a SIGTERM the tear down handling first flushes all the FDs and then closes them.

So if flushing the FDs blocks because the process initiated sending a terabyte of data over a 300bps line (for example) you can still throw a SIGKILL and abort that as well.

If you would block in fops-close() that SIGKILL won't have any effect any more because by the time close() is called the process is gone and signals are already blocked.

And yes when I learned about that issue I was also buffed that handling like this in the UNIX design is nearly 50 years old and still applies to today.

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

Probably better to make that a separate function, something like drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers which are not sure whether all jobs are done before they start tearing down.

Yes exactly that's the idea. And give that flush function a return code so that it can return -EINTR.

Quoted 67 lines
Quoted 18 lines

Yeah I don't think we should smash this into drm_sched_fini unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because drm_sched_fini is called long after all the entities are gone in drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably want to
  nuke the context by setting the tdr timeout to zero (or maybe just
as
  long as context preemption takes to be efficient), and relying on
the
  normal gpu reset flow to handle things. drm_sched_entity_flush
kinda
  does this, except not really and it's a lot more focused on the ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 6 lines

  For this case calling drm_sched_fini() after the 1:1 entity is gone should not find any linger jobs, it would actually be a bug
somewhere if
  there's a job lingering. Maybe a sanity check that there's not just
no
  jobs lingering, but also no entity left would be good here?

The check for lingering ones is in Christian's patch here IISC. At which position would you imagine the check for the entity being performed?

Quoted 6 lines

- fw scheduler without preemption support. There we kinda need the drm_sched_flush, except blocking in fops->close is not great. So
instead
  I think the following is better:
  1. drm_sched_entity_stopped, which only stops new submissions (for paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting new jobs from userspace to their entities, couldn't they?

Quoted 10 lines

  2. drm_dev_get
  3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b)
drm_sched_entity_fini
  + drm_sched_fini c) drm_dev_put

  Note that semantically this implements the refcount in the other
path
  from Phillip:

Quoted 5 lines

https://lore.kernel.org/all/[email protected]/
  Except it doesn't impose refcount on everyone else who doesn't need
it,
  and it doesn't even impose refcounting on drivers that do need it because we use drm_sched_flush and a worker to achieve the same.

I indeed wasn't happy with the refcount approach for that reason, agreed.

Quoted 5 lines

Essentially helper functions for the common use-cases instead of trying to
solve them all by putting drm_sched_flush as a potentially very blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired – as far as I can see, it is only invoked on driver teardown, so not during active operation. Teardown doesn't happen that often, and it can (if implemented correctly) only block until the driver's code has signaled the last fences. If that doesn't happen, the block would reveal a bug.

Quoted 7 lines

But don't get me wrong: I don't want to *push* this solution. I just want to understand when it could become a problem.

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable step towards the right direction?

I think that this is the right thing to do, yes.

Quoted 58 lines
Quoted 52 lines
Quoted 52 lines
Quoted 37 lines
Quoted 31 lines
Quoted 18 lines
Quoted 18 lines

When there are still entities with jobs the situation is even
worse
since the dma_fences for those jobs can never signal we can just
choose between potentially locking up core memory management and
random memory corruption. When drivers really mess it up that
well
let them run into a BUG_ON().

---
    drivers/gpu/drm/scheduler/sched_main.c | 19
++++++++++++++++++-
    1 file changed, 18 insertions(+), 1 deletion(-)
Quoted 13 lines
Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in the function's
docstring what the user is expected to have done before calling the
function.

Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to be done
before _fini() is mentioned.

Quoted 5 lines

One could also think about providing a hint at how the driver can do
that. AFAICS the only way for the driver to ensure that is to maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee when callbacks
are processed, e.g. they can be both processed from interrupt context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 6 lines
Quoted 6 lines

So when a driver (or drm_sched_fini) waits for the last submitted fence it
actually can be that the drm_sched object still needs to do some processing.
See the hack in amdgpu_vm_tlb_seq() for more background on the problem.

Quoted 13 lines

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 6 lines

So I thought this should be fairly easy because of the sched hw/public
fence split: If we wait for both all jobs to finish and for all the sched
work/tdr work to finish, and we make sure there's no entity existing that's not yet stopped we should catch them all?

Unfortunately not.

Even when you do a dma_fence_wait() on the last submission it can still be that another CPU is executing the callbacks to wake up the scheduler work item and cleanup the job.

That's one of the reasons why I think the design of keeping the job alive is so extremely awkward. The dma_fence as representation of the hw submission has a much better defined state machine and lifetime.

Regards,
Christian.

Quoted 84 lines
Quoted 84 lines

Or at least I think
it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing then I
think that's just a separate concern and we can ignore that here since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 71 lines

Regards,
Christian.

Quoted 68 lines

P.

Quoted 66 lines

Thanks,
Christian.

Quoted 63 lines

P.

Quoted 61 lines

    drm_sched_wqueue_stop(sched);

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	/*
+	 * Tearing down the scheduler wile there are still

unprocessed jobs can

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+	 * lead to use after free issues in the scheduler

fence.

drivers/gpu/drm/scheduler/sched_main.c +3 -0
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+
    	for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched-
Quoted 1 line

num_rqs;

i++)
{
    struct drm_sched_rq *rq = sched-

Quoted 1 line

sched_rq[i];

    spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-		list_for_each_entry(s_entity, &rq-
Quoted 1 line

entities,

list)

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		list_for_each_entry(s_entity, &rq-
Quoted 1 line

entities,

list) {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			/*
+			 * The justification for this

BUG_ON()
is
that tearing

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * down the scheduler while jobs

are
pending
leaves

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences unsignaled. Since we

have
dependencies

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * from the core memory management

to
eventually signal

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences this can trivially

lead to
a
system wide

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * stop because of a locked up

memory
management.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			 */
+			BUG_ON(spsc_queue_count(&s_entity-
Quoted 1 line

job_queue));

+
    /*
    * Prevents reinsertion and marks
job_queue
as idle,
    * it will removed from rq in
drm_sched_entity_fini
    * eventually
    */
    s_entity->stopped = true;

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		}
    		spin_unlock(&rq->lock);
    		kfree(sched->sched_rq[i]);
    	}
#13 lore raw
Quoted 44 lines · Danilo Krummrich
Quoted 39 lines · Christian König

Am 25.09.24 um 16:53 schrieb Philipp Stanner:

Quoted 21 lines · Simona Vetter
Quoted 20 lines
Quoted 18 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 17 lines · Christian König
Quoted 12 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 11 lines
Quoted 9 lines · Christian König

Tearing down the scheduler with jobs still on the pending
list
can
lead to use after free issues. Add a warning if drivers try
to
destroy a scheduler which still has work pushed to the HW.

Quoted 3 lines
Quoted 2 lines

Did you have time yet to look into my proposed waitque- solution?

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Mhm, I didn't got that in my inbox for some reason.

Interesting approach, I'm just not sure if we can or should wait in
drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts is
always a bug, right?

Correct, the question is how to avoid that.

Quoted 5 lines

If so, what are the disadvantages of waiting in drm_sched_fini()? We
could block buggy drivers as I see it. Which wouldn't be good, but
could then be fixed on drivers' site.

Quoted 3 lines

Sima explained that pretty well: Don't block in fops->close, do that in
fops->flush instead.

Quoted 7 lines

I agree that we shouldn't block in close(), but this effectively means that we
need to reference count the scheduler, right?

Otherwise, if we allow to just skip / interrupt the teardown, we can still leak
memory.

Having thought about it, I agree with Danilo. Having something that shall wait on green light, but can be interrupted, is no guarantee and therefore not a feasible solution.

To break down the solution space, these seem to be our options:
1. We have something (either drm_sched_fini() or a helper, e.g., drm_sched_flush()) that definitely blocks until the pending list has become empty.
2. We have jobs reference-count the scheduler, so the latter can outlive the driver and will be freed some time later.

Can anyone think of a third solution?

Solution #1 has the problem of obviously blocking unconditionally if the driver didn't make sure that all fences will be signaled within reasonable time. In my opinion, this would actually be an advantage, because it will be *very* noticable and force users to repair their driver. The driver *has* to guarantee that all fences will be signaled. If the driver has to do fishy things, having the blocking outsourced to the helper drm_sched_flush() would allow them to circumvent that.

Solution #2 has the problem of backend_ops.free_job() potentially using driver-data after the driver is gone, causing UAF. So with this solutions all drivers would have to be aware of the issue and handle it through one of DRMs primitives dedicated to such problems.

Currently, all drivers either work around the problem internally or simply ignore it, it seems.

So I'd argue that both solutions are an improvement over the existing situation. My preference would be #1.

Opinions?

P.

Quoted 328 lines
Quoted 327 lines

One issue this solves is that when you send a SIGTERM the tear down handling
first flushes all the FDs and then closes them.

So if flushing the FDs blocks because the process initiated sending a
terabyte of data over a 300bps line (for example) you can still throw a
SIGKILL and abort that as well.

If you would block in fops-close() that SIGKILL won't have any effect any
more because by the time close() is called the process is gone and signals
are already blocked.

And yes when I learned about that issue I was also buffed that handling like
this in the UNIX design is nearly 50 years old and still applies to today.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

Probably better to make that a separate function, something like
drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers which
are not sure whether all jobs are done before they start tearing down.

Yes exactly that's the idea. And give that flush function a return code so
that it can return -EINTR.

Quoted 97 lines
Quoted 26 lines

Yeah I don't think we should smash this into drm_sched_fini unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because drm_sched_fini is called long after all the entities are gone in
   drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably
want to
   nuke the context by setting the tdr timeout to zero (or maybe just
as
   long as context preemption takes to be efficient), and relying on
the
   normal gpu reset flow to handle things. drm_sched_entity_flush
kinda
   does this, except not really and it's a lot more focused on the
   ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 8 lines

   For this case calling drm_sched_fini() after the 1:1 entity is gone
   should not find any linger jobs, it would actually be a bug somewhere if
   there's a job lingering. Maybe a sanity check that there's not just
no
   jobs lingering, but also no entity left would be good here?

The check for lingering ones is in Christian's patch here IISC. At which position would you imagine the check for the entity being
performed?

Quoted 9 lines

- fw scheduler without preemption support. There we kinda need the
   drm_sched_flush, except blocking in fops->close is not great. So
instead
   I think the following is better:
   1. drm_sched_entity_stopped, which only stops new submissions (for
   paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting new jobs
from userspace to their entities, couldn't they?

Quoted 11 lines

   2. drm_dev_get
   3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b) drm_sched_entity_fini
   + drm_sched_fini c) drm_dev_put

   Note that semantically this implements the refcount in the other
path
   from Phillip:

Quoted 8 lines

https://lore.kernel.org/all/[email protected]/
   Except it doesn't impose refcount on everyone else who doesn't need
it,
   and it doesn't even impose refcounting on drivers that do need it
   because we use drm_sched_flush and a worker to achieve the same.

I indeed wasn't happy with the refcount approach for that reason, agreed.

Quoted 6 lines

Essentially helper functions for the common use-cases instead of
trying to
solve them all by putting drm_sched_flush as a potentially very blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired – as far
as I can see, it is only invoked on driver teardown, so not during
active operation. Teardown doesn't happen that often, and it can (if
implemented correctly) only block until the driver's code has signaled
the last fences. If that doesn't happen, the block would reveal a bug.

But don't get me wrong: I don't want to *push* this solution. I just
want to understand when it could become a problem.

Quoted 4 lines

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable step
towards the right direction?

I think that this is the right thing to do, yes.

Quoted 70 lines
Quoted 61 lines
Quoted 61 lines
Quoted 43 lines
Quoted 36 lines
Quoted 22 lines
Quoted 22 lines

When there are still entities with jobs the situation is
even
worse
since the dma_fences for those jobs can never signal we can
just
choose between potentially locking up core memory management and
random memory corruption. When drivers really mess it up
that
well
let them run into a BUG_ON().

Signed-off-by: Christian
König<[email protected]>
---
    drivers/gpu/drm/scheduler/sched_main.c | 19
++++++++++++++++++-

    1 file changed, 18 insertions(+), 1 deletion(-)
Quoted 14 lines
Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in the
function's
docstring what the user is expected to have done before calling the
function.

Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to
be done
before _fini() is mentioned.

Quoted 7 lines

One could also think about providing a hint at how the driver can
do
that. AFAICS the only way for the driver to ensure that is to
maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee when
callbacks
are processed, e.g. they can be both processed from interrupt context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 9 lines
Quoted 9 lines

So when a driver (or drm_sched_fini) waits for the last submitted
fence it
actually can be that the drm_sched object still needs to do some
processing.
See the hack in amdgpu_vm_tlb_seq() for more background on the
problem.

Quoted 15 lines

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 8 lines

So I thought this should be fairly easy because of the sched hw/public
fence split: If we wait for both all jobs to finish and for all the
sched
work/tdr work to finish, and we make sure there's no entity existing
that's not yet stopped we should catch them all?

Unfortunately not.

Even when you do a dma_fence_wait() on the last submission it can still be
that another CPU is executing the callbacks to wake up the scheduler work
item and cleanup the job.

That's one of the reasons why I think the design of keeping the job alive is
so extremely awkward. The dma_fence as representation of the hw submission
has a much better defined state machine and lifetime.

Regards,
Christian.

Quoted 86 lines
Quoted 86 lines

  Or at least I think
it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing
then I
think that's just a separate concern and we can ignore that here
since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 71 lines

Regards,
Christian.

Quoted 68 lines

P.

Quoted 66 lines

Thanks,
Christian.

Quoted 63 lines

P.

Quoted 61 lines

     drm_sched_wqueue_stop(sched);
+ /*
+ * Tearing down the scheduler wile there are still unprocessed jobs can
+ * lead to use after free issues in the scheduler fence.
+ */
+ WARN_ON(!list_empty(&sched->pending_list)); +
     for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched-

Quoted 1 line

num_rqs;

i++)
{
     struct drm_sched_rq *rq = sched-

Quoted 1 line

sched_rq[i];

     spin_lock(&rq->lock);
- list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list)
+ list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list) {
+ /*
+ * The justification for this
BUG_ON()
is
that tearing
+ * down the scheduler while jobs
are
pending
leaves
+ * dma_fences unsignaled. Since we
have
dependencies
+ * from the core memory management
to
eventually signal
+ * dma_fences this can trivially
lead to
a
system wide
+ * stop because of a locked up
memory
management.
+ */
+ BUG_ON(spsc_queue_count(&s_entity-

Quoted 1 line

job_queue));

+
     /*
     * Prevents reinsertion and marks
job_queue
as idle,
     * it will removed from rq in
drm_sched_entity_fini
     * eventually
     */
     s_entity->stopped = true;
+ }
     spin_unlock(&rq->lock);
     kfree(sched->sched_rq[i]);
     }

#14 lore raw

Christian, Sima?

Matthew? (+CC)

Opinions on the below?

tl;dr:
I still think it's a good thing to detectably block in drm_sched_fini(), or at the very least drm_sched_flush(), because then you'll find out that the driver is broken and can repair it.

P.

Quoted 458 lines · Philipp Stanner
Quoted 47 lines · Danilo Krummrich
Quoted 42 lines · Christian König

Am 25.09.24 um 16:53 schrieb Philipp Stanner:

Quoted 24 lines · Simona Vetter
Quoted 23 lines
Quoted 21 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 13 lines · Christian König
Quoted 12 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 11 lines
Quoted 9 lines · Christian König

Tearing down the scheduler with jobs still on the pending
list
can
lead to use after free issues. Add a warning if drivers try
to
destroy a scheduler which still has work pushed to the HW.

Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

Did you have time yet to look into my proposed
waitque-
solution?

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

Interesting approach, I'm just not sure if we can or should wait in
drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts
is
always a bug, right?

Correct, the question is how to avoid that.

Quoted 6 lines

If so, what are the disadvantages of waiting in drm_sched_fini()?
We
could block buggy drivers as I see it. Which wouldn't be good, but
could then be fixed on drivers' site.

Quoted 3 lines

Sima explained that pretty well: Don't block in fops->close, do that in
fops->flush instead.

Quoted 8 lines

I agree that we shouldn't block in close(), but this effectively means that we
need to reference count the scheduler, right?

Otherwise, if we allow to just skip / interrupt the teardown, we can
still leak
memory.

Having thought about it, I agree with Danilo. Having something that shall wait on green light, but can be interrupted, is no guarantee and
therefore not a feasible solution.

To break down the solution space, these seem to be our options:
   1. We have something (either drm_sched_fini() or a helper, e.g., drm_sched_flush()) that definitely blocks until the pending list
      has become empty.
   2. We have jobs reference-count the scheduler, so the latter can outlive the driver and will be freed some time later.

Can anyone think of a third solution?

Solution #1 has the problem of obviously blocking unconditionally if the driver didn't make sure that all fences will be signaled within reasonable time. In my opinion, this would actually be an advantage, because it will be *very* noticable and force users to repair their driver. The driver *has* to guarantee that all fences will be signaled.
If the driver has to do fishy things, having the blocking outsourced to
the helper drm_sched_flush() would allow them to circumvent that.

Solution #2 has the problem of backend_ops.free_job() potentially using
driver-data after the driver is gone, causing UAF. So with this solutions all drivers would have to be aware of the issue and handle it
through one of DRMs primitives dedicated to such problems.

Currently, all drivers either work around the problem internally or simply ignore it, it seems.

So I'd argue that both solutions are an improvement over the existing situation. My preference would be #1.

Opinions?

P.

Quoted 355 lines
Quoted 354 lines

One issue this solves is that when you send a SIGTERM the tear down
handling
first flushes all the FDs and then closes them.

So if flushing the FDs blocks because the process initiated sending
a
terabyte of data over a 300bps line (for example) you can still throw a
SIGKILL and abort that as well.

If you would block in fops-close() that SIGKILL won't have any effect any
more because by the time close() is called the process is gone and
signals
are already blocked.

And yes when I learned about that issue I was also buffed that handling like
this in the UNIX design is nearly 50 years old and still applies to
today.

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Probably better to make that a separate function, something like
drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers
which
are not sure whether all jobs are done before they start tearing
down.

Yes exactly that's the idea. And give that flush function a return
code so
that it can return -EINTR.

Quoted 108 lines
Quoted 28 lines

Yeah I don't think we should smash this into drm_sched_fini unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because
   drm_sched_fini is called long after all the entities are gone in
   drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably
want to
   nuke the context by setting the tdr timeout to zero (or maybe just
as
   long as context preemption takes to be efficient), and relying on
the
   normal gpu reset flow to handle things. drm_sched_entity_flush
kinda
   does this, except not really and it's a lot more focused on
the
   ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 11 lines

   For this case calling drm_sched_fini() after the 1:1 entity
is gone
   should not find any linger jobs, it would actually be a bug
somewhere if
   there's a job lingering. Maybe a sanity check that there's not just
no
   jobs lingering, but also no entity left would be good here?

The check for lingering ones is in Christian's patch here IISC. At which position would you imagine the check for the entity being
performed?

Quoted 10 lines

- fw scheduler without preemption support. There we kinda need
the
   drm_sched_flush, except blocking in fops->close is not great. So
instead
   I think the following is better:
   1. drm_sched_entity_stopped, which only stops new submissions (for
   paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting new jobs
from userspace to their entities, couldn't they?

Quoted 11 lines

   2. drm_dev_get
   3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b) drm_sched_entity_fini
   + drm_sched_fini c) drm_dev_put

   Note that semantically this implements the refcount in the other
path
   from Phillip:

Quoted 8 lines

https://lore.kernel.org/all/[email protected]/
   Except it doesn't impose refcount on everyone else who doesn't need
it,
   and it doesn't even impose refcounting on drivers that do need it
   because we use drm_sched_flush and a worker to achieve the same.

I indeed wasn't happy with the refcount approach for that reason,
agreed.

Quoted 7 lines

Essentially helper functions for the common use-cases instead of
trying to
solve them all by putting drm_sched_flush as a potentially very
blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired – as
far
as I can see, it is only invoked on driver teardown, so not during
active operation. Teardown doesn't happen that often, and it can
(if
implemented correctly) only block until the driver's code has signaled
the last fences. If that doesn't happen, the block would reveal a
bug.

But don't get me wrong: I don't want to *push* this solution. I just
want to understand when it could become a problem.

Quoted 4 lines

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable step
towards the right direction?

I think that this is the right thing to do, yes.

Quoted 77 lines
Quoted 68 lines
Quoted 68 lines
Quoted 48 lines
Quoted 41 lines
Quoted 25 lines
Quoted 25 lines

When there are still entities with jobs the situation
is
even
worse
since the dma_fences for those jobs can never signal
we can
just
choose between potentially locking up core memory management and
random memory corruption. When drivers really mess it
up
that
well
let them run into a BUG_ON().

Signed-off-by: Christian
König<[email protected]>
---
    drivers/gpu/drm/scheduler/sched_main.c | 19
++++++++++++++++++-

    1 file changed, 18 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in
the
function's
docstring what the user is expected to have done before
calling the
function.

Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to
be done
before _fini() is mentioned.

Quoted 8 lines

One could also think about providing a hint at how the driver can
do
that. AFAICS the only way for the driver to ensure that is
to
maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee when
callbacks
are processed, e.g. they can be both processed from interrupt
context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 9 lines
Quoted 9 lines

So when a driver (or drm_sched_fini) waits for the last submitted
fence it
actually can be that the drm_sched object still needs to do some
processing.
See the hack in amdgpu_vm_tlb_seq() for more background on the
problem.

Quoted 16 lines

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 9 lines

So I thought this should be fairly easy because of the sched hw/public
fence split: If we wait for both all jobs to finish and for all
the
sched
work/tdr work to finish, and we make sure there's no entity existing
that's not yet stopped we should catch them all?

Unfortunately not.

Even when you do a dma_fence_wait() on the last submission it can still be
that another CPU is executing the callbacks to wake up the scheduler work
item and cleanup the job.

That's one of the reasons why I think the design of keeping the job
alive is
so extremely awkward. The dma_fence as representation of the hw submission
has a much better defined state machine and lifetime.

Regards,
Christian.

Quoted 86 lines
Quoted 86 lines

  Or at least I think
it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing
then I
think that's just a separate concern and we can ignore that here
since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 71 lines

Regards,
Christian.

Quoted 68 lines

P.

Quoted 66 lines

Thanks,
Christian.

Quoted 63 lines

P.

Quoted 61 lines

     drm_sched_wqueue_stop(sched);
+ /*
+ * Tearing down the scheduler wile there are still unprocessed jobs can
+ * lead to use after free issues in the scheduler fence.
+ */
+ WARN_ON(!list_empty(&sched->pending_list)); +
     for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched-

Quoted 1 line

num_rqs;

i++)
{
     struct drm_sched_rq *rq = sched-

Quoted 1 line

sched_rq[i];

     spin_lock(&rq->lock);
- list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list)
+ list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list) {
+ /*
+ * The justification for this
BUG_ON()
is
that tearing
+ * down the scheduler while jobs
are
pending
leaves
+ * dma_fences unsignaled. Since we
have
dependencies
+ * from the core memory management
to
eventually signal
+ * dma_fences this can trivially
lead to
a
system wide
+ * stop because of a locked up
memory
management.
+ */
+ BUG_ON(spsc_queue_count(&s_entity-

Quoted 1 line

job_queue));

+
     /*
     * Prevents reinsertion and marks
job_queue
as idle,
     * it will removed from rq in
drm_sched_entity_fini
     * eventually
     */
     s_entity->stopped = true;
+ }
     spin_unlock(&rq->lock);
     kfree(sched->sched_rq[i]);
     }

#15 lore raw

Interesting, those mails just showed up in my inbox as unread. More than 14 days after you send it.

So sorry for the late reply.

Am 29.10.24 um 08:22 schrieb Philipp Stanner:

Quoted 10 lines

Christian, Sima?

Matthew? (+CC)

Opinions on the below?

tl;dr:
I still think it's a good thing to detectably block in drm_sched_fini(), or at the very least drm_sched_flush(), because then you'll find out that the driver is broken and can repair it.

As discussed in private mail as well, the third option not mentioned so far is to completely drop the free_job callback.

A good bunch of the problems we have here are caused by abusing the job as state for executing the submission on the hardware.

The original design idea of the scheduler instead was to only have the job as intermediate state between submission and picking what to execute next. E.g. the scheduler in that view is just a pipeline were you push jobs in on one end and jobs come out on the other end.

In that approach the object which represents the hardware execution is the dma_fence instead of the job. And the dma_fence has a well defined lifetime, error handling, etc...

So when we go back to this original approach it would mean that we don't need to wait for any cleanup because the scheduler wouldn't be involved in the execution of the jobs on the hardware any more.

The worst thing that could happen is that the driver messes things up and still has not executed job in an entity, but in that case we could just warn, block for the hardware fence and then kill all pending submissions with an error.- And this blocking on the hardware fence can't deadlock because we know that it is a hardware fence with certain properties and not some random free_job callback where we don't have any idea what locks the driver need.

Regards,
Christian.

Quoted 452 lines

P.

Quoted 447 lines · Philipp Stanner
Quoted 44 lines · Danilo Krummrich
Quoted 40 lines · Christian König

Am 25.09.24 um 16:53 schrieb Philipp Stanner:

Quoted 24 lines · Simona Vetter
Quoted 23 lines
Quoted 21 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 13 lines · Christian König
Quoted 12 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 11 lines
Quoted 9 lines · Christian König

Tearing down the scheduler with jobs still on the pending
list
can
lead to use after free issues. Add a warning if drivers try
to
destroy a scheduler which still has work pushed to the HW.

Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

Did you have time yet to look into my proposed
waitque-
solution?

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

Interesting approach, I'm just not sure if we can or should wait in
drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts
is
always a bug, right?

Correct, the question is how to avoid that.

Quoted 6 lines

If so, what are the disadvantages of waiting in drm_sched_fini()?
We
could block buggy drivers as I see it. Which wouldn't be good, but
could then be fixed on drivers' site.

Quoted 3 lines

Sima explained that pretty well: Don't block in fops->close, do that in
fops->flush instead.

Quoted 8 lines

I agree that we shouldn't block in close(), but this effectively means that we
need to reference count the scheduler, right?

Otherwise, if we allow to just skip / interrupt the teardown, we can
still leak
memory.

Having thought about it, I agree with Danilo. Having something that shall wait on green light, but can be interrupted, is no guarantee and
therefore not a feasible solution.

To break down the solution space, these seem to be our options:
   1. We have something (either drm_sched_fini() or a helper, e.g., drm_sched_flush()) that definitely blocks until the pending
list
      has become empty.
   2. We have jobs reference-count the scheduler, so the latter can outlive the driver and will be freed some time later.

Can anyone think of a third solution?

Solution #1 has the problem of obviously blocking unconditionally if the driver didn't make sure that all fences will be signaled within reasonable time. In my opinion, this would actually be an advantage, because it will be *very* noticable and force users to repair their driver. The driver *has* to guarantee that all fences will be signaled.
If the driver has to do fishy things, having the blocking outsourced to
the helper drm_sched_flush() would allow them to circumvent that.

Solution #2 has the problem of backend_ops.free_job() potentially using
driver-data after the driver is gone, causing UAF. So with this solutions all drivers would have to be aware of the issue and handle it
through one of DRMs primitives dedicated to such problems.

Currently, all drivers either work around the problem internally or simply ignore it, it seems.

So I'd argue that both solutions are an improvement over the existing situation. My preference would be #1.

Opinions?

P.

Quoted 349 lines
Quoted 349 lines

One issue this solves is that when you send a SIGTERM the tear down
handling
first flushes all the FDs and then closes them.

So if flushing the FDs blocks because the process initiated sending
a
terabyte of data over a 300bps line (for example) you can still throw a
SIGKILL and abort that as well.

If you would block in fops-close() that SIGKILL won't have any effect any
more because by the time close() is called the process is gone and
signals
are already blocked.

And yes when I learned about that issue I was also buffed that handling like
this in the UNIX design is nearly 50 years old and still applies to
today.

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Probably better to make that a separate function, something like
drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers
which
are not sure whether all jobs are done before they start tearing
down.

Yes exactly that's the idea. And give that flush function a return
code so
that it can return -EINTR.

Quoted 108 lines
Quoted 28 lines

Yeah I don't think we should smash this into drm_sched_fini unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because
   drm_sched_fini is called long after all the entities are
gone in
   drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably
want to
   nuke the context by setting the tdr timeout to zero (or
maybe just
as
   long as context preemption takes to be efficient), and
relying on
the
   normal gpu reset flow to handle things.
drm_sched_entity_flush
kinda
   does this, except not really and it's a lot more focused
on
the
   ringbuffer context. So maybe we want a new
drm_sched_entity_kill.

Quoted 11 lines

   For this case calling drm_sched_fini() after the 1:1
entity
is gone
   should not find any linger jobs, it would actually be a
bug
somewhere if
   there's a job lingering. Maybe a sanity check that there's
not just
no
   jobs lingering, but also no entity left would be good
here?

The check for lingering ones is in Christian's patch here IISC. At which position would you imagine the check for the entity being
performed?

Quoted 10 lines

- fw scheduler without preemption support. There we kinda need
the
   drm_sched_flush, except blocking in fops->close is not
great. So
instead
   I think the following is better:
   1. drm_sched_entity_stopped, which only stops new
submissions (for
   paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting new jobs
from userspace to their entities, couldn't they?

Quoted 11 lines

   2. drm_dev_get
   3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b)
drm_sched_entity_fini
   + drm_sched_fini c) drm_dev_put

   Note that semantically this implements the refcount in the
other
path
   from Phillip:

Quoted 8 lines

https://lore.kernel.org/all/[email protected]/
   Except it doesn't impose refcount on everyone else who
doesn't need
it,
   and it doesn't even impose refcounting on drivers that do
need it
   because we use drm_sched_flush and a worker to achieve the
same.

I indeed wasn't happy with the refcount approach for that reason,
agreed.

Quoted 7 lines

Essentially helper functions for the common use-cases instead of
trying to
solve them all by putting drm_sched_flush as a potentially very
blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired – as
far
as I can see, it is only invoked on driver teardown, so not during
active operation. Teardown doesn't happen that often, and it can
(if
implemented correctly) only block until the driver's code has signaled
the last fences. If that doesn't happen, the block would reveal a
bug.

But don't get me wrong: I don't want to *push* this solution. I just
want to understand when it could become a problem.

Quoted 4 lines

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable step
towards the right direction?

I think that this is the right thing to do, yes.

Quoted 77 lines
Quoted 68 lines
Quoted 68 lines
Quoted 48 lines
Quoted 41 lines
Quoted 25 lines
Quoted 25 lines

When there are still entities with jobs the situation
is
even
worse
since the dma_fences for those jobs can never signal
we can
just
choose between potentially locking up core memory management and
random memory corruption. When drivers really mess it
up
that
well
let them run into a BUG_ON().

Signed-off-by: Christian
König<[email protected]>

---
     drivers/gpu/drm/scheduler/sched_main.c | 19
++++++++++++++++++-
     1 file changed, 18 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in
the
function's
docstring what the user is expected to have done before
calling the
function.

Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to
be done
before _fini() is mentioned.

Quoted 8 lines

One could also think about providing a hint at how the driver can
do
that. AFAICS the only way for the driver to ensure that is
to
maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee when
callbacks
are processed, e.g. they can be both processed from interrupt
context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 9 lines
Quoted 9 lines

So when a driver (or drm_sched_fini) waits for the last submitted
fence it
actually can be that the drm_sched object still needs to do some
processing.
See the hack in amdgpu_vm_tlb_seq() for more background on the
problem.

Quoted 16 lines

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 9 lines

So I thought this should be fairly easy because of the sched hw/public
fence split: If we wait for both all jobs to finish and for all
the
sched
work/tdr work to finish, and we make sure there's no entity existing
that's not yet stopped we should catch them all?

Unfortunately not.

Even when you do a dma_fence_wait() on the last submission it can still be
that another CPU is executing the callbacks to wake up the scheduler work
item and cleanup the job.

That's one of the reasons why I think the design of keeping the job
alive is
so extremely awkward. The dma_fence as representation of the hw submission
has a much better defined state machine and lifetime.

Regards,
Christian.

Quoted 86 lines
Quoted 86 lines

  Or at least I think
it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing
then I
think that's just a separate concern and we can ignore that here
since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 71 lines

Regards,
Christian.

Quoted 68 lines

P.

Quoted 66 lines

Thanks,
Christian.

Quoted 63 lines

P.

Quoted 61 lines

     drm_sched_wqueue_stop(sched);
+ /*
+ * Tearing down the scheduler wile there are still unprocessed jobs can
+ * lead to use after free issues in the scheduler fence.
+ */
+ WARN_ON(!list_empty(&sched->pending_list)); +
     for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched-

Quoted 1 line

num_rqs;

i++)
{
     struct drm_sched_rq *rq = sched-

Quoted 1 line

sched_rq[i];

     spin_lock(&rq->lock);
- list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list)
+ list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list) {
+ /*
+ * The justification for this
BUG_ON()
is
that tearing
+ * down the scheduler while jobs
are
pending
leaves
+ * dma_fences unsignaled. Since we
have
dependencies
+ * from the core memory management
to
eventually signal
+ * dma_fences this can trivially
lead to
a
system wide
+ * stop because of a locked up
memory
management.
+ */
+ BUG_ON(spsc_queue_count(&s_entity-

Quoted 1 line

job_queue));

+
     /*
     * Prevents reinsertion and marks
job_queue
as idle,
     * it will removed from rq in
drm_sched_entity_fini
     * eventually
     */
     s_entity->stopped = true;
+ }
     spin_unlock(&rq->lock);
     kfree(sched->sched_rq[i]);
     }

#16 lore raw
Quoted 5 lines · Christian König

Interesting, those mails just showed up in my inbox as unread. More than
14 days after you send it.

So sorry for the late reply.

I smell google mail :p

Quoted 14 lines

Am 29.10.24 um 08:22 schrieb Philipp Stanner:

Quoted 11 lines

Christian, Sima?

Matthew? (+CC)

Opinions on the below?

tl;dr:
I still think it's a good thing to detectably block in drm_sched_fini(), or at the very least drm_sched_flush(), because then
you'll find out that the driver is broken and can repair it.

Quoted 3 lines

As discussed in private mail as well, the third option not mentioned so
far is to completely drop the free_job callback.

If it's really possible to do that I think this sounds like a great solution at first glance because we could decrease complexity quite substantially.

Quoted 5 lines

A good bunch of the problems we have here are caused by abusing the job
as state for executing the submission on the hardware.

Quoted 7 lines

The original design idea of the scheduler instead was to only have the
job as intermediate state between submission and picking what to execute
next. E.g. the scheduler in that view is just a pipeline were you push
jobs in on one end and jobs come out on the other end.

So let's get a bit more precise about this:
1. Driver enqueues with drm_sched_job_arm()
2. job ends up in pending_list
3. Sooner or later scheduler calls run_job()
4. Job is pushed to hardware
5. Fence gets signaled
6. ???

What would the "come out on the other end" part you describe look like?

How would jobs get removed from pending_list and, accordingly, how would we avoid leaks?

Quoted 7 lines

In that approach the object which represents the hardware execution is
the dma_fence instead of the job. And the dma_fence has a well defined
lifetime, error handling, etc...

Quoted 5 lines

So when we go back to this original approach it would mean that we don't
need to wait for any cleanup because the scheduler wouldn't be involved
in the execution of the jobs on the hardware any more.

It would be involved (to speak precisely) in the sense of the scheduler still being the one who pushes the job onto the hardware, agreed?

Quoted 3 lines

The worst thing that could happen is that the driver messes things up and still has not executed job in an entity,

I can't fully follow.

So in your mind, the driver would personally set the dma_fence callback and hereby be informed about the job being completed, right?

But you wouldn't want to aim for getting rid of struct drm_sched_job completely, or would you?

Grüße,
P.

Quoted 544 lines

but in that case we could
just warn, block for the hardware fence and then kill all pending submissions with an error.- And this blocking on the hardware fence can't deadlock because we know that it is a hardware fence with certain
properties and not some random free_job callback where we don't have any
idea what locks the driver need.

Regards,
Christian.

Quoted 531 lines

P.

Quoted 526 lines · Philipp Stanner
Quoted 58 lines · Danilo Krummrich
Quoted 47 lines · Christian König

Am 25.09.24 um 16:53 schrieb Philipp Stanner:

Quoted 29 lines · Simona Vetter
Quoted 28 lines
Quoted 26 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 17 lines
Quoted 15 lines · Christian König

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 14 lines
Quoted 11 lines · Christian König

Tearing down the scheduler with jobs still on
the
pending
list
can
lead to use after free issues. Add a warning if
drivers try
to
destroy a scheduler which still has work pushed
to
the HW.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

Did you have time yet to look into my proposed
waitque-
solution?

I don't remember seeing anything. What have I
missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Interesting approach, I'm just not sure if we can or should
wait in
drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts
is
always a bug, right?

Correct, the question is how to avoid that.

Quoted 7 lines

If so, what are the disadvantages of waiting in drm_sched_fini()?
We
could block buggy drivers as I see it. Which wouldn't be good,
but
could then be fixed on drivers' site.

Quoted 4 lines

Sima explained that pretty well: Don't block in fops->close, do
that in
fops->flush instead.

I agree that we shouldn't block in close(), but this effectively
means that we
need to reference count the scheduler, right?

Quoted 5 lines

Otherwise, if we allow to just skip / interrupt the teardown, we
can
still leak
memory.

Having thought about it, I agree with Danilo. Having something that
shall wait on green light, but can be interrupted, is no guarantee
and
therefore not a feasible solution.

To break down the solution space, these seem to be our options:
    1. We have something (either drm_sched_fini() or a helper, e.g.,
       drm_sched_flush()) that definitely blocks until the pending
list
       has become empty.
    2. We have jobs reference-count the scheduler, so the latter can
       outlive the driver and will be freed some time later.

Can anyone think of a third solution?

Solution #1 has the problem of obviously blocking unconditionally if
the driver didn't make sure that all fences will be signaled within
reasonable time. In my opinion, this would actually be an advantage,
because it will be *very* noticable and force users to repair their
driver. The driver *has* to guarantee that all fences will be signaled.
If the driver has to do fishy things, having the blocking outsourced
to
the helper drm_sched_flush() would allow them to circumvent that.

Solution #2 has the problem of backend_ops.free_job() potentially using
driver-data after the driver is gone, causing UAF. So with this solutions all drivers would have to be aware of the issue and handle
it
through one of DRMs primitives dedicated to such problems.

Currently, all drivers either work around the problem internally or
simply ignore it, it seems.

So I'd argue that both solutions are an improvement over the existing
situation. My preference would be #1.

Opinions?

P.

Quoted 404 lines
Quoted 404 lines

One issue this solves is that when you send a SIGTERM the tear
down
handling
first flushes all the FDs and then closes them.

So if flushing the FDs blocks because the process initiated sending
a
terabyte of data over a 300bps line (for example) you can still
throw a
SIGKILL and abort that as well.

If you would block in fops-close() that SIGKILL won't have any
effect any
more because by the time close() is called the process is gone
and
signals
are already blocked.

And yes when I learned about that issue I was also buffed that
handling like
this in the UNIX design is nearly 50 years old and still applies
to
today.

Quoted 10 lines
Quoted 4 lines
Quoted 4 lines

Probably better to make that a separate function,
something
like
drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers
which
are not sure whether all jobs are done before they start tearing
down.

Yes exactly that's the idea. And give that flush function a return
code so
that it can return -EINTR.

Quoted 130 lines
Quoted 33 lines

Yeah I don't think we should smash this into drm_sched_fini
unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because
    drm_sched_fini is called long after all the entities are
gone in
    drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably
want to
    nuke the context by setting the tdr timeout to zero (or
maybe just
as
    long as context preemption takes to be efficient), and
relying on
the
    normal gpu reset flow to handle things. drm_sched_entity_flush
kinda
    does this, except not really and it's a lot more focused
on
the
    ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 13 lines

    For this case calling drm_sched_fini() after the 1:1 entity
is gone
    should not find any linger jobs, it would actually be a
bug
somewhere if
    there's a job lingering. Maybe a sanity check that there's
not just
no
    jobs lingering, but also no entity left would be good here?

The check for lingering ones is in Christian's patch here IISC.
At which position would you imagine the check for the entity
being
performed?

Quoted 11 lines

- fw scheduler without preemption support. There we kinda need
the
    drm_sched_flush, except blocking in fops->close is not
great. So
instead
    I think the following is better:
    1. drm_sched_entity_stopped, which only stops new submissions (for
    paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting
new jobs
from userspace to their entities, couldn't they?

Quoted 12 lines

    2. drm_dev_get
    3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b) drm_sched_entity_fini
    + drm_sched_fini c) drm_dev_put

    Note that semantically this implements the refcount in the
other
path
    from Phillip:

Quoted 11 lines

https://lore.kernel.org/all/[email protected]/
    Except it doesn't impose refcount on everyone else who
doesn't need
it,
    and it doesn't even impose refcounting on drivers that do
need it
    because we use drm_sched_flush and a worker to achieve the
same.

I indeed wasn't happy with the refcount approach for that reason,
agreed.

Quoted 9 lines

Essentially helper functions for the common use-cases instead
of
trying to
solve them all by putting drm_sched_flush as a potentially
very
blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired –
as
far
as I can see, it is only invoked on driver teardown, so not during
active operation. Teardown doesn't happen that often, and it
can
(if
implemented correctly) only block until the driver's code has
signaled
the last fences. If that doesn't happen, the block would reveal
a
bug.

But don't get me wrong: I don't want to *push* this solution. I
just
want to understand when it could become a problem.

Quoted 5 lines

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable
step
towards the right direction?

I think that this is the right thing to do, yes.

Quoted 92 lines
Quoted 81 lines
Quoted 81 lines
Quoted 56 lines
Quoted 48 lines
Quoted 40 lines
Quoted 36 lines

When there are still entities with jobs the
situation
is
even
worse
since the dma_fences for those jobs can never
signal
we can
just
choose between potentially locking up core
memory
management and
random memory corruption. When drivers really
mess
it
up
that
well
let them run into a BUG_ON().

Signed-off-by: Christian
König<[email protected]>
---
     drivers/gpu/drm/scheduler/sched_main.c |
19
++++++++++++++++++-

     1 file changed, 18 insertions(+), 1

deletion(-)

diff --git
a/drivers/gpu/drm/scheduler/sched_main.c
b/drivers/gpu/drm/scheduler/sched_main.c
index f093616fe53c..8a46fab5cdc8 100644

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -1333,17 +1333,34 @@ void
Quoted 3 lines

drm_sched_fini(struct
drm_gpu_scheduler
*sched)

I agree with Sima that it should first be

Quoted 8 lines

documented
in
the
function's
docstring what the user is expected to have done
before
calling the
function.

Quoted 2 lines

Good point, going to update the documentation as
well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to
be done
before _fini() is mentioned.

Quoted 10 lines

One could also think about providing a hint at how the
driver can
do
that. AFAICS the only way for the driver to ensure that
is
to
maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually
currently
impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee
when
callbacks
are processed, e.g. they can be both processed from interrupt
context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 11 lines
Quoted 11 lines

So when a driver (or drm_sched_fini) waits for the last submitted
fence it
actually can be that the drm_sched object still needs to do
some
processing.
See the hack in amdgpu_vm_tlb_seq() for more background on
the
problem.

Quoted 19 lines

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 12 lines

So I thought this should be fairly easy because of the sched
hw/public
fence split: If we wait for both all jobs to finish and for
all
the
sched
work/tdr work to finish, and we make sure there's no entity
existing
that's not yet stopped we should catch them all?

Unfortunately not.

Even when you do a dma_fence_wait() on the last submission it can
still be
that another CPU is executing the callbacks to wake up the scheduler work
item and cleanup the job.

That's one of the reasons why I think the design of keeping the
job
alive is
so extremely awkward. The dma_fence as representation of the hw
submission
has a much better defined state machine and lifetime.

Regards,
Christian.

Quoted 90 lines
Quoted 90 lines

   Or at least I think
it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing
then I
think that's just a separate concern and we can ignore that
here
since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 74 lines

Regards,
Christian.

Quoted 71 lines

P.

Quoted 69 lines

Thanks,
Christian.

Quoted 66 lines

P.

Quoted 64 lines

      drm_sched_wqueue_stop(sched);
+ /*
+ * Tearing down the scheduler wile there are
still
unprocessed jobs can
+ * lead to use after free issues in the
scheduler
fence.
+ */
+ WARN_ON(!list_empty(&sched->pending_list));
+
      for (i = DRM_SCHED_PRIORITY_KERNEL; i <
sched-

Quoted 1 line

num_rqs;

i++)
{
      struct drm_sched_rq *rq = sched-

Quoted 1 line

sched_rq[i];

      spin_lock(&rq->lock);
- list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list)
+ list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list) {
+ /*
+ * The justification for this
BUG_ON()
is
that tearing
+ * down the scheduler while jobs
are
pending
leaves
+ * dma_fences unsignaled. Since we
have
dependencies
+ * from the core memory management
to
eventually signal
+ * dma_fences this can trivially
lead to
a
system wide
+ * stop because of a locked up
memory
management.
+ */
+ BUG_ON(spsc_queue_count(&s_entity-

Quoted 1 line

job_queue));

+
      /*
      * Prevents reinsertion and marks
job_queue
as idle,
      * it will removed from rq in
drm_sched_entity_fini
      * eventually
      */
      s_entity->stopped = true;
+ }
      spin_unlock(&rq->lock);
      kfree(sched->sched_rq[i]);
      }

#17 lore raw

Am 15.11.24 um 14:55 schrieb Philipp Stanner:

Quoted 20 lines

[SNIP]

Quoted 11 lines

A good bunch of the problems we have here are caused by abusing the job
as state for executing the submission on the hardware.

The original design idea of the scheduler instead was to only have the
job as intermediate state between submission and picking what to execute
next. E.g. the scheduler in that view is just a pipeline were you push
jobs in on one end and jobs come out on the other end.

So let's get a bit more precise about this:
1. Driver enqueues with drm_sched_job_arm()
2. job ends up in pending_list
3. Sooner or later scheduler calls run_job()
4. Job is pushed to hardware
5. Fence gets signaled
6. ???

Quoted 4 lines

What would the "come out on the other end" part you describe look like?

How would jobs get removed from pending_list and, accordingly, how would we avoid leaks?

Let me describe the full solution a bit further down.

Quoted 6 lines
Quoted 6 lines

In that approach the object which represents the hardware execution is
the dma_fence instead of the job. And the dma_fence has a well defined
lifetime, error handling, etc...

Quoted 7 lines
Quoted 5 lines

So when we go back to this original approach it would mean that we don't
need to wait for any cleanup because the scheduler wouldn't be involved
in the execution of the jobs on the hardware any more.

It would be involved (to speak precisely) in the sense of the scheduler still being the one who pushes the job onto the hardware, agreed?

Yes, exactly.

See in the original design the "job" wasn't even a defined structure, but rather just a void*.

So basically what the driver did was telling the scheduler here I have a bunch of different void* please tell me which one to run first.

And apart from being this identifier this void* had absolutely no meaning for the scheduler.

Quoted 6 lines
Quoted 2 lines

The worst thing that could happen is that the driver messes things up and still has not executed job in an entity,

I can't fully follow.

So in your mind, the driver would personally set the dma_fence callback and hereby be informed about the job being completed, right?

No. The run_job callback would still return the hw fence so that the scheduler can install the callback and so gets informed when the next job could be run.

Quoted 2 lines

But you wouldn't want to aim for getting rid of struct drm_sched_job completely, or would you?

No, the drm_sched_job structure was added to aid the single producer single consumer queue and so made it easier to put the jobs into a container.

In my mind the full solution for running jobs looks like this:

1. Driver enqueues with drm_sched_job_arm()
2. job ends up in pending_list
3. Sooner or later scheduler calls run_job()
4. In return scheduler gets a dma_fence representing the resulting hardware operation.
5, This fence is put into a container to keep around what the hw is actually executing.
6. At some point the fence gets signaled informing the scheduler that the next job can be pushed if enough credits are now available.

There is no free_job callback any more because after run_job is called the scheduler is done with the job and only the dma_fence which represents the actually HW operation is the object the scheduler now works with.

We would still need something like a kill_job callback which is used when an entity is released without flushing all jobs (see drm_sched_entity_kill()), but that is then only used for a specific corner case.

Blocking for the cleanup in drm_sched_fini() then becomes a trivial dma_fence_wait() on the remaining unsignaled HW fences and eventually on the latest killed fence.

The problem with this approach is that the idea of re-submitting jobs in a GPU reset by the scheduler is then basically dead. But to be honest that never worked correctly.

See the deprecated warning I already put on drm_sched_resubmit_jobs(). The job lifetime is not really well defined because of this, see the free_guilty hack as well.

Regards,
Christian.

Quoted 5 lines

Grüße,
P.

#18 lore raw
Quoted 59 lines · Christian König

 Am 15.11.24 um 14:55 schrieb Philipp Stanner:

Quoted 25 lines

[SNIP]

Quoted 6 lines


A good bunch of the problems we have here are caused by abusing the
job
as state for executing the submission on the hardware.

Quoted 9 lines

The original design idea of the scheduler instead was to only have
the
job as intermediate state between submission and picking what to execute
next. E.g. the scheduler in that view is just a pipeline were you push
jobs in on one end and jobs come out on the other end.


So let's get a bit more precise about this:
   1. Driver enqueues with drm_sched_job_arm()
   2. job ends up in pending_list
   3. Sooner or later scheduler calls run_job()
   4. Job is pushed to hardware
   5. Fence gets signaled
   6. ???

Quoted 6 lines

What would the "come out on the other end" part you describe look like?

How would jobs get removed from pending_list and, accordingly, how would we avoid leaks?


 Let me describe the full solution a bit further down.

Quoted 16 lines

Quoted 8 lines


In that approach the object which represents the hardware execution
is
the dma_fence instead of the job. And the dma_fence has a well defined
lifetime, error handling, etc...

Quoted 7 lines

So when we go back to this original approach it would mean that we
don't
need to wait for any cleanup because the scheduler wouldn't be involved
in the execution of the jobs on the hardware any more.

Quoted 5 lines


It would be involved (to speak precisely) in the sense of the scheduler
still being the one who pushes the job onto the hardware, agreed?

Quoted 12 lines


 Yes, exactly.

 See in the original design the "job" wasn't even a defined structure, but rather just a void*.

 So basically what the driver did was telling the scheduler here I have a bunch of different void* please tell me which one to run first.

 And apart from being this identifier this void* had absolutely no meaning for the scheduler.

Interesting..

Quoted 43 lines

Quoted 5 lines
Quoted 5 lines


The worst thing that could happen is that the driver messes things up
and still has not executed job in an entity,

Quoted 7 lines


I can't fully follow.

So in your mind, the driver would personally set the dma_fence callback
and hereby be informed about the job being completed, right?


 No. The run_job callback would still return the hw fence so that the scheduler can install the callback and so gets informed when the next job could be run.

Quoted 5 lines


But you wouldn't want to aim for getting rid of struct drm_sched_job
completely, or would you?


 No, the drm_sched_job structure was added to aid the single producer single consumer queue and so made it easier to put the jobs into a container.


 In my mind the full solution for running jobs looks like this:

 1. Driver enqueues with drm_sched_job_arm()
 2. job ends up in pending_list
 3. Sooner or later scheduler calls run_job()
 4. In return scheduler gets a dma_fence representing the resulting hardware operation.
 5, This fence is put into a container to keep around what the hw is actually executing.
 6. At some point the fence gets signaled informing the scheduler that the next job can be pushed if enough credits are now available.
 There is no free_job callback any more because after run_job is

Quoted 8 lines

called the scheduler is done with the job and only the dma_fence which represents the actually HW operation is the object the scheduler now works with.

 We would still need something like a kill_job callback which is used when an entity is released without flushing all jobs (see drm_sched_entity_kill()), but that is then only used for a specific corner case.

Can't we just limit the scheduler's responsibility to telling the driver that it has to flush, and if it doesn't it's a bug?

Quoted 3 lines

 Blocking for the cleanup in drm_sched_fini() then becomes a trivial dma_fence_wait() on the remaining unsignaled HW fences and eventually on the latest killed fence.

But that results in exactly the same situation as my waitque solution, doesn't it?

Blocking infinitely in drm_sched_fini():

If the driver doesn't guarantee that all fences will get signaled, then wait_event() in the waitque solution will block forever. The same with dma_fence_wait().

Or are you aiming at an interruptible dma_fence_wait(), thereby not blocking SIGKILL?

That then would still result in leaks. So basically the same situation as with an interruptible drm_sched_flush() function.

(Although I agree that would probably be more elegant)

Quoted 8 lines


 The problem with this approach is that the idea of re-submitting jobs in a GPU reset by the scheduler is then basically dead. But to be honest that never worked correctly.

 See the deprecated warning I already put on drm_sched_resubmit_jobs(). The job lifetime is not really well defined because of this, see the free_guilty hack as well.

drm_sched_resubmit_jobs() is being used by 5 drivers currently. So if we go for this approach we have to port them, first. That doesn't sound trivial to me

P.

Quoted 13 lines


 Regards,
 Christian.

Quoted 7 lines

Grüße,
P.

#19 lore raw

Am 15.11.24 um 17:07 schrieb Philipp Stanner:

Quoted 31 lines · Christian König
Quoted 30 lines

 Am 15.11.24 um 14:55 schrieb Philipp Stanner:

Quoted 5 lines

[SNIP]
But you wouldn't want to aim for getting rid of struct drm_sched_job
completely, or would you?

 No, the drm_sched_job structure was added to aid the single producer
single consumer queue and so made it easier to put the jobs into a container.

 In my mind the full solution for running jobs looks like this:

 1. Driver enqueues with drm_sched_job_arm()
 2. job ends up in pending_list
 3. Sooner or later scheduler calls run_job()
 4. In return scheduler gets a dma_fence representing the resulting
hardware operation.
 5, This fence is put into a container to keep around what the hw is
actually executing.
 6. At some point the fence gets signaled informing the scheduler
that the next job can be pushed if enough credits are now available.

 There is no free_job callback any more because after run_job is
called the scheduler is done with the job and only the dma_fence which represents the actually HW operation is the object the scheduler now works with.

Quoted 6 lines
Quoted 4 lines

 We would still need something like a kill_job callback which is used
when an entity is released without flushing all jobs (see drm_sched_entity_kill()), but that is then only used for a specific corner case.

Can't we just limit the scheduler's responsibility to telling the driver that it has to flush, and if it doesn't it's a bug?

We still need to remove the pending jobs from the scheduler if flushing times out.

Without timing out flushing and/or aborting when the process was killed we run into the same problem again that we don't want ti block on _fini().

Quoted 5 lines
Quoted 3 lines

 Blocking for the cleanup in drm_sched_fini() then becomes a trivial
dma_fence_wait() on the remaining unsignaled HW fences and eventually on the latest killed fence.

But that results in exactly the same situation as my waitque solution, doesn't it?

The key part is that dma_fence's have a documented requirement to never block infinitely.

Quoted 8 lines

Blocking infinitely in drm_sched_fini():

If the driver doesn't guarantee that all fences will get signaled, then wait_event() in the waitque solution will block forever. The same with dma_fence_wait().

Or are you aiming at an interruptible dma_fence_wait(), thereby not blocking SIGKILL?

That is basically what drm_sched_entity_flush() is already doing.

Quoted 4 lines

That then would still result in leaks. So basically the same situation as with an interruptible drm_sched_flush() function.

(Although I agree that would probably be more elegant)

If the wait_event really would just waiting for dma_fences then yes.

The problem with the waitqueue approach is that we need to wait for the free_job callback and that callback is normally called from a work item without holding any additional locks.

When drm_sched_fini starts to block for that we create a rat-tail of new dependencies since that one is most likely called from a file descriptor destruction.

Quoted 6 lines
Quoted 6 lines

 The problem with this approach is that the idea of re-submitting
jobs in a GPU reset by the scheduler is then basically dead. But to be honest that never worked correctly.

Quoted 6 lines
Quoted 3 lines

 See the deprecated warning I already put on
drm_sched_resubmit_jobs(). The job lifetime is not really well defined because of this, see the free_guilty hack as well.

drm_sched_resubmit_jobs() is being used by 5 drivers currently. So if we go for this approach we have to port them, first. That doesn't sound trivial to me

Yeah, completely agree. I think the scheduler should provide something like drm_sched_for_each_pending_fence() which helps to iterate over all the pending HW fences.

The individual drivers could then device by themself what to do, e.g. upcast the HW fence into the job and push it again or similar.

But what we really need to get away from are those fence pre-requisite violations Sima pointed out. For example we can't allocate memory for run_job.

Regards,
Christian.

Quoted 17 lines

P.

Quoted 13 lines

 Regards,
 Christian.

Quoted 7 lines

Grüße,
P.

#20 lore raw

+CC Thomas Hellström

Quoted 143 lines · Christian König

 Am 15.11.24 um 17:07 schrieb Philipp Stanner:

Quoted 55 lines


On Fri, 2024-11-15 at 15:39 +0100, Christian König wrote:

Quoted 8 lines


 Am 15.11.24 um 14:55 schrieb Philipp Stanner:

Quoted 3 lines


[SNIP]

Quoted 35 lines

Quoted 6 lines


But you wouldn't want to aim for getting rid of struct drm_sched_job
completely, or would you?



 No, the drm_sched_job structure was added to aid the single producer
single consumer queue and so made it easier to put the jobs into a
container.


 In my mind the full solution for running jobs looks like this:

 1. Driver enqueues with drm_sched_job_arm()
 2. job ends up in pending_list
 3. Sooner or later scheduler calls run_job()
 4. In return scheduler gets a dma_fence representing the resulting
hardware operation.
 5, This fence is put into a container to keep around what the hw is
actually executing.
 6. At some point the fence gets signaled informing the scheduler that the next job can be pushed if enough credits are now available.

 There is no free_job callback any more because after run_job is called the scheduler is done with the job and only the dma_fence which represents the actually HW operation is the object the scheduler now works with.

Quoted 8 lines


 We would still need something like a kill_job callback which is used
when an entity is released without flushing all jobs (see drm_sched_entity_kill()), but that is then only used for a specific
corner case.

Quoted 4 lines


Can't we just limit the scheduler's responsibility to telling the driver that it has to flush, and if it doesn't it's a bug?


 We still need to remove the pending jobs from the scheduler if flushing times out.

 Without timing out flushing and/or aborting when the process was killed we run into the same problem again that we don't want ti block on _fini().

Quoted 8 lines

Quoted 7 lines


 Blocking for the cleanup in drm_sched_fini() then becomes a trivial
dma_fence_wait() on the remaining unsignaled HW fences and eventually
on the latest killed fence.

Quoted 5 lines


But that results in exactly the same situation as my waitque solution,
doesn't it?


 The key part is that dma_fence's have a documented requirement to never block infinitely.

Quoted 9 lines


Blocking infinitely in drm_sched_fini():

If the driver doesn't guarantee that all fences will get signaled, then
wait_event() in the waitque solution will block forever. The same with
dma_fence_wait().

Quoted 3 lines

Or are you aiming at an interruptible dma_fence_wait(), thereby not blocking SIGKILL?


 That is basically what drm_sched_entity_flush() is already doing.

Quoted 7 lines


That then would still result in leaks. So basically the same situation
as with an interruptible drm_sched_flush() function.

(Although I agree that would probably be more elegant)


 If the wait_event really would just waiting for dma_fences then yes.
 The problem with the waitqueue approach is that we need to wait for the free_job callback and that callback is normally called from a work item without holding any additional locks.

 When drm_sched_fini starts to block for that we create a rat-tail of new dependencies since that one is most likely called from a file descriptor destruction.

Quoted 14 lines

Quoted 11 lines



 The problem with this approach is that the idea of re-submitting jobs in a GPU reset by the scheduler is then basically dead. But to
be honest that never worked correctly.

 See the deprecated warning I already put on drm_sched_resubmit_jobs(). The job lifetime is not really well defined because of this, see the free_guilty hack as well.

Quoted 7 lines


drm_sched_resubmit_jobs() is being used by 5 drivers currently. So if
we go for this approach we have to port them, first. That doesn't sound
trivial to me

Quoted 7 lines


 Yeah, completely agree. I think the scheduler should provide something like drm_sched_for_each_pending_fence() which helps to iterate over all the pending HW fences.

 The individual drivers could then device by themself what to do, e.g. upcast the HW fence into the job and push it again or similar.

I have talked with Dave and we would be interested in exploring the direction of getting rid of backend_ops.free_job() and doing the modifications this implies.

Matthew, Thomas, any hard NACKs on principle, or can we look into this in a future patch series?

P.

Quoted 37 lines


 But what we really need to get away from are those fence pre- requisite violations Sima pointed out. For example we can't allocate memory for run_job.

 Regards,
 Christian.

Quoted 20 lines

P.

Quoted 14 lines



 Regards,
 Christian.


Quoted 7 lines


Grüße,
P.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines





maintainer
#21 lore raw
Quoted 155 lines · Philipp Stanner

+CC Thomas Hellström

Quoted 143 lines · Christian König

 Am 15.11.24 um 17:07 schrieb Philipp Stanner:

Quoted 55 lines


On Fri, 2024-11-15 at 15:39 +0100, Christian König wrote:

Quoted 8 lines


 Am 15.11.24 um 14:55 schrieb Philipp Stanner:

Quoted 3 lines


[SNIP]

Quoted 35 lines

Quoted 6 lines


But you wouldn't want to aim for getting rid of struct drm_sched_job
completely, or would you?



 No, the drm_sched_job structure was added to aid the single producer
single consumer queue and so made it easier to put the jobs into a
container.


 In my mind the full solution for running jobs looks like this:

 1. Driver enqueues with drm_sched_job_arm()
 2. job ends up in pending_list
 3. Sooner or later scheduler calls run_job()
 4. In return scheduler gets a dma_fence representing the resulting
hardware operation.
 5, This fence is put into a container to keep around what the hw is
actually executing.
 6. At some point the fence gets signaled informing the scheduler that the next job can be pushed if enough credits are now available.

 There is no free_job callback any more because after run_job is called the scheduler is done with the job and only the dma_fence which represents the actually HW operation is the object the scheduler now works with.

Quoted 8 lines


 We would still need something like a kill_job callback which is used
when an entity is released without flushing all jobs (see drm_sched_entity_kill()), but that is then only used for a specific
corner case.

Quoted 4 lines


Can't we just limit the scheduler's responsibility to telling the driver that it has to flush, and if it doesn't it's a bug?


 We still need to remove the pending jobs from the scheduler if flushing times out.

 Without timing out flushing and/or aborting when the process was killed we run into the same problem again that we don't want ti block on _fini().

Quoted 8 lines

Quoted 7 lines


 Blocking for the cleanup in drm_sched_fini() then becomes a trivial
dma_fence_wait() on the remaining unsignaled HW fences and eventually
on the latest killed fence.

Quoted 5 lines


But that results in exactly the same situation as my waitque solution,
doesn't it?


 The key part is that dma_fence's have a documented requirement to never block infinitely.

Quoted 9 lines


Blocking infinitely in drm_sched_fini():

If the driver doesn't guarantee that all fences will get signaled, then
wait_event() in the waitque solution will block forever. The same with
dma_fence_wait().

Quoted 3 lines

Or are you aiming at an interruptible dma_fence_wait(), thereby not blocking SIGKILL?


 That is basically what drm_sched_entity_flush() is already doing.

Quoted 7 lines


That then would still result in leaks. So basically the same situation
as with an interruptible drm_sched_flush() function.

(Although I agree that would probably be more elegant)


 If the wait_event really would just waiting for dma_fences then yes.
 The problem with the waitqueue approach is that we need to wait for the free_job callback and that callback is normally called from a work item without holding any additional locks.

 When drm_sched_fini starts to block for that we create a rat-tail of new dependencies since that one is most likely called from a file descriptor destruction.

Quoted 14 lines

Quoted 11 lines



 The problem with this approach is that the idea of re-submitting jobs in a GPU reset by the scheduler is then basically dead. But to
be honest that never worked correctly.

 See the deprecated warning I already put on drm_sched_resubmit_jobs(). The job lifetime is not really well defined because of this, see the free_guilty hack as well.

Quoted 7 lines


drm_sched_resubmit_jobs() is being used by 5 drivers currently. So if
we go for this approach we have to port them, first. That doesn't sound
trivial to me

Quoted 7 lines


 Yeah, completely agree. I think the scheduler should provide something like drm_sched_for_each_pending_fence() which helps to iterate over all the pending HW fences.

 The individual drivers could then device by themself what to do, e.g. upcast the HW fence into the job and push it again or similar.

Quoted 4 lines

I have talked with Dave and we would be interested in exploring the direction of getting rid of backend_ops.free_job() and doing the modifications this implies.

Sorry again, catching up late.

Got any details to dropping free_job?

Gut reaction is this is fairly large subsystem rework, touching many drivers, and great way to regress stable software.

Quoted 3 lines

Matthew, Thomas, any hard NACKs on principle, or can we look into this in a future patch series?

Definietly not NACK outright, but see my concerns above. If ends up not being all that invasive and good cleanup, not opposed. If it starts messing with existing reset / job cancel / queue teardown flows I would be concerned given how hard it is to get though correct.

Matt

Quoted 45 lines

P.

Quoted 37 lines


 But what we really need to get away from are those fence pre- requisite violations Sima pointed out. For example we can't allocate memory for run_job.

 Regards,
 Christian.

Quoted 20 lines

P.

Quoted 14 lines



 Regards,
 Christian.


Quoted 7 lines


Grüße,
P.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines





maintainer
#22 lore raw
In reply to Philipp Stanner
Quoted 4 lines · Philipp Stanner

Christian, Sima?

Matthew? (+CC)

Sorry catching up here. Internally in Xe we ref count the scheduler as the scheduler is embedded into our user queue structure (xe_exec_queue). Jobs ref the queue once they are armed. Upon queue killing we set TDR to zero which will flush out all jobs / signal all the job's fences. Once the ref count of queue is zero we do hardware / firmware teardown of the queue and then finally call drm_sched_fini before freeing the queues memory (and thus the scheduler).

This flow seems to work pretty well but largely bypasses the scheduler functions to implement this. Not sure if this flow could be normalized at all but I would expect usage models of a 1 to 1 relationship between queue and scheduler to something similar to Xe's flow. Maybe we could write this done as design guideline so other drivers don't have to figure this out - this took me a bit to land on this.

With that, in general I agree with Christian's patch here complaining about pending jobs if drm_sched_fini is called.

Quoted 5 lines

Opinions on the below?

tl;dr:
I still think it's a good thing to detectably block in drm_sched_fini(), or at the very least drm_sched_flush(), because then

See above. I don't think drm_sched_fini should block rather just complain this patch does which says 'go fix your driver'.

Matt

Quoted 465 lines

you'll find out that the driver is broken and can repair it.

P.

Quoted 458 lines · Philipp Stanner
Quoted 47 lines · Danilo Krummrich
Quoted 42 lines · Christian König

Am 25.09.24 um 16:53 schrieb Philipp Stanner:

Quoted 24 lines · Simona Vetter
Quoted 23 lines
Quoted 21 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 13 lines · Christian König
Quoted 12 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 11 lines
Quoted 9 lines · Christian König

Tearing down the scheduler with jobs still on the pending
list
can
lead to use after free issues. Add a warning if drivers try
to
destroy a scheduler which still has work pushed to the HW.

Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

Did you have time yet to look into my proposed
waitque-
solution?

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

Interesting approach, I'm just not sure if we can or should wait in
drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts
is
always a bug, right?

Correct, the question is how to avoid that.

Quoted 6 lines

If so, what are the disadvantages of waiting in drm_sched_fini()?
We
could block buggy drivers as I see it. Which wouldn't be good, but
could then be fixed on drivers' site.

Quoted 3 lines

Sima explained that pretty well: Don't block in fops->close, do that in
fops->flush instead.

Quoted 8 lines

I agree that we shouldn't block in close(), but this effectively means that we
need to reference count the scheduler, right?

Otherwise, if we allow to just skip / interrupt the teardown, we can
still leak
memory.

Having thought about it, I agree with Danilo. Having something that shall wait on green light, but can be interrupted, is no guarantee and
therefore not a feasible solution.

To break down the solution space, these seem to be our options:
   1. We have something (either drm_sched_fini() or a helper, e.g., drm_sched_flush()) that definitely blocks until the pending list
      has become empty.
   2. We have jobs reference-count the scheduler, so the latter can outlive the driver and will be freed some time later.

Can anyone think of a third solution?

Solution #1 has the problem of obviously blocking unconditionally if the driver didn't make sure that all fences will be signaled within reasonable time. In my opinion, this would actually be an advantage, because it will be *very* noticable and force users to repair their driver. The driver *has* to guarantee that all fences will be signaled.
If the driver has to do fishy things, having the blocking outsourced to
the helper drm_sched_flush() would allow them to circumvent that.

Solution #2 has the problem of backend_ops.free_job() potentially using
driver-data after the driver is gone, causing UAF. So with this solutions all drivers would have to be aware of the issue and handle it
through one of DRMs primitives dedicated to such problems.

Currently, all drivers either work around the problem internally or simply ignore it, it seems.

So I'd argue that both solutions are an improvement over the existing situation. My preference would be #1.

Opinions?

P.

Quoted 355 lines
Quoted 354 lines

One issue this solves is that when you send a SIGTERM the tear down
handling
first flushes all the FDs and then closes them.

So if flushing the FDs blocks because the process initiated sending
a
terabyte of data over a 300bps line (for example) you can still throw a
SIGKILL and abort that as well.

If you would block in fops-close() that SIGKILL won't have any effect any
more because by the time close() is called the process is gone and
signals
are already blocked.

And yes when I learned about that issue I was also buffed that handling like
this in the UNIX design is nearly 50 years old and still applies to
today.

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Probably better to make that a separate function, something like
drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers
which
are not sure whether all jobs are done before they start tearing
down.

Yes exactly that's the idea. And give that flush function a return
code so
that it can return -EINTR.

Quoted 108 lines
Quoted 28 lines

Yeah I don't think we should smash this into drm_sched_fini unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because
   drm_sched_fini is called long after all the entities are gone in
   drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably
want to
   nuke the context by setting the tdr timeout to zero (or maybe just
as
   long as context preemption takes to be efficient), and relying on
the
   normal gpu reset flow to handle things. drm_sched_entity_flush
kinda
   does this, except not really and it's a lot more focused on
the
   ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 11 lines

   For this case calling drm_sched_fini() after the 1:1 entity
is gone
   should not find any linger jobs, it would actually be a bug
somewhere if
   there's a job lingering. Maybe a sanity check that there's not just
no
   jobs lingering, but also no entity left would be good here?

The check for lingering ones is in Christian's patch here IISC. At which position would you imagine the check for the entity being
performed?

Quoted 10 lines

- fw scheduler without preemption support. There we kinda need
the
   drm_sched_flush, except blocking in fops->close is not great. So
instead
   I think the following is better:
   1. drm_sched_entity_stopped, which only stops new submissions (for
   paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting new jobs
from userspace to their entities, couldn't they?

Quoted 11 lines

   2. drm_dev_get
   3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b) drm_sched_entity_fini
   + drm_sched_fini c) drm_dev_put

   Note that semantically this implements the refcount in the other
path
   from Phillip:

Quoted 8 lines

https://lore.kernel.org/all/[email protected]/
   Except it doesn't impose refcount on everyone else who doesn't need
it,
   and it doesn't even impose refcounting on drivers that do need it
   because we use drm_sched_flush and a worker to achieve the same.

I indeed wasn't happy with the refcount approach for that reason,
agreed.

Quoted 7 lines

Essentially helper functions for the common use-cases instead of
trying to
solve them all by putting drm_sched_flush as a potentially very
blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired – as
far
as I can see, it is only invoked on driver teardown, so not during
active operation. Teardown doesn't happen that often, and it can
(if
implemented correctly) only block until the driver's code has signaled
the last fences. If that doesn't happen, the block would reveal a
bug.

But don't get me wrong: I don't want to *push* this solution. I just
want to understand when it could become a problem.

Quoted 4 lines

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable step
towards the right direction?

I think that this is the right thing to do, yes.

Quoted 77 lines
Quoted 68 lines
Quoted 68 lines
Quoted 48 lines
Quoted 41 lines
Quoted 25 lines
Quoted 25 lines

When there are still entities with jobs the situation
is
even
worse
since the dma_fences for those jobs can never signal
we can
just
choose between potentially locking up core memory management and
random memory corruption. When drivers really mess it
up
that
well
let them run into a BUG_ON().

Signed-off-by: Christian
König<[email protected]>
---
    drivers/gpu/drm/scheduler/sched_main.c | 19
++++++++++++++++++-

    1 file changed, 18 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 8 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in
the
function's
docstring what the user is expected to have done before
calling the
function.

Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to
be done
before _fini() is mentioned.

Quoted 8 lines

One could also think about providing a hint at how the driver can
do
that. AFAICS the only way for the driver to ensure that is
to
maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee when
callbacks
are processed, e.g. they can be both processed from interrupt
context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 9 lines
Quoted 9 lines

So when a driver (or drm_sched_fini) waits for the last submitted
fence it
actually can be that the drm_sched object still needs to do some
processing.
See the hack in amdgpu_vm_tlb_seq() for more background on the
problem.

Quoted 16 lines

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 9 lines

So I thought this should be fairly easy because of the sched hw/public
fence split: If we wait for both all jobs to finish and for all
the
sched
work/tdr work to finish, and we make sure there's no entity existing
that's not yet stopped we should catch them all?

Unfortunately not.

Even when you do a dma_fence_wait() on the last submission it can still be
that another CPU is executing the callbacks to wake up the scheduler work
item and cleanup the job.

That's one of the reasons why I think the design of keeping the job
alive is
so extremely awkward. The dma_fence as representation of the hw submission
has a much better defined state machine and lifetime.

Regards,
Christian.

Quoted 86 lines
Quoted 86 lines

  Or at least I think
it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing
then I
think that's just a separate concern and we can ignore that here
since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 71 lines

Regards,
Christian.

Quoted 68 lines

P.

Quoted 66 lines

Thanks,
Christian.

Quoted 63 lines

P.

Quoted 61 lines

     drm_sched_wqueue_stop(sched);
+ /*
+ * Tearing down the scheduler wile there are still unprocessed jobs can
+ * lead to use after free issues in the scheduler fence.
+ */
+ WARN_ON(!list_empty(&sched->pending_list)); +
     for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched-

Quoted 1 line

num_rqs;

i++)
{
     struct drm_sched_rq *rq = sched-

Quoted 1 line

sched_rq[i];

     spin_lock(&rq->lock);
- list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list)
+ list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list) {
+ /*
+ * The justification for this
BUG_ON()
is
that tearing
+ * down the scheduler while jobs
are
pending
leaves
+ * dma_fences unsignaled. Since we
have
dependencies
+ * from the core memory management
to
eventually signal
+ * dma_fences this can trivially
lead to
a
system wide
+ * stop because of a locked up
memory
management.
+ */
+ BUG_ON(spsc_queue_count(&s_entity-

Quoted 1 line

job_queue));

+
     /*
     * Prevents reinsertion and marks
job_queue
as idle,
     * it will removed from rq in
drm_sched_entity_fini
     * eventually
     */
     s_entity->stopped = true;
+ }
     spin_unlock(&rq->lock);
     kfree(sched->sched_rq[i]);
     }

#23 lore raw
Quoted 19 lines · Matthew Brost
Quoted 4 lines · Philipp Stanner

Christian, Sima?

Matthew? (+CC)

Sorry catching up here. Internally in Xe we ref count the scheduler as
the scheduler is embedded into our user queue structure (xe_exec_queue).
Jobs ref the queue once they are armed. Upon queue killing we set TDR to
zero which will flush out all jobs / signal all the job's fences. Once
the ref count of queue is zero we do hardware / firmware teardown of the
queue and then finally call drm_sched_fini before freeing the queues memory (and thus the scheduler).

Quoted 7 lines

This flow seems to work pretty well but largely bypasses the scheduler
functions to implement this. Not sure if this flow could be normalized
at all but I would expect usage models of a 1 to 1 relationship between
queue and scheduler to something similar to Xe's flow.

One of the solution I proposed basically does the same: jobs added to gpu_scheduler.pending_list refcount the scheduler.

If we'd go for that solution I assume the separate refcounting could be removed from Xe. For exmaple.

Quoted 3 lines

Maybe we could
write this done as design guideline so other drivers don't have to figure this out - this took me a bit to land on this.

I already improved the documentation a while ago [1], but didn't detail how drivers are supposed to solve this. The reason is that I don't want to encourage more separate solutions while we're working on solving it centrally.

Quoted 2 lines

With that, in general I agree with Christian's patch here complaining about pending jobs if drm_sched_fini is called.

Yes, firing a WARN_ON there is also fine from my POV.

P.

[1] https://lore.kernel.org/dri-devel/[email protected]/

Quoted 553 lines
Quoted 7 lines

Opinions on the below?

tl;dr:
I still think it's a good thing to detectably block in drm_sched_fini(), or at the very least drm_sched_flush(), because then

See above. I don't think drm_sched_fini should block rather just complain this patch does which says 'go fix your driver'.

Matt

Quoted 539 lines

you'll find out that the driver is broken and can repair it.

P.

Quoted 532 lines · Philipp Stanner
Quoted 61 lines · Danilo Krummrich
Quoted 49 lines · Christian König

Am 25.09.24 um 16:53 schrieb Philipp Stanner:

Quoted 29 lines · Simona Vetter
Quoted 28 lines
Quoted 26 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 17 lines
Quoted 15 lines · Christian König

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 14 lines
Quoted 11 lines · Christian König

Tearing down the scheduler with jobs still on
the
pending
list
can
lead to use after free issues. Add a warning if
drivers try
to
destroy a scheduler which still has work pushed
to
the HW.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

Did you have time yet to look into my proposed
waitque-
solution?

I don't remember seeing anything. What have I
missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Interesting approach, I'm just not sure if we can or should
wait in
drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts
is
always a bug, right?

Correct, the question is how to avoid that.

Quoted 7 lines

If so, what are the disadvantages of waiting in drm_sched_fini()?
We
could block buggy drivers as I see it. Which wouldn't be good,
but
could then be fixed on drivers' site.

Quoted 4 lines

Sima explained that pretty well: Don't block in fops->close, do
that in
fops->flush instead.

I agree that we shouldn't block in close(), but this effectively
means that we
need to reference count the scheduler, right?

Quoted 5 lines

Otherwise, if we allow to just skip / interrupt the teardown, we
can
still leak
memory.

Having thought about it, I agree with Danilo. Having something that
shall wait on green light, but can be interrupted, is no guarantee
and
therefore not a feasible solution.

To break down the solution space, these seem to be our options:
   1. We have something (either drm_sched_fini() or a helper, e.g.,
      drm_sched_flush()) that definitely blocks until the pending list
      has become empty.
   2. We have jobs reference-count the scheduler, so the latter can
      outlive the driver and will be freed some time later.

Can anyone think of a third solution?

Solution #1 has the problem of obviously blocking unconditionally if
the driver didn't make sure that all fences will be signaled within
reasonable time. In my opinion, this would actually be an advantage,
because it will be *very* noticable and force users to repair their
driver. The driver *has* to guarantee that all fences will be signaled.
If the driver has to do fishy things, having the blocking outsourced
to
the helper drm_sched_flush() would allow them to circumvent that.

Solution #2 has the problem of backend_ops.free_job() potentially using
driver-data after the driver is gone, causing UAF. So with this solutions all drivers would have to be aware of the issue and handle
it
through one of DRMs primitives dedicated to such problems.

Currently, all drivers either work around the problem internally or
simply ignore it, it seems.

So I'd argue that both solutions are an improvement over the existing
situation. My preference would be #1.

Opinions?

P.

Quoted 406 lines
Quoted 405 lines

One issue this solves is that when you send a SIGTERM the tear
down
handling
first flushes all the FDs and then closes them.

So if flushing the FDs blocks because the process initiated sending
a
terabyte of data over a 300bps line (for example) you can still
throw a
SIGKILL and abort that as well.

If you would block in fops-close() that SIGKILL won't have any
effect any
more because by the time close() is called the process is gone
and
signals
are already blocked.

And yes when I learned about that issue I was also buffed that
handling like
this in the UNIX design is nearly 50 years old and still applies
to
today.

Quoted 10 lines
Quoted 4 lines
Quoted 4 lines

Probably better to make that a separate function,
something
like
drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers
which
are not sure whether all jobs are done before they start tearing
down.

Yes exactly that's the idea. And give that flush function a return
code so
that it can return -EINTR.

Quoted 127 lines
Quoted 32 lines

Yeah I don't think we should smash this into drm_sched_fini
unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because
   drm_sched_fini is called long after all the entities are
gone in
   drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably
want to
   nuke the context by setting the tdr timeout to zero (or
maybe just
as
   long as context preemption takes to be efficient), and relying on
the
   normal gpu reset flow to handle things. drm_sched_entity_flush
kinda
   does this, except not really and it's a lot more focused
on
the
   ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 13 lines

   For this case calling drm_sched_fini() after the 1:1 entity
is gone
   should not find any linger jobs, it would actually be a
bug
somewhere if
   there's a job lingering. Maybe a sanity check that there's
not just
no
   jobs lingering, but also no entity left would be good here?

The check for lingering ones is in Christian's patch here IISC.
At which position would you imagine the check for the entity
being
performed?

Quoted 10 lines

- fw scheduler without preemption support. There we kinda need
the
   drm_sched_flush, except blocking in fops->close is not great. So
instead
   I think the following is better:
   1. drm_sched_entity_stopped, which only stops new submissions (for
   paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting
new jobs
from userspace to their entities, couldn't they?

Quoted 12 lines

   2. drm_dev_get
   3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b) drm_sched_entity_fini
   + drm_sched_fini c) drm_dev_put

   Note that semantically this implements the refcount in the
other
path
   from Phillip:

Quoted 10 lines

https://lore.kernel.org/all/[email protected]/
   Except it doesn't impose refcount on everyone else who doesn't need
it,
   and it doesn't even impose refcounting on drivers that do
need it
   because we use drm_sched_flush and a worker to achieve the
same.

I indeed wasn't happy with the refcount approach for that reason,
agreed.

Quoted 9 lines

Essentially helper functions for the common use-cases instead
of
trying to
solve them all by putting drm_sched_flush as a potentially
very
blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired –
as
far
as I can see, it is only invoked on driver teardown, so not during
active operation. Teardown doesn't happen that often, and it
can
(if
implemented correctly) only block until the driver's code has
signaled
the last fences. If that doesn't happen, the block would reveal
a
bug.

But don't get me wrong: I don't want to *push* this solution. I
just
want to understand when it could become a problem.

Quoted 5 lines

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable
step
towards the right direction?

I think that this is the right thing to do, yes.

Quoted 91 lines
Quoted 80 lines
Quoted 80 lines
Quoted 55 lines
Quoted 47 lines
Quoted 39 lines
Quoted 35 lines

When there are still entities with jobs the
situation
is
even
worse
since the dma_fences for those jobs can never
signal
we can
just
choose between potentially locking up core
memory
management and
random memory corruption. When drivers really
mess
it
up
that
well
let them run into a BUG_ON().

Signed-off-by: Christian
König<[email protected]>
---
    drivers/gpu/drm/scheduler/sched_main.c | 19
++++++++++++++++++-

    1 file changed, 18 insertions(+), 1

deletion(-)

diff --git
a/drivers/gpu/drm/scheduler/sched_main.c
b/drivers/gpu/drm/scheduler/sched_main.c
index f093616fe53c..8a46fab5cdc8 100644

drivers/gpu/drm/scheduler/sched_main.c +0 -0
@@ -1333,17 +1333,34 @@ void
Quoted 3 lines

drm_sched_fini(struct
drm_gpu_scheduler
*sched)

I agree with Sima that it should first be

Quoted 8 lines

documented
in
the
function's
docstring what the user is expected to have done
before
calling the
function.

Quoted 2 lines

Good point, going to update the documentation as
well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to
be done
before _fini() is mentioned.

Quoted 10 lines

One could also think about providing a hint at how the
driver can
do
that. AFAICS the only way for the driver to ensure that
is
to
maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually
currently
impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee
when
callbacks
are processed, e.g. they can be both processed from interrupt
context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 11 lines
Quoted 11 lines

So when a driver (or drm_sched_fini) waits for the last submitted
fence it
actually can be that the drm_sched object still needs to do
some
processing.
See the hack in amdgpu_vm_tlb_seq() for more background on
the
problem.

Quoted 19 lines

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 12 lines

So I thought this should be fairly easy because of the sched
hw/public
fence split: If we wait for both all jobs to finish and for
all
the
sched
work/tdr work to finish, and we make sure there's no entity
existing
that's not yet stopped we should catch them all?

Unfortunately not.

Even when you do a dma_fence_wait() on the last submission it can
still be
that another CPU is executing the callbacks to wake up the scheduler work
item and cleanup the job.

That's one of the reasons why I think the design of keeping the
job
alive is
so extremely awkward. The dma_fence as representation of the hw
submission
has a much better defined state machine and lifetime.

Regards,
Christian.

Quoted 90 lines
Quoted 90 lines

  Or at least I think
it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing
then I
think that's just a separate concern and we can ignore that
here
since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 74 lines

Regards,
Christian.

Quoted 71 lines

P.

Quoted 69 lines

Thanks,
Christian.

Quoted 66 lines

P.

Quoted 64 lines

     drm_sched_wqueue_stop(sched);
+ /*
+ * Tearing down the scheduler wile there are
still
unprocessed jobs can
+ * lead to use after free issues in the
scheduler
fence.
+ */
+ WARN_ON(!list_empty(&sched->pending_list));
+
     for (i = DRM_SCHED_PRIORITY_KERNEL; i <
sched-

Quoted 1 line

num_rqs;

i++)
{
     struct drm_sched_rq *rq = sched-

Quoted 1 line

sched_rq[i];

     spin_lock(&rq->lock);
- list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list)
+ list_for_each_entry(s_entity, &rq-

Quoted 1 line

entities,

list) {
+ /*
+ * The justification for this
BUG_ON()
is
that tearing
+ * down the scheduler while jobs
are
pending
leaves
+ * dma_fences unsignaled. Since we
have
dependencies
+ * from the core memory management
to
eventually signal
+ * dma_fences this can trivially
lead to
a
system wide
+ * stop because of a locked up
memory
management.
+ */
+ BUG_ON(spsc_queue_count(&s_entity-

Quoted 1 line

job_queue));

+
     /*
     * Prevents reinsertion and marks
job_queue
as idle,
     * it will removed from rq in
drm_sched_entity_fini
     * eventually
     */
     s_entity->stopped = true;
+ }
     spin_unlock(&rq->lock);
     kfree(sched->sched_rq[i]);
     }

maintainer
#24 lore raw
Quoted 28 lines · Philipp Stanner
Quoted 19 lines · Matthew Brost
Quoted 4 lines · Philipp Stanner

Christian, Sima?

Matthew? (+CC)

Sorry catching up here. Internally in Xe we ref count the scheduler as
the scheduler is embedded into our user queue structure (xe_exec_queue).
Jobs ref the queue once they are armed. Upon queue killing we set TDR to
zero which will flush out all jobs / signal all the job's fences. Once
the ref count of queue is zero we do hardware / firmware teardown of the
queue and then finally call drm_sched_fini before freeing the queues memory (and thus the scheduler).

Quoted 7 lines

This flow seems to work pretty well but largely bypasses the scheduler
functions to implement this. Not sure if this flow could be normalized
at all but I would expect usage models of a 1 to 1 relationship between
queue and scheduler to something similar to Xe's flow.

Quoted 6 lines

One of the solution I proposed basically does the same: jobs added to gpu_scheduler.pending_list refcount the scheduler.

If we'd go for that solution I assume the separate refcounting could be removed from Xe. For exmaple.

That should work. We likely need a vfunc for when the refcount goes to zero which could either free the scheduler or initiate hw / hw teardown process. In Xe the queue and scheduler actually lives for a while after the refcount is zero due to several ping / pongs with the fw. Also long as we can still support that, moving the refcount from our queues to the scheduler seems reasonable. Ofc we need buy in from the other scheduler use cases too.

Quoted 9 lines
Quoted 4 lines

Maybe we could
write this done as design guideline so other drivers don't have to figure this out - this took me a bit to land on this.

I already improved the documentation a while ago [1], but didn't detail how drivers are supposed to solve this. The reason is that I don't want to encourage more separate solutions while we're working on solving it centrally.

Let me read this and add comments there if needed.

Matt

Quoted 566 lines
Quoted 3 lines

With that, in general I agree with Christian's patch here complaining about pending jobs if drm_sched_fini is called.

Yes, firing a WARN_ON there is also fine from my POV.

P.

[1] https://lore.kernel.org/dri-devel/[email protected]/

Quoted 553 lines
Quoted 7 lines

Opinions on the below?

tl;dr:
I still think it's a good thing to detectably block in drm_sched_fini(), or at the very least drm_sched_flush(), because then

See above. I don't think drm_sched_fini should block rather just complain this patch does which says 'go fix your driver'.

Matt

Quoted 539 lines

you'll find out that the driver is broken and can repair it.

P.

Quoted 532 lines · Philipp Stanner
Quoted 61 lines · Danilo Krummrich
Quoted 49 lines · Christian König

Am 25.09.24 um 16:53 schrieb Philipp Stanner:

Quoted 29 lines · Simona Vetter
Quoted 28 lines
Quoted 26 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 17 lines
Quoted 15 lines · Christian König

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 14 lines
Quoted 11 lines · Christian König

Tearing down the scheduler with jobs still on
the
pending
list
can
lead to use after free issues. Add a warning if
drivers try
to
destroy a scheduler which still has work pushed
to
the HW.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

Did you have time yet to look into my proposed
waitque-
solution?

I don't remember seeing anything. What have I
missed?

https://lore.kernel.org/all/[email protected]/

Mhm, I didn't got that in my inbox for some reason.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Interesting approach, I'm just not sure if we can or should
wait in
drm_sched_fini().

We do agree that jobs still pending when drm_sched_fini() starts
is
always a bug, right?

Correct, the question is how to avoid that.

Quoted 7 lines

If so, what are the disadvantages of waiting in drm_sched_fini()?
We
could block buggy drivers as I see it. Which wouldn't be good,
but
could then be fixed on drivers' site.

Quoted 4 lines

Sima explained that pretty well: Don't block in fops->close, do
that in
fops->flush instead.

I agree that we shouldn't block in close(), but this effectively
means that we
need to reference count the scheduler, right?

Quoted 5 lines

Otherwise, if we allow to just skip / interrupt the teardown, we
can
still leak
memory.

Having thought about it, I agree with Danilo. Having something that
shall wait on green light, but can be interrupted, is no guarantee
and
therefore not a feasible solution.

To break down the solution space, these seem to be our options:
   1. We have something (either drm_sched_fini() or a helper, e.g.,
      drm_sched_flush()) that definitely blocks until the pending list
      has become empty.
   2. We have jobs reference-count the scheduler, so the latter can
      outlive the driver and will be freed some time later.

Can anyone think of a third solution?

Solution #1 has the problem of obviously blocking unconditionally if
the driver didn't make sure that all fences will be signaled within
reasonable time. In my opinion, this would actually be an advantage,
because it will be *very* noticable and force users to repair their
driver. The driver *has* to guarantee that all fences will be signaled.
If the driver has to do fishy things, having the blocking outsourced
to
the helper drm_sched_flush() would allow them to circumvent that.

Solution #2 has the problem of backend_ops.free_job() potentially using
driver-data after the driver is gone, causing UAF. So with this solutions all drivers would have to be aware of the issue and handle
it
through one of DRMs primitives dedicated to such problems.

Currently, all drivers either work around the problem internally or
simply ignore it, it seems.

So I'd argue that both solutions are an improvement over the existing
situation. My preference would be #1.

Opinions?

P.

Quoted 406 lines
Quoted 405 lines

One issue this solves is that when you send a SIGTERM the tear
down
handling
first flushes all the FDs and then closes them.

So if flushing the FDs blocks because the process initiated sending
a
terabyte of data over a 300bps line (for example) you can still
throw a
SIGKILL and abort that as well.

If you would block in fops-close() that SIGKILL won't have any
effect any
more because by the time close() is called the process is gone
and
signals
are already blocked.

And yes when I learned about that issue I was also buffed that
handling like
this in the UNIX design is nearly 50 years old and still applies
to
today.

Quoted 10 lines
Quoted 4 lines
Quoted 4 lines

Probably better to make that a separate function,
something
like
drm_sched_flush() or similar.

We could do that. Such a function could then be called by drivers
which
are not sure whether all jobs are done before they start tearing
down.

Yes exactly that's the idea. And give that flush function a return
code so
that it can return -EINTR.

Quoted 127 lines
Quoted 32 lines

Yeah I don't think we should smash this into drm_sched_fini
unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because
   drm_sched_fini is called long after all the entities are
gone in
   drm_device cleanup.

- fw scheduler hardware with preemption support. There we probably
want to
   nuke the context by setting the tdr timeout to zero (or
maybe just
as
   long as context preemption takes to be efficient), and relying on
the
   normal gpu reset flow to handle things. drm_sched_entity_flush
kinda
   does this, except not really and it's a lot more focused
on
the
   ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 13 lines

   For this case calling drm_sched_fini() after the 1:1 entity
is gone
   should not find any linger jobs, it would actually be a
bug
somewhere if
   there's a job lingering. Maybe a sanity check that there's
not just
no
   jobs lingering, but also no entity left would be good here?

The check for lingering ones is in Christian's patch here IISC.
At which position would you imagine the check for the entity
being
performed?

Quoted 10 lines

- fw scheduler without preemption support. There we kinda need
the
   drm_sched_flush, except blocking in fops->close is not great. So
instead
   I think the following is better:
   1. drm_sched_entity_stopped, which only stops new submissions (for
   paranoia) but doesn't tear down the entity

Who would call that function?
Drivers using it voluntarily could just as well stop accepting
new jobs
from userspace to their entities, couldn't they?

Quoted 12 lines

   2. drm_dev_get
   3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b) drm_sched_entity_fini
   + drm_sched_fini c) drm_dev_put

   Note that semantically this implements the refcount in the
other
path
   from Phillip:

Quoted 10 lines

https://lore.kernel.org/all/[email protected]/
   Except it doesn't impose refcount on everyone else who doesn't need
it,
   and it doesn't even impose refcounting on drivers that do
need it
   because we use drm_sched_flush and a worker to achieve the
same.

I indeed wasn't happy with the refcount approach for that reason,
agreed.

Quoted 9 lines

Essentially helper functions for the common use-cases instead
of
trying to
solve them all by putting drm_sched_flush as a potentially
very
blocking
function into drm_sched_fini.

I'm still not able to see why it blocking would be undesired –
as
far
as I can see, it is only invoked on driver teardown, so not during
active operation. Teardown doesn't happen that often, and it
can
(if
implemented correctly) only block until the driver's code has
signaled
the last fences. If that doesn't happen, the block would reveal
a
bug.

But don't get me wrong: I don't want to *push* this solution. I
just
want to understand when it could become a problem.

Quoted 5 lines

Wouldn't an explicitly blocking, separate function like drm_sched_flush() or drm_sched_fini_flush() be a small, doable
step
towards the right direction?

I think that this is the right thing to do, yes.

Quoted 91 lines
Quoted 80 lines
Quoted 80 lines
Quoted 55 lines
Quoted 47 lines
Quoted 39 lines
Quoted 38 lines

When there are still entities with jobs the
situation
is
even
worse
since the dma_fences for those jobs can never
signal
we can
just
choose between potentially locking up core
memory
management and
random memory corruption. When drivers really
mess
it
up
that
well
let them run into a BUG_ON().

Signed-off-by: Christian
König<[email protected]>
---
    drivers/gpu/drm/scheduler/sched_main.c | 19
++++++++++++++++++-
    1 file changed, 18 insertions(+), 1
deletion(-)

diff --git
a/drivers/gpu/drm/scheduler/sched_main.c
b/drivers/gpu/drm/scheduler/sched_main.c
index f093616fe53c..8a46fab5cdc8 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -1333,17 +1333,34 @@ void
drm_sched_fini(struct
drm_gpu_scheduler
*sched)

I agree with Sima that it should first be

Quoted 8 lines

documented
in
the
function's
docstring what the user is expected to have done
before
calling the
function.

Quoted 2 lines

Good point, going to update the documentation as
well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to
be done
before _fini() is mentioned.

Quoted 10 lines

One could also think about providing a hint at how the
driver can
do
that. AFAICS the only way for the driver to ensure that
is
to
maintain
its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually
currently
impossible to do
this fully cleanly.

The problem is that the dma_fence object gives no guarantee
when
callbacks
are processed, e.g. they can be both processed from interrupt
context as
well as from a CPU which called dma_fence_is_signaled().

Quoted 11 lines
Quoted 11 lines

So when a driver (or drm_sched_fini) waits for the last submitted
fence it
actually can be that the drm_sched object still needs to do
some
processing.
See the hack in amdgpu_vm_tlb_seq() for more background on
the
problem.

Quoted 19 lines

Oh dear ^^'
We better work towards fixing that centrally

Thanks,
P.

Quoted 12 lines

So I thought this should be fairly easy because of the sched
hw/public
fence split: If we wait for both all jobs to finish and for
all
the
sched
work/tdr work to finish, and we make sure there's no entity
existing
that's not yet stopped we should catch them all?

Unfortunately not.

Even when you do a dma_fence_wait() on the last submission it can
still be
that another CPU is executing the callbacks to wake up the scheduler work
item and cleanup the job.

That's one of the reasons why I think the design of keeping the
job
alive is
so extremely awkward. The dma_fence as representation of the hw
submission
has a much better defined state machine and lifetime.

Regards,
Christian.

Quoted 90 lines
Quoted 90 lines

  Or at least I think
it's
a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing
then I
think that's just a separate concern and we can ignore that
here
since the
fences themselves will till get rcu-delay freed even if drm_sched_fini has
finished.
-Sima

Quoted 74 lines

Regards,
Christian.

Quoted 71 lines

P.

Quoted 69 lines

Thanks,
Christian.

Quoted 66 lines

P.

Quoted 64 lines

     drm_sched_wqueue_stop(sched);
+ /*
+ * Tearing down the scheduler wile there are
still
unprocessed jobs can
+ * lead to use after free issues in the
scheduler
fence.
+ */
+ WARN_ON(!list_empty(&sched->pending_list));
+
     for (i = DRM_SCHED_PRIORITY_KERNEL; i <
sched-
num_rqs;
i++)
{
     struct drm_sched_rq *rq = sched-
sched_rq[i];
     spin_lock(&rq->lock);
- list_for_each_entry(s_entity, &rq-
entities,
list)
+ list_for_each_entry(s_entity, &rq-
entities,
list) {
+ /*
+ * The justification for this
BUG_ON()
is
that tearing
+ * down the scheduler while jobs
are
pending
leaves
+ * dma_fences unsignaled. Since we
have
dependencies
+ * from the core memory management
to
eventually signal
+ * dma_fences this can trivially
lead to
a
system wide
+ * stop because of a locked up
memory
management.
+ */
+ BUG_ON(spsc_queue_count(&s_entity-
job_queue));
+
     /*
     * Prevents reinsertion and marks
job_queue
as idle,
     * it will removed from rq in
drm_sched_entity_fini
     * eventually
     */
     s_entity->stopped = true;
+ }
     spin_unlock(&rq->lock);
     kfree(sched->sched_rq[i]);
     }

maintainer
#25 lore raw
In reply to Simona Vetter
Quoted 21 lines · Simona Vetter
Quoted 12 lines · Christian König

Am 20.09.24 um 15:26 schrieb Philipp Stanner:

Quoted 10 lines · Christian König
Quoted 8 lines

Am 20.09.24 um 10:57 schrieb Philipp Stanner:

Quoted 6 lines · Christian König
Quoted 4 lines

Tearing down the scheduler with jobs still on the pending list can
lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

Did you have time yet to look into my proposed waitque-solution?

I don't remember seeing anything. What have I missed?

https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

Mhm, I didn't got that in my inbox for some reason.

Interesting approach, I'm just not sure if we can or should wait in drm_sched_fini().

Probably better to make that a separate function, something like drm_sched_flush() or similar.

Quoted 6 lines

Yeah I don't think we should smash this into drm_sched_fini unconditionally. I think conceptually there's about three cases:

- Ringbuffer schedules. Probably want everything as-is, because drm_sched_fini is called long after all the entities are gone in drm_device cleanup.

Even if entities are long gone we can still have jobs on the pending list of the scheduler. Calling drm_sched_fini() would still leak those jobs.

It's just that for this case it's unlikely, but I don't think we want to rely on "unlikely".

Do I miss anything?

Quoted 8 lines

- fw scheduler hardware with preemption support. There we probably want to nuke the context by setting the tdr timeout to zero (or maybe just as long as context preemption takes to be efficient), and relying on the normal gpu reset flow to handle things. drm_sched_entity_flush kinda does this, except not really and it's a lot more focused on the ringbuffer context. So maybe we want a new drm_sched_entity_kill.

Quoted 4 lines

For this case calling drm_sched_fini() after the 1:1 entity is gone should not find any linger jobs, it would actually be a bug somewhere if there's a job lingering. Maybe a sanity check that there's not just no jobs lingering, but also no entity left would be good here?

This is a timing assumption too, isn't it? What if we reach drm_sched_fini() quicker than all the free() callbacks from the jobs on the pending list are processed?

I know Xe uses the "tdr to zero trick", so I'm not aware of all implications -- in Nouveau I had to do it differently.

Quoted 9 lines

- fw scheduler without preemption support. There we kinda need the drm_sched_flush, except blocking in fops->close is not great. So instead I think the following is better:
1. drm_sched_entity_stopped, which only stops new submissions (for paranoia) but doesn't tear down the entity
2. drm_dev_get
3. launch a worker which does a) drm_sched_flush (or drm_sched_entity_flush or whatever we call it) b) drm_sched_entity_fini

I think it would be drm_sched_flush(); the jobs on the pending are not associated with any entity any more. See also 96c7c2f4d5bd ("drm/scheduler: set entity to NULL in drm_sched_entity_pop_job()").

Quoted 141 lines

+ drm_sched_fini c) drm_dev_put

Note that semantically this implements the refcount in the other path from Phillip:

https://lore.kernel.org/all/[email protected]/

Except it doesn't impose refcount on everyone else who doesn't need it, and it doesn't even impose refcounting on drivers that do need it because we use drm_sched_flush and a worker to achieve the same.

Essentially helper functions for the common use-cases instead of trying to solve them all by putting drm_sched_flush as a potentially very blocking function into drm_sched_fini.

Quoted 37 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 19 lines

When there are still entities with jobs the situation is even worse
since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well
let them run into a BUG_ON().

---
   drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-
   1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct
Quoted 2 lines

drm_gpu_scheduler
*sched)

I agree with Sima that it should first be documented in the function's
docstring what the user is expected to have done before calling the function.

Quoted 8 lines
Quoted 1 line

Good point, going to update the documentation as well.

Cool thing, thx.
Would be great if everything (not totally trivial) necessary to be done before _fini() is mentioned.

One could also think about providing a hint at how the driver can do that. AFAICS the only way for the driver to ensure that is to maintain its own, separate list of submitted jobs.

Even with a duplicated pending list it's actually currently impossible to do this fully cleanly.

Quoted 7 lines

The problem is that the dma_fence object gives no guarantee when callbacks are processed, e.g. they can be both processed from interrupt context as well as from a CPU which called dma_fence_is_signaled().

So when a driver (or drm_sched_fini) waits for the last submitted fence it actually can be that the drm_sched object still needs to do some processing. See the hack in amdgpu_vm_tlb_seq() for more background on the problem.

So I thought this should be fairly easy because of the sched hw/public fence split: If we wait for both all jobs to finish and for all the sched work/tdr work to finish, and we make sure there's no entity existing that's not yet stopped we should catch them all? Or at least I think it's a bug if any other code even tries to touch the hw fence.

If you have any other driver code which relies on the rcu freeing then I think that's just a separate concern and we can ignore that here since the fences themselves will till get rcu-delay freed even if drm_sched_fini has finished.
-Sima

Quoted 62 lines

Regards,
Christian.

Quoted 58 lines

P.

Quoted 55 lines

Thanks,
Christian.

Quoted 52 lines

P.

Quoted 50 lines

   drm_sched_wqueue_stop(sched);

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	/*
+	 * Tearing down the scheduler wile there are still

unprocessed jobs can

drivers/gpu/drm/scheduler/sched_main.c +4 -0
+	 * lead to use after free issues in the scheduler fence.
+	 */
+	WARN_ON(!list_empty(&sched->pending_list));
+
   	for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs;

i++)
{
   struct drm_sched_rq *rq = sched->sched_rq[i]; spin_lock(&rq->lock);

drivers/gpu/drm/scheduler/sched_main.c +0 -1
-		list_for_each_entry(s_entity, &rq->entities,

list)

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		list_for_each_entry(s_entity, &rq->entities,

list) {

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			/*
+			 * The justification for this BUG_ON()

is
that tearing

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * down the scheduler while jobs are

pending
leaves

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences unsignaled. Since we have

dependencies

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * from the core memory management to

eventually signal

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * dma_fences this can trivially lead to

a
system wide

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * stop because of a locked up memory

management.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			 */
+			BUG_ON(spsc_queue_count(&s_entity-
Quoted 1 line

job_queue));

+
   /*
   * Prevents reinsertion and marks
job_queue
as idle,
   * it will removed from rq in
drm_sched_entity_fini
   * eventually
   */
   s_entity->stopped = true;

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		}
   		spin_unlock(&rq->lock);
   		kfree(sched->sched_rq[i]);
   	}
Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
maintainer
#26 lore raw
In reply to Christian König
Quoted 10 lines · Christian König

Tearing down the scheduler with jobs still on the pending list can lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

When there are still entities with jobs the situation is even worse since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well let them run into a BUG_ON().

I've been talking a bit to Phillip about this offline.

I'm not sure we should ever be BUG_ON here, I think it might be an extreme answer, considering we are saying blocking userspace to let things finish is bad, I think hitting this would be much worse.

I'd rather we WARN_ON, and consider just saying screw it and block userspace close.

If we really want to avoid the hang on close possibility (though I'm mostly fine with that) then I think Sima's option to just keep a reference on the driver, let userspace close and try and clean things up on fences in the driver later.

I think this should be at least good for rust lifetimes.

Having an explicit memory leak is bad, having a managed memory leak is less bad, because at least all the memory is still pointed to by something and managed, at a guess we'd have to keep the whole driver and scheduler around, to avoid having to make special free functions. Unless there is some concept like TTM ghost objects we could get away with, but I think having to signal the fence means we should keep all the pieces.

Dave.

Quoted 49 lines
---
 drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +18 -2
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
 
        drm_sched_wqueue_stop(sched);
 
+       /*
+        * Tearing down the scheduler wile there are still unprocessed jobs can
+        * lead to use after free issues in the scheduler fence.
+        */
+       WARN_ON(!list_empty(&sched->pending_list));
+
        for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
                struct drm_sched_rq *rq = sched->sched_rq[i];
 
                spin_lock(&rq->lock);
-               list_for_each_entry(s_entity, &rq->entities, list)
+               list_for_each_entry(s_entity, &rq->entities, list) {
+                       /*
+                        * The justification for this BUG_ON() is that tearing
+                        * down the scheduler while jobs are pending leaves
+                        * dma_fences unsignaled. Since we have dependencies
+                        * from the core memory management to eventually signal
+                        * dma_fences this can trivially lead to a system wide
+                        * stop because of a locked up memory management.
+                        */
+                       BUG_ON(spsc_queue_count(&s_entity->job_queue));
+
                        /*
                         * Prevents reinsertion and marks job_queue as idle,
                         * it will removed from rq in drm_sched_entity_fini
                         * eventually
                         */
                        s_entity->stopped = true;
+               }
                spin_unlock(&rq->lock);
                kfree(sched->sched_rq[i]);
        }
--

2.34.1

author reviewer
#27 lore raw

Am 08.11.24 um 05:00 schrieb Dave Airlie:

Quoted 11 lines
Quoted 9 lines · Christian König

Tearing down the scheduler with jobs still on the pending list can lead to use after free issues. Add a warning if drivers try to destroy a scheduler which still has work pushed to the HW.

When there are still entities with jobs the situation is even worse since the dma_fences for those jobs can never signal we can just choose between potentially locking up core memory management and random memory corruption. When drivers really mess it up that well let them run into a BUG_ON().

Quoted 5 lines

I've been talking a bit to Phillip about this offline.

I'm not sure we should ever be BUG_ON here, I think it might be an extreme answer, considering we are saying blocking userspace to let things finish is bad, I think hitting this would be much worse.

Yeah, completely agree that this is the most extreme response.

The problem is that the scheduler doesn't have much of a choice in this moment any more. What we can do is the following:

1. Try to block for the queued up jobs in the entities to be processed and signaling their hardware fence.

    There is no guarantee that this won't deadlock and we are potentially blocking a SIGKILL here. We already tried it before and that turned out to be quite unstable.

2. Signal all pending fences without actually executing anything.

    Because of the dma_fence design, pipe-lining work and only keeping the latest fence for each context in the containers that can potentially lead to random memory corruptions.

3. Don't signal anything, just free up all jobs.

    That can trivially result in the core memory management waiting forever for things to make progress. Leading to complete system stops.

So the choice you have are all really bad for the system as a whole.

Quoted 2 lines

I'd rather we WARN_ON, and consider just saying screw it and block userspace close.

Going to make that a WARN_ON() if you insist, but IIRC Linus or Greg once summarized it as "A BUG() is only justified if you prevent even worse things from happening". If you ask me that's exactly the case here.

Quoted 6 lines

If we really want to avoid the hang on close possibility (though I'm mostly fine with that) then I think Sima's option to just keep a reference on the driver, let userspace close and try and clean things up on fences in the driver later.

I think this should be at least good for rust lifetimes.

The problem with the rust lifetime is that it got the direction of the dependency wrong.

A dma_fence represents an operation on the hardware, e.g. a direct access to some memory. This means that the dma_fence can only signal if the hardware tells the driver that the operation is completed.

If a dma_fence should signals because some object is not referenced any more, for example because userspace closed it, than that clearly points to a fundamentally broken design.

Quoted 7 lines

Having an explicit memory leak is bad, having a managed memory leak is less bad, because at least all the memory is still pointed to by something and managed, at a guess we'd have to keep the whole driver and scheduler around, to avoid having to make special free functions. Unless there is some concept like TTM ghost objects we could get away with, but I think having to signal the fence means we should keep all the pieces.

Well I think memory leaks are more or less harmless. What we really really need to prevent are random memory corruptions.

Those can run undetected under the radar for a very long time and cause massive damage without anybody being able to pinpoint where corruptions came from.

Regards,
Christian.

Quoted 51 lines

Dave.

Quoted 48 lines
---
  drivers/gpu/drm/scheduler/sched_main.c | 19 ++++++++++++++++++-
  1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_main.c +18 -2
index f093616fe53c..8a46fab5cdc8 100644
@@ -1333,17 +1333,34 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
 
         drm_sched_wqueue_stop(sched);
 
+       /*
+        * Tearing down the scheduler wile there are still unprocessed jobs can
+        * lead to use after free issues in the scheduler fence.
+        */
+       WARN_ON(!list_empty(&sched->pending_list));
+
         for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {
                 struct drm_sched_rq *rq = sched->sched_rq[i];
 
                 spin_lock(&rq->lock);
-               list_for_each_entry(s_entity, &rq->entities, list)
+               list_for_each_entry(s_entity, &rq->entities, list) {
+                       /*
+                        * The justification for this BUG_ON() is that tearing
+                        * down the scheduler while jobs are pending leaves
+                        * dma_fences unsignaled. Since we have dependencies
+                        * from the core memory management to eventually signal
+                        * dma_fences this can trivially lead to a system wide
+                        * stop because of a locked up memory management.
+                        */
+                       BUG_ON(spsc_queue_count(&s_entity->job_queue));
+
                         /*
                          * Prevents reinsertion and marks job_queue as idle,
                          * it will removed from rq in drm_sched_entity_fini
                          * eventually
                          */
                         s_entity->stopped = true;
+               }
                 spin_unlock(&rq->lock);
                 kfree(sched->sched_rq[i]);
         }
--

2.34.1