Netdev List
 help / color / mirror / Atom feed
* [PATCH net 0/2] devlink: Shared devlink locking fixes
@ 2026-12-02 13:38 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 ` [PATCH net 2/2] devlink: Restore parent -> nested instance locking order Cosmin Ratiu
  0 siblings, 2 replies; 3+ messages in thread
From: Cosmin Ratiu @ 2026-12-02 13:38 UTC (permalink / raw)
  To: netdev
  Cc: Jiri Pirko, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn, Simon Horman, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, Tariq Toukan, Cosmin Ratiu,
	Carolina Jubran, Dmytro Linkin, Vlad Buslov, linux-doc

This patch pair fixes a design flaw in the cross device rate scheduling
([1]) which resulted in a bug on mlx5.
One patch fixes the code, the other one the devlink shd documentation.

[1] https://lore.kernel.org/netdev/20260701073254.754518-1-tariqt@nvidia.com/

Cosmin Ratiu (2):
  devlink: Lock the shared instance directly for cross-device rates
  devlink: Restore parent -> nested instance locking order

 .../networking/devlink/devlink-shared.rst     |  7 ++-
 Documentation/networking/devlink/index.rst    | 11 +++-
 include/net/devlink.h                         |  7 ++-
 net/devlink/core.c                            | 61 +++++++++++++------
 net/devlink/devl_internal.h                   | 11 ++--
 net/devlink/rate.c                            | 53 +++++++++-------
 net/devlink/sh_dev.c                          |  2 +
 7 files changed, 103 insertions(+), 49 deletions(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH net 1/2] devlink: Lock the shared instance directly for cross-device rates
  2026-12-02 13:38 [PATCH net 0/2] devlink: Shared devlink locking fixes Cosmin Ratiu
@ 2026-12-02 13:38 ` Cosmin Ratiu
  2026-12-02 13:38 ` [PATCH net 2/2] devlink: Restore parent -> nested instance locking order Cosmin Ratiu
  1 sibling, 0 replies; 3+ messages in thread
From: Cosmin Ratiu @ 2026-12-02 13:38 UTC (permalink / raw)
  To: netdev
  Cc: Jiri Pirko, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn, Simon Horman, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, Tariq Toukan, Cosmin Ratiu,
	Carolina Jubran, Dmytro Linkin, Vlad Buslov, linux-doc

Since the referenced commit, devl_rate_lock() walks up the nested
devlink chain, locking each nested-in instance in turn, to find the
topmost instance where rates are stored.

This deadlocks on mlx5 (where nesting is SF -> PF -> shd), when a SF
add/remove concurrently runs with its parent PF unbind/reload:

  kworker (mlx5_vhca_event0)          PF unbind / devlink reload
  devl_lock(SF)                       devl_lock(PF)
  devl_unregister(SF)                 mlx5_unload()
   devlink_rates_notify_unregister()   mlx5_vhca_event_stop()
    devl_rate_lock()                    flush_workqueue()
     devl_lock(PF)  <- blocks            <- waits for kworker

Intermediate instances are only locked to safely walk the chain, while
devlink rates in shd are protected by its instance lock alone.

So fix that by removing the need to walk up the chain and avoiding the
deadlocks. This is done with:
- making the topmost instance explicit as struct devlink::shd.
- providing an accessor for it: devlink_shd().
- inheriting shd from the nested-in devlink at register time.
- refactoring devl_rate_lock() to use the saved shared instance directly
  instead of walking up the chain.

Besides avoiding the above deadlocks, this fixes a couple of latent
issues and makes the devlink infrastructure more robust since the
instance storing the rates is now fixed at register time instead of
depending on the registration state of the chain of instances between
itself and shd:
- devlink_free() runs after unregistration unlinked the instance from
  its nested-in instance, so devlink_rates_check() only checked its
  own rates, missing any rates stored in shd. It now works correctly.
- devl_rate_nodes_destroy() could in theory miss destroying rate nodes
  if called after unregistration. No in-tree driver does this currently,
  but it now does the correct thing since the instance storing the rates
  is now fixed for the lifetime of an instance.

Rates created before registration (technically possible with the current
API, but not done by any driver) are moved to the shared instance when it
is inherited.

Without a shared instance, rates are now always stored locally, so
cross-device rate parents are only possible among instances belonging to
the same shared instance, as originally intended.

Export devlink_shd() for drivers and document the locking rule.

Fixes: 6bbd1bce3099 ("devlink: Allow rate node parents from other devlinks")
Signed-off-by: Cosmin Ratiu <cratiu@nvidia.com>
---
 Documentation/networking/devlink/index.rst |  7 +++
 include/net/devlink.h                      |  7 +--
 net/devlink/core.c                         | 57 ++++++++++++++++------
 net/devlink/devl_internal.h                | 11 +++--
 net/devlink/rate.c                         | 53 ++++++++++++--------
 net/devlink/sh_dev.c                       |  2 +
 6 files changed, 93 insertions(+), 44 deletions(-)

diff --git a/Documentation/networking/devlink/index.rst b/Documentation/networking/devlink/index.rst
index 03f49389a755..aebfa5c06751 100644
--- a/Documentation/networking/devlink/index.rst
+++ b/Documentation/networking/devlink/index.rst
@@ -46,6 +46,13 @@ sure to respect following rules:
 The nested devlink info is exposed to the userspace over object-specific
 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.
+
 Interface documentation
 -----------------------
 
diff --git a/include/net/devlink.h b/include/net/devlink.h
index 7abd23376319..a1d02c957219 100644
--- a/include/net/devlink.h
+++ b/include/net/devlink.h
@@ -1599,10 +1599,10 @@ struct devlink_ops {
 				    void *priv_child, void *priv_parent,
 				    struct netlink_ext_ack *extack);
 	/* Indicates if cross-device rate nodes are supported.
-	 * This also requires a shared common ancestor object all devices that
-	 * could share rate nodes are nested in.
+	 * This also requires a shared devlink instance (see devlink_shd()) all
+	 * devices that could share rate nodes belong to.
 	 * If enabled, rate operations may be called on an instance with only
-	 * the common ancestor lock held and *without that instance lock held*.
+	 * the shared instance lock held and *without that instance lock held*.
 	 * It is the driver's responsibility to ensure proper serialization
 	 * with other operations.
 	 */
@@ -1675,6 +1675,7 @@ struct devlink *devlink_shd_get(const char *id,
 				const struct device_driver *driver);
 void devlink_shd_put(struct devlink *devlink);
 void *devlink_shd_get_priv(struct devlink *devlink);
+struct devlink *devlink_shd(const struct devlink *devlink);
 
 /**
  * struct devlink_port_ops - Port operations
diff --git a/net/devlink/core.c b/net/devlink/core.c
index 9b844e0c1bd2..07b409198e41 100644
--- a/net/devlink/core.c
+++ b/net/devlink/core.c
@@ -67,22 +67,6 @@ static void __devlink_rel_put(struct devlink_rel *rel)
 		devlink_rel_free(rel);
 }
 
-struct devlink *__must_check devlink_nested_in_get_lock(struct devlink *devlink)
-{
-	devl_assert_locked(devlink);
-	if (!devlink->rel)
-		return NULL;
-	devlink = devlinks_xa_get(devlink->rel->nested_in.devlink_index);
-	if (!devlink)
-		return NULL;
-	devl_lock(devlink);
-	if (devl_is_registered(devlink))
-		return devlink;
-	devl_unlock(devlink);
-	devlink_put(devlink);
-	return NULL;
-}
-
 static void devlink_rel_nested_in_notify_work(struct work_struct *work)
 {
 	struct devlink_rel *rel = container_of(work, struct devlink_rel,
@@ -401,6 +385,44 @@ struct devlink *devlinks_xa_lookup_get(struct net *net, unsigned long index)
 	return __devlinks_xa_find_get(net, &index, index);
 }
 
+/* Inherit the shared instance from the nested-in instance, if any. */
+static void devlink_shd_inherit(struct devlink *devlink)
+{
+	struct devlink *parent, *shd;
+
+	if (devlink->shd || !devlink->rel)
+		return;
+
+	parent = devlinks_xa_get(devlink->rel->nested_in.devlink_index);
+	if (!parent)
+		return;
+
+	shd = READ_ONCE(parent->shd);
+	if (shd) {
+		refcount_inc(&shd->refcount);
+		devlink_rates_shd_move(devlink, shd);
+		WRITE_ONCE(devlink->shd, shd);
+	}
+	devlink_put(parent);
+}
+
+/**
+ * devlink_shd - Get the shared devlink instance a devlink instance belongs to
+ * @devlink: devlink
+ *
+ * A shared devlink instance belongs to itself. Other instances inherit the
+ * shared instance of the instance they are nested in when registered.
+ * The returned instance is immutable and valid until devlink_free() of
+ * @devlink. No reference is taken, so don't pair with devlink_shd_put().
+ *
+ * Return: shared devlink instance or NULL.
+ */
+struct devlink *devlink_shd(const struct devlink *devlink)
+{
+	return READ_ONCE(devlink->shd);
+}
+EXPORT_SYMBOL_GPL(devlink_shd);
+
 /**
  * devl_register - Register devlink instance
  * @devlink: devlink
@@ -410,6 +432,7 @@ int devl_register(struct devlink *devlink)
 	ASSERT_DEVLINK_NOT_REGISTERED(devlink);
 	devl_assert_locked(devlink);
 
+	devlink_shd_inherit(devlink);
 	xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
 	devlink_notify_register(devlink);
 	devlink_rel_nested_in_notify(devlink);
@@ -544,6 +567,8 @@ void devlink_free(struct devlink *devlink)
 	WARN_ON(devlink_rates_check(devlink, NULL, NULL));
 	devl_unlock(devlink);
 	devlink_rel_put(devlink);
+	if (devlink->shd && devlink->shd != devlink)
+		devlink_put(devlink->shd);
 
 	WARN_ON(!list_empty(&devlink->trap_policer_list));
 	WARN_ON(!list_empty(&devlink->trap_group_list));
diff --git a/net/devlink/devl_internal.h b/net/devlink/devl_internal.h
index 798e707002b9..d909b2858616 100644
--- a/net/devlink/devl_internal.h
+++ b/net/devlink/devl_internal.h
@@ -62,6 +62,12 @@ struct devlink {
 	struct rcu_work rwork;
 	struct devlink_rel *rel;
 	struct xarray nested_rels;
+	/* Shared devlink instance this instance belongs to, or NULL.
+	 * Points to self for shared instances. Otherwise, inherited from the
+	 * nested-in instance at register time, if that is already registered.
+	 * Immutable once set and referenced (if != self) until devlink_free().
+	 */
+	struct devlink *shd;
 	char priv[] __aligned(NETDEV_ALIGN);
 };
 
@@ -136,10 +142,6 @@ typedef void devlink_rel_notify_cb_t(struct devlink *devlink, u32 obj_index);
 typedef void devlink_rel_cleanup_cb_t(struct devlink *devlink, u32 obj_index,
 				      u32 rel_index);
 
-/* Returns the locked+referenced nested-in instance or NULL. */
-struct devlink *__must_check
-devlink_nested_in_get_lock(struct devlink *devlink);
-
 void devlink_rel_nested_in_clear(u32 rel_index);
 int devlink_rel_nested_in_add(u32 *rel_index, u32 devlink_index,
 			      u32 obj_index, devlink_rel_notify_cb_t *notify_cb,
@@ -306,6 +308,7 @@ void devlink_traps_notify_register(struct devlink *devlink);
 void devlink_traps_notify_unregister(struct devlink *devlink);
 void devlink_rates_notify_register(struct devlink *devlink);
 void devlink_rates_notify_unregister(struct devlink *devlink);
+void devlink_rates_shd_move(struct devlink *devlink, struct devlink *shd);
 void devlink_linecards_notify_register(struct devlink *devlink);
 void devlink_linecards_notify_unregister(struct devlink *devlink);
 
diff --git a/net/devlink/rate.c b/net/devlink/rate.c
index e727c8b8b33e..ad6838f28092 100644
--- a/net/devlink/rate.c
+++ b/net/devlink/rate.c
@@ -30,34 +30,27 @@ devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)
 	return devlink_rate ?: ERR_PTR(-ENODEV);
 }
 
-/* Repeatedly walks the nested devlink chain while cross device rate nodes are
- * supported and finds the topmost instance where rates should be stored.
- * That instance is locked, referenced and returned.
- * When cross device rate nodes aren't supported the original devlink instance
- * is returned.
- */
+static bool devlink_rates_cross_dev(const struct devlink *devlink)
+{
+	return devlink->ops && devlink->ops->supported_cross_device_rate_nodes;
+}
+
+/* Locks and returns the instance where rates of 'devlink' are stored. */
 static struct devlink *devl_rate_lock(struct devlink *devlink)
 {
-	struct devlink *rate_devlink = devlink, *parent;
+	struct devlink *shd = devlink_shd(devlink);
+	struct devlink *rate_devlink;
 
 	devl_assert_locked(devlink);
 
-	while (rate_devlink->ops &&
-	       rate_devlink->ops->supported_cross_device_rate_nodes) {
-		parent = devlink_nested_in_get_lock(rate_devlink);
-		if (!parent)
-			break;
-		if (rate_devlink != devlink) {
-			/* Unlock intermediate instances. */
-			devl_unlock(rate_devlink);
-			devlink_put(rate_devlink);
-		}
-		rate_devlink = parent;
-	}
+	rate_devlink = shd && devlink_rates_cross_dev(devlink) ? shd : devlink;
+
+	if (rate_devlink != devlink)
+		devl_lock(rate_devlink);
 	return rate_devlink;
 }
 
-/* Unlocks and puts 'rate devlink' if different than 'devlink'. */
+/* Unlocks 'rate devlink' if different than 'devlink'. */
 static void devl_rate_unlock(struct devlink *devlink,
 			     struct devlink *rate_devlink)
 {
@@ -65,7 +58,6 @@ static void devl_rate_unlock(struct devlink *devlink,
 		return;
 
 	devl_unlock(rate_devlink);
-	devlink_put(rate_devlink);
 }
 
 static struct devlink_rate *
@@ -264,6 +256,25 @@ void devlink_rates_notify_register(struct devlink *devlink)
 	devl_rate_unlock(devlink, rate_devlink);
 }
 
+/* Called at register time, before 'devlink' starts belonging to 'shd'.
+ * Rates created before register are stored locally. When cross device rate
+ * nodes are supported, they must be moved to the shared instance, where they
+ * will be looked up from now on. They can only have parents among themselves,
+ * so the hierarchy stays intact.
+ */
+void devlink_rates_shd_move(struct devlink *devlink, struct devlink *shd)
+{
+	devl_assert_locked(devlink);
+
+	if (!devlink_rates_cross_dev(devlink) ||
+	    list_empty(&devlink->rate_list))
+		return;
+
+	devl_lock(shd);
+	list_splice_tail_init(&devlink->rate_list, &shd->rate_list);
+	devl_unlock(shd);
+}
+
 void devlink_rates_notify_unregister(struct devlink *devlink)
 {
 	struct devlink_rate *rate_node;
diff --git a/net/devlink/sh_dev.c b/net/devlink/sh_dev.c
index 85acce97e788..d4e376eca1f3 100644
--- a/net/devlink/sh_dev.c
+++ b/net/devlink/sh_dev.c
@@ -50,6 +50,8 @@ static struct devlink_shd *devlink_shd_create(const char *id,
 		goto err_devlink_free;
 	shd->priv_size = priv_size;
 	refcount_set(&shd->refcount, 1);
+	/* Shared instances belong to themselves. */
+	WRITE_ONCE(devlink->shd, devlink);
 
 	devl_lock(devlink);
 	devl_register(devlink);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH net 2/2] devlink: Restore parent -> nested instance locking order
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Cosmin Ratiu @ 2026-12-02 13:38 UTC (permalink / raw)
  To: netdev
  Cc: Jiri Pirko, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn, Simon Horman, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, Tariq Toukan, Cosmin Ratiu,
	Carolina Jubran, Dmytro Linkin, Vlad Buslov, linux-doc

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 13:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH net 2/2] devlink: Restore parent -> nested instance locking order Cosmin Ratiu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox