Skip to contentSkip to conversation

Fixing AMDGPUs gang submit error handling

PATCH In review Christian König 5 participants 18 messages May 15, 2025 – May 21, 2025

Subsystems (4)
lore
Share & tools
lore

Christian König

author reviewer
#0 lore raw

Hi guys,

third revision of this patch set. I've re-worked the interface completely this time since my previous assumptions on how the reservation function of the xarray work weren't correct at all.

I also added a test case to make sure I've got it right this time.

Please review and comment,
Christian.

author reviewer
#1 lore raw

PATCH 1/3 drm/sched: add drm_sched_prealloc_dependency_slots v3

Sometimes drivers need to be able to submit multiple jobs which depend on each other to different schedulers at the same time, but using drm_sched_job_add_dependency() can't fail any more after the first job is initialized.

This function preallocate memory for dependency slots so that no ENOMEM can come later while adding dependencies.

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated deps

---
 drivers/gpu/drm/scheduler/sched_main.c | 45 ++++++++++++++++++++++++++
 include/drm/gpu_scheduler.h            |  4 +++
 2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +45 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct drm_sched_job *job)
 }
 EXPORT_SYMBOL(drm_sched_job_arm);
 
+/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs which depend on
+ * each other to different schedulers at the same time, but using
+ * drm_sched_job_add_dependency() can't fail any more after the first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so that no ENOMEM can
+ * come later while adding dependencies. The index of the preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job *job,
+					   u32 *id)
+{
+	return xa_alloc(&job->dependencies, id, NULL, xa_limit_32b, GFP_KERNEL);
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to preallocated slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32 id,
+				    struct dma_fence *fence)
+{
+	fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);
+	/*
+	 * Be defensive just in case driver messed it up and used preallocated
+	 * slot twice.
+	 */
+	if (WARN_ON(fence))
+		dma_fence_put(fence);
+}
+EXPORT_SYMBOL(drm_sched_job_add_prealloc_dep);
+
 /**
  * drm_sched_job_add_dependency - adds the fence as a job dependency
  * @job: scheduler job to add the dependencies to
include/drm/gpu_scheduler.h +4 -0
index d860db087ea5..0286e0934317 100644
@@ -632,6 +632,10 @@ int drm_sched_job_init(struct drm_sched_job *job,
 		       u32 credits, void *owner);
 void drm_sched_job_arm(struct drm_sched_job *job);
 void drm_sched_entity_push_job(struct drm_sched_job *sched_job);
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job *job,
+					   u32 *id);
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32 id,
+				    struct dma_fence *fence);
 int drm_sched_job_add_dependency(struct drm_sched_job *job,
 				 struct dma_fence *fence);
 int drm_sched_job_add_syncobj_dependency(struct drm_sched_job *job,
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 51 lines · Christian König

Sometimes drivers need to be able to submit multiple jobs which depend on each other to different schedulers at the same time, but using drm_sched_job_add_dependency() can't fail any more after the first job is initialized.

This function preallocate memory for dependency slots so that no ENOMEM can come later while adding dependencies.

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated deps

---
  drivers/gpu/drm/scheduler/sched_main.c | 45 ++++++++++++++++++++++++++
  include/drm/gpu_scheduler.h            |  4 +++
  2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +26 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct drm_sched_job *job)
  }
  EXPORT_SYMBOL(drm_sched_job_arm);
 
+/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs which depend on
+ * each other to different schedulers at the same time, but using
+ * drm_sched_job_add_dependency() can't fail any more after the first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so that no ENOMEM can
+ * come later while adding dependencies. The index of the preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job *job,
+					   u32 *id)
+{
+	return xa_alloc(&job->dependencies, id, NULL, xa_limit_32b, GFP_KERNEL);
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to preallocated slot

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_job_arm(struct drm_sched_job *job) · line 896

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +10 -0
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32 id,
+				    struct dma_fence *fence)
+{
+	fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and is NULL?

Also, if someone preallocates and does not consume the slot will that confuse the iteration in drm_sched_job_dependency()?

Regards,

Tvrtko

Quoted 27 lines
drivers/gpu/drm/scheduler/sched_main.c +9 -0
+	/*
+	 * Be defensive just in case driver messed it up and used preallocated
+	 * slot twice.
+	 */
+	if (WARN_ON(fence))
+		dma_fence_put(fence);
+}
+EXPORT_SYMBOL(drm_sched_job_add_prealloc_dep);
+
  /**
   * drm_sched_job_add_dependency - adds the fence as a job dependency
   * @job: scheduler job to add the dependencies to
include/drm/gpu_scheduler.h +4 -0
index d860db087ea5..0286e0934317 100644
@@ -632,6 +632,10 @@ int drm_sched_job_init(struct drm_sched_job *job,
  		       u32 credits, void *owner);
  void drm_sched_job_arm(struct drm_sched_job *job);
  void drm_sched_entity_push_job(struct drm_sched_job *sched_job);
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job *job,
+					   u32 *id);
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32 id,
+				    struct dma_fence *fence);
  int drm_sched_job_add_dependency(struct drm_sched_job *job,
  				 struct dma_fence *fence);
  int drm_sched_job_add_syncobj_dependency(struct drm_sched_job *job,
#3 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 9 lines · Tvrtko Ursulin
Quoted 7 lines · Christian König

Sometimes drivers need to be able to submit multiple jobs which depend on
each other to different schedulers at the same time, but using drm_sched_job_add_dependency() can't fail any more after the first job is
initialized.

Quoted 7 lines
Quoted 7 lines

This function preallocate memory for dependency slots so that no ENOMEM
can come later while adding dependencies.

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated deps

I think we agreed to not put change logs into commit messages anymore :)

They aren't useful for any reader. Who needs the changelog afterwards can retreive it through the mail thread link that we add.

Quoted 59 lines
Quoted 59 lines

---
  drivers/gpu/drm/scheduler/sched_main.c | 45
++++++++++++++++++++++++++
  include/drm/gpu_scheduler.h            |  4 +++

  2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct drm_sched_job
 *job)
   }
   EXPORT_SYMBOL(drm_sched_job_arm);


+/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs which depend on
+ * each other to different schedulers at the same time, but using
+ * drm_sched_job_add_dependency() can't fail any more after the first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so that no ENOMEM can
+ * come later while adding dependencies. The index of the preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job *job,

drivers/gpu/drm/scheduler/sched_main.c +3 -0
+					   u32 *id)
+{
+	return xa_alloc(&job->dependencies, id, NULL,

xa_limit_32b, GFP_KERNEL);

drivers/gpu/drm/scheduler/sched_main.c +5 -0
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to preallocated

slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */

Quoted 8 lines
Quoted 6 lines

+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32 id,

drivers/gpu/drm/scheduler/sched_main.c +3 -0
+				    struct dma_fence *fence)
+{
+	fence = xa_store(&job->dependencies, id, fence,

GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and is NULL?

You

Quoted 3 lines

Also, if someone preallocates and does not consume the slot will that confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

Yes, it would. All operations simply give you NULL for those slots. So seems to me you have to check for NULL wherever a preallocated slot might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't answer our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't such a bad idea after all?

If we go for the NULL approach, it's probably the only sane way to then check for NULL wherever dependencies are accessed :(

Opinions?

P.

Quoted 40 lines

Regards,

Tvrtko

Quoted 34 lines
drivers/gpu/drm/scheduler/sched_main.c +2 -0
+	/*
+	 * Be defensive just in case driver messed it up and used

preallocated

drivers/gpu/drm/scheduler/sched_main.c +7 -0
+	 * slot twice.
+	 */
+	if (WARN_ON(fence))
+		dma_fence_put(fence);
+}
+EXPORT_SYMBOL(drm_sched_job_add_prealloc_dep);
+

  /**
   * drm_sched_job_add_dependency - adds the fence as a job dependency
   * @job: scheduler job to add the dependencies to

include/drm/gpu_scheduler.h +4 -0
index d860db087ea5..0286e0934317 100644
@@ -632,6 +632,10 @@ int drm_sched_job_init(struct drm_sched_job
 *job,
   		       u32 credits, void *owner);
   void drm_sched_job_arm(struct drm_sched_job *job);
   void drm_sched_entity_push_job(struct drm_sched_job *sched_job);
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job
 *job,
+					   u32 *id);
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32
 id,
+				    struct dma_fence *fence);

  int drm_sched_job_add_dependency(struct drm_sched_job *job, struct dma_fence *fence); int drm_sched_job_add_syncobj_dependency(struct drm_sched_job *job,

#4 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 92 lines · Philipp Stanner
Quoted 9 lines · Tvrtko Ursulin
Quoted 7 lines · Christian König

Sometimes drivers need to be able to submit multiple jobs which depend on
each other to different schedulers at the same time, but using drm_sched_job_add_dependency() can't fail any more after the first job is
initialized.

Quoted 7 lines
Quoted 7 lines

This function preallocate memory for dependency slots so that no ENOMEM
can come later while adding dependencies.

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated deps

I think we agreed to not put change logs into commit messages anymore :)

They aren't useful for any reader. Who needs the changelog afterwards can retreive it through the mail thread link that we add.

Quoted 59 lines
Quoted 59 lines
---
   drivers/gpu/drm/scheduler/sched_main.c | 45
++++++++++++++++++++++++++
   include/drm/gpu_scheduler.h            |  4 +++
   2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +2 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct drm_sched_job
 *job)
   }
   EXPORT_SYMBOL(drm_sched_job_arm);
 
+/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on adding

dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs which depend on
+ * each other to different schedulers at the same time, but using
+ * drm_sched_job_add_dependency() can't fail any more after the first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so that no ENOMEM can
+ * come later while adding dependencies. The index of the preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job *job,

drivers/gpu/drm/scheduler/sched_main.c +3 -0
+					   u32 *id)
+{
+	return xa_alloc(&job->dependencies, id, NULL,

xa_limit_32b, GFP_KERNEL);

drivers/gpu/drm/scheduler/sched_main.c +5 -0
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to preallocated

slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */

Quoted 8 lines
Quoted 6 lines

+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32 id,

drivers/gpu/drm/scheduler/sched_main.c +3 -0
+				    struct dma_fence *fence)
+{
+	fence = xa_store(&job->dependencies, id, fence,

GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and is NULL?

Quoted 1 line

You

Hm?

Quoted 5 lines
Quoted 3 lines

Also, if someone preallocates and does not consume the slot will that confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because that looked it would skip dependencies upon encountering an unconsumed preallocated slot, but yes, drm_sched_job_add_dependency() could explode even earlier if adding a normal dependency after preallocating a slot.

Quoted 7 lines

Yes, it would. All operations simply give you NULL for those slots. So seems to me you have to check for NULL wherever a preallocated slot might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't answer our questions about the idiomatic way to do it.

Quoted 7 lines

Maybe reserving slots with already signaled fences wasn't such a bad idea after all?

If we go for the NULL approach, it's probably the only sane way to then check for NULL wherever dependencies are accessed :(

Opinions?

Well if the xarray API returns the NULL consistently the approach from this patch is fine I think.

We just need to add two more checks to the above mentioned functions, some more unit tests probably to make sure, and that should be fine for now.

On the bikeshedding front I would perhaps suggest:

- drm_sched_job_preallocate_dependency()
- drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API and a bit easier on the eyes, to my eyes at least.

Regards,

Tvrtko

#5 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 115 lines · Tvrtko Ursulin
Quoted 91 lines · Philipp Stanner
Quoted 9 lines · Tvrtko Ursulin
Quoted 7 lines · Christian König

Sometimes drivers need to be able to submit multiple jobs which depend on
each other to different schedulers at the same time, but using drm_sched_job_add_dependency() can't fail any more after the first job is
initialized.

Quoted 7 lines
Quoted 7 lines

This function preallocate memory for dependency slots so that no ENOMEM
can come later while adding dependencies.

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated deps

I think we agreed to not put change logs into commit messages anymore :)

They aren't useful for any reader. Who needs the changelog afterwards can retreive it through the mail thread link that we add.

Quoted 58 lines
Quoted 58 lines

---
   drivers/gpu/drm/scheduler/sched_main.c | 45
++++++++++++++++++++++++++
   include/drm/gpu_scheduler.h            |  4 +++

   2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +2 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct drm_sched_job
 *job)
    }
    EXPORT_SYMBOL(drm_sched_job_arm);
+/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on adding

dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs which depend on
+ * each other to different schedulers at the same time, but using
+ * drm_sched_job_add_dependency() can't fail any more after the first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so that no ENOMEM can
+ * come later while adding dependencies. The index of the preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job *job,
+                       u32 *id)
+{
+    return xa_alloc(&job->dependencies, id, NULL, xa_limit_32b, GFP_KERNEL);

drivers/gpu/drm/scheduler/sched_main.c +5 -0
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to preallocated

slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */

Quoted 8 lines
Quoted 6 lines

+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32 id,
+                    struct dma_fence *fence)
+{
+    fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and is NULL?

Quoted 1 line

You

Hm?

Quoted 5 lines
Quoted 3 lines

Also, if someone preallocates and does not consume the slot will that confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because that looked it would skip dependencies upon encountering an unconsumed preallocated slot, but yes, drm_sched_job_add_dependency() could explode even earlier if adding a normal dependency after preallocating a slot.

Quoted 10 lines

Yes, it would. All operations simply give you NULL for those slots. So seems to me you have to check for NULL wherever a preallocated slot might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't answer our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't such a bad idea after all?

Quoted 8 lines
Quoted 4 lines

If we go for the NULL approach, it's probably the only sane way to then check for NULL wherever dependencies are accessed :(

Opinions?

Well if the xarray API returns the NULL consistently the approach from this patch is fine I think.

We just need to add two more checks to the above mentioned functions,

I need to correct myself, drm_sched_job_dependency() wouldn't be able to just skip NULLs since it relies on NULL for "no more dependencies". We would need to track something like job->max_dependency and terminate on job->last_dependency > job->max_dependency or so.

Regards,

Tvrtko

Quoted 13 lines

some more unit tests probably to make sure, and that should be fine for now.

On the bikeshedding front I would perhaps suggest:

 - drm_sched_job_preallocate_dependency()
 - drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API and a bit easier on the eyes, to my eyes at least.

Regards,

Tvrtko

#6 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 157 lines · Tvrtko Ursulin
Quoted 146 lines · Tvrtko Ursulin
Quoted 108 lines · Philipp Stanner
Quoted 17 lines · Tvrtko Ursulin
Quoted 15 lines · Christian König

Sometimes drivers need to be able to submit multiple jobs which
depend on
each other to different schedulers at the same time, but using
drm_sched_job_add_dependency() can't fail any more after the first
job is
initialized.

This function preallocate memory for dependency slots so that no
ENOMEM
can come later while adding dependencies.

Quoted 4 lines
Quoted 4 lines

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated
deps

I think we agreed to not put change logs into commit messages anymore
:)

They aren't useful for any reader. Who needs the changelog afterwards
can retreive it through the mail thread link that we add.

Quoted 68 lines
Quoted 68 lines

---
   drivers/gpu/drm/scheduler/sched_main.c | 45
++++++++++++++++++++++++++
   include/drm/gpu_scheduler.h            |  4 +++

   2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct

drm_sched_job
*job)
   }
   EXPORT_SYMBOL(drm_sched_job_arm);
+/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on adding
dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs which
depend on
+ * each other to different schedulers at the same time, but using
+ * drm_sched_job_add_dependency() can't fail any more after the
first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so that
no ENOMEM can
+ * come later while adding dependencies. The index of the preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job
*job,
+                       u32 *id)
+{
+    return xa_alloc(&job->dependencies, id, NULL, xa_limit_32b, GFP_KERNEL);

drivers/gpu/drm/scheduler/sched_main.c +5 -0
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to

preallocated
slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32

Quoted 8 lines
Quoted 5 lines

id,
+                    struct dma_fence *fence) +{
+    fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and is NULL?

Quoted 1 line

You

Hm?

Quoted 6 lines
Quoted 4 lines

Also, if someone preallocates and does not consume the slot will that
confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because that looked it would skip dependencies upon encountering an unconsumed preallocated slot, but yes, drm_sched_job_add_dependency() could explode
even earlier if adding a normal dependency after preallocating a slot.

Quoted 18 lines

Yes, it would. All operations simply give you NULL for those slots. So
seems to me you have to check for NULL wherever a preallocated slot
might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't answer
our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't such a bad
idea after all?

If we go for the NULL approach, it's probably the only sane way to then
check for NULL wherever dependencies are accessed :(

Quoted 8 lines
Quoted 1 line

Opinions?

Well if the xarray API returns the NULL consistently the approach from
this patch is fine I think.

We just need to add two more checks to the above mentioned functions,

Quoted 7 lines

I need to correct myself, drm_sched_job_dependency() wouldn't be able to
just skip NULLs since it relies on NULL for "no more dependencies". We
would need to track something like job->max_dependency and terminate on
job->last_dependency > job->max_dependency or so.

Agreed, that would have to be fixed.

I believe we should reconsider Christian's first idea [1].

Thinking about it some more:
* With the NULL version, suddenly the xarray containing only valid dependencies can sometimes contain NULL entries.
* If we could create our own tag, entries could be returned that were neither NULL nor valid fences, also requiring checks 'everywhere'.
* Only the "signaled fence as prealloc reservation" approach is fully backwards compatible and will never cause anyone to block after later reworks.

So maybe it's actually the best idea?

Sorry for the zigg-zagg. No hard requirements intended from my side, I'm willing to go with what you guys think.

Just saying, at least now I think that the already-signaled fence seems the most elegant solution. And since there's a function (dma_fence_get_stub()) for that, it seems to be in alignment with official dma_fence rules.

Philipp

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

Quoted 22 lines

Regards,

Tvrtko

Quoted 16 lines

some more unit tests probably to make sure, and that should be fine for
now.

On the bikeshedding front I would perhaps suggest:

  - drm_sched_job_preallocate_dependency()
  - drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API and a bit easier on the eyes, to my eyes at least.

Regards,

Tvrtko

#7 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 181 lines · Philipp Stanner
Quoted 157 lines · Tvrtko Ursulin
Quoted 146 lines · Tvrtko Ursulin
Quoted 108 lines · Philipp Stanner
Quoted 17 lines · Tvrtko Ursulin
Quoted 15 lines · Christian König

Sometimes drivers need to be able to submit multiple jobs which
depend on
each other to different schedulers at the same time, but using
drm_sched_job_add_dependency() can't fail any more after the first
job is
initialized.

This function preallocate memory for dependency slots so that no
ENOMEM
can come later while adding dependencies.

Quoted 4 lines
Quoted 4 lines

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated
deps

I think we agreed to not put change logs into commit messages anymore
:)

They aren't useful for any reader. Who needs the changelog afterwards
can retreive it through the mail thread link that we add.

Quoted 68 lines
Quoted 68 lines
---
    drivers/gpu/drm/scheduler/sched_main.c | 45
++++++++++++++++++++++++++
    include/drm/gpu_scheduler.h            |  4 +++
    2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct

drm_sched_job
*job)
   }
   EXPORT_SYMBOL(drm_sched_job_arm);
+/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on adding
dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs which
depend on
+ * each other to different schedulers at the same time, but using
+ * drm_sched_job_add_dependency() can't fail any more after the
first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so that
no ENOMEM can
+ * come later while adding dependencies. The index of the preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job
*job,
+                       u32 *id)
+{
+    return xa_alloc(&job->dependencies, id, NULL, xa_limit_32b, GFP_KERNEL);

drivers/gpu/drm/scheduler/sched_main.c +5 -0
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to

preallocated
slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32

Quoted 8 lines
Quoted 5 lines

id,
+                    struct dma_fence *fence) +{
+    fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and is NULL?

Quoted 1 line

You

Hm?

Quoted 6 lines
Quoted 4 lines

Also, if someone preallocates and does not consume the slot will that
confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because that looked it would skip dependencies upon encountering an unconsumed preallocated slot, but yes, drm_sched_job_add_dependency() could explode
even earlier if adding a normal dependency after preallocating a slot.

Quoted 18 lines

Yes, it would. All operations simply give you NULL for those slots. So
seems to me you have to check for NULL wherever a preallocated slot
might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't answer
our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't such a bad
idea after all?

If we go for the NULL approach, it's probably the only sane way to then
check for NULL wherever dependencies are accessed :(

Quoted 8 lines
Quoted 1 line

Opinions?

Well if the xarray API returns the NULL consistently the approach from
this patch is fine I think.

We just need to add two more checks to the above mentioned functions,

Quoted 7 lines

I need to correct myself, drm_sched_job_dependency() wouldn't be able to
just skip NULLs since it relies on NULL for "no more dependencies". We
would need to track something like job->max_dependency and terminate on
job->last_dependency > job->max_dependency or so.

Agreed, that would have to be fixed.

I believe we should reconsider Christian's first idea [1].

Thinking about it some more:
* With the NULL version, suddenly the xarray containing only valid dependencies can sometimes contain NULL entries.
* If we could create our own tag, entries could be returned that were neither NULL nor valid fences, also requiring checks 'everywhere'.
* Only the "signaled fence as prealloc reservation" approach is fully backwards compatible and will never cause anyone to block after later reworks.

So maybe it's actually the best idea?

Quoted 7 lines

Sorry for the zigg-zagg. No hard requirements intended from my side, I'm willing to go with what you guys think.

Just saying, at least now I think that the already-signaled fence seems the most elegant solution. And since there's a function (dma_fence_get_stub()) for that, it seems to be in alignment with official dma_fence rules.

Potential problem there was dma_fence_is_signaled() and fence signaling annotations. In case some driver is holding a lock over the arm+push pair. I wish we had a non-signaling is_signaled helper..

Anyway, I think both options are passable. I even like the NULL entry slightly more since it is simpler in a way and I don't mind some extra checks completely hidden in scheduler internals.

Regards,

Tvrtko

Quoted 31 lines

Philipp

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

Quoted 22 lines

Regards,

Tvrtko

Quoted 16 lines

some more unit tests probably to make sure, and that should be fine for
now.

On the bikeshedding front I would perhaps suggest:

  - drm_sched_job_preallocate_dependency()
  - drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API and a bit easier on the eyes, to my eyes at least.

Regards,

Tvrtko

#8 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 218 lines · Tvrtko Ursulin
Quoted 210 lines · Philipp Stanner
Quoted 175 lines · Tvrtko Ursulin
Quoted 165 lines · Tvrtko Ursulin
Quoted 118 lines · Philipp Stanner
Quoted 19 lines · Tvrtko Ursulin
Quoted 17 lines · Christian König

Sometimes drivers need to be able to submit multiple jobs which
depend on
each other to different schedulers at the same time, but using
drm_sched_job_add_dependency() can't fail any more after the
first
job is
initialized.

This function preallocate memory for dependency slots so that
no
ENOMEM
can come later while adding dependencies.

Quoted 4 lines
Quoted 4 lines

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated
deps

I think we agreed to not put change logs into commit messages anymore
:)

They aren't useful for any reader. Who needs the changelog afterwards
can retreive it through the mail thread link that we add.

Quoted 76 lines
Quoted 76 lines

---
    drivers/gpu/drm/scheduler/sched_main.c | 45
++++++++++++++++++++++++++
    include/drm/gpu_scheduler.h            |  4 +++

    2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct

drm_sched_job
*job)
    }
    EXPORT_SYMBOL(drm_sched_job_arm);
