qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] qemu and transparent huge pages
@ 2012-08-15 12:45 Michael Tokarev
  2012-08-15 12:51 ` Avi Kivity
  0 siblings, 1 reply; 11+ messages in thread
From: Michael Tokarev @ 2012-08-15 12:45 UTC (permalink / raw)
  To: Andrea Arcangeli; +Cc: qemu-devel, Avi Kivity

[Reposting with the right email address of Andrea]

Quite some time ago there was a thread on qemu-devel,
started by Andrea, about modifying qemu to better
use transparent huge pages:

 http://lists.gnu.org/archive/html/qemu-devel/2010-03/msg01250.html

That thread hasn't reached any conclusion, but some time
after that Avi implemented a similar change:

commit 36b586284e678da28df3af9fd0907d2b16f9311c
Author: Avi Kivity <avi@redhat.com>
Date:   Mon Sep 5 11:07:05 2011 +0300

    qemu_vmalloc: align properly for transparent hugepages and KVM

    To make good use of transparent hugepages, KVM requires that guest-physical
    and host-virtual addresses share the low 21 bits (as opposed to just the low
    12 bits normally required).

    Adjust qemu_vmalloc() to honor that requirement.  Ignore it for small region
    to avoid fragmentation.

    Signed-off-by: Avi Kivity <avi@redhat.com>
    Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

diff --git a/oslib-posix.c b/oslib-posix.c
index 196099c..a304fb0 100644
--- a/oslib-posix.c
+++ b/oslib-posix.c
@@ -35,6 +35,13 @@
 extern int daemon(int, int);
 #endif

+#if defined(__linux__) && defined(__x86_64__)
+   /* Use 2MB alignment so transparent hugepages can be used by KVM */
+#  define QEMU_VMALLOC_ALIGN (512 * 4096)
+#else
+#  define QEMU_VMALLOC_ALIGN getpagesize()
+#endif
+
 #include "config-host.h"
 #include "sysemu.h"
 #include "trace.h"
@@ -80,7 +87,12 @@ void *qemu_memalign(size_t alignment, size_t size)
 void *qemu_vmalloc(size_t size)
 {
     void *ptr;
-    ptr = qemu_memalign(getpagesize(), size);
+    size_t align = QEMU_VMALLOC_ALIGN;
+
+    if (size < align) {
+        align = getpagesize();
+    }
+    ptr = qemu_memalign(align, size);
     trace_qemu_vmalloc(size, ptr);
     return ptr;
 }


(why it is 64bit-only is a different, unrelated question).

But apparently, THP does not work still, even with 2Mb
alignment: when running a guest, AnonHugePages in
/proc/meminfo stays at 0 - either in kvm mode or in tcg
mode.  Any idea why?  What else is needed for THP to work?

This is quite a frequent question in #kvm IRC channel,
and I always suggested using -mem-path for this,  but
I'm curios why it doesn't work automatically when it
probably should?

Thanks,

/mjt

^ permalink raw reply related	[flat|nested] 11+ messages in thread
* [Qemu-devel] qemu and transparent huge pages
@ 2012-08-15 12:40 Michael Tokarev
  0 siblings, 0 replies; 11+ messages in thread
From: Michael Tokarev @ 2012-08-15 12:40 UTC (permalink / raw)
  To: Andrea Arcangeli; +Cc: qemu-devel, Avi Kivity

Quite some time ago there was a thread on qemu-devel,
started by Andrea, about modifying qemu to better
use transparent huge pages:

 http://lists.gnu.org/archive/html/qemu-devel/2010-03/msg01250.html

That thread hasn't reached any conclusion, but some time
after that Avi implemented a similar change:

commit 36b586284e678da28df3af9fd0907d2b16f9311c
Author: Avi Kivity <avi@redhat.com>
Date:   Mon Sep 5 11:07:05 2011 +0300

    qemu_vmalloc: align properly for transparent hugepages and KVM

    To make good use of transparent hugepages, KVM requires that guest-physical
    and host-virtual addresses share the low 21 bits (as opposed to just the low
    12 bits normally required).

    Adjust qemu_vmalloc() to honor that requirement.  Ignore it for small region
    to avoid fragmentation.

    Signed-off-by: Avi Kivity <avi@redhat.com>
    Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

diff --git a/oslib-posix.c b/oslib-posix.c
index 196099c..a304fb0 100644
--- a/oslib-posix.c
+++ b/oslib-posix.c
@@ -35,6 +35,13 @@
 extern int daemon(int, int);
 #endif

+#if defined(__linux__) && defined(__x86_64__)
+   /* Use 2MB alignment so transparent hugepages can be used by KVM */
+#  define QEMU_VMALLOC_ALIGN (512 * 4096)
+#else
+#  define QEMU_VMALLOC_ALIGN getpagesize()
+#endif
+
 #include "config-host.h"
 #include "sysemu.h"
 #include "trace.h"
@@ -80,7 +87,12 @@ void *qemu_memalign(size_t alignment, size_t size)
 void *qemu_vmalloc(size_t size)
 {
     void *ptr;
-    ptr = qemu_memalign(getpagesize(), size);
+    size_t align = QEMU_VMALLOC_ALIGN;
+
+    if (size < align) {
+        align = getpagesize();
+    }
+    ptr = qemu_memalign(align, size);
     trace_qemu_vmalloc(size, ptr);
     return ptr;
 }


(why it is 64bit-only is a different, unrelated question).

But apparently, THP does not work still, even with 2Mb
alignment: when running a guest, AnonHugePages in
/proc/meminfo stays at 0 - either in kvm mode or in tcg
mode.  Any idea why?  What else is needed for THP to work?

This is quite a frequent question in #kvm IRC channel,
and I always suggested using -mem-path for this,  but
I'm curios why it doesn't work automatically when it
probably should?

Thanks,

/mjt

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

end of thread, other threads:[~2012-11-13 16:39 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-15 12:45 [Qemu-devel] qemu and transparent huge pages Michael Tokarev
2012-08-15 12:51 ` Avi Kivity
2012-08-15 14:22   ` Michael Tokarev
2012-08-15 14:26     ` Avi Kivity
2012-08-15 15:03       ` Michael Tokarev
2012-08-15 15:06         ` Michael Tokarev
2012-09-16 11:19         ` Michael Tokarev
2012-11-12 15:18           ` Michael Tokarev
2012-11-13 14:30             ` Aurelien Jarno
2012-11-13 16:38               ` Michael Tokarev
  -- strict thread matches above, loose matches on Subject: below --
2012-08-15 12:40 Michael Tokarev

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).