Skip to contentSkip to conversation

lib-mul_u64_u64_div_u64-combine-overflow-and-divide-by-zero-checks.patch removed from -mm tree

merged mm-nonmm-stable Andrew Morton 1 participant 1 message Nov 20, 2025

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled
Subject: lib: mul_u64_u64_div_u64(): combine overflow and divide by zero checks
has been removed from the -mm tree. Its filename was

     lib-mul_u64_u64_div_u64-combine-overflow-and-divide-by-zero-checks.patch

This patch was dropped because it was merged into the mm-nonmm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: David Laight <[email protected]>
Subject: lib: mul_u64_u64_div_u64(): combine overflow and divide by zero checks
Date: Wed, 5 Nov 2025 20:10:28 +0000

Since the overflow check always triggers when the divisor is zero move the check for divide by zero inside the overflow check. This means there is only one test in the normal path.

Cc Biju Das <[email protected]>
Cc Borislav Betkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Li RongQing <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleinxer <[email protected]>
Cc Uwe Kleine-König <[email protected]>

---

 lib/math/div64.c |   21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
lib/math/div64.c renamed from lib/math/div64.c~lib-mul_u64_u64_div_u64-combine-overflow-and-divide-by-zero-checks +10 -11
@@ -212,12 +212,16 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u6
 
 #endif
 
-	/* make sure d is not zero, trigger runtime exception otherwise */
-	if (unlikely(d == 0)) {
-		unsigned long zero = 0;
-
-		OPTIMIZER_HIDE_VAR(zero);
-		return ~0UL/zero;
+	if (unlikely(n_hi >= d)) {
+		/* trigger runtime exception if divisor is zero */
+		if (d == 0) {
+			unsigned long zero = 0;
+
+			OPTIMIZER_HIDE_VAR(zero);
+			return ~0UL/zero;
+		}
+		/* overflow: result is unrepresentable in a u64 */
+		return ~0ULL;
 	}
 
 	int shift = __builtin_ctzll(d);
@@ -234,11 +238,6 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u6
 		 */
 	}
 
-	if (n_hi >= d) {
-		/* overflow: result is unrepresentable in a u64 */
-		return -1;
-	}
-
 	/* Do the full 128 by 64 bits division */
 
 	shift = __builtin_clzll(d);

_

Patches currently in -mm which might be from [email protected] are