Skip to contentSkip to conversation

Postcopy fix and traces

PATCH In review Dr. David Alan Gilbert (git) 6 participants 18 messages Apr 26, 2017 – May 4, 2017

lore
Share & tools
lore
#0 lore raw

cover Postcopy fix and traces

From: "Dr. David Alan Gilbert" <[email protected]>

Hi,
This is a small postcopy fix for a bug that
Christian ran into, and a couple of extra traces that would have made finding it easier.

Dave

Dr. David Alan Gilbert (2):
Postcopy: Force allocation of all-zero precopy pages
migration: Extra tracing

 include/migration/migration.h |  3 ++-
 migration/ram.c               | 14 ++++++++++----
 migration/rdma.c              |  2 +-
 migration/trace-events        |  2 ++
 4 files changed, 15 insertions(+), 6 deletions(-)
Signature · 1 line
2.12.2
#1 lore raw

PATCH 1/2 Postcopy: Force allocation of all-zero precopy pages

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

---
 include/migration/migration.h |  3 ++-
 migration/ram.c               | 12 ++++++++----
 migration/rdma.c              |  2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)
include/migration/migration.h +2 -1
index ba1a16cbc1..b47904033c 100644
@@ -265,7 +265,8 @@ uint64_t xbzrle_mig_pages_overflow(void);
 uint64_t xbzrle_mig_pages_cache_miss(void);
 double xbzrle_mig_cache_miss_rate(void);
 
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size);
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write);
 void ram_debug_dump_bitmap(unsigned long *todump, bool expected);
 /* For outgoing discard bitmap */
 int ram_postcopy_send_discard_bitmap(MigrationState *ms);
migration/ram.c +8 -4
index f48664ec62..b4ed41c725 100644
@@ -2274,10 +2274,12 @@ static inline void *host_from_ram_block_offset(RAMBlock *block,
  * @host: host address for the zero page
  * @ch: what the page is filled from.  We only support zero
  * @size: size of the zero page
+ * @always_write: Always perform the memset even if it's zero
  */
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write)
 {
-    if (ch != 0 || !is_zero_range(host, size)) {
+    if (ch != 0 || always_write || !is_zero_range(host, size)) {
         memset(host, ch, size);
     }
 }
@@ -2514,7 +2516,8 @@ static int ram_load_postcopy(QEMUFile *f)
         switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            memset(page_buffer, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(page_buffer, ch, TARGET_PAGE_SIZE,
+                                  true);
             if (ch) {
                 all_zero = false;
             }
@@ -2664,7 +2667,8 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE,
+                                  postcopy_advised);
             break;
 
         case RAM_SAVE_FLAG_PAGE:
migration/rdma.c +1 -1
index fe0a4b5a83..07a9bd75d8 100644
@@ -3164,7 +3164,7 @@ static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque)
             host_addr = block->local_host_addr +
                             (comp->offset - block->offset);
 
-            ram_handle_compressed(host_addr, comp->value, comp->length);
+            ram_handle_compressed(host_addr, comp->value, comp->length, false);
             break;
 
         case RDMA_CONTROL_REGISTER_FINISHED:
Signature · 1 line
2.12.2
#14 lore raw

PATCH 2/2 migration: Extra tracing

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

A couple more traces that would have made fixing that postcopy bug a bit easier.

---
 migration/ram.c        | 2 ++
 migration/trace-events | 2 ++
 2 files changed, 4 insertions(+)
migration/ram.c +2 -0
index b4ed41c725..3ac41ccaba 100644
@@ -812,6 +812,7 @@ static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage)
     ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
 
     p = block->host + offset;
+    trace_ram_save_page(block->idstr, (uint64_t)offset, p);
 
     /* In doubt sent page as normal */
     bytes_xmit = 0;
@@ -2614,6 +2615,7 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
                 ret = -EINVAL;
                 break;
             }
+            trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
         }
 
         switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
migration/trace-events +2 -0
index b8f01a218c..5b8ccf301c 100644
@@ -69,8 +69,10 @@ migration_bitmap_sync_start(void) ""
 migration_bitmap_sync_end(uint64_t dirty_pages) "dirty_pages %" PRIu64
 migration_throttle(void) ""
 ram_discard_range(const char *rbname, uint64_t start, size_t len) "%s: start: %" PRIx64 " %zx"
+ram_load_loop(const char *rbname, uint64_t addr, int flags, void *host) "%s: addr: %" PRIx64 " flags: %x host: %p"
 ram_load_postcopy_loop(uint64_t addr, int flags) "@%" PRIx64 " %x"
 ram_postcopy_send_discard_bitmap(void) ""
+ram_save_page(const char *rbname, uint64_t offset, void *host) "%s: offset: %" PRIx64 " host: %p"
 ram_save_queue_pages(const char *rbname, size_t start, size_t len) "%s: start: %zx len: %zx"
 
 # migration/migration.c
Signature · 1 line
2.12.2
Acked-by on v1 2/2
#15 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration: Extra tracing

Quoted 6 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

A couple more traces that would have made fixing that postcopy bug a bit easier.

Quoted 41 lines
---
 migration/ram.c        | 2 ++
 migration/trace-events | 2 ++
 2 files changed, 4 insertions(+)
migration/ram.c +2 -0
index b4ed41c725..3ac41ccaba 100644
@@ -812,6 +812,7 @@ static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage)
     ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
 
     p = block->host + offset;
+    trace_ram_save_page(block->idstr, (uint64_t)offset, p);
 
     /* In doubt sent page as normal */
     bytes_xmit = 0;
@@ -2614,6 +2615,7 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
                 ret = -EINVAL;
                 break;
             }
+            trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
         }
 
         switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
migration/trace-events +2 -0
index b8f01a218c..5b8ccf301c 100644
@@ -69,8 +69,10 @@ migration_bitmap_sync_start(void) ""
 migration_bitmap_sync_end(uint64_t dirty_pages) "dirty_pages %" PRIu64
 migration_throttle(void) ""
 ram_discard_range(const char *rbname, uint64_t start, size_t len) "%s: start: %" PRIx64 " %zx"
+ram_load_loop(const char *rbname, uint64_t addr, int flags, void *host) "%s: addr: %" PRIx64 " flags: %x host: %p"
 ram_load_postcopy_loop(uint64_t addr, int flags) "@%" PRIx64 " %x"
 ram_postcopy_send_discard_bitmap(void) ""
+ram_save_page(const char *rbname, uint64_t offset, void *host) "%s: offset: %" PRIx64 " host: %p"
 ram_save_queue_pages(const char *rbname, size_t start, size_t len) "%s: start: %zx len: %zx"
 
 # migration/migration.c
Tested-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

In reply to Dr. David Alan Gilbert (git)
Quoted 13 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

Quoted 6 lines

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

CC stable? after all the guest hangs on both sides

Has survived 40 migrations (usually failed at the 2nd)

Quoted 73 lines
---
 include/migration/migration.h |  3 ++-
 migration/ram.c               | 12 ++++++++----
 migration/rdma.c              |  2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)
include/migration/migration.h +2 -1
index ba1a16cbc1..b47904033c 100644
@@ -265,7 +265,8 @@ uint64_t xbzrle_mig_pages_overflow(void);
 uint64_t xbzrle_mig_pages_cache_miss(void);
 double xbzrle_mig_cache_miss_rate(void);
 
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size);
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write);
 void ram_debug_dump_bitmap(unsigned long *todump, bool expected);
 /* For outgoing discard bitmap */
 int ram_postcopy_send_discard_bitmap(MigrationState *ms);
migration/ram.c +8 -4
index f48664ec62..b4ed41c725 100644
@@ -2274,10 +2274,12 @@ static inline void *host_from_ram_block_offset(RAMBlock *block,
  * @host: host address for the zero page
  * @ch: what the page is filled from.  We only support zero
  * @size: size of the zero page
+ * @always_write: Always perform the memset even if it's zero
  */
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write)
 {
-    if (ch != 0 || !is_zero_range(host, size)) {
+    if (ch != 0 || always_write || !is_zero_range(host, size)) {
         memset(host, ch, size);
     }
 }
@@ -2514,7 +2516,8 @@ static int ram_load_postcopy(QEMUFile *f)
         switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            memset(page_buffer, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(page_buffer, ch, TARGET_PAGE_SIZE,
+                                  true);
             if (ch) {
                 all_zero = false;
             }
@@ -2664,7 +2667,8 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE,
+                                  postcopy_advised);
             break;
 
         case RAM_SAVE_FLAG_PAGE:
migration/rdma.c +1 -1
index fe0a4b5a83..07a9bd75d8 100644
@@ -3164,7 +3164,7 @@ static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque)
             host_addr = block->local_host_addr +
                             (comp->offset - block->offset);
 
-            ram_handle_compressed(host_addr, comp->value, comp->length);
+            ram_handle_compressed(host_addr, comp->value, comp->length, false);
             break;
 
         case RDMA_CONTROL_REGISTER_FINISHED:
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

Quoted 18 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

Quoted 7 lines
Quoted 2 lines

CC stable? after all the guest hangs on both sides

Has survived 40 migrations (usually failed at the 2nd)

Great...but.....
Andrea (added to the mail) says this shouldn't be necessary. The read we were doing in the is_zero_range() should have been sufficient to get the page mapped and that zero page should have survived.

So - I guess that's back a step, we need to figure out why the page disapepars for you.

Dave

Quoted 68 lines
Quoted 68 lines
---
 include/migration/migration.h |  3 ++-
 migration/ram.c               | 12 ++++++++----
 migration/rdma.c              |  2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)
include/migration/migration.h +2 -1
index ba1a16cbc1..b47904033c 100644
@@ -265,7 +265,8 @@ uint64_t xbzrle_mig_pages_overflow(void);
 uint64_t xbzrle_mig_pages_cache_miss(void);
 double xbzrle_mig_cache_miss_rate(void);
 
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size);
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write);
 void ram_debug_dump_bitmap(unsigned long *todump, bool expected);
 /* For outgoing discard bitmap */
 int ram_postcopy_send_discard_bitmap(MigrationState *ms);
migration/ram.c +8 -4
index f48664ec62..b4ed41c725 100644
@@ -2274,10 +2274,12 @@ static inline void *host_from_ram_block_offset(RAMBlock *block,
  * @host: host address for the zero page
  * @ch: what the page is filled from.  We only support zero
  * @size: size of the zero page
+ * @always_write: Always perform the memset even if it's zero
  */
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write)
 {
-    if (ch != 0 || !is_zero_range(host, size)) {
+    if (ch != 0 || always_write || !is_zero_range(host, size)) {
         memset(host, ch, size);
     }
 }
@@ -2514,7 +2516,8 @@ static int ram_load_postcopy(QEMUFile *f)
         switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            memset(page_buffer, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(page_buffer, ch, TARGET_PAGE_SIZE,
+                                  true);
             if (ch) {
                 all_zero = false;
             }
@@ -2664,7 +2667,8 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE,
+                                  postcopy_advised);
             break;
 
         case RAM_SAVE_FLAG_PAGE:
migration/rdma.c +0 -0
index fe0a4b5a83..07a9bd75d8 100644
@@ -3164,7 +3164,7 @@ static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque)
             host_addr = block->local_host_addr +
                             (comp->offset - block->offset);
 
Quoted 7 lines

migration/rdma.c static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque) · line 3170

Quoted 6 lines
migration/rdma.c +1 -1
-            ram_handle_compressed(host_addr, comp->value, comp->length);
+            ram_handle_compressed(host_addr, comp->value, comp->length, false);
             break;
 
         case RDMA_CONTROL_REGISTER_FINISHED:

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

#12 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

In reply to Dr. David Alan Gilbert (git)
Quoted 59 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

---
 include/migration/migration.h |  3 ++-
 migration/ram.c               | 12 ++++++++----
 migration/rdma.c              |  2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)
include/migration/migration.h +2 -1
index ba1a16cbc1..b47904033c 100644
@@ -265,7 +265,8 @@ uint64_t xbzrle_mig_pages_overflow(void);
 uint64_t xbzrle_mig_pages_cache_miss(void);
 double xbzrle_mig_cache_miss_rate(void);
 
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size);
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write);
 void ram_debug_dump_bitmap(unsigned long *todump, bool expected);
 /* For outgoing discard bitmap */
 int ram_postcopy_send_discard_bitmap(MigrationState *ms);
