From nobody Fri Dec 19 16:03:25 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC502C00A8F for ; Mon, 23 Oct 2023 23:40:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231562AbjJWXkM (ORCPT ); Mon, 23 Oct 2023 19:40:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231433AbjJWXkI (ORCPT ); Mon, 23 Oct 2023 19:40:08 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7337F9 for ; Mon, 23 Oct 2023 16:40:05 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1c9c6f9e4d2so32594385ad.2 for ; Mon, 23 Oct 2023 16:40:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698104405; x=1698709205; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=7hs5LXK64QuGvnjb5nv5KwW+waLw4KwuiCSeTdPJWrI=; b=iZwEAusYbE0n+500Y1eVOxVEOwIgNksAd3nKDsonxcMJ7037Kbl5+PxeU1R7znOzFo aR2n1VfB0zLWtXA5G38vjt2uodfT/8q2jocN9JK5d2hxy9O53iQC679sINETkKIpVhru dV1sZwF+vKMyXyAboRl1RngCYvOBEmoq2cqg5yOIX2XzFv5LSEyPn7+affm1+FXwpmNd 50QHaGI5zmBBpSgVgAJ+LP3bJjugNBtS3gHNAI98NsPEJuQ2witBcbDDPGJVilAt76R8 1KV1lGUe/4Nn3CSLjJVmlmVVbfRWkG4kZyDo3v6kasVpN+Wr11ks2LVBpu/ZDTHpE+5H eUbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698104405; x=1698709205; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7hs5LXK64QuGvnjb5nv5KwW+waLw4KwuiCSeTdPJWrI=; b=rbRwfrbS+J9zl1AINXnrdFWNfPaW8ahbfItLiiRGCvTnSgql36HY5j/Vh5FIDJWMEF Tlw6V3sqZm/VK9EkxHTVSs6m6oyzWiDHERnft/1OTlqHezxJn7lxOlzKCL98oc/qHSqL hW1zZweQGq8XersPVVEL+GQptH4GnVCJMVkTAZAZyjlXXGxlqRihk9yCuR59cHlYuIP1 jWiLlHKQZRQsssDPYilTUf8nwJm2TjoSsrAS2xFzIx1ahW3elCzCwfzgpUT9Ng3Ko1N3 l+HeJfzwRpyyuVE7D1nluMQ3s/Y7x3+3K8fL0Zvo692Ej5nJmZoZmisVLCyax4icCd38 u81g== X-Gm-Message-State: AOJu0YyWBCpeUwbmYT+H0Air6IjUZKz2CnjKyA5zgw5A7BUGrVn0NI9v SqMrIeU4z4NE348h/jbIPZazzqstQaw= X-Google-Smtp-Source: AGHT+IFtgExxSownjg4pSQppeXSJemiFNe9s7PaYVEIMlhr6QHeOjldSLqohfie35x54YlyZDzLKQOaGsEc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:6502:b0:1c9:b045:5a8b with SMTP id b2-20020a170902650200b001c9b0455a8bmr181162plk.6.1698104405345; Mon, 23 Oct 2023 16:40:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 23 Oct 2023 16:39:55 -0700 In-Reply-To: <20231023234000.2499267-1-seanjc@google.com> Mime-Version: 1.0 References: <20231023234000.2499267-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Message-ID: <20231023234000.2499267-2-seanjc@google.com> Subject: [PATCH 1/6] KVM: x86/pmu: Move PMU reset logic to common x86 code From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Mingwei Zhang , Roman Kagan , Jim Mattson , Dapeng Mi , Like Xu Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the common (or at least "ignored") aspects of resetting the vPMU to common x86 code, along with the stop/release helpers that are no used only by the common pmu.c. There is no need to manually handle fixed counters as all_valid_pmc_idx tracks both fixed and general purpose counters, and resetting the vPMU is far from a hot path, i.e. the extra bit of overhead to the PMC from the index is a non-issue. Zero fixed_ctr_ctrl in common code even though it's Intel specific. Ensuring it's zero doesn't harm AMD/SVM in any way, and stopping the fixed counters via all_valid_pmc_idx, but not clearing the associated control bits, would be odd/confusing. Make the .reset() hook optional as SVM no longer needs vendor specific handling. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm-x86-pmu-ops.h | 2 +- arch/x86/kvm/pmu.c | 40 +++++++++++++++++++++++++- arch/x86/kvm/pmu.h | 18 ------------ arch/x86/kvm/svm/pmu.c | 16 ----------- arch/x86/kvm/vmx/pmu_intel.c | 20 ------------- 5 files changed, 40 insertions(+), 56 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-pmu-ops.h b/arch/x86/include/asm/= kvm-x86-pmu-ops.h index 6c98f4bb4228..058bc636356a 100644 --- a/arch/x86/include/asm/kvm-x86-pmu-ops.h +++ b/arch/x86/include/asm/kvm-x86-pmu-ops.h @@ -22,7 +22,7 @@ KVM_X86_PMU_OP(get_msr) KVM_X86_PMU_OP(set_msr) KVM_X86_PMU_OP(refresh) KVM_X86_PMU_OP(init) -KVM_X86_PMU_OP(reset) +KVM_X86_PMU_OP_OPTIONAL(reset) KVM_X86_PMU_OP_OPTIONAL(deliver_pmi) KVM_X86_PMU_OP_OPTIONAL(cleanup) =20 diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 9ae07db6f0f6..027e9c3c2b93 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -250,6 +250,24 @@ static bool pmc_resume_counter(struct kvm_pmc *pmc) return true; } =20 +static void pmc_release_perf_event(struct kvm_pmc *pmc) +{ + if (pmc->perf_event) { + perf_event_release_kernel(pmc->perf_event); + pmc->perf_event =3D NULL; + pmc->current_config =3D 0; + pmc_to_pmu(pmc)->event_count--; + } +} + +static void pmc_stop_counter(struct kvm_pmc *pmc) +{ + if (pmc->perf_event) { + pmc->counter =3D pmc_read_counter(pmc); + pmc_release_perf_event(pmc); + } +} + static int filter_cmp(const void *pa, const void *pb, u64 mask) { u64 a =3D *(u64 *)pa & mask; @@ -654,7 +672,27 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu) =20 void kvm_pmu_reset(struct kvm_vcpu *vcpu) { - static_call(kvm_x86_pmu_reset)(vcpu); + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + struct kvm_pmc *pmc; + int i; + + bitmap_zero(pmu->reprogram_pmi, X86_PMC_IDX_MAX); + + for_each_set_bit(i, pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX) { + pmc =3D static_call(kvm_x86_pmu_pmc_idx_to_pmc)(pmu, i); + if (!pmc) + continue; + + pmc_stop_counter(pmc); + pmc->counter =3D 0; + + if (pmc_is_gp(pmc)) + pmc->eventsel =3D 0; + } + + pmu->fixed_ctr_ctrl =3D pmu->global_ctrl =3D pmu->global_status =3D 0; + + static_call_cond(kvm_x86_pmu_reset)(vcpu); } =20 void kvm_pmu_init(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 1d64113de488..a46aa9b25150 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -80,24 +80,6 @@ static inline void pmc_write_counter(struct kvm_pmc *pmc= , u64 val) pmc->counter &=3D pmc_bitmask(pmc); } =20 -static inline void pmc_release_perf_event(struct kvm_pmc *pmc) -{ - if (pmc->perf_event) { - perf_event_release_kernel(pmc->perf_event); - pmc->perf_event =3D NULL; - pmc->current_config =3D 0; - pmc_to_pmu(pmc)->event_count--; - } -} - -static inline void pmc_stop_counter(struct kvm_pmc *pmc) -{ - if (pmc->perf_event) { - pmc->counter =3D pmc_read_counter(pmc); - pmc_release_perf_event(pmc); - } -} - static inline bool pmc_is_gp(struct kvm_pmc *pmc) { return pmc->type =3D=3D KVM_PMC_GP; diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c index 373ff6a6687b..3fd47de14b38 100644 --- a/arch/x86/kvm/svm/pmu.c +++ b/arch/x86/kvm/svm/pmu.c @@ -233,21 +233,6 @@ static void amd_pmu_init(struct kvm_vcpu *vcpu) } } =20 -static void amd_pmu_reset(struct kvm_vcpu *vcpu) -{ - struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); - int i; - - for (i =3D 0; i < KVM_AMD_PMC_MAX_GENERIC; i++) { - struct kvm_pmc *pmc =3D &pmu->gp_counters[i]; - - pmc_stop_counter(pmc); - pmc->counter =3D pmc->prev_counter =3D pmc->eventsel =3D 0; - } - - pmu->global_ctrl =3D pmu->global_status =3D 0; -} - struct kvm_pmu_ops amd_pmu_ops __initdata =3D { .hw_event_available =3D amd_hw_event_available, .pmc_idx_to_pmc =3D amd_pmc_idx_to_pmc, @@ -259,7 +244,6 @@ struct kvm_pmu_ops amd_pmu_ops __initdata =3D { .set_msr =3D amd_pmu_set_msr, .refresh =3D amd_pmu_refresh, .init =3D amd_pmu_init, - .reset =3D amd_pmu_reset, .EVENTSEL_EVENT =3D AMD64_EVENTSEL_EVENT, .MAX_NR_GP_COUNTERS =3D KVM_AMD_PMC_MAX_GENERIC, .MIN_NR_GP_COUNTERS =3D AMD64_NUM_COUNTERS, diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 820d3e1f6b4f..90c1f7f07e53 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -632,26 +632,6 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu) =20 static void intel_pmu_reset(struct kvm_vcpu *vcpu) { - struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); - struct kvm_pmc *pmc =3D NULL; - int i; - - for (i =3D 0; i < KVM_INTEL_PMC_MAX_GENERIC; i++) { - pmc =3D &pmu->gp_counters[i]; - - pmc_stop_counter(pmc); - pmc->counter =3D pmc->prev_counter =3D pmc->eventsel =3D 0; - } - - for (i =3D 0; i < KVM_PMC_MAX_FIXED; i++) { - pmc =3D &pmu->fixed_counters[i]; - - pmc_stop_counter(pmc); - pmc->counter =3D pmc->prev_counter =3D 0; - } - - pmu->fixed_ctr_ctrl =3D pmu->global_ctrl =3D pmu->global_status =3D 0; - intel_pmu_release_guest_lbr_event(vcpu); } =20 --=20 2.42.0.758.gaed0368e0e-goog