mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tvrtko Ursulin <tursulin@ursulin.net>
To: Gyeyoung Baek <gye976@gmail.com>,
	Tomeu Vizoso <tomeu@tomeuvizoso.net>,
	Oded Gabbay <ogabbay@kernel.org>
Cc: Heiko Stuebner <heiko@sntech.de>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	dri-devel@lists.freedesktop.org,
	linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org,
	Philipp Stanner <phasta@kernel.org>,
	Danilo Krummrich <dakr@kernel.org>
Subject: Re: [PATCH] accel/rocket: Parallelize independent jobs across cores
Date: Wed, 2 Dec 2026 14:53:31 +0000	[thread overview]
Message-ID: <29badb49-7a4c-44af-be4d-f464e949452b@ursulin.net> (raw)
In-Reply-To: <20261202100555.3122236-1-gye976@gmail.com>


On 02/12/2026 10:05, Gyeyoung Baek wrote:
> 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.

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.

> - 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%
> 
> Signed-off-by: Gyeyoung Baek <gye976@gmail.com>
> ---
> 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(-)
> 
> diff --git a/drivers/accel/rocket/rocket_core.h b/drivers/accel/rocket/rocket_core.h
> index f6d7382854ca9..aaaa844539054 100644
> --- a/drivers/accel/rocket/rocket_core.h
> +++ b/drivers/accel/rocket/rocket_core.h
> @@ -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);
> diff --git a/drivers/accel/rocket/rocket_device.h b/drivers/accel/rocket/rocket_device.h
> index c62d567010696..319ce62f601b1 100644
> --- a/drivers/accel/rocket/rocket_device.h
> +++ b/drivers/accel/rocket/rocket_device.h
> @@ -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;
> diff --git a/drivers/accel/rocket/rocket_drv.h b/drivers/accel/rocket/rocket_drv.h
> index 9421e48ec5d84..3bd14542353ad 100644
> --- a/drivers/accel/rocket/rocket_drv.h
> +++ b/drivers/accel/rocket/rocket_drv.h
> @@ -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);
> diff --git a/drivers/accel/rocket/rocket_job.c b/drivers/accel/rocket/rocket_job.c
> index d28167208dc4a..7542c148ac2d0 100644
> --- a/drivers/accel/rocket/rocket_job.c
> +++ b/drivers/accel/rocket/rocket_job.c
> @@ -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);
>   
> diff --git a/include/uapi/drm/rocket_accel.h b/include/uapi/drm/rocket_accel.h
> index d0685e372b792..6c852e3d00aa3 100644
> --- a/include/uapi/drm/rocket_accel.h
> +++ b/include/uapi/drm/rocket_accel.h
> @@ -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


      reply	other threads:[~2026-12-02 14:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 10:05 Gyeyoung Baek
2026-12-02 14:53 ` Tvrtko Ursulin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=29badb49-7a4c-44af-be4d-f464e949452b@ursulin.net \
    --to=tursulin@ursulin.net \
    --cc=airlied@gmail.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gye976@gmail.com \
    --cc=heiko@sntech.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=ogabbay@kernel.org \
    --cc=phasta@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tomeu@tomeuvizoso.net \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®