vmx_handle_intel_pt_intr() handles ToPA PMIs for guests using Intel PT by setting the ToPA bit in KVM's software copy of the guest's GLOBAL_STATUS, and requesting a PMI for the guest. This doesn't work with a mediated PMU, as the guest's GLOBAL_STATUS lives in hardware while the guest context is loaded, and the software copy is overwritten when the guest context is put. The GLOBAL_STATUS accessors can't be used instead, as the handler runs in NMI context. The handler can't run for a vCPU with a mediated PMU though: while the guest context is loaded, the LVTPC routes PMIs to the mediated PMI vector, and perf ignores NMIs (see perf_event_nmi_handler()). NMIs from the guest are handled right after the VM-Exit, before the guest context can be put. WARN if it runs anyway. Don't bail after the WARN, as handling the PMI is still the best effort: the guest still gets a PMI, and setting the ToPA bit in the software copy is either correct (if the guest context is not loaded) or harmlessly lost. Bailing would drop the guest's PMI, and let perf handle the interrupt as a host Intel PT interrupt. Signed-off-by: Yosry Ahmed --- arch/x86/kvm/vmx/vmx.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 68fa251dda600..14d33b7ffcd93 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8484,6 +8484,16 @@ static unsigned int vmx_handle_intel_pt_intr(void) if (!vcpu || !kvm_handling_nmi_from_guest(vcpu)) return 0; + /* + * With a mediated PMU, LVTPC points at the mediated PMI vector while the + * guest runs, so a guest ToPA PMI should never arrive as an NMI. Also, + * hardware GLOBAL_STATUS is authoritative while the guest context is + * loaded, and pmu->global_status is overwritten when it is put, so the + * bit set below would be lost. The GLOBAL_STATUS accessors can't be used + * here either, as this runs in NMI context. + */ + WARN_ON_ONCE(kvm_vcpu_has_mediated_pmu(vcpu)); + kvm_make_request(KVM_REQ_PMI, vcpu); __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT, (unsigned long *)&vcpu->arch.pmu.global_status); -- 2.56.1.809.ga0b61035b5-goog