Skip to contentSkip to conversation

net: let lockdep compare instance locks

PATCH Superseded Jakub Kicinski 3 participants 8 messages May 16, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 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().

---
 include/net/netdev_lock.h | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
include/net/netdev_lock.h +21 -8
index 2a753813f849..75a2da23100d 100644
@@ -99,16 +99,29 @@ 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).
-	 */
+#ifdef CONFIG_DEBUG_NET_SMALL_RTNL
+	const struct net_device *dev_a, *dev_b;
+
+	dev_a = container_of(a, struct net_device, lock.dep_map);
+	dev_b = container_of(b, struct net_device, lock.dep_map);
+#endif
+
 	if (a == b)
 		return 0;
-	return -1;
+
+	/* Locking multiple devices usually happens under rtnl_lock */
+	if (lockdep_rtnl_is_held())
+		return -1;
+
+#ifdef CONFIG_DEBUG_NET_SMALL_RTNL
+	/* It's okay to use per-netns rtnl_lock if devices share netns */
+	if (net_eq(dev_net(dev_a), dev_net(dev_b)) &&
+	    lockdep_rtnl_net_is_held(dev_net(dev_a)))
+		return -1;
+#endif
+
+	/* Otherwise taking two instance locks is not allowed */
+	return 1;
 }
 
 #define netdev_lockdep_set_classes(dev)				\
Signature · 1 line
2.49.0
#1 lore raw

From: Jakub Kicinski <[email protected]>
Date: Thu, 15 May 2025 18:24:59 -0700

Quoted 37 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().

---
 include/net/netdev_lock.h | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
include/net/netdev_lock.h +7 -7
index 2a753813f849..75a2da23100d 100644
@@ -99,16 +99,29 @@ 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).
-	 */
+#ifdef CONFIG_DEBUG_NET_SMALL_RTNL
+	const struct net_device *dev_a, *dev_b;
+
+	dev_a = container_of(a, struct net_device, lock.dep_map);
+	dev_b = container_of(b, struct net_device, lock.dep_map);
+#endif
+
 	if (a == b)
 		return 0;

include/net/netdev_lock.h static inline void netdev_unlock_ops_compat(struct net_device *dev) · line 119

Quoted 10 lines
include/net/netdev_lock.h +9 -1
-	return -1;
+
+	/* Locking multiple devices usually happens under rtnl_lock */
+	if (lockdep_rtnl_is_held())
+		return -1;
+
+#ifdef CONFIG_DEBUG_NET_SMALL_RTNL
+	/* It's okay to use per-netns rtnl_lock if devices share netns */
+	if (net_eq(dev_net(dev_a), dev_net(dev_b)) &&
+	    lockdep_rtnl_net_is_held(dev_net(dev_a)))

Do we need

!from_cleanup_net()

before lockdep_rtnl_net_is_held() ?

__rtnl_net_lock() is not held in ops_exit_rtnl_list() and default_device_exit_batch() when calling unregister_netdevice_many().

Quoted 11 lines
include/net/netdev_lock.h +5 -0
+		return -1;
+#endif
+
+	/* Otherwise taking two instance locks is not allowed */
+	return 1;
 }
 
 #define netdev_lockdep_set_classes(dev)				\
Signature · 1 line
2.49.0
author maintainer
#2 lore raw
Quoted 7 lines · On Thu, 15 May 2025 18:49:07 -0700 Kuniyuki Iwashima wrote:

include/net/netdev_lock.h static inline void netdev_unlock_ops_compat(struct net_device *dev) · line 119

Quoted 4 lines
include/net/netdev_lock.h +4 -0
+#ifdef CONFIG_DEBUG_NET_SMALL_RTNL
+	/* It's okay to use per-netns rtnl_lock if devices share netns */
+	if (net_eq(dev_net(dev_a), dev_net(dev_b)) &&
+	    lockdep_rtnl_net_is_held(dev_net(dev_a)))  

Do we need

Quoted 6 lines

!from_cleanup_net()

before lockdep_rtnl_net_is_held() ?

__rtnl_net_lock() is not held in ops_exit_rtnl_list() and default_device_exit_batch() when calling unregister_netdevice_many().

Or do we need

if (from_cleanup_net())
return -1;

?
Is the thinking that once the big rtnl lock disappears in cleanup_net the devices are safe to destroy without any locking because there can't be any live users trying to access them?

#3 lore raw

From: Jakub Kicinski <[email protected]>
Date: Thu, 15 May 2025 19:36:09 -0700

Quoted 15 lines · On Thu, 15 May 2025 18:49:07 -0700 Kuniyuki Iwashima wrote:
Quoted 7 lines

include/net/netdev_lock.h static inline void netdev_unlock_ops_compat(struct net_device *dev) · line 119

