Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4827867rwb; Tue, 8 Aug 2023 14:47:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGZJ4f6xn1FPUlX1mFowEyvQQ0THlz2v1rroVQLvKdlRFAWz4vE9CtmxEW96PFK6GMqdMB1 X-Received: by 2002:a17:902:eac4:b0:1b8:3590:358a with SMTP id p4-20020a170902eac400b001b83590358amr888073pld.19.1691531236108; Tue, 08 Aug 2023 14:47:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691531236; cv=none; d=google.com; s=arc-20160816; b=RjdK/xfQc/SoNMHHhPF/E08EALj5FVPWa1W03cbHmBbcwdk4Rpb6IUjYU0D+g3O2bG zUsAkpuEma86eZcyU/TRiOaOE+EwvFCxDhtLzoVI8W7k9FFjIO2hVS4rpeNdFUG9GPhR fnbmAzS85CarCiZeWtbcM0ww6/HprEP6S8f+gZZEyB301kJpzaf65kvcS6TEsus+qB+V 2ha1G8Js+NNzEYTQPIOePbRP9WtYkDdjdgL1gELJEN12+Wv/0g/5y2gZO/LBzvXeL2oS lZ9+UJXbCM6JD4S9GduTC/A5lFTEdLzhBmYPLuX0pDjIL+JWjrBh3iQZmvPoyFwyZbPy B4GQ== 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=FszrW7aLJQWGeOlctsRyoTtoYJ2O/D7pXLY2EgHGbtU=; fh=UPwqiesAqvJCK7JcrgEpVEdaX86ysnZ9K1cUIusTBBs=; b=sYEE1x9SRMwoXqxsKPiOjAinMzuUqqaYOo8Y9T0ZVuLIm7yUNpvC6aKjhP8NPoQAgo SUz1n9Yme5gmkJKGjvyP8vqp74SYA95zTt676/OYW1TZcOxNnsEqJw7EJqwN8Vtbm3uv wd+7heevLt1lgW4cBSDJZTDizVCQiLoyf6HEtpg1HOrsol0hGU1zzG0xIyYg80Fma/l1 FKTHqXTAGyJOoE2ZLHsga/LQ9Ik0oHYhmiMqRwUyL02WiSdqAn8QgmlKdF7GCe1tlX9Y Oxk1P7/rU9n/VcAiYm9Ak+ZqQghr8zFObk9deQ12zcL7u9oDrxWZe/RSZmrqKG1jlllo oqCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=asvFYbtz; 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 q12-20020a170903204c00b001bbb8a61d3dsi4960289pla.562.2023.08.08.14.47.04; Tue, 08 Aug 2023 14:47:16 -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=asvFYbtz; 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 S232165AbjHHTC6 (ORCPT + 99 others); Tue, 8 Aug 2023 15:02:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230043AbjHHTBy (ORCPT ); Tue, 8 Aug 2023 15:01:54 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CA9B187EB2; 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=NJpOTnRSj8qqZCbQHRVgYJuiVfbmVTUNUDYhP4Pi3xk=; b=asvFYbtzwOK4VtgIRVzN4RP+hi+JkooHmvZRT/ljyX1C1gVeaKG/vf49 WWCUJkpOiVTEcjgA6WK7Oxvi6yUzxGPRmccyvPKYEXgCg6M0tlIiMOAxw GU4i8eCXCFN/poSb8LMTCH9ApdbdqaiNvgIBtbQzpclY7HQaCa4tVgrJK V9UOirZjxX+T7O/gy029EoM1a5EJBYEe1qXzGY5fUXx7ztijDMdsljsoN sjGkaon4xhYRSMt+oCFjtggaPnRlMAo50pt7R2FcF5cNwYmYBynn0wfx1 4dr+WFJNs05/n84ffbZZWvy4O2eJJ4Styj3WyFjg3TgN2kvMjHYdZ1AsU A==; X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="434582091" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="434582091" 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:09 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10795"; a="734377769" X-IronPort-AV: E=Sophos;i="6.01,263,1684825200"; d="scan'208";a="734377769" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by fmsmga007.fm.intel.com with ESMTP; 07 Aug 2023 23:27:05 -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 07/13] perf/x86: Add constraint for guest perf metrics event Date: Tue, 8 Aug 2023 14:31:05 +0800 Message-Id: <20230808063111.1870070-8-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 When guest wants to use PERF_METRICS MSR, a virtual metrics event needs to be created in the perf subsystem so that the guest can have exclusive ownership of the PERF_METRICS MSR. We introduce the new vmetrics constraint, so that we can couple this virtual metrics event with slots event as a events group to involves in the host perf system scheduling. Since Guest metric events are always recognized as vCPU process's events on host, they are time-sharing multiplexed with other host metric events, so that we choose bit 48 (INTEL_PMC_IDX_METRIC_BASE) as the index of this virtual metrics event. Co-developed-by: Yang Weijiang Signed-off-by: Yang Weijiang Signed-off-by: Dapeng Mi --- arch/x86/events/intel/core.c | 28 +++++++++++++++++++++------- arch/x86/events/perf_event.h | 1 + arch/x86/include/asm/perf_event.h | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 2a284ba951b7..60a2384cd936 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3147,17 +3147,26 @@ intel_bts_constraints(struct perf_event *event) return NULL; } +static struct event_constraint *intel_virt_event_constraints[] __read_mostly = { + &vlbr_constraint, + &vmetrics_constraint, +}; + /* - * Note: matches a fake event, like Fixed2. + * Note: matches a virtual event, like vmetrics. */ static struct event_constraint * -intel_vlbr_constraints(struct perf_event *event) +intel_virt_constraints(struct perf_event *event) { - struct event_constraint *c = &vlbr_constraint; + int i; + struct event_constraint *c; - if (unlikely(constraint_match(c, event->hw.config))) { - event->hw.flags |= c->flags; - return c; + for (i = 0; i < ARRAY_SIZE(intel_virt_event_constraints); i++) { + c = intel_virt_event_constraints[i]; + if (unlikely(constraint_match(c, event->hw.config))) { + event->hw.flags |= c->flags; + return c; + } } return NULL; @@ -3357,7 +3366,7 @@ __intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx, { struct event_constraint *c; - c = intel_vlbr_constraints(event); + c = intel_virt_constraints(event); if (c) return c; @@ -5349,6 +5358,11 @@ static struct attribute *spr_tsx_events_attrs[] = { NULL, }; +struct event_constraint vmetrics_constraint = + __EVENT_CONSTRAINT(INTEL_FIXED_VMETRICS_EVENT, + (1ULL << INTEL_PMC_IDX_FIXED_VMETRICS), + FIXED_EVENT_FLAGS, 1, 0, 0); + static ssize_t freeze_on_smi_show(struct device *cdev, struct device_attribute *attr, char *buf) diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index d6de4487348c..895c572f379c 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -1482,6 +1482,7 @@ void reserve_lbr_buffers(void); extern struct event_constraint bts_constraint; extern struct event_constraint vlbr_constraint; +extern struct event_constraint vmetrics_constraint; void intel_pmu_enable_bts(u64 config); diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h index 63e1ce1f4b27..d767807aae91 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -390,6 +390,21 @@ static inline bool is_topdown_idx(int idx) */ #define INTEL_PMC_IDX_FIXED_VLBR (GLOBAL_STATUS_LBRS_FROZEN_BIT) +/* + * We model guest TopDown metrics event tracing similarly. + * + * Guest metric events are recognized as vCPU process's events on host, they + * would be time-sharing multiplexed with other host metric events, so that + * we choose bit 48 (INTEL_PMC_IDX_METRIC_BASE) as the index of virtual + * metrics event. + */ +#define INTEL_PMC_IDX_FIXED_VMETRICS (INTEL_PMC_IDX_METRIC_BASE) + +/* + * Pseudo-encoding the guest metrics event as event=0x00,umask=0x11, + * since it would claim bit 48 which is effectively Fixed16. + */ +#define INTEL_FIXED_VMETRICS_EVENT 0x1100 /* * Pseudo-encoding the guest LBR event as event=0x00,umask=0x1b, * since it would claim bit 58 which is effectively Fixed26. -- 2.34.1