Skip to contentSkip to conversation

x86: use clang builtins to read and write eflags

PATCH Nick Desaulniers 2 participants 2 messages Sep 14, 2020 4 messages not in the archive

lore
Share & tools
lore
#4 lore raw
In reply to a message not in the archive

(Bill, please use ./scripts/get_maintainer.pl <patchfile> to get the appropriate list of folks and mailing lists to CC)

Quoted 6 lines · Andy Lutomirski

So with my selftests hat on, the inline asm utterly sucks. Trying to use pushfq / popfq violates the redzone and requires a gross hack to work around. It also messes up the DWARF annotations. The kernel gets a free pass on both of these because we don't use a redzone and we don't use DWARF.

Sorry, I don't fully understand:
1. What's the redzone?
2. How does pushfq/popfq violate it?
3. What is the "gross hack"/"workaround?" (So that we might consider removing it if these builtins help).
4. The kernel does use DWARF, based on configs, right?

Quoted 11 lines

Now GCC and clang's builtins are also ugly. But perhaps we could have a little wrapper that is less ugly?

static __always_inline unsigned long __read_eflags(void) {

#ifdef CONFIG_X86_64
    return __builtin_ia32_readeflags_u64();
#else
    return __builtin_ia32_readeflags_u32();
}

It looks like __builtin_ia32_readeflags_u32, __builtin_ia32_readeflags_u64, __builtin_ia32_writeeflags_u64, and __builtin_ia32_writeeflags_u32 were first available in GCC 4.9; they weren't in GCC 4.8 or older, so we can make use of them unconditionally. I think it would be nice to use the above. Could even be:

#ifdef CONFIG_X86_64
#define __read_eflags __builtin_ia32_readeflags_u64 #define __write_eflags __builtin_i32_writeeflags_u64 #else
#define __read_eflags __builtin_ia32_readeflags_u32 #define __write_eflags __builtin_i32_writeeflags_u32 #endif

Which would be concise. For smap_save() we can use clac() and stac() which might be nicer than asm goto (kudos for using asm goto, but plz no more). :^P Also, we can probably cleanup the note about GCC < 4.9 now. :)

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Why can't clang use the inline asm version?

Quoted 8 lines
Quoted 2 lines

Clang chooses the most general constraint when multiple constraints are specified. So it will use the stack when retrieving the flags.

I haven't looked at clang's generated code to see if it's sensible from an optimization perspective, but surely the kernel code right now is genuinely wrong. GCC is free to omit the frame pointer and generate a stack-relative access for the pop, in which case it will malfunction.

Sorry, this is another case I don't precisely follow, would you mind explaining it further to me please?

Bill, it looks like Clang is forcibly emitting a frame pointer for
these: https://godbolt.org/z/GPMeKc, is that right/needed? I thought -O2 implied -fomit-frame-pointer, in fact I think I fixed that in Clang for x86 most recently...

Quoted 3 lines

IOW, we appear to have an actual bug, and clang just happens to be triggering it, no?

We've seen bugs in the past where "m" constraints were used with inline asm that modified the stack pointer (commit 428b8f1d9f92 "KVM:
VMX: don't allow memory operands for inline asm that modifies SP"), though I thought only leaving the stack imbalanced (not restoring it to its previous value before the end of the inline asm) was problematic. Though commit d0a8d9378d16 ("x86/paravirt: Make native_save_fl() extern inline") is what I'm reminded of with this patch. (Stack protectors continue to wind up in places that cause trouble. GCC folks have patches to disable them on a per function basis via function attribute like Clang does, though they've been sitting in code review for a long time. Recently, they've caused some headaches with LTO as well. Once we have those, I plan on adding that to compiler_attributes.h.) I would sleep better at night if I undid/reverted d0a8d9378d16.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#5 lore raw
Quoted 6 lines · Nick Desaulniers

(Bill, please use ./scripts/get_maintainer.pl <patchfile> to get the appropriate list of folks and mailing lists to CC)

Quoted 1 line · Andy Lutomirski
Quoted 8 lines
Quoted 5 lines

So with my selftests hat on, the inline asm utterly sucks. Trying to use pushfq / popfq violates the redzone and requires a gross hack to work around. It also messes up the DWARF annotations. The kernel gets a free pass on both of these because we don't use a redzone and we don't use DWARF.

Sorry, I don't fully understand:
1. What's the redzone?

Userspace ABI. x86_64 userspace is allowed to use 128 bytes below RSP.

Quoted 1 line

2. How does pushfq/popfq violate it?

It clobbers a stack slot owned by the compiler.

Quoted 2 lines

3. What is the "gross hack"/"workaround?" (So that we might consider removing it if these builtins help).

Look in tools/testing/selftests/x86/helpers.h. I have a patch to switch to the builtins.

Quoted 1 line

4. The kernel does use DWARF, based on configs, right?

Indeed, but user code in the kernel tree (e.g. selftests) does.

Quoted 7 lines

#ifdef CONFIG_X86_64
#define __read_eflags __builtin_ia32_readeflags_u64 #define __write_eflags __builtin_i32_writeeflags_u64 #else
#define __read_eflags __builtin_ia32_readeflags_u32 #define __write_eflags __builtin_i32_writeeflags_u32 #endif

Looks reasonable to me.

Quoted 5 lines

Which would be concise. For smap_save() we can use clac() and stac() which might be nicer than asm goto (kudos for using asm goto, but plz no more). :^P Also, we can probably cleanup the note about GCC < 4.9 now. :)

I find it a bit implausible that popfq is faster than a branch, so the smap_restore() code seems suboptimal. For smap_save(), I'm not sure what's ideal, but the current code seems fine other than the bogus constraint.

Quoted 11 lines
Quoted 8 lines
Quoted 2 lines

Clang chooses the most general constraint when multiple constraints are specified. So it will use the stack when retrieving the flags.

I haven't looked at clang's generated code to see if it's sensible from an optimization perspective, but surely the kernel code right now is genuinely wrong. GCC is free to omit the frame pointer and generate a stack-relative access for the pop, in which case it will malfunction.

Sorry, this is another case I don't precisely follow, would you mind explaining it further to me please?

The compiler is permitted (and expected!) to instantiate an m constraint as something like offset(%rsp). For example, this:

unsigned long bogus_read_flags(void)

{
        unsigned long ret;
        asm volatile ("pushfq\n\tpopq %0" : "=m" (ret));
        return ret;
}

compiles to:

        pushfq
        popq -8(%rsp)
        movq    -8(%rsp), %rax
        ret

which is straight-up wrong. Changing it to "=rm" gives:

pushfq
popq %rax
ret

which is correct, but this only works because gcc politely chose the r option instead of the m option. clang chooses poorly and gives:

read_flags:

        pushfq
        popq    -8(%rsp)
        movq    -8(%rsp), %rax
        retq

which is wrong. I filed a clang bug:

https://bugs.llvm.org/show_bug.cgi?id=47530

but the kernel is buggy here -- clang is within its rights to generate the bogus sequence above. Bill's email was IMO rather obfuscated, and I assume this is what he meant.

Of course, clang unhelpfully generates poor code for the builtin, too:

nice_read_eflags:
pushq %rbp
movq %rsp, %rbp
pushfq
popq %rax
popq %rbp
retq

I filed a bug for that, too:

https://bugs.llvm.org/show_bug.cgi?id=47531

So we at least need to fix the bogus "=rm", and we should seriously consider using the builtin.