Fix hugepages with memfd on s390x and clean up related code
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
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
@@ -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
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 ins390_memory_init()fails to enable the huge page support there vias390_set_max_pagesize(). Fix it by generalizing the code, so that it looks atqemu_ram_pagesize(memdev->mr.ram_block)instead of re-trying to get the information from the filesystem.Suggested-by David Hildenbrand <[email protected]>Signed-off-by Thomas Huth <[email protected]>--- backends/hostmem.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)backends/hostmem.c +2
-6@@ -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
Re: [PATCH v2 1/3] backends/hostmem: Fix support of memory-backend-memfd in qemu_maxrampagesize()
In reply to Thomas HuthQuoted 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 ins390_memory_init()fails to enable the huge page support there vias390_set_max_pagesize(). Fix it by generalizing the code, so that it looks atqemu_ram_pagesize(memdev->mr.ram_block)instead of re-trying to get the information from the filesystem.Suggested-by David Hildenbrand <[email protected]>Signed-off-by Thomas Huth <[email protected]>
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@@ -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)
PATCH v2 2/3 softmmu/physmem: Remove the ifdef __linux__ around the pagesize functions
In reply to Thomas HuthNow 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
@@ -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
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.Signed-off-by Thomas Huth <[email protected]>--- softmmu/physmem.c | 17 ----------------- 1 file changed, 17 deletions(-)softmmu/physmem.c +0
-10@@ -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
Re: [PATCH v2 2/3] softmmu/physmem: Remove the ifdef __linux__ around the pagesize functions
In reply to Thomas HuthQuoted 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.Signed-off-by Thomas Huth <[email protected]>
Quoted 39 lines
--- softmmu/physmem.c | 17 ----------------- 1 file changed, 17 deletions(-)softmmu/physmem.c +0
-17@@ -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)
PATCH v2 3/3 util/mmap-alloc: Remove qemu_mempath_getpagesize()
In reply to Thomas HuthThe 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
@@ -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
@@ -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
Re: [PATCH v2 3/3] util/mmap-alloc: Remove qemu_mempath_getpagesize()
Quoted 4 lines · Thomas Huth
The last user of this function has just been removed, so we can drop this function now, too.
Signed-off-by Thomas Huth <[email protected]>
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v2 3/3] util/mmap-alloc: Remove qemu_mempath_getpagesize()
In reply to Thomas HuthQuoted 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.
Signed-off-by Thomas Huth <[email protected]>
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@@ -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@@ -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) {
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:
- Useqemu_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 codeThomas 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: Removeqemu_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(-)
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:
- Useqemu_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 codeThomas 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: Removeqemu_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