* [PATCH net-next v5 1/4] ipv4: draw rt_genid from a sequence shared by all netns
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 ` Qiliang Yuan
2026-11-22 9:02 ` Ralf Lici
2026-11-23 0:25 ` netdev-bot+sashiko
2026-11-21 23:21 ` [PATCH net-next v5 2/4] ipv6: take fib6_sernum " Qiliang Yuan
` (3 subsequent siblings)
4 siblings, 2 replies; 12+ messages in thread
From: Qiliang Yuan @ 2026-11-21 23:21 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov
Cc: Kuniyuki Iwashima, Ralf Lici, 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. Publish each value with a cmpxchg on the rt_genid of the netns,
drawing again if another bump changed it first, so that a bump stalled
between drawing and publishing cannot overwrite a newer value and
rt_genid stays monotonic per netns.
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) 2 0 -100%
datagrams sent after move (count) 1045 0 -100%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
include/net/net_namespace.h | 5 +----
net/ipv4/route.c | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 37 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 d0930ae4c5ae6..ece931f7d9e4d 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -393,6 +393,42 @@ 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;
+
+static int rt_genid_draw(void)
+{
+ int genid;
+
+ do {
+ genid = atomic_inc_return(&rt_genid_seq);
+ } while (!genid);
+
+ return genid;
+}
+
+/*
+ * Publish the drawn value with a cmpxchg on the netns genid, drawing again if
+ * another bump got there first, so that a stalled bump never overwrites a
+ * newer value and each bump is ordered after the ones it replaces.
+ */
+void rt_genid_bump_ipv4(struct net *net)
+{
+ int genid, old = atomic_read(&net->ipv4.rt_genid);
+
+ do {
+ genid = rt_genid_draw();
+ } while (!atomic_try_cmpxchg(&net->ipv4.rt_genid, &old, 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] 12+ messages in thread* Re: [PATCH net-next v5 1/4] ipv4: draw rt_genid from a sequence shared by all netns
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-22 9:02 ` Ralf Lici
2026-11-23 0:25 ` netdev-bot+sashiko
1 sibling, 0 replies; 12+ messages in thread
From: Ralf Lici @ 2026-11-22 9:02 UTC (permalink / raw)
To: Qiliang Yuan
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov,
Kuniyuki Iwashima, netdev, linux-kernel
On Sat, 21 Nov 2026 18:21:19 -0500, Qiliang Yuan <qiliang.yuan@linux.dev> wrote:
> 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. Publish each value with a cmpxchg on the rt_genid of the netns,
> drawing again if another bump changed it first, so that a bump stalled
> between drawing and publishing cannot overwrite a newer value and
> rt_genid stays monotonic per netns.
>
> 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) 2 0 -100%
> datagrams sent after move (count) 1045 0 -100%
>
> Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
Reviewed-by: Ralf Lici <ralf@mandelbit.com>
--
Ralf Lici
Mandelbit Srl
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH net-next v5 1/4] ipv4: draw rt_genid from a sequence shared by all netns
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-22 9:02 ` Ralf Lici
@ 2026-11-23 0:25 ` netdev-bot+sashiko
1 sibling, 0 replies; 12+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 0:25 UTC (permalink / raw)
To: qiliang.yuan
Cc: davem, edumazet, kuba, pabeni, horms, dsahern, idosch, razor,
kuniyu, ralf, netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] rt_genid_bump_ipv4() in net/ipv4/route.c can return without
changing net->ipv4.rt_genid, so a requested route-cache flush is…
- [Low] The commit message describes a netns-isolation bug in the current
tree, but the patch has no Fixes: tag and no Cc: stable.
- [Low] The fix relies on every rt_genid value being unique across netns,
and that only holds until the global 32-bit rt_genid_seq wraps.
--
ipv4: draw rt_genid from a sequence shared by all netns
This patch replaces the per-netns atomic_inc() of the IPv4 rt_genid with
values drawn from one global sequence shared by all netns. That sequence
never returns 0. Each drawn value is published with a cmpxchg on the netns
rt_genid, so a route cached in one netns cannot pass rt_is_expired() after
its device moves to a netns whose rt_genid happens to match.
> 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.
[Severity: Low]
This describes a netns isolation bug in the current tree, but the patch has
no Fixes: tag and no Cc: stable. Should it have them?
The window does exist before this patch. free_fib_info_rcu() runs as an
RCU callback, and from there the nexthop release is queued as a second,
nested callback:
free_fib_info_rcu()
nexthop_put(fi->nh)
call_rcu_hurry(&nh->rcu, nexthop_free_rcu)
The rcu_barrier() in __dev_change_net_namespace() does not wait for that
nested callback.
The likely origin is the change that let fib_info use nexthop objects.
Could the exact commit be identified and cited, so stable maintainers know
which kernels need this?
> diff --git a/net/ipv4/route.c b/net/ipv4/route.c
> index d0930ae4c5ae6..ece931f7d9e4d 100644
> --- a/net/ipv4/route.c
> +++ b/net/ipv4/route.c
> @@ -393,6 +393,42 @@ 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;
[Severity: Low]
Values from this sequence are unique only until the 32-bit rt_genid_seq
wraps. The commit message says so ("until it wraps after 2^32 bumps"), but
it doesn't mention that an unprivileged user can now force the wrap. Is
that intended?
sysctl_route_net_init() keeps the flush sysctl for netns owned by non-init
user namespaces:
net/ipv4/route.c:sysctl_route_net_init() {
...
if (net->user_ns != &init_user_ns) {
if (tbl[0].procname != ipv4_route_flush_procname)
table_size = 0;
}
...
}
After unshare -Urn, each write to /proc/sys/net/ipv4/route/flush takes
this path:
ipv4_sysctl_rtcache_flush()->rt_cache_flush()->rt_genid_bump_ipv4()
Each write advances the global rt_genid_seq, and this is cheap to repeat.
After a wrap, the genid that netns B draws can equal the genid cached on
routes in netns A. One example is the draw at NETDEV_UP after a device
move. A netns can also draw back the value of an older generation that was
invalidated, for example by an xfrm policy insertion. Either case lets
rt_is_expired() accept a stale route again.
This is hard to hit on purpose. rt_genid is not visible to userspace, so a
collision is roughly a 1 in 2^32 exact match. Per-netns 32-bit counters
could also be driven by unprivileged users before this patch.
It matters more once the later patch in the series, "net: remove
rcu_barrier() from __dev_change_net_namespace()", removes the barrier.
After that, genid uniqueness is the only check that rejects IPv4 routes
cached through a moved device.
Would a 64-bit generation be worth considering here? If not, could the
comment and commit message say that unprivileged users can drive the wrap?
> +
> +static int rt_genid_draw(void)
> +{
> + int genid;
> +
> + do {
> + genid = atomic_inc_return(&rt_genid_seq);
> + } while (!genid);
> +
> + return genid;
> +}
> +
> +/*
> + * Publish the drawn value with a cmpxchg on the netns genid, drawing again if
> + * another bump got there first, so that a stalled bump never overwrites a
> + * newer value and each bump is ordered after the ones it replaces.
> + */
> +void rt_genid_bump_ipv4(struct net *net)
> +{
> + int genid, old = atomic_read(&net->ipv4.rt_genid);
> +
> + do {
> + genid = rt_genid_draw();
> + } while (!atomic_try_cmpxchg(&net->ipv4.rt_genid, &old, genid));
[Severity: Medium]
Can this bump return without changing net->ipv4.rt_genid?
rt_genid_draw() rejects only 0. Nothing compares the drawn value with the
current genid of the netns.
Say netns A last drew V. Other netns then advance the shared rt_genid_seq
until it wraps back to V, and A's next bump draws V again. In that case
atomic_try_cmpxchg() runs with old == genid == V. It succeeds, the loop
exits, and A's rt_genid stays at V.
The old atomic_inc() always changed the value. Now the requested flush is
silently lost, and rt_is_expired() keeps accepting routes cached before it
until the next bump.
Callers that depend on the flush include:
- fib_trie route insert and delete
- rt_cache_flush() from the NETDEV_UP/NETDEV_DOWN and address handlers
- xfrm policy insertion in xfrm_policy.c
- rt_genid_bump_all() on SELinux policy reload
Would redrawing while genid == old avoid this? For example:
do {
do {
genid = rt_genid_draw();
} while (genid == old);
} while (!atomic_try_cmpxchg(&net->ipv4.rt_genid, &old, genid));
> +}
> +EXPORT_SYMBOL_GPL(rt_genid_bump_ipv4);
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090%40linux.dev
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH net-next v5 2/4] ipv6: take fib6_sernum from a sequence shared by all netns
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
2026-11-22 9:03 ` Ralf Lici
2026-11-23 0:25 ` netdev-bot+sashiko
2026-11-21 23:21 ` [PATCH net-next v5 3/4] nexthop: release cached routes when a nexthop leaves a group Qiliang Yuan
` (2 subsequent siblings)
4 siblings, 2 replies; 12+ messages in thread
From: Qiliang Yuan @ 2026-11-21 23:21 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov
Cc: Kuniyuki Iwashima, Ralf Lici, 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 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
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH net-next v5 2/4] ipv6: take fib6_sernum from a sequence shared by all netns
2026-11-21 23:21 ` [PATCH net-next v5 2/4] ipv6: take fib6_sernum " Qiliang Yuan
@ 2026-11-22 9:03 ` Ralf Lici
2026-11-23 0:25 ` netdev-bot+sashiko
1 sibling, 0 replies; 12+ messages in thread
From: Ralf Lici @ 2026-11-22 9:03 UTC (permalink / raw)
To: Qiliang Yuan
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov,
Kuniyuki Iwashima, netdev, linux-kernel
On Sat, 21 Nov 2026 18:21:20 -0500, Qiliang Yuan <qiliang.yuan@linux.dev> wrote:
> 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>
Reviewed-by: Ralf Lici <ralf@mandelbit.com>
Thanks for picking this up.
--
Ralf Lici
Mandelbit Srl
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH net-next v5 2/4] ipv6: take fib6_sernum from a sequence shared by all netns
2026-11-21 23:21 ` [PATCH net-next v5 2/4] ipv6: take fib6_sernum " Qiliang Yuan
2026-11-22 9:03 ` Ralf Lici
@ 2026-11-23 0:25 ` netdev-bot+sashiko
1 sibling, 0 replies; 12+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 0:25 UTC (permalink / raw)
To: qiliang.yuan
Cc: davem, edumazet, kuba, pabeni, horms, dsahern, idosch, razor,
kuniyu, ralf, netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] Once the shared fib6_sernum_seq has wrapped, a bump in
fib6_new_sernum() (net/ipv6/ip6_fib.c) can set a netns' fib6_sernum to…
- [Low] The commit message has no Fixes: tag and does not mention stable.
--
ipv6: take fib6_sernum from a sequence shared by all netns
This patch makes fib6_new_sernum() draw every fib6_sernum bump from one
sequence shared by all netns. The sequence hands out values from 2 to
INT_MAX and then wraps to 2. The goal is that a per-cpu nexthop route
cached against one netns no longer matches the sernum of another netns
after its device moves. rt6_is_valid() also now rejects sernum 1, the
value every netns starts with.
> 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)
[ ... ]
> +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;
> +}
[ ... ]
> 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;
[Severity: Medium]
Once fib6_sernum_seq has wrapped, can fib6_new_sernum() publish the value
the netns already holds?
Suppose a netns publishes V and then makes no more bumps. Meanwhile other
netns make about 2^31 - 2 bumps through route add/del, PMTU exceptions or
flushes. The next draw for this netns can then return V again:
fib6_new_sernum()
old = atomic_read(&net->ipv6.fib6_sernum); /* V */
new = fib6_sernum_draw(); /* V again after the wrap */
atomic_try_cmpxchg(&net->ipv6.fib6_sernum, &old, new); /* succeeds */
The callers then write V onto nodes that already hold V:
- fib6_add() via __fib6_update_sernum_upto_root()
- fib6_update_sernum_upto_root()
- fib6_update_sernum() from rt6_insert_exception()
- fib6_flush_trees() via __fib6_clean_all()
So the bump has no effect.
Would cookie-validated dsts then still pass fib6_check() and rt6_check(),
and would nexthop per-cpu dsts with sernum V still pass rt6_is_valid()?
If so, sockets would keep stale routes after a new more-specific route,
an exception insert or a flush, until the next real bump.
The old formula (old < INT_MAX ? old + 1 : 1) could never return a value
equal to old. Would drawing again while new == old be enough to close
this?
> 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));
> }
[Severity: Low]
This isn't a bug, but should the commit message carry a Fixes: tag, and
possibly a Cc: stable line?
The comparison in rt6_is_valid() and this line in ip6_rt_pcpu_alloc()
both come from commit 8f34e53b60b3 ("ipv6: Use global sernum for dst
validation with nexthop objects"):
pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
That line stores the sernum of the creating netns on per-cpu nexthop
routes.
Without Fixes: 8f34e53b60b3, stable backporters and automated stable
selection cannot tell which kernels need this fix.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090%40linux.dev
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH net-next v5 3/4] nexthop: release cached routes when a nexthop leaves a group
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 ` [PATCH net-next v5 2/4] ipv6: take fib6_sernum " Qiliang Yuan
@ 2026-11-21 23:21 ` Qiliang Yuan
2026-11-23 0:25 ` netdev-bot+sashiko
2026-11-21 23:21 ` [PATCH net-next v5 4/4] net: remove rcu_barrier() from __dev_change_net_namespace() Qiliang Yuan
2026-11-23 7:02 ` [PATCH net-next v5 0/4] net: make moving a device to another netns cheaper Qiliang Yuan
4 siblings, 1 reply; 12+ messages in thread
From: Qiliang Yuan @ 2026-11-21 23:21 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov
Cc: Kuniyuki Iwashima, Ralf Lici, 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 2832 (390-3805) 1 (1-6) -99.96%
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] 12+ messages in thread* Re: [PATCH net-next v5 3/4] nexthop: release cached routes when a nexthop leaves a group
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-23 0:25 ` netdev-bot+sashiko
0 siblings, 0 replies; 12+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 0:25 UTC (permalink / raw)
To: qiliang.yuan
Cc: davem, edumazet, kuba, pabeni, horms, dsahern, idosch, razor,
kuniyu, ralf, netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The patch adds IPv6 dst release to remove_nexthop_from_groups(),
but it skips the IPv6 invalidation step that the group-replace path…
- [Medium] The same stale-dst window remains when a group member is
replaced in place (RTM_NEWNEXTHOP with NLM_F_REPLACE on a non-group…
- [Low] The commit describes user-visible behaviour (a socket keeps
sending through a deleted group member until the nexthop is freed from…
--
nexthop: release cached routes when a nexthop leaves a group
This patch changes remove_nexthop_from_groups() so that, once the new
group arrays are published, it releases the per-cpu routes and exceptions
of a removed IPv6 nexthop. It also flushes the IPv4 route cache if any of
the affected groups carries IPv4 routes. The aim is to stop sockets from
sending through a nexthop that has left a surviving group while they wait
for that nexthop to be freed from RCU.
> 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.
[ ... ]
> Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
[Severity: Low]
This isn't a bug, but the commit message describes a user-visible
behaviour: a socket keeps sending through a deleted group member until
the nexthop is freed. There is no Fixes: tag, though.
Should one be added if this is meant as a fix? Or is the patch only
net-next groundwork for the rest of the series?
> 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
[ ... ]
> @@ -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);
[Severity: High]
Does this also need an rt_genid_bump_ipv6()? The commit message says this
works "as replacing a group already does for its old members". However,
nh_rt_cache_flush() bumps the IPv6 genid before it releases the old dsts:
net/ipv4/nexthop.c:nh_rt_cache_flush() {
...
if (have_f6i)
rt_genid_bump_ipv6(net);
...
if (nhi->family == AF_INET6)
fib6_nh_release_dsts(&nhi->fib6_nh);
...
}
Without the bump, can the released per-cpu dsts still pass
ip6_dst_check()? A per-cpu route of a group route has a non-zero sernum.
That means ip6_dst_check() returns early and never looks at
dst->obsolete:
net/ipv6/route.c:ip6_dst_check() {
...
if (rt->sernum)
return rt6_is_valid(rt) ? dst : NULL;
...
}
rt6_is_valid() compares the sernum against
rt_genid_ipv6(dev_net(rt6->dst.dev)). fib6_nh_release_dsts() calls
dst_dev_put(), which does two things to the dst:
- it moves the dst to blackhole_netdev, which lives in init_net
- it sets dst->output to dst_discard_out
In init_net, the sernum still matches if no other IPv6 FIB change has
happened since the per-cpu route was created. Take a connected UDP
socket with no bound oif. Would it keep the dead dst through
ip6_sk_dst_check() and silently drop every datagram? It would never look
the route up again and move to the remaining members.
Before the patch the same thing happened one grace period later, in
nexthop_free_rcu(). The patch makes it happen immediately. The test in
the commit message ran in a non-init netns on a PMTU exception (sernum
0), so it would not show this.
There is a second case. When the nexthop has no gateway, ip6_pol_route()
builds uncached rt6_infos for FLOWI_FLAG_KNOWN_NH lookups. These users
take that path:
- IPVS DR mode, through __ip_vs_route_output_v6() and
__ip_vs_dst_check() with a stored cookie
- nf_dup_ipv6
- raw hdrincl
Those dsts are checked by rt6_dst_from_check() -> fib6_check() against
the group f6i and the cookie. fib6_nh_release_dsts() never reaches them,
and the node sernum does not change. Can they keep sending through the
removed nexthop with no time limit?
Would it cover both cases to bump the IPv6 genid when any
nhge->nh_parent->f6i_list is non-empty, like the fi_list check below?
> + list_for_each_entry(nhge, &deferred_free, nh_list) {
> + if (!list_empty(&nhge->nh_parent->fi_list)) {
> + rt_cache_flush(net);
> + break;
> + }
> + }
[Severity: Medium]
Is the same stale-dst window still open when a group member is replaced
in place? One example is "ip nexthop replace id N ...", where N belongs
to a multipath group and no route uses N directly.
replace_nexthop_single() swaps nh_info. After the swap, the old
fib6_nh/fib_nh and their cached dsts belong to the temporary new
nexthop:
net/ipv4/nexthop.c:replace_nexthop_single() {
...
rcu_assign_pointer(old->nh_info, newi);
rcu_assign_pointer(new->nh_info, oldi);
...
}
replace_nexthop() then does:
net/ipv4/nexthop.c:replace_nexthop() {
...
nh_rt_cache_flush(net, old, new);
WARN_ON_ONCE(__remove_nexthop(net, new, NULL));
nexthop_put(new);
...
}
nh_rt_cache_flush() only checks old->fi_list and old->f6i_list. Those
lists hold routes that use old directly, not routes through the groups.
The function then returns at !replaced_nh->is_group without releasing
the old IPv6 dsts.
__remove_nexthop(net, new, NULL) does reach remove_nexthop_from_groups().
But new->grp_list is empty, so it returns early and the new release code
never runs. The per-group loop in replace_nexthop_single() only sends
hardware notifications.
So the per-cpu IPv6 dsts, the IPv6 exceptions and the IPv4 rtables built
on the old nh_info would stay valid until nexthop_free_rcu() frees new.
Should this path get the same treatment?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090%40linux.dev
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH net-next v5 4/4] net: remove rcu_barrier() from __dev_change_net_namespace()
2026-11-21 23:21 [PATCH net-next v5 0/4] net: make moving a device to another netns cheaper Qiliang Yuan
` (2 preceding siblings ...)
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 ` Qiliang Yuan
2026-11-23 0:25 ` netdev-bot+sashiko
2026-11-23 7:02 ` [PATCH net-next v5 0/4] net: make moving a device to another netns cheaper Qiliang Yuan
4 siblings, 1 reply; 12+ messages in thread
From: Qiliang Yuan @ 2026-11-21 23:21 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov
Cc: Kuniyuki Iwashima, Ralf Lici, 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 27 31 +15%
4 threads 37 48 +30%
16 threads 67 103 +54%
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 1a63f6b939f70..88dcd30f59d22 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -13168,7 +13168,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] 12+ messages in thread* Re: [PATCH net-next v5 4/4] net: remove rcu_barrier() from __dev_change_net_namespace()
2026-11-21 23:21 ` [PATCH net-next v5 4/4] net: remove rcu_barrier() from __dev_change_net_namespace() Qiliang Yuan
@ 2026-11-23 0:25 ` netdev-bot+sashiko
0 siblings, 0 replies; 12+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 0:25 UTC (permalink / raw)
To: qiliang.yuan
Cc: davem, edumazet, kuba, pabeni, horms, dsahern, idosch, razor,
kuniyu, ralf, netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] Removing rcu_barrier() from __dev_change_net_namespace()
(net/core/dev.c, right after…
- [Medium] net: race in __dev_change_net_namespace() leaves moved dev on
xfrm_dst
- [Medium] net: missing dst_dev_put() wait in
__dev_change_net_namespace()
--
net: remove rcu_barrier() from __dev_change_net_namespace()
This patch removes the rcu_barrier() that follows the NETDEV_UNREGISTER
notifier call in __dev_change_net_namespace(). The commit message says the
barrier is left over from NETDEV_UNREGISTER_FINAL handling. It also says
routes cached through the moved device are rejected by the rt_genid and
fib6_sernum checks once the device's netns changes.
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 1a63f6b939f70..88dcd30f59d22 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -13168,7 +13168,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);
>
[Severity: Medium]
Is there still anything that makes dst_dev_put() run on the old
netns's cached routes before the device is usable in the new netns?
For IPv4 routes with fi->nh == NULL, the per-cpu and exception routes
are only released from free_fib_info_rcu(), through fib_nh_release()
and fib_nh_common_release(). For IPv6 routes with f6i->nh == NULL,
fib6_drop_pcpu_from() clears pcpu_rt->from at purge time but does not
call dst_dev_put(). That only happens in fib6_info_destroy_rcu(),
through fib6_nh_release() and fib6_nh_release_dsts(). Both callbacks
are queued from NETDEV_UNREGISTER. The rcu_barrier() removed here was
what made them run before dev_net_set().
The synchronize_net() at the end of __dev_change_net_namespace() only
waits for a grace period, not for those callbacks to be invoked. So
they can still be pending after the move returns and the device has
been brought up in the new netns.
Until they run, dst->output is still ip_output() or ip6_output()
rather than dst_discard_out. dst->dev_rcu also still points at the
moved device. Some users never go through dst_check(). A corked UDP
or RAW socket in the old netns keeps the dst it got in ip_setup_cork()
or ip6_setup_cork(). On uncork, udp_push_pending_frames() reaches
__ip_make_skb(), which moves cork->dst onto the skb, and then
ip_send_skb() and dst_output(). The corked packet would then go out
through the device in the new netns.
Socket offload setup has the same problem. get_netdev_for_sock() (used
by tls_set_device_offload() and tls_set_device_offload_rx()),
net_devmem_get_sock_binding(), smc_clc_prfx_set(),
smc_clc_prfx_match(), smc_vlan_by_tcpsk() and
smc_pnet_find_roce_resource() read dst_dev_rcu(__sk_dst_get(sk))
without sk_dst_check(). In this window they would bind to the moved
device rather than blackhole_netdev.
The window is narrow. The callbacks have to still be pending after the
final synchronize_net(), and the device has to be up in the new netns.
Still, the commit message reasons only from the rt_genid and
fib6_sernum checks, and none of these paths use them. Can the barrier
be removed without first releasing these dsts some other way? For
example, would a synchronous dst_dev_put() at NETDEV_UNREGISTER work
when the device is moving?
[Severity: Medium]
Can an xfrm bundle still be built on the moved device after this?
ip_route_output_flow() and ip6_dst_lookup_flow() take a reference on
the base route and drop rcu_read_lock() before calling
xfrm_lookup_route(). xfrm_policy_queue_process() also passes
xfrm_dst_path(skb_dst(skb)) for skbs on pq->hold_queue straight to
xfrm_lookup(), without a dst_check(). None of these callers sit inside
an RCU read section, so the synchronize_net() before dev_shutdown()
does not wait for them.
For a normal FIB route, dst_dev_put() on the cached per-cpu routes
only happens from free_fib_info_rcu() and fib6_info_destroy_rcu().
NETDEV_UNREGISTER queues both with call_rcu(). The removed
rcu_barrier() made sure those callbacks had pointed the dsts at
blackhole_netdev before dev_net_set(dev, net) and NETDEV_REGISTER.
Without the barrier, NETDEV_REGISTER in the target netns can run
ipv6_add_dev() and install a new inet6_dev in dev->ip6_ptr while the
callbacks are still pending. Suppose xfrm_bundle_create() or
xfrm_create_dummy_bundle() runs in that window:
net/xfrm/xfrm_policy.c:xfrm_bundle_create() {
...
dev = dst_dev_rcu(dst);
if (!dev || !netif_is_alive(dev)) {
...
}
dst_dev_rcu(dst) still returns the moved device. netif_is_alive() is
true because reg_state stays NETREG_REGISTERED during the move.
xfrm4_fill_dst() and xfrm6_fill_dst() then netdev_hold() the device.
xfrm6_fill_dst() also takes in6_dev_get(dev), which is now the target
netns's inet6_dev. The xfrm_dst then goes onto rt_uncached_list or
rt6_uncached_list, but rt_flush_dev() and
rt6_uncached_list_flush_dev() have already run for this move.
Would this leave an xfrm_dst in the old netns that sends or queues
packets through the device in its new netns? It would also hold device
and inet6_dev references that the flush for this move already missed.
The commit message argues from the rt_genid and fib6_sernum checks.
These paths, though, hold the base route across the move and do not
recheck it before building the bundle. Was the xfrm case considered?
A plain synchronize_net() in place of the barrier would not cover
this. It waits for readers, not for the queued callbacks to be
invoked. Is there something else that orders free_fib_info_rcu() and
fib6_info_destroy_rcu() before NETDEV_REGISTER in the new netns?
[Severity: Medium]
Without this barrier, is there still a grace period between the IPv4
teardown in the old netns and dev_net_set(dev, net) plus NETDEV_REGISTER?
Only two waits remain. One is the synchronize_net() before dev_shutdown(),
which runs before the notifiers. The other is the synchronize_net() at the
end, which runs after the switch.
An IPv4 output lookup in the old netns that starts after the first
synchronize_net() can still pick the moving device as dev_out. One way in is
ip_route_output_key_hash_rcu() with saddr set, oif 0 and a multicast or
limited broadcast daddr:
net/ipv4/route.c:ip_route_output_key_hash_rcu() {
...
if (fl4->flowi4_oif == 0 &&
(ipv4_is_multicast(fl4->daddr) ||
ipv4_is_lbcast(fl4->daddr))) {
/* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
dev_out = __ip_dev_find(net, fl4->saddr, false);
...
}
The device's addresses stay in the hash until inetdev_destroy() runs at
NETDEV_UNREGISTER.
A host-scope route through the device is another way in.
fib_create_info() sets fib_nh_scope = RT_SCOPE_NOWHERE for it, and
fib_sync_down_dev() with force == false skips that scope at NETDEV_DOWN.
Suppose that reader is preempted under PREEMPT_RCU, or stalled some other
way, until after dev_net_set(). It then resumes in __mkroute_output() and
calls rt_dst_alloc(), which takes the genid from the device's current
netns:
net/ipv4/route.c:rt_dst_alloc() {
...
rt->rt_genid = rt_genid_ipv4(dev_net(dev));
...
}
At that point this is the new netns's genid. On the multicast/broadcast
path fi is NULL, so the route goes onto the uncached list. rt_flush_dev()
has already run for the old netns by then.
The validity check reads the same netns:
net/ipv4/route.c:rt_is_expired() {
...
res = rth->rt_genid != rt_genid_ipv4(dev_net_rcu(rth->dst.dev));
...
}
So ipv4_dst_check() would accept the dst. Can a socket in the old netns
then hold a dst that passes the check while its device belongs to the new
netns? That would last until the new netns bumps rt_genid through
NETDEV_UP, an address add or a route insert.
Between dev_activate() and the NETDEV_UP flush, packets would go out
through the device in the other netns.
The shared genid sequence from "ipv4: draw rt_genid from a sequence shared
by all netns" doesn't seem to help here, because the stored value really is
the new netns's genid.
The old rcu_barrier() covered this case. NETDEV_UNREGISTER always queues
RCU callbacks (neigh_parms_release(), inet_free_ifa()), and waiting for
them needs a grace period that covers the stalled reader.
Given that, does this part of the commit message hold for this
interleaving?
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
The IPv6 nexthop object case looks unreachable this way. There,
ip6_rt_pcpu_alloc() takes sernum from dev_net(dev), but
nexthop_flush_dev() removes those nexthops at NETDEV_DOWN, before the first
synchronize_net().
Would replacing rcu_barrier() with synchronize_net() be enough here? Under
RTNL that is an expedited grace period. It costs much less than
rcu_barrier() but still makes readers from the teardown phase finish before
the switch.
Another option might be to take rt_genid from the netns of the lookup
rather than from dev_net(dev).
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090%40linux.dev
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH net-next v5 0/4] net: make moving a device to another netns cheaper
2026-11-21 23:21 [PATCH net-next v5 0/4] net: make moving a device to another netns cheaper Qiliang Yuan
` (3 preceding siblings ...)
2026-11-21 23:21 ` [PATCH net-next v5 4/4] net: remove rcu_barrier() from __dev_change_net_namespace() Qiliang Yuan
@ 2026-11-23 7:02 ` Qiliang Yuan
4 siblings, 0 replies; 12+ messages in thread
From: Qiliang Yuan @ 2026-11-23 7:02 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, David Ahern, Ido Schimmel, Nikolay Aleksandrov
Cc: Qiliang Yuan, Kuniyuki Iwashima, Ralf Lici, netdev, linux-kernel
On Sat, Nov 21, 2026 at 06:21:18PM -0500, Qiliang Yuan wrote:
> Moving a device to another netns waits in rcu_barrier() with RTNL held.
[...]
Please drop this series, I'm not going to post a v6.
The barrier also waits for the RCU callbacks that release routes cached
through the device, and some users never check their route again before
sending. With v5, a UDP datagram corked on a route through the device
goes out through it after the move. Retiring the cached routes when the
device goes down doesn't close this either: a lookup that picked the
nexthop before NETDEV_DOWN can cache the route after the flush, and
that route is again only released by the callbacks. With the lookup
delayed across NETDEV_DOWN and the callbacks delayed, the corked
datagram went out through the moved device in 2/10 trials on a plain
IPv4 route and 9/10 through a nexthop object, and in none on net-next.
Closing that window would take a check after every route cache
insertion, which isn't worth it for this.
The init_net blackhole that came up in v5 3/4 is an existing bug and I
sent a fix for it to net [1].
Thanks to Ralf for the reviews.
[1] https://lore.kernel.org/r/20261123-bug-net-ipv6-dst-check-released-v1-1-769d320a2470@linux.dev
Thanks,
Qiliang
^ permalink raw reply [flat|nested] 12+ messages in thread