Re: [RFC] memory pressure detection in VMs using PSI mechanism for dynamically inflating/deflating VM memory
From: David Hildenbrand
Date: Tue Jan 24 2023 - 10:21:23 EST
On 24.01.23 00:04, Sudarshan Rajagopalan wrote:
[...]
One problematic thing is that adding memory to Linux by virtio-mem
eventually consumes memory (e.g., the memmap), especially when having
to to add a completely new memory block to Linux.
Yes we have thought about this issue as well where-in when system is
heavily on memory pressure, it would require some memory for memmap
metadata, and also few other places in memory hotplug that it would need
to alloc_pages for hot-plugging in. I think this path in memory_hotplug
may be fixed where it doesn't rely on allocating some small portion of
memory for hotplugging. But, the purpose memory_hotplug itself wasn't
for plugging memory on system being in memory pressure :).
Some small allocations might be classified as "urgent" and go to atomic
reserves (e.g., resource tree node, memory device node). The big
allocations (memmap, page-ext if enabled, eventually page tables for
direct map when not mapping huge pages) are the problematic "memory
consumers" I think.
So if you're already under severe memory pressure, these allocations
to bring up new memory can fail. The question is, if PSI can notify
"early" enough such that this barely happens in practice.
There are some possible ways to mitigate:
1) Always keep spare memory blocks by virtio-mem added to Linux, that
B B don't expose any memory yet. Memory from these block can be handed
B B over to Linux without additional Linux allocations. Of course, they
B B consume metadata, so one might want to limit them.
2) Implement memmap_on_memory support for virtio-mem. This might help in
B B some setups, where the device block size is suitable.
Did you run into that scenario already during your experiments, and
how did you deal with that?
We are exactly implementing 2) you had mentioned i.e. enabling
memmap_on_memory support for virtio-mem. This always guarantees that
free memory is always present for memmap metadata while hotplugging. But
this required us to increase memory block size to 256MB (from 128MB) for
alignment requirement of memory hotplug to enable memory_on_memmap, for
4K page size configuration. Option 1) you mentioned also seems
The memmap of 128 MiB is 2 MiB. Assuming the pageblock size is 2 MiB,
and virtio-mem supports a device block size of 2 MiB, it should "in
theory" also work with 128 MiB memory blocks.
So I'd be curious why the change to 256 MiB was required. Maybe, that
kernel config ends up with a pageblock size of 4 MiB (IIRC that can
happen without CONFIG_HUGETLB -- which we should most probbaly change to
also be PMD_ORDER due to THP).
interesting - its good to have some spare memory in hand when system is
heavily in memory pressure so that this memory can be handed over
immediately on PSI pressure and doesn't have to wait for memory plug-in
request roundtrip from Primary VM.
The idea was that you'd still do the roundtrip to request plugging of
device memory blocks, but that you could immediately expose memory to
the system (without requiring allocations), to eventually immediately
prepare the next Linux memory block while "fresh" memory is available.
This way you could handle most allocations that happen when adding a
Linux memory block.
The main idea was to always have at least one spare one lying around.
And as soon as you start exposing memory from one of them to the page
allocator, immediately prepare the next one.
Do you think having memmap_on_memory support for virtio-mem is useful to
have? If so, we can send the patch that supports this in virtio-mem?
I think yes. However, last time I though about adding support, I
realized that there are some ugly corner cases to handle cleanly.
You have to make sure that the device memory blocks to-be-used as memmap
are "plugged" even before calling add_memory_driver_managed(). And you
can only "unplug" these device memory blocks after the memory block was
removed via offline_and_remove_memory().
So the whole order of events and management of plugged device blocks
changes quite a bit ...
... and what to do if the device block size is, say 4MiB, but the memmap
is 2 MiB? Of course, one could simply skip the optimization then.
Having that said, if you managed to get it running and it's not too
ugly, please share.
--
Thanks,
David / dhildenb