From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
andrew+netdev@lunn.ch, horms@kernel.org, haiyangz@microsoft.com,
wei.liu@kernel.org, decui@microsoft.com, longli@microsoft.com,
linux-hyperv@vger.kernel.org, hawk@kernel.org, andriin@fb.com,
Jakub Kicinski <kuba@kernel.org>,
stable+noautosel@kernel.org
Subject: [PATCH net-next 4/5] hv_netvsc: move a new VF to netvsc's netns from a work item
Date: Wed, 30 Sep 2026 18:41:30 -0700 [thread overview]
Message-ID: <20261001014131.310771-5-kuba@kernel.org> (raw)
In-Reply-To: <20261001014131.310771-1-kuba@kernel.org>
When the VF registers in a different netns than netvsc, for example
because it was hot-added into init_net while netvsc lives in a container
netns, netvsc_register_vf() moves it over with dev_change_net_namespace()
straight from the VF's NETDEV_REGISTER notifier. Since commit
4c975fd70002 ("net: hold instance lock during NETDEV_REGISTER/UP") that
notifier runs under the VF's instance lock, which the netns change takes
again. For a VF with an ops lock the registration then deadlocks with
RTNL held, taking the networking of the whole VM down with it. mana
always has an ops lock, as it selects NET_SHAPER, and so does mlx5 for
its queue management ops, so every VF hot-add or servicing event hangs a
VM running netvsc in a netns, as does moving the VF out by hand.
netvsc already leaves moving its VF to vfns_work when netvsc itself
changes netns. Hand the new VF to the same work, holding a reference
until it runs. The VF still joins netvsc when it registers again in
netvsc's netns. If netvsc gets to the VF's netns before the work runs,
for example because netvsc's netns went away, there is no move to
register the VF again, so have the work take the VF over itself.
Spotted by AI while reviewing the XDP propagation series.
Not verified, and untested.
Cc: stable+noautosel@kernel.org # LLM report + LLM fix, untested
Fixes: 4c975fd70002 ("net: hold instance lock during NETDEV_REGISTER/UP")
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
drivers/net/hyperv/hyperv_net.h | 3 +++
drivers/net/hyperv/netvsc_drv.c | 41 +++++++++++++++++++++++----------
2 files changed, 32 insertions(+), 12 deletions(-)
diff --git a/drivers/net/hyperv/hyperv_net.h b/drivers/net/hyperv/hyperv_net.h
index 4841367fdab2..5c771fe8d766 100644
--- a/drivers/net/hyperv/hyperv_net.h
+++ b/drivers/net/hyperv/hyperv_net.h
@@ -1064,6 +1064,9 @@ struct net_device_context {
struct netvsc_vf_pcpu_stats __percpu *vf_stats;
struct delayed_work vf_takeover;
struct delayed_work vfns_work;
+ /* VF for vfns_work to move to our netns, see netvsc_register_vf() */
+ struct net_device *vfns_dev;
+ netdevice_tracker vfns_dev_tracker;
/* 1: allocated, serial number is valid. 0: not allocated */
u32 vf_alloc;
diff --git a/drivers/net/hyperv/netvsc_drv.c b/drivers/net/hyperv/netvsc_drv.c
index 17c86e749357..10c05809c065 100644
--- a/drivers/net/hyperv/netvsc_drv.c
+++ b/drivers/net/hyperv/netvsc_drv.c
@@ -2311,6 +2311,12 @@ static int netvsc_prepare_bonding(struct net_device *vf_netdev)
return NOTIFY_DONE;
}
+static void netvsc_vfns_dev_put(struct net_device_context *ndev_ctx)
+{
+ netdev_put(ndev_ctx->vfns_dev, &ndev_ctx->vfns_dev_tracker);
+ ndev_ctx->vfns_dev = NULL;
+}
+
static int netvsc_register_vf(struct net_device *vf_netdev, int context)
{
struct net_device_context *net_device_ctx;
@@ -2340,19 +2346,17 @@ static int netvsc_register_vf(struct net_device *vf_netdev, int context)
/* if synthetic interface is a different namespace,
* then move the VF to that namespace; join will be
- * done again in that context.
+ * done again in that context. The VF's lock is held
+ * for its registration, so leave the move to vfns_work.
*/
if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
- ret = dev_change_net_namespace(vf_netdev,
- dev_net(ndev), "eth%d");
- if (ret)
- netdev_err(vf_netdev,
- "could not move to same namespace as %s: %d\n",
- ndev->name, ret);
- else
- netdev_info(vf_netdev,
- "VF moved to namespace with: %s\n",
- ndev->name);
+ if (net_device_ctx->vfns_dev != vf_netdev) {
+ netvsc_vfns_dev_put(net_device_ctx);
+ netdev_hold(vf_netdev, &net_device_ctx->vfns_dev_tracker,
+ GFP_KERNEL);
+ net_device_ctx->vfns_dev = vf_netdev;
+ }
+ schedule_delayed_work(&net_device_ctx->vfns_work, 0);
return NOTIFY_DONE;
}
@@ -2695,6 +2699,7 @@ static void netvsc_remove(struct hv_device *dev)
rtnl_lock();
cancel_delayed_work_sync(&ndev_ctx->vfns_work);
+ netvsc_vfns_dev_put(ndev_ctx);
nvdev = rtnl_dereference(ndev_ctx->nvdev);
if (nvdev)
@@ -2738,6 +2743,7 @@ static int netvsc_suspend(struct hv_device *dev)
rtnl_lock();
cancel_delayed_work_sync(&ndev_ctx->vfns_work);
+ netvsc_vfns_dev_put(ndev_ctx);
nvdev = rtnl_dereference(ndev_ctx->nvdev);
if (nvdev == NULL) {
@@ -2815,7 +2821,9 @@ static void netvsc_event_set_vf_ns(struct net_device *ndev)
vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
if (!vf_netdev)
- return;
+ vf_netdev = ndev_ctx->vfns_dev;
+ if (!vf_netdev || vf_netdev->reg_state != NETREG_REGISTERED)
+ goto out;
if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
ret = dev_change_net_namespace(vf_netdev, dev_net(ndev),
@@ -2828,7 +2836,16 @@ static void netvsc_event_set_vf_ns(struct net_device *ndev)
netdev_info(vf_netdev,
"Moved VF to namespace with: %s\n",
ndev->name);
+ } else if (!rtnl_dereference(ndev_ctx->vf_netdev)) {
+ /* netvsc got to the VF's netns first, so no move will
+ * register the VF again, take it over from here
+ */
+ netdev_lock_ops(vf_netdev);
+ netvsc_register_vf(vf_netdev, VF_REG_IN_NOTIFIER);
+ netdev_unlock_ops(vf_netdev);
}
+out:
+ netvsc_vfns_dev_put(ndev_ctx);
}
void netvsc_vfns_work(struct work_struct *w)
--
2.55.0
next prev parent reply other threads:[~2026-10-01 1:41 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-01 1:41 [PATCH net-next 0/5] hv_netvsc: make XDP propagation act more like bonding Jakub Kicinski
2026-10-01 1:41 ` [PATCH net-next 1/5] hv_netvsc: fix the program refcount when the VF refuses XDP Jakub Kicinski
2026-10-06 19:05 ` Kameron Carr
2026-10-01 1:41 ` [PATCH net-next 2/5] hv_netvsc: hold the VF's instance lock when installing XDP on it Jakub Kicinski
2026-10-01 1:41 ` [PATCH net-next 3/5] hv_netvsc: treat the VF's XDP program the way bonding treats a slave's Jakub Kicinski
2026-10-04 16:02 ` netdev-bot+sashiko
2026-10-01 1:41 ` Jakub Kicinski [this message]
2026-10-04 16:02 ` [PATCH net-next 4/5] hv_netvsc: move a new VF to netvsc's netns from a work item netdev-bot+sashiko
2026-10-01 1:41 ` [PATCH net-next 5/5] hv_netvsc: let the core take XDP off a netvsc device that is going away Jakub Kicinski
2026-10-04 16:02 ` netdev-bot+sashiko
2026-10-02 19:38 ` [EXTERNAL] [PATCH net-next 0/5] hv_netvsc: make XDP propagation act more like bonding Haiyang Zhang
2026-10-06 20:59 ` Kameron Carr
2026-10-07 2:00 ` Jakub Kicinski
2026-10-08 21:22 ` Erni Sri Satya Vennela
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=20261001014131.310771-5-kuba@kernel.org \
--to=kuba@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=andriin@fb.com \
--cc=davem@davemloft.net \
--cc=decui@microsoft.com \
--cc=edumazet@google.com \
--cc=haiyangz@microsoft.com \
--cc=hawk@kernel.org \
--cc=horms@kernel.org \
--cc=linux-hyperv@vger.kernel.org \
--cc=longli@microsoft.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=stable+noautosel@kernel.org \
--cc=wei.liu@kernel.org \
/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