Skip to contentSkip to conversation

xt_RATEEST: acquire xt_rateest_mutex for hash insert

PATCH Applied Cong Wang 4 participants 4 messages Feb 5, 2018 – Feb 6, 2018

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH v2 1/1 xt_RATEEST: acquire xt_rateest_mutex for hash insert

rateest_hash is supposed to be protected by xt_rateest_mutex, and, as suggested by Eric, lookup and insert should be atomic, so we should acquire the xt_rateest_mutex once for both.

So introduce a non-locking helper for internal use and keep the locking one for external.

Fixes 5859034d7eb8 ("[NETFILTER]: x_tables: add RATEEST target")
Cc Pablo Neira Ayuso <[email protected]>
Cc Eric Dumazet <[email protected]>
---
 net/netfilter/xt_RATEEST.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
net/netfilter/xt_RATEEST.c +17 -5
index 498b54fd04d7..141c295191f6 100644
@@ -39,23 +39,31 @@ static void xt_rateest_hash_insert(struct xt_rateest *est)
 	hlist_add_head(&est->list, &rateest_hash[h]);
 }
 
-struct xt_rateest *xt_rateest_lookup(const char *name)
+static struct xt_rateest *__xt_rateest_lookup(const char *name)
 {
 	struct xt_rateest *est;
 	unsigned int h;
 
 	h = xt_rateest_hash(name);
-	mutex_lock(&xt_rateest_mutex);
 	hlist_for_each_entry(est, &rateest_hash[h], list) {
 		if (strcmp(est->name, name) == 0) {
 			est->refcnt++;
-			mutex_unlock(&xt_rateest_mutex);
 			return est;
 		}
 	}
-	mutex_unlock(&xt_rateest_mutex);
+
 	return NULL;
 }
+
+struct xt_rateest *xt_rateest_lookup(const char *name)
+{
+	struct xt_rateest *est;
+
+	mutex_lock(&xt_rateest_mutex);
+	est = __xt_rateest_lookup(name);
+	mutex_unlock(&xt_rateest_mutex);
+	return est;
+}
 EXPORT_SYMBOL_GPL(xt_rateest_lookup);
 
 void xt_rateest_put(struct xt_rateest *est)
@@ -100,8 +108,10 @@ static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
 
 	net_get_random_once(&jhash_rnd, sizeof(jhash_rnd));
 
-	est = xt_rateest_lookup(info->name);
+	mutex_lock(&xt_rateest_mutex);
+	est = __xt_rateest_lookup(info->name);
 	if (est) {
+		mutex_unlock(&xt_rateest_mutex);
 		/*
 		 * If estimator parameters are specified, they must match the
 		 * existing estimator.
@@ -139,11 +149,13 @@ static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
 
 	info->est = est;
 	xt_rateest_hash_insert(est);
+	mutex_unlock(&xt_rateest_mutex);
 	return 0;
 
 err2:
 	kfree(est);
 err1:
+	mutex_unlock(&xt_rateest_mutex);
 	return ret;
 }
 
Signature · 1 line
2.13.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Cong Wang

rateest_hash is supposed to be protected by xt_rateest_mutex, and, as suggested by Eric, lookup and insert should be atomic, so we should acquire the xt_rateest_mutex once for both.

So introduce a non-locking helper for internal use and keep the locking one for external.

Looks good, thanks Cong.

Reviewed-by on v2 1/1
#2 lore raw
In reply to Cong Wang
Quoted 7 lines · Cong Wang

rateest_hash is supposed to be protected by xt_rateest_mutex, and, as suggested by Eric, lookup and insert should be atomic, so we should acquire the xt_rateest_mutex once for both.

So introduce a non-locking helper for internal use and keep the locking one for external.

Quoted 6 lines
Fixes 5859034d7eb8 ("[NETFILTER]: x_tables: add RATEEST target")
Cc Pablo Neira Ayuso <[email protected]>
Cc Eric Dumazet <[email protected]>

---

Thanks !

maintainer Applied
#3 lore raw
In reply to Cong Wang
Quoted 6 lines · Cong Wang

rateest_hash is supposed to be protected by xt_rateest_mutex, and, as suggested by Eric, lookup and insert should be atomic, so we should acquire the xt_rateest_mutex once for both.

So introduce a non-locking helper for internal use and keep the locking one for external.

Applied, thanks!