cryptospore / rpms / qemu-kvm

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