Skip to contentSkip to conversation

gpu: drm: msm: use div64_u64() instead of do_div()

PATCH In review Qing Wang 2 participants 3 messages Feb 9, 2022 – Feb 10, 2022

Subsystems (4)
lore
Share & tools
lore

Qing Wang

author
#0 lore raw

PATCH 1/1 gpu: drm: msm: use div64_u64() instead of do_div()

From: Wang Qing <[email protected]>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division.

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

---
 drivers/gpu/drm/msm/msm_gpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gpu.c +1 -1
index 2c1049c..aa4617b
@@ -648,7 +648,7 @@ static void retire_submit(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
 	/* Calculate the clock frequency from the number of CP cycles */
 	if (elapsed) {
 		clock = (stats->cpcycles_end - stats->cpcycles_start) * 1000;
-		do_div(clock, elapsed);
+		div64_u64(clock, elapsed);
 	}
 
 	trace_msm_gpu_submit_retired(submit, elapsed, clock,
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Qing Wang

From: Wang Qing <[email protected]>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division.

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

Quoted 17 lines
---
  drivers/gpu/drm/msm/msm_gpu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gpu.c +1 -1
index 2c1049c..aa4617b
@@ -648,7 +648,7 @@ static void retire_submit(struct msm_gpu *gpu, struct msm_ringbuffer *ring,
  	/* Calculate the clock frequency from the number of CP cycles */
  	if (elapsed) {
  		clock = (stats->cpcycles_end - stats->cpcycles_start) * 1000;
-		do_div(clock, elapsed);
+		div64_u64(clock, elapsed);
  	}
 
  	trace_msm_gpu_submit_retired(submit, elapsed, clock,
Signature · 2 lines
With best wishes
Dmitry
#2 lore raw
Quoted 7 lines · Dmitry Baryshkov
Quoted 6 lines · Qing Wang

From: Wang Qing <[email protected]>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division.

Quoted 6 lines
Quoted 4 lines

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

After rechecking, I'd like to withdraw my R-B tag (Minecrell, thanks for pointing this out!)

The div64_u64 is not equivalent to do_div. It returns the quotient rather than modifying the first arg. Moreover it is unoptimal on 32-bit arches.

I'd suggest changing the math to remove multiplications by 1000 and 10000 before division. Or just ignoring this at all, judging from the fact that these values are used only for tracing rather than actual calculations.

Quoted 20 lines
Quoted 18 lines

---
  drivers/gpu/drm/msm/msm_gpu.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gpu.c +1 -1
index 2c1049c..aa4617b
@@ -648,7 +648,7 @@ static void retire_submit(struct msm_gpu *gpu, 
 struct msm_ringbuffer *ring,
       /* Calculate the clock frequency from the number of CP cycles */
       if (elapsed) {
           clock = (stats->cpcycles_end - stats->cpcycles_start) * 1000;
-        do_div(clock, elapsed);
+        div64_u64(clock, elapsed);
       }
       trace_msm_gpu_submit_retired(submit, elapsed, clock,
Signature · 2 lines
With best wishes
Dmitry