qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v1 5/5] softmmu/memory_mapping: optimize for RamDiscardMgr sectio


From: David Hildenbrand
Subject: [PATCH v1 5/5] softmmu/memory_mapping: optimize for RamDiscardMgr sections
Date: Wed, 10 Feb 2021 18:15:37 +0100

virtio-mem logically plugs/unplugs memory within a sparse memory region
and notifies via the RamDiscardMgr interface when parts become
plugged (populated) or unplugged (discarded).

Currently, we end up (via the two users)
1) zeroing all logically unplugged/discarded memory during TPM resets.
2) reading all logically unplugged/discarded memory when dumping, to
   figure out the content is zero.

1) is always bad, because we assume unplugged memory stays discarded
   (and is already implicitly zero).
2) isn't that bad with anonymous memory, we end up reading the zero
   page (slow and unnecessary, though). However, once we use some
   file-backed memory (future use case), even reading will populate memory.

Let's cut out all parts marked as not-populated (discarded) via the
RamDiscardMgr. As virtio-mem is the single user, this now means that
logically unplugged memory ranges will no longer be included in the
dump, which results in smaller dump files and faster dumping.

virtio-mem has a minimum granularity of 1 MiB (and the default is usually
2 MiB). Theoretically, we can see quite some fragmentation, in practice
we won't have it completely fragmented in 1 MiB pieces. Still, we might
end up with many physical ranges.

Both, the ELF format and kdump seem to be ready to support many
individual ranges (e.g., for ELF it seems to be UINT32_MAX, kdump has a
linear bitmap).

Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Dr. David Alan Gilbert <dgilbert@redhat.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Claudio Fontana <cfontana@suse.de>
Cc: Thomas Huth <thuth@redhat.com>
Cc: "Alex Bennée" <alex.bennee@linaro.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Laurent Vivier <lvivier@redhat.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
---
 softmmu/memory_mapping.c | 45 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/softmmu/memory_mapping.c b/softmmu/memory_mapping.c
index 05e8270edc..c9a3da6b54 100644
--- a/softmmu/memory_mapping.c
+++ b/softmmu/memory_mapping.c
@@ -244,6 +244,35 @@ static void guest_phys_block_add(GuestPhysBlockList *list, 
MemoryRegion *mr,
 #endif
 }
 
+typedef struct GuestPhysRDL {
+    GuestPhysBlockList *list;
+    RamDiscardListener listener;
+    MemoryRegionSection *section;
+} GuestPhysRDL;
+
+static int guest_phys_ram_discard_notify_populate(RamDiscardListener *listener,
+                                                  const MemoryRegion *const_mr,
+                                                  ram_addr_t offset,
+                                                  ram_addr_t size)
+{
+    GuestPhysRDL *rdl = container_of(listener, GuestPhysRDL, listener);
+    MemoryRegionSection *s = rdl->section;
+    const hwaddr mr_start = MAX(offset, s->offset_within_region);
+    const hwaddr mr_end = MIN(offset + size,
+                              s->offset_within_region + int128_get64(s->size));
+    uint8_t *host_addr;
+
+    if (mr_start >= mr_end) {
+        return 0;
+    }
+
+    host_addr = memory_region_get_ram_ptr(s->mr) + mr_start;
+    guest_phys_block_add(rdl->list, s->mr,
+                         mr_start + s->offset_within_address_space,
+                         mr_end + s->offset_within_address_space, host_addr);
+    return 0;
+}
+
 static void guest_phys_blocks_region_add(MemoryListener *listener,
                                          MemoryRegionSection *section)
 {
@@ -258,6 +287,22 @@ static void guest_phys_blocks_region_add(MemoryListener 
*listener,
         return;
     }
 
+    /* for special sparse regions, only add populated parts */
+    if (memory_region_has_ram_discard_mgr(section->mr)) {
+        RamDiscardMgr *rdm = memory_region_get_ram_discard_mgr(section->mr);
+        RamDiscardMgrClass *rdmc = RAM_DISCARD_MGR_GET_CLASS(rdm);
+        GuestPhysRDL rdl = {
+            .list = g->list,
+            .section = section,
+        };
+
+        ram_discard_listener_init(&rdl.listener,
+                                  guest_phys_ram_discard_notify_populate, NULL,
+                                  NULL);
+        rdmc->replay_populated(rdm, section->mr, &rdl.listener);
+        return;
+    }
+
     target_start = section->offset_within_address_space;
     target_end = target_start + int128_get64(section->size);
     host_addr = memory_region_get_ram_ptr(section->mr) +
-- 
2.29.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]