* [PATCH net-next v4 1/4] ipv4: draw rt_genid from a sequence shared by all netns
2026-11-18 2:28 [PATCH net-next v4 0/4] net: make moving a device to another netns cheaper Qiliang Yuan
@ 2026-11-18 2:28 ` Qiliang Yuan
2026-11-18 2:28 ` [PATCH net-next v4 2/4] ipv6: take fib6_sernum " Qiliang Yuan
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Qiliang Yuan @ 2026-11-18 2:28 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov
Cc: Kuniyuki Iwashima, netdev, linux-kernel, Qiliang Yuan
An IPv4 route records the rt_genid of the netns it was created in, and
rt_is_expired() compares it with the rt_genid of the netns its device
belongs to when the route is checked. Every netns starts its rt_genid
at 0 and increments it on each cache flush, so different netns often
hold the same value.
When a device moves to another netns, routes cached through it remain
usable until dst_dev_put() points them to blackhole_netdev. For a route
using a nexthop object this happens in nexthop_free_rcu(), which
free_fib_info_rcu() queues from inside an RCU callback, so the
rcu_barrier() in __dev_change_net_namespace() does not wait for it. If
the rt_genid of the new netns matches the cached one in that window,
ipv4_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 rt_genid bump from one sequence shared by all netns, which
never hands out 0, the value every netns starts with. Inserting a route
bumps rt_genid, so the routes a netns caches carry values drawn from the
sequence, which no other netns holds until it wraps after 2^32 bumps. A
moved device sends again only once it is up in its new netns, and
NETDEV_UP flushes the route cache there, so that netns no longer holds 0
either.
Concurrent bumps of one netns can leave the older of two values, so
rt_genid is no longer monotonic per netns. It is only compared for
equality, so this costs at most an extra route lookup.
A connected UDP socket in netns A caches a route through a 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. B's rt_genid is
bumped 0 to 20 times before the move, 63 trials:
before after change
trials sending after move (count) 1 0 -100%
datagrams sent after move (count) 661 0 -100%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
include/net/net_namespace.h | 5 +----
net/ipv4/route.c | 21 +++++++++++++++++++++
2 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index 4a4d72080acdc..11133ba130480 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -558,10 +558,7 @@ static inline int rt_genid_ipv6(const struct net *net)
}
#endif
-static inline void rt_genid_bump_ipv4(struct net *net)
-{
- atomic_inc(&net->ipv4.rt_genid);
-}
+void rt_genid_bump_ipv4(struct net *net);
extern void (*__fib6_flush_trees)(struct net *net);
static inline void rt_genid_bump_ipv6(struct net *net)
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index b9f5397336d34..294c9c278c403 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -393,6 +393,27 @@ static inline int ip_rt_proc_init(void)
}
#endif /* CONFIG_PROC_FS */
+/*
+ * Draw rt_genid bumps from one sequence shared by all netns. A route keeps
+ * the genid of the netns it was created in, and is checked against the netns
+ * its device is in now, so distinct values make that check fail once the
+ * device has moved. Every netns starts at 0, which the sequence never hands
+ * out, and inserting a route bumps the genid, so cached routes carry values
+ * drawn from the sequence.
+ */
+static atomic_t rt_genid_seq;
+
+void rt_genid_bump_ipv4(struct net *net)
+{
+ int genid;
+
+ do {
+ genid = atomic_inc_return(&rt_genid_seq);
+ } while (!genid);
+ atomic_set(&net->ipv4.rt_genid, genid);
+}
+EXPORT_SYMBOL_GPL(rt_genid_bump_ipv4);
+
static inline bool rt_is_expired(const struct rtable *rth)
{
bool res;
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net-next v4 2/4] ipv6: take fib6_sernum from a sequence shared by all netns
2026-11-18 2:28 [PATCH net-next v4 0/4] net: make moving a device to another netns cheaper Qiliang Yuan
2026-11-18 2:28 ` [PATCH net-next v4 1/4] ipv4: draw rt_genid from a sequence shared by all netns Qiliang Yuan
@ 2026-11-18 2:28 ` Qiliang Yuan
2026-11-18 2:28 ` [PATCH net-next v4 3/4] nexthop: release cached routes when a nexthop leaves a group Qiliang Yuan
2026-11-18 2:28 ` [PATCH net-next v4 4/4] net: remove rcu_barrier() from __dev_change_net_namespace() Qiliang Yuan
3 siblings, 0 replies; 5+ messages in thread
From: Qiliang Yuan @ 2026-11-18 2:28 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov
Cc: Kuniyuki Iwashima, netdev, linux-kernel, Qiliang Yuan
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 two tables of one netns can store their values in either
order, and fib6_sernum is no longer monotonic per netns. It is only
compared for equality, so this costs at most an extra invalidation.
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) 2 0 -100%
datagrams sent after move (count) 4399 0 -100%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
net/ipv6/ip6_fib.c | 16 +++++++++++++---
net/ipv6/route.c | 6 +++++-
2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
index e1d49897d6ab0..827051cbf7482 100644
--- a/net/ipv6/ip6_fib.c
+++ b/net/ipv6/ip6_fib.c
@@ -123,13 +123,23 @@ 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_new_sernum(struct net *net)
{
- int new, old = atomic_read(&net->ipv6.fib6_sernum);
+ int new, old = atomic_read(&fib6_sernum_seq);
do {
- new = old < INT_MAX ? old + 1 : 1;
- } while (!atomic_try_cmpxchg(&net->ipv6.fib6_sernum, &old, new));
+ new = old < INT_MAX ? old + 1 : 2;
+ } while (!atomic_try_cmpxchg(&fib6_sernum_seq, &old, new));
+ atomic_set(&net->ipv6.fib6_sernum, 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
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net-next v4 3/4] nexthop: release cached routes when a nexthop leaves a group
2026-11-18 2:28 [PATCH net-next v4 0/4] net: make moving a device to another netns cheaper Qiliang Yuan
2026-11-18 2:28 ` [PATCH net-next v4 1/4] ipv4: draw rt_genid from a sequence shared by all netns Qiliang Yuan
2026-11-18 2:28 ` [PATCH net-next v4 2/4] ipv6: take fib6_sernum " Qiliang Yuan
@ 2026-11-18 2:28 ` Qiliang Yuan
2026-11-18 2:28 ` [PATCH net-next v4 4/4] net: remove rcu_barrier() from __dev_change_net_namespace() Qiliang Yuan
3 siblings, 0 replies; 5+ messages in thread
From: Qiliang Yuan @ 2026-11-18 2:28 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov
Cc: Kuniyuki Iwashima, netdev, linux-kernel, Qiliang Yuan
When a nexthop is removed while other members keep its group alive, only
the group array changes: the routes through the group stay in the FIB,
and the dsts they cached through the removed nexthop are released when
that nexthop is freed, from an RCU callback.
Until then those dsts stay valid. IPv6 route exceptions, such as the
ones created on Packet Too Big, are checked against the cookie of their
FIB node, which does not change, so a socket keeps sending through the
removed nexthop. When the nexthop goes away with its device, removing
the device's IPv6 addresses usually flushes the routing tree first, but
not when the device carries no IPv6 address.
Once all readers see the new group arrays, release the per-cpu routes and
exceptions of a removed IPv6 nexthop, as replacing a group already does
for its old members, and flush the IPv4 route cache once if any of the
groups carries IPv4 routes. The routes using the nexthop directly are
already gone at that point.
A connected UDP socket in netns A sends to a route through group {nh 1,
nh 2}, on an IPv6 exception of nh 1 created by Packet Too Big, while nh 1
is deleted. Datagrams still sent through nh 1 after the new group is
published and before nh 1 is freed, 10 trials, median and range, the
ones left after the change being sent before the dsts are released:
before (datagrams) after (datagrams) change
per trial 1140 (367-3788) 1 (1-3) -99.9%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
net/ipv4/nexthop.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
index 42e55b5a755ee..3fccd4f2f916b 100644
--- a/net/ipv4/nexthop.c
+++ b/net/ipv4/nexthop.c
@@ -2116,6 +2116,7 @@ remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
struct nh_grp_entry *nhge, *tmp;
LIST_HEAD(deferred_free);
bool need_flush = false;
+ struct nh_info *nhi;
/* If there is nothing to do, let's avoid the costly call to
* synchronize_net()
@@ -2130,6 +2131,20 @@ remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
/* make sure all see the newly published array before releasing rtnl */
synchronize_net();
+ /* Routes through the groups can still hold dsts cached through @nh,
+ * which are only released when @nh is freed. Release them now that
+ * no reader can pick @nh from the old groups any more.
+ */
+ nhi = rtnl_dereference(nh->nh_info);
+ if (nhi->family == AF_INET6)
+ fib6_nh_release_dsts(&nhi->fib6_nh);
+ list_for_each_entry(nhge, &deferred_free, nh_list) {
+ if (!list_empty(&nhge->nh_parent->fi_list)) {
+ rt_cache_flush(net);
+ break;
+ }
+ }
+
/* Now safe to free percpu stats — all RCU readers have finished */
list_for_each_entry_safe(nhge, tmp, &deferred_free, nh_list) {
list_del(&nhge->nh_list);
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net-next v4 4/4] net: remove rcu_barrier() from __dev_change_net_namespace()
2026-11-18 2:28 [PATCH net-next v4 0/4] net: make moving a device to another netns cheaper Qiliang Yuan
` (2 preceding siblings ...)
2026-11-18 2:28 ` [PATCH net-next v4 3/4] nexthop: release cached routes when a nexthop leaves a group Qiliang Yuan
@ 2026-11-18 2:28 ` Qiliang Yuan
3 siblings, 0 replies; 5+ messages in thread
From: Qiliang Yuan @ 2026-11-18 2:28 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov
Cc: Kuniyuki Iwashima, netdev, linux-kernel, Qiliang Yuan
Commit 6549dd43c043 ("net: dev: fix the incorrect hold of net
namespace's lo device") added rcu_barrier() before
NETDEV_UNREGISTER_FINAL in dev_change_net_namespace(), so that dst
entries released by RCU callbacks were on the dst garbage list before
dst_ifdown() moved them to the loopback device of the old netns.
Commit 070f2d7e264a ("net: Drop NETDEV_UNREGISTER_FINAL") removed the
event but kept the barrier, and dead dst entries now point to
blackhole_netdev instead of a loopback device.
The barrier waits for the RCU callbacks pending on every CPU while RTNL
is held, which takes a full grace period on each netns move. Container
and sandbox managers move one end of a veth pair for every new network
namespace, so this limits how fast such namespaces can be created on the
whole system, regardless of how many threads create them.
Remove the barrier. The RCU callbacks queued by NETDEV_UNREGISTER
handlers free memory and drop device references, which the move does not
wait for. Routes cached through the moved device are rejected once its
netns changes: IPv4 and per-cpu IPv6 routes no longer match the rt_genid
or fib6_sernum of another netns, and the per-cpu routes and exceptions
of a nexthop that leaves a group were already released when it left.
Creating 400 netns, each with a veth pair whose host end is moved back
to the initial netns, a tap device, addresses and routes, in a guest
with 4 CPUs online, median of 5 runs:
before (netns/s) after (netns/s) change
1 thread 30 32 +6.7%
4 threads 40 48 +20%
16 threads 69 110 +59%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
net/core/dev.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/net/core/dev.c b/net/core/dev.c
index 3a520c798d4a8..8a899e10e91c6 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -13153,7 +13153,6 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net,
* the device is just moving and can keep their slaves up.
*/
call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
- rcu_barrier();
new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread