[RFC PATCH v2 09/13] KVM: x86: Refactor common migration preparation code out of sev_vm_move_enc_context_from

From: Ryan Afranji
Date: Fri May 16 2025 - 15:22:49 EST


From: Ackerley Tng <ackerleytng@xxxxxxxxxx>

Co-developed-by: Sagi Shahar <sagis@xxxxxxxxxx>
Signed-off-by: Sagi Shahar <sagis@xxxxxxxxxx>
Co-developed-by: Vishal Annapurve <vannapurve@xxxxxxxxxx>
Signed-off-by: Vishal Annapurve <vannapurve@xxxxxxxxxx>
Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
Signed-off-by: Ryan Afranji <afranji@xxxxxxxxxx>
---
arch/x86/include/asm/kvm_host.h | 2 +-
arch/x86/kvm/svm/sev.c | 29 +++---------------------
arch/x86/kvm/svm/svm.h | 2 +-
arch/x86/kvm/x86.c | 39 ++++++++++++++++++++++++++++++++-
4 files changed, 43 insertions(+), 29 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 6c06f3d6e081..179618300270 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1871,7 +1871,7 @@ struct kvm_x86_ops {
int (*mem_enc_register_region)(struct kvm *kvm, struct kvm_enc_region *argp);
int (*mem_enc_unregister_region)(struct kvm *kvm, struct kvm_enc_region *argp);
int (*vm_copy_enc_context_from)(struct kvm *kvm, unsigned int source_fd);
- int (*vm_move_enc_context_from)(struct kvm *kvm, unsigned int source_fd);
+ int (*vm_move_enc_context_from)(struct kvm *kvm, struct kvm *source_kvm);
void (*guest_memory_reclaimed)(struct kvm *kvm);

int (*get_feature_msr)(u32 msr, u64 *data);
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index b3048ec411e2..689521d9e26f 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -2000,34 +2000,15 @@ static int sev_check_source_vcpus(struct kvm *dst, struct kvm *src)
return 0;
}

-int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
+int sev_vm_move_enc_context_from(struct kvm *kvm, struct kvm *source_kvm)
{
struct kvm_sev_info *dst_sev = to_kvm_sev_info(kvm);
struct kvm_sev_info *src_sev, *cg_cleanup_sev;
- CLASS(fd, f)(source_fd);
- struct kvm *source_kvm;
bool charged = false;
int ret;

- if (fd_empty(f))
- return -EBADF;
-
- if (!file_is_kvm(fd_file(f)))
- return -EBADF;
-
- source_kvm = fd_file(f)->private_data;
- ret = kvm_mark_migration_in_progress(kvm, source_kvm);
- if (ret)
- return ret;
- ret = kvm_lock_two_vms(kvm, source_kvm);
- if (ret)
- goto out_mark_migration_done;
-
- if (kvm->arch.vm_type != source_kvm->arch.vm_type ||
- sev_guest(kvm) || !sev_guest(source_kvm)) {
- ret = -EINVAL;
- goto out_unlock;
- }
+ if (sev_guest(kvm) || !sev_guest(source_kvm))
+ return -EINVAL;

src_sev = to_kvm_sev_info(source_kvm);

@@ -2066,10 +2047,6 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
sev_misc_cg_uncharge(cg_cleanup_sev);
put_misc_cg(cg_cleanup_sev->misc_cg);
cg_cleanup_sev->misc_cg = NULL;
-out_unlock:
- kvm_unlock_two_vms(kvm, source_kvm);
-out_mark_migration_done:
- kvm_mark_migration_done(kvm, source_kvm);
return ret;
}

diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 35df8be621c5..7bd31c0b135a 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -757,7 +757,7 @@ int sev_mem_enc_register_region(struct kvm *kvm,
int sev_mem_enc_unregister_region(struct kvm *kvm,
struct kvm_enc_region *range);
int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd);
-int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd);
+int sev_vm_move_enc_context_from(struct kvm *kvm, struct kvm *source_kvm);
void sev_guest_memory_reclaimed(struct kvm *kvm);
int sev_handle_vmgexit(struct kvm_vcpu *vcpu);

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 422c66a033d2..637540309456 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6597,6 +6597,43 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
return 0;
}

+static int kvm_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
+{
+ int r;
+ struct kvm *source_kvm;
+ struct fd f = fdget(source_fd);
+ struct file *file = fd_file(f);
+
+ r = -EBADF;
+ if (!file)
+ return r;
+
+ if (!file_is_kvm(file))
+ goto out_fdput;
+
+ r = -EINVAL;
+ source_kvm = file->private_data;
+ if (kvm->arch.vm_type != source_kvm->arch.vm_type)
+ goto out_fdput;
+
+ r = kvm_mark_migration_in_progress(kvm, source_kvm);
+ if (r)
+ goto out_fdput;
+
+ r = kvm_lock_two_vms(kvm, source_kvm);
+ if (r)
+ goto out_mark_migration_done;
+
+ r = kvm_x86_call(vm_move_enc_context_from)(kvm, source_kvm);
+
+ kvm_unlock_two_vms(kvm, source_kvm);
+out_mark_migration_done:
+ kvm_mark_migration_done(kvm, source_kvm);
+out_fdput:
+ fdput(f);
+ return r;
+}
+
int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
struct kvm_enable_cap *cap)
{
@@ -6738,7 +6775,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
if (!kvm_x86_ops.vm_move_enc_context_from)
break;

- r = kvm_x86_call(vm_move_enc_context_from)(kvm, cap->args[0]);
+ r = kvm_vm_move_enc_context_from(kvm, cap->args[0]);
break;
case KVM_CAP_EXIT_HYPERCALL:
if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) {
--
2.49.0.1101.gccaa498523-goog