Skip to contentSkip to conversation

KVM: x86: Add AMD Guest PerfMonV2 PMU support

PATCH Superseded Like Xu 2 participants 35 messages Feb 14, 2023 – Apr 10, 2023

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Like Xu

author
#0 lore raw

v4 cover KVM: x86: Add AMD Guest PerfMonV2 PMU support

Starting with Zen4, core PMU on AMD platforms such as Genoa and Ryzen-7000 will support PerfMonV2, and it is also compatible with legacy PERFCTR_CORE behavior and msr addresses.

If you don't have access to the hardware specification, the commits d6d0c7f681fd..7685665c390d for host perf can also bring a quick overview. Its main change is the addition of three msr's equivalent to Intel V2, namely global_ctrl, global_status, global_status_clear.

It is worth noting that this feature is very attractive for reducing the overhead of PMU virtualization, since multiple msr accesses to multiple counters will be replaced by a single access to the global register, plus more accuracy gain when multiple guest counters are used.

All related testcases are passed on a Genoa box. Please feel free to run more tests, add more or share comments.

Patch 0001-0009 could be applied earlier, which may help reduce the burden on industrious reviewers.

Base: kvm-x86/pmu
Previous: [email protected]

V3 -> V4 Changelog:
- Compilability of each patch is retained; (Sean)
- Apply "if (!diff)" style and use the atomic set_bit(); (Sean)
- First optimize the helper, then expose it in pmu.h; (Sean)
- Just use scattered X86_FEATURE_PERFMON_V2; (Sean)
- Drop use of kvm_cpu_cap_has() when refresh; (Sean)
- Use tweaked kvm_pmu_cap.num_counters_gp; (Sean)
- Sanity check the number of counters enumerated by perf; (Sean)
- Apply an if-elif-else sequence for number of counters; (Sean)
- Refine commits messages to provide hidden information; (Sean)
- Apply kvm_cpu_cap_check_and_set() when enable_pmu; (Sean)
- Fix a coner case when user sets GLOBAL_STATUS reserved bits;
- For AMD GLOBAL_STATUS, writes are ignored as host does;
- For AMD GLOBAL_CTL, none #GP on writes as host does;
- For AMD GLOBAL_STATUS_CLR, none #GP on writes as host does;

Like Xu (11):
KVM: x86/pmu: Rename pmc_is_enabled() to pmc_is_globally_enabled()
KVM: x86/pmu: Rewrite reprogram_counters() to improve performance
KVM: x86/pmu: Expose reprogram_counters() in pmu.h
KVM: x86/pmu: Error when user sets the GLOBAL_STATUS reserved bits
KVM: x86/pmu: Make part of the Intel v2 PMU MSRs handling x86 generic
KVM: x86/cpuid: Use fast return for cpuid "0xa" leaf when !enable_pmu
KVM: x86/pmu: Disable vPMU if the minimum num of counters isn't met
KVM: x86/pmu: Forget PERFCTR_CORE if the min num of counters isn't met
KVM: x86/cpuid: Add X86_FEATURE_PERFMON_V2 as a scattered flag
KVM: x86/svm/pmu: Add AMD PerfMonV2 support
KVM: x86/cpuid: Add AMD CPUID ExtPerfMonAndDbg leaf 0x80000022

Sean Christopherson (1):
KVM: VMX: Refactor intel_pmu_set_msr() to align with other set_msr()
helpers

 arch/x86/include/asm/kvm-x86-pmu-ops.h |   1 -
 arch/x86/kvm/cpuid.c                   |  26 +++++-
 arch/x86/kvm/pmu.c                     |  69 +++++++++++++--
 arch/x86/kvm/pmu.h                     |  35 +++++++-
 arch/x86/kvm/reverse_cpuid.h           |   7 ++
 arch/x86/kvm/svm/pmu.c                 |  62 +++++++++-----
 arch/x86/kvm/svm/svm.c                 |  17 +++-
 arch/x86/kvm/vmx/pmu_intel.c           | 114 +++++++------------------
 arch/x86/kvm/x86.c                     |  10 +++
 9 files changed, 223 insertions(+), 118 deletions(-)
Signature · 1 line
2.39.1
author
#1 lore raw

PATCH v4 1/12 KVM: x86/pmu: Rename pmc_is_enabled() to pmc_is_globally_enabled()

From: Like Xu <[email protected]>

The name of function pmc_is_enabled() is a bit misleading. A PMC can be disabled either by PERF_CLOBAL_CTRL or by its corresponding EVTSEL. Append global semantics to its name.

---
 arch/x86/kvm/pmu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/pmu.c +3 -3
index 7b6c3ba2c8e1..af541c913acd 100644
@@ -93,7 +93,7 @@ void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops)
 #undef __KVM_X86_PMU_OP
 }
 
-static inline bool pmc_is_enabled(struct kvm_pmc *pmc)
+static inline bool pmc_is_globally_enabled(struct kvm_pmc *pmc)
 {
 	return static_call(kvm_x86_pmu_pmc_is_enabled)(pmc);
 }
@@ -409,7 +409,7 @@ static void reprogram_counter(struct kvm_pmc *pmc)
 
 	pmc_pause_counter(pmc);
 
-	if (!pmc_speculative_in_use(pmc) || !pmc_is_enabled(pmc))
+	if (!pmc_speculative_in_use(pmc) || !pmc_is_globally_enabled(pmc))
 		goto reprogram_complete;
 
 	if (!check_pmu_event_filter(pmc))
@@ -684,7 +684,7 @@ void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 perf_hw_id)
 	for_each_set_bit(i, pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX) {
 		pmc = static_call(kvm_x86_pmu_pmc_idx_to_pmc)(pmu, i);
 
-		if (!pmc || !pmc_is_enabled(pmc) || !pmc_speculative_in_use(pmc))
+		if (!pmc || !pmc_is_globally_enabled(pmc) || !pmc_speculative_in_use(pmc))
 			continue;
 
 		/* Ignore checks for edge detect, pin control, invert and CMASK bits */
Signature · 1 line
2.39.1
author
#2 lore raw

PATCH v4 2/12 KVM: VMX: Refactor intel_pmu_set_msr() to align with other set_msr() helpers

In reply to Like Xu

From: Sean Christopherson <[email protected]>

Invert the flows in intel_pmu_set_msr()'s case statements so that they follow the kernel's preferred style of:

if (<not valid>)
return <error>

<commit change>
return <success>

which is also the style used by every other set_msr() helper (except AMD's PMU variant, which doesn't use a switch statement).

Opportunstically move the "val == current" checks below the validity checks. Except for the one-off case for MSR_P6_EVNTSEL2, the reserved bit checks are extremely cheap, and the guest is unlikely to frequently write the current value, i.e. avoiding the reserved bit checks doesn't add much (any?) value.

No functional change intended.

---
 arch/x86/kvm/vmx/pmu_intel.c | 81 +++++++++++++++++++-----------------
 1 file changed, 42 insertions(+), 39 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +42 -39
index e8a3be0b9df9..6a2f8b4ed061 100644
@@ -402,44 +402,43 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 	switch (msr) {
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
-		if (pmu->fixed_ctr_ctrl == data)
-			return 0;
-		if (!(data & pmu->fixed_ctr_ctrl_mask)) {
+		if (data & pmu->fixed_ctr_ctrl_mask)
+			return 1;
+
+		if (pmu->fixed_ctr_ctrl != data)
 			reprogram_fixed_counters(pmu, data);
-			return 0;
-		}
 		break;
 	case MSR_CORE_PERF_GLOBAL_STATUS:
-		if (msr_info->host_initiated) {
-			pmu->global_status = data;
-			return 0;
-		}
-		break; /* RO MSR */
+		if (!msr_info->host_initiated)
+			return 1; /* RO MSR */
+
+		pmu->global_status = data;
+		break;
 	case MSR_CORE_PERF_GLOBAL_CTRL:
-		if (pmu->global_ctrl == data)
-			return 0;
-		if (kvm_valid_perf_global_ctrl(pmu, data)) {
+		if (!kvm_valid_perf_global_ctrl(pmu, data))
+			return 1;
+
+		if (pmu->global_ctrl != data) {
 			diff = pmu->global_ctrl ^ data;
 			pmu->global_ctrl = data;
 			reprogram_counters(pmu, diff);
-			return 0;
 		}
 		break;
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
-		if (!(data & pmu->global_ovf_ctrl_mask)) {
-			if (!msr_info->host_initiated)
-				pmu->global_status &= ~data;
-			return 0;
-		}
+		if (data & pmu->global_ovf_ctrl_mask)
+			return 1;
+
+		if (!msr_info->host_initiated)
+			pmu->global_status &= ~data;
 		break;
 	case MSR_IA32_PEBS_ENABLE:
-		if (pmu->pebs_enable == data)
-			return 0;
-		if (!(data & pmu->pebs_enable_mask)) {
+		if (data & pmu->pebs_enable_mask)
+			return 1;
+
+		if (pmu->pebs_enable != data) {
 			diff = pmu->pebs_enable ^ data;
 			pmu->pebs_enable = data;
 			reprogram_counters(pmu, diff);
-			return 0;
 		}
 		break;
 	case MSR_IA32_DS_AREA:
@@ -447,15 +446,14 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			return 1;
 		if (is_noncanonical_address(data, vcpu))
 			return 1;
+
 		pmu->ds_area = data;
-		return 0;
+		break;
 	case MSR_PEBS_DATA_CFG:
-		if (pmu->pebs_data_cfg == data)
-			return 0;
-		if (!(data & pmu->pebs_data_cfg_mask)) {
-			pmu->pebs_data_cfg = data;
-			return 0;
-		}
+		if (data & pmu->pebs_data_cfg_mask)
+			return 1;
+
+		pmu->pebs_data_cfg = data;
 		break;
 	default:
 		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
@@ -463,33 +461,38 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			if ((msr & MSR_PMC_FULL_WIDTH_BIT) &&
 			    (data & ~pmu->counter_bitmask[KVM_PMC_GP]))
 				return 1;
+
 			if (!msr_info->host_initiated &&
 			    !(msr & MSR_PMC_FULL_WIDTH_BIT))
 				data = (s64)(s32)data;
 			pmc->counter += data - pmc_read_counter(pmc);
 			pmc_update_sample_period(pmc);
-			return 0;
+			break;
 		} else if ((pmc = get_fixed_pmc(pmu, msr))) {
 			pmc->counter += data - pmc_read_counter(pmc);
 			pmc_update_sample_period(pmc);
-			return 0;
+			break;
 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
-			if (data == pmc->eventsel)
-				return 0;
 			reserved_bits = pmu->reserved_bits;
 			if ((pmc->idx == 2) &&
 			    (pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED))
 				reserved_bits ^= HSW_IN_TX_CHECKPOINTED;
-			if (!(data & reserved_bits)) {
+			if (data & reserved_bits)
+				return 1;
+
+			if (data != pmc->eventsel) {
 				pmc->eventsel = data;
 				kvm_pmu_request_counter_reprogam(pmc);
-				return 0;
 			}
-		} else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, false))
-			return 0;
+			break;
+		} else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, false)) {
+			break;
+		}
+		/* Not a known PMU MSR. */
+		return 1;
 	}
 
