* [PATCH net v2] tcp: avoid table_perturb overflow on single node hosts
@ 2026-12-02 19:35 ` Amir Hosseini via B4 Relay
0 siblings, 0 replies; 5+ messages in thread
From: Amir Hosseini via B4 Relay @ 2026-12-02 19:35 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, Kuniyuki Iwashima, David S. Miller,
Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
Muchun Song
Cc: Mike Rapoport, netdev, linux-mm, Amir Hosseini
From: Amir Hosseini <me@amir.fyi>
Since commit e67b72b90b7e ("tcp: use alloc_large_system_hash() to
allocate table_perturb"), table_perturb comes from
alloc_large_system_hash(). On 64-bit hosts with more than one node with
memory, hashdist is set and the table comes from vmalloc_huge().
Everywhere else it needs one contiguous 256KB block.
When that block isn't there, for example after many HugeTLB pages were
reserved at boot, alloc_large_system_hash() retries with smaller sizes
and ignores low_limit. The caller is never told. __inet_hash_connect()
still uses all INET_TABLE_PERTURB_SIZE entries, so the first connect()
writes past the end of the table.
I hit this on a single node QEMU guest with KASAN, using fail_page_alloc
to fail just this allocation. The table was halved to 32768 entries and
the kernel crashed on the first connect().
Switch to kvmalloc_array(). If a contiguous block is free, the table
comes from the linear map. If not, kvmalloc_array() falls back to
vmalloc(). Either way the table keeps its full size.
kvmalloc_array() was also proposed in 2022, but
alloc_large_system_hash() was picked because it spread the table across
NUMA nodes. That changed with commit a06157804399 ("mm/vmalloc: request
large order pages from buddy allocator"). Now vmalloc() takes the whole
table as one block from one node when it can, and only spreads the
pages when there is no such block. That is the same case where
kvmalloc_array() falls back to vmalloc(), so NUMA hosts lose nothing.
Fixes: e67b72b90b7e ("tcp: use alloc_large_system_hash() to allocate table_perturb")
Cc: stable@vger.kernel.org
Suggested-by: Mike Rapoport <rppt@kernel.org>
Link: https://lore.kernel.org/all/aw7fxwCeHX5jFYW1@kernel.org/
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Amir Hosseini <me@amir.fyi>
---
I also tried a 2 node guest. When I forced vmalloc() to use smaller
blocks, the table was split evenly across both nodes.
The low_limit loop itself is fixed in a separate mm patch [1]. The 2022
discussion where alloc_large_system_hash() was picked is in [2].
[1] https://lore.kernel.org/all/20261202-mm-hash-low-limit-v2-1-44a8d2426acd@amir.fyi/
[2] https://lore.kernel.org/all/CANn89iKeOfDNmy0zPWHctuUQMb4UTiGxza9j3QVMsjuXFmeuhQ@mail.gmail.com/
---
Changes in v2:
- Drop the NUMA branch, use kvmalloc_array() everywhere (Mike)
- Explain why NUMA hosts keep the same spreading
- Say that alloc_large_system_hash() ignores low_limit, and fix the
hashdist wording (Mike)
- Link to v1: https://patch.msgid.link/20261201-tcp-perturb-kvmalloc-v1-1-deee7c745fa3@amir.fyi
---
net/ipv4/inet_hashtables.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
index 860145d810ed..59507d78b42f 100644
--- a/net/ipv4/inet_hashtables.c
+++ b/net/ipv4/inet_hashtables.c
@@ -1310,12 +1310,10 @@ void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
}
/* this one is used for source ports of outgoing connections */
- table_perturb = alloc_large_system_hash("Table-perturb",
- sizeof(*table_perturb),
- INET_TABLE_PERTURB_SIZE,
- 0, 0, NULL, NULL,
- INET_TABLE_PERTURB_SIZE,
- INET_TABLE_PERTURB_SIZE);
+ table_perturb = kvmalloc_array(INET_TABLE_PERTURB_SIZE,
+ sizeof(*table_perturb), GFP_KERNEL);
+ if (!table_perturb)
+ panic("TCP: failed to alloc table_perturb");
}
int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
---
base-commit: b6c8f45ed340f00bf2004d407cf7316fe3e24c48
change-id: 20261201-tcp-perturb-kvmalloc-2e3de54b992b
Best regards,
--
Amir Hosseini <me@amir.fyi>
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH net v2] tcp: avoid table_perturb overflow on single node hosts
2026-12-02 19:35 ` Amir Hosseini via B4 Relay
(?)
@ 2026-12-03 11:09 ` Mike Rapoport
-1 siblings, 0 replies; 5+ messages in thread
From: Mike Rapoport @ 2026-12-03 11:09 UTC (permalink / raw)
To: me
Cc: Eric Dumazet, Neal Cardwell, Kuniyuki Iwashima, David S. Miller,
Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
Muchun Song, netdev, linux-mm
On Wed, Dec 02, 2026 at 07:35:03PM +0000, Amir Hosseini via B4 Relay wrote:
> From: Amir Hosseini <me@amir.fyi>
>
> Since commit e67b72b90b7e ("tcp: use alloc_large_system_hash() to
> allocate table_perturb"), table_perturb comes from
> alloc_large_system_hash(). On 64-bit hosts with more than one node with
> memory, hashdist is set and the table comes from vmalloc_huge().
> Everywhere else it needs one contiguous 256KB block.
>
> When that block isn't there, for example after many HugeTLB pages were
> reserved at boot, alloc_large_system_hash() retries with smaller sizes
> and ignores low_limit. The caller is never told. __inet_hash_connect()
> still uses all INET_TABLE_PERTURB_SIZE entries, so the first connect()
> writes past the end of the table.
>
> I hit this on a single node QEMU guest with KASAN, using fail_page_alloc
> to fail just this allocation. The table was halved to 32768 entries and
> the kernel crashed on the first connect().
>
> Switch to kvmalloc_array(). If a contiguous block is free, the table
> comes from the linear map. If not, kvmalloc_array() falls back to
> vmalloc(). Either way the table keeps its full size.
>
> kvmalloc_array() was also proposed in 2022, but
> alloc_large_system_hash() was picked because it spread the table across
> NUMA nodes. That changed with commit a06157804399 ("mm/vmalloc: request
> large order pages from buddy allocator"). Now vmalloc() takes the whole
> table as one block from one node when it can, and only spreads the
> pages when there is no such block. That is the same case where
> kvmalloc_array() falls back to vmalloc(), so NUMA hosts lose nothing.
>
> Fixes: e67b72b90b7e ("tcp: use alloc_large_system_hash() to allocate table_perturb")
> Cc: stable@vger.kernel.org
> Suggested-by: Mike Rapoport <rppt@kernel.org>
> Link: https://lore.kernel.org/all/aw7fxwCeHX5jFYW1@kernel.org/
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Amir Hosseini <me@amir.fyi>
Acked-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
> ---
> I also tried a 2 node guest. When I forced vmalloc() to use smaller
> blocks, the table was split evenly across both nodes.
>
> The low_limit loop itself is fixed in a separate mm patch [1]. The 2022
> discussion where alloc_large_system_hash() was picked is in [2].
>
> [1] https://lore.kernel.org/all/20261202-mm-hash-low-limit-v2-1-44a8d2426acd@amir.fyi/
> [2] https://lore.kernel.org/all/CANn89iKeOfDNmy0zPWHctuUQMb4UTiGxza9j3QVMsjuXFmeuhQ@mail.gmail.com/
> ---
> Changes in v2:
> - Drop the NUMA branch, use kvmalloc_array() everywhere (Mike)
> - Explain why NUMA hosts keep the same spreading
> - Say that alloc_large_system_hash() ignores low_limit, and fix the
> hashdist wording (Mike)
> - Link to v1: https://patch.msgid.link/20261201-tcp-perturb-kvmalloc-v1-1-deee7c745fa3@amir.fyi
> ---
> net/ipv4/inet_hashtables.c | 10 ++++------
> 1 file changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
> index 860145d810ed..59507d78b42f 100644
> --- a/net/ipv4/inet_hashtables.c
> +++ b/net/ipv4/inet_hashtables.c
> @@ -1310,12 +1310,10 @@ void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
> }
>
> /* this one is used for source ports of outgoing connections */
> - table_perturb = alloc_large_system_hash("Table-perturb",
> - sizeof(*table_perturb),
> - INET_TABLE_PERTURB_SIZE,
> - 0, 0, NULL, NULL,
> - INET_TABLE_PERTURB_SIZE,
> - INET_TABLE_PERTURB_SIZE);
> + table_perturb = kvmalloc_array(INET_TABLE_PERTURB_SIZE,
> + sizeof(*table_perturb), GFP_KERNEL);
> + if (!table_perturb)
> + panic("TCP: failed to alloc table_perturb");
> }
>
> int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
>
> ---
> base-commit: b6c8f45ed340f00bf2004d407cf7316fe3e24c48
> change-id: 20261201-tcp-perturb-kvmalloc-2e3de54b992b
>
> Best regards,
> --
> Amir Hosseini <me@amir.fyi>
>
>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH net v2] tcp: avoid table_perturb overflow on single node hosts
2026-12-02 19:35 ` Amir Hosseini via B4 Relay
(?)
(?)
@ 2026-12-03 11:13 ` Muchun Song
-1 siblings, 0 replies; 5+ messages in thread
From: Muchun Song @ 2026-12-03 11:13 UTC (permalink / raw)
To: me
Cc: Muchun Song, Eric Dumazet, Neal Cardwell, Kuniyuki Iwashima,
David S. Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
Simon Horman, Mike Rapoport, netdev, linux-mm
> On Dec 3, 2026, at 03:35, Amir Hosseini via B4 Relay <devnull+me.amir.fyi@kernel.org> wrote:
>
> From: Amir Hosseini <me@amir.fyi>
>
> Since commit e67b72b90b7e ("tcp: use alloc_large_system_hash() to
> allocate table_perturb"), table_perturb comes from
> alloc_large_system_hash(). On 64-bit hosts with more than one node with
> memory, hashdist is set and the table comes from vmalloc_huge().
> Everywhere else it needs one contiguous 256KB block.
>
> When that block isn't there, for example after many HugeTLB pages were
> reserved at boot, alloc_large_system_hash() retries with smaller sizes
> and ignores low_limit. The caller is never told. __inet_hash_connect()
> still uses all INET_TABLE_PERTURB_SIZE entries, so the first connect()
> writes past the end of the table.
>
> I hit this on a single node QEMU guest with KASAN, using fail_page_alloc
> to fail just this allocation. The table was halved to 32768 entries and
> the kernel crashed on the first connect().
>
> Switch to kvmalloc_array(). If a contiguous block is free, the table
> comes from the linear map. If not, kvmalloc_array() falls back to
> vmalloc(). Either way the table keeps its full size.
>
> kvmalloc_array() was also proposed in 2022, but
> alloc_large_system_hash() was picked because it spread the table across
> NUMA nodes. That changed with commit a06157804399 ("mm/vmalloc: request
> large order pages from buddy allocator"). Now vmalloc() takes the whole
> table as one block from one node when it can, and only spreads the
> pages when there is no such block. That is the same case where
> kvmalloc_array() falls back to vmalloc(), so NUMA hosts lose nothing.
>
> Fixes: e67b72b90b7e ("tcp: use alloc_large_system_hash() to allocate table_perturb")
> Cc: stable@vger.kernel.org
> Suggested-by: Mike Rapoport <rppt@kernel.org>
> Link: https://lore.kernel.org/all/aw7fxwCeHX5jFYW1@kernel.org/
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Amir Hosseini <me@amir.fyi>
Acked-by: Muchun Song (ByteDance) <muchun.song@linux.dev>
Thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH net v2] tcp: avoid table_perturb overflow on single node hosts
2026-12-02 19:35 ` Amir Hosseini via B4 Relay
` (2 preceding siblings ...)
(?)
@ 2026-12-04 0:08 ` Amir Hosseini
-1 siblings, 0 replies; 5+ messages in thread
From: Amir Hosseini @ 2026-12-04 0:08 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell, Kuniyuki Iwashima, David S. Miller,
Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
Muchun Song
Cc: Mike Rapoport, netdev, linux-mm
About the netdev AI review [1]:
"NUMA hosts lose nothing" only holds since a06157804399 (v6.19). On
stable trees up to 6.18 the table will usually come from kmalloc, on
one node, instead of being spread across nodes. They still need the
fix though. I can say this in a v3 if you want.
The INT_MAX point is right too. If CONFIG_INET_TABLE_PERTURB_ORDER is
29 or more (EXPERT only, default 16), the table is 2GB or more. Today
vmalloc can still allocate that. With this patch it panics at boot. I
doubt anyone uses it, but I can add a range to the Kconfig entry in a
separate patch.
Btw, I missed that the netdev docs ask fixes to say how the bug was
found. I hit this while testing Brendan Jackman's ASI series, where
this boot allocation often can't get a 256KB block. On mainline I
could only trigger it with fail_page_alloc.
[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202-tcp-perturb-kvmalloc-v2-1-03af5be7b237@amir.fyi
Thanks,
Amir
^ permalink raw reply [flat|nested] 5+ messages in thread