Skip to contentSkip to conversation

proposal to make hostmem listener RAM unplug safe

PATCH New Liu Ping Fan 1 participant 6 messages May 6, 2013 Arm STM32

lore
Share & tools
lore
#0 lore raw

v3 cover proposal to make hostmem listener RAM unplug safe

Open issue:
As to [PATCH v3 2/5] hostmem: AddressSpace has its own map and maintained by RCU prepared style, Paolo may want to use memory_region_find() to re-implement hostmem. It is still under discussion

v2->v3:
1.Drop the memory region's ref in virtio-blk. Now all the reference are kept inside Vring.

v1->v2:
1.split RCU prepared style update and monitor the RAM-Device refcnt into two patches (patch 2,4) 2.introduce AddrSpaceMem, which is similar to HostMem, but based on address space, while
the original HostMem only server system memory address space

*** BLURB HERE ***

Liu Ping Fan (5):
hostmem: make hostmem single, not per Vring related
hostmem: AddressSpace has its own map and maintained by RCU prepared
style
memory: add ref/unref interface for MemroyRegionOps
hostmem: hostmem listener pin RAM-Device by refcnt
Vring: use hostmem's RAM safe api

 exec.c                                |    2 +
 hw/block/dataplane/virtio-blk.c       |    8 --
 hw/virtio/dataplane/hostmem.c         |  150 ++++++++++++++++++++++++---------
 hw/virtio/dataplane/vring.c           |   98 ++++++++++++++++-----
 include/exec/memory.h                 |   10 ++
 include/hw/virtio/dataplane/hostmem.h |   33 +++++---
 include/hw/virtio/dataplane/vring.h   |   16 ++++-
 memory.c                              |   18 ++++
 8 files changed, 251 insertions(+), 84 deletions(-)
Signature · 1 line
1.7.4.4
#1 lore raw

PATCH v3 1/5 hostmem: make hostmem single, not per Vring related

From: Liu Ping Fan <[email protected]>

The hwaddr and hva mapping relation is system wide, no need to be created for each Vring

---
 exec.c                                |    2 ++
 hw/virtio/dataplane/hostmem.c         |   33 +++++++++++++++++++--------------
 hw/virtio/dataplane/vring.c           |   11 ++++-------
 include/hw/virtio/dataplane/hostmem.h |   13 +++++++++----
 include/hw/virtio/dataplane/vring.h   |    1 -
 5 files changed, 34 insertions(+), 26 deletions(-)
exec.c +2 -0
index fa1e0c3..1ec36a9 100644
@@ -49,6 +49,7 @@
 #include "translate-all.h"
 
 #include "exec/memory-internal.h"
+#include "hw/virtio/dataplane/hostmem.h"
 
 //#define DEBUG_UNASSIGNED
 //#define DEBUG_SUBPAGE
@@ -1809,6 +1810,7 @@ static void memory_map_init(void)
     memory_listener_register(&core_memory_listener, &address_space_memory);
     memory_listener_register(&io_memory_listener, &address_space_io);
     memory_listener_register(&tcg_memory_listener, &address_space_memory);
+    hostmem_init();
 
     dma_context_init(&dma_context_memory, &address_space_memory,
                      NULL, NULL, NULL);
hw/virtio/dataplane/hostmem.c +19 -14
index 37292ff..756b09f 100644
@@ -14,6 +14,10 @@
 #include "exec/address-spaces.h"
 #include "hw/virtio/dataplane/hostmem.h"
 
+HostMem *system_mem;
+
+static void hostmem_finalize(void);
+
 static int hostmem_lookup_cmp(const void *phys_, const void *region_)
 {
     hwaddr phys = *(const hwaddr *)phys_;
@@ -31,11 +35,12 @@ static int hostmem_lookup_cmp(const void *phys_, const void *region_)
 /**
  * Map guest physical address to host pointer
  */
-void *hostmem_lookup(HostMem *hostmem, hwaddr phys, hwaddr len, bool is_write)
+void *hostmem_lookup(hwaddr phys, hwaddr len, bool is_write)
 {
     HostMemRegion *region;
     void *host_addr = NULL;
     hwaddr offset_within_region;
+    HostMem *hostmem = system_mem;
 
     qemu_mutex_lock(&hostmem->current_regions_lock);
     region = bsearch(&phys, hostmem->current_regions,
@@ -137,13 +142,12 @@ static void hostmem_listener_coalesced_mmio_dummy(MemoryListener *listener,
 {
 }
 
-void hostmem_init(HostMem *hostmem)
+void hostmem_init(void)
 {
-    memset(hostmem, 0, sizeof(*hostmem));
+    system_mem = g_new0(HostMem, 1);
+    qemu_mutex_init(&system_mem->current_regions_lock);
 
-    qemu_mutex_init(&hostmem->current_regions_lock);
-
-    hostmem->listener = (MemoryListener){
+    system_mem->listener = (MemoryListener) {
         .begin = hostmem_listener_dummy,
         .commit = hostmem_listener_commit,
         .region_add = hostmem_listener_append_region,
@@ -161,16 +165,17 @@ void hostmem_init(HostMem *hostmem)
         .priority = 10,
     };
 
-    memory_listener_register(&hostmem->listener, &address_space_memory);
-    if (hostmem->num_new_regions > 0) {
-        hostmem_listener_commit(&hostmem->listener);
+    memory_listener_register(&system_mem->listener, &address_space_memory);
+    if (system_mem->num_new_regions > 0) {
+        hostmem_listener_commit(&system_mem->listener);
     }
+    atexit(hostmem_finalize);
 }
 
-void hostmem_finalize(HostMem *hostmem)
+static void hostmem_finalize(void)
 {
-    memory_listener_unregister(&hostmem->listener);
-    g_free(hostmem->new_regions);
-    g_free(hostmem->current_regions);
-    qemu_mutex_destroy(&hostmem->current_regions_lock);
+    memory_listener_unregister(&system_mem->listener);
+    g_free(system_mem->new_regions);
+    g_free(system_mem->current_regions);
+    qemu_mutex_destroy(&system_mem->current_regions_lock);
 }
hw/virtio/dataplane/vring.c +4 -7
index e0d6e83..4d6d735 100644
@@ -27,8 +27,7 @@ bool vring_setup(Vring *vring, VirtIODevice *vdev, int n)
 
     vring->broken = false;
 
-    hostmem_init(&vring->hostmem);
-    vring_ptr = hostmem_lookup(&vring->hostmem, vring_addr, vring_size, true);
+    vring_ptr = hostmem_lookup(vring_addr, vring_size, true);
     if (!vring_ptr) {
         error_report("Failed to map vring "
                      "addr %#" HWADDR_PRIx " size %" HWADDR_PRIu,
@@ -51,7 +50,6 @@ bool vring_setup(Vring *vring, VirtIODevice *vdev, int n)
 
 void vring_teardown(Vring *vring)
 {
-    hostmem_finalize(&vring->hostmem);
 }
 
 /* Disable guest->host notifies */
@@ -138,8 +136,7 @@ static int get_indirect(Vring *vring,
         struct vring_desc *desc_ptr;
 
         /* Translate indirect descriptor */
-        desc_ptr = hostmem_lookup(&vring->hostmem,
-                                  indirect->addr + found * sizeof(desc),
+        desc_ptr = hostmem_lookup(indirect->addr + found * sizeof(desc),
                                   sizeof(desc), false);
         if (!desc_ptr) {
             error_report("Failed to map indirect descriptor "
@@ -172,7 +169,7 @@ static int get_indirect(Vring *vring,
             return -ENOBUFS;
         }
 
-        iov->iov_base = hostmem_lookup(&vring->hostmem, desc.addr, desc.len,
+        iov->iov_base = hostmem_lookup(desc.addr, desc.len,
                                        desc.flags & VRING_DESC_F_WRITE);
         if (!iov->iov_base) {
             error_report("Failed to map indirect descriptor"
@@ -300,7 +297,7 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,
         }
 
         /* TODO handle non-contiguous memory across region boundaries */
-        iov->iov_base = hostmem_lookup(&vring->hostmem, desc.addr, desc.len,
+        iov->iov_base = hostmem_lookup(desc.addr, desc.len,
                                        desc.flags & VRING_DESC_F_WRITE);
         if (!iov->iov_base) {
             error_report("Failed to map vring desc addr %#" PRIx64 " len %u",
include/hw/virtio/dataplane/hostmem.h +9 -4
index b2cf093..7999991 100644
@@ -14,6 +14,7 @@
 #ifndef HOSTMEM_H
 #define HOSTMEM_H
 
+#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
 #include "exec/memory.h"
 #include "qemu/thread.h"
 
@@ -41,9 +42,6 @@ typedef struct {
     size_t num_current_regions;
 } HostMem;
 
-void hostmem_init(HostMem *hostmem);
-void hostmem_finalize(HostMem *hostmem);
-
 /**
  * Map a guest physical address to a pointer
  *
@@ -52,6 +50,13 @@ void hostmem_finalize(HostMem *hostmem);
  * can be done with other mechanisms like bdrv_drain_all() that quiesce
  * in-flight I/O.
  */
-void *hostmem_lookup(HostMem *hostmem, hwaddr phys, hwaddr len, bool is_write);
+void *hostmem_lookup(hwaddr phys, hwaddr len, bool is_write);
+void hostmem_init(void);
+
+#else
+void hostmem_init(void)
+{
+}
+#endif
 
 #endif /* HOSTMEM_H */
include/hw/virtio/dataplane/vring.h +0 -1
index 9380cb5..56acffb 100644
@@ -23,7 +23,6 @@
 #include "hw/virtio/virtio.h"
 
 typedef struct {
-    HostMem hostmem;                /* guest memory mapper */
     struct vring vr;                /* virtqueue vring mapped to host memory */
     uint16_t last_avail_idx;        /* last processed avail ring index */
     uint16_t last_used_idx;         /* last processed used ring index */
Signature · 1 line
1.7.4.4
#2 lore raw

PATCH v3 2/5 hostmem: AddressSpace has its own map and maintained by RCU prepared style

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Each address space will have a map between its hwaddr and hva, this is expressed as the struct AddrSpaceMem. Currently only address space of system memory's map is used by virtio device, and the map is stored in AddrSpaceMem *system_mem.

The map is maintained by RCU prepared style, cur_hostmem, next_hostmem, cur_lock fields in AddrSpaceMem help to access the aim. cur_hostmem is used to search, next_hostmem is used to update and will substitue cur_hostmem when done.

---
 hw/virtio/dataplane/hostmem.c         |  133 +++++++++++++++++++++++----------
 include/hw/virtio/dataplane/hostmem.h |   20 +++--
 2 files changed, 103 insertions(+), 50 deletions(-)
hw/virtio/dataplane/hostmem.c +92 -41
index 756b09f..f31a703 100644
@@ -14,7 +14,7 @@
 #include "exec/address-spaces.h"
 #include "hw/virtio/dataplane/hostmem.h"
 
-HostMem *system_mem;
+static AddrSpaceMem *system_mem;
 
 static void hostmem_finalize(void);
 
@@ -32,17 +32,39 @@ static int hostmem_lookup_cmp(const void *phys_, const void *region_)
     }
 }
 
-/**
- * Map guest physical address to host pointer
- */
-void *hostmem_lookup(hwaddr phys, hwaddr len, bool is_write)
+static void hostmem_ref(HostMem *hostmem)
+{
+    int t;
+
+    t = __sync_add_and_fetch(&hostmem->ref, 1);
+    assert(t > 0);
+}
+
+static void hostmem_unref(HostMem *hostmem)
+{
+    int t;
+
+    t = __sync_sub_and_fetch(&hostmem->ref, 1);
+    assert(t >= 0);
+    if (!t) {
+        g_free(hostmem->current_regions);
+        g_free(hostmem);
+    }
+}
+
+static void *address_space_mem_lookup(AddrSpaceMem *as_mem, hwaddr phys,
+    hwaddr len, bool is_write)
 {
     HostMemRegion *region;
     void *host_addr = NULL;
     hwaddr offset_within_region;
-    HostMem *hostmem = system_mem;
+    HostMem *hostmem;
+
+    qemu_mutex_lock(&as_mem->cur_lock);
+    hostmem = as_mem->cur_hostmem;
+    hostmem_ref(hostmem);
+    qemu_mutex_unlock(&as_mem->cur_lock);
 
-    qemu_mutex_lock(&hostmem->current_regions_lock);
     region = bsearch(&phys, hostmem->current_regions,
                      hostmem->num_current_regions,
                      sizeof(hostmem->current_regions[0]),
@@ -57,28 +79,45 @@ void *hostmem_lookup(hwaddr phys, hwaddr len, bool is_write)
     if (len <= region->size - offset_within_region) {
         host_addr = region->host_addr + offset_within_region;
     }
-out:
-    qemu_mutex_unlock(&hostmem->current_regions_lock);
 
+out:
+    hostmem_unref(hostmem);
     return host_addr;
 }
 
 /**
- * Install new regions list
+ * Map guest physical address to host pointer
  */
-static void hostmem_listener_commit(MemoryListener *listener)
+void *hostmem_lookup(hwaddr phys, hwaddr len, bool is_write)
 {
-    HostMem *hostmem = container_of(listener, HostMem, listener);
+    return address_space_mem_lookup(system_mem, phys, len, is_write);
+}
 
-    qemu_mutex_lock(&hostmem->current_regions_lock);
-    g_free(hostmem->current_regions);
-    hostmem->current_regions = hostmem->new_regions;
-    hostmem->num_current_regions = hostmem->num_new_regions;
-    qemu_mutex_unlock(&hostmem->current_regions_lock);
+static void hostmem_listener_begin(MemoryListener *listener)
+{
+    AddrSpaceMem *as_mem = container_of(listener, AddrSpaceMem, listener);
+
+    as_mem->next_hostmem = g_new0(HostMem, 1);
+    as_mem->next_hostmem->ref = 1;
+}
 
-    /* Reset new regions list */
-    hostmem->new_regions = NULL;
-    hostmem->num_new_regions = 0;
+/**
+ * Install new regions list
+ */
+static void hostmem_listener_commit(MemoryListener *listener)
+{
+    HostMem *tmp;
+    AddrSpaceMem *as_mem = container_of(listener, AddrSpaceMem, listener);
+
+    /* writer of cur_hostmem &next_hostmem is serialized by biglock
+     * in hotplug path. So only take care of r/w on cur_hostmem
+     */
+    tmp = as_mem->cur_hostmem;
+    qemu_mutex_lock(&as_mem->cur_lock);
+    as_mem->cur_hostmem = as_mem->next_hostmem;
+    qemu_mutex_unlock(&as_mem->cur_lock);
+    as_mem->next_hostmem = NULL;
+    hostmem_unref(tmp);
 }
 
 /**
@@ -88,23 +127,23 @@ static void hostmem_append_new_region(HostMem *hostmem,
                                       MemoryRegionSection *section)
 {
     void *ram_ptr = memory_region_get_ram_ptr(section->mr);
-    size_t num = hostmem->num_new_regions;
-    size_t new_size = (num + 1) * sizeof(hostmem->new_regions[0]);
+    size_t num = hostmem->num_current_regions;
+    size_t new_size = (num + 1) * sizeof(hostmem->current_regions[0]);
 
-    hostmem->new_regions = g_realloc(hostmem->new_regions, new_size);
-    hostmem->new_regions[num] = (HostMemRegion){
+    hostmem->current_regions = g_realloc(hostmem->current_regions, new_size);
+    hostmem->current_regions[num] = (HostMemRegion){
         .host_addr = ram_ptr + section->offset_within_region,
         .guest_addr = section->offset_within_address_space,
         .size = section->size,
         .readonly = section->readonly,
     };
-    hostmem->num_new_regions++;
+    hostmem->num_current_regions++;
 }
 
 static void hostmem_listener_append_region(MemoryListener *listener,
                                            MemoryRegionSection *section)
 {
-    HostMem *hostmem = container_of(listener, HostMem, listener);
+    AddrSpaceMem *as_mem = container_of(listener, AddrSpaceMem, listener);
 
     /* Ignore non-RAM regions, we may not be able to map them */
     if (!memory_region_is_ram(section->mr)) {
@@ -116,7 +155,7 @@ static void hostmem_listener_append_region(MemoryListener *listener,
         return;
     }
 
-    hostmem_append_new_region(hostmem, section);
+    hostmem_append_new_region(as_mem->next_hostmem, section);
 }
 
 /* We don't implement most MemoryListener callbacks, use these nop stubs */
@@ -142,13 +181,13 @@ static void hostmem_listener_coalesced_mmio_dummy(MemoryListener *listener,
 {
 }
 
-void hostmem_init(void)
+static AddrSpaceMem *address_space_mem_create(AddressSpace *as)
 {
-    system_mem = g_new0(HostMem, 1);
-    qemu_mutex_init(&system_mem->current_regions_lock);
+    AddrSpaceMem *as_mem = g_new0(AddrSpaceMem, 1);
 
-    system_mem->listener = (MemoryListener) {
-        .begin = hostmem_listener_dummy,
+    qemu_mutex_init(&as_mem->cur_lock);
+    as_mem->listener = (MemoryListener) {
+        .begin = hostmem_listener_begin,
         .commit = hostmem_listener_commit,
         .region_add = hostmem_listener_append_region,
         .region_del = hostmem_listener_section_dummy,
@@ -164,18 +203,30 @@ void hostmem_init(void)
         .coalesced_mmio_del = hostmem_listener_coalesced_mmio_dummy,
         .priority = 10,
     };
+    as_mem->cur_hostmem = g_new0(HostMem, 1);
+    as_mem->cur_hostmem->ref = 1;
+    memory_listener_register(&as_mem->listener, as);
 
-    memory_listener_register(&system_mem->listener, &address_space_memory);
-    if (system_mem->num_new_regions > 0) {
-        hostmem_listener_commit(&system_mem->listener);
-    }
+    return as_mem;
+}
+
+static void address_space_mem_destroy(AddrSpaceMem *as_mem)
+{
+    memory_listener_unregister(&as_mem->listener);
+    qemu_mutex_destroy(&as_mem->cur_lock);
+    hostmem_unref(as_mem->cur_hostmem);
+    assert(!as_mem->next_hostmem);
+    g_free(as_mem);
+}
+
+void hostmem_init(void)
+{
+    system_mem = address_space_mem_create(&address_space_memory);
     atexit(hostmem_finalize);
 }
 
-static void hostmem_finalize(void)
+void hostmem_finalize(void)
 {
-    memory_listener_unregister(&system_mem->listener);
-    g_free(system_mem->new_regions);
-    g_free(system_mem->current_regions);
-    qemu_mutex_destroy(&system_mem->current_regions_lock);
+    address_space_mem_destroy(system_mem);
+    system_mem = NULL;
 }
include/hw/virtio/dataplane/hostmem.h +11 -9
index 7999991..d04ce55 100644
@@ -29,19 +29,21 @@ typedef struct {
     /* The listener is invoked when regions change and a new list of regions is
      * built up completely before they are installed.
      */
-    MemoryListener listener;
-    HostMemRegion *new_regions;
-    size_t num_new_regions;
-
-    /* Current regions are accessed from multiple threads either to lookup
-     * addresses or to install a new list of regions.  The lock protects the
-     * pointer and the regions.
-     */
-    QemuMutex current_regions_lock;
+    int ref;
     HostMemRegion *current_regions;
     size_t num_current_regions;
 } HostMem;
 
+/* Helper to map address space's hw_addr to hva */
+typedef struct {
+    /* lock to protect the r/w access to cur_hostmem */
+    QemuMutex cur_lock;
+    /* switch from cur_hostmem to next_hostmem to adopt RCU */
+    HostMem *cur_hostmem;
+    HostMem *next_hostmem;
+    MemoryListener listener;
+} AddrSpaceMem;
+
 /**
  * Map a guest physical address to a pointer
  *
Signature · 1 line
1.7.4.4
#3 lore raw

PATCH v3 3/5 memory: add ref/unref interface for MemroyRegionOps

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

This pair of interface are optinal, except for those device which is used outside the biglock's protection for hot unplug. Currently, HostMem used by virtio-blk dataplane is outside biglock, so the RAM device should implement this.

---
 include/exec/memory.h |   10 ++++++++++
 memory.c              |   18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)
include/exec/memory.h +10 -0
index 9e88320..7e38fc1 100644
@@ -54,6 +54,12 @@ struct MemoryRegionIORange {
  * Memory region callbacks
  */
 struct MemoryRegionOps {
+
+    /* ref/unref pair is optional;  ref.
+     * inc refcnt of object who store MemoryRegion
+     */
+    void (*ref)(void);
+    void (*unref)(void);
     /* Read from the memory region. @addr is relative to @mr; @size is
      * in bytes. */
     uint64_t (*read)(void *opaque,
@@ -223,6 +229,10 @@ struct MemoryListener {
     QTAILQ_ENTRY(MemoryListener) link;
 };
 
+/**/
+bool memory_region_ref(MemoryRegion *mr);
+bool memory_region_unref(MemoryRegion *mr);
+
 /**
  * memory_region_init: Initialize a memory region
  *
memory.c +18 -0
index 75ca281..c29998d 100644
@@ -786,6 +786,24 @@ static bool memory_region_wrong_endianness(MemoryRegion *mr)
 #endif
 }
 
+bool memory_region_ref(MemoryRegion *mr)
+{
+    if (mr->ops && mr->ops->ref) {
+        mr->ops->ref();
+        return true;
+    }
+    return false;
+}
+
+bool memory_region_unref(MemoryRegion *mr)
+{
+    if (mr->ops && mr->ops->unref) {
+        mr->ops->unref();
+        return true;
+    }
+    return false;
+}
+
 void memory_region_init(MemoryRegion *mr,
                         const char *name,
                         uint64_t size)
Signature · 1 line
1.7.4.4
#4 lore raw

PATCH v3 4/5 hostmem: hostmem listener pin RAM-Device by refcnt

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

With ref()/unref() interface of MemoryRegion, we can pin RAM-Device when using its memory, and release it when done.

---
 hw/virtio/dataplane/hostmem.c         |   24 +++++++++++++++++++-----
 hw/virtio/dataplane/vring.c           |    8 ++++----
 include/hw/virtio/dataplane/hostmem.h |    4 +++-
 3 files changed, 26 insertions(+), 10 deletions(-)
hw/virtio/dataplane/hostmem.c +19 -5
index f31a703..420c9be 100644
@@ -42,18 +42,23 @@ static void hostmem_ref(HostMem *hostmem)
 
 static void hostmem_unref(HostMem *hostmem)
 {
-    int t;
+    int i, t;
+    HostMemRegion *hmr;
 
     t = __sync_sub_and_fetch(&hostmem->ref, 1);
     assert(t >= 0);
     if (!t) {
+        for (i = 0; i < hostmem->num_current_regions; i++) {
+            hmr = &hostmem->current_regions[i];
+            memory_region_unref(hmr->mr);
+        }
         g_free(hostmem->current_regions);
         g_free(hostmem);
     }
 }
 
 static void *address_space_mem_lookup(AddrSpaceMem *as_mem, hwaddr phys,
-    hwaddr len, bool is_write)
+    hwaddr len, MemoryRegion **mr, bool is_write)
 {
     HostMemRegion *region;
     void *host_addr = NULL;
@@ -65,6 +70,9 @@ static void *address_space_mem_lookup(AddrSpaceMem *as_mem, hwaddr phys,
     hostmem_ref(hostmem);
     qemu_mutex_unlock(&as_mem->cur_lock);
 
+    if (mr) {
+        *mr = NULL;
+    }
     region = bsearch(&phys, hostmem->current_regions,
                      hostmem->num_current_regions,
                      sizeof(hostmem->current_regions[0]),
@@ -79,7 +87,10 @@ static void *address_space_mem_lookup(AddrSpaceMem *as_mem, hwaddr phys,
     if (len <= region->size - offset_within_region) {
         host_addr = region->host_addr + offset_within_region;
     }
-
+    if (mr) {
+        *mr = region->mr;
+        memory_region_ref(*mr);
+    }
 out:
     hostmem_unref(hostmem);
     return host_addr;
@@ -88,9 +99,10 @@ out:
 /**
  * Map guest physical address to host pointer
  */
-void *hostmem_lookup(hwaddr phys, hwaddr len, bool is_write)
+void *hostmem_lookup(hwaddr phys, hwaddr len, MemoryRegion **mr,
+    bool is_write)
 {
-    return address_space_mem_lookup(system_mem, phys, len, is_write);
+    return address_space_mem_lookup(system_mem, phys, len, mr, is_write);
 }
 
 static void hostmem_listener_begin(MemoryListener *listener)
@@ -134,6 +146,7 @@ static void hostmem_append_new_region(HostMem *hostmem,
     hostmem->current_regions[num] = (HostMemRegion){
         .host_addr = ram_ptr + section->offset_within_region,
         .guest_addr = section->offset_within_address_space,
+        .mr = section->mr,
         .size = section->size,
         .readonly = section->readonly,
     };
@@ -155,6 +168,7 @@ static void hostmem_listener_append_region(MemoryListener *listener,
         return;
     }
 
+    memory_region_ref(section->mr);
     hostmem_append_new_region(as_mem->next_hostmem, section);
 }
 
hw/virtio/dataplane/vring.c +4 -4
index 4d6d735..e3c3afb 100644
@@ -27,7 +27,7 @@ bool vring_setup(Vring *vring, VirtIODevice *vdev, int n)
 
     vring->broken = false;
 
-    vring_ptr = hostmem_lookup(vring_addr, vring_size, true);
+    vring_ptr = hostmem_lookup(vring_addr, vring_size, NULL, true);
     if (!vring_ptr) {
         error_report("Failed to map vring "
                      "addr %#" HWADDR_PRIx " size %" HWADDR_PRIu,
@@ -137,7 +137,7 @@ static int get_indirect(Vring *vring,
 
         /* Translate indirect descriptor */
         desc_ptr = hostmem_lookup(indirect->addr + found * sizeof(desc),
-                                  sizeof(desc), false);
+                                  sizeof(desc), NULL, false);
         if (!desc_ptr) {
             error_report("Failed to map indirect descriptor "
                          "addr %#" PRIx64 " len %zu",
@@ -169,7 +169,7 @@ static int get_indirect(Vring *vring,
             return -ENOBUFS;
         }
 
-        iov->iov_base = hostmem_lookup(desc.addr, desc.len,
+        iov->iov_base = hostmem_lookup(desc.addr, desc.len, NULL,
                                        desc.flags & VRING_DESC_F_WRITE);
         if (!iov->iov_base) {
             error_report("Failed to map indirect descriptor"
@@ -297,7 +297,7 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,
         }
 
         /* TODO handle non-contiguous memory across region boundaries */
-        iov->iov_base = hostmem_lookup(desc.addr, desc.len,
+        iov->iov_base = hostmem_lookup(desc.addr, desc.len, NULL,
                                        desc.flags & VRING_DESC_F_WRITE);
         if (!iov->iov_base) {
             error_report("Failed to map vring desc addr %#" PRIx64 " len %u",
include/hw/virtio/dataplane/hostmem.h +3 -1
index d04ce55..094102b 100644
@@ -21,6 +21,7 @@
 typedef struct {
     void *host_addr;
     hwaddr guest_addr;
+    MemoryRegion *mr;
     uint64_t size;
     bool readonly;
 } HostMemRegion;
@@ -52,7 +53,8 @@ typedef struct {
  * can be done with other mechanisms like bdrv_drain_all() that quiesce
  * in-flight I/O.
  */
-void *hostmem_lookup(hwaddr phys, hwaddr len, bool is_write);
+void *hostmem_lookup(hwaddr phys, hwaddr len, MemoryRegion **mr,
+    bool is_write);
 void hostmem_init(void);
 
 #else
Signature · 1 line
1.7.4.4
#5 lore raw

PATCH v3 5/5 Vring: use hostmem's RAM safe api

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Before mm-ops done, we should gurantee the validaion of regions which is used by Vring self and the chunck pointed by vring desc. We acheive this goal by inc refcnt of RAM-Device. When finished, we dec this cnt through the interface of MemoryRegion.

We keep the MemoryRegion's reference info totally in Vring, so the caller can not be aware of the reference.

---
 hw/block/dataplane/virtio-blk.c     |    8 ---
 hw/virtio/dataplane/vring.c         |   93 +++++++++++++++++++++++++++-------
 include/hw/virtio/dataplane/vring.h |   15 ++++++
 3 files changed, 89 insertions(+), 27 deletions(-)
hw/block/dataplane/virtio-blk.c +0 -8
index 0356665..4babda1 100644
@@ -25,14 +25,6 @@
 #include "block/aio.h"
 #include "hw/virtio/virtio-bus.h"
 
-enum {
-    SEG_MAX = 126,                  /* maximum number of I/O segments */
-    VRING_MAX = SEG_MAX + 2,        /* maximum number of vring descriptors */
-    REQ_MAX = VRING_MAX,            /* maximum number of requests in the vring,
-                                     * is VRING_MAX / 2 with traditional and
-                                     * VRING_MAX with indirect descriptors */
-};
-
 typedef struct {
     struct iocb iocb;               /* Linux AIO control block */
     QEMUIOVector *inhdr;            /* iovecs for virtio_blk_inhdr */
hw/virtio/dataplane/vring.c +74 -19

240 lines not on this page · raw message

include/hw/virtio/dataplane/vring.h +15 -0
index 56acffb..88dac68 100644
@@ -22,13 +22,28 @@
 #include "hostmem.h"
 #include "hw/virtio/virtio.h"
 
+enum {
+    SEG_MAX = 126,                  /* maximum number of I/O segments */
+    VRING_MAX = SEG_MAX + 2,        /* maximum number of vring descriptors */
+    REQ_MAX = VRING_MAX,            /* maximum number of requests in the vring,
+                                     * is VRING_MAX / 2 with traditional and
+                                     * VRING_MAX with indirect descriptors */
+};
+
+typedef struct {
+    int used_cnt;
+    MemoryRegion **mrs;
+} MrsVect;
+
 typedef struct {
+    MemoryRegion *vring_mr;   /* RAM's memoryRegion on which this vring sits */
     struct vring vr;                /* virtqueue vring mapped to host memory */
     uint16_t last_avail_idx;        /* last processed avail ring index */
     uint16_t last_used_idx;         /* last processed used ring index */
     uint16_t signalled_used;        /* EVENT_IDX state */
     bool signalled_used_valid;
     bool broken;                    /* was there a fatal error? */
+    MrsVect mrs_vect[VRING_MAX];
 } Vring;
 
 static inline unsigned int vring_get_num(Vring *vring)
Signature · 1 line
1.7.4.4