Skip to contentSkip to conversation

virtio migration load path

PATCH In review Dr. David Alan Gilbert (git) 3 participants 10 messages Jul 16, 2018 – Jul 17, 2018

lore
Share & tools
lore
#0 lore raw

cover virtio migration load path

From: "Dr. David Alan Gilbert" <[email protected]>

One day I'll figure out how to convert the virtio load path and qemu_get_virtqeueue_element into vmstate, but until then clean up some of the error paths.

Dave

Dr. David Alan Gilbert (4):
scsi/migration: Allow bus load request to fail
virtio: Check qemu_get_virtqueue_element returns virtio-scsi/migration: Allow load_request to fail
virtio: qemu_get_virtqueue_element fail rather than assert

 hw/block/virtio-blk.c       |  4 ++++
 hw/char/virtio-serial-bus.c |  4 ++++
 hw/scsi/scsi-bus.c          |  5 +++++
 hw/scsi/virtio-scsi.c       | 19 ++++++++++++++++---
 hw/virtio/virtio.c          | 13 ++++++-------
 5 files changed, 35 insertions(+), 10 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/4 scsi/migration: Allow bus load request to fail

From: "Dr. David Alan Gilbert" <[email protected]>

Allow the load_request method on a bus to fail and error the migration cleanly.

---
 hw/scsi/scsi-bus.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/scsi/scsi-bus.c +5 -0
index 5905f6bf29..34a29c2ffd 100644
@@ -1618,6 +1618,11 @@ static int get_scsi_requests(QEMUFile *f, void *pv, size_t size,
         req->retry = (sbyte == 1);
         if (bus->info->load_request) {
             req->hba_private = bus->info->load_request(f, req);
+
+            if (!req->hba_private) {
+                scsi_req_unref(req);
+                return -1;
+            }
         }
         if (req->ops->load_request) {
             req->ops->load_request(f, req);
Signature · 1 line
2.17.1
#6 lore raw

PATCH 3/4 virtio-scsi/migration: Allow load_request to fail

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Rather than asserting, check values and return NULL on failure.

---
 hw/scsi/virtio-scsi.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
hw/scsi/virtio-scsi.c +12 -3
index 6301af76ad..3441bfe6d7 100644
@@ -204,7 +204,11 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
     uint32_t n;
 
     qemu_get_be32s(f, &n);
-    assert(n < vs->conf.num_queues);
+    if (n >= vs->conf.num_queues) {
+        error_report("%s: Bad queue number (%d vs %d)",
+                     __func__, n, vs->conf.num_queues);
+        return NULL;
+    }
     req = qemu_get_virtqueue_element(vdev, f,
                                      sizeof(VirtIOSCSIReq) + vs->cdb_size);
     if (!req) {
@@ -216,13 +220,18 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
     if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
                               sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) {
         error_report("invalid SCSI request migration data");
-        exit(1);
+        return NULL;
     }
 
     scsi_req_ref(sreq);
     req->sreq = sreq;
     if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
-        assert(req->sreq->cmd.mode == req->mode);
+        if (req->sreq->cmd.mode != req->mode) {
+            error_report("%s: Bad mode (%d vs %d)",
+                         __func__, req->sreq->cmd.mode, req->mode);
+            scsi_req_unref(sreq);
+            return NULL;
+        }
     }
     return req;
 }
Signature · 1 line
2.17.1
#8 lore raw

PATCH 4/4 virtio: qemu_get_virtqueue_element fail rather than assert

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Make it return NULL rather than assert.

---
 hw/virtio/virtio.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
hw/virtio/virtio.c +6 -7
index d4e4d98b59..aedd390240 100644
@@ -1037,13 +1037,12 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)
 
     qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
 
-    /* TODO: teach all callers that this can fail, and return failure instead
-     * of asserting here.
-     * This is just one thing (there are probably more) that must be
-     * fixed before we can allow NDEBUG compilation.
-     */
-    assert(ARRAY_SIZE(data.in_addr) >= data.in_num);
-    assert(ARRAY_SIZE(data.out_addr) >= data.out_num);
+    if (data.in_num > ARRAY_SIZE(data.in_addr) ||
+        data.out_num > ARRAY_SIZE(data.out_addr)) {
+        error_report("%s: Bad index: in=%d out=%d",
+                    __func__, data.in_num, data.out_num);
+        return NULL;
+    }
 
     elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
     elem->index = data.index;
Signature · 1 line
2.17.1
#3 lore raw

PATCH 2/4 virtio: Check qemu_get_virtqueue_element returns

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Check calls to qemu_get_virtqueue_element for NULL and pass up the chain.

---
 hw/block/virtio-blk.c       | 4 ++++
 hw/char/virtio-serial-bus.c | 4 ++++
 hw/scsi/virtio-scsi.c       | 4 ++++
 3 files changed, 12 insertions(+)
hw/block/virtio-blk.c +4 -0
index 50b5c869e3..324c6b2b27 100644
@@ -888,6 +888,10 @@ static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
         }
 
         req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
+        if (!req) {
+            error_report("%s: Bad vq element %u", __func__, vq_idx);
+            return -EINVAL;
+        }
         virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
         req->next = s->rq;
         s->rq = req;
