All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arun Iyengar" <aiyengar1990@gmail.com>
Cc: dri-devel@lists.freedesktop.org, media-ci@linuxtv.org
Subject: Re: [PATCH v3 7/7] accel/rocket: Move open files off a core at unbind
Date: Sat, 05 Dec 2026 07:31:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-251222@kernel.org> (raw)
In-Reply-To: <20261205-rocket-fixes-v3-7-e5032fa408a3@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] accel/rocket: race in rocket_job_entity_init() exposes unready
  probing cores
- [High] accel/rocket: UAF in drm_sched_entity_kill_jobs_work() on
  killed entities
- [Medium] accel/rocket: rocket_job_reinit_entities() stalls all files
  under sched_lock
--

commit 37e3f5c25d668bb6f35862e014c225d955407863
Author: Arun Iyengar <aiyengar1990@gmail.com>

accel/rocket: Move open files off a core at unbind

This commit tracks open files on rdev->files and rebuilds their DRM scheduler
entities on core bind and unbind so entities do not stay on a freed run queue.
It also switches rdev->cores and rdev->sched_lock to drmm_* allocations so
files closed after the last core unbinds can still access them.

> diff --git a/drivers/accel/rocket/rocket_job.c b/drivers/accel/rocket/rocket_job.c
> index 53f0751d7a1ec..7ad593984d3aa 100644
> --- a/drivers/accel/rocket/rocket_job.c
> +++ b/drivers/accel/rocket/rocket_job.c

[ ... ]

