LekKit / RVVM

The RISC-V Virtual Machine
GNU General Public License v3.0
937 stars 66 forks source link

Dynamic allocation / ballooning memory? #76

Open adamierymenko opened 1 year ago

adamierymenko commented 1 year ago

Another obvious idea: implement support for minimum and maximum memory and dynamic allocation / ballooning.

LekKit commented 1 year ago

That's a great idea to implement free page reporting, and that's part of recent versions of Virtio and VMWare balloon devices. I'll look into that (At last a good virtio device with no direct HW replacements, lol). There are also other good memory saving techniques in RVVM: KSM (Page merging, could also reclaim free pages in some cases) and transparent caches (Guest caches may be reclaimed by host).

Be aware however that explicit ballooning on host demand (Initial usecase of Virtio Balloon device) isn't that great of a concept: You force the guest to use less memory, and it most likely will start swapping / will OOM. It is much better to have large amount of virtual memory (zram/swap) on the host - from efficiency standpoint. Otherwise you'll just make pressure on the guest and make it suffer.

ZLangJIT commented 2 months ago

https://github.com/LekKit/RVVM/issues/147#issuecomment-2384627249

virtio-balloon is a guest device, and has two separate modes of operation:

* Actual "ballooning" (as implied by the original devie name), where host forces the guest to "reserve" it's physical pages and then reclaims those pages on the host. It's a weird, old mode which is unlikely to ever be implemented in RVVM because it's just badly designed

* Free page reporting: A newer mode, where guests gets to report pages which are **actually** unused, then the VM can zero them without any harm for the guest.

The needed host-specific functionality is already implemented in src/vma_ops.c, specifically vma_clean() function. It works on any OS including Windows. It is also already used to release unused JIT heap space. So it's a matter of writing a virtio-balloon device emulation driver.

It is mostly redundant feature if we consider KSM, but it surely will help non-Linux hosts.

which is what makes it capable of returning unused paged back to the os

eg, its current memory usage without baloon would be its peak usage for the lifetime of the processes due to the vm being unable to tell which parts of any memory it has been requested have been unused (eg freed)

eg free(malloc(10000))

ZLangJIT commented 1 month ago

this may be useful https://youtu.be/Fq47WCCm-HM

a detailed overview of traditional mem balloon and virtio mem balloon / virtio-pmem / virtio-mem ect (what it is, what it does, how they work, ect)

also details free page hinting and many other features related to guest<->host memory management

some parts can be very detailed and others can lack detail for simplification

virtio related #149

might also be related to #147