Skip to contentSkip to conversation

netfilter: nf_conncount: update last_gc only when GC has been performed

PATCH In review Fernando Fernandez Mancera 2 participants 4 messages Dec 17, 2025 – Dec 19, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 netfilter: nf_conncount: update last_gc only when GC has been performed

Currently last_gc is being updated everytime a new connection is tracked, that means that it is updated even if a GC wasn't performed. With a sufficiently high packet rate, it is possible to always bypass the GC, causing the list to grow infinitely.

Update the last_gc value only when a GC has been actually performed.

Fixes d265929930e2 ("netfilter: nf_conncount: reduce unnecessary GC")

---
v2: introduced this patch to the series

---
 net/netfilter/nf_conncount.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_conncount.c +1 -1
index 3654f1e8976c..8487808c8761 100644
@@ -229,6 +229,7 @@ static int __nf_conncount_add(struct net *net,
 
 		nf_ct_put(found_ct);
 	}
+	list->last_gc = (u32)jiffies;
 
 add_new_node:
 	if (WARN_ON_ONCE(list->count > INT_MAX)) {
@@ -248,7 +249,6 @@ static int __nf_conncount_add(struct net *net,
 	conn->jiffies32 = (u32)jiffies;
 	list_add_tail(&conn->node, &list->head);
 	list->count++;
-	list->last_gc = (u32)jiffies;
 
 out_put:
 	if (refcounted)
Signature · 1 line
2.51.1
#1 lore raw

PATCH v2 2/2 netfilter: nf_conncount: increase the connection clean up limit to 64

After the optimization to only perform one GC per jiffy, a new problem was introduced. If more than 8 new connections are tracked per jiffy the list won't be cleaned up fast enough possibly reaching the limit wrongly.

In order to prevent this issue, only skip the GC if it was already triggered during the same jiffy and the increment is lower than the clean up limit. In addition, increase the clean up limit to 64 connections to avoid triggering GC too often and do more effective GCs.

This has been tested using a HTTP server and several performance tools while having nft_connlimit/xt_connlimit or OVS limit configured.

Output of slowhttptest + OVS limit at 52000 connections:

slow HTTP test status on 340th second:

 initializing:        0
 pending:             432
 connected:           51998
 error:               0
 closed:              0
 service available:   YES
Fixes d265929930e2 ("netfilter: nf_conncount: reduce unnecessary GC")

---
v2: adjusted the code so we always run GC if we increment the list more than the clean up limit

---
 include/net/netfilter/nf_conntrack_count.h |  1 +
 net/netfilter/nf_conncount.c               | 15 ++++++++++-----
 2 files changed, 11 insertions(+), 5 deletions(-)
include/net/netfilter/nf_conntrack_count.h +1 -0
index 52a06de41aa0..cf0166520cf3 100644
@@ -13,6 +13,7 @@ struct nf_conncount_list {
 	u32 last_gc;		/* jiffies at most recent gc */
 	struct list_head head;	/* connections with the same filtering key */
 	unsigned int count;	/* length of list */
+	unsigned int last_gc_count; /* length of list at most recent gc */
 };
 
 struct nf_conncount_data *nf_conncount_init(struct net *net, unsigned int keylen);
net/netfilter/nf_conncount.c +10 -5
index 8487808c8761..288936f5c1bf 100644
@@ -34,8 +34,9 @@
 
 #define CONNCOUNT_SLOTS		256U
 
-#define CONNCOUNT_GC_MAX_NODES	8
-#define MAX_KEYLEN		5
+#define CONNCOUNT_GC_MAX_NODES		8
+#define CONNCOUNT_GC_MAX_COLLECT	64
+#define MAX_KEYLEN			5
 
 /* we will save the tuples of all connections we care about */
 struct nf_conncount_tuple {
@@ -182,12 +183,13 @@ static int __nf_conncount_add(struct net *net,
 		goto out_put;
 	}
 
-	if ((u32)jiffies == list->last_gc)
+	if ((u32)jiffies == list->last_gc &&
+	    (list->count - list->last_gc_count) < CONNCOUNT_GC_MAX_COLLECT)
 		goto add_new_node;
 
 	/* check the saved connections */
 	list_for_each_entry_safe(conn, conn_n, &list->head, node) {
-		if (collect > CONNCOUNT_GC_MAX_NODES)
+		if (collect > CONNCOUNT_GC_MAX_COLLECT)
 			break;
 
 		found = find_or_evict(net, list, conn);
@@ -230,6 +232,7 @@ static int __nf_conncount_add(struct net *net,
 		nf_ct_put(found_ct);
 	}
 	list->last_gc = (u32)jiffies;
+	list->last_gc_count = list->count;
 
 add_new_node:
 	if (WARN_ON_ONCE(list->count > INT_MAX)) {
@@ -277,6 +280,7 @@ void nf_conncount_list_init(struct nf_conncount_list *list)
 	spin_lock_init(&list->list_lock);
 	INIT_LIST_HEAD(&list->head);
 	list->count = 0;
+	list->last_gc_count = 0;
 	list->last_gc = (u32)jiffies;
 }
 EXPORT_SYMBOL_GPL(nf_conncount_list_init);
@@ -316,13 +320,14 @@ static bool __nf_conncount_gc_list(struct net *net,
 		}
 
 		nf_ct_put(found_ct);
-		if (collected > CONNCOUNT_GC_MAX_NODES)
+		if (collected > CONNCOUNT_GC_MAX_COLLECT)
 			break;
 	}
 
 	if (!list->count)
 		ret = true;
 	list->last_gc = (u32)jiffies;
+	list->last_gc_count = list->count;
 
 	return ret;
 }
Signature · 1 line
2.51.1
#2 lore raw

Re: [PATCH 2/2 nf v2] netfilter: nf_conncount: increase the connection clean up limit to 64

Hi! thanks for fix one more time) I will test it today

Quoted 112 lines · Fernando Fernandez Mancera

After the optimization to only perform one GC per jiffy, a new problem was introduced. If more than 8 new connections are tracked per jiffy the list won't be cleaned up fast enough possibly reaching the limit wrongly.

In order to prevent this issue, only skip the GC if it was already triggered during the same jiffy and the increment is lower than the clean up limit. In addition, increase the clean up limit to 64 connections to avoid triggering GC too often and do more effective GCs.

This has been tested using a HTTP server and several performance tools while having nft_connlimit/xt_connlimit or OVS limit configured.

Output of slowhttptest + OVS limit at 52000 connections:

slow HTTP test status on 340th second:

  initializing:        0
  pending:             432
  connected:           51998
  error:               0
  closed:              0
  service available:   YES
Fixes d265929930e2 ("netfilter: nf_conncount: reduce unnecessary GC")

---
v2: adjusted the code so we always run GC if we increment the list more than the clean up limit

---
  include/net/netfilter/nf_conntrack_count.h |  1 +
  net/netfilter/nf_conncount.c               | 15 ++++++++++-----
  2 files changed, 11 insertions(+), 5 deletions(-)
include/net/netfilter/nf_conntrack_count.h +1 -0
index 52a06de41aa0..cf0166520cf3 100644
@@ -13,6 +13,7 @@ struct nf_conncount_list {
  	u32 last_gc;		/* jiffies at most recent gc */
  	struct list_head head;	/* connections with the same filtering key */
  	unsigned int count;	/* length of list */
+	unsigned int last_gc_count; /* length of list at most recent gc */
  };
 
  struct nf_conncount_data *nf_conncount_init(struct net *net, unsigned int keylen);
net/netfilter/nf_conncount.c +10 -5
index 8487808c8761..288936f5c1bf 100644
@@ -34,8 +34,9 @@
 
  #define CONNCOUNT_SLOTS		256U
 
-#define CONNCOUNT_GC_MAX_NODES	8
-#define MAX_KEYLEN		5
+#define CONNCOUNT_GC_MAX_NODES		8
+#define CONNCOUNT_GC_MAX_COLLECT	64
+#define MAX_KEYLEN			5
 
  /* we will save the tuples of all connections we care about */
  struct nf_conncount_tuple {
@@ -182,12 +183,13 @@ static int __nf_conncount_add(struct net *net,
  		goto out_put;
  	}
 
-	if ((u32)jiffies == list->last_gc)
+	if ((u32)jiffies == list->last_gc &&
+	    (list->count - list->last_gc_count) < CONNCOUNT_GC_MAX_COLLECT)
  		goto add_new_node;
 
  	/* check the saved connections */
  	list_for_each_entry_safe(conn, conn_n, &list->head, node) {
-		if (collect > CONNCOUNT_GC_MAX_NODES)
+		if (collect > CONNCOUNT_GC_MAX_COLLECT)
  			break;
 
  		found = find_or_evict(net, list, conn);
@@ -230,6 +232,7 @@ static int __nf_conncount_add(struct net *net,
  		nf_ct_put(found_ct);
  	}
  	list->last_gc = (u32)jiffies;
+	list->last_gc_count = list->count;
 
  add_new_node:
  	if (WARN_ON_ONCE(list->count > INT_MAX)) {
@@ -277,6 +280,7 @@ void nf_conncount_list_init(struct nf_conncount_list *list)
  	spin_lock_init(&list->list_lock);
  	INIT_LIST_HEAD(&list->head);
  	list->count = 0;
+	list->last_gc_count = 0;
  	list->last_gc = (u32)jiffies;
  }
  EXPORT_SYMBOL_GPL(nf_conncount_list_init);
@@ -316,13 +320,14 @@ static bool __nf_conncount_gc_list(struct net *net,
  		}
 
  		nf_ct_put(found_ct);
-		if (collected > CONNCOUNT_GC_MAX_NODES)
+		if (collected > CONNCOUNT_GC_MAX_COLLECT)
  			break;
  	}
 
  	if (!list->count)
  		ret = true;
  	list->last_gc = (u32)jiffies;
+	list->last_gc_count = list->count;
 
  	return ret;
  }
Signature · 2 lines
regards,
Alexandra.
#3 lore raw

Re: [PATCH 2/2 nf v2] netfilter: nf_conncount: increase the connection clean up limit to 64

Hi!
/Tested-by/: Alexandra Rukomoinikova <[email protected]>

Quoted 116 lines · Rukomoinikova Aleksandra

Hi! thanks for fix one more time) I will test it today

Quoted 112 lines · Fernando Fernandez Mancera

After the optimization to only perform one GC per jiffy, a new problem was introduced. If more than 8 new connections are tracked per jiffy the list won't be cleaned up fast enough possibly reaching the limit wrongly.

In order to prevent this issue, only skip the GC if it was already triggered during the same jiffy and the increment is lower than the clean up limit. In addition, increase the clean up limit to 64 connections to avoid triggering GC too often and do more effective GCs.

This has been tested using a HTTP server and several performance tools while having nft_connlimit/xt_connlimit or OVS limit configured.

Output of slowhttptest + OVS limit at 52000 connections:

slow HTTP test status on 340th second:

   initializing:        0
   pending:             432
   connected:           51998
   error:               0
   closed:              0
   service available:   YES
Fixes d265929930e2 ("netfilter: nf_conncount: reduce unnecessary GC")

---
v2: adjusted the code so we always run GC if we increment the list more than the clean up limit

---
   include/net/netfilter/nf_conntrack_count.h |  1 +
   net/netfilter/nf_conncount.c               | 15 ++++++++++-----
   2 files changed, 11 insertions(+), 5 deletions(-)
include/net/netfilter/nf_conntrack_count.h +1 -0
index 52a06de41aa0..cf0166520cf3 100644
@@ -13,6 +13,7 @@ struct nf_conncount_list {
   	u32 last_gc;		/* jiffies at most recent gc */
   	struct list_head head;	/* connections with the same filtering key */
   	unsigned int count;	/* length of list */
+	unsigned int last_gc_count; /* length of list at most recent gc */
   };
 
   struct nf_conncount_data *nf_conncount_init(struct net *net, unsigned int keylen);
net/netfilter/nf_conncount.c +10 -5
index 8487808c8761..288936f5c1bf 100644
@@ -34,8 +34,9 @@
 
   #define CONNCOUNT_SLOTS		256U
 
-#define CONNCOUNT_GC_MAX_NODES	8
-#define MAX_KEYLEN		5
+#define CONNCOUNT_GC_MAX_NODES		8
+#define CONNCOUNT_GC_MAX_COLLECT	64
+#define MAX_KEYLEN			5
 
   /* we will save the tuples of all connections we care about */
   struct nf_conncount_tuple {
@@ -182,12 +183,13 @@ static int __nf_conncount_add(struct net *net,
   		goto out_put;
   	}
 
-	if ((u32)jiffies == list->last_gc)
+	if ((u32)jiffies == list->last_gc &&
+	    (list->count - list->last_gc_count) < CONNCOUNT_GC_MAX_COLLECT)
   		goto add_new_node;
 
   	/* check the saved connections */
   	list_for_each_entry_safe(conn, conn_n, &list->head, node) {
-		if (collect > CONNCOUNT_GC_MAX_NODES)
+		if (collect > CONNCOUNT_GC_MAX_COLLECT)
   			break;
 
   		found = find_or_evict(net, list, conn);
@@ -230,6 +232,7 @@ static int __nf_conncount_add(struct net *net,
   		nf_ct_put(found_ct);
   	}
   	list->last_gc = (u32)jiffies;
+	list->last_gc_count = list->count;
 
   add_new_node:
   	if (WARN_ON_ONCE(list->count > INT_MAX)) {
@@ -277,6 +280,7 @@ void nf_conncount_list_init(struct nf_conncount_list *list)
   	spin_lock_init(&list->list_lock);
   	INIT_LIST_HEAD(&list->head);
   	list->count = 0;
+	list->last_gc_count = 0;
   	list->last_gc = (u32)jiffies;
   }
   EXPORT_SYMBOL_GPL(nf_conncount_list_init);
@@ -316,13 +320,14 @@ static bool __nf_conncount_gc_list(struct net *net,
   		}
 
   		nf_ct_put(found_ct);
-		if (collected > CONNCOUNT_GC_MAX_NODES)
+		if (collected > CONNCOUNT_GC_MAX_COLLECT)
   			break;
   	}
 
   	if (!list->count)
   		ret = true;
   	list->last_gc = (u32)jiffies;
+	list->last_gc_count = list->count;
 
   	return ret;
   }
Signature · 2 lines
regards,
Alexandra.