From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796204401; b=Hmnel7sFsizHmuvWXr24T6PD1Q9G+SDaQE3qgUrP9bdqfoSsLvTSL/9oRrVXK0AIu+gj Po0BeI2NXfcdJsRVAlNBZnOQAuXPS+rg0b4JRSLYuGgAA7sJLDLtBKKM3+8GnTHeGy0Fu s7GQovXPEsdQIiWg9wgSGFDLfTcRn1ARPjz5odIjJ/N2C1YvAh3VJHBjSKI+A5qwA7nyp aJfKUeYkdUprkhug2onMtySZS/M5HUhFWqRnGB0BaLO5aZT4KZlinf5z+r9VSVdZGIrTn F6sUw869tkixck0Yhc+FTOcxLcb17jlESWmzezyI9S4V5U86e9cIBwa5KtM2IpPPp8Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796204401; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References:X-Mailer: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=PEiSFyO8II3MtezXTx/ZRqALvumCid4JrIzemk2+dEU=; b=X/GX1KGpPbJC0Y2n6CfqecaEHRsuSwOONm0ooJ91zmVcMGBGvVd8X2TbeAZGVVaeXCyn j7sQMQVTHtrY29ZLvYgKMYUHSaDbqoXWO9UFgxFM5maTJX5QDuKvVf42Mn2dHBzf4R1Y1 8fIxsfllh0nT6B8Q7s+axFwZpS6XEF0BR9JkInhBCd+scvOSmxFcVkqYiWUIOhBjl2uhV /NGWDthDESKWjwN2aI3Xe8B4PlMM8GMlttiEvF7isPXRCEEbXCbGjtMEbeqWSyg8mhl57 CO1jUnxz1C3tKPEq+0tVIrwgdHMRdyAfPg/P7M7Fv7N2/g1jBZByFwOP7RwjVM/fNqQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=UmFNh5Lw; arc=none smtp.remote-ip=209.85.128.47 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F025D606A31A for ; Wed, 2 Dec 2026 09:40:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F025D606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=UmFNh5Lw Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4ba013938d7so119334755e9.2 for ; Wed, 02 Dec 2026 01:40:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796204399; x=1796809199; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=PEiSFyO8II3MtezXTx/ZRqALvumCid4JrIzemk2+dEU=; b=UmFNh5LwFpnHwbAMsOru6/txIt0oEXSgJpAilS/g8PLb6RPQ1gjx6E0rxbmy0eBGoV 5NokeVHU8L7FfVs9/OSMNoauTFH+7DG9aZJ/9rhPO4/4YLlKvZNnZp6Ovge0Hu4wroFu +AV0rAnkQw1Fhy3zq8pP3MJh9b1EbxOkuBpXflrbiIAdE0R5AZ3/Gcz9aJ6UzJigt1lQ +vAsMpPGhAwGhUoCVEVQeP7xcs0LrfWyaqvtIfuv9YYHZyMXzhiPnWK70XLlI/x0pX2U kaKsGIRtbVjeDUJgOFO/2Awdz+PVrYIMcjGiCyyHnySt3jyn0Y6Rxtx1dxa8xA7GNCaT 2j+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796204399; x=1796809199; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=PEiSFyO8II3MtezXTx/ZRqALvumCid4JrIzemk2+dEU=; b=PLuOLNgZNZi4v1OtQisXGp+qt6hk4E6y9kuwp/o9U6QSBCCyp8do/iY4ibS6Xr5cAu PmAn2vZUb5CRjQGfm9VCMLTFDNkphxPrxt1v7XR8H/pboK8J5dTvAk1EL3mAQgpnoREU bkk4MP/TUsX2Pvu4bjDHAjs5Aw9JLy6dgAi1CLrtBqJ3jDpwSdqdLvApACNd1jw+bARp 6+AlY6Ja1KHTA+JCM2fYI84hMsf8GapwtrDPF4ub3jmLzsTtR9CWy7R43/OUqZk3uSSP DPFsgjvjk5hIniMnAoL2Tudp0WcaOd3J9dOmaCWGOc4a9eeEtE8geQTXFZfIDM6NCCCY cW2A== X-Forwarded-Encrypted: i=1; AL+hErEX1Z/exshvol1+TAXbbPDfl+GFB0JQCeDVWPtevZ4CLjTETNYfjhVQKYmMK4uHAjXtBU8x5Ys=@vger.kernel.org X-Gm-Message-State: APuU4Z9m5qRhXgVJn0qluuAxkTM19r1yGxx2BlC0NcuMKdU4aRidPqPk FqHOpgiGNpPKRxeBJ0/P106F1MRWucbSWPFNa3UMgMWclVL+Uq9uhhyA X-Gm-Gg: AZFdTZdh+rdI7u1pakx2DMi29cTmAIEHvpiiX+dyv4WV85TBzh5zmJF6EwzYkRYtRwQ lYd3XoLUyrOkCXvnjmYTMSWsTRE1Qol6z0m7sM33uhvx6qBSGiKWtQYn32NuB0GEPvrDCCHXSMf PoSiiD8Ad4RyVIX0doUN/OGSL7jTsLBgAqPvAfltrLhhQui9f1swEqrf67vS/trIzYalGuH148t 9QbVBoUE2DU5KcmTEyPIQIQMup2/7eHGVVOY233HOuvUMGZqQV8RrRQ+Va8L1f280aSH8VtX2eB cEcCSsF7IjMxlIjrKc3Mx8TlFYJCkCzqZISovArKwuOxuoZoAyTr5v401QK65/79eilLBmHcIng RzG8mK5jVX223G+QoGm2GI/cDzboSy469GHBb+Sdwt90dM4vJqZFS9FVB0IF5/srBzGSqV2/5G7 BDrNkSxbmZFSGi8mSMkautX2vjiBwcMBwXW3y/cyZyOXhzhrzVw2uwM/oqg6ziTLzBro50gN+UL UnfzW1TNYvMEXxiJkb1r2zo3qzXO1wfJPnA7T1NbzM/iUqlwxL01vqfW1xbS7sJZ2xt5th9kWOR E29jmff/jYaPUn+WBoWanI8z34KhHybYFQNhjgRnEuy26wUpgLS5J/jyGpJFqg0u X-Received: by 2002:a05:600c:6290:b0:4bb:741d:eba with SMTP id 5b1f17b1804b1-4bb88824a5bmr761783165e9.13.1796204399077; Wed, 02 Dec 2026 01:39:59 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6bb989e3sm78190502f8f.52.2026.12.02.01.39.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 01:39:58 -0800 (PST) Date: Wed, 2 Dec 2026 09:39:57 +0000 From: David Laight To: Mike Rapoport Cc: me@amir.fyi, Andrew Morton , Eric Dumazet , "David S. Miller" , linux-mm@kvack.org, Muchun Song , netdev@vger.kernel.org, Christian Brauner Subject: Re: [PATCH] mm/mm_init: don't shrink large system hash tables below low_limit Message-ID: <20261202093957.01c5ae1a@pumpkin> In-Reply-To: References: <20261123-mm-hash-low-limit-v1-1-8a803aa8ac70@amir.fyi> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 26 Nov 2026 15:02:02 +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 > > > > 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: > > > > 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()? Or even, does it make sense to me allocating such large hash tables. Many systems won't allocate enough items to make them sensible. I'm sure there is a big one in SCTP - even systems that use SCTP might only have 4 active connections. David > > 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,,1, where 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 > > --- > > 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 > > > > >