[RFC PATCH 26/26] KVM: x86/pmu: Allow emulation in the fastpath with a mediated PMU

From: Yosry Ahmed

Date: Mon Nov 23 2026 - 19:25:28 EST


Effectively revert commit f7a65e58d643 ("KVM: x86/pmu: Disallow
emulation in the fastpath if mediated PMCs are active"), as KVM no
longer relies on its software copies of PMU state being consistent with
hardware while the guest context is loaded.

KVM now accesses PMU registers in hardware if needed with the guest
context loaded, and can therefore emulate instructions correctly in the
fastpath with a mediated PMU. Specifically, KVM writes to hardware when
incrementing counters or setting bits in GLOBAL_STATUS if the guest
context is loaded.

When emulating an instruction in the fastpath, KVM increments PMCs that
count instructions with the guest context loaded. KVM now updates
counters and GLOBAL_STATUS in hardware in that case.
kvm_pmu_trigger_event() reads GLOBAL_CTRL from software to determine
which counters to increment, but the software copy is up to date even
when the guest context is loaded: either guest writes are intercepted,
or it's synced from hardware after every VM-Exit

Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
arch/x86/kvm/msrs.c | 3 ---
arch/x86/kvm/pmu.h | 10 ----------
arch/x86/kvm/x86.c | 6 ------
3 files changed, 19 deletions(-)

diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c
index c58889cf36f55..9f2172060d331 100644
--- a/arch/x86/kvm/msrs.c
+++ b/arch/x86/kvm/msrs.c
@@ -924,9 +924,6 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_msr_write);

static fastpath_t __handle_fastpath_wrmsr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
{
- if (!kvm_pmu_is_fastpath_emulation_allowed(vcpu))
- return EXIT_FASTPATH_NONE;
-
switch (msr) {
case APIC_BASE_MSR + (APIC_ICR >> 4):
if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic) ||
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 565e09b23c498..5953f3e8a5d45 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -308,16 +308,6 @@ static inline bool pmc_is_globally_enabled(struct kvm_pmc *pmc)
return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl);
}

-static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu)
-{
- struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
-
- return !kvm_vcpu_has_mediated_pmu(vcpu) ||
- !bitmap_intersects(pmu->pmc_counting_instructions,
- (unsigned long *)&pmu->global_ctrl,
- X86_PMC_IDX_MAX);
-}
-
void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);
int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index ca2a58079b019..c1469a64c880d 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -831,9 +831,6 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_invd);

fastpath_t handle_fastpath_invd(struct kvm_vcpu *vcpu)
{
- if (!kvm_pmu_is_fastpath_emulation_allowed(vcpu))
- return EXIT_FASTPATH_NONE;
-
if (!kvm_emulate_invd(vcpu))
return EXIT_FASTPATH_EXIT_USERSPACE;

@@ -8780,9 +8777,6 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_halt);

fastpath_t handle_fastpath_hlt(struct kvm_vcpu *vcpu)
{
- if (!kvm_pmu_is_fastpath_emulation_allowed(vcpu))
- return EXIT_FASTPATH_NONE;
-
if (!kvm_emulate_halt(vcpu))
return EXIT_FASTPATH_EXIT_USERSPACE;

--
2.56.1.809.ga0b61035b5-goog