Skip to contentSkip to conversation

Upgrade intel vPMU version to 5

PATCH In review Xiong Zhang 4 participants 18 messages Sep 21, 2023 – Mar 12

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover Upgrade intel vPMU version to 5

Intel recent processors have supported Architectural Performance Monitoring Version 5, while Intel vPMU still keeps on version 2. In order to use new PMU features introduced in version 4 and 5, this patchset upgrades vPMU version to 5.

Go through PMU features from version 3 to 5, the following features are supported by this patchset:
1. Streamlined Freeze LBR on PMI on version 4. This feature adds a new bit IA32_MSR_PERF_GLOBAL_STATUS.LBR_FRZ[58], it will be set when PMI happens and LBR stack is forzen. This bit also serves as a control to enable LBR stack. SW should clear this bit at the end of PMI handler to enable LBR stack.
2. IA32_PERF_GLOBAL_STATUS_RESET MSR on version 4. its address is inherited from IA32_PERF_GLOBAL_OVF_CTRL MSR, and they have the same function to clear individual bits in IA32_PERF_GLOBAL_STATUS MSR.
3. IA32_PERF_GLOBAL_STATUS_SET MSR on version 4. it allows software to set individual bits in IA32_PERF_GLOBAL_STATUS MSR.
4. IA32_PERF_GLOBAL_INUSE MSR on version 4. It provides an "InUse" bit for each programmable performance counter and fixed counter in the processor. Additionally, it includes an indicator if the PMI mechanisam has been used.
5. Fixed Counter Enumeration on version 5. CPUID.0AH.ECX provides a bit mask which enumerates the supported Fixed Counters in a processor.

For each added feature, the kvm emulation is straightforward and reflects vPMU state, please see each feature's emulation code in the following commits, a kvm unit test case or kernel selftests is added to verify this feature's emultion. Kernel doesn't use feature 3 and 4, so designed kvm unit test case is the only verification method for feature 3 and 4. I'm afraid that I miss something for these features, especially the user case for these features.

While the following features are not supported:
1. AnyThread counting: it is added in v3, and deprecated in v5. so this feature isn't supported.
2. Streamlined Freeze_PerfMon_On_PMI on version 4. Since legacy Freeze_PerMon_On_PMI on version 2 isn't supported and community think this feature has problems on native[1], so this feature's emulation isn't supported.
3. IA32_PERF_GLOBAL_STATUS.ASCI[bit 60] on version 4. This new bit relates to SGX, and will be emulated by SGX developer.
4. Domain Seperation on version 5. When INV flag in IA32_PERFEVTSELx is used, a counter stops counting when logical processor exits the C0 ACPI C-state. Guest ACPI C-state is vague, KVM has little information about guest ACPI C-state. So this feature's emulation isn't supported.

Changes since v1:
* vLBR release bug not only happens in Freeze_LBR_On_PMI, but also happens on generic case. So the first commit fixes this bug. (Like Xu)
* Add new PERF_GLOBAL_STATUS_SET and PERF_GLOBAL_STATUS_INUSE MSRs into msrs_to_save_pmu[] (Like Xu)
* If INUSE_PMI bit is set, no need to write it again. (Like Xu)
* Get Fixed Counter enumeration from host for nested vPMU, but I marked it as Todo to wait for perf driver fixed counter bitmap supporting.

Reference:
[1]: perf/intel: Remove Perfmon-v4 counter_freezing support
https://lore.kernel.org/all/[email protected]/

Like Xu (1):
KVM: x86/pmu: Add Intel PMU supported fixed counters mask

Xiong Zhang (8):
KVM: x86/PMU: Delay vLBR release to the vcpu next sched-in
KVM: x86/pmu: Don't release vLBR casued by vPMI
KVM: x85/pmu: Add Streamlined FREEZE_LBR_ON_PMI for vPMU v4
KVM: x86/pmu: Add PERF_GLOBAL_STATUS_SET MSR emulation
KVM: x86/pmu: Add MSR_CORE_PERF_GLOBAL_INUSE emulation
KVM: x86/pmu: Add fixed counter enumeration for pmu v5
KVM: x86/pmu: Upgrade pmu version to 5 on intel processor
KVM: selftests: Add fixed counters enumeration test case

 arch/x86/include/asm/kvm_host.h               |   1 -
 arch/x86/include/asm/msr-index.h              |   6 +
 arch/x86/kvm/cpuid.c                          |  10 +-
 arch/x86/kvm/pmu.c                            |   8 -
 arch/x86/kvm/pmu.h                            |  14 +-
 arch/x86/kvm/svm/pmu.c                        |   1 -
 arch/x86/kvm/vmx/pmu_intel.c                  | 205 +++++++++++++++---
 arch/x86/kvm/vmx/vmx.c                        |  19 +-
 arch/x86/kvm/vmx/vmx.h                        |   8 +
 arch/x86/kvm/x86.c                            |   1 +
 .../selftests/kvm/x86_64/vmx_pmu_caps_test.c  |  79 +++++++
 11 files changed, 308 insertions(+), 44 deletions(-)

base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d

Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/9 KVM: x86/PMU: Delay vLBR release to the vcpu next sched-in

If guest LBR is disabled at vCPU sched-in time, the vLBR event will be released, then the following guest LBR MSRs accessing will be trapped, and cause KVM to create new vLBR event. If this new vLBR event is the only user of host LBR facility, host LBR driver will reset LBR facility at vLBR creation. So guest LBR content may be changed during vCPU sched-out and sched-in.

Considering this serial:
1. Guest disables LBR.
2. Guest reads LBR MSRs, but it doesn't finish.
3. vCPU is sched-out, later sched-in, vLBR event is released.
4. Guest continue reading LBR MSRs, KVM creates vLBR event again, if this vLBR event is the only LBR user on host now, host LBR driver will reset HW LBR facility at vLBR creataion.
5. Guest gets the remain LBR MSRs with reset state.
So gueest LBR MSRs reading before vCPU sched-out is correct, while guest LBR MSRs reading after vCPU sched-out is wrong and is in reset state. Similarly guest LBR MSRs writing before vCPU sched-out is lost and is in reset state, while guest LBR MSRs writing after vCPU sched-out is correct.

This is a bug that guest LBR content is changed as vCPU's scheduling. This can happen when guest LBR MSRs accessing spans vCPU's scheduling, usually guest access LBR MSRs at guest task switch and PMI handler.

Two options could be used to fixed this bug:
a. Save guest LBR snapshot at vLBR release in step 3, then restore guest LBR after vLBR creation in step 4. But the number of LBR MSRs is near 100, this means 100 MSRs reading and 100s writing are needed for each vLBR release, the overhead is too heavy. b. Defer vLBR release in step 3.

This commit choose the option b. Guest LBR MSRs accessing is passthrough, so the interceptable guest DEBUGCTLMSR_LBR bit is used to predict guest LBR usage. If guest LBR is disabled in a whole vCPU shced time slice, KVM will predict guest LBR won't be used recently, then vLBR will be released in next vCPU sched-in. Guest LBR MSRs accessing should be finished in two vCPU sched time slice, otherwise it is maybe a guest LBR driver bug and can not be supported by this commit.

---
 arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++--
 arch/x86/kvm/vmx/vmx.c       | 12 +++++++++---
 arch/x86/kvm/vmx/vmx.h       |  2 ++
 3 files changed, 19 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +8 -2
index f2efa0bf7ae8..76d7bd8e4fc6 100644
@@ -628,6 +628,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
 	lbr_desc->records.nr = 0;
 	lbr_desc->event = NULL;
 	lbr_desc->msr_passthrough = false;
+	lbr_desc->in_use = FALSE;
 }
 
 static void intel_pmu_reset(struct kvm_vcpu *vcpu)
@@ -761,8 +762,13 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)
 
 static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)
 {
-	if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR))
-		intel_pmu_release_guest_lbr_event(vcpu);
+	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
+
+	if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR)) {
+		if (!lbr_desc->in_use)
+			intel_pmu_release_guest_lbr_event(vcpu);
+		lbr_desc->in_use = false;
+	}
 }
 
 void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu)
arch/x86/kvm/vmx/vmx.c +9 -3
index 72e3943f3693..4056e19266b5 100644
@@ -2238,9 +2238,15 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			get_vmcs12(vcpu)->guest_ia32_debugctl = data;
 
 		vmcs_write64(GUEST_IA32_DEBUGCTL, data);
