Linux Documentation
 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; 5+ 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] 5+ 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-03 13:41   ` netdev-bot+sashiko
  2026-12-02 13:38 ` [PATCH net 2/2] devlink: Restore parent -> nested instance locking order Cosmin Ratiu
  1 sibling, 1 reply; 5+ 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] 5+ 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
  2026-12-03 13:41   ` netdev-bot+sashiko
  1 sibling, 1 reply; 5+ 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] 5+ messages in thread

* Re: [PATCH net 1/2] devlink: Lock the shared instance directly for cross-device rates
  2026-12-02 13:38 ` [PATCH net 1/2] devlink: Lock the shared instance directly for cross-device rates Cosmin Ratiu
@ 2026-12-03 13:41   ` netdev-bot+sashiko
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 13:41 UTC (permalink / raw)
  To: cratiu
  Cc: netdev, jiri, davem, edumazet, kuba, pabeni, willemb, horms,
	corbet, skhan, rdunlap, tariqt, cjubran, dlinkin, vladbu,
	linux-doc

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 4

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] devlink_shd_inherit() in net/devlink/core.c has a lifetime
  race (TOCTOU) on the shared instance.
- [Low] This Fixes:-tagged deadlock fix adds and exports a new driver-
  facing API, devlink_shd(), with EXPORT_SYMBOL_GPL in…
- [Low] The devlink_shd() kernel-doc in net/devlink/core.c says the
  returned instance is "valid until devlink_free() of @devlink", and the…
- [Low] Documentation/networking/devlink/index.rst (lines 49-50) says the
  shared instance "is inherited at register time by all instances nested…
- [Low] Two pieces of documentation still describe the old model at the
  end of the series.

--

devlink: Lock the shared instance directly for cross-device rates

This commit stops devl_rate_lock() from walking the nested devlink chain.
Instead, the topmost shared instance is stored in struct devlink::shd,
which is inherited from the nested-in instance at devl_register() time,
and devl_rate_lock() locks that instance directly. This avoids an mlx5
deadlock between SF teardown and PF unbind/reload. A new devlink_shd()
accessor is exported for drivers.

> diff --git a/Documentation/networking/devlink/index.rst b/Documentation/networking/devlink/index.rst
> index 03f49389a7550..aebfa5c067519 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

[Severity: Low]
Is "inherited at register time by all instances nested in it, directly or
indirectly" accurate in every case?

devlink_shd_inherit() runs only once, from devl_register(), and copies
whatever ->shd the parent has at that moment. It returns early if
devlinks_xa_get() does not find the parent registered, or if the parent
has no ->shd yet. Since the field is immutable, nothing inherits it later.

So a nested instance registered before its intermediate parent keeps
shd == NULL for good and stores its rates locally.

The struct devlink::shd comment already has the "if that is already
registered" condition. Should index.rst state the same condition?

> +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.
> +

[ ... ]

> diff --git a/net/devlink/core.c b/net/devlink/core.c
> index 9b844e0c1bd2d..07b409198e41e 100644
> --- a/net/devlink/core.c
> +++ b/net/devlink/core.c

[ ... ]

> @@ -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);

[Severity: Medium]
Can this race with the parent being unregistered and freed?

devlinks_xa_get() only checks the DEVLINK_REGISTERED mark and takes a
reference with devlink_try_get(). That reference keeps the parent's
memory alive but does not keep the parent registered. Nothing here holds
the parent lock or rechecks devl_is_registered().

If another thread runs devl_unregister(parent) and devlink_free(parent)
in this window, devlink_free() drops the parent's reference on its shd:

devlink_free(parent)
    if (devlink->shd && devlink->shd != devlink)
        devlink_put(devlink->shd);

Suppose the shared instance was already torn down through
devlink_shd_put()->devlink_shd_destroy()->devlink_free(shd). Then that
put can be the last reference, and it queues devlink_release() through
queue_rcu_work().

The child is not in an RCU read-side section here. The plain
refcount_inc() would only warn on zero, and the dangling pointer would
still be stored in child->shd.

After the grace period, devlink_release() destroys the mutex and calls
kvfree() on the shared instance. Would the later uses then touch freed
memory? These include devl_lock(shd) in devlink_rates_shd_move() and
devl_rate_lock(), the rate_list walks, and the final
devlink_put(devlink->shd) in devlink_free(child).

The removed devlink_nested_in_get_lock() took the parent's lock and
checked devl_is_registered(), which ruled this out.

mlx5 does not seem to hit this today. Its remove_one() tears down the
nested SF and mlx5e instances before mlx5_shd_uninit() and
mlx5_devlink_free().

A related, more theoretical question: nothing seems to order the parent's
WRITE_ONCE(devlink->shd) before its xa_set_mark(DEVLINK_REGISTERED)
against the child's lockless READ_ONCE(parent->shd). On weakly ordered
CPUs, could the child see the mark but read a stale NULL shd?

Would any of these close the window?

  - taking the shd reference with devlink_try_get() or
    refcount_inc_not_zero() and skipping inheritance on failure
  - locking the parent and rechecking devl_is_registered()
  - moving the parent's devlink_put(shd) into devlink_release()

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

[Severity: Low]
Is "valid until devlink_free() of @devlink" accurate when the shared
instance has already been destroyed?

When the devlink_shd refcount reaches zero,
devlink_shd_put()->devlink_shd_destroy() runs devl_unregister(),
kfree(shd->id) and devlink_free() on the shared instance. That happens
regardless of the devlink->refcount references that nested instances take
in devlink_shd_inherit().

In mlx5, remove_one() calls mlx5_shd_uninit() before mlx5_devlink_free().
For the last PF, devlink_free(PF)->devlink_rates_check()->devl_rate_lock()
therefore locks and walks the rate_list of a shared instance that is
already unregistered and has been through devlink_free().

The PF's reference keeps the memory and the mutex alive, so this is not a
memory safety problem in-tree. Could this kernel-doc, and the
struct devlink::shd comment, say that the returned instance may already be
unregistered and that shared state such as shd->id may be gone?

> + *
> + * Return: shared devlink instance or NULL.
> + */
> +struct devlink *devlink_shd(const struct devlink *devlink)
> +{
> +	return READ_ONCE(devlink->shd);
> +}
> +EXPORT_SYMBOL_GPL(devlink_shd);

[Severity: Low]
This isn't a bug, but is this export needed in a Fixes: tagged patch? No
in-tree module seems to use devlink_shd(). The only caller is
devl_rate_lock() in net/devlink/rate.c, which is built into the same core.

mlx5 is the only driver that sets supported_cross_device_rate_nodes, and
it still takes its QoS lock based on its own mdev->shd:

drivers/net/ethernet/mellanox/mlx5/core/esw/qos.c:esw_qos_lock() {
    ...
	if (dev->shd)
		devl_lock(dev->shd);
	else
		mutex_lock(&dev->priv.eswitch->state_lock);
    ...
}

The kernel-doc above also calls the returned instance "immutable".
However, for nested instances devlink_shd() returns NULL before
devl_register() and non-NULL afterwards. The "Immutable once set" wording
in the struct comment is more precise.

[ ... ]

> diff --git a/net/devlink/rate.c b/net/devlink/rate.c
> index e727c8b8b33ea..ad6838f280923 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)

[ ... ]

> +/* 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;
>  }

[Severity: Low]
This isn't a bug, but devl_rate_lock() no longer walks ancestors and the
comment in devl_rate_nodes_destroy() still uses the old wording:

net/devlink/rate.c:devl_rate_nodes_destroy() {
    ...
		/* This could destroy rate objects on other devlinks in the
		 * same hierarchy under 'rate_devlink'. This is safe because
		 * the shared common ancestor is locked so there can be no
		 * other concurrent rate operations on devlink_rate->devlink.
		 */
    ...
}

Should it say the shared instance is locked, to match the updated
supported_cross_device_rate_nodes comment in include/net/devlink.h?

Also, the API Functions list in
Documentation/networking/devlink/devlink-shared.rst still only names
devlink_shd_get(), devlink_shd_put() and devlink_shd_get_priv(), and not
the newly exported devlink_shd().

The Locking section of devlink-shared.rst is also out of date after this
patch. The follow-up commit "devlink: Restore parent -> nested instance
locking order" rewrites that section.

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202133838.834971-1-cratiu%40nvidia.com

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

* Re: [PATCH net 2/2] devlink: Restore parent -> nested instance locking order
  2026-12-02 13:38 ` [PATCH net 2/2] devlink: Restore parent -> nested instance locking order Cosmin Ratiu
