Skip to contentSkip to conversation

timekeeping: improve arithmetic division in scale64_check_overflow()

PATCH Applied Wen Yang 2 participants 2 messages Jan 20, 2020 – Mar 4, 2020 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore

Wen Yang

author
#0 lore raw

PATCH 1/1 timekeeping: improve arithmetic division in scale64_check_overflow()

do_div() does a 64-by-32 division. Use div64_u64() instead of it if the divisor is u64, to avoid truncation to 32-bit. And as a nice side effect also cleans up the function a bit.

Cc John Stultz <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Stephen Boyd <[email protected]>
---
 kernel/time/timekeeping.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/time/timekeeping.c +1 -2
index ca69290bee2a..4fc2af4367a7 100644
@@ -1005,9 +1005,8 @@ static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
 	    ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
 		return -EOVERFLOW;
 	tmp *= mult;
-	rem *= mult;
 
-	do_div(rem, div);
+	rem = div64_u64(rem * mult, div);
 	*base = tmp + rem;
 	return 0;
 }
Signature · 1 line
2.23.0
merge bot Applied
#1 lore raw

[tip: timers/core] timekeeping: Prevent 32bit truncation in scale64_check_overflow()

The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     4cbbc3a0eeed675449b1a4d080008927121f3da3
Gitweb:        https://git.kernel.org/tip/4cbbc3a0eeed675449b1a4d080008927121f3da3
Author:        Wen Yang <[email protected]>
AuthorDate:    Mon, 20 Jan 2020 18:05:23 +08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 04 Mar 2020 10:17:51 +01:00

timekeeping: Prevent 32bit truncation in scale64_check_overflow()

While unlikely the divisor in scale64_check_overflow() could be >= 32bit in scale64_check_overflow(). do_div() truncates the divisor to 32bit at least on 32bit platforms.

Use div64_u64() instead to avoid the truncation to 32-bit.

[ tglx: Massaged changelog ]

---
 kernel/time/timekeeping.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/time/timekeeping.c +1 -2
index ca69290..4fc2af4 100644
@@ -1005,9 +1005,8 @@ static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
 	    ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
 		return -EOVERFLOW;
 	tmp *= mult;
-	rem *= mult;
 
-	do_div(rem, div);
+	rem = div64_u64(rem * mult, div);
 	*base = tmp + rem;
 	return 0;
 }