Skip to contentSkip to conversation

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

PATCH In review Jason Wang 2 participants 6 messages Jun 17, 2022 – Jun 27, 2022

Subsystems (4)
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 · 6 lines
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#1 lore raw
Quoted 31 lines · Jason Wang

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 +5 -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();

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 3674

Quoted 10 lines
drivers/net/virtio_net.c +2 -0
 		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");

Looks good but then don't we have the same issue when removing the device?

Actually I looked at virtnet_remove and I see

        unregister_netdev(vi->dev);

        net_failover_destroy(vi->failover);

        remove_vq_common(vi); <- this will reset the device

a window here?

Really, I think what we had originally was a better idea - instead of dropping interrupts they were delayed and when driver is ready to accept them it just enables them. We just need to make sure driver does not wait for interrupts before enabling them.

And I suspect we need to make this opt-in on a per driver basis.

Quoted 2 lines
Signature · 1 line
2.25.1
author
#2 lore raw
Quoted 48 lines · Michael S. Tsirkin
Quoted 31 lines · Jason Wang

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 +5 -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();

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 3674

Quoted 10 lines
drivers/net/virtio_net.c +2 -0
              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");

Looks good but then don't we have the same issue when removing the device?

Quoted 8 lines

Actually I looked at virtnet_remove and I see

        unregister_netdev(vi->dev);

        net_failover_destroy(vi->failover);

        remove_vq_common(vi); <- this will reset the device

a window here?

Probably. For safety, we probably need to reset before unregistering.

Quoted 5 lines

Really, I think what we had originally was a better idea - instead of dropping interrupts they were delayed and when driver is ready to accept them it just enables them.

The problem is that it works only on some specific setup:

- doesn't work on shared IRQ
- doesn't work on some specific driver e.g virtio-blk

Quoted 5 lines

We just need to make sure driver does not wait for interrupts before enabling them.

And I suspect we need to make this opt-in on a per driver basis.

Exactly.

Thanks

Quoted 6 lines
Quoted 5 lines

--
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw
Quoted 52 lines · Jason Wang
Quoted 51 lines · Michael S. Tsirkin
Quoted 31 lines · Jason Wang

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 +5 -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();

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 3674

Quoted 10 lines
drivers/net/virtio_net.c +2 -0
              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");

Looks good but then don't we have the same issue when removing the device?

Actually I looked at virtnet_remove and I see

        unregister_netdev(vi->dev);
Quoted 7 lines
Quoted 5 lines

net_failover_destroy(vi->failover);

remove_vq_common(vi); <- this will reset the device

a window here?

Probably. For safety, we probably need to reset before unregistering.

careful not to create new races, let's analyse this one to be sure first.

Quoted 10 lines
Quoted 5 lines

Really, I think what we had originally was a better idea - instead of dropping interrupts they were delayed and when driver is ready to accept them it just enables them.

The problem is that it works only on some specific setup:

- doesn't work on shared IRQ
- doesn't work on some specific driver e.g virtio-blk

can some core irq work fix that?

Quoted 16 lines
Quoted 5 lines

We just need to make sure driver does not wait for interrupts before enabling them.

And I suspect we need to make this opt-in on a per driver basis.

Exactly.

Thanks

Quoted 6 lines
Quoted 5 lines

--
2.25.1

author
#4 lore raw
Quoted 53 lines · Michael S. Tsirkin
Quoted 52 lines · Jason Wang
Quoted 51 lines · Michael S. Tsirkin
Quoted 31 lines · Jason Wang

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 +5 -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();

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 3674

Quoted 10 lines
drivers/net/virtio_net.c +2 -0
               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");

Looks good but then don't we have the same issue when removing the device?

Actually I looked at virtnet_remove and I see

         unregister_netdev(vi->dev);

         net_failover_destroy(vi->failover);
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

remove_vq_common(vi); <- this will reset the device

a window here?

Probably. For safety, we probably need to reset before unregistering.

careful not to create new races, let's analyse this one to be sure first.

Yes, if we do that, there could be an infinite wait in ctrl commands.

So we are probably fine here since unregister_netdev() will make sure (otherwise it should be a bug of unregister_netdev()):

1) NAPI is disabled (and synced) so no new NAPI could be enabled by the callbacks
2) TX is disabled (and synced) so the qdisc could not be scheduled even if skb_xmit_done() is called between the window

Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

Really, I think what we had originally was a better idea - instead of dropping interrupts they were delayed and when driver is ready to accept them it just enables them.

The problem is that it works only on some specific setup:

- doesn't work on shared IRQ
- doesn't work on some specific driver e.g virtio-blk

can some core irq work fix that?

Not sure. At least for the shared IRQ part, there's no way to disable a specific handler currently. More below.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

We just need to make sure driver does not wait for interrupts before enabling them.

This only help for the case:

1) the virtio_device_ready() is called after subsystem initialization/registration
2) the driver use rx interrupt

It doesn't solve the race between subsystem registration/initialization and virtio_device_ready() or the case when the virtio_device_ready() needs to be called before subsystem registration.

Thanks

Quoted 11 lines
Quoted 11 lines
Quoted 3 lines

And I suspect we need to make this opt-in on a per driver basis.

Exactly.

Thanks

Quoted 4 lines
Quoted 4 lines

--
2.25.1

author
#5 lore raw
In reply to Jason Wang
Quoted 6 lines · Jason Wang

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.

Quoted 6 lines

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

Ok, I think we're fine with this. So I will repost against -net.

If we spot issues with unregistering, we can use a separate patch for that.

Thanks

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