Article View: linux.kernel.netdev
Article #17824[PATCH nf-next v1 1/5] netfilter: nf_conncount: bound the gc walk by examined entries
From: Vladimir Vdovin
Date: Tue, 17 Nov 2026 11:53
Date: Tue, 17 Nov 2026 11:53
128 lines
15184 bytes
15184 bytes
The gc walk in __nf_conncount_add() and __nf_conncount_gc_list() stops
once CONNCOUNT_GC_MAX_COLLECT entries have been evicted, but nothing
bounds how many entries it examines to get there. Live entries and
entries another cpu is still adding (-EAGAIN) do not count, so on a
list made of such entries every pass walks the whole list, one
conntrack hash lookup per entry, under list_lock with BH disabled.
Stop a pass after CONNCOUNT_GC_MAX_SCAN examined entries and rotate
the list so that the next pass resumes where this one stopped.
Successive passes still cover the whole list, so stale entries are
reclaimed as before, spread over several passes instead of one.
The walk done for an already confirmed conntrack looks for a
duplicate, which can be anywhere in the list, so it stays unbounded.
Signed-off-by: Vladimir Vdovin <deliran@verdict.gg>
---
net/netfilter/nf_conncount.c | 34 +++++++++++++++++++++++++++++++---
1 file changed, 31 insertions(+), 3 deletions(-)
diff --git a/net/netfilter/nf_conncount.c b/net/netfilter/nf_conncount.c
index eca7a8d330ce..84290e363245 100644
--- a/net/netfilter/nf_conncount.c
+++ b/net/netfilter/nf_conncount.c
@@ -36,6 +36,8 @@
#define CONNCOUNT_GC_MAX_NODES 8
#define CONNCOUNT_GC_MAX_COLLECT 64
+/* Upper bound on the number of entries examined by one gc pass. */
+#define CONNCOUNT_GC_MAX_SCAN (CONNCOUNT_GC_MAX_COLLECT * 2)
#define MAX_KEYLEN 5
/* we will save the tuples of all connections we care about */
@@ -98,6 +100,19 @@ static void conn_free(struct nf_conncount_list *list,
kmem_cache_free(conncount_conn_cachep, conn);
}
+/* A gc pass that stopped at @conn has examined every entry in front of it.
+ * Move those behind @conn so that the next pass resumes where this one
+ * stopped instead of rescanning the same live entries over and over.
+ */
+static void gc_resume_at(struct nf_conncount_list *list,
+ struct nf_conncount_tuple *conn)
+{
+ lockdep_assert_held(&list->list_lock);
+
+ if (&conn->node != &list->head)
+ list_rotate_to_front(&conn->node, &list->head);
+}
+
static const struct nf_conntrack_tuple_hash *
find_or_evict(struct net *net, struct nf_conncount_list *list,
struct nf_conncount_tuple *conn)
@@ -176,8 +191,10 @@ static int __nf_conncount_add(struct net *net,
struct nf_conncount_tuple *conn, *conn_n;
struct nf_conntrack_tuple tuple;
struct nf_conn *ct = NULL;
+ unsigned int max_scan = CONNCOUNT_GC_MAX_SCAN;
struct nf_conn *found_ct;
unsigned int collect = 0;
+ unsigned int scanned = 0;
bool refcounted = false;
int err = 0;
@@ -195,6 +212,10 @@ static int __nf_conncount_add(struct net *net,
goto out_put;
}
+ /* The entry for this connection can be anywhere in the list,
+ * so deduplication needs a full walk.
+ */
+ max_scan = UINT_MAX;
goto check_connections;
}
@@ -205,8 +226,9 @@ static int __nf_conncount_add(struct net *net,
check_connections:
/* check the saved connections */
list_for_each_entry_safe(conn, conn_n, &list->head, node) {
- if (collect > CONNCOUNT_GC_MAX_COLLECT)
+ if (collect > CONNCOUNT_GC_MAX_COLLECT || scanned >= max_scan)
break;
+ scanned++;
found = find_or_evict(net, list, conn);
if (IS_ERR(found)) {
@@ -247,6 +269,7 @@ static int __nf_conncount_add(struct net *net,
nf_ct_put(found_ct);
}
+ gc_resume_at(list, conn);
list->last_gc = (u32)jiffies;
list->last_gc_count = list->count;
@@ -310,6 +333,7 @@ static bool __nf_conncount_gc_list(struct net *net,
struct nf_conncount_tuple *conn, *conn_n;
struct nf_conn *found_ct;
unsigned int collected = 0;
+ unsigned int scanned = 0;
bool ret = false;
/* don't bother if we just did GC */
@@ -317,6 +341,11 @@ static bool __nf_conncount_gc_list(struct net *net,
return false;
list_for_each_entry_safe(conn, conn_n, &list->head, node) {
+ if (collected > CONNCOUNT_GC_MAX_COLLECT ||
+ scanned >= CONNCOUNT_GC_MAX_SCAN)
+ break;
+ scanned++;
+
found = find_or_evict(net, list, conn);
if (IS_ERR(found)) {
if (PTR_ERR(found) == -ENOENT)
@@ -337,9 +366,8 @@ static bool __nf_conncount_gc_list(struct net *net,
}
nf_ct_put(found_ct);
- if (collected > CONNCOUNT_GC_MAX_COLLECT)
- break;
}
+ gc_resume_at(list, conn);
if (!list->count)
ret = true;
--
2.56.0
Technical Details
Message-ID:
<20261117115308.95134-2-deliran@verdict.gg>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-365208-linux-kernel-netdev=dewy.ruReferences:
<20261117115308.95134-1-deliran@verdict.gg>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-365208-linux-kernel-netdev=dewy.ru From: deliran@verdict.gg (Vladimir Vdovin) Newsgroups: linux.kernel.netdev Subject: [PATCH nf-next v1 1/5] netfilter: nf_conncount: bound the gc walk by examined entries Date: Tue, 17 Nov 2026 14:53:04 +0300 Organization: Usenet Network Message-ID: <20261117115308.95134-2-deliran@verdict.gg> References: <20261117115308.95134-1-deliran@verdict.gg> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="2320160"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:8x0UbIyeqWDgaylbWE17ZtZwG1U= sha256:emscXP9bcViqYysPt3FpRXNQBs/Ry4lxNwORJBbh6Aw= sha1:PcCO+ZhgQsa9bb3p/39ogy47QZo= sha256:fC3s0s7ofzGs3JN7bmMITEEpnaVB6ilzrygsgdKbWCA= 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 6A0F8114F2C; Tue, 17 Nov 2026 14:54:15 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.05 X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, 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=verdict.gg 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 r85oeF_xCALA; Tue, 17 Nov 2026 14:54:12 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-365208-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=verdict.gg X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id 3D061114F29; Tue, 17 Nov 2026 14:54:03 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id CB4CF326B5; Tue, 17 Nov 2026 11:53:57 +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=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794916430; b=39OE60nFXpzDYmtLKJXk2P2R4+adwcUdDJK3Bf1VCPQqRM1WjOv7jNTwgrl9nzO9sOD4 5hHY+dqZoOoME8KJcQPKN5ny+kP6chwgyn7OOSE8DRbFOudnEoofiC3qe6aTS4vFRhw67 bPtN08Fs/uVwU9WOqNMoKPk0gySe9SbWY86y0jhoK2qfpZQK5SK1jGaUuiu2x3cyapTkU 7cvrwvcUlC4kRhSJDqdwFBnp2xsIj4WyOenJ9RlobT8HHX+KPhF5eU7bEMRkdyAzq5ueV ihJDpzS+d8u7x//Drpy7uluJdzftP9+ZQ84xfg22b3g/JQF5bqtvYB2Yvxl8D2Q2D4Q== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794916430; h=X-Original-To:Received:DMARC-Filter:Received:X-ICL-RepId: Dkim-Signature:mail-alias-created-date:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List: List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe: List-Owner:Precedence:MIME-Version:Content-Transfer-Encoding: X-Authority-Info-Out:X-Proofpoint-GUID:X-Proofpoint-Spam-Details-Enc: X-Proofpoint-ORIG-GUID:X-JNJ; bh=/FnNG8vqcCiU2cyM1KkwsgWOUElHYE9t3oC7rJ9yncA=; b=3uomXE2J2RaxMDnQLIVh5tHnU0NRlec3RrrfDuytkwlugvwHsF5sYIq8rh5hCg+2ELqm EKR3p/GBzIz6lrXi6hPQscflMXKcV6+ggvbpPeewggpyWx2ubGIckQ2TynrdktOWzxSgN mHs8UiPlzzGL0Nwiqr0+1M+ptnetFXbT1HzoGRYyHBPlC0xzWT3Wznu7y1AFpyGylcizL i3M57g4V4Dc9yME02QZu90mS9kOYN2wRedzs27/BA/ehBSv04yY9B54LEE7SVxB4cDPVf /AiuF2356jgxYpVaajVijwHvwSNEk/aCy/nIF10/mUuyqT1EFku6acKnKYSP3S/pdlA== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=verdict.gg header.i=@verdict.gg header.a=rsa-sha256 header.s=sig1 header.b=e4kZUtng; dmarc=none header.from=verdict.gg; spf=pass smtp.mailfrom=verdict.gg; 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 B7898606A33A; Tue, 17 Nov 2026 11:53:50 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=verdict.gg header.i=@verdict.gg header.a=rsa-sha256 header.s=sig1 header.b=e4kZUtng X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=57.103.64.196 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794916429; b=tUH9lsaEuDBK7hy1sZReeNeJEiDmBGeWsuK0ctgeqe0tGM/kNSXMolws54LDIY19MJQq M8KM+rGT3gfe4T0w+RZZAUx+AFPcDCelO5RiO7zmiOTP1HPUPoDKDOIxpn9/q0PxNOC62 nX6+kqFNaiCVl6HGZfcvm1wiTCpdkA/Q0PsGfMBkaJY+V4tH+ZdAY3XtoASVS3cbaLUyU rhV04ufa7I8Ihc7oe54cpwhaKG3+aWJqN+efLhakSKnts/lunwooYj/2Xjl3xQkWscws6 dfMjlGH8yhwc5AOZB18gXfYjeYr2eJi9Br08Dj3IoDjpEp7mxP0dy3Nf43yzFFL9XAw== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794916429; h=DMARC-Filter:Received:X-ICL-RepId:Dkim-Signature: mail-alias-created-date:Received:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Authority-Info-Out:X-Proofpoint-GUID: X-Proofpoint-Spam-Details-Enc:X-Proofpoint-ORIG-GUID:X-JNJ; bh=/FnNG8vqcCiU2cyM1KkwsgWOUElHYE9t3oC7rJ9yncA=; b=R0le0rQs+eVaNsXYWhBAZykUfy+cCl0SY65kbxpOWKlTVQ12osh5OxqVrcmmf7owOrAA EW8htoKtgcMokvVq3Z2WrLvCKUdpein8UqOl5lYK6O4SsYR+7BbOQ9ZliaESk3CQ4NFZN C+sVBvFo0uRhaydWhQgeiVzODDA+yfRjcOuLPjYZf6RAlRsafb+0pRuPxItkwHhxSmMPE uJbf93psZ71JK+/js9BdoqtBUN538qI91smLwoTbL2f7PLP/g/PqfbzW5te8AlRw7+5Sb FupfYzCrEi6f1fsxHjD109wAeR+5+h+bUtOzsfuIL/bb5QtvtQhc80N3zrxH26j1LCA== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=verdict.gg; spf=pass smtp.mailfrom=verdict.gg; dkim=pass header.d=verdict.gg header.i=@verdict.gg header.a=rsa-sha256 header.s=sig1 header.b=e4kZUtng; arc=none smtp.remote-ip=57.103.64.196 X-Received: from outbound.pv.icloud.com (pv-2002g-snip4-5.eps.apple.com [57.103.64.196]) (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.subspace.kernel.org (Postfix) with ESMTPS id 86F17606A88B for <netdev@vger.kernel.org>; Tue, 17 Nov 2026 11:53:49 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 86F17606A88B X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=verdict.gg X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=verdict.gg X-Received: from outbound.pv.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-west-1a-20-percent-1 (Postfix) with ESMTPS id EFED318000BC; Tue, 17 Nov 2026 11:53:46 +0000 (UTC) X-ICL-RepId: 01a1e95b-2497-7cf0-9c77-3bf765588f1f X-Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=verdict.gg; s=sig1; t=1794916429; x=1826452429; bh=/FnNG8vqcCiU2cyM1KkwsgWOUElHYE9t3oC7rJ9yncA=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=e4kZUtngllAAcMXLhDcoNaGeEf9XdNSmFy4g3oZgrMykjhU+TddLjximwVCVzGcI3WGJXe5Mgz3Fq+sbqJCiXKq+F7U7PAamBQRQlW2hOHOvnmEEy07WNrnj2f2Shxq9y7Xjo/Y7+bgJ54aeZJ0E6MD1PlYZTqc2/B0YEE+0MT8jeMT1VfNvrqsvQLJT8Q7cD84FOHTokYF9Kkjt323jtKgNzLQBerWoTYbZBxAmTJm79N0oPNjrL7xPCiQpXPY9Uoo9IDonDaSzgQiX7GNJt4fE/mpGg2hFs7dp/S0GOHrQN6wPGAksSepE+DJbdycUXnPl7xn0Numz7+h6i6nU1w== X-mail-alias-created-date: 1621344842221 X-Received: from DelNac.mail.msk (unknown [17.156.192.29]) by p00-icloudmta-asmtp-us-west-1a-20-percent-1 (Postfix) with ESMTPSA id 5A85018001A9; Tue, 17 Nov 2026 11:53:42 +0000 (UTC) X-Cc: Kees Cook <kees@kernel.org>, "Gustavo A. R. Silva" <gustavoars@kernel.org>, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, dev@openvswitch.org, linux-hardening@vger.kernel.org, Fernando Fernandez Mancera <fmancera@suse.de>, Vladimir Vdovin <deliran@verdict.gg> X-Mailer: git-send-email 2.56.0 X-In-Reply-To: <20261117115308.95134-1-deliran@verdict.gg> 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-Authority-Info-Out: v=2.4 cv=f+5FxeyM c=1 sm=1 tr=0 ts=6afc404c cx=c_apl:c_pps:t_out a=aW9mcIavGNWWFvFFKOxBSA==:117 a=aW9mcIavGNWWFvFFKOxBSA==:17 a=zJRp6_cAMc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