mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] KVM: x86/pmu: Always defer PMU reprogramming on nested transitions
@ 2026-11-25 18:27 Yosry Ahmed
  2026-11-25 18:27 ` [PATCH 1/3] KVM: x86/pmu: Recalc PMC emulation when deferring counter reprogramming Yosry Ahmed
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-11-25 18:27 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, Jim Mattson, kvm, linux-kernel, Yosry Ahmed

When Host-Only/Guest-Only bits handling was added [1], we went back and
forth between deferring PMU reprogramming vs doing it synchronously.

Deferring all reprogramming is incorrect, because on mode changes (e.g.
VMRUN or WRMSR(EFER.SVME)), this would only update the to-be-emulated
PMCs after the instruction is emulated, which would count the
instruction in the previous mode. Real AMD hardware counts instructions
in the mode they retire in.

We ended up with a hybrid approach where architectural mode changes
do reprogramming synchronously, but force-leaving nested defers it, as
it may not be safe to do it synchronously.

This mini-series unifies all nested transition paths by always
calculating to-be-emulated PMCs synchronously and deferring the rest
(e.g. similar to WRMSR(EVENTSEL)).

The motivation for the series was an unprotected SRCU access from
KVM_SET_SREGS -> svm_set_efer -> svm_pmu_handle_nested_transition() ->
... -> pmc_is_event_allowed(). The obvious/simple fix (and probably more
stable-friendly one) is to hold the SRCU lock in kvm_pmu_handle_event()
when reprogramming the counters. However, I think this is a nice cleanup
to have regardless of the SRCU bug.

[1] https://lore.kernel.org/kvm/20260527234711.4175166-1-yosry@kernel.org/

Yosry Ahmed (3):
  KVM: x86/pmu: Recalc PMC emulation when deferring counter
    reprogramming
  KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions
  KVM: selftests: Set EFER.SVME via KVM_SET_SREGS in
    svm_pmu_host_guest_test

 arch/x86/kvm/pmu.c                            |  1 -
 arch/x86/kvm/pmu.h                            | 32 ++++++++-----------
 arch/x86/kvm/svm/nested.c                     | 10 +-----
 arch/x86/kvm/svm/svm.h                        | 18 ++---------
 .../kvm/x86/svm_pmu_host_guest_test.c         | 31 +++++++++++++++---
 5 files changed, 44 insertions(+), 48 deletions(-)


base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] KVM: x86/pmu: Recalc PMC emulation when deferring counter reprogramming
  2026-11-25 18:27 [PATCH 0/3] KVM: x86/pmu: Always defer PMU reprogramming on nested transitions Yosry Ahmed
@ 2026-11-25 18:27 ` Yosry Ahmed
  2026-11-25 18:27 ` [PATCH 2/3] KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions Yosry Ahmed
  2026-11-25 18:27 ` [PATCH 3/3] KVM: selftests: Set EFER.SVME via KVM_SET_SREGS in svm_pmu_host_guest_test Yosry Ahmed
  2 siblings, 0 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-11-25 18:27 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, Jim Mattson, kvm, linux-kernel, Yosry Ahmed, stable

In preparation for always deferring counter reprogramming, recalculate
PMC emulation for each counter first. On AMD, this would make sure that
mode changes (e.g. via VMRUN or toggling EFER.SVME) are immediately
propagated before instruction emulation completes, such that
instructions are counted in the mode they retire in as hardware does.

This gives kvm_pmu_request_counters_reprogram() similar treatment to
kvm_pmu_request_counter_reprogram(), as done in commit 5dfd498bad5f
("KVM: x86/pmu: Calculate set of to-be-emulated PMCs at time of
WRMSRs").

Now that kvm_pmu_request_counter_reprogram() is just a single-counter
version of kvm_pmu_request_counters_reprogram(), just call the latter
from the former.

No functional change intended, as all architectural mode-changing paths
on AMD do not currently defer counter reprogramming.

Cc: stable@vger.kernel.org
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 arch/x86/kvm/pmu.h | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 090c9bbb74f48..8aa9db1569167 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -218,26 +218,27 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops);
 void kvm_pmu_recalc_pmc_emulation(struct kvm_pmu *pmu, struct kvm_pmc *pmc);
 void kvm_pmu_handle_event(struct kvm_vcpu *vcpu);
 
