Skip to contentSkip to conversation

virtio/vhost: Add checks for uninitialized VQs

PATCH Superseded Nicholas A. Bellinger 2 participants 17 messages Mar 29, 2013 – Apr 1, 2013

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 virtio: add API to check that ring is setup

In reply to Nicholas A. Bellinger

From: Michael S. Tsirkin <[email protected]>

virtio scsi makes it legal to only setup a subset of rings. The only way to detect the ring is setup seems to be to check whether PA was written to. Add API to do this, and teach code to use it instead of checking hardware queue size.

(nab: use .vring.desc instead of .vring.pa)

---
 hw/virtio.c |    5 +++++
 hw/virtio.h |    1 +
 2 files changed, 6 insertions(+), 0 deletions(-)
hw/virtio.c +5 -0
index 26fbc79..65ba253 100644
@@ -651,6 +651,11 @@ int virtio_queue_get_num(VirtIODevice *vdev, int n)
     return vdev->vq[n].vring.num;
 }
 
+bool virtio_queue_valid(VirtIODevice *vdev, int n)
+{
+    return vdev->vq[n].vring.num && vdev->vq[n].vring.desc;
+}
+
 int virtio_queue_get_id(VirtQueue *vq)
 {
     VirtIODevice *vdev = vq->vdev;
hw/virtio.h +1 -0
index fdbe931..3086798 100644
@@ -227,6 +227,7 @@ void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
 int virtio_queue_get_num(VirtIODevice *vdev, int n);
+bool virtio_queue_valid(VirtIODevice *vdev, int n);
 void virtio_queue_notify(VirtIODevice *vdev, int n);
 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
Signature · 1 line
1.7.2.5
#3 lore raw

PATCH 2/3 virtio-pci: Add virtio_queue_valid checks ahead of virtio_queue_get_num

In reply to Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

This patch adds a number of virtio_queue_valid() checks to virtio-pci ahead of virtio_queue_get_num() usage in order to skip operation upon the detection of an uninitialized VQ.

There is one exception in virtio_ioport_read():VIRTIO_PCI_QUEUE_NUM, where virtio_queue_get_num() may still be called without a valid vdev->vq[n].vring.desc physical address.

Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 hw/virtio-pci.c |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
hw/virtio-pci.c +27 -0
index 0d67b84..231ca0c 100644
@@ -211,6 +211,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -225,6 +228,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
 
 assign_error:
     while (--n >= 0) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -246,6 +252,9 @@ static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -546,6 +555,9 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
     MSIMessage msg;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -593,6 +605,9 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
     int queue_no;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -665,6 +680,9 @@ static int kvm_virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
     int ret, queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -695,6 +713,9 @@ static void kvm_virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
     int queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -717,6 +738,9 @@ static void kvm_virtio_pci_vector_poll(PCIDevice *dev,
     VirtQueue *vq;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -790,6 +814,9 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
     }
 
     for (n = 0; n < nvqs; n++) {
+        if (!virtio_queue_valid(vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, n)) {
             break;
         }
Signature · 1 line
1.7.2.5
#8 lore raw

PATCH 3/3 vhost: Check+skip uninitialized VQs in vhost_verify_ring_mappings

In reply to Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

With the virtio_queue_valid() checks in place to skip uninitialized VQs within virtio-pci code, go ahead and skip the same uninitialized VQs during vhost_verify_ring_mappings().

Note this patch does not prevent vhost_virtqueue_start() from executing by checking virtio_queue_valid(), as other logic during seabios -> virtio-scsi LLD guest hand-off appears to depend upon this execution.

Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 hw/vhost.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/vhost.c +3 -0
index 4d6aee3..3a71aee 100644
@@ -314,6 +314,9 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev,
         hwaddr l;
         void *p;
 
+        if (!vq->ring_phys || !vq->ring_size) {
+            continue;
+        }
         if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
             continue;
         }
Signature · 1 line
1.7.2.5
#0 lore raw

cover virtio/vhost: Add checks for uninitialized VQs

From: Nicholas Bellinger <[email protected]>

Hi folks,

This series adds a virtio_queue_valid() for use by virtio-pci code in order to prevent opreations upon uninitialized VQs, that is currently expected to occur during seabios setup of virtio-scsi.

This also includes a vhost specific check for uninitialized VQs in vhost_verify_ring_mappings() to avoid this same case.

Please review.

--nab

Michael S. Tsirkin (1):
virtio: add API to check that ring is setup

Nicholas Bellinger (2):
virtio-pci: Add virtio_queue_valid checks ahead of
virtio_queue_get_num
vhost: Check+skip uninitialized VQs in vhost_verify_ring_mappings

 hw/vhost.c      |    3 +++
 hw/virtio-pci.c |   27 +++++++++++++++++++++++++++
 hw/virtio.c     |    5 +++++
 hw/virtio.h     |    1 +
 4 files changed, 36 insertions(+), 0 deletions(-)
Signature · 1 line
1.7.2.5
#2 lore raw

PATCH 1/3 virtio: add API to check that ring is setup

From: Michael S. Tsirkin <[email protected]>

virtio scsi makes it legal to only setup a subset of rings. The only way to detect the ring is setup seems to be to check whether PA was written to. Add API to do this, and teach code to use it instead of checking hardware queue size.

(nab: use .vring.desc instead of .vring.pa)

---
 hw/virtio.c |    5 +++++
 hw/virtio.h |    1 +
 2 files changed, 6 insertions(+), 0 deletions(-)
hw/virtio.c +5 -0
index 26fbc79..65ba253 100644
@@ -651,6 +651,11 @@ int virtio_queue_get_num(VirtIODevice *vdev, int n)
     return vdev->vq[n].vring.num;
 }
 
+bool virtio_queue_valid(VirtIODevice *vdev, int n)
+{
+    return vdev->vq[n].vring.num && vdev->vq[n].vring.desc;
+}
+
 int virtio_queue_get_id(VirtQueue *vq)
 {
     VirtIODevice *vdev = vq->vdev;
hw/virtio.h +1 -0
index fdbe931..3086798 100644
@@ -227,6 +227,7 @@ void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
 int virtio_queue_get_num(VirtIODevice *vdev, int n);
+bool virtio_queue_valid(VirtIODevice *vdev, int n);
 void virtio_queue_notify(VirtIODevice *vdev, int n);
 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
Signature · 1 line
1.7.2.5
#9 lore raw

PATCH 3/3 vhost: Check+skip uninitialized VQs in vhost_verify_ring_mappings

In reply to Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

With the virtio_queue_valid() checks in place to skip uninitialized VQs within virtio-pci code, go ahead and skip the same uninitialized VQs during vhost_verify_ring_mappings().

Note this patch does not prevent vhost_virtqueue_start() from executing by checking virtio_queue_valid(), as other logic during seabios -> virtio-scsi LLD guest hand-off appears to depend upon this execution.

Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 hw/vhost.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/vhost.c +3 -0
index 4d6aee3..3a71aee 100644
@@ -314,6 +314,9 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev,
         hwaddr l;
         void *p;
 
+        if (!vq->ring_phys || !vq->ring_size) {
+            continue;
+        }
         if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
             continue;
         }
Signature · 1 line
1.7.2.5
#4 lore raw

Re: [PATCH 2/3] virtio-pci: Add virtio_queue_valid checks ahead of virtio_queue_get_num

In reply to Nicholas A. Bellinger
Quoted 6 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

This patch adds a number of virtio_queue_valid() checks to virtio-pci ahead of virtio_queue_get_num() usage in order to skip operation upon the detection of an uninitialized VQ.

Quoted 8 lines

There is one exception in virtio_ioport_read():VIRTIO_PCI_QUEUE_NUM, where virtio_queue_get_num() may still be called without a valid vdev->vq[n].vring.desc physical address.

Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>

