Skip to contentSkip to conversation

vhost-net: Do not set features for backend when peer was deleted

PATCH In review Jason Wang 2 participants 5 messages Oct 26, 2010 – Oct 27, 2010

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 vhost-net: Do not set features for backend when peer was deleted

When hot-unplug a virtio nic with vhost-net backend, guest may continue to program the nic even if its peer have been deleted. We can not set features at this time as vhost_net_ack_features() may still try to use the tap related vhost_net structure which have been freed in tap_cleanup(). And setting offload features for a deleted backend is also meaningless in this situation

---
 hw/virtio-net.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/virtio-net.c +3 -0
index 7e1688c..68c8e48 100644
@@ -245,6 +245,9 @@ static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
 {
     VirtIONet *n = to_virtio_net(vdev);
 
+    if (n->nic->peer_deleted)
+        return;
+
     n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
 
     if (n->has_vnet_hdr) {
#1 lore raw
Quoted 8 lines · Jason Wang

When hot-unplug a virtio nic with vhost-net backend, guest may continue to program the nic even if its peer have been deleted. We can not set features at this time as vhost_net_ack_features() may still try to use the tap related vhost_net structure which have been freed in tap_cleanup(). And setting offload features for a deleted backend is also meaningless in this situation

Hmm. Actually, this is not enough.
We really must stop and cleanup vhost net.

Two issues are
1. vhost_net_stop needs virtio device pointer
2. virtio net has a vhost_started flag

Two ways to fix it that I see:
1. add a callback in nic and invoke when peer_deleted is set
2. add vhost_started and virtio device pointers in vhost

Path 1 seems easier ...

MST

Quoted 18 lines
---
 hw/virtio-net.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/virtio-net.c +3 -0
index 7e1688c..68c8e48 100644
@@ -245,6 +245,9 @@ static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
 {
     VirtIONet *n = to_virtio_net(vdev);
 
+    if (n->nic->peer_deleted)
+        return;
+
     n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
 
     if (n->has_vnet_hdr) {
author
#2 lore raw
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang

When hot-unplug a virtio nic with vhost-net backend, guest may continue to program the nic even if its peer have been deleted. We can not set features at this time as vhost_net_ack_features() may still try to use the tap related vhost_net structure which have been freed in tap_cleanup(). And setting offload features for a deleted backend is also meaningless in this situation

Quoted 5 lines
Quoted 1 line

Hmm. Actually, this is not enough.
We really must stop and cleanup vhost net.

But this patch just prevent the guest from programming a nic with its vhost-net backend deleted. The vhost net was still stopped and cleaned when the peer have been removed.

Quoted 31 lines

Two issues are
1. vhost_net_stop needs virtio device pointer
2. virtio net has a vhost_started flag

Two ways to fix it that I see:
1. add a callback in nic and invoke when peer_deleted is set
2. add vhost_started and virtio device pointers in vhost

Path 1 seems easier ...

MST

Quoted 18 lines
---
 hw/virtio-net.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/virtio-net.c +3 -0
index 7e1688c..68c8e48 100644
@@ -245,6 +245,9 @@ static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
 {
     VirtIONet *n = to_virtio_net(vdev);
 
+    if (n->nic->peer_deleted)
+        return;
+
     n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
 
     if (n->has_vnet_hdr) {
#3 lore raw
Quoted 11 lines · Jason Wang
Quoted 10 lines · Michael S. Tsirkin
Quoted 8 lines · Jason Wang

When hot-unplug a virtio nic with vhost-net backend, guest may continue to program the nic even if its peer have been deleted. We can not set features at this time as vhost_net_ack_features() may still try to use the tap related vhost_net structure which have been freed in tap_cleanup(). And setting offload features for a deleted backend is also meaningless in this situation

Quoted 7 lines
Quoted 3 lines

Hmm. Actually, this is not enough.
We really must stop and cleanup vhost net.

But this patch just prevent the guest from programming a nic with its vhost-net backend deleted. The vhost net was still stopped and cleaned when the peer have been removed.

No, I think it was cleanup but not stopped first. E.g. try to migrate at this point and see it crash. I suspect we also leak fds for notifiers and what not.

Quoted 31 lines
Quoted 31 lines

Two issues are
1. vhost_net_stop needs virtio device pointer
2. virtio net has a vhost_started flag

Two ways to fix it that I see:
1. add a callback in nic and invoke when peer_deleted is set
2. add vhost_started and virtio device pointers in vhost

Path 1 seems easier ...

MST

Quoted 18 lines
---
 hw/virtio-net.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/virtio-net.c +3 -0
index 7e1688c..68c8e48 100644
@@ -245,6 +245,9 @@ static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
 {
     VirtIONet *n = to_virtio_net(vdev);
 
+    if (n->nic->peer_deleted)
+        return;
+
     n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
 
     if (n->has_vnet_hdr) {
#4 lore raw
Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · Jason Wang
Quoted 13 lines · Michael S. Tsirkin
Quoted 8 lines · Jason Wang

When hot-unplug a virtio nic with vhost-net backend, guest may continue to program the nic even if its peer have been deleted. We can not set features at this time as vhost_net_ack_features() may still try to use the tap related vhost_net structure which have been freed in tap_cleanup(). And setting offload features for a deleted backend is also meaningless in this situation

Hmm. Actually, this is not enough.
We really must stop and cleanup vhost net.

Quoted 7 lines
Quoted 3 lines

But this patch just prevent the guest from programming a nic with its vhost-net backend deleted. The vhost net was still stopped and cleaned when the peer have been removed.

No, I think it was cleanup but not stopped first. E.g. try to migrate at this point and see it crash. I suspect we also leak fds for notifiers and what not.

Hmm. I missed the fact that we bring the link down, which will stop vhost_net. So no, it's not broken.

But I'd rather we didn't look at peer_deleted and leave this as an implementation detail in net.c: I think the bug is in returning an invalid pointer from get_vhost_net. Posted a patch to fix that.

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines

Two issues are
1. vhost_net_stop needs virtio device pointer
2. virtio net has a vhost_started flag

Two ways to fix it that I see:
1. add a callback in nic and invoke when peer_deleted is set
2. add vhost_started and virtio device pointers in vhost

Path 1 seems easier ...

MST

Quoted 18 lines
---
 hw/virtio-net.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/virtio-net.c +3 -0
index 7e1688c..68c8e48 100644
@@ -245,6 +245,9 @@ static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
 {
     VirtIONet *n = to_virtio_net(vdev);
 
+    if (n->nic->peer_deleted)
+        return;
+
     n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
 
     if (n->has_vnet_hdr) {