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

netfilter: nf_conncount: reduce unnecessary GC

Currently nf_conncount can trigger garbage collection (GC)
at multiple places. Each GC process takes a spin_lock_bh
to traverse the nf_conncount_list. We found that when testing
port scanning use two parallel nmap, because the number of
connection increase fast, the nf_conncount_count and its
subsequent call to __nf_conncount_add take too much time,
causing several CPU lockup. This happens when user set the
conntrack limit to +20,000, because the larger the limit,
the longer the list that GC has to traverse.

The patch mitigate the performance issue by avoiding unnecessary
GC with a timestamp. Whenever nf_conncount has done a GC,
a timestamp is updated, and beforce the next time GC is
triggered, we make sure it's more than a jiffies.
By doin this we can greatly reduce the CPU cycles and
avoid the softirq lockup.

To reproduce it in OVS,
$ ovs-appctl dpctl/ct-set-limits zone=1,limit=20000
$ ovs-appctl dpctl/ct-get-limits

At another machine, runs two nmap
$ nmap -p1- <IP>
$ nmap -p1- <IP>

Signed-off-by: William Tu <u9012063@gmail.com>
Co-authored-by: Yifeng Sun <pkusunyifeng@gmail.com>
Reported-by: Greg Rose <gvrose8192@gmail.com>
Suggested-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

authored by

William Tu
Yifeng Sun
and committed by
Pablo Neira Ayuso
d2659299 2c50fc04

+12
+1
include/net/netfilter/nf_conntrack_count.h
··· 10 10 11 11 struct nf_conncount_list { 12 12 spinlock_t list_lock; 13 + u32 last_gc; /* jiffies at most recent gc */ 13 14 struct list_head head; /* connections with the same filtering key */ 14 15 unsigned int count; /* length of list */ 15 16 };
+11
net/netfilter/nf_conncount.c
··· 132 132 struct nf_conn *found_ct; 133 133 unsigned int collect = 0; 134 134 135 + if (time_is_after_eq_jiffies((unsigned long)list->last_gc)) 136 + goto add_new_node; 137 + 135 138 /* check the saved connections */ 136 139 list_for_each_entry_safe(conn, conn_n, &list->head, node) { 137 140 if (collect > CONNCOUNT_GC_MAX_NODES) ··· 180 177 nf_ct_put(found_ct); 181 178 } 182 179 180 + add_new_node: 183 181 if (WARN_ON_ONCE(list->count > INT_MAX)) 184 182 return -EOVERFLOW; 185 183 ··· 194 190 conn->jiffies32 = (u32)jiffies; 195 191 list_add_tail(&conn->node, &list->head); 196 192 list->count++; 193 + list->last_gc = (u32)jiffies; 197 194 return 0; 198 195 } 199 196 ··· 219 214 spin_lock_init(&list->list_lock); 220 215 INIT_LIST_HEAD(&list->head); 221 216 list->count = 0; 217 + list->last_gc = (u32)jiffies; 222 218 } 223 219 EXPORT_SYMBOL_GPL(nf_conncount_list_init); 224 220 ··· 232 226 struct nf_conn *found_ct; 233 227 unsigned int collected = 0; 234 228 bool ret = false; 229 + 230 + /* don't bother if we just did GC */ 231 + if (time_is_after_eq_jiffies((unsigned long)READ_ONCE(list->last_gc))) 232 + return false; 235 233 236 234 /* don't bother if other cpu is already doing GC */ 237 235 if (!spin_trylock(&list->list_lock)) ··· 268 258 269 259 if (!list->count) 270 260 ret = true; 261 + list->last_gc = (u32)jiffies; 271 262 spin_unlock(&list->list_lock); 272 263 273 264 return ret;