Skip to contentSkip to conversation

arm64/cpufeatures: Enforce inline/const properties of cpus_have_const_cap

PATCH New Marc Zyngier 2 participants 4 messages Jan 18, 2017 – Jan 19, 2017 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64/cpufeatures: Enforce inline/const properties of cpus_have_const_cap

Despite being flagged "inline", cpus_have_const_cap may end-up being placed out of line if the compiler decides so. This would be unfortunate, as we want to be able to use this function in HYP, where we need to be 100% sure of what is mapped there. __always_inline seems to be a better choice given the constraint.

Also, be a lot tougher on non-const or out-of-range capability values (a non-const cap value shouldn't be used here, and the semantic of an OOR value is at best ill defined). In those two case, BUILD_BUG_ON is what you get.

---
 arch/arm64/include/asm/cpufeature.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/include/asm/cpufeature.h +4 -3
index b4989df..4710469 100644
@@ -105,10 +105,11 @@ static inline bool cpu_have_feature(unsigned int num)
 }
 
 /* System capability check for constant caps */
-static inline bool cpus_have_const_cap(int num)
+static __always_inline bool cpus_have_const_cap(int num)
 {
-	if (num >= ARM64_NCAPS)
-		return false;
+	BUILD_BUG_ON(!__builtin_constant_p(num));
+	BUILD_BUG_ON(num >= ARM64_NCAPS);
+
 	return static_branch_unlikely(&cpu_hwcap_keys[num]);
 }
 
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/1 arm64/cpufeatures: Enforce inline/const properties of cpus_have_const_cap

Quoted 22 lines · Marc Zyngier

Despite being flagged "inline", cpus_have_const_cap may end-up being placed out of line if the compiler decides so. This would be unfortunate, as we want to be able to use this function in HYP, where we need to be 100% sure of what is mapped there. __always_inline seems to be a better choice given the constraint.

Also, be a lot tougher on non-const or out-of-range capability values (a non-const cap value shouldn't be used here, and the semantic of an OOR value is at best ill defined). In those two case, BUILD_BUG_ON is what you get.

---
 arch/arm64/include/asm/cpufeature.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/include/asm/cpufeature.h +0 -0
index b4989df..4710469 100644

arch/arm64/include/asm/cpufeature.h static inline bool cpu_have_feature(unsigned int num) · line 111

Quoted 10 lines
arch/arm64/include/asm/cpufeature.h +3 -3
@@ -105,10 +105,11 @@ static inline bool cpu_have_feature(unsigned int num)
 }
 
 /* System capability check for constant caps */
