Commit 501cfe06 authored by Paolo Bonzini's avatar Paolo Bonzini

KVM: SEV: unify cgroup cleanup code for svm_vm_migrate_from

Use the same cleanup code independent of whether the cgroup to be
uncharged and unref'd is the source or the destination cgroup.  Use a
bool to track whether the destination cgroup has been charged, which also
fixes a bug in the error case: the destination cgroup must be uncharged
only if it does not match the source.

Fixes: b5663931 ("KVM: SEV: Add support for SEV intra host migration")
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 3e067fd8
...@@ -1614,12 +1614,6 @@ static void sev_migrate_from(struct kvm_sev_info *dst, ...@@ -1614,12 +1614,6 @@ static void sev_migrate_from(struct kvm_sev_info *dst,
src->handle = 0; src->handle = 0;
src->pages_locked = 0; src->pages_locked = 0;
if (dst->misc_cg != src->misc_cg)
sev_misc_cg_uncharge(src);
put_misc_cg(src->misc_cg);
src->misc_cg = NULL;
INIT_LIST_HEAD(&dst->regions_list); INIT_LIST_HEAD(&dst->regions_list);
list_replace_init(&src->regions_list, &dst->regions_list); list_replace_init(&src->regions_list, &dst->regions_list);
} }
...@@ -1667,9 +1661,10 @@ static int sev_es_migrate_from(struct kvm *dst, struct kvm *src) ...@@ -1667,9 +1661,10 @@ static int sev_es_migrate_from(struct kvm *dst, struct kvm *src)
int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd) int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
{ {
struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info; struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info;
struct kvm_sev_info *src_sev; struct kvm_sev_info *src_sev, *cg_cleanup_sev;
struct file *source_kvm_file; struct file *source_kvm_file;
struct kvm *source_kvm; struct kvm *source_kvm;
bool charged = false;
int ret; int ret;
ret = sev_lock_for_migration(kvm); ret = sev_lock_for_migration(kvm);
...@@ -1699,10 +1694,12 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd) ...@@ -1699,10 +1694,12 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
src_sev = &to_kvm_svm(source_kvm)->sev_info; src_sev = &to_kvm_svm(source_kvm)->sev_info;
dst_sev->misc_cg = get_current_misc_cg(); dst_sev->misc_cg = get_current_misc_cg();
cg_cleanup_sev = dst_sev;
if (dst_sev->misc_cg != src_sev->misc_cg) { if (dst_sev->misc_cg != src_sev->misc_cg) {
ret = sev_misc_cg_try_charge(dst_sev); ret = sev_misc_cg_try_charge(dst_sev);
if (ret) if (ret)
goto out_dst_put_cgroup; goto out_dst_cgroup;
charged = true;
} }
ret = sev_lock_vcpus_for_migration(kvm); ret = sev_lock_vcpus_for_migration(kvm);
...@@ -1719,6 +1716,7 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd) ...@@ -1719,6 +1716,7 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
} }
sev_migrate_from(dst_sev, src_sev); sev_migrate_from(dst_sev, src_sev);
kvm_vm_dead(source_kvm); kvm_vm_dead(source_kvm);
cg_cleanup_sev = src_sev;
ret = 0; ret = 0;
out_source_vcpu: out_source_vcpu:
...@@ -1726,12 +1724,11 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd) ...@@ -1726,12 +1724,11 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
out_dst_vcpu: out_dst_vcpu:
sev_unlock_vcpus_for_migration(kvm); sev_unlock_vcpus_for_migration(kvm);
out_dst_cgroup: out_dst_cgroup:
if (ret < 0) { /* Operates on the source on success, on the destination on failure. */
sev_misc_cg_uncharge(dst_sev); if (charged)
out_dst_put_cgroup: sev_misc_cg_uncharge(cg_cleanup_sev);
put_misc_cg(dst_sev->misc_cg); put_misc_cg(cg_cleanup_sev->misc_cg);
dst_sev->misc_cg = NULL; cg_cleanup_sev->misc_cg = NULL;
}
out_source: out_source:
sev_unlock_after_migration(source_kvm); sev_unlock_after_migration(source_kvm);
out_fput: out_fput:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment