From nobody Fri Dec 19 20:14:21 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 344A0161B54; Wed, 13 Mar 2024 16:47:46 +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=1710348466; cv=none; b=gO6BW76OzZtOWG4Ld7eU0mBa34p2hQe0MN7E4/Wz/ZAiuLwKpm+k0eduv9I1Ww+WcDH27TNEFoXRGrssHbBEN92FgxSduyv8f+CEuO+1KUSPavYeUTXFlTyJL/x4xnoKz5qIwolbfQ4U5MShybX8FXdBfd5Onskd3/Jbj6BJ1gk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348466; c=relaxed/simple; bh=SO10NYY2rFlZKfrtasX2qxmzBJJJrpWGsfzvhZ2o1io=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZJR5ZfP/TO9TYpq0Y4RvPwy5XfyM92jR8dNvWtLeOLvXoxakbsPJNGjs0dCzwMRPMlfqYpkGYnuukwnMS1VrDP0hEChjIrhdCNwthWMS6kzWJYX1yoGzSCnQyh1EPHhGfqAOvnWzMH9+dc7BoNzUpt298w/MIahDlbxVe5Sxlu0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=K+CkS2TP; 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="K+CkS2TP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F3D1BC43399; Wed, 13 Mar 2024 16:47:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710348466; bh=SO10NYY2rFlZKfrtasX2qxmzBJJJrpWGsfzvhZ2o1io=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K+CkS2TPZ9JPHWgHn5BLP/xPgNprVbd51+vQInnCgLeEj7uP8PhAW66evqV4UjHFD UmV92idviCYxNDS4VJ8g1OnOR52Zj9KKrBtOWdPMTN7OVtIE15huj80VYjRuGWIjtY eAErdLdTeQtGhH/E8mi2hyNyPoUQPVMEtCmMqJsEuwpY5fi2D90gZ57/lBvNovQkFd RLutZhGroWRVhT8S2fAv0qp4aQPQQjW3CirhX1ZPcHQHeBSnLxyOjMUdf85e3h2Lq9 9rutn7gnT8Y7uQ7mdaZ7AJJSgjqpusLBbZoEdGmPjMYG6wicZJgYz1+E+xZQxrlWHJ tHgzgf666I+HQ== 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 5.10 57/73] hv_netvsc: Register VF in netvsc_probe if NET_DEVICE_REGISTER missed Date: Wed, 13 Mar 2024 12:46:24 -0400 Message-ID: <20240313164640.616049-58-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240313164640.616049-1-sashal@kernel.org> References: <20240313164640.616049-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/v5.x/stable-review/patch-5.10.213-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.10.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.10.213-rc1 X-KernelTest-Deadline: 2024-03-15T16:46+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 e8efcc6a0b05a..0fc0f9cb3f34b 100644 --- a/drivers/net/hyperv/netvsc_drv.c +++ b/drivers/net/hyperv/netvsc_drv.c @@ -44,6 +44,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)"); @@ -2194,7 +2198,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; @@ -2217,7 +2221,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 @@ -2355,7 +2363,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; @@ -2395,7 +2403,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); @@ -2479,10 +2487,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; @@ -2590,6 +2619,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 netvsc_devinfo_put(device_info); @@ -2746,28 +2799,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