x86/tsc: Use div64_ul instead of do_div()
PATCH 1/3 x86/tsc: Use div64_ul instead of do_div()
do_div() truncates a u64 divisor to 32 bit.
This can lead to non-zero being truncated to zero for division.
Fix coccinelle warning
WARNING: do_div() does a 64-by-32 division, please consider using div64_ul instead
--- arch/x86/kernel/tsc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/tsc.c +1 -1
@@ -484,7 +484,7 @@ static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)/* Calculate the PIT value */ delta = t2 - t1; -do_div (delta, ms); +div64_ul (delta, ms);return delta; }
Signature · 1 line
2.43.2
H. Peter Anvin
reviewerQuoted 16 lines · Jules Irenge
do_div()truncates a u64 divisor to 32 bit.
This can lead to non-zero being truncated to zero for division.Fix coccinelle warning
WARNING:do_div()does a 64-by-32 division, please consider usingdiv64_ulinsteadSigned-off-by Jules Irenge <[email protected]>---
arch/x86/kernel/tsc.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/tsc.c +0
-0
arch/x86/kernel/tsc.c static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin) · line 490
Quoted 8 lines
arch/x86/kernel/tsc.c +1
-1@@ -484,7 +484,7 @@ static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)/* Calculate the PIT value */ delta = t2 - t1; -do_div (delta, ms); +div64_ul (delta, ms);return delta; }
Are you sure this is not a false positive? This is a *much* more expensive operation on 32 bits.
From: H. Peter Anvin
Quoted 20 lines
Sent: 28 April 2024 20:13
Quoted 17 lines · Jules Irenge
do_div()truncates a u64 divisor to 32 bit.
This can lead to non-zero being truncated to zero for division.Fix coccinelle warning
WARNING:do_div()does a 64-by-32 division, please consider usingdiv64_ulinsteadSigned-off-by Jules Irenge <[email protected]>---
arch/x86/kernel/tsc.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/tsc.c +0
-0@@ -484,7 +484,7 @@ static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)
Quoted 9 lines
arch/x86/kernel/tsc.c static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin) · line 490
Quoted 7 lines
arch/x86/kernel/tsc.c +1
-1/* Calculate the PIT value */ delta = t2 - t1; -do_div (delta, ms); +div64_ul (delta, ms);return delta; }Are you sure this is not a false positive? This is a *much* more expensive operation on 32 bits.
IIRC do_div() isn't that great either.
Somewhere it changed from being a simple wrapper on the x86 'div' instruction (ie 64 by 32 giving 32 and 32) to being able to generate a 64bit quotent.
But the patch is entirely broken as well.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)