-static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc)
-{
-	kvm_pmu_recalc_pmc_emulation(pmc_to_pmu(pmc), pmc);
-
-	set_bit(pmc->idx, pmc_to_pmu(pmc)->reprogram_pmi);
-	kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
-}
-
 static inline void __kvm_pmu_reprogram_counters(struct kvm_pmu *pmu,
 						u64 counters,
 						bool defer)
 {
+	DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX);
+	struct kvm_pmc *pmc;
+	int bit;
+
 	if (!counters)
 		return;
 
 	atomic64_or(counters, &pmu->__reprogram_pmi);
-	if (defer)
+	if (defer) {
+		BUILD_BUG_ON(sizeof(bitmap) != sizeof(counters));
+		bitmap_from_u64(bitmap, counters);
+		kvm_for_each_pmc(pmu, pmc, bit, bitmap)
+			kvm_pmu_recalc_pmc_emulation(pmu, pmc);
 		kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
-	else
+	} else {
 		kvm_pmu_handle_event(pmu_to_vcpu(pmu));
+	}
 }
 
 static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu,
@@ -246,6 +247,11 @@ static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu,
 	__kvm_pmu_reprogram_counters(pmu, counters, true);
 }
 
+static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc)
+{
+	kvm_pmu_request_counters_reprogram(pmc_to_pmu(pmc), BIT_ULL(pmc->idx));
+}
+
 /*
  * Check if a PMC is enabled by comparing it against global_ctrl bits.
  *
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions
  2026-11-25 18:27 [PATCH 0/3] KVM: x86/pmu: Always defer PMU reprogramming on nested transitions Yosry Ahmed
  2026-11-25 18:27 ` [PATCH 1/3] KVM: x86/pmu: Recalc PMC emulation when deferring counter reprogramming Yosry Ahmed
@ 2026-11-25 18:27 ` Yosry Ahmed
  2026-11-25 18:27 ` [PATCH 3/3] KVM: selftests: Set EFER.SVME via KVM_SET_SREGS in svm_pmu_host_guest_test Yosry Ahmed
  2 siblings, 0 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-11-25 18:27 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, Jim Mattson, kvm, linux-kernel, Yosry Ahmed, stable

Always defer PMU counter reprogramming via KVM_REQ_PMU on SVM nested
transitions and EFER.SVME toggles instead of invoking
kvm_pmu_handle_event() synchronously. The motivation for synchronous
handling was to ensure to-be-emulated PMCs are updated before
instruction emulation completes, such that instructions count in the
mode they retire in (as per the hardware behavior).

However, since kvm_pmu_request_counters_reprogram() now recalculates
to-be-emulated PMCs synchronously, and kvm_pmu_recalc_pmc_emulation() ->
pmc_is_locally_enabled() already accounts for Host-Only and Guest-Only
bits on AMD, VMRUN or WRMSR(MSR_EFER) would correctly be counted in the
mode they retire in.

In addition to being a nice cleanup, this fixes an unprotected SRCU
access (and potential UAF) when userspace updates EFER.SVME via
KVM_SET_SREGS[2], as svm_set_efer() ends up calling
kvm_mediated_pmu_refresh_event_filter() -> pmc_is_event_allowed(), which
dereferences kvm->arch.pmu_event_filter via srcu_dereference(). This
results in the following splat (with CONFIG_PROVE_RCU):

  WARNING: suspicious RCU usage
  arch/x86/kvm/pmu.c:539 suspicious rcu_dereference_check() usage!
  ...
  pmc_is_event_allowed+0x27c/0x2b0 [kvm]
  kvm_pmu_handle_event+0x365/0x880 [kvm]
  svm_set_efer+0x16b/0x1a0 [kvm_amd]
  __set_sregs_common+0x2fd/0x6a0 [kvm]
  __set_sregs+0x37/0xd0 [kvm]
  kvm_arch_vcpu_ioctl_set_sregs+0x3e/0x50 [kvm]
  kvm_vcpu_ioctl+0x53a/0x6b0 [kvm]

Fixes: a02a25a65246 ("KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions")
Cc: stable@vger.kernel.org
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 arch/x86/kvm/pmu.c        |  1 -
 arch/x86/kvm/pmu.h        | 26 ++++++++------------------
 arch/x86/kvm/svm/nested.c | 10 +---------
 arch/x86/kvm/svm/svm.h    | 18 ++----------------
 4 files changed, 11 insertions(+), 44 deletions(-)

diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index eef2f792dc9e8..ae781bce7d322 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -698,7 +698,6 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
 	kvm_for_each_pmc(pmu, pmc, bit, bitmap)
 		kvm_pmu_recalc_pmc_emulation(pmu, pmc);
 }
-EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_pmu_handle_event);
 
 int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)
 {
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 8aa9db1569167..910a09ed9274e 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -216,11 +216,8 @@ extern struct x86_pmu_capability kvm_pmu_cap;
 void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops);
 
 void kvm_pmu_recalc_pmc_emulation(struct kvm_pmu *pmu, struct kvm_pmc *pmc);
-void kvm_pmu_handle_event(struct kvm_vcpu *vcpu);
 
-static inline void __kvm_pmu_reprogram_counters(struct kvm_pmu *pmu,
-						u64 counters,
-						bool defer)
+static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu, u64 counters)
 {
 	DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX);
 	struct kvm_pmc *pmc;
@@ -229,22 +226,14 @@ static inline void __kvm_pmu_reprogram_counters(struct kvm_pmu *pmu,
 	if (!counters)
 		return;
 
+	BUILD_BUG_ON(sizeof(bitmap) != sizeof(counters));
 	atomic64_or(counters, &pmu->__reprogram_pmi);
-	if (defer) {
-		BUILD_BUG_ON(sizeof(bitmap) != sizeof(counters));
-		bitmap_from_u64(bitmap, counters);
-		kvm_for_each_pmc(pmu, pmc, bit, bitmap)
-			kvm_pmu_recalc_pmc_emulation(pmu, pmc);
-		kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
-	} else {
-		kvm_pmu_handle_event(pmu_to_vcpu(pmu));
-	}
-}
+	bitmap_from_u64(bitmap, counters);
 
-static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu,
-						      u64 counters)
-{
-	__kvm_pmu_reprogram_counters(pmu, counters, true);
+	kvm_for_each_pmc(pmu, pmc, bit, bitmap)
+		kvm_pmu_recalc_pmc_emulation(pmu, pmc);
+
+	kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
 }
 
 static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc)
@@ -278,6 +267,7 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *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);
 int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx);
 bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr);
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 1cecb82702e2c..122be289660c3 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -1539,15 +1539,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)
 		svm->nested.vmcb12_gpa = INVALID_GPA;
 
 		leave_guest_mode(vcpu);
-
-		/*
-		 * Force leaving nested is a non-architectural flow so precision
-		 * isn't a priority.  Defer updating the PMU until the next vCPU
-		 * run, potentially tolerating some imprecision to avoid poking
-		 * into PMU state from arbitrary contexts (e.g. to avoid using
-		 * stale state).
-		 */
-		__svm_pmu_handle_nested_transition(svm, true);
+		svm_pmu_handle_nested_transition(svm);
 
 		svm_switch_vmcb(svm, &svm->vmcb01);
 
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 011ab9cd40301..1c7727d47125c 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -914,26 +914,12 @@ bool nested_svm_check_vmcb01_save(struct kvm_vcpu *vcpu,
 				  struct vmcb_save_area *save);
 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb);
 
