Skip to contentSkip to conversation

clean up callback when del virtqueue

PATCH In review liujunjie 2 participants 4 messages Sep 8, 2018 – Sep 14, 2018

lore
Share & tools
lore

liujunjie

author
#0 lore raw

PATCH 1/1 clean up callback when del virtqueue

Before, we did not clear callback like handle_output when delete the virtqueue which may result be segmentfault.
The scene is as follows:
1. Start a vm with multiqueue vhost-net,
2. then we write VIRTIO_PCI_GUEST_FEATURES in PCI configuration to triger multiqueue disable in this vm which will delete the virtqueue. In this step, the tx_bh is deleted but the callback virtio_net_handle_tx_bh still exist.
3. Finally, we write VIRTIO_PCI_QUEUE_NOTIFY in PCI configuration to notify the deleted virtqueue. In this way, virtio_net_handle_tx_bh will be called and qemu will be crashed.

Although the way described above is uncommon, we had better reinforce it.

---
 hw/net/virtio-net.c | 4 +++-
 hw/virtio/virtio.c  | 3 +++
 2 files changed, 6 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +3 -1
index f154756..9bb20e3 100644
@@ -1467,7 +1467,9 @@ static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
         return;
     }
     virtio_queue_set_notification(vq, 0);
-    qemu_bh_schedule(q->tx_bh);
+    if (q->tx_bh) {
+        qemu_bh_schedule(q->tx_bh);
+    }
 }
 
 static void virtio_net_tx_timer(void *opaque)
hw/virtio/virtio.c +3 -0
index d4e4d98..7577518 100644
@@ -1604,6 +1604,9 @@ void virtio_del_queue(VirtIODevice *vdev, int n)
 
     vdev->vq[n].vring.num = 0;
     vdev->vq[n].vring.num_default = 0;
+    vdev->vq[n].vring.align = 0;
+    vdev->vq[n].handle_output = NULL;
+    vdev->vq[n].handle_aio_output = NULL;
 }
 
 static void virtio_set_isr(VirtIODevice *vdev, int value)
Signature · 1 line
1.8.3.1
#1 lore raw

ping

Quoted 62 lines

-----Original Message-----
From: liujunjie (A)
Sent: Saturday, September 08, 2018 9:05 PM
To: [email protected]; [email protected]
Cc: wangxin (U) <[email protected]>; Zhoujian (jay) <[email protected]>; Gonglei (Arei) <[email protected]>; Huangweidong (C) <[email protected]>; [email protected]; liujunjie (A) <[email protected]>
Subject: [PATCH] clean up callback when del virtqueue

Before, we did not clear callback like handle_output when delete the virtqueue which may result be segmentfault.
The scene is as follows:
1. Start a vm with multiqueue vhost-net, 2. then we write VIRTIO_PCI_GUEST_FEATURES in PCI configuration to triger multiqueue disable in this vm which will delete the virtqueue.
In this step, the tx_bh is deleted but the callback virtio_net_handle_tx_bh still exist.
3. Finally, we write VIRTIO_PCI_QUEUE_NOTIFY in PCI configuration to notify the deleted virtqueue. In this way, virtio_net_handle_tx_bh will be called and qemu will be crashed.

Although the way described above is uncommon, we had better reinforce it.

---
 hw/net/virtio-net.c | 4 +++-
 hw/virtio/virtio.c  | 3 +++
 2 files changed, 6 insertions(+), 1 deletion(-)
hw/net/virtio-net.c index f154756..9bb20e3 renamed from hw/net/virtio-net.c +0 -0

100644

hw/net/virtio-net.c +3 -1
@@ -1467,7 +1467,9 @@ static void virtio_net_handle_tx_bh(VirtIODevice
 *vdev, VirtQueue *vq)
         return;
     }
     virtio_queue_set_notification(vq, 0);
-    qemu_bh_schedule(q->tx_bh);
+    if (q->tx_bh) {
+        qemu_bh_schedule(q->tx_bh);
+    }
 }
 

static void virtio_net_tx_timer(void *opaque) diff --git a/hw/virtio/virtio.c
b/hw/virtio/virtio.c index d4e4d98..7577518 100644

hw/virtio/virtio.c +3 -1
@@ -1604,6 +1604,9 @@ void virtio_del_queue(VirtIODevice *vdev, int n)
 
     vdev->vq[n].vring.num = 0;
     vdev->vq[n].vring.num_default = 0;
+    vdev->vq[n].vring.align = 0;
+    vdev->vq[n].handle_output = NULL;
+    vdev->vq[n].handle_aio_output = NULL;
 }
 
 static void virtio_set_isr(VirtIODevice *vdev, int value)
--

1.8.3.1

#2 lore raw
In reply to liujunjie
Quoted 11 lines · liujunjie

Before, we did not clear callback like handle_output when delete the virtqueue which may result be segmentfault.
The scene is as follows:
1. Start a vm with multiqueue vhost-net,
2. then we write VIRTIO_PCI_GUEST_FEATURES in PCI configuration to triger multiqueue disable in this vm which will delete the virtqueue. In this step, the tx_bh is deleted but the callback virtio_net_handle_tx_bh still exist.
3. Finally, we write VIRTIO_PCI_QUEUE_NOTIFY in PCI configuration to notify the deleted virtqueue. In this way, virtio_net_handle_tx_bh will be called and qemu will be crashed.