-		if (intel_pmu_lbr_is_enabled(vcpu) && !to_vmx(vcpu)->lbr_desc.event &&
-		    (data & DEBUGCTLMSR_LBR))
-			intel_pmu_create_guest_lbr_event(vcpu);
+
+		if (intel_pmu_lbr_is_enabled(vcpu) && (data & DEBUGCTLMSR_LBR)) {
+			struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
+
+			lbr_desc->in_use = true;
+			if (!lbr_desc->event)
+				intel_pmu_create_guest_lbr_event(vcpu);
+		}
+
 		return 0;
 	}
 	case MSR_IA32_BNDCFGS:
arch/x86/kvm/vmx/vmx.h +2 -0
index c2130d2c8e24..547edeb52d09 100644
@@ -107,6 +107,8 @@ struct lbr_desc {
 
 	/* True if LBRs are marked as not intercepted in the MSR bitmap */
 	bool msr_passthrough;
+
+	bool in_use;
 };
 
 /*
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/9 KVM: x86/pmu: Don't release vLBR casued by vPMI

In reply to Xiong Zhang

To emulate Freeze_LBR_On_PMI feature, KVM PMU disables guest LBR at PMI injection. When guest LBR is disabled, vLBR event may be released at two vCPU sched-in later. once vLBR event is released, KVM will create a new vLBR event when guest access LBR MSRs again. First this adds overhead at vLBR event release and re-creation. Second this may changes guest LBR contend as vLBR event creation may cause host LBR drvier reset hw LBR facility.

This commit avoids the vLBR release for Freeze_LBR_On_PMI emulation. It changes boolean lbr_desc->in_use into enum lbr_desc->state, so it could express more LBR states, KVM sets lbr_desc->state as FREEZE_ON_PMI at vPMI injection, so that vLBR release function could check this state to avoid vLBR release. When guest enables LBR at the end of the guest PMI handler, KVM will set lbr_desc->state as IN_USE.

---
 arch/x86/kvm/vmx/pmu_intel.c | 8 +++++---
 arch/x86/kvm/vmx/vmx.c       | 2 +-
 arch/x86/kvm/vmx/vmx.h       | 8 +++++++-
 3 files changed, 13 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +5 -3
index 76d7bd8e4fc6..c8d46c3d1ab6 100644
@@ -628,7 +628,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
 	lbr_desc->records.nr = 0;
 	lbr_desc->event = NULL;
 	lbr_desc->msr_passthrough = false;
-	lbr_desc->in_use = FALSE;
+	lbr_desc->state = LBR_STATE_PREDICT_FREE;
 }
 
 static void intel_pmu_reset(struct kvm_vcpu *vcpu)
@@ -671,6 +671,7 @@ static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu)
 	if (data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) {
 		data &= ~DEBUGCTLMSR_LBR;
 		vmcs_write64(GUEST_IA32_DEBUGCTL, data);
+		vcpu_to_lbr_desc(vcpu)->state = LBR_STATE_FREEZE_ON_PMI;
 	}
 }
 
@@ -765,9 +766,10 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)
 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
 
 	if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR)) {
-		if (!lbr_desc->in_use)
+		if (lbr_desc->state == LBR_STATE_PREDICT_FREE)
 			intel_pmu_release_guest_lbr_event(vcpu);
-		lbr_desc->in_use = false;
+		else if (lbr_desc->state == LBR_STATE_IN_USE)
+			lbr_desc->state = LBR_STATE_PREDICT_FREE;
 	}
 }
 
arch/x86/kvm/vmx/vmx.c +1 -1
index 4056e19266b5..565df8eeb78b 100644
@@ -2242,7 +2242,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (intel_pmu_lbr_is_enabled(vcpu) && (data & DEBUGCTLMSR_LBR)) {
 			struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
 
-			lbr_desc->in_use = true;
+			lbr_desc->state = LBR_STATE_IN_USE;
 			if (!lbr_desc->event)
 				intel_pmu_create_guest_lbr_event(vcpu);
 		}
arch/x86/kvm/vmx/vmx.h +7 -1
index 547edeb52d09..0cb68a319fc8 100644
@@ -93,6 +93,12 @@ union vmx_exit_reason {
 	u32 full;
 };
 
+enum lbr_state {
+	LBR_STATE_PREDICT_FREE = 0,
+	LBR_STATE_IN_USE = 1,
+	LBR_STATE_FREEZE_ON_PMI = 2
+};
+
 struct lbr_desc {
 	/* Basic info about guest LBR records. */
 	struct x86_pmu_lbr records;
@@ -108,7 +114,7 @@ struct lbr_desc {
 	/* True if LBRs are marked as not intercepted in the MSR bitmap */
 	bool msr_passthrough;
 
-	bool in_use;
+	enum lbr_state state;
 };
 
 /*
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v2 2/9] KVM: x86/pmu: Don't release vLBR casued by vPMI

Quoted 46 lines · Xiong Zhang

To emulate Freeze_LBR_On_PMI feature, KVM PMU disables guest LBR at PMI injection. When guest LBR is disabled, vLBR event may be released at two vCPU sched-in later. once vLBR event is released, KVM will create a new vLBR event when guest access LBR MSRs again. First this adds overhead at vLBR event release and re-creation. Second this may changes guest LBR contend as vLBR event creation may cause host LBR drvier reset hw LBR facility.

This commit avoids the vLBR release for Freeze_LBR_On_PMI emulation. It changes boolean lbr_desc->in_use into enum lbr_desc->state, so it could express more LBR states, KVM sets lbr_desc->state as FREEZE_ON_PMI at vPMI injection, so that vLBR release function could check this state to avoid vLBR release. When guest enables LBR at the end of the guest PMI handler, KVM will set lbr_desc->state as IN_USE.

---
  arch/x86/kvm/vmx/pmu_intel.c | 8 +++++---
  arch/x86/kvm/vmx/vmx.c       | 2 +-
  arch/x86/kvm/vmx/vmx.h       | 8 +++++++-
  3 files changed, 13 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +2 -1
index 76d7bd8e4fc6..c8d46c3d1ab6 100644
@@ -628,7 +628,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
  	lbr_desc->records.nr = 0;
  	lbr_desc->event = NULL;
  	lbr_desc->msr_passthrough = false;
-	lbr_desc->in_use = FALSE;
+	lbr_desc->state = LBR_STATE_PREDICT_FREE;
  }
 
  static void intel_pmu_reset(struct kvm_vcpu *vcpu)
@@ -671,6 +671,7 @@ static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu)
  	if (data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) {
  		data &= ~DEBUGCTLMSR_LBR;
  		vmcs_write64(GUEST_IA32_DEBUGCTL, data);
+		vcpu_to_lbr_desc(vcpu)->state = LBR_STATE_FREEZE_ON_PMI;
  	}
  }
 
@@ -765,9 +766,10 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)
  	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);

arch/x86/kvm/vmx/pmu_intel.c static void intel_pmu_cleanup(struct kvm_vcpu *vcpu) · line 774

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +3 -2
 
  	if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR)) {
-		if (!lbr_desc->in_use)
+		if (lbr_desc->state == LBR_STATE_PREDICT_FREE)
  			intel_pmu_release_guest_lbr_event(vcpu);
-		lbr_desc->in_use = false;
+		else if (lbr_desc->state == LBR_STATE_IN_USE)
+			lbr_desc->state = LBR_STATE_PREDICT_FREE;
  	}
  }

I'm concerning whether there is a risk that the vLBR event can't be released forever. Base on the logic, the state must be set to LBR_STATE_IN_USE firstly and then could be freed in next cycle. Assuming the guest PMI handler doesn't enable LBR again as some reason, the LBR state would stay in LBR_STATE_FREEZE_ON_PMI state and the vLBR event would not be released and LBR stack would not be reset. when guest enables LBR in next time, guest may see the stale LBR data. I think we may add a backup mechanism to ensure the vLBR event can be freed if vLBR is disabled after several schedule slots.

Quoted 40 lines
arch/x86/kvm/vmx/vmx.c +1 -1
index 4056e19266b5..565df8eeb78b 100644
@@ -2242,7 +2242,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  		if (intel_pmu_lbr_is_enabled(vcpu) && (data & DEBUGCTLMSR_LBR)) {
  			struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
 
-			lbr_desc->in_use = true;
+			lbr_desc->state = LBR_STATE_IN_USE;
  			if (!lbr_desc->event)
  				intel_pmu_create_guest_lbr_event(vcpu);
  		}
arch/x86/kvm/vmx/vmx.h +7 -1
index 547edeb52d09..0cb68a319fc8 100644
@@ -93,6 +93,12 @@ union vmx_exit_reason {
  	u32 full;
  };
 
+enum lbr_state {
+	LBR_STATE_PREDICT_FREE = 0,
+	LBR_STATE_IN_USE = 1,
+	LBR_STATE_FREEZE_ON_PMI = 2
+};
+
  struct lbr_desc {
  	/* Basic info about guest LBR records. */
  	struct x86_pmu_lbr records;
