[PATCH] drm/msm: Use div64_u64() for the retired-submit GPU clock
From: EMCP333 via B4 Relay
Date: Sun Nov 22 2026 - 00:12:52 EST
From: EMCP333 <emcp@xxxxxxxx>
retire_submit() derives the GPU clock from the CP cycle and always-on
counter deltas:
if (elapsed) {
clock = cycles * 1000;
do_div(clock, elapsed);
}
elapsed is a u64, but do_div() takes a 32-bit divisor, so it is silently
truncated. The guard tests all 64 bits while the division uses only the
low 32, so any elapsed value that is a non-zero multiple of 2^32 divides
by zero, and any value of 2^32 ns or more gives a wrong clock. On 32-bit
ARM the division by zero lands in __do_div64 and prints "Division by zero
in kernel" with a backtrace on every such retire.
Observed on an Adreno 330 (MSM8974, LG Nexus 5) running a downstream
6.16 kernel: the backtrace repeated every 0.3-0.9 s from the first GPU
submit, 232 times in 34 minutes. The faulting return address,
retire_submits+0x3dc, is the instruction after the only
non-constant-divisor __do_div64 call in the function. Why elapsed has
zero low 32 bits so often on this GPU was not investigated.
Use div64_u64(), which takes a 64-bit divisor.
Tested on the downstream postmarketOS 6.16.12 msm8974 kernel on a Nexus
5: no "Division by zero" over an 1850 s soak with about 30 minutes of
GPU load (before: 232 in 34 minutes). On this tree it is build-tested
only.
The code and this changelog were prepared with the help of an AI coding
assistant. The submitter has reviewed both and takes responsibility for
them.
Fixes: 4241db42e474 ("drm/msm/gpu: Add trace events for tracking GPU submissions")
Signed-off-by: EMCP333 <emcp@xxxxxxxx>
---
drivers/gpu/drm/msm/msm_gpu.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/drivers/gpu/drm/msm/msm_gpu.c b/drivers/gpu/drm/msm/msm_gpu.c
index 8319119041..8b99998e19 100644
--- a/drivers/gpu/drm/msm/msm_gpu.c
+++ b/drivers/gpu/drm/msm/msm_gpu.c
@@ -705,10 +705,8 @@ static void retire_submit(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
cycles = stats->cpcycles_end - stats->cpcycles_start;
/* Calculate the clock frequency from the number of CP cycles */
- if (elapsed) {
- clock = cycles * 1000;
- do_div(clock, elapsed);
- }
+ if (elapsed)
+ clock = div64_u64(cycles * 1000, elapsed);
submit->queue->ctx->elapsed_ns += elapsed;
submit->queue->ctx->cycles += cycles;
---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261122-b4-drm-msm-retire-div64-11589073136c
Best regards,
--
EMCP333 <emcp@xxxxxxxx>