-static inline bool cpus_have_const_cap(int num)
+static __always_inline bool cpus_have_const_cap(int num)
 {
-	if (num >= ARM64_NCAPS)
-		return false;
+	BUILD_BUG_ON(!__builtin_constant_p(num));
+	BUILD_BUG_ON(num >= ARM64_NCAPS);

This gives different behaviour to cpus_have_const_cap when compared to cpus_have_cap, which I really don't like. What is the current behaviour if you pass a non-constant num parameter? Does the kernel actually build?

Maybe it's best to spin a separate patch that makes cpus_have_cap and cpus_have_const_cap both use __always_inline, then we can debate the merit of the BUILD_BUG_ONs separately.

Will

#2 lore raw

PATCH 1/1 arm64/cpufeatures: Enforce inline/const properties of cpus_have_const_cap

Quoted 25 lines · Will Deacon
Quoted 24 lines · Marc Zyngier

Despite being flagged "inline", cpus_have_const_cap may end-up being placed out of line if the compiler decides so. This would be unfortunate, as we want to be able to use this function in HYP, where we need to be 100% sure of what is mapped there. __always_inline seems to be a better choice given the constraint.

Also, be a lot tougher on non-const or out-of-range capability values (a non-const cap value shouldn't be used here, and the semantic of an OOR value is at best ill defined). In those two case, BUILD_BUG_ON is what you get.

---
 arch/arm64/include/asm/cpufeature.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/include/asm/cpufeature.h +0 -0
index b4989df..4710469 100644
@@ -105,10 +105,11 @@ static inline bool cpu_have_feature(unsigned int num)
 }
 
Quoted 12 lines

arch/arm64/include/asm/cpufeature.h static inline bool cpu_have_feature(unsigned int num) · line 111

Quoted 8 lines
arch/arm64/include/asm/cpufeature.h +3 -3
 /* System capability check for constant caps */
-static inline bool cpus_have_const_cap(int num)
+static __always_inline bool cpus_have_const_cap(int num)
 {
-	if (num >= ARM64_NCAPS)
-		return false;
+	BUILD_BUG_ON(!__builtin_constant_p(num));
+	BUILD_BUG_ON(num >= ARM64_NCAPS);

This gives different behaviour to cpus_have_const_cap when compared to cpus_have_cap, which I really don't like. What is the current behaviour if you pass a non-constant num parameter? Does the kernel actually build?

If your toolchain doesn't support jump labels (gcc 4.8 for example), it will build. But my point here is that if you're using the _const version, it should to be an actual constant, within the range of existing capabilities. Otherwise, I don't really understand what the semantic of _const means here.

Quoted 3 lines

Maybe it's best to spin a separate patch that makes cpus_have_cap and cpus_have_const_cap both use __always_inline, then we can debate the merit of the BUILD_BUG_ONs separately.

Sure, will do.

Thanks,

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#3 lore raw

PATCH 1/1 arm64/cpufeatures: Enforce inline/const properties of cpus_have_const_cap

Quoted 39 lines · Marc Zyngier
Quoted 25 lines · Will Deacon
Quoted 24 lines · Marc Zyngier

Despite being flagged "inline", cpus_have_const_cap may end-up being placed out of line if the compiler decides so. This would be unfortunate, as we want to be able to use this function in HYP, where we need to be 100% sure of what is mapped there. __always_inline seems to be a better choice given the constraint.

Also, be a lot tougher on non-const or out-of-range capability values (a non-const cap value shouldn't be used here, and the semantic of an OOR value is at best ill defined). In those two case, BUILD_BUG_ON is what you get.

---
 arch/arm64/include/asm/cpufeature.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/arm64/include/asm/cpufeature.h +0 -0
index b4989df..4710469 100644
@@ -105,10 +105,11 @@ static inline bool cpu_have_feature(unsigned int num)
 }
 
Quoted 12 lines

arch/arm64/include/asm/cpufeature.h static inline bool cpu_have_feature(unsigned int num) · line 111

Quoted 8 lines
arch/arm64/include/asm/cpufeature.h +3 -3
 /* System capability check for constant caps */
-static inline bool cpus_have_const_cap(int num)
+static __always_inline bool cpus_have_const_cap(int num)
 {
-	if (num >= ARM64_NCAPS)
-		return false;
+	BUILD_BUG_ON(!__builtin_constant_p(num));
+	BUILD_BUG_ON(num >= ARM64_NCAPS);

This gives different behaviour to cpus_have_const_cap when compared to cpus_have_cap, which I really don't like. What is the current behaviour if you pass a non-constant num parameter? Does the kernel actually build?

Quoted 5 lines

If your toolchain doesn't support jump labels (gcc 4.8 for example), it will build. But my point here is that if you're using the _const version, it should to be an actual constant, within the range of existing capabilities. Otherwise, I don't really understand what the semantic of _const means here.

There are two things here:

1. GCC can make non-const values constant using a runtime conditional

2. If we treat out-of-range caps as a BUILD_BUG_ON, then we've got different behaviour with cpus_have_cap, which will return false.

So I don't think that the BUILD_BUG_ON(num >= ARM64_NCAPS) makes an awful lot of sense, whilst the other BUILD_BUG_ON seems more like a sanity check on jump labels. That might be justifiable if the build failure is more obvious than what we currently get, so it's mainly the range check that I object to.

Will