Skip to contentSkip to conversation

virtio: shmem map transaction cleanup

PATCH In review Albert Esteve 1 participant 4 messages Jul 7 – Jul 16

lore
Share & tools
lore
#0 lore raw

cover virtio: shmem map transaction cleanup

This series is a follow-up cleanup to [1], which introduced the VHOST_USER_BACKEND_SHMEM_MAP request handler in vhost-user.c, and addresses suggestions raised during its review.

The first patch wraps the raw memory_region_transaction_begin()/commit() calls to hide implementation details behind dedicated helpers (see [2]).

The second patch builds on those helpes by replacing the manual begin/end calls with a g_autoptr guard that commits the transaction automatically on all exit paths, removing the send_reply_commit label which became redundant as a result.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Albert Esteve (2):
virtio: add shmem map transaction wrappers
virtio: use g_autoptr guard for shmem map transaction

 hw/virtio/vhost-user.c     | 21 ++++-----------------
 hw/virtio/virtio.c         | 12 ++++++++++++
 include/hw/virtio/virtio.h | 29 +++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 17 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/2 virtio: add shmem map transaction wrappers

Introduce virtio_add_shmem_map_start() and virtio_add_shmem_map_end() as wrappers around memory_region_transaction_begin() and memory_region_transaction_commit(). This keeps memory subsystem calls out of vhost-user.c and gives the transaction pair a name that reflects its purpose: delaying the commit until after a reply has been sent to the vhost-user backend.

---
 hw/virtio/vhost-user.c     |  6 +++---
 hw/virtio/virtio.c         | 10 ++++++++++
 include/hw/virtio/virtio.h | 17 +++++++++++++++++
 3 files changed, 30 insertions(+), 3 deletions(-)
hw/virtio/vhost-user.c +3 -3
index 517cc4ca71..f6bf9e0ba4 100644
@@ -1977,7 +1977,7 @@ vhost_user_backend_handle_shmem_map(struct vhost_dev *dev,
         }
     }
 
-    memory_region_transaction_begin();
+    virtio_add_shmem_map_start();
 
     /* Create VirtioSharedMemoryMapping object */
     VirtioSharedMemoryMapping *mapping = virtio_shared_memory_mapping_new(
@@ -2004,11 +2004,11 @@ send_reply_commit:
         hdr->size = sizeof(payload->u64);
         if (!vhost_user_send_resp(ioc, hdr, payload, &local_err)) {
             error_report_err(local_err);
-            memory_region_transaction_commit();
+            virtio_add_shmem_map_end();
             return -EFAULT;
         }
     }
-    memory_region_transaction_commit();
+    virtio_add_shmem_map_end();
     return 0;
 
 send_reply:
hw/virtio/virtio.c +10 -0
index f4d86a3655..e604781ffe 100644
@@ -3226,6 +3226,16 @@ VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid,
     return mapping;
 }
 
+void virtio_add_shmem_map_start(void)
+{
+    memory_region_transaction_begin();
+}
+
+void virtio_add_shmem_map_end(void)
+{
+    memory_region_transaction_commit();
+}
+
 int virtio_add_shmem_map(VirtioSharedMemory *shmem,
                          VirtioSharedMemoryMapping *mapping)
 {
include/hw/virtio/virtio.h +17 -0
index c99cb19d88..9935452486 100644
@@ -390,6 +390,23 @@ VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid,
                                                             uint64_t len,
                                                             bool allow_write);
 
+/**
+ * virtio_add_shmem_map_start() - Begin a shmem map transaction
+ *
+ * Begins a transaction to add a shared memory mapping. Call this before adding
+ * a shared memory mapping when the commit must be delayed until after a reply
+ * is sent to a vhost-user backend.
+ */
+void virtio_add_shmem_map_start(void);
+
+/**
+ * virtio_add_shmem_map_end() - End a shmem map transaction
+ *
+ * Completes the transaction started by virtio_add_shmem_map_start() and makes
+ * the mapping visible to the guest.
+ */
+void virtio_add_shmem_map_end(void);
+
 /**
  * virtio_add_shmem_map() - Add a memory mapping to a shared region
  * @shmem: VirtioSharedMemory region
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/2 virtio: use g_autoptr guard for shmem map transaction

In reply to Albert Esteve

Use g_autoptr(VirtioShmemGuard) to automatically commit the memory region transaction opened by virtio_add_shmem_map_start(), removing explicit virtio_add_shmem_map_end() calls on each exit path of vhost_user_backend_handle_shmem_map(). This also allows the send_reply_commit label to be folded into send_reply since the two blocks are now identical.

VirtioShmemGuard is a sentinel type whose non-NULL value triggers the cleanup function on scope exit via GLib's g_autoptr infrastructure, similar to those used in main-loop.h and rcu.h.

---
 hw/virtio/vhost-user.c     | 21 ++++-----------------
 hw/virtio/virtio.c         |  4 +++-
 include/hw/virtio/virtio.h | 16 ++++++++++++++--
 3 files changed, 21 insertions(+), 20 deletions(-)
hw/virtio/vhost-user.c +4 -17
index f6bf9e0ba4..0306b06b48 100644
@@ -1933,6 +1933,7 @@ vhost_user_backend_handle_shmem_map(struct vhost_dev *dev,
                                     VhostUserPayload *payload,
                                     int fd)
 {
+    g_autoptr(VirtioShmemGuard) shmem_guard = NULL;
     VirtioSharedMemory *shmem;
     VhostUserMMap *vu_mmap = &payload->mmap;
     VirtioSharedMemoryMapping *existing;
@@ -1977,7 +1978,7 @@ vhost_user_backend_handle_shmem_map(struct vhost_dev *dev,
         }
     }
 
-    virtio_add_shmem_map_start();
+    shmem_guard = virtio_add_shmem_map_start();
 
     /* Create VirtioSharedMemoryMapping object */
     VirtioSharedMemoryMapping *mapping = virtio_shared_memory_mapping_new(
@@ -1986,7 +1987,7 @@ vhost_user_backend_handle_shmem_map(struct vhost_dev *dev,
 
     if (!mapping) {
         ret = -EFAULT;
-        goto send_reply_commit;
+        goto send_reply;
     }
 
     /* Add the mapping to the shared memory region */
@@ -1994,22 +1995,8 @@ vhost_user_backend_handle_shmem_map(struct vhost_dev *dev,
         error_report("Failed to add shared memory mapping");
         object_unref(OBJECT(mapping));
         ret = -EFAULT;
-        goto send_reply_commit;
-    }
-
-send_reply_commit:
-    /* Send reply and commit after transaction started */
-    if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) {
-        payload->u64 = !!ret;
-        hdr->size = sizeof(payload->u64);
-        if (!vhost_user_send_resp(ioc, hdr, payload, &local_err)) {
-            error_report_err(local_err);
-            virtio_add_shmem_map_end();
-            return -EFAULT;
-        }
+        goto send_reply;
     }
-    virtio_add_shmem_map_end();
-    return 0;
 
 send_reply:
     if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) {
hw/virtio/virtio.c +3 -1
index e604781ffe..1db800fcd7 100644
@@ -3226,9 +3226,11 @@ VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid,
     return mapping;
 }
 
-void virtio_add_shmem_map_start(void)
+VirtioShmemGuard *virtio_add_shmem_map_start(void)
 {
     memory_region_transaction_begin();
+    /* Anything non-NULL causes the cleanup function to be called */
+    return (VirtioShmemGuard *)(uintptr_t)1;
 }
 
 void virtio_add_shmem_map_end(void)
