linux/arch/x86/kvm
Khushit Shah d2cfea67b1 KVM: x86: Add x2APIC "features" to control EOI broadcast suppression
[ Upstream commit 6517dfbcc9 ]

Add two flags for KVM_CAP_X2APIC_API to allow userspace to control support
for Suppress EOI Broadcasts when using a split IRQCHIP (I/O APIC emulated
by userspace), which KVM completely mishandles. When x2APIC support was
first added, KVM incorrectly advertised and "enabled" Suppress EOI
Broadcast, without fully supporting the I/O APIC side of the equation,
i.e. without adding directed EOI to KVM's in-kernel I/O APIC.

That flaw was carried over to split IRQCHIP support, i.e. KVM advertised
support for Suppress EOI Broadcasts irrespective of whether or not the
userspace I/O APIC implementation supported directed EOIs. Even worse,
KVM didn't actually suppress EOI broadcasts, i.e. userspace VMMs without
support for directed EOI came to rely on the "spurious" broadcasts.

KVM "fixed" the in-kernel I/O APIC implementation by completely disabling
support for Suppress EOI Broadcasts in commit 0bcc3fb95b ("KVM: lapic:
stop advertising DIRECTED_EOI when in-kernel IOAPIC is in use"), but
didn't do anything to remedy userspace I/O APIC implementations.

KVM's bogus handling of Suppress EOI Broadcast is problematic when the
guest relies on interrupts being masked in the I/O APIC until well after
the initial local APIC EOI. E.g. Windows with Credential Guard enabled
handles interrupts in the following order:
  1. Interrupt for L2 arrives.
  2. L1 APIC EOIs the interrupt.
  3. L1 resumes L2 and injects the interrupt.
  4. L2 EOIs after servicing.
  5. L1 performs the I/O APIC EOI.

Because KVM EOIs the I/O APIC at step #2, the guest can get an interrupt
storm, e.g. if the IRQ line is still asserted and userspace reacts to the
EOI by re-injecting the IRQ, because the guest doesn't de-assert the line
until step #4, and doesn't expect the interrupt to be re-enabled until
step #5.

Unfortunately, simply "fixing" the bug isn't an option, as KVM has no way
of knowing if the userspace I/O APIC supports directed EOIs, i.e.
suppressing EOI broadcasts would result in interrupts being stuck masked
in the userspace I/O APIC due to step #5 being ignored by userspace. And
fully disabling support for Suppress EOI Broadcast is also undesirable, as
picking up the fix would require a guest reboot, *and* more importantly
would change the virtual CPU model exposed to the guest without any buy-in
from userspace.

Add KVM_X2APIC_ENABLE_SUPPRESS_EOI_BROADCAST and
KVM_X2APIC_DISABLE_SUPPRESS_EOI_BROADCAST flags to allow userspace to
explicitly enable or disable support for Suppress EOI Broadcasts. This
gives userspace control over the virtual CPU model exposed to the guest,
as KVM should never have enabled support for Suppress EOI Broadcast without
userspace opt-in. Not setting either flag will result in legacy quirky
behavior for backward compatibility.

Disallow fully enabling SUPPRESS_EOI_BROADCAST when using an in-kernel
I/O APIC, as KVM's history/support is just as tragic.  E.g. it's not clear
that commit c806a6ad35 ("KVM: x86: call irq notifiers with directed EOI")
was entirely correct, i.e. it may have simply papered over the lack of
Directed EOI emulation in the I/O APIC.

Note, Suppress EOI Broadcasts is defined only in Intel's SDM, not in AMD's
APM. But the bit is writable on some AMD CPUs, e.g. Turin, and KVM's ABI
is to support Directed EOI (KVM's name) irrespective of guest CPU vendor.

Fixes: 7543a635aa ("KVM: x86: Add KVM exit for IOAPIC EOIs")
Closes: https://lore.kernel.org/kvm/7D497EF1-607D-4D37-98E7-DAF95F099342@nutanix.com
Cc: stable@vger.kernel.org
Suggested-by: David Woodhouse <dwmw2@infradead.org>
Signed-off-by: Khushit Shah <khushit.shah@nutanix.com>
Link: https://patch.msgid.link/20260123125657.3384063-1-khushit.shah@nutanix.com
[sean: clean up minor formatting goofs and fix a comment typo]
Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
History 2026-03-12 07:09:31 -04:00
..
mmu KVM: x86: Export KVM-internal symbols for sub-modules only 2025-09-30 13:40:02 -04:00
svm KVM: nSVM: Always use vmcb01 in VMLOAD/VMSAVE emulation 2026-03-04 07:21:05 -05:00
vmx KVM: x86: Explicitly configure supported XSS from {svm,vmx}_set_cpu_caps() 2026-02-11 13:41:40 +01:00
.gitignore KVM: x86: use a separate asm-offsets.c file 2022-11-09 12:10:17 -05:00
cpuid.c KVM: x86: Apply runtime updates to current CPUID during KVM_SET_CPUID{,2} 2026-01-02 12:57:21 +01:00
cpuid.h Merge branch 'kvm-tdx-initial' into HEAD 2025-04-07 07:36:33 -04:00
debugfs.c KVM: Get rid of return value from kvm_arch_create_vm_debugfs() 2024-02-23 21:44:58 +00:00
emulate.c - Remove a bunch of asm implementing condition flags testing in KVM's 2025-10-11 11:19:16 -07:00
fpu.h KVM: x86: Move FPU register accessors into fpu.h 2021-06-17 13:09:24 -04:00
hyperv.c KVM: x86: Export KVM-internal symbols for sub-modules only 2025-09-30 13:40:02 -04:00
hyperv.h KVM: x86: Drop superfluous kvm_hv_set_sint() => kvm_hv_synic_set_irq() wrapper 2025-06-20 13:52:43 -07:00
i8254.c Significant patch series in this pull request: 2025-08-03 16:23:09 -07:00
i8254.h KVM: x86: Move IRQ mask notifier infrastructure to I/O APIC emulation 2025-06-20 13:52:52 -07:00
i8259.c KVM: x86: Move IRQ mask notifier infrastructure to I/O APIC emulation 2025-06-20 13:52:52 -07:00
ioapic.c KVM: x86: Add x2APIC "features" to control EOI broadcast suppression 2026-03-12 07:09:31 -04:00
ioapic.h KVM: x86: Move IRQ mask notifier infrastructure to I/O APIC emulation 2025-06-20 13:52:52 -07:00
irq.c KVM: x86: Export KVM-internal symbols for sub-modules only 2025-09-30 13:40:02 -04:00
irq.h KVM: x86: Move kvm_irq_delivery_to_apic() from irq.c to lapic.c 2025-09-10 12:05:09 -07:00
Kconfig - Remove a bunch of asm implementing condition flags testing in KVM's 2025-10-11 11:19:16 -07:00
kvm-asm-offsets.c KVM: SVM: move MSR_IA32_SPEC_CTRL save/restore to assembly 2022-11-09 12:25:53 -05:00
kvm_cache_regs.h KVM: VMX: Make CR4.CET a guest owned bit 2025-09-23 10:03:09 -07:00
kvm_emulate.h KVM: x86: Don't (re)check L1 intercepts when completing userspace I/O 2025-08-19 10:53:40 -07:00
kvm_onhyperv.c KVM: x86: Export KVM-internal symbols for sub-modules only 2025-09-30 13:40:02 -04:00
kvm_onhyperv.h KVM: x86: Move Hyper-V partition assist page out of Hyper-V emulation context 2023-12-07 09:34:01 -08:00
lapic.c KVM: x86: Add x2APIC "features" to control EOI broadcast suppression 2026-03-12 07:09:31 -04:00
lapic.h KVM: x86: Add x2APIC "features" to control EOI broadcast suppression 2026-03-12 07:09:31 -04:00
Makefile KVM: x86: Fold irq_comm.c into irq.c 2025-06-20 13:52:52 -07:00
mmu.h KVM: x86/mmu: WARN on attempt to check permissions for Shadow Stack #PF 2025-09-23 09:16:53 -07:00
mtrr.c KVM: x86: drop x86.h include from cpuid.h 2024-11-01 09:22:23 -07:00
pmu.c KVM: x86/pmu: Don't try to get perf capabilities for hybrid CPUs 2025-10-10 14:25:12 -07:00
pmu.h Generic: 2025-10-06 12:37:34 -07:00
reverse_cpuid.h KVM: x86: Advertise support for the immediate form of MSR instructions 2025-08-19 11:59:47 -07:00
smm.c KVM: x86: Export KVM-internal symbols for sub-modules only 2025-09-30 13:40:02 -04:00
smm.h KVM: x86: Save and reload SSP to/from SMRAM 2025-09-23 09:11:22 -07:00
trace.h KVM: x86: Define AMD's #HV, #VC, and #SX exception vectors 2025-09-23 09:29:03 -07:00
tss.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
x86.c KVM: x86: Add x2APIC "features" to control EOI broadcast suppression 2026-03-12 07:09:31 -04:00
x86.h KVM: x86: Explicitly configure supported XSS from {svm,vmx}_set_cpu_caps() 2026-02-11 13:41:40 +01:00
xen.c KVM: x86/xen: Fix cleanup logic in emulation of Xen schedop poll hypercalls 2025-07-23 23:48:54 +02:00
xen.h KVM: x86: Update Xen TSC leaves during CPUID emulation 2025-02-25 07:09:55 -08:00