From 10c71907903c6e72402f75030c3a083dc8325887 Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Fri, 12 May 2017 14:05:10 +0200 Subject: [PATCH] virtio-rng: stop virtqueue while the CPU is stopped RH-Author: Laurent Vivier Message-id: <20170512140510.31567-1-lvivier@redhat.com> Patchwork-id: 75078 O-Subject: [RHEV-7.3.z qemu-kvm-rhev PATCH] virtio-rng: stop virtqueue while the CPU is stopped Bugzilla: 1450375 RH-Acked-by: Dr. David Alan Gilbert RH-Acked-by: Juan Quintela RH-Acked-by: Stefan Hajnoczi If we modify the virtio-rng virqueue while the vmstate is already migrated we can have some inconsistencies between the virtqueue state and the memory content. To avoid this, stop the virtqueue while the CPU is stopped. Signed-off-by: Laurent Vivier Reviewed-by: Amit Shah Reviewed-by: Stefan Hajnoczi Reviewed-by: Juan Quintela Signed-off-by: Juan Quintela (cherry picked from commit a23a6d183986ef38b705e85cabdd2af6cdc95276) Signed-off-by: Miroslav Rezanina Conflicts: hw/virtio/trace-events file has moved from ./trace-events hw/virtio/virtio-rng.c because of missing commits: 5943124 virtio: Migration helper function and macro b607579 virtio-rng: Wrap in vmstate 1a66585 virtio: prepare change VMSTATE_VIRTIO_DEVICE macro b7de81f virtio-rng: convert VMSTATE_VIRTIO_DEVICE Original patch was removing the virtio_rng_post_load() function to use virtio_rng_vm_state_change() instead. In the backport, we remove from virtio_rng_load() the part that is executed after the virtio_load() to use virtio_rng_vm_state_change() instead. BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1450375 BREW: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=13186075 Signed-off-by: Laurent Vivier --- hw/virtio/virtio-rng.c | 33 +++++++++++++++++++++++++++++---- include/hw/virtio/virtio-rng.h | 2 ++ trace-events | 3 +++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/hw/virtio/virtio-rng.c b/hw/virtio/virtio-rng.c index 6b991a7..8d15e84 100644 --- a/hw/virtio/virtio-rng.c +++ b/hw/virtio/virtio-rng.c @@ -53,6 +53,15 @@ static void chr_read(void *opaque, const void *buf, size_t size) return; } + /* we can't modify the virtqueue until + * our state is fully synced + */ + + if (!runstate_check(RUN_STATE_RUNNING)) { + trace_virtio_rng_cpu_is_stopped(vrng, size); + return; + } + vrng->quota_remaining -= size; offset = 0; @@ -61,6 +70,7 @@ static void chr_read(void *opaque, const void *buf, size_t size) if (!elem) { break; } + trace_virtio_rng_popped(vrng); len = iov_from_buf(elem->in_sg, elem->in_num, 0, buf + offset, size - offset); offset += len; @@ -140,13 +150,24 @@ static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id) return ret; } + return 0; +} + +static void virtio_rng_vm_state_change(void *opaque, int running, + RunState state) +{ + VirtIORNG *vrng = opaque; + + trace_virtio_rng_vm_state_change(vrng, running, state); + /* We may have an element ready but couldn't process it due to a quota - * limit. Make sure to try again after live migration when the quota may - * have been reset. + * limit or because CPU was stopped. Make sure to try again when the + * CPU restart. */ - virtio_rng_process(vrng); - return 0; + if (running && is_guest_ready(vrng)) { + virtio_rng_process(vrng); + } } static void check_rate_limit(void *opaque) @@ -216,6 +237,9 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp) vrng->activate_timer = true; register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save, virtio_rng_load, vrng); + + vrng->vmstate = qemu_add_vm_change_state_handler(virtio_rng_vm_state_change, + vrng); } static void virtio_rng_device_unrealize(DeviceState *dev, Error **errp) @@ -223,6 +247,7 @@ static void virtio_rng_device_unrealize(DeviceState *dev, Error **errp) VirtIODevice *vdev = VIRTIO_DEVICE(dev); VirtIORNG *vrng = VIRTIO_RNG(dev); + qemu_del_vm_change_state_handler(vrng->vmstate); timer_del(vrng->rate_limit_timer); timer_free(vrng->rate_limit_timer); unregister_savevm(dev, "virtio-rng", vrng); diff --git a/include/hw/virtio/virtio-rng.h b/include/hw/virtio/virtio-rng.h index 3f07de7..2488d1a 100644 --- a/include/hw/virtio/virtio-rng.h +++ b/include/hw/virtio/virtio-rng.h @@ -45,6 +45,8 @@ typedef struct VirtIORNG { QEMUTimer *rate_limit_timer; int64_t quota_remaining; bool activate_timer; + + VMChangeStateEntry *vmstate; } VirtIORNG; #endif diff --git a/trace-events b/trace-events index a0ae2b3..28e6268 100644 --- a/trace-events +++ b/trace-events @@ -43,8 +43,11 @@ virtio_set_status(void *vdev, uint8_t val) "vdev %p val %u" # hw/virtio/virtio-rng.c virtio_rng_guest_not_ready(void *rng) "rng %p: guest not ready" +virtio_rng_cpu_is_stopped(void *rng, int size) "rng %p: cpu is stopped, dropping %d bytes" +virtio_rng_popped(void *rng) "rng %p: elem popped" virtio_rng_pushed(void *rng, size_t len) "rng %p: %zd bytes pushed" virtio_rng_request(void *rng, size_t size, unsigned quota) "rng %p: %zd bytes requested, %u bytes quota left" +virtio_rng_vm_state_change(void *rng, int running, int state) "rng %p: state change to running %d state %d" # hw/char/virtio-serial-bus.c virtio_serial_send_control_event(unsigned int port, uint16_t event, uint16_t value) "port %u, event %u, value %u" -- 1.8.3.1