Makes sense. Minor nit: virtio_queue_valid calls virtio_queue_get_num internally, so we can drop it everywhere we know queue is valid.

Quoted 100 lines
---
 hw/virtio-pci.c |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
hw/virtio-pci.c +27 -0
index 0d67b84..231ca0c 100644
@@ -211,6 +211,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -225,6 +228,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
 
 assign_error:
     while (--n >= 0) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -246,6 +252,9 @@ static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -546,6 +555,9 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
     MSIMessage msg;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -593,6 +605,9 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
     int queue_no;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -665,6 +680,9 @@ static int kvm_virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
     int ret, queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -695,6 +713,9 @@ static void kvm_virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
     int queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -717,6 +738,9 @@ static void kvm_virtio_pci_vector_poll(PCIDevice *dev,
     VirtQueue *vq;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -790,6 +814,9 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
     }
 
     for (n = 0; n < nvqs; n++) {
+        if (!virtio_queue_valid(vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, n)) {
             break;
         }
Signature · 1 line
1.7.2.5
#7 lore raw

Re: [PATCH 2/3] virtio-pci: Add virtio_queue_valid checks ahead of virtio_queue_get_num

In reply to Nicholas A. Bellinger
Quoted 6 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

This patch adds a number of virtio_queue_valid() checks to virtio-pci ahead of virtio_queue_get_num() usage in order to skip operation upon the detection of an uninitialized VQ.

Quoted 8 lines

There is one exception in virtio_ioport_read():VIRTIO_PCI_QUEUE_NUM, where virtio_queue_get_num() may still be called without a valid vdev->vq[n].vring.desc physical address.

Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>

Makes sense. Minor nit: virtio_queue_valid calls virtio_queue_get_num internally, so we can drop it everywhere we know queue is valid.

Quoted 100 lines
---
 hw/virtio-pci.c |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
hw/virtio-pci.c +27 -0
index 0d67b84..231ca0c 100644
@@ -211,6 +211,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -225,6 +228,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
 
 assign_error:
     while (--n >= 0) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -246,6 +252,9 @@ static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -546,6 +555,9 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
     MSIMessage msg;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -593,6 +605,9 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
     int queue_no;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -665,6 +680,9 @@ static int kvm_virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
     int ret, queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -695,6 +713,9 @@ static void kvm_virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
     int queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -717,6 +738,9 @@ static void kvm_virtio_pci_vector_poll(PCIDevice *dev,
     VirtQueue *vq;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -790,6 +814,9 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
     }
 
     for (n = 0; n < nvqs; n++) {
+        if (!virtio_queue_valid(vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, n)) {
             break;
         }
Signature · 1 line
1.7.2.5
#10 lore raw

Re: [PATCH 3/3] vhost: Check+skip uninitialized VQs in vhost_verify_ring_mappings

In reply to Nicholas A. Bellinger
Quoted 9 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

With the virtio_queue_valid() checks in place to skip uninitialized VQs within virtio-pci code, go ahead and skip the same uninitialized VQs during vhost_verify_ring_mappings().

Note this patch does not prevent vhost_virtqueue_start() from executing by checking virtio_queue_valid(), as other logic during seabios -> virtio-scsi LLD guest hand-off appears to depend upon this execution.

Weird.
cpu_physical_memory_map only succeeds for PA==0 by chance, we really should not depend on this.
So the right thing really should be to skip vhost_virtqueue_start IMHO, maybe add an explicit valid flag in vhost_virtqueue
so vhost_verify_ring_mappings can check it.
What exactly does it do that is needed?

Quoted 25 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 hw/vhost.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/vhost.c +3 -0
index 4d6aee3..3a71aee 100644
@@ -314,6 +314,9 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev,
         hwaddr l;
         void *p;
 
+        if (!vq->ring_phys || !vq->ring_size) {
+            continue;
+        }
         if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
             continue;
         }
Signature · 1 line
1.7.2.5
#13 lore raw
In reply to Nicholas A. Bellinger
Quoted 8 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

Hi folks,

This series adds a virtio_queue_valid() for use by virtio-pci code in order to prevent opreations upon uninitialized VQs, that is currently expected to occur during seabios setup of virtio-scsi.

Quoted 6 lines

This also includes a vhost specific check for uninitialized VQs in vhost_verify_ring_mappings() to avoid this same case.

Please review.

--nab

Okay, and does this fix the failures in vhost_verify_ring_mappings that you've observed?

Quoted 16 lines

Michael S. Tsirkin (1):
virtio: add API to check that ring is setup

Nicholas Bellinger (2):
virtio-pci: Add virtio_queue_valid checks ahead of
virtio_queue_get_num
vhost: Check+skip uninitialized VQs in vhost_verify_ring_mappings

 hw/vhost.c      |    3 +++
 hw/virtio-pci.c |   27 +++++++++++++++++++++++++++
 hw/virtio.c     |    5 +++++
 hw/virtio.h     |    1 +
 4 files changed, 36 insertions(+), 0 deletions(-)
Signature · 1 line
1.7.2.5
#16 lore raw
In reply to Nicholas A. Bellinger
Quoted 8 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

Hi folks,

This series adds a virtio_queue_valid() for use by virtio-pci code in order to prevent opreations upon uninitialized VQs, that is currently expected to occur during seabios setup of virtio-scsi.

Quoted 6 lines

This also includes a vhost specific check for uninitialized VQs in vhost_verify_ring_mappings() to avoid this same case.

Please review.

--nab

Okay, and does this fix the failures in vhost_verify_ring_mappings that you've observed?

Quoted 16 lines

Michael S. Tsirkin (1):
virtio: add API to check that ring is setup

Nicholas Bellinger (2):
virtio-pci: Add virtio_queue_valid checks ahead of
virtio_queue_get_num
vhost: Check+skip uninitialized VQs in vhost_verify_ring_mappings

 hw/vhost.c      |    3 +++
 hw/virtio-pci.c |   27 +++++++++++++++++++++++++++
 hw/virtio.c     |    5 +++++
 hw/virtio.h     |    1 +
 4 files changed, 36 insertions(+), 0 deletions(-)
Signature · 1 line
1.7.2.5
#5 lore raw

Re: [PATCH 2/3] virtio-pci: Add virtio_queue_valid checks ahead of virtio_queue_get_num

In reply to Michael S. Tsirkin
Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

This patch adds a number of virtio_queue_valid() checks to virtio-pci ahead of virtio_queue_get_num() usage in order to skip operation upon the detection of an uninitialized VQ.

There is one exception in virtio_ioport_read():VIRTIO_PCI_QUEUE_NUM, where virtio_queue_get_num() may still be called without a valid vdev->vq[n].vring.desc physical address.

Quoted 8 lines
Quoted 4 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>

Makes sense. Minor nit: virtio_queue_valid calls virtio_queue_get_num internally, so we can drop it everywhere we know queue is valid.

Yes, of course. This includes every location in virtio-pci.c below..

Including for patch-v2.

Quoted 104 lines
Quoted 100 lines
---
 hw/virtio-pci.c |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
hw/virtio-pci.c +27 -0
index 0d67b84..231ca0c 100644
@@ -211,6 +211,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -225,6 +228,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
 
 assign_error:
     while (--n >= 0) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -246,6 +252,9 @@ static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -546,6 +555,9 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
     MSIMessage msg;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -593,6 +605,9 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
     int queue_no;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -665,6 +680,9 @@ static int kvm_virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
     int ret, queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -695,6 +713,9 @@ static void kvm_virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
     int queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -717,6 +738,9 @@ static void kvm_virtio_pci_vector_poll(PCIDevice *dev,
     VirtQueue *vq;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -790,6 +814,9 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
     }
 
     for (n = 0; n < nvqs; n++) {
+        if (!virtio_queue_valid(vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, n)) {
             break;
         }
Signature · 1 line
1.7.2.5

--
To unsubscribe from this list: send the line "unsubscribe target-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Re: [PATCH 2/3] virtio-pci: Add virtio_queue_valid checks ahead of virtio_queue_get_num

In reply to Michael S. Tsirkin
Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

This patch adds a number of virtio_queue_valid() checks to virtio-pci ahead of virtio_queue_get_num() usage in order to skip operation upon the detection of an uninitialized VQ.

There is one exception in virtio_ioport_read():VIRTIO_PCI_QUEUE_NUM, where virtio_queue_get_num() may still be called without a valid vdev->vq[n].vring.desc physical address.

Quoted 8 lines
Quoted 4 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>

Makes sense. Minor nit: virtio_queue_valid calls virtio_queue_get_num internally, so we can drop it everywhere we know queue is valid.

Yes, of course. This includes every location in virtio-pci.c below..

Including for patch-v2.

Quoted 104 lines
Quoted 100 lines
---
 hw/virtio-pci.c |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
hw/virtio-pci.c +27 -0
index 0d67b84..231ca0c 100644
@@ -211,6 +211,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -225,6 +228,9 @@ static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
 
 assign_error:
     while (--n >= 0) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -246,6 +252,9 @@ static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
     }
 
     for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
+        if (!virtio_queue_valid(proxy->vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(proxy->vdev, n)) {
             continue;
         }
@@ -546,6 +555,9 @@ static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
     MSIMessage msg;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -593,6 +605,9 @@ static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
     int queue_no;
 
     for (queue_no = 0; queue_no < nvqs; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -665,6 +680,9 @@ static int kvm_virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
     int ret, queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -695,6 +713,9 @@ static void kvm_virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
     int queue_no;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -717,6 +738,9 @@ static void kvm_virtio_pci_vector_poll(PCIDevice *dev,
     VirtQueue *vq;
 
     for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
+        if (!virtio_queue_valid(vdev, queue_no)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, queue_no)) {
             break;
         }
@@ -790,6 +814,9 @@ static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
     }
 
     for (n = 0; n < nvqs; n++) {
+        if (!virtio_queue_valid(vdev, n)) {
+            continue;
+        }
         if (!virtio_queue_get_num(vdev, n)) {
             break;
         }
Signature · 1 line
1.7.2.5

--
To unsubscribe from this list: send the line "unsubscribe target-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#11 lore raw

Re: [PATCH 3/3] vhost: Check+skip uninitialized VQs in vhost_verify_ring_mappings

In reply to Michael S. Tsirkin
Quoted 11 lines · Michael S. Tsirkin
Quoted 9 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

With the virtio_queue_valid() checks in place to skip uninitialized VQs within virtio-pci code, go ahead and skip the same uninitialized VQs during vhost_verify_ring_mappings().

Note this patch does not prevent vhost_virtqueue_start() from executing by checking virtio_queue_valid(), as other logic during seabios -> virtio-scsi LLD guest hand-off appears to depend upon this execution.

Quoted 8 lines

Weird.
cpu_physical_memory_map only succeeds for PA==0 by chance, we really should not depend on this.
So the right thing really should be to skip vhost_virtqueue_start IMHO, maybe add an explicit valid flag in vhost_virtqueue
so vhost_verify_ring_mappings can check it.
What exactly does it do that is needed?

So the issue with virtio_queue_valid() preventing vhost_virtqueue_start() execution in the original patch was that vhost_virtqueue_stop() was missing a matching virtio_queue_valid() call, which ended up triggering a bad ram pointer during subsequent cpu_physical_memory_unmap() calls to non-existent virtio queue memory..

With the matching virtio_queue_valid() call in place preventing vhost_virtqueue_stop() when vhost_virtqueue_start() is skipped for an uninitialized VQ, a explicit valid flag should not be necessary.

--nab

Quoted 29 lines
Quoted 25 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 hw/vhost.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/vhost.c +3 -0
index 4d6aee3..3a71aee 100644
@@ -314,6 +314,9 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev,
         hwaddr l;
         void *p;
 
+        if (!vq->ring_phys || !vq->ring_size) {
+            continue;
+        }
         if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
             continue;
         }
Signature · 1 line
1.7.2.5

--
To unsubscribe from this list: send the line "unsubscribe target-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#12 lore raw

Re: [PATCH 3/3] vhost: Check+skip uninitialized VQs in vhost_verify_ring_mappings

In reply to Michael S. Tsirkin
Quoted 11 lines · Michael S. Tsirkin
Quoted 9 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

With the virtio_queue_valid() checks in place to skip uninitialized VQs within virtio-pci code, go ahead and skip the same uninitialized VQs during vhost_verify_ring_mappings().

Note this patch does not prevent vhost_virtqueue_start() from executing by checking virtio_queue_valid(), as other logic during seabios -> virtio-scsi LLD guest hand-off appears to depend upon this execution.

Quoted 8 lines

Weird.
cpu_physical_memory_map only succeeds for PA==0 by chance, we really should not depend on this.
So the right thing really should be to skip vhost_virtqueue_start IMHO, maybe add an explicit valid flag in vhost_virtqueue
so vhost_verify_ring_mappings can check it.
What exactly does it do that is needed?

So the issue with virtio_queue_valid() preventing vhost_virtqueue_start() execution in the original patch was that vhost_virtqueue_stop() was missing a matching virtio_queue_valid() call, which ended up triggering a bad ram pointer during subsequent cpu_physical_memory_unmap() calls to non-existent virtio queue memory..

With the matching virtio_queue_valid() call in place preventing vhost_virtqueue_stop() when vhost_virtqueue_start() is skipped for an uninitialized VQ, a explicit valid flag should not be necessary.

--nab

Quoted 29 lines
Quoted 25 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Asias He <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 hw/vhost.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
hw/vhost.c +3 -0
index 4d6aee3..3a71aee 100644
@@ -314,6 +314,9 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev,
         hwaddr l;
         void *p;
 
+        if (!vq->ring_phys || !vq->ring_size) {
+            continue;
+        }
         if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
             continue;
         }
Signature · 1 line
1.7.2.5

--
To unsubscribe from this list: send the line "unsubscribe target-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#14 lore raw
In reply to Michael S. Tsirkin
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

Hi folks,

This series adds a virtio_queue_valid() for use by virtio-pci code in order to prevent opreations upon uninitialized VQs, that is currently expected to occur during seabios setup of virtio-scsi.

This also includes a vhost specific check for uninitialized VQs in vhost_verify_ring_mappings() to avoid this same case.

Quoted 7 lines
Quoted 3 lines

Please review.

--nab

Okay, and does this fix the failures in vhost_verify_ring_mappings that you've observed?

Unfortunately, no. I've done some more digging and will follow up with additional details on the original thread shortly..

--nab

#15 lore raw
In reply to Michael S. Tsirkin
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Nicholas A. Bellinger

From: Nicholas Bellinger <[email protected]>

Hi folks,

This series adds a virtio_queue_valid() for use by virtio-pci code in order to prevent opreations upon uninitialized VQs, that is currently expected to occur during seabios setup of virtio-scsi.

This also includes a vhost specific check for uninitialized VQs in vhost_verify_ring_mappings() to avoid this same case.

Quoted 7 lines
Quoted 3 lines

Please review.

--nab

Okay, and does this fix the failures in vhost_verify_ring_mappings that you've observed?

Unfortunately, no. I've done some more digging and will follow up with additional details on the original thread shortly..

--nab