Skip to contentSkip to conversation

net: fix abort and busy loop when the tap device is deleted

PATCH In review Denis V. Lunev 2 participants 4 messages Nov 6 – Nov 17 Networking

lore
Share & tools
lore
#0 lore raw

cover net: fix abort and busy loop when the tap device is deleted

Management can delete the host tap device of a running VM while QEMU still holds its fds, e.g. OpenStack unplugs the VIF of an instance it is deleting before the domain is gone. QEMU should carry on with a dead link. Instead:

- a multiqueue virtio-net NIC aborts on an assert as soon as the guest changes the number of queue pairs or resets the device (patch 1);
- every tap netdev makes the main loop spin at full CPU in tap_send() (patch 2).

The two patches are independent. Each adds a case to tests/qtest/virtio-net-test.c that fails without its fix. Creating the tap needs CAP_NET_ADMIN, so both cases are skipped when not run as root.

Cc "Michael S. Tsirkin" <[email protected]>
Cc Jason Wang <[email protected]>
Cc Vladimir Sementsov-Ogievskiy <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Laurent Vivier <[email protected]>
Cc Paolo Bonzini <[email protected]>

Denis V. Lunev (2):
virtio-net: fix abort in virtio_net_set_queue_pairs() on a deleted tap net/tap: fix busy loop in tap_send() after the tap device is deleted

 hw/net/virtio-net.c           |  25 +++--
 net/tap.c                     |   8 ++
 tests/qtest/virtio-net-test.c | 174 ++++++++++++++++++++++++++++++++++
 3 files changed, 200 insertions(+), 7 deletions(-)

base-commit: 76fdb3373c9a27b3f50ecc9e012cbdeb90af7151

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 virtio-net: fix abort in virtio_net_set_queue_pairs() on a deleted tap

From: Denis V. Lunev <[email protected]>

Once the tap device behind a multiqueue netdev is deleted on the host, the queue fds QEMU holds are no longer bound to any device and TUNSETQUEUE fails with EINVAL, for attach and detach alike. Management can delete the device under a running VM, e.g. OpenStack unplugs the VIF of an instance it is deleting. The next VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET from the guest then kills QEMU:

could not enable queue virtio_net_set_queue_pairs: Assertion `!r' failed.

A guest reset aborts the same way through set_features(): clearing the features detaches the extra queues and the detach fails too.

Commit a1708203b583 ("net/tap-linux.c: avoid abort when setting invalid fd") removed the earlier abort on the same path in tap_set_vnet_hdr_len(); this is the next one.

tap_fd_enable() and tap_fd_disable() already report the error. Walk the remaining queues anyway, return the failure and let the callers decide:
- the control command restores the previous number of queue pairs and returns VIRTIO_NET_ERR, so the device matches the guest, which keeps its old setting on error. The rollback cannot fail in turn: tap tracks which queues are attached and skips the ioctl for a queue whose attach or detach already failed;
- post-load fails the migration, so the source keeps running instead of the destination aborting;
- set_features() has no way to report it and carries on with a dead link, as a single queue NIC on a deleted tap already does.

The test attaches its own copy of the second queue fd before the guest asks for two queue pairs, so the attach from QEMU fails with EINVAL on a live device. Creating the tap needs CAP_NET_ADMIN, so the test is skipped without it.

Fixes fed699f9ca6a ("virtio-net: multiqueue support")
Cc "Michael S. Tsirkin" <[email protected]>
Cc Jason Wang <[email protected]>
Cc Vladimir Sementsov-Ogievskiy <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Laurent Vivier <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 hw/net/virtio-net.c           |  25 +++++---
 tests/qtest/virtio-net-test.c | 104 ++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+), 7 deletions(-)
hw/net/virtio-net.c +18 -7
index 54e42cfec9..82e3c22ca3 100644
@@ -766,24 +766,27 @@ static int peer_detach(VirtIONet *n, int index)
     return tap_disable(nc->peer);
 }
 
-static void virtio_net_set_queue_pairs(VirtIONet *n)
+static int virtio_net_set_queue_pairs(VirtIONet *n)
 {
     int i;
     int r;
+    int ret = 0;
 
     if (n->nic->peer_deleted) {
-        return;
+        return 0;
     }
 
     for (i = 0; i < n->max_queue_pairs; i++) {
         if (i < n->curr_queue_pairs) {
             r = peer_attach(n, i);
-            assert(!r);
         } else {
             r = peer_detach(n, i);
-            assert(!r);
+        }
+        if (r < 0) {
+            ret = r;
         }
     }
+    return ret;
 }
 
 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
@@ -1499,7 +1502,7 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
                                 struct iovec *iov, unsigned int iov_cnt)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
-    uint16_t queue_pairs;
+    uint16_t queue_pairs, old_queue_pairs;
     NetClientState *nc = qemu_get_queue(n->nic);
 
     virtio_net_disable_rss(n);
@@ -1532,6 +1535,7 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
         return VIRTIO_NET_ERR;
     }
 
+    old_queue_pairs = n->curr_queue_pairs;
     n->curr_queue_pairs = queue_pairs;
     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
         /*
@@ -1543,7 +1547,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
     /* stop the backend before changing the number of queue_pairs to avoid handling a
      * disabled queue */
     virtio_net_set_status(vdev, vdev->status);
-    virtio_net_set_queue_pairs(n);
+    if (virtio_net_set_queue_pairs(n) < 0) {
+        n->curr_queue_pairs = old_queue_pairs;
+        virtio_net_set_status(vdev, vdev->status);
+        virtio_net_set_queue_pairs(n);
+        return VIRTIO_NET_ERR;
+    }
 
     return VIRTIO_NET_OK;
 }
@@ -3298,7 +3307,9 @@ static int virtio_net_post_load_device(void *opaque, int version_id)
      */
     n->saved_guest_offloads = n->curr_guest_offloads;
 
-    virtio_net_set_queue_pairs(n);
+    if (virtio_net_set_queue_pairs(n) < 0) {
+        return -EINVAL;
+    }
 
     /* Find the first multicast entry in the saved MAC filter */
     for (i = 0; i < n->mac_table.in_use; i++) {
tests/qtest/virtio-net-test.c +104 -0

122 lines not on this page · raw message

base-commit: 76fdb3373c9a27b3f50ecc9e012cbdeb90af7151

Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 net/tap: fix busy loop in tap_send() after the tap device is deleted

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

When the tap device is deleted on the host while QEMU still holds its fd, poll() reports EPOLLERR on the fd every time and read() fails with EBADFD. tap_send() just breaks out of its loop with the handler still registered, the main loop wakes up again at once and QEMU spins at full CPU until the VM is destroyed. Every tap netdev is affected, single queue or multiqueue. With vhost-net the spin starts once vhost stops, e.g. on guest reboot, since vhost removes the handler while it runs.

Stop read polling on EBADFD and report it. Other read errors are left
alone: tun fails a read with EINVAL for a single packet it cannot convert, e.g. an unsupported GSO type, and the next read succeeds. EBADFD only exists on Linux and Solaris, hence the #ifdef. Anything that turns polling back on later, a vhost stop or the delivery of a queued packet, hits EBADFD once and stops it again.

The test deletes a single queue tap through netlink under a running guest and checks that the CPU time of QEMU stays low afterwards. Deleting the device needs CAP_NET_ADMIN, so the test is skipped without it.

Cc Jason Wang <[email protected]>
Cc Vladimir Sementsov-Ogievskiy <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Laurent Vivier <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 net/tap.c                     |  8 ++++
 tests/qtest/virtio-net-test.c | 70 +++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
net/tap.c +8 -0
index 9bce0d088f..8274aebda6 100644
@@ -265,6 +265,14 @@ static void tap_send(void *opaque)
         size_t min_pktsz = sizeof(min_pkt);
 
         size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
+#ifdef EBADFD
+        if (size < 0 && errno == EBADFD) {
+            error_report("tap: device is gone, stop polling: %s",
+                         strerror(errno));
+            tap_read_poll(s, false);
+            break;
+        }
+#endif
         if (size <= 0) {
             break;
         }
tests/qtest/virtio-net-test.c +70 -0

88 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw
In reply to Denis V. Lunev
Quoted 28 lines · Denis V. Lunev

This email originated from an IP that might not be authorized by the domain it was sent from. Do not click links or open attachments unless it is an email you expected to receive. Management can delete the host tap device of a running VM while QEMU
still holds its fds, e.g. OpenStack unplugs the VIF of an instance it is deleting before the domain is gone. QEMU should carry on with a dead
link. Instead:

- a multiqueue virtio-net NIC aborts on an assert as soon as the guest changes the number of queue pairs or resets the device (patch 1);
- every tap netdev makes the main loop spin at full CPU in tap_send() (patch 2).

The two patches are independent. Each adds a case to tests/qtest/virtio-net-test.c that fails without its fix. Creating the tap needs CAP_NET_ADMIN, so both cases are skipped when not run as root.

Cc "Michael S. Tsirkin" <[email protected]>
Cc Jason Wang <[email protected]>
Cc Vladimir Sementsov-Ogievskiy <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Laurent Vivier <[email protected]>
Cc Paolo Bonzini <[email protected]>

Denis V. Lunev (2):
virtio-net: fix abort in virtio_net_set_queue_pairs() on a deleted tap net/tap: fix busy loop in tap_send() after the tap device is deleted

Quoted 7 lines
 hw/net/virtio-net.c           |  25 +++--
 net/tap.c                     |   8 ++
 tests/qtest/virtio-net-test.c | 174 ++++++++++++++++++++++++++++++++++
 3 files changed, 200 insertions(+), 7 deletions(-)

base-commit: 76fdb3373c9a27b3f50ecc9e012cbdeb90af7151

ping