Guest PMIs are delivered to KVM's mediated PMI handler while KVM handles IRQs that are pending after a VM-Exit, either because the PMI caused the VM-Exit, or because it was raised right before it. Guest counters don't count while host code runs (GLOBAL_CTRL is cleared on VM-Exit on Intel, and events are guest-only on AMD), so guest PMIs shouldn't arrive at any other time. Now that the guest context can stay loaded while host code runs with IRQs enabled, a PMI arriving outside of guest IRQ handling would most likely be raised by a host PMU that perf failed to account for as conflicting with the mediated PMU, and misrouted to the guest. WARN in that case to catch such bugs. In theory, the WARN could fire for a guest PMI that is delivered late, e.g. after KVM is done handling IRQs following a VM-Exit. Checking the guest's GLOBAL_STATUS for an overflow to rule this out doesn't work as the guest can clear it, making the WARN guest-triggerable. Such late guest PMIs haven't been observed when stress testing on 8 different platforms (Skylake, Cascade, Icelake, Sapphirerapids, Emeraldrapids, Graniterapids, Genoa, or Turin). Signed-off-by: Yosry Ahmed --- arch/x86/kvm/pmu.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 80229f3d477fb..b1f949b9e05b5 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -220,6 +220,14 @@ void kvm_handle_guest_mediated_pmi(void) if (WARN_ON_ONCE(!vcpu || !kvm_vcpu_has_mediated_pmu(vcpu))) return; + /* + * Guest PMIs are delivered while KVM handles IRQs that are pending after + * a VM-Exit, as guest counters don't count while host code runs. A PMI + * arriving at any other time was raised by a host PMU that perf failed + * to account for, and was misrouted to the guest. + */ + WARN_ON_ONCE(vcpu->arch.handling_intr_from_guest != KVM_HANDLING_IRQ); + kvm_make_request(KVM_REQ_PMI, vcpu); } -- 2.56.1.809.ga0b61035b5-goog