Skip to contentSkip to conversation

virtio-mmio: fixes to QueueNum, QueueNumMax

PATCH In review Peter Maydell 2 participants 7 messages Jul 26, 2013 – Aug 11, 2013

lore
Share & tools
lore
#0 lore raw

v2 cover virtio-mmio: fixes to QueueNum, QueueNumMax

These patches fix a couple of bugs in virtio-mmio's handling of the registers that deal with the queue size:

* as mst points out, letting the guest flip a queue between "exists" and "doesn't exist" is a bad idea
* QueueNumMax wasn't reading the correct value for nonexistent queues

This doesn't include any change to the behaviour of queuesize on reset (discussed in other thread); the current behaviour is not a problem for well-behaved guests, and safe in the face of badly-behaved guests, and currently improving the reset behaviour is blocked by an unrelated bug.

v1->v2: changes as per mst review:
* avoid explicit "== 0" comparisons
* avoid unnecessary parens round comparison ops
* do the "don't flip between existent and nonexistent" check with "!!num != !!oldnum" (and add a comment noting why we're doing this check)

Peter Maydell (2):
hw/virtio/virtio: Don't allow guests to add/remove queues hw/virtio/virtio-mmio: Make QueueNumMax read 0 for unavailable queues

 hw/virtio/virtio-mmio.c |    3 +++
 hw/virtio/virtio.c      |   12 +++++++++---
 2 files changed, 12 insertions(+), 3 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v2 1/2 hw/virtio/virtio: Don't allow guests to add/remove queues

A queue size of 0 is used to indicate a nonexistent queue, so don't allow the guest to flip a queue between zero-size and non-zero-size. Don't permit setting of negative queue sizes either.

---
 hw/virtio/virtio.c |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +9 -3
index 09f62c6..60653f7 100644
@@ -673,10 +673,16 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
 
 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
 {
-    if (num <= VIRTQUEUE_MAX_SIZE) {
-        vdev->vq[n].vring.num = num;
-        virtqueue_init(&vdev->vq[n]);
+    /* Don't allow guest to flip queue between existent and
+     * nonexistent states, or to set it to an invalid size.
+     */
+    if (!!num != !!vdev->vq[n].vring.num ||
+        num > VIRTQUEUE_MAX_SIZE ||
+        num < 0) {
+        return;
     }
+    vdev->vq[n].vring.num = num;
+    virtqueue_init(&vdev->vq[n]);
 }
 
 int virtio_queue_get_num(VirtIODevice *vdev, int n)
Signature · 1 line
1.7.9.5
Reviewed-by on v2 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH v2 for-1.6 1/2] hw/virtio/virtio: Don't allow guests to add/remove queues

Quoted 6 lines · Peter Maydell

A queue size of 0 is used to indicate a nonexistent queue, so don't allow the guest to flip a queue between zero-size and non-zero-size. Don't permit setting of negative queue sizes either.

Quoted 30 lines
---
 hw/virtio/virtio.c |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
hw/virtio/virtio.c +9 -3
index 09f62c6..60653f7 100644
@@ -673,10 +673,16 @@ hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
 
 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
 {
-    if (num <= VIRTQUEUE_MAX_SIZE) {
-        vdev->vq[n].vring.num = num;
-        virtqueue_init(&vdev->vq[n]);
+    /* Don't allow guest to flip queue between existent and
+     * nonexistent states, or to set it to an invalid size.
+     */
+    if (!!num != !!vdev->vq[n].vring.num ||
+        num > VIRTQUEUE_MAX_SIZE ||
+        num < 0) {
+        return;
     }
+    vdev->vq[n].vring.num = num;
+    virtqueue_init(&vdev->vq[n]);
 }
 
 int virtio_queue_get_num(VirtIODevice *vdev, int n)
Signature · 1 line
1.7.9.5
#3 lore raw

PATCH v2 2/2 hw/virtio/virtio-mmio: Make QueueNumMax read 0 for unavailable queues

In reply to Peter Maydell

The virtio-mmio spec says that QueueNumMax must read zero for queues which are unavailable; implement this, rather than always returning VIRTQUEUE_MAX_SIZE.

---
 hw/virtio/virtio-mmio.c |    3 +++
 1 file changed, 3 insertions(+)
hw/virtio/virtio-mmio.c +3 -0
index 54d6679..9cf79ce 100644
@@ -151,6 +151,9 @@ static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size)
         }
         return proxy->host_features;
     case VIRTIO_MMIO_QUEUENUMMAX:
+        if (!virtio_queue_get_num(vdev, vdev->queue_sel)) {
+            return 0;
+        }
         return VIRTQUEUE_MAX_SIZE;
     case VIRTIO_MMIO_QUEUEPFN:
         return virtio_queue_get_addr(vdev, vdev->queue_sel)
Signature · 1 line
1.7.9.5
Reviewed-by on v2 2/2
#4 lore raw

Re: [Qemu-devel] [PATCH v2 for-1.6 2/2] hw/virtio/virtio-mmio: Make QueueNumMax read 0 for unavailable queues

Quoted 5 lines · Peter Maydell

The virtio-mmio spec says that QueueNumMax must read zero for queues which are unavailable; implement this, rather than always returning VIRTQUEUE_MAX_SIZE.

Quoted 20 lines
---
 hw/virtio/virtio-mmio.c |    3 +++
 1 file changed, 3 insertions(+)
hw/virtio/virtio-mmio.c +3 -0
index 54d6679..9cf79ce 100644
@@ -151,6 +151,9 @@ static uint64_t virtio_mmio_read(void *opaque, hwaddr offset, unsigned size)
         }
         return proxy->host_features;
     case VIRTIO_MMIO_QUEUENUMMAX:
+        if (!virtio_queue_get_num(vdev, vdev->queue_sel)) {
+            return 0;
+        }
         return VIRTQUEUE_MAX_SIZE;
     case VIRTIO_MMIO_QUEUEPFN:
         return virtio_queue_get_addr(vdev, vdev->queue_sel)
Signature · 1 line
1.7.9.5
#5 lore raw
In reply to Peter Maydell
Quoted 21 lines · Peter Maydell

These patches fix a couple of bugs in virtio-mmio's handling of the registers that deal with the queue size:

* as mst points out, letting the guest flip a queue between "exists" and "doesn't exist" is a bad idea
* QueueNumMax wasn't reading the correct value for nonexistent queues

This doesn't include any change to the behaviour of queuesize on reset (discussed in other thread); the current behaviour is not a problem for well-behaved guests, and safe in the face of badly-behaved guests, and currently improving the reset behaviour is blocked by an unrelated bug.

v1->v2: changes as per mst review:
* avoid explicit "== 0" comparisons
* avoid unnecessary parens round comparison ops
* do the "don't flip between existent and nonexistent" check with "!!num != !!oldnum" (and add a comment noting why we're doing this check)

Quoted 3 lines

Peter Maydell (2):
hw/virtio/virtio: Don't allow guests to add/remove queues hw/virtio/virtio-mmio: Make QueueNumMax read 0 for unavailable queues

These didn't make it into 1.6, but in the absence of any review comments I'm putting them into arm-devs for post-1.6.

thanks
-- PMM

#6 lore raw
Quoted 26 lines · Peter Maydell
Quoted 21 lines · Peter Maydell

These patches fix a couple of bugs in virtio-mmio's handling of the registers that deal with the queue size:

* as mst points out, letting the guest flip a queue between "exists" and "doesn't exist" is a bad idea
* QueueNumMax wasn't reading the correct value for nonexistent queues

This doesn't include any change to the behaviour of queuesize on reset (discussed in other thread); the current behaviour is not a problem for well-behaved guests, and safe in the face of badly-behaved guests, and currently improving the reset behaviour is blocked by an unrelated bug.

v1->v2: changes as per mst review:
* avoid explicit "== 0" comparisons
* avoid unnecessary parens round comparison ops
* do the "don't flip between existent and nonexistent" check with "!!num != !!oldnum" (and add a comment noting why we're doing this check)

Quoted 3 lines

Peter Maydell (2):
hw/virtio/virtio: Don't allow guests to add/remove queues hw/virtio/virtio-mmio: Make QueueNumMax read 0 for unavailable queues

Quoted 5 lines

These didn't make it into 1.6, but in the absence of any review comments I'm putting them into arm-devs for post-1.6.

thanks
-- PMM

I'd say these are important bugfixes, should be OK for 1.6 still.