@@ -108,7 +114,7 @@ struct lbr_desc {
  	/* True if LBRs are marked as not intercepted in the MSR bitmap */
  	bool msr_passthrough;
 
-	bool in_use;
+	enum lbr_state state;
  };
 
  /*
#4 lore raw

RE: [PATCH v2 2/9] KVM: x86/pmu: Don't release vLBR casued by vPMI

Quoted 60 lines · Xiong Zhang
Quoted 35 lines

To emulate Freeze_LBR_On_PMI feature, KVM PMU disables guest LBR at PMI injection. When guest LBR is disabled, vLBR event may be released at two vCPU sched-in later. once vLBR event is released, KVM will create a new vLBR event when guest access LBR MSRs again. First this adds overhead at vLBR event release and re-creation. Second this may changes guest LBR contend as vLBR event creation may cause host LBR drvier reset hw LBR facility.

This commit avoids the vLBR release for Freeze_LBR_On_PMI emulation. It changes boolean lbr_desc->in_use into enum lbr_desc->state, so it could express more LBR states, KVM sets lbr_desc->state as FREEZE_ON_PMI at vPMI injection, so that vLBR release function could check this state to avoid vLBR release. When guest enables LBR at the end of the guest PMI handler, KVM will set lbr_desc->state as IN_USE.

---
  arch/x86/kvm/vmx/pmu_intel.c | 8 +++++---
  arch/x86/kvm/vmx/vmx.c       | 2 +-
  arch/x86/kvm/vmx/vmx.h       | 8 +++++++-
  3 files changed, 13 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/arch/x86/kvm/vmx/pmu_intel.c index 76d7bd8e4fc6..c8d46c3d1ab6 100644

arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -628,7 +628,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
  	lbr_desc->records.nr = 0;
  	lbr_desc->event = NULL;
  	lbr_desc->msr_passthrough = false;
-	lbr_desc->in_use = FALSE;
+	lbr_desc->state = LBR_STATE_PREDICT_FREE;
  }
 
  static void intel_pmu_reset(struct kvm_vcpu *vcpu) @@ -671,6 +671,7
Quoted 1 line

@@ static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu

*vcpu)

Quoted 9 lines
  	if (data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) {
  		data &= ~DEBUGCTLMSR_LBR;
  		vmcs_write64(GUEST_IA32_DEBUGCTL, data);
arch/x86/kvm/vmx/pmu_intel.c +1 -0
+		vcpu_to_lbr_desc(vcpu)->state = LBR_STATE_FREEZE_ON_PMI;
  	}
  }
@@ -765,9 +766,10 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)
  	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);

arch/x86/kvm/vmx/pmu_intel.c static void intel_pmu_cleanup(struct kvm_vcpu *vcpu) · line 774

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +3 -2
 
  	if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR)) {
-		if (!lbr_desc->in_use)
+		if (lbr_desc->state == LBR_STATE_PREDICT_FREE)
  			intel_pmu_release_guest_lbr_event(vcpu);
-		lbr_desc->in_use = false;
+		else if (lbr_desc->state == LBR_STATE_IN_USE)
+			lbr_desc->state = LBR_STATE_PREDICT_FREE;
  	}
  }
Quoted 8 lines

I'm concerning whether there is a risk that the vLBR event can't be released forever. Base on the logic, the state must be set to LBR_STATE_IN_USE firstly and then could be freed in next cycle. Assuming the guest PMI handler doesn't enable LBR again as some reason, the LBR state would stay in LBR_STATE_FREEZE_ON_PMI state and the vLBR event would not be released and LBR stack would not be reset.
when guest enables LBR in next time, guest
may see the stale LBR data.

Processor won't reset LBR stack by itself, LBR driver resets LBR stack. So guest LBR driver could reset vLBR before enable it.

Quoted 2 lines

I think we may add a backup mechanism to ensure
the vLBR event can be freed if vLBR is disabled after several schedule slots.

Without this commits, disabled vLBR will be released in next two schedule slot. This commit is an optimization that vLBR disabled by KVM lbr_freeze_on_pmi emulation never be released during guest PMI handler. Compared with the suggested backup mechanism, I prefer to drop this optimization.

thanks

Quoted 41 lines

arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu,

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c index renamed from arch/x86/kvm/vmx/vmx.c +0 -0

4056e19266b5..565df8eeb78b 100644

arch/x86/kvm/vmx/vmx.c +0 -0
@@ -2242,7 +2242,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu,

struct msr_data *msr_info)

arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, · line 2242

Quoted 1 line
arch/x86/kvm/vmx/vmx.c +0 -0
  		if (intel_pmu_lbr_is_enabled(vcpu) && (data &

DEBUGCTLMSR_LBR)) {

Quoted 33 lines
arch/x86/kvm/vmx/vmx.c +1 -1
  			struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
 
-			lbr_desc->in_use = true;
+			lbr_desc->state = LBR_STATE_IN_USE;
  			if (!lbr_desc->event)
  				intel_pmu_create_guest_lbr_event(vcpu);
  		}
arch/x86/kvm/vmx/vmx.h index renamed from arch/x86/kvm/vmx/vmx.h +0 -0

547edeb52d09..0cb68a319fc8 100644

arch/x86/kvm/vmx/vmx.h +7 -1
@@ -93,6 +93,12 @@ union vmx_exit_reason {
  	u32 full;
  };
 
+enum lbr_state {
+	LBR_STATE_PREDICT_FREE = 0,
+	LBR_STATE_IN_USE = 1,
+	LBR_STATE_FREEZE_ON_PMI = 2
+};
+
  struct lbr_desc {
  	/* Basic info about guest LBR records. */
  	struct x86_pmu_lbr records;
@@ -108,7 +114,7 @@ struct lbr_desc {
  	/* True if LBRs are marked as not intercepted in the MSR bitmap */
  	bool msr_passthrough;
 
-	bool in_use;
+	enum lbr_state state;
  };
 
  /*
#5 lore raw

PATCH v2 3/9 KVM: x85/pmu: Add Streamlined FREEZE_LBR_ON_PMI for vPMU v4

In reply to Xiong Zhang

Arch PMU version 4 adds a streamlined FREEZE_LBR_ON_PMI feature, this feature adds LBR_FRZ[bit 58] into IA32_PERF_GLOBAL_STATUS, this bit is set due to the following conditions:
-- IA32_DEBUGCTL.FREEZE_LBR_ON_PMI has been set
-- A performance counter, configured to generate PMI, has overflowed to signal a PMI. Consequently the LBR stack is frozen. Effectively, this bit also serves as a control to enabled capturing data in the LBR stack. When this bit is set, LBR stack is frozen, and new LBR records won't be filled.

The sequence of streamlined freeze LBR is:
1. Profiling agent set IA32_DEBUGCTL.FREEZE_LBR_ON_PMI, and enable a performance counter to generate PMI on overflow.
2. Processor generates PMI and sets IA32_PERF_GLOBAL_STATUS.LBR_FRZ, then LBR stack is forzen.
3. Profiling agent PMI handler handles overflow, and clears IA32_PERF_GLOBAL_STATUS.
4. When IA32_PERF_GLOBAL_STATUS.LBR_FRZ is cleared in step 3, processor resume LBR stack, and new LBR records can be filled again.

In order to emulate this behavior, LBR stack must be frozen on guest vPMI. KVM has two choice to do this:
1. KVM stops vLBR event through perf_event_pause(), and put vLBR event into off state, then vLBR lose LBR hw resource, finally guest couldn't read LBR records in guest PMI handler. This choice couldn't be used.
2. KVM clears guest DEBUGCTLMSR_LBR bit in VMCS on PMI, so when guest is running, LBR HW stack is disabled, while vLBR event is still active and own LBR HW, so guest could still read LBR records in guest PMI handler. But the sequence of streamlined freeze LBR doesn't clear DEBUGCTLMSR_LBR bit, so when guest read guest DEBUGCTL_MSR, KVM will return a value with DEBUGCTLMSR_LBR bit set during LBR freezing. Once guest clears IA32_PERF_GLOBAL_STATUS.LBR_FRZ in step 4, KVM will re-enable guest LBR through setting guest DEBUGCTL_LBR bit in VMCS.

As KVM needs re-enable guest LBR when guest clears global status, the handling of GLOBAL_OVF_CTRL MSR is moved from common pmu.c into vmx/pmu_intel.c.

---
 arch/x86/include/asm/msr-index.h |  1 +
 arch/x86/kvm/pmu.c               |  8 ------
 arch/x86/kvm/vmx/pmu_intel.c     | 49 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c           |  7 +++++
 4 files changed, 57 insertions(+), 8 deletions(-)
arch/x86/include/asm/msr-index.h +1 -0
index 1d111350197f..badc2f729a8e 100644
@@ -1054,6 +1054,7 @@
 /* PERF_GLOBAL_OVF_CTL bits */
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT	55
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI		(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
+#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE		BIT_ULL(58)
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT		62
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF			(1ULL <<  MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT		63
arch/x86/kvm/pmu.c +0 -8
index edb89b51b383..4b6a508f3f0b 100644
@@ -640,14 +640,6 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			reprogram_counters(pmu, diff);
 		}
 		break;
-	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
-		/*
-		 * GLOBAL_OVF_CTRL, a.k.a. GLOBAL STATUS_RESET, clears bits in
-		 * GLOBAL_STATUS, and so the set of reserved bits is the same.
-		 */
-		if (data & pmu->global_status_mask)
-			return 1;
-		fallthrough;
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
 		if (!msr_info->host_initiated)
 			pmu->global_status &= ~data;
arch/x86/kvm/vmx/pmu_intel.c +49 -0

73 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.c +7 -0
index 565df8eeb78b..66e4cf714dbd 100644
@@ -2113,6 +2113,13 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		break;
 	case MSR_IA32_DEBUGCTLMSR:
 		msr_info->data = vmcs_read64(GUEST_IA32_DEBUGCTL);
+		/*
+		 * In streamlined Freeze_LBR_On_PMI, guest should see LBR_EN.
+		 */
+		if ((vcpu_to_pmu(vcpu)->global_status &
+		     MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE) &&
+		    vcpu_to_pmu(vcpu)->version >= 4)
+			msr_info->data |= DEBUGCTLMSR_LBR;
 		break;
 	default:
 	find_uret_msr:
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2 3/9] KVM: x85/pmu: Add Streamlined FREEZE_LBR_ON_PMI for vPMU v4

Quoted 100 lines · Xiong Zhang

Arch PMU version 4 adds a streamlined FREEZE_LBR_ON_PMI feature, this feature adds LBR_FRZ[bit 58] into IA32_PERF_GLOBAL_STATUS, this bit is set due to the following conditions:
-- IA32_DEBUGCTL.FREEZE_LBR_ON_PMI has been set
-- A performance counter, configured to generate PMI, has overflowed to signal a PMI. Consequently the LBR stack is frozen. Effectively, this bit also serves as a control to enabled capturing data in the LBR stack. When this bit is set, LBR stack is frozen, and new LBR records won't be filled.

The sequence of streamlined freeze LBR is:
1. Profiling agent set IA32_DEBUGCTL.FREEZE_LBR_ON_PMI, and enable a performance counter to generate PMI on overflow.
2. Processor generates PMI and sets IA32_PERF_GLOBAL_STATUS.LBR_FRZ, then LBR stack is forzen.
3. Profiling agent PMI handler handles overflow, and clears IA32_PERF_GLOBAL_STATUS.
4. When IA32_PERF_GLOBAL_STATUS.LBR_FRZ is cleared in step 3, processor resume LBR stack, and new LBR records can be filled again.

In order to emulate this behavior, LBR stack must be frozen on guest vPMI. KVM has two choice to do this:
1. KVM stops vLBR event through perf_event_pause(), and put vLBR event into off state, then vLBR lose LBR hw resource, finally guest couldn't read LBR records in guest PMI handler. This choice couldn't be used.
2. KVM clears guest DEBUGCTLMSR_LBR bit in VMCS on PMI, so when guest is running, LBR HW stack is disabled, while vLBR event is still active and own LBR HW, so guest could still read LBR records in guest PMI handler. But the sequence of streamlined freeze LBR doesn't clear DEBUGCTLMSR_LBR bit, so when guest read guest DEBUGCTL_MSR, KVM will return a value with DEBUGCTLMSR_LBR bit set during LBR freezing. Once guest clears IA32_PERF_GLOBAL_STATUS.LBR_FRZ in step 4, KVM will re-enable guest LBR through setting guest DEBUGCTL_LBR bit in VMCS.

As KVM needs re-enable guest LBR when guest clears global status, the handling of GLOBAL_OVF_CTRL MSR is moved from common pmu.c into vmx/pmu_intel.c.

---
  arch/x86/include/asm/msr-index.h |  1 +
  arch/x86/kvm/pmu.c               |  8 ------
  arch/x86/kvm/vmx/pmu_intel.c     | 49 ++++++++++++++++++++++++++++++++
  arch/x86/kvm/vmx/vmx.c           |  7 +++++
  4 files changed, 57 insertions(+), 8 deletions(-)
arch/x86/include/asm/msr-index.h +1 -0
index 1d111350197f..badc2f729a8e 100644
@@ -1054,6 +1054,7 @@
  /* PERF_GLOBAL_OVF_CTL bits */
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT	55
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI		(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
+#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE		BIT_ULL(58)
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT		62
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF			(1ULL <<  MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT		63
arch/x86/kvm/pmu.c +0 -8
index edb89b51b383..4b6a508f3f0b 100644
@@ -640,14 +640,6 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  			reprogram_counters(pmu, diff);
  		}
  		break;
-	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
-		/*
-		 * GLOBAL_OVF_CTRL, a.k.a. GLOBAL STATUS_RESET, clears bits in
-		 * GLOBAL_STATUS, and so the set of reserved bits is the same.
-		 */
-		if (data & pmu->global_status_mask)
-			return 1;
-		fallthrough;
  	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
  		if (!msr_info->host_initiated)
  			pmu->global_status &= ~data;
arch/x86/kvm/vmx/pmu_intel.c +12 -0
index c8d46c3d1ab6..6e3bbe777bf5 100644
@@ -426,6 +426,34 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
  		pmu->pebs_data_cfg = data;
  		break;
+	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+		/*
+		 * GLOBAL_OVF_CTRL, a.k.a. GLOBAL STATUS_RESET, clears bits in
+		 * GLOBAL_STATUS, and so the set of reserved bits is the same.
+		 */
+		if (data & pmu->global_status_mask)
+			return 1;
+
+		/*
+		 * Guest clears GLOBAL_STATUS_LBR_FREEZE bit, KVM re-enable
+		 * Guset LBR which is disabled at vPMI injection for streamlined
+		 * Freeze_LBR_On_PMI.

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

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +10 -0
+		 */
+		if (pmu->version >= 4 && !msr_info->host_initiated &&
+		    (data & MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE) &&
+		    (pmu->global_status & MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE)) {
+			u64 debug_ctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+
+			if (!(debug_ctl & DEBUGCTLMSR_LBR)) {
+				debug_ctl |= DEBUGCTLMSR_LBR;
+				vmcs_write64(GUEST_IA32_DEBUGCTL, debug_ctl);
+			}

I'm thinking if we can enable guest LBR directly here. I'm afraid that some guest handlers may not follow the SDM strictly and they may disable LBR before writing GLOBAL_OVF_CTRL in guest PMI handler, then we should not enable LBR directly here.

Quoted 69 lines
arch/x86/kvm/vmx/pmu_intel.c +27 -0
+			vcpu_to_lbr_desc(vcpu)->state = LBR_STATE_IN_USE;
+		}
+
+		if (!msr_info->host_initiated)
+			pmu->global_status &= ~data;
+		break;
  	default:
  		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
  		    (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {
@@ -565,6 +593,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
  	if (vmx_pt_mode_is_host_guest())
  		pmu->global_status_mask &=
  				~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
+	if (pmu->version >= 4)
+		pmu->global_status_mask &=
+				~MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE;
 
  	entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
  	if (entry &&
@@ -675,6 +706,22 @@ static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu)
  	}
  }
 
+static void intel_pmu_streamlined_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu)
+{
+	u64 data = vmcs_read64(GUEST_IA32_DEBUGCTL);
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+
+	/*
+	 * Even if streamlined freezing LBR won't clear LBR_EN like legacy
+	 * freezing LBR, here legacy freezing LBR is called to freeze LBR HW
+	 * for streamlined freezing LBR when guest run. But guest VM will
+	 * see a fake guest DEBUGCTL MSR with LBR_EN bit set.
+	 */
+	intel_pmu_legacy_freezing_lbrs_on_pmi(vcpu);
+	if ((data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) && (data & DEBUGCTLMSR_LBR))
+		pmu->global_status |= MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE;
+}
+
  static void intel_pmu_deliver_pmi(struct kvm_vcpu *vcpu)
  {
  	u8 version = vcpu_to_pmu(vcpu)->version;
@@ -684,6 +731,8 @@ static void intel_pmu_deliver_pmi(struct kvm_vcpu *vcpu)
 
  	if (version > 1 && version < 4)
  		intel_pmu_legacy_freezing_lbrs_on_pmi(vcpu);
+	else if (version >= 4)
+		intel_pmu_streamlined_freezing_lbrs_on_pmi(vcpu);
  }
 
  static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu, bool set)
arch/x86/kvm/vmx/vmx.c +7 -0
index 565df8eeb78b..66e4cf714dbd 100644
@@ -2113,6 +2113,13 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  		break;
  	case MSR_IA32_DEBUGCTLMSR:
  		msr_info->data = vmcs_read64(GUEST_IA32_DEBUGCTL);
+		/*
+		 * In streamlined Freeze_LBR_On_PMI, guest should see LBR_EN.
+		 */
+		if ((vcpu_to_pmu(vcpu)->global_status &
+		     MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE) &&
+		    vcpu_to_pmu(vcpu)->version >= 4)
+			msr_info->data |= DEBUGCTLMSR_LBR;
  		break;
  	default:
  	find_uret_msr:
#7 lore raw

RE: [PATCH v2 3/9] KVM: x85/pmu: Add Streamlined FREEZE_LBR_ON_PMI for vPMU v4

Quoted 122 lines · Xiong Zhang
Quoted 38 lines

Arch PMU version 4 adds a streamlined FREEZE_LBR_ON_PMI feature, this feature adds LBR_FRZ[bit 58] into IA32_PERF_GLOBAL_STATUS, this bit is set due to the following conditions:
-- IA32_DEBUGCTL.FREEZE_LBR_ON_PMI has been set
-- A performance counter, configured to generate PMI, has overflowed to signal a PMI. Consequently the LBR stack is frozen. Effectively, this bit also serves as a control to enabled capturing data in the LBR stack. When this bit is set, LBR stack is frozen, and new LBR records won't be filled.

The sequence of streamlined freeze LBR is:
1. Profiling agent set IA32_DEBUGCTL.FREEZE_LBR_ON_PMI, and enable a performance counter to generate PMI on overflow.
2. Processor generates PMI and sets IA32_PERF_GLOBAL_STATUS.LBR_FRZ, then LBR stack is forzen.
3. Profiling agent PMI handler handles overflow, and clears IA32_PERF_GLOBAL_STATUS.
4. When IA32_PERF_GLOBAL_STATUS.LBR_FRZ is cleared in step 3, processor resume LBR stack, and new LBR records can be filled again.

In order to emulate this behavior, LBR stack must be frozen on guest vPMI. KVM has two choice to do this:
1. KVM stops vLBR event through perf_event_pause(), and put vLBR event into off state, then vLBR lose LBR hw resource, finally guest couldn't read LBR records in guest PMI handler. This choice couldn't be used.
2. KVM clears guest DEBUGCTLMSR_LBR bit in VMCS on PMI, so when guest is running, LBR HW stack is disabled, while vLBR event is still active and own LBR HW, so guest could still read LBR records in guest PMI handler. But the sequence of streamlined freeze LBR doesn't clear DEBUGCTLMSR_LBR bit, so when guest read guest DEBUGCTL_MSR, KVM will return a value with DEBUGCTLMSR_LBR bit set during LBR freezing. Once guest clears IA32_PERF_GLOBAL_STATUS.LBR_FRZ in step 4, KVM will re-enable guest LBR through setting guest DEBUGCTL_LBR bit in VMCS.

As KVM needs re-enable guest LBR when guest clears global status, the handling of GLOBAL_OVF_CTRL MSR is moved from common pmu.c into vmx/pmu_intel.c.

Quoted 6 lines
---
  arch/x86/include/asm/msr-index.h |  1 +
  arch/x86/kvm/pmu.c               |  8 ------
  arch/x86/kvm/vmx/pmu_intel.c     | 49

++++++++++++++++++++++++++++++++

arch/x86/include/asm/msr-index.h line 1056

Quoted 12 lines
  arch/x86/kvm/vmx/vmx.c           |  7 +++++
  4 files changed, 57 insertions(+), 8 deletions(-)
arch/x86/include/asm/msr-index.h +0 -0
index 1d111350197f..badc2f729a8e 100644
@@ -1054,6 +1054,7 @@
  /* PERF_GLOBAL_OVF_CTL bits */
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT	55
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI

(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)

arch/x86/include/asm/msr-index.h line 1057

Quoted 1 line
arch/x86/include/asm/msr-index.h +1 -0
+#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE
	BIT_ULL(58)

arch/x86/include/asm/msr-index.h line 1059

Quoted 2 lines
arch/x86/include/asm/msr-index.h +0 -0
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT		62
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF

(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)

arch/x86/include/asm/msr-index.h line 1060

Quoted 1 line
arch/x86/include/asm/msr-index.h +0 -0
  #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT
	63

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct

Quoted 5 lines
arch/x86/kvm/pmu.c index renamed from arch/x86/kvm/pmu.c +0 -0

edb89b51b383..4b6a508f3f0b 100644

arch/x86/kvm/pmu.c +0 -0
@@ -640,14 +640,6 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct

msr_data *msr_info)

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

Quoted 6 lines
arch/x86/kvm/pmu.c +0 -3
  			reprogram_counters(pmu, diff);
  		}
  		break;
-	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
-		/*
-		 * GLOBAL_OVF_CTRL, a.k.a. GLOBAL STATUS_RESET, clears

bits in

Quoted 13 lines
arch/x86/kvm/pmu.c +0 -5
-		 * GLOBAL_STATUS, and so the set of reserved bits is the same.
-		 */
-		if (data & pmu->global_status_mask)
-			return 1;
-		fallthrough;
  	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
  		if (!msr_info->host_initiated)
  			pmu->global_status &= ~data;
(quoted diff) +0 -0

b/arch/x86/kvm/vmx/pmu_intel.c index c8d46c3d1ab6..6e3bbe777bf5 100644

arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu · line 432

Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +3 -0
@@ -426,6 +426,34 @@ static int intel_pmu_set_msr(struct kvm_vcpu
 *vcpu, struct msr_data *msr_info)
 
  		pmu->pebs_data_cfg = data;
  		break;
+	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+		/*
+		 * GLOBAL_OVF_CTRL, a.k.a. GLOBAL STATUS_RESET, clears

bits in

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

Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +7 -0
+		 * GLOBAL_STATUS, and so the set of reserved bits is the same.
+		 */
+		if (data & pmu->global_status_mask)
+			return 1;
+
+		/*
+		 * Guest clears GLOBAL_STATUS_LBR_FREEZE bit, KVM re-

enable

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

Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +5 -0
+		 * Guset LBR which is disabled at vPMI injection for streamlined
+		 * Freeze_LBR_On_PMI.
+		 */
+		if (pmu->version >= 4 && !msr_info->host_initiated &&
+		    (data & MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE)

&&

arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu · line 445

Quoted 1 line
arch/x86/kvm/vmx/pmu_intel.c +1 -0
+		    (pmu->global_status &

MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE)) {

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

Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +5 -0
+			u64 debug_ctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+
+			if (!(debug_ctl & DEBUGCTLMSR_LBR)) {
+				debug_ctl |= DEBUGCTLMSR_LBR;
+				vmcs_write64(GUEST_IA32_DEBUGCTL,

debug_ctl);

Quoted 7 lines

arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu · line 451

Quoted 1 line
arch/x86/kvm/vmx/pmu_intel.c +1 -0
+			}

I'm thinking if we can enable guest LBR directly here. I'm afraid that some guest handlers may not follow the SDM strictly and they may disable LBR before writing GLOBAL_OVF_CTRL in guest PMI handler, then we should not enable LBR directly here.

Yes, guest LBR can not be enabled in this case. Current code use guest DEBUGCTLMSR trap both guest itself and kvm emulated lbr state, it is hard to distinguish the lbr is disabled / enabled by guest itself or kvm emulation, I will add emulated_lbr_state variable to track kvm LBR emulation in next version.

thanks

Quoted 80 lines
Quoted 11 lines
arch/x86/kvm/vmx/pmu_intel.c +6 -0
+			vcpu_to_lbr_desc(vcpu)->state = LBR_STATE_IN_USE;
+		}
+
+		if (!msr_info->host_initiated)
+			pmu->global_status &= ~data;
+		break;
  	default:
  		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||

(pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) { @@ -

565,6

Quoted 4 lines

+593,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
if (vmx_pt_mode_is_host_guest())
pmu->global_status_mask &=

	~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;

arch/x86/kvm/vmx/pmu_intel.c static void intel_pmu_refresh(struct kvm_vcpu *vcpu)

Quoted 3 lines
arch/x86/kvm/vmx/pmu_intel.c +3 -0
+	if (pmu->version >= 4)
+		pmu->global_status_mask &=
+
	~MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE;

arch/x86/kvm/vmx/pmu_intel.c static void

Quoted 4 lines
  	entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
  	if (entry &&
arch/x86/kvm/vmx/pmu_intel.c +0 -0
@@ -675,6 +706,22 @@ static void

intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu)

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +3 -0
  	}
  }
 
+static void intel_pmu_streamlined_freezing_lbrs_on_pmi(struct
+kvm_vcpu *vcpu) {
+	u64 data = vmcs_read64(GUEST_IA32_DEBUGCTL);

arch/x86/kvm/vmx/pmu_intel.c static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu)

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +10 -0
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+
+	/*
+	 * Even if streamlined freezing LBR won't clear LBR_EN like legacy
+	 * freezing LBR, here legacy freezing LBR is called to freeze LBR HW
+	 * for streamlined freezing LBR when guest run. But guest VM will
+	 * see a fake guest DEBUGCTL MSR with LBR_EN bit set.
+	 */
+	intel_pmu_legacy_freezing_lbrs_on_pmi(vcpu);
+	if ((data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) && (data &

DEBUGCTLMSR_LBR))

arch/x86/kvm/vmx/pmu_intel.c static void · line 722

Quoted 1 line
arch/x86/kvm/vmx/pmu_intel.c +1 -0
+		pmu->global_status |=

MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE;

arch/x86/kvm/vmx/pmu_intel.c static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu)

Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +2 -0
+}
+
  static void intel_pmu_deliver_pmi(struct kvm_vcpu *vcpu)
  {
  	u8 version = vcpu_to_pmu(vcpu)->version; @@ -684,6 +731,8 @@

static

arch/x86/kvm/vmx/vmx.c static int vmx_get_msr(struct kvm_vcpu *vcpu,

Quoted 14 lines

void intel_pmu_deliver_pmi(struct kvm_vcpu *vcpu)

  	if (version > 1 && version < 4)
  		intel_pmu_legacy_freezing_lbrs_on_pmi(vcpu);
arch/x86/kvm/vmx/pmu_intel.c +2 -0
+	else if (version >= 4)
+		intel_pmu_streamlined_freezing_lbrs_on_pmi(vcpu);
  }
 
  static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu,

bool set) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 565df8eeb78b..66e4cf714dbd 100644

arch/x86/kvm/vmx/vmx.c +0 -0
@@ -2113,6 +2113,13 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu,

struct msr_data *msr_info)

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

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2 -0
  		break;
  	case MSR_IA32_DEBUGCTLMSR:
  		msr_info->data = vmcs_read64(GUEST_IA32_DEBUGCTL);
+		/*
+		 * In streamlined Freeze_LBR_On_PMI, guest should see

LBR_EN.

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +5 -0
+		 */
+		if ((vcpu_to_pmu(vcpu)->global_status &
+		     MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE) &&
+		    vcpu_to_pmu(vcpu)->version >= 4)
+			msr_info->data |= DEBUGCTLMSR_LBR;
  		break;
  	default:
  	find_uret_msr:
#8 lore raw

PATCH v2 4/9 KVM: x86/pmu: Add PERF_GLOBAL_STATUS_SET MSR emulation

In reply to Xiong Zhang

The IA32_PERF_GLOBAL_STATUS_SET MSR is introduced with arch PMU version 4. It allows software to set individual bits in IA32_PERF_GLOBAL_STATUS MSR. It can be used by a VMM to virtualize the state of IA32_PERF_GLOBAL_STATUS across VMS.

If the running VM owns the whole PMU, different VM will have different perf global status, VMM needs to restore IA32_PERF_GLOBAL_STATUS MSR at VM switch, but IA32_PERF_GLOBAL_STATUS MSR is read only, so VMM can use IA32_PERF_GLOBAL_STATUS_SET MSR to restore PERF_GLOBAL_STATUS MSR.

This commit adds this MSR emulation, so that L1 VMM could use it. As it is mainly used by VMM to restore VM's PERF_GLOBAL_STATUS MSR during VM switch, it doesn't need to inject PMI or FREEZE_LBR when VMM write it.

---
Changelog:
v1-v2: Add it into msrs_to_save_pmu[]

---
 arch/x86/include/asm/msr-index.h |  1 +
 arch/x86/kvm/vmx/pmu_intel.c     | 16 ++++++++++++++++
 arch/x86/kvm/x86.c               |  1 +
 3 files changed, 18 insertions(+)
arch/x86/include/asm/msr-index.h +1 -0
index badc2f729a8e..50d231f76003 100644
@@ -1048,6 +1048,7 @@
 #define MSR_CORE_PERF_GLOBAL_STATUS	0x0000038e
 #define MSR_CORE_PERF_GLOBAL_CTRL	0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL	0x00000390
+#define MSR_CORE_PERF_GLOBAL_STATUS_SET 0x00000391
 
 #define MSR_PERF_METRICS		0x00000329
 
arch/x86/kvm/vmx/pmu_intel.c +16 -0
index 6e3bbe777bf5..957663b403f2 100644
@@ -206,6 +206,8 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	switch (msr) {
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
 		return kvm_pmu_has_perf_global_ctrl(pmu);
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		return vcpu_to_pmu(vcpu)->version >= 4;
 	case MSR_IA32_PEBS_ENABLE:
 		ret = vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT;
 		break;
@@ -355,6 +357,9 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
 		msr_info->data = pmu->fixed_ctr_ctrl;
 		break;
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		msr_info->data = 0;
+		break;
 	case MSR_IA32_PEBS_ENABLE:
 		msr_info->data = pmu->pebs_enable;
 		break;
@@ -454,6 +459,17 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (!msr_info->host_initiated)
 			pmu->global_status &= ~data;
 		break;
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		/*
+		 * GLOBAL STATUS_SET, sets bits in GLOBAL_STATUS, so the
+		 * set of reserved bits are the same.
+		 */
+		if (data & pmu->global_status_mask)
+			return 1;
+
+		if (!msr_info->host_initiated)
+			pmu->global_status |= data;
+		break;
 	default:
 		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
 		    (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {
arch/x86/kvm/x86.c +1 -0
index 6c9c81e82e65..aae60461d0d9 100644
@@ -1471,6 +1471,7 @@ static const u32 msrs_to_save_pmu[] = {
 	MSR_ARCH_PERFMON_FIXED_CTR0 + 2,
 	MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
 	MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
+	MSR_CORE_PERF_GLOBAL_STATUS_SET,
 	MSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG,
 
 	/* This part of MSRs should match KVM_INTEL_PMC_MAX_GENERIC. */
Signature · 1 line
2.34.1
#9 lore raw

PATCH v2 5/9 KVM: x86/pmu: Add MSR_CORE_PERF_GLOBAL_INUSE emulation

In reply to Xiong Zhang

Arch PMU v4 introduces a new MSR, MSR_CORE_PERF_GLOBAL_INUSE. It provides as "InUse" bit for each GP counter and fixed counter in processor. Additionally PMI InUse[bit 63] indicates if the PMI mechanism has been configured.

Each bit's definition references Architectural Performance Monitoring Version 4 section of SDM.

---
ChangeLog:
v1->v2: Check INUSE_PMI bit before writing, Add this new MSR into msrs_to_save_pmu[]

---
 arch/x86/include/asm/msr-index.h |  4 ++
 arch/x86/kvm/vmx/pmu_intel.c     | 63 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c               |  2 +-
 3 files changed, 68 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +4 -0
index 50d231f76003..1879046ad0cb 100644
@@ -1049,6 +1049,7 @@
 #define MSR_CORE_PERF_GLOBAL_CTRL	0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL	0x00000390
 #define MSR_CORE_PERF_GLOBAL_STATUS_SET 0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE	0x00000392
 
 #define MSR_PERF_METRICS		0x00000329
 
@@ -1061,6 +1062,9 @@
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT		63
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD			(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT)
 
+/* PERF_GLOBAL_INUSE bits */
+#define MSR_CORE_PERF_GLOBAL_INUSE_PMI				BIT_ULL(63)
+
 /* Geode defined MSRs */
 #define MSR_GEODE_BUSCONT_CONF0		0x00001900
 
arch/x86/kvm/vmx/pmu_intel.c +63 -0

87 lines not on this page · raw message

arch/x86/kvm/x86.c +1 -1
index aae60461d0d9..4e70588e4355 100644
@@ -1471,7 +1471,7 @@ static const u32 msrs_to_save_pmu[] = {
 	MSR_ARCH_PERFMON_FIXED_CTR0 + 2,
 	MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
 	MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
-	MSR_CORE_PERF_GLOBAL_STATUS_SET,
+	MSR_CORE_PERF_GLOBAL_STATUS_SET, MSR_CORE_PERF_GLOBAL_INUSE,
 	MSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG,
 
 	/* This part of MSRs should match KVM_INTEL_PMC_MAX_GENERIC. */
Signature · 1 line
2.34.1
#10 lore raw

PATCH v2 6/9 KVM: x86/pmu: Add Intel PMU supported fixed counters mask

In reply to Xiong Zhang

From: Like Xu <[email protected]>

Per Intel SDM, fixed-function performance counter 'i' is supported:

	FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i);

if pmu.version >=5, ECX is supported fixed counters bit mask. if 1 < pmu.version < 5, EDX[4:0] is number of contiguous fixed-function performance counters starting from 0.

which means that the KVM user space can use EDX to limit the number of fixed counters starting from 0 and at the same time, using ECX to enable part of other KVM supported fixed counters. i.e: pmu.version = 5, ECX= 0x5, EDX[4:0]=1, FxCtrl[2, 0] are supported, FxCtrl[1] isn't supported.

Add Fixed counter bit mask into all_valid_pmc_idx, and use it to perform the semantic checks.

Since fixed counter may be non-continuous, nr_arch_fixed_counters can not be used to enumerate fixed counters, for_each_set_bit_from() is used to enumerate fixed counters, and nr_arch_fixed_counters is deleted.

---
 arch/x86/include/asm/kvm_host.h |  1 -
 arch/x86/kvm/pmu.h              | 12 +++++-
 arch/x86/kvm/svm/pmu.c          |  1 -
 arch/x86/kvm/vmx/pmu_intel.c    | 69 ++++++++++++++++++++-------------
 4 files changed, 52 insertions(+), 31 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
index 1a4def36d5bb..50d2e51bd143 100644
@@ -515,7 +515,6 @@ struct kvm_pmc {
 struct kvm_pmu {
 	u8 version;
 	unsigned nr_arch_gp_counters;
-	unsigned nr_arch_fixed_counters;
 	unsigned available_event_types;
 	u64 fixed_ctr_ctrl;
 	u64 fixed_ctr_ctrl_mask;
arch/x86/kvm/pmu.h +10 -2
index 7d9ba301c090..4bab4819ea6c 100644
@@ -125,14 +125,22 @@ static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
 	return NULL;
 }
 
+static inline bool fixed_ctr_is_supported(struct kvm_pmu *pmu, unsigned int idx)
+{
+	return test_bit(INTEL_PMC_IDX_FIXED + idx, pmu->all_valid_pmc_idx);
+}
+
 /* returns fixed PMC with the specified MSR */
 static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
 {
 	int base = MSR_CORE_PERF_FIXED_CTR0;
 
-	if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) {
+	if (msr >= base && msr < base + KVM_PMC_MAX_FIXED) {
 		u32 index = array_index_nospec(msr - base,
-					       pmu->nr_arch_fixed_counters);
+					       KVM_PMC_MAX_FIXED);
+
+		if (!fixed_ctr_is_supported(pmu, index))
+			return NULL;
 
 		return &pmu->fixed_counters[index];
 	}
arch/x86/kvm/svm/pmu.c +0 -1
index cef5a3d0abd0..d0a12e739989 100644
@@ -213,7 +213,6 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->raw_event_mask = AMD64_RAW_EVENT_MASK;
 	/* not applicable to AMD; but clean them to prevent any fall out */
 	pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
-	pmu->nr_arch_fixed_counters = 0;
 	bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters);
 }
 
arch/x86/kvm/vmx/pmu_intel.c +42 -27

155 lines not on this page · raw message

Signature · 1 line
2.34.1
#11 lore raw

PATCH v2 7/9 KVM: x86/pmu: Add fixed counter enumeration for pmu v5

In reply to Xiong Zhang

With Arch PMU v5, CPUID.0AH.ECX is a bit mask which enumerates the supported Fixed Counters. If bit 'i' is set, it implies that Fixed Counter 'i' is supported.

This commit add CPUID.0AH.ECX emulation for vPMU version 5, KVM support Fixed Counter enumeration starting from 0 by default, user could modify it through SET_CPUID2 ioctl.

When perf driver supports fixed counter bitmap and exports it, KVM will get it from perf driver, and set it as
KVM supported CPUID.0AH.ECX.

---
 arch/x86/kvm/cpuid.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +9 -1
index 0544e30b4946..e10844e2f211 100644
@@ -1004,7 +1004,15 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 
 		entry->eax = eax.full;
 		entry->ebx = kvm_pmu_cap.events_mask;
-		entry->ecx = 0;
+		if (kvm_pmu_cap.version < 5)
+			entry->ecx = 0;
+		else
+			/*
+			 * Todo: When perf driver supports Fixed counter bitmap
+			 * and export it, KVM will get it from perf driver and set
+			 * it into entry->ecx.
+			 */
+			entry->ecx = (1ULL << kvm_pmu_cap.num_counters_fixed) - 1;
 		entry->edx = edx.full;
 		break;
 	}
Signature · 1 line
2.34.1
#12 lore raw

PATCH v2 8/9 KVM: x86/pmu: Upgrade pmu version to 5 on intel processor

In reply to Xiong Zhang

Modern intel processors have supported Architectural Performance Monitoring Version 5, this commit upgrade Intel vcpu's vPMU version from 2 to 5.

Go through PMU features from version 3 to 5, the following features are not supported:
1. AnyThread counting: it is added in v3, and deprecated in v5.
2. Streamed Freeze_PerfMon_On_PMI in v4, since legacy Freeze_PerMon_ON_PMI isn't supported, the new one won't be supported neither.
3. IA32_PERF_GLOBAL_STATUS.ASCI[bit 60]: Related to SGX, and will be emulated by SGX developer later.
4. Domain Separation in v5. When INV flag in IA32_PERFEVTSELx is used, a counter stops counting when logical processor exits the C0 ACPI C-state. KVM has little information about guest ACPI C-state.

When guest enable unsupported features through WRMSR, KVM will inject a #GP into guest.

---
Changelog:
v1->v2: kvm_pmu_cap.version will not exceed 2 for AMD, no need to differentiate between Intel and AMD.

---
 arch/x86/kvm/pmu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/pmu.h +1 -1
index 4bab4819ea6c..b416094917fd 100644
@@ -215,7 +215,7 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
 		return;
 	}
 
-	kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
+	kvm_pmu_cap.version = min(kvm_pmu_cap.version, 5);
 	kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
 					  pmu_ops->MAX_NR_GP_COUNTERS);
 	kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
Signature · 1 line
2.34.1
#13 lore raw

PATCH v2 9/9 KVM: selftests: Add fixed counters enumeration test case

In reply to Xiong Zhang

vPMU v5 adds fixed counter enumeration, which allows user space to specify which fixed counters are supported through emulated CPUID.0Ah.ECX.

This commit adds a test case which specify the max fixed counter supported only, so guest can access the max fixed counter only, #GP exception will be happen once guest access other fixed counters.

---
 .../selftests/kvm/x86_64/vmx_pmu_caps_test.c  | 79 +++++++++++++++++++
 1 file changed, 79 insertions(+)
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +79 -0
index ebbcb0a3f743..c690cb389ae2 100644
@@ -18,6 +18,8 @@
 #include "kvm_util.h"
 #include "vmx.h"
 
