Re: [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc()

From: Harry Yoo

Date: Wed Dec 02 2026 - 09:51:50 EST


On Wed, Dec 02, 2026 at 01:52:51AM +0900, Sang-Heon Jeon wrote:
> If SLAB_STORE_USER is enabled, the requested size is tracked and
> kzalloc() zeroes only the requested size.
>
> But if kvrealloc() falls back to kvmalloc(), it copies the bytes beyond
> the requested size up to ksize(p), which might be non-zero even if
> __GFP_ZERO is set.

ksize() says "This should only be used internally to query the true size
of allocations. It is not meant to be a wqay to discover the usable size
of an allocation after the fact. Instead, use kmalloc_size_roundup().
Using memory beyond the originally requested allocation
size may trigger KASAN, UBSAN_BOUNDS, and/or FORTIFY_SOURCE"

> So copy only the requested size instead, like krealloc() already does.
>
> Fixes: 590b9d576cae ("mm: kvmalloc: align kvrealloc() with krealloc()")
> Signed-off-by: Sang-Heon Jeon <ekffu200098@xxxxxxxxx>
> ---
>
> mm/slub.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/mm/slub.c b/mm/slub.c
> index 8e6e900fc39b..9742c2825a06 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -7272,10 +7272,18 @@ void *kvrealloc_node_align_noprof(const void *p, DECL_TOKEN_PARAMS(size, token),
> if (!n)
> return NULL;
>
> - if (p) {
> + if (!ZERO_OR_NULL_PTR(p)) {
> + struct slab *slab = virt_to_slab(p);
> + size_t old_size;
> +
> + if (!slab)
> + old_size = ksize(p);
> + else
> + old_size = get_orig_size(slab->slab_cache, (void *)p);
> +
> /* We already know that `p` is not a vmalloc address. */
> kasan_disable_current();
> - memcpy(n, kasan_reset_tag(p), min(size, ksize(p)));
> + memcpy(n, kasan_reset_tag(p), min(size, old_size));

Do we really need this when kvrealloc() frees a slab object and
allocates a vmalloc object(), why?

> kasan_enable_current();
>
> kfree(p);
> --
> 2.53.0
>

--
Cheers,
Harry / Hyeonggon