Skip to contentSkip to conversation

KVM: x86: Drop deferred updates of EOI bitmaps

PATCH Applied Sean Christopherson 1 participant 5 messages Oct 27 – Nov 13

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

v3 cover KVM: x86: Drop deferred updates of EOI bitmaps

Stop deferring EOI bitmap updates. As pointed out by Amirmohammad, I missed this one when removing all the other cases where KVM defers an update until vmcs01 is active.

Patch 1 fixes a pre-existing bug Sashiko pointed out where KVM doesn't update the EOI bitmaps if APICv is temporarily inhibited.

v3: Add the aforementioned patch 1. [Sashiko]

v2: https://lore.kernel.org/all/[email protected]

v1: https://lore.kernel.org/all/[email protected]

Sean Christopherson (3):
KVM: VMX: Update EOI bitmaps even if APICv is (temporarily) inhibited
KVM: x86: Switch to vmcs01 to update EOI exiting bitmap on-demand if
L2 is active
KVM: x86: Consolidate invocations of kvm_x86_ops.load_eoi_exitmap()

 arch/x86/include/asm/kvm_host.h |  3 +--
 arch/x86/kvm/regs.h             |  6 ------
 arch/x86/kvm/vmx/vmx.c          |  4 +++-
 arch/x86/kvm/x86.c              | 27 +++++++--------------------
 4 files changed, 11 insertions(+), 29 deletions(-)

base-commit: 7e60162b16f9da0d2fa83da5c1723b6853a0ac1d

Signature · 1 line
2.56.0.307.g5fe7bd680c-goog
author maintainer
#1 lore raw

PATCH v3 1/3 KVM: VMX: Update EOI bitmaps even if APICv is (temporarily) inhibited

As KVM does with IRTEs for device posted interrupts, update the EOI bitmaps if APICv *can* be enabled for the vCPU, not just if APICv is *currently* enabled and active. I.e. keep the bitmaps up-to-date at all times so that KVM doesn't need to refresh the bitmaps if APICv is unhibited in the future.

In the worst case scenario, failure to update the EOI bitmaps means KVM could miss an EOI and effectively break level-triggered IRQs, e.g. if a GSI routing change occurred while APICv was inhibitied.

Fixes d62caabb41f3 ("kvm/x86: per-vcpu apicv deactivation support")
---
 arch/x86/kvm/vmx/vmx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +1 -1
index b341af4c6057..d8e66d67138f 100644
@@ -7079,7 +7079,7 @@ int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
 
 void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
 {
-	if (!kvm_vcpu_apicv_active(vcpu))
+	if (!lapic_in_kernel(vcpu) || !enable_apicv)
 		return;
 
 	vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
Signature · 1 line
2.56.0.307.g5fe7bd680c-goog
author maintainer
#2 lore raw

PATCH v3 2/3 KVM: x86: Switch to vmcs01 to update EOI exiting bitmap on-demand if L2 is active

In reply to Sean Christopherson

If KVM needs to update the EOI bitmap while L2 is running, temporarily load vmcs01 and immediately refresh the bitmap in vmcs01 instead of deferring the update until the next nested VM-Exit. I.e. give the EOI bitmap the same treatment as the many other updates that were previously deferred.

---
 arch/x86/include/asm/kvm_host.h |  3 +--
 arch/x86/kvm/regs.h             |  6 ------
 arch/x86/kvm/vmx/vmx.c          |  2 ++
 arch/x86/kvm/x86.c              | 13 -------------
 4 files changed, 3 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -2
index 0378577d9d8b..92aae551bfae 100644
@@ -113,7 +113,7 @@
 #define KVM_REQ_HV_RESET		KVM_ARCH_REQ(20)
 #define KVM_REQ_HV_EXIT			KVM_ARCH_REQ(21)
 #define KVM_REQ_HV_STIMER		KVM_ARCH_REQ(22)
-#define KVM_REQ_LOAD_EOI_EXITMAP	KVM_ARCH_REQ(23)
+/*#define FREE!!!  USE ME!!!		KVM_ARCH_REQ(23) */
 #define KVM_REQ_GET_NESTED_STATE_PAGES	KVM_ARCH_REQ(24)
 #define KVM_REQ_APICV_UPDATE \
 	KVM_ARCH_REQ_FLAGS(25, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
@@ -746,7 +746,6 @@ struct kvm_vcpu_arch {
 	u64 host_debugctl;
 	u64 apic_base;
 	struct kvm_lapic *apic;    /* kernel irqchip context */
-	bool load_eoi_exitmap_pending;
 	DECLARE_BITMAP(ioapic_handled_vectors, 256);
 	unsigned long apic_attention;
 	int32_t apic_arb_prio;
arch/x86/kvm/regs.h +0 -6
index 97c17b120a57..04d047f46753 100644
@@ -463,12 +463,6 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu)
 static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
 {
 	vcpu->arch.hflags &= ~HF_GUEST_MASK;
-
-	if (vcpu->arch.load_eoi_exitmap_pending) {
-		vcpu->arch.load_eoi_exitmap_pending = false;
-		kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
-	}
-
 	vcpu->stat.guest_mode = 0;
 }
 
arch/x86/kvm/vmx/vmx.c +2 -0
index d8e66d67138f..ba5575002538 100644
@@ -7082,6 +7082,8 @@ void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
 	if (!lapic_in_kernel(vcpu) || !enable_apicv)
 		return;
 
+	guard(vmx_vmcs01)(vcpu);
+
 	vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
 	vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
 	vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
arch/x86/kvm/x86.c +0 -13
index 32563a91fba9..0028d57e964a 100644
@@ -8062,17 +8062,6 @@ static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
 		kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
 #endif
 
-	if (is_guest_mode(vcpu))
-		vcpu->arch.load_eoi_exitmap_pending = true;
-	else
-		kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
-}
-
-static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
-{
-	if (!kvm_apic_hw_enabled(vcpu->arch.apic))
-		return;
-
 #ifdef CONFIG_KVM_HYPERV
 	if (to_hv_vcpu(vcpu)) {
 		u64 eoi_exit_bitmap[4];
@@ -8240,8 +8229,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		}
 		if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
 			vcpu_scan_ioapic(vcpu);
-		if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
-			vcpu_load_eoi_exitmap(vcpu);
 		if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
 			kvm_vcpu_reload_apic_access_page(vcpu);
 #ifdef CONFIG_KVM_HYPERV
Signature · 1 line
2.56.0.307.g5fe7bd680c-goog
author maintainer
#3 lore raw

PATCH v3 3/3 KVM: x86: Consolidate invocations of kvm_x86_ops.load_eoi_exitmap()

In reply to Sean Christopherson

Rework KVM's handling of EOI exit bitmap updates to consolidate the calls to vendor code so that it's easier to see that the Hyper-V path is doing the exact same thing, just with extra input to account for the SynIC vectors.

No functional change intended.

---
 arch/x86/kvm/x86.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +7 -7
index 0028d57e964a..483dac323c8e 100644
@@ -8047,6 +8047,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_inc_or_dec_irq_window_inhibit);
 
 static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
 {
+	unsigned long *eoi_exit_bitmap = vcpu->arch.ioapic_handled_vectors;
+	DECLARE_BITMAP(__eoi_exit_bitmap, 256) __maybe_unused;
+
 	if (!kvm_apic_present(vcpu))
 		return;
 
@@ -8064,17 +8067,14 @@ static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
 
 #ifdef CONFIG_KVM_HYPERV
 	if (to_hv_vcpu(vcpu)) {
-		u64 eoi_exit_bitmap[4];
-
-		bitmap_or((ulong *)eoi_exit_bitmap,
+		bitmap_or(__eoi_exit_bitmap,
 			  vcpu->arch.ioapic_handled_vectors,
 			  to_hv_synic(vcpu)->vec_bitmap, 256);
-		kvm_x86_call(load_eoi_exitmap)(vcpu, eoi_exit_bitmap);
-		return;
+
+		eoi_exit_bitmap = __eoi_exit_bitmap;
 	}
 #endif
-	kvm_x86_call(load_eoi_exitmap)(
-		vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors);
+	kvm_x86_call(load_eoi_exitmap)(vcpu, (u64 *)eoi_exit_bitmap);
 }
 
 void kvm_arch_guest_memory_reclaimed(struct kvm *kvm)
Signature · 1 line
2.56.0.307.g5fe7bd680c-goog
author maintainer Applied
#4 lore raw
In reply to Sean Christopherson
Quoted 8 lines · Sean Christopherson

Stop deferring EOI bitmap updates. As pointed out by Amirmohammad, I missed this one when removing all the other cases where KVM defers an update until vmcs01 is active.

Patch 1 fixes a pre-existing bug Sashiko pointed out where KVM doesn't update the EOI bitmaps if APICv is temporarily inhibited.

[...]

Applied to kvm-x86 misc, thanks!

[1/3] KVM: VMX: Update EOI bitmaps even if APICv is (temporarily) inhibited

      https://github.com/kvm-x86/linux/commit/224c5cbd9194
[2/3] KVM: x86: Switch to vmcs01 to update EOI exiting bitmap on-demand if L2 is active
      https://github.com/kvm-x86/linux/commit/70ab7673336b
[3/3] KVM: x86: Consolidate invocations of kvm_x86_ops.load_eoi_exitmap()
      https://github.com/kvm-x86/linux/commit/77eb3b1f9bf8

--
https://github.com/kvm-x86/linux/tree/next