BUILD_BUG_ON(!__builtin_constant_p(feature)) breaks bcc trace tool
Hi,
We added:
BUILD_BUG_ON(!__builtin_constant_p(feature))
to cpu_has_feature() and mmu_has_feature() in order to catch usage issues (such as cpu_has_feature(cpu_has_feature(X)). Unfortunately LLVM isn't smart enough to resolve this, and it errors out.
I work around it in my clang/LLVM builds of the kernel, but I have just discovered that it causes a lot of issues for the bcc (eBPF) trace tool (which uses LLVM).
How should we work around this? Wrap the checks in !clang perhaps?
Anton
Hi,
Quoted 8 lines
We added:
BUILD_BUG_ON(!__builtin_constant_p(feature))
to
cpu_has_feature()andmmu_has_feature()in order to catch usage issues (such as cpu_has_feature(cpu_has_feature(X)). Unfortunately LLVM isn't smart enough to resolve this, and it errors out.
Quoted 5 lines
I work around it in my clang/LLVM builds of the kernel, but I have just discovered that it causes a lot of issues for the bcc (eBPF) trace tool (which uses LLVM).
How should we work around this? Wrap the checks in !clang perhaps?
Looks like it's a weakness in LLVM with inlining:
#include <assert.h>
#if 1
static inline void foo(unsigned long x)
{
assert(__builtin_constant_p(x));
}#else
#define foo(X) assert(__builtin_constant_p(X))
#endif
int main(void)
{
foo(1);
return 0;
}And there is an old bug on it:
https://llvm.org/bugs/show_bug.cgi?id=4898
Anton
Michael Ellerman
reviewerQuoted 27 lines · Anton Blanchard
Quoted 8 lines
We added:
BUILD_BUG_ON(!__builtin_constant_p(feature))
to
cpu_has_feature()andmmu_has_feature()in order to catch usage issues (such as cpu_has_feature(cpu_has_feature(X)). Unfortunately LLVM isn't smart enough to resolve this, and it errors out.Quoted 5 lines
I work around it in my clang/LLVM builds of the kernel, but I have just discovered that it causes a lot of issues for the bcc (eBPF) trace tool (which uses LLVM).
How should we work around this? Wrap the checks in !clang perhaps?
Looks like it's a weakness in LLVM with inlining:
#include <
assert.h>#if 1
static inline void foo(unsigned long x){ assert(__builtin_constant_p(x)); }#else
#define foo(X) assert(__builtin_constant_p(X))
#endif
Quoted 6 lines
int main(void)
{ foo(1); return 0; }
OK. cpu_has_feature() is __always_inline:
static __always_inline bool cpu_has_feature(unsigned long feature)
{
int i;
BUILD_BUG_ON(!__builtin_constant_p(feature));But I guess even that doesn't work?
Hmm, in fact it seems because we don't define CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING and CONFIG_OPTIMIZE_INLINING, we get:
#define inline inline __attribute__((always_inline)) notrace
So in fact every inline function is marked always_inline all the time, which seems dubious.
But still, it seems clang is ignoring always_inline.
cheers
Quoted 10 lines · Michael Ellerman
Hmm, in fact it seems because we don't define
CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLININGandCONFIG_OPTIMIZE_INLINING, we get:#define inline inline __attribute__((always_inline)) notrace
So in fact every inline function is marked
always_inlineall the time, which seems dubious.But still, it seems clang is ignoring
always_inline.
I think the problem is that __builtin_constant_p() is usually false on clang, at least for arguments
that are not constant pointers.
Arnd
From: Michael Ellerman
Quoted 22 lines
Sent: 24 January 2017 06:16
Quoted 20 lines · Anton Blanchard
Quoted 8 lines
We added:
BUILD_BUG_ON(!__builtin_constant_p(feature))
to
cpu_has_feature()andmmu_has_feature()in order to catch usage issues (such as cpu_has_feature(cpu_has_feature(X)). Unfortunately LLVM isn't smart enough to resolve this, and it errors out.Quoted 5 lines
I work around it in my clang/LLVM builds of the kernel, but I have just discovered that it causes a lot of issues for the bcc (eBPF) trace tool (which uses LLVM).
How should we work around this? Wrap the checks in !clang perhaps?
Looks like it's a weakness in LLVM with inlining:
#include <
assert.h>#if 1
static inline void foo(unsigned long x)
Does making this 'const unsigned long x' help?
ISTR that making a slight difference to the instruction patterns gcc would use.
Quoted 6 lines
Quoted 6 lines
{ assert(__builtin_constant_p(x)); }#else
#define foo(X) assert(__builtin_constant_p(X))
#endif
...
Quoted 4 lines
#define inline inline __attribute__((always_inline)) notrace
=20
So in fact every inline function is markedalways_inlineall the time, which seems dubious.
I've had to do that in the past to get gcc to inline some small leaf functi= ons
that were only called once.
(That was for some embedded code where I don't actually want any function c= alls
at all.)
To my mind 'inline' should mean 'always_inline' since you need to explicitl= y
stop functions being inlined even when not marked 'inline'.
David
Quoted 7 lines · David Laight
From: Michael Ellerman
Quoted 4 lines
#define inline inline __attribute__((always_inline)) notrace
So in fact every inline function is marked
always_inlineall the time, which seems dubious.
Quoted 7 lines
I've had to do that in the past to get gcc to inline some small leaf functions that were only called once.
(That was for some embedded code where I don't actually want any function calls at all.)To my mind 'inline' should mean '
always_inline' since you need to explicitly stop functions being inlined even when not marked 'inline'.
On x86, this is configurable using OPTIMIZE_INLINING, but IIRC no other architecture
supports it in mainline Linux. I have played a bit with enabling it on ARM, which
showed a couple of build warnings and errors from the changed inlining, but they were all fixable. I have not submitted that since I have not actually been able to do extensive runtime testing on it.
It may turn out to be worthwhile for powerpc, where you have a much more limited set of configurations you care about and you already do some regular testing. Potentially this is more efficient than the current default, and any function that
actually requires being forced inline can be annotated as __always_inline rather than plain inline.
Arnd
Michael Ellerman
reviewerQuoted 11 lines · Anton Blanchard
We added:
BUILD_BUG_ON(!__builtin_constant_p(feature))
to
cpu_has_feature()andmmu_has_feature()in order to catch usage issues (such as cpu_has_feature(cpu_has_feature(X)). Unfortunately LLVM isn't smart enough to resolve this, and it errors out.I work around it in my clang/LLVM builds of the kernel, but I have just discovered that it causes a lot of issues for the bcc (eBPF) trace tool (which uses LLVM).
I didn't understand that part, but Aneesh explained to me that it's because bcc pulls in the kernel-internal headers.
I guess as a quick fix we just have to #ifdef it, can you confirm this works?
arch/powerpc/include/asm/cpu_has_feature.h +2 -0
@@ -23,7 +23,9 @@ static __always_inline bool cpu_has_feature(unsigned long feature) {int i; +#ifndef __clang__/* clang can't cope with this */ BUILD_BUG_ON (!__builtin_constant_p (feature)); +#endif #ifdef CONFIG_JUMP_LABEL_FEATURE_CHECK_DEBUG if (!static_key_initialized) {
arch/powerpc/include/asm/mmu.h +2 -0
@@ -160,7 +160,9 @@ static __always_inline bool mmu_has_feature(unsigned long feature) {int i; +#ifndef __clang__/* clang can't cope with this */ BUILD_BUG_ON (!__builtin_constant_p (feature)); +#endif #ifdef CONFIG_JUMP_LABEL_FEATURE_CHECK_DEBUG if (!static_key_initialized) {
cheers
Naveen N. Rao
Tested-byQuoted 13 lines · Michael Ellerman
Quoted 11 lines · Anton Blanchard
We added:
BUILD_BUG_ON(!__builtin_constant_p(feature))
to
cpu_has_feature()andmmu_has_feature()in order to catch usage issues (such as cpu_has_feature(cpu_has_feature(X)). Unfortunately LLVM isn't smart enough to resolve this, and it errors out.I work around it in my clang/LLVM builds of the kernel, but I have just discovered that it causes a lot of issues for the bcc (eBPF) trace tool (which uses LLVM).
Quoted 5 lines
I didn't understand that part, but Aneesh explained to me that it's because bcc pulls in the kernel-internal headers.
I guess as a quick fix we just have to #ifdef it, can you confirm this works?
This works and solves the issue for me. In the absence of a better approach, can you please push this for v4.10?
Thanks,
Naveen
Quoted 34 lines
arch/powerpc/include/asm/cpu_has_feature.h +2
-0@@ -23,7 +23,9 @@ static __always_inline bool cpu_has_feature(unsigned long feature) {int i; +#ifndef __clang__/* clang can't cope with this */ BUILD_BUG_ON (!__builtin_constant_p (feature)); +#endif #ifdef CONFIG_JUMP_LABEL_FEATURE_CHECK_DEBUG if (!static_key_initialized) {arch/powerpc/include/asm/mmu.h +2
-0@@ -160,7 +160,9 @@ static __always_inline bool mmu_has_feature(unsigned long feature) {int i; +#ifndef __clang__/* clang can't cope with this */ BUILD_BUG_ON (!__builtin_constant_p (feature)); +#endif #ifdef CONFIG_JUMP_LABEL_FEATURE_CHECK_DEBUG if (!static_key_initialized) {cheers