From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53141) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xa6zG-0001vs-1l for qemu-devel@nongnu.org; Fri, 03 Oct 2014 13:49:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xa6z9-0001fK-TD for qemu-devel@nongnu.org; Fri, 03 Oct 2014 13:49:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:11200) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xa6z9-0001f9-Lt for qemu-devel@nongnu.org; Fri, 03 Oct 2014 13:49:43 -0400 From: "Dr. David Alan Gilbert (git)" Date: Fri, 3 Oct 2014 18:47:46 +0100 Message-Id: <1412358473-31398-41-git-send-email-dgilbert@redhat.com> In-Reply-To: <1412358473-31398-1-git-send-email-dgilbert@redhat.com> References: <1412358473-31398-1-git-send-email-dgilbert@redhat.com> Subject: [Qemu-devel] [PATCH v4 40/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, lilei@linux.vnet.ibm.com, quintela@redhat.com, cristian.klein@cs.umu.se, amit.shah@redhat.com, yanghy@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. Signed-off-by: Dr. David Alan Gilbert --- arch_init.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 9 deletions(-) diff --git a/arch_init.c b/arch_init.c index 2f4345a..0ba627b 100644 --- a/arch_init.c +++ b/arch_init.c @@ -1458,9 +1458,20 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) return 0; } +/* + * 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) + * rb: Pointer to RAMBlock* that gets filled in with the RB we find + */ static inline void *host_from_stream_offset(QEMUFile *f, + MigrationIncomingState *mis, ram_addr_t offset, - int flags) + int flags, RAMBlock **rb) { static RAMBlock *block = NULL; char id[256]; @@ -1471,8 +1482,11 @@ static inline void *host_from_stream_offset(QEMUFile *f, error_report("Ack, bad migration stream!"); return NULL; } + if (rb) { + *rb = block; + } - return memory_region_get_ram_ptr(block->mr) + offset; + goto gotit; } len = qemu_get_byte(f); @@ -1480,12 +1494,22 @@ static inline void *host_from_stream_offset(QEMUFile *f, id[len] = 0; QTAILQ_FOREACH(block, &ram_list.blocks, next) { - if (!strncmp(id, block->idstr, sizeof(id))) - return memory_region_get_ram_ptr(block->mr) + offset; + if (!strncmp(id, block->idstr, sizeof(id))) { + if (rb) { + *rb = block; + } + goto gotit; + } } error_report("Can't find block %s!", id); return NULL; + +gotit: + postcopy_hook_early_receive(mis, + (offset + (*rb)->offset) >> TARGET_PAGE_BITS); + return memory_region_get_ram_ptr(block->mr) + offset; + } /* @@ -1515,6 +1539,13 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) ram_addr_t addr; int flags, 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 = mis->postcopy_ram_state >= + POSTCOPY_RAM_INCOMING_LISTENING; seq_iter++; @@ -1523,6 +1554,7 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) } while (!ret) { + RAMBlock *rb = 0; /* =0 needed to silence compiler */ addr = qemu_get_be64(f); flags = addr & ~TARGET_PAGE_MASK; @@ -1570,7 +1602,7 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) void *host; uint8_t ch; - host = host_from_stream_offset(f, addr, flags); + host = host_from_stream_offset(f, mis, addr, flags, &rb); if (!host) { error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); ret = -EINVAL; @@ -1578,20 +1610,66 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) } 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 { + if (!ch) { + ret = postcopy_place_zero_page(mis, host, + (addr + rb->offset) >> TARGET_PAGE_BITS); + } else { + void *tmp; + tmp = postcopy_get_tmp_page(mis, (addr + rb->offset) >> + TARGET_PAGE_BITS); + + if (!tmp) { + return -ENOMEM; + } + memset(tmp, ch, TARGET_PAGE_SIZE); + ret = postcopy_place_page(mis, host, tmp, + (addr + rb->offset) >> TARGET_PAGE_BITS); + } + if (ret) { + error_report("ram_load: Failure in postcopy compress @" + "%zx/%p;%s+%zx", + addr, host, rb->idstr, rb->offset); + return ret; + } + } } else if (flags & RAM_SAVE_FLAG_PAGE) { void *host; - host = host_from_stream_offset(f, addr, flags); + host = host_from_stream_offset(f, mis, addr, flags, &rb); if (!host) { error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); ret = -EINVAL; break; } - qemu_get_buffer(f, host, TARGET_PAGE_SIZE); + if (!postcopy_running) { + qemu_get_buffer(f, host, TARGET_PAGE_SIZE); + } else { + void *tmp = postcopy_get_tmp_page(mis, (addr + rb->offset) >> + TARGET_PAGE_BITS); + + if (!tmp) { + return -ENOMEM; + } + qemu_get_buffer(f, tmp, TARGET_PAGE_SIZE); + ret = postcopy_place_page(mis, host, tmp, + (addr + rb->offset) >> TARGET_PAGE_BITS); + if (ret) { + error_report("ram_load: Failure in postcopy simple" + "@%zx/%p;%s+%zx", + addr, host, rb->idstr, rb->offset); + return ret; + } + } } else if (flags & RAM_SAVE_FLAG_XBZRLE) { - void *host = host_from_stream_offset(f, addr, flags); + if (postcopy_running) { + error_report("XBZRLE RAM block in postcopy mode @%zx\n", addr); + return -EINVAL; + } + void *host = host_from_stream_offset(f, mis, addr, flags, &rb); if (!host) { error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); ret = -EINVAL; -- 1.9.3