Skip to contentSkip to conversation

KVM: x86: Always set kvm_run->if_flag

PATCH Superseded Marc Orr 6 participants 10 messages Dec 7, 2021 – Dec 9, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Marc Orr

author
#0 lore raw

PATCH 1/1 KVM: x86: Always set kvm_run->if_flag

The kvm_run struct's if_flag is apart of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted regiesters (i.e., with guest_state_protected set).

Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")
---
 arch/x86/include/asm/kvm-x86-ops.h | 1 +
 arch/x86/include/asm/kvm_host.h    | 1 +
 arch/x86/kvm/svm/svm.c             | 8 ++++++++
 arch/x86/kvm/vmx/vmx.c             | 6 ++++++
 arch/x86/kvm/x86.c                 | 9 +--------
 5 files changed, 17 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index cefe1d81e2e8..9e50da3ed01a 100644
@@ -47,6 +47,7 @@ KVM_X86_OP(set_dr7)
 KVM_X86_OP(cache_reg)
 KVM_X86_OP(get_rflags)
 KVM_X86_OP(set_rflags)
+KVM_X86_OP(get_if_flag)
 KVM_X86_OP(tlb_flush_all)
 KVM_X86_OP(tlb_flush_current)
 KVM_X86_OP_NULL(tlb_remote_flush)
arch/x86/include/asm/kvm_host.h +1 -0
index 860ed500580c..a7f868ff23e7 100644
@@ -1349,6 +1349,7 @@ struct kvm_x86_ops {
 	void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
 	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
 	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
+	bool (*get_if_flag)(struct kvm_vcpu *vcpu);
 
 	void (*tlb_flush_all)(struct kvm_vcpu *vcpu);
 	void (*tlb_flush_current)(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm/svm.c +8 -0
index d0f68d11ec70..91608f8c0cde 100644
@@ -1585,6 +1585,13 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
 	to_svm(vcpu)->vmcb->save.rflags = rflags;
 }
 
+static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
+{
+	struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+
+	return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);
+}
+
 static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
 {
 	switch (reg) {
@@ -4621,6 +4628,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
 	.cache_reg = svm_cache_reg,
 	.get_rflags = svm_get_rflags,
 	.set_rflags = svm_set_rflags,
+	.get_if_flag = svm_get_if_flag,
 
 	.tlb_flush_all = svm_flush_tlb,
 	.tlb_flush_current = svm_flush_tlb,
arch/x86/kvm/vmx/vmx.c +6 -0
index 9453743ce0c4..6056baa13977 100644
@@ -1363,6 +1363,11 @@ void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
 		vmx->emulation_required = vmx_emulation_required(vcpu);
 }
 
+static bool vmx_get_if_flag(struct kvm_vcpu *vcpu)
+{
+	return !!(vmx_get_rflags(vcpu) & X86_EFLAGS_IF);
+}
+
 u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
 {
 	u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
@@ -7575,6 +7580,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
 	.cache_reg = vmx_cache_reg,
 	.get_rflags = vmx_get_rflags,
 	.set_rflags = vmx_set_rflags,
+	.get_if_flag = vmx_get_if_flag,
 
 	.tlb_flush_all = vmx_flush_tlb_all,
 	.tlb_flush_current = vmx_flush_tlb_current,
arch/x86/kvm/x86.c +1 -8
index e0aa4dd53c7f..45e836db5bcd 100644
@@ -8995,14 +8995,7 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu)
 {
 	struct kvm_run *kvm_run = vcpu->run;
 
-	/*
-	 * if_flag is obsolete and useless, so do not bother
-	 * setting it for SEV-ES guests.  Userspace can just
-	 * use kvm_run->ready_for_interrupt_injection.
-	 */
-	kvm_run->if_flag = !vcpu->arch.guest_state_protected
-		&& (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
-
+	kvm_run->if_flag = static_call(kvm_x86_get_if_flag)(vcpu);
 	kvm_run->cr8 = kvm_get_cr8(vcpu);
 	kvm_run->apic_base = kvm_get_apic_base(vcpu);
 
Signature · 1 line
2.34.1.400.ga245620fadb-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 2 lines · Marc Orr

The kvm_run struct's if_flag is apart of the userspace/kernel API. The

Typo: 'a part'.

Quoted 4 lines

SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted regiesters (i.e., with guest_state_protected set).

Typo: 'registers'.

arch/x86/kvm/x86.c static void post_kvm_run_save(struct kvm_vcpu *vcpu) · line 8998

Quoted 13 lines
Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")

---

arch/x86/kvm/x86.c +1 -8
-       /*
-        * if_flag is obsolete and useless, so do not bother
-        * setting it for SEV-ES guests.  Userspace can just
-        * use kvm_run->ready_for_interrupt_injection.
-        */
-       kvm_run->if_flag = !vcpu->arch.guest_state_protected
-               && (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
-
+       kvm_run->if_flag = static_call(kvm_x86_get_if_flag)(vcpu);

I'm sorry that I missed that change when it first went by. Maintaining backwards compatibility with existing userspace code is a fundamental tenet of Linux kernel development.

#2 lore raw
In reply to Marc Orr
Quoted 45 lines · Marc Orr

The kvm_run struct's if_flag is apart of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted regiesters (i.e., with guest_state_protected set).

Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")
---
  arch/x86/include/asm/kvm-x86-ops.h | 1 +
  arch/x86/include/asm/kvm_host.h    | 1 +
  arch/x86/kvm/svm/svm.c             | 8 ++++++++
  arch/x86/kvm/vmx/vmx.c             | 6 ++++++
  arch/x86/kvm/x86.c                 | 9 +--------
  5 files changed, 17 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index cefe1d81e2e8..9e50da3ed01a 100644
@@ -47,6 +47,7 @@ KVM_X86_OP(set_dr7)
  KVM_X86_OP(cache_reg)
  KVM_X86_OP(get_rflags)
  KVM_X86_OP(set_rflags)
+KVM_X86_OP(get_if_flag)
  KVM_X86_OP(tlb_flush_all)
  KVM_X86_OP(tlb_flush_current)
  KVM_X86_OP_NULL(tlb_remote_flush)
arch/x86/include/asm/kvm_host.h +1 -0
index 860ed500580c..a7f868ff23e7 100644
@@ -1349,6 +1349,7 @@ struct kvm_x86_ops {
  	void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
  	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
+	bool (*get_if_flag)(struct kvm_vcpu *vcpu);
 
  	void (*tlb_flush_all)(struct kvm_vcpu *vcpu);
  	void (*tlb_flush_current)(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm/svm.c +0 -0
index d0f68d11ec70..91608f8c0cde 100644

arch/x86/kvm/svm/svm.c static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) · line 1592

Quoted 8 lines
arch/x86/kvm/svm/svm.c +5 -0
@@ -1585,6 +1585,13 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  	to_svm(vcpu)->vmcb->save.rflags = rflags;
  }
 
+static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
+{
+	struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+
+	return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);

I'm not sure if this is always valid to use for non SEV-ES guests. Maybe the better thing would be:

	return sev_es_guest(vcpu->kvm) ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
				       : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;

(Since this function returns a bool, I don't think you need the !!)

Thanks,
Tom

Quoted 58 lines
arch/x86/kvm/svm/svm.c +3 -0
+}
+
  static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  {
  	switch (reg) {
@@ -4621,6 +4628,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
  	.cache_reg = svm_cache_reg,
  	.get_rflags = svm_get_rflags,
  	.set_rflags = svm_set_rflags,
+	.get_if_flag = svm_get_if_flag,
 
  	.tlb_flush_all = svm_flush_tlb,
  	.tlb_flush_current = svm_flush_tlb,
arch/x86/kvm/vmx/vmx.c +6 -0
index 9453743ce0c4..6056baa13977 100644
@@ -1363,6 +1363,11 @@ void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  		vmx->emulation_required = vmx_emulation_required(vcpu);
  }
 
+static bool vmx_get_if_flag(struct kvm_vcpu *vcpu)
+{
+	return !!(vmx_get_rflags(vcpu) & X86_EFLAGS_IF);
+}
+
  u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  {
  	u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
@@ -7575,6 +7580,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
  	.cache_reg = vmx_cache_reg,
  	.get_rflags = vmx_get_rflags,
  	.set_rflags = vmx_set_rflags,
+	.get_if_flag = vmx_get_if_flag,
 
  	.tlb_flush_all = vmx_flush_tlb_all,
  	.tlb_flush_current = vmx_flush_tlb_current,
arch/x86/kvm/x86.c +1 -8
index e0aa4dd53c7f..45e836db5bcd 100644
@@ -8995,14 +8995,7 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu)
  {
  	struct kvm_run *kvm_run = vcpu->run;
 
-	/*
-	 * if_flag is obsolete and useless, so do not bother
-	 * setting it for SEV-ES guests.  Userspace can just
-	 * use kvm_run->ready_for_interrupt_injection.
-	 */
-	kvm_run->if_flag = !vcpu->arch.guest_state_protected
-		&& (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
-
+	kvm_run->if_flag = static_call(kvm_x86_get_if_flag)(vcpu);
  	kvm_run->cr8 = kvm_get_cr8(vcpu);
  	kvm_run->apic_base = kvm_get_apic_base(vcpu);
 
author
#3 lore raw
Quoted 59 lines · Tom Lendacky
Quoted 45 lines · Marc Orr

The kvm_run struct's if_flag is apart of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted regiesters (i.e., with guest_state_protected set).

Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")
---
  arch/x86/include/asm/kvm-x86-ops.h | 1 +
  arch/x86/include/asm/kvm_host.h    | 1 +
  arch/x86/kvm/svm/svm.c             | 8 ++++++++
  arch/x86/kvm/vmx/vmx.c             | 6 ++++++
  arch/x86/kvm/x86.c                 | 9 +--------
  5 files changed, 17 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index cefe1d81e2e8..9e50da3ed01a 100644
@@ -47,6 +47,7 @@ KVM_X86_OP(set_dr7)
  KVM_X86_OP(cache_reg)
  KVM_X86_OP(get_rflags)
  KVM_X86_OP(set_rflags)
+KVM_X86_OP(get_if_flag)
  KVM_X86_OP(tlb_flush_all)
  KVM_X86_OP(tlb_flush_current)
  KVM_X86_OP_NULL(tlb_remote_flush)
arch/x86/include/asm/kvm_host.h +1 -0
index 860ed500580c..a7f868ff23e7 100644
@@ -1349,6 +1349,7 @@ struct kvm_x86_ops {
      void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
      unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
      void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
+     bool (*get_if_flag)(struct kvm_vcpu *vcpu);
 
      void (*tlb_flush_all)(struct kvm_vcpu *vcpu);
      void (*tlb_flush_current)(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm/svm.c +0 -0
index d0f68d11ec70..91608f8c0cde 100644

arch/x86/kvm/svm/svm.c static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) · line 1592

Quoted 8 lines
arch/x86/kvm/svm/svm.c +5 -0
@@ -1585,6 +1585,13 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
      to_svm(vcpu)->vmcb->save.rflags = rflags;
  }
 
+static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
+{
+     struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+
+     return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);

I'm not sure if this is always valid to use for non SEV-ES guests. Maybe the better thing would be:

Quoted 4 lines
        return sev_es_guest(vcpu->kvm) ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
                                       : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;

(Since this function returns a bool, I don't think you need the !!)

I had the same reservations when writing the patch. (Why fix what's not broken.) The reason I wrote the patch this way is based on what I read in APM vol2: Appendix B Layout of VMCB: "GUEST_INTERRUPT_MASK - Value of the RFLAGS.IF bit for the guest."

Also, I had _thought_ that svm_interrupt_allowed() -- the AMD-specific function used to populate ready_for_interrupt_injection -- was relying on GUEST_INTERRUPT_MASK. But now I'm reading the code again, and I realized I was overly focused on the SEV-ES handling. That code is actually extracting the IF bit from the RFLAGS register in the same way you've proposed here.

Changing the patch as you've suggested SGTM. I can send out a v2. I'll wait a day or two to see if there are any other comments first. I guess the alternative would be to change svm_interrupt_blocked() to solely use the SVM_GUEST_INTERRUPT_MASK. If we were confident that it was sufficient, it would be a nice little cleanup. But regardless, I think we should keep the code introduced by this patch consistent with svm_interrupt_blocked().

Also, noted on the !! not being needed when returning from a bool function. I'll keep this in mind in the future. Thanks!

#4 lore raw
Quoted 50 lines · Marc Orr
Quoted 49 lines · Tom Lendacky
Quoted 48 lines · Marc Orr

The kvm_run struct's if_flag is apart of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted regiesters (i.e., with guest_state_protected set).

Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")
---
  arch/x86/include/asm/kvm-x86-ops.h | 1 +
  arch/x86/include/asm/kvm_host.h    | 1 +
  arch/x86/kvm/svm/svm.c             | 8 ++++++++
  arch/x86/kvm/vmx/vmx.c             | 6 ++++++
  arch/x86/kvm/x86.c                 | 9 +--------
  5 files changed, 17 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index cefe1d81e2e8..9e50da3ed01a 100644
@@ -47,6 +47,7 @@ KVM_X86_OP(set_dr7)
  KVM_X86_OP(cache_reg)
  KVM_X86_OP(get_rflags)
  KVM_X86_OP(set_rflags)
+KVM_X86_OP(get_if_flag)
  KVM_X86_OP(tlb_flush_all)
  KVM_X86_OP(tlb_flush_current)
  KVM_X86_OP_NULL(tlb_remote_flush)
arch/x86/include/asm/kvm_host.h +1 -0
index 860ed500580c..a7f868ff23e7 100644
@@ -1349,6 +1349,7 @@ struct kvm_x86_ops {
      void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
      unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
      void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
+     bool (*get_if_flag)(struct kvm_vcpu *vcpu);
 
      void (*tlb_flush_all)(struct kvm_vcpu *vcpu);
      void (*tlb_flush_current)(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm/svm.c +0 -0
index d0f68d11ec70..91608f8c0cde 100644
@@ -1585,6 +1585,13 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
      to_svm(vcpu)->vmcb->save.rflags = rflags;
  }
 
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/svm/svm.c static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) · line 1592

Quoted 5 lines
arch/x86/kvm/svm/svm.c +5 -0
+static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
+{
+     struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+
+     return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);

I'm not sure if this is always valid to use for non SEV-ES guests. Maybe the better thing would be:

I also noticed long ago that SVM_GUEST_INTERRUPT_MASK seems to duplicate the EFLAGS.IF value, it seems to have no other purpose.

Best regards,
Maxim Levitsky

Quoted 29 lines
Quoted 5 lines
        return sev_es_guest(vcpu->kvm) ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
                                       : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;

(Since this function returns a bool, I don't think you need the !!)

I had the same reservations when writing the patch. (Why fix what's not broken.) The reason I wrote the patch this way is based on what I read in APM vol2: Appendix B Layout of VMCB: "GUEST_INTERRUPT_MASK - Value of the RFLAGS.IF bit for the guest."

Also, I had _thought_ that svm_interrupt_allowed() -- the AMD-specific function used to populate ready_for_interrupt_injection -- was relying on GUEST_INTERRUPT_MASK. But now I'm reading the code again, and I realized I was overly focused on the SEV-ES handling. That code is actually extracting the IF bit from the RFLAGS register in the same way you've proposed here.

Changing the patch as you've suggested SGTM. I can send out a v2. I'll wait a day or two to see if there are any other comments first. I guess the alternative would be to change svm_interrupt_blocked() to solely use the SVM_GUEST_INTERRUPT_MASK. If we were confident that it was sufficient, it would be a nice little cleanup. But regardless, I think we should keep the code introduced by this patch consistent with svm_interrupt_blocked().

Also, noted on the !! not being needed when returning from a bool function. I'll keep this in mind in the future. Thanks!

#5 lore raw
In reply to Marc Orr
Quoted 63 lines · Marc Orr
Quoted 62 lines · Tom Lendacky
Quoted 45 lines · Marc Orr

The kvm_run struct's if_flag is apart of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted regiesters (i.e., with guest_state_protected set).

Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")
---
   arch/x86/include/asm/kvm-x86-ops.h | 1 +
   arch/x86/include/asm/kvm_host.h    | 1 +
   arch/x86/kvm/svm/svm.c             | 8 ++++++++
   arch/x86/kvm/vmx/vmx.c             | 6 ++++++
   arch/x86/kvm/x86.c                 | 9 +--------
   5 files changed, 17 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index cefe1d81e2e8..9e50da3ed01a 100644
@@ -47,6 +47,7 @@ KVM_X86_OP(set_dr7)
   KVM_X86_OP(cache_reg)
   KVM_X86_OP(get_rflags)
   KVM_X86_OP(set_rflags)
+KVM_X86_OP(get_if_flag)
   KVM_X86_OP(tlb_flush_all)
   KVM_X86_OP(tlb_flush_current)
   KVM_X86_OP_NULL(tlb_remote_flush)
arch/x86/include/asm/kvm_host.h +1 -0
index 860ed500580c..a7f868ff23e7 100644
@@ -1349,6 +1349,7 @@ struct kvm_x86_ops {
       void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
       unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
       void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
+     bool (*get_if_flag)(struct kvm_vcpu *vcpu);
 
       void (*tlb_flush_all)(struct kvm_vcpu *vcpu);
       void (*tlb_flush_current)(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm/svm.c +0 -0
index d0f68d11ec70..91608f8c0cde 100644

arch/x86/kvm/svm/svm.c static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) · line 1592

Quoted 8 lines
arch/x86/kvm/svm/svm.c +5 -0
@@ -1585,6 +1585,13 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
       to_svm(vcpu)->vmcb->save.rflags = rflags;
   }
 
+static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
+{
+     struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+
+     return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);

I'm not sure if this is always valid to use for non SEV-ES guests. Maybe the better thing would be:

         return sev_es_guest(vcpu->kvm) ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
                                        : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;
Quoted 6 lines
Quoted 1 line

(Since this function returns a bool, I don't think you need the !!)

I had the same reservations when writing the patch. (Why fix what's not broken.) The reason I wrote the patch this way is based on what I read in APM vol2: Appendix B Layout of VMCB: "GUEST_INTERRUPT_MASK - Value of the RFLAGS.IF bit for the guest."

I just verified with the hardware team that this flag is indeed only set for a guest with protected state (SEV-ES / SEV-SNP). An update to the APM will be made.

Thanks,
Tom

Quoted 19 lines

Also, I had _thought_ that svm_interrupt_allowed() -- the AMD-specific function used to populate ready_for_interrupt_injection -- was relying on GUEST_INTERRUPT_MASK. But now I'm reading the code again, and I realized I was overly focused on the SEV-ES handling. That code is actually extracting the IF bit from the RFLAGS register in the same way you've proposed here.

Changing the patch as you've suggested SGTM. I can send out a v2. I'll wait a day or two to see if there are any other comments first. I guess the alternative would be to change svm_interrupt_blocked() to solely use the SVM_GUEST_INTERRUPT_MASK. If we were confident that it was sufficient, it would be a nice little cleanup. But regardless, I think we should keep the code introduced by this patch consistent with svm_interrupt_blocked().

Also, noted on the !! not being needed when returning from a bool function. I'll keep this in mind in the future. Thanks!

#6 lore raw
Quoted 16 lines · Tom Lendacky
Quoted 15 lines · Marc Orr
Quoted 13 lines · Tom Lendacky

Review

Quoted 5 lines
(quoted diff) +5 -0
+static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
+{
+     struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+
+     return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);

I'm not sure if this is always valid to use for non SEV-ES guests. Maybe the better thing would be:

         return sev_es_guest(vcpu->kvm) ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
                                        : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;

(Since this function returns a bool, I don't think you need the !!)

Quoted 8 lines
Quoted 4 lines

I had the same reservations when writing the patch. (Why fix what's not broken.) The reason I wrote the patch this way is based on what I read in APM vol2: Appendix B Layout of VMCB: "GUEST_INTERRUPT_MASK - Value of the RFLAGS.IF bit for the guest."

I just verified with the hardware team that this flag is indeed only set for a guest with protected state (SEV-ES / SEV-SNP). An update to the APM will be made.

svm_interrupt_blocked() should be modified to use the new svm_get_if_flag() helper so that the SEV-{ES,SN} behavior is contained in a single location, e.g.

arch/x86/kvm/svm/svm.c +3 -10
index 208566f63bce..fef04e9fa9c9 100644
@@ -3583,14 +3583,10 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
        if (!gif_set(svm))
                return true;
 
-       if (sev_es_guest(vcpu->kvm)) {
-               /*
-                * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask
-                * bit to determine the state of the IF flag.
-                */
-               if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK))
+       if (!is_guest_mode(vcpu)) {
+               if (!svm_get_if_flag(vcpu))
                        return true;
-       } else if (is_guest_mode(vcpu)) {
+       } else {
                /* As long as interrupts are being delivered...  */
                if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
                    ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
@@ -3600,9 +3596,6 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
                /* ... vmexits aren't blocked by the interrupt shadow  */
                if (nested_exit_on_intr(svm))
                        return false;
-       } else {
-               if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
-                       return true;
        }
 
        return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
author
#7 lore raw
Quoted 53 lines · Sean Christopherson
Quoted 16 lines · Tom Lendacky
Quoted 15 lines · Marc Orr
Quoted 13 lines · Tom Lendacky

Review

Quoted 5 lines
(quoted diff) +5 -0
+static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
+{
+     struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+
+     return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);

I'm not sure if this is always valid to use for non SEV-ES guests. Maybe the better thing would be:

         return sev_es_guest(vcpu->kvm) ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
                                        : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;

(Since this function returns a bool, I don't think you need the !!)

Quoted 8 lines
Quoted 4 lines

I had the same reservations when writing the patch. (Why fix what's not broken.) The reason I wrote the patch this way is based on what I read in APM vol2: Appendix B Layout of VMCB: "GUEST_INTERRUPT_MASK - Value of the RFLAGS.IF bit for the guest."

I just verified with the hardware team that this flag is indeed only set for a guest with protected state (SEV-ES / SEV-SNP). An update to the APM will be made.

svm_interrupt_blocked() should be modified to use the new svm_get_if_flag() helper so that the SEV-{ES,SN} behavior is contained in a single location, e.g.

arch/x86/kvm/svm/svm.c +3 -7
index 208566f63bce..fef04e9fa9c9 100644
@@ -3583,14 +3583,10 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
        if (!gif_set(svm))
                return true;
 
-       if (sev_es_guest(vcpu->kvm)) {
-               /*
-                * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask
-                * bit to determine the state of the IF flag.
-                */
-               if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK))
+       if (!is_guest_mode(vcpu)) {
+               if (!svm_get_if_flag(vcpu))
                        return true;
-       } else if (is_guest_mode(vcpu)) {
+       } else {
                /* As long as interrupts are being delivered...  */
                if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
                    ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)

arch/x86/kvm/svm/svm.c bool svm_interrupt_blocked(struct kvm_vcpu *vcpu) · line 3601

Quoted 9 lines
arch/x86/kvm/svm/svm.c +0 -3
@@ -3600,9 +3596,6 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
                /* ... vmexits aren't blocked by the interrupt shadow  */
                if (nested_exit_on_intr(svm))
                        return false;
-       } else {
-               if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
-                       return true;
        }
 
        return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);

Agreed. This is a nice change. I'll incorporate it into v2. Thanks!

author
#8 lore raw
In reply to Tom Lendacky
Quoted 67 lines · Tom Lendacky
Quoted 65 lines · Marc Orr
Quoted 63 lines · Tom Lendacky
Quoted 45 lines · Marc Orr

The kvm_run struct's if_flag is apart of the userspace/kernel API. The SEV-ES patches failed to set this flag because it's no longer needed by QEMU (according to the comment in the source code). However, other hypervisors may make use of this flag. Therefore, set the flag for guests with encrypted regiesters (i.e., with guest_state_protected set).

Fixes f1c6366e3043 ("KVM: SVM: Add required changes to support intercepts under SEV-ES")
---
   arch/x86/include/asm/kvm-x86-ops.h | 1 +
   arch/x86/include/asm/kvm_host.h    | 1 +
   arch/x86/kvm/svm/svm.c             | 8 ++++++++
   arch/x86/kvm/vmx/vmx.c             | 6 ++++++
   arch/x86/kvm/x86.c                 | 9 +--------
   5 files changed, 17 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index cefe1d81e2e8..9e50da3ed01a 100644
@@ -47,6 +47,7 @@ KVM_X86_OP(set_dr7)
   KVM_X86_OP(cache_reg)
   KVM_X86_OP(get_rflags)
   KVM_X86_OP(set_rflags)
+KVM_X86_OP(get_if_flag)
   KVM_X86_OP(tlb_flush_all)
   KVM_X86_OP(tlb_flush_current)
   KVM_X86_OP_NULL(tlb_remote_flush)
arch/x86/include/asm/kvm_host.h +1 -0
index 860ed500580c..a7f868ff23e7 100644
@@ -1349,6 +1349,7 @@ struct kvm_x86_ops {
       void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
       unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
       void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
+     bool (*get_if_flag)(struct kvm_vcpu *vcpu);
 
       void (*tlb_flush_all)(struct kvm_vcpu *vcpu);
       void (*tlb_flush_current)(struct kvm_vcpu *vcpu);
arch/x86/kvm/svm/svm.c +0 -0
index d0f68d11ec70..91608f8c0cde 100644

arch/x86/kvm/svm/svm.c static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) · line 1592

Quoted 8 lines
arch/x86/kvm/svm/svm.c +5 -0
@@ -1585,6 +1585,13 @@ static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
       to_svm(vcpu)->vmcb->save.rflags = rflags;
   }
 
+static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
+{
+     struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+
+     return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);

I'm not sure if this is always valid to use for non SEV-ES guests. Maybe the better thing would be:

         return sev_es_guest(vcpu->kvm) ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
                                        : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;

(Since this function returns a bool, I don't think you need the !!)

Quoted 8 lines
Quoted 4 lines

I had the same reservations when writing the patch. (Why fix what's not broken.) The reason I wrote the patch this way is based on what I read in APM vol2: Appendix B Layout of VMCB: "GUEST_INTERRUPT_MASK - Value of the RFLAGS.IF bit for the guest."

I just verified with the hardware team that this flag is indeed only set for a guest with protected state (SEV-ES / SEV-SNP). An update to the APM will be made.

Got it now. Then the change you suggested is a must! Thanks, Tom.

maintainer
#9 lore raw
Quoted 18 lines · Marc Orr
Quoted 18 lines
Quoted 17 lines
Quoted 8 lines

arch/x86/kvm/svm/svm.c static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) · line 1592

Quoted 5 lines
arch/x86/kvm/svm/svm.c +5 -0
+static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
+{
+     struct vmcb *vmcb = to_svm(vcpu)->vmcb;
+
+     return !!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK);

I'm not sure if this is always valid to use for non SEV-ES guests. Maybe the better thing would be:

Quoted 4 lines
          return sev_es_guest(vcpu->kvm) ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
                                         : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;

(Since this function returns a bool, I don't think you need the !!)

I had the same reservations when writing the patch. (Why fix what's not broken.) The reason I wrote the patch this way is based on what I read in APM vol2: Appendix B Layout of VMCB: "GUEST_INTERRUPT_MASK - Value of the RFLAGS.IF bit for the guest."

Quoted 5 lines
Quoted 3 lines

I just verified with the hardware team that this flag is indeed only set for a guest with protected state (SEV-ES / SEV-SNP). An update to the APM will be made.

Got it now. Then the change you suggested is a must! Thanks, Tom.

Besides, the bit wouldn't have existed on old (pre-SEV-ES) processors.

Paolo