From: Qiliang Yuan <qiliang.yuan@linux.dev>
To: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>,
Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>,
David Ahern <dsahern@kernel.org>,
Ido Schimmel <idosch@nvidia.com>,
Nikolay Aleksandrov <razor@blackwall.org>
Cc: Kuniyuki Iwashima <kuniyu@google.com>,
Ralf Lici <ralf@mandelbit.com>,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
Qiliang Yuan <qiliang.yuan@linux.dev>
Subject: [PATCH net-next v5 2/4] ipv6: take fib6_sernum from a sequence shared by all netns
Date: Sat, 21 Nov 2026 18:21:20 -0500 [thread overview]
Message-ID: <20261121-bug-net-netns-move-rcu-barrier-v5-2-100a3b5d9090@linux.dev> (raw)
In-Reply-To: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev>
A per-cpu IPv6 route of a nexthop object records fib6_sernum of the
netns it was created in, and rt6_is_valid() compares it with fib6_sernum
of the netns its device belongs to when the route is checked. Every netns
starts its fib6_sernum at 1 and increments it on each tree change, so
different netns often hold the same value.
When a device moves to another netns, such a route remains usable until
nexthop_free_rcu() releases it, and fib6_info_destroy_rcu() queues that
callback from inside an RCU callback, after the device has moved. If the
fib6_sernum of the new netns matches the cached one in that window,
ip6_dst_check() accepts the route and a socket in the old netns keeps
sending through a device that now belongs to another netns.
Take every fib6_sernum bump from one sequence shared by all netns, which
only hands out values from 2 up, and have rt6_is_valid() reject 1, the
value every netns starts with: fib6_add() publishes a route before it
bumps fib6_sernum, so a route looked up in between can record 1. 0 still
means there is no sernum to check. Any other value a route records was
drawn from the sequence, which no other netns holds until it wraps after
2^31 bumps. fib6_new_sernum() callers only hold their own table's
tb6_lock, so bumps of one netns can race. Publish each value with a
cmpxchg on the fib6_sernum of the netns, drawing again if another bump
changed it first. As before, this orders the routing change before the
new value and the value before the nodes stamped with it, and keeps
fib6_sernum monotonic per netns.
A connected UDP socket in netns A caches a route through an IPv6 nexthop
object on a veth. The veth is moved to netns B and brought up with one
RTM_NEWLINK, then the socket sends 200000 datagrams. A route is added to
and removed from B 0 to 20 times before the move, 63 trials:
before after change
trials sending after move (count) 3 0 -100%
datagrams sent after move (count) 6634 0 -100%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
net/ipv6/ip6_fib.c | 28 +++++++++++++++++++++++++++-
net/ipv6/route.c | 6 +++++-
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
index e1d49897d6ab0..71490edb01e99 100644
--- a/net/ipv6/ip6_fib.c
+++ b/net/ipv6/ip6_fib.c
@@ -123,12 +123,38 @@ static void fib6_walkers_skip_rt(struct net *net, struct fib6_info *rt)
fib6_walkers_replace_leaf(net, rt, next);
}
+/*
+ * Draw fib6_sernum bumps from one sequence shared by all netns, so that a
+ * route cached against one netns does not match the sernum of another one,
+ * e.g. after its device moved, until the sequence wraps. Every netns starts
+ * at 1 and the sequence only hands out values from 2 up: 0 means no sernum
+ * to check.
+ */
+static atomic_t fib6_sernum_seq = ATOMIC_INIT(1);
+
+static int fib6_sernum_draw(void)
+{
+ int new, old = atomic_read(&fib6_sernum_seq);
+
+ do {
+ new = old < INT_MAX ? old + 1 : 2;
+ } while (!atomic_try_cmpxchg(&fib6_sernum_seq, &old, new));
+
+ return new;
+}
+
+/*
+ * Publish the drawn value with a cmpxchg on the netns sernum, drawing again if
+ * another bump got there first. The successful cmpxchg orders the routing
+ * change before the new sernum and the new sernum before the nodes stamped
+ * with it, and a stalled bump cannot overwrite a newer value.
+ */
static int fib6_new_sernum(struct net *net)
{
int new, old = atomic_read(&net->ipv6.fib6_sernum);
do {
- new = old < INT_MAX ? old + 1 : 1;
+ new = fib6_sernum_draw();
} while (!atomic_try_cmpxchg(&net->ipv6.fib6_sernum, &old, new));
return new;
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 4a4c5ace63c54..6731665fbb16d 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1466,7 +1466,11 @@ 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));
+ /* Reject the initial sernum, which every netns shares and which a
+ * route can record before its insertion bumps the sernum.
+ */
+ return rt6->sernum != 1 &&
+ rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
}
/* It should be called with rcu_read_lock() acquired */
--
2.43.0
next prev parent reply other threads:[~2026-11-21 23:21 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 23:21 [PATCH net-next v5 0/4] net: make moving a device to another netns cheaper Qiliang Yuan
2026-11-21 23:21 ` [PATCH net-next v5 1/4] ipv4: draw rt_genid from a sequence shared by all netns Qiliang Yuan
2026-11-21 23:21 ` Qiliang Yuan [this message]
2026-11-21 23:21 ` [PATCH net-next v5 3/4] nexthop: release cached routes when a nexthop leaves a group Qiliang Yuan
2026-11-21 23:21 ` [PATCH net-next v5 4/4] net: remove rcu_barrier() from __dev_change_net_namespace() Qiliang Yuan
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=20261121-bug-net-netns-move-rcu-barrier-v5-2-100a3b5d9090@linux.dev \
--to=qiliang.yuan@linux.dev \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=ralf@mandelbit.com \
--cc=razor@blackwall.org \
/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
all inboxes | Powered by JetHome®