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
6207This capability can be used to check / enable 2nd DAWR feature provided6207This capability can be used to check / enable 2nd DAWR feature provided
6208by POWER10 processor.6208by POWER10 processor.
6209 6209
62107.24 KVM_CAP_VM_COPY_ENC_CONTEXT_FROM
6211-------------------------------------
6212
6213Architectures: x86 SEV enabled
6214Type: vm
6215Parameters: args[0] is the fd of the source vm
6216Returns: 0 on success; ENOTTY on error
6217
6218This capability enables userspace to copy encryption context from the vm
6219indicated by the fd to the vm this is called on.
6220
6221This is intended to support in-guest workloads scheduled by the host. This
6222allows the in-guest workload to maintain its own NPTs and keeps the two vms
6223from accidentally clobbering each other with interrupts and the like (separate
6224APIC/MSRs/etc).
6225
62107.25 KVM_CAP_SGX_ATTRIBUTE62267.25 KVM_CAP_SGX_ATTRIBUTE
6211----------------------6227----------------------
6212 6228
@@ -6749,4 +6765,4 @@ in the kernel based fast path. If they can not be handled by the kernel, · 536 unmodified lines
6749they will get passed on to user space. So user space still has to have6765they will get passed on to user space. So user space still has to have
6750an implementation for these despite the in kernel acceleration.6766an implementation for these despite the in kernel acceleration.
6751 6767
6752This capability is always enabled.6768This capability is always enabled.
arch/x86/include/asm/kvm_host.h+1−0
@@ -1349,6 +1349,7 @@ struct kvm_x86_ops { · 1348 unmodified lines
1349 int (*mem_enc_op)(struct kvm *kvm, void __user *argp);1349 int (*mem_enc_op)(struct kvm *kvm, void __user *argp);
1350 int (*mem_enc_reg_region)(struct kvm *kvm, struct kvm_enc_region *argp);1350 int (*mem_enc_reg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
1351 int (*mem_enc_unreg_region)(struct kvm *kvm, struct kvm_enc_region *argp);1351 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);
1352 1353
1353 int (*get_msr_feature)(struct kvm_msr_entry *entry);1354 int (*get_msr_feature)(struct kvm_msr_entry *entry);
1354 1355
arch/x86/kvm/svm/sev.c+90−0
@@ -66,6 +66,11 @@ static int sev_flush_asids(void) · 65 unmodified lines
66 return ret;66 return ret;
67}67}
68 68
69static inline bool is_mirroring_enc_context(struct kvm *kvm)
70{
71 return !!to_kvm_svm(kvm)->sev_info.enc_context_owner;
72}
73
69/* Must be called with the sev_bitmap_lock held */74/* Must be called with the sev_bitmap_lock held */
70static bool __sev_recycle_asids(int min_asid, int max_asid)75static bool __sev_recycle_asids(int min_asid, int max_asid)
71{76{
@@ -1122,6 +1127,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) · 1050 unmodified lines
1122 1127
1123 mutex_lock(&kvm->lock);1128 mutex_lock(&kvm->lock);
1124 1129
1130 /* enc_context_owner handles all memory enc operations */
1131 if (is_mirroring_enc_context(kvm)) {
1132 r = -EINVAL;
1133 goto out;
1134 }
1135
1125 switch (sev_cmd.id) {1136 switch (sev_cmd.id) {
1126 case KVM_SEV_ES_INIT:1137 case KVM_SEV_ES_INIT:
1127 if (!sev_es) {1138 if (!sev_es) {
@@ -1185,6 +1196,10 @@ int svm_register_enc_region(struct kvm *kvm, · 57 unmodified lines
1185 if (!sev_guest(kvm))1196 if (!sev_guest(kvm))
1186 return -ENOTTY;1197 return -ENOTTY;
1187 1198
1199 /* If kvm is mirroring encryption context it isn't responsible for it */
1200 if (is_mirroring_enc_context(kvm))
1201 return -EINVAL;
1202
1188 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)1203 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
1189 return -EINVAL;1204 return -EINVAL;
1190 1205
@@ -1251,6 +1266,10 @@ int svm_unregister_enc_region(struct kvm *kvm, · 60 unmodified lines
1251 struct enc_region *region;1266 struct enc_region *region;
1252 int ret;1267 int ret;
1253 1268
1269 /* If kvm is mirroring encryption context it isn't responsible for it */
1270 if (is_mirroring_enc_context(kvm))
1271 return -EINVAL;
1272
1254 mutex_lock(&kvm->lock);1273 mutex_lock(&kvm->lock);
1255 1274
1256 if (!sev_guest(kvm)) {1275 if (!sev_guest(kvm)) {
@@ -1281,6 +1300,71 @@ failed: · 24 unmodified lines
1281 return ret;1300 return ret;
1282}1301}
1283 1302
1303int 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
1357e_mirror_unlock:
1358 mutex_unlock(&kvm->lock);
1359 kvm_put_kvm(source_kvm);
1360 return ret;
1361e_source_unlock:
1362 mutex_unlock(&source_kvm->lock);
1363e_source_put:
1364 fput(source_kvm_file);
1365 return ret;
1366}
1367
1284void sev_vm_destroy(struct kvm *kvm)1368void sev_vm_destroy(struct kvm *kvm)
1285{1369{
1286 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;1370 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
1290 if (!sev_guest(kvm))1374 if (!sev_guest(kvm))
1291 return;1375 return;
1292 1376
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
1293 mutex_lock(&kvm->lock);1383 mutex_lock(&kvm->lock);
1294 1384
1295 /*1385 /*
arch/x86/kvm/svm/svm.c+2−0
@@ -4589,6 +4589,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { · 4588 unmodified lines
4589 .mem_enc_reg_region = svm_register_enc_region,4589 .mem_enc_reg_region = svm_register_enc_region,
4590 .mem_enc_unreg_region = svm_unregister_enc_region,4590 .mem_enc_unreg_region = svm_unregister_enc_region,
4591 4591
4592 .vm_copy_enc_context_from = svm_vm_copy_asid_from,
4593
4592 .can_emulate_instruction = svm_can_emulate_instruction,4594 .can_emulate_instruction = svm_can_emulate_instruction,
4593 4595
4594 .apic_init_signal_blocked = svm_apic_init_signal_blocked,4596 .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
68 unsigned long pages_locked; /* Number of pages locked */68 unsigned long pages_locked; /* Number of pages locked */
69 struct list_head regions_list; /* List of registered regions */69 struct list_head regions_list; /* List of registered regions */
70 u64 ap_jump_table; /* SEV-ES AP Jump Table address */70 u64 ap_jump_table; /* SEV-ES AP Jump Table address */
71 struct kvm *enc_context_owner; /* Owner of copied encryption context */
71};72};
72 73
73struct kvm_svm {74struct kvm_svm {
@@ -580,6 +581,7 @@ int svm_register_enc_region(struct kvm *kvm, · 506 unmodified lines
580 struct kvm_enc_region *range);581 struct kvm_enc_region *range);
581int svm_unregister_enc_region(struct kvm *kvm,582int svm_unregister_enc_region(struct kvm *kvm,
582 struct kvm_enc_region *range);583 struct kvm_enc_region *range);
584int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd);
583void pre_sev_run(struct vcpu_svm *svm, int cpu);585void pre_sev_run(struct vcpu_svm *svm, int cpu);
584void __init sev_hardware_setup(void);586void __init sev_hardware_setup(void);
585void sev_hardware_teardown(void);587void 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
3808#ifdef CONFIG_X86_SGX_KVM3808#ifdef CONFIG_X86_SGX_KVM
3809 case KVM_CAP_SGX_ATTRIBUTE:3809 case KVM_CAP_SGX_ATTRIBUTE:
3810#endif3810#endif
3811 case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM:
3811 r = 1;3812 r = 1;
3812 break;3813 break;
3813 case KVM_CAP_SET_GUEST_DEBUG2:3814 case KVM_CAP_SET_GUEST_DEBUG2:
@@ -4714,7 +4715,6 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, · 900 unmodified lines
4714 kvm_update_pv_runtime(vcpu);4715 kvm_update_pv_runtime(vcpu);
4715 4716
4716 return 0;4717 return 0;
4717
4718 default:4718 default:
4719 return -EINVAL;4719 return -EINVAL;
4720 }4720 }
@@ -5413,6 +5413,11 @@ split_irqchip_unlock: · 692 unmodified lines
5413 break;5413 break;
5414 }5414 }
5415#endif5415#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;
5416 default:5421 default:
5417 r = -EINVAL;5422 r = -EINVAL;
5418 break;5423 break;
include/linux/kvm_host.h+1−0
@@ -654,6 +654,7 @@ void kvm_exit(void); · 653 unmodified lines
654 654
655void kvm_get_kvm(struct kvm *kvm);655void kvm_get_kvm(struct kvm *kvm);
656void kvm_put_kvm(struct kvm *kvm);656void kvm_put_kvm(struct kvm *kvm);
657bool file_is_kvm(struct file *file);
657void kvm_put_kvm_no_destroy(struct kvm *kvm);658void kvm_put_kvm_no_destroy(struct kvm *kvm);
658 659
659static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id)660static 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
1080#define KVM_CAP_PPC_DAWR1 1941080#define KVM_CAP_PPC_DAWR1 194
1081#define KVM_CAP_SET_GUEST_DEBUG2 1951081#define KVM_CAP_SET_GUEST_DEBUG2 195
1082#define KVM_CAP_SGX_ATTRIBUTE 1961082#define KVM_CAP_SGX_ATTRIBUTE 196
1083#define KVM_CAP_VM_COPY_ENC_CONTEXT_FROM 197
1083 1084
1084#ifdef KVM_CAP_IRQ_ROUTING1085#ifdef KVM_CAP_IRQ_ROUTING
1085 1086
virt/kvm/kvm_main.c+6−0
@@ -4197,6 +4197,12 @@ static struct file_operations kvm_vm_fops = { · 4196 unmodified lines
4197 KVM_COMPAT(kvm_vm_compat_ioctl),4197 KVM_COMPAT(kvm_vm_compat_ioctl),
4198};4198};
4199 4199
4200bool file_is_kvm(struct file *file)
4201{
4202 return file && file->f_op == &kvm_vm_fops;
4203}
4204EXPORT_SYMBOL_GPL(file_is_kvm);
4205
4200static int kvm_dev_ioctl_create_vm(unsigned long type)4206static int kvm_dev_ioctl_create_vm(unsigned long type)
4201{4207{
4202 int r;4208 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