Skip to contentSkip to conversation

virtio-net: fix race between ndo_open() and virtio_device_ready()

PATCH Superseded Jason Wang 2 participants 3 messages Jun 27, 2022

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

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().

Fixes 4baf1e33d0842 ("virtio_net: enable VQs early")
---
 drivers/net/virtio_net.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +7 -1
index db05b5e930be..8a5810bcb839 100644
@@ -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
maintainer Acked-by on v1 1/1
#1 lore raw
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() 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

s/Fixing/Fix/

Quoted 5 lines

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 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

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
index db05b5e930be..8a5810bcb839 100644
@@ -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
author
#2 lore raw
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
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() 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

s/Fixing/Fix/

Quoted 8 lines
Quoted 5 lines

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 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

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
index db05b5e930be..8a5810bcb839 100644
@@ -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