From: Amir Hosseini <m…@amir.fyi>
alloc_large_system_hash() takes a low_limit, the smallest table the
caller can work with. But when an allocation fails, it keeps halving
the table and never looks at low_limit.
UDP and TCP both depend on that limit. Commit 31fe62b95866 ("mm: add a
low limit to alloc_large_system_hash") added it for UDP and dropped
UDP's own size check. So if the UDP table comes out too small, bind()
to port 0 overflows a bitmap on the stack. TCP's table_perturb always
uses its full size, and the first connect() writes past its end.
This only happens when an allocation fails at boot, for example when
most memory was reserved for HugeTLB pages. It's rare.
Stop halving at low_limit. If the allocation still fails, the kernel
panics with "Failed to allocate <name> hash table". It already does
that when the smallest table it tries can't be allocated. A panic at
boot is better than memory corruption later on.
Callers that pass a low_limit of 0 see no change. The others now panic
in this case, instead of getting a smaller table than they asked for.
I tested this in a single node QEMU guest with KASAN, by making the UDP
and table_perturb allocations fail. Without the patch, both tables
shrank and KASAN reported out-of-bounds writes. With it, the boot stops
with the panic.
Fixes: 31fe62b95866 ("mm: add a low limit to alloc_large_system_hash")
Cc: stab…@vger.kernel.org
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Amir Hosseini <m…@amir.fyi>
---
For UDP I used a test-only hack that fails its allocation above order 0.
The table ended up with 64 slots, and KASAN caught a stack-out-of-bounds
write in udp_lib_lport_inuse(). For table_perturb, plain fail_page_alloc
was enough. The table got halved and the kernel crashed on the first
connect().
table_perturb also has its own fix in net, which falls back to vmalloc
[1]. Neither patch depends on the other.
[1] https://lore.kernel.org/all/2026…@amir.fyi/
---
Changes in v2:
- Only stop halving at low_limit, drop the vmalloc_huge() fallback (Mike)
- Rewrite the changelog, rebase on v7.4-rc5
- Link to v1: https://patch.msgid.link/2026…@amir.fyi
---
mm/mm_init.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/mm/mm_init.c b/mm/mm_init.c
index d99af80ec752..36031ba34709 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -2359,6 +2359,7 @@ void __init page_alloc_init_late(void)
* - it is assumed that the hash table must contain an exact power-of-2
* quantity of entries
* - limit is the number of hash buckets, not the total allocation size
+ * - the table is not shrunk below low_limit buckets when an allocation fails
*/
void *__init alloc_large_system_hash(const char *tablename,
unsigned long bucketsize,
@@ -2371,7 +2372,7 @@ void *__init alloc_large_system_hash(const char *tablename,
unsigned long high_limit)
{
unsigned long long max = high_limit;
- unsigned long log2qty, size;
+ unsigned long log2qty, min_log2qty, size;
void *table;
gfp_t gfp_flags;
bool virt;
@@ -2420,6 +2421,7 @@ void *__init alloc_large_system_hash(const char *tablename,
numentries = max;
log2qty = ilog2(numentries);
+ min_log2qty = max(order_base_2(low_limit), 1);
gfp_flags = (flags & HASH_ZERO) ? GFP_ATOMIC | __GFP_ZERO : GFP_ATOMIC;
do {
@@ -2445,7 +2447,7 @@ void *__init alloc_large_system_hash(const char *tablename,
table = alloc_pages_exact(size, gfp_flags);
kmemleak_alloc(table, size, 1, gfp_flags);
}
- } while (!table && size > PAGE_SIZE && --log2qty);
+ } while (!table && size > PAGE_SIZE && --log2qty >= min_log2qty);
if (!table)
panic("Failed to allocate %s hash table\n", tablename);
---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261123-mm-hash-low-limit-37a3a47c7cd1
Best regards,
--
Amir Hosseini <m…@amir.fyi>
[PATCH v2] mm/mm_init: don't shrink large system hash tables below low_limit
- From
- devn…@kernel.org (Amir Hosseini via B4 Relay)
- Organization
- Usenet Network
- Message-ID
- <2026…@amir.fyi>
- Date
- Newsgroups
- linux.kernel.netdev