[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 04/15] oslib: rework anonimous RAM allocation
From: |
Michael S. Tsirkin |
Subject: |
[Qemu-devel] [PULL 04/15] oslib: rework anonimous RAM allocation |
Date: |
Fri, 2 Oct 2015 16:45:14 +0300 |
At the moment we first allocate RAM, sometimes more than necessary for
alignment reasons. We then free the extra RAM.
Rework this to avoid the temporary allocation: reserve the
range by mapping it with PROT_NONE, then use just the
necessary range with MAP_FIXED.
Signed-off-by: Michael S. Tsirkin <address@hidden>
Reviewed-by: Paolo Bonzini <address@hidden>
Acked-by: Paolo Bonzini <address@hidden>
---
util/oslib-posix.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 3ae4987..27972d4 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -129,9 +129,9 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment)
{
size_t align = QEMU_VMALLOC_ALIGN;
size_t total = size + align - getpagesize();
- void *ptr = mmap(0, total, PROT_READ | PROT_WRITE,
- MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+ void *ptr = mmap(0, total, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
size_t offset = QEMU_ALIGN_UP((uintptr_t)ptr, align) - (uintptr_t)ptr;
+ void *ptr1;
if (ptr == MAP_FAILED) {
return NULL;
@@ -140,6 +140,14 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment)
if (alignment) {
*alignment = align;
}
+
+ ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE,
+ MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+ if (ptr1 == MAP_FAILED) {
+ munmap(ptr, total);
+ return NULL;
+ }
+
ptr += offset;
total -= offset;
--
MST
- [Qemu-devel] [PULL 11/15] vhost-user: unit test for new messages, (continued)
- [Qemu-devel] [PULL 11/15] vhost-user: unit test for new messages, Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 12/15] memhp: extend address auto assignment to support gaps, Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 07/15] pc: Add a comment explaining why pc_compat_2_4() doesn't exist, Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 03/15] virtio-net: correctly drop truncated packets, Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 05/15] oslib: allocate PROT_NONE pages on top of RAM, Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 10/15] vhost-user-test: do not reinvent glib-compat.h, Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 13/15] pc: memhp: force gaps between DIMM's GPA, Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 02/15] virtio: introduce virtqueue_discard(), Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 09/15] virtio-9p: migrate virtio subsections, Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 04/15] oslib: rework anonimous RAM allocation,
Michael S. Tsirkin <=
- [Qemu-devel] [PULL 15/15] vhost-user-test: fix predictable filename on tmpfs, Michael S. Tsirkin, 2015/10/08
- [Qemu-devel] [PULL 14/15] vhost-user-test: use tmpfs by default, Michael S. Tsirkin, 2015/10/08
Re: [Qemu-devel] [PULL 00/15] virtio,pc features, fixes, Michael S. Tsirkin, 2015/10/08