Skip to contentSkip to conversation

vhost: used_memslots refactoring

PATCH Superseded Jay Zhou 3 participants 11 messages Mar 5, 2018 – Mar 20, 2018

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Jay Zhou

author
#0 lore raw

PATCH v9 1/1 vhost: used_memslots refactoring

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-kernel respectively.

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

---
 hw/virtio/vhost-backend.c         | 15 +++++++-
 hw/virtio/vhost-user.c            | 77 ++++++++++++++++++++++++++-------------
 hw/virtio/vhost.c                 | 13 +++----
 include/hw/virtio/vhost-backend.h |  6 ++-
 4 files changed, 75 insertions(+), 36 deletions(-)
hw/virtio/vhost-backend.c +14 -1
index 7f09efa..59def69 100644
@@ -15,6 +15,8 @@
 #include "hw/virtio/vhost-backend.h"
 #include "qemu/error-report.h"
 
+static unsigned int vhost_kernel_used_memslots;
+
 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
                              void *arg)
 {
@@ -62,6 +64,11 @@ static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
     return limit;
 }
 
+static bool vhost_kernel_has_free_memslots(struct vhost_dev *dev)
+{
+    return vhost_kernel_used_memslots < vhost_kernel_memslots_limit(dev);
+}
+
 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
                                         struct vhost_vring_file *file)
 {
@@ -233,11 +240,16 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,
         qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL);
 }
 
+static void vhost_kernel_set_used_memslots(struct vhost_dev *dev)
+{
+    vhost_kernel_used_memslots = dev->mem->nregions;
+}
+
 static const VhostOps kernel_ops = {
         .backend_type = VHOST_BACKEND_TYPE_KERNEL,
         .vhost_backend_init = vhost_kernel_init,
         .vhost_backend_cleanup = vhost_kernel_cleanup,
-        .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
+        .vhost_backend_has_free_memslots = vhost_kernel_has_free_memslots,
         .vhost_net_set_backend = vhost_kernel_net_set_backend,
         .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
         .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint,
@@ -264,6 +276,7 @@ static const VhostOps kernel_ops = {
 #endif /* CONFIG_VHOST_VSOCK */
         .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback,
         .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg,
+        .vhost_set_used_memslots = vhost_kernel_set_used_memslots,
 };
 
 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
hw/virtio/vhost-user.c +51 -26

124 lines not on this page · raw message

hw/virtio/vhost.c +6 -7
index d8d0ef9..17262d2 100644
@@ -44,20 +44,19 @@
 static struct vhost_log *vhost_log;
 static struct vhost_log *vhost_log_shm;
 
-static unsigned int used_memslots;
 static QLIST_HEAD(, vhost_dev) vhost_devices =
     QLIST_HEAD_INITIALIZER(vhost_devices);
 
 bool vhost_has_free_slot(void)
 {
-    unsigned int slots_limit = ~0U;
     struct vhost_dev *hdev;
 
     QLIST_FOREACH(hdev, &vhost_devices, entry) {
-        unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
-        slots_limit = MIN(slots_limit, r);
+        if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
+            return false;
+        }
     }
-    return slots_limit > used_memslots;
+    return true;
 }
 
 static void vhost_dev_sync_region(struct vhost_dev *dev,
@@ -446,7 +445,6 @@ static void vhost_commit(MemoryListener *listener)
                        dev->n_mem_sections * sizeof dev->mem->regions[0];
     dev->mem = g_realloc(dev->mem, regions_size);
     dev->mem->nregions = dev->n_mem_sections;
-    used_memslots = dev->mem->nregions;
     for (i = 0; i < dev->n_mem_sections; i++) {
         struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
         struct MemoryRegionSection *mrs = dev->mem_sections + i;
@@ -458,6 +456,7 @@ static void vhost_commit(MemoryListener *listener)
             mrs->offset_within_region;
         cur_vmr->flags_padding   = 0;
     }
+    dev->vhost_ops->vhost_set_used_memslots(dev);
 
     if (!dev->started) {
         goto out;
@@ -1202,7 +1201,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     memory_listener_register(&hdev->memory_listener, &address_space_memory);
     QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
 
-    if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) {
+    if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
         error_report("vhost backend memory slots limit is less"
                 " than current number of present memory slots");
         r = -1;
include/hw/virtio/vhost-backend.h +4 -2
index 5dac61f..ff06c2d 100644
@@ -36,7 +36,7 @@ struct vhost_iotlb_msg;
 
 typedef int (*vhost_backend_init)(struct vhost_dev *dev, void *opaque);
 typedef int (*vhost_backend_cleanup)(struct vhost_dev *dev);
-typedef int (*vhost_backend_memslots_limit)(struct vhost_dev *dev);
+typedef bool (*vhost_backend_has_free_memslots)(struct vhost_dev *dev);
 
 typedef int (*vhost_net_set_backend_op)(struct vhost_dev *dev,
                                 struct vhost_vring_file *file);
@@ -94,6 +94,7 @@ typedef int (*vhost_set_config_op)(struct vhost_dev *dev, const uint8_t *data,
                                    uint32_t flags);
 typedef int (*vhost_get_config_op)(struct vhost_dev *dev, uint8_t *config,
                                    uint32_t config_len);
+typedef void (*vhost_set_used_memslots_op)(struct vhost_dev *dev);
 
 typedef int (*vhost_crypto_create_session_op)(struct vhost_dev *dev,
                                               void *session_info,
@@ -105,7 +106,7 @@ typedef struct VhostOps {
     VhostBackendType backend_type;
     vhost_backend_init vhost_backend_init;
     vhost_backend_cleanup vhost_backend_cleanup;
-    vhost_backend_memslots_limit vhost_backend_memslots_limit;
+    vhost_backend_has_free_memslots vhost_backend_has_free_memslots;
     vhost_net_set_backend_op vhost_net_set_backend;
     vhost_net_set_mtu_op vhost_net_set_mtu;
     vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint;
@@ -136,6 +137,7 @@ typedef struct VhostOps {
     vhost_send_device_iotlb_msg_op vhost_send_device_iotlb_msg;
     vhost_get_config_op vhost_get_config;
     vhost_set_config_op vhost_set_config;
+    vhost_set_used_memslots_op vhost_set_used_memslots;
     vhost_crypto_create_session_op vhost_crypto_create_session;
     vhost_crypto_close_session_op vhost_crypto_close_session;
 } VhostOps;
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 18 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-kernel respectively.

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

Quoted 1 line

Igor - does you ack still stand?

Quoted 289 lines
---
 hw/virtio/vhost-backend.c         | 15 +++++++-
 hw/virtio/vhost-user.c            | 77 ++++++++++++++++++++++++++-------------
 hw/virtio/vhost.c                 | 13 +++----
 include/hw/virtio/vhost-backend.h |  6 ++-
 4 files changed, 75 insertions(+), 36 deletions(-)
hw/virtio/vhost-backend.c +14 -1
index 7f09efa..59def69 100644
@@ -15,6 +15,8 @@
 #include "hw/virtio/vhost-backend.h"
 #include "qemu/error-report.h"
 
+static unsigned int vhost_kernel_used_memslots;
+
 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
                              void *arg)
 {
@@ -62,6 +64,11 @@ static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
     return limit;
 }
 
+static bool vhost_kernel_has_free_memslots(struct vhost_dev *dev)
+{
+    return vhost_kernel_used_memslots < vhost_kernel_memslots_limit(dev);
+}
+
 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
                                         struct vhost_vring_file *file)
 {
@@ -233,11 +240,16 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,
         qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL);
 }
 
+static void vhost_kernel_set_used_memslots(struct vhost_dev *dev)
+{
+    vhost_kernel_used_memslots = dev->mem->nregions;
+}
+
 static const VhostOps kernel_ops = {
         .backend_type = VHOST_BACKEND_TYPE_KERNEL,
         .vhost_backend_init = vhost_kernel_init,
         .vhost_backend_cleanup = vhost_kernel_cleanup,
-        .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
+        .vhost_backend_has_free_memslots = vhost_kernel_has_free_memslots,
         .vhost_net_set_backend = vhost_kernel_net_set_backend,
         .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
         .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint,
@@ -264,6 +276,7 @@ static const VhostOps kernel_ops = {
 #endif /* CONFIG_VHOST_VSOCK */
         .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback,
         .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg,
+        .vhost_set_used_memslots = vhost_kernel_set_used_memslots,
 };
 
 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
hw/virtio/vhost-user.c +51 -26
index 41ff5cf..ef14249 100644
@@ -163,6 +163,8 @@ static VhostUserMsg m __attribute__ ((unused));
 /* The version of the protocol we support */
 #define VHOST_USER_VERSION    (0x1)
 
+static bool vhost_user_free_memslots = true;
+
 struct vhost_user {
     CharBackend *chr;
     int slave_fd;
@@ -330,12 +332,43 @@ static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,
     return 0;
 }
 
+static int vhost_user_prepare_msg(struct vhost_dev *dev, VhostUserMemory *mem,
+                                  int *fds)
+{
+    int i, fd;
+
+    vhost_user_free_memslots = true;
+    for (i = 0, mem->nregions = 0; i < dev->mem->nregions; ++i) {
+        struct vhost_memory_region *reg = dev->mem->regions + i;
+        ram_addr_t offset;
+        MemoryRegion *mr;
+
+        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
+        mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
+                                     &offset);
+        fd = memory_region_get_fd(mr);
+        if (fd > 0) {
+            if (mem->nregions == VHOST_MEMORY_MAX_NREGIONS) {
+                vhost_user_free_memslots = false;
+                return -1;
+            }
+
+            mem->regions[mem->nregions].userspace_addr = reg->userspace_addr;
+            mem->regions[mem->nregions].memory_size = reg->memory_size;
+            mem->regions[mem->nregions].guest_phys_addr = reg->guest_phys_addr;
+            mem->regions[mem->nregions].mmap_offset = offset;
+            fds[mem->nregions++] = fd;
+        }
+    }
+
+    return 0;
+}
+
 static int vhost_user_set_mem_table(struct vhost_dev *dev,
                                     struct vhost_memory *mem)
 {
     int fds[VHOST_MEMORY_MAX_NREGIONS];
-    int i, fd;
-    size_t fd_num = 0;
+    size_t fd_num;
     bool reply_supported = virtio_has_feature(dev->protocol_features,
                                               VHOST_USER_PROTOCOL_F_REPLY_ACK);
 
@@ -348,29 +381,12 @@ static int vhost_user_set_mem_table(struct vhost_dev *dev,
         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
     }
 
-    for (i = 0; i < dev->mem->nregions; ++i) {
-        struct vhost_memory_region *reg = dev->mem->regions + i;
-        ram_addr_t offset;
-        MemoryRegion *mr;
-
-        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
-        mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
-                                     &offset);
-        fd = memory_region_get_fd(mr);
-        if (fd > 0) {
-            if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
-                error_report("Failed preparing vhost-user memory table msg");
-                return -1;
-            }
-            msg.payload.memory.regions[fd_num].userspace_addr = reg->userspace_addr;
-            msg.payload.memory.regions[fd_num].memory_size  = reg->memory_size;
-            msg.payload.memory.regions[fd_num].guest_phys_addr = reg->guest_phys_addr;
-            msg.payload.memory.regions[fd_num].mmap_offset = offset;
-            fds[fd_num++] = fd;
-        }
+    if (vhost_user_prepare_msg(dev, &msg.payload.memory, fds) < 0) {
+        error_report("Failed preparing vhost-user memory table msg");
+        return -1;
     }
 
-    msg.payload.memory.nregions = fd_num;
+    fd_num = msg.payload.memory.nregions;
 
     if (!fd_num) {
         error_report("Failed initializing vhost-user memory map, "
@@ -886,9 +902,9 @@ static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_user_memslots_limit(struct vhost_dev *dev)
+static bool vhost_user_has_free_memslots(struct vhost_dev *dev)
 {
-    return VHOST_MEMORY_MAX_NREGIONS;
+    return vhost_user_free_memslots;
 }
 
 static bool vhost_user_requires_shm_log(struct vhost_dev *dev)
@@ -1156,11 +1172,19 @@ vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id)
     return 0;
 }
 
+static void vhost_user_set_used_memslots(struct vhost_dev *dev)
+{
+    int fds[VHOST_MEMORY_MAX_NREGIONS];
+    VhostUserMsg msg;
+
+    vhost_user_prepare_msg(dev, &msg.payload.memory, fds);
+}
+
 const VhostOps user_ops = {
         .backend_type = VHOST_BACKEND_TYPE_USER,
         .vhost_backend_init = vhost_user_init,
         .vhost_backend_cleanup = vhost_user_cleanup,
-        .vhost_backend_memslots_limit = vhost_user_memslots_limit,
+        .vhost_backend_has_free_memslots = vhost_user_has_free_memslots,
         .vhost_set_log_base = vhost_user_set_log_base,
         .vhost_set_mem_table = vhost_user_set_mem_table,
         .vhost_set_vring_addr = vhost_user_set_vring_addr,
@@ -1184,6 +1208,7 @@ const VhostOps user_ops = {
         .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg,
         .vhost_get_config = vhost_user_get_config,
         .vhost_set_config = vhost_user_set_config,
+        .vhost_set_used_memslots = vhost_user_set_used_memslots,
         .vhost_crypto_create_session = vhost_user_crypto_create_session,
         .vhost_crypto_close_session = vhost_user_crypto_close_session,
 };
hw/virtio/vhost.c +6 -7
index d8d0ef9..17262d2 100644
@@ -44,20 +44,19 @@
 static struct vhost_log *vhost_log;
 static struct vhost_log *vhost_log_shm;
 
-static unsigned int used_memslots;
 static QLIST_HEAD(, vhost_dev) vhost_devices =
     QLIST_HEAD_INITIALIZER(vhost_devices);
 
 bool vhost_has_free_slot(void)
 {
-    unsigned int slots_limit = ~0U;
     struct vhost_dev *hdev;
 
     QLIST_FOREACH(hdev, &vhost_devices, entry) {
-        unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
-        slots_limit = MIN(slots_limit, r);
+        if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
+            return false;
+        }
     }
-    return slots_limit > used_memslots;
+    return true;
 }
 
 static void vhost_dev_sync_region(struct vhost_dev *dev,
@@ -446,7 +445,6 @@ static void vhost_commit(MemoryListener *listener)
                        dev->n_mem_sections * sizeof dev->mem->regions[0];
     dev->mem = g_realloc(dev->mem, regions_size);
     dev->mem->nregions = dev->n_mem_sections;
-    used_memslots = dev->mem->nregions;
     for (i = 0; i < dev->n_mem_sections; i++) {
         struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
         struct MemoryRegionSection *mrs = dev->mem_sections + i;
@@ -458,6 +456,7 @@ static void vhost_commit(MemoryListener *listener)
             mrs->offset_within_region;
         cur_vmr->flags_padding   = 0;
     }
+    dev->vhost_ops->vhost_set_used_memslots(dev);
 
     if (!dev->started) {
         goto out;
@@ -1202,7 +1201,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     memory_listener_register(&hdev->memory_listener, &address_space_memory);
     QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
 
-    if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) {
+    if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
         error_report("vhost backend memory slots limit is less"
                 " than current number of present memory slots");
         r = -1;
include/hw/virtio/vhost-backend.h +4 -2
index 5dac61f..ff06c2d 100644
@@ -36,7 +36,7 @@ struct vhost_iotlb_msg;
 
 typedef int (*vhost_backend_init)(struct vhost_dev *dev, void *opaque);
 typedef int (*vhost_backend_cleanup)(struct vhost_dev *dev);
-typedef int (*vhost_backend_memslots_limit)(struct vhost_dev *dev);
+typedef bool (*vhost_backend_has_free_memslots)(struct vhost_dev *dev);
 
 typedef int (*vhost_net_set_backend_op)(struct vhost_dev *dev,
                                 struct vhost_vring_file *file);
@@ -94,6 +94,7 @@ typedef int (*vhost_set_config_op)(struct vhost_dev *dev, const uint8_t *data,
                                    uint32_t flags);
 typedef int (*vhost_get_config_op)(struct vhost_dev *dev, uint8_t *config,
                                    uint32_t config_len);
+typedef void (*vhost_set_used_memslots_op)(struct vhost_dev *dev);
 
 typedef int (*vhost_crypto_create_session_op)(struct vhost_dev *dev,
                                               void *session_info,
@@ -105,7 +106,7 @@ typedef struct VhostOps {
     VhostBackendType backend_type;
     vhost_backend_init vhost_backend_init;
     vhost_backend_cleanup vhost_backend_cleanup;
-    vhost_backend_memslots_limit vhost_backend_memslots_limit;
+    vhost_backend_has_free_memslots vhost_backend_has_free_memslots;
     vhost_net_set_backend_op vhost_net_set_backend;
     vhost_net_set_mtu_op vhost_net_set_mtu;
     vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint;
@@ -136,6 +137,7 @@ typedef struct VhostOps {
     vhost_send_device_iotlb_msg_op vhost_send_device_iotlb_msg;
     vhost_get_config_op vhost_get_config;
     vhost_set_config_op vhost_set_config;
+    vhost_set_used_memslots_op vhost_set_used_memslots;
     vhost_crypto_create_session_op vhost_crypto_create_session;
     vhost_crypto_close_session_op vhost_crypto_close_session;
 } VhostOps;
Signature · 1 line
1.8.3.1
Acked-by
#2 lore raw
Quoted 19 lines · On Mon, 5 Mar 2018 17:37:42 +0200 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 18 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-kernel respectively.

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

Quoted 3 lines
Quoted 1 line

Igor - does you ack still stand?

Rebased version looks fine so:

Quoted 290 lines
Quoted 289 lines
---
 hw/virtio/vhost-backend.c         | 15 +++++++-
 hw/virtio/vhost-user.c            | 77 ++++++++++++++++++++++++++-------------
 hw/virtio/vhost.c                 | 13 +++----
 include/hw/virtio/vhost-backend.h |  6 ++-
 4 files changed, 75 insertions(+), 36 deletions(-)
hw/virtio/vhost-backend.c +14 -1
index 7f09efa..59def69 100644
@@ -15,6 +15,8 @@
 #include "hw/virtio/vhost-backend.h"
 #include "qemu/error-report.h"
 
+static unsigned int vhost_kernel_used_memslots;
+
 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
                              void *arg)
 {
@@ -62,6 +64,11 @@ static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
     return limit;
 }
 
+static bool vhost_kernel_has_free_memslots(struct vhost_dev *dev)
+{
+    return vhost_kernel_used_memslots < vhost_kernel_memslots_limit(dev);
+}
+
 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
                                         struct vhost_vring_file *file)
 {
@@ -233,11 +240,16 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,
         qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL);
 }
 
+static void vhost_kernel_set_used_memslots(struct vhost_dev *dev)
+{
+    vhost_kernel_used_memslots = dev->mem->nregions;
+}
+
 static const VhostOps kernel_ops = {
         .backend_type = VHOST_BACKEND_TYPE_KERNEL,
         .vhost_backend_init = vhost_kernel_init,
         .vhost_backend_cleanup = vhost_kernel_cleanup,
-        .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
+        .vhost_backend_has_free_memslots = vhost_kernel_has_free_memslots,
         .vhost_net_set_backend = vhost_kernel_net_set_backend,
         .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
         .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint,
@@ -264,6 +276,7 @@ static const VhostOps kernel_ops = {
 #endif /* CONFIG_VHOST_VSOCK */
         .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback,
         .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg,
+        .vhost_set_used_memslots = vhost_kernel_set_used_memslots,
 };
 
 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
hw/virtio/vhost-user.c +51 -26
index 41ff5cf..ef14249 100644
@@ -163,6 +163,8 @@ static VhostUserMsg m __attribute__ ((unused));
 /* The version of the protocol we support */
 #define VHOST_USER_VERSION    (0x1)
 
+static bool vhost_user_free_memslots = true;
+
 struct vhost_user {
     CharBackend *chr;
     int slave_fd;
@@ -330,12 +332,43 @@ static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,
     return 0;
 }
 
+static int vhost_user_prepare_msg(struct vhost_dev *dev, VhostUserMemory *mem,
+                                  int *fds)
+{
+    int i, fd;
+
+    vhost_user_free_memslots = true;
+    for (i = 0, mem->nregions = 0; i < dev->mem->nregions; ++i) {
+        struct vhost_memory_region *reg = dev->mem->regions + i;
+        ram_addr_t offset;
+        MemoryRegion *mr;
+
+        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
+        mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
+                                     &offset);
+        fd = memory_region_get_fd(mr);
+        if (fd > 0) {
+            if (mem->nregions == VHOST_MEMORY_MAX_NREGIONS) {
+                vhost_user_free_memslots = false;
+                return -1;
+            }
+
+            mem->regions[mem->nregions].userspace_addr = reg->userspace_addr;
+            mem->regions[mem->nregions].memory_size = reg->memory_size;
+            mem->regions[mem->nregions].guest_phys_addr = reg->guest_phys_addr;
+            mem->regions[mem->nregions].mmap_offset = offset;
+            fds[mem->nregions++] = fd;
+        }
+    }
+
+    return 0;
+}
+
 static int vhost_user_set_mem_table(struct vhost_dev *dev,
                                     struct vhost_memory *mem)
 {
     int fds[VHOST_MEMORY_MAX_NREGIONS];
-    int i, fd;
-    size_t fd_num = 0;
+    size_t fd_num;
     bool reply_supported = virtio_has_feature(dev->protocol_features,
                                               VHOST_USER_PROTOCOL_F_REPLY_ACK);
 
@@ -348,29 +381,12 @@ static int vhost_user_set_mem_table(struct vhost_dev *dev,
         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
     }
 
-    for (i = 0; i < dev->mem->nregions; ++i) {
-        struct vhost_memory_region *reg = dev->mem->regions + i;
-        ram_addr_t offset;
-        MemoryRegion *mr;
-
-        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
-        mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
-                                     &offset);
-        fd = memory_region_get_fd(mr);
-        if (fd > 0) {
-            if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
-                error_report("Failed preparing vhost-user memory table msg");
-                return -1;
-            }
-            msg.payload.memory.regions[fd_num].userspace_addr = reg->userspace_addr;
-            msg.payload.memory.regions[fd_num].memory_size  = reg->memory_size;
-            msg.payload.memory.regions[fd_num].guest_phys_addr = reg->guest_phys_addr;
-            msg.payload.memory.regions[fd_num].mmap_offset = offset;
-            fds[fd_num++] = fd;
-        }
+    if (vhost_user_prepare_msg(dev, &msg.payload.memory, fds) < 0) {
+        error_report("Failed preparing vhost-user memory table msg");
+        return -1;
     }
 
-    msg.payload.memory.nregions = fd_num;
+    fd_num = msg.payload.memory.nregions;
 
     if (!fd_num) {
         error_report("Failed initializing vhost-user memory map, "
@@ -886,9 +902,9 @@ static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_user_memslots_limit(struct vhost_dev *dev)
+static bool vhost_user_has_free_memslots(struct vhost_dev *dev)
 {
-    return VHOST_MEMORY_MAX_NREGIONS;
+    return vhost_user_free_memslots;
 }
 
 static bool vhost_user_requires_shm_log(struct vhost_dev *dev)
@@ -1156,11 +1172,19 @@ vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id)
     return 0;
 }
 
+static void vhost_user_set_used_memslots(struct vhost_dev *dev)
+{
+    int fds[VHOST_MEMORY_MAX_NREGIONS];
+    VhostUserMsg msg;
+
+    vhost_user_prepare_msg(dev, &msg.payload.memory, fds);
+}
+
 const VhostOps user_ops = {
         .backend_type = VHOST_BACKEND_TYPE_USER,
         .vhost_backend_init = vhost_user_init,
         .vhost_backend_cleanup = vhost_user_cleanup,
-        .vhost_backend_memslots_limit = vhost_user_memslots_limit,
+        .vhost_backend_has_free_memslots = vhost_user_has_free_memslots,
         .vhost_set_log_base = vhost_user_set_log_base,
         .vhost_set_mem_table = vhost_user_set_mem_table,
         .vhost_set_vring_addr = vhost_user_set_vring_addr,
@@ -1184,6 +1208,7 @@ const VhostOps user_ops = {
         .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg,
         .vhost_get_config = vhost_user_get_config,
         .vhost_set_config = vhost_user_set_config,
+        .vhost_set_used_memslots = vhost_user_set_used_memslots,
         .vhost_crypto_create_session = vhost_user_crypto_create_session,
         .vhost_crypto_close_session = vhost_user_crypto_close_session,
 };
hw/virtio/vhost.c +6 -7
index d8d0ef9..17262d2 100644
@@ -44,20 +44,19 @@
 static struct vhost_log *vhost_log;
 static struct vhost_log *vhost_log_shm;
 
-static unsigned int used_memslots;
 static QLIST_HEAD(, vhost_dev) vhost_devices =
     QLIST_HEAD_INITIALIZER(vhost_devices);
 
 bool vhost_has_free_slot(void)
 {
-    unsigned int slots_limit = ~0U;
     struct vhost_dev *hdev;
 
     QLIST_FOREACH(hdev, &vhost_devices, entry) {
-        unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
-        slots_limit = MIN(slots_limit, r);
+        if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
+            return false;
+        }
     }
-    return slots_limit > used_memslots;
+    return true;
 }
 
 static void vhost_dev_sync_region(struct vhost_dev *dev,
@@ -446,7 +445,6 @@ static void vhost_commit(MemoryListener *listener)
                        dev->n_mem_sections * sizeof dev->mem->regions[0];
     dev->mem = g_realloc(dev->mem, regions_size);
     dev->mem->nregions = dev->n_mem_sections;
-    used_memslots = dev->mem->nregions;
     for (i = 0; i < dev->n_mem_sections; i++) {
         struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
         struct MemoryRegionSection *mrs = dev->mem_sections + i;
@@ -458,6 +456,7 @@ static void vhost_commit(MemoryListener *listener)
             mrs->offset_within_region;
         cur_vmr->flags_padding   = 0;
     }
+    dev->vhost_ops->vhost_set_used_memslots(dev);
 
     if (!dev->started) {
         goto out;
@@ -1202,7 +1201,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     memory_listener_register(&hdev->memory_listener, &address_space_memory);
     QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
 
-    if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) {
+    if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
         error_report("vhost backend memory slots limit is less"
                 " than current number of present memory slots");
         r = -1;
include/hw/virtio/vhost-backend.h +4 -2
index 5dac61f..ff06c2d 100644
@@ -36,7 +36,7 @@ struct vhost_iotlb_msg;
 
 typedef int (*vhost_backend_init)(struct vhost_dev *dev, void *opaque);
 typedef int (*vhost_backend_cleanup)(struct vhost_dev *dev);
-typedef int (*vhost_backend_memslots_limit)(struct vhost_dev *dev);
+typedef bool (*vhost_backend_has_free_memslots)(struct vhost_dev *dev);
 
 typedef int (*vhost_net_set_backend_op)(struct vhost_dev *dev,
                                 struct vhost_vring_file *file);
@@ -94,6 +94,7 @@ typedef int (*vhost_set_config_op)(struct vhost_dev *dev, const uint8_t *data,
                                    uint32_t flags);
 typedef int (*vhost_get_config_op)(struct vhost_dev *dev, uint8_t *config,
                                    uint32_t config_len);
+typedef void (*vhost_set_used_memslots_op)(struct vhost_dev *dev);
 
 typedef int (*vhost_crypto_create_session_op)(struct vhost_dev *dev,
                                               void *session_info,
@@ -105,7 +106,7 @@ typedef struct VhostOps {
     VhostBackendType backend_type;
     vhost_backend_init vhost_backend_init;
     vhost_backend_cleanup vhost_backend_cleanup;
-    vhost_backend_memslots_limit vhost_backend_memslots_limit;
+    vhost_backend_has_free_memslots vhost_backend_has_free_memslots;
     vhost_net_set_backend_op vhost_net_set_backend;
     vhost_net_set_mtu_op vhost_net_set_mtu;
     vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint;
@@ -136,6 +137,7 @@ typedef struct VhostOps {
     vhost_send_device_iotlb_msg_op vhost_send_device_iotlb_msg;
     vhost_get_config_op vhost_get_config;
     vhost_set_config_op vhost_set_config;
+    vhost_set_used_memslots_op vhost_set_used_memslots;
     vhost_crypto_create_session_op vhost_crypto_create_session;
     vhost_crypto_close_session_op vhost_crypto_close_session;
 } VhostOps;
Signature · 1 line
1.8.3.1
#3 lore raw
In reply to Jay Zhou
Quoted 9 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-kernel respectively.

Below should go after ---

Quoted 11 lines

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

When built with clang this causes runtime warnings (during make check) about misaligned access to structures.

The issue is that vhost_user_prepare_msg requests VhostUserMemory which compiler assumes but is then used with a pointer into a packed structure - where fields are not aligned.

Quoted 164 lines
---
 hw/virtio/vhost-backend.c         | 15 +++++++-
 hw/virtio/vhost-user.c            | 77 ++++++++++++++++++++++++++-------------
 hw/virtio/vhost.c                 | 13 +++----
 include/hw/virtio/vhost-backend.h |  6 ++-
 4 files changed, 75 insertions(+), 36 deletions(-)
hw/virtio/vhost-backend.c +14 -1
index 7f09efa..59def69 100644
@@ -15,6 +15,8 @@
 #include "hw/virtio/vhost-backend.h"
 #include "qemu/error-report.h"
 
+static unsigned int vhost_kernel_used_memslots;
+
 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
                              void *arg)
 {
@@ -62,6 +64,11 @@ static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
     return limit;
 }
 
+static bool vhost_kernel_has_free_memslots(struct vhost_dev *dev)
+{
+    return vhost_kernel_used_memslots < vhost_kernel_memslots_limit(dev);
+}
+
 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
                                         struct vhost_vring_file *file)
 {
@@ -233,11 +240,16 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,
         qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL);
 }
 
+static void vhost_kernel_set_used_memslots(struct vhost_dev *dev)
+{
+    vhost_kernel_used_memslots = dev->mem->nregions;
+}
+
 static const VhostOps kernel_ops = {
         .backend_type = VHOST_BACKEND_TYPE_KERNEL,
         .vhost_backend_init = vhost_kernel_init,
         .vhost_backend_cleanup = vhost_kernel_cleanup,
-        .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
+        .vhost_backend_has_free_memslots = vhost_kernel_has_free_memslots,
         .vhost_net_set_backend = vhost_kernel_net_set_backend,
         .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
         .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint,
@@ -264,6 +276,7 @@ static const VhostOps kernel_ops = {
 #endif /* CONFIG_VHOST_VSOCK */
         .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback,
         .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg,
+        .vhost_set_used_memslots = vhost_kernel_set_used_memslots,
 };
 
 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
hw/virtio/vhost-user.c +41 -25
index 41ff5cf..ef14249 100644
@@ -163,6 +163,8 @@ static VhostUserMsg m __attribute__ ((unused));
 /* The version of the protocol we support */
 #define VHOST_USER_VERSION    (0x1)
 
+static bool vhost_user_free_memslots = true;
+
 struct vhost_user {
     CharBackend *chr;
     int slave_fd;
@@ -330,12 +332,43 @@ static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,
     return 0;
 }
 
+static int vhost_user_prepare_msg(struct vhost_dev *dev, VhostUserMemory *mem,
+                                  int *fds)
+{
+    int i, fd;
+
+    vhost_user_free_memslots = true;
+    for (i = 0, mem->nregions = 0; i < dev->mem->nregions; ++i) {
+        struct vhost_memory_region *reg = dev->mem->regions + i;
+        ram_addr_t offset;
+        MemoryRegion *mr;
+
+        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
+        mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
+                                     &offset);
+        fd = memory_region_get_fd(mr);
+        if (fd > 0) {
+            if (mem->nregions == VHOST_MEMORY_MAX_NREGIONS) {
+                vhost_user_free_memslots = false;
+                return -1;
+            }
+
+            mem->regions[mem->nregions].userspace_addr = reg->userspace_addr;
+            mem->regions[mem->nregions].memory_size = reg->memory_size;
+            mem->regions[mem->nregions].guest_phys_addr = reg->guest_phys_addr;
+            mem->regions[mem->nregions].mmap_offset = offset;
+            fds[mem->nregions++] = fd;
+        }
+    }
+
+    return 0;
+}
+
 static int vhost_user_set_mem_table(struct vhost_dev *dev,
                                     struct vhost_memory *mem)
 {
     int fds[VHOST_MEMORY_MAX_NREGIONS];
-    int i, fd;
-    size_t fd_num = 0;
+    size_t fd_num;
     bool reply_supported = virtio_has_feature(dev->protocol_features,
                                               VHOST_USER_PROTOCOL_F_REPLY_ACK);
 
@@ -348,29 +381,12 @@ static int vhost_user_set_mem_table(struct vhost_dev *dev,
         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
     }
 
-    for (i = 0; i < dev->mem->nregions; ++i) {
-        struct vhost_memory_region *reg = dev->mem->regions + i;
-        ram_addr_t offset;
-        MemoryRegion *mr;
-
-        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
-        mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
-                                     &offset);
-        fd = memory_region_get_fd(mr);
-        if (fd > 0) {
-            if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
-                error_report("Failed preparing vhost-user memory table msg");
-                return -1;
-            }
-            msg.payload.memory.regions[fd_num].userspace_addr = reg->userspace_addr;
-            msg.payload.memory.regions[fd_num].memory_size  = reg->memory_size;
-            msg.payload.memory.regions[fd_num].guest_phys_addr = reg->guest_phys_addr;
-            msg.payload.memory.regions[fd_num].mmap_offset = offset;
-            fds[fd_num++] = fd;
-        }
+    if (vhost_user_prepare_msg(dev, &msg.payload.memory, fds) < 0) {
+        error_report("Failed preparing vhost-user memory table msg");
+        return -1;
     }
 
-    msg.payload.memory.nregions = fd_num;
+    fd_num = msg.payload.memory.nregions;
 
     if (!fd_num) {
         error_report("Failed initializing vhost-user memory map, "
@@ -886,9 +902,9 @@ static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)
     return idx;
 }
 
-static int vhost_user_memslots_limit(struct vhost_dev *dev)
+static bool vhost_user_has_free_memslots(struct vhost_dev *dev)
 {
-    return VHOST_MEMORY_MAX_NREGIONS;
+    return vhost_user_free_memslots;
 }
 
 static bool vhost_user_requires_shm_log(struct vhost_dev *dev)

hw/virtio/vhost-user.c vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id) · line 1180

Quoted 9 lines
hw/virtio/vhost-user.c +6 -0
@@ -1156,11 +1172,19 @@ vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id)
     return 0;
 }
 
+static void vhost_user_set_used_memslots(struct vhost_dev *dev)
+{
+    int fds[VHOST_MEMORY_MAX_NREGIONS];
+    VhostUserMsg msg;
+
+    vhost_user_prepare_msg(dev, &msg.payload.memory, fds);

Oops. This is something I don't understand.

Why is the message prepared here and then discarded?

Quoted 114 lines
hw/virtio/vhost-user.c +4 -1
+}
+
 const VhostOps user_ops = {
         .backend_type = VHOST_BACKEND_TYPE_USER,
         .vhost_backend_init = vhost_user_init,
         .vhost_backend_cleanup = vhost_user_cleanup,
-        .vhost_backend_memslots_limit = vhost_user_memslots_limit,
+        .vhost_backend_has_free_memslots = vhost_user_has_free_memslots,
         .vhost_set_log_base = vhost_user_set_log_base,
         .vhost_set_mem_table = vhost_user_set_mem_table,
         .vhost_set_vring_addr = vhost_user_set_vring_addr,
@@ -1184,6 +1208,7 @@ const VhostOps user_ops = {
         .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg,
         .vhost_get_config = vhost_user_get_config,
         .vhost_set_config = vhost_user_set_config,
+        .vhost_set_used_memslots = vhost_user_set_used_memslots,
         .vhost_crypto_create_session = vhost_user_crypto_create_session,
         .vhost_crypto_close_session = vhost_user_crypto_close_session,
 };
hw/virtio/vhost.c +6 -7
index d8d0ef9..17262d2 100644
@@ -44,20 +44,19 @@
 static struct vhost_log *vhost_log;
 static struct vhost_log *vhost_log_shm;
 
-static unsigned int used_memslots;
 static QLIST_HEAD(, vhost_dev) vhost_devices =
     QLIST_HEAD_INITIALIZER(vhost_devices);
 
 bool vhost_has_free_slot(void)
 {
-    unsigned int slots_limit = ~0U;
     struct vhost_dev *hdev;
 
     QLIST_FOREACH(hdev, &vhost_devices, entry) {
-        unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
-        slots_limit = MIN(slots_limit, r);
+        if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
+            return false;
+        }
     }
-    return slots_limit > used_memslots;
+    return true;
 }
 
 static void vhost_dev_sync_region(struct vhost_dev *dev,
@@ -446,7 +445,6 @@ static void vhost_commit(MemoryListener *listener)
                        dev->n_mem_sections * sizeof dev->mem->regions[0];
     dev->mem = g_realloc(dev->mem, regions_size);
     dev->mem->nregions = dev->n_mem_sections;
-    used_memslots = dev->mem->nregions;
     for (i = 0; i < dev->n_mem_sections; i++) {
         struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
         struct MemoryRegionSection *mrs = dev->mem_sections + i;
@@ -458,6 +456,7 @@ static void vhost_commit(MemoryListener *listener)
             mrs->offset_within_region;
         cur_vmr->flags_padding   = 0;
     }
+    dev->vhost_ops->vhost_set_used_memslots(dev);
 
     if (!dev->started) {
         goto out;
@@ -1202,7 +1201,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     memory_listener_register(&hdev->memory_listener, &address_space_memory);
     QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
 
-    if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) {
+    if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
         error_report("vhost backend memory slots limit is less"
                 " than current number of present memory slots");
         r = -1;
include/hw/virtio/vhost-backend.h +4 -2
index 5dac61f..ff06c2d 100644
@@ -36,7 +36,7 @@ struct vhost_iotlb_msg;
 
 typedef int (*vhost_backend_init)(struct vhost_dev *dev, void *opaque);
 typedef int (*vhost_backend_cleanup)(struct vhost_dev *dev);
-typedef int (*vhost_backend_memslots_limit)(struct vhost_dev *dev);
+typedef bool (*vhost_backend_has_free_memslots)(struct vhost_dev *dev);
 
 typedef int (*vhost_net_set_backend_op)(struct vhost_dev *dev,
                                 struct vhost_vring_file *file);
@@ -94,6 +94,7 @@ typedef int (*vhost_set_config_op)(struct vhost_dev *dev, const uint8_t *data,
                                    uint32_t flags);
 typedef int (*vhost_get_config_op)(struct vhost_dev *dev, uint8_t *config,
                                    uint32_t config_len);
+typedef void (*vhost_set_used_memslots_op)(struct vhost_dev *dev);
 
 typedef int (*vhost_crypto_create_session_op)(struct vhost_dev *dev,
                                               void *session_info,
@@ -105,7 +106,7 @@ typedef struct VhostOps {
     VhostBackendType backend_type;
     vhost_backend_init vhost_backend_init;
     vhost_backend_cleanup vhost_backend_cleanup;
-    vhost_backend_memslots_limit vhost_backend_memslots_limit;
+    vhost_backend_has_free_memslots vhost_backend_has_free_memslots;
     vhost_net_set_backend_op vhost_net_set_backend;
     vhost_net_set_mtu_op vhost_net_set_mtu;
     vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint;
@@ -136,6 +137,7 @@ typedef struct VhostOps {
     vhost_send_device_iotlb_msg_op vhost_send_device_iotlb_msg;
     vhost_get_config_op vhost_get_config;
     vhost_set_config_op vhost_set_config;
+    vhost_set_used_memslots_op vhost_set_used_memslots;
     vhost_crypto_create_session_op vhost_crypto_create_session;
     vhost_crypto_close_session_op vhost_crypto_close_session;
 } VhostOps;
Signature · 1 line
1.8.3.1
#4 lore raw

Hi Michael,

Quoted 20 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 9:34 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei (Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 8 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-kernel respectively.

Quoted 1 line

Below should go after ---

Thanks for reminding.

Quoted 13 lines
Quoted 9 lines

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

Quoted 3 lines
Quoted 6 lines

When built with clang this causes runtime warnings (during make check) about misaligned access to structures.

The issue is that vhost_user_prepare_msg requests VhostUserMemory which compiler assumes but is then used with a pointer into a packed structure - where fields are not aligned.

Sorry I missed the patch you have sent to fix the alignment, I have replied to that thread.

Quoted 189 lines
Quoted 5 lines

---
 hw/virtio/vhost-backend.c         | 15 +++++++-
 hw/virtio/vhost-user.c            | 77 ++++++++++++++++++++++++++---------

----

Quoted 9 lines
 hw/virtio/vhost.c                 | 13 +++----
 include/hw/virtio/vhost-backend.h |  6 ++-
 4 files changed, 75 insertions(+), 36 deletions(-)
hw/virtio/vhost-backend.c +0 -0
index 7f09efa..59def69 100644

hw/virtio/vhost-backend.c line 20

Quoted 6 lines
hw/virtio/vhost-backend.c +2 -0
@@ -15,6 +15,8 @@
 #include "hw/virtio/vhost-backend.h"
 #include "qemu/error-report.h"
 
+static unsigned int vhost_kernel_used_memslots;
+
 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int

request,

hw/virtio/vhost-user.c static int vhost_user_set_log_base(struct vhost_dev

Quoted 52 lines
hw/virtio/vhost-backend.c +0 -0
                              void *arg)  { @@ -62,6 +64,11 @@ static
 int vhost_kernel_memslots_limit(struct vhost_dev *dev)
     return limit;

}

hw/virtio/vhost-backend.c +4 -0
+static bool vhost_kernel_has_free_memslots(struct vhost_dev *dev) {
+    return vhost_kernel_used_memslots <
+vhost_kernel_memslots_limit(dev); }
+
 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
                                         struct vhost_vring_file

*file) { @@ -233,11 +240,16 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,

         qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL,

NULL); }

hw/virtio/vhost-backend.c +5 -1
+static void vhost_kernel_set_used_memslots(struct vhost_dev *dev) {
+    vhost_kernel_used_memslots = dev->mem->nregions; }
+
 static const VhostOps kernel_ops = {
         .backend_type = VHOST_BACKEND_TYPE_KERNEL,
         .vhost_backend_init = vhost_kernel_init,
         .vhost_backend_cleanup = vhost_kernel_cleanup,
-        .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
+        .vhost_backend_has_free_memslots =
+ vhost_kernel_has_free_memslots,
         .vhost_net_set_backend = vhost_kernel_net_set_backend,
         .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
         .vhost_scsi_clear_endpoint =

vhost_kernel_scsi_clear_endpoint, @@ -264,6 +276,7 @@ static const VhostOps kernel_ops = { #endif /* CONFIG_VHOST_VSOCK */
.vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback, .vhost_send_device_iotlb_msg =
vhost_kernel_send_device_iotlb_msg,
+ .vhost_set_used_memslots = vhost_kernel_set_used_memslots,
};

int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType
backend_type) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 41ff5cf..ef14249 100644

hw/virtio/vhost-user.c +2 -0
@@ -163,6 +163,8 @@ static VhostUserMsg m __attribute__ ((unused));
 /* The version of the protocol we support */
 #define VHOST_USER_VERSION    (0x1)
 
+static bool vhost_user_free_memslots = true;
+
 struct vhost_user {
     CharBackend *chr;
     int slave_fd;
@@ -330,12 +332,43 @@ static int vhost_user_set_log_base(struct vhost_dev

*dev, uint64_t base,

hw/virtio/vhost-user.c static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,

Quoted 4 lines
hw/virtio/vhost-user.c +1 -0
     return 0;
 }
 
+static int vhost_user_prepare_msg(struct vhost_dev *dev, VhostUserMemory

*mem,

hw/virtio/vhost-user.c static int vhost_user_set_mem_table(struct vhost_dev

Quoted 44 lines
hw/virtio/vhost-user.c +31 -2
+                                  int *fds) {
+    int i, fd;
+
+    vhost_user_free_memslots = true;
+    for (i = 0, mem->nregions = 0; i < dev->mem->nregions; ++i) {
+        struct vhost_memory_region *reg = dev->mem->regions + i;
+        ram_addr_t offset;
+        MemoryRegion *mr;
+
+        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
+        mr = memory_region_from_host((void *)(uintptr_t)reg-
 userspace_addr,
+                                     &offset);
+        fd = memory_region_get_fd(mr);
+        if (fd > 0) {
+            if (mem->nregions == VHOST_MEMORY_MAX_NREGIONS) {
+                vhost_user_free_memslots = false;
+                return -1;
+            }
+
+            mem->regions[mem->nregions].userspace_addr = reg-
 userspace_addr;
+            mem->regions[mem->nregions].memory_size = reg->memory_size;
+            mem->regions[mem->nregions].guest_phys_addr = reg-
 guest_phys_addr;
+            mem->regions[mem->nregions].mmap_offset = offset;
+            fds[mem->nregions++] = fd;
+        }
+    }
+
+    return 0;
+}
+
 static int vhost_user_set_mem_table(struct vhost_dev *dev,
                                     struct vhost_memory *mem)  {
     int fds[VHOST_MEMORY_MAX_NREGIONS];
-    int i, fd;
-    size_t fd_num = 0;
+    size_t fd_num;
     bool reply_supported = virtio_has_feature(dev->protocol_features,

VHOST_USER_PROTOCOL_F_REPLY_ACK);

hw/virtio/vhost-user.c +0 -0
@@ -348,29 +381,12 @@ static int vhost_user_set_mem_table(struct vhost_dev

*dev,

Quoted 6 lines
hw/virtio/vhost-user.c +0 -3
         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
     }
 
-    for (i = 0; i < dev->mem->nregions; ++i) {
-        struct vhost_memory_region *reg = dev->mem->regions + i;
-        ram_addr_t offset;

hw/virtio/vhost-user.c static int vhost_user_set_mem_table(struct vhost_dev *dev,

Quoted 10 lines
hw/virtio/vhost-user.c +0 -9
-        MemoryRegion *mr;
-
-        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
-        mr = memory_region_from_host((void *)(uintptr_t)reg-
 userspace_addr,
-                                     &offset);
-        fd = memory_region_get_fd(mr);
-        if (fd > 0) {
-            if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
-                error_report("Failed preparing vhost-user memory table

msg");

hw/virtio/vhost-user.c static int vhost_user_get_vq_index(struct vhost_dev

Quoted 22 lines
hw/virtio/vhost-user.c +4 -9
-                return -1;
-            }
-            msg.payload.memory.regions[fd_num].userspace_addr = reg-
 userspace_addr;
-            msg.payload.memory.regions[fd_num].memory_size  = reg-
 memory_size;
-            msg.payload.memory.regions[fd_num].guest_phys_addr = reg-
 guest_phys_addr;
-            msg.payload.memory.regions[fd_num].mmap_offset = offset;
-            fds[fd_num++] = fd;
-        }
+    if (vhost_user_prepare_msg(dev, &msg.payload.memory, fds) < 0) {
+        error_report("Failed preparing vhost-user memory table msg");
+        return -1;
     }
 
-    msg.payload.memory.nregions = fd_num;
+    fd_num = msg.payload.memory.nregions;
 
     if (!fd_num) {
         error_report("Failed initializing vhost-user memory map, "
@@ -886,9 +902,9 @@ static int vhost_user_get_vq_index(struct vhost_dev

*dev, int idx)

hw/virtio/vhost-user.c static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)

Quoted 12 lines
hw/virtio/vhost-user.c +2 -3
     return idx;
 }
 
-static int vhost_user_memslots_limit(struct vhost_dev *dev)
+static bool vhost_user_has_free_memslots(struct vhost_dev *dev)
 {
-    return VHOST_MEMORY_MAX_NREGIONS;
+    return vhost_user_free_memslots;
 }
 
 static bool vhost_user_requires_shm_log(struct vhost_dev *dev) @@
-1156,11 +1172,19 @@ vhost_user_crypto_close_session(struct vhost_dev *dev,

uint64_t session_id)

hw/virtio/vhost-user.c vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id) · line 1180

Quoted 8 lines
     return 0;

}

hw/virtio/vhost-user.c +5 -0
+static void vhost_user_set_used_memslots(struct vhost_dev *dev) {
+    int fds[VHOST_MEMORY_MAX_NREGIONS];
+    VhostUserMsg msg;
+
+    vhost_user_prepare_msg(dev, &msg.payload.memory, fds);

Oops. This is something I don't understand.

Quoted 2 lines

Why is the message prepared here and then discarded?

The purpose of vhost_user_set_used_memslots() is to set the boolean value
of vhost_user_free_memslots, which indicating whether there're free memeslots for vhost user. Since there're code duplicating inside vhost_user_set_used_memslots() and vhost_user_set_mem_table(), Igor suggested that we could create a new function to avoid duplicating.
Here, the value of VhostUserMsg is not needed by the caller vhost_user_set_used_memslots(), so we just discarded.

Regards,
Jay

Quoted 123 lines

hw/virtio/vhost.c int vhost_dev_init(struct vhost_dev *hdev, void

Quoted 67 lines
hw/virtio/vhost-user.c +6 -1
+}
+
 const VhostOps user_ops = {
         .backend_type = VHOST_BACKEND_TYPE_USER,
         .vhost_backend_init = vhost_user_init,
         .vhost_backend_cleanup = vhost_user_cleanup,
-        .vhost_backend_memslots_limit = vhost_user_memslots_limit,
+        .vhost_backend_has_free_memslots =
+ vhost_user_has_free_memslots,
         .vhost_set_log_base = vhost_user_set_log_base,
         .vhost_set_mem_table = vhost_user_set_mem_table,
         .vhost_set_vring_addr = vhost_user_set_vring_addr, @@ -1184,6
+1208,7 @@ const VhostOps user_ops = {
         .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg,
         .vhost_get_config = vhost_user_get_config,
         .vhost_set_config = vhost_user_set_config,
+        .vhost_set_used_memslots = vhost_user_set_used_memslots,
         .vhost_crypto_create_session = vhost_user_crypto_create_session,
         .vhost_crypto_close_session =

vhost_user_crypto_close_session, }; diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index d8d0ef9..17262d2 100644

hw/virtio/vhost.c +4 -5
@@ -44,20 +44,19 @@
 static struct vhost_log *vhost_log;
 static struct vhost_log *vhost_log_shm;
 
-static unsigned int used_memslots;
 static QLIST_HEAD(, vhost_dev) vhost_devices =
     QLIST_HEAD_INITIALIZER(vhost_devices);
 
 bool vhost_has_free_slot(void)
 {
-    unsigned int slots_limit = ~0U;
     struct vhost_dev *hdev;
 
     QLIST_FOREACH(hdev, &vhost_devices, entry) {
-        unsigned int r = hdev->vhost_ops-
 vhost_backend_memslots_limit(hdev);
-        slots_limit = MIN(slots_limit, r);
+        if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
+            return false;
+        }
     }
-    return slots_limit > used_memslots;
+    return true;
 }
 

static void vhost_dev_sync_region(struct vhost_dev *dev, @@ -446,7
+445,6 @@ static void vhost_commit(MemoryListener *listener)

                        dev->n_mem_sections * sizeof dev->mem->regions[0];
     dev->mem = g_realloc(dev->mem, regions_size);
     dev->mem->nregions = dev->n_mem_sections;
hw/virtio/vhost.c +1 -2
-    used_memslots = dev->mem->nregions;
     for (i = 0; i < dev->n_mem_sections; i++) {
         struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
         struct MemoryRegionSection *mrs = dev->mem_sections + i; @@
-458,6 +456,7 @@ static void vhost_commit(MemoryListener *listener)
             mrs->offset_within_region;
         cur_vmr->flags_padding   = 0;
     }
+    dev->vhost_ops->vhost_set_used_memslots(dev);
 
     if (!dev->started) {
         goto out;
@@ -1202,7 +1201,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void

*opaque,

hw/virtio/vhost.c int vhost_dev_init(struct vhost_dev *hdev, void · line 1201

Quoted 1 line
hw/virtio/vhost.c +0 -0
     memory_listener_register(&hdev->memory_listener,

&address_space_memory);

Quoted 15 lines
hw/virtio/vhost.c +1 -1
     QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
 
-    if (used_memslots > hdev->vhost_ops-
 vhost_backend_memslots_limit(hdev)) {
+    if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
         error_report("vhost backend memory slots limit is less"
                 " than current number of present memory slots");
         r = -1;
include/hw/virtio/vhost-backend.h +0 -0
index 5dac61f..ff06c2d 100644
@@ -36,7 +36,7 @@ struct vhost_iotlb_msg;
 

include/hw/virtio/vhost-backend.h struct vhost_iotlb_msg;

Quoted 10 lines
include/hw/virtio/vhost-backend.h +2 -1
 typedef int (*vhost_backend_init)(struct vhost_dev *dev, void
 *opaque);  typedef int (*vhost_backend_cleanup)(struct vhost_dev
 *dev); -typedef int (*vhost_backend_memslots_limit)(struct vhost_dev
 *dev);
+typedef bool (*vhost_backend_has_free_memslots)(struct vhost_dev
+*dev);
 
 typedef int (*vhost_net_set_backend_op)(struct vhost_dev *dev,
                                 struct vhost_vring_file *file); @@
-94,6 +94,7 @@ typedef int (*vhost_set_config_op)(struct vhost_dev *dev,

const uint8_t *data,

Quoted 27 lines

uint32_t flags); typedef int
(*vhost_get_config_op)(struct vhost_dev *dev, uint8_t *config,

                                    uint32_t config_len);
include/hw/virtio/vhost-backend.h +4 -2
+typedef void (*vhost_set_used_memslots_op)(struct vhost_dev *dev);
 
 typedef int (*vhost_crypto_create_session_op)(struct vhost_dev *dev,
                                               void *session_info, @@
-105,7 +106,7 @@ typedef struct VhostOps {
     VhostBackendType backend_type;
     vhost_backend_init vhost_backend_init;
     vhost_backend_cleanup vhost_backend_cleanup;
-    vhost_backend_memslots_limit vhost_backend_memslots_limit;
+    vhost_backend_has_free_memslots vhost_backend_has_free_memslots;
     vhost_net_set_backend_op vhost_net_set_backend;
     vhost_net_set_mtu_op vhost_net_set_mtu;
     vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint; @@ -136,6
+137,7 @@ typedef struct VhostOps {
     vhost_send_device_iotlb_msg_op vhost_send_device_iotlb_msg;
     vhost_get_config_op vhost_get_config;
     vhost_set_config_op vhost_set_config;
+    vhost_set_used_memslots_op vhost_set_used_memslots;
     vhost_crypto_create_session_op vhost_crypto_create_session;
     vhost_crypto_close_session_op vhost_crypto_close_session;  }

VhostOps;
--
1.8.3.1

#5 lore raw
Quoted 249 lines · Zhoujian (jay)

Hi Michael,

Quoted 20 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 9:34 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei (Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 8 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-kernel respectively.

Quoted 1 line

Below should go after ---

Thanks for reminding.

Quoted 13 lines
Quoted 9 lines

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

Quoted 3 lines
Quoted 6 lines

When built with clang this causes runtime warnings (during make check) about misaligned access to structures.

The issue is that vhost_user_prepare_msg requests VhostUserMemory which compiler assumes but is then used with a pointer into a packed structure - where fields are not aligned.

Sorry I missed the patch you have sent to fix the alignment, I have replied to that thread.

Quoted 189 lines
Quoted 5 lines

---
 hw/virtio/vhost-backend.c         | 15 +++++++-
 hw/virtio/vhost-user.c            | 77 ++++++++++++++++++++++++++---------

----

Quoted 9 lines
 hw/virtio/vhost.c                 | 13 +++----
 include/hw/virtio/vhost-backend.h |  6 ++-
 4 files changed, 75 insertions(+), 36 deletions(-)
hw/virtio/vhost-backend.c +0 -0
index 7f09efa..59def69 100644

hw/virtio/vhost-backend.c line 20

Quoted 6 lines
hw/virtio/vhost-backend.c +2 -0
@@ -15,6 +15,8 @@
 #include "hw/virtio/vhost-backend.h"
 #include "qemu/error-report.h"
 
+static unsigned int vhost_kernel_used_memslots;
+
 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int

request,

hw/virtio/vhost-user.c static int vhost_user_set_log_base(struct vhost_dev

Quoted 52 lines
hw/virtio/vhost-backend.c +0 -0
                              void *arg)  { @@ -62,6 +64,11 @@ static
 int vhost_kernel_memslots_limit(struct vhost_dev *dev)
     return limit;

}

hw/virtio/vhost-backend.c +4 -0
+static bool vhost_kernel_has_free_memslots(struct vhost_dev *dev) {
+    return vhost_kernel_used_memslots <
+vhost_kernel_memslots_limit(dev); }
+
 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
                                         struct vhost_vring_file

*file) { @@ -233,11 +240,16 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,

         qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL,

NULL); }

hw/virtio/vhost-backend.c +5 -1
+static void vhost_kernel_set_used_memslots(struct vhost_dev *dev) {
+    vhost_kernel_used_memslots = dev->mem->nregions; }
+
 static const VhostOps kernel_ops = {
         .backend_type = VHOST_BACKEND_TYPE_KERNEL,
         .vhost_backend_init = vhost_kernel_init,
         .vhost_backend_cleanup = vhost_kernel_cleanup,
-        .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
+        .vhost_backend_has_free_memslots =
+ vhost_kernel_has_free_memslots,
         .vhost_net_set_backend = vhost_kernel_net_set_backend,
         .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
         .vhost_scsi_clear_endpoint =

vhost_kernel_scsi_clear_endpoint, @@ -264,6 +276,7 @@ static const VhostOps kernel_ops = { #endif /* CONFIG_VHOST_VSOCK */
.vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback, .vhost_send_device_iotlb_msg =
vhost_kernel_send_device_iotlb_msg,
+ .vhost_set_used_memslots = vhost_kernel_set_used_memslots,
};

int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType
backend_type) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 41ff5cf..ef14249 100644

hw/virtio/vhost-user.c +2 -0
@@ -163,6 +163,8 @@ static VhostUserMsg m __attribute__ ((unused));
 /* The version of the protocol we support */
 #define VHOST_USER_VERSION    (0x1)
 
+static bool vhost_user_free_memslots = true;
+
 struct vhost_user {
     CharBackend *chr;
     int slave_fd;
@@ -330,12 +332,43 @@ static int vhost_user_set_log_base(struct vhost_dev

*dev, uint64_t base,

hw/virtio/vhost-user.c static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,

Quoted 4 lines
hw/virtio/vhost-user.c +1 -0
     return 0;
 }
 
+static int vhost_user_prepare_msg(struct vhost_dev *dev, VhostUserMemory

*mem,

hw/virtio/vhost-user.c static int vhost_user_set_mem_table(struct vhost_dev

Quoted 44 lines
hw/virtio/vhost-user.c +31 -2
+                                  int *fds) {
+    int i, fd;
+
+    vhost_user_free_memslots = true;
+    for (i = 0, mem->nregions = 0; i < dev->mem->nregions; ++i) {
+        struct vhost_memory_region *reg = dev->mem->regions + i;
+        ram_addr_t offset;
+        MemoryRegion *mr;
+
+        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
+        mr = memory_region_from_host((void *)(uintptr_t)reg-
 userspace_addr,
+                                     &offset);
+        fd = memory_region_get_fd(mr);
+        if (fd > 0) {
+            if (mem->nregions == VHOST_MEMORY_MAX_NREGIONS) {
+                vhost_user_free_memslots = false;
+                return -1;
+            }
+
+            mem->regions[mem->nregions].userspace_addr = reg-
 userspace_addr;
+            mem->regions[mem->nregions].memory_size = reg->memory_size;
+            mem->regions[mem->nregions].guest_phys_addr = reg-
 guest_phys_addr;
+            mem->regions[mem->nregions].mmap_offset = offset;
+            fds[mem->nregions++] = fd;
+        }
+    }
+
+    return 0;
+}
+
 static int vhost_user_set_mem_table(struct vhost_dev *dev,
                                     struct vhost_memory *mem)  {
     int fds[VHOST_MEMORY_MAX_NREGIONS];
-    int i, fd;
-    size_t fd_num = 0;
+    size_t fd_num;
     bool reply_supported = virtio_has_feature(dev->protocol_features,

VHOST_USER_PROTOCOL_F_REPLY_ACK);

hw/virtio/vhost-user.c +0 -0
@@ -348,29 +381,12 @@ static int vhost_user_set_mem_table(struct vhost_dev

*dev,

Quoted 6 lines
hw/virtio/vhost-user.c +0 -3
         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
     }
 
-    for (i = 0; i < dev->mem->nregions; ++i) {
-        struct vhost_memory_region *reg = dev->mem->regions + i;
-        ram_addr_t offset;

hw/virtio/vhost-user.c static int vhost_user_set_mem_table(struct vhost_dev *dev,

Quoted 10 lines
hw/virtio/vhost-user.c +0 -9
-        MemoryRegion *mr;
-
-        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
-        mr = memory_region_from_host((void *)(uintptr_t)reg-
 userspace_addr,
-                                     &offset);
-        fd = memory_region_get_fd(mr);
-        if (fd > 0) {
-            if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
-                error_report("Failed preparing vhost-user memory table

msg");

hw/virtio/vhost-user.c static int vhost_user_get_vq_index(struct vhost_dev

Quoted 22 lines
hw/virtio/vhost-user.c +4 -9
-                return -1;
-            }
-            msg.payload.memory.regions[fd_num].userspace_addr = reg-
 userspace_addr;
-            msg.payload.memory.regions[fd_num].memory_size  = reg-
 memory_size;
-            msg.payload.memory.regions[fd_num].guest_phys_addr = reg-
 guest_phys_addr;
-            msg.payload.memory.regions[fd_num].mmap_offset = offset;
-            fds[fd_num++] = fd;
-        }
+    if (vhost_user_prepare_msg(dev, &msg.payload.memory, fds) < 0) {
+        error_report("Failed preparing vhost-user memory table msg");
+        return -1;
     }
 
-    msg.payload.memory.nregions = fd_num;
+    fd_num = msg.payload.memory.nregions;
 
     if (!fd_num) {
         error_report("Failed initializing vhost-user memory map, "
@@ -886,9 +902,9 @@ static int vhost_user_get_vq_index(struct vhost_dev

*dev, int idx)

hw/virtio/vhost-user.c static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)

Quoted 12 lines
hw/virtio/vhost-user.c +2 -3
     return idx;
 }
 
-static int vhost_user_memslots_limit(struct vhost_dev *dev)
+static bool vhost_user_has_free_memslots(struct vhost_dev *dev)
 {
-    return VHOST_MEMORY_MAX_NREGIONS;
+    return vhost_user_free_memslots;
 }
 
 static bool vhost_user_requires_shm_log(struct vhost_dev *dev) @@
-1156,11 +1172,19 @@ vhost_user_crypto_close_session(struct vhost_dev *dev,

uint64_t session_id)

hw/virtio/vhost-user.c vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id) · line 1180

Quoted 8 lines
     return 0;

}

hw/virtio/vhost-user.c +5 -0
+static void vhost_user_set_used_memslots(struct vhost_dev *dev) {
+    int fds[VHOST_MEMORY_MAX_NREGIONS];
+    VhostUserMsg msg;
+
+    vhost_user_prepare_msg(dev, &msg.payload.memory, fds);

Oops. This is something I don't understand.

Quoted 2 lines

Why is the message prepared here and then discarded?

The purpose of vhost_user_set_used_memslots() is to set the boolean value
of vhost_user_free_memslots, which indicating whether there're free memeslots for vhost user. Since there're code duplicating inside vhost_user_set_used_memslots() and vhost_user_set_mem_table(), Igor suggested that we could create a new function to avoid duplicating.
Here, the value of VhostUserMsg is not needed by the caller vhost_user_set_used_memslots(), so we just discarded.

Quoted 2 lines

Regards,
Jay

I think I misunderstood the meaning of that variable. It seems to be set when there are more slots than supported.

What vhost_user_free_memslots implies is that it is set when there are no free slots, even if existing config fits.

A better name would be vhost_user_out_of_memslots maybe?

And I missed the fact that it (as well as the prepare call) can actually fail when out of slots.
Shouldn't it return status too?

Quoted 123 lines
Quoted 123 lines

hw/virtio/vhost.c int vhost_dev_init(struct vhost_dev *hdev, void

Quoted 67 lines
hw/virtio/vhost-user.c +6 -1
+}
+
 const VhostOps user_ops = {
         .backend_type = VHOST_BACKEND_TYPE_USER,
         .vhost_backend_init = vhost_user_init,
         .vhost_backend_cleanup = vhost_user_cleanup,
-        .vhost_backend_memslots_limit = vhost_user_memslots_limit,
+        .vhost_backend_has_free_memslots =
+ vhost_user_has_free_memslots,
         .vhost_set_log_base = vhost_user_set_log_base,
         .vhost_set_mem_table = vhost_user_set_mem_table,
         .vhost_set_vring_addr = vhost_user_set_vring_addr, @@ -1184,6
+1208,7 @@ const VhostOps user_ops = {
         .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg,
         .vhost_get_config = vhost_user_get_config,
         .vhost_set_config = vhost_user_set_config,
+        .vhost_set_used_memslots = vhost_user_set_used_memslots,
         .vhost_crypto_create_session = vhost_user_crypto_create_session,
         .vhost_crypto_close_session =

vhost_user_crypto_close_session, }; diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index d8d0ef9..17262d2 100644

hw/virtio/vhost.c +4 -5
@@ -44,20 +44,19 @@
 static struct vhost_log *vhost_log;
 static struct vhost_log *vhost_log_shm;
 
-static unsigned int used_memslots;
 static QLIST_HEAD(, vhost_dev) vhost_devices =
     QLIST_HEAD_INITIALIZER(vhost_devices);
 
 bool vhost_has_free_slot(void)
 {
-    unsigned int slots_limit = ~0U;
     struct vhost_dev *hdev;
 
     QLIST_FOREACH(hdev, &vhost_devices, entry) {
-        unsigned int r = hdev->vhost_ops-
 vhost_backend_memslots_limit(hdev);
-        slots_limit = MIN(slots_limit, r);
+        if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
+            return false;
+        }
     }
-    return slots_limit > used_memslots;
+    return true;
 }
 

static void vhost_dev_sync_region(struct vhost_dev *dev, @@ -446,7
+445,6 @@ static void vhost_commit(MemoryListener *listener)

                        dev->n_mem_sections * sizeof dev->mem->regions[0];
     dev->mem = g_realloc(dev->mem, regions_size);
     dev->mem->nregions = dev->n_mem_sections;
hw/virtio/vhost.c +1 -2
-    used_memslots = dev->mem->nregions;
     for (i = 0; i < dev->n_mem_sections; i++) {
         struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
         struct MemoryRegionSection *mrs = dev->mem_sections + i; @@
-458,6 +456,7 @@ static void vhost_commit(MemoryListener *listener)
             mrs->offset_within_region;
         cur_vmr->flags_padding   = 0;
     }
+    dev->vhost_ops->vhost_set_used_memslots(dev);
 
     if (!dev->started) {
         goto out;
@@ -1202,7 +1201,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void

*opaque,

hw/virtio/vhost.c int vhost_dev_init(struct vhost_dev *hdev, void · line 1201

Quoted 1 line
hw/virtio/vhost.c +0 -0
     memory_listener_register(&hdev->memory_listener,

&address_space_memory);

Quoted 15 lines
hw/virtio/vhost.c +1 -1
     QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
 
-    if (used_memslots > hdev->vhost_ops-
 vhost_backend_memslots_limit(hdev)) {
+    if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
         error_report("vhost backend memory slots limit is less"
                 " than current number of present memory slots");
         r = -1;
include/hw/virtio/vhost-backend.h +0 -0
index 5dac61f..ff06c2d 100644
@@ -36,7 +36,7 @@ struct vhost_iotlb_msg;
 

include/hw/virtio/vhost-backend.h struct vhost_iotlb_msg;

Quoted 10 lines
include/hw/virtio/vhost-backend.h +2 -1
 typedef int (*vhost_backend_init)(struct vhost_dev *dev, void
 *opaque);  typedef int (*vhost_backend_cleanup)(struct vhost_dev
 *dev); -typedef int (*vhost_backend_memslots_limit)(struct vhost_dev
 *dev);
+typedef bool (*vhost_backend_has_free_memslots)(struct vhost_dev
+*dev);
 
 typedef int (*vhost_net_set_backend_op)(struct vhost_dev *dev,
                                 struct vhost_vring_file *file); @@
-94,6 +94,7 @@ typedef int (*vhost_set_config_op)(struct vhost_dev *dev,

const uint8_t *data,

Quoted 27 lines

uint32_t flags); typedef int
(*vhost_get_config_op)(struct vhost_dev *dev, uint8_t *config,

                                    uint32_t config_len);
include/hw/virtio/vhost-backend.h +4 -2
+typedef void (*vhost_set_used_memslots_op)(struct vhost_dev *dev);
 
 typedef int (*vhost_crypto_create_session_op)(struct vhost_dev *dev,
                                               void *session_info, @@
-105,7 +106,7 @@ typedef struct VhostOps {
     VhostBackendType backend_type;
     vhost_backend_init vhost_backend_init;
     vhost_backend_cleanup vhost_backend_cleanup;
-    vhost_backend_memslots_limit vhost_backend_memslots_limit;
+    vhost_backend_has_free_memslots vhost_backend_has_free_memslots;
     vhost_net_set_backend_op vhost_net_set_backend;
     vhost_net_set_mtu_op vhost_net_set_mtu;
     vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint; @@ -136,6
+137,7 @@ typedef struct VhostOps {
     vhost_send_device_iotlb_msg_op vhost_send_device_iotlb_msg;
     vhost_get_config_op vhost_get_config;
     vhost_set_config_op vhost_set_config;
+    vhost_set_used_memslots_op vhost_set_used_memslots;
     vhost_crypto_create_session_op vhost_crypto_create_session;
     vhost_crypto_close_session_op vhost_crypto_close_session;  }

VhostOps;
--
1.8.3.1

#6 lore raw
Quoted 275 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 10:51 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei (Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 22 lines · Zhoujian (jay)

Hi Michael,

Quoted 20 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 9:34 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei
(Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 8 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-

kernel respectively.

Quoted 22 lines
Quoted 3 lines

Below should go after ---

Thanks for reminding.

Quoted 16 lines
Quoted 9 lines

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

Quoted 3 lines

When built with clang this causes runtime warnings (during make check) about misaligned access to structures.

Quoted 12 lines
Quoted 3 lines

The issue is that vhost_user_prepare_msg requests VhostUserMemory which compiler assumes but is then used with a pointer into a packed structure - where fields are not aligned.

Sorry I missed the patch you have sent to fix the alignment, I have replied to that thread.

Quoted 5 lines
Quoted 5 lines

---
 hw/virtio/vhost-backend.c         | 15 +++++++-
 hw/virtio/vhost-user.c            | 77 ++++++++++++++++++++++++++-----

----

Quoted 205 lines
Quoted 194 lines

----

Quoted 15 lines
 hw/virtio/vhost.c                 | 13 +++----
 include/hw/virtio/vhost-backend.h |  6 ++-
 4 files changed, 75 insertions(+), 36 deletions(-)
hw/virtio/vhost-backend.c +2 -0
index 7f09efa..59def69 100644
@@ -15,6 +15,8 @@
 #include "hw/virtio/vhost-backend.h"
 #include "qemu/error-report.h"
 
+static unsigned int vhost_kernel_used_memslots;
+
 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long
Quoted 1 line

int

request,

Quoted 55 lines
                              void *arg)  { @@ -62,6 +64,11 @@
static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
     return limit;

}

hw/virtio/vhost-backend.c +4 -0
+static bool vhost_kernel_has_free_memslots(struct vhost_dev *dev) {
+    return vhost_kernel_used_memslots <
+vhost_kernel_memslots_limit(dev); }
+
 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
                                         struct vhost_vring_file

*file) { @@ -233,11 +240,16 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,

         qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL,

NULL); }

hw/virtio/vhost-backend.c +5 -1
+static void vhost_kernel_set_used_memslots(struct vhost_dev *dev) {
+    vhost_kernel_used_memslots = dev->mem->nregions; }
+
 static const VhostOps kernel_ops = {
         .backend_type = VHOST_BACKEND_TYPE_KERNEL,
         .vhost_backend_init = vhost_kernel_init,
         .vhost_backend_cleanup = vhost_kernel_cleanup,
-        .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
+        .vhost_backend_has_free_memslots =
+ vhost_kernel_has_free_memslots,
         .vhost_net_set_backend = vhost_kernel_net_set_backend,
         .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
         .vhost_scsi_clear_endpoint =

vhost_kernel_scsi_clear_endpoint, @@ -264,6 +276,7 @@ static const VhostOps kernel_ops = { #endif /* CONFIG_VHOST_VSOCK */
.vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback, .vhost_send_device_iotlb_msg =
vhost_kernel_send_device_iotlb_msg,
+ .vhost_set_used_memslots =
+ vhost_kernel_set_used_memslots,
};

int vhost_set_backend_type(struct vhost_dev *dev,
VhostBackendType
backend_type) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 41ff5cf..ef14249 100644

hw/virtio/vhost-user.c +2 -0
@@ -163,6 +163,8 @@ static VhostUserMsg m __attribute__
 ((unused));
 /* The version of the protocol we support */
 #define VHOST_USER_VERSION    (0x1)
 
+static bool vhost_user_free_memslots = true;
+
 struct vhost_user {
     CharBackend *chr;
     int slave_fd;
hw/virtio/vhost-user.c +0 -0
@@ -330,12 +332,43 @@ static int vhost_user_set_log_base(struct
Quoted 1 line

vhost_dev

*dev, uint64_t base,

hw/virtio/vhost-user.c

Quoted 5 lines
     return 0;

}

hw/virtio/vhost-user.c +2 -0
+static int vhost_user_prepare_msg(struct vhost_dev *dev,
+VhostUserMemory

*mem,

Quoted 45 lines
hw/virtio/vhost-user.c +11 -0
+                                  int *fds) {
+    int i, fd;
+
+    vhost_user_free_memslots = true;
+    for (i = 0, mem->nregions = 0; i < dev->mem->nregions; ++i) {
+        struct vhost_memory_region *reg = dev->mem->regions + i;
+        ram_addr_t offset;
+        MemoryRegion *mr;
+
+        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
+        mr = memory_region_from_host((void *)(uintptr_t)reg-

userspace_addr,

hw/virtio/vhost-user.c +9 -0
+                                     &offset);
+        fd = memory_region_get_fd(mr);
+        if (fd > 0) {
+            if (mem->nregions == VHOST_MEMORY_MAX_NREGIONS) {
+                vhost_user_free_memslots = false;
+                return -1;
+            }
+
+            mem->regions[mem->nregions].userspace_addr = reg-

userspace_addr;

hw/virtio/vhost-user.c +2 -0
+            mem->regions[mem->nregions].memory_size = reg->memory_size;
+            mem->regions[mem->nregions].guest_phys_addr = reg-

guest_phys_addr;

hw/virtio/vhost-user.c +9 -2
+            mem->regions[mem->nregions].mmap_offset = offset;
+            fds[mem->nregions++] = fd;
+        }
+    }
+
+    return 0;
+}
+
 static int vhost_user_set_mem_table(struct vhost_dev *dev,
                                     struct vhost_memory *mem)  {
     int fds[VHOST_MEMORY_MAX_NREGIONS];
-    int i, fd;
-    size_t fd_num = 0;
+    size_t fd_num;
     bool reply_supported =

virtio_has_feature(dev->protocol_features,

VHOST_USER_PROTOCOL_F_REPLY_ACK);

hw/virtio/vhost-user.c +0 -0
@@ -348,29 +381,12 @@ static int vhost_user_set_mem_table(struct
Quoted 1 line

vhost_dev

*dev,

Quoted 11 lines
         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
     }
hw/virtio/vhost-user.c +0 -7
-    for (i = 0; i < dev->mem->nregions; ++i) {
-        struct vhost_memory_region *reg = dev->mem->regions + i;
-        ram_addr_t offset;
-        MemoryRegion *mr;
-
-        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
-        mr = memory_region_from_host((void *)(uintptr_t)reg-

userspace_addr,

hw/virtio/vhost-user.c static int vhost_user_set_mem_table(struct vhost_dev *dev,

Quoted 5 lines
hw/virtio/vhost-user.c +0 -5
-                                     &offset);
-        fd = memory_region_get_fd(mr);
-        if (fd > 0) {
-            if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
-                error_report("Failed preparing vhost-user memory table

msg");

Quoted 22 lines
hw/virtio/vhost-user.c +0 -3
-                return -1;
-            }
-            msg.payload.memory.regions[fd_num].userspace_addr = reg-

userspace_addr;
- msg.payload.memory.regions[fd_num].memory_size = reg-
memory_size;
- msg.payload.memory.regions[fd_num].guest_phys_addr = reg-
guest_phys_addr;

hw/virtio/vhost-user.c +4 -4
-            msg.payload.memory.regions[fd_num].mmap_offset = offset;
-            fds[fd_num++] = fd;
-        }
+    if (vhost_user_prepare_msg(dev, &msg.payload.memory, fds) < 0) {
+        error_report("Failed preparing vhost-user memory table msg");
+        return -1;
     }
 
-    msg.payload.memory.nregions = fd_num;
+    fd_num = msg.payload.memory.nregions;
 
     if (!fd_num) {
         error_report("Failed initializing vhost-user memory map, "
@@ -886,9 +902,9 @@ static int vhost_user_get_vq_index(struct
Quoted 1 line

vhost_dev

*dev, int idx)

Quoted 12 lines
     return idx;

}

hw/virtio/vhost-user.c +2 -3
-static int vhost_user_memslots_limit(struct vhost_dev *dev)
+static bool vhost_user_has_free_memslots(struct vhost_dev *dev)
 {
-    return VHOST_MEMORY_MAX_NREGIONS;
+    return vhost_user_free_memslots;
 }
 
 static bool vhost_user_requires_shm_log(struct vhost_dev *dev) @@
-1156,11 +1172,19 @@ vhost_user_crypto_close_session(struct
Quoted 1 line

vhost_dev *dev,

uint64_t session_id)

hw/virtio/vhost-user.c vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id) · line 1180

Quoted 8 lines
     return 0;

}

hw/virtio/vhost-user.c +5 -0
+static void vhost_user_set_used_memslots(struct vhost_dev *dev) {
+    int fds[VHOST_MEMORY_MAX_NREGIONS];
+    VhostUserMsg msg;
+
+    vhost_user_prepare_msg(dev, &msg.payload.memory, fds);

Oops. This is something I don't understand.

Quoted 2 lines

Why is the message prepared here and then discarded?

The purpose of vhost_user_set_used_memslots() is to set the boolean value of vhost_user_free_memslots, which indicating whether there're free memeslots for vhost user. Since there're code duplicating inside vhost_user_set_used_memslots() and vhost_user_set_mem_table(), Igor suggested that we could create a new function to avoid duplicating. Here, the value of VhostUserMsg is not needed by the caller vhost_user_set_used_memslots(), so we just discarded.

Quoted 6 lines
Quoted 2 lines

Regards,
Jay

I think I misunderstood the meaning of that variable. It seems to be set when there are more slots than supported.

Yes.

Quoted 5 lines

What vhost_user_free_memslots implies is that it is set when there are no free slots, even if existing config fits.

A better name would be vhost_user_out_of_memslots maybe?

vhost_user_free_memslots is set TRUE by default, if there are more slots than supported it is set to FALSE.
vhost_user_out_of_memslots is another option, I think it should be set FALSE by default, if there are more slots than supported it is set to TRUE.

Since two functions vhost_has_free_slot() and the callback vhost_backend_has_free_memslots() are using this variable, the name vhost_user_free_memslots seems a little matching to these function names.

If you still prefer vhost_user_out_of_memslots, pls let me know.

Quoted 5 lines

And I missed the fact that it (as well as the prepare call) can actually fail when out of slots.
Shouldn't it return status too?

vhost_user_free_memslots is always set to false when prepare call failed, this is what vhost_user_set_used_memslots() wants to do, so e.g. when we hotplug memory DIMM devices, it will return false while calling vhost_has_free_slot().

So, I think vhost_user_set_used_memslots() doesn't need to handle or care about the return status of vhost_user_prepare_msg(), the return value is only useful to another caller vhost_user_set_mem_table()

Regards,
Jay

Quoted 138 lines
Quoted 17 lines
Quoted 17 lines
Quoted 7 lines
hw/virtio/vhost-user.c +2 -0
+}
+
 const VhostOps user_ops = {
         .backend_type = VHOST_BACKEND_TYPE_USER,
         .vhost_backend_init = vhost_user_init,

hw/virtio/vhost-user.c vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id)

Quoted 10 lines
hw/virtio/vhost-user.c +3 -2
         .vhost_backend_cleanup = vhost_user_cleanup,
-        .vhost_backend_memslots_limit = vhost_user_memslots_limit,
+        .vhost_backend_has_free_memslots =
+ vhost_user_has_free_memslots,
         .vhost_set_log_base = vhost_user_set_log_base,
         .vhost_set_mem_table = vhost_user_set_mem_table,
         .vhost_set_vring_addr = vhost_user_set_vring_addr, @@
-1184,6
+1208,7 @@ const VhostOps user_ops = {
         .vhost_send_device_iotlb_msg =

vhost_user_send_device_iotlb_msg,

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

.vhost_get_config = vhost_user_get_config, .vhost_set_config = vhost_user_set_config,
+ .vhost_set_used_memslots = vhost_user_set_used_memslots,
.vhost_crypto_create_session =

vhost_user_crypto_create_session,

Quoted 115 lines
Quoted 31 lines
Quoted 31 lines

.vhost_crypto_close_session =
vhost_user_crypto_close_session, }; diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index d8d0ef9..17262d2 100644

hw/virtio/vhost.c +0 -1
@@ -44,20 +44,19 @@
 static struct vhost_log *vhost_log;  static struct vhost_log
 *vhost_log_shm;
 
-static unsigned int used_memslots;  static QLIST_HEAD(,

vhost_dev) vhost_devices =

     QLIST_HEAD_INITIALIZER(vhost_devices);

bool vhost_has_free_slot(void)
{

hw/virtio/vhost.c +0 -2
-    unsigned int slots_limit = ~0U;
     struct vhost_dev *hdev;
 
     QLIST_FOREACH(hdev, &vhost_devices, entry) {
-        unsigned int r = hdev->vhost_ops-

vhost_backend_memslots_limit(hdev);

hw/virtio/vhost.c +4 -2
-        slots_limit = MIN(slots_limit, r);
+        if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev)) {
+            return false;
+        }
     }
-    return slots_limit > used_memslots;
+    return true;
 }
Quoted 4 lines
Quoted 4 lines

static void vhost_dev_sync_region(struct vhost_dev *dev, @@
-446,7
+445,6 @@ static void vhost_commit(MemoryListener *listener)

                        dev->n_mem_sections * sizeof dev->mem-

regions[0];

Quoted 79 lines
Quoted 16 lines
     dev->mem = g_realloc(dev->mem, regions_size);
     dev->mem->nregions = dev->n_mem_sections;
-    used_memslots = dev->mem->nregions;
     for (i = 0; i < dev->n_mem_sections; i++) {
         struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
         struct MemoryRegionSection *mrs = dev->mem_sections + i;
@@
-458,6 +456,7 @@ static void vhost_commit(MemoryListener *listener)
             mrs->offset_within_region;
         cur_vmr->flags_padding   = 0;
     }
hw/virtio/vhost.c +1 -0
+    dev->vhost_ops->vhost_set_used_memslots(dev);
 
     if (!dev->started) {
         goto out;
@@ -1202,7 +1201,7 @@ int vhost_dev_init(struct vhost_dev *hdev,
Quoted 1 line

void

*opaque,

Quoted 1 line
     memory_listener_register(&hdev->memory_listener,

&address_space_memory);

Quoted 25 lines
     QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);

- if (used_memslots > hdev->vhost_ops-
vhost_backend_memslots_limit(hdev)) {

hw/virtio/vhost.c +2 -0
+    if (!hdev->vhost_ops->vhost_backend_has_free_memslots(hdev))
+ {
         error_report("vhost backend memory slots limit is less"
                 " than current number of present memory slots");
         r = -1;
include/hw/virtio/vhost-backend.h +2 -0
index 5dac61f..ff06c2d 100644
@@ -36,7 +36,7 @@ struct vhost_iotlb_msg;
 
 typedef int (*vhost_backend_init)(struct vhost_dev *dev, void
 *opaque);  typedef int (*vhost_backend_cleanup)(struct vhost_dev
 *dev); -typedef int (*vhost_backend_memslots_limit)(struct
 vhost_dev *dev);
+typedef bool (*vhost_backend_has_free_memslots)(struct vhost_dev
+*dev);
 
 typedef int (*vhost_net_set_backend_op)(struct vhost_dev *dev,
                                 struct vhost_vring_file *file);
Quoted 3 lines

@@
-94,6 +94,7 @@ typedef int (*vhost_set_config_op)(struct vhost_dev *dev,

const uint8_t *data,

Quoted 30 lines

uint32_t flags); typedef int
(*vhost_get_config_op)(struct vhost_dev *dev, uint8_t *config,

                                    uint32_t config_len);
include/hw/virtio/vhost-backend.h +2 -0
+typedef void (*vhost_set_used_memslots_op)(struct vhost_dev
+*dev);
 
 typedef int (*vhost_crypto_create_session_op)(struct vhost_dev *dev,
                                               void *session_info,

@@

-105,7 +106,7 @@ typedef struct VhostOps {
     VhostBackendType backend_type;
     vhost_backend_init vhost_backend_init;
     vhost_backend_cleanup vhost_backend_cleanup;
include/hw/virtio/vhost-backend.h +4 -1
-    vhost_backend_memslots_limit vhost_backend_memslots_limit;
+    vhost_backend_has_free_memslots
+ vhost_backend_has_free_memslots;
     vhost_net_set_backend_op vhost_net_set_backend;
     vhost_net_set_mtu_op vhost_net_set_mtu;
     vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint; @@ -136,6
+137,7 @@ typedef struct VhostOps {
     vhost_send_device_iotlb_msg_op vhost_send_device_iotlb_msg;
     vhost_get_config_op vhost_get_config;
     vhost_set_config_op vhost_set_config;
+    vhost_set_used_memslots_op vhost_set_used_memslots;
     vhost_crypto_create_session_op vhost_crypto_create_session;
     vhost_crypto_close_session_op vhost_crypto_close_session;  }

VhostOps;
--
1.8.3.1

#7 lore raw
Quoted 295 lines · Zhoujian (jay)
Quoted 275 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 10:51 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei (Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 22 lines · Zhoujian (jay)

Hi Michael,

Quoted 20 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 9:34 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei
(Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 8 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-

kernel respectively.

Quoted 22 lines
Quoted 3 lines

Below should go after ---

Thanks for reminding.

Quoted 16 lines
Quoted 9 lines

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

Quoted 3 lines

When built with clang this causes runtime warnings (during make check) about misaligned access to structures.

Quoted 12 lines
Quoted 3 lines

The issue is that vhost_user_prepare_msg requests VhostUserMemory which compiler assumes but is then used with a pointer into a packed structure - where fields are not aligned.

Sorry I missed the patch you have sent to fix the alignment, I have replied to that thread.

Quoted 5 lines
Quoted 5 lines

---
 hw/virtio/vhost-backend.c         | 15 +++++++-
 hw/virtio/vhost-user.c            | 77 ++++++++++++++++++++++++++-----

----

Quoted 205 lines
Quoted 194 lines

----

Quoted 15 lines
 hw/virtio/vhost.c                 | 13 +++----
 include/hw/virtio/vhost-backend.h |  6 ++-
 4 files changed, 75 insertions(+), 36 deletions(-)
hw/virtio/vhost-backend.c +2 -0
index 7f09efa..59def69 100644
@@ -15,6 +15,8 @@
 #include "hw/virtio/vhost-backend.h"
 #include "qemu/error-report.h"
 
+static unsigned int vhost_kernel_used_memslots;
+
 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long
Quoted 1 line

int

request,

Quoted 55 lines
                              void *arg)  { @@ -62,6 +64,11 @@
static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
     return limit;

}

hw/virtio/vhost-backend.c +4 -0
+static bool vhost_kernel_has_free_memslots(struct vhost_dev *dev) {
+    return vhost_kernel_used_memslots <
+vhost_kernel_memslots_limit(dev); }
+
 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
                                         struct vhost_vring_file

*file) { @@ -233,11 +240,16 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,

         qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL,

NULL); }

hw/virtio/vhost-backend.c +5 -1
+static void vhost_kernel_set_used_memslots(struct vhost_dev *dev) {
+    vhost_kernel_used_memslots = dev->mem->nregions; }
+
 static const VhostOps kernel_ops = {
         .backend_type = VHOST_BACKEND_TYPE_KERNEL,
         .vhost_backend_init = vhost_kernel_init,
         .vhost_backend_cleanup = vhost_kernel_cleanup,
-        .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
+        .vhost_backend_has_free_memslots =
+ vhost_kernel_has_free_memslots,
         .vhost_net_set_backend = vhost_kernel_net_set_backend,
         .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
         .vhost_scsi_clear_endpoint =

vhost_kernel_scsi_clear_endpoint, @@ -264,6 +276,7 @@ static const VhostOps kernel_ops = { #endif /* CONFIG_VHOST_VSOCK */
.vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback, .vhost_send_device_iotlb_msg =
vhost_kernel_send_device_iotlb_msg,
+ .vhost_set_used_memslots =
+ vhost_kernel_set_used_memslots,
};

int vhost_set_backend_type(struct vhost_dev *dev,
VhostBackendType
backend_type) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 41ff5cf..ef14249 100644

hw/virtio/vhost-user.c +2 -0
@@ -163,6 +163,8 @@ static VhostUserMsg m __attribute__
 ((unused));
 /* The version of the protocol we support */
 #define VHOST_USER_VERSION    (0x1)
 
+static bool vhost_user_free_memslots = true;
+
 struct vhost_user {
     CharBackend *chr;
     int slave_fd;
hw/virtio/vhost-user.c +0 -0
@@ -330,12 +332,43 @@ static int vhost_user_set_log_base(struct
Quoted 1 line

vhost_dev

*dev, uint64_t base,

hw/virtio/vhost-user.c

Quoted 5 lines
     return 0;

}

hw/virtio/vhost-user.c +2 -0
+static int vhost_user_prepare_msg(struct vhost_dev *dev,
+VhostUserMemory

*mem,

Quoted 45 lines
hw/virtio/vhost-user.c +11 -0
+                                  int *fds) {
+    int i, fd;
+
+    vhost_user_free_memslots = true;
+    for (i = 0, mem->nregions = 0; i < dev->mem->nregions; ++i) {
+        struct vhost_memory_region *reg = dev->mem->regions + i;
+        ram_addr_t offset;
+        MemoryRegion *mr;
+
+        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
+        mr = memory_region_from_host((void *)(uintptr_t)reg-

userspace_addr,

hw/virtio/vhost-user.c +9 -0
+                                     &offset);
+        fd = memory_region_get_fd(mr);
+        if (fd > 0) {
+            if (mem->nregions == VHOST_MEMORY_MAX_NREGIONS) {
+                vhost_user_free_memslots = false;
+                return -1;
+            }
+
+            mem->regions[mem->nregions].userspace_addr = reg-

userspace_addr;

hw/virtio/vhost-user.c +2 -0
+            mem->regions[mem->nregions].memory_size = reg->memory_size;
+            mem->regions[mem->nregions].guest_phys_addr = reg-

guest_phys_addr;

hw/virtio/vhost-user.c +9 -2
+            mem->regions[mem->nregions].mmap_offset = offset;
+            fds[mem->nregions++] = fd;
+        }
+    }
+
+    return 0;
+}
+
 static int vhost_user_set_mem_table(struct vhost_dev *dev,
                                     struct vhost_memory *mem)  {
     int fds[VHOST_MEMORY_MAX_NREGIONS];
-    int i, fd;
-    size_t fd_num = 0;
+    size_t fd_num;
     bool reply_supported =

virtio_has_feature(dev->protocol_features,

VHOST_USER_PROTOCOL_F_REPLY_ACK);

hw/virtio/vhost-user.c +0 -0
@@ -348,29 +381,12 @@ static int vhost_user_set_mem_table(struct
Quoted 1 line

vhost_dev

*dev,

Quoted 11 lines
         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
     }
hw/virtio/vhost-user.c +0 -7
-    for (i = 0; i < dev->mem->nregions; ++i) {
-        struct vhost_memory_region *reg = dev->mem->regions + i;
-        ram_addr_t offset;
-        MemoryRegion *mr;
-
-        assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
-        mr = memory_region_from_host((void *)(uintptr_t)reg-

userspace_addr,

hw/virtio/vhost-user.c static int vhost_user_set_mem_table(struct vhost_dev *dev,

Quoted 5 lines
hw/virtio/vhost-user.c +0 -5
-                                     &offset);
-        fd = memory_region_get_fd(mr);
-        if (fd > 0) {
-            if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
-                error_report("Failed preparing vhost-user memory table

msg");

Quoted 22 lines
hw/virtio/vhost-user.c +0 -3
-                return -1;
-            }
-            msg.payload.memory.regions[fd_num].userspace_addr = reg-

userspace_addr;
- msg.payload.memory.regions[fd_num].memory_size = reg-
memory_size;
- msg.payload.memory.regions[fd_num].guest_phys_addr = reg-
guest_phys_addr;

hw/virtio/vhost-user.c +4 -4
-            msg.payload.memory.regions[fd_num].mmap_offset = offset;
-            fds[fd_num++] = fd;
-        }
+    if (vhost_user_prepare_msg(dev, &msg.payload.memory, fds) < 0) {
+        error_report("Failed preparing vhost-user memory table msg");
+        return -1;
     }
 
-    msg.payload.memory.nregions = fd_num;
+    fd_num = msg.payload.memory.nregions;
 
     if (!fd_num) {
         error_report("Failed initializing vhost-user memory map, "
@@ -886,9 +902,9 @@ static int vhost_user_get_vq_index(struct
Quoted 1 line

vhost_dev

*dev, int idx)

Quoted 12 lines
     return idx;

}

hw/virtio/vhost-user.c +2 -3
-static int vhost_user_memslots_limit(struct vhost_dev *dev)
+static bool vhost_user_has_free_memslots(struct vhost_dev *dev)
 {
-    return VHOST_MEMORY_MAX_NREGIONS;
+    return vhost_user_free_memslots;
 }
 
 static bool vhost_user_requires_shm_log(struct vhost_dev *dev) @@
-1156,11 +1172,19 @@ vhost_user_crypto_close_session(struct
Quoted 1 line

vhost_dev *dev,

uint64_t session_id)

hw/virtio/vhost-user.c vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id) · line 1180

Quoted 8 lines
     return 0;

}

hw/virtio/vhost-user.c +5 -0
+static void vhost_user_set_used_memslots(struct vhost_dev *dev) {
+    int fds[VHOST_MEMORY_MAX_NREGIONS];
+    VhostUserMsg msg;
+
+    vhost_user_prepare_msg(dev, &msg.payload.memory, fds);

Oops. This is something I don't understand.

Quoted 2 lines

Why is the message prepared here and then discarded?

The purpose of vhost_user_set_used_memslots() is to set the boolean value of vhost_user_free_memslots, which indicating whether there're free memeslots for vhost user. Since there're code duplicating inside vhost_user_set_used_memslots() and vhost_user_set_mem_table(), Igor suggested that we could create a new function to avoid duplicating. Here, the value of VhostUserMsg is not needed by the caller vhost_user_set_used_memslots(), so we just discarded.

Quoted 6 lines
Quoted 2 lines

Regards,
Jay

I think I misunderstood the meaning of that variable. It seems to be set when there are more slots than supported.

Yes.

Quoted 5 lines

What vhost_user_free_memslots implies is that it is set when there are no free slots, even if existing config fits.

A better name would be vhost_user_out_of_memslots maybe?

vhost_user_free_memslots is set TRUE by default, if there are more slots than supported it is set to FALSE.
vhost_user_out_of_memslots is another option, I think it should be set FALSE by default, if there are more slots than supported it is set to TRUE.

Quoted 4 lines

Since two functions vhost_has_free_slot() and the callback vhost_backend_has_free_memslots() are using this variable, the name vhost_user_free_memslots seems a little matching to these function names.

So vhost_has_free_slot is actually slightly wrong after your patch too.

Quoted 12 lines

If you still prefer vhost_user_out_of_memslots, pls let me know.

Quoted 5 lines

And I missed the fact that it (as well as the prepare call) can actually fail when out of slots.
Shouldn't it return status too?

vhost_user_free_memslots is always set to false when prepare call failed, this is what vhost_user_set_used_memslots() wants to do, so e.g. when we hotplug memory DIMM devices, it will return false while calling vhost_has_free_slot().

Quoted 6 lines

So, I think vhost_user_set_used_memslots() doesn't need to handle or care about the return status of vhost_user_prepare_msg(), the return value is only useful to another caller vhost_user_set_mem_table()

Regards,
Jay

So function names are a problem here I think. If the function has an important side effect it should be reflected in the name. Or we could add a wrapper which does the right thing.

Signature · 1 line
MST
#8 lore raw
Quoted 331 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 8:36 PM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei (Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 66 lines · Zhoujian (jay)
Quoted 66 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 10:51 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei
(Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 25 lines · Zhoujian (jay)

Hi Michael,

Quoted 23 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 9:34 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei
(Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 11 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to
dev->mem->nregions, which is correct for vhost kernel, but not dev->mem->for
vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-

kernel respectively.

Quoted 26 lines
Quoted 3 lines

Below should go after ---

Thanks for reminding.

Quoted 19 lines
Quoted 9 lines

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

Quoted 3 lines

When built with clang this causes runtime warnings (during make check) about misaligned access to structures.

The issue is that vhost_user_prepare_msg requests VhostUserMemory which compiler assumes but is then used with a pointer into a packed structure - where fields are not aligned.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Sorry I missed the patch you have sent to fix the alignment, I have replied to that thread.

Quoted 5 lines
Quoted 5 lines

---
 hw/virtio/vhost-backend.c         | 15 +++++++-
 hw/virtio/vhost-user.c            | 77 ++++++++++++++++++++++++++-

----

Quoted 19 lines
Quoted 19 lines

----

Quoted 18 lines
Quoted 18 lines

----

Quoted 15 lines
 hw/virtio/vhost.c                 | 13 +++----
 include/hw/virtio/vhost-backend.h |  6 ++-
 4 files changed, 75 insertions(+), 36 deletions(-)
(quoted diff) +0 -0

b/hw/virtio/vhost-backend.c index 7f09efa..59def69 100644

hw/virtio/vhost-backend.c +2 -0
@@ -15,6 +15,8 @@
 #include "hw/virtio/vhost-backend.h"
 #include "qemu/error-report.h"
 
+static unsigned int vhost_kernel_used_memslots;
+
 static int vhost_kernel_call(struct vhost_dev *dev, unsigned
Quoted 1 line

long int

request,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
                              void *arg)  { @@ -62,6 +64,11 @@
static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
     return limit;

}

+static bool vhost_kernel_has_free_memslots(struct vhost_dev *dev)

{

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost-backend.c +3 -0
+    return vhost_kernel_used_memslots <
+vhost_kernel_memslots_limit(dev); }
+
 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
                                         struct

vhost_vring_file
*file) { @@ -233,11 +240,16 @@ static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,

         qemu_set_fd_handler((uintptr_t)dev->opaque, NULL,

NULL, NULL); }

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+static void vhost_kernel_set_used_memslots(struct vhost_dev *dev)

{

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

hw/virtio/vhost-backend.c static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,

Quoted 7 lines
hw/virtio/vhost-backend.c +2 -1
+    vhost_kernel_used_memslots = dev->mem->nregions; }
+
 static const VhostOps kernel_ops = {
         .backend_type = VHOST_BACKEND_TYPE_KERNEL,
         .vhost_backend_init = vhost_kernel_init,
         .vhost_backend_cleanup = vhost_kernel_cleanup,
-        .vhost_backend_memslots_limit =

vhost_kernel_memslots_limit,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+ .vhost_backend_has_free_memslots =
+ vhost_kernel_has_free_memslots,
.vhost_net_set_backend = vhost_kernel_net_set_backend, .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint, .vhost_scsi_clear_endpoint =
vhost_kernel_scsi_clear_endpoint, @@ -264,6 +276,7 @@ static const VhostOps kernel_ops = { #endif /* CONFIG_VHOST_VSOCK */
.vhost_set_iotlb_callback =

vhost_kernel_set_iotlb_callback,

Quoted 84 lines
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 23 lines

.vhost_send_device_iotlb_msg =
vhost_kernel_send_device_iotlb_msg,
+ .vhost_set_used_memslots =
+ vhost_kernel_set_used_memslots,
};

int vhost_set_backend_type(struct vhost_dev *dev,
VhostBackendType
backend_type) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 41ff5cf..ef14249 100644

hw/virtio/vhost-user.c +2 -0
@@ -163,6 +163,8 @@ static VhostUserMsg m __attribute__
 ((unused));
 /* The version of the protocol we support */
 #define VHOST_USER_VERSION    (0x1)
 
+static bool vhost_user_free_memslots = true;
+
 struct vhost_user {
     CharBackend *chr;
     int slave_fd;
hw/virtio/vhost-user.c +0 -0
@@ -330,12 +332,43 @@ static int
Quoted 1 line

vhost_user_set_log_base(struct vhost_dev

*dev, uint64_t base,

hw/virtio/vhost-user.c

Quoted 5 lines
     return 0;

}

hw/virtio/vhost-user.c +2 -0
+static int vhost_user_prepare_msg(struct vhost_dev *dev,
+VhostUserMemory

*mem,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

hw/virtio/vhost-user.c static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,

Quoted 10 lines
hw/virtio/vhost-user.c +10 -0
+                                  int *fds) {
+    int i, fd;
+
+    vhost_user_free_memslots = true;
+    for (i = 0, mem->nregions = 0; i < dev->mem->nregions; ++i) {
+        struct vhost_memory_region *reg = dev->mem->regions + i;
+        ram_addr_t offset;
+        MemoryRegion *mr;
+
+        assert((uintptr_t)reg->userspace_addr == reg-

userspace_addr);

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

+ mr = memory_region_from_host((void *)(uintptr_t)reg-
userspace_addr,

hw/virtio/vhost-user.c +3 -0
+                                     &offset);
+        fd = memory_region_get_fd(mr);
+        if (fd > 0) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost-user.c +6 -0
+            if (mem->nregions == VHOST_MEMORY_MAX_NREGIONS) {
+                vhost_user_free_memslots = false;
+                return -1;
+            }
+
+            mem->regions[mem->nregions].userspace_addr = reg-

userspace_addr;
+ mem->regions[mem->nregions].memory_size = reg-

memory_size;

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 23 lines

+ mem->regions[mem->nregions].guest_phys_addr =
+ reg-
guest_phys_addr;

hw/virtio/vhost-user.c +9 -2
+            mem->regions[mem->nregions].mmap_offset = offset;
+            fds[mem->nregions++] = fd;
+        }
+    }
+
+    return 0;
+}
+
 static int vhost_user_set_mem_table(struct vhost_dev *dev,
                                     struct vhost_memory *mem)  {
     int fds[VHOST_MEMORY_MAX_NREGIONS];
-    int i, fd;
-    size_t fd_num = 0;
+    size_t fd_num;
     bool reply_supported =

virtio_has_feature(dev->protocol_features,

VHOST_USER_PROTOCOL_F_REPLY_ACK);

hw/virtio/vhost-user.c +0 -0
@@ -348,29 +381,12 @@ static int
Quoted 1 line

vhost_user_set_mem_table(struct vhost_dev

*dev,

Quoted 3 lines
         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
     }
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

hw/virtio/vhost-user.c static int vhost_user_set_mem_table(struct vhost_dev *dev,

Quoted 6 lines
hw/virtio/vhost-user.c +0 -6
-    for (i = 0; i < dev->mem->nregions; ++i) {
-        struct vhost_memory_region *reg = dev->mem->regions + i;
-        ram_addr_t offset;
-        MemoryRegion *mr;
-
-        assert((uintptr_t)reg->userspace_addr == reg-

userspace_addr);

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

- mr = memory_region_from_host((void *)(uintptr_t)reg-
userspace_addr,

hw/virtio/vhost-user.c +0 -5
-                                     &offset);
-        fd = memory_region_get_fd(mr);
-        if (fd > 0) {
-            if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
-                error_report("Failed preparing vhost-user memory

table

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

msg");

Quoted 3 lines
hw/virtio/vhost-user.c +0 -3
-                return -1;
-            }
-            msg.payload.memory.regions[fd_num].userspace_addr =

reg-

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

userspace_addr;
- msg.payload.memory.regions[fd_num].memory_size = reg-
memory_size;
- msg.payload.memory.regions[fd_num].guest_phys_addr =

reg-

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

guest_phys_addr;
- msg.payload.memory.regions[fd_num].mmap_offset =

offset;

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

hw/virtio/vhost-user.c static int vhost_user_set_mem_table(struct vhost_dev *dev,

Quoted 3 lines
hw/virtio/vhost-user.c +1 -2
-            fds[fd_num++] = fd;
-        }
+    if (vhost_user_prepare_msg(dev, &msg.payload.memory, fds) < 0)

{

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ error_report("Failed preparing vhost-user memory table

msg");

Quoted 70 lines
Quoted 50 lines
Quoted 50 lines
Quoted 38 lines
Quoted 10 lines
hw/virtio/vhost-user.c +2 -1
+        return -1;
     }
 
-    msg.payload.memory.nregions = fd_num;
+    fd_num = msg.payload.memory.nregions;
 
     if (!fd_num) {
         error_report("Failed initializing vhost-user memory map, "
@@ -886,9 +902,9 @@ static int vhost_user_get_vq_index(struct

vhost_dev

*dev, int idx)

Quoted 12 lines
     return idx;

}

hw/virtio/vhost-user.c +3 -2
-static int vhost_user_memslots_limit(struct vhost_dev *dev)
+static bool vhost_user_has_free_memslots(struct vhost_dev
+*dev)
 {
-    return VHOST_MEMORY_MAX_NREGIONS;
+    return vhost_user_free_memslots;
 }
 
 static bool vhost_user_requires_shm_log(struct vhost_dev
Quoted 3 lines

*dev) @@
-1156,11 +1172,19 @@ vhost_user_crypto_close_session(struct vhost_dev *dev,

uint64_t session_id)

hw/virtio/vhost-user.c vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id) · line 1180

Quoted 8 lines
     return 0;

}

hw/virtio/vhost-user.c +5 -0
+static void vhost_user_set_used_memslots(struct vhost_dev *dev) {
+    int fds[VHOST_MEMORY_MAX_NREGIONS];
+    VhostUserMsg msg;
+
+    vhost_user_prepare_msg(dev, &msg.payload.memory, fds);

Oops. This is something I don't understand.

Quoted 2 lines

Why is the message prepared here and then discarded?

The purpose of vhost_user_set_used_memslots() is to set the boolean value of vhost_user_free_memslots, which indicating whether there're free memeslots for vhost user. Since there're code duplicating inside
vhost_user_set_used_memslots() and vhost_user_set_mem_table(), Igor suggested that we could create a new function to avoid duplicating. Here, the value of VhostUserMsg is not needed by the caller vhost_user_set_used_memslots(), so we just discarded.

Quoted 6 lines
Quoted 2 lines

Regards,
Jay

I think I misunderstood the meaning of that variable. It seems to be set when there are more slots than supported.

Yes.

Quoted 5 lines

What vhost_user_free_memslots implies is that it is set when there are no free slots, even if existing config fits.

A better name would be vhost_user_out_of_memslots maybe?

vhost_user_free_memslots is set TRUE by default, if there are more slots than supported it is set to FALSE. vhost_user_out_of_memslots is another option, I think it should be set FALSE by default, if there are more slots than supported it is set to TRUE.

Quoted 6 lines
Quoted 4 lines

Since two functions vhost_has_free_slot() and the callback vhost_backend_has_free_memslots() are using this variable, the name vhost_user_free_memslots seems a little matching to these function names.

So vhost_has_free_slot is actually slightly wrong after your patch too.

It should be set to FALSE if the number of slots is equal to the limit, indeed.

Regards,
Jay

Quoted 28 lines
Quoted 6 lines

If you still prefer vhost_user_out_of_memslots, pls let me know.

Quoted 2 lines
Quoted 7 lines
Quoted 3 lines

And I missed the fact that it (as well as the prepare call) can actually fail when out of slots.
Shouldn't it return status too?

vhost_user_free_memslots is always set to false when prepare call failed, this is what vhost_user_set_used_memslots() wants to do, so e.g. when we hotplug memory DIMM devices, it will return false while

calling vhost_has_free_slot().

Quoted 7 lines

So, I think vhost_user_set_used_memslots() doesn't need to handle or care about the return status of vhost_user_prepare_msg(), the return value is only useful to another caller vhost_user_set_mem_table()

Regards,
Jay

So function names are a problem here I think. If the function has an important side effect it should be reflected in the name. Or we could add a wrapper which does the right thing.

--
MST

#9 lore raw
In reply to Zhoujian (jay)
Quoted 42 lines · Zhoujian (jay)

Hi Michael,

Quoted 20 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 9:34 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei (Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 8 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-kernel respectively.

Quoted 1 line

Below should go after ---

Thanks for reminding.

Quoted 16 lines
Quoted 9 lines

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

Quoted 3 lines

When built with clang this causes runtime warnings (during make check) about misaligned access to structures.

Quoted 6 lines
Quoted 3 lines

The issue is that vhost_user_prepare_msg requests VhostUserMemory which compiler assumes but is then used with a pointer into a packed structure - where fields are not aligned.

Sorry I missed the patch you have sent to fix the alignment, I have replied to that thread.

I've dropped this from the pull for now. Sorry about that. Once next pull is merged, pls rebase and post a version fixing up the alignment issues.

Thanks for your effort!

Signature · 1 line
MST
#10 lore raw
Quoted 61 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 11:14 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei (Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 22 lines · Zhoujian (jay)

Hi Michael,

Quoted 20 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Tuesday, March 20, 2018 9:34 AM
To: Zhoujian (jay) <[email protected]>
Cc: [email protected]; [email protected]; Huangweidong (C) <[email protected]>; wangxin (U) <[email protected]>; Gonglei
(Arei) <[email protected]>; Liuzhe (Ahriy, Euler) <[email protected]>
Subject: Re: [PATCH v9] vhost: used_memslots refactoring

Quoted 8 lines · Jay Zhou

Used_memslots is shared by vhost kernel and user, it is equal to dev->mem->nregions, which is correct for vhost kernel, but not for vhost user, the latter one uses memory regions that have file descriptor. E.g. a VM has a vhost-user NIC and 8(vhost user memslot upper limit) memory slots, it will be failed to hotplug a new DIMM device since vhost_has_free_slot() finds no free slot left. It should be successful if only part of memory slots have file descriptor, so setting used memslots for vhost-user and vhost-

kernel respectively.

Quoted 26 lines
Quoted 3 lines

Below should go after ---

Thanks for reminding.

Quoted 19 lines
Quoted 9 lines

v7 ... v9:
- rebased on the master
v2 ... v6:
- delete the "used_memslots" global variable, and add it for vhost-user and vhost-kernel separately
- refine the function, commit log
- used_memslots refactoring

Quoted 3 lines

When built with clang this causes runtime warnings (during make check) about misaligned access to structures.

The issue is that vhost_user_prepare_msg requests VhostUserMemory which compiler assumes but is then used with a pointer into a packed structure - where fields are not aligned.

Quoted 7 lines
Quoted 2 lines

Sorry I missed the patch you have sent to fix the alignment, I have replied to that thread.

I've dropped this from the pull for now. Sorry about that. Once next pull is merged, pls rebase and post a version fixing up the alignment issues.

Thanks for your effort!

Hi Michael,
Sorry for the trouble, it's okay for me, will fix the alignment issues in the next version.

Regards,
Jay

Quoted 3 lines

--
MST