qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Viktor Prutyanov <viktor@daynix.com>
To: annie.li@oracle.com, akihiko.odaki@daynix.com, kkostiuk@redhat.com
Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, yan@daynix.com,
	viktor@daynix.com, viktor.prutyanov@phystech.edu
Subject: [PATCH 4/5] elf2dmp: use Linux mmap with MAP_NORESERVE when possible
Date: Thu, 14 Sep 2023 01:46:56 +0300	[thread overview]
Message-ID: <20230913224657.11606-5-viktor@daynix.com> (raw)
In-Reply-To: <20230913224657.11606-1-viktor@daynix.com>

Glib's g_mapped_file_new maps file with PROT_READ|PROT_WRITE and
MAP_PRIVATE. This leads to premature physical memory allocation of dump
file size on Linux hosts and may fail. On Linux, mapping the file with
MAP_NORESERVE limits the allocation by available memory.

Signed-off-by: Viktor Prutyanov <viktor@daynix.com>
---
 contrib/elf2dmp/qemu_elf.c | 66 +++++++++++++++++++++++++++++++-------
 contrib/elf2dmp/qemu_elf.h |  4 +++
 2 files changed, 58 insertions(+), 12 deletions(-)

diff --git a/contrib/elf2dmp/qemu_elf.c b/contrib/elf2dmp/qemu_elf.c
index ebda60dcb8..94a8c3ad15 100644
--- a/contrib/elf2dmp/qemu_elf.c
+++ b/contrib/elf2dmp/qemu_elf.c
@@ -165,10 +165,37 @@ static bool check_ehdr(QEMU_Elf *qe)
     return true;
 }
 
-int QEMU_Elf_init(QEMU_Elf *qe, const char *filename)
+static int QEMU_Elf_map(QEMU_Elf *qe, const char *filename)
 {
+#ifdef CONFIG_LINUX
+    struct stat st;
+
+    printf("Using Linux's mmap\n");
+
+    qe->fd = open(filename, O_RDONLY, 0);
+    if (qe->fd == -1) {
+        eprintf("Failed to open ELF dump file \'%s\'\n", filename);
+        return 1;
+    }
+
+    if (fstat(qe->fd, &st)) {
+        eprintf("Failed to get size of ELF dump file\n");
+        close(qe->fd);
+        return 1;
+    }
+    qe->size = st.st_size;
+
+    qe->map = mmap(NULL, qe->size, PROT_READ | PROT_WRITE,
+            MAP_PRIVATE | MAP_NORESERVE, qe->fd, 0);
+    if (qe->map == MAP_FAILED) {
+        eprintf("Failed to map ELF file\n");
+        close(qe->fd);
+        return 1;
+    }
+#else
     GError *gerr = NULL;
-    int err = 0;
+
+    printf("Using GLib's mmap\n");
 
     qe->gmf = g_mapped_file_new(filename, TRUE, &gerr);
     if (gerr) {
@@ -179,29 +206,44 @@ int QEMU_Elf_init(QEMU_Elf *qe, const char *filename)
 
     qe->map = g_mapped_file_get_contents(qe->gmf);
     qe->size = g_mapped_file_get_length(qe->gmf);
+#endif
+
+    return 0;
+}
+
+static void QEMU_Elf_unmap(QEMU_Elf *qe)
+{
+#ifdef CONFIG_LINUX
+    munmap(qe->map, qe->size);
+    close(qe->fd);
+#else
+    g_mapped_file_unref(qe->gmf);
+#endif
+}
+
+int QEMU_Elf_init(QEMU_Elf *qe, const char *filename)
+{
+    if (QEMU_Elf_map(qe, filename)) {
+        return 1;
+    }
 
     if (!check_ehdr(qe)) {
         eprintf("Input file has the wrong format\n");
-        err = 1;
-        goto out_unmap;
+        QEMU_Elf_unmap(qe);
+        return 1;
     }
 
     if (init_states(qe)) {
         eprintf("Failed to extract QEMU CPU states\n");
-        err = 1;
-        goto out_unmap;
+        QEMU_Elf_unmap(qe);
+        return 1;
     }
 
     return 0;
-
-out_unmap:
-    g_mapped_file_unref(qe->gmf);
-
-    return err;
 }
 
 void QEMU_Elf_exit(QEMU_Elf *qe)
 {
     exit_states(qe);
-    g_mapped_file_unref(qe->gmf);
+    QEMU_Elf_unmap(qe);
 }
diff --git a/contrib/elf2dmp/qemu_elf.h b/contrib/elf2dmp/qemu_elf.h
index b2f0d9cbc9..2a71beca8e 100644
--- a/contrib/elf2dmp/qemu_elf.h
+++ b/contrib/elf2dmp/qemu_elf.h
@@ -32,7 +32,11 @@ typedef struct QEMUCPUState {
 int is_system(QEMUCPUState *s);
 
 typedef struct QEMU_Elf {
+#ifdef CONFIG_POSIX
+    int fd;
+#else
     GMappedFile *gmf;
+#endif
     size_t size;
     void *map;
     QEMUCPUState **state;
-- 
2.21.0



  parent reply	other threads:[~2023-09-13 22:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-13 22:46 [PATCH 0/5] elf2dmp: improve Win2022, Win11 and large dumps Viktor Prutyanov
2023-09-13 22:46 ` [PATCH 1/5] elf2dmp: replace PE export name check with PDB name check Viktor Prutyanov
2023-09-13 22:46 ` [PATCH 2/5] elf2dmp: introduce physical block alignment Viktor Prutyanov
2023-09-13 22:46 ` [PATCH 3/5] elf2dmp: introduce merging of physical memory runs Viktor Prutyanov
2023-09-14  7:45   ` Akihiko Odaki
2023-09-13 22:46 ` Viktor Prutyanov [this message]
2023-09-14  7:42   ` [PATCH 4/5] elf2dmp: use Linux mmap with MAP_NORESERVE when possible Akihiko Odaki
2023-09-15 16:55     ` Viktor Prutyanov
2023-09-13 22:46 ` [PATCH 5/5] elf2dmp: rework PDB_STREAM_INDEXES::segments obtaining Viktor Prutyanov
2023-09-14  7:53   ` Akihiko Odaki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230913224657.11606-5-viktor@daynix.com \
    --to=viktor@daynix.com \
    --cc=akihiko.odaki@daynix.com \
    --cc=annie.li@oracle.com \
    --cc=kkostiuk@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=viktor.prutyanov@phystech.edu \
    --cc=yan@daynix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).