x86/cpufeature: bpf hack for clang not supporting asm goto
Subsystems (4)
PATCH v3 1/1 x86/cpufeature: bpf hack for clang not supporting asm goto
Commit d0266046ad54 ("x86: Remove FAST_FEATURE_TESTS") removed X86_FAST_FEATURE_TESTS and make macro static_cpu_has() always use __always_inline function _static_cpu_has() funciton. The static_cpu_has() uses gcc feature asm goto construct, which is not supported by clang.
Issues
======
Currently, for BPF programs written in C, the only widely-supported compiler is clang. Because of clang lacking support
of asm goto construct, if you try to compile
bpf programs under samples/bpf/,
$ make -j20 && make headers_install && make samples/bpf/ you will see a lot of failures like below:
clang -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include \
-I/home/yhs/work/bpf-next/arch/x86/include \
-I./arch/x86/include/generated -I/home/yhs/work/bpf-next/include \
-I./include -I/home/yhs/work/bpf-next/arch/x86/include/uapi \
-I./arch/x86/include/generated/uapi \
-I/home/yhs/work/bpf-next/include/uapi -I./include/generated/uapi \
-include /home/yhs/work/bpf-next/include/linux/kconfig.h
-Isamples/bpf \
-I/home/yhs/work/bpf-next/tools/testing/selftests/bpf/ \
-D__KERNEL__ -Wno-unused-value -Wno-pointer-sign \
-D__TARGET_ARCH_x86 -Wno-compare-distinct-pointer-types \
-Wno-gnu-variable-sized-type-not-at-end \
-Wno-address-of-packed-member -Wno-tautological-compare \
-Wno-unknown-warning-option \
-O2 -emit-llvm -c /home/yhs/work/bpf-next/samples/bpf/xdp_redirect_cpu_kern.c \
-o -| llc -march=bpf -filetype=obj -o samples/bpf/xdp_redirect_cpu_kern.o
In file included from /home/yhs/work/bpf-next/samples/bpf/xdp_redirect_cpu_kern.c:10:
In file included from /home/yhs/work/bpf-next/include/uapi/linux/in.h:24:
In file included from /home/yhs/work/bpf-next/include/linux/socket.h:8:
In file included from /home/yhs/work/bpf-next/include/linux/uio.h:13:
In file included from /home/yhs/work/bpf-next/include/linux/thread_info.h:38:
In file included from /home/yhs/work/bpf-next/arch/x86/include/asm/thread_info.h:53:
/home/yhs/work/bpf-next/arch/x86/include/asm/cpufeature.h:150:2: error: 'asm goto' constructs are not supported yet
asm_volatile_goto("1: jmp 6f\n"
^
/home/yhs/work/bpf-next/include/linux/compiler-gcc.h:296:42: note: expanded from macro 'asm_volatile_goto'
#define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
^ 1 error generated.
...
In file included from /home/yhs/work/bpf-next/samples/bpf/tracex4_kern.c:7:
In file included from /home/yhs/work/bpf-next/include/linux/ptrace.h:6:
In file included from /home/yhs/work/bpf-next/include/linux/sched.h:14:
In file included from /home/yhs/work/bpf-next/include/linux/pid.h:5:
In file included from /home/yhs/work/bpf-next/include/linux/rculist.h:11:
In file included from /home/yhs/work/bpf-next/include/linux/rcupdate.h:40:
In file included from /home/yhs/work/bpf-next/include/linux/preempt.h:81:
In file included from /home/yhs/work/bpf-next/arch/x86/include/asm/preempt.h:7:
In file included from /home/yhs/work/bpf-next/include/linux/thread_info.h:38:
In file included from /home/yhs/work/bpf-next/arch/x86/include/asm/thread_info.h:53:
/home/yhs/work/bpf-next/arch/x86/include/asm/cpufeature.h:150:2: error: 'asm goto' constructs are not supported yet
...
In the above, bpf compilation looks like below
clang -target <native_arch> ... -emit-llvm -c -o - | llc -march=bpf ...
The clang compilation targets the native architecture and generates the LLVM IR bytecode, and the llc compilation takes the IR bytecode and generates bpf instructions.
If kernel header files accessed by bpf program have asm goto construct, e.g., arch/x86/include/asm/cpufeature.h, the "clang -target <native_arch> ..." compilation will fail as clang does not support asm goto yet.
Solution
=========
The solution proposed in this patch does not need user space change. When the kernel detects the compiler has asm goto support,
it sets CC_HAVE_ASM_GOTO, the patch added the additional marco definition __NO_CLANG_BPF_HACK. In arch/x86/include/asm/cpufeature.h, the asm goto construct is accessed only if __NO_CLANG_BPF_HACK is defined. This should not impact kernel compilation functionality since for x86, we have
ifndef CC_HAVE_ASM_GOTO
$(error Compiler lacks asm-goto support.)
endif
So __NO_CLANG_BPF_HACK should be always defined during kernel compilation targeting x86.
User space code which compiles bpf program does not have __NO_CLANG_BPF_HACK defined so it will go to alternate path which avoids asm goto.
This approach is preferred since the already deployed bcc scripts, or any other bpf applicaitons utilizing LLVM JIT compilation functionality, will continue work with the new kernel without re-compilation and re-deployment.
Note that this is a hack in the kernel to workaround bpf compilation issue. The hack will be removed once clang starts to support asm goto.
--- Makefile | 1 + arch/x86/include/asm/cpufeature.h | 5 +++++ 2 files changed, 6 insertions(+)
Changelog:
v2 -> v3:
. Changed macro name from NO_BPF_WORKAROUND to __NO_CLANG_BPF_HACK.
Explained the solution in more details.
v1 -> v2:
. Use NO_BPF_WORKAROUND macro instead of CC_HAVE_ASM_GOTO
to make it explicit that this is a workaround.
Peter,
Could you take a look at this patch revision and give your opinion? More and more people hit this issue and have to manually work around it. It would be good if this can be resolved soon.
Thanks!
Makefile +1 -0
@@ -504,6 +504,7 @@ export RETPOLINE_CFLAGS ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y) CC_HAVE_ASM_GOTO := 1 KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO + KBUILD_CFLAGS += -D__NO_CLANG_BPF_HACK KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO endif
arch/x86/include/asm/cpufeature.h +5 -0
@@ -140,6 +140,8 @@ extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);#define setup_force_cpu_bug (bit) setup_force_cpu_cap(bit) +/* this macro is a temporary hack for bpf until clang gains asm-goto support */ +#ifdef __NO_CLANG_BPF_HACK/* * Static testing of CPU features. Used the same as boot_cpu_has(). * These will statically patch the target code for additional
@@ -195,6 +197,9 @@ static __always_inline __pure bool _static_cpu_has(u16 bit) boot_cpu_has(bit) : \ _static_cpu_has(bit) \ ) +#else +#define static_cpu_has (bit) boot_cpu_has(bit) +#endif #define cpu_has_bug (c, bit) cpu_has(c, (bit))#define set_cpu_bug (c, bit) set_cpu_cap(c, (bit))
Signature · 1 line
2.9.5
Quoted 4 lines · Yonghong Song
This approach is preferred since the already deployed bcc scripts, or any other bpf applicaitons utilizing LLVM JIT compilation functionality, will continue work with the new kernel without re-compilation and re-deployment.
So I really hate this and would much rather see the BPF build environment changed. It not consistenyly having __BPF__ defined really smells like a bug on your end.
Sometimes you just need to update tools... Is it really too hard to do -D__BPF__ in the bpf build process that we need to mollest the kernel for it?
Quoted 2 lines
Note that this is a hack in the kernel to workaround bpf compilation issue. The hack will be removed once clang starts to support asm goto.
Note that that ^^ already mandates people re-deploy their bpf tools, so why is llvm supporting asm-goto a better point to re-deploy than fixing a consistent __BPF__ define for the bpf build environment?
Makefile export RETPOLINE_CFLAGS · line 509
Quoted 11 lines
Makefile +1
-0@@ -504,6 +504,7 @@ export RETPOLINE_CFLAGS ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y) CC_HAVE_ASM_GOTO := 1 KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO + KBUILD_CFLAGS += -D__NO_CLANG_BPF_HACK KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO endif
I really think this is the wrong thing to do; but if the x86 maintainers are willing to take this, I'll grudingly shut up.
Ingo, Thomas?
Quoted 26 lines
arch/x86/include/asm/cpufeature.h +5
-0@@ -140,6 +140,8 @@ extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);#define setup_force_cpu_bug (bit) setup_force_cpu_cap(bit) +/* this macro is a temporary hack for bpf until clang gains asm-goto support */ +#ifdef __NO_CLANG_BPF_HACK/* * Static testing of CPU features. Used the same as boot_cpu_has(). * These will statically patch the target code for additional @@ -195,6 +197,9 @@ static __always_inline __pure bool _static_cpu_has(u16 bit) boot_cpu_has(bit) : \ _static_cpu_has(bit) \ ) +#else +#define static_cpu_has (bit) boot_cpu_has(bit) +#endif #define cpu_has_bug (c, bit) cpu_has(c, (bit))#define set_cpu_bug (c, bit) set_cpu_cap(c, (bit))Signature · 1 line
2.9.5
Quoted 15 lines · Peter Zijlstra
Quoted 4 lines · Yonghong Song
This approach is preferred since the already deployed bcc scripts, or any other bpf applicaitons utilizing LLVM JIT compilation functionality, will continue work with the new kernel without re-compilation and re-deployment.
So I really hate this and would much rather see the BPF build environment changed. It not consistenyly having
__BPF__defined really smells like a bug on your end.Sometimes you just need to update tools... Is it really too hard to do -D__BPF__ in the bpf build process that we need to mollest the kernel for it?
Quoted 6 lines
Quoted 2 lines
Note that this is a hack in the kernel to workaround bpf compilation issue. The hack will be removed once clang starts to support asm goto.
Note that that ^^ already mandates people re-deploy their bpf tools, so why is llvm supporting asm-goto a better point to re-deploy than fixing a consistent
__BPF__define for the bpf build environment?
This thread has been going for over a month now. Looks like we're starting to go in circles and what we discussed earlier got lost. To recap:
- this is NOT a bpf issue. libbcc is the first user that got broken by commit d0266046ad54 ("x86: Remove FAST_FEATURE_TESTS")
- all other libraries and tools (like fuzzers, static code analyzers, etc) that are using clang front-end to process kernel headers are broken too
That makes me wonder what happened with "we do not break user space" rule?
I think the safest course of action would be to partially revert the offending commit d0266046ad54 which we proposed first.
Since you were concerned that this is somehow will disincentivize clang folks to add asm-goto support, we agreed to add this __NO_CLANG_BPF_HACK macro to un-break libbcc at least, so that existing libbcc installations will continue to work when people upgrade kernels.
The __BPF__ macro is automatically defined by clang when '-target bpf' is used. This is NOT the case here. libbcc has to use native target when processing kernel headers.
It happens from time to time that one or the other libbcc script gets broken by new kernel because kprobe symbol that the script is using got changed in the recent kernel. That's ok and we deal with this situation all the time. What's different this time that ALL bcc scripts are broken because of this commit and we cannot workaround in user space.
Borislav Petkov
maintainerQuoted 1 line · Alexei Starovoitov
That makes me wonder what happened with "we do not break user space" rule?
As someone already pointed out on IRC, arch/x86/include/asm/cpufeature.h is solely a kernel header so nothing but kernel should include it. So forget the userspace breakage "argument".
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Quoted 3 lines · Borislav Petkov
As someone already pointed out on IRC,
arch/x86/include/asm/cpufeature.his solely a kernel header so nothing but kernel should include it. So forget the userspace breakage "argument".
For what is worth, I have the same exact problem in a relatively popular open source system call tracer, and my attempt to fix the issue from user space has been:
https://github.com/draios/sysdig/commit/2958eb1d52e047f4b93d1238be803e7c405bdec2
While I can definitely live with that (and I'd be happy to submit a patch to samples/ following the same approach) and absolutely respect the technical authority of the reviewers here, it would be nice to recognize that these changes actually affect users to a certain degree, even if from a technical point of view they don't break userspace.
From a practical point of view, BPF is widely used from userspace programs
to access some kernel data structures to gather visibility information, and even the simplest use case, such as including linux/sched.h to access some task_struct members, ends up pulling in arch/x86/include/asm/cpufeature.h, thus (ab)using that file. Adding these quirks definitely increases the complexity a developer needs to keep in mind in order to take advantage of a BPF based instrumentation.
Thanks
Quoted 6 lines · Borislav Petkov
Quoted 1 line · Alexei Starovoitov
That makes me wonder what happened with "we do not break user space" rule?
As someone already pointed out on IRC,
arch/x86/include/asm/cpufeature.his solely a kernel header so nothing but kernel should include it. So forget the userspace breakage "argument".
libbcc is a library. It's not only used by bcc scripts, but by production services that compile bpf programs with clang.
Without this kernel fix the companies won't be able to upgrade the kernel. Even if we could somehow hack libbcc and workaround in user space (which is not possible as already explained), it's a long dependency chain to recompile and upgrade core services before upgrading the kernel which makes even theoretical user space fix impractical.
I see no option, but to fix the kernel.
Regardless whether it's called user space breakage or kernel breakage.
Borislav Petkov
maintainerQuoted 2 lines · Alexei Starovoitov
libbcc is a library. It's not only used by bcc scripts, but by production services that compile bpf programs with clang.
Let me get this straight: libbcc fails to compile because it includes (through some long include chain) the kernel header arch/x86/include/asm/cpufeature.h.
Is that it?
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.Borislav Petkov
maintainerQuoted 32 lines · Yonghong Song
Commit
d0266046ad54("x86: RemoveFAST_FEATURE_TESTS") removedX86_FAST_FEATURE_TESTSand make macrostatic_cpu_has()always use__always_inlinefunction_static_cpu_has()funciton. Thestatic_cpu_has()uses gcc feature asm goto construct, which is not supported by clang.Issues
======Currently, for BPF programs written in C, the only widely-supported compiler is clang. Because of clang lacking support
of asm goto construct, if you try to compile
bpf programs undersamples/bpf/,$ make -j20 && make headers_install && make samples/bpf/ you will see a lot of failures like below:clang -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/7/include \ -I/home/yhs/work/bpf-next/arch/x86/include \ -I./arch/x86/include/generated -I/home/yhs/work/bpf-next/include \ -I./include -I/home/yhs/work/bpf-next/arch/x86/include/uapi \ -I./arch/x86/include/generated/uapi \ -I/home/yhs/work/bpf-next/include/uapi -I./include/generated/uapi \ -include /home/yhs/work/bpf-next/include/linux/kconfig.h -Isamples/bpf \ -I/home/yhs/work/bpf-next/tools/testing/selftests/bpf/ \ -D__KERNEL__ -Wno-unused-value -Wno-pointer-sign \ -D__TARGET_ARCH_x86 -Wno-compare-distinct-pointer-types \ -Wno-gnu-variable-sized-type-not-at-end \ -Wno-address-of-packed-member -Wno-tautological-compare \ -Wno-unknown-warning-option \ -O2 -emit-llvm -c /home/yhs/work/bpf-next/samples/bpf/xdp_redirect_cpu_kern.c \ -o -| llc -march=bpf -filetype=obj -o samples/bpf/xdp_redirect_cpu_kern.o
Quoted 1 line
In file included from
/home/yhs/work/bpf-next/samples/bpf/xdp_redirect_cpu_kern.c:10:
This is not the kernel's problem.
Fix the samples to build successfully like the rest of the things in tools/ use kernel machinery and undefine/redefine stuff so that it builds. For example, tools/include/asm/alternative-asm.h redefines two macros. So do something along those lines.
Signature · 4 lines
Regards/Gruss,
Boris.
Good mailing practices for 400: avoid top-posting and trim the reply.