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/20191212171137.13872-1-da...@redhat.com/ 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(-) -- 2.24.1