Ram blocks with resizable anonymous allocations under POSIX
cover Ram blocks with resizable anonymous allocations under POSIX
We already allow resizable ram blocks for anonymous memory, however, they are not actually resized. All memory is mmaped() R/W, including the memory exceeding the used_length, up to the max_length.
When resizing, effectively only the boundary is moved. Implement actually resizable anonymous allocations and make use of them in resizable ram blocks when possible. Memory exceeding the used_length will be inaccessible. Especially ram block notifiers require care.
Having actually resizable anonymous allocations (via mmap-hackery) allows to reserve a big region in virtual address space and grow the accessible/usable part on demand. Even if "/proc/sys/vm/overcommit_memory" is set to "never" under Linux, huge reservations will succeed. If there is not enough memory when resizing (to populate parts of the reserved region), trying to resize will fail. Only the actually used size is reserved in the OS.
E.g., virtio-mem [1] wants to reserve big resizable memory regions and grow the usable part on demand. I think this change is worth sending out individually. Accompanied by a bunch of minor fixes and cleanups.
[1] https://lore.kernel.org/kvm/[email protected]/
David Hildenbrand (13):
util: vfio-helpers: Factor out and fix processing of existings ram
blocks
exec: Factor out setting ram settings (madvise ...) into
qemu_ram_apply_settings()
exec: Reuse qemu_ram_apply_settings() in qemu_ram_remap()
exec: Drop "shared" parameter from ram_block_add() util/mmap-alloc: Factor out calculation of pagesize to mmap_pagesize() util/mmap-alloc: Factor out reserving of a memory region to
mmap_reserve()
util/mmap-alloc: Factor out populating of memory to mmap_populate() util/mmap-alloc: Prepare for resizable mmaps util/mmap-alloc: Implement resizable mmaps
numa: Introduce ram_block_notify_resized() and
ram_block_notifiers_support_resize()
util: vfio-helpers: Implement ram_block_resized()
util: oslib: Resizable anonymous allocations under POSIX
exec: Ram blocks with resizable anonymous allocations under POSIX
exec.c | 99 ++++++++++++++++++---- hw/core/numa.c | 39 +++++++++ include/exec/cpu-common.h | 3 + include/exec/memory.h | 8 ++ include/exec/ramlist.h | 4 + include/qemu/mmap-alloc.h | 21 +++-- include/qemu/osdep.h | 6 +- stubs/ram-block.c | 20 ----- util/mmap-alloc.c | 168 ++++++++++++++++++++++++-------------- util/oslib-posix.c | 37 ++++++++- util/oslib-win32.c | 14 ++++ util/trace-events | 5 +- util/vfio-helpers.c | 33 ++++---- 13 files changed, 331 insertions(+), 126 deletions(-)
Signature · 1 line
2.24.1
PATCH 1/13 util: vfio-helpers: Factor out and fix processing of existings ram blocks
Factor it out into common code when a new notifier is registered, just as done with the memory region notifier. This allows us to have the logic about how to process existing ram blocks at a central place (which will be extended soon).
Just like when adding a new ram block, we have to register the max_length. We don't have a way to get notified about resizes yet, and some memory would not be mapped when growing the ram block.
Note: Currently, ram blocks are only "fake resized". All memory (max_length) is accessible.
We can get rid of a bunch of functions in stubs/ram-block.c . Print the warning from inside qemu_vfio_ram_block_added().
--- exec.c | 5 +++++ hw/core/numa.c | 14 ++++++++++++++ include/exec/cpu-common.h | 1 + stubs/ram-block.c | 20 -------------------- util/vfio-helpers.c | 28 +++++++--------------------- 5 files changed, 27 insertions(+), 41 deletions(-)
exec.c +5 -0
@@ -2017,6 +2017,11 @@ ram_addr_t qemu_ram_get_used_length(RAMBlock *rb)return rb->used_length; } +ram_addr_t qemu_ram_get_max_length (RAMBlock *rb) +{ +return rb->max_length; +} +bool qemu_ram_is_shared (RAMBlock *rb) {return rb->flags &RAM_SHARED ;
hw/core/numa.c +14 -0
@@ -899,9 +899,23 @@ void query_numa_node_mem(NumaNodeMem node_mem[], MachineState *ms) } } +static int ram_block_notify_add_single (RAMBlock *rb,void *opaque) +{ +ram_addr_t size =qemu_ram_get_max_length (rb); +void *host =qemu_ram_get_host_addr (rb); +RAMBlockNotifier *notifier = opaque; + +if (host) { + notifier->ram_block_added (notifier, host, size); + } +return 0 ; +} +void ram_block_notifier_add (RAMBlockNotifier *n) {QLIST_INSERT_HEAD (&ram_list.ramblock_notifiers, n, next); +/* Notify about all existing ram blocks. */ +qemu_ram_foreach_block (ram_block_notify_add_single, n); }void ram_block_notifier_remove (RAMBlockNotifier *n)
include/exec/cpu-common.h +1 -0
@@ -59,6 +59,7 @@ const char *qemu_ram_get_idstr(RAMBlock *rb);void *qemu_ram_get_host_addr (RAMBlock *rb);ram_addr_t qemu_ram_get_offset (RAMBlock *rb);ram_addr_t qemu_ram_get_used_length (RAMBlock *rb); +ram_addr_t qemu_ram_get_max_length (RAMBlock *rb);bool qemu_ram_is_shared (RAMBlock *rb);bool qemu_ram_is_uf_zeroable (RAMBlock *rb);void qemu_ram_set_uf_zeroable (RAMBlock *rb);
stubs/ram-block.c +0 -20
@@ -2,21 +2,6 @@#include "exec/ramlist.h" #include "exec/cpu-common.h" -void *qemu_ram_get_host_addr (RAMBlock *rb) -{ -return 0 ; -} - -ram_addr_t qemu_ram_get_offset (RAMBlock *rb) -{ -return 0 ; -} - -ram_addr_t qemu_ram_get_used_length (RAMBlock *rb) -{ -return 0 ; -} -void ram_block_notifier_add (RAMBlockNotifier *n) { }
@@ -24,8 +9,3 @@ void ram_block_notifier_add(RAMBlockNotifier *n)void ram_block_notifier_remove (RAMBlockNotifier *n) { } - -int qemu_ram_foreach_block (RAMBlockIterFunc func,void *opaque) -{ -return 0 ; -}
util/vfio-helpers.c +7 -21
@@ -376,8 +376,13 @@ static void qemu_vfio_ram_block_added(RAMBlockNotifier *n, void *host, size_t size) {QEMUVFIOState *s =container_of (n, QEMUVFIOState, ram_notifier); +int ret; +trace_qemu_vfio_ram_block_added (s, host, size); -qemu_vfio_dma_map (s, host, size, false,NULL ); + ret =qemu_vfio_dma_map (s, host, size, false,NULL ); +if (ret) { +error_report ("qemu_vfio_dma_map(%p, %zu) failed: %d" , host, size, ret); + } } static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n,
@@ -390,33 +395,14 @@ static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n, } } -static int qemu_vfio_init_ramblock (RAMBlock *rb,void *opaque) -{ -void *host_addr =qemu_ram_get_host_addr (rb); -ram_addr_t length =qemu_ram_get_used_length (rb); -int ret; -QEMUVFIOState *s = opaque; - -if (!host_addr) { -return 0 ; - } - ret =qemu_vfio_dma_map (s, host_addr, length, false,NULL ); -if (ret) { -fprintf (stderr,"qemu_vfio_init_ramblock: failed %p %" PRId64"\n" , - host_addr, (uint64_t )length); - } -return 0 ; -} -static void qemu_vfio_open_common (QEMUVFIOState *s) {qemu_mutex_init (&s->lock); s->ram_notifier.ram_block_added = qemu_vfio_ram_block_added; s->ram_notifier.ram_block_removed = qemu_vfio_ram_block_removed; -ram_block_notifier_add (&s->ram_notifier); s->low_water_mark =QEMU_VFIO_IOVA_MIN ; s->high_water_mark =QEMU_VFIO_IOVA_MAX ; -qemu_ram_foreach_block (qemu_vfio_init_ramblock, s); +ram_block_notifier_add (&s->ram_notifier); }/**
Signature · 1 line
2.24.1
PATCH 2/13 exec: Factor out setting ram settings (madvise ...) into qemu_ram_apply_settings()
In reply to David HildenbrandFactor all settings out into qemu_ram_apply_settings().
For memory_try_enable_merging(), the important bit is that it won't be called with XEN - which is now still the case as new_block->host will remain NULL.
--- exec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
exec.c +10 -5
@@ -2121,6 +2121,15 @@ static int memory_try_enable_merging(void *addr, size_t len)return qemu_madvise (addr, len,QEMU_MADV_MERGEABLE ); } +static void qemu_ram_apply_settings (void *host,size_t length) +{ +memory_try_enable_merging (host, length); +qemu_ram_setup_dump (host, length); +qemu_madvise (host, length,QEMU_MADV_HUGEPAGE ); +/* MADV_DONTFORK is also needed by KVM in absence of synchronous MMU */ +qemu_madvise (host, length,QEMU_MADV_DONTFORK ); +} +/* Only legal before guest might have detected the memory size: e.g. on * incoming migration, or right after reset. *
@@ -2271,7 +2280,6 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared)qemu_mutex_unlock_ramlist ();return ; } -memory_try_enable_merging (new_block->host, new_block->max_length); } }
@@ -2309,10 +2317,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared) DIRTY_CLIENTS_ALL);if (new_block->host) { -qemu_ram_setup_dump (new_block->host, new_block->max_length); -qemu_madvise (new_block->host, new_block->max_length,QEMU_MADV_HUGEPAGE ); -/* MADV_DONTFORK is also needed by KVM in absence of synchronous MMU */ -qemu_madvise (new_block->host, new_block->max_length,QEMU_MADV_DONTFORK ); +qemu_ram_apply_settings (new_block->host, new_block->max_length);ram_block_notify_add (new_block->host, new_block->max_length); } }
Signature · 1 line
2.24.1
Re: [PATCH v1 02/13] exec: Factor out setting ram settings (madvise ...) into qemu_ram_apply_settings()
Quoted 6 lines · David Hildenbrand
Factor all settings out into
qemu_ram_apply_settings().For
memory_try_enable_merging(), the important bit is that it won't be called with XEN - which is now still the case asnew_block->hostwill remain NULL.
Quoted 6 lines
Cc Richard Henderson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- exec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
r~
PATCH 3/13 exec: Reuse qemu_ram_apply_settings() in qemu_ram_remap()
In reply to David HildenbrandI don't see why we shouldn't apply all settings to make it look like the surrounding RAM (and enable proper VMA merging).
Note: memory backend settings might have overridden these settings. We would need a callback to let the memory backend fix that up.
--- exec.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
exec.c +1 -2
@@ -2552,8 +2552,7 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length) length, addr);exit (1 ); } -memory_try_enable_merging (vaddr, length); -qemu_ram_setup_dump (vaddr, length); +qemu_ram_apply_settings (vaddr, length); } } }
Signature · 1 line
2.24.1
Re: [PATCH v1 03/13] exec: Reuse qemu_ram_apply_settings() in qemu_ram_remap()
Quoted 6 lines · David Hildenbrand
I don't see why we shouldn't apply all settings to make it look like the surrounding RAM (and enable proper VMA merging).
Note: memory backend settings might have overridden these settings. We would need a callback to let the memory backend fix that up.
Quoted 6 lines
Cc Richard Henderson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- exec.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
r~
PATCH 4/13 exec: Drop "shared" parameter from ram_block_add()
In reply to David HildenbrandProperly store it in the flags of the ram block instead (and the flag even already exists and is used).
E.g., qemu_ram_is_shared() now properly succeeds on all ram blocks that are actually shared.
--- exec.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
exec.c +8 -4
@@ -2249,7 +2249,7 @@ static void dirty_memory_extend(ram_addr_t old_ram_size, } } -static void ram_block_add (RAMBlock *new_block,Error **errp,bool shared) +static void ram_block_add (RAMBlock *new_block,Error **errp) {RAMBlock *block;RAMBlock *last_block =NULL ;
@@ -2272,7 +2272,8 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared) } }else { new_block->host =phys_mem_alloc (new_block->max_length, - &new_block->mr->align, shared); + &new_block->mr->align, +qemu_ram_is_shared (new_block));if (!new_block->host) { error_setg_errno(errp, errno, "cannot set up guest memory '%s'",
@@ -2376,7 +2377,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,return NULL ; } -ram_block_add (new_block, &local_err, ram_flags &RAM_SHARED ); +ram_block_add (new_block, &local_err);if (local_err) {g_free (new_block);error_propagate (errp, local_err);
@@ -2438,10 +2439,13 @@ RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,if (host) { new_block->flags |=RAM_PREALLOC ; } +if (share) { + new_block->flags |=RAM_SHARED ; + }if (resizeable) { new_block->flags |=RAM_RESIZEABLE ; } -ram_block_add (new_block, &local_err, share); +ram_block_add (new_block, &local_err);if (local_err) {g_free (new_block);error_propagate (errp, local_err);
Signature · 1 line
2.24.1
Re: [PATCH v1 04/13] exec: Drop "shared" parameter from ram_block_add()
Quoted 6 lines · David Hildenbrand
Properly store it in the flags of the ram block instead (and the flag even already exists and is used).
E.g.,
qemu_ram_is_shared()now properly succeeds on all ram blocks that are actually shared.
Quoted 6 lines
Cc Richard Henderson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- exec.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
r~
PATCH 5/13 util/mmap-alloc: Factor out calculation of pagesize to mmap_pagesize()
In reply to David HildenbrandFactor it out and add a comment.
--- util/mmap-alloc.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
util/mmap-alloc.c +12 -9
@@ -82,17 +82,27 @@ size_t qemu_mempath_getpagesize(const char *mem_path)return qemu_real_host_page_size; } +static inline size_t mmap_pagesize (int fd) +{ +#if defined(__powerpc64__) && defined(__linux__) +/* Mappings in the same segment must share the same page size */ +return qemu_fd_getpagesize (fd); +#else +return qemu_real_host_page_size; +#endif +} +void *qemu_ram_mmap (int fd,size_t size,size_t align,bool shared,bool is_pmem) { +const size_t pagesize =mmap_pagesize (fd);int flags;int map_sync_flags =0 ;int guardfd;size_t offset; -size_t pagesize;size_t total;void *guardptr;void *ptr;
@@ -113,7 +123,6 @@ void *qemu_ram_mmap(int fd,* anonymous memory is OK. */ flags =MAP_PRIVATE ; - pagesize =qemu_fd_getpagesize (fd);if (fd ==-1 || pagesize == qemu_real_host_page_size) { guardfd =-1 ; flags |=MAP_ANONYMOUS ;
@@ -123,7 +132,6 @@ void *qemu_ram_mmap(int fd, }#else guardfd =-1 ; - pagesize = qemu_real_host_page_size; flags =MAP_PRIVATE |MAP_ANONYMOUS ;#endif
@@ -198,15 +206,10 @@ void *qemu_ram_mmap(int fd,void qemu_ram_munmap (int fd,void *ptr,size_t size) { -size_t pagesize; +const size_t pagesize =mmap_pagesize (fd);if (ptr) {/* Unmap both the RAM block and the guard page */ -#if defined(__powerpc64__) && defined(__linux__) - pagesize =qemu_fd_getpagesize (fd); -#else - pagesize = qemu_real_host_page_size; -#endif munmap (ptr, size + pagesize); } }
Signature · 1 line
2.24.1
Re: [PATCH v1 05/13] util/mmap-alloc: Factor out calculation of pagesize to mmap_pagesize()
Hello, David.
Quoted 8 lines · David Hildenbrand
Factor it out and add a comment.
Cc "Michael S. Tsirkin" <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Greg Kurz <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Quoted 71 lines
--- util/mmap-alloc.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)util/mmap-alloc.c +12
-9@@ -82,17 +82,27 @@ size_t qemu_mempath_getpagesize(const char *mem_path)return qemu_real_host_page_size; } +static inline size_t mmap_pagesize (int fd) +{ +#if defined(__powerpc64__) && defined(__linux__) +/* Mappings in the same segment must share the same page size */ +return qemu_fd_getpagesize (fd); +#else +return qemu_real_host_page_size; +#endif +} +void *qemu_ram_mmap (int fd,size_t size,size_t align,bool shared,bool is_pmem) { +const size_t pagesize =mmap_pagesize (fd);int flags;int map_sync_flags =0 ;int guardfd;size_t offset; -size_t pagesize;size_t total;void *guardptr;void *ptr;@@ -113,7 +123,6 @@ void *qemu_ram_mmap(int fd,* anonymous memory is OK. */ flags =MAP_PRIVATE ; - pagesize =qemu_fd_getpagesize (fd);if (fd ==-1 || pagesize == qemu_real_host_page_size) { guardfd =-1 ; flags |=MAP_ANONYMOUS ;@@ -123,7 +132,6 @@ void *qemu_ram_mmap(int fd, }#else guardfd =-1 ; - pagesize = qemu_real_host_page_size; flags =MAP_PRIVATE |MAP_ANONYMOUS ;#endif @@ -198,15 +206,10 @@ void *qemu_ram_mmap(int fd,void qemu_ram_munmap (int fd,void *ptr,size_t size) { -size_t pagesize; +const size_t pagesize =mmap_pagesize (fd);if (ptr) {/* Unmap both the RAM block and the guard page */ -#if defined(__powerpc64__) && defined(__linux__) - pagesize =qemu_fd_getpagesize (fd); -#else - pagesize = qemu_real_host_page_size; -#endif munmap (ptr, size + pagesize); } }
Signature · 1 line
Murilo
Re: [PATCH v1 05/13] util/mmap-alloc: Factor out calculation of pagesize to mmap_pagesize()
In reply to David HildenbrandQuoted 11 lines · David Hildenbrand
Factor it out and add a comment.
Cc "Michael S. Tsirkin" <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Greg Kurz <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/mmap-alloc.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
r~
PATCH 6/13 util/mmap-alloc: Factor out reserving of a memory region to mmap_reserve()
In reply to David HildenbrandWe want to reserve a memory region without actually populating memory. Let's factor that out.
--- util/mmap-alloc.c | 58 +++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 25 deletions(-)
util/mmap-alloc.c +33 -25
@@ -82,6 +82,38 @@ size_t qemu_mempath_getpagesize(const char *mem_path)return qemu_real_host_page_size; } +/* +* Reserve a new memory region of the requested size to be used for mapping +* from the given fd (if any). +*/ +static void *mmap_reserve (size_t size,int fd) +{ +int flags =MAP_PRIVATE ; + +#if defined(__powerpc64__) && defined(__linux__) +/* +* On ppc64 mappings in the same segment (aka slice) must share the same +* page size. Since we will be re-allocating part of this segment +* from the supplied fd, we should make sure to use the same page size, to +* this end we mmap the supplied fd. In this case, set MAP_NORESERVE to +* avoid allocating backing store memory. +* We do this unless we are using the system page size, in which case +* anonymous memory is OK. +*/ +if (fd ==-1 ||qemu_fd_getpagesize (fd) == qemu_real_host_page_size) { + fd =-1 ; + flags |=MAP_ANONYMOUS ; + }else { + flags |=MAP_NORESERVE ; + } +#else + fd =-1 ; + flags |=MAP_ANONYMOUS ; +#endif + +return mmap (0 , size,PROT_NONE , flags, fd,0 ); +} +static inline size_t mmap_pagesize (int fd) {#if defined(__powerpc64__) && defined(__linux__)
@@ -101,7 +133,6 @@ void *qemu_ram_mmap(int fd,const size_t pagesize =mmap_pagesize (fd);int flags;int map_sync_flags =0 ; -int guardfd;size_t offset;size_t total;void *guardptr;
@@ -113,30 +144,7 @@ void *qemu_ram_mmap(int fd,*/ total = size + align; -#if defined(__powerpc64__) && defined(__linux__) -/* On ppc64 mappings in the same segment (aka slice) must share the same -* page size. Since we will be re-allocating part of this segment -* from the supplied fd, we should make sure to use the same page size, to -* this end we mmap the supplied fd. In this case, set MAP_NORESERVE to -* avoid allocating backing store memory. -* We do this unless we are using the system page size, in which case -* anonymous memory is OK. -*/ - flags =MAP_PRIVATE ; -if (fd ==-1 || pagesize == qemu_real_host_page_size) { - guardfd =-1 ; - flags |=MAP_ANONYMOUS ; - }else { - guardfd = fd; - flags |=MAP_NORESERVE ; - } -#else - guardfd =-1 ; - flags =MAP_PRIVATE |MAP_ANONYMOUS ; -#endif - - guardptr =mmap (0 , total,PROT_NONE , flags, guardfd,0 ); - + guardptr =mmap_reserve (total, fd);if (guardptr ==MAP_FAILED ) {return MAP_FAILED ; }
Signature · 1 line
2.24.1
Re: [PATCH v1 06/13] util/mmap-alloc: Factor out reserving of a memory region to mmap_reserve()
Hello, David.
Quoted 9 lines · David Hildenbrand
We want to reserve a memory region without actually populating memory. Let's factor that out.
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Quoted 88 lines
--- util/mmap-alloc.c | 58 +++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 25 deletions(-)util/mmap-alloc.c +33
-25@@ -82,6 +82,38 @@ size_t qemu_mempath_getpagesize(const char *mem_path)return qemu_real_host_page_size; } +/* +* Reserve a new memory region of the requested size to be used for mapping +* from the given fd (if any). +*/ +static void *mmap_reserve (size_t size,int fd) +{ +int flags =MAP_PRIVATE ; + +#if defined(__powerpc64__) && defined(__linux__) +/* +* On ppc64 mappings in the same segment (aka slice) must share the same +* page size. Since we will be re-allocating part of this segment +* from the supplied fd, we should make sure to use the same page size, to +* this end we mmap the supplied fd. In this case, set MAP_NORESERVE to +* avoid allocating backing store memory. +* We do this unless we are using the system page size, in which case +* anonymous memory is OK. +*/ +if (fd ==-1 ||qemu_fd_getpagesize (fd) == qemu_real_host_page_size) { + fd =-1 ; + flags |=MAP_ANONYMOUS ; + }else { + flags |=MAP_NORESERVE ; + } +#else + fd =-1 ; + flags |=MAP_ANONYMOUS ; +#endif + +return mmap (0 , size,PROT_NONE , flags, fd,0 ); +} +static inline size_t mmap_pagesize (int fd) {#if defined(__powerpc64__) && defined(__linux__)@@ -101,7 +133,6 @@ void *qemu_ram_mmap(int fd,const size_t pagesize =mmap_pagesize (fd);int flags;int map_sync_flags =0 ; -int guardfd;size_t offset;size_t total;void *guardptr;@@ -113,30 +144,7 @@ void *qemu_ram_mmap(int fd,*/ total = size + align; -#if defined(__powerpc64__) && defined(__linux__) -/* On ppc64 mappings in the same segment (aka slice) must share the same -* page size. Since we will be re-allocating part of this segment -* from the supplied fd, we should make sure to use the same page size, to -* this end we mmap the supplied fd. In this case, set MAP_NORESERVE to -* avoid allocating backing store memory. -* We do this unless we are using the system page size, in which case -* anonymous memory is OK. -*/ - flags =MAP_PRIVATE ; -if (fd ==-1 || pagesize == qemu_real_host_page_size) { - guardfd =-1 ; - flags |=MAP_ANONYMOUS ; - }else { - guardfd = fd; - flags |=MAP_NORESERVE ; - } -#else - guardfd =-1 ; - flags =MAP_PRIVATE |MAP_ANONYMOUS ; -#endif - - guardptr =mmap (0 , total,PROT_NONE , flags, guardfd,0 ); - + guardptr =mmap_reserve (total, fd);if (guardptr ==MAP_FAILED ) {return MAP_FAILED ; }
Signature · 1 line
Murilo
Re: [PATCH v1 06/13] util/mmap-alloc: Factor out reserving of a memory region to mmap_reserve()
In reply to David HildenbrandQuoted 39 lines · David Hildenbrand
We want to reserve a memory region without actually populating memory. Let's factor that out.
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/mmap-alloc.c | 58 +++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 25 deletions(-)util/mmap-alloc.c +18
-0@@ -82,6 +82,38 @@ size_t qemu_mempath_getpagesize(const char *mem_path)return qemu_real_host_page_size; } +/* +* Reserve a new memory region of the requested size to be used for mapping +* from the given fd (if any). +*/ +static void *mmap_reserve (size_t size,int fd) +{ +int flags =MAP_PRIVATE ; + +#if defined(__powerpc64__) && defined(__linux__) +/* +* On ppc64 mappings in the same segment (aka slice) must share the same +* page size. Since we will be re-allocating part of this segment +* from the supplied fd, we should make sure to use the same page size, to +* this end we mmap the supplied fd. In this case, set MAP_NORESERVE to +* avoid allocating backing store memory. +* We do this unless we are using the system page size, in which case +* anonymous memory is OK. +*/
util/mmap-alloc.c size_t qemu_mempath_getpagesize(const char *mem_path) · line 112
Quoted 10 lines
util/mmap-alloc.c +10
-0+if (fd ==-1 ||qemu_fd_getpagesize (fd) == qemu_real_host_page_size) { + fd =-1 ; + flags |=MAP_ANONYMOUS ; + }else { + flags |=MAP_NORESERVE ; + } +#else + fd =-1 ; + flags |=MAP_ANONYMOUS ; +#endif
Because this is just code movement,
But is there a reason not to add MAP_NORESERVE all of the time? It seems to match intent, in that we're reserving vma but not planning to use the memory, anonymous or not.
r~
Re: [PATCH v1 06/13] util/mmap-alloc: Factor out reserving of a memory region to mmap_reserve()
Quoted 52 lines · Richard Henderson
Quoted 39 lines · David Hildenbrand
We want to reserve a memory region without actually populating memory. Let's factor that out.
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/mmap-alloc.c | 58 +++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 25 deletions(-)util/mmap-alloc.c +18
-0@@ -82,6 +82,38 @@ size_t qemu_mempath_getpagesize(const char *mem_path)return qemu_real_host_page_size; } +/* +* Reserve a new memory region of the requested size to be used for mapping +* from the given fd (if any). +*/ +static void *mmap_reserve (size_t size,int fd) +{ +int flags =MAP_PRIVATE ; + +#if defined(__powerpc64__) && defined(__linux__) +/* +* On ppc64 mappings in the same segment (aka slice) must share the same +* page size. Since we will be re-allocating part of this segment +* from the supplied fd, we should make sure to use the same page size, to +* this end we mmap the supplied fd. In this case, set MAP_NORESERVE to +* avoid allocating backing store memory. +* We do this unless we are using the system page size, in which case +* anonymous memory is OK. +*/ util/mmap-alloc.c size_t qemu_mempath_getpagesize(const char *mem_path) · line 112
Quoted 10 lines
util/mmap-alloc.c +10
-0+if (fd ==-1 ||qemu_fd_getpagesize (fd) == qemu_real_host_page_size) { + fd =-1 ; + flags |=MAP_ANONYMOUS ; + }else { + flags |=MAP_NORESERVE ; + } +#else + fd =-1 ; + flags |=MAP_ANONYMOUS ; +#endif Because this is just code movement,
Quoted 5 lines
Reviewed-by Richard Henderson <[email protected]>But is there a reason not to add
MAP_NORESERVEall of the time? It seems to match intent, in that we're reserving vma but not planning to use the memory, anonymous or not.
AFAIK, if you mmap something PROT_NONE, it's an implicit MAP_NORESERVE. I keep setting in conditionally, because I am not sure if any POSIX system (or older kernel) might choke on MAP_NORESERVE. e.g.,
"In kernels before 2.6, this flag had effect only for private writable mappings." sounds like it would get ignored. But also sounds like it's somewhat Linux specific.
Signature · 3 lines
Thanks, David / dhildenb
PATCH 7/13 util/mmap-alloc: Factor out populating of memory to mmap_populate()
In reply to David HildenbrandWe want to populate memory within a reserved memory region. Let's factor that out.
--- util/mmap-alloc.c | 89 +++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 42 deletions(-)
util/mmap-alloc.c +47 -42
@@ -114,6 +114,50 @@ static void *mmap_reserve(size_t size, int fd)return mmap (0 , size,PROT_NONE , flags, fd,0 ); } +/* +* Populate memory in a reserved region from the given fd (if any). +*/ +static void *mmap_populate (void *ptr,size_t size,int fd,bool shared, +bool is_pmem) +{ +int map_sync_flags =0 ; +int flags =MAP_FIXED ; +void *new_ptr; + + flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; + flags |= shared ?MAP_SHARED :MAP_PRIVATE ; +if (shared && is_pmem) { + map_sync_flags =MAP_SYNC |MAP_SHARED_VALIDATE ; + } + + new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags | map_sync_flags, + fd,0 ); +if (new_ptr ==MAP_FAILED && map_sync_flags) { +if (errno ==ENOTSUP ) { +char *proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd); +char *file_name =g_malloc0 (PATH_MAX ); +int len =readlink (proc_link, file_name,PATH_MAX -1 ); + +if (len <0 ) { + len =0 ; + } + file_name[len] ='\0' ; +fprintf (stderr,"Warning: requesting persistence across crashes " +"for backend file %s failed. Proceeding without " +"persistence, data might become corrupted in case of host " +"crash.\n" , file_name); +g_free (proc_link); +g_free (file_name); + } +/* +* If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try +* again without these flags to handle backwards compatibility. +*/ + new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags, fd,0 ); + } +return new_ptr; +} +static inline size_t mmap_pagesize (int fd) {#if defined(__powerpc64__) && defined(__linux__)
@@ -131,12 +175,8 @@ void *qemu_ram_mmap(int fd, bool is_pmem) {const size_t pagesize =mmap_pagesize (fd); -int flags; -int map_sync_flags =0 ; -size_t offset; -size_t total; -void *guardptr; -void *ptr; +size_t offset, total; +void *ptr, *guardptr;/* * Note: this always allocates at least one extra page of virtual address
@@ -153,44 +193,9 @@ void *qemu_ram_mmap(int fd,/* Always align to host page size */ assert (align >= pagesize); - flags =MAP_FIXED ; - flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; - flags |= shared ?MAP_SHARED :MAP_PRIVATE ; -if (shared && is_pmem) { - map_sync_flags =MAP_SYNC |MAP_SHARED_VALIDATE ; - } - offset =QEMU_ALIGN_UP ((uintptr_t )guardptr, align) - (uintptr_t )guardptr; - ptr =mmap (guardptr + offset, size,PROT_READ |PROT_WRITE , - flags | map_sync_flags, fd,0 ); - -if (ptr ==MAP_FAILED && map_sync_flags) { -if (errno ==ENOTSUP ) { -char *proc_link, *file_name; -int len; - proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd); - file_name =g_malloc0 (PATH_MAX ); - len =readlink (proc_link, file_name,PATH_MAX -1 ); -if (len <0 ) { - len =0 ; - } - file_name[len] ='\0' ; -fprintf (stderr,"Warning: requesting persistence across crashes " -"for backend file %s failed. Proceeding without " -"persistence, data might become corrupted in case of host " -"crash.\n" , file_name); -g_free (proc_link); -g_free (file_name); - } -/* -* if map failed with MAP_SHARED_VALIDATE | MAP_SYNC, -* we will remove these flags to handle compatibility. -*/ - ptr =mmap (guardptr + offset, size,PROT_READ |PROT_WRITE , - flags, fd,0 ); - } - + ptr =mmap_populate (guardptr + offset, size, fd, shared, is_pmem);if (ptr ==MAP_FAILED ) {munmap (guardptr, total);return MAP_FAILED ;
Signature · 1 line
2.24.1
Re: [PATCH v1 07/13] util/mmap-alloc: Factor out populating of memory to mmap_populate()
Hello, David.
Quoted 10 lines · David Hildenbrand
We want to populate memory within a reserved memory region. Let's factor that out.
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
A minor comment below.
Quoted 10 lines
util/mmap-alloc.c | 89 +++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 42 deletions(-)util/mmap-alloc.c +0
-0@@ -114,6 +114,50 @@ static void *mmap_reserve(size_t size, int fd)return mmap (0 , size,PROT_NONE , flags, fd,0 ); }
util/mmap-alloc.c static void *mmap_reserve(size_t size, int fd) · line 125
Quoted 10 lines
util/mmap-alloc.c +9
-0+/* +* Populate memory in a reserved region from the given fd (if any). +*/ +static void *mmap_populate (void *ptr,size_t size,int fd,bool shared, +bool is_pmem) +{ +int map_sync_flags =0 ; +int flags =MAP_FIXED ; +void *new_ptr;
Do you think another name would be welcome here, e.g.: "populated_ptr" or "populated_memptr" or just "populated"?
Quoted 90 lines
util/mmap-alloc.c +37
-36+ + flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; + flags |= shared ?MAP_SHARED :MAP_PRIVATE ; +if (shared && is_pmem) { + map_sync_flags =MAP_SYNC |MAP_SHARED_VALIDATE ; + } + + new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags | map_sync_flags, + fd,0 ); +if (new_ptr ==MAP_FAILED && map_sync_flags) { +if (errno ==ENOTSUP ) { +char *proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd); +char *file_name =g_malloc0 (PATH_MAX ); +int len =readlink (proc_link, file_name,PATH_MAX -1 ); + +if (len <0 ) { + len =0 ; + } + file_name[len] ='\0' ; +fprintf (stderr,"Warning: requesting persistence across crashes " +"for backend file %s failed. Proceeding without " +"persistence, data might become corrupted in case of host " +"crash.\n" , file_name); +g_free (proc_link); +g_free (file_name); + } +/* +* If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try +* again without these flags to handle backwards compatibility. +*/ + new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags, fd,0 ); + } +return new_ptr; +} +static inline size_t mmap_pagesize (int fd) {#if defined(__powerpc64__) && defined(__linux__)@@ -131,12 +175,8 @@ void *qemu_ram_mmap(int fd, bool is_pmem) {const size_t pagesize =mmap_pagesize (fd); -int flags; -int map_sync_flags =0 ; -size_t offset; -size_t total; -void *guardptr; -void *ptr; +size_t offset, total; +void *ptr, *guardptr;/* * Note: this always allocates at least one extra page of virtual address @@ -153,44 +193,9 @@ void *qemu_ram_mmap(int fd,/* Always align to host page size */ assert (align >= pagesize); - flags =MAP_FIXED ; - flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; - flags |= shared ?MAP_SHARED :MAP_PRIVATE ; -if (shared && is_pmem) { - map_sync_flags =MAP_SYNC |MAP_SHARED_VALIDATE ; - } - offset =QEMU_ALIGN_UP ((uintptr_t )guardptr, align) - (uintptr_t )guardptr; - ptr =mmap (guardptr + offset, size,PROT_READ |PROT_WRITE , - flags | map_sync_flags, fd,0 ); - -if (ptr ==MAP_FAILED && map_sync_flags) { -if (errno ==ENOTSUP ) { -char *proc_link, *file_name; -int len; - proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd); - file_name =g_malloc0 (PATH_MAX ); - len =readlink (proc_link, file_name,PATH_MAX -1 ); -if (len <0 ) { - len =0 ; - } - file_name[len] ='\0' ; -fprintf (stderr,"Warning: requesting persistence across crashes " -"for backend file %s failed. Proceeding without " -"persistence, data might become corrupted in case of host " -"crash.\n" , file_name); -g_free (proc_link); -g_free (file_name); - } -/* -* if map failed with MAP_SHARED_VALIDATE | MAP_SYNC,
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 201
Quoted 10 lines
util/mmap-alloc.c +1
-6-* we will remove these flags to handle compatibility. -*/ - ptr =mmap (guardptr + offset, size,PROT_READ |PROT_WRITE , - flags, fd,0 ); - } - + ptr =mmap_populate (guardptr + offset, size, fd, shared, is_pmem);if (ptr ==MAP_FAILED ) {munmap (guardptr, total);return MAP_FAILED ;
--
Murilo
Re: [PATCH v1 07/13] util/mmap-alloc: Factor out populating of memory to mmap_populate()
Quoted 29 lines · Murilo Opsfelder Araújo
Hello, David.
Quoted 10 lines · David Hildenbrand
We want to populate memory within a reserved memory region. Let's factor that out.
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
A minor comment below.
Quoted 11 lines
util/mmap-alloc.c | 89 +++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 42 deletions(-)util/mmap-alloc.c +0
-0@@ -114,6 +114,50 @@ static void *mmap_reserve(size_t size, int fd)return mmap (0 , size,PROT_NONE , flags, fd,0 ); }
Quoted 12 lines
util/mmap-alloc.c static void *mmap_reserve(size_t size, int fd) · line 125
Quoted 9 lines
util/mmap-alloc.c +9
-0+/* +* Populate memory in a reserved region from the given fd (if any). +*/ +static void *mmap_populate (void *ptr,size_t size,int fd,bool shared, +bool is_pmem) +{ +int map_sync_flags =0 ; +int flags =MAP_FIXED ; +void *new_ptr;Do you think another name would be welcome here, e.g.: "
populated_ptr" or "populated_memptr" or just "populated"?
I'll go with populated_ptr - thanks!
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 07/13] util/mmap-alloc: Factor out populating of memory to mmap_populate()
In reply to David HildenbrandQuoted 9 lines · David Hildenbrand
We want to populate memory within a reserved memory region. Let's factor that out.
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Quoted 3 lines
--- util/mmap-alloc.c | 89 +++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 42 deletions(-)
r~
PATCH 8/13 util/mmap-alloc: Prepare for resizable mmaps
In reply to David HildenbrandWhen shrinking a mmap we want to re-reserve the already populated area. When growing a memory region, we want to populate starting with a given fd_offset. Prepare by allowing to pass these parameters.
Also, let's make sure we always process full pages, to avoid unmapping/remapping pages that are already in use when growing/shrinking. (existing callers seem to guarantee this, but that's not obvious)
--- util/mmap-alloc.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)
util/mmap-alloc.c +21 -11
@@ -83,12 +83,12 @@ size_t qemu_mempath_getpagesize(const char *mem_path) }/* -* Reserve a new memory region of the requested size to be used for mapping -* from the given fd (if any). +* Reserve a new memory region of the requested size or re-reserve parts +* of an existing region to be used for mapping from the given fd (if any). */ -static void *mmap_reserve (size_t size,int fd) +static void *mmap_reserve (void *ptr,size_t size,int fd) { -int flags =MAP_PRIVATE ; +int flags =MAP_PRIVATE | (ptr ?MAP_FIXED :0 );#if defined(__powerpc64__) && defined(__linux__)/*
@@ -111,19 +111,23 @@ static void *mmap_reserve(size_t size, int fd) flags |=MAP_ANONYMOUS ;#endif -return mmap (0 , size,PROT_NONE , flags, fd,0 ); +return mmap (ptr, size,PROT_NONE , flags, fd,0 ); }/* * Populate memory in a reserved region from the given fd (if any). */ -static void *mmap_populate (void *ptr,size_t size,int fd,bool shared, -bool is_pmem) +static void *mmap_populate (void *ptr,size_t size,int fd,size_t fd_offset, +bool shared,bool is_pmem) {int map_sync_flags =0 ;int flags =MAP_FIXED ;void *new_ptr; +if (fd ==-1 ) { + fd_offset =0 ; + } + flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; flags |= shared ?MAP_SHARED :MAP_PRIVATE ;if (shared && is_pmem) {
@@ -131,7 +135,7 @@ static void *mmap_populate(void *ptr, size_t size, int fd, bool shared, } new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags | map_sync_flags, - fd,0 ); + fd, fd_offset);if (new_ptr ==MAP_FAILED && map_sync_flags) {if (errno ==ENOTSUP ) {char *proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd);
@@ -153,7 +157,7 @@ static void *mmap_populate(void *ptr, size_t size, int fd, bool shared,* If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try * again without these flags to handle backwards compatibility. */ - new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags, fd,0 ); + new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags, fd, fd_offset); }return new_ptr; }
@@ -178,13 +182,16 @@ void *qemu_ram_mmap(int fd,size_t offset, total;void *ptr, *guardptr; +/* we can only map whole pages */ + size =QEMU_ALIGN_UP (size, pagesize); +/* * Note: this always allocates at least one extra page of virtual address * space, even if size is already aligned. */ total = size + align; - guardptr =mmap_reserve (total, fd); + guardptr =mmap_reserve (0 , total, fd);if (guardptr ==MAP_FAILED ) {return MAP_FAILED ; }
@@ -195,7 +202,7 @@ void *qemu_ram_mmap(int fd, offset =QEMU_ALIGN_UP ((uintptr_t )guardptr, align) - (uintptr_t )guardptr; - ptr =mmap_populate (guardptr + offset, size, fd, shared, is_pmem); + ptr =mmap_populate (guardptr + offset, size, fd,0 , shared, is_pmem);if (ptr ==MAP_FAILED ) {munmap (guardptr, total);return MAP_FAILED ;
@@ -221,6 +228,9 @@ void qemu_ram_munmap(int fd, void *ptr, size_t size) {const size_t pagesize =mmap_pagesize (fd); +/* we can only map whole pages */ + size =QEMU_ALIGN_UP (size, pagesize); +if (ptr) {/* Unmap both the RAM block and the guard page */ munmap (ptr, size + pagesize);
Signature · 1 line
2.24.1
Re: [PATCH v1 08/13] util/mmap-alloc: Prepare for resizable mmaps
Hello, David.
Quoted 85 lines · On Monday, February 3, 2020 3:31:20 PM -03 David Hildenbrand wrote:
When shrinking a mmap we want to re-reserve the already populated area. When growing a memory region, we want to populate starting with a given
fd_offset. Prepare by allowing to pass these parameters.Also, let's make sure we always process full pages, to avoid unmapping/remapping pages that are already in use when growing/shrinking. (existing callers seem to guarantee this, but that's not obvious)
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/mmap-alloc.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)util/mmap-alloc.c +11
-7@@ -83,12 +83,12 @@ size_t qemu_mempath_getpagesize(const char *mem_path) }/* -* Reserve a new memory region of the requested size to be used for mapping -* from the given fd (if any). +* Reserve a new memory region of the requested size or re-reserve parts +* of an existing region to be used for mapping from the given fd (if any). */ -static void *mmap_reserve (size_t size,int fd) +static void *mmap_reserve (void *ptr,size_t size,int fd) { -int flags =MAP_PRIVATE ; +int flags =MAP_PRIVATE | (ptr ?MAP_FIXED :0 );#if defined(__powerpc64__) && defined(__linux__)/* @@ -111,19 +111,23 @@ static void *mmap_reserve(size_t size, int fd) flags |=MAP_ANONYMOUS ;#endif -return mmap (0 , size,PROT_NONE , flags, fd,0 ); +return mmap (ptr, size,PROT_NONE , flags, fd,0 ); }/* * Populate memory in a reserved region from the given fd (if any). */ -static void *mmap_populate (void *ptr,size_t size,int fd,bool shared, -bool is_pmem) +static void *mmap_populate (void *ptr,size_t size,int fd,size_t fd_offset, + bool shared, bool is_pmem) {int map_sync_flags =0 ;int flags =MAP_FIXED ;void *new_ptr; +if (fd ==-1 ) { + fd_offset =0 ; + } + flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; flags |= shared ?MAP_SHARED :MAP_PRIVATE ;if (shared && is_pmem) {@@ -131,7 +135,7 @@ static void *mmap_populate(void *ptr, size_t size, int fd, bool shared, } new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags | map_sync_flags, - fd,0 ); + fd, fd_offset);if (new_ptr ==MAP_FAILED && map_sync_flags) {if (errno ==ENOTSUP ) {char *proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd);@@ -153,7 +157,7 @@ static void *mmap_populate(void *ptr, size_t size, intfd, bool shared, * If mmap failed with
MAP_SHARED_VALIDATE|MAP_SYNC, we will try * again without these flags to handle backwards compatibility. */util/mmap-alloc.c +1
-1- new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags, fd,0 ); + new_ptr = mmap(ptr, size, PROT_READ | PROT_WRITE, flags, fd,
fd_offset); }return new_ptr;}
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 187
Quoted 6 lines
util/mmap-alloc.c +3
-0@@ -178,13 +182,16 @@ void *qemu_ram_mmap(int fd,size_t offset, total;void *ptr, *guardptr; +/* we can only map whole pages */ + size =QEMU_ALIGN_UP (size, pagesize); +
Caller already rounds up size to block->page_size.
Why this QEMU_ALIGN_UP is necessary?
Quoted 5 lines
util/mmap-alloc.c +0
-0/* * Note: this always allocates at least one extra page of virtual address * space, even if size is already aligned.
*/ total = size + align;
If size was aligned above with pagesize boundary, why would this align be necessary?
Can the pagesize differ from memory region align?
Quoted 17 lines
util/mmap-alloc.c +2
-2- guardptr =mmap_reserve (total, fd); + guardptr =mmap_reserve (0 , total, fd);if (guardptr ==MAP_FAILED ) {return MAP_FAILED ; }@@ -195,7 +202,7 @@ void *qemu_ram_mmap(int fd, offset =QEMU_ALIGN_UP ((uintptr_t )guardptr, align) - (uintptr_t )guardptr; - ptr =mmap_populate (guardptr + offset, size, fd, shared, is_pmem); + ptr =mmap_populate (guardptr + offset, size, fd,0 , shared, is_pmem);if (ptr ==MAP_FAILED ) {munmap (guardptr, total);return MAP_FAILED;
util/mmap-alloc.c void qemu_ram_munmap(int fd, void *ptr, size_t size) · line 233
Quoted 6 lines
util/mmap-alloc.c +3
-0@@ -221,6 +228,9 @@ void qemu_ram_munmap(int fd, void *ptr, size_t size) {const size_t pagesize =mmap_pagesize (fd); +/* we can only map whole pages */ + size =QEMU_ALIGN_UP (size, pagesize); +
I'm trying to understand why this align is necessary, too.
Quoted 3 lines
util/mmap-alloc.c +0
-0if (ptr) {/* Unmap both the RAM block and the guard page */ munmap (ptr, size + pagesize);
Signature · 1 line
Murilo
Re: [PATCH v1 08/13] util/mmap-alloc: Prepare for resizable mmaps
Quoted 91 lines · Murilo Opsfelder Araújo
Hello, David.
Quoted 88 lines · On Monday, February 3, 2020 3:31:20 PM -03 David Hildenbrand wrote:
When shrinking a mmap we want to re-reserve the already populated area. When growing a memory region, we want to populate starting with a given
fd_offset. Prepare by allowing to pass these parameters.Also, let's make sure we always process full pages, to avoid unmapping/remapping pages that are already in use when growing/shrinking. (existing callers seem to guarantee this, but that's not obvious)
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/mmap-alloc.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)util/mmap-alloc.c +11
-7@@ -83,12 +83,12 @@ size_t qemu_mempath_getpagesize(const char *mem_path) }/* -* Reserve a new memory region of the requested size to be used for mapping -* from the given fd (if any). +* Reserve a new memory region of the requested size or re-reserve parts +* of an existing region to be used for mapping from the given fd (if any). */ -static void *mmap_reserve (size_t size,int fd) +static void *mmap_reserve (void *ptr,size_t size,int fd) { -int flags =MAP_PRIVATE ; +int flags =MAP_PRIVATE | (ptr ?MAP_FIXED :0 );#if defined(__powerpc64__) && defined(__linux__)/* @@ -111,19 +111,23 @@ static void *mmap_reserve(size_t size, int fd) flags |=MAP_ANONYMOUS ;#endif -return mmap (0 , size,PROT_NONE , flags, fd,0 ); +return mmap (ptr, size,PROT_NONE , flags, fd,0 ); }/* * Populate memory in a reserved region from the given fd (if any). */ -static void *mmap_populate (void *ptr,size_t size,int fd,bool shared, -bool is_pmem) +static void *mmap_populate (void *ptr,size_t size,int fd,size_t fd_offset, + bool shared, bool is_pmem) {int map_sync_flags =0 ;int flags =MAP_FIXED ;void *new_ptr; +if (fd ==-1 ) { + fd_offset =0 ; + } + flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; flags |= shared ?MAP_SHARED :MAP_PRIVATE ;if (shared && is_pmem) {@@ -131,7 +135,7 @@ static void *mmap_populate(void *ptr, size_t size, int fd, bool shared, } new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags | map_sync_flags, - fd,0 ); + fd, fd_offset);if (new_ptr ==MAP_FAILED && map_sync_flags) {if (errno ==ENOTSUP ) {char *proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd);@@ -153,7 +157,7 @@ static void *mmap_populate(void *ptr, size_t size, intfd, bool shared, * If mmap failed with
MAP_SHARED_VALIDATE|MAP_SYNC, we will try * again without these flags to handle backwards compatibility. */util/mmap-alloc.c +1
-1- new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags, fd,0 ); + new_ptr = mmap(ptr, size, PROT_READ | PROT_WRITE, flags, fd,
fd_offset); }return new_ptr;}
util/mmap-alloc.c +0
-0@@ -178,13 +182,16 @@ void *qemu_ram_mmap(int fd,size_t offset, total;void *ptr, *guardptr;
Quoted 7 lines
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 187
Quoted 3 lines
util/mmap-alloc.c +3
-0+/* we can only map whole pages */ + size =QEMU_ALIGN_UP (size, pagesize); +Caller already rounds up size to
block->page_size.Why this
QEMU_ALIGN_UPis necessary?
Thanks for having a look
I guess you read the patch description, right? :)
"(existing callers seem to guarantee this, but that's
not obvious)"
Do you prefer a g_assert(IS_ALIGNED()) instead?
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 08/13] util/mmap-alloc: Prepare for resizable mmaps
Hello, David.
Quoted 128 lines · On Thursday, February 6, 2020 5:52:26 AM -03 David Hildenbrand wrote:
Quoted 119 lines · Murilo Opsfelder Araújo
Hello, David.
Quoted 116 lines · On Monday, February 3, 2020 3:31:20 PM -03 David Hildenbrand wrote:
When shrinking a mmap we want to re-reserve the already populated area. When growing a memory region, we want to populate starting with a given
fd_offset. Prepare by allowing to pass these parameters.Also, let's make sure we always process full pages, to avoid unmapping/remapping pages that are already in use when growing/shrinking. (existing callers seem to guarantee this, but that's not obvious)
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
util/mmap-alloc.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)util/mmap-alloc.c +0
-1@@ -83,12 +83,12 @@ size_t qemu_mempath_getpagesize(const char *mem_path) }/* -* Reserve a new memory region of the requested size to be used for mapping - * from the given fd (if any).
+ * Reserve a new memory region of the requested size or re-reserve parts
+ * of an existing region to be used for mapping from the given fd (if any). */util/mmap-alloc.c +8
-5-static void *mmap_reserve (size_t size,int fd) +static void *mmap_reserve (void *ptr,size_t size,int fd) { -int flags =MAP_PRIVATE ; +int flags =MAP_PRIVATE | (ptr ?MAP_FIXED :0 );#if defined(__powerpc64__) && defined(__linux__)/* @@ -111,19 +111,23 @@ static void *mmap_reserve(size_t size, int fd) flags |=MAP_ANONYMOUS ;#endif -return mmap (0 , size,PROT_NONE , flags, fd,0 ); +return mmap (ptr, size,PROT_NONE , flags, fd,0 ); }/* * Populate memory in a reserved region from the given fd (if any). */ -static void *mmap_populate (void *ptr,size_t size,int fd,bool shared, -bool is_pmem) +static void *mmap_populate (void *ptr,size_t size,int fd,size_t fd_offset, + bool shared, bool is_pmem) {int map_sync_flags =0 ;int flags =MAP_FIXED ;void *new_ptr; +if (fd ==-1 ) { + fd_offset =0 ; + } + flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; flags |= shared ?MAP_SHARED :MAP_PRIVATE ;if (shared && is_pmem) {@@ -131,7 +135,7 @@ static void *mmap_populate(void *ptr, size_t size,int
fd, bool shared, }
new_ptr= mmap(ptr, size,PROT_READ|PROT_WRITE, flags |
map_sync_flags, - fd, 0);util/mmap-alloc.c +1
-0+ fd, fd_offset);if (new_ptr ==MAP_FAILED && map_sync_flags) {if (errno ==ENOTSUP ) {char *proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd);@@ -153,7 +157,7 @@ static void *mmap_populate(void *ptr, size_t size,int
fd, bool shared, * If mmap failed withMAP_SHARED_VALIDATE|MAP_SYNC, we will try * again without these flags to handle backwards compatibility. */util/mmap-alloc.c +1
-1- new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags, fd,0 ); + new_ptr = mmap(ptr, size, PROT_READ | PROT_WRITE, flags, fd,
fd_offset); }return new_ptr;}
util/mmap-alloc.c +0
-0@@ -178,13 +182,16 @@ void *qemu_ram_mmap(int fd,size_t offset, total;void *ptr, *guardptr;Quoted 7 lines
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 187
Quoted 3 lines
util/mmap-alloc.c +3
-0+/* we can only map whole pages */ + size =QEMU_ALIGN_UP (size, pagesize); +Caller already rounds up size to
block->page_size.Why this
QEMU_ALIGN_UPis necessary?
Quoted 8 lines
Thanks for having a look
I guess you read the patch description, right? :)
"(existing callers seem to guarantee this, but that's
not obvious)"Do you prefer a g_assert(IS_ALIGNED()) instead?
I guess you mean QEMU_IS_ALIGNED(). But yes, I think we could just check alignments here, so callers do the alignments first.
We could have, for example, a new helper function mmap_align(int size) that returned size already aligned.
Quoted 2 lines
Thanks!
--
Murilo
Re: [PATCH v1 08/13] util/mmap-alloc: Prepare for resizable mmaps
Quoted 135 lines · Murilo Opsfelder Araújo
Hello, David.
Quoted 132 lines · On Thursday, February 6, 2020 5:52:26 AM -03 David Hildenbrand wrote:
Quoted 119 lines · Murilo Opsfelder Araújo
Hello, David.
Quoted 116 lines · On Monday, February 3, 2020 3:31:20 PM -03 David Hildenbrand wrote:
When shrinking a mmap we want to re-reserve the already populated area. When growing a memory region, we want to populate starting with a given
fd_offset. Prepare by allowing to pass these parameters.Also, let's make sure we always process full pages, to avoid unmapping/remapping pages that are already in use when growing/shrinking. (existing callers seem to guarantee this, but that's not obvious)
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>---
util/mmap-alloc.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)util/mmap-alloc.c +0
-1@@ -83,12 +83,12 @@ size_t qemu_mempath_getpagesize(const char *mem_path) }/* -* Reserve a new memory region of the requested size to be used for mapping - * from the given fd (if any).
+ * Reserve a new memory region of the requested size or re-reserve parts
+ * of an existing region to be used for mapping from the given fd (if any). */util/mmap-alloc.c +8
-5-static void *mmap_reserve (size_t size,int fd) +static void *mmap_reserve (void *ptr,size_t size,int fd) { -int flags =MAP_PRIVATE ; +int flags =MAP_PRIVATE | (ptr ?MAP_FIXED :0 );#if defined(__powerpc64__) && defined(__linux__)/* @@ -111,19 +111,23 @@ static void *mmap_reserve(size_t size, int fd) flags |=MAP_ANONYMOUS ;#endif -return mmap (0 , size,PROT_NONE , flags, fd,0 ); +return mmap (ptr, size,PROT_NONE , flags, fd,0 ); }/* * Populate memory in a reserved region from the given fd (if any). */ -static void *mmap_populate (void *ptr,size_t size,int fd,bool shared, -bool is_pmem) +static void *mmap_populate (void *ptr,size_t size,int fd,size_t fd_offset, + bool shared, bool is_pmem) {int map_sync_flags =0 ;int flags =MAP_FIXED ;void *new_ptr; +if (fd ==-1 ) { + fd_offset =0 ; + } + flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; flags |= shared ?MAP_SHARED :MAP_PRIVATE ;if (shared && is_pmem) {@@ -131,7 +135,7 @@ static void *mmap_populate(void *ptr, size_t size,int
fd, bool shared, }
new_ptr= mmap(ptr, size,PROT_READ|PROT_WRITE, flags |
map_sync_flags, - fd, 0);util/mmap-alloc.c +1
-0+ fd, fd_offset);if (new_ptr ==MAP_FAILED && map_sync_flags) {if (errno ==ENOTSUP ) {char *proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd);@@ -153,7 +157,7 @@ static void *mmap_populate(void *ptr, size_t size,int
fd, bool shared, * If mmap failed withMAP_SHARED_VALIDATE|MAP_SYNC, we will try * again without these flags to handle backwards compatibility. */util/mmap-alloc.c +1
-1- new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags, fd,0 ); + new_ptr = mmap(ptr, size, PROT_READ | PROT_WRITE, flags, fd,
fd_offset); }return new_ptr;}
util/mmap-alloc.c +0
-0@@ -178,13 +182,16 @@ void *qemu_ram_mmap(int fd,size_t offset, total;void *ptr, *guardptr;Quoted 7 lines
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 187
Quoted 3 lines
util/mmap-alloc.c +3
-0+/* we can only map whole pages */ + size =QEMU_ALIGN_UP (size, pagesize); +Caller already rounds up size to
block->page_size.Why this
QEMU_ALIGN_UPis necessary?Thanks for having a look
I guess you read the patch description, right? :)
Quoted 7 lines
Quoted 4 lines
"(existing callers seem to guarantee this, but that's
not obvious)"Do you prefer a g_assert(IS_ALIGNED()) instead?
I guess you mean
QEMU_IS_ALIGNED(). But yes, I think we could just check alignments here, so callers do the alignments first.
Yeh, you got the idea :)
I'll convert these to asserts for now. Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 08/13] util/mmap-alloc: Prepare for resizable mmaps
In reply to Murilo Opsfelder AraújoQuoted 105 lines · Murilo Opsfelder Araújo
Hello, David.
Quoted 85 lines · On Monday, February 3, 2020 3:31:20 PM -03 David Hildenbrand wrote:
When shrinking a mmap we want to re-reserve the already populated area. When growing a memory region, we want to populate starting with a given
fd_offset. Prepare by allowing to pass these parameters.Also, let's make sure we always process full pages, to avoid unmapping/remapping pages that are already in use when growing/shrinking. (existing callers seem to guarantee this, but that's not obvious)
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/mmap-alloc.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)util/mmap-alloc.c +11
-7@@ -83,12 +83,12 @@ size_t qemu_mempath_getpagesize(const char *mem_path) }/* -* Reserve a new memory region of the requested size to be used for mapping -* from the given fd (if any). +* Reserve a new memory region of the requested size or re-reserve parts +* of an existing region to be used for mapping from the given fd (if any). */ -static void *mmap_reserve (size_t size,int fd) +static void *mmap_reserve (void *ptr,size_t size,int fd) { -int flags =MAP_PRIVATE ; +int flags =MAP_PRIVATE | (ptr ?MAP_FIXED :0 );#if defined(__powerpc64__) && defined(__linux__)/* @@ -111,19 +111,23 @@ static void *mmap_reserve(size_t size, int fd) flags |=MAP_ANONYMOUS ;#endif -return mmap (0 , size,PROT_NONE , flags, fd,0 ); +return mmap (ptr, size,PROT_NONE , flags, fd,0 ); }/* * Populate memory in a reserved region from the given fd (if any). */ -static void *mmap_populate (void *ptr,size_t size,int fd,bool shared, -bool is_pmem) +static void *mmap_populate (void *ptr,size_t size,int fd,size_t fd_offset, + bool shared, bool is_pmem) {int map_sync_flags =0 ;int flags =MAP_FIXED ;void *new_ptr; +if (fd ==-1 ) { + fd_offset =0 ; + } + flags |= fd ==-1 ?MAP_ANONYMOUS :0 ; flags |= shared ?MAP_SHARED :MAP_PRIVATE ;if (shared && is_pmem) {@@ -131,7 +135,7 @@ static void *mmap_populate(void *ptr, size_t size, int fd, bool shared, } new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags | map_sync_flags, - fd,0 ); + fd, fd_offset);if (new_ptr ==MAP_FAILED && map_sync_flags) {if (errno ==ENOTSUP ) {char *proc_link =g_strdup_printf ("/proc/self/fd/%d" , fd);@@ -153,7 +157,7 @@ static void *mmap_populate(void *ptr, size_t size, intfd, bool shared, * If mmap failed with
MAP_SHARED_VALIDATE|MAP_SYNC, we will try * again without these flags to handle backwards compatibility. */util/mmap-alloc.c +1
-1- new_ptr =mmap (ptr, size,PROT_READ |PROT_WRITE , flags, fd,0 ); + new_ptr = mmap(ptr, size, PROT_READ | PROT_WRITE, flags, fd,
fd_offset); }return new_ptr;}
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 187
Quoted 6 lines
util/mmap-alloc.c +3
-0@@ -178,13 +182,16 @@ void *qemu_ram_mmap(int fd,size_t offset, total;void *ptr, *guardptr; +/* we can only map whole pages */ + size =QEMU_ALIGN_UP (size, pagesize); +Caller already rounds up size to
block->page_size.Why this
QEMU_ALIGN_UPis necessary?Quoted 5 lines
util/mmap-alloc.c +0
-0/* * Note: this always allocates at least one extra page of virtual address * space, even if size is already aligned.
*/ total = size + align;
Quoted 4 lines
If size was aligned above with pagesize boundary, why would this align be necessary?
Can the pagesize differ from memory region align?
Sorry, skipped this comment.
Yes, e.g., we want to align ram blocks for KVM to hugepage size, to allow for transparent huge pages. So the comment still holds.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 08/13] util/mmap-alloc: Prepare for resizable mmaps
In reply to David HildenbrandQuoted 15 lines · David Hildenbrand
When shrinking a mmap we want to re-reserve the already populated area. When growing a memory region, we want to populate starting with a given
fd_offset. Prepare by allowing to pass these parameters.Also, let's make sure we always process full pages, to avoid unmapping/remapping pages that are already in use when growing/shrinking. (existing callers seem to guarantee this, but that's not obvious)
Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Quoted 3 lines
--- util/mmap-alloc.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-)
r~
PATCH 9/13 util/mmap-alloc: Implement resizable mmaps
In reply to David HildenbrandImplement resizable mmaps. For now, the actual resizing is not wired up. Introduce qemu_ram_mmap_resizable() and qemu_ram_mmap_resize(). Make qemu_ram_mmap() a wrapper of qemu_ram_mmap_resizable().
--- include/qemu/mmap-alloc.h | 21 ++++++++++++------- util/mmap-alloc.c | 44 ++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 20 deletions(-)
include/qemu/mmap-alloc.h +13 -8
@@ -7,11 +7,13 @@ size_t qemu_fd_getpagesize(int fd);size_t qemu_mempath_getpagesize (const char *mem_path);/** -* qemu_ram_mmap: mmap the specified file or device. +* qemu_ram_mmap_resizable: reserve a memory region of @max_size to mmap the +* specified file or device and mmap @size of it. * * Parameters: * @fd: the file or the device to mmap * @size: the number of bytes to be mmaped +* @max_size: the number of bytes to be reserved * @align: if not zero, specify the alignment of the starting mapping address; * otherwise, the alignment in use will be determined by QEMU. * @shared: map has RAM_SHARED flag.
@@ -21,12 +23,15 @@ size_t qemu_mempath_getpagesize(const char *mem_path);* On success, return a pointer to the mapped area. * On failure, return MAP_FAILED. */ -void *qemu_ram_mmap (int fd, -size_t size, -size_t align, -bool shared, -bool is_pmem); - -void qemu_ram_munmap (int fd,void *ptr,size_t size); +void *qemu_ram_mmap_resizable (int fd,size_t size,size_t max_size, +size_t align,bool shared,bool is_pmem); +void *qemu_ram_mmap_resize (void *ptr,int fd,size_t old_size,size_t new_size, +bool shared,bool is_pmem); +static inline void *qemu_ram_mmap (int fd,size_t size,size_t align, +bool shared,bool is_pmem) +{ +return qemu_ram_mmap_resizable (fd, size, size, align, shared, is_pmem); +} +void qemu_ram_munmap (int fd,void *ptr,size_t max_size);#endif
util/mmap-alloc.c +32 -12
77 lines not on this page · raw message
Signature · 1 line
2.24.1
Re: [PATCH v1 09/13] util/mmap-alloc: Implement resizable mmaps
Quoted 11 lines · David Hildenbrand
util/mmap-alloc.c +6
-1+void *qemu_ram_mmap_resize (void *ptr,int fd,size_t old_size,size_t new_size, +bool shared,bool is_pmem) {const size_t pagesize =mmap_pagesize (fd);/* we can only map whole pages */ - size =QEMU_ALIGN_UP (size, pagesize); + old_size =QEMU_ALIGN_UP (old_size, pagesize); + new_size =QEMU_ALIGN_UP (new_size, pagesize); + +/* we support actually resizable memory regions only on Linux */
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 245
Quoted 10 lines
util/mmap-alloc.c +10
-0+if (old_size < new_size) { +/* populate the missing piece into the reserved area */ + ptr =mmap_populate (ptr + old_size, new_size - old_size, fd, old_size, + shared, is_pmem); + }else if (old_size > new_size) { +/* discard this piece, keeping the area reserved (should never fail) */ + ptr =mmap_reserve (ptr + new_size, old_size - new_size, fd); + } +return ptr; +}
What does the return value indicate?
Is it just for != MAP_FAILED?
Would we be better off with an assert? There's the comment re mmap_reserve, but I can't see why mmap_populate should fail either.
Assuming an assert isn't viable, are we better off with a boolean return? With an Error **ptr?
r~
Re: [PATCH v1 09/13] util/mmap-alloc: Implement resizable mmaps
Quoted 17 lines · Richard Henderson
Quoted 16 lines · David Hildenbrand
util/mmap-alloc.c +11
-1+void *qemu_ram_mmap_resize (void *ptr,int fd,size_t old_size,size_t new_size, +bool shared,bool is_pmem) {const size_t pagesize =mmap_pagesize (fd);/* we can only map whole pages */ - size =QEMU_ALIGN_UP (size, pagesize); + old_size =QEMU_ALIGN_UP (old_size, pagesize); + new_size =QEMU_ALIGN_UP (new_size, pagesize); + +/* we support actually resizable memory regions only on Linux */ +if (old_size < new_size) { +/* populate the missing piece into the reserved area */ + ptr =mmap_populate (ptr + old_size, new_size - old_size, fd, old_size, + shared, is_pmem); + }else if (old_size > new_size) {
Quoted 8 lines
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 245
Quoted 5 lines
util/mmap-alloc.c +5
-0+/* discard this piece, keeping the area reserved (should never fail) */ + ptr =mmap_reserve (ptr + new_size, old_size - new_size, fd); + } +return ptr; +}What does the return value indicate?
Is it just for !=MAP_FAILED?
It indicates if resizing succeeded. In a previous version I returned an int via
ptr == MAP_FAILED ? -errno : 0;
Populating will usually only fail because we're out of memory.
Populating and reserving *might* fail if we are out of VMAs in the kernel. VMA merging will make sure that the number of VMAs will not explode (usually 2-3 VMAs for one resizable region: populated VMA + Reserved VMA + Guard page VMA). But once we would be close to the VMA limit, it could happen - but it's highly unlikely.
Quoted 2 lines
Assuming an assert isn't viable, are we better off with a boolean return? With an Error **ptr?
either that or an int. What do you prefer?
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 09/13] util/mmap-alloc: Implement resizable mmaps
Quoted 41 lines · David Hildenbrand
Quoted 17 lines · Richard Henderson
Quoted 16 lines · David Hildenbrand
util/mmap-alloc.c +11
-1+void *qemu_ram_mmap_resize (void *ptr,int fd,size_t old_size,size_t new_size, +bool shared,bool is_pmem) {const size_t pagesize =mmap_pagesize (fd);/* we can only map whole pages */ - size =QEMU_ALIGN_UP (size, pagesize); + old_size =QEMU_ALIGN_UP (old_size, pagesize); + new_size =QEMU_ALIGN_UP (new_size, pagesize); + +/* we support actually resizable memory regions only on Linux */ +if (old_size < new_size) { +/* populate the missing piece into the reserved area */ + ptr =mmap_populate (ptr + old_size, new_size - old_size, fd, old_size, + shared, is_pmem); + }else if (old_size > new_size) {Quoted 8 lines
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 245
Quoted 5 lines
util/mmap-alloc.c +5
-0+/* discard this piece, keeping the area reserved (should never fail) */ + ptr =mmap_reserve (ptr + new_size, old_size - new_size, fd); + } +return ptr; +}What does the return value indicate?
Is it just for !=MAP_FAILED?It indicates if resizing succeeded. In a previous version I returned an int via
ptr ==
MAP_FAILED? -errno : 0;Populating will usually only fail because we're out of memory.
Populating and reserving *might* fail if we are out of VMAs in the kernel. VMA merging will make sure that the number of VMAs will not explode (usually 2-3 VMAs for one resizable region: populated VMA + Reserved VMA + Guard page VMA). But once we would be close to the VMA limit, it could happen - but it's highly unlikely.
Quoted 4 lines
Quoted 2 lines
Assuming an assert isn't viable, are we better off with a boolean return? With an Error **ptr?
either that or an int. What do you prefer?
I'll go with a bool for now. "return ptr != MAP_FAILED;"
The actual error will usually be -ENOMEM, so there is no real benefit in returning it.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 09/13] util/mmap-alloc: Implement resizable mmaps
In reply to David HildenbrandHello, David.
Quoted 105 lines · On Monday, February 3, 2020 3:31:21 PM -03 David Hildenbrand wrote:
Implement resizable mmaps. For now, the actual resizing is not wired up. Introduce
qemu_ram_mmap_resizable()andqemu_ram_mmap_resize(). Makeqemu_ram_mmap()a wrapper ofqemu_ram_mmap_resizable().Cc "Michael S. Tsirkin" <[email protected]>Cc Greg Kurz <[email protected]>Cc Murilo Opsfelder Araujo <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- include/qemu/mmap-alloc.h | 21 ++++++++++++------- util/mmap-alloc.c | 44 ++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 20 deletions(-)include/qemu/mmap-alloc.h +1
-1@@ -7,11 +7,13 @@ size_t qemu_fd_getpagesize(int fd);size_t qemu_mempath_getpagesize (const char *mem_path);/** -* qemu_ram_mmap: mmap the specified file or device. +* qemu_ram_mmap_resizable: reserve a memory region of @max_size to mmap the + * specified file or device and mmap @size of
it. *
* Parameters:* @fd: the file or the device to mmap * @size: the number of bytes to be mmaped + * @max_size: the number of bytes to be reserved * @align: if not zero, specify the alignment of the starting mappingaddress; * otherwise, the alignment in use will be determined by
QEMU. * @shared: map hasRAM_SHAREDflag.include/qemu/mmap-alloc.h +9
-7@@ -21,12 +23,15 @@ size_t qemu_mempath_getpagesize(const char *mem_path);* On success, return a pointer to the mapped area. * On failure, return MAP_FAILED. */ -void *qemu_ram_mmap (int fd, -size_t size, -size_t align, -bool shared, -bool is_pmem); - -void qemu_ram_munmap (int fd,void *ptr,size_t size); +void *qemu_ram_mmap_resizable (int fd,size_t size,size_t max_size, +size_t align,bool shared,bool is_pmem); +void *qemu_ram_mmap_resize (void *ptr,int fd,size_t old_size,size_t new_size, + bool shared, bool is_pmem); +static inline void *qemu_ram_mmap (int fd,size_t size,size_t align, +bool shared,bool is_pmem) +{ +return qemu_ram_mmap_resizable (fd, size, size, align, shared, is_pmem); +} +void qemu_ram_munmap (int fd,void *ptr,size_t max_size);#endif util/mmap-alloc.c +3
-5@@ -172,11 +172,8 @@ static inline size_t mmap_pagesize(int fd)#endif } -void *qemu_ram_mmap (int fd, -size_t size, -size_t align, -bool shared, -bool is_pmem) +void *qemu_ram_mmap_resizable (int fd,size_t size,size_t max_size, +size_t align,bool shared,bool is_pmem) {const size_t pagesize =mmap_pagesize (fd);size_t offset, total;@@ -184,12 +181,14 @@ void *qemu_ram_mmap(int fd,/* we can only map whole pages */ size =QEMU_ALIGN_UP (size, pagesize); + max_size =QEMU_ALIGN_UP (max_size, pagesize);/* * Note: this always allocates at least one extra page of virtual address - * space, even if size is already aligned.
+ * space, even if the size is already aligned. We will reserve an area
of + * at leastmax_size, but only populate the requested part of it.
*/util/mmap-alloc.c +3
-3- total = size + align; + total = max_size + align; guardptr =mmap_reserve (0 , total, fd);if (guardptr ==MAP_FAILED ) {@@ -217,22 +216,43 @@ void *qemu_ram_mmap(int fd,* a guard page guarding against potential buffer overflows. */ total -= offset; -if (total > size + pagesize) { -munmap (ptr + size + pagesize, total - size - pagesize); +if (total > max_size + pagesize) { +munmap (ptr + max_size + pagesize, total - max_size - pagesize); }return ptr; }
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 233
Quoted 10 lines
util/mmap-alloc.c +3
-2-void qemu_ram_munmap(int fd, void *ptr, size_t size) +void *qemu_ram_mmap_resize (void *ptr,int fd,size_t old_size,size_t new_size, + bool shared, bool is_pmem) {const size_t pagesize =mmap_pagesize (fd);/* we can only map whole pages */ - size =QEMU_ALIGN_UP (size, pagesize); + old_size =QEMU_ALIGN_UP (old_size, pagesize); + new_size =QEMU_ALIGN_UP (new_size, pagesize);
Shouldn't we just assert old_size and new_size are aligned with pagesize?
util/mmap-alloc.c void *qemu_ram_mmap(int fd,
Quoted 10 lines
util/mmap-alloc.c +7
-0+ +/* we support actually resizable memory regions only on Linux */ +if (old_size < new_size) { +/* populate the missing piece into the reserved area */ + ptr =mmap_populate (ptr + old_size, new_size - old_size, fd, old_size, + shared, is_pmem); + }else if (old_size > new_size) { +/* discard this piece, keeping the area reserved (should never fail) */ + ptr =mmap_reserve (ptr + new_size, old_size - new_size, fd); + }
I find the behaviour of this function somewhat confusing. Perhaps I'm missing something and need your help to clarify. Please bear with me.
For the case where we want to grow in size, it returns a populated area (PROT_READ | PROT_WRITE flags).
And for the case where we want to shrink in size, it returns a reserved area (PROT_NONE flag), requiring the caller to call mmap_populate() again to be able to use that memory.
I believe the behaviour should be consistent regardless if we want to grow or shrink in size. Either return a reserved or an already populated area. Not both.
Would "old_size == new_size" situation be possible? In this case, ptr would be returned without changing protection flags of the mapping.
Shouldn't we also assert that new_size <= max_size?
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 252
Quoted 9 lines
util/mmap-alloc.c +9
-0+return ptr; +} + +void qemu_ram_munmap (int fd,void *ptr,size_t max_size) +{ +const size_t pagesize =mmap_pagesize (fd); + +/* we can only map whole pages */ + max_size =QEMU_ALIGN_UP (max_size, pagesize);
Shouldn't we just assert this and leave the alignment to the caller?
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 258
Quoted 7 lines
util/mmap-alloc.c +1
-1if (ptr) {/* Unmap both the RAM block and the guard page */ -munmap (ptr, size + pagesize); +munmap (ptr, max_size + pagesize); } }
--
Murilo
Re: [PATCH v1 09/13] util/mmap-alloc: Implement resizable mmaps
Quoted 8 lines
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 233
Quoted 4 lines
/* we can only map whole pages */util/mmap-alloc.c +2
-1- size =QEMU_ALIGN_UP (size, pagesize); + old_size =QEMU_ALIGN_UP (old_size, pagesize); + new_size =QEMU_ALIGN_UP (new_size, pagesize);Shouldn't we just assert
old_sizeandnew_sizeare aligned with pagesize?
Already reworked :)
Quoted 17 lines
Quoted 10 lines
util/mmap-alloc.c +5
-0+ +/* we support actually resizable memory regions only on Linux */ +if (old_size < new_size) { +/* populate the missing piece into the reserved area */ + ptr = mmap_populate(ptr + old_size, new_size - old_size, fd,
old_size, + shared,is_pmem);(quoted diff) +2
-0+ } else if (old_size > new_size) { + /* discard this piece, keeping the area reserved (should neverfail) */ + ptr = mmap_reserve(ptr +
new_size,old_size-new_size,
fd); + }I find the behaviour of this function somewhat confusing. Perhaps I'm missing something and need your help to clarify. Please bear with me.
For the case where we want to grow in size, it returns a populated area (
PROT_READ|PROT_WRITEflags).
Quoted 7 lines
And for the case where we want to shrink in size, it returns a reserved area (
PROT_NONEflag), requiring the caller to callmmap_populate()again to be able to use that memory.I believe the behaviour should be consistent regardless if we want to grow or shrink in size. Either return a reserved or an already populated area. Not both.
The return value is only used to differentiate between success (!= MAP_FAILED) and failure (== MAP_FAILED), nothing else.
For now, I switched to returning a bool instead (resized vs. not resized), that avoids this inconsistency. See my reply to Richard's comment.
Quoted 3 lines
Would "
old_size==new_size" situation be possible? In this case, ptr would be returned without changing protection flags of the mapping.
It could, although in this patch set, it never would :) The result would be a NOP, which is the right thing to do.
Quoted 2 lines
Shouldn't we also assert that
new_size<=max_size?
Then we would have to pass max_size, just for asserting. I'll leave that to the callers for now.
Quoted 12 lines
util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 252
Quoted 10 lines
util/mmap-alloc.c +9
-0+return ptr; +} + +void qemu_ram_munmap (int fd,void *ptr,size_t max_size) +{ +const size_t pagesize =mmap_pagesize (fd); + +/* we can only map whole pages */ + max_size =QEMU_ALIGN_UP (max_size, pagesize);Shouldn't we just assert this and leave the alignment to the caller?
Already reworked.
Thanks!
Signature · 3 lines
Thanks, David / dhildenb
PATCH 10/13 numa: Introduce ram_block_notify_resized() and ram_block_notifiers_support_resize()
In reply to David HildenbrandWe want to actually resize ram blocks (make everything between used_length and max_length inaccessible) - however, not all ram block notifiers will support that.
So introduce a way to detect if any registered notifier does not support it and add a way to notify all notifiers that support it.
Using ram_block_notifiers_support_resize(), we can keep the existing handling for these special cases until they implement support (e.g., SEV, HAX) to resize.
--- hw/core/numa.c | 21 +++++++++++++++++++++ include/exec/ramlist.h | 4 ++++ util/trace-events | 1 + 3 files changed, 26 insertions(+)
hw/core/numa.c +21 -0
@@ -940,3 +940,24 @@ void ram_block_notify_remove(void *host, size_t size) notifier->ram_block_removed (notifier, host, size); } } + +void ram_block_notify_resized (void *host,size_t oldsize,size_t newsize) +{ +RAMBlockNotifier *notifier; + + QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { + notifier->ram_block_resized (notifier, host, oldsize, newsize); + } +} + +bool ram_block_notifiers_support_resize (void ) +{ +RAMBlockNotifier *notifier; + + QLIST_FOREACH(notifier, &ram_list.ramblock_notifiers, next) { +if (!notifier->ram_block_resized) { +return false; + } + } +return true; +}
include/exec/ramlist.h +4 -0
@@ -67,6 +67,8 @@ void qemu_mutex_unlock_ramlist(void);struct RAMBlockNotifier {void (*ram_block_added)(RAMBlockNotifier *n,void *host,size_t size);void (*ram_block_removed)(RAMBlockNotifier *n,void *host,size_t size); +void (*ram_block_resized)(RAMBlockNotifier *n,void *host,size_t oldsize, +size_t newsize);QLIST_ENTRY (RAMBlockNotifier) next; };
@@ -74,6 +76,8 @@ void ram_block_notifier_add(RAMBlockNotifier *n);void ram_block_notifier_remove (RAMBlockNotifier *n);void ram_block_notify_add (void *host,size_t size);void ram_block_notify_remove (void *host,size_t size); +void ram_block_notify_resized (void *host,size_t oldsize,size_t newsize); +bool ram_block_notifiers_support_resize (void );void ram_block_dump (Monitor *mon);
util/trace-events +1 -0
@@ -76,6 +76,7 @@ qemu_mutex_unlock(void *mutex, const char *file, const int line) "released mutex qemu_vfio_dma_reset_temporary(void *s) "s %p" qemu_vfio_ram_block_added(void *s, void *p, size_t size) "s %p host %p size 0x%zx" qemu_vfio_ram_block_removed(void *s, void *p, size_t size) "s %p host %p size 0x%zx" +qemu_vfio_ram_block_resized(void *s, void *p, size_t oldsize, size_t newsizze) "s %p host %p oldsize 0x%zx newsize 0x%zx" qemu_vfio_find_mapping(void *s, void *p) "s %p host %p" qemu_vfio_new_mapping(void *s, void *host, size_t size, int index, uint64_t iova) "s %p host %p size %zu index %d iova 0x%"PRIx64 qemu_vfio_do_mapping(void *s, void *host, size_t size, uint64_t iova) "s %p host %p size %zu iova 0x%"PRIx64
Signature · 1 line
2.24.1
PATCH 11/13 util: vfio-helpers: Implement ram_block_resized()
In reply to David HildenbrandLet's implement ram_block_resized().
Note: Resizing is currently only allowed during reboot or when migration starts.
--- util/vfio-helpers.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
util/vfio-helpers.c +13 -0
@@ -395,11 +395,24 @@ static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n, } } +static void qemu_vfio_ram_block_resized (RAMBlockNotifier *n,void *host, +size_t oldsize,size_t newsize) +{ +QEMUVFIOState *s =container_of (n, QEMUVFIOState, ram_notifier); +if (host) { +trace_qemu_vfio_ram_block_resized (s, host, oldsize, newsize); +/* Note: Not atomic - we need a new ioctl for that. */ +qemu_vfio_ram_block_removed (n, host, oldsize); +qemu_vfio_ram_block_added (n, host, newsize); + } +} +static void qemu_vfio_open_common (QEMUVFIOState *s) {qemu_mutex_init (&s->lock); s->ram_notifier.ram_block_added = qemu_vfio_ram_block_added; s->ram_notifier.ram_block_removed = qemu_vfio_ram_block_removed; + s->ram_notifier.ram_block_resized = qemu_vfio_ram_block_resized; s->low_water_mark =QEMU_VFIO_IOVA_MIN ; s->high_water_mark =QEMU_VFIO_IOVA_MAX ;ram_block_notifier_add (&s->ram_notifier);
Signature · 1 line
2.24.1
Re: [PATCH v1 11/13] util: vfio-helpers: Implement ram_block_resized()
Quoted 34 lines · David Hildenbrand
Let's implement
ram_block_resized().Note: Resizing is currently only allowed during reboot or when migration starts.
Cc "Dr. David Alan Gilbert" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Markus Armbruster <[email protected]>Cc Alex Williamson <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- util/vfio-helpers.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)util/vfio-helpers.c +11
-0@@ -395,11 +395,24 @@ static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n, } } +static void qemu_vfio_ram_block_resized (RAMBlockNotifier *n,void *host, +size_t oldsize,size_t newsize) +{ +QEMUVFIOState *s =container_of (n, QEMUVFIOState, ram_notifier); +if (host) { +trace_qemu_vfio_ram_block_resized (s, host, oldsize, newsize); +/* Note: Not atomic - we need a new ioctl for that. */ +qemu_vfio_ram_block_removed (n, host, oldsize); +qemu_vfio_ram_block_added (n, host, newsize); + } +}
util/vfio-helpers.c static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n, · line 418
Quoted 10 lines
util/vfio-helpers.c +2
-0+static void qemu_vfio_open_common (QEMUVFIOState *s) {qemu_mutex_init (&s->lock); s->ram_notifier.ram_block_added = qemu_vfio_ram_block_added; s->ram_notifier.ram_block_removed = qemu_vfio_ram_block_removed; + s->ram_notifier.ram_block_resized = qemu_vfio_ram_block_resized; s->low_water_mark =QEMU_VFIO_IOVA_MIN ; s->high_water_mark =QEMU_VFIO_IOVA_MAX ;ram_block_notifier_add (&s->ram_notifier);
I'll most probably change the handling, to reserve the IOVA for max_size of the ram block, but only map the usable size. Addresses in the IOVA won't be reused.
If I am not wrong, hotplugging+unplugging DIMMs a couple of times can easily eat up the whole IOVA. Same would be true on every resize.
At this point, I really detest ram block notifiers. :)
Signature · 3 lines
Thanks, David / dhildenb
PATCH 12/13 util: oslib: Resizable anonymous allocations under POSIX
In reply to David HildenbrandIntroduce qemu_anon_ram_alloc_resizable() and qemu_anon_ram_resize(). Implement them under POSIX and make them return NULL under WIN32.
Under POSIX, we make use of resizable mmaps. An implementation under WIN32 is theoretically possible AFAIK and can be added later.
In qemu_anon_ram_free(), rename the size parameter to max_size, to make it clearer that we have to use the maximum size when freeing resizable anonymous allocations.
--- include/qemu/osdep.h | 6 +++++- util/oslib-posix.c | 37 ++++++++++++++++++++++++++++++++++--- util/oslib-win32.c | 14 ++++++++++++++ util/trace-events | 4 +++- 4 files changed, 56 insertions(+), 5 deletions(-)
include/qemu/osdep.h +5 -1
@@ -311,8 +311,12 @@ int qemu_daemon(int nochdir, int noclose);void *qemu_try_memalign (size_t alignment,size_t size);void *qemu_memalign (size_t alignment,size_t size);void *qemu_anon_ram_alloc (size_t size,uint64_t *align,bool shared); +void *qemu_anon_ram_alloc_resizable (size_t size,size_t max_size, +uint64_t *align,bool shared); +void *qemu_anon_ram_resize (void *ptr,size_t old_size,size_t new_size, +bool shared);void qemu_vfree (void *ptr); -void qemu_anon_ram_free (void *ptr,size_t size); +void qemu_anon_ram_free (void *ptr,size_t max_size);#define QEMU_MADV_INVALID -1
util/oslib-posix.c +34 -3
@@ -219,16 +219,47 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared)return ptr; } +void *qemu_anon_ram_alloc_resizable (size_t size,size_t max_size, +uint64_t *alignment,bool shared) +{ +size_t align =QEMU_VMALLOC_ALIGN ; +void *ptr =qemu_ram_mmap_resizable (-1 , size, max_size, align, shared, + false); + +if (ptr ==MAP_FAILED ) { +return NULL ; + } + +if (alignment) { + *alignment = align; + } + +trace_qemu_anon_ram_alloc_resizable (size, max_size, ptr); +return ptr; +} + +void *qemu_anon_ram_resize (void *ptr,size_t old_size,size_t new_size, +bool shared) +{ + ptr =qemu_ram_mmap_resize (ptr,-1 , old_size, new_size, shared, false); +if (ptr ==MAP_FAILED ) { +return NULL ; + } + +trace_qemu_anon_ram_resize (old_size, new_size, ptr); +return ptr; +} +void qemu_vfree (void *ptr) {trace_qemu_vfree (ptr);free (ptr); } -void qemu_anon_ram_free (void *ptr,size_t size) +void qemu_anon_ram_free (void *ptr,size_t max_size) { -trace_qemu_anon_ram_free (ptr, size); -qemu_ram_munmap (-1 , ptr, size); +trace_qemu_anon_ram_free (ptr, max_size); +qemu_ram_munmap (-1 , ptr, max_size); }void qemu_set_block (int fd)
util/oslib-win32.c +14 -0
@@ -90,6 +90,20 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared)return ptr; } +void *qemu_anon_ram_alloc_resizable (size_t size,size_t max_size, +uint64_t *align,bool shared) +{ +/* resizable ram not implemented yet */ +return NULL ; +} + +void *qemu_anon_ram_resize (void *ptr,size_t old_size,size_t new_size, +bool shared) +{ +/* resizable ram not implemented yet */ +return NULL ; +} +void qemu_vfree (void *ptr) {trace_qemu_vfree (ptr);
util/trace-events +3 -1
@@ -46,8 +46,10 @@ qemu_co_mutex_unlock_return(void *mutex, void *self) "mutex %p self %p" # oslib-posix.c qemu_memalign(size_t alignment, size_t size, void *ptr) "alignment %zu size %zu ptr %p" qemu_anon_ram_alloc(size_t size, void *ptr) "size %zu ptr %p" +qemu_anon_ram_alloc_resizable(size_t size, size_t max_size, void *ptr) "size %zu max_size %zu ptr %p" +qemu_anon_ram_resize(size_t old_size, size_t new_size, void *ptr) "old_size %zu new_size %zu ptr %p" qemu_vfree(void *ptr) "ptr %p" -qemu_anon_ram_free(void *ptr, size_t size) "ptr %p size %zu" +qemu_anon_ram_free(void *ptr, size_t max_size) "ptr %p max_size %zu" # hbitmap.c hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned long cur) "hb %p hbi %p pos %"PRId64" cur 0x%lx"
Signature · 1 line
2.24.1
PATCH 13/13 exec: Ram blocks with resizable anonymous allocations under POSIX
In reply to David HildenbrandWe can now make use of resizable anonymous allocations to implement actually resizable ram blocks. Resizable anonymous allocations are not implemented under WIN32 yet and are not available when using alternative allocators. Fall back to the existing handling.
We also have to fallback to the existing handling in case any ram block notifier does not support resizing (esp., AMD SEV, HAX) yet. Remember in RAM_RESIZEABLE_ALLOC if we are using resizable anonymous allocations.
As the mmap()-hackery will invalidate some madvise settings, we have to re-apply them after resizing. After resizing, notify the ram block notifiers.
The benefit of actually resizable ram blocks is that e.g., under Linux, only the actual size will be reserved (even if "/proc/sys/vm/overcommit_memory" is set to "never"). Additional memory will be reserved when trying to resize, which allows to have ram blocks that start small but can theoretically grow very large.
--- exec.c | 68 +++++++++++++++++++++++++++++++++++---- hw/core/numa.c | 10 ++++-- include/exec/cpu-common.h | 2 ++ include/exec/memory.h | 8 +++++ 4 files changed, 79 insertions(+), 9 deletions(-)
exec.c +62 -6
@@ -2053,6 +2053,16 @@ void qemu_ram_unset_migratable(RAMBlock *rb) rb->flags &= ~RAM_MIGRATABLE ; } +bool qemu_ram_is_resizable (RAMBlock *rb) +{ +return rb->flags &RAM_RESIZEABLE ; +} + +bool qemu_ram_is_resizable_alloc (RAMBlock *rb) +{ +return rb->flags &RAM_RESIZEABLE_ALLOC ; +} +/* Called with iothread lock held. */ void qemu_ram_set_idstr (RAMBlock *new_block,const char *name,DeviceState *dev) {
@@ -2139,6 +2149,8 @@ static void qemu_ram_apply_settings(void *host, size_t length)*/ int qemu_ram_resize (RAMBlock *block,ram_addr_t newsize,Error **errp) { +const uint64_t oldsize = block->used_length; +assert (block); newsize =HOST_PAGE_ALIGN (newsize);
@@ -2147,7 +2159,7 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp)return 0 ; } -if (!(block->flags &RAM_RESIZEABLE )) { +if (!qemu_ram_is_resizable (block)) { error_setg_errno(errp, EINVAL, "Length mismatch: %s: 0x" RAM_ADDR_FMT " in != 0x" RAM_ADDR_FMT, block->idstr,
@@ -2163,10 +2175,26 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp)return -EINVAL ; } +if (qemu_ram_is_resizable_alloc (block)) { +g_assert (ram_block_notifiers_support_resize ()); +if (qemu_anon_ram_resize (block->host, block->used_length, + newsize, block->flags &RAM_SHARED ) ==NULL ) { +error_setg_errno (errp, -ENOMEM , +"Could not allocate enough memory." ); +return -ENOMEM ; + } + } +cpu_physical_memory_clear_dirty_range (block->offset, block->used_length); block->used_length = newsize;cpu_physical_memory_set_dirty_range (block->offset, block->used_length,DIRTY_CLIENTS_ALL ); +if (block->host &&qemu_ram_is_resizable_alloc (block)) { +/* re-apply settings that might have been overriden by the resize */ +qemu_ram_apply_settings (block->host, block->max_length); +ram_block_notify_resized (block->host, oldsize, block->used_length); + } +memory_region_set_size (block->mr, newsize);if (block->resized) { block->resized (block->idstr, newsize, block->host);
@@ -2249,6 +2277,28 @@ static void dirty_memory_extend(ram_addr_t old_ram_size, } } +static void ram_block_alloc_ram (RAMBlock *rb) +{ +const bool shared =qemu_ram_is_shared (rb); + +/* +* If we can, try to allocate actually resizable ram. Will also fail +* if qemu_anon_ram_alloc_resizable() is not implemented. +*/ +if (phys_mem_alloc == qemu_anon_ram_alloc && +qemu_ram_is_resizable (rb) && +ram_block_notifiers_support_resize ()) { + rb->host =qemu_anon_ram_alloc_resizable (rb->used_length, + rb->max_length, &rb->mr->align, + shared); +if (rb->host) { + rb->flags |=RAM_RESIZEABLE_ALLOC ; +return ; + } + } + rb->host =phys_mem_alloc (rb->max_length, &rb->mr->align, shared); +} +static void ram_block_add (RAMBlock *new_block,Error **errp) {RAMBlock *block;
@@ -2271,9 +2321,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)return ; } }else { - new_block->host =phys_mem_alloc (new_block->max_length, - &new_block->mr->align, -qemu_ram_is_shared (new_block)); +ram_block_alloc_ram (new_block);if (!new_block->host) { error_setg_errno(errp, errno, "cannot set up guest memory '%s'",
@@ -2319,7 +2367,11 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)if (new_block->host) {qemu_ram_apply_settings (new_block->host, new_block->max_length); -ram_block_notify_add (new_block->host, new_block->max_length); +if (qemu_ram_is_resizable_alloc (new_block)) { +ram_block_notify_add (new_block->host, new_block->used_length); + }else { +ram_block_notify_add (new_block->host, new_block->max_length); + } } }
@@ -2502,7 +2554,11 @@ void qemu_ram_free(RAMBlock *block) }if (block->host) { -ram_block_notify_remove (block->host, block->max_length); +if (qemu_ram_is_resizable_alloc (block)) { +ram_block_notify_remove (block->host, block->used_length); + }else { +ram_block_notify_remove (block->host, block->max_length); + } }qemu_mutex_lock_ramlist ();
hw/core/numa.c +7 -3
@@ -901,12 +901,16 @@ void query_numa_node_mem(NumaNodeMem node_mem[], MachineState *ms)static int ram_block_notify_add_single (RAMBlock *rb,void *opaque) { -ram_addr_t size =qemu_ram_get_max_length (rb);void *host =qemu_ram_get_host_addr (rb);RAMBlockNotifier *notifier = opaque; -if (host) { - notifier->ram_block_added (notifier, host, size); +if (!host) { +return 0 ; + } +if (qemu_ram_is_resizable_alloc (rb)) { + notifier->ram_block_added (notifier, host,qemu_ram_get_used_length (rb)); + }else { + notifier->ram_block_added (notifier, host,qemu_ram_get_max_length (rb)); }return 0 ; }
include/exec/cpu-common.h +2 -0
@@ -66,6 +66,8 @@ void qemu_ram_set_uf_zeroable(RAMBlock *rb);bool qemu_ram_is_migratable (RAMBlock *rb);void qemu_ram_set_migratable (RAMBlock *rb);void qemu_ram_unset_migratable (RAMBlock *rb); +bool qemu_ram_is_resizable (RAMBlock *rb); +bool qemu_ram_is_resizable_alloc (RAMBlock *rb);size_t qemu_ram_pagesize (RAMBlock *block);size_t qemu_ram_pagesize_largest (void );
include/exec/memory.h +8 -0
@@ -129,6 +129,14 @@ typedef struct IOMMUNotifier IOMMUNotifier;/* RAM is a persistent kind memory */ #define RAM_PMEM (1 << 5) +/* +* Implies RAM_RESIZEABLE. Memory beyond the used_length is inaccessible +* (esp. initially and after resizing). For such memory blocks, only the +* used_length is reserved in the OS - resizing might fail. Will only be +* used with host OS support and if all ram block notifiers support resizing. +*/ +#define RAM_RESIZEABLE_ALLOC (1 << 6) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end,
Signature · 1 line
2.24.1
Re: [PATCH v1 13/13] exec: Ram blocks with resizable anonymous allocations under POSIX
Quoted 76 lines · David Hildenbrand
We can now make use of resizable anonymous allocations to implement actually resizable ram blocks. Resizable anonymous allocations are not implemented under WIN32 yet and are not available when using alternative allocators. Fall back to the existing handling.
We also have to fallback to the existing handling in case any ram block notifier does not support resizing (esp., AMD SEV, HAX) yet. Remember in
RAM_RESIZEABLE_ALLOCif we are using resizable anonymous allocations.As the mmap()-hackery will invalidate some madvise settings, we have to re-apply them after resizing. After resizing, notify the ram block notifiers.
The benefit of actually resizable ram blocks is that e.g., under Linux, only the actual size will be reserved (even if "
/proc/sys/vm/overcommit_memory" is set to "never"). Additional memory will be reserved when trying to resize, which allows to have ram blocks that start small but can theoretically grow very large.Cc Richard Henderson <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Cc Eduardo Habkost <[email protected]>Cc Marcel Apfelbaum <[email protected]>Cc Stefan Weil <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- exec.c | 68 +++++++++++++++++++++++++++++++++++---- hw/core/numa.c | 10 ++++-- include/exec/cpu-common.h | 2 ++ include/exec/memory.h | 8 +++++ 4 files changed, 79 insertions(+), 9 deletions(-)exec.c +13
-1@@ -2053,6 +2053,16 @@ void qemu_ram_unset_migratable(RAMBlock *rb) rb->flags &= ~RAM_MIGRATABLE ; } +bool qemu_ram_is_resizable (RAMBlock *rb) +{ +return rb->flags &RAM_RESIZEABLE ; +} + +bool qemu_ram_is_resizable_alloc (RAMBlock *rb) +{ +return rb->flags &RAM_RESIZEABLE_ALLOC ; +} +/* Called with iothread lock held. */ void qemu_ram_set_idstr (RAMBlock *new_block,const char *name,DeviceState *dev) {@@ -2139,6 +2149,8 @@ static void qemu_ram_apply_settings(void *host, size_t length)*/ int qemu_ram_resize (RAMBlock *block,ram_addr_t newsize,Error **errp) { +const uint64_t oldsize = block->used_length; +assert (block); newsize =HOST_PAGE_ALIGN (newsize);@@ -2147,7 +2159,7 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp)return 0 ; } -if (!(block->flags &RAM_RESIZEABLE )) { +if (!qemu_ram_is_resizable (block)) { error_setg_errno(errp, EINVAL, "Length mismatch: %s: 0x" RAM_ADDR_FMT " in != 0x" RAM_ADDR_FMT, block->idstr,@@ -2163,10 +2175,26 @@ int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp)return -EINVAL ; }
exec.c int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp) · line 2187
Quoted 10 lines
exec.c +10
-0+if (qemu_ram_is_resizable_alloc (block)) { +g_assert (ram_block_notifiers_support_resize ()); +if (qemu_anon_ram_resize (block->host, block->used_length, + newsize, block->flags &RAM_SHARED ) ==NULL ) { +error_setg_errno (errp, -ENOMEM , +"Could not allocate enough memory." ); +return -ENOMEM ; + } + } +
I'll most probably rework to have separate paths when growing/shrinking, with a different sequence of steps. (e.g., perform actual shrinking as last step, so all mappings remain valid, and ignore errors (which are unlikely either way)).
Signature · 3 lines
Thanks, David / dhildenb
Quoted 17 lines · David Hildenbrand
We already allow resizable ram blocks for anonymous memory, however, they are not actually resized. All memory is
mmaped()R/W, including the memory exceeding theused_length, up to themax_length.When resizing, effectively only the boundary is moved. Implement actually resizable anonymous allocations and make use of them in resizable ram blocks when possible. Memory exceeding the
used_lengthwill be inaccessible. Especially ram block notifiers require care.Having actually resizable anonymous allocations (via mmap-hackery) allows to reserve a big region in virtual address space and grow the accessible/usable part on demand. Even if "
/proc/sys/vm/overcommit_memory" is set to "never" under Linux, huge reservations will succeed. If there is not enough memory when resizing (to populate parts of the reserved region), trying to resize will fail. Only the actually used size is reserved in the OS.
Quoted 5 lines
E.g., virtio-mem [1] wants to reserve big resizable memory regions and grow the usable part on demand. I think this change is worth sending out individually. Accompanied by a bunch of minor fixes and cleanups.
How does this inteact with all the prealloc/mlock things designed for realtime?
Quoted 36 lines
David Hildenbrand (13):
util: vfio-helpers: Factor out and fix processing of existings ram
blocks
exec: Factor out setting ram settings (madvise ...) into
qemu_ram_apply_settings()
exec: Reuseqemu_ram_apply_settings()inqemu_ram_remap()
exec: Drop "shared" parameter fromram_block_add()util/mmap-alloc: Factor out calculation of pagesize tommap_pagesize()util/mmap-alloc: Factor out reserving of a memory region to
mmap_reserve()
util/mmap-alloc: Factor out populating of memory tommap_populate()util/mmap-alloc: Prepare for resizable mmaps util/mmap-alloc: Implement resizable mmaps
numa: Introduceram_block_notify_resized()and
ram_block_notifiers_support_resize()
util: vfio-helpers: Implementram_block_resized()
util: oslib: Resizable anonymous allocations under POSIX
exec: Ram blocks with resizable anonymous allocations under POSIXexec.c | 99 ++++++++++++++++++---- hw/core/numa.c | 39 +++++++++ include/exec/cpu-common.h | 3 + include/exec/memory.h | 8 ++ include/exec/ramlist.h | 4 + include/qemu/mmap-alloc.h | 21 +++-- include/qemu/osdep.h | 6 +- stubs/ram-block.c | 20 ----- util/mmap-alloc.c | 168 ++++++++++++++++++++++++-------------- util/oslib-posix.c | 37 ++++++++- util/oslib-win32.c | 14 ++++ util/trace-events | 5 +- util/vfio-helpers.c | 33 ++++---- 13 files changed, 331 insertions(+), 126 deletions(-)Signature · 1 line
2.24.1
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · David Hildenbrand
We already allow resizable ram blocks for anonymous memory, however, they are not actually resized. All memory is
mmaped()R/W, including the memory exceeding theused_length, up to themax_length.When resizing, effectively only the boundary is moved. Implement actually resizable anonymous allocations and make use of them in resizable ram blocks when possible. Memory exceeding the
used_lengthwill be inaccessible. Especially ram block notifiers require care.Having actually resizable anonymous allocations (via mmap-hackery) allows to reserve a big region in virtual address space and grow the accessible/usable part on demand. Even if "
/proc/sys/vm/overcommit_memory" is set to "never" under Linux, huge reservations will succeed. If there is not enough memory when resizing (to populate parts of the reserved region), trying to resize will fail. Only the actually used size is reserved in the OS.
Quoted 8 lines
Quoted 5 lines
E.g., virtio-mem [1] wants to reserve big resizable memory regions and grow the usable part on demand. I think this change is worth sending out individually. Accompanied by a bunch of minor fixes and cleanups.
How does this inteact with all the prealloc/mlock things designed for realtime?
- Prealloc: we don't support resizable ram blocks with prealloc -- qemu_ram_alloc_from_ptr() is the only way to get "real" preallocated ram blocks from a pointer. They are never resizable.
-- "prealloc" with memory backends (e.g., backends/hostmem.c): Memory
backends don't support resizable ram blocks yet. I have patches to support that, and disallow prealloc for them.
-- file-based ram blocks are not resizable
- mlock
-- os_mlock() does a mlockall(MCL_CURRENT | MCL_FUTURE)
-- That should work just fine with ordinary mmap() invalidating old
mmaps and creating new mmaps(). Just like when hotplugging/unplugging a DIMM.
Resizing currently only happens during reboot/migration. If mlock_all would result in issues, we could fallback to old handling (if (enable_mlock) ...) - but I don't think this is necessary.
So I don't see an issues with that :) Thanks!
Signature · 3 lines
Thanks, David / dhildenb
Quoted 17 lines · * David Hildenbrand ([email protected]) wrote:
We already allow resizable ram blocks for anonymous memory, however, they are not actually resized. All memory is
mmaped()R/W, including the memory exceeding theused_length, up to themax_length.When resizing, effectively only the boundary is moved. Implement actually resizable anonymous allocations and make use of them in resizable ram blocks when possible. Memory exceeding the
used_lengthwill be inaccessible. Especially ram block notifiers require care.Having actually resizable anonymous allocations (via mmap-hackery) allows to reserve a big region in virtual address space and grow the accessible/usable part on demand. Even if "
/proc/sys/vm/overcommit_memory" is set to "never" under Linux, huge reservations will succeed. If there is not enough memory when resizing (to populate parts of the reserved region), trying to resize will fail. Only the actually used size is reserved in the OS.
Quoted 5 lines
E.g., virtio-mem [1] wants to reserve big resizable memory regions and grow the usable part on demand. I think this change is worth sending out individually. Accompanied by a bunch of minor fixes and cleanups.
There's a few bits I've not understood from skimming the patches:
a) Am I correct in thinking you PROT_NONE the extra space so you can
gkrow/shrink it?
b) What does kvm see - does it have a slot for the whole space or for
only the used space?
I ask because we found with virtiofs/DAX experiments that on Power,
kvm gets upset if you give it a mapping with PROT_NONE.
(That maybe less of an issue if you change the mapping after the
slot is created).
c) It's interesting this is keyed off the RAMBlock notifiers - do memory_listener's on the address space the block is mapped into get triggered? I'm wondering how vhost (and vhost-user) in particular see this.
Dave
Quoted 20 lines
David Hildenbrand (13):
util: vfio-helpers: Factor out and fix processing of existings ram
blocks
exec: Factor out setting ram settings (madvise ...) into
qemu_ram_apply_settings()
exec: Reuseqemu_ram_apply_settings()inqemu_ram_remap()
exec: Drop "shared" parameter fromram_block_add()util/mmap-alloc: Factor out calculation of pagesize tommap_pagesize()util/mmap-alloc: Factor out reserving of a memory region to
mmap_reserve()
util/mmap-alloc: Factor out populating of memory tommap_populate()util/mmap-alloc: Prepare for resizable mmaps util/mmap-alloc: Implement resizable mmaps
numa: Introduceram_block_notify_resized()and
ram_block_notifiers_support_resize()
util: vfio-helpers: Implementram_block_resized()
util: oslib: Resizable anonymous allocations under POSIX
exec: Ram blocks with resizable anonymous allocations under POSIX
Quoted 18 lines
exec.c | 99 ++++++++++++++++++---- hw/core/numa.c | 39 +++++++++ include/exec/cpu-common.h | 3 + include/exec/memory.h | 8 ++ include/exec/ramlist.h | 4 + include/qemu/mmap-alloc.h | 21 +++-- include/qemu/osdep.h | 6 +- stubs/ram-block.c | 20 ----- util/mmap-alloc.c | 168 ++++++++++++++++++++++++-------------- util/oslib-posix.c | 37 ++++++++- util/oslib-win32.c | 14 ++++ util/trace-events | 5 +- util/vfio-helpers.c | 33 ++++---- 13 files changed, 331 insertions(+), 126 deletions(-)Signature · 1 line
2.24.1
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 22 lines
Am 06.02.2020 um 21:11 schrieb Dr. David Alan Gilbert <[email protected]>:
Quoted 17 lines · * David Hildenbrand
We already allow resizable ram blocks for anonymous memory, however, they are not actually resized. All memory is
mmaped()R/W, including the memory exceeding theused_length, up to themax_length.When resizing, effectively only the boundary is moved. Implement actually resizable anonymous allocations and make use of them in resizable ram blocks when possible. Memory exceeding the
used_lengthwill be inaccessible. Especially ram block notifiers require care.Having actually resizable anonymous allocations (via mmap-hackery) allows to reserve a big region in virtual address space and grow the accessible/usable part on demand. Even if "
/proc/sys/vm/overcommit_memory" is set to "never" under Linux, huge reservations will succeed. If there is not enough memory when resizing (to populate parts of the reserved region), trying to resize will fail. Only the actually used size is reserved in the OS.
Quoted 8 lines
Quoted 5 lines
E.g., virtio-mem [1] wants to reserve big resizable memory regions and grow the usable part on demand. I think this change is worth sending out individually. Accompanied by a bunch of minor fixes and cleanups.
There's a few bits I've not understood from skimming the patches:
Thanks for having a look!
Quoted 2 lines
a) Am I correct in thinking you
PROT_NONEthe extra space so you can
gkrow/shrink it?
Yes!
Quoted 2 lines
b) What does kvm see - does it have a slot for the whole space or for
only the used space?
Only the used space. Resizing triggers a resize of the memory region. That triggers memory notifiers, which remove the old kvm memslot and re-add the new kvm memslot. (That‘s existing handling, so nothing new).
So KVM will not see PROT_NONE when creating a slot.
Quoted 4 lines
I ask because we found with virtiofs/DAX experiments that on Power,
kvm gets upset if you give it a mapping withPROT_NONE.
(That maybe less of an issue if you change the mapping after the
slot is created).
That should work as expected. Resizing *while* kvm is running is tricky, but that‘s not part of this series and a different story :) right now, resizing is only valid on reboot/incoming migration.
Quoted 5 lines
c) It's interesting this is keyed off the RAMBlock notifiers - do memory_listener's on the address space the block is mapped into get triggered? I'm wondering how vhost (and vhost-user) in particular see this.
Yes, memory listeners get triggered. Old region is removed, new one is added. Nothing changed on that front.
The issue with ram block notifiers is that they did not do a „remove old, add new“ on resizes. They only added the full ram block. Bad. E.g., vfio wants to pin all memory - which would fail on PROT_NONE.
E.g., for HAX, there is no kernel ioctl to remove a ram block ... for SEV there is, but I am not sure about the implications when converting back and forth between encrypted/unencrypted. So SEV and HAX require legacy handling.
Cheers!
Quoted 36 lines · * David Hildenbrand ([email protected]) wrote:
Quoted 22 lines
Am 06.02.2020 um 21:11 schrieb Dr. David Alan Gilbert <[email protected]>:
Quoted 17 lines · * David Hildenbrand
We already allow resizable ram blocks for anonymous memory, however, they are not actually resized. All memory is
mmaped()R/W, including the memory exceeding theused_length, up to themax_length.When resizing, effectively only the boundary is moved. Implement actually resizable anonymous allocations and make use of them in resizable ram blocks when possible. Memory exceeding the
used_lengthwill be inaccessible. Especially ram block notifiers require care.Having actually resizable anonymous allocations (via mmap-hackery) allows to reserve a big region in virtual address space and grow the accessible/usable part on demand. Even if "
/proc/sys/vm/overcommit_memory" is set to "never" under Linux, huge reservations will succeed. If there is not enough memory when resizing (to populate parts of the reserved region), trying to resize will fail. Only the actually used size is reserved in the OS.Quoted 8 lines
Quoted 5 lines
E.g., virtio-mem [1] wants to reserve big resizable memory regions and grow the usable part on demand. I think this change is worth sending out individually. Accompanied by a bunch of minor fixes and cleanups.
There's a few bits I've not understood from skimming the patches:
Thanks for having a look!
Quoted 2 lines
a) Am I correct in thinking you
PROT_NONEthe extra space so you can
gkrow/shrink it?
Quoted 8 lines
Yes!
Quoted 2 lines
b) What does kvm see - does it have a slot for the whole space or for
only the used space?Only the used space. Resizing triggers a resize of the memory region. That triggers memory notifiers, which remove the old kvm memslot and re-add the new kvm memslot. (That‘s existing handling, so nothing new).
So KVM will not see
PROT_NONEwhen creating a slot.
OK, that's easy then.
Quoted 6 lines
Quoted 4 lines
I ask because we found with virtiofs/DAX experiments that on Power,
kvm gets upset if you give it a mapping withPROT_NONE.
(That maybe less of an issue if you change the mapping after the
slot is created).That should work as expected. Resizing *while* kvm is running is tricky, but that‘s not part of this series and a different story :) right now, resizing is only valid on reboot/incoming migration.
Hmm 'when' during an incoming migration; I ask because of userfaultfd setup for postcopy. Also note those things can combine - i.e. a reboot that happens during a migration (we've already got a pile of related bugs).
Quoted 11 lines
Quoted 5 lines
c) It's interesting this is keyed off the RAMBlock notifiers - do memory_listener's on the address space the block is mapped into get triggered? I'm wondering how vhost (and vhost-user) in particular see this.
Yes, memory listeners get triggered. Old region is removed, new one is added. Nothing changed on that front.
The issue with ram block notifiers is that they did not do a „remove old, add new“ on resizes. They only added the full ram block. Bad. E.g., vfio wants to pin all memory - which would fail on
PROT_NONE.E.g., for HAX, there is no kernel ioctl to remove a ram block ... for SEV there is, but I am not sure about the implications when converting back and forth between encrypted/unencrypted. So SEV and HAX require legacy handling.
I guess for a memory listener it just sees a new layout after the commit and then can figure out what changed.
Dave
Quoted 2 lines
Cheers!
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 6 lines
Quoted 1 line
That should work as expected. Resizing *while* kvm is running is tricky, but that‘s not part of this series and a different story :) right now, resizing is only valid on reboot/incoming migration.
Hmm 'when' during an incoming migration; I ask because of userfaultfd setup for postcopy. Also note those things can combine - i.e. a reboot that happens during a migration (we've already got a pile of related bugs).
Yes, resizing while migration is in progress (source already sent the rem block sizes, receiving side in postcopy), or when dumping is active is bad. In general, resizing while *somebody* thinks the used_length of a ram block won't change is bad.
Incoming migration: migration/ram.c: ram_load_precopy(): RAM_SAVE_FLAG_MEM_SIZE
So in preload, before the guest is running and before postcopy has started. At that point, performing the resize is fine.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
c) It's interesting this is keyed off the RAMBlock notifiers - do memory_listener's on the address space the block is mapped into get triggered? I'm wondering how vhost (and vhost-user) in particular see this.
Quoted 8 lines
Quoted 5 lines
Yes, memory listeners get triggered. Old region is removed, new one is added. Nothing changed on that front.
The issue with ram block notifiers is that they did not do a „remove old, add new“ on resizes. They only added the full ram block. Bad. E.g., vfio wants to pin all memory - which would fail on
PROT_NONE.E.g., for HAX, there is no kernel ioctl to remove a ram block ... for SEV there is, but I am not sure about the implications when converting back and forth between encrypted/unencrypted. So SEV and HAX require legacy handling.
I guess for a memory listener it just sees a new layout after the commit and then can figure out what changed.
Exactly.
Signature · 3 lines
Thanks, David / dhildenb