xmalloc: correct _xrealloc() for large or changing alignment
PATCH 1/1 xmalloc: correct _xrealloc() for large or changing alignment
The curr_size calculation on the non-page-aligned path may include leading padding; therefore that size cannot be passed unadjusted to memcpy() (or the min() used in its invocation).
Further, for the "can be re-used" case on the non-page-aligned path, if alignment changes from what was used for the original calculation, the new pointer could differ from the original one. Hence memory contents may need moving.
No in-tree users presently exercise either of the problem cases. Add some self-testing to cover them.
xen/common/xmalloc_tlsf.c +78 -1
@@ -681,13 +681,20 @@ void *_xrealloc(void *ptr, unsigned longif ( tmp_size <= curr_size ) { +void *p0 = p; +/* Add alignment padding. */ p =add_padding (p, align);ASSERT (!((unsigned long )p & (align -1 ))); -return p; +if ( p == ptr ) +return ptr; + +return memmove (p, ptr,min (curr_size - (p - p0), size)); } + + curr_size -= ptr - p; } p =_xmalloc (size, align);
@@ -730,3 +737,73 @@ void xfree(void *p)xmem_pool_free (p, xenpool); } + +#ifdef CONFIG_SELF_TESTS + +#include <xen/init.h> + +static int cf_check __init selftest(void) +{ +unsigned char *ptr =_xmalloc (1 ,1 ), *new; +unsigned long align; + +if ( !ptr ) +return -ENOMEM ; + +BUG_ON (!IS_ALIGNED ((unsigned long )ptr,MEM_ALIGN )); + ptr[0 ] ='A' ; + + new =_xrealloc (ptr,MEM_ALIGN ,MEM_ALIGN ); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new,MEM_ALIGN )); +BUG_ON (new[0 ] !='A' ); +memset (new +1 ,'B' ,MEM_ALIGN -1 ); + ptr = new; + +for ( align =MEM_ALIGN <<2 ; align <PAGE_SIZE ; align <<=1 ) +if ( !IS_ALIGNED ((unsigned long )ptr, align) ) +break ; + new =_xrealloc (ptr,MEM_ALIGN , align); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new, align)); +BUG_ON (new[0 ] !='A' ); +BUG_ON (memchr_inv (new +1 ,'B' ,MEM_ALIGN -1 )); + ptr = new; + + new =_xrealloc (ptr, align >>=1 ,MEM_ALIGN ); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new,MEM_ALIGN )); +BUG_ON (new[0 ] !='A' ); +BUG_ON (memchr_inv (new +1 ,'B' ,MEM_ALIGN -1 )); +memset (new +MEM_ALIGN ,'C' , align -MEM_ALIGN ); + ptr = new; + + new =_xrealloc (ptr, align <<1 ,MEM_ALIGN <<1 ); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new,MEM_ALIGN <<1 )); +BUG_ON (new[0 ] !='A' ); +BUG_ON (memchr_inv (new +1 ,'B' ,MEM_ALIGN -1 )); +BUG_ON (memchr_inv (new +MEM_ALIGN ,'C' , align -MEM_ALIGN )); + ptr = new; + + new =_xrealloc (ptr, align,MEM_ALIGN ); +if ( !new ) + goto out; +BUG_ON (!IS_ALIGNED ((unsigned long )new,MEM_ALIGN )); +BUG_ON (new[0 ] !='A' ); +BUG_ON (memchr_inv (new +1 ,'B' ,MEM_ALIGN -1 )); +BUG_ON (memchr_inv (new +MEM_ALIGN ,'C' , align -MEM_ALIGN )); + ptr = new; + + out: +xfree (ptr); + +return new ?0 : -ENOMEM ; +} +presmp_initcall (selftest); + +#endif /* CONFIG_SELF_TESTS */
xen/common/xmalloc_tlsf.c void xfree(void *p) · line 743
Quoted 8 lines · Jan Beulich
xen/common/xmalloc_tlsf.c +4
-0@@ -730,3 +737,73 @@ void xfree(void *p)xmem_pool_free (p, xenpool); } + +#ifdef CONFIG_SELF_TESTS + +#include <xen/init.h>
PPC also needs xen/errno.h here, to allow ...
xen/common/xmalloc_tlsf.c void xfree(void *p) · line 751
Quoted 7 lines
xen/common/xmalloc_tlsf.c +7
-0+static int cf_check __init selftest(void) +{ +unsigned char *ptr =_xmalloc (1 ,1 ), *new; +unsigned long align; + +if ( !ptr ) +return -ENOMEM ;
... this to compile. Fixed locally.
Jan
Quoted 9 lines · Jan Beulich
xen/common/xmalloc_tlsf.c +2
-0@@ -681,13 +681,20 @@ void *_xrealloc(void *ptr, unsigned longif ( tmp_size <= curr_size ) { +void *p0 = p; +/* Add alignment padding. */
xen/common/xmalloc_tlsf.c void *_xrealloc(void *ptr, unsigned long · line 695
Quoted 10 lines
xen/common/xmalloc_tlsf.c +4
-1p =add_padding (p, align);ASSERT (!((unsigned long )p & (align -1 ))); -return p; +if ( p == ptr ) +return ptr; + +return memmove (p, ptr,min (curr_size - (p - p0), size)); }
When the alignment gets larger, the new p can be above ptr. Then add_padding() writes the padding marker (b->size = pad | FREE_BLOCK at p - 8) inside the old data, before memmove() copies it. So 4 bytes of the data are lost.
Would it be simpler to keep the block only when ptr itself can stay, and take the _xmalloc() path otherwise? Like the page-aligned path above:
if ( !((unsigned long)ptr & (align - 1)) &&
size <= curr_size - (ptr - p) )
return ptr;
curr_size -= ptr - p;Then nothing needs moving inside the block. As you mentioned, no in-tree caller changes the alignment, so the extra allocation should not matter.
The self test doesn't hit this case, because each time the alignment gets larger the new size also doesn't fit the block. I tested it with an extra step where:
- the old block is re-used: the new request fits in the block that is already allocated, and
- the pointer moves up: the old pointer is not aligned to the new alignment, so add_padding() gives a new pointer above it.
In a debug build booted in QEMU, this step hits a BUG_ON() with your patch. With the change above, that step and your self test pass. I can send the step if it is useful.
Stephen
Quoted 29 lines · Stephen Cheng
Quoted 9 lines · Jan Beulich
xen/common/xmalloc_tlsf.c +2
-0@@ -681,13 +681,20 @@ void *_xrealloc(void *ptr, unsigned longif ( tmp_size <= curr_size ) { +void *p0 = p; +/* Add alignment padding. */ xen/common/xmalloc_tlsf.c void *_xrealloc(void *ptr, unsigned long · line 695
Quoted 10 lines
xen/common/xmalloc_tlsf.c +4
-1p =add_padding (p, align);ASSERT (!((unsigned long )p & (align -1 ))); -return p; +if ( p == ptr ) +return ptr; + +return memmove (p, ptr,min (curr_size - (p - p0), size)); }When the alignment gets larger, the new p can be above ptr. Then
add_padding()writes the padding marker (b->size= pad |FREE_BLOCKat p - 8) inside the old data, beforememmove()copies it. So 4 bytes of the data are lost.Would it be simpler to keep the block only when ptr itself can stay, and take the
_xmalloc()path otherwise? Like the page-aligned path above:
Quoted 8 lines
if ( !((unsigned long)ptr & (align - 1)) && size <= curr_size - (ptr - p) ) return ptr; curr_size -= ptr - p;Then nothing needs moving inside the block. As you mentioned, no in-tree caller changes the alignment, so the extra allocation should not matter.
Hmm, I'd like to avoid discouraging use of xrealloc() because of it being incapable to re-use the same block. Granted uses with changing alignment are relatively unlikely, but still. v2 coming. Thanks for pointing out this issue.
Jan