From: Mike Rapoport <rppt@kernel.org>
To: me@amir.fyi
Cc: Andrew Morton <akpm@linux-foundation.org>,
Eric Dumazet <edumazet@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
linux-mm@kvack.org, Muchun Song <muchun.song@linux.dev>,
netdev@vger.kernel.org, Christian Brauner <brauner@kernel.org>
Subject: Re: [PATCH] mm/mm_init: don't shrink large system hash tables below low_limit
Date: Thu, 26 Nov 2026 15:02:02 +0200 [thread overview]
Message-ID: <awgtymEawotNXV8f@kernel.org> (raw)
In-Reply-To: <20261123-mm-hash-low-limit-v1-1-8a803aa8ac70@amir.fyi>
(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.
next prev parent reply other threads:[~2026-11-26 13:02 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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-23 20:15 ` Amir Hosseini
2026-11-26 13:02 ` Mike Rapoport [this message]
2026-11-29 23:13 ` Amir Hosseini
2026-12-01 14:55 ` Mike Rapoport
2026-12-01 18:24 ` Amir Hosseini
2026-11-30 16:14 ` Christian Brauner
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=awgtymEawotNXV8f@kernel.org \
--to=rppt@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=linux-mm@kvack.org \
--cc=me@amir.fyi \
--cc=muchun.song@linux.dev \
--cc=netdev@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.