[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 15/16] memory: Add region_resize() callback to memory notifier
From: |
David Hildenbrand |
Subject: |
[PATCH v2 15/16] memory: Add region_resize() callback to memory notifier |
Date: |
Wed, 12 Feb 2020 14:36:00 +0100 |
Let's provide a way for memory notifiers to get notified about a resize.
If the region_resize() callback is not implemented by a notifier, we
mimic the old behavior by removing the old section and adding the
new, resized section.
The existing code would remove all sections first and then add the new
ones. When resizing, we will now remove+re-add in a single shot. As we
grow in the adding phase and shrink in the removal phase, this should
not make a difference.
This callback is helpful when backends (like KVM) want to implement
atomic resizes of memory sections (e.g., resize while VCPUs are running and
using the section).
Note 1: Resizing while changing logging is unlikely, but nothing speaks
against allowing it.
Note 2: Resizing MMIO regions is unlikely (coalesced io handling), but
nothing speaks against it.
Signed-off-by: David Hildenbrand <address@hidden>
---
include/exec/memory.h | 19 ++++++++++
memory.c | 85 ++++++++++++++++++++++++++++++++++++++++---
2 files changed, 99 insertions(+), 5 deletions(-)
diff --git a/include/exec/memory.h b/include/exec/memory.h
index dfedd88f13..1ec5432340 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -493,6 +493,25 @@ struct MemoryListener {
*/
void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
+ /**
+ * @region_resize:
+ *
+ * Called during an address space update transaction,
+ * for a section of the address space that is in the same place in the
+ * address space as in the last transaction, however, the size changed.
+ * Dirty memory logging can change as well.
+ *
+ * Note: If this callback is not implemented well, the resize is
+ * communicated via a region_del(), followed by a region_add()
+ * instead.
+ *
+ * @listener: The #MemoryListener.
+ * @section: The old #MemoryRegionSection.
+ * @new: The new size.
+ */
+ void (*region_resize)(MemoryListener *listener,
+ MemoryRegionSection *section, Int128 new);
+
/**
* @log_start:
*
diff --git a/memory.c b/memory.c
index 5c62702618..0d9fe189ad 100644
--- a/memory.c
+++ b/memory.c
@@ -246,6 +246,17 @@ static bool flatrange_equal(FlatRange *a, FlatRange *b)
&& a->nonvolatile == b->nonvolatile;
}
+static bool flatrange_resized(FlatRange *a, FlatRange *b)
+{
+ return a->mr == b->mr
+ && int128_eq(a->addr.start, b->addr.start)
+ && int128_ne(a->addr.size, b->addr.size)
+ && a->offset_in_region == b->offset_in_region
+ && a->romd_mode == b->romd_mode
+ && a->readonly == b->readonly
+ && a->nonvolatile == b->nonvolatile;
+}
+
static FlatView *flatview_new(MemoryRegion *mr_root)
{
FlatView *view;
@@ -875,6 +886,51 @@ static void flat_range_coalesced_io_add(FlatRange *fr,
AddressSpace *as)
}
}
+static void memory_listener_resize_region(FlatRange *fr, AddressSpace *as,
+ enum ListenerDirection dir,
+ Int128 new)
+{
+ FlatView *as_view = address_space_to_flatview(as);
+ MemoryRegionSection old_mrs = section_from_flat_range(fr, as_view);
+ MemoryRegionSection new_mrs = old_mrs;
+ MemoryListener *listener;
+
+ new_mrs.size = new;
+
+ switch (dir) {
+ case Forward:
+ QTAILQ_FOREACH(listener, &as->listeners, link_as) {
+ if (listener->region_resize) {
+ listener->region_resize(listener, &old_mrs, new);
+ continue;
+ }
+ if (listener->region_del) {
+ listener->region_del(listener, &old_mrs);
+ }
+ if (listener->region_add) {
+ listener->region_add(listener, &new_mrs);
+ }
+ }
+ break;
+ case Reverse:
+ QTAILQ_FOREACH_REVERSE(listener, &as->listeners, link_as) {
+ if (listener->region_resize) {
+ listener->region_resize(listener, &old_mrs, new);
+ continue;
+ }
+ if (listener->region_del) {
+ listener->region_del(listener, &old_mrs);
+ }
+ if (listener->region_add) {
+ listener->region_add(listener, &new_mrs);
+ }
+ }
+ break;
+ default:
+ g_assert_not_reached();
+ }
+}
+
static void address_space_update_topology_pass(AddressSpace *as,
const FlatView *old_view,
const FlatView *new_view,
@@ -899,11 +955,30 @@ static void
address_space_update_topology_pass(AddressSpace *as,
frnew = NULL;
}
- if (frold
- && (!frnew
- || int128_lt(frold->addr.start, frnew->addr.start)
- || (int128_eq(frold->addr.start, frnew->addr.start)
- && !flatrange_equal(frold, frnew)))) {
+ if (frold && frnew && flatrange_resized(frold, frnew)) {
+ /* In both and only the size (+ eventually logging) changed. */
+
+ if (int128_lt(frold->addr.size, frnew->addr.size) && adding) {
+ /* Grow in the adding phase. */
+ memory_listener_resize_region(frold, as, Forward,
+ frnew->addr.size);
+ flat_range_coalesced_io_del(frold, as);
+ flat_range_coalesced_io_add(frnew, as);
+ } else if (int128_gt(frold->addr.size, frnew->addr.size) &&
+ !adding) {
+ /* Shrink in the removal phase. */
+ memory_listener_resize_region(frold, as, Reverse,
+ frnew->addr.size);
+ flat_range_coalesced_io_del(frold, as);
+ flat_range_coalesced_io_add(frnew, as);
+ }
+
+ ++iold;
+ ++inew;
+ } else if (frold && (!frnew
+ || int128_lt(frold->addr.start, frnew->addr.start)
+ || (int128_eq(frold->addr.start,
frnew->addr.start)
+ && !flatrange_equal(frold, frnew)))) {
/* In old but not in new, or in both but attributes changed. */
if (!adding) {
--
2.24.1
- [PATCH v2 06/16] exec: Provide owner when resizing memory region, (continued)
- [PATCH v2 06/16] exec: Provide owner when resizing memory region, David Hildenbrand, 2020/02/12
- [PATCH v2 07/16] memory: Add memory_region_max_size() and memory_region_is_resizable(), David Hildenbrand, 2020/02/12
- [PATCH v2 08/16] memory: Disallow resizing to 0, David Hildenbrand, 2020/02/12
- [PATCH v2 09/16] memory-device: properly deal with resizable memory regions, David Hildenbrand, 2020/02/12
- [PATCH v2 10/16] hostmem: Factor out applying settings, David Hildenbrand, 2020/02/12
- [PATCH v2 11/16] hostmem: Factor out common checks into host_memory_backend_validate(), David Hildenbrand, 2020/02/12
- [PATCH v2 12/16] hostmem: Introduce "managed-size" for memory-backend-ram, David Hildenbrand, 2020/02/12
- [PATCH v2 13/16] qmp/hmp: Expose "managed-size" for memory backends, David Hildenbrand, 2020/02/12
- [PATCH v2 14/16] virtio-mem: Support for resizable memory regions, David Hildenbrand, 2020/02/12
- [PATCH v2 15/16] memory: Add region_resize() callback to memory notifier,
David Hildenbrand <=
- [PATCH v2 16/16] kvm: Implement region_resize() for atomic memory section resizes, David Hildenbrand, 2020/02/12
- Re: [PATCH v2 00/16] Ram blocks with resizable anonymous allocations under POSIX, David Hildenbrand, 2020/02/12