KVM: x86: allow hotplug of VCPU with APIC ID over 0xff
authorRadim Krčmář <rkrcmar@redhat.com>
Thu, 15 Dec 2016 17:06:48 +0000 (18:06 +0100)
committerRadim Krčmář <rkrcmar@redhat.com>
Mon, 9 Jan 2017 13:47:48 +0000 (14:47 +0100)
LAPIC after reset is in xAPIC mode, which poses a problem for hotplug of
VCPUs with high APIC ID, because reset VCPU is waiting for INIT/SIPI,
but there is no way to uniquely address it using xAPIC.

From many possible options, we chose the one that also works on real
hardware: accepting interrupts addressed to LAPIC's x2APIC ID even in
xAPIC mode.

KVM intentionally differs from real hardware, because real hardware
(Knights Landing) does just "x2apic_id & 0xff" to decide whether to
accept the interrupt in xAPIC mode and it can deliver one interrupt to
more than one physical destination, e.g. 0x123 to 0x123 and 0x23.

Fixes: 682f732ecf73 ("KVM: x86: bump MAX_VCPUS to 288")
Reviewed-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/lapic.c

index 3ebef53d20a0059c6698ed0ec144fa5ba15fae01..7e9ac4606279a913a0f7d7cb8a9118b5f4a96985 100644 (file)
@@ -189,15 +189,26 @@ static void recalculate_apic_map(struct kvm *kvm)
                struct kvm_lapic *apic = vcpu->arch.apic;
                struct kvm_lapic **cluster;
                u16 mask;
-               u32 ldr, aid;
+               u32 ldr;
+               u8 xapic_id;
+               u32 x2apic_id;
 
                if (!kvm_apic_present(vcpu))
                        continue;
 
-               aid = apic_x2apic_mode(apic) ? kvm_x2apic_id(apic)
-                                            : kvm_xapic_id(apic);
-               if (aid <= new->max_apic_id)
-                       new->phys_map[aid] = apic;
+               xapic_id = kvm_xapic_id(apic);
+               x2apic_id = kvm_x2apic_id(apic);
+
+               /* Hotplug hack: see kvm_apic_match_physical_addr(), ... */
+               if ((apic_x2apic_mode(apic) || x2apic_id > 0xff) &&
+                               x2apic_id <= new->max_apic_id)
+                       new->phys_map[x2apic_id] = apic;
+               /*
+                * ... xAPIC ID of VCPUs with APIC ID > 0xff will wrap-around,
+                * prevent them from masking VCPUs with APIC ID <= 0xff.
+                */
+               if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
+                       new->phys_map[xapic_id] = apic;
 
                ldr = kvm_lapic_get_reg(apic, APIC_LDR);
 
@@ -604,6 +615,15 @@ static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
        if (apic_x2apic_mode(apic))
                return mda == kvm_x2apic_id(apic);
 
+       /*
+        * Hotplug hack: Make LAPIC in xAPIC mode also accept interrupts as if
+        * it were in x2APIC mode.  Hotplugged VCPUs start in xAPIC mode and
+        * this allows unique addressing of VCPUs with APIC ID over 0xff.
+        * The 0xff condition is needed because writeable xAPIC ID.
+        */
+       if (kvm_x2apic_id(apic) > 0xff && mda == kvm_x2apic_id(apic))
+               return true;
+
        return mda == kvm_xapic_id(apic);
 }