Skip to content

Commit 50c0ada

Browse files
jasowangmstsirkin
authored andcommitted
virtio-net: fix race between ndo_open() and virtio_device_ready()
We currently call virtio_device_ready() after netdev registration. Since ndo_open() can be called immediately after register_netdev, this means there exists a race between ndo_open() and virtio_device_ready(): the driver may start to use the device before DRIVER_OK which violates the spec. Fix this by switching to use register_netdevice() and protect the virtio_device_ready() with rtnl_lock() to make sure ndo_open() can only be called after virtio_device_ready(). Fixes: 4baf1e3 ("virtio_net: enable VQs early") Signed-off-by: Jason Wang <[email protected]> Message-Id: <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent c346dae commit 50c0ada

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

drivers/net/virtio_net.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3655,14 +3655,20 @@ static int virtnet_probe(struct virtio_device *vdev)
36553655
if (vi->has_rss || vi->has_rss_hash_report)
36563656
virtnet_init_default_rss(vi);
36573657

3658-
err = register_netdev(dev);
3658+
/* serialize netdev register + virtio_device_ready() with ndo_open() */
3659+
rtnl_lock();
3660+
3661+
err = register_netdevice(dev);
36593662
if (err) {
36603663
pr_debug("virtio_net: registering device failed\n");
3664+
rtnl_unlock();
36613665
goto free_failover;
36623666
}
36633667

36643668
virtio_device_ready(vdev);
36653669

3670+
rtnl_unlock();
3671+
36663672
err = virtnet_cpu_notif_add(vi);
36673673
if (err) {
36683674
pr_debug("virtio_net: registering cpu notifier failed\n");

0 commit comments

Comments
 (0)