From patchwork Tue Apr 9 12:52:34 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 235069 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 388552C00AB for ; Tue, 9 Apr 2013 22:53:52 +1000 (EST) Received: from localhost ([::1]:59797 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UPY3a-0006rI-Eq for incoming@patchwork.ozlabs.org; Tue, 09 Apr 2013 08:53:50 -0400 Received: from eggs.gnu.org ([208.118.235.92]:57125) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UPY3M-0006rC-4E for qemu-devel@nongnu.org; Tue, 09 Apr 2013 08:53:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UPY3K-000506-Iy for qemu-devel@nongnu.org; Tue, 09 Apr 2013 08:53:35 -0400 Received: from mail-qa0-f52.google.com ([209.85.216.52]:57136) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UPY3K-0004zx-Ec for qemu-devel@nongnu.org; Tue, 09 Apr 2013 08:53:34 -0400 Received: by mail-qa0-f52.google.com with SMTP id bs12so1695267qab.4 for ; Tue, 09 Apr 2013 05:53:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:cc:subject:date:message-id:x-mailer; bh=9+Oe34FL77hOeRsS26CFiVNcAh2lv733BzVRZ3bFRWc=; b=hT6EYhpfEoyZf9le/lierj2bhIZO0FFvDF3pnemnG+dngp9+ZVsjoYQ5CqTQ/+mAGz vHGxV6eIUKxW75XQjU3uKx+IHQUElDm8mDrMlDw1OAZQzWjWz9BvZEmjjWKfSqOEqHeT vHoWHzf7pFkbqHe+pANLDbZX0/H+h6ajKmUHa3KjSHLkjIifMPSCSZYexhirEgVvcV2I Gc6fp3WuSbKMK5vKIOdMTjUOpHnv6ratcBtvlhkUYqNEEu4xyITC55hv0qTqz6139nQc KNDeHpXC4GG2CkK7moOwq8v+8t2DfPN1Q0eEvnv92swkcM4re56m8C2PEOjNb7q5kjkf wM0w== X-Received: by 10.49.60.1 with SMTP id d1mr23576847qer.33.1365512014000; Tue, 09 Apr 2013 05:53:34 -0700 (PDT) Received: from yakj.usersys.redhat.com (93-34-176-20.ip50.fastwebnet.it. [93.34.176.20]) by mx.google.com with ESMTPS id c3sm5879477qed.1.2013.04.09.05.53.31 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 09 Apr 2013 05:53:33 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 9 Apr 2013 14:52:34 +0200 Message-Id: <1365511954-10606-1-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.2 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.216.52 Cc: kwolf@redhat.com, aliguori@us.ibm.com, quintela@redhat.com, pl@kamp.de, armbru@redhat.com, owasserm@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH v2] migration: initialize RAM to zero 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 Using qemu_memalign only leaves the RAM zero by chance, because libc will usually use mmap to satisfy our huge requests. But memory will not be zero when using MALLOC_PERTURB_ with a nonzero value. In the case of incoming migration, this breaks a recently-introduced invariant (commit f1c7279, migration: do not sent zero pages in bulk stage, 2013-03-26). To fix this, use mmap ourselves to get a well-aligned, always zero block for the RAM. Mmap-ed memory is easy to "trim" at the sides. This also removes the need to do something special on valgrind (see commit c2a8238a, Support running QEMU on Valgrind, 2011-10-31), thus effectively reverts that patch. Signed-off-by: Paolo Bonzini Reviewed-by: Juan Quintela --- v1->v2: drop CONFIG_VALGRIND [Markus], test mmap return value [Juan] util/oslib-posix.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 4e4b819..8538509 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -40,7 +40,6 @@ extern int daemon(int, int); Valgrind does not support alignments larger than 1 MiB, therefore we need special code which handles running on Valgrind. */ # define QEMU_VMALLOC_ALIGN (512 * 4096) -# define CONFIG_VALGRIND #elif defined(__linux__) && defined(__s390x__) /* Use 1 MiB (segment size) alignment so gmap can be used by KVM. */ # define QEMU_VMALLOC_ALIGN (256 * 4096) @@ -52,12 +51,8 @@ extern int daemon(int, int); #include "sysemu/sysemu.h" #include "trace.h" #include "qemu/sockets.h" +#include -#if defined(CONFIG_VALGRIND) -static int running_on_valgrind = -1; -#else -# define running_on_valgrind 0 -#endif #ifdef CONFIG_LINUX #include #endif @@ -108,22 +103,28 @@ void *qemu_memalign(size_t alignment, size_t size) /* alloc shared memory pages */ void *qemu_vmalloc(size_t size) { - void *ptr; size_t align = QEMU_VMALLOC_ALIGN; + size_t total = size + align - getpagesize(); + void *ptr = mmap(0, total, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + size_t offset = QEMU_ALIGN_UP((uintptr_t)ptr, align) - (uintptr_t)ptr; -#if defined(CONFIG_VALGRIND) - if (running_on_valgrind < 0) { - /* First call, test whether we are running on Valgrind. - This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */ - const char *ld = getenv("LD_PRELOAD"); - running_on_valgrind = (ld != NULL && strstr(ld, "vgpreload")); + if ((intptr_t) ptr == -1) { + fprintf(stderr, "Failed to allocate %zu B: %s\n", + size, strerror(errno)); + abort(); } -#endif - if (size < align || running_on_valgrind) { - align = getpagesize(); + ptr += offset; + total -= offset; + + if (offset > 0) { + munmap(ptr - offset, offset); } - ptr = qemu_memalign(align, size); + if (total > size) { + munmap(ptr + size, total - size); + } + trace_qemu_vmalloc(size, ptr); return ptr; }