All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
	andrew+netdev@lunn.ch, horms@kernel.org, sdf@fomichev.me,
	kuniyu@amazon.com, Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net-next] net: let lockdep compare instance locks
Date: Thu, 15 May 2025 18:24:59 -0700	[thread overview]
Message-ID: <20250516012459.1385997-1-kuba@kernel.org> (raw)

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

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 include/net/netdev_lock.h | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/include/net/netdev_lock.h b/include/net/netdev_lock.h
index 2a753813f849..75a2da23100d 100644
--- a/include/net/netdev_lock.h
+++ b/include/net/netdev_lock.h
@@ -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)				\
-- 
2.49.0


             reply	other threads:[~2025-05-16  1:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-16  1:24 Jakub Kicinski [this message]
2025-05-16  1:49 ` [PATCH net-next] net: let lockdep compare instance locks Kuniyuki Iwashima
2025-05-16  2:36   ` Jakub Kicinski
2025-05-16  2:59     ` Kuniyuki Iwashima
2025-05-16 15:22       ` Jakub Kicinski
2025-05-16 17:14         ` Jakub Kicinski
2025-05-16 17:50           ` Kuniyuki Iwashima
2025-05-16 17:50           ` Stanislav Fomichev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250516012459.1385997-1-kuba@kernel.org \
    --to=kuba@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuniyu@amazon.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.