Skip to contentSkip to conversation

Guest support for Enhanced SMT Protection

PATCH New Pratik R. Sampat 1 participant 4 messages Nov 17

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v5 cover Guest support for Enhanced SMT Protection

Enhanced SMT Protection (ESMTP) allows an SEV-SNP VM to require that, while one of its vCPUs is in guest mode, every SMT sibling thread on that physical core is either idle in host mode or running a vCPU the guest itself has declared a legal sibling. This mitigates the side-channel risk of sharing core resources with untrusted host threads or with another guest.

Unlike core scheduling, where co-residency is a host kernel policy expressed with cookies, ESMTP is enforced by hardware. The sibling mask lives in the VMSA. The host is not trusted to run arbitrary kernel, userspace, or interrupt-handling work on a sibling thread while an ESMTP vCPU is active on that core.

This series adds the guest side. The VMM enables ESMTP for an SNP guest through SEV_FEATURES[17], and the guest observes it as SEV_STATUS[19]. Two new VMSA fields carry the policy: VCPU_ID identifies the vCPU within the guest, and VCPU_SIBLING_MASK selects which of its peers it may share a physical core with. Two vCPUs are legal siblings when both are ESMTP vCPUs of the same guest (matching ASID), their VCPU_SIBLING_MASK values match, and their VCPU_IDs agree outside that mask.

Merging
-------
Following the discussion on v4 [4], guest and host support are now split into separate series to untangle x86 and KVM. This series only touches guest code and the VMSA definitions it needs, has no dependency on KVM, and is meant to go through tip. Nothing changes for a guest unless the hypervisor enables ESMTP, so it is safe to merge ahead of host support. KVM host support will be posted separately, potentially on top of the scattered.c rework as suggested by Boris and Sean.

Testing
-------
End-to-end testing needs the host support, which is not part of this series. It was tested with the v4 host patches and patched OVMF [1] and QEMU [2] builds. Launch an SEV-SNP guest with ESMTP enabled on the sev-snp-guest object:

-object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,esmtp=on

The guest reports the feature in dmesg among the SNP feature names:

  # dmesg | grep -i SEV
  ... SEV-SNP ... ESMTProt ...

To check the sibling behaviour on an SMT=2 host:

* Identify a sibling pair X and Y on the host:
cat /sys/devices/system/cpu/cpuX/topology/thread_siblings_list
* Pin two of the guest's vCPUs to X and Y.
* Load both vCPUs in the guest, e.g. stress-ng --cpu 2. Both host CPUs should show 100% guest utilization, since the two vCPUs are legal siblings.
* Start a host task on one of the pair, e.g. taskset -c X stress-ng --cpu 1. CPU X's time should now be split between host and guest, and guest utilization on CPU Y should drop, since the vCPU on Y cannot enter guest mode while the host task runs on X.

Based on tip/master.

v5:
* Split guest and host support into separate series, per Sean and Boris [4]. This series carries guest support only.
* New patch 1: move the VCPU_ID/VCPU_SIBLING_MASK VMSA definitions out of the KVM host patch so that the guest side builds on its own. Retaining Tom's reviewed-by since it is functionally unchanged.
* Drop the sibling-mask comments from patches 2 and 3 - Boris
* Rebase onto tip/master.

Host-side changes from v1-v4 are left out below and will be listed in the KVM series.

v4: https://lore.kernel.org/all/[email protected]/
* Fix conflict of reserved bits introduced by PML support in vmcb_control_area

v3: https://lore.kernel.org/kvm/[email protected]
* Collected Reviewed-by tags - Tom
* Use sev_status to determine SNP support for guest as well - Tom
* Not syncing the tools/ copy of msr-index.h, since several bits apart from ESMTP differ and the perf tooling maintainers usually sync it. Happy to add a sync patch if needed.

v2: https://lore.kernel.org/kvm/[email protected]/
* Populate the ESMTP VMSA fields for Hyper-V AP bring-up - Sashiko
* Add ESMTP to SNP_FEATURES_IMPL and SNP_FEATURES_IMPL_REQ - local Sashiko instance

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

[1]: https://github.com/tianocore/edk2/pull/13128
[2]: https://lore.kernel.org/qemu-devel/[email protected]/
[3]: https://lore.kernel.org/all/[email protected]/

Pratik R. Sampat (3):
x86/sev: Add Enhanced SMT Protection fields to the VMSA x86/sev: Add guest support for Enhanced SMT Protection x86/hyperv: Add guest support for Enhanced SMT Protection

 arch/x86/boot/compressed/sev.c   | 6 ++++--
 arch/x86/coco/sev/core.c         | 7 +++++++
 arch/x86/hyperv/ivm.c            | 5 +++++
 arch/x86/include/asm/msr-index.h | 4 +++-
 arch/x86/include/asm/svm.h       | 8 +++++++-
 5 files changed, 26 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/3 x86/sev: Add Enhanced SMT Protection fields to the VMSA

Enhanced SMT Protection (ESMTP) adds two VMSA fields that carry an SEV-SNP vCPU's co-residency policy. VCPU_ID identifies the vCPU within the guest and VCPU_SIBLING_MASK selects which of its peers it may share a physical core with. Two ESMTP vCPUs of the same guest are legal siblings when their VCPU_SIBLING_MASK values match and their VCPU_IDs agree outside that mask.

Add both fields to struct sev_es_save_area so that the guest can populate them in the AP VMSAs it builds and the hypervisor can populate them in the VMSAs it builds.

Refer to the AMD64 APM Vol 2, section "AMD64 Enhanced SMT Protection".

---
v4..v5:
* Move the VCPU_ID/VCPU_SIBLING_MASK VMSA definitions out of the KVM host patch so that the guest side builds on its own.

---
 arch/x86/include/asm/svm.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/include/asm/svm.h +7 -1
index 83fd49b26a4b..aceb405449a5 100644
@@ -486,6 +486,11 @@ struct sev_es_save_area {
 	u8 fpreg_x87[80];
 	u8 fpreg_xmm[256];
 	u8 fpreg_ymm[256];
+	u8 reserved_0x670[560];
+
+	/* Enhanced SMT Protection */
+	u32 vcpu_id;
+	u32 vcpu_sibling_mask;
 } __packed;
 
 struct ghcb_save_area {
@@ -558,7 +563,7 @@ struct vmcb {
 
 #define EXPECTED_VMCB_SAVE_AREA_SIZE		744
 #define EXPECTED_GHCB_SAVE_AREA_SIZE		1032
-#define EXPECTED_SEV_ES_SAVE_AREA_SIZE		1648
+#define EXPECTED_SEV_ES_SAVE_AREA_SIZE		2216
 #define EXPECTED_VMCB_CONTROL_AREA_SIZE		1024
 #define EXPECTED_GHCB_SIZE			PAGE_SIZE
 
@@ -593,6 +598,7 @@ static inline void __unused_size_checks(void)
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x320);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x380);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x3f0);
+	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x670);
 
 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0x0);
 	BUILD_BUG_RESERVED_OFFSET(ghcb_save_area, 0xcc);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v5 2/3 x86/sev: Add guest support for Enhanced SMT Protection

In reply to Pratik R. Sampat

Enhanced SMT Protection requires every VMSA to carry the vCPU's identity and the mask of SMT siblings it may co-run with. The guest builds the VMSA itself when bringing up an AP, so populate both fields there, before the page is turned into a VMSA and while it is still writable.

Fully set the sibling mask so that all threads of the same guest trust each other.

---
v4..v5:
* Remove comment about vCPU sibling mask configuration - Boris

v3..v4: No change

---
 arch/x86/boot/compressed/sev.c   | 6 ++++--
 arch/x86/coco/sev/core.c         | 7 +++++++
 arch/x86/include/asm/msr-index.h | 4 +++-
 3 files changed, 14 insertions(+), 3 deletions(-)
arch/x86/boot/compressed/sev.c +4 -2
index c6512f2ea31e..4e0fa5a5f70b 100644
@@ -188,7 +188,8 @@ bool sev_es_check_ghcb_fault(unsigned long address)
 				 MSR_AMD64_SNP_RESERVED_BIT13 |		\
 				 MSR_AMD64_SNP_RESERVED_BIT15 |		\
 				 MSR_AMD64_SNP_SECURE_AVIC |		\
-				 MSR_AMD64_SNP_RESERVED_BITS19_22 |	\
+				 MSR_AMD64_SNP_ESMT_PROT |		\
+				 MSR_AMD64_SNP_RESERVED_BITS20_22 |	\
 				 MSR_AMD64_SNP_RESERVED_MASK)
 
 #ifdef CONFIG_AMD_SECURE_AVIC
@@ -204,7 +205,8 @@ bool sev_es_check_ghcb_fault(unsigned long address)
  */
 #define SNP_FEATURES_IMPL	(MSR_AMD64_SNP_DEBUG_SWAP |	\
 				 MSR_AMD64_SNP_SECURE_TSC |	\
-				 SNP_FEATURE_SECURE_AVIC)
+				 SNP_FEATURE_SECURE_AVIC |	\
+				 MSR_AMD64_SNP_ESMT_PROT)
 
 u64 snp_get_unsupported_features(u64 status)
 {
arch/x86/coco/sev/core.c +7 -0
index eb2e853e950a..92bf3afd4351 100644
@@ -89,6 +89,7 @@ static const char * const sev_status_feat_names[] = {
 	[MSR_AMD64_SNP_VMSA_REG_PROT_BIT]	= "VMSARegProt",
 	[MSR_AMD64_SNP_SMT_PROT_BIT]		= "SMTProt",
 	[MSR_AMD64_SNP_SECURE_AVIC_BIT]		= "SecureAVIC",
+	[MSR_AMD64_SNP_ESMT_PROT_BIT]		= "ESMTProt",
 	[MSR_AMD64_SNP_IBPB_ON_ENTRY_BIT]	= "IBPBOnEntry",
 };
 
@@ -849,6 +850,12 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip, unsigned
 	vmsa->vmpl		= snp_vmpl;
 	vmsa->sev_features	= sev_status >> 2;
 
+	if (sev_status & MSR_AMD64_SEV_SNP_ENABLED &&
+	    (sev_status & MSR_AMD64_SNP_ESMT_PROT)) {
+		vmsa->vcpu_id = apic_id;
+		vmsa->vcpu_sibling_mask = U32_MAX;
+	}
+
 	/* Populate AP's TSC scale/offset to get accurate TSC values. */
 	if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
 		vmsa->tsc_scale = snp_tsc_scale;
arch/x86/include/asm/msr-index.h +3 -1
index c0914829a925..83517f53d757 100644
@@ -754,7 +754,9 @@
 #define MSR_AMD64_SNP_SMT_PROT		BIT_ULL(MSR_AMD64_SNP_SMT_PROT_BIT)
 #define MSR_AMD64_SNP_SECURE_AVIC_BIT	18
 #define MSR_AMD64_SNP_SECURE_AVIC	BIT_ULL(MSR_AMD64_SNP_SECURE_AVIC_BIT)
-#define MSR_AMD64_SNP_RESERVED_BITS19_22 GENMASK_ULL(22, 19)
+#define MSR_AMD64_SNP_ESMT_PROT_BIT	19
+#define MSR_AMD64_SNP_ESMT_PROT	BIT_ULL(MSR_AMD64_SNP_ESMT_PROT_BIT)
+#define MSR_AMD64_SNP_RESERVED_BITS20_22 GENMASK_ULL(22, 20)
 #define MSR_AMD64_SNP_IBPB_ON_ENTRY_BIT	23
 #define MSR_AMD64_SNP_IBPB_ON_ENTRY	BIT_ULL(MSR_AMD64_SNP_IBPB_ON_ENTRY_BIT)
 #define MSR_AMD64_SNP_RESV_BIT		24
Signature · 1 line
2.43.0
#3 lore raw

PATCH v5 3/3 x86/hyperv: Add guest support for Enhanced SMT Protection

In reply to Pratik R. Sampat

Add corresponding hyperv support for Enhanced SMT protection. A fully enlightened SEV-SNP guest on Hyper-V builds the VMSA itself when bringing up an AP, so populate both fields there, before the page is turned into a VMSA and while it is still writable.

Similar to KVM, fully set the sibling mask so that all threads of the same guest trust each other.

---
v4..v5:
* Remove comment about vCPU sibling mask configuration - Boris

v3..v4: No change

---
 arch/x86/hyperv/ivm.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/hyperv/ivm.c +5 -0
index 2ce4dfe53472..bd57cac18181 100644
@@ -347,6 +347,11 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu)
 	vmsa->vmpl = 0;
 	vmsa->sev_features = sev_status >> 2;
 
+	if (sev_status & MSR_AMD64_SNP_ESMT_PROT) {
+		vmsa->vcpu_id = apic_id;
+		vmsa->vcpu_sibling_mask = U32_MAX;
+	}
+
 	ret = snp_set_vmsa(vmsa, true);
 	if (ret) {
 		pr_err("RMPADJUST(%llx) failed: %llx\n", (u64)vmsa, ret);
Signature · 1 line
2.43.0