From nobody Tue Nov 24 12:37:58 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.10 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795303297; b=V4ROTsoq9tS5y7Y93lt7myTV+AWTWpJ5QKYaffwH8rkTpd/TQ4YbMAR7eZm3iGMq4Nhr L5t2NuC6XPty4pZDTfMnS5KoHqOixd6GNPbD2OT2nSFEIWWivYCHOoV89xdN75I2NCDK1 46GC2LY91tzRXkWzypJsXBFduYMkuhLhPNSE0AxbfkVm8k52g/oYEBKqczYbwwIJGg8xY wLbfOLBuSYfTH++Rm91UM71Oth0BLMN2+sra8KF/zLuVWf3gCHawzjYm0n1Sd1w4tKRHe qgVBbbY+MEYnm027dk6RrIIrSXSM3ALa7bAJwRmrVDcdC03/eYj0tZDjczMwLiCul7Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795303297; 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=3gMGDCLaEgUC2hMaGZa9GCT/AfH6+NUMIun0cDvixLKR6nk0C3/yrdDN6KmXDljQHr3V EIq9iItwMtrRwxAaZ6Sr+0EDoYCbhoqHNFmpIJkIrCHh7ta+ADjHGFDBE8umihbxaHdhn OFHCP2zMC3SlJsTrEjeeD9Df+Sn1Q0jgmmGNZrMCDqr9EoGGlbRSdinGL5JWuTdnl2IRl n3nYoqMnxLMNNTIMH9c8K8Z8LDtUWwbp+O4GyHN2ENwWK+wutf37Z2kDCyTU054+e+SXM /KtWVnSSGqMY5fQpQJ77ogUpsTd+oI2+g1GevtcxoLzc7c5M7WvzQIkVhCU+I+oUnCg== 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.10 Received: from mta1.migadu.com (out-10.mta1.migadu.com [95.215.58.10]) (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 240FD606A32A for ; Sat, 21 Nov 2026 23:21:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 240FD606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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-Envelope-To: linux-kernel@vger.kernel.org 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: linux-kernel@vger.kernel.org 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 From: Qiliang Yuan Date: Sat, 21 Nov 2026 18:21:19 -0500 Subject: [PATCH net-next v5 1/4] ipv4: draw rt_genid from a sequence shared by all netns X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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> In-Reply-To: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev> To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , David Ahern , Ido Schimmel , Nikolay Aleksandrov Cc: Kuniyuki Iwashima , Ralf Lici , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Qiliang Yuan X-Mailer: b4 0.13.0 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 Reviewed-by: Ralf Lici --- 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 =20 -static inline void rt_genid_bump_ipv4(struct net *net) -{ - atomic_inc(&net->ipv4.rt_genid); -} +void rt_genid_bump_ipv4(struct net *net); =20 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 */ =20 +/* + * 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 ne= tns + * 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 han= ds + * out, and inserting a route bumps the genid, so cached routes carry valu= es + * drawn from the sequence. + */ +static atomic_t rt_genid_seq; + +static int rt_genid_draw(void) +{ + int genid; + + do { + genid =3D atomic_inc_return(&rt_genid_seq); + } while (!genid); + + return genid; +} + +/* + * Publish the drawn value with a cmpxchg on the netns genid, drawing agai= n 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 =3D atomic_read(&net->ipv4.rt_genid); + + do { + genid =3D 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; --=20 2.43.0 From nobody Tue Nov 24 12:37:58 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.17 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795303304; b=eNofgsjTLL0M+aR647L13kAyezzxDw+WtHEi+oAZGMI9U4XFmPWsRfWF3PHIXeKbsDQ6 +TX7m+fz0O4CdGkXR9xH5W1uipfFDcXzuhcEbeOoZ71nKoyoxhyhTDEZP7WtFDdWQGP8h XeDX8wC7DrNyWvvFCdLy56mnuBUGAI6OQTpxUFZN9wLFg/a+OA9oPHlx6vP3IEHIpIDgT ojjkC6ZpXho4PcGAFJoGncHxwqT0cfd4cmCA0kvrUu+LNDCLEnqo9z+c917RAYXsBrdfw Ik86LTifMU1DrsI5QmXQ7PPOanqRjNBYx/hLJ0wg+1FopOhwufdVALhRBdavKk/Q4DA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795303304; 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=Qyi/9ZzWIcwjyKbG6DREb3P6l5Kv2NVOsJQWAKjCwHk=; b=4066rIPIT39cfobBgdyHkQ+xncXoXv4cHOZ/sTSfUopa/cwaBVvnkrP2Xrd6otKjY/39 qhlKll4WYi0GrZEMa1+E917YsRIOFMGMGTfPoNMe8w7Pf2sD66oGoYOMF+CrJm3urNPpw /VSHsbNbI0bz9Kasfp68cnrMUej65j8bvf8AlVWrTeUsckFqALLZhfCA0ILq/wSRaS/rC E8K9HZ2yj/wTECL+KW/y1Ira4LsDO3vEL9B9RDMiBZFswzCShXbUpJwRDdhlUyl+mpSZo 6ObgzHLyfcuE8rw+WXyXAXis7Mp6MB2uL3b1HamwG370XH3MWOuXLPcJCbKoM5OqVqg== 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=ZoOqeNnc; arc=none smtp.remote-ip=95.215.58.17 Received: from mta1.migadu.com (out-17.mta1.migadu.com [95.215.58.17]) (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 12D98606A330 for ; Sat, 21 Nov 2026 23:21:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 12D98606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=ZoOqeNnc X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=FXG9/+uOSa9pGCyOSFGRZpF2RP3UjgNYjNCd6ZQHjNk=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795303301; v=1; x=1795908101; b=ZoOqeNncaunvxmxxeFzI/bmuz9trllPpSjr/lnaYiud0Z5ZPvvs4ESAdrz27vvTJ+kQTVdEK 4/SPWnML6wIFVqXjwCgqEgSg5atzF5ZBRg63ZSVnG7ApO74EzElJNh74MqqqQTcA2uNEzqcUQlm jpeZSUqhMWKB0Rli0NC6F1pE= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id f7db6ba311952e4e; Sat, 21 Nov 2026 23:21:40 +0000 X-Mizu-Trace-ID: f7db6ba311952e4e X-Migadu-Flow: FLOW_OUT From: Qiliang Yuan Date: Sat, 21 Nov 2026 18:21:20 -0500 Subject: [PATCH net-next v5 2/4] ipv6: take fib6_sernum from a sequence shared by all netns X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261121-bug-net-netns-move-rcu-barrier-v5-2-100a3b5d9090@linux.dev> References: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev> In-Reply-To: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev> To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , David Ahern , Ido Schimmel , Nikolay Aleksandrov Cc: Kuniyuki Iwashima , Ralf Lici , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Qiliang Yuan X-Mailer: b4 0.13.0 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 Reviewed-by: Ralf Lici --- 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, str= uct fib6_info *rt) fib6_walkers_replace_leaf(net, rt, next); } =20 +/* + * 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 star= ts + * at 1 and the sequence only hands out values from 2 up: 0 means no sernum + * to check. + */ +static atomic_t fib6_sernum_seq =3D ATOMIC_INIT(1); + +static int fib6_sernum_draw(void) +{ + int new, old =3D atomic_read(&fib6_sernum_seq); + + do { + new =3D 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 aga= in 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 =3D atomic_read(&net->ipv6.fib6_sernum); =20 do { - new =3D old < INT_MAX ? old + 1 : 1; + new =3D fib6_sernum_draw(); } while (!atomic_try_cmpxchg(&net->ipv6.fib6_sernum, &old, new)); =20 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 stru= ct fib6_result *res) =20 static bool rt6_is_valid(const struct rt6_info *rt6) { - return rt6->sernum =3D=3D 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 !=3D 1 && + rt6->sernum =3D=3D rt_genid_ipv6(dev_net(rt6->dst.dev)); } =20 /* It should be called with rcu_read_lock() acquired */ --=20 2.43.0 From nobody Tue Nov 24 12:37:58 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.22 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795303308; b=ZSYIvjHR6IfiUA6i4zuW8rVCt4DIz5waq4SzSeXBPBgVVYmQKITz/iv6YC56VrHtmYBe DpoKGhAejEqXvQg9PTylmTgY1v83xy4NC35P3luWczzQkyo5vXYZwLZxl9hLaYA7NuiAo L1YaW5k+gAvMp/DQSQ2Y9RYS8UDkEatmPIxKjrx7OyIR2r0aVZSwwakBeWsE16TtREluH fXsq3HAdX5/wTp/NJ+BtyqX6axn6u9qP7Sp2JxY01JKpGEKWIlYqHpeCLkhN0M3WwAVmT vhP7bHM2wINBm8Ao8eHk86fP/4ybml7F6YNtEnCx1FbPVhxKWWkvLyEHXs8YyU3YccA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795303308; 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=CeRmkXb4PaO+jzA+zVIO/f6aAfAmNuevRYNzWo5MlYc=; b=mQlwkvYbx6yRGNjyC87VBN+b76ehH+JkEAyDsnpAGICox3pYjxMR8txbJVKNV0ZlzUqd IsH8u0Jm8ODAdyggUTStpCG5lVZNvH3RAzxkjfo6S4ZpoiPg3988/nJYMUo5iBibRtiDc SJgjbxMqyB3OGROmizvGsZWJj45pSIPcc8VjzDFu5Bf1RMLWUeI2Fwb7BJ4qJzHwthh2t VMbC23XJI2Bpydjsx/SzaZ56bYyAduhioKWPc5ZqOe3eYXa2obD+Zr1f7BMhw/pyqjIC6 ArlLXwx57Iidj5fPIM066DzUV1wz6o5J6057q25iyUVlVp1J1fK4Gz4vLzlGGLS5kcg== 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=NMw2xf2q; arc=none smtp.remote-ip=95.215.58.22 Received: from mta1.migadu.com (out-22.mta1.migadu.com [95.215.58.22]) (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 3E212606A32C for ; Sat, 21 Nov 2026 23:21:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3E212606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=NMw2xf2q X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=hm61AzN/MAJszg1nQNhYCz6kJuFkbVQtftEGzmQdmDk=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795303306; v=1; x=1795908106; b=NMw2xf2qjvWfL8XJKyJwUglsH9z+WHrBF+WPQ7ioM8bXKF9ohNkRGLXbUZMGAPZjZ+k1s6PJ l+x2JTkZcobUGfKZRdSlp3BX6xb861/HXqRmQs1GNfJ1Wl4V4PYuyXFlbsmGl+j0oaRX5lKymjl ZkR67yLY6WvkcPztZ3wmPmiQ= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 21ba8d938a5604e2; Sat, 21 Nov 2026 23:21:46 +0000 X-Mizu-Trace-ID: 21ba8d938a5604e2 X-Migadu-Flow: FLOW_OUT From: Qiliang Yuan Date: Sat, 21 Nov 2026 18:21:21 -0500 Subject: [PATCH net-next v5 3/4] nexthop: release cached routes when a nexthop leaves a group X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261121-bug-net-netns-move-rcu-barrier-v5-3-100a3b5d9090@linux.dev> References: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev> In-Reply-To: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev> To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , David Ahern , Ido Schimmel , Nikolay Aleksandrov Cc: Kuniyuki Iwashima , Ralf Lici , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Qiliang Yuan X-Mailer: b4 0.13.0 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 --- 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 ne= xthop *nh, struct nh_grp_entry *nhge, *tmp; LIST_HEAD(deferred_free); bool need_flush =3D false; + struct nh_info *nhi; =20 /* 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 n= exthop *nh, /* make sure all see the newly published array before releasing rtnl */ synchronize_net(); =20 + /* 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 =3D rtnl_dereference(nh->nh_info); + if (nhi->family =3D=3D 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 =E2=80=94 all RCU readers have finished = */ list_for_each_entry_safe(nhge, tmp, &deferred_free, nh_list) { list_del(&nhge->nh_list); --=20 2.43.0 From nobody Tue Nov 24 12:37:58 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.29 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795303314; b=2lV/AXI88HM1X+lAvbLyEoELsR0FM5fBqTvvtLfYUWsNiK/MH0+8uWs2KBJdee3WUKMW zBjkDMKBP5b9WUuMyHOFullnDrJjVqXGy5NXkSpwKFXvfUr8QQF9ShroVgLFPVVcfku9F u+a688J4nLIFnOhnS/YADRetWgXMuTSroelnUkh63jZF9lDq17EnRkavvLGkUlz1v/R7y gMjx0lwrsBRSlts+jhVsWZKE50pwYFAHsqs36Rfyhi/PBY7IPDs863lVY6uwIPW3fHnPw ZQQUuRpURD7HZPuTJRII7/hSZnIqHgDa8Lu5m/OgWn401BGEyIVlmUzFkcqngi4IncQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795303314; 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=zyKALNKnFjZykKRGyCn9IPvxDmhW60d0Qms1kA+cJHQ=; b=jUD6H254pYlhVAYgOTxpZNPO7En0eXXpVZ+p26DuF2jllAY2Cd9C34Nx/Cx6G+Ih2/Ob 0Oe1sN/O9oH5Dlb/dr5Obc4hNb2FQdGFrOlyFH1yWVQFww51ucNpH6U4aR2CXTaXAclpZ KpS0QYsWqukI72m0S8dChjRU7QGdAuNVt0CDomEaCpjNzR8G7kr9xmBscGL/fT7reWWA4 0BW/BY2dxaq4FHMOwcYWCy4UKzoprHJ2h4cVpAHQe61ACw7JP5TDZabxsZS7uGg3patyq 2ogqFK56hGlQwTUhMHt+a0f4tx1/gYFN3R9QNU+l3xjoN9QwydvDGn7LwANwo5w+AKA== 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=mdYB9RGA; arc=none smtp.remote-ip=95.215.58.29 Received: from mta1.migadu.com (out-29.mta1.migadu.com [95.215.58.29]) (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 C8E69606A32A for ; Sat, 21 Nov 2026 23:21:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C8E69606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=mdYB9RGA X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=Izj7XCHwlTZoYZL1ePfZUkbgMrLk3intiIl1aTCAGtw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795303312; v=1; x=1795908112; b=mdYB9RGAn7Eb93JIvp6V35b4foYxRdINgKrQ45lCK31uXP0qYoM+eucGQQpbgsIwG1rkMssh mH+v3A/ebl0TaPDQfGWFzUjkFNa68OAGmkEcxcVYJxjPFEZbrY9mI67EuaOkqpERw86NEiBY+Vj D2478TVTtBq0vpJQnqWPAtwo= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 8730f2f79daef3a3; Sat, 21 Nov 2026 23:21:52 +0000 X-Mizu-Trace-ID: 8730f2f79daef3a3 X-Migadu-Flow: FLOW_OUT From: Qiliang Yuan Date: Sat, 21 Nov 2026 18:21:22 -0500 Subject: [PATCH net-next v5 4/4] net: remove rcu_barrier() from __dev_change_net_namespace() X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261121-bug-net-netns-move-rcu-barrier-v5-4-100a3b5d9090@linux.dev> References: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev> In-Reply-To: <20261121-bug-net-netns-move-rcu-barrier-v5-0-100a3b5d9090@linux.dev> To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , David Ahern , Ido Schimmel , Nikolay Aleksandrov Cc: Kuniyuki Iwashima , Ralf Lici , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Qiliang Yuan X-Mailer: b4 0.13.0 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 --- 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 *d= ev, struct net *net, * the device is just moving and can keep their slaves up. */ call_netdevice_notifiers(NETDEV_UNREGISTER, dev); - rcu_barrier(); =20 new_nsid =3D peernet2id_alloc(dev_net(dev), net, GFP_KERNEL); =20 --=20 2.43.0