Skip to contentSkip to conversation

vhost: logs sharing

PATCH Superseded Jason Wang 2 participants 8 messages Apr 10, 2015 – Apr 30, 2015

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH v2 1/1 vhost: logs sharing

Currently we allocate one vhost log per vhost device. This is sub optimal when:

- Guest has several device with vhost as backend
- Guest has multiqueue devices

In the above cases, we can avoid the memory allocation by sharing a single vhost log among all the vhost devices. This is done through:

- Introducing a new vhost_log structure with refcnt inside.
- Using a global pointer to vhost_log structure that will be used. And introduce helper to get the log with expected log size and helper to
- drop the refcnt to the old log.
- Each vhost device still keep track of a pointer to the log that was used.

With above, if no resize happens, all vhost device will share a single vhost log. During resize, a new vhost_log structure will be allocated and made for the global pointer. And each vhost devices will drop the refcnt to the old log.

Tested by doing scp during migration for a 2 queues virtio-net-pci.

Cc Michael S. Tsirkin <[email protected]>

---
Changes from V1:
- Drop the list of vhost log, instead, using a global pointer instead

---
 hw/virtio/vhost.c         | 66 ++++++++++++++++++++++++++++++++++++++---------
 include/hw/virtio/vhost.h |  9 ++++++-
 2 files changed, 62 insertions(+), 13 deletions(-)
hw/virtio/vhost.c +54 -12
index 5a12861..e16c2db 100644
@@ -22,15 +22,19 @@
 #include "hw/virtio/virtio-bus.h"
 #include "migration/migration.h"
 
+static struct vhost_log *vhost_log;
+
 static void vhost_dev_sync_region(struct vhost_dev *dev,
                                   MemoryRegionSection *section,
                                   uint64_t mfirst, uint64_t mlast,
                                   uint64_t rfirst, uint64_t rlast)
 {
+    vhost_log_chunk_t *log = dev->log->log;
+
     uint64_t start = MAX(mfirst, rfirst);
     uint64_t end = MIN(mlast, rlast);
-    vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
-    vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
+    vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
+    vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
     uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
 
     if (end < start) {
@@ -280,22 +284,55 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
     }
     return log_size;
 }
+static struct vhost_log *vhost_log_alloc(uint64_t size)
+{
+    struct vhost_log *log = g_malloc0(sizeof *log + size * sizeof(*(log->log)));
+
+    log->size = size;
+    log->refcnt = 1;
+
+    return log;
+}
+
+static struct vhost_log *vhost_log_get(uint64_t size)
+{
+    if (!vhost_log || vhost_log->size != size) {
+        vhost_log = vhost_log_alloc(size);
+    } else {
+        ++vhost_log->refcnt;
+    }
+
+    return vhost_log;
+}
+
+static void vhost_log_put(struct vhost_log *log)
+{
+    if (!log) {
+        return;
+    }
+
+    --log->refcnt;
+    if (log->refcnt == 0) {
+        if (vhost_log == log) {
+            vhost_log = NULL;
+        }
+        g_free(log);
+    }
+}
 
 static inline void vhost_dev_log_resize(struct vhost_dev* dev, uint64_t size)
 {
-    vhost_log_chunk_t *log;
-    uint64_t log_base;
+    struct vhost_log *log = vhost_log_get(size);
+    uint64_t log_base = (uint64_t)(unsigned long)log->log;
     int r;
 
-    log = g_malloc0(size * sizeof *log);
-    log_base = (uint64_t)(unsigned long)log;
     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_LOG_BASE, &log_base);
     assert(r >= 0);
     /* Sync only the range covered by the old log */
     if (dev->log_size) {
         vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
     }
-    g_free(dev->log);
+    vhost_log_put(dev->log);
     dev->log = log;
     dev->log_size = size;
 }
@@ -601,7 +638,7 @@ static int vhost_migration_log(MemoryListener *listener, int enable)
         if (r < 0) {
             return r;
         }
-        g_free(dev->log);
+        vhost_log_put(dev->log);
         dev->log = NULL;
         dev->log_size = 0;
     } else {
@@ -1058,9 +1095,11 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
 
     if (hdev->log_enabled) {
         hdev->log_size = vhost_get_log_size(hdev);
-        hdev->log = hdev->log_size ?
-            g_malloc0(hdev->log_size * sizeof *hdev->log) : NULL;
-        r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE, hdev->log);
+        if (hdev->log_size) {
+            hdev->log = vhost_log_get(hdev->log_size);
+        }
+        r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE,
+                                        hdev->log_size ? hdev->log->log : NULL);
         if (r < 0) {
             r = -errno;
             goto fail_log;
@@ -1069,6 +1108,9 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
 
     return 0;
 fail_log:
+    if (hdev->log_size) {
+        vhost_log_put(hdev->log);
+    }
 fail_vq:
     while (--i >= 0) {
         vhost_virtqueue_stop(hdev,
@@ -1098,7 +1140,7 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
     vhost_log_sync_range(hdev, 0, ~0x0ull);
 
     hdev->started = false;
-    g_free(hdev->log);
+    vhost_log_put(hdev->log);
     hdev->log = NULL;
     hdev->log_size = 0;
 }
include/hw/virtio/vhost.h +8 -1
index d5593d1..3879778 100644
@@ -28,6 +28,13 @@ typedef unsigned long vhost_log_chunk_t;
 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
 #define VHOST_INVALID_FEATURE_BIT   (0xff)
 
+struct vhost_log {
+    QLIST_ENTRY(vhost_log) node;
+    unsigned long long size;
+    int refcnt;
+    vhost_log_chunk_t log[0];
+};
+
 struct vhost_memory;
 struct vhost_dev {
     MemoryListener memory_listener;
@@ -43,7 +50,6 @@ struct vhost_dev {
     unsigned long long backend_features;
     bool started;
     bool log_enabled;
-    vhost_log_chunk_t *log;
     unsigned long long log_size;
     Error *migration_blocker;
     bool force;
@@ -52,6 +58,7 @@ struct vhost_dev {
     hwaddr mem_changed_end_addr;
     const VhostOps *vhost_ops;
     void *opaque;
+    struct vhost_log *log;
 };
 
 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
Signature · 1 line
2.1.0
#1 lore raw
Quoted 21 lines · Jason Wang

Currently we allocate one vhost log per vhost device. This is sub optimal when:

- Guest has several device with vhost as backend
- Guest has multiqueue devices

In the above cases, we can avoid the memory allocation by sharing a single vhost log among all the vhost devices. This is done through:

- Introducing a new vhost_log structure with refcnt inside.
- Using a global pointer to vhost_log structure that will be used. And introduce helper to get the log with expected log size and helper to
- drop the refcnt to the old log.
- Each vhost device still keep track of a pointer to the log that was used.

With above, if no resize happens, all vhost device will share a single vhost log. During resize, a new vhost_log structure will be allocated and made for the global pointer. And each vhost devices will drop the refcnt to the old log.

Quoted 7 lines

Tested by doing scp during migration for a 2 queues virtio-net-pci.

Cc Michael S. Tsirkin <[email protected]>

---
Changes from V1:
- Drop the list of vhost log, instead, using a global pointer instead

I don't think it works like this. If you have a global pointer, you also need a global listener, have that sync all devices.

Quoted 39 lines
---
 hw/virtio/vhost.c         | 66 ++++++++++++++++++++++++++++++++++++++---------
 include/hw/virtio/vhost.h |  9 ++++++-
 2 files changed, 62 insertions(+), 13 deletions(-)
hw/virtio/vhost.c +10 -2
index 5a12861..e16c2db 100644
@@ -22,15 +22,19 @@
 #include "hw/virtio/virtio-bus.h"
 #include "migration/migration.h"
 
+static struct vhost_log *vhost_log;
+
 static void vhost_dev_sync_region(struct vhost_dev *dev,
                                   MemoryRegionSection *section,
                                   uint64_t mfirst, uint64_t mlast,
                                   uint64_t rfirst, uint64_t rlast)
 {
+    vhost_log_chunk_t *log = dev->log->log;
+
     uint64_t start = MAX(mfirst, rfirst);
     uint64_t end = MIN(mlast, rlast);
-    vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
-    vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
+    vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
+    vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
     uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
 
     if (end < start) {
@@ -280,22 +284,55 @@ static uint64_t vhost_get_log_size(struct vhost_dev *dev)
     }
     return log_size;
 }
+static struct vhost_log *vhost_log_alloc(uint64_t size)
+{
+    struct vhost_log *log = g_malloc0(sizeof *log + size * sizeof(*(log->log)));
+

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct vhost_dev *dev) · line 300

Quoted 10 lines
hw/virtio/vhost.c +10 -0
+    log->size = size;
+    log->refcnt = 1;
+
+    return log;
+}
+
+static struct vhost_log *vhost_log_get(uint64_t size)
+{
+    if (!vhost_log || vhost_log->size != size) {
+        vhost_log = vhost_log_alloc(size);

This just leaks the old log if size != size.

Quoted 17 lines
hw/virtio/vhost.c +17 -0
+    } else {
+        ++vhost_log->refcnt;
+    }
+
+    return vhost_log;
+}
+
+static void vhost_log_put(struct vhost_log *log)
+{
+    if (!log) {
+        return;
+    }
+
+    --log->refcnt;
+    if (log->refcnt == 0) {
+        if (vhost_log == log) {
+            vhost_log = NULL;

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct vhost_dev *dev) · line 325

Quoted 10 lines
hw/virtio/vhost.c +5 -2
+        }
+        g_free(log);
+    }
+}
 
 static inline void vhost_dev_log_resize(struct vhost_dev* dev, uint64_t size)
 {
-    vhost_log_chunk_t *log;
-    uint64_t log_base;
+    struct vhost_log *log = vhost_log_get(size);

At this point next device will try to use the
new log, but there is nothing there.

Quoted 64 lines
hw/virtio/vhost.c +12 -8
+    uint64_t log_base = (uint64_t)(unsigned long)log->log;
     int r;
 
-    log = g_malloc0(size * sizeof *log);
-    log_base = (uint64_t)(unsigned long)log;
     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_LOG_BASE, &log_base);
     assert(r >= 0);
     /* Sync only the range covered by the old log */
     if (dev->log_size) {
         vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
     }
-    g_free(dev->log);
+    vhost_log_put(dev->log);
     dev->log = log;
     dev->log_size = size;
 }
@@ -601,7 +638,7 @@ static int vhost_migration_log(MemoryListener *listener, int enable)
         if (r < 0) {
             return r;
         }
-        g_free(dev->log);
+        vhost_log_put(dev->log);
         dev->log = NULL;
         dev->log_size = 0;
     } else {
@@ -1058,9 +1095,11 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
 
     if (hdev->log_enabled) {
         hdev->log_size = vhost_get_log_size(hdev);
-        hdev->log = hdev->log_size ?
-            g_malloc0(hdev->log_size * sizeof *hdev->log) : NULL;
-        r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE, hdev->log);
+        if (hdev->log_size) {
+            hdev->log = vhost_log_get(hdev->log_size);
+        }
+        r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE,
+                                        hdev->log_size ? hdev->log->log : NULL);
         if (r < 0) {
             r = -errno;
             goto fail_log;
@@ -1069,6 +1108,9 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
 
     return 0;
 fail_log:
+    if (hdev->log_size) {
+        vhost_log_put(hdev->log);
+    }
 fail_vq:
     while (--i >= 0) {
         vhost_virtqueue_stop(hdev,
@@ -1098,7 +1140,7 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
     vhost_log_sync_range(hdev, 0, ~0x0ull);
 
     hdev->started = false;
-    g_free(hdev->log);
+    vhost_log_put(hdev->log);
     hdev->log = NULL;
     hdev->log_size = 0;
 }
include/hw/virtio/vhost.h +0 -0
index d5593d1..3879778 100644

include/hw/virtio/vhost.h typedef unsigned long vhost_log_chunk_t; · line 37

Quoted 10 lines
include/hw/virtio/vhost.h +7 -0
@@ -28,6 +28,13 @@ typedef unsigned long vhost_log_chunk_t;
 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
 #define VHOST_INVALID_FEATURE_BIT   (0xff)
 
+struct vhost_log {
+    QLIST_ENTRY(vhost_log) node;
+    unsigned long long size;
+    int refcnt;
+    vhost_log_chunk_t log[0];
+};
+

Pls fix coding style here: typedef ... VhostLog.

Quoted 21 lines
include/hw/virtio/vhost.h +1 -1
 struct vhost_memory;
 struct vhost_dev {
     MemoryListener memory_listener;
@@ -43,7 +50,6 @@ struct vhost_dev {
     unsigned long long backend_features;
     bool started;
     bool log_enabled;
-    vhost_log_chunk_t *log;
     unsigned long long log_size;
     Error *migration_blocker;
     bool force;
@@ -52,6 +58,7 @@ struct vhost_dev {
     hwaddr mem_changed_end_addr;
     const VhostOps *vhost_ops;
     void *opaque;
+    struct vhost_log *log;
 };
 
 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
Signature · 1 line
2.1.0
author
#2 lore raw
Quoted 37 lines · Michael S. Tsirkin
Quoted 27 lines · Jason Wang

Currently we allocate one vhost log per vhost device. This is sub optimal when:

- Guest has several device with vhost as backend
- Guest has multiqueue devices

In the above cases, we can avoid the memory allocation by sharing a single vhost log among all the vhost devices. This is done through:

- Introducing a new vhost_log structure with refcnt inside.
- Using a global pointer to vhost_log structure that will be used.
And
introduce helper to get the log with expected log size and helper
to
- drop the refcnt to the old log.
- Each vhost device still keep track of a pointer to the log that
was
used.

With above, if no resize happens, all vhost device will share a
single
vhost log. During resize, a new vhost_log structure will be
allocated
and made for the global pointer. And each vhost devices will drop
the
refcnt to the old log.

Quoted 8 lines

Tested by doing scp during migration for a 2 queues virtio-net-pci.

Cc Michael S. Tsirkin <[email protected]>

---
Changes from V1:
- Drop the list of vhost log, instead, using a global pointer
instead

Quoted 2 lines

I don't think it works like this. If you have a global pointer, you also need a global listener, have that sync all devices.

It doesn't conflict, see my comments below.

Quoted 56 lines
Quoted 39 lines

---

  hw/virtio/vhost.c         | 66 
++++++++++++++++++++++++++++++++++++++---------
  include/hw/virtio/vhost.h |  9 ++++++-
  2 files changed, 62 insertions(+), 13 deletions(-)

diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 5a12861..e16c2db 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -22,15 +22,19 @@
#include "hw/virtio/virtio-bus.h" #include "migration/migration.h"

+static struct vhost_log *vhost_log;
+
static void vhost_dev_sync_region(struct vhost_dev *dev,
MemoryRegionSection *section, uint64_t mfirst, uint64_t mlast, uint64_t rfirst, uint64_t rlast)
{
+ vhost_log_chunk_t *log = dev->log->log;
+

      uint64_t start = MAX(mfirst, rfirst);
      uint64_t end = MIN(mlast, rlast);
 -    vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
 -    vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
 +    vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
 +    vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
      uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
  
      if (end < start) {
 @@ -280,22 +284,55 @@ static uint64_t vhost_get_log_size(struct 
vhost_dev *dev)
      }
      return log_size;
Quoted 16 lines

}
+static struct vhost_log *vhost_log_alloc(uint64_t size) +{
+ struct vhost_log *log = g_malloc0(sizeof *log + size *
sizeof(*(log->log)));
+
+ log->size = size;
+ log->refcnt = 1;
+
+ return log;
+}
+
+static struct vhost_log *vhost_log_get(uint64_t size) +{
+ if (!vhost_log || vhost_log->size != size) {
+ vhost_log = vhost_log_alloc(size);

Quoted 1 line

This just leaks the old log if size != size.

But old log is reference counted and will be freed during vhost_log_put() if refcnt drops to zero.

Quoted 30 lines
Quoted 23 lines

+ } else {
+ ++vhost_log->refcnt;
+ }
+
+ return vhost_log;
+}
+
+static void vhost_log_put(struct vhost_log *log) +{
+ if (!log) {
+ return;
+ }
+
+ --log->refcnt;
+ if (log->refcnt == 0) {
+ if (vhost_log == log) {
+ vhost_log = NULL;
+ }
+ g_free(log);
+ }
+}

Quoted 6 lines

static inline void vhost_dev_log_resize(struct vhost_dev* dev,
uint64_t size)
{

 -    vhost_log_chunk_t *log;
 -    uint64_t log_base;
 +    struct vhost_log *log = vhost_log_get(size);
Quoted 2 lines

At this point next device will try to use the
new log, but there is nothing there.

vhost_log_get() will either allocate and return a new log if size is change or just increase the refcnt and use current log. So it works in fact?

Quoted 85 lines
Quoted 77 lines

 +    uint64_t log_base = (uint64_t)(unsigned long)log->log;
      int r;

- log = g_malloc0(size * sizeof *log);

 -    log_base = (uint64_t)(unsigned long)log;
      r = dev->vhost_ops->vhost_call(dev, VHOST_SET_LOG_BASE, 
&log_base);
      assert(r >= 0);
      /* Sync only the range covered by the old log */
      if (dev->log_size) {
          vhost_log_sync_range(dev, 0, dev->log_size * 
VHOST_LOG_CHUNK - 1);
      }
 -    g_free(dev->log);
 +    vhost_log_put(dev->log);
      dev->log = log;
      dev->log_size = size;
  }
 @@ -601,7 +638,7 @@ static int vhost_migration_log(MemoryListener 
*listener, int enable)
          if (r < 0) {
              return r;
          }
 -        g_free(dev->log);
 +        vhost_log_put(dev->log);
          dev->log = NULL;
          dev->log_size = 0;
      } else {

@@ -1058,9 +1095,11 @@ int vhost_dev_start(struct vhost_dev *hdev,
VirtIODevice *vdev)

      if (hdev->log_enabled) {
          hdev->log_size = vhost_get_log_size(hdev);

- hdev->log = hdev->log_size ?
- g_malloc0(hdev->log_size * sizeof *hdev->log) : NULL;
- r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE,
hdev->log);
+ if (hdev->log_size) {
+ hdev->log = vhost_log_get(hdev->log_size);
+ }
+ r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE,
+ hdev->log_size ?

hdev->log->log : NULL);
          if (r < 0) {
              r = -errno;
              goto fail_log;

@@ -1069,6 +1108,9 @@ int vhost_dev_start(struct vhost_dev *hdev,
VirtIODevice *vdev)

      return 0;
  fail_log:
 +    if (hdev->log_size) {
 +        vhost_log_put(hdev->log);
 +    }
  fail_vq:
      while (--i >= 0) {
          vhost_virtqueue_stop(hdev,
 @@ -1098,7 +1140,7 @@ void vhost_dev_stop(struct vhost_dev *hdev, 
VirtIODevice *vdev)
      vhost_log_sync_range(hdev, 0, ~0x0ull);
  
      hdev->started = false;
 -    g_free(hdev->log);
 +    vhost_log_put(hdev->log);
      hdev->log = NULL;
      hdev->log_size = 0;

}
diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index d5593d1..3879778 100644
--- a/include/hw/virtio/vhost.h
+++ b/include/hw/virtio/vhost.h
@@ -28,6 +28,13 @@ typedef unsigned long vhost_log_chunk_t;
#define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
#define VHOST_INVALID_FEATURE_BIT (0xff)

Quoted 7 lines

+struct vhost_log {

 +    QLIST_ENTRY(vhost_log) node;
 +    unsigned long long size;
 +    int refcnt;
 +    vhost_log_chunk_t log[0];

+};
+

Quoted 1 line

Pls fix coding style here: typedef ... VhostLog.

Is this a new style requirement? (I'm asking since e.g the blow vhost_dev structure does not have a typedef)

Quoted 23 lines
Quoted 23 lines

struct vhost_memory;

  struct vhost_dev {
      MemoryListener memory_listener;
 @@ -43,7 +50,6 @@ struct vhost_dev {
      unsigned long long backend_features;
      bool started;
      bool log_enabled;
 -    vhost_log_chunk_t *log;
      unsigned long long log_size;
      Error *migration_blocker;
      bool force;
 @@ -52,6 +58,7 @@ struct vhost_dev {
      hwaddr mem_changed_end_addr;
      const VhostOps *vhost_ops;
      void *opaque;

+ struct vhost_log *log;
};

int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
--
2.1.0

#3 lore raw
Quoted 87 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 18 lines · Jason Wang

Currently we allocate one vhost log per vhost device. This is sub optimal when:
- Guest has several device with vhost as backend
- Guest has multiqueue devices
In the above cases, we can avoid the memory allocation by sharing a single vhost log among all the vhost devices. This is done through:
- Introducing a new vhost_log structure with refcnt inside.
- Using a global pointer to vhost_log structure that will be used. And introduce helper to get the log with expected log size and helper to
- drop the refcnt to the old log.
- Each vhost device still keep track of a pointer to the log that was used.
With above, if no resize happens, all vhost device will share a
single
vhost log. During resize, a new vhost_log structure will be allocated and made for the global pointer. And each vhost devices will drop the refcnt to the old log.
Tested by doing scp during migration for a 2 queues virtio-net-pci.

Quoted 5 lines
Cc Michael S. Tsirkin <[email protected]>

---
Changes from V1:
- Drop the list of vhost log, instead, using a global pointer instead

Quoted 2 lines

I don't think it works like this. If you have a global pointer, you also need a global listener, have that sync all devices.

It doesn't conflict, see my comments below.

Quoted 46 lines
Quoted 46 lines


---
 hw/virtio/vhost.c         | 66
++++++++++++++++++++++++++++++++++++++---------
 include/hw/virtio/vhost.h |  9 ++++++-
 2 files changed, 62 insertions(+), 13 deletions(-)
hw/virtio/vhost.c +14 -2
index 5a12861..e16c2db 100644
@@ -22,15 +22,19 @@
 #include "hw/virtio/virtio-bus.h"
 #include "migration/migration.h"
+static struct vhost_log *vhost_log;
+
 static void vhost_dev_sync_region(struct vhost_dev *dev,
                                   MemoryRegionSection *section,
                                   uint64_t mfirst, uint64_t mlast,
                                   uint64_t rfirst, uint64_t rlast)
 {
+    vhost_log_chunk_t *log = dev->log->log;
+
     uint64_t start = MAX(mfirst, rfirst);
     uint64_t end = MIN(mlast, rlast);
-    vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
-    vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
+    vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
+    vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
     uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
     if (end < start) {
@@ -280,22 +284,55 @@ static uint64_t vhost_get_log_size(struct
 vhost_dev *dev)
     }
     return log_size;
 }
+static struct vhost_log *vhost_log_alloc(uint64_t size)
+{
+    struct vhost_log *log = g_malloc0(sizeof *log + size *
 sizeof(*(log->log)));
+
+    log->size = size;
+    log->refcnt = 1;
+
+    return log;
Quoted 8 lines

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct vhost_dev *dev) · line 300

Quoted 6 lines
hw/virtio/vhost.c +6 -0
+}
+
+static struct vhost_log *vhost_log_get(uint64_t size)
+{
+    if (!vhost_log || vhost_log->size != size) {
+        vhost_log = vhost_log_alloc(size);

This just leaks the old log if size != size.

Quoted 2 lines

But old log is reference counted and will be freed during vhost_log_put() if refcnt drops to zero.

You need a pointer to reference-count it.
You return pointer to new object, no one references the old one.

Quoted 32 lines
Quoted 23 lines
Quoted 23 lines
hw/virtio/vhost.c +21 -0
+    } else {
+        ++vhost_log->refcnt;
+    }
+
+    return vhost_log;
+}
+
+static void vhost_log_put(struct vhost_log *log)
+{
+    if (!log) {
+        return;
+    }
+
+    --log->refcnt;
+    if (log->refcnt == 0) {
+        if (vhost_log == log) {
+            vhost_log = NULL;
+        }
+        g_free(log);
+    }
+}
    static inline void vhost_dev_log_resize(struct vhost_dev* dev,
Quoted 8 lines

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct vhost_dev *dev) · line 325

Quoted 5 lines
hw/virtio/vhost.c +1 -2
 uint64_t size)
 {
-    vhost_log_chunk_t *log;
-    uint64_t log_base;
+    struct vhost_log *log = vhost_log_get(size);

At this point next device will try to use the
new log, but there is nothing there.

Quoted 2 lines

vhost_log_get() will either allocate and return a new log if size is change or just increase the refcnt and use current log. So it works in fact?

But old log is lost and is not synced.

Quoted 82 lines
Quoted 73 lines
Quoted 73 lines
hw/virtio/vhost.c +2 -3
+    uint64_t log_base = (uint64_t)(unsigned long)log->log;
     int r;
-    log = g_malloc0(size * sizeof *log);
-    log_base = (uint64_t)(unsigned long)log;
     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_LOG_BASE,
 &log_base);
     assert(r >= 0);
     /* Sync only the range covered by the old log */
     if (dev->log_size) {
         vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK -
 1);
     }
-    g_free(dev->log);
+    vhost_log_put(dev->log);
     dev->log = log;
     dev->log_size = size;

}

hw/virtio/vhost.c +1 -1
@@ -601,7 +638,7 @@ static int vhost_migration_log(MemoryListener
 *listener, int enable)
         if (r < 0) {
             return r;
         }
-        g_free(dev->log);
+        vhost_log_put(dev->log);
         dev->log = NULL;
         dev->log_size = 0;
     } else {
hw/virtio/vhost.c +5 -3
@@ -1058,9 +1095,11 @@ int vhost_dev_start(struct vhost_dev *hdev,
 VirtIODevice *vdev)
     if (hdev->log_enabled) {
         hdev->log_size = vhost_get_log_size(hdev);
-        hdev->log = hdev->log_size ?
-            g_malloc0(hdev->log_size * sizeof *hdev->log) : NULL;
-        r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE,
 hdev->log);
+        if (hdev->log_size) {
+            hdev->log = vhost_log_get(hdev->log_size);
+        }
+        r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE,
+                                        hdev->log_size ?
 hdev->log->log : NULL);
         if (r < 0) {
             r = -errno;
             goto fail_log;
hw/virtio/vhost.c +4 -1
@@ -1069,6 +1108,9 @@ int vhost_dev_start(struct vhost_dev *hdev,
 VirtIODevice *vdev)
     return 0;
 fail_log:
+    if (hdev->log_size) {
+        vhost_log_put(hdev->log);
+    }
 fail_vq:
     while (--i >= 0) {
         vhost_virtqueue_stop(hdev,
@@ -1098,7 +1140,7 @@ void vhost_dev_stop(struct vhost_dev *hdev,
 VirtIODevice *vdev)
     vhost_log_sync_range(hdev, 0, ~0x0ull);
     hdev->started = false;
-    g_free(hdev->log);
+    vhost_log_put(hdev->log);
     hdev->log = NULL;
     hdev->log_size = 0;
 }
include/hw/virtio/vhost.h +1 -0
index d5593d1..3879778 100644
@@ -28,6 +28,13 @@ typedef unsigned long vhost_log_chunk_t;
 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
 #define VHOST_INVALID_FEATURE_BIT   (0xff)
+struct vhost_log {
Quoted 8 lines

include/hw/virtio/vhost.h typedef unsigned long vhost_log_chunk_t; · line 37

Quoted 6 lines
include/hw/virtio/vhost.h +6 -0
+    QLIST_ENTRY(vhost_log) node;
+    unsigned long long size;
+    int refcnt;
+    vhost_log_chunk_t log[0];
+};
+

Pls fix coding style here: typedef ... VhostLog.

Quoted 2 lines

Is this a new style requirement? (I'm asking since e.g the blow vhost_dev structure does not have a typedef)

You are right here, you can keep it as is for now.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
include/hw/virtio/vhost.h +1 -2
 struct vhost_memory;
 struct vhost_dev {
     MemoryListener memory_listener;
@@ -43,7 +50,6 @@ struct vhost_dev {
     unsigned long long backend_features;
     bool started;
     bool log_enabled;
-    vhost_log_chunk_t *log;
     unsigned long long log_size;
     Error *migration_blocker;
     bool force;
@@ -52,6 +58,7 @@ struct vhost_dev {
     hwaddr mem_changed_end_addr;
     const VhostOps *vhost_ops;
     void *opaque;
+    struct vhost_log *log;
 };
 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
--  2.1.0
author
#4 lore raw
Quoted 100 lines · Michael S. Tsirkin
Quoted 99 lines · Jason Wang
Quoted 6 lines · Michael S. Tsirkin
Quoted 5 lines · Jason Wang

Currently we allocate one vhost log per vhost device. This is sub optimal when:
- Guest has several device with vhost as backend
- Guest has multiqueue devices
In the above cases, we can avoid the memory allocation by

sharing a

Quoted 1 line
Quoted 1 line

single vhost log among all the vhost devices. This is done

through:

Quoted 2 lines
Quoted 2 lines

- Introducing a new vhost_log structure with refcnt inside.
- Using a global pointer to vhost_log structure that will be

used. And

Quoted 1 line
Quoted 1 line

introduce helper to get the log with expected log size and

helper to

Quoted 2 lines
Quoted 2 lines

- drop the refcnt to the old log.
- Each vhost device still keep track of a pointer to the log

that was

Quoted 4 lines
Quoted 4 lines

used.
With above, if no resize happens, all vhost device will share a
single
vhost log. During resize, a new vhost_log structure will be

allocated

Quoted 1 line
Quoted 1 line

and made for the global pointer. And each vhost devices will

drop the

Quoted 2 lines
Quoted 2 lines

refcnt to the old log.
Tested by doing scp during migration for a 2 queues

virtio-net-pci.

Quoted 5 lines
Quoted 5 lines
Cc Michael S. Tsirkin <[email protected]>

---
Changes from V1:
- Drop the list of vhost log, instead, using a global pointer

instead

Quoted 3 lines

I don't think it works like this. If you have a global pointer, you also need a global listener, have that sync all devices.

It doesn't conflict, see my comments below.

Quoted 12 lines
Quoted 12 lines


---
 hw/virtio/vhost.c         | 66
++++++++++++++++++++++++++++++++++++++---------
 include/hw/virtio/vhost.h |  9 ++++++-
 2 files changed, 62 insertions(+), 13 deletions(-)
hw/virtio/vhost.c +0 -0
index 5a12861..e16c2db 100644
Quoted 8 lines

hw/virtio/vhost.c line 28

Quoted 8 lines
hw/virtio/vhost.c +2 -0
@@ -22,15 +22,19 @@
 #include "hw/virtio/virtio-bus.h"
 #include "migration/migration.h"
+static struct vhost_log *vhost_log;
+
 static void vhost_dev_sync_region(struct vhost_dev *dev,
                                   MemoryRegionSection *section,
                                   uint64_t mfirst, uint64_t 

mlast,

Quoted 1 line

hw/virtio/vhost.c line 29

Quoted 1 line
hw/virtio/vhost.c +0 -0
                                   uint64_t rfirst, uint64_t 

rlast)

Quoted 6 lines

hw/virtio/vhost.c

Quoted 6 lines
hw/virtio/vhost.c +2 -1
 {
+    vhost_log_chunk_t *log = dev->log->log;
+
     uint64_t start = MAX(mfirst, rfirst);
     uint64_t end = MIN(mlast, rlast);
-    vhost_log_chunk_t *from = dev->log + start / 

VHOST_LOG_CHUNK;

Quoted 1 line

hw/virtio/vhost.c line 32

Quoted 1 line
hw/virtio/vhost.c +0 -1
-    vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 

1;

Quoted 25 lines
Quoted 24 lines
hw/virtio/vhost.c +16 -0
+    vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
+    vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
     uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
     if (end < start) {
@@ -280,22 +284,55 @@ static uint64_t vhost_get_log_size(struct
 vhost_dev *dev)
     }
     return log_size;
 }
+static struct vhost_log *vhost_log_alloc(uint64_t size)
+{
+    struct vhost_log *log = g_malloc0(sizeof *log + size *
 sizeof(*(log->log)));
+
+    log->size = size;
+    log->refcnt = 1;
+
+    return log;
+}
+
+static struct vhost_log *vhost_log_get(uint64_t size)
+{
+    if (!vhost_log || vhost_log->size != size) {
+        vhost_log = vhost_log_alloc(size);
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

This just leaks the old log if size != size.

But old log is reference counted and will be freed during
vhost_log_put() if
refcnt drops to zero.

You need a pointer to reference-count it.
You return pointer to new object, no one references the old one.

The pointer is just vhost_log->log. The old pointer will be kept in vhost_dev_log_resize() until 1) new log was set through ioctl and 2) the old log was synced. Then vhost_log_put() will drop the reference count to the old log.

Quoted 34 lines
Quoted 34 lines
Quoted 16 lines
Quoted 16 lines
hw/virtio/vhost.c +14 -0
+    } else {
+        ++vhost_log->refcnt;
+    }
+
+    return vhost_log;
+}
+
+static void vhost_log_put(struct vhost_log *log)
+{
+    if (!log) {
+        return;
+    }
+
+    --log->refcnt;
Quoted 8 lines

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct vhost_dev *dev)

Quoted 8 lines
hw/virtio/vhost.c +7 -0
+    if (log->refcnt == 0) {
+        if (vhost_log == log) {
+            vhost_log = NULL;
+        }
+        g_free(log);
+    }
+}
    static inline void vhost_dev_log_resize(struct vhost_dev* 

dev,

Quoted 8 lines

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct vhost_dev *dev) · line 325

Quoted 5 lines
hw/virtio/vhost.c +1 -2
 uint64_t size)
 {
-    vhost_log_chunk_t *log;
-    uint64_t log_base;
+    struct vhost_log *log = vhost_log_get(size);

At this point next device will try to use the
new log, but there is nothing there.

Quoted 6 lines
Quoted 4 lines

vhost_log_get() will either allocate and return a new log if size
is change
or just increase the refcnt and use current log. So it works in
fact?

But old log is lost and is not synced.

As I replied above, it was not lost and kept in vhost_log->log.

#5 lore raw
Quoted 98 lines · Jason Wang
Quoted 95 lines · Michael S. Tsirkin
Quoted 93 lines · Jason Wang
    On Tue, Apr 28, 2015 at 5:37 PM, Michael S. Tsirkin
Quoted 9 lines · <[email protected]> wrote:
Quoted 8 lines · Jason Wang

Currently we allocate one vhost log per vhost device. This is sub optimal when:
- Guest has several device with vhost as backend
- Guest has multiqueue devices
In the above cases, we can avoid the memory allocation by sharing a single vhost log among all the vhost devices. This is done through:
- Introducing a new vhost_log structure with refcnt inside.
- Using a global pointer to vhost_log structure that will be used.

And

Quoted 1 line
Quoted 1 line

introduce helper to get the log with expected log size and helper

to

Quoted 2 lines
Quoted 2 lines

- drop the refcnt to the old log.
- Each vhost device still keep track of a pointer to the log that

was

Quoted 4 lines
Quoted 4 lines

used.
With above, if no resize happens, all vhost device will share a
single
vhost log. During resize, a new vhost_log structure will be

allocated

Quoted 1 line
Quoted 1 line

and made for the global pointer. And each vhost devices will drop

the

Quoted 7 lines
Quoted 7 lines

refcnt to the old log.
Tested by doing scp during migration for a 2 queues virtio-net-pci.

Cc Michael S. Tsirkin <[email protected]>

---
Changes from V1:
- Drop the list of vhost log, instead, using a global pointer

instead

Quoted 3 lines

I don't think it works like this. If you have a global pointer, you also need a global listener, have that sync all devices.

It doesn't conflict, see my comments below.

Quoted 46 lines
Quoted 46 lines


---
 hw/virtio/vhost.c         | 66
++++++++++++++++++++++++++++++++++++++---------
 include/hw/virtio/vhost.h |  9 ++++++-
 2 files changed, 62 insertions(+), 13 deletions(-)
hw/virtio/vhost.c +14 -2
index 5a12861..e16c2db 100644
@@ -22,15 +22,19 @@
 #include "hw/virtio/virtio-bus.h"
 #include "migration/migration.h"
+static struct vhost_log *vhost_log;
+
 static void vhost_dev_sync_region(struct vhost_dev *dev,
                                   MemoryRegionSection *section,
                                   uint64_t mfirst, uint64_t mlast,
                                   uint64_t rfirst, uint64_t rlast)
 {
+    vhost_log_chunk_t *log = dev->log->log;
+
     uint64_t start = MAX(mfirst, rfirst);
     uint64_t end = MIN(mlast, rlast);
-    vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
-    vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
+    vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
+    vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
     uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
     if (end < start) {
@@ -280,22 +284,55 @@ static uint64_t vhost_get_log_size(struct
 vhost_dev *dev)
     }
     return log_size;
 }
+static struct vhost_log *vhost_log_alloc(uint64_t size)
+{
+    struct vhost_log *log = g_malloc0(sizeof *log + size *
 sizeof(*(log->log)));
+
+    log->size = size;
+    log->refcnt = 1;
+
+    return log;
Quoted 8 lines

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct vhost_dev *dev) · line 300

Quoted 6 lines
hw/virtio/vhost.c +6 -0
+}
+
+static struct vhost_log *vhost_log_get(uint64_t size)
+{
+    if (!vhost_log || vhost_log->size != size) {
+        vhost_log = vhost_log_alloc(size);

This just leaks the old log if size != size.

But old log is reference counted and will be freed during
vhost_log_put() if
refcnt drops to zero.

Quoted 6 lines
Quoted 2 lines

You need a pointer to reference-count it.
You return pointer to new object, no one references the old one.

The pointer is just vhost_log->log. The old pointer will be kept in vhost_dev_log_resize() until 1) new log was set through ioctl and 2) the old log was synced.

vhost_dev_log_resize is per device, isn't it? So the log is synced in device 1, but not in device 2.

Quoted 42 lines

Then vhost_log_put() will drop the reference count to the old log.

Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 19 lines
hw/virtio/vhost.c +17 -0
+    } else {
+        ++vhost_log->refcnt;
+    }
+
+    return vhost_log;
+}
+
+static void vhost_log_put(struct vhost_log *log)
+{
+    if (!log) {
+        return;
+    }
+
+    --log->refcnt;
+    if (log->refcnt == 0) {
+        if (vhost_log == log) {
+            vhost_log = NULL;

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct vhost_dev *dev) · line 325

Quoted 10 lines
hw/virtio/vhost.c +5 -2
+        }
+        g_free(log);
+    }
+}
    static inline void vhost_dev_log_resize(struct vhost_dev* dev,
 uint64_t size)
 {
-    vhost_log_chunk_t *log;
-    uint64_t log_base;
+    struct vhost_log *log = vhost_log_get(size);

At this point next device will try to use the
new log, but there is nothing there.

Quoted 5 lines
Quoted 3 lines

vhost_log_get() will either allocate and return a new log if size is
change
or just increase the refcnt and use current log. So it works in fact?

But old log is lost and is not synced.

As I replied above, it was not lost and kept in vhost_log->log.

author
#6 lore raw
Quoted 119 lines · Michael S. Tsirkin
Quoted 118 lines · Jason Wang
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Jason Wang
    On Tue, Apr 28, 2015 at 5:37 PM, Michael S. Tsirkin
Quoted 2 lines · <[email protected]> wrote:
Quoted 1 line · Jason Wang

Currently we allocate one vhost log per vhost device. This is

sub

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

optimal when:
- Guest has several device with vhost as backend
- Guest has multiqueue devices
In the above cases, we can avoid the memory allocation by

sharing a

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

single vhost log among all the vhost devices. This is done

through:

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

- Introducing a new vhost_log structure with refcnt inside.
- Using a global pointer to vhost_log structure that will be

used.

Quoted 2 lines
Quoted 2 lines

And

Quoted 1 line
Quoted 1 line

introduce helper to get the log with expected log size and

helper

Quoted 3 lines
Quoted 3 lines

to

Quoted 2 lines
Quoted 2 lines

- drop the refcnt to the old log.
- Each vhost device still keep track of a pointer to the log

that

Quoted 3 lines
Quoted 3 lines

was

Quoted 2 lines
Quoted 2 lines

used.
With above, if no resize happens, all vhost device will

share a

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

single
vhost log. During resize, a new vhost_log structure will be

allocated

Quoted 1 line
Quoted 1 line

and made for the global pointer. And each vhost devices will

drop

Quoted 3 lines
Quoted 3 lines

the

Quoted 2 lines
Quoted 2 lines

refcnt to the old log.
Tested by doing scp during migration for a 2 queues

virtio-net-pci.

Quoted 13 lines
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Cc Michael S. Tsirkin <[email protected]>

---
Changes from V1:
- Drop the list of vhost log, instead, using a global pointer

instead

Quoted 3 lines

I don't think it works like this. If you have a global pointer, you also need a global listener, have that sync all devices.

It doesn't conflict, see my comments below.

Quoted 3 lines
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
---
 hw/virtio/vhost.c         | 66
++++++++++++++++++++++++++++++++++++++---------
 include/hw/virtio/vhost.h |  9 ++++++-
 2 files changed, 62 insertions(+), 13 deletions(-)
hw/virtio/vhost.c +0 -1
index 5a12861..e16c2db 100644
--- a/hw/virtio/vhost.c
Quoted 8 lines
Quoted 8 lines

hw/virtio/vhost.c line 27

Quoted 8 lines

+++ b/hw/virtio/vhost.c

hw/virtio/vhost.c +2 -0
@@ -22,15 +22,19 @@
 #include "hw/virtio/virtio-bus.h"
 #include "migration/migration.h"
+static struct vhost_log *vhost_log;
+
 static void vhost_dev_sync_region(struct vhost_dev *dev,
                                   MemoryRegionSection 

*section,

Quoted 1 line
Quoted 1 line
Quoted 1 line

hw/virtio/vhost.c line 28

Quoted 1 line
hw/virtio/vhost.c +0 -0
                                   uint64_t mfirst, uint64_t 

mlast,

Quoted 1 line
Quoted 1 line
Quoted 1 line

hw/virtio/vhost.c line 29

Quoted 1 line
hw/virtio/vhost.c +0 -0
                                   uint64_t rfirst, uint64_t 

rlast)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

hw/virtio/vhost.c

Quoted 6 lines
hw/virtio/vhost.c +2 -1
 {
+    vhost_log_chunk_t *log = dev->log->log;
+
     uint64_t start = MAX(mfirst, rfirst);
     uint64_t end = MIN(mlast, rlast);
-    vhost_log_chunk_t *from = dev->log + start / 

VHOST_LOG_CHUNK;

Quoted 1 line
Quoted 1 line
Quoted 1 line

hw/virtio/vhost.c line 32

Quoted 1 line
hw/virtio/vhost.c +0 -1
-    vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK 

+ 1;

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

hw/virtio/vhost.c

Quoted 3 lines
hw/virtio/vhost.c +2 -0
+    vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
+    vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
     uint64_t addr = (start / VHOST_LOG_CHUNK) * 

VHOST_LOG_CHUNK;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

hw/virtio/vhost.c static uint64_t

Quoted 2 lines
hw/virtio/vhost.c +0 -0
     if (end < start) {
@@ -280,22 +284,55 @@ static uint64_t 

vhost_get_log_size(struct

Quoted 27 lines
Quoted 21 lines
Quoted 13 lines
Quoted 13 lines
hw/virtio/vhost.c +8 -0
 vhost_dev *dev)
     }
     return log_size;
 }
+static struct vhost_log *vhost_log_alloc(uint64_t size)
+{
+    struct vhost_log *log = g_malloc0(sizeof *log + size *
 sizeof(*(log->log)));
+
+    log->size = size;
+    log->refcnt = 1;
+
+    return log;
Quoted 8 lines

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct vhost_dev *dev) · line 300

Quoted 6 lines
hw/virtio/vhost.c +6 -0
+}
+
+static struct vhost_log *vhost_log_get(uint64_t size)
+{
+    if (!vhost_log || vhost_log->size != size) {
+        vhost_log = vhost_log_alloc(size);

This just leaks the old log if size != size.

Quoted 3 lines

But old log is reference counted and will be freed during
vhost_log_put() if
refcnt drops to zero.

You need a pointer to reference-count it.
You return pointer to new object, no one references the old one.

Quoted 6 lines
Quoted 4 lines

The pointer is just vhost_log->log. The old pointer will be kept in vhost_dev_log_resize() until 1) new log was set through ioctl and
2) the old
log was synced.

vhost_dev_log_resize is per device, isn't it?

Yes.

Quoted 2 lines

So the log is synced in device 1, but not in device 2.

But we will do resizing one by one for all listeners. So the sync of device 2 will happen soon afterwards.

#7 lore raw
Quoted 119 lines · Jason Wang
Quoted 107 lines · Michael S. Tsirkin
Quoted 106 lines · Jason Wang
    On Tue, Apr 28, 2015 at 6:30 PM, Michael S. Tsirkin
Quoted 9 lines · <[email protected]> wrote:
Quoted 8 lines · Jason Wang
    On Tue, Apr 28, 2015 at 5:37 PM, Michael S. Tsirkin
Quoted 6 lines · <[email protected]> wrote:
Quoted 5 lines · Jason Wang

Currently we allocate one vhost log per vhost device. This is sub optimal when:
- Guest has several device with vhost as backend
- Guest has multiqueue devices
In the above cases, we can avoid the memory allocation by sharing

a

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

single vhost log among all the vhost devices. This is done

through:

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

- Introducing a new vhost_log structure with refcnt inside.
- Using a global pointer to vhost_log structure that will be

used.

Quoted 2 lines
Quoted 2 lines

And

Quoted 1 line
Quoted 1 line

introduce helper to get the log with expected log size and

helper

Quoted 8 lines
Quoted 8 lines

to

Quoted 2 lines
Quoted 2 lines

- drop the refcnt to the old log.
- Each vhost device still keep track of a pointer to the log that

was

Quoted 4 lines
Quoted 4 lines

used.
With above, if no resize happens, all vhost device will share a
single
vhost log. During resize, a new vhost_log structure will be

Quoted 5 lines
Quoted 5 lines

allocated

Quoted 1 line
Quoted 1 line

and made for the global pointer. And each vhost devices will drop

the

Quoted 2 lines
Quoted 2 lines

refcnt to the old log.
Tested by doing scp during migration for a 2 queues

virtio-net-pci.

Quoted 22 lines
Quoted 22 lines
Quoted 5 lines
Quoted 5 lines
Cc Michael S. Tsirkin <[email protected]>

---
Changes from V1:
- Drop the list of vhost log, instead, using a global pointer

instead

Quoted 3 lines

I don't think it works like this. If you have a global pointer, you also need a global listener, have that sync all devices.

It doesn't conflict, see my comments below.

Quoted 12 lines
Quoted 12 lines


---
 hw/virtio/vhost.c         | 66
++++++++++++++++++++++++++++++++++++++---------
 include/hw/virtio/vhost.h |  9 ++++++-
 2 files changed, 62 insertions(+), 13 deletions(-)
hw/virtio/vhost.c +0 -0
index 5a12861..e16c2db 100644
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/virtio/vhost.c line 28

Quoted 8 lines
hw/virtio/vhost.c +2 -0
@@ -22,15 +22,19 @@
 #include "hw/virtio/virtio-bus.h"
 #include "migration/migration.h"
+static struct vhost_log *vhost_log;
+
 static void vhost_dev_sync_region(struct vhost_dev *dev,
                                   MemoryRegionSection *section,
                                   uint64_t mfirst, uint64_t

mlast,

Quoted 1 line
Quoted 1 line
Quoted 1 line

hw/virtio/vhost.c line 29

Quoted 1 line
hw/virtio/vhost.c +0 -0
                                   uint64_t rfirst, uint64_t

rlast)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

hw/virtio/vhost.c line 31

Quoted 6 lines
hw/virtio/vhost.c +2 -1
 {
+    vhost_log_chunk_t *log = dev->log->log;
+
     uint64_t start = MAX(mfirst, rfirst);
     uint64_t end = MIN(mlast, rlast);
-    vhost_log_chunk_t *from = dev->log + start /

VHOST_LOG_CHUNK;

Quoted 1 line
Quoted 1 line
Quoted 1 line

hw/virtio/vhost.c line 32

Quoted 1 line
hw/virtio/vhost.c +0 -1
-    vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK +

1;

Quoted 30 lines
Quoted 29 lines
Quoted 18 lines
Quoted 18 lines
hw/virtio/vhost.c +10 -0
+    vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
+    vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
     uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
     if (end < start) {
@@ -280,22 +284,55 @@ static uint64_t vhost_get_log_size(struct
 vhost_dev *dev)
     }
     return log_size;
 }
+static struct vhost_log *vhost_log_alloc(uint64_t size)
+{
+    struct vhost_log *log = g_malloc0(sizeof *log + size *
 sizeof(*(log->log)));
+
+    log->size = size;
+    log->refcnt = 1;
+
+    return log;
Quoted 8 lines

hw/virtio/vhost.c static uint64_t vhost_get_log_size(struct · line 302

Quoted 6 lines
hw/virtio/vhost.c +6 -0
+}
+
+static struct vhost_log *vhost_log_get(uint64_t size)
+{
+    if (!vhost_log || vhost_log->size != size) {
+        vhost_log = vhost_log_alloc(size);

This just leaks the old log if size != size.

But old log is reference counted and will be freed during
vhost_log_put() if
refcnt drops to zero.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

You need a pointer to reference-count it.
You return pointer to new object, no one references the old one.

The pointer is just vhost_log->log. The old pointer will be kept in vhost_dev_log_resize() until 1) new log was set through ioctl and 2) the old
log was synced.

vhost_dev_log_resize is per device, isn't it?

Quoted 7 lines

Yes.

Quoted 2 lines

So the log is synced in device 1, but not in device 2.

But we will do resizing one by one for all listeners. So the sync of device 2 will happen soon afterwards.

yes but it will get the new log from vhost_log_get, not the old one, and sync the new buffer not the old one.

Signature · 1 line
MSt