Skip to content

Commit 3427b2a

Browse files
congwangummakynes
authored andcommitted
netfilter: make xt_rateest hash table per net
As suggested by Eric, we need to make the xt_rateest hash table and its lock per netns to reduce lock contentions. Cc: Florian Westphal <fw@strlen.de> Cc: Eric Dumazet <edumazet@google.com> Cc: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 0d7df90 commit 3427b2a

File tree

3 files changed

+72
-33
lines changed

3 files changed

+72
-33
lines changed

include/net/netfilter/xt_rateest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct xt_rateest {
2121
struct net_rate_estimator __rcu *rate_est;
2222
};
2323

24-
struct xt_rateest *xt_rateest_lookup(const char *name);
25-
void xt_rateest_put(struct xt_rateest *est);
24+
struct xt_rateest *xt_rateest_lookup(struct net *net, const char *name);
25+
void xt_rateest_put(struct net *net, struct xt_rateest *est);
2626

2727
#endif /* _XT_RATEEST_H */

net/netfilter/xt_RATEEST.c

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@
1414
#include <linux/slab.h>
1515
#include <net/gen_stats.h>
1616
#include <net/netlink.h>
17+
#include <net/netns/generic.h>
1718

1819
#include <linux/netfilter/x_tables.h>
1920
#include <linux/netfilter/xt_RATEEST.h>
2021
#include <net/netfilter/xt_rateest.h>
2122

22-
static DEFINE_MUTEX(xt_rateest_mutex);
23-
2423
#define RATEEST_HSIZE 16
25-
static struct hlist_head rateest_hash[RATEEST_HSIZE] __read_mostly;
24+
25+
struct xt_rateest_net {
26+
struct mutex hash_lock;
27+
struct hlist_head hash[RATEEST_HSIZE];
28+
};
29+
30+
static unsigned int xt_rateest_id;
31+
2632
static unsigned int jhash_rnd __read_mostly;
2733

2834
static unsigned int xt_rateest_hash(const char *name)
@@ -31,21 +37,23 @@ static unsigned int xt_rateest_hash(const char *name)
3137
(RATEEST_HSIZE - 1);
3238
}
3339

34-
static void xt_rateest_hash_insert(struct xt_rateest *est)
40+
static void xt_rateest_hash_insert(struct xt_rateest_net *xn,
41+
struct xt_rateest *est)
3542
{
3643
unsigned int h;
3744

3845
h = xt_rateest_hash(est->name);
39-
hlist_add_head(&est->list, &rateest_hash[h]);
46+
hlist_add_head(&est->list, &xn->hash[h]);
4047
}
4148

42-
static struct xt_rateest *__xt_rateest_lookup(const char *name)
49+
static struct xt_rateest *__xt_rateest_lookup(struct xt_rateest_net *xn,
50+
const char *name)
4351
{
4452
struct xt_rateest *est;
4553
unsigned int h;
4654

4755
h = xt_rateest_hash(name);
48-
hlist_for_each_entry(est, &rateest_hash[h], list) {
56+
hlist_for_each_entry(est, &xn->hash[h], list) {
4957
if (strcmp(est->name, name) == 0) {
5058
est->refcnt++;
5159
return est;
@@ -55,20 +63,23 @@ static struct xt_rateest *__xt_rateest_lookup(const char *name)
5563
return NULL;
5664
}
5765

58-
struct xt_rateest *xt_rateest_lookup(const char *name)
66+
struct xt_rateest *xt_rateest_lookup(struct net *net, const char *name)
5967
{
68+
struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
6069
struct xt_rateest *est;
6170

62-
mutex_lock(&xt_rateest_mutex);
63-
est = __xt_rateest_lookup(name);
64-
mutex_unlock(&xt_rateest_mutex);
71+
mutex_lock(&xn->hash_lock);
72+
est = __xt_rateest_lookup(xn, name);
73+
mutex_unlock(&xn->hash_lock);
6574
return est;
6675
}
6776
EXPORT_SYMBOL_GPL(xt_rateest_lookup);
6877

69-
void xt_rateest_put(struct xt_rateest *est)
78+
void xt_rateest_put(struct net *net, struct xt_rateest *est)
7079
{
71-
mutex_lock(&xt_rateest_mutex);
80+
struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
81+
82+
mutex_lock(&xn->hash_lock);
7283
if (--est->refcnt == 0) {
7384
hlist_del(&est->list);
7485
gen_kill_estimator(&est->rate_est);
@@ -78,7 +89,7 @@ void xt_rateest_put(struct xt_rateest *est)
7889
*/
7990
kfree_rcu(est, rcu);
8091
}
81-
mutex_unlock(&xt_rateest_mutex);
92+
mutex_unlock(&xn->hash_lock);
8293
}
8394
EXPORT_SYMBOL_GPL(xt_rateest_put);
8495

@@ -98,6 +109,7 @@ xt_rateest_tg(struct sk_buff *skb, const struct xt_action_param *par)
98109

99110
static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
100111
{
112+
struct xt_rateest_net *xn = net_generic(par->net, xt_rateest_id);
101113
struct xt_rateest_target_info *info = par->targinfo;
102114
struct xt_rateest *est;
103115
struct {
@@ -108,18 +120,18 @@ static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
108120

109121
net_get_random_once(&jhash_rnd, sizeof(jhash_rnd));
110122

111-
mutex_lock(&xt_rateest_mutex);
112-
est = __xt_rateest_lookup(info->name);
123+
mutex_lock(&xn->hash_lock);
124+
est = __xt_rateest_lookup(xn, info->name);
113125
if (est) {
114-
mutex_unlock(&xt_rateest_mutex);
126+
mutex_unlock(&xn->hash_lock);
115127
/*
116128
* If estimator parameters are specified, they must match the
117129
* existing estimator.
118130
*/
119131
if ((!info->interval && !info->ewma_log) ||
120132
(info->interval != est->params.interval ||
121133
info->ewma_log != est->params.ewma_log)) {
122-
xt_rateest_put(est);
134+
xt_rateest_put(par->net, est);
123135
return -EINVAL;
124136
}
125137
info->est = est;
@@ -148,22 +160,22 @@ static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
148160
goto err2;
149161

150162
info->est = est;
151-
xt_rateest_hash_insert(est);
152-
mutex_unlock(&xt_rateest_mutex);
163+
xt_rateest_hash_insert(xn, est);
164+
mutex_unlock(&xn->hash_lock);
153165
return 0;
154166

155167
err2:
156168
kfree(est);
157169
err1:
158-
mutex_unlock(&xt_rateest_mutex);
170+
mutex_unlock(&xn->hash_lock);
159171
return ret;
160172
}
161173

162174
static void xt_rateest_tg_destroy(const struct xt_tgdtor_param *par)
163175
{
164176
struct xt_rateest_target_info *info = par->targinfo;
165177

166-
xt_rateest_put(info->est);
178+
xt_rateest_put(par->net, info->est);
167179
}
168180

169181
static struct xt_target xt_rateest_tg_reg __read_mostly = {
@@ -178,19 +190,46 @@ static struct xt_target xt_rateest_tg_reg __read_mostly = {
178190
.me = THIS_MODULE,
179191
};
180192

181-
static int __init xt_rateest_tg_init(void)
193+
static __net_init int xt_rateest_net_init(struct net *net)
194+
{
195+
struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
196+
int i;
197+
198+
mutex_init(&xn->hash_lock);
199+
for (i = 0; i < ARRAY_SIZE(xn->hash); i++)
200+
INIT_HLIST_HEAD(&xn->hash[i]);
201+
return 0;
202+
}
203+
204+
static void __net_exit xt_rateest_net_exit(struct net *net)
182205
{
183-
unsigned int i;
206+
struct xt_rateest_net *xn = net_generic(net, xt_rateest_id);
207+
int i;
208+
209+
for (i = 0; i < ARRAY_SIZE(xn->hash); i++)
210+
WARN_ON_ONCE(!hlist_empty(&xn->hash[i]));
211+
}
184212

185-
for (i = 0; i < ARRAY_SIZE(rateest_hash); i++)
186-
INIT_HLIST_HEAD(&rateest_hash[i]);
213+
static struct pernet_operations xt_rateest_net_ops = {
214+
.init = xt_rateest_net_init,
215+
.exit = xt_rateest_net_exit,
216+
.id = &xt_rateest_id,
217+
.size = sizeof(struct xt_rateest_net),
218+
};
219+
220+
static int __init xt_rateest_tg_init(void)
221+
{
222+
int err = register_pernet_subsys(&xt_rateest_net_ops);
187223

224+
if (err)
225+
return err;
188226
return xt_register_target(&xt_rateest_tg_reg);
189227
}
190228

191229
static void __exit xt_rateest_tg_fini(void)
192230
{
193231
xt_unregister_target(&xt_rateest_tg_reg);
232+
unregister_pernet_subsys(&xt_rateest_net_ops);
194233
}
195234

196235

net/netfilter/xt_rateest.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ static int xt_rateest_mt_checkentry(const struct xt_mtchk_param *par)
9595
}
9696

9797
ret = -ENOENT;
98-
est1 = xt_rateest_lookup(info->name1);
98+
est1 = xt_rateest_lookup(par->net, info->name1);
9999
if (!est1)
100100
goto err1;
101101

102102
est2 = NULL;
103103
if (info->flags & XT_RATEEST_MATCH_REL) {
104-
est2 = xt_rateest_lookup(info->name2);
104+
est2 = xt_rateest_lookup(par->net, info->name2);
105105
if (!est2)
106106
goto err2;
107107
}
@@ -111,7 +111,7 @@ static int xt_rateest_mt_checkentry(const struct xt_mtchk_param *par)
111111
return 0;
112112

113113
err2:
114-
xt_rateest_put(est1);
114+
xt_rateest_put(par->net, est1);
115115
err1:
116116
return ret;
117117
}
@@ -120,9 +120,9 @@ static void xt_rateest_mt_destroy(const struct xt_mtdtor_param *par)
120120
{
121121
struct xt_rateest_match_info *info = par->matchinfo;
122122

123-
xt_rateest_put(info->est1);
123+
xt_rateest_put(par->net, info->est1);
124124
if (info->est2)
125-
xt_rateest_put(info->est2);
125+
xt_rateest_put(par->net, info->est2);
126126
}
127127

128128
static struct xt_match xt_rateest_mt_reg __read_mostly = {

0 commit comments

Comments
 (0)