Make RamDiscardManager work with multiple sources
Subsystems (2)
v3 cover Make RamDiscardManager work with multiple sources
From: Marc-André Lureau <[email protected]>
Hi,
This is an attempt to fix the incompatibility of virtio-mem with confidential VMs. The solution implements what was discussed earlier with D. Hildenbrand:
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/#3502238
The first patches are misc cleanups. Then some code refactoring to have split a manager/source. And finally, the manager learns to deal with multiple sources.
I haven't done thorough testing. I only launched a SEV guest with a virtio-mem device. It would be nice to have more tests for those scenarios with VFIO/virtio-mem/confvm.. In any case, review & testing needed!
(should fix https://issues.redhat.com/browse/RHEL-131968)
v3: issues found by Cédric
- fix assertion error on shutdown, due to rcu-defer cleanup
- fix API doc warnings
v2:
- drop replay_{populated,discarded} from source, suggested by Peter Xu
- add extra manager cleanup
- add r-b tags for preliminary patches
thanks
Marc-André Lureau (15):
system/rba: use DIV_ROUND_UP
memory: drop RamDiscardListener::double_discard_supported
virtio-mem: use warn_report_err_once()
system/memory: minor doc fix
kvm: replace RamDicardManager by the RamBlockAttribute system/memory: split RamDiscardManager into source and manager system/memory: move RamDiscardManager to separate compilation unit system/memory: constify section arguments system/ram-discard-manager: implement replay via is_populated
iteration
virtio-mem: remove replay_populated/replay_discarded implementation system/ram-discard-manager: drop replay from source interface system/memory: implement RamDiscardManager multi-source aggregation system/physmem: destroy ram block attributes before RCU-deferred
reclaim
system/memory: add RamDiscardManager reference counting and cleanup
tests: add unit tests for RamDiscardManager multi-source aggregation
include/hw/vfio/vfio-container.h | 2 +- include/hw/vfio/vfio-cpr.h | 2 +- include/hw/virtio/virtio-mem.h | 3 - include/system/memory.h | 287 +---- include/system/ram-discard-manager.h | 358 ++++++ include/system/ramblock.h | 3 +- accel/kvm/kvm-all.c | 2 +- hw/vfio/cpr-legacy.c | 4 +- hw/vfio/listener.c | 12 +- hw/virtio/virtio-mem.c | 290 +---- migration/ram.c | 6 +- system/memory.c | 83 +- system/memory_mapping.c | 4 +- system/physmem.c | 2 +- system/ram-block-attributes.c | 279 +---- system/ram-discard-manager.c | 612 +++++++++ tests/unit/test-ram-discard-manager-stubs.c | 48 + tests/unit/test-ram-discard-manager.c | 1234 +++++++++++++++++++ system/meson.build | 1 + tests/unit/meson.build | 8 +- 20 files changed, 2361 insertions(+), 879 deletions(-) create mode 100644 include/system/ram-discard-manager.h create mode 100644 system/ram-discard-manager.c create mode 100644 tests/unit/test-ram-discard-manager-stubs.c create mode 100644 tests/unit/test-ram-discard-manager.c
Signature · 1 line
2.53.0
PATCH v3 1/15 system/rba: use DIV_ROUND_UP
From: Marc-André Lureau <[email protected]>
Mostly for readability.
--- system/ram-block-attributes.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
system/ram-block-attributes.c +1 -2
@@ -401,8 +401,7 @@ RamBlockAttributes *ram_block_attributes_create(RAMBlock *ram_block)object_unref (OBJECT (attr));return NULL ; } - attr->bitmap_size = -ROUND_UP (int128_get64 (mr->size), block_size) / block_size; + attr->bitmap_size =DIV_ROUND_UP (int128_get64 (mr->size), block_size); attr->bitmap =bitmap_new (attr->bitmap_size);return attr;
Signature · 1 line
2.53.0
Re: [PATCH v3 01/15] system/rba: use DIV_ROUND_UP
Quoted 6 lines · On Thu, Feb 26, 2026 at 02:59:46PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Mostly for readability.
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>
Signature · 1 line
Peter Xu
PATCH v3 2/15 memory: drop RamDiscardListener::double_discard_supported
In reply to [email protected]From: Marc-André Lureau <[email protected]>
This was never turned off, effectively some dead code.
--- include/system/memory.h | 12 +----------- hw/vfio/listener.c | 2 +- hw/virtio/virtio-mem.c | 22 ++-------------------- system/ram-block-attributes.c | 23 +---------------------- 4 files changed, 5 insertions(+), 54 deletions(-)
include/system/memory.h +1 -11
@@ -580,26 +580,16 @@ struct RamDiscardListener {*/ 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) +NotifyRamDiscard discard_fn) { rdl->notify_populate = populate_fn; rdl->notify_discard = discard_fn; - rdl->double_discard_supported = double_discard_supported; }/**
hw/vfio/listener.c +1 -1
@@ -283,7 +283,7 @@ static bool vfio_ram_discard_register_listener(VFIOContainer *bcontainer,ram_discard_listener_init (&vrdl->listener, vfio_ram_discard_notify_populate, - vfio_ram_discard_notify_discard, true); + vfio_ram_discard_notify_discard);ram_discard_manager_register_listener (rdm, &vrdl->listener, section);QLIST_INSERT_HEAD (&bcontainer->vrdl_list, vrdl, next);
hw/virtio/virtio-mem.c +2 -20
@@ -331,14 +331,6 @@ static int virtio_mem_notify_populate_cb(MemoryRegionSection *s, void *arg)return rdl->notify_populate (rdl, s); } -static int virtio_mem_notify_discard_cb (MemoryRegionSection *s,void *arg) -{ -RamDiscardListener *rdl = arg; - - rdl->notify_discard (rdl, s); -return 0 ; -} -static void virtio_mem_notify_unplug (VirtIOMEM *vmem,uint64_t offset,uint64_t size) {
@@ -398,12 +390,7 @@ static void virtio_mem_notify_unplug_all(VirtIOMEM *vmem) } QLIST_FOREACH(rdl, &vmem->rdl_list, next) { -if (rdl->double_discard_supported) { - rdl->notify_discard (rdl, rdl->section); - }else { -virtio_mem_for_each_plugged_section (vmem, rdl->section, rdl, - virtio_mem_notify_discard_cb); - } + rdl->notify_discard (rdl, rdl->section); } }
@@ -1824,12 +1811,7 @@ static void virtio_mem_rdm_unregister_listener(RamDiscardManager *rdm,g_assert (rdl->section->mr == &vmem->memdev->mr);if (vmem->size) { -if (rdl->double_discard_supported) { - rdl->notify_discard (rdl, rdl->section); - }else { -virtio_mem_for_each_plugged_section (vmem, rdl->section, rdl, - virtio_mem_notify_discard_cb); - } + rdl->notify_discard (rdl, rdl->section); }memory_region_section_free_copy (rdl->section);
system/ram-block-attributes.c +1 -22
@@ -61,16 +61,6 @@ ram_block_attributes_notify_populate_cb(MemoryRegionSection *section,return rdl->notify_populate (rdl, section); } -static int -ram_block_attributes_notify_discard_cb (MemoryRegionSection *section, -void *arg) -{ -RamDiscardListener *rdl = arg; - - rdl->notify_discard (rdl, section); -return 0 ; -} - static int ram_block_attributes_for_each_populated_section(const RamBlockAttributes *attr, MemoryRegionSection *section,
@@ -191,22 +181,11 @@ ram_block_attributes_rdm_unregister_listener(RamDiscardManager *rdm, RamDiscardListener *rdl) {RamBlockAttributes *attr =RAM_BLOCK_ATTRIBUTES (rdm); -int ret;g_assert (rdl->section);g_assert (rdl->section->mr == attr->ram_block->mr); -if (rdl->double_discard_supported) { - rdl->notify_discard (rdl, rdl->section); - }else { - ret =ram_block_attributes_for_each_populated_section (attr, - rdl->section, rdl, ram_block_attributes_notify_discard_cb); -if (ret) { -error_report ("%s: Failed to unregister RAM discard listener: %s" , - __func__,strerror (-ret)); -exit (1 ); - } - } + rdl->notify_discard (rdl, rdl->section);memory_region_section_free_copy (rdl->section); rdl->section =NULL ;
Signature · 1 line
2.53.0
Re: [PATCH v3 02/15] memory: drop RamDiscardListener::double_discard_supported
Quoted 7 lines · On 2/26/26 14:59, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
This was never turned off, effectively some dead code.
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>---
Great, thanks!
Signature · 3 lines
Cheers, David
Re: [PATCH v3 02/15] memory: drop RamDiscardListener::double_discard_supported
In reply to [email protected]Quoted 6 lines · On Thu, Feb 26, 2026 at 02:59:47PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
This was never turned off, effectively some dead code.
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>
Signature · 1 line
Peter Xu
PATCH v3 3/15 virtio-mem: use warn_report_err_once()
In reply to [email protected]From: Marc-André Lureau <[email protected]>
--- hw/virtio/virtio-mem.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
hw/virtio/virtio-mem.c +1 -12
@@ -594,18 +594,7 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem, uint64_t start_gpa,Error *local_err =NULL ;if (!qemu_prealloc_mem (fd, area, size,1 ,NULL , false, &local_err)) { -static bool warned; - -/* -* Warn only once, we don't want to fill the log with these -* warnings. -*/ -if (!warned) { -warn_report_err (local_err); - warned = true; - }else { -error_free (local_err); - } +warn_report_err_once (local_err); ret = -EBUSY ; } }
Signature · 1 line
2.53.0
Re: [PATCH v3 03/15] virtio-mem: use warn_report_err_once()
Quoted 22 lines · On 2/26/26 14:59, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>--- hw/virtio/virtio-mem.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)hw/virtio/virtio-mem.c +0
-6@@ -594,18 +594,7 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem, uint64_t start_gpa,Error *local_err =NULL ;if (!qemu_prealloc_mem (fd, area, size,1 ,NULL , false, &local_err)) { -static bool warned; - -/* -* Warn only once, we don't want to fill the log with these -* warnings. -*/
hw/virtio/virtio-mem.c static int virtio_mem_set_block_state(VirtIOMEM *vmem, uint64_t start_gpa, · line 600
Quoted 10 lines
hw/virtio/virtio-mem.c +1
-6-if (!warned) { -warn_report_err (local_err); - warned = true; - }else { -error_free (local_err); - } +warn_report_err_once (local_err); ret = -EBUSY ; } }
Thanks!
Signature · 3 lines
Cheers, David
Re: [PATCH v3 03/15] virtio-mem: use warn_report_err_once()
In reply to [email protected]Quoted 4 lines · On Thu, Feb 26, 2026 at 02:59:48PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>
Signature · 1 line
Peter Xu
PATCH v3 4/15 system/memory: minor doc fix
In reply to [email protected]From: Marc-André Lureau <[email protected]>
--- include/system/memory.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/system/memory.h +1 -1
@@ -574,7 +574,7 @@ struct RamDiscardListener {* new population (e.g., unmap). * * @rdl: the #RamDiscardListener getting notified -* @section: the #MemoryRegionSection to get populated. The section +* @section: the #MemoryRegionSection to get discarded. The section * is aligned within the memory region to the minimum granularity * unless it would exceed the registered section. */
Signature · 1 line
2.53.0
Re: [PATCH v3 04/15] system/memory: minor doc fix
Quoted 5 lines · On 2/26/26 14:59, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>---
Signature · 3 lines
Cheers, David
Re: [PATCH v3 04/15] system/memory: minor doc fix
In reply to [email protected]Quoted 4 lines · On Thu, Feb 26, 2026 at 02:59:49PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>
Signature · 1 line
Peter Xu
PATCH v3 5/15 kvm: replace RamDicardManager by the RamBlockAttribute
In reply to [email protected]From: Marc-André Lureau <[email protected]>
No need to cast through the RamDiscardManager interface, use the RamBlock already retrieved. Makes it more direct and readable, and allow further refactoring to make RamDiscardManager an aggregator object in the following patches.
--- accel/kvm/kvm-all.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
accel/kvm/kvm-all.c +1 -1
@@ -3124,7 +3124,7 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private) addr =memory_region_get_ram_ptr (mr) + section.offset_within_region; rb =qemu_ram_block_from_host (addr, false, &offset); - ret =ram_block_attributes_state_change (RAM_BLOCK_ATTRIBUTES (mr->rdm), + ret =ram_block_attributes_state_change (rb->attributes, offset, size, to_private);if (ret) { error_report("Failed to notify the listener the state change of "
Signature · 1 line
2.53.0
Re: [PATCH v3 05/15] kvm: replace RamDicardManager by the RamBlockAttribute
Quoted 9 lines · On Thu, Feb 26, 2026 at 02:59:50PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
No need to cast through the RamDiscardManager interface, use the RamBlock already retrieved. Makes it more direct and readable, and allow further refactoring to make RamDiscardManager an aggregator object in the following patches.
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH v3 05/15] kvm: replace RamDicardManager by the RamBlockAttribute
In reply to [email protected]Quoted 9 lines · On 2/26/2026 9:59 PM, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
No need to cast through the RamDiscardManager interface, use the RamBlock already retrieved. Makes it more direct and readable, and allow further refactoring to make RamDiscardManager an aggregator object in the following patches.
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>
PATCH v3 6/15 system/memory: split RamDiscardManager into source and manager
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Refactor the RamDiscardManager interface into two distinct components:
- RamDiscardSource: An interface that state providers (virtio-mem, RamBlockAttributes) implement to provide discard state information (granularity, populated/discarded ranges, replay callbacks).
- RamDiscardManager: A concrete QOM object that wraps a source, owns the listener list, and handles listener registration/unregistration and notifications.
This separation moves the listener management logic from individual source implementations into the central RamDiscardManager, reducing code duplication between virtio-mem and RamBlockAttributes.
The change prepares for future work where a RamDiscardManager could aggregate multiple sources.
Note, the original virtio-mem code had conditions before discard:
if (vmem->size) {
rdl->notify_discard(rdl, rdl->section); }
however, the new code calls discard unconditionally. This is considered safe, since the populate/discard of sections are already asymmetrical (unplug & unregister all listener section unconditionally).
--- include/hw/virtio/virtio-mem.h | 3 - include/system/memory.h | 197 ++++++++++++++++------------- include/system/ramblock.h | 3 +- hw/virtio/virtio-mem.c | 163 +++++------------------- system/memory.c | 218 +++++++++++++++++++++++++++++---- system/ram-block-attributes.c | 171 ++++++++------------------ 6 files changed, 386 insertions(+), 369 deletions(-)
include/hw/virtio/virtio-mem.h +0 -3
@@ -118,9 +118,6 @@ struct VirtIOMEM {/* notifiers to notify when "size" changes */ NotifierList size_change_notifiers; -/* listeners to notify on plug/unplug activity. */ - QLIST_HEAD(, RamDiscardListener) rdl_list; -/* Catch system resets -> qemu_devices_reset() only. */ VirtioMemSystemReset *system_reset; };
include/system/memory.h +111 -86
302 lines not on this page · raw message
include/system/ramblock.h +1 -2
@@ -99,11 +99,10 @@ struct RamBlockAttributes {/* 1-setting of the bitmap represents ram is populated (shared) */ unsigned bitmap_size;unsigned long *bitmap; - - QLIST_HEAD(, RamDiscardListener) rdl_list; };/* @offset: the offset within the RAMBlock */ +int ram_block_discard_range (RAMBlock *rb,uint64_t offset,size_t length);/* @offset: the offset within the RAMBlock */ int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t offset,
hw/virtio/virtio-mem.c +32 -131
297 lines not on this page · raw message
system/memory.c +191 -27
279 lines not on this page · raw message
system/ram-block-attributes.c +51 -120
262 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH v3 06/15] system/memory: split RamDiscardManager into source and manager
Quoted 18 lines · On Thu, Feb 26, 2026 at 02:59:51PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Refactor the RamDiscardManager interface into two distinct components:
- RamDiscardSource: An interface that state providers (virtio-mem, RamBlockAttributes) implement to provide discard state information (granularity, populated/discarded ranges, replay callbacks).
- RamDiscardManager: A concrete QOM object that wraps a source, owns the listener list, and handles listener registration/unregistration and notifications.This separation moves the listener management logic from individual source implementations into the central RamDiscardManager, reducing code duplication between virtio-mem and RamBlockAttributes.
The change prepares for future work where a RamDiscardManager could aggregate multiple sources.
Note, the original virtio-mem code had conditions before discard:
Quoted 8 lines
if (vmem->size) { rdl->notify_discard(rdl, rdl->section);}
however, the new code calls discard unconditionally. This is considered safe, since the populate/discard of sections are already asymmetrical (unplug & unregister all listener section unconditionally).Signed-off-by Marc-André Lureau <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH v3 06/15] system/memory: split RamDiscardManager into source and manager
In reply to [email protected]Quoted 35 lines · On 2/26/2026 9:59 PM, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Refactor the RamDiscardManager interface into two distinct components:
- RamDiscardSource: An interface that state providers (virtio-mem, RamBlockAttributes) implement to provide discard state information (granularity, populated/discarded ranges, replay callbacks).
- RamDiscardManager: A concrete QOM object that wraps a source, owns the listener list, and handles listener registration/unregistration and notifications.This separation moves the listener management logic from individual source implementations into the central RamDiscardManager, reducing code duplication between virtio-mem and RamBlockAttributes.
The change prepares for future work where a RamDiscardManager could aggregate multiple sources.
Note, the original virtio-mem code had conditions before discard:
if (vmem->size) { rdl->notify_discard(rdl, rdl->section);}
however, the new code calls discard unconditionally. This is considered safe, since the populate/discard of sections are already asymmetrical (unplug & unregister all listener section unconditionally).Signed-off-by Marc-André Lureau <[email protected]>--- include/hw/virtio/virtio-mem.h | 3 - include/system/memory.h | 197 ++++++++++++++++------------- include/system/ramblock.h | 3 +- hw/virtio/virtio-mem.c | 163 +++++------------------- system/memory.c | 218 +++++++++++++++++++++++++++++---- system/ram-block-attributes.c | 171 ++++++++------------------ 6 files changed, 386 insertions(+), 369 deletions(-)
[...]
Quoted 8 lines
system/memory.c +0
-0@@ -2105,34 +2105,88 @@ RamDiscardManager *memory_region_get_ram_discard_manager(MemoryRegion *mr)return mr->rdm; }
system/memory.c RamDiscardManager *memory_region_get_ram_discard_manager(MemoryRegion *mr) · line 2115
Quoted 10 lines
system/memory.c +8
-2-int memory_region_set_ram_discard_manager (MemoryRegion *mr, -RamDiscardManager *rdm) +static RamDiscardManager *ram_discard_manager_new (MemoryRegion *mr, +RamDiscardSource *rds) +{ +RamDiscardManager *rdm =RAM_DISCARD_MANAGER (object_new (TYPE_RAM_DISCARD_MANAGER )); + + rdm->rds = rds; + rdm->mr = mr; +QLIST_INIT (&rdm->rdl_list);
Is this QLIST_INIT() redundant since it is already called in ram_discard_manager_initfn()?
Quoted 3 lines
system/memory.c +3
-0+return rdm; +} +
[...]
Quoted 53 lines
system/memory.c +50
-2+} + +static void ram_discard_manager_initfn (Object *obj) +{ +RamDiscardManager *rdm =RAM_DISCARD_MANAGER (obj); + +QLIST_INIT (&rdm->rdl_list); +} + +static void ram_discard_manager_finalize (Object *obj) +{ +RamDiscardManager *rdm =RAM_DISCARD_MANAGER (obj); -g_assert (rdmc->replay_discarded); -return rdmc->replay_discarded (rdm, section, replay_fn, opaque); +g_assert (QLIST_EMPTY (&rdm->rdl_list)); +} + +int ram_discard_manager_notify_populate (RamDiscardManager *rdm, +uint64_t offset,uint64_t size) +{ +RamDiscardListener *rdl, *rdl2; +int ret =0 ; + + QLIST_FOREACH(rdl, &rdm->rdl_list, next) { +MemoryRegionSection tmp = *rdl->section; + +if (!memory_region_section_intersect_range (&tmp, offset, size)) { +continue ; + } + ret = rdl->notify_populate (rdl, &tmp); +if (ret) { +break ; + } + } + +if (ret) { +/* Notify all already-notified listeners about discard. */ + QLIST_FOREACH(rdl2, &rdm->rdl_list, next) { +MemoryRegionSection tmp = *rdl2->section; + +if (rdl2 == rdl) { +break ; + } +if (!memory_region_section_intersect_range (&tmp, offset, size)) { +continue ; + } + rdl2->notify_discard (rdl2, &tmp); + } + } +return ret; +} +
[...]
Quoted 14 lines
static int ram_block_attributes_notify_populate(RamBlockAttributes *attr,
uint64_t offset, uint64_t size){
system/memory.c +0
-9-RamDiscardListener *rdl; -int ret =0 ; - - QLIST_FOREACH(rdl, &attr->rdl_list, next) { -MemoryRegionSection tmp = *rdl->section; - -if (!memory_region_section_intersect_range (&tmp, offset, size)) { -continue ; - }
system/memory.c
Quoted 10 lines
system/memory.c +2
-6- ret = rdl->notify_populate (rdl, &tmp); -if (ret) { -break ; - } - } +RamDiscardManager *rdm =memory_region_get_ram_discard_manager (attr->ram_block->mr); -return ret; +return ram_discard_manager_notify_populate (rdm, offset, size); }
This change introduces a slight difference, as it will perform a rollback if ->notify_populate() fails. I believe this is acceptable since, currently, memory conversion failures result in QEMU terminating rather than resuming the guest or retrying the operation. And this rollback is necessary if we plan to support retry operations in the future. Therefore, we can retain this modification.
PATCH v3 7/15 system/memory: move RamDiscardManager to separate compilation unit
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Extract RamDiscardManager and RamDiscardSource from system/memory.c into dedicated a unit.
This reduces coupling and allows code that only needs the RamDiscardManager interface to avoid pulling in all of memory.h dependencies.
--- include/system/memory.h | 280 +------------------------ include/system/ram-discard-manager.h | 297 +++++++++++++++++++++++++++ system/memory.c | 221 -------------------- system/ram-discard-manager.c | 240 ++++++++++++++++++++++ system/meson.build | 1 + 5 files changed, 539 insertions(+), 500 deletions(-) create mode 100644 include/system/ram-discard-manager.h create mode 100644 system/ram-discard-manager.c
include/system/memory.h +1 -279
@@ -16,6 +16,7 @@#include "exec/hwaddr.h" #include "system/ram_addr.h" +#include "system/ram-discard-manager.h" #include "exec/memattrs.h" #include "exec/memop.h" #include "qemu/bswap.h"
@@ -48,18 +49,6 @@ typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass;DECLARE_OBJ_CHECKERS (IOMMUMemoryRegion, IOMMUMemoryRegionClass, IOMMU_MEMORY_REGION, TYPE_IOMMU_MEMORY_REGION) -#define TYPE_RAM_DISCARD_MANAGER "ram-discard-manager" -typedef struct RamDiscardManagerClass RamDiscardManagerClass ; -typedef struct RamDiscardManager RamDiscardManager ; -DECLARE_OBJ_CHECKERS (RamDiscardManager, RamDiscardManagerClass, -RAM_DISCARD_MANAGER ,TYPE_RAM_DISCARD_MANAGER ); - -#define TYPE_RAM_DISCARD_SOURCE "ram-discard-source" -typedef struct RamDiscardSourceClass RamDiscardSourceClass ; -typedef struct RamDiscardSource RamDiscardSource ; -DECLARE_OBJ_CHECKERS (RamDiscardSource, RamDiscardSourceClass, -RAM_DISCARD_SOURCE ,TYPE_RAM_DISCARD_SOURCE ); -#ifdef CONFIG_FUZZ void fuzz_dma_read_cb(size_t addr, size_t len,
@@ -548,273 +537,6 @@ struct IOMMUMemoryRegionClass {int (*num_indexes)(IOMMUMemoryRegion *iommu); }; -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 discarded. The section -* is aligned within the memory region to the minimum granularity -* unless it would exceed the registered section. -*/ -NotifyRamDiscard notify_discard; - -MemoryRegionSection *section; -QLIST_ENTRY (RamDiscardListener) next; -}; - -static inline void ram_discard_listener_init (RamDiscardListener *rdl, -NotifyRamPopulate populate_fn, -NotifyRamDiscard discard_fn) -{ - rdl->notify_populate = populate_fn; - rdl->notify_discard = discard_fn; -} - -/** -* typedef ReplayRamDiscardState: -* -* The callback handler for #RamDiscardSourceClass.replay_populated/ -* #RamDiscardSourceClass.replay_discarded to invoke on populated/discarded -* parts. -* -* @section: the #MemoryRegionSection of populated/discarded part -* @opaque: pointer to forward to the callback -* -* Returns 0 on success, or a negative error if failed. -*/ -typedef int (*ReplayRamDiscardState )(MemoryRegionSection *section, -void *opaque); - -/* -* RamDiscardSourceClass: -* -* A #RamDiscardSource provides information about which parts of a specific -* RAM #MemoryRegion are currently populated (accessible) vs discarded. -* -* This is an interface that state providers (like virtio-mem or -* RamBlockAttributes) implement to provide discard state information. A -* #RamDiscardManager wraps sources and manages listener registrations and -* notifications. -*/ -struct RamDiscardSourceClass { -/* 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 #RamDiscardSource. -* -* @rds: the #RamDiscardSource -* @mr: the #MemoryRegion -* -* Returns the minimum granularity. -*/ -uint64_t (*get_min_granularity)(const RamDiscardSource *rds, -const MemoryRegion *mr); - -/** -* @is_populated: -* -* Check whether the given #MemoryRegionSection is completely populated -* (i.e., no parts are currently discarded) via the #RamDiscardSource. -* There are no alignment requirements. -* -* @rds: the #RamDiscardSource -* @section: the #MemoryRegionSection -* -* Returns whether the given range is completely populated. -*/ -bool (*is_populated)(const RamDiscardSource *rds, -const MemoryRegionSection *section); - -/** -* @replay_populated: -* -* Call the #ReplayRamDiscardState callback for all populated parts within -* the #MemoryRegionSection via the #RamDiscardSource. -* -* In case any call fails, no further calls are made. -* -* @rds: the #RamDiscardSource -* @section: the #MemoryRegionSection -* @replay_fn: the #ReplayRamDiscardState callback -* @opaque: pointer to forward to the callback -* -* Returns 0 on success, or a negative error if any notification failed. -*/ -int (*replay_populated)(const RamDiscardSource *rds, -MemoryRegionSection *section, -ReplayRamDiscardState replay_fn,void *opaque); - -/** -* @replay_discarded: -* -* Call the #ReplayRamDiscardState callback for all discarded parts within -* the #MemoryRegionSection via the #RamDiscardSource. -* -* @rds: the #RamDiscardSource -* @section: the #MemoryRegionSection -* @replay_fn: the #ReplayRamDiscardState callback -* @opaque: pointer to forward to the callback -* -* Returns 0 on success, or a negative error if any notification failed. -*/ -int (*replay_discarded)(const RamDiscardSource *rds, -MemoryRegionSection *section, -ReplayRamDiscardState replay_fn,void *opaque); -}; - -/** -* RamDiscardManager: -* -* 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/accessed by the VM. -* -* A #RamDiscardManager can only be set for a RAM #MemoryRegion while the -* #MemoryRegion isn't mapped into an address space yet (either directly -* or via an alias); it cannot change while the #MemoryRegion is -* mapped into an address space. -* -* 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 #RamDiscardSource 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 -* becoming discarded in a different granularity than it was populated and the -* other way around. -*/ -struct RamDiscardManager { -Object parent; - -RamDiscardSource *rds; -MemoryRegion *mr; - QLIST_HEAD(, RamDiscardListener) rdl_list; -}; - -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); - -/** -* ram_discard_manager_replay_populated: -* -* A wrapper to call the #RamDiscardSourceClass.replay_populated callback -* of the #RamDiscardSource sources. -* -* @rdm: the #RamDiscardManager -* @section: the #MemoryRegionSection -* @replay_fn: the #ReplayRamDiscardState callback -* @opaque: pointer to forward to the callback -* -* Returns 0 on success, or a negative error if any notification failed. -*/ -int ram_discard_manager_replay_populated (const RamDiscardManager *rdm, -MemoryRegionSection *section, -ReplayRamDiscardState replay_fn, -void *opaque); - -/** -* ram_discard_manager_replay_discarded: -* -* A wrapper to call the #RamDiscardSourceClass.replay_discarded callback -* of the #RamDiscardSource sources. -* -* @rdm: the #RamDiscardManager -* @section: the #MemoryRegionSection -* @replay_fn: the #ReplayRamDiscardState callback -* @opaque: pointer to forward to the callback -* -* Returns 0 on success, or a negative error if any notification failed. -*/ -int ram_discard_manager_replay_discarded (const RamDiscardManager *rdm, -MemoryRegionSection *section, -ReplayRamDiscardState 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); - -/* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. -*/ -int ram_discard_manager_notify_populate (RamDiscardManager *rdm, -uint64_t offset,uint64_t size); - -/* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. -*/ -void ram_discard_manager_notify_discard (RamDiscardManager *rdm, -uint64_t offset,uint64_t size); - -/* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. -*/ -void ram_discard_manager_notify_discard_all (RamDiscardManager *rdm); - -/* -* Replay populated sections to all registered listeners. -* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. -*/ -int ram_discard_manager_replay_populated_to_listeners (RamDiscardManager *rdm); -/** * memory_translate_iotlb: Extract addresses from a TLB entry. * Called with rcu_read_lock held.
include/system/ram-discard-manager.h new file +297 -0
297 lines not on this page · raw message
system/memory.c +0 -221
243 lines not on this page · raw message
system/ram-discard-manager.c new file +240 -0
240 lines not on this page · raw message
system/meson.build +1 -0
@@ -18,6 +18,7 @@ system_ss.add(files( 'globals.c', 'ioport.c', 'ram-block-attributes.c', + 'ram-discard-manager.c', 'memory_mapping.c', 'memory.c', 'physmem.c',
Signature · 1 line
2.53.0
Re: [PATCH v3 07/15] system/memory: move RamDiscardManager to separate compilation unit
Quoted 10 lines · On Thu, Feb 26, 2026 at 02:59:52PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Extract RamDiscardManager and RamDiscardSource from
system/memory.cinto dedicated a unit.This reduces coupling and allows code that only needs the RamDiscardManager interface to avoid pulling in all of
memory.hdependencies.Signed-off-by Marc-André Lureau <[email protected]>
Signature · 1 line
Peter Xu
PATCH v3 8/15 system/memory: constify section arguments
In reply to [email protected]From: Marc-André Lureau <[email protected]>
The sections shouldn't be modified.
--- include/hw/vfio/vfio-container.h | 2 +- include/hw/vfio/vfio-cpr.h | 2 +- include/system/ram-discard-manager.h | 14 +++++++------- hw/vfio/cpr-legacy.c | 4 ++-- hw/vfio/listener.c | 10 +++++----- hw/virtio/virtio-mem.c | 10 +++++----- migration/ram.c | 6 +++--- system/memory_mapping.c | 4 ++-- system/ram-block-attributes.c | 8 ++++---- system/ram-discard-manager.c | 10 +++++----- 10 files changed, 35 insertions(+), 35 deletions(-)
include/hw/vfio/vfio-container.h +1 -1
@@ -277,7 +277,7 @@ struct VFIOIOMMUClass { };VFIORamDiscardListener *vfio_find_ram_discard_listener ( -VFIOContainer *bcontainer,MemoryRegionSection *section); +VFIOContainer *bcontainer,const MemoryRegionSection *section);void vfio_container_region_add (VFIOContainer *bcontainer,MemoryRegionSection *section,bool cpr_remap);
include/hw/vfio/vfio-cpr.h +1 -1
@@ -69,7 +69,7 @@ void vfio_cpr_giommu_remap(struct VFIOContainer *bcontainer, MemoryRegionSection *section);bool vfio_cpr_ram_discard_replay_populated ( -struct VFIOContainer *bcontainer,MemoryRegionSection *section); +struct VFIOContainer *bcontainer,const MemoryRegionSection *section);void vfio_cpr_save_vector_fd (struct VFIOPCIDevice *vdev,const char *name,int nr,int fd);
include/system/ram-discard-manager.h +7 -7
@@ -26,9 +26,9 @@ DECLARE_OBJ_CHECKERS(RamDiscardSource, RamDiscardSourceClass,typedef struct RamDiscardListener RamDiscardListener ;typedef int (*NotifyRamPopulate )(RamDiscardListener *rdl, -MemoryRegionSection *section); +const MemoryRegionSection *section);typedef void (*NotifyRamDiscard )(RamDiscardListener *rdl, -MemoryRegionSection *section); +const MemoryRegionSection *section);struct RamDiscardListener {/*
@@ -86,7 +86,7 @@ static inline void ram_discard_listener_init(RamDiscardListener *rdl,* * Returns 0 on success, or a negative error if failed. */ -typedef int (*ReplayRamDiscardState )(MemoryRegionSection *section, +typedef int (*ReplayRamDiscardState )(const MemoryRegionSection *section,void *opaque);/*
@@ -151,7 +151,7 @@ struct RamDiscardSourceClass {* Returns 0 on success, or a negative error if any notification failed. */ int (*replay_populated)(const RamDiscardSource *rds, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque);/**
@@ -168,7 +168,7 @@ struct RamDiscardSourceClass {* Returns 0 on success, or a negative error if any notification failed. */ int (*replay_discarded)(const RamDiscardSource *rds, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque); };
@@ -237,7 +237,7 @@ bool ram_discard_manager_is_populated(const RamDiscardManager *rdm,* Returns 0 on success, or a negative error if any notification failed. */ int ram_discard_manager_replay_populated (const RamDiscardManager *rdm, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque);
@@ -255,7 +255,7 @@ int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,* Returns 0 on success, or a negative error if any notification failed. */ int ram_discard_manager_replay_discarded (const RamDiscardManager *rdm, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque);
hw/vfio/cpr-legacy.c +2 -2
@@ -226,7 +226,7 @@ void vfio_cpr_giommu_remap(VFIOContainer *bcontainer,memory_region_iommu_replay (giommu->iommu_mr, &giommu->n); } -static int vfio_cpr_rdm_remap (MemoryRegionSection *section,void *opaque) +static int vfio_cpr_rdm_remap (const MemoryRegionSection *section,void *opaque) {RamDiscardListener *rdl = opaque;
@@ -242,7 +242,7 @@ static int vfio_cpr_rdm_remap(MemoryRegionSection *section, void *opaque)* directly, which calls vfio_legacy_cpr_dma_map. */ bool vfio_cpr_ram_discard_replay_populated (VFIOContainer *bcontainer, -MemoryRegionSection *section) +const MemoryRegionSection *section) {RamDiscardManager *rdm =memory_region_get_ram_discard_manager (section->mr); VFIORamDiscardListener *vrdl =
hw/vfio/listener.c +5 -5
@@ -203,7 +203,7 @@ out: }static void vfio_ram_discard_notify_discard (RamDiscardListener *rdl, -MemoryRegionSection *section) +const MemoryRegionSection *section) {VFIORamDiscardListener *vrdl =container_of (rdl, VFIORamDiscardListener, listener);
@@ -221,7 +221,7 @@ static void vfio_ram_discard_notify_discard(RamDiscardListener *rdl, }static int vfio_ram_discard_notify_populate (RamDiscardListener *rdl, -MemoryRegionSection *section) +const MemoryRegionSection *section) {VFIORamDiscardListener *vrdl =container_of (rdl, VFIORamDiscardListener, listener);
@@ -465,7 +465,7 @@ static void vfio_device_error_append(VFIODevice *vbasedev, Error **errp) }VFIORamDiscardListener *vfio_find_ram_discard_listener ( -VFIOContainer *bcontainer,MemoryRegionSection *section) +VFIOContainer *bcontainer,const MemoryRegionSection *section) {VFIORamDiscardListener *vrdl =NULL ;
@@ -1147,8 +1147,8 @@ out: } } -static int vfio_ram_discard_query_dirty_bitmap (MemoryRegionSection *section, -void *opaque) +static int vfio_ram_discard_query_dirty_bitmap (const MemoryRegionSection *section, +void *opaque) {const hwaddr size =int128_get64 (section->size);const hwaddr iova = section->offset_within_address_space;
hw/virtio/virtio-mem.c +5 -5
@@ -262,7 +262,7 @@ static int virtio_mem_for_each_plugged_range(VirtIOMEM *vmem, void *arg,typedef int (*virtio_mem_section_cb )(MemoryRegionSection *s,void *arg);static int virtio_mem_for_each_plugged_section (const VirtIOMEM *vmem, -MemoryRegionSection *s, +const MemoryRegionSection *s,void *arg,virtio_mem_section_cb cb) {
@@ -294,7 +294,7 @@ static int virtio_mem_for_each_plugged_section(const VirtIOMEM *vmem, }static int virtio_mem_for_each_unplugged_section (const VirtIOMEM *vmem, -MemoryRegionSection *s, +const MemoryRegionSection *s,void *arg,virtio_mem_section_cb cb) {
@@ -1680,7 +1680,7 @@ static int virtio_mem_rds_replay_cb(MemoryRegionSection *s, void *arg) }static int virtio_mem_rds_replay_populated (const RamDiscardSource *rds, -MemoryRegionSection *s, +const MemoryRegionSection *s,ReplayRamDiscardState replay_fn,void *opaque) {
@@ -1692,11 +1692,11 @@ static int virtio_mem_rds_replay_populated(const RamDiscardSource *rds,g_assert (s->mr == &vmem->memdev->mr);return virtio_mem_for_each_plugged_section (vmem, s, &data, - virtio_mem_rds_replay_cb); + virtio_mem_rds_replay_cb); }static int virtio_mem_rds_replay_discarded (const RamDiscardSource *rds, -MemoryRegionSection *s, +const MemoryRegionSection *s,ReplayRamDiscardState replay_fn,void *opaque) {
migration/ram.c +3 -3
@@ -860,7 +860,7 @@ static inline bool migration_bitmap_clear_dirty(RAMState *rs,return ret; } -static int dirty_bitmap_clear_section (MemoryRegionSection *section, +static int dirty_bitmap_clear_section (const MemoryRegionSection *section,void *opaque) {const hwaddr offset = section->offset_within_region;
@@ -1595,7 +1595,7 @@ static inline void populate_read_range(RAMBlock *block, ram_addr_t offset, } } -static inline int populate_read_section (MemoryRegionSection *section, +static inline int populate_read_section (const MemoryRegionSection *section,void *opaque) {const hwaddr size =int128_get64 (section->size);
@@ -1670,7 +1670,7 @@ void ram_write_tracking_prepare(void) } } -static inline int uffd_protect_section (MemoryRegionSection *section, +static inline int uffd_protect_section (const MemoryRegionSection *section,void *opaque) {const hwaddr size =int128_get64 (section->size);
system/memory_mapping.c +2 -2
@@ -196,7 +196,7 @@ typedef struct GuestPhysListener { } GuestPhysListener;static void guest_phys_block_add_section (GuestPhysListener *g, -MemoryRegionSection *section) +const MemoryRegionSection *section) {const hwaddr target_start = section->offset_within_address_space;const hwaddr target_end = target_start +int128_get64 (section->size);
@@ -248,7 +248,7 @@ static void guest_phys_block_add_section(GuestPhysListener *g,#endif } -static int guest_phys_ram_populate_cb (MemoryRegionSection *section, +static int guest_phys_ram_populate_cb (const MemoryRegionSection *section,void *opaque) {GuestPhysListener *g = opaque;
system/ram-block-attributes.c +4 -4
@@ -37,7 +37,7 @@ typedef int (*ram_block_attributes_section_cb)(MemoryRegionSection *s,static int ram_block_attributes_for_each_populated_section (const RamBlockAttributes *attr, -MemoryRegionSection *section, +const MemoryRegionSection *section,void *arg,ram_block_attributes_section_cb cb) {
@@ -78,7 +78,7 @@ ram_block_attributes_for_each_populated_section(const RamBlockAttributes *attr,static int ram_block_attributes_for_each_discarded_section (const RamBlockAttributes *attr, -MemoryRegionSection *section, +const MemoryRegionSection *section,void *arg,ram_block_attributes_section_cb cb) {
@@ -161,7 +161,7 @@ ram_block_attributes_rds_is_populated(const RamDiscardSource *rds,static int ram_block_attributes_rds_replay_populated (const RamDiscardSource *rds, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque) {
@@ -175,7 +175,7 @@ ram_block_attributes_rds_replay_populated(const RamDiscardSource *rds,static int ram_block_attributes_rds_replay_discarded (const RamDiscardSource *rds, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque) {
system/ram-discard-manager.c +5 -5
@@ -28,7 +28,7 @@ static bool ram_discard_source_is_populated(const RamDiscardSource *rds, }static int ram_discard_source_replay_populated (const RamDiscardSource *rds, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque) {
@@ -39,7 +39,7 @@ static int ram_discard_source_replay_populated(const RamDiscardSource *rds, }static int ram_discard_source_replay_discarded (const RamDiscardSource *rds, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque) {
@@ -74,7 +74,7 @@ bool ram_discard_manager_is_populated(const RamDiscardManager *rdm, }int ram_discard_manager_replay_populated (const RamDiscardManager *rdm, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque) {
@@ -83,7 +83,7 @@ int ram_discard_manager_replay_populated(const RamDiscardManager *rdm, }int ram_discard_manager_replay_discarded (const RamDiscardManager *rdm, -MemoryRegionSection *section, +const MemoryRegionSection *section,ReplayRamDiscardState replay_fn,void *opaque) {
@@ -164,7 +164,7 @@ void ram_discard_manager_notify_discard_all(RamDiscardManager *rdm) } } -static int rdm_populate_cb (MemoryRegionSection *section,void *opaque) +static int rdm_populate_cb (const MemoryRegionSection *section,void *opaque) {RamDiscardListener *rdl = opaque;
Signature · 1 line
2.53.0
Re: [PATCH v3 08/15] system/memory: constify section arguments
Quoted 6 lines · On Thu, Feb 26, 2026 at 02:59:53PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
The sections shouldn't be modified.
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>
Signature · 1 line
Peter Xu
PATCH v3 9/15 system/ram-discard-manager: implement replay via is_populated iteration
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Replace the source-level replay wrappers with a new replay_by_populated_state() helper that iterates the section at min-granularity, calls is_populated() for each chunk, and aggregates consecutive chunks of the same state before invoking the callback.
This moves the iteration logic from individual sources into the manager, preparing for multi-source aggregation where the manager must combine state from multiple sources anyway.
The replay_populated/replay_discarded vtable entries in RamDiscardSourceClass are no longer called but remain in the interface for now; they will be removed in follow-up commits along with the now-dead source implementations.
--- system/ram-discard-manager.c | 85 ++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 24 deletions(-)
system/ram-discard-manager.c +61 -24
@@ -27,26 +27,65 @@ static bool ram_discard_source_is_populated(const RamDiscardSource *rds,return rdsc->is_populated (rds, section); } -static int ram_discard_source_replay_populated (const RamDiscardSource *rds, -const MemoryRegionSection *section, -ReplayRamDiscardState replay_fn, -void *opaque) +/* +* Iterate the section at source granularity, aggregating consecutive chunks +* with matching populated state, and call replay_fn for each run. +*/ +static int replay_by_populated_state (const RamDiscardManager *rdm, +const MemoryRegionSection *section, +bool replay_populated, +ReplayRamDiscardState replay_fn, +void *opaque) { -RamDiscardSourceClass *rdsc =RAM_DISCARD_SOURCE_GET_CLASS (rds); +uint64_t granularity, offset, size, end, pos, run_start; +bool in_run = false; +int ret =0 ; -g_assert (rdsc->replay_populated); -return rdsc->replay_populated (rds, section, replay_fn, opaque); -} + granularity =ram_discard_source_get_min_granularity (rdm->rds, rdm->mr); + offset = section->offset_within_region; + size =int128_get64 (section->size); + end = offset + size; + +/* Align iteration to granularity boundaries */ + pos =QEMU_ALIGN_DOWN (offset, granularity); + +for (; pos < end; pos += granularity) { +MemoryRegionSection chunk = { + .mr = section->mr, + .offset_within_region = pos, + .size =int128_make64 (granularity), + }; +bool populated =ram_discard_source_is_populated (rdm->rds, &chunk); + +if (populated == replay_populated) { +if (!in_run) { + run_start = pos; + in_run = true; + } + }else if (in_run) { +MemoryRegionSection tmp = *section; + +if (memory_region_section_intersect_range (&tmp, run_start, + pos - run_start)) { + ret =replay_fn (&tmp, opaque); +if (ret) { +return ret; + } + } + in_run = false; + } + } -static int ram_discard_source_replay_discarded (const RamDiscardSource *rds, -const MemoryRegionSection *section, -ReplayRamDiscardState replay_fn, -void *opaque) -{ -RamDiscardSourceClass *rdsc =RAM_DISCARD_SOURCE_GET_CLASS (rds); +if (in_run) { +MemoryRegionSection tmp = *section; -g_assert (rdsc->replay_discarded); -return rdsc->replay_discarded (rds, section, replay_fn, opaque); +if (memory_region_section_intersect_range (&tmp, run_start, + pos - run_start)) { + ret =replay_fn (&tmp, opaque); + } + } + +return ret; } RamDiscardManager *ram_discard_manager_new(MemoryRegion *mr,
@@ -78,8 +117,7 @@ int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,ReplayRamDiscardState replay_fn, void *opaque) { - return ram_discard_source_replay_populated(rdm->rds, section, -replay_fn ,opaque ); + return replay_by_populated_state(rdm, section, true, replay_fn, opaque); } int ram_discard_manager_replay_discarded(const RamDiscardManager *rdm,
@@ -87,8 +125,7 @@ int ram_discard_manager_replay_discarded(const RamDiscardManager *rdm,ReplayRamDiscardState replay_fn, void *opaque) { - return ram_discard_source_replay_discarded(rdm->rds, section, -replay_fn ,opaque ); + return replay_by_populated_state(rdm, section, false, replay_fn, opaque); }static void ram_discard_manager_initfn (Object *obj)
@@ -182,8 +219,8 @@ void ram_discard_manager_register_listener(RamDiscardManager *rdm, rdl->section =memory_region_section_new_copy (section);QLIST_INSERT_HEAD (&rdm->rdl_list, rdl, next); - ret =ram_discard_source_replay_populated (rdm->rds, rdl->section, - rdm_populate_cb, rdl); + ret =ram_discard_manager_replay_populated (rdm, rdl->section, + rdm_populate_cb, rdl);if (ret) {error_report ("%s: Replaying populated ranges failed: %s" , __func__,strerror (-ret));
@@ -208,8 +245,8 @@ int ram_discard_manager_replay_populated_to_listeners(RamDiscardManager *rdm)int ret =0 ; QLIST_FOREACH(rdl, &rdm->rdl_list, next) { - ret =ram_discard_source_replay_populated (rdm->rds, rdl->section, - rdm_populate_cb, rdl); + ret =ram_discard_manager_replay_populated (rdm, rdl->section, + rdm_populate_cb, rdl);if (ret) {break ; }
Signature · 1 line
2.53.0
Re: [PATCH v3 09/15] system/ram-discard-manager: implement replay via is_populated iteration
Quoted 11 lines · On 2/26/26 14:59, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Replace the source-level replay wrappers with a new
replay_by_populated_state()helper that iterates the section at min-granularity, callsis_populated()for each chunk, and aggregates consecutive chunks of the same state before invoking the callback.This moves the iteration logic from individual sources into the manager, preparing for multi-source aggregation where the manager must combine state from multiple sources anyway.
Quoted 7 lines
The replay_populated/replay_discarded vtable entries in RamDiscardSourceClass are no longer called but remain in the interface for now; they will be removed in follow-up commits along with the now-dead source implementations.
Signed-off-by Marc-André Lureau <[email protected]>---
Isn't it significantly more expensive for large guests to possibly iterate in 4k granularity?
The nice thing about the old implementation was that we could just scan a bitmap, that is ideally in 2M granularity.
Signature · 3 lines
Cheers, David
Re: [PATCH v3 09/15] system/ram-discard-manager: implement replay via is_populated iteration
Hi
Quoted 18 lines · David Hildenbrand
Quoted 16 lines · On 2/26/26 14:59, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Replace the source-level replay wrappers with a new
replay_by_populated_state()helper that iterates the section at min-granularity, callsis_populated()for each chunk, and aggregates consecutive chunks of the same state before invoking the callback.This moves the iteration logic from individual sources into the manager, preparing for multi-source aggregation where the manager must combine state from multiple sources anyway.
The replay_populated/replay_discarded vtable entries in RamDiscardSourceClass are no longer called but remain in the interface for now; they will be removed in follow-up commits along with the now-dead source implementations.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Marc-André Lureau <[email protected]>---
Isn't it significantly more expensive for large guests to possibly iterate in 4k granularity?
It's a bit hard to say. How likely is it to have a very large RamDiscardManager with a min-granularity of 4k?
Quoted 3 lines
The nice thing about the old implementation was that we could just scan a bitmap, that is ideally in 2M granularity.
We can still iterate with 2M granularity after this patch. However, it may be less effective than iterating using find_next_bit(). Whether this is noticeable remains to be seen.
Note: this change was a suggestion from Peter in v1, as discussed in
https://patchew.org/QEMU/[email protected]/[email protected]/#[email protected]
Re: [PATCH v3 09/15] system/ram-discard-manager: implement replay via is_populated iteration
Quoted 3 lines · Marc-André Lureau
We can still iterate with 2M granularity after this patch. However, it may be less effective than iterating using
find_next_bit(). Whether this is noticeable remains to be seen.
IIUC when in extreme scattered cases the results should be similar, say, when the bitmap (2M for each bit) is 10101010....10101b. OTOH the worst case scenario here is when e.g. the whole region is fully populated.
So.. if we have any concern, maybe we could run a VFIO population test on non-CoCo, before or after this patch, having the whole region requested available (so a bitmap with 111111111....111111b).
Thanks,
Signature · 1 line
Peter Xu
PATCH v3 10/15 virtio-mem: remove replay_populated/replay_discarded implementation
In reply to [email protected]From: Marc-André Lureau <[email protected]>
The replay iteration logic has been moved into the RamDiscardManager, which now iterates at source granularity using is_populated(). The source-level replay_populated/replay_discarded methods and their helpers are no longer called.
Remove the now-dead replay methods, the VirtIOMEMReplayData struct, the virtio_mem_for_each_plugged/unplugged_section() helpers (only used by the replay methods), and the virtio_mem_section_cb typedef.
--- hw/virtio/virtio-mem.c | 112 ----------------------------------------- 1 file changed, 112 deletions(-)
hw/virtio/virtio-mem.c +0 -112
@@ -259,72 +259,6 @@ static int virtio_mem_for_each_plugged_range(VirtIOMEM *vmem, void *arg,return ret; } -typedef int (*virtio_mem_section_cb )(MemoryRegionSection *s,void *arg); - -static int virtio_mem_for_each_plugged_section (const VirtIOMEM *vmem, -const MemoryRegionSection *s, -void *arg, -virtio_mem_section_cb cb) -{ -unsigned long first_bit, last_bit; -uint64_t offset, size; -int ret =0 ; - - first_bit = s->offset_within_region / vmem->block_size; - first_bit =find_next_bit (vmem->bitmap, vmem->bitmap_size, first_bit); -while (first_bit < vmem->bitmap_size) { -MemoryRegionSection tmp = *s; - - offset = first_bit * vmem->block_size; - last_bit =find_next_zero_bit (vmem->bitmap, vmem->bitmap_size, - first_bit +1 ) -1 ; - size = (last_bit - first_bit +1 ) * vmem->block_size; - -if (!memory_region_section_intersect_range (&tmp, offset, size)) { -break ; - } - ret =cb (&tmp, arg); -if (ret) { -break ; - } - first_bit =find_next_bit (vmem->bitmap, vmem->bitmap_size, - last_bit +2 ); - } -return ret; -} - -static int virtio_mem_for_each_unplugged_section (const VirtIOMEM *vmem, -const MemoryRegionSection *s, -void *arg, -virtio_mem_section_cb cb) -{ -unsigned long first_bit, last_bit; -uint64_t offset, size; -int ret =0 ; - - first_bit = s->offset_within_region / vmem->block_size; - first_bit =find_next_zero_bit (vmem->bitmap, vmem->bitmap_size, first_bit); -while (first_bit < vmem->bitmap_size) { -MemoryRegionSection tmp = *s; - - offset = first_bit * vmem->block_size; - last_bit =find_next_bit (vmem->bitmap, vmem->bitmap_size, - first_bit +1 ) -1 ; - size = (last_bit - first_bit +1 ) * vmem->block_size; - -if (!memory_region_section_intersect_range (&tmp, offset, size)) { -break ; - } - ret =cb (&tmp, arg); -if (ret) { -break ; - } - first_bit =find_next_zero_bit (vmem->bitmap, vmem->bitmap_size, - last_bit +2 ); - } -return ret; -} -static void virtio_mem_notify_unplug (VirtIOMEM *vmem,uint64_t offset,uint64_t size) {
@@ -1667,50 +1601,6 @@ static bool virtio_mem_rds_is_populated(const RamDiscardSource *rds,return virtio_mem_is_range_plugged (vmem, start_gpa, end_gpa - start_gpa); } -struct VirtIOMEMReplayData { -ReplayRamDiscardState fn; -void *opaque; -}; - -static int virtio_mem_rds_replay_cb (MemoryRegionSection *s,void *arg) -{ -struct VirtIOMEMReplayData *data = arg; - -return data->fn (s, data->opaque); -} - -static int virtio_mem_rds_replay_populated (const RamDiscardSource *rds, -const MemoryRegionSection *s, -ReplayRamDiscardState replay_fn, -void *opaque) -{ -const VirtIOMEM *vmem =VIRTIO_MEM (rds); -struct VirtIOMEMReplayData data = { - .fn = replay_fn, - .opaque = opaque, - }; - -g_assert (s->mr == &vmem->memdev->mr); -return virtio_mem_for_each_plugged_section (vmem, s, &data, - virtio_mem_rds_replay_cb); -} - -static int virtio_mem_rds_replay_discarded (const RamDiscardSource *rds, -const MemoryRegionSection *s, -ReplayRamDiscardState replay_fn, -void *opaque) -{ -const VirtIOMEM *vmem =VIRTIO_MEM (rds); -struct VirtIOMEMReplayData data = { - .fn = replay_fn, - .opaque = opaque, - }; - -g_assert (s->mr == &vmem->memdev->mr); -return virtio_mem_for_each_unplugged_section (vmem, s, &data, - virtio_mem_rds_replay_cb); -} -static void virtio_mem_unplug_request_check (VirtIOMEM *vmem,Error **errp) {if (vmem->unplugged_inaccessible ==ON_OFF_AUTO_OFF ) {
@@ -1766,8 +1656,6 @@ static void virtio_mem_class_init(ObjectClass *klass, const void *data) rdsc->get_min_granularity = virtio_mem_rds_get_min_granularity; rdsc->is_populated = virtio_mem_rds_is_populated; - rdsc->replay_populated = virtio_mem_rds_replay_populated; - rdsc->replay_discarded = virtio_mem_rds_replay_discarded; }static const TypeInfo virtio_mem_info = {
Signature · 1 line
2.53.0
PATCH v3 11/15 system/ram-discard-manager: drop replay from source interface
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Remove replay_populated and replay_discarded from RamDiscardSourceClass now that the RamDiscardManager handles replay iteration internally via is_populated.
Remove the now-dead replay methods, helpers, and for_each_populated/discarded_section() from ram-block-attributes, which was the last source still carrying this code.
--- include/system/ram-discard-manager.h | 52 +++-------- system/ram-block-attributes.c | 130 --------------------------- 2 files changed, 10 insertions(+), 172 deletions(-)
include/system/ram-discard-manager.h +10 -42
@@ -77,8 +77,8 @@ static inline void ram_discard_listener_init(RamDiscardListener *rdl,/** * typedef ReplayRamDiscardState: * -* The callback handler for #RamDiscardSourceClass.replay_populated/ -* #RamDiscardSourceClass.replay_discarded to invoke on populated/discarded +* The callback handler used by ram_discard_manager_replay_populated() and +* ram_discard_manager_replay_discarded() to invoke on populated/discarded * parts. * * @section: the #MemoryRegionSection of populated/discarded part
@@ -134,42 +134,6 @@ struct RamDiscardSourceClass {*/ bool (*is_populated)(const RamDiscardSource *rds,const MemoryRegionSection *section); - -/** -* @replay_populated: -* -* Call the #ReplayRamDiscardState callback for all populated parts within -* the #MemoryRegionSection via the #RamDiscardSource. -* -* In case any call fails, no further calls are made. -* -* @rds: the #RamDiscardSource -* @section: the #MemoryRegionSection -* @replay_fn: the #ReplayRamDiscardState callback -* @opaque: pointer to forward to the callback -* -* Returns 0 on success, or a negative error if any notification failed. -*/ -int (*replay_populated)(const RamDiscardSource *rds, -const MemoryRegionSection *section, -ReplayRamDiscardState replay_fn,void *opaque); - -/** -* @replay_discarded: -* -* Call the #ReplayRamDiscardState callback for all discarded parts within -* the #MemoryRegionSection via the #RamDiscardSource. -* -* @rds: the #RamDiscardSource -* @section: the #MemoryRegionSection -* @replay_fn: the #ReplayRamDiscardState callback -* @opaque: pointer to forward to the callback -* -* Returns 0 on success, or a negative error if any notification failed. -*/ -int (*replay_discarded)(const RamDiscardSource *rds, -const MemoryRegionSection *section, -ReplayRamDiscardState replay_fn,void *opaque); };/**
@@ -226,8 +190,10 @@ bool ram_discard_manager_is_populated(const RamDiscardManager *rdm,/** * ram_discard_manager_replay_populated: * -* A wrapper to call the #RamDiscardSourceClass.replay_populated callback -* of the #RamDiscardSource sources. +* Iterate the given #MemoryRegionSection at minimum granularity, calling +* #RamDiscardSourceClass.is_populated for each chunk, and invoke @replay_fn +* for each contiguous populated range. In case any call fails, no further +* calls are made. * * @rdm: the #RamDiscardManager * @section: the #MemoryRegionSection
@@ -244,8 +210,10 @@ int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,/** * ram_discard_manager_replay_discarded: * -* A wrapper to call the #RamDiscardSourceClass.replay_discarded callback -* of the #RamDiscardSource sources. +* Iterate the given #MemoryRegionSection at minimum granularity, calling +* #RamDiscardSourceClass.is_populated for each chunk, and invoke @replay_fn +* for each contiguous discarded range. In case any call fails, no further +* calls are made. * * @rdm: the #RamDiscardManager * @section: the #MemoryRegionSection
system/ram-block-attributes.c +0 -130
146 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v3 12/15 system/memory: implement RamDiscardManager multi-source aggregation
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Refactor RamDiscardManager to aggregate multiple RamDiscardSource instances. This enables scenarios where multiple components (e.g., virtio-mem and RamBlockAttributes) can coordinate memory discard state for the same memory region.
The aggregation uses:
- Populated: ALL sources populated
- Discarded: ANY source discarded
When a source is added with existing listeners, they are notified about regions that become discarded. When a source is removed, listeners are notified about regions that become populated.
--- include/system/ram-discard-manager.h | 143 +++++++-- hw/virtio/virtio-mem.c | 8 +- system/memory.c | 15 +- system/ram-block-attributes.c | 6 +- system/ram-discard-manager.c | 427 ++++++++++++++++++++++++--- 5 files changed, 515 insertions(+), 84 deletions(-)
include/system/ram-discard-manager.h +118 -25
@@ -170,30 +170,96 @@ struct RamDiscardSourceClass {* becoming discarded in a different granularity than it was populated and the * other way around. */ + +typedef struct RamDiscardSourceEntry RamDiscardSourceEntry ; + +struct RamDiscardSourceEntry { +RamDiscardSource *rds; +QLIST_ENTRY (RamDiscardSourceEntry) next; +}; +struct RamDiscardManager {Object parent; -RamDiscardSource *rds; -MemoryRegion *mr; +struct MemoryRegion *mr; + QLIST_HEAD(, RamDiscardSourceEntry) source_list; +uint64_t min_granularity; QLIST_HEAD(, RamDiscardListener) rdl_list; }; -RamDiscardManager *ram_discard_manager_new (MemoryRegion *mr, -RamDiscardSource *rds); +RamDiscardManager *ram_discard_manager_new (MemoryRegion *mr); + +/** +* ram_discard_manager_add_source: +* +* Register a #RamDiscardSource with the #RamDiscardManager. The manager +* aggregates state from all registered sources using AND semantics: a region +* is considered populated only if ALL sources report it as populated. +* +* If listeners are already registered, they will be notified about any +* regions that become discarded due to adding this source. Specifically, +* for each region that the new source reports as discarded, if all other +* sources reported it as populated, listeners receive a discard notification. +* +* If any listener rejects the notification (returns an error), previously +* notified listeners are rolled back with populate notifications and the +* source is not added. +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource to add +* +* Returns: 0 on success, -EBUSY if @source is already registered, or a +* negative error code if a listener rejected the state change. +*/ +int ram_discard_manager_add_source (RamDiscardManager *rdm, +RamDiscardSource *source); + +/** +* ram_discard_manager_del_source: +* +* Unregister a #RamDiscardSource from the #RamDiscardManager. +* +* If listeners are already registered, they will be notified about any +* regions that become populated due to removing this source. Specifically, +* for each region that the removed source reported as discarded, if all +* remaining sources report it as populated, listeners receive a populate +* notification. +* +* If any listener rejects the notification (returns an error), previously +* notified listeners are rolled back with discard notifications and the +* source is not removed. +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource to remove +* +* Returns: 0 on success, -ENOENT if @source is not registered, or a +* negative error code if a listener rejected the state change. +*/ +int ram_discard_manager_del_source (RamDiscardManager *rdm, +RamDiscardSource *source); +uint64_t ram_discard_manager_get_min_granularity (const RamDiscardManager *rdm,const MemoryRegion *mr); +/** +* ram_discard_manager_is_populated: +* +* Check if the given memory region section is populated. +* If the manager has no sources, it is considered populated. +* +* @rdm: the #RamDiscardManager +* @section: the #MemoryRegionSection to check +* +* Returns: true if the section is populated, false otherwise. +*/ bool ram_discard_manager_is_populated (const RamDiscardManager *rdm,const MemoryRegionSection *section);/** * ram_discard_manager_replay_populated: * -* Iterate the given #MemoryRegionSection at minimum granularity, calling -* #RamDiscardSourceClass.is_populated for each chunk, and invoke @replay_fn -* for each contiguous populated range. In case any call fails, no further -* calls are made. +* Call @replay_fn on regions that are populated in all sources. * * @rdm: the #RamDiscardManager * @section: the #MemoryRegionSection
@@ -210,10 +276,7 @@ int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,/** * ram_discard_manager_replay_discarded: * -* Iterate the given #MemoryRegionSection at minimum granularity, calling -* #RamDiscardSourceClass.is_populated for each chunk, and invoke @replay_fn -* for each contiguous discarded range. In case any call fails, no further -* calls are made. +* Call @replay_fn on regions that are discarded in any sources. * * @rdm: the #RamDiscardManager * @section: the #MemoryRegionSection
@@ -234,31 +297,61 @@ void ram_discard_manager_register_listener(RamDiscardManager *rdm,void ram_discard_manager_unregister_listener (RamDiscardManager *rdm,RamDiscardListener *rdl); -/* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. +/** +* ram_discard_manager_notify_populate: +* +* Notify listeners that a region is about to be populated by a source. +* For multi-source aggregation, only notifies when all sources agree +* the region is populated (intersection). +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource that is populating +* @offset: offset within the memory region +* @size: size of the region being populated +* +* Returns 0 on success, or a negative error if any listener rejects. */ int ram_discard_manager_notify_populate (RamDiscardManager *rdm, +RamDiscardSource *source,uint64_t offset,uint64_t size); -/* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. +/** +* ram_discard_manager_notify_discard: +* +* Notify listeners that a region has been discarded by a source. +* For multi-source aggregation, always notifies immediately +* (union semantics - any source discarding makes region discarded). +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource that is discarding +* @offset: offset within the memory region +* @size: size of the region being discarded */ void ram_discard_manager_notify_discard (RamDiscardManager *rdm, +RamDiscardSource *source,uint64_t offset,uint64_t size); -/* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. +/** +* ram_discard_manager_notify_discard_all: +* +* Notify listeners that all regions have been discarded by a source. +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource that is discarding */ -void ram_discard_manager_notify_discard_all (RamDiscardManager *rdm); +void ram_discard_manager_notify_discard_all (RamDiscardManager *rdm, +RamDiscardSource *source); -/* +/** +* ram_discard_manager_replay_populated_to_listeners: +* * Replay populated sections to all registered listeners. +* For multi-source aggregation, only replays regions where all sources +* are populated (intersection). * -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. +* @rdm: the #RamDiscardManager +* +* Returns 0 on success, or a negative error if any notification failed. */ int ram_discard_manager_replay_populated_to_listeners (RamDiscardManager *rdm);
hw/virtio/virtio-mem.c +5 -3
@@ -264,7 +264,8 @@ static void virtio_mem_notify_unplug(VirtIOMEM *vmem, uint64_t offset, {RamDiscardManager *rdm =memory_region_get_ram_discard_manager (&vmem->memdev->mr); -ram_discard_manager_notify_discard (rdm, offset, size); +ram_discard_manager_notify_discard (rdm,RAM_DISCARD_SOURCE (vmem), + offset, size); } static int virtio_mem_notify_plug(VirtIOMEM *vmem, uint64_t offset,
@@ -272,7 +273,8 @@ static int virtio_mem_notify_plug(VirtIOMEM *vmem, uint64_t offset, {RamDiscardManager *rdm =memory_region_get_ram_discard_manager (&vmem->memdev->mr); -return ram_discard_manager_notify_populate (rdm, offset, size); +return ram_discard_manager_notify_populate (rdm,RAM_DISCARD_SOURCE (vmem), + offset, size); }static void virtio_mem_notify_unplug_all (VirtIOMEM *vmem)
@@ -283,7 +285,7 @@ static void virtio_mem_notify_unplug_all(VirtIOMEM *vmem)return ; } -ram_discard_manager_notify_discard_all (rdm); +ram_discard_manager_notify_discard_all (rdm,RAM_DISCARD_SOURCE (vmem)); } static bool virtio_mem_is_range_plugged(const VirtIOMEM *vmem,
system/memory.c +8 -7
@@ -2109,21 +2109,22 @@ int memory_region_add_ram_discard_source(MemoryRegion *mr, RamDiscardSource *source) {g_assert (memory_region_is_ram (mr)); -if (mr->rdm) { -return -EBUSY ; + +if (!mr->rdm) { + mr->rdm =ram_discard_manager_new (mr); } - mr->rdm =ram_discard_manager_new (mr,RAM_DISCARD_SOURCE (source)); -return 0 ; +return ram_discard_manager_add_source (mr->rdm, source); }void memory_region_del_ram_discard_source (MemoryRegion *mr,RamDiscardSource *source) { -g_assert (mr->rdm->rds == source); +g_assert (mr->rdm); + +ram_discard_manager_del_source (mr->rdm, source); -object_unref (mr->rdm); - mr->rdm =NULL ; +/* if there is no source and no listener left, we could free rdm */ }/* Called with rcu_read_lock held. */
system/ram-block-attributes.c +4 -2
@@ -90,7 +90,8 @@ ram_block_attributes_notify_discard(RamBlockAttributes *attr, {RamDiscardManager *rdm =memory_region_get_ram_discard_manager (attr->ram_block->mr); -ram_discard_manager_notify_discard (rdm, offset, size); +ram_discard_manager_notify_discard (rdm,RAM_DISCARD_SOURCE (attr), + offset, size); } static int
@@ -99,7 +100,8 @@ ram_block_attributes_notify_populate(RamBlockAttributes *attr, {RamDiscardManager *rdm =memory_region_get_ram_discard_manager (attr->ram_block->mr); -return ram_discard_manager_notify_populate (rdm, offset, size); +return ram_discard_manager_notify_populate (rdm,RAM_DISCARD_SOURCE (attr), + offset, size); } int ram_block_attributes_state_change(RamBlockAttributes *attr,
system/ram-discard-manager.c +380 -47
550 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH v3 12/15] system/memory: implement RamDiscardManager multi-source aggregation
Quoted 11 lines · On Thu, Feb 26, 2026 at 02:59:57PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Refactor RamDiscardManager to aggregate multiple RamDiscardSource instances. This enables scenarios where multiple components (e.g., virtio-mem and RamBlockAttributes) can coordinate memory discard state for the same memory region.
The aggregation uses:
- Populated: ALL sources populated
- Discarded: ANY source discarded
Quoted 5 lines
When a source is added with existing listeners, they are notified about regions that become discarded. When a source is removed, listeners are notified about regions that become populated.
Signed-off-by Marc-André Lureau <[email protected]>
Looks nice to me in general, feel free to take:
Only some nitpicks and pure comments below.
Quoted 20 lines
--- include/system/ram-discard-manager.h | 143 +++++++-- hw/virtio/virtio-mem.c | 8 +- system/memory.c | 15 +- system/ram-block-attributes.c | 6 +- system/ram-discard-manager.c | 427 ++++++++++++++++++++++++--- 5 files changed, 515 insertions(+), 84 deletions(-)include/system/ram-discard-manager.h +4
-0@@ -170,30 +170,96 @@ struct RamDiscardSourceClass {* becoming discarded in a different granularity than it was populated and the * other way around. */ + +typedef struct RamDiscardSourceEntry RamDiscardSourceEntry ; + +struct RamDiscardSourceEntry {
include/system/ram-discard-manager.h struct RamDiscardSourceClass { · line 184
Quoted 10 lines
include/system/ram-discard-manager.h +5
-2+RamDiscardSource *rds; +QLIST_ENTRY (RamDiscardSourceEntry) next; +}; +struct RamDiscardManager {Object parent; -RamDiscardSource *rds; -MemoryRegion *mr; +struct MemoryRegion *mr;
s/struct//
Quoted 228 lines
include/system/ram-discard-manager.h +109
-23+ QLIST_HEAD(, RamDiscardSourceEntry) source_list; +uint64_t min_granularity; QLIST_HEAD(, RamDiscardListener) rdl_list; }; -RamDiscardManager *ram_discard_manager_new (MemoryRegion *mr, -RamDiscardSource *rds); +RamDiscardManager *ram_discard_manager_new (MemoryRegion *mr); + +/** +* ram_discard_manager_add_source: +* +* Register a #RamDiscardSource with the #RamDiscardManager. The manager +* aggregates state from all registered sources using AND semantics: a region +* is considered populated only if ALL sources report it as populated. +* +* If listeners are already registered, they will be notified about any +* regions that become discarded due to adding this source. Specifically, +* for each region that the new source reports as discarded, if all other +* sources reported it as populated, listeners receive a discard notification. +* +* If any listener rejects the notification (returns an error), previously +* notified listeners are rolled back with populate notifications and the +* source is not added. +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource to add +* +* Returns: 0 on success, -EBUSY if @source is already registered, or a +* negative error code if a listener rejected the state change. +*/ +int ram_discard_manager_add_source (RamDiscardManager *rdm, +RamDiscardSource *source); + +/** +* ram_discard_manager_del_source: +* +* Unregister a #RamDiscardSource from the #RamDiscardManager. +* +* If listeners are already registered, they will be notified about any +* regions that become populated due to removing this source. Specifically, +* for each region that the removed source reported as discarded, if all +* remaining sources report it as populated, listeners receive a populate +* notification. +* +* If any listener rejects the notification (returns an error), previously +* notified listeners are rolled back with discard notifications and the +* source is not removed. +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource to remove +* +* Returns: 0 on success, -ENOENT if @source is not registered, or a +* negative error code if a listener rejected the state change. +*/ +int ram_discard_manager_del_source (RamDiscardManager *rdm, +RamDiscardSource *source); +uint64_t ram_discard_manager_get_min_granularity (const RamDiscardManager *rdm,const MemoryRegion *mr); +/** +* ram_discard_manager_is_populated: +* +* Check if the given memory region section is populated. +* If the manager has no sources, it is considered populated. +* +* @rdm: the #RamDiscardManager +* @section: the #MemoryRegionSection to check +* +* Returns: true if the section is populated, false otherwise. +*/ bool ram_discard_manager_is_populated (const RamDiscardManager *rdm,const MemoryRegionSection *section);/** * ram_discard_manager_replay_populated: * -* Iterate the given #MemoryRegionSection at minimum granularity, calling -* #RamDiscardSourceClass.is_populated for each chunk, and invoke @replay_fn -* for each contiguous populated range. In case any call fails, no further -* calls are made. +* Call @replay_fn on regions that are populated in all sources. * * @rdm: the #RamDiscardManager * @section: the #MemoryRegionSection @@ -210,10 +276,7 @@ int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,/** * ram_discard_manager_replay_discarded: * -* Iterate the given #MemoryRegionSection at minimum granularity, calling -* #RamDiscardSourceClass.is_populated for each chunk, and invoke @replay_fn -* for each contiguous discarded range. In case any call fails, no further -* calls are made. +* Call @replay_fn on regions that are discarded in any sources. * * @rdm: the #RamDiscardManager * @section: the #MemoryRegionSection @@ -234,31 +297,61 @@ void ram_discard_manager_register_listener(RamDiscardManager *rdm,void ram_discard_manager_unregister_listener (RamDiscardManager *rdm,RamDiscardListener *rdl); -/* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. +/** +* ram_discard_manager_notify_populate: +* +* Notify listeners that a region is about to be populated by a source. +* For multi-source aggregation, only notifies when all sources agree +* the region is populated (intersection). +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource that is populating +* @offset: offset within the memory region +* @size: size of the region being populated +* +* Returns 0 on success, or a negative error if any listener rejects. */ int ram_discard_manager_notify_populate (RamDiscardManager *rdm, +RamDiscardSource *source,uint64_t offset,uint64_t size); -/* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. +/** +* ram_discard_manager_notify_discard: +* +* Notify listeners that a region has been discarded by a source. +* For multi-source aggregation, always notifies immediately +* (union semantics - any source discarding makes region discarded). +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource that is discarding +* @offset: offset within the memory region +* @size: size of the region being discarded */ void ram_discard_manager_notify_discard (RamDiscardManager *rdm, +RamDiscardSource *source,uint64_t offset,uint64_t size); -/* -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. +/** +* ram_discard_manager_notify_discard_all: +* +* Notify listeners that all regions have been discarded by a source. +* +* @rdm: the #RamDiscardManager +* @source: the #RamDiscardSource that is discarding */ -void ram_discard_manager_notify_discard_all (RamDiscardManager *rdm); +void ram_discard_manager_notify_discard_all (RamDiscardManager *rdm, +RamDiscardSource *source); -/* +/** +* ram_discard_manager_replay_populated_to_listeners: +* * Replay populated sections to all registered listeners. +* For multi-source aggregation, only replays regions where all sources +* are populated (intersection). * -* Note: later refactoring should take the source into account and the manager -* should be able to aggregate multiple sources. +* @rdm: the #RamDiscardManager +* +* Returns 0 on success, or a negative error if any notification failed. */ int ram_discard_manager_replay_populated_to_listeners (RamDiscardManager *rdm);hw/virtio/virtio-mem.c +5
-3@@ -264,7 +264,8 @@ static void virtio_mem_notify_unplug(VirtIOMEM *vmem, uint64_t offset, {RamDiscardManager *rdm =memory_region_get_ram_discard_manager (&vmem->memdev->mr); -ram_discard_manager_notify_discard (rdm, offset, size); +ram_discard_manager_notify_discard (rdm,RAM_DISCARD_SOURCE (vmem), + offset, size); } static int virtio_mem_notify_plug(VirtIOMEM *vmem, uint64_t offset,@@ -272,7 +273,8 @@ static int virtio_mem_notify_plug(VirtIOMEM *vmem, uint64_t offset, {RamDiscardManager *rdm =memory_region_get_ram_discard_manager (&vmem->memdev->mr); -return ram_discard_manager_notify_populate (rdm, offset, size); +return ram_discard_manager_notify_populate (rdm,RAM_DISCARD_SOURCE (vmem), + offset, size); }static void virtio_mem_notify_unplug_all (VirtIOMEM *vmem)@@ -283,7 +285,7 @@ static void virtio_mem_notify_unplug_all(VirtIOMEM *vmem)return ; } -ram_discard_manager_notify_discard_all (rdm); +ram_discard_manager_notify_discard_all (rdm,RAM_DISCARD_SOURCE (vmem)); } static bool virtio_mem_is_range_plugged(const VirtIOMEM *vmem,system/memory.c +4
-4@@ -2109,21 +2109,22 @@ int memory_region_add_ram_discard_source(MemoryRegion *mr, RamDiscardSource *source) {g_assert (memory_region_is_ram (mr)); -if (mr->rdm) { -return -EBUSY ; + +if (!mr->rdm) { + mr->rdm =ram_discard_manager_new (mr); } - mr->rdm =ram_discard_manager_new (mr,RAM_DISCARD_SOURCE (source)); -return 0 ; +return ram_discard_manager_add_source (mr->rdm, source); } void memory_region_del_ram_discard_source(MemoryRegion *mr,
system/memory.c int memory_region_add_ram_discard_source(MemoryRegion *mr, · line 2127
Quoted 10 lines
system/memory.c +4
-3RamDiscardSource *source) { -g_assert (mr->rdm->rds == source); +g_assert (mr->rdm); + +ram_discard_manager_del_source (mr->rdm, source); -object_unref (mr->rdm); - mr->rdm =NULL ; +/* if there is no source and no listener left, we could free rdm */
Maybe squash patch 14 directly into this one would be better?
Quoted 182 lines
system/memory.c +0
-0}/* Called with rcu_read_lock held. */ system/ram-block-attributes.c +4
-2@@ -90,7 +90,8 @@ ram_block_attributes_notify_discard(RamBlockAttributes *attr, {RamDiscardManager *rdm =memory_region_get_ram_discard_manager (attr->ram_block->mr); -ram_discard_manager_notify_discard (rdm, offset, size); +ram_discard_manager_notify_discard (rdm,RAM_DISCARD_SOURCE (attr), + offset, size); } static int@@ -99,7 +100,8 @@ ram_block_attributes_notify_populate(RamBlockAttributes *attr, {RamDiscardManager *rdm =memory_region_get_ram_discard_manager (attr->ram_block->mr); -return ram_discard_manager_notify_populate (rdm, offset, size); +return ram_discard_manager_notify_populate (rdm,RAM_DISCARD_SOURCE (attr), + offset, size); } int ram_block_attributes_state_change(RamBlockAttributes *attr,system/ram-discard-manager.c +99
-13@@ -7,6 +7,7 @@#include "qemu/osdep.h" #include "qemu/error-report.h" +#include "qemu/queue.h" #include "system/memory.h" static uint64_t ram_discard_source_get_min_granularity(const RamDiscardSource *rds,@@ -28,20 +29,21 @@ static bool ram_discard_source_is_populated(const RamDiscardSource *rds, }/* -* Iterate the section at source granularity, aggregating consecutive chunks -* with matching populated state, and call replay_fn for each run. +* Iterate a single source's populated or discarded regions and call +* replay_fn for each contiguous run. */ -static int replay_by_populated_state (const RamDiscardManager *rdm, -const MemoryRegionSection *section, -bool replay_populated, -ReplayRamDiscardState replay_fn, -void *opaque) +static int replay_source_by_state (const RamDiscardSource *source, +const MemoryRegion *mr, +const MemoryRegionSection *section, +bool replay_populated, +ReplayRamDiscardState replay_fn, +void *opaque) {uint64_t granularity, offset, size, end, pos, run_start;bool in_run = false;int ret =0 ; - granularity =ram_discard_source_get_min_granularity (rdm->rds, rdm->mr); + granularity =ram_discard_source_get_min_granularity (source, mr); offset = section->offset_within_region; size =int128_get64 (section->size); end = offset + size;@@ -55,7 +57,7 @@ static int replay_by_populated_state(const RamDiscardManager *rdm, .offset_within_region = pos, .size = int128_make64(granularity), }; -bool populated =ram_discard_source_is_populated (rdm->rds, &chunk); +bool populated =ram_discard_source_is_populated (source, &chunk);if (populated == replay_populated) {if (!in_run) {@@ -88,28 +90,338 @@ static int replay_by_populated_state(const RamDiscardManager *rdm,return ret; } -RamDiscardManager *ram_discard_manager_new (MemoryRegion *mr, -RamDiscardSource *rds) +RamDiscardManager *ram_discard_manager_new (MemoryRegion *mr) {RamDiscardManager *rdm; rdm =RAM_DISCARD_MANAGER (object_new (TYPE_RAM_DISCARD_MANAGER )); - rdm->rds = rds; rdm->mr = mr; -QLIST_INIT (&rdm->rdl_list);return rdm; } +static void ram_discard_manager_update_granularity (RamDiscardManager *rdm) +{ +RamDiscardSourceEntry *entry; +uint64_t granularity =0 ; + + QLIST_FOREACH(entry, &rdm->source_list, next) { +uint64_t src_granularity; + + src_granularity = +ram_discard_source_get_min_granularity (entry->rds, rdm->mr); +g_assert (src_granularity !=0 ); +if (granularity ==0 ) { + granularity = src_granularity; + }else { + granularity =MIN (granularity, src_granularity); + } + } + rdm->min_granularity = granularity; +} + +static RamDiscardSourceEntry * +ram_discard_manager_find_source (RamDiscardManager *rdm,RamDiscardSource *rds) +{ +RamDiscardSourceEntry *entry; + + QLIST_FOREACH(entry, &rdm->source_list, next) { +if (entry->rds == rds) { +return entry; + } + } +return NULL ; +} + +static int rdl_populate_cb (const MemoryRegionSection *section,void *opaque) +{ +RamDiscardListener *rdl = opaque; +MemoryRegionSection tmp = *rdl->section; + +g_assert (section->mr == rdl->section->mr); + +if (!memory_region_section_intersect_range (&tmp, + section->offset_within_region, +int128_get64 (section->size))) { +return 0 ; + } + +return rdl->notify_populate (rdl, &tmp); +} + +static int rdl_discard_cb (const MemoryRegionSection *section,void *opaque) +{ +RamDiscardListener *rdl = opaque; +MemoryRegionSection tmp = *rdl->section; + +g_assert (section->mr == rdl->section->mr); + +if (!memory_region_section_intersect_range (&tmp, + section->offset_within_region, +int128_get64 (section->size))) { +return 0 ; + } + + rdl->notify_discard (rdl, &tmp); +return 0 ; +} + +static bool rdm_is_all_populated_skip (const RamDiscardManager *rdm, +const MemoryRegionSection *section, +const RamDiscardSource *skip_source) +{ +RamDiscardSourceEntry *entry; + + QLIST_FOREACH(entry, &rdm->source_list, next) { +if (skip_source && entry->rds == skip_source) { +continue ; + } +if (!ram_discard_source_is_populated (entry->rds, section)) { +return false; + } + } +return true; +} + +typedef struct SourceNotifyCtx { + RamDiscardManager *rdm; + RamDiscardListener *rdl; + RamDiscardSource *source; /* added or removed */
system/ram-discard-manager.c static int replay_by_populated_state(const RamDiscardManager *rdm, · line 198
Quoted 10 lines
system/ram-discard-manager.c +10
-0+} SourceNotifyCtx; + +/* +* Unified helper to replay regions based on populated state. +* If replay_populated is true: replay regions where ALL sources are populated. +* If replay_populated is false: replay regions where ANY source is discarded. +*/ +static int replay_by_populated_state(const RamDiscardManager *rdm, + const MemoryRegionSection *section, + const RamDiscardSource *skip_source,
Indeed we still need skip_source to handle the cases where one source updated the populated status / bitmap to notify the change..
IIUC if we can cache the global status into a per-manager merged bitmap, then we could drop skip_source. That'll also help in the case you mentioned in the previous patch because then a replay can walk that merged bitmap, but as you also mentioned we don't yet know if the perf is an issue yet. I also don't know if a merged bitmap in the manager would help much if there're only two sources: I believe it'll help more if we have a lot more sources, but unlikely.. So just a quick thought.
Thanks,
Quoted 401 lines
system/ram-discard-manager.c +271
-34+bool replay_populated, + ReplayRamDiscardState replay_fn, + void *user_opaque) +{ + uint64_t granularity = rdm->min_granularity; +uint64_t offset, end_offset; +uint64_t run_start =0 ; +bool in_run = false; +int ret =0 ; + +if (QLIST_EMPTY (&rdm->source_list)) { +if (replay_populated) { +return replay_fn (section, user_opaque); + } +return 0 ; + } + +g_assert (granularity !=0 ); + + offset = section->offset_within_region; + end_offset = offset +int128_get64 (section->size); + +while (offset < end_offset) { +MemoryRegionSection subsection = { + .mr = section->mr, + .offset_within_region = offset, + .size =int128_make64 (MIN (granularity, end_offset - offset)), + }; +bool all_populated; +bool included; + + all_populated =rdm_is_all_populated_skip (rdm, &subsection, + skip_source); + included = replay_populated ? all_populated : !all_populated; + +if (included) { +if (!in_run) { + run_start = offset; + in_run = true; + } + }else { +if (in_run) { +MemoryRegionSection run_section = { + .mr = section->mr, + .offset_within_region = run_start, + .size =int128_make64 (offset - run_start), + }; + ret =replay_fn (&run_section, user_opaque); +if (ret) { +return ret; + } + in_run = false; + } + } +if (granularity > end_offset - offset) { +break ; + } + offset += granularity; + } + +if (in_run) { +MemoryRegionSection run_section = { + .mr = section->mr, + .offset_within_region = run_start, + .size =int128_make64 (end_offset - run_start), + }; + ret =replay_fn (&run_section, user_opaque); + } + +return ret; +} + +static int add_source_check_discard_cb (const MemoryRegionSection *section, +void *opaque) +{ +SourceNotifyCtx *ctx = opaque; + +return replay_by_populated_state (ctx->rdm, section, ctx->source, true, + rdl_discard_cb, ctx->rdl); +} + +static int del_source_check_populate_cb (const MemoryRegionSection *section, +void *opaque) +{ +SourceNotifyCtx *ctx = opaque; + +return replay_by_populated_state (ctx->rdm, section, ctx->source, true, + rdl_populate_cb, ctx->rdl); +} + +int ram_discard_manager_add_source (RamDiscardManager *rdm, +RamDiscardSource *source) +{ +RamDiscardSourceEntry *entry; +RamDiscardListener *rdl, *rdl2; +int ret =0 ; + +if (ram_discard_manager_find_source (rdm, source)) { +return -EBUSY ; + } + +/* +* If there are existing listeners, notify them about regions that +* become discarded due to adding this source. Only notify for regions +* that were previously populated (all other sources agreed). +*/ + QLIST_FOREACH(rdl, &rdm->rdl_list, next) { +SourceNotifyCtx ctx = { + .rdm = rdm, + .rdl = rdl, +/* no need to set source */ + }; + ret =replay_source_by_state (source, rdm->mr, rdl->section, + false, + add_source_check_discard_cb, &ctx); +if (ret) { +break ; + } + } +if (ret) { + QLIST_FOREACH(rdl2, &rdm->rdl_list, next) { +SourceNotifyCtx ctx = { + .rdm = rdm, + .rdl = rdl2, + }; +replay_source_by_state (source, rdm->mr, rdl2->section, + false, + del_source_check_populate_cb, + &ctx); +if (rdl == rdl2) { +break ; + } + } + +return ret; + } + + entry =g_new0 (RamDiscardSourceEntry,1 ); + entry->rds = source; +QLIST_INSERT_HEAD (&rdm->source_list, entry, next); + +ram_discard_manager_update_granularity (rdm); + +return ret; +} + +int ram_discard_manager_del_source (RamDiscardManager *rdm, +RamDiscardSource *source) +{ +RamDiscardSourceEntry *entry; +RamDiscardListener *rdl, *rdl2; +int ret =0 ; + + entry =ram_discard_manager_find_source (rdm, source); +if (!entry) { +return -ENOENT ; + } + +/* +* If there are existing listeners, check if any regions become +* populated due to removing this source. +*/ + QLIST_FOREACH(rdl, &rdm->rdl_list, next) { +SourceNotifyCtx ctx = { + .rdm = rdm, + .rdl = rdl, + .source = source, + }; +/* +* From the previously discarded regions, check if any +* regions become populated. +*/ + ret =replay_source_by_state (source, rdm->mr, rdl->section, + false, + del_source_check_populate_cb, + &ctx); +if (ret) { +break ; + } + } +if (ret) { + QLIST_FOREACH(rdl2, &rdm->rdl_list, next) { +SourceNotifyCtx ctx = { + .rdm = rdm, + .rdl = rdl2, + .source = source, + }; +replay_source_by_state (source, rdm->mr, rdl2->section, + false, + add_source_check_discard_cb, + &ctx); +if (rdl == rdl2) { +break ; + } + } + +return ret; + } + +QLIST_REMOVE (entry, next); +g_free (entry); +ram_discard_manager_update_granularity (rdm); +return ret; +} +uint64_t ram_discard_manager_get_min_granularity (const RamDiscardManager *rdm,const MemoryRegion *mr) { -return ram_discard_source_get_min_granularity (rdm->rds, mr); +g_assert (mr == rdm->mr); +return rdm->min_granularity; } +/* +* Aggregated query: returns true only if ALL sources report populated (AND). +*/ bool ram_discard_manager_is_populated (const RamDiscardManager *rdm,const MemoryRegionSection *section) { -return ram_discard_source_is_populated (rdm->rds, section); +RamDiscardSourceEntry *entry; + + QLIST_FOREACH(entry, &rdm->source_list, next) { +if (!ram_discard_source_is_populated (entry->rds, section)) { +return false; + } + } +return true; } int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,@@ -117,7 +429,8 @@ int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,ReplayRamDiscardState replay_fn, void *opaque) { - return replay_by_populated_state(rdm, section, true, replay_fn, opaque); + return replay_by_populated_state(rdm, section, NULL, true, +replay_fn ,opaque ); } int ram_discard_manager_replay_discarded(const RamDiscardManager *rdm,@@ -125,14 +438,17 @@ int ram_discard_manager_replay_discarded(const RamDiscardManager *rdm,ReplayRamDiscardState replay_fn, void *opaque) { - return replay_by_populated_state(rdm, section, false, replay_fn, opaque); + return replay_by_populated_state(rdm, section, NULL, false, +replay_fn ,opaque ); }static void ram_discard_manager_initfn (Object *obj) {RamDiscardManager *rdm =RAM_DISCARD_MANAGER (obj); +QLIST_INIT (&rdm->source_list);QLIST_INIT (&rdm->rdl_list); + rdm->min_granularity =0 ; }static void ram_discard_manager_finalize (Object *obj)@@ -140,74 +456,91 @@ static void ram_discard_manager_finalize(Object *obj)RamDiscardManager *rdm =RAM_DISCARD_MANAGER (obj);g_assert (QLIST_EMPTY (&rdm->rdl_list)); +g_assert (QLIST_EMPTY (&rdm->source_list)); }int ram_discard_manager_notify_populate (RamDiscardManager *rdm, +RamDiscardSource *source,uint64_t offset,uint64_t size) {RamDiscardListener *rdl, *rdl2; +MemoryRegionSection section = { + .mr = rdm->mr, + .offset_within_region = offset, + .size =int128_make64 (size), + };int ret =0 ; - QLIST_FOREACH(rdl, &rdm->rdl_list, next) { -MemoryRegionSection tmp = *rdl->section; +g_assert (ram_discard_manager_find_source (rdm, source)); -if (!memory_region_section_intersect_range (&tmp, offset, size)) { -continue ; - } - ret = rdl->notify_populate (rdl, &tmp); +/* +* Only notify about regions that are populated in ALL sources. +* replay_by_populated_state checks all sources including the one that +* just populated. +*/ + QLIST_FOREACH(rdl, &rdm->rdl_list, next) { + ret =replay_by_populated_state (rdm, §ion,NULL , true, + rdl_populate_cb, rdl);if (ret) {break ; } }if (ret) { -/* Notify all already-notified listeners about discard. */ +/* +* Rollback: notify discard for listeners we already notified, +* including the failing listener which may have been partially +* notified. Listeners must handle discard notifications for +* regions they didn't receive populate notifications for. +*/ QLIST_FOREACH(rdl2, &rdm->rdl_list, next) { -MemoryRegionSection tmp = *rdl2->section; - +replay_by_populated_state (rdm, §ion,NULL , true, + rdl_discard_cb, rdl2);if (rdl2 == rdl) {break ; } -if (!memory_region_section_intersect_range (&tmp, offset, size)) { -continue ; - } - rdl2->notify_discard (rdl2, &tmp); } }return ret; }void ram_discard_manager_notify_discard (RamDiscardManager *rdm, +RamDiscardSource *source,uint64_t offset,uint64_t size) {RamDiscardListener *rdl; - +MemoryRegionSection section = { + .mr = rdm->mr, + .offset_within_region = offset, + .size =int128_make64 (size), + }; + +g_assert (ram_discard_manager_find_source (rdm, source)); + +/* +* Only notify about ranges that were aggregately populated before this +* source's discard. Since the source has already updated its state, +* we use replay_by_populated_state with this source skipped - it will +* replay only the ranges where all OTHER sources are populated. +*/ QLIST_FOREACH(rdl, &rdm->rdl_list, next) { -MemoryRegionSection tmp = *rdl->section; - -if (!memory_region_section_intersect_range (&tmp, offset, size)) { -continue ; - } - rdl->notify_discard (rdl, &tmp); +replay_by_populated_state (rdm, §ion, source, true, + rdl_discard_cb, rdl); } } -void ram_discard_manager_notify_discard_all (RamDiscardManager *rdm) +void ram_discard_manager_notify_discard_all (RamDiscardManager *rdm, +RamDiscardSource *source) {RamDiscardListener *rdl; +g_assert (ram_discard_manager_find_source (rdm, source)); + QLIST_FOREACH(rdl, &rdm->rdl_list, next) { rdl->notify_discard (rdl, rdl->section); } } -static int rdm_populate_cb (const MemoryRegionSection *section,void *opaque) -{ -RamDiscardListener *rdl = opaque; - -return rdl->notify_populate (rdl, section); -} -void ram_discard_manager_register_listener (RamDiscardManager *rdm,RamDiscardListener *rdl,MemoryRegionSection *section)@@ -220,7 +553,7 @@ void ram_discard_manager_register_listener(RamDiscardManager *rdm,QLIST_INSERT_HEAD (&rdm->rdl_list, rdl, next); ret =ram_discard_manager_replay_populated (rdm, rdl->section, - rdm_populate_cb, rdl); + rdl_populate_cb, rdl);if (ret) {error_report ("%s: Replaying populated ranges failed: %s" , __func__,strerror (-ret));@@ -246,7 +579,7 @@ int ram_discard_manager_replay_populated_to_listeners(RamDiscardManager *rdm) QLIST_FOREACH(rdl, &rdm->rdl_list, next) { ret =ram_discard_manager_replay_populated (rdm, rdl->section, - rdm_populate_cb, rdl); + rdl_populate_cb, rdl);if (ret) {break ; }Signature · 1 line
2.53.0
Signature · 1 line
Peter Xu
PATCH v3 13/15 system/physmem: destroy ram block attributes before RCU-deferred reclaim
In reply to [email protected]From: Marc-André Lureau <[email protected]>
ram_block_attributes_destroy() was called from reclaim_ramblock(), which runs as an RCU callback deferred by call_rcu().
However,when the RamDiscardManager is finalized, it will assert that its source_list is empty in the next commit. Since the RCU callback hasn't run yet, the source added by ram_block_attributes_create() is still attached.
Move ram_block_attributes_destroy() into qemu_ram_free() so the source is removed synchronously. This is safe because qemu_ram_free() during shutdown runs after pause_all_vcpus(), so no vCPU thread can concurrently access the attributes via kvm_convert_memory().
--- system/physmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
system/physmem.c +1 -1
@@ -2589,7 +2589,6 @@ static void reclaim_ramblock(RAMBlock *block) }if (block->guest_memfd >=0 ) { -ram_block_attributes_destroy (block->attributes);close (block->guest_memfd);ram_block_coordinated_discard_require (false); }
@@ -2618,6 +2617,7 @@ void qemu_ram_free(RAMBlock *block)/* Write list before version */ smp_wmb (); ram_list.version++; +g_clear_pointer (&block->attributes, ram_block_attributes_destroy);call_rcu (block, reclaim_ramblock, rcu);qemu_mutex_unlock_ramlist (); }
Signature · 1 line
2.53.0
Re: [PATCH v3 13/15] system/physmem: destroy ram block attributes before RCU-deferred reclaim
Quoted 10 lines · On Thu, Feb 26, 2026 at 02:59:58PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
ram_block_attributes_destroy()was called fromreclaim_ramblock(), which runs as an RCU callback deferred bycall_rcu().However,when the RamDiscardManager is finalized, it will assert that its
source_listis empty in the next commit. Since the RCU callback hasn't run yet, the source added byram_block_attributes_create()is still attached.
Quoted 6 lines
Move
ram_block_attributes_destroy()intoqemu_ram_free()so the source is removed synchronously. This is safe becauseqemu_ram_free()during shutdown runs afterpause_all_vcpus(), so no vCPU thread can concurrently access the attributes viakvm_convert_memory().Signed-off-by Marc-André Lureau <[email protected]>
Signature · 1 line
Peter Xu
PATCH v3 14/15 system/memory: add RamDiscardManager reference counting and cleanup
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Listeners now hold a reference to the RamDiscardManager, ensuring it stays alive while listeners are registered. The RDM is eagerly freed when the last source and listener are removed, and also unreffed during MemoryRegion finalization as a safety net.
This completes the TODO left in the previous commit and prevents both use-after-free and memory leaks of the RamDiscardManager.
--- system/memory.c | 7 +++++-- system/ram-discard-manager.c | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-)
system/memory.c +5 -2
@@ -1817,6 +1817,7 @@ static void memory_region_finalize(Object *obj)memory_region_clear_coalescing (mr);g_free ((char *)mr->name);g_free (mr->ioeventfds); +object_unref (mr->rdm); }Object *memory_region_owner (MemoryRegion *mr)
@@ -2123,8 +2124,10 @@ void memory_region_del_ram_discard_source(MemoryRegion *mr,g_assert (mr->rdm);ram_discard_manager_del_source (mr->rdm, source); - -/* if there is no source and no listener left, we could free rdm */ +if (QLIST_EMPTY (&mr->rdm->source_list) &&QLIST_EMPTY (&mr->rdm->rdl_list)) { +object_unref (mr->rdm); + mr->rdm =NULL ; + } }/* Called with rcu_read_lock held. */
system/ram-discard-manager.c +2 -0
@@ -549,6 +549,7 @@ void ram_discard_manager_register_listener(RamDiscardManager *rdm,g_assert (section->mr == rdm->mr); +object_ref (rdm); rdl->section =memory_region_section_new_copy (section);QLIST_INSERT_HEAD (&rdm->rdl_list, rdl, next);
@@ -570,6 +571,7 @@ void ram_discard_manager_unregister_listener(RamDiscardManager *rdm,memory_region_section_free_copy (rdl->section); rdl->section =NULL ;QLIST_REMOVE (rdl, next); +object_unref (rdm); }int ram_discard_manager_replay_populated_to_listeners (RamDiscardManager *rdm)
Signature · 1 line
2.53.0
Re: [PATCH v3 14/15] system/memory: add RamDiscardManager reference counting and cleanup
Quoted 11 lines · On Thu, Feb 26, 2026 at 02:59:59PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Listeners now hold a reference to the RamDiscardManager, ensuring it stays alive while listeners are registered. The RDM is eagerly freed when the last source and listener are removed, and also unreffed during MemoryRegion finalization as a safety net.
This completes the TODO left in the previous commit and prevents both use-after-free and memory leaks of the RamDiscardManager.
Signed-off-by Marc-André Lureau <[email protected]>
Signature · 1 line
Peter Xu
PATCH v3 15/15 tests: add unit tests for RamDiscardManager multi-source aggregation
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Add various unit tests for the RamDiscardManager multi-source aggregation functionality.
The test uses a TestRamDiscardSource QOM object that tracks populated state via a bitmap, similar to RamBlockAttributes implementation.
--- tests/unit/test-ram-discard-manager-stubs.c | 48 + tests/unit/test-ram-discard-manager.c | 1234 +++++++++++++++++++ tests/unit/meson.build | 8 +- 3 files changed, 1289 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test-ram-discard-manager-stubs.c create mode 100644 tests/unit/test-ram-discard-manager.c
tests/unit/test-ram-discard-manager-stubs.c new file +48 -0
@@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#include "qemu/osdep.h" +#include "qom/object.h" +#include "glib.h" +#include "system/memory.h" + +RamDiscardManager *memory_region_get_ram_discard_manager (MemoryRegion *mr) +{ +return mr->rdm; +} + +int memory_region_add_ram_discard_source (MemoryRegion *mr, +RamDiscardSource *source) +{ +if (!mr->rdm) { + mr->rdm =ram_discard_manager_new (mr); + } +return ram_discard_manager_add_source (mr->rdm, source); +} + +void memory_region_del_ram_discard_source (MemoryRegion *mr, +RamDiscardSource *source) +{ +RamDiscardManager *rdm = mr->rdm; + +if (!rdm) { +return ; + } + +ram_discard_manager_del_source (rdm, source); +} + +uint64_t memory_region_size (MemoryRegion *mr) +{ +return int128_get64 (mr->size); +} + +MemoryRegionSection *memory_region_section_new_copy (MemoryRegionSection *s) +{ +MemoryRegionSection *copy =g_new (MemoryRegionSection,1 ); + *copy = *s; +return copy; +} + +void memory_region_section_free_copy (MemoryRegionSection *s) +{ +g_free (s); +}
tests/unit/test-ram-discard-manager.c new file +1234 -0
1,234 lines not on this page · raw message
tests/unit/meson.build +7 -1
@@ -136,7 +136,13 @@ if have_system 'test-bufferiszero': [], 'test-smp-parse': [qom, meson.project_source_root() / 'hw/core/machine-smp.c'], 'test-vmstate': [migration, io], - 'test-yank': ['socket-helpers.c', qom, io, chardev] + 'test-yank': ['socket-helpers.c', qom, io, chardev], + 'test-ram-discard-manager': [ + 'test-ram-discard-manager.c', + 'test-ram-discard-manager-stubs.c', + meson.project_source_root() / 'system/ram-discard-manager.c', + genh, qemuutil, qom + ], } if config_host_data.get('CONFIG_INOTIFY1') tests += {'test-util-filemonitor': []}
Signature · 1 line
2.53.0
Quoted 8 lines · On Thu, Feb 26, 2026 at 02:59:45PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Hi,
This is an attempt to fix the incompatibility of virtio-mem with confidential VMs. The solution implements what was discussed earlier with D. Hildenbrand:
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/#3502238
Quoted 8 lines
The first patches are misc cleanups. Then some code refactoring to have split a manager/source. And finally, the manager learns to deal with multiple sources.
I haven't done thorough testing. I only launched a SEV guest with a virtio-mem device. It would be nice to have more tests for those scenarios with VFIO/virtio-mem/confvm.. In any case, review & testing needed!
(should fix https://issues.redhat.com/browse/RHEL-131968)
Hi, Marc-André,
Just FYI that this series fails some CI tests:
https://gitlab.com/peterx/qemu/-/pipelines/2361780109
Frankly I don't yet know on why rust fails with this, maybe you have better idea.. So I'll leave that to you..
===8<===
error: unused import: InterfaceClass
--> rust/bindings/system-sys/libsystem_sys.rlib.p/structured/lib.rs:23:15
|
23 | use qom_sys::{InterfaceClass, Object, ObjectClass};
| ^^^^^^^^^^^^^^
| = note: -D unused-imports implied by -D warnings = help: to override -D warnings add #[allow(unused_imports)]
error: aborting due to 1 previous error
===8<===
The other thing is this series will generate tons of checkpatch issues, almost only line width issues and unmaintained files, so they're trivial. It seems to me it'll be nice if this series can land 11.0. We have one more week.
Please take a look when repost, thanks!
Signature · 1 line
Peter Xu
Hi
Quoted 31 lines · Peter Xu
Quoted 8 lines · On Thu, Feb 26, 2026 at 02:59:45PM +0100, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Hi,
This is an attempt to fix the incompatibility of virtio-mem with confidential VMs. The solution implements what was discussed earlier with D. Hildenbrand:
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/#3502238Quoted 8 lines
The first patches are misc cleanups. Then some code refactoring to have split a manager/source. And finally, the manager learns to deal with multiple sources.
I haven't done thorough testing. I only launched a SEV guest with a virtio-mem device. It would be nice to have more tests for those scenarios with VFIO/virtio-mem/confvm.. In any case, review & testing needed!
(should fix https://issues.redhat.com/browse/RHEL-131968)
Hi, Marc-André,
Just FYI that this series fails some CI tests:
https://gitlab.com/peterx/qemu/-/pipelines/2361780109
Frankly I don't yet know on why rust fails with this, maybe you have better idea.. So I'll leave that to you..
===8<===
error: unused import:InterfaceClass
-->rust/bindings/system-sys/libsystem_sys.rlib.p/structured/lib.rs:23:15
Quoted 9 lines
| 23 | use qom_sys::{InterfaceClass, Object, ObjectClass}; | ^^^^^^^^^^^^^^ |= note:
-D unused-importsimplied by-D warnings= help: to override-D warningsadd#[allow(unused_imports)]
error: aborting due to 1 previous error
===8<===
ok, this is because the RDM is no longer in memory.h, and thus objects are no longer needed. Fixed
Quoted 5 lines
The other thing is this series will generate tons of checkpatch issues, almost only line width issues and unmaintained files, so they're trivial. It seems to me it'll be nice if this series can land 11.0. We have one more week.
Please take a look when repost, thanks!
ok, thanks
Quoted 6 lines · On Thu, Feb 26, 2026 at 02:59:45PM +0100, [email protected] wrote:
Marc-André Lureau (15):
system/rba: useDIV_ROUND_UP
memory: dropRamDiscardListener::double_discard_supported
virtio-mem: usewarn_report_err_once()system/memory: minor doc fix
kvm: replace RamDicardManager by the RamBlockAttribute
If no objection, I'll queue these init five patches for 11.0.
Thanks,
Signature · 1 line
Peter Xu
Quoted 8 lines · On 2/26/2026 9:59 PM, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Hi,
This is an attempt to fix the incompatibility of virtio-mem with confidential VMs. The solution implements what was discussed earlier with D. Hildenbrand:
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/#3502238
Quoted 6 lines
The first patches are misc cleanups. Then some code refactoring to have split a manager/source. And finally, the manager learns to deal with multiple sources.
I haven't done thorough testing. I only launched a SEV guest with a virtio-mem device. It would be nice to have more tests for those scenarios with VFIO/virtio-mem/confvm.. In any case, review & testing needed!
Is this series aimed to enable virtio-mem (memory hotplug) in a confidential VM? I tested it within a TD guest using the qemu command:
qemu-system-x86_64
...
-m 2G,maxmem=10G \
-smp sockets=1,cores=2 \
-object memory-backend-ram,id=mem0,size=2G \
-numa node,nodeid=0,cpus=0-1,memdev=mem0 \
-object memory-backend-ram,id=vmem0,size=8G \
-device virtio-mem-pci,id=vm0,memdev=vmem0,node=0,requested-size=300M \
-object tdx-guest,id=tdx \
-machine q35,kernel_irqchip=split,hpet=off,memory-encryption=tdx \
...The TD VM will exit with the error when the guest kernel loads the virtio-mem driver:
kvm_intel: Guest access before accepting 0x108008000 on vCPU 0
I think it still lacks some support to accept the TD guest memory before using it.
Quoted 59 lines
(should fix https://issues.redhat.com/browse/RHEL-131968)
v3: issues found by Cédric
- fix assertion error on shutdown, due to rcu-defer cleanup
- fix API doc warningsv2:
- drop replay_{populated,discarded} from source, suggested by Peter Xu
- add extra manager cleanup
- add r-b tags for preliminary patchesthanks
Marc-André Lureau (15):
system/rba: useDIV_ROUND_UP
memory: dropRamDiscardListener::double_discard_supported
virtio-mem: usewarn_report_err_once()
system/memory: minor doc fix
kvm: replace RamDicardManager by the RamBlockAttribute system/memory: split RamDiscardManager into source and manager system/memory: move RamDiscardManager to separate compilation unit system/memory: constify section arguments system/ram-discard-manager: implement replay viais_populated
iteration
virtio-mem: remove replay_populated/replay_discarded implementation system/ram-discard-manager: drop replay from source interface system/memory: implement RamDiscardManager multi-source aggregation system/physmem: destroy ram block attributes before RCU-deferred
reclaim
system/memory: add RamDiscardManager reference counting and cleanup
tests: add unit tests for RamDiscardManager multi-source aggregationinclude/hw/vfio/vfio-container.h | 2 +- include/hw/vfio/vfio-cpr.h | 2 +- include/hw/virtio/virtio-mem.h | 3 - include/system/memory.h | 287 +---- include/system/ram-discard-manager.h | 358 ++++++ include/system/ramblock.h | 3 +- accel/kvm/kvm-all.c | 2 +- hw/vfio/cpr-legacy.c | 4 +- hw/vfio/listener.c | 12 +- hw/virtio/virtio-mem.c | 290 +---- migration/ram.c | 6 +- system/memory.c | 83 +- system/memory_mapping.c | 4 +- system/physmem.c | 2 +- system/ram-block-attributes.c | 279 +---- system/ram-discard-manager.c | 612 +++++++++ tests/unit/test-ram-discard-manager-stubs.c | 48 + tests/unit/test-ram-discard-manager.c | 1234 +++++++++++++++++++ system/meson.build | 1 + tests/unit/meson.build | 8 +- 20 files changed, 2361 insertions(+), 879 deletions(-) create mode 100644 include/system/ram-discard-manager.h create mode 100644 system/ram-discard-manager.c create mode 100644 tests/unit/test-ram-discard-manager-stubs.c create mode 100644 tests/unit/test-ram-discard-manager.c
Hi
Quoted 34 lines · Chenyi Qiang
Quoted 8 lines · On 2/26/2026 9:59 PM, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Hi,
This is an attempt to fix the incompatibility of virtio-mem with confidential VMs. The solution implements what was discussed earlier with D. Hildenbrand:
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/#3502238Quoted 6 lines
The first patches are misc cleanups. Then some code refactoring to have split a manager/source. And finally, the manager learns to deal with multiple sources.
I haven't done thorough testing. I only launched a SEV guest with a virtio-mem device. It would be nice to have more tests for those scenarios with VFIO/virtio-mem/confvm.. In any case, review & testing needed!
Is this series aimed to enable virtio-mem (memory hotplug) in a confidential VM? I tested it within a TD guest using the qemu command:
qemu-system-x86_64
... -m 2G,maxmem=10G \ -smp sockets=1,cores=2 \ -object memory-backend-ram,id=mem0,size=2G \ -numa node,nodeid=0,cpus=0-1,memdev=mem0 \ -object memory-backend-ram,id=vmem0,size=8G \ -device virtio-mem-pci,id=vm0,memdev=vmem0,node=0,requested-size=300M \ -object tdx-guest,id=tdx \ -machine q35,kernel_irqchip=split,hpet=off,memory-encryption=tdx \ ...
Quoted 6 lines
The TD VM will exit with the error when the guest kernel loads the virtio-mem driver:
kvm_intel: Guest access before accepting 0x108008000 on vCPU 0I think it still lacks some support to accept the TD guest memory before using it.
It becomes increasingly hard to provide easy to setup reproducibe environments.. but here is the libvirt qemu command I just tested succesfully against:
qemu-system-x86_64 -name guest=rhel10,debug-threads=on -S -object {"qom-type":"secret","id":"masterKey0","format":"raw","file":"/home/elmarco/.config/libvirt/qemu/lib/domain-3-rhel10/master-key.aes"} -machine pc-q35-10.1,usb=off,smm=off,dump-guest-core=off,confidential-guest-support=lsec0,hpet=off,acpi=on
-accel kvm -cpu host,migratable=on -bios/usr/share/edk2/ovmf/OVMF.inteltdx.fd -m
size=4096000k,slots=1,maxmem=8192000k -overcommit mem-lock=off -smp
2,sockets=2,cores=1,threads=1 -object
{"qom-type":"memory-backend-ram","id":"ram-node0","size":4194304000}
-numa node,nodeid=0,cpus=0-1,memdev=ram-node0 -uuid
0f3e6b74-80e3-45e7-a0e6-ae7dfc897606 -display none -no-user-config
-nodefaults -chardev socket,id=charmonitor,fd=23,server=on,wait=off
-mon chardev=charmonitor,id=monitor,mode=control -rtc
base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay
-no-shutdown -global ICH9-LPC.disable_s3=1 -global
ICH9-LPC.disable_s4=1 -boot strict=on -device {"driver":"pcie-root-port","port":8,"chassis":1,"id":"pci.1","bus":"pcie.0","multifunction":true,"addr":"0x1"}
-device {"driver":"pcie-root-port","port":9,"chassis":2,"id":"pci.2","bus":"pcie.0","addr":"0x1.0x1"}
-device {"driver":"pcie-root-port","port":10,"chassis":3,"id":"pci.3","bus":"pcie.0","addr":"0x1.0x2"}
-device {"driver":"pcie-root-port","port":11,"chassis":4,"id":"pci.4","bus":"pcie.0","addr":"0x1.0x3"}
-device {"driver":"pcie-root-port","port":12,"chassis":5,"id":"pci.5","bus":"pcie.0","addr":"0x1.0x4"}
-device {"driver":"pcie-root-port","port":13,"chassis":6,"id":"pci.6","bus":"pcie.0","addr":"0x1.0x5"}
-device {"driver":"pcie-root-port","port":14,"chassis":7,"id":"pci.7","bus":"pcie.0","addr":"0x1.0x6"}
-device {"driver":"pcie-root-port","port":15,"chassis":8,"id":"pci.8","bus":"pcie.0","addr":"0x1.0x7"}
-device {"driver":"pcie-root-port","port":16,"chassis":9,"id":"pci.9","bus":"pcie.0","multifunction":true,"addr":"0x2"} -device {"driver":"pcie-root-port","port":17,"chassis":10,"id":"pci.10","bus":"pcie.0","addr":"0x2.0x1"}
-device {"driver":"pcie-root-port","port":18,"chassis":11,"id":"pci.11","bus":"pcie.0","addr":"0x2.0x2"}
-device {"driver":"pcie-root-port","port":19,"chassis":12,"id":"pci.12","bus":"pcie.0","addr":"0x2.0x3"}
-device {"driver":"pcie-root-port","port":20,"chassis":13,"id":"pci.13","bus":"pcie.0","addr":"0x2.0x4"}
-device {"driver":"pcie-root-port","port":21,"chassis":14,"id":"pci.14","bus":"pcie.0","addr":"0x2.0x5"}
-device {"driver":"qemu-xhci","p2":15,"p3":15,"id":"usb","bus":"pci.2","addr":"0x0"}
-device {"driver":"virtio-serial-pci","id":"virtio-serial0","bus":"pci.3","addr":"0x0"}
-object {"qom-type":"memory-backend-ram","id":"memvirtiomem0","reserve":false,"size":4194304000}
-device {"driver":"virtio-mem-pci","node":0,"block-size":2097152,"memdev":"memvirtiomem0","id":"virtiomem0","bus":"pci.7","addr":"0x0"} -blockdev {"driver":"file","filename":"/home/elmarco/rhel-10.qcow2","node-name":"libvirt-1-storage","auto-read-only":true,"discard":"unmap"} -blockdev {"node-name":"libvirt-1-format","read-only":false,"driver":"qcow2","file":"libvirt-1-storage","backing":null} -device {"driver":"virtio-blk-pci","bus":"pci.4","addr":"0x0","drive":"libvirt-1-format","id":"virtio-disk0","bootindex":1} -netdev {"type":"user","id":"hostnet0"} -device {"driver":"virtio-net-pci","netdev":"hostnet0","id":"net0","mac":"52:54:00:82:08:40","bus":"pci.1","addr":"0x0"}
-chardev pty,id=charserial0 -device
{"driver":"isa-serial","chardev":"charserial0","id":"serial0","index":0}
-chardev socket,id=charchannel0,fd=22,server=on,wait=off -device {"driver":"virtserialport","bus":"virtio-serial0.0","nr":1,"chardev":"charchannel0","id":"channel0","name":"org.qemu.guest_agent.0"} -chardev socket,id=chrtpm,path=/run/user/1006/libvirt/qemu/run/swtpm/3-rhel10-swtpm.sock
-tpmdev emulator,id=tpm-tpm0,chardev=chrtpm -device
{"driver":"tpm-crb","tpmdev":"tpm-tpm0","id":"tpm0"} -audiodev
{"id":"audio1","driver":"none"} -global ICH9-LPC.noreboot=off
-watchdog-action reset -device
{"driver":"virtio-balloon-pci","id":"balloon0","bus":"pci.5","addr":"0x0"}
-object {"qom-type":"rng-random","id":"objrng0","filename":"/dev/urandom"}
-device {"driver":"virtio-rng-pci","rng":"objrng0","id":"rng0","bus":"pci.6","addr":"0x0"}
-object {"qom-type":"tdx-guest","id":"lsec0","quote-generation-socket":{"type":"unix","path":"/var/run/tdx-qgs/qgs.socket"},"attributes":268435456} -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny
-msg timestamp=on
Then I hotplugged some mem:
virsh --connect qemu:///session update-memory-device rhel10 --requested-size 2GiB
[ 166.228943] virtio_mem virtio5: plugged size: 0x0 [ 166.231784] virtio_mem virtio5: requested size: 0x80000000
[root@rhel10-server ~]# lsmem
RANGE SIZE STATE REMOVABLE BLOCK
0x0000000000000000-0x000000007fffffff 2G online yes 0-15 0x0000000100000000-0x00000001ffffffff 4G online yes 32-63
Memory block size: 128M Total online memory: 6G Total offline memory: 0B
The host kernel used is experimental; it's a patched version of 6.19.0-rc8 that I don't have the details)
Quoted 60 lines
Quoted 59 lines
(should fix https://issues.redhat.com/browse/RHEL-131968)
v3: issues found by Cédric
- fix assertion error on shutdown, due to rcu-defer cleanup
- fix API doc warningsv2:
- drop replay_{populated,discarded} from source, suggested by Peter Xu
- add extra manager cleanup
- add r-b tags for preliminary patchesthanks
Marc-André Lureau (15):
system/rba: useDIV_ROUND_UP
memory: dropRamDiscardListener::double_discard_supported
virtio-mem: usewarn_report_err_once()
system/memory: minor doc fix
kvm: replace RamDicardManager by the RamBlockAttribute system/memory: split RamDiscardManager into source and manager system/memory: move RamDiscardManager to separate compilation unit system/memory: constify section arguments system/ram-discard-manager: implement replay viais_populated
iteration
virtio-mem: remove replay_populated/replay_discarded implementation system/ram-discard-manager: drop replay from source interface system/memory: implement RamDiscardManager multi-source aggregation system/physmem: destroy ram block attributes before RCU-deferred
reclaim
system/memory: add RamDiscardManager reference counting and cleanup
tests: add unit tests for RamDiscardManager multi-source aggregationinclude/hw/vfio/vfio-container.h | 2 +- include/hw/vfio/vfio-cpr.h | 2 +- include/hw/virtio/virtio-mem.h | 3 - include/system/memory.h | 287 +---- include/system/ram-discard-manager.h | 358 ++++++ include/system/ramblock.h | 3 +- accel/kvm/kvm-all.c | 2 +- hw/vfio/cpr-legacy.c | 4 +- hw/vfio/listener.c | 12 +- hw/virtio/virtio-mem.c | 290 +---- migration/ram.c | 6 +- system/memory.c | 83 +- system/memory_mapping.c | 4 +- system/physmem.c | 2 +- system/ram-block-attributes.c | 279 +---- system/ram-discard-manager.c | 612 +++++++++ tests/unit/test-ram-discard-manager-stubs.c | 48 + tests/unit/test-ram-discard-manager.c | 1234 +++++++++++++++++++ system/meson.build | 1 + tests/unit/meson.build | 8 +- 20 files changed, 2361 insertions(+), 879 deletions(-) create mode 100644 include/system/ram-discard-manager.h create mode 100644 system/ram-discard-manager.c create mode 100644 tests/unit/test-ram-discard-manager-stubs.c create mode 100644 tests/unit/test-ram-discard-manager.c
Quoted 115 lines · Marc-André Lureau
Hi
Quoted 34 lines · Chenyi Qiang
Quoted 8 lines · On 2/26/2026 9:59 PM, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Hi,
This is an attempt to fix the incompatibility of virtio-mem with confidential VMs. The solution implements what was discussed earlier with D. Hildenbrand:
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/#3502238Quoted 6 lines
The first patches are misc cleanups. Then some code refactoring to have split a manager/source. And finally, the manager learns to deal with multiple sources.
I haven't done thorough testing. I only launched a SEV guest with a virtio-mem device. It would be nice to have more tests for those scenarios with VFIO/virtio-mem/confvm.. In any case, review & testing needed!
Is this series aimed to enable virtio-mem (memory hotplug) in a confidential VM? I tested it within a TD guest using the qemu command:
qemu-system-x86_64
... -m 2G,maxmem=10G \ -smp sockets=1,cores=2 \ -object memory-backend-ram,id=mem0,size=2G \ -numa node,nodeid=0,cpus=0-1,memdev=mem0 \ -object memory-backend-ram,id=vmem0,size=8G \ -device virtio-mem-pci,id=vm0,memdev=vmem0,node=0,requested-size=300M \ -object tdx-guest,id=tdx \ -machine q35,kernel_irqchip=split,hpet=off,memory-encryption=tdx \ ...Quoted 6 lines
The TD VM will exit with the error when the guest kernel loads the virtio-mem driver:
kvm_intel: Guest access before accepting 0x108008000 on vCPU 0I think it still lacks some support to accept the TD guest memory before using it.
It becomes increasingly hard to provide easy to setup reproducibe environments.. but here is the libvirt qemu command I just tested succesfully against:
qemu-system-x86_64 -name guest=rhel10,debug-threads=on -S -object {"qom-type":"secret","id":"masterKey0","format":"raw","file":"/home/elmarco/.config/libvirt/qemu/lib/domain-3-rhel10/master-key.aes"}
(quoted diff) +0
-2-machine pc-q35-10.1,usb=off,smm=off,dump-guest-core=off,confidential-guest-support=lsec0,hpet=off,acpi=on -accel kvm -cpu host,migratable=on -bios
/usr/share/edk2/ovmf/OVMF.inteltdx.fd-m size=4096000k,slots=1,maxmem=8192000k -overcommit mem-lock=off -smp 2,sockets=2,cores=1,threads=1 -object {"qom-type":"memory-backend-ram","id":"ram-node0","size":4194304000} -numa node,nodeid=0,cpus=0-1,memdev=ram-node0 -uuid 0f3e6b74-80e3-45e7-a0e6-ae7dfc897606 -display none -no-user-config(quoted diff) +0
-2-nodefaults -chardev socket,id=charmonitor,fd=23,server=on,wait=off -mon chardev=charmonitor,id=monitor,mode=control -rtcbase=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay
-no-shutdown -global ICH9-LPC.disable_s3=1 -global
ICH9-LPC.disable_s4=1 -boot strict=on -device {"driver":"pcie-root-port","port":8,"chassis":1,"id":"pci.1","bus":"pcie.0","multifunction":true,"addr":"0x1"}(quoted diff) +0
-21-device {"driver":"pcie-root-port","port":9,"chassis":2,"id":"pci.2","bus":"pcie.0","addr":"0x1.0x1"} -device {"driver":"pcie-root-port","port":10,"chassis":3,"id":"pci.3","bus":"pcie.0","addr":"0x1.0x2"} -device {"driver":"pcie-root-port","port":11,"chassis":4,"id":"pci.4","bus":"pcie.0","addr":"0x1.0x3"} -device {"driver":"pcie-root-port","port":12,"chassis":5,"id":"pci.5","bus":"pcie.0","addr":"0x1.0x4"} -device {"driver":"pcie-root-port","port":13,"chassis":6,"id":"pci.6","bus":"pcie.0","addr":"0x1.0x5"} -device {"driver":"pcie-root-port","port":14,"chassis":7,"id":"pci.7","bus":"pcie.0","addr":"0x1.0x6"} -device {"driver":"pcie-root-port","port":15,"chassis":8,"id":"pci.8","bus":"pcie.0","addr":"0x1.0x7"} -device {"driver":"pcie-root-port","port":16,"chassis":9,"id":"pci.9","bus":"pcie.0","multifunction":true,"addr":"0x2"} -device {"driver":"pcie-root-port","port":17,"chassis":10,"id":"pci.10","bus":"pcie.0","addr":"0x2.0x1"} -device {"driver":"pcie-root-port","port":18,"chassis":11,"id":"pci.11","bus":"pcie.0","addr":"0x2.0x2"} -device {"driver":"pcie-root-port","port":19,"chassis":12,"id":"pci.12","bus":"pcie.0","addr":"0x2.0x3"} -device {"driver":"pcie-root-port","port":20,"chassis":13,"id":"pci.13","bus":"pcie.0","addr":"0x2.0x4"} -device {"driver":"pcie-root-port","port":21,"chassis":14,"id":"pci.14","bus":"pcie.0","addr":"0x2.0x5"} -device {"driver":"qemu-xhci","p2":15,"p3":15,"id":"usb","bus":"pci.2","addr":"0x0"} -device {"driver":"virtio-serial-pci","id":"virtio-serial0","bus":"pci.3","addr":"0x0"} -object {"qom-type":"memory-backend-ram","id":"memvirtiomem0","reserve":false,"size":4194304000} -device {"driver":"virtio-mem-pci","node":0,"block-size":2097152,"memdev":"memvirtiomem0","id":"virtiomem0","bus":"pci.7","addr":"0x0"} -blockdev {"driver":"file","filename":"/home/elmarco/rhel-10.qcow2","node-name":"libvirt-1-storage","auto-read-only":true,"discard":"unmap"} -blockdev {"node-name":"libvirt-1-format","read-only":false,"driver":"qcow2","file":"libvirt-1-storage","backing":null} -device {"driver":"virtio-blk-pci","bus":"pci.4","addr":"0x0","drive":"libvirt-1-format","id":"virtio-disk0","bootindex":1} -netdev {"type":"user","id":"hostnet0"} -device{"driver":"virtio-net-pci","netdev":"hostnet0","id":"net0","mac":"52:54:00:82:08:40","bus":"pci.1","addr":"0x0"} -chardev pty,id=charserial0 -device
{"driver":"isa-serial","chardev":"charserial0","id":"serial0","index":0}
-chardev socket,id=charchannel0,fd=22,server=on,wait=off -device {"driver":"virtserialport","bus":"virtio-serial0.0","nr":1,"chardev":"charchannel0","id":"channel0","name":"org.qemu.guest_agent.0"}(quoted diff) +0
-2-chardev socket,id=chrtpm,path=/run/user/1006/libvirt/qemu/run/swtpm/3-rhel10-swtpm.sock -tpmdev emulator,id=tpm-tpm0,chardev=chrtpm -device{"driver":"tpm-crb","tpmdev":"tpm-tpm0","id":"tpm0"} -audiodev {"id":"audio1","driver":"none"} -global ICH9-LPC.noreboot=off -watchdog-action reset -device {"driver":"virtio-balloon-pci","id":"balloon0","bus":"pci.5","addr":"0x0"}
(quoted diff) +0
-5-object {"qom-type":"rng-random","id":"objrng0","filename":"/dev/urandom"} -device {"driver":"virtio-rng-pci","rng":"objrng0","id":"rng0","bus":"pci.6","addr":"0x0"} -object {"qom-type":"tdx-guest","id":"lsec0","quote-generation-socket":{"type":"unix","path":"/var/run/tdx-qgs/qgs.socket"},"attributes":268435456} -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny -msg timestamp=onThen I hotplugged some mem:
virsh --connect qemu:///session update-memory-device rhel10 --requested-size 2GiB[ 166.228943] virtio_mem virtio5: plugged size: 0x0 [ 166.231784] virtio_mem virtio5: requested size: 0x80000000[root@rhel10-server ~]# lsmem
RANGE SIZE STATE REMOVABLE BLOCK0x0000000000000000-0x000000007fffffff 2G online yes 0-15 0x0000000100000000-0x00000001ffffffff 4G online yes 32-63
Quoted 6 lines
Memory block size: 128M Total online memory: 6G Total offline memory: 0BThe host kernel used is experimental; it's a patched version of 6.19.0-rc8 that I don't have the details)
It seems related to the memory size. Maybe you can have a try with "-m 2G,maxmem=4G". I hit the problem when the size <= 2G and no range above 4g at the beginning.
Hi
Quoted 123 lines · Chenyi Qiang
Quoted 119 lines · Marc-André Lureau
Hi
Quoted 34 lines · Chenyi Qiang
Quoted 8 lines · On 2/26/2026 9:59 PM, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
Hi,
This is an attempt to fix the incompatibility of virtio-mem with confidential VMs. The solution implements what was discussed earlier with D. Hildenbrand:
https://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/#3502238Quoted 6 lines
The first patches are misc cleanups. Then some code refactoring to have split a manager/source. And finally, the manager learns to deal with multiple sources.
I haven't done thorough testing. I only launched a SEV guest with a virtio-mem device. It would be nice to have more tests for those scenarios with VFIO/virtio-mem/confvm.. In any case, review & testing needed!
Is this series aimed to enable virtio-mem (memory hotplug) in a confidential VM? I tested it within a TD guest using the qemu command:
qemu-system-x86_64
... -m 2G,maxmem=10G \ -smp sockets=1,cores=2 \ -object memory-backend-ram,id=mem0,size=2G \ -numa node,nodeid=0,cpus=0-1,memdev=mem0 \ -object memory-backend-ram,id=vmem0,size=8G \ -device virtio-mem-pci,id=vm0,memdev=vmem0,node=0,requested-size=300M \ -object tdx-guest,id=tdx \ -machine q35,kernel_irqchip=split,hpet=off,memory-encryption=tdx \ ...Quoted 6 lines
The TD VM will exit with the error when the guest kernel loads the virtio-mem driver:
kvm_intel: Guest access before accepting 0x108008000 on vCPU 0I think it still lacks some support to accept the TD guest memory before using it.
It becomes increasingly hard to provide easy to setup reproducibe environments.. but here is the libvirt qemu command I just tested succesfully against:
qemu-system-x86_64 -name guest=rhel10,debug-threads=on -S -object {"qom-type":"secret","id":"masterKey0","format":"raw","file":"/home/elmarco/.config/libvirt/qemu/lib/domain-3-rhel10/master-key.aes"}
(quoted diff) +0
-2-machine pc-q35-10.1,usb=off,smm=off,dump-guest-core=off,confidential-guest-support=lsec0,hpet=off,acpi=on -accel kvm -cpu host,migratable=on -bios
/usr/share/edk2/ovmf/OVMF.inteltdx.fd-m size=4096000k,slots=1,maxmem=8192000k -overcommit mem-lock=off -smp 2,sockets=2,cores=1,threads=1 -object {"qom-type":"memory-backend-ram","id":"ram-node0","size":4194304000} -numa node,nodeid=0,cpus=0-1,memdev=ram-node0 -uuid 0f3e6b74-80e3-45e7-a0e6-ae7dfc897606 -display none -no-user-config(quoted diff) +0
-2-nodefaults -chardev socket,id=charmonitor,fd=23,server=on,wait=off -mon chardev=charmonitor,id=monitor,mode=control -rtcbase=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay
-no-shutdown -global ICH9-LPC.disable_s3=1 -global
ICH9-LPC.disable_s4=1 -boot strict=on -device {"driver":"pcie-root-port","port":8,"chassis":1,"id":"pci.1","bus":"pcie.0","multifunction":true,"addr":"0x1"}(quoted diff) +0
-21-device {"driver":"pcie-root-port","port":9,"chassis":2,"id":"pci.2","bus":"pcie.0","addr":"0x1.0x1"} -device {"driver":"pcie-root-port","port":10,"chassis":3,"id":"pci.3","bus":"pcie.0","addr":"0x1.0x2"} -device {"driver":"pcie-root-port","port":11,"chassis":4,"id":"pci.4","bus":"pcie.0","addr":"0x1.0x3"} -device {"driver":"pcie-root-port","port":12,"chassis":5,"id":"pci.5","bus":"pcie.0","addr":"0x1.0x4"} -device {"driver":"pcie-root-port","port":13,"chassis":6,"id":"pci.6","bus":"pcie.0","addr":"0x1.0x5"} -device {"driver":"pcie-root-port","port":14,"chassis":7,"id":"pci.7","bus":"pcie.0","addr":"0x1.0x6"} -device {"driver":"pcie-root-port","port":15,"chassis":8,"id":"pci.8","bus":"pcie.0","addr":"0x1.0x7"} -device {"driver":"pcie-root-port","port":16,"chassis":9,"id":"pci.9","bus":"pcie.0","multifunction":true,"addr":"0x2"} -device {"driver":"pcie-root-port","port":17,"chassis":10,"id":"pci.10","bus":"pcie.0","addr":"0x2.0x1"} -device {"driver":"pcie-root-port","port":18,"chassis":11,"id":"pci.11","bus":"pcie.0","addr":"0x2.0x2"} -device {"driver":"pcie-root-port","port":19,"chassis":12,"id":"pci.12","bus":"pcie.0","addr":"0x2.0x3"} -device {"driver":"pcie-root-port","port":20,"chassis":13,"id":"pci.13","bus":"pcie.0","addr":"0x2.0x4"} -device {"driver":"pcie-root-port","port":21,"chassis":14,"id":"pci.14","bus":"pcie.0","addr":"0x2.0x5"} -device {"driver":"qemu-xhci","p2":15,"p3":15,"id":"usb","bus":"pci.2","addr":"0x0"} -device {"driver":"virtio-serial-pci","id":"virtio-serial0","bus":"pci.3","addr":"0x0"} -object {"qom-type":"memory-backend-ram","id":"memvirtiomem0","reserve":false,"size":4194304000} -device {"driver":"virtio-mem-pci","node":0,"block-size":2097152,"memdev":"memvirtiomem0","id":"virtiomem0","bus":"pci.7","addr":"0x0"} -blockdev {"driver":"file","filename":"/home/elmarco/rhel-10.qcow2","node-name":"libvirt-1-storage","auto-read-only":true,"discard":"unmap"} -blockdev {"node-name":"libvirt-1-format","read-only":false,"driver":"qcow2","file":"libvirt-1-storage","backing":null} -device {"driver":"virtio-blk-pci","bus":"pci.4","addr":"0x0","drive":"libvirt-1-format","id":"virtio-disk0","bootindex":1} -netdev {"type":"user","id":"hostnet0"} -device{"driver":"virtio-net-pci","netdev":"hostnet0","id":"net0","mac":"52:54:00:82:08:40","bus":"pci.1","addr":"0x0"} -chardev pty,id=charserial0 -device
{"driver":"isa-serial","chardev":"charserial0","id":"serial0","index":0}
-chardev socket,id=charchannel0,fd=22,server=on,wait=off -device {"driver":"virtserialport","bus":"virtio-serial0.0","nr":1,"chardev":"charchannel0","id":"channel0","name":"org.qemu.guest_agent.0"}(quoted diff) +0
-2-chardev socket,id=chrtpm,path=/run/user/1006/libvirt/qemu/run/swtpm/3-rhel10-swtpm.sock -tpmdev emulator,id=tpm-tpm0,chardev=chrtpm -device{"driver":"tpm-crb","tpmdev":"tpm-tpm0","id":"tpm0"} -audiodev {"id":"audio1","driver":"none"} -global ICH9-LPC.noreboot=off -watchdog-action reset -device {"driver":"virtio-balloon-pci","id":"balloon0","bus":"pci.5","addr":"0x0"}
(quoted diff) +0
-5-object {"qom-type":"rng-random","id":"objrng0","filename":"/dev/urandom"} -device {"driver":"virtio-rng-pci","rng":"objrng0","id":"rng0","bus":"pci.6","addr":"0x0"} -object {"qom-type":"tdx-guest","id":"lsec0","quote-generation-socket":{"type":"unix","path":"/var/run/tdx-qgs/qgs.socket"},"attributes":268435456} -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny -msg timestamp=onThen I hotplugged some mem:
virsh --connect qemu:///session update-memory-device rhel10 --requested-size 2GiB[ 166.228943] virtio_mem virtio5: plugged size: 0x0 [ 166.231784] virtio_mem virtio5: requested size: 0x80000000[root@rhel10-server ~]# lsmem
RANGE SIZE STATE REMOVABLE BLOCK0x0000000000000000-0x000000007fffffff 2G online yes 0-15 0x0000000100000000-0x00000001ffffffff 4G online yes 32-63Memory block size: 128M Total online memory: 6G Total offline memory: 0B
Quoted 5 lines
Quoted 2 lines
The host kernel used is experimental; it's a patched version of 6.19.0-rc8 that I don't have the details)
It seems related to the memory size. Maybe you can have a try with "-m 2G,maxmem=4G". I hit the problem when the size <= 2G and no range above 4g at the beginning.
Right, it crashes when I use -m 2G:
error: kvm run failed Input/output error
EAX=00000000 EBX=00000000 ECX=00000000 EDX=000c06f2 ESI=00000000 EDI=00000000 EBP=00000000 ESP=00000000 EIP=0000fff0 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0 ES =0000 00000000 0000ffff 00009300
CS =f000 ffff0000 0000ffff 00009b00
SS =0000 00000000 0000ffff 00009300
DS =0000 00000000 0000ffff 00009300
FS =0000 00000000 0000ffff 00009300
GS =0000 00000000 0000ffff 00009300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT= 00000000 0000ffff
IDT= 00000000 0000ffff
CR0=60000010 CR2=00000000 CR3=00000000 CR4=00000000 DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000
DR6=00000000ffff0ff0 DR7=0000000000000400 EFER=0000000000000000
Code=00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <00> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00kvm_mem_ioeventfd_del: error deleting ioeventfd: Input/output error (5)
This is a bit outside of my comfort zone,
maybe Paolo can provide some hints?