|
|
383d26 |
From 5e5b310f575cab0403a84de8e6e7505d6f167730 Mon Sep 17 00:00:00 2001
|
|
|
383d26 |
From: Sam Bobroff <sbobroff@redhat.com>
|
|
|
383d26 |
Date: Tue, 16 Apr 2019 05:29:09 +0200
|
|
|
383d26 |
Subject: [PATCH 162/163] mmap-alloc: unfold qemu_ram_mmap()
|
|
|
383d26 |
|
|
|
383d26 |
RH-Author: Sam Bobroff <sbobroff@redhat.com>
|
|
|
383d26 |
Message-id: <1555392550-21945-2-git-send-email-sbobroff@redhat.com>
|
|
|
383d26 |
Patchwork-id: 85701
|
|
|
383d26 |
O-Subject: [RHEL-7.7 qemu-kvm-rhev BZ1672819 PATCH 1/2 REPOST] mmap-alloc: unfold qemu_ram_mmap()
|
|
|
383d26 |
Bugzilla: 1672819
|
|
|
383d26 |
RH-Acked-by: David Gibson <dgibson@redhat.com>
|
|
|
383d26 |
RH-Acked-by: Thomas Huth <thuth@redhat.com>
|
|
|
383d26 |
RH-Acked-by: Pankaj Gupta <pagupta@redhat.com>
|
|
|
383d26 |
|
|
|
383d26 |
From: Murilo Opsfelder Araujo <muriloo@linux.ibm.com>
|
|
|
383d26 |
|
|
|
383d26 |
Unfold parts of qemu_ram_mmap() for the sake of understanding, moving
|
|
|
383d26 |
declarations to the top, and keeping architecture-specifics in the
|
|
|
383d26 |
ifdef-else blocks. No changes in the function behaviour.
|
|
|
383d26 |
|
|
|
383d26 |
Give ptr and ptr1 meaningful names:
|
|
|
383d26 |
ptr -> guardptr : pointer to the PROT_NONE guard region
|
|
|
383d26 |
ptr1 -> ptr : pointer to the mapped memory returned to caller
|
|
|
383d26 |
|
|
|
383d26 |
Signed-off-by: Murilo Opsfelder Araujo <muriloo@linux.ibm.com>
|
|
|
383d26 |
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
|
|
|
383d26 |
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
|
|
|
383d26 |
Reviewed-by: Greg Kurz <groug@kaod.org>
|
|
|
383d26 |
(cherry picked from commit 94af9e34821c5c47a3c69fe242e32d0b33c2fff6)
|
|
|
383d26 |
|
|
|
383d26 |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1672819
|
|
|
383d26 |
Testing: Check that hugepage backed RAM removed from a guest is free'd
|
|
|
383d26 |
on the host.
|
|
|
383d26 |
Signed-off-by: Sam Bobroff <sbobroff@redhat.com>
|
|
|
383d26 |
Upstream: Patch is in dgibson/ppc-for-4.0
|
|
|
383d26 |
|
|
|
383d26 |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
383d26 |
---
|
|
|
383d26 |
util/mmap-alloc.c | 53 ++++++++++++++++++++++++++++++++++-------------------
|
|
|
383d26 |
1 file changed, 34 insertions(+), 19 deletions(-)
|
|
|
383d26 |
|
|
|
383d26 |
diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c
|
|
|
383d26 |
index 2fd8cbc..94ee517 100644
|
|
|
383d26 |
--- a/util/mmap-alloc.c
|
|
|
383d26 |
+++ b/util/mmap-alloc.c
|
|
|
383d26 |
@@ -75,11 +75,19 @@ size_t qemu_mempath_getpagesize(const char *mem_path)
|
|
|
383d26 |
|
|
|
383d26 |
void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
|
|
|
383d26 |
{
|
|
|
383d26 |
+ int flags;
|
|
|
383d26 |
+ int guardfd;
|
|
|
383d26 |
+ size_t offset;
|
|
|
383d26 |
+ size_t total;
|
|
|
383d26 |
+ void *guardptr;
|
|
|
383d26 |
+ void *ptr;
|
|
|
383d26 |
+
|
|
|
383d26 |
/*
|
|
|
383d26 |
* Note: this always allocates at least one extra page of virtual address
|
|
|
383d26 |
* space, even if size is already aligned.
|
|
|
383d26 |
*/
|
|
|
383d26 |
- size_t total = size + align;
|
|
|
383d26 |
+ total = size + align;
|
|
|
383d26 |
+
|
|
|
383d26 |
#if defined(__powerpc64__) && defined(__linux__)
|
|
|
383d26 |
/* On ppc64 mappings in the same segment (aka slice) must share the same
|
|
|
383d26 |
* page size. Since we will be re-allocating part of this segment
|
|
|
383d26 |
@@ -89,16 +97,22 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
|
|
|
383d26 |
* We do this unless we are using the system page size, in which case
|
|
|
383d26 |
* anonymous memory is OK.
|
|
|
383d26 |
*/
|
|
|
383d26 |
- int anonfd = fd == -1 || qemu_fd_getpagesize(fd) == getpagesize() ? -1 : fd;
|
|
|
383d26 |
- int flags = anonfd == -1 ? MAP_ANONYMOUS : MAP_NORESERVE;
|
|
|
383d26 |
- void *ptr = mmap(0, total, PROT_NONE, flags | MAP_PRIVATE, anonfd, 0);
|
|
|
383d26 |
+ flags = MAP_PRIVATE;
|
|
|
383d26 |
+ if (fd == -1 || qemu_fd_getpagesize(fd) == getpagesize()) {
|
|
|
383d26 |
+ guardfd = -1;
|
|
|
383d26 |
+ flags |= MAP_ANONYMOUS;
|
|
|
383d26 |
+ } else {
|
|
|
383d26 |
+ guardfd = fd;
|
|
|
383d26 |
+ flags |= MAP_NORESERVE;
|
|
|
383d26 |
+ }
|
|
|
383d26 |
#else
|
|
|
383d26 |
- void *ptr = mmap(0, total, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
|
|
383d26 |
+ guardfd = -1;
|
|
|
383d26 |
+ flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
|
|
383d26 |
#endif
|
|
|
383d26 |
- size_t offset;
|
|
|
383d26 |
- void *ptr1;
|
|
|
383d26 |
|
|
|
383d26 |
- if (ptr == MAP_FAILED) {
|
|
|
383d26 |
+ guardptr = mmap(0, total, PROT_NONE, flags, guardfd, 0);
|
|
|
383d26 |
+
|
|
|
383d26 |
+ if (guardptr == MAP_FAILED) {
|
|
|
383d26 |
return MAP_FAILED;
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
@@ -106,19 +120,20 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
|
|
|
383d26 |
/* Always align to host page size */
|
|
|
383d26 |
assert(align >= getpagesize());
|
|
|
383d26 |
|
|
|
383d26 |
- offset = QEMU_ALIGN_UP((uintptr_t)ptr, align) - (uintptr_t)ptr;
|
|
|
383d26 |
- ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE,
|
|
|
383d26 |
- MAP_FIXED |
|
|
|
383d26 |
- (fd == -1 ? MAP_ANONYMOUS : 0) |
|
|
|
383d26 |
- (shared ? MAP_SHARED : MAP_PRIVATE),
|
|
|
383d26 |
- fd, 0);
|
|
|
383d26 |
- if (ptr1 == MAP_FAILED) {
|
|
|
383d26 |
- munmap(ptr, total);
|
|
|
383d26 |
+ flags = MAP_FIXED;
|
|
|
383d26 |
+ flags |= fd == -1 ? MAP_ANONYMOUS : 0;
|
|
|
383d26 |
+ flags |= shared ? MAP_SHARED : MAP_PRIVATE;
|
|
|
383d26 |
+ offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
|
|
|
383d26 |
+
|
|
|
383d26 |
+ ptr = mmap(guardptr + offset, size, PROT_READ | PROT_WRITE, flags, fd, 0);
|
|
|
383d26 |
+
|
|
|
383d26 |
+ if (ptr == MAP_FAILED) {
|
|
|
383d26 |
+ munmap(guardptr, total);
|
|
|
383d26 |
return MAP_FAILED;
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
if (offset > 0) {
|
|
|
383d26 |
- munmap(ptr, offset);
|
|
|
383d26 |
+ munmap(guardptr, offset);
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
/*
|
|
|
383d26 |
@@ -127,10 +142,10 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
|
|
|
383d26 |
*/
|
|
|
383d26 |
total -= offset;
|
|
|
383d26 |
if (total > size + getpagesize()) {
|
|
|
383d26 |
- munmap(ptr1 + size + getpagesize(), total - size - getpagesize());
|
|
|
383d26 |
+ munmap(ptr + size + getpagesize(), total - size - getpagesize());
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
- return ptr1;
|
|
|
383d26 |
+ return ptr;
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
void qemu_ram_munmap(void *ptr, size_t size)
|
|
|
383d26 |
--
|
|
|
383d26 |
1.8.3.1
|
|
|
383d26 |
|