Re: [PATCH v2 15/16] memory,vhost: Allow for marking memory device memory regions unmergeable

Philippe Mathieu-Daudé
2023-08-28T22:44:53Z

On 25/8/23 15:21, David Hildenbrand wrote:
> Let's allow for marking memory regions unmergeable, to teach
> flatview code and vhost to not merge adjacent aliases to the same memory
> region into a larger memory section; instead, we want separate aliases to
> stay separate such that we can atomically map/unmap aliases without
> affecting other aliases.
>
> This is desired for virtio-mem mapping device memory located on a RAM
> memory region via multiple aliases into a memory region container,
> resulting in separate memslots that can get (un)mapped atomically.
>
> As an example with virtio-mem, the layout would look something like this:
> [...]
> 0000000240000000-00000020bfffffff (prio 0, i/o): device-memory
> 0000000240000000-000000043fffffff (prio 0, i/o): virtio-mem
> 0000000240000000-000000027fffffff (prio 0, ram): alias memslot-0 @mem2 0000000000000000-000000003fffffff
> 0000000280000000-00000002bfffffff (prio 0, ram): alias memslot-1 @mem2 0000000040000000-000000007fffffff
> 00000002c0000000-00000002ffffffff (prio 0, ram): alias memslot-2 @mem2 0000000080000000-00000000bfffffff
> [...]
>
> Without unmergable memory regions, all three memslots would get merged into
> a single memory section. For example, when mapping another alias (e.g.,
> virtio-mem-memslot-3) or when unmapping any of the mapped aliases,
> memory listeners will first get notified about the removal of the big
> memory section to then get notified about re-adding of the new
> (differently merged) memory section(s).
>
> In an ideal world, memory listeners would be able to deal with that
> atomically, like KVM nowadays does. However, (a) supporting this for other
> memory listeners (vhost-user, vfio) is fairly hard: temporary removal
> can result in all kinds of issues on concurrent access to guest memory;
> and (b) this handling is undesired, because temporarily removing+readding
> can consume quite some time on bigger memslots and is not efficient
> (e.g., vfio unpinning and repinning pages ...).
>
> Let's allow for marking a memory region unmergeable, such that we
> can atomically (un)map aliases to the same memory region, similar to
> (un)mapping individual DIMMs.
>
> Similarly, teach vhost code to not redo what flatview core stopped doing:
> don't merge such sections. Merging in vhost code is really only relevant
> for handling random holes in boot memory where; without this merging,
> the vhost-user backend wouldn't be able to mmap() some boot memory
> backed on hugetlb.
>
> We'll use this for virtio-mem next.
>
> Signed-off-by: David Hildenbrand <david@redhat.com>
> ---
> hw/virtio/vhost.c | 4 ++--
> include/exec/memory.h | 22 ++++++++++++++++++++++
> softmmu/memory.c | 31 +++++++++++++++++++++++++------
> 3 files changed, 49 insertions(+), 8 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>