Patchwork [v3,1/5] hostmem: make hostmem single, not per Vring related

login
register
mail settings
Submitter pingfan liu
Date May 6, 2013, 12:48 p.m.
Message ID <1367844540-9024-2-git-send-email-qemulist@gmail.com>
Download mbox | patch
Permalink /patch/241630/
State New
Headers show

Comments

pingfan liu - May 6, 2013, 12:48 p.m.
From: Liu Ping Fan <pingfank@linux.vnet.ibm.com>

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

Signed-off-by: Liu Ping Fan <pingfank@linux.vnet.ibm.com>
---
 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(-)

Patch

diff --git a/exec.c b/exec.c
index fa1e0c3..1ec36a9 100644
--- a/exec.c
+++ b/exec.c
@@ -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);
diff --git a/hw/virtio/dataplane/hostmem.c b/hw/virtio/dataplane/hostmem.c
index 37292ff..756b09f 100644
--- a/hw/virtio/dataplane/hostmem.c
+++ b/hw/virtio/dataplane/hostmem.c
@@ -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);
 }
diff --git a/hw/virtio/dataplane/vring.c b/hw/virtio/dataplane/vring.c
index e0d6e83..4d6d735 100644
--- a/hw/virtio/dataplane/vring.c
+++ b/hw/virtio/dataplane/vring.c
@@ -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",
diff --git a/include/hw/virtio/dataplane/hostmem.h b/include/hw/virtio/dataplane/hostmem.h
index b2cf093..7999991 100644
--- a/include/hw/virtio/dataplane/hostmem.h
+++ b/include/hw/virtio/dataplane/hostmem.h
@@ -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 */
diff --git a/include/hw/virtio/dataplane/vring.h b/include/hw/virtio/dataplane/vring.h
index 9380cb5..56acffb 100644
--- a/include/hw/virtio/dataplane/vring.h
+++ b/include/hw/virtio/dataplane/vring.h
@@ -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 */