Article View: linux.kernel.netdev
Article #25731[PATCH v2] mm/mm_init: don't shrink large system hash tables below low_limit
From: Amir Hosseini via B4 Relay
Date: Wed, 02 Dec 2026 09:59
Date: Wed, 02 Dec 2026 09:59
99 lines
14674 bytes
14674 bytes
From: Amir Hosseini <me@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: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Amir Hosseini <me@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/20261201-tcp-perturb-kvmalloc-v1-1-deee7c745fa3@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/20261123-mm-hash-low-limit-v1-1-8a803aa8ac70@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 <me@amir.fyi>
Technical Details
Message-ID:
<20261202-mm-hash-low-limit-v2-1-44a8d2426acd@amir.fyi>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375714-linux-kernel-netdev=dewy.ruShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375714-linux-kernel-netdev=dewy.ru From: devnull+me.amir.fyi@kernel.org (Amir Hosseini via B4 Relay) Newsgroups: linux.kernel.netdev Subject: [PATCH v2] mm/mm_init: don't shrink large system hash tables below low_limit Date: Wed, 02 Dec 2026 09:59:43 +0000 Organization: Usenet Network Message-ID: <20261202-mm-hash-low-limit-v2-1-44a8d2426acd@amir.fyi> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Injection-Info: usenet.network; logging-data="1943002"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:pNZcKN4jKcDrmpbLexth+ad2FdY= sha256:3K3p7PxomvSj4Z5co81OeYc8pgDO0T/M4qaojHjVaZY= sha1:ULNP/g9QmYE+vWFRFdlPvTDLgxw= sha256:h6eVHMGEtbYoRTDTXueJFyufEMRq0z+5m0VTQtg5GF4= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 71B981175F5; Wed, 2 Dec 2026 13:00:26 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.4 X-Spam-Status: No, score=-3.4 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PSEHkE6GNfT2; Wed, 2 Dec 2026 13:00:22 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375714-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 673BF1175DB; Wed, 2 Dec 2026 13:00:14 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 2498F37999; Wed, 2 Dec 2026 10:00:08 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796205608; b=sNPMDGExegS8sunuXcXAmMqznTdnLmz+2KfNUYjympw74UyUMbd6pDGtBmHnO4xQGMKT 3evUHCgyVjCrjZ2cnFFns/EL9yDYfzekPdfIbnTyE18Pm1YHF7YRIAg+85/55/A6U9h7L 6oPDBtdW9507kZDLrQ/9jHP/Xtx5t9krduDfRGvp7RhzvJcLa5DVPiTJEQS7dQUUbd8iY DXrIYYJcM+E0sXz4UKFldNEN7EYRGxsK/Pc/GOt7MhZ/E1f9QONo4PRsH/s+Mq5zVV5nS cSmwy/AtwhE+I4hel+S4bYuc1MY0t3u32tOg+WZuIhHjtLmuw1r4qZnn8kTVWZqYbSQ== X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796205608; h=X-Original-To:Received:Received:DKIM-Signature:Received:From:Date: Subject:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=+1yJKXfIDwLvP1UBsYWc5y+URSlR79b80PTrPUP/Mu0=; b=H0ZBbDRd/OM0w5a60+cJoFP8U6U/mS2B2JOec23whW+b4GtGFpl20DW30Kl9PQASbl4u 1IoPEG5jXPqde7QRWmOApAZnXZdfZL177FF2Sbe/cQ/KQeyCX8yCQgXkzWkjRP2Jxe8yN AQS/GNXmwpyxss2TbaEiaOG22g/X6kO5bWaHtB/qt+VYzPB2CJ8TTpDQx3fPG5dwHdjNj zqbK1KJ6dCdHeutWDgioTgkixq+HXgxIyXQOKToDVPirbQYTyUziCU82/Ym9GLCf//4nv BETMn4MU33tyHj6P9hw61M4Dc3lujJA6b+A8QLtaFrOIWWYW4Azdu3pbidqSTprLhKQ== X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=azfhX7W0; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EFE6A606A329; Wed, 2 Dec 2026 10:00:07 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=azfhX7W0 X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796205607; b=3J8y5mVFVtjbosKnqrBp6uRNRyK8Lu38vYvRGpVgG7zcIqLW0izROCyMzYscEFwXUNCJ HaThlmDd0oYXGusZ1GYMbKJvqN6jw34cSa5+AHMevhwZ6739naW5JCOMRB7KmR2SkKdDs DleqkIZ9G1QrX6G+l30WshtLYj3xLfsc+jPbm2ntl779MSkXcEIB/HZk7Gzcl6VnPtZfr EJoAdNbOPm5o5exuv8SAk/QmSuC5bHoG6WgoBNtb4gQDghcyRZ6IZeEW+FTEz72mXmElq svIRuR1IU4lrSQncXe0Ho5dgFFdrmi0EAxR4xuoCSokW+3KuEPONSOqw/MkA+S9Uceg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796205607; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=+1yJKXfIDwLvP1UBsYWc5y+URSlR79b80PTrPUP/Mu0=; b=vKjZ/SIViULO80cuFjuwN6eGOD0WnfSUpDfGkbyr6cdjZr6Ble3oq2mAYT0spYztc5Vh 12rUhz5a/s1l7kWsUcPqvpCMjbXBHU2DzxD2CAszHGJxIq1ylj26yGzMZymTEKXh7unV6 ht67V6Jawein5Hu2iyKEKvFCXuGeae2PIvS9xjXL+kiRLGH3xj9FERF125BG8LkxG5DRt JpOvpx4dxmS02pWi4ocEXkMAcOTqpOmU8st/3nK1Gkl/HJlmQSFirHlDDzk7Iv2IZse8i EpRgQysocwsWvTAMuYTWk/IwaXZ2+cbf42DZ4dbo0OExCNShPRB8Y8bIlZpq8Zuj+Lw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=azfhX7W0; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 164D9606A32D for <netdev@vger.kernel.org>; Wed, 2 Dec 2026 10:00:06 +0000 (UTC) X-Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 X-ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796205606; b=gqd/bNGVdg6BqK8BaiwC81DCOHWakP2swOCULLu10lZYXt5EZ9c6MQmv2XiyCJQ6GZqG Nfv0y/j8vvFt2Wu3Nf55d45Ekn0veyX+9z7sjqPY0+aBHJx7VMpkZ6Vs+woym5AsOxJY1 DgiwzbMXkv1q+mKWJj8+0CGbbRiNO9678gHgV1xHLMLeXM6oirPfhjWeAqyM/eI74PCYw INi4FRRRKWzA9oVryKrUtllc1SLieVDDjvAexJ0NddoCRJbsktb21OVMsjAb/d4Z1/4qb 3Q7vYYvg7EOeZNbrxjSGwOIn+blSX8eA6Xm+VGox649xgyi3uzNPEbrmGF769HZkXZA== X-ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796205606; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=+1yJKXfIDwLvP1UBsYWc5y+URSlR79b80PTrPUP/Mu0=; b=g8rjpUmHC/xM1P4HogcCAzJ4Cs+hgc9Zl7EKm8Ioa2nQFDOf53mBuRMwrTIcgFzDDB5A +wE2IKvtY3kiNcfEZU0ohKIzyFU9Pyp9erv9iWpv3pE3fvZaiwk/fW5+qmw/LGnzccGHr EQfeUGx2yyvxldrZ5xQJct7d+BS1IVP/3Wq9un+ywNNxY9KomjMecOqh120NW3svLzhGP eAjxy2kaBj5aAIQdpZATURkYzMuwH/9Xu5GxTcgYoa84TaFSYPdDl5VPLKNOdeDu6YkMO 7SBk56oWo/YD3Ns+8b2ZRx2V3c5XESDdT7Jshhno3eStWdT0s7dsTEVP7S5XFlErE5A== X-ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 X-Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id B5E8D1F00899; Wed, 2 Dec 2026 09:59:45 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796205606; bh=+1yJKXfIDwLvP1UBsYWc5y+URSlR79b80PTrPUP/Mu0=; h=From:Date:Subject:To:Cc:Reply-To; b=azfhX7W0ui+351SUThKzju8e77J37xi0Y0IqxdW2mXnNwcCfOTo5jPipnhhhjZ28e psA7bDHmy4GECpmD9RRBXMnyxTwRiLsK5/j/sK4bPerSOEWDIEprGfwHRooHTC2Hyw 0qWUqyt9x4MCeKkt/rl54GhRRzP+8vducUv4S/NWprepITqeTFhWh63HvptBiaU2kx DpyKXm0T5/fjvVn+IdMzMRieWBZnHdM/pl72s9YjvW6aHpR+kXDDHVj5BhuYPEMmJj YFOVVfABFHmZnKPM3t/uPFQ0z8geVPDu0OX0EbNU94WNrK4vB4QAd8N+RkgjQmAUkl M7x8UvitPXh8A== X-Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 172584F364; Wed, 2 Dec 2026 09:59:45 +0000 (UTC) X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-B4-Tracking: v=1; b=H4sIAAAAAAAC/32NQQ6CMBBFr0Jm7RjaGtu48h6GxViKHUOBtIgSw t0tuHf5kvffXyC5yC7BpVgguokT910GeSjAeuoeDrnODLKUZyGkwhDQU/LY9m9sOfCISpOik7b a1gLyboiu4c/evFU/Tq/709lxC22G5zT2cd5PJ7F5//qTQIGGTKmIDFldXilwPDYzQ7Wu6xcii i5TwwAAAA== X-Change-ID: 20261123-mm-hash-low-limit-37a3a47c7cd1 X-Cc: Eric Dumazet <edumazet@kernel.org>, "David S. Miller" <davem@davemloft.net>, Willem de Bruijn <willemdebruijn.kernel@gmail.com>, Christian Brauner <brauner@kernel.org>, Muchun Song <muchun.song@linux.dev>, linux-mm@kvack.org, netdev@vger.kernel.org, Amir Hosseini <me@amir.fyi> X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796205584; l=3981; i=me@amir.fyi; s=archbox; h=from:subject:message-id; bh=uROSNLpJ4djfqpCSO7dXIrwm4BjpZrlrzBkvx41N+No=; b=ZYr0DN3qsJQbCXdSGxKI8mqsvU8Mdx451F96WFUaUzg9l+2QxH0voqli0VllO78ZhFTnxq1w+ r4TDagPQByGBIQFbtsWXzXPP2QWPV+4OxSXGdrt6RVcA4pbVkL595Z9 X-Developer-Key: i=me@amir.fyi; a=ed25519; pk=YmuzvXl0k+J8pTqIG0kjwtCeJ1ggyIgBHm2jFmzNbeU= X-Endpoint-Received: by B4 Relay for me@amir.fyi/archbox with auth_id=1620 X-Original-From: Amir Hosseini <me@amir.fyi> X-Reply-To: me@amir.fyi