Skip to content

Commit bf42b02

Browse files
committed
KVM: SEV: Do COPY_ENC_CONTEXT_FROM with both VMs locked
Now that we have a facility to lock two VMs with deadlock protection, use it for the creation of mirror VMs as well. One of COPY_ENC_CONTEXT_FROM(dst, src) and COPY_ENC_CONTEXT_FROM(src, dst) would always fail, so the combination is nonsensical and it is okay to return -EBUSY if it is attempted. This sidesteps the question of what happens if a VM is MOVE_ENC_CONTEXT_FROM'd at the same time as it is COPY_ENC_CONTEXT_FROM'd: the locking prevents that from happening. Cc: Peter Gonda <[email protected]> Cc: Sean Christopherson <[email protected]> Reviewed-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent dc79c9f commit bf42b02

File tree

1 file changed

+24
-42
lines changed

1 file changed

+24
-42
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,77 +1955,59 @@ int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd)
19551955
{
19561956
struct file *source_kvm_file;
19571957
struct kvm *source_kvm;
1958-
struct kvm_sev_info source_sev, *mirror_sev;
1958+
struct kvm_sev_info *source_sev, *mirror_sev;
19591959
int ret;
19601960

19611961
source_kvm_file = fget(source_fd);
19621962
if (!file_is_kvm(source_kvm_file)) {
19631963
ret = -EBADF;
1964-
goto e_source_put;
1964+
goto e_source_fput;
19651965
}
19661966

19671967
source_kvm = source_kvm_file->private_data;
1968-
mutex_lock(&source_kvm->lock);
1969-
1970-
if (!sev_guest(source_kvm)) {
1971-
ret = -EINVAL;
1972-
goto e_source_unlock;
1973-
}
1968+
ret = sev_lock_two_vms(kvm, source_kvm);
1969+
if (ret)
1970+
goto e_source_fput;
19741971

1975-
/* Mirrors of mirrors should work, but let's not get silly */
1976-
if (is_mirroring_enc_context(source_kvm) || source_kvm == kvm) {
1972+
/*
1973+
* Mirrors of mirrors should work, but let's not get silly. Also
1974+
* disallow out-of-band SEV/SEV-ES init if the target is already an
1975+
* SEV guest, or if vCPUs have been created. KVM relies on vCPUs being
1976+
* created after SEV/SEV-ES initialization, e.g. to init intercepts.
1977+
*/
1978+
if (sev_guest(kvm) || !sev_guest(source_kvm) ||
1979+
is_mirroring_enc_context(source_kvm) || kvm->created_vcpus) {
19771980
ret = -EINVAL;
1978-
goto e_source_unlock;
1981+
goto e_unlock;
19791982
}
19801983

1981-
memcpy(&source_sev, &to_kvm_svm(source_kvm)->sev_info,
1982-
sizeof(source_sev));
1983-
19841984
/*
19851985
* The mirror kvm holds an enc_context_owner ref so its asid can't
19861986
* disappear until we're done with it
19871987
*/
1988+
source_sev = &to_kvm_svm(source_kvm)->sev_info;
19881989
kvm_get_kvm(source_kvm);
19891990

1990-
fput(source_kvm_file);
1991-
mutex_unlock(&source_kvm->lock);
1992-
mutex_lock(&kvm->lock);
1993-
1994-
/*
1995-
* Disallow out-of-band SEV/SEV-ES init if the target is already an
1996-
* SEV guest, or if vCPUs have been created. KVM relies on vCPUs being
1997-
* created after SEV/SEV-ES initialization, e.g. to init intercepts.
1998-
*/
1999-
if (sev_guest(kvm) || kvm->created_vcpus) {
2000-
ret = -EINVAL;
2001-
goto e_mirror_unlock;
2002-
}
2003-
20041991
/* Set enc_context_owner and copy its encryption context over */
20051992
mirror_sev = &to_kvm_svm(kvm)->sev_info;
20061993
mirror_sev->enc_context_owner = source_kvm;
20071994
mirror_sev->active = true;
2008-
mirror_sev->asid = source_sev.asid;
2009-
mirror_sev->fd = source_sev.fd;
2010-
mirror_sev->es_active = source_sev.es_active;
2011-
mirror_sev->handle = source_sev.handle;
1995+
mirror_sev->asid = source_sev->asid;
1996+
mirror_sev->fd = source_sev->fd;
1997+
mirror_sev->es_active = source_sev->es_active;
1998+
mirror_sev->handle = source_sev->handle;
20121999
INIT_LIST_HEAD(&mirror_sev->regions_list);
2000+
ret = 0;
2001+
20132002
/*
20142003
* Do not copy ap_jump_table. Since the mirror does not share the same
20152004
* KVM contexts as the original, and they may have different
20162005
* memory-views.
20172006
*/
20182007

2019-
mutex_unlock(&kvm->lock);
2020-
return 0;
2021-
2022-
e_mirror_unlock:
2023-
mutex_unlock(&kvm->lock);
2024-
kvm_put_kvm(source_kvm);
2025-
return ret;
2026-
e_source_unlock:
2027-
mutex_unlock(&source_kvm->lock);
2028-
e_source_put:
2008+
e_unlock:
2009+
sev_unlock_two_vms(kvm, source_kvm);
2010+
e_source_fput:
20292011
if (source_kvm_file)
20302012
fput(source_kvm_file);
20312013
return ret;

0 commit comments

Comments
 (0)