|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [PATCH] xmalloc: correct _xrealloc() for large or changing alignment
On 25/11/2026 08:35, Jan Beulich wrote:
> --- a/xen/common/xmalloc_tlsf.c
> +++ b/xen/common/xmalloc_tlsf.c
> @@ -681,13 +681,20 @@ void *_xrealloc(void *ptr, unsigned long
>
> if ( 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));
> }
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
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |