mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drm/msm: Use div64_u64() for the retired-submit GPU clock
@ 2026-11-22  5:12 EMCP333 via B4 Relay
  2026-11-23 14:54 ` Konrad Dybcio
  0 siblings, 1 reply; 2+ messages in thread
From: EMCP333 via B4 Relay @ 2026-11-22  5:12 UTC (permalink / raw)
  To: Rob Clark, Dmitry Baryshkov
  Cc: Sean Paul, Konrad Dybcio, Akhil P Oommen, Abhinav Kumar,
	Jessica Zhang, Marijn Suijten, David Airlie, Simona Vetter,
	Jordan Crouse, linux-arm-msm, dri-devel, freedreno, linux-kernel,
	EMCP333

From: EMCP333 <emcp@enser.io>

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@enser.io>
---
 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@enser.io>



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23 14:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22  5:12 [PATCH] drm/msm: Use div64_u64() for the retired-submit GPU clock EMCP333 via B4 Relay
2026-11-23 14:54 ` Konrad Dybcio

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®