netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net,v2] hv_netvsc: Fix VF namespace also in synthetic NIC NETDEV_REGISTER event
@ 2024-10-16 15:43 Haiyang Zhang
  2024-10-17 15:44 ` Simon Horman
  0 siblings, 1 reply; 3+ messages in thread
From: Haiyang Zhang @ 2024-10-16 15:43 UTC (permalink / raw)
  To: linux-hyperv, netdev
  Cc: haiyangz, kys, wei.liu, decui, edumazet, kuba, pabeni, stephen,
	davem, linux-kernel, stable

The existing code moves VF to the same namespace as the synthetic NIC
during netvsc_register_vf(). But, if the synthetic device is moved to a
new namespace after the VF registration, the VF won't be moved together.

To make the behavior more consistent, add a namespace check for synthetic
NIC's NETDEV_REGISTER event (generated during its move), and move the VF
if it is not in the same namespace.

Cc: stable@vger.kernel.org
Fixes: c0a41b887ce6 ("hv_netvsc: move VF to same namespace as netvsc device")
Suggested-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Haiyang Zhang <haiyangz@microsoft.com>
---
v2: Move my fix to synthetic NIC's NETDEV_REGISTER event as suggested by Stephen.

---
 drivers/net/hyperv/netvsc_drv.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/drivers/net/hyperv/netvsc_drv.c b/drivers/net/hyperv/netvsc_drv.c
index 153b97f8ec0d..54e98356ee93 100644
--- a/drivers/net/hyperv/netvsc_drv.c
+++ b/drivers/net/hyperv/netvsc_drv.c
@@ -2798,6 +2798,30 @@ static struct  hv_driver netvsc_drv = {
 	},
 };
 
+/* Set VF's namespace same as the synthetic NIC */
+static void netvsc_event_set_vf_ns(struct net_device *ndev)
+{
+	struct net_device_context *ndev_ctx = netdev_priv(ndev);
+	struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
+	int ret;
+
+	if (!vf_netdev)
+		return;
+
+	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,
+				   "Cannot move to same namespace as %s: %d\n",
+				   ndev->name, ret);
+		else
+			netdev_info(vf_netdev,
+				    "Moved VF to namespace with: %s\n",
+				    ndev->name);
+	}
+}
+
 /*
  * On Hyper-V, every VF interface is matched with a corresponding
  * synthetic interface. The synthetic interface is presented first
@@ -2810,6 +2834,11 @@ static int netvsc_netdev_event(struct notifier_block *this,
 	struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
 	int ret = 0;
 
+	if (event_dev->netdev_ops == &device_ops && event == NETDEV_REGISTER) {
+		netvsc_event_set_vf_ns(event_dev);
+		return NOTIFY_DONE;
+	}
+
 	ret = check_dev_is_matching_vf(event_dev);
 	if (ret != 0)
 		return NOTIFY_DONE;
-- 
2.34.1


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

* Re: [PATCH net,v2] hv_netvsc: Fix VF namespace also in synthetic NIC NETDEV_REGISTER event
  2024-10-16 15:43 [PATCH net,v2] hv_netvsc: Fix VF namespace also in synthetic NIC NETDEV_REGISTER event Haiyang Zhang
@ 2024-10-17 15:44 ` Simon Horman
  2024-10-17 16:06   ` Haiyang Zhang
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Horman @ 2024-10-17 15:44 UTC (permalink / raw)
  To: Haiyang Zhang
  Cc: linux-hyperv, netdev, kys, wei.liu, decui, edumazet, kuba, pabeni,
	stephen, davem, linux-kernel, stable

On Wed, Oct 16, 2024 at 08:43:57AM -0700, Haiyang Zhang wrote:
> The existing code moves VF to the same namespace as the synthetic NIC
> during netvsc_register_vf(). But, if the synthetic device is moved to a
> new namespace after the VF registration, the VF won't be moved together.
> 
> To make the behavior more consistent, add a namespace check for synthetic
> NIC's NETDEV_REGISTER event (generated during its move), and move the VF
> if it is not in the same namespace.
> 
> Cc: stable@vger.kernel.org
> Fixes: c0a41b887ce6 ("hv_netvsc: move VF to same namespace as netvsc device")
> Suggested-by: Stephen Hemminger <stephen@networkplumber.org>
> Signed-off-by: Haiyang Zhang <haiyangz@microsoft.com>
> ---
> v2: Move my fix to synthetic NIC's NETDEV_REGISTER event as suggested by Stephen.
> 
> ---
>  drivers/net/hyperv/netvsc_drv.c | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/drivers/net/hyperv/netvsc_drv.c b/drivers/net/hyperv/netvsc_drv.c
> index 153b97f8ec0d..54e98356ee93 100644
> --- a/drivers/net/hyperv/netvsc_drv.c
> +++ b/drivers/net/hyperv/netvsc_drv.c
> @@ -2798,6 +2798,30 @@ static struct  hv_driver netvsc_drv = {
>  	},
>  };
>  
> +/* Set VF's namespace same as the synthetic NIC */
> +static void netvsc_event_set_vf_ns(struct net_device *ndev)
> +{
> +	struct net_device_context *ndev_ctx = netdev_priv(ndev);
> +	struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
> +	int ret;

In Networking code it is preferred to arrange local variables in reverse
xmas tree order - longest line to shortest.

I believe that could be achieved as follows (completely untested!):

	struct net_device_context *ndev_ctx = netdev_priv(ndev);
	struct net_device *vf_netdev;
	int ret;

	vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
	if (!vf_netdev)
		return;

With that addressed please feel free to add:

Reviewed-by: Simon Horman <horms@kernel.org>

> +
> +	if (!vf_netdev)
> +		return;
> +
> +	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,
> +				   "Cannot move to same namespace as %s: %d\n",
> +				   ndev->name, ret);
> +		else
> +			netdev_info(vf_netdev,
> +				    "Moved VF to namespace with: %s\n",
> +				    ndev->name);
> +	}
> +}
> +
>  /*
>   * On Hyper-V, every VF interface is matched with a corresponding
>   * synthetic interface. The synthetic interface is presented first
> @@ -2810,6 +2834,11 @@ static int netvsc_netdev_event(struct notifier_block *this,
>  	struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
>  	int ret = 0;
>  
> +	if (event_dev->netdev_ops == &device_ops && event == NETDEV_REGISTER) {
> +		netvsc_event_set_vf_ns(event_dev);
> +		return NOTIFY_DONE;
> +	}
> +
>  	ret = check_dev_is_matching_vf(event_dev);
>  	if (ret != 0)
>  		return NOTIFY_DONE;

-- 
pw-bot: changes-requested

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

* RE: [PATCH net,v2] hv_netvsc: Fix VF namespace also in synthetic NIC NETDEV_REGISTER event
  2024-10-17 15:44 ` Simon Horman
@ 2024-10-17 16:06   ` Haiyang Zhang
  0 siblings, 0 replies; 3+ messages in thread
From: Haiyang Zhang @ 2024-10-17 16:06 UTC (permalink / raw)
  To: Simon Horman
  Cc: linux-hyperv@vger.kernel.org, netdev@vger.kernel.org,
	KY Srinivasan, wei.liu@kernel.org, Dexuan Cui,
	edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
	stephen@networkplumber.org, davem@davemloft.net,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org



> -----Original Message-----
> From: Simon Horman <horms@kernel.org>
> Sent: Thursday, October 17, 2024 11:45 AM
> To: Haiyang Zhang <haiyangz@microsoft.com>
> Cc: linux-hyperv@vger.kernel.org; netdev@vger.kernel.org; KY Srinivasan
> <kys@microsoft.com>; wei.liu@kernel.org; Dexuan Cui <decui@microsoft.com>;
> edumazet@google.com; kuba@kernel.org; pabeni@redhat.com;
> stephen@networkplumber.org; davem@davemloft.net; linux-
> kernel@vger.kernel.org; stable@vger.kernel.org
> Subject: Re: [PATCH net,v2] hv_netvsc: Fix VF namespace also in synthetic
> NIC NETDEV_REGISTER event
> 
> On Wed, Oct 16, 2024 at 08:43:57AM -0700, Haiyang Zhang wrote:
> > The existing code moves VF to the same namespace as the synthetic NIC
> > during netvsc_register_vf(). But, if the synthetic device is moved to a
> > new namespace after the VF registration, the VF won't be moved together.
> >
> > To make the behavior more consistent, add a namespace check for
> synthetic
> > NIC's NETDEV_REGISTER event (generated during its move), and move the VF
> > if it is not in the same namespace.
> >
> > Cc: stable@vger.kernel.org
> > Fixes: c0a41b887ce6 ("hv_netvsc: move VF to same namespace as netvsc
> device")
> > Suggested-by: Stephen Hemminger <stephen@networkplumber.org>
> > Signed-off-by: Haiyang Zhang <haiyangz@microsoft.com>
> > ---
> > v2: Move my fix to synthetic NIC's NETDEV_REGISTER event as suggested by
> Stephen.
> >
> > ---
> >  drivers/net/hyperv/netvsc_drv.c | 29 +++++++++++++++++++++++++++++
> >  1 file changed, 29 insertions(+)
> >
> > diff --git a/drivers/net/hyperv/netvsc_drv.c
> b/drivers/net/hyperv/netvsc_drv.c
> > index 153b97f8ec0d..54e98356ee93 100644
> > --- a/drivers/net/hyperv/netvsc_drv.c
> > +++ b/drivers/net/hyperv/netvsc_drv.c
> > @@ -2798,6 +2798,30 @@ static struct  hv_driver netvsc_drv = {
> >  	},
> >  };
> >
> > +/* Set VF's namespace same as the synthetic NIC */
> > +static void netvsc_event_set_vf_ns(struct net_device *ndev)
> > +{
> > +	struct net_device_context *ndev_ctx = netdev_priv(ndev);
> > +	struct net_device *vf_netdev = rtnl_dereference(ndev_ctx-
> >vf_netdev);
> > +	int ret;
> 
> In Networking code it is preferred to arrange local variables in reverse
> xmas tree order - longest line to shortest.
> 
> I believe that could be achieved as follows (completely untested!):
> 
> 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
> 	struct net_device *vf_netdev;
> 	int ret;
> 
> 	vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
> 	if (!vf_netdev)
> 		return;
> 
> With that addressed please feel free to add:
> 
> Reviewed-by: Simon Horman <horms@kernel.org>

Sure, I will update them to RCT order.

Thanks,
- Haiyang

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

end of thread, other threads:[~2024-10-17 16:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-16 15:43 [PATCH net,v2] hv_netvsc: Fix VF namespace also in synthetic NIC NETDEV_REGISTER event Haiyang Zhang
2024-10-17 15:44 ` Simon Horman
2024-10-17 16:06   ` Haiyang Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).