KVM: allow KVM_BUG/KVM_BUG_ON to handle 64-bit cond
Subsystems (2)
PATCH 1/1 KVM: allow KVM_BUG/KVM_BUG_ON to handle 64-bit cond
Current KVM_BUG and KVM_BUG_ON assumes that 'cond' passed from callers is 32-bit as it casts 'cond' to the type of int. This will be wrong if 'cond' provided by a caller is 64-bit, e.g. an error code of 0xc0000d0300000000 will be converted to 0, which is not expected. Improves the implementation by using !!(cond) in KVM_BUG and KVM_BUG_ON. Compared to changing 'int' to 'int64_t', this has less LOCs.
--- include/linux/kvm_host.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
include/linux/kvm_host.h +4 -8
@@ -881,20 +881,16 @@ static inline void kvm_vm_bugged(struct kvm *kvm) #define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ - \ - if (WARN_ONCE(__ret && !(kvm)->vm_bugged, fmt)) \ + if (WARN_ONCE(!!cond && !(kvm)->vm_bugged, fmt)) \ kvm_vm_bugged(kvm); \ - unlikely(__ret); \ + unlikely(!!cond); \ })#define KVM_BUG_ON (cond, kvm) \ ({ \ - int __ret = (cond); \ - \ - if (WARN_ON_ONCE(__ret && !(kvm)->vm_bugged)) \ + if (WARN_ON_ONCE(!!(cond) && !(kvm)->vm_bugged)) \ kvm_vm_bugged(kvm); \ - unlikely(__ret); \ + unlikely(!!(cond)); \ })static inline void kvm_vcpu_srcu_read_lock (struct kvm_vcpu *vcpu)
Signature · 1 line
2.27.0
Quoted 19 lines · Wei Wang
Current
KVM_BUGandKVM_BUG_ONassumes that 'cond' passed from callers is 32-bit as it casts 'cond' to the type of int. This will be wrong if 'cond' provided by a caller is 64-bit, e.g. an error code of0xc0000d0300000000will be converted to 0, which is not expected. Improves the implementation by using !!(cond) inKVM_BUGandKVM_BUG_ON. Compared to changing 'int' to 'int64_t', this has less LOCs.Fixes 0b8f11737cff ("KVM: Add infrastructure and macro to mark VM as bugged")Signed-off-by Wei Wang <[email protected]>--- include/linux/kvm_host.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)include/linux/kvm_host.h +0
-0
include/linux/kvm_host.h static inline void kvm_vm_bugged(struct kvm *kvm) · line 886
Quoted 10 lines
include/linux/kvm_host.h +2
-4@@ -881,20 +881,16 @@ static inline void kvm_vm_bugged(struct kvm *kvm) #define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ - \ - if (WARN_ONCE(__ret && !(kvm)->vm_bugged, fmt)) \ + if (WARN_ONCE(!!cond && !(kvm)->vm_bugged, fmt)) \ kvm_vm_bugged(kvm); \ - unlikely(__ret); \ + unlikely(!!cond); \
Do you want to use brackets for these two places as well, i.e.: !!(cond).
Quoted 15 lines
include/linux/kvm_host.h +2
-5})#define KVM_BUG_ON (cond, kvm) \ ({ \ - int __ret = (cond); \ - \ - if (WARN_ON_ONCE(__ret && !(kvm)->vm_bugged)) \ + if (WARN_ON_ONCE(!!(cond) && !(kvm)->vm_bugged)) \ kvm_vm_bugged(kvm); \ - unlikely(__ret); \ + unlikely(!!(cond)); \ })static inline void kvm_vcpu_srcu_read_lock (struct kvm_vcpu *vcpu) --
Quoted 2 lines
2.27.0
Thanks for catching this one.
-Mingwei
Quoted 7 lines · Wei Wang
Current
KVM_BUGandKVM_BUG_ONassumes that 'cond' passed from callers is 32-bit as it casts 'cond' to the type of int. This will be wrong if 'cond' provided by a caller is 64-bit, e.g. an error code of0xc0000d0300000000will be converted to 0, which is not expected. Improves the implementation by using !!(cond) inKVM_BUGandKVM_BUG_ON. Compared to changing 'int' to 'int64_t', this has less LOCs.
Less LOC is nice to have, but please preserve the behavior that "cond" is evaluated only once by KVM_BUG() and KVM_BUG_ON(). i.e. KVM_BUG_ON(do_something(), kvm) should only result in a single call to do_something().
Quoted 34 lines
Fixes 0b8f11737cff ("KVM: Add infrastructure and macro to mark VM as bugged")Signed-off-by Wei Wang <[email protected]>--- include/linux/kvm_host.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)include/linux/kvm_host.h +4
-8@@ -881,20 +881,16 @@ static inline void kvm_vm_bugged(struct kvm *kvm) #define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ - \ - if (WARN_ONCE(__ret && !(kvm)->vm_bugged, fmt)) \ + if (WARN_ONCE(!!cond && !(kvm)->vm_bugged, fmt)) \ kvm_vm_bugged(kvm); \ - unlikely(__ret); \ + unlikely(!!cond); \ })#define KVM_BUG_ON (cond, kvm) \ ({ \ - int __ret = (cond); \ - \ - if (WARN_ON_ONCE(__ret && !(kvm)->vm_bugged)) \ + if (WARN_ON_ONCE(!!(cond) && !(kvm)->vm_bugged)) \ kvm_vm_bugged(kvm); \ - unlikely(__ret); \ + unlikely(!!(cond)); \ })
Quoted 10 lines · David Matlack
Quoted 2 lines · Wei Wang
Current
KVM_BUGandKVM_BUG_ONassumes that 'cond' passed fromcallers
Quoted 5 lines
is 32-bit as it casts 'cond' to the type of int. This will be wrong if 'cond' provided by a caller is 64-bit, e.g. an error code of
0xc0000d0300000000will be converted to 0, which is not expected. Improves the implementation by using !!(cond) inKVM_BUGandKVM_BUG_ON. Compared to changing 'int' to 'int64_t', this has less LOCs.
Quoted 4 lines
Less LOC is nice to have, but please preserve the behavior that "cond" is evaluated only once by
KVM_BUG()andKVM_BUG_ON(). i.e.KVM_BUG_ON(do_something(), kvm)should only result in a single call todo_something().
Good point, thanks! Using 'typeof(cond)' looks like a better choice.
Quoted 11 lines · Wei W
Quoted 10 lines · David Matlack
Quoted 2 lines · Wei Wang
Current
KVM_BUGandKVM_BUG_ONassumes that 'cond' passed fromcallers
Quoted 5 lines
is 32-bit as it casts 'cond' to the type of int. This will be wrong if 'cond' provided by a caller is 64-bit, e.g. an error code of
0xc0000d0300000000will be converted to 0, which is not expected. Improves the implementation by using !!(cond) inKVM_BUGandKVM_BUG_ON. Compared to changing 'int' to 'int64_t', this has less LOCs.
Quoted 6 lines
Quoted 4 lines
Less LOC is nice to have, but please preserve the behavior that "cond" is evaluated only once by
KVM_BUG()andKVM_BUG_ON(). i.e.KVM_BUG_ON(do_something(), kvm)should only result in a single call todo_something().Good point, thanks! Using 'typeof(cond)' looks like a better choice.
I don't get it. Why bothering the type if we just do this?
include/linux/kvm_host.h +2 -2
@@ -848,7 +848,7 @@ static inline void kvm_vm_bugged(struct kvm *kvm) #define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ + int __ret = !!(cond); \ \ if (WARN_ONCE(__ret && !(kvm)->vm_bugged, fmt)) \ kvm_vm_bugged(kvm); \
@@ -857,7 +857,7 @@ static inline void kvm_vm_bugged(struct kvm *kvm)#define KVM_BUG_ON (cond, kvm) \ ({ \ - int __ret = (cond); \ + int __ret = !!(cond); \ \ if (WARN_ON_ONCE(__ret && !(kvm)->vm_bugged)) \ kvm_vm_bugged(kvm); \
include/linux/kvm_host.h static inline void kvm_vm_bugged(struct kvm *kvm) · line 851
Quoted 12 lines · Mingwei Zhang
I don't get it. Why bothering the type if we just do this?
include/linux/kvm_host.h index renamed from include/linux/kvm_host.h +0
-0
4f26b244f6d0..10455253c6ea100644include/linux/kvm_host.h +1
-1@@ -848,7 +848,7 @@ static inline void kvm_vm_bugged(struct kvm *kvm) #define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ + int __ret = !!(cond); \
This is essentially "bool __ret". No biggie to change it this way. But I'm inclined to retain the original intention to have the macro return the value that was passed in:
typeof(cond) __ret = (cond);
Let's what others vote for.
Quoted 10 lines · Wei W
Quoted 8 lines · Mingwei Zhang
I don't get it. Why bothering the type if we just do this?
include/linux/kvm_host.h index renamed from include/linux/kvm_host.h +0
-0
4f26b244f6d0..10455253c6ea100644include/linux/kvm_host.h +0
-0@@ -848,7 +848,7 @@ static inline void kvm_vm_bugged(struct kvm *kvm)
Quoted 6 lines
include/linux/kvm_host.h static inline void kvm_vm_bugged(struct kvm *kvm) · line 851
Quoted 4 lines
include/linux/kvm_host.h +1
-1#define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ + int __ret = !!(cond); \This is essentially "bool
__ret". No biggie to change it this way.
!! will return an int, not a boolean, but it is used as a boolean. This is consistent with the original code which _is_ returning an integer.
Quoted 3 lines
But I'm inclined to retain the original intention to have the macro return the value that was passed in:
typeof(cond)__ret= (cond);
hmm, I think it is appropriate to retain the original type of 'cond' especially since it may also involve other arithmetic operations. But I doubt it will be very useful. For instance, who is going to write this code?
......
if (KVM_BUG(cond, true) & some_mask)
do_something()
......
Quoted 2 lines
Let's what others vote for.
Please fix this bug first before introducing nice features.
Thanks.
-Mingwei
-Mingwei
Quoted 10 lines · Mingwei Zhang
Quoted 10 lines · Mingwei Zhang
Quoted 9 lines
I don't get it. Why bothering the type if we just do this?
include/linux/kvm_host.h +0
-0@@ -848,7 +848,7 @@ static inline void kvm_vm_bugged(struct kvm *kvm)
Quoted 8 lines
Quoted 6 lines
include/linux/kvm_host.h static inline void kvm_vm_bugged(struct kvm *kvm) · line 851
Quoted 4 lines
include/linux/kvm_host.h +1
-1#define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ + int __ret = !!(cond); \This is essentially "bool
__ret". No biggie to change it this way.!! will return an int, not a boolean, but it is used as a boolean.
What's the point of defining it as an int when actually being used as a Boolean? Original returning of an 'int' is a bug in this sense. Either returning a Boolean or the same type (length) as cond is good way to me.
Quoted 10 lines
This is consistent with the original code which _is_ returning an integer.
Quoted 3 lines
But I'm inclined to retain the original intention to have the macro return the value that was passed in:
typeof(cond)__ret= (cond);hmm, I think it is appropriate to retain the original type of 'cond' especially since it may also involve other arithmetic operations. But I doubt it will be very useful. For instance, who is going to write this code?
Maybe there is, maybe not. But it doesn’t hurt anything to leave the flexibility there using typeof(cond). As said, I'm also fine to use 'bool ret', but probably not 'int' for no good reason.
Quoted 19 lines · Wei W
Quoted 17 lines · Mingwei Zhang
Quoted 16 lines · Mingwei Zhang
include/linux/kvm_host.h static inline void kvm_vm_bugged(struct kvm · line 852
Quoted 13 lines
I don't get it. Why bothering the type if we just do this?
include/linux/kvm_host.h +1
-1@@ -848,7 +848,7 @@ static inline void kvm_vm_bugged(struct kvm *kvm) #define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ + int __ret = !!(cond); \This is essentially "bool
__ret". No biggie to change it this way.
Quoted 5 lines
Quoted 1 line
!! will return an int, not a boolean, but it is used as a boolean.
What's the point of defining it as an int when actually being used as a Boolean? Original returning of an 'int' is a bug in this sense. Either returning a Boolean or the same type (length) as cond is good way to me.
What's the point of using an integer? I think we need to ask the original author. But I think one of the reasons might be convenience as the return value. I am not sure if we can return a boolean in the function. But it should be fine here since it is a macro.
Anyway, returning an 'int' is not a bug. The bug is the casting from 'cond' to the integer that may lose information and this is what you have captured.
Quoted 7 lines
Quoted 7 lines
This is consistent with the original code which _is_ returning an integer.
Quoted 3 lines
But I'm inclined to retain the original intention to have the macro return the value that was passed in:
typeof(cond)__ret= (cond);
Quoted 8 lines
Quoted 4 lines
hmm, I think it is appropriate to retain the original type of 'cond' especially since it may also involve other arithmetic operations. But I doubt it will be very useful. For instance, who is going to write this code?
Maybe there is, maybe not. But it doesn’t hurt anything to leave the flexibility there using typeof(cond). As said, I'm also fine to use 'bool ret', but probably not 'int' for no good reason.
Right, maybe or maybe not. But using typeof(cond) for the variable does not always provide benefits. For instance if the 'cond' is resolved as the 8-byte type like u64, we are wasting space at runtime. We could have used a shorter type. In addition, throwing this to the compiler creates complexity and sometimes bugs since the compiler could have different behaviors.
So, I prefer not having typeof(cond) for KVM_BUG(). But if you have strong opinions using typeof, go ahead.
Thanks.
-Mingwei
Quoted 26 lines · Mingwei Zhang
Quoted 19 lines · Wei W
Quoted 17 lines · Mingwei Zhang
Quoted 16 lines · Mingwei Zhang
include/linux/kvm_host.h static inline void kvm_vm_bugged(struct kvm · line 852
Quoted 13 lines
I don't get it. Why bothering the type if we just do this?
include/linux/kvm_host.h +1
-1@@ -848,7 +848,7 @@ static inline void kvm_vm_bugged(struct kvm *kvm) #define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ + int __ret = !!(cond); \This is essentially "bool
__ret". No biggie to change it this way.Quoted 5 lines
Quoted 1 line
!! will return an int, not a boolean, but it is used as a boolean.
What's the point of defining it as an int when actually being used as a Boolean? Original returning of an 'int' is a bug in this sense. Either returning a Boolean or the same type (length) as cond is good way to me.
Quoted 8 lines
What's the point of using an integer? I think we need to ask the original author. But I think one of the reasons might be convenience as the return value. I am not sure if we can return a boolean in the function. But it should be fine here since it is a macro.
Anyway, returning an 'int' is not a bug. The bug is the casting from 'cond' to the integer that may lose information and this is what you have captured.
typeof() won't work if cond is a bitfield. See commit 8d4fbcfbe0a4 ("Fix WARN_ON() on bitfield ops") from Linus from back in 2007:
commit 8d4fbcfbe0a4bfc73e7f0297c59ae514e1f1436f
Author: Linus Torvalds <[email protected]> Date: Tue Jul 31 21:12:07 2007 -0700
Fix WARN_ON() on bitfield ops
Alexey Dobriyan noticed that the new WARN_ON() semantics that were introduced by commit 684f978347deb42d180373ac4c427f82ef963171 (to also return the value to be warned on) didn't compile when given a bitfield, because the typeof doesn't work for bitfields.
So instead of the typeof trick, use an "int" variable together with a "!!(x)" expression, as suggested by Al Viro.
To make matters more interesting, Paul Mackerras points out that that is sub-optimal on Power, but the old asm-coded comparison seems to be buggy anyway on 32-bit Power if the conditional was 64-bit, so I think there are more problems there.
Regardless, the new WARN_ON() semantics may have been a bad idea. But this at least avoids the more serious complications.
include/asm-generic/bug.h +1 -1
@@ -33,7 +33,7 @@ struct bug_entry {#ifndef HAVE_ARCH_WARN_ON #define WARN_ON (condition) ({ \ - typeof(condition) __ret_warn_on = (condition); \ + int __ret_warn_on = !!(condition); \ if (unlikely(__ret_warn_on)) { \ printk("WARNING: at %s:%d %s()\n", __FILE__, \ __LINE__, __FUNCTION__); \
@
[...]
As for int versus bool, I don't see a strong argument for either. So let's stick with int since that's what the current code is using and that aligns with the generic kernel WARN_ON().
If someone wants to propose using a bool instead of an int that should be a separate commit anyway and needs an actual justification.
Quoted 32 lines · David Matlack
Quoted 20 lines
Quoted 20 lines · Mingwei Zhang
Quoted 19 lines
Quoted 18 lines · Mingwei Zhang
Quoted 13 lines
I don't get it. Why bothering the type if we just do this?
(quoted diff) +0
-0
b/include/linux/kvm_host.hindex4f26b244f6d0..10455253c6ea100644include/linux/kvm_host.h +0
-0@@ -848,7 +848,7 @@ static inline void kvm_vm_bugged(structkvm
*kvm)#define
KVM_BUG(cond, kvm, fmt...)\
({ \include/linux/kvm_host.h
Quoted 2 lines
include/linux/kvm_host.h +1
-1-int __ret = (cond); \ +int __ret = !!(cond); \This is essentially "bool
__ret". No biggie to change it this way.Quoted 3 lines
Quoted 3 lines
Quoted 1 line
!! will return an int, not a boolean, but it is used as a boolean.
What's the point of defining it as an int when actually being used as a
Boolean?
Quoted 8 lines
Quoted 2 lines
Original returning of an 'int' is a bug in this sense. Either returning a Boolean or the same type (length) as cond is good way to me.
What's the point of using an integer? I think we need to ask the original author. But I think one of the reasons might be convenience as the return value. I am not sure if we can return a boolean in the function. But it should be fine here since it is a macro.
Quoted 6 lines
Quoted 3 lines
Anyway, returning an 'int' is not a bug. The bug is the casting from 'cond' to the integer that may lose information and this is what you have captured.
typeof()won't work if cond is a bitfield. See commit8d4fbcfbe0a4("FixWARN_ON()on bitfield ops") from Linus from back in 2007:
Yes, this seems to be a good reason for not going for typeof. Thanks for sharing.
Quoted 7 lines
As for int versus bool, I don't see a strong argument for either. So let's stick with int since that's what the current code is using and that aligns with the generic kernel
WARN_ON().If someone wants to propose using a bool instead of an int that should be a separate commit anyway and needs an actual justification.
Wait a bit. Let me seek for a valid reason from the WARN side first.
CodingStyle already says:
bool function return types and stack variables are always fine to use whenever appropriate. Use of bool is encouraged to improve readability and is often a better option than 'int' for storing boolean values.
Sean Christopherson
reviewerQuoted 33 lines · Wei W
Quoted 32 lines · David Matlack
Quoted 20 lines
Quoted 20 lines · Mingwei Zhang
Quoted 19 lines
Quoted 18 lines · Mingwei Zhang
Quoted 13 lines
I don't get it. Why bothering the type if we just do this?
(quoted diff) +0
-0
b/include/linux/kvm_host.hindex4f26b244f6d0..10455253c6ea100644include/linux/kvm_host.h +0
-0@@ -848,7 +848,7 @@ static inline void kvm_vm_bugged(structkvm
*kvm)#define
KVM_BUG(cond, kvm, fmt...)\
({ \include/linux/kvm_host.h
Quoted 2 lines
include/linux/kvm_host.h +1
-1-int __ret = (cond); \ +int __ret = !!(cond); \This is essentially "bool
__ret". No biggie to change it this way.Quoted 3 lines
Quoted 3 lines
Quoted 1 line
!! will return an int, not a boolean, but it is used as a boolean.
What's the point of defining it as an int when actually being used as a
Boolean?
Quoted 8 lines
Quoted 2 lines
Original returning of an 'int' is a bug in this sense. Either returning a Boolean or the same type (length) as cond is good way to me.
What's the point of using an integer? I think we need to ask the original author. But I think one of the reasons might be convenience as the return value. I am not sure if we can return a boolean in the function. But it should be fine here since it is a macro.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Anyway, returning an 'int' is not a bug. The bug is the casting from 'cond' to the integer that may lose information and this is what you have captured.
typeof()won't work if cond is a bitfield. See commit8d4fbcfbe0a4("FixWARN_ON()on bitfield ops") from Linus from back in 2007:Yes, this seems to be a good reason for not going for typeof. Thanks for sharing.
Ya, just make __ret a bool. I'm 99% certain I just loosely copied from WARN_ON(), but missed the !!.
Quoted 6 lines · Wei Wang
Current
KVM_BUGandKVM_BUG_ONassumes that 'cond' passed from callers is 32-bit as it casts 'cond' to the type of int. This will be wrong if 'cond' provided by a caller is 64-bit, e.g. an error code of0xc0000d0300000000will be converted to 0, which is not expected. Improves the implementation by using !!(cond) inKVM_BUGandKVM_BUG_ON. Compared to changing 'int' to 'int64_t', this has less LOCs.
This changes its semantics. cond is evaluated twice. Also the return value of KVM_BUG_ON() is changed to bool. typeof?
Perhaps return type of bool is okay, though.
Thanks,
Quoted 38 lines
Fixes 0b8f11737cff ("KVM: Add infrastructure and macro to mark VM as bugged")Signed-off-by Wei Wang <[email protected]>--- include/linux/kvm_host.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)include/linux/kvm_host.h +4
-8@@ -881,20 +881,16 @@ static inline void kvm_vm_bugged(struct kvm *kvm) #define KVM_BUG(cond, kvm, fmt...) \ ({ \ - int __ret = (cond); \ - \ - if (WARN_ONCE(__ret && !(kvm)->vm_bugged, fmt)) \ + if (WARN_ONCE(!!cond && !(kvm)->vm_bugged, fmt)) \ kvm_vm_bugged(kvm); \ - unlikely(__ret); \ + unlikely(!!cond); \ })#define KVM_BUG_ON (cond, kvm) \ ({ \ - int __ret = (cond); \ - \ - if (WARN_ON_ONCE(__ret && !(kvm)->vm_bugged)) \ + if (WARN_ON_ONCE(!!(cond) && !(kvm)->vm_bugged)) \ kvm_vm_bugged(kvm); \ - unlikely(__ret); \ + unlikely(!!(cond)); \ })static inline void kvm_vcpu_srcu_read_lock (struct kvm_vcpu *vcpu)Signature · 1 line
2.27.0
Signature · 1 line
Isaku Yamahata <[email protected]>