[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v3 08/10] virtio: handle virtqueue_read_next_desc()
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [PATCH v3 08/10] virtio: handle virtqueue_read_next_desc() errors |
Date: |
Tue, 12 Apr 2016 14:25:32 +0100 |
Stop processing the vring if an avail ring index is invalid.
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
hw/virtio/virtio.c | 45 ++++++++++++++++++++++++++++++++-------------
1 file changed, 32 insertions(+), 13 deletions(-)
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 493d6a7..58599cf 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -353,28 +353,33 @@ static unsigned int virtqueue_get_head(VirtQueue *vq,
unsigned int idx)
return head;
}
-static unsigned virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
- hwaddr desc_pa, unsigned int max)
-{
- unsigned int next;
+enum {
+ VIRTQUEUE_READ_DESC_ERROR = -1,
+ VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */
+ VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */
+};
+static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
+ hwaddr desc_pa, unsigned int max,
+ unsigned int *next)
+{
/* If this descriptor says it doesn't chain, we're done. */
if (!(desc->flags & VRING_DESC_F_NEXT)) {
- return max;
+ return VIRTQUEUE_READ_DESC_DONE;
}
/* Check they're not leading us off end of descriptors. */
- next = desc->next;
+ *next = desc->next;
/* Make sure compiler knows to grab that: we don't want it changing! */
smp_wmb();
- if (next >= max) {
- error_report("Desc next is %u", next);
- exit(1);
+ if (*next >= max) {
+ virtio_error(vdev, "Desc next is %u", *next);
+ return VIRTQUEUE_READ_DESC_ERROR;
}
- vring_desc_read(vdev, desc, desc_pa, next);
- return next;
+ vring_desc_read(vdev, desc, desc_pa, *next);
+ return VIRTQUEUE_READ_DESC_MORE;
}
void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
@@ -383,6 +388,7 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int
*in_bytes,
{
unsigned int idx;
unsigned int total_bufs, in_total, out_total;
+ int rc;
idx = vq->last_avail_idx;
@@ -435,7 +441,13 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int
*in_bytes,
if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
goto done;
}
- } while ((i = virtqueue_read_next_desc(vdev, &desc, desc_pa, max)) !=
max);
+
+ rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i);
+ } while (rc == VIRTQUEUE_READ_DESC_MORE);
+
+ if (rc == VIRTQUEUE_READ_DESC_ERROR) {
+ goto err;
+ }
if (!indirect)
total_bufs = num_bufs;
@@ -587,6 +599,7 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
hwaddr addr[VIRTQUEUE_MAX_SIZE];
struct iovec iov[VIRTQUEUE_MAX_SIZE];
VRingDesc desc;
+ int rc;
if (unlikely(vdev->broken)) {
return NULL;
@@ -649,7 +662,13 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
virtio_error(vdev, "Looped descriptor");
goto err_undo_map;
}
- } while ((i = virtqueue_read_next_desc(vdev, &desc, desc_pa, max)) != max);
+
+ rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i);
+ } while (rc == VIRTQUEUE_READ_DESC_MORE);
+
+ if (rc == VIRTQUEUE_READ_DESC_ERROR) {
+ goto err_undo_map;
+ }
/* Now copy what we have collected and mapped */
elem = virtqueue_alloc_element(sz, out_num, in_num);
--
2.5.5
- [Qemu-devel] [PATCH v3 00/10] virtio: avoid exit() when device enters invalid states, Stefan Hajnoczi, 2016/04/12
- [Qemu-devel] [PATCH v3 01/10] virtio: fix stray tab character, Stefan Hajnoczi, 2016/04/12
- [Qemu-devel] [PATCH v3 02/10] include: update virtio_config.h Linux header, Stefan Hajnoczi, 2016/04/12
- [Qemu-devel] [PATCH v3 03/10] virtio: stop virtqueue processing if device is broken, Stefan Hajnoczi, 2016/04/12
- [Qemu-devel] [PATCH v3 04/10] virtio: migrate vdev->broken flag, Stefan Hajnoczi, 2016/04/12
- [Qemu-devel] [PATCH v3 05/10] virtio: handle virtqueue_map_desc() errors, Stefan Hajnoczi, 2016/04/12
- [Qemu-devel] [PATCH v3 06/10] virtio: handle virtqueue_get_avail_bytes() errors, Stefan Hajnoczi, 2016/04/12
- [Qemu-devel] [PATCH v3 07/10] virtio: use unsigned int for virtqueue_get_avail_bytes() index, Stefan Hajnoczi, 2016/04/12
- [Qemu-devel] [PATCH v3 08/10] virtio: handle virtqueue_read_next_desc() errors,
Stefan Hajnoczi <=
- [Qemu-devel] [PATCH v3 09/10] virtio: handle virtqueue_num_heads() errors, Stefan Hajnoczi, 2016/04/12
- [Qemu-devel] [PATCH v3 10/10] virtio: handle virtqueue_get_head() errors, Stefan Hajnoczi, 2016/04/12