-
-static inline void __svm_pmu_handle_nested_transition(struct vcpu_svm *svm,
-						      bool defer)
+static inline void svm_pmu_handle_nested_transition(struct vcpu_svm *svm)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(&svm->vcpu);
 	u64 counters = *(u64 *)pmu->pmc_has_mode_specific_enables;
 
-	__kvm_pmu_reprogram_counters(pmu, counters, defer);
-}
-
-static inline void svm_pmu_handle_nested_transition(struct vcpu_svm *svm)
-{
-	/*
-	 * Do NOT defer reprogramming the counters by default.  Instructions
-	 * causing a state change are counted based on the _new_ CPU state
-	 * (e.g. a successful VMRUN is counted in guest mode). Hence, the
-	 * counters should be reprogrammed with the new state _before_ the
-	 * instruction is potentially counted upon emulation completion.
-	 */
-	__svm_pmu_handle_nested_transition(svm, false);
+	kvm_pmu_request_counters_reprogram(pmu, counters);
 }
 
 extern struct kvm_x86_nested_ops svm_nested_ops;
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] KVM: selftests: Set EFER.SVME via KVM_SET_SREGS in svm_pmu_host_guest_test
  2026-11-25 18:27 [PATCH 0/3] KVM: x86/pmu: Always defer PMU reprogramming on nested transitions Yosry Ahmed
  2026-11-25 18:27 ` [PATCH 1/3] KVM: x86/pmu: Recalc PMC emulation when deferring counter reprogramming Yosry Ahmed
  2026-11-25 18:27 ` [PATCH 2/3] KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions Yosry Ahmed
@ 2026-11-25 18:27 ` Yosry Ahmed
  2 siblings, 0 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-11-25 18:27 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, Jim Mattson, kvm, linux-kernel, Yosry Ahmed

Extend svm_pmu_host_guest_test to set EFER.SVME from userspace via
KVM_SET_SREGS and verify that Host-Only counters resume counting. Switch
the existing filter syncs to GUEST_SYNC2() so the guest can select
between updating the PMU event filter and setting EFER.SVME.

Setting EFER.SVME via KVM_SET_SREGS exercises the __set_sregs_common()
-> svm_set_efer() -> svm_pmu_handle_nested_transition() path from
userspace (and specifically from KVM_SET_SREGS, where kvm->srcu is not
held).

Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 .../kvm/x86/svm_pmu_host_guest_test.c         | 31 ++++++++++++++++---
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c b/tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
index c5b5cd788d93f..3f743dbaf851f 100644
--- a/tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
+++ b/tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
@@ -37,6 +37,11 @@
 
 #define LOOP_INSNS	1000
 
+enum sync_cmd {
+	SYNC_SET_FILTER,
+	SYNC_SET_SVME,
+};
+
 static __always_inline void run_instruction_loop(void)
 {
 	unsigned int i;
@@ -128,12 +133,12 @@ static void l1_guest_code(struct svm_test_data *svm)
 	assert_pmc_counts(deltas, BIT(PMC_NONE) | BIT(PMC_H) | BIT(PMC_HG));
 
 	/* Step 5: Set KVM_PMU_EVENT_DENY - all counters stop */
-	GUEST_SYNC(KVM_PMU_EVENT_DENY);
+	GUEST_SYNC2(SYNC_SET_FILTER, KVM_PMU_EVENT_DENY);
 	run_and_measure(deltas);
 	assert_pmc_counts(deltas, 0);
 
 	/* Step 6: Set KVM_PMU_EVENT_ALLOW - back to all except Guest-only */
-	GUEST_SYNC(KVM_PMU_EVENT_ALLOW);
+	GUEST_SYNC2(SYNC_SET_FILTER, KVM_PMU_EVENT_ALLOW);
 	run_and_measure(deltas);
 	assert_pmc_counts(deltas, BIT(PMC_NONE) | BIT(PMC_H) | BIT(PMC_HG));
 
@@ -153,6 +158,11 @@ static void l1_guest_code(struct svm_test_data *svm)
 	run_and_measure(deltas);
 	assert_pmc_counts(deltas, BIT(PMC_NONE));
 
+	/* Step 10: Set SVME via KVM_SET_SREGS - Host-Only counters resume */
+	GUEST_SYNC1(SYNC_SET_SVME);
+	run_and_measure(deltas);
+	assert_pmc_counts(deltas, BIT(PMC_NONE) | BIT(PMC_H) | BIT(PMC_HG));
+
 	GUEST_DONE();
 }
 
@@ -174,6 +184,7 @@ static struct kvm_pmu_event_filter *alloc_event_filter(u64 event)
 int main(int argc, char *argv[])
 {
 	struct kvm_pmu_event_filter *filter;
+	struct kvm_sregs sregs;
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
 	struct ucall uc;
@@ -201,8 +212,20 @@ int main(int argc, char *argv[])
 		case UCALL_DONE:
 			goto done;
 		case UCALL_SYNC:
-			filter->action = uc.args[1];
-			vm_ioctl(vm, KVM_SET_PMU_EVENT_FILTER, filter);
+			switch (uc.args[0]) {
+			case SYNC_SET_FILTER:
+				filter->action = uc.args[1];
+				vm_ioctl(vm, KVM_SET_PMU_EVENT_FILTER, filter);
+				break;
+			case SYNC_SET_SVME:
+				vcpu_sregs_get(vcpu, &sregs);
+				sregs.efer |= EFER_SVME;
+				vcpu_sregs_set(vcpu, &sregs);
+				break;
+			default:
+				TEST_FAIL("Unknown sync cmd %lu", uc.args[0]);
+				goto done;
+			}
 			break;
 		default:
 			TEST_FAIL("Unknown ucall %lu", uc.cmd);
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-25 18:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 18:27 [PATCH 0/3] KVM: x86/pmu: Always defer PMU reprogramming on nested transitions Yosry Ahmed
2026-11-25 18:27 ` [PATCH 1/3] KVM: x86/pmu: Recalc PMC emulation when deferring counter reprogramming Yosry Ahmed
2026-11-25 18:27 ` [PATCH 2/3] KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions Yosry Ahmed
2026-11-25 18:27 ` [PATCH 3/3] KVM: selftests: Set EFER.SVME via KVM_SET_SREGS in svm_pmu_host_guest_test Yosry Ahmed

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®