Skip to contentSkip to conversation

migration pull

GIT PULL Amit Shah 6 participants 23 messages Feb 23, 2016 – Mar 8, 2016

lore
Share & tools
lore

Amit Shah

author
#0 lore raw

The following changes since commit 8eb779e4223a18db9838a49ece1bc72cfdfb7761:

Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging (2016-02-22 16:55:41 +0000)

are available in the git repository at:

https://git.kernel.org/pub/scm/virt/qemu/amit/migration.git tags/migration-for-2.6-3

for you to fetch changes up to 612f0af57aa1e8d4e09d7f1a1c442e1d943cbf0c:

cutils: add avx2 instruction optimization (2016-02-23 12:53:03 +0530)

----------------------------------------------------------------
Migration:
- enable avx2 instructions when available
- fix a qcow2 assert
- minor code rearrangement

----------------------------------------------------------------

Denis V. Lunev (2):
migration: move bdrv_invalidate_cache_all of of coroutine context
migration: move bdrv_invalidate_cache_all of of coroutine context

Liang Li (2):
configure: detect ifunc and avx2 attribute
cutils: add avx2 instruction optimization

Wei Yang (1):
migration: reorder code to make it symmetric

 configure             |  21 +++++++++
 include/qemu-common.h |   8 +---
 migration/migration.c |  89 ++++++++++++++++++++-----------------
 migration/savevm.c    |  32 ++++++++------
 util/cutils.c         | 118 ++++++++++++++++++++++++++++++++++++++++++++++++--
 5 files changed, 204 insertions(+), 64 deletions(-)
Signature · 1 line
2.5.0
author
#1 lore raw

[Qemu-devel] [PULL 1/5] migration: move bdrv_invalidate_cache_all of of coroutine context

From: "Denis V. Lunev" <[email protected]>

There is a possibility to hit an assert in qcow2_get_specific_info that s->qcow_version is undefined. This happens when VM in starting from suspended state, i.e. it processes incoming migration, and in the same time 'info block' is called.

The problem is that qcow2_invalidate_cache() closes the image and memset()s BDRVQcowState in the middle.

The patch moves processing of bdrv_invalidate_cache_all out of coroutine context for standard migration to avoid that.

CC Paolo Bonzini <[email protected]>
CC Juan Quintela <[email protected]>
CC Amit Shah <[email protected]>

Message-Id: <[email protected]>

---
 migration/migration.c | 89 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 49 insertions(+), 40 deletions(-)
migration/migration.c +49 -40
index a64cfcd..1f8535e 100644
@@ -323,13 +323,59 @@ void qemu_start_incoming_migration(const char *uri, Error **errp)
     }
 }
 
+static void process_incoming_migration_bh(void *opaque)
+{
+    Error *local_err = NULL;
+    MigrationIncomingState *mis = opaque;
+
+    /* Make sure all file formats flush their mutable metadata */
+    bdrv_invalidate_cache_all(&local_err);
+    if (local_err) {
+        migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
+                          MIGRATION_STATUS_FAILED);
+        error_report_err(local_err);
+        migrate_decompress_threads_join();
+        exit(EXIT_FAILURE);
+    }
+
+    /*
+     * This must happen after all error conditions are dealt with and
+     * we're sure the VM is going to be running on this host.
+     */
+    qemu_announce_self();
+
+    /* If global state section was not received or we are in running
+       state, we need to obey autostart. Any other state is set with
+       runstate_set. */
+
+    if (!global_state_received() ||
+        global_state_get_runstate() == RUN_STATE_RUNNING) {
+        if (autostart) {
+            vm_start();
+        } else {
+            runstate_set(RUN_STATE_PAUSED);
+        }
+    } else {
+        runstate_set(global_state_get_runstate());
+    }
+    migrate_decompress_threads_join();
+    /*
+     * This must happen after any state changes since as soon as an external
+     * observer sees this event they might start to prod at the VM assuming
+     * it's ready to use.
+     */
+    migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
+                      MIGRATION_STATUS_COMPLETED);
+    migration_incoming_state_destroy();
+}
+
 static void process_incoming_migration_co(void *opaque)
 {
     QEMUFile *f = opaque;
-    Error *local_err = NULL;
     MigrationIncomingState *mis;
     PostcopyState ps;
     int ret;
+    QEMUBH *bh;
 
     mis = migration_incoming_state_new(f);
     postcopy_state_set(POSTCOPY_INCOMING_NONE);
@@ -369,45 +415,8 @@ static void process_incoming_migration_co(void *opaque)
         exit(EXIT_FAILURE);
     }
 
-    /* Make sure all file formats flush their mutable metadata */
-    bdrv_invalidate_cache_all(&local_err);
-    if (local_err) {
-        migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
-                          MIGRATION_STATUS_FAILED);
-        error_report_err(local_err);
-        migrate_decompress_threads_join();
-        exit(EXIT_FAILURE);
-    }
-
-    /*
-     * This must happen after all error conditions are dealt with and
-     * we're sure the VM is going to be running on this host.
-     */
-    qemu_announce_self();
-
-    /* If global state section was not received or we are in running
-       state, we need to obey autostart. Any other state is set with
-       runstate_set. */
-
-    if (!global_state_received() ||
-        global_state_get_runstate() == RUN_STATE_RUNNING) {
-        if (autostart) {
-            vm_start();
-        } else {
-            runstate_set(RUN_STATE_PAUSED);
-        }
-    } else {
-        runstate_set(global_state_get_runstate());
-    }
-    migrate_decompress_threads_join();
-    /*
-     * This must happen after any state changes since as soon as an external
-     * observer sees this event they might start to prod at the VM assuming
-     * it's ready to use.
-     */
-    migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
-                      MIGRATION_STATUS_COMPLETED);
-    migration_incoming_state_destroy();
+    bh = qemu_bh_new(process_incoming_migration_bh, mis);
+    qemu_bh_schedule(bh);
 }
 
 void process_incoming_migration(QEMUFile *f)
Signature · 1 line
2.5.0
author
#2 lore raw

[Qemu-devel] [PULL 2/5] migration: move bdrv_invalidate_cache_all of of coroutine context

In reply to Amit Shah

From: "Denis V. Lunev" <[email protected]>

There is a possibility to hit an assert in qcow2_get_specific_info that s->qcow_version is undefined. This happens when VM in starting from suspended state, i.e. it processes incoming migration, and in the same time 'info block' is called.

The problem is that qcow2_invalidate_cache() closes the image and memset()s BDRVQcowState in the middle.

The patch moves processing of bdrv_invalidate_cache_all out of coroutine context for postcopy migration to avoid that. This function is called with the following stack:
process_incoming_migration_co
qemu_loadvm_state
qemu_loadvm_state_main
loadvm_process_command
loadvm_postcopy_handle_run

Message-Id: <[email protected]>

---
 migration/savevm.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
migration/savevm.c +17 -10
index 94f2894..8415fd9 100644
@@ -1496,18 +1496,10 @@ static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
     return 0;
 }
 
-/* After all discards we can start running and asking for pages */
-static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
+static void loadvm_postcopy_handle_run_bh(void *opaque)
 {
-    PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
     Error *local_err = NULL;
 
-    trace_loadvm_postcopy_handle_run();
-    if (ps != POSTCOPY_INCOMING_LISTENING) {
-        error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
-        return -1;
-    }
-
     /* TODO we should move all of this lot into postcopy_ram.c or a shared code
      * in migration.c
      */
@@ -1519,7 +1511,6 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
     bdrv_invalidate_cache_all(&local_err);
     if (local_err) {
         error_report_err(local_err);
-        return -1;
     }
 
     trace_loadvm_postcopy_handle_run_cpu_sync();
@@ -1534,6 +1525,22 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
         /* leave it paused and let management decide when to start the CPU */
         runstate_set(RUN_STATE_PAUSED);
     }
+}
+
+/* After all discards we can start running and asking for pages */
+static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
+{
+    PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
+    QEMUBH *bh;
+
+    trace_loadvm_postcopy_handle_run();
+    if (ps != POSTCOPY_INCOMING_LISTENING) {
+        error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
+        return -1;
+    }
+
+    bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, NULL);
+    qemu_bh_schedule(bh);
 
     /* We need to finish reading the stream from the package
      * and also stop reading anything more from the stream that loaded the
Signature · 1 line
2.5.0
author
#9 lore raw

[Qemu-devel] [PULL 3/5] migration: reorder code to make it symmetric

In reply to Amit Shah

From: Wei Yang <[email protected]>

In qemu_savevm_state_complete_precopy(), it iterates on each device to add a json object and transfer related status to destination, while the order of the last two steps could be refined.

Current order:

json_start_object()
save_section_header()
vmstate_save()
json_end_object()
save_section_footer()

After the change:

json_start_object()
save_section_header()
vmstate_save()
save_section_footer()
json_end_object()

This patch reorder the code to to make it symmetric. No functional change.

Message-Id: <[email protected]>

---
 migration/savevm.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
migration/savevm.c +2 -3
index 8415fd9..60ab119 100644
@@ -1088,12 +1088,11 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
         json_prop_int(vmdesc, "instance_id", se->instance_id);
 
         save_section_header(f, se, QEMU_VM_SECTION_FULL);
-
         vmstate_save(f, se, vmdesc);
-
-        json_end_object(vmdesc);
         trace_savevm_section_end(se->idstr, se->section_id, 0);
         save_section_footer(f, se);
+
+        json_end_object(vmdesc);
     }
 
     if (!in_postcopy) {
Signature · 1 line
2.5.0
author
#10 lore raw

[Qemu-devel] [PULL 4/5] configure: detect ifunc and avx2 attribute

In reply to Amit Shah

From: Liang Li <[email protected]>

Detect if the compiler can support the ifun and avx2, if so, set CONFIG_AVX2_OPT which will be used to turn on the avx2 instruction optimization.

Message-Id: <[email protected]>

---
 configure | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
configure +21 -0
index 0aa249b..d508f10 100755
@@ -279,6 +279,7 @@ smartcard=""
 libusb=""
 usb_redir=""
 opengl=""
+avx2_opt="no"
 zlib="yes"
 lzo=""
 snappy=""
@@ -1772,6 +1773,21 @@ EOF
 fi
 
 ##########################################
+# avx2 optimization requirement check
+
+cat > $TMPC << EOF
+static void bar(void) {}
+static void *bar_ifunc(void) {return (void*) bar;}
+static void foo(void) __attribute__((ifunc("bar_ifunc")));
+int main(void) { foo(); return 0; }
+EOF
+if compile_prog "-mavx2" "" ; then
+    if readelf --syms $TMPE |grep "IFUNC.*foo" >/dev/null 2>&1; then
+        avx2_opt="yes"
+    fi
+fi
+
+#########################################
 # zlib check
 
 if test "$zlib" != "no" ; then
@@ -4776,6 +4792,7 @@ echo "bzip2 support     $bzip2"
 echo "NUMA host support $numa"
 echo "tcmalloc support  $tcmalloc"
 echo "jemalloc support  $jemalloc"
+echo "avx2 optimization $avx2_opt"
 
 if test "$sdl_too_old" = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -5160,6 +5177,10 @@ if test "$opengl" = "yes" ; then
   echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
 fi
 
+if test "$avx2_opt" = "yes" ; then
+  echo "CONFIG_AVX2_OPT=y" >> $config_host_mak
+fi
+
 if test "$lzo" = "yes" ; then
   echo "CONFIG_LZO=y" >> $config_host_mak
 fi
Signature · 1 line
2.5.0
author
#11 lore raw

[Qemu-devel] [PULL 5/5] cutils: add avx2 instruction optimization

In reply to Amit Shah

From: Liang Li <[email protected]>

buffer_find_nonzero_offset() is a hot function during live migration. Now it use SSE2 instructions for optimization. For platform supports AVX2 instructions, use AVX2 instructions for optimization can help to improve the performance of buffer_find_nonzero_offset() about 30% comparing to SSE2.

Live migration can be faster with this optimization, the test result shows that for an 8GiB RAM idle guest just boots, this patch can help to shorten the total live migration time about 6%.

This patch use the ifunc mechanism to select the proper function when running, for platform supports AVX2, execute the AVX2 instructions, else, execute the original instructions.

Message-Id: <[email protected]>

---
 include/qemu-common.h |   8 +---
 util/cutils.c         | 118 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 115 insertions(+), 11 deletions(-)
include/qemu-common.h +1 -7
index f557be7..d26b2f1 100644
@@ -477,13 +477,7 @@ void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
 #endif
 
 #define BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR 8
-static inline bool
-can_use_buffer_find_nonzero_offset(const void *buf, size_t len)
-{
-    return (len % (BUFFER_FIND_NONZERO_OFFSET_UNROLL_FACTOR
-                   * sizeof(VECTYPE)) == 0
-            && ((uintptr_t) buf) % sizeof(VECTYPE) == 0);
-}
+bool can_use_buffer_find_nonzero_offset(const void *buf, size_t len);
 size_t buffer_find_nonzero_offset(const void *buf, size_t len);
 
 /*
util/cutils.c +114 -4

147 lines not on this page · raw message

Signature · 1 line
2.5.0
#12 lore raw
In reply to Amit Shah
Quoted 10 lines · Amit Shah

The following changes since commit 8eb779e4223a18db9838a49ece1bc72cfdfb7761:

Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging (2016-02-22 16:55:41 +0000)

are available in the git repository at:

https://git.kernel.org/pub/scm/virt/qemu/amit/migration.git tags/migration-for-2.6-3

for you to fetch changes up to 612f0af57aa1e8d4e09d7f1a1c442e1d943cbf0c:

Quoted 7 lines

cutils: add avx2 instruction optimization (2016-02-23 12:53:03 +0530)

----------------------------------------------------------------
Migration:
- enable avx2 instructions when available
- fix a qcow2 assert
- minor code rearrangement

Hi. I'm afraid this doesn't compile for x86-64 Linux:

/home/petmay01/linaro/qemu-for-merges/util/cutils.c: In function ‘can_use_buffer_find_nonzero_offset_avx2’:
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:242:29: error:
‘__m256i’ undeclared (first use in this function)
* sizeof(AVX2_VECTYPE)) == 0
^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:242:29: note: each undeclared identifier is reported only once for each function it appears in /home/petmay01/linaro/qemu-for-merges/util/cutils.c: In function ‘buffer_find_nonzero_offset_avx2’:
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:248:5: error:
unknown type name ‘__m256i’

     const AVX2_VECTYPE *p = buf;
     ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:249:5: error:
unknown type name ‘__m256i’
     const AVX2_VECTYPE zero = (AVX2_VECTYPE){0};
     ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:249:27: error:
‘__m256i’ undeclared (first use in this function)
     const AVX2_VECTYPE zero = (AVX2_VECTYPE){0};
                           ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:249:35: error:
expected ‘,’ or ‘;’ before ‘{’ token
     const AVX2_VECTYPE zero = (AVX2_VECTYPE){0};
                                   ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:259:9: error:
implicit declaration of function ‘_mm256_movemask_epi8’
[-Werror=implicit-function-declaration]
         if (!AVX2_ALL_EQ(p[i], zero)) {
         ^

/home/petmay01/linaro/qemu-for-merges/util/cutils.c:259:9: error:
nested extern declaration of ‘_mm256_movemask_epi8’ [-Werror=nested-externs] /home/petmay01/linaro/qemu-for-merges/util/cutils.c:259:9: error:
implicit declaration of function ‘_mm256_cmpeq_epi8’ [-Werror=implicit-function-declaration] /home/petmay01/linaro/qemu-for-merges/util/cutils.c:259:9: error:
nested extern declaration of ‘_mm256_cmpeq_epi8’ [-Werror=nested-externs] /home/petmay01/linaro/qemu-for-merges/util/cutils.c:267:17: error:
expected ‘;’ before ‘tmp0’

         AVX2_VECTYPE tmp0 = AVX2_VEC_OR(p[i + 0], p[i + 1]);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:268:17: error:
expected ‘;’ before ‘tmp1’
         AVX2_VECTYPE tmp1 = AVX2_VEC_OR(p[i + 2], p[i + 3]);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:269:17: error:
expected ‘;’ before ‘tmp2’
         AVX2_VECTYPE tmp2 = AVX2_VEC_OR(p[i + 4], p[i + 5]);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:270:17: error:
expected ‘;’ before ‘tmp3’
         AVX2_VECTYPE tmp3 = AVX2_VEC_OR(p[i + 6], p[i + 7]);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:271:17: error:
expected ‘;’ before ‘tmp01’
         AVX2_VECTYPE tmp01 = AVX2_VEC_OR(tmp0, tmp1);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:272:17: error:
expected ‘;’ before ‘tmp23’
         AVX2_VECTYPE tmp23 = AVX2_VEC_OR(tmp2, tmp3);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:273:9: error:
implicit declaration of function ‘_mm256_or_si256’
[-Werror=implicit-function-declaration]
         if (!AVX2_ALL_EQ(AVX2_VEC_OR(tmp01, tmp23), zero)) {
         ^

/home/petmay01/linaro/qemu-for-merges/util/cutils.c:273:9: error:
nested extern declaration of ‘_mm256_or_si256’ [-Werror=nested-externs] /home/petmay01/linaro/qemu-for-merges/util/cutils.c:273:71: error:
‘tmp01’ undeclared (first use in this function)

         if (!AVX2_ALL_EQ(AVX2_VEC_OR(tmp01, tmp23), zero)) {
                                                                       ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:273:78: error:
‘tmp23’ undeclared (first use in this function)
         if (!AVX2_ALL_EQ(AVX2_VEC_OR(tmp01, tmp23), zero)) {
                                                                              ^

/home/petmay01/linaro/qemu-for-merges/util/cutils.c: In function ‘can_use_buffer_find_nonzero_offset_avx2’:
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:244:1: error:
control reaches end of non-void function [-Werror=return-type]
}
^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c: In function ‘buffer_find_nonzero_offset_avx2’:
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:279:1: error:
control reaches end of non-void function [-Werror=return-type]
}
^
cc1: all warnings being treated as errors

thanks
-- PMM

author
#13 lore raw
Quoted 13 lines · Peter Maydell
Quoted 12 lines · Amit Shah

The following changes since commit 8eb779e4223a18db9838a49ece1bc72cfdfb7761:

Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging (2016-02-22 16:55:41 +0000)

are available in the git repository at:

https://git.kernel.org/pub/scm/virt/qemu/amit/migration.git tags/migration-for-2.6-3

for you to fetch changes up to 612f0af57aa1e8d4e09d7f1a1c442e1d943cbf0c:

cutils: add avx2 instruction optimization (2016-02-23 12:53:03 +0530)

Quoted 7 lines
Quoted 5 lines

----------------------------------------------------------------
Migration:
- enable avx2 instructions when available
- fix a qcow2 assert
- minor code rearrangement

Hi. I'm afraid this doesn't compile for x86-64 Linux:

Compiles for me, but adding Liang Li so he can respond.

Quoted 96 lines

/home/petmay01/linaro/qemu-for-merges/util/cutils.c: In function ‘can_use_buffer_find_nonzero_offset_avx2’:
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:242:29: error:
‘__m256i’ undeclared (first use in this function)
* sizeof(AVX2_VECTYPE)) == 0
^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:242:29: note: each undeclared identifier is reported only once for each function it appears in /home/petmay01/linaro/qemu-for-merges/util/cutils.c: In function ‘buffer_find_nonzero_offset_avx2’:
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:248:5: error:
unknown type name ‘__m256i’

     const AVX2_VECTYPE *p = buf;
     ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:249:5: error:
unknown type name ‘__m256i’
     const AVX2_VECTYPE zero = (AVX2_VECTYPE){0};
     ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:249:27: error:
‘__m256i’ undeclared (first use in this function)
     const AVX2_VECTYPE zero = (AVX2_VECTYPE){0};
                           ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:249:35: error:
expected ‘,’ or ‘;’ before ‘{’ token
     const AVX2_VECTYPE zero = (AVX2_VECTYPE){0};
                                   ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:259:9: error:
implicit declaration of function ‘_mm256_movemask_epi8’
[-Werror=implicit-function-declaration]
         if (!AVX2_ALL_EQ(p[i], zero)) {
         ^

/home/petmay01/linaro/qemu-for-merges/util/cutils.c:259:9: error:
nested extern declaration of ‘_mm256_movemask_epi8’ [-Werror=nested-externs] /home/petmay01/linaro/qemu-for-merges/util/cutils.c:259:9: error:
implicit declaration of function ‘_mm256_cmpeq_epi8’ [-Werror=implicit-function-declaration] /home/petmay01/linaro/qemu-for-merges/util/cutils.c:259:9: error:
nested extern declaration of ‘_mm256_cmpeq_epi8’ [-Werror=nested-externs] /home/petmay01/linaro/qemu-for-merges/util/cutils.c:267:17: error:
expected ‘;’ before ‘tmp0’

         AVX2_VECTYPE tmp0 = AVX2_VEC_OR(p[i + 0], p[i + 1]);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:268:17: error:
expected ‘;’ before ‘tmp1’
         AVX2_VECTYPE tmp1 = AVX2_VEC_OR(p[i + 2], p[i + 3]);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:269:17: error:
expected ‘;’ before ‘tmp2’
         AVX2_VECTYPE tmp2 = AVX2_VEC_OR(p[i + 4], p[i + 5]);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:270:17: error:
expected ‘;’ before ‘tmp3’
         AVX2_VECTYPE tmp3 = AVX2_VEC_OR(p[i + 6], p[i + 7]);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:271:17: error:
expected ‘;’ before ‘tmp01’
         AVX2_VECTYPE tmp01 = AVX2_VEC_OR(tmp0, tmp1);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:272:17: error:
expected ‘;’ before ‘tmp23’
         AVX2_VECTYPE tmp23 = AVX2_VEC_OR(tmp2, tmp3);
                 ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:273:9: error:
implicit declaration of function ‘_mm256_or_si256’
[-Werror=implicit-function-declaration]
         if (!AVX2_ALL_EQ(AVX2_VEC_OR(tmp01, tmp23), zero)) {
         ^

/home/petmay01/linaro/qemu-for-merges/util/cutils.c:273:9: error:
nested extern declaration of ‘_mm256_or_si256’ [-Werror=nested-externs] /home/petmay01/linaro/qemu-for-merges/util/cutils.c:273:71: error:
‘tmp01’ undeclared (first use in this function)

         if (!AVX2_ALL_EQ(AVX2_VEC_OR(tmp01, tmp23), zero)) {
                                                                       ^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:273:78: error:
‘tmp23’ undeclared (first use in this function)
         if (!AVX2_ALL_EQ(AVX2_VEC_OR(tmp01, tmp23), zero)) {
                                                                              ^

/home/petmay01/linaro/qemu-for-merges/util/cutils.c: In function ‘can_use_buffer_find_nonzero_offset_avx2’:
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:244:1: error:
control reaches end of non-void function [-Werror=return-type]
}
^
/home/petmay01/linaro/qemu-for-merges/util/cutils.c: In function ‘buffer_find_nonzero_offset_avx2’:
/home/petmay01/linaro/qemu-for-merges/util/cutils.c:279:1: error:
control reaches end of non-void function [-Werror=return-type]
}
^
cc1: all warnings being treated as errors

Quoted 2 lines

thanks
-- PMM

Amit

#14 lore raw
In reply to Peter Maydell
Quoted 1 line · Peter Maydell

Hi. I'm afraid this doesn't compile for x86-64 Linux:

What compiler is this, and does the following compile with no particular extra options?

#pragma GCC target("avx2")
#include <immintrin.h>
__m256i foo;

Paolo

#22 lore raw
In reply to Peter Maydell
Quoted 21 lines

Cc: qemu list; Dr. David Alan Gilbert; Juan Quintela
Subject: Re: [Qemu-devel] [PULL 0/5] migration pull

Quoted 1 line · Amit Shah

The following changes since commit

8eb779e4223a18db9838a49ece1bc72cfdfb7761:

Quoted 10 lines

Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into
staging (2016-02-22 16:55:41 +0000)

are available in the git repository at:

https://git.kernel.org/pub/scm/virt/qemu/amit/migration.git
tags/migration-for-2.6-3

for you to fetch changes up to

612f0af57aa1e8d4e09d7f1a1c442e1d943cbf0c:

Quoted 4 lines

cutils: add avx2 instruction optimization (2016-02-23 12:53:03
+0530)

Quoted 8 lines
Quoted 5 lines

----------------------------------------------------------------
Migration:
- enable avx2 instructions when available
- fix a qcow2 assert
- minor code rearrangement

Hi. I'm afraid this doesn't compile for x86-64 Linux:

Hi PMM,

Sorry for that, could you tell me which version of GCC are you using? It's strange.

Liang

#15 lore raw
In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini
Quoted 1 line · Peter Maydell

Hi. I'm afraid this doesn't compile for x86-64 Linux:

What compiler is this, and does the following compile with no particular extra options?

#pragma GCC target("avx2")
#include <immintrin.h>
__m256i foo;

This is stock gcc from Ubuntu trusty:

$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

That code fragment you suggest compiles fine normally, but not if I add -save-temps:

$ cat /tmp/zz9.c
#pragma GCC target("avx2")
#include <immintrin.h>
__m256i foo;
$ gcc -g -Wall -o /tmp/zz9.o -c /tmp/zz9.c
$ echo $?
0
$ gcc -g -Wall -o /tmp/zz9.o -c /tmp/zz9.c -save-temps
/tmp/zz9.c:4:1: error: unknown type name ‘__m256i’
 __m256i foo;
 ^
/tmp/zz9.c: In function ‘bar’:
/tmp/zz9.c:7:19: error: ‘__m256i’ undeclared (first use in this function)
     return sizeof(__m256i);
                   ^
/tmp/zz9.c:7:19: note: each undeclared identifier is reported only
once for each function it appears in
/tmp/zz9.c:8:1: warning: control reaches end of non-void function
[-Wreturn-type]
 }
 ^

This seems to be because -save-temps causes the #pragma not to actually #define __AVX__.

This feels all pretty fragile to me and I think we should probably avoid messing with the target pragma if we can.

thanks
-- PMM

#16 lore raw
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Peter Maydell
Quoted 1 line

Hi. I'm afraid this doesn't compile for x86-64 Linux:

What compiler is this, and does the following compile with no particular extra options?

Quoted 8 lines
Quoted 3 lines

#pragma GCC target("avx2")
#include <immintrin.h>
__m256i foo;

This is stock gcc from Ubuntu trusty:

$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

The same version as I used for test, it compiles for me. What's lead to the different result? The command I used for building:

# ./configure --target-list=x86_64-softmmu
# make -j4
# ./configure --target-list=x86_64-linux-user
#make -j4

Both success.

Liang

#17 lore raw
In reply to Peter Maydell
Quoted 25 lines · Peter Maydell

That code fragment you suggest compiles fine normally, but not if I add -save-temps:

$ cat /tmp/zz9.c
#pragma GCC target("avx2")
#include <immintrin.h>
__m256i foo;
$ gcc -g -Wall -o /tmp/zz9.o -c /tmp/zz9.c
$ echo $?
0
$ gcc -g -Wall -o /tmp/zz9.o -c /tmp/zz9.c -save-temps
/tmp/zz9.c:4:1: error: unknown type name ‘__m256i’
 __m256i foo;
 ^
/tmp/zz9.c: In function ‘bar’:
/tmp/zz9.c:7:19: error: ‘__m256i’ undeclared (first use in this function)
     return sizeof(__m256i);
                   ^
/tmp/zz9.c:7:19: note: each undeclared identifier is reported only
once for each function it appears in
/tmp/zz9.c:8:1: warning: control reaches end of non-void function
[-Wreturn-type]
 }
 ^
Quoted 2 lines

This seems to be because -save-temps causes the #pragma not to actually #define __AVX__.

This is because -save-temps causes gcc to invoke the preprocessor and the compiler as separate passes, and the standalone preprocessor doesn't know that the target pragma should result in a new #define, so the result is that the immintrin.h doesn't pull in what it should.

This is also the reason why my build failed -- I use ccache, which is another tool that results in the preprocessor being done as a standalone pass rather than in the same pass as compilation proper.

Arguably it's a gcc bug that the target pragma doesn't cause the standalone preprocessor to define the same #defines that you get if it's all in one pass, but regardless I don't think we can break ccache builds, so you'll need to find a different way to do this, I'm afraid.

(Also gcc's docs don't say anything about target pragmas adding #defines so either the docs or the implementation are wrong.)

thanks
-- PMM

#18 lore raw
Quoted 40 lines · Peter Maydell
Quoted 25 lines · Peter Maydell

That code fragment you suggest compiles fine normally, but not if I add -save-temps:

$ cat /tmp/zz9.c
#pragma GCC target("avx2")
#include <immintrin.h>
__m256i foo;
$ gcc -g -Wall -o /tmp/zz9.o -c /tmp/zz9.c
$ echo $?
0
$ gcc -g -Wall -o /tmp/zz9.o -c /tmp/zz9.c -save-temps
/tmp/zz9.c:4:1: error: unknown type name ‘__m256i’
 __m256i foo;
 ^
/tmp/zz9.c: In function ‘bar’:
/tmp/zz9.c:7:19: error: ‘__m256i’ undeclared (first use in this function)
     return sizeof(__m256i);
                   ^
/tmp/zz9.c:7:19: note: each undeclared identifier is reported only
once for each function it appears in
/tmp/zz9.c:8:1: warning: control reaches end of non-void function
[-Wreturn-type]
 }
 ^
Quoted 2 lines

This seems to be because -save-temps causes the #pragma not to actually #define __AVX__.

This is because -save-temps causes gcc to invoke the preprocessor and the compiler as separate passes, and the standalone preprocessor doesn't know that the target pragma should result in a new #define, so the result is that the immintrin.h doesn't pull in what it should.

This is also the reason why my build failed -- I use ccache, which is another tool that results in the preprocessor being done as a standalone pass rather than in the same pass as compilation proper.

Quoted 5 lines

Arguably it's a gcc bug that the target pragma doesn't cause the standalone preprocessor to define the same #defines that you get if it's all in one pass, but regardless I don't think we can break ccache builds, so you'll need to find a different way to do this, I'm afraid.

It's a bug in the header file, it was fixed in 4.9.

https://gcc.gnu.org/ml/gcc-patches/2013-06/txtvBBiTsFs8g.txt

Amit or Liang, can you restrict the new optimization to GCC 4.9+?

Paolo

#19 lore raw
Quoted 44 lines · Peter Maydell
Quoted 1 line

On 23 February 2016 at 10:43, Peter Maydell <[email protected]>

Quoted 30 lines · wrote:
Quoted 22 lines

That code fragment you suggest compiles fine normally, but not if I add -save-temps:

$ cat /tmp/zz9.c
#pragma GCC target("avx2")
#include <immintrin.h>
__m256i foo;
$ gcc -g -Wall -o /tmp/zz9.o -c /tmp/zz9.c $ echo $?
0
$ gcc -g -Wall -o /tmp/zz9.o -c /tmp/zz9.c -save-temps
/tmp/zz9.c:4:1: error: unknown type name ‘__m256i’
 __m256i foo;
 ^
/tmp/zz9.c: In function ‘bar’:
/tmp/zz9.c:7:19: error: ‘__m256i’ undeclared (first use in this function)
     return sizeof(__m256i);
                   ^
/tmp/zz9.c:7:19: note: each undeclared identifier is reported only
once for each function it appears in
/tmp/zz9.c:8:1: warning: control reaches end of non-void function
[-Wreturn-type]  }  ^
Quoted 2 lines

This seems to be because -save-temps causes the #pragma not to actually #define __AVX__.

This is because -save-temps causes gcc to invoke the preprocessor and the compiler as separate passes, and the standalone preprocessor doesn't know that the target pragma should result in a new #define, so the result is that the immintrin.h doesn't pull in what it should.

Quoted 8 lines

This is also the reason why my build failed -- I use ccache, which is another tool that results in the preprocessor being done as a standalone pass rather than in the same pass as compilation proper.

Arguably it's a gcc bug that the target pragma doesn't cause the standalone preprocessor to define the same #defines that you get if it's all in one pass, but regardless I don't think we can break ccache builds, so you'll need to find a different way to do this, I'm afraid.

Quoted 7 lines

It's a bug in the header file, it was fixed in 4.9.

https://gcc.gnu.org/ml/gcc-patches/2013-06/txtvBBiTsFs8g.txt

Amit or Liang, can you restrict the new optimization to GCC 4.9+?

Paolo

Of course, how about using '#if QEMU_GNUC_PREREQ(4, 9) && defined CONFIG_AVX2_OPT' instead of '#ifdef CONFIG_AVX2_OPT' in cutils.c?

Liang

#3 lore raw

Re: [Qemu-devel] [PULL 2/5] migration: move bdrv_invalidate_cache_all of of coroutine context

In reply to Amit Shah
Quoted 19 lines · * Amit Shah ([email protected]) wrote:

From: "Denis V. Lunev" <[email protected]>

There is a possibility to hit an assert in qcow2_get_specific_info that s->qcow_version is undefined. This happens when VM in starting from suspended state, i.e. it processes incoming migration, and in the same time 'info block' is called.

The problem is that qcow2_invalidate_cache() closes the image and memset()s BDRVQcowState in the middle.

The patch moves processing of bdrv_invalidate_cache_all out of coroutine context for postcopy migration to avoid that. This function is called with the following stack:
process_incoming_migration_co
qemu_loadvm_state
qemu_loadvm_state_main
loadvm_process_command
loadvm_postcopy_handle_run

Quoted 2 lines

hmm; actually - this segs in a variety of different ways; there are two problems:

a) + bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, NULL);
That's the easy one; that NULL should be 'mis', because the bh is expecting to use it as a MigrationIncomingState so it segs fairly reliably in the qemu_bh_delete(mis->bh)

b) The harder problem is that there's a race where qemu_bh_delete segs, and I'm not 100% sure why yet - it only does it sometime (i.e. run virt-test and leave it and it occasionally does it). From the core it looks like qemu->bh is corrupt (0x10101010...) so maybe mis has been freed at that point?
I'm suspecting this is the postcopy_ram_listen_thread freeing mis at the end of it, but I don't know yet.

Dave

Quoted 68 lines

CC: Paolo Bonzini <[email protected]>
CC: Juan Quintela <[email protected]>
CC: Amit Shah <[email protected]>
Message-Id: <[email protected]>

---
 migration/savevm.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
migration/savevm.c +17 -10
index 94f2894..8415fd9 100644
@@ -1496,18 +1496,10 @@ static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
     return 0;
 }
 
-/* After all discards we can start running and asking for pages */
-static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
+static void loadvm_postcopy_handle_run_bh(void *opaque)
 {
-    PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
     Error *local_err = NULL;
 
-    trace_loadvm_postcopy_handle_run();
-    if (ps != POSTCOPY_INCOMING_LISTENING) {
-        error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
-        return -1;
-    }
-
     /* TODO we should move all of this lot into postcopy_ram.c or a shared code
      * in migration.c
      */
@@ -1519,7 +1511,6 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
     bdrv_invalidate_cache_all(&local_err);
     if (local_err) {
         error_report_err(local_err);
-        return -1;
     }
 
     trace_loadvm_postcopy_handle_run_cpu_sync();
@@ -1534,6 +1525,22 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
         /* leave it paused and let management decide when to start the CPU */
         runstate_set(RUN_STATE_PAUSED);
     }
+}
+
+/* After all discards we can start running and asking for pages */
+static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
+{
+    PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
+    QEMUBH *bh;
+
+    trace_loadvm_postcopy_handle_run();
+    if (ps != POSTCOPY_INCOMING_LISTENING) {
+        error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
+        return -1;
+    }
+
+    bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, NULL);
+    qemu_bh_schedule(bh);
 
     /* We need to finish reading the stream from the package
      * and also stop reading anything more from the stream that loaded the
Signature · 1 line
2.5.0

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#4 lore raw

Re: [Qemu-devel] [PULL 2/5] migration: move bdrv_invalidate_cache_all of of coroutine context

Quoted 7 lines · Dr. David Alan Gilbert

b) The harder problem is that there's a race where qemu_bh_delete segs, and I'm not 100% sure why yet - it only does it sometime (i.e. run virt-test and leave it and it occasionally does it). From the core it looks like qemu->bh is corrupt (0x10101010...) so maybe mis has been freed at that point?
I'm suspecting this is the postcopy_ram_listen_thread freeing mis at the end of it, but I don't know yet.

That should be it. Maybe the patch can simply be reverted, because loadvm_postcopy_handle_run runs from a thread and not a coroutine. Is this correct?

However I have a bug or two for you to fix, too:

1) as far as I can see, postcopy_ram_listen_thread is not holding the mutex during the call to qemu_loadvm_state_main. Is that a bug?

2) no one is currently joining mis->listen_thread, I suspect it actually should be QEMU_THREAD_DETACHED.

:)

Paolo

#5 lore raw

Re: [Qemu-devel] [PULL 2/5] migration: move bdrv_invalidate_cache_all of of coroutine context

Quoted 11 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 7 lines · Dr. David Alan Gilbert

b) The harder problem is that there's a race where qemu_bh_delete segs, and I'm not 100% sure why yet - it only does it sometime (i.e. run virt-test and leave it and it occasionally does it). From the core it looks like qemu->bh is corrupt (0x10101010...) so maybe mis has been freed at that point?
I'm suspecting this is the postcopy_ram_listen_thread freeing mis at the end of it, but I don't know yet.

Quoted 3 lines

That should be it. Maybe the patch can simply be reverted, because loadvm_postcopy_handle_run runs from a thread and not a coroutine. Is this correct?

That's still in the main thread, the 'run' comes from the packaged postcopy state, but is after the 'listener' thread has been started.

I need to understand this anyway; the way it's supposed to work is that
if postcopy is being used then not much cleanup happens in process_incoming_migration_co instead it exits and lets postcopy_ram_listen_thread do the cleanup
at the end; I've not quite figured out what's going on here
but it almost looks like both of them are cleaning up - that shouldn't happen.

Quoted 4 lines

However I have a bug or two for you to fix, too:

1) as far as I can see, postcopy_ram_listen_thread is not holding the mutex during the call to qemu_loadvm_state_main. Is that a bug?

No; the guest is running, the only thing that gets loaded by that listen thread is data that's postcopied - i.e. currently just ram pages that are loaded atomically.

Quoted 2 lines

2) no one is currently joining mis->listen_thread, I suspect it actually should be QEMU_THREAD_DETACHED.

OK, that looks like the easier one.

Dave

Quoted 4 lines

:)

Paolo

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#6 lore raw

Re: [Qemu-devel] [PULL 2/5] migration: move bdrv_invalidate_cache_all of of coroutine context

In reply to Dr. David Alan Gilbert
Quoted 38 lines · Dr. David Alan Gilbert
Quoted 19 lines · * Amit Shah ([email protected]) wrote:

From: "Denis V. Lunev" <[email protected]>

There is a possibility to hit an assert in qcow2_get_specific_info that s->qcow_version is undefined. This happens when VM in starting from suspended state, i.e. it processes incoming migration, and in the same time 'info block' is called.

The problem is that qcow2_invalidate_cache() closes the image and memset()s BDRVQcowState in the middle.

The patch moves processing of bdrv_invalidate_cache_all out of coroutine context for postcopy migration to avoid that. This function is called with the following stack:
process_incoming_migration_co
qemu_loadvm_state
qemu_loadvm_state_main
loadvm_process_command
loadvm_postcopy_handle_run

Quoted 2 lines

hmm; actually - this segs in a variety of different ways; there are two problems:

a) + bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, NULL);
That's the easy one; that NULL should be 'mis', because the bh is expecting to use it as a MigrationIncomingState so it segs fairly reliably in the qemu_bh_delete(mis->bh)

b) The harder problem is that there's a race where qemu_bh_delete segs, and I'm not 100% sure why yet - it only does it sometime (i.e. run virt-test and leave it and it occasionally does it). From the core it looks like qemu->bh is corrupt (0x10101010...) so maybe mis has been freed at that point?
I'm suspecting this is the postcopy_ram_listen_thread freeing mis at the end of it, but I don't know yet.

Quoted 1 line

Dave

Yes. this is exactly use-after-free. I have looked into the code and this seems correct.

Could you try this simple patch?

Den

migration/savevm.c +8 -9
index 96e7db5..9a020ef 100644
@@ -1446,15 +1446,6 @@ static void *postcopy_ram_listen_thread(void *opaque)
 
     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
                                    MIGRATION_STATUS_COMPLETED);
-    /*
-     * If everything has worked fine, then the main thread has waited
-     * for us to start, and we're the last use of the mis.
-     * (If something broke then qemu will have to exit anyway since it's
-     * got a bad migration state).
-     */
-    migration_incoming_state_destroy();
-
-
     return NULL;
 }
 
@@ -1533,6 +1524,14 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)
     }
 
     qemu_bh_delete(mis->bh);
+
+    /*
+     * If everything has worked fine, then the main thread has waited
+     * for us to start, and we're the last use of the mis.
+     * (If something broke then qemu will have to exit anyway since it's
+     * got a bad migration state).
+     */
+    migration_incoming_state_destroy();
 }
 
 /* After all discards we can start running and asking for pages */
#21 lore raw
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Amit or Liang, can you restrict the new optimization to GCC 4.9+?

Paolo

Of course, how about using '#if QEMU_GNUC_PREREQ(4, 9) && defined

CONFIG_AVX2_OPT' instead of '#ifdef CONFIG_AVX2_OPT' in cutils.c?

Hi Liang, are you going to submit a new series with this?

Yes, just wait for a few minutes.

Quoted 4 lines

Thanks,

Amit

#7 lore raw

Re: [Qemu-devel] [PULL 2/5] migration: move bdrv_invalidate_cache_all of of coroutine context

In reply to Denis V. Lunev
Quoted 39 lines · * Denis V. Lunev ([email protected]) wrote:
Quoted 38 lines · Dr. David Alan Gilbert
Quoted 19 lines · * Amit Shah ([email protected]) wrote:

From: "Denis V. Lunev" <[email protected]>

