Skip to content

Commit 1c4ba72

Browse files
Xiong Zhangsean-jc
authored andcommitted
KVM: x86/pmu: Register PMI handler for mediated vPMU
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>
1 parent 9ba0bb4 commit 1c4ba72

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

arch/x86/kvm/pmu.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
157157
perf_get_hw_event_config(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
158158
}
159159

160+
void kvm_handle_guest_mediated_pmi(void)
161+
{
162+
struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
163+
164+
if (WARN_ON_ONCE(!vcpu || !kvm_vcpu_has_mediated_pmu(vcpu)))
165+
return;
166+
167+
kvm_make_request(KVM_REQ_PMI, vcpu);
168+
}
169+
160170
static inline void __kvm_perf_overflow(struct kvm_pmc *pmc, bool in_pmi)
161171
{
162172
struct kvm_pmu *pmu = pmc_to_pmu(pmc);

arch/x86/kvm/pmu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct kvm_pmu_ops {
4646

4747
void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops);
4848

49+
void kvm_handle_guest_mediated_pmi(void);
50+
4951
static inline bool kvm_pmu_has_perf_global_ctrl(struct kvm_pmu *pmu)
5052
{
5153
/*

arch/x86/kvm/x86.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10111,7 +10111,8 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
1011110111
set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
1011210112
#endif
1011310113

10114-
__kvm_register_perf_callbacks(ops->handle_intel_pt_intr, NULL);
10114+
__kvm_register_perf_callbacks(ops->handle_intel_pt_intr,
10115+
enable_mediated_pmu ? kvm_handle_guest_mediated_pmi : NULL);
1011510116

1011610117
if (IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && tdp_mmu_enabled)
1011710118
kvm_caps.supported_vm_types |= BIT(KVM_X86_SW_PROTECTED_VM);

0 commit comments

Comments
 (0)