Skip to contentSkip to conversation

KVM: x86/pmu: Enable Fixed Counter3 and Topdown Perf Metrics

RFC PATCH In review Like Xu 3 participants 13 messages Dec 12, 2022 – Dec 15, 2022

Subsystems (7)
x86 KVM perf events arm64 +3 more subsystems
lore
Share & tools
lore

Like Xu

author
#0 lore raw

cover KVM: x86/pmu: Enable Fixed Counter3 and Topdown Perf Metrics

Hi,

The Ice Lake core PMU provides built-in support for Top-down u-arch Analysis (TMA) method level 1 metrics. These metrics are always available to cross-validate performance observations, freeing general purpose counters to count other events in high counter utilization scenarios. For more details about the method, refer to Top-Down Analysis Method chapter (Appendix B.1) of the Intel® 64 and IA-32 Architectures Optimization Reference Manual. (SDM 19.3.9.3 Performance Metrics)

This patchset enables Intel Guest Topdow for KVM-based guests. Its basic enabling framework remains unchanged, a perf_metric msr is introduced, a group (rather than one) of perf_events is created in KVM by binding to fiexed counter3 to obtain hardware resources, and the guest value of perf_metric msr is assembled based on the count of grouped perf_events.

On KVM, patches 0004/5/6 may be reviewd independently if KVM only enable fixed counter3 as normal slot event for count and sampling. Patch 7 updates the infrastructure for creating grouped events in KVM, and patch 8 uses group events to emulate guest MSR_PERF_METRICS.

On Perf, Patches 0001-0003 are awaiting review for tip/perf/core, and could be accepted separately if they make sense. TBH, I don't think our perf/core is fully prepared to support kernel space grouped counters, considering comments around perf_enable_diasable(). But after much exploration on my part, this is probably the most promising way to get KVM to create slots plus metrics events. If the addition of *group_leader messes things up, please shout at me on your needs.

More details in each commit messages may answer code-related questions.

A classic perf tool usage on a linux guest is as follows:

$ perf stat --topdown --td-level=1 -I1000 --no-metric-only sleep 1
#           time             counts unit events
     1.000548528         34,505,682      slots
     1.000548528         14,208,222      topdown-retiring                 #     41.5% Retiring
     1.000548528          1,623,796      topdown-bad-spec                 #      4.7% Bad Speculation
     1.000548528         14,614,171      topdown-fe-bound                 #     42.7% Frontend Bound
     1.000548528          3,788,859      topdown-be-bound                 #     11.1% Backend Bound

Related KUT will follow if there are no obstructive negative comments.

Nit, pre-patches includes:
https://lore.kernel.org/kvm/[email protected]/
https://lore.kernel.org/kvm/[email protected]/

Please feel free to comment and share your feedback.

Thanks,

Like Xu (8):
perf/core: Add *group_leader to perf_event_create_kernel_counter()
perf: x86/core: Expose the available number of the Topdown metrics
perf: x86/core: Snyc PERF_METRICS bit together with fixed counter3
KVM: x86/pmu: Add Intel CPUID-hinted Topdown Slots event
KVM: x86/pmu: Add kernel-defined slots event to enable Fixed Counter3
KVM: x86/pmu: properly use INTEL_PMC_FIXED_RDPMC_BASE macro
KVM: x86/pmu: Use flex *event arrays to implement grouped events
KVM: x86/pmu: Add MSR_PERF_METRICS MSR emulation to enable Topdown

 arch/arm64/kvm/pmu-emul.c                 |   4 +-
 arch/x86/events/core.c                    |   1 +
 arch/x86/events/intel/core.c              |   3 +
 arch/x86/include/asm/kvm_host.h           |  14 +-
 arch/x86/include/asm/msr-index.h          |   1 +
 arch/x86/include/asm/perf_event.h         |   1 +
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c |   4 +-
 arch/x86/kvm/pmu.c                        | 149 ++++++++++++++++++++--
 arch/x86/kvm/pmu.h                        |  31 +++--
 arch/x86/kvm/svm/pmu.c                    |   1 +
 arch/x86/kvm/vmx/pmu_intel.c              |  53 +++++++-
 arch/x86/kvm/vmx/vmx.c                    |   3 +
 arch/x86/kvm/x86.c                        |   9 +-
 include/linux/perf_event.h                |   1 +
 kernel/events/core.c                      |   4 +-
 kernel/events/hw_breakpoint.c             |   4 +-
 kernel/events/hw_breakpoint_test.c        |   2 +-
 kernel/watchdog_hld.c                     |   2 +-
 18 files changed, 239 insertions(+), 48 deletions(-)
Signature · 1 line
2.38.2
author
#1 lore raw

RFC PATCH 1/8 perf/core: Add *group_leader to perf_event_create_kernel_counter()

From: Like Xu <[email protected]>

Like syscalls users, kernel-space perf_event creators may also use group counters abstraction to gain pmu functionalities, and an in-kernel counter groups behave much like normal 'single' counters, following the group semantics-based behavior.

No functional changes at this time. An example will be that KVM creates Intel slot event as group leader and other topdown metric events to emulate MSR_PERF_METRICS pmu capability for guests.

---
 arch/arm64/kvm/pmu-emul.c                 | 4 ++--
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 4 ++--
 arch/x86/kvm/pmu.c                        | 2 +-
 arch/x86/kvm/vmx/pmu_intel.c              | 2 +-
 include/linux/perf_event.h                | 1 +
 kernel/events/core.c                      | 4 +++-
 kernel/events/hw_breakpoint.c             | 4 ++--
 kernel/events/hw_breakpoint_test.c        | 2 +-
 kernel/watchdog_hld.c                     | 2 +-
 9 files changed, 14 insertions(+), 11 deletions(-)
arch/arm64/kvm/pmu-emul.c +2 -2
index 24908400e190..11c3386bc86b 100644
@@ -624,7 +624,7 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc)
 
 	attr.sample_period = compute_period(pmc, kvm_pmu_get_pmc_value(pmc));
 
-	event = perf_event_create_kernel_counter(&attr, -1, current,
+	event = perf_event_create_kernel_counter(&attr, -1, current, NULL,
 						 kvm_pmu_perf_overflow, pmc);
 
 	if (IS_ERR(event)) {
@@ -713,7 +713,7 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(void)
 	attr.config = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
 	attr.sample_period = GENMASK(63, 0);
 
-	event = perf_event_create_kernel_counter(&attr, -1, current,
+	event = perf_event_create_kernel_counter(&attr, -1, current, NULL,
 						 kvm_pmu_perf_overflow, &attr);
 
 	if (IS_ERR(event)) {
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +2 -2
index d961ae3ed96e..43e54bb200cd 100644
@@ -952,12 +952,12 @@ static int measure_residency_fn(struct perf_event_attr *miss_attr,
 	u64 tmp;
 
 	miss_event = perf_event_create_kernel_counter(miss_attr, plr->cpu,
-						      NULL, NULL, NULL);
+						      NULL, NULL, NULL, NULL);
 	if (IS_ERR(miss_event))
 		goto out;
 
 	hit_event = perf_event_create_kernel_counter(hit_attr, plr->cpu,
-						     NULL, NULL, NULL);
+						     NULL, NULL, NULL, NULL);
 	if (IS_ERR(hit_event))
 		goto out_miss;
 
arch/x86/kvm/pmu.c +1 -1
index eb594620dd75..f6c8180241d7 100644
@@ -204,7 +204,7 @@ static int pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, u64 config,
 			attr.precise_ip = 3;
 	}
 
-	event = perf_event_create_kernel_counter(&attr, -1, current,
+	event = perf_event_create_kernel_counter(&attr, -1, current, NULL,
 						 kvm_perf_overflow, pmc);
 	if (IS_ERR(event)) {
 		pr_debug_ratelimited("kvm_pmu: event creation failed %ld for pmc->idx = %d\n",
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index f951dc756456..b746381307c7 100644
@@ -299,7 +299,7 @@ int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu)
 	}
 
 	event = perf_event_create_kernel_counter(&attr, -1,
-						current, NULL, NULL);
+						current, NULL, NULL, NULL);
 	if (IS_ERR(event)) {
 		pr_debug_ratelimited("%s: failed %ld\n",
 					__func__, PTR_ERR(event));
include/linux/perf_event.h +1 -0
index 0031f7b4d9ab..5f34e1d0bff8 100644
@@ -1023,6 +1023,7 @@ extern struct perf_event *
 perf_event_create_kernel_counter(struct perf_event_attr *attr,
 				int cpu,
 				struct task_struct *task,
+				struct perf_event *group_leader,
 				perf_overflow_handler_t callback,
 				void *context);
 extern void perf_pmu_migrate_context(struct pmu *pmu,
kernel/events/core.c +3 -1
index 7f04f995c975..f671b1a9a691 100644
@@ -12674,12 +12674,14 @@ SYSCALL_DEFINE5(perf_event_open,
  * @attr: attributes of the counter to create
  * @cpu: cpu in which the counter is bound
  * @task: task to profile (NULL for percpu)
+ * @group_leader: event group leader
  * @overflow_handler: callback to trigger when we hit the event
  * @context: context data could be used in overflow_handler callback
  */
 struct perf_event *
 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
 				 struct task_struct *task,
+				 struct perf_event *group_leader,
 				 perf_overflow_handler_t overflow_handler,
 				 void *context)
 {
@@ -12694,7 +12696,7 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
 	if (attr->aux_output)
 		return ERR_PTR(-EINVAL);
 
-	event = perf_event_alloc(attr, cpu, task, NULL, NULL,
+	event = perf_event_alloc(attr, cpu, task, group_leader, NULL,
 				 overflow_handler, context, -1);
 	if (IS_ERR(event)) {
 		err = PTR_ERR(event);
kernel/events/hw_breakpoint.c +2 -2
index c3797701339c..65b5b1421e62 100644
@@ -771,7 +771,7 @@ register_user_hw_breakpoint(struct perf_event_attr *attr,
 			    void *context,
 			    struct task_struct *tsk)
 {
-	return perf_event_create_kernel_counter(attr, -1, tsk, triggered,
+	return perf_event_create_kernel_counter(attr, -1, tsk, NULL, triggered,
 						context);
 }
 EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
@@ -881,7 +881,7 @@ register_wide_hw_breakpoint(struct perf_event_attr *attr,
 
 	cpus_read_lock();
 	for_each_online_cpu(cpu) {
-		bp = perf_event_create_kernel_counter(attr, cpu, NULL,
+		bp = perf_event_create_kernel_counter(attr, cpu, NULL, NULL,
 						      triggered, context);
 		if (IS_ERR(bp)) {
 			err = PTR_ERR(bp);
kernel/events/hw_breakpoint_test.c +1 -1
index c57610f52bb4..b3597df12284 100644
@@ -39,7 +39,7 @@ static struct perf_event *register_test_bp(int cpu, struct task_struct *tsk, int
 	attr.bp_addr = (unsigned long)&break_vars[idx];
 	attr.bp_len = HW_BREAKPOINT_LEN_1;
 	attr.bp_type = HW_BREAKPOINT_RW;
-	return perf_event_create_kernel_counter(&attr, cpu, tsk, NULL, NULL);
+	return perf_event_create_kernel_counter(&attr, cpu, tsk, NULL, NULL, NULL);
 }
 
 static void unregister_test_bp(struct perf_event **bp)
kernel/watchdog_hld.c +1 -1
index 247bf0b1582c..bb755dadba54 100644
@@ -173,7 +173,7 @@ static int hardlockup_detector_event_create(void)
 	wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
 
 	/* Try to register using hardware perf events */
-	evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL,
+	evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL, NULL,
 					       watchdog_overflow_callback, NULL);
 	if (IS_ERR(evt)) {
 		pr_debug("Perf event create on CPU %d failed with %ld\n", cpu,
Signature · 1 line
2.38.2
maintainer
#2 lore raw

Re: [PATCH RFC 1/8] perf/core: Add *group_leader to perf_event_create_kernel_counter()

Quoted 35 lines · Like Xu

From: Like Xu <[email protected]>

Like syscalls users, kernel-space perf_event creators may also use group counters abstraction to gain pmu functionalities, and an in-kernel counter groups behave much like normal 'single' counters, following the group semantics-based behavior.

No functional changes at this time. An example will be that KVM creates Intel slot event as group leader and other topdown metric events to emulate MSR_PERF_METRICS pmu capability for guests.

---
 arch/arm64/kvm/pmu-emul.c                 | 4 ++--
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 4 ++--
 arch/x86/kvm/pmu.c                        | 2 +-
 arch/x86/kvm/vmx/pmu_intel.c              | 2 +-
 include/linux/perf_event.h                | 1 +
 kernel/events/core.c                      | 4 +++-
 kernel/events/hw_breakpoint.c             | 4 ++--
 kernel/events/hw_breakpoint_test.c        | 2 +-
 kernel/watchdog_hld.c                     | 2 +-
 9 files changed, 14 insertions(+), 11 deletions(-)
arch/arm64/kvm/pmu-emul.c +0 -0
index 24908400e190..11c3386bc86b 100644

arch/arm64/kvm/pmu-emul.c static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc) · line 628

Quoted 6 lines
arch/arm64/kvm/pmu-emul.c +1 -1
@@ -624,7 +624,7 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc)
 
 	attr.sample_period = compute_period(pmc, kvm_pmu_get_pmc_value(pmc));
 
-	event = perf_event_create_kernel_counter(&attr, -1, current,
+	event = perf_event_create_kernel_counter(&attr, -1, current, NULL,
 						 kvm_pmu_perf_overflow, pmc);

Wouldn't it be better to have a separate helper that takes the group leader as a parameter, and reimplement perf_event_create_kernel_counter() in term of this helper?

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author Applied
#3 lore raw

Re: [PATCH RFC 1/8] perf/core: Add *group_leader to perf_event_create_kernel_counter()

Quoted 48 lines · Marc Zyngier
Quoted 35 lines · Like Xu

From: Like Xu <[email protected]>

Like syscalls users, kernel-space perf_event creators may also use group counters abstraction to gain pmu functionalities, and an in-kernel counter groups behave much like normal 'single' counters, following the group semantics-based behavior.

No functional changes at this time. An example will be that KVM creates Intel slot event as group leader and other topdown metric events to emulate MSR_PERF_METRICS pmu capability for guests.

---
  arch/arm64/kvm/pmu-emul.c                 | 4 ++--
  arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 4 ++--
  arch/x86/kvm/pmu.c                        | 2 +-
  arch/x86/kvm/vmx/pmu_intel.c              | 2 +-
  include/linux/perf_event.h                | 1 +
  kernel/events/core.c                      | 4 +++-
  kernel/events/hw_breakpoint.c             | 4 ++--
  kernel/events/hw_breakpoint_test.c        | 2 +-
  kernel/watchdog_hld.c                     | 2 +-
  9 files changed, 14 insertions(+), 11 deletions(-)
arch/arm64/kvm/pmu-emul.c +0 -0
index 24908400e190..11c3386bc86b 100644

arch/arm64/kvm/pmu-emul.c static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc) · line 628

Quoted 6 lines
arch/arm64/kvm/pmu-emul.c +1 -1
@@ -624,7 +624,7 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc)
 
  	attr.sample_period = compute_period(pmc, kvm_pmu_get_pmc_value(pmc));
 
-	event = perf_event_create_kernel_counter(&attr, -1, current,
+	event = perf_event_create_kernel_counter(&attr, -1, current, NULL,
  						 kvm_pmu_perf_overflow, pmc);

Wouldn't it be better to have a separate helper that takes the group leader as a parameter, and reimplement perf_event_create_kernel_counter() in term of this helper?

Quoted 2 lines
	M.

Applied. It makes changes more concise, thank you.

#4 lore raw

Re: [PATCH RFC 1/8] perf/core: Add *group_leader to perf_event_create_kernel_counter()

In reply to Like Xu
Quoted 20 lines
kernel/events/core.c +2 -0
index 7f04f995c975..f671b1a9a691 100644
@@ -12674,12 +12674,14 @@ SYSCALL_DEFINE5(perf_event_open,
  * @attr: attributes of the counter to create
  * @cpu: cpu in which the counter is bound
  * @task: task to profile (NULL for percpu)
+ * @group_leader: event group leader
  * @overflow_handler: callback to trigger when we hit the event
  * @context: context data could be used in overflow_handler callback
  */
 struct perf_event *
 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
 				 struct task_struct *task,
+				 struct perf_event *group_leader,
 				 perf_overflow_handler_t overflow_handler,
 				 void *context)
 {

kernel/events/core.c perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, · line 12700

Quoted 6 lines
kernel/events/core.c +1 -1
@@ -12694,7 +12696,7 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
 	if (attr->aux_output)
 		return ERR_PTR(-EINVAL);
 
-	event = perf_event_alloc(attr, cpu, task, NULL, NULL,
+	event = perf_event_alloc(attr, cpu, task, group_leader, NULL,
 				 overflow_handler, context, -1);

Grouping involves lot of complexities. Setting group_leader won't be sufficient. Please see perf_event_open() syscall code for more detail.

Thanks,
Ravi

author
#5 lore raw

Re: [PATCH RFC 1/8] perf/core: Add *group_leader to perf_event_create_kernel_counter()

Quoted 30 lines · Ravi Bangoria
Quoted 20 lines
kernel/events/core.c +2 -0
index 7f04f995c975..f671b1a9a691 100644
@@ -12674,12 +12674,14 @@ SYSCALL_DEFINE5(perf_event_open,
   * @attr: attributes of the counter to create
   * @cpu: cpu in which the counter is bound
   * @task: task to profile (NULL for percpu)
+ * @group_leader: event group leader
   * @overflow_handler: callback to trigger when we hit the event
   * @context: context data could be used in overflow_handler callback
   */
  struct perf_event *
  perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  				 struct task_struct *task,
+				 struct perf_event *group_leader,
  				 perf_overflow_handler_t overflow_handler,
  				 void *context)
  {

kernel/events/core.c perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, · line 12700

Quoted 6 lines
kernel/events/core.c +1 -1
@@ -12694,7 +12696,7 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  	if (attr->aux_output)
  		return ERR_PTR(-EINVAL);
 
-	event = perf_event_alloc(attr, cpu, task, NULL, NULL,
+	event = perf_event_alloc(attr, cpu, task, group_leader, NULL,
  				 overflow_handler, context, -1);

Grouping involves lot of complexities. Setting group_leader won't be sufficient. Please see perf_event_open() syscall code for more detail.

Quoted 2 lines

Thanks,
Ravi

This is the main reason why the RFC tag is added. More detailed professional reviews is encouraged.

AFAI, there does exist a number of code gaps here to support grouped events in the kernel, but there are also opportunities, as there may be other new use cases bringing innovation.

I have to confirm this idea with maintainers first, the alternative is to create yet another special perf_event similar to PMC_IDX_FIXED_VLBR, which schedules perf_metrics MSR for KVM stuff.

PeterZ, any input ?

author
#6 lore raw

RFC PATCH 2/8 perf: x86/core: Expose the available number of the Topdown metrics

In reply to Like Xu

From: Like Xu <[email protected]>

Intel Sapphire Rapids server has 8 metrics events, while the Intel Ice Lake only supports 4 metrics events. The available number of the Topdown metrics are model specific without architecture hint.

To support guest Topdown metrics, KVM may only rely on the cpu model to emulate the correct number of metrics event on the platforms. It would be nice to have the perf core tell KVM the available number of Topdown metrics, just like x86_pmu.num_counters.

---
 arch/x86/events/core.c            | 1 +
 arch/x86/include/asm/perf_event.h | 1 +
 2 files changed, 2 insertions(+)
arch/x86/events/core.c +1 -0
index b30b8bbcd1e2..d0d84c7a6876 100644
@@ -3006,6 +3006,7 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
 	 * which available for all cores.
 	 */
 	cap->num_counters_gp	= x86_pmu.num_counters;
+	cap->num_topdown_events = x86_pmu.num_topdown_events;
 	cap->num_counters_fixed	= x86_pmu.num_counters_fixed;
 	cap->bit_width_gp	= x86_pmu.cntval_bits;
 	cap->bit_width_fixed	= x86_pmu.cntval_bits;
arch/x86/include/asm/perf_event.h +1 -0
index 5d0f6891ae61..3e263d291595 100644
@@ -219,6 +219,7 @@ struct x86_pmu_capability {
 	int		version;
 	int		num_counters_gp;
 	int		num_counters_fixed;
+	int		num_topdown_events;
 	int		bit_width_gp;
 	int		bit_width_fixed;
 	unsigned int	events_mask;
Signature · 1 line
2.38.2
author
#7 lore raw

RFC PATCH 3/8 perf: x86/core: Snyc PERF_METRICS bit together with fixed counter3

In reply to Like Xu

From: Like Xu <[email protected]>

When the guest uses topdown (the fixed counter 3 and perf_metrics msr), the sharing rule on the PERF_METRICS bit on the GLOBAL_CTRL msr does not change, that is, it should be updated synchronously with the fixed counter 3. Considering that guest topdown feature has just been enabled, this is not a strictly bug fix.

---
 arch/x86/events/intel/core.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/events/intel/core.c +3 -0
index 1b92bf05fd65..e7897fd9f7ab 100644
@@ -2436,6 +2436,8 @@ static void intel_pmu_disable_fixed(struct perf_event *event)
 		 */
 		if (*(u64 *)cpuc->active_mask & INTEL_PMC_OTHER_TOPDOWN_BITS(idx))
 			return;
+
+		intel_clear_masks(event, GLOBAL_CTRL_EN_PERF_METRICS);
 		idx = INTEL_PMC_IDX_FIXED_SLOTS;
 	}
 
@@ -2729,6 +2731,7 @@ static void intel_pmu_enable_fixed(struct perf_event *event)
 		if (*(u64 *)cpuc->active_mask & INTEL_PMC_OTHER_TOPDOWN_BITS(idx))
 			return;
 
+		intel_set_masks(event, GLOBAL_CTRL_EN_PERF_METRICS);
 		idx = INTEL_PMC_IDX_FIXED_SLOTS;
 	}
 
Signature · 1 line
2.38.2
author
#8 lore raw

RFC PATCH 4/8 KVM: x86/pmu: Add Intel CPUID-hinted Topdown Slots event

In reply to Like Xu

From: Like Xu <[email protected]>

This event counts the total number of available slots for an unhalted logical processor. Software can use this event as the denominator for the top-level metrics of the Top-down Microarchitecture Analysis method.

Although the MSR_PERF_METRICS MSR required for topdown events is not currently available in the guest, relying only on the data provided by the slots event is sufficient for pmu users to perceive differences in cpu pipeline machine-width across micro-architectures.

The standalone slots event, like the instruction event, can be counted with gp counter or fixed counter 3 (if any). As the last CPUID-hinted Architectural Performance Events, its availability is also controlled by CPUID.AH.EBX. On the Linux, perf user may encode "-e cpu/event=0xa4, umask=0x01/" or "-e cpu/slots/" to count slots events.

---
 arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +6 -4
index b746381307c7..d86a6ba8c3f9 100644
@@ -31,10 +31,11 @@
  * 4 - PERF_COUNT_HW_CACHE_MISSES
  * 5 - PERF_COUNT_HW_BRANCH_INSTRUCTIONS
  * 6 - PERF_COUNT_HW_BRANCH_MISSES
+ * 7 - CPUID-hinted Topdown Slots event (available on gp counter)
  *
  * the second part of hw_events is defined by the generic kernel perf:
  *
- * 7 - PERF_COUNT_HW_REF_CPU_CYCLES
+ * 8 - PERF_COUNT_HW_REF_CPU_CYCLES
  */
 static struct kvm_pmu_hw_event intel_arch_events[] = {
 	[0] = { 0x3c, 0x00 },
@@ -44,12 +45,13 @@ static struct kvm_pmu_hw_event intel_arch_events[] = {
 	[4] = { 0x2e, 0x41 },
 	[5] = { 0xc4, 0x00 },
 	[6] = { 0xc5, 0x00 },
+	[7] = { 0xa4, 0x01 },
 	/* The above index must match CPUID 0x0A.EBX bit vector */
-	[7] = { 0x00, 0x03 },
+	[8] = { 0x00, 0x03 },
 };
 
 /* mapping between fixed pmc index and intel_arch_events array */
-static int fixed_pmc_events[] = {1, 0, 7};
+static int fixed_pmc_events[] = {1, 0, 8};
 
 static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
 {
@@ -109,7 +111,7 @@ static bool intel_hw_event_available(struct kvm_pmc *pmc)
 			continue;
 
 		/* disable event that reported as not present by cpuid */
-		if ((i < 7) && !(pmu->available_event_types & (1 << i)))
+		if (i < 8 && !(pmu->available_event_types & (1 << i)))
 			return false;
 
 		break;
Signature · 1 line
2.38.2
author
#9 lore raw

RFC PATCH 5/8 KVM: x86/pmu: Add kernel-defined slots event to enable Fixed Counter3

In reply to Like Xu

From: Like Xu <[email protected]>

The Topdown Slots event can be enabled on gp counter or fixed counter3 and it does not differ from other fixed counters in terms of the use of count and sampling modes (except for the hardware logic for event accumulation).

According to commit 6017608936c1 ("perf/x86/intel: Add Icelake support"), KVM or any perf in-kernel user needs to reprogram fixed counter3 via the kernel-defined Topdown Slots event for real fixed counter3 on the host.

Opportunistically fix a typo, s/msrs_to_saved_all/msrs_to_save_all/.

---
 arch/x86/include/asm/kvm_host.h | 2 +-
 arch/x86/kvm/vmx/pmu_intel.c    | 4 +++-
 arch/x86/kvm/x86.c              | 6 +++---
 3 files changed, 7 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index aa4eb8cfcd7e..413f2e104543 100644
@@ -513,7 +513,7 @@ struct kvm_pmc {
 #define KVM_INTEL_PMC_MAX_GENERIC	8
 #define MSR_ARCH_PERFMON_PERFCTR_MAX	(MSR_ARCH_PERFMON_PERFCTR0 + KVM_INTEL_PMC_MAX_GENERIC - 1)
 #define MSR_ARCH_PERFMON_EVENTSEL_MAX	(MSR_ARCH_PERFMON_EVENTSEL0 + KVM_INTEL_PMC_MAX_GENERIC - 1)
-#define KVM_PMC_MAX_FIXED	3
+#define KVM_PMC_MAX_FIXED	4
 #define KVM_AMD_PMC_MAX_GENERIC	6
 struct kvm_pmu {
 	unsigned nr_arch_gp_counters;
arch/x86/kvm/vmx/pmu_intel.c +3 -1
index d86a6ba8c3f9..637fd709f5f4 100644
@@ -36,6 +36,7 @@
  * the second part of hw_events is defined by the generic kernel perf:
  *
  * 8 - PERF_COUNT_HW_REF_CPU_CYCLES
+ * 9 - Kernel-defined Topdown Slots event (available on fixed counter 3)
  */
 static struct kvm_pmu_hw_event intel_arch_events[] = {
 	[0] = { 0x3c, 0x00 },
@@ -48,10 +49,11 @@ static struct kvm_pmu_hw_event intel_arch_events[] = {
 	[7] = { 0xa4, 0x01 },
 	/* The above index must match CPUID 0x0A.EBX bit vector */
 	[8] = { 0x00, 0x03 },
+	[9] = { 0x00, 0x04 },
 };
 
 /* mapping between fixed pmc index and intel_arch_events array */
-static int fixed_pmc_events[] = {1, 0, 8};
+static int fixed_pmc_events[] = {1, 0, 8, 9};
 
 static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
 {
arch/x86/kvm/x86.c +3 -3
index 312aea1854ae..0b61cb58c877 100644
@@ -1435,7 +1435,7 @@ static const u32 msrs_to_save_all[] = {
 	MSR_IA32_UMWAIT_CONTROL,
 
 	MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
-	MSR_ARCH_PERFMON_FIXED_CTR0 + 2,
+	MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
 	MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
 	MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
 	MSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG,
@@ -7001,8 +7001,8 @@ static void kvm_init_msr_list(void)
 	u32 dummy[2];
 	unsigned i;
 
-	BUILD_BUG_ON_MSG(KVM_PMC_MAX_FIXED != 3,
-			 "Please update the fixed PMCs in msrs_to_saved_all[]");
+	BUILD_BUG_ON_MSG(KVM_PMC_MAX_FIXED != 4,
+			 "Please update the fixed PMCs in msrs_to_save_all[]");
 
 	num_msrs_to_save = 0;
 	num_emulated_msrs = 0;
Signature · 1 line
2.38.2
author
#10 lore raw

RFC PATCH 6/8 KVM: x86/pmu: properly use INTEL_PMC_FIXED_RDPMC_BASE macro

In reply to Like Xu

From: Like Xu <[email protected]>

Use macro INTEL_PMC_FIXED_RDPMC_BASE in the rdpmc context to improve readability. No functional change intended.

---
 arch/x86/kvm/vmx/pmu_intel.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +2 -2
index 637fd709f5f4..b69d337d51d9 100644
@@ -136,7 +136,7 @@ static bool intel_pmc_is_enabled(struct kvm_pmc *pmc)
 static bool intel_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
-	bool fixed = idx & (1u << 30);
+	bool fixed = idx & INTEL_PMC_FIXED_RDPMC_BASE;
 
 	idx &= ~(3u << 30);
 
@@ -148,7 +148,7 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
 					    unsigned int idx, u64 *mask)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
-	bool fixed = idx & (1u << 30);
+	bool fixed = idx & INTEL_PMC_FIXED_RDPMC_BASE;
 	struct kvm_pmc *counters;
 	unsigned int num_counters;
 
Signature · 1 line
2.38.2
author
#11 lore raw

RFC PATCH 7/8 KVM: x86/pmu: Use flex *event arrays to implement grouped events

In reply to Like Xu

From: Like Xu <[email protected]>

The current vPMU would expect that a pmc tends to represent a hardware event and use pmc->counter to track the event value.

But from the perspective of the counter designer,a hardware counter can choose to sacrifice the maximum bit width of the counter and instead reflect the values of multiple hardware events on different fields of the same counter.

For example, a 32-bit counter can be divided into four 8-bit wide hardware counters, and these hardware events are kept incrementing independently in their own bitfields. Software can read the counter values once to get the latest values of multiple hardware events, thus saving the number of software accesses to the hardware interface and in virtualization, particularly reducing the number of vm-exits.

The most natural way to emulate this all-in-one hardware counter in KVM is to use a 1-to-N mapping relationship, i.e., a pmc can be associated with multiple perf_events, and these events are created in a group manner, which are scheduled in the host perf as a group to obtain hardware resources and present them to the guest.

In implementation, when the guest accesses this all-in-one counter, its pmc->max_nr_events changes according to the hardware definition, triggering the kvm's group event creation path, which is centrally created and then enabled in order, which eliminates the code differences of separate enablement. The grouped events are also released as a group.

Which hardware events correspond to each pmc and how to divide the available bitfields is predefined by the hardware vendor, and KVM does not freely combine them. A many-to-one pmc is no different from a one-to-one pmc in many cases. The first pmc->perf_event is always the source of the pmc->counter value, which implies that most functions that manipulate the state of pmc->perf_event do not need to be modified.

Note, the specific rules for splicing multiple perf_event->count values into a single value are defined by the hardware event, which does not and should not appear in this generic change.

---
 arch/x86/include/asm/kvm_host.h |  6 ++++-
 arch/x86/kvm/pmu.c              | 44 ++++++++++++++++++++++++++-------
 arch/x86/kvm/pmu.h              | 15 ++++++++---
 arch/x86/kvm/svm/pmu.c          |  1 +
 arch/x86/kvm/vmx/pmu_intel.c    |  2 ++
 5 files changed, 54 insertions(+), 14 deletions(-)
arch/x86/include/asm/kvm_host.h +5 -1
index 413f2e104543..73740aa891d3 100644
@@ -494,12 +494,12 @@ enum pmc_type {
 struct kvm_pmc {
 	enum pmc_type type;
 	u8 idx;
+	u8 max_nr_events;
 	bool is_paused;
 	bool intr;
 	u64 counter;
 	u64 prev_counter;
 	u64 eventsel;
-	struct perf_event *perf_event;
 	struct kvm_vcpu *vcpu;
 	/*
 	 * only for creating or reusing perf_event,
@@ -507,6 +507,10 @@ struct kvm_pmc {
 	 * ctrl value for fixed counters.
 	 */
 	u64 current_config;
+	union {
+		struct perf_event *perf_event;
+		DECLARE_FLEX_ARRAY(struct perf_event *, perf_events);
+	};
 };
 
 /* More counters may conflict with other existing Architectural MSRs */
arch/x86/kvm/pmu.c +35 -9

72 lines not on this page · raw message

arch/x86/kvm/pmu.h +11 -4
index b9e29a199ab8..e4b738b7c208 100644
@@ -64,12 +64,19 @@ static inline u64 pmc_read_counter(struct kvm_pmc *pmc)
 
 static inline void pmc_release_perf_event(struct kvm_pmc *pmc)
 {
-	if (pmc->perf_event) {
-		perf_event_release_kernel(pmc->perf_event);
-		pmc->perf_event = NULL;
-		pmc->current_config = 0;
+	unsigned int i;
+
+	for (i = 0; i < pmc->max_nr_events; i++) {
+		if (!pmc->perf_events[i])
+			continue;
+
+		perf_event_release_kernel(pmc->perf_events[i]);
+		pmc->perf_events[i] = NULL;
 		pmc_to_pmu(pmc)->event_count--;
 	}
+
+	pmc->current_config = 0;
+	pmc->max_nr_events = 1;
 }
 
 static inline void pmc_stop_counter(struct kvm_pmc *pmc)
arch/x86/kvm/svm/pmu.c +1 -0
index 0e313fbae055..09e81200f657 100644
@@ -200,6 +200,7 @@ static void amd_pmu_init(struct kvm_vcpu *vcpu)
 		pmu->gp_counters[i].vcpu = vcpu;
 		pmu->gp_counters[i].idx = i;
 		pmu->gp_counters[i].current_config = 0;
+		pmu->gp_counters[i].max_nr_events = 1;
 	}
 }
 
arch/x86/kvm/vmx/pmu_intel.c +2 -0
index b69d337d51d9..8e1f679d4d9d 100644
@@ -642,6 +642,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
 		pmu->gp_counters[i].vcpu = vcpu;
 		pmu->gp_counters[i].idx = i;
 		pmu->gp_counters[i].current_config = 0;
+		pmu->gp_counters[i].max_nr_events = 1;
 	}
 
 	for (i = 0; i < KVM_PMC_MAX_FIXED; i++) {
@@ -649,6 +650,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
 		pmu->fixed_counters[i].vcpu = vcpu;
 		pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED;
 		pmu->fixed_counters[i].current_config = 0;
+		pmu->fixed_counters[i].max_nr_events = 1;
 	}
 
 	lbr_desc->records.nr = 0;
Signature · 1 line
2.38.2
author
#12 lore raw

RFC PATCH 8/8 KVM: x86/pmu: Add MSR_PERF_METRICS MSR emulation to enable Topdown

In reply to Like Xu

From: Like Xu <[email protected]>

The Ice Lake core PMU provides built-in support for Top-down Micro- architecture Analysis (TMA) method level 1 metrics. These metrics are always available to cross-validate performance observations, freeing general purpose counters to count other events in high counter utilization scenarios.

A new MSR called MSR_PERF_METRICS, hinted by PERF_CAPABILITIES. PERF_METRICS_AVAILABLE (bit 15), reports four (on icx) TMA Level 1 metrics directly. The lower 32 bits are divided into four 8-bit fields each of which is an integer fraction of 255. When performance metrics use type 2000H (INTEL_PMC_FIXED_RDPMC_METRICS), rdpmc could read the value of perf_metric.

Bit EN_PERF_METRICS[48] have also been added to the following MSRs:
- MSR_CORE_PERF_GLOBAL_CTRL
- MSR_CORE_PERF_GLOBAL_STATUS
- MSR_CORE_PERF_GLOBAL_OVF_CTRL

When it comes to KVM implementation, the topdown mode is only enabled when guest starts both registers, PERF_METRICS and fixed counter 3, from zero.

In topdwon mode, vPMU creates a group of zero sample period events for fixed counter3. The first pmc->event is a group leader slot event, and its event->count is used to emulate counter value, marking bit 35 when it generates an interrupt, as usual. (Note, the fixed counter3 can be used independently, for counting or sampling, but isn't compatible with topdown mode at the same time).

The four (or more) sub-events for perf_metrics are each emulated with a metric event (its event attr.config is kernel-defined, starting with INTEL_TD_METRIC_RETIRING), which increments independently and share the same bit INTEL_PERF_METRICS_IDX on overflow.

When pmu->perf_metric is read, vPMU collects all event->count from multiple metric events, but their original values have already been processed by the host perf core (defined in icl_get_metrics_event_value(), with a division and multiplication). This part of calculation logic needs to be reversed in vPMU to restore their real values and stitch them together in turn to form pmu->perf_metric.

The pmc_read_counter() has been enhanced, moved out of the header file, and wrapped with EXPORT_SYMBOL_GPL. A little trick is that the PMC corresponding to bit 35 and 48 (INTEL_PERF_METRICS_IDX) are both fixed counter3, in order to lazy release of all above grouped events.

---
 arch/x86/include/asm/kvm_host.h  |   6 ++
 arch/x86/include/asm/msr-index.h |   1 +
 arch/x86/kvm/pmu.c               | 105 ++++++++++++++++++++++++++++++-
 arch/x86/kvm/pmu.h               |  16 ++---
 arch/x86/kvm/vmx/pmu_intel.c     |  33 ++++++++++
 arch/x86/kvm/vmx/vmx.c           |   3 +
 arch/x86/kvm/x86.c               |   3 +
 7 files changed, 153 insertions(+), 14 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -0
index 73740aa891d3..4f2e2ede09b6 100644
@@ -522,6 +522,7 @@ struct kvm_pmc {
 struct kvm_pmu {
 	unsigned nr_arch_gp_counters;
 	unsigned nr_arch_fixed_counters;
+	unsigned int nr_perf_metrics;
 	unsigned available_event_types;
 	u64 fixed_ctr_ctrl;
 	u64 fixed_ctr_ctrl_mask;
@@ -564,6 +565,11 @@ struct kvm_pmu {
 	 */
 	u64 host_cross_mapped_mask;
 
+	/* Intel Topdown Performance Metrics */
+	u64 perf_metrics;
+	u64 perf_metrics_mask;
+	bool metric_event_ovf;
+
 	/*
 	 * The gate to release perf_events not marked in
 	 * pmc_in_use only once in a vcpu time slice.
arch/x86/include/asm/msr-index.h +1 -0
index 4a2af82553e4..5dde0242ff28 100644
@@ -246,6 +246,7 @@
 #define PERF_CAP_PEBS_BASELINE         BIT_ULL(14)
 #define PERF_CAP_PEBS_MASK	(PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \
 				 PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE)
+#define MSR_CAP_PERF_METRICS           BIT_ULL(15)
 
 #define MSR_IA32_RTIT_CTL		0x00000570
 #define RTIT_CTL_TRACEEN		BIT(0)
arch/x86/kvm/pmu.c +103 -2

165 lines not on this page · raw message

arch/x86/kvm/pmu.h +4 -12
index e4b738b7c208..be800a0c5366 100644
@@ -10,6 +10,8 @@
 
 #define MSR_IA32_MISC_ENABLE_PMU_RO_MASK (MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL |	\
 					  MSR_IA32_MISC_ENABLE_BTS_UNAVAIL)
+#define INTEL_PERF_METRICS_IDX GLOBAL_CTRL_EN_PERF_METRICS
+#define KVM_PERF_METRICS_NUM_MAX	8
 
 /* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */
 #define fixed_ctrl_field(ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf)
@@ -42,6 +44,8 @@ struct kvm_pmu_ops {
 };
 
 void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops);
+void pmu_read_perf_metrics(struct kvm_pmu *pmu, u64 slots);
+u64 pmc_read_counter(struct kvm_pmc *pmc);
 
 static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
 {
@@ -50,18 +54,6 @@ static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
 	return pmu->counter_bitmask[pmc->type];
 }
 
-static inline u64 pmc_read_counter(struct kvm_pmc *pmc)
-{
-	u64 counter, enabled, running;
-
-	counter = pmc->counter;
-	if (pmc->perf_event && !pmc->is_paused)
-		counter += perf_event_read_value(pmc->perf_event,
-						 &enabled, &running);
-	/* FIXME: Scaling needed? */
-	return counter & pmc_bitmask(pmc);
-}
-
 static inline void pmc_release_perf_event(struct kvm_pmc *pmc)
 {
 	unsigned int i;
arch/x86/kvm/vmx/pmu_intel.c +33 -0
index 8e1f679d4d9d..52fef388fdb0 100644
@@ -81,6 +81,8 @@ static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
 	if (pmc_idx < INTEL_PMC_IDX_FIXED) {
 		return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + pmc_idx,
 				  MSR_P6_EVNTSEL0);
+	} else if (pmc_idx == INTEL_PERF_METRICS_IDX) {
+		return get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR3);
 	} else {
 		u32 idx = pmc_idx - INTEL_PMC_IDX_FIXED;
 
@@ -160,6 +162,12 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
 		counters = pmu->gp_counters;
 		num_counters = pmu->nr_arch_gp_counters;
 	}
+
+	if (idx & INTEL_PMC_FIXED_RDPMC_METRICS) {
+		fixed = true;
+		idx = 3;
+	}
+
 	if (idx >= num_counters)
 		return NULL;
 	*mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
@@ -229,6 +237,9 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 		ret = (perf_capabilities & PERF_CAP_PEBS_BASELINE) &&
 			((perf_capabilities & PERF_CAP_PEBS_FORMAT) > 3);
 		break;
+	case MSR_PERF_METRICS:
+		ret = vcpu->arch.perf_capabilities & MSR_CAP_PERF_METRICS;
+		break;
 	default:
 		ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) ||
 			get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) ||
@@ -385,6 +396,10 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_PEBS_DATA_CFG:
 		msr_info->data = pmu->pebs_data_cfg;
 		return 0;
+	case MSR_PERF_METRICS:
+		pmc_read_counter(&pmu->fixed_counters[3]);
+		msr_info->data = pmu->perf_metrics;
+		return 0;
 	default:
 		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
 		    (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {
@@ -472,6 +487,14 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			return 0;
 		}
 		break;
+	case MSR_PERF_METRICS:
+		if (pmu->perf_metrics == data)
+			return 0;
+		if (!(data & pmu->perf_metrics_mask)) {
+			pmu->perf_metrics = data;
+			return 0;
+		}
+		break;
 	default:
 		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
 		    (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {
@@ -545,6 +568,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->fixed_ctr_ctrl_mask = ~0ull;
 	pmu->pebs_enable_mask = ~0ull;
 	pmu->pebs_data_cfg_mask = ~0ull;
+	pmu->perf_metrics_mask = ~0ull;
 
 	entry = kvm_find_cpuid_entry(vcpu, 0xa);
 	if (!entry || !vcpu->kvm->arch.enable_pmu)
@@ -584,6 +608,13 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 		pmu->fixed_ctr_ctrl_mask &= ~(0xbull << (i * 4));
 	counter_mask = ~(((1ull << pmu->nr_arch_gp_counters) - 1) |
 		(((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED));
+	if (vcpu->arch.perf_capabilities & MSR_CAP_PERF_METRICS) {
+		counter_mask &= ~(1ULL << INTEL_PERF_METRICS_IDX);
+		pmu->nr_perf_metrics = min_t(int, KVM_PERF_METRICS_NUM_MAX,
+					     kvm_pmu_cap.num_topdown_events);
+		for (i = 0; i < pmu->nr_perf_metrics; i++)
+			pmu->perf_metrics_mask &= ~(0xffull << (i * 8));
+	}
 	pmu->global_ctrl_mask = counter_mask;
 	pmu->global_ovf_ctrl_mask = pmu->global_ctrl_mask
 			& ~(MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF |
@@ -656,6 +687,8 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
 	lbr_desc->records.nr = 0;
 	lbr_desc->event = NULL;
 	lbr_desc->msr_passthrough = false;
+
+	BUILD_BUG_ON(KVM_PERF_METRICS_NUM_MAX > INTEL_TD_METRIC_NUM);
 }
 
 static void intel_pmu_reset(struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.c +3 -0
index fe5615fd8295..57312b5a3d9d 100644
@@ -7721,6 +7721,9 @@ static u64 vmx_get_perf_capabilities(void)
 			perf_cap &= ~PERF_CAP_PEBS_BASELINE;
 	}
 
+	if (kvm_pmu_cap.num_topdown_events)
+		perf_cap |= host_perf_cap & MSR_CAP_PERF_METRICS;
+
 	return perf_cap;
 }
 
arch/x86/kvm/x86.c +3 -0
index 0b61cb58c877..a50b3ad7294c 100644
@@ -1439,6 +1439,7 @@ static const u32 msrs_to_save_all[] = {
 	MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
 	MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
 	MSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG,
+	MSR_PERF_METRICS,
 
 	/* This part of MSRs should match KVM_INTEL_PMC_MAX_GENERIC. */
 	MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
@@ -3880,6 +3881,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_IA32_PEBS_ENABLE:
 	case MSR_IA32_DS_AREA:
 	case MSR_PEBS_DATA_CFG:
+	case MSR_PERF_METRICS:
 	case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
 		if (kvm_pmu_is_valid_msr(vcpu, msr))
 			return kvm_pmu_set_msr(vcpu, msr_info);
@@ -3983,6 +3985,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_IA32_PEBS_ENABLE:
 	case MSR_IA32_DS_AREA:
 	case MSR_PEBS_DATA_CFG:
+	case MSR_PERF_METRICS:
 	case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
 		if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
 			return kvm_pmu_get_msr(vcpu, msr_info);
Signature · 1 line
2.38.2