[PATCH RFC v3 07/13] perf/x86: Add constraint for guest perf metrics event
From: Dapeng Mi
Date: Tue Aug 22 2023 - 01:06:31 EST
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 <weijiang.yang@xxxxxxxxx>
Signed-off-by: Yang Weijiang <weijiang.yang@xxxxxxxxx>
Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
---
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