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

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®