Skip to content

Commit 8073e96

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nat: use keyed locks
no need to serialize on a single lock, we can partition the table and add/delete in parallel to different slots. This restores one of the advantages that got lost with the rhlist revert. Cc: Ivan Babrou <[email protected]> Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent e1bf168 commit 8073e96

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

net/netfilter/nf_nat_core.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <net/netfilter/nf_conntrack_zones.h>
3131
#include <linux/netfilter/nf_nat.h>
3232

33-
static DEFINE_SPINLOCK(nf_nat_lock);
33+
static spinlock_t nf_nat_locks[CONNTRACK_LOCKS];
3434

3535
static DEFINE_MUTEX(nf_nat_proto_mutex);
3636
static const struct nf_nat_l3proto __rcu *nf_nat_l3protos[NFPROTO_NUMPROTO]
@@ -425,13 +425,15 @@ nf_nat_setup_info(struct nf_conn *ct,
425425

426426
if (maniptype == NF_NAT_MANIP_SRC) {
427427
unsigned int srchash;
428+
spinlock_t *lock;
428429

429430
srchash = hash_by_src(net,
430431
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
431-
spin_lock_bh(&nf_nat_lock);
432+
lock = &nf_nat_locks[srchash % ARRAY_SIZE(nf_nat_locks)];
433+
spin_lock_bh(lock);
432434
hlist_add_head_rcu(&ct->nat_bysource,
433435
&nf_nat_bysource[srchash]);
434-
spin_unlock_bh(&nf_nat_lock);
436+
spin_unlock_bh(lock);
435437
}
436438

437439
/* It's done. */
@@ -525,6 +527,16 @@ static int nf_nat_proto_remove(struct nf_conn *i, void *data)
525527
return i->status & IPS_NAT_MASK ? 1 : 0;
526528
}
527529

530+
static void __nf_nat_cleanup_conntrack(struct nf_conn *ct)
531+
{
532+
unsigned int h;
533+
534+
h = hash_by_src(nf_ct_net(ct), &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
535+
spin_lock_bh(&nf_nat_locks[h % ARRAY_SIZE(nf_nat_locks)]);
536+
hlist_del_rcu(&ct->nat_bysource);
537+
spin_unlock_bh(&nf_nat_locks[h % ARRAY_SIZE(nf_nat_locks)]);
538+
}
539+
528540
static int nf_nat_proto_clean(struct nf_conn *ct, void *data)
529541
{
530542
if (nf_nat_proto_remove(ct, data))
@@ -540,9 +552,7 @@ static int nf_nat_proto_clean(struct nf_conn *ct, void *data)
540552
* will delete entry from already-freed table.
541553
*/
542554
clear_bit(IPS_SRC_NAT_DONE_BIT, &ct->status);
543-
spin_lock_bh(&nf_nat_lock);
544-
hlist_del_rcu(&ct->nat_bysource);
545-
spin_unlock_bh(&nf_nat_lock);
555+
__nf_nat_cleanup_conntrack(ct);
546556

547557
/* don't delete conntrack. Although that would make things a lot
548558
* simpler, we'd end up flushing all conntracks on nat rmmod.
@@ -670,11 +680,8 @@ EXPORT_SYMBOL_GPL(nf_nat_l3proto_unregister);
670680
/* No one using conntrack by the time this called. */
671681
static void nf_nat_cleanup_conntrack(struct nf_conn *ct)
672682
{
673-
if (ct->status & IPS_SRC_NAT_DONE) {
674-
spin_lock_bh(&nf_nat_lock);
675-
hlist_del_rcu(&ct->nat_bysource);
676-
spin_unlock_bh(&nf_nat_lock);
677-
}
683+
if (ct->status & IPS_SRC_NAT_DONE)
684+
__nf_nat_cleanup_conntrack(ct);
678685
}
679686

680687
static struct nf_ct_ext_type nat_extend __read_mostly = {
@@ -796,10 +803,12 @@ static struct nf_ct_helper_expectfn follow_master_nat = {
796803

797804
static int __init nf_nat_init(void)
798805
{
799-
int ret;
806+
int ret, i;
800807

801808
/* Leave them the same for the moment. */
802809
nf_nat_htable_size = nf_conntrack_htable_size;
810+
if (nf_nat_htable_size < ARRAY_SIZE(nf_nat_locks))
811+
nf_nat_htable_size = ARRAY_SIZE(nf_nat_locks);
803812

804813
nf_nat_bysource = nf_ct_alloc_hashtable(&nf_nat_htable_size, 0);
805814
if (!nf_nat_bysource)
@@ -812,6 +821,9 @@ static int __init nf_nat_init(void)
812821
return ret;
813822
}
814823

824+
for (i = 0; i < ARRAY_SIZE(nf_nat_locks); i++)
825+
spin_lock_init(&nf_nat_locks[i]);
826+
815827
nf_ct_helper_expectfn_register(&follow_master_nat);
816828

817829
BUG_ON(nfnetlink_parse_nat_setup_hook != NULL);

0 commit comments

Comments
 (0)