Re: [PATCH v3] drm/msm: preemption aware hangcheck

From: Dmitry Baryshkov

Date: Fri Nov 27 2026 - 14:49:22 EST


On Fri, Nov 06, 2026 at 05:46:28PM +0100, Anna Maniscalco wrote:
> Rework hangcheck code to work well toghether with preemption.
>
> 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@xxxxxxxxx>
> ---
> 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.

This text should start the commit message, it's a description of the
problem. The text under three dashes will not land in the kernel and so
nobody would know why you were doing these changes.

>
> depends on https://lore.kernel.org/linux-arm-msm/20261102190010.720443-1-robin.clark@xxxxxxxxxxxxxxxx
> ---
> Changes in v3:
> - Addressed sashiko's feedback
> - Rebased on reworked IFPC disable series
> - Link to v2: https://patch.msgid.link/20261029-preemption_aware_hangcheck-v2-1-be0056e1f02d@xxxxxxxxx
>
> Changes in v2:
> - Various fixes
> - Fix interaction with IFPC by using OOB vote
> - Link to v1: https://lore.kernel.org/r/20250911-preemption_aware_hangcheck-v1-0-974819876819@xxxxxxxxx
>
> To: Rob Clark <robin.clark@xxxxxxxxxxxxxxxx>
> To: Sean Paul <sean@xxxxxxxxxx>
> To: Konrad Dybcio <konradybcio@xxxxxxxxxx>
> To: Akhil P Oommen <akhilpo@xxxxxxxxxxxxxxxx>
> To: Dmitry Baryshkov <lumag@xxxxxxxxxx>
> To: Abhinav Kumar <abhinav.kumar@xxxxxxxxx>
> To: Jessica Zhang <jesszhan0024@xxxxxxxxx>
> To: Marijn Suijten <marijn.suijten@xxxxxxxxxxxxxx>
> To: David Airlie <airlied@xxxxxxxxx>
> To: Simona Vetter <simona@xxxxxxxx>
> Cc: linux-arm-msm@xxxxxxxxxxxxxxx
> Cc: dri-devel@xxxxxxxxxxxxxxxxxxxxx
> Cc: freedreno@xxxxxxxxxxxxxxxxxxxxx
> Cc: linux-kernel@xxxxxxxxxxxxxxx
> ---
> drivers/gpu/drm/msm/adreno/a5xx_gpu.c | 5 +--
> drivers/gpu/drm/msm/adreno/a6xx_gmu.c | 6 +---
> drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 54 +++++++++++++++++++++++------
> drivers/gpu/drm/msm/adreno/a6xx_gpu.h | 1 +
> drivers/gpu/drm/msm/adreno/a6xx_preempt.c | 32 +++++++++++++-----
> drivers/gpu/drm/msm/adreno/a8xx_gpu.c | 12 ++-----
> drivers/gpu/drm/msm/adreno/adreno_gpu.c | 3 +-
> drivers/gpu/drm/msm/msm_gpu.c | 56 ++++++++++++++++++++++++-------
> drivers/gpu/drm/msm/msm_gpu.h | 11 ++++++
> drivers/gpu/drm/msm/msm_ringbuffer.h | 10 ++++++
> 10 files changed, 140 insertions(+), 50 deletions(-)
>
> diff --git a/drivers/gpu/drm/msm/adreno/a5xx_gpu.c b/drivers/gpu/drm/msm/adreno/a5xx_gpu.c
> index 16c7335c4b1f..8800352455df 100644
> --- a/drivers/gpu/drm/msm/adreno/a5xx_gpu.c
> +++ b/drivers/gpu/drm/msm/adreno/a5xx_gpu.c
> @@ -1251,10 +1251,7 @@ static void a5xx_fault_detect_irq(struct msm_gpu *gpu)
> gpu_read64(gpu, REG_A5XX_CP_IB2_BASE),
> gpu_read(gpu, REG_A5XX_CP_IB2_BUFSZ));
>
> - /* Turn off the hangcheck timer to keep it from bothering us */
> - timer_delete(&gpu->hangcheck_timer);
> -
> - kthread_queue_work(gpu->worker, &gpu->recover_work);
> + msm_trigger_recovery(gpu);
> }
>
> #define RBBM_ERROR_MASK \
> diff --git a/drivers/gpu/drm/msm/adreno/a6xx_gmu.c b/drivers/gpu/drm/msm/adreno/a6xx_gmu.c
> index bba5468db42e..bcbe1b949217 100644
> --- a/drivers/gpu/drm/msm/adreno/a6xx_gmu.c
> +++ b/drivers/gpu/drm/msm/adreno/a6xx_gmu.c
> @@ -28,11 +28,7 @@ static void a6xx_gmu_fault(struct a6xx_gmu *gmu)
> /* FIXME: add a banner here */
> gmu->hung = true;
>
> - /* Turn off the hangcheck timer while we are resetting */
> - timer_delete(&gpu->hangcheck_timer);
> -
> - /* Queue the GPU handler because we need to treat this as a recovery */
> - kthread_queue_work(gpu->worker, &gpu->recover_work);
> + msm_trigger_recovery(gpu);
> }
>
> static irqreturn_t a6xx_gmu_irq(int irq, void *data)
> diff --git a/drivers/gpu/drm/msm/adreno/a6xx_gpu.c b/drivers/gpu/drm/msm/adreno/a6xx_gpu.c
> index 773c1727dd31..179e42119307 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);
> @@ -1838,13 +1844,10 @@ static void a6xx_fault_detect_irq(struct msm_gpu *gpu)
> gpu_read64(gpu, REG_A6XX_CP_IB2_BASE),
> gpu_read(gpu, REG_A6XX_CP_IB2_REM_SIZE));
>
> - /* Turn off the hangcheck timer to keep it from bothering us */
> - timer_delete(&gpu->hangcheck_timer);
> -
> /* Turn off interrupts to avoid triggering recovery again */
> gpu_write(gpu, REG_A6XX_RBBM_INT_0_MASK, 0);
>
> - kthread_queue_work(gpu->worker, &gpu->recover_work);
> + msm_trigger_recovery(gpu);
> }
>
> static void a7xx_sw_fuse_violation_irq(struct msm_gpu *gpu)
> @@ -1861,11 +1864,8 @@ static void a7xx_sw_fuse_violation_irq(struct msm_gpu *gpu)
> * to legacy blending.
> */
> if (status & (A7XX_CX_MISC_SW_FUSE_VALUE_RAYTRACING |
> - A7XX_CX_MISC_SW_FUSE_VALUE_LPAC)) {
> - timer_delete(&gpu->hangcheck_timer);
> -
> - kthread_queue_work(gpu->worker, &gpu->recover_work);
> - }
> + A7XX_CX_MISC_SW_FUSE_VALUE_LPAC))
> + msm_trigger_recovery(gpu);
> }
>
> static void a6xx_gpu_keepalive_vote(struct msm_gpu *gpu, bool on)
> @@ -2293,6 +2293,39 @@ 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);
> +
> + /*
> + * a6xx_gmu_set_oob cannot be called while holding a spinlock.
> + * Limit the scope.
> + */
> + 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);

Isn't this inverted? With the single ring, there is no other ring and no
preemtpion.

> +
> + return ret;
> +}
> +
> static struct msm_ringbuffer *a6xx_active_ring(struct msm_gpu *gpu)
> {
> struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);

> @@ -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(ring ? CP_REG_TO_MEM : CP_NOP, 3);

ring is always non-NULL here

> + 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;

--
With best wishes
Dmitry