Skip to contentSkip to conversation

xfrm_state locking regression...

PATCH David Miller 4 participants 95 messages Sep 3, 2008 – Oct 1, 2008

Subsystems (3)
lore
Share & tools
lore

David Miller

author maintainer
#0 lore raw

I've been hitting a lockup on my machine, and the way to hit it is sort of straight forward.

1) Setup IPSEC connection using openswan

2) Use something like XAUTH where openswan is not able refresh the IPSEC policy when it times out because it has no way to prompt the user for the XAUTH password again.

3) After the timeout, try to send a packet over the IPSEC connection, a simple DNS lookup over it works for me.

4) Shut down IPSEC daemon.

At this point with SMP we'll wedge in __xfrm_state_destroy() trying to obtain xfrm_state_lock.

This problem seems to have been introduced by:

commit 4c563f7669c10a12354b72b518c2287ffc6ebfb3

Author: Timo Teras <[email protected]>
Date:   Thu Feb 28 21:31:08 2008 -0800

[XFRM]: Speed up xfrm_policy and xfrm_state walking

(BTW, I want to point out how hesitant I was at the time to apply
this patch. It touches a lot of delicate areas...)

This is what added the xfrm_state_lock grabbing in __xfrm_state_destroy().

Such new locking cannot work properly, without also updating a lot of other code in net/xfrm/xfrm_state.c

The new locking means that it is absolutely illegal to do an xfrm_state_put() while holding xfrm_state_lock. Yet we do this all over the place!

Asynchronously another context can drop the count to one, and this xfrm_state_put() call will thus call into __xfrm_state_destroy() and try to take xfrm_state_lock (again) causing the deadlock.

I'm not exactly sure how to fix this currently.

I guess we can add a local pointer variable to the relevant functions "struct xfrm_state *to_put", which is initialized to NULL, and we replace the put calls with assignments to to_put. In the "out" path, after we drop the xfrm_state_lock, we do a put if "to_put" is non-NULL.

Something like the patch below, but frankly this is some ugly stuff.

I double checked xfrm_policy.c, since it has the potential to have similar issues due to the above changeset, but it appears to be OK the best I can tell.

ipsec: Fix deadlock in xfrm_state management.

Ever since commit 4c563f7669c10a12354b72b518c2287ffc6ebfb3 ("[XFRM]: Speed up xfrm_policy and xfrm_state walking") it is illegal to call __xfrm_state_destroy (and thus xfrm_state_put()) with xfrm_state_lock held. If we do, we'll deadlock since we have the lock already and __xfrm_state_destroy() tries to take it again.

Fix this by pushing the xfrm_state_put() calls after the lock is dropped.

net/xfrm/xfrm_state.c +31 -12
index 4c6914e..98f20cc 100644
@@ -780,11 +780,13 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 {
 	unsigned int h;
 	struct hlist_node *entry;
-	struct xfrm_state *x, *x0;
+	struct xfrm_state *x, *x0, *to_put;
 	int acquire_in_progress = 0;
 	int error = 0;
 	struct xfrm_state *best = NULL;
 
+	to_put = NULL;
+
 	spin_lock_bh(&xfrm_state_lock);
 	h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family);
 	hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
@@ -833,7 +835,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 		if (tmpl->id.spi &&
 		    (x0 = __xfrm_state_lookup(daddr, tmpl->id.spi,
 					      tmpl->id.proto, family)) != NULL) {
-			xfrm_state_put(x0);
+			to_put = x0;
 			error = -EEXIST;
 			goto out;
 		}
@@ -849,7 +851,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 		error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
 		if (error) {
 			x->km.state = XFRM_STATE_DEAD;
-			xfrm_state_put(x);
+			to_put = x;
 			x = NULL;
 			goto out;
 		}
@@ -870,7 +872,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 			xfrm_hash_grow_check(x->bydst.next != NULL);
 		} else {
 			x->km.state = XFRM_STATE_DEAD;
-			xfrm_state_put(x);
+			to_put = x;
 			x = NULL;
 			error = -ESRCH;
 		}
@@ -881,6 +883,8 @@ out:
 	else
 		*err = acquire_in_progress ? -EAGAIN : error;
 	spin_unlock_bh(&xfrm_state_lock);
+	if (to_put)
+		xfrm_state_put(to_put);
 	return x;
 }
 
@@ -1067,18 +1071,20 @@ static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq);
 
 int xfrm_state_add(struct xfrm_state *x)
 {
-	struct xfrm_state *x1;
+	struct xfrm_state *x1, *to_put;
 	int family;
 	int err;
 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
 
 	family = x->props.family;
 
+	to_put = NULL;
+
 	spin_lock_bh(&xfrm_state_lock);
 
 	x1 = __xfrm_state_locate(x, use_spi, family);
 	if (x1) {
-		xfrm_state_put(x1);
+		to_put = x1;
 		x1 = NULL;
 		err = -EEXIST;
 		goto out;
@@ -1088,7 +1094,7 @@ int xfrm_state_add(struct xfrm_state *x)
 		x1 = __xfrm_find_acq_byseq(x->km.seq);
 		if (x1 && ((x1->id.proto != x->id.proto) ||
 		    xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
-			xfrm_state_put(x1);
+			to_put = x1;
 			x1 = NULL;
 		}
 	}
@@ -1110,6 +1116,9 @@ out:
 		xfrm_state_put(x1);
 	}
 
+	if (to_put)
+		xfrm_state_put(to_put);
+
 	return err;
 }
 EXPORT_SYMBOL(xfrm_state_add);
@@ -1269,10 +1278,12 @@ EXPORT_SYMBOL(xfrm_state_migrate);
 
 int xfrm_state_update(struct xfrm_state *x)
 {
-	struct xfrm_state *x1;
+	struct xfrm_state *x1, *to_put;
 	int err;
 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
 
+	to_put = NULL;
+
 	spin_lock_bh(&xfrm_state_lock);
 	x1 = __xfrm_state_locate(x, use_spi, x->props.family);
 
@@ -1281,7 +1292,7 @@ int xfrm_state_update(struct xfrm_state *x)
 		goto out;
 
 	if (xfrm_state_kern(x1)) {
-		xfrm_state_put(x1);
+		to_put = x1;
 		err = -EEXIST;
 		goto out;
 	}
@@ -1295,6 +1306,9 @@ int xfrm_state_update(struct xfrm_state *x)
 out:
 	spin_unlock_bh(&xfrm_state_lock);
 
+	if (to_put)
+		xfrm_state_put(to_put);
+
 	if (err)
 		return err;
 
@@ -1483,11 +1497,13 @@ EXPORT_SYMBOL(xfrm_get_acqseq);
 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
 {
 	unsigned int h;
-	struct xfrm_state *x0;
+	struct xfrm_state *x0, *to_put;
 	int err = -ENOENT;
 	__be32 minspi = htonl(low);
 	__be32 maxspi = htonl(high);
 
+	to_put = NULL;
+
 	spin_lock_bh(&x->lock);
 	if (x->km.state == XFRM_STATE_DEAD)
 		goto unlock;
@@ -1501,7 +1517,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
 	if (minspi == maxspi) {
 		x0 = xfrm_state_lookup(&x->id.daddr, minspi, x->id.proto, x->props.family);
 		if (x0) {
-			xfrm_state_put(x0);
+			to_put = x0;
 			goto unlock;
 		}
 		x->id.spi = minspi;
@@ -1514,7 +1530,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
 				x->id.spi = htonl(spi);
 				break;
 			}
-			xfrm_state_put(x0);
+			to_put = x0;
 		}
 	}
 	if (x->id.spi) {
@@ -1529,6 +1545,9 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
 unlock:
 	spin_unlock_bh(&x->lock);
 
+	if (to_put)
+		xfrm_state_put(to_put);
+
 	return err;
 }
 EXPORT_SYMBOL(xfrm_alloc_spi);
author maintainer
#1 lore raw

From: David Miller <[email protected]>
Date: Tue, 02 Sep 2008 19:51:48 -0700 (PDT)

Quoted 3 lines
(quoted diff) +0 -0
@@ -1483,11 +1497,13 @@ EXPORT_SYMBOL(xfrm_get_acqseq);
 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
 {

...

Review int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high) · line 1536

Quoted 9 lines
(quoted diff) +1 -1
@@ -1514,7 +1530,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
 				x->id.spi = htonl(spi);
 				break;
 			}
-			xfrm_state_put(x0);
+			to_put = x0;
 		}
 	}
 	if (x->id.spi) {

This part of my patch is buggy, we loop here so would need to remember more state to do the puts right.

But luckily, this function doesn't need this to_put code, as it grabs x->lock not xfrm_state_lock so all the changes to this function can simply be removed :)

I'm going to do some testing on this and commit it unless more problems are found.

ipsec: Fix deadlock in xfrm_state management.

Ever since commit 4c563f7669c10a12354b72b518c2287ffc6ebfb3 ("[XFRM]: Speed up xfrm_policy and xfrm_state walking") it is illegal to call __xfrm_state_destroy (and thus xfrm_state_put()) with xfrm_state_lock held. If we do, we'll deadlock since we have the lock already and __xfrm_state_destroy() tries to take it again.

Fix this by pushing the xfrm_state_put() calls after the lock is dropped.

net/xfrm/xfrm_state.c +23 -9
index 4c6914e..7bd62f6 100644
@@ -780,11 +780,13 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 {
 	unsigned int h;
 	struct hlist_node *entry;
-	struct xfrm_state *x, *x0;
+	struct xfrm_state *x, *x0, *to_put;
 	int acquire_in_progress = 0;
 	int error = 0;
 	struct xfrm_state *best = NULL;
 
+	to_put = NULL;
+
 	spin_lock_bh(&xfrm_state_lock);
 	h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family);
 	hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
@@ -833,7 +835,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 		if (tmpl->id.spi &&
 		    (x0 = __xfrm_state_lookup(daddr, tmpl->id.spi,
 					      tmpl->id.proto, family)) != NULL) {
-			xfrm_state_put(x0);
+			to_put = x0;
 			error = -EEXIST;
 			goto out;
 		}
@@ -849,7 +851,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 		error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
 		if (error) {
 			x->km.state = XFRM_STATE_DEAD;
-			xfrm_state_put(x);
+			to_put = x;
 			x = NULL;
 			goto out;
 		}
@@ -870,7 +872,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 			xfrm_hash_grow_check(x->bydst.next != NULL);
 		} else {
 			x->km.state = XFRM_STATE_DEAD;
-			xfrm_state_put(x);
+			to_put = x;
 			x = NULL;
 			error = -ESRCH;
 		}
@@ -881,6 +883,8 @@ out:
 	else
 		*err = acquire_in_progress ? -EAGAIN : error;
 	spin_unlock_bh(&xfrm_state_lock);
+	if (to_put)
+		xfrm_state_put(to_put);
 	return x;
 }
 
@@ -1067,18 +1071,20 @@ static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq);
 
 int xfrm_state_add(struct xfrm_state *x)
 {
-	struct xfrm_state *x1;
+	struct xfrm_state *x1, *to_put;
 	int family;
 	int err;
 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
 
 	family = x->props.family;
 
+	to_put = NULL;
+
 	spin_lock_bh(&xfrm_state_lock);
 
 	x1 = __xfrm_state_locate(x, use_spi, family);
 	if (x1) {
-		xfrm_state_put(x1);
+		to_put = x1;
 		x1 = NULL;
 		err = -EEXIST;
 		goto out;
@@ -1088,7 +1094,7 @@ int xfrm_state_add(struct xfrm_state *x)
 		x1 = __xfrm_find_acq_byseq(x->km.seq);
 		if (x1 && ((x1->id.proto != x->id.proto) ||
 		    xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
-			xfrm_state_put(x1);
+			to_put = x1;
 			x1 = NULL;
 		}
 	}
@@ -1110,6 +1116,9 @@ out:
 		xfrm_state_put(x1);
 	}
 
+	if (to_put)
+		xfrm_state_put(to_put);
+
 	return err;
 }
 EXPORT_SYMBOL(xfrm_state_add);
@@ -1269,10 +1278,12 @@ EXPORT_SYMBOL(xfrm_state_migrate);
 
 int xfrm_state_update(struct xfrm_state *x)
 {
-	struct xfrm_state *x1;
+	struct xfrm_state *x1, *to_put;
 	int err;
 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
 
+	to_put = NULL;
+
 	spin_lock_bh(&xfrm_state_lock);
 	x1 = __xfrm_state_locate(x, use_spi, x->props.family);
 
@@ -1281,7 +1292,7 @@ int xfrm_state_update(struct xfrm_state *x)
 		goto out;
 
 	if (xfrm_state_kern(x1)) {
-		xfrm_state_put(x1);
+		to_put = x1;
 		err = -EEXIST;
 		goto out;
 	}
@@ -1295,6 +1306,9 @@ int xfrm_state_update(struct xfrm_state *x)
 out:
 	spin_unlock_bh(&xfrm_state_lock);
 
+	if (to_put)
+		xfrm_state_put(to_put);
+
 	if (err)
 		return err;
 
maintainer
#2 lore raw
In reply to David Miller
Quoted 2 lines · David Miller

This is what added the xfrm_state_lock grabbing in __xfrm_state_destroy().

This is simply bogus. We should remove the entry from the list when the state is deleted, not when it's destroyed. That is, we should be deleting x->all in places like __xfrm_state_delete instead of __xfrm_state_destroy.

The fact that we're adding x->all in __xfrm_state_insert means that we should be deleting it from its counter-part, which is __xfrm_state_delete.

The same thing applies to the policies of course. Once a policy or state is deleted there is no reason why it should show up in a walk just because some dst is holding onto it.

Quoted 9 lines

ipsec: Fix deadlock in xfrm_state management.

Ever since commit 4c563f7669c10a12354b72b518c2287ffc6ebfb3 ("[XFRM]: Speed up xfrm_policy and xfrm_state walking") it is illegal to call __xfrm_state_destroy (and thus xfrm_state_put()) with xfrm_state_lock held. If we do, we'll deadlock since we have the lock already and __xfrm_state_destroy() tries to take it again.

Quoted 4 lines

Fix this by pushing the xfrm_state_put() calls after the lock is dropped.

However, moving things out of critical sections is always a good idea. So I think your patch is an improvement regardless of why it came about :)

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
Quoted 8 lines · Herbert Xu
Quoted 1 line · David Miller

This is what added the xfrm_state_lock grabbing in __xfrm_state_destroy().

This is simply bogus. We should remove the entry from the list when the state is deleted, not when it's destroyed. That is, we should be deleting x->all in places like __xfrm_state_delete instead of __xfrm_state_destroy.

Quoted 7 lines

The fact that we're adding x->all in __xfrm_state_insert means that we should be deleting it from its counter-part, which is __xfrm_state_delete.

The same thing applies to the policies of course. Once a policy or state is deleted there is no reason why it should show up in a walk just because some dst is holding onto it.

But then caller of xfrm_state_walk() can be still holding a reference to the entry and dumping of the whole chain fails, because the entry is no longer part of the chain when dumping continues later.

The walking coding skips entries which are dead so the entry is only kept temporarily to make a full traversal of the list.

Quoted 9 lines
Quoted 9 lines

ipsec: Fix deadlock in xfrm_state management.

Ever since commit 4c563f7669c10a12354b72b518c2287ffc6ebfb3 ("[XFRM]: Speed up xfrm_policy and xfrm_state walking") it is illegal to call __xfrm_state_destroy (and thus xfrm_state_put()) with xfrm_state_lock held. If we do, we'll deadlock since we have the lock already and __xfrm_state_destroy() tries to take it again.

Quoted 8 lines
Quoted 4 lines

Fix this by pushing the xfrm_state_put() calls after the lock is dropped.

However, moving things out of critical sections is always a good idea. So I think your patch is an improvement regardless of why it came about :)

Right. Also alternatively the xfrm_state_all could be protected by a different lock than xfrm_state_lock.

- Timo

maintainer
#4 lore raw
Quoted 9 lines · Timo Teräs

But then caller of xfrm_state_walk() can be still holding a reference to the entry and dumping of the whole chain fails, because the entry is no longer part of the chain when dumping continues later.

The walking coding skips entries which are dead so the entry is only kept temporarily to make a full traversal of the list.

Ah I see your point now.

Actually another thing we should do is to postpone the unlinking into the GC process. This doesn't really need to be immediate.

And here is a patch to fix a couple of changes in user-behaviour.

ipsec: Restore larval states and socket policies in dump

The commit commit 4c563f7669c10a12354b72b518c2287ffc6ebfb3 ("[XFRM]:
Speed up xfrm_policy and xfrm_state walking") inadvertently removed larval states and socket policies from netlink dumps. This patch restores them.

net/xfrm/xfrm_policy.c +1 -0
index 841b32a..5138845 100644
@@ -1077,6 +1077,7 @@ static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
 	struct hlist_head *chain = policy_hash_bysel(&pol->selector,
 						     pol->family, dir);
 
+	list_add_tail(&pol->bytype, &xfrm_policy_bytype[pol->type]);
 	hlist_add_head(&pol->bydst, chain);
 	hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
 	xfrm_policy_count[dir]++;
net/xfrm/xfrm_state.c +2 -0
index 4c6914e..89551b6 100644
@@ -856,6 +856,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 
 		if (km_query(x, tmpl, pol) == 0) {
 			x->km.state = XFRM_STATE_ACQ;
+			list_add_tail(&x->all, &xfrm_state_all);
 			hlist_add_head(&x->bydst, xfrm_state_bydst+h);
 			h = xfrm_src_hash(daddr, saddr, family);
 			hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
@@ -1051,6 +1052,7 @@ static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 re
 		xfrm_state_hold(x);
 		x->timer.expires = jiffies + sysctl_xfrm_acq_expires*HZ;
 		add_timer(&x->timer);
+		list_add_tail(&x->all, &xfrm_state_all);
 		hlist_add_head(&x->bydst, xfrm_state_bydst+h);
 		h = xfrm_src_hash(daddr, saddr, family);
 		hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#5 lore raw
Quoted 6 lines · Herbert Xu
Quoted 5 lines · Timo Teräs

But then caller of xfrm_state_walk() can be still holding a reference to the entry and dumping of the whole chain fails, because the entry is no longer part of the chain when dumping continues later.

Quoted 8 lines
Quoted 3 lines

The walking coding skips entries which are dead so the entry is only kept temporarily to make a full traversal of the list.

Ah I see your point now.

Actually another thing we should do is to postpone the unlinking into the GC process. This doesn't really need to be immediate.

Yes, might be a good idea.

Quoted 8 lines

And here is a patch to fix a couple of changes in user-behaviour.

ipsec: Restore larval states and socket policies in dump

The commit commit 4c563f7669c10a12354b72b518c2287ffc6ebfb3 ("[XFRM]:
Speed up xfrm_policy and xfrm_state walking") inadvertently removed larval states and socket policies from netlink dumps. This patch restores them.

Oh, I seemed to have missed that there was multiple places where the list insertion happens. Would it make sense to make function for inserting the entries in the lists?

- Timo

maintainer
#8 lore raw
In reply to Timo Teräs
Quoted 3 lines · Timo Teräs

Right. Also alternatively the xfrm_state_all could be protected by a different lock than xfrm_state_lock.

Yes that's a good idea. Here's a patch to do that for states. I'm still thinking about whether policy destruction should be restructured or not so I'm not patching that.

ipsec: Move state dump list unlinking into GC

This patch moves the unlinking of x->all (for dumping) into the GC and changes the lock to xfrm_cfg_mutex.

net/xfrm/xfrm_state.c +4 -4
index 89551b6..f29923a 100644
@@ -380,6 +380,10 @@ static void xfrm_put_mode(struct xfrm_mode *mode)
 
 static void xfrm_state_gc_destroy(struct xfrm_state *x)
 {
+	mutex_lock(&xfrm_cfg_mutex);
+	list_del(&x->all);
+	mutex_unlock(&xfrm_cfg_mutex);
+
 	del_timer_sync(&x->timer);
 	del_timer_sync(&x->rtimer);
 	kfree(x->aalg);
@@ -540,10 +544,6 @@ void __xfrm_state_destroy(struct xfrm_state *x)
 {
 	WARN_ON(x->km.state != XFRM_STATE_DEAD);
 
-	spin_lock_bh(&xfrm_state_lock);
-	list_del(&x->all);
-	spin_unlock_bh(&xfrm_state_lock);
-
 	spin_lock_bh(&xfrm_state_gc_lock);
 	hlist_add_head(&x->bydst, &xfrm_state_gc_list);
 	spin_unlock_bh(&xfrm_state_gc_lock);

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#6 lore raw
In reply to Timo Teräs
Quoted 4 lines · Timo Teräs

Oh, I seemed to have missed that there was multiple places where the list insertion happens. Would it make sense to make function for inserting the entries in the lists?

Sure, grep for the places that change xfrm_state_num and you've got your function :)

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#9 lore raw
In reply to Herbert Xu
Quoted 4 lines · Herbert Xu
Quoted 2 lines · Timo Teräs

Right. Also alternatively the xfrm_state_all could be protected by a different lock than xfrm_state_lock.

Quoted 8 lines

Yes that's a good idea. Here's a patch to do that for states. I'm still thinking about whether policy destruction should be restructured or not so I'm not patching that.

ipsec: Move state dump list unlinking into GC

This patch moves the unlinking of x->all (for dumping) into the GC and changes the lock to xfrm_cfg_mutex.

Shouldn't this also change the locking in all places where x->all is used?

maintainer
#10 lore raw
Quoted 8 lines · Timo Teräs
Quoted 5 lines

ipsec: Move state dump list unlinking into GC

This patch moves the unlinking of x->all (for dumping) into the GC and changes the lock to xfrm_cfg_mutex.

Shouldn't this also change the locking in all places where x->all is used?

You're right. I'd forgotten about the larval states for some reason, probably because I just added them to the list :)

So let's keep xfrm_state_lock and just move it.

ipsec: Move state dump list unlinking into GC

This patch moves the unlinking of x->all (for dumping) into the GC so that we know for sure what locks we hold when we unlink the entry.

net/xfrm/xfrm_state.c +4 -4
index 89551b6..0244325 100644
@@ -380,6 +380,10 @@ static void xfrm_put_mode(struct xfrm_mode *mode)
 
 static void xfrm_state_gc_destroy(struct xfrm_state *x)
 {
+	spin_lock_bh(&xfrm_state_lock);
+	list_del(&x->all);
+	spin_unlock_bh(&xfrm_state_lock);
+
 	del_timer_sync(&x->timer);
 	del_timer_sync(&x->rtimer);
 	kfree(x->aalg);
@@ -540,10 +544,6 @@ void __xfrm_state_destroy(struct xfrm_state *x)
 {
 	WARN_ON(x->km.state != XFRM_STATE_DEAD);
 
-	spin_lock_bh(&xfrm_state_lock);
-	list_del(&x->all);
-	spin_unlock_bh(&xfrm_state_lock);
-
 	spin_lock_bh(&xfrm_state_gc_lock);
 	hlist_add_head(&x->bydst, &xfrm_state_gc_list);
 	spin_unlock_bh(&xfrm_state_gc_lock);

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#94 lore raw
In reply to Timo Teräs

From: Timo Teräs <[email protected]>
Date: Wed, 03 Sep 2008 08:07:33 +0300

Quoted 2 lines

Also alternatively the xfrm_state_all could be protected by a different lock than xfrm_state_lock.

Before your changes the state destroy was protected by no central locks at all. There was no need.

Since the reference goes to zero, there are no external references to this object.

But this is exactly what you added, an external references that is not reference counted.

This is yet another reason all of Herbert's objections to your location for ->all list add and removal are sound.

So we now have to take a centralized lock twice when getting rid of an XFRM state object. Which is potentially a scalability performance regression.

author maintainer
#11 lore raw
In reply to Herbert Xu

From: Herbert Xu <[email protected]>
Date: Wed, 3 Sep 2008 15:50:41 +1000

Quoted 7 lines · Timo Teräs
Quoted 6 lines
Quoted 5 lines

ipsec: Move state dump list unlinking into GC

This patch moves the unlinking of x->all (for dumping) into the GC and changes the lock to xfrm_cfg_mutex.

Quoted 7 lines
Quoted 2 lines

Shouldn't this also change the locking in all places where x->all is used?

You're right. I'd forgotten about the larval states for some reason, probably because I just added them to the list :)

So let's keep xfrm_state_lock and just move it.

How about we un-crap the reference counting?

Every list an object lives on is an external reference, and that is the rule that is violated by this ->all list handling.

And that's why we need to take a single shared lock twice just to get rid of an xfrm_state object now.

#12 lore raw
Quoted 15 lines · David Miller

From: Herbert Xu <[email protected]>
Date: Wed, 3 Sep 2008 15:50:41 +1000

Quoted 11 lines · Timo Teräs
Quoted 6 lines
Quoted 4 lines

ipsec: Move state dump list unlinking into GC

This patch moves the unlinking of x->all (for dumping) into the GC and changes the lock to xfrm_cfg_mutex.

Shouldn't this also change the locking in all places where x->all is used?

You're right. I'd forgotten about the larval states for some reason, probably because I just added them to the list :)

So let's keep xfrm_state_lock and just move it.

Quoted 7 lines

How about we un-crap the reference counting?

Every list an object lives on is an external reference, and that is the rule that is violated by this ->all list handling.

And that's why we need to take a single shared lock twice just to get rid of an xfrm_state object now.

Well, it's just another list keeping a reference like ->bydst, ->bysrc and ->byspi. The actual amount of external references is still correct (the walking code calls _hold() when it returns while keeping an external pointer).

The difference is that node should not be unlinked from ->all until all other references are gone. For other lists the unlinking can be done earlier since they are used only for lookups.

Any good other ways to enumerate to list entries while allowing to keep a temporary "iterator"? The previous method was crap too.

- Timo

author maintainer
#13 lore raw

From: Timo Teräs <[email protected]>
Date: Wed, 03 Sep 2008 09:27:47 +0300

Quoted 4 lines

Well, it's just another list keeping a reference like ->bydst, ->bysrc and ->byspi. The actual amount of external references is still correct (the walking code calls _hold() when it returns while keeping an external pointer).

->bydst, ->bysrc, and ->byspi are counted as a single external reference because:

1) They are controlled as a group

2) Doing 3 atomic operations is more expensive than one

I know because I did that conversion from 3 refcount operations down to 1 and I timed it with stress tests, which showed that it made a huge performance difference for the control path of our IPSEC stack.

Quoted 3 lines

The difference is that node should not be unlinked from ->all until all other references are gone. For other lists the unlinking can be done earlier since they are used only for lookups.

Once there are no list references, there cannot be any other references. So in fact it seems to me that unlinking when the xfrm_state is removed from those other lists makes perfect sense.

If __xfrm_state_delete sets the state to DEAD, and you skip xfrm_state objects marked DEAD, why does the ->all list reference have to survive past __xfrm_state_delete()?

It seems the perfect place to do the ->all removal.

Quoted 2 lines

Any good other ways to enumerate to list entries while allowing to keep a temporary "iterator"? The previous method was crap too.

At least the old stuff was self-consistent and only needed one central lock grab to destoy an object.

#14 lore raw
Quoted 11 lines · David Miller

From: Timo Teräs <[email protected]>
Date: Wed, 03 Sep 2008 09:27:47 +0300

Quoted 4 lines

Well, it's just another list keeping a reference like ->bydst, ->bysrc and ->byspi. The actual amount of external references is still correct (the walking code calls _hold() when it returns while keeping an external pointer).

->bydst, ->bysrc, and ->byspi are counted as a single external reference because:

Quoted 7 lines

1) They are controlled as a group

2) Doing 3 atomic operations is more expensive than one

I know because I did that conversion from 3 refcount operations down to 1 and I timed it with stress tests, which showed that it made a huge performance difference for the control path of our IPSEC stack.

I was a bit confused what you meant by "external reference". But yes, in this sense it's adding a new external reference.

Quoted 8 lines
Quoted 3 lines

The difference is that node should not be unlinked from ->all until all other references are gone. For other lists the unlinking can be done earlier since they are used only for lookups.

Once there are no list references, there cannot be any other references. So in fact it seems to me that unlinking when the xfrm_state is removed from those other lists makes perfect sense.

Quoted 5 lines

If __xfrm_state_delete sets the state to DEAD, and you skip xfrm_state objects marked DEAD, why does the ->all list reference have to survive past __xfrm_state_delete()?

It seems the perfect place to do the ->all removal.

1. xfrm_state_walk() called, it returns but holds an entry since the walking was interrupted temporarily (e.g. full netlink buffer).

2. xfrm_state_delete() called to the entry that xfrm_state_walk() is keeping a pointer to and it is unlinked.

3. xfrm_state_walk() called again, it tries to resume list walking but whoops, the entry was unlinked and kaboom.

Quoted 5 lines
Quoted 2 lines

Any good other ways to enumerate to list entries while allowing to keep a temporary "iterator"? The previous method was crap too.

At least the old stuff was self-consistent and only needed one central lock grab to destoy an object.

Yes, but the dumping code produced crap. It could dump same entry multiple times, miss entries and was dog slow. With it there was no possibility to keep userland in sync with kernel SPD/SAD because entries were lost.

- Timo

author maintainer
#15 lore raw

From: Timo Teräs <[email protected]>
Date: Wed, 03 Sep 2008 09:45:48 +0300

Quoted 11 lines
Quoted 9 lines · David Miller

Once there are no list references, there cannot be any other references. So in fact it seems to me that unlinking when the xfrm_state is removed from those other lists makes perfect sense.

If __xfrm_state_delete sets the state to DEAD, and you skip xfrm_state objects marked DEAD, why does the ->all list reference have to survive past __xfrm_state_delete()?

It seems the perfect place to do the ->all removal.

Quoted 8 lines

1. xfrm_state_walk() called, it returns but holds an entry since the walking was interrupted temporarily (e.g. full netlink buffer).

2. xfrm_state_delete() called to the entry that xfrm_state_walk() is keeping a pointer to and it is unlinked.

3. xfrm_state_walk() called again, it tries to resume list walking but whoops, the entry was unlinked and kaboom.

Get creative, use a key of some sort to continue the walk, that's what other netlink'ish subsystems use.

Quoted 4 lines

Yes, but the dumping code produced crap. It could dump same entry multiple times, miss entries and was dog slow. With it there was no possibility to keep userland in sync with kernel SPD/SAD because entries were lost.

I'd rather see an entry twice in a dump than have my IPSEC gateway lockup, or run slower because we take a lock twice as often as necessary during object destruction.

#16 lore raw
Quoted 2 lines · David Miller

Get creative, use a key of some sort to continue the walk, that's what other netlink'ish subsystems use.

Ok. ctnetlink_dump_table() seems to iterate the hash list and keeps a unique id where it left. If that entry was deleted it starts again from that hash bucket.

We could iterate SPD by idx. By SPI would be nice for SAD, but not all entries have SPI. I guess we could use either of the bysrc or bydst hash and memorize xfrm_id. Restart from first hash bucket entry if the memorized entry gets deleted, and restart from the beginning if hash gets resized.

