We want to separate the two cases whereby we discard ram - uncoordinated: e.g., virito-balloon - coordinated: e.g., virtio-mem coordinated via the RamDiscardMgr
Reviewed-by: Pankaj Gupta <pankaj.gu...@cloud.ionos.com> Acked-by: Michael S. Tsirkin <m...@redhat.com> Cc: Paolo Bonzini <pbonz...@redhat.com> Cc: "Michael S. Tsirkin" <m...@redhat.com> Cc: Alex Williamson <alex.william...@redhat.com> Cc: Dr. David Alan Gilbert <dgilb...@redhat.com> Cc: Igor Mammedov <imamm...@redhat.com> Cc: Pankaj Gupta <pankaj.gupta.li...@gmail.com> Cc: Peter Xu <pet...@redhat.com> Cc: Auger Eric <eric.au...@redhat.com> Cc: Wei Yang <richard.weiy...@linux.alibaba.com> Cc: teawater <teawat...@linux.alibaba.com> Cc: Marek Kedzierski <mkedz...@redhat.com> Signed-off-by: David Hildenbrand <da...@redhat.com> --- include/exec/memory.h | 18 +++++++++++++-- softmmu/physmem.c | 54 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 6132910767..fa41c1aee8 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -2818,6 +2818,12 @@ static inline MemOp devend_memop(enum device_endian end) */ int ram_block_discard_disable(bool state); +/* + * See ram_block_discard_disable(): only disable uncoordinated discards, + * keeping coordinated discards (via the RamDiscardMgr) enabled. + */ +int ram_block_uncoordinated_discard_disable(bool state); + /* * Inhibit technologies that disable discarding of pages in RAM blocks. * @@ -2827,12 +2833,20 @@ int ram_block_discard_disable(bool state); int ram_block_discard_require(bool state); /* - * Test if discarding of memory in ram blocks is disabled. + * See ram_block_discard_require(): only inhibit technologies that disable + * uncoordinated discarding of pages in RAM blocks, allowing co-existance with + * technologies that only inhibit uncoordinated discards (via the + * RamDiscardMgr). + */ +int ram_block_coordinated_discard_require(bool state); + +/* + * Test if any discarding of memory in ram blocks is disabled. */ bool ram_block_discard_is_disabled(void); /* - * Test if discarding of memory in ram blocks is required to work reliably. + * Test if any discarding of memory in ram blocks is required to work reliably. */ bool ram_block_discard_is_required(void); diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 6550217c26..19f19ad3a8 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -3677,8 +3677,14 @@ void mtree_print_dispatch(AddressSpaceDispatch *d, MemoryRegion *root) } } +/* Require any discards to work. */ static unsigned int ram_block_discard_requirers; +/* Require only coordinated discards to work. */ +static unsigned int ram_block_coordinated_discard_requirers; +/* Disable any discards. */ static unsigned int ram_block_discard_disablers; +/* Disable only uncoordinated discards. */ +static unsigned int ram_block_uncoordinated_discard_disablers; static QemuMutex ram_block_discard_disable_mutex; static void ram_block_discard_disable_mutex_lock(void) @@ -3704,10 +3710,27 @@ int ram_block_discard_disable(bool state) ram_block_discard_disable_mutex_lock(); if (!state) { ram_block_discard_disablers--; - } else if (!ram_block_discard_requirers) { - ram_block_discard_disablers++; + } else if (ram_block_discard_requirers || + ram_block_coordinated_discard_requirers) { + ret = -EBUSY; } else { + ram_block_discard_disablers++; + } + ram_block_discard_disable_mutex_unlock(); + return ret; +} + +int ram_block_uncoordinated_discard_disable(bool state) +{ + int ret = 0; + + ram_block_discard_disable_mutex_lock(); + if (!state) { + ram_block_uncoordinated_discard_disablers--; + } else if (ram_block_discard_requirers) { ret = -EBUSY; + } else { + ram_block_uncoordinated_discard_disablers++; } ram_block_discard_disable_mutex_unlock(); return ret; @@ -3720,10 +3743,27 @@ int ram_block_discard_require(bool state) ram_block_discard_disable_mutex_lock(); if (!state) { ram_block_discard_requirers--; - } else if (!ram_block_discard_disablers) { - ram_block_discard_requirers++; + } else if (ram_block_discard_disablers || + ram_block_uncoordinated_discard_disablers) { + ret = -EBUSY; } else { + ram_block_discard_requirers++; + } + ram_block_discard_disable_mutex_unlock(); + return ret; +} + +int ram_block_coordinated_discard_require(bool state) +{ + int ret = 0; + + ram_block_discard_disable_mutex_lock(); + if (!state) { + ram_block_coordinated_discard_requirers--; + } else if (ram_block_discard_disablers) { ret = -EBUSY; + } else { + ram_block_coordinated_discard_requirers++; } ram_block_discard_disable_mutex_unlock(); return ret; @@ -3731,10 +3771,12 @@ int ram_block_discard_require(bool state) bool ram_block_discard_is_disabled(void) { - return qatomic_read(&ram_block_discard_disablers); + return qatomic_read(&ram_block_discard_disablers) || + qatomic_read(&ram_block_uncoordinated_discard_disablers); } bool ram_block_discard_is_required(void) { - return qatomic_read(&ram_block_discard_requirers); + return qatomic_read(&ram_block_discard_requirers) || + qatomic_read(&ram_block_coordinated_discard_requirers); } -- 2.29.2