Skip to contentSkip to conversation

time: Use div64_long() instead of do_div()

PATCH Superseded Thorsten Blum 2 participants 5 messages Feb 25, 2024 – Feb 26, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 time: Use div64_long() instead of do_div()

Fixes Coccinelle/coccicheck warning reported by do_div.cocci.

Compared to do_div(), div64_long() does not implicitly cast the divisor and does not unnecessarily calculate the remainder.

---
 kernel/time/jiffies.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/jiffies.c +1 -1
index bc4db9e5ab70..9d23178e2b6a 100644
@@ -91,7 +91,7 @@ int register_refined_jiffies(long cycles_per_second)
 	/* shift_hz stores hz<<8 for extra accuracy */
 	shift_hz = (u64)cycles_per_second << 8;
 	shift_hz += cycles_per_tick/2;
-	do_div(shift_hz, cycles_per_tick);
+	shift_hz = div64_long(shift_hz, cycles_per_tick);
 	/* Calculate nsec_per_tick using shift_hz */
 	nsec_per_tick = (u64)NSEC_PER_SEC << 8;
 	nsec_per_tick += (u32)shift_hz/2;
Signature · 1 line
2.43.2
#1 lore raw

Thorsten,

Quoted 15 lines · Thorsten Blum

Fixes Coccinelle/coccicheck warning reported by do_div.cocci.

Compared to do_div(), div64_long() does not implicitly cast the divisor and does not unnecessarily calculate the remainder.

---
 kernel/time/jiffies.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/jiffies.c +0 -0
index bc4db9e5ab70..9d23178e2b6a 100644

kernel/time/jiffies.c int register_refined_jiffies(long cycles_per_second) · line 94

Quoted 5 lines
kernel/time/jiffies.c +1 -1
@@ -91,7 +91,7 @@ int register_refined_jiffies(long cycles_per_second)
 	/* shift_hz stores hz<<8 for extra accuracy */
 	shift_hz = (u64)cycles_per_second << 8;
 	shift_hz += cycles_per_tick/2;
-	do_div(shift_hz, cycles_per_tick);
+	shift_hz = div64_long(shift_hz, cycles_per_tick);

I am considering using div64_ul() to calculate the result. as shift_hz is unsigned long, assume the sign bit of divisor cycles_per_tick never be set in this context,then div64_long() will do a extra sign extension for result. Or are there other recommendations?

Thanks.

Quoted 3 lines
kernel/time/jiffies.c +0 -0
 	/* Calculate nsec_per_tick using shift_hz */
 	nsec_per_tick = (u64)NSEC_PER_SEC << 8;
 	nsec_per_tick += (u32)shift_hz/2;
Signature · 2 lines
BR
Liao, Chang
#2 lore raw
Quoted 6 lines · Chang

I am considering using div64_ul() to calculate the result. as shift_hz is unsigned long, assume the sign bit of divisor cycles_per_tick never be set in this context,then div64_long() will do a extra sign extension for result.

Yes, div64_ul() is better and also removes the warning.

I'll submit a v2 shortly.

Thanks,
Thorsten

#3 lore raw

PATCH v2 1/1 time: Use div64_ul() instead of do_div()

Fixes Coccinelle/coccicheck warning reported by do_div.cocci.

Compared to do_div(), div64_ul() does not implicitly cast the divisor and does not unnecessarily calculate the remainder.

---
Changes in v2:
- s/div64_long/div64_ul/ as suggested by Chang Liao

---
 kernel/time/jiffies.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/jiffies.c +1 -1
index bc4db9e5ab70..fccee15e94d5 100644
@@ -91,7 +91,7 @@ int register_refined_jiffies(long cycles_per_second)
 	/* shift_hz stores hz<<8 for extra accuracy */
 	shift_hz = (u64)cycles_per_second << 8;
 	shift_hz += cycles_per_tick/2;
-	do_div(shift_hz, cycles_per_tick);
+	shift_hz = div64_ul(shift_hz, cycles_per_tick);
 	/* Calculate nsec_per_tick using shift_hz */
 	nsec_per_tick = (u64)NSEC_PER_SEC << 8;
 	nsec_per_tick += (u32)shift_hz/2;
Signature · 1 line
2.43.2
Reviewed-by on v2 1/1
#4 lore raw

Re: [PATCH v2] time: Use div64_ul() instead of do_div()

Quoted 18 lines · Thorsten Blum

Fixes Coccinelle/coccicheck warning reported by do_div.cocci.

Compared to do_div(), div64_ul() does not implicitly cast the divisor and does not unnecessarily calculate the remainder.

---
Changes in v2:
- s/div64_long/div64_ul/ as suggested by Chang Liao

---
 kernel/time/jiffies.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/jiffies.c +0 -0
index bc4db9e5ab70..fccee15e94d5 100644

kernel/time/jiffies.c int register_refined_jiffies(long cycles_per_second) · line 94

Quoted 5 lines
kernel/time/jiffies.c +1 -1
@@ -91,7 +91,7 @@ int register_refined_jiffies(long cycles_per_second)
 	/* shift_hz stores hz<<8 for extra accuracy */
 	shift_hz = (u64)cycles_per_second << 8;
 	shift_hz += cycles_per_tick/2;
-	do_div(shift_hz, cycles_per_tick);
+	shift_hz = div64_ul(shift_hz, cycles_per_tick);

LGTM

Quoted 3 lines
kernel/time/jiffies.c +0 -0
 	/* Calculate nsec_per_tick using shift_hz */
 	nsec_per_tick = (u64)NSEC_PER_SEC << 8;
 	nsec_per_tick += (u32)shift_hz/2;
Signature · 2 lines
BR
Liao, Chang