hw/char/virtio-serial-bus.c +4 -0
index d2dd8ab502..e99dc9bf59 100644
@@ -781,6 +781,10 @@ static int fetch_active_ports_list(QEMUFile *f,
 
             port->elem =
                 qemu_get_virtqueue_element(vdev, f, sizeof(VirtQueueElement));
+            if (!port->elem) {
+                error_report("%s: Bad vq element", __func__);
+                return -EINVAL;
+            }
 
             /*
              *  Port was throttled on source machine.  Let's
hw/scsi/virtio-scsi.c +4 -0
index 3aa99717e2..6301af76ad 100644
@@ -207,6 +207,10 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
     assert(n < vs->conf.num_queues);
     req = qemu_get_virtqueue_element(vdev, f,
                                      sizeof(VirtIOSCSIReq) + vs->cdb_size);
+    if (!req) {
+        error_report("%s: Bad vq element", __func__);
+        return NULL;
+    }
     virtio_scsi_init_req(s, vs->cmd_vqs[n], req);
 
     if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
Signature · 1 line
2.17.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [Qemu-devel] [PATCH for 3.1 1/4] scsi/migration: Allow bus load request to fail

In reply to Dr. David Alan Gilbert (git)
Quoted 9 lines · On Mon, 16 Jul 2018 18:37:40 +0100 "Dr. David Alan Gilbert (git)"…

From: "Dr. David Alan Gilbert" <[email protected]>

Allow the load_request method on a bus to fail and error the migration cleanly.

---
 hw/scsi/scsi-bus.c | 5 +++++
 1 file changed, 5 insertions(+)
Reviewed-by on v1 2/4
#4 lore raw

Re: [Qemu-devel] [PATCH for 3.1 2/4] virtio: Check qemu_get_virtqueue_element returns

In reply to Dr. David Alan Gilbert (git)
Quoted 17 lines · On Mon, 16 Jul 2018 18:37:41 +0100 "Dr. David Alan Gilbert (git)"…

From: "Dr. David Alan Gilbert" <[email protected]>

Check calls to qemu_get_virtqueue_element for NULL and pass up the chain.

---
 hw/block/virtio-blk.c       | 4 ++++
 hw/char/virtio-serial-bus.c | 4 ++++
 hw/scsi/virtio-scsi.c       | 4 ++++
 3 files changed, 12 insertions(+)
hw/block/virtio-blk.c +0 -0
index 50b5c869e3..324c6b2b27 100644

hw/block/virtio-blk.c static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f, · line 892

Quoted 5 lines
hw/block/virtio-blk.c +2 -0
@@ -888,6 +888,10 @@ static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
         }
 
         req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
+        if (!req) {
+            error_report("%s: Bad vq element %u", __func__, vq_idx);

Minor nit: vq_idx is the virtqueue index, and this message makes it look like it is the 'bad vq element'... either add 'vq index', or drop it completely from the error message?

hw/block/virtio-blk.c static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f, · line 897

Quoted 5 lines
hw/block/virtio-blk.c +2 -0
+            return -EINVAL;
+        }
         virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
         req->next = s->rq;
         s->rq = req;

Anyway,

Reviewed-by on v1 3/4
#7 lore raw

Re: [Qemu-devel] [PATCH for 3.1 3/4] virtio-scsi/migration: Allow load_request to fail

In reply to Dr. David Alan Gilbert (git)
Quoted 15 lines · On Mon, 16 Jul 2018 18:37:42 +0100 "Dr. David Alan Gilbert (git)"…

From: "Dr. David Alan Gilbert" <[email protected]>

Rather than asserting, check values and return NULL on failure.

---
 hw/scsi/virtio-scsi.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
hw/scsi/virtio-scsi.c +0 -0
index 6301af76ad..3441bfe6d7 100644

hw/scsi/virtio-scsi.c static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) · line 209

Quoted 7 lines
hw/scsi/virtio-scsi.c +3 -1
@@ -204,7 +204,11 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
     uint32_t n;
 
     qemu_get_be32s(f, &n);
-    assert(n < vs->conf.num_queues);
+    if (n >= vs->conf.num_queues) {
+        error_report("%s: Bad queue number (%d vs %d)",
+                     __func__, n, vs->conf.num_queues);

Hm... "(%d, num_queues %d)" might be a bit clearer?

hw/scsi/virtio-scsi.c static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) · line 214

Quoted 5 lines
hw/scsi/virtio-scsi.c +2 -0
+        return NULL;
+    }
     req = qemu_get_virtqueue_element(vdev, f,
                                      sizeof(VirtIOSCSIReq) + vs->cdb_size);
     if (!req) {

Still,

Reviewed-by on v1 4/4
#9 lore raw

Re: [Qemu-devel] [PATCH for 3.1 4/4] virtio: qemu_get_virtqueue_element fail rather than assert

In reply to Dr. David Alan Gilbert (git)

On Mon, 16 Jul 2018 18:37:43 +0100
"Dr. David Alan Gilbert (git)" <[email protected]> wrote:

$SUBJECT reads a bit odd without a 'make', but it is already long...

Quoted 8 lines

From: "Dr. David Alan Gilbert" <[email protected]>

Make it return NULL rather than assert.

---
 hw/virtio/virtio.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
#5 lore raw

Re: [Qemu-devel] [PATCH for 3.1 2/4] virtio: Check qemu_get_virtqueue_element returns

In reply to Dr. David Alan Gilbert (git)
Quoted 4 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Check calls to qemu_get_virtqueue_element for NULL and pass up the chain.

What happens to the device state that has been partially deserialized (e.g. virtio-blk's s->rq linked list)?

It's not clear to me that simply returning NULL is enough to put QEMU into a sane state without memory leaks or crashes if we decide to retry.