yeahuh / rpms / qemu-kvm

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