Skip to contentSkip to conversation

migration: fix virtio-rng

PATCH Superseded Laurent Vivier 2 participants 6 messages Apr 11, 2017 – Apr 12, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover migration: fix virtio-rng

When post-copy migration is enabled, the destination guest can ask for memory from the source when the vmstate is restored.

In the case of virtio, a part of the virtqueue
is migrated by the vmstate structure (last_avail_idx) another part is migrated inside the RAM (used_idx). On the source side, the virtqueue can be modified whereas the vmstate is already migrated, and the destination side can ask for the value in RAM. In this case we have an inconsistency that can generate this kind of error:

    "VQ 0 size 0x8 < last_avail_idx 0xa - used_idx 0"

in hw/virtio/virtio.c:2180, virtio_load().

This happens with virtio-rng as the chr_read() function which modifies the virqueue is called
by the rng backend and the rng backend continues to run while the migration is running and the CPU is stopped.

This series fixes this problem by ignoring chr_read() calls while the CPU is stopped. The first patch of the series fixes another problem triggered by this error
case: a use-after-free case.

The probability to have this problem is very low, as generally the post-copy phase is very short, so the window to modify the virtqueue while the vmstate has been sent is very small... except if you are doing trans-continental guest migration with high latency and post-copy phase that can be run for minutes.

I've been able to reproduce the problem locally on a host, by adding network latency with "tc". Another condition is to have an rng daemon running in the guest to generate events in the virtio-rng device.

Laurent Vivier (2):
migration: don't close a file descriptor while it can be in use
virtio-rng: stop virtqueue while the CPU is stopped

 hw/virtio/trace-events |  2 ++
 hw/virtio/virtio-rng.c | 10 ++++++++++
 migration/migration.c  |  6 +++---
 3 files changed, 15 insertions(+), 3 deletions(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/2 migration: don't close a file descriptor while it can be in use

If we close the QEMUFile descriptor in process_incoming_migration_co() while it has been stopped by an error, the postcopy_ram_listen_thread() can try to continue to use it. And as the memory has been freed it is working with an invalid pointer and crashes.

Fix this by releasing the memory after having managed the error case (which, in fact, calls exit())

---
 migration/migration.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
migration/migration.c +3 -3
index ad4036f..e024e0a 100644
@@ -436,9 +436,6 @@ static void process_incoming_migration_co(void *opaque)
         qemu_thread_join(&mis->colo_incoming_thread);
     }
 
-    qemu_fclose(f);
-    free_xbzrle_decoded_buf();
-
     if (ret < 0) {
         migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
                           MIGRATION_STATUS_FAILED);
@@ -447,6 +444,9 @@ static void process_incoming_migration_co(void *opaque)
         exit(EXIT_FAILURE);
     }
 
+    qemu_fclose(f);
+    free_xbzrle_decoded_buf();
+
     mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
     qemu_bh_schedule(mis->bh);
 }
Signature · 1 line
2.9.3
#2 lore raw

PATCH 2/2 virtio-rng: stop virtqueue while the CPU is stopped

In reply to Laurent Vivier

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.

---
 hw/virtio/trace-events |  2 ++
 hw/virtio/virtio-rng.c | 10 ++++++++++
 2 files changed, 12 insertions(+)
hw/virtio/trace-events +2 -0
index 6926eed..564a4b8 100644
@@ -11,6 +11,8 @@ 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) "rng %p: cpu is stopped"
+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"
 
hw/virtio/virtio-rng.c +10 -0
index 9639f4e..d270d56 100644
@@ -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);
+        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;
Signature · 1 line
2.9.3
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-rng: stop virtqueue while the CPU is stopped

Quoted 7 lines · Laurent Vivier
hw/virtio/virtio-rng.c +0 -0
index 9639f4e..d270d56 100644
@@ -53,6 +53,15 @@ static void chr_read(void *opaque, const void *buf, size_t size)
         return;
     }

hw/virtio/virtio-rng.c static void chr_read(void *opaque, const void *buf, size_t size) · line 64

Quoted 10 lines
hw/virtio/virtio-rng.c +9 -0
 
+    /* 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);
+        return;
+    }
+

I'm concerned about what happens when the guest is stopped and resumed (e.g. 'stop' and 'cont' monitor commands). Since we throw away the chr_read() callback the device will hang unless the guest kicks it again?

It's not clear to me that the rate limit timer will help us...

Stefan

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-rng: stop virtqueue while the CPU is stopped

Quoted 24 lines · Stefan Hajnoczi
Quoted 7 lines · Laurent Vivier
hw/virtio/virtio-rng.c +0 -0
index 9639f4e..d270d56 100644
@@ -53,6 +53,15 @@ static void chr_read(void *opaque, const void *buf, size_t size)
         return;
     }

hw/virtio/virtio-rng.c static void chr_read(void *opaque, const void *buf, size_t size) · line 64

Quoted 10 lines
hw/virtio/virtio-rng.c +9 -0
 
+    /* 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);
+        return;
+    }
+

I'm concerned about what happens when the guest is stopped and resumed (e.g. 'stop' and 'cont' monitor commands). Since we throw away the chr_read() callback the device will hang unless the guest kicks it again?

Quoted 1 line

It's not clear to me that the rate limit timer will help us...

I think you're right (even if it seems hard to generate this case)

What is the best solution:

- re-arming the timer/the backend request by calling virtio_rng_process() before the "return;"

or

- adding a vmstate change handler to call virtio_rng_process()?

Thanks,
Laurent

#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] virtio-rng: stop virtqueue while the CPU is stopped

Quoted 25 lines · Laurent Vivier
Quoted 24 lines · Stefan Hajnoczi
Quoted 7 lines · Laurent Vivier
hw/virtio/virtio-rng.c +0 -0
index 9639f4e..d270d56 100644
@@ -53,6 +53,15 @@ static void chr_read(void *opaque, const void *buf, size_t size)
         return;
     }

hw/virtio/virtio-rng.c static void chr_read(void *opaque, const void *buf, size_t size) · line 64

Quoted 10 lines
hw/virtio/virtio-rng.c +9 -0
 
+    /* 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);
+        return;
+    }
+

I'm concerned about what happens when the guest is stopped and resumed (e.g. 'stop' and 'cont' monitor commands). Since we throw away the chr_read() callback the device will hang unless the guest kicks it again?

Quoted 8 lines
Quoted 1 line

It's not clear to me that the rate limit timer will help us...

I think you're right (even if it seems hard to generate this case)

What is the best solution:

- re-arming the timer/the backend request by calling virtio_rng_process() before the "return;"

This would waste CPU and throw away entropy bits.

Quoted 3 lines

or

- adding a vmstate change handler to call virtio_rng_process()?

Yes, I think vm change handlers solve the problem.