Skip to contentSkip to conversation

[5.15] Compilation error in arch/x86/kvm/mmu/spte.h with clang-14

BUG [email protected] 7 participants 13 messages Oct 4, 2021 – Oct 15, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

I encounter the following issue when compiling 5.15-rc4 with clang-14:

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

        return __is_bad_mt_xwr(rsvd_check, spte) |
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                 ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

(no issue with gcc-11)

Tor

maintainer
#1 lore raw
Quoted 8 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

         return __is_bad_mt_xwr(rsvd_check, spte) |
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                  ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

The warning is wrong, as mentioned in the line right above:

/*
* Use a bitwise-OR instead of a logical-OR to aggregate the reserved
* bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
* (this is extremely unlikely to be short-circuited as true). */

Paolo

#2 lore raw
Quoted 7 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:26 geschrieben:

Quoted 2 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

Quoted 8 lines
Quoted 6 lines

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

         return __is_bad_mt_xwr(rsvd_check, spte) |
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                  ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

The warning is wrong, as mentioned in the line right above:

So it's an issue with clang-14 then?
(I add Nick and Nathan)

Quoted 8 lines

/*
* Use a bitwise-OR instead of a logical-OR to aggregate the reserved
* bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
* (this is extremely unlikely to be short-circuited as true). */

Paolo

maintainer
#3 lore raw
Quoted 14 lines · On 04/10/21 11:30, [email protected] wrote:
Quoted 14 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:26 geschrieben:

Quoted 8 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

          return __is_bad_mt_xwr(rsvd_check, spte) |
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                   ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

Quoted 4 lines
Quoted 1 line

The warning is wrong, as mentioned in the line right above:

So it's an issue with clang-14 then?
(I add Nick and Nathan)

My clang here doesn't have the option, so I'm going to ask---are you using W=1? I can see why clang is warning for KVM's code, but in my opinion such a check should only be in -Wextra.

Paolo

Quoted 9 lines
Quoted 8 lines

/*
* Use a bitwise-OR instead of a logical-OR to aggregate the reserved
* bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
* (this is extremely unlikely to be short-circuited as true). */

Paolo

#4 lore raw
Quoted 19 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:49 geschrieben:

Quoted 14 lines · On 04/10/21 11:30, [email protected] wrote:
Quoted 14 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:26 geschrieben:

Quoted 8 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

          return __is_bad_mt_xwr(rsvd_check, spte) |
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                   ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

The warning is wrong, as mentioned in the line right above:

So it's an issue with clang-14 then?
(I add Nick and Nathan)

My clang here doesn't have the option, so I'm going to ask---are you using W=1? I can see why clang is warning for KVM's code, but in my opinion such a check should only be in -Wextra.

I don't use any options (not that I'm aware of). Clang version 14.0.0 5f7a5353301b776ffb0e5fb048992898507bf7ee

Quoted 12 lines

Paolo

Quoted 9 lines
Quoted 8 lines

/*
* Use a bitwise-OR instead of a logical-OR to aggregate the reserved
* bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
* (this is extremely unlikely to be short-circuited as true). */

Paolo

#5 lore raw
Quoted 27 lines

[email protected] hat am 04.10.2021 12:10 geschrieben:

Quoted 23 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:49 geschrieben:

Quoted 18 lines · On 04/10/21 11:30, [email protected] wrote:
Quoted 7 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:26 geschrieben:

Quoted 2 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

Quoted 8 lines
Quoted 6 lines

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

          return __is_bad_mt_xwr(rsvd_check, spte) |
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                   ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

The warning is wrong, as mentioned in the line right above:

So it's an issue with clang-14 then?
(I add Nick and Nathan)

Quoted 6 lines
Quoted 3 lines

My clang here doesn't have the option, so I'm going to ask---are you using W=1? I can see why clang is warning for KVM's code, but in my opinion such a check should only be in -Wextra.

I don't use any options (not that I'm aware of). Clang version 14.0.0 5f7a5353301b776ffb0e5fb048992898507bf7ee

Probably the cause for this bug is this recent llvm commit:
https://github.com/llvm/llvm-project/commit/f59cc9542bfb461d16ad12b2cc4be4abbfd9d96e

Quoted 13 lines
Quoted 13 lines

Paolo

Quoted 9 lines
Quoted 8 lines

/*
* Use a bitwise-OR instead of a logical-OR to aggregate the reserved
* bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
* (this is extremely unlikely to be short-circuited as true). */

Paolo

#6 lore raw
In reply to Paolo Bonzini
Quoted 16 lines · Paolo Bonzini
Quoted 14 lines · On 04/10/21 11:30, [email protected] wrote:
Quoted 14 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:26 geschrieben:

Quoted 8 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

          return __is_bad_mt_xwr(rsvd_check, spte) |
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                   ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

The warning is wrong, as mentioned in the line right above:

So it's an issue with clang-14 then?
(I add Nick and Nathan)

My clang here doesn't have the option, so I'm going to ask---are you using W=1? I can see why clang is warning for KVM's code, but in my opinion such a check should only be in -Wextra.

This is a newly added warning in top of tree clang.

Quoted 13 lines

Paolo

Quoted 9 lines
Quoted 8 lines

/*
* Use a bitwise-OR instead of a logical-OR to aggregate the reserved
* bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
* (this is extremely unlikely to be short-circuited as true). */

Paolo

Signature · 2 lines
Thanks,
~Nick Desaulniers
#7 lore raw
Quoted 11 lines · Nick Desaulniers
Quoted 9 lines · Paolo Bonzini
Quoted 7 lines · On 04/10/21 11:30, [email protected] wrote:
Quoted 7 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:26 geschrieben:

Quoted 2 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

          return __is_bad_mt_xwr(rsvd_check, spte) |
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                   ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

The warning is wrong, as mentioned in the line right above:

Casting the bool to an int doesn't seem that onerous.

Quoted 27 lines
Quoted 6 lines
Quoted 2 lines

So it's an issue with clang-14 then?
(I add Nick and Nathan)

My clang here doesn't have the option, so I'm going to ask---are you using W=1? I can see why clang is warning for KVM's code, but in my opinion such a check should only be in -Wextra.

This is a newly added warning in top of tree clang.

Quoted 13 lines

Paolo

Quoted 9 lines
Quoted 8 lines

/*
* Use a bitwise-OR instead of a logical-OR to aggregate the reserved
* bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
* (this is extremely unlikely to be short-circuited as true). */

Paolo

--
Thanks,
~Nick Desaulniers

#8 lore raw
Quoted 19 lines · Jim Mattson
Quoted 18 lines · Nick Desaulniers
Quoted 16 lines · Paolo Bonzini
Quoted 14 lines · On 04/10/21 11:30, [email protected] wrote:
Quoted 14 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:26 geschrieben:

Quoted 8 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

          return __is_bad_mt_xwr(rsvd_check, spte) |
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                   ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

The warning is wrong, as mentioned in the line right above:

Casting the bool to an int doesn't seem that onerous.

Alternatively, could we just change both of the functions to return u64? I understand that they are being used in boolean contexts only but it seems like this would make it clear that a boolean or bitwise operator on them is acceptable.

arch/x86/kvm/mmu/spte.h +4 -4
index eb7b227fc6cf..0ca215bfe3a3 100644
@@ -295,14 +295,14 @@ static inline u64 get_rsvd_bits(struct rsvd_bits_validate *rsvd_check, u64 pte,
 	return rsvd_check->rsvd_bits_mask[bit7][level-1];
 }
 
-static inline bool __is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check,
-				      u64 pte, int level)
+static inline u64 __is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check,
+				     u64 pte, int level)
 {
 	return pte & get_rsvd_bits(rsvd_check, pte, level);
 }
 
-static inline bool __is_bad_mt_xwr(struct rsvd_bits_validate *rsvd_check,
-				   u64 pte)
+static inline u64 __is_bad_mt_xwr(struct rsvd_bits_validate *rsvd_check,
+				  u64 pte)
 {
 	return rsvd_check->bad_mt_xwr & BIT_ULL(pte & 0x3f);
 }
Quoted 28 lines
Quoted 27 lines
Quoted 6 lines
Quoted 2 lines

So it's an issue with clang-14 then?
(I add Nick and Nathan)

My clang here doesn't have the option, so I'm going to ask---are you using W=1? I can see why clang is warning for KVM's code, but in my opinion such a check should only be in -Wextra.

This is a newly added warning in top of tree clang.

Quoted 13 lines

Paolo

Quoted 9 lines
Quoted 8 lines

/*
* Use a bitwise-OR instead of a logical-OR to aggregate the reserved
* bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
* (this is extremely unlikely to be short-circuited as true). */

Paolo

--
Thanks,
~Nick Desaulniers

#9 lore raw
Quoted 20 lines · Nathan Chancellor
Quoted 19 lines · Jim Mattson
Quoted 18 lines · Nick Desaulniers
Quoted 16 lines · Paolo Bonzini
Quoted 14 lines · On 04/10/21 11:30, [email protected] wrote:
Quoted 14 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:26 geschrieben:

Quoted 8 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

          return __is_bad_mt_xwr(rsvd_check, spte) |
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                   ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

The warning is wrong, as mentioned in the line right above:

Casting the bool to an int doesn't seem that onerous.

Alternatively, could we just change both of the functions to return u64? I understand that they are being used in boolean contexts only but it seems like this would make it clear that a boolean or bitwise operator on them is acceptable.

If we want to fix this, my vote is for casting to an int and updating the comment in is_rsvd_spte(). I think I'd vote to fix this? IIRC KVM has had bitwise goofs in the past that manifested as real bugs, it would be nice to turn this on.

Or maybe add a macro to handle this? E.g.

arch/x86/kvm/mmu/spte.h +8 -2
index 7c0b09461349..38aeb4b21925 100644
@@ -307,6 +307,12 @@ static inline bool __is_bad_mt_xwr(struct rsvd_bits_validate *rsvd_check,
        return rsvd_check->bad_mt_xwr & BIT_ULL(pte & 0x3f);
 }
 
+/*
+ * Macro for intentional bitwise-OR of two booleans, which requires casting at
+ * least one of the results to an int to suppress -Wbitwise-instead-of-logical.
+ */
+#define BITWISE_BOOLEAN_OR(a, b) (!!((int)(a) | (int)(b)))
+
 static __always_inline bool is_rsvd_spte(struct rsvd_bits_validate *rsvd_check,
                                         u64 spte, int level)
 {
@@ -315,8 +321,8 @@ static __always_inline bool is_rsvd_spte(struct rsvd_bits_validate *rsvd_check,
         * bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
         * (this is extremely unlikely to be short-circuited as true).
         */
-       return __is_bad_mt_xwr(rsvd_check, spte) |
-              __is_rsvd_bits_set(rsvd_check, spte, level);
+       return BITWISE_BOOLEAN_OR(__is_bad_mt_xwr(rsvd_check, spte),
+                                 __is_rsvd_bits_set(rsvd_check, spte, level));
 }
 
 static inline bool spte_can_locklessly_be_made_writable(u64 spte)
#10 lore raw
Quoted 24 lines · Sean Christopherson
Quoted 22 lines · Nathan Chancellor
Quoted 21 lines · Jim Mattson
Quoted 19 lines · Nick Desaulniers
Quoted 17 lines · Paolo Bonzini
Quoted 15 lines · On 04/10/21 11:30, [email protected] wrote:
Quoted 15 lines

Paolo Bonzini <[email protected]> hat am 04.10.2021 11:26 geschrieben:

Quoted 8 lines · On 04/10/21 11:08, [email protected] wrote:

I encounter the following issue when compiling 5.15-rc4 with clang-14:

In file included from arch/x86/kvm/mmu/mmu.c:27:
arch/x86/kvm/mmu/spte.h:318:9: error: use of bitwise '|' with boolean operands [-Werror,-Wbitwise-instead-of-logical]

          return __is_bad_mt_xwr(rsvd_check, spte) |
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                   ||

arch/x86/kvm/mmu/spte.h:318:9: note: cast one or both operands to int to silence this warning

The warning is wrong, as mentioned in the line right above:

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Casting the bool to an int doesn't seem that onerous.

Alternatively, could we just change both of the functions to return u64? I understand that they are being used in boolean contexts only but it seems like this would make it clear that a boolean or bitwise operator on them is acceptable.

If we want to fix this, my vote is for casting to an int and updating the comment

At the least, I think bitwise operations should only be performed on unsigned types.

Quoted 4 lines

in is_rsvd_spte(). I think I'd vote to fix this? IIRC KVM has had bitwise goofs in the past that manifested as real bugs, it would be nice to turn this on.

Or maybe add a macro to handle this? E.g.

I think Nathan's suggestion was much cleaner. If explicit casts are enough to silence the warning, then I think Jim's suggestion is even better (though unsigned, not signed int).

Quoted 29 lines
arch/x86/kvm/mmu/spte.h +8 -2
index 7c0b09461349..38aeb4b21925 100644
@@ -307,6 +307,12 @@ static inline bool __is_bad_mt_xwr(struct rsvd_bits_validate *rsvd_check,
        return rsvd_check->bad_mt_xwr & BIT_ULL(pte & 0x3f);
 }
 
+/*
+ * Macro for intentional bitwise-OR of two booleans, which requires casting at
+ * least one of the results to an int to suppress -Wbitwise-instead-of-logical.
+ */
+#define BITWISE_BOOLEAN_OR(a, b) (!!((int)(a) | (int)(b)))
+
 static __always_inline bool is_rsvd_spte(struct rsvd_bits_validate *rsvd_check,
                                         u64 spte, int level)
 {
@@ -315,8 +321,8 @@ static __always_inline bool is_rsvd_spte(struct rsvd_bits_validate *rsvd_check,
         * bits and EPT's invalid memtype/XWR checks to avoid an extra Jcc
         * (this is extremely unlikely to be short-circuited as true).
         */
-       return __is_bad_mt_xwr(rsvd_check, spte) |
-              __is_rsvd_bits_set(rsvd_check, spte, level);
+       return BITWISE_BOOLEAN_OR(__is_bad_mt_xwr(rsvd_check, spte),
+                                 __is_rsvd_bits_set(rsvd_check, spte, level));
 }
 
 static inline bool spte_can_locklessly_be_made_writable(u64 spte)
Signature · 2 lines
Thanks,
~Nick Desaulniers
maintainer
#11 lore raw
Quoted 4 lines · Nick Desaulniers
Quoted 1 line

If we want to fix this, my vote is for casting to an int and updating the comment

At the least, I think bitwise operations should only be performed on unsigned types.

This is not a bitwise operation, it's a non-short-circuiting boolean operation. I'll apply Jim's suggestion.

Paolo

#12 lore raw
Quoted 10 lines · Paolo Bonzini
Quoted 3 lines · Nick Desaulniers
Quoted 1 line

If we want to fix this, my vote is for casting to an int and updating the comment

At the least, I think bitwise operations should only be performed on unsigned types.

This is not a bitwise operation, it's a non-short-circuiting boolean operation. I'll apply Jim's suggestion.

What about making it an inline function, which would require evaluation of arguments:

	static __always_inline bool BITWISE_BOOLEAN_OR(bool a, bool b)
	{
	    return a || b; // Safe here, because arguments have been evaluated
	}

Suggesting that because I'm always nervous about casts in macros hiding something that the type system would otherwise catch.

Christophe

Quoted 3 lines

Paolo