memory: Introduce RamDiscardManager for RAM memory regions

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: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
Acked-by: Michael 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: David Hildenbrand <david@redhat.com>
Message-Id: <20210413095531.25603-2-david@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
This commit is contained in:
David Hildenbrand 2021-04-13 11:55:19 +02:00 committed by Eduardo Habkost
parent cdcf766d0b
commit 8947d7fc4e
2 changed files with 335 additions and 22 deletions

View File

@ -42,6 +42,12 @@ typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass;
DECLARE_OBJ_CHECKERS(IOMMUMemoryRegion, IOMMUMemoryRegionClass,
IOMMU_MEMORY_REGION, TYPE_IOMMU_MEMORY_REGION)
#define TYPE_RAM_DISCARD_MANAGER "qemu:ram-discard-manager"
typedef struct RamDiscardManagerClass RamDiscardManagerClass;
typedef struct RamDiscardManager RamDiscardManager;
DECLARE_OBJ_CHECKERS(RamDiscardManager, RamDiscardManagerClass,
RAM_DISCARD_MANAGER, TYPE_RAM_DISCARD_MANAGER);
#ifdef CONFIG_FUZZ
void fuzz_dma_read_cb(size_t addr,
size_t len,
@ -65,6 +71,28 @@ struct ReservedRegion {
unsigned type;
};
/**
* struct MemoryRegionSection: describes a fragment of a #MemoryRegion
*
* @mr: the region, or %NULL if empty
* @fv: the flat view of the address space the region is mapped in
* @offset_within_region: the beginning of the section, relative to @mr's start
* @size: the size of the section; will not exceed @mr's boundaries
* @offset_within_address_space: the address of the first byte of the section
* relative to the region's address space
* @readonly: writes to this section are ignored
* @nonvolatile: this section is non-volatile
*/
struct MemoryRegionSection {
Int128 size;
MemoryRegion *mr;
FlatView *fv;
hwaddr offset_within_region;
hwaddr offset_within_address_space;
bool readonly;
bool nonvolatile;
};
typedef struct IOMMUTLBEntry IOMMUTLBEntry;
/* See address_space_translate: bit 0 is read, bit 1 is write. */
@ -448,6 +476,206 @@ struct IOMMUMemoryRegionClass {
Error **errp);
};
typedef struct RamDiscardListener RamDiscardListener;
typedef int (*NotifyRamPopulate)(RamDiscardListener *rdl,
MemoryRegionSection *section);
typedef void (*NotifyRamDiscard)(RamDiscardListener *rdl,
MemoryRegionSection *section);
struct RamDiscardListener {
/*
* @notify_populate:
*
* Notification that previously discarded memory is about to get populated.
* Listeners are able to object. If any listener objects, already
* successfully notified listeners are notified about a discard again.
*
* @rdl: the #RamDiscardListener getting notified
* @section: the #MemoryRegionSection to get populated. The section
* is aligned within the memory region to the minimum granularity
* unless it would exceed the registered section.
*
* Returns 0 on success. If the notification is rejected by the listener,
* an error is returned.
*/
NotifyRamPopulate notify_populate;
/*
* @notify_discard:
*
* Notification that previously populated memory was discarded successfully
* and listeners should drop all references to such memory and prevent
* new population (e.g., unmap).
*
* @rdl: the #RamDiscardListener getting notified
* @section: the #MemoryRegionSection to get populated. The section
* is aligned within the memory region to the minimum granularity
* unless it would exceed the registered section.
*/
NotifyRamDiscard notify_discard;
/*
* @double_discard_supported:
*
* The listener suppors getting @notify_discard notifications that span
* already discarded parts.
*/
bool double_discard_supported;
MemoryRegionSection *section;
QLIST_ENTRY(RamDiscardListener) next;
};
static inline void ram_discard_listener_init(RamDiscardListener *rdl,
NotifyRamPopulate populate_fn,
NotifyRamDiscard discard_fn,
bool double_discard_supported)
{
rdl->notify_populate = populate_fn;
rdl->notify_discard = discard_fn;
rdl->double_discard_supported = double_discard_supported;
}
typedef int (*ReplayRamPopulate)(MemoryRegionSection *section, void *opaque);
/*
* RamDiscardManagerClass:
*
* A #RamDiscardManager coordinates which parts of specific RAM #MemoryRegion
* regions are currently populated to be used/accessed by the VM, notifying
* after parts were discarded (freeing up memory) and before parts will be
* populated (consuming memory), to be used/acessed by the VM.
*
* A #RamDiscardManager can only be set for a RAM #MemoryRegion while the
* #MemoryRegion isn't mapped yet; it cannot change while the #MemoryRegion is
* mapped.
*
* The #RamDiscardManager is intended to be used by technologies that are
* incompatible with discarding of RAM (e.g., VFIO, which may pin all
* memory inside a #MemoryRegion), and require proper coordination to only
* map the currently populated parts, to hinder parts that are expected to
* remain discarded from silently getting populated and consuming memory.
* Technologies that support discarding of RAM don't have to bother and can
* simply map the whole #MemoryRegion.
*
* An example #RamDiscardManager is virtio-mem, which logically (un)plugs
* memory within an assigned RAM #MemoryRegion, coordinated with the VM.
* Logically unplugging memory consists of discarding RAM. The VM agreed to not
* access unplugged (discarded) memory - especially via DMA. virtio-mem will
* properly coordinate with listeners before memory is plugged (populated),
* and after memory is unplugged (discarded).
*
* Listeners are called in multiples of the minimum granularity (unless it
* would exceed the registered range) and changes are aligned to the minimum
* granularity within the #MemoryRegion. Listeners have to prepare for memory
* becomming discarded in a different granularity than it was populated and the
* other way around.
*/
struct RamDiscardManagerClass {
/* private */
InterfaceClass parent_class;
/* public */
/**
* @get_min_granularity:
*
* Get the minimum granularity in which listeners will get notified
* about changes within the #MemoryRegion via the #RamDiscardManager.
*
* @rdm: the #RamDiscardManager
* @mr: the #MemoryRegion
*
* Returns the minimum granularity.
*/
uint64_t (*get_min_granularity)(const RamDiscardManager *rdm,
const MemoryRegion *mr);
/**
* @is_populated:
*
* Check whether the given #MemoryRegionSection is completely populated
* (i.e., no parts are currently discarded) via the #RamDiscardManager.
* There are no alignment requirements.
*
* @rdm: the #RamDiscardManager
* @section: the #MemoryRegionSection
*
* Returns whether the given range is completely populated.
*/
bool (*is_populated)(const RamDiscardManager *rdm,
const MemoryRegionSection *section);
/**
* @replay_populated:
*
* Call the #ReplayRamPopulate callback for all populated parts within the
* #MemoryRegionSection via the #RamDiscardManager.
*
* In case any call fails, no further calls are made.
*
* @rdm: the #RamDiscardManager
* @section: the #MemoryRegionSection
* @replay_fn: the #ReplayRamPopulate callback
* @opaque: pointer to forward to the callback
*
* Returns 0 on success, or a negative error if any notification failed.
*/
int (*replay_populated)(const RamDiscardManager *rdm,
MemoryRegionSection *section,
ReplayRamPopulate replay_fn, void *opaque);
/**
* @register_listener:
*
* Register a #RamDiscardListener for the given #MemoryRegionSection and
* immediately notify the #RamDiscardListener about all populated parts
* within the #MemoryRegionSection via the #RamDiscardManager.
*
* In case any notification fails, no further notifications are triggered
* and an error is logged.
*
* @rdm: the #RamDiscardManager
* @rdl: the #RamDiscardListener
* @section: the #MemoryRegionSection
*/
void (*register_listener)(RamDiscardManager *rdm,
RamDiscardListener *rdl,
MemoryRegionSection *section);
/**
* @unregister_listener:
*
* Unregister a previously registered #RamDiscardListener via the
* #RamDiscardManager after notifying the #RamDiscardListener about all
* populated parts becoming unpopulated within the registered
* #MemoryRegionSection.
*
* @rdm: the #RamDiscardManager
* @rdl: the #RamDiscardListener
*/
void (*unregister_listener)(RamDiscardManager *rdm,
RamDiscardListener *rdl);
};
uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager *rdm,
const MemoryRegion *mr);
bool ram_discard_manager_is_populated(const RamDiscardManager *rdm,
const MemoryRegionSection *section);
int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,
MemoryRegionSection *section,
ReplayRamPopulate replay_fn,
void *opaque);
void ram_discard_manager_register_listener(RamDiscardManager *rdm,
RamDiscardListener *rdl,
MemoryRegionSection *section);
void ram_discard_manager_unregister_listener(RamDiscardManager *rdm,
RamDiscardListener *rdl);
typedef struct CoalescedMemoryRange CoalescedMemoryRange;
typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
@ -494,6 +722,7 @@ struct MemoryRegion {
const char *name;
unsigned ioeventfd_nb;
MemoryRegionIoeventfd *ioeventfds;
RamDiscardManager *rdm; /* Only for RAM */
};
struct IOMMUMemoryRegion {
@ -825,28 +1054,6 @@ typedef bool (*flatview_cb)(Int128 start,
*/
void flatview_for_each_range(FlatView *fv, flatview_cb cb, void *opaque);
/**
* struct MemoryRegionSection: describes a fragment of a #MemoryRegion
*
* @mr: the region, or %NULL if empty
* @fv: the flat view of the address space the region is mapped in
* @offset_within_region: the beginning of the section, relative to @mr's start
* @size: the size of the section; will not exceed @mr's boundaries
* @offset_within_address_space: the address of the first byte of the section
* relative to the region's address space
* @readonly: writes to this section are ignored
* @nonvolatile: this section is non-volatile
*/
struct MemoryRegionSection {
Int128 size;
MemoryRegion *mr;
FlatView *fv;
hwaddr offset_within_region;
hwaddr offset_within_address_space;
bool readonly;
bool nonvolatile;
};
static inline bool MemoryRegionSection_eq(MemoryRegionSection *a,
MemoryRegionSection *b)
{
@ -2023,6 +2230,41 @@ bool memory_region_present(MemoryRegion *container, hwaddr addr);
*/
bool memory_region_is_mapped(MemoryRegion *mr);
/**
* memory_region_get_ram_discard_manager: get the #RamDiscardManager for a
* #MemoryRegion
*
* The #RamDiscardManager cannot change while a memory region is mapped.
*
* @mr: the #MemoryRegion
*/
RamDiscardManager *memory_region_get_ram_discard_manager(MemoryRegion *mr);
/**
* memory_region_has_ram_discard_manager: check whether a #MemoryRegion has a
* #RamDiscardManager assigned
*
* @mr: the #MemoryRegion
*/
static inline bool memory_region_has_ram_discard_manager(MemoryRegion *mr)
{
return !!memory_region_get_ram_discard_manager(mr);
}
/**
* memory_region_set_ram_discard_manager: set the #RamDiscardManager for a
* #MemoryRegion
*
* This function must not be called for a mapped #MemoryRegion, a #MemoryRegion
* that does not cover RAM, or a #MemoryRegion that already has a
* #RamDiscardManager assigned.
*
* @mr: the #MemoryRegion
* @rdm: #RamDiscardManager to set
*/
void memory_region_set_ram_discard_manager(MemoryRegion *mr,
RamDiscardManager *rdm);
/**
* memory_region_find: translate an address/size relative to a
* MemoryRegion into a #MemoryRegionSection.

View File

@ -2027,6 +2027,70 @@ int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr)
return imrc->num_indexes(iommu_mr);
}
RamDiscardManager *memory_region_get_ram_discard_manager(MemoryRegion *mr)
{
if (!memory_region_is_mapped(mr) || !memory_region_is_ram(mr)) {
return NULL;
}
return mr->rdm;
}
void memory_region_set_ram_discard_manager(MemoryRegion *mr,
RamDiscardManager *rdm)
{
g_assert(memory_region_is_ram(mr) && !memory_region_is_mapped(mr));
g_assert(!rdm || !mr->rdm);
mr->rdm = rdm;
}
uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager *rdm,
const MemoryRegion *mr)
{
RamDiscardManagerClass *rdmc = RAM_DISCARD_MANAGER_GET_CLASS(rdm);
g_assert(rdmc->get_min_granularity);
return rdmc->get_min_granularity(rdm, mr);
}
bool ram_discard_manager_is_populated(const RamDiscardManager *rdm,
const MemoryRegionSection *section)
{
RamDiscardManagerClass *rdmc = RAM_DISCARD_MANAGER_GET_CLASS(rdm);
g_assert(rdmc->is_populated);
return rdmc->is_populated(rdm, section);
}
int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,
MemoryRegionSection *section,
ReplayRamPopulate replay_fn,
void *opaque)
{
RamDiscardManagerClass *rdmc = RAM_DISCARD_MANAGER_GET_CLASS(rdm);
g_assert(rdmc->replay_populated);
return rdmc->replay_populated(rdm, section, replay_fn, opaque);
}
void ram_discard_manager_register_listener(RamDiscardManager *rdm,
RamDiscardListener *rdl,
MemoryRegionSection *section)
{
RamDiscardManagerClass *rdmc = RAM_DISCARD_MANAGER_GET_CLASS(rdm);
g_assert(rdmc->register_listener);
rdmc->register_listener(rdm, rdl, section);
}
void ram_discard_manager_unregister_listener(RamDiscardManager *rdm,
RamDiscardListener *rdl)
{
RamDiscardManagerClass *rdmc = RAM_DISCARD_MANAGER_GET_CLASS(rdm);
g_assert(rdmc->unregister_listener);
rdmc->unregister_listener(rdm, rdl);
}
void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
{
uint8_t mask = 1 << client;
@ -3320,10 +3384,17 @@ static const TypeInfo iommu_memory_region_info = {
.abstract = true,
};
static const TypeInfo ram_discard_manager_info = {
.parent = TYPE_INTERFACE,
.name = TYPE_RAM_DISCARD_MANAGER,
.class_size = sizeof(RamDiscardManagerClass),
};
static void memory_register_types(void)
{
type_register_static(&memory_region_info);
type_register_static(&iommu_memory_region_info);
type_register_static(&ram_discard_manager_info);
}
type_init(memory_register_types)