Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4255410rdb; Mon, 11 Dec 2023 13:34:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwkisj3kYiZ8GTn92KvXIEcmg3vIcT9kBLZGhkYUTi05owwbFWM46CVZ/axquQh3IROZXT X-Received: by 2002:a17:90b:1886:b0:286:7ec2:e503 with SMTP id mn6-20020a17090b188600b002867ec2e503mr3965683pjb.17.1702330442364; Mon, 11 Dec 2023 13:34:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702330442; cv=none; d=google.com; s=arc-20160816; b=067CW5QpkFFzB2W98vxgWZiq8PcRzV/ILn/v5sVC6qgB51Jpy9G3tU5gjBZFNC1dMs i9qtQRrQrh/xu3IdyB64vXQXRaRC7Ncv0oxYNSZHXWZk7FEkNXTN85ActHwLegE1KWph luhhAPmGoDINsl0E9AT6z3NbKaHtZBAMtHgP8C0jnZV0u4JI6JUqKUilAsc17gbFlwXh 1DBe1sGmiZJ7ebexh8IcdLjuD57qUC6W2ITIj15/B9mWKgxOVlK8eWdG7SRD388X1D6Q OKOHmMFEBq2gGNZ7vKPVuFtY4OU9kuyyHlxnmYloW+nVN/EAIVhGDZY28ycsXGCoFux4 bYkw== 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=wX1Wg/mW222hGnVKsBmQT/O39fFEc/bGrsBIuqYGAfk=; fh=yfOPHZ3sJKLh/7lGMoXs88fE0YiBCOg7kHy7X60hTj0=; b=ZqnXrAGiadjwZ/KldvpqoaeikxtPn1pj4SuvyLnTCuc9jWAOY4qKXTDIjKXTZ5/Mtd fpgdUJ5wx4QJfHrw3zClQNL2IUQ31HoOp3QhRSbWJ34WuycJiRwXUlRTyq+P2DJtDUF1 WUnixcY+d5VQG4f/ZHfC2yj7I1j069KccBltJW4n/iFEdRoSykX5it4jLipa2PGH5KyE 0/HI5fq1yalbV1lV5Wesciacfwmo9eJZe3xpbNw995xlXLUmHVWJWPFxHjGlWSm4HsrD zUuGiXv3/gI2qwzxfNlxbXwG5+LytYqRtjSBne+Bhit8QT63OR8IH3CEdocg/2aPCNHD cbxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vOYCnctu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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. [23.128.96.32]) by mx.google.com with ESMTPS id c6-20020a17090a1d0600b0028654d511a4si8018219pjd.178.2023.12.11.13.34.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 13:34:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vOYCnctu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 C0A998058B65; Mon, 11 Dec 2023 13:33:59 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345434AbjLKVdq (ORCPT + 99 others); Mon, 11 Dec 2023 16:33:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345467AbjLKVdk (ORCPT ); Mon, 11 Dec 2023 16:33:40 -0500 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 778D710E for ; Mon, 11 Dec 2023 13:33:46 -0800 (PST) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-54c77d011acso959a12.1 for ; Mon, 11 Dec 2023 13:33:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702330425; x=1702935225; 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=wX1Wg/mW222hGnVKsBmQT/O39fFEc/bGrsBIuqYGAfk=; b=vOYCnctuQc9LLVQrGdohewlBDupxI+Bq/so9cC0p/M3+ompdeyTicvMkwhg3tC8mDK KiucCEWOKzslulBnPb73EMu30aIE2HfeR5OeGmEKE3w+TRTWIXPNhjA2sDyN3XpIsPfn M3NR3pUhxsM6+VdrYEGwOpAsAVbZMSAxlMKMT8a57MmgzOXgB40wzU0Eu+Du+qsdUGJV Av/JwwpNCfBRZQi8dVigjARYAgeWyKbSiIqZY4NIuMnaxrEFQ64oEsm86Io5avsdn3qZ 1riG43e5QjHCvS668RC7dvjfS6/W/a/QPYqI/l6q5oRBSqE8uuzEZFRzcrPnbUTu9yXO TUZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702330425; x=1702935225; 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=wX1Wg/mW222hGnVKsBmQT/O39fFEc/bGrsBIuqYGAfk=; b=unLZPq/Mx/BtTbNgV3bgz3DEOoV4IKJ5PyUs+1uu0we47REWPNMVOwlqCkb2U/Q5Pg 2YXTlT1EzlDeuiSOz30aflBzqZs7Zva5Hv4rnnvIL4Z/WmAjWPrCMn4D4lBb+DbkxWyY UV4VzaJ0fHir+6pKxDxYMbfU6DTStEd+rqneiqiguzY6iSIRXuWFvMvObCrBOq5opXnv QfejuBniFVyu9PVqCtpQXufX3W4y9ku9PPG0m3gS0FwfyR4EJUdR3vW9GfUPkF3Pgx/B 2SO2HWImpKB8+RoDrsPAn6+wI35JEqiRNCWCsYWvLJmsJ+8AG9eCQ4Abye4GaOAzziA2 maTg== X-Gm-Message-State: AOJu0Yy46uP5Apo3BLgROadrAYO1U/vzo+cuYdVpB/cpD6lTvPM/AfNY MIa8+K78tUPh37h65jbS0kz0q5UR0aBKADFhpyaJlA== X-Received: by 2002:a50:c35d:0:b0:544:466b:3b20 with SMTP id q29-20020a50c35d000000b00544466b3b20mr308625edb.5.1702330424693; Mon, 11 Dec 2023 13:33:44 -0800 (PST) MIME-Version: 1.0 References: <20231202000417.922113-1-seanjc@google.com> <20231202000417.922113-11-seanjc@google.com> In-Reply-To: From: Jim Mattson Date: Mon, 11 Dec 2023 13:33:29 -0800 Message-ID: Subject: Re: [PATCH v9 10/28] KVM: x86/pmu: Explicitly check for RDPMC of unsupported Intel PMC types To: "Mi, Dapeng" Cc: Sean Christopherson , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Jinrong Liang , Aaron Lewis , Like Xu 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]); Mon, 11 Dec 2023 13:33:59 -0800 (PST) On Sun, Dec 10, 2023 at 10:26=E2=80=AFPM Mi, Dapeng wrote: > > > On 12/2/2023 8:03 AM, Sean Christopherson wrote: > > Explicitly check for attempts to read unsupported PMC types instead of > > letting the bounds check fail. Functionally, letting the check fail is > > ok, but it's unnecessarily subtle and does a poor job of documenting th= e > > architectural behavior that KVM is emulating. > > > > Opportunistically add macros for the type vs. index to further document > > what is going on. > > > > Signed-off-by: Sean Christopherson > > --- > > arch/x86/kvm/vmx/pmu_intel.c | 11 +++++++++-- > > 1 file changed, 9 insertions(+), 2 deletions(-) > > > > diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.= c > > index 644de27bd48a..bd4f4bdf5419 100644 > > --- a/arch/x86/kvm/vmx/pmu_intel.c > > +++ b/arch/x86/kvm/vmx/pmu_intel.c > > @@ -23,6 +23,9 @@ > > /* Perf's "BASE" is wildly misleading, this is a single-bit flag, not= a base. */ > > #define INTEL_RDPMC_FIXED INTEL_PMC_FIXED_RDPMC_BASE > > > > +#define INTEL_RDPMC_TYPE_MASK GENMASK(31, 16) > > +#define INTEL_RDPMC_INDEX_MASK GENMASK(15, 0) > > + > > #define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR= 0) > > > > static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) > > @@ -82,9 +85,13 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct= kvm_vcpu *vcpu, > > /* > > * Fixed PMCs are supported on all architectural PMUs. Note, KVM= only > > * emulates fixed PMCs for PMU v2+, but the flag itself is still = valid, > > - * i.e. let RDPMC fail due to accessing a non-existent counter. > > + * i.e. let RDPMC fail due to accessing a non-existent counter. = Reject > > + * attempts to read all other types, which are unknown/unsupporte= d. > > */ > > - idx &=3D ~INTEL_RDPMC_FIXED; > > + if (idx & INTEL_RDPMC_TYPE_MASK & ~INTEL_RDPMC_FIXED) You know how I hate to be pedantic (ROFL), but the SDM only says: If the processor does support architectural performance monitoring (CPUID.0AH:EAX[7:0] =E2=89=A0 0), ECX[31:16] specifies type of PMC while ECX[15:0] specifies the index of the PMC to be read within that type. It does not say that the types are bitwise-exclusive. Yes, the types defined thus far are bitwise-exclusive, but who knows what tomorrow may bring? > > + return NULL; > > + > > + idx &=3D INTEL_RDPMC_INDEX_MASK; > > if (fixed) { > > counters =3D pmu->fixed_counters; > > num_counters =3D pmu->nr_arch_fixed_counters; > Reviewed-by: Dapeng Mi