From: Jiwoo Kim <arpe1618@gmail.com>
To: "Boris Brezillon" <boris.brezillon@collabora.com>,
"Rob Herring" <robh@kernel.org>,
"Steven Price" <steven.price@arm.com>,
"Adrián Larumbe" <adrian.larumbe@collabora.com>
Cc: Danilo Krummrich <dakr@kernel.org>,
Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
Thomas Zimmermann <tzimmermann@suse.de>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: [PATCH v2 3/4] drm/panfrost: Stop a job queued after its JM context was destroyed
Date: Sat, 14 Nov 2026 17:20:15 +0900 [thread overview]
Message-ID: <20261114082016.2454-4-arpe1618@gmail.com> (raw)
In-Reply-To: <20261114082016.2454-1-arpe1618@gmail.com>
panfrost_jm_ctx_destroy(), which runs on JM_CTX_DESTROY and when the
file is closed, sets jm_ctx->destroyed and then stops the jobs of the
context that it finds in pfdev->jobs[]. panfrost_job_run() checks the
flag once, without any lock, and panfrost_job_hw_submit() can then
sleep in pm_runtime_get_sync() before it queues the job in
pfdev->jobs[] under job_lock. A job that is between the check and the
queueing when its context is destroyed is therefore neither cancelled
nor stopped, and can then be started after its context, and possibly
its file, are gone. With profiling enabled, its engine_usage also still
points into the panfrost_file_priv that panfrost_postclose() frees, and
panfrost_dequeue_job() writes to it when the job completes.
Before commit 6aa8bc58ac88 ("drm/panfrost: Introduce JM contexts for
manging job resources"), the close path called
drm_sched_entity_destroy(), which waits for a run_job() call in
progress on the entity, before it looked for the jobs to stop. The
entities are now only destroyed when the last reference to the context
is dropped.
Move the per-job part of panfrost_jm_ctx_destroy() into a helper, and
call it from panfrost_job_hw_submit() on the job it has just queued if
the context has been destroyed by then. The check in
panfrost_job_hw_submit() and the loop in panfrost_jm_ctx_destroy() both
run under job_lock, and panfrost_jm_ctx_destroy() sets the flag before
it takes the lock, so with the previous patch, such a job is either
found by panfrost_jm_ctx_destroy() or sees the flag, and it then goes
through the same stop and completion path in both cases. Cancelling the
job in panfrost_job_hw_submit() instead would leave a gap in the fence
seqnos of the slot, which the job chain flags rely on. Use READ_ONCE()
and WRITE_ONCE() for the flag, as it is written outside job_lock and
read without it in panfrost_job_run().
This was found with an AI coding assistant, which also drafted this
patch and its changelog. It has not been reproduced, and the change
has only been compile-tested.
Fixes: 6aa8bc58ac88 ("drm/panfrost: Introduce JM contexts for manging job resources")
Cc: stable@vger.kernel.org
Signed-off-by: Jiwoo Kim <arpe1618@gmail.com>
---
panfrost_stop_job() is the loop body of panfrost_jm_ctx_destroy() moved
into a helper unchanged, apart from the lockdep assertion.
When the job is in subslot 0, the hard stop is written right after
JS_COMMAND_NEXT=START, as panfrost_jm_ctx_destroy() can already do for
a job it finds just after it was started. Whether the job manager can
miss a stop that early could not be checked on hardware.
Arun Iyengar's "Resume long jobs instead of resetting" series also
changes panfrost_job_hw_submit(); this patch does not depend on it:
https://lore.kernel.org/r/20261113-panfrost-long-jobs-v3-0-80ae78d38f6e@gmail.com
drivers/gpu/drm/panfrost/panfrost_job.c | 68 ++++++++++++++++---------
1 file changed, 44 insertions(+), 24 deletions(-)
diff --git a/drivers/gpu/drm/panfrost/panfrost_job.c b/drivers/gpu/drm/panfrost/panfrost_job.c
index 7a3137dacb9d..0dbed7a50910 100644
--- a/drivers/gpu/drm/panfrost/panfrost_job.c
+++ b/drivers/gpu/drm/panfrost/panfrost_job.c
@@ -202,6 +202,37 @@ panfrost_enqueue_job(struct panfrost_device *pfdev, int slot,
return 1;
}
+/* Stop a job of a destroyed JM context, found in pfdev->jobs[slot][subslot]. */
+static void panfrost_stop_job(struct panfrost_device *pfdev, int slot,
+ unsigned int subslot, struct panfrost_job *job)
+{
+ u32 cmd;
+
+ lockdep_assert_held(&pfdev->js->job_lock);
+
+ if (subslot == 1) {
+ /* Try to cancel the job before it starts */
+ job_write(pfdev, JS_COMMAND_NEXT(slot), JS_COMMAND_NOP);
+ /* Reset the job head so it doesn't get restarted if
+ * the job in the first slot failed.
+ */
+ job->jc = 0;
+ }
+
+ if (panfrost_has_hw_feature(pfdev, HW_FEATURE_JOBCHAIN_DISAMBIGUATION)) {
+ cmd = panfrost_get_job_chain_flag(job) ?
+ JS_COMMAND_HARD_STOP_1 :
+ JS_COMMAND_HARD_STOP_0;
+ } else {
+ cmd = JS_COMMAND_HARD_STOP;
+ }
+
+ job_write(pfdev, JS_COMMAND(slot), cmd);
+
+ /* Jobs can outlive their file context */
+ job->engine_usage = NULL;
+}
+
static int panfrost_job_hw_submit(struct panfrost_job *job, int js)
{
struct panfrost_device *pfdev = job->pfdev;
@@ -272,6 +303,16 @@ static int panfrost_job_hw_submit(struct panfrost_job *job, int js)
"JS: Submitting atom %p to js[%d][%d] with head=0x%llx AS %d",
job, js, subslot, jc_head, cfg & 0xf);
}
+
+ /*
+ * panfrost_jm_ctx_destroy() only stops the jobs it finds in
+ * pfdev->jobs[], so stop this one here if its context was destroyed
+ * while we were getting here, e.g. while waiting for the GPU to
+ * resume. job_lock orders this against panfrost_jm_ctx_destroy().
+ */
+ if (READ_ONCE(job->ctx->destroyed))
+ panfrost_stop_job(pfdev, js, subslot, job);
+
spin_unlock(&pfdev->js->job_lock);
return 0;
@@ -404,7 +445,7 @@ static struct dma_fence *panfrost_job_run(struct drm_sched_job *sched_job)
struct dma_fence *fence = NULL;
int ret;
- if (job->ctx->destroyed)
+ if (READ_ONCE(job->ctx->destroyed))
return ERR_PTR(-ECANCELED);
if (unlikely(job->base.s_fence->finished.error))
@@ -1100,14 +1141,13 @@ int panfrost_jm_ctx_destroy(struct drm_file *file, u32 handle)
if (!jm_ctx)
return -EINVAL;
- jm_ctx->destroyed = true;
+ WRITE_ONCE(jm_ctx->destroyed, true);
/* Kill in-flight jobs */
spin_lock(&pfdev->js->job_lock);
for (u32 i = 0; i < ARRAY_SIZE(jm_ctx->slot_entity); i++) {
for (int j = ARRAY_SIZE(pfdev->jobs[0]) - 1; j >= 0; j--) {
struct panfrost_job *job = pfdev->jobs[i][j];
- u32 cmd;
/*
* The scheduler clears job->base.entity when it pops the
@@ -1116,27 +1156,7 @@ int panfrost_jm_ctx_destroy(struct drm_file *file, u32 handle)
if (!job || job->ctx != jm_ctx)
continue;
- if (j == 1) {
- /* Try to cancel the job before it starts */
- job_write(pfdev, JS_COMMAND_NEXT(i), JS_COMMAND_NOP);
- /* Reset the job head so it doesn't get restarted if
- * the job in the first slot failed.
- */
- job->jc = 0;
- }
-
- if (panfrost_has_hw_feature(pfdev, HW_FEATURE_JOBCHAIN_DISAMBIGUATION)) {
- cmd = panfrost_get_job_chain_flag(job) ?
- JS_COMMAND_HARD_STOP_1 :
- JS_COMMAND_HARD_STOP_0;
- } else {
- cmd = JS_COMMAND_HARD_STOP;
- }
-
- job_write(pfdev, JS_COMMAND(i), cmd);
-
- /* Jobs can outlive their file context */
- job->engine_usage = NULL;
+ panfrost_stop_job(pfdev, i, j, job);
}
}
spin_unlock(&pfdev->js->job_lock);
--
2.43.0
next prev parent reply other threads:[~2026-11-14 8:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-14 8:20 [PATCH v2 0/4] drm/panfrost: Fix jobs that outlive their JM context Jiwoo Kim
2026-11-14 8:20 ` [PATCH v2 1/4] drm/panfrost: Hold a reference to the MMU context of a job Jiwoo Kim
2026-11-14 8:20 ` [PATCH v2 2/4] drm/panfrost: Find the jobs of a destroyed JM context by their context Jiwoo Kim
2026-11-14 8:20 ` Jiwoo Kim [this message]
2026-11-14 8:20 ` [PATCH v2 4/4] drm/panfrost: Don't destroy uninitialized entities of a new JM context Jiwoo Kim
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=20261114082016.2454-4-arpe1618@gmail.com \
--to=arpe1618@gmail.com \
--cc=adrian.larumbe@collabora.com \
--cc=airlied@gmail.com \
--cc=boris.brezillon@collabora.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=robh@kernel.org \
--cc=simona@ffwll.ch \
--cc=stable@vger.kernel.org \
--cc=steven.price@arm.com \
--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