Linux Documentation
 help / color / mirror / Atom feed
From: Cosmin Ratiu <cratiu@nvidia.com>
To: <netdev@vger.kernel.org>
Cc: Jiri Pirko <jiri@resnulli.us>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	"Simon Horman" <horms@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	"Tariq Toukan" <tariqt@nvidia.com>,
	Cosmin Ratiu <cratiu@nvidia.com>,
	Carolina Jubran <cjubran@nvidia.com>,
	Dmytro Linkin <dlinkin@nvidia.com>,
	Vlad Buslov <vladbu@nvidia.com>, <linux-doc@vger.kernel.org>
Subject: [PATCH net 2/2] devlink: Restore parent -> nested instance locking order
Date: Wed, 2 Dec 2026 15:38:38 +0200	[thread overview]
Message-ID: <20261202133838.834971-3-cratiu@nvidia.com> (raw)
In-Reply-To: <20261202133838.834971-1-cratiu@nvidia.com>

Commit 0061b5199d7c ("devlink: Reverse locking order for nested
instances") and commit 3bf4c5970ca8 ("devlink: Update nested instance
locking comment") documented that a nested instance lock must be taken
before its nested-in (parent) instance lock, in preparation for
cross-device rates walking up the nested devlink chain.

The previous commit removed that walk. Devlink core now only takes the
shared instance lock while holding a nested instance lock, never the
lock of an intermediate parent. Drivers, on the other hand, take the
parent lock first, e.g. mlx5 holds the PF devlink lock while tearing
down its SFs.

Restore the original parent -> nested order in the documentation and in
the devlink_rel_nested_in_notify() comment, and document the shared
instance lock as the exception, always taken last. Also update
devlink-shared.rst, which described the shared instance lock order as
following the general nested instance rule.

Fixes: 0061b5199d7c ("devlink: Reverse locking order for nested instances")
Fixes: 3bf4c5970ca8 ("devlink: Update nested instance locking comment")
Signed-off-by: Cosmin Ratiu <cratiu@nvidia.com>
---
 Documentation/networking/devlink/devlink-shared.rst |  7 +++++--
 Documentation/networking/devlink/index.rst          | 12 ++++++------
 net/devlink/core.c                                  |  4 +++-
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/Documentation/networking/devlink/devlink-shared.rst b/Documentation/networking/devlink/devlink-shared.rst
index 16bf6a7d25d9..aa594c06d552 100644
--- a/Documentation/networking/devlink/devlink-shared.rst
+++ b/Documentation/networking/devlink/devlink-shared.rst
@@ -85,8 +85,11 @@ Locking
 
 A global mutex (``shd_mutex``) protects the shared instances list during registration/deregistration.
 
-Similarly to other nested devlink instance relationships, devlink lock of
-the shared instance should be always taken after the devlink lock of PF.
+Unlike other nested devlink instance relationships, devlink lock of the
+shared instance is always taken last, after the devlink lock of the PF or
+of any other instance nested in it. It must not be held while taking
+another instance lock or while waiting for a nested instance to be torn
+down.
 
 Reference Counting
 ------------------
diff --git a/Documentation/networking/devlink/index.rst b/Documentation/networking/devlink/index.rst
index aebfa5c06751..2fc929bf5544 100644
--- a/Documentation/networking/devlink/index.rst
+++ b/Documentation/networking/devlink/index.rst
@@ -31,8 +31,8 @@ sure to respect following rules:
 
  - Lock ordering should be maintained. If driver needs to take instance
    lock of both nested and parent instances at the same time, devlink
-   instance lock of the nested instance should be taken first, only then
-   instance lock of the parent instance could be taken.
+   instance lock of the parent instance should be taken first, only then
+   instance lock of the nested instance could be taken.
  - Driver should use object-specific helpers to setup the nested relationship
    before registering the nested devlink instance:
 
@@ -48,10 +48,10 @@ attributes of devlink netlink.
 
 A shared devlink instance (see ``devlink_shd_get()``) is inherited at
 register time by all instances nested in it, directly or indirectly, and
-can be obtained with ``devlink_shd()``. Devlink core may take the shared
-instance lock while holding a nested instance lock, without taking the
-locks of intermediate instances. Hence, the shared instance lock must not
-be held while waiting for any nested instance to be torn down.
+can be obtained with ``devlink_shd()``. As an exception to the rule above,
+the shared instance lock is taken last, after any other instance lock it
+may be nested with. Hence, it must not be held while taking another
+instance lock or while waiting for any nested instance to be torn down.
 
 Interface documentation
 -----------------------
diff --git a/net/devlink/core.c b/net/devlink/core.c
index 07b409198e41..009df043d253 100644
--- a/net/devlink/core.c
+++ b/net/devlink/core.c
@@ -184,7 +184,9 @@ int devlink_rel_nested_in_add(u32 *rel_index, u32 devlink_index,
  * a notification of a change of this object should be sent
  * over netlink. The parent devlink instance lock needs to be
  * taken during the notification preparation.
- * Since the parent may or may not be locked, 'work' is utilized.
+ * However, since the devlink lock of nested instance is held here,
+ * we would end with wrong devlink instance lock ordering and
+ * deadlock. Therefore the work is utilized to avoid that.
  */
 void devlink_rel_nested_in_notify(struct devlink *devlink)
 {
-- 
2.53.0


      parent reply	other threads:[~2026-12-02 13:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 13:38 [PATCH net 0/2] devlink: Shared devlink locking fixes Cosmin Ratiu
2026-12-02 13:38 ` [PATCH net 1/2] devlink: Lock the shared instance directly for cross-device rates Cosmin Ratiu
2026-12-02 13:38 ` Cosmin Ratiu [this message]

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=20261202133838.834971-3-cratiu@nvidia.com \
    --to=cratiu@nvidia.com \
    --cc=cjubran@nvidia.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=dlinkin@nvidia.com \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tariqt@nvidia.com \
    --cc=vladbu@nvidia.com \
    --cc=willemb@google.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox