]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: x86/pmu: Register PMI handler for mediated vPMU
authorXiong Zhang <xiong.y.zhang@linux.intel.com>
Sat, 6 Dec 2025 00:16:55 +0000 (16:16 -0800)
committerSean Christopherson <seanjc@google.com>
Thu, 8 Jan 2026 19:52:06 +0000 (11:52 -0800)
Register a dedicated PMI handler with perf's callback when mediated PMU
support is enabled.  Perf routes PMIs that arrive while guest context is
loaded to the provided callback, by modifying the CPU's LVTPC to point at
a dedicated mediated PMI IRQ vector.

WARN upon receipt of a mediated PMI if there is no active vCPU, or if the
vCPU doesn't have a mediated PMU.  Even if a PMI manages to skid past
VM-Exit, it should never be delayed all the way beyond unloading the vCPU.
And while running vCPUs without a mediated PMU, the LVTPC should never be
wired up to the mediated PMI IRQ vector, i.e. should always be routed
through perf's NMI handler.

Signed-off-by: Xiong Zhang <xiong.y.zhang@linux.intel.com>
Signed-off-by: Mingwei Zhang <mizhang@google.com>
Tested-by: Xudong Hao <xudong.hao@intel.com>
Co-developed-by: Sean Christopherson <seanjc@google.com>
Tested-by: Manali Shukla <manali.shukla@amd.com>
Link: https://patch.msgid.link/20251206001720.468579-20-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/pmu.c
arch/x86/kvm/pmu.h
arch/x86/kvm/x86.c

index 131e24246b09e6e5f7ff35a93e9f7217e200e461..0b67920fa0697976c7a3f7acbb4e79cbbb46bd02 100644 (file)
@@ -157,6 +157,16 @@ void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
                perf_get_hw_event_config(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
 }
 
+void kvm_handle_guest_mediated_pmi(void)
+{
+       struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+
+       if (WARN_ON_ONCE(!vcpu || !kvm_vcpu_has_mediated_pmu(vcpu)))
+               return;
+
+       kvm_make_request(KVM_REQ_PMI, vcpu);
+}
+
 static inline void __kvm_perf_overflow(struct kvm_pmc *pmc, bool in_pmi)
 {
        struct kvm_pmu *pmu = pmc_to_pmu(pmc);
index a5c7c026b919a1717d8d3dfdba15f6acd0e1dfcf..9849c2bb720dfdd53063d0bf82cc3050ddef2e9e 100644 (file)
@@ -46,6 +46,8 @@ struct kvm_pmu_ops {
 
 void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops);
 
+void kvm_handle_guest_mediated_pmi(void);
+
 static inline bool kvm_pmu_has_perf_global_ctrl(struct kvm_pmu *pmu)
 {
        /*
index fb3a5e861553855c6c5fb52296dcbbac538e2761..1623afddff3b6105c6c65585f7386f686feca91f 100644 (file)
@@ -10111,7 +10111,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
                set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
 #endif
 
-       __kvm_register_perf_callbacks(ops->handle_intel_pt_intr, NULL);
+       __kvm_register_perf_callbacks(ops->handle_intel_pt_intr,
+                                     enable_mediated_pmu ? kvm_handle_guest_mediated_pmi : NULL);
 
        if (IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && tdp_mmu_enabled)
                kvm_caps.supported_vm_types |= BIT(KVM_X86_SW_PROTECTED_VM);