Skip to contentSkip to conversation

KVM: x86: Fix physical APIC map routing for aliased xAPIC IDs

PATCH Applied Alejandro Jimenez 2 participants 4 messages Nov 9 – Nov 13

Subsystems (5)
Documentation x86 KVM Kselftest +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover KVM: x86: Fix physical APIC map routing for aliased xAPIC IDs

Fix physical APIC map routing when an xAPIC-mode vCPU with an ID above 0xff is created before a vCPU whose ID matches its truncated xAPIC ID.
With KVM_X2APIC_API_USE_32BIT_IDS enabled, the existing alias entry in the xAPIC range can prevent the lower-ID vCPU from being reached via its xAPIC ID, and result in incorrect delivery of INIT/SIPI during CPU hotplug.

Following Sean's proposal [1], always allow the vCPU whose ID exactly matches the aliased xAPIC ID to install an entry in the map, even if one is already present. The full-ID (i.e. x2APIC ID) routes are still used to reach the higher-ID vCPUs in xAPIC mode, preserving KVM hotplug behavior.

The standalone selftest covers both creation orders and checks delivery to both the aliased xAPIC ID ("low") and the full x2APIC ID ("high") destinations. Example output on an unpatched kernel:

ok 1 Destination 0x1d: Lower-ID vCPU 29 created first. # Expected IRQ only on vCPU 29.
# Higher-ID vCPU 285: IRQ vector 0xe0 not delivered. # Lower-ID vCPU 29: IRQ vector 0xe0 delivered.
ok 2 Destination 0x11d: Lower-ID vCPU 29 created first. # Expected IRQ only on vCPU 285.
# Higher-ID vCPU 285: IRQ vector 0xe1 delivered.
# Lower-ID vCPU 29: IRQ vector 0xe1 not delivered. not ok 3 Destination 0x1d: Higher-ID vCPU 285 created first. # Expected IRQ only on vCPU 29.
# Higher-ID vCPU 285: IRQ vector 0xe0 delivered.
# Lower-ID vCPU 29: IRQ vector 0xe0 not delivered. ok 4 Destination 0x11d: Higher-ID vCPU 285 created first. # Expected IRQ only on vCPU 285.
# Higher-ID vCPU 285: IRQ vector 0xe1 delivered.
# Lower-ID vCPU 29: IRQ vector 0xe1 not delivered. # Totals: pass:3 fail:1 xfail:0 xpass:0 skip:0 error:0

Note the third test case fails because vCPU 285 was created before vCPU 29.

Changes since v1 [2]:
- Prefer an exact vCPU ID match instead of selecting the lowest vCPU ID, and update the errata wording.
- Move the selftest out of recalc_apic_map_test into a standalone apic_id_routing_test.
- Cover both creation orders, and check delivery via the higher vCPU's full x2APIC ID.
- Use kselftest, allowing the full-ID test to run even when an earlier alias check fails in unpatched kernels.

Thank you,
Alejandro

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

Alejandro Jimenez (2):
KVM: x86: Prefer exact vCPU ID matches for physical APIC map aliases
KVM: selftests: Test exact vCPU ID matching for aliased xAPIC IDs

 Documentation/virt/kvm/x86/errata.rst         |   9 +-
 arch/x86/kvm/lapic.c                          |   7 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/x86/apic_id_routing_test.c  | 217 ++++++++++++++++++
 4 files changed, 227 insertions(+), 7 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/apic_id_routing_test.c

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/2 KVM: x86: Prefer exact vCPU ID matches for physical APIC map aliases

When KVM_X2APIC_API_USE_32BIT_IDS is enabled, KVM allows events to target a vCPU by its x2APIC ID even while the vCPU is in xAPIC mode. The optimized physical APIC map also creates an entry for its truncated xAPIC ID.

If a vCPU with an ID > 0xff is created before a vCPU whose ID matches that truncated xAPIC ID, the map retains the initial entry and it is not updated to allow reaching the "low" vCPU ID via its xAPIC ID. This can result in INIT/SIPI addressed to the xAPIC ID being delivered to the wrong vCPU.

Allow a vCPU in xAPIC mode to replace an existing entry when its vCPU ID exactly matches the xAPIC ID. This ensures vCPUs are reachable via their non-truncated xAPIC IDs, regardless of creation order, and preserves the map entries using the full x2APIC IDs for vCPUs that require them.

Update the errata wording to describe this behavior, instead of promising delivery to the vCPU with the lowest ID.

Fixes 5bd5db385b3e ("KVM: x86: allow hotplug of VCPU with APIC ID over 0xff")
Assisted-by Codex:gpt-6-astra
---
 Documentation/virt/kvm/x86/errata.rst | 9 +++++----
 arch/x86/kvm/lapic.c                  | 7 ++++---
 2 files changed, 9 insertions(+), 7 deletions(-)
Documentation/virt/kvm/x86/errata.rst +5 -4
index a9cf0e004651..58a7adbb221a 100644
@@ -63,10 +63,11 @@ When KVM_X2APIC_API_USE_32BIT_IDS is enabled, KVM activates a hack/quirk that
 allows sending events to a single vCPU using its x2APIC ID even if the target
 vCPU has legacy xAPIC enabled, e.g. to bring up hotplugged vCPUs via INIT-SIPI
 on VMs with > 255 vCPUs.  A side effect of the quirk is that, if multiple vCPUs
