From: Jan Beulich <jbeulich@suse.com>
To: "xen-devel@lists.xenproject.org" <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Julien Grall" <julien@xen.org>,
"Stefano Stabellini" <sstabellini@kernel.org>,
"Anthony PERARD" <anthony.perard@vates.tech>,
"Michal Orzel" <michal.orzel@amd.com>,
"Roger Pau Monné" <roger@xenproject.org>
Subject: [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment
Date: Thu, 3 Dec 2026 14:17:58 +0100 [thread overview]
Message-ID: <b1ea2805-fffa-42f2-914a-abfcbd748c9d@suse.com> (raw)
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.
Fixes: 50879e805edb ("xen/common: Introduce _xrealloc function")
Reported-by: Roger Pau Monné <roger.pau@amd.com>
Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
v2: Also include xen/errno.h in selftest section. Fix block moving up in-
place due to higher alignment. Add respective case to self-test.
--- a/xen/common/xmalloc_tlsf.c
+++ b/xen/common/xmalloc_tlsf.c
@@ -578,17 +578,23 @@ static void *strip_padding(void *p)
return p;
}
-static void *add_padding(void *p, unsigned long align)
+static void *add_padding(void *p, unsigned long align, bool dry)
{
unsigned int pad;
if ( (pad = -(long)p & (align - 1)) != 0 )
{
void *q = p + pad;
- struct bhdr *b = q - BHDR_OVERHEAD;
ASSERT(q > p);
- b->size = pad | FREE_BLOCK;
+
+ if ( !dry )
+ {
+ struct bhdr *b = q - BHDR_OVERHEAD;
+
+ b->size = pad | FREE_BLOCK;
+ }
+
p = q;
}
@@ -622,7 +628,7 @@ void *_xmalloc(unsigned long size, unsig
return xmalloc_whole_pages(size - align + MEM_ALIGN, align);
/* Add alignment padding. */
- p = add_padding(p, align);
+ p = add_padding(p, align, false);
ASSERT(((unsigned long)p & (align - 1)) == 0);
return p;
@@ -681,13 +687,23 @@ void *_xrealloc(void *ptr, unsigned long
if ( tmp_size <= curr_size )
{
- /* Add alignment padding. */
- p = add_padding(p, align);
+ void *p0 = p;
+ /* Determine alignment padding. */
+ p = add_padding(p, align, true);
ASSERT(!((unsigned long)p & (align - 1)));
+ if ( p != ptr )
+ ptr = memmove(p, ptr, min(curr_size - (p - p0), size));
+
+ /* Add alignment padding. */
+ p = add_padding(p0, align, false);
+ ASSERT(p == ptr);
+
return p;
}
+
+ curr_size -= ptr - p;
}
p = _xmalloc(size, align);
@@ -730,3 +746,83 @@ void xfree(void *p)
xmem_pool_free(p, xenpool);
}
+
+#ifdef CONFIG_SELF_TESTS
+
+#include <xen/errno.h>
+#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 << 2);
+ if ( !new )
+ goto out;
+ BUG_ON(!IS_ALIGNED((unsigned long)new, MEM_ALIGN << 2));
+ 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 */
next reply other threads:[~2026-12-03 13:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 13:17 Jan Beulich [this message]
2026-12-04 2:55 ` [PATCH v2] xmalloc: correct _xrealloc() for large or changing alignment Stephen Cheng
2026-12-04 6:42 ` Jan Beulich
2026-12-04 8:11 ` Jan Beulich
2026-12-04 9:03 ` Stephen Cheng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b1ea2805-fffa-42f2-914a-abfcbd748c9d@suse.com \
--to=jbeulich@suse.com \
--cc=andrew.cooper3@citrix.com \
--cc=anthony.perard@vates.tech \
--cc=julien@xen.org \
--cc=michal.orzel@amd.com \
--cc=roger@xenproject.org \
--cc=sstabellini@kernel.org \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.