From patchwork Wed Sep 27 03:31:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mi, Dapeng" X-Patchwork-Id: 145340 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2576502vqu; Wed, 27 Sep 2023 05:03:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF2MxZjXa+HO/x0NRs1mt2I7c+Y0RvbOjvd7JCqVpdUGZIZbi6FmcYrDS4STQzjOOexipXL X-Received: by 2002:a05:6358:5913:b0:135:499a:382b with SMTP id g19-20020a056358591300b00135499a382bmr2752234rwf.7.1695816236294; Wed, 27 Sep 2023 05:03:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695816236; cv=none; d=google.com; s=arc-20160816; b=RTb9lYp2dVqt+OrE60WALj5aqPxBUbvr3aUtAJz19N7T5HMDUg6nxQLU2ul/NO1Zf3 UFo/UgNTxxlZwsEyEKmL4rPYxOU+IqSAE0H8JeToHpE1NJu2Jt0gMae3pkEfOc/7pf9E 0Y2DpxPQZZ3nMzMLnFxQjjUDdY/PbkJQeG4LFtFwtmFziP8222lFk+f51yqcnZI+uacC kpOmPfP5fwNwt2OWg6vfe8bG87/BQZa8ydQEVw5RyCgcVKTIUPVlzqpUJ3+kehhNrKVt HP7Bp7tG3uqH2YV0glsGjN6p74U6YzZyEB0TG4oirHKLsA45DB4lxAoNpsaPb+bLygaz 2rOA== 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=Ouo4KRM9R3ZrOoYyWBzN50Kev+zdJsC8Lu1n046pKVM=; fh=SYwTdU7AJ6bh+wuJYieKOpYo9FOAEtzrlv0Krk9EVUs=; b=QJGEIaeLsJjvi0lVRueOqnELAGekIZ+R0JSLuteTWtOMUR3+EDOztPLz8YStQVurMI mw9kXVDF1sOJmdgQlx65VokH0Gghaqo4N78rsc8rtqBCBkNEyEaO4HrQ2iYOOOFE/4k8 1ZgkcTIVCeNlnlWOO4LiQwtVB2x7cniBDY6g/jP2ETgdG2KBamQp2HKYMEr0pmbfx/0J H80Ycja8RRITD/FtqJ9B6qJLPhr43Gft4fvG/xN1uFIItX826G2ux5LKpm2QR1JZSSUu FE6kSobHCxpD6f+c7843VmrHLqe0BwOz5xd8b9uKMe28hNUPo+M/JOs+CdHyMzglIhyA YeSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=L4ScdBYG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id i193-20020a636dca000000b00573f885c4easi13308664pgc.753.2023.09.27.05.03.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 05:03:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=L4ScdBYG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id F1693807CED2; Tue, 26 Sep 2023 21:30:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229762AbjI0EaG (ORCPT + 26 others); Wed, 27 Sep 2023 00:30:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229612AbjI0E3I (ORCPT ); Wed, 27 Sep 2023 00:29:08 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE1451F00; Tue, 26 Sep 2023 20:24:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695785088; x=1727321088; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kiUvulAGjvP1BxKp2QU0q0nvXG70PvnCMAlK3vrVfm8=; b=L4ScdBYGaf6xaHx3mjikmiPWF3Fz2S9sMY8dpJVz5CqGu31FkNR3XYXz eJeHkMuMu4DRhfzzli3v8HbrDP+rJ1fL2O6Py2l5yot50wy5jdLOHhyDg PibxBGYo+YKYkAbv8yohowloyy8nxgu2+z3hNvp6Mi9Am7UFEFzNltjHw PAQdhjCZGJjokogetsqm8yoOl5gP7vYQCzsM0hOBYf//qRnwo2LBT24Ov ya6dm9rG4BXSt4wHoNtdZsCPz/VRUI3Ig6AkowdEM3XCR9EnLQMzuWKCf RQ5HxE1pU23BDiAB78eiq3/iBUNKo6fshUN6c5f5ujd0QOGtZ5B55wa68 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="366780821" X-IronPort-AV: E=Sophos;i="6.03,179,1694761200"; d="scan'208";a="366780821" 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:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="864637170" X-IronPort-AV: E=Sophos;i="6.03,179,1694761200"; d="scan'208";a="864637170" Received: from dmi-pnp-i7.sh.intel.com ([10.239.159.155]) by fmsmga002.fm.intel.com with ESMTP; 26 Sep 2023 20:24:43 -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 07/13] perf/x86: Add constraint for guest perf metrics event Date: Wed, 27 Sep 2023 11:31:18 +0800 Message-Id: <20230927033124.1226509-8-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 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 pete.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 (pete.vger.email [0.0.0.0]); Tue, 26 Sep 2023 21:30:44 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778192205557700859 X-GMAIL-MSGID: 1778192205557700859 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 fa355d3658a6..1c349290677c 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3158,17 +3158,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; @@ -3368,7 +3377,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; @@ -5369,6 +5378,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 c8ba2be7585d..a0d12989a483 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.