-	return 1;
+	return 0;
 }
 
 static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu)
Signature · 1 line
2.39.1
#3 lore raw

Re: [PATCH v4 02/12] KVM: VMX: Refactor intel_pmu_set_msr() to align with other set_msr() helpers

arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)

Quoted 10 lines · Like Xu

From: Sean Christopherson <[email protected]>

---
 arch/x86/kvm/vmx/pmu_intel.c | 81 +++++++++++++++++++-----------------
 1 file changed, 42 insertions(+), 39 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index e8a3be0b9df9..6a2f8b4ed061 100644
@@ -402,44 +402,43 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)

Gah, I forgot to post a patch that gives intel_pmu_get_msr() the same treatment. I'll replace this patch with the combined version below when applying, or will post it separately if a v5 is needed.

From: Sean Christopherson <[email protected]>
Date: Thu, 26 Jan 2023 17:08:03 -0800
Subject: [PATCH] KVM: VMX: Refactor intel_pmu_{g,}set_msr() to align with
other helpers

Invert the flows in intel_pmu_{g,s}et_msr()'s case statements so that they follow the kernel's preferred style of:

if (<not valid>)
return <error>

<commit change>
return <success>

which is also the style used by every other {g,s}et_msr() helper (except AMD's PMU variant, which doesn't use a switch statement).

Modify the "set" paths with costly side effects, i.e. that reprogram counters, to skip only the side effects, i.e. to perform reserved bits checks even if the value is unchanged. None of the reserved bits checks are expensive, so there's no strong justification for skipping them, and guarding only the side effect makes it slightly more obvious what is being skipped and why.

No functional change intended (assuming no reserved bit bugs).

---
 arch/x86/kvm/vmx/pmu_intel.c | 109 ++++++++++++++++++-----------------
 1 file changed, 57 insertions(+), 52 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +57 -52
index e8a3be0b9df9..432662f71207 100644
@@ -351,45 +351,47 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	switch (msr) {
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
 		msr_info->data = pmu->fixed_ctr_ctrl;
-		return 0;
+		break;
 	case MSR_CORE_PERF_GLOBAL_STATUS:
 		msr_info->data = pmu->global_status;
-		return 0;
+		break;
 	case MSR_CORE_PERF_GLOBAL_CTRL:
 		msr_info->data = pmu->global_ctrl;
-		return 0;
+		break;
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
 		msr_info->data = 0;
-		return 0;
+		break;
 	case MSR_IA32_PEBS_ENABLE:
 		msr_info->data = pmu->pebs_enable;
-		return 0;
+		break;
 	case MSR_IA32_DS_AREA:
 		msr_info->data = pmu->ds_area;
-		return 0;
+		break;
 	case MSR_PEBS_DATA_CFG:
 		msr_info->data = pmu->pebs_data_cfg;
-		return 0;
+		break;
 	default:
 		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
 		    (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {
 			u64 val = pmc_read_counter(pmc);
 			msr_info->data =
 				val & pmu->counter_bitmask[KVM_PMC_GP];
-			return 0;
+			break;
 		} else if ((pmc = get_fixed_pmc(pmu, msr))) {
 			u64 val = pmc_read_counter(pmc);
 			msr_info->data =
 				val & pmu->counter_bitmask[KVM_PMC_FIXED];
-			return 0;
+			break;
 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
 			msr_info->data = pmc->eventsel;
-			return 0;
-		} else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, true))
-			return 0;
+			break;
+		} else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, true)) {
+			break;
+		}
+		return 1;
 	}
 
-	return 1;
+	return 0;
 }
 
 static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
@@ -402,44 +404,43 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 	switch (msr) {
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
-		if (pmu->fixed_ctr_ctrl == data)
-			return 0;
-		if (!(data & pmu->fixed_ctr_ctrl_mask)) {
+		if (data & pmu->fixed_ctr_ctrl_mask)
+			return 1;
+
+		if (pmu->fixed_ctr_ctrl != data)
 			reprogram_fixed_counters(pmu, data);
-			return 0;
-		}
 		break;
 	case MSR_CORE_PERF_GLOBAL_STATUS:
-		if (msr_info->host_initiated) {
-			pmu->global_status = data;
-			return 0;
-		}
-		break; /* RO MSR */
+		if (!msr_info->host_initiated)
+			return 1; /* RO MSR */
+
+		pmu->global_status = data;
+		break;
 	case MSR_CORE_PERF_GLOBAL_CTRL:
-		if (pmu->global_ctrl == data)
-			return 0;
-		if (kvm_valid_perf_global_ctrl(pmu, data)) {
+		if (!kvm_valid_perf_global_ctrl(pmu, data))
+			return 1;
+
+		if (pmu->global_ctrl != data) {
 			diff = pmu->global_ctrl ^ data;
 			pmu->global_ctrl = data;
 			reprogram_counters(pmu, diff);
-			return 0;
 		}
 		break;
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
-		if (!(data & pmu->global_ovf_ctrl_mask)) {
-			if (!msr_info->host_initiated)
-				pmu->global_status &= ~data;
-			return 0;
-		}
+		if (data & pmu->global_ovf_ctrl_mask)
+			return 1;
+
+		if (!msr_info->host_initiated)
+			pmu->global_status &= ~data;
 		break;
 	case MSR_IA32_PEBS_ENABLE:
-		if (pmu->pebs_enable == data)
-			return 0;
-		if (!(data & pmu->pebs_enable_mask)) {
+		if (data & pmu->pebs_enable_mask)
+			return 1;
+
+		if (pmu->pebs_enable != data) {
 			diff = pmu->pebs_enable ^ data;
 			pmu->pebs_enable = data;
 			reprogram_counters(pmu, diff);
-			return 0;
 		}
 		break;
 	case MSR_IA32_DS_AREA:
@@ -447,15 +448,14 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			return 1;
 		if (is_noncanonical_address(data, vcpu))
 			return 1;
+
 		pmu->ds_area = data;
-		return 0;
+		break;
 	case MSR_PEBS_DATA_CFG:
-		if (pmu->pebs_data_cfg == data)
-			return 0;
-		if (!(data & pmu->pebs_data_cfg_mask)) {
-			pmu->pebs_data_cfg = data;
-			return 0;
-		}
+		if (data & pmu->pebs_data_cfg_mask)
+			return 1;
+
+		pmu->pebs_data_cfg = data;
 		break;
 	default:
 		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
@@ -463,33 +463,38 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			if ((msr & MSR_PMC_FULL_WIDTH_BIT) &&
 			    (data & ~pmu->counter_bitmask[KVM_PMC_GP]))
 				return 1;
+
 			if (!msr_info->host_initiated &&
 			    !(msr & MSR_PMC_FULL_WIDTH_BIT))
 				data = (s64)(s32)data;
 			pmc->counter += data - pmc_read_counter(pmc);
 			pmc_update_sample_period(pmc);
-			return 0;
+			break;
 		} else if ((pmc = get_fixed_pmc(pmu, msr))) {
 			pmc->counter += data - pmc_read_counter(pmc);
 			pmc_update_sample_period(pmc);
-			return 0;
+			break;
 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
-			if (data == pmc->eventsel)
-				return 0;
 			reserved_bits = pmu->reserved_bits;
 			if ((pmc->idx == 2) &&
 			    (pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED))
 				reserved_bits ^= HSW_IN_TX_CHECKPOINTED;
-			if (!(data & reserved_bits)) {
+			if (data & reserved_bits)
+				return 1;
+
+			if (data != pmc->eventsel) {
 				pmc->eventsel = data;
 				kvm_pmu_request_counter_reprogam(pmc);
-				return 0;
 			}
-		} else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, false))
-			return 0;
+			break;
+		} else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, false)) {
+			break;
+		}
+		/* Not a known PMU MSR. */
+		return 1;
 	}
 
-	return 1;
+	return 0;
 }
 
 static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu)

base-commit: 62ef199250cd46fb66fe98267137b7f64e0b41b4

author
#4 lore raw

Re: [PATCH v4 02/12] KVM: VMX: Refactor intel_pmu_set_msr() to align with other set_msr() helpers

Quoted 3 lines · Sean Christopherson

Gah, I forgot to post a patch that gives intel_pmu_get_msr() the same treatment. I'll replace this patch with the combined version below when applying, or will post it separately if a v5 is needed.

It's fine for me to apply this new patch first and then apply the remaining patches with only a tiny rebase effort (tests still remains healthy). More, if you have more comments on any other patches that need changing after the radio silence, please roar at me on this version.

Quoted 5 lines

From: Sean Christopherson<[email protected]>
Date: Thu, 26 Jan 2023 17:08:03 -0800
Subject: [PATCH] KVM: VMX: Refactor intel_pmu_{g,}set_msr() to align with
other helpers

author
#5 lore raw

Re: [PATCH v4 02/12] KVM: VMX: Refactor intel_pmu_set_msr() to align with other set_msr() helpers

Hi Sean,

the kvm-x86/pmu branch is basically unchanged up to now, do you need a V5 for this feature ? Thanks.

Quoted 16 lines · Like Xu
Quoted 3 lines · Sean Christopherson

Gah, I forgot to post a patch that gives intel_pmu_get_msr() the same treatment. I'll replace this patch with the combined version below when applying, or will post it separately if a v5 is needed.

It's fine for me to apply this new patch first and then apply the remaining patches with only a tiny rebase effort (tests still remains healthy). More, if you have more comments on any other patches that need changing after the radio silence, please roar at me on this version.

Quoted 5 lines

From: Sean Christopherson<[email protected]>
Date: Thu, 26 Jan 2023 17:08:03 -0800
Subject: [PATCH] KVM: VMX: Refactor intel_pmu_{g,}set_msr() to align with other helpers

#6 lore raw

Re: [PATCH v4 02/12] KVM: VMX: Refactor intel_pmu_set_msr() to align with other set_msr() helpers

Quoted 4 lines · Like Xu

Hi Sean,

the kvm-x86/pmu branch is basically unchanged up to now, do you need a V5 for this feature ? Thanks.

Unknown at this time, I've been fighting fires and am behind where I wanted to be in terms of review for 6.4. I'm partially batching areas this time around for a variety of reasons. Next up is selftests, and PMU is after. Hopefully selftests will go relatively quickly and I'll be able to focus on PMU sometime next week. Sorry for the delay.

author
#7 lore raw

PATCH v4 3/12 KVM: x86/pmu: Rewrite reprogram_counters() to improve performance

In reply to Like Xu

From: Like Xu <[email protected]>

A valid pmc is always tested before using pmu->reprogram_pmi. Eliminate this part of the redundancy by setting the counter's bitmask directly, and in addition, trigger KVM_REQ_PMU only once to save more cpu cycles.

---
 arch/x86/kvm/vmx/pmu_intel.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +7 -7
index 6a2f8b4ed061..069e1aae418c 100644
@@ -73,16 +73,16 @@ static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
 	}
 }
 
-static void reprogram_counters(struct kvm_pmu *pmu, u64 diff)
+static inline void reprogram_counters(struct kvm_pmu *pmu, u64 diff)
 {
 	int bit;
-	struct kvm_pmc *pmc;
 
-	for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX) {
-		pmc = intel_pmc_idx_to_pmc(pmu, bit);
-		if (pmc)
-			kvm_pmu_request_counter_reprogam(pmc);
-	}
+	if (!diff)
+		return;
+
+	for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
+		set_bit(bit, pmu->reprogram_pmi);
+	kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
 }
 
 static bool intel_hw_event_available(struct kvm_pmc *pmc)
Signature · 1 line
2.39.1
author
#8 lore raw

PATCH v4 4/12 KVM: x86/pmu: Expose reprogram_counters() in pmu.h

In reply to Like Xu

From: Like Xu <[email protected]>

The optimization stands on its own, whereas the code movement is justified only by the incoming AMD PMU v2 support.

---
 arch/x86/kvm/pmu.h           | 12 ++++++++++++
 arch/x86/kvm/vmx/pmu_intel.c | 12 ------------
 2 files changed, 12 insertions(+), 12 deletions(-)
arch/x86/kvm/pmu.h +12 -0
index 79988dafb15b..1eb50129fae7 100644
@@ -189,6 +189,18 @@ static inline void kvm_pmu_request_counter_reprogam(struct kvm_pmc *pmc)
 	kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
 }
 
+static inline void reprogram_counters(struct kvm_pmu *pmu, u64 diff)
+{
+	int bit;
+
+	if (!diff)
+		return;
+
+	for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
+		set_bit(bit, pmu->reprogram_pmi);
+	kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
+}
+
 void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);
 void kvm_pmu_handle_event(struct kvm_vcpu *vcpu);
 int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
arch/x86/kvm/vmx/pmu_intel.c +0 -12
index 069e1aae418c..904f832fc55d 100644
@@ -73,18 +73,6 @@ static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
 	}
 }
 
-static inline void reprogram_counters(struct kvm_pmu *pmu, u64 diff)
-{
-	int bit;
-
-	if (!diff)
-		return;
-
-	for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
-		set_bit(bit, pmu->reprogram_pmi);
-	kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
-}
-
 static bool intel_hw_event_available(struct kvm_pmc *pmc)
 {
 	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
Signature · 1 line
2.39.1
author
#9 lore raw

PATCH v4 5/12 KVM: x86/pmu: Error when user sets the GLOBAL_STATUS reserved bits

In reply to Like Xu

From: Like Xu <[email protected]>

If the user space sets reserved bits when restoring the MSR_CORE_ PERF_GLOBAL_STATUS register, these bits will be accidentally returned when the guest runs a read access to this register, and cannot be cleared up inside the guest, which makes the guest's PMI handler very confused.

---
 arch/x86/kvm/vmx/pmu_intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index 904f832fc55d..aaea25d2cae8 100644
@@ -397,7 +397,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			reprogram_fixed_counters(pmu, data);
 		break;
 	case MSR_CORE_PERF_GLOBAL_STATUS:
-		if (!msr_info->host_initiated)
+		if (!msr_info->host_initiated || (data & pmu->global_ovf_ctrl_mask))
 			return 1; /* RO MSR */
 
 		pmu->global_status = data;
Signature · 1 line
2.39.1
#10 lore raw

Re: [PATCH v4 05/12] KVM: x86/pmu: Error when user sets the GLOBAL_STATUS reserved bits

Quoted 6 lines · Like Xu

From: Like Xu <[email protected]>

If the user space sets reserved bits when restoring the MSR_CORE_ PERF_GLOBAL_STATUS register, these bits will be accidentally returned when the guest runs a read access to this register, and cannot be cleared up inside the guest, which makes the guest's PMI handler very confused.

The changelog needs to state what the patch actually does.

Quoted 10 lines
---
 arch/x86/kvm/vmx/pmu_intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index 904f832fc55d..aaea25d2cae8 100644

arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 400

Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -397,7 +397,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			reprogram_fixed_counters(pmu, data);
 		break;
 	case MSR_CORE_PERF_GLOBAL_STATUS:
-		if (!msr_info->host_initiated)
+		if (!msr_info->host_initiated || (data & pmu->global_ovf_ctrl_mask))

This is wrong. Bits 60:58 are reserved in IA32_PERF_GLOBAL_OVF_CTRL, but are ASCI, CTR_FREEZE, and LBR_FREEZE respectively in MSR_CORE_PERF_GLOBAL_STATUS.

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +0 -0
 			return 1; /* RO MSR */
 
 		pmu->global_status = data;
Signature · 1 line
2.39.1
author
#11 lore raw

Re: [PATCH v4 05/12] KVM: x86/pmu: Error when user sets the GLOBAL_STATUS reserved bits

Quoted 15 lines · Sean Christopherson
Quoted 6 lines · Like Xu

From: Like Xu <[email protected]>

If the user space sets reserved bits when restoring the MSR_CORE_ PERF_GLOBAL_STATUS register, these bits will be accidentally returned when the guest runs a read access to this register, and cannot be cleared up inside the guest, which makes the guest's PMI handler very confused.

The changelog needs to state what the patch actually does.

Quoted 5 lines
---
  arch/x86/kvm/vmx/pmu_intel.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 400

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index 904f832fc55d..aaea25d2cae8 100644
@@ -397,7 +397,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  			reprogram_fixed_counters(pmu, data);
  		break;
  	case MSR_CORE_PERF_GLOBAL_STATUS:
-		if (!msr_info->host_initiated)
+		if (!msr_info->host_initiated || (data & pmu->global_ovf_ctrl_mask))

This is wrong. Bits 60:58 are reserved in IA32_PERF_GLOBAL_OVF_CTRL, but are ASCI, CTR_FREEZE, and LBR_FREEZE respectively in MSR_CORE_PERF_GLOBAL_STATUS.

CTR_FREEZE and LBR_FREEZE are only required for the guest CPUID.0AH: EAX[7:0]>3. PMU support (ASCI bit) for guest SGX isn't supported either.

So for now, reusing pmu->global_ovf_ctrl_mask here is effective enough.

Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +0 -0
  			return 1; /* RO MSR */
 
  		pmu->global_status = data;
Signature · 1 line
2.39.1
#12 lore raw

Re: [PATCH v4 05/12] KVM: x86/pmu: Error when user sets the GLOBAL_STATUS reserved bits

Quoted 30 lines · Like Xu
Quoted 15 lines · Sean Christopherson
Quoted 6 lines · Like Xu

From: Like Xu <[email protected]>

If the user space sets reserved bits when restoring the MSR_CORE_ PERF_GLOBAL_STATUS register, these bits will be accidentally returned when the guest runs a read access to this register, and cannot be cleared up inside the guest, which makes the guest's PMI handler very confused.

The changelog needs to state what the patch actually does.

Quoted 5 lines
---
  arch/x86/kvm/vmx/pmu_intel.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 400

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index 904f832fc55d..aaea25d2cae8 100644
@@ -397,7 +397,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  			reprogram_fixed_counters(pmu, data);
  		break;
  	case MSR_CORE_PERF_GLOBAL_STATUS:
-		if (!msr_info->host_initiated)
+		if (!msr_info->host_initiated || (data & pmu->global_ovf_ctrl_mask))

This is wrong. Bits 60:58 are reserved in IA32_PERF_GLOBAL_OVF_CTRL, but are ASCI, CTR_FREEZE, and LBR_FREEZE respectively in MSR_CORE_PERF_GLOBAL_STATUS.

Quoted 4 lines

CTR_FREEZE and LBR_FREEZE are only required for the guest CPUID.0AH: EAX[7:0]>3. PMU support (ASCI bit) for guest SGX isn't supported either.

So for now, reusing pmu->global_ovf_ctrl_mask here is effective enough.

And "good enough for now" is exactly how we end up with bugs, especially when "good enough" relies on assumptions that aren't well documented.

author
#13 lore raw

PATCH v4 6/12 KVM: x86/pmu: Make part of the Intel v2 PMU MSRs handling x86 generic

In reply to Like Xu

From: Like Xu <[email protected]>

The AMD PerfMonV2 defines three registers similar to part of the Intel v2 PMU registers, including the GLOBAL_CTRL, GLOBAL_STATUS and GLOBAL_OVF_CTRL MSRs. For better code reuse, this specific part of the handling can be extracted to make it generic for X86 as a straight code movement.

Specifically, move the kvm_pmu_set/get_msr() hanlders of GLOBAL_STATUS, GLOBAL_CTRL, GLOBAL_OVF_CTRL defined by intel to generic pmu.c and remove the callback function .pmc_is_globally_enabled, which is very helpful to introduce the AMD PerfMonV2 code later.

The new non-prefix pmc_is_globally_enabled() works well as legacy AMD vPMU version is indexed as 1. Note that the specific *_is_valid_msr will continue to be used to avoid cross-vendor msr access.

---
 arch/x86/include/asm/kvm-x86-pmu-ops.h |  1 -
 arch/x86/kvm/pmu.c                     | 55 +++++++++++++++++++++++---
 arch/x86/kvm/pmu.h                     | 17 +++++++-
 arch/x86/kvm/svm/pmu.c                 |  9 -----
 arch/x86/kvm/vmx/pmu_intel.c           | 49 +----------------------
 5 files changed, 67 insertions(+), 64 deletions(-)
arch/x86/include/asm/kvm-x86-pmu-ops.h +0 -1
index c17e3e96fc1d..6c98f4bb4228 100644
@@ -13,7 +13,6 @@ BUILD_BUG_ON(1)
  * at the call sites.
  */
 KVM_X86_PMU_OP(hw_event_available)
-KVM_X86_PMU_OP(pmc_is_enabled)
 KVM_X86_PMU_OP(pmc_idx_to_pmc)
 KVM_X86_PMU_OP(rdpmc_ecx_to_pmc)
 KVM_X86_PMU_OP(msr_idx_to_pmc)
arch/x86/kvm/pmu.c +50 -5

72 lines not on this page · raw message

arch/x86/kvm/pmu.h +16 -1
index 1eb50129fae7..d1cc02c8da88 100644
@@ -20,7 +20,6 @@
 
 struct kvm_pmu_ops {
 	bool (*hw_event_available)(struct kvm_pmc *pmc);
-	bool (*pmc_is_enabled)(struct kvm_pmc *pmc);
 	struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu, int pmc_idx);
 	struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu,
 		unsigned int idx, u64 *mask);
@@ -201,6 +200,22 @@ static inline void reprogram_counters(struct kvm_pmu *pmu, u64 diff)
 	kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
 }
 
+/*
+ * Check if a PMC is enabled by comparing it against global_ctrl bits.
+ *
+ * If the current version of vPMU doesn't have global_ctrl MSR,
+ * all vPMCs are enabled (return TRUE).
+ */
+static inline bool pmc_is_globally_enabled(struct kvm_pmc *pmc)
+{
+	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
+
+	if (pmu->version < 2)
+		return true;
+
+	return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl);
+}
+
 void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);
 void kvm_pmu_handle_event(struct kvm_vcpu *vcpu);
 int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
arch/x86/kvm/svm/pmu.c +0 -9
index cc77a0681800..9e12142e0c4b 100644
@@ -78,14 +78,6 @@ static bool amd_hw_event_available(struct kvm_pmc *pmc)
 	return true;
 }
 
-/* check if a PMC is enabled by comparing it against global_ctrl bits. Because
- * AMD CPU doesn't have global_ctrl MSR, all PMCs are enabled (return TRUE).
- */
-static bool amd_pmc_is_enabled(struct kvm_pmc *pmc)
-{
-	return true;
-}
-
 static bool amd_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -220,7 +212,6 @@ static void amd_pmu_reset(struct kvm_vcpu *vcpu)
 
 struct kvm_pmu_ops amd_pmu_ops __initdata = {
 	.hw_event_available = amd_hw_event_available,
-	.pmc_is_enabled = amd_pmc_is_enabled,
 	.pmc_idx_to_pmc = amd_pmc_idx_to_pmc,
 	.rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc,
 	.msr_idx_to_pmc = amd_msr_idx_to_pmc,
arch/x86/kvm/vmx/pmu_intel.c +1 -48

85 lines not on this page · raw message

Signature · 1 line
2.39.1
#14 lore raw

Re: [PATCH v4 06/12] KVM: x86/pmu: Make part of the Intel v2 PMU MSRs handling x86 generic

Quoted 3 lines · Like Xu

From: Like Xu <[email protected]>

The AMD PerfMonV2 defines three registers similar to part of the Intel

Drop the "The", i.e. just "AMD PerfMonV2 defines ..."

Quoted 4 lines

v2 PMU registers, including the GLOBAL_CTRL, GLOBAL_STATUS and GLOBAL_OVF_CTRL MSRs. For better code reuse, this specific part of the handling can be extracted to make it generic for X86 as a straight code movement.

State what the patch actually does, not what it could do, or what can be done.

Quoted 1 line

Specifically, move the kvm_pmu_set/get_msr() hanlders of GLOBAL_STATUS,

s/hanlders/handlers

Quoted 1 line

GLOBAL_CTRL, GLOBAL_OVF_CTRL defined by intel to generic pmu.c and

Intel

Quoted 4 lines

remove the callback function .pmc_is_globally_enabled, which is very helpful to introduce the AMD PerfMonV2 code later.

The new non-prefix pmc_is_globally_enabled() works well as legacy AMD

What prefix? It was pmc_is_globally_enabled() before and it's pmc_is_globally_enabled() now?

#15 lore raw

Re: [PATCH v4 06/12] KVM: x86/pmu: Make part of the Intel v2 PMU MSRs handling x86 generic

In reply to Like Xu
Quoted 52 lines · Like Xu
arch/x86/kvm/pmu.c +43 -0
@@ -574,11 +569,61 @@ static void kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u32 msr)
 
 int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	u32 msr = msr_info->index;
+
+	switch (msr) {
+	case MSR_CORE_PERF_GLOBAL_STATUS:
+		msr_info->data = pmu->global_status;
+		return 0;
+	case MSR_CORE_PERF_GLOBAL_CTRL:
+		msr_info->data = pmu->global_ctrl;
+		return 0;
+	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+		msr_info->data = 0;
+		return 0;
+	default:
+		break;
+	}
+
 	return static_call(kvm_x86_pmu_get_msr)(vcpu, msr_info);
 }
 
 int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	u32 msr = msr_info->index;
+	u64 data = msr_info->data;
+	u64 diff;
+
+	switch (msr) {
+	case MSR_CORE_PERF_GLOBAL_STATUS:
+		if (!msr_info->host_initiated || (data & pmu->global_ovf_ctrl_mask))
+			return 1; /* RO MSR */
+
+		pmu->global_status = data;
+		return 0;
+	case MSR_CORE_PERF_GLOBAL_CTRL:
+		if (!kvm_valid_perf_global_ctrl(pmu, data))
+			return 1;
+
+		if (pmu->global_ctrl != data) {
+			diff = pmu->global_ctrl ^ data;
+			pmu->global_ctrl = data;
+			reprogram_counters(pmu, diff);
+		}
+		return 0;
+	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+		if (data & pmu->global_ovf_ctrl_mask)
+			return 1;
+

arch/x86/kvm/pmu.c static void kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u32 msr) · line 629

Quoted 10 lines
arch/x86/kvm/pmu.c +7 -0
+		if (!msr_info->host_initiated)
+			pmu->global_status &= ~data;
+		return 0;
+	default:
+		break;
+	}
+
 	kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
 	return static_call(kvm_x86_pmu_set_msr)(vcpu, msr_info);
 }

Please tweak these to follow the patterns for other MSR helpers (see below). I don't actually mind the style, but people get used to the pattern and make mistakes when there are unexpected deviations.

int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)

{
	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
	u32 msr = msr_info->index;

	switch (msr) {
	case MSR_CORE_PERF_GLOBAL_STATUS:
		msr_info->data = pmu->global_status;
		break;
	case MSR_CORE_PERF_GLOBAL_CTRL:
		msr_info->data = pmu->global_ctrl;
		break;
	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
		msr_info->data = 0;
		break;
	default:
		return static_call(kvm_x86_pmu_get_msr)(vcpu, msr_info);
	}

	return 0;
}

int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)

{
	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
	u32 msr = msr_info->index;
	u64 data = msr_info->data;
	u64 diff;

	switch (msr) {
	case MSR_CORE_PERF_GLOBAL_STATUS:
		if (!msr_info->host_initiated)
			return 1; /* RO MSR */

		pmu->global_status = data;
		break;
	case MSR_CORE_PERF_GLOBAL_CTRL:
		if (!kvm_valid_perf_global_ctrl(pmu, data))
			return 1;

		if (pmu->global_ctrl != data) {
			diff = pmu->global_ctrl ^ data;
			pmu->global_ctrl = data;
			reprogram_counters(pmu, diff);
		}
		break;
	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
		if (data & pmu->global_ovf_ctrl_mask)
			return 1;

		if (!msr_info->host_initiated)
			pmu->global_status &= ~data;
		break;
	default:
		kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
		return static_call(kvm_x86_pmu_set_msr)(vcpu, msr_info);
	}

	return 0;
}
author
#16 lore raw

PATCH v4 7/12 KVM: x86/cpuid: Use fast return for cpuid "0xa" leaf when !enable_pmu

In reply to Like Xu

From: Like Xu <[email protected]>

Add an explicit !enable_pmu check as relying on kvm_pmu_cap to be zeroed isn't obvious. Although when !enable_pmu, KVM will have zero-padded kvm_pmu_cap to do subsequent cpuid leaf assignments, one extra branch instruction saves a few subsequent zero-assignment instructions, speeding things up a bit.

---
 arch/x86/kvm/cpuid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
index 2a9f1e200dbc..b0bb5f9f5307 100644
@@ -944,7 +944,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 		union cpuid10_eax eax;
 		union cpuid10_edx edx;
 
-		if (!static_cpu_has(X86_FEATURE_ARCH_PERFMON)) {
+		if (!enable_pmu || !static_cpu_has(X86_FEATURE_ARCH_PERFMON)) {
 			entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
 			break;
 		}
Signature · 1 line
2.39.1
#17 lore raw

Re: [PATCH v4 07/12] KVM: x86/cpuid: Use fast return for cpuid "0xa" leaf when !enable_pmu

KVM: x86: Explicitly zero cpuid "0xa" leaf when PMU is disabled

Quoted 7 lines · Like Xu

From: Like Xu <[email protected]>

Add an explicit !enable_pmu check as relying on kvm_pmu_cap to be zeroed isn't obvious. Although when !enable_pmu, KVM will have zero-padded kvm_pmu_cap to do subsequent cpuid leaf assignments, one extra branch instruction saves a few subsequent zero-assignment instructions, speeding things up a bit.

Drop the performance blurb, as I stated multiple times, optimizing this code is a non-goal.

author
#18 lore raw

PATCH v4 8/12 KVM: x86/pmu: Disable vPMU if the minimum num of counters isn't met

In reply to Like Xu

From: Like Xu <[email protected]>

For compatibility with old software, KVM/AMD should never report less than four counters if vPMU is supported. Thus KVM should sanity check the number of counters enumerated by perf and explicitly disable vPMU support if the min isn't met. E.g. if KVM needs 4 counters and perf says there are 3, then something is wrong and enumerating 4 to the guest is only going to cause more troubles.

---
 arch/x86/kvm/pmu.h | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kvm/pmu.h +6 -0
index d1cc02c8da88..46db5404894e 100644
@@ -170,6 +170,12 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
 	if ((is_intel && !kvm_pmu_cap.version) || !kvm_pmu_cap.num_counters_gp)
 		enable_pmu = false;
 
+	/*
+	 * For AMD, disable vPMU if the minimum number of counters isn't met.
+	 */
+	if (!is_intel && kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS)
+		enable_pmu = false;
+
 	if (!enable_pmu) {
 		memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap));
 		return;
Signature · 1 line
2.39.1
#19 lore raw

Re: [PATCH v4 08/12] KVM: x86/pmu: Disable vPMU if the minimum num of counters isn't met

Quoted 4 lines · Like Xu

From: Like Xu <[email protected]>

For compatibility with old software, KVM/AMD should never report less than four counters if vPMU is supported.

Explain _why_. Anchor what "should" be done in hardware specifications and architecture.

Quoted 4 lines

Thus KVM should sanity check the number of counters enumerated by perf and explicitly disable vPMU support if the min isn't met. E.g. if KVM needs 4 counters and perf says there are 3, then something is wrong and enumerating 4 to the guest is only going to cause more troubles.

Again, state what the patch actually does, not what KVM "should do". E.g.

Disable PMU support when running on AMD and perf reports fewer than four general purpose counters. All AMD PMUs must define at least four counters due to AMD's legacy architecture hardcoding the number of counters without providing a way to enumerate the number of counters to software, e.g. from AMD's APM.

The legacy architecture defines four performance counters

Virtualizing fewer than four counters can lead to guest instability as software expects four counters to be available.

Quoted 11 lines
---
 arch/x86/kvm/pmu.h | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kvm/pmu.h +0 -0
index d1cc02c8da88..46db5404894e 100644

arch/x86/kvm/pmu.h static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) · line 175

Quoted 6 lines
arch/x86/kvm/pmu.h +3 -0
@@ -170,6 +170,12 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
 	if ((is_intel && !kvm_pmu_cap.version) || !kvm_pmu_cap.num_counters_gp)
 		enable_pmu = false;
 
+	/*
+	 * For AMD, disable vPMU if the minimum number of counters isn't met.
+	 */

Doesn't need to be a multiple line comment. This comment is also useless. It's quite clear from the code that PMU support is being disabled when there aren't enough counters, what's missing is _why_.

Quoted 9 lines
arch/x86/kvm/pmu.h +3 -0
+	if (!is_intel && kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS)
+		enable_pmu = false;
+
 	if (!enable_pmu) {
 		memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap));
 		return;
Signature · 1 line
2.39.1
#20 lore raw

Re: [PATCH v4 08/12] KVM: x86/pmu: Disable vPMU if the minimum num of counters isn't met

Quoted 23 lines · Sean Christopherson
Quoted 11 lines · Like Xu
---
 arch/x86/kvm/pmu.h | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kvm/pmu.h +0 -0
index d1cc02c8da88..46db5404894e 100644

arch/x86/kvm/pmu.h static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) · line 175

Quoted 6 lines
arch/x86/kvm/pmu.h +3 -0
@@ -170,6 +170,12 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
 	if ((is_intel && !kvm_pmu_cap.version) || !kvm_pmu_cap.num_counters_gp)
 		enable_pmu = false;
 
+	/*
+	 * For AMD, disable vPMU if the minimum number of counters isn't met.
+	 */

Doesn't need to be a multiple line comment. This comment is also useless. It's quite clear from the code that PMU support is being disabled when there aren't enough counters, what's missing is _why_.

Quoted 1 line

arch/x86/kvm/pmu.h static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops) · line 176

Quoted 1 line
arch/x86/kvm/pmu.h +1 -0
+	if (!is_intel && kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS)

Actually, rather than bleed AMD details into common code, define a const int kvm_pmu_ops, e.g.

const int MIN_NR_GP_COUNTERS

and then the above !kvm_pmu_cap.num_counters_gp can get replaced with a more generic
check. That will also give us a convenient location to document _why_ Intel
and AMD have different mins (in particular, AMD's lack of any way to enumerate less than four counters to the guest).

E.g. end up with

	if (enable_pmu) {
		perf_get_x86_pmu_capability(&kvm_pmu_cap);

		/*
		 * For Intel, only support guest architectural pmu
		 * on a host with architectural pmu.
		 */
		if ((is_intel && !kvm_pmu_cap.version) ||
		    (kvm_pmu_cap.num_counters_gp < pmu_ops->MIN_NR_GP_COUNTERS))
			enable_pmu = false;
	}

Hmm, and doesn't have to be done in this series, but we could do the same for the min/max PMU versions.

Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/pmu.h +2 -0
+		enable_pmu = false;
+
 	if (!enable_pmu) {
 		memset(&kvm_pmu_cap, 0, sizeof(kvm_pmu_cap));
 		return;
Signature · 1 line
2.39.1
author
#21 lore raw

PATCH v4 9/12 KVM: x86/pmu: Forget PERFCTR_CORE if the min num of counters isn't met

In reply to Like Xu

From: Like Xu <[email protected]>

KVM should sanity check the number of counters enumerated by perf and explicitly drop PERFCTR_CORE support if the min isn't met. E.g. if KVM needs 6 counters and perf says there are 4, then something is wrong and enumerating 6 to the guest is only going to cause more issues.

Opportunistically, the existing kvm_cpu_cap_check_and_set() makes it easier to simplify the host check before setting the PERFCTR_CORE flag.

---
 arch/x86/kvm/svm/svm.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/svm.c +8 -3
index dd21e8b1a259..f4a4691b4f4e 100644
@@ -4910,9 +4910,14 @@ static __init void svm_set_cpu_caps(void)
 	    boot_cpu_has(X86_FEATURE_AMD_SSBD))
 		kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
 
-	/* AMD PMU PERFCTR_CORE CPUID */
-	if (enable_pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
-		kvm_cpu_cap_set(X86_FEATURE_PERFCTR_CORE);
+	if (enable_pmu) {
+		if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE) {
+			kvm_pmu_cap.num_counters_gp = AMD64_NUM_COUNTERS;
+		} else {
+			/* AMD PMU PERFCTR_CORE CPUID */
+			kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
+		}
+	}
 
 	/* CPUID 0x8000001F (SME/SEV features) */
 	sev_set_cpu_caps();
Signature · 1 line
2.39.1
#22 lore raw

Re: [PATCH v4 09/12] KVM: x86/pmu: Forget PERFCTR_CORE if the min num of counters isn't met

Quoted 9 lines · Like Xu

From: Like Xu <[email protected]>

KVM should sanity check the number of counters enumerated by perf and explicitly drop PERFCTR_CORE support if the min isn't met. E.g. if KVM needs 6 counters and perf says there are 4, then something is wrong and enumerating 6 to the guest is only going to cause more issues.

Opportunistically, the existing kvm_cpu_cap_check_and_set() makes it easier to simplify the host check before setting the PERFCTR_CORE flag.

Once again, state what the patch does.

Quoted 11 lines
---
 arch/x86/kvm/svm/svm.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/svm.c +0 -0
index dd21e8b1a259..f4a4691b4f4e 100644

arch/x86/kvm/svm/svm.c static __init void svm_set_cpu_caps(void) · line 4913

Quoted 7 lines
arch/x86/kvm/svm/svm.c +1 -3
@@ -4910,9 +4910,14 @@ static __init void svm_set_cpu_caps(void)
 	    boot_cpu_has(X86_FEATURE_AMD_SSBD))
 		kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
 
-	/* AMD PMU PERFCTR_CORE CPUID */
-	if (enable_pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
-		kvm_cpu_cap_set(X86_FEATURE_PERFCTR_CORE);
+	if (enable_pmu) {

A comment would be very helpful here. Even better would be if we can convince
perf to rename AMD64_NUM_COUNTERS to AMD64_LEGACY_NUM_COUNTERS.

arch/x86/kvm/svm/svm.c static __init void svm_set_cpu_caps(void) · line 4915

Quoted 2 lines
arch/x86/kvm/svm/svm.c +2 -0
+		if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE) {
+			kvm_pmu_cap.num_counters_gp = AMD64_NUM_COUNTERS;

Hmm, this subtly relies on kvm_init_pmu_capability() running first. That _probably_ won't change, but like the arch LBRs side of things I would prefer to avoid taking unnecessarily. E.g. something like this?

/*
* Enumerate support for PERFCTR_CORE if and only if KVM has
* access to enough counters to virtualize "core" support,
* otherwise limit vPMU support to the legacy number of counters. */
if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
kvm_pmu_cap.num_counters_gp);

It's a bit paranoid, but practically speaking it doesn't cost us anything.

arch/x86/kvm/svm/svm.c static __init void svm_set_cpu_caps(void) · line 4917

Quoted 2 lines
arch/x86/kvm/svm/svm.c +2 -0
+		} else {
+			/* AMD PMU PERFCTR_CORE CPUID */

Meh, this is not a very helpful comment, no need to carry it forward. And if you drop it, then the need for curly braces goes away.

Quoted 9 lines
arch/x86/kvm/svm/svm.c +3 -0
+			kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
+		}
+	}
 
 	/* CPUID 0x8000001F (SME/SEV features) */
 	sev_set_cpu_caps();
Signature · 1 line
2.39.1
author
#23 lore raw

PATCH v4 10/12 KVM: x86/cpuid: Add X86_FEATURE_PERFMON_V2 as a scattered flag

In reply to Like Xu

From: Like Xu <[email protected]>

Considering that the core kernel may also want to know this flag, to avoid confusion this needs to be a scattered flag rather than a pure KVM flag so that KVM can redirect it to the hardware-defined bit position, which is the role of __feature_translate() and KVM_X86_FEATURE_PERFMON_V2.

---
 arch/x86/kvm/reverse_cpuid.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kvm/reverse_cpuid.h +7 -0
