[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <q2o7r916-5601-11pn-30pn-8n5ns6p079o7@onlyvoer.pbz>
Date: Mon, 16 Jun 2025 14:48:44 -0400 (EDT)
From: Nicolas Pitre <npitre@...libre.com>
To: Andrew Morton <akpm@...ux-foundation.org>,
David Laight <david.laight.linux@...il.com>
cc: Oleg Nesterov <oleg@...hat.com>,
Uwe Kleine-König <u.kleine-koenig@...libre.com>,
Peter Zijlstra <peterz@...radead.org>,
Biju Das <biju.das.jz@...renesas.com>, linux-kernel@...r.kernel.org
Subject: [PATCH] mul_u64_u64_div_u64: fix the division-by-zero behavior
The current implementation forces a compile-time 1/0 division, which
generates an undefined instruction (ud2 on x86) rather than a proper
runtime division-by-zero exception.
Change to trigger an actual div-by-0 exception at runtime, consistent
with other division operations. Use a non-1 dividend to prevent the
compiler from optimizing the division into a comparison.
Signed-off-by: Nicolas Pitre <npitre@...libre.com>
diff --git a/lib/math/div64.c b/lib/math/div64.c
index 5faa29208bdb..eaa0c7e8b974 100644
--- a/lib/math/div64.c
+++ b/lib/math/div64.c
@@ -212,12 +212,13 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
#endif
- /* make sure c is not zero, trigger exception otherwise */
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdiv-by-zero"
- if (unlikely(c == 0))
- return 1/0;
-#pragma GCC diagnostic pop
+ /* make sure c is not zero, trigger runtime exception otherwise */
+ if (unlikely(c == 0)) {
+ unsigned long zero = 0;
+
+ asm ("" : "+r" (zero)); /* hide actual value from the compiler */
+ return ~0UL/zero;
+ }
int shift = __builtin_ctzll(c);
Powered by blists - more mailing lists