-have the same physical APIC ID, KVM will deliver events targeting that APIC ID
-only to the vCPU with the lowest vCPU ID.  If KVM_X2APIC_API_USE_32BIT_IDS is
-not enabled, KVM follows x86 architecture when processing interrupts (all vCPUs
-matching the target APIC ID receive the interrupt).
+have the same physical legacy xAPIC ID, KVM may deliver events targeting that
+xAPIC ID only to the vCPU whose vCPU ID matches the aliased x2APIC ID. If
+KVM_X2APIC_API_USE_32BIT_IDS is not enabled, KVM follows x86 architecture when
+processing interrupts (all vCPUs matching the target APIC ID receive the
+interrupt).
 
 MTRRs
 -----
arch/x86/kvm/lapic.c +4 -3
index ecf611845fb1..e6bd9be0a107 100644
@@ -297,8 +297,8 @@ static int kvm_recalculate_phys_map(struct kvm_apic_map *new,
 	 * Apply KVM's hotplug hack if userspace has enable 32-bit APIC IDs.
 	 * Allow sending events to vCPUs by their x2APIC ID even if the target
 	 * vCPU is in legacy xAPIC mode, and silently ignore aliased xAPIC IDs
-	 * (the x2APIC ID is truncated to 8 bits, causing IDs > 0xff to wrap
-	 * and collide).
+	 * but make sure the non-truncated xAPIC ID always "wins" (the x2APIC
+	 * ID is truncated to 8 bits, causing IDs > 0xff to wrap and collide).
 	 *
 	 * Honor the architectural (and KVM's non-optimized) behavior if
 	 * userspace has not enabled 32-bit x2APIC IDs.  Each APIC is supposed
@@ -312,7 +312,8 @@ static int kvm_recalculate_phys_map(struct kvm_apic_map *new,
 		if (apic_x2apic_mode(apic) || x2apic_id > 0xff)
 			new->phys_map[x2apic_id] = apic;
 
-		if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
+		if (!apic_x2apic_mode(apic) &&
+		    (!new->phys_map[xapic_id] || vcpu->vcpu_id == xapic_id))
 			new->phys_map[xapic_id] = apic;
 	} else {
 		/*
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/2 KVM: selftests: Test exact vCPU ID matching for aliased xAPIC IDs

In reply to Alejandro Jimenez

When KVM_X2APIC_API_USE_32BIT_IDS is enabled, KVM's documented behavior has the side effect of delivering events targeting an aliased physical APIC ID only to the vCPU whose vCPU ID matches the aliased x2APIC ID. This behavior should be consistent regardless of vCPU creation order.

Add a test using two vCPU IDs that alias to the same xAPIC ID e.g. vCPU IDs 29 and 285, which both resolve to xAPIC ID 29. Exercise both creation orders in separate VMs to detect selection based on creation order instead of vCPU ID.

Inject a physical, fixed-delivery MSI and verify that only the vCPU with the ID matching the destination xAPIC ID has the vector pending. The LAPIC state is retrieved without running either vCPU, so the interrupt is not consumed before the check.

Additionally, target the higher-ID vCPU using its full x2APIC ID to verify that it remains reachable in xAPIC mode.

Assisted-by Codex:gpt-6-astra
---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/x86/apic_id_routing_test.c  | 217 ++++++++++++++++++
 2 files changed, 218 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/apic_id_routing_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 17a4256e9081..4c0bd5da170f 100644
@@ -141,6 +141,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
 TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_nested_la57_state_test
 TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test
+TEST_GEN_PROGS_x86 += x86/apic_id_routing_test
 TEST_GEN_PROGS_x86 += x86/xapic_ipi_test
 TEST_GEN_PROGS_x86 += x86/xapic_state_test
 TEST_GEN_PROGS_x86 += x86/xapic_tpr_test
tools/testing/selftests/kvm/x86/apic_id_routing_test.c new file +217 -0

217 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer Applied
#3 lore raw
In reply to Alejandro Jimenez
Quoted 6 lines · Alejandro Jimenez

Fix physical APIC map routing when an xAPIC-mode vCPU with an ID above 0xff is created before a vCPU whose ID matches its truncated xAPIC ID.
With KVM_X2APIC_API_USE_32BIT_IDS enabled, the existing alias entry in the xAPIC range can prevent the lower-ID vCPU from being reached via its xAPIC ID, and result in incorrect delivery of INIT/SIPI during CPU hotplug.

Quoted 6 lines

Following Sean's proposal [1], always allow the vCPU whose ID exactly matches the aliased xAPIC ID to install an entry in the map, even if one is already present. The full-ID (i.e. x2APIC ID) routes are still used to reach the higher-ID vCPUs in xAPIC mode, preserving KVM hotplug behavior.

[...]

Applied to kvm-x86 misc, thanks!

[1/2] KVM: x86: Prefer exact vCPU ID matches for physical APIC map aliases

      https://github.com/kvm-x86/linux/commit/a64b916ef22f
[2/2] KVM: selftests: Test exact vCPU ID matching for aliased xAPIC IDs
      https://github.com/kvm-x86/linux/commit/a6d590ba95ef

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