[RFC PATCH 13/26] KVM: x86/pmu: Compute eventsel_hw from eventsel when reprogramming
From: Yosry Ahmed
Date: Mon Nov 23 2026 - 19:24:25 EST
Instead of only setting the enable bit in eventsel_hw when refreshing
the event filter, compute the entire eventsel_hw from eventsel when
reprogramming a counter that's allowed to count, and rename
kvm_mediated_pmu_refresh_event_filter() to
kvm_mediated_pmu_reprogram_counter() accordingly. As counters are only
allowed to count if the enable bit is set in eventsel, copying eventsel
also sets the enable bit.
Move forcing GUESTONLY and clearing HOSTONLY on AMD from
amd_pmu_set_msr() to the reprogramming path, using vendor-provided bits
to set and clear, and have amd_pmu_set_msr() record the guest's value
as-is like Intel. Writing an event selector always requests a reprogram,
which is handled before the guest context is loaded, so the value is
fixed up before it's written to hardware.
For counters that can't count, the event selector in hardware may now
differ in the GUESTONLY and HOSTONLY bits, which is benign as the enable
bit is cleared.
Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
arch/x86/kvm/pmu.c | 8 ++++----
arch/x86/kvm/pmu.h | 2 ++
arch/x86/kvm/svm/pmu.c | 5 +++--
3 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 6f82213058f14..0a5299532c086 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -570,7 +570,7 @@ static bool pmc_is_event_allowed(struct kvm_pmc *pmc)
return is_fixed_event_allowed(filter, pmc->idx);
}
-static void kvm_mediated_pmu_refresh_event_filter(struct kvm_pmc *pmc)
+static void kvm_mediated_pmu_reprogram_counter(struct kvm_pmc *pmc)
{
bool allowed = pmc_is_locally_enabled(pmc) && pmc_is_event_allowed(pmc);
struct kvm_pmu *pmu = pmc_to_pmu(pmc);
@@ -578,8 +578,8 @@ static void kvm_mediated_pmu_refresh_event_filter(struct kvm_pmc *pmc)
if (pmc_is_gp(pmc)) {
pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
if (allowed)
- pmc->eventsel_hw |= pmc->eventsel &
- ARCH_PERFMON_EVENTSEL_ENABLE;
+ pmc->eventsel_hw = (pmc->eventsel | kvm_pmu_ops.EVENTSEL_HW_SET) &
+ ~kvm_pmu_ops.EVENTSEL_HW_CLR;
} else {
u64 mask = intel_fixed_bits_by_idx(pmc->idx - KVM_FIXED_PMC_BASE_IDX, 0xf);
@@ -598,7 +598,7 @@ static int reprogram_counter(struct kvm_pmc *pmc)
u8 fixed_ctr_ctrl;
if (kvm_vcpu_has_mediated_pmu(pmu_to_vcpu(pmu))) {
- kvm_mediated_pmu_refresh_event_filter(pmc);
+ kvm_mediated_pmu_reprogram_counter(pmc);
return 0;
}
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index fe1baa50ebceb..462df3a9de491 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -44,6 +44,8 @@ struct kvm_pmu_ops {
void (*write_global_ctrl)(u64 global_ctrl);
const u64 EVENTSEL_EVENT;
+ const u64 EVENTSEL_HW_SET;
+ const u64 EVENTSEL_HW_CLR;
const int MAX_NR_GP_COUNTERS;
const int MIN_NR_GP_COUNTERS;
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index c18286545a7ac..232c4779e5a32 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -166,8 +166,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
data &= ~pmu->reserved_bits;
if (data != pmc->eventsel) {
pmc->eventsel = data;
- pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY) |
- AMD64_EVENTSEL_GUESTONLY;
+ pmc->eventsel_hw = data;
if (data & AMD64_EVENTSEL_HOST_GUEST_MASK)
__set_bit(pmc->idx, pmu->pmc_has_mode_specific_enables);
@@ -317,6 +316,8 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = {
.mediated_put = amd_mediated_pmu_put,
.EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT,
+ .EVENTSEL_HW_SET = AMD64_EVENTSEL_GUESTONLY,
+ .EVENTSEL_HW_CLR = AMD64_EVENTSEL_HOSTONLY,
.MAX_NR_GP_COUNTERS = KVM_MAX_NR_AMD_GP_COUNTERS,
.MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS,
--
2.56.1.809.ga0b61035b5-goog