There is a possibility to hit an assert in qcow2_get_specific_info that s->qcow_version is undefined. This happens when VM in starting from suspended state, i.e. it processes incoming migration, and in the same time 'info block' is called.

The problem is that qcow2_invalidate_cache() closes the image and memset()s BDRVQcowState in the middle.

The patch moves processing of bdrv_invalidate_cache_all out of coroutine context for postcopy migration to avoid that. This function is called with the following stack:
process_incoming_migration_co
qemu_loadvm_state
qemu_loadvm_state_main
loadvm_process_command
loadvm_postcopy_handle_run

Quoted 2 lines

hmm; actually - this segs in a variety of different ways; there are two problems:

a) + bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, NULL);
That's the easy one; that NULL should be 'mis', because the bh is expecting to use it as a MigrationIncomingState so it segs fairly reliably in the qemu_bh_delete(mis->bh)

b) The harder problem is that there's a race where qemu_bh_delete segs, and I'm not 100% sure why yet - it only does it sometime (i.e. run virt-test and leave it and it occasionally does it). From the core it looks like qemu->bh is corrupt (0x10101010...) so maybe mis has been freed at that point?
I'm suspecting this is the postcopy_ram_listen_thread freeing mis at the end of it, but I don't know yet.

Quoted 6 lines
Quoted 1 line

Dave

Yes. this is exactly use-after-free. I have looked into the code and this seems correct.

Could you try this simple patch?

Hmm no, that's not right.
The order for postcopy is that we are running the listen thread and then receive the 'run', and the listening thread is still running - so you can't destroy the incoming state during the run.
It can't get destroyed until both the main thread has finished loading the migration AND the listen thread has finished.

Hmm - that does give me an idea about the other seg I saw; I need to check it; but I think the problem is probably the case of a very short postcopy where the listen thread exits before the handle_run_bh is triggered; (and since I've only seen it in my virt-test setup, and I know it can do very short postcopies)
I think the fix here is to pass loadvm_postcopy_handle_run_bh a pointer to it's own bh structure rather than store it in mis->bh; that way it doesn't use mis at all.

Dave

Quoted 29 lines

Den

migration/savevm.c +1 -9
index 96e7db5..9a020ef 100644
@@ -1446,15 +1446,6 @@ static void *postcopy_ram_listen_thread(void *opaque)
 
     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
                                    MIGRATION_STATUS_COMPLETED);
-    /*
-     * If everything has worked fine, then the main thread has waited
-     * for us to start, and we're the last use of the mis.
-     * (If something broke then qemu will have to exit anyway since it's
-     * got a bad migration state).
-     */
-    migration_incoming_state_destroy();
-
-
     return NULL;
 }
 
@@ -1533,6 +1524,14 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)
     }
 
     qemu_bh_delete(mis->bh);
+

migration/savevm.c static void loadvm_postcopy_handle_run_bh(void *opaque) · line 1537

Quoted 10 lines
migration/savevm.c +7 -0
+    /*
+     * If everything has worked fine, then the main thread has waited
+     * for us to start, and we're the last use of the mis.
+     * (If something broke then qemu will have to exit anyway since it's
+     * got a bad migration state).
+     */
+    migration_incoming_state_destroy();
 }
 
 /* After all discards we can start running and asking for pages */

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#8 lore raw

Re: [Qemu-devel] [PULL 2/5] migration: move bdrv_invalidate_cache_all of of coroutine context

Quoted 100 lines · Dr. David Alan Gilbert
Quoted 39 lines · * Denis V. Lunev ([email protected]) wrote:
Quoted 38 lines · Dr. David Alan Gilbert
Quoted 19 lines · * Amit Shah ([email protected]) wrote:

From: "Denis V. Lunev" <[email protected]>

There is a possibility to hit an assert in qcow2_get_specific_info that s->qcow_version is undefined. This happens when VM in starting from suspended state, i.e. it processes incoming migration, and in the same time 'info block' is called.

The problem is that qcow2_invalidate_cache() closes the image and memset()s BDRVQcowState in the middle.

The patch moves processing of bdrv_invalidate_cache_all out of coroutine context for postcopy migration to avoid that. This function is called with the following stack:
process_incoming_migration_co
qemu_loadvm_state
qemu_loadvm_state_main
loadvm_process_command
loadvm_postcopy_handle_run

Quoted 2 lines

hmm; actually - this segs in a variety of different ways; there are two problems:

a) + bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, NULL);
That's the easy one; that NULL should be 'mis', because the bh is expecting to use it as a MigrationIncomingState so it segs fairly reliably in the qemu_bh_delete(mis->bh)

b) The harder problem is that there's a race where qemu_bh_delete segs, and I'm not 100% sure why yet - it only does it sometime (i.e. run virt-test and leave it and it occasionally does it). From the core it looks like qemu->bh is corrupt (0x10101010...) so maybe mis has been freed at that point?
I'm suspecting this is the postcopy_ram_listen_thread freeing mis at the end of it, but I don't know yet.

Quoted 5 lines
Quoted 1 line

Dave

Yes. this is exactly use-after-free. I have looked into the code and this seems correct.

Could you try this simple patch?

Hmm no, that's not right.
The order for postcopy is that we are running the listen thread and then receive the 'run', and the listening thread is still running - so you can't destroy the incoming state during the run.
It can't get destroyed until both the main thread has finished loading the migration AND the listen thread has finished.

Hmm - that does give me an idea about the other seg I saw; I need to check it; but I think the problem is probably the case of a very short postcopy where the listen thread exits before the handle_run_bh is triggered; (and since I've only seen it in my virt-test setup, and I know it can do very short postcopies)
I think the fix here is to pass loadvm_postcopy_handle_run_bh a pointer to it's own bh structure rather than store it in mis->bh; that way it doesn't use mis at all.

Dave

Quoted 37 lines

Den

migration/savevm.c +8 -9
index 96e7db5..9a020ef 100644
@@ -1446,15 +1446,6 @@ static void *postcopy_ram_listen_thread(void *opaque)
 
      migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
                                     MIGRATION_STATUS_COMPLETED);
-    /*
-     * If everything has worked fine, then the main thread has waited
-     * for us to start, and we're the last use of the mis.
-     * (If something broke then qemu will have to exit anyway since it's
-     * got a bad migration state).
-     */
-    migration_incoming_state_destroy();
-
-
      return NULL;
  }
 
@@ -1533,6 +1524,14 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)
      }
 
      qemu_bh_delete(mis->bh);
+
+    /*
+     * If everything has worked fine, then the main thread has waited
+     * for us to start, and we're the last use of the mis.
+     * (If something broke then qemu will have to exit anyway since it's
+     * got a bad migration state).
+     */
+    migration_incoming_state_destroy();
  }
 
Quoted 3 lines

migration/savevm.c static void loadvm_postcopy_handle_run_bh(void *opaque) · line 1537

Quoted 1 line
migration/savevm.c +0 -0
  /* After all discards we can start running and asking for pages */

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

This will help for sure. The idea to reuse migration state seems wrong.

Den