From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: davem@davemloft.net, netdev@vger.kernel.org, kuba@kernel.org,
pabeni@redhat.com, edumazet@google.com, fw@strlen.de,
horms@kernel.org
Subject: [PATCH net-next 05/11] netfilter: nf_conncount: split count_tree_node rbtree walk into helper
Date: Sun, 14 Jun 2026 13:45:59 +0200 [thread overview]
Message-ID: <20260614114605.474783-6-pablo@netfilter.org> (raw)
In-Reply-To: <20260614114605.474783-1-pablo@netfilter.org>
From: Florian Westphal <fw@strlen.de>
Add find_tree_node() helper that fetches a matching rbtree node.
This is used by followup patch to optionally search the tree again while
preventing concurrent updates via tree lock.
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
net/netfilter/nf_conncount.c | 102 +++++++++++++++++++++--------------
1 file changed, 62 insertions(+), 40 deletions(-)
diff --git a/net/netfilter/nf_conncount.c b/net/netfilter/nf_conncount.c
index faecc05d34d4..56ac64ecfb75 100644
--- a/net/netfilter/nf_conncount.c
+++ b/net/netfilter/nf_conncount.c
@@ -488,6 +488,34 @@ insert_tree(struct net *net,
return count;
}
+static struct nf_conncount_rb *
+find_tree_node(struct nf_conncount_root *root, struct nf_conncount_data *data,
+ const u32 *key)
+{
+ struct rb_node *parent;
+
+ parent = rcu_dereference_check(root->root.rb_node,
+ lockdep_is_held(&root->lock));
+ while (parent) {
+ struct nf_conncount_rb *rbconn;
+ int diff;
+
+ rbconn = rb_entry(parent, struct nf_conncount_rb, node);
+
+ diff = key_diff(key, rbconn->key, data->keylen);
+ if (diff < 0)
+ parent = rcu_dereference_check(parent->rb_left,
+ lockdep_is_held(&root->lock));
+ else if (diff > 0)
+ parent = rcu_dereference_check(parent->rb_right,
+ lockdep_is_held(&root->lock));
+ else
+ return rbconn;
+ }
+
+ return ERR_PTR(-ENOENT);
+}
+
static unsigned int
count_tree(struct net *net,
const struct sk_buff *skb,
@@ -496,59 +524,53 @@ count_tree(struct net *net,
const u32 *key)
{
struct nf_conncount_root *root;
- struct rb_node *parent;
struct nf_conncount_rb *rbconn;
unsigned int hash;
+ int ret;
hash = jhash2(key, data->keylen, data->initval) % CONNCOUNT_SLOTS;
root = &data->root[hash];
- parent = rcu_dereference(root->root.rb_node);
- while (parent) {
- int diff;
-
- rbconn = rb_entry(parent, struct nf_conncount_rb, node);
+ rbconn = find_tree_node(root, data, key);
+ if (IS_ERR(rbconn)) {
+ if (PTR_ERR(rbconn) == -ENOENT) {
+ if (!skb)
+ return 0;
- diff = key_diff(key, rbconn->key, data->keylen);
- if (diff < 0) {
- parent = rcu_dereference(parent->rb_left);
- } else if (diff > 0) {
- parent = rcu_dereference(parent->rb_right);
- } else {
- int ret;
+ return insert_tree(net, skb, l3num, data, hash, key);
+ }
+ DEBUG_NET_WARN_ON_ONCE(IS_ERR(rbconn));
+ }
- if (!skb) {
- nf_conncount_gc_list(net, &rbconn->list);
- return rbconn->list.count;
- }
+ DEBUG_NET_WARN_ON_ONCE(IS_ERR_OR_NULL(rbconn));
+ if (IS_ERR_OR_NULL(rbconn))
+ return 0;
- spin_lock_bh(&rbconn->list.list_lock);
- /* Node might be about to be free'd.
- * We need to defer to insert_tree() in this case.
- */
- if (rbconn->list.count == 0) {
- spin_unlock_bh(&rbconn->list.list_lock);
- break;
- }
+ if (!skb) {
+ nf_conncount_gc_list(net, &rbconn->list);
+ return rbconn->list.count;
+ }
- /* same source network -> be counted! */
- ret = __nf_conncount_add(net, skb, l3num, &rbconn->list);
- spin_unlock_bh(&rbconn->list.list_lock);
- if (ret && ret != -EEXIST) {
- return 0; /* hotdrop */
- } else {
- /* -EEXIST means add was skipped, update the list */
- if (ret == -EEXIST)
- nf_conncount_gc_list(net, &rbconn->list);
- return rbconn->list.count;
- }
- }
+ spin_lock_bh(&rbconn->list.list_lock);
+ /* Node might be about to be free'd.
+ * We need to defer to insert_tree() in this case.
+ */
+ if (rbconn->list.count == 0) {
+ spin_unlock_bh(&rbconn->list.list_lock);
+ return insert_tree(net, skb, l3num, data, hash, key);
}
- if (!skb)
- return 0;
+ /* same source network -> be counted! */
+ ret = __nf_conncount_add(net, skb, l3num, &rbconn->list);
+ spin_unlock_bh(&rbconn->list.list_lock);
+
+ if (ret && ret != -EEXIST)
+ return 0; /* hotdrop */
+ /* -EEXIST means add was skipped, update the list */
+ if (ret == -EEXIST)
+ nf_conncount_gc_list(net, &rbconn->list);
- return insert_tree(net, skb, l3num, data, hash, key);
+ return rbconn->list.count;
}
static void tree_gc_worker(struct work_struct *work)
--
2.47.3
next prev parent reply other threads:[~2026-06-14 11:46 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-14 11:45 [PATCH net-next 00/11] Netfilter/IPVS updates for net-next Pablo Neira Ayuso
2026-06-14 11:45 ` [PATCH net-next 01/11] ipvs: Replace use of system_unbound_wq with system_dfl_long_wq Pablo Neira Ayuso
2026-06-14 11:45 ` [PATCH net-next 02/11] netfilter: nf_tables: use DEBUG_NET_WARN_ON_ONCE in packet and control paths Pablo Neira Ayuso
2026-06-14 11:45 ` [PATCH net-next 03/11] netfilter: nf_conncount: callers must hold rcu read lock Pablo Neira Ayuso
2026-06-14 11:45 ` [PATCH net-next 04/11] netfilter: nf_conncount: use per nf_conncount_data spinlocks Pablo Neira Ayuso
2026-06-14 11:45 ` Pablo Neira Ayuso [this message]
2026-06-14 11:46 ` [PATCH net-next 06/11] netfilter: nf_conncount: add sequence counter to detect tree modifications Pablo Neira Ayuso
2026-06-14 11:46 ` [PATCH net-next 07/11] netfilter: nf_conncount: gc and rcu fixes Pablo Neira Ayuso
2026-06-14 11:46 ` [PATCH net-next 08/11] netfilter: conntrack: check NULL when retrieving ct extension Pablo Neira Ayuso
2026-06-14 11:46 ` [PATCH net-next 09/11] netfilter: flowtable: bail out if forward path cannot be discovered Pablo Neira Ayuso
2026-06-14 11:46 ` [PATCH net-next 10/11] ipvs: fix doc syntax for conn_max sysctl Pablo Neira Ayuso
2026-06-14 11:46 ` [PATCH net-next 11/11] netfilter: nf_dup_netdev: add nf_dev_xmit_recursion*() helpers and use them Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260614114605.474783-6-pablo@netfilter.org \
--to=pablo@netfilter.org \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=fw@strlen.de \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=pabeni@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox