Skip to content

Commit 34bd82a

Browse files
committed
KVM: SVM: Move SEV-ES VMSA allocation to a dedicated sev_vcpu_create() helper
Add a dedicated sev_vcpu_create() helper to allocate the VMSA page for SEV-ES+ vCPUs, and to allow for consolidating a variety of related SEV+ code in the near future. No functional change intended. Reviewed-by: Nikunj A Dadhania <nikunj@amd.com> Link: https://lore.kernel.org/r/20250819234833.3080255-5-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 7b59c73 commit 34bd82a

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

arch/x86/kvm/svm/sev.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4561,6 +4561,26 @@ void sev_init_vmcb(struct vcpu_svm *svm)
45614561
sev_es_init_vmcb(svm);
45624562
}
45634563

4564+
int sev_vcpu_create(struct kvm_vcpu *vcpu)
4565+
{
4566+
struct vcpu_svm *svm = to_svm(vcpu);
4567+
struct page *vmsa_page;
4568+
4569+
if (!sev_es_guest(vcpu->kvm))
4570+
return 0;
4571+
4572+
/*
4573+
* SEV-ES guests require a separate (from the VMCB) VMSA page used to
4574+
* contain the encrypted register state of the guest.
4575+
*/
4576+
vmsa_page = snp_safe_alloc_page();
4577+
if (!vmsa_page)
4578+
return -ENOMEM;
4579+
4580+
svm->sev_es.vmsa = page_address(vmsa_page);
4581+
return 0;
4582+
}
4583+
45644584
void sev_es_vcpu_reset(struct vcpu_svm *svm)
45654585
{
45664586
struct kvm_vcpu *vcpu = &svm->vcpu;

arch/x86/kvm/svm/svm.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,6 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
12751275
{
12761276
struct vcpu_svm *svm;
12771277
struct page *vmcb01_page;
1278-
struct page *vmsa_page = NULL;
12791278
int err;
12801279

12811280
BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
@@ -1286,24 +1285,18 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
12861285
if (!vmcb01_page)
12871286
goto out;
12881287

1289-
if (sev_es_guest(vcpu->kvm)) {
1290-
/*
1291-
* SEV-ES guests require a separate VMSA page used to contain
1292-
* the encrypted register state of the guest.
1293-
*/
1294-
vmsa_page = snp_safe_alloc_page();
1295-
if (!vmsa_page)
1296-
goto error_free_vmcb_page;
1297-
}
1288+
err = sev_vcpu_create(vcpu);
1289+
if (err)
1290+
goto error_free_vmcb_page;
12981291

12991292
err = avic_init_vcpu(svm);
13001293
if (err)
1301-
goto error_free_vmsa_page;
1294+
goto error_free_sev;
13021295

13031296
svm->msrpm = svm_vcpu_alloc_msrpm();
13041297
if (!svm->msrpm) {
13051298
err = -ENOMEM;
1306-
goto error_free_vmsa_page;
1299+
goto error_free_sev;
13071300
}
13081301

13091302
svm->x2avic_msrs_intercepted = true;
@@ -1312,16 +1305,12 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
13121305
svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
13131306
svm_switch_vmcb(svm, &svm->vmcb01);
13141307

1315-
if (vmsa_page)
1316-
svm->sev_es.vmsa = page_address(vmsa_page);
1317-
13181308
svm->guest_state_loaded = false;
13191309

13201310
return 0;
13211311

1322-
error_free_vmsa_page:
1323-
if (vmsa_page)
1324-
__free_page(vmsa_page);
1312+
error_free_sev:
1313+
sev_free_vcpu(vcpu);
13251314
error_free_vmcb_page:
13261315
__free_page(vmcb01_page);
13271316
out:

arch/x86/kvm/svm/svm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ static inline struct page *snp_safe_alloc_page(void)
854854
return snp_safe_alloc_page_node(numa_node_id(), GFP_KERNEL_ACCOUNT);
855855
}
856856

857+
int sev_vcpu_create(struct kvm_vcpu *vcpu);
857858
void sev_free_vcpu(struct kvm_vcpu *vcpu);
858859
void sev_vm_destroy(struct kvm *kvm);
859860
void __init sev_set_cpu_caps(void);
@@ -880,6 +881,7 @@ static inline struct page *snp_safe_alloc_page(void)
880881
return snp_safe_alloc_page_node(numa_node_id(), GFP_KERNEL_ACCOUNT);
881882
}
882883

884+
static inline int sev_vcpu_create(struct kvm_vcpu *vcpu) { return 0; }
883885
static inline void sev_free_vcpu(struct kvm_vcpu *vcpu) {}
884886
static inline void sev_vm_destroy(struct kvm *kvm) {}
885887
static inline void __init sev_set_cpu_caps(void) {}

0 commit comments

Comments
 (0)