From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50297) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yi4HW-0007Lh-DE for qemu-devel@nongnu.org; Tue, 14 Apr 2015 13:05:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yi4HU-0002zu-9a for qemu-devel@nongnu.org; Tue, 14 Apr 2015 13:05:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57895) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yi4HT-0002ze-Q8 for qemu-devel@nongnu.org; Tue, 14 Apr 2015 13:05:48 -0400 From: "Dr. David Alan Gilbert (git)" Date: Tue, 14 Apr 2015 18:04:04 +0100 Message-Id: <1429031053-4454-39-git-send-email-dgilbert@redhat.com> In-Reply-To: <1429031053-4454-1-git-send-email-dgilbert@redhat.com> References: <1429031053-4454-1-git-send-email-dgilbert@redhat.com> Subject: [Qemu-devel] [PATCH v6 38/47] Postcopy: Use helpers to map pages during migration List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aarcange@redhat.com, yamahata@private.email.ne.jp, quintela@redhat.com, amit.shah@redhat.com, pbonzini@redhat.com, david@gibson.dropbear.id.au, yayanghy@cn.fujitsu.com From: "Dr. David Alan Gilbert" In postcopy, the destination guest is running at the same time as it's receiving pages; as we receive new pages we must put them into the guests address space atomically to avoid a running CPU accessing a partially written page. Use the helpers in postcopy-ram.c to map these pages. qemu_get_buffer_less_copy is used to avoid a copy out of qemu_file in the case that postcopy is going to do a copy anyway. Signed-off-by: Dr. David Alan Gilbert --- arch_init.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 20 deletions(-) diff --git a/arch_init.c b/arch_init.c index c96c4c1..0d3e865 100644 --- a/arch_init.c +++ b/arch_init.c @@ -1476,7 +1476,17 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) /* Must be called from within a rcu critical section. * Returns a pointer from within the RCU-protected ram_list. */ +/* + * Read a RAMBlock ID from the stream f, find the host address of the + * start of that block and add on 'offset' + * + * f: Stream to read from + * mis: MigrationIncomingState + * offset: Offset within the block + * flags: Page flags (mostly to see if it's a continuation of previous block) + */ static inline void *host_from_stream_offset(QEMUFile *f, + MigrationIncomingState *mis, ram_addr_t offset, int flags) { @@ -1489,7 +1499,6 @@ static inline void *host_from_stream_offset(QEMUFile *f, error_report("Ack, bad migration stream!"); return NULL; } - return memory_region_get_ram_ptr(block->mr) + offset; } @@ -1534,6 +1543,16 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) { int flags = 0, ret = 0; static uint64_t seq_iter; + /* + * System is running in postcopy mode, page inserts to host memory must be + * atomic + */ + MigrationIncomingState *mis = migration_incoming_get_current(); + bool postcopy_running = postcopy_state_get(mis) >= + POSTCOPY_INCOMING_LISTENING; + void *postcopy_host_page = NULL; + bool postcopy_place_needed = false; + bool matching_page_sizes = qemu_host_page_size == TARGET_PAGE_SIZE; seq_iter++; @@ -1549,13 +1568,57 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) rcu_read_lock(); while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { ram_addr_t addr, total_ram_bytes; - void *host; + void *host = 0; + void *page_buffer = 0; + void *postcopy_place_source = 0; uint8_t ch; + bool all_zero = false; addr = qemu_get_be64(f); flags = addr & ~TARGET_PAGE_MASK; addr &= TARGET_PAGE_MASK; + if (flags & (RAM_SAVE_FLAG_COMPRESS | RAM_SAVE_FLAG_PAGE | + RAM_SAVE_FLAG_XBZRLE)) { + host = host_from_stream_offset(f, mis, addr, flags); + if (!host) { + error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); + ret = -EINVAL; + break; + } + if (!postcopy_running) { + page_buffer = host; + } else { + /* + * Postcopy requires that we place whole host pages atomically. + * To make it atomic, the data is read into a temporary page + * that's moved into place later. + * The migration protocol uses, possibly smaller, target-pages + * however the source ensures it always sends all the components + * of a host page in order. + */ + if (!postcopy_host_page) { + postcopy_host_page = postcopy_get_tmp_page(mis); + } + page_buffer = postcopy_host_page + + ((uintptr_t)host & ~qemu_host_page_mask); + /* If all TP are zero then we can optimise the place */ + if (!((uintptr_t)host & ~qemu_host_page_mask)) { + all_zero = true; + } + + /* + * If it's the last part of a host page then we place the host + * page + */ + postcopy_place_needed = (((uintptr_t)host + TARGET_PAGE_SIZE) & + ~qemu_host_page_mask) == 0; + postcopy_place_source = postcopy_host_page; + } + } else { + postcopy_place_needed = false; + } + switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { case RAM_SAVE_FLAG_MEM_SIZE: /* Synchronize RAM block list */ @@ -1592,30 +1655,36 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) } break; case RAM_SAVE_FLAG_COMPRESS: - host = host_from_stream_offset(f, addr, flags); - if (!host) { - error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); - ret = -EINVAL; - break; - } ch = qemu_get_byte(f); - ram_handle_compressed(host, ch, TARGET_PAGE_SIZE); + if (!postcopy_running) { + ram_handle_compressed(host, ch, TARGET_PAGE_SIZE); + } else { + memset(page_buffer, ch, TARGET_PAGE_SIZE); + if (ch) { + all_zero = false; + } + } break; + case RAM_SAVE_FLAG_PAGE: - host = host_from_stream_offset(f, addr, flags); - if (!host) { - error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); - ret = -EINVAL; - break; + all_zero = false; + if (!postcopy_place_needed || !matching_page_sizes) { + qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE); + } else { + /* Avoids the qemu_file copy during postcopy, which is + * going to do a copy later; can only do it when we + * do this read in one go (matching page sizes) + */ + qemu_get_buffer_less_copy(f, (uint8_t **)&postcopy_place_source, + TARGET_PAGE_SIZE); } - qemu_get_buffer(f, host, TARGET_PAGE_SIZE); break; + case RAM_SAVE_FLAG_XBZRLE: - host = host_from_stream_offset(f, addr, flags); - if (!host) { - error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); - ret = -EINVAL; - break; + all_zero = false; + if (postcopy_running) { + error_report("XBZRLE RAM block in postcopy mode @%zx\n", addr); + return -EINVAL; } if (load_xbzrle(f, addr, host) < 0) { error_report("Failed to decompress XBZRLE page at " @@ -1636,6 +1705,14 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) ret = -EINVAL; } } + + if (postcopy_place_needed) { + /* This gets called at the last target page in the host page */ + ret = postcopy_place_page(mis, host + TARGET_PAGE_SIZE - + qemu_host_page_size, + postcopy_place_source, + all_zero); + } if (!ret) { ret = qemu_file_get_error(f); } -- 2.1.0