From: "Jan Beulich" Subject: [PATCH] fix off-by-one mistakes in vm_alloc() Date: Thu, 18 Jul 2013 10:59:40 +0100 Message-ID: <51E7D8AC02000078000E5DEF@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartFECE459C.0__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Uzkzw-0006yw-1x for xen-devel@lists.xenproject.org; Thu, 18 Jul 2013 09:59:44 +0000 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: Keir Fraser 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. --=__PartFECE459C.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Also add another pair of assertions to catch eventual further cases of incorrect accounting, and remove the temporary debuggin messages again which commit 68caac7f ("x86: don't use destroy_xen_mappings() for vunmap()") added. Signed-off-by: Jan Beulich --- a/xen/common/vmap.c +++ b/xen/common/vmap.c @@ -57,8 +57,8 @@ void *vm_alloc(unsigned int nr, unsigned { struct page_info *pg; =20 - ASSERT(!test_bit(vm_low, vm_bitmap)); - for ( start =3D vm_low; ; ) + ASSERT(vm_low =3D=3D vm_top || !test_bit(vm_low, vm_bitmap)); + for ( start =3D vm_low; start < vm_top; ) { bit =3D find_next_bit(vm_bitmap, vm_top, start + 1); if ( bit > vm_top ) @@ -68,12 +68,18 @@ void *vm_alloc(unsigned int nr, unsigned * corresponding page a guard one. */ start =3D (start + align) & ~(align - 1); - if ( start + nr <=3D bit ) - break; - start =3D bit < vm_top ? - find_next_zero_bit(vm_bitmap, vm_top, bit + 1) : bit; - if ( start >=3D vm_top ) - break; + if ( bit < vm_top ) + { + if ( start + nr < bit ) + break; + start =3D find_next_zero_bit(vm_bitmap, vm_top, bit + 1); + } + else + { + if ( start + nr <=3D bit ) + break; + start =3D bit; + } } =20 if ( start < vm_top ) @@ -115,6 +121,10 @@ void *vm_alloc(unsigned int nr, unsigned =20 for ( bit =3D start; bit < start + nr; ++bit ) __set_bit(bit, vm_bitmap); + if ( bit < vm_top ) + ASSERT(!test_bit(bit, vm_bitmap)); + else + ASSERT(bit =3D=3D vm_top); if ( start <=3D vm_low + 2 ) vm_low =3D bit; spin_unlock(&vm_lock); @@ -177,7 +187,6 @@ void *__vmap(const unsigned long *mfn, u void *va =3D vm_alloc(nr * granularity, align); unsigned long cur =3D (unsigned long)va; =20 -printk("vmap(%p:%#x)\n", va, nr * granularity);//temp for ( ; va && nr--; ++mfn, cur +=3D PAGE_SIZE * granularity ) { if ( map_pages_to_xen(cur, *mfn, granularity, flags) ) @@ -202,7 +211,6 @@ void vunmap(const void *va) =20 destroy_xen_mappings(addr, addr + PAGE_SIZE * vm_size(va)); #else /* Avoid tearing down intermediate page tables. */ -printk("vunmap(%p:%#x)\n", va, vm_size(va));//temp map_pages_to_xen((unsigned long)va, 0, vm_size(va), _PAGE_NONE); #endif vm_free(va); --=__PartFECE459C.0__= Content-Type: text/plain; name="vmap-off-by-one.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="vmap-off-by-one.patch" fix off-by-one mistakes in vm_alloc()=0A=0AAlso add another pair of = assertions to catch eventual further cases of=0Aincorrect accounting, and = remove the temporary debuggin messages again=0Awhich commit 68caac7f = ("x86: don't use destroy_xen_mappings() for=0Avunmap()") added.=0A=0ASigned= -off-by: Jan Beulich =0A=0A--- a/xen/common/vmap.c=0A+++= b/xen/common/vmap.c=0A@@ -57,8 +57,8 @@ void *vm_alloc(unsigned int nr, = unsigned=0A {=0A struct page_info *pg;=0A =0A- = ASSERT(!test_bit(vm_low, vm_bitmap));=0A- for ( start =3D vm_low; ; = )=0A+ ASSERT(vm_low =3D=3D vm_top || !test_bit(vm_low, vm_bitmap));= =0A+ for ( start =3D vm_low; start < vm_top; )=0A {=0A = bit =3D find_next_bit(vm_bitmap, vm_top, start + 1);=0A = if ( bit > vm_top )=0A@@ -68,12 +68,18 @@ void *vm_alloc(unsigned int nr, = unsigned=0A * corresponding page a guard one.=0A = */=0A start =3D (start + align) & ~(align - 1);=0A- = if ( start + nr <=3D bit )=0A- break;=0A- start = =3D bit < vm_top ?=0A- find_next_zero_bit(vm_bitmap, = vm_top, bit + 1) : bit;=0A- if ( start >=3D vm_top )=0A- = break;=0A+ if ( bit < vm_top )=0A+ {=0A+ = if ( start + nr < bit )=0A+ break;=0A+ = start =3D find_next_zero_bit(vm_bitmap, vm_top, bit + 1);=0A+ = }=0A+ else=0A+ {=0A+ if ( = start + nr <=3D bit )=0A+ break;=0A+ = start =3D bit;=0A+ }=0A }=0A =0A if ( start < = vm_top )=0A@@ -115,6 +121,10 @@ void *vm_alloc(unsigned int nr, unsigned=0A= =0A for ( bit =3D start; bit < start + nr; ++bit )=0A = __set_bit(bit, vm_bitmap);=0A+ if ( bit < vm_top )=0A+ ASSERT(!te= st_bit(bit, vm_bitmap));=0A+ else=0A+ ASSERT(bit =3D=3D = vm_top);=0A if ( start <=3D vm_low + 2 )=0A vm_low =3D bit;=0A = spin_unlock(&vm_lock);=0A@@ -177,7 +187,6 @@ void *__vmap(const = unsigned long *mfn, u=0A void *va =3D vm_alloc(nr * granularity, = align);=0A unsigned long cur =3D (unsigned long)va;=0A =0A-printk("vmap= (%p:%#x)\n", va, nr * granularity);//temp=0A for ( ; va && nr--; = ++mfn, cur +=3D PAGE_SIZE * granularity )=0A {=0A if ( = map_pages_to_xen(cur, *mfn, granularity, flags) )=0A@@ -202,7 +211,6 @@ = void vunmap(const void *va)=0A =0A destroy_xen_mappings(addr, addr + = PAGE_SIZE * vm_size(va));=0A #else /* Avoid tearing down intermediate page = tables. */=0A-printk("vunmap(%p:%#x)\n", va, vm_size(va));//temp=0A = map_pages_to_xen((unsigned long)va, 0, vm_size(va), _PAGE_NONE);=0A = #endif=0A vm_free(va);=0A --=__PartFECE459C.0__= 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 --=__PartFECE459C.0__=--