KVM: SEV: Fall back to __vmalloc() for SEV-ES scratch
Sean Christopherson
maintainercover KVM: SEV: Fall back to __vmalloc() for SEV-ES scratch
Two cleanups related to handling failure to setup the GHCB scratch area for SEV-ES. Found by inspection when reviewing the SNP series. Compile tested only.
Sean Christopherson (2):
KVM: SEV: Return appropriate error codes if SEV-ES scratch setup fails
KVM: SEV: Fall back to __vmalloc() for SEV-ES scratch area if
necessary
arch/x86/kvm/svm/sev.c | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-)
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
Sean Christopherson
maintainerPATCH 1/2 KVM: SEV: Return appropriate error codes if SEV-ES scratch setup fails
Return appropriate error codes if setting up the GHCB scratch area for an SEV-ES guest fails. In particular, returning -EINVAL instead of -ENOMEM when allocating the kernel buffer could be confusing as userspace would likely suspect a guest issue.
--- arch/x86/kvm/svm/sev.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)
arch/x86/kvm/svm/sev.c +18 -13
@@ -2299,7 +2299,7 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu) }#define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE) -static bool setup_vmgexit_scratch (struct vcpu_svm *svm,bool sync,u64 len) +static int setup_vmgexit_scratch (struct vcpu_svm *svm,bool sync,u64 len) {struct vmcb_control_area *control = &svm->vmcb->control;struct ghcb *ghcb = svm->ghcb;
@@ -2310,14 +2310,14 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_gpa_beg =ghcb_get_sw_scratch (ghcb);if (!scratch_gpa_beg) {pr_err ("vmgexit: scratch gpa not provided\n" ); -return false; +return -EINVAL ; } scratch_gpa_end = scratch_gpa_beg + len;if (scratch_gpa_end < scratch_gpa_beg) {pr_err ("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n" , len, scratch_gpa_beg); -return false; +return -EINVAL ; }if ((scratch_gpa_beg &PAGE_MASK ) == control->ghcb_gpa) {
@@ -2335,7 +2335,7 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_gpa_end > ghcb_scratch_end) {pr_err ("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n" , scratch_gpa_beg, scratch_gpa_end); -return false; +return -EINVAL ; } scratch_va = (void *)svm->ghcb;
@@ -2348,18 +2348,18 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)if (len >GHCB_SCRATCH_AREA_LIMIT ) {pr_err ("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n" , len,GHCB_SCRATCH_AREA_LIMIT ); -return false; +return -EINVAL ; } scratch_va =kzalloc (len,GFP_KERNEL_ACCOUNT );if (!scratch_va) -return false; +return -ENOMEM ;if (kvm_read_guest (svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {/* Unable to copy scratch area from guest */ pr_err ("vmgexit: kvm_read_guest for scratch area failed\n" );kfree (scratch_va); -return false; +return -EFAULT ; }/*
@@ -2375,7 +2375,7 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) svm->ghcb_sa = scratch_va; svm->ghcb_sa_len = len; -return true; +return 0 ; } static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,
@@ -2514,10 +2514,10 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)ghcb_set_sw_exit_info_1 (ghcb,0 );ghcb_set_sw_exit_info_2 (ghcb,0 ); - ret = -EINVAL ;switch (exit_code) {case SVM_VMGEXIT_MMIO_READ : -if (!setup_vmgexit_scratch (svm, true, control->exit_info_2)) + ret =setup_vmgexit_scratch (svm, true, control->exit_info_2); +if (ret)break ; ret = kvm_sev_es_mmio_read(vcpu,
@@ -2526,7 +2526,8 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) svm->ghcb_sa);break ;case SVM_VMGEXIT_MMIO_WRITE : -if (!setup_vmgexit_scratch (svm, false, control->exit_info_2)) + ret =setup_vmgexit_scratch (svm, false, control->exit_info_2); +if (ret)break ; ret = kvm_sev_es_mmio_write(vcpu,
@@ -2569,6 +2570,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)vcpu_unimpl (vcpu,"vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n" , control->exit_info_1, control->exit_info_2); + ret = -EINVAL ;break ;default : ret =svm_invoke_exit_handler (vcpu, exit_code);
@@ -2579,8 +2581,11 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)int sev_es_string_io (struct vcpu_svm *svm,int size,unsigned int port,int in) { -if (!setup_vmgexit_scratch (svm, in, svm->vmcb->control.exit_info_2)) -return -EINVAL ; +int r; + + r =setup_vmgexit_scratch (svm, in, svm->vmcb->control.exit_info_2); +if (r) +return r;return kvm_sev_es_string_io (&svm->vcpu, size, port, svm->ghcb_sa, svm->ghcb_sa_len / size, in);
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
Re: [PATCH 1/2] KVM: SEV: Return appropriate error codes if SEV-ES scratch setup fails
Quoted 4 lines · Sean Christopherson
Return appropriate error codes if setting up the GHCB scratch area for an SEV-ES guest fails. In particular, returning
-EINVALinstead of-ENOMEMwhen allocating the kernel buffer could be confusing as userspace would likely suspect a guest issue.
Based on previous feedback and to implement the changes to the GHCB specification, I'm planning on submitting a patch that will return an error code back to the guest, instead of terminating the guest, if the scratch area fails to be setup properly. So you could hold off on this patch if you want.
Thanks,
Tom
Quoted 124 lines
Fixes 8f423a80d299 ("KVM: SVM: Support MMIO for an SEV-ES guest")Cc Tom Lendacky <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/sev.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)arch/x86/kvm/svm/sev.c +18
-13@@ -2299,7 +2299,7 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu) }#define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE) -static bool setup_vmgexit_scratch (struct vcpu_svm *svm,bool sync,u64 len) +static int setup_vmgexit_scratch (struct vcpu_svm *svm,bool sync,u64 len) {struct vmcb_control_area *control = &svm->vmcb->control;struct ghcb *ghcb = svm->ghcb;@@ -2310,14 +2310,14 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_gpa_beg =ghcb_get_sw_scratch (ghcb);if (!scratch_gpa_beg) {pr_err ("vmgexit: scratch gpa not provided\n" ); -return false; +return -EINVAL ; } scratch_gpa_end = scratch_gpa_beg + len;if (scratch_gpa_end < scratch_gpa_beg) {pr_err ("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n" , len, scratch_gpa_beg); -return false; +return -EINVAL ; }if ((scratch_gpa_beg &PAGE_MASK ) == control->ghcb_gpa) {@@ -2335,7 +2335,7 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_gpa_end > ghcb_scratch_end) {pr_err ("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n" , scratch_gpa_beg, scratch_gpa_end); -return false; +return -EINVAL ; } scratch_va = (void *)svm->ghcb;@@ -2348,18 +2348,18 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)if (len >GHCB_SCRATCH_AREA_LIMIT ) {pr_err ("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n" , len,GHCB_SCRATCH_AREA_LIMIT ); -return false; +return -EINVAL ; } scratch_va =kzalloc (len,GFP_KERNEL_ACCOUNT );if (!scratch_va) -return false; +return -ENOMEM ;if (kvm_read_guest (svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) {/* Unable to copy scratch area from guest */ pr_err ("vmgexit: kvm_read_guest for scratch area failed\n" );kfree (scratch_va); -return false; +return -EFAULT ; }/* @@ -2375,7 +2375,7 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) svm->ghcb_sa = scratch_va; svm->ghcb_sa_len = len; -return true; +return 0 ; } static void set_ghcb_msr_bits(struct vcpu_svm *svm, u64 value, u64 mask,@@ -2514,10 +2514,10 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)ghcb_set_sw_exit_info_1 (ghcb,0 );ghcb_set_sw_exit_info_2 (ghcb,0 ); - ret = -EINVAL ;switch (exit_code) {case SVM_VMGEXIT_MMIO_READ : -if (!setup_vmgexit_scratch (svm, true, control->exit_info_2)) + ret =setup_vmgexit_scratch (svm, true, control->exit_info_2); +if (ret)break ; ret = kvm_sev_es_mmio_read(vcpu,@@ -2526,7 +2526,8 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) svm->ghcb_sa);break ;case SVM_VMGEXIT_MMIO_WRITE : -if (!setup_vmgexit_scratch (svm, false, control->exit_info_2)) + ret =setup_vmgexit_scratch (svm, false, control->exit_info_2); +if (ret)break ; ret = kvm_sev_es_mmio_write(vcpu,@@ -2569,6 +2570,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)vcpu_unimpl (vcpu,"vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n" , control->exit_info_1, control->exit_info_2); + ret = -EINVAL ;break ;default : ret =svm_invoke_exit_handler (vcpu, exit_code);@@ -2579,8 +2581,11 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)int sev_es_string_io (struct vcpu_svm *svm,int size,unsigned int port,int in) { -if (!setup_vmgexit_scratch (svm, in, svm->vmcb->control.exit_info_2)) -return -EINVAL ; +int r; + + r =setup_vmgexit_scratch (svm, in, svm->vmcb->control.exit_info_2); +if (r) +return r;return kvm_sev_es_string_io (&svm->vcpu, size, port, svm->ghcb_sa, svm->ghcb_sa_len / size, in);
Paolo Bonzini
maintainerRe: [PATCH 1/2] KVM: SEV: Return appropriate error codes if SEV-ES scratch setup fails
Quoted 7 lines · Tom Lendacky
Quoted 5 lines
Return appropriate error codes if setting up the GHCB scratch area for an SEV-ES guest fails. In particular, returning
-EINVALinstead of-ENOMEMwhen allocating the kernel buffer could be confusing as userspace would likely suspect a guest issue.Based on previous feedback and to implement the changes to the GHCB specification, I'm planning on submitting a patch that will return an error code back to the guest, instead of terminating the guest, if the scratch area fails to be setup properly. So you could hold off on this patch if you want.
I think we still want these two patches in 5.16.
Paolo
Re: [PATCH 1/2] KVM: SEV: Return appropriate error codes if SEV-ES scratch setup fails
Quoted 10 lines · Paolo Bonzini
Quoted 7 lines · Tom Lendacky
Quoted 5 lines
Return appropriate error codes if setting up the GHCB scratch area for an SEV-ES guest fails. In particular, returning
-EINVALinstead of-ENOMEMwhen allocating the kernel buffer could be confusing as userspace would likely suspect a guest issue.Based on previous feedback and to implement the changes to the GHCB specification, I'm planning on submitting a patch that will return an error code back to the guest, instead of terminating the guest, if the scratch area fails to be setup properly. So you could hold off on this patch if you want.
I think we still want these two patches in 5.16.
Ok, I'll rebase my changes on top of these then once you push them.
Thanks,
Tom
Quoted 3 lines
Paolo
Sean Christopherson
maintainerPATCH 2/2 KVM: SEV: Fall back to __vmalloc() for SEV-ES scratch area if necessary
In reply to Sean ChristophersonUse kvzalloc() to allocate KVM's buffer for SEV-ES's GHCB scratch area so that KVM falls back to __vmalloc() if physically contiguous memory isn't available. The buffer is purely a KVM software construct, i.e. there's no need for it to be physically contiguous, and at a max allowed size of 16kb it's just large enough that kzalloc() could feasibly fail due to memory fragmentation.
--- arch/x86/kvm/svm/sev.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +4 -4
@@ -2030,7 +2030,7 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)__free_page (virt_to_page (svm->vmsa));if (svm->ghcb_sa_free) -kfree (svm->ghcb_sa); +kvfree (svm->ghcb_sa); }static void dump_ghcb (struct vcpu_svm *svm)
@@ -2262,7 +2262,7 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm) svm->ghcb_sa_sync = false; } -kfree (svm->ghcb_sa); +kvfree (svm->ghcb_sa); svm->ghcb_sa =NULL ; svm->ghcb_sa_free = false; }
@@ -2350,7 +2350,7 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) len, GHCB_SCRATCH_AREA_LIMIT);return -EINVAL ; } - scratch_va =kzalloc (len,GFP_KERNEL_ACCOUNT ); + scratch_va =kvzalloc (len,GFP_KERNEL_ACCOUNT );if (!scratch_va)return -ENOMEM ;
@@ -2358,7 +2358,7 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len)/* Unable to copy scratch area from guest */ pr_err ("vmgexit: kvm_read_guest for scratch area failed\n" ); -kfree (scratch_va); +kvfree (scratch_va);return -EFAULT ; }
Signature · 1 line
2.34.0.rc0.344.g81b53c2807-goog
Paolo Bonzini
maintainerRe: [PATCH 2/2] KVM: SEV: Fall back to __vmalloc() for SEV-ES scratch area if necessary
Quoted 4 lines · Sean Christopherson
The buffer is purely a KVM software construct, i.e. there's no need for it to be physically contiguous, and at a max allowed size of 16kb it's just large enough that
kzalloc()could feasibly fail due to memory fragmentation.
That isn't entirely correct, as kzalloc() won't really fail for requests up to 32kb, i.e. order <= PAGE_ALLOC_COSTLY_ORDER. However, the reasoning is correct so I queued the patches.
Paolo