aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorMurilo Opsfelder Araujo <muriloo@linux.ibm.com>2019-01-30 21:36:05 -0200
committerMichael S. Tsirkin <mst@redhat.com>2019-02-05 10:58:33 -0500
commit7265c2b9716369b339d778b9ef64a8161eb8f99b (patch)
treeb9d56f32f71ac1b9556a2ddc99130bfb9fe81cdf /util
parent94af9e34821c5c47a3c69fe242e32d0b33c2fff6 (diff)
downloadqemu-7265c2b9716369b339d778b9ef64a8161eb8f99b.zip
qemu-7265c2b9716369b339d778b9ef64a8161eb8f99b.tar.gz
qemu-7265c2b9716369b339d778b9ef64a8161eb8f99b.tar.bz2
mmap-alloc: fix hugetlbfs misaligned length in ppc64
The commit 7197fb4058bcb68986bae2bb2c04d6370f3e7218 ("util/mmap-alloc: fix hugetlb support on ppc64") fixed Huge TLB mappings on ppc64. However, we still need to consider the underlying huge page size during munmap() because it requires that both address and length be a multiple of the underlying huge page size for Huge TLB mappings. Quote from "Huge page (Huge TLB) mappings" paragraph under NOTES section of the munmap(2) manual: "For munmap(), addr and length must both be a multiple of the underlying huge page size." On ppc64, the munmap() in qemu_ram_munmap() does not work for Huge TLB mappings because the mapped segment can be aligned with the underlying huge page size, not aligned with the native system page size, as returned by getpagesize(). This has the side effect of not releasing huge pages back to the pool after a hugetlbfs file-backed memory device is hot-unplugged. This patch fixes the situation in qemu_ram_mmap() and qemu_ram_munmap() by considering the underlying page size on ppc64. After this patch, memory hot-unplug releases huge pages back to the pool. Fixes: 7197fb4058bcb68986bae2bb2c04d6370f3e7218 Signed-off-by: Murilo Opsfelder Araujo <muriloo@linux.ibm.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Greg Kurz <groug@kaod.org>
Diffstat (limited to 'util')
-rw-r--r--util/mmap-alloc.c22
-rw-r--r--util/oslib-posix.c2
2 files changed, 17 insertions, 7 deletions
diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c
index f71ea03..8565885 100644
--- a/util/mmap-alloc.c
+++ b/util/mmap-alloc.c
@@ -80,6 +80,7 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
int flags;
int guardfd;
size_t offset;
+ size_t pagesize;
size_t total;
void *guardptr;
void *ptr;
@@ -100,7 +101,8 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
* anonymous memory is OK.
*/
flags = MAP_PRIVATE;
- if (fd == -1 || qemu_fd_getpagesize(fd) == getpagesize()) {
+ pagesize = qemu_fd_getpagesize(fd);
+ if (fd == -1 || pagesize == getpagesize()) {
guardfd = -1;
flags |= MAP_ANONYMOUS;
} else {
@@ -109,6 +111,7 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
}
#else
guardfd = -1;
+ pagesize = getpagesize();
flags = MAP_PRIVATE | MAP_ANONYMOUS;
#endif
@@ -120,7 +123,7 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
assert(is_power_of_2(align));
/* Always align to host page size */
- assert(align >= getpagesize());
+ assert(align >= pagesize);
flags = MAP_FIXED;
flags |= fd == -1 ? MAP_ANONYMOUS : 0;
@@ -143,17 +146,24 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
* a guard page guarding against potential buffer overflows.
*/
total -= offset;
- if (total > size + getpagesize()) {
- munmap(ptr + size + getpagesize(), total - size - getpagesize());
+ if (total > size + pagesize) {
+ munmap(ptr + size + pagesize, total - size - pagesize);
}
return ptr;
}
-void qemu_ram_munmap(void *ptr, size_t size)
+void qemu_ram_munmap(int fd, void *ptr, size_t size)
{
+ size_t pagesize;
+
if (ptr) {
/* Unmap both the RAM block and the guard page */
- munmap(ptr, size + getpagesize());
+#if defined(__powerpc64__) && defined(__linux__)
+ pagesize = qemu_fd_getpagesize(fd);
+#else
+ pagesize = getpagesize();
+#endif
+ munmap(ptr, size + pagesize);
}
}
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 4ce1ba9..37c5854 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -226,7 +226,7 @@ void qemu_vfree(void *ptr)
void qemu_anon_ram_free(void *ptr, size_t size)
{
trace_qemu_anon_ram_free(ptr, size);
- qemu_ram_munmap(ptr, size);
+ qemu_ram_munmap(-1, ptr, size);
}
void qemu_set_block(int fd)