Skip to content

RFC KVM: SEV: Add PMC virtualization and filtering for confidential guests

Manali Shukla kvm 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest9 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/9x86/cpufeatures: Add SevPerfCtrVirt feature bitarch/x86/include/asm/cpufeatures.h+1 -00 replies
2/9KVM: SEV: Add VMSA fields for PMC virtualizationarch/x86/include/asm/svm.h+10 -20 replies
3/9x86/sev: Add PMC virtualization support for SNP guestsarch/x86/boot/compressed/sev.c arch/x86/coco/sev/core.c arch/x86/include/asm/msr-index.h+40 -30 replies
4/9x86/sev: Define the GHCB PMC-filter protocolarch/x86/include/asm/sev-common.h arch/x86/include/uapi/asm/svm.h+14 -00 replies
5/9x86/sev: Forward trapped PMC event-select writes to the hypervisorarch/x86/coco/core.c arch/x86/coco/sev/core.c arch/x86/coco/sev/internal.h +2 more+81 -10 replies
6/9KVM: SEV: Add support for hardware-switched PMUarch/x86/include/asm/svm.h arch/x86/kvm/svm/pmu.c arch/x86/kvm/svm/sev.c +2 more+39 -20 replies
7/9KVM: SEV: Filter PMC event-selects forwarded by SEV-ES and SNP guestsarch/x86/kvm/pmu.c arch/x86/kvm/pmu.h arch/x86/kvm/svm/pmu.c +3 more+140 -70 replies
8/9KVM: x86/pmu: Disallow PMU event filter changes for protected guestsarch/x86/kvm/pmu.c+11 -00 replies
9/9KVM: SEV: Enable hardware-assisted PMC filtering for SEV-ES and SNP guestsarch/x86/kvm/svm/sev.c arch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+16 -110 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC PATCH 0/9] KVM: SEV: Add PMC virtualization and filtering for confidential guests

The hardware-switched mediated PMU series [1] disallows PMC virtualization for SEV-ES and SEV-SNP guests: their event-selects live in the encrypted VMSA, so KVM can neither read them nor intercept their writes, and couldn't apply the PMU event filter to them at all.

Hardware-assisted PMC filtering [2] closes that gap. Hardware traps a guest write that enables an event missing from the VMCB's allow list, and VMRUN fails if an enabled counter's event isn't listed, so an SEV-ES or SEV-SNP guest can't count an event KVM hasn't approved.

This RFC proposes a new GHCB protocol through which the guest shares its event-selects with KVM, so that KVM can keep the guest from counting events the host doesn't allow. Comments on the protocol design are welcome.

Overview
--------
Patches 1, 2 and 4 add definitions used by both the guest and the host:
the SevPerfCtrVirt feature bit, the VMSA fields for PMC virtualization, and the proposed GHCB PMC-filter protocol.

Guest patches:
- Patch 3 adds PMC virtualization support for SEV-SNP guests, and seeds PerfCntrGlobalCtl in the VMSAs of SEV-SNP APs.
- Patch 5 forwards trapped event-select writes to the hypervisor.

Host (KVM) patches:
- Patch 6 adds the hardware-switched PMU for SEV-ES and SEV-SNP guests.
- Patch 7 filters the event-selects these guests forward.
- Patch 8 fails PMU event filter changes once the VM has vCPUs.
- Patch 9 advertises PMC virtualization only when hardware-assisted PMC filtering is enabled.

Design
------
- KVM can neither read an SEV-ES or SEV-SNP guest's event-selects nor intercept their writes. On a trapped write, the guest's #VC handler forwards the write with SVM_VMGEXIT_PMC_FILTER, RCX and EDX:EAX holding the write, and the scratch buffer every event-select. KVM emulates the write and runs the reprogramming synchronously, and returns the filtered values in EDX:EAX and the buffer. The guest programs them, honoring only bits being cleared.

- KVM adds an event to the allow list only when its filter allows it. When the list is full, KVM rebuilds it from the enabled event-selects in the buffer. As these are guest-supplied, only events the filter allows are listed. A guest that lies about them can only make its own VMRUN fail.

- SVM_SEV_FEAT_PERFCTR_VIRT is advertised for SEV-ES and SEV-SNP guests, whose kernel discovers PMC virtualization through SEV_STATUS bit 13, and hardware-assisted PMC filtering is supported.

Limitations
-----------
- Support for PMC virtualization for SEV-ES and SEV-SNP guests depends on PMC filtering.

Testing
-------
- SEV, SEV-ES and SEV-SNP guests with perfctr-virt and hardware-assisted PMC filtering, with a PMU event filter denying events 0xC0 and 0xC2: denied events never count and allowed events do.

Questions for discussion
------------------------
- For SEV-ES and SEV-SNP guests, KVM reprograms the counters right away in the SVM_VMGEXIT_PMC_FILTER handler, instead of just before VM-entry as for other guests. Is that OK, or is there a better place to do it?

- KVM can't see the event-selects of an SEV-ES or SEV-SNP guest, so it can't apply a new filter to counters that are already set up. So KVM_SET_PMU_EVENT_FILTER fails with EBUSY once the VM has vCPUs. The filter can only be set before vCPUs are created. Is this acceptable?

These patches are based on

branch:         kvm-x86/next (https://github.com/kvm-x86/linux.git)
Base commit:    6cf46b37b0356 (kvm-x86-next-2026.08.07)

+ hardware-switched PMU series [1]
+ hardware-assisted PMC filtering series [2]

[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/T/

Manali Shukla (5):
x86/sev: Define the GHCB PMC-filter protocol
x86/sev: Forward trapped PMC event-select writes to the hypervisor
KVM: SEV: Filter PMC event-selects forwarded by SEV-ES and SNP guests
KVM: x86/pmu: Disallow PMU event filter changes for protected guests
KVM: SEV: Enable hardware-assisted PMC filtering for SEV-ES and SNP
guests

Sandipan Das (4):
x86/cpufeatures: Add SevPerfCtrVirt feature bit
KVM: SEV: Add VMSA fields for PMC virtualization x86/sev: Add PMC virtualization support for SNP guests
KVM: SEV: Add support for hardware-switched PMU

 arch/x86/boot/compressed/sev.c     |  5 +-
 arch/x86/coco/core.c               |  3 ++
 arch/x86/coco/sev/core.c           | 35 ++++++++++++
 arch/x86/coco/sev/internal.h       |  1 +
 arch/x86/coco/sev/vc-handle.c      | 68 +++++++++++++++++++++++++
 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/msr-index.h   |  3 +-
 arch/x86/include/asm/sev-common.h  | 12 +++++
 arch/x86/include/asm/svm.h         | 13 ++++-
 arch/x86/include/uapi/asm/svm.h    |  2 +
 arch/x86/kvm/pmu.c                 | 18 ++++++-
 arch/x86/kvm/pmu.h                 |  1 +
 arch/x86/kvm/svm/pmu.c             | 85 ++++++++++++++++++++++++++++--
 arch/x86/kvm/svm/sev.c             | 83 ++++++++++++++++++++++++++++-
 arch/x86/kvm/svm/svm.c             | 18 ++++---
 arch/x86/kvm/svm/svm.h             |  6 +++
 include/linux/cc_platform.h        |  8 +++
 17 files changed, 344 insertions(+), 18 deletions(-)

base-commit: 6cf46b37b0356b3408f8ba949e10462cc71b533d

Signature · 1 line
2.43.0