index 4945456fd646..333e28b0a13c 100644
@@ -15,6 +15,7 @@ enum kvm_only_cpuid_leafs {
 	CPUID_12_EAX	 = NCAPINTS,
 	CPUID_7_1_EDX,
 	CPUID_8000_0007_EDX,
+	CPUID_8000_0022_EAX,
 	NR_KVM_CPU_CAPS,
 
 	NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS,
@@ -47,6 +48,9 @@ enum kvm_only_cpuid_leafs {
 /* CPUID level 0x80000007 (EDX). */
 #define KVM_X86_FEATURE_CONSTANT_TSC	KVM_X86_FEATURE(CPUID_8000_0007_EDX, 8)
 
+/* CPUID level 0x80000022 (EAX) */
+#define KVM_X86_FEATURE_PERFMON_V2	KVM_X86_FEATURE(CPUID_8000_0022_EAX, 0)
+
 struct cpuid_reg {
 	u32 function;
 	u32 index;
@@ -73,6 +77,7 @@ static const struct cpuid_reg reverse_cpuid[] = {
 	[CPUID_8000_001F_EAX] = {0x8000001f, 0, CPUID_EAX},
 	[CPUID_7_1_EDX]       = {         7, 1, CPUID_EDX},
 	[CPUID_8000_0007_EDX] = {0x80000007, 0, CPUID_EDX},
+	[CPUID_8000_0022_EAX] = {0x80000022, 0, CPUID_EAX},
 };
 
 /*
@@ -107,6 +112,8 @@ static __always_inline u32 __feature_translate(int x86_feature)
 		return KVM_X86_FEATURE_SGX_EDECCSSA;
 	else if (x86_feature == X86_FEATURE_CONSTANT_TSC)
 		return KVM_X86_FEATURE_CONSTANT_TSC;
+	else if (x86_feature == X86_FEATURE_PERFMON_V2)
+		return KVM_X86_FEATURE_PERFMON_V2;
 
 	return x86_feature;
 }
Signature · 1 line
2.39.1
#24 lore raw

Re: [PATCH v4 10/12] KVM: x86/cpuid: Add X86_FEATURE_PERFMON_V2 as a scattered flag

This shortlog is flat out wrong. This patch does not add X86_FEATURE_PERFMON_V2, it adds a KVM-only leaf to redirect X86_FEATURE_PERFMON_V2 to its architectural location when KVM queries the leaf, e.g. via guest_cpuid_has().

Quoted 3 lines · Like Xu

From: Like Xu <[email protected]>

Considering that the core kernel may also want to know this flag, to avoid

Please write changelogs so that they do not depend on the shortlog for context. I know the tip maintainers in particular like to make the changelog a continuation of the shortlog, but I find it really annoying, especially when reviewing in mutt, e.g. as I'm typing this, I don't even have the shortlog visible.

In the vast majority of cases, being more explicit rarely adds more than a few chars, e.g. you'll save more by reducing the fluff in this changelog.

Quoted 3 lines

confusion this needs to be a scattered flag rather than a pure KVM flag so that KVM can redirect it to the hardware-defined bit position, which is the role of __feature_translate() and KVM_X86_FEATURE_PERFMON_V2.

Like the shortlog, the changelog is best misleading, X86_FEATURE_PERFMON_V2 is already a scattered flag.

Define a KVM-only leaf for AMD's PerfMonV2 feature flag to redirect the kernel's scattered version to its architectural location, e.g. so that KVM can query guest support via guest_cpuid_has().

author
#25 lore raw

PATCH v4 11/12 KVM: x86/svm/pmu: Add AMD PerfMonV2 support

In reply to Like Xu

From: Like Xu <[email protected]>

If AMD Performance Monitoring Version 2 (PerfMonV2) is detected by the guest, it can use a new scheme to manage the Core PMCs using the new global control and status registers.

In addition to benefiting from the PerfMonV2 functionality in the same way as the host (higher precision), the guest also can reduce the number of vm-exits by lowering the total number of MSRs accesses.

In terms of implementation details, amd_is_valid_msr() is resurrected since three newly added MSRs could not be mapped to one vPMC. The possibility of emulating PerfMonV2 on the mainframe has also been eliminated for reasons of precision.

---
 arch/x86/kvm/pmu.c     | 14 +++++++++--
 arch/x86/kvm/svm/pmu.c | 53 ++++++++++++++++++++++++++++++++++--------
 arch/x86/kvm/x86.c     | 10 ++++++++
 3 files changed, 65 insertions(+), 12 deletions(-)
arch/x86/kvm/pmu.c +12 -2
index 5a3428d212dd..d5be8320fd54 100644
@@ -574,12 +574,15 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 	switch (msr) {
 	case MSR_CORE_PERF_GLOBAL_STATUS:
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
 		msr_info->data = pmu->global_status;
 		return 0;
 	case MSR_CORE_PERF_GLOBAL_CTRL:
+	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
 		msr_info->data = pmu->global_ctrl;
 		return 0;
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
 		msr_info->data = 0;
 		return 0;
 	default:
@@ -600,13 +603,19 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_CORE_PERF_GLOBAL_STATUS:
 		if (!msr_info->host_initiated || (data & pmu->global_ovf_ctrl_mask))
 			return 1; /* RO MSR */
+		fallthrough;
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+		if (!msr_info->host_initiated)
+			return 0; /* Writes are ignored */
 
 		pmu->global_status = data;
 		return 0;
 	case MSR_CORE_PERF_GLOBAL_CTRL:
 		if (!kvm_valid_perf_global_ctrl(pmu, data))
 			return 1;
-
+		fallthrough;
+	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
+		data &= ~pmu->global_ctrl_mask;
 		if (pmu->global_ctrl != data) {
 			diff = pmu->global_ctrl ^ data;
 			pmu->global_ctrl = data;
@@ -616,7 +625,8 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
 		if (data & pmu->global_ovf_ctrl_mask)
 			return 1;
-
+		fallthrough;
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
 		if (!msr_info->host_initiated)
 			pmu->global_status &= ~data;
 		return 0;
arch/x86/kvm/svm/pmu.c +43 -10

87 lines not on this page · raw message

arch/x86/kvm/x86.c +10 -0
index 64c567a1b32b..74e0c53b6a00 100644
@@ -1464,6 +1464,10 @@ static const u32 msrs_to_save_pmu[] = {
 	MSR_F15H_PERF_CTL3, MSR_F15H_PERF_CTL4, MSR_F15H_PERF_CTL5,
 	MSR_F15H_PERF_CTR0, MSR_F15H_PERF_CTR1, MSR_F15H_PERF_CTR2,
 	MSR_F15H_PERF_CTR3, MSR_F15H_PERF_CTR4, MSR_F15H_PERF_CTR5,
+
+	MSR_AMD64_PERF_CNTR_GLOBAL_CTL,
+	MSR_AMD64_PERF_CNTR_GLOBAL_STATUS,
+	MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR,
 };
 
 static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_base) +
@@ -7067,6 +7071,12 @@ static void kvm_probe_msr_to_save(u32 msr_index)
 		    kvm_pmu_cap.num_counters_fixed)
 			return;
 		break;
+	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
+		if (!kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2))
+			return;
+		break;
 	case MSR_IA32_XFD:
 	case MSR_IA32_XFD_ERR:
 		if (!kvm_cpu_cap_has(X86_FEATURE_XFD))
Signature · 1 line
2.39.1
#26 lore raw

Re: [PATCH v4 11/12] KVM: x86/svm/pmu: Add AMD PerfMonV2 support

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 609

Quoted 3 lines · Like Xu
arch/x86/kvm/pmu.c +3 -0
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+		if (!msr_info->host_initiated)
+			return 0; /* Writes are ignored */

Where is the "writes ignored" behavior documented? I can't find anything in the APM that defines write behavior.

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 616

Quoted 8 lines
arch/x86/kvm/pmu.c +1 -1
 		pmu->global_status = data;
 		return 0;
 	case MSR_CORE_PERF_GLOBAL_CTRL:
 		if (!kvm_valid_perf_global_ctrl(pmu, data))
 			return 1;
-
+		fallthrough;

This _definitely_ needs a comment. Hmm, and I would prefer to reverse these, i.e.

	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
		data &= ~pmu->global_ctrl_mask;
		fallthrough;
	case MSR_CORE_PERF_GLOBAL_CTRL:
		if (!kvm_valid_perf_global_ctrl(pmu, data))
			return 1;

It's a bit arbitrary, but either Intel or AMD is going to end up with extra code, and IMO skipping a validity check is more alarming than skipping clearing of reserved bits, i.e. will look like a bug to future readers.

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 628

Quoted 11 lines
arch/x86/kvm/pmu.c +3 -1
+	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
+		data &= ~pmu->global_ctrl_mask;
 		if (pmu->global_ctrl != data) {
 			diff = pmu->global_ctrl ^ data;
 			pmu->global_ctrl = data;
@@ -616,7 +625,8 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
 		if (data & pmu->global_ovf_ctrl_mask)
 			return 1;
-
+		fallthrough;

Here too. Argh, the APM doesn't actually define what happens on reserved bits, it just says "WO". I vote to be conservative and ignore writes to reserved bits. And then we can have one comment for the whole block, e.g.

	/*
	 * Note, AMD ignores writes to read-only PMU MSRs/bits, whereas Intel
	 * generates #GP on attempts to write reserved bits or RO MSRs.
	 */
	switch (msr) {
	case MSR_CORE_PERF_GLOBAL_STATUS:
		if (!msr_info->host_initiated)
			return 1; /* RO MSR */
		fallthrough;
	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
		if (!msr_info->host_initiated)
			break;

		pmu->global_status = data;
		break;
	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
		data &= ~pmu->global_ctrl_mask;
		fallthrough;
	case MSR_CORE_PERF_GLOBAL_CTRL:
		if (!kvm_valid_perf_global_ctrl(pmu, data))
			return 1;

		if (pmu->global_ctrl != data) {
			diff = pmu->global_ctrl ^ data;
			pmu->global_ctrl = data;
			reprogram_counters(pmu, diff);
		}
		break;
	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
		fallthrough;
	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
		if (data & pmu->global_ovf_ctrl_mask)
			return 1;

		if (!msr_info->host_initiated)
			pmu->global_status &= ~data;
		break;
	default:
		kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
		return static_call(kvm_x86_pmu_set_msr)(vcpu, msr_info);
	}

	return 0;	

arch/x86/kvm/svm/pmu.c static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 190

Quoted 12 lines
arch/x86/kvm/svm/pmu.c +6 -1
@@ -164,20 +181,34 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	struct kvm_cpuid_entry2 *entry;
+	union cpuid_0x80000022_ebx ebx;
 
-	if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE))
+	pmu->version = 1;
+	if (guest_cpuid_has(vcpu, X86_FEATURE_PERFMON_V2)) {
+		pmu->version = 2;
+		entry = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0);

No need for the intermediate "entry".

arch/x86/kvm/svm/pmu.c static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 191

Quoted 1 line
arch/x86/kvm/svm/pmu.c +1 -0
+		ebx.full = entry->ebx;

Oof, at first glance this looks like a potential null-pointer deref bug. I believe we can do

		/*
		 * Note, PERFMON_V2 is also in 0x80000022.0x0, i.e. the guest
		 * CPUID entry is guaranteed to be non-NULL.
		 */
		BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
			     x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index != 0x80000022);
		ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;

arch/x86/kvm/svm/pmu.c static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 196

Quoted 5 lines
arch/x86/kvm/svm/pmu.c +4 -0
+		pmu->nr_arch_gp_counters = min_t(unsigned int,
+						 ebx.split.num_core_pmc,
+						 kvm_pmu_cap.num_counters_gp);
+	} else if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
 		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;

This needs to be sanitized, no? E.g. if KVM only has access to 4 counters, but userspace sets X86_FEATURE_PERFCTR_CORE anyways. Hrm, unless I'm missing something, that's a pre-existing bug.

If I'm right, can you add a patch to cap nr_arch_gp_counters at kvm_pmu_cap.num_counters_gp in the common flow, i.e. after this if-else block? Then there is no change needed in this patch, e.g. we'll naturally end up with:

	union cpuid_0x80000022_ebx ebx;

	pmu->version = 1;
	if (guest_cpuid_has(vcpu, X86_FEATURE_PERFMON_V2)) {
		pmu->version = 2;
		/*                                                              
                 * Note, PERFMON_V2 is also in 0x80000022.0x0, i.e. the guest   
                 * CPUID entry is guaranteed to be non-NULL.                    
                 */                                                             
                BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
                             x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);
		ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;
		pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
	} else if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
	} else {
		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
	}

	pmu->nr_arch_gp_counters = min_t(unsigned int,
					 pmu->nr_arch_gp_counters,
				       	 kvm_pmu_cap.num_counters_gp);
author
#27 lore raw

Re: [PATCH v4 11/12] KVM: x86/svm/pmu: Add AMD PerfMonV2 support

Quoted 7 lines · Sean Christopherson

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 609

Quoted 3 lines · Like Xu
arch/x86/kvm/pmu.c +3 -0
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+		if (!msr_info->host_initiated)
+			return 0; /* Writes are ignored */

Where is the "writes ignored" behavior documented? I can't find anything in the APM that defines write behavior.

KVM would follow the real hardware behavior once specifications stay silent on details or secret.

Quoted 74 lines

Review

Quoted 9 lines
  
  		pmu->global_status = data;
  		return 0;
  	case MSR_CORE_PERF_GLOBAL_CTRL:
  		if (!kvm_valid_perf_global_ctrl(pmu, data))
  			return 1;
(quoted diff) +1 -1
-
+		fallthrough;

This _definitely_ needs a comment. Hmm, and I would prefer to reverse these, i.e.

	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
		data &= ~pmu->global_ctrl_mask;
		fallthrough;
	case MSR_CORE_PERF_GLOBAL_CTRL:
		if (!kvm_valid_perf_global_ctrl(pmu, data))
			return 1;

It's a bit arbitrary, but either Intel or AMD is going to end up with extra code, and IMO skipping a validity check is more alarming than skipping clearing of reserved bits, i.e. will look like a bug to future readers.

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 628

Quoted 11 lines
arch/x86/kvm/pmu.c +3 -1
+	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
+		data &= ~pmu->global_ctrl_mask;
  		if (pmu->global_ctrl != data) {
  			diff = pmu->global_ctrl ^ data;
  			pmu->global_ctrl = data;
@@ -616,7 +625,8 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
  		if (data & pmu->global_ovf_ctrl_mask)
  			return 1;
-
+		fallthrough;

Here too. Argh, the APM doesn't actually define what happens on reserved bits, it just says "WO". I vote to be conservative and ignore writes to reserved bits. And then we can have one comment for the whole block, e.g.

	/*
	 * Note, AMD ignores writes to read-only PMU MSRs/bits, whereas Intel
	 * generates #GP on attempts to write reserved bits or RO MSRs.
	 */
	switch (msr) {
	case MSR_CORE_PERF_GLOBAL_STATUS:
		if (!msr_info->host_initiated)
			return 1; /* RO MSR */
		fallthrough;
	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
		if (!msr_info->host_initiated)
			break;

		pmu->global_status = data;
		break;
	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
		data &= ~pmu->global_ctrl_mask;
		fallthrough;
	case MSR_CORE_PERF_GLOBAL_CTRL:
		if (!kvm_valid_perf_global_ctrl(pmu, data))
			return 1;

		if (pmu->global_ctrl != data) {
			diff = pmu->global_ctrl ^ data;
			pmu->global_ctrl = data;
			reprogram_counters(pmu, diff);
		}
		break;
	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
		fallthrough;
	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
		if (data & pmu->global_ovf_ctrl_mask)
			return 1;

		if (!msr_info->host_initiated)
Quoted 8 lines
			pmu->global_status &= ~data;
		break;
	default:
		kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
		return static_call(kvm_x86_pmu_set_msr)(vcpu, msr_info);
	}

	return 0;	

AMD doesn't generates #GP on attempts to write PMU RO MSRs and reserved bits.

How about this:

	/*
	 * Note, AMD ignores writes to reserved bits and read-only PMU MSRs,
	 * whereas Intel generates #GP on attempts to write reserved/RO MSRs.
	 */
	switch (msr) {
	case MSR_CORE_PERF_GLOBAL_STATUS:
		if (!msr_info->host_initiated || (data & pmu->global_ovf_ctrl_mask))
			return 1; /* RO MSR */
		fallthrough;
	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
		if (!msr_info->host_initiated)
			break;

		pmu->global_status = data;
		break;
	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
		data &= ~pmu->global_ctrl_mask;
		fallthrough;
	case MSR_CORE_PERF_GLOBAL_CTRL:
		if (!kvm_valid_perf_global_ctrl(pmu, data))
			return 1;

		if (pmu->global_ctrl != data) {
			diff = pmu->global_ctrl ^ data;
			pmu->global_ctrl = data;
			reprogram_counters(pmu, diff);
		}
		break;
	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
		if (data & pmu->global_ovf_ctrl_mask)
			return 1;
		fallthrough;
	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
		if (!msr_info->host_initiated)
			pmu->global_status &= ~data;
		break;
	default:
		kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
		return static_call(kvm_x86_pmu_set_msr)(vcpu, msr_info);
	}

	return 0;
Quoted 20 lines

arch/x86/kvm/svm/pmu.c static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 190

Quoted 13 lines
arch/x86/kvm/svm/pmu.c +6 -1
@@ -164,20 +181,34 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
  {
  	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	struct kvm_cpuid_entry2 *entry;
+	union cpuid_0x80000022_ebx ebx;
 
-	if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE))
+	pmu->version = 1;
+	if (guest_cpuid_has(vcpu, X86_FEATURE_PERFMON_V2)) {
+		pmu->version = 2;
+		entry = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0);

No need for the intermediate "entry".

arch/x86/kvm/svm/pmu.c static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 191

Quoted 1 line
arch/x86/kvm/svm/pmu.c +1 -0
+		ebx.full = entry->ebx;

Oof, at first glance this looks like a potential null-pointer deref bug. I believe we can do

Quoted 6 lines
		/*
		 * Note, PERFMON_V2 is also in 0x80000022.0x0, i.e. the guest
		 * CPUID entry is guaranteed to be non-NULL.
		 */
		BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
			     x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index != 0x80000022);

x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);

Quoted 11 lines
		ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;

arch/x86/kvm/svm/pmu.c static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 196

Quoted 5 lines
arch/x86/kvm/svm/pmu.c +4 -0
+		pmu->nr_arch_gp_counters = min_t(unsigned int,
+						 ebx.split.num_core_pmc,
+						 kvm_pmu_cap.num_counters_gp);
+	} else if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
  		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;

This needs to be sanitized, no? E.g. if KVM only has access to 4 counters, but userspace sets X86_FEATURE_PERFCTR_CORE anyways. Hrm, unless I'm missing something, that's a pre-existing bug.

Now your point is that if a user space more capbility than KVM can support, KVM should constrain it. Your previous preference was that the user space can set capbilities that evene if KVM doesn't support as long as it doesn't break KVM and host and the guest will eat its own.

Quoted 27 lines

If I'm right, can you add a patch to cap nr_arch_gp_counters at kvm_pmu_cap.num_counters_gp in the common flow, i.e. after this if-else block? Then there is no change needed in this patch, e.g. we'll naturally end up with:

	union cpuid_0x80000022_ebx ebx;

	pmu->version = 1;
	if (guest_cpuid_has(vcpu, X86_FEATURE_PERFMON_V2)) {
		pmu->version = 2;
		/*
                  * Note, PERFMON_V2 is also in 0x80000022.0x0, i.e. the guest
                  * CPUID entry is guaranteed to be non-NULL.
                  */
                 BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
                              x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);
		ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;
		pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
	} else if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
	} else {
		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
	}

	pmu->nr_arch_gp_counters = min_t(unsigned int,
					 pmu->nr_arch_gp_counters,
				       	 kvm_pmu_cap.num_counters_gp);
#28 lore raw

Re: [PATCH v4 11/12] KVM: x86/svm/pmu: Add AMD PerfMonV2 support

Quoted 11 lines · Like Xu
Quoted 7 lines · Sean Christopherson

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 609

Quoted 3 lines · Like Xu
arch/x86/kvm/pmu.c +3 -0
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+		if (!msr_info->host_initiated)
+			return 0; /* Writes are ignored */

Where is the "writes ignored" behavior documented? I can't find anything in the APM that defines write behavior.

KVM would follow the real hardware behavior once specifications stay silent on details or secret.

So is that a "this isn't actually documented anywhere" answer? It's not your responsibility to get AMD to document their CPUs, but I want to clearly document when KVM's behavior is based solely off of observed hardware behavior, versus an actual specification.

Quoted 6 lines

How about this:

	/*
	 * Note, AMD ignores writes to reserved bits and read-only PMU MSRs,
	 * whereas Intel generates #GP on attempts to write reserved/RO MSRs.
	 */

Looks good.

Quoted 10 lines
Quoted 9 lines

arch/x86/kvm/svm/pmu.c static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 196

Quoted 5 lines
arch/x86/kvm/svm/pmu.c +4 -0
+		pmu->nr_arch_gp_counters = min_t(unsigned int,
+						 ebx.split.num_core_pmc,
+						 kvm_pmu_cap.num_counters_gp);
+	} else if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
  		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;

This needs to be sanitized, no? E.g. if KVM only has access to 4 counters, but userspace sets X86_FEATURE_PERFCTR_CORE anyways. Hrm, unless I'm missing something, that's a pre-existing bug.

Quoted 5 lines

Now your point is that if a user space more capbility than KVM can support, KVM should constrain it.
Your previous preference was that the user space can set capbilities that evene if KVM doesn't support as long as it doesn't break KVM and host and the guest will eat its own.

Letting userspace define a "bad" configuration is perfectly ok, but KVM needs to be careful not to endanger itself by consuming the bad state. A good example is the handling of nested SVM features in svm_vcpu_after_set_cpuid(). KVM lets userspace define anything and everything, but KVM only actually tries to utilize a feature if the feature is actually supported in hardware.

In this case, it's not clear to me that putting a bogus value into "nr_arch_gp_counters" is safe (for KVM). And AIUI, the guest can't actually use more than kvm_pmu_cap.num_counters_gp counters, i.e. KVM isn't arbitrarily restricting the setup.

author
#29 lore raw

Re: [PATCH v4 11/12] KVM: x86/svm/pmu: Add AMD PerfMonV2 support

Quoted 10 lines · Sean Christopherson
Quoted 9 lines · Like Xu
Quoted 7 lines · Sean Christopherson

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 609

Quoted 3 lines · Like Xu
arch/x86/kvm/pmu.c +3 -0
+	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+		if (!msr_info->host_initiated)
+			return 0; /* Writes are ignored */

Where is the "writes ignored" behavior documented? I can't find anything in the APM that defines write behavior.

Quoted 7 lines
Quoted 2 lines

KVM would follow the real hardware behavior once specifications stay silent on details or secret.

So is that a "this isn't actually documented anywhere" answer? It's not your responsibility to get AMD to document their CPUs, but I want to clearly document when KVM's behavior is based solely off of observed hardware behavior, versus an actual specification.

Indeed, you draw a clearer line than APM or PPR.

Spec-defined:

RO: Read-only. Readable; writes are ignored (Per PPR "AccessType Definitions")
WO: Writable. Reads are undefined. (Per PPR "AccessType Definitions")

And vPMU will refer to real HW observations for the (hidden) undefined behaviour. More comments in the new version may help. Please check.

Quoted 32 lines
Quoted 7 lines

How about this:

	/*
	 * Note, AMD ignores writes to reserved bits and read-only PMU MSRs,
	 * whereas Intel generates #GP on attempts to write reserved/RO MSRs.
	 */

Looks good.

Quoted 10 lines
Quoted 9 lines

arch/x86/kvm/svm/pmu.c static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 196

Quoted 5 lines
arch/x86/kvm/svm/pmu.c +4 -0
+		pmu->nr_arch_gp_counters = min_t(unsigned int,
+						 ebx.split.num_core_pmc,
+						 kvm_pmu_cap.num_counters_gp);
+	} else if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
   		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;

This needs to be sanitized, no? E.g. if KVM only has access to 4 counters, but userspace sets X86_FEATURE_PERFCTR_CORE anyways. Hrm, unless I'm missing something, that's a pre-existing bug.

Quoted 5 lines

Now your point is that if a user space more capbility than KVM can support, KVM should constrain it.
Your previous preference was that the user space can set capbilities that evene if KVM doesn't support as long as it doesn't break KVM and host and the guest will eat its own.

Letting userspace define a "bad" configuration is perfectly ok, but KVM needs to be careful not to endanger itself by consuming the bad state. A good example is the handling of nested SVM features in svm_vcpu_after_set_cpuid(). KVM lets userspace define anything and everything, but KVM only actually tries to utilize a feature if the feature is actually supported in hardware.

Quoted 4 lines

In this case, it's not clear to me that putting a bogus value into "nr_arch_gp_counters" is safe (for KVM). And AIUI, the guest can't actually use more than kvm_pmu_cap.num_counters_gp counters, i.e. KVM isn't arbitrarily restricting the setup.

AFAI, when a guest has more counters (N) than the host (M), and they are all enabled, thus KVM will create an equal number (N) of perf_events, and these events will occupy real hardware counters (M) in the host perf scheduler subsystem in a round robin way.

From the point of view of a vCPU, its virtual counters can only occupy the hardware part of the time slice to count for guest payload, which affects the accuracy. However, from the host security point of view, too many counters will only result in too many perf_events created by KVM, which is a normal usage for the perf subsystem, called perf counter multiplexing. It seems to be safe (using perf API for KVM).

But considering that scheduling too many perf_events is also a performance overhead, it can also be seen as a performance attack on the scheduling of vCPU processes on host.

Back to the diff itself, code for intel_pmu does a similar sanity check, thus here we just let AMD_PMU follow the same decision pattern. Please refer to the latest version.

author
#30 lore raw

PATCH v4 12/12 KVM: x86/cpuid: Add AMD CPUID ExtPerfMonAndDbg leaf 0x80000022

In reply to Like Xu

From: Like Xu <[email protected]>

CPUID leaf 0x80000022 i.e. ExtPerfMonAndDbg advertises some new performance monitoring features for AMD processors.

Bit 0 of EAX indicates support for Performance Monitoring Version 2 (PerfMonV2) features. If found to be set during PMU initialization, the EBX bits of the same CPUID function can be used to determine the number of available PMCs for different PMU types.

Expose the relevant bits via KVM_GET_SUPPORTED_CPUID so that guests can make use of the PerfMonV2 features.

---
 arch/x86/kvm/cpuid.c   | 24 +++++++++++++++++++++++-
 arch/x86/kvm/svm/svm.c |  6 ++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +23 -1
index b0bb5f9f5307..274cae531d7f 100644
@@ -1124,7 +1124,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 		entry->edx = 0;
 		break;
 	case 0x80000000:
-		entry->eax = min(entry->eax, 0x80000021);
+		entry->eax = min(entry->eax, 0x80000022);
 		/*
 		 * Serializing LFENCE is reported in a multitude of ways, and
 		 * NullSegClearsBase is not reported in CPUID on Zen2; help
@@ -1247,6 +1247,28 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 		if (!static_cpu_has_bug(X86_BUG_NULL_SEG))
 			entry->eax |= BIT(6);
 		break;
+	/* AMD Extended Performance Monitoring and Debug */
+	case 0x80000022: {
+		union cpuid_0x80000022_ebx ebx;
+
+		entry->ecx = entry->edx = 0;
+		if (!enable_pmu || !kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2)) {
+			entry->eax = entry->ebx;
+			break;
+		}
+
+		cpuid_entry_override(entry, CPUID_8000_0022_EAX);
+
+		if (kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2))
+			ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
+		else if (kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
+			ebx.split.num_core_pmc = AMD64_NUM_COUNTERS_CORE;
+		else
+			ebx.split.num_core_pmc = AMD64_NUM_COUNTERS;
+
+		entry->ebx = ebx.full;
+		break;
+	}
 	/*Add support for Centaur's CPUID instruction*/
 	case 0xC0000000:
 		/*Just support up to 0xC0000004 now*/
arch/x86/kvm/svm/svm.c +6 -0
index f4a4691b4f4e..2472fa8746c2 100644
@@ -4916,6 +4916,12 @@ static __init void svm_set_cpu_caps(void)
 		} else {
 			/* AMD PMU PERFCTR_CORE CPUID */
 			kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
+			/*
+			 * KVM only supports AMD PerfMon V2, even if it supports V3+.
+			 * For PerfMon V3+, it's unsafe to expect V2 bit is set or cleared.
+			 */
+			if (kvm_pmu_cap.version > 1)
+				kvm_cpu_cap_set(X86_FEATURE_PERFMON_V2);
 		}
 	}
 
Signature · 1 line
2.39.1
#31 lore raw

Re: [PATCH v4 12/12] KVM: x86/cpuid: Add AMD CPUID ExtPerfMonAndDbg leaf 0x80000022

arch/x86/kvm/svm/svm.c static __init void svm_set_cpu_caps(void) · line 4920

Quoted 10 lines · Like Xu
arch/x86/kvm/svm/svm.c +2 -0
index f4a4691b4f4e..2472fa8746c2 100644
@@ -4916,6 +4916,12 @@ static __init void svm_set_cpu_caps(void)
 		} else {
 			/* AMD PMU PERFCTR_CORE CPUID */
 			kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
+			/*
+			 * KVM only supports AMD PerfMon V2, even if it supports V3+.

Ha! A perfect example of why I strongly prefer that changelogs and comments avoid pronouns. The above "it" reads as:

* KVM only supports AMD PerfMon V2, even if KVM supports V3+.

which is clearly nonsensical.

arch/x86/kvm/svm/svm.c static __init void svm_set_cpu_caps(void) · line 4921

Quoted 1 line
arch/x86/kvm/svm/svm.c +1 -0
+			 * For PerfMon V3+, it's unsafe to expect V2 bit is set or cleared.

If it's unsafe to assume anything v3+ implying v2 support, then it's definitely not safe to assume that KVM can blindly set v2 without future changes. I don't see any reason not to do

arch/x86/kvm/cpuid.c +4 -0
index bd324962bb7e..1192f605ad47 100644
@@ -756,6 +756,10 @@ void kvm_set_cpu_caps(void)
                F(NULL_SEL_CLR_BASE) | F(AUTOIBRS) | 0 /* PrefetchCtlMsr */
        );
 
+       kvm_cpu_cap_mask(CPUID_8000_0022_EAX,
+               F(PERFMON_V2)
+       );
+
        /*
         * Synthesize "LFENCE is serializing" into the AMD-defined entry in
         * KVM's supported CPUID if the feature is reported as supported by the

and then this code can be:

			if (kvm_pmu_cap.version != 2)
				kvm_cpu_cap_clear(X86_FEATURE_PERFMON_V2);

Ah, but presumably the

if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)

path also needs to clear PERFMON_V2. I think I'd still vote to grab host CPUID and clear here (instead of setting).

What is the relationship between PERFCTR_CORE and PERFMON_V2? E.g. if v2 depends on having PERFCTR_CORE, then we can do:

	if (enable_pmu) {
		if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
			kvm_pmu_cap.num_counters_gp = AMD64_NUM_COUNTERS;
		else
			kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);

		if (kvm_pmu_cap.version != 2 ||
		    !kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
			kvm_cpu_cap_clear(X86_FEATURE_PERFMON_V2);
author
#32 lore raw

Re: [PATCH v4 12/12] KVM: x86/cpuid: Add AMD CPUID ExtPerfMonAndDbg leaf 0x80000022

Quoted 15 lines · Sean Christopherson

arch/x86/kvm/svm/svm.c static __init void svm_set_cpu_caps(void) · line 4920

Quoted 10 lines · Like Xu
arch/x86/kvm/svm/svm.c +2 -0
index f4a4691b4f4e..2472fa8746c2 100644
@@ -4916,6 +4916,12 @@ static __init void svm_set_cpu_caps(void)
  		} else {
  			/* AMD PMU PERFCTR_CORE CPUID */
  			kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
+			/*
+			 * KVM only supports AMD PerfMon V2, even if it supports V3+.

Ha! A perfect example of why I strongly prefer that changelogs and comments avoid pronouns. The above "it" reads as:

Quoted 3 lines

* KVM only supports AMD PerfMon V2, even if KVM supports V3+.

which is clearly nonsensical.

I get your point. Thanks.

Quoted 30 lines

arch/x86/kvm/svm/svm.c static __init void svm_set_cpu_caps(void) · line 4921

Quoted 3 lines
arch/x86/kvm/svm/svm.c +1 -0
+			 * For PerfMon V3+, it's unsafe to expect V2 bit is set or cleared.

If it's unsafe to assume anything v3+ implying v2 support, then it's definitely not safe to assume that KVM can blindly set v2 without future changes. I don't see any reason not to do

arch/x86/kvm/cpuid.c +4 -0
index bd324962bb7e..1192f605ad47 100644
@@ -756,6 +756,10 @@ void kvm_set_cpu_caps(void)
                 F(NULL_SEL_CLR_BASE) | F(AUTOIBRS) | 0 /* PrefetchCtlMsr */
         );
 
+       kvm_cpu_cap_mask(CPUID_8000_0022_EAX,
+               F(PERFMON_V2)
+       );
+
         /*
          * Synthesize "LFENCE is serializing" into the AMD-defined entry in
          * KVM's supported CPUID if the feature is reported as supported by the

and then this code can be:

			if (kvm_pmu_cap.version != 2)
				kvm_cpu_cap_clear(X86_FEATURE_PERFMON_V2);
Quoted 6 lines

Ah, but presumably the

if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)

path also needs to clear PERFMON_V2. I think I'd still vote to grab host CPUID and clear here (instead of setting).

Looks good to me.

Quoted 3 lines

What is the relationship between PERFCTR_CORE and PERFMON_V2? E.g. if v2 depends on having PERFCTR_CORE, then we can do:

Yes, the PERFCTR_CORE bit will always be set if the v2 bit is set.

Quoted 10 lines
	if (enable_pmu) {
		if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
			kvm_pmu_cap.num_counters_gp = AMD64_NUM_COUNTERS;
		else
			kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);

		if (kvm_pmu_cap.version != 2 ||
		    !kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
			kvm_cpu_cap_clear(X86_FEATURE_PERFMON_V2);
#33 lore raw
In reply to Like Xu
Quoted 10 lines · Like Xu

Starting with Zen4, core PMU on AMD platforms such as Genoa and Ryzen-7000 will support PerfMonV2, and it is also compatible with legacy PERFCTR_CORE behavior and msr addresses.

If you don't have access to the hardware specification, the commits d6d0c7f681fd..7685665c390d for host perf can also bring a quick overview. Its main change is the addition of three msr's equivalent to Intel V2, namely global_ctrl, global_status, global_status_clear.

[...]

Applied 1-3 to kvm-x86 pmu. Note, I grabbed my full version of patch 2 that also converts the "get" path.

My apologies for not reviewing this earlier in the cycle. I need to pivot to TDX+SNP stuff "soon", so AMD v2 support will likely miss 6.4, but I'll prioritize reviewing and merging support in 6.5.

Thanks!

[01/12] KVM: x86/pmu: Rename pmc_is_enabled() to pmc_is_globally_enabled()

        https://github.com/kvm-x86/linux/commit/cdd2fbf6360e
[02/12] KVM: VMX: Refactor intel_pmu_{g,}set_msr() to align with other helpers
        https://github.com/kvm-x86/linux/commit/8bca8c5ce40b
[03/12] KVM: x86/pmu: Rewrite reprogram_counters() to improve performance
        https://github.com/kvm-x86/linux/commit/649bccd7fac9

--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes

author
#34 lore raw
Quoted 10 lines · Sean Christopherson
Quoted 9 lines · Like Xu

Starting with Zen4, core PMU on AMD platforms such as Genoa and Ryzen-7000 will support PerfMonV2, and it is also compatible with legacy PERFCTR_CORE behavior and msr addresses.

If you don't have access to the hardware specification, the commits d6d0c7f681fd..7685665c390d for host perf can also bring a quick overview. Its main change is the addition of three msr's equivalent to Intel V2, namely global_ctrl, global_status, global_status_clear.

Quoted 8 lines
Quoted 1 line

[...]

Applied 1-3 to kvm-x86 pmu. Note, I grabbed my full version of patch 2 that also converts the "get" path.

My apologies for not reviewing this earlier in the cycle. I need to pivot to TDX+SNP stuff "soon", so AMD v2 support will likely miss 6.4, but I'll prioritize reviewing and merging support in 6.5.

Thanks for your review time.

Since the biggest points of discussion so far is around

	[PATCH v4 11/12] KVM: x86/svm/pmu: Add AMD PerfMonV2 support,

so once the proposal on that part is confirmed by you, V5 is already in sight.

Quoted 13 lines

Thanks!

[01/12] KVM: x86/pmu: Rename pmc_is_enabled() to pmc_is_globally_enabled()

         https://github.com/kvm-x86/linux/commit/cdd2fbf6360e
[02/12] KVM: VMX: Refactor intel_pmu_{g,}set_msr() to align with other helpers
         https://github.com/kvm-x86/linux/commit/8bca8c5ce40b
[03/12] KVM: x86/pmu: Rewrite reprogram_counters() to improve performance
         https://github.com/kvm-x86/linux/commit/649bccd7fac9

--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes