From patchwork Fri Jul 4 17:41:50 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Dr. David Alan Gilbert" X-Patchwork-Id: 367179 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5F4141400D6 for ; Sat, 5 Jul 2014 04:00:23 +1000 (EST) Received: from localhost ([::1]:37511 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X37mV-0004Gb-Kk for incoming@patchwork.ozlabs.org; Fri, 04 Jul 2014 14:00:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:32987) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X37Vz-0008CU-3r for qemu-devel@nongnu.org; Fri, 04 Jul 2014 13:43:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X37Vs-0000bS-7m for qemu-devel@nongnu.org; Fri, 04 Jul 2014 13:43:15 -0400 Received: from mx1.redhat.com ([209.132.183.28]:11831) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X37Vr-0000bB-TD for qemu-devel@nongnu.org; Fri, 04 Jul 2014 13:43:08 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s64Hh5pw009999 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 4 Jul 2014 13:43:05 -0400 Received: from dgilbert-t530.home.treblig.org (vpn1-7-141.ams2.redhat.com [10.36.7.141]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s64HfvU8030576; Fri, 4 Jul 2014 13:43:03 -0400 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org Date: Fri, 4 Jul 2014 18:41:50 +0100 Message-Id: <1404495717-4239-40-git-send-email-dgilbert@redhat.com> In-Reply-To: <1404495717-4239-1-git-send-email-dgilbert@redhat.com> References: <1404495717-4239-1-git-send-email-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: aarcange@redhat.com, yamahata@private.email.ne.jp, lilei@linux.vnet.ibm.com, quintela@redhat.com Subject: [Qemu-devel] [PATCH 39/46] Postcopy: Use helpers to map pages during migration X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/arch_init.c b/arch_init.c index 58eccc1..b971f47 100644 --- a/arch_init.c +++ b/arch_init.c @@ -1328,9 +1328,18 @@ 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 + * 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, ram_addr_t offset, - int flags) + int flags, RAMBlock **rb) { static RAMBlock *block = NULL; char id[256]; @@ -1341,6 +1350,9 @@ 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; } @@ -1350,8 +1362,12 @@ 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))) + if (!strncmp(id, block->idstr, sizeof(id))) { + if (rb) { + *rb = block; + } return memory_region_get_ram_ptr(block->mr) + offset; + } } error_report("Can't find block %s!", id); @@ -1385,6 +1401,12 @@ 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 + */ + bool postcopy_running = f->mis->postcopy_ram_state >= + POSTCOPY_RAM_INCOMING_LISTENING; seq_iter++; @@ -1439,8 +1461,9 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) } else if (flags & RAM_SAVE_FLAG_COMPRESS) { void *host; uint8_t ch; + RAMBlock *rb; - host = host_from_stream_offset(f, addr, flags); + host = host_from_stream_offset(f, addr, flags, &rb); if (!host) { error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); ret = -EINVAL; @@ -1448,20 +1471,63 @@ 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(f->mis, host, + (addr + rb->offset) >> TARGET_PAGE_BITS); + } else { + void *tmp; + tmp = postcopy_get_tmp_page(f->mis); + if (!tmp) { + return -ENOMEM; + } + memset(tmp, ch, TARGET_PAGE_SIZE); + ret = postcopy_place_page(f->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; + RAMBlock *rb; - host = host_from_stream_offset(f, addr, flags); + host = host_from_stream_offset(f, 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(f->mis); + if (!tmp) { + return -ENOMEM; + } + qemu_get_buffer(f, tmp, TARGET_PAGE_SIZE); + ret = postcopy_place_page(f->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, addr, flags, NULL); if (!host) { error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); ret = -EINVAL;