Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp3783957rdb; Sun, 10 Dec 2023 22:27:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IH8/Ei2TjTVpyxqP+TS+Xg7qO9n/9OqVntm39qeA2z4O4Ey9EP56IVBu9RBcsBj6jvulOSK X-Received: by 2002:a05:6e02:20cb:b0:35d:6bc5:e72f with SMTP id 11-20020a056e0220cb00b0035d6bc5e72fmr6923062ilq.47.1702276067468; Sun, 10 Dec 2023 22:27:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702276067; cv=none; d=google.com; s=arc-20160816; b=ZaRt9oBvo2qdRo+KMyHC/NQ2bwjFny2wy2GqYF6hucYHpmRMBEpxusUXOw35pxsPc+ vwoH/dEJlr2UoevRUz7TsVTFx+ZqXHWnkcRui+PgbTLUW2/dZgDntT0c2KTa7ldotiIe o3i9sO+2oz7SPxGwuQC9ERi7DRfd1rw4XnEVjpMU/tJ67cit4TPLKIpZt4GK6NO//uc4 ubS7Yaz6HKLJdB5OOYko670459f85vMJbtD5+lzvYS6BErEAbkeE9udC6El3lJlZaiFu plKcJIaImAvpiUt2qdJQ4ZVX2ChOPelDP7ByrBP2DH/Oam4MUJTAzLu2vFpJuL2m3Nhp EUtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=tYeyTOP3GrupOUWe4Ji5NByMvkd0IvV/SBgPq+9jrkI=; fh=8hMhf/bWLGeBzWmgLGn3yaL17tj1bW1VaYHi0JARJ5w=; b=PvOTFuMenRqWy1X2zzlONR7vC9d5pKzzQMaS07LjEyIMzjjaruvXrWFTJ/C5Ot84w2 mIrqagtmPXXmLk/NhQTgkbXXhwcJDpFr73TBrbKibvVRi9Eu8qU3C96i1xxYySMaLawG vHKhElCCFvxcLqbfIgoDco38GPvGIQPAQifNDd6HMwX7UyvaABto1zUE1Ulg07LV8liG BCOj7i5BntHuBLon9H6AKJXVFD8qsbjYGgBbWki2EhExOw8rs8V9Q7Dkft89F7i9E3GQ b61CW00v79RoGLKvfFbHadKBbYg3bBtbeEBcAObgocz54zh4rsZDqQgLrRfq1okiTZBj 01zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dY0NF6Gd; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id bz39-20020a056a02062700b005bdc61e1793si5729020pgb.358.2023.12.10.22.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 22:27:47 -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=@intel.com header.s=Intel header.b=dY0NF6Gd; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 120F3806582D; Sun, 10 Dec 2023 22:27:45 -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 S233435AbjLKG0M (ORCPT + 99 others); Mon, 11 Dec 2023 01:26:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjLKG0K (ORCPT ); Mon, 11 Dec 2023 01:26:10 -0500 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35A96CD; Sun, 10 Dec 2023 22:26:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702275977; x=1733811977; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=vNb45DFkURFhGFAwMuG9PmhaPApB+4U0Sq1BO/qtZ/Q=; b=dY0NF6GdL+qV+9Wyh5Ar7D0UVzCII3WgNcHnFiq3Z3Zx0cEVLKei/tzh 2drWMK49/cHav0xz+iNL1FcpIBzPbxoUBzmir0nMgJctdWNNut6eidbv4 Pbxco4/lT4ZfbUXZLOVmYNL4OM/wpuXpeI7b0cj83iopwVzgnu1rurdJ2 PAvjEVxzBlNXwtRTApO+5TwBqsU171KTGMEEYpExc8PjfXdbxmguqF9uh GichJVZdd+NdXHYRYebAozvqXSVLk1O4R3YtXqS1GNd2ZBRQ0NVLzBog2 LolNCNAspAsL9NQKwGdf0OBWWtduQhGEX4C7LMHUMgo6cwizOA7vnq9ru g==; X-IronPort-AV: E=McAfee;i="6600,9927,10920"; a="1716778" X-IronPort-AV: E=Sophos;i="6.04,267,1695711600"; d="scan'208";a="1716778" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Dec 2023 22:26:16 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,267,1695711600"; d="scan'208";a="14290999" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.93.5.53]) ([10.93.5.53]) by orviesa002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Dec 2023 22:26:14 -0800 Message-ID: Date: Mon, 11 Dec 2023 14:26:11 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v9 10/28] KVM: x86/pmu: Explicitly check for RDPMC of unsupported Intel PMC types Content-Language: en-US To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Jim Mattson , Jinrong Liang , Aaron Lewis , Like Xu References: <20231202000417.922113-1-seanjc@google.com> <20231202000417.922113-11-seanjc@google.com> From: "Mi, Dapeng" In-Reply-To: <20231202000417.922113-11-seanjc@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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]); Sun, 10 Dec 2023 22:27:45 -0800 (PST) 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 the > 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_PERFCTR0) > > 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/unsupported. > */ > - idx &= ~INTEL_RDPMC_FIXED; > + if (idx & INTEL_RDPMC_TYPE_MASK & ~INTEL_RDPMC_FIXED) > + return NULL; > + > + idx &= INTEL_RDPMC_INDEX_MASK; > if (fixed) { > counters = pmu->fixed_counters; > num_counters = pmu->nr_arch_fixed_counters; Reviewed-by: Dapeng Mi