qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] migration: initialize RAM to zero
@ 2013-04-09 12:52 Paolo Bonzini
  2013-04-09 13:20 ` Markus Armbruster
  0 siblings, 1 reply; 5+ messages in thread
From: Paolo Bonzini @ 2013-04-09 12:52 UTC (permalink / raw)
  To: qemu-devel; +Cc: kwolf, aliguori, quintela, pl, armbru, owasserm, stefanha

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 <pbonzini@redhat.com>
---
        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 <sys/mman.h>
 
-#if defined(CONFIG_VALGRIND)
-static int running_on_valgrind = -1;
-#else
-#  define running_on_valgrind 0
-#endif
 #ifdef CONFIG_LINUX
 #include <sys/syscall.h>
 #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;
 }
-- 
1.8.2

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2] migration: initialize RAM to zero
  2013-04-09 12:52 [Qemu-devel] [PATCH v2] migration: initialize RAM to zero Paolo Bonzini
@ 2013-04-09 13:20 ` Markus Armbruster
  2013-04-09 14:56   ` Paolo Bonzini
  0 siblings, 1 reply; 5+ messages in thread
From: Markus Armbruster @ 2013-04-09 13:20 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kwolf, aliguori, quintela, pl, qemu-devel, owasserm, stefanha

Paolo Bonzini <pbonzini@redhat.com> writes:

> 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 <pbonzini@redhat.com>
> ---
>         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 <sys/mman.h>
>  
> -#if defined(CONFIG_VALGRIND)
> -static int running_on_valgrind = -1;
> -#else
> -#  define running_on_valgrind 0
> -#endif
>  #ifdef CONFIG_LINUX
>  #include <sys/syscall.h>
>  #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) {

Recommend ptr == MAP_FAILED

> +        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;
>  }

Looks good otherwise.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2] migration: initialize RAM to zero
  2013-04-09 13:20 ` Markus Armbruster
@ 2013-04-09 14:56   ` Paolo Bonzini
  2013-04-09 15:04     ` Markus Armbruster
  2013-04-09 15:22     ` Juan Quintela
  0 siblings, 2 replies; 5+ messages in thread
From: Paolo Bonzini @ 2013-04-09 14:56 UTC (permalink / raw)
  To: Markus Armbruster
  Cc: kwolf, aliguori, quintela, pl, qemu-devel, owasserm, stefanha

Il 09/04/2013 15:20, Markus Armbruster ha scritto:
>> > -#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) {
> Recommend ptr == MAP_FAILED
> 

Worth respinning?

Paolo

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2] migration: initialize RAM to zero
  2013-04-09 14:56   ` Paolo Bonzini
@ 2013-04-09 15:04     ` Markus Armbruster
  2013-04-09 15:22     ` Juan Quintela
  1 sibling, 0 replies; 5+ messages in thread
From: Markus Armbruster @ 2013-04-09 15:04 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kwolf, aliguori, quintela, pl, qemu-devel, owasserm, stefanha

Paolo Bonzini <pbonzini@redhat.com> writes:

> Il 09/04/2013 15:20, Markus Armbruster ha scritto:
>>> > -#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) {
>> Recommend ptr == MAP_FAILED
>> 
>
> Worth respinning?

I'd do it, but it's really your choice.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2] migration: initialize RAM to zero
  2013-04-09 14:56   ` Paolo Bonzini
  2013-04-09 15:04     ` Markus Armbruster
@ 2013-04-09 15:22     ` Juan Quintela
  1 sibling, 0 replies; 5+ messages in thread
From: Juan Quintela @ 2013-04-09 15:22 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: kwolf, aliguori, pl, Markus Armbruster, qemu-devel, owasserm,
	stefanha

Paolo Bonzini <pbonzini@redhat.com> wrote:
> Il 09/04/2013 15:20, Markus Armbruster ha scritto:
>>> > -#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) {
>> Recommend ptr == MAP_FAILED
>> 
>
> Worth respinning?

Do it,  and put the:

Reviewed-by: Juan Quintela <quintela@redhat.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-04-09 15:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-09 12:52 [Qemu-devel] [PATCH v2] migration: initialize RAM to zero Paolo Bonzini
2013-04-09 13:20 ` Markus Armbruster
2013-04-09 14:56   ` Paolo Bonzini
2013-04-09 15:04     ` Markus Armbruster
2013-04-09 15:22     ` Juan Quintela

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).