If nested vCPU initialization fails, cleanup the allocated memory since
it is no longer handled by the caller.

Fixes: c47984aabead ("nvmx: implement support for MSR bitmaps")
Fixes: f5bdb4aaa165 ("x86/hvm: Obsolete the use of HVM_PARAM_NESTEDHVM")
Signed-off-by: Ross Lagerwall <[email protected]>
---
 xen/arch/x86/hvm/vmx/vvmx.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c
index e4f3a5fe4c71..cf9aecb4c3e4 100644
--- a/xen/arch/x86/hvm/vmx/vvmx.c
+++ b/xen/arch/x86/hvm/vmx/vvmx.c
@@ -53,6 +53,13 @@ void nvmx_cpu_dead(unsigned int cpu)
     XFREE(per_cpu(vvmcs_buf, cpu));
 }
 
+static void vcpu_relinquish_resources(struct vcpu *v)
+{
+    struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
+
+    FREE_XENHEAP_PAGE(nvmx->msr_merged);
+}
+
 int cf_check nvmx_vcpu_initialise(struct vcpu *v)
 {
     struct domain *d = v->domain;
@@ -89,7 +96,7 @@ int cf_check nvmx_vcpu_initialise(struct vcpu *v)
         if ( !vmread_bitmap )
         {
             gdprintk(XENLOG_ERR, "nest: allocation for vmread bitmap 
failed\n");
-            return -ENOMEM;
+            goto err;
         }
         v->arch.hvm.vmx.vmread_bitmap = vmread_bitmap;
 
@@ -99,7 +106,7 @@ int cf_check nvmx_vcpu_initialise(struct vcpu *v)
         if ( !vmwrite_bitmap )
         {
             gdprintk(XENLOG_ERR, "nest: allocation for vmwrite bitmap 
failed\n");
-            return -ENOMEM;
+            goto err;
         }
         v->arch.hvm.vmx.vmwrite_bitmap = vmwrite_bitmap;
 
@@ -124,7 +131,7 @@ int cf_check nvmx_vcpu_initialise(struct vcpu *v)
     {
         nvmx->msr_merged = alloc_xenheap_page();
         if ( !nvmx->msr_merged )
-            return -ENOMEM;
+            goto err;
     }
 
     nvmx->ept.enabled = 0;
@@ -139,6 +146,11 @@ int cf_check nvmx_vcpu_initialise(struct vcpu *v)
     nvmx->msrbitmap = NULL;
     INIT_LIST_HEAD(&nvmx->launched_list);
     return 0;
+
+ err:
+    nvmx_vcpu_destroy(v);
+    vcpu_relinquish_resources(v);
+    return -ENOMEM;
 }
  
 void cf_check nvmx_vcpu_destroy(struct vcpu *v)
@@ -183,13 +195,6 @@ void cf_check nvmx_vcpu_destroy(struct vcpu *v)
     }
 }
 
-static void vcpu_relinquish_resources(struct vcpu *v)
-{
-    struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
-
-    FREE_XENHEAP_PAGE(nvmx->msr_merged);
-}
-
 void cf_check nvmx_domain_relinquish_resources(struct domain *d)
 {
     struct vcpu *v;
-- 
2.51.0


Reply via email to