Re: [PATCH v2 05/16] KVM: x86/pmu: Add PMC bitmap accessor helpers

From: Chen, Zide

Date: Wed Oct 28 2026 - 13:47:05 EST




On 10/28/2026 9:32 AM, Jim Mattson wrote:
> On Thu, Aug 27, 2026 at 3:48 PM Zide Chen <zide.chen@xxxxxxxxx> wrote:
>>
>> pmu->nr_arch_{gp,fixed}_counters is not able to represent that a PMU
>> may include non-contiguous GP or fixed counters.
>>
>> pmu->pmc_exists already holds a bitmap indicating both fixed and
>> general-purpose counters, and loops over valid counters can be done via
>> pmu->pmc_exists alone. Extend it to a union so that the u64 alias is
>> available for convenient mask arithmetic operations.
>>
>> Add the necessary helpers to prepare for bitmap-based PMC counter
>> implementation.
>>
>> No functional change intended.
>>
>> Co-developed-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
>> Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
>> Signed-off-by: Zide Chen <zide.chen@xxxxxxxxx>
>> ---
>> v2:
>> - Rename kvm_{gp,fixed}_pmc_supported() to kvm_is_{gp,fixed}_pmc_supported()
>> and simplify them by removing the temporary bitmap. (Sean)
>> - Split kvm_for_each_set_pmc_idx() to kvm_for_each_gp_counter()
>> and kvm_for_each_fixed_counter(). (Sean)
>> ---
>> arch/x86/include/asm/kvm_host.h | 5 +++-
>> arch/x86/kvm/pmu.h | 53 +++++++++++++++++++++++++++++----
>> 2 files changed, 51 insertions(+), 7 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
>> index 90d80483df7f..49a8a2e9cee0 100644
>> --- a/arch/x86/include/asm/kvm_host.h
>> +++ b/arch/x86/include/asm/kvm_host.h
>> @@ -594,7 +594,10 @@ struct kvm_pmu {
>> DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
>> atomic64_t __reprogram_pmi;
>> };
>> - DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
>> + union {
>> + DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
>> + u64 pmc_exists64;
>> + };
>
> Is it worth adding:
>
> BUILD_BUG_ON(sizeof(pmu->pmc_exists) != sizeof(pmu->pmc_exists64));

Good idea! I can add this:

diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 1dac6ae2cbc2..41eaefeea429 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -1051,6 +1051,8 @@ void kvm_pmu_init(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

+ BUILD_BUG_ON(sizeof(pmu->pmc_exists) != sizeof(pmu->pmc_exists64));
+
memset(pmu, 0, sizeof(*pmu));
kvm_pmu_call(init)(vcpu);
}

>
> Either way,
>
> Reviewed-by: Jim Mattson <jmattson@xxxxxxxxxx>