From: "Thomas Weißschuh" <linux@weissschuh.net>
To: Nicolas Pitre <npitre@baylibre.com>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
"David Laight" <david.laight.linux@gmail.com>,
"Oleg Nesterov" <oleg@redhat.com>,
"Uwe Kleine-König" <u.kleine-koenig@baylibre.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Biju Das" <biju.das.jz@bp.renesas.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mul_u64_u64_div_u64: fix the division-by-zero behavior
Date: Mon, 16 Jun 2025 20:58:29 +0200 [thread overview]
Message-ID: <f256c2d1-6f35-447d-9f21-30d0c4a2419a@t-8ch.de> (raw)
In-Reply-To: <q2o7r916-5601-11pn-30pn-8n5ns6p079o7@onlyvoer.pbz>
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);
>
next prev parent reply other threads:[~2025-06-16 18:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-16 18:48 Nicolas Pitre
2025-06-16 18:58 ` Thomas Weißschuh [this message]
2025-06-16 19:02 ` Nicolas Pitre
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f256c2d1-6f35-447d-9f21-30d0c4a2419a@t-8ch.de \
--to=linux@weissschuh.net \
--cc=akpm@linux-foundation.org \
--cc=biju.das.jz@bp.renesas.com \
--cc=david.laight.linux@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=npitre@baylibre.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=u.kleine-koenig@baylibre.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®