Skip to content

Commit 17d44a9

Browse files
committed
KVM: SEV: Prohibit migration of a VM that has mirrors
VMs that mirror an encryption context rely on the owner to keep the ASID allocated. Performing a KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM would cause a dangling ASID: 1. copy context from A to B (gets ref to A) 2. move context from A to L (moves ASID from A to L) 3. close L (releases ASID from L, B still references it) The right way to do the handoff instead is to create a fresh mirror VM on the destination first: 1. copy context from A to B (gets ref to A) [later] 2. close B (releases ref to A) 3. move context from A to L (moves ASID from A to L) 4. copy context from L to M So, catch the situation by adding a count of how many VMs are mirroring this one's encryption context. Fixes: 0b020f5 ("KVM: SEV: Add support for SEV-ES intra host migration") Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent bf42b02 commit 17d44a9

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,16 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
16961696
}
16971697

16981698
src_sev = &to_kvm_svm(source_kvm)->sev_info;
1699+
1700+
/*
1701+
* VMs mirroring src's encryption context rely on it to keep the
1702+
* ASID allocated, but below we are clearing src_sev->asid.
1703+
*/
1704+
if (src_sev->num_mirrored_vms) {
1705+
ret = -EBUSY;
1706+
goto out_unlock;
1707+
}
1708+
16991709
dst_sev->misc_cg = get_current_misc_cg();
17001710
cg_cleanup_sev = dst_sev;
17011711
if (dst_sev->misc_cg != src_sev->misc_cg) {
@@ -1987,6 +1997,7 @@ int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd)
19871997
*/
19881998
source_sev = &to_kvm_svm(source_kvm)->sev_info;
19891999
kvm_get_kvm(source_kvm);
2000+
source_sev->num_mirrored_vms++;
19902001

19912002
/* Set enc_context_owner and copy its encryption context over */
19922003
mirror_sev = &to_kvm_svm(kvm)->sev_info;
@@ -2019,12 +2030,21 @@ void sev_vm_destroy(struct kvm *kvm)
20192030
struct list_head *head = &sev->regions_list;
20202031
struct list_head *pos, *q;
20212032

2033+
WARN_ON(sev->num_mirrored_vms);
2034+
20222035
if (!sev_guest(kvm))
20232036
return;
20242037

20252038
/* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */
20262039
if (is_mirroring_enc_context(kvm)) {
2027-
kvm_put_kvm(sev->enc_context_owner);
2040+
struct kvm *owner_kvm = sev->enc_context_owner;
2041+
struct kvm_sev_info *owner_sev = &to_kvm_svm(owner_kvm)->sev_info;
2042+
2043+
mutex_lock(&owner_kvm->lock);
2044+
if (!WARN_ON(!owner_sev->num_mirrored_vms))
2045+
owner_sev->num_mirrored_vms--;
2046+
mutex_unlock(&owner_kvm->lock);
2047+
kvm_put_kvm(owner_kvm);
20282048
return;
20292049
}
20302050

arch/x86/kvm/svm/svm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct kvm_sev_info {
7979
struct list_head regions_list; /* List of registered regions */
8080
u64 ap_jump_table; /* SEV-ES AP Jump Table address */
8181
struct kvm *enc_context_owner; /* Owner of copied encryption context */
82+
unsigned long num_mirrored_vms; /* Number of VMs sharing this ASID */
8283
struct misc_cg *misc_cg; /* For misc cgroup accounting */
8384
atomic_t migration_in_progress;
8485
};

tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,50 @@ static void test_sev_mirror_parameters(void)
294294
kvm_vm_free(vm_no_vcpu);
295295
}
296296

297+
static void test_sev_move_copy(void)
298+
{
299+
struct kvm_vm *dst_vm, *sev_vm, *mirror_vm, *dst_mirror_vm;
300+
int ret;
301+
302+
sev_vm = sev_vm_create(/* es= */ false);
303+
dst_vm = aux_vm_create(true);
304+
mirror_vm = aux_vm_create(false);
305+
dst_mirror_vm = aux_vm_create(false);
306+
307+
sev_mirror_create(mirror_vm->fd, sev_vm->fd);
308+
ret = __sev_migrate_from(dst_vm->fd, sev_vm->fd);
309+
TEST_ASSERT(ret == -1 && errno == EBUSY,
310+
"Cannot migrate VM that has mirrors. ret %d, errno: %d\n", ret,
311+
errno);
312+
313+
/* The mirror itself can be migrated. */
314+
sev_migrate_from(dst_mirror_vm->fd, mirror_vm->fd);
315+
ret = __sev_migrate_from(dst_vm->fd, sev_vm->fd);
316+
TEST_ASSERT(ret == -1 && errno == EBUSY,
317+
"Cannot migrate VM that has mirrors. ret %d, errno: %d\n", ret,
318+
errno);
319+
320+
/*
321+
* mirror_vm is not a mirror anymore, dst_mirror_vm is. Thus,
322+
* the owner can be copied as soon as dst_mirror_vm is gone.
323+
*/
324+
kvm_vm_free(dst_mirror_vm);
325+
sev_migrate_from(dst_vm->fd, sev_vm->fd);
326+
327+
kvm_vm_free(mirror_vm);
328+
kvm_vm_free(dst_vm);
329+
kvm_vm_free(sev_vm);
330+
}
331+
297332
int main(int argc, char *argv[])
298333
{
299334
if (kvm_check_cap(KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM)) {
300335
test_sev_migrate_from(/* es= */ false);
301336
test_sev_migrate_from(/* es= */ true);
302337
test_sev_migrate_locking();
303338
test_sev_migrate_parameters();
339+
if (kvm_check_cap(KVM_CAP_VM_COPY_ENC_CONTEXT_FROM))
340+
test_sev_move_copy();
304341
}
305342
if (kvm_check_cap(KVM_CAP_VM_COPY_ENC_CONTEXT_FROM)) {
306343
test_sev_mirror(/* es= */ false);

0 commit comments

Comments
 (0)