Re: [PATCH v3 3/8] KVM: x86/pmu: Rewrite reprogram_counters() to improve performance

From: Sean Christopherson
Date: Thu Jan 19 2023 - 20:10:09 EST


On Fri, Nov 11, 2022, Like Xu wrote:
> From: Like Xu <likexu@xxxxxxxxxxx>
>
> A valid pmc is always tested before using pmu->reprogram_pmi. Eliminate
> this part of the redundancy by setting the counter's bitmask directly,
> and in addition, trigger KVM_REQ_PMU only once to save more cpu cycles.
>
> Signed-off-by: Like Xu <likexu@xxxxxxxxxxx>
> ---
> arch/x86/kvm/pmu.h | 11 +++++++++++
> arch/x86/kvm/vmx/pmu_intel.c | 12 ------------
> 2 files changed, 11 insertions(+), 12 deletions(-)
>
> diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
> index 2b5376ba66ea..be552c8217a0 100644
> --- a/arch/x86/kvm/pmu.h
> +++ b/arch/x86/kvm/pmu.h
> @@ -189,6 +189,17 @@ static inline void kvm_pmu_request_counter_reprogam(struct kvm_pmc *pmc)
> kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
> }
>
> +static inline void reprogram_counters(struct kvm_pmu *pmu, u64 diff)
> +{
> + int bit;
> +
> + if (diff) {

Prefer this, it's slightly cleaner:

if (!diff)
return;

> + for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
> + __set_bit(bit, pmu->reprogram_pmi);

Needs to use the atomic set_bit().