Skip to contentSkip to conversation

ipt_hashlimit updates

PATCH Applied Harald Welte 2 participants 2 messages Feb 22, 2005 – Feb 24, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipt_hashlimit updates

Hi Dave!

here are three incremental patches to ipt_hashlimit.

The first one is a bugfix, and should go in before 2.6.11 is released.

The other two are optimizations that can wait for 2.6.12. If you'd like, I can resubmit them at a later point.

Please apply,
thanks.

Signature · 5 lines
- Harald Welte <[email protected]>                 http://netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie

[PATCH] ipt_hashlimit rule load time race condition

This is the best we've got: We cannot release and re-grab lock, since checkentry() is called before ip_tables.c grabs ipt_mutex. We also cannot grab the hashtable spinlock, since htable_create will call vmalloc, and that can sleep. And we cannot just re-search the list of htable's in htable_create(), since then we would create duplicate proc files.

net/ipv4/netfilter/ipt_hashlimit.c +10 -0
@@ -98,6 +98,7 @@
 };
 
 static DECLARE_RWLOCK(hashlimit_lock);	/* protects htables list */
+static DECLARE_MUTEX(hlimit_mutex);	/* additional checkentry protection */
 static LIST_HEAD(hashlimit_htables);
 static kmem_cache_t *hashlimit_cachep;
 
@@ -531,10 +532,19 @@
 	if (!r->cfg.expire)
 		return 0;
 
+	/* This is the best we've got: We cannot release and re-grab lock,
+	 * since checkentry() is called before ip_tables.c grabs ipt_mutex.  
+	 * We also cannot grab the hashtable spinlock, since htable_create will 
+	 * call vmalloc, and that can sleep.  And we cannot just re-search
+	 * the list of htable's in htable_create(), since then we would
+	 * create duplicate proc files. -HW */
+	down(&hlimit_mutex);
 	r->hinfo = htable_find_get(r->name);
 	if (!r->hinfo && (htable_create(r) != 0)) {
+		up(&hlimit_mutex);
 		return 0;
 	}
+	up(&hlimit_mutex);
 
 	/* Ugly hack: For SMP, we only want to use one set */
 	r->u.master = r;

[PATCH] ipt_hashlimit: replace rwlock with spinlock

As Samuel points out, the rwlock doesn't really make much sense.

net/ipv4/netfilter/ipt_hashlimit.c +8 -8
@@ -97,7 +97,7 @@ struct ipt_hashlimit_htable {
 	struct list_head hash[0];	/* hashtable itself */
 };
 
-static DECLARE_RWLOCK(hashlimit_lock);	/* protects htables list */
+static DECLARE_LOCK(hashlimit_lock);	/* protects htables list */
 static DECLARE_MUTEX(hlimit_mutex);	/* additional checkentry protection */
 static LIST_HEAD(hashlimit_htables);
 static kmem_cache_t *hashlimit_cachep;
@@ -230,9 +230,9 @@ static int htable_create(struct ipt_hash
 	hinfo->timer.function = htable_gc;
 	add_timer(&hinfo->timer);
 
-	WRITE_LOCK(&hashlimit_lock);
+	LOCK_BH(&hashlimit_lock);
 	list_add(&hinfo->list, &hashlimit_htables);
-	WRITE_UNLOCK(&hashlimit_lock);
+	UNLOCK_BH(&hashlimit_lock);
 
 	return 0;
 }
@@ -296,15 +296,15 @@ static struct ipt_hashlimit_htable *htab
 {
 	struct ipt_hashlimit_htable *hinfo;
 
-	READ_LOCK(&hashlimit_lock);
+	LOCK_BH(&hashlimit_lock);
 	list_for_each_entry(hinfo, &hashlimit_htables, list) {
 		if (!strcmp(name, hinfo->pde->name)) {
 			atomic_inc(&hinfo->use);
-			READ_UNLOCK(&hashlimit_lock);
+			UNLOCK_BH(&hashlimit_lock);
 			return hinfo;
 		}
 	}
-	READ_UNLOCK(&hashlimit_lock);
+	UNLOCK_BH(&hashlimit_lock);
 
 	return NULL;
 }
@@ -312,9 +312,9 @@ static struct ipt_hashlimit_htable *htab
 static void htable_put(struct ipt_hashlimit_htable *hinfo)
 {
 	if (atomic_dec_and_test(&hinfo->use)) {
-		WRITE_LOCK(&hashlimit_lock);
+		LOCK_BH(&hashlimit_lock);
 		list_del(&hinfo->list);
-		WRITE_UNLOCK(&hashlimit_lock);
+		UNLOCK_BH(&hashlimit_lock);
 		htable_destroy(hinfo);
 	}
 }

[PATCH] ipt_hashlimit: use hlist instead of list head

Use hlist instead of list_head to save lots of memory for the hash buckets.

net/ipv4/netfilter/ipt_hashlimit.c +35 -24

145 lines not on this page · raw message

maintainer Applied
#1 lore raw
Quoted 1 line · On Tue, 22 Feb 2005 12:49:00 +0100 Harald Welte <[email protected]> wrote:

The first one is a bugfix, and should go in before 2.6.11 is released.

Applied to 2.6.11

Quoted 2 lines

The other two are optimizations that can wait for 2.6.12. If you'd like, I can resubmit them at a later point.

Applied to 2.6.12-pending

Thanks Harald.