@ 2026-12-03 13:41   ` netdev-bot+sashiko
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 13:41 UTC (permalink / raw)
  To: cratiu
  Cc: netdev, jiri, davem, edumazet, kuba, pabeni, willemb, horms,
	corbet, skhan, rdunlap, tariqt, cjubran, dlinkin, vladbu,
	linux-doc

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] This patch restores a comment on devlink_rel_nested_in_notify()
  (net/devlink/core.c, lines 187-189).

--

devlink: Restore parent -> nested instance locking order

This commit restores the parent -> nested devlink instance locking order
in the documentation and in the devlink_rel_nested_in_notify() comment.
It also documents the shared instance lock as an exception that is always
taken last, and updates devlink-shared.rst to match.

> diff --git a/Documentation/networking/devlink/index.rst b/Documentation/networking/devlink/index.rst
> index aebfa5c067519..2fc929bf55443 100644
> --- a/Documentation/networking/devlink/index.rst
> +++ b/Documentation/networking/devlink/index.rst

[ ... ]

> @@ -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 07b409198e41e..009df043d2531 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)
>  {

[Severity: Low]
Does this restored comment still hold when the nested-in parent is the
shared instance?

The index.rst and devlink-shared.rst changes in this same patch say the
shared instance lock is "taken last, after any other instance lock it may
be nested with". In that case, taking the parent lock while holding the
nested lock is the allowed order.

mlx5_init_one() sets up exactly this case, with the shared instance as
the nested_in parent of the PF:

drivers/net/ethernet/mellanox/mlx5/core/main.c:mlx5_init_one() {
    ...
	devl_lock(devlink);
	if (dev->shd) {
		err = devl_nested_devlink_set(dev->shd, devlink);
    ...
}

In the same devl_register() call, core code already takes the shared lock
directly while the PF lock is held. After that it calls
devlink_rel_nested_in_notify():

devl_register()
  devlink_shd_inherit()
    devlink_rates_shd_move(devlink, shd)
      devl_assert_locked(devlink);
      ...
      devl_lock(shd);
  ...
  devlink_rel_nested_in_notify(devlink)

For this parent, the comment says the direct order would be wrong and
could deadlock, while the updated docs say it is the required order.
Using the work is still correct, and it is still needed for non-shared
parents such as a PF with SFs nested in its port functions. So this only
affects the comment, not runtime behaviour.

Could the comment be limited to non-shared parents, or could it mention
the shared instance exception?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202133838.834971-1-cratiu%40nvidia.com

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

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

Thread overview: 5+ 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-03 13:41   ` netdev-bot+sashiko
2026-12-02 13:38 ` [PATCH net 2/2] devlink: Restore parent -> nested instance locking order Cosmin Ratiu
2026-12-03 13:41   ` netdev-bot+sashiko

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