migration/ram.c +4 -2
index f48664ec62..b4ed41c725 100644
@@ -2274,10 +2274,12 @@ static inline void *host_from_ram_block_offset(RAMBlock *block,
  * @host: host address for the zero page
  * @ch: what the page is filled from.  We only support zero
  * @size: size of the zero page
+ * @always_write: Always perform the memset even if it's zero
  */
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write)
 {
-    if (ch != 0 || !is_zero_range(host, size)) {
+    if (ch != 0 || always_write || !is_zero_range(host, size)) {
         memset(host, ch, size);
     }
 }

migration/ram.c static int ram_load_postcopy(QEMUFile *f) · line 2520

Quoted 6 lines
migration/ram.c +2 -1
@@ -2514,7 +2516,8 @@ static int ram_load_postcopy(QEMUFile *f)
         switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            memset(page_buffer, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(page_buffer, ch, TARGET_PAGE_SIZE,
+                                  true);

This is weird, if we are in postcopy, we were already doing the memset unconditionally, so .... weird as it can be.

I agree with Andrea that if the is_zero_range() don't assure that the page is there, it is really strange.

Can you look at utils/bufferiszero() and see what that function is doing in power? File is a mess of ifdefs to optimize for intel as far as I can see.

Later, Juan.

Quoted 26 lines
migration/ram.c +2 -1
             if (ch) {
                 all_zero = false;
             }
@@ -2664,7 +2667,8 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE,
+                                  postcopy_advised);
             break;
 
         case RAM_SAVE_FLAG_PAGE:
migration/rdma.c +1 -1
index fe0a4b5a83..07a9bd75d8 100644
@@ -3164,7 +3164,7 @@ static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque)
             host_addr = block->local_host_addr +
                             (comp->offset - block->offset);
 
-            ram_handle_compressed(host_addr, comp->value, comp->length);
+            ram_handle_compressed(host_addr, comp->value, comp->length, false);
             break;
 
         case RDMA_CONTROL_REGISTER_FINISHED:
#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

In reply to Dr. David Alan Gilbert

Hello,

Quoted 27 lines · Dr. David Alan Gilbert
Quoted 18 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

Quoted 7 lines
Quoted 2 lines

CC stable? after all the guest hangs on both sides

Has survived 40 migrations (usually failed at the 2nd)

Quoted 7 lines

Great...but.....
Andrea (added to the mail) says this shouldn't be necessary. The read we were doing in the is_zero_range() should have been sufficient to get the page mapped and that zero page should have survived.

So - I guess that's back a step, we need to figure out why the page disapepars for you.

Yes reading during precopy is enough to fill the hole and prevent userfault missing faults to trigger.

Somehow the pagetable must be mapped by a zeropage or a hugezeropage or a regular page allocated during a previous precopy pass or a pre-zeroed subpage part of a THP.

Even if the hugezeropage is splitted later by a MADV_DONTNEED with postcopy starts, they will become 4k zeropages.

After a read succeeds, nothing (except MADV_DONTNEED or other explicit syscalls which qemu would need to invoke explicitly between is_zero_range and UFFDIO_REGISTER) should be able to bring the pagetable back to its "pte_none/pmd_none" state that will then trigger missing userfaults during postcopy later.

Thanks,
Andrea

#10 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

In reply to Dr. David Alan Gilbert
Quoted 24 lines · Dr. David Alan Gilbert
Quoted 23 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 13 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

Quoted 6 lines

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

CC stable? after all the guest hangs on both sides

Quoted 7 lines
Quoted 2 lines

Has survived 40 migrations (usually failed at the 2nd)

Great...but.....
Andrea (added to the mail) says this shouldn't be necessary. The read we were doing in the is_zero_range() should have been sufficient to get the page mapped and that zero page should have survived.

We do not do is_zero_range if ch==0 because of lazy evaluation, no?

Quoted 84 lines

So - I guess that's back a step, we need to figure out why the page disapepars for you.

Dave

Quoted 68 lines
Quoted 68 lines
---
 include/migration/migration.h |  3 ++-
 migration/ram.c               | 12 ++++++++----
 migration/rdma.c              |  2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)
include/migration/migration.h +2 -1
index ba1a16cbc1..b47904033c 100644
@@ -265,7 +265,8 @@ uint64_t xbzrle_mig_pages_overflow(void);
 uint64_t xbzrle_mig_pages_cache_miss(void);
 double xbzrle_mig_cache_miss_rate(void);
 
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size);
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write);
 void ram_debug_dump_bitmap(unsigned long *todump, bool expected);
 /* For outgoing discard bitmap */
 int ram_postcopy_send_discard_bitmap(MigrationState *ms);
migration/ram.c +8 -4
index f48664ec62..b4ed41c725 100644
@@ -2274,10 +2274,12 @@ static inline void *host_from_ram_block_offset(RAMBlock *block,
  * @host: host address for the zero page
  * @ch: what the page is filled from.  We only support zero
  * @size: size of the zero page
+ * @always_write: Always perform the memset even if it's zero
  */
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write)
 {
-    if (ch != 0 || !is_zero_range(host, size)) {
+    if (ch != 0 || always_write || !is_zero_range(host, size)) {
         memset(host, ch, size);
     }
 }
@@ -2514,7 +2516,8 @@ static int ram_load_postcopy(QEMUFile *f)
         switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            memset(page_buffer, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(page_buffer, ch, TARGET_PAGE_SIZE,
+                                  true);
             if (ch) {
                 all_zero = false;
             }
@@ -2664,7 +2667,8 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE,
+                                  postcopy_advised);
             break;
 
         case RAM_SAVE_FLAG_PAGE:
migration/rdma.c +0 -0
index fe0a4b5a83..07a9bd75d8 100644
@@ -3164,7 +3164,7 @@ static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque)
             host_addr = block->local_host_addr +
                             (comp->offset - block->offset);
 
Quoted 7 lines

migration/rdma.c static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque) · line 3170

Quoted 6 lines
migration/rdma.c +1 -1
-            ram_handle_compressed(host_addr, comp->value, comp->length);
+            ram_handle_compressed(host_addr, comp->value, comp->length, false);
             break;
 
         case RDMA_CONTROL_REGISTER_FINISHED:

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

#11 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

Quoted 28 lines · Christian Borntraeger
Quoted 27 lines · Dr. David Alan Gilbert
Quoted 25 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 13 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

Quoted 6 lines

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

CC stable? after all the guest hangs on both sides

Has survived 40 migrations (usually failed at the 2nd)

Quoted 6 lines
Quoted 4 lines

Great...but.....
Andrea (added to the mail) says this shouldn't be necessary. The read we were doing in the is_zero_range() should have been sufficient to get the page mapped and that zero page should have survived.

We do not do is_zero_range if ch==0 because of lazy evaluation, no?

Sorry misread that code. We do.

#5 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

In reply to Andrea Arcangeli
Quoted 45 lines · Andrea Arcangeli

Hello,

Quoted 27 lines · Dr. David Alan Gilbert
Quoted 18 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

Quoted 7 lines
Quoted 2 lines

CC stable? after all the guest hangs on both sides

Has survived 40 migrations (usually failed at the 2nd)

Quoted 7 lines

Great...but.....
Andrea (added to the mail) says this shouldn't be necessary. The read we were doing in the is_zero_range() should have been sufficient to get the page mapped and that zero page should have survived.

So - I guess that's back a step, we need to figure out why the page disapepars for you.

Yes reading during precopy is enough to fill the hole and prevent userfault missing faults to trigger.

Somehow the pagetable must be mapped by a zeropage or a hugezeropage or a regular page allocated during a previous precopy pass or a pre-zeroed subpage part of a THP.

Quoted 8 lines

Even if the hugezeropage is splitted later by a MADV_DONTNEED with postcopy starts, they will become 4k zeropages.

After a read succeeds, nothing (except MADV_DONTNEED or other explicit syscalls which qemu would need to invoke explicitly between is_zero_range and UFFDIO_REGISTER) should be able to bring the pagetable back to its "pte_none/pmd_none" state that will then trigger missing userfaults during postcopy later.

No matter what finally the solution would be (after see Juan's comment, I am curious about whether is_zero_page() behaves differently in power now)... Dave, would it worth mentioning in ram_handle_compressed() about this read side-effect? Otherwise imho it might be hard for many people to quickly notice this.

Thanks,

Signature · 1 line
Peter Xu
#6 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

Quoted 46 lines · Peter Xu
Quoted 45 lines · Andrea Arcangeli

Hello,

Quoted 27 lines · Dr. David Alan Gilbert
Quoted 18 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 17 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

Quoted 7 lines
Quoted 2 lines

CC stable? after all the guest hangs on both sides

Has survived 40 migrations (usually failed at the 2nd)

Quoted 7 lines

Great...but.....
Andrea (added to the mail) says this shouldn't be necessary. The read we were doing in the is_zero_range() should have been sufficient to get the page mapped and that zero page should have survived.

So - I guess that's back a step, we need to figure out why the page disapepars for you.

Yes reading during precopy is enough to fill the hole and prevent userfault missing faults to trigger.

Somehow the pagetable must be mapped by a zeropage or a hugezeropage or a regular page allocated during a previous precopy pass or a pre-zeroed subpage part of a THP.

Quoted 8 lines
Quoted 8 lines

Even if the hugezeropage is splitted later by a MADV_DONTNEED with postcopy starts, they will become 4k zeropages.

After a read succeeds, nothing (except MADV_DONTNEED or other explicit syscalls which qemu would need to invoke explicitly between is_zero_range and UFFDIO_REGISTER) should be able to bring the pagetable back to its "pte_none/pmd_none" state that will then trigger missing userfaults during postcopy later.

I have started instrumenting the kernel. I can see a set_pte_at for this address and I see an (to be understood) invalidation shortly after that which explains why I get a fault.

Quoted 4 lines

No matter what finally the solution would be (after see Juan's comment, I am curious about whether is_zero_page() behaves differently in power now)... Dave, would it worth mentioning in

s390 is not power. :-)

And we fall back to the normal buffer_zero_int which reads.

Quoted 2 lines

ram_handle_compressed() about this read side-effect? Otherwise imho it might be hard for many people to quickly notice this.

#13 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

In reply to Juan Quintela
Quoted 61 lines · * Juan Quintela ([email protected]) wrote:
Quoted 60 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

When an all-zero page is received during the precopy phase of a postcopy-enabled migration we must force allocation otherwise accesses to the page will still get blocked by userfault.

Symptom:
a) If the page is accessed by a device during device-load then we get a deadlock as the source finishes sending all its pages but the destination device-load is still paused and so doesn't clean up.

b) If the page is accessed later, then the thread will stay paused until the end of migration rather than carrying on running, until we release userfault at the end.

---
 include/migration/migration.h |  3 ++-
 migration/ram.c               | 12 ++++++++----
 migration/rdma.c              |  2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)
include/migration/migration.h +2 -1
index ba1a16cbc1..b47904033c 100644
@@ -265,7 +265,8 @@ uint64_t xbzrle_mig_pages_overflow(void);
 uint64_t xbzrle_mig_pages_cache_miss(void);
 double xbzrle_mig_cache_miss_rate(void);
 
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size);
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write);
 void ram_debug_dump_bitmap(unsigned long *todump, bool expected);
 /* For outgoing discard bitmap */
 int ram_postcopy_send_discard_bitmap(MigrationState *ms);
migration/ram.c +4 -2
index f48664ec62..b4ed41c725 100644
@@ -2274,10 +2274,12 @@ static inline void *host_from_ram_block_offset(RAMBlock *block,
  * @host: host address for the zero page
  * @ch: what the page is filled from.  We only support zero
  * @size: size of the zero page
+ * @always_write: Always perform the memset even if it's zero
  */
-void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
+void ram_handle_compressed(void *host, uint8_t ch, uint64_t size,
+                           bool always_write)
 {
-    if (ch != 0 || !is_zero_range(host, size)) {
+    if (ch != 0 || always_write || !is_zero_range(host, size)) {
         memset(host, ch, size);
     }
 }
@@ -2514,7 +2516,8 @@ static int ram_load_postcopy(QEMUFile *f)
         switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
Quoted 8 lines

migration/ram.c static int ram_load_postcopy(QEMUFile *f) · line 2520

Quoted 5 lines
migration/ram.c +2 -1
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            memset(page_buffer, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(page_buffer, ch, TARGET_PAGE_SIZE,
+                                  true);

This is weird, if we are in postcopy, we were already doing the memset unconditionally, so .... weird as it can be.

No, we were already doing the memset once we had switched into postcopy mode, this code does it while we're still in the precopy phase.

Dave

Quoted 31 lines

I agree with Andrea that if the is_zero_range() don't assure that the page is there, it is really strange.

Can you look at utils/bufferiszero() and see what that function is doing in power? File is a mess of ifdefs to optimize for intel as far as I can see.

Later, Juan.

Quoted 21 lines
migration/ram.c +2 -1
             if (ch) {
                 all_zero = false;
             }
@@ -2664,7 +2667,8 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 
         case RAM_SAVE_FLAG_COMPRESS:
             ch = qemu_get_byte(f);
-            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
+            ram_handle_compressed(host, ch, TARGET_PAGE_SIZE,
+                                  postcopy_advised);
             break;
 
         case RAM_SAVE_FLAG_PAGE:
migration/rdma.c +0 -0
index fe0a4b5a83..07a9bd75d8 100644
@@ -3164,7 +3164,7 @@ static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque)
             host_addr = block->local_host_addr +
                             (comp->offset - block->offset);
 
Quoted 5 lines

migration/rdma.c static int qemu_rdma_registration_handle(QEMUFile *f, void *opaque) · line 3170

Quoted 5 lines
migration/rdma.c +1 -1
-            ram_handle_compressed(host_addr, comp->value, comp->length);
+            ram_handle_compressed(host_addr, comp->value, comp->length, false);
             break;
 
         case RDMA_CONTROL_REGISTER_FINISHED:

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

#7 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

In reply to Christian Borntraeger
Quoted 3 lines · Christian Borntraeger

I have started instrumenting the kernel. I can see a set_pte_at for this address and I see an (to be understood) invalidation shortly after that which explains why I get a fault.

Sounds great that you can see an invalidation shortly after, that is the real source of the problem. Can you get a stack trace of such invalidation?

Thanks!
Andrea

#8 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

Quoted 5 lines · Andrea Arcangeli
Quoted 3 lines · Christian Borntraeger

I have started instrumenting the kernel. I can see a set_pte_at for this address and I see an (to be understood) invalidation shortly after that which explains why I get a fault.

Quoted 7 lines

Sounds great that you can see an invalidation shortly after, that is the real source of the problem. Can you get a stack trace of such invalidation?

Thanks!
Andrea

Finally got it. I had a test module in that guest, which triggered a storage key operation. Normally we no longer use the storage keys in Linux. Therefore KVM disables storage key support and intercepts all storage key instructions to enable the support for that lazily.This makes paging easier and faster to not worry about those. When we enable storage keys, we must not use shared pages as the storage key is a property of the physical page frame (and not of the virtual page). Therefore, this enablement makes mm_forbids_zeropage return true and removes all existing zero pages.
(see commit 2faee8ff9dc6f4bfe46f6d2d110add858140fb20
s390/mm: prevent and break zero page mappings in case of storage keys)
In this case it was called while migrating the storage keys (via kvm ioctl) resulting in zero page mappings going away. (see qemu hw/s390x/s390-skeys.c)

Apr 28 14:48:43 s38lp08 kernel: ([<000000000011218a>] show_trace+0x62/0x78)
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000112278>] show_stack+0x68/0xe0 
Apr 28 14:48:43 s38lp08 kernel:  [<000000000066f82e>] dump_stack+0x7e/0xb0 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000123b2c>] ptep_xchg_direct+0x254/0x288 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000127cfe>] __s390_enable_skey+0x76/0xa0 
Apr 28 14:48:43 s38lp08 kernel:  [<00000000002e5278>] __walk_page_range+0x270/0x500 
Apr 28 14:48:43 s38lp08 kernel:  [<00000000002e5592>] walk_page_range+0x8a/0x148 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000127bc6>] s390_enable_skey+0x116/0x140 
Apr 28 14:48:43 s38lp08 kernel:  [<000000000013fd92>] kvm_arch_vm_ioctl+0x11ea/0x1c70 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000131aa2>] kvm_vm_ioctl+0xca/0x710 
Apr 28 14:48:43 s38lp08 kernel:  [<00000000003460e8>] do_vfs_ioctl+0xa8/0x608 
Apr 28 14:48:43 s38lp08 kernel:  [<00000000003466ec>] SyS_ioctl+0xa4/0xb8 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000923460>] system_call+0xc4/0x23c

As a result a userfault on this virtual address will indeed go back to QEMU and asks again for that page. And then QEMU "oh I have that page already transferred" (even if it was detected as zero page and just faulted in by reading from it) So I will not write it again.

Several options:
- let postcopy not discard a page, even it if must already be there (patch from David)
- change s390-skeys to register_savevm_live and do the skey enablement very early (but this will be impossible for incoming data from old versions)
- let kernel s390_enable_skey actually fault in (might show big memory consumption)
- let qemu hw/s390x/s390-skeys.c tell the migration code that pages might need retransmissions
....

#9 lore raw

Re: [Qemu-devel] [PATCH 1/2] Postcopy: Force allocation of all-zero precopy pages

Quoted 42 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 5 lines · Andrea Arcangeli
Quoted 3 lines · Christian Borntraeger

I have started instrumenting the kernel. I can see a set_pte_at for this address and I see an (to be understood) invalidation shortly after that which explains why I get a fault.

Quoted 7 lines

Sounds great that you can see an invalidation shortly after, that is the real source of the problem. Can you get a stack trace of such invalidation?

Thanks!
Andrea

Finally got it. I had a test module in that guest, which triggered a storage key operation. Normally we no longer use the storage keys in Linux. Therefore KVM disables storage key support and intercepts all storage key instructions to enable the support for that lazily.This makes paging easier and faster to not worry about those. When we enable storage keys, we must not use shared pages as the storage key is a property of the physical page frame (and not of the virtual page). Therefore, this enablement makes mm_forbids_zeropage return true and removes all existing zero pages.
(see commit 2faee8ff9dc6f4bfe46f6d2d110add858140fb20
s390/mm: prevent and break zero page mappings in case of storage keys)
In this case it was called while migrating the storage keys (via kvm ioctl) resulting in zero page mappings going away. (see qemu hw/s390x/s390-skeys.c)

Apr 28 14:48:43 s38lp08 kernel: ([<000000000011218a>] show_trace+0x62/0x78)
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000112278>] show_stack+0x68/0xe0 
Apr 28 14:48:43 s38lp08 kernel:  [<000000000066f82e>] dump_stack+0x7e/0xb0 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000123b2c>] ptep_xchg_direct+0x254/0x288 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000127cfe>] __s390_enable_skey+0x76/0xa0 
Apr 28 14:48:43 s38lp08 kernel:  [<00000000002e5278>] __walk_page_range+0x270/0x500 
Apr 28 14:48:43 s38lp08 kernel:  [<00000000002e5592>] walk_page_range+0x8a/0x148 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000127bc6>] s390_enable_skey+0x116/0x140 
Apr 28 14:48:43 s38lp08 kernel:  [<000000000013fd92>] kvm_arch_vm_ioctl+0x11ea/0x1c70 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000131aa2>] kvm_vm_ioctl+0xca/0x710 
Apr 28 14:48:43 s38lp08 kernel:  [<00000000003460e8>] do_vfs_ioctl+0xa8/0x608 
Apr 28 14:48:43 s38lp08 kernel:  [<00000000003466ec>] SyS_ioctl+0xa4/0xb8 
Apr 28 14:48:43 s38lp08 kernel:  [<0000000000923460>] system_call+0xc4/0x23c
Quoted 7 lines

As a result a userfault on this virtual address will indeed go back to QEMU and asks again for that page. And then QEMU "oh I have that page already transferred" (even if it was detected as zero page and just faulted in by reading from it) So I will not write it again.

Several options:
- let postcopy not discard a page, even it if must already be there (patch from David)

Yes so that patch was forcing the population of zero pages when received; now that might cause a lot of memory consumption - so it's not a great idea if we can avoid it.

Quoted 5 lines

- change s390-skeys to register_savevm_live and do the skey enablement very early (but this will be impossible for incoming data from old versions)
- let kernel s390_enable_skey actually fault in (might show big memory consumption)
- let qemu hw/s390x/s390-skeys.c tell the migration code that pages might need retransmissions

Note that it would have to do that on the source side prior to/or in the discard phase of postcopy entry; any later and the source will ignore those requests (which is not easy to fix - because it's a safeguard against it overwriting new data on the destination by old data in a race).

Dave

Quoted 2 lines

....

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

Reviewed-by on v1 2/2
#16 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration: Extra tracing

In reply to Dr. David Alan Gilbert (git)
Quoted 6 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

A couple more traces that would have made fixing that postcopy bug a bit easier.

Quoted 41 lines
---
 migration/ram.c        | 2 ++
 migration/trace-events | 2 ++
 2 files changed, 4 insertions(+)
migration/ram.c +2 -0
index b4ed41c725..3ac41ccaba 100644
@@ -812,6 +812,7 @@ static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage)
     ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
 
     p = block->host + offset;
+    trace_ram_save_page(block->idstr, (uint64_t)offset, p);
 
     /* In doubt sent page as normal */
     bytes_xmit = 0;
@@ -2614,6 +2615,7 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
                 ret = -EINVAL;
                 break;
             }
+            trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
         }
 
         switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
migration/trace-events +2 -0
index b8f01a218c..5b8ccf301c 100644
@@ -69,8 +69,10 @@ migration_bitmap_sync_start(void) ""
 migration_bitmap_sync_end(uint64_t dirty_pages) "dirty_pages %" PRIu64
 migration_throttle(void) ""
 ram_discard_range(const char *rbname, uint64_t start, size_t len) "%s: start: %" PRIx64 " %zx"
+ram_load_loop(const char *rbname, uint64_t addr, int flags, void *host) "%s: addr: %" PRIx64 " flags: %x host: %p"
 ram_load_postcopy_loop(uint64_t addr, int flags) "@%" PRIx64 " %x"
 ram_postcopy_send_discard_bitmap(void) ""
+ram_save_page(const char *rbname, uint64_t offset, void *host) "%s: offset: %" PRIx64 " host: %p"
 ram_save_queue_pages(const char *rbname, size_t start, size_t len) "%s: start: %zx len: %zx"
 
 # migration/migration.c