mm/slab_common: reject zero object_size before calculate_alignment

calculate_alignment() with SLAB_HWCACHE_ALIGN halves ralign in a
while (size <= ralign / 2) loop. When size is 0, ralign eventually
reaches 0 and the condition stays true indefinitely, hanging the
kernel.

kmem_cache_sanity_check() rejected size > KMALLOC_MAX_SIZE but not
size == 0. Add !size to the check so a 0-size kmem_cache_create() is
caught on CONFIG_DEBUG_VM builds, where it can be diagnosed when it
surfaces.

Suggested-by: Hao Li <hao.li@linux.dev>
Assisted-by: Codex:gpt-5.6-sol
Signed-off-by: Longlong Xia <xialonglong@kylinos.cn>
Reviewed-by: Hao Li <hao.li@linux.dev>
Reviewed-by: Harry Yoo (Meta) <harry@kernel.org>
Link: https://patch.msgid.link/20260826015506.3178758-1-xialonglong2025@163.com
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
This commit is contained in:
Longlong Xia 2026-08-26 09:55:06 +08:00 committed by Vlastimil Babka (SUSE)
parent 874d785e89
commit 208854282d

View File

@ -102,7 +102,7 @@ static bool kmem_cache_is_duplicate_name(const char *name)
static int kmem_cache_sanity_check(const char *name, unsigned int size)
{
if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) {
if (!name || in_interrupt() || !size || size > KMALLOC_MAX_SIZE) {
pr_err("kmem_cache_create(%s) integrity check failed\n", name);
return -EINVAL;
}