Skip to contentSkip to conversation

accel/rocket: Parallelize independent jobs across cores

PATCH In review Gyeyoung Baek 2 participants 3 messages Dec 2

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/rocket: Parallelize independent jobs across cores

The jobs of models with independent branches, such as inception, could run concurrently, but they currently run one after the other on a single core.

For example, if jobs B1, B2 and B3 each read the output of job P, and none of them reads the output of another:

        .--> B1
  P ----+--> B2
        '--> B3
  now:                          with this patch:
  core0  [P ][B1][B2][B3]       core0  [P ][B1]
  core1                         core1      [B2]
  core2                         core2      [B3]
         ----------------> t           --------> t

This is because all the jobs of a submission go to a single entity, and an entity runs on one core at a time.

To fix this:
- Give each file one entity per core, and pick the core when queueing a job. Jobs are then only ordered by their BO dependencies. Document this in the uAPI.
- Pick, in this order: a core whose last job the job depends on, the least recently used idle core, the least recently used core.
- Picking the entity now needs the fences in the dma_resv of the BOs, which is shared between submissions and can only be read with its lock held. So move drm_sched_job_init(), which takes the entity, after the BOs are locked and the core is picked.

On RK3588 with Mesa main, comparing the inference time before and after this patch (mean of 8 runs of the median of 300 inferences), models with independent branches got faster. Models without branches also got slightly faster, as consecutive inferences now take turns on the idle cores.

with branches inception_v1 -11.3%

                     ssd_mobilenet_v2       -6.1%
                     ssdlite_mobiledet      -5.9%
  without branches   mobilenet_v2           -3.5%
                     mobilenet_v1           -1.6%

---
Depends on "accel/rocket: publish read fences for input buffers" (4d8cb7be620e in drm-misc-fixes).

 drivers/accel/rocket/rocket_core.h   |   5 +
 drivers/accel/rocket/rocket_device.h |   3 +
 drivers/accel/rocket/rocket_drv.h    |   5 +-
 drivers/accel/rocket/rocket_job.c    | 174 ++++++++++++++++++++-------
 include/uapi/drm/rocket_accel.h      |   3 +
 5 files changed, 146 insertions(+), 44 deletions(-)
drivers/accel/rocket/rocket_core.h +5 -0
index f6d7382854ca9..aaaa844539054 100644
@@ -55,6 +55,11 @@ struct rocket_core {
 	struct drm_gpu_scheduler sched;
 	u64 fence_context;
 	u64 emit_seqno;
+
+	/* Finished fence of the last job queued on this core, protected by sched_lock. */
+	struct dma_fence *last_fence;
+	/* Value of rocket_device.queued_jobs when @last_fence was queued. */
+	u64 last_queued;
 };
 
 int rocket_core_init(struct rocket_core *core);
drivers/accel/rocket/rocket_device.h +3 -0
index c62d567010696..319ce62f601b1 100644
@@ -17,6 +17,9 @@ struct rocket_device {
 
 	struct mutex sched_lock;
 
+	/* Number of jobs queued so far, protected by sched_lock. */
+	u64 queued_jobs;
+
 	struct rocket_core *cores;
 	unsigned int num_cores;
 	unsigned int max_cores;
drivers/accel/rocket/rocket_drv.h +3 -2
index 9421e48ec5d84..3bd14542353ad 100644
@@ -23,8 +23,9 @@ struct rocket_file_priv {
 	struct drm_mm mm;
 	struct mutex mm_lock;
 
-	struct drm_gpu_scheduler **scheds;
-	struct drm_sched_entity sched_entity;
+	/* One entity per core, see rocket_pick_core(). */
+	struct drm_sched_entity *sched_entities;
+	unsigned int num_sched_entities;
 };
 
 struct rocket_iommu_domain *rocket_iommu_domain_get(struct rocket_file_priv *rocket_priv);
drivers/accel/rocket/rocket_job.c +132 -42

245 lines not on this page · raw message

include/uapi/drm/rocket_accel.h +3 -0
index d0685e372b792..6c852e3d00aa3 100644
@@ -161,6 +161,9 @@ struct drm_rocket_job {
  * struct drm_rocket_submit - ioctl argument for submitting commands to the NPU.
  *
  * The kernel will schedule the execution of these jobs in dependency order.
+ * Dependencies, including those on jobs of earlier submissions, are only
+ * given by the BOs listed in each struct drm_rocket_job. Jobs that don't
+ * depend on each other may run concurrently on different cores.
  */
 struct drm_rocket_submit {
 	/**

base-commit: 28f286b0dfa9610d4ee2447415e964e8bc603056
prerequisite-patch-id: 5f2509e3f6505d397b609881d54814a3d7a47bd8

Signature · 1 line
2.43.0
#1 lore raw
Quoted 17 lines · Gyeyoung Baek

The jobs of models with independent branches, such as inception, could run concurrently, but they currently run one after the other on a single core.

For example, if jobs B1, B2 and B3 each read the output of job P, and none of them reads the output of another:

         .--> B1
   P ----+--> B2
         '--> B3
   now:                          with this patch:
   core0  [P ][B1][B2][B3]       core0  [P ][B1]
   core1                         core1      [B2]
   core2                         core2      [B3]
          ----------------> t           --------> t
Quoted 7 lines

This is because all the jobs of a submission go to a single entity, and an entity runs on one core at a time.

To fix this:
- Give each file one entity per core, and pick the core when queueing a job. Jobs are then only ordered by their BO dependencies. Document this in the uAPI.

Strictly speaking you could give all schedulers to all entities, right? Existing sched->score tracking *should* make sure different entities submitting in turn get assign different schedulers via drm_sched_pick_best().

With that I am not sure you would need to complicate things with own sched selection logic - sounds like it is partly implementing different drm_sched_pick_best().

In general, the problem with why a single entity does not work really is that drm_sched_entity_select_rq() has this artificial coupling preventing run queue / scheduler changes while there are queued or active jobs in the entity. In the ideal world it should work.

I touched so many broken DRM scheduler areas recently that I cannot from the top of my head remember how ingrained is this limitation. I *think* I tried to fix it by a larger rework couple years back but hit some issue. So I am now interested to remind myself what the issue was. If I manage to rememeber I will update the thread. :)

Anyway, I am just thinking out loud here. Feel free to proceed as you wish in rocket.

Regards,

Tvrtko

P.S. Adding DRM scheduler maintainers for visibility.

Quoted 348 lines

- Pick, in this order: a core whose last job the job depends on, the least recently used idle core, the least recently used core.
- Picking the entity now needs the fences in the dma_resv of the BOs, which is shared between submissions and can only be read with its lock held. So move drm_sched_job_init(), which takes the entity, after the BOs are locked and the core is picked.

On RK3588 with Mesa main, comparing the inference time before and after this patch (mean of 8 runs of the median of 300 inferences), models with independent branches got faster. Models without branches also got slightly faster, as consecutive inferences now take turns on the idle cores.

with branches inception_v1 -11.3%

                      ssd_mobilenet_v2       -6.1%
                      ssdlite_mobiledet      -5.9%
   without branches   mobilenet_v2           -3.5%
                      mobilenet_v1           -1.6%

---
Depends on "accel/rocket: publish read fences for input buffers" (4d8cb7be620e in drm-misc-fixes).

  drivers/accel/rocket/rocket_core.h   |   5 +
  drivers/accel/rocket/rocket_device.h |   3 +
  drivers/accel/rocket/rocket_drv.h    |   5 +-
  drivers/accel/rocket/rocket_job.c    | 174 ++++++++++++++++++++-------
  include/uapi/drm/rocket_accel.h      |   3 +
  5 files changed, 146 insertions(+), 44 deletions(-)
drivers/accel/rocket/rocket_core.h +5 -0
index f6d7382854ca9..aaaa844539054 100644
@@ -55,6 +55,11 @@ struct rocket_core {
  	struct drm_gpu_scheduler sched;
  	u64 fence_context;
  	u64 emit_seqno;
+
+	/* Finished fence of the last job queued on this core, protected by sched_lock. */
+	struct dma_fence *last_fence;
+	/* Value of rocket_device.queued_jobs when @last_fence was queued. */
+	u64 last_queued;
  };
 
  int rocket_core_init(struct rocket_core *core);
drivers/accel/rocket/rocket_device.h +3 -0
index c62d567010696..319ce62f601b1 100644
@@ -17,6 +17,9 @@ struct rocket_device {
 
  	struct mutex sched_lock;
 
+	/* Number of jobs queued so far, protected by sched_lock. */
+	u64 queued_jobs;
+
  	struct rocket_core *cores;
  	unsigned int num_cores;
  	unsigned int max_cores;
drivers/accel/rocket/rocket_drv.h +3 -2
index 9421e48ec5d84..3bd14542353ad 100644
@@ -23,8 +23,9 @@ struct rocket_file_priv {
  	struct drm_mm mm;
  	struct mutex mm_lock;
 
-	struct drm_gpu_scheduler **scheds;
-	struct drm_sched_entity sched_entity;
+	/* One entity per core, see rocket_pick_core(). */
+	struct drm_sched_entity *sched_entities;
+	unsigned int num_sched_entities;
  };
 
  struct rocket_iommu_domain *rocket_iommu_domain_get(struct rocket_file_priv *rocket_priv);
drivers/accel/rocket/rocket_job.c +132 -42
index d28167208dc4a..7542c148ac2d0 100644
@@ -210,11 +210,91 @@ static void rocket_attach_object_fences(struct drm_gem_object **bos,
  		dma_resv_add_fence(bos[i]->resv, fence, usage);
  }
 
-static int rocket_job_push(struct rocket_job *job)
+/* Returns true if @job has to wait for @fence because of its BOs. */
+static bool rocket_job_depends_on(struct rocket_job *job, struct dma_fence *fence)
  {
+	struct dma_resv_iter cursor;
+	struct dma_fence *dep;
+	u32 i;
+
+	for (i = 0; i < job->in_bo_count; i++) {
+		dma_resv_for_each_fence(&cursor, job->in_bos[i]->resv,
+					dma_resv_usage_rw(false), dep) {
+			if (dep == fence)
+				return true;
+		}
+	}
+
+	for (i = 0; i < job->out_bo_count; i++) {
+		dma_resv_for_each_fence(&cursor, job->out_bos[i]->resv,
+					dma_resv_usage_rw(true), dep) {
+			if (dep == fence)
+				return true;
+		}
+	}
+
+	return false;
+}
+
+/*
+ * Returns the core to queue @job on, the first of:
+ * 1. a core whose last queued job @job depends on,
+ * 2. the least recently used idle core,
+ * 3. the least recently used core.
+ *
+ * Called with the reservations of the BOs of @job and rdev->sched_lock held.
+ */
+static unsigned int rocket_pick_core(struct rocket_file_priv *file_priv,
+				     struct rocket_job *job)
+{
+	struct rocket_device *rdev = file_priv->rdev;
+	unsigned int i, best = 0;
+	bool found = false;
+
+	/* 1. */
+	for (i = 0; i < file_priv->num_sched_entities; i++) {
+		struct rocket_core *core = &rdev->cores[i];
+
+		if (!core->last_fence || dma_fence_is_signaled(core->last_fence))
+			continue;
+
+		if (rocket_job_depends_on(job, core->last_fence))
+			return i;
+	}
+
+	/* 2. */
+	for (i = 0; i < file_priv->num_sched_entities; i++) {
+		struct rocket_core *core = &rdev->cores[i];
+
+		if (core->last_fence && !dma_fence_is_signaled(core->last_fence))
+			continue;
+
+		if (!found || core->last_queued < rdev->cores[best].last_queued) {
+			best = i;
+			found = true;
+		}
+	}
+
+	if (found)
+		return best;
+
+	/* 3. */
+	for (i = 1; i < file_priv->num_sched_entities; i++) {
+		if (rdev->cores[i].last_queued < rdev->cores[best].last_queued)
+			best = i;
+	}
+
+	return best;
+}
+
+static int rocket_job_push(struct rocket_job *job, struct drm_file *file)
+{
+	struct rocket_file_priv *file_priv = file->driver_priv;
  	struct rocket_device *rdev = job->rdev;
+	struct drm_sched_entity *entity;
  	struct drm_gem_object **bos;
  	struct ww_acquire_ctx acquire_ctx;
+	struct rocket_core *core;
  	int bo_count;
  	int ret = 0;
 
@@ -231,24 +311,35 @@ static int rocket_job_push(struct rocket_job *job)
  	if (ret)
  		goto err;
 
-	ret = rocket_acquire_object_fences(job->in_bos, job->in_bo_count,
-					   &job->base, false);
-	if (ret)
-		goto err_unlock;
-
-	ret = rocket_acquire_object_fences(job->out_bos, job->out_bo_count,
-					   &job->base, true);
-	if (ret)
-		goto err_unlock;
-
  	scoped_guard(mutex, &rdev->sched_lock) {
+		entity = &file_priv->sched_entities[rocket_pick_core(file_priv, job)];
+		ret = drm_sched_job_init(&job->base, entity, 1, NULL, file->client_id);
+		if (ret)
+			goto err_unlock;
+
+		ret = rocket_acquire_object_fences(job->in_bos, job->in_bo_count,
+						   &job->base, false);
+		if (!ret)
+			ret = rocket_acquire_object_fences(job->out_bos, job->out_bo_count,
+							   &job->base, true);
+		if (ret) {
+			drm_sched_job_cleanup(&job->base);
+			goto err_unlock;
+		}
+
  		drm_sched_job_arm(&job->base);
 
  		job->inference_done_fence = dma_fence_get(&job->base.s_fence->finished);
+		core = container_of(job->base.sched, struct rocket_core, sched);
 
  		kref_get(&job->refcount); /* put by scheduler job completion */
 
  		drm_sched_entity_push_job(&job->base);
+
+		/* Record the job as the last one of its core, for rocket_pick_core(). */
+		dma_fence_put(core->last_fence);
+		core->last_fence = dma_fence_get(job->inference_done_fence);
+		core->last_queued = ++rdev->queued_jobs;
  	}
 
  	rocket_attach_object_fences(job->in_bos, job->in_bo_count,
@@ -530,6 +621,9 @@ void rocket_job_fini(struct rocket_core *core)
  {
  	drm_sched_fini(&core->sched);
 
+	dma_fence_put(core->last_fence);
+	core->last_fence = NULL;
+
  	cancel_work_sync(&core->reset.work);
  	destroy_workqueue(core->reset.wq);
  }
@@ -537,35 +631,42 @@ void rocket_job_fini(struct rocket_core *core)
  int rocket_job_open(struct rocket_file_priv *rocket_priv)
  {
  	struct rocket_device *rdev = rocket_priv->rdev;
-	struct drm_gpu_scheduler **scheds = kmalloc_objs(*scheds,
-							 rdev->num_cores);
  	unsigned int core;
  	int ret;
 
-	if (!scheds)
+	rocket_priv->sched_entities = kzalloc_objs(*rocket_priv->sched_entities,
+						   rdev->num_cores);
+	if (!rocket_priv->sched_entities)
  		return -ENOMEM;
 
-	for (core = 0; core < rdev->num_cores; core++)
-		scheds[core] = &rdev->cores[core].sched;
+	for (core = 0; core < rdev->num_cores; core++) {
+		struct drm_gpu_scheduler *sched = &rdev->cores[core].sched;
 
-	ret = drm_sched_entity_init(&rocket_priv->sched_entity,
-				    DRM_SCHED_PRIORITY_NORMAL,
-				    scheds,
-				    rdev->num_cores, NULL);
-	if (ret)
-		kfree(scheds);
-	else
-		rocket_priv->scheds = scheds;
+		ret = drm_sched_entity_init(&rocket_priv->sched_entities[core],
+					    DRM_SCHED_PRIORITY_NORMAL,
+					    &sched, 1, NULL);
+		if (ret)
+			goto err_destroy_entities;
+	}
+
+	rocket_priv->num_sched_entities = rdev->num_cores;
+
+	return 0;
 
+err_destroy_entities:
+	while (core--)
+		drm_sched_entity_destroy(&rocket_priv->sched_entities[core]);
+	kfree(rocket_priv->sched_entities);
  	return ret;
  }
 
  void rocket_job_close(struct rocket_file_priv *rocket_priv)
  {
-	struct drm_sched_entity *entity = &rocket_priv->sched_entity;
+	unsigned int core;
 
-	drm_sched_entity_destroy(entity);
-	kfree(rocket_priv->scheds);
+	for (core = 0; core < rocket_priv->num_sched_entities; core++)
+		drm_sched_entity_destroy(&rocket_priv->sched_entities[core]);
+	kfree(rocket_priv->sched_entities);
  }
 
  int rocket_job_is_idle(struct rocket_core *core)
@@ -602,37 +703,26 @@ static int rocket_ioctl_submit_job(struct drm_device *dev, struct drm_file *file
  	rjob->rdev = rdev;
  	rjob->domain = rocket_iommu_domain_get(file_priv);
 
-	ret = drm_sched_job_init(&rjob->base,
-				 &file_priv->sched_entity,
-				 1, NULL, file->client_id);
-	if (ret)
-		goto out_put_job;
-
  	ret = rocket_copy_tasks(dev, file, job, rjob);
  	if (ret)
-		goto out_cleanup_job;
+		goto out_put_job;
 
  	ret = drm_gem_objects_lookup(file, u64_to_user_ptr(job->in_bo_handles),
  				     job->in_bo_handle_count, &rjob->in_bos);
  	if (ret)
-		goto out_cleanup_job;
+		goto out_put_job;
 
  	rjob->in_bo_count = job->in_bo_handle_count;
 
  	ret = drm_gem_objects_lookup(file, u64_to_user_ptr(job->out_bo_handles),
  				     job->out_bo_handle_count, &rjob->out_bos);
  	if (ret)
-		goto out_cleanup_job;
+		goto out_put_job;
 
  	rjob->out_bo_count = job->out_bo_handle_count;
 
-	ret = rocket_job_push(rjob);
-	if (ret)
-		goto out_cleanup_job;
+	ret = rocket_job_push(rjob, file);
 
-out_cleanup_job:
-	if (ret)
-		drm_sched_job_cleanup(&rjob->base);
  out_put_job:
  	rocket_job_put(rjob);
 
include/uapi/drm/rocket_accel.h +3 -0
index d0685e372b792..6c852e3d00aa3 100644
@@ -161,6 +161,9 @@ struct drm_rocket_job {
   * struct drm_rocket_submit - ioctl argument for submitting commands to the NPU.
   *
   * The kernel will schedule the execution of these jobs in dependency order.
+ * Dependencies, including those on jobs of earlier submissions, are only
+ * given by the BOs listed in each struct drm_rocket_job. Jobs that don't
+ * depend on each other may run concurrently on different cores.
   */
  struct drm_rocket_submit {
  	/**

base-commit: 28f286b0dfa9610d4ee2447415e964e8bc603056
prerequisite-patch-id: 5f2509e3f6505d397b609881d54814a3d7a47bd8

#2 lore raw
Quoted 27 lines · Tvrtko Ursulin
Quoted 17 lines · Gyeyoung Baek

The jobs of models with independent branches, such as inception, could run concurrently, but they currently run one after the other on a single core.

For example, if jobs B1, B2 and B3 each read the output of job P, and none of them reads the output of another:

         .--> B1
   P ----+--> B2
         '--> B3

   now:                          with this patch:
   core0  [P ][B1][B2][B3]       core0  [P ][B1] core1                         core1      [B2] core2                         core2      [B3] ----------------> t           --------> t

Quoted 7 lines

This is because all the jobs of a submission go to a single entity, and an entity runs on one core at a time.

To fix this:
- Give each file one entity per core, and pick the core when queueing a job. Jobs are then only ordered by their BO dependencies. Document this in the uAPI.

Quoted 5 lines

Strictly speaking you could give all schedulers to all entities, right? Existing sched->score tracking *should* make sure different entities submitting in turn get assign different schedulers via drm_sched_pick_best().

With that I am not sure you would need to complicate things with own sched selection logic - sounds like it is partly implementing different drm_sched_pick_best().

In general, the problem with why a single entity does not work really is that drm_sched_entity_select_rq() has this artificial coupling preventing run queue / scheduler changes while there are queued or active jobs in the entity. In the ideal world it should work.

Scratch this, I got confused due context switching. One entity shouldn't execute in parallel to itself is fundamental.

Tvrtko

Quoted 352 lines

I touched so many broken DRM scheduler areas recently that I cannot from the top of my head remember how ingrained is this limitation. I *think* I tried to fix it by a larger rework couple years back but hit some issue. So I am now interested to remind myself what the issue was. If I manage to rememeber I will update the thread. :)

Anyway, I am just thinking out loud here. Feel free to proceed as you wish in rocket.

Regards,

Tvrtko

P.S. Adding DRM scheduler maintainers for visibility.

Quoted 341 lines

- Pick, in this order: a core whose last job the job depends on, the least recently used idle core, the least recently used core.
- Picking the entity now needs the fences in the dma_resv of the BOs, which is shared between submissions and can only be read with its lock held. So move drm_sched_job_init(), which takes the entity, after the BOs are locked and the core is picked.

On RK3588 with Mesa main, comparing the inference time before and after this patch (mean of 8 runs of the median of 300 inferences), models with independent branches got faster. Models without branches also got slightly faster, as consecutive inferences now take turns on the idle cores.

   with branches      inception_v1          -11.3% ssd_mobilenet_v2       -6.1% ssdlite_mobiledet      -5.9% without branches   mobilenet_v2           -3.5% mobilenet_v1           -1.6%

---
Depends on "accel/rocket: publish read fences for input buffers" (4d8cb7be620e in drm-misc-fixes).

  drivers/accel/rocket/rocket_core.h   |   5 + drivers/accel/rocket/rocket_device.h |   3 + drivers/accel/rocket/rocket_drv.h    |   5 +- drivers/accel/rocket/rocket_job.c    | 174 ++++++++++++++++++++------- include/uapi/drm/rocket_accel.h      |   3 +

  5 files changed, 146 insertions(+), 44 deletions(-)
drivers/accel/rocket/rocket_core.h +2 -0
index f6d7382854ca9..aaaa844539054 100644
@@ -55,6 +55,11 @@ struct rocket_core {
       struct drm_gpu_scheduler sched;
       u64 fence_context;
       u64 emit_seqno;
+
+    /* Finished fence of the last job queued on this core, protected 

by sched_lock. */
+    struct dma_fence *last_fence;
+    /* Value of rocket_device.queued_jobs when @last_fence was queued. */
+    u64 last_queued;
  };
  int rocket_core_init(struct rocket_core *core);

drivers/accel/rocket/rocket_device.h +3 -0
index c62d567010696..319ce62f601b1 100644
@@ -17,6 +17,9 @@ struct rocket_device {
       struct mutex sched_lock;
+    /* Number of jobs queued so far, protected by sched_lock. */
+    u64 queued_jobs;
+
       struct rocket_core *cores;
       unsigned int num_cores;
       unsigned int max_cores;
drivers/accel/rocket/rocket_drv.h +3 -2
index 9421e48ec5d84..3bd14542353ad 100644
@@ -23,8 +23,9 @@ struct rocket_file_priv {
       struct drm_mm mm;
       struct mutex mm_lock;
-    struct drm_gpu_scheduler **scheds;
-    struct drm_sched_entity sched_entity;
+    /* One entity per core, see rocket_pick_core(). */
+    struct drm_sched_entity *sched_entities;
+    unsigned int num_sched_entities;
   };
   struct rocket_iommu_domain *rocket_iommu_domain_get(struct rocket_file_priv *rocket_priv);
drivers/accel/rocket/rocket_job.c +32 -1
index d28167208dc4a..7542c148ac2d0 100644
@@ -210,11 +210,91 @@ static void rocket_attach_object_fences(struct 
 drm_gem_object **bos,
           dma_resv_add_fence(bos[i]->resv, fence, usage);
   }
-static int rocket_job_push(struct rocket_job *job)
+/* Returns true if @job has to wait for @fence because of its BOs. */
+static bool rocket_job_depends_on(struct rocket_job *job, struct 
 dma_fence *fence)
   {
+    struct dma_resv_iter cursor;
+    struct dma_fence *dep;
+    u32 i;
+
+    for (i = 0; i < job->in_bo_count; i++) {
+        dma_resv_for_each_fence(&cursor, job->in_bos[i]->resv,
+                    dma_resv_usage_rw(false), dep) {
+            if (dep == fence)
+                return true;
+        }
+    }
+
+    for (i = 0; i < job->out_bo_count; i++) {
+        dma_resv_for_each_fence(&cursor, job->out_bos[i]->resv,
+                    dma_resv_usage_rw(true), dep) {
+            if (dep == fence)
+                return true;
+        }
+    }
+
+    return false;
+}
+
+/*
+ * Returns the core to queue @job on, the first of:
+ * 1. a core whose last queued job @job depends on,
+ * 2. the least recently used idle core,
+ * 3. the least recently used core.
+ *
+ * Called with the reservations of the BOs of @job and rdev- 
Quoted 1 line

sched_lock held.

+ */

drivers/accel/rocket/rocket_job.c +12 -0
+static unsigned int rocket_pick_core(struct rocket_file_priv *file_priv,
+                     struct rocket_job *job)
+{
+    struct rocket_device *rdev = file_priv->rdev;
+    unsigned int i, best = 0;
+    bool found = false;
+
+    /* 1. */
+    for (i = 0; i < file_priv->num_sched_entities; i++) {
+        struct rocket_core *core = &rdev->cores[i];
+
+        if (!core->last_fence || dma_fence_is_signaled(core- 
Quoted 1 line

last_fence))

+            continue;

drivers/accel/rocket/rocket_job.c +10 -0
+
+        if (rocket_job_depends_on(job, core->last_fence))
+            return i;
+    }
+
+    /* 2. */
+    for (i = 0; i < file_priv->num_sched_entities; i++) {
+        struct rocket_core *core = &rdev->cores[i];
+
+        if (core->last_fence && !dma_fence_is_signaled(core- 
Quoted 1 line

last_fence))

+            continue;
+
+        if (!found || core->last_queued < rdev-

Quoted 1 line

cores[best].last_queued) {

+            best = i;
+            found = true;
+        }
+    }

drivers/accel/rocket/rocket_job.c +14 -0
+
+    if (found)
+        return best;
+
+    /* 3. */
+    for (i = 1; i < file_priv->num_sched_entities; i++) {
+        if (rdev->cores[i].last_queued < rdev->cores[best].last_queued)
+            best = i;
+    }
+
+    return best;
+}
+
+static int rocket_job_push(struct rocket_job *job, struct drm_file 

*file)
+{
+    struct rocket_file_priv *file_priv = file->driver_priv; struct rocket_device *rdev = job->rdev;
+    struct drm_sched_entity *entity;
      struct drm_gem_object **bos;
      struct ww_acquire_ctx acquire_ctx;
+    struct rocket_core *core;
      int bo_count;
      int ret = 0;

drivers/accel/rocket/rocket_job.c +15 -10
@@ -231,24 +311,35 @@ static int rocket_job_push(struct rocket_job *job)
       if (ret)
           goto err;
-    ret = rocket_acquire_object_fences(job->in_bos, job->in_bo_count,
-                       &job->base, false);
-    if (ret)
-        goto err_unlock;
-
-    ret = rocket_acquire_object_fences(job->out_bos, job->out_bo_count,
-                       &job->base, true);
-    if (ret)
-        goto err_unlock;
-
       scoped_guard(mutex, &rdev->sched_lock) {
+        entity = &file_priv- 
 >sched_entities[rocket_pick_core(file_priv, job)];
+        ret = drm_sched_job_init(&job->base, entity, 1, NULL, file- 
 >client_id);
+        if (ret)
+            goto err_unlock;
+
+        ret = rocket_acquire_object_fences(job->in_bos, job- 
 >in_bo_count,
+                           &job->base, false);
+        if (!ret)
+            ret = rocket_acquire_object_fences(job->out_bos, job- 
 >out_bo_count,
+                               &job->base, true);
+        if (ret) {
+            drm_sched_job_cleanup(&job->base);
+            goto err_unlock;
+        }
+
           drm_sched_job_arm(&job->base);

          job->inference_done_fence = dma_fence_get(&job-

Quoted 1 line

base.s_fence->finished);

+        core = container_of(job->base.sched, struct rocket_core, sched); kref_get(&job->refcount); /* put by scheduler job completion */ drm_sched_entity_push_job(&job->base);
+
+        /* Record the job as the last one of its core, for rocket_pick_core(). */
+        dma_fence_put(core->last_fence);
+        core->last_fence = dma_fence_get(job->inference_done_fence); +        core->last_queued = ++rdev->queued_jobs;
      }
      rocket_attach_object_fences(job->in_bos, job->in_bo_count,

drivers/accel/rocket/rocket_job.c +26 -16
@@ -530,6 +621,9 @@ void rocket_job_fini(struct rocket_core *core)
   {
       drm_sched_fini(&core->sched);
+    dma_fence_put(core->last_fence);
+    core->last_fence = NULL;
+
       cancel_work_sync(&core->reset.work);
       destroy_workqueue(core->reset.wq);
   }
@@ -537,35 +631,42 @@ void rocket_job_fini(struct rocket_core *core)
   int rocket_job_open(struct rocket_file_priv *rocket_priv)
   {
       struct rocket_device *rdev = rocket_priv->rdev;
-    struct drm_gpu_scheduler **scheds = kmalloc_objs(*scheds,
-                             rdev->num_cores);
       unsigned int core;
       int ret;
-    if (!scheds)
+    rocket_priv->sched_entities = kzalloc_objs(*rocket_priv- 
 >sched_entities,
+                           rdev->num_cores);
+    if (!rocket_priv->sched_entities)
           return -ENOMEM;
-    for (core = 0; core < rdev->num_cores; core++)
-        scheds[core] = &rdev->cores[core].sched;
+    for (core = 0; core < rdev->num_cores; core++) {
+        struct drm_gpu_scheduler *sched = &rdev->cores[core].sched;
-    ret = drm_sched_entity_init(&rocket_priv->sched_entity,
-                    DRM_SCHED_PRIORITY_NORMAL,
-                    scheds,
-                    rdev->num_cores, NULL);
-    if (ret)
-        kfree(scheds);
-    else
-        rocket_priv->scheds = scheds;
+        ret = drm_sched_entity_init(&rocket_priv->sched_entities[core],
+                        DRM_SCHED_PRIORITY_NORMAL,
+                        &sched, 1, NULL);
+        if (ret)
+            goto err_destroy_entities;
+    }
+
+    rocket_priv->num_sched_entities = rdev->num_cores;
+
+    return 0;
+err_destroy_entities:
+    while (core--)
+        drm_sched_entity_destroy(&rocket_priv->sched_entities[core]);
+    kfree(rocket_priv->sched_entities);
       return ret;
   }
   void rocket_job_close(struct rocket_file_priv *rocket_priv)
   {
-    struct drm_sched_entity *entity = &rocket_priv->sched_entity;
+    unsigned int core;
-    drm_sched_entity_destroy(entity);
-    kfree(rocket_priv->scheds);
+    for (core = 0; core < rocket_priv->num_sched_entities; core++)
+        drm_sched_entity_destroy(&rocket_priv->sched_entities[core]);
+    kfree(rocket_priv->sched_entities);
   }
   int rocket_job_is_idle(struct rocket_core *core)
@@ -602,37 +703,26 @@ static int rocket_ioctl_submit_job(struct 

drm_device *dev, struct drm_file *file
      rjob->rdev = rdev;
      rjob->domain = rocket_iommu_domain_get(file_priv); -    ret = drm_sched_job_init(&rjob->base,
-                 &file_priv->sched_entity,
-                 1, NULL, file->client_id);
-    if (ret)
-        goto out_put_job;
-
      ret = rocket_copy_tasks(dev, file, job, rjob); if (ret)
-        goto out_cleanup_job;
+        goto out_put_job;
      ret = drm_gem_objects_lookup(file, u64_to_user_ptr(job-

Quoted 1 line

in_bo_handles),

                       job->in_bo_handle_count, &rjob->in_bos); if (ret)
-        goto out_cleanup_job;
+        goto out_put_job;
      rjob->in_bo_count = job->in_bo_handle_count; ret = drm_gem_objects_lookup(file, u64_to_user_ptr(job-

Quoted 1 line

out_bo_handles),

                       job->out_bo_handle_count, &rjob->out_bos); if (ret)
-        goto out_cleanup_job;
+        goto out_put_job;
      rjob->out_bo_count = job->out_bo_handle_count;
-    ret = rocket_job_push(rjob);
-    if (ret)
-        goto out_cleanup_job;
+    ret = rocket_job_push(rjob, file); -out_cleanup_job:
-    if (ret)
-        drm_sched_job_cleanup(&rjob->base); out_put_job:
      rocket_job_put(rjob);

include/uapi/drm/rocket_accel.h +1 -0
index d0685e372b792..6c852e3d00aa3 100644
@@ -161,6 +161,9 @@ struct drm_rocket_job {
    * struct drm_rocket_submit - ioctl argument for submitting commands to the NPU.
    *
    * The kernel will schedule the execution of these jobs in dependency order.
+ * Dependencies, including those on jobs of earlier submissions, are 

only
+ * given by the BOs listed in each struct drm_rocket_job. Jobs that don't
+ * depend on each other may run concurrently on different cores. */
  struct drm_rocket_submit {
      /**

base-commit: 28f286b0dfa9610d4ee2447415e964e8bc603056
prerequisite-patch-id: 5f2509e3f6505d397b609881d54814a3d7a47bd8