KVM: SEV: Prohibit migration of a VM that has mirrors
authorPaolo Bonzini <pbonzini@redhat.com>
Tue, 23 Nov 2021 00:50:34 +0000 (19:50 -0500)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 30 Nov 2021 08:54:14 +0000 (03:54 -0500)
commit17d44a96f000fe1040d4ba1c34e458c63be6b7ce
tree759824b697177936ad53c887de4604947141e8c3
parentbf42b02b19e27d6849852a41dd734af4c05e73c6
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: 0b020f5af092 ("KVM: SEV: Add support for SEV-ES intra host migration")
Message-Id: <20211123005036.2954379-11-pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/svm/sev.c
arch/x86/kvm/svm/svm.h
tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c