Skip to content

Commit c4a3922

Browse files
congwangummakynes
authored andcommitted
netfilter: xt_hashlimit: reduce hashlimit_mutex scope for htable_put()
It is unnecessary to hold hashlimit_mutex for htable_destroy() as it is already removed from the global hashtable and its refcount is already zero. Also, switch hinfo->use to refcount_t so that we don't have to hold the mutex until it reaches zero in htable_put(). Reported-and-tested-by: [email protected] Acked-by: Florian Westphal <[email protected]> Signed-off-by: Cong Wang <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 259039f commit c4a3922

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

net/netfilter/xt_hashlimit.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/netfilter_ipv6/ip6_tables.h>
3737
#include <linux/mutex.h>
3838
#include <linux/kernel.h>
39+
#include <linux/refcount.h>
3940
#include <uapi/linux/netfilter/xt_hashlimit.h>
4041

4142
#define XT_HASHLIMIT_ALL (XT_HASHLIMIT_HASH_DIP | XT_HASHLIMIT_HASH_DPT | \
@@ -114,7 +115,7 @@ struct dsthash_ent {
114115

115116
struct xt_hashlimit_htable {
116117
struct hlist_node node; /* global list of all htables */
117-
int use;
118+
refcount_t use;
118119
u_int8_t family;
119120
bool rnd_initialized;
120121

@@ -315,7 +316,7 @@ static int htable_create(struct net *net, struct hashlimit_cfg3 *cfg,
315316
for (i = 0; i < hinfo->cfg.size; i++)
316317
INIT_HLIST_HEAD(&hinfo->hash[i]);
317318

318-
hinfo->use = 1;
319+
refcount_set(&hinfo->use, 1);
319320
hinfo->count = 0;
320321
hinfo->family = family;
321322
hinfo->rnd_initialized = false;
@@ -420,7 +421,7 @@ static struct xt_hashlimit_htable *htable_find_get(struct net *net,
420421
hlist_for_each_entry(hinfo, &hashlimit_net->htables, node) {
421422
if (!strcmp(name, hinfo->name) &&
422423
hinfo->family == family) {
423-
hinfo->use++;
424+
refcount_inc(&hinfo->use);
424425
return hinfo;
425426
}
426427
}
@@ -429,12 +430,11 @@ static struct xt_hashlimit_htable *htable_find_get(struct net *net,
429430

430431
static void htable_put(struct xt_hashlimit_htable *hinfo)
431432
{
432-
mutex_lock(&hashlimit_mutex);
433-
if (--hinfo->use == 0) {
433+
if (refcount_dec_and_mutex_lock(&hinfo->use, &hashlimit_mutex)) {
434434
hlist_del(&hinfo->node);
435+
mutex_unlock(&hashlimit_mutex);
435436
htable_destroy(hinfo);
436437
}
437-
mutex_unlock(&hashlimit_mutex);
438438
}
439439

440440
/* The algorithm used is the Simple Token Bucket Filter (TBF)

0 commit comments

Comments
 (0)