Pablo Greco e6a3ae
From 6b3478bb8b5718d86cb04f41043a8e0cce4df24c Mon Sep 17 00:00:00 2001
Pablo Greco e6a3ae
From: "plai@redhat.com" <plai@redhat.com>
Pablo Greco e6a3ae
Date: Tue, 20 Aug 2019 16:12:49 +0100
Pablo Greco e6a3ae
Subject: [PATCH 02/11] mmap-alloc: unfold qemu_ram_mmap()
Pablo Greco e6a3ae
MIME-Version: 1.0
Pablo Greco e6a3ae
Content-Type: text/plain; charset=UTF-8
Pablo Greco e6a3ae
Content-Transfer-Encoding: 8bit
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: plai@redhat.com
Pablo Greco e6a3ae
Message-id: <1566317571-5697-3-git-send-email-plai@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 90083
Pablo Greco e6a3ae
O-Subject: [RHEL8.2 qemu-kvm PATCH 2/4] mmap-alloc: unfold qemu_ram_mmap()
Pablo Greco e6a3ae
Bugzilla: 1539282
Pablo Greco e6a3ae
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Pankaj Gupta <pagupta@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
From: Murilo Opsfelder Araujo <muriloo@linux.ibm.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Unfold parts of qemu_ram_mmap() for the sake of understanding, moving
Pablo Greco e6a3ae
declarations to the top, and keeping architecture-specifics in the
Pablo Greco e6a3ae
ifdef-else blocks.  No changes in the function behaviour.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Give ptr and ptr1 meaningful names:
Pablo Greco e6a3ae
  ptr  -> guardptr : pointer to the PROT_NONE guard region
Pablo Greco e6a3ae
  ptr1 -> ptr      : pointer to the mapped memory returned to caller
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Signed-off-by: Murilo Opsfelder Araujo <muriloo@linux.ibm.com>
Pablo Greco e6a3ae
Reviewed-by: Greg Kurz <groug@kaod.org>
Pablo Greco e6a3ae
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Pablo Greco e6a3ae
(cherry picked from commit 2044c3e7116eeac0449dcb4a4130cc8f8b9310da)
Pablo Greco e6a3ae
Signed-off-by: Paul Lai <plai@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 util/mmap-alloc.c | 53 ++++++++++++++++++++++++++++++++++-------------------
Pablo Greco e6a3ae
 1 file changed, 34 insertions(+), 19 deletions(-)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c
Pablo Greco e6a3ae
index 55d1890..b29fcee 100644
Pablo Greco e6a3ae
--- a/util/mmap-alloc.c
Pablo Greco e6a3ae
+++ b/util/mmap-alloc.c
Pablo Greco e6a3ae
@@ -79,11 +79,19 @@ void *qemu_ram_mmap(int fd,
Pablo Greco e6a3ae
                     bool shared,
Pablo Greco e6a3ae
                     bool is_pmem)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
+    int flags;
Pablo Greco e6a3ae
+    int guardfd;
Pablo Greco e6a3ae
+    size_t offset;
Pablo Greco e6a3ae
+    size_t total;
Pablo Greco e6a3ae
+    void *guardptr;
Pablo Greco e6a3ae
+    void *ptr;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
     /*
Pablo Greco e6a3ae
      * Note: this always allocates at least one extra page of virtual address
Pablo Greco e6a3ae
      * space, even if size is already aligned.
Pablo Greco e6a3ae
      */
Pablo Greco e6a3ae
-    size_t total = size + align;
Pablo Greco e6a3ae
+    total = size + align;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
 #if defined(__powerpc64__) && defined(__linux__)
Pablo Greco e6a3ae
     /* On ppc64 mappings in the same segment (aka slice) must share the same
Pablo Greco e6a3ae
      * page size. Since we will be re-allocating part of this segment
Pablo Greco e6a3ae
@@ -93,16 +101,22 @@ void *qemu_ram_mmap(int fd,
Pablo Greco e6a3ae
      * We do this unless we are using the system page size, in which case
Pablo Greco e6a3ae
      * anonymous memory is OK.
Pablo Greco e6a3ae
      */
Pablo Greco e6a3ae
-    int anonfd = fd == -1 || qemu_fd_getpagesize(fd) == getpagesize() ? -1 : fd;
Pablo Greco e6a3ae
-    int flags = anonfd == -1 ? MAP_ANONYMOUS : MAP_NORESERVE;
Pablo Greco e6a3ae
-    void *ptr = mmap(0, total, PROT_NONE, flags | MAP_PRIVATE, anonfd, 0);
Pablo Greco e6a3ae
+    flags = MAP_PRIVATE;
Pablo Greco e6a3ae
+    if (fd == -1 || qemu_fd_getpagesize(fd) == getpagesize()) {
Pablo Greco e6a3ae
+        guardfd = -1;
Pablo Greco e6a3ae
+        flags |= MAP_ANONYMOUS;
Pablo Greco e6a3ae
+    } else {
Pablo Greco e6a3ae
+        guardfd = fd;
Pablo Greco e6a3ae
+        flags |= MAP_NORESERVE;
Pablo Greco e6a3ae
+    }
Pablo Greco e6a3ae
 #else
Pablo Greco e6a3ae
-    void *ptr = mmap(0, total, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Pablo Greco e6a3ae
+    guardfd = -1;
Pablo Greco e6a3ae
+    flags = MAP_PRIVATE | MAP_ANONYMOUS;
Pablo Greco e6a3ae
 #endif
Pablo Greco e6a3ae
-    size_t offset;
Pablo Greco e6a3ae
-    void *ptr1;
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    if (ptr == MAP_FAILED) {
Pablo Greco e6a3ae
+    guardptr = mmap(0, total, PROT_NONE, flags, guardfd, 0);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    if (guardptr == MAP_FAILED) {
Pablo Greco e6a3ae
         return MAP_FAILED;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
@@ -110,19 +124,20 @@ void *qemu_ram_mmap(int fd,
Pablo Greco e6a3ae
     /* Always align to host page size */
Pablo Greco e6a3ae
     assert(align >= getpagesize());
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    offset = QEMU_ALIGN_UP((uintptr_t)ptr, align) - (uintptr_t)ptr;
Pablo Greco e6a3ae
-    ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE,
Pablo Greco e6a3ae
-                MAP_FIXED |
Pablo Greco e6a3ae
-                (fd == -1 ? MAP_ANONYMOUS : 0) |
Pablo Greco e6a3ae
-                (shared ? MAP_SHARED : MAP_PRIVATE),
Pablo Greco e6a3ae
-                fd, 0);
Pablo Greco e6a3ae
-    if (ptr1 == MAP_FAILED) {
Pablo Greco e6a3ae
-        munmap(ptr, total);
Pablo Greco e6a3ae
+    flags = MAP_FIXED;
Pablo Greco e6a3ae
+    flags |= fd == -1 ? MAP_ANONYMOUS : 0;
Pablo Greco e6a3ae
+    flags |= shared ? MAP_SHARED : MAP_PRIVATE;
Pablo Greco e6a3ae
+    offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    ptr = mmap(guardptr + offset, size, PROT_READ | PROT_WRITE, flags, fd, 0);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    if (ptr == MAP_FAILED) {
Pablo Greco e6a3ae
+        munmap(guardptr, total);
Pablo Greco e6a3ae
         return MAP_FAILED;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     if (offset > 0) {
Pablo Greco e6a3ae
-        munmap(ptr, offset);
Pablo Greco e6a3ae
+        munmap(guardptr, offset);
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /*
Pablo Greco e6a3ae
@@ -131,10 +146,10 @@ void *qemu_ram_mmap(int fd,
Pablo Greco e6a3ae
      */
Pablo Greco e6a3ae
     total -= offset;
Pablo Greco e6a3ae
     if (total > size + getpagesize()) {
Pablo Greco e6a3ae
-        munmap(ptr1 + size + getpagesize(), total - size - getpagesize());
Pablo Greco e6a3ae
+        munmap(ptr + size + getpagesize(), total - size - getpagesize());
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    return ptr1;
Pablo Greco e6a3ae
+    return ptr;
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 void qemu_ram_munmap(void *ptr, size_t size)
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae