Re: [PATCH] x86/math64: handle #DE in mul_u64_u64_div_u64()

From: Oleg Nesterov
Date: Wed Jul 23 2025 - 05:39:58 EST


On 07/22, David Laight wrote:
>
> On Tue, 22 Jul 2025 15:21:48 +0200
> Oleg Nesterov <oleg@xxxxxxxxxx> wrote:
>
> > static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div)
> > {
> > char ok = 0;
> > u64 q;
> >
> > asm ("mulq %3; 1: divq %4; movb $1,%1; 2:\n"
> > _ASM_EXTABLE(1b, 2b)
> > : "=a" (q), "+q" (ok)
> > : "a" (a), "rm" (mul), "rm" (div)
> > : "rdx");
> >
> > if (ok)
> > return q;
> > BUG_ON(!div);
> > WARN_ONCE(1, "muldiv overflow.\n");
>
> I wonder what WARN_ON_ONCE("muldiv overflow") outputs?

Well, it outputs "muldiv overflow." ;) So I am not sure it is better
than just WARN_ON_ONCE(1).

> Actually, without the BUG or WARN you want:
> u64 fail = ~(u64)0;
> then
> incq $1 ... "+r" (fail)
> and finally
> return q | fail;
> to remove the conditional branches from the normal path
> (apart from one the caller might do)

I was thinking about

static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div)
{
u64 q;

asm ("mulq %2; 1: divq %3; jmp 3f; 2: movq $-1,%0; 3:\n"
_ASM_EXTABLE(1b, 2b)
: "=a" (q)
: "a" (a), "rm" (mul), "rm" (div)
: "rdx");

return q;
}

to remove the conditional branch and additional variable. Your version
is probably beterr... But this is without WARN/BUG.

So, which version do you prefer?

Oleg.