diff mbox series

memory: notify hypervisor of all eventfds during listener (de)registration

Message ID 20240910041936.5142-1-anisinha@redhat.com
State New
Headers show
Series memory: notify hypervisor of all eventfds during listener (de)registration | expand

Commit Message

Ani Sinha Sept. 10, 2024, 4:19 a.m. UTC
When a new listener for an address space is registered, the hypervisor must be
informed of all existing eventfds for that address space by calling
eventfd_add() for that listener. Similarly, when a listener is de-registered
from an address space, the hypervisor must be informed of all existing eventfds
for that address space with a call to eventfd_del().

Signed-off-by: Ani Sinha <anisinha@redhat.com>
---
 system/memory.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
diff mbox series

Patch

diff --git a/system/memory.c b/system/memory.c
index 5e6eb459d5..ac1b809491 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -941,6 +941,41 @@  static void flat_range_coalesced_io_add(FlatRange *fr, AddressSpace *as)
     }
 }
 
+static void
+flat_range_coalesced_io_notify_listener_add_del(FlatRange *fr,
+                                                MemoryListener *listener,
+                                                AddressSpace *as, bool add)
+{
+   CoalescedMemoryRange *cmr;
+   MemoryRegion *mr = fr->mr;
+   FlatView *fw = address_space_to_flatview(as);
+
+   QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
+       AddrRange tmp;
+       MemoryRegionSection mrs = section_from_flat_range(fr, fw);
+
+       tmp = addrrange_shift(cmr->addr,
+                             int128_sub(fr->addr.start,
+                                        int128_make64(fr->offset_in_region)));
+
+       if (!addrrange_intersects(tmp, fr->addr)) {
+           return;
+       }
+       tmp = addrrange_intersection(tmp, fr->addr);
+
+       if (add) {
+           listener->coalesced_io_add(listener, &mrs,
+                                      int128_get64(tmp.start),
+                                      int128_get64(tmp.size));
+       } else {
+           listener->coalesced_io_del(listener, &mrs,
+                                      int128_get64(tmp.start),
+                                      int128_get64(tmp.size));
+       }
+   }
+}
+
+
 static void address_space_update_topology_pass(AddressSpace *as,
                                                const FlatView *old_view,
                                                const FlatView *new_view,
@@ -3015,8 +3050,10 @@  void memory_global_dirty_log_stop(unsigned int flags)
 static void listener_add_address_space(MemoryListener *listener,
                                        AddressSpace *as)
 {
+    unsigned i;
     FlatView *view;
     FlatRange *fr;
+    MemoryRegionIoeventfd *fd;
 
     if (listener->begin) {
         listener->begin(listener);
@@ -3041,10 +3078,27 @@  static void listener_add_address_space(MemoryListener *listener,
         if (listener->region_add) {
             listener->region_add(listener, &section);
         }
+        flat_range_coalesced_io_notify_listener_add_del(fr, listener, as, true);
         if (fr->dirty_log_mask && listener->log_start) {
             listener->log_start(listener, &section, 0, fr->dirty_log_mask);
         }
     }
+
+    /*
+     * register all eventfds for this address space for the newly registered
+     * listener.
+     */
+    for (i = 0; i < as->ioeventfd_nb; i++) {
+        fd = &as->ioeventfds[i];
+        MemoryRegionSection section = (MemoryRegionSection) {
+            .fv = address_space_to_flatview(as),
+            .offset_within_address_space = int128_get64(fd->addr.start),
+            .size = fd->addr.size,
+        };
+        listener->eventfd_add(listener, &section,
+                              fd->match_data, fd->data, fd->e);
+    }
+
     if (listener->commit) {
         listener->commit(listener);
     }
@@ -3054,8 +3108,10 @@  static void listener_add_address_space(MemoryListener *listener,
 static void listener_del_address_space(MemoryListener *listener,
                                        AddressSpace *as)
 {
+    unsigned i;
     FlatView *view;
     FlatRange *fr;
+    MemoryRegionIoeventfd *fd;
 
     if (listener->begin) {
         listener->begin(listener);
@@ -3067,10 +3123,28 @@  static void listener_del_address_space(MemoryListener *listener,
         if (fr->dirty_log_mask && listener->log_stop) {
             listener->log_stop(listener, &section, fr->dirty_log_mask, 0);
         }
+        flat_range_coalesced_io_notify_listener_add_del(fr, listener,
+                                                        as, false);
         if (listener->region_del) {
             listener->region_del(listener, &section);
         }
     }
+
+    /*
+     * de-register all eventfds for this address space for the current
+     * listener.
+     */
+    for (i = 0; i < as->ioeventfd_nb; i++) {
+        fd = &as->ioeventfds[i];
+        MemoryRegionSection section = (MemoryRegionSection) {
+            .fv = address_space_to_flatview(as),
+            .offset_within_address_space = int128_get64(fd->addr.start),
+            .size = fd->addr.size,
+        };
+        listener->eventfd_del(listener, &section,
+                              fd->match_data, fd->data, fd->e);
+    }
+
     if (listener->commit) {
         listener->commit(listener);
     }