* [PATCH] xmalloc: correct _xrealloc() for large or changing alignment
@ 2026-11-25 8:35 Jan Beulich
2026-12-01 9:08 ` Jan Beulich
0 siblings, 1 reply; 2+ messages in thread
From: Jan Beulich @ 2026-11-25 8:35 UTC (permalink / raw)
To: xen-devel@lists.xenproject.org
Cc: Andrew Cooper, Julien Grall, Stefano Stabellini, Anthony PERARD,
Michal Orzel, Roger Pau Monné
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>
--- 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));
}
+
+ 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 */
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] xmalloc: correct _xrealloc() for large or changing alignment
2026-11-25 8:35 [PATCH] xmalloc: correct _xrealloc() for large or changing alignment Jan Beulich
@ 2026-12-01 9:08 ` Jan Beulich
0 siblings, 0 replies; 2+ messages in thread
From: Jan Beulich @ 2026-12-01 9:08 UTC (permalink / raw)
To: xen-devel@lists.xenproject.org
Cc: Andrew Cooper, Julien Grall, Stefano Stabellini, Anthony PERARD,
Michal Orzel, Roger Pau Monné
On 25.11.2026 09:35, Jan Beulich wrote:
> @@ -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 ...
> +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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-01 9:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 8:35 [PATCH] xmalloc: correct _xrealloc() for large or changing alignment Jan Beulich
2026-12-01 9:08 ` Jan Beulich
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.