From: Anna Maniscalco <anna.maniscalco2000@gmail.com>
To: Rob Clark <robin.clark@oss.qualcomm.com>,
Sean Paul <sean@poorly.run>,
Konrad Dybcio <konradybcio@kernel.org>,
Dmitry Baryshkov <lumag@kernel.org>,
Abhinav Kumar <abhinav.kumar@linux.dev>,
Marijn Suijten <marijn.suijten@somainline.org>,
David Airlie <airlied@gmail.com>,
Simona Vetter <simona@ffwll.ch>,
Akhil P Oommen <akhilpo@oss.qualcomm.com>,
Jessica Zhang <jesszhan0024@gmail.com>,
Jessica Zhang <jesszhan0024@gmail.com>
Cc: linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org,
freedreno@lists.freedesktop.org, linux-kernel@vger.kernel.org,
Anna Maniscalco <anna.maniscalco2000@gmail.com>
Subject: [PATCH v4 2/2] drm/msm: preemption aware hangcheck
Date: Sat, 05 Dec 2026 17:01:47 +0100 [thread overview]
Message-ID: <20261205-preemption_aware_hangcheck-v4-2-1df0153b07c2@gmail.com> (raw)
In-Reply-To: <20261205-preemption_aware_hangcheck-v4-0-1df0153b07c2@gmail.com>
Ever since we added support for preemption hangcheck has been somewhat
broken as it is not aware of multiple rings.
In some cases it might not recognize that one ring is stuck if the gpu
switches in and out of it.
Track the time a job has spent in a ring by storing timestamps of the
`CP_ALWAYS_ON_CONTEXT` register at the beginning of a job and when
switching rings as well as reading it back if the ring is currently
active.
Signed-off-by: Anna Maniscalco <anna.maniscalco2000@gmail.com>
---
drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 38 ++++++++++++++++++++-
drivers/gpu/drm/msm/adreno/a6xx_gpu.h | 1 +
drivers/gpu/drm/msm/adreno/a6xx_preempt.c | 30 ++++++++++++-----
drivers/gpu/drm/msm/adreno/adreno_gpu.c | 3 +-
drivers/gpu/drm/msm/msm_gpu.c | 56 ++++++++++++++++++++++++-------
drivers/gpu/drm/msm/msm_gpu.h | 6 +++-
drivers/gpu/drm/msm/msm_ringbuffer.h | 10 ++++++
7 files changed, 121 insertions(+), 23 deletions(-)
diff --git a/drivers/gpu/drm/msm/adreno/a6xx_gpu.c b/drivers/gpu/drm/msm/adreno/a6xx_gpu.c
index af4c4d5712d3..502fd61fdcb6 100644
--- a/drivers/gpu/drm/msm/adreno/a6xx_gpu.c
+++ b/drivers/gpu/drm/msm/adreno/a6xx_gpu.c
@@ -519,6 +519,9 @@ static void a7xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
get_stats_counter(ring, rbbm_perfctr_cp0, rbmemptr_stats(ring, index, cpcycles_start));
get_stats_counter(ring, cp_always_on_context, rbmemptr_stats(ring, index, alwayson_start));
+ get_stats_counter(ring, cp_always_on_context,
+ rbmemptr(ring, last_job_start_ctx));
+
OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
OUT_RING(ring, CP_SET_THREAD_BOTH);
@@ -1474,8 +1477,11 @@ static int hw_init(struct msm_gpu *gpu)
/* Always come up on rb 0 */
a6xx_gpu->cur_ring = gpu->rb[0];
- for (i = 0; i < gpu->nr_rings; i++)
+ for (i = 0; i < gpu->nr_rings; i++) {
gpu->rb[i]->cur_ctx_seqno = 0;
+ gpu->rb[i]->memptrs->last_job_start_ctx = 0;
+ gpu->rb[i]->memptrs->last_active_ctxcycles = 0;
+ }
/* Enable the SQE_to start the CP engine */
gpu_write(gpu, REG_A6XX_CP_SQE_CNTL, 1);
@@ -2287,6 +2293,35 @@ static u64 a6xx_get_timestamp(struct msm_gpu *gpu)
return gpu_read64(gpu, REG_A6XX_CP_ALWAYS_ON_COUNTER);
}
+static int a6xx_get_ctx_timestamp(struct msm_ringbuffer *ring, uint64_t *value)
+{
+ struct msm_gpu *gpu = ring->gpu;
+ struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
+ struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
+ int ret = 0;
+
+ /* Inhibit IFPC while accessing registers: */
+ guard(ifpc_disable)(gpu);
+
+ if (gpu->nr_rings > 1) {
+ guard(spinlock_irqsave)(&a6xx_gpu->eval_lock);
+
+ /*
+ * Make sure the GMU keeps the GPU on while we read the register
+ *
+ * This should normally not be waking up the gpu since the
+ * hangcheck timer shouldn't fire when the gpu is idle.
+ */
+ if (a6xx_in_preempt(a6xx_gpu) || ring != a6xx_gpu->cur_ring)
+ return -EBUSY;
+
+ *value = gpu_read64(gpu, REG_A6XX_CP_ALWAYS_ON_CONTEXT);
+ } else
+ *value = gpu_read64(gpu, REG_A6XX_CP_ALWAYS_ON_CONTEXT);
+
+ return ret;
+}
+
static struct msm_ringbuffer *a6xx_active_ring(struct msm_gpu *gpu)
{
struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
@@ -2886,6 +2921,7 @@ const struct adreno_gpu_funcs a7xx_gpu_funcs = {
.progress = a6xx_progress,
.ifpc_update = a6xx_gmu_ifpc_update,
.perfcntr_configure = a6xx_perfcntr_configure,
+ .get_ctx_timestamp = a6xx_get_ctx_timestamp,
},
.init = a6xx_gpu_init,
.get_timestamp = a6xx_gmu_get_timestamp,
diff --git a/drivers/gpu/drm/msm/adreno/a6xx_gpu.h b/drivers/gpu/drm/msm/adreno/a6xx_gpu.h
index c0d7ca0969e1..36cf6d7f3d44 100644
--- a/drivers/gpu/drm/msm/adreno/a6xx_gpu.h
+++ b/drivers/gpu/drm/msm/adreno/a6xx_gpu.h
@@ -105,6 +105,7 @@ struct a6xx_gpu {
struct drm_gem_object *preempt_postamble_bo;
void *preempt_postamble_ptr;
uint64_t preempt_postamble_iova;
+ uint64_t preempt_postamble_cntreset_end;
uint64_t preempt_postamble_len;
bool postamble_enabled;
diff --git a/drivers/gpu/drm/msm/adreno/a6xx_preempt.c b/drivers/gpu/drm/msm/adreno/a6xx_preempt.c
index fb3e77f7bcc6..3e3b579f5146 100644
--- a/drivers/gpu/drm/msm/adreno/a6xx_preempt.c
+++ b/drivers/gpu/drm/msm/adreno/a6xx_preempt.c
@@ -24,11 +24,15 @@ static void a6xx_preempt_timer(struct timer_list *t)
msm_trigger_recovery(gpu);
}
-static void preempt_prepare_postamble(struct a6xx_gpu *a6xx_gpu)
+static void preempt_prepare_postamble(struct a6xx_gpu *a6xx_gpu, struct msm_ringbuffer *ring)
{
u32 *postamble = a6xx_gpu->preempt_postamble_ptr;
+ uint64_t last_active_ctxcycles = 0;
u32 count = 0;
+ if (ring)
+ last_active_ctxcycles = rbmemptr(ring, last_active_ctxcycles);
+
postamble[count++] = PKT7(CP_REG_RMW, 3);
postamble[count++] = REG_A6XX_RBBM_PERFCTR_SRAM_INIT_CMD;
postamble[count++] = 0;
@@ -43,6 +47,17 @@ static void preempt_prepare_postamble(struct a6xx_gpu *a6xx_gpu)
postamble[count++] = CP_WAIT_REG_MEM_4_MASK(0x1);
postamble[count++] = CP_WAIT_REG_MEM_5_DELAY_LOOP_CYCLES(0);
+ a6xx_gpu->preempt_postamble_cntreset_end = count;
+
+ if (last_active_ctxcycles) {
+ postamble[count++] = PKT7(CP_REG_TO_MEM, 3);
+ postamble[count++] = CP_REG_TO_MEM_0_REG(REG_A6XX_CP_ALWAYS_ON_CONTEXT) |
+ CP_REG_TO_MEM_0_CNT(2) |
+ CP_REG_TO_MEM_0_IS_64B;
+ postamble[count++] = lower_32_bits(last_active_ctxcycles);
+ postamble[count++] = upper_32_bits(last_active_ctxcycles);
+ }
+
a6xx_gpu->preempt_postamble_len = count;
a6xx_gpu->postamble_enabled = true;
@@ -54,9 +69,9 @@ static void preempt_disable_postamble(struct a6xx_gpu *a6xx_gpu)
/*
* Disable the postamble by replacing the first packet header with a NOP
- * that covers the whole buffer.
+ * that skips the counters reset part.
*/
- *postamble = PKT7(CP_NOP, (a6xx_gpu->preempt_postamble_len - 1));
+ *postamble = PKT7(CP_NOP, (a6xx_gpu->preempt_postamble_cntreset_end - 1));
a6xx_gpu->postamble_enabled = false;
}
@@ -263,10 +278,9 @@ void a6xx_preempt_trigger(struct msm_gpu *gpu)
/* Enable or disable postamble as needed */
sysprof = msm_gpu_sysprof_no_perfcntr_zap(gpu);
- if (!sysprof && !a6xx_gpu->postamble_enabled)
- preempt_prepare_postamble(a6xx_gpu);
-
- if (sysprof && a6xx_gpu->postamble_enabled)
+ if (!sysprof)
+ preempt_prepare_postamble(a6xx_gpu, ring);
+ else
preempt_disable_postamble(a6xx_gpu);
/* Set the preemption state to triggered */
@@ -382,7 +396,7 @@ void a6xx_preempt_init(struct msm_gpu *gpu)
if (IS_ERR(a6xx_gpu->preempt_postamble_ptr))
goto fail;
- preempt_prepare_postamble(a6xx_gpu);
+ preempt_prepare_postamble(a6xx_gpu, NULL);
timer_setup(&a6xx_gpu->preempt_timer, a6xx_preempt_timer, 0);
diff --git a/drivers/gpu/drm/msm/adreno/adreno_gpu.c b/drivers/gpu/drm/msm/adreno/adreno_gpu.c
index dadfe4c69438..9218fc567144 100644
--- a/drivers/gpu/drm/msm/adreno/adreno_gpu.c
+++ b/drivers/gpu/drm/msm/adreno/adreno_gpu.c
@@ -335,7 +335,8 @@ int adreno_fault_handler(struct msm_gpu *gpu, unsigned long iova, int flags,
struct msm_gpu_fault_info fault_info = {};
/* Turn off the hangcheck timer to keep it from bothering us */
- timer_delete(&gpu->hangcheck_timer);
+ for (int i = 0; i < gpu->nr_rings; i++)
+ timer_delete(&gpu->rb[i]->hangcheck_timer);
/* Let any concurrent GMU transactions know that the MMU may be
* blocked for a while and they should wait on us.
diff --git a/drivers/gpu/drm/msm/msm_gpu.c b/drivers/gpu/drm/msm/msm_gpu.c
index 8ed395cc69f5..5232305de58a 100644
--- a/drivers/gpu/drm/msm/msm_gpu.c
+++ b/drivers/gpu/drm/msm/msm_gpu.c
@@ -467,7 +467,9 @@ static void recover_worker(struct kthread_work *work)
struct drm_device *dev = gpu->dev;
struct msm_drm_private *priv = dev->dev_private;
struct msm_gem_submit *submit;
- struct msm_ringbuffer *cur_ring = gpu->funcs->active_ring(gpu);
+ struct msm_ringbuffer *cur_ring = gpu->hung_ring ?
+ gpu->hung_ring : gpu->funcs->active_ring(gpu);
+ gpu->hung_ring = NULL;
char *comm = NULL, *cmd = NULL;
unsigned int noreclaim_flag;
struct task_struct *task;
@@ -629,11 +631,11 @@ void msm_gpu_fault_crashstate_capture(struct msm_gpu *gpu, struct msm_gpu_fault_
mutex_unlock(&gpu->lock);
}
-static void hangcheck_timer_reset(struct msm_gpu *gpu)
+static void hangcheck_ring_timer_reset(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
{
struct msm_drm_private *priv = gpu->dev->dev_private;
- mod_timer(&gpu->hangcheck_timer,
- round_jiffies_up(jiffies + msecs_to_jiffies(priv->hangcheck_period)));
+ mod_timer(&ring->hangcheck_timer,
+ round_jiffies_up(jiffies + msecs_to_jiffies(priv->hangcheck_period)));
}
static bool made_progress(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
@@ -651,11 +653,31 @@ static bool made_progress(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
return true;
}
-static void hangcheck_handler(struct timer_list *t)
+static bool check_ring_timeout(struct msm_ringbuffer *ring, unsigned long timeout)
+{
+ struct msm_gpu *gpu = ring->gpu;
+ u64 start, end;
+
+ if (!gpu->funcs->get_ctx_timestamp)
+ return !made_progress(gpu, ring);
+
+ start = ring->memptrs->last_job_start_ctx;
+
+ if (gpu->funcs->get_ctx_timestamp(ring, &end))
+ end = ring->memptrs->last_active_ctxcycles;
+
+ if (end >= start)
+ return (end - start) > timeout;
+ else
+ return false;
+}
+
+static void hangcheck_worker(struct kthread_work *work)
{
- struct msm_gpu *gpu = timer_container_of(gpu, t, hangcheck_timer);
+ struct msm_ringbuffer *ring = container_of(work, struct msm_ringbuffer, hangcheck_work);
+ struct msm_gpu *gpu = ring->gpu;
+ struct msm_drm_private *priv = gpu->dev->dev_private;
struct drm_device *dev = gpu->dev;
- struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu);
uint32_t fence = ring->memptrs->fence;
if (fence != ring->hangcheck_fence) {
@@ -663,7 +685,7 @@ static void hangcheck_handler(struct timer_list *t)
ring->hangcheck_fence = fence;
ring->hangcheck_progress_retries = 0;
} else if (fence_before(fence, ring->fctx->last_fence) &&
- !made_progress(gpu, ring)) {
+ check_ring_timeout(ring, priv->hangcheck_period * 19200)) {
/* no progress and not done.. hung! */
ring->hangcheck_fence = fence;
ring->hangcheck_progress_retries = 0;
@@ -674,17 +696,26 @@ static void hangcheck_handler(struct timer_list *t)
DRM_DEV_ERROR(dev->dev, "%s: submitted fence: %u\n",
gpu->name, ring->fctx->last_fence);
+ gpu->hung_ring = ring;
kthread_queue_work(gpu->worker, &gpu->recover_work);
}
/* if still more pending work, reset the hangcheck timer: */
if (fence_after(ring->fctx->last_fence, ring->hangcheck_fence))
- hangcheck_timer_reset(gpu);
+ hangcheck_ring_timer_reset(gpu, ring);
/* workaround for missing irq: */
msm_gpu_retire(gpu);
}
+static void hangcheck_handler(struct timer_list *t)
+{
+ struct msm_ringbuffer *ring = timer_container_of(ring, t, hangcheck_timer);
+ struct msm_gpu *gpu = ring->gpu;
+
+ kthread_queue_work(gpu->worker, &ring->hangcheck_work);
+}
+
/*
* Cmdstream submission/retirement:
*/
@@ -826,7 +857,7 @@ void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
submit->ring->cur_ctx_seqno = submit->queue->ctx->seqno;
pm_runtime_put(&gpu->pdev->dev);
- hangcheck_timer_reset(gpu);
+ hangcheck_ring_timer_reset(gpu, submit->ring);
}
/*
@@ -923,8 +954,6 @@ int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
if (funcs->progress)
priv->hangcheck_period /= 2;
- timer_setup(&gpu->hangcheck_timer, hangcheck_handler, 0);
-
/* Map registers: */
gpu->mmio = msm_ioremap(pdev, config->ioname);
if (IS_ERR(gpu->mmio)) {
@@ -1006,6 +1035,9 @@ int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
goto fail;
}
+ timer_setup(&gpu->rb[i]->hangcheck_timer, hangcheck_handler, 0);
+ kthread_init_work(&gpu->rb[i]->hangcheck_work, hangcheck_worker);
+
memptrs += sizeof(struct msm_rbmemptrs);
memptrs_iova += sizeof(struct msm_rbmemptrs);
}
diff --git a/drivers/gpu/drm/msm/msm_gpu.h b/drivers/gpu/drm/msm/msm_gpu.h
index c87a6e32658f..ae83e5059eea 100644
--- a/drivers/gpu/drm/msm/msm_gpu.h
+++ b/drivers/gpu/drm/msm/msm_gpu.h
@@ -101,6 +101,7 @@ struct msm_gpu_funcs {
/* Flush perfcntrs before reading (optional): */
void (*perfcntr_flush)(struct msm_gpu *gpu);
+ int (*get_ctx_timestamp)(struct msm_ringbuffer *ring, uint64_t *value);
};
/* Additional state for iommu faults: */
@@ -249,6 +250,8 @@ struct msm_gpu {
/* work for handling GPU recovery: */
struct kthread_work recover_work;
+ struct msm_ringbuffer *hung_ring;
+
/** retire_event: notified when submits are retired: */
wait_queue_head_t retire_event;
@@ -818,7 +821,8 @@ static inline void msm_gpu_crashstate_put(struct msm_gpu *gpu)
static inline void msm_trigger_recovery(struct msm_gpu *gpu)
{
- timer_delete(&gpu->hangcheck_timer);
+ for (int i = 0; i < gpu->nr_rings; i++)
+ timer_delete(&gpu->rb[i]->hangcheck_timer);
kthread_queue_work(gpu->worker, &gpu->recover_work);
}
diff --git a/drivers/gpu/drm/msm/msm_ringbuffer.h b/drivers/gpu/drm/msm/msm_ringbuffer.h
index 0bfb6b0f42fa..6209c2fd3e67 100644
--- a/drivers/gpu/drm/msm/msm_ringbuffer.h
+++ b/drivers/gpu/drm/msm/msm_ringbuffer.h
@@ -39,6 +39,9 @@ struct msm_rbmemptrs {
volatile u32 context_idr;
volatile u32 perfcntr_fence;
+
+ volatile u64 last_job_start_ctx;
+ volatile u64 last_active_ctxcycles;
};
struct msm_cp_state {
@@ -77,6 +80,13 @@ struct msm_ringbuffer {
uint64_t memptrs_iova;
struct msm_fence_context *fctx;
+ /* Hang and Inactivity Detection:
+ */
+ struct timer_list hangcheck_timer;
+
+ /* work for handling GPU hangcheck: */
+ struct kthread_work hangcheck_work;
+
/**
* hangcheck_progress_retries:
*
--
2.56.1
prev parent reply other threads:[~2026-12-05 16:01 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 16:01 [PATCH v4 0/2] Make hang check aware of preemption Anna Maniscalco
2026-12-05 16:01 ` [PATCH v4 1/2] drm/msm: Add an helper to trigger recovery Anna Maniscalco
2026-12-05 16:01 ` Anna Maniscalco [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-preemption_aware_hangcheck-v4-2-1df0153b07c2@gmail.com \
--to=anna.maniscalco2000@gmail.com \
--cc=abhinav.kumar@linux.dev \
--cc=airlied@gmail.com \
--cc=akhilpo@oss.qualcomm.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=freedreno@lists.freedesktop.org \
--cc=jesszhan0024@gmail.com \
--cc=konradybcio@kernel.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lumag@kernel.org \
--cc=marijn.suijten@somainline.org \
--cc=robin.clark@oss.qualcomm.com \
--cc=sean@poorly.run \
--cc=simona@ffwll.ch \
/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®