Am 24.07.2026 um 15:04 hat Michael S. Tsirkin geschrieben:
> Currently virtio-blk, virtio-serial and virtio-scsi all crash on invalid
> migration streams because qemu_get_virtqueue_element has no way to
> detect and report mapping failures.
>
> Not nice.
>
> Let's propagate mapping errors through qemu_get_virtqueue_element and
> fail migration instead.
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3888
> Signed-off-by: Michael S. Tsirkin <[email protected]>
> ---
> include/hw/virtio/virtio.h | 2 +-
> hw/block/virtio-blk.c | 4 ++++
> hw/char/virtio-serial-bus.c | 4 ++++
> hw/scsi/virtio-scsi.c | 6 +++++
> hw/virtio/virtio.c | 48 +++++++++++++++++++++++++++----------
> 5 files changed, 50 insertions(+), 14 deletions(-)
>
> diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
> index c99cb19d88..27c5fe3a6b 100644
> --- a/include/hw/virtio/virtio.h
> +++ b/include/hw/virtio/virtio.h
> @@ -320,7 +320,7 @@ bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
> void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
> unsigned int len, unsigned int idx);
>
> -void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
> +bool virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
> void *virtqueue_pop(VirtQueue *vq, size_t sz);
> unsigned int virtqueue_drop_all(VirtQueue *vq);
> void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
> diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
> index 6b92066aff..cb6a276a82 100644
> --- a/hw/block/virtio-blk.c
> +++ b/hw/block/virtio-blk.c
> @@ -1384,6 +1384,10 @@ static int virtio_blk_load_device(VirtIODevice *vdev,
> QEMUFile *f,
> }
>
> req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
> + if (!req) {
> + error_report("Failed to restore virtio-blk request");
> + return -EINVAL;
> + }
> virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
Instead of having error_report() calls deep down in device code, could
we just pass an Error **errp VirtioDeviceClass.load/save() and print the
error in virtio_load/save()?
And the next step would then be to change virtio_load/save() to take an
Error **errp and to switch virtio_vmstate_info from .get/.put to
.load/.save that actually allow returning an error.
> WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
> diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c
> index c1973f0248..0f56321cc7 100644
> --- a/hw/char/virtio-serial-bus.c
> +++ b/hw/char/virtio-serial-bus.c
> @@ -763,6 +763,10 @@ static int fetch_active_ports_list(QEMUFile *f,
>
> port->elem =
> qemu_get_virtqueue_element(vdev, f,
> sizeof(VirtQueueElement));
> + if (!port->elem) {
> + error_report("Failed to restore virtio-serial element");
> + return -EINVAL;
> + }
>
> /*
> * Port was throttled on source machine. Let's
> diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c
> index 53c3c88460..8675afc6e1 100644
> --- a/hw/scsi/virtio-scsi.c
> +++ b/hw/scsi/virtio-scsi.c
> @@ -18,6 +18,7 @@
> #include "standard-headers/linux/virtio_ids.h"
> #include "hw/virtio/virtio-scsi.h"
> #include "migration/qemu-file-types.h"
> +#include "migration/qemu-file.h"
> #include "qemu/defer-call.h"
> #include "qemu/error-report.h"
> #include "qemu/iov.h"
> @@ -273,6 +274,11 @@ 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("Failed to restore virtio-scsi request");
> + qemu_file_set_error(f, -EINVAL);
Is this to break the loop in get_scsi_requests() instead of silently
ignoring the error? Wouldn't it be better to actually use the return
value from this function there (and to pass errors up instead of always
returning 0)?
> + return NULL;
> + }
> virtio_scsi_init_req(s, vs->cmd_vqs[n], req);
>
> if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
More context:
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);
}
I think if you want to avoid crashes, you'll also want to avoid exit(1)
calls? Though maybe that's a separate patch because it's unrelated to
qemu_get_virtqueue_element().
Kevin