Skip to contentSkip to conversation

i386/sev: Enhanced SMT Protection for SEV-SNP guests

PATCH New Pratik R. Sampat 1 participant 3 messages Sep 14 KVM

lore
Share & tools
lore
#0 lore raw

cover i386/sev: Enhanced SMT Protection for SEV-SNP guests

Enhanced SMT Protection (ESMTP) is an SEV-SNP feature that lets a guest constrain what runs on the sibling thread of the core its vCPUs execute on. Each vCPU's VMSA carries a VCPU_ID and a VCPU_SIBLING_MASK, and hardware admits a VMRUN only when the sibling thread is idle in host mode or is running a vCPU that the mask permits.

This series adds support for to opt-in esmtp support from KVM and engages core scheduling onto the vCPUs to bundle trusted siblings in one group.

Usage
-----
Requires patched Linux and edk2 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

ESMTP is opt-in because it carries a performance cost as VMRUN stalls until the sibling runs work from a trusted vCPU or is in idle.

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

  # dmesg | grep -i SEV

Pratik R. Sampat (2):
i386/sev: Introduce Enhanced SMT Protection for SEV-SNP i386/sev: Add core scheduling for ESMTP enabled guests

 qapi/qom.json     |  9 ++++++-
 target/i386/sev.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++
 target/i386/sev.h |  1 +
 3 files changed, 71 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 i386/sev: Introduce Enhanced SMT Protection for SEV-SNP

Enhanced SMT Protection (ESMTP) protects an SEV-SNP guest from sibling side channels by requiring that, while a vCPU is in guest mode, its SMT sibling thread is either idle in host mode or executing a vCPU the guest has declared to be a legal sibling.

Add an "esmtp" property to request and use it to advertize this feature for KVM to write it into the launch-time VMSAs.

Sample command-line:
-object sev-snp-guest,id=sev0,policy=0xb0000,cbitpos=51,reduced-phys-bits=1,esmtp=on

---
 qapi/qom.json     |  9 ++++++++-
 target/i386/sev.c | 15 +++++++++++++++
 target/i386/sev.h |  1 +
 3 files changed, 24 insertions(+), 1 deletion(-)
qapi/qom.json +8 -1
index 4a9b7f908844..010c5cc2bb82 100644
@@ -1119,6 +1119,12 @@
 # @tsc-frequency: set secure TSC frequency.  Only valid if Secure TSC
 #     is enabled (default: zero) (since 11.1)
 #
+# @esmtp: Enable Enhanced SMT Protection support for the guest.  Adds
+#     corresponding VMSA feature bit to KVM_SEV_INIT2 vmsa_features so
+#     the host applies it to each vCPU's VMSA.  Opt-in feature as it
+#     provides additional protection but with runtime overhead.
+#     (default: false) (since 11.2)
+#
 # Since: 9.1
 ##
 { 'struct': 'SevSnpGuestProperties',
@@ -1132,7 +1138,8 @@
             '*host-data': 'str',
             '*vcek-disabled': 'bool',
             '*secure-tsc': 'bool',
-            '*tsc-frequency': 'uint32' } }
+            '*tsc-frequency': 'uint32',
+            '*esmtp': 'bool' } }
 
 ##
 # @TdxGuestProperties:
target/i386/sev.c +15 -0
index cc16c6b07123..0b5767057175 100644
@@ -3255,6 +3255,18 @@ sev_snp_guest_set_tsc_frequency(Object *obj, Visitor *v, const char *name,
     SEV_SNP_GUEST(obj)->tsc_khz = value / 1000;
 }
 
+static bool
+sev_snp_guest_get_esmtp(Object *obj, Error **errp)
+{
+    return is_sev_feature_set(SEV_COMMON(obj), SVM_SEV_FEAT_ESMTP);
+}
+
+static void
+sev_snp_guest_set_esmtp(Object *obj, bool value, Error **errp)
+{
+    sev_set_feature(SEV_COMMON(obj), SVM_SEV_FEAT_ESMTP, value);
+}
+
 static void
 sev_snp_guest_class_init(ObjectClass *oc, const void *data)
 {
@@ -3296,6 +3308,9 @@ sev_snp_guest_class_init(ObjectClass *oc, const void *data)
     object_class_property_add(oc, "tsc-frequency", "uint32",
                               sev_snp_guest_get_tsc_frequency,
                               sev_snp_guest_set_tsc_frequency, NULL, NULL);
+    object_class_property_add_bool(oc, "esmtp",
+                                   sev_snp_guest_get_esmtp,
+                                   sev_snp_guest_set_esmtp);
 }
 
 static void
