* [PATCH net] ipv6: do not wait for a running gc in ip6_dst_gc()
@ 2026-12-01 1:55 Willem de Bruijn
2026-12-01 2:34 ` David Ahern
0 siblings, 1 reply; 3+ messages in thread
From: Willem de Bruijn @ 2026-12-01 1:55 UTC (permalink / raw)
To: netdev
Cc: David Ahern, Ido Schimmel, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Chris Mason, stable,
Willem de Bruijn
From: Chris Mason <mason@kernel.org>
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: <stable@vger.kernel.org> # 6.18.x: 12eddc685744: ipv6: annotate data-races around sysctl.ip6_rt_gc_interval
Cc: <stable@vger.kernel.org> # 6.18.x
Signed-off-by: Chris Mason <mason@kernel.org>
[willemb: add lockdep_assert_held() and WRITE_ONCE() on ip6_rt_last_gc,
expand commit message]
Signed-off-by: Willem de Bruijn <willemb@google.com>
---
include/net/ip6_fib.h | 1 +
net/ipv6/ip6_fib.c | 44 +++++++++++++++++++++++++++++++++++--------
net/ipv6/route.c | 4 +++-
3 files changed, 40 insertions(+), 9 deletions(-)
diff --git a/include/net/ip6_fib.h b/include/net/ip6_fib.h
index 232289b439c3..8d5240981284 100644
--- a/include/net/ip6_fib.h
+++ b/include/net/ip6_fib.h
@@ -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);
diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
index e1d49897d6ab..6e81be2bedf7 100644
--- a/net/ipv6/ip6_fib.c
+++ b/net/ipv6/ip6_fib.c
@@ -2446,34 +2446,62 @@ static void fib6_gc_all(struct net *net, struct fib6_gc_args *gc_args)
rcu_read_unlock();
}
-void fib6_run_gc(unsigned long expires, struct net *net, bool force)
+static void __fib6_run_gc(unsigned long expires, struct net *net)
{
struct fib6_gc_args gc_args;
int ip6_rt_gc_interval;
unsigned long now;
- if (force) {
- spin_lock_bh(&net->ipv6.fib6_gc_lock);
- } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
- mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
- return;
- }
+ lockdep_assert_held(&net->ipv6.fib6_gc_lock);
+
ip6_rt_gc_interval = READ_ONCE(net->ipv6.sysctl.ip6_rt_gc_interval);
gc_args.timeout = expires ? (int)expires : ip6_rt_gc_interval;
gc_args.more = 0;
fib6_gc_all(net, &gc_args);
now = jiffies;
- net->ipv6.ip6_rt_last_gc = now;
+ WRITE_ONCE(net->ipv6.ip6_rt_last_gc, now);
if (gc_args.more)
mod_timer(&net->ipv6.ip6_fib_timer,
round_jiffies(now + ip6_rt_gc_interval));
else
timer_delete(&net->ipv6.ip6_fib_timer);
+}
+
+void fib6_run_gc(unsigned long expires, struct net *net, bool force)
+{
+ if (force) {
+ spin_lock_bh(&net->ipv6.fib6_gc_lock);
+ } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
+ mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
+ return;
+ }
+
+ __fib6_run_gc(expires, net);
spin_unlock_bh(&net->ipv6.fib6_gc_lock);
}
+/* For ip6_dst_gc(): neither wait for a run on another CPU, nor make a run
+ * before min_interval has passed since the last one. Only a run raises
+ * ip6_rt_gc_expire. Returns true after a run.
+ */
+bool fib6_try_run_gc(struct net *net, int min_interval)
+{
+ bool run;
+
+ if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock))
+ return false;
+
+ run = !time_after(net->ipv6.ip6_rt_last_gc + min_interval, jiffies);
+ if (run)
+ __fib6_run_gc(atomic_inc_return(&net->ipv6.ip6_rt_gc_expire),
+ net);
+ spin_unlock_bh(&net->ipv6.fib6_gc_lock);
+
+ return run;
+}
+
static void fib6_gc_timer_cb(struct timer_list *t)
{
struct net *arg = timer_container_of(arg, t, ipv6.ip6_fib_timer);
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index dbc878c09b87..ee7c8f66dc3d 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -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);
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net] ipv6: do not wait for a running gc in ip6_dst_gc()
2026-12-01 1:55 [PATCH net] ipv6: do not wait for a running gc in ip6_dst_gc() Willem de Bruijn
@ 2026-12-01 2:34 ` David Ahern
2026-12-01 15:41 ` willemb
0 siblings, 1 reply; 3+ messages in thread
From: David Ahern @ 2026-12-01 2:34 UTC (permalink / raw)
To: Willem de Bruijn, netdev
Cc: Ido Schimmel, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Chris Mason, stable
On 11/30/26 6:55 PM, Willem de Bruijn wrote:
> From: Chris Mason <mason@kernel.org>
>
> 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: <stable@vger.kernel.org> # 6.18.x: 12eddc685744: ipv6: annotate data-races around sysctl.ip6_rt_gc_interval
> Cc: <stable@vger.kernel.org> # 6.18.x
> Signed-off-by: Chris Mason <mason@kernel.org>
> [willemb: add lockdep_assert_held() and WRITE_ONCE() on ip6_rt_last_gc,
> expand commit message]
> Signed-off-by: Willem de Bruijn <willemb@google.com>
> ---
> include/net/ip6_fib.h | 1 +
> net/ipv6/ip6_fib.c | 44 +++++++++++++++++++++++++++++++++++--------
> net/ipv6/route.c | 4 +++-
> 3 files changed, 40 insertions(+), 9 deletions(-)
>
ipv4 has gc_thresh set to ~0 in its dst_ops. At this point seems like
ipv6 should be able to move in that direction.
Double checking my thinking with claude (sonnet-5.5):
IPv6 already bounds its dst cache the way IPv4 does, so the only code
change is setting the gc_thresh default to ~0 (like IPv4 does).
How IPv4 limits growth (net/ipv4/route.c)
- There is no .gc callback and ipv4_dst_ops.gc_thresh = ~0, so nothing
is enforced by dst count.
- Cached exceptions (fnhe) sit in hash buckets. On insert,
fnhe_remove_oldest() evicts entries once a bucket's depth passes a
randomized FNHE_RECLAIM_DEPTH.
- Everything else is bounded by the number of nexthops.
IPv6 already does the same
- __rt6_insert_exception() caps each bucket at a randomized
FIB6_MAX_DEPTH and evicts with rt6_exception_remove_oldest().
- Per-cpu routes are allocated DST_NOCOUNT and are bounded by nexthops.
- max_size is already INT_MAX.
The exception count per FIB entry is bounded by bucket depth times
FIB6_GC_BUCKET_SIZE, which I didn't check. The bound could still be
large with many nexthops, and the same is true for IPv4.
###
IPv6 has a timer that calls fib6_run_gc every gc_interval seconds to
control for example the RAs.
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net] ipv6: do not wait for a running gc in ip6_dst_gc()
2026-12-01 2:34 ` David Ahern
@ 2026-12-01 15:41 ` willemb
0 siblings, 0 replies; 3+ messages in thread
From: willemb @ 2026-12-01 15:41 UTC (permalink / raw)
To: David Ahern, Willem de Bruijn, netdev
Cc: Ido Schimmel, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Chris Mason, stable
David Ahern wrote:
> On 11/30/26 6:55 PM, Willem de Bruijn wrote:
> > From: Chris Mason <mason@kernel.org>
> >
> > 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: <stable@vger.kernel.org> # 6.18.x: 12eddc685744: ipv6: annotate
> data-races around sysctl.ip6_rt_gc_interval
> > Cc: <stable@vger.kernel.org> # 6.18.x
> > Signed-off-by: Chris Mason <mason@kernel.org>
> > [willemb: add lockdep_assert_held() and WRITE_ONCE() on ip6_rt_last_gc,
> > expand commit message]
> > Signed-off-by: Willem de Bruijn <willemb@google.com>
> > ---
> > include/net/ip6_fib.h | 1 +
> > net/ipv6/ip6_fib.c | 44 +++++++++++++++++++++++++++++++++++--------
> > net/ipv6/route.c | 4 +++-
> > 3 files changed, 40 insertions(+), 9 deletions(-)
> >
> ipv4 has gc_thresh set to ~0 in its dst_ops. At this point seems like
> ipv6 should be able to move in that direction.
Are you suggesting that for stable?
I would suggest that for net-next to indeed make the two stacks more
alike.
But for stable to avoid the behavioral change. Also, allegedly quite
a few tunings set net.ipv6.route.gc_thresh explicitly, so those would
still not be mitigated.
> Double checking my thinking with claude (sonnet-5.5):
> IPv6 already bounds its dst cache the way IPv4 does, so the only code
> change is setting the gc_thresh default to ~0 (like IPv4 does).
> How IPv4 limits growth (net/ipv4/route.c)
> - There is no .gc callback and ipv4_dst_ops.gc_thresh = ~0, so nothing
> is enforced by dst count.
> - Cached exceptions (fnhe) sit in hash buckets. On insert,
> fnhe_remove_oldest() evicts entries once a bucket's depth passes a
> randomized FNHE_RECLAIM_DEPTH.
> - Everything else is bounded by the number of nexthops.
> IPv6 already does the same
> - __rt6_insert_exception() caps each bucket at a randomized
> FIB6_MAX_DEPTH and evicts with rt6_exception_remove_oldest().
> - Per-cpu routes are allocated DST_NOCOUNT and are bounded by nexthops.
> - max_size is already INT_MAX.
> The exception count per FIB entry is bounded by bucket depth times
> FIB6_GC_BUCKET_SIZE, which I didn't check. The bound could still be
> large with many nexthops, and the same is true for IPv4.
> ###
> IPv6 has a timer that calls fib6_run_gc every gc_interval seconds to
> control for example the RAs.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-01 15:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 1:55 [PATCH net] ipv6: do not wait for a running gc in ip6_dst_gc() Willem de Bruijn
2026-12-01 2:34 ` David Ahern
2026-12-01 15:41 ` willemb
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox