qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Juan Quintela <quintela@redhat.com>
To: qemu-devel@nongnu.org
Cc: dgilbert@redhat.com, Laurent Vivier <lvivier@redhat.com>
Subject: [Qemu-devel] [PULL 61/65] virtio-rng: stop virtqueue while the CPU is stopped
Date: Fri, 21 Apr 2017 13:56:42 +0200	[thread overview]
Message-ID: <20170421115646.15544-62-quintela@redhat.com> (raw)
In-Reply-To: <20170421115646.15544-1-quintela@redhat.com>

From: Laurent Vivier <lvivier@redhat.com>

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 <lvivier@redhat.com>
Reviewed-by:  Amit Shah <amit@kernel.org>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 hw/virtio/trace-events         |  3 +++
 hw/virtio/virtio-rng.c         | 29 +++++++++++++++++++++++------
 include/hw/virtio/virtio-rng.h |  2 ++
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events
index 6926eed..1f7a7c1 100644
--- a/hw/virtio/trace-events
+++ b/hw/virtio/trace-events
@@ -11,8 +11,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/virtio/virtio-balloon.c
 #
diff --git a/hw/virtio/virtio-rng.c b/hw/virtio/virtio-rng.c
index 9639f4e..a6ee501 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;
@@ -120,17 +130,21 @@ static uint64_t get_features(VirtIODevice *vdev, uint64_t f, Error **errp)
     return f;
 }
 
-static int virtio_rng_post_load(void *opaque, int version_id)
+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)
@@ -198,6 +212,9 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
     vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
                                                check_rate_limit, vrng);
     vrng->activate_timer = true;
+
+    vrng->vmstate = qemu_add_vm_change_state_handler(virtio_rng_vm_state_change,
+                                                     vrng);
 }
 
 static void virtio_rng_device_unrealize(DeviceState *dev, Error **errp)
@@ -205,6 +222,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);
     virtio_cleanup(vdev);
@@ -218,7 +236,6 @@ static const VMStateDescription vmstate_virtio_rng = {
         VMSTATE_VIRTIO_DEVICE,
         VMSTATE_END_OF_LIST()
     },
-    .post_load =  virtio_rng_post_load,
 };
 
 static Property virtio_rng_properties[] = {
diff --git a/include/hw/virtio/virtio-rng.h b/include/hw/virtio/virtio-rng.h
index 2d40abd..922dce7 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
-- 
2.9.3

  parent reply	other threads:[~2017-04-21 11:58 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-21 11:55 [Qemu-devel] [PULL 00/65] Migration pull request Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 01/65] ram: Update all functions comments Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 02/65] ram: Rename flush_page_queue() to migration_page_queue_free() Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 03/65] ram: Rename block_name to rbname Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 04/65] ram: Create RAMState Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 05/65] ram: Add dirty_rate_high_cnt to RAMState Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 06/65] ram: Move bitmap_sync_count into RAMState Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 07/65] ram: Move start time " Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 08/65] ram: Move bytes_xfer_prev " Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 09/65] ram: Change byte_xfer_{prev, now} type to uint64_t Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 10/65] ram: Move num_dirty_pages_period into RAMState Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 11/65] ram: Change num_dirty_pages_period type to uint64_t Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 12/65] ram: Move xbzrle_cache_miss_prev into RAMState Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 13/65] ram: Move iterations_prev " Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 14/65] ram: Move dup_pages " Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 15/65] ram: Remove unused dup_mig_bytes_transferred() Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 16/65] ram: Remove unused pages_skipped variable Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 17/65] ram: Move norm_pages to RAMState Juan Quintela
2017-04-21 11:55 ` [Qemu-devel] [PULL 18/65] ram: Remove norm_mig_bytes_transferred Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 19/65] ram: Move iterations into RAMState Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 20/65] ram: Move xbzrle_bytes " Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 21/65] ram: Move xbzrle_pages " Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 22/65] ram: Move xbzrle_cache_miss " Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 23/65] ram: Move xbzrle_cache_miss_rate " Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 24/65] ram: Move xbzrle_overflows " Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 25/65] ram: Move migration_dirty_pages to RAMState Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 26/65] ram: Everything was init to zero, so use memset Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 27/65] ram: Move migration_bitmap_mutex into RAMState Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 28/65] ram: Move migration_bitmap_rcu " Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 29/65] ram: Move bytes_transferred " Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 30/65] ram: Use the RAMState bytes_transferred parameter Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 31/65] ram: Remove ram_save_remaining Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 32/65] ram: Move last_req_rb to RAMState Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 33/65] ram: Move src_page_req* " Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 34/65] ram: Create ram_dirty_sync_count() Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 35/65] ram: Remove dirty_bytes_rate Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 36/65] ram: Move dirty_pages_rate to RAMState Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 37/65] ram: Move postcopy_requests into RAMState Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 38/65] ram: Add QEMUFile to RAMState Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 39/65] ram: Move QEMUFile into RAMState Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 40/65] ram: Remove compression_switch and inline its logic Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 41/65] migration: Remove MigrationState from migration_in_postcopy Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 42/65] ram: We don't need MigrationState parameter anymore Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 43/65] ram: Rename qemu_target_page_bits() to qemu_target_page_size() Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 44/65] ram: Add page-size to output in 'info migrate' Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 45/65] ram: Pass RAMBlock to bitmap_sync Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 46/65] ram: ram_discard_range() don't use the mis parameter Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 47/65] ram: reorganize last_sent_block Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 48/65] ram: Use page number instead of an address for the bitmap operations Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 49/65] ram: Remember last_page instead of last_offset Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 50/65] ram: Change offset field in PageSearchStatus to page Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 51/65] ram: Use ramblock and page offset instead of absolute offset Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 52/65] ram: rename last_ram_offset() last_ram_pages() Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 53/65] ram: Use RAMBitmap type for coherence Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 54/65] migration: Remove MigrationState parameter from migration_is_idle() Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 55/65] qdev: qdev_hotplug is really a bool Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 56/65] qdev: Export qdev_hot_removed Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 57/65] qdev: Move qdev_unplug() to qdev-monitor.c Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 58/65] migration: Disable hotplug/unplug during migration Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 59/65] ram: Remove migration_bitmap_extend() Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 60/65] migration: don't close a file descriptor while it can be in use Juan Quintela
2017-04-21 11:56 ` Juan Quintela [this message]
2017-04-21 11:56 ` [Qemu-devel] [PULL 62/65] migration: set current_active_state once Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 63/65] migration: rename max_size to threshold_size Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 64/65] hmp: info migrate_capability format tunes Juan Quintela
2017-04-21 11:56 ` [Qemu-devel] [PULL 65/65] hmp: info migrate_parameters " Juan Quintela
2017-04-21 16:09 ` [Qemu-devel] [PULL 00/65] Migration pull request Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170421115646.15544-62-quintela@redhat.com \
    --to=quintela@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).