net: Support per-netns device unregistration
Subsystems (2)
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...
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 | 18 +++- drivers/net/ipvlan/ipvlan_main.c | 153 +++++++++++++++++++++++++------ drivers/net/ipvlan/ipvtap.c | 16 ++-- drivers/net/veth.c | 34 ++++--- include/linux/netdevice.h | 22 +++++ include/linux/rtnetlink.h | 8 ++ include/net/net_namespace.h | 3 + net/core/dev.c | 129 +++++++++++++++++++++++++- net/core/net_namespace.c | 4 + net/core/rtnetlink.c | 57 ++++++++++-- 11 files changed, 418 insertions(+), 69 deletions(-)
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
PATCH 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
@@ -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
PATCH 2/14 rtnetlink: Call unregister_netdevice_many() only once in rtnl_link_unregister().
In reply to Kuniyuki IwashimaWhen 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
@@ -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
PATCH 3/14 rtnetlink: Add per-netns rtnl_work.
In reply to Kuniyuki IwashimaThe 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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 4/14 net: Wrap default_device_exit_net() with __rtnl_net_lock().
In reply to Kuniyuki Iwashimadefault_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
@@ -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
PATCH 5/14 net: Hold __rtnl_net_lock() in netdev_wait_allrefs_any().
In reply to Kuniyuki IwashimaCurrently, 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
@@ -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
PATCH 6/14 net: Add per-netns netdev unregistration infra.
In reply to Kuniyuki IwashimaWhen 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.
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.)
--- include/linux/netdevice.h | 16 +++++++ include/net/net_namespace.h | 2 + net/core/dev.c | 85 +++++++++++++++++++++++++++++++++++++ net/core/net_namespace.c | 2 + net/core/rtnetlink.c | 4 ++ 5 files changed, 109 insertions(+)
include/linux/netdevice.h +16 -0
@@ -2241,6 +2241,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 +3475,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
@@ -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 +85 -0
115 lines not on this page · raw message
net/core/net_namespace.c +2 -0
@@ -423,6 +423,8 @@ static __net_init int preinit_net(struct net *net, struct user_namespace *user_nmutex_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
@@ -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
PATCH 7/14 net: Call unregister_netdevice_many() per netns.
In reply to Kuniyuki IwashimaFor 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
@@ -3479,6 +3479,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,
@@ -3486,6 +3487,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
@@ -12546,6 +12546,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)
@@ -13179,12 +13201,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
@@ -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
@@ -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
PATCH 8/14 veth: Support per-netns device unregistration.
In reply to Kuniyuki IwashimaCurrently, 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
@@ -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
PATCH 9/14 bareudp: Protect bareudp_list with mutex.
In reply to Kuniyuki Iwashimastruct 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 gn->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
@@ -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
PATCH 10/14 bareudp: Support per-netns netdev unregistration.
In reply to Kuniyuki Iwashimabareudp_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 ipv42. 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(&gn->sock_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
@@ -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
PATCH 11/14 ipvlan: Convert ipvl_port.count to refcount_t.
In reply to Kuniyuki Iwashimastruct 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
@@ -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
PATCH 12/14 ipvlan: Synchronise ipvlan_init() and ipvlan_uninit() for the same lower dev.
In reply to Kuniyuki Iwashimaipvlan_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
@@ -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
PATCH 13/14 ipvlan: Protect ipvl_port.ipvlans with mutex.
In reply to Kuniyuki Iwashimastruct 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() and struct ipvtap_dev is moved to ipvlan.c/h for CONFIG_IPVLAN=y but CONFIG_IPVTAP=m.
--- drivers/net/ipvlan/ipvlan.h | 14 ++++++++- drivers/net/ipvlan/ipvlan_main.c | 54 +++++++++++++++++++++++++++++--- drivers/net/ipvlan/ipvtap.c | 15 +++------ 3 files changed, 67 insertions(+), 16 deletions(-)
drivers/net/ipvlan/ipvlan.h +13 -1
@@ -16,6 +16,9 @@#include <linux/if_arp.h> #include <linux/if_link.h> #include <linux/if_vlan.h> +#if IS_ENABLED (CONFIG_IPVTAP ) +#include <linux/if_tap.h> +#endif #include <linux/ip.h> #include <linux/inetdevice.h> #include <linux/netfilter.h>
@@ -91,6 +94,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 +172,6 @@ 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_setup (struct net_device *dev);int ipvlan_link_register (struct rtnl_link_ops *ops);#ifdef CONFIG_IPVLAN_L3S
@@ -207,4 +210,13 @@ 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 ) +struct ipvtap_dev { +struct ipvl_dev vlan; +struct tap_dev tap; +}; + +void __ipvtap_dellink (struct net_device *dev,struct list_head *head); +#endif +#endif /* __IPVLAN_H */
drivers/net/ipvlan/ipvlan_main.c +49 -5
119 lines not on this page · raw message
drivers/net/ipvlan/ipvtap.c +5 -10
@@ -2,7 +2,6 @@#include <linux/etherdevice.h> #include "ipvlan.h" #include <linux/if_vlan.h> -#include <linux/if_tap.h> #include <linux/interrupt.h> #include <linux/nsproxy.h> #include <linux/compat.h>
@@ -43,11 +42,6 @@ static struct class ipvtap_class = { .namespace = ipvtap_net_namespace, }; -struct ipvtap_dev { -struct ipvl_dev vlan; -struct tap_dev tap; -}; -static void ipvtap_count_tx_dropped (struct tap_dev *tap) { struct ipvtap_dev *vlantap = container_of(tap, struct ipvtap_dev, tap);
@@ -112,11 +106,12 @@ static int ipvtap_newlink(struct net_device *dev,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)
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
PATCH 14/14 ipvlan: Support per-netns netdev unregistration.
In reply to Kuniyuki IwashimaWhen 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 | 4 +++- drivers/net/ipvlan/ipvlan_main.c | 25 ++++++++++++++++--------- drivers/net/ipvlan/ipvtap.c | 3 ++- 3 files changed, 21 insertions(+), 11 deletions(-)
drivers/net/ipvlan/ipvlan.h +3 -1
@@ -72,6 +72,7 @@ struct ipvl_dev {DECLARE_BITMAP (mac_filters,IPVLAN_MAC_FILTER_SIZE );netdev_features_t sfeatures;u32 msg_enable; +bool dying; };struct ipvl_addr {
@@ -216,7 +217,8 @@ struct ipvtap_dev {struct tap_dev tap; }; -void __ipvtap_dellink (struct net_device *dev,struct list_head *head); +void __ipvtap_dellink (struct net *net,struct net_device *dev, +struct list_head *head);#endif #endif /* __IPVLAN_H */
drivers/net/ipvlan/ipvlan_main.c +16 -9
@@ -700,7 +700,8 @@ int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params, }EXPORT_SYMBOL_GPL (ipvlan_link_new); -static void __ipvlan_link_delete (struct net_device *dev,struct list_head *head) +static 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;
@@ -715,7 +716,7 @@ static 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); }
@@ -724,18 +725,20 @@ 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); }#if IS_ENABLED (CONFIG_IPVTAP ) -void __ipvtap_dellink (struct net_device *dev,struct list_head *head) +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); }EXPORT_SYMBOL_GPL (__ipvtap_dellink);#endif
@@ -832,22 +835,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 (ipvlan->dev, &lst_kill); +__ipvtap_dellink (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 +2 -1
@@ -110,7 +110,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
syzbot ci has tested the following series
syzbot ci has tested the following series
[v1] net: Support per-netns device unregistration
https://lore.kernel.org/all/[email protected]
* [PATCH v1 net-next 01/14] rtnetlink: Lock sock_net(skb->sk) in rtnl_newlink().
* [PATCH v1 net-next 02/14] rtnetlink: Call unregister_netdevice_many() only once in rtnl_link_unregister().
* [PATCH v1 net-next 03/14] rtnetlink: Add per-netns rtnl_work.
* [PATCH v1 net-next 04/14] net: Wrap default_device_exit_net() with __rtnl_net_lock().
* [PATCH v1 net-next 05/14] net: Hold __rtnl_net_lock() in netdev_wait_allrefs_any().
* [PATCH v1 net-next 06/14] net: Add per-netns netdev unregistration infra.
* [PATCH v1 net-next 07/14] net: Call unregister_netdevice_many() per netns.
* [PATCH v1 net-next 08/14] veth: Support per-netns device unregistration.
* [PATCH v1 net-next 09/14] bareudp: Protect bareudp_list with mutex.
* [PATCH v1 net-next 10/14] bareudp: Support per-netns netdev unregistration.
* [PATCH v1 net-next 11/14] ipvlan: Convert ipvl_port.count to refcount_t.
* [PATCH v1 net-next 12/14] ipvlan: Synchronise ipvlan_init() and ipvlan_uninit() for the same lower dev.
* [PATCH v1 net-next 13/14] ipvlan: Protect ipvl_port.ipvlans with mutex.
* [PATCH v1 net-next 14/14] ipvlan: Support per-netns netdev unregistration.
and found the following issue:
possible deadlock in __dev_change_net_namespace
Full report is available here:
https://ci.syzbot.org/series/a744b257-d741-4780-8a53-f156b2a7afc9
***
possible deadlock in __dev_change_net_namespace
tree: net-next URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/netdev/net-next.git base: d6e81529749190123aa0040626c7e5dbc20fdc9a arch: amd64 compiler: Debian clang version 22.1.6 (++20260514074242+fc4aad7b5db3-1~exp1~20260514074407.73), Debian LLD 22.1.6 config: https://ci.syzbot.org/builds/243cd0ec-28f9-4d21-8f16-3d2fbad8388d/config syz repro: https://ci.syzbot.org/findings/a8a0740d-fdec-4a20-9aa5-7cb955707913/syz_repro
veth0_macvtap: left promiscuous mode
============================================
WARNING: possible recursive locking detected
syzkaller #0 Not tainted
--------------------------------------------
syz.1.18/5814 is trying to acquire lock:ffffffff9a9b0418 (&net->dev_unreg_lock){+.+.}-{3:3}, at: spin_lock include/linux/spinlock.h:342 [inline] ffffffff9a9b0418 (&net->dev_unreg_lock){+.+.}-{3:3}, at: unregister_netdevice_move_net net/core/dev.c:-1 [inline] ffffffff9a9b0418 (&net->dev_unreg_lock){+.+.}-{3:3}, at: __dev_change_net_namespace+0x1479/0x2200 net/core/dev.c:12768
but task is already holding lock:ffff88810b6cf8d8 (&net->dev_unreg_lock){+.+.}-{3:3}, at: spin_lock include/linux/spinlock.h:342 [inline] ffff88810b6cf8d8 (&net->dev_unreg_lock){+.+.}-{3:3}, at: unregister_netdevice_move_net net/core/dev.c:-1 [inline] ffff88810b6cf8d8 (&net->dev_unreg_lock){+.+.}-{3:3}, at: __dev_change_net_namespace+0x146a/0x2200 net/core/dev.c:12768
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0
---- lock(&net->dev_unreg_lock);
lock(&net->dev_unreg_lock);
*** DEADLOCK ***
May be due to missing lock nesting notation
4 locks held by syz.1.18/5814:
#0: ffffffff8fdeb0c0 (rtnl_mutex){+.+.}-{4:4}, at: rtnl_lock net/core/rtnetlink.c:80 [inline]
#0: ffffffff8fdeb0c0 (rtnl_mutex){+.+.}-{4:4}, at: rtnl_nets_lock+0x2a/0x2b0 net/core/rtnetlink.c:366 #1: ffffffff9a9b0380 (&net->rtnl_mutex){+.+.}-{4:4}, at: rtnl_nets_lock+0x76/0x2b0 net/core/rtnetlink.c:369 #2: ffff88810b6cf840 (&net->rtnl_mutex){+.+.}-{4:4}, at: rtnl_nets_lock+0xbf/0x2b0 net/core/rtnetlink.c:369 #3: ffff88810b6cf8d8 (&net->dev_unreg_lock){+.+.}-{3:3}, at: spin_lock include/linux/spinlock.h:342 [inline] #3: ffff88810b6cf8d8 (&net->dev_unreg_lock){+.+.}-{3:3}, at: unregister_netdevice_move_net net/core/dev.c:-1 [inline] #3: ffff88810b6cf8d8 (&net->dev_unreg_lock){+.+.}-{3:3}, at: __dev_change_net_namespace+0x146a/0x2200 net/core/dev.c:12768
stack backtrace: CPU: 0 UID: 0 PID: 5814 Comm: syz.1.18 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 print_deadlock_bug+0x279/0x290 kernel/locking/lockdep.c:3041 check_deadlock kernel/locking/lockdep.c:3093 [inline] validate_chain kernel/locking/lockdep.c:3895 [inline] __lock_acquire+0x24df/0x2cf0 kernel/locking/lockdep.c:5237 lock_acquire+0x106/0x350 kernel/locking/lockdep.c:5868 __raw_spin_lock include/linux/spinlock_api_smp.h:158 [inline] _raw_spin_lock+0x2e/0x40 kernel/locking/spinlock.c:158 spin_lock include/linux/spinlock.h:342 [inline] unregister_netdevice_move_net net/core/dev.c:-1 [inline] __dev_change_net_namespace+0x1479/0x2200 net/core/dev.c:12768 do_setlink+0x2d1/0x4670 net/core/rtnetlink.c:3148 rtnl_changelink net/core/rtnetlink.c:3880 [inline] __rtnl_newlink net/core/rtnetlink.c:4053 [inline] rtnl_newlink+0x1612/0x1b50 net/core/rtnetlink.c:4192 rtnetlink_rcv_msg+0x802/0xc00 net/core/rtnetlink.c:7109 netlink_rcv_skb+0x226/0x4a0 net/netlink/af_netlink.c:2556 netlink_unicast_kernel net/netlink/af_netlink.c:1319 [inline] netlink_unicast+0x7bb/0x940 net/netlink/af_netlink.c:1345 netlink_sendmsg+0x813/0xb40 net/netlink/af_netlink.c:1900 sock_sendmsg_nosec+0x13a/0x180 net/socket.c:775 __sock_sendmsg net/socket.c:790 [inline] ____sys_sendmsg+0x54e/0x850 net/socket.c:2684 ___sys_sendmsg+0x2a5/0x360 net/socket.c:2738 __sys_sendmsg net/socket.c:2770 [inline] __do_sys_sendmsg net/socket.c:2775 [inline] __se_sys_sendmsg net/socket.c:2773 [inline] __x64_sys_sendmsg+0x1b1/0x290 net/socket.c:2773 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0x174/0x580 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fbdc639ce59 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fbdc59fe028 EFLAGS: 00000246 ORIG_RAX: 000000000000002e RAX: ffffffffffffffda RBX: 00007fbdc6615fa0 RCX: 00007fbdc639ce59 RDX: 0000000000000000 RSI: 0000200000000740 RDI: 0000000000000003 RBP: 00007fbdc6432e6f R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007fbdc6616038 R14: 00007fbdc6615fa0 R15: 00007ffd94dcb1f8 </TASK>
syz.1.18 (5814) used greatest stack depth: 19864 bytes left
***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].
To test a patch for this bug, please reply with #syz test (should be on a separate line).
The patch should be attached to the email.
Note: arguments like custom git repos and branches are not supported.
From: syzbot ci <[email protected]>
Date: Thu, 02 Jul 2026 00:45:10 -0700
Quoted 55 lines
syzbot ci has tested the following series
[v1] net: Support per-netns device unregistration
https://lore.kernel.org/all/[email protected]
* [PATCH v1 net-next 01/14] rtnetlink: Locksock_net(skb->sk)inrtnl_newlink().
* [PATCH v1 net-next 02/14] rtnetlink: Callunregister_netdevice_many()only once inrtnl_link_unregister().
* [PATCH v1 net-next 03/14] rtnetlink: Add per-netnsrtnl_work.
* [PATCH v1 net-next 04/14] net: Wrapdefault_device_exit_net()with__rtnl_net_lock().
* [PATCH v1 net-next 05/14] net: Hold__rtnl_net_lock()innetdev_wait_allrefs_any().
* [PATCH v1 net-next 06/14] net: Add per-netns netdev unregistration infra.
* [PATCH v1 net-next 07/14] net: Callunregister_netdevice_many()per netns.
* [PATCH v1 net-next 08/14] veth: Support per-netns device unregistration.
* [PATCH v1 net-next 09/14] bareudp: Protectbareudp_listwith mutex.
* [PATCH v1 net-next 10/14] bareudp: Support per-netns netdev unregistration.
* [PATCH v1 net-next 11/14] ipvlan: Convert ipvl_port.count torefcount_t.
* [PATCH v1 net-next 12/14] ipvlan: Synchroniseipvlan_init()andipvlan_uninit()for the same lower dev.
* [PATCH v1 net-next 13/14] ipvlan: Protect ipvl_port.ipvlans with mutex.
* [PATCH v1 net-next 14/14] ipvlan: Support per-netns netdev unregistration.and found the following issue:
possible deadlock in__dev_change_net_namespaceFull report is available here:
https://ci.syzbot.org/series/a744b257-d741-4780-8a53-f156b2a7afc9***
possible deadlock in
__dev_change_net_namespacetree: net-next URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/netdev/net-next.git base: d6e81529749190123aa0040626c7e5dbc20fdc9a arch: amd64 compiler: Debian clang version 22.1.6 (++20260514074242+fc4aad7b5db3-1~exp1~20260514074407.73), Debian LLD 22.1.6 config: https://ci.syzbot.org/builds/243cd0ec-28f9-4d21-8f16-3d2fbad8388d/config syz repro: https://ci.syzbot.org/findings/a8a0740d-fdec-4a20-9aa5-7cb955707913/syz_repro
veth0_macvtap: left promiscuous mode
============================================
WARNING: possible recursive locking detected
syzkaller #0 Not tainted
--------------------------------------------
syz.1.18/5814 is trying to acquire lock:ffffffff9a9b0418(&net->dev_unreg_lock){+.+.}-{3:3}, at:spin_lockinclude/linux/spinlock.h:342[inline]ffffffff9a9b0418(&net->dev_unreg_lock){+.+.}-{3:3}, at:unregister_netdevice_move_netnet/core/dev.c:-1 [inline]ffffffff9a9b0418(&net->dev_unreg_lock){+.+.}-{3:3}, at:__dev_change_net_namespace+0x1479/0x2200net/core/dev.c:12768but task is already holding lock:
ffff88810b6cf8d8(&net->dev_unreg_lock){+.+.}-{3:3}, at:spin_lockinclude/linux/spinlock.h:342[inline]ffff88810b6cf8d8(&net->dev_unreg_lock){+.+.}-{3:3}, at:unregister_netdevice_move_netnet/core/dev.c:-1 [inline]ffff88810b6cf8d8(&net->dev_unreg_lock){+.+.}-{3:3}, at:__dev_change_net_namespace+0x146a/0x2200net/core/dev.c:12768other info that might help us debug this:
Possible unsafe locking scenario:
Quoted 8 lines
CPU0 ----
lock(&net->dev_unreg_lock);
lock(&net->dev_unreg_lock);*** DEADLOCK ***
May be due to missing lock nesting notation
Oh right, I'll squash this to patch 6.
---8<---
net/core/dev.c +2 -2
@@ -12574,10 +12574,10 @@ static void unregister_netdevice_move_net(struct net *net_old, {if (net_old > net) {spin_lock (&net->dev_unreg_lock); -spin_lock (&net_old->dev_unreg_lock); +spin_lock_nested (&net_old->dev_unreg_lock,SINGLE_DEPTH_NESTING ); }else {spin_lock (&net_old->dev_unreg_lock); -spin_lock (&net->dev_unreg_lock); +spin_lock_nested (&net->dev_unreg_lock,SINGLE_DEPTH_NESTING ); }if (!list_empty (&dev->unreg_list_net)) {
---8<---