* [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; 5+ 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] 5+ 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
2026-11-29 23:13 ` Amir Hosseini
2026-11-30 16:14 ` Christian Brauner
0 siblings, 2 replies; 5+ 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] 5+ messages in thread
* Re: [PATCH] mm/mm_init: don't shrink large system hash tables below low_limit
2026-11-26 13:02 ` Mike Rapoport
@ 2026-11-29 23:13 ` Amir Hosseini
2026-12-01 14:55 ` Mike Rapoport
2026-11-30 16:14 ` Christian Brauner
1 sibling, 1 reply; 5+ messages in thread
From: Amir Hosseini @ 2026-11-29 23:13 UTC (permalink / raw)
To: rppt; +Cc: akpm, edumazet, davem, brauner, muchun.song, linux-mm, netdev
On Thu, Nov 26, 2026 at 03:02:02PM +0200, Mike Rapoport wrote:
> This is hard to read :/
Sorry about that, I will rewrite it in v2.
> So, AFAIU the gist is here is that page allocator fails, let's fallback to
> vmalloc_huge().
The main problem is what happens after the failure. The loop keeps
halving the table below low_limit, but table_perturb passes no shift or
mask, so TCP still uses all 65536 entries. The first connect() then
writes random data past the end of the table. In my test it hit the TCP
established hash and the kernel crashed.
> 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.
It is rare, but it does happen. hugetlb_init() takes the boot time 2M
pages from the buddy allocator before inet_init() runs, so with a large
hugepages= there may be no order-6 block left. Muchun hit this failure
on a real server, which is why e67b72b90b7e moved table_perturb to
alloc_large_system_hash(). That works on NUMA because hashdist uses
vmalloc, but on a single node system the table is halved instead. On
mainline I could only trigger it with fail_page_alloc. I first saw it
with the out-of-tree ASI patches, where it happened in 3 of 4 boots
with 2 vCPUs.
> AFAIU, we can just drop the alloc_pages_exact() case and keep memblock for
> HASH_EARLY and vmalloc_huge() for the rest.
That would fix this case too, and 64-bit NUMA machines already work this
way. My only concerns are that on x86 tables smaller than 2M would be
mapped with 4K pages instead of the direct map's large pages, and that
32-bit has little vmalloc space, as the comment above HASHDIST_DEFAULT
says.
Either way, I think the loop should not go below low_limit, so that a
failure panics instead of corrupting memory. I can send v2 with that and
whichever vmalloc_huge() change you and Eric prefer.
Thanks,
Amir
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/mm_init: don't shrink large system hash tables below low_limit
2026-11-26 13:02 ` Mike Rapoport
2026-11-29 23:13 ` Amir Hosseini
@ 2026-11-30 16:14 ` Christian Brauner
1 sibling, 0 replies; 5+ messages in thread
From: Christian Brauner @ 2026-11-30 16:14 UTC (permalink / raw)
To: Mike Rapoport
Cc: me, Andrew Morton, Eric Dumazet, David S. Miller, linux-mm,
Muchun Song, netdev
On Thu, Nov 26, 2026 at 03:02:02PM +0200, Mike Rapoport wrote:
> (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?
I think the dentry and inode cache are unchanged by what you suggest.
Tables under 2 mb would get mapped with 4 k pages instead of direct-map
huge pages. That would affect mnt_init() and move it to vmalloc_huge().
Would probably mean a few tlb misses in lookup_mnt()? That needs to be
really really fast on mount crossings. So if this doesn't degrade lookup
performance I don't care that much.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/mm_init: don't shrink large system hash tables below low_limit
2026-11-29 23:13 ` Amir Hosseini
@ 2026-12-01 14:55 ` Mike Rapoport
0 siblings, 0 replies; 5+ messages in thread
From: Mike Rapoport @ 2026-12-01 14:55 UTC (permalink / raw)
To: Amir Hosseini
Cc: akpm, edumazet, davem, brauner, muchun.song, linux-mm, netdev
On Sun, Nov 29, 2026 at 11:13:12PM +0000, Amir Hosseini wrote:
> On Thu, Nov 26, 2026 at 03:02:02PM +0200, Mike Rapoport wrote:
> > This is hard to read :/
>
> Sorry about that, I will rewrite it in v2.
>
> > So, AFAIU the gist is here is that page allocator fails, let's fallback to
> > vmalloc_huge().
>
> The main problem is what happens after the failure. The loop keeps
> halving the table below low_limit, but table_perturb passes no shift or
> mask, so TCP still uses all 65536 entries. The first connect() then
> writes random data past the end of the table. In my test it hit the TCP
> established hash and the kernel crashed.
I looked more closely at the table_perturb case, and it seems it is really
set on using 65536 entries.
Since that table is allocated in an initcall why not simply replace it with
kvmalloc_array()?
If there is enough free contiguous memory, the kmalloc() branch will
allocate it. If not, vmalloc_huge() for 265k buffer anyway falls back to
small pages.
> > 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.
>
> It is rare, but it does happen. hugetlb_init() takes the boot time 2M
> pages from the buddy allocator before inet_init() runs, so with a large
> hugepages= there may be no order-6 block left. Muchun hit this failure
> on a real server, which is why e67b72b90b7e moved table_perturb to
> alloc_large_system_hash(). That works on NUMA because hashdist uses
> vmalloc, but on a single node system the table is halved instead. On
> mainline I could only trigger it with fail_page_alloc. I first saw it
> with the out-of-tree ASI patches, where it happened in 3 of 4 boots
> with 2 vCPUs.
>
> > AFAIU, we can just drop the alloc_pages_exact() case and keep memblock for
> > HASH_EARLY and vmalloc_huge() for the rest.
>
> That would fix this case too, and 64-bit NUMA machines already work this
> way. My only concerns are that on x86 tables smaller than 2M would be
> mapped with 4K pages instead of the direct map's large pages, and that
> 32-bit has little vmalloc space, as the comment above HASHDIST_DEFAULT
> says.
>
> Either way, I think the loop should not go below low_limit, so that a
> failure panics instead of corrupting memory. I can send v2 with that and
> whichever vmalloc_huge() change you and Eric prefer.
You are right that the loop should respect the low_limit, but the entire
alloc_large_system_hash() is quite convoluted and I believe it requires
more drastic refactoring.
> Thanks,
> Amir
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-01 14:55 UTC | newest]
Thread overview: 5+ 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
2026-11-29 23:13 ` Amir Hosseini
2026-12-01 14:55 ` Mike Rapoport
2026-11-30 16:14 ` Christian Brauner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox