Skip to content

Commit 92e47ba

Browse files
Liping Zhangummakynes
authored andcommitted
netfilter: conntrack: simplify the code by using nf_conntrack_get_ht
Since commit 64b8763 ("netfilter: conntrack: fix race between nf_conntrack proc read and hash resize") introduce the nf_conntrack_get_ht, so there's no need to check nf_conntrack_generation again and again to get the hash table and hash size. And convert nf_conntrack_get_ht to inline function here. Suggested-by: Pablo Neira Ayuso <[email protected]> Signed-off-by: Liping Zhang <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent adf0516 commit 92e47ba

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

include/net/netfilter/nf_conntrack.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,29 @@ struct kernel_param;
303303

304304
int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp);
305305
int nf_conntrack_hash_resize(unsigned int hashsize);
306+
307+
extern struct hlist_nulls_head *nf_conntrack_hash;
306308
extern unsigned int nf_conntrack_htable_size;
309+
extern seqcount_t nf_conntrack_generation;
307310
extern unsigned int nf_conntrack_max;
308311

312+
/* must be called with rcu read lock held */
313+
static inline void
314+
nf_conntrack_get_ht(struct hlist_nulls_head **hash, unsigned int *hsize)
315+
{
316+
struct hlist_nulls_head *hptr;
317+
unsigned int sequence, hsz;
318+
319+
do {
320+
sequence = read_seqcount_begin(&nf_conntrack_generation);
321+
hsz = nf_conntrack_htable_size;
322+
hptr = nf_conntrack_hash;
323+
} while (read_seqcount_retry(&nf_conntrack_generation, sequence));
324+
325+
*hash = hptr;
326+
*hsize = hsz;
327+
}
328+
309329
struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
310330
const struct nf_conntrack_zone *zone,
311331
gfp_t flags);

include/net/netfilter/nf_conntrack_core.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ bool nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
5151
const struct nf_conntrack_l3proto *l3proto,
5252
const struct nf_conntrack_l4proto *l4proto);
5353

54-
void nf_conntrack_get_ht(struct hlist_nulls_head **hash, unsigned int *hsize);
55-
5654
/* Find a connection corresponding to a tuple. */
5755
struct nf_conntrack_tuple_hash *
5856
nf_conntrack_find_get(struct net *net,
@@ -83,7 +81,6 @@ print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
8381

8482
#define CONNTRACK_LOCKS 1024
8583

86-
extern struct hlist_nulls_head *nf_conntrack_hash;
8784
extern spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
8885
void nf_conntrack_lock(spinlock_t *lock);
8986

net/netfilter/nf_conntrack_core.c

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ EXPORT_SYMBOL_GPL(nf_conntrack_hash);
7474

7575
static __read_mostly struct kmem_cache *nf_conntrack_cachep;
7676
static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
77-
static __read_mostly seqcount_t nf_conntrack_generation;
7877
static __read_mostly DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
7978
static __read_mostly bool nf_conntrack_locks_all;
8079

@@ -162,6 +161,7 @@ static void nf_conntrack_all_unlock(void)
162161

163162
unsigned int nf_conntrack_htable_size __read_mostly;
164163
unsigned int nf_conntrack_max __read_mostly;
164+
seqcount_t nf_conntrack_generation __read_mostly;
165165

166166
DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
167167
EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
@@ -478,23 +478,6 @@ nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
478478
net_eq(net, nf_ct_net(ct));
479479
}
480480

481-
/* must be called with rcu read lock held */
482-
void nf_conntrack_get_ht(struct hlist_nulls_head **hash, unsigned int *hsize)
483-
{
484-
struct hlist_nulls_head *hptr;
485-
unsigned int sequence, hsz;
486-
487-
do {
488-
sequence = read_seqcount_begin(&nf_conntrack_generation);
489-
hsz = nf_conntrack_htable_size;
490-
hptr = nf_conntrack_hash;
491-
} while (read_seqcount_retry(&nf_conntrack_generation, sequence));
492-
493-
*hash = hptr;
494-
*hsize = hsz;
495-
}
496-
EXPORT_SYMBOL_GPL(nf_conntrack_get_ht);
497-
498481
/*
499482
* Warning :
500483
* - Caller must take a reference on returned object
@@ -507,14 +490,11 @@ ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
507490
struct nf_conntrack_tuple_hash *h;
508491
struct hlist_nulls_head *ct_hash;
509492
struct hlist_nulls_node *n;
510-
unsigned int bucket, sequence;
493+
unsigned int bucket, hsize;
511494

512495
begin:
513-
do {
514-
sequence = read_seqcount_begin(&nf_conntrack_generation);
515-
bucket = scale_hash(hash);
516-
ct_hash = nf_conntrack_hash;
517-
} while (read_seqcount_retry(&nf_conntrack_generation, sequence));
496+
nf_conntrack_get_ht(&ct_hash, &hsize);
497+
bucket = reciprocal_scale(hash, hsize);
518498

519499
hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
520500
if (nf_ct_key_equal(h, tuple, zone, net)) {
@@ -820,18 +800,15 @@ nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
820800
const struct nf_conntrack_zone *zone;
821801
struct nf_conntrack_tuple_hash *h;
822802
struct hlist_nulls_head *ct_hash;
823-
unsigned int hash, sequence;
803+
unsigned int hash, hsize;
824804
struct hlist_nulls_node *n;
825805
struct nf_conn *ct;
826806

827807
zone = nf_ct_zone(ignored_conntrack);
828808

829809
rcu_read_lock();
830-
do {
831-
sequence = read_seqcount_begin(&nf_conntrack_generation);
832-
hash = hash_conntrack(net, tuple);
833-
ct_hash = nf_conntrack_hash;
834-
} while (read_seqcount_retry(&nf_conntrack_generation, sequence));
810+
nf_conntrack_get_ht(&ct_hash, &hsize);
811+
hash = __hash_conntrack(net, tuple, hsize);
835812

836813
hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
837814
ct = nf_ct_tuplehash_to_ctrack(h);
@@ -897,14 +874,11 @@ static noinline int early_drop(struct net *net, unsigned int _hash)
897874

898875
for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
899876
struct hlist_nulls_head *ct_hash;
900-
unsigned hash, sequence, drops;
877+
unsigned int hash, hsize, drops;
901878

902879
rcu_read_lock();
903-
do {
904-
sequence = read_seqcount_begin(&nf_conntrack_generation);
905-
hash = scale_hash(_hash++);
906-
ct_hash = nf_conntrack_hash;
907-
} while (read_seqcount_retry(&nf_conntrack_generation, sequence));
880+
nf_conntrack_get_ht(&ct_hash, &hsize);
881+
hash = reciprocal_scale(_hash++, hsize);
908882

909883
drops = early_drop_list(net, &ct_hash[hash]);
910884
rcu_read_unlock();

0 commit comments

Comments
 (0)