Quoted 4 lines
include/net/netdev_lock.h +4 -0
+#ifdef CONFIG_DEBUG_NET_SMALL_RTNL
+	/* It's okay to use per-netns rtnl_lock if devices share netns */
+	if (net_eq(dev_net(dev_a), dev_net(dev_b)) &&
+	    lockdep_rtnl_net_is_held(dev_net(dev_a)))  

Do we need

Quoted 6 lines

!from_cleanup_net()

before lockdep_rtnl_net_is_held() ?

__rtnl_net_lock() is not held in ops_exit_rtnl_list() and default_device_exit_batch() when calling unregister_netdevice_many().

Quoted 6 lines

Or do we need

if (from_cleanup_net())
return -1;

?

Ah right, otherwise we'll return 1 for cleanup_net() :)

Quoted 3 lines

Is the thinking that once the big rtnl lock disappears in cleanup_net the devices are safe to destroy without any locking because there can't be any live users trying to access them?

I hope yes, but removing VF via sysfs and removing netns might race and need some locking ?

author maintainer
#4 lore raw
Quoted 6 lines · On Thu, 15 May 2025 19:59:41 -0700 Kuniyuki Iwashima wrote:
Quoted 3 lines

Is the thinking that once the big rtnl lock disappears in cleanup_net the devices are safe to destroy without any locking because there can't be any live users trying to access them?

I hope yes, but removing VF via sysfs and removing netns might race and need some locking ?

I think we should take the small lock around default_device_exit_net() and then we'd be safe? Either a given VF gets moved to init_net first or the sysfs gets to it and unregisters it safely in the old netns.

author maintainer
#5 lore raw
Quoted 11 lines · On Fri, 16 May 2025 08:22:43 -0700 Jakub Kicinski wrote:
Quoted 6 lines · On Thu, 15 May 2025 19:59:41 -0700 Kuniyuki Iwashima wrote:
Quoted 3 lines

Is the thinking that once the big rtnl lock disappears in cleanup_net the devices are safe to destroy without any locking because there can't be any live users trying to access them?

I hope yes, but removing VF via sysfs and removing netns might race and need some locking ?

I think we should take the small lock around default_device_exit_net() and then we'd be safe? Either a given VF gets moved to init_net first or the sysfs gets to it and unregisters it safely in the old netns.

Thinking about it some more, we'll have to revisit this problem before removing the big lock, anyway. I'm leaning towards doing this for now:

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 during batched device removal for now.
+	 */
+	return lockdep_rtnl_is_held() ? -1 : 1;
 }
 
 #define netdev_lockdep_set_classes(dev)				\
#6 lore raw

From: Jakub Kicinski <[email protected]>
Date: Fri, 16 May 2025 10:14:41 -0700

Quoted 11 lines · On Fri, 16 May 2025 08:22:43 -0700 Jakub Kicinski wrote:
Quoted 10 lines · On Thu, 15 May 2025 19:59:41 -0700 Kuniyuki Iwashima wrote:
Quoted 6 lines
Quoted 3 lines

Is the thinking that once the big rtnl lock disappears in cleanup_net the devices are safe to destroy without any locking because there can't be any live users trying to access them?

I hope yes, but removing VF via sysfs and removing netns might race and need some locking ?

I think we should take the small lock around default_device_exit_net() and then we'd be safe?

Agree. The 'queuing dev for destruction' part will be only racy.

Quoted 5 lines
Quoted 2 lines

Either a given VF gets moved to init_net first
or the sysfs gets to it and unregisters it safely in the old netns.

Thinking about it some more, we'll have to revisit this problem before removing the big lock, anyway. I'm leaning towards doing this for now:

This looks good to me.

Quoted 29 lines
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 during batched device removal for now.
+	 */
+	return lockdep_rtnl_is_held() ? -1 : 1;
 }
 
 #define netdev_lockdep_set_classes(dev)				\
#7 lore raw
In reply to Jakub Kicinski
Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · On Fri, 16 May 2025 08:22:43 -0700 Jakub Kicinski wrote:
Quoted 6 lines · On Thu, 15 May 2025 19:59:41 -0700 Kuniyuki Iwashima wrote:
Quoted 3 lines

Is the thinking that once the big rtnl lock disappears in cleanup_net the devices are safe to destroy without any locking because there can't be any live users trying to access them?

I hope yes, but removing VF via sysfs and removing netns might race and need some locking ?

Quoted 6 lines
Quoted 3 lines

I think we should take the small lock around default_device_exit_net() and then we'd be safe? Either a given VF gets moved to init_net first or the sysfs gets to it and unregisters it safely in the old netns.

Thinking about it some more, we'll have to revisit this problem before removing the big lock, anyway. I'm leaning towards doing this for now:

+1