Skip to contentSkip to conversation

KVM: x86: lapic: don't touch irr_pending in kvm_apic_update_apicv when inhibiting it

PATCH In review Sasha Levin 2 participants 4 messages Feb 22, 2022 – Mar 1, 2022

Subsystems (2)
lore
Share & tools
lore

Sasha Levin

author maintainer
#0 lore raw

PATCH 1/2 KVM: x86: lapic: don't touch irr_pending in kvm_apic_update_apicv when inhibiting it

From: Maxim Levitsky <[email protected]>

[ Upstream commit 755c2bf878607dbddb1423df9abf16b82205896f ]

kvm_apic_update_apicv is called when AVIC is still active, thus IRR bits can be set by the CPU after it is called, and don't cause the irr_pending to be set to true.

Also logic in avic_kick_target_vcpu doesn't expect a race with this function so to make it simple, just keep irr_pending set to true and let the next interrupt injection to the guest clear it.

Message-Id: <[email protected]>

---
 arch/x86/kvm/lapic.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +6 -1
index 91c2dc9f198df..5f935e7a09566 100644
@@ -2306,7 +2306,12 @@ void kvm_apic_update_apicv(struct kvm_vcpu *vcpu)
 		apic->irr_pending = true;
 		apic->isr_count = 1;
 	} else {
-		apic->irr_pending = (apic_search_irr(apic) != -1);
+		/*
+		 * Don't clear irr_pending, searching the IRR can race with
+		 * updates from the CPU as APICv is still active from hardware's
+		 * perspective.  The flag will be cleared as appropriate when
+		 * KVM injects the interrupt.
+		 */
 		apic->isr_count = count_vectors(apic->regs + APIC_ISR);
 	}
 }
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 2/2 KVM: x86: nSVM: deal with L1 hypervisor that intercepts interrupts but lets L2 control them

From: Maxim Levitsky <[email protected]>

[ Upstream commit 2b0ecccb55310a4b8ad5d59c703cf8c821be6260 ]

Fix a corner case in which the L1 hypervisor intercepts interrupts (INTERCEPT_INTR) and either doesn't set virtual interrupt masking (V_INTR_MASKING) or enters a nested guest with EFLAGS.IF disabled prior to the entry.

In this case, despite the fact that L1 intercepts the interrupts, KVM still needs to set up an interrupt window to wait before injecting the INTR vmexit.

Currently the KVM instead enters an endless loop of 'req_immediate_exit'.

Exactly the same issue also happens for SMIs and NMI. Fix this as well.

Note that on VMX, this case is impossible as there is only 'vmexit on external interrupts' execution control which either set, in which case both host and guest's EFLAGS.IF
are ignored, or not set, in which case no VMexits are delivered.

Message-Id: <[email protected]>

---
 arch/x86/kvm/svm/svm.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/svm.c +13 -4
index f05aa7290267d..aeee6df523af9 100644
@@ -3465,11 +3465,13 @@ static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 	if (svm->nested.nested_run_pending)
 		return -EBUSY;
 
+	if (svm_nmi_blocked(vcpu))
+		return 0;
+
 	/* An NMI must not be injected into L2 if it's supposed to VM-Exit.  */
 	if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
 		return -EBUSY;
-
-	return !svm_nmi_blocked(vcpu);
+	return 1;
 }
 
 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
@@ -3521,9 +3523,13 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
 static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
+
 	if (svm->nested.nested_run_pending)
 		return -EBUSY;
 
+	if (svm_interrupt_blocked(vcpu))
+		return 0;
+
 	/*
 	 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
 	 * e.g. if the IRQ arrived asynchronously after checking nested events.
@@ -3531,7 +3537,7 @@ static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 	if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
 		return -EBUSY;
 
-	return !svm_interrupt_blocked(vcpu);
+	return 1;
 }
 
 static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
@@ -4286,11 +4292,14 @@ static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 	if (svm->nested.nested_run_pending)
 		return -EBUSY;
 
+	if (svm_smi_blocked(vcpu))
+		return 0;
+
 	/* An SMI must not be injected into L2 if it's supposed to VM-Exit.  */
 	if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
 		return -EBUSY;
 
-	return !svm_smi_blocked(vcpu);
+	return 1;
 }
 
 static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/2
#3 lore raw
In reply to Sasha Levin
Quoted 28 lines · Sasha Levin

From: Maxim Levitsky <[email protected]>

[ Upstream commit 755c2bf878607dbddb1423df9abf16b82205896f ]

kvm_apic_update_apicv is called when AVIC is still active, thus IRR bits can be set by the CPU after it is called, and don't cause the irr_pending to be set to true.

Also logic in avic_kick_target_vcpu doesn't expect a race with this function so to make it simple, just keep irr_pending set to true and let the next interrupt injection to the guest clear it.

Message-Id: <[email protected]>

---
  arch/x86/kvm/lapic.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +0 -0
index 91c2dc9f198df..5f935e7a09566 100644
@@ -2306,7 +2306,12 @@ void kvm_apic_update_apicv(struct kvm_vcpu *vcpu)
  		apic->irr_pending = true;
  		apic->isr_count = 1;
  	} else {

arch/x86/kvm/lapic.c void kvm_apic_update_apicv(struct kvm_vcpu *vcpu) · line 2317

Quoted 10 lines
arch/x86/kvm/lapic.c +6 -1
-		apic->irr_pending = (apic_search_irr(apic) != -1);
+		/*
+		 * Don't clear irr_pending, searching the IRR can race with
+		 * updates from the CPU as APICv is still active from hardware's
+		 * perspective.  The flag will be cleared as appropriate when
+		 * KVM injects the interrupt.
+		 */
  		apic->isr_count = count_vectors(apic->regs + APIC_ISR);
  	}
  }

Paolo

maintainer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH MANUALSEL 5.15 2/2] KVM: x86: nSVM: deal with L1 hypervisor that intercepts interrupts but lets L2 control them

In reply to Sasha Levin
Quoted 80 lines · Sasha Levin

From: Maxim Levitsky <[email protected]>

[ Upstream commit 2b0ecccb55310a4b8ad5d59c703cf8c821be6260 ]

Fix a corner case in which the L1 hypervisor intercepts interrupts (INTERCEPT_INTR) and either doesn't set virtual interrupt masking (V_INTR_MASKING) or enters a nested guest with EFLAGS.IF disabled prior to the entry.

In this case, despite the fact that L1 intercepts the interrupts, KVM still needs to set up an interrupt window to wait before injecting the INTR vmexit.

Currently the KVM instead enters an endless loop of 'req_immediate_exit'.

Exactly the same issue also happens for SMIs and NMI. Fix this as well.

Note that on VMX, this case is impossible as there is only 'vmexit on external interrupts' execution control which either set, in which case both host and guest's EFLAGS.IF
are ignored, or not set, in which case no VMexits are delivered.

Message-Id: <[email protected]>

---
  arch/x86/kvm/svm/svm.c | 17 +++++++++++++----
  1 file changed, 13 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/svm.c +11 -3
index f05aa7290267d..aeee6df523af9 100644
@@ -3465,11 +3465,13 @@ static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
  	if (svm->nested.nested_run_pending)
  		return -EBUSY;
 
+	if (svm_nmi_blocked(vcpu))
+		return 0;
+
  	/* An NMI must not be injected into L2 if it's supposed to VM-Exit.  */
  	if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
  		return -EBUSY;
-
-	return !svm_nmi_blocked(vcpu);
+	return 1;
  }
 
  static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
@@ -3521,9 +3523,13 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
  static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
  {
  	struct vcpu_svm *svm = to_svm(vcpu);
+
  	if (svm->nested.nested_run_pending)
  		return -EBUSY;
 
+	if (svm_interrupt_blocked(vcpu))
+		return 0;
+
  	/*
  	 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
  	 * e.g. if the IRQ arrived asynchronously after checking nested events.
@@ -3531,7 +3537,7 @@ static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
  	if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
  		return -EBUSY;
 
-	return !svm_interrupt_blocked(vcpu);
+	return 1;
  }
 
  static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
@@ -4286,11 +4292,14 @@ static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
  	if (svm->nested.nested_run_pending)
  		return -EBUSY;
 
+	if (svm_smi_blocked(vcpu))
+		return 0;

arch/x86/kvm/svm/svm.c static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection) · line 4305

Quoted 10 lines
arch/x86/kvm/svm/svm.c +2 -1
+
  	/* An SMI must not be injected into L2 if it's supposed to VM-Exit.  */
  	if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
  		return -EBUSY;
 
-	return !svm_smi_blocked(vcpu);
+	return 1;
  }
 
  static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)

Paolo