From: Yosry Ahmed <yosry@kernel.org>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Jim Mattson <jmattson@google.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Yosry Ahmed <yosry@kernel.org>,
stable@vger.kernel.org
Subject: [PATCH 2/3] KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions
Date: Wed, 25 Nov 2026 18:27:02 +0000 [thread overview]
Message-ID: <20261125182703.1694998-3-yosry@kernel.org> (raw)
In-Reply-To: <20261125182703.1694998-1-yosry@kernel.org>
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
next prev parent reply other threads:[~2026-11-25 18:27 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 18:27 [PATCH 0/3] KVM: x86/pmu: Always defer PMU reprogramming on " 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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125182703.1694998-3-yosry@kernel.org \
--to=yosry@kernel.org \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®