Blame SOURCES/kvm-mmap-alloc-unfold-qemu_ram_mmap.patch

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