+uint8_t fixed_counter_num;
+
 union perf_capabilities {
 	struct {
 		u64	lbr_format:6;
@@ -233,6 +235,81 @@ static void test_lbr_perf_capabilities(union perf_capabilities host_cap)
 	kvm_vm_free(vm);
 }
 
+static void guest_v5_code(void)
+{
+	uint8_t  vector, i;
+	uint64_t val;
+
+	for (i = 0; i < fixed_counter_num; i++) {
+		vector = rdmsr_safe(MSR_CORE_PERF_FIXED_CTR0 + i, &val);
+
+		/*
+		 * Only the max fixed counter is supported, #GP will be generated
+		 * when guest access other fixed counters.
+		 */
+		if (i == fixed_counter_num - 1)
+			__GUEST_ASSERT(vector != GP_VECTOR,
+				       "Max Fixed counter is accessible, but get #GP");
+		else
+			__GUEST_ASSERT(vector == GP_VECTOR,
+				       "Fixed counter isn't accessible, but access is ok");
+	}
+
+	GUEST_DONE();
+}
+
+#define PMU_NR_FIXED_COUNTERS_MASK  0x1f
+
+static void test_fixed_counter_enumeration(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	int r;
+	struct kvm_cpuid_entry2 *ent;
+	struct ucall uc;
+	uint32_t fixed_counter_bit_mask;
+
+	if (kvm_cpu_property(X86_PROPERTY_PMU_VERSION) < 5)
+		return;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_v5_code);
+	vm_init_descriptor_tables(vm);
+	vcpu_init_descriptor_tables(vcpu);
+
+	ent = vcpu_get_cpuid_entry(vcpu, 0xa);
+	fixed_counter_num = ent->edx & PMU_NR_FIXED_COUNTERS_MASK;
+	TEST_ASSERT(fixed_counter_num > 0, "fixed counter isn't supported");
+	fixed_counter_bit_mask = (1ul << fixed_counter_num) - 1;
+	TEST_ASSERT(ent->ecx == fixed_counter_bit_mask,
+		    "cpuid.0xa.ecx != %x", fixed_counter_bit_mask);
+
+	if (fixed_counter_num == 1) {
+		kvm_vm_free(vm);
+		return;
+	}
+
+	/* Support the max Fixed Counter only */
+	ent->ecx = 1UL << (fixed_counter_num - 1);
+	ent->edx &= ~(u32)PMU_NR_FIXED_COUNTERS_MASK;
+
+	r = __vcpu_set_cpuid(vcpu);
+	TEST_ASSERT(!r, "Setting modified cpuid.0xa.ecx and edx failed");
+
+	vcpu_run(vcpu);
+
+	switch (get_ucall(vcpu, &uc)) {
+	case UCALL_ABORT:
+		REPORT_GUEST_ASSERT(uc);
+		break;
+	case UCALL_DONE:
+		break;
+	default:
+		TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
+	}
+
+	kvm_vm_free(vm);
+}
+
 int main(int argc, char *argv[])
 {
 	union perf_capabilities host_cap;
@@ -253,4 +330,6 @@ int main(int argc, char *argv[])
 	test_immutable_perf_capabilities(host_cap);
 	test_guest_wrmsr_perf_capabilities(host_cap);
 	test_lbr_perf_capabilities(host_cap);
+
+	test_fixed_counter_enumeration();
 }
Signature · 1 line
2.34.1
#14 lore raw
In reply to Xiong Zhang

On Thu, 21 Sep 2023 at 16:29:48 +0800, [email protected] wrote:> Intel recent processors have supported Architectural Performance

Quoted 3 lines

Monitoring Version 5, while Intel vPMU still keeps on version 2. In order to use new PMU features introduced in version 4 and 5, this patchset upgrades vPMU version to 5.

What happened to this series?

Amazon Development Centre (London) Ltd. Registered in England and Wales with registration number 04543232 with its registered office at 1 Principal Place, Worship Street, London EC2A 2FA, United Kingdom.

#15 lore raw
Quoted 5 lines · David

On Thu, 21 Sep 2023 at 16:29:48 +0800, [email protected] wrote:> Intel recent processors have supported Architectural Performance

Quoted 3 lines

Monitoring Version 5, while Intel vPMU still keeps on version 2. In order to use new PMU features introduced in version 4 and 5, this patchset upgrades vPMU version to 5.

What happened to this series?

This implementation base on the legacy perf-based emulated vPMU has been dropped. The support for higher PMU verion (5/6) would be re-implemented base on the new mediated vPMU framework. Thanks.

Quoted 9 lines

Amazon Development Centre (London) Ltd.Registered in England and Wales with registration number 04543232 with its registered office at 1 Principal Place, Worship Street, London EC2A 2FA, United Kingdom.

#16 lore raw
Quoted 10 lines · Dapeng
Quoted 5 lines · David

On Thu, 21 Sep 2023 at 16:29:48 +0800, [email protected] wrote:> Intel recent processors have supported Architectural Performance

Quoted 3 lines

Monitoring Version 5, while Intel vPMU still keeps on version 2. In order to use new PMU features introduced in version 4 and 5, this patchset upgrades vPMU version to 5.

What happened to this series?

This implementation base on the legacy perf-based emulated vPMU has been dropped. The support for higher PMU verion (5/6) would be re-implemented base on the new mediated vPMU framework. Thanks.

Thanks. A bit like this?

https://git.infradead.org/?p=users/dwmw2/linux.git;a=shortlog;h=refs/heads/pmuv5

David Woodhouse (1):
KVM: selftests: Add tests for PMU v4 GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Like Xu (1):
KVM: x86/pmu: Add Intel PMU supported fixed counters mask

Xiong Zhang (7):
KVM: x86/pmu: Delay vLBR release to the vcpu next sched-in
KVM: x86/pmu: Don't release vLBR caused by vPMI
KVM: x86/pmu: Add Streamlined FREEZE_LBR_ON_PMI for vPMU v4
KVM: x86/pmu: Add PERF_GLOBAL_STATUS_SET MSR emulation
KVM: x86/pmu: Add MSR_CORE_PERF_GLOBAL_INUSE emulation
KVM: x86/pmu: Add fixed counter enumeration for pmu v5
KVM: x86/pmu: Upgrade pmu version to 5 on intel processor

 arch/x86/include/asm/msr-index.h                   |   5 +
 arch/x86/kvm/cpuid.c                               |   5 +-
 arch/x86/kvm/pmu.c                                 |  14 +-
 arch/x86/kvm/pmu.h                                 |  17 ++-
 arch/x86/kvm/svm/pmu.c                             |   1 +
 arch/x86/kvm/vmx/pmu_intel.c                       | 161 +++++++++++++++++++--
 arch/x86/kvm/vmx/pmu_intel.h                       |   9 ++
 arch/x86/kvm/vmx/vmx.c                             |  19 ++-
 arch/x86/kvm/x86.c                                 |   3 +-
 tools/arch/x86/include/asm/msr-index.h             |   6 +
 .../testing/selftests/kvm/x86/pmu_counters_test.c  |  92 ++++++++++++
 11 files changed, 301 insertions(+), 31 deletions(-)
#17 lore raw
Quoted 6 lines · David Woodhouse
Quoted 5 lines · Dapeng
Quoted 4 lines · David

On Thu, 21 Sep 2023 at 16:29:48 +0800, [email protected] wrote:> Intel recent processors have supported Architectural Performance

Quoted 3 lines

Monitoring Version 5, while Intel vPMU still keeps on version 2. In order to use new PMU features introduced in version 4 and 5, this patchset upgrades vPMU version to 5.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

What happened to this series?

This implementation base on the legacy perf-based emulated vPMU has been dropped. The support for higher PMU verion (5/6) would be re-implemented base on the new mediated vPMU framework. Thanks.

Thanks. A bit like this?

https://git.infradead.org/?p=users/dwmw2/linux.git;a=shortlog;h=refs/heads/pmuv5

Just a little, only a little bit code would be reused and most of code would be re-implemented base on the mediated vPMU. Thanks.

Quoted 32 lines

David Woodhouse (1):
KVM: selftests: Add tests for PMU v4 GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Like Xu (1):
KVM: x86/pmu: Add Intel PMU supported fixed counters mask

Xiong Zhang (7):
KVM: x86/pmu: Delay vLBR release to the vcpu next sched-in
KVM: x86/pmu: Don't release vLBR caused by vPMI
KVM: x86/pmu: Add Streamlined FREEZE_LBR_ON_PMI for vPMU v4
KVM: x86/pmu: Add PERF_GLOBAL_STATUS_SET MSR emulation
KVM: x86/pmu: Add MSR_CORE_PERF_GLOBAL_INUSE emulation
KVM: x86/pmu: Add fixed counter enumeration for pmu v5
KVM: x86/pmu: Upgrade pmu version to 5 on intel processor

 arch/x86/include/asm/msr-index.h                   |   5 +
 arch/x86/kvm/cpuid.c                               |   5 +-
 arch/x86/kvm/pmu.c                                 |  14 +-
 arch/x86/kvm/pmu.h                                 |  17 ++-
 arch/x86/kvm/svm/pmu.c                             |   1 +
 arch/x86/kvm/vmx/pmu_intel.c                       | 161 +++++++++++++++++++--
 arch/x86/kvm/vmx/pmu_intel.h                       |   9 ++
 arch/x86/kvm/vmx/vmx.c                             |  19 ++-
 arch/x86/kvm/x86.c                                 |   3 +-
 tools/arch/x86/include/asm/msr-index.h             |   6 +
 .../testing/selftests/kvm/x86/pmu_counters_test.c  |  92 ++++++++++++
 11 files changed, 301 insertions(+), 31 deletions(-)