mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mul_u64_u64_div_u64: fix the division-by-zero behavior
@ 2025-06-16 18:48 Nicolas Pitre
  2025-06-16 18:58 ` Thomas Weißschuh
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Pitre @ 2025-06-16 18:48 UTC (permalink / raw)
  To: Andrew Morton, David Laight
  Cc: Oleg Nesterov, Uwe Kleine-König, Peter Zijlstra, Biju Das,
	linux-kernel

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@baylibre.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);
 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mul_u64_u64_div_u64: fix the division-by-zero behavior
  2025-06-16 18:48 [PATCH] mul_u64_u64_div_u64: fix the division-by-zero behavior Nicolas Pitre
@ 2025-06-16 18:58 ` Thomas Weißschuh
  2025-06-16 19:02   ` Nicolas Pitre
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Weißschuh @ 2025-06-16 18:58 UTC (permalink / raw)
  To: Nicolas Pitre
  Cc: Andrew Morton, David Laight, Oleg Nesterov,
	Uwe Kleine-König, Peter Zijlstra, Biju Das, linux-kernel

On 2025-06-16 14:48:44-0400, Nicolas Pitre wrote:
> 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@baylibre.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 */

This is OPTIMIZER_HIDE_VAR().

> +		return ~0UL/zero;
> +	}
>  
>  	int shift = __builtin_ctzll(c);
>  

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mul_u64_u64_div_u64: fix the division-by-zero behavior
  2025-06-16 18:58 ` Thomas Weißschuh
@ 2025-06-16 19:02   ` Nicolas Pitre
  0 siblings, 0 replies; 3+ messages in thread
From: Nicolas Pitre @ 2025-06-16 19:02 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Andrew Morton, David Laight, Oleg Nesterov,
	Uwe Kleine-König, Peter Zijlstra, Biju Das, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1447 bytes --]

On Mon, 16 Jun 2025, Thomas Weißschuh wrote:

> On 2025-06-16 14:48:44-0400, Nicolas Pitre wrote:
> > 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@baylibre.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 */
> 
> This is OPTIMIZER_HIDE_VAR().

Excellent! I thought I'd seen that somewhere but couldn't remember it.


> 
> > +		return ~0UL/zero;
> > +	}
> >  
> >  	int shift = __builtin_ctzll(c);
> >  
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-16 19:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-16 18:48 [PATCH] mul_u64_u64_div_u64: fix the division-by-zero behavior Nicolas Pitre
2025-06-16 18:58 ` Thomas Weißschuh
2025-06-16 19:02   ` Nicolas Pitre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®