* [PATCH 0/3] mm/slub: two small cleanups and a small fix
@ 2026-12-01 16:52 Sang-Heon Jeon
2026-12-01 16:52 ` [PATCH 1/3] mm/slub: remove always-true check in calculate_sizes() Sang-Heon Jeon
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Sang-Heon Jeon @ 2026-12-01 16:52 UTC (permalink / raw)
To: Andrew Morton, Harry Yoo, Vlastimil Babka
Cc: Christoph Lameter, Danilo Krummrich, David Rientjes, Hao Li,
linux-kernel, linux-mm, Michal Hocko, Roman Gushchin
Hello,
While reading the slab code, I found two small cleanups and a small fix.
Patch 1 removes the always-true check in calculate_sizes().
Patch 2 uses kmem_cache_alloc() in bootstrap() instead of
kmem_cache_zalloc().
Patch 3 copies only the requested size in kvrealloc() instead of
ksize(p) bytes.
These are *really* not urgent, so it is totally okay to respond after
the firefighting is over.
Sang-Heon Jeon (3):
mm/slub: remove always-true check in calculate_sizes()
mm/slub: use kmem_cache_alloc() in bootstrap()
mm/slub: copy only the requested size in kvrealloc()
mm/slub.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
base-commit: 0b0985aeb9979c5fa5c3b872cecdf97ed4ed2b3e
--
2.53.0
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH 1/3] mm/slub: remove always-true check in calculate_sizes() 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 ` Sang-Heon Jeon 2026-12-01 16:52 ` [PATCH 2/3] mm/slub: use kmem_cache_alloc() in bootstrap() Sang-Heon Jeon ` (2 subsequent siblings) 3 siblings, 0 replies; 8+ messages in thread From: Sang-Heon Jeon @ 2026-12-01 16:52 UTC (permalink / raw) To: Vlastimil Babka, Harry Yoo, Andrew Morton Cc: Christoph Lameter, David Rientjes, Hao Li, linux-kernel, linux-mm, Roman Gushchin When calculate_order() succeeds, a slab of the returned order holds at least one and at most MAX_OBJS_PER_PAGE objects, so oo_objects(s->oo) is never zero. So remove the check and return 1 explicitly. No functional change. Signed-off-by: Sang-Heon Jeon <ekffu200098@gmail.com> --- mm/slub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/slub.c b/mm/slub.c index 47a664427c50..e966a0de1515 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -8233,7 +8233,7 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) s->oo = oo_make(order, size); s->min = oo_make(get_order(size), size); - return !!oo_objects(s->oo); + return 1; } static void list_slab_objects(struct kmem_cache *s, struct slab *slab) -- 2.53.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/3] mm/slub: use kmem_cache_alloc() in bootstrap() 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 ` Sang-Heon Jeon 2026-12-01 16:52 ` [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc() Sang-Heon Jeon 2026-12-02 11:29 ` [PATCH 0/3] mm/slub: two small cleanups and a small fix Jose A. Perez de Azpillaga 3 siblings, 0 replies; 8+ messages in thread From: Sang-Heon Jeon @ 2026-12-01 16:52 UTC (permalink / raw) To: Vlastimil Babka, Harry Yoo, Andrew Morton Cc: Christoph Lameter, David Rientjes, Hao Li, linux-kernel, linux-mm, Roman Gushchin bootstrap() allocates a kmem_cache with kmem_cache_zalloc(), which returns zero-initialized memory. memcpy() then overwrites the whole object, and nothing uses it in between. So allocate it with kmem_cache_alloc() instead. No functional change. Signed-off-by: Sang-Heon Jeon <ekffu200098@gmail.com> --- mm/slub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/slub.c b/mm/slub.c index e966a0de1515..8e6e900fc39b 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -8671,7 +8671,7 @@ static int slab_memory_callback(struct notifier_block *self, static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache) { int node; - struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT); + struct kmem_cache *s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT); struct kmem_cache_node *n; memcpy(s, static_cache, kmem_cache->object_size); -- 2.53.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc() 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 2026-12-02 14:51 ` Harry Yoo 2026-12-02 11:29 ` [PATCH 0/3] mm/slub: two small cleanups and a small fix Jose A. Perez de Azpillaga 3 siblings, 1 reply; 8+ messages in thread From: Sang-Heon Jeon @ 2026-12-01 16:52 UTC (permalink / raw) To: Vlastimil Babka, Harry Yoo, Andrew Morton Cc: Christoph Lameter, Danilo Krummrich, David Rientjes, Hao Li, linux-kernel, linux-mm, Michal Hocko, Roman Gushchin 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 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc() 2026-12-01 16:52 ` [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc() Sang-Heon Jeon @ 2026-12-02 14:51 ` Harry Yoo 2026-12-02 16:04 ` Sang-Heon Jeon 0 siblings, 1 reply; 8+ messages in thread From: Harry Yoo @ 2026-12-02 14:51 UTC (permalink / raw) To: Sang-Heon Jeon Cc: Vlastimil Babka, Andrew Morton, Christoph Lameter, Danilo Krummrich, David Rientjes, Hao Li, linux-kernel, linux-mm, Michal Hocko, Roman Gushchin 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@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)); 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc() 2026-12-02 14:51 ` Harry Yoo @ 2026-12-02 16:04 ` Sang-Heon Jeon 2026-12-02 16:25 ` Harry Yoo 0 siblings, 1 reply; 8+ messages in thread From: Sang-Heon Jeon @ 2026-12-02 16:04 UTC (permalink / raw) To: Harry Yoo Cc: Vlastimil Babka, Andrew Morton, Christoph Lameter, Danilo Krummrich, David Rientjes, Hao Li, linux-kernel, linux-mm, Michal Hocko, Roman Gushchin 2026년 12월 2일 (수) 오후 11:51, Harry Yoo <harry@kernel.org>님이 작성: > > 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@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)); > > Do we really need this when kvrealloc() frees a slab object and > allocates a vmalloc object(), why? The case that i identified is below, SLAB_STORE_USER enabled: 1. process A allocates with kzalloc(80). write something to it and free. 2. process B allocates with kzalloc(70). get the same object from A allocated. still 70-80 bytes have data from process A 3. process B calls kvrealloc() with __GFP_ZERO and allocates a vmalloc object. 4. then copy ksize(p) = 96 bytes which might include non-zero and did not originate from B. So, if you feel the existing logic is not harmful to production, I'm okay with that too. In that case, feel free to drop this patch. Let me know if I miss or misunderstand something. Thank you. > > kasan_enable_current(); > > > > kfree(p); > > -- > > 2.53.0 > > > > -- > Cheers, > Harry / Hyeonggon Best regards, Sang-Heon Jeon ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc() 2026-12-02 16:04 ` Sang-Heon Jeon @ 2026-12-02 16:25 ` Harry Yoo 0 siblings, 0 replies; 8+ messages in thread From: Harry Yoo @ 2026-12-02 16:25 UTC (permalink / raw) To: Sang-Heon Jeon Cc: Vlastimil Babka, Andrew Morton, Christoph Lameter, Danilo Krummrich, David Rientjes, Hao Li, linux-kernel, linux-mm, Michal Hocko, Roman Gushchin On Thu, Dec 03, 2026 at 01:04:56AM +0900, Sang-Heon Jeon wrote: > 2026년 12월 2일 (수) 오후 11:51, Harry Yoo <harry@kernel.org>님이 작성: > > > 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? > > The case that i identified is below, SLAB_STORE_USER enabled: > > 1. process A allocates with kzalloc(80). write something to it and free. > 2. process B allocates with kzalloc(70). get the same object from A > allocated. still 70-80 bytes have data from process A > 3. process B calls kvrealloc() with __GFP_ZERO and allocates a vmalloc object. > 4. then copy ksize(p) = 96 bytes which might include non-zero and did > not originate from B. Oh, I see. I overlooked that. > So, if you feel the existing logic is not harmful to production, I'm > okay with that too. In that case, feel free to drop this patch. A bug is a bug, it should be fixed anyway... 1. Could you please try to extract this to a helper and deduplicate this? __do_krealloc() has the same logic and this patch doesn't handle kfence objects. 2. Please make sure that this issue could be reproduced before this patch and this patch resolves the issue. Thanks! -- Cheers, Harry / Hyeonggon ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 0/3] mm/slub: two small cleanups and a small fix 2026-12-01 16:52 [PATCH 0/3] mm/slub: two small cleanups and a small fix Sang-Heon Jeon ` (2 preceding siblings ...) 2026-12-01 16:52 ` [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc() Sang-Heon Jeon @ 2026-12-02 11:29 ` Jose A. Perez de Azpillaga 3 siblings, 0 replies; 8+ messages in thread From: Jose A. Perez de Azpillaga @ 2026-12-02 11:29 UTC (permalink / raw) To: Sang-Heon Jeon, Andrew Morton, Harry Yoo, Vlastimil Babka Cc: Christoph Lameter, Danilo Krummrich, David Rientjes, Hao Li, linux-kernel, linux-mm, Michal Hocko, Roman Gushchin On 12/1/26 5:52 PM, Sang-Heon Jeon wrote: > Hello, > > While reading the slab code, I found two small cleanups and a small fix. > > Patch 1 removes the always-true check in calculate_sizes(). > > Patch 2 uses kmem_cache_alloc() in bootstrap() instead of > kmem_cache_zalloc(). > > Patch 3 copies only the requested size in kvrealloc() instead of > ksize(p) bytes. > > These are *really* not urgent, so it is totally okay to respond after > the firefighting is over. all LGTM: Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com> -- cheers, jose a. p-a ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-02 16:26 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 ` [PATCH 3/3] mm/slub: copy only the requested size in kvrealloc() Sang-Heon Jeon 2026-12-02 14:51 ` Harry Yoo 2026-12-02 16:04 ` Sang-Heon Jeon 2026-12-02 16:25 ` Harry Yoo 2026-12-02 11:29 ` [PATCH 0/3] mm/slub: two small cleanups and a small fix Jose A. Perez de Azpillaga
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox