Skip to contentSkip to conversation

KVM: x86: use inlines instead of macros for is_sev_*guest

PATCH In review Paolo Bonzini 2 participants 3 messages Apr 13 – Apr 14

Subsystems (3)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86: use inlines instead of macros for is_sev_*guest

This helps avoiding more embarrassment to this maintainer, but also will catch mistakes more easily for others.

---
 arch/x86/kvm/svm/svm.h | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/svm.h +13 -3
index fd0652b32c81..a10668d17a16 100644
@@ -422,9 +422,19 @@ static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
 	return ____sev_snp_guest(vcpu->kvm);
 }
 #else
-#define is_sev_guest(vcpu) false
-#define is_sev_es_guest(vcpu) false
-#define is_sev_snp_guest(vcpu) false
+static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+static __always_inline bool is_sev_es_guest(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
 #endif
 
 static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val)
Signature · 1 line
2.53.0
#1 lore raw
Quoted 1 line · Paolo Bonzini

This helps avoiding more embarrassment to this maintainer,

Not building SEV support before pushing to kvm/queue is certainly a choice :-D

Quoted 21 lines

but also will catch mistakes more easily for others.

---
 arch/x86/kvm/svm/svm.h | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/svm.h +3 -3
index fd0652b32c81..a10668d17a16 100644
@@ -422,9 +422,19 @@ static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
 	return ____sev_snp_guest(vcpu->kvm);
 }
 #else
-#define is_sev_guest(vcpu) false
-#define is_sev_es_guest(vcpu) false
-#define is_sev_snp_guest(vcpu) false
+static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu)
+{
+	return false;

arch/x86/kvm/svm/svm.h static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu) · line 437

Quoted 10 lines
arch/x86/kvm/svm/svm.h +10 -0
+}
+static __always_inline bool is_sev_es_guest(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
+{
+	return false;
+}

As called out in commit 45d522d3ee9c ("KVM: SVM: Macrofy SEV=n versions of sev_xxx_guest()"), use of macros is intentional as gcc-12 at least generates suboptimal code if there's a proper function.

That said, gcc-11 (I don't think I have gcc-12 laying around anywhere) generates identical code for all of kvm-amd.ko with and without macros, so maybe whatever I saw a transient issue? I don't have a strong motivation for caring about any version of gcc, so I'm a-ok using __always_inline functions.

Alternatively, e.g. if a "bad code generation" issue pops up again, we could use BUILD_BUG_ON_INVALID() to validate the input.

arch/x86/kvm/svm/svm.h +3 -13
index 5440486a3d32..3f572f30f28c 100644
@@ -425,19 +425,9 @@ static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
        return ____sev_snp_guest(vcpu->kvm);
 }
 #else
-static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu)
-{
-       return false;
-}
-static __always_inline bool is_sev_es_guest(struct kvm_vcpu *vcpu)
-{
-       return false;
-}
-
-static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
-{
-       return false;
-}
+#define is_sev_guest(vcpu) ({ BUILD_BUG_ON_INVALID((vcpu)->kvm); false; })
+#define is_sev_es_guest(vcpu) ({ BUILD_BUG_ON_INVALID((vcpu)->kvm); false; })
+#define is_sev_snp_guest(vcpu) ({ BUILD_BUG_ON_INVALID((vcpu)->kvm); false; })
 #endif
 
 static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val)
author maintainer
#2 lore raw
Quoted 4 lines · Sean Christopherson
Quoted 1 line · Paolo Bonzini

This helps avoiding more embarrassment to this maintainer,

Not building SEV support before pushing to kvm/queue is certainly a choice :-D

Fortunately, the part where I actually do more than a build smoke test and push to kvm/next has some automation... but the bots are too fast these days.

Quoted 8 lines

As called out in commit 45d522d3ee9c ("KVM: SVM: Macrofy SEV=n versions of sev_xxx_guest()"), use of macros is intentional as gcc-12 at least generates suboptimal code if there's a proper function.

That said, gcc-11 (I don't think I have gcc-12 laying around anywhere) generates identical code for all of kvm-amd.ko with and without macros, so maybe whatever I saw a transient issue? I don't have a strong motivation for caring about any version of gcc, so I'm a-ok using __always_inline functions.

Yeah, it seems strange for the compiler to have this problem except at -O0.

Quoted 7 lines
  #else
arch/x86/kvm/svm/svm.h +0 -6
-static __always_inline bool is_sev_guest(struct kvm_vcpu *vcpu)
-{
-       return false;
-}
-static __always_inline bool is_sev_es_guest(struct kvm_vcpu *vcpu)
-{

arch/x86/kvm/svm/svm.h static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu) · line 430

Quoted 10 lines
arch/x86/kvm/svm/svm.h +3 -7
-       return false;
-}
-
-static __always_inline bool is_sev_snp_guest(struct kvm_vcpu *vcpu)
-{
-       return false;
-}
+#define is_sev_guest(vcpu) ({ BUILD_BUG_ON_INVALID((vcpu)->kvm); false; })
+#define is_sev_es_guest(vcpu) ({ BUILD_BUG_ON_INVALID((vcpu)->kvm); false; })
+#define is_sev_snp_guest(vcpu) ({ BUILD_BUG_ON_INVALID((vcpu)->kvm); false; })

BUILD_BUG_ON_INVALID isn't great because it doesn't have type checking.

Paolo