Skip to content

KVM: SVM: Add support for AMD LBR Extension v2

Shivansh Dhiman kvm sashiko-reviews 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest12 patches 31 repliesIn review
  2. v17 patches 5 repliesSuperseded

Changes in v2 from the cover letter

* Rebased on kvm-x86-next-2026.10.13. Use newly added functions svm_get_supported_debugctl() and svm_need_lbr_virtualization().
* New patch to report the LBRv2 MSRs for live migration (patch 5).
* New patch to support LBRv2 for SEV-ES/SNP guests (patch 8).
* New patches to support LBRv2 for nested SVM (patches 9 and 10).
* Split v1 patch 2 into the CPUID feature bit (patch 2) and the perf change (patch 6). Fix authorship.
* Advertise LbrExtV2 only with a 16-entry host LBR stack, and removed the per-vCPU clear of the feature.
* Use KVM_BUG_ON() for an out-of-range LBR stack index.
* Do not mark the VMCB dirty on LBRv2 MSR writes.
* Updated commit messages.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/12KVM: SVM: Add VMCB fields for LBRv2 virtualizationarch/x86/include/asm/svm.h+11 -32 replies
2/12KVM: SVM: Add the LbrExtV2 CPUID feature bitarch/x86/kvm/reverse_cpuid.h+2 -02 replies
3/12KVM: SVM: Emulate guest accesses to LBRv2 MSRsarch/x86/include/asm/svm.h arch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+74 -02 replies
4/12KVM: SVM: Enable hardware-assisted LBRv2 virtualizationarch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+45 -15 replies
5/12KVM: x86: Add support for Live Migrationarch/x86/kvm/msrs.c+26 -02 replies
6/12perf/x86: Expose the LBR stack depth via x86_pmu_capabilityarch/x86/events/core.c arch/x86/include/asm/perf_event.h+2 -02 replies
7/12KVM: SVM: Advertise LbrExtV2 to userspacearch/x86/kvm/cpuid.c arch/x86/kvm/svm/svm.c+15 -05 replies
8/12KVM: SVM: Support LBRv2 virtualization for SEV-ES guestsarch/x86/include/asm/svm.h arch/x86/kvm/svm/svm.c+22 -52 replies
9/12KVM: nSVM: Propagate LBRv2 state across nested transitionsarch/x86/kvm/svm/nested.c arch/x86/kvm/svm/svm.h+45 -11 reply
10/12KVM: nSVM: Pass through LBRv2 MSRs to L2arch/x86/kvm/svm/nested.c+22 -12 replies
11/12KVM: SVM: Add LBR/PMC freeze supportarch/x86/kvm/cpuid.c arch/x86/kvm/reverse_cpuid.h arch/x86/kvm/svm/svm.h+9 -21 reply
12/12KVM: x86: Use SCATTERED_F() for PERFMON_V2arch/x86/kvm/cpuid.c+1 -12 replies

Reviews trailers per patch

5 of 12 patches have a Reviewed-by or Acked-by.

PatchJim Mattson
1/12R-b
2/12R-b
3/12
4/12
5/12R-b
6/12R-b
7/12
8/12
9/12
10/12
11/12
12/12R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 00/12] KVM: SVM: Add support for AMD LBR Extension v2

This series adds SVM support for virtualizing AMD's Last Branch Record Extension Version 2 (LbrExtV2) in KVM.

Background
----------
LbrExtV2, introduced on Zen 4, is a branch sampling facility. It records the last taken branches in a 16-entry stack of dedicated MSRs:
* MSR 0xC000010E - Last Branch Stack Select (LBR_SELECT)
* MSR 0xC000010F - Debug Extension Control (DBG_EXTN_CFG)
* MSR 0xC0010300..0xC001031F - 16 LBR FROM/TO MSR pairs

AMD hardware extends the VMCB save area with fields for these MSRs. When V_LBR (VMCB.MISC_CTL2.LBR_VIRTUALIZATION_ENABLE) is set, the CPU saves and restores the guest's LbrExtV2 state on VMRUN/#VMEXIT. Thus, the guest can use LBRv2 without interception.

V_LBR is enabled lazily. KVM enables LBR virtualization only when the guest sets DBG_EXTN_CFG.LBRV2EN (bit 6) to record branches. Thus, there is no save/restore cost on VMRUN/#VMEXIT while the guest does not use LBRv2. The series also adds the dependent LBR and PMC freeze-on-PMI feature.

Major changes in v2
-------------------
* Added LBRv2 support for SEV-ES/SNP guests.
* Added LBRv2 support for nested SVM.
* Added live migration support for the LBRv2 MSRs.

Testing
-------
Disable the NMI watchdog on the host and enable the mediated PMU before the test:

$ echo 0 | sudo tee /proc/sys/kernel/nmi_watchdog
$ sudo modprobe kvm_amd enable_mediated_pmu=1

Testing has been done by running perf LBR test inside the guest:

$ ./tools/perf/perf test -vvvv "LBR"

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

Related QEMU patchset:
https://lore.kernel.org/kvm/[email protected]/

Regards,
Shivansh

---
Changelog:

v1 -> v2:
* Rebased on kvm-x86-next-2026.10.13. Use newly added functions svm_get_supported_debugctl() and svm_need_lbr_virtualization().
* New patch to report the LBRv2 MSRs for live migration (patch 5).
* New patch to support LBRv2 for SEV-ES/SNP guests (patch 8).
* New patches to support LBRv2 for nested SVM (patches 9 and 10).
* Split v1 patch 2 into the CPUID feature bit (patch 2) and the perf change (patch 6). Fix authorship.
* Advertise LbrExtV2 only with a 16-entry host LBR stack, and removed the per-vCPU clear of the feature.
* Use KVM_BUG_ON() for an out-of-range LBR stack index.
* Do not mark the VMCB dirty on LBRv2 MSR writes.
* Updated commit messages.

---
Sandipan Das (3):
KVM: SVM: Add VMCB fields for LBRv2 virtualization
KVM: SVM: Add the LbrExtV2 CPUID feature bit perf/x86: Expose the LBR stack depth via x86_pmu_capability

Shivansh Dhiman (9):
KVM: SVM: Emulate guest accesses to LBRv2 MSRs
KVM: SVM: Enable hardware-assisted LBRv2 virtualization
KVM: x86: Add support for Live Migration
KVM: SVM: Advertise LbrExtV2 to userspace
KVM: SVM: Support LBRv2 virtualization for SEV-ES guests
KVM: nSVM: Propagate LBRv2 state across nested transitions
KVM: nSVM: Pass through LBRv2 MSRs to L2
KVM: SVM: Add LBR/PMC freeze support
KVM: x86: Use SCATTERED_F() for PERFMON_V2

 arch/x86/events/core.c            |   1 +
 arch/x86/include/asm/perf_event.h |   1 +
 arch/x86/include/asm/svm.h        |  28 ++++--
 arch/x86/kvm/cpuid.c              |   6 +-
 arch/x86/kvm/msrs.c               |  26 ++++++
 arch/x86/kvm/reverse_cpuid.h      |   6 +-
 arch/x86/kvm/svm/nested.c         |  51 ++++++++++-
 arch/x86/kvm/svm/svm.c            | 137 +++++++++++++++++++++++++++++-
 arch/x86/kvm/svm/svm.h            |  26 ++++++
 9 files changed, 271 insertions(+), 11 deletions(-)

base-commit: dea4f6380f4ec96e8f3b0aaf2343764bd0d25ef4

Signature · 1 line
2.43.0