Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netfilter: conntrack: switch connlabels to atomic_t

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>

authored by

Florian Westphal and committed by
Pablo Neira Ayuso
643d1260 ee6f05dc

+10 -11
+1 -1
include/net/netfilter/nf_conntrack_labels.h
··· 39 39 #ifdef CONFIG_NF_CONNTRACK_LABELS 40 40 struct net *net = nf_ct_net(ct); 41 41 42 - if (net->ct.labels_used == 0) 42 + if (atomic_read(&net->ct.labels_used) == 0) 43 43 return NULL; 44 44 45 45 return nf_ct_ext_add(ct, NF_CT_EXT_LABELS, GFP_ATOMIC);
+1 -1
include/net/netns/conntrack.h
··· 107 107 struct nf_ct_event_notifier __rcu *nf_conntrack_event_cb; 108 108 struct nf_ip_net nf_ct_proto; 109 109 #if defined(CONFIG_NF_CONNTRACK_LABELS) 110 - unsigned int labels_used; 110 + atomic_t labels_used; 111 111 #endif 112 112 }; 113 113 #endif
+8 -9
net/netfilter/nf_conntrack_labels.c
··· 11 11 #include <net/netfilter/nf_conntrack_ecache.h> 12 12 #include <net/netfilter/nf_conntrack_labels.h> 13 13 14 - static DEFINE_SPINLOCK(nf_connlabels_lock); 15 - 16 14 static int replace_u32(u32 *address, u32 mask, u32 new) 17 15 { 18 16 u32 old, tmp; ··· 58 60 59 61 int nf_connlabels_get(struct net *net, unsigned int bits) 60 62 { 63 + int v; 64 + 61 65 if (BIT_WORD(bits) >= NF_CT_LABELS_MAX_SIZE / sizeof(long)) 62 66 return -ERANGE; 63 67 64 - spin_lock(&nf_connlabels_lock); 65 - net->ct.labels_used++; 66 - spin_unlock(&nf_connlabels_lock); 67 - 68 68 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE / sizeof(long) >= U8_MAX); 69 + 70 + v = atomic_inc_return_relaxed(&net->ct.labels_used); 71 + WARN_ON_ONCE(v <= 0); 69 72 70 73 return 0; 71 74 } ··· 74 75 75 76 void nf_connlabels_put(struct net *net) 76 77 { 77 - spin_lock(&nf_connlabels_lock); 78 - net->ct.labels_used--; 79 - spin_unlock(&nf_connlabels_lock); 78 + int v = atomic_dec_return_relaxed(&net->ct.labels_used); 79 + 80 + WARN_ON_ONCE(v < 0); 80 81 } 81 82 EXPORT_SYMBOL_GPL(nf_connlabels_put);