This way we could avoid the additional locking and guarantee that all entries are dumped in reasonable time.

Quoted 8 lines
Quoted 4 lines

Yes, but the dumping code produced crap. It could dump same entry multiple times, miss entries and was dog slow. With it there was no possibility to keep userland in sync with kernel SPD/SAD because entries were lost.

I'd rather see an entry twice in a dump than have my IPSEC gateway lockup, or run slower because we take a lock twice as often as necessary during object destruction.

Seeing entry twice is not a problem. Not seeing an entry at all was the real problem. Also listing SAD could take tens of seconds on modern system. That's not nice either.

- Timo

maintainer
#17 lore raw
In reply to David Miller
Quoted 3 lines · David Miller

Get creative, use a key of some sort to continue the walk, that's what other netlink'ish subsystems use.

OK let's see if this is creative enough :)

The next step after this patch is to kill x->all altogether and go back to the old walking method (plus the saved state).

ipsec: Use RCU-like construct for saved state within a walk

Now that we save states within a walk we need synchronisation so that the list the saved state is on doesn't disappear from under us.

As it stands this is done by keeping the state on the list which is bad because it gets in the way of the management of the state life-cycle.

An alternative is to make our own pseudo-RCU system where we use counters to indicate which state can't be freed immediately as it may be referenced by an ongoing walk when that resumes.

include/net/xfrm.h +5 -10
index 2933d74..4bb9499 100644
@@ -120,9 +120,11 @@ extern struct mutex xfrm_cfg_mutex;
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	/* Note: bydst is re-used during gc */
 	struct list_head	all;
-	struct hlist_node	bydst;
+	union {
+		struct list_head	gclist;
+		struct hlist_node	bydst;
+	};
 	struct hlist_node	bysrc;
 	struct hlist_node	byspi;
 
@@ -1286,16 +1288,9 @@ static inline void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
 	walk->count = 0;
 }
 
-static inline void xfrm_state_walk_done(struct xfrm_state_walk *walk)
-{
-	if (walk->state != NULL) {
-		xfrm_state_put(walk->state);
-		walk->state = NULL;
-	}
-}
-
 extern int xfrm_state_walk(struct xfrm_state_walk *walk,
 			   int (*func)(struct xfrm_state *, int, void*), void *);
+extern void xfrm_state_walk_done(struct xfrm_state_walk *walk);
 extern struct xfrm_state *xfrm_state_alloc(void);
 extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, 
 					  struct flowi *fl, struct xfrm_tmpl *tmpl,
net/xfrm/xfrm_state.c +37 -13

103 lines not on this page · raw message

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#18 lore raw

From: Herbert Xu <[email protected]>
Date: Fri, 5 Sep 2008 21:55:07 +1000

Quoted 20 lines
net/xfrm/xfrm_state.c +6 -6
@@ -403,17 +408,23 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)
 
 static void xfrm_state_gc_task(struct work_struct *data)
 {
-	struct xfrm_state *x;
-	struct hlist_node *entry, *tmp;
-	struct hlist_head gc_list;
+	struct xfrm_state *x, *tmp;
+	static LIST_HEAD(gc_list);
+	unsigned long completed;
 
 	spin_lock_bh(&xfrm_state_gc_lock);
-	gc_list.first = xfrm_state_gc_list.first;
-	INIT_HLIST_HEAD(&xfrm_state_gc_list);
+	list_splice_tail_init(&xfrm_state_gc_list, &gc_list);
 	spin_unlock_bh(&xfrm_state_gc_lock);
 
-	hlist_for_each_entry_safe(x, entry, tmp, &gc_list, bydst)
+	mutex_lock(&xfrm_cfg_mutex);
+	completed = xfrm_state_walk_completed;

net/xfrm/xfrm_state.c static void xfrm_state_gc_destroy(struct xfrm_state *x) · line 430

Quoted 10 lines
net/xfrm/xfrm_state.c +6 -0
+	mutex_unlock(&xfrm_cfg_mutex);
+
+	list_for_each_entry_safe(x, tmp, &gc_list, gclist) {
+		if ((long)(x->lastused - completed) > 0)
+			break;
 		xfrm_state_gc_destroy(x);
+	}
 
 	wake_up(&km_waitq);
 }

What happens to all of the entries on the local gc_list which you will be skipping when you break out of that last loop?

It looks like they will never get processed.

maintainer
#19 lore raw
Quoted 31 lines · David Miller

From: Herbert Xu <[email protected]>
Date: Fri, 5 Sep 2008 21:55:07 +1000

Quoted 28 lines
net/xfrm/xfrm_state.c +12 -6
@@ -403,17 +408,23 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)
 
 static void xfrm_state_gc_task(struct work_struct *data)
 {
-	struct xfrm_state *x;
-	struct hlist_node *entry, *tmp;
-	struct hlist_head gc_list;
+	struct xfrm_state *x, *tmp;
+	static LIST_HEAD(gc_list);
+	unsigned long completed;
 
 	spin_lock_bh(&xfrm_state_gc_lock);
-	gc_list.first = xfrm_state_gc_list.first;
-	INIT_HLIST_HEAD(&xfrm_state_gc_list);
+	list_splice_tail_init(&xfrm_state_gc_list, &gc_list);
 	spin_unlock_bh(&xfrm_state_gc_lock);
 
-	hlist_for_each_entry_safe(x, entry, tmp, &gc_list, bydst)
+	mutex_lock(&xfrm_cfg_mutex);
+	completed = xfrm_state_walk_completed;
+	mutex_unlock(&xfrm_cfg_mutex);
+
+	list_for_each_entry_safe(x, tmp, &gc_list, gclist) {
+		if ((long)(x->lastused - completed) > 0)
+			break;
 		xfrm_state_gc_destroy(x);
+	}
 
Quoted 7 lines

net/xfrm/xfrm_state.c static void xfrm_state_gc_destroy(struct xfrm_state *x) · line 430

Quoted 2 lines
net/xfrm/xfrm_state.c +0 -0
 	wake_up(&km_waitq);
 }

What happens to all of the entries on the local gc_list which you will be skipping when you break out of that last loop?

It looks like they will never get processed.

I made gc_list static, so they'll be processed when the GC task runs again. Also whenever completed is incremented we schedule the GC task.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#20 lore raw

From: Herbert Xu <[email protected]>
Date: Tue, 9 Sep 2008 10:18:28 +1000

Quoted 9 lines · David Miller
Quoted 4 lines

What happens to all of the entries on the local gc_list which you will be skipping when you break out of that last loop?

It looks like they will never get processed.

I made gc_list static, so they'll be processed when the GC task runs again. Also whenever completed is incremented we schedule the GC task.

Thanks, I didn't catch that "static" bit :-)

author maintainer
#21 lore raw
In reply to Herbert Xu

From: Herbert Xu <[email protected]>
Date: Fri, 5 Sep 2008 21:55:07 +1000

Quoted 1 line

ipsec: Use RCU-like construct for saved state within a walk

Ok, now that I finally understand how this works I like it.

The only comment I would make is that maybe it's a bit excessive to trigger the GC worker every time we walk the states.

Instead you could expose the GC task's local list go:

	xfrm_state_walk_completed++;
	smp_wmb();
	if (!list_empty(&xfrm_state_gc_work_list))
		schedule_work(&xfrm_state_gc_work);

in xfrm_state_walk_done().

author maintainer Applied
#7 lore raw
In reply to Herbert Xu

From: Herbert Xu <[email protected]>
Date: Wed, 3 Sep 2008 15:23:30 +1000

Quoted 8 lines

ipsec: Restore larval states and socket policies in dump

The commit commit 4c563f7669c10a12354b72b518c2287ffc6ebfb3 ("[XFRM]:
Speed up xfrm_policy and xfrm_state walking") inadvertently removed larval states and socket policies from netlink dumps. This patch restores them.

Applied to net-2.6, thanks Herbert.

maintainer
#22 lore raw
In reply to David Miller
Quoted 3 lines · David Miller

The only comment I would make is that maybe it's a bit excessive to trigger the GC worker every time we walk the states.

Good point!

I've avoided the memory barrier by simply extending the mutexed section in the GC to cover the list splicing. Here's the updated patch:

ipsec: Use RCU-like construct for saved state within a walk

Now that we save states within a walk we need synchronisation so that the list the saved state is on doesn't disappear from under us.

As it stands this is done by keeping the state on the list which is bad because it gets in the way of the management of the state life-cycle.

An alternative is to make our own pseudo-RCU system where we use counters to indicate which state can't be freed immediately as it may be referenced by an ongoing walk when that resumes.

include/net/xfrm.h +5 -10
index 2933d74..4bb9499 100644
@@ -120,9 +120,11 @@ extern struct mutex xfrm_cfg_mutex;
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	/* Note: bydst is re-used during gc */
 	struct list_head	all;
-	struct hlist_node	bydst;
+	union {
+		struct list_head	gclist;
+		struct hlist_node	bydst;
+	};
 	struct hlist_node	bysrc;
 	struct hlist_node	byspi;
 
@@ -1286,16 +1288,9 @@ static inline void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
 	walk->count = 0;
 }
 
-static inline void xfrm_state_walk_done(struct xfrm_state_walk *walk)
-{
-	if (walk->state != NULL) {
-		xfrm_state_put(walk->state);
-		walk->state = NULL;
-	}
-}
-
 extern int xfrm_state_walk(struct xfrm_state_walk *walk,
 			   int (*func)(struct xfrm_state *, int, void*), void *);
+extern void xfrm_state_walk_done(struct xfrm_state_walk *walk);
 extern struct xfrm_state *xfrm_state_alloc(void);
 extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, 
 					  struct flowi *fl, struct xfrm_tmpl *tmpl,
net/xfrm/xfrm_state.c +39 -13

105 lines not on this page · raw message

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#23 lore raw

From: Herbert Xu <[email protected]>
Date: Wed, 10 Sep 2008 00:33:12 +1000

Quoted 5 lines

I've avoided the memory barrier by simply extending the mutexed section in the GC to cover the list splicing. Here's the updated patch:

ipsec: Use RCU-like construct for saved state within a walk

Looks good. I'll be able to merge this next time I pull net-2.6 into net-next-2.6 because of your larval/socket policy fix which touched similar areas.

author maintainer
#78 lore raw
In reply to Herbert Xu

From: Herbert Xu <[email protected]>
Date: Wed, 3 Sep 2008 15:50:41 +1000

Quoted 7 lines

ipsec: Move state dump list unlinking into GC

This patch moves the unlinking of x->all (for dumping) into the GC so that we know for sure what locks we hold when we unlink the entry.

After your patch I just applied, things are a bit different now.

Are you going to respin this or is it not relevant any longer?

Quoted 33 lines
net/xfrm/xfrm_state.c +4 -4
index 89551b6..0244325 100644
@@ -380,6 +380,10 @@ static void xfrm_put_mode(struct xfrm_mode *mode)
 
 static void xfrm_state_gc_destroy(struct xfrm_state *x)
 {
+	spin_lock_bh(&xfrm_state_lock);
+	list_del(&x->all);
+	spin_unlock_bh(&xfrm_state_lock);
+
 	del_timer_sync(&x->timer);
 	del_timer_sync(&x->rtimer);
 	kfree(x->aalg);
@@ -540,10 +544,6 @@ void __xfrm_state_destroy(struct xfrm_state *x)
 {
 	WARN_ON(x->km.state != XFRM_STATE_DEAD);
 
-	spin_lock_bh(&xfrm_state_lock);
-	list_del(&x->all);
-	spin_unlock_bh(&xfrm_state_lock);
-
 	spin_lock_bh(&xfrm_state_gc_lock);
 	hlist_add_head(&x->bydst, &xfrm_state_gc_list);
 	spin_unlock_bh(&xfrm_state_gc_lock);

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#79 lore raw
Quoted 9 lines · David Miller

From: Herbert Xu <[email protected]>
Date: Wed, 3 Sep 2008 15:50:41 +1000

Quoted 6 lines

ipsec: Move state dump list unlinking into GC

This patch moves the unlinking of x->all (for dumping) into the GC so that we know for sure what locks we hold when we unlink the entry.

Quoted 5 lines
Quoted 1 line

After your patch I just applied, things are a bit different now.

Are you going to respin this or is it not relevant any longer?

This patch is now redundant.

What could be done though is to remove x->all and restore the original walking by hashing. This would eliminate a list node from xfrm_state, and would also restore the original dump ordering.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#80 lore raw

From: Herbert Xu <[email protected]>
Date: Wed, 10 Sep 2008 13:15:59 +1000

Quoted 1 line

This patch is now redundant.

Ok.

Quoted 3 lines

What could be done though is to remove x->all and restore the original walking by hashing. This would eliminate a list node from xfrm_state, and would also restore the original dump ordering.

Yes I remember you mentioning that.

If you're too busy I can try to spin this idea into a patch.

maintainer
#81 lore raw
Quoted 8 lines · David Miller
Quoted 4 lines

What could be done though is to remove x->all and restore the original walking by hashing. This would eliminate a list node from xfrm_state, and would also restore the original dump ordering.

Yes I remember you mentioning that.

If you're too busy I can try to spin this idea into a patch.

That would be very much appreciated. Thanks Dave!

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#82 lore raw

From: Herbert Xu <[email protected]>
Date: Wed, 10 Sep 2008 13:23:04 +1000

Quoted 11 lines · David Miller
Quoted 8 lines
Quoted 4 lines

What could be done though is to remove x->all and restore the original walking by hashing. This would eliminate a list node from xfrm_state, and would also restore the original dump ordering.

Yes I remember you mentioning that.

If you're too busy I can try to spin this idea into a patch.

That would be very much appreciated. Thanks Dave!

No problem. It might be a little bit of a chore because this new walker design is intimately tied to the af_key non-atomic dump changes.

maintainer
#83 lore raw
Quoted 4 lines · David Miller

No problem. It might be a little bit of a chore because this new walker design is intimately tied to the af_key non-atomic dump changes.

Actually I was mistaken as to how the original dump worked. I'd thought that it actually kept track of which bucket it was in and resumed from that bucket. However in reality it only had a global counter and would always start walking from the beginning up until the counted value. So it isn't as easy as just copying the old code across :)

While it is possible to restore the old order and save a little bit of memory, I certainly don't think this is very urgent.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#84 lore raw

From: Herbert Xu <[email protected]>
Date: Wed, 10 Sep 2008 14:01:07 +1000

Quoted 6 lines · David Miller
Quoted 4 lines

No problem. It might be a little bit of a chore because this new walker design is intimately tied to the af_key non-atomic dump changes.

Quoted 6 lines

Actually I was mistaken as to how the original dump worked. I'd thought that it actually kept track of which bucket it was in and resumed from that bucket. However in reality it only had a global counter and would always start walking from the beginning up until the counted value. So it isn't as easy as just copying the old code across :)

Only AF_KEY gave an error, and this ended the dump. This was one of Timo's goals, to make AF_KEY continue where it left off in subsequent dump calls done by the user, when we hit the socket limit.

Quoted 2 lines

While it is possible to restore the old order and save a little bit of memory, I certainly don't think this is very urgent.

Too late, I already implemented it :-)

I'm about to give the following patch a test:

ipsec: Restore hash based xfrm_state dumping.

Get rid of ->all member of struct xfrm_state, and just use a hash iteration like we used before.

This shrinks the size of struct xfrm_state, and also restores the dump ordering of 2.6.25 and previous.

include/net/xfrm.h +3 -2
index 4bb9499..ba0e47c 100644
@@ -120,7 +120,6 @@ extern struct mutex xfrm_cfg_mutex;
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	struct list_head	all;
 	union {
 		struct list_head	gclist;
 		struct hlist_node	bydst;
@@ -1247,6 +1246,7 @@ struct xfrm6_tunnel {
 
 struct xfrm_state_walk {
 	struct xfrm_state *state;
+	unsigned int chain;
 	int count;
 	u8 proto;
 };
@@ -1283,9 +1283,10 @@ extern int xfrm_proc_init(void);
 
 static inline void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
 {
-	walk->proto = proto;
 	walk->state = NULL;
+	walk->chain = 0;
 	walk->count = 0;
+	walk->proto = proto;
 }
 
 extern int xfrm_state_walk(struct xfrm_state_walk *walk,
net/xfrm/xfrm_state.c +22 -24

96 lines not on this page · raw message

maintainer
#85 lore raw
Quoted 5 lines · David Miller

Only AF_KEY gave an error, and this ended the dump. This was one of Timo's goals, to make AF_KEY continue where it left off in subsequent dump calls done by the user, when we hit the socket limit.

I see.

Quoted 4 lines
Quoted 2 lines

While it is possible to restore the old order and save a little bit of memory, I certainly don't think this is very urgent.

Too late, I already implemented it :-)

Awesome :) Your patch is much simpler than I thought.

include/net/xfrm.h extern struct mutex xfrm_cfg_mutex; · line 125

Quoted 12 lines
include/net/xfrm.h +0 -1
index 4bb9499..ba0e47c 100644
@@ -120,7 +120,6 @@ extern struct mutex xfrm_cfg_mutex;
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	struct list_head	all;
 	union {
 		struct list_head	gclist;
 		struct hlist_node	bydst;

This union now needs to move across to bysrc or byspi since bydst needs to be preserved for walking even after a node is added to the GC.

include/net/xfrm.h int __xfrm_state_delete(struct xfrm_state *x) · line 567

Quoted 6 lines
include/net/xfrm.h +0 -1
@@ -566,7 +564,6 @@ int __xfrm_state_delete(struct xfrm_state *x)
 		x->km.state = XFRM_STATE_DEAD;
 		spin_lock(&xfrm_state_lock);
 		x->lastused = xfrm_state_walk_ongoing;
-		list_del_rcu(&x->all);
 		hlist_del(&x->bydst);

We need hlist_del_rcu on bydst for the same reason.

Quoted 20 lines
include/net/xfrm.h +2 -14
@@ -1571,24 +1564,29 @@ int xfrm_state_walk(struct xfrm_state_walk *walk,
 	old = x = walk->state;
 	walk->state = NULL;
 	spin_lock_bh(&xfrm_state_lock);
-	if (x == NULL)
-		x = list_first_entry(&xfrm_state_all, struct xfrm_state, all);
-	list_for_each_entry_from(x, &xfrm_state_all, all) {
-		if (x->km.state == XFRM_STATE_DEAD)
-			continue;
-		if (!xfrm_id_proto_match(x->id.proto, walk->proto))
-			continue;
-		if (last) {
-			err = func(last, walk->count, data);
-			if (err) {
-				xfrm_state_hold(last);
-				walk->state = last;
-				xfrm_state_walk_ongoing++;
-				goto out;
+	for (i = walk->chain; i <= xfrm_state_hmask; i++) {
+		struct hlist_head *h = xfrm_state_bydst + i;

include/net/xfrm.h int xfrm_state_walk(struct xfrm_state_walk *walk, · line 1578

Quoted 10 lines
include/net/xfrm.h +10 -0
+		struct hlist_node *entry;
+
+		if (!x)
+			x = hlist_entry(h->first, struct xfrm_state, bydst);
+		hlist_for_each_entry_from(x, entry, bydst) {
+			if (!xfrm_id_proto_match(x->id.proto, walk->proto))
+				continue;
+			if (last) {
+				err = func(last, walk->count, data);
+				if (err) {

This is a bug in the original patch. When we get an error on the very last node we'll lose that node instead of dumping it later. I wonder if we could just get rid of last altogether...

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#86 lore raw

From: Herbert Xu <[email protected]>
Date: Wed, 10 Sep 2008 14:22:47 +1000

Quoted 5 lines · David Miller
Quoted 4 lines
include/net/xfrm.h +0 -0
@@ -120,7 +120,6 @@ extern struct mutex xfrm_cfg_mutex;
 /* Full description of state of transformer. */
 struct xfrm_state
 {
Quoted 8 lines

include/net/xfrm.h extern struct mutex xfrm_cfg_mutex; · line 125

Quoted 4 lines
include/net/xfrm.h +0 -1
-	struct list_head	all;
 	union {
 		struct list_head	gclist;
 		struct hlist_node	bydst;

This union now needs to move across to bysrc or byspi since bydst needs to be preserved for walking even after a node is added to the GC.

That would explain the crash I just got :)

Quoted 8 lines

net/xfrm/xfrm_state.c int __xfrm_state_delete(struct xfrm_state *x) · line 567

Quoted 6 lines
net/xfrm/xfrm_state.c +0 -1
@@ -566,7 +564,6 @@ int __xfrm_state_delete(struct xfrm_state *x)
 		x->km.state = XFRM_STATE_DEAD;
 		spin_lock(&xfrm_state_lock);
 		x->lastused = xfrm_state_walk_ongoing;
-		list_del_rcu(&x->all);
 		hlist_del(&x->bydst);

We need hlist_del_rcu on bydst for the same reason.

Ok, gotcha.

Quoted 3 lines

This is a bug in the original patch. When we get an error on the very last node we'll lose that node instead of dumping it later. I wonder if we could just get rid of last altogether...

I'll take a look at this too after I resolve and test the above issues.

Thanks.

author maintainer
#87 lore raw

From: David Miller <[email protected]>
Date: Tue, 09 Sep 2008 21:24:26 -0700 (PDT)

Quoted 17 lines

From: Herbert Xu <[email protected]>
Date: Wed, 10 Sep 2008 14:22:47 +1000

Quoted 5 lines · David Miller
Quoted 4 lines
include/net/xfrm.h +0 -0
@@ -120,7 +120,6 @@ extern struct mutex xfrm_cfg_mutex;
 /* Full description of state of transformer. */
 struct xfrm_state
 {
Quoted 8 lines

include/net/xfrm.h extern struct mutex xfrm_cfg_mutex; · line 125

Quoted 4 lines
include/net/xfrm.h +0 -1
-	struct list_head	all;
 	union {
 		struct list_head	gclist;
 		struct hlist_node	bydst;

This union now needs to move across to bysrc or byspi since bydst needs to be preserved for walking even after a node is added to the GC.

Quoted 1 line

That would explain the crash I just got :)

No, it doesn't explain the crash/hang. And it happens even without my patch.

I get a hang in del_timer_sync() and it's from a workqueue so I suspect your patch :-)

I can't see what does the list delete from the GC leftover list? Entries seem to stay on there forever. We even kfree() the object.

The list iteration is:

	list_for_each_entry_safe(x, tmp, &xfrm_state_gc_leftovers, gclist) {
		if ((long)(x->lastused - completed) > 0)
			break;
		xfrm_state_gc_destroy(x);
	}

And xfrm_state_gc_destroy() doesn't do a list_del(&x->gclist) or similar.

author maintainer
#88 lore raw

From: David Miller <[email protected]>
Date: Tue, 09 Sep 2008 21:48:29 -0700 (PDT)

Quoted 9 lines

The list iteration is:

	list_for_each_entry_safe(x, tmp, &xfrm_state_gc_leftovers, gclist) {
		if ((long)(x->lastused - completed) > 0)
			break;
		xfrm_state_gc_destroy(x);
	}

And xfrm_state_gc_destroy() doesn't do a list_del(&x->gclist) or similar.

I'm now testing the following fix:

net/xfrm/xfrm_state.c +1 -0
index aaafcee..abbe270 100644
@@ -423,6 +423,7 @@ static void xfrm_state_gc_task(struct work_struct *data)
 	list_for_each_entry_safe(x, tmp, &xfrm_state_gc_leftovers, gclist) {
 		if ((long)(x->lastused - completed) > 0)
 			break;
+		list_del(&x->gclist);
 		xfrm_state_gc_destroy(x);
 	}
 
maintainer
#89 lore raw
In reply to David Miller
Quoted 3 lines · David Miller

I can't see what does the list delete from the GC leftover list? Entries seem to stay on there forever. We even kfree() the object.

Doh! Yes there's supposed to be a list_del in there.

Here's a fixed patch:

ipsec: Use RCU-like construct for saved state within a walk

Now that we save states within a walk we need synchronisation so that the list the saved state is on doesn't disappear from under us.

As it stands this is done by keeping the state on the list which is bad because it gets in the way of the management of the state life-cycle.

An alternative is to make our own pseudo-RCU system where we use counters to indicate which state can't be freed immediately as it may be referenced by an ongoing walk when that resumes.

include/net/xfrm.h +5 -10
index 2933d74..4bb9499 100644
@@ -120,9 +120,11 @@ extern struct mutex xfrm_cfg_mutex;
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	/* Note: bydst is re-used during gc */
 	struct list_head	all;
-	struct hlist_node	bydst;
+	union {
+		struct list_head	gclist;
+		struct hlist_node	bydst;
+	};
 	struct hlist_node	bysrc;
 	struct hlist_node	byspi;
 
@@ -1286,16 +1288,9 @@ static inline void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
 	walk->count = 0;
 }
 
-static inline void xfrm_state_walk_done(struct xfrm_state_walk *walk)
-{
-	if (walk->state != NULL) {
-		xfrm_state_put(walk->state);
-		walk->state = NULL;
-	}
-}
-
 extern int xfrm_state_walk(struct xfrm_state_walk *walk,
 			   int (*func)(struct xfrm_state *, int, void*), void *);
+extern void xfrm_state_walk_done(struct xfrm_state_walk *walk);
 extern struct xfrm_state *xfrm_state_alloc(void);
 extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, 
 					  struct flowi *fl, struct xfrm_tmpl *tmpl,
net/xfrm/xfrm_state.c +40 -13

106 lines not on this page · raw message

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#91 lore raw
In reply to David Miller
Quoted 14 lines · David Miller

From: Herbert Xu <[email protected]>
Date: Wed, 10 Sep 2008 14:01:07 +1000

Quoted 10 lines · David Miller
Quoted 3 lines

No problem. It might be a little bit of a chore because this new walker design is intimately tied to the af_key non-atomic dump changes.

Actually I was mistaken as to how the original dump worked. I'd thought that it actually kept track of which bucket it was in and resumed from that bucket. However in reality it only had a global counter and would always start walking from the beginning up until the counted value. So it isn't as easy as just copying the old code across :)

Quoted 4 lines

Only AF_KEY gave an error, and this ended the dump. This was one of Timo's goals, to make AF_KEY continue where it left off in subsequent dump calls done by the user, when we hit the socket limit.

Yes, this was the other goal. The other goal was to speed up dumping in netlink from O(n^2) to O(n). And fixing the problem that netlink might miss to dump some entries (if an entry was removed from the beginning of the hash when dumping was ongoing).

Quoted 7 lines

ipsec: Restore hash based xfrm_state dumping.

Get rid of ->all member of struct xfrm_state, and just use a hash iteration like we used before.

This shrinks the size of struct xfrm_state, and also restores the dump ordering of 2.6.25 and previous.

I think bad things will happen if the hash gets resized between xfrm_state_walk() calls. Since it assumes that the walk->state entry has remained in walk->chain bucket.

The dumping order shouldn't really make any difference. But reducing struct xfrm_state is definitely good. The only downside is that when an entry is inserted to the beginning of the hash while dump is ongoing, it won't be dumped at all. But that is not really a problem since you get a notification about that entry separately.

Cheers,
Timo

author maintainer
#90 lore raw
In reply to Herbert Xu

From: Herbert Xu <[email protected]>
Date: Wed, 10 Sep 2008 14:53:50 +1000

Quoted 6 lines · David Miller
Quoted 3 lines

I can't see what does the list delete from the GC leftover list? Entries seem to stay on there forever. We even kfree() the object.

Doh! Yes there's supposed to be a list_del in there.

Your original change was already in net-next-2.6 so I commited the one-liner fix.

Thanks.

author maintainer
#92 lore raw
In reply to Timo Teräs

From: Timo Teräs <[email protected]>
Date: Wed, 10 Sep 2008 08:16:12 +0300

Quoted 13 lines
Quoted 7 lines · David Miller

ipsec: Restore hash based xfrm_state dumping.

Get rid of ->all member of struct xfrm_state, and just use a hash iteration like we used before.

This shrinks the size of struct xfrm_state, and also restores the dump ordering of 2.6.25 and previous.

I think bad things will happen if the hash gets resized between xfrm_state_walk() calls. Since it assumes that the walk->state entry has remained in walk->chain bucket.

Quoted 6 lines

The dumping order shouldn't really make any difference. But reducing struct xfrm_state is definitely good. The only downside is that when an entry is inserted to the beginning of the hash while dump is ongoing, it won't be dumped at all. But that is not really a problem since you get a notification about that entry separately.

That's a good point, the hash resizing case.

If the hash grows, we'll see some entries again.

If it shrinks, we might skip some instead, which is very undesirable.

Actually, thinking about it some more, both possibilities (skipping and dups) seem to be possible in both situations (growing and shrinking).

Ehm... I think we should put this patch and idea aside for a little bit. :-)

maintainer
#93 lore raw
Quoted 5 lines · David Miller

That's a good point, the hash resizing case.

If the hash grows, we'll see some entries again.

Quoted 8 lines

If it shrinks, we might skip some instead, which is very undesirable.

Actually, thinking about it some more, both possibilities (skipping and dups) seem to be possible in both situations (growing and shrinking).

Ehm... I think we should put this patch and idea aside for a little bit. :-)

Yeah the hash resizing thing really kills this approach. In fact it seems that the original walker was written before resizing was added.

I definitely agree that ordering isn't as important as showing the entries reliably.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#25 lore raw
In reply to Herbert Xu
Quoted 17 lines · Herbert Xu
Quoted 3 lines · David Miller

The only comment I would make is that maybe it's a bit excessive to trigger the GC worker every time we walk the states.

Good point!

I've avoided the memory barrier by simply extending the mutexed section in the GC to cover the list splicing. Here's the updated patch:

ipsec: Use RCU-like construct for saved state within a walk

Now that we save states within a walk we need synchronisation so that the list the saved state is on doesn't disappear from under us.

Quoted 7 lines

As it stands this is done by keeping the state on the list which is bad because it gets in the way of the management of the state life-cycle.

An alternative is to make our own pseudo-RCU system where we use counters to indicate which state can't be freed immediately as it may be referenced by an ongoing walk when that resumes.

There is only one reader at a time, right? Otherwise, I don't see how the increments and reads of xfrm_state_walk_completed line up.

Thanx, Paul

Quoted 165 lines
include/net/xfrm.h +5 -10
index 2933d74..4bb9499 100644
@@ -120,9 +120,11 @@ extern struct mutex xfrm_cfg_mutex;
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	/* Note: bydst is re-used during gc */
 	struct list_head	all;
-	struct hlist_node	bydst;
+	union {
+		struct list_head	gclist;
+		struct hlist_node	bydst;
+	};
 	struct hlist_node	bysrc;
 	struct hlist_node	byspi;
 
@@ -1286,16 +1288,9 @@ static inline void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
 	walk->count = 0;
 }
 
-static inline void xfrm_state_walk_done(struct xfrm_state_walk *walk)
-{
-	if (walk->state != NULL) {
-		xfrm_state_put(walk->state);
-		walk->state = NULL;
-	}
-}
-
 extern int xfrm_state_walk(struct xfrm_state_walk *walk,
 			   int (*func)(struct xfrm_state *, int, void*), void *);
+extern void xfrm_state_walk_done(struct xfrm_state_walk *walk);
 extern struct xfrm_state *xfrm_state_alloc(void);
 extern struct xfrm_state *xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, 
 					  struct flowi *fl, struct xfrm_tmpl *tmpl,
net/xfrm/xfrm_state.c +39 -13
index 7bd62f6..d90f936 100644
@@ -59,6 +59,11 @@ static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
 static unsigned int xfrm_state_num;
 static unsigned int xfrm_state_genid;
 
+/* Counter indicating ongoing walk, protected by xfrm_state_lock. */
+static unsigned long xfrm_state_walk_ongoing;
+/* Counter indicating walk completion, protected by xfrm_cfg_mutex. */
+static unsigned long xfrm_state_walk_completed;
+
 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
 
@@ -191,7 +196,8 @@ static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
 
 static struct work_struct xfrm_state_gc_work;
-static HLIST_HEAD(xfrm_state_gc_list);
+static LIST_HEAD(xfrm_state_gc_leftovers);
+static LIST_HEAD(xfrm_state_gc_list);
 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
 
 int __xfrm_state_delete(struct xfrm_state *x);
@@ -403,17 +409,22 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)
 
 static void xfrm_state_gc_task(struct work_struct *data)
 {
-	struct xfrm_state *x;
-	struct hlist_node *entry, *tmp;
-	struct hlist_head gc_list;
+	struct xfrm_state *x, *tmp;
+	unsigned long completed;
 
+	mutex_lock(&xfrm_cfg_mutex);
 	spin_lock_bh(&xfrm_state_gc_lock);
-	gc_list.first = xfrm_state_gc_list.first;
-	INIT_HLIST_HEAD(&xfrm_state_gc_list);
+	list_splice_tail_init(&xfrm_state_gc_list, &xfrm_state_gc_leftovers);
 	spin_unlock_bh(&xfrm_state_gc_lock);
 
-	hlist_for_each_entry_safe(x, entry, tmp, &gc_list, bydst)
+	completed = xfrm_state_walk_completed;
+	mutex_unlock(&xfrm_cfg_mutex);
+
+	list_for_each_entry_safe(x, tmp, &xfrm_state_gc_leftovers, gclist) {
+		if ((long)(x->lastused - completed) > 0)
+			break;
 		xfrm_state_gc_destroy(x);
+	}
 
 	wake_up(&km_waitq);
 }
@@ -540,12 +551,8 @@ void __xfrm_state_destroy(struct xfrm_state *x)
 {
 	WARN_ON(x->km.state != XFRM_STATE_DEAD);
 
-	spin_lock_bh(&xfrm_state_lock);
-	list_del(&x->all);
-	spin_unlock_bh(&xfrm_state_lock);
-
 	spin_lock_bh(&xfrm_state_gc_lock);
-	hlist_add_head(&x->bydst, &xfrm_state_gc_list);
+	list_add_tail(&x->gclist, &xfrm_state_gc_list);
 	spin_unlock_bh(&xfrm_state_gc_lock);
 	schedule_work(&xfrm_state_gc_work);
 }
@@ -558,6 +565,8 @@ int __xfrm_state_delete(struct xfrm_state *x)
 	if (x->km.state != XFRM_STATE_DEAD) {
 		x->km.state = XFRM_STATE_DEAD;
 		spin_lock(&xfrm_state_lock);
+		x->lastused = xfrm_state_walk_ongoing;
+		list_del_rcu(&x->all);
 		hlist_del(&x->bydst);
 		hlist_del(&x->bysrc);
 		if (x->id.spi)
@@ -1572,6 +1581,7 @@ int xfrm_state_walk(struct xfrm_state_walk *walk,
 			if (err) {
 				xfrm_state_hold(last);
 				walk->state = last;
+				xfrm_state_walk_ongoing++;
 				goto out;
 			}
 		}
@@ -1586,12 +1596,28 @@ int xfrm_state_walk(struct xfrm_state_walk *walk,
 		err = func(last, 0, data);
 out:
 	spin_unlock_bh(&xfrm_state_lock);
-	if (old != NULL)
+	if (old != NULL) {
 		xfrm_state_put(old);
+		xfrm_state_walk_completed++;
+		if (!list_empty(&xfrm_state_gc_leftovers))
+			schedule_work(&xfrm_state_gc_work);
+	}
 	return err;
 }
 EXPORT_SYMBOL(xfrm_state_walk);
 
+void xfrm_state_walk_done(struct xfrm_state_walk *walk)
+{
+	if (walk->state != NULL) {
+		xfrm_state_put(walk->state);
+		walk->state = NULL;
+		xfrm_state_walk_completed++;
+		if (!list_empty(&xfrm_state_gc_leftovers))
+			schedule_work(&xfrm_state_gc_work);
+	}
+}
+EXPORT_SYMBOL(xfrm_state_walk_done);
+
 
 void xfrm_replay_notify(struct xfrm_state *x, int event)
 {

Thanks,

Signature · 8 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#26 lore raw

From: "Paul E. McKenney" <[email protected]>
Date: Thu, 11 Sep 2008 14:24:59 -0700

Quoted 22 lines · Herbert Xu
Quoted 21 lines · David Miller
Quoted 3 lines

The only comment I would make is that maybe it's a bit excessive to trigger the GC worker every time we walk the states.

Good point!

I've avoided the memory barrier by simply extending the mutexed section in the GC to cover the list splicing. Here's the updated patch:

ipsec: Use RCU-like construct for saved state within a walk

Now that we save states within a walk we need synchronisation so that the list the saved state is on doesn't disappear from under us.

As it stands this is done by keeping the state on the list which is bad because it gets in the way of the management of the state life-cycle.

Quoted 6 lines
Quoted 3 lines

An alternative is to make our own pseudo-RCU system where we use counters to indicate which state can't be freed immediately as it may be referenced by an ongoing walk when that resumes.

There is only one reader at a time, right? Otherwise, I don't see how the increments and reads of xfrm_state_walk_completed line up.

The RTNL semaphore is held across the modifications of the counters.

#27 lore raw
Quoted 25 lines · David Miller

From: "Paul E. McKenney" <[email protected]>
Date: Thu, 11 Sep 2008 14:24:59 -0700

Quoted 22 lines · Herbert Xu
Quoted 21 lines · David Miller
Quoted 3 lines

The only comment I would make is that maybe it's a bit excessive to trigger the GC worker every time we walk the states.

Good point!

I've avoided the memory barrier by simply extending the mutexed section in the GC to cover the list splicing. Here's the updated patch:

ipsec: Use RCU-like construct for saved state within a walk

Now that we save states within a walk we need synchronisation so that the list the saved state is on doesn't disappear from under us.

As it stands this is done by keeping the state on the list which is bad because it gets in the way of the management of the state life-cycle.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

An alternative is to make our own pseudo-RCU system where we use counters to indicate which state can't be freed immediately as it may be referenced by an ongoing walk when that resumes.

There is only one reader at a time, right? Otherwise, I don't see how the increments and reads of xfrm_state_walk_completed line up.

The RTNL semaphore is held across the modifications of the counters.

Got it, thank you, sorry for the noise!

Thanx, Paul

maintainer
#28 lore raw
In reply to David Miller
Quoted 5 lines · David Miller
Quoted 3 lines

There is only one reader at a time, right? Otherwise, I don't see how the increments and reads of xfrm_state_walk_completed line up.

The RTNL semaphore is held across the modifications of the counters.

s/RTNL/xfrm_cfg_mutex/

but otherwise what Dave said :)

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#29 lore raw
Quoted 10 lines · Herbert Xu
Quoted 5 lines · David Miller
Quoted 3 lines

There is only one reader at a time, right? Otherwise, I don't see how the increments and reads of xfrm_state_walk_completed line up.

The RTNL semaphore is held across the modifications of the counters.

s/RTNL/xfrm_cfg_mutex/

but otherwise what Dave said :)

Thank you for the updated information, and I guess I don't feel quite so bad about the false alarm. ;-)

Thanx, Paul

#30 lore raw
In reply to Herbert Xu
Quoted 11 lines · Herbert Xu

On Mon, Sep 08, 2008 at 05:25:13PM -0700, David Miller wrote:
I've avoided the memory barrier by simply extending the mutexed section in the GC to cover the list splicing. Here's the updated patch:

ipsec: Use RCU-like construct for saved state within a walk

Now that we save states within a walk we need synchronisation so that the list the saved state is on doesn't disappear from under us.

Quoted 7 lines

As it stands this is done by keeping the state on the list which is bad because it gets in the way of the management of the state life-cycle.

An alternative is to make our own pseudo-RCU system where we use counters to indicate which state can't be freed immediately as it may be referenced by an ongoing walk when that resumes.

Does not this logic fail if:
1. completed = ongoing
2. 1st walk started and iterator kept (a->lastused = ongoing, ongoing++)
3. 2nd walk started and iterator kept (b->lastused = ongoing, ongoing++)
4. 2nd walk finished (completed++)
5. gc triggered: a gets deleted since a->lastused == completed
6. 1st walk continued but freed memory accessed as a was deleted

Though currently it does not affect, since xfrm_state_hold/_put are still called when keeping the iterator, so the entries won't actually get garbage collected anyway. So the completed/ongoing counting is basically useless. Or am I missing something?

Wouldn't it be enough to do the list_del_rcu on delete? And just keep reference as previously?

- Timo

#31 lore raw
Quoted 28 lines · Timo Teräs
Quoted 11 lines · Herbert Xu

On Mon, Sep 08, 2008 at 05:25:13PM -0700, David Miller wrote:
I've avoided the memory barrier by simply extending the mutexed section in the GC to cover the list splicing. Here's the updated patch:

ipsec: Use RCU-like construct for saved state within a walk

Now that we save states within a walk we need synchronisation so that the list the saved state is on doesn't disappear from under us.

Quoted 7 lines

As it stands this is done by keeping the state on the list which is bad because it gets in the way of the management of the state life-cycle.

An alternative is to make our own pseudo-RCU system where we use counters to indicate which state can't be freed immediately as it may be referenced by an ongoing walk when that resumes.

Does not this logic fail if:
1. completed = ongoing
2. 1st walk started and iterator kept (a->lastused = ongoing, ongoing++)
3. 2nd walk started and iterator kept (b->lastused = ongoing, ongoing++)
4. 2nd walk finished (completed++)
5. gc triggered: a gets deleted since a->lastused == completed
6. 1st walk continued but freed memory accessed as a was deleted

Quoted 4 lines

Though currently it does not affect, since xfrm_state_hold/_put are still called when keeping the iterator, so the entries won't actually get garbage collected anyway. So the completed/ongoing counting is basically useless. Or am I missing something?

Obviously I missed the fact that it prevents deletion of the entries which the iterator held list node might still point to.

But the flaw still exists: the entries which interator->next points can be deleted if there is a walking that takes a long time and meanwhile we get walks that complete fast.

Quoted 2 lines

Wouldn't it be enough to do the list_del_rcu on delete? And just keep reference as previously?

So if we do list_del_rcu() on delete, could we also xfrm_state_hold() the entry pointed to by that list entry. And then on GC we could xfrm_state_put() the next entry.

- Timo

maintainer
#32 lore raw
Quoted 4 lines · Timo Teräs

But the flaw still exists: the entries which interator->next points can be deleted if there is a walking that takes a long time and meanwhile we get walks that complete fast.

Thanks, I guess that's why I'm not paid to work on RCU :)

Quoted 3 lines

So if we do list_del_rcu() on delete, could we also xfrm_state_hold() the entry pointed to by that list entry. And then on GC we could xfrm_state_put() the next entry.

Unfortunately it's not that simple since we'll be in the same bind if the entry after the next entry gets deleted as well as the next entry.

The only other solution is to go back to the original scheme where we keep the list intact until GC. However, nobody has come up with a way of doing that that allows the SA creation path to run locklessly with respect to the dumping path.

So I think we'll have to stick with the quasi-RCU solution for now. Here's a patch to fix the flaw that you discovered.

ipsec: Fix xfrm_state_walk race

As discovered by Timo Teräs, the currently xfrm_state_walk scheme is racy because if a second dump finishes before the first, we may free xfrm states that the first dump would walk over later.

This patch fixes this by storing the dumps in a list in order to calculate the correct completion counter which cures this problem.

I've expanded netlink_cb in order to accomodate the extra state related to this. It shouldn't be a big deal since netlink_cb is kmalloced for each dump and we're just increasing it by 4 or 8 bytes.

include/linux/netlink.h +1 -1
index 9ff1b54..cbba776 100644
@@ -220,7 +220,7 @@ struct netlink_callback
 	int		(*dump)(struct sk_buff * skb, struct netlink_callback *cb);
 	int		(*done)(struct netlink_callback *cb);
 	int		family;
-	long		args[6];
+	long		args[7];
 };
 
 struct netlink_notify
include/net/xfrm.h +3 -7
index 4bb9499..48630b2 100644
@@ -1246,6 +1246,8 @@ struct xfrm6_tunnel {
 };
 
 struct xfrm_state_walk {
+	struct list_head list;
+	unsigned long genid;
 	struct xfrm_state *state;
 	int count;
 	u8 proto;
@@ -1281,13 +1283,7 @@ static inline void xfrm6_fini(void)
 extern int xfrm_proc_init(void);
 #endif
 
-static inline void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
-{
-	walk->proto = proto;
-	walk->state = NULL;
-	walk->count = 0;
-}
-
+extern void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto);
 extern int xfrm_state_walk(struct xfrm_state_walk *walk,
 			   int (*func)(struct xfrm_state *, int, void*), void *);
 extern void xfrm_state_walk_done(struct xfrm_state_walk *walk);
net/xfrm/xfrm_state.c +30 -9
index fe84a46..f7805c5 100644
@@ -64,6 +64,9 @@ static unsigned long xfrm_state_walk_ongoing;
 /* Counter indicating walk completion, protected by xfrm_cfg_mutex. */
 static unsigned long xfrm_state_walk_completed;
 
+/* List of outstanding state walks used to set the completed counter.  */
+static LIST_HEAD(xfrm_state_walks);
+
 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
 
@@ -1582,7 +1585,6 @@ int xfrm_state_walk(struct xfrm_state_walk *walk,
 			if (err) {
 				xfrm_state_hold(last);
 				walk->state = last;
-				xfrm_state_walk_ongoing++;
 				goto out;
 			}
 		}
@@ -1597,25 +1599,44 @@ int xfrm_state_walk(struct xfrm_state_walk *walk,
 		err = func(last, 0, data);
 out:
 	spin_unlock_bh(&xfrm_state_lock);
-	if (old != NULL) {
+	if (old != NULL)
 		xfrm_state_put(old);
-		xfrm_state_walk_completed++;
-		if (!list_empty(&xfrm_state_gc_leftovers))
-			schedule_work(&xfrm_state_gc_work);
-	}
 	return err;
 }
 EXPORT_SYMBOL(xfrm_state_walk);
 
+void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
+{
+	walk->proto = proto;
+	walk->state = NULL;
+	walk->count = 0;
+	list_add_tail(&walk->list, &xfrm_state_walks);
+	walk->genid = ++xfrm_state_walk_ongoing;
+}
+EXPORT_SYMBOL(xfrm_state_walk_init);
+
 void xfrm_state_walk_done(struct xfrm_state_walk *walk)
 {
+	struct list_head *prev;
+
 	if (walk->state != NULL) {
 		xfrm_state_put(walk->state);
 		walk->state = NULL;
-		xfrm_state_walk_completed++;
-		if (!list_empty(&xfrm_state_gc_leftovers))
-			schedule_work(&xfrm_state_gc_work);
 	}
+
+	prev = walk->list.prev;
+	list_del(&walk->list);
+
+	if (prev != &xfrm_state_walks) {
+		list_entry(prev, struct xfrm_state_walk, list)->genid =
+			walk->genid;
+		return;
+	}
+
+	xfrm_state_walk_completed = walk->genid;
+
+	if (!list_empty(&xfrm_state_gc_leftovers))
+		schedule_work(&xfrm_state_gc_work);
 }
 EXPORT_SYMBOL(xfrm_state_walk_done);
 

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#33 lore raw
Quoted 8 lines · Herbert Xu
Quoted 3 lines · Timo Teräs

So if we do list_del_rcu() on delete, could we also xfrm_state_hold() the entry pointed to by that list entry. And then on GC we could xfrm_state_put() the next entry.

Unfortunately it's not that simple since we'll be in the same bind if the entry after the next entry gets deleted as well as the next entry.

Well, I was thinking that we hold the next pointer. And when continuing the dump, we can first skip all entries that are marked as dead (each next pointer is valid since each of the next pointers are held once). When we find the first valid entry to dump we _put() the originally held entry. That would recursively _put() all the next entries which were held.

Quoted 7 lines

The only other solution is to go back to the original scheme where we keep the list intact until GC. However, nobody has come up with a way of doing that that allows the SA creation path to run locklessly with respect to the dumping path.

So I think we'll have to stick with the quasi-RCU solution for now. Here's a patch to fix the flaw that you discovered.

But yes, this would work as well.

Not sure which one would be faster. I guess the holding of individual entries would be at least more memory efficient.

Cheers,
Timo

maintainer
#34 lore raw
Quoted 11 lines · Timo Teräs
Quoted 4 lines

Unfortunately it's not that simple since we'll be in the same bind if the entry after the next entry gets deleted as well as the next entry.

Well, I was thinking that we hold the next pointer. And when continuing the dump, we can first skip all entries that are marked as dead (each next pointer is valid since each of the next pointers are held once). When we find the first valid entry to dump we _put() the originally held entry. That would recursively _put() all the next entries which were held.

No that doesn't work. Let's say we store the entry X in walk->state, and we hold X as well as X->next. Now X, X->next, and X->next->next get deleted from the list. What'll happen is that X and X->next will stick around but X->next->next will be freed. So when we resume from X we'll dump X and X->next correctly, but then hit X->next->next and be in the same shithole.

Really, the only way to do what you want here is to hold everything from X to the very end of the list. The problem with that is that you can no longer drop the reference counts when we resume.

You can see why even if we only hold X and X->next. Should X->next be deleted from the list, when we resume from X we'll no longer be able to drop the reference count on the original X->next since it now points to a new entry.

Quoted 2 lines

Not sure which one would be faster. I guess the holding of individual entries would be at least more memory efficient.

I think this is a lot more efficient than storing every single entry from X to the end of the list :)

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#77 lore raw
In reply to Herbert Xu

From: Herbert Xu <[email protected]>
Date: Mon, 22 Sep 2008 19:42:56 +0800

Quoted 10 lines

ipsec: Fix xfrm_state_walk race

As discovered by Timo Teräs, the currently xfrm_state_walk scheme is racy because if a second dump finishes before the first, we may free xfrm states that the first dump would walk over later.

This patch fixes this by storing the dumps in a list in order to calculate the correct completion counter which cures this problem.

Quoted 6 lines

I've expanded netlink_cb in order to accomodate the extra state related to this. It shouldn't be a big deal since netlink_cb is kmalloced for each dump and we're just increasing it by 4 or 8 bytes.

Looks good, applied to net-next-2.6

#35 lore raw
In reply to Herbert Xu
Quoted 11 lines · Herbert Xu
Quoted 9 lines · Timo Teräs
Quoted 3 lines

Unfortunately it's not that simple since we'll be in the same bind if the entry after the next entry gets deleted as well as the next entry.

Well, I was thinking that we hold the next pointer. And when continuing the dump, we can first skip all entries that are marked as dead (each next pointer is valid since each of the next pointers are held once). When we find the first valid entry to dump we _put() the originally held entry. That would recursively _put() all the next entries which were held.

Quoted 6 lines

No that doesn't work. Let's say we store the entry X in walk->state, and we hold X as well as X->next. Now X, X->next, and X->next->next get deleted from the list. What'll happen is that X and X->next will stick around but X->next->next will be freed. So when we resume from X we'll dump X and X->next correctly, but then hit X->next->next and be in the same shithole.

I think it would work. Here's the scenarios:
We hold X as dumping is interrupted there. X->next points statically to some non-deleted entry and is held.

Now, if X->next gets deleted, it's marked dead and X->next->next is held too. Thus when there is multiple deleted entries in chain, the whole chain is held recursively/iteratively. When walking is continued on X the first thing we do is skip all dead entries from X, after that we put X and that would trigger put() for all X->next:s which were held iteratively.

If X->next is not deleted, and X->next->next gets deleted, the X->next list structure is updated correctly by list_del_rcu and the entry can be actually freed even if the walking didn't iterate that entry (it would be skipped anyway as it's marked dead on deletion).

So the idea was to hold X->next from deletion function, not from the walking function. That would be, we always hold deleted->next when there are ongoing walks. And on final _put() we _put() the ->next entry.

I think that would work.

- Timo

maintainer
#36 lore raw
Quoted 7 lines · Timo Teräs

So the idea was to hold X->next from deletion function, not from the walking function. That would be, we always hold deleted->next when there are ongoing walks. And on final _put() we _put() the ->next entry.

I think that would work.

Right, this would work.

However, now you're again slowing down the relatively fast path of state deletion by moving extra work there from the non-critical dump path.

When we optimise code for time, we don't necessarily try to minimise the work overall. We instead try to minimise the amount of work on the critical paths. It's OK to let the other paths such as dumping do a bit more work if it improves the fast paths.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#37 lore raw
Quoted 6 lines · Herbert Xu
Quoted 5 lines · Timo Teräs

So the idea was to hold X->next from deletion function, not from the walking function. That would be, we always hold deleted->next when there are ongoing walks. And on final _put() we _put() the ->next entry.

Quoted 7 lines
Quoted 1 line

I think that would work.

Right, this would work.

However, now you're again slowing down the relatively fast path of state deletion by moving extra work there from the non-critical dump path.

The extra step there wold be a hold call. The recursive _put on a _put of some entry can happen only on dump path. As otherwise the ->next entry is first held in state delete, but would be immediately _put on the _put as the final step of _delete().

So basically one additional atomic_inc() and one atomic_dec() on the normal _delete() path.

Quoted 4 lines

When we optimise code for time, we don't necessarily try to minimise the work overall. We instead try to minimise the amount of work on the critical paths. It's OK to let the other paths such as dumping do a bit more work if it improves the fast paths.

Not sure about the overall penalty, but yes I know it would have some penalty. But at least there would be no locking.

Particularly the thing that can happen on your approach is that if there is a user land process that gets suspended during a dump processing, it would prevent the garbage collection of all entries for all eternity until that process is continued or killed.

So this would allow deletion and GC of entries even during walking. But this was just a thought. Maybe it's not worth trying.

Cheers,
Timo

maintainer
#38 lore raw
Quoted 8 lines · Timo Teräs

The extra step there wold be a hold call. The recursive _put on a _put of some entry can happen only on dump path. As otherwise the ->next entry is first held in state delete, but would be immediately _put on the _put as the final step of _delete().

So basically one additional atomic_inc() and one atomic_dec() on the normal _delete() path.

Can you post a patch? If this can be done locklessly then yes it would probably be a good way to go. However, I'm not sure whether I understand your lockless proposal yet.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#39 lore raw
Quoted 6 lines · Herbert Xu
Quoted 5 lines · Timo Teräs

The extra step there wold be a hold call. The recursive _put on a _put of some entry can happen only on dump path. As otherwise the ->next entry is first held in state delete, but would be immediately _put on the _put as the final step of _delete().

Quoted 6 lines
Quoted 2 lines

So basically one additional atomic_inc() and one atomic_dec() on the normal _delete() path.

Can you post a patch? If this can be done locklessly then yes it would probably be a good way to go. However, I'm not sure whether I understand your lockless proposal yet.

Something like this. I just compile tested, so I'm not sure if it actually works.

This basically reverts the GC changes. The interesting bits are in xfrm_state_delete(). It will _hold() the ->next entry unless we were at last entry. This will make sure that when an entry is in XFRM_STATE_DEAD the all.next is valid all the way until the entry is destroyed.

The corresponding put is in _destroy(). Added it as a final thing to do so hopefully compiler optimizes tail recursion to iteration. (Seemed to do that in my case.)

And finally, _walk() was right all along. It returns to dumping and first skips all dead entries. And just before return, when all dead entries are already skipped the originally held entry is _put(). That _put call (or the one in _walk_done()) will result all dead entries that were held, to be iteratively put.

- Timo

include/linux/netlink.h +1 -1
index cbba776..9ff1b54 100644
@@ -220,7 +220,7 @@ struct netlink_callback
 	int		(*dump)(struct sk_buff * skb, struct netlink_callback *cb);
 	int		(*done)(struct netlink_callback *cb);
 	int		family;
-	long		args[7];
+	long		args[6];
 };
 
 struct netlink_notify
include/net/xfrm.h +1 -3
index 48630b2..d5cba7b 100644
@@ -122,7 +122,7 @@ struct xfrm_state
 {
 	struct list_head	all;
 	union {
-		struct list_head	gclist;
+		struct hlist_node	gclist;
 		struct hlist_node	bydst;
 	};
 	struct hlist_node	bysrc;
@@ -1246,8 +1246,6 @@ struct xfrm6_tunnel {
 };
 
 struct xfrm_state_walk {
-	struct list_head list;
-	unsigned long genid;
 	struct xfrm_state *state;
 	int count;
 	u8 proto;
net/xfrm/xfrm_state.c +18 -42

116 lines not on this page · raw message

maintainer
#40 lore raw
Quoted 3 lines · Timo Teräs

Something like this. I just compile tested, so I'm not sure if it actually works.

It is quite clever :)

Quoted 3 lines

The corresponding put is in _destroy(). Added it as a final thing to do so hopefully compiler optimizes tail recursion to iteration. (Seemed to do that in my case.)

Yeah we should open code this to be absolutely sure that we don't get killed by a stupid compiler.

However, I think this is still open to the same problem that my patch had, i.e., if a dumper goes to sleep during the dump we may prevent entries from being freed indefinitely. Yes your idea is better in that we may only withhold say a fraction (depending on the order of deletion it could be anywhere from none to every entry) of the entries instead of all of them, but fundamentally the same issue is still there.

Considering the fact that dumps require root privileges I'm not convinced as of yet that this is worth it.

Hmm, could we perhaps go back to your original scheme of keeping everything on the list and see if we can use RCU to make it lockless instead?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#41 lore raw
Quoted 5 lines · Herbert Xu
Quoted 2 lines · Timo Teräs

Something like this. I just compile tested, so I'm not sure if it actually works.

It is quite clever :)

Thanks :)

And actually, we can make _delete() do normal list_del() and not hold the next pointer if there are no running dumps. This is the common case anyway. Then there would be no penalty at all.

Quoted 6 lines
Quoted 3 lines

The corresponding put is in _destroy(). Added it as a final thing to do so hopefully compiler optimizes tail recursion to iteration. (Seemed to do that in my case.)

Yeah we should open code this to be absolutely sure that we don't get killed by a stupid compiler.

Right. The patch was just to show the idea. Will do a proper patch with this and the above mentioned optimization if you think we should change to this until we get really cool lockless dumping.

Quoted 10 lines

However, I think this is still open to the same problem that my patch had, i.e., if a dumper goes to sleep during the dump we may prevent entries from being freed indefinitely. Yes your idea is better in that we may only withhold say a fraction (depending on the order of deletion it could be anywhere from none to every entry) of the entries instead of all of them, but fundamentally the same issue is still there.

Considering the fact that dumps require root privileges I'm not convinced as of yet that this is worth it.

You are right. It can keep memory allocated. And in worst case you end up keeping all the deleted entries (entries are deleted in the order they appear in all list). But I would not be to worried about it either.

Quoted 3 lines

Hmm, could we perhaps go back to your original scheme of keeping everything on the list and see if we can use RCU to make it lockless instead?

That would be ideal of course.

- Timo

#42 lore raw
In reply to Herbert Xu
Quoted 8 lines · Herbert Xu

However, I think this is still open to the same problem that my patch had, i.e., if a dumper goes to sleep during the dump we may prevent entries from being freed indefinitely. Yes your idea is better in that we may only withhold say a fraction (depending on the order of deletion it could be anywhere from none to every entry) of the entries instead of all of them, but fundamentally the same issue is still there.

Quoted 6 lines

Considering the fact that dumps require root privileges I'm not convinced as of yet that this is worth it.

Hmm, could we perhaps go back to your original scheme of keeping everything on the list and see if we can use RCU to make it lockless instead?

How about this: we keep list of walks as your latest patch does. When walking is interrupted, we do not hold the entry, we just store the pointer to walk iterator. When the entry is deleted from the lists we go through the walk contexts, and if someone is pointing to the entry being deleted, we just update it to next.

The list of walkers can be protected by xfrm_state_lock. That needs to be taken anyway for accessing/modifying the other lists.

During most of the time, there is no walks active, so the penalty for _destroy() is minimal.

This would make it possibly to reclaim the deleted entries right away.

Does this sound better?

Cheers,
Timo

maintainer
#43 lore raw
Quoted 5 lines · Timo Teräs

This would make it possibly to reclaim the deleted entries right away.

Does this sound better?

Yep this sounds pretty good to me.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#44 lore raw
Quoted 9 lines · Herbert Xu
Quoted 4 lines · Timo Teräs

This would make it possibly to reclaim the deleted entries right away.

Does this sound better?

Yep this sounds pretty good to me.

Thanks,

So the patch would look something like this. Compile tested. Will test later when it becomes possible to reboot my box.

Cheers,
Timo

ipsec: Fix up xfrm_state_walk.state on node deletion

Now that we track xfrm_state_walks, it makes more sense to fix up the state pointer on deletion of the node if needed.

This allows accurately to delete all entries immediately, instead of possibly waiting for userland.

Also fixed locking of xfrm_state_walks list handling.

---
 include/linux/netlink.h |    2 +-
 include/net/xfrm.h      |    3 +-
 net/xfrm/xfrm_state.c   |   77 +++++++++++++++++-----------------------------
 3 files changed, 31 insertions(+), 51 deletions(-)
include/linux/netlink.h +1 -1
index cbba776..9ff1b54 100644
@@ -220,7 +220,7 @@ struct netlink_callback
 	int		(*dump)(struct sk_buff * skb, struct netlink_callback *cb);
 	int		(*done)(struct netlink_callback *cb);
 	int		family;
-	long		args[7];
+	long		args[6];
 };
 
 struct netlink_notify
include/net/xfrm.h +1 -2
index 48630b2..7f787c7 100644
@@ -122,7 +122,7 @@ struct xfrm_state
 {
 	struct list_head	all;
 	union {
-		struct list_head	gclist;
+		struct hlist_node	gclist;
 		struct hlist_node	bydst;
 	};
 	struct hlist_node	bysrc;
@@ -1247,7 +1247,6 @@ struct xfrm6_tunnel {
 
 struct xfrm_state_walk {
 	struct list_head list;
-	unsigned long genid;
 	struct xfrm_state *state;
 	int count;
 	u8 proto;
net/xfrm/xfrm_state.c +29 -48

154 lines not on this page · raw message

Signature · 1 line
1.5.4.3
maintainer
#45 lore raw

net/xfrm/xfrm_state.c EXPORT_SYMBOL(__xfrm_state_destroy); · line 568

Quoted 5 lines · Timo Teräs
net/xfrm/xfrm_state.c +4 -0
+		list_for_each_entry(walk, &xfrm_state_walks, list) {
+			if (walk->state == x)
+				walk->state = next;
+		}

Just make a new list in xfrm_state that has all the dumpers sitting on it. In fact all you need is a hlist, or even just a pointer.

Walking through an unbounded list on the fast path is not good :)

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#46 lore raw
Quoted 8 lines · Herbert Xu

net/xfrm/xfrm_state.c EXPORT_SYMBOL(__xfrm_state_destroy); · line 568

Quoted 4 lines · Timo Teräs
net/xfrm/xfrm_state.c +4 -0
+		list_for_each_entry(walk, &xfrm_state_walks, list) {
+			if (walk->state == x)
+				walk->state = next;
+		}

Just make a new list in xfrm_state that has all the dumpers sitting on it. In fact all you need is a hlist, or even just a pointer.

There can be a lot of xfrm_state structs. As in thousands. So it will take more memory then. hlist would be enough, so it'd be a 4/8 bytes addition. Single pointer would not be enough as we can have multiple walks on the same entry.

Quoted 1 line

Walking through an unbounded list on the fast path is not good :)

I was thinking about the same. That's why I wasn't so sure if this is better. In practice there aren't many walks active. Maybe we limit the maximum simultaneous walks?

But in real life, there is only a one or two walkers if any active. So I would not consider a global update too expensive. But if you think it might become a problem, I'd add an hlist to xfrm_state of walkers referencing that entry.

- Timo

maintainer
#47 lore raw
Quoted 5 lines · Timo Teräs

There can be a lot of xfrm_state structs. As in thousands. So it will take more memory then. hlist would be enough, so it'd be a 4/8 bytes addition. Single pointer would not be enough as we can have multiple walks on the same entry.

Right, we need doubly linked lists in the walkers to ease unlinking. But only a single pointer (i.e., an hlist) is needed in xfrm_state.

Quoted 3 lines

I was thinking about the same. That's why I wasn't so sure if this is better. In practice there aren't many walks active. Maybe we limit the maximum simultaneous walks?

That sounds like a hack :)

Quoted 4 lines

But in real life, there is only a one or two walkers if any active. So I would not consider a global update too expensive. But if you think it might become a problem, I'd add an hlist to xfrm_state of walkers referencing that entry.

Well in real life dumps don't tend to get suspended either :) In any case, a suspended dump is only going to pin down some memory, while a very long list walk may kill the box.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#48 lore raw
Quoted 8 lines · Herbert Xu
Quoted 4 lines · Timo Teräs

There can be a lot of xfrm_state structs. As in thousands. So it will take more memory then. hlist would be enough, so it'd be a 4/8 bytes addition. Single pointer would not be enough as we can have multiple walks on the same entry.

Right, we need doubly linked lists in the walkers to ease unlinking. But only a single pointer (i.e., an hlist) is needed in xfrm_state.

Right.

Quoted 5 lines
Quoted 3 lines

I was thinking about the same. That's why I wasn't so sure if this is better. In practice there aren't many walks active. Maybe we limit the maximum simultaneous walks?

That sounds like a hack :)

Yes :)

Quoted 8 lines
Quoted 4 lines

But in real life, there is only a one or two walkers if any active. So I would not consider a global update too expensive. But if you think it might become a problem, I'd add an hlist to xfrm_state of walkers referencing that entry.

Well in real life dumps don't tend to get suspended either :) In any case, a suspended dump is only going to pin down some memory, while a very long list walk may kill the box.

So, what to do?
1. Go back to: list_del_rcu, xfrm_state_hold(all.next) on delete and xfrm_state_put(all.next) on destruct.
2. Add per-entry hlist of walkers currently referencing it.
3. Use the global walker list.

1 can keep memory allocated until userland wakes up. 2 & 3 can make the delete of that entry slow if there's many walkers suspended.

Btw. the current stuff in net-next is broken. There's no locking for xfrm_state_walkers list handling.

Cheers,
Timo

maintainer
#49 lore raw
Quoted 9 lines · Timo Teräs

So, what to do?
1. Go back to: list_del_rcu, xfrm_state_hold(all.next) on delete and xfrm_state_put(all.next) on destruct.
2. Add per-entry hlist of walkers currently referencing it.
3. Use the global walker list.

1 can keep memory allocated until userland wakes up. 2 & 3 can make the delete of that entry slow if there's many walkers suspended.

I'd cross 3 off the list because 2 is just so much better :)

I'd slightly lean towards 2 but now that you mention it yes even that is vulnerable to loads of dumpers sitting on the same entry. So SELINUX folks wouldn't like that :)

Quoted 2 lines

Btw. the current stuff in net-next is broken. There's no locking for xfrm_state_walkers list handling.

What about xfrm_cfg_mutex?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#50 lore raw
Quoted 7 lines · Herbert Xu
Quoted 6 lines · Timo Teräs

So, what to do?
1. Go back to: list_del_rcu, xfrm_state_hold(all.next) on delete and xfrm_state_put(all.next) on destruct.
2. Add per-entry hlist of walkers currently referencing it.
3. Use the global walker list.

Quoted 8 lines
Quoted 2 lines

1 can keep memory allocated until userland wakes up. 2 & 3 can make the delete of that entry slow if there's many walkers suspended.

I'd cross 3 off the list because 2 is just so much better :)

I'd slightly lean towards 2 but now that you mention it yes even that is vulnerable to loads of dumpers sitting on the same entry. So SELINUX folks wouldn't like that :)

Umm... right. It's a tricky problem. Cannot think of perfect solution atm. But I guess 3 is in general case the best. But in worst case scenarios 1 performs better.

I have no strong opinion either way. So what ever you want, I'm happy to provide a patch for.

Quoted 4 lines
Quoted 2 lines

Btw. the current stuff in net-next is broken. There's no locking for xfrm_state_walkers list handling.

What about xfrm_cfg_mutex?

It's used only in xfrm_state_gc_task. xfrm_state_walk_{init,done} touch xfrm_state_walks list without locking properly. At least in the version I'm looking (= net-next-2.6 via git web interface).

- Timo

maintainer
#51 lore raw
Quoted 3 lines · Timo Teräs

I have no strong opinion either way. So what ever you want, I'm happy to provide a patch for.

Let's just leave it as it is for now.

Quoted 5 lines
Quoted 1 line

What about xfrm_cfg_mutex?

It's used only in xfrm_state_gc_task. xfrm_state_walk_{init,done} touch xfrm_state_walks list without locking properly. At least in the version I'm looking (= net-next-2.6 via git web interface).

Their callers should be holding it.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#52 lore raw
Quoted 5 lines · Herbert Xu
Quoted 2 lines · Timo Teräs

I have no strong opinion either way. So what ever you want, I'm happy to provide a patch for.

Let's just leave it as it is for now.

Well, 1 is an improvement over the current implementation, so I think it'd be better than not doing anything.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

What about xfrm_cfg_mutex?

It's used only in xfrm_state_gc_task. xfrm_state_walk_{init,done} touch xfrm_state_walks list without locking properly. At least in the version I'm looking (= net-next-2.6 via git web interface).

Their callers should be holding it.

Ah, the other layers take it at least on _walk_init paths. But _walk_done can be called from recv() syscalls. The af_key implementation does not take xfrm_cfg_mutex there. I don't think xfrm_user does that either as it does not pass cb_mutex to netlink_kernel_create. So at least the _state_walk_done path is unsafe as-is, I think.

- Timo

maintainer
#53 lore raw
Quoted 7 lines · Timo Teräs

Ah, the other layers take it at least on _walk_init paths. But _walk_done can be called from recv() syscalls. The af_key implementation does not take xfrm_cfg_mutex there. I don't think xfrm_user does that either as it does not pass cb_mutex to netlink_kernel_create. So at least the _state_walk_done path is unsafe as-is, I think.

OK we'd need to fix that up.

However, I've got a new idea :) Let's put the dumpers on the list directly and then we won't have to deal any of this crap about states going away.

Warning: compile tested only!

ipsec: Put dumpers on the dump list

As it is we go to extraordinary lengths to ensure that states don't go away while dumpers go to sleep. It's much easier if we just put the dumpers themselves on the list since they can't go away while they're going.

I've also changed the order of addition on new states to prevent a never-ending dump.

Finally the obsolete last optimisation is now gone.

include/linux/netlink.h +1 -1
index cbba776..9ff1b54 100644
@@ -220,7 +220,7 @@ struct netlink_callback
 	int		(*dump)(struct sk_buff * skb, struct netlink_callback *cb);
 	int		(*done)(struct netlink_callback *cb);
 	int		family;
-	long		args[7];
+	long		args[6];
 };
 
 struct netlink_notify
include/net/xfrm.h +7 -6
index 48630b2..17f9494 100644
@@ -120,9 +120,8 @@ extern struct mutex xfrm_cfg_mutex;
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	struct list_head	all;
 	union {
-		struct list_head	gclist;
+		struct hlist_node	gclist;
 		struct hlist_node	bydst;
 	};
 	struct hlist_node	bysrc;
@@ -138,6 +137,8 @@ struct xfrm_state
 
 	/* Key manger bits */
 	struct {
+		/* These two fields correspond to xfrm_state_walk. */
+		struct list_head all;
 		u8		state;
 		u8		dying;
 		u32		seq;
@@ -1246,11 +1247,11 @@ struct xfrm6_tunnel {
 };
 
 struct xfrm_state_walk {
-	struct list_head list;
-	unsigned long genid;
-	struct xfrm_state *state;
-	int count;
+	/* These two fields correspond to xfrm_state. */
+	struct list_head all;
+	u8 state;
 	u8 proto;
+	int count;
 };
 
 struct xfrm_policy_walk {
net/xfrm/xfrm_state.c +27 -69

180 lines not on this page · raw message

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#54 lore raw
Quoted 8 lines · Herbert Xu
Quoted 6 lines · Timo Teräs

Ah, the other layers take it at least on _walk_init paths. But _walk_done can be called from recv() syscalls. The af_key implementation does not take xfrm_cfg_mutex there. I don't think xfrm_user does that either as it does not pass cb_mutex to netlink_kernel_create. So at least the _state_walk_done path is unsafe as-is, I think.

Quoted 5 lines

OK we'd need to fix that up.

However, I've got a new idea :) Let's put the dumpers on the list directly and then we won't have to deal any of this crap about states going away.

Now this is an interesting idea... I like this a lot.

Only comment is that there really should be struct for the shared stuff. Otherwise it's bound to break at some point.

Cheers,
Timo

maintainer
#55 lore raw
Quoted 3 lines · Timo Teräs

Only comment is that there really should be struct for the shared stuff. Otherwise it's bound to break at some point.

That's why I put comments there. If people change it without reading the comments, they'll ignore the struct too :)

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#56 lore raw
Quoted 6 lines · Herbert Xu
Quoted 2 lines · Timo Teräs

Only comment is that there really should be struct for the shared stuff. Otherwise it's bound to break at some point.

That's why I put comments there. If people change it without reading the comments, they'll ignore the struct too :)

Well, it's also because in the dump routine the entries are cast to xfrm_state, even if it was a walker entry. This is just wrong, though it probably works since only the specific entries are used.

There should be some intermediate struct which we use to iterate in dumping routing, check if it was iterator/real entry (can be still based on the state thing). And only after that cast to struct xfrm_state. It would make the dumping routine more readable.

Cheers,
Timo

maintainer
#57 lore raw
Quoted 5 lines · Timo Teräs

Well, it's also because in the dump routine the entries are cast to xfrm_state, even if it was a walker entry. This is just wrong, though it probably works since only the specific entries are used.

No it works because all walker entries set the state to DEAD so they're skipped by everybody else.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#58 lore raw
Quoted 8 lines · Herbert Xu
Quoted 4 lines · Timo Teräs

Well, it's also because in the dump routine the entries are cast to xfrm_state, even if it was a walker entry. This is just wrong, though it probably works since only the specific entries are used.

No it works because all walker entries set the state to DEAD so they're skipped by everybody else.

Yes, I know. I was pointing the fact that the walker function iterates using struct xfrm_state. So temporarily when it is iterating through the walker entry, we get strct xfrm_state pointer which points to some place before the struct xfrm_state_walk. Now since the km.state is checked first, those are skipped.

I find it very confusing to let the code say "iterate through list of struct xfrm_state" when it is not such a list. It is a list of struct xfrm_state or struct xfrm_state_walk. So I'd use some intermediate struct to so the code can say e.g "iterate through list of struct xfrm_state_dump_entry" or whatever.

Or at least add a comment to the dumping function to say that we have struct xfrm_state, but in matter of fact it can be also struct xfrm_state_walk pointer with displacement, so we better check km.state first.

Cheers,
Timo

maintainer
#59 lore raw
Quoted 5 lines · Timo Teräs

Or at least add a comment to the dumping function to say that we have struct xfrm_state, but in matter of fact it can be also struct xfrm_state_walk pointer with displacement, so we better check km.state first.

Which is exactly what we do. The first thing we check in the loop is km.state. I really don't see your problem.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#60 lore raw
Quoted 8 lines · Herbert Xu
Quoted 4 lines · Timo Teräs

Or at least add a comment to the dumping function to say that we have struct xfrm_state, but in matter of fact it can be also struct xfrm_state_walk pointer with displacement, so we better check km.state first.

Which is exactly what we do. The first thing we check in the loop is km.state. I really don't see your problem.

Yes. I'm not complaining that the code does not work.

Just saying that the code easily misleads the reader. And in this kind of non-obvious places we should have some comments. Or make the code more readable by adding the intermediate struct.

Cheers,
Timo

maintainer
#61 lore raw
Quoted 4 lines · Timo Teräs

Just saying that the code easily misleads the reader. And in this kind of non-obvious places we should have some comments. Or make the code more readable by adding the intermediate struct.

Fair enough. Feel free to reformat the patch and add the struct to make it more bullet-proof. I'm sorry I've got some paid work to do now :)

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#62 lore raw
Quoted 8 lines · Herbert Xu
Quoted 3 lines · Timo Teräs

Just saying that the code easily misleads the reader. And in this kind of non-obvious places we should have some comments. Or make the code more readable by adding the intermediate struct.

Fair enough. Feel free to reformat the patch and add the struct to make it more bullet-proof. I'm sorry I've got some paid work to do now :)

Ok, here's one try. I also updated the list_add() calls introduced in commit "ipsec: Restore larval states and socket policies in dump".

Same warning still applies: only compile tested.

We should probably do similar fix for the SPD dumping. I can make a patch for that later today.

- Timo

ipsec: Put dumpers on the dump list

Based on Herbert Xu's patch and idea. Improved the original patch to apply cleanly and modified iteration code to be more readable by using a common struct for entries in all list.

As it is we go to extraordinary lengths to ensure that states don't go away while dumpers go to sleep. It's much easier if we just put the dumpers themselves on the list since they can't go away while they're going.

I've also changed the order of addition on new states to prevent a never-ending dump.

Finally the obsolete last optimisation is now gone.

include/linux/netlink.h +1 -1
index cbba776..9ff1b54 100644
@@ -220,7 +220,7 @@ struct netlink_callback
 	int		(*dump)(struct sk_buff * skb, struct netlink_callback *cb);
 	int		(*done)(struct netlink_callback *cb);
 	int		family;
-	long		args[7];
+	long		args[6];
 };
 
 struct netlink_notify