+/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on
adding
dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs
which
depend on
+ * each other to different schedulers at the same time, but
using
+ * drm_sched_job_add_dependency() can't fail any more after
the
first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so
that
no ENOMEM can
+ * come later while adding dependencies. The index of the
preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job
*job,
+                       u32 *id)
+{
+    return xa_alloc(&job->dependencies, id, NULL, xa_limit_32b, GFP_KERNEL);

drivers/gpu/drm/scheduler/sched_main.c +5 -0
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to

preallocated
slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32
id,

Quoted 8 lines
Quoted 4 lines

+                    struct dma_fence *fence) +{
+    fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and is
NULL?

Quoted 1 line

You

Hm?

Quoted 6 lines
Quoted 4 lines

Also, if someone preallocates and does not consume the slot will that
confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because that
looked it would skip dependencies upon encountering an unconsumed
preallocated slot, but yes, drm_sched_job_add_dependency() could
explode
even earlier if adding a normal dependency after preallocating a
slot.

Quoted 16 lines

Yes, it would. All operations simply give you NULL for those slots. So
seems to me you have to check for NULL wherever a preallocated
slot
might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't answer
our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't such a
bad
idea after all?

Quoted 6 lines

If we go for the NULL approach, it's probably the only sane way
to then
check for NULL wherever dependencies are accessed :(

Opinions?

Quoted 7 lines

Well if the xarray API returns the NULL consistently the approach
from
this patch is fine I think.

We just need to add two more checks to the above mentioned functions,

Quoted 10 lines

I need to correct myself, drm_sched_job_dependency() wouldn't be able
to
just skip NULLs since it relies on NULL for "no more dependencies".
We
would need to track something like job->max_dependency and terminate
on
job->last_dependency > job->max_dependency or so.

Agreed, that would have to be fixed.

I believe we should reconsider Christian's first idea [1].

Thinking about it some more:
  * With the NULL version, suddenly the xarray containing only valid
    dependencies can sometimes contain NULL entries.
  * If we could create our own tag, entries could be returned that were
    neither NULL nor valid fences, also requiring checks 'everywhere'.
  * Only the "signaled fence as prealloc reservation" approach is fully
    backwards compatible and will never cause anyone to block after later reworks.

So maybe it's actually the best idea?

Sorry for the zigg-zagg. No hard requirements intended from my side,
I'm willing to go with what you guys think.

Quoted 5 lines

Just saying, at least now I think that the already-signaled fence seems
the most elegant solution. And since there's a function (dma_fence_get_stub()) for that, it seems to be in alignment with official dma_fence rules.

Quoted 5 lines

Potential problem there was dma_fence_is_signaled() and fence signaling
annotations. In case some driver is holding a lock over the arm+push pair. I wish we had a non-signaling is_signaled helper..

Yes! +1!

But Christian doesn't like that direction:

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

P.

Quoted 51 lines

Anyway, I think both options are passable. I even like the NULL entry slightly more since it is simpler in a way and I don't mind some extra
checks completely hidden in scheduler internals.

Regards,

Tvrtko

Quoted 35 lines

Philipp

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

Quoted 24 lines

Regards,

Tvrtko

Quoted 18 lines

some more unit tests probably to make sure, and that should be fine
for
now.

On the bikeshedding front I would perhaps suggest:

   - drm_sched_job_preallocate_dependency()
   - drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API and a bit
easier on the eyes, to my eyes at least.

Regards,

Tvrtko

#9 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 225 lines · Philipp Stanner
Quoted 218 lines · Tvrtko Ursulin
Quoted 210 lines · Philipp Stanner
Quoted 175 lines · Tvrtko Ursulin
Quoted 165 lines · Tvrtko Ursulin
Quoted 118 lines · Philipp Stanner
Quoted 19 lines · Tvrtko Ursulin
Quoted 17 lines · Christian König

Sometimes drivers need to be able to submit multiple jobs which
depend on
each other to different schedulers at the same time, but using
drm_sched_job_add_dependency() can't fail any more after the
first
job is
initialized.

This function preallocate memory for dependency slots so that
no
ENOMEM
can come later while adding dependencies.

Quoted 4 lines
Quoted 4 lines

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated
deps

I think we agreed to not put change logs into commit messages anymore
:)

They aren't useful for any reader. Who needs the changelog afterwards
can retreive it through the mail thread link that we add.

Quoted 76 lines
Quoted 76 lines
---
     drivers/gpu/drm/scheduler/sched_main.c | 45
++++++++++++++++++++++++++
     include/drm/gpu_scheduler.h            |  4 +++
     2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct

drm_sched_job
*job)
    }
    EXPORT_SYMBOL(drm_sched_job_arm);
+/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on
adding
dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs
which
depend on
+ * each other to different schedulers at the same time, but
using
+ * drm_sched_job_add_dependency() can't fail any more after
the
first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so
that
no ENOMEM can
+ * come later while adding dependencies. The index of the
preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job
*job,
+                       u32 *id)
+{
+    return xa_alloc(&job->dependencies, id, NULL, xa_limit_32b, GFP_KERNEL);

drivers/gpu/drm/scheduler/sched_main.c +5 -0
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to

preallocated
slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32
id,

Quoted 8 lines
Quoted 4 lines

+                    struct dma_fence *fence) +{
+    fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and is
NULL?

Quoted 1 line

You

Hm?

Quoted 6 lines
Quoted 4 lines

Also, if someone preallocates and does not consume the slot will that
confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because that
looked it would skip dependencies upon encountering an unconsumed
preallocated slot, but yes, drm_sched_job_add_dependency() could
explode
even earlier if adding a normal dependency after preallocating a
slot.

Quoted 16 lines

Yes, it would. All operations simply give you NULL for those slots. So
seems to me you have to check for NULL wherever a preallocated
slot
might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't answer
our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't such a
bad
idea after all?

Quoted 6 lines

If we go for the NULL approach, it's probably the only sane way
to then
check for NULL wherever dependencies are accessed :(

Opinions?

Quoted 7 lines

Well if the xarray API returns the NULL consistently the approach
from
this patch is fine I think.

We just need to add two more checks to the above mentioned functions,

Quoted 10 lines

I need to correct myself, drm_sched_job_dependency() wouldn't be able
to
just skip NULLs since it relies on NULL for "no more dependencies".
We
would need to track something like job->max_dependency and terminate
on
job->last_dependency > job->max_dependency or so.

Agreed, that would have to be fixed.

I believe we should reconsider Christian's first idea [1].

Thinking about it some more:
  * With the NULL version, suddenly the xarray containing only
valid
    dependencies can sometimes contain NULL entries.
  * If we could create our own tag, entries could be returned that
were
    neither NULL nor valid fences, also requiring checks
'everywhere'.
  * Only the "signaled fence as prealloc reservation" approach is
fully
    backwards compatible and will never cause anyone to block after later reworks.

So maybe it's actually the best idea?

Sorry for the zigg-zagg. No hard requirements intended from my side,
I'm willing to go with what you guys think.

Quoted 5 lines

Just saying, at least now I think that the already-signaled fence seems
the most elegant solution. And since there's a function (dma_fence_get_stub()) for that, it seems to be in alignment with official dma_fence rules.

Quoted 5 lines

Potential problem there was dma_fence_is_signaled() and fence signaling
annotations. In case some driver is holding a lock over the arm+push pair. I wish we had a non-signaling is_signaled helper..

Quoted 5 lines

Yes! +1!

But Christian doesn't like that direction:

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

Thanks, I read this but ended up uncertain on the conclusion.

For instance Christian at the end comments like this:

"""
You can test the flag if you know what the fence means to you, that is not a problem at all. """

That was in the context of testing the signaled bit without opportunistic signaling.

For me, from the scheduler dependencies side, that should exactly apply. Scheduler knows it does not need to add a signaled fence to the dep array so AFAICS it is fine to skip it. And it may easily be opportunistic signaling ends up a problem for the scheduler.

So maybe such helper would be okay after all.

Or if the concern is helper might encourage some potentially unsafe usage, in that case it should come with kerneldoc describing. It is not like review is guaranteed to catch someone using test_bit directly anyway so for me, on balance, helper is always better.

Regards,

Tvrtko

Quoted 55 lines

P.

Quoted 51 lines

Anyway, I think both options are passable. I even like the NULL entry slightly more since it is simpler in a way and I don't mind some extra
checks completely hidden in scheduler internals.

Regards,

Tvrtko

Quoted 35 lines

Philipp

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

Quoted 24 lines

Regards,

Tvrtko

Quoted 18 lines

some more unit tests probably to make sure, and that should be fine
for
now.

On the bikeshedding front I would perhaps suggest:

   - drm_sched_job_preallocate_dependency()
   - drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API and a bit
easier on the eyes, to my eyes at least.

Regards,

Tvrtko

#10 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 10 lines · Tvrtko Ursulin
Quoted 8 lines · Philipp Stanner
Quoted 7 lines · Tvrtko Ursulin
Quoted 5 lines · Philipp Stanner
Quoted 4 lines · Tvrtko Ursulin
Quoted 2 lines · Tvrtko Ursulin

[snip]

Quoted 160 lines
Quoted 141 lines
Quoted 134 lines
Quoted 126 lines
Quoted 88 lines
Quoted 80 lines
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
(quoted diff) +3 -0
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency

to
preallocated
slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot
dependency.
+ */
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job
*job, u32
id,
+                    struct dma_fence *fence)

Quoted 8 lines
Quoted 3 lines

+{
+    fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and
is
NULL?

Quoted 1 line

You

Hm?

Quoted 7 lines
Quoted 5 lines

Also, if someone preallocates and does not consume the slot
will that
confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because
that
looked it would skip dependencies upon encountering an unconsumed
preallocated slot, but yes, drm_sched_job_add_dependency() could
explode
even earlier if adding a normal dependency after preallocating
a
slot.

Quoted 18 lines

Yes, it would. All operations simply give you NULL for those
slots. So
seems to me you have to check for NULL wherever a preallocated
slot
might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't
answer
our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't such a
bad
idea after all?

Quoted 7 lines

If we go for the NULL approach, it's probably the only sane
way
to then
check for NULL wherever dependencies are accessed :(

Opinions?

Quoted 7 lines

Well if the xarray API returns the NULL consistently the approach
from
this patch is fine I think.

We just need to add two more checks to the above mentioned functions,

Quoted 11 lines

I need to correct myself, drm_sched_job_dependency() wouldn't be
able
to
just skip NULLs since it relies on NULL for "no more dependencies".
We
would need to track something like job->max_dependency and terminate
on
job->last_dependency > job->max_dependency or so.

Agreed, that would have to be fixed.

I believe we should reconsider Christian's first idea [1].

Thinking about it some more:
   * With the NULL version, suddenly the xarray containing only valid
     dependencies can sometimes contain NULL entries.
   * If we could create our own tag, entries could be returned that
were
     neither NULL nor valid fences, also requiring checks 'everywhere'.
   * Only the "signaled fence as prealloc reservation" approach is
fully
     backwards compatible and will never cause anyone to block after
     later reworks.

So maybe it's actually the best idea?

Sorry for the zigg-zagg. No hard requirements intended from my side,
I'm willing to go with what you guys think.

Quoted 7 lines

Just saying, at least now I think that the already-signaled fence
seems
the most elegant solution. And since there's a function (dma_fence_get_stub()) for that, it seems to be in alignment with
official dma_fence rules.

Quoted 6 lines

Potential problem there was dma_fence_is_signaled() and fence signaling
annotations. In case some driver is holding a lock over the arm+push
pair. I wish we had a non-signaling is_signaled helper..

Quoted 5 lines

Yes! +1!

But Christian doesn't like that direction:

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

Thanks, I read this but ended up uncertain on the conclusion.

For instance Christian at the end comments like this:

"""
You can test the flag if you know what the fence means to you, that is
not a problem at all.
"""

That was in the context of testing the signaled bit without opportunistic signaling.

Quoted 7 lines

For me, from the scheduler dependencies side, that should exactly apply.
Scheduler knows it does not need to add a signaled fence to the dep array so AFAICS it is fine to skip it. And it may easily be opportunistic signaling ends up a problem for the scheduler.

So maybe such helper would be okay after all.

The thing is that, if I understand him correctly, Christian doesn't want a helper. He wants "us" to just use test_bit().

My point is just that dma_fence_is_signaled() is a horrible name.

The function pci_is_enabled() tells you whether the PCI device is enabled. What it doesn't do is

bool pci_is_enabled(pdev)
{

   if (crazy_callback_is_implemented()) {
      pci_enable_device();
      return true;

}

...
}

It's not intuitive that a function called "{something}_is_signaled()" does signal that thing. Although I get that the syntactical idea probably is that from the GPUs POV the fence is already signaled when this or that seqno has been reached.

Anyways, judging aside, if a wrapper for test_bit(dma_fence) were acceptable, then it would need to steal dma_fence_is_signaled()'s name, and dma_fence_is_signaled() would have to get a new name. Which is precisely what was rejected, as I see it.

P.

Quoted 73 lines

Or if the concern is helper might encourage some potentially unsafe usage, in that case it should come with kerneldoc describing. It is not
like review is guaranteed to catch someone using test_bit directly anyway so for me, on balance, helper is always better.

Regards,

Tvrtko

Quoted 58 lines

P.

Quoted 54 lines

Anyway, I think both options are passable. I even like the NULL entry
slightly more since it is simpler in a way and I don't mind some extra
checks completely hidden in scheduler internals.

Regards,

Tvrtko

Quoted 37 lines

Philipp

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

Quoted 26 lines

Regards,

Tvrtko

Quoted 20 lines

some more unit tests probably to make sure, and that should be
fine
for
now.

On the bikeshedding front I would perhaps suggest:

    - drm_sched_job_preallocate_dependency()
    - drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API and a
bit
easier on the eyes, to my eyes at least.

Regards,

Tvrtko

#11 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 180 lines · Philipp Stanner
Quoted 10 lines · Tvrtko Ursulin
Quoted 8 lines · Philipp Stanner
Quoted 7 lines · Tvrtko Ursulin
Quoted 5 lines · Philipp Stanner
Quoted 4 lines · Tvrtko Ursulin
Quoted 2 lines · Tvrtko Ursulin

[snip]

Quoted 166 lines
Quoted 141 lines
Quoted 134 lines
Quoted 126 lines
Quoted 88 lines
Quoted 80 lines
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
(quoted diff) +3 -0
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency

to
preallocated
slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the preallocated slot
dependency.
+ */
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job
*job, u32
id,
+                    struct dma_fence *fence)

Quoted 8 lines
Quoted 3 lines

+{
+    fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and
is
NULL?

Quoted 1 line

You

Hm?

Quoted 7 lines
Quoted 5 lines

Also, if someone preallocates and does not consume the slot
will that
confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because
that
looked it would skip dependencies upon encountering an unconsumed
preallocated slot, but yes, drm_sched_job_add_dependency() could
explode
even earlier if adding a normal dependency after preallocating
a
slot.

Quoted 18 lines

Yes, it would. All operations simply give you NULL for those
slots. So
seems to me you have to check for NULL wherever a preallocated
slot
might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't
answer
our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't such a
bad
idea after all?

Quoted 7 lines

If we go for the NULL approach, it's probably the only sane
way
to then
check for NULL wherever dependencies are accessed :(

Opinions?

Quoted 7 lines

Well if the xarray API returns the NULL consistently the approach
from
this patch is fine I think.

We just need to add two more checks to the above mentioned functions,

Quoted 11 lines

I need to correct myself, drm_sched_job_dependency() wouldn't be
able
to
just skip NULLs since it relies on NULL for "no more dependencies".
We
would need to track something like job->max_dependency and terminate
on
job->last_dependency > job->max_dependency or so.

Agreed, that would have to be fixed.

I believe we should reconsider Christian's first idea [1].

Thinking about it some more:
   * With the NULL version, suddenly the xarray containing only
valid
     dependencies can sometimes contain NULL entries.
   * If we could create our own tag, entries could be returned
that
were
     neither NULL nor valid fences, also requiring checks
'everywhere'.
   * Only the "signaled fence as prealloc reservation" approach
is
fully
     backwards compatible and will never cause anyone to block
after
     later reworks.

So maybe it's actually the best idea?

Sorry for the zigg-zagg. No hard requirements intended from my side,
I'm willing to go with what you guys think.

Quoted 7 lines

Just saying, at least now I think that the already-signaled fence
seems
the most elegant solution. And since there's a function (dma_fence_get_stub()) for that, it seems to be in alignment with
official dma_fence rules.

Quoted 6 lines

Potential problem there was dma_fence_is_signaled() and fence signaling
annotations. In case some driver is holding a lock over the arm+push
pair. I wish we had a non-signaling is_signaled helper..

Quoted 5 lines

Yes! +1!

But Christian doesn't like that direction:

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

Thanks, I read this but ended up uncertain on the conclusion.

For instance Christian at the end comments like this:

"""
You can test the flag if you know what the fence means to you, that is
not a problem at all.
"""

That was in the context of testing the signaled bit without opportunistic signaling.

For me, from the scheduler dependencies side, that should exactly apply.
Scheduler knows it does not need to add a signaled fence to the dep array so AFAICS it is fine to skip it. And it may easily be opportunistic signaling ends up a problem for the scheduler.

Quoted 4 lines
Quoted 1 line

So maybe such helper would be okay after all.

The thing is that, if I understand him correctly, Christian doesn't want a helper. He wants "us" to just use test_bit().

I suspected it may be only about not renaming into some variant of check_and_signal etc. With which I agree with, that the name should be left alone, because...

Quoted 1 line

My point is just that dma_fence_is_signaled() is a horrible name.

... as dodgy as it is, and despite me also failing to really understand the reason why it _has_ to be have opportunistic signaling, I think those semantics are by now pretty much ingrained into people dealing with this API so I would leave it as is.

What I was thinking was the double underscore version of the helper and some kerneldoc to say when it is safe to use. For me a helper is better than poking directly.

Quoted 18 lines

The function pci_is_enabled() tells you whether the PCI device is enabled. What it doesn't do is

bool pci_is_enabled(pdev)

{
    if (crazy_callback_is_implemented()) {
       pci_enable_device();
       return true;
    }
   ...
}

It's not intuitive that a function called "{something}_is_signaled()" does signal that thing. Although I get that the syntactical idea probably is that from the GPUs POV the fence is already signaled when this or that seqno has been reached.

Quoted 4 lines

Anyways, judging aside, if a wrapper for test_bit(dma_fence) were acceptable, then it would need to steal dma_fence_is_signaled()'s name, and dma_fence_is_signaled() would have to get a new name. Which is precisely what was rejected, as I see it.

__dma_fence_is_signaled() is what I would do and leave the existing one as is.

Regards,

Tvrtko

Quoted 73 lines
Quoted 72 lines

Or if the concern is helper might encourage some potentially unsafe usage, in that case it should come with kerneldoc describing. It is not
like review is guaranteed to catch someone using test_bit directly anyway so for me, on balance, helper is always better.

Regards,

Tvrtko

Quoted 58 lines

P.

Quoted 54 lines

Anyway, I think both options are passable. I even like the NULL entry
slightly more since it is simpler in a way and I don't mind some extra
checks completely hidden in scheduler internals.

Regards,

Tvrtko

Quoted 37 lines

Philipp

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

Quoted 26 lines

Regards,

Tvrtko

Quoted 20 lines

some more unit tests probably to make sure, and that should be
fine
for
now.

On the bikeshedding front I would perhaps suggest:

    - drm_sched_job_preallocate_dependency()
    - drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API and a
bit
easier on the eyes, to my eyes at least.

Regards,

Tvrtko

#12 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

Quoted 256 lines · Tvrtko Ursulin
Quoted 204 lines · Philipp Stanner
Quoted 10 lines · Tvrtko Ursulin
Quoted 8 lines · Philipp Stanner
Quoted 7 lines · Tvrtko Ursulin
Quoted 5 lines · Philipp Stanner
Quoted 4 lines · Tvrtko Ursulin
Quoted 2 lines · Tvrtko Ursulin

[snip]

Quoted 190 lines
Quoted 163 lines
Quoted 156 lines
Quoted 147 lines
Quoted 102 lines
Quoted 98 lines
Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
(quoted diff) +3 -0
+
+/**
+ * drm_sched_job_add_prealloc_dep - add

dependency
to
preallocated
slot
+ * @job: scheduler job where dependencies will
be
added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
+ * Consumes @fence and adds it to the
preallocated
slot
dependency.
+ */
+void drm_sched_job_add_prealloc_dep(struct
drm_sched_job
*job, u32
id,
+                    struct dma_fence *fence)
+{
+    fence = xa_store(&job->dependencies, id,

Quoted 8 lines
Quoted 2 lines

fence,
GFP_ATOMIC);

Add assert that the passed id exists (was
preallocated)
and
is
NULL?

Quoted 1 line

You

Hm?

Quoted 9 lines
Quoted 7 lines

Also, if someone preallocates and does not consume the
slot
will that
confuse the iteration in drm_sched_job_dependency()?

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because
that
looked it would skip dependencies upon encountering an unconsumed
preallocated slot, but yes, drm_sched_job_add_dependency()
could
explode
even earlier if adding a normal dependency after preallocating
a
slot.

Quoted 21 lines

Yes, it would. All operations simply give you NULL for
those
slots. So
seems to me you have to check for NULL wherever a preallocated
slot
might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that
Wilcox
didn't
answer
our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't
such a
bad
idea after all?

Quoted 8 lines

If we go for the NULL approach, it's probably the only
sane
way
to then
check for NULL wherever dependencies are accessed :(

Opinions?

Well if the xarray API returns the NULL consistently the
approach
from
this patch is fine I think.

Quoted 3 lines

We just need to add two more checks to the above
mentioned
functions,

Quoted 13 lines

I need to correct myself, drm_sched_job_dependency() wouldn't
be
able
to
just skip NULLs since it relies on NULL for "no more dependencies".
We
would need to track something like job->max_dependency and
terminate
on
job->last_dependency > job->max_dependency or so.

Agreed, that would have to be fixed.

I believe we should reconsider Christian's first idea [1].

Thinking about it some more:
    * With the NULL version, suddenly the xarray containing only
valid
      dependencies can sometimes contain NULL entries.
    * If we could create our own tag, entries could be returned
that
were
      neither NULL nor valid fences, also requiring checks 'everywhere'.
    * Only the "signaled fence as prealloc reservation" approach
is
fully
      backwards compatible and will never cause anyone to block
after
      later reworks.

So maybe it's actually the best idea?

Sorry for the zigg-zagg. No hard requirements intended from my
side,
I'm willing to go with what you guys think.

Quoted 8 lines

Just saying, at least now I think that the already-signaled fence
seems
the most elegant solution. And since there's a function (dma_fence_get_stub()) for that, it seems to be in alignment
with
official dma_fence rules.

Quoted 6 lines

Potential problem there was dma_fence_is_signaled() and fence signaling
annotations. In case some driver is holding a lock over the arm+push
pair. I wish we had a non-signaling is_signaled helper..

Quoted 5 lines

Yes! +1!

But Christian doesn't like that direction:

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

Thanks, I read this but ended up uncertain on the conclusion.

For instance Christian at the end comments like this:

"""
You can test the flag if you know what the fence means to you, that
is
not a problem at all.
"""

That was in the context of testing the signaled bit without opportunistic signaling.

For me, from the scheduler dependencies side, that should exactly apply.
Scheduler knows it does not need to add a signaled fence to the dep
array so AFAICS it is fine to skip it. And it may easily be opportunistic signaling ends up a problem for the scheduler.

Quoted 4 lines
Quoted 1 line

So maybe such helper would be okay after all.

The thing is that, if I understand him correctly, Christian doesn't want a helper. He wants "us" to just use test_bit().

I suspected it may be only about not renaming into some variant of check_and_signal etc. With which I agree with, that the name should be
left alone, because...

Quoted 1 line

My point is just that dma_fence_is_signaled() is a horrible name.

... as dodgy as it is, and despite me also failing to really understand
the reason why it _has_ to be have opportunistic signaling, I think those semantics are by now pretty much ingrained into people dealing with this API so I would leave it as is.

What I was thinking was the double underscore version of the helper and
some kerneldoc to say when it is safe to use. For me a helper is better
than poking directly.

Quoted 20 lines

The function pci_is_enabled() tells you whether the PCI device is enabled. What it doesn't do is

bool pci_is_enabled(pdev)
{
    if (crazy_callback_is_implemented()) {
       pci_enable_device();
       return true;
    }

   ...
}

It's not intuitive that a function called "{something}_is_signaled()"
does signal that thing. Although I get that the syntactical idea probably is that from the GPUs POV the fence is already signaled when
this or that seqno has been reached.

Quoted 5 lines

Anyways, judging aside, if a wrapper for test_bit(dma_fence) were acceptable, then it would need to steal dma_fence_is_signaled()'s name,
and dma_fence_is_signaled() would have to get a new name. Which is precisely what was rejected, as I see it.

Quoted 3 lines

__dma_fence_is_signaled() is what I would do and leave the existing one
as is.

Fine by me. If you provide a patch for dma_fence, I can do a review.

P.

Quoted 86 lines

Regards,

Tvrtko

Quoted 80 lines
Quoted 79 lines

Or if the concern is helper might encourage some potentially unsafe
usage, in that case it should come with kerneldoc describing. It is
not
like review is guaranteed to catch someone using test_bit directly
anyway so for me, on balance, helper is always better.

Regards,

Tvrtko

Quoted 62 lines

P.

Quoted 58 lines

Anyway, I think both options are passable. I even like the NULL
entry
slightly more since it is simpler in a way and I don't mind some
extra
checks completely hidden in scheduler internals.

Regards,

Tvrtko

Quoted 39 lines

Philipp

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

Quoted 28 lines

Regards,

Tvrtko

Quoted 22 lines

some more unit tests probably to make sure, and that should
be
fine
for
now.

On the bikeshedding front I would perhaps suggest:

     - drm_sched_job_preallocate_dependency()
     - drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API
and a
bit
easier on the eyes, to my eyes at least.

Regards,

Tvrtko

maintainer
#13 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

In reply to Philipp Stanner

Sorry for the delayed reply.

Quoted 6 lines · Philipp Stanner
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Also, if someone preallocates and does not consume the slot
will that
confuse the iteration in drm_sched_job_dependency()?

No it doesn't. The xarray is filtering NULL and XA_ZERO_ENTRY values in the iterator.

As far as I know that is *not* documented anywhere and it took me quite some time to figure that out.

E.g. you can explicitly ask for a NULL/XA_ZERO_ENTRY entry with xa_load(), but you won't see it in xa_for_each().

Quoted 119 lines
Quoted 119 lines
Quoted 103 lines
Quoted 96 lines
Quoted 88 lines
Quoted 50 lines
Quoted 42 lines
Quoted 2 lines

drm_sched_job_add_dependency() you mean.

I was actually thinking of drm_sched_job_dependency() because
that
looked it would skip dependencies upon encountering an unconsumed
preallocated slot, but yes, drm_sched_job_add_dependency() could
explode
even earlier if adding a normal dependency after preallocating
a
slot.

Quoted 18 lines

Yes, it would. All operations simply give you NULL for those
slots. So
seems to me you have to check for NULL wherever a preallocated
slot
might drop out. That would then be a bug.

It's kind of tricky, all that. It's a pity that Wilcox didn't
answer
our questions about the idiomatic way to do it.

Maybe reserving slots with already signaled fences wasn't such a
bad
idea after all?

Quoted 7 lines

If we go for the NULL approach, it's probably the only sane
way
to then
check for NULL wherever dependencies are accessed :(

Opinions?

Quoted 7 lines

Well if the xarray API returns the NULL consistently the approach
from
this patch is fine I think.

We just need to add two more checks to the above mentioned functions,

Quoted 11 lines

I need to correct myself, drm_sched_job_dependency() wouldn't be
able
to
just skip NULLs since it relies on NULL for "no more dependencies".
We
would need to track something like job->max_dependency and terminate
on
job->last_dependency > job->max_dependency or so.

Agreed, that would have to be fixed.

I believe we should reconsider Christian's first idea [1].

Thinking about it some more:
   * With the NULL version, suddenly the xarray containing only valid
     dependencies can sometimes contain NULL entries.
   * If we could create our own tag, entries could be returned that
were
     neither NULL nor valid fences, also requiring checks 'everywhere'.
   * Only the "signaled fence as prealloc reservation" approach is
fully
     backwards compatible and will never cause anyone to block after
     later reworks.

So maybe it's actually the best idea?

Sorry for the zigg-zagg. No hard requirements intended from my side,
I'm willing to go with what you guys think.

Quoted 7 lines

Just saying, at least now I think that the already-signaled fence
seems
the most elegant solution. And since there's a function (dma_fence_get_stub()) for that, it seems to be in alignment with
official dma_fence rules.

Quoted 6 lines

Potential problem there was dma_fence_is_signaled() and fence signaling
annotations. In case some driver is holding a lock over the arm+push
pair. I wish we had a non-signaling is_signaled helper..

Quoted 5 lines

Yes! +1!

But Christian doesn't like that direction:

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

Thanks, I read this but ended up uncertain on the conclusion.

For instance Christian at the end comments like this:

"""
You can test the flag if you know what the fence means to you, that is
not a problem at all.
"""

Quoted 8 lines
Quoted 8 lines

That was in the context of testing the signaled bit without opportunistic signaling.

For me, from the scheduler dependencies side, that should exactly apply.
Scheduler knows it does not need to add a signaled fence to the dep array so AFAICS it is fine to skip it. And it may easily be opportunistic signaling ends up a problem for the scheduler.

Well that was not what I meant.

The code which issued the fence can test the fence flags directly.

But the scheduler is a consumer of fences somebody else issued.

In other words we could do something like dma_fence_is_stub() or similar, to have a clean interface. But please no testing of the fence flags in the scheduler directly.

Anyway, we don't need it. The general assumption that the xarray returns NULL for the iterator is incorrect. NULL entries are simply skipped over.

Regards,
Christian.

Quoted 107 lines
Quoted 2 lines

So maybe such helper would be okay after all.

The thing is that, if I understand him correctly, Christian doesn't want a helper. He wants "us" to just use test_bit().

My point is just that dma_fence_is_signaled() is a horrible name.

The function pci_is_enabled() tells you whether the PCI device is enabled. What it doesn't do is

bool pci_is_enabled(pdev)

{
   if (crazy_callback_is_implemented()) {
      pci_enable_device();
      return true;
   }

...
}

It's not intuitive that a function called "{something}_is_signaled()" does signal that thing. Although I get that the syntactical idea probably is that from the GPUs POV the fence is already signaled when this or that seqno has been reached.

Anyways, judging aside, if a wrapper for test_bit(dma_fence) were acceptable, then it would need to steal dma_fence_is_signaled()'s name, and dma_fence_is_signaled() would have to get a new name. Which is precisely what was rejected, as I see it.

P.

Quoted 73 lines

Or if the concern is helper might encourage some potentially unsafe usage, in that case it should come with kerneldoc describing. It is not
like review is guaranteed to catch someone using test_bit directly anyway so for me, on balance, helper is always better.

Regards,

Tvrtko

Quoted 58 lines

P.

Quoted 54 lines

Anyway, I think both options are passable. I even like the NULL entry
slightly more since it is simpler in a way and I don't mind some extra
checks completely hidden in scheduler internals.

Regards,

Tvrtko

Quoted 37 lines

Philipp

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

Quoted 26 lines

Regards,

Tvrtko

Quoted 20 lines

some more unit tests probably to make sure, and that should be
fine
for
now.

On the bikeshedding front I would perhaps suggest:

    - drm_sched_job_preallocate_dependency()
    - drm_sched_job_replace_dependency()

Reads a little bit more aligned with the rest of the API and a
bit
easier on the eyes, to my eyes at least.

Regards,

Tvrtko

maintainer
#14 lore raw

Re: [PATCH 1/3] drm/sched: add drm_sched_prealloc_dependency_slots v3

In reply to Tvrtko Ursulin
Quoted 56 lines · Tvrtko Ursulin
Quoted 54 lines · Christian König

Sometimes drivers need to be able to submit multiple jobs which depend on each other to different schedulers at the same time, but using drm_sched_job_add_dependency() can't fail any more after the first job is initialized.

This function preallocate memory for dependency slots so that no ENOMEM can come later while adding dependencies.

v2: rework implementation an documentation
v3: rework from scratch, use separate function to add preallocated deps

---
  drivers/gpu/drm/scheduler/sched_main.c | 45 ++++++++++++++++++++++++++ include/drm/gpu_scheduler.h            |  4 +++

  2 files changed, 49 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index f7118497e47a..b95e7089aa70 100644
@@ -858,6 +858,51 @@ void drm_sched_job_arm(struct drm_sched_job *job)
   }
   EXPORT_SYMBOL(drm_sched_job_arm);

  +/**
+ * drm_sched_job_prealloc_dependency_slot - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @id: id for the allocated slot
+  *
+ * Sometimes drivers need to be able to submit multiple jobs which depend on
+ * each other to different schedulers at the same time, but using
+ * drm_sched_job_add_dependency() can't fail any more after the first job is
+ * initialized.
+ *
+ * This function preallocate memory for a dependency slot so that no ENOMEM can
+ * come later while adding dependencies. The index of the preallocated slot is
+ * returned in @id.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */

drivers/gpu/drm/scheduler/sched_main.c +13 -0
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job *job,
+                       u32 *id)
+{
+    return xa_alloc(&job->dependencies, id, NULL, xa_limit_32b, GFP_KERNEL);
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slot);
+
+/**
+ * drm_sched_job_add_prealloc_dep - add dependency to preallocated slot
+ * @job: scheduler job where dependencies will be added
+ * @id: the preallocated slot index
+ * @fence: the dependency to add
+ *
Quoted 8 lines

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_job_arm(struct drm_sched_job *job) · line 896

Quoted 6 lines

+ * Consumes @fence and adds it to the preallocated slot dependency.
+ */

drivers/gpu/drm/scheduler/sched_main.c +4 -0
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32 id,
+                    struct dma_fence *fence)
+{
+    fence = xa_store(&job->dependencies, id, fence, GFP_ATOMIC);

Add assert that the passed id exists (was preallocated) and is NULL?

That is partially done by the WARN_ON below, but I couldn't find a way to check if the slot was pre-allocated.

Quoted 1 line

Also, if someone preallocates and does not consume the slot will that confuse the iteration in drm_sched_job_dependency()?

No, slots with NULL in them are skipped by xa_for_each(). I will add a comment explaining that.

Thanks,
Christian.

Quoted 33 lines

Regards,

Tvrtko

Quoted 27 lines

+    /*
+     * Be defensive just in case driver messed it up and used preallocated +     * slot twice.
+     */
+    if (WARN_ON(fence))
+        dma_fence_put(fence);

drivers/gpu/drm/scheduler/sched_main.c +3 -0
+}
+EXPORT_SYMBOL(drm_sched_job_add_prealloc_dep);
+

  /**
   * drm_sched_job_add_dependency - adds the fence as a job dependency
   * @job: scheduler job to add the dependencies to

include/drm/gpu_scheduler.h +4 -0
index d860db087ea5..0286e0934317 100644
@@ -632,6 +632,10 @@ int drm_sched_job_init(struct drm_sched_job *job,
                  u32 credits, void *owner);
   void drm_sched_job_arm(struct drm_sched_job *job);
   void drm_sched_entity_push_job(struct drm_sched_job *sched_job);
+int drm_sched_job_prealloc_dependency_slot(struct drm_sched_job *job,
+                       u32 *id);
+void drm_sched_job_add_prealloc_dep(struct drm_sched_job *job, u32 id,
+                    struct dma_fence *fence);
   int drm_sched_job_add_dependency(struct drm_sched_job *job,
                    struct dma_fence *fence);
   int drm_sched_job_add_syncobj_dependency(struct drm_sched_job *job,
author reviewer
#15 lore raw

PATCH 2/3 drm/sched: Add a test for prealloced fence slots

In reply to Christian König

Just to exercise the functionality.

---
 drivers/gpu/drm/scheduler/tests/tests_basic.c | 59 ++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/tests/tests_basic.c +58 -1
index 7230057e0594..abe7370047bd 100644
@@ -469,8 +469,65 @@ static struct kunit_suite drm_sched_credits = {
 	.test_cases = drm_sched_credits_tests,
 };
 
+static void drm_sched_test_prealloc(struct kunit *test)
+{
+	struct dma_fence *stub = dma_fence_get_stub();
+	struct drm_mock_sched_entity *entity;
+	struct drm_mock_scheduler *sched;
+	struct drm_mock_sched_job *job;
+	u32 id1, id2;
+	bool done;
+	int ret;
+
+	/*
+	 * Check if preallocation of dependency slots work
+	 */
+
+	sched = drm_mock_sched_new(test, MAX_SCHEDULE_TIMEOUT);
+
+	entity = drm_mock_sched_entity_new(test,
+					   DRM_SCHED_PRIORITY_NORMAL,
+					   sched);
+
+	job = drm_mock_sched_job_new(test, entity);
+
+	ret = drm_sched_job_add_dependency(&job->base, dma_fence_get(stub));
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	ret = drm_sched_job_prealloc_dependency_slot(&job->base, &id1);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	ret = drm_sched_job_prealloc_dependency_slot(&job->base, &id2);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	KUNIT_ASSERT_NE(test, id1, id2);
+
+	drm_sched_job_add_prealloc_dep(&job->base, id1, dma_fence_get(stub));
+	drm_sched_job_add_prealloc_dep(&job->base, id2, dma_fence_get(stub));
+
+	drm_mock_sched_job_submit(job);
+
+	done = drm_mock_sched_job_wait_scheduled(job, HZ);
+	KUNIT_ASSERT_TRUE(test, done);
+
+	drm_mock_sched_entity_free(entity);
+	drm_mock_sched_fini(sched);
+	dma_fence_put(stub);
+}
+
+static struct kunit_case drm_sched_prealloc_tests[] = {
+	KUNIT_CASE(drm_sched_test_prealloc),
+	{}
+};
+
+static struct kunit_suite drm_sched_prealloc = {
+	.name = "drm_sched_basic_prealloc_tests",
+	.test_cases = drm_sched_prealloc_tests,
+};
+
 kunit_test_suites(&drm_sched_basic,
 		  &drm_sched_timeout,
 		  &drm_sched_priority,
 		  &drm_sched_modify_sched,
-		  &drm_sched_credits);
+		  &drm_sched_credits,
+		  &drm_sched_prealloc);
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH 2/3] drm/sched: Add a test for prealloced fence slots

Quoted 49 lines · Christian König

Just to exercise the functionality.

---
  drivers/gpu/drm/scheduler/tests/tests_basic.c | 59 ++++++++++++++++++-
  1 file changed, 58 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/tests/tests_basic.c +34 -0
index 7230057e0594..abe7370047bd 100644
@@ -469,8 +469,65 @@ static struct kunit_suite drm_sched_credits = {
  	.test_cases = drm_sched_credits_tests,
  };
 
+static void drm_sched_test_prealloc(struct kunit *test)
+{
+	struct dma_fence *stub = dma_fence_get_stub();
+	struct drm_mock_sched_entity *entity;
+	struct drm_mock_scheduler *sched;
+	struct drm_mock_sched_job *job;
+	u32 id1, id2;
+	bool done;
+	int ret;
+
+	/*
+	 * Check if preallocation of dependency slots work
+	 */
+
+	sched = drm_mock_sched_new(test, MAX_SCHEDULE_TIMEOUT);
+
+	entity = drm_mock_sched_entity_new(test,
+					   DRM_SCHED_PRIORITY_NORMAL,
+					   sched);
+
+	job = drm_mock_sched_job_new(test, entity);
+
+	ret = drm_sched_job_add_dependency(&job->base, dma_fence_get(stub));
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	ret = drm_sched_job_prealloc_dependency_slot(&job->base, &id1);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	ret = drm_sched_job_prealloc_dependency_slot(&job->base, &id2);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	KUNIT_ASSERT_NE(test, id1, id2);
+
+	drm_sched_job_add_prealloc_dep(&job->base, id1, dma_fence_get(stub));

drivers/gpu/drm/scheduler/tests/tests_basic.c static struct kunit_suite drm_sched_credits = { · line 515

Quoted 10 lines
drivers/gpu/drm/scheduler/tests/tests_basic.c +10 -0
+	drm_sched_job_add_prealloc_dep(&job->base, id2, dma_fence_get(stub));
+
+	drm_mock_sched_job_submit(job);
+
+	done = drm_mock_sched_job_wait_scheduled(job, HZ);
+	KUNIT_ASSERT_TRUE(test, done);
+
+	drm_mock_sched_entity_free(entity);
+	drm_mock_sched_fini(sched);
+	dma_fence_put(stub);

it is necessary to release/free the job "drm_mock_sched_job_free(job)".

Regards,
Arvind

Quoted 19 lines
drivers/gpu/drm/scheduler/tests/tests_basic.c +14 -1
+}
+
+static struct kunit_case drm_sched_prealloc_tests[] = {
+	KUNIT_CASE(drm_sched_test_prealloc),
+	{}
+};
+
+static struct kunit_suite drm_sched_prealloc = {
+	.name = "drm_sched_basic_prealloc_tests",
+	.test_cases = drm_sched_prealloc_tests,
+};
+
  kunit_test_suites(&drm_sched_basic,
  		  &drm_sched_timeout,
  		  &drm_sched_priority,
  		  &drm_sched_modify_sched,
-		  &drm_sched_credits);
+		  &drm_sched_credits,
+		  &drm_sched_prealloc);
author reviewer
#17 lore raw

PATCH 3/3 drm/amdgpu: fix gang submission error handling

In reply to Christian König

For the unlikely case that we ran into an ENOMEM while fixing up the gang submission dependencies we can't clean up any more since the gang members are already armed.

Fix this by using pre-allocated dependency slots and re-ordering the code, also fix a double unref since the fence reference is also dropped on error.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 49 +++++++++++++++-----------
 1 file changed, 28 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +28 -21
index 82df06a72ee0..b50a5532f4c6 100644
@@ -1282,6 +1282,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 {
 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
 	struct amdgpu_job *leader = p->gang_leader;
+	u32 fence_slots[AMDGPU_CS_GANG_SIZE];
 	struct amdgpu_bo_list_entry *e;
 	struct drm_gem_object *gobj;
 	unsigned long index;
@@ -1289,36 +1290,23 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 	uint64_t seq;
 	int r;
 
-	for (i = 0; i < p->gang_size; ++i)
-		drm_sched_job_arm(&p->jobs[i]->base);
-
+	/* Preallocate the memory for the gang dependencies */
 	for (i = 0; i < p->gang_size; ++i) {
-		struct dma_fence *fence;
-
-		if (p->jobs[i] == leader)
-			continue;
-
-		fence = &p->jobs[i]->base.s_fence->scheduled;
-		dma_fence_get(fence);
-		r = drm_sched_job_add_dependency(&leader->base, fence);
-		if (r) {
-			dma_fence_put(fence);
+		r = drm_sched_job_prealloc_dependency_slot(&leader->base,
+							   &fence_slots[i]);
+		if (r)
 			return r;
-		}
 	}
 
-	if (p->gang_size > 1) {
-		for (i = 0; i < p->gang_size; ++i)
-			amdgpu_job_set_gang_leader(p->jobs[i], leader);
-	}
-
-	/* No memory allocation is allowed while holding the notifier lock.
+	/*
+	 * No memory allocation is allowed while holding the notifier lock.
 	 * The lock is held until amdgpu_cs_submit is finished and fence is
 	 * added to BOs.
 	 */
 	mutex_lock(&p->adev->notifier_lock);
 
-	/* If userptr are invalidated after amdgpu_cs_parser_bos(), return
+	/*
+	 * If userptr are invalidated after amdgpu_cs_parser_bos(), return
 	 * -EAGAIN, drmIoctl in libdrm will restart the amdgpu_cs_ioctl.
 	 */
 	r = 0;
@@ -1333,6 +1321,25 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 		return r;
 	}
 
+	for (i = 0; i < p->gang_size; ++i)
+		drm_sched_job_arm(&p->jobs[i]->base);
+
+	for (i = 0; i < p->gang_size; ++i) {
+		struct dma_fence *fence;
+
+		if (p->jobs[i] == leader)
+			continue;
+
+		fence = dma_fence_get(&p->jobs[i]->base.s_fence->scheduled);
+		drm_sched_job_add_prealloc_dep(&leader->base, fence_slots[i],
+					       fence);
+	}
+
+	if (p->gang_size > 1) {
+		for (i = 0; i < p->gang_size; ++i)
+			amdgpu_job_set_gang_leader(p->jobs[i], leader);
+	}
+
 	p->fence = dma_fence_get(&leader->base.s_fence->finished);
 	drm_exec_for_each_locked_object(&p->exec, index, gobj) {
 
Signature · 1 line
2.34.1