[RFC PATCH 14/26] KVM: x86/pmu: Zero eventsel_hw for counters that can't count
From: Yosry Ahmed
Date: Mon Nov 23 2026 - 19:24:39 EST
When reprogramming a counter that can't count, zero eventsel_hw instead
of only clearing the enable bit, so that putting the guest context
doesn't need to clear its event selector in hardware. The guest's view
is unaffected, as reads return eventsel, and the counter doesn't count
either way.
Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
arch/x86/kvm/pmu.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 0a5299532c086..44d947b854b9c 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -576,7 +576,11 @@ static void kvm_mediated_pmu_reprogram_counter(struct kvm_pmc *pmc)
struct kvm_pmu *pmu = pmc_to_pmu(pmc);
if (pmc_is_gp(pmc)) {
- pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
+ /*
+ * Leave eventsel_hw zero for counters that can't count, so that
+ * they don't need to be cleared when putting the guest context.
+ */
+ pmc->eventsel_hw = 0;
if (allowed)
pmc->eventsel_hw = (pmc->eventsel | kvm_pmu_ops.EVENTSEL_HW_SET) &
~kvm_pmu_ops.EVENTSEL_HW_CLR;
--
2.56.1.809.ga0b61035b5-goog