include/hw/virtio/virtio.h +14 -2
index 9935452486..b9e67f789c 100644
@@ -104,6 +104,8 @@ enum virtio_device_endian {
 #define TYPE_VIRTIO_SHARED_MEMORY_MAPPING "virtio-shared-memory-mapping"
 OBJECT_DECLARE_SIMPLE_TYPE(VirtioSharedMemoryMapping, VIRTIO_SHARED_MEMORY_MAPPING)
 
+typedef void VirtioShmemGuard;
+
 /**
  * VirtioSharedMemoryMapping:
  * @parent: Parent QOM object
@@ -396,17 +398,27 @@ VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid,
  * Begins a transaction to add a shared memory mapping. Call this before adding
  * a shared memory mapping when the commit must be delayed until after a reply
  * is sent to a vhost-user backend.
+ *
+ * Returns: Non-NULL guard that triggers cleanup on scope exit.
  */
-void virtio_add_shmem_map_start(void);
+VirtioShmemGuard *virtio_add_shmem_map_start(void);
 
 /**
  * virtio_add_shmem_map_end() - End a shmem map transaction
  *
  * Completes the transaction started by virtio_add_shmem_map_start() and makes
- * the mapping visible to the guest.
+ * the mapping visible to the guest. Prefer using g_autoptr(VirtioShmemGuard)
+ * over calling this directly.
  */
 void virtio_add_shmem_map_end(void);
 
+static inline void virtio_shmem_guard_cleanup(VirtioShmemGuard *guard)
+{
+    virtio_add_shmem_map_end();
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(VirtioShmemGuard, virtio_shmem_guard_cleanup)
+
 /**
  * virtio_add_shmem_map() - Add a memory mapping to a shared region
  * @shmem: VirtioSharedMemory region
Signature · 1 line
2.54.0
#3 lore raw

Re: [PATCH 2/2] virtio: use g_autoptr guard for shmem map transaction

Quoted 11 lines · Albert Esteve

Use g_autoptr(VirtioShmemGuard) to automatically commit the memory region transaction opened by virtio_add_shmem_map_start(), removing explicit virtio_add_shmem_map_end() calls on each exit path of vhost_user_backend_handle_shmem_map(). This also allows the send_reply_commit label to be folded into send_reply since the two blocks are now identical.

VirtioShmemGuard is a sentinel type whose non-NULL value triggers the cleanup function on scope exit via GLib's g_autoptr infrastructure, similar to those used in main-loop.h and rcu.h.

This patch is now superseded by
https://lore.kernel.org/all/[email protected]/

I will drop this patch from the series and send the single remaining patch in v2.

Quoted 125 lines
---
 hw/virtio/vhost-user.c     | 21 ++++-----------------
 hw/virtio/virtio.c         |  4 +++-
 include/hw/virtio/virtio.h | 16 ++++++++++++++--
 3 files changed, 21 insertions(+), 20 deletions(-)
hw/virtio/vhost-user.c +4 -17
index f6bf9e0ba4..0306b06b48 100644
@@ -1933,6 +1933,7 @@ vhost_user_backend_handle_shmem_map(struct vhost_dev *dev,
                                     VhostUserPayload *payload,
                                     int fd)
 {
+    g_autoptr(VirtioShmemGuard) shmem_guard = NULL;
     VirtioSharedMemory *shmem;
     VhostUserMMap *vu_mmap = &payload->mmap;
     VirtioSharedMemoryMapping *existing;
@@ -1977,7 +1978,7 @@ vhost_user_backend_handle_shmem_map(struct vhost_dev *dev,
         }
     }
 
-    virtio_add_shmem_map_start();
+    shmem_guard = virtio_add_shmem_map_start();
 
     /* Create VirtioSharedMemoryMapping object */
     VirtioSharedMemoryMapping *mapping = virtio_shared_memory_mapping_new(
@@ -1986,7 +1987,7 @@ vhost_user_backend_handle_shmem_map(struct vhost_dev *dev,
 
     if (!mapping) {
         ret = -EFAULT;
-        goto send_reply_commit;
+        goto send_reply;
     }
 
     /* Add the mapping to the shared memory region */
@@ -1994,22 +1995,8 @@ vhost_user_backend_handle_shmem_map(struct vhost_dev *dev,
         error_report("Failed to add shared memory mapping");
         object_unref(OBJECT(mapping));
         ret = -EFAULT;
-        goto send_reply_commit;
-    }
-
-send_reply_commit:
-    /* Send reply and commit after transaction started */
-    if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) {
-        payload->u64 = !!ret;
-        hdr->size = sizeof(payload->u64);
-        if (!vhost_user_send_resp(ioc, hdr, payload, &local_err)) {
-            error_report_err(local_err);
-            virtio_add_shmem_map_end();
-            return -EFAULT;
-        }
+        goto send_reply;
     }
-    virtio_add_shmem_map_end();
-    return 0;
 
 send_reply:
     if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) {
hw/virtio/virtio.c +3 -1
index e604781ffe..1db800fcd7 100644
@@ -3226,9 +3226,11 @@ VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid,
     return mapping;
 }
 
-void virtio_add_shmem_map_start(void)
+VirtioShmemGuard *virtio_add_shmem_map_start(void)
 {
     memory_region_transaction_begin();
+    /* Anything non-NULL causes the cleanup function to be called */
+    return (VirtioShmemGuard *)(uintptr_t)1;
 }
 
 void virtio_add_shmem_map_end(void)
include/hw/virtio/virtio.h +14 -3
index 9935452486..b9e67f789c 100644
@@ -104,6 +104,8 @@ enum virtio_device_endian {
 #define TYPE_VIRTIO_SHARED_MEMORY_MAPPING "virtio-shared-memory-mapping"
 OBJECT_DECLARE_SIMPLE_TYPE(VirtioSharedMemoryMapping, VIRTIO_SHARED_MEMORY_MAPPING)
 
+typedef void VirtioShmemGuard;
+
 /**
  * VirtioSharedMemoryMapping:
  * @parent: Parent QOM object
@@ -396,17 +398,27 @@ VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid,
  * Begins a transaction to add a shared memory mapping. Call this before adding
  * a shared memory mapping when the commit must be delayed until after a reply
  * is sent to a vhost-user backend.
+ *
+ * Returns: Non-NULL guard that triggers cleanup on scope exit.
  */
-void virtio_add_shmem_map_start(void);
+VirtioShmemGuard *virtio_add_shmem_map_start(void);
 
 /**
  * virtio_add_shmem_map_end() - End a shmem map transaction
  *
  * Completes the transaction started by virtio_add_shmem_map_start() and makes
- * the mapping visible to the guest.
+ * the mapping visible to the guest. Prefer using g_autoptr(VirtioShmemGuard)
+ * over calling this directly.
  */
 void virtio_add_shmem_map_end(void);
 
+static inline void virtio_shmem_guard_cleanup(VirtioShmemGuard *guard)
+{
+    virtio_add_shmem_map_end();
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(VirtioShmemGuard, virtio_shmem_guard_cleanup)
+
 /**
  * virtio_add_shmem_map() - Add a memory mapping to a shared region
  * @shmem: VirtioSharedMemory region
--

2.54.0