Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp103885rdb; Tue, 31 Oct 2023 01:59:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGl/0N619BSmW/3g5Zg+UYia3X9KGdk2dXnJ9YE+QJIkb/fc19YEnpc9vRMgNbna+L6AXbM X-Received: by 2002:a17:90b:f83:b0:27d:2108:af18 with SMTP id ft3-20020a17090b0f8300b0027d2108af18mr11268395pjb.25.1698742757084; Tue, 31 Oct 2023 01:59:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698742757; cv=none; d=google.com; s=arc-20160816; b=uhzfv9wFvVWy1TPylNuvJd6j3bc2rQx4cCCE1Tb/xW5bPuZ2Eo1ZJ9QMrv9GpdIBEV /DDR7MzG0O1IcqIVD8tHgrDbaETn7uUQwB+IhSU94bVHFVRXnPvfa948uEl6G2wERFTR 4HPsiQNEmBwKb8vjaW8wwLsf2VQQFFRDrq2EMGGYuJAeSmbOh7ZqXOQnjgYjhzUZ9vgm ynjdHU3EJGGPZEy3x46Q7xU8pDM9qWZg3+8C/LVE1vBevfjIXHXw3b8HmtHAb0cPQPY/ VZ0Jut7s75OYzylj8CHphFnUlhM24FEpJ65BHMnCttDY51R1wz5qwGhwr3dAi+zARgZi Vb6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JSScWVlLwWi59xyO7sZiM2/5J3zgsNiHFPBn0dFN7y4=; fh=r/CSYkmVqow/F40IE/fnZlug3mhg+bnKYMiBh80d4zU=; b=QHjMFJflq+apEtuguz0FOtS+jayGucoyx5iA19cAYtDl3Qz1sgHZcBUnG17PkzP65X lOEfkppJ8YouWLzaIst+q09XorifTUb7k8blRCMQq2HApycrsl6rzdi4//0q8K0sac+U aursgVNzAnUjUrfyl7P8Fllgf8XkWDkqZz/KX9KlJekOB6qe0+J89xpuV9dcMVVBM3WC TpULRZtz/c6YmKC9n1/NVt4Av/cNzASdoKlfoGARN5YSZOuO5j7LeS4b5qDWhr87UObd Txltu77+rANEx9EamnN4piEDItbUO0Ehx8TlT0ot5H3u4Shg/GHBKZ6Z8EQQQxfJx80Y ro/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hj290L3v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id h18-20020a17090ac39200b00262f937bf93si674071pjt.77.2023.10.31.01.59.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 01:59:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hj290L3v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (Postfix) with ESMTP id 4F1AB80C8414; Tue, 31 Oct 2023 01:59:14 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343580AbjJaI6t (ORCPT + 99 others); Tue, 31 Oct 2023 04:58:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235753AbjJaI6n (ORCPT ); Tue, 31 Oct 2023 04:58:43 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C495DDF; Tue, 31 Oct 2023 01:58:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698742720; x=1730278720; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6HMFBByNeJuGrMJLEILM43k8UegRNuWkjShAtvq8eEI=; b=hj290L3vbOCPo5+1T3tiK20nLF/waP7bTodyuvforIJOboppxehXDXDJ JEkFCENAdDOJ712SfloRAna3dPtqatPM+IaiEbZRnwpruSSnmeDnvhw+n vSb4zPu498kywYUHJd4dHN8C650jk+DtQo8SUiTfOLpgfFecjwOobPAUA lOg23+hVV6Q7I3V2+sr5UmgRrd5sqnl3upJbJ3AfpuIKnqXmBpJKasqdQ TJfXEewlsuexUWYOKj5aki0zbJQ/d7pjMeruYulk98GuVXwU6bmSLVFHH b+dccN2RCMmIFbWkrQCIVP/NBJdLXPQzgOQWmLb9IRwaUFQhbI7KfvA/o Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10879"; a="378627571" X-IronPort-AV: E=Sophos;i="6.03,265,1694761200"; d="scan'208";a="378627571" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Oct 2023 01:58:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.03,265,1694761200"; d="scan'208";a="8257906" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by orviesa001.jf.intel.com with ESMTP; 31 Oct 2023 01:58:37 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zhenyu Wang , Zhang Xiong , Jim Mattson , Mingwei Zhang , Like Xu , Dapeng Mi , Dapeng Mi , Like Xu Subject: [Patch 2/2] KVM: x86/pmu: Support PMU fixed counter 3 Date: Tue, 31 Oct 2023 17:06:13 +0800 Message-Id: <20231031090613.2872700-3-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231031090613.2872700-1-dapeng1.mi@linux.intel.com> References: <20231031090613.2872700-1-dapeng1.mi@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 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 fry.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 (fry.vger.email [0.0.0.0]); Tue, 31 Oct 2023 01:59:14 -0700 (PDT) The TopDown slots event can be enabled on gp counter or fixed counter 3 and it does not differ from other fixed counters in terms of the use of count and sampling modes (except for the hardware logic for event accumulation). According to commit 6017608936c1 ("perf/x86/intel: Add Icelake support"), KVM or any perf in-kernel user needs to reprogram fixed counter 3 via the kernel-defined TopDown slots event for real fixed counter 3 on the host. Co-developed-by: Like Xu Signed-off-by: Like Xu Signed-off-by: Dapeng Mi --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++++ arch/x86/kvm/x86.c | 4 ++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index d7036982332e..44b47950491a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -517,7 +517,7 @@ struct kvm_pmc { #define KVM_INTEL_PMC_MAX_GENERIC 8 #define MSR_ARCH_PERFMON_PERFCTR_MAX (MSR_ARCH_PERFMON_PERFCTR0 + KVM_INTEL_PMC_MAX_GENERIC - 1) #define MSR_ARCH_PERFMON_EVENTSEL_MAX (MSR_ARCH_PERFMON_EVENTSEL0 + KVM_INTEL_PMC_MAX_GENERIC - 1) -#define KVM_PMC_MAX_FIXED 3 +#define KVM_PMC_MAX_FIXED 4 #define MSR_ARCH_PERFMON_FIXED_CTR_MAX (MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_PMC_MAX_FIXED - 1) #define KVM_AMD_PMC_MAX_GENERIC 6 struct kvm_pmu { diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index e32353f1143f..d5af64b1ef69 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -45,6 +45,14 @@ enum intel_pmu_architectural_events { * core crystal clock or the bus clock (yeah, "architectural"). */ PSEUDO_ARCH_REFERENCE_CYCLES = NR_REAL_INTEL_ARCH_EVENTS, + /* + * Pseudo-architectural event used to implement IA32_FIXED_CTR3, a.k.a. + * topDown slots. The topdown slots event counts the total number of + * available slots for an unhalted logical processor. The topdwon slots + * event with PERF_METRICS MSR together provides support for topdown + * micro-architecture analysis method. + */ + PSEUDO_ARCH_TOPDOWN_SLOTS, NR_INTEL_ARCH_EVENTS, }; @@ -61,6 +69,7 @@ static struct { [INTEL_ARCH_BRANCHES_MISPREDICTED] = { 0xc5, 0x00 }, [INTEL_ARCH_TOPDOWN_SLOTS] = { 0xa4, 0x01 }, [PSEUDO_ARCH_REFERENCE_CYCLES] = { 0x00, 0x03 }, + [PSEUDO_ARCH_TOPDOWN_SLOTS] = { 0x00, 0x04 }, }; /* mapping between fixed pmc index and intel_arch_events array */ @@ -68,6 +77,7 @@ static int fixed_pmc_events[] = { [0] = INTEL_ARCH_INSTRUCTIONS_RETIRED, [1] = INTEL_ARCH_CPU_CYCLES, [2] = PSEUDO_ARCH_REFERENCE_CYCLES, + [3] = PSEUDO_ARCH_TOPDOWN_SLOTS, }; static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2c924075f6f1..90c60b6899a5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1468,7 +1468,7 @@ static const u32 msrs_to_save_base[] = { static const u32 msrs_to_save_pmu[] = { MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, - MSR_ARCH_PERFMON_FIXED_CTR0 + 2, + MSR_ARCH_PERFMON_FIXED_CTR2, MSR_ARCH_PERFMON_FIXED_CTR3, MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL, MSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG, @@ -7318,7 +7318,7 @@ static void kvm_init_msr_lists(void) { unsigned i; - BUILD_BUG_ON_MSG(KVM_PMC_MAX_FIXED != 3, + BUILD_BUG_ON_MSG(KVM_PMC_MAX_FIXED != 4, "Please update the fixed PMCs in msrs_to_save_pmu[]"); num_msrs_to_save = 0; -- 2.34.1