Skip to contentSkip to conversation

ipv6: do not wait for a running gc in ip6_dst_gc()

PATCH New Willem de Bruijn 1 participant 1 message Dec 1

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

PATCH 1/1 ipv6: do not wait for a running gc in ip6_dst_gc()

From: Chris Mason <[email protected]>

With more than net.ipv6.route.gc_thresh cached routes, CPUs that allocate an IPv6 route while the route garbage collector runs can stall for as many gc runs as there are CPUs waiting, with bottom halves disabled.

While more than net.ipv6.route.gc_thresh cached routes exist, dst_alloc() calls ip6_dst_gc(), which starts a gc run, fib6_run_gc(), unless the last one ended less than gc_min_interval ago. A gc run holds fib6_gc_lock with bottom halves disabled, and sets ip6_rt_last_gc, the time at which the last gc run ended, only at its end. But ip6_dst_gc() tests ip6_rt_last_gc before it takes the lock, so each CPU that passes the test during a gc run spins for the lock and then starts a gc run of its own.

Commit 2ac3ac8f86f2 ("ipv6: prevent fib6_run_gc() contention") in v3.11 fixed this contention, which it reported as soft lockups on a high-traffic router with many processors, by having ip6_dst_gc() only try the lock unless the number of entries exceeded max_size. Commit 14956643550f ("ipv6: slight optimization in ip6_dst_gc") in v3.16 made ip6_dst_gc() wait for the lock again in all cases, on the premise that the number of entries always exceeds max_size at that point. That does not hold once gc_min_interval has passed since the last gc run.

The fix used here is to give ip6_dst_gc() a variant of fib6_run_gc(), fib6_try_run_gc(), which returns at once if another CPU holds fib6_gc_lock, and repeats the test of ip6_rt_last_gc under the lock.

Found and fixed with an LLM; reproduced on an unmodified kernel with an LLM-generated reproducer in a 16-CPU KVM guest. Verified with the reproducer and IPv6 route selftests (fib_tests.sh, fib_nexthops.sh).

Fixes 14956643550f ("ipv6: slight optimization in ip6_dst_gc")
Cc <[email protected]> # 6.18.x: 12eddc685744: ipv6: annotate data-races around sysctl.ip6_rt_gc_interval
Cc <[email protected]> # 6.18.x

[willemb: add lockdep_assert_held() and WRITE_ONCE() on ip6_rt_last_gc,
expand commit message]

---
 include/net/ip6_fib.h |  1 +
 net/ipv6/ip6_fib.c    | 44 +++++++++++++++++++++++++++++++++++--------
 net/ipv6/route.c      |  4 +++-
 3 files changed, 40 insertions(+), 9 deletions(-)
include/net/ip6_fib.h +1 -0
index 232289b439c3..8d5240981284 100644
@@ -539,6 +539,7 @@ void inet6_rt_del_notify(struct fib6_info *rt, struct nl_info *info,
 void fib6_age_exceptions(struct fib6_info *rt, struct fib6_gc_args *gc_args,
 			 unsigned long now);
 void fib6_run_gc(unsigned long expires, struct net *net, bool force);
+bool fib6_try_run_gc(struct net *net, int min_interval);
 void fib6_gc_cleanup(void);
 
 int fib6_init(void);
net/ipv6/ip6_fib.c +36 -8

70 lines not on this page · raw message

net/ipv6/route.c +3 -1
index dbc878c09b87..ee7c8f66dc3d 100644
@@ -3381,7 +3381,9 @@ static void ip6_dst_gc(struct dst_ops *ops)
 	if (time_after(rt_last_gc + rt_min_interval, jiffies))
 		goto out;
 
-	fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire), net, true);
+	if (!fib6_try_run_gc(net, rt_min_interval))
+		goto out;
+
 	entries = dst_entries_get_slow(ops);
 	if (entries < ops->gc_thresh)
 		atomic_set(&net->ipv6.ip6_rt_gc_expire, rt_gc_timeout >> 1);
Signature · 1 line
2.56.1.839.g22a8290db9-goog