target/i386/sev.h +1 -0
index 7725f92e1959..6b4128dea4ae 100644
@@ -49,6 +49,7 @@ bool sev_snp_enabled(void);
 #define SVM_SEV_FEAT_SNP_ACTIVE     BIT(0)
 #define SVM_SEV_FEAT_DEBUG_SWAP     BIT(5)
 #define SVM_SEV_FEAT_SECURE_TSC     BIT(9)
+#define SVM_SEV_FEAT_ESMTP          BIT(17)
 
 typedef struct SevKernelLoaderContext {
     char *setup_data;
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 i386/sev: Add core scheduling for ESMTP enabled guests

In reply to Pratik R. Sampat

Group the vCPU threads for core scheduling such that trusted siblings share cookies and can be co-scheduled on the same core.

The grouping cannot affect isolation. Hardware checks each pairing against the mask in the VMSA, which only the guest writes, so nothing chosen here can bring about a pairing hardware would have refused.

It does decide whether the guest makes progress. Co-scheduling two vCPUs whose masks disagree makes every VMRUN exit with VMEXIT_ILLSIB, which KVM can only answer by retrying, and that never converges.

---
 target/i386/sev.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
target/i386/sev.c +47 -0
index 0b5767057175..34d9851d4e94 100644
@@ -18,6 +18,7 @@
 #include <linux/psp-sev.h>
 
 #include <sys/ioctl.h>
+#include <sys/prctl.h>
 
 #include "qapi/error.h"
 #include "qom/object_interfaces.h"
@@ -2031,6 +2032,47 @@ static int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
     return 0;
 }
 
+/*
+ * Mint a cookie for the first vCPU and copy it to all the others, so that
+ * every vCPU thread of this guest ends up in one core-scheduling group.
+ */
+static void esmtp_core_sched_setup(CPUState *leader, run_on_cpu_data arg)
+{
+    CPUState *cpu;
+    int ret;
+
+    ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, leader->thread_id,
+                PR_SCHED_CORE_SCOPE_THREAD, 0);
+    if (ret < 0) {
+        error_report("vCPU %lu: failed to create core-sched cookie: %s",
+                     kvm_arch_vcpu_id(leader), strerror(errno));
+        return;
+    }
+
+    CPU_FOREACH(cpu) {
+        if (cpu == leader) {
+            continue;
+        }
+
+        ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_TO, cpu->thread_id,
+                    PR_SCHED_CORE_SCOPE_THREAD, 0);
+        if (ret < 0) {
+            error_report("vCPU %lu: failed to share core-sched cookie: %s",
+                         kvm_arch_vcpu_id(cpu), strerror(errno));
+            return;
+        }
+    }
+}
+
+static void esmtp_core_sched_notify(Notifier *notifier, void *data)
+{
+    run_on_cpu(first_cpu, esmtp_core_sched_setup, RUN_ON_CPU_NULL);
+}
+
+static Notifier sev_snp_esmtp_notify = {
+    .notify = esmtp_core_sched_notify,
+};
+
 static int sev_snp_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
 {
     MachineState *ms = MACHINE(qdev_get_machine());
@@ -2049,6 +2091,11 @@ static int sev_snp_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
     g_free(sev_snp_guest->kernel_hashes_data);
     sev_snp_guest->kernel_hashes_data = NULL;
 
+    /* Setup core scheduling cookies for esmtp after the vCPUS are all up */
+    if (is_sev_feature_set(sev_common, SVM_SEV_FEAT_ESMTP)) {
+        qemu_add_machine_init_done_notifier(&sev_snp_esmtp_notify);
+    }
+
     return 0;
 }
 
Signature · 1 line
2.43.0