Re: [PATCH v3 04/18] KVM: x86/pmu: Add PMC bitmap accessor helpers

From: Chen, Zide

Date: Mon Nov 09 2026 - 17:30:18 EST




On 11/9/2026 4:07 PM, Yosry Ahmed wrote:
>>> diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
>>> index 98e6f2443b82e..37a6664fdb9c9 100644
>>> --- a/arch/x86/kvm/pmu.h
>>> +++ b/arch/x86/kvm/pmu.h
>>> @@ -149,11 +149,11 @@ static inline struct kvm_pmc *kvm_pmc_idx_to_pmc(struct kvm_pmu *pmu, int idx)
>>> * which is already constrained by KVM_MAX_NR_{AMD,INTEL}_{GP,FIXED}_COUNTERS,
>>> * so iteration up to KVM's maximum counter count is safe.
>>> */
>>> -#define kvm_for_each_gp_counter(i, mask) \
>>> - for_each_set_bit((i), &(mask), KVM_MAX_NR_GP_COUNTERS)
>>> +#define kvm_for_each_gp_counter(i, pmu) \
>>> + for_each_set_bit((i), &(unsigned long){ kvm_gp_pmc_mask(pmu) }, KVM_MAX_NR_GP_COUNTERS)
>>>
>>> -#define kvm_for_each_fixed_counter(i, mask) \
>>> - for_each_set_bit((i), &(mask), KVM_MAX_NR_FIXED_COUNTERS)
>>> +#define kvm_for_each_fixed_counter(i, pmu) \
>>> + for_each_set_bit((i), &(unsigned long){ kvm_fixed_pmc_mask(pmu) }, KVM_MAX_NR_FIXED_COUNTERS)
>>
>> Right, I didn't realize C99 compound literals could be used here.
>>
>> However, with this suggested code, kvm_gp_pmc_mask() would be
>> re-evaluated on every iteration. It seems this “expression with side
>> effects evaluated more than once” is not encouraged by the coding style,
>> is it?
>
> kvm_gp_pmc_mask() and kvm_fixed_pmc_mask() have no side effects, right?

Right, you are completely right—they are pure functions with no side
effects at all.

I was just curious whether re-evaluating the compound literal on every
iteration might introduce any subtle overhead, given they could be
called in the pmu context switch. I'm very happy to go with
your approach!

Thanks again!