include/net/xfrm.h +13 -16
index 48630b2..becaa1e 100644
@@ -117,12 +117,21 @@ extern struct mutex xfrm_cfg_mutex;
       metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
  */
 
+struct xfrm_state_walk {
+	struct list_head	all;
+	u8			state;
+	union {
+		u8		dying;
+		u8		proto;
+	};
+	u32			seq;
+};
+
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	struct list_head	all;
 	union {
-		struct list_head	gclist;
+		struct hlist_node	gclist;
 		struct hlist_node	bydst;
 	};
 	struct hlist_node	bysrc;
@@ -136,12 +145,8 @@ struct xfrm_state
 
 	u32			genid;
 
-	/* Key manger bits */
-	struct {
-		u8		state;
-		u8		dying;
-		u32		seq;
-	} km;
+	/* Key manager bits */
+	struct xfrm_state_walk	km;
 
 	/* Parameters of this state. */
 	struct {
@@ -1245,14 +1250,6 @@ struct xfrm6_tunnel {
 	int priority;
 };
 
-struct xfrm_state_walk {
-	struct list_head list;
-	unsigned long genid;
-	struct xfrm_state *state;
-	int count;
-	u8 proto;
-};
-
 struct xfrm_policy_walk {
 	struct xfrm_policy *policy;
 	int count;
net/xfrm/xfrm_state.c +35 -75

202 lines not on this page · raw message

maintainer
#63 lore raw
Quoted 2 lines · Timo Teräs

Same warning still applies: only compile tested.

Looks good to me, I espeically like the count/seq bit. But I'm sure Dave would appreciate it if we actually ran this too :)

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#64 lore raw
Quoted 5 lines · Herbert Xu
Quoted 1 line · Timo Teräs

Same warning still applies: only compile tested.

Looks good to me, I espeically like the count/seq bit. But I'm sure Dave would appreciate it if we actually ran this too :)

Gave it a test spin and found one bug. The walking routine will get called one extra time when it has successfully dumped everything so we need to check for that in the beginning; the earlier version would start all over from the beginning resulting never ending dumps.

So the patch is now as follows. I have an almost ready patch for xfrm_policy dumping too. Will finish it and send tomorrow.

- Timo

ipsec: Put xfrm_state dumpers on the dump list

Based on Herbert Xu's patch and idea. Improved the original patch to apply cleanly and modified iteration code to be more readable by using a common struct for entries in all list.

As it is we go to extraordinary lengths to ensure that states don't go away while dumpers go to sleep. It's much easier if we just put the dumpers themselves on the list since they can't go away while they're going.

I've also changed the order of addition on new states to prevent a never-ending dump.

Finally the obsolete last optimisation is now gone.

---
 include/linux/netlink.h |    2 +-
 include/net/xfrm.h      |   29 ++++++-------
 net/xfrm/xfrm_state.c   |  109 +++++++++++++++-------------------------------
 3 files changed, 50 insertions(+), 90 deletions(-)
include/linux/netlink.h +1 -1
index cbba776..9ff1b54 100644
@@ -220,7 +220,7 @@ struct netlink_callback
 	int		(*dump)(struct sk_buff * skb, struct netlink_callback *cb);
 	int		(*done)(struct netlink_callback *cb);
 	int		family;
-	long		args[7];
+	long		args[6];
 };
 
 struct netlink_notify
include/net/xfrm.h +13 -16
index 48630b2..becaa1e 100644
@@ -117,12 +117,21 @@ extern struct mutex xfrm_cfg_mutex;
       metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
  */
 
+struct xfrm_state_walk {
+	struct list_head	all;
+	u8			state;
+	union {
+		u8		dying;
+		u8		proto;
+	};
+	u32			seq;
+};
+
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	struct list_head	all;
 	union {
-		struct list_head	gclist;
+		struct hlist_node	gclist;
 		struct hlist_node	bydst;
 	};
 	struct hlist_node	bysrc;
@@ -136,12 +145,8 @@ struct xfrm_state
 
 	u32			genid;
 
-	/* Key manger bits */
-	struct {
-		u8		state;
-		u8		dying;
-		u32		seq;
-	} km;
+	/* Key manager bits */
+	struct xfrm_state_walk	km;
 
 	/* Parameters of this state. */
 	struct {
@@ -1245,14 +1250,6 @@ struct xfrm6_tunnel {
 	int priority;
 };
 
-struct xfrm_state_walk {
-	struct list_head list;
-	unsigned long genid;
-	struct xfrm_state *state;
-	int count;
-	u8 proto;
-};
-
 struct xfrm_policy_walk {
 	struct xfrm_policy *policy;
 	int count;
net/xfrm/xfrm_state.c +36 -73

203 lines not on this page · raw message

maintainer
#65 lore raw
Quoted 6 lines · Timo Teräs

Gave it a test spin and found one bug. The walking routine will get called one extra time when it has successfully dumped everything so we need to check for that in the beginning; the earlier version would start all over from the beginning resulting never ending dumps.

Aha, that's why the if statement was there :)

Thanks for all the work!

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#66 lore raw
Quoted 8 lines · Herbert Xu
Quoted 5 lines · Timo Teräs

Gave it a test spin and found one bug. The walking routine will get called one extra time when it has successfully dumped everything so we need to check for that in the beginning; the earlier version would start all over from the beginning resulting never ending dumps.

Aha, that's why the if statement was there :)

Yup.

I forgot to test pf_key support. And remembered why the "last" thingy was in the walking routines. In pf_key dumps, the dump is terminated when an entry with seq zero is received; that's why the final entry received special treatment. And now that breaks. The first entry is zero so pf_key dumps only one entry.

Not sure if we should fix this in pf_key or in the walking routines.

Cheers,
Timo

maintainer
#67 lore raw
Quoted 8 lines · Timo Teräs

I forgot to test pf_key support. And remembered why the "last" thingy was in the walking routines. In pf_key dumps, the dump is terminated when an entry with seq zero is received; that's why the final entry received special treatment. And now that breaks. The first entry is zero so pf_key dumps only one entry.

Not sure if we should fix this in pf_key or in the walking routines.

This is part of the user-space ABI (and an RFC) so wecan't change it. I've put the last stuff back and fixed up the error handling on the very last entry.

ipsec: Put xfrm_state dumpers on the dump list

Based on Herbert Xu's patch and idea. Timo improved the original patch to apply cleanly and modified iteration code to be more readable by using a common struct for entries in all list.

As it is we go to extraordinary lengths to ensure that states don't go away while dumpers go to sleep. It's much easier if we just put the dumpers themselves on the list since they can't go away while they're going.

I've also changed the order of addition on new states to prevent a never-ending dump.

include/linux/netlink.h +1 -1
index cbba776..9ff1b54 100644
@@ -220,7 +220,7 @@ struct netlink_callback
 	int		(*dump)(struct sk_buff * skb, struct netlink_callback *cb);
 	int		(*done)(struct netlink_callback *cb);
 	int		family;
-	long		args[7];
+	long		args[6];
 };
 
 struct netlink_notify
include/net/xfrm.h +13 -16
index 48630b2..becaa1e 100644
@@ -117,12 +117,21 @@ extern struct mutex xfrm_cfg_mutex;
       metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
  */
 
+struct xfrm_state_walk {
+	struct list_head	all;
+	u8			state;
+	union {
+		u8		dying;
+		u8		proto;
+	};
+	u32			seq;
+};
+
 /* Full description of state of transformer. */
 struct xfrm_state
 {
-	struct list_head	all;
 	union {
-		struct list_head	gclist;
+		struct hlist_node	gclist;
 		struct hlist_node	bydst;
 	};
 	struct hlist_node	bysrc;
@@ -136,12 +145,8 @@ struct xfrm_state
 
 	u32			genid;
 
-	/* Key manger bits */
-	struct {
-		u8		state;
-		u8		dying;
-		u32		seq;
-	} km;
+	/* Key manager bits */
+	struct xfrm_state_walk	km;
 
 	/* Parameters of this state. */
 	struct {
@@ -1245,14 +1250,6 @@ struct xfrm6_tunnel {
 	int priority;
 };
 
-struct xfrm_state_walk {
-	struct list_head list;
-	unsigned long genid;
-	struct xfrm_state *state;
-	int count;
-	u8 proto;
-};
-
 struct xfrm_policy_walk {
 	struct xfrm_policy *policy;
 	int count;
net/xfrm/xfrm_state.c +48 -72

215 lines not on this page · raw message

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#68 lore raw
Quoted 7 lines · Herbert Xu
Quoted 6 lines · Timo Teräs

I forgot to test pf_key support. And remembered why the "last" thingy was in the walking routines. In pf_key dumps, the dump is terminated when an entry with seq zero is received; that's why the final entry received special treatment. And now that breaks. The first entry is zero so pf_key dumps only one entry.

Quoted 5 lines
Quoted 1 line

Not sure if we should fix this in pf_key or in the walking routines.

This is part of the user-space ABI (and an RFC) so wecan't change it. I've put the last stuff back and fixed up the error handling on the very last entry.

I was just figuring if we need to change seq in pf_key.c kernel code or in xfrm_state.c. But I guess it's good if the walking walks as previously.

Now there is a new problem: if dumping is interrupted and meanwhile all the remaining entries get deleted, there is no entry to dump with seq zero when dumping is eventually continued.

Since xfrm_user does not care for the seq as end-of-message, this problem appears only in af_key.

Maybe we should make af_key.c code instead cache one entry before sending it to user space. We could then get rid of the last hack inside xfrm core walking code.

Cheers,
Timo

maintainer
#69 lore raw
Quoted 4 lines · Timo Teräs

Maybe we should make af_key.c code instead cache one entry before sending it to user space. We could then get rid of the last hack inside xfrm core walking code.

Yes that should work. But you want to keep cache the skb instead of the state since the latter can die.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#70 lore raw
Quoted 7 lines · Herbert Xu
Quoted 3 lines · Timo Teräs

Maybe we should make af_key.c code instead cache one entry before sending it to user space. We could then get rid of the last hack inside xfrm core walking code.

Yes that should work. But you want to keep cache the skb instead of the state since the latter can die.

Exactly, keep the converted entry in skb, since we have to allocate that anyway. And when there's no more entries coming from enumerator the sequence number of final skb is adjusted to zero.

Not sure if I can finish it today, but I think I should have the time by tomorrow. Unless you have time to do it before that, I'll post a patch later this week. Will update the similar xfrm_policy patch for that too, before sending it.

- Timo

maintainer
#71 lore raw
Quoted 5 lines · Timo Teräs

Not sure if I can finish it today, but I think I should have the time by tomorrow. Unless you have time to do it before that, I'll post a patch later this week. Will update the similar xfrm_policy patch for that too, before sending it.

Thanks! I've got some other work to do so feel free to post this tomorrow.

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#72 lore raw
Quoted 8 lines · Herbert Xu
Quoted 4 lines · Timo Teräs

Not sure if I can finish it today, but I think I should have the time by tomorrow. Unless you have time to do it before that, I'll post a patch later this week. Will update the similar xfrm_policy patch for that too, before sending it.

Thanks! I've got some other work to do so feel free to post this tomorrow.

It was simpler than I thought (and I thought I would be more busy with other things). Anyway, here goes. This is a single patch that modifies both xfrm_state and xfrm_policy dumping. This was to get af_key patch nicer (the final skb patching/sending is common code).

Also, since the af_key implementation is the only place where this seq number is used, we might consider removing it from the core walking code. It's pretty af_key specfic too.

The xfrm_policy patch uses moves xfrm_policy.dead to xfrm_policy_walk_entry which is the struct shared with walkers and the real entries.

I did some testing to ensure that it works with xfrm and af_key as expected (with entries added by hand and dumping them).

ipsec: Put dumpers on the dump list

Herbert Xu came up with the idea and the original patch to make xfrm_state dump list contain also dumpers:

As it is we go to extraordinary lengths to ensure that states don't go away while dumpers go to sleep. It's much easier if we just put the dumpers themselves on the list since they can't go away while they're going.

I've also changed the order of addition on new states to prevent a never-ending dump.

Timo Teräs improved the patch to apply cleanly to latest tree, modified iteration code to be more readable by using a common struct for entries in the list, implemented the same idea for xfrm_policy dumping and moved the af_key specific "last" entry caching to af_key.

include/linux/netlink.h +1 -1
index cbba776..9ff1b54 100644
@@ -220,7 +220,7 @@ struct netlink_callback
 	int		(*dump)(struct sk_buff * skb, struct netlink_callback *cb);
 	int		(*done)(struct netlink_callback *cb);
 	int		family;
-	long		args[7];
+	long		args[6];
 };
 
 struct netlink_notify
include/net/xfrm.h +28 -42

119 lines not on this page · raw message

net/key/af_key.c +43 -19

100 lines not on this page · raw message

net/xfrm/xfrm_policy.c +59 -52

232 lines not on this page · raw message

net/xfrm/xfrm_state.c +36 -73

203 lines not on this page · raw message

net/xfrm/xfrm_user.c +2 -2
index 04c4150..76f75df 100644
@@ -1102,7 +1102,7 @@ static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p,
 	return xp;
  error:
 	*errp = err;
-	xp->dead = 1;
+	xp->walk.dead = 1;
 	xfrm_policy_destroy(xp);
 	return NULL;
 }
@@ -1595,7 +1595,7 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
 		return -ENOENT;
 
 	read_lock(&xp->lock);
-	if (xp->dead) {
+	if (xp->walk.dead) {
 		read_unlock(&xp->lock);
 		goto out;
 	}
#73 lore raw
Quoted 8 lines · Timo Teräs
Quoted 2 lines · Herbert Xu

Thanks! I've got some other work to do so feel free to post this tomorrow.

It was simpler than I thought (and I thought I would be more busy with other things). Anyway, here goes. This is a single patch that modifies both xfrm_state and xfrm_policy dumping. This was to get af_key patch nicer (the final skb patching/sending is common code).

One more thing I noticed. If the af_key socket is closed while dumping is ongoing the current code can leak stuff. We should call the .done. And also in the new patch, release the stored skb.

The right place to do that would be pfkey_release, I assume?

- Timo

maintainer
#74 lore raw
Quoted 2 lines · Timo Teräs

The right place to do that would be pfkey_release, I assume?

Yes that sounds right.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#75 lore raw
In reply to Timo Teräs

From: Timo Teräs <[email protected]>
Date: Thu, 25 Sep 2008 15:36:04 +0300

Quoted 10 lines
Quoted 8 lines · Timo Teräs
Quoted 2 lines · Herbert Xu

Thanks! I've got some other work to do so feel free to post this tomorrow.

It was simpler than I thought (and I thought I would be more busy with other things). Anyway, here goes. This is a single patch that modifies both xfrm_state and xfrm_policy dumping. This was to get af_key patch nicer (the final skb patching/sending is common code).

Quoted 5 lines

One more thing I noticed. If the af_key socket is closed while dumping is ongoing the current code can leak stuff. We should call the .done. And also in the new patch, release the stored skb.

The right place to do that would be pfkey_release, I assume?

I reread this thread, and the most recent patch seems good and even tested :-) But it seems that you want to make some more tweaks, right?

Please post explicitly the patch you want me to apply to net-next-2.6 once you reach that point, thanks!

maintainer
#76 lore raw
Quoted 9 lines · David Miller
Quoted 2 lines

The right place to do that would be pfkey_release, I assume?

I reread this thread, and the most recent patch seems good and even tested :-) But it seems that you want to make some more tweaks, right?

Please post explicitly the patch you want me to apply to net-next-2.6 once you reach that point, thanks!

Yes I think Timo is still touching it up.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt