From: David Ahern <dsahern@kernel.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, idosch@nvidia.com, davem@davemloft.net,
edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
horms@kernel.org, ralf@mandelbit.com, ja@ssi.bg,
David Ahern <dahern@nvidia.com>
Subject: [PATCH net-next v2 04/13] ipv6: invalidate the failing dst in ip6_link_failure()
Date: Wed, 25 Nov 2026 18:33:30 -0700 [thread overview]
Message-ID: <94591642bbaefaea15cf2aa2e0d5ca4e4de74686.1795642432.git.dahern@nvidia.com> (raw)
In-Reply-To: <cover.1795642432.git.dahern@nvidia.com>
From: David Ahern <dahern@nvidia.com>
When a default route's neighbour fails, ip6_link_failure() writes -1 to
the fn_sernum of the route's node so that users of dsts under that node
look the route up again and let router selection pick another default
router. That invalidates every dst under the node, which for a failing
router that can trigger this for every packet sent through it is far
more than needed.
Invalidate the failing dst only, which for a pcpu dst is one CPU's
copy. Do it by setting the top bit of its sernum: the netns route
generation, which a dst with a non-zero sernum is compared against, is
always positive, so the dst fails the check and its users do the lookup
again, and a pcpu dst that fails it is dropped from its slot and
rebuilt. The rest of sernum is left as it was, and setting the bit is
idempotent, so concurrent invalidators need no lock.
sernum of a published dst can now change under its readers, so read it
with READ_ONCE(), and in rt6_get_cookie() test and return a single
sample. A reader that sees the value before the mark and one that sees
it after both get a non-zero sernum: setting the bit never makes it
zero.
Signed-off-by: David Ahern <dahern@nvidia.com>
---
include/net/ip6_fib.h | 27 +++++++++++++++++++++++++--
net/ipv6/route.c | 18 +++++++++---------
2 files changed, 34 insertions(+), 11 deletions(-)
diff --git a/include/net/ip6_fib.h b/include/net/ip6_fib.h
index 232289b439c3..bfc1895e8591 100644
--- a/include/net/ip6_fib.h
+++ b/include/net/ip6_fib.h
@@ -300,9 +300,14 @@ static inline u32 rt6_get_cookie(const struct rt6_info *rt)
{
struct fib6_info *from;
u32 cookie = 0;
+ int sernum;
- if (rt->sernum)
- return rt->sernum;
+ /* Can be stale-marked concurrently (rt6_dst_invalidate()): test and
+ * return one sample.
+ */
+ sernum = READ_ONCE(rt->sernum);
+ if (sernum)
+ return sernum;
rcu_read_lock();
@@ -315,6 +320,24 @@ static inline u32 rt6_get_cookie(const struct rt6_info *rt)
return cookie;
}
+/* An invalidated dst keeps the generation it was built under in the low
+ * bits, so state derived from it (exceptions) can be stamped with the FIB
+ * state it actually came from. The top bit makes it fail rt6_is_valid():
+ * the netns generation is always positive.
+ */
+#define RT6_SERNUM_STALE INT_MIN
+
+static inline void rt6_dst_invalidate(struct rt6_info *rt)
+{
+ int sernum = READ_ONCE(rt->sernum);
+
+ /* Do not dirty the cache line of a dst that other CPUs read on the
+ * fast path when it is already stale.
+ */
+ if (!(sernum & RT6_SERNUM_STALE))
+ WRITE_ONCE(rt->sernum, sernum | RT6_SERNUM_STALE);
+}
+
static inline void ip6_rt_put(struct rt6_info *rt)
{
/* dst_release() accepts a NULL parameter.
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 52a30a62bcec..39204a0b42d1 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1466,7 +1466,7 @@ static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
static bool rt6_is_valid(const struct rt6_info *rt6)
{
- return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
+ return READ_ONCE(rt6->sernum) == rt_genid_ipv6(dev_net(rt6->dst.dev));
}
/* It should be called with rcu_read_lock() acquired */
@@ -1476,7 +1476,7 @@ static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
- if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
+ if (pcpu_rt && READ_ONCE(pcpu_rt->sernum) && !rt6_is_valid(pcpu_rt)) {
struct rt6_info *prev, **p;
p = this_cpu_ptr(res->nh->rt6i_pcpu);
@@ -2855,7 +2855,7 @@ INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
rt = dst_rt6_info(dst);
- if (rt->sernum)
+ if (READ_ONCE(rt->sernum))
return rt6_is_valid(rt) ? dst : NULL;
rcu_read_lock();
@@ -2912,14 +2912,14 @@ static void ip6_link_failure(struct sk_buff *skb)
rt6_remove_exception_rt(rt);
} else {
struct fib6_info *from;
- struct fib6_node *fn;
+ /* Force sockets using this dst to look the route up
+ * again; a stale-marked sernum never matches the
+ * netns generation.
+ */
from = rcu_dereference(rt->from);
- if (from) {
- fn = rcu_dereference(from->fib6_node);
- if (fn && (rt->rt6i_flags & RTF_DEFAULT))
- WRITE_ONCE(fn->fn_sernum, -1);
- }
+ if (from && (rt->rt6i_flags & RTF_DEFAULT))
+ rt6_dst_invalidate(rt);
}
rcu_read_unlock();
}
--
2.54.0 (Apple Git-157)
next prev parent reply other threads:[~2026-11-26 1:33 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 1:33 [PATCH net-next v2 00/13] ipv6: validate cached dsts against the netns route generation David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 01/13] ipv6: allocate the exception entry before removing the one it replaces David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 02/13] ipv6: keep a superseded pcpu dst working until its holders notice David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 03/13] ipv6: don't insert a PMTU exception whose MTU could not be stored David Ahern
2026-11-26 1:33 ` David Ahern [this message]
2026-11-26 1:33 ` [PATCH net-next v2 05/13] ipv6: tell route dumps about exception inserts with a table sequence David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 06/13] ipv6: validate dsts against the netns route generation David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 07/13] ipv6: make rt_genid_bump_ipv6() a plain counter increment David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 08/13] nexthop: factor out nh_has_f6i() David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 09/13] nexthop: bump the IPv6 route generation on group changes David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 10/13] ipv6: stamp dsts with the generation sampled before the FIB lookup David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 11/13] selftests: net: pmtu: check that a deleted cached exception stays deleted David Ahern
2026-11-26 1:33 ` [PATCH net-next v2 12/13] selftests: net: fib_tests: Add stale pcpu test David Ahern
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=94591642bbaefaea15cf2aa2e0d5ca4e4de74686.1795642432.git.dahern@nvidia.com \
--to=dsahern@kernel.org \
--cc=dahern@nvidia.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=ja@ssi.bg \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=ralf@mandelbit.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox