From: "Chen, Zide" <zide.chen@intel.com>
To: Yosry Ahmed <yosry@kernel.org>
Cc: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
kvm@vger.kernel.org, Jim Mattson <jmattson@google.com>,
Andi Kleen <ak@linux.intel.com>,
Stephane Eranian <eranian@google.com>,
linux-kernel@vger.kernel.org, Mingwei Zhang <mizhang@google.com>,
Das Sandipan <Sandipan.Das@amd.com>,
Shukla Manali <Manali.Shukla@amd.com>,
Dapeng Mi <dapeng1.mi@linux.intel.com>,
Xudong Hao <xudong.hao@intel.com>
Subject: Re: [PATCH v4 06/21] KVM: x86/pmu: Drop nr_arch_{gp,fixed}_counters from kvm_pmu
Date: Wed, 18 Nov 2026 11:25:28 -0600 [thread overview]
Message-ID: <77f19d29-224d-474d-afc2-25f0ae1bd6cc@intel.com> (raw)
In-Reply-To: <avz3U-w-CBtTM0uH@google.com>
On 11/17/2026 6:58 PM, Yosry Ahmed wrote:
> On Thu, Nov 12, 2026 at 01:32:12PM -0700, Zide Chen wrote:
>> From: Dapeng Mi <dapeng1.mi@linux.intel.com>
>>
>> Populate and use pmc_exists directly when walking guest-visible PMCs,
>> instead of relying on pmu->nr_arch_{gp,fixed}_counters as intermediate
>> state, in preparation for switching KVM's host PMU capabilities from
>> counter counts to counter bitmaps.
>>
>> Extend pmu->pmc_exists to a union so that the u64 alias (pmc_exists64)
>> is available for mask arithmetic operations. Introduce
>> kvm_{gp,fixed}_pmc_mask() to extract the respective counter bitmasks,
>> and provide the kvm_for_each_{gp,fixed}_counter() iterator macros to
>> replace open-coded counter index loops.
>>
>> Consolidate PMU MSR intercept setup in VMX and SVM into single loops.
>> In SVM, this also fixes legacy counter interception by re-enabling
>> intercepts if a vCPU's counter count is narrowed.
>>
>> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
>> Co-developed-by: Zide Chen <zide.chen@intel.com>
>> Signed-off-by: Zide Chen <zide.chen@intel.com>
> [..]
>> diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
>> index fbab4f5c9181..b312f80f5cda 100644
>> --- a/arch/x86/kvm/pmu.h
>> +++ b/arch/x86/kvm/pmu.h
>> @@ -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.
>
>> +}
>> +
>> 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.
>>
>> __set_bit(KVM_FIXED_PMC_BASE_IDX + i, pmu->pmc_in_use);
>> kvm_pmu_request_counter_reprogram(pmc);
> [..]
>> @@ -497,7 +501,7 @@ static void intel_pmu_enable_fixed_counter_bits(struct kvm_pmu *pmu, u64 bits)
>> {
>> int i;
>>
>> - for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
>> + kvm_for_each_fixed_counter(i, pmu)
>> pmu->fixed_ctr_ctrl_rsvd &= ~intel_fixed_bits_by_idx(i, bits);
>> }
>>
>> @@ -505,11 +509,13 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
>> {
>> struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
>> struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
>> + int nr_gp_counters, nr_fixed_counters;
>> struct kvm_cpuid_entry2 *entry;
>> union cpuid10_eax eax;
>> union cpuid10_edx edx;
>> u64 perf_capabilities;
>> u64 counter_rsvd;
>> + u64 fixed_mask;
>>
>> if (!lbr_desc)
>> return;
>> @@ -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.
>
>> +
>> + nr_gp_counters = min_t(int, eax.split.num_counters,
>> + kvm_pmu_cap.num_counters_gp);
>> + pmu->pmc_exists64 = BIT_ULL(nr_gp_counters) - 1;
>> +
>> entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
>> if (entry &&
>> (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
>> @@ -565,8 +577,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
>> if (pmu->version == 1)
>> return;
>>
>> - pmu->nr_arch_fixed_counters = min_t(int, edx.split.num_counters_fixed,
>> - kvm_pmu_cap.num_counters_fixed);
>> + pmu->pmc_exists64 |= fixed_mask << KVM_FIXED_PMC_BASE_IDX;
>> edx.split.bit_width_fixed = min_t(int, edx.split.bit_width_fixed,
>> kvm_pmu_cap.bit_width_fixed);
>> pmu->counter_bitmask[KVM_PMC_FIXED] = BIT_ULL(edx.split.bit_width_fixed) - 1;
>> @@ -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.
>
>> pmu->global_ctrl_rsvd = counter_rsvd;
>>
>> pmu->global_status_rsvd = pmu->global_ctrl_rsvd
>> @@ -591,7 +602,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
>> pmu->pebs_data_cfg_rsvd = ~0xff00000full;
>> intel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);
>> } else {
>> - pmu->pebs_enable_rsvd = ~(BIT_ULL(pmu->nr_arch_gp_counters) - 1);
>> + pmu->pebs_enable_rsvd = ~kvm_gp_pmc_mask(pmu);
>> }
>> }
>> }
next prev parent reply other threads:[~2026-11-18 17:25 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 20:32 [PATCH v4 00/21] KVM: x86/pmu: Add mediated vPMU PerfMon v5 support Zide Chen
2026-11-12 20:32 ` [PATCH v4 01/21] KVM: SVM: Drop counter MSR checks in amd_is_valid_msr() Zide Chen
2026-11-14 18:49 ` Yosry Ahmed
2026-11-16 19:02 ` Chen, Zide
2026-11-18 0:28 ` Yosry Ahmed
2026-11-12 20:32 ` [PATCH v4 02/21] KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions Zide Chen
2026-11-12 20:32 ` [PATCH v4 03/21] KVM: x86/pmu: Rename all_valid_pmc_idx to pmc_exists Zide Chen
2026-11-12 20:32 ` [PATCH v4 04/21] KVM: x86/pmu: Rename reserved_bits to eventsel_rsvd in kvm_pmu Zide Chen
2026-11-12 20:32 ` [PATCH v4 05/21] KVM: x86/pmu: Use pmc_exists in PMC lookup helpers Zide Chen
2026-11-14 18:53 ` Yosry Ahmed
2026-11-12 20:32 ` [PATCH v4 06/21] KVM: x86/pmu: Drop nr_arch_{gp,fixed}_counters from kvm_pmu Zide Chen
2026-11-17 17:23 ` Yosry Ahmed
2026-11-17 20:02 ` Chen, Zide
2026-11-18 0:59 ` Yosry Ahmed
2026-11-18 15:46 ` Yosry Ahmed
2026-11-18 15:58 ` Chen, Zide
2026-11-18 0:58 ` Yosry Ahmed
2026-11-18 17:25 ` Chen, Zide [this message]
2026-11-18 17:31 ` Yosry Ahmed
2026-11-12 20:32 ` [PATCH v4 07/21] KVM: x86/pmu: Expose kvm_host_pmu to vendor modules Zide Chen
2026-11-12 20:32 ` [PATCH v4 08/21] perf/x86: Plumb counter bitmap from x86_pmu to x86_pmu_cap Zide Chen
2026-11-12 20:32 ` [PATCH v4 09/21] KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities Zide Chen
2026-11-13 6:40 ` Mi, Dapeng
2026-11-13 18:22 ` Chen, Zide
2026-11-18 16:05 ` Yosry Ahmed
2026-11-12 20:32 ` [PATCH v4 10/21] perf/x86: Remove num_counters_{gp,fixed} from x86_pmu_capability Zide Chen
2026-11-12 20:32 ` [PATCH v4 11/21] KVM: x86/pmu: Document GLOBAL_STATUS_RESET passthrough erratum Zide Chen
2026-11-12 20:32 ` [PATCH v4 12/21] KVM: x86/pmu: Mark CondChgd reserved in global_status_rsvd Zide Chen
2026-11-12 20:32 ` [PATCH v4 13/21] KVM: x86/pmu: Emulate IA32_PERF_GLOBAL_STATUS_SET MSR Zide Chen
2026-11-18 16:12 ` Yosry Ahmed
2026-11-12 20:32 ` [PATCH v4 14/21] KVM: x86/pmu: Emulate IA32_PERF_GLOBAL_INUSE MSR Zide Chen
2026-11-12 20:32 ` [PATCH v4 15/21] KVM: x86/pmu: Clamp pmu->version to kvm_pmu_cap.version Zide Chen
2026-11-18 16:17 ` Yosry Ahmed
2026-11-12 20:32 ` [PATCH v4 16/21] KVM: x86/pmu: Populate CPUID.0AH:ECX fixed-counter bitmap Zide Chen
2026-11-12 20:32 ` [PATCH v4 17/21] KVM: x86/pmu: Factor out fixed counter control bit calculation Zide Chen
2026-11-18 16:20 ` Yosry Ahmed
2026-11-12 20:32 ` [PATCH v4 18/21] KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters Zide Chen
2026-11-12 20:32 ` [PATCH v4 19/21] KVM: x86/pmu: Ignore AnyThread bit if CPUID.0AH:EDX[15] is set Zide Chen
2026-11-12 20:32 ` [PATCH v4 20/21] KVM: x86/pmu: Advertise PerfMon version 5 on Intel hosts Zide Chen
2026-11-12 20:32 ` [PATCH v4 21/21] KVM: selftests: Support fixed counters bitmap in pmu_counters_test Zide Chen
2026-11-12 21:00 ` sashiko-bot
2026-11-13 20:17 ` Chen, Zide
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=77f19d29-224d-474d-afc2-25f0ae1bd6cc@intel.com \
--to=zide.chen@intel.com \
--cc=Manali.Shukla@amd.com \
--cc=Sandipan.Das@amd.com \
--cc=ak@linux.intel.com \
--cc=dapeng1.mi@linux.intel.com \
--cc=eranian@google.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mizhang@google.com \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=seanjc@google.com \
--cc=xudong.hao@intel.com \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox