Skip to contentSkip to conversation

drm/sched: add drm_sched_prealloc_dependency_slots v2

PATCH In review Christian König 3 participants 11 messages Mar 21, 2025 – Apr 14, 2025

Subsystems (4)
lore
Share & tools
lore

Christian König

author reviewer
#0 lore raw

PATCH 1/2 drm/sched: add drm_sched_prealloc_dependency_slots v2

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

---
 drivers/gpu/drm/scheduler/sched_main.c | 44 ++++++++++++++++++++++++--
 include/drm/gpu_scheduler.h            |  2 ++
 2 files changed, 43 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +41 -3
index 4d4219fbe49d..ee3701f346b2 100644
@@ -852,6 +852,39 @@ void drm_sched_job_arm(struct drm_sched_job *job)
 }
 EXPORT_SYMBOL(drm_sched_job_arm);
 
+/**
+ * drm_sched_job_prealloc_dependency_slots - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @num_deps: number of dependencies to preallocate slots for
+  *
+ * 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.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slots(struct drm_sched_job *job,
+					    unsigned int num_deps)
+{
+	u32 id = 0;
+	int ret;
+
+	while (num_deps--) {
+		ret = xa_alloc(&job->dependencies, &id, XA_ZERO_ENTRY,
+			       xa_limit_32b, GFP_KERNEL);
+		if (ret != 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slots);
+
 /**
  * drm_sched_job_add_dependency - adds the fence as a job dependency
  * @job: scheduler job to add the dependencies to
@@ -878,10 +911,15 @@ int drm_sched_job_add_dependency(struct drm_sched_job *job,
 	 * engines involved, rather than the number of BOs.
 	 */
 	xa_for_each(&job->dependencies, index, entry) {
-		if (entry->context != fence->context)
+		if (xa_is_zero(entry)) {
+			/*
+			 * Reserved entries must not alloc memory, but let's
+			 * use GFP_ATOMIC just to be on the defensive side.
+			*/
+			xa_store(&job->dependencies, index, fence, GFP_ATOMIC);
+		} else if (entry->context != fence->context) {
 			continue;
-
-		if (dma_fence_is_later(fence, entry)) {
+		} else if (dma_fence_is_later(fence, entry)) {
 			dma_fence_put(entry);
 			xa_store(&job->dependencies, index, fence, GFP_KERNEL);
 		} else {
include/drm/gpu_scheduler.h +2 -0
index 1a7e377d4cbb..916e820b27ff 100644
@@ -632,6 +632,8 @@ 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_slots(struct drm_sched_job *job,
+					    unsigned int num_deps);
 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
author reviewer
#1 lore raw

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

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 | 57 +++++++++++++++-----------
 1 file changed, 33 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +33 -24
index 5cc5f59e3018..25e7f7d356d7 100644
@@ -1285,36 +1285,21 @@ 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);
-
-	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);
-			return r;
-		}
-	}
-
-	if (p->gang_size > 1) {
-		for (i = 0; i < p->gang_size; ++i)
-			amdgpu_job_set_gang_leader(p->jobs[i], leader);
-	}
+	/* Preallocate the memory for the gang dependencies */
+	r = drm_sched_job_prealloc_dependency_slots(&leader->base,
+						    p->gang_size - 1);
+	if (r)
+		return r;
 
-	/* 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;
@@ -1329,6 +1314,30 @@ 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);
+		r = drm_sched_job_add_dependency(&leader->base, fence);
+		/*
+		 * We can't abort here with an error any more, but we should
+		 * also never run into an error since the slots for the
+		 * dependency fences are preallocated.
+		 */
+		WARN_ON(r);
+	}
+
+	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
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix gang submission error handling

Quoted 84 lines · 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 | 57 +++++++++++++++-----------
  1 file changed, 33 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +26 -24
index 5cc5f59e3018..25e7f7d356d7 100644
@@ -1285,36 +1285,21 @@ 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);
-
-	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);
-			return r;
-		}
-	}
-
-	if (p->gang_size > 1) {
-		for (i = 0; i < p->gang_size; ++i)
-			amdgpu_job_set_gang_leader(p->jobs[i], leader);
-	}
+	/* Preallocate the memory for the gang dependencies */
+	r = drm_sched_job_prealloc_dependency_slots(&leader->base,
+						    p->gang_size - 1);
+	if (r)
+		return r;
 
