Skip to contentSkip to conversation

i386/sev: SMT Protection for SEV-SNP guests

PATCH New Pratik R. Sampat 1 participant 5 messages Dec 5 KVM

v2 of 4 New Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

SMT Protection (SMTP) protects an SEV-SNP guest on Zen 4+ systems from sibling side channels by requiring that, while one of its vCPUs is in guest mode, every SMT sibling thread on that physical core is HLT idle in host mode.

Enhanced SMT Protection (ESMTP) is an SEV-SNP feature on Zen5+ systems 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 to opt-in for [E]SMTP from KVM and engages core scheduling onto the vCPUs to either mint new cookies for each vCPU for SMTP or to bundle trusted siblings in one group for ESMTP.

Usage
-----
Requires patched Linux and edk2 builds. Launch an SEV-SNP guest with [E]SMTP enabled on the sev-snp-guest object:

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

Or,

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

SMTP and ESMTP are mutually exclusive.

Both SMTP and ESMTP are opt-in because they carry 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
  ... SEV-SNP ... SMTProt ...
Or,
  ... SEV-SNP ... ESMTProt ...

v1..v2:
* Added in SMT protection to support older class EPYC systems
* Rebased with qemu master

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

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

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

PATCH v2 1/4 i386/sev: Introduce SMT Protection for SEV-SNP

SMT Protection (SMTP) protects an SEV-SNP guest from sibling side channels by requiring that, while one of its vCPUs is in guest mode, every SMT sibling thread on that physical core is HLT idle in host mode.

Add an "smtp" 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,smtp=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 b8237a94be01..3e270895a6d6 100644
@@ -1101,6 +1101,12 @@
 # @tsc-frequency: set secure TSC frequency.  Only valid if Secure TSC
 #     is enabled (default: zero) (since 11.1)
 #
+# @smtp: Enable 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)
+#
 # Features:
 #
 # @confidential-guest-reset: If present, the hypervisor supports
@@ -1119,7 +1125,8 @@
             '*host-data': 'str',
             '*vcek-disabled': 'bool',
             '*secure-tsc': 'bool',
-            '*tsc-frequency': 'uint32' },
+            '*tsc-frequency': 'uint32',
+            '*smtp': 'bool' },
   'features': [ 'confidential-guest-reset' ] }
 
 ##
target/i386/sev.c +15 -0
index 604c2255ac04..845f0b1f37f2 100644
@@ -3316,6 +3316,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_smtp(Object *obj, Error **errp)
+{
+    return is_sev_feature_set(SEV_COMMON(obj), SVM_SEV_FEAT_SMTP);
+}
+
+static void
+sev_snp_guest_set_smtp(Object *obj, bool value, Error **errp)
+{
+    sev_set_feature(SEV_COMMON(obj), SVM_SEV_FEAT_SMTP, value);
+}
+
 static void
 sev_snp_guest_class_init(ObjectClass *oc, const void *data)
 {
@@ -3357,6 +3369,9 @@ sev_snp_guest_class_init(ObjectClass *oc, const void *data)
     object_class_property_add(oc, "tsc-frequency", "uint32", &uint64_type_info,
                               sev_snp_guest_get_tsc_frequency,
                               sev_snp_guest_set_tsc_frequency, NULL, NULL);
+    object_class_property_add_bool(oc, "smtp",
+                                   sev_snp_guest_get_smtp,
+                                   sev_snp_guest_set_smtp);
 }
 
 static void
target/i386/sev.h +1 -0
index 7725f92e1959..2c16a73d9a61 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_SMTP           BIT(15)
 
 typedef struct SevKernelLoaderContext {
     char *setup_data;
Signature · 1 line
2.43.0
#2 lore raw

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

In reply to Pratik R. Sampat

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 | 21 +++++++++++++++++++++
 target/i386/sev.h |  1 +
 3 files changed, 30 insertions(+), 1 deletion(-)
qapi/qom.json +8 -1
index 3e270895a6d6..d60936a56be1 100644
@@ -1107,6 +1107,12 @@
 #     provides additional protection but with runtime overhead.
 #     (default: false)
 #
+# @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)
+#
 # Features:
 #
 # @confidential-guest-reset: If present, the hypervisor supports
@@ -1126,7 +1132,8 @@
             '*vcek-disabled': 'bool',
             '*secure-tsc': 'bool',
             '*tsc-frequency': 'uint32',
-            '*smtp': 'bool' },
+            '*smtp': 'bool',
+            '*esmtp': 'bool' },
   'features': [ 'confidential-guest-reset' ] }
 
 ##
target/i386/sev.c +21 -0
index 845f0b1f37f2..1b5b833974cc 100644
@@ -545,6 +545,12 @@ static int check_sev_features(SevCommonState *sev_common, uint64_t sev_features,
                        "SEV: TSC frequency can only be set if Secure TSC is enabled");
             return -1;
         }
+        if ((sev_features & SVM_SEV_FEAT_ESMTP) &&
+            (sev_features & SVM_SEV_FEAT_SMTP)) {
+            error_setg(errp,
+                       "SEV: SMT Protection and Enhanced SMT Protection are mutually exclusive");
+            return -1;
+        }
     } else {
         if (sev_features && sev_es_enabled()) {
             error_setg(errp,
@@ -3328,6 +3334,18 @@ sev_snp_guest_set_smtp(Object *obj, bool value, Error **errp)
     sev_set_feature(SEV_COMMON(obj), SVM_SEV_FEAT_SMTP, value);
 }
 
+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)
 {
@@ -3372,6 +3390,9 @@ sev_snp_guest_class_init(ObjectClass *oc, const void *data)
     object_class_property_add_bool(oc, "smtp",
                                    sev_snp_guest_get_smtp,
                                    sev_snp_guest_set_smtp);
+    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 2c16a73d9a61..566155417de8 100644
@@ -50,6 +50,7 @@ bool sev_snp_enabled(void);
 #define SVM_SEV_FEAT_DEBUG_SWAP     BIT(5)
 #define SVM_SEV_FEAT_SECURE_TSC     BIT(9)
 #define SVM_SEV_FEAT_SMTP           BIT(15)
+#define SVM_SEV_FEAT_ESMTP          BIT(17)
 
 typedef struct SevKernelLoaderContext {
     char *setup_data;
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/4 i386/sev: Add core scheduling for SMT Protection

In reply to Pratik R. Sampat

Mint a new cookie for each vCPU thread. A vCPU with a unique cookie is never scheduled concurrently on the sibling thread of the same core. So, while one CPU thread runs, the scheduler forces the sibling to idle.

The core scheduling mechanism cannot affect isolation. Hardware enforces the requirement that while a vCPU is in the guest mode, the sibling thread is in idle.

It does decide whether the guest makes progress even when the vCPU threads are scheduled or pinned on the same core without being stalled in a KVM VMEXIT_IDLE_REQUIRED retry loop.

---
 target/i386/sev.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
target/i386/sev.c +31 -0
index 1b5b833974cc..711d28780983 100644
@@ -18,6 +18,7 @@
 #include <linux/psp-sev.h>
 
 #include <sys/ioctl.h>
+#include <sys/prctl.h>
 
 #include "qapi/error.h"
 #include "qapi/qapi-type-infos-common.h"
@@ -2040,6 +2041,31 @@ static int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
     return 0;
 }
 
+static void smtp_core_sched_setup(CPUState *leader, run_on_cpu_data arg)
+{
+    CPUState *cpu;
+    int ret;
+
+    CPU_FOREACH(cpu) {
+        ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, cpu->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(cpu), strerror(errno));
+            return;
+        }
+    }
+}
+
+static void smtp_core_sched_notify(Notifier *notifier, void *data)
+{
+    run_on_cpu(first_cpu, smtp_core_sched_setup, RUN_ON_CPU_NULL);
+}
+
+static Notifier sev_snp_smtp_notify = {
+    .notify = smtp_core_sched_notify,
+};
+
 static int sev_snp_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
 {
     MachineState *ms = MACHINE(qdev_get_machine());
@@ -2058,6 +2084,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 SMTP after the vCPUs are all up */
+    if (is_sev_feature_set(sev_common, SVM_SEV_FEAT_SMTP)) {
+        qemu_add_machine_init_done_notifier(&sev_snp_smtp_notify);
+    }
+
     return 0;
 }
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/4 i386/sev: Add core scheduling for Enhanced SMT Protection

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 | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
target/i386/sev.c +46 -0
index 711d28780983..b10c0f24cea9 100644
@@ -2041,6 +2041,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 void smtp_core_sched_setup(CPUState *leader, run_on_cpu_data arg)
 {
     CPUState *cpu;
@@ -2089,6 +2130,11 @@ static int sev_snp_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
         qemu_add_machine_init_done_notifier(&sev_snp_smtp_notify);
     }
 
+    /* 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