Skip to contentSkip to conversation

system/physmem: Propagate AddressSpace to MapClient helpers

PATCH In review Philippe Mathieu-Daudé 3 participants 9 messages May 7, 2024

lore
Share & tools
lore
#0 lore raw

cover system/physmem: Propagate AddressSpace to MapClient helpers

Respin of Mattias patch [1 split to ease review.
Preliminary use QEMU_LOCK_GUARD to simplify.

I'm OK to include this and the endianness fix [2]
if Mattias agrees, once first patch is reviewed.

Regards,

Phil.

[1 https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/

Mattias Nissler (2):
system/physmem: Propagate AddressSpace to MapClient helpers system/physmem: Per-AddressSpace bounce buffering

Philippe Mathieu-Daudé (1):
system/physmem: Replace qemu_mutex_lock() calls with QEMU_LOCK_GUARD

 include/exec/cpu-common.h |  2 -
 include/exec/memory.h     | 45 +++++++++++++++++-
 system/dma-helpers.c      |  4 +-
 system/memory.c           |  7 +++
 system/physmem.c          | 98 +++++++++++++++------------------------
 5 files changed, 90 insertions(+), 66 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/3 system/physmem: Replace qemu_mutex_lock() calls with QEMU_LOCK_GUARD

Simplify cpu_[un]register_map_client() and cpu_notify_map_clients() by replacing the pair of qemu_mutex_lock/qemu_mutex_unlock calls by the WITH_QEMU_LOCK_GUARD() macro.

---
 system/physmem.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
system/physmem.c +3 -6
index d3a3d8a45c..5486014cf2 100644
@@ -3086,7 +3086,7 @@ void cpu_register_map_client(QEMUBH *bh)
 {
     MapClient *client = g_malloc(sizeof(*client));
 
-    qemu_mutex_lock(&map_client_list_lock);
+    QEMU_LOCK_GUARD(&map_client_list_lock);
     client->bh = bh;
     QLIST_INSERT_HEAD(&map_client_list, client, link);
     /* Write map_client_list before reading in_use.  */
@@ -3094,7 +3094,6 @@ void cpu_register_map_client(QEMUBH *bh)
     if (!qatomic_read(&bounce.in_use)) {
         cpu_notify_map_clients_locked();
     }
-    qemu_mutex_unlock(&map_client_list_lock);
 }
 
 void cpu_exec_init_all(void)
@@ -3117,21 +3116,19 @@ void cpu_unregister_map_client(QEMUBH *bh)
 {
     MapClient *client;
 
-    qemu_mutex_lock(&map_client_list_lock);
+    QEMU_LOCK_GUARD(&map_client_list_lock);
     QLIST_FOREACH(client, &map_client_list, link) {
         if (client->bh == bh) {
             cpu_unregister_map_client_do(client);
             break;
         }
     }
-    qemu_mutex_unlock(&map_client_list_lock);
 }
 
 static void cpu_notify_map_clients(void)
 {
-    qemu_mutex_lock(&map_client_list_lock);
+    QEMU_LOCK_GUARD(&map_client_list_lock);
     cpu_notify_map_clients_locked();
-    qemu_mutex_unlock(&map_client_list_lock);
 }
 
 static bool flatview_access_valid(FlatView *fv, hwaddr addr, hwaddr len,
Signature · 1 line
2.41.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] system/physmem: Replace qemu_mutex_lock() calls with QEMU_LOCK_GUARD

Quoted 8 lines · Philippe Mathieu-Daudé

Simplify cpu_[un]register_map_client() and cpu_notify_map_clients() by replacing the pair of qemu_mutex_lock/qemu_mutex_unlock calls by the WITH_QEMU_LOCK_GUARD() macro.

---
  system/physmem.c | 9 +++------
  1 file changed, 3 insertions(+), 6 deletions(-)

r~

#3 lore raw

PATCH 2/3 system/physmem: Propagate AddressSpace to MapClient helpers

In reply to Philippe Mathieu-Daudé

From: Mattias Nissler <[email protected]>

Propagate AddressSpace handler to following helpers:
- register_map_client()
- unregister_map_client()
- notify_map_clients[_locked]()

Rename them using 'address_space_' prefix instead of 'cpu_'.

The AddressSpace argument will be used in the next commit.

Message-ID: <[email protected]>

[PMD: Split patch, part 1/2]
---
 include/exec/cpu-common.h |  2 --
 include/exec/memory.h     | 26 ++++++++++++++++++++++++--
 system/dma-helpers.c      |  4 ++--
 system/physmem.c          | 24 ++++++++++++------------
 4 files changed, 38 insertions(+), 18 deletions(-)
include/exec/cpu-common.h +0 -2
index 8bc397e251..815342d043 100644
@@ -147,8 +147,6 @@ void *cpu_physical_memory_map(hwaddr addr,
                               bool is_write);
 void cpu_physical_memory_unmap(void *buffer, hwaddr len,
                                bool is_write, hwaddr access_len);
-void cpu_register_map_client(QEMUBH *bh);
-void cpu_unregister_map_client(QEMUBH *bh);
 
 bool cpu_physical_memory_is_io(hwaddr phys_addr);
 
include/exec/memory.h +24 -2
index dadb5cd65a..e1e0c5a3de 100644
@@ -2946,8 +2946,8 @@ bool address_space_access_valid(AddressSpace *as, hwaddr addr, hwaddr len,
  * May return %NULL and set *@plen to zero(0), if resources needed to perform
  * the mapping are exhausted.
  * Use only for reads OR writes - not for read-modify-write operations.
- * Use cpu_register_map_client() to know when retrying the map operation is
- * likely to succeed.
+ * Use address_space_register_map_client() to know when retrying the map
+ * operation is likely to succeed.
  *
  * @as: #AddressSpace to be accessed
  * @addr: address within that address space
@@ -2972,6 +2972,28 @@ void *address_space_map(AddressSpace *as, hwaddr addr,
 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
                          bool is_write, hwaddr access_len);
 
+/*
+ * address_space_register_map_client: Register a callback to invoke when
+ * resources for address_space_map() are available again.
+ *
+ * address_space_map may fail when there are not enough resources available,
+ * such as when bounce buffer memory would exceed the limit. The callback can
+ * be used to retry the address_space_map operation. Note that the callback
+ * gets automatically removed after firing.
+ *
+ * @as: #AddressSpace to be accessed
+ * @bh: callback to invoke when address_space_map() retry is appropriate
+ */
+void address_space_register_map_client(AddressSpace *as, QEMUBH *bh);
+
+/*
+ * address_space_unregister_map_client: Unregister a callback that has
+ * previously been registered and not fired yet.
+ *
+ * @as: #AddressSpace to be accessed
+ * @bh: callback to unregister
+ */
+void address_space_unregister_map_client(AddressSpace *as, QEMUBH *bh);
 
 /* Internal functions, part of the implementation of address_space_read.  */
 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
system/dma-helpers.c +2 -2
index 9b221cf94e..74013308f5 100644
@@ -169,7 +169,7 @@ static void dma_blk_cb(void *opaque, int ret)
     if (dbs->iov.size == 0) {
         trace_dma_map_wait(dbs);
         dbs->bh = aio_bh_new(ctx, reschedule_dma, dbs);
-        cpu_register_map_client(dbs->bh);
+        address_space_register_map_client(dbs->sg->as, dbs->bh);
         return;
     }
 
@@ -197,7 +197,7 @@ static void dma_aio_cancel(BlockAIOCB *acb)
     }
 
     if (dbs->bh) {
-        cpu_unregister_map_client(dbs->bh);
+        address_space_unregister_map_client(dbs->sg->as, dbs->bh);
         qemu_bh_delete(dbs->bh);
         dbs->bh = NULL;
     }
system/physmem.c +12 -12
index 5486014cf2..27e754ff57 100644
@@ -3065,24 +3065,24 @@ QemuMutex map_client_list_lock;
 static QLIST_HEAD(, MapClient) map_client_list
     = QLIST_HEAD_INITIALIZER(map_client_list);
 
-static void cpu_unregister_map_client_do(MapClient *client)
+static void address_space_unregister_map_client_do(MapClient *client)
 {
     QLIST_REMOVE(client, link);
     g_free(client);
 }
 
-static void cpu_notify_map_clients_locked(void)
+static void address_space_notify_map_clients_locked(AddressSpace *as)
 {
     MapClient *client;
 
     while (!QLIST_EMPTY(&map_client_list)) {
         client = QLIST_FIRST(&map_client_list);
         qemu_bh_schedule(client->bh);
-        cpu_unregister_map_client_do(client);
+        address_space_unregister_map_client_do(client);
     }
 }
 
-void cpu_register_map_client(QEMUBH *bh)
+void address_space_register_map_client(AddressSpace *as, QEMUBH *bh)
 {
     MapClient *client = g_malloc(sizeof(*client));
 
@@ -3092,7 +3092,7 @@ void cpu_register_map_client(QEMUBH *bh)
     /* Write map_client_list before reading in_use.  */
     smp_mb();
     if (!qatomic_read(&bounce.in_use)) {
-        cpu_notify_map_clients_locked();
+        address_space_notify_map_clients_locked(as);
     }
 }
 
@@ -3112,23 +3112,23 @@ void cpu_exec_init_all(void)
     qemu_mutex_init(&map_client_list_lock);
 }
 
-void cpu_unregister_map_client(QEMUBH *bh)
+void address_space_unregister_map_client(AddressSpace *as, QEMUBH *bh)
 {
     MapClient *client;
 
     QEMU_LOCK_GUARD(&map_client_list_lock);
     QLIST_FOREACH(client, &map_client_list, link) {
         if (client->bh == bh) {
-            cpu_unregister_map_client_do(client);
+            address_space_unregister_map_client_do(client);
             break;
         }
     }
 }
 
-static void cpu_notify_map_clients(void)
+static void address_space_notify_map_clients(AddressSpace *as)
 {
     QEMU_LOCK_GUARD(&map_client_list_lock);
-    cpu_notify_map_clients_locked();
+    address_space_notify_map_clients_locked(as);
 }
 
 static bool flatview_access_valid(FlatView *fv, hwaddr addr, hwaddr len,
@@ -3195,8 +3195,8 @@ flatview_extend_translation(FlatView *fv, hwaddr addr,
  * May map a subset of the requested range, given by and returned in *plen.
  * May return NULL if resources needed to perform the mapping are exhausted.
  * Use only for reads OR writes - not for read-modify-write operations.
- * Use cpu_register_map_client() to know when retrying the map operation is
- * likely to succeed.
+ * Use address_space_register_map_client() to know when retrying the map
+ * operation is likely to succeed.
  */
 void *address_space_map(AddressSpace *as,
                         hwaddr addr,
@@ -3279,7 +3279,7 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
     memory_region_unref(bounce.mr);
     /* Clear in_use before reading map_client_list.  */
     qatomic_set_mb(&bounce.in_use, false);
-    cpu_notify_map_clients();
+    address_space_notify_map_clients(as);
 }
 
 void *cpu_physical_memory_map(hwaddr addr,
Signature · 1 line
2.41.0
#4 lore raw

PATCH 3/3 system/physmem: Per-AddressSpace bounce buffering

In reply to Philippe Mathieu-Daudé

From: Mattias Nissler <[email protected]>

Instead of using a single global bounce buffer, give each AddressSpace its own bounce buffer. The MapClient callback mechanism moves to AddressSpace accordingly.

This is in preparation for generalizing bounce buffer handling further to allow multiple bounce buffers, with a total allocation limit configured per AddressSpace.

Message-ID: <[email protected]>

[PMD: Split patch, part 2/2]
---
 include/exec/memory.h | 19 +++++++++++
 system/memory.c       |  7 +++++
 system/physmem.c      | 73 ++++++++++++++++---------------------------
 3 files changed, 53 insertions(+), 46 deletions(-)
include/exec/memory.h +19 -0
index e1e0c5a3de..d417d7f363 100644
@@ -1112,6 +1112,19 @@ struct MemoryListener {
     QTAILQ_ENTRY(MemoryListener) link_as;
 };
 
+typedef struct AddressSpaceMapClient {
+    QEMUBH *bh;
+    QLIST_ENTRY(AddressSpaceMapClient) link;
+} AddressSpaceMapClient;
+
+typedef struct {
+    MemoryRegion *mr;
+    void *buffer;
+    hwaddr addr;
+    hwaddr len;
+    bool in_use;
+} BounceBuffer;
+
 /**
  * struct AddressSpace: describes a mapping of addresses to #MemoryRegion objects
  */
@@ -1129,6 +1142,12 @@ struct AddressSpace {
     struct MemoryRegionIoeventfd *ioeventfds;
     QTAILQ_HEAD(, MemoryListener) listeners;
     QTAILQ_ENTRY(AddressSpace) address_spaces_link;
+
+    /* Bounce buffer to use for this address space. */
+    BounceBuffer bounce;
+    /* List of callbacks to invoke when buffers free up */
+    QemuMutex map_client_list_lock;
+    QLIST_HEAD(, AddressSpaceMapClient) map_client_list;
 };
 
 typedef struct AddressSpaceDispatch AddressSpaceDispatch;
system/memory.c +7 -0
index 49f1cb2c38..642a449f8c 100644
@@ -3174,6 +3174,9 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
     as->ioeventfds = NULL;
     QTAILQ_INIT(&as->listeners);
     QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
+    as->bounce.in_use = false;
+    qemu_mutex_init(&as->map_client_list_lock);
+    QLIST_INIT(&as->map_client_list);
     as->name = g_strdup(name ? name : "anonymous");
     address_space_update_topology(as);
     address_space_update_ioeventfds(as);
@@ -3181,6 +3184,10 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
 
 static void do_address_space_destroy(AddressSpace *as)
 {
+    assert(!qatomic_read(&as->bounce.in_use));
+    assert(QLIST_EMPTY(&as->map_client_list));
+    qemu_mutex_destroy(&as->map_client_list_lock);
+
     assert(QTAILQ_EMPTY(&as->listeners));
 
     flatview_unref(as->current_map);
system/physmem.c +27 -46

145 lines not on this page · raw message

Signature · 1 line
2.41.0
author Reviewed-by on v1 3/3
#5 lore raw

Re: [PATCH 3/3] system/physmem: Per-AddressSpace bounce buffering

Quoted 13 lines · Philippe Mathieu-Daudé

From: Mattias Nissler <[email protected]>

Instead of using a single global bounce buffer, give each AddressSpace its own bounce buffer. The MapClient callback mechanism moves to AddressSpace accordingly.

This is in preparation for generalizing bounce buffer handling further to allow multiple bounce buffers, with a total allocation limit configured per AddressSpace.

Quoted 8 lines

Message-ID: <[email protected]>

[PMD: Split patch, part 2/2]
---
  include/exec/memory.h | 19 +++++++++++
  system/memory.c       |  7 +++++
  system/physmem.c      | 73 ++++++++++++++++---------------------------
  3 files changed, 53 insertions(+), 46 deletions(-)
#6 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Respin of Mattias patch [1 split to ease review.
Preliminary use QEMU_LOCK_GUARD to simplify.

I'm OK to include this and the endianness fix [2]
if Mattias agrees, once first patch is reviewed.

To be honest, given that this patch series has been lingering for almost a year now, I'm fine with whatever gets us closer to getting this landed. I believe Peter was also considering doing a pull request for the series, so you may want to coordinate with him if you haven't already.

Quoted 26 lines

Regards,

Phil.

[1 https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/

Mattias Nissler (2):
system/physmem: Propagate AddressSpace to MapClient helpers system/physmem: Per-AddressSpace bounce buffering

Philippe Mathieu-Daudé (1):
system/physmem: Replace qemu_mutex_lock() calls with QEMU_LOCK_GUARD

 include/exec/cpu-common.h |  2 -
 include/exec/memory.h     | 45 +++++++++++++++++-
 system/dma-helpers.c      |  4 +-
 system/memory.c           |  7 +++
 system/physmem.c          | 98 +++++++++++++++------------------------
 5 files changed, 90 insertions(+), 66 deletions(-)

--
2.41.0

#7 lore raw
Quoted 5 lines · Mattias Nissler
Quoted 4 lines · Philippe Mathieu-Daudé

Respin of Mattias patch [1 split to ease review.
Preliminary use QEMU_LOCK_GUARD to simplify.

Quoted 8 lines
Quoted 2 lines

I'm OK to include this and the endianness fix [2]
if Mattias agrees, once first patch is reviewed.

To be honest, given that this patch series has been lingering for almost a year now, I'm fine with whatever gets us closer to getting this landed. I believe Peter was also considering doing a pull request for the series, so you may want to coordinate with him if you haven't already.

Well I'm sorry, today is the first time I've been looking at it, and was trying to help reviewing. I see I was Cc'ed on earlier versions but missed them. OK, I'll see with Peter.

Phil.

#8 lore raw
Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Mattias Nissler
Quoted 4 lines · Philippe Mathieu-Daudé

Respin of Mattias patch [1 split to ease review.
Preliminary use QEMU_LOCK_GUARD to simplify.

Quoted 8 lines
Quoted 2 lines

I'm OK to include this and the endianness fix [2]
if Mattias agrees, once first patch is reviewed.

To be honest, given that this patch series has been lingering for almost a year now, I'm fine with whatever gets us closer to getting this landed. I believe Peter was also considering doing a pull request for the series, so you may want to coordinate with him if you haven't already.

Quoted 3 lines

Well I'm sorry, today is the first time I've been looking at it, and was trying to help reviewing. I see I was Cc'ed on earlier versions but missed them. OK, I'll see with Peter.

It's fine, sorry for being a bit negative.