From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796191750; b=eWOPq6SWSzpBwMUnNKiDLJnNcx4UuJ51PBeM1ha7++Z713DrwZUJ8EpIupCzwNSrsrTb cJR8fY6m37H1oP5r/h013ofjqjv4AiDT+nZ8SIrLwV5IwOYHWECjImGZYk0TD4G5xM4mU Vmu8LuQLeVPm2oYmEi0JeL074VZgR+HKDzUD3FyqMCHQFBCE6eHeDogOHDhV2E/e5iI7s VoYcHwRtNoUfMsGgkFVFT/T237/yWNjngMN+iHSOnxML4jWzTjjb3jAFfE1D/IPp0LVxK sJW988nNSbjZyFey1Uaq2V5IJkNndwhfTc8e3FBrHddYp4ZJ/Hy9/LmgGC+TDOMSJpA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796191750; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=EZnhC8JvjPXA+gxnRH3zmGL0EAgImmRqM79IUe2kE38=; b=00vKaGLrj6D0RG135YE61DGJKjzlhv9vVNbJ0Iep0go2UfbBiBSPyjr+equJtM64cOr/ oube0xiP8azG0phWxsjXEIQ1x4BsgsZO4V0Hx3GA/TvGBsYYF56eG08UCh9WqWowWolXV M5UNaPyfXHcWE3B8jHoqI4nIfycMrzyYIr/e9N15NMe+koZ39mw26q7453vzq//8NdDKh kYZTIs5gL7JJgobQeEppihjm7T1pulTVt7UU4W64LnqqyvtOJD8MxwfkXaVnmbwkNnFjg AXV0KCfr51yaADax8aP/C6fgdfWI+fqy/tsoxREFmI0esh6uw66QA4OGr/07KnxGVOw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=nebusec.ai; spf=pass smtp.mailfrom=nebusec.ai; dkim=pass header.d=nebusec.ai header.i=@nebusec.ai header.a=rsa-sha256 header.s=google header.b=U9i9LzHB; arc=none smtp.remote-ip=74.125.82.47 Received: from mail-dl1-f47.google.com (mail-dl1-f47.google.com [74.125.82.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 B676C606A322 for ; Wed, 2 Dec 2026 06:09:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B676C606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=nebusec.ai Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nebusec.ai Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nebusec.ai header.i=@nebusec.ai header.a=rsa-sha256 header.s=google header.b=U9i9LzHB Received: by mail-dl1-f47.google.com with SMTP id a92af1059eb24-1aefd1bf74aso8077267c88.1 for ; Tue, 01 Dec 2026 22:09:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nebusec.ai; s=google; t=1796191749; x=1796796549; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=EZnhC8JvjPXA+gxnRH3zmGL0EAgImmRqM79IUe2kE38=; b=U9i9LzHBQQB2ucPTrv6hlrjScKjEO7YFpWul200VTqIHPG56a8KJtZTWdsJDMwNYkO eS2h+H9ymKOxFnbu3Z+6uMBETP0sYzsrBWigjLodjTRyzoPeI9zzWg4L/uStrOiSJ0Mr rcucbrqpm4pQM29lZMVaW2AAHaC3x8HbXrFeDfrUgM18AvZlL5Em2mE4n3/krhNgWP4/ WsMF51Q1nNhw7++BoyCB8GRQRQ+qebh6nfS/jEXiRT8mOEHQwfW+tofC3ltOlJghlP9z SE7SlgJyR1/FLdm+rVc/l7UVlDp/HX2i6S36XDd8P5e2yNuhqeD95v+gDig9bK5etJCz gZdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796191749; x=1796796549; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=EZnhC8JvjPXA+gxnRH3zmGL0EAgImmRqM79IUe2kE38=; b=yejI8y2zhk2Ok3o839r9AKYyqUJd4XPhmsOrdNyPgTEaQ51DIZnTrFwAyHLhmG1TG+ Qdr5+sX8qgpiu8qmVgA89iMrMC6aT7CxivUgFCcwBfvKEWLJRlfwkClwxpahsWVFiDi0 8ledaUE5R9ZxzH6149Hi+feZ5A+hjvTQ8uiz5oMdqyzBz42NwEJc2THncsb2TiHewdgw mo8reu1GhBhrmyTG6hLnJIJ7zPYDLkbbIy5k/vVvw0Gn8sq51lNAeFQ2g9nZ2YttylEz HUVj3eWyTbQqDI6a67ebuRpRlmtXCscGGrQcxfEW4Isixpdlfwp9XNO1ynq1cjqPO8oh ORcQ== X-Gm-Message-State: APuU4Z/PBPBZGq703pznDp8qqDYGC1qYZxB18Axo0pTMPvLlnoZBISIx 0KXnzxv6TXZCFA90UT5xEjKmDa/kx+2y0gv07PxbdcXsQTAMNa3sqgxOGAgWNDPuct7haEaJrIY k1Kb4Iw== X-Gm-Gg: AZFdTZevZJ2mNUcv2zJxI+KkHqCA0r5TzZKwZW+wX8CGbvPuk0SrcY2jbZqs9GJsugb NUXadwu/mw8zwoA8df8amolk8Im+0tE7vKRjTyRWdEY6L/jUsFVzv949aZPi7ZZ7VsGL4BvougK ssXgwjmfoTRiysVPwJXhpHVGRAg/TThrdrXB6AnSUb0Qf5J7BqsYaZnIOE5IVFxwfnEkhjpoOTq vpOpcHM/GABAzYVsQJJXEKAbagOgpS4jYNpnGIE4jgBOp3EAde+dA396TRzlRAvOshuudTiJCrm rCArklwJRcr3BGTNzYAPgh2/Id2d7VHuTWRJi4X1IH1h2sPJT1Vj9rGCqzhvkVQ2MRrRRAFZljr baYZ5FznLLRvjoBrxbq9Ip/3T5xGs7EWE0FlXaG2/0GAWz6ZQYlOUFMAHoV5XrQbigjDTJ1WvWB 0HgDybE9/UU/N4DX/3cAq6abC8p/VfrtUkMNrskN3rpB5deCrG+xtQzYsUZY2mDq79OQHhrE5qf G3bErl9hMPEgwtNo9EayqE1IeWiDybVIfJ1mRxpBG9aID/5/f5IGCqZ3Uq7zc3pt3PTzSiRmZvM 3sbPIt1whD2rLJmWAqMEpRrwONMkNplojbGhtxla01pq3e0pmp5ibaB84xs= X-Received: by 2002:a05:7022:7f19:b0:19f:cc98:f603 with SMTP id a92af1059eb24-1b5520d7d86mr35765482c88.1.1796191748578; Tue, 01 Dec 2026 22:09:08 -0800 (PST) Received: from enjou-Legion-Y7000P-2019 ([155.94.157.51]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5569bffd2sm84284730c88.0.2026.12.01.22.09.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 22:09:08 -0800 (PST) From: Ren Wei To: netdev@vger.kernel.org Cc: dsahern@kernel.org, idosch@nvidia.com, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, willemb@google.com, horms@kernel.org, vega@nebusec.ai, caoruide123@gmail.com, weir@nebusec.ai Subject: [PATCH net 1/1] inetpeer: bound the peer cache across network namespaces Date: Wed, 2 Dec 2026 14:08:59 +0800 Message-ID: <56dff40001dac2b78e4d53ff9cdf5991751823bf.1795767225.git.caoruide123@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: 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-Transfer-Encoding: 8bit From: Ruide Cao The inet_peer_threshold value is calculated from host memory. But each network namespace has its own IPv4 and IPv6 peer trees. Cache misses can fill every namespace up to the threshold. The shared slab cache is also not accounted to memory cgroups. commit 4b9d9be839fd ("inetpeer: remove unused list") dropped compute_total(). That function summed the IPv4 and IPv6 trees for a host-wide GC threshold. The commit switched to a per-base comparison. Later, commit c8a627ed06d6 ("inetpeer: add namespace support for inetpeer") moved those per-base trees into each network namespace. So the same budget is multiplied by the number of namespaces. Reserve entries from a host-wide atomic count before allocating. Release the reservation when the allocation fails or when a tree entry is removed. Also account the cache to the allocating memory cgroup. When the host-wide limit is reached, the current miss returns NULL. Callers already handle allocation failure. The miss is not retried in the same call. Per-network-namespace peer state is kept. The host-wide bound on the cache is restored. Eric Dumazet posted a related patch in August 2026. It would cap each base at twice inet_peer_threshold. A per-base cap does not limit the total across namespaces, and he later dropped the patch. This patch is not a reply to that thread and not a resend of that patch. It replaces that withdrawn attempt with a single host-wide counter. The SipHash tree-ordering change from that series, 2ee66e948717, is already upstream. It randomizes lookup and eviction. It does not provide a host-wide bound across namespaces. Link: https://lore.kernel.org/netdev/20260812205946.356185-2-edumazet@google.com/ Fixes: 4b9d9be839fd ("inetpeer: remove unused list") Cc: stable@vger.kernel.org Reported-by: Vega Signed-off-by: Ruide Cao Signed-off-by: Ren Wei --- net/ipv4/inetpeer.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c index adf6dc8a95b4..06c26dc3ff04 100644 --- a/net/ipv4/inetpeer.c +++ b/net/ipv4/inetpeer.c @@ -44,7 +44,7 @@ * 1. Nodes may appear in the tree only with the pool lock held. * 2. Nodes may disappear from the tree only with the pool lock held * AND reference count being 0. - * 3. Global variable peer_total is modified under the pool lock. + * 3. Global variable peer_total is modified atomically. * 4. struct inet_peer fields modification: * rb_node: pool lock * refcnt: atomically against modifications on other CPU; @@ -53,6 +53,7 @@ */ static struct kmem_cache *peer_cachep __ro_after_init; +static atomic_t peer_total; static siphash_aligned_key_t inetpeer_hash_key __read_mostly; static u64 inetpeer_addr_hash(const struct inetpeer_addr *a) @@ -97,6 +98,20 @@ int inet_peer_threshold __read_mostly; /* start to throw entries more int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */ int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min */ +static bool inet_peer_try_alloc(void) +{ + int threshold, total; + + threshold = READ_ONCE(inet_peer_threshold); + total = atomic_read(&peer_total); + do { + if (total >= threshold) + return false; + } while (!atomic_try_cmpxchg(&peer_total, &total, total + 1)); + + return true; +} + /* Called from ip_output.c:ip_init */ void __init inet_initpeers(void) { @@ -108,7 +123,8 @@ void __init inet_initpeers(void) inet_peer_threshold = clamp_val(nr_entries, 4096, 65536 + 128); - peer_cachep = KMEM_CACHE(inet_peer, SLAB_HWCACHE_ALIGN | SLAB_PANIC); + peer_cachep = KMEM_CACHE(inet_peer, + SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT); } /* Called with rcu_read_lock() or base->lock held */ @@ -190,6 +206,7 @@ static void inet_peer_gc(struct inet_peer_base *base, if (p) { rb_erase(&p->rb_node, &base->rb_root); base->total--; + atomic_dec(&peer_total); kfree_rcu(p, rcu); } } @@ -222,7 +239,7 @@ struct inet_peer *inet_getpeer(struct inet_peer_base *base, gc_cnt = 0; p = lookup(daddr, dhash, base, seq, gc_stack, &gc_cnt, &parent, &pp); - if (!p) { + if (!p && inet_peer_try_alloc()) { p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC); if (p) { p->daddr = *daddr; @@ -241,6 +258,8 @@ struct inet_peer *inet_getpeer(struct inet_peer_base *base, rb_link_node(&p->rb_node, parent, pp); rb_insert_color(&p->rb_node, &base->rb_root); base->total++; + } else { + atomic_dec(&peer_total); } } if (gc_cnt) @@ -309,6 +328,7 @@ void inetpeer_invalidate_tree(struct inet_peer_base *base) p = rb_next(p); rb_erase(&peer->rb_node, &base->rb_root); + atomic_dec(&peer_total); inet_putpeer(peer); cond_resched(); } -- 2.47.3