History

KVM: x86: Support KVM VMs sharing SEV context

Add a capability for userspace to mirror SEV encryption context from one vm to another. On our side, this is intended to support a Migration Helper vCPU, but it can also be used generically to support other in-guest workloads scheduled by the host. The intention is for the primary guest and the mirror to have nearly identical memslots. The primary benefits of this are that: 1) The VMs do not share KVM contexts (think APIC/MSRs/etc), so they can't accidentally clobber each other. 2) The VMs can have different memory-views, which is necessary for post-copy migration (the migration vCPUs on the target need to read and write to pages, when the primary guest would VMEXIT). This does not change the threat model for AMD SEV. Any memory involved is still owned by the primary guest and its initial state is still attested to through the normal SEV_LAUNCH_* flows. If userspace wanted to circumvent SEV, they could achieve the same effect by simply attaching a vCPU to the primary VM. This patch deliberately leaves userspace in charge of the memslots for the mirror, as it already has the power to mess with them in the primary guest. This patch does not support SEV-ES (much less SNP), as it does not handle handing off attested VMSAs to the mirror. For additional context, we need a Migration Helper because SEV PSP migration is far too slow for our live migration on its own. Using an in-guest migrator lets us speed this up significantly.

Nathan Tempelman committed 5 years ago

Commit 54526d11 parent ee695f29 files · +126 −2Browse files

Unified · Split

9 files
  1. Documentation/virt/kvm/api.rst +17 −1
  2. arch/x86/include/asm/kvm_host.h +1 −0
  3. arch/x86/kvm/svm/sev.c +90 −0
  4. arch/x86/kvm/svm/svm.c +2 −0
  5. arch/x86/kvm/svm/svm.h +2 −0
  6. arch/x86/kvm/x86.c +6 −1
  7. include/linux/kvm_host.h +1 −0
  8. include/uapi/linux/kvm.h +1 −0
  9. virt/kvm/kvm_main.c +6 −0
Documentation/virt/kvm/api.rst+17−1
@@ -6207,6 +6207,22 @@ KVM_RUN_BUS_LOCK flag is used to distinguish between them. · 6206 unmodified lines
62076207 This capability can be used to check / enable 2nd DAWR feature provided
62086208 by POWER10 processor.
62096209
6210+7.24 KVM_CAP_VM_COPY_ENC_CONTEXT_FROM
6211+-------------------------------------
6212+
6213+Architectures: x86 SEV enabled
6214+Type: vm
6215+Parameters: args[0] is the fd of the source vm
6216+Returns: 0 on success; ENOTTY on error
6217+
6218+This capability enables userspace to copy encryption context from the vm
6219+indicated by the fd to the vm this is called on.
6220+
6221+This is intended to support in-guest workloads scheduled by the host. This
6222+allows the in-guest workload to maintain its own NPTs and keeps the two vms
6223+from accidentally clobbering each other with interrupts and the like (separate
6224+APIC/MSRs/etc).
6225+
62106226 7.25 KVM_CAP_SGX_ATTRIBUTE
62116227 ----------------------
62126228
@@ -6749,4 +6765,4 @@ in the kernel based fast path. If they can not be handled by the kernel, · 536 unmodified lines
67496765 they will get passed on to user space. So user space still has to have
67506766 an implementation for these despite the in kernel acceleration.
67516767
6752−This capability is always enabled.
6768+This capability is always enabled.
arch/x86/include/asm/kvm_host.h+1−0
@@ -1349,6 +1349,7 @@ struct kvm_x86_ops { · 1348 unmodified lines
13491349 int (*mem_enc_op)(struct kvm *kvm, void __user *argp);
13501350 int (*mem_enc_reg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
13511351 int (*mem_enc_unreg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
1352+ int (*vm_copy_enc_context_from)(struct kvm *kvm, unsigned int source_fd);
13521353
13531354 int (*get_msr_feature)(struct kvm_msr_entry *entry);
13541355
arch/x86/kvm/svm/sev.c+90−0
@@ -66,6 +66,11 @@ static int sev_flush_asids(void) · 65 unmodified lines
6666 return ret;
6767 }
6868
69+static inline bool is_mirroring_enc_context(struct kvm *kvm)
70+{
71+ return !!to_kvm_svm(kvm)->sev_info.enc_context_owner;
72+}
73+
6974 /* Must be called with the sev_bitmap_lock held */
7075 static bool __sev_recycle_asids(int min_asid, int max_asid)
7176 {
@@ -1122,6 +1127,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) · 1050 unmodified lines
11221127
11231128 mutex_lock(&kvm->lock);
11241129
1130+ /* enc_context_owner handles all memory enc operations */
1131+ if (is_mirroring_enc_context(kvm)) {
1132+ r = -EINVAL;
1133+ goto out;
1134+ }
1135+
11251136 switch (sev_cmd.id) {
11261137 case KVM_SEV_ES_INIT:
11271138 if (!sev_es) {
@@ -1185,6 +1196,10 @@ int svm_register_enc_region(struct kvm *kvm, · 57 unmodified lines
11851196 if (!sev_guest(kvm))
11861197 return -ENOTTY;
11871198
1199+ /* If kvm is mirroring encryption context it isn't responsible for it */
1200+ if (is_mirroring_enc_context(kvm))
1201+ return -EINVAL;
1202+
11881203 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
11891204 return -EINVAL;
11901205
@@ -1251,6 +1266,10 @@ int svm_unregister_enc_region(struct kvm *kvm, · 60 unmodified lines
12511266 struct enc_region *region;
12521267 int ret;
12531268
1269+ /* If kvm is mirroring encryption context it isn't responsible for it */
1270+ if (is_mirroring_enc_context(kvm))
1271+ return -EINVAL;
1272+
12541273 mutex_lock(&kvm->lock);
12551274
12561275 if (!sev_guest(kvm)) {
@@ -1281,6 +1300,71 @@ failed: · 24 unmodified lines
12811300 return ret;
12821301 }
12831302
1303+int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd)
1304+{
1305+ struct file *source_kvm_file;
1306+ struct kvm *source_kvm;
1307+ struct kvm_sev_info *mirror_sev;
1308+ unsigned int asid;
1309+ int ret;
1310+
1311+ source_kvm_file = fget(source_fd);
1312+ if (!file_is_kvm(source_kvm_file)) {
1313+ ret = -EBADF;
1314+ goto e_source_put;
1315+ }
1316+
1317+ source_kvm = source_kvm_file->private_data;
1318+ mutex_lock(&source_kvm->lock);
1319+
1320+ if (!sev_guest(source_kvm)) {
1321+ ret = -EINVAL;
1322+ goto e_source_unlock;
1323+ }
1324+
1325+ /* Mirrors of mirrors should work, but let's not get silly */
1326+ if (is_mirroring_enc_context(source_kvm) || source_kvm == kvm) {
1327+ ret = -EINVAL;
1328+ goto e_source_unlock;
1329+ }
1330+
1331+ asid = to_kvm_svm(source_kvm)->sev_info.asid;
1332+
1333+ /*
1334+ * The mirror kvm holds an enc_context_owner ref so its asid can't
1335+ * disappear until we're done with it
1336+ */
1337+ kvm_get_kvm(source_kvm);
1338+
1339+ fput(source_kvm_file);
1340+ mutex_unlock(&source_kvm->lock);
1341+ mutex_lock(&kvm->lock);
1342+
1343+ if (sev_guest(kvm)) {
1344+ ret = -EINVAL;
1345+ goto e_mirror_unlock;
1346+ }
1347+
1348+ /* Set enc_context_owner and copy its encryption context over */
1349+ mirror_sev = &to_kvm_svm(kvm)->sev_info;
1350+ mirror_sev->enc_context_owner = source_kvm;
1351+ mirror_sev->asid = asid;
1352+ mirror_sev->active = true;
1353+
1354+ mutex_unlock(&kvm->lock);
1355+ return 0;
1356+
1357+e_mirror_unlock:
1358+ mutex_unlock(&kvm->lock);
1359+ kvm_put_kvm(source_kvm);
1360+ return ret;
1361+e_source_unlock:
1362+ mutex_unlock(&source_kvm->lock);
1363+e_source_put:
1364+ fput(source_kvm_file);
1365+ return ret;
1366+}
1367+
12841368 void sev_vm_destroy(struct kvm *kvm)
12851369 {
12861370 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
@@ -1290,6 +1374,12 @@ void sev_vm_destroy(struct kvm *kvm) · 3 unmodified lines
12901374 if (!sev_guest(kvm))
12911375 return;
12921376
1377+ /* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */
1378+ if (is_mirroring_enc_context(kvm)) {
1379+ kvm_put_kvm(sev->enc_context_owner);
1380+ return;
1381+ }
1382+
12931383 mutex_lock(&kvm->lock);
12941384
12951385 /*
arch/x86/kvm/svm/svm.c+2−0
@@ -4589,6 +4589,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { · 4588 unmodified lines
45894589 .mem_enc_reg_region = svm_register_enc_region,
45904590 .mem_enc_unreg_region = svm_unregister_enc_region,
45914591
4592+ .vm_copy_enc_context_from = svm_vm_copy_asid_from,
4593+
45924594 .can_emulate_instruction = svm_can_emulate_instruction,
45934595
45944596 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
arch/x86/kvm/svm/svm.h+2−0
@@ -68,6 +68,7 @@ struct kvm_sev_info { · 67 unmodified lines
6868 unsigned long pages_locked; /* Number of pages locked */
6969 struct list_head regions_list; /* List of registered regions */
7070 u64 ap_jump_table; /* SEV-ES AP Jump Table address */
71+ struct kvm *enc_context_owner; /* Owner of copied encryption context */
7172 };
7273
7374 struct kvm_svm {
@@ -580,6 +581,7 @@ int svm_register_enc_region(struct kvm *kvm, · 506 unmodified lines
580581 struct kvm_enc_region *range);
581582 int svm_unregister_enc_region(struct kvm *kvm,
582583 struct kvm_enc_region *range);
584+int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd);
583585 void pre_sev_run(struct vcpu_svm *svm, int cpu);
584586 void __init sev_hardware_setup(void);
585587 void sev_hardware_teardown(void);
arch/x86/kvm/x86.c+6−1
@@ -3808,6 +3808,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · 3807 unmodified lines
38083808 #ifdef CONFIG_X86_SGX_KVM
38093809 case KVM_CAP_SGX_ATTRIBUTE:
38103810 #endif
3811+ case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
38113812 r = 1;
38123813 break;
38133814 case KVM_CAP_SET_GUEST_DEBUG2:
@@ -4714,7 +4715,6 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, · 900 unmodified lines
47144715 kvm_update_pv_runtime(vcpu);
47154716
47164717 return 0;
4717−
47184718 default:
47194719 return -EINVAL;
47204720 }
@@ -5413,6 +5413,11 @@ split_irqchip_unlock: · 692 unmodified lines
54135413 break;
54145414 }
54155415 #endif
5416+ case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
5417+ r = -EINVAL;
5418+ if (kvm_x86_ops.vm_copy_enc_context_from)
5419+ r = kvm_x86_ops.vm_copy_enc_context_from(kvm, cap->args[0]);
5420+ return r;
54165421 default:
54175422 r = -EINVAL;
54185423 break;
include/linux/kvm_host.h+1−0
@@ -654,6 +654,7 @@ void kvm_exit(void); · 653 unmodified lines
654654
655655 void kvm_get_kvm(struct kvm *kvm);
656656 void kvm_put_kvm(struct kvm *kvm);
657+bool file_is_kvm(struct file *file);
657658 void kvm_put_kvm_no_destroy(struct kvm *kvm);
658659
659660 static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id)
include/uapi/linux/kvm.h+1−0
@@ -1080,6 +1080,7 @@ struct kvm_ppc_resize_hpt { · 1079 unmodified lines
10801080 #define KVM_CAP_PPC_DAWR1 194
10811081 #define KVM_CAP_SET_GUEST_DEBUG2 195
10821082 #define KVM_CAP_SGX_ATTRIBUTE 196
1083+#define KVM_CAP_VM_COPY_ENC_CONTEXT_FROM 197
10831084
10841085 #ifdef KVM_CAP_IRQ_ROUTING
10851086
virt/kvm/kvm_main.c+6−0
@@ -4197,6 +4197,12 @@ static struct file_operations kvm_vm_fops = { · 4196 unmodified lines
41974197 KVM_COMPAT(kvm_vm_compat_ioctl),
41984198 };
41994199
4200+bool file_is_kvm(struct file *file)
4201+{
4202+ return file && file->f_op == &kvm_vm_fops;
4203+}
4204+EXPORT_SYMBOL_GPL(file_is_kvm);
4205+
42004206 static int kvm_dev_ioctl_create_vm(unsigned long type)
42014207 {
42024208 int r;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t