From nobody Fri Dec 19 19:16:46 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 B205514A088; Wed, 13 Mar 2024 16:43:27 +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=1710348207; cv=none; b=QvHmdkUIdwdYFbffQ+iybO8bhDThBntvlVOWvOy2we5U4vLH/W7gMqFaokmOuwRUxtmg5Rri6G6R1Ls+SBpCdVOvoHbdPnSLMiei4CJPmIerjFljzUy+t73KPWgxK/CYpgOXIgE443/eh1U3Zl0CSQE/cZOfZtpsOOqSJfs+Wo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348207; c=relaxed/simple; bh=OMRKw+JfNCMJGVklikilPsgcB18UH1vcqNvrL8kA3uc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=P652mck84vDCE/pBljwUPONXZByRg1ppC0lsLe/gW8eXdx7U+n6iX1gGP12zUSYQAZJIYK6SBQuE/1w+kyBOzgNP3aEJHrySvVvwbsmOBohHkdvklVAlTqj5liHfRYgTO3f40+QvbuwgDAm+20AsjLPxzBcNV/LM35ruVg4iaac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RtVgTFMU; 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="RtVgTFMU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98D12C433C7; Wed, 13 Mar 2024 16:43:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710348207; bh=OMRKw+JfNCMJGVklikilPsgcB18UH1vcqNvrL8kA3uc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RtVgTFMU9BvHRG3tbMrrecW52x6kohQ7657bTDU/w2NELEptTVsYdGVBiDZFX9wdn 9Oo63dwBViaf+Vxnz9NgLVEymDKkd99W6A6Lczjcp9Vemco8lOE5hmS7QXtb8WGtVN 1FhSZ8obGjnhD0Sz1SW3bPDVhqwEzUr9Ml19xXqPN1oxhcbxU+hJTnunuaGpAKxqm9 0oPTrWjfvMkq3RBPW25rkBL6RqCHVXZshyspJlCD4vkI+NtoZKsb+cZY0VBb3XJA0N xvuRgMtxUGbUyqUQF8zpq3k4bEwiOaG7aW+yswbFOkFTsRcv6jBHSpubMyME7ThTtI 1MmQThKXIjwwA== 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.15 55/76] hv_netvsc: Register VF in netvsc_probe if NET_DEVICE_REGISTER missed Date: Wed, 13 Mar 2024 12:42:02 -0400 Message-ID: <20240313164223.615640-56-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240313164223.615640-1-sashal@kernel.org> References: <20240313164223.615640-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.15.152-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.15.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.15.152-rc1 X-KernelTest-Deadline: 2024-03-15T16:42+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 a7d1304466880..804c11cbccff6 100644 --- a/drivers/net/hyperv/netvsc_drv.c +++ b/drivers/net/hyperv/netvsc_drv.c @@ -42,6 +42,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 4K pages)"); @@ -2204,7 +2208,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; @@ -2227,7 +2231,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 @@ -2365,7 +2373,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; @@ -2405,7 +2413,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); @@ -2503,10 +2511,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; @@ -2614,6 +2643,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); @@ -2770,28 +2823,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