Skip to contentSkip to conversation

KVM: x86/pmu: Add support for AMD Host-Only/Guest-Only bits

PATCH In review Jim Mattson 3 participants 9 messages Feb 7 – Mar 5

Subsystems (5)
x86 KVM Kselftest perf events +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover KVM: x86/pmu: Add support for AMD Host-Only/Guest-Only bits

This series adds support for AMD's Host-Only and Guest-Only performance counter eventsel bits in KVM's mediated PMU passthrough implementation.

These bits allow an nSVM-enabled guest to configure performance counters that count only during L1 execution (Host-Only) or only during L2 execution (Guest-Only).

KVM updates the hardware event selector ENABLE bit at the following state transitions to ensure counters only count in the appropriate mode:

- EFER.SVME changes: Enable/disable Guest-Only counters
- Nested VMRUN: Disable Host-Only, enable Guest-Only counters
- Nested VMEXIT: Enable Host-Only, disable Guest-Only counters

v1: https://lore.kernel.org/kvm/[email protected]/

v2: https://lore.kernel.org/kvm/[email protected]/

v2 -> v3:
* Dropped the svm_enter_guest_mode() and svm_leave_guest_mode() wrappers introduced in v2 [Yosry]
* Introduced a generic nested_transition callback in kvm_x86_ops to avoid confusing SVM-specific wrappers and unnecessary code replication
* Fixed a latent bug with L2 stack alignment, which was triggered by a movdqa instruction in l2_guest_code() that referenced the L2 stack. Note that l2_guest_code() expects the stack to be 16-byte misaligned at function entry. It was not.

Jim Mattson (5):
KVM: x86/pmu: Introduce amd_pmu_set_eventsel_hw()
KVM: x86/pmu: Disable Host-Only/Guest-Only events as appropriate for
vCPU state
KVM: x86/pmu: Refresh Host-Only/Guest-Only eventsel at nested
transitions
KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated
PMU
KVM: selftests: x86: Add svm_pmu_host_guest_test for
Host-Only/Guest-Only bits

 arch/x86/include/asm/kvm-x86-ops.h            |   1 +
 arch/x86/include/asm/kvm_host.h               |   2 +
 arch/x86/include/asm/perf_event.h             |   2 +
 arch/x86/kvm/kvm_cache_regs.h                 |   2 +
 arch/x86/kvm/svm/pmu.c                        |  42 +++-
 arch/x86/kvm/svm/svm.c                        |   3 +
 arch/x86/kvm/svm/svm.h                        |   5 +
 arch/x86/kvm/x86.c                            |   1 +
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 tools/testing/selftests/kvm/include/x86/pmu.h |   6 +
 .../selftests/kvm/include/x86/processor.h     |   2 +
 .../kvm/x86/svm_pmu_host_guest_test.c         | 199 ++++++++++++++++++
 12 files changed, 264 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
#1 lore raw

PATCH v3 1/5 KVM: x86/pmu: Introduce amd_pmu_set_eventsel_hw()

Extract the computation of eventsel_hw from amd_pmu_set_msr() into a separate helper function, amd_pmu_set_eventsel_hw().

No functional change intended.

---
 arch/x86/kvm/svm/pmu.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/pmu.c +7 -2
index 7aa298eeb072..d9ca633f9f49 100644
@@ -147,6 +147,12 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	return 1;
 }
 
+static void amd_pmu_set_eventsel_hw(struct kvm_pmc *pmc)
+{
+	pmc->eventsel_hw = (pmc->eventsel & ~AMD64_EVENTSEL_HOSTONLY) |
+			   AMD64_EVENTSEL_GUESTONLY;
+}
+
 static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -166,8 +172,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		data &= ~pmu->reserved_bits;
 		if (data != pmc->eventsel) {
 			pmc->eventsel = data;
-			pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY) |
-					   AMD64_EVENTSEL_GUESTONLY;
+			amd_pmu_set_eventsel_hw(pmc);
 			kvm_pmu_request_counter_reprogram(pmc);
 		}
 		return 0;
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
#2 lore raw

PATCH v3 2/5 KVM: x86/pmu: Disable Host-Only/Guest-Only events as appropriate for vCPU state

In reply to Jim Mattson

Update amd_pmu_set_eventsel_hw() to clear the event selector's hardware enable bit when the PMC should not count based on the guest's Host-Only and Guest-Only event selector bits and the current vCPU state.

---
 arch/x86/include/asm/perf_event.h |  2 ++
 arch/x86/kvm/svm/pmu.c            | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)
arch/x86/include/asm/perf_event.h +2 -0
index 0d9af4135e0a..4dfe12053c09 100644
@@ -58,6 +58,8 @@
 #define AMD64_EVENTSEL_INT_CORE_ENABLE			(1ULL << 36)
 #define AMD64_EVENTSEL_GUESTONLY			(1ULL << 40)
 #define AMD64_EVENTSEL_HOSTONLY				(1ULL << 41)
+#define AMD64_EVENTSEL_HOST_GUEST_MASK			\
+	(AMD64_EVENTSEL_HOSTONLY | AMD64_EVENTSEL_GUESTONLY)
 
 #define AMD64_EVENTSEL_INT_CORE_SEL_SHIFT		37
 #define AMD64_EVENTSEL_INT_CORE_SEL_MASK		\
arch/x86/kvm/svm/pmu.c +18 -0
index d9ca633f9f49..8d451110a94d 100644
@@ -149,8 +149,26 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 static void amd_pmu_set_eventsel_hw(struct kvm_pmc *pmc)
 {
+	struct kvm_vcpu *vcpu = pmc->vcpu;
+	u64 host_guest_bits;
+
 	pmc->eventsel_hw = (pmc->eventsel & ~AMD64_EVENTSEL_HOSTONLY) |
 			   AMD64_EVENTSEL_GUESTONLY;
+
+	if (!(pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE))
+		return;
+
+	if (!(vcpu->arch.efer & EFER_SVME))
+		return;
+
+	host_guest_bits = pmc->eventsel & AMD64_EVENTSEL_HOST_GUEST_MASK;
+	if (!host_guest_bits || host_guest_bits == AMD64_EVENTSEL_HOST_GUEST_MASK)
+		return;
+
+	if (!!(host_guest_bits & AMD64_EVENTSEL_GUESTONLY) == is_guest_mode(vcpu))
+		return;
+
+	pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
 }
 
 static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
#3 lore raw

Re: [PATCH v3 2/5] KVM: x86/pmu: Disable Host-Only/Guest-Only events as appropriate for vCPU state

Quoted 39 lines · Jim Mattson

Update amd_pmu_set_eventsel_hw() to clear the event selector's hardware enable bit when the PMC should not count based on the guest's Host-Only and Guest-Only event selector bits and the current vCPU state.

---
 arch/x86/include/asm/perf_event.h |  2 ++
 arch/x86/kvm/svm/pmu.c            | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)
arch/x86/include/asm/perf_event.h +2 -0
index 0d9af4135e0a..4dfe12053c09 100644
@@ -58,6 +58,8 @@
 #define AMD64_EVENTSEL_INT_CORE_ENABLE			(1ULL << 36)
 #define AMD64_EVENTSEL_GUESTONLY			(1ULL << 40)
 #define AMD64_EVENTSEL_HOSTONLY				(1ULL << 41)
+#define AMD64_EVENTSEL_HOST_GUEST_MASK			\
+	(AMD64_EVENTSEL_HOSTONLY | AMD64_EVENTSEL_GUESTONLY)
 
 #define AMD64_EVENTSEL_INT_CORE_SEL_SHIFT		37
 #define AMD64_EVENTSEL_INT_CORE_SEL_MASK		\
