Thread View: linux.kernel.netdev
6 messages
6 total messages
Started by Vladimir Vdovin
Tue, 17 Nov 2026 11:53
[PATCH nf-next v1 0/5] netfilter: nf_conncount: reduce list_lock contention at the limit
Author: Vladimir Vdovin
Date: Tue, 17 Nov 2026 11:53
Date: Tue, 17 Nov 2026 11:53
108 lines
16399 bytes
16399 bytes
Follow-up to [1]: Florian asked for the slot count to come from the
cpu count, Fernando for a way to see the contention. Additional
profiling showed the slot locks were not the main problem, the
per-key list is.
The setup is a 128-cpu hypervisor (64 rx queues) running openvswitch
with per-zone conntrack limits under a 2 Mpps SYN flood against one
VM. OVS keys nf_conncount by zone id, so the whole zone shares one
nf_conncount_list and its list_lock. Once the zone is at its limit
(500k), 96.8% of all cycles are in native_queued_spin_lock_slowpath
behind the spin_lock_bh() in count_tree(), and the host is
unreachable.
Two things add up to that:
* openvswitch has no way to ask whether the zone has room without
recording the connection, so above the limit every packet still
takes list_lock, records an entry that ovs_ct_commit() rejects at
once, and leaves it for gc to evict;
* the gc walk stops after 64 *evicted* entries, but live entries and
-EAGAIN entries do not count, so under flood every walk covers the
whole list: 500k nf_conntrack_find_get() under list_lock with BH
off. And nf_conncount_gc_list() takes the trylock before it checks
whether gc already ran this jiffy, so the count-only lookup is not
lockless either.
Patch 1, nf_conncount: bound the gc walk by examined entries.
A pass stops after 128 examined entries and the list is rotated so
successive passes still cover all of it.
Patch 2, nf_conncount: check last_gc before taking list_lock.
The count-only lookup stays lockless when gc already ran this
jiffy; one cpu per jiffy touches the lock, the rest read the count.
Patch 3, nf_conncount: size the number of slots from the cpu count.
Florian's suggestion from [1].
Patch 4, openvswitch: do not record connections above the zone limit.
Look the count up first with the existing count-only mode of
nf_conncount_count_skb() and record only below the limit.
Patch 5, openvswitch: warn about a zone above its limit without a lock.
The shared state of net_warn_ratelimited() was 2.7% of all cycles.
Each half was also tried alone: patches 1-3 without 4 leave the host
as it was (94% in the slowpath, recording path), patches 4-5 without
1-3 too (97%: the check and the recording are not atomic, every gc
pass that dips the count below the limit lets a burst of cpus record
past it, and the first of them walks the whole list under list_lock).
Numbers, two separate before/after measurements on the same machine.
1. The production case, before and after the whole series:
openvswitch, zone at its limit, 2 Mpps SYN flood.
spinlock slowpath softirq host
unpatched 96% 100% unreachable
patches 1-5 0% 40% responsive
2. The nf_conncount part alone, without openvswitch, before and after
patches 1-3: two netns over a veth pair, iptables "-m connlimit
--connlimit-above 10000 --connlimit-mask 0" in the receiving one
(one list, the same shape as a zone), trafgen in the other with 64
threads sending SYNs from random sources to random ports. veth
delivers on the sending cpu, so that is 64 cpus on one list_lock.
Measured with the list full:
nf_conncount_count_skb() calls/s
unpatched 0.5 - 4 ms 40k
patches 1-3 16 - 64 us 340k
The lock stays contended here, over 90% of all cycles in the
slowpath both before and after, because connlimit records every
connection, see below. What changes is how long the lock is held
per acquisition, and with it how many packets get through.
The connlimit matches record before they compare, as openvswitch did,
and unlike openvswitch they cannot stop: a match does not know whether
the connection will be dropped, and a softlimit setup needs the
connections above the limit counted. So they keep the convoy, and
patches 1-3 are what helps them.
Tested on the nft side with a ct count test between two netns (limit
enforced, closed and dropped connections reclaimed, list recovers
after being full) under DEBUG_LIST and lockdep.
[1] https://lore.kernel.org/all/20260413123712.42993-1-deliran@verdict.gg/
Vladimir Vdovin (5):
netfilter: nf_conncount: bound the gc walk by examined entries
netfilter: nf_conncount: check last_gc before taking list_lock in
gc_list
netfilter: nf_conncount: size the number of slots from the cpu count
openvswitch: conntrack: do not record connections above the zone limit
openvswitch: conntrack: warn about a zone above its limit without a
lock
net/netfilter/nf_conncount.c | 86 +++++++++++++++++++++++++++++-------
net/openvswitch/conntrack.c | 31 +++++++++++--
net/openvswitch/datapath.h | 1 +
3 files changed, 99 insertions(+), 19 deletions(-)
base-commit: 734bef548a96e8774d6eef5dfa2026c6edc4e80b
--
2.56.0
[PATCH nf-next v1 1/5] netfilter: nf_conncount: bound the gc walk by examined entries
Author: 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
[PATCH nf-next v1 2/5] netfilter: nf_conncount: check last_gc before taking list_lock in gc_list
Author: Vladimir Vdovin
Date: Tue, 17 Nov 2026 11:53
Date: Tue, 17 Nov 2026 11:53
37 lines
12381 bytes
12381 bytes
nf_conncount_gc_list() takes list_lock first and only then finds out,
in __nf_conncount_gc_list(), that gc already ran during this jiffy and
there is nothing to do. The trylock still writes to the lock, so every
caller contends on it even though at most one of them per jiffy does
any work.
This matters on the count-only path of nf_conncount_count_skb(), where
the lookup itself is lockless: when many cpus query the same list at
once, the gc attempt is the only thing they serialize on.
Read last_gc before the trylock and leave early. The check under the
lock stays, as it is the one that decides when two cpus race for the
same jiffy.
Signed-off-by: Vladimir Vdovin <deliran@verdict.gg>
---
net/netfilter/nf_conncount.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/net/netfilter/nf_conncount.c b/net/netfilter/nf_conncount.c
index 84290e363245..be9e395e25b6 100644
--- a/net/netfilter/nf_conncount.c
+++ b/net/netfilter/nf_conncount.c
@@ -382,6 +382,10 @@ bool nf_conncount_gc_list(struct net *net,
{
bool ret;
+ /* don't bother if we just did GC */
+ if ((u32)jiffies == READ_ONCE(list->last_gc))
+ return false;
+
/* don't bother if other cpu is already doing GC */
if (!spin_trylock_bh(&list->list_lock))
return false;
--
2.56.0
[PATCH nf-next v1 3/5] netfilter: nf_conncount: size the number of slots from the cpu count
Author: Vladimir Vdovin
Date: Tue, 17 Nov 2026 11:53
Date: Tue, 17 Nov 2026 11:53
150 lines
16308 bytes
16308 bytes
Every key space is spread over CONNCOUNT_SLOTS rb-trees, each with its
own lock, so that cpus inserting or reaping different keys do not
serialize on one another. The number has been 256 since 2014, when
that was comfortably above the cpu count of anything running this
code. With 128 or more rx cpus two of them needing the same slot lock
at once is no longer rare, and the array also bounds how many trees
the gc worker can walk independently.
Size it at module init from the number of possible cpus, 16 slots per
cpu rounded up to a power of two, between the historical 256 and 8192.
A small system keeps exactly what it had; a 160 cpu one gets 4096
slots. Possible rather than online cpus: the array is allocated once,
and cpus that come online later still need their share of slots. The
array becomes a flexible array member allocated with struct_size(),
and the pending tree bitmap goes with it.
Suggested-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Vladimir Vdovin <deliran@verdict.gg>
---
net/netfilter/nf_conncount.c | 48 ++++++++++++++++++++++++++----------
1 file changed, 35 insertions(+), 13 deletions(-)
diff --git a/net/netfilter/nf_conncount.c b/net/netfilter/nf_conncount.c
index be9e395e25b6..7a4b00c2bc65 100644
--- a/net/netfilter/nf_conncount.c
+++ b/net/netfilter/nf_conncount.c
@@ -32,7 +32,11 @@
#include <net/netfilter/nf_conntrack_tuple.h>
#include <net/netfilter/nf_conntrack_zones.h>
-#define CONNCOUNT_SLOTS 256U
+/* Number of rb-trees (and locks) a key space is spread over. Sized from
+ * the number of possible cpus at module init, see nf_conncount_modinit().
+ */
+#define CONNCOUNT_SLOTS_MIN 256U
+#define CONNCOUNT_SLOTS_MAX 8192U
#define CONNCOUNT_GC_MAX_NODES 8
#define CONNCOUNT_GC_MAX_COLLECT 64
@@ -65,13 +69,15 @@ struct nf_conncount_root {
struct nf_conncount_data {
unsigned int keylen;
u32 initval;
- struct nf_conncount_root root[CONNCOUNT_SLOTS];
struct net *net;
struct work_struct gc_work;
- unsigned long pending_trees[BITS_TO_LONGS(CONNCOUNT_SLOTS)];
+ unsigned long *pending_trees;
unsigned int gc_tree;
+ unsigned int nr_slots;
+ struct nf_conncount_root root[] __counted_by(nr_slots);
};
+static unsigned int conncount_slots __read_mostly;
static struct kmem_cache *conncount_rb_cachep __read_mostly;
static struct kmem_cache *conncount_conn_cachep __read_mostly;
@@ -574,7 +580,7 @@ count_tree(struct net *net,
unsigned int hash;
int ret;
- hash = jhash2(key, data->keylen, data->initval) % CONNCOUNT_SLOTS;
+ hash = jhash2(key, data->keylen, data->initval) & (data->nr_slots - 1);
root = &data->root[hash];
rbconn = find_tree_node(root, data, key);
@@ -634,9 +640,9 @@ static void tree_gc_worker(struct work_struct *work)
struct rb_node *node;
if (data->gc_tree == 0)
- data->gc_tree = find_first_bit(data->pending_trees, CONNCOUNT_SLOTS);
+ data->gc_tree = find_first_bit(data->pending_trees, data->nr_slots);
- tree = data->gc_tree % CONNCOUNT_SLOTS;
+ tree = data->gc_tree & (data->nr_slots - 1);
root = &data->root[tree];
spin_lock_bh(&root->lock);
@@ -679,10 +685,10 @@ static void tree_gc_worker(struct work_struct *work)
tree_nodes_free(root, gc_nodes, gc_count);
clear_bit(tree, data->pending_trees);
- next_tree = (tree + 1) % CONNCOUNT_SLOTS;
- next_tree = find_next_bit(data->pending_trees, CONNCOUNT_SLOTS, next_tree);
+ next_tree = (tree + 1) & (data->nr_slots - 1);
+ next_tree = find_next_bit(data->pending_trees, data->nr_slots, next_tree);
- if (next_tree < CONNCOUNT_SLOTS) {
+ if (next_tree < data->nr_slots) {
data->gc_tree = next_tree;
schedule_work(work);
} else {
@@ -717,18 +723,26 @@ static void nf_conncount_root_init(struct nf_conncount_root *r)
struct nf_conncount_data *nf_conncount_init(struct net *net, unsigned int keylen)
{
struct nf_conncount_data *data;
- int i;
+ unsigned int i;
if (keylen % sizeof(u32) ||
keylen / sizeof(u32) > MAX_KEYLEN ||
keylen == 0)
return ERR_PTR(-EINVAL);
- data = kvzalloc_obj(*data, GFP_KERNEL_ACCOUNT);
+ data = kvzalloc(struct_size(data, root, conncount_slots),
+ GFP_KERNEL_ACCOUNT);
if (!data)
return ERR_PTR(-ENOMEM);
- for (i = 0; i < ARRAY_SIZE(data->root); ++i)
+ data->nr_slots = conncount_slots;
+ data->pending_trees = bitmap_zalloc(data->nr_slots, GFP_KERNEL_ACCOUNT);
+ if (!data->pending_trees) {
+ kvfree(data);
+ return ERR_PTR(-ENOMEM);
+ }
+
+ for (i = 0; i < data->nr_slots; ++i)
nf_conncount_root_init(&data->root[i]);
data->keylen = keylen / sizeof(u32);
@@ -771,15 +785,23 @@ void nf_conncount_destroy(struct net *net, struct nf_conncount_data *data)
disable_work_sync(&data->gc_work);
- for (i = 0; i < ARRAY_SIZE(data->root); ++i)
+ for (i = 0; i < data->nr_slots; ++i)
destroy_tree(&data->root[i]);
+ bitmap_free(data->pending_trees);
kvfree(data);
}
EXPORT_SYMBOL_GPL(nf_conncount_destroy);
static int __init nf_conncount_modinit(void)
{
+ /* Each slot has its own lock, so give the cpus that can be in
+ * insert_tree() or the gc worker at the same time enough of them to
+ * rarely collide, while a small system keeps the historical 256.
+ */
+ conncount_slots = clamp(roundup_pow_of_two(num_possible_cpus() * 16),
+ CONNCOUNT_SLOTS_MIN, CONNCOUNT_SLOTS_MAX);
+
conncount_conn_cachep = KMEM_CACHE(nf_conncount_tuple, 0);
if (!conncount_conn_cachep)
return -ENOMEM;
--
2.56.0
[PATCH nf-next v1 4/5] openvswitch: conntrack: do not record connections above the zone limit
Author: Vladimir Vdovin
Date: Tue, 17 Nov 2026 11:53
Date: Tue, 17 Nov 2026 11:53
43 lines
12597 bytes
12597 bytes
ovs_ct_check_limit() records every new connection in nf_conncount and only then rejects it when the zone is above its limit. Recording means taking the zone's list_lock and walking the list for gc, so above the limit every rx cpu serializes on that lock for every packet, to add an entry that is rejected right after and that gc has to evict later. Look the count up first. nf_conncount_count_skb() without an skb does a lockless rbtree lookup, runs gc on the list if it has not been done during this jiffy, and returns the count without recording anything. When the zone has no room for another connection, reject on that count and never touch list_lock. Below the limit the connection is recorded as before, at the cost of one extra lockless lookup. Signed-off-by: Vladimir Vdovin <deliran@verdict.gg> --- net/openvswitch/conntrack.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/net/openvswitch/conntrack.c b/net/openvswitch/conntrack.c index d3326edcabf7..e78a62cec051 100644 --- a/net/openvswitch/conntrack.c +++ b/net/openvswitch/conntrack.c @@ -955,6 +955,17 @@ static int ovs_ct_check_limit(struct net *net, if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED) return 0; + /* Nothing is recorded while the zone has no room for another + * connection: a lookup without an skb is lockless and only runs gc + * on the list, so the count keeps following the real number of + * connections while new ones are being rejected. + */ + connections = nf_conncount_count_skb(net, NULL, info->family, + ct_limit_info->data, + &conncount_key); + if (connections >= per_zone_limit) + return -ENOMEM; + connections = nf_conncount_count_skb(net, skb, info->family, ct_limit_info->data, &conncount_key); -- 2.56.0
[PATCH nf-next v1 5/5] openvswitch: conntrack: warn about a zone above its limit without a lock
Author: Vladimir Vdovin
Date: Tue, 17 Nov 2026 11:53
Date: Tue, 17 Nov 2026 11:53
76 lines
13593 bytes
13593 bytes
ovs_ct_commit() warns through net_warn_ratelimited() for every new
connection a zone above its limit refuses. The rate limiting itself
costs: every call serializes on the shared ratelimit state, and with
a zone at its limit under flood that is every rx cpu for every packet.
Keep one timestamp per netns instead: a caller that finds it in the
future returns at once, the first one past it moves it a second ahead
and warns. Two cpus racing past it in the same jiffy may both warn,
which is harmless, and nothing is serialized.
Signed-off-by: Vladimir Vdovin <deliran@verdict.gg>
---
net/openvswitch/conntrack.c | 20 +++++++++++++++++---
net/openvswitch/datapath.h | 1 +
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/net/openvswitch/conntrack.c b/net/openvswitch/conntrack.c
index e78a62cec051..ee90af14ac81 100644
--- a/net/openvswitch/conntrack.c
+++ b/net/openvswitch/conntrack.c
@@ -936,6 +936,21 @@ static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
return info->default_limit;
}
+/* At most one warning per second. net_warn_ratelimited() would do, but it
+ * serializes its callers on the ratelimit state, and when a zone is above
+ * its limit every rx cpu is a caller for every packet.
+ */
+static void ovs_ct_limit_warn(struct net *net, u16 zone)
+{
+ struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
+
+ if (time_before(jiffies, READ_ONCE(ovs_net->ct_limit_warn_next)))
+ return;
+
+ WRITE_ONCE(ovs_net->ct_limit_warn_next, jiffies + HZ);
+ pr_warn("openvswitch: zone: %u exceeds conntrack limit\n", zone);
+}
+
static int ovs_ct_check_limit(struct net *net,
const struct sk_buff *skb,
const struct ovs_conntrack_info *info)
@@ -999,9 +1014,7 @@ static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
if (!nf_ct_is_confirmed(ct)) {
err = ovs_ct_check_limit(net, skb, info);
if (err) {
- net_warn_ratelimited("openvswitch: zone: %u "
- "exceeds conntrack limit\n",
- info->zone.id);
+ ovs_ct_limit_warn(net, info->zone.id);
return err;
}
}
@@ -1638,6 +1651,7 @@ static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
pr_err("openvswitch: failed to init nf_conncount %d\n", err);
return err;
}
+ ovs_net->ct_limit_warn_next = jiffies;
rcu_assign_pointer(ovs_net->ct_limit_info, info);
return 0;
}
diff --git a/net/openvswitch/datapath.h b/net/openvswitch/datapath.h
index 7a7afaeceee5..9c6a5b5a8939 100644
--- a/net/openvswitch/datapath.h
+++ b/net/openvswitch/datapath.h
@@ -177,6 +177,7 @@ struct ovs_net {
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
struct ovs_ct_limit_info __rcu *ct_limit_info;
struct ovs_ct_limit_info *ct_limit_exit_data;
+ unsigned long ct_limit_warn_next;
#endif
bool xt_label;
};
--
2.56.0
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads