[PATCH net] netdevsim: allow reload_down when current netns is exiting
From: Thanh Nguyen
Date: Fri Dec 04 2026 - 09:16:39 EST
When a network namespace containing a netdevsim instance is destroyed,
devlink_pernet_pre_exit() reloads the devlink instance back into
init_net. If debugfs dont_allow_reload is set, nsim_dev_reload_down()
returns -EOPNOTSUPP without tearing down netdevsim's per-netns
notifiers (hwstats netdevice_nb, fib_nb, nexthop_nb), yet
devlink_pernet_pre_exit() still moves devlink_net(devlink) to
init_net.
Subsequently, when nsim_dev_hwstats_exit() runs during device removal
or reload, it calls unregister_netdevice_notifier_net() against
init_net instead of the exiting namespace, silently failing with
-ENOENT and destroying hwstats->hwsdev_list_lock while netdevice_nb
remains linked on the exiting namespace's netdev_chain. Remaining
teardown in cleanup_net() then invokes nsim_dev_netdevice_event() on
the destroyed mutex / freed nsim_dev, triggering
DEBUG_LOCKS_WARN_ON(lock->magic != lock) and a use-after-free.
Do not let dont_allow_reload reject reload_down() when the current
network namespace is already being destroyed (!check_net()), so
nsim_dev_reload_destroy() cleanly unregisters all per-netns notifiers
before devlink transitions the instance to init_net.
Reported-by: syzbot+a104228f8a378b00e6ad@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=a104228f8a378b00e6ad
Fixes: 6b3e73774815 ("devlink: fix reload error on netns teardown")
Signed-off-by: Thanh Nguyen <thanhnguyentuan2007@xxxxxxxxx>
---
drivers/net/netdevsim/dev.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c
index c6b17b387..f016a97e1 100644
--- a/drivers/net/netdevsim/dev.c
+++ b/drivers/net/netdevsim/dev.c
@@ -951,9 +951,10 @@ static int nsim_dev_reload_down(struct devlink *devlink, bool netns_change,
{
struct nsim_dev *nsim_dev = devlink_priv(devlink);
- if (nsim_dev->dont_allow_reload) {
+ if (nsim_dev->dont_allow_reload && check_net(devlink_net(devlink))) {
/* For testing purposes, user set debugfs dont_allow_reload
- * value to true. So forbid it.
+ * value to true. So forbid it, unless the current network
+ * namespace is being destroyed.
*/
NL_SET_ERR_MSG_MOD(extack, "User forbid the reload for testing purposes");
return -EOPNOTSUPP;
--
2.56.1