Skip to contentSkip to conversation

Fix hugepages with memfd on s390x and clean up related code

PATCH In review Thomas Huth 3 participants 12 messages Aug 10, 2022 – Aug 26, 2022 Arm STM32

lore
Share & tools
lore
#0 lore raw

v2 cover Fix hugepages with memfd on s390x and clean up related code

The first patch fixes the problem that hugepages cannot be used via the "memory-backend-memfd" object on s390x. The second and third patch are some clean-ups that can be done after generalizing the code in the first patch.

v2:
- Use qemu_ram_pagesize(memdev->mr.ram_block) instead of adding additional code for the memfd object
- Added the two clean-up patches on top to simplify the code

Thomas Huth (3):
backends/hostmem: Fix support of memory-backend-memfd in
qemu_maxrampagesize()
softmmu/physmem: Remove the ifdef __linux__ around the pagesize
functions
util/mmap-alloc: Remove qemu_mempath_getpagesize()

 include/qemu/mmap-alloc.h |  2 --
 backends/hostmem.c        | 14 ++------------
 softmmu/physmem.c         | 17 -----------------
 util/mmap-alloc.c         | 31 -------------------------------
 4 files changed, 2 insertions(+), 62 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/3 backends/hostmem: Fix support of memory-backend-memfd in qemu_maxrampagesize()

It is currently not possible yet to use "memory-backend-memfd" on s390x with hugepages enabled. This problem is caused by qemu_maxrampagesize() not taking memory-backend-memfd objects into account yet, so the code in s390_memory_init() fails to enable the huge page support there via s390_set_max_pagesize(). Fix it by generalizing the code, so that it looks at qemu_ram_pagesize(memdev->mr.ram_block) instead of re-trying to get the information from the filesystem.

---
 backends/hostmem.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
backends/hostmem.c +2 -12
index 624bb7ecd3..4428e06738 100644
@@ -306,22 +306,12 @@ 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);
+    size_t pagesize = qemu_ram_pagesize(memdev->mr.ram_block);
+    g_assert(pagesize >= qemu_real_host_page_size());
     return pagesize;
 }
-#else
-size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
-{
-    return qemu_real_host_page_size();
-}
-#endif
 
 static void
 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
Signature · 1 line
2.31.1
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] backends/hostmem: Fix support of memory-backend-memfd in qemu_maxrampagesize()

Quoted 34 lines · Thomas Huth

It is currently not possible yet to use "memory-backend-memfd" on s390x with hugepages enabled. This problem is caused by qemu_maxrampagesize() not taking memory-backend-memfd objects into account yet, so the code in s390_memory_init() fails to enable the huge page support there via s390_set_max_pagesize(). Fix it by generalizing the code, so that it looks at qemu_ram_pagesize(memdev->mr.ram_block) instead of re-trying to get the information from the filesystem.

---
 backends/hostmem.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
backends/hostmem.c +2 -6
index 624bb7ecd3..4428e06738 100644
@@ -306,22 +306,12 @@ 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);
+    size_t pagesize = qemu_ram_pagesize(memdev->mr.ram_block);
+    g_assert(pagesize >= qemu_real_host_page_size());
     return pagesize;

backends/hostmem.c bool host_memory_backend_is_mapped(HostMemoryBackend *backend) · line 317

Quoted 10 lines
backends/hostmem.c +0 -6
 }
-#else
-size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
-{
-    return qemu_real_host_page_size();
-}
-#endif
 
 static void
 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] backends/hostmem: Fix support of memory-backend-memfd in qemu_maxrampagesize()

In reply to Thomas Huth
Quoted 11 lines · On Wed, 10 Aug 2022 14:57:18 +0200 Thomas Huth <[email protected]> wrote:

It is currently not possible yet to use "memory-backend-memfd" on s390x with hugepages enabled. This problem is caused by qemu_maxrampagesize() not taking memory-backend-memfd objects into account yet, so the code in s390_memory_init() fails to enable the huge page support there via s390_set_max_pagesize(). Fix it by generalizing the code, so that it looks at qemu_ram_pagesize(memdev->mr.ram_block) instead of re-trying to get the information from the filesystem.

more removed lines than added, I like it

Quoted 33 lines
---
 backends/hostmem.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
backends/hostmem.c +2 -12
index 624bb7ecd3..4428e06738 100644
@@ -306,22 +306,12 @@ 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);
+    size_t pagesize = qemu_ram_pagesize(memdev->mr.ram_block);
+    g_assert(pagesize >= qemu_real_host_page_size());
     return pagesize;
 }
-#else
-size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
-{
-    return qemu_real_host_page_size();
-}
-#endif
 
 static void
 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
#4 lore raw

PATCH v2 2/3 softmmu/physmem: Remove the ifdef __linux__ around the pagesize functions

In reply to Thomas Huth

Now that host_memory_backend_pagesize() is not depending on the hugetlb memory path handling anymore, we can also remove the #ifdef and the TOCTTOU comment from the calling functions - the code should now work equally well on all host architectures.

---
 softmmu/physmem.c | 17 -----------------
 1 file changed, 17 deletions(-)
softmmu/physmem.c +0 -17
index dc3c3e5f2e..50231bab30 100644
@@ -1331,13 +1331,6 @@ GString *ram_block_format(void)
     return buf;
 }
 
-#ifdef __linux__
-/*
- * FIXME TOCTTOU: this iterates over memory backends' mem-path, which
- * may or may not name the same files / on the same filesystem now as
- * when we actually open and map them.  Iterate over the file
- * descriptors instead, and use qemu_fd_getpagesize().
- */
 static int find_min_backend_pagesize(Object *obj, void *opaque)
 {
     long *hpsize_min = opaque;
@@ -1391,16 +1384,6 @@ long qemu_maxrampagesize(void)
     object_child_foreach(memdev_root, find_max_backend_pagesize, &pagesize);
     return pagesize;
 }
-#else
-long qemu_minrampagesize(void)
-{
-    return qemu_real_host_page_size();
-}
-long qemu_maxrampagesize(void)
-{
-    return qemu_real_host_page_size();
-}
-#endif
 
 #ifdef CONFIG_POSIX
 static int64_t get_file_size(int fd)
Signature · 1 line
2.31.1
Acked-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] softmmu/physmem: Remove the ifdef __linux__ around the pagesize functions

Quoted 35 lines · Thomas Huth

Now that host_memory_backend_pagesize() is not depending on the hugetlb memory path handling anymore, we can also remove the #ifdef and the TOCTTOU comment from the calling functions - the code should now work equally well on all host architectures.

---
 softmmu/physmem.c | 17 -----------------
 1 file changed, 17 deletions(-)
softmmu/physmem.c +0 -10
index dc3c3e5f2e..50231bab30 100644
@@ -1331,13 +1331,6 @@ GString *ram_block_format(void)
     return buf;
 }
 
-#ifdef __linux__
-/*
- * FIXME TOCTTOU: this iterates over memory backends' mem-path, which
- * may or may not name the same files / on the same filesystem now as
- * when we actually open and map them.  Iterate over the file
- * descriptors instead, and use qemu_fd_getpagesize().
- */
 static int find_min_backend_pagesize(Object *obj, void *opaque)
 {
     long *hpsize_min = opaque;
@@ -1391,16 +1384,6 @@ long qemu_maxrampagesize(void)
     object_child_foreach(memdev_root, find_max_backend_pagesize, &pagesize);
     return pagesize;
 }
-#else
-long qemu_minrampagesize(void)
-{

softmmu/physmem.c long qemu_maxrampagesize(void) · line 1389

Quoted 10 lines
softmmu/physmem.c +0 -7
-    return qemu_real_host_page_size();
-}
-long qemu_maxrampagesize(void)
-{
-    return qemu_real_host_page_size();
-}
-#endif
 
 #ifdef CONFIG_POSIX
 static int64_t get_file_size(int fd)
Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3] softmmu/physmem: Remove the ifdef __linux__ around the pagesize functions

In reply to Thomas Huth
Quoted 6 lines · On Wed, 10 Aug 2022 14:57:19 +0200 Thomas Huth <[email protected]> wrote:

Now that host_memory_backend_pagesize() is not depending on the hugetlb memory path handling anymore, we can also remove the #ifdef and the TOCTTOU comment from the calling functions - the code should now work equally well on all host architectures.

Quoted 39 lines
---
 softmmu/physmem.c | 17 -----------------
 1 file changed, 17 deletions(-)
softmmu/physmem.c +0 -17
index dc3c3e5f2e..50231bab30 100644
@@ -1331,13 +1331,6 @@ GString *ram_block_format(void)
     return buf;
 }
 
-#ifdef __linux__
-/*
- * FIXME TOCTTOU: this iterates over memory backends' mem-path, which
- * may or may not name the same files / on the same filesystem now as
- * when we actually open and map them.  Iterate over the file
- * descriptors instead, and use qemu_fd_getpagesize().
- */
 static int find_min_backend_pagesize(Object *obj, void *opaque)
 {
     long *hpsize_min = opaque;
@@ -1391,16 +1384,6 @@ long qemu_maxrampagesize(void)
     object_child_foreach(memdev_root, find_max_backend_pagesize, &pagesize);
     return pagesize;
 }
-#else
-long qemu_minrampagesize(void)
-{
-    return qemu_real_host_page_size();
-}
-long qemu_maxrampagesize(void)
-{
-    return qemu_real_host_page_size();
-}
-#endif
 
 #ifdef CONFIG_POSIX
 static int64_t get_file_size(int fd)
#7 lore raw

PATCH v2 3/3 util/mmap-alloc: Remove qemu_mempath_getpagesize()

In reply to Thomas Huth

The last user of this function has just been removed, so we can drop this function now, too.

---
 include/qemu/mmap-alloc.h |  2 --
 util/mmap-alloc.c         | 31 -------------------------------
 2 files changed, 33 deletions(-)
include/qemu/mmap-alloc.h +0 -2
index 5076695cc8..2825e231a7 100644
@@ -4,8 +4,6 @@
 
 size_t qemu_fd_getpagesize(int fd);
 
-size_t qemu_mempath_getpagesize(const char *mem_path);
-
 /**
  * qemu_ram_mmap: mmap anonymous memory, the specified file or device.
  *
util/mmap-alloc.c +0 -31
index 5b90cb68ea..5ed7d29183 100644
@@ -53,37 +53,6 @@ size_t qemu_fd_getpagesize(int fd)
     return qemu_real_host_page_size();
 }
 
-size_t qemu_mempath_getpagesize(const char *mem_path)
-{
-#ifdef CONFIG_LINUX
-    struct statfs fs;
-    int ret;
-
-    if (mem_path) {
-        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 (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 */
-    return QEMU_VMALLOC_ALIGN;
-#endif
-#endif
-
-    return qemu_real_host_page_size();
-}
-
 #define OVERCOMMIT_MEMORY_PATH "/proc/sys/vm/overcommit_memory"
 static bool map_noreserve_effective(int fd, uint32_t qemu_map_flags)
 {
Signature · 1 line
2.31.1
Reviewed-by on v2 3/3
#9 lore raw

Re: [PATCH v2 3/3] util/mmap-alloc: Remove qemu_mempath_getpagesize()

In reply to Thomas Huth
Quoted 4 lines · On Wed, 10 Aug 2022 14:57:20 +0200 Thomas Huth <[email protected]> wrote:

The last user of this function has just been removed, so we can drop this function now, too.

Quoted 60 lines
---
 include/qemu/mmap-alloc.h |  2 --
 util/mmap-alloc.c         | 31 -------------------------------
 2 files changed, 33 deletions(-)
include/qemu/mmap-alloc.h +0 -2
index 5076695cc8..2825e231a7 100644
@@ -4,8 +4,6 @@
 
 size_t qemu_fd_getpagesize(int fd);
 
-size_t qemu_mempath_getpagesize(const char *mem_path);
-
 /**
  * qemu_ram_mmap: mmap anonymous memory, the specified file or device.
  *
util/mmap-alloc.c +0 -31
index 5b90cb68ea..5ed7d29183 100644
@@ -53,37 +53,6 @@ size_t qemu_fd_getpagesize(int fd)
     return qemu_real_host_page_size();
 }
 
-size_t qemu_mempath_getpagesize(const char *mem_path)
-{
-#ifdef CONFIG_LINUX
-    struct statfs fs;
-    int ret;
-
-    if (mem_path) {
-        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 (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 */
-    return QEMU_VMALLOC_ALIGN;
-#endif
-#endif
-
-    return qemu_real_host_page_size();
-}
-
 #define OVERCOMMIT_MEMORY_PATH "/proc/sys/vm/overcommit_memory"
 static bool map_noreserve_effective(int fd, uint32_t qemu_map_flags)
 {
#10 lore raw
In reply to Thomas Huth
Quoted 4 lines · On Wed, 10 Aug 2022 14:57:17 +0200 Thomas Huth <[email protected]> wrote:

The first patch fixes the problem that hugepages cannot be used via the "memory-backend-memfd" object on s390x. The second and third patch are some clean-ups that can be done after generalizing the code in the first patch.

thanks for fixing this

Quoted 19 lines

v2:
- Use qemu_ram_pagesize(memdev->mr.ram_block) instead of adding additional code for the memfd object
- Added the two clean-up patches on top to simplify the code

Thomas Huth (3):
backends/hostmem: Fix support of memory-backend-memfd in
qemu_maxrampagesize()
softmmu/physmem: Remove the ifdef __linux__ around the pagesize
functions
util/mmap-alloc: Remove qemu_mempath_getpagesize()

 include/qemu/mmap-alloc.h |  2 --
 backends/hostmem.c        | 14 ++------------
 softmmu/physmem.c         | 17 -----------------
 util/mmap-alloc.c         | 31 -------------------------------
 4 files changed, 2 insertions(+), 62 deletions(-)
#11 lore raw
In reply to Thomas Huth
Quoted 17 lines · Thomas Huth

The first patch fixes the problem that hugepages cannot be used via the "memory-backend-memfd" object on s390x. The second and third patch are some clean-ups that can be done after generalizing the code in the first patch.

v2:
- Use qemu_ram_pagesize(memdev->mr.ram_block) instead of adding additional code for the memfd object
- Added the two clean-up patches on top to simplify the code

Thomas Huth (3):
backends/hostmem: Fix support of memory-backend-memfd in
qemu_maxrampagesize()
softmmu/physmem: Remove the ifdef __linux__ around the pagesize
functions
util/mmap-alloc: Remove qemu_mempath_getpagesize()

Quoted 6 lines
  include/qemu/mmap-alloc.h |  2 --
  backends/hostmem.c        | 14 ++------------
  softmmu/physmem.c         | 17 -----------------
  util/mmap-alloc.c         | 31 -------------------------------
  4 files changed, 2 insertions(+), 62 deletions(-)

Thanks to David and Claudio for the reviews! FWIW, I'll take this through my s390x-next branch since it fixes a s390x-related problem:

https://gitlab.com/thuth/qemu/-/commits/s390x-next/

Thomas