Skip to contentSkip to conversation

net: Support per-netns device unregistration

PATCH Applied Kuniyuki Iwashima 3 participants 26 messages Jul 3 – Jul 11

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover net: Support per-netns device unregistration

The biggest blocker to per-netns RTNL is netdev unregistration.

It starts within a single netns, but it can eventually involve multiple namespaces.

There are three types of such cross-netns devices:

1. Paired devices (e.g., netkit, veth, vxcan)
-> Unregistering one device also deletes its peer, which
may reside in another netns.

2. Tunnel devices (e.g., bareudp, geneve, etc)
-> Destroying a netns removes devices in another netns if
their backend sockets reside in the dying netns

3. Stacked devices (e.g., ipvlan, macvlan, etc)
-> Removing the lower device also removes multiple upper
devices, each of which may reside in different namespaces.

While the first two device types require at most two rtnl_net_lock()s, the stacked type has no upper limit. This makes it impossible to freeze all necessary namespaces in advance.

This series introduces per-netns work, initially suggested at NetConf 2024, to delegate the unregistration of such cross-netns devices.

https://netdev.bots.linux.dev/netconf/2024/kuniyu.pdf#page=62

The first half of the series wraps NETDEV_UNREGISTER (in core) with per-netns RTNL, adds a helper for per-netns device unregistration, and forces per-netns device unregistration in the core code when CONFIG_DEBUG_NET_SMALL_RTNL=y.

The latter half picks out one from each type (veth, bareudp, ipvlan) and converts them to support per-netns device unregistration, although the operations are **still serialised under RTNL** for now.

Please note that this series focuses only on the device unregistration paths. For example, there are ASSERT_RTNL() left in other paths, and Sashiko may point it out, but they are out of scope.

This is just the first step, and we need more incremental changes to completely remove RTNL anyway.

Now, we can see that unregistering a lower device (veth0 below) removes upper devices (ipvl2, ipvl3) in different namespaces using per-netns work with a different PID. The lower device (veth0) is freed only after all upper ipvlan devices have called netdev_put() in ipvlan_uninit().

  # ip netns add ns1
  # ip netns add ns2
  # ip netns add ns3
  # ip -n ns1 link add veth0 type veth peer veth1
  # ip -n ns2 link add ipvl2 link veth0 link-netns ns1 type ipvlan mode l2
  # ip -n ns3 link add ipvl3 link veth0 link-netns ns1 type ipvlan mode l2
  # ip -n ns1 link del veth0
  # bpftrace -e '#include <linux/netdevice.h>
  kprobe:ipvlan_uninit,
  kprobe:veth_dellink,
  kprobe:free_netdev {
      $dev = (struct net_device *)arg0;
      printf("PID: %d | DEV: %s%s\n", pid, $dev->name, kstack());
  }'

PID: 2010 | DEV: veth0

          veth_dellink+5
          rtnl_dellink+1213
          rtnetlink_rcv_msg+1791
  ...
  PID: 440 | DEV: ipvl2
          ipvlan_uninit+5
          unregister_netdevice_many_notify+7129
          unregister_netdevice_many_net+1050
          rtnl_net_work_func+136
  ...
  PID: 440 | DEV: ipvl2
          free_netdev+5
          netdev_run_todo+4798
          process_scheduled_works+2538
  ...
  PID: 440 | DEV: ipvl3
          ipvlan_uninit+5
          unregister_netdevice_many_notify+7129
          unregister_netdevice_many_net+1050
          rtnl_net_work_func+136
          process_scheduled_works+2538
  ...
  PID: 2010 | DEV: veth0
          free_netdev+5
          netdev_run_todo+4798
          rtnl_dellink+1507
          rtnetlink_rcv_msg+1791
  ...
  PID: 440 | DEV: ipvl3
          free_netdev+5
          netdev_run_todo+4798
          process_scheduled_works+2538

...

Changes:
v2:
* Patch 6
* Use spin_lock_nested() in unregister_netdevice_move_net()
* Add kdoc for dev->unreg_list_net
* Add DEBUG_NET_WARN_ON_ONCE() in unregister_netdevice_queue()
* Patch 13
* Add __ipvtap_dellink_ptr for CONFIG_IPVLAN=y and CONFIG_TAP=m

v1: https://lore.kernel.org/netdev/[email protected]/

Kuniyuki Iwashima (14):
rtnetlink: Lock sock_net(skb->sk) in rtnl_newlink().
rtnetlink: Call unregister_netdevice_many() only once in
rtnl_link_unregister().
rtnetlink: Add per-netns rtnl_work.
net: Wrap default_device_exit_net() with __rtnl_net_lock().
net: Hold __rtnl_net_lock() in netdev_wait_allrefs_any().
net: Add per-netns netdev unregistration infra.
net: Call unregister_netdevice_many() per netns.
veth: Support per-netns device unregistration.
bareudp: Protect bareudp_list with mutex.
bareudp: Support per-netns netdev unregistration.
ipvlan: Convert ipvl_port.count to refcount_t.
ipvlan: Synchronise ipvlan_init() and ipvlan_uninit() for the same
lower dev.
ipvlan: Protect ipvl_port.ipvlans with mutex.
ipvlan: Support per-netns netdev unregistration.

 drivers/net/bareudp.c            |  43 ++++++++-
 drivers/net/ipvlan/ipvlan.h      |  12 ++-
 drivers/net/ipvlan/ipvlan_main.c | 147 ++++++++++++++++++++++++-------
 drivers/net/ipvlan/ipvtap.c      |  25 +++++-
 drivers/net/veth.c               |  34 ++++---
 include/linux/netdevice.h        |  24 +++++
 include/linux/rtnetlink.h        |   8 ++
 include/net/net_namespace.h      |   3 +
 net/core/dev.c                   | 135 +++++++++++++++++++++++++++-
 net/core/net_namespace.c         |   4 +
 net/core/rtnetlink.c             |  57 ++++++++++--
 11 files changed, 429 insertions(+), 63 deletions(-)
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#1 lore raw

PATCH v2 1/14 rtnetlink: Lock sock_net(skb->sk) in rtnl_newlink().

There are a few cases where rtnl_net_lock() is not properly held in rtnl_newlink().

When either of IFLA_NET_NS_PID / IFLA_NET_NS_FD / IFLA_TARGET_NETNSID is specified but IFLA_LINK_NETNSID is not, sock_net(skb->sk) is used as link_net in rtnl_newlink_link_net().

In addition, the do_setlink() path uses sock_net(skb->sk) and one from the three netns attributes while rtnl_link_get_net_capable() returns only one of four.

Let's add sock_net(skb->sk) to rtnl_nets in rtnl_newlink().

---
No fixes tag is needed since there is no real bug nor assertion.

---
 net/core/rtnetlink.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
net/core/rtnetlink.c +6 -3
index 12aa3aa1688b..f39c93e80e20 100644
@@ -282,10 +282,11 @@ static int rtnl_net_cmp_locks(const struct net *net_a, const struct net *net_b)
 #endif
 
 struct rtnl_nets {
-	/* ->newlink() needs to freeze 3 netns at most;
-	 * 2 for the new device, 1 for its peer.
+	/* ->newlink() needs to freeze 4 netns at most;
+	 * 2 for the new device, 1 for its peer, 1 for
+	 * an existing device (do_setlink() path).
 	 */
-	struct net *net[3];
+	struct net *net[4];
 	unsigned char len;
 };
 
@@ -4155,6 +4156,8 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
 		}
 	}
 
+	rtnl_nets_add(&rtnl_nets, get_net(sock_net(skb->sk)));
+
 	rtnl_nets_lock(&rtnl_nets);
 	ret = __rtnl_newlink(skb, nlh, ops, tgt_net, link_net, peer_net, tbs, data, extack);
 	rtnl_nets_unlock(&rtnl_nets);
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#2 lore raw

PATCH v2 2/14 rtnetlink: Call unregister_netdevice_many() only once in rtnl_link_unregister().

In reply to Kuniyuki Iwashima

When rtnl_link_unregister() is called during module unload, it calls __rtnl_kill_links() for every netns.

__rtnl_kill_links() collects all devices of the unloaded module and passes them to unregister_netdevice_many().

Let's move unregister_netdevice_many() to rtnl_link_unregister() to unregister all devices across netns in a single batch.

---
 net/core/rtnetlink.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
net/core/rtnetlink.c +7 -5
index f39c93e80e20..7207da002fb5 100644
@@ -637,16 +637,15 @@ int rtnl_link_register(struct rtnl_link_ops *ops)
 }
 EXPORT_SYMBOL_GPL(rtnl_link_register);
 
-static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
+static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops,
+			      struct list_head *dev_kill_list)
 {
 	struct net_device *dev;
-	LIST_HEAD(list_kill);
 
 	for_each_netdev(net, dev) {
 		if (dev->rtnl_link_ops == ops)
-			ops->dellink(dev, &list_kill);
+			ops->dellink(dev, dev_kill_list);
 	}
-	unregister_netdevice_many(&list_kill);
 }
 
 /* Return with the rtnl_lock held when there are no network
@@ -677,6 +676,7 @@ static void rtnl_lock_unregistering_all(void)
  */
 void rtnl_link_unregister(struct rtnl_link_ops *ops)
 {
+	LIST_HEAD(dev_kill_list);
 	struct net *net;
 
 	mutex_lock(&link_ops_mutex);
@@ -691,7 +691,9 @@ void rtnl_link_unregister(struct rtnl_link_ops *ops)
 	rtnl_lock_unregistering_all();
 
 	for_each_net(net)
-		__rtnl_kill_links(net, ops);
+		__rtnl_kill_links(net, ops, &dev_kill_list);
+
+	unregister_netdevice_many(&dev_kill_list);
 
 	rtnl_unlock();
 	up_write(&pernet_ops_rwsem);
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#3 lore raw

PATCH v2 3/14 rtnetlink: Add per-netns rtnl_work.

In reply to Kuniyuki Iwashima

The biggest blocker to per-netns RTNL is netdev unregistration.

It starts within a single netns (e.g., during a device lookup or netns dismantle), but it can eventually involve multiple namespaces, such as when upper ipvlan devices reside in different netns.

This prevents us from acquiring multiple rtnl_net_lock()s beforehand.

When we encounter such a cross-netns device, we must delegate the unregistration to the work of the netns where the device actually resides.

Let's add per-netns rtnl_work to support the deferred netdev unregistration.

---
 include/linux/rtnetlink.h   |  8 ++++++++
 include/net/net_namespace.h |  1 +
 net/core/net_namespace.c    |  1 +
 net/core/rtnetlink.c        | 26 ++++++++++++++++++++++++++
 4 files changed, 36 insertions(+)
include/linux/rtnetlink.h +8 -0
index ea39dd23a197..95729339e7a5 100644
@@ -115,6 +115,10 @@ bool rtnl_net_is_locked(struct net *net);
 
 bool lockdep_rtnl_net_is_held(struct net *net);
 
+void rtnl_net_queue_work(struct net *net);
+void rtnl_net_flush_workqueue(void);
+void rtnl_net_work_func(struct work_struct *work);
+
 #define rcu_dereference_rtnl_net(net, p)				\
 	rcu_dereference_check(p, lockdep_rtnl_net_is_held(net))
 #define rtnl_net_dereference(net, p)					\
@@ -150,6 +154,10 @@ static inline void ASSERT_RTNL_NET(struct net *net)
 	ASSERT_RTNL();
 }
 
