Skip to contentSkip to conversation

ipv6: prevent fib6_run_gc() contention

PATCH Superseded Michal Kubecek 3 participants 9 messages Jun 4, 2013 – Jul 31, 2013

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipv6: prevent fib6_run_gc() contention

On a high-traffic router with many processors and many IPv6 dst entries, soft lockup in fib6_run_gc() can occur when number of entries reaches gc_thresh.

This happens because fib6_run_gc() uses fib6_gc_lock to allow only one thread to run the garbage collector but ip6_dst_gc() doesn't update net->ipv6.ip6_rt_last_gc until fib6_run_gc() returns. On a system with many entries, this can take some time so that in the meantime, other threads pass the tests in ip6_dst_gc() (ip6_rt_last_gc is still not updated) and wait for the lock. They then have to run the garbage collector one after another which blocks them for quite long.

Resolve this by replacing special value ~0UL of expire parameter to fib6_run_gc() by explicit "force" parameter to choose between spin_lock_bh() and spin_trylock_bh() and call fib6_run_gc() with force=false if gc_thresh is reached but not max_size.

---
 include/net/ip6_fib.h |  2 +-
 net/ipv6/ip6_fib.c    | 19 ++++++++-----------
 net/ipv6/ndisc.c      |  4 ++--
 net/ipv6/route.c      |  4 ++--
 4 files changed, 13 insertions(+), 16 deletions(-)
include/net/ip6_fib.h +1 -1
index 2a601e7..48ec25a 100644
@@ -300,7 +300,7 @@ extern void			inet6_rt_notify(int event, struct rt6_info *rt,
 						struct nl_info *info);
 
 extern void			fib6_run_gc(unsigned long expires,
-					    struct net *net);
+					    struct net *net, bool force);
 
 extern void			fib6_gc_cleanup(void);
 
net/ipv6/ip6_fib.c +8 -11
index 192dd1a..1dcd618 100644
@@ -1625,19 +1625,16 @@ static int fib6_age(struct rt6_info *rt, void *arg)
 
 static DEFINE_SPINLOCK(fib6_gc_lock);
 
-void fib6_run_gc(unsigned long expires, struct net *net)
+void fib6_run_gc(unsigned long expires, struct net *net, bool force)
 {
-	if (expires != ~0UL) {
+	if (force)
 		spin_lock_bh(&fib6_gc_lock);
-		gc_args.timeout = expires ? (int)expires :
-			net->ipv6.sysctl.ip6_rt_gc_interval;
-	} else {
-		if (!spin_trylock_bh(&fib6_gc_lock)) {
-			mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
-			return;
-		}
-		gc_args.timeout = net->ipv6.sysctl.ip6_rt_gc_interval;
+	else if (!spin_trylock_bh(&fib6_gc_lock)) {
+		mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
+		return;
 	}
+	gc_args.timeout = expires ? (int)expires :
+			  net->ipv6.sysctl.ip6_rt_gc_interval;
 
 	gc_args.more = icmp6_dst_gc();
 
@@ -1654,7 +1651,7 @@ void fib6_run_gc(unsigned long expires, struct net *net)
 
 static void fib6_gc_timer_cb(unsigned long arg)
 {
-	fib6_run_gc(0, (struct net *)arg);
+	fib6_run_gc(0, (struct net *)arg, true);
 }
 
 static int __net_init fib6_net_init(struct net *net)
net/ipv6/ndisc.c +2 -2
index 2712ab2..bf60c25 100644
@@ -1575,7 +1575,7 @@ static int ndisc_netdev_event(struct notifier_block *this, unsigned long event,
 	switch (event) {
 	case NETDEV_CHANGEADDR:
 		neigh_changeaddr(&nd_tbl, dev);
-		fib6_run_gc(~0UL, net);
+		fib6_run_gc(0, net, false);
 		idev = in6_dev_get(dev);
 		if (!idev)
 			break;
@@ -1585,7 +1585,7 @@ static int ndisc_netdev_event(struct notifier_block *this, unsigned long event,
 		break;
 	case NETDEV_DOWN:
 		neigh_ifdown(&nd_tbl, dev);
-		fib6_run_gc(~0UL, net);
+		fib6_run_gc(0, net, false);
 		break;
 	case NETDEV_NOTIFY_PEERS:
 		ndisc_send_unsol_na(dev);
net/ipv6/route.c +2 -2
index ad0aa6b..fdb5ccb 100644
@@ -1300,7 +1300,7 @@ static int ip6_dst_gc(struct dst_ops *ops)
 		goto out;
 
 	net->ipv6.ip6_rt_gc_expire++;
-	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
+	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, entries > rt_max_size);
 	net->ipv6.ip6_rt_last_gc = now;
 	entries = dst_entries_get_slow(ops);
 	if (entries < ops->gc_thresh)
@@ -2801,7 +2801,7 @@ int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
 	net = (struct net *)ctl->extra1;
 	delay = net->ipv6.sysctl.flush_delay;
 	proc_dointvec(ctl, write, buffer, lenp, ppos);
-	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
+	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
 	return 0;
 }
 
Signature · 1 line
1.8.1.4
maintainer
#1 lore raw

From: Michal Kubecek <[email protected]>
Date: Tue, 4 Jun 2013 13:08:59 +0200

Quoted 13 lines

On a high-traffic router with many processors and many IPv6 dst entries, soft lockup in fib6_run_gc() can occur when number of entries reaches gc_thresh.

This happens because fib6_run_gc() uses fib6_gc_lock to allow only one thread to run the garbage collector but ip6_dst_gc() doesn't update net->ipv6.ip6_rt_last_gc until fib6_run_gc() returns. On a system with many entries, this can take some time so that in the meantime, other threads pass the tests in ip6_dst_gc() (ip6_rt_last_gc is still not updated) and wait for the lock. They then have to run the garbage collector one after another which blocks them for quite long.

Quoted 6 lines

Resolve this by replacing special value ~0UL of expire parameter to fib6_run_gc() by explicit "force" parameter to choose between spin_lock_bh() and spin_trylock_bh() and call fib6_run_gc() with force=false if gc_thresh is reached but not max_size.

It seems to me that it would be much simpler to simply update ip6_rt_last_gc first, that way the other threads would elide the GC call.

Can you see if simply doing that fixes the problem too?

Thanks.

#2 lore raw
Quoted 21 lines · David Miller

From: Michal Kubecek <[email protected]>
Date: Tue, 4 Jun 2013 13:08:59 +0200

Quoted 18 lines

On a high-traffic router with many processors and many IPv6 dst entries, soft lockup in fib6_run_gc() can occur when number of entries reaches gc_thresh.

This happens because fib6_run_gc() uses fib6_gc_lock to allow only one thread to run the garbage collector but ip6_dst_gc() doesn't update net->ipv6.ip6_rt_last_gc until fib6_run_gc() returns. On a system with many entries, this can take some time so that in the meantime, other threads pass the tests in ip6_dst_gc() (ip6_rt_last_gc is still not updated) and wait for the lock. They then have to run the garbage collector one after another which blocks them for quite long.

Resolve this by replacing special value ~0UL of expire parameter to fib6_run_gc() by explicit "force" parameter to choose between spin_lock_bh() and spin_trylock_bh() and call fib6_run_gc() with force=false if gc_thresh is reached but not max_size.

Quoted 5 lines
Quoted 1 line

It seems to me that it would be much simpler to simply update ip6_rt_last_gc first, that way the other threads would elide the GC call.

That was my original idea but I was afraid that while the remaining window in ip6_dst_gc() would be very short and probably safe, we could still run into problem if fib6_gc_lock was locked by some other caller of fib6_run_gc() which doesn't update net->ipv6.ip6_rt_last_gc, especially via a timer.

Michal Kubecek

#3 lore raw
Quoted 5 lines · Michal Kubecek

That was my original idea but I was afraid that while the remaining window in ip6_dst_gc() would be very short and probably safe, we could still run into problem if fib6_gc_lock was locked by some other caller of fib6_run_gc() which doesn't update net->ipv6.ip6_rt_last_gc, especially via a timer.

This looks a bug (but not a big one) to me :

We should update ip6_rt_last_gc if we did a gc,
not only from ip6_dst_gc()

So, I would move the "net->ipv6.ip6_rt_last_gc = now;" from ip6_dst_gc() to fib6_run_gc(), right after the spin_lock_bh() ?

maintainer
#4 lore raw
In reply to Michal Kubecek

From: Michal Kubecek <[email protected]>
Date: Tue, 11 Jun 2013 12:07:18 +0200

Quoted 28 lines · David Miller
Quoted 21 lines

From: Michal Kubecek <[email protected]>
Date: Tue, 4 Jun 2013 13:08:59 +0200

Quoted 18 lines

On a high-traffic router with many processors and many IPv6 dst entries, soft lockup in fib6_run_gc() can occur when number of entries reaches gc_thresh.

This happens because fib6_run_gc() uses fib6_gc_lock to allow only one thread to run the garbage collector but ip6_dst_gc() doesn't update net->ipv6.ip6_rt_last_gc until fib6_run_gc() returns. On a system with many entries, this can take some time so that in the meantime, other threads pass the tests in ip6_dst_gc() (ip6_rt_last_gc is still not updated) and wait for the lock. They then have to run the garbage collector one after another which blocks them for quite long.

Resolve this by replacing special value ~0UL of expire parameter to fib6_run_gc() by explicit "force" parameter to choose between spin_lock_bh() and spin_trylock_bh() and call fib6_run_gc() with force=false if gc_thresh is reached but not max_size.

Quoted 5 lines
Quoted 1 line

It seems to me that it would be much simpler to simply update ip6_rt_last_gc first, that way the other threads would elide the GC call.

Quoted 5 lines

That was my original idea but I was afraid that while the remaining window in ip6_dst_gc() would be very short and probably safe, we could still run into problem if fib6_gc_lock was locked by some other caller of fib6_run_gc() which doesn't update net->ipv6.ip6_rt_last_gc, especially via a timer.

Why don't you simply try it and find out?

#5 lore raw
Quoted 9 lines · David Miller

From: Michal Kubecek <[email protected]>
Date: Tue, 11 Jun 2013 12:07:18 +0200

Quoted 6 lines · David Miller
Quoted 4 lines

It seems to me that it would be much simpler to simply update ip6_rt_last_gc first, that way the other threads would elide the GC call.

Quoted 7 lines
Quoted 5 lines

That was my original idea but I was afraid that while the remaining window in ip6_dst_gc() would be very short and probably safe, we could still run into problem if fib6_gc_lock was locked by some other caller of fib6_run_gc() which doesn't update net->ipv6.ip6_rt_last_gc, especially via a timer.

Why don't you simply try it and find out?

I don't have the system where the issue was observed at hand. I'll have to ask the customer who reported it to run the test so that it may take some time.

Michal Kubecek

maintainer
#6 lore raw

From: Michal Kubecek <[email protected]>
Date: Tue, 11 Jun 2013 22:49:12 +0200

Quoted 16 lines · David Miller
Quoted 15 lines

From: Michal Kubecek <[email protected]>
Date: Tue, 11 Jun 2013 12:07:18 +0200

Quoted 11 lines · David Miller
Quoted 4 lines

It seems to me that it would be much simpler to simply update ip6_rt_last_gc first, that way the other threads would elide the GC call.

That was my original idea but I was afraid that while the remaining window in ip6_dst_gc() would be very short and probably safe, we could still run into problem if fib6_gc_lock was locked by some other caller of fib6_run_gc() which doesn't update net->ipv6.ip6_rt_last_gc, especially via a timer.

Quoted 5 lines
Quoted 1 line

Why don't you simply try it and find out?

I don't have the system where the issue was observed at hand. I'll have to ask the customer who reported it to run the test so that it may take some time.

Please also take care of the issue Eric mentioned, in that some GC calls erroneously do not update the last GC timestamp.

#7 lore raw
Quoted 19 lines · David Miller

From: Michal Kubecek <[email protected]>
Date: Tue, 11 Jun 2013 22:49:12 +0200

Quoted 16 lines · David Miller
Quoted 15 lines

From: Michal Kubecek <[email protected]>
Date: Tue, 11 Jun 2013 12:07:18 +0200

Quoted 11 lines · David Miller
Quoted 4 lines

It seems to me that it would be much simpler to simply update ip6_rt_last_gc first, that way the other threads would elide the GC call.

That was my original idea but I was afraid that while the remaining window in ip6_dst_gc() would be very short and probably safe, we could still run into problem if fib6_gc_lock was locked by some other caller of fib6_run_gc() which doesn't update net->ipv6.ip6_rt_last_gc, especially via a timer.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Why don't you simply try it and find out?

I don't have the system where the issue was observed at hand. I'll have to ask the customer who reported it to run the test so that it may take some time.

Please also take care of the issue Eric mentioned, in that some GC calls erroneously do not update the last GC timestamp.

I received the feedback today. The customer reported the patch moving update of ip6_rt_last_gc into fib6_run_gc() before the call of fib6_clean_all() was not sufficient to prevent the contention with their workload.

So I suggest to return back to the approach I submitted originally but to move the update into fib6_run_gc() anyway so that every run of the garbage collector updates it. I will submit the patches tomorrow.

Michal Kubecek

maintainer
#8 lore raw

From: Michal Kubecek <[email protected]>
Date: Thu, 1 Aug 2013 00:19:31 +0200

Quoted 8 lines

I received the feedback today. The customer reported the patch moving update of ip6_rt_last_gc into fib6_run_gc() before the call of fib6_clean_all() was not sufficient to prevent the contention with their workload.

So I suggest to return back to the approach I submitted originally but to move the update into fib6_run_gc() anyway so that every run of the garbage collector updates it. I will submit the patches tomorrow.

Fair enough.