Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Sang-Heon Jeon <ekffu200098@gmail.com>
To: Vlastimil Babka <vbabka@kernel.org>, Harry Yoo <harry@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Christoph Lameter <cl@gentwo.org>,
	Danilo Krummrich <dakr@kernel.org>,
	David Rientjes <rientjes@google.com>, Hao Li <hao.li@linux.dev>,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	Michal Hocko <mhocko@suse.com>,
	Roman Gushchin <roman.gushchin@linux.dev>
Subject: [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc()
Date: Wed,  2 Dec 2026 01:52:51 +0900	[thread overview]
Message-ID: <20261201165256.1286791-4-ekffu200098@gmail.com> (raw)
In-Reply-To: <20261201165256.1286791-1-ekffu200098@gmail.com>

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.

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@gmail.com>
---
 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));
 			kasan_enable_current();
 
 			kfree(p);
-- 
2.53.0



      parent reply	other threads:[~2026-12-01 16:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 16:52 [PATCH 0/3] mm/slub: two small cleanups and a small fix Sang-Heon Jeon
2026-12-01 16:52 ` [PATCH 1/3] mm/slub: remove always-true check in calculate_sizes() Sang-Heon Jeon
2026-12-01 16:52 ` [PATCH 2/3] mm/slub: use kmem_cache_alloc() in bootstrap() Sang-Heon Jeon
2026-12-01 16:52 ` Sang-Heon Jeon [this message]

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=20261201165256.1286791-4-ekffu200098@gmail.com \
    --to=ekffu200098@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=dakr@kernel.org \
    --cc=hao.li@linux.dev \
    --cc=harry@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=vbabka@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox