* [PATCH] mm/mm_init: don't shrink large system hash tables below low_limit
@ 2026-11-23 20:15 Amir Hosseini via B4 Relay
2026-11-26 13:02 ` Mike Rapoport
0 siblings, 1 reply; 2+ messages in thread
From: Amir Hosseini via B4 Relay @ 2026-11-23 20:15 UTC (permalink / raw)
To: Mike Rapoport, Andrew Morton, Eric Dumazet, David S. Miller
Cc: linux-mm, Muchun Song, netdev, Amir Hosseini
From: Amir Hosseini <me@amir.fyi>
When an allocation fails, alloc_large_system_hash() halves the table and
retries until it is down to a single page. It does not stop at
low_limit, although commit 31fe62b95866 ("mm: add a low limit to
alloc_large_system_hash") added low_limit precisely so that callers need
not check the size they get back.
The TCP source port perturbation table depends on that. It passes
low_limit == high_limit == INET_TABLE_PERTURB_SIZE and neither
_hash_shift nor _hash_mask, so it cannot learn the real size, and
__inet_hash_connect() fills and indexes INET_TABLE_PERTURB_SIZE entries.
On a single node system hashdist is off and the table needs one order-6
GFP_ATOMIC allocation. If that fails, e.g. because most memory was
reserved for HugeTLB at boot, the situation commit e67b72b90b7e ("tcp:
use alloc_large_system_hash() to allocate table_perturb") was meant to
handle, the table is silently halved and the first connect() writes up
to 240 KiB of random bytes past its end, typically into the TCP
established hash table:
Table-perturb hash table entries: 32768 (order: 5, 131072 bytes, linear)
...
Oops: general protection fault, probably for non-canonical address 0xe480dbf6b420b3d9: 0000 [#1] SMP KASAN
RIP: 0010:__inet_lookup_established+0x168/0x5d0
Call Trace:
<IRQ>
tcp_v4_rcv
ip_protocol_deliver_rcu
ip_local_deliver
Stop halving at low_limit. If the linear allocation still fails, retry
the same size with vmalloc_huge(), which this function already uses for
tables that are too large for the page allocator or when hashdist is
set, and which does not need physically contiguous memory. If that
fails too, panic as before.
Reproduced on v7.4-rc4 in an x86_64 QEMU guest (virtme-ng, 1 node,
KASAN) by failing just that allocation with CONFIG_FAIL_PAGE_ALLOC and
fail_page_alloc=1,100,<space>,1, where <space> was measured with the
kmem:mm_page_alloc tracepoint so that the one injected failure hits the
Table-perturb allocation. Without this patch the table is halved and
the kernel crashes as above on the first connect(); with it the table
keeps 65536 entries (vmalloc) and the system works normally.
The reproducer and fix were developed with the help of Claude Code.
Fixes: 31fe62b95866 ("mm: add a low limit to alloc_large_system_hash")
Cc: stable@vger.kernel.org
Signed-off-by: Amir Hosseini <me@amir.fyi>
---
mm/mm_init.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/mm/mm_init.c b/mm/mm_init.c
index d99af80ec752..418068bad006 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 has at least low_limit entries, callers may rely on that
*/
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,19 @@ 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 &&
+ --log2qty);
+
+ /*
+ * Shrinking below low_limit would break callers that size other data
+ * from it, so fall back to vmalloc, which needs no contiguous memory.
+ */
+ if (!table && !virt && !(flags & HASH_EARLY)) {
+ table = vmalloc_huge(size, gfp_flags);
+ virt = true;
+ if (table)
+ huge = is_vm_area_hugepages(table);
+ }
if (!table)
panic("Failed to allocate %s hash table\n", tablename);
---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261123-mm-hash-low-limit-37a3a47c7cd1
Best regards,
--
Amir Hosseini <me@amir.fyi>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] mm/mm_init: don't shrink large system hash tables below low_limit
2026-11-23 20:15 [PATCH] mm/mm_init: don't shrink large system hash tables below low_limit Amir Hosseini via B4 Relay
@ 2026-11-26 13:02 ` Mike Rapoport
0 siblings, 0 replies; 2+ messages in thread
From: Mike Rapoport @ 2026-11-26 13:02 UTC (permalink / raw)
To: me
Cc: Andrew Morton, Eric Dumazet, David S. Miller, linux-mm,
Muchun Song, netdev, Christian Brauner
(added Christian)
On Mon, Nov 23, 2026 at 08:15:49PM +0000, Amir Hosseini via B4 Relay wrote:
> From: Amir Hosseini <me@amir.fyi>
>
> When an allocation fails, alloc_large_system_hash() halves the table and
> retries until it is down to a single page. It does not stop at
> low_limit, although commit 31fe62b95866 ("mm: add a low limit to
> alloc_large_system_hash") added low_limit precisely so that callers need
> not check the size they get back.
>
> The TCP source port perturbation table depends on that. It passes
> low_limit == high_limit == INET_TABLE_PERTURB_SIZE and neither
> _hash_shift nor _hash_mask, so it cannot learn the real size, and
> __inet_hash_connect() fills and indexes INET_TABLE_PERTURB_SIZE entries.
> On a single node system hashdist is off and the table needs one order-6
> GFP_ATOMIC allocation. If that fails, e.g. because most memory was
> reserved for HugeTLB at boot, the situation commit e67b72b90b7e ("tcp:
> use alloc_large_system_hash() to allocate table_perturb") was meant to
> handle, the table is silently halved and the first connect() writes up
> to 240 KiB of random bytes past its end, typically into the TCP
> established hash table:
This is hard to read :/
> Table-perturb hash table entries: 32768 (order: 5, 131072 bytes, linear)
> ...
> Oops: general protection fault, probably for non-canonical address 0xe480dbf6b420b3d9: 0000 [#1] SMP KASAN
> RIP: 0010:__inet_lookup_established+0x168/0x5d0
> Call Trace:
> <IRQ>
> tcp_v4_rcv
> ip_protocol_deliver_rcu
> ip_local_deliver
>
> Stop halving at low_limit. If the linear allocation still fails, retry
> the same size with vmalloc_huge(), which this function already uses for
> tables that are too large for the page allocator or when hashdist is
> set, and which does not need physically contiguous memory. If that
> fails too, panic as before.
So, AFAIU the gist is here is that page allocator fails, let's fallback to
vmalloc_huge().
The large hashes are normally allocated at boot time when memory is not yet
fragmented, so it's very unlikely that page allocator would fail.
But there is another question: do we really need physically contiguous
memory for the hashes we get from alloc_large_system_hash()?
AFAIU, we can just drop the alloc_pages_exact() case and keep memblock for
HASH_EARLY and vmalloc_huge() for the rest.
Eric, Christian, what do you think?
> Reproduced on v7.4-rc4 in an x86_64 QEMU guest (virtme-ng, 1 node,
> KASAN) by failing just that allocation with CONFIG_FAIL_PAGE_ALLOC and
> fail_page_alloc=1,100,<space>,1, where <space> was measured with the
> kmem:mm_page_alloc tracepoint so that the one injected failure hits the
> Table-perturb allocation. Without this patch the table is halved and
> the kernel crashes as above on the first connect(); with it the table
> keeps 65536 entries (vmalloc) and the system works normally.
>
> The reproducer and fix were developed with the help of Claude Code.
>
> Fixes: 31fe62b95866 ("mm: add a low limit to alloc_large_system_hash")
> Cc: stable@vger.kernel.org
> Signed-off-by: Amir Hosseini <me@amir.fyi>
> ---
> mm/mm_init.c | 18 ++++++++++++++++--
> 1 file changed, 16 insertions(+), 2 deletions(-)
>
> diff --git a/mm/mm_init.c b/mm/mm_init.c
> index d99af80ec752..418068bad006 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 has at least low_limit entries, callers may rely on that
> */
> 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,19 @@ 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 &&
> + --log2qty);
> +
> + /*
> + * Shrinking below low_limit would break callers that size other data
> + * from it, so fall back to vmalloc, which needs no contiguous memory.
> + */
> + if (!table && !virt && !(flags & HASH_EARLY)) {
> + table = vmalloc_huge(size, gfp_flags);
> + virt = true;
> + if (table)
> + huge = is_vm_area_hugepages(table);
> + }
>
> if (!table)
> panic("Failed to allocate %s hash table\n", tablename);
>
> ---
> base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
> change-id: 20261123-mm-hash-low-limit-37a3a47c7cd1
>
> Best regards,
> --
> Amir Hosseini <me@amir.fyi>
>
>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-26 13:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 20:15 [PATCH] mm/mm_init: don't shrink large system hash tables below low_limit Amir Hosseini via B4 Relay
2026-11-26 13:02 ` Mike Rapoport
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox