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

From: David Laight
Date: Fri Jul 25 2025 - 17:46:51 EST


On Fri, 25 Jul 2025 12:12:02 +0200
Oleg Nesterov <oleg@xxxxxxxxxx> wrote:

> On 07/24, H. Peter Anvin wrote:
> >
> > On July 24, 2025 4:14:26 AM PDT, Oleg Nesterov <oleg@xxxxxxxxxx> wrote:
> > >Finally. If we really want to optimize this function as much as possible,
> > >we can add the CONFIG_CC_HAS_ASM_GOTO_OUTPUT version as Peter suggests.
> > >I guess this should work:
>
> ...
>
> > >> Forgot to mention... Not that I think this is a good idea, but if we don't
> > >> use BUG/WARN, we can probably add EX_FLAG_ and do something like below.
>
> ...
>
> > Seems good to me.
>
> Thanks, but which one? "asm goto" or EX_FLAG_XXX_AX hack?
>
> As for the latter. I took another look at asm/extable_fixup_types.h
> and it turns out we don't need a new EX_FLAG_, this version
>
> static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div)
> {
> u64 q;
>
> asm ("mulq %2; 1: divq %3; 2:\n"
> _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_IMM_REG | EX_DATA_IMM(-1))

That should be _ASM_EXTABLE_TYPE_REG() with an extra %%rax parameter.
It works because ax is register zero.

> : "=a" (q)
> : "a" (a), "rm" (mul), "rm" (div)

The "rm" should both be ASM_INPUT_RM

> : "rdx");
>
> return q;
> }
>
> seems to work and I guess it is the absolute winner performance wise.
>
> But to me the main question is: Peter, David, do we want to add
> BUG and/or WARN into mul_u64_u64_div_u64??? If yes, then this version
> won't work.

Looking through the code in extable.[ch] there is actually scope for adding
an extra EX_TYPE that is the same as IMM_REG but contains a WARN_ONCE().
It would be a 'global' ONCE for all such traps, but that probably doesn't matter.
The easiest way to make it 'per trap site' is to modify the 'exception_table_entry'
itself (eg change the type), but that might not be acceptable.
An alternative would be a special EX_TYPE_DIVIDE_OVERFLOW with an explicit message.

Probably best to leave all that for later.

Perhaps more useful would be adding an 'int *overflow' argument.
If not a compile-time NULL then set to 0 on entry and -1 if there is a trap.
That would need the 'jmp in the normal path' asm version - so two copies
of the entire asm block.

Another one for a later patch.

David

>
> Oleg.
>