Good catch.

Quoted 14 lines

Although the way described above is uncommon, we had better reinforce it.

---
  hw/net/virtio-net.c | 4 +++-
  hw/virtio/virtio.c  | 3 +++
  2 files changed, 6 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +0 -0
index f154756..9bb20e3 100644

hw/net/virtio-net.c static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) · line 1472

Quoted 7 lines
hw/net/virtio-net.c +3 -1
@@ -1467,7 +1467,9 @@ static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
          return;
      }
      virtio_queue_set_notification(vq, 0);
-    qemu_bh_schedule(q->tx_bh);
+    if (q->tx_bh) {
+        qemu_bh_schedule(q->tx_bh);
+    }

I believe this is not necessary since handle_output is NULL now?

hw/virtio/virtio.c void virtio_del_queue(VirtIODevice *vdev, int n) · line 1607

Quoted 12 lines
hw/net/virtio-net.c +0 -0
  }
 
  static void virtio_net_tx_timer(void *opaque)
hw/virtio/virtio.c +1 -0
index d4e4d98..7577518 100644
@@ -1604,6 +1604,9 @@ void virtio_del_queue(VirtIODevice *vdev, int n)
 
      vdev->vq[n].vring.num = 0;
      vdev->vq[n].vring.num_default = 0;
+    vdev->vq[n].vring.align = 0;

Is this related or fix for another bug?

Thanks

Quoted 5 lines
hw/virtio/virtio.c +2 -0
+    vdev->vq[n].handle_output = NULL;
+    vdev->vq[n].handle_aio_output = NULL;
  }
 
  static void virtio_set_isr(VirtIODevice *vdev, int value)
#3 lore raw
Quoted 51 lines

-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Friday, September 14, 2018 8:45 PM
To: liujunjie (A) <[email protected]>; [email protected]
Cc: Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; [email protected]; Gonglei (Arei) <[email protected]>; Zhoujian (jay) <[email protected]>
Subject: Re: [Qemu-devel] [PATCH] clean up callback when del virtqueue

Quoted 11 lines · liujunjie

Before, we did not clear callback like handle_output when delete the virtqueue which may result be segmentfault.
The scene is as follows:
1. Start a vm with multiqueue vhost-net, 2. then we write VIRTIO_PCI_GUEST_FEATURES in PCI configuration to triger multiqueue disable in this vm which will delete the virtqueue.
In this step, the tx_bh is deleted but the callback virtio_net_handle_tx_bh still exist.
3. Finally, we write VIRTIO_PCI_QUEUE_NOTIFY in PCI configuration to notify the deleted virtqueue. In this way, virtio_net_handle_tx_bh will be called and qemu will be crashed.

Good catch.

hw/net/virtio-net.c static void virtio_net_handle_tx_bh(VirtIODevice

Quoted 14 lines

Although the way described above is uncommon, we had better reinforce it.

---
  hw/net/virtio-net.c | 4 +++-
  hw/virtio/virtio.c  | 3 +++
  2 files changed, 6 insertions(+), 1 deletion(-)
hw/net/virtio-net.c index renamed from hw/net/virtio-net.c +0 -0

f154756..9bb20e3 100644

hw/net/virtio-net.c +0 -0
@@ -1467,7 +1467,9 @@ static void virtio_net_handle_tx_bh(VirtIODevice

*vdev, VirtQueue *vq)

hw/net/virtio-net.c static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) · line 1472

Quoted 7 lines
hw/net/virtio-net.c +3 -1
          return;
      }
      virtio_queue_set_notification(vq, 0);
-    qemu_bh_schedule(q->tx_bh);
+    if (q->tx_bh) {
+        qemu_bh_schedule(q->tx_bh);
+    }
Quoted 1 line

I believe this is not necessary since handle_output is NULL now?

Yes, it is not necessary.

Quoted 10 lines
Quoted 10 lines
hw/net/virtio-net.c +0 -0
  }
 
  static void virtio_net_tx_timer(void *opaque) diff --git

a/hw/virtio/virtio.c b/hw/virtio/virtio.c index d4e4d98..7577518 100644

hw/virtio/virtio.c +0 -0
@@ -1604,6 +1604,9 @@ void virtio_del_queue(VirtIODevice *vdev, int n)
 
Quoted 5 lines

hw/virtio/virtio.c void virtio_del_queue(VirtIODevice *vdev, int n) · line 1607

Quoted 3 lines
hw/virtio/virtio.c +1 -0
      vdev->vq[n].vring.num = 0;
      vdev->vq[n].vring.num_default = 0;
+    vdev->vq[n].vring.align = 0;

Is this related or fix for another bug?

It's not related for another bug. So it is also unnecessary to add this, too.
The reason why add it is only trying to correspond to the function <virtio_add_queue> in the same file.

Quoted 8 lines

Thanks

Quoted 5 lines
hw/virtio/virtio.c +2 -0
+    vdev->vq[n].handle_output = NULL;
+    vdev->vq[n].handle_aio_output = NULL;
  }
 
  static void virtio_set_isr(VirtIODevice *vdev, int value)