net: don't mix device locking in dev_close_many() calls
Subsystems (2)
Jakub Kicinski
maintainerPATCH 1/1 net: don't mix device locking in dev_close_many() calls
Lockdep found the following dependency:
&dev_instance_lock_key#3 -->
&rdev->wiphy.mtx -->
&net->xdp.lock -->
&xs->mutex -->
&dev_instance_lock_key#3The first dependency is the problem. wiphy mutex should be outside the instance locks. The problem happens in notifiers (as always) for CLOSE. We only hold the instance lock for ops locked devices during CLOSE, and WiFi netdevs are not ops locked. Unfortunately, when we dev_close_many() during netns dismantle we may be holding the instance lock of _another_ netdev when issuing a CLOSE for a WiFi device.
Lockdep's "Possible unsafe locking scenario" only prints 3 locks and we have 4, plus I think we'd need 3 CPUs, like this:
CPU0 CPU1 CPU2
---- ---- ----
lock(&xs->mutex);
lock(&dev_instance_lock_key#3);
lock(&rdev->wiphy.mtx);
lock(&net->xdp.lock);
lock(&xs->mutex);
lock(&rdev->wiphy.mtx); lock(&dev_instance_lock_key#3);
Tho, I don't think that's possible as CPU1 and CPU2 would be under rtnl_lock. Even if we have per-netns rtnl_lock and wiphy can span network namespaces - CPU0 and CPU1 must be in the same netns to see dev_instance_lock, so CPU0 can't be installing a socket as CPU1 is tearing the netns down.
Regardless, our expected lock ordering is that wiphy lock is taken before instance locks, so let's fix this.
Go over the ops locked and non-locked devices separately. Note that calling dev_close_many() on an empty list is perfectly fine. All processing (including RCU syncs) are conditional on the list not being empty, already.
---
CC: [email protected]
--- net/core/dev.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
net/core/dev.c +13 -4
@@ -11932,15 +11932,24 @@ void unregister_netdevice_many_notify(struct list_head *head,BUG_ON (dev->reg_state !=NETREG_REGISTERED ); } -/* If device is running, close it first. */ +/* If device is running, close it first. Start with ops locked... */ list_for_each_entry (dev, head, unreg_list) { -list_add_tail (&dev->close_list, &close_head); -netdev_lock_ops (dev); +if (netdev_need_ops_lock (dev)) { +list_add_tail (&dev->close_list, &close_head); +netdev_lock (dev); + } + } +dev_close_many (&close_head, true); +/* ... now unlock them and go over the rest. */ +list_for_each_entry (dev, head, unreg_list) { +if (netdev_need_ops_lock (dev)) +netdev_unlock (dev); +else +list_add_tail (&dev->close_list, &close_head); }dev_close_many (&close_head, true);list_for_each_entry (dev, head, unreg_list) { -netdev_unlock_ops (dev);/* And unlink it from device chain. */ unlist_netdevice (dev);netdev_lock (dev);
Signature · 1 line
2.49.0
Quoted 38 lines · Jakub Kicinski
Lockdep found the following dependency:
&dev_instance_lock_key#3 -->
&rdev->wiphy.mtx --> &net->xdp.lock --> &xs->mutex --> &dev_instance_lock_key#3The first dependency is the problem. wiphy mutex should be outside the instance locks. The problem happens in notifiers (as always) for CLOSE. We only hold the instance lock for ops locked devices during CLOSE, and WiFi netdevs are not ops locked. Unfortunately, when we
dev_close_many()during netns dismantle we may be holding the instance lock of _another_ netdev when issuing a CLOSE for a WiFi device.Lockdep's "Possible unsafe locking scenario" only prints 3 locks and we have 4, plus I think we'd need 3 CPUs, like this:
CPU0 CPU1 CPU2 ---- ---- ---- lock(&xs->mutex); lock(&dev_instance_lock_key#3); lock(&rdev->wiphy.mtx); lock(&net->xdp.lock); lock(&xs->mutex); lock(&rdev->wiphy.mtx);
lock(&dev_instance_lock_key#3);Tho, I don't think that's possible as CPU1 and CPU2 would be under
rtnl_lock. Even if we have per-netnsrtnl_lockand wiphy can span network namespaces - CPU0 and CPU1 must be in the same netns to seedev_instance_lock, so CPU0 can't be installing a socket as CPU1 is tearing the netns down.Regardless, our expected lock ordering is that wiphy lock is taken before instance locks, so let's fix this.
Quoted 8 lines
Go over the ops locked and non-locked devices separately. Note that calling
dev_close_many()on an empty list is perfectly fine. All processing (including RCU syncs) are conditional on the list not being empty, already.Fixes 7e4d784f5810 ("net: hold netdev instance lock during rtnetlink operations")Reported-by [email protected]Signed-off-by Jakub Kicinski <[email protected]>
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Sat, 12 Apr 2025 16:30:11 -0700 you wrote:
Lockdep found the following dependency:
&dev_instance_lock_key#3 -->
&rdev->wiphy.mtx --> &net->xdp.lock --> &xs->mutex --> &dev_instance_lock_key#3[...]
Here is the summary with links:
- [net] net: don't mix device locking in dev_close_many() calls
https://git.kernel.org/netdev/net/c/f0433eea4688
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html