-	/* 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;
@@ -1329,6 +1314,30 @@ 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);
+		r = drm_sched_job_add_dependency(&leader->base, fence);
+		/*
+		 * We can't abort here with an error any more, but we should
+		 * also never run into an error since the slots for the
+		 * dependency fences are preallocated.
+		 */
+		WARN_ON(r);

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, · line 1343

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +7 -0
+	}
+
+	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) {
 

Regards,

Tvrtko

#3 lore raw
In reply to Christian König
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 5 lines

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

v2: rework implementation an documentation

For drm/sched Danilo & I think that changelogs shouldn't be in the commit message anymore. The Link: applied by the DRM tools will be sufficient to find the history in the archives if necessary.

Quoted 48 lines

---
 drivers/gpu/drm/scheduler/sched_main.c | 44
++++++++++++++++++++++++--
 include/drm/gpu_scheduler.h            |  2 ++

 2 files changed, 43 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 4d4219fbe49d..ee3701f346b2 100644
@@ -852,6 +852,39 @@ void drm_sched_job_arm(struct drm_sched_job
 *job)
  }
  EXPORT_SYMBOL(drm_sched_job_arm);


+/**
+ * drm_sched_job_prealloc_dependency_slots - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @num_deps: number of dependencies to preallocate slots for +  *
+ * 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.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slots(struct drm_sched_job *job,

drivers/gpu/drm/scheduler/sched_main.c +7 -0
+					    unsigned int num_deps)
+{
+	u32 id = 0;
+	int ret;
+
+	while (num_deps--) {
+		ret = xa_alloc(&job->dependencies, &id,

XA_ZERO_ENTRY,

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

Quoted 1 line
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			       xa_limit_32b, GFP_KERNEL);

Fine by me, but out of curiousity about the xarray: you mentioned xa_reserve() might work, too?

P.

Quoted 50 lines
drivers/gpu/drm/scheduler/sched_main.c +8 -0
+		if (ret != 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slots);
+

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

drivers/gpu/drm/scheduler/sched_main.c +3 -1
@@ -878,10 +911,15 @@ int drm_sched_job_add_dependency(struct
 drm_sched_job *job,
  	 * engines involved, rather than the number of BOs.
  	 */
  	xa_for_each(&job->dependencies, index, entry) {
-		if (entry->context != fence->context)
+		if (xa_is_zero(entry)) {
+			/*
+			 * Reserved entries must not alloc memory,

but let's

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * use GFP_ATOMIC just to be on the

defensive side.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			*/
+			xa_store(&job->dependencies, index, fence,

GFP_ATOMIC);

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		} else if (entry->context != fence->context) {

  continue;

drivers/gpu/drm/scheduler/sched_main.c +1 -2
-
-		if (dma_fence_is_later(fence, entry)) {
+		} else if (dma_fence_is_later(fence, entry)) {

  dma_fence_put(entry); xa_store(&job->dependencies, index, fence, GFP_KERNEL);
  } else {

include/drm/gpu_scheduler.h +2 -0
index 1a7e377d4cbb..916e820b27ff 100644
@@ -632,6 +632,8 @@ 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_slots(struct drm_sched_job
 *job,
+					    unsigned int num_deps);
  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
#4 lore raw

Am 21.03.25 um 19:05 schrieb Philipp Stanner:

Quoted 8 lines · Christian König
Quoted 7 lines

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 8 lines
Quoted 5 lines

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

v2: rework implementation an documentation

For drm/sched Danilo & I think that changelogs shouldn't be in the commit message anymore. The Link: applied by the DRM tools will be sufficient to find the history in the archives if necessary.

Sure, going to drop that.

Quoted 43 lines
Quoted 43 lines

---
 drivers/gpu/drm/scheduler/sched_main.c | 44
++++++++++++++++++++++++--
 include/drm/gpu_scheduler.h            |  2 ++

 2 files changed, 43 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 4d4219fbe49d..ee3701f346b2 100644
@@ -852,6 +852,39 @@ void drm_sched_job_arm(struct drm_sched_job
 *job)
  }
  EXPORT_SYMBOL(drm_sched_job_arm);


+/**
+ * drm_sched_job_prealloc_dependency_slots - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @num_deps: number of dependencies to preallocate slots for +  *
+ * 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.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slots(struct drm_sched_job *job,

drivers/gpu/drm/scheduler/sched_main.c +3 -0
+					    unsigned int num_deps)
+{
+	u32 id = 0;
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +4 -0
+	int ret;
+
+	while (num_deps--) {
+		ret = xa_alloc(&job->dependencies, &id,

XA_ZERO_ENTRY,

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			       xa_limit_32b, GFP_KERNEL);

Fine by me, but out of curiousity about the xarray: you mentioned xa_reserve() might work, too?

Different use case, xa_reserve() can only reserve a specific index and not allocate one.

Allocating and reserving at the same time seems to be done by the pattern xa_alloc(...XA_ZERO_ENTRY...) like we use here.

Regards,
Christian.

Quoted 54 lines

P.

Quoted 50 lines
drivers/gpu/drm/scheduler/sched_main.c +8 -0
+		if (ret != 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slots);
+

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

drivers/gpu/drm/scheduler/sched_main.c +3 -1
@@ -878,10 +911,15 @@ int drm_sched_job_add_dependency(struct
 drm_sched_job *job,
  	 * engines involved, rather than the number of BOs.
  	 */
  	xa_for_each(&job->dependencies, index, entry) {
-		if (entry->context != fence->context)
+		if (xa_is_zero(entry)) {
+			/*
+			 * Reserved entries must not alloc memory,

but let's

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * use GFP_ATOMIC just to be on the

defensive side.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			*/
+			xa_store(&job->dependencies, index, fence,

GFP_ATOMIC);

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		} else if (entry->context != fence->context) {

  continue;

drivers/gpu/drm/scheduler/sched_main.c +1 -2
-
-		if (dma_fence_is_later(fence, entry)) {
+		} else if (dma_fence_is_later(fence, entry)) {

  dma_fence_put(entry); xa_store(&job->dependencies, index, fence, GFP_KERNEL);
  } else {

include/drm/gpu_scheduler.h +2 -0
index 1a7e377d4cbb..916e820b27ff 100644
@@ -632,6 +632,8 @@ 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_slots(struct drm_sched_job
 *job,
+					    unsigned int num_deps);
  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,

#5 lore raw
In reply to Christian König
Quoted 61 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

---
  drivers/gpu/drm/scheduler/sched_main.c | 44 ++++++++++++++++++++++++--
  include/drm/gpu_scheduler.h            |  2 ++
  2 files changed, 43 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +33 -0
index 4d4219fbe49d..ee3701f346b2 100644
@@ -852,6 +852,39 @@ void drm_sched_job_arm(struct drm_sched_job *job)
  }
  EXPORT_SYMBOL(drm_sched_job_arm);
 
+/**
+ * drm_sched_job_prealloc_dependency_slots - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @num_deps: number of dependencies to preallocate slots for
+  *
+ * 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.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slots(struct drm_sched_job *job,
+					    unsigned int num_deps)
+{
+	u32 id = 0;
+	int ret;
+
+	while (num_deps--) {
+		ret = xa_alloc(&job->dependencies, &id, XA_ZERO_ENTRY,
+			       xa_limit_32b, GFP_KERNEL);
+		if (ret != 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slots);
+
  /**
   * drm_sched_job_add_dependency - adds the fence as a job dependency
   * @job: scheduler job to add the dependencies to

drivers/gpu/drm/scheduler/sched_main.c int drm_sched_job_add_dependency(struct drm_sched_job *job, · line 914

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
@@ -878,10 +911,15 @@ int drm_sched_job_add_dependency(struct drm_sched_job *job,
  	 * engines involved, rather than the number of BOs.
  	 */
  	xa_for_each(&job->dependencies, index, entry) {
-		if (entry->context != fence->context)
+		if (xa_is_zero(entry)) {

If xa_for_each shows them I think we also need to skip them in drm_sched_job_dependency() and drm_sched_job_cleanup(). Or remove the unused ones at arm time. Hm, we could also put a noisy fail if add_dependency allocates after a job is armed. (I can imagine a few unit tests for this once we merge them.)

Regards,

Tvrtko

Quoted 26 lines
drivers/gpu/drm/scheduler/sched_main.c +7 -2
+			/*
+			 * Reserved entries must not alloc memory, but let's
+			 * use GFP_ATOMIC just to be on the defensive side.
+			*/
+			xa_store(&job->dependencies, index, fence, GFP_ATOMIC);
+		} else if (entry->context != fence->context) {
  			continue;
-
-		if (dma_fence_is_later(fence, entry)) {
+		} else if (dma_fence_is_later(fence, entry)) {
  			dma_fence_put(entry);
  			xa_store(&job->dependencies, index, fence, GFP_KERNEL);
  		} else {
include/drm/gpu_scheduler.h +2 -0
index 1a7e377d4cbb..916e820b27ff 100644
@@ -632,6 +632,8 @@ 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_slots(struct drm_sched_job *job,
+					    unsigned int num_deps);
  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
#6 lore raw

Am 24.03.25 um 09:35 schrieb Tvrtko Ursulin:

Quoted 61 lines · Christian König
Quoted 59 lines

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

---
  drivers/gpu/drm/scheduler/sched_main.c | 44 ++++++++++++++++++++++++-- include/drm/gpu_scheduler.h            |  2 ++

  2 files changed, 43 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 4d4219fbe49d..ee3701f346b2 100644
@@ -852,6 +852,39 @@ void drm_sched_job_arm(struct drm_sched_job *job)
   }
   EXPORT_SYMBOL(drm_sched_job_arm);

  +/**
+ * drm_sched_job_prealloc_dependency_slots - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @num_deps: number of dependencies to preallocate slots for
+  *
+ * 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.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */

drivers/gpu/drm/scheduler/sched_main.c +17 -0
+int drm_sched_job_prealloc_dependency_slots(struct drm_sched_job *job,
+                        unsigned int num_deps)
+{
+    u32 id = 0;
+    int ret;
+
+    while (num_deps--) {
+        ret = xa_alloc(&job->dependencies, &id, XA_ZERO_ENTRY,
+                   xa_limit_32b, GFP_KERNEL);
+        if (ret != 0)
+            return ret;
+    }
+
+    return 0;
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slots);
+

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

Quoted 8 lines

drivers/gpu/drm/scheduler/sched_main.c int drm_sched_job_add_dependency(struct drm_sched_job *job, · line 914

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -1
@@ -878,10 +911,15 @@ int drm_sched_job_add_dependency(struct drm_sched_job *job,
        * engines involved, rather than the number of BOs.
        */
       xa_for_each(&job->dependencies, index, entry) {
-        if (entry->context != fence->context)
+        if (xa_is_zero(entry)) {

If xa_for_each shows them I think we also need to skip them in drm_sched_job_dependency() and drm_sched_job_cleanup().

Oh, really good point. I've completely forgotten about them.

Quoted 1 line

Or remove the unused ones at arm time. Hm, we could also put a noisy fail if add_dependency allocates after a job is armed. (I can imagine a few unit tests for this once we merge them.)

Good point as well. I was also waiting for your unit testing stuff to land in drm-misc-next so I can write a test for that.

Thanks,
Christian.

Quoted 32 lines

Regards,

Tvrtko

Quoted 26 lines
drivers/gpu/drm/scheduler/sched_main.c +7 -2
+            /*
+             * Reserved entries must not alloc memory, but let's
+             * use GFP_ATOMIC just to be on the defensive side.
+            */
+            xa_store(&job->dependencies, index, fence, GFP_ATOMIC);
+        } else if (entry->context != fence->context) {
               continue;
-
-        if (dma_fence_is_later(fence, entry)) {
+        } else if (dma_fence_is_later(fence, entry)) {
               dma_fence_put(entry);
               xa_store(&job->dependencies, index, fence, GFP_KERNEL);
           } else {
include/drm/gpu_scheduler.h +2 -0
index 1a7e377d4cbb..916e820b27ff 100644
@@ -632,6 +632,8 @@ 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_slots(struct drm_sched_job *job,
+                        unsigned int num_deps);
   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,
#7 lore raw
In reply to Christian König
Quoted 60 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

---
 drivers/gpu/drm/scheduler/sched_main.c | 44
++++++++++++++++++++++++--
 include/drm/gpu_scheduler.h            |  2 ++

 2 files changed, 43 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 4d4219fbe49d..ee3701f346b2 100644
@@ -852,6 +852,39 @@ void drm_sched_job_arm(struct drm_sched_job
 *job)
  }
  EXPORT_SYMBOL(drm_sched_job_arm);


+/**
+ * drm_sched_job_prealloc_dependency_slots - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @num_deps: number of dependencies to preallocate slots for +  *
+ * 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.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slots(struct drm_sched_job *job,

drivers/gpu/drm/scheduler/sched_main.c +7 -0
+					    unsigned int num_deps)
+{
+	u32 id = 0;
+	int ret;
+
+	while (num_deps--) {
+		ret = xa_alloc(&job->dependencies, &id,

XA_ZERO_ENTRY,

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

Quoted 1 line
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			       xa_limit_32b, GFP_KERNEL);

I've had some time to re-read the xarray documentation and I think that this is what xa_reserve() was born for. The Book of Documentation/core- api/xarray.rst sayeth:

"Sometimes you need to ensure that a subsequent call to xa_store() will not need to allocate memory. The xa_reserve() function will store a reserved entry at the indicated index. Users of the normal API will see this entry as containing ``NULL``."

That's far better, this way we don't have to use that more or less xarray-internal flag.

Quoted 25 lines
drivers/gpu/drm/scheduler/sched_main.c +8 -0
+		if (ret != 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slots);
+

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

drivers/gpu/drm/scheduler/sched_main.c +3 -1
@@ -878,10 +911,15 @@ int drm_sched_job_add_dependency(struct
 drm_sched_job *job,
  	 * engines involved, rather than the number of BOs.
  	 */
  	xa_for_each(&job->dependencies, index, entry) {
-		if (entry->context != fence->context)
+		if (xa_is_zero(entry)) {
+			/*
+			 * Reserved entries must not alloc memory,

but let's

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * use GFP_ATOMIC just to be on the

defensive side.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			*/
+			xa_store(&job->dependencies, index, fence,
Quoted 1 line

GFP_ATOMIC);

And regarding this – it can actually never happen, but you provide ATOMIC just to be sure?

I think it would be better if we'd just run into an obvious bug here instead, so like a deadlock with GFP_KERNEL.

That's how we do it with pointers that cannot be NULL, too. If the impossible were to happen and it were NULL, we'd crash.

P.

Quoted 24 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		} else if (entry->context != fence->context) {

  continue;

drivers/gpu/drm/scheduler/sched_main.c +1 -2
-
-		if (dma_fence_is_later(fence, entry)) {
+		} else if (dma_fence_is_later(fence, entry)) {

  dma_fence_put(entry); xa_store(&job->dependencies, index, fence, GFP_KERNEL);
  } else {

include/drm/gpu_scheduler.h +2 -0
index 1a7e377d4cbb..916e820b27ff 100644
@@ -632,6 +632,8 @@ 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_slots(struct drm_sched_job
 *job,
+					    unsigned int num_deps);
  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
#8 lore raw

Am 09.04.25 um 12:28 schrieb Philipp Stanner:

Quoted 66 lines · Christian König
Quoted 60 lines

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

---
 drivers/gpu/drm/scheduler/sched_main.c | 44
++++++++++++++++++++++++--
 include/drm/gpu_scheduler.h            |  2 ++

 2 files changed, 43 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 4d4219fbe49d..ee3701f346b2 100644
@@ -852,6 +852,39 @@ void drm_sched_job_arm(struct drm_sched_job
 *job)
  }
  EXPORT_SYMBOL(drm_sched_job_arm);


+/**
+ * drm_sched_job_prealloc_dependency_slots - avoid ENOMEM on adding dependencies
+ * @job: scheduler job where dependencies will be added
+ * @num_deps: number of dependencies to preallocate slots for +  *
+ * 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.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slots(struct drm_sched_job *job,

drivers/gpu/drm/scheduler/sched_main.c +7 -0
+					    unsigned int num_deps)
+{
+	u32 id = 0;
+	int ret;
+
+	while (num_deps--) {
+		ret = xa_alloc(&job->dependencies, &id,

XA_ZERO_ENTRY,

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

Quoted 1 line
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			       xa_limit_32b, GFP_KERNEL);

I've had some time to re-read the xarray documentation and I think that this is what xa_reserve() was born for. The Book of Documentation/core- api/xarray.rst sayeth:

Quoted 7 lines

"Sometimes you need to ensure that a subsequent call to xa_store() will not need to allocate memory. The xa_reserve() function will store a reserved entry at the indicated index. Users of the normal API will see this entry as containing ``NULL``."

That's far better, this way we don't have to use that more or less xarray-internal flag.

Yeah I have seen that as well. The reason why I didn't followed this route was that I wasn't sure if I then need to check for NULL entries while iterating over the XA.

Additional to that I couldn't figure out of hand how to determine a the next free index slot.

Have you found any example how to use that? I mean the documentation could certainly be improved a bit.

Regards,
Christian.

Quoted 63 lines
Quoted 27 lines
drivers/gpu/drm/scheduler/sched_main.c +8 -0
+		if (ret != 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slots);
+

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

drivers/gpu/drm/scheduler/sched_main.c +3 -1
@@ -878,10 +911,15 @@ int drm_sched_job_add_dependency(struct
 drm_sched_job *job,
  	 * engines involved, rather than the number of BOs.
  	 */
  	xa_for_each(&job->dependencies, index, entry) {
-		if (entry->context != fence->context)
+		if (xa_is_zero(entry)) {
+			/*
+			 * Reserved entries must not alloc memory,

but let's

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * use GFP_ATOMIC just to be on the

defensive side.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			*/
+			xa_store(&job->dependencies, index, fence,
Quoted 1 line

GFP_ATOMIC);

And regarding this – it can actually never happen, but you provide ATOMIC just to be sure?

I think it would be better if we'd just run into an obvious bug here instead, so like a deadlock with GFP_KERNEL.

That's how we do it with pointers that cannot be NULL, too. If the impossible were to happen and it were NULL, we'd crash.

P.

Quoted 24 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		} else if (entry->context != fence->context) {

  continue;

drivers/gpu/drm/scheduler/sched_main.c +1 -2
-
-		if (dma_fence_is_later(fence, entry)) {
+		} else if (dma_fence_is_later(fence, entry)) {

  dma_fence_put(entry); xa_store(&job->dependencies, index, fence, GFP_KERNEL);
  } else {

include/drm/gpu_scheduler.h +2 -0
index 1a7e377d4cbb..916e820b27ff 100644
@@ -632,6 +632,8 @@ 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_slots(struct drm_sched_job
 *job,
+					    unsigned int num_deps);
  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,

#9 lore raw

+Cc Matthew

Quoted 87 lines · Christian König

Am 09.04.25 um 12:28 schrieb Philipp Stanner:

Quoted 74 lines · Christian König
Quoted 66 lines

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

---
 drivers/gpu/drm/scheduler/sched_main.c | 44
++++++++++++++++++++++++--
 include/drm/gpu_scheduler.h            |  2 ++

 2 files changed, 43 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 4d4219fbe49d..ee3701f346b2 100644
@@ -852,6 +852,39 @@ void drm_sched_job_arm(struct drm_sched_job
 *job)
  }
  EXPORT_SYMBOL(drm_sched_job_arm);


+/**
+ * drm_sched_job_prealloc_dependency_slots - avoid ENOMEM on adding
dependencies
+ * @job: scheduler job where dependencies will be added
+ * @num_deps: number of dependencies to preallocate slots for +  *
+ * 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.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slots(struct drm_sched_job *job,

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

num_deps)

drivers/gpu/drm/scheduler/sched_main.c +6 -0
+{
+	u32 id = 0;
+	int ret;
+
+	while (num_deps--) {
+		ret = xa_alloc(&job->dependencies, &id,

XA_ZERO_ENTRY,

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

Quoted 1 line
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			       xa_limit_32b, GFP_KERNEL);

I've had some time to re-read the xarray documentation and I think that
this is what xa_reserve() was born for. The Book of Documentation/core-
api/xarray.rst sayeth:

Quoted 7 lines

"Sometimes you need to ensure that a subsequent call to xa_store() will not need to allocate memory.  The xa_reserve() function will store a reserved entry at the indicated index.  Users of the normal API will see this entry as containing ``NULL``."

That's far better, this way we don't have to use that more or less xarray-internal flag.

Yeah I have seen that as well. The reason why I didn't followed this route was that I wasn't sure if I then need to check for NULL entries while iterating over the XA.

Quoted 5 lines

Additional to that I couldn't figure out of hand how to determine a the next free index slot.

Have you found any example how to use that? I mean the documentation could certainly be improved a bit.

Maybe Matthew can help us out here?

Matthew, what would be the idiomatic way to do this, and can we help out with improving the Xarray's documentation?

Thx,
P.

Quoted 76 lines

Regards,
Christian.

Quoted 71 lines
Quoted 29 lines
drivers/gpu/drm/scheduler/sched_main.c +8 -0
+		if (ret != 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slots);
+

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

drivers/gpu/drm/scheduler/sched_main.c +3 -1
@@ -878,10 +911,15 @@ int drm_sched_job_add_dependency(struct
 drm_sched_job *job,
  	 * engines involved, rather than the number of BOs.
  	 */
  	xa_for_each(&job->dependencies, index, entry) {
-		if (entry->context != fence->context)
+		if (xa_is_zero(entry)) {
+			/*
+			 * Reserved entries must not alloc
 memory,

but let's

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * use GFP_ATOMIC just to be on the

defensive side.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			*/
+			xa_store(&job->dependencies, index,
Quoted 2 lines

fence,
GFP_ATOMIC);

And regarding this – it can actually never happen, but you provide ATOMIC just to be sure?

I think it would be better if we'd just run into an obvious bug here
instead, so like a deadlock with GFP_KERNEL.

That's how we do it with pointers that cannot be NULL, too. If the impossible were to happen and it were NULL, we'd crash.

P.

Quoted 28 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		} else if (entry->context != fence->context) {

  continue;

drivers/gpu/drm/scheduler/sched_main.c +1 -2
-
-		if (dma_fence_is_later(fence, entry)) {
+		} else if (dma_fence_is_later(fence, entry)) {

  dma_fence_put(entry); xa_store(&job->dependencies, index, fence,
GFP_KERNEL);
  } else {

include/drm/gpu_scheduler.h +2 -0
index 1a7e377d4cbb..916e820b27ff 100644
@@ -632,6 +632,8 @@ 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_slots(struct drm_sched_job
 *job,
+					    unsigned int
 num_deps);

 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,

#10 lore raw
Quoted 93 lines · Philipp Stanner

+Cc Matthew

Quoted 90 lines · Christian König

Am 09.04.25 um 12:28 schrieb Philipp Stanner:

Quoted 89 lines · Christian König
Quoted 71 lines

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

---
 drivers/gpu/drm/scheduler/sched_main.c | 44
++++++++++++++++++++++++--
 include/drm/gpu_scheduler.h            |  2 ++

 2 files changed, 43 insertions(+), 3 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 4d4219fbe49d..ee3701f346b2 100644
@@ -852,6 +852,39 @@ void drm_sched_job_arm(struct

drm_sched_job
*job)
 }
 EXPORT_SYMBOL(drm_sched_job_arm);

+/**
+ * drm_sched_job_prealloc_dependency_slots - avoid ENOMEM on adding
dependencies
+ * @job: scheduler job where dependencies will be added
+ * @num_deps: number of dependencies to preallocate slots for +  *
+ * 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.
+ *
+ * Return:
+ * 0 on success, or an error on failing to expand the array.
+ */
+int drm_sched_job_prealloc_dependency_slots(struct drm_sched_job
*job,

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

num_deps)

drivers/gpu/drm/scheduler/sched_main.c +6 -0
+{
+	u32 id = 0;
+	int ret;
+
+	while (num_deps--) {
+		ret = xa_alloc(&job->dependencies, &id,

XA_ZERO_ENTRY,

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

Quoted 1 line
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			       xa_limit_32b, GFP_KERNEL);

I've had some time to re-read the xarray documentation and I think
that
this is what xa_reserve() was born for. The Book of Documentation/core-
api/xarray.rst sayeth:

"Sometimes you need to ensure that a subsequent call to xa_store()
will not need to allocate memory.  The xa_reserve() function will store a reserved entry at the indicated index.  Users of the normal API will see this entry as containing ``NULL``."

That's far better, this way we don't have to use that more or less
xarray-internal flag.

I've tried to look through the code and think it through…

Quoted 6 lines
Quoted 6 lines

Yeah I have seen that as well. The reason why I didn't followed this
route was that I wasn't sure if I then need to check for NULL entries
while iterating over the XA.

AFAICS, when you use xa_reserve(), xa_load() and xa_for_each() will return NULL for the reserved entries – therefore potentially blowing up the scheduler without NULL checks, when someone uses the new prealloc function without actually filling in the dependencies later. At least the documentation says so:

"The xa_reserve() function will store a reserved entry at the indicated index. Users of the normal API will see this entry as containing ``NULL``."

So that's definitely not a good idea.

BUT the same seems to be the case for xa_alloc(…, XA_ZERO_ENTRY, …)?

xa_load() will *definitely* return NULL, since it utilizes xa_zero_to_null(). We have one use, in sched_entity.c. That use should only ever evaluate a valid dependency, so can't realistically speaking be NULL.

So the more interesting question is how, xa_for_each(), our main work horse, behaves. It uses xa_find(), which uses xas_find(), which… seems to be OK?

xa_find()'s docu says:
"* Return: The entry, if found, otherwise %NULL."

???

I agree we should aim for documenting that better. It could also make sense to _consider_ changing xa_for_each() so that it doesn't return reserved entries, but only 'used' entries.

P.

Quoted 97 lines
Quoted 7 lines

Additional to that I couldn't figure out of hand how to determine a the next free index slot.

Have you found any example how to use that? I mean the documentation
could certainly be improved a bit.

Maybe Matthew can help us out here?

Matthew, what would be the idiomatic way to do this, and can we help out with improving the Xarray's documentation?

Thx,
P.

Quoted 80 lines

Regards,
Christian.

Quoted 75 lines
Quoted 29 lines
drivers/gpu/drm/scheduler/sched_main.c +8 -0
+		if (ret != 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_sched_job_prealloc_dependency_slots);
+

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

drivers/gpu/drm/scheduler/sched_main.c +3 -1
@@ -878,10 +911,15 @@ int drm_sched_job_add_dependency(struct
 drm_sched_job *job,
  	 * engines involved, rather than the number of BOs.
  	 */
  	xa_for_each(&job->dependencies, index, entry) {
-		if (entry->context != fence->context)
+		if (xa_is_zero(entry)) {
+			/*
+			 * Reserved entries must not alloc
 memory,

but let's

drivers/gpu/drm/scheduler/sched_main.c +1 -0
+			 * use GFP_ATOMIC just to be on the

defensive side.

drivers/gpu/drm/scheduler/sched_main.c +2 -0
+			*/
+			xa_store(&job->dependencies, index,
Quoted 2 lines

fence,
GFP_ATOMIC);

And regarding this – it can actually never happen, but you provide
ATOMIC just to be sure?

I think it would be better if we'd just run into an obvious bug here
instead, so like a deadlock with GFP_KERNEL.

That's how we do it with pointers that cannot be NULL, too. If the
impossible were to happen and it were NULL, we'd crash.

P.

Quoted 30 lines
drivers/gpu/drm/scheduler/sched_main.c +1 -0
+		} else if (entry->context != fence->context) {

  continue;

drivers/gpu/drm/scheduler/sched_main.c +1 -2
-
-		if (dma_fence_is_later(fence, entry)) {
+		} else if (dma_fence_is_later(fence, entry)) {

  dma_fence_put(entry); xa_store(&job->dependencies, index, fence,
GFP_KERNEL);
  } else {

include/drm/gpu_scheduler.h +0 -0
index 1a7e377d4cbb..916e820b27ff 100644
@@ -632,6 +632,8 @@ 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_slots(struct drm_sched_job
*job,

include/drm/gpu_scheduler.h +1 -0
+					    unsigned int

num_deps);
 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,