cd9d161
From d12ac10d3ce10d3e1c9b23aeca397caa74be49d3 Mon Sep 17 00:00:00 2001
cd9d161
From: Avi Kivity <avi@redhat.com>
cd9d161
Date: Mon, 5 Sep 2011 11:07:05 +0300
cd9d161
Subject: [PATCH] qemu_vmalloc: align properly for transparent hugepages and
cd9d161
 KVM
cd9d161
MIME-Version: 1.0
cd9d161
Content-Type: text/plain; charset=UTF-8
cd9d161
Content-Transfer-Encoding: 8bit
cd9d161
cd9d161
To make good use of transparent hugepages, KVM requires that guest-physical
cd9d161
and host-virtual addresses share the low 21 bits (as opposed to just the low
cd9d161
12 bits normally required).
cd9d161
cd9d161
Adjust qemu_vmalloc() to honor that requirement.  Ignore it for small regions
cd9d161
to avoid fragmentation.
cd9d161
cd9d161
Signed-off-by: Avi Kivity <avi@redhat.com>
cd9d161
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
cd9d161
(cherry picked from commit 36b586284e678da28df3af9fd0907d2b16f9311c)
cd9d161
cd9d161
Signed-off-by: Bruce Rogers <brogers@suse.com>
cd9d161
Signed-off-by: Andreas Färber <afaerber@suse.de>
cd9d161
---
cd9d161
 oslib-posix.c | 14 +++++++++++++-
cd9d161
 1 file changed, 13 insertions(+), 1 deletion(-)
cd9d161
cd9d161
diff --git a/oslib-posix.c b/oslib-posix.c
cd9d161
index 196099c..a304fb0 100644
cd9d161
--- a/oslib-posix.c
cd9d161
+++ b/oslib-posix.c
cd9d161
@@ -35,6 +35,13 @@
cd9d161
 extern int daemon(int, int);
cd9d161
 #endif
cd9d161
 
cd9d161
+#if defined(__linux__) && defined(__x86_64__)
cd9d161
+   /* Use 2MB alignment so transparent hugepages can be used by KVM */
cd9d161
+#  define QEMU_VMALLOC_ALIGN (512 * 4096)
cd9d161
+#else
cd9d161
+#  define QEMU_VMALLOC_ALIGN getpagesize()
cd9d161
+#endif
cd9d161
+
cd9d161
 #include "config-host.h"
cd9d161
 #include "sysemu.h"
cd9d161
 #include "trace.h"
cd9d161
@@ -80,7 +87,12 @@ void *qemu_memalign(size_t alignment, size_t size)
cd9d161
 void *qemu_vmalloc(size_t size)
cd9d161
 {
cd9d161
     void *ptr;
cd9d161
-    ptr = qemu_memalign(getpagesize(), size);
cd9d161
+    size_t align = QEMU_VMALLOC_ALIGN;
cd9d161
+
cd9d161
+    if (size < align) {
cd9d161
+        align = getpagesize();
cd9d161
+    }
cd9d161
+    ptr = qemu_memalign(align, size);
cd9d161
     trace_qemu_vmalloc(size, ptr);
cd9d161
     return ptr;
cd9d161
 }
cd9d161
-- 
cd9d161
1.7.11.2
cd9d161