Skip to contentSkip to conversation

qemu_vmalloc: align properly for transparent hugepages and KVM

PATCH Applied Avi Kivity 3 participants 4 messages Sep 5, 2011 – Sep 9, 2011

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH 1/1 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 regions to avoid fragmentation.

---
 oslib-posix.c |   14 +++++++++++++-
 1 files changed, 13 insertions(+), 1 deletions(-)
oslib-posix.c +13 -1
index 196099c..a304fb0 100644
@@ -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 @@ int qemu_daemon(int nochdir, int noclose)
 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;
 }
Signature · 1 line
1.7.6.1
#1 lore raw
Quoted 17 lines · Avi Kivity

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 regions to avoid fragmentation.

---
 oslib-posix.c |   14 +++++++++++++-
 1 files changed, 13 insertions(+), 1 deletions(-)
oslib-posix.c +0 -0
index 196099c..a304fb0 100644

oslib-posix.c line 39

Quoted 5 lines
oslib-posix.c +2 -0
@@ -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 */

Aren't transparent hugepages also available in TCG mode? Then just remove "by KVM" from subject and comment.

Jan

Quoted 22 lines
oslib-posix.c +11 -1
+#  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 @@ int qemu_daemon(int nochdir, int noclose)
 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;
 }
Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#2 lore raw
Quoted 21 lines · Jan Kiszka
Quoted 20 lines · Avi Kivity

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 regions to avoid fragmentation.

---

  oslib-posix.c |   14 +++++++++++++-
  1 files changed, 13 insertions(+), 1 deletions(-)

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

Quoted 5 lines
Quoted 2 lines

+#if defined(__linux__)&& defined(__x86_64__)
+ /* Use 2MB alignment so transparent hugepages can be used by KVM */

Aren't transparent hugepages also available in TCG mode? Then just remove "by KVM" from subject and comment.

They are, but they don't require the special alignment.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw
In reply to Avi Kivity
Quoted 8 lines · Avi Kivity

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 regions to avoid fragmentation.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 36 lines
---
  oslib-posix.c |   14 +++++++++++++-
  1 files changed, 13 insertions(+), 1 deletions(-)
oslib-posix.c +13 -1
index 196099c..a304fb0 100644
@@ -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 @@ int qemu_daemon(int nochdir, int noclose)
  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;
  }