[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 29/30] elf2dmp: use Linux mmap with MAP_NORESERVE when possible
From: |
Peter Maydell |
Subject: |
[PULL 29/30] elf2dmp: use Linux mmap with MAP_NORESERVE when possible |
Date: |
Thu, 21 Sep 2023 18:37:19 +0100 |
From: Viktor Prutyanov <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>
Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Message-id: 20230915170153.10959-5-viktor@daynix.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
contrib/elf2dmp/qemu_elf.h | 2 ++
contrib/elf2dmp/qemu_elf.c | 68 +++++++++++++++++++++++++++++++-------
2 files changed, 58 insertions(+), 12 deletions(-)
diff --git a/contrib/elf2dmp/qemu_elf.h b/contrib/elf2dmp/qemu_elf.h
index b2f0d9cbc9b..afa75f10b2d 100644
--- a/contrib/elf2dmp/qemu_elf.h
+++ b/contrib/elf2dmp/qemu_elf.h
@@ -32,7 +32,9 @@ typedef struct QEMUCPUState {
int is_system(QEMUCPUState *s);
typedef struct QEMU_Elf {
+#ifndef CONFIG_LINUX
GMappedFile *gmf;
+#endif
size_t size;
void *map;
QEMUCPUState **state;
diff --git a/contrib/elf2dmp/qemu_elf.c b/contrib/elf2dmp/qemu_elf.c
index ebda60dcb8a..de6ad744c6d 100644
--- a/contrib/elf2dmp/qemu_elf.c
+++ b/contrib/elf2dmp/qemu_elf.c
@@ -165,10 +165,40 @@ 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;
+ int fd;
+
+ printf("Using Linux mmap\n");
+
+ fd = open(filename, O_RDONLY, 0);
+ if (fd == -1) {
+ eprintf("Failed to open ELF dump file \'%s\'\n", filename);
+ return 1;
+ }
+
+ if (fstat(fd, &st)) {
+ eprintf("Failed to get size of ELF dump file\n");
+ close(fd);
+ return 1;
+ }
+ qe->size = st.st_size;
+
+ qe->map = mmap(NULL, qe->size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_NORESERVE, fd, 0);
+ if (qe->map == MAP_FAILED) {
+ eprintf("Failed to map ELF file\n");
+ close(fd);
+ return 1;
+ }
+
+ close(fd);
+#else
GError *gerr = NULL;
- int err = 0;
+
+ printf("Using GLib mmap\n");
qe->gmf = g_mapped_file_new(filename, TRUE, &gerr);
if (gerr) {
@@ -179,29 +209,43 @@ 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);
+#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);
}
--
2.34.1
- [PULL 19/30] target/arm: Implement the SETG* instructions, (continued)
- [PULL 19/30] target/arm: Implement the SETG* instructions, Peter Maydell, 2023/09/21
- [PULL 20/30] target/arm: Implement MTE tag-checking functions for FEAT_MOPS copies, Peter Maydell, 2023/09/21
- [PULL 16/30] target/arm: Implement MTE tag-checking functions for FEAT_MOPS, Peter Maydell, 2023/09/21
- [PULL 13/30] target/arm: Pass unpriv bool to get_a64_user_mem_index(), Peter Maydell, 2023/09/21
- [PULL 06/30] linux-user/elfload.c: Report previously missing arm32 hwcaps, Peter Maydell, 2023/09/21
- [PULL 23/30] audio/jackaudio: Avoid dynamic stack allocation in qjack_client_init, Peter Maydell, 2023/09/21
- [PULL 21/30] target/arm: Implement the CPY* instructions, Peter Maydell, 2023/09/21
- [PULL 18/30] target/arm: Define new TB flag for ATA0, Peter Maydell, 2023/09/21
- [PULL 28/30] elf2dmp: introduce merging of physical memory runs, Peter Maydell, 2023/09/21
- [PULL 15/30] target/arm: New function allocation_tag_mem_probe(), Peter Maydell, 2023/09/21
- [PULL 29/30] elf2dmp: use Linux mmap with MAP_NORESERVE when possible,
Peter Maydell <=
- [PULL 24/30] audio/jackaudio: Avoid dynamic stack allocation in qjack_process(), Peter Maydell, 2023/09/21
- [PULL 30/30] elf2dmp: rework PDB_STREAM_INDEXES::segments obtaining, Peter Maydell, 2023/09/21
- [PULL 27/30] elf2dmp: introduce physical block alignment, Peter Maydell, 2023/09/21
- [PULL 25/30] sbsa-ref: add non-secure EL2 virtual timer, Peter Maydell, 2023/09/21
- [PULL 26/30] elf2dmp: replace PE export name check with PDB name check, Peter Maydell, 2023/09/21
- [PULL 22/30] target/arm: Enable FEAT_MOPS for CPU 'max', Peter Maydell, 2023/09/21
- Re: [PULL 00/30] target-arm queue, Stefan Hajnoczi, 2023/09/25