> @@ -520,6 +525,62 @@ static irqreturn_t rocket_job_irq_handler(int irq, void *data)
>  	return IRQ_WAKE_THREAD;
>  }
>  
> +/* Build the entity on the cores that are up. Called with sched_lock held. */
> +static int rocket_job_entity_init(struct rocket_file_priv *rocket_priv)
> +{
> +	struct rocket_device *rdev = rocket_priv->rdev;
> +	unsigned int core, n = 0;
> +	int ret;
> +
> +	for (core = 0; core < rdev->max_cores; core++) {
> +		struct rocket_core *c = &rdev->cores[core];
> +
> +		if (!READ_ONCE(c->removing) && drm_sched_wqueue_ready(&c->sched))
> +			rocket_priv->scheds[n++] = &c->sched;
> +	}

[Severity: High]
Can rocket_job_entity_init() expose a probing core before rocket_core_init()
has finished setting up runtime PM and hardware state?

rocket_open() drops rocket_cores_lock before calling rocket_job_open(), while
rocket_probe() runs rocket_core_init() without holding rdev->sched_lock.
Because rocket_job_init() calls drm_sched_init() before pm_runtime_enable(),
pm_runtime_resume_and_get(), and the VERSION register reads, a concurrently
opened file can pick up the half-initialized core:

CPU0 (rocket_probe)             CPU1 (rocket_open)
rocket_core_init()
  rocket_job_init()
    drm_sched_init()
      sched->ready = true
                                rocket_job_open()
                                  rocket_job_entity_init()
                                    adds probing core to scheds
                                rocket_job_run()
                                  pm_runtime_resume_and_get() <-- -EACCES
  pm_runtime_enable()

In addition, core->removing and sched->ready are written with plain stores in
rocket_job_init() and drm_sched_init() and read via drm_sched_wqueue_ready()
with no shared lock or release/acquire ordering.  On weakly ordered ARM64
(RK3588), drm_sched_entity_init() can observe sched->ready before
sched->sched_rq is visible, leaving entity->rq NULL or uninitialized and
risking a NULL pointer dereference in drm_sched_job_arm().

Marking a core schedulable under rdev->sched_lock only after
rocket_core_init() finishes runtime PM and hardware initialization would
close both races.

[ ... ]

> +void rocket_job_reinit_entities(struct rocket_device *rdev)
> +{
> +	struct rocket_file_priv *rocket_priv;
> +
> +	guard(mutex)(&rdev->sched_lock);
> +
> +	list_for_each_entry(rocket_priv, &rdev->files, node) {
> +		guard(mutex)(&rocket_priv->entity_lock);
> +
> +		if (rocket_priv->closed)
> +			continue;
> +
> +		if (rocket_priv->num_scheds) {
> +			drm_sched_entity_destroy(&rocket_priv->sched_entity);
> +			rocket_priv->num_scheds = 0;
> +
> +			/* Close waits only for last_job_fence: no second job on the NPU */
> +			if (rocket_priv->last_job_fence)
> +				dma_fence_wait(rocket_priv->last_job_fence, false);
> +		}

[Severity: High]
Can asynchronous killed-job cleanup in drm_sched_entity_kill_jobs_work()
use-after-free rdev->cores and rdev after the last core unbinds and the file
closes?

When drm_sched_entity_flush() returns early with jobs still queued (for
example on SIGKILL in rocket_job_reinit_entities(), or after its 1-second
timeout in rocket_job_close()), drm_sched_entity_fini() calls
drm_sched_entity_kill(), which schedules drm_sched_entity_kill_jobs_work() on
system_wq.  Neither rocket_job_reinit_entities(), rocket_job_close(), nor
drm_sched_fini() waits for that work because last_job_fence is only updated
in rocket_job_run() and never covers killed queued jobs, and queued jobs do
not hold a drm_dev_get() or module reference:

CPU0 (unbind / close)             kworker (system_wq)
rocket_job_close()
  drm_sched_entity_destroy()
    drm_sched_entity_flush()
      /* times out or SIGKILL */
    drm_sched_entity_kill()
      schedule_work(&job->work)
  dma_fence_wait(last_job_fence)
drm_release() -> drm_dev_put()
  drm_managed_release()
    kfree(rdev->cores)
    kfree(rdev)
                                  drm_sched_entity_kill_jobs_work()
                                    job->sched->ops->free_job(job)
                                      <-- use-after-free

Separately, if a core bind or partial unbind task receives SIGKILL while
rocket_job_reinit_entities() is in wait_event_killable() inside
drm_sched_entity_flush(), drm_sched_entity_fini() kills queued jobs belonging
to unrelated healthy clients with -ESRCH.

> +
> +		rocket_job_entity_init(rocket_priv);
> +	}
> +}

[Severity: Medium]
Does holding rdev->sched_lock across drm_sched_entity_destroy() and
uninterruptible dma_fence_wait() for every open file cause unnecessary global
stalls on core bind and partial unbind?

rocket_job_reinit_entities() drains and rebuilds every file's entity
sequentially under rdev->sched_lock (and rocket_cores_lock) even on core
bind, where no run queue is being freed, and for entities running on healthy
cores during a single-core unbind.  While rdev->sched_lock is held,
concurrent rocket_job_push() callers block on it while already holding their
BO dma_resv locks, starving healthy NPU cores and stalling other dma_resv
lockers as well as rocket_open() and rocket_job_close().

Calling drm_sched_entity_modify_sched() under rdev->sched_lock would update
entity->sched_list in place on bind and for entities on healthy cores during
partial unbind, reserving drm_sched_entity_destroy() only for entities
assigned to the removing core (or when no cores remain).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-rocket-fixes-v3-0-e5032fa408a3@gmail.com?part=7

      reply	other threads:[~2026-12-05  7:31 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  6:51 [PATCH v3 0/7] accel/rocket: Fix teardown with a job on the NPU Arun Iyengar via B4 Relay
2026-12-05  6:51 ` Arun Iyengar via B4 Relay
2026-12-05  6:51 ` Arun Iyengar
2026-12-05  6:51 ` [PATCH v3 1/7] accel/rocket: Wait for in-flight jobs before releasing the file's BOs Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar
2026-12-05  6:51 ` [PATCH v3 2/7] accel/rocket: Reset the core before detaching its IOMMU Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar
2026-12-05  6:51 ` [PATCH v3 3/7] accel/rocket: Stop the scheduler before releasing the IOMMU group Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar
2026-12-05  6:51 ` [PATCH v3 4/7] accel/rocket: Serialise core probe and removal Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar
2026-12-05  7:16   ` sashiko-bot
2026-12-05  6:51 ` [PATCH v3 5/7] accel/rocket: Reset the core at unbind Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar
2026-12-05  6:51 ` [PATCH v3 6/7] accel/rocket: Free the jobs left at scheduler teardown Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar
2026-12-05  6:51 ` [PATCH v3 7/7] accel/rocket: Move open files off a core at unbind Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar via B4 Relay
2026-12-05  6:51   ` Arun Iyengar
2026-12-05  7:31   ` sashiko-bot [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=sashiko-outbox-251222@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aiyengar1990@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=media-ci@linuxtv.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.