Skip to content

Commit 9cae43d

Browse files
Shradha Guptadavem330
authored andcommitted
hv_netvsc: Register VF in netvsc_probe if NET_DEVICE_REGISTER missed
If hv_netvsc driver is unloaded and reloaded, the NET_DEVICE_REGISTER handler cannot perform VF register successfully as the register call is received before netvsc_probe is finished. This is because we register register_netdevice_notifier() very early( even before vmbus_driver_register()). To fix this, we try to register each such matching VF( if it is visible as a netdevice) at the end of netvsc_probe. Cc: stable@vger.kernel.org Fixes: 8552085 ("hv_netvsc: Fix race of register_netdevice_notifier and VF register") Suggested-by: Dexuan Cui <decui@microsoft.com> Signed-off-by: Shradha Gupta <shradhagupta@linux.microsoft.com> Reviewed-by: Haiyang Zhang <haiyangz@microsoft.com> Reviewed-by: Dexuan Cui <decui@microsoft.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b09b58e commit 9cae43d

File tree

1 file changed

+62
-20
lines changed

1 file changed

+62
-20
lines changed

drivers/net/hyperv/netvsc_drv.c

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
#define LINKCHANGE_INT (2 * HZ)
4343
#define VF_TAKEOVER_INT (HZ / 10)
4444

45+
/* Macros to define the context of vf registration */
46+
#define VF_REG_IN_PROBE 1
47+
#define VF_REG_IN_NOTIFIER 2
48+
4549
static unsigned int ring_size __ro_after_init = 128;
4650
module_param(ring_size, uint, 0444);
4751
MODULE_PARM_DESC(ring_size, "Ring buffer size (# of 4K pages)");
@@ -2185,7 +2189,7 @@ static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
21852189
}
21862190

21872191
static int netvsc_vf_join(struct net_device *vf_netdev,
2188-
struct net_device *ndev)
2192+
struct net_device *ndev, int context)
21892193
{
21902194
struct net_device_context *ndev_ctx = netdev_priv(ndev);
21912195
int ret;
@@ -2208,7 +2212,11 @@ static int netvsc_vf_join(struct net_device *vf_netdev,
22082212
goto upper_link_failed;
22092213
}
22102214

2211-
schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
2215+
/* If this registration is called from probe context vf_takeover
2216+
* is taken care of later in probe itself.
2217+
*/
2218+
if (context == VF_REG_IN_NOTIFIER)
2219+
schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
22122220

22132221
call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
22142222

@@ -2346,7 +2354,7 @@ static int netvsc_prepare_bonding(struct net_device *vf_netdev)
23462354
return NOTIFY_DONE;
23472355
}
23482356

2349-
static int netvsc_register_vf(struct net_device *vf_netdev)
2357+
static int netvsc_register_vf(struct net_device *vf_netdev, int context)
23502358
{
23512359
struct net_device_context *net_device_ctx;
23522360
struct netvsc_device *netvsc_dev;
@@ -2386,7 +2394,7 @@ static int netvsc_register_vf(struct net_device *vf_netdev)
23862394

23872395
netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
23882396

2389-
if (netvsc_vf_join(vf_netdev, ndev) != 0)
2397+
if (netvsc_vf_join(vf_netdev, ndev, context) != 0)
23902398
return NOTIFY_DONE;
23912399

23922400
dev_hold(vf_netdev);
@@ -2484,10 +2492,31 @@ static int netvsc_unregister_vf(struct net_device *vf_netdev)
24842492
return NOTIFY_OK;
24852493
}
24862494

2495+
static int check_dev_is_matching_vf(struct net_device *event_ndev)
2496+
{
2497+
/* Skip NetVSC interfaces */
2498+
if (event_ndev->netdev_ops == &device_ops)
2499+
return -ENODEV;
2500+
2501+
/* Avoid non-Ethernet type devices */
2502+
if (event_ndev->type != ARPHRD_ETHER)
2503+
return -ENODEV;
2504+
2505+
/* Avoid Vlan dev with same MAC registering as VF */
2506+
if (is_vlan_dev(event_ndev))
2507+
return -ENODEV;
2508+
2509+
/* Avoid Bonding master dev with same MAC registering as VF */
2510+
if (netif_is_bond_master(event_ndev))
2511+
return -ENODEV;
2512+
2513+
return 0;
2514+
}
2515+
24872516
static int netvsc_probe(struct hv_device *dev,
24882517
const struct hv_vmbus_device_id *dev_id)
24892518
{
2490-
struct net_device *net = NULL;
2519+
struct net_device *net = NULL, *vf_netdev;
24912520
struct net_device_context *net_device_ctx;
24922521
struct netvsc_device_info *device_info = NULL;
24932522
struct netvsc_device *nvdev;
@@ -2599,6 +2628,30 @@ static int netvsc_probe(struct hv_device *dev,
25992628
}
26002629

26012630
list_add(&net_device_ctx->list, &netvsc_dev_list);
2631+
2632+
/* When the hv_netvsc driver is unloaded and reloaded, the
2633+
* NET_DEVICE_REGISTER for the vf device is replayed before probe
2634+
* is complete. This is because register_netdevice_notifier() gets
2635+
* registered before vmbus_driver_register() so that callback func
2636+
* is set before probe and we don't miss events like NETDEV_POST_INIT
2637+
* So, in this section we try to register the matching vf device that
2638+
* is present as a netdevice, knowing that its register call is not
2639+
* processed in the netvsc_netdev_notifier(as probing is progress and
2640+
* get_netvsc_byslot fails).
2641+
*/
2642+
for_each_netdev(dev_net(net), vf_netdev) {
2643+
ret = check_dev_is_matching_vf(vf_netdev);
2644+
if (ret != 0)
2645+
continue;
2646+
2647+
if (net != get_netvsc_byslot(vf_netdev))
2648+
continue;
2649+
2650+
netvsc_prepare_bonding(vf_netdev);
2651+
netvsc_register_vf(vf_netdev, VF_REG_IN_PROBE);
2652+
__netvsc_vf_setup(net, vf_netdev);
2653+
break;
2654+
}
26022655
rtnl_unlock();
26032656

26042657
netvsc_devinfo_put(device_info);
@@ -2754,28 +2807,17 @@ static int netvsc_netdev_event(struct notifier_block *this,
27542807
unsigned long event, void *ptr)
27552808
{
27562809
struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2810+
int ret = 0;
27572811

2758-
/* Skip our own events */
2759-
if (event_dev->netdev_ops == &device_ops)
2760-
return NOTIFY_DONE;
2761-
2762-
/* Avoid non-Ethernet type devices */
2763-
if (event_dev->type != ARPHRD_ETHER)
2764-
return NOTIFY_DONE;
2765-
2766-
/* Avoid Vlan dev with same MAC registering as VF */
2767-
if (is_vlan_dev(event_dev))
2768-
return NOTIFY_DONE;
2769-
2770-
/* Avoid Bonding master dev with same MAC registering as VF */
2771-
if (netif_is_bond_master(event_dev))
2812+
ret = check_dev_is_matching_vf(event_dev);
2813+
if (ret != 0)
27722814
return NOTIFY_DONE;
27732815

27742816
switch (event) {
27752817
case NETDEV_POST_INIT:
27762818
return netvsc_prepare_bonding(event_dev);
27772819
case NETDEV_REGISTER:
2778-
return netvsc_register_vf(event_dev);
2820+
return netvsc_register_vf(event_dev, VF_REG_IN_NOTIFIER);
27792821
case NETDEV_UNREGISTER:
27802822
return netvsc_unregister_vf(event_dev);
27812823
case NETDEV_UP:

0 commit comments

Comments
 (0)