🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #13074

[PATCH net-next v3 1/4] ipv4: draw rt_genid from a sequence shared by all netns

#13074
From: Qiliang Yuan
Date: Mon, 09 Nov 2026 08:18
91 lines
11883 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.
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
  trials sending after move          3         0
  datagrams sent after move       3132         0

Fixes: 4c7e8084fd46 ("ipv4: Plumb support for nexthop object in a fib_info")
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 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 c29a22ca178fa..140858a2fe6d4 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -557,10 +557,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 d0a5f6d77094c..16d470ab50142 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
Technical Details
Message-ID: <20261109-bug-net-netns-move-rcu-barrier-v3-1-6084b02610b3@linux.dev>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-359121-linux-kernel-netdev=dewy.ru
References: <20261109-bug-net-netns-move-rcu-barrier-v3-0-6084b02610b3@linux.dev>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-359121-linux-kernel-netdev=dewy.ru
From: qiliang.yuan@linux.dev (Qiliang Yuan)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next v3 1/4] ipv4: draw rt_genid from a sequence shared
 by all netns
Date: Mon, 09 Nov 2026 03:18:12 -0500
Organization: Usenet Network
Message-ID: <20261109-bug-net-netns-move-rcu-barrier-v3-1-6084b02610b3@linux.dev>
References: <20261109-bug-net-netns-move-rcu-barrier-v3-0-6084b02610b3@linux.dev>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
	logging-data="306918"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:i0tHM4QtThBdlQ1aJYYr8IRf4go= sha256:mELGKbShYNP5LgoH/uvfxBOwJIYwG53D6xLcM5GVt5Q=
	sha1:X2/lhZJU6KYuPLDM6DrMwj+vnV0= sha256:y3G2ml10NlKMMuwCyDvGBfs7G0E50QcECXuqJkiT+eA=
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 52A6E11373F; Mon,  9 Nov 2026 11:20:19 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.051
X-Spam-Status: No, score=-3.051 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, 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 tj90Ft5LXE0c;
	Mon,  9 Nov 2026 11:20:16 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-359121-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 sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id D913011370E; Mon,  9 Nov 2026 11:20:07 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id B2EAEE2497; Mon,  9 Nov 2026 08:18:32 +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=1794212312;
	b=wX9i1KO0/JfP8tcEh7GAQM447FROqmkYRfojFrp/5KRtrk54D/FQr9mfo36rtloKxZx6
	 BkSpzGSluSA6+Z8wkgQAP+yDVIwqJWKi6OI7iIM7//OsXXsAv0Adcmh8U7XOdYAXnyJdF
	 VhT3ChxntlbQveRvEWeSS9rJGeXdkTlfLQP8+W3Jf8O4B8HJVivA0cmkui6yH5Ubp4Yux
	 tML9jy6gxwnXJKqdda8IWqg16LsLQL+Hlh5TEZ74gxrTxGA/f5CsyXJ+bk5BFGtTCZEsN
	 1tneZxuVkCWOEN3OrHwRREo6hVgpJdq5TgfGKnwjfb8cgyfGWfrp5mjAscXbA1qhDAw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794212312;
	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=s1V/602Pvf6uE5BCQE9B8z2Cenl6Olf1l+jKmakHRg8=;
	b=DM/Zj6ON8T3rfiC9b7e+cWAWyM1n/rq1Jp7CFqt2zc53uHWKi2lHZggSpAeydQprNhN8
	 9iMnrwmwxVJN9IpCYtXzILyTV295X9pirSTglMiEi1dBU6RxIoZiFlbwfdRboXLk29dXw
	 M1NUzpb74UiZo31fvb7IToMSW4W29bLHpmt69kQRx9kL6Y+GU6E0ScWbLSS5FT0nYXs83
	 i3JF8yK+IKfPbCrWOgbPGMS4TUY8TpkPlPi9w3qGFecEcOGZXr3HfSlj/MkVdq/D07B9k
	 kgX1mDJSWZBPbslX4XC9CHVmBLuplqt6/Ln4eOLbvdWfwhY5P/uUw4l84i5VkEhrkSg==
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=BkOQTj7X;
	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 826E7606A321;
	Mon,  9 Nov 2026 08:18:32 +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=BkOQTj7X
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.142
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1794212311;
	b=l1olXbrcoZichOk2072z8nkb7Vh93ni038Kx/yS9rh5sN6yzXTQ2ht8QoAWCIUIuvyeY
	 FgHmBHT7EXrMFOZGdZPQ4EejBPU433SrdtSb9N2dFZToUuRYAN7WnUOfGje8H7o/wSStX
	 w6fl6iqkbmS2gWByV9qXjAZg01+hBXYgyIWepD56d0MlQBuIGueN3vPpCKAzbnTjPlORy
	 MaPOtucaNRsyd8UQXdxP+gZ2x9xNykSaFm6SQw1xU2jcbUPwC3mDeqCypGmcmu+DKw/Bi
	 74H6mwFRI3LFPy9zTZkZA3wAQykObZX1fCYZ6NHZGE+Q2WLxntaZTTLhQYG2955BANA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794212311;
	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=s1V/602Pvf6uE5BCQE9B8z2Cenl6Olf1l+jKmakHRg8=;
	b=O2l+jGsPWY0q4708/Km+X/dqjoTZFh7WgQGdRY+dvPINEkuOIyVSVktDUw8SxIs6W1Hx
	 Vv+EsWBl2yP6O6KIwKP0PVr7iN0erHvARkNb8pDAm5MrqKmSoO/rEPJL0A5TWErTQZVnb
	 1o0XRjao4nDCnGJ/M98qAVnnjY8EhniKw1IdsNEECou5ILfMu++slQtKVIsNx0N2yIXR2
	 oy1QgbbFiMs5EueW8XbdyfLXkWiqVbjQWrDMc20BaNO5AGgXR79nUMOwEVC7itP7ic16N
	 1u9GvNRXumGgl5ylkiRwjk0T/N77Zj+dWTMNrnZ88PmIJTyFqGToUHDBbsPSF3ihWZA==
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=BkOQTj7X;
	arc=none smtp.remote-ip=91.218.175.142
X-Received: from mta0.migadu.com (out-142.mta0.migadu.com [91.218.175.142])
	(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 F1E5F606A334
	for <netdev@vger.kernel.org>; Mon,  9 Nov 2026 08:18:30 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F1E5F606A334
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=+uxdZdGdNJPaHe9dS43uJhHV3BwO0JbjvLd8aDJ6RQU=;
 c=simple/simple; d=linux.dev;
 h=from:to:subject:date:message-id:mime-version:content-type; s=key1;
 t=1794212308; v=1; x=1794817108;
 b=BkOQTj7XinxkpwMPXbz0G/PIct5Z+LitNKtZcia8vb6+FWZ42bUxtjDP15HHYnqTM/hdglcQ
 yosH6a4eLAKqXGkLoP1KIc4QMzq+sFOX5VItDIReIyAAouJSK0QWBGy6TxC0bbFdaznrX6eWKfe
 oA7WWHrsQhhXuel4TMveGNAc=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by smtp.migadu.com with ESMTPS id 6fb67f112d4cb988;
	Mon, 09 Nov 2026 08:18:28 +0000
X-Mizu-Trace-ID: 6fb67f112d4cb988
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: <20261109-bug-net-netns-move-rcu-barrier-v3-0-6084b02610b3@linux.dev>
X-Cc: Kuniyuki Iwashima <kuniyu@google.com>, netdev@vger.kernel.org, 
 linux-kernel@vger.kernel.org, Qiliang Yuan <qiliang.yuan@linux.dev>
X-Mailer: b4 0.13.0