Skip to content

Commit 6defa24

Browse files
pgondabonzini
authored andcommitted
KVM: SEV: Init target VMCBs in sev_migrate_from
The target VMCBs during an intra-host migration need to correctly setup for running SEV and SEV-ES guests. Add sev_init_vmcb() function and make sev_es_init_vmcb() static. sev_init_vmcb() uses the now private function to init SEV-ES guests VMCBs when needed. Fixes: 0b020f5 ("KVM: SEV: Add support for SEV-ES intra host migration") Fixes: b566393 ("KVM: SEV: Add support for SEV intra host migration") Signed-off-by: Peter Gonda <[email protected]> Cc: Marc Orr <[email protected]> Cc: Paolo Bonzini <[email protected]> Cc: Sean Christopherson <[email protected]> Cc: Tom Lendacky <[email protected]> Cc: [email protected] Cc: [email protected] Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent ebdec85 commit 6defa24

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,19 +1665,24 @@ static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
16651665
{
16661666
struct kvm_sev_info *dst = &to_kvm_svm(dst_kvm)->sev_info;
16671667
struct kvm_sev_info *src = &to_kvm_svm(src_kvm)->sev_info;
1668+
struct kvm_vcpu *dst_vcpu, *src_vcpu;
1669+
struct vcpu_svm *dst_svm, *src_svm;
16681670
struct kvm_sev_info *mirror;
1671+
unsigned long i;
16691672

16701673
dst->active = true;
16711674
dst->asid = src->asid;
16721675
dst->handle = src->handle;
16731676
dst->pages_locked = src->pages_locked;
16741677
dst->enc_context_owner = src->enc_context_owner;
1678+
dst->es_active = src->es_active;
16751679

16761680
src->asid = 0;
16771681
src->active = false;
16781682
src->handle = 0;
16791683
src->pages_locked = 0;
16801684
src->enc_context_owner = NULL;
1685+
src->es_active = false;
16811686

16821687
list_cut_before(&dst->regions_list, &src->regions_list, &src->regions_list);
16831688

@@ -1704,26 +1709,21 @@ static void sev_migrate_from(struct kvm *dst_kvm, struct kvm *src_kvm)
17041709
list_del(&src->mirror_entry);
17051710
list_add_tail(&dst->mirror_entry, &owner_sev_info->mirror_vms);
17061711
}
1707-
}
17081712

1709-
static int sev_es_migrate_from(struct kvm *dst, struct kvm *src)
1710-
{
1711-
unsigned long i;
1712-
struct kvm_vcpu *dst_vcpu, *src_vcpu;
1713-
struct vcpu_svm *dst_svm, *src_svm;
1713+
kvm_for_each_vcpu(i, dst_vcpu, dst_kvm) {
1714+
dst_svm = to_svm(dst_vcpu);
17141715

1715-
if (atomic_read(&src->online_vcpus) != atomic_read(&dst->online_vcpus))
1716-
return -EINVAL;
1716+
sev_init_vmcb(dst_svm);
17171717

1718-
kvm_for_each_vcpu(i, src_vcpu, src) {
1719-
if (!src_vcpu->arch.guest_state_protected)
1720-
return -EINVAL;
1721-
}
1718+
if (!dst->es_active)
1719+
continue;
17221720

1723-
kvm_for_each_vcpu(i, src_vcpu, src) {
1721+
/*
1722+
* Note, the source is not required to have the same number of
1723+
* vCPUs as the destination when migrating a vanilla SEV VM.
1724+
*/
1725+
src_vcpu = kvm_get_vcpu(dst_kvm, i);
17241726
src_svm = to_svm(src_vcpu);
1725-
dst_vcpu = kvm_get_vcpu(dst, i);
1726-
dst_svm = to_svm(dst_vcpu);
17271727

17281728
/*
17291729
* Transfer VMSA and GHCB state to the destination. Nullify and
@@ -1740,8 +1740,23 @@ static int sev_es_migrate_from(struct kvm *dst, struct kvm *src)
17401740
src_svm->vmcb->control.vmsa_pa = INVALID_PAGE;
17411741
src_vcpu->arch.guest_state_protected = false;
17421742
}
1743-
to_kvm_svm(src)->sev_info.es_active = false;
1744-
to_kvm_svm(dst)->sev_info.es_active = true;
1743+
}
1744+
1745+
static int sev_check_source_vcpus(struct kvm *dst, struct kvm *src)
1746+
{
1747+
struct kvm_vcpu *src_vcpu;
1748+
unsigned long i;
1749+
1750+
if (!sev_es_guest(src))
1751+
return 0;
1752+
1753+
if (atomic_read(&src->online_vcpus) != atomic_read(&dst->online_vcpus))
1754+
return -EINVAL;
1755+
1756+
kvm_for_each_vcpu(i, src_vcpu, src) {
1757+
if (!src_vcpu->arch.guest_state_protected)
1758+
return -EINVAL;
1759+
}
17451760

17461761
return 0;
17471762
}
@@ -1789,11 +1804,9 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
17891804
if (ret)
17901805
goto out_dst_vcpu;
17911806

1792-
if (sev_es_guest(source_kvm)) {
1793-
ret = sev_es_migrate_from(kvm, source_kvm);
1794-
if (ret)
1795-
goto out_source_vcpu;
1796-
}
1807+
ret = sev_check_source_vcpus(kvm, source_kvm);
1808+
if (ret)
1809+
goto out_source_vcpu;
17971810

17981811
sev_migrate_from(kvm, source_kvm);
17991812
kvm_vm_dead(source_kvm);
@@ -2914,7 +2927,7 @@ int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
29142927
count, in);
29152928
}
29162929

2917-
void sev_es_init_vmcb(struct vcpu_svm *svm)
2930+
static void sev_es_init_vmcb(struct vcpu_svm *svm)
29182931
{
29192932
struct kvm_vcpu *vcpu = &svm->vcpu;
29202933

@@ -2967,6 +2980,15 @@ void sev_es_init_vmcb(struct vcpu_svm *svm)
29672980
}
29682981
}
29692982

2983+
void sev_init_vmcb(struct vcpu_svm *svm)
2984+
{
2985+
svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
2986+
clr_exception_intercept(svm, UD_VECTOR);
2987+
2988+
if (sev_es_guest(svm->vcpu.kvm))
2989+
sev_es_init_vmcb(svm);
2990+
}
2991+
29702992
void sev_es_vcpu_reset(struct vcpu_svm *svm)
29712993
{
29722994
/*

arch/x86/kvm/svm/svm.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,15 +1212,8 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
12121212
svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
12131213
}
12141214

1215-
if (sev_guest(vcpu->kvm)) {
1216-
svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
1217-
clr_exception_intercept(svm, UD_VECTOR);
1218-
1219-
if (sev_es_guest(vcpu->kvm)) {
1220-
/* Perform SEV-ES specific VMCB updates */
1221-
sev_es_init_vmcb(svm);
1222-
}
1223-
}
1215+
if (sev_guest(vcpu->kvm))
1216+
sev_init_vmcb(svm);
12241217

12251218
svm_hv_init_vmcb(vmcb);
12261219
init_vmcb_after_set_cpuid(vcpu);

arch/x86/kvm/svm/svm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,10 +649,10 @@ void __init sev_set_cpu_caps(void);
649649
void __init sev_hardware_setup(void);
650650
void sev_hardware_unsetup(void);
651651
int sev_cpu_init(struct svm_cpu_data *sd);
652+
void sev_init_vmcb(struct vcpu_svm *svm);
652653
void sev_free_vcpu(struct kvm_vcpu *vcpu);
653654
int sev_handle_vmgexit(struct kvm_vcpu *vcpu);
654655
int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);
655-
void sev_es_init_vmcb(struct vcpu_svm *svm);
656656
void sev_es_vcpu_reset(struct vcpu_svm *svm);
657657
void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
658658
void sev_es_prepare_switch_to_guest(struct sev_es_save_area *hostsa);

0 commit comments

Comments
 (0)