arch/x86/kvm/svm/pmu.c +6 -0
index d9ca633f9f49..8d451110a94d 100644
@@ -149,8 +149,26 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 static void amd_pmu_set_eventsel_hw(struct kvm_pmc *pmc)
 {
+	struct kvm_vcpu *vcpu = pmc->vcpu;
+	u64 host_guest_bits;
+
 	pmc->eventsel_hw = (pmc->eventsel & ~AMD64_EVENTSEL_HOSTONLY) |
 			   AMD64_EVENTSEL_GUESTONLY;
+
+	if (!(pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE))
+		return;

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

Quoted 10 lines
arch/x86/kvm/svm/pmu.c +10 -0
+
+	if (!(vcpu->arch.efer & EFER_SVME))
+		return;
+
+	host_guest_bits = pmc->eventsel & AMD64_EVENTSEL_HOST_GUEST_MASK;
+	if (!host_guest_bits || host_guest_bits == AMD64_EVENTSEL_HOST_GUEST_MASK)
+		return;
+
+	if (!!(host_guest_bits & AMD64_EVENTSEL_GUESTONLY) == is_guest_mode(vcpu))
+		return;

This seems to disable the PMCs after exits from an L2 guest to the L0 hypervisor. For such transitions, the corresponding L1 vCPU's PMC has GuestOnly set but is_guest_mode() is false as this function is called at the very end of leave_guest_mode() after vcpu->stat.guest_mode is set to 0.

Is this a correct interpretation of the condition above?

Quoted 5 lines
arch/x86/kvm/svm/pmu.c +2 -0
+
+	pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
 }
 
 static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
#4 lore raw

Re: [PATCH v3 2/5] KVM: x86/pmu: Disable Host-Only/Guest-Only events as appropriate for vCPU state

Quoted 57 lines · Sandipan Das
Quoted 39 lines · Jim Mattson

Update amd_pmu_set_eventsel_hw() to clear the event selector's hardware enable bit when the PMC should not count based on the guest's Host-Only and Guest-Only event selector bits and the current vCPU state.

---
 arch/x86/include/asm/perf_event.h |  2 ++
 arch/x86/kvm/svm/pmu.c            | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)
arch/x86/include/asm/perf_event.h +2 -0
index 0d9af4135e0a..4dfe12053c09 100644
@@ -58,6 +58,8 @@
 #define AMD64_EVENTSEL_INT_CORE_ENABLE                       (1ULL << 36)
 #define AMD64_EVENTSEL_GUESTONLY                     (1ULL << 40)
 #define AMD64_EVENTSEL_HOSTONLY                              (1ULL << 41)
+#define AMD64_EVENTSEL_HOST_GUEST_MASK                       \
+     (AMD64_EVENTSEL_HOSTONLY | AMD64_EVENTSEL_GUESTONLY)
 
 #define AMD64_EVENTSEL_INT_CORE_SEL_SHIFT            37
 #define AMD64_EVENTSEL_INT_CORE_SEL_MASK             \
arch/x86/kvm/svm/pmu.c +6 -0
index d9ca633f9f49..8d451110a94d 100644
@@ -149,8 +149,26 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 static void amd_pmu_set_eventsel_hw(struct kvm_pmc *pmc)
 {
+     struct kvm_vcpu *vcpu = pmc->vcpu;
+     u64 host_guest_bits;
+
      pmc->eventsel_hw = (pmc->eventsel & ~AMD64_EVENTSEL_HOSTONLY) |
                         AMD64_EVENTSEL_GUESTONLY;
+
+     if (!(pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE))
+             return;

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

Quoted 10 lines
arch/x86/kvm/svm/pmu.c +10 -0
+
+     if (!(vcpu->arch.efer & EFER_SVME))
+             return;
+
+     host_guest_bits = pmc->eventsel & AMD64_EVENTSEL_HOST_GUEST_MASK;
+     if (!host_guest_bits || host_guest_bits == AMD64_EVENTSEL_HOST_GUEST_MASK)
+             return;
+
+     if (!!(host_guest_bits & AMD64_EVENTSEL_GUESTONLY) == is_guest_mode(vcpu))
+             return;

This seems to disable the PMCs after exits from an L2 guest to the L0 hypervisor. For such transitions, the corresponding L1 vCPU's PMC has GuestOnly set but is_guest_mode() is false as this function is called at the very end of leave_guest_mode() after vcpu->stat.guest_mode is set to 0.

Quoted 1 line

Is this a correct interpretation of the condition above?

I think you are confusing the VMCB02 bits (that always have Guest-Only set) and the VMCB12 bits (which are under the control of L1.

If L1 sets only Guest-Only, that indicates a desire to monitor only L2 execution. So, yes, after emulating an exit from L2 to L1, the PMC is disabled.

Quoted 6 lines
Quoted 5 lines
arch/x86/kvm/svm/pmu.c +2 -0
+
+     pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
 }
 
 static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
#5 lore raw

PATCH v3 3/5 KVM: x86/pmu: Refresh Host-Only/Guest-Only eventsel at nested transitions

In reply to Jim Mattson

Add amd_pmu_refresh_host_guest_eventsel_hw() to recalculate eventsel_hw for all PMCs based on the current vCPU state. This is needed because Host-Only and Guest-Only counters must be enabled/disabled at:

- SVME changes: When EFER.SVME is modified, counters with Guest-Only bits need their hardware enable state updated.

- Nested transitions: When entering or leaving guest mode, Host-Only counters should be disabled/enabled and Guest-Only counters should be enabled/disabled accordingly.

Add a nested_transition() callback to kvm_x86_ops and call it from enter_guest_mode() and leave_guest_mode() to ensure the PMU state stays synchronized with guest mode transitions.

---
 arch/x86/include/asm/kvm-x86-ops.h |  1 +
 arch/x86/include/asm/kvm_host.h    |  2 ++
 arch/x86/kvm/kvm_cache_regs.h      |  2 ++
 arch/x86/kvm/svm/pmu.c             | 12 ++++++++++++
 arch/x86/kvm/svm/svm.c             |  3 +++
 arch/x86/kvm/svm/svm.h             |  5 +++++
 arch/x86/kvm/x86.c                 |  1 +
 7 files changed, 26 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
index de709fb5bd76..62ac8ecd26e9 100644
@@ -108,6 +108,7 @@ KVM_X86_OP(get_entry_info)
 KVM_X86_OP(check_intercept)
 KVM_X86_OP(handle_exit_irqoff)
 KVM_X86_OP_OPTIONAL(update_cpu_dirty_logging)
+KVM_X86_OP_OPTIONAL(nested_transition)
 KVM_X86_OP_OPTIONAL(vcpu_blocking)
 KVM_X86_OP_OPTIONAL(vcpu_unblocking)
 KVM_X86_OP_OPTIONAL(pi_update_irte)
arch/x86/include/asm/kvm_host.h +2 -0
index ff07c45e3c73..8dbc5c731859 100644
@@ -1901,6 +1901,8 @@ struct kvm_x86_ops {
 
 	void (*update_cpu_dirty_logging)(struct kvm_vcpu *vcpu);
 
+	void (*nested_transition)(struct kvm_vcpu *vcpu);
+
 	const struct kvm_x86_nested_ops *nested_ops;
 
 	void (*vcpu_blocking)(struct kvm_vcpu *vcpu);
arch/x86/kvm/kvm_cache_regs.h +2 -0
index 8ddb01191d6f..14e2cbab8312 100644
@@ -227,6 +227,7 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu)
 {
 	vcpu->arch.hflags |= HF_GUEST_MASK;
 	vcpu->stat.guest_mode = 1;
+	kvm_x86_call(nested_transition)(vcpu);
 }
 
 static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
@@ -239,6 +240,7 @@ static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
 	}
 
 	vcpu->stat.guest_mode = 0;
+	kvm_x86_call(nested_transition)(vcpu);
 }
 
 static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/pmu.c +12 -0
index 8d451110a94d..e2a849fc7daa 100644
@@ -171,6 +171,18 @@ static void amd_pmu_set_eventsel_hw(struct kvm_pmc *pmc)
 	pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
 }
 
+void amd_pmu_refresh_host_guest_eventsel_hw(struct kvm_vcpu *vcpu)
+{
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	int i;
+
+	if (pmu->reserved_bits & AMD64_EVENTSEL_HOST_GUEST_MASK)
+		return;
+
+	for (i = 0; i < pmu->nr_arch_gp_counters; i++)
+		amd_pmu_set_eventsel_hw(&pmu->gp_counters[i]);
+}
+
 static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
arch/x86/kvm/svm/svm.c +3 -0
index 5f0136dbdde6..5753388542cf 100644
@@ -244,6 +244,8 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
 			if (svm_gp_erratum_intercept && !sev_guest(vcpu->kvm))
 				set_exception_intercept(svm, GP_VECTOR);
 		}
+
+		amd_pmu_refresh_host_guest_eventsel_hw(vcpu);
 	}
 
 	svm->vmcb->save.efer = efer | EFER_SVME;
@@ -5222,6 +5224,7 @@ struct kvm_x86_ops svm_x86_ops __initdata = {
 
 	.check_intercept = svm_check_intercept,
 	.handle_exit_irqoff = svm_handle_exit_irqoff,
+	.nested_transition = amd_pmu_refresh_host_guest_eventsel_hw,
 
 	.nested_ops = &svm_nested_ops,
 
arch/x86/kvm/svm/svm.h +5 -0
index ebd7b36b1ceb..c31ef7c46d58 100644
@@ -864,6 +864,11 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
 void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_area *hostsa);
 void sev_es_unmap_ghcb(struct vcpu_svm *svm);
 
+
+/* pmu.c */
+void amd_pmu_refresh_host_guest_eventsel_hw(struct kvm_vcpu *vcpu);
+
+
 #ifdef CONFIG_KVM_AMD_SEV
 int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp);
 int sev_mem_enc_register_region(struct kvm *kvm,
arch/x86/kvm/x86.c +1 -0
index db3f393192d9..01ccbaa5b2e6 100644
@@ -150,6 +150,7 @@ struct kvm_x86_ops kvm_x86_ops __read_mostly;
 #include <asm/kvm-x86-ops.h>
 EXPORT_STATIC_CALL_GPL(kvm_x86_get_cs_db_l_bits);
 EXPORT_STATIC_CALL_GPL(kvm_x86_cache_reg);
+EXPORT_STATIC_CALL_GPL(kvm_x86_nested_transition);
 
 static bool __read_mostly ignore_msrs = 0;
 module_param(ignore_msrs, bool, 0644);
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
#6 lore raw

Re: [PATCH v3 3/5] KVM: x86/pmu: Refresh Host-Only/Guest-Only eventsel at nested transitions

Quoted 7 lines · Jim Mattson

Add amd_pmu_refresh_host_guest_eventsel_hw() to recalculate eventsel_hw for all PMCs based on the current vCPU state. This is needed because Host-Only and Guest-Only counters must be enabled/disabled at:

- SVME changes: When EFER.SVME is modified, counters with Guest-Only bits need their hardware enable state updated.

Quoted 7 lines

- Nested transitions: When entering or leaving guest mode, Host-Only counters should be disabled/enabled and Guest-Only counters should be enabled/disabled accordingly.

Add a nested_transition() callback to kvm_x86_ops and call it from enter_guest_mode() and leave_guest_mode() to ensure the PMU state stays synchronized with guest mode transitions.

Blech, I'm not a fan of this kvm_x86_ops hook. I especially don't like calling out to vendor code from {enter,leave}_guest_mode(). The subtle dependency on vcpu-arch.efer being up-to-date in svm_set_efer() is a little nasty too.

More importantly, I think this series is actively buggy, as I don't see anything in amd_pmu_refresh_host_guest_eventsel_hw() that restricts it to the mediated PMU. And I'm pretty sure that path will bypass the PMU event filter. And I believe kvm_pmu_recalc_pmc_emulation() also needs to be invoked so that emulated instructions are counted correctly.

To avoid ordering issues and bugs where event filtering and guest/host handling clobber each other, I think we should funnel all processing through KVM_REQ_PMU, and then do something like this:

arch/x86/kvm/kvm_cache_regs.h +4 -2
index 14e2cbab8312..a2a9492063f7 100644
@@ -227,7 +227,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu)
 {
        vcpu->arch.hflags |= HF_GUEST_MASK;
        vcpu->stat.guest_mode = 1;
-       kvm_x86_call(nested_transition)(vcpu);
+
+       kvm_pmu_handle_nested_transition();
 }
 
 static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
@@ -240,7 +241,8 @@ static inline void leave_guest_mode(struct kvm_vcpu *vcpu)
        }
 
        vcpu->stat.guest_mode = 0;
-       kvm_x86_call(nested_transition)(vcpu);
+
+       kvm_pmu_handle_nested_transition();
 }
 
 static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
arch/x86/kvm/pmu.h +12 -0
index 0925246731cb..098dae2d45b4 100644
@@ -244,6 +244,18 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu)
                                  X86_PMC_IDX_MAX);
 }
 
+static inline void kvm_pmu_handle_nested_transition(struct kvm_vcpu *vcpu)
+{
+       if (!kvm_vcpu_has_mediated_pmu(vcpu))
+               return;
+
+       if (vcpu_to_pmu(vcpu)->reserved_bits & AMD64_EVENTSEL_HOST_GUEST_MASK)
+               return;
+
+       atomic64_set(&vcpu_to_pmu(vcpu)->__reprogram_pmi, -1ull);
+       kvm_make_request(KVM_REQ_PMU, vcpu);
+}
+
 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);
#7 lore raw

PATCH v3 4/5 KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated PMU

In reply to Jim Mattson

If the vCPU advertises SVM and uses the mediated PMU, allow the guest to set the Host-Only and Guest-Only bits in the event selector MSRs.

---
 arch/x86/kvm/svm/pmu.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/svm/pmu.c +3 -0
index e2a849fc7daa..7de7d8d00427 100644
@@ -243,6 +243,9 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 
 	pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(48) - 1;
 	pmu->reserved_bits = 0xfffffff000280000ull;
+	if (guest_cpu_cap_has(vcpu, X86_FEATURE_SVM) &&
+	    kvm_vcpu_has_mediated_pmu(vcpu))
+		pmu->reserved_bits &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
 	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;
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
#8 lore raw

PATCH v3 5/5 KVM: selftests: x86: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits

In reply to Jim Mattson

Add a selftest to verify KVM correctly virtualizes the AMD PMU Host-Only (bit 41) and Guest-Only (bit 40) event selector bits across all relevant SVM state transitions.

The test programs 4 PMCs simultaneously with all combinations of the Host-Only and Guest-Only bits, then verifies correct counting behavior:
1. SVME=0: all counters count (Host-Only/Guest-Only bits ignored)
2. Set SVME=1: Host-Only and neither/both count; Guest-Only stops
3. VMRUN to L2: Guest-Only and neither/both count; Host-Only stops
4. VMEXIT to L1: Host-Only and neither/both count; Guest-Only stops
5. Clear SVME=0: all counters count (bits ignored again)

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 tools/testing/selftests/kvm/include/x86/pmu.h |   6 +
 .../selftests/kvm/include/x86/processor.h     |   2 +
 .../kvm/x86/svm_pmu_host_guest_test.c         | 199 ++++++++++++++++++
 4 files changed, 208 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 58eee0474db6..f20ddd58ee81 100644
@@ -112,6 +112,7 @@ TEST_GEN_PROGS_x86 += x86/svm_vmcall_test
 TEST_GEN_PROGS_x86 += x86/svm_int_ctl_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test
+TEST_GEN_PROGS_x86 += x86/svm_pmu_host_guest_test
 TEST_GEN_PROGS_x86 += x86/tsc_scaling_sync
 TEST_GEN_PROGS_x86 += x86/sync_regs_test
 TEST_GEN_PROGS_x86 += x86/ucna_injection_test
tools/testing/selftests/kvm/include/x86/pmu.h +6 -0
index 72575eadb63a..af9b279c78df 100644
@@ -38,6 +38,12 @@
 #define ARCH_PERFMON_EVENTSEL_INV		BIT_ULL(23)
 #define ARCH_PERFMON_EVENTSEL_CMASK		GENMASK_ULL(31, 24)
 
+/*
+ * These are AMD-specific bits.
+ */
+#define AMD64_EVENTSEL_GUESTONLY		BIT_ULL(40)
+#define AMD64_EVENTSEL_HOSTONLY			BIT_ULL(41)
+
 /* RDPMC control flags, Intel only. */
 #define INTEL_RDPMC_METRICS			BIT_ULL(29)
 #define INTEL_RDPMC_FIXED			BIT_ULL(30)
tools/testing/selftests/kvm/include/x86/processor.h +2 -0
index 4ebae4269e68..10ee2d4db1e3 100644
@@ -19,6 +19,8 @@
 #include "kvm_util.h"
 #include "ucall_common.h"
 
+#define __stack_aligned__	__aligned(16)
+
 extern bool host_cpu_is_intel;
 extern bool host_cpu_is_amd;
 extern uint64_t guest_tsc_khz;
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c new file +199 -0

199 lines not on this page · raw message

Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog