[PATCH nf-next v1 1/5] netfilter: nf_conncount: bound the gc walk by examined entries

From: Vladimir Vdovin

Date: Tue Nov 17 2026 - 06:54:05 EST


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@xxxxxxxxxx>
---
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