Skip to content
  • David Hildenbrand's avatar
    8947d7fc
    memory: Introduce RamDiscardManager for RAM memory regions · 8947d7fc
    David Hildenbrand authored
    We have some special RAM memory regions (managed by virtio-mem), whereby
    the guest agreed to only use selected memory ranges. "unused" parts are
    discarded so they won't consume memory - to logically unplug these memory
    ranges. Before the VM is allowed to use such logically unplugged memory
    again, coordination with the hypervisor is required.
    
    This results in "sparse" mmaps/RAMBlocks/memory regions, whereby only
    coordinated parts are valid to be used/accessed by the VM.
    
    In most cases, we don't care about that - e.g., in KVM, we simply have a
    single KVM memory slot. However, in case of vfio, registering the
    whole region with the kernel results in all pages getting pinned, and
    therefore an unexpected high memory consumption - discarding of RAM in
    that context is broken.
    
    Let's introduce a way to coordinate discarding/populating memory within a
    RAM memory region with such special consumers of RAM memory regions: they
    can register as listeners and get updates on memory getting discarded and
    populated. Using this machinery, vfio will be able to map only the
    currently populated parts, resulting in discarded parts not getting pinned
    and not consuming memory.
    
    A RamDiscardManager has to be set for a memory region before it is getting
    mapped, and cannot change while the memory region is mapped.
    
    Note: At some point, we might want to let RAMBlock users (esp. vfio used
    for nvme://
    
    ) consume this interface as well. We'll need RAMBlock notifier
    calls when a RAMBlock is getting mapped/unmapped (via the corresponding
    memory region), so we can properly register a listener there as well.
    
    Reviewed-by: default avatarPankaj Gupta <pankaj.gupta@cloud.ionos.com>
    Acked-by: default avatarMichael S. Tsirkin <mst@redhat.com>
    Cc: Paolo Bonzini <pbonzini@redhat.com>
    Cc: "Michael S. Tsirkin" <mst@redhat.com>
    Cc: Alex Williamson <alex.williamson@redhat.com>
    Cc: Dr. David Alan Gilbert <dgilbert@redhat.com>
    Cc: Igor Mammedov <imammedo@redhat.com>
    Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
    Cc: Peter Xu <peterx@redhat.com>
    Cc: Auger Eric <eric.auger@redhat.com>
    Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
    Cc: teawater <teawaterz@linux.alibaba.com>
    Cc: Marek Kedzierski <mkedzier@redhat.com>
    Signed-off-by: default avatarDavid Hildenbrand <david@redhat.com>
    Message-Id: <20210413095531.25603-2-david@redhat.com>
    Signed-off-by: default avatarEduardo Habkost <ehabkost@redhat.com>
    8947d7fc
    memory: Introduce RamDiscardManager for RAM memory regions
    David Hildenbrand authored
    We have some special RAM memory regions (managed by virtio-mem), whereby
    the guest agreed to only use selected memory ranges. "unused" parts are
    discarded so they won't consume memory - to logically unplug these memory
    ranges. Before the VM is allowed to use such logically unplugged memory
    again, coordination with the hypervisor is required.
    
    This results in "sparse" mmaps/RAMBlocks/memory regions, whereby only
    coordinated parts are valid to be used/accessed by the VM.
    
    In most cases, we don't care about that - e.g., in KVM, we simply have a
    single KVM memory slot. However, in case of vfio, registering the
    whole region with the kernel results in all pages getting pinned, and
    therefore an unexpected high memory consumption - discarding of RAM in
    that context is broken.
    
    Let's introduce a way to coordinate discarding/populating memory within a
    RAM memory region with such special consumers of RAM memory regions: they
    can register as listeners and get updates on memory getting discarded and
    populated. Using this machinery, vfio will be able to map only the
    currently populated parts, resulting in discarded parts not getting pinned
    and not consuming memory.
    
    A RamDiscardManager has to be set for a memory region before it is getting
    mapped, and cannot change while the memory region is mapped.
    
    Note: At some point, we might want to let RAMBlock users (esp. vfio used
    for nvme://
    
    ) consume this interface as well. We'll need RAMBlock notifier
    calls when a RAMBlock is getting mapped/unmapped (via the corresponding
    memory region), so we can properly register a listener there as well.
    
    Reviewed-by: default avatarPankaj Gupta <pankaj.gupta@cloud.ionos.com>
    Acked-by: default avatarMichael S. Tsirkin <mst@redhat.com>
    Cc: Paolo Bonzini <pbonzini@redhat.com>
    Cc: "Michael S. Tsirkin" <mst@redhat.com>
    Cc: Alex Williamson <alex.williamson@redhat.com>
    Cc: Dr. David Alan Gilbert <dgilbert@redhat.com>
    Cc: Igor Mammedov <imammedo@redhat.com>
    Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
    Cc: Peter Xu <peterx@redhat.com>
    Cc: Auger Eric <eric.auger@redhat.com>
    Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
    Cc: teawater <teawaterz@linux.alibaba.com>
    Cc: Marek Kedzierski <mkedzier@redhat.com>
    Signed-off-by: default avatarDavid Hildenbrand <david@redhat.com>
    Message-Id: <20210413095531.25603-2-david@redhat.com>
    Signed-off-by: default avatarEduardo Habkost <ehabkost@redhat.com>
Loading