Skip to content

Commit 501cfe0

Browse files
committed
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: b566393 ("KVM: SEV: Add support for SEV intra host migration") Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 3e067fd commit 501cfe0

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,12 +1614,6 @@ static void sev_migrate_from(struct kvm_sev_info *dst,
16141614
src->handle = 0;
16151615
src->pages_locked = 0;
16161616

1617-
if (dst->misc_cg != src->misc_cg)
1618-
sev_misc_cg_uncharge(src);
1619-
1620-
put_misc_cg(src->misc_cg);
1621-
src->misc_cg = NULL;
1622-
16231617
INIT_LIST_HEAD(&dst->regions_list);
16241618
list_replace_init(&src->regions_list, &dst->regions_list);
16251619
}
@@ -1667,9 +1661,10 @@ static int sev_es_migrate_from(struct kvm *dst, struct kvm *src)
16671661
int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
16681662
{
16691663
struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info;
1670-
struct kvm_sev_info *src_sev;
1664+
struct kvm_sev_info *src_sev, *cg_cleanup_sev;
16711665
struct file *source_kvm_file;
16721666
struct kvm *source_kvm;
1667+
bool charged = false;
16731668
int ret;
16741669

16751670
ret = sev_lock_for_migration(kvm);
@@ -1699,10 +1694,12 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
16991694

17001695
src_sev = &to_kvm_svm(source_kvm)->sev_info;
17011696
dst_sev->misc_cg = get_current_misc_cg();
1697+
cg_cleanup_sev = dst_sev;
17021698
if (dst_sev->misc_cg != src_sev->misc_cg) {
17031699
ret = sev_misc_cg_try_charge(dst_sev);
17041700
if (ret)
1705-
goto out_dst_put_cgroup;
1701+
goto out_dst_cgroup;
1702+
charged = true;
17061703
}
17071704

17081705
ret = sev_lock_vcpus_for_migration(kvm);
@@ -1719,19 +1716,19 @@ int svm_vm_migrate_from(struct kvm *kvm, unsigned int source_fd)
17191716
}
17201717
sev_migrate_from(dst_sev, src_sev);
17211718
kvm_vm_dead(source_kvm);
1719+
cg_cleanup_sev = src_sev;
17221720
ret = 0;
17231721

17241722
out_source_vcpu:
17251723
sev_unlock_vcpus_for_migration(source_kvm);
17261724
out_dst_vcpu:
17271725
sev_unlock_vcpus_for_migration(kvm);
17281726
out_dst_cgroup:
1729-
if (ret < 0) {
1730-
sev_misc_cg_uncharge(dst_sev);
1731-
out_dst_put_cgroup:
1732-
put_misc_cg(dst_sev->misc_cg);
1733-
dst_sev->misc_cg = NULL;
1734-
}
1727+
/* Operates on the source on success, on the destination on failure. */
1728+
if (charged)
1729+
sev_misc_cg_uncharge(cg_cleanup_sev);
1730+
put_misc_cg(cg_cleanup_sev->misc_cg);
1731+
cg_cleanup_sev->misc_cg = NULL;
17351732
out_source:
17361733
sev_unlock_after_migration(source_kvm);
17371734
out_fput:

0 commit comments

Comments
 (0)