Linux Media Controller development
 help / color / mirror / Atom feed
From: Arun Iyengar via B4 Relay <devnull+aiyengar1990.gmail.com@kernel.org>
To: Tomeu Vizoso <tomeu@tomeuvizoso.net>, Oded Gabbay <ogabbay@kernel.org>
Cc: "Heiko Stuebner" <heiko@sntech.de>,
	"Jeff Hugo" <jeff.hugo@oss.qualcomm.com>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Philipp Stanner" <phasta@kernel.org>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Igor Paunovic" <royalnet026@gmail.com>,
	"Midgy BALON" <midgy971@gmail.com>,
	"Jiaxing Hu" <gahing@gahingwoo.com>,
	dri-devel@lists.freedesktop.org,
	linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org,
	stable@vger.kernel.org, "Arun Iyengar" <aiyengar1990@gmail.com>
Subject: [PATCH v3 7/7] accel/rocket: Move open files off a core at unbind
Date: Sat, 05 Dec 2026 06:51:11 +0000	[thread overview]
Message-ID: <20261205-rocket-fixes-v3-7-e5032fa408a3@gmail.com> (raw)
In-Reply-To: <20261205-rocket-fixes-v3-0-e5032fa408a3@gmail.com>

From: Arun Iyengar <aiyengar1990@gmail.com>

A file's scheduler entity can stay on the run queue of a core that is
unbound. drm_sched_fini() frees that run queue, and the next submit or
the file's close then uses it. Jobs queued on that core never run.

At unbind and bind, destroy each open file's entity, which waits until
its queued jobs have started (those for the unbound core fail with
-ENODEV), and build it again on the cores that are up. Wait for the
file's last job before the rebuild: close waits only for that one, and
the new entity could run another job next to it.
Jobs submitted on a file left with no core are dropped (-ENODEV inside;
rocket_ioctl_submit() returns it only with commit 3185975d75f8
("accel/rocket: Propagate job submission errors")).

Keep sched_lock and the core array until the DRM device is released,
as files closed after the last core is unbound still use them.

Fixes: 0810d5ad88a1 ("accel/rocket: Add job submission IOCTL")
Cc: stable@vger.kernel.org
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
 drivers/accel/rocket/rocket_core.c   |   2 +
 drivers/accel/rocket/rocket_device.c |   7 ++-
 drivers/accel/rocket/rocket_device.h |   2 +
 drivers/accel/rocket/rocket_drv.h    |   6 ++
 drivers/accel/rocket/rocket_job.c    | 106 +++++++++++++++++++++++++++++------
 drivers/accel/rocket/rocket_job.h    |   1 +
 6 files changed, 105 insertions(+), 19 deletions(-)

diff --git a/drivers/accel/rocket/rocket_core.c b/drivers/accel/rocket/rocket_core.c
index e51b64845a8b..f49efc18a66e 100644
--- a/drivers/accel/rocket/rocket_core.c
+++ b/drivers/accel/rocket/rocket_core.c
@@ -95,6 +95,8 @@ int rocket_core_init(struct rocket_core *core)
 
 	dev_info(dev, "Rockchip NPU core %d version: %d\n", core->index, version);
 
+	rocket_job_reinit_entities(core->rdev);
+
 	return 0;
 }
 
diff --git a/drivers/accel/rocket/rocket_device.c b/drivers/accel/rocket/rocket_device.c
index efd004194c1a..26a65e1b8a24 100644
--- a/drivers/accel/rocket/rocket_device.c
+++ b/drivers/accel/rocket/rocket_device.c
@@ -2,6 +2,7 @@
 /* Copyright 2024-2025 Tomeu Vizoso <tomeu@tomeuvizoso.net> */
 
 #include <drm/drm_drv.h>
+#include <drm/drm_managed.h>
 #include <linux/array_size.h>
 #include <linux/clk.h>
 #include <linux/dma-mapping.h>
@@ -33,7 +34,7 @@ struct rocket_device *rocket_device_init(struct platform_device *pdev,
 
 	rdev->max_cores = num_cores;
 
-	rdev->cores = devm_kcalloc(dev, num_cores, sizeof(*rdev->cores), GFP_KERNEL);
+	rdev->cores = drmm_kcalloc(ddev, num_cores, sizeof(*rdev->cores), GFP_KERNEL);
 	if (!rdev->cores)
 		return ERR_PTR(-ENOMEM);
 
@@ -43,10 +44,12 @@ struct rocket_device *rocket_device_init(struct platform_device *pdev,
 	if (err)
 		return ERR_PTR(err);
 
-	err = devm_mutex_init(dev, &rdev->sched_lock);
+	err = drmm_mutex_init(ddev, &rdev->sched_lock);
 	if (err)
 		return ERR_PTR(-ENOMEM);
 
+	INIT_LIST_HEAD(&rdev->files);
+
 	err = drm_dev_register(ddev, 0);
 	if (err)
 		return ERR_PTR(err);
diff --git a/drivers/accel/rocket/rocket_device.h b/drivers/accel/rocket/rocket_device.h
index c62d56701069..4ee8935def26 100644
--- a/drivers/accel/rocket/rocket_device.h
+++ b/drivers/accel/rocket/rocket_device.h
@@ -16,6 +16,8 @@ struct rocket_device {
 	struct drm_device ddev;
 
 	struct mutex sched_lock;
+	/* Open files, under @sched_lock */
+	struct list_head files;
 
 	struct rocket_core *cores;
 	unsigned int num_cores;
diff --git a/drivers/accel/rocket/rocket_drv.h b/drivers/accel/rocket/rocket_drv.h
index f7a77ea98467..cfd76b2428e8 100644
--- a/drivers/accel/rocket/rocket_drv.h
+++ b/drivers/accel/rocket/rocket_drv.h
@@ -24,8 +24,14 @@ struct rocket_file_priv {
 
 	struct rocket_vm *vm;
 
+	struct list_head node;
+
 	struct drm_gpu_scheduler **scheds;
+	unsigned int num_scheds;
 	struct drm_sched_entity sched_entity;
+	/* Serialises @sched_entity changes at close, core unbind and bind */
+	struct mutex entity_lock;
+	bool closed;
 
 	struct dma_fence *last_job_fence;
 };
diff --git a/drivers/accel/rocket/rocket_job.c b/drivers/accel/rocket/rocket_job.c
index 53f0751d7a1e..7ad593984d3a 100644
--- a/drivers/accel/rocket/rocket_job.c
+++ b/drivers/accel/rocket/rocket_job.c
@@ -232,6 +232,11 @@ static int rocket_job_push(struct rocket_job *job)
 		goto err;
 
 	scoped_guard(mutex, &rdev->sched_lock) {
+		if (!job->file_priv->num_scheds) {
+			ret = -ENODEV;
+			goto err_unlock;
+		}
+
 		drm_sched_job_arm(&job->base);
 
 		job->inference_done_fence = dma_fence_get(&job->base.s_fence->finished);
@@ -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;
+	}
+
+	if (!n)
+		return -ENODEV;
+
+	ret = drm_sched_entity_init(&rocket_priv->sched_entity,
+				    DRM_SCHED_PRIORITY_NORMAL,
+				    rocket_priv->scheds, n, NULL);
+	if (!ret)
+		rocket_priv->num_scheds = n;
+
+	return ret;
+}
+
+/*
+ * Rebuild every open file's entity on the cores that are up. The flush in
+ * drm_sched_entity_destroy() runs the queued jobs; those for a removing
+ * core fail.
+ */
+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);
+		}
+
+		rocket_job_entity_init(rocket_priv);
+	}
+}
+
 int rocket_job_init(struct rocket_core *core)
 {
 	struct drm_sched_init_args args = {
@@ -581,6 +642,9 @@ void rocket_job_fini(struct rocket_core *core)
 	queue_work(core->reset.wq, &core->reset.work);
 	flush_work(&core->reset.work);
 
+	/* drm_sched_fini() frees the run queues the entities are on */
+	rocket_job_reinit_entities(core->rdev);
+
 	drm_sched_fini(&core->sched);
 
 	cancel_work_sync(&core->reset.work);
@@ -590,40 +654,48 @@ 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->scheds = kmalloc_objs(*rocket_priv->scheds, rdev->max_cores);
+	if (!rocket_priv->scheds)
 		return -ENOMEM;
 
-	for (core = 0; core < rdev->num_cores; core++)
-		scheds[core] = &rdev->cores[core].sched;
+	mutex_init(&rocket_priv->entity_lock);
 
-	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;
+	guard(mutex)(&rdev->sched_lock);
 
-	return ret;
+	ret = rocket_job_entity_init(rocket_priv);
+	if (ret) {
+		mutex_destroy(&rocket_priv->entity_lock);
+		kfree(rocket_priv->scheds);
+		return ret;
+	}
+
+	list_add(&rocket_priv->node, &rdev->files);
+
+	return 0;
 }
 
 void rocket_job_close(struct rocket_file_priv *rocket_priv)
 {
-	struct drm_sched_entity *entity = &rocket_priv->sched_entity;
+	struct rocket_device *rdev = rocket_priv->rdev;
+
+	scoped_guard(mutex, &rocket_priv->entity_lock) {
+		if (rocket_priv->num_scheds)
+			drm_sched_entity_destroy(&rocket_priv->sched_entity);
+		rocket_priv->num_scheds = 0;
+		rocket_priv->closed = true;
+	}
 
-	drm_sched_entity_destroy(entity);
+	scoped_guard(mutex, &rdev->sched_lock)
+		list_del(&rocket_priv->node);
 
 	if (rocket_priv->last_job_fence) {
 		dma_fence_wait(rocket_priv->last_job_fence, false);
 		dma_fence_put(rocket_priv->last_job_fence);
 	}
 
+	mutex_destroy(&rocket_priv->entity_lock);
 	kfree(rocket_priv->scheds);
 }
 
diff --git a/drivers/accel/rocket/rocket_job.h b/drivers/accel/rocket/rocket_job.h
index 27db6f861250..ff9756cf09cb 100644
--- a/drivers/accel/rocket/rocket_job.h
+++ b/drivers/accel/rocket/rocket_job.h
@@ -48,6 +48,7 @@ int rocket_ioctl_submit(struct drm_device *dev, void *data, struct drm_file *fil
 
 int rocket_job_init(struct rocket_core *core);
 void rocket_job_fini(struct rocket_core *core);
+void rocket_job_reinit_entities(struct rocket_device *rdev);
 int rocket_job_open(struct rocket_file_priv *rocket_priv);
 void rocket_job_close(struct rocket_file_priv *rocket_priv);
 int rocket_job_is_idle(struct rocket_core *core);

-- 
2.55.0



      parent reply	other threads:[~2026-12-05  6:51 UTC|newest]

Thread overview: 8+ 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 ` [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 ` [PATCH v3 2/7] accel/rocket: Reset the core before detaching its IOMMU Arun Iyengar via B4 Relay
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 ` [PATCH v3 4/7] accel/rocket: Serialise core probe and removal Arun Iyengar via B4 Relay
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 ` [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 [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=20261205-rocket-fixes-v3-7-e5032fa408a3@gmail.com \
    --to=devnull+aiyengar1990.gmail.com@kernel.org \
    --cc=aiyengar1990@gmail.com \
    --cc=christian.koenig@amd.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gahing@gahingwoo.com \
    --cc=heiko@sntech.de \
    --cc=jeff.hugo@oss.qualcomm.com \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=matthew.brost@intel.com \
    --cc=midgy971@gmail.com \
    --cc=ogabbay@kernel.org \
    --cc=phasta@kernel.org \
    --cc=royalnet026@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=tomeu@tomeuvizoso.net \
    /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