Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib: mul_u64_u64_div_u64(): combine overflow and divide by zero checks

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.

Link: https://lkml.kernel.org/r/20251105201035.64043-3-david.laight.linux@gmail.com
Signed-off-by: David Laight <david.laight.linux@gmail.com>
Reviewed-by: Nicolas Pitre <npitre@baylibre.com>
Cc: Biju Das <biju.das.jz@bp.renesas.com>
Cc: Borislav Betkov <bp@alien8.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Li RongQing <lirongqing@baidu.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleinxer <tglx@linutronix.de>
Cc: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

David Laight and committed by
Andrew Morton
08092bab 5944f875

+9 -10
+9 -10
lib/math/div64.c
··· 212 212 213 213 #endif 214 214 215 - /* make sure d is not zero, trigger runtime exception otherwise */ 216 - if (unlikely(d == 0)) { 217 - unsigned long zero = 0; 215 + if (unlikely(n_hi >= d)) { 216 + /* trigger runtime exception if divisor is zero */ 217 + if (d == 0) { 218 + unsigned long zero = 0; 218 219 219 - OPTIMIZER_HIDE_VAR(zero); 220 - return ~0UL/zero; 220 + OPTIMIZER_HIDE_VAR(zero); 221 + return ~0UL/zero; 222 + } 223 + /* overflow: result is unrepresentable in a u64 */ 224 + return ~0ULL; 221 225 } 222 226 223 227 int shift = __builtin_ctzll(d); ··· 236 232 * res = div64_u64_rem(n, d >> shift, &rem); 237 233 * rem = (rem << shift) + (n_lo - (n << shift)); 238 234 */ 239 - } 240 - 241 - if (n_hi >= d) { 242 - /* overflow: result is unrepresentable in a u64 */ 243 - return -1; 244 235 } 245 236 246 237 /* Do the full 128 by 64 bits division */