netfilter: conntrack: switch connlabels to atomic_t
authorFlorian Westphal <fw@strlen.de>
Fri, 20 Oct 2023 12:38:15 +0000 (14:38 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 24 Oct 2023 11:16:30 +0000 (13:16 +0200)
The spinlock is back from the day when connabels did not have
a fixed size and reallocation had to be supported.

Remove it.  This change also allows to call the helpers from
softirq or timers without deadlocks.

Also add WARN()s to catch refcounting imbalances.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/net/netfilter/nf_conntrack_labels.h
include/net/netns/conntrack.h
net/netfilter/nf_conntrack_labels.c

index fcb19a4..6903f72 100644 (file)
@@ -39,7 +39,7 @@ static inline struct nf_conn_labels *nf_ct_labels_ext_add(struct nf_conn *ct)
 #ifdef CONFIG_NF_CONNTRACK_LABELS
        struct net *net = nf_ct_net(ct);
 
-       if (net->ct.labels_used == 0)
+       if (atomic_read(&net->ct.labels_used) == 0)
                return NULL;
 
        return nf_ct_ext_add(ct, NF_CT_EXT_LABELS, GFP_ATOMIC);
index 1f463b3..bae9148 100644 (file)
@@ -107,7 +107,7 @@ struct netns_ct {
        struct nf_ct_event_notifier __rcu *nf_conntrack_event_cb;
        struct nf_ip_net        nf_ct_proto;
 #if defined(CONFIG_NF_CONNTRACK_LABELS)
-       unsigned int            labels_used;
+       atomic_t                labels_used;
 #endif
 };
 #endif
index 6e70e13..6c46aad 100644 (file)
@@ -11,8 +11,6 @@
 #include <net/netfilter/nf_conntrack_ecache.h>
 #include <net/netfilter/nf_conntrack_labels.h>
 
-static DEFINE_SPINLOCK(nf_connlabels_lock);
-
 static int replace_u32(u32 *address, u32 mask, u32 new)
 {
        u32 old, tmp;
@@ -60,23 +58,24 @@ EXPORT_SYMBOL_GPL(nf_connlabels_replace);
 
 int nf_connlabels_get(struct net *net, unsigned int bits)
 {
+       int v;
+
        if (BIT_WORD(bits) >= NF_CT_LABELS_MAX_SIZE / sizeof(long))
                return -ERANGE;
 
-       spin_lock(&nf_connlabels_lock);
-       net->ct.labels_used++;
-       spin_unlock(&nf_connlabels_lock);
-
        BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE / sizeof(long) >= U8_MAX);
 
+       v = atomic_inc_return_relaxed(&net->ct.labels_used);
+       WARN_ON_ONCE(v <= 0);
+
        return 0;
 }
 EXPORT_SYMBOL_GPL(nf_connlabels_get);
 
 void nf_connlabels_put(struct net *net)
 {
-       spin_lock(&nf_connlabels_lock);
-       net->ct.labels_used--;
-       spin_unlock(&nf_connlabels_lock);
+       int v = atomic_dec_return_relaxed(&net->ct.labels_used);
+
+       WARN_ON_ONCE(v < 0);
 }
 EXPORT_SYMBOL_GPL(nf_connlabels_put);