From: David Woodhouse <[email protected]>

With APICv live on plane-0 vCPUs while their plane-1 twins use
software injection, the twins trip the APICv coherency checks: stale
vcpu-vs-VM activation state at vcpu_enter_guest(), and timer interrupt
injection against a vCPU that is not the running one. Cross-plane
event delivery depends on software-injection bookkeeping that hardware
acceleration bypasses.

Add APICV_INHIBIT_REASON_PLANES, set it when a plane above 0 is
created, and include it in the VMX and AVIC required-inhibit masks;
__kvm_set_or_clear_apicv_inhibit() silently ignores a reason the
vendor mask does not carry. Drop the inhibit again when the
KVM_CREATE_PLANE error path destroys the last nonzero plane of a live
VM; at VM teardown plane 0 is already gone and the update is skipped.

Signed-off-by: David Woodhouse <[email protected]>
---
 arch/x86/kvm/svm/svm.h  |  3 ++-
 arch/x86/kvm/vmx/main.c |  3 ++-
 arch/x86/kvm/x86.c      | 41 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.h      |  8 ++++++++
 4 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 2b7663722be6..e7fced399e05 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -982,7 +982,8 @@ extern struct kvm_x86_nested_ops svm_nested_ops;
        BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) |    \
        BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) |  \
        BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED) |  \
-       BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG)   \
+       BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG) | \
+       BIT(APICV_INHIBIT_REASON_PLANES)                \
 )
 
 bool __init avic_hardware_setup(void);
diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c
index 2d84910df724..14d21d02bff4 100644
--- a/arch/x86/kvm/vmx/main.c
+++ b/arch/x86/kvm/vmx/main.c
@@ -1004,7 +1004,8 @@ static int vt_handle_exit(struct kvm_vcpu *vcpu,
         BIT(APICV_INHIBIT_REASON_BLOCKIRQ) |                   \
         BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) |        \
         BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) |           \
-        BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED))
+        BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) | \
+        BIT(APICV_INHIBIT_REASON_PLANES))
 
 struct kvm_x86_ops vt_x86_ops __initdata = {
        .name = KBUILD_MODNAME,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index e8cefde75838..6d3d93e85a65 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -10247,12 +10247,53 @@ int kvm_arch_plane_init(struct kvm *kvm, struct 
kvm_plane *plane,
 {
        mutex_init(&plane->arch.apic_map_lock);
 
+       /*
+        * Cross-plane event delivery (forced VTL entry, twin kicks, NMI
+        * folding) relies on software injection via KVM_REQ_EVENT; hardware
+        * APIC virtualization on one twin would bypass the bookkeeping the
+        * other twin's plane depends on.
+        */
+       if (plane_level > 0)
+               kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PLANES);
+
        return 0;
 }
 
 void kvm_arch_plane_destroy(struct kvm_plane *plane)
 {
+       struct kvm *kvm = plane->kvm;
+       int i;
+
        kvfree(rcu_dereference_check(plane->arch.apic_map, 1));
+
+       if (plane->level == 0)
+               return;
+
+       /*
+        * Drop the APICv inhibit taken in kvm_arch_plane_init() when the
+        * last nonzero plane goes away.  Only the KVM_CREATE_PLANE error
+        * path destroys a nonzero plane while the VM is live; at VM
+        * teardown plane 0 is destroyed first and kvm->planes[0] is
+        * already NULL, which skips the update (the inhibit machinery
+        * needs plane 0 and SRCU, both gone by then).
+        */
+       if (!kvm->planes[0])
+               return;
+
+       /*
+        * Reaching here means a nonzero plane died while the VM is live,
+        * which only the KVM_CREATE_PLANE error path does - under
+        * kvm->lock.  A future kvm_destroy_planes() reordered to tear
+        * nonzero planes down before plane 0 would get here locklessly;
+        * catch that instead of silently poking freed APICv state.
+        */
+       lockdep_assert_held(&kvm->lock);
+
+       for (i = 1; i < KVM_MAX_PLANES; i++)
+               if (kvm->planes[i] && kvm->planes[i] != plane)
+                       return;
+
+       kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PLANES);
 }
 
 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
index 498974d38773..fb87ceaa9563 100644
--- a/arch/x86/kvm/x86.h
+++ b/arch/x86/kvm/x86.h
@@ -580,6 +580,13 @@ enum kvm_apicv_inhibit {
        APICV_INHIBIT_REASON_APIC_ID_MODIFIED,
        APICV_INHIBIT_REASON_APIC_BASE_MODIFIED,
 
+       /*
+        * APICv is inhibited when the VM has multiple planes (Hyper-V VTLs).
+        * Cross-plane event delivery relies on software injection; hardware
+        * acceleration on one twin would bypass it.
+        */
+       APICV_INHIBIT_REASON_PLANES,
+
        /******************************************************/
        /* INHIBITs that are relevant only to the AMD's AVIC. */
        /******************************************************/
@@ -637,6 +644,7 @@ enum kvm_apicv_inhibit {
        __APICV_INHIBIT_REASON(PHYSICAL_ID_ALIASED),    \
        __APICV_INHIBIT_REASON(APIC_ID_MODIFIED),       \
        __APICV_INHIBIT_REASON(APIC_BASE_MODIFIED),     \
+       __APICV_INHIBIT_REASON(PLANES),                 \
        __APICV_INHIBIT_REASON(NESTED),                 \
        __APICV_INHIBIT_REASON(IRQWIN),                 \
        __APICV_INHIBIT_REASON(PIT_REINJ),              \
-- 
2.50.1


Reply via email to