From nobody Fri Dec 19 17:35:33 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7D541A16F9; Wed, 13 Mar 2024 17:05:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710349512; cv=none; b=bE9txqNcQrBS8rO71SSeogqTfq9UuTOcPRTBlzcJfJ9knjGQt2FRan7lUPWdRQQ328GngnkkJ0W4pha+NJMuB0VT6AlxGQQBN7c//eJ2N8TavP5YFn23dXK8vj3vLulkL9RkRj6bRJDNKwKM+lcAPCBRrkAlagj6vo2+/z4tFXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710349512; c=relaxed/simple; bh=BH+MxCUCdycXo7ftN770mnp7dSqNEGwhqb5BXmthRYs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KZ11p1Q+xJgR/2prXrdiCmv4/aNn97WIQmthuDmf3gaGxFF0wFKwlIiZPwOFXkjAU54uZSHvYfLffPymJKoVK4DO2+TZWsgcxjiDMVeZELrk29cJEowk2sIZXsM4I+pa6eeUnYDLV6X8OFlPgG0YWsz4KX11GHjCxllP8/bntUE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bhEsDyLf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bhEsDyLf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9CC42C43390; Wed, 13 Mar 2024 17:05:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710349512; bh=BH+MxCUCdycXo7ftN770mnp7dSqNEGwhqb5BXmthRYs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bhEsDyLfS5Kv07v8KiqtRkQeAQkW9fv1KU1Pp9rpjyotAVgrN3rPwsl6is35K1f6z 2cZFLIDDbLL91czV4Ow+TKvXcuceclVgjgXD4RkJ9DFBq93rPmBViKMKI1ziOaKhgd Re7ZI8fe5uSu13wr/s5QmngXZfpF9Uc8lbKDX/VwFz7TMF3TeeOOitjVhwBEufdvdP dqX7D2lsxTHDdYhvwWrz/5GYhE6aWUP5UebcdHyijwWI85sBHD8J5MnsdJIr4dz3Ko ZoP70Znl30Z0r4SqU+L2FH3X8g171dU43XhbUJPgUUFdAkL8mkHDCbiRqM8P6YCz6u zhf8qsa6rixHg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Shradha Gupta , Dexuan Cui , Haiyang Zhang , "David S . Miller" , Sasha Levin Subject: [PATCH 4.19 31/41] hv_netvsc: Register VF in netvsc_probe if NET_DEVICE_REGISTER missed Date: Wed, 13 Mar 2024 13:04:25 -0400 Message-ID: <20240313170435.616724-32-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240313170435.616724-1-sashal@kernel.org> References: <20240313170435.616724-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v4.x/stable-review/patch-4.19.310-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-4.19.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 4.19.310-rc1 X-KernelTest-Deadline: 2024-03-15T17:04+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Shradha Gupta [ Upstream commit 9cae43da9867412f8bd09aee5c8a8dc5e8dc3dc2 ] 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: 85520856466e ("hv_netvsc: Fix race of register_netdevice_notifier an= d VF register") Suggested-by: Dexuan Cui Signed-off-by: Shradha Gupta Reviewed-by: Haiyang Zhang Reviewed-by: Dexuan Cui Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/net/hyperv/netvsc_drv.c | 82 +++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/drivers/net/hyperv/netvsc_drv.c b/drivers/net/hyperv/netvsc_dr= v.c index 2d1c6f10d4e19..e24513e34306c 100644 --- a/drivers/net/hyperv/netvsc_drv.c +++ b/drivers/net/hyperv/netvsc_drv.c @@ -54,6 +54,10 @@ #define LINKCHANGE_INT (2 * HZ) #define VF_TAKEOVER_INT (HZ / 10) =20 +/* Macros to define the context of vf registration */ +#define VF_REG_IN_PROBE 1 +#define VF_REG_IN_NOTIFIER 2 + static unsigned int ring_size __ro_after_init =3D 128; module_param(ring_size, uint, 0444); MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)"); @@ -2025,7 +2029,7 @@ static rx_handler_result_t netvsc_vf_handle_frame(str= uct sk_buff **pskb) } =20 static int netvsc_vf_join(struct net_device *vf_netdev, - struct net_device *ndev) + struct net_device *ndev, int context) { struct net_device_context *ndev_ctx =3D netdev_priv(ndev); int ret; @@ -2048,7 +2052,11 @@ static int netvsc_vf_join(struct net_device *vf_netd= ev, goto upper_link_failed; } =20 - schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT); + /* If this registration is called from probe context vf_takeover + * is taken care of later in probe itself. + */ + if (context =3D=3D VF_REG_IN_NOTIFIER) + schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT); =20 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev); =20 @@ -2186,7 +2194,7 @@ static int netvsc_prepare_bonding(struct net_device *= vf_netdev) return NOTIFY_DONE; } =20 -static int netvsc_register_vf(struct net_device *vf_netdev) +static int netvsc_register_vf(struct net_device *vf_netdev, int context) { struct net_device_context *net_device_ctx; struct netvsc_device *netvsc_dev; @@ -2225,7 +2233,7 @@ static int netvsc_register_vf(struct net_device *vf_n= etdev) =20 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name); =20 - if (netvsc_vf_join(vf_netdev, ndev) !=3D 0) + if (netvsc_vf_join(vf_netdev, ndev, context) !=3D 0) return NOTIFY_DONE; =20 dev_hold(vf_netdev); @@ -2285,10 +2293,31 @@ static int netvsc_unregister_vf(struct net_device *= vf_netdev) return NOTIFY_OK; } =20 +static int check_dev_is_matching_vf(struct net_device *event_ndev) +{ + /* Skip NetVSC interfaces */ + if (event_ndev->netdev_ops =3D=3D &device_ops) + return -ENODEV; + + /* Avoid non-Ethernet type devices */ + if (event_ndev->type !=3D ARPHRD_ETHER) + return -ENODEV; + + /* Avoid Vlan dev with same MAC registering as VF */ + if (is_vlan_dev(event_ndev)) + return -ENODEV; + + /* Avoid Bonding master dev with same MAC registering as VF */ + if (netif_is_bond_master(event_ndev)) + return -ENODEV; + + return 0; +} + static int netvsc_probe(struct hv_device *dev, const struct hv_vmbus_device_id *dev_id) { - struct net_device *net =3D NULL; + struct net_device *net =3D NULL, *vf_netdev; struct net_device_context *net_device_ctx; struct netvsc_device_info *device_info =3D NULL; struct netvsc_device *nvdev; @@ -2391,6 +2420,30 @@ static int netvsc_probe(struct hv_device *dev, } =20 list_add(&net_device_ctx->list, &netvsc_dev_list); + + /* When the hv_netvsc driver is unloaded and reloaded, the + * NET_DEVICE_REGISTER for the vf device is replayed before probe + * is complete. This is because register_netdevice_notifier() gets + * registered before vmbus_driver_register() so that callback func + * is set before probe and we don't miss events like NETDEV_POST_INIT + * So, in this section we try to register the matching vf device that + * is present as a netdevice, knowing that its register call is not + * processed in the netvsc_netdev_notifier(as probing is progress and + * get_netvsc_byslot fails). + */ + for_each_netdev(dev_net(net), vf_netdev) { + ret =3D check_dev_is_matching_vf(vf_netdev); + if (ret !=3D 0) + continue; + + if (net !=3D get_netvsc_byslot(vf_netdev)) + continue; + + netvsc_prepare_bonding(vf_netdev); + netvsc_register_vf(vf_netdev, VF_REG_IN_PROBE); + __netvsc_vf_setup(net, vf_netdev); + break; + } rtnl_unlock(); =20 kfree(device_info); @@ -2483,28 +2536,17 @@ static int netvsc_netdev_event(struct notifier_bloc= k *this, unsigned long event, void *ptr) { struct net_device *event_dev =3D netdev_notifier_info_to_dev(ptr); + int ret =3D 0; =20 - /* Skip our own events */ - if (event_dev->netdev_ops =3D=3D &device_ops) - return NOTIFY_DONE; - - /* Avoid non-Ethernet type devices */ - if (event_dev->type !=3D ARPHRD_ETHER) - return NOTIFY_DONE; - - /* Avoid Vlan dev with same MAC registering as VF */ - if (is_vlan_dev(event_dev)) - return NOTIFY_DONE; - - /* Avoid Bonding master dev with same MAC registering as VF */ - if (netif_is_bond_master(event_dev)) + ret =3D check_dev_is_matching_vf(event_dev); + if (ret !=3D 0) return NOTIFY_DONE; =20 switch (event) { case NETDEV_POST_INIT: return netvsc_prepare_bonding(event_dev); case NETDEV_REGISTER: - return netvsc_register_vf(event_dev); + return netvsc_register_vf(event_dev, VF_REG_IN_NOTIFIER); case NETDEV_UNREGISTER: return netvsc_unregister_vf(event_dev); case NETDEV_UP: --=20 2.43.0