From: Cosmin Ratiu <cratiu@nvidia.com>
To: "netdev-bot+sashiko@kernel.org" <netdev-bot+sashiko@kernel.org>
Cc: "corbet@lwn.net" <corbet@lwn.net>,
"davem@davemloft.net" <davem@davemloft.net>,
"edumazet@kernel.org" <edumazet@kernel.org>,
"willemb@google.com" <willemb@google.com>,
"rdunlap@infradead.org" <rdunlap@infradead.org>,
Dmytro Linkin <dlinkin@nvidia.com>,
"pabeni@redhat.com" <pabeni@redhat.com>,
"jiri@resnulli.us" <jiri@resnulli.us>,
"skhan@linuxfoundation.org" <skhan@linuxfoundation.org>,
Carolina Jubran <cjubran@nvidia.com>,
"horms@kernel.org" <horms@kernel.org>,
Vlad Buslov <vladbu@nvidia.com>,
"kuba@kernel.org" <kuba@kernel.org>,
Tariq Toukan <tariqt@nvidia.com>,
"linux-doc@vger.kernel.org" <linux-doc@vger.kernel.org>,
"netdev@vger.kernel.org" <netdev@vger.kernel.org>
Subject: Re: [PATCH net 1/2] devlink: Lock the shared instance directly for cross-device rates
Date: Fri, 4 Dec 2026 13:41:24 +0000 [thread overview]
Message-ID: <f4cd80d08bc87cdc8f5bd39b8f6e7ed82b8574c9.camel@nvidia.com> (raw)
In-Reply-To: <179630527873.3146.8481195294038085787@kernel.org>
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.
>
> > 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.
>
> > +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()
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.
>
> > + 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?
Will fix.
>
> > + *
> > + * 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.
>
> [ ... ]
>
> > 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.
>
> [ ... ]
>
Will fix. The 2nd patch fixes the documentation.
Cosmin.
next prev parent reply other threads:[~2026-12-04 13:41 UTC|newest]
Thread overview: 7+ 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-03 13:41 ` netdev-bot+sashiko
2026-12-04 13:41 ` Cosmin Ratiu [this message]
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
2026-12-04 13:42 ` Cosmin Ratiu
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=f4cd80d08bc87cdc8f5bd39b8f6e7ed82b8574c9.camel@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-bot+sashiko@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