[PATCH] vhost: reduce the set_mem_table call frenquency

Li Feng posted 1 patch 1 year, 8 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20220823053820.35499-1-fengli@smartx.com
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>
hw/virtio/vhost.c         | 14 ++++++++++++++
include/hw/virtio/vhost.h |  2 ++
2 files changed, 16 insertions(+)
[PATCH] vhost: reduce the set_mem_table call frenquency
Posted by Li Feng 1 year, 8 months ago
If the vhost memory layout doesn't change, don't need to call the vhost
backend.
The set_mem_table is time consuming when sending to vhost-user backend.

On aarch64, the edk2 uefi firmware will write the pflash which will
trigger the vhost_commit hundreds of times.

Signed-off-by: Li Feng <fengli@smartx.com>
---
 hw/virtio/vhost.c         | 14 ++++++++++++++
 include/hw/virtio/vhost.h |  2 ++
 2 files changed, 16 insertions(+)

diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index f758f177bb..848d2f20d6 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -523,6 +523,11 @@ static void vhost_commit(MemoryListener *listener)
     /* Rebuild the regions list from the new sections list */
     regions_size = offsetof(struct vhost_memory, regions) +
                        dev->n_mem_sections * sizeof dev->mem->regions[0];
+    if (dev->mem && dev->started) {
+        g_free(dev->old_mem);
+        dev->old_mem = dev->mem;
+        dev->mem = NULL;
+    }
     dev->mem = g_realloc(dev->mem, regions_size);
     dev->mem->nregions = dev->n_mem_sections;
     used_memslots = dev->mem->nregions;
@@ -542,6 +547,12 @@ static void vhost_commit(MemoryListener *listener)
         goto out;
     }
 
+    if (dev->old_mem && dev->regions_size == regions_size &&
+            memcmp(dev->mem, dev->old_mem, dev->regions_size) == 0) {
+        goto out;
+    }
+
+    dev->regions_size = regions_size;
     for (i = 0; i < dev->mem->nregions; i++) {
         if (vhost_verify_ring_mappings(dev,
                        (void *)(uintptr_t)dev->mem->regions[i].userspace_addr,
@@ -1445,6 +1456,8 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
     hdev->n_mem_sections = 0;
     hdev->mem_sections = NULL;
+    hdev->old_mem = NULL;
+    hdev->regions_size = 0;
     hdev->log = NULL;
     hdev->log_size = 0;
     hdev->log_enabled = false;
@@ -1491,6 +1504,7 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
     }
     g_free(hdev->mem);
     g_free(hdev->mem_sections);
+    g_free(hdev->old_mem);
     if (hdev->vhost_ops) {
         hdev->vhost_ops->vhost_backend_cleanup(hdev);
     }
diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h
index a346f23d13..b1d7287099 100644
--- a/include/hw/virtio/vhost.h
+++ b/include/hw/virtio/vhost.h
@@ -73,6 +73,8 @@ struct vhost_dev {
     MemoryListener memory_listener;
     MemoryListener iommu_listener;
     struct vhost_memory *mem;
+    struct vhost_memory *old_mem;
+    int regions_size;
     int n_mem_sections;
     MemoryRegionSection *mem_sections;
     int n_tmp_sections;
-- 
2.37.2
Re: [PATCH] vhost: reduce the set_mem_table call frenquency
Posted by Li Feng 1 year, 8 months ago
Sorry, looks like I use the old qemu code, master has fix this issue.
Just ignore this patch.

> 2022年8月23日 下午1:38,Li Feng <fengli@smartx.com> 写道:
> 
> If the vhost memory layout doesn't change, don't need to call the vhost
> backend.
> The set_mem_table is time consuming when sending to vhost-user backend.
> 
> On aarch64, the edk2 uefi firmware will write the pflash which will
> trigger the vhost_commit hundreds of times.
> 
> Signed-off-by: Li Feng <fengli@smartx.com>
> ---
> hw/virtio/vhost.c         | 14 ++++++++++++++
> include/hw/virtio/vhost.h |  2 ++
> 2 files changed, 16 insertions(+)
> 
> diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
> index f758f177bb..848d2f20d6 100644
> --- a/hw/virtio/vhost.c
> +++ b/hw/virtio/vhost.c
> @@ -523,6 +523,11 @@ static void vhost_commit(MemoryListener *listener)
>     /* Rebuild the regions list from the new sections list */
>     regions_size = offsetof(struct vhost_memory, regions) +
>                        dev->n_mem_sections * sizeof dev->mem->regions[0];
> +    if (dev->mem && dev->started) {
> +        g_free(dev->old_mem);
> +        dev->old_mem = dev->mem;
> +        dev->mem = NULL;
> +    }
>     dev->mem = g_realloc(dev->mem, regions_size);
>     dev->mem->nregions = dev->n_mem_sections;
>     used_memslots = dev->mem->nregions;
> @@ -542,6 +547,12 @@ static void vhost_commit(MemoryListener *listener)
>         goto out;
>     }
> 
> +    if (dev->old_mem && dev->regions_size == regions_size &&
> +            memcmp(dev->mem, dev->old_mem, dev->regions_size) == 0) {
> +        goto out;
> +    }
> +
> +    dev->regions_size = regions_size;
>     for (i = 0; i < dev->mem->nregions; i++) {
>         if (vhost_verify_ring_mappings(dev,
>                        (void *)(uintptr_t)dev->mem->regions[i].userspace_addr,
> @@ -1445,6 +1456,8 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
>     hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
>     hdev->n_mem_sections = 0;
>     hdev->mem_sections = NULL;
> +    hdev->old_mem = NULL;
> +    hdev->regions_size = 0;
>     hdev->log = NULL;
>     hdev->log_size = 0;
>     hdev->log_enabled = false;
> @@ -1491,6 +1504,7 @@ void vhost_dev_cleanup(struct vhost_dev *hdev)
>     }
>     g_free(hdev->mem);
>     g_free(hdev->mem_sections);
> +    g_free(hdev->old_mem);
>     if (hdev->vhost_ops) {
>         hdev->vhost_ops->vhost_backend_cleanup(hdev);
>     }
> diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h
> index a346f23d13..b1d7287099 100644
> --- a/include/hw/virtio/vhost.h
> +++ b/include/hw/virtio/vhost.h
> @@ -73,6 +73,8 @@ struct vhost_dev {
>     MemoryListener memory_listener;
>     MemoryListener iommu_listener;
>     struct vhost_memory *mem;
> +    struct vhost_memory *old_mem;
> +    int regions_size;
>     int n_mem_sections;
>     MemoryRegionSection *mem_sections;
>     int n_tmp_sections;
> -- 
> 2.37.2
>