Re: [PATCH] powerpc/bug: Remove specific powerpc BUG_ON()

From: Christophe Leroy
Date: Thu Feb 11 2021 - 11:37:10 EST




Le 11/02/2021 à 15:30, Segher Boessenkool a écrit :
On Thu, Feb 11, 2021 at 03:09:43PM +0100, Christophe Leroy wrote:
Le 11/02/2021 à 12:49, Segher Boessenkool a écrit :
On Thu, Feb 11, 2021 at 07:41:52AM +0000, Christophe Leroy wrote:
powerpc BUG_ON() is based on using twnei or tdnei instruction,
which obliges gcc to format the condition into a 0 or 1 value
in a register.

Huh? Why is that?

Will it work better if this used __builtin_trap? Or does the kernel only
detect very specific forms of trap instructions?

We already made a try with __builtin_trap() 1,5 year ago, see
https://patchwork.ozlabs.org/project/linuxppc-dev/patch/20510ce03cc9463f1c9e743c1d93b939de501b53.1566219503.git.christophe.leroy@xxxxxx/

The main problems encountered are:
- It is only possible to use it for BUG_ON, not for WARN_ON because GCC
considers it as noreturn. Is there any workaround ?

A trap is noreturn by definition:

-- Built-in Function: void __builtin_trap (void)
This function causes the program to exit abnormally.

- The kernel (With CONFIG_DEBUG_BUGVERBOSE) needs to be able to identify
the source file and line corresponding to the trap. How can that be done
with __builtin_trap() ?

The DWARF debug info should be sufficient. Perhaps you can post-process
some way?

You can create a trap that falls through yourself (by having a trap-on
condition with a condition that is always true, but make the compiler
not see that). This isn't efficient though.

Could you file a feature request (in bugzilla)? It is probably useful
for generic code as well, but we could implement this for powerpc only
if needed.


Ok I will.


For sure using __builtin_trap() would be the best.

unsigned long test1(unsigned long msr)
{
WARN_ON((msr & (MSR_DR | MSR_IR | MSR_RI)) != (MSR_DR | MSR_IR | MSR_RI));

return msr;
}

unsigned long test2(unsigned long msr)
{
if ((msr & (MSR_DR | MSR_IR | MSR_RI)) != (MSR_DR | MSR_IR | MSR_RI))
__builtin_trap();

return msr;
}

000003c0 <test1>:
3c0: 70 69 00 32 andi. r9,r3,50
3c4: 69 29 00 32 xori r9,r9,50
3c8: 31 49 ff ff addic r10,r9,-1
3cc: 7d 2a 49 10 subfe r9,r10,r9
3d0: 0f 09 00 00 twnei r9,0
3d4: 4e 80 00 20 blr

000003d8 <test2>:
3d8: 70 69 00 32 andi. r9,r3,50
3dc: 0f 09 00 32 twnei r9,50
3e0: 4e 80 00 20 blr

Christophe