Re: [PATCH v4 06/21] KVM: x86/pmu: Drop nr_arch_{gp,fixed}_counters from kvm_pmu

From: Yosry Ahmed

Date: Wed Nov 18 2026 - 12:32:15 EST


> >> @@ -88,6 +88,18 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)
> >> return enable_mediated_pmu && vcpu_to_pmu(vcpu)->version;
> >> }
> >>
> >> +static inline u64 kvm_gp_pmc_mask(struct kvm_pmu *pmu)
> >> +{
> >> + return pmu->pmc_exists64 &
> >> + GENMASK_ULL(KVM_MAX_NR_GP_COUNTERS - 1, 0);
> >> +}
> >> +
> >> +static inline u64 kvm_fixed_pmc_mask(struct kvm_pmu *pmu)
> >> +{
> >> + return (pmu->pmc_exists64 >> KVM_FIXED_PMC_BASE_IDX) &
> >> + GENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);
> >
> > Why do we need to & with the
> > KVM_MAX_NR_GP_COUNTERS/KVM_MAX_NR_FIXED_COUNTERS mask here? Isn't
> > pmc_exists64 already clamped?
>
> For kvm_gp_pmc_mask(), masking with GENMASK_ULL(31, 0) is required to
> strip off the fixed counter bits [34:32] and bit 58.
>
> For kvm_fixed_pmc_mask(), bit 58 (INTEL_PMC_IDX_FIXED_VLBR) could show
> up at bit (58 - 32) = 26 in the returned fixed mask. So the mask is
> necessary to strip it off.

I lost track of it being shared, ignore this, sorry.

>
> >
> >> +}
> >> +
> >> static inline bool kvm_is_gp_pmc_supported(struct kvm_pmu *pmu, unsigned int idx)
> >> {
> >> return idx < KVM_MAX_NR_GP_COUNTERS &&
> > [..]
> >> diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
> >> index fadc466cd600..68d23c7877a2 100644
> >> --- a/arch/x86/kvm/vmx/pmu_intel.c
> >> +++ b/arch/x86/kvm/vmx/pmu_intel.c
> >> @@ -71,14 +71,14 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
> >>
> >> pmu->fixed_ctr_ctrl = data;
> >> pmu->fixed_ctr_ctrl_hw = data;
> >> - for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
> >> + kvm_for_each_fixed_counter(i, pmu) {
> >> u8 new_ctrl = fixed_ctrl_field(data, i);
> >> u8 old_ctrl = fixed_ctrl_field(old_fixed_ctr_ctrl, i);
> >>
> >> if (old_ctrl == new_ctrl)
> >> continue;
> >>
> >> - pmc = get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + i);
> >> + pmc = &pmu->fixed_counters[i];
> >
> > By bypassing the helper we lose the array_index_nospec(), right? Same
> > applies if we directly access the array elsewhere.
>
> Right, but array_index_nospec() is only needed when the index comes from
> untrusted guest input (like in get_fixed_pmc()).
>
> Here 'i' is an internal loop iterator bounded by
> KVM_MAX_NR_FIXED_COUNTERS and kvm_fixed_pmc_mask(). Direct indexing
> &pmu->fixed_counters[i] matches kvm_pmu_{load,put}_guest_pmcs() in pmu.c.

Ack.


> >> @@ -535,8 +541,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
> >> if (!pmu->version)
> >> return;
> >>
> >> - pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters,
> >> - kvm_pmu_cap.num_counters_gp);
> >> eax.split.bit_width = min_t(int, eax.split.bit_width,
> >> kvm_pmu_cap.bit_width_gp);
> >> pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(eax.split.bit_width) - 1;
> >> @@ -544,6 +548,14 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
> >> kvm_pmu_cap.events_mask_len);
> >> pmu->available_event_types = ~entry->ebx & (BIT_ULL(eax.split.mask_length) - 1);
> >>
> >> + nr_fixed_counters = min_t(int, edx.split.num_counters_fixed,
> >> + kvm_pmu_cap.num_counters_fixed);
> >> + fixed_mask = BIT_ULL(nr_fixed_counters) - 1;
> >
> > Probably clearer to move these right above where fixed_mask is used.
>
> Agreed. Additionally, fixed counters only exist starting in PMU v2, so
> it makes sense to move the fixed-counter setup down after the
> 'pmu->version == 1' check.
>
> The reason it was originally placed higher up is that in the upcoming
> patch ("Populate CPUID.0AH:ECX fixed-counter bitmap"), fixed_mask will
> also incorporate CPUID.0AH:ECX. But 'entry' is repurposed to look up
> leaf 7.0 right above the version == 1 check, which clobbers leaf 0AH.
>
> To move fixed_mask down cleanly, we need separate entry_10 and
> entry_7 variables. Since that adjustment is not tiny, I propose
> splitting it out into a separate preparation patch, not to mess up this
> already large patch.

Ack.

> >> @@ -575,8 +586,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
> >> INTEL_FIXED_0_USER |
> >> INTEL_FIXED_0_ENABLE_PMI);
> >>
> >> - counter_rsvd = ~((BIT_ULL(pmu->nr_arch_gp_counters) - 1) |
> >> - ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
> >> + counter_rsvd = ~(kvm_gp_pmc_mask(pmu) |
> >> + (kvm_fixed_pmc_mask(pmu) << KVM_FIXED_PMC_BASE_IDX));
> >
> > Can we use pmc_exists64 here like the AMD code?
>
> Unlike AMD, Intel's may have bit 58 (INTEL_PMC_IDX_FIXED_VLBR) set.
> counter_rsvd is sued to specifically restricts the unreserved bits to
> valid GP and fixed counters.

That's a shame :)

Thanks for patiently answering my questions.