Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp4885182rwb; Tue, 6 Sep 2022 14:31:34 -0700 (PDT) X-Google-Smtp-Source: AA6agR5Enz0I+E1SvclcMEfY719vPwZiB99dCZhZzJ8C2sODHvDIxDWTgkUqDroz2pPT43Drtap/ X-Received: by 2002:a05:6402:2989:b0:44e:90d0:b9ff with SMTP id eq9-20020a056402298900b0044e90d0b9ffmr489671edb.110.1662499894448; Tue, 06 Sep 2022 14:31:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662499894; cv=none; d=google.com; s=arc-20160816; b=0P5YthEfbUW9R3aXyog0UubfiXCwqwTS0P1lwUtYIm9fFRk2Y8efxPCVY2QQB+i0jL v2t7s31Mk+SRqYQMHzvNv23WkE4wSDaxQlkbVQ55v4dnwpsJ76H0INODCWWjT1wACLnC TKks7vjHELnFGCoQ6mlPG01Iu/OQ48HTbo9aK7vH+FRvspxeZ+xKzpRZXpx96bTRehAr 1yjesWqZqeSGEWK6nHsbO3F+4R6RkYfKdmlTjVfrO18mbN78Ld2IzwU9XdflxSP7il/J CSGk58gvTQ+QAhvUKg0Vr/YEkhoxPeeq+bMnE5A0I+6bSAgO5R/ESeRtiEQGamUzOwa8 YNjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=gbBq9oSsimXhwPcj/18IdLMsGZl+C+oGEqB+OkpNjck=; b=SMuTZAinx1DsjIFbEbC/S9lu76GFmZc1BT3OZeRECZghN/1yxvW/C6CPzk4tQbvjGX E1wBIri7D3SoMLQEhXg+CezFVR92tEQL2R1cpAodNJKTDgyvG9wzflmMskvM8v3Ctyz0 3R752xzRjw2EvF6NznNPtVpDrf5zbj6utmppUpwO5aGpqRHfONo8dJgkdQyAWbN9iLjG rK+ndIezxdc4qP6O+sDUC6wKo4/2MHOCkIQ+Kfg1nXwWmMXEK6QTcLzg1v9yWaOuZLrs 4MelUbuudz9m+rMndueqbYFlSPMepwHgFwAqjpuQRRo54cFVsK90iEhC8kX9PRBaz39k DALQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oK3e02N3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id eb13-20020a0564020d0d00b004480d6fefe4si1951924edb.239.2022.09.06.14.31.08; Tue, 06 Sep 2022 14:31:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=oK3e02N3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231410AbiIFUVI (ORCPT + 99 others); Tue, 6 Sep 2022 16:21:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230262AbiIFUUj (ORCPT ); Tue, 6 Sep 2022 16:20:39 -0400 Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 493A976447 for ; Tue, 6 Sep 2022 13:19:57 -0700 (PDT) Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-127a3a39131so10209356fac.13 for ; Tue, 06 Sep 2022 13:19:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=gbBq9oSsimXhwPcj/18IdLMsGZl+C+oGEqB+OkpNjck=; b=oK3e02N3juvYgzLxWq4Q6AHFAqYWQ11CFPvfKK0z1jomy+ZlHIJUgUwH8XXWCgmPw3 BS4c/ZNzuJcrZU2oALV/MZ1fmCGzww9/IDT3CGrxKZoytQwOl0Yb8HS0CMB889pKDu4r aztCp5vEItHWSjiYXBnjOlbvPKCwhhkhhRiIN9yWNWClEMZfsO1WLNI6U8woEE45SX4o 6JvnpCyl9bT+nkjwd74yYclBw2j1TzUnbJRxuRv5Eh2cQVQJIYLf3akT2ndm2tRLgs6g nrJ5kpIcIkFHR6gafvMcTVLvW6OsSKJRwFN7rsK65IxUM557keRXNXpzyX6sdDS7xS3P pYaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=gbBq9oSsimXhwPcj/18IdLMsGZl+C+oGEqB+OkpNjck=; b=bdBoxjv4GxUUR02InWmZmhRs4Pqa22Xruu40/SO2yUgrJ4AGS3HxXH06pP1l+KfUGy 2MZoEJUSWu5on/PGZJGi2jXeATrzSEuDo5MfAxcZ7SiB1ZTuXw/avbay3qgS37GE8x9w yUQ0HGsxaj4tJpcRgE1jVlvY2DJ9epjCcDKAbLChOXcBROpFcMQQFEho+TFkwOP4E1Us VLR6ggW7zzqikkzGk08GSJ0QE5jcSsIqeUM7Upnhp8c6v5lHdjfFMnwbqsvo5SFooPSt wkUExQlhMc4v2UVjAqDBjSbcYQ8shPSItU/W5HrAqqvZ5IFpNAdEaHfZF3O2qsp+eDWT wg0g== X-Gm-Message-State: ACgBeo3bwEyJ0pvlVGyrkeBOdxBGc2Ca7tsV4qrFvo71fI3hVNfksPkc 7NMcLvV3A5CD3+kILj9RkYnMvgiSzQnoWsdMwFBL9w== X-Received: by 2002:a05:6870:c596:b0:101:6409:ae62 with SMTP id ba22-20020a056870c59600b001016409ae62mr12440717oab.112.1662495596120; Tue, 06 Sep 2022 13:19:56 -0700 (PDT) MIME-Version: 1.0 References: <20220905123946.95223-1-likexu@tencent.com> <20220905123946.95223-4-likexu@tencent.com> In-Reply-To: From: Jim Mattson Date: Tue, 6 Sep 2022 13:19:45 -0700 Message-ID: Subject: Re: [PATCH 3/4] KVM: x86/svm/pmu: Add AMD PerfMonV2 support To: Like Xu Cc: Sean Christopherson , Paolo Bonzini , Sandipan Das , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Sep 6, 2022 at 5:45 AM Like Xu wrote: > > On 6/9/2022 2:00 am, Jim Mattson wrote: > > On Mon, Sep 5, 2022 at 5:44 AM Like Xu wrote: > >> > >> From: Like Xu > >> > >> If AMD Performance Monitoring Version 2 (PerfMonV2) is detected > >> by the guest, it can use a new scheme to manage the Core PMCs using > >> the new global control and status registers. > >> > >> In addition to benefiting from the PerfMonV2 functionality in the same > >> way as the host (higher precision), the guest also can reduce the number > >> of vm-exits by lowering the total number of MSRs accesses. > >> > >> In terms of implementation details, amd_is_valid_msr() is resurrected > >> since three newly added MSRs could not be mapped to one vPMC. > >> The possibility of emulating PerfMonV2 on the mainframe has also > >> been eliminated for reasons of precision. > >> > >> Co-developed-by: Sandipan Das > >> Signed-off-by: Sandipan Das > >> Signed-off-by: Like Xu > >> --- > >> arch/x86/kvm/pmu.c | 6 +++++ > >> arch/x86/kvm/svm/pmu.c | 50 +++++++++++++++++++++++++++++++++--------- > >> arch/x86/kvm/x86.c | 11 ++++++++++ > >> 3 files changed, 57 insertions(+), 10 deletions(-) > >> > >> diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c > >> index 7002e1b74108..56b4f898a246 100644 > >> --- a/arch/x86/kvm/pmu.c > >> +++ b/arch/x86/kvm/pmu.c > >> @@ -455,12 +455,15 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > >> > >> switch (msr) { > >> case MSR_CORE_PERF_GLOBAL_STATUS: > >> + case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS: > >> msr_info->data = pmu->global_status; > >> return 0; > >> case MSR_CORE_PERF_GLOBAL_CTRL: > >> + case MSR_AMD64_PERF_CNTR_GLOBAL_CTL: > >> msr_info->data = pmu->global_ctrl; > >> return 0; > >> case MSR_CORE_PERF_GLOBAL_OVF_CTRL: > >> + case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR: > >> msr_info->data = 0; > >> return 0; > >> default: > >> @@ -479,12 +482,14 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > >> > >> switch (msr) { > >> case MSR_CORE_PERF_GLOBAL_STATUS: > >> + case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS: > >> if (msr_info->host_initiated) { > >> pmu->global_status = data; > >> return 0; > >> } > >> break; /* RO MSR */ > >> case MSR_CORE_PERF_GLOBAL_CTRL: > >> + case MSR_AMD64_PERF_CNTR_GLOBAL_CTL: > >> if (pmu->global_ctrl == data) > >> return 0; > >> if (kvm_valid_perf_global_ctrl(pmu, data)) { > >> @@ -495,6 +500,7 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > >> } > >> break; > >> case MSR_CORE_PERF_GLOBAL_OVF_CTRL: > >> + case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR: > >> if (!(data & pmu->global_ovf_ctrl_mask)) { > >> if (!msr_info->host_initiated) > >> pmu->global_status &= ~data; > >> diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c > >> index 3a20972e9f1a..4c7d408e3caa 100644 > >> --- a/arch/x86/kvm/svm/pmu.c > >> +++ b/arch/x86/kvm/svm/pmu.c > >> @@ -92,12 +92,6 @@ static struct kvm_pmc *amd_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, > >> return amd_pmc_idx_to_pmc(vcpu_to_pmu(vcpu), idx & ~(3u << 30)); > >> } > >> > >> -static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) > >> -{ > >> - /* All MSRs refer to exactly one PMC, so msr_idx_to_pmc is enough. */ > >> - return false; > >> -} > >> - > >> static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, u32 msr) > >> { > >> struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); > >> @@ -109,6 +103,29 @@ static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, u32 msr) > >> return pmc; > >> } > >> > >> +static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) > >> +{ > >> + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); > >> + > >> + switch (msr) { > >> + case MSR_K7_EVNTSEL0 ... MSR_K7_PERFCTR3: > >> + return pmu->version > 0; > >> + case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: > >> + return guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE); > >> + case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS: > >> + case MSR_AMD64_PERF_CNTR_GLOBAL_CTL: > >> + case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR: > >> + return pmu->version > 1; > >> + default: > >> + if (msr > MSR_F15H_PERF_CTR5 && > >> + msr < MSR_F15H_PERF_CTL0 + 2 * KVM_AMD_PMC_MAX_GENERIC) > >> + return pmu->version > 1; > > > > Should this be bounded by guest CPUID.80000022H:EBX[NumCorePmc] > > (unless host-initiated)? > > Indeed, how about: > > default: > if (msr > MSR_F15H_PERF_CTR5 && > msr < MSR_F15H_PERF_CTL0 + 2 * pmu->nr_arch_gp_counters) > return pmu->version > 1; > > and for host-initiated: > > #define MSR_F15H_PERF_MSR_MAX \ > (MSR_F15H_PERF_CTR0 + 2 * (KVM_AMD_PMC_MAX_GENERIC - 1)) I think there may be an off-by-one error here. > > kvm_{set|get}_msr_common() > case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_MSR_MAX: > if (kvm_pmu_is_valid_msr(vcpu, msr)) > return kvm_pmu_set_msr(vcpu, msr_info); > ? > > > > >> + break; > >> + } > >> + > >> + return amd_msr_idx_to_pmc(vcpu, msr); > >> +} > >> + > >> static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > >> { > >> struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); > >> @@ -162,20 +179,31 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > >> static void amd_pmu_refresh(struct kvm_vcpu *vcpu) > >> { > >> struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); > >> + struct kvm_cpuid_entry2 *entry; > >> + union cpuid_0x80000022_ebx ebx; > >> > >> - if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) > >> + pmu->version = 1; > >> + entry = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0); > >> + if (kvm_pmu_cap.version > 1 && entry && (entry->eax & BIT(0))) { > >> + pmu->version = 2; > >> + ebx.full = entry->ebx; > >> + pmu->nr_arch_gp_counters = min3((unsigned int)ebx.split.num_core_pmc, > >> + (unsigned int)kvm_pmu_cap.num_counters_gp, > >> + (unsigned int)KVM_AMD_PMC_MAX_GENERIC); > >> + pmu->global_ctrl_mask = ~((1ull << pmu->nr_arch_gp_counters) - 1); > >> + pmu->global_ovf_ctrl_mask = pmu->global_ctrl_mask; > >> + } else if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) { > >> pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE; > > > > The logic above doesn't seem quite right, since guest_cpuid_has(vcpu, > > X86_FEATURE_PERFCTR_CORE) promises 6 PMCs, regardless of what > > CPUID.80000022 says. > > I would have expected the appearance of CPUID.80000022 to override PERFCTR_CORE, > now I don't think it's a good idea as you do, so how about: > > amd_pmu_refresh(): > > bool perfctr_core = guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE); > > pmu->version = 1; > if (kvm_pmu_cap.version > 1) > entry = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0); > > if (!perfctr_core) > pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS; > if (entry && (entry->eax & BIT(0))) { > pmu->version = 2; > ebx.full = entry->ebx; > pmu->nr_arch_gp_counters = min3((unsigned int)ebx.split.num_core_pmc, > (unsigned int)kvm_pmu_cap.num_counters_gp, > (unsigned int)KVM_AMD_PMC_MAX_GENERIC); > } > /* PERFCTR_CORE promises 6 PMCs, regardless of CPUID.80000022 */ > if (perfctr_core) { > pmu->nr_arch_gp_counters = max(pmu->nr_arch_gp_counters, > AMD64_NUM_COUNTERS_CORE); > } Even if X86_FEATURE_PERFCTR_CORE is clear, all AMD CPUs promise 4 PMCs. > > if (pmu->version > 1) { > pmu->global_ctrl_mask = ~((1ull << pmu->nr_arch_gp_counters) - 1); > pmu->global_ovf_ctrl_mask = pmu->global_ctrl_mask; > } > > ? > >