From: "Jan Beulich" Subject: [PATCH v2] x86/nhvm: properly clean up after failure to set up all vCPU-s Date: Thu, 21 Feb 2013 11:26:23 +0000 Message-ID: <5126126F02000078000BFD83@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part54642E4F.2__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Tim Deegan , Olaf Hering , Eddie Dong , Jun Nakajima List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part54642E4F.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Otherwise we may leak memory when setting up nHVM fails half way. This implies that the individual destroy functions will have to remain capable (in the VMX case they first need to be made so, following 26486:7648ef657fe7 and 26489:83a3fa9c8434) of being called for a vCPU that the corresponding init function was never run on. Once at it, also clean up some inefficiencies in the corresponding parameter validation code. Signed-off-by: Jan Beulich --- v2: nVMX fixes required by 26486:7648ef657fe7 and 26489:83a3fa9c8434. --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -3916,20 +3916,25 @@ long do_hvm_op(unsigned long op, XEN_GUE rc =3D -EPERM; break; } + if ( !a.value ) + break; if ( a.value > 1 ) rc =3D -EINVAL; - if ( !is_hvm_domain(d) ) - rc =3D -EINVAL; /* Remove the check below once we have * shadow-on-shadow. */ - if ( cpu_has_svm && !paging_mode_hap(d) && a.value ) + if ( cpu_has_svm && !paging_mode_hap(d) ) rc =3D -EINVAL; /* Set up NHVM state for any vcpus that are already up */ if ( !d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM] ) + { for_each_vcpu(d, v) if ( rc =3D=3D 0 ) rc =3D nestedhvm_vcpu_initialise(v); + if ( rc ) + for_each_vcpu(d, v) + nestedhvm_vcpu_destroy(v); + } break; case HVM_PARAM_BUFIOREQ_EVTCHN: rc =3D -EINVAL; --- a/xen/arch/x86/hvm/nestedhvm.c +++ b/xen/arch/x86/hvm/nestedhvm.c @@ -87,7 +87,7 @@ nestedhvm_vcpu_initialise(struct vcpu *v void nestedhvm_vcpu_destroy(struct vcpu *v) { - if ( nestedhvm_enabled(v->domain) && hvm_funcs.nhvm_vcpu_destroy ) + if ( hvm_funcs.nhvm_vcpu_destroy ) hvm_funcs.nhvm_vcpu_destroy(v); } =20 --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -62,7 +62,7 @@ int nvmx_vcpu_initialise(struct vcpu *v) if ( !nvcpu->nv_n2vmcx ) { gdprintk(XENLOG_ERR, "nest: allocation for shadow vmcs failed\n");= - goto out; + return -ENOMEM; } =20 /* non-root VMREAD/VMWRITE bitmap. */ @@ -75,7 +75,7 @@ int nvmx_vcpu_initialise(struct vcpu *v) if ( !vmread_bitmap ) { gdprintk(XENLOG_ERR, "nest: allocation for vmread bitmap = failed\n"); - goto out1; + return -ENOMEM; } v->arch.hvm_vmx.vmread_bitmap =3D vmread_bitmap; =20 @@ -83,7 +83,7 @@ int nvmx_vcpu_initialise(struct vcpu *v) if ( !vmwrite_bitmap ) { gdprintk(XENLOG_ERR, "nest: allocation for vmwrite bitmap = failed\n"); - goto out2; + return -ENOMEM; } v->arch.hvm_vmx.vmwrite_bitmap =3D vmwrite_bitmap; =20 @@ -118,12 +118,6 @@ int nvmx_vcpu_initialise(struct vcpu *v) nvmx->msrbitmap =3D NULL; INIT_LIST_HEAD(&nvmx->launched_list); return 0; -out2: - free_domheap_page(v->arch.hvm_vmx.vmread_bitmap); -out1: - free_xenheap_page(nvcpu->nv_n2vmcx); -out: - return -ENOMEM; } =20 void nvmx_vcpu_destroy(struct vcpu *v) @@ -147,16 +141,24 @@ void nvmx_vcpu_destroy(struct vcpu *v) nvcpu->nv_n2vmcx =3D NULL; } =20 - list_for_each_entry_safe(item, n, &nvmx->launched_list, node) - { - list_del(&item->node); - xfree(item); - } + /* Must also cope with nvmx_vcpu_initialise() not having got called. = */ + if ( nvmx->launched_list.next ) + list_for_each_entry_safe(item, n, &nvmx->launched_list, node) + { + list_del(&item->node); + xfree(item); + } =20 if ( v->arch.hvm_vmx.vmread_bitmap ) + { free_domheap_page(v->arch.hvm_vmx.vmread_bitmap); + v->arch.hvm_vmx.vmread_bitmap =3D NULL; + } if ( v->arch.hvm_vmx.vmwrite_bitmap ) + { free_domheap_page(v->arch.hvm_vmx.vmwrite_bitmap); + v->arch.hvm_vmx.vmwrite_bitmap =3D NULL; + } } =20 void nvmx_domain_relinquish_resources(struct domain *d) --=__Part54642E4F.2__= Content-Type: text/plain; name="x86-nhvm-enable-failure.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-nhvm-enable-failure.patch" x86/nhvm: properly clean up after failure to set up all vCPU-s=0A=0AOtherwi= se we may leak memory when setting up nHVM fails half way.=0A=0AThis = implies that the individual destroy functions will have to remain=0Acapable= (in the VMX case they first need to be made so, following=0A26486:7648ef65= 7fe7 and 26489:83a3fa9c8434) of being called for a vCPU=0Athat the = corresponding init function was never run on.=0A=0AOnce at it, also clean = up some inefficiencies in the corresponding=0Aparameter validation = code.=0A=0ASigned-off-by: Jan Beulich =0A---=0Av2: nVMX = fixes required by 26486:7648ef657fe7 and 26489:83a3fa9c8434.=0A=0A--- = a/xen/arch/x86/hvm/hvm.c=0A+++ b/xen/arch/x86/hvm/hvm.c=0A@@ -3916,20 = +3916,25 @@ long do_hvm_op(unsigned long op, XEN_GUE=0A = rc =3D -EPERM;=0A break;=0A }=0A+ = if ( !a.value )=0A+ break;=0A = if ( a.value > 1 )=0A rc =3D -EINVAL;=0A- = if ( !is_hvm_domain(d) )=0A- rc =3D -EINVAL;=0A = /* Remove the check below once we have=0A * = shadow-on-shadow.=0A */=0A- if ( cpu_has_sv= m && !paging_mode_hap(d) && a.value )=0A+ if ( cpu_has_svm = && !paging_mode_hap(d) )=0A rc =3D -EINVAL;=0A = /* Set up NHVM state for any vcpus that are already up */=0A = if ( !d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM] )=0A+ = {=0A for_each_vcpu(d, v)=0A = if ( rc =3D=3D 0 )=0A rc =3D nestedhvm_vc= pu_initialise(v);=0A+ if ( rc )=0A+ = for_each_vcpu(d, v)=0A+ nestedhvm_vcpu_destro= y(v);=0A+ }=0A break;=0A case = HVM_PARAM_BUFIOREQ_EVTCHN:=0A rc =3D -EINVAL;=0A--- = a/xen/arch/x86/hvm/nestedhvm.c=0A+++ b/xen/arch/x86/hvm/nestedhvm.c=0A@@ = -87,7 +87,7 @@ nestedhvm_vcpu_initialise(struct vcpu *v=0A void=0A = nestedhvm_vcpu_destroy(struct vcpu *v)=0A {=0A- if ( nestedhvm_enabled(v= ->domain) && hvm_funcs.nhvm_vcpu_destroy )=0A+ if ( hvm_funcs.nhvm_vcpu_= destroy )=0A hvm_funcs.nhvm_vcpu_destroy(v);=0A }=0A =0A--- = a/xen/arch/x86/hvm/vmx/vvmx.c=0A+++ b/xen/arch/x86/hvm/vmx/vvmx.c=0A@@ = -62,7 +62,7 @@ int nvmx_vcpu_initialise(struct vcpu *v)=0A if ( = !nvcpu->nv_n2vmcx )=0A {=0A gdprintk(XENLOG_ERR, "nest: = allocation for shadow vmcs failed\n");=0A- goto out;=0A+ return = -ENOMEM;=0A }=0A =0A /* non-root VMREAD/VMWRITE bitmap. */=0A@@ = -75,7 +75,7 @@ int nvmx_vcpu_initialise(struct vcpu *v)=0A if ( = !vmread_bitmap )=0A {=0A gdprintk(XENLOG_ERR, "nest: = allocation for vmread bitmap failed\n");=0A- goto out1;=0A+ = return -ENOMEM;=0A }=0A v->arch.hvm_vmx.vmread_bitma= p =3D vmread_bitmap;=0A =0A@@ -83,7 +83,7 @@ int nvmx_vcpu_initialise(struc= t vcpu *v)=0A if ( !vmwrite_bitmap )=0A {=0A = gdprintk(XENLOG_ERR, "nest: allocation for vmwrite bitmap failed\n");=0A- = goto out2;=0A+ return -ENOMEM;=0A }=0A = v->arch.hvm_vmx.vmwrite_bitmap =3D vmwrite_bitmap;=0A =0A@@ -118,12 = +118,6 @@ int nvmx_vcpu_initialise(struct vcpu *v)=0A nvmx->msrbitmap = =3D NULL;=0A INIT_LIST_HEAD(&nvmx->launched_list);=0A return = 0;=0A-out2:=0A- free_domheap_page(v->arch.hvm_vmx.vmread_bitmap);=0A-out= 1:=0A- free_xenheap_page(nvcpu->nv_n2vmcx);=0A-out:=0A- return = -ENOMEM;=0A }=0A =0A void nvmx_vcpu_destroy(struct vcpu *v)=0A@@ -147,16 = +141,24 @@ void nvmx_vcpu_destroy(struct vcpu *v)=0A nvcpu->nv_n2vm= cx =3D NULL;=0A }=0A =0A- list_for_each_entry_safe(item, n, = &nvmx->launched_list, node)=0A- {=0A- list_del(&item->node);=0A- = xfree(item);=0A- }=0A+ /* Must also cope with nvmx_vcpu_initia= lise() not having got called. */=0A+ if ( nvmx->launched_list.next = )=0A+ list_for_each_entry_safe(item, n, &nvmx->launched_list, = node)=0A+ {=0A+ list_del(&item->node);=0A+ = xfree(item);=0A+ }=0A =0A if ( v->arch.hvm_vmx.vmread_bitmap = )=0A+ {=0A free_domheap_page(v->arch.hvm_vmx.vmread_bitmap);=0A+= v->arch.hvm_vmx.vmread_bitmap =3D NULL;=0A+ }=0A if ( = v->arch.hvm_vmx.vmwrite_bitmap )=0A+ {=0A free_domheap_page(v->a= rch.hvm_vmx.vmwrite_bitmap);=0A+ v->arch.hvm_vmx.vmwrite_bitmap =3D = NULL;=0A+ }=0A }=0A =0A void nvmx_domain_relinquish_resources(struct = domain *d)=0A --=__Part54642E4F.2__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part54642E4F.2__=--