Commit 1a625056 authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: x86: Directly return __vmalloc() result in ->vm_alloc()

Directly return the __vmalloc() result in {svm,vmx}_vm_alloc() to pave
the way for handling VM alloc/free in common x86 code, and to obviate
the need to check the result of __vmalloc() in vendor specific code.
Add a build-time assertion to ensure each structs' "kvm" field stays at
offset 0, which allows interpreting a "struct kvm_{svm,vmx}" as a
"struct kvm".
Signed-off-by: default avatarSean Christopherson <sean.j.christopherson@intel.com>
Reviewed-by: default avatarVitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent d18b2f43
...@@ -1946,19 +1946,15 @@ static void __unregister_enc_region_locked(struct kvm *kvm, ...@@ -1946,19 +1946,15 @@ static void __unregister_enc_region_locked(struct kvm *kvm,
static struct kvm *svm_vm_alloc(void) static struct kvm *svm_vm_alloc(void)
{ {
struct kvm_svm *kvm_svm = __vmalloc(sizeof(struct kvm_svm), BUILD_BUG_ON(offsetof(struct kvm_svm, kvm) != 0);
GFP_KERNEL_ACCOUNT | __GFP_ZERO,
PAGE_KERNEL);
if (!kvm_svm)
return NULL;
return &kvm_svm->kvm; return __vmalloc(sizeof(struct kvm_svm),
GFP_KERNEL_ACCOUNT | __GFP_ZERO, PAGE_KERNEL);
} }
static void svm_vm_free(struct kvm *kvm) static void svm_vm_free(struct kvm *kvm)
{ {
vfree(to_kvm_svm(kvm)); vfree(kvm);
} }
static void sev_vm_destroy(struct kvm *kvm) static void sev_vm_destroy(struct kvm *kvm)
......
...@@ -6681,20 +6681,16 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) ...@@ -6681,20 +6681,16 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
static struct kvm *vmx_vm_alloc(void) static struct kvm *vmx_vm_alloc(void)
{ {
struct kvm_vmx *kvm_vmx = __vmalloc(sizeof(struct kvm_vmx), BUILD_BUG_ON(offsetof(struct kvm_vmx, kvm) != 0);
GFP_KERNEL_ACCOUNT | __GFP_ZERO,
PAGE_KERNEL);
if (!kvm_vmx) return __vmalloc(sizeof(struct kvm_vmx),
return NULL; GFP_KERNEL_ACCOUNT | __GFP_ZERO, PAGE_KERNEL);
return &kvm_vmx->kvm;
} }
static void vmx_vm_free(struct kvm *kvm) static void vmx_vm_free(struct kvm *kvm)
{ {
kfree(kvm->arch.hyperv.hv_pa_pg); kfree(kvm->arch.hyperv.hv_pa_pg);
vfree(to_kvm_vmx(kvm)); vfree(kvm);
} }
static void vmx_free_vcpu(struct kvm_vcpu *vcpu) static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment