virtio-net: fix race between ndo_open() and virtio_device_ready()
Subsystems (4)
PATCH 1/1 virtio-net: fix race between ndo_open() and virtio_device_ready()
We used to call virtio_device_ready() after netdev registration. This cause a race between ndo_open() and virtio_device_ready(): if ndo_open() is called before virtio_device_ready(), the driver may start to use the device before DRIVER_OK which violates the spec.
Fixing 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().
--- drivers/net/virtio_net.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +7 -1
@@ -3655,14 +3655,20 @@ static int virtnet_probe(struct virtio_device *vdev)if (vi->has_rss || vi->has_rss_hash_report)virtnet_init_default_rss (vi); - err =register_netdev (dev); +/* serialize netdev register + virtio_device_ready() with ndo_open() */ +rtnl_lock (); + + err =register_netdevice (dev);if (err) {pr_debug ("virtio_net: registering device failed\n" ); +rtnl_unlock (); goto free_failover; }virtio_device_ready (vdev); +rtnl_unlock (); + err =virtnet_cpu_notif_add (vi);if (err) {pr_debug ("virtio_net: registering cpu notifier failed\n" );
Signature · 1 line
2.25.1
Quoted 1 line · Jason Wang
We used to call
virtio_device_ready()after netdev registration.
s/used to call/currently call/
Quoted 2 lines
This
cause
s/This cause/Since ndo_open can be called immediately after register_netdev, this means there exists/
Quoted 5 lines
a race between
ndo_open()andvirtio_device_ready(): ifndo_open()is called beforevirtio_device_ready(), the driver may start to use the device beforeDRIVER_OKwhich violates the spec.Fixing
s/Fixing/Fix/
Quoted 5 lines
this by switching to use
register_netdevice()and protect thevirtio_device_ready()withrtnl_lock()to make surendo_open()can only be called aftervirtio_device_ready().Fixes 4baf1e33d0842 ("virtio_net: enable VQs early")
it's an unusual use of Fixes - the patch in question does not introduce the problem, it just does not fix it completely. But OK I guess.
Quoted 1 line
Signed-off-by Jason Wang <[email protected]>
With commit log changes:
Quoted 32 lines
--- drivers/net/virtio_net.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/net/virtio_net.c +7
-1@@ -3655,14 +3655,20 @@ static int virtnet_probe(struct virtio_device *vdev)if (vi->has_rss || vi->has_rss_hash_report)virtnet_init_default_rss (vi); - err =register_netdev (dev); +/* serialize netdev register + virtio_device_ready() with ndo_open() */ +rtnl_lock (); + + err =register_netdevice (dev);if (err) {pr_debug ("virtio_net: registering device failed\n" ); +rtnl_unlock (); goto free_failover; }virtio_device_ready (vdev); +rtnl_unlock (); + err =virtnet_cpu_notif_add (vi);if (err) {pr_debug ("virtio_net: registering cpu notifier failed\n" );Signature · 1 line
2.25.1
Quoted 20 lines · Michael S. Tsirkin
Quoted 1 line · Jason Wang
We used to call
virtio_device_ready()after netdev registration.s/used to call/currently call/
Quoted 2 lines
This
causes/This cause/Since
ndo_opencan be called immediately afterregister_netdev, this means there exists/Quoted 5 lines
a race between
ndo_open()andvirtio_device_ready(): ifndo_open()is called beforevirtio_device_ready(), the driver may start to use the device beforeDRIVER_OKwhich violates the spec.Fixing
s/Fixing/Fix/
Quoted 8 lines
Quoted 5 lines
this by switching to use
register_netdevice()and protect thevirtio_device_ready()withrtnl_lock()to make surendo_open()can only be called aftervirtio_device_ready().Fixes 4baf1e33d0842 ("virtio_net: enable VQs early")it's an unusual use of Fixes - the patch in question does not introduce the problem, it just does not fix it completely.
Yes, but I couldn't find a better commit.
Quoted 5 lines
But OK I guess.
Quoted 1 line
Signed-off-by Jason Wang <[email protected]>With commit log changes:
Will post a new version with the above fixed.
Thanks
Quoted 36 lines
Quoted 32 lines
--- drivers/net/virtio_net.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/net/virtio_net.c +7
-2@@ -3655,14 +3655,20 @@ static int virtnet_probe(struct virtio_device *vdev)if (vi->has_rss || vi->has_rss_hash_report)virtnet_init_default_rss (vi); - err =register_netdev (dev); +/* serialize netdev register + virtio_device_ready() with ndo_open() */ +rtnl_lock (); + + err =register_netdevice (dev);if (err) {pr_debug ("virtio_net: registering device failed\n" ); +rtnl_unlock (); goto free_failover; }virtio_device_ready (vdev); +rtnl_unlock (); + err =virtnet_cpu_notif_add (vi);if (err) {pr_debug ("virtio_net: registering cpu notifier failed\n" ); --2.25.1