Skip to contentSkip to conversation

Helpers to obtain host page sizes for guest RAM

PATCH In review David Gibson 2 participants 5 messages Apr 11, 2018 – Apr 12, 2018 Arm STM32

lore
Share & tools
lore
#0 lore raw

v3 cover Helpers to obtain host page sizes for guest RAM

This series makes some small changes to make it easier to obtain the host page size backing given portions of guest RAM. We use this in a couple of places currently, and I have one or two more to add in some upcoming code.

Assuming there are no objections, what should be the procedure for staging this? Can I put it in my for-2.13 ppc tree, even though it's technically generic code, or should it go through someone else's tree?

Changes since v2:
* Corrected grammar error in commit message
* Correct build error on non-linux hosts
Changes since v1:
* Reworked design based on Eduardo's feedback
* Split into two patches

David Gibson (2):
Make qemu_mempath_getpagesize() accept NULL
Add host_memory_backend_pagesize() helper

 backends/hostmem.c       | 18 ++++++++++++++++++
 exec.c                   | 20 +++++---------------
 include/sysemu/hostmem.h |  2 ++
 target/ppc/kvm.c         | 10 +---------
 util/mmap-alloc.c        | 26 ++++++++++++++------------
 5 files changed, 40 insertions(+), 36 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH v3 1/2 Make qemu_mempath_getpagesize() accept NULL

qemu_mempath_getpagesize() gets the effective (host side) page size for a block of memory backed by an mmap()ed file on the host. It requires the mem_path parameter to be non-NULL.

This ends up meaning all the callers need a different case for handling anonymous memory (for memory-backend-ram or default memory with -mem-path is not specified).

We can make all those callers a little simpler by having qemu_mempath_getpagesize() accept NULL, and treat that as the anonymous memory case.

---
 exec.c            | 21 ++++++---------------
 target/ppc/kvm.c  |  8 ++------
 util/mmap-alloc.c | 26 ++++++++++++++------------
 3 files changed, 22 insertions(+), 33 deletions(-)
exec.c +6 -15
index 02b1efebb7..b38b004563 100644
@@ -1488,19 +1488,14 @@ void ram_block_dump(Monitor *mon)
  */
 static int find_max_supported_pagesize(Object *obj, void *opaque)
 {
-    char *mem_path;
     long *hpsize_min = opaque;
 
     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
-        mem_path = object_property_get_str(obj, "mem-path", NULL);
-        if (mem_path) {
-            long hpsize = qemu_mempath_getpagesize(mem_path);
-            g_free(mem_path);
-            if (hpsize < *hpsize_min) {
-                *hpsize_min = hpsize;
-            }
-        } else {
-            *hpsize_min = getpagesize();
+        char *mem_path = object_property_get_str(obj, "mem-path", NULL);
+        long hpsize = qemu_mempath_getpagesize(mem_path);
+        g_free(mem_path);
+        if (hpsize < *hpsize_min) {
+            *hpsize_min = hpsize;
         }
     }
 
@@ -1513,11 +1508,7 @@ long qemu_getrampagesize(void)
     long mainrampagesize;
     Object *memdev_root;
 
-    if (mem_path) {
-        mainrampagesize = qemu_mempath_getpagesize(mem_path);
-    } else {
-        mainrampagesize = getpagesize();
-    }
+    mainrampagesize = qemu_mempath_getpagesize(mem_path);
 
     /* it's possible we have memory-backend objects with
      * hugepage-backed RAM. these may get mapped into system
target/ppc/kvm.c +2 -6
index 79a436a384..e24fa50dc9 100644
@@ -499,12 +499,8 @@ bool kvmppc_is_mem_backend_page_size_ok(const char *obj_path)
     char *mempath = object_property_get_str(mem_obj, "mem-path", NULL);
     long pagesize;
 
-    if (mempath) {
-        pagesize = qemu_mempath_getpagesize(mempath);
-        g_free(mempath);
-    } else {
-        pagesize = getpagesize();
-    }
+    pagesize = qemu_mempath_getpagesize(mempath);
+    g_free(mempath);
 
     return pagesize >= max_cpu_page_size;
 }
util/mmap-alloc.c +14 -12
index 2fd8cbcc6f..fd329eccd8 100644
@@ -50,19 +50,21 @@ size_t qemu_mempath_getpagesize(const char *mem_path)
     struct statfs fs;
     int ret;
 
-    do {
-        ret = statfs(mem_path, &fs);
-    } while (ret != 0 && errno == EINTR);
-
-    if (ret != 0) {
-        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
-                strerror(errno));
-        exit(1);
-    }
+    if (mem_path) {
+        do {
+            ret = statfs(mem_path, &fs);
+        } while (ret != 0 && errno == EINTR);
 
-    if (fs.f_type == HUGETLBFS_MAGIC) {
-        /* It's hugepage, return the huge page size */
-        return fs.f_bsize;
+        if (ret != 0) {
+            fprintf(stderr, "Couldn't statfs() memory path: %s\n",
+                    strerror(errno));
+            exit(1);
+        }
+
+        if (fs.f_type == HUGETLBFS_MAGIC) {
+            /* It's hugepage, return the huge page size */
+            return fs.f_bsize;
+        }
     }
 #ifdef __sparc__
     /* SPARC Linux needs greater alignment than the pagesize */
Signature · 1 line
2.14.3
#2 lore raw

PATCH v3 2/2 Add host_memory_backend_pagesize() helper

In reply to David Gibson

There are a couple places (one generic, one target specific) where we need to get the host page size associated with a particular memory backend. I have some upcoming code which will add another place which wants this. So, for convenience, add a helper function to calculate this.

host_memory_backend_pagesize() returns the host pagesize for a given HostMemoryBackend object.

---
 backends/hostmem.c       | 18 ++++++++++++++++++
 exec.c                   |  5 ++---
 include/sysemu/hostmem.h |  2 ++
 target/ppc/kvm.c         |  6 +-----
 4 files changed, 23 insertions(+), 8 deletions(-)
backends/hostmem.c +18 -0
index f61093654e..6a0c474222 100644
@@ -18,6 +18,7 @@
 #include "qapi/visitor.h"
 #include "qemu/config-file.h"
 #include "qom/object_interfaces.h"
+#include "qemu/mmap-alloc.h"
 
 #ifdef CONFIG_NUMA
 #include <numaif.h>
@@ -262,6 +263,23 @@ bool host_memory_backend_is_mapped(HostMemoryBackend *backend)
     return backend->is_mapped;
 }
 
+#ifdef __linux__
+size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
+{
+    Object *obj = OBJECT(memdev);
+    char *path = object_property_get_str(obj, "mem-path", NULL);
+    size_t pagesize = qemu_mempath_getpagesize(path);
+
+    g_free(path);
+    return pagesize;
+}
+#else
+size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
+{
+    return getpagesize();
+}
+#endif
+
 static void
 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
 {
exec.c +2 -3
index b38b004563..c7fcefa851 100644
@@ -1491,9 +1491,8 @@ static int find_max_supported_pagesize(Object *obj, void *opaque)
     long *hpsize_min = opaque;
 
     if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) {
-        char *mem_path = object_property_get_str(obj, "mem-path", NULL);
-        long hpsize = qemu_mempath_getpagesize(mem_path);
-        g_free(mem_path);
+        long hpsize = host_memory_backend_pagesize(MEMORY_BACKEND(obj));
+
         if (hpsize < *hpsize_min) {
             *hpsize_min = hpsize;
         }
include/sysemu/hostmem.h +2 -0
index 47bc9846ac..bc36899bb8 100644
@@ -68,4 +68,6 @@ MemoryRegion *host_memory_backend_get_memory(HostMemoryBackend *backend,
 
 void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped);
 bool host_memory_backend_is_mapped(HostMemoryBackend *backend);
+size_t host_memory_backend_pagesize(HostMemoryBackend *memdev);
+
 #endif
target/ppc/kvm.c +1 -5
index e24fa50dc9..f62f7ac288 100644
@@ -496,11 +496,7 @@ static void kvm_fixup_page_sizes(PowerPCCPU *cpu)
 bool kvmppc_is_mem_backend_page_size_ok(const char *obj_path)
 {
     Object *mem_obj = object_resolve_path(obj_path, NULL);
-    char *mempath = object_property_get_str(mem_obj, "mem-path", NULL);
-    long pagesize;
-
-    pagesize = qemu_mempath_getpagesize(mempath);
-    g_free(mempath);
+    long pagesize = host_memory_backend_pagesize(MEMORY_BACKEND(mem_obj));
 
     return pagesize >= max_cpu_page_size;
 }
Signature · 1 line
2.14.3
Acked-by on the v3 series
#3 lore raw
In reply to David Gibson
Quoted 8 lines · David Gibson

This series makes some small changes to make it easier to obtain the host page size backing given portions of guest RAM. We use this in a couple of places currently, and I have one or two more to add in some upcoming code.

Assuming there are no objections, what should be the procedure for staging this? Can I put it in my for-2.13 ppc tree, even though it's technically generic code, or should it go through someone else's tree?

Yes, go ahead:

Paolo

#4 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · David Gibson

This series makes some small changes to make it easier to obtain the host page size backing given portions of guest RAM. We use this in a couple of places currently, and I have one or two more to add in some upcoming code.

Quoted 7 lines
Quoted 3 lines

Assuming there are no objections, what should be the procedure for staging this? Can I put it in my for-2.13 ppc tree, even though it's technically generic code, or should it go through someone else's tree?

Yes, go ahead:

I'm assuming that's an ack to putting it through my tree.

In which case, done, thanks.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson