[RFC PATCH 38/41] KVM: x86/pmu: Introduce PMU helper to increment counter

From: Xiong Zhang
Date: Fri Jan 26 2024 - 04:48:05 EST


From: Mingwei Zhang <mizhang@xxxxxxxxxx>

Introduce PMU helper to increment counter for passthrough PMU because it is
able to conveniently return the overflow condition instead of deferring the
overflow check to KVM_REQ_PMU in original implementation. In addition, this
helper function can hide architecture details.

Signed-off-by: Mingwei Zhang <mizhang@xxxxxxxxxx>
---
arch/x86/include/asm/kvm_host.h | 1 +
arch/x86/kvm/pmu.c | 15 +++++++++++++++
2 files changed, 16 insertions(+)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index b02688ed74f7..869de0d81055 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -501,6 +501,7 @@ struct kvm_pmc {
bool is_paused;
bool intr;
u64 counter;
+ u64 emulated_counter;
u64 prev_counter;
u64 eventsel;
u64 eventsel_hw;
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index e7ad97734705..7b0bac1ac4bf 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -434,6 +434,21 @@ static void reprogram_counter(struct kvm_pmc *pmc)
pmc->prev_counter = 0;
}

+static bool kvm_passthrough_pmu_incr_counter(struct kvm_pmc *pmc)
+{
+ if (!pmc->emulated_counter)
+ return false;
+
+ pmc->counter += pmc->emulated_counter;
+ pmc->emulated_counter = 0;
+ pmc->counter &= pmc_bitmask(pmc);
+
+ if (!pmc->counter)
+ return true;
+
+ return false;
+}
+
void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
--
2.34.1