From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796305280; b=1QUAmgguDLPSsSLjaMbDYFf5tOhrdO2+pcPEcTTrigYWwUSQmeYrS3ytogsb2lcn+3OS e0JURSaBlJf4fScyeGK3TVimXw/eMdByljar6yATVoJ0c0P9TisKf3teaGoIRix8DqpZu /Nq9ScUjzjvHU4G0qUJPvpcRplDVtz1vZ6qM6UZliJB6vKt5QUXZd6PCU/9VKkpGspmpN XZDCqywjycDRRnipV1FsAuMYE62Ouogvr4U3Ggk2Sv+0qdD3Z6ekTM0yUzb/hcw56RrP5 sz6IJtrS4KLjABs22MBnhu9Qs2TEva7HW2AECD7Jt80grcbtETInXNN4kUS9/BM4pNw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796305280; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=VIHuX7FafUT5oEVpA+7Mt1hACt/q7/ni52oyyBFZixs=; b=QAAvvtCj6sZhprHKV5pi5IpPpaeG7NMRHjavQ79IYL5tO71Ri4hp5cW1Ba7AFuCGTnAM 0NQ4XbKwLAhP2VzDRO5R5+5Kbw3f8cJH9sE6GbXA94NKhbwTuQ3BjJtmW/YPwr4NKPxaP mhgbOvnfEPwC2gC3GXfGUw3JwkVv4Gf4oIAPpdu1uzHKzh2GQ2nEalk6X6MLREj4o3D4T vHnPTcwGCbw2A2WxZwvAjgzuBUPQXGFJaoOmncNaD5oWuXY8G70nhfIUe+2zSn4cEl5Hf G1B0dtwx/wcwBAC0EixB9PA47NNxXExKlrF2TWqWelffZF3bvZ3soX5uw2TC4GAVCyA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mfxAxjdT; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 796F9606A322; Thu, 3 Dec 2026 13:41:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mfxAxjdT Received: by smtp.kernel.org (Postfix) with ESMTPSA id 333A41F00898; Thu, 3 Dec 2026 13:41:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796305280; bh=VIHuX7FafUT5oEVpA+7Mt1hACt/q7/ni52oyyBFZixs=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=mfxAxjdTchL+4vQyLOFcF1rC2xJ8wWdECO4FOa9Kx7g7B2++3Sa/VJXosto0sOaAT PtsLNswg7xGuC0KsX9jzPrIOJMxoAUqvX+0EXzRr06Utec+vlx/UQSypok8ElW3xCm MDdLX71WsCo+RFvnCVr7JpVMIiaRFMPR9AyXIMgDnWxS6R6d33a7J1SBcLvpke6myR RDbDwOXkyl82H7UYW7Sy6VvHuqaG7dI4rlHMJQQ/m14LvSLZb4LzSl/MkvnS5IcOAG m6qxCJ/xiWgf7AAGxKWHIT3TdAYKcJV4O3yI6EgKk6D58S/S7hG1EnqHNL+5/tnsZV rMF1dd8ns9B7A== Subject: Re: [PATCH net 1/2] devlink: Lock the shared instance directly for cross-device rates From: netdev-bot+sashiko@kernel.org To: cratiu@nvidia.com Cc: netdev@vger.kernel.org, jiri@resnulli.us, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, willemb@google.com, horms@kernel.org, corbet@lwn.net, skhan@linuxfoundation.org, rdunlap@infradead.org, tariqt@nvidia.com, cjubran@nvidia.com, dlinkin@nvidia.com, vladbu@nvidia.com, linux-doc@vger.kernel.org Date: Thu, 03 Dec 2026 13:41:18 +0000 Message-ID: <179630527873.3146.8481195294038085787@kernel.org> In-Reply-To: <20261202133838.834971-2-cratiu@nvidia.com> References: <20261202133838.834971-2-cratiu@nvidia.com> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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