Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4750773rwb; Tue, 8 Aug 2023 13:16:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGrLtF2R71zIZU40xFd96/EG64RZnScMY9gdG0qew/ONaFGX+seF2Edsqn6mflGWFRskVcu X-Received: by 2002:a2e:b707:0:b0:2b6:bb21:8d74 with SMTP id j7-20020a2eb707000000b002b6bb218d74mr446791ljo.1.1691525762586; Tue, 08 Aug 2023 13:16:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691525762; cv=none; d=google.com; s=arc-20160816; b=IMnixnX/exc3sjJRDoQ2mvCqwRO2vEDmbqjj8VyAwMjO3VBTUdgxH7iyulA1IQOvVQ GKZkiYnlfRnVvodVezTzaXv5P8IwdPINuXDFuAfCXQHEqRDDzl3SbOH2ec3lV+9elNUA s5Ml36lBTKxdFcOAun6r3KWElxJFeQUWPwC9k7IuvpJrEty/JkFe3vCSElz8lT8BTw82 l4SFLT04hWK0ZFwzVA9UXwELEpnW+NAqt5QB9+tWtgZiAkOwiZDVgOdvb4MfVyvUFnJn AOPnHoOJH0rcGAYC/I/6pY84dYA7ssVVeAs9rWWrd4hafegeoRwbc9ttbg/+sifNEhTt 4DUg== 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=HVhwhVLk25zY1A+SdJ+NhaE1QxP4iJjvhmVVGITe6gY=; fh=UPwqiesAqvJCK7JcrgEpVEdaX86ysnZ9K1cUIusTBBs=; b=R0mV4ouFJy/+ns0IJIdJbb4Ej2LC4BulxIyplLEZ4/eRNuLPoPXE2+wNaFG43+HZr9 iKUEizp4JOChwbHk+K/a5KGmmAD6hSjt6nbHWWjsSH1Oq7hKqjadobOOJ+fHGS0/xbvP MQaTvdyxmhQ/A1mXSi8sRabgohbiv+RyufT2Yp4szkty+Dt+xTXRsLgXj8I0yEDc23nk g9/H0lSQWHCgiE7fw/scyDsPyqXKmedFIjzpl1tFtW4LXfKi4xGc4KVgVjyRiNRrAK9k J7YZz7MGJvOMVQ3Dg1hMlyxdMMl+LmAXbjFCr/6bAmmkRDHnuCr0W6JZiFALz62ENBLx 2BRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HtPqLcpf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jt23-20020a170906ca1700b00988c552332fsi6878914ejb.300.2023.08.08.13.15.36; Tue, 08 Aug 2023 13:16:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HtPqLcpf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233746AbjHHTDF (ORCPT + 99 others); Tue, 8 Aug 2023 15:03:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230210AbjHHTBz (ORCPT ); Tue, 8 Aug 2023 15:01:55 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04BF5187EB5; Tue, 8 Aug 2023 10:31: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=1691515901; x=1723051901; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=A2i155f/78hm+K7xs6LxpWipuowJCdKoFaCM1OoXNYw=; b=HtPqLcpfGuvbcMAslGqpQLeg+mGDhJnBmTO2G37SQkAVe6bMtZHvFViT JdtflhiWhTBY/NUWnXzGA5y7gAezhRRWLt30AX3UvW8XEvL38hRn5/KNE UpdvRXOugp8U4/KqZRlhiS4cfLCwsOZxIIPrpeHO4/ryZEbPq6wHurUsp QXI2KuVFV+DNLSJiMi6dgO9NJ1MkLfZSbsrDijiZgc+ceWvtTHVOdcN/C h6kY4SlVKP3VLMoDg1RRkBEOO5oTblGPB3uPP/9i7pIo/BUQgQtY1Y+e9 li28f0R0kTjPonobx4MHhiFjRD4y+Q+KQVZQTJBiIT+Uh12NX9vd+/QvL A==; X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="434582139" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="434582139" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2023 23:27:24 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="734377806" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="734377806" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by fmsmga007.fm.intel.com with ESMTP; 07 Aug 2023 23:27:19 -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 RFV v2 09/13] perf/x86/intel: Handle KVM virtual metrics event in perf system Date: Tue, 8 Aug 2023 14:31:07 +0800 Message-Id: <20230808063111.1870070-10-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808063111.1870070-1-dapeng1.mi@linux.intel.com> References: <20230808063111.1870070-1-dapeng1.mi@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 37 ++++++++++++++++++++++++++++-------- arch/x86/events/perf_event.h | 9 ++++++++- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 60a2384cd936..564f602b81f1 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -2608,6 +2608,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) { @@ -2627,6 +2636,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. * @@ -2654,9 +2674,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); } /* @@ -2664,9 +2684,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, @@ -3847,8 +3867,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 895c572f379c..e0703f743713 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