From nobody Sun Sep 14 09:38:23 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 DE91EC38142 for ; Tue, 24 Jan 2023 23:49:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234341AbjAXXtT (ORCPT ); Tue, 24 Jan 2023 18:49:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234273AbjAXXtQ (ORCPT ); Tue, 24 Jan 2023 18:49:16 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C7A2D3 for ; Tue, 24 Jan 2023 15:49:15 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id a18-20020a62bd12000000b0056e7b61ec78so7396912pff.17 for ; Tue, 24 Jan 2023 15:49:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=xHI4355sZtl+nMxSrviADFAGPhVPX2cjlZsacGsw9pI=; b=RAAd1Iu4UYOh1XsF8Cfe52BTlzhxuWODb0kKCcvoM9rqv0AU2jMW7496Q3aayEBvSQ neTdVSs7NeBj8zQJoFhhPzVm4joC8mZ8YbsOFxm5lNQKU799j0EnbwviVogAmKs8Ab1c fqS18Q9i6Nn0gHXXGy/B2LkZR7WW+r80VpeFpfcDNbJXHe2wRky4HEb6hHsxiUCk0P8u h9rY0X2k6Jt+brsvgb/OoozZLc/VmyA2i597RiNFtjRGBnXywLqsLHtWIG4FkJethTsc V9zGA5ufPSq/AnK4LrkicqQCJOeYOFd7ga8eRDs+oArYI8rhs6ln+HkHH/9o+/AxO0cD hAXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=xHI4355sZtl+nMxSrviADFAGPhVPX2cjlZsacGsw9pI=; b=AJg6svjFMMOtXIiVK8Cj/VEAogytnq8jBA9zjDIPO03wlZTwdQ6+PwCtKZAZiWX8AW eTp8ILvz9xy7DoOyboSgRO1HrHXCudydzi05X061zjNz9SpGLrsfCOaiz0+qZfAUctJw q3iOVAwLwH2TpULEtgfsc0szB1acDK2KpY7orbriZV3Qqkee+fS5e/UF28FC8GqdIdfR PSIe9MqRC/8ikO9W4Vl/ju4Pwom8qWnlVcP0G1YHnZE7wjfQEQ5hQhxvUduyTdj22C/u UYKnTmhg81uf+ew9Bl4XVfuGFUYFm1ZlL1E2mutiOsp/Xj9re824MtWqadU9ohKydwG1 nKLA== X-Gm-Message-State: AFqh2kp+hhWqTi2MaB3imB2BlVyTUaY0Sg0cWD0lX0qZBTNuySEOVC0n ZhHR2nZGVDNm1dZZT8jTDTUrKVt7ozo= X-Google-Smtp-Source: AMrXdXvYyk1sWuRZ5UE/FSLyVafxn2kpbItwCV1ruZJj5qGZULz4RlmSKkf8HnPq/QxXtVj4pEO2mBr8YmE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:aa7:972c:0:b0:579:6402:5b39 with SMTP id k12-20020aa7972c000000b0057964025b39mr3200002pfg.11.1674604155069; Tue, 24 Jan 2023 15:49:15 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Jan 2023 23:49:00 +0000 In-Reply-To: <20230124234905.3774678-1-seanjc@google.com> Mime-Version: 1.0 References: <20230124234905.3774678-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.456.gfc5497dd1b-goog Message-ID: <20230124234905.3774678-2-seanjc@google.com> Subject: [PATCH 1/6] KVM: x86/pmu: Cap kvm_pmu_cap.num_counters_gp at KVM's internal max From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Aaron Lewis , Weijiang Yang , 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" Limit kvm_pmu_cap.num_counters_gp during kvm_init_pmu_capability() based on the vendor PMU capabilities so that consuming num_counters_gp naturally does the right thing. This fixes a mostly theoretical bug where KVM could over-report its PMU support in KVM_GET_SUPPORTED_CPUID for leaf 0xA, e.g. if the number of counters reported by perf is greater than KVM's hardcoded internal limit. Incorporating input from the AMD PMU also avoids over-reporting MSRs to save when running on AMD. Signed-off-by: Sean Christopherson --- arch/x86/kvm/pmu.h | 5 ++++- arch/x86/kvm/svm/pmu.c | 1 + arch/x86/kvm/vmx/pmu_intel.c | 1 + arch/x86/kvm/x86.c | 6 +++--- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 30bfccc6df60..8c04e9109db0 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -42,6 +42,7 @@ struct kvm_pmu_ops { void (*cleanup)(struct kvm_vcpu *vcpu); =20 const u64 EVENTSEL_EVENT; + const int MAX_NR_GP_COUNTERS; }; =20 void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops); @@ -163,7 +164,7 @@ static inline bool pmc_speculative_in_use(struct kvm_pm= c *pmc) =20 extern struct x86_pmu_capability kvm_pmu_cap; =20 -static inline void kvm_init_pmu_capability(void) +static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_o= ps) { bool is_intel =3D boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_INTEL; =20 @@ -182,6 +183,8 @@ static inline void kvm_init_pmu_capability(void) } =20 kvm_pmu_cap.version =3D min(kvm_pmu_cap.version, 2); + kvm_pmu_cap.num_counters_gp =3D min(kvm_pmu_cap.num_counters_gp, + pmu_ops->MAX_NR_GP_COUNTERS); kvm_pmu_cap.num_counters_fixed =3D min(kvm_pmu_cap.num_counters_fixed, KVM_PMC_MAX_FIXED); } diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c index 5da8c292e3e3..cc77a0681800 100644 --- a/arch/x86/kvm/svm/pmu.c +++ b/arch/x86/kvm/svm/pmu.c @@ -232,4 +232,5 @@ struct kvm_pmu_ops amd_pmu_ops __initdata =3D { .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, }; diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 7980fda3978d..5d525d677967 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -812,4 +812,5 @@ struct kvm_pmu_ops intel_pmu_ops __initdata =3D { .deliver_pmi =3D intel_pmu_deliver_pmi, .cleanup =3D intel_pmu_cleanup, .EVENTSEL_EVENT =3D ARCH_PERFMON_EVENTSEL_EVENT, + .MAX_NR_GP_COUNTERS =3D KVM_INTEL_PMC_MAX_GENERIC, }; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index da02a08e21b5..ad95ce92a154 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7061,12 +7061,12 @@ static void kvm_init_msr_list(void) break; case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR_MAX: if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=3D - min(KVM_INTEL_PMC_MAX_GENERIC, kvm_pmu_cap.num_counters_gp)) + kvm_pmu_cap.num_counters_gp) continue; break; case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL_MAX: if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=3D - min(KVM_INTEL_PMC_MAX_GENERIC, kvm_pmu_cap.num_counters_gp)) + kvm_pmu_cap.num_counters_gp) continue; break; case MSR_IA32_XFD: @@ -9386,7 +9386,7 @@ static int __kvm_x86_vendor_init(struct kvm_x86_init_= ops *ops) if (boot_cpu_has(X86_FEATURE_XSAVES)) rdmsrl(MSR_IA32_XSS, host_xss); =20 - kvm_init_pmu_capability(); + kvm_init_pmu_capability(ops->pmu_ops); =20 r =3D ops->hardware_setup(); if (r !=3D 0) --=20 2.39.1.456.gfc5497dd1b-goog