+static inline void rtnl_net_flush_workqueue(void)
+{
+}
+
 #define rcu_dereference_rtnl_net(net, p)		\
 	rcu_dereference_rtnl(p)
 #define rtnl_net_dereference(net, p)			\
include/net/net_namespace.h +1 -0
index 80de5e98a66d..a989019af5f7 100644
@@ -197,6 +197,7 @@ struct net {
 #ifdef CONFIG_DEBUG_NET_SMALL_RTNL
 	/* Move to a better place when the config guard is removed. */
 	struct mutex		rtnl_mutex;
+	struct work_struct	rtnl_work;
 #endif
 #if IS_ENABLED(CONFIG_VSOCKETS)
 	struct netns_vsock	vsock;
net/core/net_namespace.c +1 -0
index d9dafe24f57e..d1aeff9de580 100644
@@ -422,6 +422,7 @@ static __net_init int preinit_net(struct net *net, struct user_namespace *user_n
 #ifdef CONFIG_DEBUG_NET_SMALL_RTNL
 	mutex_init(&net->rtnl_mutex);
 	lock_set_cmp_fn(&net->rtnl_mutex, rtnl_net_lock_cmp_fn, NULL);
+	INIT_WORK(&net->rtnl_work, rtnl_net_work_func);
 #endif
 
 	INIT_LIST_HEAD(&net->ptype_all);
net/core/rtnetlink.c +26 -0
index 7207da002fb5..7959519e7375 100644
@@ -273,6 +273,26 @@ bool lockdep_rtnl_net_is_held(struct net *net)
 	return lockdep_rtnl_is_held() && lockdep_is_held(&net->rtnl_mutex);
 }
 EXPORT_SYMBOL(lockdep_rtnl_net_is_held);
+
+static struct workqueue_struct *rtnl_net_wq;
+
+void rtnl_net_queue_work(struct net *net)
+{
+	queue_work(rtnl_net_wq, &net->rtnl_work);
+}
+
+void rtnl_net_flush_workqueue(void)
+{
+	flush_workqueue(rtnl_net_wq);
+}
+
+void rtnl_net_work_func(struct work_struct *work)
+{
+	struct net *net = container_of(work, struct net, rtnl_work);
+
+	rtnl_net_lock(net);
+	rtnl_net_unlock(net);
+}
 #else
 static int rtnl_net_cmp_locks(const struct net *net_a, const struct net *net_b)
 {
@@ -7226,4 +7246,10 @@ void __init rtnetlink_init(void)
 	register_netdevice_notifier(&rtnetlink_dev_notifier);
 
 	rtnl_register_many(rtnetlink_rtnl_msg_handlers);
+
+#ifdef CONFIG_DEBUG_NET_SMALL_RTNL
+	rtnl_net_wq = create_workqueue("rtnl_net");
+	if (!rtnl_net_wq)
+		panic("Could not create rtnl_net workq");
+#endif
 }
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#4 lore raw

Re: [PATCH v2 net-next 03/14] rtnetlink: Add per-netns rtnl_work.

Replying to Sashiko review,

On Thu, Jul 2, 2026 at 5:10 PM Kuniyuki Iwashima <[email protected]> wrote:
[...]

Quoted 5 lines
net/core/rtnetlink.c +0 -0
index 7207da002fb5..7959519e7375 100644

net/core/rtnetlink.c bool lockdep_rtnl_net_is_held(struct net *net) · line 282

Quoted 10 lines
net/core/rtnetlink.c +7 -0
@@ -273,6 +273,26 @@ bool lockdep_rtnl_net_is_held(struct net *net)
        return lockdep_rtnl_is_held() && lockdep_is_held(&net->rtnl_mutex);
 }
 EXPORT_SYMBOL(lockdep_rtnl_net_is_held);
+
+static struct workqueue_struct *rtnl_net_wq;
+
+void rtnl_net_queue_work(struct net *net)
+{
+       queue_work(rtnl_net_wq, &net->rtnl_work);
+}

---8<---

net/core/rtnetlink.c bool lockdep_rtnl_net_is_held(struct net *net) · line 281

Quoted 1 line
net/core/rtnetlink.c +1 -0
+ queue_work(rtnl_net_wq, &net->rtnl_work);

Can this queue work on a freed struct net object, leading to a use-after-free?
If a thread in netns B deletes a cross-netns device (such as veth) whose peer is in netns A, it can nullify the cross-reference pointers (e.g., in veth_dellink()) but get preempted just before calling unregister_netdevice_queue_net() for netns A.
---8<---

No, the concerned veth will get a new netdev_hold() in patch 8 to prevent peer (and its netns) from being freed.

#5 lore raw

PATCH v2 4/14 net: Wrap default_device_exit_net() with __rtnl_net_lock().

In reply to Kuniyuki Iwashima

default_device_exit_net() could call dev_change_net_namespace() to move devices from a dying netns to init_net.

Let's hold the two netns __rtnl_net_lock() around it.

---
 net/core/dev.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/core/dev.c +9 -1
index 4b3d5cfdf6e0..c477c4f84ed9 100644
@@ -13034,7 +13034,7 @@ static void __net_exit default_device_exit_net(struct net *net)
 	 * Push all migratable network devices back to the
 	 * initial network namespace
 	 */
-	ASSERT_RTNL();
+
 	for_each_netdev_safe(net, dev, aux) {
 		int err;
 		char fb_name[IFNAMSIZ];
@@ -13077,11 +13077,19 @@ static void __net_exit default_device_exit_batch(struct list_head *net_list)
 	LIST_HEAD(dev_kill_list);
 
 	rtnl_lock();
+
+	__rtnl_net_lock(&init_net);
+
 	list_for_each_entry(net, net_list, exit_list) {
+		__rtnl_net_lock(net);
 		default_device_exit_net(net);
+		__rtnl_net_unlock(net);
+
 		cond_resched();
 	}
 
+	__rtnl_net_unlock(&init_net);
+
 	list_for_each_entry(net, net_list, exit_list) {
 		for_each_netdev_reverse(net, dev) {
 			if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#6 lore raw

PATCH v2 5/14 net: Hold __rtnl_net_lock() in netdev_wait_allrefs_any().

In reply to Kuniyuki Iwashima

Currently, netdev_run_todo() processes pending devices from multiple namespaces in a batch.

To expand the per-netns RTNL coverage for NETDEV_UNREGISTER, let's acquire __rtnl_net_lock() in netdev_wait_allrefs_any().

Note that netdev_run_todo() itself will need to be namespacified before RTNL is removed.

---
 net/core/dev.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/core/dev.c +6 -1
index c477c4f84ed9..48818a194fa5 100644
@@ -11608,8 +11608,13 @@ static struct net_device *netdev_wait_allrefs_any(struct list_head *list)
 			rtnl_lock();
 
 			/* Rebroadcast unregister notification */
-			list_for_each_entry(dev, list, todo_list)
+			list_for_each_entry(dev, list, todo_list) {
+				struct net *net = dev_net(dev);
+
+				__rtnl_net_lock(net);
 				call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
+				__rtnl_net_unlock(net);
+			}
 
 			__rtnl_unlock();
 			rcu_barrier();
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#7 lore raw

Re: [PATCH v2 net-next 05/14] net: Hold __rtnl_net_lock() in netdev_wait_allrefs_any().

Replying to Sashiko review,

On Thu, Jul 2, 2026 at 5:10 PM Kuniyuki Iwashima <[email protected]> wrote:
[...]

net/core/dev.c static struct net_device *netdev_wait_allrefs_any(struct list_head *list) · line 11614

Quoted 13 lines
net/core/dev.c +4 -1
index c477c4f84ed9..48818a194fa5 100644
@@ -11608,8 +11608,13 @@ static struct net_device *netdev_wait_allrefs_any(struct list_head *list)
                        rtnl_lock();
 
                        /* Rebroadcast unregister notification */
-                       list_for_each_entry(dev, list, todo_list)
+                       list_for_each_entry(dev, list, todo_list) {
+                               struct net *net = dev_net(dev);
+
+                               __rtnl_net_lock(net);

---8<---
This is a pre-existing issue, but can this cause a use-after-free on the net namespace memory?
---8<---

No, netdev_wait_allrefs_any() must be done for all netdev in dying netns.

net/core/dev.c static struct net_device *netdev_wait_allrefs_any(struct list_head *list) · line 11619

Quoted 5 lines
net/core/dev.c +2 -0
                                call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
+                               __rtnl_net_unlock(net);
+                       }
 
                        __rtnl_unlock();

---8<---
Can __rtnl_net_unlock() leave items on net_todo_list and trigger the WARN_ON in __rtnl_unlock()?

If a concurrent thread queues unregistrations to net->dev_unreg_head, ---8<---

Once RTNL is removed, yes, but before that, we need to revisit here as mentioned in the commit message.

#8 lore raw

PATCH v2 6/14 net: Add per-netns netdev unregistration infra.

In reply to Kuniyuki Iwashima

When we need to unregister a netdev in a different netns, we will delegate its unregistration to per-netns work.

There are three types of such cross-netns devices:

1. Paired devices (e.g., netkit, veth, vxcan)
-> Unregistering one device also deletes its peer, which
may reside in another netns.

2. Tunnel devices (e.g., bareudp, geneve, etc)
-> Destroying a netns removes devices in another netns if
their backend sockets reside in the dying netns

3. Stacked devices (e.g., ipvlan, macvlan, etc)
-> Removing the lower device also removes multiple upper
devices, each of which may reside in different namespaces.

In these cases, we will use unregister_netdevice_queue_net() to queue such potential cross-netns devices for destruction.

Each driver must not call both unregister_netdevice_queue_net() and unregister_netdevice_queue() for the same device. See the subsequent veth/bareudp/ipvlan patches for how they avoid double queueing.

unregister_netdevice_queue_net() takes net and dev. If dev resides in the net, it simply calls unregister_netdevice_queue().

If dev_net(dev) is different from the net, it enqueues the device to dev_net(dev)->dev_unreg_head and schedules the per-netns work.

When __rtnl_net_unlock() is called from the per-netns work (or another thread already holding the lock), unregister_netdevice_many_net() collects the queued devices and calls unregister_netdevice_many() to perform the actual unregistration.

During netns dismantle, rtnl_net_flush_workqueue() is called at the end of default_device_exit_batch() to ensure that cross-netns devices in the other alive netns are unregistered.

Once RTNL is removed, a device could be moved to another netns while being queued to net->dev_unreg_head.

__dev_change_net_namespace() handles this race by acquiring net->dev_unreg_lock of both the old and new netns after dev_set_net() and moving the device between their dev_unreg_head lists.

Since dev_set_net() and unregister_netdevice_queue_net() are synchronised by netdev_lock(), the device is either queued to the old netns's dev_unreg_head and then moved, or queued directly to the new netns.

Note that unregister_netdevice_move_net() does not need to call rtnl_net_queue_work() because __dev_change_net_namespace() is (supposed to be) called with rtnl_net_lock(). (Not all callers hold it yet, but the race does not happen until all callers are converted and RTNL is removed.)

---
v2:
* Use spin_lock_nested() in unregister_netdevice_move_net()
* Add kdoc for dev->unreg_list_net
* Add DEBUG_NET_WARN_ON_ONCE() in unregister_netdevice_queue()

---
 include/linux/netdevice.h   | 18 ++++++++
 include/net/net_namespace.h |  2 +
 net/core/dev.c              | 91 +++++++++++++++++++++++++++++++++++++
 net/core/net_namespace.c    |  2 +
 net/core/rtnetlink.c        |  4 ++
 5 files changed, 117 insertions(+)
include/linux/netdevice.h +18 -0
index 9981d637f8b5..108d8d7ea75b 100644
@@ -1845,6 +1845,8 @@ enum netdev_reg_state {
  *	@napi_list:	List entry used for polling NAPI devices
  *	@unreg_list:	List entry  when we are unregistering the
  *			device; see the function unregister_netdev
+ *	@unreg_list_net:List entry when we are unregistering the cross-netns
+ *			device; see the function unregister_netdevice_queue_net()
  *	@close_list:	List entry used when we are closing the device
  *	@ptype_all:     Device-specific packet handlers for all protocols
  *	@ptype_specific: Device-specific, protocol-specific packet handlers
@@ -2241,6 +2243,9 @@ struct net_device {
 	struct list_head	dev_list;
 	struct list_head	napi_list;
 	struct list_head	unreg_list;
+#ifdef CONFIG_DEBUG_NET_SMALL_RTNL
+	struct list_head	unreg_list_net;
+#endif
 	struct list_head	close_list;
 	struct list_head	ptype_all;
 
@@ -3472,6 +3477,19 @@ static inline void unregister_netdevice(struct net_device *dev)
 	unregister_netdevice_queue(dev, NULL);
 }
 
+#ifdef CONFIG_DEBUG_NET_SMALL_RTNL
+void unregister_netdevice_queue_net(struct net *net, struct net_device *dev,
+				    struct list_head *head);
+void unregister_netdevice_many_net(struct net *net);
+#else
+static inline void unregister_netdevice_queue_net(struct net *net,
+						  struct net_device *dev,
+						  struct list_head *head)
+{
+	unregister_netdevice_queue(dev, head);
+}
+#endif
+
 int netdev_refcnt_read(const struct net_device *dev);
 void free_netdev(struct net_device *dev);
 
include/net/net_namespace.h +2 -0
index a989019af5f7..501af1999fe8 100644
@@ -198,6 +198,8 @@ struct net {
 	/* Move to a better place when the config guard is removed. */
 	struct mutex		rtnl_mutex;
 	struct work_struct	rtnl_work;
+	struct list_head	dev_unreg_head;
+	spinlock_t		dev_unreg_lock;
 #endif
 #if IS_ENABLED(CONFIG_VSOCKETS)
 	struct netns_vsock	vsock;
net/core/dev.c +91 -0

127 lines not on this page · raw message

net/core/net_namespace.c +2 -0
index d1aeff9de580..578b48cf5318 100644
@@ -423,6 +423,8 @@ static __net_init int preinit_net(struct net *net, struct user_namespace *user_n
 	mutex_init(&net->rtnl_mutex);
 	lock_set_cmp_fn(&net->rtnl_mutex, rtnl_net_lock_cmp_fn, NULL);
 	INIT_WORK(&net->rtnl_work, rtnl_net_work_func);
+	INIT_LIST_HEAD(&net->dev_unreg_head);
+	spin_lock_init(&net->dev_unreg_lock);
 #endif
 
 	INIT_LIST_HEAD(&net->ptype_all);
net/core/rtnetlink.c +4 -0
index 7959519e7375..544498d3c325 100644
@@ -197,6 +197,7 @@ void __rtnl_net_unlock(struct net *net)
 {
 	ASSERT_RTNL();
 
+	unregister_netdevice_many_net(net);
 	mutex_unlock(&net->rtnl_mutex);
 }
 EXPORT_SYMBOL(__rtnl_net_unlock);
@@ -290,6 +291,9 @@ void rtnl_net_work_func(struct work_struct *work)
 {
 	struct net *net = container_of(work, struct net, rtnl_work);
 
+	if (list_empty(&net->dev_unreg_head))
+		return;
+
 	rtnl_net_lock(net);
 	rtnl_net_unlock(net);
 }
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#9 lore raw

Re: [PATCH v2 net-next 06/14] net: Add per-netns netdev unregistration infra.

Replying to Sashiko review,

On Thu, Jul 2, 2026 at 5:10 PM Kuniyuki Iwashima <[email protected]> wrote:
[...]

Quoted 6 lines

Note that unregister_netdevice_move_net() does not need to call rtnl_net_queue_work() because __dev_change_net_namespace() is (supposed to be) called with rtnl_net_lock(). (Not all callers hold it yet, but the race does not happen until all callers are converted and RTNL is removed.)

[...]

Quoted 25 lines
net/core/dev.c +25 -0
+static void unregister_netdevice_move_net(struct net *net_old,
+                                         struct net *net,
+                                         struct net_device *dev)
+{
+       if (net_old > net) {
+               spin_lock(&net->dev_unreg_lock);
+               spin_lock_nested(&net_old->dev_unreg_lock, SINGLE_DEPTH_NESTING);
+       } else {
+               spin_lock(&net_old->dev_unreg_lock);
+               spin_lock_nested(&net->dev_unreg_lock, SINGLE_DEPTH_NESTING);
+       }
+
+       if (!list_empty(&dev->unreg_list_net)) {
+               list_del(&dev->unreg_list_net);
+               list_add_tail(&dev->unreg_list_net, &net->dev_unreg_head);
+       }
+
+       spin_unlock(&net_old->dev_unreg_lock);
+       spin_unlock(&net->dev_unreg_lock);
+}
+
+void unregister_netdevice_many_net(struct net *net)
+{
+       struct net_device *dev, *tmp;
+       LIST_HEAD(unreg_head_net);

net/core/dev.c void unregister_netdevice_many(struct list_head *head) · line 12589

Quoted 10 lines
net/core/dev.c +10 -0
+       LIST_HEAD(unreg_head);
+
+       spin_lock(&net->dev_unreg_lock);
+       list_splice_init(&net->dev_unreg_head, &unreg_head_net);
+       spin_unlock(&net->dev_unreg_lock);
+
+       list_for_each_entry_safe(dev, tmp, &unreg_head_net, unreg_list_net) {
+               list_del_init(&dev->unreg_list_net);
+               list_add_tail(&dev->unreg_list, &unreg_head);
+       }

---8<---
Will this result in a stalled unregistration if a cross-netns device is deleted and concurrently moved to a new namespace?

If a user triggers deletion, unregister_netdevice_queue_net() queues the device in dev_unreg_head and schedules the async workqueue. Because unregistration runs asynchronously, dev->reg_state remains NETREG_REGISTERED initially. If the pending device is concurrently moved to a new netns via

dev_change_net_namespace(), unregister_netdevice_move_net() correctly transfers the device to the new netns's dev_unreg_head list.

However, does this block miss a call to rtnl_net_queue_work(net) to schedule processing for the new netns?
---8<---

I didn't add rtnl_net_queue_work() intentionally as mentioned in the commit message.

For now, such a race does not occur thanks to RTNL.

Once RTNL is removed, all dev_change_net_namespace() callers must be called under per-netns RTNL, so even if the race occurred, the dev would be removed at __rtnl_net_unlock().

maintainer
#10 lore raw

Re: [PATCH v2 net-next 06/14] net: Add per-netns netdev unregistration infra.

In reply to Kuniyuki Iwashima
Quoted 17 lines · Kuniyuki Iwashima

When we need to unregister a netdev in a different netns, we will delegate its unregistration to per-netns work.

There are three types of such cross-netns devices:

1. Paired devices (e.g., netkit, veth, vxcan)
-> Unregistering one device also deletes its peer, which
may reside in another netns.

2. Tunnel devices (e.g., bareudp, geneve, etc)
-> Destroying a netns removes devices in another netns if
their backend sockets reside in the dying netns

3. Stacked devices (e.g., ipvlan, macvlan, etc)
-> Removing the lower device also removes multiple upper
devices, each of which may reside in different namespaces.

Quoted 7 lines

In these cases, we will use unregister_netdevice_queue_net() to queue such potential cross-netns devices for destruction.

Each driver must not call both unregister_netdevice_queue_net() and unregister_netdevice_queue() for the same device. See the subsequent veth/bareudp/ipvlan patches for how they avoid double queueing.

Only if a repost is needed, possibly avoid the double negation above?

"""
Each driver must exactly call either unregister_netdevice_queue_net() or unregister_netdevice_queue()...
"""

/P

#11 lore raw

PATCH v2 7/14 net: Call unregister_netdevice_many() per netns.

In reply to Kuniyuki Iwashima

For per-netns device unregistration, the list passed to unregister_netdevice_many() must contain devices from a single netns only (once all callers are converted).

Let's move collected devices in the following functions to net->dev_unreg_head and let __rtnl_net_unlock() pass them to unregister_netdevice_many().

* default_device_exit_batch()
* ops_exit_rtnl_list()
* __rtnl_kill_links()

This allows incremental conversion of each driver to support per-netns device unregistration without affecting the normal kernel where CONFIG_DEBUG_NET_SMALL_RTNL is disabled.

Note that this change unbatches synchronize_rcu() etc in unregister_netdevice_many(), but we can later split it into multiple stages to batch them again.

---
 include/linux/netdevice.h |  6 ++++++
 net/core/dev.c            | 27 +++++++++++++++++++++++++++
 net/core/net_namespace.c  |  1 +
 net/core/rtnetlink.c      |  6 +++++-
 4 files changed, 39 insertions(+), 1 deletion(-)
include/linux/netdevice.h +6 -0
index 108d8d7ea75b..8db25b79573e 100644
@@ -3481,6 +3481,7 @@ static inline void unregister_netdevice(struct net_device *dev)
 void unregister_netdevice_queue_net(struct net *net, struct net_device *dev,
 				    struct list_head *head);
 void unregister_netdevice_many_net(struct net *net);
+void unregister_netdevice_queue_many_net(struct net *net, struct list_head *head);
 #else
 static inline void unregister_netdevice_queue_net(struct net *net,
 						  struct net_device *dev,
@@ -3488,6 +3489,11 @@ static inline void unregister_netdevice_queue_net(struct net *net,
 {
 	unregister_netdevice_queue(dev, head);
 }
+
+static inline void unregister_netdevice_queue_many_net(struct net *net,
+						       struct list_head *head)
+{
+}
 #endif
 
 int netdev_refcnt_read(const struct net_device *dev);
net/core/dev.c +27 -0
index ed41c704c941..c43a44fb649f 100644
@@ -12552,6 +12552,28 @@ void unregister_netdevice_queue_net(struct net *net, struct net_device *dev,
 }
 EXPORT_SYMBOL(unregister_netdevice_queue_net);
 
+void unregister_netdevice_queue_many_net(struct net *net, struct list_head *head)
+{
+	struct net_device *dev, *tmp;
+
+	spin_lock(&net->dev_unreg_lock);
+	list_for_each_entry_safe(dev, tmp, head, unreg_list) {
+		/* Once all cross-netns unregister_netdevice_queue() is
+		 * converted to _net() (or for debugging), remove this check.
+		 */
+		if (!net_eq(dev_net(dev), net))
+			continue;
+
+		DEBUG_NET_WARN_ONCE(!net_eq(dev_net(dev), net),
+				    "%s was unregistered from a different netns.\n",
+				    dev->name);
+
+		list_del_init(&dev->unreg_list);
+		list_move_tail(&dev->unreg_list_net, &net->dev_unreg_head);
+	}
+	spin_unlock(&net->dev_unreg_lock);
+}
+
 static void unregister_netdevice_move_net(struct net *net_old,
 					  struct net *net,
 					  struct net_device *dev)
@@ -13185,12 +13207,17 @@ static void __net_exit default_device_exit_batch(struct list_head *net_list)
 	__rtnl_net_unlock(&init_net);
 
 	list_for_each_entry(net, net_list, exit_list) {
+		__rtnl_net_lock(net);
+
 		for_each_netdev_reverse(net, dev) {
 			if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
 				dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
 			else
 				unregister_netdevice_queue(dev, &dev_kill_list);
 		}
+
+		unregister_netdevice_queue_many_net(net, &dev_kill_list);
+		__rtnl_net_unlock(net);
 	}
 	unregister_netdevice_many(&dev_kill_list);
 	rtnl_unlock();
net/core/net_namespace.c +1 -0
index 578b48cf5318..a91d2b58aadd 100644
@@ -181,6 +181,7 @@ static void ops_exit_rtnl_list(const struct list_head *ops_list,
 				ops->exit_rtnl(net, &dev_kill_list);
 		}
 
+		unregister_netdevice_queue_many_net(net, &dev_kill_list);
 		__rtnl_net_unlock(net);
 	}
 
net/core/rtnetlink.c +5 -1
index 544498d3c325..b129f793d851 100644
@@ -714,8 +714,12 @@ void rtnl_link_unregister(struct rtnl_link_ops *ops)
 	down_write(&pernet_ops_rwsem);
 	rtnl_lock_unregistering_all();
 
-	for_each_net(net)
+	for_each_net(net) {
+		__rtnl_net_lock(net);
 		__rtnl_kill_links(net, ops, &dev_kill_list);
+		unregister_netdevice_queue_many_net(net, &dev_kill_list);
+		__rtnl_net_unlock(net);
+	}
 
 	unregister_netdevice_many(&dev_kill_list);
 
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#12 lore raw

PATCH v2 8/14 veth: Support per-netns device unregistration.

In reply to Kuniyuki Iwashima

Currently, veth_dellink() unregisters both local and peer devices synchronously under RTNL.

Once RTNL is removed, it can be called concurrently from different netns.

Let's use xchg() and unregister_netdevice_queue_net() to support per-netns device unregistration.

This way, each device is queued for destruction only once by the winner of the race.

Note that the extra netdev_hold() ensures that @peer obtained by the first xchg() is not freed during the subsequent access to netdev_priv(peer). The 2nd xchg() overwrites @dev to balance the refcount.

Tested:

1. Create two veth pairs (veth1-2, veth3-4) between two netns (ns1 & ns2).

  # ip netns add ns1
  # ip netns add ns2
  # ip -n ns1 link add veth1 type veth peer veth2 netns ns2
  # ip -n ns1 link add veth3 type veth peer veth4 netns ns2

2. Run bpftrace to check if the same process does NOT unregister the paired veth devices

  # bpftrace -e '#include <linux/netdevice.h>
  kprobe:free_netdev {
      $dev = (struct net_device *)arg0;
      printf("PID: %d | DEV: %s%s\n", pid, $dev->name, kstack());
  }'

3. Remove veth2 in ns2 and check bpftrace output

  # ip -n ns2 link del veth2

PID: 2194 | DEV: veth2

          free_netdev+5
          netdev_run_todo+4798
          rtnl_dellink+1507
          rtnetlink_rcv_msg+1791
  ...
  PID: 448 | DEV: veth1
          free_netdev+5
          netdev_run_todo+4798
          process_scheduled_works+2538

...

4. Remove ns2 (thus veth4) and check bpftrace output

  # ip netns del ns2

PID: 571 | DEV: veth4

          free_netdev+5
          netdev_run_todo+4798
          default_device_exit_batch+2271
          ops_undo_list+993
          cleanup_net+1122
          process_scheduled_works+2538
  ...
  PID: 441 | DEV: veth3
          free_netdev+5
          netdev_run_todo+4798
          process_scheduled_works+2538

...

---
 drivers/net/veth.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/net/veth.c +21 -13
index 1c5142149175..8170bf33ccf9 100644
@@ -77,6 +77,7 @@ struct veth_priv {
 	struct bpf_prog		*_xdp_prog;
 	struct veth_rq		*rq;
 	unsigned int		requested_headroom;
+	netdevice_tracker	peer_tracker;
 };
 
 struct veth_xdp_tx_bq {
@@ -1901,15 +1902,17 @@ static int veth_newlink(struct net_device *dev,
 
 	priv = netdev_priv(dev);
 	rcu_assign_pointer(priv->peer, peer);
+	netdev_hold(peer, &priv->peer_tracker, GFP_KERNEL);
 	err = veth_init_queues(dev, tb);
 	if (err)
 		goto err_queues;
 
 	priv = netdev_priv(peer);
 	rcu_assign_pointer(priv->peer, dev);
+	netdev_hold(dev, &priv->peer_tracker, GFP_KERNEL);
 	err = veth_init_queues(peer, tb);
 	if (err)
-		goto err_queues;
+		goto err_peer_queues;
 
 	veth_disable_gro(dev);
 	/* update XDP supported features */
@@ -1918,7 +1921,11 @@ static int veth_newlink(struct net_device *dev,
 
 	return 0;
 
+err_peer_queues:
+	netdev_put(dev, &priv->peer_tracker);
+	priv = netdev_priv(dev);
 err_queues:
+	netdev_put(peer, &priv->peer_tracker);
 	unregister_netdevice(dev);
 err_register_dev:
 	/* nothing to do */
@@ -1933,24 +1940,25 @@ static int veth_newlink(struct net_device *dev,
 
 static void veth_dellink(struct net_device *dev, struct list_head *head)
 {
-	struct veth_priv *priv;
+	netdevice_tracker *peer_tracker;
 	struct net_device *peer;
+	struct veth_priv *priv;
 
 	priv = netdev_priv(dev);
-	peer = rtnl_dereference(priv->peer);
+	peer_tracker = &priv->peer_tracker;
+	peer = unrcu_pointer(xchg(&priv->peer, NULL));
+	if (!peer)
+		return;
 
-	/* Note : dellink() is called from default_device_exit_batch(),
-	 * before a rcu_synchronize() point. The devices are guaranteed
-	 * not being freed before one RCU grace period.
-	 */
-	RCU_INIT_POINTER(priv->peer, NULL);
 	unregister_netdevice_queue(dev, head);
 
-	if (peer) {
-		priv = netdev_priv(peer);
-		RCU_INIT_POINTER(priv->peer, NULL);
-		unregister_netdevice_queue(peer, head);
-	}
+	priv = netdev_priv(peer);
+	dev = unrcu_pointer(xchg(&priv->peer, NULL));
+	if (dev)
+		unregister_netdevice_queue_net(dev_net(dev), peer, head);
+
+	netdev_put(peer, peer_tracker);
+	netdev_put(dev, &priv->peer_tracker);
 }
 
 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#13 lore raw

Re: [PATCH v2 net-next 08/14] veth: Support per-netns device unregistration.

Replying to Sashiko review,

On Thu, Jul 2, 2026 at 5:10 PM Kuniyuki Iwashima <[email protected]> wrote:
[...]

drivers/net/veth.c static int veth_newlink(struct net_device *dev, · line 1905

Quoted 5 lines
drivers/net/veth.c +1 -0
@@ -1901,15 +1902,17 @@ static int veth_newlink(struct net_device *dev,
 
        priv = netdev_priv(dev);
        rcu_assign_pointer(priv->peer, peer);
+       netdev_hold(peer, &priv->peer_tracker, GFP_KERNEL);

---8<---
Because the devices are already registered and visible to the system at this point, could a concurrent ip link del trigger veth_dellink() right after rcu_assign_pointer() publishes priv->peer, but before netdev_hold() is called?
---8<---

No, ->newlink() and ->dellink() are serialised with the same rtnl_net_lock().

Quoted 15 lines
drivers/net/veth.c +2 -1
        err = veth_init_queues(dev, tb);
        if (err)
                goto err_queues;
 
        priv = netdev_priv(peer);
        rcu_assign_pointer(priv->peer, dev);
+       netdev_hold(dev, &priv->peer_tracker, GFP_KERNEL);
        err = veth_init_queues(peer, tb);
        if (err)
-               goto err_queues;
+               goto err_peer_queues;
 
        veth_disable_gro(dev);
        /* update XDP supported features */

drivers/net/veth.c static int veth_newlink(struct net_device *dev, · line 1929

Quoted 9 lines
drivers/net/veth.c +4 -0
@@ -1918,7 +1921,11 @@ static int veth_newlink(struct net_device *dev,
 
        return 0;
 
+err_peer_queues:
+       netdev_put(dev, &priv->peer_tracker);
+       priv = netdev_priv(dev);
 err_queues:
+       netdev_put(peer, &priv->peer_tracker);
        unregister_netdevice(dev);

---8<---
If veth_init_queues() fails, does this error path safely handle a concurrent veth_dellink()?
---8<---

This depends on the same wrong assumption, so no, ->newlink() holds multiple rtnl_net_lock() so that the concurrent ->dellink() never occurs.

Quoted 5 lines
drivers/net/veth.c +0 -0
 err_register_dev:
        /* nothing to do */
@@ -1933,24 +1940,25 @@ static int veth_newlink(struct net_device *dev,
 
 static void veth_dellink(struct net_device *dev, struct list_head *head)

drivers/net/veth.c static int veth_newlink(struct net_device *dev, · line 1949

Quoted 10 lines
drivers/net/veth.c +4 -2
 {
-       struct veth_priv *priv;
+       netdevice_tracker *peer_tracker;
        struct net_device *peer;
+       struct veth_priv *priv;
 
        priv = netdev_priv(dev);
-       peer = rtnl_dereference(priv->peer);
+       peer_tracker = &priv->peer_tracker;
+       peer = unrcu_pointer(xchg(&priv->peer, NULL));

---8<---
Does changing this to use xchg() introduce a data race with read paths? Functions like veth_set_xdp_features() still use rtnl_dereference(),
...
Might this need READ_ONCE() or rcu_dereference() to safely handle the concurrent xchg() writes?
---8<---

Yes, but with another series before removing RTNL.

As mentioned in the cover letter, the device unreg path is the only target of this series, and we need more changes in other paths.

#14 lore raw

PATCH v2 9/14 bareudp: Protect bareudp_list with mutex.

In reply to Kuniyuki Iwashima

struct bareudp_dev.net is the netns where the backend bareudp socket resides.

struct bareudp_dev is linked to the bareudp_net.bareudp_list of the socket's netns.

During netns dismantle or module unload, bareudp_exit_rtnl_net() iterates the list and queues devices for destruction regardless of the devices' netns.

Thus, once RTNL is removed, the list can be modified concurrently from different netns due to device removal.

Let's protect it with per-netns mutex.

bareudp_newlink() is still protected by rtnl_net_lock()s, so acquiring bn->lock twice in bareudp_find_dev() and bareudp_configure() is not a problem.

---
 drivers/net/bareudp.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/net/bareudp.c +29 -2
index 5ef841c85526..7dedf4867e7b 100644
@@ -36,6 +36,7 @@ static unsigned int bareudp_net_id;
 
 struct bareudp_net {
 	struct list_head        bareudp_list;
+	struct mutex		lock;
 };
 
 struct bareudp_conf {
@@ -636,10 +637,15 @@ static struct bareudp_dev *bareudp_find_dev(struct bareudp_net *bn,
 {
 	struct bareudp_dev *bareudp, *t = NULL;
 
+	mutex_lock(&bn->lock);
+
 	list_for_each_entry(bareudp, &bn->bareudp_list, next) {
 		if (conf->port == bareudp->port)
 			t = bareudp;
 	}
+
+	mutex_unlock(&bn->lock);
+
 	return t;
 }
 
@@ -675,7 +681,10 @@ static int bareudp_configure(struct net *net, struct net_device *dev,
 	if (err)
 		return err;
 
+	mutex_lock(&bn->lock);
 	list_add(&bareudp->next, &bn->bareudp_list);
+	mutex_unlock(&bn->lock);
+
 	return 0;
 }
 
@@ -692,7 +701,7 @@ static int bareudp_link_config(struct net_device *dev,
 	return 0;
 }
 
-static void bareudp_dellink(struct net_device *dev, struct list_head *head)
+static void __bareudp_dellink(struct net_device *dev, struct list_head *head)
 {
 	struct bareudp_dev *bareudp = netdev_priv(dev);
 
@@ -700,6 +709,18 @@ static void bareudp_dellink(struct net_device *dev, struct list_head *head)
 	unregister_netdevice_queue(dev, head);
 }
 
+static void bareudp_dellink(struct net_device *dev, struct list_head *head)
+{
+	struct bareudp_dev *bareudp = netdev_priv(dev);
+	struct bareudp_net *bn;
+
+	bn = net_generic(bareudp->net, bareudp_net_id);
+
+	mutex_lock(&bn->lock);
+	__bareudp_dellink(dev, head);
+	mutex_unlock(&bn->lock);
+}
+
 static int bareudp_newlink(struct net_device *dev,
 			   struct rtnl_newlink_params *params,
 			   struct netlink_ext_ack *extack)
@@ -776,6 +797,8 @@ static __net_init int bareudp_init_net(struct net *net)
 	struct bareudp_net *bn = net_generic(net, bareudp_net_id);
 
 	INIT_LIST_HEAD(&bn->bareudp_list);
+	mutex_init(&bn->lock);
+
 	return 0;
 }
 
@@ -785,8 +808,12 @@ static void __net_exit bareudp_exit_rtnl_net(struct net *net,
 	struct bareudp_net *bn = net_generic(net, bareudp_net_id);
 	struct bareudp_dev *bareudp, *next;
 
+	mutex_lock(&bn->lock);
+
 	list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
-		bareudp_dellink(bareudp->dev, dev_kill_list);
+		__bareudp_dellink(bareudp->dev, dev_kill_list);
+
+	mutex_unlock(&bn->lock);
 }
 
 static struct pernet_operations bareudp_net_ops = {
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#15 lore raw

PATCH v2 10/14 bareudp: Support per-netns netdev unregistration.

In reply to Kuniyuki Iwashima

bareudp_exit_rtnl_net() iterates bareudp devices whose sockets are in the dying netns and queues them for destruction.

So the devices may reside in different netns.

Let's use unregister_netdevice_queue_net() to support per-netns device unregistration.

list_del() is changed to list_del_init() to avoid queueing the same device twice.

Even after bareudp_exit_rtnl_net() queues a cross-netns bareudp device, bareudp_dellink() could be called concurrently for it (once RTNL is removed). In such a case, __rtnl_net_unlock() will perform the unregistration.

Note that bareudp uses register_pernet_subsys() instead of _device(), so default_device_exit_batch() guarantees that the async per-netns works are flushed before ->exit().

Tested:

1. Create bareudp device across two netns.

  # ip netns add ns1
  # ip netns add ns2
  # ip -n ns1 link add bareudp0 link-netns ns2 type bareudp \
    dstport 9292 ethertype ipv4

2. Run bpftrace to check that bareudp_uninit() is called between ->exit_rtnl() and ->exit().

  # bpftrace -e '#include <linux/netdevice.h>
  kprobe:bareudp_uninit {
      $dev = (struct net_device *)arg0;
      printf("PID: %d | DEV: %s%s\n", pid, $dev->name, kstack());
  }
  kprobe:bareudp_exit_rtnl_net,
  kprobe:bareudp_exit_net {
      printf("PID: %d%s\n", pid, kstack());
  }'

3. Remove the netns where the bareudp socket resides

  # ip netns del ns2

Now, we can see bareudp0 is unregistered by per-netns work instead of cleanup_net() and it finishes before ->exit() to avoid WARN_ON_ONCE(!list_empty(&bn->bareudp_list)) there.

PID: 576

          bareudp_exit_rtnl_net+5
          ops_undo_list+702
          cleanup_net+1122
          process_scheduled_works+2538
  ...
  PID: 470 | DEV: bareudp0
          bareudp_uninit+5
          unregister_netdevice_many_notify+7129
          unregister_netdevice_many_net+1050
          rtnl_net_work_func+136
          process_scheduled_works+2538
  ...
  PID: 576
          bareudp_exit_net+5
          ops_undo_list+1064
          cleanup_net+1122
          process_scheduled_works+2538
---
 drivers/net/bareudp.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/net/bareudp.c +15 -5
index 7dedf4867e7b..c3b5ed52d877 100644
@@ -701,12 +701,13 @@ static int bareudp_link_config(struct net_device *dev,
 	return 0;
 }
 
-static void __bareudp_dellink(struct net_device *dev, struct list_head *head)
+static void __bareudp_dellink(struct net *net, struct net_device *dev,
+			      struct list_head *head)
 {
 	struct bareudp_dev *bareudp = netdev_priv(dev);
 
-	list_del(&bareudp->next);
-	unregister_netdevice_queue(dev, head);
+	list_del_init(&bareudp->next);
+	unregister_netdevice_queue_net(net, dev, head);
 }
 
 static void bareudp_dellink(struct net_device *dev, struct list_head *head)
@@ -717,7 +718,8 @@ static void bareudp_dellink(struct net_device *dev, struct list_head *head)
 	bn = net_generic(bareudp->net, bareudp_net_id);
 
 	mutex_lock(&bn->lock);
-	__bareudp_dellink(dev, head);
+	if (!list_empty(&bareudp->next))
+		__bareudp_dellink(dev_net(dev), dev, head);
 	mutex_unlock(&bn->lock);
 }
 
@@ -811,14 +813,22 @@ static void __net_exit bareudp_exit_rtnl_net(struct net *net,
 	mutex_lock(&bn->lock);
 
 	list_for_each_entry_safe(bareudp, next, &bn->bareudp_list, next)
-		__bareudp_dellink(bareudp->dev, dev_kill_list);
+		__bareudp_dellink(net, bareudp->dev, dev_kill_list);
 
 	mutex_unlock(&bn->lock);
 }
 
+static void __net_exit bareudp_exit_net(struct net *net)
+{
+	struct bareudp_net *bn = net_generic(net, bareudp_net_id);
+
+	WARN_ON_ONCE(!list_empty(&bn->bareudp_list));
+}
+
 static struct pernet_operations bareudp_net_ops = {
 	.init = bareudp_init_net,
 	.exit_rtnl = bareudp_exit_rtnl_net,
+	.exit = bareudp_exit_net,
 	.id   = &bareudp_net_id,
 	.size = sizeof(struct bareudp_net),
 };
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#16 lore raw

PATCH v2 11/14 ipvlan: Convert ipvl_port.count to refcount_t.

In reply to Kuniyuki Iwashima

struct ipvl_port is shared between a lower device and its upper ipvlan devices.

While each upper device can always access ipvl_port safely via ipvlan_dev.port, the lower device relies on RTNL to access it via net_device.rx_handler_data.

Once RTNL is removed, the lower device cannot read ipvl_port safely in ipvlan_device_event() because the port could be freed concurrently and net_device.rx_handler_data is set to NULL if the last ipvlan device in another namespace is unregistered.

Let's convert ipvl_port.count to refcount_t and use RCU along with refcount_inc_not_zero() in ipvlan_device_event().

netdev_put() in ipvlan_port_destroy() is also moved down after cancel_work_sync(), which is the last user of port->dev.

Note that ipvlan->port is now set in ipvlan_init() so that it can be used in ipvlan_uninit(), instead of ipvlan_port_get_rtnl() (rtnl_dereference()).

---
 drivers/net/ipvlan/ipvlan.h      |  2 +-
 drivers/net/ipvlan/ipvlan_main.c | 75 ++++++++++++++++++++++----------
 2 files changed, 52 insertions(+), 25 deletions(-)
drivers/net/ipvlan/ipvlan.h +1 -1
index 80f84fc87008..78f9107fa752 100644
@@ -96,7 +96,7 @@ struct ipvl_port {
 	u16			dev_id_start;
 	struct work_struct	wq;
 	struct sk_buff_head	backlog;
-	int			count;
+	refcount_t		count;
 	struct ida		ida;
 	netdevice_tracker	dev_tracker;
 };
drivers/net/ipvlan/ipvlan_main.c +51 -24

148 lines not on this page · raw message

Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#17 lore raw

Re: [PATCH v2 net-next 11/14] ipvlan: Convert ipvl_port.count to refcount_t.

Replying to Sashiko review,

On Thu, Jul 2, 2026 at 5:10 PM Kuniyuki Iwashima <[email protected]> wrote:
[...]

drivers/net/ipvlan/ipvlan_main.c static int ipvlan_port_create(struct net_device *dev) · line 105

Quoted 12 lines
drivers/net/ipvlan/ipvlan_main.c +3 -3
-static void ipvlan_port_destroy(struct net_device *dev)
+static void ipvlan_port_destroy(struct ipvl_port *port)
 {
-       struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
+       struct net_device *dev = port->dev;
        struct sk_buff *skb;
 
-       netdev_put(dev, &port->dev_tracker);
        if (port->mode == IPVLAN_MODE_L3S)
                ipvlan_l3s_unregister(port);
+
        netdev_rx_handler_unregister(dev);

---8<---
Can this trigger a lockdep warning due to a missing global RTNL lock? ---8<--

Once RTNL is removed, yes, but out-of-scope of this series as mentioned in the cover letter.

Quoted 14 lines
        cancel_work_sync(&port->wq);
drivers/net/ipvlan/ipvlan_main.c +6 -0
+       netdev_put(dev, &port->dev_tracker);
+
        while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
                dev_put(skb->dev);
                kfree_skb(skb);
@@ -111,6 +114,27 @@ static void ipvlan_port_destroy(struct net_device *dev)
        kfree(port);
 }
 
+static void ipvlan_port_put(struct ipvl_port *port)
+{
+       if (refcount_dec_and_test(&port->count))
+               ipvlan_port_destroy(port);

drivers/net/ipvlan/ipvlan_main.c static void ipvlan_port_destroy(struct net_device *dev) · line 130

Quoted 10 lines
drivers/net/ipvlan/ipvlan_main.c +10 -0
+}
+
+static struct ipvl_port *ipvlan_port_get(struct net_device *dev)
+{
+       struct ipvl_port *port = NULL;
+
+       rcu_read_lock();
+       if (netif_is_ipvlan_port(dev)) {
+               port = ipvlan_port_get_rcu(dev);
+               if (!refcount_inc_not_zero(&port->count))

---8<---
Is there a potential NULL pointer dereference here due to CPU read-read reordering?
In ipvlan_port_get(), netif_is_ipvlan_port(dev) checks dev->rx_handler, and then ipvlan_port_get_rcu(dev) fetches dev->rx_handler_data. Because there is only a control dependency and no data dependency or memory barrier between these reads, the CPU might speculatively reorder them. If this races with netdev_rx_handler_register() on another CPU during interface creation, could the reader fetch the old NULL rx_handler_data along with the new non-NULL rx_handler, resulting in a NULL port pointer being passed to refcount_inc_not_zero()?
---8<---

No, ipvlan_port_get() is called from ipvlan_device_event(), and netdev_rx_handler_register() is called from ->newlink(), so they are under the same rtnl_net_lock().

#18 lore raw

PATCH v2 12/14 ipvlan: Synchronise ipvlan_init() and ipvlan_uninit() for the same lower dev.

In reply to Kuniyuki Iwashima

ipvlan_uninit() for the last ipvlan device resets the lower device's rx_handler_data to NULL.

Once RTNL is removed, ipvlan_init() would race with ipvlan_uninit(), which could leak a newly allocated ipvl_port.

ipvlan_init() ipvlan_uninit()

  |                               |- if (refcount_dec_and_test(old_port))
  ...                                |- ipvlan_port_destroy(old_port)
  |                                     '
  |- refcount_inc_not_zero(old_port) <-- fails
  |- ipvlan_port_create(phy_dev)        .
     |- new_port = kzalloc()            |
     |- phy_dev->rx_handler_data = new_port
                                        |- phy_dev->rx_handler_data = NULL
					...
					`- kfree(old_port);

Let's synchronise the two by holding the lower device's netdev_lock().

---
 drivers/net/ipvlan/ipvlan_main.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/net/ipvlan/ipvlan_main.c +15 -0
index b4906a8d24ef..7adad781e9b5 100644
@@ -177,9 +177,12 @@ static int ipvlan_init(struct net_device *dev)
 	if (!ipvlan->pcpu_stats)
 		return -ENOMEM;
 
+	netdev_lock(phy_dev);
+
 	if (!netif_is_ipvlan_port(phy_dev)) {
 		err = ipvlan_port_create(phy_dev);
 		if (err < 0) {
+			netdev_unlock(phy_dev);
 			free_percpu(ipvlan->pcpu_stats);
 			return err;
 		}
@@ -190,6 +193,8 @@ static int ipvlan_init(struct net_device *dev)
 		refcount_inc(&port->count);
 	}
 
+	netdev_unlock(phy_dev);
+
 	ipvlan->port = port;
 
 	return 0;
@@ -198,9 +203,19 @@ static int ipvlan_init(struct net_device *dev)
 static void ipvlan_uninit(struct net_device *dev)
 {
 	struct ipvl_dev *ipvlan = netdev_priv(dev);
+	netdevice_tracker dev_tracker;
+	struct net_device *phy_dev;
 
 	free_percpu(ipvlan->pcpu_stats);
+
+	phy_dev = ipvlan->phy_dev;
+	netdev_hold(phy_dev, &dev_tracker, GFP_KERNEL);
+	netdev_lock(phy_dev);
+
 	ipvlan_port_put(ipvlan->port);
+
+	netdev_unlock(phy_dev);
+	netdev_put(phy_dev, &dev_tracker);
 }
 
 static int ipvlan_open(struct net_device *dev)
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#19 lore raw

Re: [PATCH v2 net-next 12/14] ipvlan: Synchronise ipvlan_init() and ipvlan_uninit() for the same lower dev.

Replying to Sashiko review,

Quoted 44 lines · Kuniyuki Iwashima

ipvlan_uninit() for the last ipvlan device resets the lower device's rx_handler_data to NULL.

Once RTNL is removed, ipvlan_init() would race with ipvlan_uninit(), which could leak a newly allocated ipvl_port.

  ipvlan_init()                   ipvlan_uninit()
  |                               |- if (refcount_dec_and_test(old_port))
  ...                                |- ipvlan_port_destroy(old_port)
  |                                     '
  |- refcount_inc_not_zero(old_port) <-- fails
  |- ipvlan_port_create(phy_dev)        .
     |- new_port = kzalloc()            |
     |- phy_dev->rx_handler_data = new_port
                                        |- phy_dev->rx_handler_data = NULL
                                        ...
                                        `- kfree(old_port);

Let's synchronise the two by holding the lower device's netdev_lock().

---
 drivers/net/ipvlan/ipvlan_main.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/net/ipvlan/ipvlan_main.c +3 -0
index b4906a8d24ef..7adad781e9b5 100644
@@ -177,9 +177,12 @@ static int ipvlan_init(struct net_device *dev)
        if (!ipvlan->pcpu_stats)
                return -ENOMEM;
 
+       netdev_lock(phy_dev);
+
        if (!netif_is_ipvlan_port(phy_dev)) {
                err = ipvlan_port_create(phy_dev);
                if (err < 0) {
+                       netdev_unlock(phy_dev);
                        free_percpu(ipvlan->pcpu_stats);
                        return err;
                }

drivers/net/ipvlan/ipvlan_main.c static int ipvlan_init(struct net_device *dev) · line 194

Quoted 2 lines
drivers/net/ipvlan/ipvlan_main.c +0 -0
@@ -190,6 +193,8 @@ static int ipvlan_init(struct net_device *dev)
                refcount_inc(&port->count);
        }

---8<---
Does this fully resolve the race condition if ipvlan_device_event() drops the final reference?
---8<---

Yes. netdev_lock() here is to prevent race between ->ndo_init() (newlink()) and ->ndo_uninit() of an upper device, where the latter does not always hold the same rtnl_net_lock() with the former.

---8<---
Because ipvlan_device_event() temporarily acquires a port reference via ipvlan_port_get() and releases it via ipvlan_port_put() without holding netdev_lock(phy_dev), could the following sequence occur? ---8<---

ipvlan_device_event() for the lower dev and ipvlan_init() (->newlink()) for the upper dev are under the same rtnl_net_lock(), so no race occurs.

#20 lore raw

PATCH v2 13/14 ipvlan: Protect ipvl_port.ipvlans with mutex.

In reply to Kuniyuki Iwashima

struct ipvl_port is shared between a lower device and its upper ipvlan devices.

All upper devices are linked to ipvl_port.ipvlans.

Once RTNL is removed, the list can be modified concurrently from different netns due to device removal.

Let's protect it with a per-port mutex.

NETDEV_PRECHANGEUPPER and NETDEV_CHANGEUPPER are explicitly skipped to avoid deadlock for netdev_upper_dev_unlink() called from NETDEV_UNREGISTER.

Note that __ipvtap_dellink_ptr is added for CONFIG_IPVLAN=y but CONFIG_TAP=m and CONFIG_IPVTAP=m.

---
v2: Add __ipvtap_dellink_ptr for CONFIG_IPVLAN=y and CONFIG_TAP=m

---
 drivers/net/ipvlan/ipvlan.h      |  8 +++++-
 drivers/net/ipvlan/ipvlan_main.c | 49 ++++++++++++++++++++++++++++----
 drivers/net/ipvlan/ipvtap.c      | 23 ++++++++++++---
 3 files changed, 70 insertions(+), 10 deletions(-)
drivers/net/ipvlan/ipvlan.h +7 -1
index 78f9107fa752..9d3835c14e5e 100644
@@ -91,6 +91,7 @@ struct ipvl_port {
 	struct hlist_head	hlhead[IPVLAN_HASH_SIZE];
 	spinlock_t		addrs_lock; /* guards hash-table and addrs */
 	struct list_head	ipvlans;
+	struct mutex		pnodes_lock;
 	u16			mode;
 	u16			flags;
 	u16			dev_id_start;
@@ -168,7 +169,7 @@ void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
 		     unsigned int len, bool success, bool mcast);
 int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params,
 		    struct netlink_ext_ack *extack);
-void ipvlan_link_delete(struct net_device *dev, struct list_head *head);
+void __ipvlan_link_delete(struct net_device *dev, struct list_head *head);
 void ipvlan_link_setup(struct net_device *dev);
 int ipvlan_link_register(struct rtnl_link_ops *ops);
 #ifdef CONFIG_IPVLAN_L3S
@@ -207,4 +208,9 @@ static inline bool netif_is_ipvlan_port(const struct net_device *dev)
 	return rcu_access_pointer(dev->rx_handler) == ipvlan_handle_frame;
 }
 
+#if IS_ENABLED(CONFIG_IPVTAP)
+extern void (*__ipvtap_dellink_ptr)(struct net_device *dev,
+				    struct list_head *head);
+#endif
+
 #endif /* __IPVLAN_H */
drivers/net/ipvlan/ipvlan_main.c +44 -5

120 lines not on this page · raw message

drivers/net/ipvlan/ipvtap.c +19 -4
index 2d6bbddd1edd..99eaa29057b4 100644
@@ -109,14 +109,24 @@ static int ipvtap_newlink(struct net_device *dev,
 	return err;
 }
 
+static void __ipvtap_dellink(struct net_device *dev, struct list_head *head)
+{
+	struct ipvtap_dev *vlantap = netdev_priv(dev);
+
+	netdev_rx_handler_unregister(dev);
+	tap_del_queues(&vlantap->tap);
+	__ipvlan_link_delete(dev, head);
+}
+
 static void ipvtap_dellink(struct net_device *dev,
 			   struct list_head *head)
 {
-	struct ipvtap_dev *vlan = netdev_priv(dev);
+	struct ipvtap_dev *vlantap = netdev_priv(dev);
+	struct ipvl_port *port = vlantap->vlan.port;
 
-	netdev_rx_handler_unregister(dev);
-	tap_del_queues(&vlan->tap);
-	ipvlan_link_delete(dev, head);
+	mutex_lock(&port->pnodes_lock);
+	__ipvtap_dellink(dev, head);
+	mutex_unlock(&port->pnodes_lock);
 }
 
 static void ipvtap_setup(struct net_device *dev)
@@ -198,6 +208,8 @@ static int __init ipvtap_init(void)
 {
 	int err;
 
+	__ipvtap_dellink_ptr = __ipvtap_dellink;
+
 	err = tap_create_cdev(&ipvtap_cdev, &ipvtap_major, "ipvtap",
 			      THIS_MODULE);
 	if (err)
@@ -224,6 +236,8 @@ static int __init ipvtap_init(void)
 out2:
 	tap_destroy_cdev(ipvtap_major, &ipvtap_cdev);
 out1:
+	__ipvtap_dellink_ptr = NULL;
+
 	return err;
 }
 module_init(ipvtap_init);
@@ -234,6 +248,7 @@ static void __exit ipvtap_exit(void)
 	unregister_netdevice_notifier(&ipvtap_notifier_block);
 	class_unregister(&ipvtap_class);
 	tap_destroy_cdev(ipvtap_major, &ipvtap_cdev);
+	__ipvtap_dellink_ptr = NULL;
 }
 module_exit(ipvtap_exit);
 MODULE_ALIAS_RTNL_LINK("ipvtap");
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
maintainer
#21 lore raw

Re: [PATCH v2 net-next 13/14] ipvlan: Protect ipvl_port.ipvlans with mutex.

Quoted 4 lines · Kuniyuki Iwashima
drivers/net/ipvlan/ipvlan_main.c +0 -0
@@ -800,9 +831,15 @@ static int ipvlan_device_event(struct notifier_block *unused,
 		if (dev->reg_state != NETREG_UNREGISTERING)
 			break;
 

drivers/net/ipvlan/ipvlan_main.c static int ipvlan_device_event(struct notifier_block *unused, · line 840

Quoted 10 lines
drivers/net/ipvlan/ipvlan_main.c +7 -3
-		list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
-			ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
-							    &lst_kill);
+		list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode) {
+#if IS_ENABLED(CONFIG_IPVTAP)
+			if (ipvlan->dev->rtnl_link_ops != &ipvlan_link_ops)
+				__ipvtap_dellink_ptr(ipvlan->dev, &lst_kill);
+			else
+#endif
+				__ipvlan_link_delete(ipvlan->dev, &lst_kill);

I'm not sure if it's worthy a repost, but what about adding a link_delete_unlocked() cb to struct ipvl_dev *? IMHO should make this code more straight forward.

/P

#22 lore raw

Re: [PATCH v2 net-next 13/14] ipvlan: Protect ipvl_port.ipvlans with mutex.

Quoted 6 lines · Paolo Abeni
Quoted 4 lines · Kuniyuki Iwashima
drivers/net/ipvlan/ipvlan_main.c +0 -0
@@ -800,9 +831,15 @@ static int ipvlan_device_event(struct notifier_block *unused,
              if (dev->reg_state != NETREG_UNREGISTERING)
                      break;
 
Quoted 14 lines

drivers/net/ipvlan/ipvlan_main.c static int ipvlan_device_event(struct notifier_block *unused, · line 840

Quoted 10 lines
drivers/net/ipvlan/ipvlan_main.c +7 -3
-             list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
-                     ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
-                                                         &lst_kill);
+             list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode) {
+#if IS_ENABLED(CONFIG_IPVTAP)
+                     if (ipvlan->dev->rtnl_link_ops != &ipvlan_link_ops)
+                             __ipvtap_dellink_ptr(ipvlan->dev, &lst_kill);
+                     else
+#endif
+                             __ipvlan_link_delete(ipvlan->dev, &lst_kill);

I'm not sure if it's worthy a repost, but what about adding a link_delete_unlocked() cb to struct ipvl_dev *? IMHO should make this code more straight forward.

Since port is allocated in ndo_init(), ipvtap needs to wrap it and overwrite port->ops everytime for one time (or at ->newlink), but I don't have strong opinion here.

It would be nicer if I could follow up since this series gets 5-days penalty on Suie due to too many Sashiko false-positive, get_net(), and a build warning due to xchg() for veth, and we will likely lose context next week :)

maintainer
#23 lore raw

Re: [PATCH v2 net-next 13/14] ipvlan: Protect ipvl_port.ipvlans with mutex.

Quoted 21 lines · Kuniyuki Iwashima
Quoted 5 lines · Paolo Abeni
Quoted 4 lines · Kuniyuki Iwashima
drivers/net/ipvlan/ipvlan_main.c +0 -0
@@ -800,9 +831,15 @@ static int ipvlan_device_event(struct notifier_block *unused,
              if (dev->reg_state != NETREG_UNREGISTERING)
                      break;
 
Quoted 14 lines

drivers/net/ipvlan/ipvlan_main.c static int ipvlan_device_event(struct notifier_block *unused, · line 840

Quoted 10 lines
drivers/net/ipvlan/ipvlan_main.c +7 -3
-             list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
-                     ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
-                                                         &lst_kill);
+             list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode) {
+#if IS_ENABLED(CONFIG_IPVTAP)
+                     if (ipvlan->dev->rtnl_link_ops != &ipvlan_link_ops)
+                             __ipvtap_dellink_ptr(ipvlan->dev, &lst_kill);
+                     else
+#endif
+                             __ipvlan_link_delete(ipvlan->dev, &lst_kill);

I'm not sure if it's worthy a repost, but what about adding a link_delete_unlocked() cb to struct ipvl_dev *? IMHO should make this code more straight forward.

Quoted 8 lines

Since port is allocated in ndo_init(), ipvtap needs to wrap it and overwrite port->ops everytime for one time (or at ->newlink), but I don't have strong opinion here.

It would be nicer if I could follow up since this series gets 5-days penalty on Suie due to too many Sashiko false-positive, get_net(), and a build warning due to xchg() for veth, and we will likely lose context next week :)

FTR, I'm not strictly following Suie order - it's more a mix of Suie, FIFO and common sense. What impact most in series processing is the sheer amount of patches in PW vs avail time.

/P

#24 lore raw

PATCH v2 14/14 ipvlan: Support per-netns netdev unregistration.

In reply to Kuniyuki Iwashima

When a lower device is unregistered, its upper ipvlan devices must also be unregistered. However, these upper devices may reside in different netns than the lower device.

Let's use unregister_netdevice_queue_net() to support per-netns device unregistration for ipvlan.

The new dying flag in struct ipvl_dev is used to avoid a race that ipvlan_link_delete() is called while its lower device is being removed in ipvlan_device_event().

If dying is true in ipvlan_link_delete(), the ipvlan device is already destructed but not yet unregistered. In this case, unregistration will be done in __rtnl_net_unlock() of the ->dellink() caller.

Tested:

1. Create veth in ns1 and two ipvlan devices in ns2 and ns3.

  # ip netns add ns1
  # ip netns add ns2
  # ip netns add ns3
  # ip -n ns1 link add veth0 type veth peer veth1
  # ip -n ns2 link add ipvl2 link veth0 link-netns ns1 type ipvlan mode l2
  # ip -n ns3 link add ipvl3 link veth0 link-netns ns1 type ipvlan mode l2

2. Run bpftrace to check that veth is unregistered first but wait ipvlan to be unregistered

  # bpftrace -e '#include <linux/netdevice.h>
  kprobe:ipvlan_uninit,
  kprobe:veth_dellink,
  kprobe:free_netdev {
      $dev = (struct net_device *)arg0;
      printf("PID: %d | DEV: %s%s\n", pid, $dev->name, kstack());
  }'

3. Remove the lower veth0 in ns1.

  # ip -n ns1 link del veth0

We can see that veth0 is freed after unregistering ipvl2 and ipvl3 in per-netns work because ipvl_port holds refcount of veth0.

PID: 2010 | DEV: veth0

          veth_dellink+5
          rtnl_dellink+1213
          rtnetlink_rcv_msg+1791
  ...
  PID: 440 | DEV: ipvl2
          ipvlan_uninit+5
          unregister_netdevice_many_notify+7129
          unregister_netdevice_many_net+1050
          rtnl_net_work_func+136
          process_scheduled_works+2538
  ...
  PID: 440 | DEV: ipvl2
          free_netdev+5
          netdev_run_todo+4798
          process_scheduled_works+2538
  ...
  PID: 440 | DEV: ipvl3
          ipvlan_uninit+5
          unregister_netdevice_many_notify+7129
          unregister_netdevice_many_net+1050
          rtnl_net_work_func+136
          process_scheduled_works+2538
  ...
  PID: 2010 | DEV: veth0
          free_netdev+5
          netdev_run_todo+4798
          rtnl_dellink+1507
          rtnetlink_rcv_msg+1791
  ...
  PID: 440 | DEV: ipvl3
          free_netdev+5
          netdev_run_todo+4798
          process_scheduled_works+2538

...

---
 drivers/net/ipvlan/ipvlan.h      |  6 ++++--
 drivers/net/ipvlan/ipvlan_main.c | 22 ++++++++++++++--------
 drivers/net/ipvlan/ipvtap.c      |  8 +++++---
 3 files changed, 23 insertions(+), 13 deletions(-)
drivers/net/ipvlan/ipvlan.h +4 -2
index 9d3835c14e5e..8d05ad480438 100644
@@ -69,6 +69,7 @@ struct ipvl_dev {
 	DECLARE_BITMAP(mac_filters, IPVLAN_MAC_FILTER_SIZE);
 	netdev_features_t	sfeatures;
 	u32			msg_enable;
+	bool			dying;
 };
 
 struct ipvl_addr {
@@ -169,7 +170,8 @@ void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
 		     unsigned int len, bool success, bool mcast);
 int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params,
 		    struct netlink_ext_ack *extack);
-void __ipvlan_link_delete(struct net_device *dev, struct list_head *head);
+void __ipvlan_link_delete(struct net *net, struct net_device *dev,
+			  struct list_head *head);
 void ipvlan_link_setup(struct net_device *dev);
 int ipvlan_link_register(struct rtnl_link_ops *ops);
 #ifdef CONFIG_IPVLAN_L3S
@@ -209,7 +211,7 @@ static inline bool netif_is_ipvlan_port(const struct net_device *dev)
 }
 
 #if IS_ENABLED(CONFIG_IPVTAP)
-extern void (*__ipvtap_dellink_ptr)(struct net_device *dev,
+extern void (*__ipvtap_dellink_ptr)(struct net *net, struct net_device *dev,
 				    struct list_head *head);
 #endif
 
drivers/net/ipvlan/ipvlan_main.c +14 -8
index 6d7479a8a9c6..ee46a55f73d1 100644
@@ -8,7 +8,7 @@
 #include "ipvlan.h"
 
 #if IS_ENABLED(CONFIG_IPVTAP)
-void (*__ipvtap_dellink_ptr)(struct net_device *dev,
+void (*__ipvtap_dellink_ptr)(struct net *net, struct net_device *dev,
 			     struct list_head *head);
 EXPORT_SYMBOL(__ipvtap_dellink_ptr);
 #endif
@@ -706,7 +706,8 @@ int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params,
 }
 EXPORT_SYMBOL_GPL(ipvlan_link_new);
 
-void __ipvlan_link_delete(struct net_device *dev, struct list_head *head)
+void __ipvlan_link_delete(struct net *net, struct net_device *dev,
+			  struct list_head *head)
 {
 	struct ipvl_dev *ipvlan = netdev_priv(dev);
 	struct ipvl_addr *addr, *next;
@@ -721,7 +722,7 @@ void __ipvlan_link_delete(struct net_device *dev, struct list_head *head)
 
 	ida_free(&ipvlan->port->ida, dev->dev_id);
 	list_del_rcu(&ipvlan->pnode);
-	unregister_netdevice_queue(dev, head);
+	unregister_netdevice_queue_net(net, dev, head);
 	netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
 }
 EXPORT_SYMBOL(__ipvlan_link_delete);
@@ -731,7 +732,8 @@ static void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
 	struct ipvl_dev *ipvlan = netdev_priv(dev);
 
 	mutex_lock(&ipvlan->port->pnodes_lock);
-	__ipvlan_link_delete(dev, head);
+	if (!ipvlan->dying)
+		__ipvlan_link_delete(dev_net(dev), dev, head);
 	mutex_unlock(&ipvlan->port->pnodes_lock);
 }
 
@@ -827,22 +829,26 @@ static int ipvlan_device_event(struct notifier_block *unused,
 			ipvlan_migrate_l3s_hook(oldnet, newnet);
 		break;
 	}
-	case NETDEV_UNREGISTER:
+	case NETDEV_UNREGISTER: {
+		struct net *net = dev_net(dev);
+
 		if (dev->reg_state != NETREG_UNREGISTERING)
 			break;
 
 		list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode) {
+			ipvlan->dying = true;
+
 #if IS_ENABLED(CONFIG_IPVTAP)
 			if (ipvlan->dev->rtnl_link_ops != &ipvlan_link_ops)
-				__ipvtap_dellink_ptr(ipvlan->dev, &lst_kill);
+				__ipvtap_dellink_ptr(net, ipvlan->dev, &lst_kill);
 			else
 #endif
-				__ipvlan_link_delete(ipvlan->dev, &lst_kill);
+				__ipvlan_link_delete(net, ipvlan->dev, &lst_kill);
 		}
 
 		unregister_netdevice_many(&lst_kill);
 		break;
-
+	}
 	case NETDEV_FEAT_CHANGE:
 		list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
 			netif_inherit_tso_max(ipvlan->dev, dev);
drivers/net/ipvlan/ipvtap.c +5 -3
index 99eaa29057b4..66c949d94261 100644
@@ -109,13 +109,14 @@ static int ipvtap_newlink(struct net_device *dev,
 	return err;
 }
 
-static void __ipvtap_dellink(struct net_device *dev, struct list_head *head)
+static void __ipvtap_dellink(struct net *net, struct net_device *dev,
+			     struct list_head *head)
 {
 	struct ipvtap_dev *vlantap = netdev_priv(dev);
 
 	netdev_rx_handler_unregister(dev);
 	tap_del_queues(&vlantap->tap);
-	__ipvlan_link_delete(dev, head);
+	__ipvlan_link_delete(net, dev, head);
 }
 
 static void ipvtap_dellink(struct net_device *dev,
@@ -125,7 +126,8 @@ static void ipvtap_dellink(struct net_device *dev,
 	struct ipvl_port *port = vlantap->vlan.port;
 
 	mutex_lock(&port->pnodes_lock);
-	__ipvtap_dellink(dev, head);
+	if (!vlantap->vlan.dying)
+		__ipvtap_dellink(dev_net(dev), dev, head);
 	mutex_unlock(&port->pnodes_lock);
 }
 
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#25 lore raw
In reply to Kuniyuki Iwashima
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 8 lines · On Fri, 3 Jul 2026 00:09:11 +0000 you wrote:

The biggest blocker to per-netns RTNL is netdev unregistration.

It starts within a single netns, but it can eventually involve multiple namespaces.

There are three types of such cross-netns devices:

[...]

Here is the summary with links:
- [v2,net-next,01/14] rtnetlink: Lock sock_net(skb->sk) in rtnl_newlink().

    https://git.kernel.org/netdev/net-next/c/5ecbbb179e0c
  - [v2,net-next,02/14] rtnetlink: Call unregister_netdevice_many() only once in rtnl_link_unregister().
    https://git.kernel.org/netdev/net-next/c/49c26d4bf1da
  - [v2,net-next,03/14] rtnetlink: Add per-netns rtnl_work.
    https://git.kernel.org/netdev/net-next/c/c6cfaf97837e
  - [v2,net-next,04/14] net: Wrap default_device_exit_net() with __rtnl_net_lock().
    https://git.kernel.org/netdev/net-next/c/2b12ec257849
  - [v2,net-next,05/14] net: Hold __rtnl_net_lock() in netdev_wait_allrefs_any().
    https://git.kernel.org/netdev/net-next/c/0fa296dd520e
  - [v2,net-next,06/14] net: Add per-netns netdev unregistration infra.
    https://git.kernel.org/netdev/net-next/c/af3634d4ac65
  - [v2,net-next,07/14] net: Call unregister_netdevice_many() per netns.
    https://git.kernel.org/netdev/net-next/c/d0008553a70a
  - [v2,net-next,08/14] veth: Support per-netns device unregistration.
    https://git.kernel.org/netdev/net-next/c/d7fda2c776b2
  - [v2,net-next,09/14] bareudp: Protect bareudp_list with mutex.
    https://git.kernel.org/netdev/net-next/c/a278ea7ba32a
  - [v2,net-next,10/14] bareudp: Support per-netns netdev unregistration.
    https://git.kernel.org/netdev/net-next/c/f1de92507a91
  - [v2,net-next,11/14] ipvlan: Convert ipvl_port.count to refcount_t.
    https://git.kernel.org/netdev/net-next/c/acb351b5a899
  - [v2,net-next,12/14] ipvlan: Synchronise ipvlan_init() and ipvlan_uninit() for the same lower dev.
    https://git.kernel.org/netdev/net-next/c/aabbdb8c76d7
  - [v2,net-next,13/14] ipvlan: Protect ipvl_port.ipvlans with mutex.
    https://git.kernel.org/netdev/net-next/c/35add1093e2f
  - [v2,net-next,14/14] ipvlan: Support per-netns netdev unregistration.
    https://git.kernel.org/netdev/net-next/c/00a40d809207

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html