From: Ren Wei <weir@nebusec.ai>
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 [thread overview]
Message-ID: <56dff40001dac2b78e4d53ff9cdf5991751823bf.1795767225.git.caoruide123@gmail.com> (raw)
In-Reply-To: <cover.1795767225.git.caoruide123@gmail.com>
From: Ruide Cao <caoruide123@gmail.com>
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 <vega@nebusec.ai>
Signed-off-by: Ruide Cao <caoruide123@gmail.com>
Signed-off-by: Ren Wei <weir@nebusec.ai>
---
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
prev parent reply other threads:[~2026-12-02 6:09 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 6:08 [PATCH net 0/1] inetpeer: bound the peer cache across network namespaces Ren Wei
2026-12-02 6:08 ` Ren Wei [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=56dff40001dac2b78e4d53ff9cdf5991751823bf.1795767225.git.caoruide123@gmail.com \
--to=weir@nebusec.ai \
--cc=caoruide123@gmail.com \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=vega@nebusec.ai \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox