Re: [PATCH net 1/2] devlink: Lock the shared instance directly for cross-device rates

From
Cosmin Ratiu <hidden>
Date
2026-12-04 13:41:27
Also in
linux-doc
Reply to
netdev-bot+sashiko@kernel.org, 2026-12-03
On Thu, 2026-12-03 at 13:41 +0000, netdev-bot+sashiko@kernel.org wrote:
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.
quoted
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?
Will fix in V2.
quoted
+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.
+
[ ... ]
quoted
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
[ ... ]
quoted
@@ -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-
quoted
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()
A good find! Will fix in V2.
locking the parent isn't an option here, as it reintroduces the wrong
locking order I'm trying to fix with the patch.
But using the devlinks xa_lock to do the inheritance seems like a
decent option, I've tried it and it seems fine.
quoted
+		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()-
quoted
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?
Will fix.
quoted
+ *
+ * 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.
There's another mlx5 patch which fixes another bug and uses this
exported helper. I'll send it alongside V2.
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.
Will fix.
[ ... ]
quoted
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)
[ ... ]
quoted
+/* 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-
quoted
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-
quoted
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.

[ ... ]
Will fix. The 2nd patch fixes the documentation.

Cosmin.
Thread (7 messages) 7 messages, 2 authors, last reply 18h ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help