[RFC PATCH 11/26] KVM: x86/pmu: Move PMC MSR and type helpers earlier

From: Yosry Ahmed

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


Move {fixed_counter,gp_counter,gp_eventsel}_msr() above
pmc_write_counter(), and pmc_is_{gp,fixed}() above pmc_read_counter(),
in preparation for using them to access PMCs in hardware while the
guest's mediated PMU context is loaded.

No functional change intended.

Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
arch/x86/kvm/pmu.c | 30 +++++++++++++++---------------
arch/x86/kvm/pmu.h | 20 ++++++++++----------
2 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index eef2f792dc9e8..4d170abb2b384 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -404,6 +404,21 @@ static void pmc_update_sample_period(struct kvm_pmc *pmc)
get_sample_period(pmc, pmc->counter));
}

+static __always_inline u32 fixed_counter_msr(u32 idx)
+{
+ return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
+}
+
+static __always_inline u32 gp_counter_msr(u32 idx)
+{
+ return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
+}
+
+static __always_inline u32 gp_eventsel_msr(u32 idx)
+{
+ return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
+}
+
void pmc_write_counter(struct kvm_pmc *pmc, u64 val)
{
if (kvm_vcpu_has_mediated_pmu(pmc->vcpu)) {
@@ -1304,21 +1319,6 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)
return r;
}

-static __always_inline u32 fixed_counter_msr(u32 idx)
-{
- return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
-}
-
-static __always_inline u32 gp_counter_msr(u32 idx)
-{
- return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
-}
-
-static __always_inline u32 gp_eventsel_msr(u32 idx)
-{
- return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE;
-}
-
static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 090c9bbb74f48..ff4b6f15b915c 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -127,6 +127,16 @@ static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
return pmu->counter_bitmask[pmc->type];
}

+static inline bool pmc_is_gp(struct kvm_pmc *pmc)
+{
+ return pmc->type == KVM_PMC_GP;
+}
+
+static inline bool pmc_is_fixed(struct kvm_pmc *pmc)
+{
+ return pmc->type == KVM_PMC_FIXED;
+}
+
static inline u64 pmc_read_counter(struct kvm_pmc *pmc)
{
u64 counter, enabled, running;
@@ -145,16 +155,6 @@ static inline u64 pmc_read_counter(struct kvm_pmc *pmc)

void pmc_write_counter(struct kvm_pmc *pmc, u64 val);

-static inline bool pmc_is_gp(struct kvm_pmc *pmc)
-{
- return pmc->type == KVM_PMC_GP;
-}
-
-static inline bool pmc_is_fixed(struct kvm_pmc *pmc)
-{
- return pmc->type == KVM_PMC_FIXED;
-}
-
static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu,
u64 data)
{
--
2.56.1.809.ga0b61035b5-goog