Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp1065546rdf; Sat, 4 Nov 2023 05:26:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH7ZGVbPRfaT7sWiWNGBR9aYp/I10QAHLQ4Ptid0r6WopWKuTnzOYA9XmYB6Jdk8l6AtO4V X-Received: by 2002:a05:6e02:2162:b0:359:53a4:b23c with SMTP id s2-20020a056e02216200b0035953a4b23cmr8211264ilv.6.1699100818719; Sat, 04 Nov 2023 05:26:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699100818; cv=none; d=google.com; s=arc-20160816; b=vgJMdlsEzSchq4vSlz6vKMThfszR91zymiqVFttkm1VwGzK2UI7eVFYKQZmUF9GQcJ wGJWjOKUMRLbHGrZ2tPG9GplE6AhFVbJtClCP0XI9SKlAxfuFCWy34YserFxHCdB2rKi 4UG8ui7CNGgPIRq5N40rcI6S7+ZhfNPTBolfpsnpiMc0WqWuPn+RWMsxWgrYlXZFkD6f Iv9sjGEMbjQZTUaCHLfwDsLi5+3Stmjg3nxe7QY7Zb7GK5X9/4cODJhqG37R0ZXkSHwv Evnoaknfmzx/eHljKGMiKH0GJe1P99UCpFx225fRMMF8U3Qs9YmwXuJfczBHpSySIKfi XFPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=l5HDM/kMANYSHmWA+0ee6/FQ7sAYRigJq4+6ccpg/KU=; fh=+P/YAw2zernQMYCM+6PO88cyfvFWVdhqutSbGYwZXbc=; b=dTwM0/hWkOHnlv4Wbxn0gco3Z+dCuzpTKApFTk0DN8XwtPGqNbR5LrAKBa4OuEpdE6 kWl7S+nVzCpPcGRsZ1DzVSqLWxcKQ0p3k2j5cvbbc4X6hFEX1pseewrp64cww5mszmjs G768P5mZySiulODSPJexp1THnyR07TKmCiPqE+0+PJJ9nPdVrE3eWc2Amdr2bs8vANf/ +vvTjmU66MYlIh3jB/5IAS3WtBQyN2iWzv35nr1Q8ZiDVjPRtBRe6ZbN5l0/9j2op5r5 4kw84UKPeiuYkmMshAvaAqGgLmAZbc3pDuQcIaa97eugknV8u147POFK2WnN2zBwyIo2 fpnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=TJM16Swe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id q7-20020a637507000000b005b11e5a69fdsi3589118pgc.508.2023.11.04.05.26.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Nov 2023 05:26:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=TJM16Swe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id B8B1B8082853; Sat, 4 Nov 2023 05:26:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229822AbjKDMZx (ORCPT + 99 others); Sat, 4 Nov 2023 08:25:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjKDMZw (ORCPT ); Sat, 4 Nov 2023 08:25:52 -0400 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73A0019D for ; Sat, 4 Nov 2023 05:25:49 -0700 (PDT) Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-507a5edc2ebso1238e87.1 for ; Sat, 04 Nov 2023 05:25:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699100748; x=1699705548; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=l5HDM/kMANYSHmWA+0ee6/FQ7sAYRigJq4+6ccpg/KU=; b=TJM16SweCACKQVC1/4lHHgO4DsP9Z5w9SqilfukLgZXHvrefOlZgrcDKssFYsx/A9T PJX0UE+FwBzgpMMT5NnasiTJFZm88gQEEiqGKW/DGPL+TAdVTtx2yhyF4p7CGlYq+moX 8bzJYZY1okldrP1WX4WcSwy5L1+hWZas8KW7wKsOCWhz0ZjWhQYd3ItnB+gh9Nzb7C// 58gN2K/Rg5stFioAdiAm9IMt5p4TIXlX05mrUro+xeUikG4Qxw1QDC9kk3jWmCRuLoKe CayCWX0VWb0Vqm2Rjghx0xdX7D7RYxoiL+jucq1eA2fU+3DtTomR/7ydAMHbzFB0LYaC +uFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699100748; x=1699705548; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=l5HDM/kMANYSHmWA+0ee6/FQ7sAYRigJq4+6ccpg/KU=; b=db3X6eQyNRj2xqIzuOD3yq75ELoFOZfJaWVGnd/KjAjj5odjNp+UVg66RGKzVqArMg IXNmdZbkwjX6E2GCSGPsSzNvQKuzZlRWChepOoqbd/bIKtN9du5buEV4ILfcndfHVBbW IJY+pq8CyF1+255oTLTjCBtKkKuOOctkX7cUZFFN7/CrmJbF1tjSYKwvtNog+K68RaEa NSIKq84huExJlxbAbm8nzCyLhLSa5wLKHSJaoLfc/0FSrQVv7gNXYpKbr3jQaoOSanbo F8KiXnJTQM87Ze3OQXY4u2ra8mMHwnzttjbmxt8blquBsxFH0bAnALJks1F53LCjDkpx Gf4A== X-Gm-Message-State: AOJu0YzRTSzS6KE1h9BeyNEmbzLulSUNYCg10tIzx/medEj8fXroZg3y COjZQFhWwlWnz0BlxifV5yQ8yFcAQRxO68pIa+/Bng== X-Received: by 2002:a05:6512:b89:b0:501:b029:1a47 with SMTP id b9-20020a0565120b8900b00501b0291a47mr41070lfv.1.1699100747484; Sat, 04 Nov 2023 05:25:47 -0700 (PDT) MIME-Version: 1.0 References: <20231104000239.367005-1-seanjc@google.com> <20231104000239.367005-3-seanjc@google.com> In-Reply-To: <20231104000239.367005-3-seanjc@google.com> From: Jim Mattson Date: Sat, 4 Nov 2023 05:25:30 -0700 Message-ID: Subject: Re: [PATCH v6 02/20] KVM: x86/pmu: Don't enumerate support for fixed counters KVM can't virtualize To: Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Dapeng Mi , Jinrong Liang , Like Xu , Aaron Lewis Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sat, 04 Nov 2023 05:26:28 -0700 (PDT) On Fri, Nov 3, 2023 at 5:02=E2=80=AFPM Sean Christopherson wrote: > > Hide fixed counters for which perf is incapable of creating the associate= d > architectural event. Except for the so called pseudo-architectural event > for counting TSC reference cycle, KVM virtualizes fixed counters by > creating a perf event for the associated general purpose architectural > event. If the associated event isn't supported in hardware, KVM can't > actually virtualize the fixed counter because perf will likely not progra= m > up the correct event. Won't it? My understanding was that perf preferred to use a fixed counter when there was a choice of fixed or general purpose counter. Unless the fixed counter is already assigned to a perf_event, KVM's request should be satisfied by assigning the fixed counter. > Note, this issue is almost certainly limited to running KVM on a funky > virtual CPU model, no known real hardware has an asymmetric PMU where a > fixed counter is supported but the associated architectural event is not. This seems like a fix looking for a problem. Has the "problem" actually been encountered? > Fixes: f5132b01386b ("KVM: Expose a version 2 architectural PMU to a gues= ts") > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/pmu.h | 4 ++++ > arch/x86/kvm/vmx/pmu_intel.c | 31 +++++++++++++++++++++++++++++++ > 2 files changed, 35 insertions(+) > > diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h > index 1d64113de488..5341e8f69a22 100644 > --- a/arch/x86/kvm/pmu.h > +++ b/arch/x86/kvm/pmu.h > @@ -19,6 +19,7 @@ > #define VMWARE_BACKDOOR_PMC_APPARENT_TIME 0x10002 > > struct kvm_pmu_ops { > + void (*init_pmu_capability)(void); > bool (*hw_event_available)(struct kvm_pmc *pmc); > struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu, int pmc_id= x); > struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu, > @@ -218,6 +219,9 @@ static inline void kvm_init_pmu_capability(const stru= ct kvm_pmu_ops *pmu_ops) > pmu_ops->MAX_NR_GP_COUNTERS); > kvm_pmu_cap.num_counters_fixed =3D min(kvm_pmu_cap.num_counters_f= ixed, > KVM_PMC_MAX_FIXED); > + > + if (pmu_ops->init_pmu_capability) > + pmu_ops->init_pmu_capability(); > } > > static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc= ) > diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c > index 1b13a472e3f2..3316fdea212a 100644 > --- a/arch/x86/kvm/vmx/pmu_intel.c > +++ b/arch/x86/kvm/vmx/pmu_intel.c > @@ -68,6 +68,36 @@ static int fixed_pmc_events[] =3D { > [2] =3D PSEUDO_ARCH_REFERENCE_CYCLES, > }; > > +static void intel_init_pmu_capability(void) > +{ > + int i; > + > + /* > + * Perf may (sadly) back a guest fixed counter with a general pur= pose > + * counter, and so KVM must hide fixed counters whose associated > + * architectural event are unsupported. On real hardware, this s= hould > + * never happen, but if KVM is running on a funky virtual CPU mod= el... > + * > + * TODO: Drop this horror if/when KVM stops using perf events for > + * guest fixed counters, or can explicitly request fixed counters= . > + */ > + for (i =3D 0; i < kvm_pmu_cap.num_counters_fixed; i++) { > + int event =3D fixed_pmc_events[i]; > + > + /* > + * Ignore pseudo-architectural events, they're a bizarre = way of > + * requesting events from perf that _can't_ be backed wit= h a > + * general purpose architectural event, i.e. they're guar= anteed > + * to be backed by the real fixed counter. > + */ > + if (event < NR_REAL_INTEL_ARCH_EVENTS && > + (kvm_pmu_cap.events_mask & BIT(event))) > + break; > + } > + > + kvm_pmu_cap.num_counters_fixed =3D i; > +} > + > static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) > { > struct kvm_pmc *pmc; > @@ -789,6 +819,7 @@ void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu= ) > } > > struct kvm_pmu_ops intel_pmu_ops __initdata =3D { > + .init_pmu_capability =3D intel_init_pmu_capability, > .hw_event_available =3D intel_hw_event_available, > .pmc_idx_to_pmc =3D intel_pmc_idx_to_pmc, > .rdpmc_ecx_to_pmc =3D intel_rdpmc_ecx_to_pmc, > -- > 2.42.0.869.gea05f2083d-goog >