memory: support stateless memory listeners
authorAvi Kivity <avi@redhat.com>
Wed, 8 Feb 2012 19:36:02 +0000 (21:36 +0200)
committerAvi Kivity <avi@redhat.com>
Wed, 29 Feb 2012 11:44:42 +0000 (13:44 +0200)
Current memory listeners are incremental; that is, they are expected to
maintain their own state, and receive callbacks for changes to that state.

This patch adds support for stateless listeners; these work by receiving
a ->begin() callback (which tells them that new state is coming), a
sequence of ->region_add() and ->region_nop() callbacks, and then a
->commit() callback which signifies the end of the new state.  They should
ignore ->region_del() callbacks.

Signed-off-by: Avi Kivity <avi@redhat.com>
exec.c
hw/vhost.c
kvm-all.c
memory.c
memory.h
xen-all.c

diff --git a/exec.c b/exec.c
index 16973a675fbf761203054f264c69329bfe72fd52..6726afd347029abd2e683811e2f4e1e2fdf9591e 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -3488,6 +3488,14 @@ static void io_mem_init(void)
                           "watch", UINT64_MAX);
 }
 
+static void core_begin(MemoryListener *listener)
+{
+}
+
+static void core_commit(MemoryListener *listener)
+{
+}
+
 static void core_region_add(MemoryListener *listener,
                             MemoryRegionSection *section)
 {
@@ -3500,6 +3508,11 @@ static void core_region_del(MemoryListener *listener,
     cpu_register_physical_memory_log(section, false);
 }
 
+static void core_region_nop(MemoryListener *listener,
+                            MemoryRegionSection *section)
+{
+}
+
 static void core_log_start(MemoryListener *listener,
                            MemoryRegionSection *section)
 {
@@ -3537,6 +3550,14 @@ static void core_eventfd_del(MemoryListener *listener,
 {
 }
 
+static void io_begin(MemoryListener *listener)
+{
+}
+
+static void io_commit(MemoryListener *listener)
+{
+}
+
 static void io_region_add(MemoryListener *listener,
                           MemoryRegionSection *section)
 {
@@ -3551,6 +3572,11 @@ static void io_region_del(MemoryListener *listener,
     isa_unassign_ioport(section->offset_within_address_space, section->size);
 }
 
+static void io_region_nop(MemoryListener *listener,
+                          MemoryRegionSection *section)
+{
+}
+
 static void io_log_start(MemoryListener *listener,
                          MemoryRegionSection *section)
 {
@@ -3587,8 +3613,11 @@ static void io_eventfd_del(MemoryListener *listener,
 }
 
 static MemoryListener core_memory_listener = {
+    .begin = core_begin,
+    .commit = core_commit,
     .region_add = core_region_add,
     .region_del = core_region_del,
+    .region_nop = core_region_nop,
     .log_start = core_log_start,
     .log_stop = core_log_stop,
     .log_sync = core_log_sync,
@@ -3600,8 +3629,11 @@ static MemoryListener core_memory_listener = {
 };
 
 static MemoryListener io_memory_listener = {
+    .begin = io_begin,
+    .commit = io_commit,
     .region_add = io_region_add,
     .region_del = io_region_del,
+    .region_nop = io_region_nop,
     .log_start = io_log_start,
     .log_stop = io_log_stop,
     .log_sync = io_log_sync,
index 01f676a959caa8c90e1bf9d750884048cd7e0293..8d3ba5b608dc3041e81f999e9ef35e9ffd0c8a13 100644 (file)
@@ -436,6 +436,14 @@ static bool vhost_section(MemoryRegionSection *section)
         && memory_region_is_ram(section->mr);
 }
 
+static void vhost_begin(MemoryListener *listener)
+{
+}
+
+static void vhost_commit(MemoryListener *listener)
+{
+}
+
 static void vhost_region_add(MemoryListener *listener,
                              MemoryRegionSection *section)
 {
@@ -476,6 +484,11 @@ static void vhost_region_del(MemoryListener *listener,
     }
 }
 
+static void vhost_region_nop(MemoryListener *listener,
+                             MemoryRegionSection *section)
+{
+}
+
 static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
                                     struct vhost_virtqueue *vq,
                                     unsigned idx, bool enable_log)
@@ -756,8 +769,11 @@ int vhost_dev_init(struct vhost_dev *hdev, int devfd, bool force)
     hdev->features = features;
 
     hdev->memory_listener = (MemoryListener) {
+        .begin = vhost_begin,
+        .commit = vhost_commit,
         .region_add = vhost_region_add,
         .region_del = vhost_region_del,
+        .region_nop = vhost_region_nop,
         .log_start = vhost_log_start,
         .log_stop = vhost_log_stop,
         .log_sync = vhost_log_sync,
index 15bc42fcdbbec577bc57d9822198c786f982ee6d..c07823df988766626466755783baf153fbe1421b 100644 (file)
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -680,6 +680,14 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
     }
 }
 
+static void kvm_begin(MemoryListener *listener)
+{
+}
+
+static void kvm_commit(MemoryListener *listener)
+{
+}
+
 static void kvm_region_add(MemoryListener *listener,
                            MemoryRegionSection *section)
 {
@@ -692,6 +700,11 @@ static void kvm_region_del(MemoryListener *listener,
     kvm_set_phys_mem(section, false);
 }
 
+static void kvm_region_nop(MemoryListener *listener,
+                           MemoryRegionSection *section)
+{
+}
+
 static void kvm_log_sync(MemoryListener *listener,
                          MemoryRegionSection *section)
 {
@@ -795,8 +808,11 @@ static void kvm_eventfd_del(MemoryListener *listener,
 }
 
 static MemoryListener kvm_memory_listener = {
+    .begin = kvm_begin,
+    .commit = kvm_commit,
     .region_add = kvm_region_add,
     .region_del = kvm_region_del,
+    .region_nop = kvm_region_nop,
     .log_start = kvm_log_start,
     .log_stop = kvm_log_stop,
     .log_sync = kvm_log_sync,
index 35e14838590e647bdb94263b0d1e4732bfc0da32..6565e2e6964d5581549ee6322595b30511f93ad3 100644 (file)
--- a/memory.c
+++ b/memory.c
@@ -676,6 +676,7 @@ static void address_space_update_topology_pass(AddressSpace *as,
             /* In both (logging may have changed) */
 
             if (adding) {
+                MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
                 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
                     MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop);
                 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
@@ -722,6 +723,8 @@ static void memory_region_update_topology(MemoryRegion *mr)
         return;
     }
 
+    MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
+
     if (address_space_memory.root) {
         address_space_update_topology(&address_space_memory);
     }
@@ -729,6 +732,8 @@ static void memory_region_update_topology(MemoryRegion *mr)
         address_space_update_topology(&address_space_io);
     }
 
+    MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
+
     memory_region_update_pending = false;
 }
 
index bc9600b40205cd49459f158514cbf1732158a739..b7bccd19680fc3bf8288f141114d180aa797b89a 100644 (file)
--- a/memory.h
+++ b/memory.h
@@ -180,8 +180,11 @@ typedef struct MemoryListener MemoryListener;
  * Use with memory_listener_register() and memory_listener_unregister().
  */
 struct MemoryListener {
+    void (*begin)(MemoryListener *listener);
+    void (*commit)(MemoryListener *listener);
     void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
     void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
+    void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
     void (*log_start)(MemoryListener *listener, MemoryRegionSection *section);
     void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section);
     void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
index a58a397ca44999f5827300472e9ea54bc7310693..6a11342665c713178f42c8c154d9ad6ff8a50e3e 100644 (file)
--- a/xen-all.c
+++ b/xen-all.c
@@ -394,6 +394,14 @@ static void xen_set_memory(struct MemoryListener *listener,
     }
 }
 
+static void xen_begin(MemoryListener *listener)
+{
+}
+
+static void xen_commit(MemoryListener *listener)
+{
+}
+
 static void xen_region_add(MemoryListener *listener,
                            MemoryRegionSection *section)
 {
@@ -406,6 +414,11 @@ static void xen_region_del(MemoryListener *listener,
     xen_set_memory(listener, section, false);
 }
 
+static void xen_region_nop(MemoryListener *listener,
+                           MemoryRegionSection *section)
+{
+}
+
 static void xen_sync_dirty_bitmap(XenIOState *state,
                                   target_phys_addr_t start_addr,
                                   ram_addr_t size)
@@ -500,8 +513,11 @@ static void xen_eventfd_del(MemoryListener *listener,
 }
 
 static MemoryListener xen_memory_listener = {
+    .begin = xen_begin,
+    .commit = xen_commit,
     .region_add = xen_region_add,
     .region_del = xen_region_del,
+    .region_nop = xen_region_nop,
     .log_start = xen_log_start,
     .log_stop = xen_log_stop,
     .log_sync = xen_log_sync,