Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2418144rdh; Wed, 27 Sep 2023 01:50:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEIrpPBupVzV7i44y3R1uvP8DoM0cqAYLyGdm8R2CMYzPIN1oFoqNCrXa5AmiEG6FAQj1XW X-Received: by 2002:a05:6a21:a58b:b0:15d:4cf1:212e with SMTP id gd11-20020a056a21a58b00b0015d4cf1212emr8605541pzc.4.1695804647911; Wed, 27 Sep 2023 01:50:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695804647; cv=none; d=google.com; s=arc-20160816; b=WfNT0ULdN08JE4vBuA6myku3M0NhvCh6ppC+/tAHIyiylXDZN+OyT6c1e8ZKo6zRY6 f7d/vRQkpMk8D3lcRUAMw+VVSImxYz6XSJsLqeOBbL627Jfvud9CmWSuxekIzPmWwkAF VZu2IpDwNn0f+PK9eKLJbfGBGvZY/+Mri3WgteqsPb83kXQjR3MEtVhP5+MHHgKHh/VR FAPOnvWkcUSj3RiJeWIAohN5EYhsNQ1z/pM1N3lCQII2o8wZgj2dVL2wG8hKPt9BYoKm Q+Fpl2d74fN9Q6NBzj3BL8LvyDte5txPPIphHzL9jJK53+PS4azBdfwlR7z7ZQD7kI7A 7dLw== 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=ylZoRAHxqS5Mr1JJFJmNSsHEXCynfao3EC2rzuIk4V4=; fh=SYwTdU7AJ6bh+wuJYieKOpYo9FOAEtzrlv0Krk9EVUs=; b=l7wv7/9X4LFdXaYShsDbOdO277/EZky1QMz4KKYs19Byo9BXJkiq1iySHdFsOXMWUG nIxceDZ7UBm8pWS4OUq8zmownZehDACcg8AM89MLnRXHscEs5G5yflRNs4l+bjkI7gnE EJok4CyeaJ8LiQTyV5SoC2BBTmjs+y5WR/qz57yOxNfpW1Da7YmF1c8rgAi7fNFSfw5z gj3rEVYmLiX17btLF+an/JIndO2kOxb3otOFiltSxgdBCmojd/5Hn42yumS7oBrjSP0C FSsFWvl0l4GbZh1tmRR4Wkkq/KFtRFeis6aesIh6Ja6XauXlp6VB5gtuI6N+GDgUUI27 5ffw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mtqXLLjb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id c29-20020a63725d000000b00565e509dc75si3462330pgn.359.2023.09.27.01.50.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 01:50:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mtqXLLjb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id B1E4D8304ED8; Tue, 26 Sep 2023 21:13:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229721AbjI0ENj (ORCPT + 99 others); Wed, 27 Sep 2023 00:13:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229762AbjI0EM2 (ORCPT ); Wed, 27 Sep 2023 00:12:28 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 561FE1F05; Tue, 26 Sep 2023 20:25:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695785109; x=1727321109; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4eYYvngh2M8HCkytzEnEBzL4UPADCh50c6O5Q3CPDV8=; b=mtqXLLjbmJ9JxXaFfCPq+hPXq+GQJ0SZZW6FE0F9x9TMHfz5vspGpHJy 4pQ25IcUkFs0eW4JDlq8zPkR0HsXG6ujMr/Ef8GD48vceUCcOqVWJPmOm njJPfgDrHixMPYHvs2fHGZUE9PenZ0EUBHEAdnWYxbjNkpUNvaFPHxiio B+PFK9+2YELTJJmQhrixbqg+FlJ0C5wqJ6vNrJ+7DyT945q21YxWnhzET H5z2m6/gAjTr1pBMUFRURbMPmHSjIgmg28ss3A/QuVf7zx/DGpF8f18La ehjFBIEahmkj3+SbcoRO/fQNCDZm9+b5foSNiZ9Sh8IX9d09L/Y5tl2nI g==; X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="366780849" X-IronPort-AV: E=Sophos;i="6.03,179,1694761200"; d="scan'208";a="366780849" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 20:24:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="864637188" X-IronPort-AV: E=Sophos;i="6.03,179,1694761200"; d="scan'208";a="864637188" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by fmsmga002.fm.intel.com with ESMTP; 26 Sep 2023 20:24:53 -0700 From: Dapeng Mi To: Sean Christopherson , Paolo Bonzini , Peter Zijlstra , Arnaldo Carvalho de Melo , Kan Liang , Like Xu , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter Cc: kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zhenyu Wang , Zhang Xiong , Lv Zhiyuan , Yang Weijiang , Dapeng Mi , Dapeng Mi Subject: [Patch v4 09/13] perf/x86/intel: Handle KVM virtual metrics event in perf system Date: Wed, 27 Sep 2023 11:31:20 +0800 Message-Id: <20230927033124.1226509-10-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230927033124.1226509-1-dapeng1.mi@linux.intel.com> References: <20230927033124.1226509-1-dapeng1.mi@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Tue, 26 Sep 2023 21:13:45 -0700 (PDT) KVM creates a virtual metrics event to claim the PERF_METRICS MSR, but this virtual metrics event can't be recognized by perf system as it uses a different event code with known metrics events. We need to modify perf system code and make the KVM virtual metrics event can be recognized and processed by perf system. The counter of virtual metrics event doesn't save the real count value like other normal events, instead it's used to store the raw data of PERF_METRICS MSR, so KVM can obtain the raw data of PERF_METRICS after the virtual metrics event is disabled. Signed-off-by: Dapeng Mi --- arch/x86/events/intel/core.c | 39 +++++++++++++++++++++++++++--------- arch/x86/events/perf_event.h | 9 ++++++++- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 1c349290677c..df56e091eb25 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -2546,7 +2546,7 @@ static int icl_set_topdown_event_period(struct perf_event *event) hwc->saved_metric = 0; } - if ((hwc->saved_slots) && is_slots_event(event)) { + if (is_slots_event(event)) { wrmsrl(MSR_CORE_PERF_FIXED_CTR3, hwc->saved_slots); wrmsrl(MSR_PERF_METRICS, hwc->saved_metric); } @@ -2619,6 +2619,15 @@ static void __icl_update_topdown_event(struct perf_event *event, } } +static inline void __icl_update_vmetrics_event(struct perf_event *event, u64 metrics) +{ + /* + * For the guest metrics event, the count would be used to save + * the raw data of PERF_METRICS MSR. + */ + local64_set(&event->count, metrics); +} + static void update_saved_topdown_regs(struct perf_event *event, u64 slots, u64 metrics, int metric_end) { @@ -2638,6 +2647,17 @@ static void update_saved_topdown_regs(struct perf_event *event, u64 slots, } } +static inline void _intel_update_topdown_event(struct perf_event *event, + u64 slots, u64 metrics, + u64 last_slots, u64 last_metrics) +{ + if (is_vmetrics_event(event)) + __icl_update_vmetrics_event(event, metrics); + else + __icl_update_topdown_event(event, slots, metrics, + last_slots, last_metrics); +} + /* * Update all active Topdown events. * @@ -2665,9 +2685,9 @@ static u64 intel_update_topdown_event(struct perf_event *event, int metric_end) if (!is_topdown_idx(idx)) continue; other = cpuc->events[idx]; - __icl_update_topdown_event(other, slots, metrics, - event ? event->hw.saved_slots : 0, - event ? event->hw.saved_metric : 0); + _intel_update_topdown_event(other, slots, metrics, + event ? event->hw.saved_slots : 0, + event ? event->hw.saved_metric : 0); } /* @@ -2675,9 +2695,9 @@ static u64 intel_update_topdown_event(struct perf_event *event, int metric_end) * in active_mask e.g. x86_pmu_stop() */ if (event && !test_bit(event->hw.idx, cpuc->active_mask)) { - __icl_update_topdown_event(event, slots, metrics, - event->hw.saved_slots, - event->hw.saved_metric); + _intel_update_topdown_event(event, slots, metrics, + event->hw.saved_slots, + event->hw.saved_metric); /* * In x86_pmu_stop(), the event is cleared in active_mask first, @@ -3858,8 +3878,9 @@ static int core_pmu_hw_config(struct perf_event *event) static bool is_available_metric_event(struct perf_event *event) { - return is_metric_event(event) && - event->attr.config <= INTEL_TD_METRIC_AVAILABLE_MAX; + return (is_metric_event(event) && + event->attr.config <= INTEL_TD_METRIC_AVAILABLE_MAX) || + is_vmetrics_event(event); } static inline bool is_mem_loads_event(struct perf_event *event) diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index a0d12989a483..7238b7f871ce 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -105,9 +105,16 @@ static inline bool is_slots_event(struct perf_event *event) return (event->attr.config & INTEL_ARCH_EVENT_MASK) == INTEL_TD_SLOTS; } +static inline bool is_vmetrics_event(struct perf_event *event) +{ + return (event->attr.config & INTEL_ARCH_EVENT_MASK) == + INTEL_FIXED_VMETRICS_EVENT; +} + static inline bool is_topdown_event(struct perf_event *event) { - return is_metric_event(event) || is_slots_event(event); + return is_metric_event(event) || is_slots_event(event) || + is_vmetrics_event(event); } struct amd_nb { -- 2.34.1