[RFC PATCH 23/26] KVM: x86/pmu: Add stats for mediated PMU context switches

From: Yosry Ahmed

Date: Mon Nov 23 2026 - 19:24:37 EST


Add per-vCPU stats counting how many times the guest's mediated PMU
context is put, and why, mainly to help debug performance problems
caused by frequent guest context switches, e.g. due to host perf events
conflicting with the mediated PMU. The stats are also useful for
testing. Add:

- pmu_ctx_switches: all puts of the guest context.

- pmu_ctx_switches_vmexit: puts on VM-Exits, because perf has events
conflicting with the mediated PMU. Note, the guest context is only
put on slowpath VM-Exits, not on every VM-Exit.

- pmu_ctx_switches_reclaim: puts because perf reclaimed the mediated
PMU.

Don't add a stat for puts when the vCPU is put, e.g. when the vCPU task
is scheduled out or KVM_RUN returns to userspace, as it can be derived
from the other stats, and when exactly the vCPU is put is an
implementation detail.

Return whether the guest context was actually put from
kvm_mediated_pmu_put(), as it may already be put when perf reclaims the
mediated PMU. The guest context is always loaded on slowpath VM-Exits
for vCPUs with a mediated PMU, so WARN if it wasn't put there, and count
the put unconditionally.

Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
arch/x86/include/asm/kvm_host.h | 3 +++
arch/x86/kvm/pmu.c | 11 ++++++++---
arch/x86/kvm/pmu.h | 2 +-
arch/x86/kvm/x86.c | 9 +++++++--
4 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 3efef3415f8e8..366b00ffa37f4 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1477,6 +1477,9 @@ struct kvm_vcpu_stat {
u64 preemption_other;
u64 guest_mode;
u64 notify_window_exits;
+ u64 pmu_ctx_switches;
+ u64 pmu_ctx_switches_vmexit;
+ u64 pmu_ctx_switches_reclaim;
};

struct x86_instruction_info;
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 4879578c7d579..80229f3d477fb 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -230,7 +230,8 @@ void kvm_handle_mediated_pmu_reclaim(void)
if (WARN_ON_ONCE(!vcpu || !kvm_vcpu_has_mediated_pmu(vcpu)))
return;

- kvm_mediated_pmu_put(vcpu);
+ if (kvm_mediated_pmu_put(vcpu))
+ ++vcpu->stat.pmu_ctx_switches_reclaim;
}

static inline void __kvm_perf_overflow(struct kvm_pmc *pmc, bool in_pmi)
@@ -1489,13 +1490,14 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
}
}

-void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)
+/* Returns true if the guest context was loaded and is now put. */
+bool kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)
{
lockdep_assert_irqs_disabled();

if (!is_guest_mediated_pmu_loaded() ||
KVM_BUG_ON(!lapic_in_kernel(vcpu), vcpu->kvm))
- return;
+ return false;

/*
* Defer handling of PERF_GLOBAL_CTRL to vendor code. On Intel, it's
@@ -1508,4 +1510,7 @@ void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)
perf_put_guest_lvtpc();

perf_put_guest_context();
+
+ ++vcpu->stat.pmu_ctx_switches;
+ return true;
}
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 89807e7a3c771..565e09b23c498 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -333,7 +333,7 @@ void kvm_pmu_instruction_retired(struct kvm_vcpu *vcpu);
void kvm_pmu_branch_retired(struct kvm_vcpu *vcpu);
void kvm_pmu_sync_lvtpc(struct kvm_vcpu *vcpu, u32 lvtpc);
void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu);
-void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu);
+bool kvm_mediated_pmu_put(struct kvm_vcpu *vcpu);

bool is_vmware_backdoor_pmc(u32 pmc_idx);
bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 7fb954ed8e84b..ca2a58079b019 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -299,6 +299,9 @@ const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {
STATS_DESC_COUNTER(VCPU, preemption_other),
STATS_DESC_IBOOLEAN(VCPU, guest_mode),
STATS_DESC_COUNTER(VCPU, notify_window_exits),
+ STATS_DESC_COUNTER(VCPU, pmu_ctx_switches),
+ STATS_DESC_COUNTER(VCPU, pmu_ctx_switches_vmexit),
+ STATS_DESC_COUNTER(VCPU, pmu_ctx_switches_reclaim),
};

const struct kvm_stats_header kvm_vcpu_stats_header = {
@@ -8439,8 +8442,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)

kvm_load_host_pkru(vcpu);

- if (kvm_vcpu_has_mediated_pmu(vcpu) && perf_has_mediated_pmu_conflicts())
- kvm_mediated_pmu_put(vcpu);
+ if (kvm_vcpu_has_mediated_pmu(vcpu) && perf_has_mediated_pmu_conflicts()) {
+ WARN_ON_ONCE(!kvm_mediated_pmu_put(vcpu));
+ ++vcpu->stat.pmu_ctx_switches_vmexit;
+ }

/*
* Do this here before restoring debug registers on the host. And
--
2.56.1.809.ga0b61035b5-goog