[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] xmalloc: correct _xrealloc() for large or changing alignment


  • To: jbeulich@xxxxxxxx
  • From: Stephen Cheng <stephen.cheng@xxxxxxxxxx>
  • Date: Thu, 3 Dec 2026 16:06:49 +0800
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=M0vtUe28AHBT+G1nkF14DpIYmW09A5uaiBIniaE3VYo=; b=Y1yzM0Go8ji3aALSIgLk1ssLzqfuChQlUS22+3WFM1Ha4ddb8OFYrTif+yCse28BrlIGjBGH3oBPrC5WqN/ZBxumZuSIwaDMAaKyS05xEp+ikggHfBhrPWu57zDA8INAHhX+ynao2gmRWiLdriPPObGl0dumRyxFdChSRLIVcgY2li1oNlQnEPVwMFSzYiac+v+7yAipvUBYzqy5j8fkeVHuOhqvm+ONOYUIlAOHpG60n02U3Dp67HkKBSPNGgG1/Lvp2NB9xWVcPNf5uQ/VUBj7uvq3f/0r+3I5IL0akiEszORBW+DgpvNH0taKYv2/y6/GZRi+j6xUDb6YmmXXpw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xX5Wue3nTGs6W4HyX1wa5eKic6pBAiCQ9aZkt1QIISF3QBqjTUUPxmzh+jxUBuvOAdGTGiEdAnr5v1y3vv3eKOHp8p4aF7RNyCEcpwedGgAJs3Ekr/PzszWe4VtV2Yq4QuBhtHJD8Q1tEPDTi6fQqMJ9aLXQq8o4LWZA0370efGxzjAwqpozc/Xi2qZSSexVdkFrD2mXhbC3OR8eztn1N54ON5wbA7l0SRBHKMFwsjrfFjmvGfYVKqKb8WLRWB4S1PXzXj56eoLy1PgXYx9NPppkpOPX8Y2vWAQJt4ojG3GXQexUVdH3aElxhIWzu9l21yzdCmldRa+Ev2aPvAElYg==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=citrix.com header.i="@citrix.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: andrew.cooper3@xxxxxxxxxx, anthony.perard@xxxxxxxxxx, julien@xxxxxxx, michal.orzel@xxxxxxx, roger@xxxxxxxxxxxxxx, sstabellini@xxxxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx, Stephen Cheng <stephen.cheng@xxxxxxxxxx>
  • Delivery-date: Thu, 03 Dec 2026 08:07:39 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.