[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools t
From: |
David Hildenbrand |
Subject: |
[PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools to qemu_ram_mmap() |
Date: |
Mon, 8 Mar 2021 16:05:57 +0100 |
Let's introduce a new set of flags that abstract mmap logic and replace
our current set of bools, to prepare for another flag.
Signed-off-by: David Hildenbrand <david@redhat.com>
---
include/qemu/mmap-alloc.h | 17 +++++++++++------
softmmu/physmem.c | 8 +++++---
util/mmap-alloc.c | 14 +++++++-------
util/oslib-posix.c | 3 ++-
4 files changed, 25 insertions(+), 17 deletions(-)
diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h
index 456ff87df1..55664ea9f3 100644
--- a/include/qemu/mmap-alloc.h
+++ b/include/qemu/mmap-alloc.h
@@ -6,6 +6,15 @@ size_t qemu_fd_getpagesize(int fd);
size_t qemu_mempath_getpagesize(const char *mem_path);
+/* Map PROT_READ instead of PROT_READ|PROT_WRITE. */
+#define QEMU_RAM_MMAP_READONLY (1 << 0)
+
+/* Map MAP_SHARED instead of MAP_PRIVATE. */
+#define QEMU_RAM_MMAP_SHARED (1 << 1)
+
+/* Map MAP_SYNC|MAP_SHARED_VALIDATE if possible, fallback and warn otherwise.
*/
+#define QEMU_RAM_MMAP_PMEM (1 << 2)
+
/**
* qemu_ram_mmap: mmap the specified file or device.
*
@@ -14,9 +23,7 @@ size_t qemu_mempath_getpagesize(const char *mem_path);
* @size: the number of bytes to be mmaped
* @align: if not zero, specify the alignment of the starting mapping address;
* otherwise, the alignment in use will be determined by QEMU.
- * @readonly: true for a read-only mapping, false for read/write.
- * @shared: map has RAM_SHARED flag.
- * @is_pmem: map has RAM_PMEM flag.
+ * @mmap_flags: QEMU_RAM_MMAP_* flags
* @map_offset: map starts at offset of map_offset from the start of fd
*
* Return:
@@ -26,9 +33,7 @@ size_t qemu_mempath_getpagesize(const char *mem_path);
void *qemu_ram_mmap(int fd,
size_t size,
size_t align,
- bool readonly,
- bool shared,
- bool is_pmem,
+ uint32_t mmap_flags,
off_t map_offset);
void qemu_ram_munmap(int fd, void *ptr, size_t size);
diff --git a/softmmu/physmem.c b/softmmu/physmem.c
index 8f90cb4cd2..ec7a382ccd 100644
--- a/softmmu/physmem.c
+++ b/softmmu/physmem.c
@@ -1533,6 +1533,7 @@ static void *file_ram_alloc(RAMBlock *block,
off_t offset,
Error **errp)
{
+ uint32_t mmap_flags;
void *area;
block->page_size = qemu_fd_getpagesize(fd);
@@ -1580,9 +1581,10 @@ static void *file_ram_alloc(RAMBlock *block,
perror("ftruncate");
}
- area = qemu_ram_mmap(fd, memory, block->mr->align, readonly,
- block->flags & RAM_SHARED, block->flags & RAM_PMEM,
- offset);
+ mmap_flags = readonly ? QEMU_RAM_MMAP_READONLY : 0;
+ mmap_flags |= (block->flags & RAM_SHARED) ? QEMU_RAM_MMAP_SHARED : 0;
+ mmap_flags |= (block->flags & RAM_PMEM) ? QEMU_RAM_MMAP_PMEM : 0;
+ area = qemu_ram_mmap(fd, memory, block->mr->align, mmap_flags, offset);
if (area == MAP_FAILED) {
error_setg_errno(errp, errno,
"unable to map backing store for guest RAM");
diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c
index 0e2bd7bc0e..bd8f7ab547 100644
--- a/util/mmap-alloc.c
+++ b/util/mmap-alloc.c
@@ -118,9 +118,12 @@ static void *mmap_reserve(size_t size, int fd)
* Activate memory in a reserved region from the given fd (if any), to make
* it accessible.
*/
-static void *mmap_activate(void *ptr, size_t size, int fd, bool readonly,
- bool shared, bool is_pmem, off_t map_offset)
+static void *mmap_activate(void *ptr, size_t size, int fd, uint32_t mmap_flags,
+ off_t map_offset)
{
+ const bool readonly = mmap_flags & QEMU_RAM_MMAP_READONLY;
+ const bool shared = mmap_flags & QEMU_RAM_MMAP_SHARED;
+ const bool is_pmem = mmap_flags & QEMU_RAM_MMAP_PMEM;
const int prot = PROT_READ | (readonly ? 0 : PROT_WRITE);
int map_sync_flags = 0;
int flags = MAP_FIXED;
@@ -173,9 +176,7 @@ static inline size_t mmap_guard_pagesize(int fd)
void *qemu_ram_mmap(int fd,
size_t size,
size_t align,
- bool readonly,
- bool shared,
- bool is_pmem,
+ uint32_t mmap_flags,
off_t map_offset)
{
const size_t guard_pagesize = mmap_guard_pagesize(fd);
@@ -199,8 +200,7 @@ void *qemu_ram_mmap(int fd,
offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
- ptr = mmap_activate(guardptr + offset, size, fd, readonly, shared, is_pmem,
- map_offset);
+ ptr = mmap_activate(guardptr + offset, size, fd, mmap_flags, map_offset);
if (ptr == MAP_FAILED) {
munmap(guardptr, total);
return MAP_FAILED;
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 36820fec16..1d250416f1 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -229,8 +229,9 @@ void *qemu_memalign(size_t alignment, size_t size)
/* alloc shared memory pages */
void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared)
{
+ const uint32_t mmap_flags = shared ? QEMU_RAM_MMAP_SHARED : 0;
size_t align = QEMU_VMALLOC_ALIGN;
- void *ptr = qemu_ram_mmap(-1, size, align, false, shared, false, 0);
+ void *ptr = qemu_ram_mmap(-1, size, align, mmap_flags, 0);
if (ptr == MAP_FAILED) {
return NULL;
--
2.29.2
- Re: [PATCH v3 02/12] softmmu/physmem: Fix ram_block_discard_range() to handle shared anonymous memory, (continued)
Re: [PATCH v3 02/12] softmmu/physmem: Fix ram_block_discard_range() to handle shared anonymous memory, Peter Xu, 2021/03/11
[PATCH v3 03/12] softmmu/physmem: Fix qemu_ram_remap() to handle shared anonymous memory, David Hildenbrand, 2021/03/08
[PATCH v3 04/12] util/mmap-alloc: Factor out calculation of the pagesize for the guard page, David Hildenbrand, 2021/03/08
[PATCH v3 05/12] util/mmap-alloc: Factor out reserving of a memory region to mmap_reserve(), David Hildenbrand, 2021/03/08
[PATCH v3 06/12] util/mmap-alloc: Factor out activating of memory to mmap_activate(), David Hildenbrand, 2021/03/08
[PATCH v3 07/12] softmmu/memory: Pass ram_flags into qemu_ram_alloc_from_fd(), David Hildenbrand, 2021/03/08
[PATCH v3 08/12] softmmu/memory: Pass ram_flags into memory_region_init_ram_shared_nomigrate(), David Hildenbrand, 2021/03/08
[PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools to qemu_ram_mmap(),
David Hildenbrand <=
- Re: [PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools to qemu_ram_mmap(), Peter Xu, 2021/03/09
- Re: [PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools to qemu_ram_mmap(), David Hildenbrand, 2021/03/09
- Re: [PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools to qemu_ram_mmap(), Peter Xu, 2021/03/09
- Re: [PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools to qemu_ram_mmap(), David Hildenbrand, 2021/03/10
- Re: [PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools to qemu_ram_mmap(), David Hildenbrand, 2021/03/10
- Re: [PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools to qemu_ram_mmap(), David Hildenbrand, 2021/03/10
- Re: [PATCH v3 09/12] util/mmap-alloc: Pass flags instead of separate bools to qemu_ram_mmap(), Peter Xu, 2021/03/10
[PATCH v3 10/12] memory: introduce RAM_NORESERVE and wire it up in qemu_ram_mmap(), David Hildenbrand, 2021/03/08
[PATCH v3 11/12] util/mmap-alloc: Support RAM_NORESERVE via MAP_NORESERVE, David Hildenbrand, 2021/03/08