Article View: linux.kernel.netdev
Article #20281[PATCH net-next v5 1/4] ipv4: draw rt_genid from a sequence shared by all netns
From: Qiliang Yuan
Date: Sat, 21 Nov 2026 23:21
Date: Sat, 21 Nov 2026 23:21
108 lines
12412 bytes
12412 bytes
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
Technical Details
Message-ID:
<20261121-bug-net-netns-move-rcu-barrier-v5-1-100a3b5d9090@linux.dev>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-368515-linux-kernel-netdev=dewy.ruReferences:
<20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-368515-linux-kernel-netdev=dewy.ru From: qiliang.yuan@linux.dev (Qiliang Yuan) Newsgroups: linux.kernel.netdev Subject: [PATCH net-next v5 1/4] ipv4: draw rt_genid from a sequence shared by all netns Date: Sat, 21 Nov 2026 18:21:19 -0500 Organization: Usenet Network Message-ID: <20261121-bug-net-netns-move-rcu-barrier-v5-1-100a3b5d9090@linux.dev> References: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Injection-Info: usenet.network; logging-data="3520346"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:3dWrWzWCzMtDFMl6c5HU0GF4AeI= sha256:GSf0Ol0pha4sL1b6JK42uAmfsNUYdzbM4P73zRXiHNs= sha1:zq7IV2ABVoFbp9z/N+0Ib2GtgWo= sha256:kuG3nHdJ7P9XfGHdvsCFRk93BXxf7C09oZ0b9AhCGKA= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 894E7115C31; Sun, 22 Nov 2026 02:21:58 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.151 X-Spam-Status: No, score=-3.151 tagged_above=-9999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key) header.d=linux.dev X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id dXtRmHHFUQTI; Sun, 22 Nov 2026 02:21:55 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-368515-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74]) by mail.dewy.ru (Postfix) with ESMTPS id 09744115C2B; Sun, 22 Nov 2026 02:21:44 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sto.lore.kernel.org (Postfix) with ESMTP id 1E4846074C4; Sat, 21 Nov 2026 23:21:44 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795303299; b=xI41aBq/l8ocOwuMAK8k6OxXvfWxlY3m6nRmBmOFn1UmsCgnGVxCHcgn8aqUP4w0fbGP ZEcX8q6gSSP1RLKaOgYffRjYrfK2dvYaPLOeGvh10srRDrRKNct9djerzaa5M1tLqTeAj XZPmvACSV9NaOpJO6JiehZZVszNS5Spmx2da8tVkxZQ46DCK8irbUENE6ZKpdivHH3O3P 8xe/GWgziNsa9k/1cEY2TBpgXhFdmbdNF7Wo3wT/YqveQNE2UHqftXZrglVt4AYP5TpUj Sb9rNg5KpUtp+8vyNSXp5SAC6tI4StJmY0IaK3SXOUV/EEvOyFAACBXV+/JaKuKlHNg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795303299; h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature: X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:From:Date: Subject:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer; bh=WqWQvGkWPd+0TqpWTmw1oriDkQ6wvnerzWbOi83HkgY=; b=e+ruN5JGDdROsgjNgucJvs7u8srithrGk79ojZEVIy7iGHkXxeo2sEf503C5W9vHb97U tgu6tnBJUg/vUOYMO6qKIzGCQhwwIvMGgkIB1uNVYaZet35F42YDn9BJdg70G2VMjrnGB g2I/ysLheX8c5FnU6cky4WBv7j8yMey9mayeoUk8tN9ZC923mBxB7SmBu+MvA7tJzEqke 5m3tN4pIRtv/2vyrXuTDYX64wYIwOOPpKIH+XEOiUpkEM8xqcTrDzmCM1Mrj1sP9+/VG+ XJqpE90mhJqpOeXB5sNAAtVIM2GV9GOZs73ez3lsc7JEhV7JEd9uS57MoOCOxDgkWQg== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=EAWXAU6P; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4D05B606A33F; Sat, 21 Nov 2026 23:21:39 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=EAWXAU6P X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.9 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795303298; b=zxi3pbd9PoAO9bZAyua24odNT8/RLhjxd7kNF6vlM03mSedZr3oxF/b7VkHhCYxNmGct oekaP7n4vj736J8fdRxoWRlHPF79dz9N9wBBvPuwvAa85iREN/0gBrT0xQqF32Jsz4l02 5JCMz1ArOKl7fkrWC7mEXQMK+cC0Z4jSee4wBA6QZ/2xfRfTIFXws/LGVwiY5rHTAWWCn ziW9TVLJtlSJfU3Au27l5MKTV7f9lzey7cNTsHRTf3ZX1MCMrx6nLYMXkpZ7QU8DUofUM zpASVeMD9B/XYgZqqGTNpm1ldZADJ9gnY79SvEqLveNJOqXI1rtWw0EtGljemvdQuxQ== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795303298; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:Date:Subject:MIME-Version: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer; bh=WqWQvGkWPd+0TqpWTmw1oriDkQ6wvnerzWbOi83HkgY=; b=hlQeDJEDKxpECRGn2+TZfRLjd06QotI5LyWKFlXgT7vXtQLXyNrAu+dZxzZLv6nnSGcN ARd2jBnfMVJU+6Henf8iK7Zvvg79XRJlCmMIDvJZKasSkWw5/qm0ImSzW8WYaq9lEqpEo UMSJg0Wlfw2eberR1pdnCKunwiLiGaB1FE3lqcVucB5Y4Krxk2CrGoUHBnAOH+hAeNYon LE0zit8sdApkQC8nMKNH4T/HhqGCbOrrzLaViAel2P2TgsoRaWGt3lsRTJhaI508ia3UD fpX7kDpVUYuGbwj+acoiezHEB+ufbYkP2+7GZUs6arM3qX5Yid2TZ+9/bHto2jr/KEg== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=EAWXAU6P; arc=none smtp.remote-ip=95.215.58.9 X-Received: from mta1.migadu.com (out-9.mta1.migadu.com [95.215.58.9]) (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 2266D606A320 for <netdev@vger.kernel.org>; Sat, 21 Nov 2026 23:21:36 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2266D606A320 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Envelope-To: netdev@vger.kernel.org X-DKIM-Signature: a=rsa-sha256; bh=GNxMHNhRI9kjhozZi5ARCVY2OmD8h9tpOu+9lhr1rvs=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795303295; v=1; x=1795908095; b=EAWXAU6PCOyr5Rne97llYqGyuHCev1DqaaekynDfJ59jZ8de4sErkO5q3lyyJ6BRnot/XN1m 4ejknXZvUyO2FHeu/gUtvWIV6own1MlrfkQ44DnP8wW+WhTzdhy7ExqXoWD+mho78IYnWEwUurD UR6pC/83Pl+iAji0opDVja00= X-Envelope-To: netdev@vger.kernel.org X-Received: by smtp.migadu.com with ESMTPS id 25c397f5e1f87c6d; Sat, 21 Nov 2026 23:21:35 +0000 X-Mizu-Trace-ID: 25c397f5e1f87c6d X-Migadu-Flow: FLOW_OUT X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-In-Reply-To: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev> X-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> X-Mailer: b4 0.13.0