Skip to contentSkip to conversation

net: let lockdep compare instance locks

PATCH Applied Jakub Kicinski 4 participants 4 messages May 17, 2025 – May 21, 2025

Subsystems (2)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH v2 1/1 net: let lockdep compare instance locks

AFAIU always returning -1 from lockdep's compare function basically disables checking of dependencies between given locks. Try to be a little more precise about what guarantees that instance locks won't deadlock.

Right now we only nest them under protection of rtnl_lock. Mostly in unregister_netdevice_many() and dev_close_many().

---
v2:
- drop the speculative small rtnl handling
v1: https://lore.kernel.org/[email protected]

---
 include/net/netdev_lock.h | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
include/net/netdev_lock.h +7 -8
index 2a753813f849..c345afecd4c5 100644
@@ -99,16 +99,15 @@ static inline void netdev_unlock_ops_compat(struct net_device *dev)
 static inline int netdev_lock_cmp_fn(const struct lockdep_map *a,
 				     const struct lockdep_map *b)
 {
-	/* Only lower devices currently grab the instance lock, so no
-	 * real ordering issues can occur. In the near future, only
-	 * hardware devices will grab instance lock which also does not
-	 * involve any ordering. Suppress lockdep ordering warnings
-	 * until (if) we start grabbing instance lock on pure SW
-	 * devices (bond/team/veth/etc).
-	 */
 	if (a == b)
 		return 0;
-	return -1;
+
+	/* Allow locking multiple devices only under rtnl_lock,
+	 * the exact order doesn't matter.
+	 * Note that upper devices don't lock their ops, so nesting
+	 * mostly happens in batched device removal for now.
+	 */
+	return lockdep_rtnl_is_held() ? -1 : 1;
 }
 
 #define netdev_lockdep_set_classes(dev)				\
Signature · 1 line
2.49.0
Acked-by on v2 1/1
#1 lore raw
Quoted 5 lines · Jakub Kicinski

AFAIU always returning -1 from lockdep's compare function basically disables checking of dependencies between given locks. Try to be a little more precise about what guarantees that instance locks won't deadlock.

Quoted 7 lines

Right now we only nest them under protection of rtnl_lock. Mostly in unregister_netdevice_many() and dev_close_many().

---
v2:
- drop the speculative small rtnl handling

Reviewed-by on v2 1/1
#2 lore raw
In reply to Jakub Kicinski

From: Jakub Kicinski <[email protected]>
Date: Sat, 17 May 2025 13:08:10 -0700

Quoted 9 lines

AFAIU always returning -1 from lockdep's compare function basically disables checking of dependencies between given locks. Try to be a little more precise about what guarantees that instance locks won't deadlock.

Right now we only nest them under protection of rtnl_lock. Mostly in unregister_netdevice_many() and dev_close_many().

Quoted 39 lines

---
v2:
- drop the speculative small rtnl handling
v1: https://lore.kernel.org/[email protected]

---
 include/net/netdev_lock.h | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
include/net/netdev_lock.h +7 -8
index 2a753813f849..c345afecd4c5 100644
@@ -99,16 +99,15 @@ static inline void netdev_unlock_ops_compat(struct net_device *dev)
 static inline int netdev_lock_cmp_fn(const struct lockdep_map *a,
 				     const struct lockdep_map *b)
 {
-	/* Only lower devices currently grab the instance lock, so no
-	 * real ordering issues can occur. In the near future, only
-	 * hardware devices will grab instance lock which also does not
-	 * involve any ordering. Suppress lockdep ordering warnings
-	 * until (if) we start grabbing instance lock on pure SW
-	 * devices (bond/team/veth/etc).
-	 */
 	if (a == b)
 		return 0;
-	return -1;
+
+	/* Allow locking multiple devices only under rtnl_lock,
+	 * the exact order doesn't matter.
+	 * Note that upper devices don't lock their ops, so nesting
+	 * mostly happens in batched device removal for now.
+	 */
+	return lockdep_rtnl_is_held() ? -1 : 1;
 }
 
 #define netdev_lockdep_set_classes(dev)				\
Signature · 1 line
2.49.0
#3 lore raw
In reply to Jakub Kicinski
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Sat, 17 May 2025 13:08:10 -0700 you wrote:

AFAIU always returning -1 from lockdep's compare function basically disables checking of dependencies between given locks. Try to be a little more precise about what guarantees that instance locks won't deadlock.

Right now we only nest them under protection of rtnl_lock. Mostly in unregister_netdevice_many() and dev_close_many().

[...]

Here is the summary with links:
- [net-next,v2] net: let lockdep compare instance locks

    https://git.kernel.org/netdev/net-next/c/4c2bd7913f52

You are awesome, thank you!

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