Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796090216; b=WU/yBRs/yYdRNx2EsDumdIiyd0pmO47pn+k1e756goTtzpWW/q8m1i6vujVVlKamiP9w igCrp6gdkxyc/LaHV7d7pBxOc/dqUuLlY5lGQ8yN3ZDLZ+4q/tW0H8kgAB2VZM5ha/e60 Mfr7naKKbw3a8ucDENV9GGUN4t6daKz4AqvZcDdF1yFKnf1uUG2Sc78oHvwg2NHPl94s5 BCIPV0GEFYLRW72bVD5N6xI9sDwffScwRNVn7sGyrneM8HAwtHBpHTUQPcopa2eK0C5FH V08yXQYD+A/HM10qJGVQOe2tP3C3lLtNqPWG8hqdyxBpjC872wLRuXHntgkrBcWssTQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796090216; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=jUotYXjfAHEfNOZHruK3NZBovu3CauXIAnE5QGR8qXw=; b=UTInhPGqPa+H275izhQM6Urc1ajZgrDC2JIT5zAIOsK/cR43mqiBBjT6/cyXXElGlOqs Z2seaLd99kQS0Cyr0fFret2Aa7jyQ3OBDJCvseIJG1yE04PcpZOM5l/jCWHsjOqtZCATr k4DajdbS8t//7V/cD/f0ux8rd4b3u7AwV3Yewut77N27KbPpcO7qFk9MBl4Gc7duUwNyw wa49XdHY9YXCrBBW6Rh5E7kdfIAw2oejMiyFZro7GQHxRtlDtmZAxx74Bq5Sy6K2qv9UJ q13S/4uaHFu4fUrjkyX7uXEJ0OKyoxSEd18YXXA0QV5BpxNJpaSgYy/wURAdj7igXvA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--willemb.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=WUGE4piV; arc=none smtp.remote-ip=209.85.222.198 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00464606A31A for ; Tue, 1 Dec 2026 01:56:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 00464606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--willemb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=WUGE4piV Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-93cb7f9bb86so2107038285a.2 for ; Mon, 30 Nov 2026 17:56:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796090215; x=1796695015; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=jUotYXjfAHEfNOZHruK3NZBovu3CauXIAnE5QGR8qXw=; b=WUGE4piVwXaZhKxgEKZxieO9HU+rkMQ66TH5s19WHaahKGlzdJvEtiGpyEZHmm4Au4 pSE9P4FqigAvD14BSREEWOhiW4LNjSH5uF8ONxpvPv2WvIEqtcIi5/N5lZjs8O6Mi8ft f+QmNj80D8glFb56f1dPzBR+P/4BhU/OT51ucbLdX4XpO25196npJORskEtOFUZ6279I VMK0NjjvjvycjHnKOY5/PN2vqqi+MwZC0IAMi0KGng85iB9B9kUrzigO9us4HZX9BWrH DNeX6cGZYZbpuUisjyL8RleiBqSzGjeA+BLLC2wSodO4arLKQKV6kCEHwBNkgmLbnnIM AYDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796090215; x=1796695015; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=jUotYXjfAHEfNOZHruK3NZBovu3CauXIAnE5QGR8qXw=; b=cKOhDwk+ZjF+pA9LPIl1Kqt8M7CujZhcUaIPKSS8KR/z1Ea1dB/etkI0XKQitF7x+t JacDjZCS4wbY0sQQvSBx1nQZEDJwlU1pPpDOv7+MbBd5tKjxvyAALl2WXh8JByP5Bo6X xJXKTFIguOR+akYywH0fpyQKz8H2sxbuOnhFau412ZfI/Qi+KkoUxwrK0elIHNPRr4ph UVDoPKDwruLabJ1Yw8yY9oofOdeEGp03xx8AyqI2Yih8WMpMZi5/FnKjE0LXxjCqL85Y W5X4/2uO2oGP8u0oP68worIp3CT3zN0KrgJ5wGYubqYvZDkDJM5AtoqYkvUz14y7y1YU 9zdA== X-Forwarded-Encrypted: i=1; AL+hErH+y6JaiBfww8HnQipiKY/AS7OIZmlDStcY+lu/vmAW5fAjv4PTk1nM5EL67UBw+mpd3C3k0zY=@vger.kernel.org X-Gm-Message-State: APuU4Z/EyKTbanmDQ93tgIjwXJXQxZjJ8MNkmefsKFazQiwjAZuGAZxe sOXFPsGlPS7xM9fv/cab0RZ4vYqfQPjFIGBySD139FF3sfyPox982P8N+73B4IZ0PO8/TmXwv0t wK8H8Yvg7Cg== X-Received: from qkld23.prod.google.com ([2002:a05:620a:1377:b0:93f:2ede:1069]) (user=willemb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:2b98:b0:939:a199:45b4 with SMTP id af79cd13be357-949ba9a85c1mr6526773585a.39.1796090214631; Mon, 30 Nov 2026 17:56:54 -0800 (PST) Date: Mon, 30 Nov 2026 20:55:51 -0500 X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201015652.38202-1-willemb@google.com> Subject: [PATCH net] ipv6: do not wait for a running gc in ip6_dst_gc() From: Willem de Bruijn To: netdev@vger.kernel.org Cc: David Ahern , Ido Schimmel , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Chris Mason , stable@vger.kernel.org, Willem de Bruijn Content-Type: text/plain; charset="UTF-8" From: Chris Mason 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: # 6.18.x: 12eddc685744: ipv6: annotate data-races around sysctl.ip6_rt_gc_interval Cc: # 6.18.x Signed-off-by: Chris Mason [willemb: add lockdep_assert_held() and WRITE_ONCE() on ip6_rt_last_gc, expand commit message] Signed-off-by: Willem de Bruijn --- 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