Move memory listener register to vhost_vdpa_init
v5 cover Move memory listener register to vhost_vdpa_init
Current memory operations like pinning may take a lot of time at the destination. Currently they are done after the source of the migration is stopped, and before the workload is resumed at the destination. This is a period where neigher traffic can flow, nor the VM workload can continue (downtime).
We can do better as we know the memory layout of the guest RAM at the destination from the moment that all devices are initializaed. So moving that operation allows QEMU to communicate the kernel the maps while the workload is still running in the source, so Linux can start mapping them.
As a small drawback, there is a time in the initialization where QEMU cannot respond to QMP etc. By some testing, this time is about 0.2seconds. This may be further reduced (or increased) depending on the vdpa driver and the platform hardware, and it is dominated by the cost of memory pinning.
This matches the time that we move out of the called downtime window. The guest-visible downtime is measured as the elapsed trace time between the last vhost_vdpa_suspend on the source and the last vhost_vdpa_set_vring_enable_one on the destination. In other words, from "guest CPUs freeze" to the instant the final Rx/Tx queue-pair is able to start moving data.
Using ConnectX-6 Dx (MLX5) NICs in vhost-vDPA mode with 8 queue-pairs, the series reduces guest-visible downtime during back-to-back live migrations by more than half:
- 39G VM: 4.72s -> 2.09s (-2.63s, ~56% improvement)
- 128G VM: 14.72s -> 5.83s (-8.89s, ~60% improvement)
Future directions on top of this series may include to move more things ahead of the migration time, like set DRIVER_OK or perform actual iterative migration of virtio-net devices.
Comments are welcome.
This series is a different approach of series [1]. As the title does not reflect the changes anymore, please refer to the previous one to know the series history.
This series is based on [2] and has already been merged.
[Jonah Palmer]
This series was rebased after [3] was pulled in, as [3] was a prerequisite fix for this series.
v5:
---
* Update performance metrics from the change in v4.
v4:
---
* Add memory listener unregistration to vhost_vdpa_reset_device.
* Remove memory listener unregistration from vhost_vdpa_reset_status.
v3:
---
* Rebase
v2:
---
* Move the memory listener registration to vhost_vdpa_set_owner function.
* Move the iova_tree allocation to net_vhost_vdpa_init.
v1 at https://lists.gnu.org/archive/html/qemu-devel/2024-01/msg02136.html.
[1] https://patchwork.kernel.org/project/qemu-devel/cover/[email protected]/
[2] https://lists.gnu.org/archive/html/qemu-devel/2024-01/msg05910.html
[3] https://lore.kernel.org/qemu-devel/[email protected]/
Eugenio Pérez (7):
vdpa: check for iova tree initialized at net_client_start
vdpa: reorder vhost_vdpa_set_backend_cap
vdpa: set backend capabilities at vhost_vdpa_init
vdpa: add listener_registered
vdpa: reorder listener assignment
vdpa: move iova_tree allocation to net_vhost_vdpa_init
vdpa: move memory listener register to vhost_vdpa_init
hw/virtio/vhost-vdpa.c | 107 +++++++++++++++++++++------------ include/hw/virtio/vhost-vdpa.h | 22 ++++++- net/vhost-vdpa.c | 34 +---------- 3 files changed, 93 insertions(+), 70 deletions(-)
Signature · 1 line
2.43.5
PATCH v5 1/7 vdpa: check for iova tree initialized at net_client_start
From: Eugenio Pérez <[email protected]>
To map the guest memory while it is migrating we need to create the iova_tree, as long as the destination uses x-svq=on. Checking to not override it.
The function vhost_vdpa_net_client_stop clear it if the device is stopped. If the guest starts the device again, the iova tree is recreated by vhost_vdpa_net_data_start_first or vhost_vdpa_net_cvq_start if needed, so old behavior is kept.
--- net/vhost-vdpa.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/vhost-vdpa.c +3 -1
@@ -366,7 +366,9 @@ static void vhost_vdpa_net_data_start_first(VhostVDPAState *s)migration_add_notifier (&s->migration_state, vdpa_net_migration_state_notifier); -if (v->shadow_vqs_enabled) { + +/* iova_tree may be initialized by vhost_vdpa_net_load_setup */ +if (v->shadow_vqs_enabled && !v->shared->iova_tree) { v->shared->iova_tree =vhost_iova_tree_new (v->shared->iova_range.first, v->shared->iova_range.last); }
Signature · 1 line
2.43.5
PATCH v5 2/7 vdpa: reorder vhost_vdpa_set_backend_cap
In reply to Jonah PalmerFrom: Eugenio Pérez <[email protected]>
It will be used directly by vhost_vdpa_init.
--- hw/virtio/vhost-vdpa.c | 60 +++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 30 deletions(-)
hw/virtio/vhost-vdpa.c +30 -30
@@ -594,6 +594,36 @@ static void vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v) v->shadow_vqs =g_steal_pointer (&shadow_vqs); } +static int vhost_vdpa_set_backend_cap (struct vhost_dev *dev) +{ +struct vhost_vdpa *v = dev->opaque; + +uint64_t features; +uint64_t f =0x1ULL <<VHOST_BACKEND_F_IOTLB_MSG_V2 | +0x1ULL <<VHOST_BACKEND_F_IOTLB_BATCH | +0x1ULL <<VHOST_BACKEND_F_IOTLB_ASID | +0x1ULL <<VHOST_BACKEND_F_SUSPEND ; +int r; + +if (vhost_vdpa_call (dev,VHOST_GET_BACKEND_FEATURES , &features)) { +return -EFAULT ; + } + + features &= f; + +if (vhost_vdpa_first_dev (dev)) { + r =vhost_vdpa_call (dev,VHOST_SET_BACKEND_FEATURES , &features); +if (r) { +return -EFAULT ; + } + } + + dev->backend_cap = features; + v->shared->backend_cap = features; + +return 0 ; +} +static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) {struct vhost_vdpa *v = opaque;
@@ -841,36 +871,6 @@ static int vhost_vdpa_set_features(struct vhost_dev *dev,return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_FEATURES_OK ); } -static int vhost_vdpa_set_backend_cap (struct vhost_dev *dev) -{ -struct vhost_vdpa *v = dev->opaque; - -uint64_t features; -uint64_t f =0x1ULL <<VHOST_BACKEND_F_IOTLB_MSG_V2 | -0x1ULL <<VHOST_BACKEND_F_IOTLB_BATCH | -0x1ULL <<VHOST_BACKEND_F_IOTLB_ASID | -0x1ULL <<VHOST_BACKEND_F_SUSPEND ; -int r; - -if (vhost_vdpa_call (dev,VHOST_GET_BACKEND_FEATURES , &features)) { -return -EFAULT ; - } - - features &= f; - -if (vhost_vdpa_first_dev (dev)) { - r =vhost_vdpa_call (dev,VHOST_SET_BACKEND_FEATURES , &features); -if (r) { -return -EFAULT ; - } - } - - dev->backend_cap = features; - v->shared->backend_cap = features; - -return 0 ; -} -static int vhost_vdpa_get_device_id (struct vhost_dev *dev,uint32_t *device_id) {
Signature · 1 line
2.43.5
PATCH v5 3/7 vdpa: set backend capabilities at vhost_vdpa_init
In reply to Jonah PalmerFrom: Eugenio Pérez <[email protected]>
The backend does not reset them until the vdpa file descriptor is closed so there is no harm in doing it only once.
This allows the destination of a live migration to premap memory in batches, using VHOST_BACKEND_F_IOTLB_BATCH.
--- hw/virtio/vhost-vdpa.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/virtio/vhost-vdpa.c +6 -1
@@ -634,6 +634,12 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) v->dev = dev; dev->opaque = opaque ; v->shared->listener = vhost_vdpa_memory_listener; + + ret =vhost_vdpa_set_backend_cap (dev); +if (unlikely (ret !=0 )) { +return ret; + } +vhost_vdpa_init_svq (dev, v);error_propagate (&dev->migration_blocker, v->migration_blocker);
@@ -1563,7 +1569,6 @@ const VhostOps vdpa_ops = { .vhost_set_vring_kick = vhost_vdpa_set_vring_kick, .vhost_set_vring_call = vhost_vdpa_set_vring_call, .vhost_get_features = vhost_vdpa_get_features, - .vhost_set_backend_cap = vhost_vdpa_set_backend_cap, .vhost_set_owner = vhost_vdpa_set_owner, .vhost_set_vring_endian = NULL, .vhost_backend_memslots_limit = vhost_vdpa_memslots_limit,
Signature · 1 line
2.43.5
PATCH v5 4/7 vdpa: add listener_registered
In reply to Jonah PalmerFrom: Eugenio Pérez <[email protected]>
Check if the listener has been registered or not, so it needs to be registered again at start.
--- hw/virtio/vhost-vdpa.c | 7 ++++++- include/hw/virtio/vhost-vdpa.h | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-)
hw/virtio/vhost-vdpa.c +6 -1
@@ -1379,7 +1379,10 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) "IOMMU and try again");return -1 ; } -memory_listener_register (&v->shared->listener, dev->vdev->dma_as); +if (!v->shared->listener_registered) { +memory_listener_register (&v->shared->listener, dev->vdev->dma_as); + v->shared->listener_registered = true; + }return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_DRIVER_OK ); }
@@ -1399,6 +1402,8 @@ static void vhost_vdpa_reset_status(struct vhost_dev *dev)vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE |VIRTIO_CONFIG_S_DRIVER );memory_listener_unregister (&v->shared->listener); + v->shared->listener_registered = false; + } static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base,
include/hw/virtio/vhost-vdpa.h +6 -0
@@ -51,6 +51,12 @@ typedef struct vhost_vdpa_shared {bool iotlb_batch_begin_sent; +/* +* The memory listener has been registered, so DMA maps have been sent to +* the device. +*/ +bool listener_registered; +/* Vdpa must send shadow addresses as IOTLB key for data queues, not GPA */ bool shadow_data;
Signature · 1 line
2.43.5
PATCH v5 5/7 vdpa: reorder listener assignment
In reply to Jonah PalmerFrom: Eugenio Pérez <[email protected]>
Since commit f6fe3e333f ("vdpa: move memory listener to vhost_vdpa_shared") this piece of code repeatedly assign shared->listener members. This was not a problem as it was not used until device start.
However next patches move the listener registration to this vhost_vdpa_init function. When the listener is registered it is added to an embedded linked list, so setting its members again will cause memory corruption to the linked list node.
Do the right thing and only set it in the first vdpa device.
--- hw/virtio/vhost-vdpa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/virtio/vhost-vdpa.c +1 -1
@@ -633,7 +633,6 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) v->dev = dev; dev->opaque = opaque ; - v->shared->listener = vhost_vdpa_memory_listener; ret =vhost_vdpa_set_backend_cap (dev);if (unlikely (ret !=0 )) {
@@ -675,6 +674,7 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp)vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE |VIRTIO_CONFIG_S_DRIVER ); + v->shared->listener = vhost_vdpa_memory_listener;return 0 ; }
Signature · 1 line
2.43.5
PATCH v5 6/7 vdpa: move iova_tree allocation to net_vhost_vdpa_init
In reply to Jonah PalmerFrom: Eugenio Pérez <[email protected]>
As we are moving to keep the mapping through all the vdpa device life instead of resetting it at VirtIO reset, we need to move all its dependencies to the initialization too. In particular devices with x-svq=on need a valid iova_tree from the beginning.
Simplify the code also consolidating the two creation points: the first data vq in case of SVQ active and CVQ start in case only CVQ uses it.
--- include/hw/virtio/vhost-vdpa.h | 16 ++++++++++++++- net/vhost-vdpa.c | 36 +++------------------------------- 2 files changed, 18 insertions(+), 34 deletions(-)
include/hw/virtio/vhost-vdpa.h +15 -1
@@ -43,7 +43,21 @@ typedef struct vhost_vdpa_shared {struct vhost_vdpa_iova_range iova_range; QLIST_HEAD(, vdpa_iommu) iommu_list; -/* IOVA mapping used by the Shadow Virtqueue */ +/* +* IOVA mapping used by the Shadow Virtqueue +* +* It is shared among all ASID for simplicity, whether CVQ shares ASID with +* guest or not: +* - Memory listener need access to guest's memory addresses allocated in +* the IOVA tree. +* - There should be plenty of IOVA address space for both ASID not to +* worry about collisions between them. Guest's translations are still +* validated with virtio virtqueue_pop so there is no risk for the guest +* to access memory that it shouldn't. +* +* To allocate a iova tree per ASID is doable but it complicates the code +* and it is not worth it for the moment. +*/ VhostIOVATree *iova_tree;/* Copy of backend features */
net/vhost-vdpa.c +3 -33
@@ -235,6 +235,7 @@ static void vhost_vdpa_cleanup(NetClientState *nc)return ; }qemu_close (s->vhost_vdpa.shared->device_fd); +g_clear_pointer (&s->vhost_vdpa.shared->iova_tree, vhost_iova_tree_delete);g_free (s->vhost_vdpa.shared); }
@@ -362,16 +363,8 @@ static int vdpa_net_migration_state_notifier(NotifierWithReturn *notifier,static void vhost_vdpa_net_data_start_first (VhostVDPAState *s) { -struct vhost_vdpa *v = &s->vhost_vdpa; -migration_add_notifier (&s->migration_state, vdpa_net_migration_state_notifier); - -/* iova_tree may be initialized by vhost_vdpa_net_load_setup */ -if (v->shadow_vqs_enabled && !v->shared->iova_tree) { - v->shared->iova_tree =vhost_iova_tree_new (v->shared->iova_range.first, - v->shared->iova_range.last); - } }static int vhost_vdpa_net_data_start (NetClientState *nc)
@@ -418,19 +411,12 @@ static int vhost_vdpa_net_data_load(NetClientState *nc)static void vhost_vdpa_net_client_stop (NetClientState *nc) {VhostVDPAState *s =DO_UPCAST (VhostVDPAState, nc, nc); -struct vhost_dev *dev;assert (nc->info->type ==NET_CLIENT_DRIVER_VHOST_VDPA );if (s->vhost_vdpa.index ==0 ) {migration_remove_notifier (&s->migration_state); } - - dev = s->vhost_vdpa.dev; -if (dev->vq_index + dev->nvqs == dev->vq_index_end) { -g_clear_pointer (&s->vhost_vdpa.shared->iova_tree, - vhost_iova_tree_delete); - } }static NetClientInfo net_vhost_vdpa_info = {
@@ -602,24 +588,6 @@ out:return 0 ; } -/* -* If other vhost_vdpa already have an iova_tree, reuse it for simplicity, -* whether CVQ shares ASID with guest or not, because: -* - Memory listener need access to guest's memory addresses allocated in -* the IOVA tree. -* - There should be plenty of IOVA address space for both ASID not to -* worry about collisions between them. Guest's translations are still -* validated with virtio virtqueue_pop so there is no risk for the guest -* to access memory that it shouldn't. -* -* To allocate a iova tree per ASID is doable but it complicates the code -* and it is not worth it for the moment. -*/ -if (!v->shared->iova_tree) { - v->shared->iova_tree =vhost_iova_tree_new (v->shared->iova_range.first, - v->shared->iova_range.last); - } - r =vhost_vdpa_cvq_map_buf (&s->vhost_vdpa, s->cvq_cmd_out_buffer,vhost_vdpa_net_cvq_cmd_page_len (), false);if (unlikely (r <0 )) {
@@ -1728,6 +1696,8 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer, s->vhost_vdpa.shared->device_fd = vdpa_device_fd; s->vhost_vdpa.shared->iova_range = iova_range; s->vhost_vdpa.shared->shadow_data = svq; + s->vhost_vdpa.shared->iova_tree =vhost_iova_tree_new (iova_range.first, + iova_range.last); }else if (!is_datapath) { s->cvq_cmd_out_buffer = mmap(NULL, vhost_vdpa_net_cvq_cmd_page_len(), PROT_READ | PROT_WRITE,
Signature · 1 line
2.43.5
PATCH v5 7/7 vdpa: move memory listener register to vhost_vdpa_init
In reply to Jonah PalmerFrom: Eugenio Pérez <[email protected]>
Current memory operations like pinning may take a lot of time at the destination. Currently they are done after the source of the migration is stopped, and before the workload is resumed at the destination. This is a period where neigher traffic can flow, nor the VM workload can continue (downtime).
We can do better as we know the memory layout of the guest RAM at the destination from the moment that all devices are initializaed. So moving that operation allows QEMU to communicate the kernel the maps while the workload is still running in the source, so Linux can start mapping them.
As a small drawback, there is a time in the initialization where QEMU cannot respond to QMP etc. By some testing, this time is about 0.2seconds. This may be further reduced (or increased) depending on the vdpa driver and the platform hardware, and it is dominated by the cost of memory pinning.
This matches the time that we move out of the called downtime window. The downtime is measured as the elapsed trace time between the last vhost_vdpa_suspend on the source and the last vhost_vdpa_set_vring_enable_one on the destination. In other words, from "guest CPUs freeze" to the instant the final Rx/Tx queue-pair is able to start moving data.
Using ConnectX-6 Dx (MLX5) NICs in vhost-vDPA mode with 8 queue-pairs, the series reduces guest-visible downtime during back-to-back live migrations by more than half:
- 39G VM: 4.72s -> 2.09s (-2.63s, ~56% improvement)
- 128G VM: 14.72s -> 5.83s (-8.89s, ~60% improvement)
---
v4:
Updated performance measurements in the commit log.
v3:
Move memory listener unregistration from vhost_vdpa_reset_status to vhost_vdpa_reset_device. By unregistering the listener here, we can guarantee that every reset leaves the device in an expected state. Also remove the duplicate call in vhost_vdpa_reset_status.
v2:
Move the memory listener registration to vhost_vdpa_set_owner function. In case of hotplug the vdpa device, the memory is already set up, and leaving memory listener register call in the init function made maps occur before set owner call.
To be 100% safe, let's put it right after set_owner call.
--- hw/virtio/vhost-vdpa.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-)
hw/virtio/vhost-vdpa.c +28 -7
@@ -894,8 +894,14 @@ static int vhost_vdpa_reset_device(struct vhost_dev *dev) ret =vhost_vdpa_call (dev,VHOST_VDPA_SET_STATUS , &status);trace_vhost_vdpa_reset_device (dev); +if (ret) { +return ret; + } + +memory_listener_unregister (&v->shared->listener); + v->shared->listener_registered = false; v->suspended = false; -return ret; +return 0 ; }static int vhost_vdpa_get_vq_index (struct vhost_dev *dev,int idx)
@@ -1379,6 +1385,11 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) "IOMMU and try again");return -1 ; } +if (v->shared->listener_registered && + dev->vdev->dma_as != v->shared->listener.address_space) { +memory_listener_unregister (&v->shared->listener); + v->shared->listener_registered = false; + }if (!v->shared->listener_registered) {memory_listener_register (&v->shared->listener, dev->vdev->dma_as); v->shared->listener_registered = true;
@@ -1392,8 +1403,6 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)static void vhost_vdpa_reset_status (struct vhost_dev *dev) { -struct vhost_vdpa *v = dev->opaque; -if (!vhost_vdpa_last_dev (dev)) {return ; }
@@ -1401,9 +1410,6 @@ static void vhost_vdpa_reset_status(struct vhost_dev *dev)vhost_vdpa_reset_device (dev);vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE |VIRTIO_CONFIG_S_DRIVER ); -memory_listener_unregister (&v->shared->listener); - v->shared->listener_registered = false; - } static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base,
@@ -1537,12 +1543,27 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev,static int vhost_vdpa_set_owner (struct vhost_dev *dev) { +int r; +struct vhost_vdpa *v; +if (!vhost_vdpa_first_dev (dev)) {return 0 ; }trace_vhost_vdpa_set_owner (dev); -return vhost_vdpa_call (dev,VHOST_SET_OWNER ,NULL ); + r =vhost_vdpa_call (dev,VHOST_SET_OWNER ,NULL ); +if (unlikely (r <0 )) { +return r; + } + +/* +* Being optimistic and listening address space memory. If the device +* uses vIOMMU, it is changed at vhost_vdpa_dev_start. +*/ + v = dev->opaque; +memory_listener_register (&v->shared->listener, &address_space_memory); + v->shared->listener_registered = true; +return 0 ; } static int vhost_vdpa_vq_get_addr(struct vhost_dev *dev,
Signature · 1 line
2.43.5